1 // SPDX-License-Identifier: GPL-2.0
3 * Support routines for initializing a PCI subsystem
5 * Extruded from code written by
6 * Dave Rusling (david.rusling@reo.mts.dec.com)
7 * David Mosberger (davidm@cs.arizona.edu)
8 * David Miller (davem@redhat.com)
10 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
11 * PCI-PCI bridges cleanup, sorted resource allocation.
12 * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
13 * Converted to allocation in 3 passes, which gives
14 * tighter packing. Prefetchable range support.
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/errno.h>
22 #include <linux/ioport.h>
23 #include <linux/cache.h>
24 #include <linux/slab.h>
25 #include <linux/acpi.h>
28 unsigned int pci_flags;
29 EXPORT_SYMBOL_GPL(pci_flags);
31 struct pci_dev_resource {
32 struct list_head list;
35 resource_size_t start;
37 resource_size_t add_size;
38 resource_size_t min_align;
42 static void free_list(struct list_head *head)
44 struct pci_dev_resource *dev_res, *tmp;
46 list_for_each_entry_safe(dev_res, tmp, head, list) {
47 list_del(&dev_res->list);
53 * add_to_list() - Add a new resource tracker to the list
54 * @head: Head of the list
55 * @dev: Device to which the resource belongs
56 * @res: Resource to be tracked
57 * @add_size: Additional size to be optionally added to the resource
58 * @min_align: Minimum memory window alignment
60 static int add_to_list(struct list_head *head, struct pci_dev *dev,
61 struct resource *res, resource_size_t add_size,
62 resource_size_t min_align)
64 struct pci_dev_resource *tmp;
66 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
72 tmp->start = res->start;
74 tmp->flags = res->flags;
75 tmp->add_size = add_size;
76 tmp->min_align = min_align;
78 list_add(&tmp->list, head);
83 static void remove_from_list(struct list_head *head, struct resource *res)
85 struct pci_dev_resource *dev_res, *tmp;
87 list_for_each_entry_safe(dev_res, tmp, head, list) {
88 if (dev_res->res == res) {
89 list_del(&dev_res->list);
96 static struct pci_dev_resource *res_to_dev_res(struct list_head *head,
99 struct pci_dev_resource *dev_res;
101 list_for_each_entry(dev_res, head, list) {
102 if (dev_res->res == res)
109 static resource_size_t get_res_add_size(struct list_head *head,
110 struct resource *res)
112 struct pci_dev_resource *dev_res;
114 dev_res = res_to_dev_res(head, res);
115 return dev_res ? dev_res->add_size : 0;
118 static resource_size_t get_res_add_align(struct list_head *head,
119 struct resource *res)
121 struct pci_dev_resource *dev_res;
123 dev_res = res_to_dev_res(head, res);
124 return dev_res ? dev_res->min_align : 0;
127 /* Sort resources by alignment */
128 static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
133 pci_dev_for_each_resource(dev, r, i) {
134 struct pci_dev_resource *dev_res, *tmp;
135 resource_size_t r_align;
138 if (r->flags & IORESOURCE_PCI_FIXED)
141 if (!(r->flags) || r->parent)
144 r_align = pci_resource_alignment(dev, r);
146 pci_warn(dev, "BAR %d: %pR has bogus alignment\n",
151 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
153 panic("%s: kzalloc() failed!\n", __func__);
157 /* Fallback is smallest one or list is empty */
159 list_for_each_entry(dev_res, head, list) {
160 resource_size_t align;
162 align = pci_resource_alignment(dev_res->dev,
165 if (r_align > align) {
170 /* Insert it just before n */
171 list_add_tail(&tmp->list, n);
175 static void __dev_sort_resources(struct pci_dev *dev, struct list_head *head)
177 u16 class = dev->class >> 8;
179 /* Don't touch classless devices or host bridges or IOAPICs */
180 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
183 /* Don't touch IOAPIC devices already enabled by firmware */
184 if (class == PCI_CLASS_SYSTEM_PIC) {
186 pci_read_config_word(dev, PCI_COMMAND, &command);
187 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
191 pdev_sort_resources(dev, head);
194 static inline void reset_resource(struct resource *res)
202 * reassign_resources_sorted() - Satisfy any additional resource requests
204 * @realloc_head: Head of the list tracking requests requiring
205 * additional resources
206 * @head: Head of the list tracking requests with allocated
209 * Walk through each element of the realloc_head and try to procure additional
210 * resources for the element, provided the element is in the head list.
212 static void reassign_resources_sorted(struct list_head *realloc_head,
213 struct list_head *head)
215 struct resource *res;
216 struct pci_dev_resource *add_res, *tmp;
217 struct pci_dev_resource *dev_res;
218 resource_size_t add_size, align;
221 list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
222 bool found_match = false;
225 /* Skip resource that has been reset */
229 /* Skip this resource if not found in head list */
230 list_for_each_entry(dev_res, head, list) {
231 if (dev_res->res == res) {
236 if (!found_match) /* Just skip */
239 idx = res - &add_res->dev->resource[0];
240 add_size = add_res->add_size;
241 align = add_res->min_align;
242 if (!resource_size(res)) {
244 res->end = res->start + add_size - 1;
245 if (pci_assign_resource(add_res->dev, idx))
248 res->flags |= add_res->flags &
249 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
250 if (pci_reassign_resource(add_res->dev, idx,
252 pci_info(add_res->dev, "failed to add %llx res[%d]=%pR\n",
253 (unsigned long long) add_size, idx,
257 list_del(&add_res->list);
263 * assign_requested_resources_sorted() - Satisfy resource requests
265 * @head: Head of the list tracking requests for resources
266 * @fail_head: Head of the list tracking requests that could not be
269 * Satisfy resource requests of each element in the list. Add requests that
270 * could not be satisfied to the failed_list.
272 static void assign_requested_resources_sorted(struct list_head *head,
273 struct list_head *fail_head)
275 struct resource *res;
276 struct pci_dev_resource *dev_res;
279 list_for_each_entry(dev_res, head, list) {
281 idx = res - &dev_res->dev->resource[0];
282 if (resource_size(res) &&
283 pci_assign_resource(dev_res->dev, idx)) {
286 * If the failed resource is a ROM BAR and
287 * it will be enabled later, don't add it
290 if (!((idx == PCI_ROM_RESOURCE) &&
291 (!(res->flags & IORESOURCE_ROM_ENABLE))))
292 add_to_list(fail_head,
302 static unsigned long pci_fail_res_type_mask(struct list_head *fail_head)
304 struct pci_dev_resource *fail_res;
305 unsigned long mask = 0;
307 /* Check failed type */
308 list_for_each_entry(fail_res, fail_head, list)
309 mask |= fail_res->flags;
312 * One pref failed resource will set IORESOURCE_MEM, as we can
313 * allocate pref in non-pref range. Will release all assigned
314 * non-pref sibling resources according to that bit.
316 return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
319 static bool pci_need_to_release(unsigned long mask, struct resource *res)
321 if (res->flags & IORESOURCE_IO)
322 return !!(mask & IORESOURCE_IO);
324 /* Check pref at first */
325 if (res->flags & IORESOURCE_PREFETCH) {
326 if (mask & IORESOURCE_PREFETCH)
328 /* Count pref if its parent is non-pref */
329 else if ((mask & IORESOURCE_MEM) &&
330 !(res->parent->flags & IORESOURCE_PREFETCH))
336 if (res->flags & IORESOURCE_MEM)
337 return !!(mask & IORESOURCE_MEM);
339 return false; /* Should not get here */
342 static void __assign_resources_sorted(struct list_head *head,
343 struct list_head *realloc_head,
344 struct list_head *fail_head)
347 * Should not assign requested resources at first. They could be
348 * adjacent, so later reassign can not reallocate them one by one in
349 * parent resource window.
351 * Try to assign requested + add_size at beginning. If could do that,
352 * could get out early. If could not do that, we still try to assign
353 * requested at first, then try to reassign add_size for some resources.
355 * Separate three resource type checking if we need to release
356 * assigned resource after requested + add_size try.
358 * 1. If IO port assignment fails, will release assigned IO
360 * 2. If pref MMIO assignment fails, release assigned pref
361 * MMIO. If assigned pref MMIO's parent is non-pref MMIO
362 * and non-pref MMIO assignment fails, will release that
363 * assigned pref MMIO.
364 * 3. If non-pref MMIO assignment fails or pref MMIO
365 * assignment fails, will release assigned non-pref MMIO.
367 LIST_HEAD(save_head);
368 LIST_HEAD(local_fail_head);
369 struct pci_dev_resource *save_res;
370 struct pci_dev_resource *dev_res, *tmp_res, *dev_res2;
371 unsigned long fail_type;
372 resource_size_t add_align, align;
374 /* Check if optional add_size is there */
375 if (!realloc_head || list_empty(realloc_head))
376 goto requested_and_reassign;
378 /* Save original start, end, flags etc at first */
379 list_for_each_entry(dev_res, head, list) {
380 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
381 free_list(&save_head);
382 goto requested_and_reassign;
386 /* Update res in head list with add_size in realloc_head list */
387 list_for_each_entry_safe(dev_res, tmp_res, head, list) {
388 dev_res->res->end += get_res_add_size(realloc_head,
392 * There are two kinds of additional resources in the list:
393 * 1. bridge resource -- IORESOURCE_STARTALIGN
394 * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN
395 * Here just fix the additional alignment for bridge
397 if (!(dev_res->res->flags & IORESOURCE_STARTALIGN))
400 add_align = get_res_add_align(realloc_head, dev_res->res);
403 * The "head" list is sorted by alignment so resources with
404 * bigger alignment will be assigned first. After we
405 * change the alignment of a dev_res in "head" list, we
406 * need to reorder the list by alignment to make it
409 if (add_align > dev_res->res->start) {
410 resource_size_t r_size = resource_size(dev_res->res);
412 dev_res->res->start = add_align;
413 dev_res->res->end = add_align + r_size - 1;
415 list_for_each_entry(dev_res2, head, list) {
416 align = pci_resource_alignment(dev_res2->dev,
418 if (add_align > align) {
419 list_move_tail(&dev_res->list,
428 /* Try updated head list with add_size added */
429 assign_requested_resources_sorted(head, &local_fail_head);
431 /* All assigned with add_size? */
432 if (list_empty(&local_fail_head)) {
433 /* Remove head list from realloc_head list */
434 list_for_each_entry(dev_res, head, list)
435 remove_from_list(realloc_head, dev_res->res);
436 free_list(&save_head);
441 /* Check failed type */
442 fail_type = pci_fail_res_type_mask(&local_fail_head);
443 /* Remove not need to be released assigned res from head list etc */
444 list_for_each_entry_safe(dev_res, tmp_res, head, list)
445 if (dev_res->res->parent &&
446 !pci_need_to_release(fail_type, dev_res->res)) {
447 /* Remove it from realloc_head list */
448 remove_from_list(realloc_head, dev_res->res);
449 remove_from_list(&save_head, dev_res->res);
450 list_del(&dev_res->list);
454 free_list(&local_fail_head);
455 /* Release assigned resource */
456 list_for_each_entry(dev_res, head, list)
457 if (dev_res->res->parent)
458 release_resource(dev_res->res);
459 /* Restore start/end/flags from saved list */
460 list_for_each_entry(save_res, &save_head, list) {
461 struct resource *res = save_res->res;
463 res->start = save_res->start;
464 res->end = save_res->end;
465 res->flags = save_res->flags;
467 free_list(&save_head);
469 requested_and_reassign:
470 /* Satisfy the must-have resource requests */
471 assign_requested_resources_sorted(head, fail_head);
473 /* Try to satisfy any additional optional resource requests */
475 reassign_resources_sorted(realloc_head, head);
479 static void pdev_assign_resources_sorted(struct pci_dev *dev,
480 struct list_head *add_head,
481 struct list_head *fail_head)
485 __dev_sort_resources(dev, &head);
486 __assign_resources_sorted(&head, add_head, fail_head);
490 static void pbus_assign_resources_sorted(const struct pci_bus *bus,
491 struct list_head *realloc_head,
492 struct list_head *fail_head)
497 list_for_each_entry(dev, &bus->devices, bus_list)
498 __dev_sort_resources(dev, &head);
500 __assign_resources_sorted(&head, realloc_head, fail_head);
503 void pci_setup_cardbus(struct pci_bus *bus)
505 struct pci_dev *bridge = bus->self;
506 struct resource *res;
507 struct pci_bus_region region;
509 pci_info(bridge, "CardBus bridge to %pR\n",
512 res = bus->resource[0];
513 pcibios_resource_to_bus(bridge->bus, ®ion, res);
514 if (res->flags & IORESOURCE_IO) {
516 * The IO resource is allocated a range twice as large as it
517 * would normally need. This allows us to set both IO regs.
519 pci_info(bridge, " bridge window %pR\n", res);
520 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
522 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
526 res = bus->resource[1];
527 pcibios_resource_to_bus(bridge->bus, ®ion, res);
528 if (res->flags & IORESOURCE_IO) {
529 pci_info(bridge, " bridge window %pR\n", res);
530 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
532 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
536 res = bus->resource[2];
537 pcibios_resource_to_bus(bridge->bus, ®ion, res);
538 if (res->flags & IORESOURCE_MEM) {
539 pci_info(bridge, " bridge window %pR\n", res);
540 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
542 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
546 res = bus->resource[3];
547 pcibios_resource_to_bus(bridge->bus, ®ion, res);
548 if (res->flags & IORESOURCE_MEM) {
549 pci_info(bridge, " bridge window %pR\n", res);
550 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
552 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
556 EXPORT_SYMBOL(pci_setup_cardbus);
559 * Initialize bridges with base/limit values we have collected. PCI-to-PCI
560 * Bridge Architecture Specification rev. 1.1 (1998) requires that if there
561 * are no I/O ports or memory behind the bridge, the corresponding range
562 * must be turned off by writing base value greater than limit to the
563 * bridge's base/limit registers.
565 * Note: care must be taken when updating I/O base/limit registers of
566 * bridges which support 32-bit I/O. This update requires two config space
567 * writes, so it's quite possible that an I/O window of the bridge will
568 * have some undesirable address (e.g. 0) after the first write. Ditto
569 * 64-bit prefetchable MMIO.
571 static void pci_setup_bridge_io(struct pci_dev *bridge)
573 struct resource *res;
574 struct pci_bus_region region;
575 unsigned long io_mask;
576 u8 io_base_lo, io_limit_lo;
580 io_mask = PCI_IO_RANGE_MASK;
581 if (bridge->io_window_1k)
582 io_mask = PCI_IO_1K_RANGE_MASK;
584 /* Set up the top and bottom of the PCI I/O segment for this bus */
585 res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
586 pcibios_resource_to_bus(bridge->bus, ®ion, res);
587 if (res->flags & IORESOURCE_IO) {
588 pci_read_config_word(bridge, PCI_IO_BASE, &l);
589 io_base_lo = (region.start >> 8) & io_mask;
590 io_limit_lo = (region.end >> 8) & io_mask;
591 l = ((u16) io_limit_lo << 8) | io_base_lo;
592 /* Set up upper 16 bits of I/O base/limit */
593 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
594 pci_info(bridge, " bridge window %pR\n", res);
596 /* Clear upper 16 bits of I/O base/limit */
600 /* Temporarily disable the I/O range before updating PCI_IO_BASE */
601 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
602 /* Update lower 16 bits of I/O base/limit */
603 pci_write_config_word(bridge, PCI_IO_BASE, l);
604 /* Update upper 16 bits of I/O base/limit */
605 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
608 static void pci_setup_bridge_mmio(struct pci_dev *bridge)
610 struct resource *res;
611 struct pci_bus_region region;
614 /* Set up the top and bottom of the PCI Memory segment for this bus */
615 res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
616 pcibios_resource_to_bus(bridge->bus, ®ion, res);
617 if (res->flags & IORESOURCE_MEM) {
618 l = (region.start >> 16) & 0xfff0;
619 l |= region.end & 0xfff00000;
620 pci_info(bridge, " bridge window %pR\n", res);
624 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
627 static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge)
629 struct resource *res;
630 struct pci_bus_region region;
634 * Clear out the upper 32 bits of PREF limit. If
635 * PCI_PREF_BASE_UPPER32 was non-zero, this temporarily disables
636 * PREF range, which is ok.
638 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
640 /* Set up PREF base/limit */
642 res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
643 pcibios_resource_to_bus(bridge->bus, ®ion, res);
644 if (res->flags & IORESOURCE_PREFETCH) {
645 l = (region.start >> 16) & 0xfff0;
646 l |= region.end & 0xfff00000;
647 if (res->flags & IORESOURCE_MEM_64) {
648 bu = upper_32_bits(region.start);
649 lu = upper_32_bits(region.end);
651 pci_info(bridge, " bridge window %pR\n", res);
655 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
657 /* Set the upper 32 bits of PREF base & limit */
658 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
659 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
662 static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
664 struct pci_dev *bridge = bus->self;
666 pci_info(bridge, "PCI bridge to %pR\n",
669 if (type & IORESOURCE_IO)
670 pci_setup_bridge_io(bridge);
672 if (type & IORESOURCE_MEM)
673 pci_setup_bridge_mmio(bridge);
675 if (type & IORESOURCE_PREFETCH)
676 pci_setup_bridge_mmio_pref(bridge);
678 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
681 void __weak pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
685 void pci_setup_bridge(struct pci_bus *bus)
687 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
690 pcibios_setup_bridge(bus, type);
691 __pci_setup_bridge(bus, type);
695 int pci_claim_bridge_resource(struct pci_dev *bridge, int i)
697 if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END)
700 if (pci_claim_resource(bridge, i) == 0)
701 return 0; /* Claimed the window */
703 if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
706 if (!pci_bus_clip_resource(bridge, i))
707 return -EINVAL; /* Clipping didn't change anything */
710 case PCI_BRIDGE_IO_WINDOW:
711 pci_setup_bridge_io(bridge);
713 case PCI_BRIDGE_MEM_WINDOW:
714 pci_setup_bridge_mmio(bridge);
716 case PCI_BRIDGE_PREF_MEM_WINDOW:
717 pci_setup_bridge_mmio_pref(bridge);
723 if (pci_claim_resource(bridge, i) == 0)
724 return 0; /* Claimed a smaller window */
730 * Check whether the bridge supports optional I/O and prefetchable memory
731 * ranges. If not, the respective base/limit registers must be read-only
734 static void pci_bridge_check_ranges(struct pci_bus *bus)
736 struct pci_dev *bridge = bus->self;
737 struct resource *b_res;
739 b_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
740 b_res->flags |= IORESOURCE_MEM;
742 if (bridge->io_window) {
743 b_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
744 b_res->flags |= IORESOURCE_IO;
747 if (bridge->pref_window) {
748 b_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
749 b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
750 if (bridge->pref_64_window) {
751 b_res->flags |= IORESOURCE_MEM_64 |
752 PCI_PREF_RANGE_TYPE_64;
758 * Helper function for sizing routines. Assigned resources have non-NULL
761 * Return first unassigned resource of the correct type. If there is none,
762 * return first assigned resource of the correct type. If none of the
763 * above, return NULL.
765 * Returning an assigned resource of the correct type allows the caller to
766 * distinguish between already assigned and no resource of the correct type.
768 static struct resource *find_bus_resource_of_type(struct pci_bus *bus,
769 unsigned long type_mask,
772 struct resource *r, *r_assigned = NULL;
774 pci_bus_for_each_resource(bus, r) {
775 if (r == &ioport_resource || r == &iomem_resource)
777 if (r && (r->flags & type_mask) == type && !r->parent)
779 if (r && (r->flags & type_mask) == type && !r_assigned)
785 static resource_size_t calculate_iosize(resource_size_t size,
786 resource_size_t min_size,
787 resource_size_t size1,
788 resource_size_t add_size,
789 resource_size_t children_add_size,
790 resource_size_t old_size,
791 resource_size_t align)
798 * To be fixed in 2.5: we should have sort of HAVE_ISA flag in the
801 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
802 size = (size & 0xff) + ((size & ~0xffUL) << 2);
808 size = ALIGN(max(size, add_size) + children_add_size, align);
812 static resource_size_t calculate_memsize(resource_size_t size,
813 resource_size_t min_size,
814 resource_size_t add_size,
815 resource_size_t children_add_size,
816 resource_size_t old_size,
817 resource_size_t align)
826 size = ALIGN(max(size, add_size) + children_add_size, align);
830 resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
836 #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */
837 #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */
838 #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */
840 static resource_size_t window_alignment(struct pci_bus *bus, unsigned long type)
842 resource_size_t align = 1, arch_align;
844 if (type & IORESOURCE_MEM)
845 align = PCI_P2P_DEFAULT_MEM_ALIGN;
846 else if (type & IORESOURCE_IO) {
848 * Per spec, I/O windows are 4K-aligned, but some bridges have
849 * an extension to support 1K alignment.
851 if (bus->self && bus->self->io_window_1k)
852 align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
854 align = PCI_P2P_DEFAULT_IO_ALIGN;
857 arch_align = pcibios_window_alignment(bus, type);
858 return max(align, arch_align);
862 * pbus_size_io() - Size the I/O window of a given bus
865 * @min_size: The minimum I/O window that must be allocated
866 * @add_size: Additional optional I/O window
867 * @realloc_head: Track the additional I/O window on this list
869 * Sizing the I/O windows of the PCI-PCI bridge is trivial, since these
870 * windows have 1K or 4K granularity and the I/O ranges of non-bridge PCI
871 * devices are limited to 256 bytes. We must be careful with the ISA
874 static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
875 resource_size_t add_size,
876 struct list_head *realloc_head)
879 struct resource *b_res = find_bus_resource_of_type(bus, IORESOURCE_IO,
881 resource_size_t size = 0, size0 = 0, size1 = 0;
882 resource_size_t children_add_size = 0;
883 resource_size_t min_align, align;
888 /* If resource is already assigned, nothing more to do */
892 min_align = window_alignment(bus, IORESOURCE_IO);
893 list_for_each_entry(dev, &bus->devices, bus_list) {
896 pci_dev_for_each_resource(dev, r) {
897 unsigned long r_size;
899 if (r->parent || !(r->flags & IORESOURCE_IO))
901 r_size = resource_size(r);
904 /* Might be re-aligned for ISA */
909 align = pci_resource_alignment(dev, r);
910 if (align > min_align)
914 children_add_size += get_res_add_size(realloc_head, r);
918 size0 = calculate_iosize(size, min_size, size1, 0, 0,
919 resource_size(b_res), min_align);
920 size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
921 calculate_iosize(size, min_size, size1, add_size, children_add_size,
922 resource_size(b_res), min_align);
923 if (!size0 && !size1) {
924 if (bus->self && (b_res->start || b_res->end))
925 pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
926 b_res, &bus->busn_res);
931 b_res->start = min_align;
932 b_res->end = b_res->start + size0 - 1;
933 b_res->flags |= IORESOURCE_STARTALIGN;
934 if (bus->self && size1 > size0 && realloc_head) {
935 add_to_list(realloc_head, bus->self, b_res, size1-size0,
937 pci_info(bus->self, "bridge window %pR to %pR add_size %llx\n",
938 b_res, &bus->busn_res,
939 (unsigned long long) size1 - size0);
943 static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
946 resource_size_t align = 0;
947 resource_size_t min_align = 0;
950 for (order = 0; order <= max_order; order++) {
951 resource_size_t align1 = 1;
953 align1 <<= (order + 20);
957 else if (ALIGN(align + min_align, min_align) < align1)
958 min_align = align1 >> 1;
959 align += aligns[order];
966 * pbus_size_mem() - Size the memory window of a given bus
969 * @mask: Mask the resource flag, then compare it with type
970 * @type: The type of free resource from bridge
971 * @type2: Second match type
972 * @type3: Third match type
973 * @min_size: The minimum memory window that must be allocated
974 * @add_size: Additional optional memory window
975 * @realloc_head: Track the additional memory window on this list
977 * Calculate the size of the bus and minimal alignment which guarantees
978 * that all child resources fit in this size.
980 * Return -ENOSPC if there's no available bus resource of the desired
981 * type. Otherwise, set the bus resource start/end to indicate the
982 * required size, add things to realloc_head (if supplied), and return 0.
984 static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
985 unsigned long type, unsigned long type2,
986 unsigned long type3, resource_size_t min_size,
987 resource_size_t add_size,
988 struct list_head *realloc_head)
991 resource_size_t min_align, align, size, size0, size1;
992 resource_size_t aligns[24]; /* Alignments from 1MB to 8TB */
993 int order, max_order;
994 struct resource *b_res = find_bus_resource_of_type(bus,
995 mask | IORESOURCE_PREFETCH, type);
996 resource_size_t children_add_size = 0;
997 resource_size_t children_add_align = 0;
998 resource_size_t add_align = 0;
1003 /* If resource is already assigned, nothing more to do */
1007 memset(aligns, 0, sizeof(aligns));
1011 list_for_each_entry(dev, &bus->devices, bus_list) {
1015 pci_dev_for_each_resource(dev, r, i) {
1016 resource_size_t r_size;
1018 if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) ||
1019 ((r->flags & mask) != type &&
1020 (r->flags & mask) != type2 &&
1021 (r->flags & mask) != type3))
1023 r_size = resource_size(r);
1024 #ifdef CONFIG_PCI_IOV
1025 /* Put SRIOV requested res to the optional list */
1026 if (realloc_head && i >= PCI_IOV_RESOURCES &&
1027 i <= PCI_IOV_RESOURCE_END) {
1028 add_align = max(pci_resource_alignment(dev, r), add_align);
1029 r->end = r->start - 1;
1030 add_to_list(realloc_head, dev, r, r_size, 0 /* Don't care */);
1031 children_add_size += r_size;
1036 * aligns[0] is for 1MB (since bridge memory
1037 * windows are always at least 1MB aligned), so
1038 * keep "order" from being negative for smaller
1041 align = pci_resource_alignment(dev, r);
1042 order = __ffs(align) - 20;
1045 if (order >= ARRAY_SIZE(aligns)) {
1046 pci_warn(dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
1047 i, r, (unsigned long long) align);
1051 size += max(r_size, align);
1053 * Exclude ranges with size > align from calculation of
1056 if (r_size <= align)
1057 aligns[order] += align;
1058 if (order > max_order)
1062 children_add_size += get_res_add_size(realloc_head, r);
1063 children_add_align = get_res_add_align(realloc_head, r);
1064 add_align = max(add_align, children_add_align);
1069 min_align = calculate_mem_align(aligns, max_order);
1070 min_align = max(min_align, window_alignment(bus, b_res->flags));
1071 size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), min_align);
1072 add_align = max(min_align, add_align);
1073 size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
1074 calculate_memsize(size, min_size, add_size, children_add_size,
1075 resource_size(b_res), add_align);
1076 if (!size0 && !size1) {
1077 if (bus->self && (b_res->start || b_res->end))
1078 pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
1079 b_res, &bus->busn_res);
1083 b_res->start = min_align;
1084 b_res->end = size0 + min_align - 1;
1085 b_res->flags |= IORESOURCE_STARTALIGN;
1086 if (bus->self && size1 > size0 && realloc_head) {
1087 add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align);
1088 pci_info(bus->self, "bridge window %pR to %pR add_size %llx add_align %llx\n",
1089 b_res, &bus->busn_res,
1090 (unsigned long long) (size1 - size0),
1091 (unsigned long long) add_align);
1096 unsigned long pci_cardbus_resource_alignment(struct resource *res)
1098 if (res->flags & IORESOURCE_IO)
1099 return pci_cardbus_io_size;
1100 if (res->flags & IORESOURCE_MEM)
1101 return pci_cardbus_mem_size;
1105 static void pci_bus_size_cardbus(struct pci_bus *bus,
1106 struct list_head *realloc_head)
1108 struct pci_dev *bridge = bus->self;
1109 struct resource *b_res;
1110 resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
1113 b_res = &bridge->resource[PCI_CB_BRIDGE_IO_0_WINDOW];
1115 goto handle_b_res_1;
1117 * Reserve some resources for CardBus. We reserve a fixed amount
1118 * of bus space for CardBus bridges.
1120 b_res->start = pci_cardbus_io_size;
1121 b_res->end = b_res->start + pci_cardbus_io_size - 1;
1122 b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
1124 b_res->end -= pci_cardbus_io_size;
1125 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
1126 pci_cardbus_io_size);
1130 b_res = &bridge->resource[PCI_CB_BRIDGE_IO_1_WINDOW];
1132 goto handle_b_res_2;
1133 b_res->start = pci_cardbus_io_size;
1134 b_res->end = b_res->start + pci_cardbus_io_size - 1;
1135 b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
1137 b_res->end -= pci_cardbus_io_size;
1138 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
1139 pci_cardbus_io_size);
1143 /* MEM1 must not be pref MMIO */
1144 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1145 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
1146 ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
1147 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1148 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1151 /* Check whether prefetchable memory is supported by this bridge. */
1152 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1153 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
1154 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
1155 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1156 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1159 b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_0_WINDOW];
1161 goto handle_b_res_3;
1163 * If we have prefetchable memory support, allocate two regions.
1164 * Otherwise, allocate one region of twice the size.
1166 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
1167 b_res->start = pci_cardbus_mem_size;
1168 b_res->end = b_res->start + pci_cardbus_mem_size - 1;
1169 b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
1170 IORESOURCE_STARTALIGN;
1172 b_res->end -= pci_cardbus_mem_size;
1173 add_to_list(realloc_head, bridge, b_res,
1174 pci_cardbus_mem_size, pci_cardbus_mem_size);
1177 /* Reduce that to half */
1178 b_res_3_size = pci_cardbus_mem_size;
1182 b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_1_WINDOW];
1185 b_res->start = pci_cardbus_mem_size;
1186 b_res->end = b_res->start + b_res_3_size - 1;
1187 b_res->flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
1189 b_res->end -= b_res_3_size;
1190 add_to_list(realloc_head, bridge, b_res, b_res_3_size,
1191 pci_cardbus_mem_size);
1198 void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
1200 struct pci_dev *dev;
1201 unsigned long mask, prefmask, type2 = 0, type3 = 0;
1202 resource_size_t additional_io_size = 0, additional_mmio_size = 0,
1203 additional_mmio_pref_size = 0;
1204 struct resource *pref;
1205 struct pci_host_bridge *host;
1208 list_for_each_entry(dev, &bus->devices, bus_list) {
1209 struct pci_bus *b = dev->subordinate;
1213 switch (dev->hdr_type) {
1214 case PCI_HEADER_TYPE_CARDBUS:
1215 pci_bus_size_cardbus(b, realloc_head);
1218 case PCI_HEADER_TYPE_BRIDGE:
1220 __pci_bus_size_bridges(b, realloc_head);
1226 if (pci_is_root_bus(bus)) {
1227 host = to_pci_host_bridge(bus->bridge);
1228 if (!host->size_windows)
1230 pci_bus_for_each_resource(bus, pref)
1231 if (pref && (pref->flags & IORESOURCE_PREFETCH))
1233 hdr_type = -1; /* Intentionally invalid - not a PCI device. */
1235 pref = &bus->self->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
1236 hdr_type = bus->self->hdr_type;
1240 case PCI_HEADER_TYPE_CARDBUS:
1241 /* Don't size CardBuses yet */
1244 case PCI_HEADER_TYPE_BRIDGE:
1245 pci_bridge_check_ranges(bus);
1246 if (bus->self->is_hotplug_bridge) {
1247 additional_io_size = pci_hotplug_io_size;
1248 additional_mmio_size = pci_hotplug_mmio_size;
1249 additional_mmio_pref_size = pci_hotplug_mmio_pref_size;
1253 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
1254 additional_io_size, realloc_head);
1257 * If there's a 64-bit prefetchable MMIO window, compute
1258 * the size required to put all 64-bit prefetchable
1261 mask = IORESOURCE_MEM;
1262 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
1263 if (pref && (pref->flags & IORESOURCE_MEM_64)) {
1264 prefmask |= IORESOURCE_MEM_64;
1265 ret = pbus_size_mem(bus, prefmask, prefmask,
1267 realloc_head ? 0 : additional_mmio_pref_size,
1268 additional_mmio_pref_size, realloc_head);
1271 * If successful, all non-prefetchable resources
1272 * and any 32-bit prefetchable resources will go in
1273 * the non-prefetchable window.
1277 type2 = prefmask & ~IORESOURCE_MEM_64;
1278 type3 = prefmask & ~IORESOURCE_PREFETCH;
1283 * If there is no 64-bit prefetchable window, compute the
1284 * size required to put all prefetchable resources in the
1285 * 32-bit prefetchable window (if there is one).
1288 prefmask &= ~IORESOURCE_MEM_64;
1289 ret = pbus_size_mem(bus, prefmask, prefmask,
1291 realloc_head ? 0 : additional_mmio_pref_size,
1292 additional_mmio_pref_size, realloc_head);
1295 * If successful, only non-prefetchable resources
1296 * will go in the non-prefetchable window.
1301 additional_mmio_size += additional_mmio_pref_size;
1303 type2 = type3 = IORESOURCE_MEM;
1307 * Compute the size required to put everything else in the
1308 * non-prefetchable window. This includes:
1310 * - all non-prefetchable resources
1311 * - 32-bit prefetchable resources if there's a 64-bit
1312 * prefetchable window or no prefetchable window at all
1313 * - 64-bit prefetchable resources if there's no prefetchable
1316 * Note that the strategy in __pci_assign_resource() must match
1317 * that used here. Specifically, we cannot put a 32-bit
1318 * prefetchable resource in a 64-bit prefetchable window.
1320 pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
1321 realloc_head ? 0 : additional_mmio_size,
1322 additional_mmio_size, realloc_head);
1327 void pci_bus_size_bridges(struct pci_bus *bus)
1329 __pci_bus_size_bridges(bus, NULL);
1331 EXPORT_SYMBOL(pci_bus_size_bridges);
1333 static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r)
1335 struct resource *parent_r;
1336 unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM |
1337 IORESOURCE_PREFETCH;
1339 pci_bus_for_each_resource(b, parent_r) {
1343 if ((r->flags & mask) == (parent_r->flags & mask) &&
1344 resource_contains(parent_r, r))
1345 request_resource(parent_r, r);
1350 * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they are
1351 * skipped by pbus_assign_resources_sorted().
1353 static void pdev_assign_fixed_resources(struct pci_dev *dev)
1357 pci_dev_for_each_resource(dev, r) {
1360 if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) ||
1361 !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
1365 while (b && !r->parent) {
1366 assign_fixed_resource_on_bus(b, r);
1372 void __pci_bus_assign_resources(const struct pci_bus *bus,
1373 struct list_head *realloc_head,
1374 struct list_head *fail_head)
1377 struct pci_dev *dev;
1379 pbus_assign_resources_sorted(bus, realloc_head, fail_head);
1381 list_for_each_entry(dev, &bus->devices, bus_list) {
1382 pdev_assign_fixed_resources(dev);
1384 b = dev->subordinate;
1388 __pci_bus_assign_resources(b, realloc_head, fail_head);
1390 switch (dev->hdr_type) {
1391 case PCI_HEADER_TYPE_BRIDGE:
1392 if (!pci_is_enabled(dev))
1393 pci_setup_bridge(b);
1396 case PCI_HEADER_TYPE_CARDBUS:
1397 pci_setup_cardbus(b);
1401 pci_info(dev, "not setting up bridge for bus %04x:%02x\n",
1402 pci_domain_nr(b), b->number);
1408 void pci_bus_assign_resources(const struct pci_bus *bus)
1410 __pci_bus_assign_resources(bus, NULL, NULL);
1412 EXPORT_SYMBOL(pci_bus_assign_resources);
1414 static void pci_claim_device_resources(struct pci_dev *dev)
1418 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
1419 struct resource *r = &dev->resource[i];
1421 if (!r->flags || r->parent)
1424 pci_claim_resource(dev, i);
1428 static void pci_claim_bridge_resources(struct pci_dev *dev)
1432 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
1433 struct resource *r = &dev->resource[i];
1435 if (!r->flags || r->parent)
1438 pci_claim_bridge_resource(dev, i);
1442 static void pci_bus_allocate_dev_resources(struct pci_bus *b)
1444 struct pci_dev *dev;
1445 struct pci_bus *child;
1447 list_for_each_entry(dev, &b->devices, bus_list) {
1448 pci_claim_device_resources(dev);
1450 child = dev->subordinate;
1452 pci_bus_allocate_dev_resources(child);
1456 static void pci_bus_allocate_resources(struct pci_bus *b)
1458 struct pci_bus *child;
1461 * Carry out a depth-first search on the PCI bus tree to allocate
1462 * bridge apertures. Read the programmed bridge bases and
1463 * recursively claim the respective bridge resources.
1466 pci_read_bridge_bases(b);
1467 pci_claim_bridge_resources(b->self);
1470 list_for_each_entry(child, &b->children, node)
1471 pci_bus_allocate_resources(child);
1474 void pci_bus_claim_resources(struct pci_bus *b)
1476 pci_bus_allocate_resources(b);
1477 pci_bus_allocate_dev_resources(b);
1479 EXPORT_SYMBOL(pci_bus_claim_resources);
1481 static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
1482 struct list_head *add_head,
1483 struct list_head *fail_head)
1487 pdev_assign_resources_sorted((struct pci_dev *)bridge,
1488 add_head, fail_head);
1490 b = bridge->subordinate;
1494 __pci_bus_assign_resources(b, add_head, fail_head);
1496 switch (bridge->class >> 8) {
1497 case PCI_CLASS_BRIDGE_PCI:
1498 pci_setup_bridge(b);
1501 case PCI_CLASS_BRIDGE_CARDBUS:
1502 pci_setup_cardbus(b);
1506 pci_info(bridge, "not setting up bridge for bus %04x:%02x\n",
1507 pci_domain_nr(b), b->number);
1512 #define PCI_RES_TYPE_MASK \
1513 (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\
1516 static void pci_bridge_release_resources(struct pci_bus *bus,
1519 struct pci_dev *dev = bus->self;
1521 unsigned int old_flags;
1522 struct resource *b_res;
1525 b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
1528 * 1. If IO port assignment fails, release bridge IO port.
1529 * 2. If non pref MMIO assignment fails, release bridge nonpref MMIO.
1530 * 3. If 64bit pref MMIO assignment fails, and bridge pref is 64bit,
1531 * release bridge pref MMIO.
1532 * 4. If pref MMIO assignment fails, and bridge pref is 32bit,
1533 * release bridge pref MMIO.
1534 * 5. If pref MMIO assignment fails, and bridge pref is not
1535 * assigned, release bridge nonpref MMIO.
1537 if (type & IORESOURCE_IO)
1539 else if (!(type & IORESOURCE_PREFETCH))
1541 else if ((type & IORESOURCE_MEM_64) &&
1542 (b_res[2].flags & IORESOURCE_MEM_64))
1544 else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
1545 (b_res[2].flags & IORESOURCE_PREFETCH))
1555 /* If there are children, release them all */
1556 release_child_resources(r);
1557 if (!release_resource(r)) {
1558 type = old_flags = r->flags & PCI_RES_TYPE_MASK;
1559 pci_info(dev, "resource %d %pR released\n",
1560 PCI_BRIDGE_RESOURCES + idx, r);
1561 /* Keep the old size */
1562 r->end = resource_size(r) - 1;
1566 /* Avoiding touch the one without PREF */
1567 if (type & IORESOURCE_PREFETCH)
1568 type = IORESOURCE_PREFETCH;
1569 __pci_setup_bridge(bus, type);
1570 /* For next child res under same bridge */
1571 r->flags = old_flags;
1581 * Try to release PCI bridge resources from leaf bridge, so we can allocate
1582 * a larger window later.
1584 static void pci_bus_release_bridge_resources(struct pci_bus *bus,
1586 enum release_type rel_type)
1588 struct pci_dev *dev;
1589 bool is_leaf_bridge = true;
1591 list_for_each_entry(dev, &bus->devices, bus_list) {
1592 struct pci_bus *b = dev->subordinate;
1596 is_leaf_bridge = false;
1598 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1601 if (rel_type == whole_subtree)
1602 pci_bus_release_bridge_resources(b, type,
1606 if (pci_is_root_bus(bus))
1609 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1612 if ((rel_type == whole_subtree) || is_leaf_bridge)
1613 pci_bridge_release_resources(bus, type);
1616 static void pci_bus_dump_res(struct pci_bus *bus)
1618 struct resource *res;
1621 pci_bus_for_each_resource(bus, res, i) {
1622 if (!res || !res->end || !res->flags)
1625 dev_info(&bus->dev, "resource %d %pR\n", i, res);
1629 static void pci_bus_dump_resources(struct pci_bus *bus)
1632 struct pci_dev *dev;
1635 pci_bus_dump_res(bus);
1637 list_for_each_entry(dev, &bus->devices, bus_list) {
1638 b = dev->subordinate;
1642 pci_bus_dump_resources(b);
1646 static int pci_bus_get_depth(struct pci_bus *bus)
1649 struct pci_bus *child_bus;
1651 list_for_each_entry(child_bus, &bus->children, node) {
1654 ret = pci_bus_get_depth(child_bus);
1655 if (ret + 1 > depth)
1663 * -1: undefined, will auto detect later
1664 * 0: disabled by user
1665 * 1: disabled by auto detect
1666 * 2: enabled by user
1667 * 3: enabled by auto detect
1677 static enum enable_type pci_realloc_enable = undefined;
1678 void __init pci_realloc_get_opt(char *str)
1680 if (!strncmp(str, "off", 3))
1681 pci_realloc_enable = user_disabled;
1682 else if (!strncmp(str, "on", 2))
1683 pci_realloc_enable = user_enabled;
1685 static bool pci_realloc_enabled(enum enable_type enable)
1687 return enable >= user_enabled;
1690 #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1691 static int iov_resources_unassigned(struct pci_dev *dev, void *data)
1694 bool *unassigned = data;
1696 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1697 struct resource *r = &dev->resource[i + PCI_IOV_RESOURCES];
1698 struct pci_bus_region region;
1700 /* Not assigned or rejected by kernel? */
1704 pcibios_resource_to_bus(dev->bus, ®ion, r);
1705 if (!region.start) {
1707 return 1; /* Return early from pci_walk_bus() */
1714 static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1715 enum enable_type enable_local)
1717 bool unassigned = false;
1718 struct pci_host_bridge *host;
1720 if (enable_local != undefined)
1721 return enable_local;
1723 host = pci_find_host_bridge(bus);
1724 if (host->preserve_config)
1725 return auto_disabled;
1727 pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
1729 return auto_enabled;
1731 return enable_local;
1734 static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1735 enum enable_type enable_local)
1737 return enable_local;
1741 static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res,
1742 struct list_head *add_list,
1743 resource_size_t new_size)
1745 resource_size_t add_size, size = resource_size(res);
1753 if (new_size > size) {
1754 add_size = new_size - size;
1755 pci_dbg(bridge, "bridge window %pR extended by %pa\n", res,
1757 } else if (new_size < size) {
1758 add_size = size - new_size;
1759 pci_dbg(bridge, "bridge window %pR shrunken by %pa\n", res,
1765 res->end = res->start + new_size - 1;
1767 /* If the resource is part of the add_list, remove it now */
1769 remove_from_list(add_list, res);
1772 static void remove_dev_resource(struct resource *avail, struct pci_dev *dev,
1773 struct resource *res)
1775 resource_size_t size, align, tmp;
1777 size = resource_size(res);
1781 align = pci_resource_alignment(dev, res);
1782 align = align ? ALIGN(avail->start, align) - avail->start : 0;
1784 avail->start = min(avail->start + tmp, avail->end + 1);
1787 static void remove_dev_resources(struct pci_dev *dev, struct resource *io,
1788 struct resource *mmio,
1789 struct resource *mmio_pref)
1791 struct resource *res;
1793 pci_dev_for_each_resource(dev, res) {
1794 if (resource_type(res) == IORESOURCE_IO) {
1795 remove_dev_resource(io, dev, res);
1796 } else if (resource_type(res) == IORESOURCE_MEM) {
1799 * Make sure prefetchable memory is reduced from
1800 * the correct resource. Specifically we put 32-bit
1801 * prefetchable memory in non-prefetchable window
1802 * if there is an 64-bit prefetchable window.
1804 * See comments in __pci_bus_size_bridges() for
1807 if ((res->flags & IORESOURCE_PREFETCH) &&
1808 ((res->flags & IORESOURCE_MEM_64) ==
1809 (mmio_pref->flags & IORESOURCE_MEM_64)))
1810 remove_dev_resource(mmio_pref, dev, res);
1812 remove_dev_resource(mmio, dev, res);
1818 * io, mmio and mmio_pref contain the total amount of bridge window space
1819 * available. This includes the minimal space needed to cover all the
1820 * existing devices on the bus and the possible extra space that can be
1821 * shared with the bridges.
1823 static void pci_bus_distribute_available_resources(struct pci_bus *bus,
1824 struct list_head *add_list,
1826 struct resource mmio,
1827 struct resource mmio_pref)
1829 unsigned int normal_bridges = 0, hotplug_bridges = 0;
1830 struct resource *io_res, *mmio_res, *mmio_pref_res;
1831 struct pci_dev *dev, *bridge = bus->self;
1832 resource_size_t io_per_b, mmio_per_b, mmio_pref_per_b, align;
1834 io_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
1835 mmio_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
1836 mmio_pref_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
1839 * The alignment of this bridge is yet to be considered, hence it must
1840 * be done now before extending its bridge window.
1842 align = pci_resource_alignment(bridge, io_res);
1843 if (!io_res->parent && align)
1844 io.start = min(ALIGN(io.start, align), io.end + 1);
1846 align = pci_resource_alignment(bridge, mmio_res);
1847 if (!mmio_res->parent && align)
1848 mmio.start = min(ALIGN(mmio.start, align), mmio.end + 1);
1850 align = pci_resource_alignment(bridge, mmio_pref_res);
1851 if (!mmio_pref_res->parent && align)
1852 mmio_pref.start = min(ALIGN(mmio_pref.start, align),
1856 * Now that we have adjusted for alignment, update the bridge window
1857 * resources to fill as much remaining resource space as possible.
1859 adjust_bridge_window(bridge, io_res, add_list, resource_size(&io));
1860 adjust_bridge_window(bridge, mmio_res, add_list, resource_size(&mmio));
1861 adjust_bridge_window(bridge, mmio_pref_res, add_list,
1862 resource_size(&mmio_pref));
1865 * Calculate how many hotplug bridges and normal bridges there
1866 * are on this bus. We will distribute the additional available
1867 * resources between hotplug bridges.
1869 for_each_pci_bridge(dev, bus) {
1870 if (dev->is_hotplug_bridge)
1876 if (!(hotplug_bridges + normal_bridges))
1880 * Calculate the amount of space we can forward from "bus" to any
1881 * downstream buses, i.e., the space left over after assigning the
1882 * BARs and windows on "bus".
1884 list_for_each_entry(dev, &bus->devices, bus_list) {
1885 if (!dev->is_virtfn)
1886 remove_dev_resources(dev, &io, &mmio, &mmio_pref);
1890 * If there is at least one hotplug bridge on this bus it gets all
1891 * the extra resource space that was left after the reductions
1894 * If there are no hotplug bridges the extra resource space is
1895 * split between non-hotplug bridges. This is to allow possible
1896 * hotplug bridges below them to get the extra space as well.
1898 if (hotplug_bridges) {
1899 io_per_b = div64_ul(resource_size(&io), hotplug_bridges);
1900 mmio_per_b = div64_ul(resource_size(&mmio), hotplug_bridges);
1901 mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
1904 io_per_b = div64_ul(resource_size(&io), normal_bridges);
1905 mmio_per_b = div64_ul(resource_size(&mmio), normal_bridges);
1906 mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
1910 for_each_pci_bridge(dev, bus) {
1911 struct resource *res;
1914 b = dev->subordinate;
1917 if (hotplug_bridges && !dev->is_hotplug_bridge)
1920 res = &dev->resource[PCI_BRIDGE_IO_WINDOW];
1923 * Make sure the split resource space is properly aligned
1924 * for bridge windows (align it down to avoid going above
1925 * what is available).
1927 align = pci_resource_alignment(dev, res);
1928 io.end = align ? io.start + ALIGN_DOWN(io_per_b, align) - 1
1929 : io.start + io_per_b - 1;
1932 * The x_per_b holds the extra resource space that can be
1933 * added for each bridge but there is the minimal already
1934 * reserved as well so adjust x.start down accordingly to
1935 * cover the whole space.
1937 io.start -= resource_size(res);
1939 res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
1940 align = pci_resource_alignment(dev, res);
1941 mmio.end = align ? mmio.start + ALIGN_DOWN(mmio_per_b, align) - 1
1942 : mmio.start + mmio_per_b - 1;
1943 mmio.start -= resource_size(res);
1945 res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
1946 align = pci_resource_alignment(dev, res);
1947 mmio_pref.end = align ? mmio_pref.start +
1948 ALIGN_DOWN(mmio_pref_per_b, align) - 1
1949 : mmio_pref.start + mmio_pref_per_b - 1;
1950 mmio_pref.start -= resource_size(res);
1952 pci_bus_distribute_available_resources(b, add_list, io, mmio,
1955 io.start += io.end + 1;
1956 mmio.start += mmio.end + 1;
1957 mmio_pref.start += mmio_pref.end + 1;
1961 static void pci_bridge_distribute_available_resources(struct pci_dev *bridge,
1962 struct list_head *add_list)
1964 struct resource available_io, available_mmio, available_mmio_pref;
1966 if (!bridge->is_hotplug_bridge)
1969 pci_dbg(bridge, "distributing available resources\n");
1971 /* Take the initial extra resources from the hotplug port */
1972 available_io = bridge->resource[PCI_BRIDGE_IO_WINDOW];
1973 available_mmio = bridge->resource[PCI_BRIDGE_MEM_WINDOW];
1974 available_mmio_pref = bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
1976 pci_bus_distribute_available_resources(bridge->subordinate,
1977 add_list, available_io,
1979 available_mmio_pref);
1982 static bool pci_bridge_resources_not_assigned(struct pci_dev *dev)
1984 const struct resource *r;
1987 * If the child device's resources are not yet assigned it means we
1988 * are configuring them (not the boot firmware), so we should be
1989 * able to extend the upstream bridge resources in the same way we
1990 * do with the normal hotplug case.
1992 r = &dev->resource[PCI_BRIDGE_IO_WINDOW];
1993 if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
1995 r = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
1996 if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
1998 r = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
1999 if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
2006 pci_root_bus_distribute_available_resources(struct pci_bus *bus,
2007 struct list_head *add_list)
2009 struct pci_dev *dev, *bridge = bus->self;
2011 for_each_pci_bridge(dev, bus) {
2014 b = dev->subordinate;
2019 * Need to check "bridge" here too because it is NULL
2020 * in case of root bus.
2022 if (bridge && pci_bridge_resources_not_assigned(dev))
2023 pci_bridge_distribute_available_resources(bridge,
2026 pci_root_bus_distribute_available_resources(b, add_list);
2031 * First try will not touch PCI bridge res.
2032 * Second and later try will clear small leaf bridge res.
2033 * Will stop till to the max depth if can not find good one.
2035 void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
2037 LIST_HEAD(realloc_head);
2038 /* List of resources that want additional resources */
2039 struct list_head *add_list = NULL;
2040 int tried_times = 0;
2041 enum release_type rel_type = leaf_only;
2042 LIST_HEAD(fail_head);
2043 struct pci_dev_resource *fail_res;
2044 int pci_try_num = 1;
2045 enum enable_type enable_local;
2047 /* Don't realloc if asked to do so */
2048 enable_local = pci_realloc_detect(bus, pci_realloc_enable);
2049 if (pci_realloc_enabled(enable_local)) {
2050 int max_depth = pci_bus_get_depth(bus);
2052 pci_try_num = max_depth + 1;
2053 dev_info(&bus->dev, "max bus depth: %d pci_try_num: %d\n",
2054 max_depth, pci_try_num);
2059 * Last try will use add_list, otherwise will try good to have as must
2060 * have, so can realloc parent bridge resource
2062 if (tried_times + 1 == pci_try_num)
2063 add_list = &realloc_head;
2065 * Depth first, calculate sizes and alignments of all subordinate buses.
2067 __pci_bus_size_bridges(bus, add_list);
2069 pci_root_bus_distribute_available_resources(bus, add_list);
2071 /* Depth last, allocate resources and update the hardware. */
2072 __pci_bus_assign_resources(bus, add_list, &fail_head);
2074 BUG_ON(!list_empty(add_list));
2077 /* Any device complain? */
2078 if (list_empty(&fail_head))
2081 if (tried_times >= pci_try_num) {
2082 if (enable_local == undefined)
2083 dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
2084 else if (enable_local == auto_enabled)
2085 dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
2087 free_list(&fail_head);
2091 dev_info(&bus->dev, "No. %d try to assign unassigned res\n",
2094 /* Third times and later will not check if it is leaf */
2095 if ((tried_times + 1) > 2)
2096 rel_type = whole_subtree;
2099 * Try to release leaf bridge's resources that doesn't fit resource of
2100 * child device under that bridge.
2102 list_for_each_entry(fail_res, &fail_head, list)
2103 pci_bus_release_bridge_resources(fail_res->dev->bus,
2104 fail_res->flags & PCI_RES_TYPE_MASK,
2107 /* Restore size and flags */
2108 list_for_each_entry(fail_res, &fail_head, list) {
2109 struct resource *res = fail_res->res;
2112 res->start = fail_res->start;
2113 res->end = fail_res->end;
2114 res->flags = fail_res->flags;
2116 if (pci_is_bridge(fail_res->dev)) {
2117 idx = res - &fail_res->dev->resource[0];
2118 if (idx >= PCI_BRIDGE_RESOURCES &&
2119 idx <= PCI_BRIDGE_RESOURCE_END)
2123 free_list(&fail_head);
2128 /* Dump the resource on buses */
2129 pci_bus_dump_resources(bus);
2132 void __init pci_assign_unassigned_resources(void)
2134 struct pci_bus *root_bus;
2136 list_for_each_entry(root_bus, &pci_root_buses, node) {
2137 pci_assign_unassigned_root_bus_resources(root_bus);
2139 /* Make sure the root bridge has a companion ACPI device */
2140 if (ACPI_HANDLE(root_bus->bridge))
2141 acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
2145 void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
2147 struct pci_bus *parent = bridge->subordinate;
2148 /* List of resources that want additional resources */
2149 LIST_HEAD(add_list);
2151 int tried_times = 0;
2152 LIST_HEAD(fail_head);
2153 struct pci_dev_resource *fail_res;
2157 __pci_bus_size_bridges(parent, &add_list);
2160 * Distribute remaining resources (if any) equally between hotplug
2161 * bridges below. This makes it possible to extend the hierarchy
2162 * later without running out of resources.
2164 pci_bridge_distribute_available_resources(bridge, &add_list);
2166 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
2167 BUG_ON(!list_empty(&add_list));
2170 if (list_empty(&fail_head))
2173 if (tried_times >= 2) {
2174 /* Still fail, don't need to try more */
2175 free_list(&fail_head);
2179 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
2183 * Try to release leaf bridge's resources that aren't big enough
2184 * to contain child device resources.
2186 list_for_each_entry(fail_res, &fail_head, list)
2187 pci_bus_release_bridge_resources(fail_res->dev->bus,
2188 fail_res->flags & PCI_RES_TYPE_MASK,
2191 /* Restore size and flags */
2192 list_for_each_entry(fail_res, &fail_head, list) {
2193 struct resource *res = fail_res->res;
2196 res->start = fail_res->start;
2197 res->end = fail_res->end;
2198 res->flags = fail_res->flags;
2200 if (pci_is_bridge(fail_res->dev)) {
2201 idx = res - &fail_res->dev->resource[0];
2202 if (idx >= PCI_BRIDGE_RESOURCES &&
2203 idx <= PCI_BRIDGE_RESOURCE_END)
2207 free_list(&fail_head);
2212 retval = pci_reenable_device(bridge);
2214 pci_err(bridge, "Error reenabling bridge (%d)\n", retval);
2215 pci_set_master(bridge);
2217 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
2219 int pci_reassign_bridge_resources(struct pci_dev *bridge, unsigned long type)
2221 struct pci_dev_resource *dev_res;
2222 struct pci_dev *next;
2229 down_read(&pci_bus_sem);
2231 /* Walk to the root hub, releasing bridge BARs when possible */
2235 for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCE_END;
2237 struct resource *res = &bridge->resource[i];
2239 if ((res->flags ^ type) & PCI_RES_TYPE_MASK)
2242 /* Ignore BARs which are still in use */
2246 ret = add_to_list(&saved, bridge, res, 0, 0);
2250 pci_info(bridge, "BAR %d: releasing %pR\n",
2254 release_resource(res);
2259 if (i == PCI_BRIDGE_RESOURCE_END)
2262 next = bridge->bus ? bridge->bus->self : NULL;
2265 if (list_empty(&saved)) {
2266 up_read(&pci_bus_sem);
2270 __pci_bus_size_bridges(bridge->subordinate, &added);
2271 __pci_bridge_assign_resources(bridge, &added, &failed);
2272 BUG_ON(!list_empty(&added));
2274 if (!list_empty(&failed)) {
2279 list_for_each_entry(dev_res, &saved, list) {
2280 /* Skip the bridge we just assigned resources for */
2281 if (bridge == dev_res->dev)
2284 bridge = dev_res->dev;
2285 pci_setup_bridge(bridge->subordinate);
2289 up_read(&pci_bus_sem);
2293 /* Restore size and flags */
2294 list_for_each_entry(dev_res, &failed, list) {
2295 struct resource *res = dev_res->res;
2297 res->start = dev_res->start;
2298 res->end = dev_res->end;
2299 res->flags = dev_res->flags;
2303 /* Revert to the old configuration */
2304 list_for_each_entry(dev_res, &saved, list) {
2305 struct resource *res = dev_res->res;
2307 bridge = dev_res->dev;
2308 i = res - bridge->resource;
2310 res->start = dev_res->start;
2311 res->end = dev_res->end;
2312 res->flags = dev_res->flags;
2314 pci_claim_resource(bridge, i);
2315 pci_setup_bridge(bridge->subordinate);
2318 up_read(&pci_bus_sem);
2323 void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
2325 struct pci_dev *dev;
2326 /* List of resources that want additional resources */
2327 LIST_HEAD(add_list);
2329 down_read(&pci_bus_sem);
2330 for_each_pci_bridge(dev, bus)
2331 if (pci_has_subordinate(dev))
2332 __pci_bus_size_bridges(dev->subordinate, &add_list);
2333 up_read(&pci_bus_sem);
2334 __pci_bus_assign_resources(bus, &add_list, NULL);
2335 BUG_ON(!list_empty(&add_list));
2337 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);