2 * Common pmac/prep/chrp pci routines. -- Cort
5 #include <linux/kernel.h>
7 #include <linux/delay.h>
8 #include <linux/string.h>
9 #include <linux/init.h>
10 #include <linux/capability.h>
11 #include <linux/sched.h>
12 #include <linux/errno.h>
13 #include <linux/bootmem.h>
14 #include <linux/irq.h>
15 #include <linux/list.h>
17 #include <linux/slab.h>
19 #include <asm/processor.h>
22 #include <asm/sections.h>
23 #include <asm/pci-bridge.h>
24 #include <asm/byteorder.h>
25 #include <asm/uaccess.h>
29 unsigned long isa_io_base
;
30 unsigned long pci_dram_offset
;
31 int pcibios_assign_bus_offset
= 1;
33 static u8
*pci_to_OF_bus_map
;
35 /* By default, we don't re-assign bus numbers. We do this only on
38 static int pci_assign_all_buses
;
40 static int pci_bus_count
;
43 * Functions below are used on OpenFirmware machines.
46 make_one_node_map(struct device_node
*node
, u8 pci_bus
)
51 if (pci_bus
>= pci_bus_count
)
53 bus_range
= of_get_property(node
, "bus-range", &len
);
54 if (bus_range
== NULL
|| len
< 2 * sizeof(int)) {
55 printk(KERN_WARNING
"Can't get bus-range for %s, "
56 "assuming it starts at 0\n", node
->full_name
);
57 pci_to_OF_bus_map
[pci_bus
] = 0;
59 pci_to_OF_bus_map
[pci_bus
] = bus_range
[0];
61 for_each_child_of_node(node
, node
) {
63 const unsigned int *class_code
, *reg
;
65 class_code
= of_get_property(node
, "class-code", NULL
);
67 ((*class_code
>> 8) != PCI_CLASS_BRIDGE_PCI
&&
68 (*class_code
>> 8) != PCI_CLASS_BRIDGE_CARDBUS
))
70 reg
= of_get_property(node
, "reg", NULL
);
73 dev
= pci_get_bus_and_slot(pci_bus
, ((reg
[0] >> 8) & 0xff));
74 if (!dev
|| !dev
->subordinate
) {
78 make_one_node_map(node
, dev
->subordinate
->number
);
84 pcibios_make_OF_bus_map(void)
87 struct pci_controller
*hose
, *tmp
;
88 struct property
*map_prop
;
89 struct device_node
*dn
;
91 pci_to_OF_bus_map
= kmalloc(pci_bus_count
, GFP_KERNEL
);
92 if (!pci_to_OF_bus_map
) {
93 printk(KERN_ERR
"Can't allocate OF bus map !\n");
97 /* We fill the bus map with invalid values, that helps
100 for (i
= 0; i
< pci_bus_count
; i
++)
101 pci_to_OF_bus_map
[i
] = 0xff;
103 /* For each hose, we begin searching bridges */
104 list_for_each_entry_safe(hose
, tmp
, &hose_list
, list_node
) {
105 struct device_node
*node
= hose
->dn
;
109 make_one_node_map(node
, hose
->first_busno
);
111 dn
= of_find_node_by_path("/");
112 map_prop
= of_find_property(dn
, "pci-OF-bus-map", NULL
);
114 BUG_ON(pci_bus_count
> map_prop
->length
);
115 memcpy(map_prop
->value
, pci_to_OF_bus_map
, pci_bus_count
);
119 printk(KERN_INFO
"PCI->OF bus map:\n");
120 for (i
= 0; i
< pci_bus_count
; i
++) {
121 if (pci_to_OF_bus_map
[i
] == 0xff)
123 printk(KERN_INFO
"%d -> %d\n", i
, pci_to_OF_bus_map
[i
]);
128 typedef int (*pci_OF_scan_iterator
)(struct device_node
*node
, void *data
);
130 static struct device_node
*scan_OF_pci_childs(struct device_node
*parent
,
131 pci_OF_scan_iterator filter
, void *data
)
133 struct device_node
*node
;
134 struct device_node
*sub_node
;
136 for_each_child_of_node(parent
, node
) {
137 const unsigned int *class_code
;
139 if (filter(node
, data
)) {
144 /* For PCI<->PCI bridges or CardBus bridges, we go down
145 * Note: some OFs create a parent node "multifunc-device" as
146 * a fake root for all functions of a multi-function device,
147 * we go down them as well.
149 class_code
= of_get_property(node
, "class-code", NULL
);
151 ((*class_code
>> 8) != PCI_CLASS_BRIDGE_PCI
&&
152 (*class_code
>> 8) != PCI_CLASS_BRIDGE_CARDBUS
)) &&
153 strcmp(node
->name
, "multifunc-device"))
155 sub_node
= scan_OF_pci_childs(node
, filter
, data
);
164 static struct device_node
*scan_OF_for_pci_dev(struct device_node
*parent
,
167 struct device_node
*np
, *cnp
;
171 for_each_child_of_node(parent
, np
) {
172 reg
= of_get_property(np
, "reg", &psize
);
173 if (reg
&& psize
>= 4 && ((reg
[0] >> 8) & 0xff) == devfn
)
176 /* Note: some OFs create a parent node "multifunc-device" as
177 * a fake root for all functions of a multi-function device,
178 * we go down them as well. */
179 if (!strcmp(np
->name
, "multifunc-device")) {
180 cnp
= scan_OF_for_pci_dev(np
, devfn
);
189 static struct device_node
*scan_OF_for_pci_bus(struct pci_bus
*bus
)
191 struct device_node
*parent
, *np
;
193 /* Are we a root bus ? */
194 if (bus
->self
== NULL
|| bus
->parent
== NULL
) {
195 struct pci_controller
*hose
= pci_bus_to_host(bus
);
198 return of_node_get(hose
->dn
);
201 /* not a root bus, we need to get our parent */
202 parent
= scan_OF_for_pci_bus(bus
->parent
);
206 /* now iterate for children for a match */
207 np
= scan_OF_for_pci_dev(parent
, bus
->self
->devfn
);
214 * Scans the OF tree for a device node matching a PCI device
217 pci_busdev_to_OF_node(struct pci_bus
*bus
, int devfn
)
219 struct device_node
*parent
, *np
;
221 pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus
->number
, devfn
);
222 parent
= scan_OF_for_pci_bus(bus
);
225 pr_debug(" parent is %s\n", parent
? parent
->full_name
: "<NULL>");
226 np
= scan_OF_for_pci_dev(parent
, devfn
);
228 pr_debug(" result is %s\n", np
? np
->full_name
: "<NULL>");
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.
236 EXPORT_SYMBOL(pci_busdev_to_OF_node
);
239 pci_device_to_OF_node(struct pci_dev
*dev
)
241 return pci_busdev_to_OF_node(dev
->bus
, dev
->devfn
);
243 EXPORT_SYMBOL(pci_device_to_OF_node
);
246 find_OF_pci_device_filter(struct device_node
*node
, void *data
)
248 return ((void *)node
== data
);
252 * Returns the PCI device matching a given OF node
255 pci_device_from_OF_node(struct device_node
*node
, u8
*bus
, u8
*devfn
)
257 const unsigned int *reg
;
258 struct pci_controller
*hose
;
259 struct pci_dev
*dev
= NULL
;
261 /* Make sure it's really a PCI device */
262 hose
= pci_find_hose_for_OF_device(node
);
263 if (!hose
|| !hose
->dn
)
265 if (!scan_OF_pci_childs(hose
->dn
,
266 find_OF_pci_device_filter
, (void *)node
))
268 reg
= of_get_property(node
, "reg", NULL
);
271 *bus
= (reg
[0] >> 16) & 0xff;
272 *devfn
= ((reg
[0] >> 8) & 0xff);
274 /* Ok, here we need some tweak. If we have already renumbered
275 * all busses, we can't rely on the OF bus number any more.
276 * the pci_to_OF_bus_map is not enough as several PCI busses
277 * may match the same OF bus number.
279 if (!pci_to_OF_bus_map
)
282 for_each_pci_dev(dev
)
283 if (pci_to_OF_bus_map
[dev
->bus
->number
] == *bus
&&
284 dev
->devfn
== *devfn
) {
285 *bus
= dev
->bus
->number
;
292 EXPORT_SYMBOL(pci_device_from_OF_node
);
294 /* We create the "pci-OF-bus-map" property now so it appears in the
298 pci_create_OF_bus_map(void)
300 struct property
*of_prop
;
301 struct device_node
*dn
;
303 of_prop
= (struct property
*) alloc_bootmem(sizeof(struct property
) + \
307 dn
= of_find_node_by_path("/");
309 memset(of_prop
, -1, sizeof(struct property
) + 256);
310 of_prop
->name
= "pci-OF-bus-map";
311 of_prop
->length
= 256;
312 of_prop
->value
= &of_prop
[1];
313 prom_add_property(dn
, of_prop
);
318 static void __devinit
pcibios_scan_phb(struct pci_controller
*hose
)
321 struct device_node
*node
= hose
->dn
;
322 unsigned long io_offset
;
323 struct resource
*res
= &hose
->io_resource
;
325 pr_debug("PCI: Scanning PHB %s\n",
326 node
? node
->full_name
: "<NO NAME>");
328 /* Create an empty bus for the toplevel */
329 bus
= pci_create_bus(hose
->parent
, hose
->first_busno
, hose
->ops
, hose
);
331 printk(KERN_ERR
"Failed to create bus for PCI domain %04x\n",
332 hose
->global_number
);
335 bus
->secondary
= hose
->first_busno
;
338 /* Fixup IO space offset */
339 io_offset
= (unsigned long)hose
->io_base_virt
- isa_io_base
;
340 res
->start
= (res
->start
+ io_offset
) & 0xffffffffu
;
341 res
->end
= (res
->end
+ io_offset
) & 0xffffffffu
;
343 /* Wire up PHB bus resources */
344 pcibios_setup_phb_resources(hose
);
347 hose
->last_busno
= bus
->subordinate
= pci_scan_child_bus(bus
);
350 static int __init
pcibios_init(void)
352 struct pci_controller
*hose
, *tmp
;
355 printk(KERN_INFO
"PCI: Probing PCI hardware\n");
357 if (pci_flags
& PCI_REASSIGN_ALL_BUS
) {
358 printk(KERN_INFO
"setting pci_asign_all_busses\n");
359 pci_assign_all_buses
= 1;
362 /* Scan all of the recorded PCI controllers. */
363 list_for_each_entry_safe(hose
, tmp
, &hose_list
, list_node
) {
364 if (pci_assign_all_buses
)
365 hose
->first_busno
= next_busno
;
366 hose
->last_busno
= 0xff;
367 pcibios_scan_phb(hose
);
368 printk(KERN_INFO
"calling pci_bus_add_devices()\n");
369 pci_bus_add_devices(hose
->bus
);
370 if (pci_assign_all_buses
|| next_busno
<= hose
->last_busno
)
371 next_busno
= hose
->last_busno
+ \
372 pcibios_assign_bus_offset
;
374 pci_bus_count
= next_busno
;
376 /* OpenFirmware based machines need a map of OF bus
377 * numbers vs. kernel bus numbers since we may have to
380 if (pci_assign_all_buses
)
381 pcibios_make_OF_bus_map();
383 /* Call common code to handle resource allocation */
384 pcibios_resource_survey();
389 subsys_initcall(pcibios_init
);
391 static struct pci_controller
*
392 pci_bus_to_hose(int bus
)
394 struct pci_controller
*hose
, *tmp
;
396 list_for_each_entry_safe(hose
, tmp
, &hose_list
, list_node
)
397 if (bus
>= hose
->first_busno
&& bus
<= hose
->last_busno
)
402 /* Provide information on locations of various I/O regions in physical
403 * memory. Do this on a per-card basis so that we choose the right
405 * Note that the returned IO or memory base is a physical address
408 long sys_pciconfig_iobase(long which
, unsigned long bus
, unsigned long devfn
)
410 struct pci_controller
*hose
;
411 long result
= -EOPNOTSUPP
;
413 hose
= pci_bus_to_hose(bus
);
418 case IOBASE_BRIDGE_NUMBER
:
419 return (long)hose
->first_busno
;
421 return (long)hose
->pci_mem_offset
;
423 return (long)hose
->io_base_phys
;
425 return (long)isa_io_base
;
427 return (long)isa_mem_base
;