2 * drivers/pci/setup-bus.c
4 * Extruded from code written by
5 * Dave Rusling (david.rusling@reo.mts.dec.com)
6 * David Mosberger (davidm@cs.arizona.edu)
7 * David Miller (davem@redhat.com)
9 * Support routines for initializing a PCI subsystem.
13 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
14 * PCI-PCI bridges cleanup, sorted resource allocation.
15 * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
16 * Converted to allocation in 3 passes, which gives
17 * tighter packing. Prefetchable range support.
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/pci.h>
24 #include <linux/errno.h>
25 #include <linux/ioport.h>
26 #include <linux/cache.h>
27 #include <linux/slab.h>
30 #define DEBUG_CONFIG 1
32 #define DBG(x...) printk(x)
37 static void pbus_assign_resources_sorted(struct pci_bus
*bus
)
41 struct resource_list head
, *list
, *tmp
;
45 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
46 u16
class = dev
->class >> 8;
48 /* Don't touch classless devices or host bridges or ioapics. */
49 if (class == PCI_CLASS_NOT_DEFINED
||
50 class == PCI_CLASS_BRIDGE_HOST
)
53 /* Don't touch ioapic devices already enabled by firmware */
54 if (class == PCI_CLASS_SYSTEM_PIC
) {
56 pci_read_config_word(dev
, PCI_COMMAND
, &command
);
57 if (command
& (PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
))
61 pdev_sort_resources(dev
, &head
);
64 for (list
= head
.next
; list
;) {
66 idx
= res
- &list
->dev
->resource
[0];
67 if (pci_assign_resource(list
->dev
, idx
)) {
78 void pci_setup_cardbus(struct pci_bus
*bus
)
80 struct pci_dev
*bridge
= bus
->self
;
81 struct pci_bus_region region
;
83 printk("PCI: Bus %d, cardbus bridge: %s\n",
84 bus
->number
, pci_name(bridge
));
86 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[0]);
87 if (bus
->resource
[0]->flags
& IORESOURCE_IO
) {
89 * The IO resource is allocated a range twice as large as it
90 * would normally need. This allows us to set both IO regs.
92 printk(" IO window: %08lx-%08lx\n",
93 region
.start
, region
.end
);
94 pci_write_config_dword(bridge
, PCI_CB_IO_BASE_0
,
96 pci_write_config_dword(bridge
, PCI_CB_IO_LIMIT_0
,
100 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[1]);
101 if (bus
->resource
[1]->flags
& IORESOURCE_IO
) {
102 printk(" IO window: %08lx-%08lx\n",
103 region
.start
, region
.end
);
104 pci_write_config_dword(bridge
, PCI_CB_IO_BASE_1
,
106 pci_write_config_dword(bridge
, PCI_CB_IO_LIMIT_1
,
110 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[2]);
111 if (bus
->resource
[2]->flags
& IORESOURCE_MEM
) {
112 printk(" PREFETCH window: %08lx-%08lx\n",
113 region
.start
, region
.end
);
114 pci_write_config_dword(bridge
, PCI_CB_MEMORY_BASE_0
,
116 pci_write_config_dword(bridge
, PCI_CB_MEMORY_LIMIT_0
,
120 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[3]);
121 if (bus
->resource
[3]->flags
& IORESOURCE_MEM
) {
122 printk(" MEM window: %08lx-%08lx\n",
123 region
.start
, region
.end
);
124 pci_write_config_dword(bridge
, PCI_CB_MEMORY_BASE_1
,
126 pci_write_config_dword(bridge
, PCI_CB_MEMORY_LIMIT_1
,
130 EXPORT_SYMBOL(pci_setup_cardbus
);
132 /* Initialize bridges with base/limit values we have collected.
133 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
134 requires that if there is no I/O ports or memory behind the
135 bridge, corresponding range must be turned off by writing base
136 value greater than limit to the bridge's base/limit registers.
138 Note: care must be taken when updating I/O base/limit registers
139 of bridges which support 32-bit I/O. This update requires two
140 config space writes, so it's quite possible that an I/O window of
141 the bridge will have some undesirable address (e.g. 0) after the
142 first write. Ditto 64-bit prefetchable MMIO. */
143 static void __devinit
144 pci_setup_bridge(struct pci_bus
*bus
)
146 struct pci_dev
*bridge
= bus
->self
;
147 struct pci_bus_region region
;
150 DBG(KERN_INFO
"PCI: Bridge: %s\n", pci_name(bridge
));
152 /* Set up the top and bottom of the PCI I/O segment for this bus. */
153 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[0]);
154 if (bus
->resource
[0]->flags
& IORESOURCE_IO
) {
155 pci_read_config_dword(bridge
, PCI_IO_BASE
, &l
);
157 l
|= (region
.start
>> 8) & 0x00f0;
158 l
|= region
.end
& 0xf000;
159 /* Set up upper 16 bits of I/O base/limit. */
160 io_upper16
= (region
.end
& 0xffff0000) | (region
.start
>> 16);
161 DBG(KERN_INFO
" IO window: %04lx-%04lx\n",
162 region
.start
, region
.end
);
165 /* Clear upper 16 bits of I/O base/limit. */
168 DBG(KERN_INFO
" IO window: disabled.\n");
170 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
171 pci_write_config_dword(bridge
, PCI_IO_BASE_UPPER16
, 0x0000ffff);
172 /* Update lower 16 bits of I/O base/limit. */
173 pci_write_config_dword(bridge
, PCI_IO_BASE
, l
);
174 /* Update upper 16 bits of I/O base/limit. */
175 pci_write_config_dword(bridge
, PCI_IO_BASE_UPPER16
, io_upper16
);
177 /* Set up the top and bottom of the PCI Memory segment
179 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[1]);
180 if (bus
->resource
[1]->flags
& IORESOURCE_MEM
) {
181 l
= (region
.start
>> 16) & 0xfff0;
182 l
|= region
.end
& 0xfff00000;
183 DBG(KERN_INFO
" MEM window: %08lx-%08lx\n",
184 region
.start
, region
.end
);
188 DBG(KERN_INFO
" MEM window: disabled.\n");
190 pci_write_config_dword(bridge
, PCI_MEMORY_BASE
, l
);
192 /* Clear out the upper 32 bits of PREF limit.
193 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
194 disables PREF range, which is ok. */
195 pci_write_config_dword(bridge
, PCI_PREF_LIMIT_UPPER32
, 0);
197 /* Set up PREF base/limit. */
198 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[2]);
199 if (bus
->resource
[2]->flags
& IORESOURCE_PREFETCH
) {
200 l
= (region
.start
>> 16) & 0xfff0;
201 l
|= region
.end
& 0xfff00000;
202 DBG(KERN_INFO
" PREFETCH window: %08lx-%08lx\n",
203 region
.start
, region
.end
);
207 DBG(KERN_INFO
" PREFETCH window: disabled.\n");
209 pci_write_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, l
);
211 /* Clear out the upper 32 bits of PREF base. */
212 pci_write_config_dword(bridge
, PCI_PREF_BASE_UPPER32
, 0);
214 pci_write_config_word(bridge
, PCI_BRIDGE_CONTROL
, bus
->bridge_ctl
);
217 /* Check whether the bridge supports optional I/O and
218 prefetchable memory ranges. If not, the respective
219 base/limit registers must be read-only and read as 0. */
220 static void pci_bridge_check_ranges(struct pci_bus
*bus
)
224 struct pci_dev
*bridge
= bus
->self
;
225 struct resource
*b_res
;
227 b_res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
];
228 b_res
[1].flags
|= IORESOURCE_MEM
;
230 pci_read_config_word(bridge
, PCI_IO_BASE
, &io
);
232 pci_write_config_word(bridge
, PCI_IO_BASE
, 0xf0f0);
233 pci_read_config_word(bridge
, PCI_IO_BASE
, &io
);
234 pci_write_config_word(bridge
, PCI_IO_BASE
, 0x0);
237 b_res
[0].flags
|= IORESOURCE_IO
;
238 /* DECchip 21050 pass 2 errata: the bridge may miss an address
239 disconnect boundary by one PCI data phase.
240 Workaround: do not use prefetching on this device. */
241 if (bridge
->vendor
== PCI_VENDOR_ID_DEC
&& bridge
->device
== 0x0001)
243 pci_read_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, &pmem
);
245 pci_write_config_dword(bridge
, PCI_PREF_MEMORY_BASE
,
247 pci_read_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, &pmem
);
248 pci_write_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, 0x0);
251 b_res
[2].flags
|= IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
254 /* Helper function for sizing routines: find first available
255 bus resource of a given type. Note: we intentionally skip
256 the bus resources which have already been assigned (that is,
257 have non-NULL parent resource). */
258 static struct resource
*find_free_bus_resource(struct pci_bus
*bus
, unsigned long type
)
262 unsigned long type_mask
= IORESOURCE_IO
| IORESOURCE_MEM
|
265 for (i
= 0; i
< PCI_BUS_NUM_RESOURCES
; i
++) {
266 r
= bus
->resource
[i
];
267 if (r
== &ioport_resource
|| r
== &iomem_resource
)
269 if (r
&& (r
->flags
& type_mask
) == type
&& !r
->parent
)
275 /* Sizing the IO windows of the PCI-PCI bridge is trivial,
276 since these windows have 4K granularity and the IO ranges
277 of non-bridge PCI devices are limited to 256 bytes.
278 We must be careful with the ISA aliasing though. */
279 static void pbus_size_io(struct pci_bus
*bus
)
282 struct resource
*b_res
= find_free_bus_resource(bus
, IORESOURCE_IO
);
283 unsigned long size
= 0, size1
= 0;
288 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
291 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
292 struct resource
*r
= &dev
->resource
[i
];
293 unsigned long r_size
;
295 if (r
->parent
|| !(r
->flags
& IORESOURCE_IO
))
297 r_size
= r
->end
- r
->start
+ 1;
300 /* Might be re-aligned for ISA */
306 /* To be fixed in 2.5: we should have sort of HAVE_ISA
307 flag in the struct pci_bus. */
308 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
309 size
= (size
& 0xff) + ((size
& ~0xffUL
) << 2);
311 size
= ALIGN(size
+ size1
, 4096);
316 /* Alignment of the IO window is always 4K */
318 b_res
->end
= b_res
->start
+ size
- 1;
321 /* Calculate the size of the bus and minimal alignment which
322 guarantees that all child resources fit in this size. */
323 static int pbus_size_mem(struct pci_bus
*bus
, unsigned long mask
, unsigned long type
)
326 unsigned long min_align
, align
, size
;
327 unsigned long aligns
[12]; /* Alignments from 1Mb to 2Gb */
328 int order
, max_order
;
329 struct resource
*b_res
= find_free_bus_resource(bus
, type
);
334 memset(aligns
, 0, sizeof(aligns
));
338 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
341 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
342 struct resource
*r
= &dev
->resource
[i
];
343 unsigned long r_size
;
345 if (r
->parent
|| (r
->flags
& mask
) != type
)
347 r_size
= r
->end
- r
->start
+ 1;
348 /* For bridges size != alignment */
349 align
= (i
< PCI_BRIDGE_RESOURCES
) ? r_size
: r
->start
;
350 order
= __ffs(align
) - 20;
352 printk(KERN_WARNING
"PCI: region %s/%d "
353 "too large: %llx-%llx\n",
355 (unsigned long long)r
->start
,
356 (unsigned long long)r
->end
);
363 /* Exclude ranges with size > align from
364 calculation of the alignment. */
366 aligns
[order
] += align
;
367 if (order
> max_order
)
374 for (order
= 0; order
<= max_order
; order
++) {
375 unsigned long align1
= 1UL << (order
+ 20);
379 else if (ALIGN(align
+ min_align
, min_align
) < align1
)
380 min_align
= align1
>> 1;
381 align
+= aligns
[order
];
383 size
= ALIGN(size
, min_align
);
388 b_res
->start
= min_align
;
389 b_res
->end
= size
+ min_align
- 1;
393 static void __devinit
394 pci_bus_size_cardbus(struct pci_bus
*bus
)
396 struct pci_dev
*bridge
= bus
->self
;
397 struct resource
*b_res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
];
401 * Reserve some resources for CardBus. We reserve
402 * a fixed amount of bus space for CardBus bridges.
404 b_res
[0].start
= pci_cardbus_io_size
;
405 b_res
[0].end
= b_res
[0].start
+ pci_cardbus_io_size
- 1;
406 b_res
[0].flags
|= IORESOURCE_IO
;
408 b_res
[1].start
= pci_cardbus_io_size
;
409 b_res
[1].end
= b_res
[1].start
+ pci_cardbus_io_size
- 1;
410 b_res
[1].flags
|= IORESOURCE_IO
;
413 * Check whether prefetchable memory is supported
416 pci_read_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, &ctrl
);
417 if (!(ctrl
& PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
)) {
418 ctrl
|= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
;
419 pci_write_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, ctrl
);
420 pci_read_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, &ctrl
);
424 * If we have prefetchable memory support, allocate
425 * two regions. Otherwise, allocate one region of
428 if (ctrl
& PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
) {
429 b_res
[2].start
= pci_cardbus_mem_size
;
430 b_res
[2].end
= b_res
[2].start
+ pci_cardbus_mem_size
- 1;
431 b_res
[2].flags
|= IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
433 b_res
[3].start
= pci_cardbus_mem_size
;
434 b_res
[3].end
= b_res
[3].start
+ pci_cardbus_mem_size
- 1;
435 b_res
[3].flags
|= IORESOURCE_MEM
;
437 b_res
[3].start
= pci_cardbus_mem_size
* 2;
438 b_res
[3].end
= b_res
[3].start
+ pci_cardbus_mem_size
* 2 - 1;
439 b_res
[3].flags
|= IORESOURCE_MEM
;
443 void pci_bus_size_bridges(struct pci_bus
*bus
)
446 unsigned long mask
, prefmask
;
448 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
449 struct pci_bus
*b
= dev
->subordinate
;
453 switch (dev
->class >> 8) {
454 case PCI_CLASS_BRIDGE_CARDBUS
:
455 pci_bus_size_cardbus(b
);
458 case PCI_CLASS_BRIDGE_PCI
:
460 pci_bus_size_bridges(b
);
469 switch (bus
->self
->class >> 8) {
470 case PCI_CLASS_BRIDGE_CARDBUS
:
471 /* don't size cardbuses yet. */
474 case PCI_CLASS_BRIDGE_PCI
:
475 pci_bridge_check_ranges(bus
);
478 /* If the bridge supports prefetchable range, size it
479 separately. If it doesn't, or its prefetchable window
480 has already been allocated by arch code, try
481 non-prefetchable range for both types of PCI memory
483 mask
= IORESOURCE_MEM
;
484 prefmask
= IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
485 if (pbus_size_mem(bus
, prefmask
, prefmask
))
486 mask
= prefmask
; /* Success, size non-prefetch only. */
487 pbus_size_mem(bus
, mask
, IORESOURCE_MEM
);
491 EXPORT_SYMBOL(pci_bus_size_bridges
);
493 void pci_bus_assign_resources(struct pci_bus
*bus
)
498 pbus_assign_resources_sorted(bus
);
500 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
501 b
= dev
->subordinate
;
505 pci_bus_assign_resources(b
);
507 switch (dev
->class >> 8) {
508 case PCI_CLASS_BRIDGE_PCI
:
512 case PCI_CLASS_BRIDGE_CARDBUS
:
513 pci_setup_cardbus(b
);
517 printk(KERN_INFO
"PCI: not setting up bridge %s "
518 "for bus %d\n", pci_name(dev
), b
->number
);
523 EXPORT_SYMBOL(pci_bus_assign_resources
);
526 pci_assign_unassigned_resources(void)
530 /* Depth first, calculate sizes and alignments of all
531 subordinate buses. */
532 list_for_each_entry(bus
, &pci_root_buses
, node
) {
533 pci_bus_size_bridges(bus
);
535 /* Depth last, allocate resources and update the hardware. */
536 list_for_each_entry(bus
, &pci_root_buses
, node
) {
537 pci_bus_assign_resources(bus
);
538 pci_enable_bridges(bus
);