blob: c1bdfb424658ab03bf6e62bd088fe2ed637b80ab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/pci/setup-bus.c
3 *
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)
8 *
9 * Support routines for initializing a PCI subsystem.
10 */
11
12/*
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.
18 */
19
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
29
30#define DEBUG_CONFIG 1
31#if DEBUG_CONFIG
32#define DBG(x...) printk(x)
33#else
34#define DBG(x...)
35#endif
36
37#define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
38
39/*
40 * FIXME: IO should be max 256 bytes. However, since we may
41 * have a P2P bridge below a cardbus bridge, we need 4K.
42 */
43#define CARDBUS_IO_SIZE (4096)
44#define CARDBUS_MEM_SIZE (32*1024*1024)
45
46static void __devinit
47pbus_assign_resources_sorted(struct pci_bus *bus)
48{
49 struct pci_dev *dev;
50 struct resource *res;
51 struct resource_list head, *list, *tmp;
52 int idx;
53
54 bus->bridge_ctl &= ~PCI_BRIDGE_CTL_VGA;
55
56 head.next = NULL;
57 list_for_each_entry(dev, &bus->devices, bus_list) {
58 u16 class = dev->class >> 8;
59
60 /* Don't touch classless devices and host bridges. */
61 if (class == PCI_CLASS_NOT_DEFINED ||
62 class == PCI_CLASS_BRIDGE_HOST)
63 continue;
64
65 if (class == PCI_CLASS_DISPLAY_VGA ||
66 class == PCI_CLASS_NOT_DEFINED_VGA)
67 bus->bridge_ctl |= PCI_BRIDGE_CTL_VGA;
68
69 pdev_sort_resources(dev, &head);
70 }
71
72 for (list = head.next; list;) {
73 res = list->res;
74 idx = res - &list->dev->resource[0];
Rajesh Shah542df5d2005-04-28 00:25:50 -070075 if (pci_assign_resource(list->dev, idx)) {
76 res->start = 0;
77 res->flags = 0;
78 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 tmp = list;
80 list = list->next;
81 kfree(tmp);
82 }
83}
84
85static void __devinit
86pci_setup_cardbus(struct pci_bus *bus)
87{
88 struct pci_dev *bridge = bus->self;
89 struct pci_bus_region region;
90
91 printk("PCI: Bus %d, cardbus bridge: %s\n",
92 bus->number, pci_name(bridge));
93
94 pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
95 if (bus->resource[0]->flags & IORESOURCE_IO) {
96 /*
97 * The IO resource is allocated a range twice as large as it
98 * would normally need. This allows us to set both IO regs.
99 */
100 printk(" IO window: %08lx-%08lx\n",
101 region.start, region.end);
102 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
103 region.start);
104 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
105 region.end);
106 }
107
108 pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
109 if (bus->resource[1]->flags & IORESOURCE_IO) {
110 printk(" IO window: %08lx-%08lx\n",
111 region.start, region.end);
112 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
113 region.start);
114 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
115 region.end);
116 }
117
118 pcibios_resource_to_bus(bridge, &region, bus->resource[2]);
119 if (bus->resource[2]->flags & IORESOURCE_MEM) {
120 printk(" PREFETCH window: %08lx-%08lx\n",
121 region.start, region.end);
122 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
123 region.start);
124 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
125 region.end);
126 }
127
128 pcibios_resource_to_bus(bridge, &region, bus->resource[3]);
129 if (bus->resource[3]->flags & IORESOURCE_MEM) {
130 printk(" MEM window: %08lx-%08lx\n",
131 region.start, region.end);
132 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
133 region.start);
134 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
135 region.end);
136 }
137}
138
139/* Initialize bridges with base/limit values we have collected.
140 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
141 requires that if there is no I/O ports or memory behind the
142 bridge, corresponding range must be turned off by writing base
143 value greater than limit to the bridge's base/limit registers.
144
145 Note: care must be taken when updating I/O base/limit registers
146 of bridges which support 32-bit I/O. This update requires two
147 config space writes, so it's quite possible that an I/O window of
148 the bridge will have some undesirable address (e.g. 0) after the
149 first write. Ditto 64-bit prefetchable MMIO. */
150static void __devinit
151pci_setup_bridge(struct pci_bus *bus)
152{
153 struct pci_dev *bridge = bus->self;
154 struct pci_bus_region region;
155 u32 l, io_upper16;
156
157 DBG(KERN_INFO "PCI: Bridge: %s\n", pci_name(bridge));
158
159 /* Set up the top and bottom of the PCI I/O segment for this bus. */
160 pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
161 if (bus->resource[0]->flags & IORESOURCE_IO) {
162 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
163 l &= 0xffff0000;
164 l |= (region.start >> 8) & 0x00f0;
165 l |= region.end & 0xf000;
166 /* Set up upper 16 bits of I/O base/limit. */
167 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
168 DBG(KERN_INFO " IO window: %04lx-%04lx\n",
169 region.start, region.end);
170 }
171 else {
172 /* Clear upper 16 bits of I/O base/limit. */
173 io_upper16 = 0;
174 l = 0x00f0;
175 DBG(KERN_INFO " IO window: disabled.\n");
176 }
177 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
178 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
179 /* Update lower 16 bits of I/O base/limit. */
180 pci_write_config_dword(bridge, PCI_IO_BASE, l);
181 /* Update upper 16 bits of I/O base/limit. */
182 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
183
184 /* Set up the top and bottom of the PCI Memory segment
185 for this bus. */
186 pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
187 if (bus->resource[1]->flags & IORESOURCE_MEM) {
188 l = (region.start >> 16) & 0xfff0;
189 l |= region.end & 0xfff00000;
190 DBG(KERN_INFO " MEM window: %08lx-%08lx\n",
191 region.start, region.end);
192 }
193 else {
194 l = 0x0000fff0;
195 DBG(KERN_INFO " MEM window: disabled.\n");
196 }
197 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
198
199 /* Clear out the upper 32 bits of PREF limit.
200 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
201 disables PREF range, which is ok. */
202 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
203
204 /* Set up PREF base/limit. */
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 DBG(KERN_INFO " PREFETCH window: %08lx-%08lx\n",
210 region.start, region.end);
211 }
212 else {
213 l = 0x0000fff0;
214 DBG(KERN_INFO " PREFETCH window: disabled.\n");
215 }
216 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
217
218 /* Clear out the upper 32 bits of PREF base. */
219 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 0);
220
221 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
222}
223
224/* Check whether the bridge supports optional I/O and
225 prefetchable memory ranges. If not, the respective
226 base/limit registers must be read-only and read as 0. */
227static void __devinit
228pci_bridge_check_ranges(struct pci_bus *bus)
229{
230 u16 io;
231 u32 pmem;
232 struct pci_dev *bridge = bus->self;
233 struct resource *b_res;
234
235 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
236 b_res[1].flags |= IORESOURCE_MEM;
237
238 pci_read_config_word(bridge, PCI_IO_BASE, &io);
239 if (!io) {
240 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
241 pci_read_config_word(bridge, PCI_IO_BASE, &io);
242 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
243 }
244 if (io)
245 b_res[0].flags |= IORESOURCE_IO;
246 /* DECchip 21050 pass 2 errata: the bridge may miss an address
247 disconnect boundary by one PCI data phase.
248 Workaround: do not use prefetching on this device. */
249 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
250 return;
251 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
252 if (!pmem) {
253 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
254 0xfff0fff0);
255 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
256 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
257 }
258 if (pmem)
259 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
260}
261
262/* Helper function for sizing routines: find first available
263 bus resource of a given type. Note: we intentionally skip
264 the bus resources which have already been assigned (that is,
265 have non-NULL parent resource). */
266static struct resource * __devinit
267find_free_bus_resource(struct pci_bus *bus, unsigned long type)
268{
269 int i;
270 struct resource *r;
271 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
272 IORESOURCE_PREFETCH;
273
274 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
275 r = bus->resource[i];
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400276 if (r == &ioport_resource || r == &iomem_resource)
277 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (r && (r->flags & type_mask) == type && !r->parent)
279 return r;
280 }
281 return NULL;
282}
283
284/* Sizing the IO windows of the PCI-PCI bridge is trivial,
285 since these windows have 4K granularity and the IO ranges
286 of non-bridge PCI devices are limited to 256 bytes.
287 We must be careful with the ISA aliasing though. */
288static void __devinit
289pbus_size_io(struct pci_bus *bus)
290{
291 struct pci_dev *dev;
292 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
293 unsigned long size = 0, size1 = 0;
294
295 if (!b_res)
296 return;
297
298 list_for_each_entry(dev, &bus->devices, bus_list) {
299 int i;
300
301 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
302 struct resource *r = &dev->resource[i];
303 unsigned long r_size;
304
305 if (r->parent || !(r->flags & IORESOURCE_IO))
306 continue;
307 r_size = r->end - r->start + 1;
308
309 if (r_size < 0x400)
310 /* Might be re-aligned for ISA */
311 size += r_size;
312 else
313 size1 += r_size;
314 }
315 }
316/* To be fixed in 2.5: we should have sort of HAVE_ISA
317 flag in the struct pci_bus. */
318#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
319 size = (size & 0xff) + ((size & ~0xffUL) << 2);
320#endif
321 size = ROUND_UP(size + size1, 4096);
322 if (!size) {
323 b_res->flags = 0;
324 return;
325 }
326 /* Alignment of the IO window is always 4K */
327 b_res->start = 4096;
328 b_res->end = b_res->start + size - 1;
329}
330
331/* Calculate the size of the bus and minimal alignment which
332 guarantees that all child resources fit in this size. */
333static int __devinit
334pbus_size_mem(struct pci_bus *bus, unsigned long mask, unsigned long type)
335{
336 struct pci_dev *dev;
337 unsigned long min_align, align, size;
338 unsigned long aligns[12]; /* Alignments from 1Mb to 2Gb */
339 int order, max_order;
340 struct resource *b_res = find_free_bus_resource(bus, type);
341
342 if (!b_res)
343 return 0;
344
345 memset(aligns, 0, sizeof(aligns));
346 max_order = 0;
347 size = 0;
348
349 list_for_each_entry(dev, &bus->devices, bus_list) {
350 int i;
351
352 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
353 struct resource *r = &dev->resource[i];
354 unsigned long r_size;
355
356 if (r->parent || (r->flags & mask) != type)
357 continue;
358 r_size = r->end - r->start + 1;
359 /* For bridges size != alignment */
360 align = (i < PCI_BRIDGE_RESOURCES) ? r_size : r->start;
361 order = __ffs(align) - 20;
362 if (order > 11) {
363 printk(KERN_WARNING "PCI: region %s/%d "
364 "too large: %lx-%lx\n",
365 pci_name(dev), i, r->start, r->end);
366 r->flags = 0;
367 continue;
368 }
369 size += r_size;
370 if (order < 0)
371 order = 0;
372 /* Exclude ranges with size > align from
373 calculation of the alignment. */
374 if (r_size == align)
375 aligns[order] += align;
376 if (order > max_order)
377 max_order = order;
378 }
379 }
380
381 align = 0;
382 min_align = 0;
383 for (order = 0; order <= max_order; order++) {
384 unsigned long align1 = 1UL << (order + 20);
385
386 if (!align)
387 min_align = align1;
388 else if (ROUND_UP(align + min_align, min_align) < align1)
389 min_align = align1 >> 1;
390 align += aligns[order];
391 }
392 size = ROUND_UP(size, min_align);
393 if (!size) {
394 b_res->flags = 0;
395 return 1;
396 }
397 b_res->start = min_align;
398 b_res->end = size + min_align - 1;
399 return 1;
400}
401
402static void __devinit
403pci_bus_size_cardbus(struct pci_bus *bus)
404{
405 struct pci_dev *bridge = bus->self;
406 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
407 u16 ctrl;
408
409 /*
410 * Reserve some resources for CardBus. We reserve
411 * a fixed amount of bus space for CardBus bridges.
412 */
413 b_res[0].start = CARDBUS_IO_SIZE;
414 b_res[0].end = b_res[0].start + CARDBUS_IO_SIZE - 1;
415 b_res[0].flags |= IORESOURCE_IO;
416
417 b_res[1].start = CARDBUS_IO_SIZE;
418 b_res[1].end = b_res[1].start + CARDBUS_IO_SIZE - 1;
419 b_res[1].flags |= IORESOURCE_IO;
420
421 /*
422 * Check whether prefetchable memory is supported
423 * by this bridge.
424 */
425 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
426 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
427 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
428 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
429 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
430 }
431
432 /*
433 * If we have prefetchable memory support, allocate
434 * two regions. Otherwise, allocate one region of
435 * twice the size.
436 */
437 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
438 b_res[2].start = CARDBUS_MEM_SIZE;
439 b_res[2].end = b_res[2].start + CARDBUS_MEM_SIZE - 1;
440 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
441
442 b_res[3].start = CARDBUS_MEM_SIZE;
443 b_res[3].end = b_res[3].start + CARDBUS_MEM_SIZE - 1;
444 b_res[3].flags |= IORESOURCE_MEM;
445 } else {
446 b_res[3].start = CARDBUS_MEM_SIZE * 2;
447 b_res[3].end = b_res[3].start + CARDBUS_MEM_SIZE * 2 - 1;
448 b_res[3].flags |= IORESOURCE_MEM;
449 }
450}
451
452void __devinit
453pci_bus_size_bridges(struct pci_bus *bus)
454{
455 struct pci_dev *dev;
456 unsigned long mask, prefmask;
457
458 list_for_each_entry(dev, &bus->devices, bus_list) {
459 struct pci_bus *b = dev->subordinate;
460 if (!b)
461 continue;
462
463 switch (dev->class >> 8) {
464 case PCI_CLASS_BRIDGE_CARDBUS:
465 pci_bus_size_cardbus(b);
466 break;
467
468 case PCI_CLASS_BRIDGE_PCI:
469 default:
470 pci_bus_size_bridges(b);
471 break;
472 }
473 }
474
475 /* The root bus? */
476 if (!bus->self)
477 return;
478
479 switch (bus->self->class >> 8) {
480 case PCI_CLASS_BRIDGE_CARDBUS:
481 /* don't size cardbuses yet. */
482 break;
483
484 case PCI_CLASS_BRIDGE_PCI:
485 pci_bridge_check_ranges(bus);
486 default:
487 pbus_size_io(bus);
488 /* If the bridge supports prefetchable range, size it
489 separately. If it doesn't, or its prefetchable window
490 has already been allocated by arch code, try
491 non-prefetchable range for both types of PCI memory
492 resources. */
493 mask = IORESOURCE_MEM;
494 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
495 if (pbus_size_mem(bus, prefmask, prefmask))
496 mask = prefmask; /* Success, size non-prefetch only. */
497 pbus_size_mem(bus, mask, IORESOURCE_MEM);
498 break;
499 }
500}
501EXPORT_SYMBOL(pci_bus_size_bridges);
502
503void __devinit
504pci_bus_assign_resources(struct pci_bus *bus)
505{
506 struct pci_bus *b;
507 struct pci_dev *dev;
508
509 pbus_assign_resources_sorted(bus);
510
511 if (bus->bridge_ctl & PCI_BRIDGE_CTL_VGA) {
512 /* Propagate presence of the VGA to upstream bridges */
513 for (b = bus; b->parent; b = b->parent) {
514 b->bridge_ctl |= PCI_BRIDGE_CTL_VGA;
515 }
516 }
517 list_for_each_entry(dev, &bus->devices, bus_list) {
518 b = dev->subordinate;
519 if (!b)
520 continue;
521
522 pci_bus_assign_resources(b);
523
524 switch (dev->class >> 8) {
525 case PCI_CLASS_BRIDGE_PCI:
526 pci_setup_bridge(b);
527 break;
528
529 case PCI_CLASS_BRIDGE_CARDBUS:
530 pci_setup_cardbus(b);
531 break;
532
533 default:
534 printk(KERN_INFO "PCI: not setting up bridge %s "
535 "for bus %d\n", pci_name(dev), b->number);
536 break;
537 }
538 }
539}
540EXPORT_SYMBOL(pci_bus_assign_resources);
541
542void __init
543pci_assign_unassigned_resources(void)
544{
545 struct pci_bus *bus;
546
547 /* Depth first, calculate sizes and alignments of all
548 subordinate buses. */
549 list_for_each_entry(bus, &pci_root_buses, node) {
550 pci_bus_size_bridges(bus);
551 }
552 /* Depth last, allocate resources and update the hardware. */
553 list_for_each_entry(bus, &pci_root_buses, node) {
554 pci_bus_assign_resources(bus);
555 pci_enable_bridges(bus);
556 }
557}