Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / pci / setup-bus.c
blobcba46e2dac0c2e799c73f72bf7afb0d3b1127986
1 /*
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
31 #if DEBUG_CONFIG
32 #define DBG(x...) printk(x)
33 #else
34 #define DBG(x...)
35 #endif
37 static void pbus_assign_resources_sorted(struct pci_bus *bus)
39 struct pci_dev *dev;
40 struct resource *res;
41 struct resource_list head, *list, *tmp;
42 int idx;
44 head.next = NULL;
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)
51 continue;
53 /* Don't touch ioapic devices already enabled by firmware */
54 if (class == PCI_CLASS_SYSTEM_PIC) {
55 u16 command;
56 pci_read_config_word(dev, PCI_COMMAND, &command);
57 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
58 continue;
61 pdev_sort_resources(dev, &head);
64 for (list = head.next; list;) {
65 res = list->res;
66 idx = res - &list->dev->resource[0];
67 if (pci_assign_resource(list->dev, idx)) {
68 res->start = 0;
69 res->end = 0;
70 res->flags = 0;
72 tmp = list;
73 list = list->next;
74 kfree(tmp);
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, &region, 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(KERN_INFO " IO window: 0x%08lx-0x%08lx\n",
93 (unsigned long)region.start,
94 (unsigned long)region.end);
95 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
96 region.start);
97 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
98 region.end);
101 pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
102 if (bus->resource[1]->flags & IORESOURCE_IO) {
103 printk(KERN_INFO " IO window: 0x%08lx-0x%08lx\n",
104 (unsigned long)region.start,
105 (unsigned long)region.end);
106 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
107 region.start);
108 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
109 region.end);
112 pcibios_resource_to_bus(bridge, &region, bus->resource[2]);
113 if (bus->resource[2]->flags & IORESOURCE_MEM) {
114 printk(KERN_INFO " PREFETCH window: 0x%08lx-0x%08lx\n",
115 (unsigned long)region.start,
116 (unsigned long)region.end);
117 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
118 region.start);
119 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
120 region.end);
123 pcibios_resource_to_bus(bridge, &region, bus->resource[3]);
124 if (bus->resource[3]->flags & IORESOURCE_MEM) {
125 printk(KERN_INFO " MEM window: 0x%08lx-0x%08lx\n",
126 (unsigned long)region.start,
127 (unsigned long)region.end);
128 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
129 region.start);
130 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
131 region.end);
134 EXPORT_SYMBOL(pci_setup_cardbus);
136 /* Initialize bridges with base/limit values we have collected.
137 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
138 requires that if there is no I/O ports or memory behind the
139 bridge, corresponding range must be turned off by writing base
140 value greater than limit to the bridge's base/limit registers.
142 Note: care must be taken when updating I/O base/limit registers
143 of bridges which support 32-bit I/O. This update requires two
144 config space writes, so it's quite possible that an I/O window of
145 the bridge will have some undesirable address (e.g. 0) after the
146 first write. Ditto 64-bit prefetchable MMIO. */
147 static void __devinit
148 pci_setup_bridge(struct pci_bus *bus)
150 struct pci_dev *bridge = bus->self;
151 struct pci_bus_region region;
152 u32 l, bu, lu, io_upper16;
154 DBG(KERN_INFO "PCI: Bridge: %s\n", pci_name(bridge));
156 /* Set up the top and bottom of the PCI I/O segment for this bus. */
157 pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
158 if (bus->resource[0]->flags & IORESOURCE_IO) {
159 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
160 l &= 0xffff0000;
161 l |= (region.start >> 8) & 0x00f0;
162 l |= region.end & 0xf000;
163 /* Set up upper 16 bits of I/O base/limit. */
164 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
165 DBG(KERN_INFO " IO window: %04lx-%04lx\n",
166 (unsigned long)region.start,
167 (unsigned long)region.end);
169 else {
170 /* Clear upper 16 bits of I/O base/limit. */
171 io_upper16 = 0;
172 l = 0x00f0;
173 DBG(KERN_INFO " IO window: disabled.\n");
175 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
176 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
177 /* Update lower 16 bits of I/O base/limit. */
178 pci_write_config_dword(bridge, PCI_IO_BASE, l);
179 /* Update upper 16 bits of I/O base/limit. */
180 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
182 /* Set up the top and bottom of the PCI Memory segment
183 for this bus. */
184 pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
185 if (bus->resource[1]->flags & IORESOURCE_MEM) {
186 l = (region.start >> 16) & 0xfff0;
187 l |= region.end & 0xfff00000;
188 DBG(KERN_INFO " MEM window: 0x%08lx-0x%08lx\n",
189 (unsigned long)region.start,
190 (unsigned long)region.end);
192 else {
193 l = 0x0000fff0;
194 DBG(KERN_INFO " MEM window: disabled.\n");
196 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
198 /* Clear out the upper 32 bits of PREF limit.
199 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
200 disables PREF range, which is ok. */
201 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
203 /* Set up PREF base/limit. */
204 bu = lu = 0;
205 pcibios_resource_to_bus(bridge, &region, bus->resource[2]);
206 if (bus->resource[2]->flags & IORESOURCE_PREFETCH) {
207 l = (region.start >> 16) & 0xfff0;
208 l |= region.end & 0xfff00000;
209 <<<<<<< HEAD:drivers/pci/setup-bus.c
210 #ifdef CONFIG_RESOURCES_64BIT
211 bu = region.start >> 32;
212 lu = region.end >> 32;
213 #endif
214 =======
215 bu = upper_32_bits(region.start);
216 lu = upper_32_bits(region.end);
217 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/pci/setup-bus.c
218 DBG(KERN_INFO " PREFETCH window: 0x%016llx-0x%016llx\n",
219 (unsigned long long)region.start,
220 (unsigned long long)region.end);
222 else {
223 l = 0x0000fff0;
224 DBG(KERN_INFO " PREFETCH window: disabled.\n");
226 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
228 /* Set the upper 32 bits of PREF base & limit. */
229 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
230 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
232 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
235 /* Check whether the bridge supports optional I/O and
236 prefetchable memory ranges. If not, the respective
237 base/limit registers must be read-only and read as 0. */
238 static void pci_bridge_check_ranges(struct pci_bus *bus)
240 u16 io;
241 u32 pmem;
242 struct pci_dev *bridge = bus->self;
243 struct resource *b_res;
245 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
246 b_res[1].flags |= IORESOURCE_MEM;
248 pci_read_config_word(bridge, PCI_IO_BASE, &io);
249 if (!io) {
250 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
251 pci_read_config_word(bridge, PCI_IO_BASE, &io);
252 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
254 if (io)
255 b_res[0].flags |= IORESOURCE_IO;
256 /* DECchip 21050 pass 2 errata: the bridge may miss an address
257 disconnect boundary by one PCI data phase.
258 Workaround: do not use prefetching on this device. */
259 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
260 return;
261 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
262 if (!pmem) {
263 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
264 0xfff0fff0);
265 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
266 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
268 if (pmem)
269 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
272 /* Helper function for sizing routines: find first available
273 bus resource of a given type. Note: we intentionally skip
274 the bus resources which have already been assigned (that is,
275 have non-NULL parent resource). */
276 static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
278 int i;
279 struct resource *r;
280 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
281 IORESOURCE_PREFETCH;
283 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
284 r = bus->resource[i];
285 if (r == &ioport_resource || r == &iomem_resource)
286 continue;
287 if (r && (r->flags & type_mask) == type && !r->parent)
288 return r;
290 return NULL;
293 /* Sizing the IO windows of the PCI-PCI bridge is trivial,
294 since these windows have 4K granularity and the IO ranges
295 of non-bridge PCI devices are limited to 256 bytes.
296 We must be careful with the ISA aliasing though. */
297 static void pbus_size_io(struct pci_bus *bus)
299 struct pci_dev *dev;
300 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
301 unsigned long size = 0, size1 = 0;
303 if (!b_res)
304 return;
306 list_for_each_entry(dev, &bus->devices, bus_list) {
307 int i;
309 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
310 struct resource *r = &dev->resource[i];
311 unsigned long r_size;
313 if (r->parent || !(r->flags & IORESOURCE_IO))
314 continue;
315 r_size = r->end - r->start + 1;
317 if (r_size < 0x400)
318 /* Might be re-aligned for ISA */
319 size += r_size;
320 else
321 size1 += r_size;
324 /* To be fixed in 2.5: we should have sort of HAVE_ISA
325 flag in the struct pci_bus. */
326 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
327 size = (size & 0xff) + ((size & ~0xffUL) << 2);
328 #endif
329 size = ALIGN(size + size1, 4096);
330 if (!size) {
331 b_res->flags = 0;
332 return;
334 /* Alignment of the IO window is always 4K */
335 b_res->start = 4096;
336 b_res->end = b_res->start + size - 1;
339 /* Calculate the size of the bus and minimal alignment which
340 guarantees that all child resources fit in this size. */
341 static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, unsigned long type)
343 struct pci_dev *dev;
344 resource_size_t min_align, align, size;
345 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
346 int order, max_order;
347 struct resource *b_res = find_free_bus_resource(bus, type);
349 if (!b_res)
350 return 0;
352 memset(aligns, 0, sizeof(aligns));
353 max_order = 0;
354 size = 0;
356 list_for_each_entry(dev, &bus->devices, bus_list) {
357 int i;
359 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
360 struct resource *r = &dev->resource[i];
361 resource_size_t r_size;
363 if (r->parent || (r->flags & mask) != type)
364 continue;
365 r_size = r->end - r->start + 1;
366 /* For bridges size != alignment */
367 align = (i < PCI_BRIDGE_RESOURCES) ? r_size : r->start;
368 order = __ffs(align) - 20;
369 if (order > 11) {
370 printk(KERN_WARNING "PCI: region %s/%d "
371 "too large: 0x%016llx-0x%016llx\n",
372 pci_name(dev), i,
373 (unsigned long long)r->start,
374 (unsigned long long)r->end);
375 r->flags = 0;
376 continue;
378 size += r_size;
379 if (order < 0)
380 order = 0;
381 /* Exclude ranges with size > align from
382 calculation of the alignment. */
383 if (r_size == align)
384 aligns[order] += align;
385 if (order > max_order)
386 max_order = order;
390 align = 0;
391 min_align = 0;
392 for (order = 0; order <= max_order; order++) {
393 #ifdef CONFIG_RESOURCES_64BIT
394 resource_size_t align1 = 1ULL << (order + 20);
395 #else
396 resource_size_t align1 = 1U << (order + 20);
397 #endif
398 if (!align)
399 min_align = align1;
400 else if (ALIGN(align + min_align, min_align) < align1)
401 min_align = align1 >> 1;
402 align += aligns[order];
404 size = ALIGN(size, min_align);
405 if (!size) {
406 b_res->flags = 0;
407 return 1;
409 b_res->start = min_align;
410 b_res->end = size + min_align - 1;
411 return 1;
414 static void __devinit
415 pci_bus_size_cardbus(struct pci_bus *bus)
417 struct pci_dev *bridge = bus->self;
418 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
419 u16 ctrl;
422 * Reserve some resources for CardBus. We reserve
423 * a fixed amount of bus space for CardBus bridges.
425 b_res[0].start = pci_cardbus_io_size;
426 b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
427 b_res[0].flags |= IORESOURCE_IO;
429 b_res[1].start = pci_cardbus_io_size;
430 b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
431 b_res[1].flags |= IORESOURCE_IO;
434 * Check whether prefetchable memory is supported
435 * by this bridge.
437 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
438 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
439 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
440 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
441 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
445 * If we have prefetchable memory support, allocate
446 * two regions. Otherwise, allocate one region of
447 * twice the size.
449 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
450 b_res[2].start = pci_cardbus_mem_size;
451 b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
452 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
454 b_res[3].start = pci_cardbus_mem_size;
455 b_res[3].end = b_res[3].start + pci_cardbus_mem_size - 1;
456 b_res[3].flags |= IORESOURCE_MEM;
457 } else {
458 b_res[3].start = pci_cardbus_mem_size * 2;
459 b_res[3].end = b_res[3].start + pci_cardbus_mem_size * 2 - 1;
460 b_res[3].flags |= IORESOURCE_MEM;
464 void __ref pci_bus_size_bridges(struct pci_bus *bus)
466 struct pci_dev *dev;
467 unsigned long mask, prefmask;
469 list_for_each_entry(dev, &bus->devices, bus_list) {
470 struct pci_bus *b = dev->subordinate;
471 if (!b)
472 continue;
474 switch (dev->class >> 8) {
475 case PCI_CLASS_BRIDGE_CARDBUS:
476 pci_bus_size_cardbus(b);
477 break;
479 case PCI_CLASS_BRIDGE_PCI:
480 default:
481 pci_bus_size_bridges(b);
482 break;
486 /* The root bus? */
487 if (!bus->self)
488 return;
490 switch (bus->self->class >> 8) {
491 case PCI_CLASS_BRIDGE_CARDBUS:
492 /* don't size cardbuses yet. */
493 break;
495 case PCI_CLASS_BRIDGE_PCI:
496 /* don't size subtractive decoding (transparent)
497 * PCI-to-PCI bridges */
498 if (bus->self->transparent)
499 break;
500 pci_bridge_check_ranges(bus);
501 /* fall through */
502 default:
503 pbus_size_io(bus);
504 /* If the bridge supports prefetchable range, size it
505 separately. If it doesn't, or its prefetchable window
506 has already been allocated by arch code, try
507 non-prefetchable range for both types of PCI memory
508 resources. */
509 mask = IORESOURCE_MEM;
510 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
511 if (pbus_size_mem(bus, prefmask, prefmask))
512 mask = prefmask; /* Success, size non-prefetch only. */
513 pbus_size_mem(bus, mask, IORESOURCE_MEM);
514 break;
517 EXPORT_SYMBOL(pci_bus_size_bridges);
519 void __ref pci_bus_assign_resources(struct pci_bus *bus)
521 struct pci_bus *b;
522 struct pci_dev *dev;
524 pbus_assign_resources_sorted(bus);
526 list_for_each_entry(dev, &bus->devices, bus_list) {
527 b = dev->subordinate;
528 if (!b)
529 continue;
531 pci_bus_assign_resources(b);
533 switch (dev->class >> 8) {
534 case PCI_CLASS_BRIDGE_PCI:
535 pci_setup_bridge(b);
536 break;
538 case PCI_CLASS_BRIDGE_CARDBUS:
539 pci_setup_cardbus(b);
540 break;
542 default:
543 printk(KERN_INFO "PCI: not setting up bridge %s "
544 "for bus %d\n", pci_name(dev), b->number);
545 break;
549 EXPORT_SYMBOL(pci_bus_assign_resources);
551 void __init
552 pci_assign_unassigned_resources(void)
554 struct pci_bus *bus;
556 /* Depth first, calculate sizes and alignments of all
557 subordinate buses. */
558 list_for_each_entry(bus, &pci_root_buses, node) {
559 pci_bus_size_bridges(bus);
561 /* Depth last, allocate resources and update the hardware. */
562 list_for_each_entry(bus, &pci_root_buses, node) {
563 pci_bus_assign_resources(bus);
564 pci_enable_bridges(bus);