uboot/drivers/pci/pci-uclass.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Copyright (c) 2014 Google, Inc
   4 * Written by Simon Glass <sjg@chromium.org>
   5 */
   6
   7#define LOG_CATEGORY UCLASS_PCI
   8
   9#include <common.h>
  10#include <dm.h>
  11#include <errno.h>
  12#include <init.h>
  13#include <log.h>
  14#include <malloc.h>
  15#include <pci.h>
  16#include <asm/global_data.h>
  17#include <asm/io.h>
  18#include <dm/device-internal.h>
  19#include <dm/lists.h>
  20#include <dm/uclass-internal.h>
  21#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
  22#include <asm/fsp/fsp_support.h>
  23#endif
  24#include <dt-bindings/pci/pci.h>
  25#include <linux/delay.h>
  26#include "pci_internal.h"
  27
  28DECLARE_GLOBAL_DATA_PTR;
  29
  30int pci_get_bus(int busnum, struct udevice **busp)
  31{
  32        int ret;
  33
  34        ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
  35
  36        /* Since buses may not be numbered yet try a little harder with bus 0 */
  37        if (ret == -ENODEV) {
  38                ret = uclass_first_device_err(UCLASS_PCI, busp);
  39                if (ret)
  40                        return ret;
  41                ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
  42        }
  43
  44        return ret;
  45}
  46
  47struct udevice *pci_get_controller(struct udevice *dev)
  48{
  49        while (device_is_on_pci_bus(dev))
  50                dev = dev->parent;
  51
  52        return dev;
  53}
  54
  55pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
  56{
  57        struct pci_child_plat *pplat = dev_get_parent_plat(dev);
  58        struct udevice *bus = dev->parent;
  59
  60        /*
  61         * This error indicates that @dev is a device on an unprobed PCI bus.
  62         * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below
  63         * will produce a bad BDF>
  64         *
  65         * A common cause of this problem is that this function is called in the
  66         * of_to_plat() method of @dev. Accessing the PCI bus in that
  67         * method is not allowed, since it has not yet been probed. To fix this,
  68         * move that access to the probe() method of @dev instead.
  69         */
  70        if (!device_active(bus))
  71                log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name,
  72                        bus->name);
  73        return PCI_ADD_BUS(dev_seq(bus), pplat->devfn);
  74}
  75
  76/**
  77 * pci_get_bus_max() - returns the bus number of the last active bus
  78 *
  79 * Return: last bus number, or -1 if no active buses
  80 */
  81static int pci_get_bus_max(void)
  82{
  83        struct udevice *bus;
  84        struct uclass *uc;
  85        int ret = -1;
  86
  87        ret = uclass_get(UCLASS_PCI, &uc);
  88        uclass_foreach_dev(bus, uc) {
  89                if (dev_seq(bus) > ret)
  90                        ret = dev_seq(bus);
  91        }
  92
  93        debug("%s: ret=%d\n", __func__, ret);
  94
  95        return ret;
  96}
  97
  98int pci_last_busno(void)
  99{
 100        return pci_get_bus_max();
 101}
 102
 103int pci_get_ff(enum pci_size_t size)
 104{
 105        switch (size) {
 106        case PCI_SIZE_8:
 107                return 0xff;
 108        case PCI_SIZE_16:
 109                return 0xffff;
 110        default:
 111                return 0xffffffff;
 112        }
 113}
 114
 115static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
 116                                ofnode *rnode)
 117{
 118        struct fdt_pci_addr addr;
 119        ofnode node;
 120        int ret;
 121
 122        dev_for_each_subnode(node, bus) {
 123                ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
 124                                           &addr);
 125                if (ret)
 126                        continue;
 127
 128                if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
 129                        continue;
 130
 131                *rnode = node;
 132                break;
 133        }
 134};
 135
 136int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
 137                       struct udevice **devp)
 138{
 139        struct udevice *dev;
 140
 141        for (device_find_first_child(bus, &dev);
 142             dev;
 143             device_find_next_child(&dev)) {
 144                struct pci_child_plat *pplat;
 145
 146                pplat = dev_get_parent_plat(dev);
 147                if (pplat && pplat->devfn == find_devfn) {
 148                        *devp = dev;
 149                        return 0;
 150                }
 151        }
 152
 153        return -ENODEV;
 154}
 155
 156int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
 157{
 158        struct udevice *bus;
 159        int ret;
 160
 161        ret = pci_get_bus(PCI_BUS(bdf), &bus);
 162        if (ret)
 163                return ret;
 164        return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
 165}
 166
 167static int pci_device_matches_ids(struct udevice *dev,
 168                                  const struct pci_device_id *ids)
 169{
 170        struct pci_child_plat *pplat;
 171        int i;
 172
 173        pplat = dev_get_parent_plat(dev);
 174        if (!pplat)
 175                return -EINVAL;
 176        for (i = 0; ids[i].vendor != 0; i++) {
 177                if (pplat->vendor == ids[i].vendor &&
 178                    pplat->device == ids[i].device)
 179                        return i;
 180        }
 181
 182        return -EINVAL;
 183}
 184
 185int pci_bus_find_devices(struct udevice *bus, const struct pci_device_id *ids,
 186                         int *indexp, struct udevice **devp)
 187{
 188        struct udevice *dev;
 189
 190        /* Scan all devices on this bus */
 191        for (device_find_first_child(bus, &dev);
 192             dev;
 193             device_find_next_child(&dev)) {
 194                if (pci_device_matches_ids(dev, ids) >= 0) {
 195                        if ((*indexp)-- <= 0) {
 196                                *devp = dev;
 197                                return 0;
 198                        }
 199                }
 200        }
 201
 202        return -ENODEV;
 203}
 204
 205int pci_find_device_id(const struct pci_device_id *ids, int index,
 206                       struct udevice **devp)
 207{
 208        struct udevice *bus;
 209
 210        /* Scan all known buses */
 211        for (uclass_first_device(UCLASS_PCI, &bus);
 212             bus;
 213             uclass_next_device(&bus)) {
 214                if (!pci_bus_find_devices(bus, ids, &index, devp))
 215                        return 0;
 216        }
 217        *devp = NULL;
 218
 219        return -ENODEV;
 220}
 221
 222static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
 223                                  unsigned int device, int *indexp,
 224                                  struct udevice **devp)
 225{
 226        struct pci_child_plat *pplat;
 227        struct udevice *dev;
 228
 229        for (device_find_first_child(bus, &dev);
 230             dev;
 231             device_find_next_child(&dev)) {
 232                pplat = dev_get_parent_plat(dev);
 233                if (pplat->vendor == vendor && pplat->device == device) {
 234                        if (!(*indexp)--) {
 235                                *devp = dev;
 236                                return 0;
 237                        }
 238                }
 239        }
 240
 241        return -ENODEV;
 242}
 243
 244int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
 245                       struct udevice **devp)
 246{
 247        struct udevice *bus;
 248
 249        /* Scan all known buses */
 250        for (uclass_first_device(UCLASS_PCI, &bus);
 251             bus;
 252             uclass_next_device(&bus)) {
 253                if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
 254                        return device_probe(*devp);
 255        }
 256        *devp = NULL;
 257
 258        return -ENODEV;
 259}
 260
 261int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
 262{
 263        struct udevice *dev;
 264
 265        /* Scan all known buses */
 266        for (pci_find_first_device(&dev);
 267             dev;
 268             pci_find_next_device(&dev)) {
 269                struct pci_child_plat *pplat = dev_get_parent_plat(dev);
 270
 271                if (pplat->class == find_class && !index--) {
 272                        *devp = dev;
 273                        return device_probe(*devp);
 274                }
 275        }
 276        *devp = NULL;
 277
 278        return -ENODEV;
 279}
 280
 281int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
 282                         unsigned long value, enum pci_size_t size)
 283{
 284        struct dm_pci_ops *ops;
 285
 286        ops = pci_get_ops(bus);
 287        if (!ops->write_config)
 288                return -ENOSYS;
 289        return ops->write_config(bus, bdf, offset, value, size);
 290}
 291
 292int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
 293                            u32 clr, u32 set)
 294{
 295        ulong val;
 296        int ret;
 297
 298        ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
 299        if (ret)
 300                return ret;
 301        val &= ~clr;
 302        val |= set;
 303
 304        return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
 305}
 306
 307static int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
 308                            enum pci_size_t size)
 309{
 310        struct udevice *bus;
 311        int ret;
 312
 313        ret = pci_get_bus(PCI_BUS(bdf), &bus);
 314        if (ret)
 315                return ret;
 316
 317        return pci_bus_write_config(bus, bdf, offset, value, size);
 318}
 319
 320int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
 321                        enum pci_size_t size)
 322{
 323        struct udevice *bus;
 324
 325        for (bus = dev; device_is_on_pci_bus(bus);)
 326                bus = bus->parent;
 327        return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
 328                                    size);
 329}
 330
 331int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
 332{
 333        return pci_write_config(bdf, offset, value, PCI_SIZE_32);
 334}
 335
 336int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
 337{
 338        return pci_write_config(bdf, offset, value, PCI_SIZE_16);
 339}
 340
 341int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
 342{
 343        return pci_write_config(bdf, offset, value, PCI_SIZE_8);
 344}
 345
 346int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
 347{
 348        return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
 349}
 350
 351int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
 352{
 353        return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
 354}
 355
 356int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
 357{
 358        return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
 359}
 360
 361int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
 362                        unsigned long *valuep, enum pci_size_t size)
 363{
 364        struct dm_pci_ops *ops;
 365
 366        ops = pci_get_ops(bus);
 367        if (!ops->read_config)
 368                return -ENOSYS;
 369        return ops->read_config(bus, bdf, offset, valuep, size);
 370}
 371
 372static int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
 373                           enum pci_size_t size)
 374{
 375        struct udevice *bus;
 376        int ret;
 377
 378        ret = pci_get_bus(PCI_BUS(bdf), &bus);
 379        if (ret)
 380                return ret;
 381
 382        return pci_bus_read_config(bus, bdf, offset, valuep, size);
 383}
 384
 385int dm_pci_read_config(const struct udevice *dev, int offset,
 386                       unsigned long *valuep, enum pci_size_t size)
 387{
 388        const struct udevice *bus;
 389
 390        for (bus = dev; device_is_on_pci_bus(bus);)
 391                bus = bus->parent;
 392        return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
 393                                   size);
 394}
 395
 396int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
 397{
 398        unsigned long value;
 399        int ret;
 400
 401        ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
 402        if (ret)
 403                return ret;
 404        *valuep = value;
 405
 406        return 0;
 407}
 408
 409int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
 410{
 411        unsigned long value;
 412        int ret;
 413
 414        ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
 415        if (ret)
 416                return ret;
 417        *valuep = value;
 418
 419        return 0;
 420}
 421
 422int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
 423{
 424        unsigned long value;
 425        int ret;
 426
 427        ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
 428        if (ret)
 429                return ret;
 430        *valuep = value;
 431
 432        return 0;
 433}
 434
 435int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
 436{
 437        unsigned long value;
 438        int ret;
 439
 440        ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
 441        if (ret)
 442                return ret;
 443        *valuep = value;
 444
 445        return 0;
 446}
 447
 448int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
 449{
 450        unsigned long value;
 451        int ret;
 452
 453        ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
 454        if (ret)
 455                return ret;
 456        *valuep = value;
 457
 458        return 0;
 459}
 460
 461int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
 462{
 463        unsigned long value;
 464        int ret;
 465
 466        ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
 467        if (ret)
 468                return ret;
 469        *valuep = value;
 470
 471        return 0;
 472}
 473
 474int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
 475{
 476        u8 val;
 477        int ret;
 478
 479        ret = dm_pci_read_config8(dev, offset, &val);
 480        if (ret)
 481                return ret;
 482        val &= ~clr;
 483        val |= set;
 484
 485        return dm_pci_write_config8(dev, offset, val);
 486}
 487
 488int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
 489{
 490        u16 val;
 491        int ret;
 492
 493        ret = dm_pci_read_config16(dev, offset, &val);
 494        if (ret)
 495                return ret;
 496        val &= ~clr;
 497        val |= set;
 498
 499        return dm_pci_write_config16(dev, offset, val);
 500}
 501
 502int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
 503{
 504        u32 val;
 505        int ret;
 506
 507        ret = dm_pci_read_config32(dev, offset, &val);
 508        if (ret)
 509                return ret;
 510        val &= ~clr;
 511        val |= set;
 512
 513        return dm_pci_write_config32(dev, offset, val);
 514}
 515
 516static void set_vga_bridge_bits(struct udevice *dev)
 517{
 518        struct udevice *parent = dev->parent;
 519        u16 bc;
 520
 521        while (dev_seq(parent) != 0) {
 522                dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
 523                bc |= PCI_BRIDGE_CTL_VGA;
 524                dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
 525                parent = parent->parent;
 526        }
 527}
 528
 529int pci_auto_config_devices(struct udevice *bus)
 530{
 531        struct pci_controller *hose = dev_get_uclass_priv(bus);
 532        struct pci_child_plat *pplat;
 533        unsigned int sub_bus;
 534        struct udevice *dev;
 535        int ret;
 536
 537        sub_bus = dev_seq(bus);
 538        debug("%s: start\n", __func__);
 539        pciauto_config_init(hose);
 540        for (ret = device_find_first_child(bus, &dev);
 541             !ret && dev;
 542             ret = device_find_next_child(&dev)) {
 543                unsigned int max_bus;
 544                int ret;
 545
 546                debug("%s: device %s\n", __func__, dev->name);
 547                if (dev_has_ofnode(dev) &&
 548                    dev_read_bool(dev, "pci,no-autoconfig"))
 549                        continue;
 550                ret = dm_pciauto_config_device(dev);
 551                if (ret < 0)
 552                        return log_msg_ret("auto", ret);
 553                max_bus = ret;
 554                sub_bus = max(sub_bus, max_bus);
 555
 556                if (dev_get_parent(dev) == bus)
 557                        continue;
 558
 559                pplat = dev_get_parent_plat(dev);
 560                if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
 561                        set_vga_bridge_bits(dev);
 562        }
 563        if (hose->last_busno < sub_bus)
 564                hose->last_busno = sub_bus;
 565        debug("%s: done\n", __func__);
 566
 567        return log_msg_ret("sub", sub_bus);
 568}
 569
 570int pci_generic_mmap_write_config(
 571        const struct udevice *bus,
 572        int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
 573                      void **addrp),
 574        pci_dev_t bdf,
 575        uint offset,
 576        ulong value,
 577        enum pci_size_t size)
 578{
 579        void *address;
 580
 581        if (addr_f(bus, bdf, offset, &address) < 0)
 582                return 0;
 583
 584        switch (size) {
 585        case PCI_SIZE_8:
 586                writeb(value, address);
 587                return 0;
 588        case PCI_SIZE_16:
 589                writew(value, address);
 590                return 0;
 591        case PCI_SIZE_32:
 592                writel(value, address);
 593                return 0;
 594        default:
 595                return -EINVAL;
 596        }
 597}
 598
 599int pci_generic_mmap_read_config(
 600        const struct udevice *bus,
 601        int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
 602                      void **addrp),
 603        pci_dev_t bdf,
 604        uint offset,
 605        ulong *valuep,
 606        enum pci_size_t size)
 607{
 608        void *address;
 609
 610        if (addr_f(bus, bdf, offset, &address) < 0) {
 611                *valuep = pci_get_ff(size);
 612                return 0;
 613        }
 614
 615        switch (size) {
 616        case PCI_SIZE_8:
 617                *valuep = readb(address);
 618                return 0;
 619        case PCI_SIZE_16:
 620                *valuep = readw(address);
 621                return 0;
 622        case PCI_SIZE_32:
 623                *valuep = readl(address);
 624                return 0;
 625        default:
 626                return -EINVAL;
 627        }
 628}
 629
 630int dm_pci_hose_probe_bus(struct udevice *bus)
 631{
 632        u8 header_type;
 633        int sub_bus;
 634        int ret;
 635        int ea_pos;
 636        u8 reg;
 637
 638        debug("%s\n", __func__);
 639
 640        dm_pci_read_config8(bus, PCI_HEADER_TYPE, &header_type);
 641        header_type &= 0x7f;
 642        if (header_type != PCI_HEADER_TYPE_BRIDGE) {
 643                debug("%s: Skipping PCI device %d with Non-Bridge Header Type 0x%x\n",
 644                      __func__, PCI_DEV(dm_pci_get_bdf(bus)), header_type);
 645                return log_msg_ret("probe", -EINVAL);
 646        }
 647
 648        if (IS_ENABLED(CONFIG_PCI_ENHANCED_ALLOCATION))
 649                ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA);
 650        else
 651                ea_pos = 0;
 652
 653        if (ea_pos) {
 654                dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8),
 655                                    &reg);
 656                sub_bus = reg;
 657        } else {
 658                sub_bus = pci_get_bus_max() + 1;
 659        }
 660        debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
 661        dm_pciauto_prescan_setup_bridge(bus, sub_bus);
 662
 663        ret = device_probe(bus);
 664        if (ret) {
 665                debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
 666                      ret);
 667                return log_msg_ret("probe", ret);
 668        }
 669
 670        if (!ea_pos)
 671                sub_bus = pci_get_bus_max();
 672
 673        dm_pciauto_postscan_setup_bridge(bus, sub_bus);
 674
 675        return sub_bus;
 676}
 677
 678/**
 679 * pci_match_one_device - Tell if a PCI device structure has a matching
 680 *                        PCI device id structure
 681 * @id: single PCI device id structure to match
 682 * @find: the PCI device id structure to match against
 683 *
 684 * Returns true if the finding pci_device_id structure matched or false if
 685 * there is no match.
 686 */
 687static bool pci_match_one_id(const struct pci_device_id *id,
 688                             const struct pci_device_id *find)
 689{
 690        if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
 691            (id->device == PCI_ANY_ID || id->device == find->device) &&
 692            (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
 693            (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
 694            !((id->class ^ find->class) & id->class_mask))
 695                return true;
 696
 697        return false;
 698}
 699
 700/**
 701 * pci_need_device_pre_reloc() - Check if a device should be bound
 702 *
 703 * This checks a list of vendor/device-ID values indicating devices that should
 704 * be bound before relocation.
 705 *
 706 * @bus: Bus to check
 707 * @vendor: Vendor ID to check
 708 * @device: Device ID to check
 709 * Return: true if the vendor/device is in the list, false if not
 710 */
 711static bool pci_need_device_pre_reloc(struct udevice *bus, uint vendor,
 712                                      uint device)
 713{
 714        u32 vendev;
 715        int index;
 716
 717        for (index = 0;
 718             !dev_read_u32_index(bus, "u-boot,pci-pre-reloc", index,
 719                                 &vendev);
 720             index++) {
 721                if (vendev == PCI_VENDEV(vendor, device))
 722                        return true;
 723        }
 724
 725        return false;
 726}
 727
 728/**
 729 * pci_find_and_bind_driver() - Find and bind the right PCI driver
 730 *
 731 * This only looks at certain fields in the descriptor.
 732 *
 733 * @parent:     Parent bus
 734 * @find_id:    Specification of the driver to find
 735 * @bdf:        Bus/device/function addreess - see PCI_BDF()
 736 * @devp:       Returns a pointer to the device created
 737 * Return: 0 if OK, -EPERM if the device is not needed before relocation and
 738 *         therefore was not created, other -ve value on error
 739 */
 740static int pci_find_and_bind_driver(struct udevice *parent,
 741                                    struct pci_device_id *find_id,
 742                                    pci_dev_t bdf, struct udevice **devp)
 743{
 744        struct pci_driver_entry *start, *entry;
 745        ofnode node = ofnode_null();
 746        const char *drv;
 747        int n_ents;
 748        int ret;
 749        char name[30], *str;
 750        bool bridge;
 751
 752        *devp = NULL;
 753
 754        debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
 755              find_id->vendor, find_id->device);
 756
 757        /* Determine optional OF node */
 758        if (ofnode_valid(dev_ofnode(parent)))
 759                pci_dev_find_ofnode(parent, bdf, &node);
 760
 761        if (ofnode_valid(node) && !ofnode_is_available(node)) {
 762                debug("%s: Ignoring disabled device\n", __func__);
 763                return log_msg_ret("dis", -EPERM);
 764        }
 765
 766        start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
 767        n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
 768        for (entry = start; entry != start + n_ents; entry++) {
 769                const struct pci_device_id *id;
 770                struct udevice *dev;
 771                const struct driver *drv;
 772
 773                for (id = entry->match;
 774                     id->vendor || id->subvendor || id->class_mask;
 775                     id++) {
 776                        if (!pci_match_one_id(id, find_id))
 777                                continue;
 778
 779                        drv = entry->driver;
 780
 781                        /*
 782                         * In the pre-relocation phase, we only bind devices
 783                         * whose driver has the DM_FLAG_PRE_RELOC set, to save
 784                         * precious memory space as on some platforms as that
 785                         * space is pretty limited (ie: using Cache As RAM).
 786                         */
 787                        if (!(gd->flags & GD_FLG_RELOC) &&
 788                            !(drv->flags & DM_FLAG_PRE_RELOC))
 789                                return log_msg_ret("pre", -EPERM);
 790
 791                        /*
 792                         * We could pass the descriptor to the driver as
 793                         * plat (instead of NULL) and allow its bind()
 794                         * method to return -ENOENT if it doesn't support this
 795                         * device. That way we could continue the search to
 796                         * find another driver. For now this doesn't seem
 797                         * necesssary, so just bind the first match.
 798                         */
 799                        ret = device_bind(parent, drv, drv->name, NULL, node,
 800                                          &dev);
 801                        if (ret)
 802                                goto error;
 803                        debug("%s: Match found: %s\n", __func__, drv->name);
 804                        dev->driver_data = id->driver_data;
 805                        *devp = dev;
 806                        return 0;
 807                }
 808        }
 809
 810        bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
 811        /*
 812         * In the pre-relocation phase, we only bind bridge devices to save
 813         * precious memory space as on some platforms as that space is pretty
 814         * limited (ie: using Cache As RAM).
 815         */
 816        if (!(gd->flags & GD_FLG_RELOC) && !bridge &&
 817            !pci_need_device_pre_reloc(parent, find_id->vendor,
 818                                       find_id->device))
 819                return log_msg_ret("notbr", -EPERM);
 820
 821        /* Bind a generic driver so that the device can be used */
 822        sprintf(name, "pci_%x:%x.%x", dev_seq(parent), PCI_DEV(bdf),
 823                PCI_FUNC(bdf));
 824        str = strdup(name);
 825        if (!str)
 826                return -ENOMEM;
 827        drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
 828
 829        ret = device_bind_driver_to_node(parent, drv, str, node, devp);
 830        if (ret) {
 831                debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
 832                free(str);
 833                return ret;
 834        }
 835        debug("%s: No match found: bound generic driver instead\n", __func__);
 836
 837        return 0;
 838
 839error:
 840        debug("%s: No match found: error %d\n", __func__, ret);
 841        return ret;
 842}
 843
 844__weak extern void board_pci_fixup_dev(struct udevice *bus, struct udevice *dev)
 845{
 846}
 847
 848int pci_bind_bus_devices(struct udevice *bus)
 849{
 850        ulong vendor, device;
 851        ulong header_type;
 852        pci_dev_t bdf, end;
 853        bool found_multi;
 854        int ari_off;
 855        int ret;
 856
 857        found_multi = false;
 858        end = PCI_BDF(dev_seq(bus), PCI_MAX_PCI_DEVICES - 1,
 859                      PCI_MAX_PCI_FUNCTIONS - 1);
 860        for (bdf = PCI_BDF(dev_seq(bus), 0, 0); bdf <= end;
 861             bdf += PCI_BDF(0, 0, 1)) {
 862                struct pci_child_plat *pplat;
 863                struct udevice *dev;
 864                ulong class;
 865
 866                if (!PCI_FUNC(bdf))
 867                        found_multi = false;
 868                if (PCI_FUNC(bdf) && !found_multi)
 869                        continue;
 870
 871                /* Check only the first access, we don't expect problems */
 872                ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
 873                                          PCI_SIZE_16);
 874                if (ret || vendor == 0xffff || vendor == 0x0000)
 875                        continue;
 876
 877                pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
 878                                    &header_type, PCI_SIZE_8);
 879
 880                if (!PCI_FUNC(bdf))
 881                        found_multi = header_type & 0x80;
 882
 883                debug("%s: bus %d/%s: found device %x, function %d", __func__,
 884                      dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
 885                pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
 886                                    PCI_SIZE_16);
 887                pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
 888                                    PCI_SIZE_32);
 889                class >>= 8;
 890
 891                /* Find this device in the device tree */
 892                ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
 893                debug(": find ret=%d\n", ret);
 894
 895                /* If nothing in the device tree, bind a device */
 896                if (ret == -ENODEV) {
 897                        struct pci_device_id find_id;
 898                        ulong val;
 899
 900                        memset(&find_id, '\0', sizeof(find_id));
 901                        find_id.vendor = vendor;
 902                        find_id.device = device;
 903                        find_id.class = class;
 904                        if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
 905                                pci_bus_read_config(bus, bdf,
 906                                                    PCI_SUBSYSTEM_VENDOR_ID,
 907                                                    &val, PCI_SIZE_32);
 908                                find_id.subvendor = val & 0xffff;
 909                                find_id.subdevice = val >> 16;
 910                        }
 911                        ret = pci_find_and_bind_driver(bus, &find_id, bdf,
 912                                                       &dev);
 913                }
 914                if (ret == -EPERM)
 915                        continue;
 916                else if (ret)
 917                        return ret;
 918
 919                /* Update the platform data */
 920                pplat = dev_get_parent_plat(dev);
 921                pplat->devfn = PCI_MASK_BUS(bdf);
 922                pplat->vendor = vendor;
 923                pplat->device = device;
 924                pplat->class = class;
 925
 926                if (IS_ENABLED(CONFIG_PCI_ARID)) {
 927                        ari_off = dm_pci_find_ext_capability(dev,
 928                                                             PCI_EXT_CAP_ID_ARI);
 929                        if (ari_off) {
 930                                u16 ari_cap;
 931
 932                                /*
 933                                 * Read Next Function number in ARI Cap
 934                                 * Register
 935                                 */
 936                                dm_pci_read_config16(dev, ari_off + 4,
 937                                                     &ari_cap);
 938                                /*
 939                                 * Update next scan on this function number,
 940                                 * subtract 1 in BDF to satisfy loop increment.
 941                                 */
 942                                if (ari_cap & 0xff00) {
 943                                        bdf = PCI_BDF(PCI_BUS(bdf),
 944                                                      PCI_DEV(ari_cap),
 945                                                      PCI_FUNC(ari_cap));
 946                                        bdf = bdf - 0x100;
 947                                }
 948                        }
 949                }
 950
 951                board_pci_fixup_dev(bus, dev);
 952        }
 953
 954        return 0;
 955}
 956
 957static void decode_regions(struct pci_controller *hose, ofnode parent_node,
 958                           ofnode node)
 959{
 960        int pci_addr_cells, addr_cells, size_cells;
 961        int cells_per_record;
 962        struct bd_info *bd;
 963        const u32 *prop;
 964        int max_regions;
 965        int len;
 966        int i;
 967
 968        prop = ofnode_get_property(node, "ranges", &len);
 969        if (!prop) {
 970                debug("%s: Cannot decode regions\n", __func__);
 971                return;
 972        }
 973
 974        pci_addr_cells = ofnode_read_simple_addr_cells(node);
 975        addr_cells = ofnode_read_simple_addr_cells(parent_node);
 976        size_cells = ofnode_read_simple_size_cells(node);
 977
 978        /* PCI addresses are always 3-cells */
 979        len /= sizeof(u32);
 980        cells_per_record = pci_addr_cells + addr_cells + size_cells;
 981        hose->region_count = 0;
 982        debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
 983              cells_per_record);
 984
 985        /* Dynamically allocate the regions array */
 986        max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS;
 987        hose->regions = (struct pci_region *)
 988                calloc(1, max_regions * sizeof(struct pci_region));
 989
 990        for (i = 0; i < max_regions; i++, len -= cells_per_record) {
 991                u64 pci_addr, addr, size;
 992                int space_code;
 993                u32 flags;
 994                int type;
 995                int pos;
 996
 997                if (len < cells_per_record)
 998                        break;
 999                flags = fdt32_to_cpu(prop[0]);
1000                space_code = (flags >> 24) & 3;
1001                pci_addr = fdtdec_get_number(prop + 1, 2);
1002                prop += pci_addr_cells;
1003                addr = fdtdec_get_number(prop, addr_cells);
1004                prop += addr_cells;
1005                size = fdtdec_get_number(prop, size_cells);
1006                prop += size_cells;
1007                debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
1008                      __func__, hose->region_count, pci_addr, addr, size, space_code);
1009                if (space_code & 2) {
1010                        type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
1011                                        PCI_REGION_MEM;
1012                } else if (space_code & 1) {
1013                        type = PCI_REGION_IO;
1014                } else {
1015                        continue;
1016                }
1017
1018                if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
1019                    type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
1020                        debug(" - pci_addr beyond the 32-bit boundary, ignoring\n");
1021                        continue;
1022                }
1023
1024                if (!IS_ENABLED(CONFIG_PHYS_64BIT) && upper_32_bits(addr)) {
1025                        debug(" - addr beyond the 32-bit boundary, ignoring\n");
1026                        continue;
1027                }
1028
1029                if (~((pci_addr_t)0) - pci_addr < size) {
1030                        debug(" - PCI range exceeds max address, ignoring\n");
1031                        continue;
1032                }
1033
1034                if (~((phys_addr_t)0) - addr < size) {
1035                        debug(" - phys range exceeds max address, ignoring\n");
1036                        continue;
1037                }
1038
1039                pos = -1;
1040                if (!IS_ENABLED(CONFIG_PCI_REGION_MULTI_ENTRY)) {
1041                        for (i = 0; i < hose->region_count; i++) {
1042                                if (hose->regions[i].flags == type)
1043                                        pos = i;
1044                        }
1045                }
1046
1047                if (pos == -1)
1048                        pos = hose->region_count++;
1049                debug(" - type=%d, pos=%d\n", type, pos);
1050                pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
1051        }
1052
1053        /* Add a region for our local memory */
1054        bd = gd->bd;
1055        if (!bd)
1056                return;
1057
1058        for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
1059                if (bd->bi_dram[i].size) {
1060                        phys_addr_t start = bd->bi_dram[i].start;
1061
1062                        if (IS_ENABLED(CONFIG_PCI_MAP_SYSTEM_MEMORY))
1063                                start = virt_to_phys((void *)(uintptr_t)bd->bi_dram[i].start);
1064
1065                        pci_set_region(hose->regions + hose->region_count++,
1066                                       start, start, bd->bi_dram[i].size,
1067                                       PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
1068                }
1069        }
1070
1071        return;
1072}
1073
1074static int pci_uclass_pre_probe(struct udevice *bus)
1075{
1076        struct pci_controller *hose;
1077        struct uclass *uc;
1078        int ret;
1079
1080        debug("%s, bus=%d/%s, parent=%s\n", __func__, dev_seq(bus), bus->name,
1081              bus->parent->name);
1082        hose = dev_get_uclass_priv(bus);
1083
1084        /*
1085         * Set the sequence number, if device_bind() doesn't. We want control
1086         * of this so that numbers are allocated as devices are probed. That
1087         * ensures that sub-bus numbered is correct (sub-buses must get numbers
1088         * higher than their parents)
1089         */
1090        if (dev_seq(bus) == -1) {
1091                ret = uclass_get(UCLASS_PCI, &uc);
1092                if (ret)
1093                        return ret;
1094                bus->seq_ = uclass_find_next_free_seq(uc);
1095        }
1096
1097        /* For bridges, use the top-level PCI controller */
1098        if (!device_is_on_pci_bus(bus)) {
1099                hose->ctlr = bus;
1100                decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
1101        } else {
1102                struct pci_controller *parent_hose;
1103
1104                parent_hose = dev_get_uclass_priv(bus->parent);
1105                hose->ctlr = parent_hose->bus;
1106        }
1107
1108        hose->bus = bus;
1109        hose->first_busno = dev_seq(bus);
1110        hose->last_busno = dev_seq(bus);
1111        if (dev_has_ofnode(bus)) {
1112                hose->skip_auto_config_until_reloc =
1113                        dev_read_bool(bus,
1114                                      "u-boot,skip-auto-config-until-reloc");
1115        }
1116
1117        return 0;
1118}
1119
1120static int pci_uclass_post_probe(struct udevice *bus)
1121{
1122        struct pci_controller *hose = dev_get_uclass_priv(bus);
1123        int ret;
1124
1125        debug("%s: probing bus %d\n", __func__, dev_seq(bus));
1126        ret = pci_bind_bus_devices(bus);
1127        if (ret)
1128                return log_msg_ret("bind", ret);
1129
1130        if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
1131            (!hose->skip_auto_config_until_reloc ||
1132             (gd->flags & GD_FLG_RELOC))) {
1133                ret = pci_auto_config_devices(bus);
1134                if (ret < 0)
1135                        return log_msg_ret("cfg", ret);
1136        }
1137
1138#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1139        /*
1140         * Per Intel FSP specification, we should call FSP notify API to
1141         * inform FSP that PCI enumeration has been done so that FSP will
1142         * do any necessary initialization as required by the chipset's
1143         * BIOS Writer's Guide (BWG).
1144         *
1145         * Unfortunately we have to put this call here as with driver model,
1146         * the enumeration is all done on a lazy basis as needed, so until
1147         * something is touched on PCI it won't happen.
1148         *
1149         * Note we only call this 1) after U-Boot is relocated, and 2)
1150         * root bus has finished probing.
1151         */
1152        if ((gd->flags & GD_FLG_RELOC) && dev_seq(bus) == 0 && ll_boot_init()) {
1153                ret = fsp_init_phase_pci();
1154                if (ret)
1155                        return log_msg_ret("fsp", ret);
1156        }
1157#endif
1158
1159        return 0;
1160}
1161
1162static int pci_uclass_child_post_bind(struct udevice *dev)
1163{
1164        struct pci_child_plat *pplat;
1165
1166        if (!dev_has_ofnode(dev))
1167                return 0;
1168
1169        pplat = dev_get_parent_plat(dev);
1170
1171        /* Extract vendor id and device id if available */
1172        ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1173
1174        /* Extract the devfn from fdt_pci_addr */
1175        pplat->devfn = pci_get_devfn(dev);
1176
1177        return 0;
1178}
1179
1180static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
1181                                  uint offset, ulong *valuep,
1182                                  enum pci_size_t size)
1183{
1184        struct pci_controller *hose = dev_get_uclass_priv(bus);
1185
1186        return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1187}
1188
1189static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1190                                   uint offset, ulong value,
1191                                   enum pci_size_t size)
1192{
1193        struct pci_controller *hose = dev_get_uclass_priv(bus);
1194
1195        return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1196}
1197
1198static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1199{
1200        struct udevice *dev;
1201        int ret = 0;
1202
1203        /*
1204         * Scan through all the PCI controllers. On x86 there will only be one
1205         * but that is not necessarily true on other hardware.
1206         */
1207        do {
1208                device_find_first_child(bus, &dev);
1209                if (dev) {
1210                        *devp = dev;
1211                        return 0;
1212                }
1213                ret = uclass_next_device(&bus);
1214                if (ret)
1215                        return ret;
1216        } while (bus);
1217
1218        return 0;
1219}
1220
1221int pci_find_next_device(struct udevice **devp)
1222{
1223        struct udevice *child = *devp;
1224        struct udevice *bus = child->parent;
1225        int ret;
1226
1227        /* First try all the siblings */
1228        *devp = NULL;
1229        while (child) {
1230                device_find_next_child(&child);
1231                if (child) {
1232                        *devp = child;
1233                        return 0;
1234                }
1235        }
1236
1237        /* We ran out of siblings. Try the next bus */
1238        ret = uclass_next_device(&bus);
1239        if (ret)
1240                return ret;
1241
1242        return bus ? skip_to_next_device(bus, devp) : 0;
1243}
1244
1245int pci_find_first_device(struct udevice **devp)
1246{
1247        struct udevice *bus;
1248        int ret;
1249
1250        *devp = NULL;
1251        ret = uclass_first_device(UCLASS_PCI, &bus);
1252        if (ret)
1253                return ret;
1254
1255        return skip_to_next_device(bus, devp);
1256}
1257
1258ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1259{
1260        switch (size) {
1261        case PCI_SIZE_8:
1262                return (value >> ((offset & 3) * 8)) & 0xff;
1263        case PCI_SIZE_16:
1264                return (value >> ((offset & 2) * 8)) & 0xffff;
1265        default:
1266                return value;
1267        }
1268}
1269
1270ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1271                          enum pci_size_t size)
1272{
1273        uint off_mask;
1274        uint val_mask, shift;
1275        ulong ldata, mask;
1276
1277        switch (size) {
1278        case PCI_SIZE_8:
1279                off_mask = 3;
1280                val_mask = 0xff;
1281                break;
1282        case PCI_SIZE_16:
1283                off_mask = 2;
1284                val_mask = 0xffff;
1285                break;
1286        default:
1287                return value;
1288        }
1289        shift = (offset & off_mask) * 8;
1290        ldata = (value & val_mask) << shift;
1291        mask = val_mask << shift;
1292        value = (old & ~mask) | ldata;
1293
1294        return value;
1295}
1296
1297int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index)
1298{
1299        int pci_addr_cells, addr_cells, size_cells;
1300        int cells_per_record;
1301        const u32 *prop;
1302        int len;
1303        int i = 0;
1304
1305        prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len);
1306        if (!prop) {
1307                log_err("PCI: Device '%s': Cannot decode dma-ranges\n",
1308                        dev->name);
1309                return -EINVAL;
1310        }
1311
1312        pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev));
1313        addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent));
1314        size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev));
1315
1316        /* PCI addresses are always 3-cells */
1317        len /= sizeof(u32);
1318        cells_per_record = pci_addr_cells + addr_cells + size_cells;
1319        debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1320              cells_per_record);
1321
1322        while (len) {
1323                memp->bus_start = fdtdec_get_number(prop + 1, 2);
1324                prop += pci_addr_cells;
1325                memp->phys_start = fdtdec_get_number(prop, addr_cells);
1326                prop += addr_cells;
1327                memp->size = fdtdec_get_number(prop, size_cells);
1328                prop += size_cells;
1329
1330                if (i == index)
1331                        return 0;
1332                i++;
1333                len -= cells_per_record;
1334        }
1335
1336        return -EINVAL;
1337}
1338
1339int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1340                    struct pci_region **memp, struct pci_region **prefp)
1341{
1342        struct udevice *bus = pci_get_controller(dev);
1343        struct pci_controller *hose = dev_get_uclass_priv(bus);
1344        int i;
1345
1346        *iop = NULL;
1347        *memp = NULL;
1348        *prefp = NULL;
1349        for (i = 0; i < hose->region_count; i++) {
1350                switch (hose->regions[i].flags) {
1351                case PCI_REGION_IO:
1352                        if (!*iop || (*iop)->size < hose->regions[i].size)
1353                                *iop = hose->regions + i;
1354                        break;
1355                case PCI_REGION_MEM:
1356                        if (!*memp || (*memp)->size < hose->regions[i].size)
1357                                *memp = hose->regions + i;
1358                        break;
1359                case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1360                        if (!*prefp || (*prefp)->size < hose->regions[i].size)
1361                                *prefp = hose->regions + i;
1362                        break;
1363                }
1364        }
1365
1366        return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1367}
1368
1369u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
1370{
1371        u32 addr;
1372        int bar;
1373
1374        bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1375        dm_pci_read_config32(dev, bar, &addr);
1376
1377        /*
1378         * If we get an invalid address, return this so that comparisons with
1379         * FDT_ADDR_T_NONE work correctly
1380         */
1381        if (addr == 0xffffffff)
1382                return addr;
1383        else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
1384                return addr & PCI_BASE_ADDRESS_IO_MASK;
1385        else
1386                return addr & PCI_BASE_ADDRESS_MEM_MASK;
1387}
1388
1389void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1390{
1391        int bar;
1392
1393        bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1394        dm_pci_write_config32(dev, bar, addr);
1395}
1396
1397phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1398                               size_t len, unsigned long mask,
1399                               unsigned long flags)
1400{
1401        struct udevice *ctlr;
1402        struct pci_controller *hose;
1403        struct pci_region *res;
1404        pci_addr_t offset;
1405        int i;
1406
1407        /* The root controller has the region information */
1408        ctlr = pci_get_controller(dev);
1409        hose = dev_get_uclass_priv(ctlr);
1410
1411        if (hose->region_count == 0)
1412                return bus_addr;
1413
1414        for (i = 0; i < hose->region_count; i++) {
1415                res = &hose->regions[i];
1416
1417                if ((res->flags & mask) != flags)
1418                        continue;
1419
1420                if (bus_addr < res->bus_start)
1421                        continue;
1422
1423                offset = bus_addr - res->bus_start;
1424                if (offset >= res->size)
1425                        continue;
1426
1427                if (len > res->size - offset)
1428                        continue;
1429
1430                return res->phys_start + offset;
1431        }
1432
1433        puts("pci_hose_bus_to_phys: invalid physical address\n");
1434        return 0;
1435}
1436
1437pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1438                              size_t len, unsigned long mask,
1439                              unsigned long flags)
1440{
1441        struct udevice *ctlr;
1442        struct pci_controller *hose;
1443        struct pci_region *res;
1444        phys_addr_t offset;
1445        int i;
1446
1447        /* The root controller has the region information */
1448        ctlr = pci_get_controller(dev);
1449        hose = dev_get_uclass_priv(ctlr);
1450
1451        if (hose->region_count == 0)
1452                return phys_addr;
1453
1454        for (i = 0; i < hose->region_count; i++) {
1455                res = &hose->regions[i];
1456
1457                if ((res->flags & mask) != flags)
1458                        continue;
1459
1460                if (phys_addr < res->phys_start)
1461                        continue;
1462
1463                offset = phys_addr - res->phys_start;
1464                if (offset >= res->size)
1465                        continue;
1466
1467                if (len > res->size - offset)
1468                        continue;
1469
1470                return res->bus_start + offset;
1471        }
1472
1473        puts("pci_hose_phys_to_bus: invalid physical address\n");
1474        return 0;
1475}
1476
1477static phys_addr_t dm_pci_map_ea_virt(struct udevice *dev, int ea_off,
1478                                      struct pci_child_plat *pdata)
1479{
1480        phys_addr_t addr = 0;
1481
1482        /*
1483         * In the case of a Virtual Function device using BAR
1484         * base and size, add offset for VFn BAR(1, 2, 3...n)
1485         */
1486        if (pdata->is_virtfn) {
1487                size_t sz;
1488                u32 ea_entry;
1489
1490                /* MaxOffset, 1st DW */
1491                dm_pci_read_config32(dev, ea_off + 8, &ea_entry);
1492                sz = ea_entry & PCI_EA_FIELD_MASK;
1493                /* Fill up lower 2 bits */
1494                sz |= (~PCI_EA_FIELD_MASK);
1495
1496                if (ea_entry & PCI_EA_IS_64) {
1497                        /* MaxOffset 2nd DW */
1498                        dm_pci_read_config32(dev, ea_off + 16, &ea_entry);
1499                        sz |= ((u64)ea_entry) << 32;
1500                }
1501
1502                addr = (pdata->virtid - 1) * (sz + 1);
1503        }
1504
1505        return addr;
1506}
1507
1508static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, size_t offset,
1509                               size_t len, int ea_off,
1510                               struct pci_child_plat *pdata)
1511{
1512        int ea_cnt, i, entry_size;
1513        int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1514        u32 ea_entry;
1515        phys_addr_t addr;
1516
1517        if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1518                /*
1519                 * In the case of a Virtual Function device, device is
1520                 * Physical function, so pdata will point to required VF
1521                 * specific data.
1522                 */
1523                if (pdata->is_virtfn)
1524                        bar_id += PCI_EA_BEI_VF_BAR0;
1525        }
1526
1527        /* EA capability structure header */
1528        dm_pci_read_config32(dev, ea_off, &ea_entry);
1529        ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1530        ea_off += PCI_EA_FIRST_ENT;
1531
1532        for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1533                /* Entry header */
1534                dm_pci_read_config32(dev, ea_off, &ea_entry);
1535                entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1536
1537                if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1538                        continue;
1539
1540                /* Base address, 1st DW */
1541                dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1542                addr = ea_entry & PCI_EA_FIELD_MASK;
1543                if (ea_entry & PCI_EA_IS_64) {
1544                        /* Base address, 2nd DW, skip over 4B MaxOffset */
1545                        dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1546                        addr |= ((u64)ea_entry) << 32;
1547                }
1548
1549                if (IS_ENABLED(CONFIG_PCI_SRIOV))
1550                        addr += dm_pci_map_ea_virt(dev, ea_off, pdata);
1551
1552                if (~((phys_addr_t)0) - addr < offset)
1553                        return NULL;
1554
1555                /* size ignored for now */
1556                return map_physmem(addr + offset, len, MAP_NOCACHE);
1557        }
1558
1559        return 0;
1560}
1561
1562void *dm_pci_map_bar(struct udevice *dev, int bar, size_t offset, size_t len,
1563                     unsigned long mask, unsigned long flags)
1564{
1565        struct pci_child_plat *pdata = dev_get_parent_plat(dev);
1566        struct udevice *udev = dev;
1567        pci_addr_t pci_bus_addr;
1568        u32 bar_response;
1569        int ea_off;
1570
1571        if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1572                /*
1573                 * In case of Virtual Function devices, use PF udevice
1574                 * as EA capability is defined in Physical Function
1575                 */
1576                if (pdata->is_virtfn)
1577                        udev = pdata->pfdev;
1578        }
1579
1580        /*
1581         * if the function supports Enhanced Allocation use that instead of
1582         * BARs
1583         * Incase of virtual functions, pdata will help read VF BEI
1584         * and EA entry size.
1585         */
1586        if (IS_ENABLED(CONFIG_PCI_ENHANCED_ALLOCATION))
1587                ea_off = dm_pci_find_capability(udev, PCI_CAP_ID_EA);
1588        else
1589                ea_off = 0;
1590
1591        if (ea_off)
1592                return dm_pci_map_ea_bar(udev, bar, offset, len, ea_off, pdata);
1593
1594        /* read BAR address */
1595        dm_pci_read_config32(udev, bar, &bar_response);
1596        pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1597
1598        if (~((pci_addr_t)0) - pci_bus_addr < offset)
1599                return NULL;
1600
1601        /*
1602         * Forward the length argument to dm_pci_bus_to_virt. The length will
1603         * be used to check that the entire address range has been declared as
1604         * a PCI range, but a better check would be to probe for the size of
1605         * the bar and prevent overflow more locally.
1606         */
1607        return dm_pci_bus_to_virt(udev, pci_bus_addr + offset, len, mask, flags,
1608                                  MAP_NOCACHE);
1609}
1610
1611static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
1612{
1613        int ttl = PCI_FIND_CAP_TTL;
1614        u8 id;
1615        u16 ent;
1616
1617        dm_pci_read_config8(dev, pos, &pos);
1618
1619        while (ttl--) {
1620                if (pos < PCI_STD_HEADER_SIZEOF)
1621                        break;
1622                pos &= ~3;
1623                dm_pci_read_config16(dev, pos, &ent);
1624
1625                id = ent & 0xff;
1626                if (id == 0xff)
1627                        break;
1628                if (id == cap)
1629                        return pos;
1630                pos = (ent >> 8);
1631        }
1632
1633        return 0;
1634}
1635
1636int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1637{
1638        return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1639                                            cap);
1640}
1641
1642int dm_pci_find_capability(struct udevice *dev, int cap)
1643{
1644        u16 status;
1645        u8 header_type;
1646        u8 pos;
1647
1648        dm_pci_read_config16(dev, PCI_STATUS, &status);
1649        if (!(status & PCI_STATUS_CAP_LIST))
1650                return 0;
1651
1652        dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1653        if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1654                pos = PCI_CB_CAPABILITY_LIST;
1655        else
1656                pos = PCI_CAPABILITY_LIST;
1657
1658        return _dm_pci_find_next_capability(dev, pos, cap);
1659}
1660
1661int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
1662{
1663        u32 header;
1664        int ttl;
1665        int pos = PCI_CFG_SPACE_SIZE;
1666
1667        /* minimum 8 bytes per capability */
1668        ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1669
1670        if (start)
1671                pos = start;
1672
1673        dm_pci_read_config32(dev, pos, &header);
1674        /*
1675         * If we have no capabilities, this is indicated by cap ID,
1676         * cap version and next pointer all being 0.
1677         */
1678        if (header == 0)
1679                return 0;
1680
1681        while (ttl--) {
1682                if (PCI_EXT_CAP_ID(header) == cap)
1683                        return pos;
1684
1685                pos = PCI_EXT_CAP_NEXT(header);
1686                if (pos < PCI_CFG_SPACE_SIZE)
1687                        break;
1688
1689                dm_pci_read_config32(dev, pos, &header);
1690        }
1691
1692        return 0;
1693}
1694
1695int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1696{
1697        return dm_pci_find_next_ext_capability(dev, 0, cap);
1698}
1699
1700int dm_pci_flr(struct udevice *dev)
1701{
1702        int pcie_off;
1703        u32 cap;
1704
1705        /* look for PCI Express Capability */
1706        pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1707        if (!pcie_off)
1708                return -ENOENT;
1709
1710        /* check FLR capability */
1711        dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1712        if (!(cap & PCI_EXP_DEVCAP_FLR))
1713                return -ENOENT;
1714
1715        dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1716                               PCI_EXP_DEVCTL_BCR_FLR);
1717
1718        /* wait 100ms, per PCI spec */
1719        mdelay(100);
1720
1721        return 0;
1722}
1723
1724#if defined(CONFIG_PCI_SRIOV)
1725int pci_sriov_init(struct udevice *pdev, int vf_en)
1726{
1727        u16 vendor, device;
1728        struct udevice *bus;
1729        struct udevice *dev;
1730        pci_dev_t bdf;
1731        u16 ctrl;
1732        u16 num_vfs;
1733        u16 total_vf;
1734        u16 vf_offset;
1735        u16 vf_stride;
1736        int vf, ret;
1737        int pos;
1738
1739        pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1740        if (!pos) {
1741                debug("Error: SRIOV capability not found\n");
1742                return -ENOENT;
1743        }
1744
1745        dm_pci_read_config16(pdev, pos + PCI_SRIOV_CTRL, &ctrl);
1746
1747        dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1748        if (vf_en > total_vf)
1749                vf_en = total_vf;
1750        dm_pci_write_config16(pdev, pos + PCI_SRIOV_NUM_VF, vf_en);
1751
1752        ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
1753        dm_pci_write_config16(pdev, pos + PCI_SRIOV_CTRL, ctrl);
1754
1755        dm_pci_read_config16(pdev, pos + PCI_SRIOV_NUM_VF, &num_vfs);
1756        if (num_vfs > vf_en)
1757                num_vfs = vf_en;
1758
1759        dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_OFFSET, &vf_offset);
1760        dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_STRIDE, &vf_stride);
1761
1762        dm_pci_read_config16(pdev, PCI_VENDOR_ID, &vendor);
1763        dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_DID, &device);
1764
1765        bdf = dm_pci_get_bdf(pdev);
1766
1767        pci_get_bus(PCI_BUS(bdf), &bus);
1768
1769        if (!bus)
1770                return -ENODEV;
1771
1772        bdf += PCI_BDF(0, 0, vf_offset);
1773
1774        for (vf = 0; vf < num_vfs; vf++) {
1775                struct pci_child_plat *pplat;
1776                ulong class;
1777
1778                pci_bus_read_config(bus, bdf, PCI_CLASS_DEVICE,
1779                                    &class, PCI_SIZE_16);
1780
1781                debug("%s: bus %d/%s: found VF %x:%x\n", __func__,
1782                      dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
1783
1784                /* Find this device in the device tree */
1785                ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
1786
1787                if (ret == -ENODEV) {
1788                        struct pci_device_id find_id;
1789
1790                        memset(&find_id, '\0', sizeof(find_id));
1791                        find_id.vendor = vendor;
1792                        find_id.device = device;
1793                        find_id.class = class;
1794
1795                        ret = pci_find_and_bind_driver(bus, &find_id,
1796                                                       bdf, &dev);
1797
1798                        if (ret)
1799                                return ret;
1800                }
1801
1802                /* Update the platform data */
1803                pplat = dev_get_parent_plat(dev);
1804                pplat->devfn = PCI_MASK_BUS(bdf);
1805                pplat->vendor = vendor;
1806                pplat->device = device;
1807                pplat->class = class;
1808                pplat->is_virtfn = true;
1809                pplat->pfdev = pdev;
1810                pplat->virtid = vf * vf_stride + vf_offset;
1811
1812                debug("%s: bus %d/%s: found VF %x:%x %x:%x class %lx id %x\n",
1813                      __func__, dev_seq(dev), dev->name, PCI_DEV(bdf),
1814                      PCI_FUNC(bdf), vendor, device, class, pplat->virtid);
1815                bdf += PCI_BDF(0, 0, vf_stride);
1816        }
1817
1818        return 0;
1819}
1820
1821int pci_sriov_get_totalvfs(struct udevice *pdev)
1822{
1823        u16 total_vf;
1824        int pos;
1825
1826        pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1827        if (!pos) {
1828                debug("Error: SRIOV capability not found\n");
1829                return -ENOENT;
1830        }
1831
1832        dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1833
1834        return total_vf;
1835}
1836#endif /* SRIOV */
1837
1838UCLASS_DRIVER(pci) = {
1839        .id             = UCLASS_PCI,
1840        .name           = "pci",
1841        .flags          = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
1842        .post_bind      = dm_scan_fdt_dev,
1843        .pre_probe      = pci_uclass_pre_probe,
1844        .post_probe     = pci_uclass_post_probe,
1845        .child_post_bind = pci_uclass_child_post_bind,
1846        .per_device_auto        = sizeof(struct pci_controller),
1847        .per_child_plat_auto    = sizeof(struct pci_child_plat),
1848};
1849
1850static const struct dm_pci_ops pci_bridge_ops = {
1851        .read_config    = pci_bridge_read_config,
1852        .write_config   = pci_bridge_write_config,
1853};
1854
1855static const struct udevice_id pci_bridge_ids[] = {
1856        { .compatible = "pci-bridge" },
1857        { }
1858};
1859
1860U_BOOT_DRIVER(pci_bridge_drv) = {
1861        .name           = "pci_bridge_drv",
1862        .id             = UCLASS_PCI,
1863        .of_match       = pci_bridge_ids,
1864        .ops            = &pci_bridge_ops,
1865};
1866
1867UCLASS_DRIVER(pci_generic) = {
1868        .id             = UCLASS_PCI_GENERIC,
1869        .name           = "pci_generic",
1870};
1871
1872static const struct udevice_id pci_generic_ids[] = {
1873        { .compatible = "pci-generic" },
1874        { }
1875};
1876
1877U_BOOT_DRIVER(pci_generic_drv) = {
1878        .name           = "pci_generic_drv",
1879        .id             = UCLASS_PCI_GENERIC,
1880        .of_match       = pci_generic_ids,
1881};
1882
1883int pci_init(void)
1884{
1885        struct udevice *bus;
1886
1887        /*
1888         * Enumerate all known controller devices. Enumeration has the side-
1889         * effect of probing them, so PCIe devices will be enumerated too.
1890         */
1891        for (uclass_first_device_check(UCLASS_PCI, &bus);
1892             bus;
1893             uclass_next_device_check(&bus)) {
1894                ;
1895        }
1896
1897        return 0;
1898}
1899