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

pci/of: Match PCI devices to OF nodes dynamically

powerpc has two different ways of matching PCI devices to their
corresponding OF node (if any) for historical reasons. The ppc64 one
does a scan looking for matching bus/dev/fn, while the ppc32 one does a
scan looking only for matching dev/fn on each level in order to be
agnostic to busses being renumbered (which Linux does on some
platforms).

This removes both and instead moves the matching code to the PCI core
itself. It's the most logical place to do it: when a pci_dev is created,
we know the parent and thus can do a single level scan for the matching
device_node (if any).

The benefit is that all archs now get the matching for free. There's one
hook the arch might want to provide to match a PHB bus to its device
node. A default weak implementation is provided that looks for the
parent device device node, but it's not entirely reliable on powerpc for
various reasons so powerpc provides its own.

Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Acked-by: Michal Simek <monstr@monstr.eu>
Acked-by: Jesse Barnes <jbarnes@virtuousgeek.org>

+278 -379
+7 -7
arch/microblaze/include/asm/pci-bridge.h
··· 105 105 }; 106 106 107 107 #ifdef CONFIG_PCI 108 - static inline struct pci_controller *pci_bus_to_host(const struct pci_bus *bus) 108 + static inline struct device_node *pci_device_to_OF_node(struct pci_dev *dev) 109 109 { 110 - return bus->sysdata; 110 + return dev->dev.of_node; 111 111 } 112 112 113 113 static inline struct device_node *pci_bus_to_OF_node(struct pci_bus *bus) 114 114 { 115 - struct pci_controller *host; 115 + return bus->dev.of_node; 116 + } 116 117 117 - if (bus->self) 118 - return pci_device_to_OF_node(bus->self); 119 - host = pci_bus_to_host(bus); 120 - return host ? host->dn : NULL; 118 + static inline struct pci_controller *pci_bus_to_host(const struct pci_bus *bus) 119 + { 120 + return bus->sysdata; 121 121 } 122 122 123 123 static inline int isa_vaddr_is_ioport(void __iomem *address)
-15
arch/microblaze/include/asm/prom.h
··· 29 29 extern int early_uartlite_console(void); 30 30 extern int early_uart16550_console(void); 31 31 32 - #ifdef CONFIG_PCI 33 - /* 34 - * PCI <-> OF matching functions 35 - * (XXX should these be here?) 36 - */ 37 - struct pci_bus; 38 - struct pci_dev; 39 - extern int pci_device_from_OF_node(struct device_node *node, 40 - u8 *bus, u8 *devfn); 41 - extern struct device_node *pci_busdev_to_OF_node(struct pci_bus *bus, 42 - int devfn); 43 - extern struct device_node *pci_device_to_OF_node(struct pci_dev *dev); 44 - extern void pci_create_OF_bus_map(void); 45 - #endif 46 - 47 32 /* 48 33 * OF address retreival & translation 49 34 */
+7 -33
arch/microblaze/pci/pci_32.c
··· 210 210 return np; 211 211 } 212 212 213 - /* 214 - * Scans the OF tree for a device node matching a PCI device 215 - */ 216 - struct device_node * 217 - pci_busdev_to_OF_node(struct pci_bus *bus, int devfn) 218 - { 219 - struct device_node *parent, *np; 220 - 221 - pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn); 222 - parent = scan_OF_for_pci_bus(bus); 223 - if (parent == NULL) 224 - return NULL; 225 - pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>"); 226 - np = scan_OF_for_pci_dev(parent, devfn); 227 - of_node_put(parent); 228 - pr_debug(" result is %s\n", np ? np->full_name : "<NULL>"); 229 - 230 - /* XXX most callers don't release the returned node 231 - * mostly because ppc64 doesn't increase the refcount, 232 - * we need to fix that. 233 - */ 234 - return np; 235 - } 236 - EXPORT_SYMBOL(pci_busdev_to_OF_node); 237 - 238 - struct device_node* 239 - pci_device_to_OF_node(struct pci_dev *dev) 240 - { 241 - return pci_busdev_to_OF_node(dev->bus, dev->devfn); 242 - } 243 - EXPORT_SYMBOL(pci_device_to_OF_node); 244 - 245 213 static int 246 214 find_OF_pci_device_filter(struct device_node *node, void *data) 247 215 { ··· 283 315 } 284 316 } 285 317 318 + struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus) 319 + { 320 + struct pci_controller *hose = bus->sysdata; 321 + 322 + return of_node_get(hose->dn); 323 + } 324 + 286 325 static void __devinit pcibios_scan_phb(struct pci_controller *hose) 287 326 { 288 327 struct pci_bus *bus; ··· 307 332 hose->global_number); 308 333 return; 309 334 } 310 - bus.dev->of_node = of_node_get(node); 311 335 bus->secondary = hose->first_busno; 312 336 hose->bus = bus; 313 337
+11 -24
arch/powerpc/include/asm/pci-bridge.h
··· 169 169 return bus->sysdata; 170 170 } 171 171 172 - #ifndef CONFIG_PPC64 172 + static inline struct device_node *pci_device_to_OF_node(struct pci_dev *dev) 173 + { 174 + return dev->dev.of_node; 175 + } 173 176 174 177 static inline struct device_node *pci_bus_to_OF_node(struct pci_bus *bus) 175 178 { 176 - struct pci_controller *host; 177 - 178 - if (bus->self) 179 - return pci_device_to_OF_node(bus->self); 180 - host = pci_bus_to_host(bus); 181 - return host ? host->dn : NULL; 179 + return bus->dev.of_node; 182 180 } 181 + 182 + #ifndef CONFIG_PPC64 183 + 184 + extern int pci_device_from_OF_node(struct device_node *node, 185 + u8 *bus, u8 *devfn); 186 + extern void pci_create_OF_bus_map(void); 183 187 184 188 static inline int isa_vaddr_is_ioport(void __iomem *address) 185 189 { ··· 227 223 /* Get the pointer to a device_node's pci_dn */ 228 224 #define PCI_DN(dn) ((struct pci_dn *) (dn)->data) 229 225 230 - extern struct device_node *fetch_dev_dn(struct pci_dev *dev); 231 226 extern void * update_dn_pci_info(struct device_node *dn, void *data); 232 - 233 - /* Get a device_node from a pci_dev. This code must be fast except 234 - * in the case where the sysdata is incorrect and needs to be fixed 235 - * up (this will only happen once). */ 236 - static inline struct device_node *pci_device_to_OF_node(struct pci_dev *dev) 237 - { 238 - return dev->dev.of_node ? dev->dev.of_node : fetch_dev_dn(dev); 239 - } 240 227 241 228 static inline int pci_device_from_OF_node(struct device_node *np, 242 229 u8 *bus, u8 *devfn) ··· 237 242 *bus = PCI_DN(np)->busno; 238 243 *devfn = PCI_DN(np)->devfn; 239 244 return 0; 240 - } 241 - 242 - static inline struct device_node *pci_bus_to_OF_node(struct pci_bus *bus) 243 - { 244 - if (bus->self) 245 - return pci_device_to_OF_node(bus->self); 246 - else 247 - return bus->dev.of_node; /* Must be root bus (PHB) */ 248 245 } 249 246 250 247 /** Find the bus corresponding to the indicated device node */
+1 -2
arch/powerpc/include/asm/pci.h
··· 179 179 extern struct pci_dev *of_create_pci_dev(struct device_node *node, 180 180 struct pci_bus *bus, int devfn); 181 181 182 - extern void of_scan_pci_bridge(struct device_node *node, 183 - struct pci_dev *dev); 182 + extern void of_scan_pci_bridge(struct pci_dev *dev); 184 183 185 184 extern void of_scan_bus(struct device_node *node, struct pci_bus *bus); 186 185 extern void of_rescan_bus(struct device_node *node, struct pci_bus *bus);
-14
arch/powerpc/include/asm/prom.h
··· 22 22 23 23 #define HAVE_ARCH_DEVTREE_FIXUPS 24 24 25 - #ifdef CONFIG_PPC32 26 - /* 27 - * PCI <-> OF matching functions 28 - * (XXX should these be here?) 29 - */ 30 - struct pci_bus; 31 - struct pci_dev; 32 - extern int pci_device_from_OF_node(struct device_node *node, 33 - u8* bus, u8* devfn); 34 - extern struct device_node* pci_busdev_to_OF_node(struct pci_bus *, int); 35 - extern struct device_node* pci_device_to_OF_node(struct pci_dev *); 36 - extern void pci_create_OF_bus_map(void); 37 - #endif 38 - 39 25 /* 40 26 * OF address retreival & translation 41 27 */
+7 -4
arch/powerpc/kernel/pci-common.c
··· 1097 1097 if (dev->is_added) 1098 1098 continue; 1099 1099 1100 - /* Setup OF node pointer in the device */ 1101 - dev->dev.of_node = pci_device_to_OF_node(dev); 1102 - 1103 1100 /* Fixup NUMA node as it may not be setup yet by the generic 1104 1101 * code and is needed by the DMA init 1105 1102 */ ··· 1682 1685 return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap); 1683 1686 } 1684 1687 1688 + struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus) 1689 + { 1690 + struct pci_controller *hose = bus->sysdata; 1691 + 1692 + return of_node_get(hose->dn); 1693 + } 1694 + 1685 1695 /** 1686 1696 * pci_scan_phb - Given a pci_controller, setup and scan the PCI bus 1687 1697 * @hose: Pointer to the PCI host controller instance structure ··· 1709 1705 hose->global_number); 1710 1706 return; 1711 1707 } 1712 - bus->dev.of_node = of_node_get(node); 1713 1708 bus->secondary = hose->first_busno; 1714 1709 hose->bus = bus; 1715 1710
+13 -137
arch/powerpc/kernel/pci_32.c
··· 167 167 #endif 168 168 } 169 169 170 - typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data); 171 - 172 - static struct device_node* 173 - scan_OF_pci_childs(struct device_node *parent, pci_OF_scan_iterator filter, void* data) 174 - { 175 - struct device_node *node; 176 - struct device_node* sub_node; 177 - 178 - for_each_child_of_node(parent, node) { 179 - const unsigned int *class_code; 180 - 181 - if (filter(node, data)) { 182 - of_node_put(node); 183 - return node; 184 - } 185 - 186 - /* For PCI<->PCI bridges or CardBus bridges, we go down 187 - * Note: some OFs create a parent node "multifunc-device" as 188 - * a fake root for all functions of a multi-function device, 189 - * we go down them as well. 190 - */ 191 - class_code = of_get_property(node, "class-code", NULL); 192 - if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI && 193 - (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) && 194 - strcmp(node->name, "multifunc-device")) 195 - continue; 196 - sub_node = scan_OF_pci_childs(node, filter, data); 197 - if (sub_node) { 198 - of_node_put(node); 199 - return sub_node; 200 - } 201 - } 202 - return NULL; 203 - } 204 - 205 - static struct device_node *scan_OF_for_pci_dev(struct device_node *parent, 206 - unsigned int devfn) 207 - { 208 - struct device_node *np, *cnp; 209 - const u32 *reg; 210 - unsigned int psize; 211 - 212 - for_each_child_of_node(parent, np) { 213 - reg = of_get_property(np, "reg", &psize); 214 - if (reg && psize >= 4 && ((reg[0] >> 8) & 0xff) == devfn) 215 - return np; 216 - 217 - /* Note: some OFs create a parent node "multifunc-device" as 218 - * a fake root for all functions of a multi-function device, 219 - * we go down them as well. */ 220 - if (!strcmp(np->name, "multifunc-device")) { 221 - cnp = scan_OF_for_pci_dev(np, devfn); 222 - if (cnp) 223 - return cnp; 224 - } 225 - } 226 - return NULL; 227 - } 228 - 229 - 230 - static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus) 231 - { 232 - struct device_node *parent, *np; 233 - 234 - /* Are we a root bus ? */ 235 - if (bus->self == NULL || bus->parent == NULL) { 236 - struct pci_controller *hose = pci_bus_to_host(bus); 237 - if (hose == NULL) 238 - return NULL; 239 - return of_node_get(hose->dn); 240 - } 241 - 242 - /* not a root bus, we need to get our parent */ 243 - parent = scan_OF_for_pci_bus(bus->parent); 244 - if (parent == NULL) 245 - return NULL; 246 - 247 - /* now iterate for children for a match */ 248 - np = scan_OF_for_pci_dev(parent, bus->self->devfn); 249 - of_node_put(parent); 250 - 251 - return np; 252 - } 253 - 254 - /* 255 - * Scans the OF tree for a device node matching a PCI device 256 - */ 257 - struct device_node * 258 - pci_busdev_to_OF_node(struct pci_bus *bus, int devfn) 259 - { 260 - struct device_node *parent, *np; 261 - 262 - pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn); 263 - parent = scan_OF_for_pci_bus(bus); 264 - if (parent == NULL) 265 - return NULL; 266 - pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>"); 267 - np = scan_OF_for_pci_dev(parent, devfn); 268 - of_node_put(parent); 269 - pr_debug(" result is %s\n", np ? np->full_name : "<NULL>"); 270 - 271 - /* XXX most callers don't release the returned node 272 - * mostly because ppc64 doesn't increase the refcount, 273 - * we need to fix that. 274 - */ 275 - return np; 276 - } 277 - EXPORT_SYMBOL(pci_busdev_to_OF_node); 278 - 279 - struct device_node* 280 - pci_device_to_OF_node(struct pci_dev *dev) 281 - { 282 - return pci_busdev_to_OF_node(dev->bus, dev->devfn); 283 - } 284 - EXPORT_SYMBOL(pci_device_to_OF_node); 285 - 286 - static int 287 - find_OF_pci_device_filter(struct device_node* node, void* data) 288 - { 289 - return ((void *)node == data); 290 - } 291 170 292 171 /* 293 172 * Returns the PCI device matching a given OF node 294 173 */ 295 - int 296 - pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn) 174 + int pci_device_from_OF_node(struct device_node *node, u8 *bus, u8 *devfn) 297 175 { 298 - const unsigned int *reg; 299 - struct pci_controller* hose; 300 - struct pci_dev* dev = NULL; 301 - 302 - /* Make sure it's really a PCI device */ 303 - hose = pci_find_hose_for_OF_device(node); 304 - if (!hose || !hose->dn) 176 + struct pci_dev *dev = NULL; 177 + const __be32 *reg; 178 + int size; 179 + 180 + /* Check if it might have a chance to be a PCI device */ 181 + if (!pci_find_hose_for_OF_device(node)) 305 182 return -ENODEV; 306 - if (!scan_OF_pci_childs(hose->dn, 307 - find_OF_pci_device_filter, (void *)node)) 183 + 184 + reg = of_get_property(node, "reg", &size); 185 + if (!reg || size < 5 * sizeof(u32)) 308 186 return -ENODEV; 309 - reg = of_get_property(node, "reg", NULL); 310 - if (!reg) 311 - return -ENODEV; 312 - *bus = (reg[0] >> 16) & 0xff; 313 - *devfn = ((reg[0] >> 8) & 0xff); 187 + 188 + *bus = (be32_to_cpup(&reg[0]) >> 16) & 0xff; 189 + *devfn = (be32_to_cpup(&reg[0]) >> 8) & 0xff; 314 190 315 191 /* Ok, here we need some tweak. If we have already renumbered 316 192 * all busses, we can't rely on the OF bus number any more.
-47
arch/powerpc/kernel/pci_dn.c
··· 142 142 traverse_pci_devices(dn, update_dn_pci_info, phb); 143 143 } 144 144 145 - /* 146 - * Traversal func that looks for a <busno,devfcn> value. 147 - * If found, the pci_dn is returned (thus terminating the traversal). 148 - */ 149 - static void *is_devfn_node(struct device_node *dn, void *data) 150 - { 151 - int busno = ((unsigned long)data >> 8) & 0xff; 152 - int devfn = ((unsigned long)data) & 0xff; 153 - struct pci_dn *pci = dn->data; 154 - 155 - if (pci && (devfn == pci->devfn) && (busno == pci->busno)) 156 - return dn; 157 - return NULL; 158 - } 159 - 160 - /* 161 - * This is the "slow" path for looking up a device_node from a 162 - * pci_dev. It will hunt for the device under its parent's 163 - * phb and then update of_node pointer. 164 - * 165 - * It may also do fixups on the actual device since this happens 166 - * on the first read/write. 167 - * 168 - * Note that it also must deal with devices that don't exist. 169 - * In this case it may probe for real hardware ("just in case") 170 - * and add a device_node to the device tree if necessary. 171 - * 172 - * Is this function necessary anymore now that dev->dev.of_node is 173 - * used to store the node pointer? 174 - * 175 - */ 176 - struct device_node *fetch_dev_dn(struct pci_dev *dev) 177 - { 178 - struct pci_controller *phb = dev->sysdata; 179 - struct device_node *dn; 180 - unsigned long searchval = (dev->bus->number << 8) | dev->devfn; 181 - 182 - if (WARN_ON(!phb)) 183 - return NULL; 184 - 185 - dn = traverse_pci_devices(phb->dn, is_devfn_node, (void *)searchval); 186 - if (dn) 187 - dev->dev.of_node = dn; 188 - return dn; 189 - } 190 - EXPORT_SYMBOL(fetch_dev_dn); 191 - 192 145 /** 193 146 * pci_devs_phb_init - Initialize phbs and pci devs under them. 194 147 *
+3 -6
arch/powerpc/kernel/pci_of_scan.c
··· 202 202 * this routine in turn call of_scan_bus() recusively to scan for more child 203 203 * devices. 204 204 */ 205 - void __devinit of_scan_pci_bridge(struct device_node *node, 206 - struct pci_dev *dev) 205 + void __devinit of_scan_pci_bridge(struct pci_dev *dev) 207 206 { 207 + struct device_node *node = dev->dev.of_node; 208 208 struct pci_bus *bus; 209 209 const u32 *busrange, *ranges; 210 210 int len, i, mode; ··· 238 238 bus->primary = dev->bus->number; 239 239 bus->subordinate = busrange[1]; 240 240 bus->bridge_ctl = 0; 241 - bus->dev.of_node = of_node_get(node); 242 241 243 242 /* parse ranges property */ 244 243 /* PCI #address-cells == 3 and #size-cells == 2 always */ ··· 334 335 list_for_each_entry(dev, &bus->devices, bus_list) { 335 336 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || 336 337 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { 337 - struct device_node *child = pci_device_to_OF_node(dev); 338 - if (child) 339 - of_scan_pci_bridge(child, dev); 338 + of_scan_pci_bridge(dev); 340 339 } 341 340 } 342 341 }
+2 -1
arch/powerpc/platforms/powermac/pci.c
··· 17 17 #include <linux/init.h> 18 18 #include <linux/bootmem.h> 19 19 #include <linux/irq.h> 20 + #include <linux/of_pci.h> 20 21 21 22 #include <asm/sections.h> 22 23 #include <asm/io.h> ··· 236 235 237 236 if (offset >= 0x100) 238 237 return PCIBIOS_BAD_REGISTER_NUMBER; 239 - np = pci_busdev_to_OF_node(bus, devfn); 238 + np = of_pci_find_child_device(bus->dev.of_node, devfn); 240 239 if (np == NULL) 241 240 return PCIBIOS_DEVICE_NOT_FOUND; 242 241
+1 -1
arch/sparc/kernel/pci.c
··· 284 284 dev->sysdata = node; 285 285 dev->dev.parent = bus->bridge; 286 286 dev->dev.bus = &pci_bus_type; 287 - dev->dev.of_node = node; 287 + dev->dev.of_node = of_node_get(node); 288 288 dev->devfn = devfn; 289 289 dev->multifunction = 0; /* maybe a lie? */ 290 290 set_pcie_port_type(dev);
+7 -1
drivers/of/Kconfig
··· 71 71 72 72 config OF_PCI 73 73 def_tristate PCI 74 - depends on PCI && (PPC || MICROBLAZE || X86) 74 + depends on PCI 75 75 help 76 76 OpenFirmware PCI bus accessors 77 + 78 + config OF_PCI_IRQ 79 + def_tristate PCI 80 + depends on OF_PCI && OF_IRQ 81 + help 82 + OpenFirmware PCI IRQ routing helpers 77 83 78 84 endmenu # OF
+1
drivers/of/Makefile
··· 10 10 obj-$(CONFIG_OF_SPI) += of_spi.o 11 11 obj-$(CONFIG_OF_MDIO) += of_mdio.o 12 12 obj-$(CONFIG_OF_PCI) += of_pci.o 13 + obj-$(CONFIG_OF_PCI_IRQ) += of_pci_irq.o
+33 -85
drivers/of/of_pci.c
··· 1 1 #include <linux/kernel.h> 2 2 #include <linux/of_pci.h> 3 - #include <linux/of_irq.h> 4 3 #include <asm/prom.h> 5 4 6 - /** 7 - * of_irq_map_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_map_pci(struct pci_dev *pdev, struct of_irq *out_irq) 5 + static inline int __of_pci_pci_compare(struct device_node *node, 6 + unsigned int devfn) 18 7 { 19 - struct device_node *dn, *ppnode; 20 - struct pci_dev *ppdev; 21 - u32 lspec; 22 - __be32 lspec_be; 23 - __be32 laddr[3]; 24 - u8 pin; 25 - int rc; 8 + unsigned int size; 9 + const __be32 *reg = of_get_property(node, "reg", &size); 26 10 27 - /* Check if we have a device node, if yes, fallback to standard 28 - * device tree parsing 29 - */ 30 - dn = pci_device_to_OF_node(pdev); 31 - if (dn) { 32 - rc = of_irq_map_one(dn, 0, out_irq); 33 - if (!rc) 34 - return rc; 35 - } 36 - 37 - /* Ok, we don't, time to have fun. Let's start by building up an 38 - * interrupt spec. we assume #interrupt-cells is 1, which is standard 39 - * for PCI. If you do different, then don't use that routine. 40 - */ 41 - rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin); 42 - if (rc != 0) 43 - return rc; 44 - /* No pin, exit */ 45 - if (pin == 0) 46 - return -ENODEV; 47 - 48 - /* Now we walk up the PCI tree */ 49 - lspec = pin; 50 - for (;;) { 51 - /* Get the pci_dev of our parent */ 52 - ppdev = pdev->bus->self; 53 - 54 - /* Ouch, it's a host bridge... */ 55 - if (ppdev == NULL) { 56 - ppnode = pci_bus_to_OF_node(pdev->bus); 57 - 58 - /* No node for host bridge ? give up */ 59 - if (ppnode == NULL) 60 - return -EINVAL; 61 - } else { 62 - /* We found a P2P bridge, check if it has a node */ 63 - ppnode = pci_device_to_OF_node(ppdev); 64 - } 65 - 66 - /* Ok, we have found a parent with a device-node, hand over to 67 - * the OF parsing code. 68 - * We build a unit address from the linux device to be used for 69 - * resolution. Note that we use the linux bus number which may 70 - * not match your firmware bus numbering. 71 - * Fortunately, in most cases, interrupt-map-mask doesn't 72 - * include the bus number as part of the matching. 73 - * You should still be careful about that though if you intend 74 - * to rely on this function (you ship a firmware that doesn't 75 - * create device nodes for all PCI devices). 76 - */ 77 - if (ppnode) 78 - break; 79 - 80 - /* We can only get here if we hit a P2P bridge with no node, 81 - * let's do standard swizzling and try again 82 - */ 83 - lspec = pci_swizzle_interrupt_pin(pdev, lspec); 84 - pdev = ppdev; 85 - } 86 - 87 - lspec_be = cpu_to_be32(lspec); 88 - laddr[0] = cpu_to_be32((pdev->bus->number << 16) | (pdev->devfn << 8)); 89 - laddr[1] = laddr[2] = cpu_to_be32(0); 90 - return of_irq_map_raw(ppnode, &lspec_be, 1, laddr, out_irq); 11 + if (!reg || size < 5 * sizeof(__be32)) 12 + return 0; 13 + return ((be32_to_cpup(&reg[0]) >> 8) & 0xff) == devfn; 91 14 } 92 - EXPORT_SYMBOL_GPL(of_irq_map_pci); 15 + 16 + struct device_node *of_pci_find_child_device(struct device_node *parent, 17 + unsigned int devfn) 18 + { 19 + struct device_node *node, *node2; 20 + 21 + for_each_child_of_node(parent, node) { 22 + if (__of_pci_pci_compare(node, devfn)) 23 + return node; 24 + /* 25 + * Some OFs create a parent node "multifunc-device" as 26 + * a fake root for all functions of a multi-function 27 + * device we go down them as well. 28 + */ 29 + if (!strcmp(node->name, "multifunc-device")) { 30 + for_each_child_of_node(node, node2) { 31 + if (__of_pci_pci_compare(node2, devfn)) { 32 + of_node_put(node); 33 + return node2; 34 + } 35 + } 36 + } 37 + } 38 + return NULL; 39 + } 40 + EXPORT_SYMBOL_GPL(of_pci_find_child_device);
+92
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 <asm/prom.h> 5 + 6 + /** 7 + * of_irq_map_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_map_pci(struct pci_dev *pdev, struct of_irq *out_irq) 18 + { 19 + struct device_node *dn, *ppnode; 20 + struct pci_dev *ppdev; 21 + u32 lspec; 22 + __be32 lspec_be; 23 + __be32 laddr[3]; 24 + u8 pin; 25 + int rc; 26 + 27 + /* Check if we have a device node, if yes, fallback to standard 28 + * device tree parsing 29 + */ 30 + dn = pci_device_to_OF_node(pdev); 31 + if (dn) { 32 + rc = of_irq_map_one(dn, 0, out_irq); 33 + if (!rc) 34 + return rc; 35 + } 36 + 37 + /* Ok, we don't, time to have fun. Let's start by building up an 38 + * interrupt spec. we assume #interrupt-cells is 1, which is standard 39 + * for PCI. If you do different, then don't use that routine. 40 + */ 41 + rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin); 42 + if (rc != 0) 43 + return rc; 44 + /* No pin, exit */ 45 + if (pin == 0) 46 + return -ENODEV; 47 + 48 + /* Now we walk up the PCI tree */ 49 + lspec = pin; 50 + for (;;) { 51 + /* Get the pci_dev of our parent */ 52 + ppdev = pdev->bus->self; 53 + 54 + /* Ouch, it's a host bridge... */ 55 + if (ppdev == NULL) { 56 + ppnode = pci_bus_to_OF_node(pdev->bus); 57 + 58 + /* No node for host bridge ? give up */ 59 + if (ppnode == NULL) 60 + return -EINVAL; 61 + } else { 62 + /* We found a P2P bridge, check if it has a node */ 63 + ppnode = pci_device_to_OF_node(ppdev); 64 + } 65 + 66 + /* Ok, we have found a parent with a device-node, hand over to 67 + * the OF parsing code. 68 + * We build a unit address from the linux device to be used for 69 + * resolution. Note that we use the linux bus number which may 70 + * not match your firmware bus numbering. 71 + * Fortunately, in most cases, interrupt-map-mask doesn't 72 + * include the bus number as part of the matching. 73 + * You should still be careful about that though if you intend 74 + * to rely on this function (you ship a firmware that doesn't 75 + * create device nodes for all PCI devices). 76 + */ 77 + if (ppnode) 78 + break; 79 + 80 + /* We can only get here if we hit a P2P bridge with no node, 81 + * let's do standard swizzling and try again 82 + */ 83 + lspec = pci_swizzle_interrupt_pin(pdev, lspec); 84 + pdev = ppdev; 85 + } 86 + 87 + lspec_be = cpu_to_be32(lspec); 88 + laddr[0] = cpu_to_be32((pdev->bus->number << 16) | (pdev->devfn << 8)); 89 + laddr[1] = laddr[2] = cpu_to_be32(0); 90 + return of_irq_map_raw(ppnode, &lspec_be, 1, laddr, out_irq); 91 + } 92 + EXPORT_SYMBOL_GPL(of_irq_map_pci);
+2
drivers/pci/Makefile
··· 70 70 71 71 obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen-pcifront.o 72 72 73 + obj-$(CONFIG_OF) += of.o 74 + 73 75 ccflags-$(CONFIG_PCI_DEBUG) := -DDEBUG
+1 -1
drivers/pci/hotplug/rpadlpar_core.c
··· 158 158 /* Scan below the new bridge */ 159 159 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || 160 160 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) 161 - of_scan_pci_bridge(dn, dev); 161 + of_scan_pci_bridge(dev); 162 162 163 163 /* Map IO space for child bus, which may or may not succeed */ 164 164 pcibios_map_io_space(dev->subordinate);
+61
drivers/pci/of.c
··· 1 + /* 2 + * PCI <-> OF mapping helpers 3 + * 4 + * Copyright 2011 IBM Corp. 5 + * 6 + * This program is free software; you can redistribute it and/or 7 + * modify it under the terms of the GNU General Public License 8 + * as published by the Free Software Foundation; either version 9 + * 2 of the License, or (at your option) any later version. 10 + */ 11 + 12 + #include <linux/kernel.h> 13 + #include <linux/pci.h> 14 + #include <linux/of.h> 15 + #include <linux/of_pci.h> 16 + #include "pci.h" 17 + 18 + void 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 + 26 + void 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 + 32 + void pci_set_bus_of_node(struct pci_bus *bus) 33 + { 34 + if (bus->self == NULL) 35 + bus->dev.of_node = pcibios_get_phb_of_node(bus); 36 + else 37 + bus->dev.of_node = of_node_get(bus->self->dev.of_node); 38 + } 39 + 40 + void pci_release_bus_of_node(struct pci_bus *bus) 41 + { 42 + of_node_put(bus->dev.of_node); 43 + bus->dev.of_node = NULL; 44 + } 45 + 46 + struct device_node * __weak pcibios_get_phb_of_node(struct pci_bus *bus) 47 + { 48 + /* This should only be called for PHBs */ 49 + if (WARN_ON(bus->self || bus->parent)) 50 + return NULL; 51 + 52 + /* Look for a node pointer in either the intermediary device we 53 + * create above the root bus or it's own parent. Normally only 54 + * the later is populated. 55 + */ 56 + if (bus->bridge->of_node) 57 + return of_node_get(bus->bridge->of_node); 58 + if (bus->bridge->parent->of_node) 59 + return of_node_get(bus->bridge->parent->of_node); 60 + return NULL; 61 + }
+6 -1
drivers/pci/probe.c
··· 52 52 if (pci_bus->bridge) 53 53 put_device(pci_bus->bridge); 54 54 pci_bus_remove_resources(pci_bus); 55 + pci_release_bus_of_node(pci_bus); 55 56 kfree(pci_bus); 56 57 } 57 58 ··· 589 588 590 589 child->self = bridge; 591 590 child->bridge = get_device(&bridge->dev); 592 - 591 + pci_set_bus_of_node(child); 593 592 pci_set_bus_speed(child); 594 593 595 594 /* Set up default resource pointers and names.. */ ··· 1039 1038 1040 1039 pci_dev = to_pci_dev(dev); 1041 1040 pci_release_capabilities(pci_dev); 1041 + pci_release_of_node(pci_dev); 1042 1042 kfree(pci_dev); 1043 1043 } 1044 1044 ··· 1158 1156 dev->devfn = devfn; 1159 1157 dev->vendor = l & 0xffff; 1160 1158 dev->device = (l >> 16) & 0xffff; 1159 + 1160 + pci_set_of_node(dev); 1161 1161 1162 1162 if (pci_setup_device(dev)) { 1163 1163 kfree(dev); ··· 1413 1409 goto dev_reg_err; 1414 1410 b->bridge = get_device(dev); 1415 1411 device_enable_async_suspend(b->bridge); 1412 + pci_set_bus_of_node(b); 1416 1413 1417 1414 if (!parent) 1418 1415 set_dev_node(b->bridge, pcibus_to_node(b));
+5
include/linux/of_pci.h
··· 6 6 struct pci_dev; 7 7 struct of_irq; 8 8 int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq); 9 + 10 + struct device_node; 11 + struct device_node *of_pci_find_child_device(struct device_node *parent, 12 + unsigned int devfn); 13 + 9 14 #endif
+18
include/linux/pci.h
··· 1589 1589 int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off, 1590 1590 unsigned int len, const char *kw); 1591 1591 1592 + /* PCI <-> OF binding helpers */ 1593 + #ifdef CONFIG_OF 1594 + struct device_node; 1595 + extern void pci_set_of_node(struct pci_dev *dev); 1596 + extern void pci_release_of_node(struct pci_dev *dev); 1597 + extern void pci_set_bus_of_node(struct pci_bus *bus); 1598 + extern void pci_release_bus_of_node(struct pci_bus *bus); 1599 + 1600 + /* Arch may override this (weak) */ 1601 + extern struct device_node * __weak pcibios_get_phb_of_node(struct pci_bus *bus); 1602 + 1603 + #else /* CONFIG_OF */ 1604 + static inline void pci_set_of_node(struct pci_dev *dev) { } 1605 + static inline void pci_release_of_node(struct pci_dev *dev) { } 1606 + static inline void pci_set_bus_of_node(struct pci_bus *bus) { } 1607 + static inline void pci_release_bus_of_node(struct pci_bus *bus) { } 1608 + #endif /* CONFIG_OF */ 1609 + 1592 1610 #endif /* __KERNEL__ */ 1593 1611 #endif /* LINUX_PCI_H */