Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

PCI: Move OF-related PCI functions into PCI core

Following what has been done for other subsystems, move the remaining PCI
related code out of drivers/of/ and into drivers/pci/of.c

With this, we can kill a few kconfig symbols.

Signed-off-by: Rob Herring <robh@kernel.org>
[bhelgaas: minor whitespace, comment cleanups]
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Cc: Frank Rowand <frowand.list@gmail.com>

authored by

Rob Herring and committed by
Bjorn Helgaas
4670d610 1291a0d5

+517 -542
-1
arch/arm/mach-mvebu/Kconfig
··· 10 10 select ZONE_DMA if ARM_LPAE 11 11 select GPIOLIB 12 12 select PCI_QUIRKS if PCI 13 - select OF_ADDRESS_PCI 14 13 15 14 if ARCH_MVEBU 16 15
-16
drivers/of/Kconfig
··· 62 62 config OF_ADDRESS 63 63 def_bool y 64 64 depends on !SPARC && HAS_IOMEM 65 - select OF_ADDRESS_PCI if PCI 66 - 67 - config OF_ADDRESS_PCI 68 - bool 69 65 70 66 config OF_IRQ 71 67 def_bool y ··· 77 81 select FIXED_PHY 78 82 help 79 83 OpenFirmware MDIO bus (Ethernet PHY) accessors 80 - 81 - config OF_PCI 82 - def_tristate PCI 83 - depends on PCI 84 - help 85 - OpenFirmware PCI bus accessors 86 - 87 - config OF_PCI_IRQ 88 - def_tristate PCI 89 - depends on OF_PCI && OF_IRQ 90 - help 91 - OpenFirmware PCI IRQ routing helpers 92 84 93 85 config OF_RESERVED_MEM 94 86 depends on OF_EARLY_FLATTREE
-2
drivers/of/Makefile
··· 10 10 obj-$(CONFIG_OF_NET) += of_net.o 11 11 obj-$(CONFIG_OF_UNITTEST) += unittest.o 12 12 obj-$(CONFIG_OF_MDIO) += of_mdio.o 13 - obj-$(CONFIG_OF_PCI) += of_pci.o 14 - obj-$(CONFIG_OF_PCI_IRQ) += of_pci_irq.o 15 13 obj-$(CONFIG_OF_RESERVED_MEM) += of_reserved_mem.o 16 14 obj-$(CONFIG_OF_RESOLVE) += resolver.o 17 15 obj-$(CONFIG_OF_OVERLAY) += overlay.o
+3 -5
drivers/of/address.c
··· 96 96 return IORESOURCE_MEM; 97 97 } 98 98 99 - #ifdef CONFIG_OF_ADDRESS_PCI 99 + #ifdef CONFIG_PCI 100 100 /* 101 101 * PCI bus specific translator 102 102 */ ··· 171 171 { 172 172 return of_bus_default_translate(addr + 1, offset, na - 1); 173 173 } 174 - #endif /* CONFIG_OF_ADDRESS_PCI */ 175 174 176 - #ifdef CONFIG_PCI 177 175 const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size, 178 176 unsigned int *flags) 179 177 { ··· 424 426 */ 425 427 426 428 static struct of_bus of_busses[] = { 427 - #ifdef CONFIG_OF_ADDRESS_PCI 429 + #ifdef CONFIG_PCI 428 430 /* PCI */ 429 431 { 430 432 .name = "pci", ··· 435 437 .translate = of_bus_pci_translate, 436 438 .get_flags = of_bus_pci_get_flags, 437 439 }, 438 - #endif /* CONFIG_OF_ADDRESS_PCI */ 440 + #endif /* CONFIG_PCI */ 439 441 /* ISA */ 440 442 { 441 443 .name = "isa",
-384
drivers/of/of_pci.c
··· 1 - #define pr_fmt(fmt) "OF: PCI: " fmt 2 - 3 - #include <linux/kernel.h> 4 - #include <linux/export.h> 5 - #include <linux/of.h> 6 - #include <linux/of_address.h> 7 - #include <linux/of_device.h> 8 - #include <linux/of_pci.h> 9 - #include <linux/slab.h> 10 - 11 - static inline int __of_pci_pci_compare(struct device_node *node, 12 - unsigned int data) 13 - { 14 - int devfn; 15 - 16 - devfn = of_pci_get_devfn(node); 17 - if (devfn < 0) 18 - return 0; 19 - 20 - return devfn == data; 21 - } 22 - 23 - struct device_node *of_pci_find_child_device(struct device_node *parent, 24 - unsigned int devfn) 25 - { 26 - struct device_node *node, *node2; 27 - 28 - for_each_child_of_node(parent, node) { 29 - if (__of_pci_pci_compare(node, devfn)) 30 - return node; 31 - /* 32 - * Some OFs create a parent node "multifunc-device" as 33 - * a fake root for all functions of a multi-function 34 - * device we go down them as well. 35 - */ 36 - if (!strcmp(node->name, "multifunc-device")) { 37 - for_each_child_of_node(node, node2) { 38 - if (__of_pci_pci_compare(node2, devfn)) { 39 - of_node_put(node); 40 - return node2; 41 - } 42 - } 43 - } 44 - } 45 - return NULL; 46 - } 47 - EXPORT_SYMBOL_GPL(of_pci_find_child_device); 48 - 49 - /** 50 - * of_pci_get_devfn() - Get device and function numbers for a device node 51 - * @np: device node 52 - * 53 - * Parses a standard 5-cell PCI resource and returns an 8-bit value that can 54 - * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device 55 - * and function numbers respectively. On error a negative error code is 56 - * returned. 57 - */ 58 - int of_pci_get_devfn(struct device_node *np) 59 - { 60 - u32 reg[5]; 61 - int error; 62 - 63 - error = of_property_read_u32_array(np, "reg", reg, ARRAY_SIZE(reg)); 64 - if (error) 65 - return error; 66 - 67 - return (reg[0] >> 8) & 0xff; 68 - } 69 - EXPORT_SYMBOL_GPL(of_pci_get_devfn); 70 - 71 - /** 72 - * of_pci_parse_bus_range() - parse the bus-range property of a PCI device 73 - * @node: device node 74 - * @res: address to a struct resource to return the bus-range 75 - * 76 - * Returns 0 on success or a negative error-code on failure. 77 - */ 78 - int of_pci_parse_bus_range(struct device_node *node, struct resource *res) 79 - { 80 - u32 bus_range[2]; 81 - int error; 82 - 83 - error = of_property_read_u32_array(node, "bus-range", bus_range, 84 - ARRAY_SIZE(bus_range)); 85 - if (error) 86 - return error; 87 - 88 - res->name = node->name; 89 - res->start = bus_range[0]; 90 - res->end = bus_range[1]; 91 - res->flags = IORESOURCE_BUS; 92 - 93 - return 0; 94 - } 95 - EXPORT_SYMBOL_GPL(of_pci_parse_bus_range); 96 - 97 - /** 98 - * This function will try to obtain the host bridge domain number by 99 - * finding a property called "linux,pci-domain" of the given device node. 100 - * 101 - * @node: device tree node with the domain information 102 - * 103 - * Returns the associated domain number from DT in the range [0-0xffff], or 104 - * a negative value if the required property is not found. 105 - */ 106 - int of_get_pci_domain_nr(struct device_node *node) 107 - { 108 - u32 domain; 109 - int error; 110 - 111 - error = of_property_read_u32(node, "linux,pci-domain", &domain); 112 - if (error) 113 - return error; 114 - 115 - return (u16)domain; 116 - } 117 - EXPORT_SYMBOL_GPL(of_get_pci_domain_nr); 118 - 119 - /** 120 - * This function will try to find the limitation of link speed by finding 121 - * a property called "max-link-speed" of the given device node. 122 - * 123 - * @node: device tree node with the max link speed information 124 - * 125 - * Returns the associated max link speed from DT, or a negative value if the 126 - * required property is not found or is invalid. 127 - */ 128 - int of_pci_get_max_link_speed(struct device_node *node) 129 - { 130 - u32 max_link_speed; 131 - 132 - if (of_property_read_u32(node, "max-link-speed", &max_link_speed) || 133 - max_link_speed > 4) 134 - return -EINVAL; 135 - 136 - return max_link_speed; 137 - } 138 - EXPORT_SYMBOL_GPL(of_pci_get_max_link_speed); 139 - 140 - /** 141 - * of_pci_check_probe_only - Setup probe only mode if linux,pci-probe-only 142 - * is present and valid 143 - */ 144 - void of_pci_check_probe_only(void) 145 - { 146 - u32 val; 147 - int ret; 148 - 149 - ret = of_property_read_u32(of_chosen, "linux,pci-probe-only", &val); 150 - if (ret) { 151 - if (ret == -ENODATA || ret == -EOVERFLOW) 152 - pr_warn("linux,pci-probe-only without valid value, ignoring\n"); 153 - return; 154 - } 155 - 156 - if (val) 157 - pci_add_flags(PCI_PROBE_ONLY); 158 - else 159 - pci_clear_flags(PCI_PROBE_ONLY); 160 - 161 - pr_info("PROBE_ONLY %sabled\n", val ? "en" : "dis"); 162 - } 163 - EXPORT_SYMBOL_GPL(of_pci_check_probe_only); 164 - 165 - #if defined(CONFIG_OF_ADDRESS) 166 - /** 167 - * of_pci_get_host_bridge_resources - Parse PCI host bridge resources from DT 168 - * @dev: device node of the host bridge having the range property 169 - * @busno: bus number associated with the bridge root bus 170 - * @bus_max: maximum number of buses for this bridge 171 - * @resources: list where the range of resources will be added after DT parsing 172 - * @io_base: pointer to a variable that will contain on return the physical 173 - * address for the start of the I/O range. Can be NULL if the caller doesn't 174 - * expect IO ranges to be present in the device tree. 175 - * 176 - * It is the caller's job to free the @resources list. 177 - * 178 - * This function will parse the "ranges" property of a PCI host bridge device 179 - * node and setup the resource mapping based on its content. It is expected 180 - * that the property conforms with the Power ePAPR document. 181 - * 182 - * It returns zero if the range parsing has been successful or a standard error 183 - * value if it failed. 184 - */ 185 - int of_pci_get_host_bridge_resources(struct device_node *dev, 186 - unsigned char busno, unsigned char bus_max, 187 - struct list_head *resources, resource_size_t *io_base) 188 - { 189 - struct resource_entry *window; 190 - struct resource *res; 191 - struct resource *bus_range; 192 - struct of_pci_range range; 193 - struct of_pci_range_parser parser; 194 - char range_type[4]; 195 - int err; 196 - 197 - if (io_base) 198 - *io_base = (resource_size_t)OF_BAD_ADDR; 199 - 200 - bus_range = kzalloc(sizeof(*bus_range), GFP_KERNEL); 201 - if (!bus_range) 202 - return -ENOMEM; 203 - 204 - pr_info("host bridge %pOF ranges:\n", dev); 205 - 206 - err = of_pci_parse_bus_range(dev, bus_range); 207 - if (err) { 208 - bus_range->start = busno; 209 - bus_range->end = bus_max; 210 - bus_range->flags = IORESOURCE_BUS; 211 - pr_info(" No bus range found for %pOF, using %pR\n", 212 - dev, bus_range); 213 - } else { 214 - if (bus_range->end > bus_range->start + bus_max) 215 - bus_range->end = bus_range->start + bus_max; 216 - } 217 - pci_add_resource(resources, bus_range); 218 - 219 - /* Check for ranges property */ 220 - err = of_pci_range_parser_init(&parser, dev); 221 - if (err) 222 - goto parse_failed; 223 - 224 - pr_debug("Parsing ranges property...\n"); 225 - for_each_of_pci_range(&parser, &range) { 226 - /* Read next ranges element */ 227 - if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO) 228 - snprintf(range_type, 4, " IO"); 229 - else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM) 230 - snprintf(range_type, 4, "MEM"); 231 - else 232 - snprintf(range_type, 4, "err"); 233 - pr_info(" %s %#010llx..%#010llx -> %#010llx\n", range_type, 234 - range.cpu_addr, range.cpu_addr + range.size - 1, 235 - range.pci_addr); 236 - 237 - /* 238 - * If we failed translation or got a zero-sized region 239 - * then skip this range 240 - */ 241 - if (range.cpu_addr == OF_BAD_ADDR || range.size == 0) 242 - continue; 243 - 244 - res = kzalloc(sizeof(struct resource), GFP_KERNEL); 245 - if (!res) { 246 - err = -ENOMEM; 247 - goto parse_failed; 248 - } 249 - 250 - err = of_pci_range_to_resource(&range, dev, res); 251 - if (err) { 252 - kfree(res); 253 - continue; 254 - } 255 - 256 - if (resource_type(res) == IORESOURCE_IO) { 257 - if (!io_base) { 258 - pr_err("I/O range found for %pOF. Please provide an io_base pointer to save CPU base address\n", 259 - dev); 260 - err = -EINVAL; 261 - goto conversion_failed; 262 - } 263 - if (*io_base != (resource_size_t)OF_BAD_ADDR) 264 - pr_warn("More than one I/O resource converted for %pOF. CPU base address for old range lost!\n", 265 - dev); 266 - *io_base = range.cpu_addr; 267 - } 268 - 269 - pci_add_resource_offset(resources, res, res->start - range.pci_addr); 270 - } 271 - 272 - return 0; 273 - 274 - conversion_failed: 275 - kfree(res); 276 - parse_failed: 277 - resource_list_for_each_entry(window, resources) 278 - kfree(window->res); 279 - pci_free_resource_list(resources); 280 - return err; 281 - } 282 - EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources); 283 - #endif /* CONFIG_OF_ADDRESS */ 284 - 285 - /** 286 - * of_pci_map_rid - Translate a requester ID through a downstream mapping. 287 - * @np: root complex device node. 288 - * @rid: PCI requester ID to map. 289 - * @map_name: property name of the map to use. 290 - * @map_mask_name: optional property name of the mask to use. 291 - * @target: optional pointer to a target device node. 292 - * @id_out: optional pointer to receive the translated ID. 293 - * 294 - * Given a PCI requester ID, look up the appropriate implementation-defined 295 - * platform ID and/or the target device which receives transactions on that 296 - * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or 297 - * @id_out may be NULL if only the other is required. If @target points to 298 - * a non-NULL device node pointer, only entries targeting that node will be 299 - * matched; if it points to a NULL value, it will receive the device node of 300 - * the first matching target phandle, with a reference held. 301 - * 302 - * Return: 0 on success or a standard error code on failure. 303 - */ 304 - int of_pci_map_rid(struct device_node *np, u32 rid, 305 - const char *map_name, const char *map_mask_name, 306 - struct device_node **target, u32 *id_out) 307 - { 308 - u32 map_mask, masked_rid; 309 - int map_len; 310 - const __be32 *map = NULL; 311 - 312 - if (!np || !map_name || (!target && !id_out)) 313 - return -EINVAL; 314 - 315 - map = of_get_property(np, map_name, &map_len); 316 - if (!map) { 317 - if (target) 318 - return -ENODEV; 319 - /* Otherwise, no map implies no translation */ 320 - *id_out = rid; 321 - return 0; 322 - } 323 - 324 - if (!map_len || map_len % (4 * sizeof(*map))) { 325 - pr_err("%pOF: Error: Bad %s length: %d\n", np, 326 - map_name, map_len); 327 - return -EINVAL; 328 - } 329 - 330 - /* The default is to select all bits. */ 331 - map_mask = 0xffffffff; 332 - 333 - /* 334 - * Can be overridden by "{iommu,msi}-map-mask" property. 335 - * If of_property_read_u32() fails, the default is used. 336 - */ 337 - if (map_mask_name) 338 - of_property_read_u32(np, map_mask_name, &map_mask); 339 - 340 - masked_rid = map_mask & rid; 341 - for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) { 342 - struct device_node *phandle_node; 343 - u32 rid_base = be32_to_cpup(map + 0); 344 - u32 phandle = be32_to_cpup(map + 1); 345 - u32 out_base = be32_to_cpup(map + 2); 346 - u32 rid_len = be32_to_cpup(map + 3); 347 - 348 - if (rid_base & ~map_mask) { 349 - pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n", 350 - np, map_name, map_name, 351 - map_mask, rid_base); 352 - return -EFAULT; 353 - } 354 - 355 - if (masked_rid < rid_base || masked_rid >= rid_base + rid_len) 356 - continue; 357 - 358 - phandle_node = of_find_node_by_phandle(phandle); 359 - if (!phandle_node) 360 - return -ENODEV; 361 - 362 - if (target) { 363 - if (*target) 364 - of_node_put(phandle_node); 365 - else 366 - *target = phandle_node; 367 - 368 - if (*target != phandle_node) 369 - continue; 370 - } 371 - 372 - if (id_out) 373 - *id_out = masked_rid - rid_base + out_base; 374 - 375 - pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n", 376 - np, map_name, map_mask, rid_base, out_base, 377 - rid_len, rid, masked_rid - rid_base + out_base); 378 - return 0; 379 - } 380 - 381 - pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n", 382 - np, map_name, rid, target && *target ? *target : NULL); 383 - return -EFAULT; 384 - }
-131
drivers/of/of_pci_irq.c
··· 1 - #include <linux/kernel.h> 2 - #include <linux/of_pci.h> 3 - #include <linux/of_irq.h> 4 - #include <linux/export.h> 5 - 6 - /** 7 - * of_irq_parse_pci - Resolve the interrupt for a PCI device 8 - * @pdev: the device whose interrupt is to be resolved 9 - * @out_irq: structure of_irq filled by this function 10 - * 11 - * This function resolves the PCI interrupt for a given PCI device. If a 12 - * device-node exists for a given pci_dev, it will use normal OF tree 13 - * walking. If not, it will implement standard swizzling and walk up the 14 - * PCI tree until an device-node is found, at which point it will finish 15 - * resolving using the OF tree walking. 16 - */ 17 - int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args *out_irq) 18 - { 19 - struct device_node *dn, *ppnode; 20 - struct pci_dev *ppdev; 21 - __be32 laddr[3]; 22 - u8 pin; 23 - int rc; 24 - 25 - /* Check if we have a device node, if yes, fallback to standard 26 - * device tree parsing 27 - */ 28 - dn = pci_device_to_OF_node(pdev); 29 - if (dn) { 30 - rc = of_irq_parse_one(dn, 0, out_irq); 31 - if (!rc) 32 - return rc; 33 - } 34 - 35 - /* Ok, we don't, time to have fun. Let's start by building up an 36 - * interrupt spec. we assume #interrupt-cells is 1, which is standard 37 - * for PCI. If you do different, then don't use that routine. 38 - */ 39 - rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin); 40 - if (rc != 0) 41 - goto err; 42 - /* No pin, exit with no error message. */ 43 - if (pin == 0) 44 - return -ENODEV; 45 - 46 - /* Now we walk up the PCI tree */ 47 - for (;;) { 48 - /* Get the pci_dev of our parent */ 49 - ppdev = pdev->bus->self; 50 - 51 - /* Ouch, it's a host bridge... */ 52 - if (ppdev == NULL) { 53 - ppnode = pci_bus_to_OF_node(pdev->bus); 54 - 55 - /* No node for host bridge ? give up */ 56 - if (ppnode == NULL) { 57 - rc = -EINVAL; 58 - goto err; 59 - } 60 - } else { 61 - /* We found a P2P bridge, check if it has a node */ 62 - ppnode = pci_device_to_OF_node(ppdev); 63 - } 64 - 65 - /* Ok, we have found a parent with a device-node, hand over to 66 - * the OF parsing code. 67 - * We build a unit address from the linux device to be used for 68 - * resolution. Note that we use the linux bus number which may 69 - * not match your firmware bus numbering. 70 - * Fortunately, in most cases, interrupt-map-mask doesn't 71 - * include the bus number as part of the matching. 72 - * You should still be careful about that though if you intend 73 - * to rely on this function (you ship a firmware that doesn't 74 - * create device nodes for all PCI devices). 75 - */ 76 - if (ppnode) 77 - break; 78 - 79 - /* We can only get here if we hit a P2P bridge with no node, 80 - * let's do standard swizzling and try again 81 - */ 82 - pin = pci_swizzle_interrupt_pin(pdev, pin); 83 - pdev = ppdev; 84 - } 85 - 86 - out_irq->np = ppnode; 87 - out_irq->args_count = 1; 88 - out_irq->args[0] = pin; 89 - laddr[0] = cpu_to_be32((pdev->bus->number << 16) | (pdev->devfn << 8)); 90 - laddr[1] = laddr[2] = cpu_to_be32(0); 91 - rc = of_irq_parse_raw(laddr, out_irq); 92 - if (rc) 93 - goto err; 94 - return 0; 95 - err: 96 - if (rc == -ENOENT) { 97 - dev_warn(&pdev->dev, 98 - "%s: no interrupt-map found, INTx interrupts not available\n", 99 - __func__); 100 - pr_warn_once("%s: possibly some PCI slots don't have level triggered interrupts capability\n", 101 - __func__); 102 - } else { 103 - dev_err(&pdev->dev, "%s: failed with rc=%d\n", __func__, rc); 104 - } 105 - return rc; 106 - } 107 - EXPORT_SYMBOL_GPL(of_irq_parse_pci); 108 - 109 - /** 110 - * of_irq_parse_and_map_pci() - Decode a PCI irq from the device tree and map to a virq 111 - * @dev: The pci device needing an irq 112 - * @slot: PCI slot number; passed when used as map_irq callback. Unused 113 - * @pin: PCI irq pin number; passed when used as map_irq callback. Unused 114 - * 115 - * @slot and @pin are unused, but included in the function so that this 116 - * function can be used directly as the map_irq callback to 117 - * pci_assign_irq() and struct pci_host_bridge.map_irq pointer 118 - */ 119 - int of_irq_parse_and_map_pci(const struct pci_dev *dev, u8 slot, u8 pin) 120 - { 121 - struct of_phandle_args oirq; 122 - int ret; 123 - 124 - ret = of_irq_parse_pci(dev, &oirq); 125 - if (ret) 126 - return 0; /* Proper return code 0 == NO_IRQ */ 127 - 128 - return irq_create_of_mapping(&oirq); 129 - } 130 - EXPORT_SYMBOL_GPL(of_irq_parse_and_map_pci); 131 -
+513 -2
drivers/pci/of.c
··· 8 8 * as published by the Free Software Foundation; either version 9 9 * 2 of the License, or (at your option) any later version. 10 10 */ 11 + #define pr_fmt(fmt) "PCI: OF: " fmt 11 12 12 13 #include <linux/irqdomain.h> 13 14 #include <linux/kernel.h> 14 15 #include <linux/pci.h> 15 16 #include <linux/of.h> 16 17 #include <linux/of_irq.h> 18 + #include <linux/of_address.h> 17 19 #include <linux/of_pci.h> 18 20 #include "pci.h" 19 21 ··· 53 51 if (WARN_ON(bus->self || bus->parent)) 54 52 return NULL; 55 53 56 - /* Look for a node pointer in either the intermediary device we 57 - * create above the root bus or it's own parent. Normally only 54 + /* 55 + * Look for a node pointer in either the intermediary device we 56 + * create above the root bus or its own parent. Normally only 58 57 * the later is populated. 59 58 */ 60 59 if (bus->bridge->of_node) ··· 91 88 return NULL; 92 89 #endif 93 90 } 91 + 92 + 93 + static inline int __of_pci_pci_compare(struct device_node *node, 94 + unsigned int data) 95 + { 96 + int devfn; 97 + 98 + devfn = of_pci_get_devfn(node); 99 + if (devfn < 0) 100 + return 0; 101 + 102 + return devfn == data; 103 + } 104 + 105 + struct device_node *of_pci_find_child_device(struct device_node *parent, 106 + unsigned int devfn) 107 + { 108 + struct device_node *node, *node2; 109 + 110 + for_each_child_of_node(parent, node) { 111 + if (__of_pci_pci_compare(node, devfn)) 112 + return node; 113 + /* 114 + * Some OFs create a parent node "multifunc-device" as 115 + * a fake root for all functions of a multi-function 116 + * device we go down them as well. 117 + */ 118 + if (!strcmp(node->name, "multifunc-device")) { 119 + for_each_child_of_node(node, node2) { 120 + if (__of_pci_pci_compare(node2, devfn)) { 121 + of_node_put(node); 122 + return node2; 123 + } 124 + } 125 + } 126 + } 127 + return NULL; 128 + } 129 + EXPORT_SYMBOL_GPL(of_pci_find_child_device); 130 + 131 + /** 132 + * of_pci_get_devfn() - Get device and function numbers for a device node 133 + * @np: device node 134 + * 135 + * Parses a standard 5-cell PCI resource and returns an 8-bit value that can 136 + * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device 137 + * and function numbers respectively. On error a negative error code is 138 + * returned. 139 + */ 140 + int of_pci_get_devfn(struct device_node *np) 141 + { 142 + u32 reg[5]; 143 + int error; 144 + 145 + error = of_property_read_u32_array(np, "reg", reg, ARRAY_SIZE(reg)); 146 + if (error) 147 + return error; 148 + 149 + return (reg[0] >> 8) & 0xff; 150 + } 151 + EXPORT_SYMBOL_GPL(of_pci_get_devfn); 152 + 153 + /** 154 + * of_pci_parse_bus_range() - parse the bus-range property of a PCI device 155 + * @node: device node 156 + * @res: address to a struct resource to return the bus-range 157 + * 158 + * Returns 0 on success or a negative error-code on failure. 159 + */ 160 + int of_pci_parse_bus_range(struct device_node *node, struct resource *res) 161 + { 162 + u32 bus_range[2]; 163 + int error; 164 + 165 + error = of_property_read_u32_array(node, "bus-range", bus_range, 166 + ARRAY_SIZE(bus_range)); 167 + if (error) 168 + return error; 169 + 170 + res->name = node->name; 171 + res->start = bus_range[0]; 172 + res->end = bus_range[1]; 173 + res->flags = IORESOURCE_BUS; 174 + 175 + return 0; 176 + } 177 + EXPORT_SYMBOL_GPL(of_pci_parse_bus_range); 178 + 179 + /** 180 + * This function will try to obtain the host bridge domain number by 181 + * finding a property called "linux,pci-domain" of the given device node. 182 + * 183 + * @node: device tree node with the domain information 184 + * 185 + * Returns the associated domain number from DT in the range [0-0xffff], or 186 + * a negative value if the required property is not found. 187 + */ 188 + int of_get_pci_domain_nr(struct device_node *node) 189 + { 190 + u32 domain; 191 + int error; 192 + 193 + error = of_property_read_u32(node, "linux,pci-domain", &domain); 194 + if (error) 195 + return error; 196 + 197 + return (u16)domain; 198 + } 199 + EXPORT_SYMBOL_GPL(of_get_pci_domain_nr); 200 + 201 + /** 202 + * This function will try to find the limitation of link speed by finding 203 + * a property called "max-link-speed" of the given device node. 204 + * 205 + * @node: device tree node with the max link speed information 206 + * 207 + * Returns the associated max link speed from DT, or a negative value if the 208 + * required property is not found or is invalid. 209 + */ 210 + int of_pci_get_max_link_speed(struct device_node *node) 211 + { 212 + u32 max_link_speed; 213 + 214 + if (of_property_read_u32(node, "max-link-speed", &max_link_speed) || 215 + max_link_speed > 4) 216 + return -EINVAL; 217 + 218 + return max_link_speed; 219 + } 220 + EXPORT_SYMBOL_GPL(of_pci_get_max_link_speed); 221 + 222 + /** 223 + * of_pci_check_probe_only - Setup probe only mode if linux,pci-probe-only 224 + * is present and valid 225 + */ 226 + void of_pci_check_probe_only(void) 227 + { 228 + u32 val; 229 + int ret; 230 + 231 + ret = of_property_read_u32(of_chosen, "linux,pci-probe-only", &val); 232 + if (ret) { 233 + if (ret == -ENODATA || ret == -EOVERFLOW) 234 + pr_warn("linux,pci-probe-only without valid value, ignoring\n"); 235 + return; 236 + } 237 + 238 + if (val) 239 + pci_add_flags(PCI_PROBE_ONLY); 240 + else 241 + pci_clear_flags(PCI_PROBE_ONLY); 242 + 243 + pr_info("PROBE_ONLY %sabled\n", val ? "en" : "dis"); 244 + } 245 + EXPORT_SYMBOL_GPL(of_pci_check_probe_only); 246 + 247 + #if defined(CONFIG_OF_ADDRESS) 248 + /** 249 + * of_pci_get_host_bridge_resources - Parse PCI host bridge resources from DT 250 + * @dev: device node of the host bridge having the range property 251 + * @busno: bus number associated with the bridge root bus 252 + * @bus_max: maximum number of buses for this bridge 253 + * @resources: list where the range of resources will be added after DT parsing 254 + * @io_base: pointer to a variable that will contain on return the physical 255 + * address for the start of the I/O range. Can be NULL if the caller doesn't 256 + * expect I/O ranges to be present in the device tree. 257 + * 258 + * It is the caller's job to free the @resources list. 259 + * 260 + * This function will parse the "ranges" property of a PCI host bridge device 261 + * node and setup the resource mapping based on its content. It is expected 262 + * that the property conforms with the Power ePAPR document. 263 + * 264 + * It returns zero if the range parsing has been successful or a standard error 265 + * value if it failed. 266 + */ 267 + int of_pci_get_host_bridge_resources(struct device_node *dev, 268 + unsigned char busno, unsigned char bus_max, 269 + struct list_head *resources, resource_size_t *io_base) 270 + { 271 + struct resource_entry *window; 272 + struct resource *res; 273 + struct resource *bus_range; 274 + struct of_pci_range range; 275 + struct of_pci_range_parser parser; 276 + char range_type[4]; 277 + int err; 278 + 279 + if (io_base) 280 + *io_base = (resource_size_t)OF_BAD_ADDR; 281 + 282 + bus_range = kzalloc(sizeof(*bus_range), GFP_KERNEL); 283 + if (!bus_range) 284 + return -ENOMEM; 285 + 286 + pr_info("host bridge %pOF ranges:\n", dev); 287 + 288 + err = of_pci_parse_bus_range(dev, bus_range); 289 + if (err) { 290 + bus_range->start = busno; 291 + bus_range->end = bus_max; 292 + bus_range->flags = IORESOURCE_BUS; 293 + pr_info(" No bus range found for %pOF, using %pR\n", 294 + dev, bus_range); 295 + } else { 296 + if (bus_range->end > bus_range->start + bus_max) 297 + bus_range->end = bus_range->start + bus_max; 298 + } 299 + pci_add_resource(resources, bus_range); 300 + 301 + /* Check for ranges property */ 302 + err = of_pci_range_parser_init(&parser, dev); 303 + if (err) 304 + goto parse_failed; 305 + 306 + pr_debug("Parsing ranges property...\n"); 307 + for_each_of_pci_range(&parser, &range) { 308 + /* Read next ranges element */ 309 + if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO) 310 + snprintf(range_type, 4, " IO"); 311 + else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM) 312 + snprintf(range_type, 4, "MEM"); 313 + else 314 + snprintf(range_type, 4, "err"); 315 + pr_info(" %s %#010llx..%#010llx -> %#010llx\n", range_type, 316 + range.cpu_addr, range.cpu_addr + range.size - 1, 317 + range.pci_addr); 318 + 319 + /* 320 + * If we failed translation or got a zero-sized region 321 + * then skip this range 322 + */ 323 + if (range.cpu_addr == OF_BAD_ADDR || range.size == 0) 324 + continue; 325 + 326 + res = kzalloc(sizeof(struct resource), GFP_KERNEL); 327 + if (!res) { 328 + err = -ENOMEM; 329 + goto parse_failed; 330 + } 331 + 332 + err = of_pci_range_to_resource(&range, dev, res); 333 + if (err) { 334 + kfree(res); 335 + continue; 336 + } 337 + 338 + if (resource_type(res) == IORESOURCE_IO) { 339 + if (!io_base) { 340 + pr_err("I/O range found for %pOF. Please provide an io_base pointer to save CPU base address\n", 341 + dev); 342 + err = -EINVAL; 343 + goto conversion_failed; 344 + } 345 + if (*io_base != (resource_size_t)OF_BAD_ADDR) 346 + pr_warn("More than one I/O resource converted for %pOF. CPU base address for old range lost!\n", 347 + dev); 348 + *io_base = range.cpu_addr; 349 + } 350 + 351 + pci_add_resource_offset(resources, res, res->start - range.pci_addr); 352 + } 353 + 354 + return 0; 355 + 356 + conversion_failed: 357 + kfree(res); 358 + parse_failed: 359 + resource_list_for_each_entry(window, resources) 360 + kfree(window->res); 361 + pci_free_resource_list(resources); 362 + return err; 363 + } 364 + EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources); 365 + #endif /* CONFIG_OF_ADDRESS */ 366 + 367 + /** 368 + * of_pci_map_rid - Translate a requester ID through a downstream mapping. 369 + * @np: root complex device node. 370 + * @rid: PCI requester ID to map. 371 + * @map_name: property name of the map to use. 372 + * @map_mask_name: optional property name of the mask to use. 373 + * @target: optional pointer to a target device node. 374 + * @id_out: optional pointer to receive the translated ID. 375 + * 376 + * Given a PCI requester ID, look up the appropriate implementation-defined 377 + * platform ID and/or the target device which receives transactions on that 378 + * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or 379 + * @id_out may be NULL if only the other is required. If @target points to 380 + * a non-NULL device node pointer, only entries targeting that node will be 381 + * matched; if it points to a NULL value, it will receive the device node of 382 + * the first matching target phandle, with a reference held. 383 + * 384 + * Return: 0 on success or a standard error code on failure. 385 + */ 386 + int of_pci_map_rid(struct device_node *np, u32 rid, 387 + const char *map_name, const char *map_mask_name, 388 + struct device_node **target, u32 *id_out) 389 + { 390 + u32 map_mask, masked_rid; 391 + int map_len; 392 + const __be32 *map = NULL; 393 + 394 + if (!np || !map_name || (!target && !id_out)) 395 + return -EINVAL; 396 + 397 + map = of_get_property(np, map_name, &map_len); 398 + if (!map) { 399 + if (target) 400 + return -ENODEV; 401 + /* Otherwise, no map implies no translation */ 402 + *id_out = rid; 403 + return 0; 404 + } 405 + 406 + if (!map_len || map_len % (4 * sizeof(*map))) { 407 + pr_err("%pOF: Error: Bad %s length: %d\n", np, 408 + map_name, map_len); 409 + return -EINVAL; 410 + } 411 + 412 + /* The default is to select all bits. */ 413 + map_mask = 0xffffffff; 414 + 415 + /* 416 + * Can be overridden by "{iommu,msi}-map-mask" property. 417 + * If of_property_read_u32() fails, the default is used. 418 + */ 419 + if (map_mask_name) 420 + of_property_read_u32(np, map_mask_name, &map_mask); 421 + 422 + masked_rid = map_mask & rid; 423 + for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) { 424 + struct device_node *phandle_node; 425 + u32 rid_base = be32_to_cpup(map + 0); 426 + u32 phandle = be32_to_cpup(map + 1); 427 + u32 out_base = be32_to_cpup(map + 2); 428 + u32 rid_len = be32_to_cpup(map + 3); 429 + 430 + if (rid_base & ~map_mask) { 431 + pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n", 432 + np, map_name, map_name, 433 + map_mask, rid_base); 434 + return -EFAULT; 435 + } 436 + 437 + if (masked_rid < rid_base || masked_rid >= rid_base + rid_len) 438 + continue; 439 + 440 + phandle_node = of_find_node_by_phandle(phandle); 441 + if (!phandle_node) 442 + return -ENODEV; 443 + 444 + if (target) { 445 + if (*target) 446 + of_node_put(phandle_node); 447 + else 448 + *target = phandle_node; 449 + 450 + if (*target != phandle_node) 451 + continue; 452 + } 453 + 454 + if (id_out) 455 + *id_out = masked_rid - rid_base + out_base; 456 + 457 + pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n", 458 + np, map_name, map_mask, rid_base, out_base, 459 + rid_len, rid, masked_rid - rid_base + out_base); 460 + return 0; 461 + } 462 + 463 + pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n", 464 + np, map_name, rid, target && *target ? *target : NULL); 465 + return -EFAULT; 466 + } 467 + 468 + #if IS_ENABLED(CONFIG_OF_IRQ) 469 + /** 470 + * of_irq_parse_pci - Resolve the interrupt for a PCI device 471 + * @pdev: the device whose interrupt is to be resolved 472 + * @out_irq: structure of_irq filled by this function 473 + * 474 + * This function resolves the PCI interrupt for a given PCI device. If a 475 + * device-node exists for a given pci_dev, it will use normal OF tree 476 + * walking. If not, it will implement standard swizzling and walk up the 477 + * PCI tree until an device-node is found, at which point it will finish 478 + * resolving using the OF tree walking. 479 + */ 480 + int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args *out_irq) 481 + { 482 + struct device_node *dn, *ppnode; 483 + struct pci_dev *ppdev; 484 + __be32 laddr[3]; 485 + u8 pin; 486 + int rc; 487 + 488 + /* 489 + * Check if we have a device node, if yes, fallback to standard 490 + * device tree parsing 491 + */ 492 + dn = pci_device_to_OF_node(pdev); 493 + if (dn) { 494 + rc = of_irq_parse_one(dn, 0, out_irq); 495 + if (!rc) 496 + return rc; 497 + } 498 + 499 + /* 500 + * Ok, we don't, time to have fun. Let's start by building up an 501 + * interrupt spec. we assume #interrupt-cells is 1, which is standard 502 + * for PCI. If you do different, then don't use that routine. 503 + */ 504 + rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin); 505 + if (rc != 0) 506 + goto err; 507 + /* No pin, exit with no error message. */ 508 + if (pin == 0) 509 + return -ENODEV; 510 + 511 + /* Now we walk up the PCI tree */ 512 + for (;;) { 513 + /* Get the pci_dev of our parent */ 514 + ppdev = pdev->bus->self; 515 + 516 + /* Ouch, it's a host bridge... */ 517 + if (ppdev == NULL) { 518 + ppnode = pci_bus_to_OF_node(pdev->bus); 519 + 520 + /* No node for host bridge ? give up */ 521 + if (ppnode == NULL) { 522 + rc = -EINVAL; 523 + goto err; 524 + } 525 + } else { 526 + /* We found a P2P bridge, check if it has a node */ 527 + ppnode = pci_device_to_OF_node(ppdev); 528 + } 529 + 530 + /* 531 + * Ok, we have found a parent with a device-node, hand over to 532 + * the OF parsing code. 533 + * We build a unit address from the linux device to be used for 534 + * resolution. Note that we use the linux bus number which may 535 + * not match your firmware bus numbering. 536 + * Fortunately, in most cases, interrupt-map-mask doesn't 537 + * include the bus number as part of the matching. 538 + * You should still be careful about that though if you intend 539 + * to rely on this function (you ship a firmware that doesn't 540 + * create device nodes for all PCI devices). 541 + */ 542 + if (ppnode) 543 + break; 544 + 545 + /* 546 + * We can only get here if we hit a P2P bridge with no node; 547 + * let's do standard swizzling and try again 548 + */ 549 + pin = pci_swizzle_interrupt_pin(pdev, pin); 550 + pdev = ppdev; 551 + } 552 + 553 + out_irq->np = ppnode; 554 + out_irq->args_count = 1; 555 + out_irq->args[0] = pin; 556 + laddr[0] = cpu_to_be32((pdev->bus->number << 16) | (pdev->devfn << 8)); 557 + laddr[1] = laddr[2] = cpu_to_be32(0); 558 + rc = of_irq_parse_raw(laddr, out_irq); 559 + if (rc) 560 + goto err; 561 + return 0; 562 + err: 563 + if (rc == -ENOENT) { 564 + dev_warn(&pdev->dev, 565 + "%s: no interrupt-map found, INTx interrupts not available\n", 566 + __func__); 567 + pr_warn_once("%s: possibly some PCI slots don't have level triggered interrupts capability\n", 568 + __func__); 569 + } else { 570 + dev_err(&pdev->dev, "%s: failed with rc=%d\n", __func__, rc); 571 + } 572 + return rc; 573 + } 574 + EXPORT_SYMBOL_GPL(of_irq_parse_pci); 575 + 576 + /** 577 + * of_irq_parse_and_map_pci() - Decode a PCI IRQ from the device tree and map to a VIRQ 578 + * @dev: The PCI device needing an IRQ 579 + * @slot: PCI slot number; passed when used as map_irq callback. Unused 580 + * @pin: PCI IRQ pin number; passed when used as map_irq callback. Unused 581 + * 582 + * @slot and @pin are unused, but included in the function so that this 583 + * function can be used directly as the map_irq callback to 584 + * pci_assign_irq() and struct pci_host_bridge.map_irq pointer 585 + */ 586 + int of_irq_parse_and_map_pci(const struct pci_dev *dev, u8 slot, u8 pin) 587 + { 588 + struct of_phandle_args oirq; 589 + int ret; 590 + 591 + ret = of_irq_parse_pci(dev, &oirq); 592 + if (ret) 593 + return 0; /* Proper return code 0 == NO_IRQ */ 594 + 595 + return irq_create_of_mapping(&oirq); 596 + } 597 + EXPORT_SYMBOL_GPL(of_irq_parse_and_map_pci); 598 + #endif /* CONFIG_OF_IRQ */
+1 -1
include/linux/of_pci.h
··· 9 9 struct of_phandle_args; 10 10 struct device_node; 11 11 12 - #ifdef CONFIG_OF_PCI 12 + #if IS_ENABLED(CONFIG_OF) && IS_ENABLED(CONFIG_PCI) 13 13 int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args *out_irq); 14 14 struct device_node *of_pci_find_child_device(struct device_node *parent, 15 15 unsigned int devfn);