linux/drivers/pci/of.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * PCI <-> OF mapping helpers
   4 *
   5 * Copyright 2011 IBM Corp.
   6 */
   7#define pr_fmt(fmt)     "PCI: OF: " fmt
   8
   9#include <linux/irqdomain.h>
  10#include <linux/kernel.h>
  11#include <linux/pci.h>
  12#include <linux/of.h>
  13#include <linux/of_irq.h>
  14#include <linux/of_address.h>
  15#include <linux/of_pci.h>
  16#include "pci.h"
  17
  18void pci_set_of_node(struct pci_dev *dev)
  19{
  20        if (!dev->bus->dev.of_node)
  21                return;
  22        dev->dev.of_node = of_pci_find_child_device(dev->bus->dev.of_node,
  23                                                    dev->devfn);
  24}
  25
  26void pci_release_of_node(struct pci_dev *dev)
  27{
  28        of_node_put(dev->dev.of_node);
  29        dev->dev.of_node = NULL;
  30}
  31
  32void pci_set_bus_of_node(struct pci_bus *bus)
  33{
  34        struct device_node *node;
  35
  36        if (bus->self == NULL) {
  37                node = pcibios_get_phb_of_node(bus);
  38        } else {
  39                node = of_node_get(bus->self->dev.of_node);
  40                if (node && of_property_read_bool(node, "external-facing"))
  41                        bus->self->external_facing = true;
  42        }
  43        bus->dev.of_node = node;
  44}
  45
  46void pci_release_bus_of_node(struct pci_bus *bus)
  47{
  48        of_node_put(bus->dev.of_node);
  49        bus->dev.of_node = NULL;
  50}
  51
  52struct device_node * __weak pcibios_get_phb_of_node(struct pci_bus *bus)
  53{
  54        /* This should only be called for PHBs */
  55        if (WARN_ON(bus->self || bus->parent))
  56                return NULL;
  57
  58        /*
  59         * Look for a node pointer in either the intermediary device we
  60         * create above the root bus or its own parent. Normally only
  61         * the later is populated.
  62         */
  63        if (bus->bridge->of_node)
  64                return of_node_get(bus->bridge->of_node);
  65        if (bus->bridge->parent && bus->bridge->parent->of_node)
  66                return of_node_get(bus->bridge->parent->of_node);
  67        return NULL;
  68}
  69
  70struct irq_domain *pci_host_bridge_of_msi_domain(struct pci_bus *bus)
  71{
  72#ifdef CONFIG_IRQ_DOMAIN
  73        struct irq_domain *d;
  74
  75        if (!bus->dev.of_node)
  76                return NULL;
  77
  78        /* Start looking for a phandle to an MSI controller. */
  79        d = of_msi_get_domain(&bus->dev, bus->dev.of_node, DOMAIN_BUS_PCI_MSI);
  80        if (d)
  81                return d;
  82
  83        /*
  84         * If we don't have an msi-parent property, look for a domain
  85         * directly attached to the host bridge.
  86         */
  87        d = irq_find_matching_host(bus->dev.of_node, DOMAIN_BUS_PCI_MSI);
  88        if (d)
  89                return d;
  90
  91        return irq_find_host(bus->dev.of_node);
  92#else
  93        return NULL;
  94#endif
  95}
  96
  97static inline int __of_pci_pci_compare(struct device_node *node,
  98                                       unsigned int data)
  99{
 100        int devfn;
 101
 102        devfn = of_pci_get_devfn(node);
 103        if (devfn < 0)
 104                return 0;
 105
 106        return devfn == data;
 107}
 108
 109struct device_node *of_pci_find_child_device(struct device_node *parent,
 110                                             unsigned int devfn)
 111{
 112        struct device_node *node, *node2;
 113
 114        for_each_child_of_node(parent, node) {
 115                if (__of_pci_pci_compare(node, devfn))
 116                        return node;
 117                /*
 118                 * Some OFs create a parent node "multifunc-device" as
 119                 * a fake root for all functions of a multi-function
 120                 * device we go down them as well.
 121                 */
 122                if (of_node_name_eq(node, "multifunc-device")) {
 123                        for_each_child_of_node(node, node2) {
 124                                if (__of_pci_pci_compare(node2, devfn)) {
 125                                        of_node_put(node);
 126                                        return node2;
 127                                }
 128                        }
 129                }
 130        }
 131        return NULL;
 132}
 133EXPORT_SYMBOL_GPL(of_pci_find_child_device);
 134
 135/**
 136 * of_pci_get_devfn() - Get device and function numbers for a device node
 137 * @np: device node
 138 *
 139 * Parses a standard 5-cell PCI resource and returns an 8-bit value that can
 140 * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device
 141 * and function numbers respectively. On error a negative error code is
 142 * returned.
 143 */
 144int of_pci_get_devfn(struct device_node *np)
 145{
 146        u32 reg[5];
 147        int error;
 148
 149        error = of_property_read_u32_array(np, "reg", reg, ARRAY_SIZE(reg));
 150        if (error)
 151                return error;
 152
 153        return (reg[0] >> 8) & 0xff;
 154}
 155EXPORT_SYMBOL_GPL(of_pci_get_devfn);
 156
 157/**
 158 * of_pci_parse_bus_range() - parse the bus-range property of a PCI device
 159 * @node: device node
 160 * @res: address to a struct resource to return the bus-range
 161 *
 162 * Returns 0 on success or a negative error-code on failure.
 163 */
 164int of_pci_parse_bus_range(struct device_node *node, struct resource *res)
 165{
 166        u32 bus_range[2];
 167        int error;
 168
 169        error = of_property_read_u32_array(node, "bus-range", bus_range,
 170                                           ARRAY_SIZE(bus_range));
 171        if (error)
 172                return error;
 173
 174        res->name = node->name;
 175        res->start = bus_range[0];
 176        res->end = bus_range[1];
 177        res->flags = IORESOURCE_BUS;
 178
 179        return 0;
 180}
 181EXPORT_SYMBOL_GPL(of_pci_parse_bus_range);
 182
 183/**
 184 * This function will try to obtain the host bridge domain number by
 185 * finding a property called "linux,pci-domain" of the given device node.
 186 *
 187 * @node: device tree node with the domain information
 188 *
 189 * Returns the associated domain number from DT in the range [0-0xffff], or
 190 * a negative value if the required property is not found.
 191 */
 192int of_get_pci_domain_nr(struct device_node *node)
 193{
 194        u32 domain;
 195        int error;
 196
 197        error = of_property_read_u32(node, "linux,pci-domain", &domain);
 198        if (error)
 199                return error;
 200
 201        return (u16)domain;
 202}
 203EXPORT_SYMBOL_GPL(of_get_pci_domain_nr);
 204
 205/**
 206 * This function will try to find the limitation of link speed by finding
 207 * a property called "max-link-speed" of the given device node.
 208 *
 209 * @node: device tree node with the max link speed information
 210 *
 211 * Returns the associated max link speed from DT, or a negative value if the
 212 * required property is not found or is invalid.
 213 */
 214int of_pci_get_max_link_speed(struct device_node *node)
 215{
 216        u32 max_link_speed;
 217
 218        if (of_property_read_u32(node, "max-link-speed", &max_link_speed) ||
 219            max_link_speed > 4)
 220                return -EINVAL;
 221
 222        return max_link_speed;
 223}
 224EXPORT_SYMBOL_GPL(of_pci_get_max_link_speed);
 225
 226/**
 227 * of_pci_check_probe_only - Setup probe only mode if linux,pci-probe-only
 228 *                           is present and valid
 229 */
 230void of_pci_check_probe_only(void)
 231{
 232        u32 val;
 233        int ret;
 234
 235        ret = of_property_read_u32(of_chosen, "linux,pci-probe-only", &val);
 236        if (ret) {
 237                if (ret == -ENODATA || ret == -EOVERFLOW)
 238                        pr_warn("linux,pci-probe-only without valid value, ignoring\n");
 239                return;
 240        }
 241
 242        if (val)
 243                pci_add_flags(PCI_PROBE_ONLY);
 244        else
 245                pci_clear_flags(PCI_PROBE_ONLY);
 246
 247        pr_info("PROBE_ONLY %sabled\n", val ? "en" : "dis");
 248}
 249EXPORT_SYMBOL_GPL(of_pci_check_probe_only);
 250
 251#if defined(CONFIG_OF_ADDRESS)
 252/**
 253 * devm_of_pci_get_host_bridge_resources() - Resource-managed parsing of PCI
 254 *                                           host bridge resources from DT
 255 * @dev: host bridge device
 256 * @busno: bus number associated with the bridge root bus
 257 * @bus_max: maximum number of buses for this bridge
 258 * @resources: list where the range of resources will be added after DT parsing
 259 * @io_base: pointer to a variable that will contain on return the physical
 260 * address for the start of the I/O range. Can be NULL if the caller doesn't
 261 * expect I/O ranges to be present in the device tree.
 262 *
 263 * This function will parse the "ranges" property of a PCI host bridge device
 264 * node and setup the resource mapping based on its content. It is expected
 265 * that the property conforms with the Power ePAPR document.
 266 *
 267 * It returns zero if the range parsing has been successful or a standard error
 268 * value if it failed.
 269 */
 270int devm_of_pci_get_host_bridge_resources(struct device *dev,
 271                        unsigned char busno, unsigned char bus_max,
 272                        struct list_head *resources, resource_size_t *io_base)
 273{
 274        struct device_node *dev_node = dev->of_node;
 275        struct resource *res, tmp_res;
 276        struct resource *bus_range;
 277        struct of_pci_range range;
 278        struct of_pci_range_parser parser;
 279        char range_type[4];
 280        int err;
 281
 282        if (io_base)
 283                *io_base = (resource_size_t)OF_BAD_ADDR;
 284
 285        bus_range = devm_kzalloc(dev, sizeof(*bus_range), GFP_KERNEL);
 286        if (!bus_range)
 287                return -ENOMEM;
 288
 289        dev_info(dev, "host bridge %pOF ranges:\n", dev_node);
 290
 291        err = of_pci_parse_bus_range(dev_node, bus_range);
 292        if (err) {
 293                bus_range->start = busno;
 294                bus_range->end = bus_max;
 295                bus_range->flags = IORESOURCE_BUS;
 296                dev_info(dev, "  No bus range found for %pOF, using %pR\n",
 297                         dev_node, bus_range);
 298        } else {
 299                if (bus_range->end > bus_range->start + bus_max)
 300                        bus_range->end = bus_range->start + bus_max;
 301        }
 302        pci_add_resource(resources, bus_range);
 303
 304        /* Check for ranges property */
 305        err = of_pci_range_parser_init(&parser, dev_node);
 306        if (err)
 307                goto failed;
 308
 309        dev_dbg(dev, "Parsing ranges property...\n");
 310        for_each_of_pci_range(&parser, &range) {
 311                /* Read next ranges element */
 312                if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
 313                        snprintf(range_type, 4, " IO");
 314                else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
 315                        snprintf(range_type, 4, "MEM");
 316                else
 317                        snprintf(range_type, 4, "err");
 318                dev_info(dev, "  %s %#010llx..%#010llx -> %#010llx\n",
 319                         range_type, range.cpu_addr,
 320                         range.cpu_addr + range.size - 1, range.pci_addr);
 321
 322                /*
 323                 * If we failed translation or got a zero-sized region
 324                 * then skip this range
 325                 */
 326                if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
 327                        continue;
 328
 329                err = of_pci_range_to_resource(&range, dev_node, &tmp_res);
 330                if (err)
 331                        continue;
 332
 333                res = devm_kmemdup(dev, &tmp_res, sizeof(tmp_res), GFP_KERNEL);
 334                if (!res) {
 335                        err = -ENOMEM;
 336                        goto failed;
 337                }
 338
 339                if (resource_type(res) == IORESOURCE_IO) {
 340                        if (!io_base) {
 341                                dev_err(dev, "I/O range found for %pOF. Please provide an io_base pointer to save CPU base address\n",
 342                                        dev_node);
 343                                err = -EINVAL;
 344                                goto failed;
 345                        }
 346                        if (*io_base != (resource_size_t)OF_BAD_ADDR)
 347                                dev_warn(dev, "More than one I/O resource converted for %pOF. CPU base address for old range lost!\n",
 348                                         dev_node);
 349                        *io_base = range.cpu_addr;
 350                }
 351
 352                pci_add_resource_offset(resources, res, res->start - range.pci_addr);
 353        }
 354
 355        return 0;
 356
 357failed:
 358        pci_free_resource_list(resources);
 359        return err;
 360}
 361EXPORT_SYMBOL_GPL(devm_of_pci_get_host_bridge_resources);
 362#endif /* CONFIG_OF_ADDRESS */
 363
 364#if IS_ENABLED(CONFIG_OF_IRQ)
 365/**
 366 * of_irq_parse_pci - Resolve the interrupt for a PCI device
 367 * @pdev:       the device whose interrupt is to be resolved
 368 * @out_irq:    structure of_phandle_args filled by this function
 369 *
 370 * This function resolves the PCI interrupt for a given PCI device. If a
 371 * device-node exists for a given pci_dev, it will use normal OF tree
 372 * walking. If not, it will implement standard swizzling and walk up the
 373 * PCI tree until an device-node is found, at which point it will finish
 374 * resolving using the OF tree walking.
 375 */
 376static int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args *out_irq)
 377{
 378        struct device_node *dn, *ppnode;
 379        struct pci_dev *ppdev;
 380        __be32 laddr[3];
 381        u8 pin;
 382        int rc;
 383
 384        /*
 385         * Check if we have a device node, if yes, fallback to standard
 386         * device tree parsing
 387         */
 388        dn = pci_device_to_OF_node(pdev);
 389        if (dn) {
 390                rc = of_irq_parse_one(dn, 0, out_irq);
 391                if (!rc)
 392                        return rc;
 393        }
 394
 395        /*
 396         * Ok, we don't, time to have fun. Let's start by building up an
 397         * interrupt spec.  we assume #interrupt-cells is 1, which is standard
 398         * for PCI. If you do different, then don't use that routine.
 399         */
 400        rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
 401        if (rc != 0)
 402                goto err;
 403        /* No pin, exit with no error message. */
 404        if (pin == 0)
 405                return -ENODEV;
 406
 407        /* Now we walk up the PCI tree */
 408        for (;;) {
 409                /* Get the pci_dev of our parent */
 410                ppdev = pdev->bus->self;
 411
 412                /* Ouch, it's a host bridge... */
 413                if (ppdev == NULL) {
 414                        ppnode = pci_bus_to_OF_node(pdev->bus);
 415
 416                        /* No node for host bridge ? give up */
 417                        if (ppnode == NULL) {
 418                                rc = -EINVAL;
 419                                goto err;
 420                        }
 421                } else {
 422                        /* We found a P2P bridge, check if it has a node */
 423                        ppnode = pci_device_to_OF_node(ppdev);
 424                }
 425
 426                /*
 427                 * Ok, we have found a parent with a device-node, hand over to
 428                 * the OF parsing code.
 429                 * We build a unit address from the linux device to be used for
 430                 * resolution. Note that we use the linux bus number which may
 431                 * not match your firmware bus numbering.
 432                 * Fortunately, in most cases, interrupt-map-mask doesn't
 433                 * include the bus number as part of the matching.
 434                 * You should still be careful about that though if you intend
 435                 * to rely on this function (you ship a firmware that doesn't
 436                 * create device nodes for all PCI devices).
 437                 */
 438                if (ppnode)
 439                        break;
 440
 441                /*
 442                 * We can only get here if we hit a P2P bridge with no node;
 443                 * let's do standard swizzling and try again
 444                 */
 445                pin = pci_swizzle_interrupt_pin(pdev, pin);
 446                pdev = ppdev;
 447        }
 448
 449        out_irq->np = ppnode;
 450        out_irq->args_count = 1;
 451        out_irq->args[0] = pin;
 452        laddr[0] = cpu_to_be32((pdev->bus->number << 16) | (pdev->devfn << 8));
 453        laddr[1] = laddr[2] = cpu_to_be32(0);
 454        rc = of_irq_parse_raw(laddr, out_irq);
 455        if (rc)
 456                goto err;
 457        return 0;
 458err:
 459        if (rc == -ENOENT) {
 460                dev_warn(&pdev->dev,
 461                        "%s: no interrupt-map found, INTx interrupts not available\n",
 462                        __func__);
 463                pr_warn_once("%s: possibly some PCI slots don't have level triggered interrupts capability\n",
 464                        __func__);
 465        } else {
 466                dev_err(&pdev->dev, "%s: failed with rc=%d\n", __func__, rc);
 467        }
 468        return rc;
 469}
 470
 471/**
 472 * of_irq_parse_and_map_pci() - Decode a PCI IRQ from the device tree and map to a VIRQ
 473 * @dev: The PCI device needing an IRQ
 474 * @slot: PCI slot number; passed when used as map_irq callback. Unused
 475 * @pin: PCI IRQ pin number; passed when used as map_irq callback. Unused
 476 *
 477 * @slot and @pin are unused, but included in the function so that this
 478 * function can be used directly as the map_irq callback to
 479 * pci_assign_irq() and struct pci_host_bridge.map_irq pointer
 480 */
 481int of_irq_parse_and_map_pci(const struct pci_dev *dev, u8 slot, u8 pin)
 482{
 483        struct of_phandle_args oirq;
 484        int ret;
 485
 486        ret = of_irq_parse_pci(dev, &oirq);
 487        if (ret)
 488                return 0; /* Proper return code 0 == NO_IRQ */
 489
 490        return irq_create_of_mapping(&oirq);
 491}
 492EXPORT_SYMBOL_GPL(of_irq_parse_and_map_pci);
 493#endif  /* CONFIG_OF_IRQ */
 494
 495int pci_parse_request_of_pci_ranges(struct device *dev,
 496                                    struct list_head *resources,
 497                                    struct resource **bus_range)
 498{
 499        int err, res_valid = 0;
 500        resource_size_t iobase;
 501        struct resource_entry *win, *tmp;
 502
 503        INIT_LIST_HEAD(resources);
 504        err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff, resources,
 505                                                    &iobase);
 506        if (err)
 507                return err;
 508
 509        err = devm_request_pci_bus_resources(dev, resources);
 510        if (err)
 511                goto out_release_res;
 512
 513        resource_list_for_each_entry_safe(win, tmp, resources) {
 514                struct resource *res = win->res;
 515
 516                switch (resource_type(res)) {
 517                case IORESOURCE_IO:
 518                        err = devm_pci_remap_iospace(dev, res, iobase);
 519                        if (err) {
 520                                dev_warn(dev, "error %d: failed to map resource %pR\n",
 521                                         err, res);
 522                                resource_list_destroy_entry(win);
 523                        }
 524                        break;
 525                case IORESOURCE_MEM:
 526                        res_valid |= !(res->flags & IORESOURCE_PREFETCH);
 527                        break;
 528                case IORESOURCE_BUS:
 529                        if (bus_range)
 530                                *bus_range = res;
 531                        break;
 532                }
 533        }
 534
 535        if (res_valid)
 536                return 0;
 537
 538        dev_err(dev, "non-prefetchable memory resource required\n");
 539        err = -EINVAL;
 540
 541 out_release_res:
 542        pci_free_resource_list(resources);
 543        return err;
 544}
 545EXPORT_SYMBOL_GPL(pci_parse_request_of_pci_ranges);
 546
 547