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>
28 #include <linux/config.h>
30 #define DEBUG_CONFIG 1
32 # define DBGC(args) printk args
37 #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
40 * FIXME: IO should be max 256 bytes. However, since we may
41 * have a P2P bridge below a cardbus bridge, we need 4K.
43 #define CARDBUS_IO_SIZE (4096)
44 #ifdef CONFIG_ARCH_MOXACPU // add by Victor Yu. 01-05-2006
45 #define CARDBUS_MEM_SIZE (4*1024*1024)
47 #define CARDBUS_MEM_SIZE (32*1024*1024)
51 pbus_assign_resources_sorted(struct pci_bus
*bus
)
55 struct resource_list head
, *list
, *tmp
;
58 bus
->bridge_ctl
&= ~PCI_BRIDGE_CTL_VGA
;
61 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
62 u16
class = dev
->class >> 8;
64 if (class == PCI_CLASS_DISPLAY_VGA
65 || class == PCI_CLASS_NOT_DEFINED_VGA
)
66 bus
->bridge_ctl
|= PCI_BRIDGE_CTL_VGA
;
68 pdev_sort_resources(dev
, &head
);
71 for (list
= head
.next
; list
;) {
73 idx
= res
- &list
->dev
->resource
[0];
74 pci_assign_resource(list
->dev
, idx
);
82 pci_setup_cardbus(struct pci_bus
*bus
)
84 struct pci_dev
*bridge
= bus
->self
;
85 struct pci_bus_region region
;
87 printk("PCI: Bus %d, cardbus bridge: %s\n",
88 bus
->number
, pci_name(bridge
));
90 #if 0 // This is a bug. It will not match PCMCIA device driver. Victor Yu. 01-11-2006
91 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[0]);
92 if (bus
->resource
[0]->flags
& IORESOURCE_IO
) {
94 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[2]);
95 if (bus
->resource
[2]->flags
& IORESOURCE_IO
) {
98 * The IO resource is allocated a range twice as large as it
99 * would normally need. This allows us to set both IO regs.
101 printk(" IO window: %08lx-%08lx\n",
102 region
.start
, region
.end
);
103 #ifdef CONFIG_ARCH_MOXACPU // add by Victor Yu. 01-10-2006
104 pci_write_config_dword(bridge
, PCI_CB_IO_BASE_0
,
105 PHY_ADDRESS(region
.start
));
106 pci_write_config_dword(bridge
, PCI_CB_IO_LIMIT_0
,
107 PHY_ADDRESS(region
.end
));
109 pci_write_config_dword(bridge
, PCI_CB_IO_BASE_0
,
111 pci_write_config_dword(bridge
, PCI_CB_IO_LIMIT_0
,
116 #if 0 // This is a bug. It will not match PCMCIA device driver. Victor Yu. 01-11-2006
117 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[1]);
118 if (bus
->resource
[1]->flags
& IORESOURCE_IO
) {
120 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[3]);
121 if (bus
->resource
[3]->flags
& IORESOURCE_IO
) {
123 printk(" IO window: %08lx-%08lx\n",
124 region
.start
, region
.end
);
125 #ifdef CONFIG_ARCH_MOXACPU // add by Victor Yu. 01-10-2006
126 pci_write_config_dword(bridge
, PCI_CB_IO_BASE_1
,
127 PHY_ADDRESS(region
.start
));
128 pci_write_config_dword(bridge
, PCI_CB_IO_LIMIT_1
,
129 PHY_ADDRESS(region
.end
));
131 pci_write_config_dword(bridge
, PCI_CB_IO_BASE_1
,
133 pci_write_config_dword(bridge
, PCI_CB_IO_LIMIT_1
,
138 #if 0 // This is a bug. It will not match PCMCIA device driver. Victor Yu. 01-11-2006
139 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[2]);
140 if (bus
->resource
[2]->flags
& IORESOURCE_MEM
) {
142 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[0]);
143 if (bus
->resource
[0]->flags
& IORESOURCE_MEM
) {
145 printk(" PREFETCH window: %08lx-%08lx\n",
146 region
.start
, region
.end
);
147 #ifdef CONFIG_ARCH_MOXACPU // add by Victor Yu. 01-10-2006
148 pci_write_config_dword(bridge
, PCI_CB_MEMORY_BASE_0
,
149 MEM_PHY_ADDRESS(region
.start
));
150 pci_write_config_dword(bridge
, PCI_CB_MEMORY_LIMIT_0
,
151 MEM_PHY_ADDRESS(region
.end
));
153 pci_write_config_dword(bridge
, PCI_CB_MEMORY_BASE_0
,
155 pci_write_config_dword(bridge
, PCI_CB_MEMORY_LIMIT_0
,
160 #if 0 // This is a bug. It will not match PCMCIA device driver. Victor Yu. 01-11-2006
161 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[3]);
162 if (bus
->resource
[3]->flags
& IORESOURCE_MEM
) {
164 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[1]);
165 if (bus
->resource
[1]->flags
& IORESOURCE_MEM
) {
167 printk(" MEM window: %08lx-%08lx\n",
168 region
.start
, region
.end
);
169 #ifdef CONFIG_ARCH_MOXACPU // add by Victor Yu. 01-10-2006
170 pci_write_config_dword(bridge
, PCI_CB_MEMORY_BASE_1
,
171 MEM_PHY_ADDRESS(region
.start
));
172 pci_write_config_dword(bridge
, PCI_CB_MEMORY_LIMIT_1
,
173 MEM_PHY_ADDRESS(region
.end
));
175 pci_write_config_dword(bridge
, PCI_CB_MEMORY_BASE_1
,
177 pci_write_config_dword(bridge
, PCI_CB_MEMORY_LIMIT_1
,
183 /* Initialize bridges with base/limit values we have collected.
184 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
185 requires that if there is no I/O ports or memory behind the
186 bridge, corresponding range must be turned off by writing base
187 value greater than limit to the bridge's base/limit registers.
189 Note: care must be taken when updating I/O base/limit registers
190 of bridges which support 32-bit I/O. This update requires two
191 config space writes, so it's quite possible that an I/O window of
192 the bridge will have some undesirable address (e.g. 0) after the
193 first write. Ditto 64-bit prefetchable MMIO. */
194 static void __devinit
195 pci_setup_bridge(struct pci_bus
*bus
)
197 struct pci_dev
*bridge
= bus
->self
;
198 struct pci_bus_region region
;
201 DBGC((KERN_INFO
"PCI: Bus %d, bridge: %s\n",
202 bus
->number
, pci_name(bridge
)));
204 /* Set up the top and bottom of the PCI I/O segment for this bus. */
205 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[0]);
206 if (bus
->resource
[0]->flags
& IORESOURCE_IO
) {
207 pci_read_config_dword(bridge
, PCI_IO_BASE
, &l
);
209 l
|= (PHY_ADDRESS(region
.start
) >> 8) & 0x00f0;
210 l
|= PHY_ADDRESS(region
.end
) & 0xf000;
211 /* Set up upper 16 bits of I/O base/limit. */
212 io_upper16
= (PHY_ADDRESS(region
.end
) & 0xffff0000) | (PHY_ADDRESS(region
.start
) >> 16);
213 DBGC((KERN_INFO
" IO window: %04lx-%04lx\n",
214 PHY_ADDRESS(region
.start
), PHY_ADDRESS(region
.end
)));
217 /* Clear upper 16 bits of I/O base/limit. */
221 DBGC((KERN_INFO
" IO window: disabled.\n"));
223 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
224 pci_write_config_dword(bridge
, PCI_IO_BASE_UPPER16
, 0x0000ffff);
225 /* Update lower 16 bits of I/O base/limit. */
226 pci_write_config_dword(bridge
, PCI_IO_BASE
, l
);
227 /* Update upper 16 bits of I/O base/limit. */
228 pci_write_config_dword(bridge
, PCI_IO_BASE_UPPER16
, io_upper16
);
230 /* Set up the top and bottom of the PCI Memory segment
232 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[1]);
233 if (bus
->resource
[1]->flags
& IORESOURCE_MEM
) {
234 l
= (MEM_PHY_ADDRESS(region
.start
) >> 16) & 0xfff0;
235 l
|= MEM_PHY_ADDRESS(region
.end
) & 0xfff00000;
236 DBGC((KERN_INFO
" MEM window: %08lx-%08lx\n",
237 MEM_PHY_ADDRESS(region
.start
), MEM_PHY_ADDRESS(region
.end
)));
241 DBGC((KERN_INFO
" MEM window: disabled.\n"));
244 pci_write_config_dword(bridge
, PCI_MEMORY_BASE
, l
);
246 /* Clear out the upper 32 bits of PREF limit.
247 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
248 disables PREF range, which is ok. */
249 pci_write_config_dword(bridge
, PCI_PREF_LIMIT_UPPER32
, 0);
251 /* Set up PREF base/limit. */
252 pcibios_resource_to_bus(bridge
, ®ion
, bus
->resource
[2]);
253 if (bus
->resource
[2]->flags
& IORESOURCE_PREFETCH
) {
254 l
= (region
.start
>> 16) & 0xfff0;
255 l
|= region
.end
& 0xfff00000;
256 DBGC((KERN_INFO
" PREFETCH window: %08lx-%08lx\n",
257 region
.start
, region
.end
));
261 DBGC((KERN_INFO
" PREFETCH window: disabled.\n"));
263 pci_write_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, l
);
265 /* Clear out the upper 32 bits of PREF base. */
266 pci_write_config_dword(bridge
, PCI_PREF_BASE_UPPER32
, 0);
268 pci_write_config_word(bridge
, PCI_BRIDGE_CONTROL
, bus
->bridge_ctl
);
271 /* Check whether the bridge supports optional I/O and
272 prefetchable memory ranges. If not, the respective
273 base/limit registers must be read-only and read as 0. */
274 static void __devinit
275 pci_bridge_check_ranges(struct pci_bus
*bus
)
279 struct pci_dev
*bridge
= bus
->self
;
280 struct resource
*b_res
;
282 b_res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
];
283 b_res
[1].flags
|= IORESOURCE_MEM
;
285 pci_read_config_word(bridge
, PCI_IO_BASE
, &io
);
287 pci_write_config_word(bridge
, PCI_IO_BASE
, 0xf0f0);
288 pci_read_config_word(bridge
, PCI_IO_BASE
, &io
);
289 pci_write_config_word(bridge
, PCI_IO_BASE
, 0x0);
292 b_res
[0].flags
|= IORESOURCE_IO
;
293 /* DECchip 21050 pass 2 errata: the bridge may miss an address
294 disconnect boundary by one PCI data phase.
295 Workaround: do not use prefetching on this device. */
296 if (bridge
->vendor
== PCI_VENDOR_ID_DEC
&& bridge
->device
== 0x0001)
298 pci_read_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, &pmem
);
300 pci_write_config_dword(bridge
, PCI_PREF_MEMORY_BASE
,
302 pci_read_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, &pmem
);
303 pci_write_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, 0x0);
306 b_res
[2].flags
|= IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
309 /* Helper function for sizing routines: find first available
310 bus resource of a given type. Note: we intentionally skip
311 the bus resources which have already been assigned (that is,
312 have non-NULL parent resource). */
313 static struct resource
* __devinit
314 find_free_bus_resource(struct pci_bus
*bus
, unsigned long type
)
318 unsigned long type_mask
= IORESOURCE_IO
| IORESOURCE_MEM
|
321 for (i
= 0; i
< PCI_BUS_NUM_RESOURCES
; i
++) {
322 r
= bus
->resource
[i
];
323 if (r
&& (r
->flags
& type_mask
) == type
&& !r
->parent
)
329 /* Sizing the IO windows of the PCI-PCI bridge is trivial,
330 since these windows have 4K granularity and the IO ranges
331 of non-bridge PCI devices are limited to 256 bytes.
332 We must be careful with the ISA aliasing though. */
333 static void __devinit
334 pbus_size_io(struct pci_bus
*bus
)
337 struct resource
*b_res
= find_free_bus_resource(bus
, IORESOURCE_IO
);
338 unsigned long size
= 0, size1
= 0;
343 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
346 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
347 struct resource
*r
= &dev
->resource
[i
];
348 unsigned long r_size
;
350 if (r
->parent
|| !(r
->flags
& IORESOURCE_IO
))
352 r_size
= r
->end
- r
->start
+ 1;
355 /* Might be re-aligned for ISA */
361 /* To be fixed in 2.5: we should have sort of HAVE_ISA
362 flag in the struct pci_bus. */
363 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
364 size
= (size
& 0xff) + ((size
& ~0xffUL
) << 2);
366 size
= ROUND_UP(size
+ size1
, 4096);
371 /* Alignment of the IO window is always 4K */
373 b_res
->end
= b_res
->start
+ size
- 1;
376 /* Calculate the size of the bus and minimal alignment which
377 guarantees that all child resources fit in this size. */
379 pbus_size_mem(struct pci_bus
*bus
, unsigned long mask
, unsigned long type
)
382 unsigned long min_align
, align
, size
;
383 unsigned long aligns
[12]; /* Alignments from 1Mb to 2Gb */
384 int order
, max_order
;
385 struct resource
*b_res
= find_free_bus_resource(bus
, type
);
390 memset(aligns
, 0, sizeof(aligns
));
394 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
397 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
398 struct resource
*r
= &dev
->resource
[i
];
399 unsigned long r_size
;
401 if (r
->parent
|| (r
->flags
& mask
) != type
)
403 r_size
= r
->end
- r
->start
+ 1;
404 /* For bridges size != alignment */
405 align
= (i
< PCI_BRIDGE_RESOURCES
) ? r_size
: r
->start
;
406 order
= __ffs(align
) - 20;
408 printk(KERN_WARNING
"PCI: region %s/%d "
409 "too large: %lx-%lx\n",
410 pci_name(dev
), i
, r
->start
, r
->end
);
417 /* Exclude ranges with size > align from
418 calculation of the alignment. */
420 aligns
[order
] += align
;
421 if (order
> max_order
)
428 for (order
= 0; order
<= max_order
; order
++) {
429 unsigned long align1
= 1UL << (order
+ 20);
433 else if (ROUND_UP(align
+ min_align
, min_align
) < align1
)
434 min_align
= align1
>> 1;
435 align
+= aligns
[order
];
437 size
= ROUND_UP(size
, min_align
);
442 b_res
->start
= min_align
;
443 b_res
->end
= size
+ min_align
- 1;
447 static void __devinit
448 pci_bus_size_cardbus(struct pci_bus
*bus
)
450 struct pci_dev
*bridge
= bus
->self
;
451 struct resource
*b_res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
];
455 * Reserve some resources for CardBus. We reserve
456 * a fixed amount of bus space for CardBus bridges.
458 #if 0 // This is a bug. It will not match PCMCIA device driver. Victor Yu. 01-11-2006
459 b_res
[0].start
= CARDBUS_IO_SIZE
;
460 b_res
[0].end
= b_res
[0].start
+ CARDBUS_IO_SIZE
- 1;
461 b_res
[0].flags
|= IORESOURCE_IO
;
463 b_res
[1].start
= CARDBUS_IO_SIZE
;
464 b_res
[1].end
= b_res
[1].start
+ CARDBUS_IO_SIZE
- 1;
465 b_res
[1].flags
|= IORESOURCE_IO
;
467 b_res
[2].start
= CARDBUS_IO_SIZE
;
468 b_res
[2].end
= b_res
[2].start
+ CARDBUS_IO_SIZE
- 1;
469 b_res
[2].flags
|= IORESOURCE_IO
;
471 b_res
[3].start
= CARDBUS_IO_SIZE
;
472 b_res
[3].end
= b_res
[3].start
+ CARDBUS_IO_SIZE
- 1;
473 b_res
[3].flags
|= IORESOURCE_IO
;
477 * Check whether prefetchable memory is supported
480 pci_read_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, &ctrl
);
481 if (!(ctrl
& PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
)) {
482 ctrl
|= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
;
483 pci_write_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, ctrl
);
484 pci_read_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, &ctrl
);
488 * If we have prefetchable memory support, allocate
489 * two regions. Otherwise, allocate one region of
492 if (ctrl
& PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
) {
493 #if 0 // This is a bug. It will not match PCMCIA device driver. Victor Yu. 01-11-2006
494 b_res
[2].start
= CARDBUS_MEM_SIZE
;
495 b_res
[2].end
= b_res
[2].start
+ CARDBUS_MEM_SIZE
- 1;
496 b_res
[2].flags
|= IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
498 b_res
[3].start
= CARDBUS_MEM_SIZE
;
499 b_res
[3].end
= b_res
[3].start
+ CARDBUS_MEM_SIZE
- 1;
500 b_res
[3].flags
|= IORESOURCE_MEM
;
502 b_res
[0].start
= CARDBUS_MEM_SIZE
;
503 b_res
[0].end
= b_res
[0].start
+ CARDBUS_MEM_SIZE
- 1;
504 b_res
[0].flags
|= IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
506 b_res
[1].start
= CARDBUS_MEM_SIZE
;
507 b_res
[1].end
= b_res
[1].start
+ CARDBUS_MEM_SIZE
- 1;
508 b_res
[1].flags
|= IORESOURCE_MEM
;
511 #if 0 // This is a bug. It will not match PCMCIA device driver. Victor Yu. 01-11-2006
512 b_res
[3].start
= CARDBUS_MEM_SIZE
* 2;
513 b_res
[3].end
= b_res
[3].start
+ CARDBUS_MEM_SIZE
* 2 - 1;
514 b_res
[3].flags
|= IORESOURCE_MEM
;
516 b_res
[1].start
= CARDBUS_MEM_SIZE
* 2;
517 b_res
[1].end
= b_res
[1].start
+ CARDBUS_MEM_SIZE
* 2 - 1;
518 b_res
[1].flags
|= IORESOURCE_MEM
;
524 pci_bus_size_bridges(struct pci_bus
*bus
)
527 unsigned long mask
, prefmask
;
529 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
530 struct pci_bus
*b
= dev
->subordinate
;
534 switch (dev
->class >> 8) {
535 case PCI_CLASS_BRIDGE_CARDBUS
:
536 pci_bus_size_cardbus(b
);
539 case PCI_CLASS_BRIDGE_PCI
:
541 pci_bus_size_bridges(b
);
550 switch (bus
->self
->class >> 8) {
551 case PCI_CLASS_BRIDGE_CARDBUS
:
552 /* don't size cardbuses yet. */
555 case PCI_CLASS_BRIDGE_PCI
:
556 pci_bridge_check_ranges(bus
);
559 /* If the bridge supports prefetchable range, size it
560 separately. If it doesn't, or its prefetchable window
561 has already been allocated by arch code, try
562 non-prefetchable range for both types of PCI memory
564 mask
= IORESOURCE_MEM
;
565 prefmask
= IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
566 if (pbus_size_mem(bus
, prefmask
, prefmask
))
567 mask
= prefmask
; /* Success, size non-prefetch only. */
568 pbus_size_mem(bus
, mask
, IORESOURCE_MEM
);
572 EXPORT_SYMBOL(pci_bus_size_bridges
);
575 pci_bus_assign_resources(struct pci_bus
*bus
)
580 pbus_assign_resources_sorted(bus
);
582 if (bus
->bridge_ctl
& PCI_BRIDGE_CTL_VGA
) {
583 /* Propagate presence of the VGA to upstream bridges */
584 for (b
= bus
; b
->parent
; b
= b
->parent
) {
585 b
->bridge_ctl
|= PCI_BRIDGE_CTL_VGA
;
588 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
589 b
= dev
->subordinate
;
593 pci_bus_assign_resources(b
);
595 switch (dev
->class >> 8) {
596 case PCI_CLASS_BRIDGE_PCI
:
600 case PCI_CLASS_BRIDGE_CARDBUS
:
601 pci_setup_cardbus(b
);
605 printk(KERN_INFO
"PCI: not setting up bridge %s "
606 "for bus %d\n", pci_name(dev
), b
->number
);
611 EXPORT_SYMBOL(pci_bus_assign_resources
);
614 pci_assign_unassigned_resources(void)
616 struct list_head
*ln
;
618 /* Depth first, calculate sizes and alignments of all
619 subordinate buses. */
620 list_for_each(ln
, &pci_root_buses
) {
621 pci_bus_size_bridges(pci_bus_b(ln
));
623 /* Depth last, allocate resources and update the hardware. */
624 list_for_each(ln
, &pci_root_buses
) {
625 pci_bus_assign_resources(pci_bus_b(ln
));
626 pci_enable_bridges(pci_bus_b(ln
));