aboutsummaryrefslogtreecommitdiffstats
path: root/arch/ppc64/kernel/pci.c
diff options
context:
space:
mode:
authorPaul Mackerras <paulus@samba.org>2005-09-12 03:17:36 -0400
committerPaul Mackerras <paulus@samba.org>2005-09-12 03:17:36 -0400
commit4267292b0f368c1633ff3316a53b5f7fbada95f8 (patch)
tree4830de68a91351ef29445ec2a7f7ad8d7145b0b9 /arch/ppc64/kernel/pci.c
parent357d596bd552ad157a906289ab13ea6ba7e66e3d (diff)
ppc64: Set up PCI tree from Open Firmware device tree
This adds code which gives us the option on ppc64 of instantiating the PCI tree (the tree of pci_bus and pci_dev structs) from the Open Firmware device tree rather than by probing PCI configuration space. The OF device tree has a node for each PCI device and bridge in the system, with properties that tell us what addresses the firmware has configured for them and other details. There are a couple of reasons why this is needed. First, on systems with a hypervisor, there is a PCI-PCI bridge per slot under the PCI host bridges. These PCI-PCI bridges have special isolation features for virtualization. We can't write to their config space, and we are not supposed to be reading their config space either. The firmware tells us about the address ranges that they pass in the OF device tree. Secondly, on powermacs, the interrupt controller is in a PCI device that may be behind a PCI-PCI bridge. If we happened to take an interrupt just at the point when the device or a bridge on the path to it was disabled for probing, we would crash when we try to access the interrupt controller. I have implemented a platform-specific function which is called for each PCI bridge (host or PCI-PCI) to say whether the code should look in the device tree or use normal PCI probing for the devices under that bridge. On pSeries machines we use the device tree if we're running under a hypervisor, otherwise we use normal probing. On powermacs we use normal probing for the AGP bridge, since the device for the AGP bridge itself isn't shown in the device tree (at least on my G5), and the device tree for everything else. This has been tested on a dual G5 powermac, a partition on a POWER5 machine (running under the hypervisor), and a legacy iSeries partition. Signed-off-by: Paul Mackerras <paulus@samba.org>
Diffstat (limited to 'arch/ppc64/kernel/pci.c')
-rw-r--r--arch/ppc64/kernel/pci.c425
1 files changed, 335 insertions, 90 deletions
diff --git a/arch/ppc64/kernel/pci.c b/arch/ppc64/kernel/pci.c
index 8447dcc2c2b3..861138ad092c 100644
--- a/arch/ppc64/kernel/pci.c
+++ b/arch/ppc64/kernel/pci.c
@@ -51,6 +51,10 @@ unsigned long io_page_mask;
51 51
52EXPORT_SYMBOL(io_page_mask); 52EXPORT_SYMBOL(io_page_mask);
53 53
54#ifdef CONFIG_PPC_MULTIPLATFORM
55static void fixup_resource(struct resource *res, struct pci_dev *dev);
56static void do_bus_setup(struct pci_bus *bus);
57#endif
54 58
55unsigned int pcibios_assign_all_busses(void) 59unsigned int pcibios_assign_all_busses(void)
56{ 60{
@@ -225,10 +229,287 @@ static void __init pcibios_claim_of_setup(void)
225} 229}
226#endif 230#endif
227 231
232#ifdef CONFIG_PPC_MULTIPLATFORM
233static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
234{
235 u32 *prop;
236 int len;
237
238 prop = (u32 *) get_property(np, name, &len);
239 if (prop && len >= 4)
240 return *prop;
241 return def;
242}
243
244static unsigned int pci_parse_of_flags(u32 addr0)
245{
246 unsigned int flags = 0;
247
248 if (addr0 & 0x02000000) {
249 flags |= IORESOURCE_MEM;
250 if (addr0 & 0x40000000)
251 flags |= IORESOURCE_PREFETCH;
252 } else if (addr0 & 0x01000000)
253 flags |= IORESOURCE_IO;
254 return flags;
255}
256
257#define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
258
259static void pci_parse_of_addrs(struct device_node *node, struct pci_dev *dev)
260{
261 u64 base, size;
262 unsigned int flags;
263 struct resource *res;
264 u32 *addrs, i;
265 int proplen;
266
267 addrs = (u32 *) get_property(node, "assigned-addresses", &proplen);
268 if (!addrs)
269 return;
270 for (; proplen >= 20; proplen -= 20, addrs += 5) {
271 flags = pci_parse_of_flags(addrs[0]);
272 if (!flags)
273 continue;
274 base = GET_64BIT(addrs, 1);
275 size = GET_64BIT(addrs, 3);
276 if (!size)
277 continue;
278 i = addrs[0] & 0xff;
279 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
280 res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
281 } else if (i == dev->rom_base_reg) {
282 res = &dev->resource[PCI_ROM_RESOURCE];
283 flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
284 } else {
285 printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
286 continue;
287 }
288 res->start = base;
289 res->end = base + size - 1;
290 res->flags = flags;
291 res->name = pci_name(dev);
292 fixup_resource(res, dev);
293 }
294}
295
296static struct pci_dev *of_create_pci_dev(struct device_node *node,
297 struct pci_bus *bus, int devfn)
298{
299 struct pci_dev *dev;
300 const char *type;
301
302 dev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
303 if (!dev)
304 return NULL;
305 type = get_property(node, "device_type", NULL);
306 if (type == NULL)
307 type = "";
308
309 memset(dev, 0, sizeof(struct pci_dev));
310 dev->bus = bus;
311 dev->sysdata = node;
312 dev->dev.parent = bus->bridge;
313 dev->dev.bus = &pci_bus_type;
314 dev->devfn = devfn;
315 dev->multifunction = 0; /* maybe a lie? */
316
317 dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
318 dev->device = get_int_prop(node, "device-id", 0xffff);
319 dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
320 dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
321
322 dev->cfg_size = 256; /*pci_cfg_space_size(dev);*/
323
324 sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
325 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
326 dev->class = get_int_prop(node, "class-code", 0);
327
328 dev->current_state = 4; /* unknown power state */
329
330 if (!strcmp(type, "pci")) {
331 /* a PCI-PCI bridge */
332 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
333 dev->rom_base_reg = PCI_ROM_ADDRESS1;
334 } else if (!strcmp(type, "cardbus")) {
335 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
336 } else {
337 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
338 dev->rom_base_reg = PCI_ROM_ADDRESS;
339 dev->irq = NO_IRQ;
340 if (node->n_intrs > 0) {
341 dev->irq = node->intrs[0].line;
342 pci_write_config_byte(dev, PCI_INTERRUPT_LINE,
343 dev->irq);
344 }
345 }
346
347 pci_parse_of_addrs(node, dev);
348
349 pci_device_add(dev, bus);
350
351 /* XXX pci_scan_msi_device(dev); */
352
353 return dev;
354}
355
356static void of_scan_pci_bridge(struct device_node *node, struct pci_dev *dev);
357
358static void __devinit of_scan_bus(struct device_node *node,
359 struct pci_bus *bus)
360{
361 struct device_node *child = NULL;
362 u32 *reg;
363 int reglen, devfn;
364 struct pci_dev *dev;
365
366 while ((child = of_get_next_child(node, child)) != NULL) {
367 reg = (u32 *) get_property(child, "reg", &reglen);
368 if (reg == NULL || reglen < 20)
369 continue;
370 devfn = (reg[0] >> 8) & 0xff;
371 /* create a new pci_dev for this device */
372 dev = of_create_pci_dev(child, bus, devfn);
373 if (!dev)
374 continue;
375 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
376 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
377 of_scan_pci_bridge(child, dev);
378 }
379
380 do_bus_setup(bus);
381}
382
383static void __devinit of_scan_pci_bridge(struct device_node *node,
384 struct pci_dev *dev)
385{
386 struct pci_bus *bus;
387 u32 *busrange, *ranges;
388 int len, i, mode;
389 struct resource *res;
390 unsigned int flags;
391 u64 size;
392
393 /* parse bus-range property */
394 busrange = (u32 *) get_property(node, "bus-range", &len);
395 if (busrange == NULL || len != 8) {
396 printk(KERN_ERR "Can't get bus-range for PCI-PCI bridge %s\n",
397 node->full_name);
398 return;
399 }
400 ranges = (u32 *) get_property(node, "ranges", &len);
401 if (ranges == NULL) {
402 printk(KERN_ERR "Can't get ranges for PCI-PCI bridge %s\n",
403 node->full_name);
404 return;
405 }
406
407 bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
408 if (!bus) {
409 printk(KERN_ERR "Failed to create pci bus for %s\n",
410 node->full_name);
411 return;
412 }
413
414 bus->primary = dev->bus->number;
415 bus->subordinate = busrange[1];
416 bus->bridge_ctl = 0;
417 bus->sysdata = node;
418
419 /* parse ranges property */
420 /* PCI #address-cells == 3 and #size-cells == 2 always */
421 res = &dev->resource[PCI_BRIDGE_RESOURCES];
422 for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
423 res->flags = 0;
424 bus->resource[i] = res;
425 ++res;
426 }
427 i = 1;
428 for (; len >= 32; len -= 32, ranges += 8) {
429 flags = pci_parse_of_flags(ranges[0]);
430 size = GET_64BIT(ranges, 6);
431 if (flags == 0 || size == 0)
432 continue;
433 if (flags & IORESOURCE_IO) {
434 res = bus->resource[0];
435 if (res->flags) {
436 printk(KERN_ERR "PCI: ignoring extra I/O range"
437 " for bridge %s\n", node->full_name);
438 continue;
439 }
440 } else {
441 if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
442 printk(KERN_ERR "PCI: too many memory ranges"
443 " for bridge %s\n", node->full_name);
444 continue;
445 }
446 res = bus->resource[i];
447 ++i;
448 }
449 res->start = GET_64BIT(ranges, 1);
450 res->end = res->start + size - 1;
451 res->flags = flags;
452 fixup_resource(res, dev);
453 }
454 sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
455 bus->number);
456
457 mode = PCI_PROBE_NORMAL;
458 if (ppc_md.pci_probe_mode)
459 mode = ppc_md.pci_probe_mode(bus);
460 if (mode == PCI_PROBE_DEVTREE)
461 of_scan_bus(node, bus);
462 else if (mode == PCI_PROBE_NORMAL)
463 pci_scan_child_bus(bus);
464}
465#endif /* CONFIG_PPC_MULTIPLATFORM */
466
467static void __devinit scan_phb(struct pci_controller *hose)
468{
469 struct pci_bus *bus;
470 struct device_node *node = hose->arch_data;
471 int i, mode;
472 struct resource *res;
473
474 bus = pci_create_bus(NULL, hose->first_busno, hose->ops, node);
475 if (bus == NULL) {
476 printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
477 hose->global_number);
478 return;
479 }
480 bus->secondary = hose->first_busno;
481 hose->bus = bus;
482
483 bus->resource[0] = res = &hose->io_resource;
484 if (res->flags && request_resource(&ioport_resource, res))
485 printk(KERN_ERR "Failed to request PCI IO region "
486 "on PCI domain %04x\n", hose->global_number);
487
488 for (i = 0; i < 3; ++i) {
489 res = &hose->mem_resources[i];
490 bus->resource[i+1] = res;
491 if (res->flags && request_resource(&iomem_resource, res))
492 printk(KERN_ERR "Failed to request PCI memory region "
493 "on PCI domain %04x\n", hose->global_number);
494 }
495
496 mode = PCI_PROBE_NORMAL;
497#ifdef CONFIG_PPC_MULTIPLATFORM
498 if (ppc_md.pci_probe_mode)
499 mode = ppc_md.pci_probe_mode(bus);
500 if (mode == PCI_PROBE_DEVTREE) {
501 bus->subordinate = hose->last_busno;
502 of_scan_bus(node, bus);
503 }
504#endif /* CONFIG_PPC_MULTIPLATFORM */
505 if (mode == PCI_PROBE_NORMAL)
506 hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
507 pci_bus_add_devices(bus);
508}
509
228static int __init pcibios_init(void) 510static int __init pcibios_init(void)
229{ 511{
230 struct pci_controller *hose, *tmp; 512 struct pci_controller *hose, *tmp;
231 struct pci_bus *bus;
232 513
233 /* For now, override phys_mem_access_prot. If we need it, 514 /* For now, override phys_mem_access_prot. If we need it,
234 * later, we may move that initialization to each ppc_md 515 * later, we may move that initialization to each ppc_md
@@ -242,13 +523,8 @@ static int __init pcibios_init(void)
242 printk("PCI: Probing PCI hardware\n"); 523 printk("PCI: Probing PCI hardware\n");
243 524
244 /* Scan all of the recorded PCI controllers. */ 525 /* Scan all of the recorded PCI controllers. */
245 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 526 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
246 hose->last_busno = 0xff; 527 scan_phb(hose);
247 bus = pci_scan_bus(hose->first_busno, hose->ops,
248 hose->arch_data);
249 hose->bus = bus;
250 hose->last_busno = bus->subordinate;
251 }
252 528
253#ifndef CONFIG_PPC_ISERIES 529#ifndef CONFIG_PPC_ISERIES
254 if (pci_probe_only) 530 if (pci_probe_only)
@@ -820,120 +1096,89 @@ void phbs_remap_io(void)
820/* 1096/*
821 * ppc64 can have multifunction devices that do not respond to function 0. 1097 * ppc64 can have multifunction devices that do not respond to function 0.
822 * In this case we must scan all functions. 1098 * In this case we must scan all functions.
1099 * XXX this can go now, we use the OF device tree in all the
1100 * cases that caused problems. -- paulus
823 */ 1101 */
824int pcibios_scan_all_fns(struct pci_bus *bus, int devfn) 1102int pcibios_scan_all_fns(struct pci_bus *bus, int devfn)
825{ 1103{
826 struct device_node *busdn, *dn;
827
828 if (bus->self)
829 busdn = pci_device_to_OF_node(bus->self);
830 else
831 busdn = bus->sysdata; /* must be a phb */
832
833 if (busdn == NULL)
834 return 0;
835
836 /*
837 * Check to see if there is any of the 8 functions are in the
838 * device tree. If they are then we need to scan all the
839 * functions of this slot.
840 */
841 for (dn = busdn->child; dn; dn = dn->sibling) {
842 struct pci_dn *pdn = dn->data;
843 if (pdn && (pdn->devfn >> 3) == (devfn >> 3))
844 return 1;
845 }
846
847 return 0; 1104 return 0;
848} 1105}
849 1106
1107static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
1108{
1109 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1110 unsigned long start, end, mask, offset;
1111
1112 if (res->flags & IORESOURCE_IO) {
1113 offset = (unsigned long)hose->io_base_virt - pci_io_base;
1114
1115 start = res->start += offset;
1116 end = res->end += offset;
1117
1118 /* Need to allow IO access to pages that are in the
1119 ISA range */
1120 if (start < MAX_ISA_PORT) {
1121 if (end > MAX_ISA_PORT)
1122 end = MAX_ISA_PORT;
1123
1124 start >>= PAGE_SHIFT;
1125 end >>= PAGE_SHIFT;
1126
1127 /* get the range of pages for the map */
1128 mask = ((1 << (end+1)) - 1) ^ ((1 << start) - 1);
1129 io_page_mask |= mask;
1130 }
1131 } else if (res->flags & IORESOURCE_MEM) {
1132 res->start += hose->pci_mem_offset;
1133 res->end += hose->pci_mem_offset;
1134 }
1135}
850 1136
851void __devinit pcibios_fixup_device_resources(struct pci_dev *dev, 1137void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
852 struct pci_bus *bus) 1138 struct pci_bus *bus)
853{ 1139{
854 /* Update device resources. */ 1140 /* Update device resources. */
855 struct pci_controller *hose = pci_bus_to_host(bus);
856 int i; 1141 int i;
857 1142
858 for (i = 0; i < PCI_NUM_RESOURCES; i++) { 1143 for (i = 0; i < PCI_NUM_RESOURCES; i++)
859 if (dev->resource[i].flags & IORESOURCE_IO) { 1144 if (dev->resource[i].flags)
860 unsigned long offset = (unsigned long)hose->io_base_virt 1145 fixup_resource(&dev->resource[i], dev);
861 - pci_io_base;
862 unsigned long start, end, mask;
863
864 start = dev->resource[i].start += offset;
865 end = dev->resource[i].end += offset;
866
867 /* Need to allow IO access to pages that are in the
868 ISA range */
869 if (start < MAX_ISA_PORT) {
870 if (end > MAX_ISA_PORT)
871 end = MAX_ISA_PORT;
872
873 start >>= PAGE_SHIFT;
874 end >>= PAGE_SHIFT;
875
876 /* get the range of pages for the map */
877 mask = ((1 << (end+1))-1) ^ ((1 << start)-1);
878 io_page_mask |= mask;
879 }
880 }
881 else if (dev->resource[i].flags & IORESOURCE_MEM) {
882 dev->resource[i].start += hose->pci_mem_offset;
883 dev->resource[i].end += hose->pci_mem_offset;
884 }
885 }
886} 1146}
887EXPORT_SYMBOL(pcibios_fixup_device_resources); 1147EXPORT_SYMBOL(pcibios_fixup_device_resources);
888 1148
889void __devinit pcibios_fixup_bus(struct pci_bus *bus) 1149static void __devinit do_bus_setup(struct pci_bus *bus)
890{ 1150{
891 struct pci_controller *hose = pci_bus_to_host(bus); 1151 struct pci_dev *dev;
892 struct pci_dev *dev = bus->self;
893 struct resource *res;
894 int i;
895 1152
896 if (!dev) { 1153 ppc_md.iommu_bus_setup(bus);
897 /* Root bus. */
898 1154
899 hose->bus = bus; 1155 list_for_each_entry(dev, &bus->devices, bus_list)
900 bus->resource[0] = res = &hose->io_resource; 1156 ppc_md.iommu_dev_setup(dev);
901 1157
902 if (res->flags && request_resource(&ioport_resource, res)) 1158 if (ppc_md.irq_bus_setup)
903 printk(KERN_ERR "Failed to request IO on " 1159 ppc_md.irq_bus_setup(bus);
904 "PCI domain %d\n", pci_domain_nr(bus)); 1160}
905 1161
906 for (i = 0; i < 3; ++i) { 1162void __devinit pcibios_fixup_bus(struct pci_bus *bus)
907 res = &hose->mem_resources[i]; 1163{
908 bus->resource[i+1] = res; 1164 struct pci_dev *dev = bus->self;
909 if (res->flags && request_resource(&iomem_resource, res)) 1165
910 printk(KERN_ERR "Failed to request MEM on " 1166 if (dev && pci_probe_only &&
911 "PCI domain %d\n", 1167 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
912 pci_domain_nr(bus));
913 }
914 } else if (pci_probe_only &&
915 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
916 /* This is a subordinate bridge */ 1168 /* This is a subordinate bridge */
917 1169
918 pci_read_bridge_bases(bus); 1170 pci_read_bridge_bases(bus);
919 pcibios_fixup_device_resources(dev, bus); 1171 pcibios_fixup_device_resources(dev, bus);
920 } 1172 }
921 1173
922 ppc_md.iommu_bus_setup(bus); 1174 do_bus_setup(bus);
923
924 list_for_each_entry(dev, &bus->devices, bus_list)
925 ppc_md.iommu_dev_setup(dev);
926
927 if (ppc_md.irq_bus_setup)
928 ppc_md.irq_bus_setup(bus);
929 1175
930 if (!pci_probe_only) 1176 if (!pci_probe_only)
931 return; 1177 return;
932 1178
933 list_for_each_entry(dev, &bus->devices, bus_list) { 1179 list_for_each_entry(dev, &bus->devices, bus_list)
934 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) 1180 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
935 pcibios_fixup_device_resources(dev, bus); 1181 pcibios_fixup_device_resources(dev, bus);
936 }
937} 1182}
938EXPORT_SYMBOL(pcibios_fixup_bus); 1183EXPORT_SYMBOL(pcibios_fixup_bus);
939 1184