2 * Port for PPC64 David Engebretsen, IBM Corp.
3 * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
5 * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
6 * Rework, based on alpha PCI code.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/pci.h>
18 #include <linux/string.h>
19 #include <linux/init.h>
20 #include <linux/bootmem.h>
22 #include <linux/list.h>
23 #include <linux/syscalls.h>
24 #include <linux/irq.h>
25 #include <linux/vmalloc.h>
27 #include <asm/processor.h>
30 #include <asm/pci-bridge.h>
31 #include <asm/byteorder.h>
32 #include <asm/machdep.h>
33 #include <asm/ppc-pci.h>
35 unsigned long pci_probe_only
= 1;
37 /* pci_io_base -- the base address from which io bars are offsets.
38 * This is the lowest I/O base address (so bar values are always positive),
39 * and it *must* be the start of ISA space if an ISA bus exists because
40 * ISA drivers use hard coded offsets. If no ISA bus exists nothing
41 * is mapped on the first 64K of IO space
43 unsigned long pci_io_base
= ISA_IO_BASE
;
44 EXPORT_SYMBOL(pci_io_base
);
48 static void fixup_broken_pcnet32(struct pci_dev
* dev
)
50 if ((dev
->class>>8 == PCI_CLASS_NETWORK_ETHERNET
)) {
51 dev
->vendor
= PCI_VENDOR_ID_AMD
;
52 pci_write_config_word(dev
, PCI_VENDOR_ID
, PCI_VENDOR_ID_AMD
);
55 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT
, PCI_ANY_ID
, fixup_broken_pcnet32
);
58 static u32
get_int_prop(struct device_node
*np
, const char *name
, u32 def
)
63 prop
= of_get_property(np
, name
, &len
);
69 static unsigned int pci_parse_of_flags(u32 addr0
)
71 unsigned int flags
= 0;
73 if (addr0
& 0x02000000) {
74 flags
= IORESOURCE_MEM
| PCI_BASE_ADDRESS_SPACE_MEMORY
;
75 flags
|= (addr0
>> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64
;
76 flags
|= (addr0
>> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M
;
77 if (addr0
& 0x40000000)
78 flags
|= IORESOURCE_PREFETCH
79 | PCI_BASE_ADDRESS_MEM_PREFETCH
;
80 } else if (addr0
& 0x01000000)
81 flags
= IORESOURCE_IO
| PCI_BASE_ADDRESS_SPACE_IO
;
86 static void pci_parse_of_addrs(struct device_node
*node
, struct pci_dev
*dev
)
95 addrs
= of_get_property(node
, "assigned-addresses", &proplen
);
98 pr_debug(" parse addresses (%d bytes) @ %p\n", proplen
, addrs
);
99 for (; proplen
>= 20; proplen
-= 20, addrs
+= 5) {
100 flags
= pci_parse_of_flags(addrs
[0]);
103 base
= of_read_number(&addrs
[1], 2);
104 size
= of_read_number(&addrs
[3], 2);
108 pr_debug(" base: %llx, size: %llx, i: %x\n",
109 (unsigned long long)base
,
110 (unsigned long long)size
, i
);
112 if (PCI_BASE_ADDRESS_0
<= i
&& i
<= PCI_BASE_ADDRESS_5
) {
113 res
= &dev
->resource
[(i
- PCI_BASE_ADDRESS_0
) >> 2];
114 } else if (i
== dev
->rom_base_reg
) {
115 res
= &dev
->resource
[PCI_ROM_RESOURCE
];
116 flags
|= IORESOURCE_READONLY
| IORESOURCE_CACHEABLE
;
118 printk(KERN_ERR
"PCI: bad cfg reg num 0x%x\n", i
);
122 res
->end
= base
+ size
- 1;
124 res
->name
= pci_name(dev
);
128 struct pci_dev
*of_create_pci_dev(struct device_node
*node
,
129 struct pci_bus
*bus
, int devfn
)
134 dev
= alloc_pci_dev();
137 type
= of_get_property(node
, "device_type", NULL
);
141 pr_debug(" create device, devfn: %x, type: %s\n", devfn
, type
);
145 dev
->dev
.parent
= bus
->bridge
;
146 dev
->dev
.bus
= &pci_bus_type
;
148 dev
->multifunction
= 0; /* maybe a lie? */
150 dev
->vendor
= get_int_prop(node
, "vendor-id", 0xffff);
151 dev
->device
= get_int_prop(node
, "device-id", 0xffff);
152 dev
->subsystem_vendor
= get_int_prop(node
, "subsystem-vendor-id", 0);
153 dev
->subsystem_device
= get_int_prop(node
, "subsystem-id", 0);
155 dev
->cfg_size
= pci_cfg_space_size(dev
);
157 dev_set_name(&dev
->dev
, "%04x:%02x:%02x.%d", pci_domain_nr(bus
),
158 dev
->bus
->number
, PCI_SLOT(devfn
), PCI_FUNC(devfn
));
159 dev
->class = get_int_prop(node
, "class-code", 0);
160 dev
->revision
= get_int_prop(node
, "revision-id", 0);
162 pr_debug(" class: 0x%x\n", dev
->class);
163 pr_debug(" revision: 0x%x\n", dev
->revision
);
165 dev
->current_state
= 4; /* unknown power state */
166 dev
->error_state
= pci_channel_io_normal
;
167 dev
->dma_mask
= 0xffffffff;
169 if (!strcmp(type
, "pci") || !strcmp(type
, "pciex")) {
170 /* a PCI-PCI bridge */
171 dev
->hdr_type
= PCI_HEADER_TYPE_BRIDGE
;
172 dev
->rom_base_reg
= PCI_ROM_ADDRESS1
;
173 } else if (!strcmp(type
, "cardbus")) {
174 dev
->hdr_type
= PCI_HEADER_TYPE_CARDBUS
;
176 dev
->hdr_type
= PCI_HEADER_TYPE_NORMAL
;
177 dev
->rom_base_reg
= PCI_ROM_ADDRESS
;
178 /* Maybe do a default OF mapping here */
182 pci_parse_of_addrs(node
, dev
);
184 pr_debug(" adding to system ...\n");
186 pci_device_add(dev
, bus
);
190 EXPORT_SYMBOL(of_create_pci_dev
);
192 static void __devinit
__of_scan_bus(struct device_node
*node
,
193 struct pci_bus
*bus
, int rescan_existing
)
195 struct device_node
*child
;
200 pr_debug("of_scan_bus(%s) bus no %d... \n",
201 node
->full_name
, bus
->number
);
203 /* Scan direct children */
204 for_each_child_of_node(node
, child
) {
205 pr_debug(" * %s\n", child
->full_name
);
206 reg
= of_get_property(child
, "reg", ®len
);
207 if (reg
== NULL
|| reglen
< 20)
209 devfn
= (reg
[0] >> 8) & 0xff;
211 /* create a new pci_dev for this device */
212 dev
= of_create_pci_dev(child
, bus
, devfn
);
215 pr_debug(" dev header type: %x\n", dev
->hdr_type
);
218 /* Apply all fixups necessary. We don't fixup the bus "self"
219 * for an existing bridge that is being rescanned
221 if (!rescan_existing
)
222 pcibios_setup_bus_self(bus
);
223 pcibios_setup_bus_devices(bus
);
225 /* Now scan child busses */
226 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
227 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
228 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
229 struct device_node
*child
= pci_device_to_OF_node(dev
);
231 of_scan_pci_bridge(child
, dev
);
236 void __devinit
of_scan_bus(struct device_node
*node
,
239 __of_scan_bus(node
, bus
, 0);
241 EXPORT_SYMBOL_GPL(of_scan_bus
);
243 void __devinit
of_rescan_bus(struct device_node
*node
,
246 __of_scan_bus(node
, bus
, 1);
248 EXPORT_SYMBOL_GPL(of_rescan_bus
);
250 void __devinit
of_scan_pci_bridge(struct device_node
*node
,
254 const u32
*busrange
, *ranges
;
256 struct resource
*res
;
260 pr_debug("of_scan_pci_bridge(%s)\n", node
->full_name
);
262 /* parse bus-range property */
263 busrange
= of_get_property(node
, "bus-range", &len
);
264 if (busrange
== NULL
|| len
!= 8) {
265 printk(KERN_DEBUG
"Can't get bus-range for PCI-PCI bridge %s\n",
269 ranges
= of_get_property(node
, "ranges", &len
);
270 if (ranges
== NULL
) {
271 printk(KERN_DEBUG
"Can't get ranges for PCI-PCI bridge %s\n",
276 bus
= pci_add_new_bus(dev
->bus
, dev
, busrange
[0]);
278 printk(KERN_ERR
"Failed to create pci bus for %s\n",
283 bus
->primary
= dev
->bus
->number
;
284 bus
->subordinate
= busrange
[1];
288 /* parse ranges property */
289 /* PCI #address-cells == 3 and #size-cells == 2 always */
290 res
= &dev
->resource
[PCI_BRIDGE_RESOURCES
];
291 for (i
= 0; i
< PCI_NUM_RESOURCES
- PCI_BRIDGE_RESOURCES
; ++i
) {
293 bus
->resource
[i
] = res
;
297 for (; len
>= 32; len
-= 32, ranges
+= 8) {
298 flags
= pci_parse_of_flags(ranges
[0]);
299 size
= of_read_number(&ranges
[6], 2);
300 if (flags
== 0 || size
== 0)
302 if (flags
& IORESOURCE_IO
) {
303 res
= bus
->resource
[0];
305 printk(KERN_ERR
"PCI: ignoring extra I/O range"
306 " for bridge %s\n", node
->full_name
);
310 if (i
>= PCI_NUM_RESOURCES
- PCI_BRIDGE_RESOURCES
) {
311 printk(KERN_ERR
"PCI: too many memory ranges"
312 " for bridge %s\n", node
->full_name
);
315 res
= bus
->resource
[i
];
318 res
->start
= of_read_number(&ranges
[1], 2);
319 res
->end
= res
->start
+ size
- 1;
322 sprintf(bus
->name
, "PCI Bus %04x:%02x", pci_domain_nr(bus
),
324 pr_debug(" bus name: %s\n", bus
->name
);
326 mode
= PCI_PROBE_NORMAL
;
327 if (ppc_md
.pci_probe_mode
)
328 mode
= ppc_md
.pci_probe_mode(bus
);
329 pr_debug(" probe mode: %d\n", mode
);
331 if (mode
== PCI_PROBE_DEVTREE
)
332 of_scan_bus(node
, bus
);
333 else if (mode
== PCI_PROBE_NORMAL
)
334 pci_scan_child_bus(bus
);
336 EXPORT_SYMBOL(of_scan_pci_bridge
);
338 void __devinit
scan_phb(struct pci_controller
*hose
)
341 struct device_node
*node
= hose
->dn
;
344 pr_debug("PCI: Scanning PHB %s\n",
345 node
? node
->full_name
: "<NO NAME>");
347 /* Create an empty bus for the toplevel */
348 bus
= pci_create_bus(hose
->parent
, hose
->first_busno
, hose
->ops
, node
);
350 printk(KERN_ERR
"Failed to create bus for PCI domain %04x\n",
351 hose
->global_number
);
354 bus
->secondary
= hose
->first_busno
;
357 /* Get some IO space for the new PHB */
358 pcibios_map_io_space(bus
);
360 /* Wire up PHB bus resources */
361 pcibios_setup_phb_resources(hose
);
363 /* Get probe mode and perform scan */
364 mode
= PCI_PROBE_NORMAL
;
365 if (node
&& ppc_md
.pci_probe_mode
)
366 mode
= ppc_md
.pci_probe_mode(bus
);
367 pr_debug(" probe mode: %d\n", mode
);
368 if (mode
== PCI_PROBE_DEVTREE
) {
369 bus
->subordinate
= hose
->last_busno
;
370 of_scan_bus(node
, bus
);
373 if (mode
== PCI_PROBE_NORMAL
)
374 hose
->last_busno
= bus
->subordinate
= pci_scan_child_bus(bus
);
377 static int __init
pcibios_init(void)
379 struct pci_controller
*hose
, *tmp
;
381 printk(KERN_INFO
"PCI: Probing PCI hardware\n");
383 /* For now, override phys_mem_access_prot. If we need it,g
384 * later, we may move that initialization to each ppc_md
386 ppc_md
.phys_mem_access_prot
= pci_phys_mem_access_prot
;
389 ppc_pci_flags
|= PPC_PCI_PROBE_ONLY
;
391 /* On ppc64, we always enable PCI domains and we keep domain 0
392 * backward compatible in /proc for video cards
394 ppc_pci_flags
|= PPC_PCI_ENABLE_PROC_DOMAINS
| PPC_PCI_COMPAT_DOMAIN_0
;
396 /* Scan all of the recorded PCI controllers. */
397 list_for_each_entry_safe(hose
, tmp
, &hose_list
, list_node
) {
399 pci_bus_add_devices(hose
->bus
);
402 /* Call common code to handle resource allocation */
403 pcibios_resource_survey();
405 printk(KERN_DEBUG
"PCI: Probing PCI hardware done\n");
410 subsys_initcall(pcibios_init
);
412 #ifdef CONFIG_HOTPLUG
414 int pcibios_unmap_io_space(struct pci_bus
*bus
)
416 struct pci_controller
*hose
;
418 WARN_ON(bus
== NULL
);
420 /* If this is not a PHB, we only flush the hash table over
421 * the area mapped by this bridge. We don't play with the PTE
422 * mappings since we might have to deal with sub-page alignemnts
423 * so flushing the hash table is the only sane way to make sure
424 * that no hash entries are covering that removed bridge area
425 * while still allowing other busses overlapping those pages
428 struct resource
*res
= bus
->resource
[0];
430 pr_debug("IO unmapping for PCI-PCI bridge %s\n",
431 pci_name(bus
->self
));
433 __flush_hash_table_range(&init_mm
, res
->start
+ _IO_BASE
,
434 res
->end
+ _IO_BASE
+ 1);
438 /* Get the host bridge */
439 hose
= pci_bus_to_host(bus
);
441 /* Check if we have IOs allocated */
442 if (hose
->io_base_alloc
== 0)
445 pr_debug("IO unmapping for PHB %s\n", hose
->dn
->full_name
);
446 pr_debug(" alloc=0x%p\n", hose
->io_base_alloc
);
448 /* This is a PHB, we fully unmap the IO area */
449 vunmap(hose
->io_base_alloc
);
453 EXPORT_SYMBOL_GPL(pcibios_unmap_io_space
);
455 #endif /* CONFIG_HOTPLUG */
457 int __devinit
pcibios_map_io_space(struct pci_bus
*bus
)
459 struct vm_struct
*area
;
460 unsigned long phys_page
;
461 unsigned long size_page
;
462 unsigned long io_virt_offset
;
463 struct pci_controller
*hose
;
465 WARN_ON(bus
== NULL
);
467 /* If this not a PHB, nothing to do, page tables still exist and
468 * thus HPTEs will be faulted in when needed
471 pr_debug("IO mapping for PCI-PCI bridge %s\n",
472 pci_name(bus
->self
));
473 pr_debug(" virt=0x%016llx...0x%016llx\n",
474 bus
->resource
[0]->start
+ _IO_BASE
,
475 bus
->resource
[0]->end
+ _IO_BASE
);
479 /* Get the host bridge */
480 hose
= pci_bus_to_host(bus
);
481 phys_page
= _ALIGN_DOWN(hose
->io_base_phys
, PAGE_SIZE
);
482 size_page
= _ALIGN_UP(hose
->pci_io_size
, PAGE_SIZE
);
484 /* Make sure IO area address is clear */
485 hose
->io_base_alloc
= NULL
;
487 /* If there's no IO to map on that bus, get away too */
488 if (hose
->pci_io_size
== 0 || hose
->io_base_phys
== 0)
491 /* Let's allocate some IO space for that guy. We don't pass
492 * VM_IOREMAP because we don't care about alignment tricks that
493 * the core does in that case. Maybe we should due to stupid card
494 * with incomplete address decoding but I'd rather not deal with
495 * those outside of the reserved 64K legacy region.
497 area
= __get_vm_area(size_page
, 0, PHB_IO_BASE
, PHB_IO_END
);
500 hose
->io_base_alloc
= area
->addr
;
501 hose
->io_base_virt
= (void __iomem
*)(area
->addr
+
502 hose
->io_base_phys
- phys_page
);
504 pr_debug("IO mapping for PHB %s\n", hose
->dn
->full_name
);
505 pr_debug(" phys=0x%016llx, virt=0x%p (alloc=0x%p)\n",
506 hose
->io_base_phys
, hose
->io_base_virt
, hose
->io_base_alloc
);
507 pr_debug(" size=0x%016lx (alloc=0x%016lx)\n",
508 hose
->pci_io_size
, size_page
);
510 /* Establish the mapping */
511 if (__ioremap_at(phys_page
, area
->addr
, size_page
,
512 _PAGE_NO_CACHE
| _PAGE_GUARDED
) == NULL
)
515 /* Fixup hose IO resource */
516 io_virt_offset
= (unsigned long)hose
->io_base_virt
- _IO_BASE
;
517 hose
->io_resource
.start
+= io_virt_offset
;
518 hose
->io_resource
.end
+= io_virt_offset
;
520 pr_debug(" hose->io_resource=0x%016llx...0x%016llx\n",
521 hose
->io_resource
.start
, hose
->io_resource
.end
);
525 EXPORT_SYMBOL_GPL(pcibios_map_io_space
);
527 unsigned long pci_address_to_pio(phys_addr_t address
)
529 struct pci_controller
*hose
, *tmp
;
531 list_for_each_entry_safe(hose
, tmp
, &hose_list
, list_node
) {
532 if (address
>= hose
->io_base_phys
&&
533 address
< (hose
->io_base_phys
+ hose
->pci_io_size
)) {
535 (unsigned long)hose
->io_base_virt
- _IO_BASE
;
536 return base
+ (address
- hose
->io_base_phys
);
539 return (unsigned int)-1;
541 EXPORT_SYMBOL_GPL(pci_address_to_pio
);
544 #define IOBASE_BRIDGE_NUMBER 0
545 #define IOBASE_MEMORY 1
547 #define IOBASE_ISA_IO 3
548 #define IOBASE_ISA_MEM 4
550 long sys_pciconfig_iobase(long which
, unsigned long in_bus
,
551 unsigned long in_devfn
)
553 struct pci_controller
* hose
;
554 struct list_head
*ln
;
555 struct pci_bus
*bus
= NULL
;
556 struct device_node
*hose_node
;
558 /* Argh ! Please forgive me for that hack, but that's the
559 * simplest way to get existing XFree to not lockup on some
560 * G5 machines... So when something asks for bus 0 io base
561 * (bus 0 is HT root), we return the AGP one instead.
563 if (in_bus
== 0 && machine_is_compatible("MacRISC4")) {
564 struct device_node
*agp
;
566 agp
= of_find_compatible_node(NULL
, NULL
, "u3-agp");
572 /* That syscall isn't quite compatible with PCI domains, but it's
573 * used on pre-domains setup. We return the first match
576 for (ln
= pci_root_buses
.next
; ln
!= &pci_root_buses
; ln
= ln
->next
) {
578 if (in_bus
>= bus
->number
&& in_bus
<= bus
->subordinate
)
582 if (bus
== NULL
|| bus
->sysdata
== NULL
)
585 hose_node
= (struct device_node
*)bus
->sysdata
;
586 hose
= PCI_DN(hose_node
)->phb
;
589 case IOBASE_BRIDGE_NUMBER
:
590 return (long)hose
->first_busno
;
592 return (long)hose
->pci_mem_offset
;
594 return (long)hose
->io_base_phys
;
596 return (long)isa_io_base
;
605 int pcibus_to_node(struct pci_bus
*bus
)
607 struct pci_controller
*phb
= pci_bus_to_host(bus
);
610 EXPORT_SYMBOL(pcibus_to_node
);