Merge commit 'refs/merge-requests/1' of git://gitorious.org/linux-on-wince-htc/linux_...
[htc-linux.git] / drivers / pci / setup-bus.c
blobdd58c6a36a515a3d639aff579f7321852d77c52a
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>
28 #include "pci.h"
30 static void pbus_assign_resources_sorted(const struct pci_bus *bus)
32 struct pci_dev *dev;
33 struct resource *res;
34 struct resource_list head, *list, *tmp;
35 int idx;
37 head.next = NULL;
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)
44 continue;
46 /* Don't touch ioapic devices already enabled by firmware */
47 if (class == PCI_CLASS_SYSTEM_PIC) {
48 u16 command;
49 pci_read_config_word(dev, PCI_COMMAND, &command);
50 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
51 continue;
54 pdev_sort_resources(dev, &head);
57 for (list = head.next; list;) {
58 res = list->res;
59 idx = res - &list->dev->resource[0];
60 if (pci_assign_resource(list->dev, idx)) {
61 res->start = 0;
62 res->end = 0;
63 res->flags = 0;
65 tmp = list;
66 list = list->next;
67 kfree(tmp);
71 void pci_setup_cardbus(struct pci_bus *bus)
73 struct pci_dev *bridge = bus->self;
74 struct pci_bus_region region;
76 dev_info(&bridge->dev, "CardBus bridge, secondary bus %04x:%02x\n",
77 pci_domain_nr(bus), bus->number);
79 pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
80 if (bus->resource[0]->flags & IORESOURCE_IO) {
82 * The IO resource is allocated a range twice as large as it
83 * would normally need. This allows us to set both IO regs.
85 dev_info(&bridge->dev, " IO window: %#08lx-%#08lx\n",
86 (unsigned long)region.start,
87 (unsigned long)region.end);
88 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
89 region.start);
90 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
91 region.end);
94 pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
95 if (bus->resource[1]->flags & IORESOURCE_IO) {
96 dev_info(&bridge->dev, " IO window: %#08lx-%#08lx\n",
97 (unsigned long)region.start,
98 (unsigned long)region.end);
99 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
100 region.start);
101 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
102 region.end);
105 pcibios_resource_to_bus(bridge, &region, bus->resource[2]);
106 if (bus->resource[2]->flags & IORESOURCE_MEM) {
107 dev_info(&bridge->dev, " PREFETCH window: %#08lx-%#08lx\n",
108 (unsigned long)region.start,
109 (unsigned long)region.end);
110 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
111 region.start);
112 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
113 region.end);
116 pcibios_resource_to_bus(bridge, &region, bus->resource[3]);
117 if (bus->resource[3]->flags & IORESOURCE_MEM) {
118 dev_info(&bridge->dev, " MEM window: %#08lx-%#08lx\n",
119 (unsigned long)region.start,
120 (unsigned long)region.end);
121 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
122 region.start);
123 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
124 region.end);
127 EXPORT_SYMBOL(pci_setup_cardbus);
129 /* Initialize bridges with base/limit values we have collected.
130 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
131 requires that if there is no I/O ports or memory behind the
132 bridge, corresponding range must be turned off by writing base
133 value greater than limit to the bridge's base/limit registers.
135 Note: care must be taken when updating I/O base/limit registers
136 of bridges which support 32-bit I/O. This update requires two
137 config space writes, so it's quite possible that an I/O window of
138 the bridge will have some undesirable address (e.g. 0) after the
139 first write. Ditto 64-bit prefetchable MMIO. */
140 static void pci_setup_bridge(struct pci_bus *bus)
142 struct pci_dev *bridge = bus->self;
143 struct pci_bus_region region;
144 u32 l, bu, lu, io_upper16;
146 if (pci_is_enabled(bridge))
147 return;
149 dev_info(&bridge->dev, "PCI bridge, secondary bus %04x:%02x\n",
150 pci_domain_nr(bus), bus->number);
152 /* Set up the top and bottom of the PCI I/O segment for this bus. */
153 pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
154 if (bus->resource[0]->flags & IORESOURCE_IO) {
155 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
156 l &= 0xffff0000;
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 dev_info(&bridge->dev, " IO window: %#04lx-%#04lx\n",
162 (unsigned long)region.start,
163 (unsigned long)region.end);
165 else {
166 /* Clear upper 16 bits of I/O base/limit. */
167 io_upper16 = 0;
168 l = 0x00f0;
169 dev_info(&bridge->dev, " IO window: disabled\n");
171 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
172 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
173 /* Update lower 16 bits of I/O base/limit. */
174 pci_write_config_dword(bridge, PCI_IO_BASE, l);
175 /* Update upper 16 bits of I/O base/limit. */
176 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
178 /* Set up the top and bottom of the PCI Memory segment
179 for this bus. */
180 pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
181 if (bus->resource[1]->flags & IORESOURCE_MEM) {
182 l = (region.start >> 16) & 0xfff0;
183 l |= region.end & 0xfff00000;
184 dev_info(&bridge->dev, " MEM window: %#08lx-%#08lx\n",
185 (unsigned long)region.start,
186 (unsigned long)region.end);
188 else {
189 l = 0x0000fff0;
190 dev_info(&bridge->dev, " MEM window: disabled\n");
192 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
194 /* Clear out the upper 32 bits of PREF limit.
195 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
196 disables PREF range, which is ok. */
197 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
199 /* Set up PREF base/limit. */
200 bu = lu = 0;
201 pcibios_resource_to_bus(bridge, &region, bus->resource[2]);
202 if (bus->resource[2]->flags & IORESOURCE_PREFETCH) {
203 int width = 8;
204 l = (region.start >> 16) & 0xfff0;
205 l |= region.end & 0xfff00000;
206 if (bus->resource[2]->flags & IORESOURCE_MEM_64) {
207 bu = upper_32_bits(region.start);
208 lu = upper_32_bits(region.end);
209 width = 16;
211 dev_info(&bridge->dev, " PREFETCH window: %#0*llx-%#0*llx\n",
212 width, (unsigned long long)region.start,
213 width, (unsigned long long)region.end);
215 else {
216 l = 0x0000fff0;
217 dev_info(&bridge->dev, " PREFETCH window: disabled\n");
219 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
221 /* Set the upper 32 bits of PREF base & limit. */
222 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
223 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
225 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
228 /* Check whether the bridge supports optional I/O and
229 prefetchable memory ranges. If not, the respective
230 base/limit registers must be read-only and read as 0. */
231 static void pci_bridge_check_ranges(struct pci_bus *bus)
233 u16 io;
234 u32 pmem;
235 struct pci_dev *bridge = bus->self;
236 struct resource *b_res;
238 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
239 b_res[1].flags |= IORESOURCE_MEM;
241 pci_read_config_word(bridge, PCI_IO_BASE, &io);
242 if (!io) {
243 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
244 pci_read_config_word(bridge, PCI_IO_BASE, &io);
245 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
247 if (io)
248 b_res[0].flags |= IORESOURCE_IO;
249 /* DECchip 21050 pass 2 errata: the bridge may miss an address
250 disconnect boundary by one PCI data phase.
251 Workaround: do not use prefetching on this device. */
252 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
253 return;
254 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
255 if (!pmem) {
256 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
257 0xfff0fff0);
258 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
259 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
261 if (pmem) {
262 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
263 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64)
264 b_res[2].flags |= IORESOURCE_MEM_64;
267 /* double check if bridge does support 64 bit pref */
268 if (b_res[2].flags & IORESOURCE_MEM_64) {
269 u32 mem_base_hi, tmp;
270 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
271 &mem_base_hi);
272 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
273 0xffffffff);
274 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
275 if (!tmp)
276 b_res[2].flags &= ~IORESOURCE_MEM_64;
277 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
278 mem_base_hi);
282 /* Helper function for sizing routines: find first available
283 bus resource of a given type. Note: we intentionally skip
284 the bus resources which have already been assigned (that is,
285 have non-NULL parent resource). */
286 static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
288 int i;
289 struct resource *r;
290 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
291 IORESOURCE_PREFETCH;
293 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
294 r = bus->resource[i];
295 if (r == &ioport_resource || r == &iomem_resource)
296 continue;
297 if (r && (r->flags & type_mask) == type && !r->parent)
298 return r;
300 return NULL;
303 /* Sizing the IO windows of the PCI-PCI bridge is trivial,
304 since these windows have 4K granularity and the IO ranges
305 of non-bridge PCI devices are limited to 256 bytes.
306 We must be careful with the ISA aliasing though. */
307 static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size)
309 struct pci_dev *dev;
310 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
311 unsigned long size = 0, size1 = 0;
313 if (!b_res)
314 return;
316 list_for_each_entry(dev, &bus->devices, bus_list) {
317 int i;
319 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
320 struct resource *r = &dev->resource[i];
321 unsigned long r_size;
323 if (r->parent || !(r->flags & IORESOURCE_IO))
324 continue;
325 r_size = resource_size(r);
327 if (r_size < 0x400)
328 /* Might be re-aligned for ISA */
329 size += r_size;
330 else
331 size1 += r_size;
334 if (size < min_size)
335 size = min_size;
336 /* To be fixed in 2.5: we should have sort of HAVE_ISA
337 flag in the struct pci_bus. */
338 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
339 size = (size & 0xff) + ((size & ~0xffUL) << 2);
340 #endif
341 size = ALIGN(size + size1, 4096);
342 if (!size) {
343 b_res->flags = 0;
344 return;
346 /* Alignment of the IO window is always 4K */
347 b_res->start = 4096;
348 b_res->end = b_res->start + size - 1;
349 b_res->flags |= IORESOURCE_STARTALIGN;
352 /* Calculate the size of the bus and minimal alignment which
353 guarantees that all child resources fit in this size. */
354 static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
355 unsigned long type, resource_size_t min_size)
357 struct pci_dev *dev;
358 resource_size_t min_align, align, size;
359 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
360 int order, max_order;
361 struct resource *b_res = find_free_bus_resource(bus, type);
362 unsigned int mem64_mask = 0;
364 if (!b_res)
365 return 0;
367 memset(aligns, 0, sizeof(aligns));
368 max_order = 0;
369 size = 0;
371 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
372 b_res->flags &= ~IORESOURCE_MEM_64;
374 list_for_each_entry(dev, &bus->devices, bus_list) {
375 int i;
377 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
378 struct resource *r = &dev->resource[i];
379 resource_size_t r_size;
381 if (r->parent || (r->flags & mask) != type)
382 continue;
383 r_size = resource_size(r);
384 /* For bridges size != alignment */
385 align = pci_resource_alignment(dev, r);
386 order = __ffs(align) - 20;
387 if (order > 11) {
388 dev_warn(&dev->dev, "BAR %d bad alignment %llx: "
389 "%pR\n", i, (unsigned long long)align, r);
390 r->flags = 0;
391 continue;
393 size += r_size;
394 if (order < 0)
395 order = 0;
396 /* Exclude ranges with size > align from
397 calculation of the alignment. */
398 if (r_size == align)
399 aligns[order] += align;
400 if (order > max_order)
401 max_order = order;
402 mem64_mask &= r->flags & IORESOURCE_MEM_64;
405 if (size < min_size)
406 size = min_size;
408 align = 0;
409 min_align = 0;
410 for (order = 0; order <= max_order; order++) {
411 resource_size_t align1 = 1;
413 align1 <<= (order + 20);
415 if (!align)
416 min_align = align1;
417 else if (ALIGN(align + min_align, min_align) < align1)
418 min_align = align1 >> 1;
419 align += aligns[order];
421 size = ALIGN(size, min_align);
422 if (!size) {
423 b_res->flags = 0;
424 return 1;
426 b_res->start = min_align;
427 b_res->end = size + min_align - 1;
428 b_res->flags |= IORESOURCE_STARTALIGN;
429 b_res->flags |= mem64_mask;
430 return 1;
433 static void pci_bus_size_cardbus(struct pci_bus *bus)
435 struct pci_dev *bridge = bus->self;
436 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
437 u16 ctrl;
440 * Reserve some resources for CardBus. We reserve
441 * a fixed amount of bus space for CardBus bridges.
443 b_res[0].start = 0;
444 b_res[0].end = pci_cardbus_io_size - 1;
445 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
447 b_res[1].start = 0;
448 b_res[1].end = pci_cardbus_io_size - 1;
449 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
452 * Check whether prefetchable memory is supported
453 * by this bridge.
455 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
456 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
457 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
458 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
459 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
463 * If we have prefetchable memory support, allocate
464 * two regions. Otherwise, allocate one region of
465 * twice the size.
467 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
468 b_res[2].start = 0;
469 b_res[2].end = pci_cardbus_mem_size - 1;
470 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
472 b_res[3].start = 0;
473 b_res[3].end = pci_cardbus_mem_size - 1;
474 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
475 } else {
476 b_res[3].start = 0;
477 b_res[3].end = pci_cardbus_mem_size * 2 - 1;
478 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
482 void __ref pci_bus_size_bridges(struct pci_bus *bus)
484 struct pci_dev *dev;
485 unsigned long mask, prefmask;
486 resource_size_t min_mem_size = 0, min_io_size = 0;
488 list_for_each_entry(dev, &bus->devices, bus_list) {
489 struct pci_bus *b = dev->subordinate;
490 if (!b)
491 continue;
493 switch (dev->class >> 8) {
494 case PCI_CLASS_BRIDGE_CARDBUS:
495 pci_bus_size_cardbus(b);
496 break;
498 case PCI_CLASS_BRIDGE_PCI:
499 default:
500 pci_bus_size_bridges(b);
501 break;
505 /* The root bus? */
506 if (!bus->self)
507 return;
509 switch (bus->self->class >> 8) {
510 case PCI_CLASS_BRIDGE_CARDBUS:
511 /* don't size cardbuses yet. */
512 break;
514 case PCI_CLASS_BRIDGE_PCI:
515 pci_bridge_check_ranges(bus);
516 if (bus->self->is_hotplug_bridge) {
517 min_io_size = pci_hotplug_io_size;
518 min_mem_size = pci_hotplug_mem_size;
520 default:
521 pbus_size_io(bus, min_io_size);
522 /* If the bridge supports prefetchable range, size it
523 separately. If it doesn't, or its prefetchable window
524 has already been allocated by arch code, try
525 non-prefetchable range for both types of PCI memory
526 resources. */
527 mask = IORESOURCE_MEM;
528 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
529 if (pbus_size_mem(bus, prefmask, prefmask, min_mem_size))
530 mask = prefmask; /* Success, size non-prefetch only. */
531 else
532 min_mem_size += min_mem_size;
533 pbus_size_mem(bus, mask, IORESOURCE_MEM, min_mem_size);
534 break;
537 EXPORT_SYMBOL(pci_bus_size_bridges);
539 void __ref pci_bus_assign_resources(const struct pci_bus *bus)
541 struct pci_bus *b;
542 struct pci_dev *dev;
544 pbus_assign_resources_sorted(bus);
546 list_for_each_entry(dev, &bus->devices, bus_list) {
547 b = dev->subordinate;
548 if (!b)
549 continue;
551 pci_bus_assign_resources(b);
553 switch (dev->class >> 8) {
554 case PCI_CLASS_BRIDGE_PCI:
555 pci_setup_bridge(b);
556 break;
558 case PCI_CLASS_BRIDGE_CARDBUS:
559 pci_setup_cardbus(b);
560 break;
562 default:
563 dev_info(&dev->dev, "not setting up bridge for bus "
564 "%04x:%02x\n", pci_domain_nr(b), b->number);
565 break;
569 EXPORT_SYMBOL(pci_bus_assign_resources);
571 static void pci_bus_dump_res(struct pci_bus *bus)
573 int i;
575 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
576 struct resource *res = bus->resource[i];
577 if (!res || !res->end)
578 continue;
580 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %s %pR\n", i,
581 (res->flags & IORESOURCE_IO) ? "io: " :
582 ((res->flags & IORESOURCE_PREFETCH)? "pref mem":"mem:"),
583 res);
587 static void pci_bus_dump_resources(struct pci_bus *bus)
589 struct pci_bus *b;
590 struct pci_dev *dev;
593 pci_bus_dump_res(bus);
595 list_for_each_entry(dev, &bus->devices, bus_list) {
596 b = dev->subordinate;
597 if (!b)
598 continue;
600 pci_bus_dump_resources(b);
604 void __init
605 pci_assign_unassigned_resources(void)
607 struct pci_bus *bus;
609 /* Depth first, calculate sizes and alignments of all
610 subordinate buses. */
611 list_for_each_entry(bus, &pci_root_buses, node) {
612 pci_bus_size_bridges(bus);
614 /* Depth last, allocate resources and update the hardware. */
615 list_for_each_entry(bus, &pci_root_buses, node) {
616 pci_bus_assign_resources(bus);
617 pci_enable_bridges(bus);
620 /* dump the resource on buses */
621 list_for_each_entry(bus, &pci_root_buses, node) {
622 pci_bus_dump_resources(bus);