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 struct resource_list_x {
31 struct resource_list_x *next;
34 resource_size_t start;
39 static void add_to_failed_list(struct resource_list_x *head,
40 struct pci_dev *dev, struct resource *res)
42 struct resource_list_x *list = head;
43 struct resource_list_x *ln = list->next;
44 struct resource_list_x *tmp;
46 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
48 pr_warning("add_to_failed_list: kmalloc() failed!\n");
55 tmp->start = res->start;
57 tmp->flags = res->flags;
61 static void free_failed_list(struct resource_list_x *head)
63 struct resource_list_x *list, *tmp;
65 for (list = head->next; list;) {
74 static void pbus_assign_resources_sorted(const struct pci_bus *bus,
75 struct resource_list_x *fail_head)
79 struct resource_list head, *list, *tmp;
83 list_for_each_entry(dev, &bus->devices, bus_list) {
84 u16 class = dev->class >> 8;
86 /* Don't touch classless devices or host bridges or ioapics. */
87 if (class == PCI_CLASS_NOT_DEFINED ||
88 class == PCI_CLASS_BRIDGE_HOST)
91 /* Don't touch ioapic devices already enabled by firmware */
92 if (class == PCI_CLASS_SYSTEM_PIC) {
94 pci_read_config_word(dev, PCI_COMMAND, &command);
95 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
99 pdev_sort_resources(dev, &head);
102 for (list = head.next; list;) {
104 idx = res - &list->dev->resource[0];
105 if (pci_assign_resource(list->dev, idx)) {
106 if (fail_head && !pci_is_root_bus(list->dev->bus))
107 add_to_failed_list(fail_head, list->dev, res);
118 void pci_setup_cardbus(struct pci_bus *bus)
120 struct pci_dev *bridge = bus->self;
121 struct resource *res;
122 struct pci_bus_region region;
124 dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
125 bus->secondary, bus->subordinate);
127 res = bus->resource[0];
128 pcibios_resource_to_bus(bridge, ®ion, res);
129 if (res->flags & IORESOURCE_IO) {
131 * The IO resource is allocated a range twice as large as it
132 * would normally need. This allows us to set both IO regs.
134 dev_info(&bridge->dev, " bridge window %pR\n", res);
135 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
137 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
141 res = bus->resource[1];
142 pcibios_resource_to_bus(bridge, ®ion, res);
143 if (res->flags & IORESOURCE_IO) {
144 dev_info(&bridge->dev, " bridge window %pR\n", res);
145 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
147 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
151 res = bus->resource[2];
152 pcibios_resource_to_bus(bridge, ®ion, res);
153 if (res->flags & IORESOURCE_MEM) {
154 dev_info(&bridge->dev, " bridge window %pR\n", res);
155 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
157 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
161 res = bus->resource[3];
162 pcibios_resource_to_bus(bridge, ®ion, res);
163 if (res->flags & IORESOURCE_MEM) {
164 dev_info(&bridge->dev, " bridge window %pR\n", res);
165 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
167 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
171 EXPORT_SYMBOL(pci_setup_cardbus);
173 /* Initialize bridges with base/limit values we have collected.
174 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
175 requires that if there is no I/O ports or memory behind the
176 bridge, corresponding range must be turned off by writing base
177 value greater than limit to the bridge's base/limit registers.
179 Note: care must be taken when updating I/O base/limit registers
180 of bridges which support 32-bit I/O. This update requires two
181 config space writes, so it's quite possible that an I/O window of
182 the bridge will have some undesirable address (e.g. 0) after the
183 first write. Ditto 64-bit prefetchable MMIO. */
184 static void pci_setup_bridge_io(struct pci_bus *bus)
186 struct pci_dev *bridge = bus->self;
187 struct resource *res;
188 struct pci_bus_region region;
191 /* Set up the top and bottom of the PCI I/O segment for this bus. */
192 res = bus->resource[0];
193 pcibios_resource_to_bus(bridge, ®ion, res);
194 if (res->flags & IORESOURCE_IO) {
195 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
197 l |= (region.start >> 8) & 0x00f0;
198 l |= region.end & 0xf000;
199 /* Set up upper 16 bits of I/O base/limit. */
200 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
201 dev_info(&bridge->dev, " bridge window %pR\n", res);
203 /* Clear upper 16 bits of I/O base/limit. */
206 dev_info(&bridge->dev, " bridge window [io disabled]\n");
208 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
209 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
210 /* Update lower 16 bits of I/O base/limit. */
211 pci_write_config_dword(bridge, PCI_IO_BASE, l);
212 /* Update upper 16 bits of I/O base/limit. */
213 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
216 static void pci_setup_bridge_mmio(struct pci_bus *bus)
218 struct pci_dev *bridge = bus->self;
219 struct resource *res;
220 struct pci_bus_region region;
223 /* Set up the top and bottom of the PCI Memory segment for this bus. */
224 res = bus->resource[1];
225 pcibios_resource_to_bus(bridge, ®ion, res);
226 if (res->flags & IORESOURCE_MEM) {
227 l = (region.start >> 16) & 0xfff0;
228 l |= region.end & 0xfff00000;
229 dev_info(&bridge->dev, " bridge window %pR\n", res);
232 dev_info(&bridge->dev, " bridge window [mem disabled]\n");
234 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
237 static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
239 struct pci_dev *bridge = bus->self;
240 struct resource *res;
241 struct pci_bus_region region;
244 /* Clear out the upper 32 bits of PREF limit.
245 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
246 disables PREF range, which is ok. */
247 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
249 /* Set up PREF base/limit. */
251 res = bus->resource[2];
252 pcibios_resource_to_bus(bridge, ®ion, res);
253 if (res->flags & IORESOURCE_PREFETCH) {
254 l = (region.start >> 16) & 0xfff0;
255 l |= region.end & 0xfff00000;
256 if (res->flags & IORESOURCE_MEM_64) {
257 bu = upper_32_bits(region.start);
258 lu = upper_32_bits(region.end);
260 dev_info(&bridge->dev, " bridge window %pR\n", res);
263 dev_info(&bridge->dev, " bridge window [mem pref disabled]\n");
265 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
267 /* Set the upper 32 bits of PREF base & limit. */
268 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
269 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
272 static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
274 struct pci_dev *bridge = bus->self;
276 if (pci_is_enabled(bridge))
279 dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
280 bus->secondary, bus->subordinate);
282 if (type & IORESOURCE_IO)
283 pci_setup_bridge_io(bus);
285 if (type & IORESOURCE_MEM)
286 pci_setup_bridge_mmio(bus);
288 if (type & IORESOURCE_PREFETCH)
289 pci_setup_bridge_mmio_pref(bus);
291 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
294 static void pci_setup_bridge(struct pci_bus *bus)
296 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
299 __pci_setup_bridge(bus, type);
302 /* Check whether the bridge supports optional I/O and
303 prefetchable memory ranges. If not, the respective
304 base/limit registers must be read-only and read as 0. */
305 static void pci_bridge_check_ranges(struct pci_bus *bus)
309 struct pci_dev *bridge = bus->self;
310 struct resource *b_res;
312 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
313 b_res[1].flags |= IORESOURCE_MEM;
315 pci_read_config_word(bridge, PCI_IO_BASE, &io);
317 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
318 pci_read_config_word(bridge, PCI_IO_BASE, &io);
319 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
322 b_res[0].flags |= IORESOURCE_IO;
323 /* DECchip 21050 pass 2 errata: the bridge may miss an address
324 disconnect boundary by one PCI data phase.
325 Workaround: do not use prefetching on this device. */
326 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
328 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
330 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
332 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
333 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
336 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
337 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64)
338 b_res[2].flags |= IORESOURCE_MEM_64;
341 /* double check if bridge does support 64 bit pref */
342 if (b_res[2].flags & IORESOURCE_MEM_64) {
343 u32 mem_base_hi, tmp;
344 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
346 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
348 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
350 b_res[2].flags &= ~IORESOURCE_MEM_64;
351 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
356 /* Helper function for sizing routines: find first available
357 bus resource of a given type. Note: we intentionally skip
358 the bus resources which have already been assigned (that is,
359 have non-NULL parent resource). */
360 static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
364 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
367 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
368 r = bus->resource[i];
369 if (r == &ioport_resource || r == &iomem_resource)
371 if (r && (r->flags & type_mask) == type && !r->parent)
377 /* Sizing the IO windows of the PCI-PCI bridge is trivial,
378 since these windows have 4K granularity and the IO ranges
379 of non-bridge PCI devices are limited to 256 bytes.
380 We must be careful with the ISA aliasing though. */
381 static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size)
384 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
385 unsigned long size = 0, size1 = 0, old_size;
390 list_for_each_entry(dev, &bus->devices, bus_list) {
393 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
394 struct resource *r = &dev->resource[i];
395 unsigned long r_size;
397 if (r->parent || !(r->flags & IORESOURCE_IO))
399 r_size = resource_size(r);
402 /* Might be re-aligned for ISA */
410 old_size = resource_size(b_res);
413 /* To be fixed in 2.5: we should have sort of HAVE_ISA
414 flag in the struct pci_bus. */
415 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
416 size = (size & 0xff) + ((size & ~0xffUL) << 2);
418 size = ALIGN(size + size1, 4096);
422 if (b_res->start || b_res->end)
423 dev_info(&bus->self->dev, "disabling bridge window "
424 "%pR to [bus %02x-%02x] (unused)\n", b_res,
425 bus->secondary, bus->subordinate);
429 /* Alignment of the IO window is always 4K */
431 b_res->end = b_res->start + size - 1;
432 b_res->flags |= IORESOURCE_STARTALIGN;
435 /* Calculate the size of the bus and minimal alignment which
436 guarantees that all child resources fit in this size. */
437 static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
438 unsigned long type, resource_size_t min_size)
441 resource_size_t min_align, align, size, old_size;
442 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
443 int order, max_order;
444 struct resource *b_res = find_free_bus_resource(bus, type);
445 unsigned int mem64_mask = 0;
450 memset(aligns, 0, sizeof(aligns));
454 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
455 b_res->flags &= ~IORESOURCE_MEM_64;
457 list_for_each_entry(dev, &bus->devices, bus_list) {
460 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
461 struct resource *r = &dev->resource[i];
462 resource_size_t r_size;
464 if (r->parent || (r->flags & mask) != type)
466 r_size = resource_size(r);
467 /* For bridges size != alignment */
468 align = pci_resource_alignment(dev, r);
469 order = __ffs(align) - 20;
471 dev_warn(&dev->dev, "disabling BAR %d: %pR "
472 "(bad alignment %#llx)\n", i, r,
473 (unsigned long long) align);
480 /* Exclude ranges with size > align from
481 calculation of the alignment. */
483 aligns[order] += align;
484 if (order > max_order)
486 mem64_mask &= r->flags & IORESOURCE_MEM_64;
491 old_size = resource_size(b_res);
499 for (order = 0; order <= max_order; order++) {
500 resource_size_t align1 = 1;
502 align1 <<= (order + 20);
506 else if (ALIGN(align + min_align, min_align) < align1)
507 min_align = align1 >> 1;
508 align += aligns[order];
510 size = ALIGN(size, min_align);
512 if (b_res->start || b_res->end)
513 dev_info(&bus->self->dev, "disabling bridge window "
514 "%pR to [bus %02x-%02x] (unused)\n", b_res,
515 bus->secondary, bus->subordinate);
519 b_res->start = min_align;
520 b_res->end = size + min_align - 1;
521 b_res->flags |= IORESOURCE_STARTALIGN;
522 b_res->flags |= mem64_mask;
526 static void pci_bus_size_cardbus(struct pci_bus *bus)
528 struct pci_dev *bridge = bus->self;
529 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
533 * Reserve some resources for CardBus. We reserve
534 * a fixed amount of bus space for CardBus bridges.
537 b_res[0].end = pci_cardbus_io_size - 1;
538 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
541 b_res[1].end = pci_cardbus_io_size - 1;
542 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
545 * Check whether prefetchable memory is supported
548 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
549 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
550 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
551 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
552 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
556 * If we have prefetchable memory support, allocate
557 * two regions. Otherwise, allocate one region of
560 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
562 b_res[2].end = pci_cardbus_mem_size - 1;
563 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
566 b_res[3].end = pci_cardbus_mem_size - 1;
567 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
570 b_res[3].end = pci_cardbus_mem_size * 2 - 1;
571 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
575 void __ref pci_bus_size_bridges(struct pci_bus *bus)
578 unsigned long mask, prefmask;
579 resource_size_t min_mem_size = 0, min_io_size = 0;
581 list_for_each_entry(dev, &bus->devices, bus_list) {
582 struct pci_bus *b = dev->subordinate;
586 switch (dev->class >> 8) {
587 case PCI_CLASS_BRIDGE_CARDBUS:
588 pci_bus_size_cardbus(b);
591 case PCI_CLASS_BRIDGE_PCI:
593 pci_bus_size_bridges(b);
602 switch (bus->self->class >> 8) {
603 case PCI_CLASS_BRIDGE_CARDBUS:
604 /* don't size cardbuses yet. */
607 case PCI_CLASS_BRIDGE_PCI:
608 pci_bridge_check_ranges(bus);
609 if (bus->self->is_hotplug_bridge) {
610 min_io_size = pci_hotplug_io_size;
611 min_mem_size = pci_hotplug_mem_size;
614 pbus_size_io(bus, min_io_size);
615 /* If the bridge supports prefetchable range, size it
616 separately. If it doesn't, or its prefetchable window
617 has already been allocated by arch code, try
618 non-prefetchable range for both types of PCI memory
620 mask = IORESOURCE_MEM;
621 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
622 if (pbus_size_mem(bus, prefmask, prefmask, min_mem_size))
623 mask = prefmask; /* Success, size non-prefetch only. */
625 min_mem_size += min_mem_size;
626 pbus_size_mem(bus, mask, IORESOURCE_MEM, min_mem_size);
630 EXPORT_SYMBOL(pci_bus_size_bridges);
632 static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
633 struct resource_list_x *fail_head)
638 pbus_assign_resources_sorted(bus, fail_head);
640 list_for_each_entry(dev, &bus->devices, bus_list) {
641 b = dev->subordinate;
645 __pci_bus_assign_resources(b, fail_head);
647 switch (dev->class >> 8) {
648 case PCI_CLASS_BRIDGE_PCI:
652 case PCI_CLASS_BRIDGE_CARDBUS:
653 pci_setup_cardbus(b);
657 dev_info(&dev->dev, "not setting up bridge for bus "
658 "%04x:%02x\n", pci_domain_nr(b), b->number);
664 void __ref pci_bus_assign_resources(const struct pci_bus *bus)
666 __pci_bus_assign_resources(bus, NULL);
668 EXPORT_SYMBOL(pci_bus_assign_resources);
670 static void pci_bridge_release_resources(struct pci_bus *bus,
674 bool changed = false;
677 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
681 for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
683 r = &dev->resource[idx];
684 if ((r->flags & type_mask) != type)
689 * if there are children under that, we should release them
692 release_child_resources(r);
693 if (!release_resource(r)) {
694 dev_printk(KERN_DEBUG, &dev->dev,
695 "resource %d %pR released\n", idx, r);
696 /* keep the old size */
697 r->end = resource_size(r) - 1;
705 /* avoiding touch the one without PREF */
706 if (type & IORESOURCE_PREFETCH)
707 type = IORESOURCE_PREFETCH;
708 __pci_setup_bridge(bus, type);
717 * try to release pci bridge resources that is from leaf bridge,
718 * so we can allocate big new one later
720 static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
722 enum release_type rel_type)
725 bool is_leaf_bridge = true;
727 list_for_each_entry(dev, &bus->devices, bus_list) {
728 struct pci_bus *b = dev->subordinate;
732 is_leaf_bridge = false;
734 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
737 if (rel_type == whole_subtree)
738 pci_bus_release_bridge_resources(b, type,
742 if (pci_is_root_bus(bus))
745 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
748 if ((rel_type == whole_subtree) || is_leaf_bridge)
749 pci_bridge_release_resources(bus, type);
752 static void pci_bus_dump_res(struct pci_bus *bus)
756 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
757 struct resource *res = bus->resource[i];
759 if (!res || !res->end || !res->flags)
762 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
766 static void pci_bus_dump_resources(struct pci_bus *bus)
772 pci_bus_dump_res(bus);
774 list_for_each_entry(dev, &bus->devices, bus_list) {
775 b = dev->subordinate;
779 pci_bus_dump_resources(b);
784 pci_assign_unassigned_resources(void)
788 /* Depth first, calculate sizes and alignments of all
789 subordinate buses. */
790 list_for_each_entry(bus, &pci_root_buses, node) {
791 pci_bus_size_bridges(bus);
793 /* Depth last, allocate resources and update the hardware. */
794 list_for_each_entry(bus, &pci_root_buses, node) {
795 pci_bus_assign_resources(bus);
796 pci_enable_bridges(bus);
799 /* dump the resource on buses */
800 list_for_each_entry(bus, &pci_root_buses, node) {
801 pci_bus_dump_resources(bus);