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 static void pbus_assign_resources_sorted(struct pci_bus
*bus
)
34 struct resource_list head
, *list
, *tmp
;
38 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
39 u16
class = dev
->class >> 8;
41 /* Don't touch classless devices or host bridges or ioapics. */
42 if (class == PCI_CLASS_NOT_DEFINED
||
43 class == PCI_CLASS_BRIDGE_HOST
)
46 /* Don't touch ioapic devices already enabled by firmware */
47 if (class == PCI_CLASS_SYSTEM_PIC
) {
49 pci_read_config_word(dev
, PCI_COMMAND
, &command
);
50 if (command
& (PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
))
54 pdev_sort_resources(dev
, &head
);
57 for (list
= head
.next
; list
;) {
59 idx
= res
- &list
->dev
->resource
[0];
60 if (pci_assign_resource(list
->dev
, idx
)) {
61 /* FIXME: get rid of this */
72 void pci_setup_cardbus(struct pci_bus
*bus
)
74 struct pci_dev
*bridge
= bus
->self
;
75 struct pci_bus_region region
;
77 dev_info(&bridge
->dev
, "CardBus bridge, secondary bus %04x:%02x\n",
78 pci_domain_nr(bus
), bus
->number
);
80 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[0]);
81 if (bus
->resource
[0]->flags
& IORESOURCE_IO
) {
83 * The IO resource is allocated a range twice as large as it
84 * would normally need. This allows us to set both IO regs.
86 dev_info(&bridge
->dev
, " IO window: %#08lx-%#08lx\n",
87 (unsigned long)region
.start
,
88 (unsigned long)region
.end
);
89 pci_write_config_dword(bridge
, PCI_CB_IO_BASE_0
,
91 pci_write_config_dword(bridge
, PCI_CB_IO_LIMIT_0
,
95 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[1]);
96 if (bus
->resource
[1]->flags
& IORESOURCE_IO
) {
97 dev_info(&bridge
->dev
, " IO window: %#08lx-%#08lx\n",
98 (unsigned long)region
.start
,
99 (unsigned long)region
.end
);
100 pci_write_config_dword(bridge
, PCI_CB_IO_BASE_1
,
102 pci_write_config_dword(bridge
, PCI_CB_IO_LIMIT_1
,
106 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[2]);
107 if (bus
->resource
[2]->flags
& IORESOURCE_MEM
) {
108 dev_info(&bridge
->dev
, " PREFETCH window: %#08lx-%#08lx\n",
109 (unsigned long)region
.start
,
110 (unsigned long)region
.end
);
111 pci_write_config_dword(bridge
, PCI_CB_MEMORY_BASE_0
,
113 pci_write_config_dword(bridge
, PCI_CB_MEMORY_LIMIT_0
,
117 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[3]);
118 if (bus
->resource
[3]->flags
& IORESOURCE_MEM
) {
119 dev_info(&bridge
->dev
, " MEM window: %#08lx-%#08lx\n",
120 (unsigned long)region
.start
,
121 (unsigned long)region
.end
);
122 pci_write_config_dword(bridge
, PCI_CB_MEMORY_BASE_1
,
124 pci_write_config_dword(bridge
, PCI_CB_MEMORY_LIMIT_1
,
128 EXPORT_SYMBOL(pci_setup_cardbus
);
130 /* Initialize bridges with base/limit values we have collected.
131 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
132 requires that if there is no I/O ports or memory behind the
133 bridge, corresponding range must be turned off by writing base
134 value greater than limit to the bridge's base/limit registers.
136 Note: care must be taken when updating I/O base/limit registers
137 of bridges which support 32-bit I/O. This update requires two
138 config space writes, so it's quite possible that an I/O window of
139 the bridge will have some undesirable address (e.g. 0) after the
140 first write. Ditto 64-bit prefetchable MMIO. */
141 static void pci_setup_bridge(struct pci_bus
*bus
)
143 struct pci_dev
*bridge
= bus
->self
;
144 struct pci_bus_region region
;
145 u32 l
, bu
, lu
, io_upper16
;
147 dev_info(&bridge
->dev
, "PCI bridge, secondary bus %04x:%02x\n",
148 pci_domain_nr(bus
), bus
->number
);
150 /* Set up the top and bottom of the PCI I/O segment for this bus. */
151 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[0]);
152 if (bus
->resource
[0]->flags
& IORESOURCE_IO
) {
153 pci_read_config_dword(bridge
, PCI_IO_BASE
, &l
);
155 l
|= (region
.start
>> 8) & 0x00f0;
156 l
|= region
.end
& 0xf000;
157 /* Set up upper 16 bits of I/O base/limit. */
158 io_upper16
= (region
.end
& 0xffff0000) | (region
.start
>> 16);
159 dev_info(&bridge
->dev
, " IO window: %#04lx-%#04lx\n",
160 (unsigned long)region
.start
,
161 (unsigned long)region
.end
);
164 /* Clear upper 16 bits of I/O base/limit. */
167 dev_info(&bridge
->dev
, " IO window: disabled\n");
169 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
170 pci_write_config_dword(bridge
, PCI_IO_BASE_UPPER16
, 0x0000ffff);
171 /* Update lower 16 bits of I/O base/limit. */
172 pci_write_config_dword(bridge
, PCI_IO_BASE
, l
);
173 /* Update upper 16 bits of I/O base/limit. */
174 pci_write_config_dword(bridge
, PCI_IO_BASE_UPPER16
, io_upper16
);
176 /* Set up the top and bottom of the PCI Memory segment
178 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[1]);
179 if (bus
->resource
[1]->flags
& IORESOURCE_MEM
) {
180 l
= (region
.start
>> 16) & 0xfff0;
181 l
|= region
.end
& 0xfff00000;
182 dev_info(&bridge
->dev
, " MEM window: %#08lx-%#08lx\n",
183 (unsigned long)region
.start
,
184 (unsigned long)region
.end
);
188 dev_info(&bridge
->dev
, " 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. */
199 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[2]);
200 if (bus
->resource
[2]->flags
& IORESOURCE_PREFETCH
) {
201 l
= (region
.start
>> 16) & 0xfff0;
202 l
|= region
.end
& 0xfff00000;
203 bu
= upper_32_bits(region
.start
);
204 lu
= upper_32_bits(region
.end
);
205 dev_info(&bridge
->dev
, " PREFETCH window: %#016llx-%#016llx\n",
206 (unsigned long long)region
.start
,
207 (unsigned long long)region
.end
);
211 dev_info(&bridge
->dev
, " PREFETCH window: disabled\n");
213 pci_write_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, l
);
215 /* Set the upper 32 bits of PREF base & limit. */
216 pci_write_config_dword(bridge
, PCI_PREF_BASE_UPPER32
, bu
);
217 pci_write_config_dword(bridge
, PCI_PREF_LIMIT_UPPER32
, lu
);
219 pci_write_config_word(bridge
, PCI_BRIDGE_CONTROL
, bus
->bridge_ctl
);
222 /* Check whether the bridge supports optional I/O and
223 prefetchable memory ranges. If not, the respective
224 base/limit registers must be read-only and read as 0. */
225 static void pci_bridge_check_ranges(struct pci_bus
*bus
)
229 struct pci_dev
*bridge
= bus
->self
;
230 struct resource
*b_res
;
232 b_res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
];
233 b_res
[1].flags
|= IORESOURCE_MEM
;
235 pci_read_config_word(bridge
, PCI_IO_BASE
, &io
);
237 pci_write_config_word(bridge
, PCI_IO_BASE
, 0xf0f0);
238 pci_read_config_word(bridge
, PCI_IO_BASE
, &io
);
239 pci_write_config_word(bridge
, PCI_IO_BASE
, 0x0);
242 b_res
[0].flags
|= IORESOURCE_IO
;
243 /* DECchip 21050 pass 2 errata: the bridge may miss an address
244 disconnect boundary by one PCI data phase.
245 Workaround: do not use prefetching on this device. */
246 if (bridge
->vendor
== PCI_VENDOR_ID_DEC
&& bridge
->device
== 0x0001)
248 pci_read_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, &pmem
);
250 pci_write_config_dword(bridge
, PCI_PREF_MEMORY_BASE
,
252 pci_read_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, &pmem
);
253 pci_write_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, 0x0);
256 b_res
[2].flags
|= IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
259 /* Helper function for sizing routines: find first available
260 bus resource of a given type. Note: we intentionally skip
261 the bus resources which have already been assigned (that is,
262 have non-NULL parent resource). */
263 static struct resource
*find_free_bus_resource(struct pci_bus
*bus
, unsigned long type
)
267 unsigned long type_mask
= IORESOURCE_IO
| IORESOURCE_MEM
|
270 for (i
= 0; i
< PCI_BUS_NUM_RESOURCES
; i
++) {
271 r
= bus
->resource
[i
];
272 if (r
== &ioport_resource
|| r
== &iomem_resource
)
274 if (r
&& (r
->flags
& type_mask
) == type
&& !r
->parent
)
280 /* Sizing the IO windows of the PCI-PCI bridge is trivial,
281 since these windows have 4K granularity and the IO ranges
282 of non-bridge PCI devices are limited to 256 bytes.
283 We must be careful with the ISA aliasing though. */
284 static void pbus_size_io(struct pci_bus
*bus
)
287 struct resource
*b_res
= find_free_bus_resource(bus
, IORESOURCE_IO
);
288 unsigned long size
= 0, size1
= 0;
293 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
296 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
297 struct resource
*r
= &dev
->resource
[i
];
298 unsigned long r_size
;
300 if (r
->parent
|| !(r
->flags
& IORESOURCE_IO
))
302 r_size
= r
->end
- r
->start
+ 1;
305 /* Might be re-aligned for ISA */
311 /* To be fixed in 2.5: we should have sort of HAVE_ISA
312 flag in the struct pci_bus. */
313 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
314 size
= (size
& 0xff) + ((size
& ~0xffUL
) << 2);
316 size
= ALIGN(size
+ size1
, 4096);
321 /* Alignment of the IO window is always 4K */
323 b_res
->end
= b_res
->start
+ size
- 1;
324 b_res
->flags
|= IORESOURCE_STARTALIGN
;
327 /* Calculate the size of the bus and minimal alignment which
328 guarantees that all child resources fit in this size. */
329 static int pbus_size_mem(struct pci_bus
*bus
, unsigned long mask
, unsigned long type
)
332 resource_size_t min_align
, align
, size
;
333 resource_size_t aligns
[12]; /* Alignments from 1Mb to 2Gb */
334 int order
, max_order
;
335 struct resource
*b_res
= find_free_bus_resource(bus
, type
);
340 memset(aligns
, 0, sizeof(aligns
));
344 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
347 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
348 struct resource
*r
= &dev
->resource
[i
];
349 resource_size_t r_size
;
351 if (r
->parent
|| (r
->flags
& mask
) != type
)
353 r_size
= r
->end
- r
->start
+ 1;
354 /* For bridges size != alignment */
355 align
= resource_alignment(r
);
356 order
= __ffs(align
) - 20;
358 dev_warn(&dev
->dev
, "BAR %d bad alignment %llx: "
359 "%#016llx-%#016llx\n", i
,
360 (unsigned long long)align
,
361 (unsigned long long)r
->start
,
362 (unsigned long long)r
->end
);
369 /* Exclude ranges with size > align from
370 calculation of the alignment. */
372 aligns
[order
] += align
;
373 if (order
> max_order
)
380 for (order
= 0; order
<= max_order
; order
++) {
381 resource_size_t align1
= 1;
383 align1
<<= (order
+ 20);
387 else if (ALIGN(align
+ min_align
, min_align
) < align1
)
388 min_align
= align1
>> 1;
389 align
+= aligns
[order
];
391 size
= ALIGN(size
, min_align
);
396 b_res
->start
= min_align
;
397 b_res
->end
= size
+ min_align
- 1;
398 b_res
->flags
|= IORESOURCE_STARTALIGN
;
402 static void pci_bus_size_cardbus(struct pci_bus
*bus
)
404 struct pci_dev
*bridge
= bus
->self
;
405 struct resource
*b_res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
];
409 * Reserve some resources for CardBus. We reserve
410 * a fixed amount of bus space for CardBus bridges.
413 b_res
[0].end
= pci_cardbus_io_size
- 1;
414 b_res
[0].flags
|= IORESOURCE_IO
| IORESOURCE_SIZEALIGN
;
417 b_res
[1].end
= pci_cardbus_io_size
- 1;
418 b_res
[1].flags
|= IORESOURCE_IO
| IORESOURCE_SIZEALIGN
;
421 * Check whether prefetchable memory is supported
424 pci_read_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, &ctrl
);
425 if (!(ctrl
& PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
)) {
426 ctrl
|= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
;
427 pci_write_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, ctrl
);
428 pci_read_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, &ctrl
);
432 * If we have prefetchable memory support, allocate
433 * two regions. Otherwise, allocate one region of
436 if (ctrl
& PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
) {
438 b_res
[2].end
= pci_cardbus_mem_size
- 1;
439 b_res
[2].flags
|= IORESOURCE_MEM
| IORESOURCE_PREFETCH
| IORESOURCE_SIZEALIGN
;
442 b_res
[3].end
= pci_cardbus_mem_size
- 1;
443 b_res
[3].flags
|= IORESOURCE_MEM
| IORESOURCE_SIZEALIGN
;
446 b_res
[3].end
= pci_cardbus_mem_size
* 2 - 1;
447 b_res
[3].flags
|= IORESOURCE_MEM
| IORESOURCE_SIZEALIGN
;
451 void __ref
pci_bus_size_bridges(struct pci_bus
*bus
)
454 unsigned long mask
, prefmask
;
456 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
457 struct pci_bus
*b
= dev
->subordinate
;
461 switch (dev
->class >> 8) {
462 case PCI_CLASS_BRIDGE_CARDBUS
:
463 pci_bus_size_cardbus(b
);
466 case PCI_CLASS_BRIDGE_PCI
:
468 pci_bus_size_bridges(b
);
477 switch (bus
->self
->class >> 8) {
478 case PCI_CLASS_BRIDGE_CARDBUS
:
479 /* don't size cardbuses yet. */
482 case PCI_CLASS_BRIDGE_PCI
:
483 pci_bridge_check_ranges(bus
);
486 /* If the bridge supports prefetchable range, size it
487 separately. If it doesn't, or its prefetchable window
488 has already been allocated by arch code, try
489 non-prefetchable range for both types of PCI memory
491 mask
= IORESOURCE_MEM
;
492 prefmask
= IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
493 if (pbus_size_mem(bus
, prefmask
, prefmask
))
494 mask
= prefmask
; /* Success, size non-prefetch only. */
495 pbus_size_mem(bus
, mask
, IORESOURCE_MEM
);
499 EXPORT_SYMBOL(pci_bus_size_bridges
);
501 void __ref
pci_bus_assign_resources(struct pci_bus
*bus
)
506 pbus_assign_resources_sorted(bus
);
508 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
509 b
= dev
->subordinate
;
513 pci_bus_assign_resources(b
);
515 switch (dev
->class >> 8) {
516 case PCI_CLASS_BRIDGE_PCI
:
520 case PCI_CLASS_BRIDGE_CARDBUS
:
521 pci_setup_cardbus(b
);
525 dev_info(&dev
->dev
, "not setting up bridge for bus "
526 "%04x:%02x\n", pci_domain_nr(b
), b
->number
);
531 EXPORT_SYMBOL(pci_bus_assign_resources
);
533 static void pci_bus_dump_res(struct pci_bus
*bus
)
537 for (i
= 0; i
< PCI_BUS_NUM_RESOURCES
; i
++) {
538 struct resource
*res
= bus
->resource
[i
];
542 printk(KERN_INFO
"bus: %02x index %x %s: [%llx, %llx]\n",
544 (res
->flags
& IORESOURCE_IO
) ? "io port" : "mmio",
545 (unsigned long long) res
->start
,
546 (unsigned long long) res
->end
);
550 static void pci_bus_dump_resources(struct pci_bus
*bus
)
556 pci_bus_dump_res(bus
);
558 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
559 b
= dev
->subordinate
;
563 pci_bus_dump_resources(b
);
568 pci_assign_unassigned_resources(void)
572 /* Depth first, calculate sizes and alignments of all
573 subordinate buses. */
574 list_for_each_entry(bus
, &pci_root_buses
, node
) {
575 pci_bus_size_bridges(bus
);
577 /* Depth last, allocate resources and update the hardware. */
578 list_for_each_entry(bus
, &pci_root_buses
, node
) {
579 pci_bus_assign_resources(bus
);
580 pci_enable_bridges(bus
);
583 /* dump the resource on buses */
584 list_for_each_entry(bus
, &pci_root_buses
, node
) {
585 pci_bus_dump_resources(bus
);