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/bitops.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/errno.h>
23 #include <linux/ioport.h>
24 #include <linux/cache.h>
25 #include <linux/limits.h>
26 #include <linux/sizes.h>
27 #include <linux/slab.h>
28 #include <linux/acpi.h>
31 unsigned int pci_flags;
32 EXPORT_SYMBOL_GPL(pci_flags);
34 struct pci_dev_resource {
35 struct list_head list;
38 resource_size_t start;
40 resource_size_t add_size;
41 resource_size_t min_align;
45 static void free_list(struct list_head *head)
47 struct pci_dev_resource *dev_res, *tmp;
49 list_for_each_entry_safe(dev_res, tmp, head, list) {
50 list_del(&dev_res->list);
56 * add_to_list() - Add a new resource tracker to the list
57 * @head: Head of the list
58 * @dev: Device to which the resource belongs
59 * @res: Resource to be tracked
60 * @add_size: Additional size to be optionally added to the resource
61 * @min_align: Minimum memory window alignment
63 static int add_to_list(struct list_head *head, struct pci_dev *dev,
64 struct resource *res, resource_size_t add_size,
65 resource_size_t min_align)
67 struct pci_dev_resource *tmp;
69 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
75 tmp->start = res->start;
77 tmp->flags = res->flags;
78 tmp->add_size = add_size;
79 tmp->min_align = min_align;
81 list_add(&tmp->list, head);
86 static void remove_from_list(struct list_head *head, struct resource *res)
88 struct pci_dev_resource *dev_res, *tmp;
90 list_for_each_entry_safe(dev_res, tmp, head, list) {
91 if (dev_res->res == res) {
92 list_del(&dev_res->list);
99 static struct pci_dev_resource *res_to_dev_res(struct list_head *head,
100 struct resource *res)
102 struct pci_dev_resource *dev_res;
104 list_for_each_entry(dev_res, head, list) {
105 if (dev_res->res == res)
112 static resource_size_t get_res_add_size(struct list_head *head,
113 struct resource *res)
115 struct pci_dev_resource *dev_res;
117 dev_res = res_to_dev_res(head, res);
118 return dev_res ? dev_res->add_size : 0;
121 static resource_size_t get_res_add_align(struct list_head *head,
122 struct resource *res)
124 struct pci_dev_resource *dev_res;
126 dev_res = res_to_dev_res(head, res);
127 return dev_res ? dev_res->min_align : 0;
130 static void restore_dev_resource(struct pci_dev_resource *dev_res)
132 struct resource *res = dev_res->res;
134 res->start = dev_res->start;
135 res->end = dev_res->end;
136 res->flags = dev_res->flags;
139 static bool pdev_resources_assignable(struct pci_dev *dev)
141 u16 class = dev->class >> 8, command;
143 /* Don't touch classless devices or host bridges or IOAPICs */
144 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
147 /* Don't touch IOAPIC devices already enabled by firmware */
148 if (class == PCI_CLASS_SYSTEM_PIC) {
149 pci_read_config_word(dev, PCI_COMMAND, &command);
150 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
157 /* Sort resources by alignment */
158 static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
163 if (!pdev_resources_assignable(dev))
166 pci_dev_for_each_resource(dev, r, i) {
167 const char *r_name = pci_resource_name(dev, i);
168 struct pci_dev_resource *dev_res, *tmp;
169 resource_size_t r_align;
172 if (r->flags & IORESOURCE_PCI_FIXED)
175 if (!(r->flags) || r->parent)
178 r_align = pci_resource_alignment(dev, r);
180 pci_warn(dev, "%s %pR: alignment must not be zero\n",
185 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
187 panic("%s: kzalloc() failed!\n", __func__);
190 tmp->start = r->start;
192 tmp->flags = r->flags;
194 /* Fallback is smallest one or list is empty */
196 list_for_each_entry(dev_res, head, list) {
197 resource_size_t align;
199 align = pci_resource_alignment(dev_res->dev,
202 if (r_align > align) {
207 /* Insert it just before n */
208 list_add_tail(&tmp->list, n);
212 bool pci_resource_is_optional(const struct pci_dev *dev, int resno)
214 const struct resource *res = pci_resource_n(dev, resno);
216 if (pci_resource_is_iov(resno))
218 if (resno == PCI_ROM_RESOURCE && !(res->flags & IORESOURCE_ROM_ENABLE))
224 static inline void reset_resource(struct resource *res)
232 * reassign_resources_sorted() - Satisfy any additional resource requests
234 * @realloc_head: Head of the list tracking requests requiring
235 * additional resources
236 * @head: Head of the list tracking requests with allocated
239 * Walk through each element of the realloc_head and try to procure additional
240 * resources for the element, provided the element is in the head list.
242 static void reassign_resources_sorted(struct list_head *realloc_head,
243 struct list_head *head)
245 struct pci_dev_resource *add_res, *tmp;
246 struct pci_dev_resource *dev_res;
248 struct resource *res;
249 const char *res_name;
250 resource_size_t add_size, align;
253 list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
254 bool found_match = false;
258 idx = pci_resource_num(dev, res);
261 * Skip resource that failed the earlier assignment and is
262 * not optional as it would just fail again.
264 if (!res->parent && resource_size(res) &&
265 !pci_resource_is_optional(dev, idx))
268 /* Skip this resource if not found in head list */
269 list_for_each_entry(dev_res, head, list) {
270 if (dev_res->res == res) {
275 if (!found_match) /* Just skip */
278 res_name = pci_resource_name(dev, idx);
279 add_size = add_res->add_size;
280 align = add_res->min_align;
282 resource_set_range(res, align,
283 resource_size(res) + add_size);
284 if (pci_assign_resource(dev, idx)) {
286 "%s %pR: ignoring failure in optional allocation\n",
289 } else if (add_size > 0) {
290 res->flags |= add_res->flags &
291 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
292 if (pci_reassign_resource(dev, idx, add_size, align))
293 pci_info(dev, "%s %pR: failed to add optional %llx\n",
295 (unsigned long long) add_size);
298 list_del(&add_res->list);
304 * assign_requested_resources_sorted() - Satisfy resource requests
306 * @head: Head of the list tracking requests for resources
307 * @fail_head: Head of the list tracking requests that could not be
309 * @optional: Assign also optional resources
311 * Satisfy resource requests of each element in the list. Add requests that
312 * could not be satisfied to the failed_list.
314 static void assign_requested_resources_sorted(struct list_head *head,
315 struct list_head *fail_head,
318 struct pci_dev_resource *dev_res;
319 struct resource *res;
324 list_for_each_entry(dev_res, head, list) {
327 idx = pci_resource_num(dev, res);
328 optional_res = pci_resource_is_optional(dev, idx);
330 if (!resource_size(res))
333 if (!optional && optional_res)
336 if (pci_assign_resource(dev, idx)) {
338 add_to_list(fail_head, dev, res,
346 static unsigned long pci_fail_res_type_mask(struct list_head *fail_head)
348 struct pci_dev_resource *fail_res;
349 unsigned long mask = 0;
351 /* Check failed type */
352 list_for_each_entry(fail_res, fail_head, list)
353 mask |= fail_res->flags;
356 * One pref failed resource will set IORESOURCE_MEM, as we can
357 * allocate pref in non-pref range. Will release all assigned
358 * non-pref sibling resources according to that bit.
360 return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
363 static bool pci_need_to_release(unsigned long mask, struct resource *res)
365 if (res->flags & IORESOURCE_IO)
366 return !!(mask & IORESOURCE_IO);
368 /* Check pref at first */
369 if (res->flags & IORESOURCE_PREFETCH) {
370 if (mask & IORESOURCE_PREFETCH)
372 /* Count pref if its parent is non-pref */
373 else if ((mask & IORESOURCE_MEM) &&
374 !(res->parent->flags & IORESOURCE_PREFETCH))
380 if (res->flags & IORESOURCE_MEM)
381 return !!(mask & IORESOURCE_MEM);
383 return false; /* Should not get here */
386 /* Return: @true if assignment of a required resource failed. */
387 static bool pci_required_resource_failed(struct list_head *fail_head)
389 struct pci_dev_resource *fail_res;
391 list_for_each_entry(fail_res, fail_head, list) {
392 int idx = pci_resource_num(fail_res->dev, fail_res->res);
394 if (!pci_resource_is_optional(fail_res->dev, idx))
400 static void __assign_resources_sorted(struct list_head *head,
401 struct list_head *realloc_head,
402 struct list_head *fail_head)
405 * Should not assign requested resources at first. They could be
406 * adjacent, so later reassign can not reallocate them one by one in
407 * parent resource window.
409 * Try to assign required and any optional resources at beginning
410 * (add_size included). If all required resources were successfully
411 * assigned, get out early. If could not do that, we still try to
412 * assign required at first, then try to reassign some optional
415 * Separate three resource type checking if we need to release
416 * assigned resource after requested + add_size try.
418 * 1. If IO port assignment fails, will release assigned IO
420 * 2. If pref MMIO assignment fails, release assigned pref
421 * MMIO. If assigned pref MMIO's parent is non-pref MMIO
422 * and non-pref MMIO assignment fails, will release that
423 * assigned pref MMIO.
424 * 3. If non-pref MMIO assignment fails or pref MMIO
425 * assignment fails, will release assigned non-pref MMIO.
427 LIST_HEAD(save_head);
428 LIST_HEAD(local_fail_head);
429 LIST_HEAD(dummy_head);
430 struct pci_dev_resource *save_res;
431 struct pci_dev_resource *dev_res, *tmp_res, *dev_res2;
432 struct resource *res;
434 const char *res_name;
436 unsigned long fail_type;
437 resource_size_t add_align, align;
440 realloc_head = &dummy_head;
442 /* Check if optional add_size is there */
443 if (list_empty(realloc_head))
446 /* Save original start, end, flags etc at first */
447 list_for_each_entry(dev_res, head, list) {
448 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
449 free_list(&save_head);
454 /* Update res in head list with add_size in realloc_head list */
455 list_for_each_entry_safe(dev_res, tmp_res, head, list) {
458 res->end += get_res_add_size(realloc_head, res);
461 * There are two kinds of additional resources in the list:
462 * 1. bridge resource -- IORESOURCE_STARTALIGN
463 * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN
464 * Here just fix the additional alignment for bridge
466 if (!(res->flags & IORESOURCE_STARTALIGN))
469 add_align = get_res_add_align(realloc_head, res);
472 * The "head" list is sorted by alignment so resources with
473 * bigger alignment will be assigned first. After we
474 * change the alignment of a dev_res in "head" list, we
475 * need to reorder the list by alignment to make it
478 if (add_align > res->start) {
479 resource_set_range(res, add_align, resource_size(res));
481 list_for_each_entry(dev_res2, head, list) {
482 align = pci_resource_alignment(dev_res2->dev,
484 if (add_align > align) {
485 list_move_tail(&dev_res->list,
495 assign_requested_resources_sorted(head, &local_fail_head, true);
497 /* All non-optional resources assigned? */
498 if (list_empty(&local_fail_head)) {
499 /* Remove head list from realloc_head list */
500 list_for_each_entry(dev_res, head, list)
501 remove_from_list(realloc_head, dev_res->res);
502 free_list(&save_head);
506 /* Without realloc_head and only optional fails, nothing more to do. */
507 if (!pci_required_resource_failed(&local_fail_head) &&
508 list_empty(realloc_head)) {
509 list_for_each_entry(save_res, &save_head, list) {
510 struct resource *res = save_res->res;
515 restore_dev_resource(save_res);
517 free_list(&local_fail_head);
518 free_list(&save_head);
522 /* Check failed type */
523 fail_type = pci_fail_res_type_mask(&local_fail_head);
524 /* Remove not need to be released assigned res from head list etc */
525 list_for_each_entry_safe(dev_res, tmp_res, head, list) {
528 if (res->parent && !pci_need_to_release(fail_type, res)) {
529 /* Remove it from realloc_head list */
530 remove_from_list(realloc_head, res);
531 remove_from_list(&save_head, res);
532 list_del(&dev_res->list);
537 free_list(&local_fail_head);
538 /* Release assigned resource */
539 list_for_each_entry(dev_res, head, list) {
546 idx = pci_resource_num(dev, res);
547 res_name = pci_resource_name(dev, idx);
548 pci_dbg(dev, "%s %pR: releasing\n", res_name, res);
550 release_resource(res);
551 restore_dev_resource(dev_res);
553 /* Restore start/end/flags from saved list */
554 list_for_each_entry(save_res, &save_head, list)
555 restore_dev_resource(save_res);
556 free_list(&save_head);
558 /* Satisfy the must-have resource requests */
559 assign_requested_resources_sorted(head, NULL, false);
561 /* Try to satisfy any additional optional resource requests */
562 if (!list_empty(realloc_head))
563 reassign_resources_sorted(realloc_head, head);
566 /* Reset any failed resource, cannot use fail_head as it can be NULL. */
567 list_for_each_entry(dev_res, head, list) {
575 add_to_list(fail_head, dev, res,
586 static void pdev_assign_resources_sorted(struct pci_dev *dev,
587 struct list_head *add_head,
588 struct list_head *fail_head)
592 pdev_sort_resources(dev, &head);
593 __assign_resources_sorted(&head, add_head, fail_head);
597 static void pbus_assign_resources_sorted(const struct pci_bus *bus,
598 struct list_head *realloc_head,
599 struct list_head *fail_head)
604 list_for_each_entry(dev, &bus->devices, bus_list)
605 pdev_sort_resources(dev, &head);
607 __assign_resources_sorted(&head, realloc_head, fail_head);
610 void pci_setup_cardbus(struct pci_bus *bus)
612 struct pci_dev *bridge = bus->self;
613 struct resource *res;
614 struct pci_bus_region region;
616 pci_info(bridge, "CardBus bridge to %pR\n",
619 res = bus->resource[0];
620 pcibios_resource_to_bus(bridge->bus, ®ion, res);
621 if (res->flags & IORESOURCE_IO) {
623 * The IO resource is allocated a range twice as large as it
624 * would normally need. This allows us to set both IO regs.
626 pci_info(bridge, " bridge window %pR\n", res);
627 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
629 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
633 res = bus->resource[1];
634 pcibios_resource_to_bus(bridge->bus, ®ion, res);
635 if (res->flags & IORESOURCE_IO) {
636 pci_info(bridge, " bridge window %pR\n", res);
637 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
639 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
643 res = bus->resource[2];
644 pcibios_resource_to_bus(bridge->bus, ®ion, res);
645 if (res->flags & IORESOURCE_MEM) {
646 pci_info(bridge, " bridge window %pR\n", res);
647 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
649 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
653 res = bus->resource[3];
654 pcibios_resource_to_bus(bridge->bus, ®ion, res);
655 if (res->flags & IORESOURCE_MEM) {
656 pci_info(bridge, " bridge window %pR\n", res);
657 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
659 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
663 EXPORT_SYMBOL(pci_setup_cardbus);
666 * Initialize bridges with base/limit values we have collected. PCI-to-PCI
667 * Bridge Architecture Specification rev. 1.1 (1998) requires that if there
668 * are no I/O ports or memory behind the bridge, the corresponding range
669 * must be turned off by writing base value greater than limit to the
670 * bridge's base/limit registers.
672 * Note: care must be taken when updating I/O base/limit registers of
673 * bridges which support 32-bit I/O. This update requires two config space
674 * writes, so it's quite possible that an I/O window of the bridge will
675 * have some undesirable address (e.g. 0) after the first write. Ditto
676 * 64-bit prefetchable MMIO.
678 static void pci_setup_bridge_io(struct pci_dev *bridge)
680 struct resource *res;
681 const char *res_name;
682 struct pci_bus_region region;
683 unsigned long io_mask;
684 u8 io_base_lo, io_limit_lo;
688 io_mask = PCI_IO_RANGE_MASK;
689 if (bridge->io_window_1k)
690 io_mask = PCI_IO_1K_RANGE_MASK;
692 /* Set up the top and bottom of the PCI I/O segment for this bus */
693 res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
694 res_name = pci_resource_name(bridge, PCI_BRIDGE_IO_WINDOW);
695 pcibios_resource_to_bus(bridge->bus, ®ion, res);
696 if (res->flags & IORESOURCE_IO) {
697 pci_read_config_word(bridge, PCI_IO_BASE, &l);
698 io_base_lo = (region.start >> 8) & io_mask;
699 io_limit_lo = (region.end >> 8) & io_mask;
700 l = ((u16) io_limit_lo << 8) | io_base_lo;
701 /* Set up upper 16 bits of I/O base/limit */
702 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
703 pci_info(bridge, " %s %pR\n", res_name, res);
705 /* Clear upper 16 bits of I/O base/limit */
709 /* Temporarily disable the I/O range before updating PCI_IO_BASE */
710 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
711 /* Update lower 16 bits of I/O base/limit */
712 pci_write_config_word(bridge, PCI_IO_BASE, l);
713 /* Update upper 16 bits of I/O base/limit */
714 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
717 static void pci_setup_bridge_mmio(struct pci_dev *bridge)
719 struct resource *res;
720 const char *res_name;
721 struct pci_bus_region region;
724 /* Set up the top and bottom of the PCI Memory segment for this bus */
725 res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
726 res_name = pci_resource_name(bridge, PCI_BRIDGE_MEM_WINDOW);
727 pcibios_resource_to_bus(bridge->bus, ®ion, res);
728 if (res->flags & IORESOURCE_MEM) {
729 l = (region.start >> 16) & 0xfff0;
730 l |= region.end & 0xfff00000;
731 pci_info(bridge, " %s %pR\n", res_name, res);
735 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
738 static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge)
740 struct resource *res;
741 const char *res_name;
742 struct pci_bus_region region;
746 * Clear out the upper 32 bits of PREF limit. If
747 * PCI_PREF_BASE_UPPER32 was non-zero, this temporarily disables
748 * PREF range, which is ok.
750 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
752 /* Set up PREF base/limit */
754 res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
755 res_name = pci_resource_name(bridge, PCI_BRIDGE_PREF_MEM_WINDOW);
756 pcibios_resource_to_bus(bridge->bus, ®ion, res);
757 if (res->flags & IORESOURCE_PREFETCH) {
758 l = (region.start >> 16) & 0xfff0;
759 l |= region.end & 0xfff00000;
760 if (res->flags & IORESOURCE_MEM_64) {
761 bu = upper_32_bits(region.start);
762 lu = upper_32_bits(region.end);
764 pci_info(bridge, " %s %pR\n", res_name, res);
768 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
770 /* Set the upper 32 bits of PREF base & limit */
771 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
772 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
775 static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
777 struct pci_dev *bridge = bus->self;
779 pci_info(bridge, "PCI bridge to %pR\n", &bus->busn_res);
781 if (type & IORESOURCE_IO)
782 pci_setup_bridge_io(bridge);
784 if (type & IORESOURCE_MEM)
785 pci_setup_bridge_mmio(bridge);
787 if (type & IORESOURCE_PREFETCH)
788 pci_setup_bridge_mmio_pref(bridge);
790 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
793 void __weak pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
797 static void pci_setup_bridge(struct pci_bus *bus)
799 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
802 pcibios_setup_bridge(bus, type);
803 __pci_setup_bridge(bus, type);
807 int pci_claim_bridge_resource(struct pci_dev *bridge, int i)
809 if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END)
812 if (pci_claim_resource(bridge, i) == 0)
813 return 0; /* Claimed the window */
815 if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
818 if (!pci_bus_clip_resource(bridge, i))
819 return -EINVAL; /* Clipping didn't change anything */
822 case PCI_BRIDGE_IO_WINDOW:
823 pci_setup_bridge_io(bridge);
825 case PCI_BRIDGE_MEM_WINDOW:
826 pci_setup_bridge_mmio(bridge);
828 case PCI_BRIDGE_PREF_MEM_WINDOW:
829 pci_setup_bridge_mmio_pref(bridge);
835 if (pci_claim_resource(bridge, i) == 0)
836 return 0; /* Claimed a smaller window */
842 * Check whether the bridge supports optional I/O and prefetchable memory
843 * ranges. If not, the respective base/limit registers must be read-only
846 static void pci_bridge_check_ranges(struct pci_bus *bus)
848 struct pci_dev *bridge = bus->self;
849 struct resource *b_res;
851 b_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
852 b_res->flags |= IORESOURCE_MEM;
854 if (bridge->io_window) {
855 b_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
856 b_res->flags |= IORESOURCE_IO;
859 if (bridge->pref_window) {
860 b_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
861 b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
862 if (bridge->pref_64_window) {
863 b_res->flags |= IORESOURCE_MEM_64 |
864 PCI_PREF_RANGE_TYPE_64;
870 * Helper function for sizing routines. Assigned resources have non-NULL
873 * Return first unassigned resource of the correct type. If there is none,
874 * return first assigned resource of the correct type. If none of the
875 * above, return NULL.
877 * Returning an assigned resource of the correct type allows the caller to
878 * distinguish between already assigned and no resource of the correct type.
880 static struct resource *find_bus_resource_of_type(struct pci_bus *bus,
881 unsigned long type_mask,
884 struct resource *r, *r_assigned = NULL;
886 pci_bus_for_each_resource(bus, r) {
887 if (r == &ioport_resource || r == &iomem_resource)
889 if (r && (r->flags & type_mask) == type && !r->parent)
891 if (r && (r->flags & type_mask) == type && !r_assigned)
897 static resource_size_t calculate_iosize(resource_size_t size,
898 resource_size_t min_size,
899 resource_size_t size1,
900 resource_size_t add_size,
901 resource_size_t children_add_size,
902 resource_size_t old_size,
903 resource_size_t align)
910 * To be fixed in 2.5: we should have sort of HAVE_ISA flag in the
913 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
914 size = (size & 0xff) + ((size & ~0xffUL) << 2);
918 size = max(size, add_size) + children_add_size;
919 return ALIGN(max(size, old_size), align);
922 static resource_size_t calculate_memsize(resource_size_t size,
923 resource_size_t min_size,
924 resource_size_t add_size,
925 resource_size_t children_add_size,
926 resource_size_t old_size,
927 resource_size_t align)
934 size = max(size, add_size) + children_add_size;
935 return ALIGN(max(size, old_size), align);
938 resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
944 #define PCI_P2P_DEFAULT_MEM_ALIGN SZ_1M
945 #define PCI_P2P_DEFAULT_IO_ALIGN SZ_4K
946 #define PCI_P2P_DEFAULT_IO_ALIGN_1K SZ_1K
948 static resource_size_t window_alignment(struct pci_bus *bus, unsigned long type)
950 resource_size_t align = 1, arch_align;
952 if (type & IORESOURCE_MEM)
953 align = PCI_P2P_DEFAULT_MEM_ALIGN;
954 else if (type & IORESOURCE_IO) {
956 * Per spec, I/O windows are 4K-aligned, but some bridges have
957 * an extension to support 1K alignment.
959 if (bus->self && bus->self->io_window_1k)
960 align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
962 align = PCI_P2P_DEFAULT_IO_ALIGN;
965 arch_align = pcibios_window_alignment(bus, type);
966 return max(align, arch_align);
970 * pbus_size_io() - Size the I/O window of a given bus
973 * @min_size: The minimum I/O window that must be allocated
974 * @add_size: Additional optional I/O window
975 * @realloc_head: Track the additional I/O window on this list
977 * Sizing the I/O windows of the PCI-PCI bridge is trivial, since these
978 * windows have 1K or 4K granularity and the I/O ranges of non-bridge PCI
979 * devices are limited to 256 bytes. We must be careful with the ISA
982 static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
983 resource_size_t add_size,
984 struct list_head *realloc_head)
987 struct resource *b_res = find_bus_resource_of_type(bus, IORESOURCE_IO,
989 resource_size_t size = 0, size0 = 0, size1 = 0;
990 resource_size_t children_add_size = 0;
991 resource_size_t min_align, align;
996 /* If resource is already assigned, nothing more to do */
1000 min_align = window_alignment(bus, IORESOURCE_IO);
1001 list_for_each_entry(dev, &bus->devices, bus_list) {
1004 pci_dev_for_each_resource(dev, r) {
1005 unsigned long r_size;
1007 if (r->parent || !(r->flags & IORESOURCE_IO))
1009 r_size = resource_size(r);
1012 /* Might be re-aligned for ISA */
1017 align = pci_resource_alignment(dev, r);
1018 if (align > min_align)
1022 children_add_size += get_res_add_size(realloc_head, r);
1026 size0 = calculate_iosize(size, min_size, size1, 0, 0,
1027 resource_size(b_res), min_align);
1030 if (realloc_head && (add_size > 0 || children_add_size > 0)) {
1031 size1 = calculate_iosize(size, min_size, size1, add_size,
1032 children_add_size, resource_size(b_res),
1036 if (!size0 && !size1) {
1037 if (bus->self && (b_res->start || b_res->end))
1038 pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
1039 b_res, &bus->busn_res);
1044 resource_set_range(b_res, min_align, size0);
1045 b_res->flags |= IORESOURCE_STARTALIGN;
1046 if (bus->self && size1 > size0 && realloc_head) {
1047 add_to_list(realloc_head, bus->self, b_res, size1-size0,
1049 pci_info(bus->self, "bridge window %pR to %pR add_size %llx\n",
1050 b_res, &bus->busn_res,
1051 (unsigned long long) size1 - size0);
1055 static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
1058 resource_size_t align = 0;
1059 resource_size_t min_align = 0;
1062 for (order = 0; order <= max_order; order++) {
1063 resource_size_t align1 = 1;
1065 align1 <<= order + __ffs(SZ_1M);
1069 else if (ALIGN(align + min_align, min_align) < align1)
1070 min_align = align1 >> 1;
1071 align += aligns[order];
1078 * pbus_upstream_space_available - Check no upstream resource limits allocation
1080 * @mask: Mask the resource flag, then compare it with type
1081 * @type: The type of resource from bridge
1082 * @size: The size required from the bridge window
1083 * @align: Required alignment for the resource
1085 * Checks that @size can fit inside the upstream bridge resources that are
1088 * Return: %true if enough space is available on all assigned upstream
1091 static bool pbus_upstream_space_available(struct pci_bus *bus, unsigned long mask,
1092 unsigned long type, resource_size_t size,
1093 resource_size_t align)
1095 struct resource_constraint constraint = {
1096 .max = RESOURCE_SIZE_MAX,
1099 struct pci_bus *downstream = bus;
1102 while ((bus = bus->parent)) {
1103 if (pci_is_root_bus(bus))
1106 pci_bus_for_each_resource(bus, r) {
1107 if (!r || !r->parent || (r->flags & mask) != type)
1110 if (resource_size(r) >= size) {
1111 struct resource gap = {};
1113 if (find_resource_space(r, &gap, size, &constraint) == 0) {
1116 "Assigned bridge window %pR to %pR free space at %pR\n",
1117 r, &bus->busn_res, &gap);
1124 "Assigned bridge window %pR to %pR cannot fit 0x%llx required for %s bridging to %pR\n",
1126 (unsigned long long)size,
1127 pci_name(downstream->self),
1128 &downstream->busn_res);
1139 * pbus_size_mem() - Size the memory window of a given bus
1142 * @mask: Mask the resource flag, then compare it with type
1143 * @type: The type of free resource from bridge
1144 * @type2: Second match type
1145 * @type3: Third match type
1146 * @min_size: The minimum memory window that must be allocated
1147 * @add_size: Additional optional memory window
1148 * @realloc_head: Track the additional memory window on this list
1150 * Calculate the size of the bus and minimal alignment which guarantees
1151 * that all child resources fit in this size.
1153 * Return -ENOSPC if there's no available bus resource of the desired
1154 * type. Otherwise, set the bus resource start/end to indicate the
1155 * required size, add things to realloc_head (if supplied), and return 0.
1157 static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
1158 unsigned long type, unsigned long type2,
1159 unsigned long type3, resource_size_t min_size,
1160 resource_size_t add_size,
1161 struct list_head *realloc_head)
1163 struct pci_dev *dev;
1164 resource_size_t min_align, win_align, align, size, size0, size1 = 0;
1165 resource_size_t aligns[28]; /* Alignments from 1MB to 128TB */
1166 int order, max_order;
1167 struct resource *b_res = find_bus_resource_of_type(bus,
1168 mask | IORESOURCE_PREFETCH, type);
1169 resource_size_t children_add_size = 0;
1170 resource_size_t children_add_align = 0;
1171 resource_size_t add_align = 0;
1176 /* If resource is already assigned, nothing more to do */
1180 memset(aligns, 0, sizeof(aligns));
1184 list_for_each_entry(dev, &bus->devices, bus_list) {
1188 pci_dev_for_each_resource(dev, r, i) {
1189 const char *r_name = pci_resource_name(dev, i);
1190 resource_size_t r_size;
1192 if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) ||
1193 ((r->flags & mask) != type &&
1194 (r->flags & mask) != type2 &&
1195 (r->flags & mask) != type3))
1197 r_size = resource_size(r);
1199 /* Put SRIOV requested res to the optional list */
1200 if (realloc_head && pci_resource_is_optional(dev, i)) {
1201 add_align = max(pci_resource_alignment(dev, r), add_align);
1202 add_to_list(realloc_head, dev, r, 0, 0 /* Don't care */);
1203 children_add_size += r_size;
1208 * aligns[0] is for 1MB (since bridge memory
1209 * windows are always at least 1MB aligned), so
1210 * keep "order" from being negative for smaller
1213 align = pci_resource_alignment(dev, r);
1214 order = __ffs(align) - __ffs(SZ_1M);
1217 if (order >= ARRAY_SIZE(aligns)) {
1218 pci_warn(dev, "%s %pR: disabling; bad alignment %#llx\n",
1219 r_name, r, (unsigned long long) align);
1223 size += max(r_size, align);
1225 * Exclude ranges with size > align from calculation of
1228 if (r_size <= align)
1229 aligns[order] += align;
1230 if (order > max_order)
1234 children_add_size += get_res_add_size(realloc_head, r);
1235 children_add_align = get_res_add_align(realloc_head, r);
1236 add_align = max(add_align, children_add_align);
1241 win_align = window_alignment(bus, b_res->flags);
1242 min_align = calculate_mem_align(aligns, max_order);
1243 min_align = max(min_align, win_align);
1244 size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), min_align);
1246 if (bus->self && size0 &&
1247 !pbus_upstream_space_available(bus, mask | IORESOURCE_PREFETCH, type,
1248 size0, min_align)) {
1249 min_align = 1ULL << (max_order + __ffs(SZ_1M));
1250 min_align = max(min_align, win_align);
1251 size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), win_align);
1252 pci_info(bus->self, "bridge window %pR to %pR requires relaxed alignment rules\n",
1253 b_res, &bus->busn_res);
1256 if (realloc_head && (add_size > 0 || children_add_size > 0)) {
1257 add_align = max(min_align, add_align);
1258 size1 = calculate_memsize(size, min_size, add_size, children_add_size,
1259 resource_size(b_res), add_align);
1261 if (bus->self && size1 &&
1262 !pbus_upstream_space_available(bus, mask | IORESOURCE_PREFETCH, type,
1263 size1, add_align)) {
1264 min_align = 1ULL << (max_order + __ffs(SZ_1M));
1265 min_align = max(min_align, win_align);
1266 size1 = calculate_memsize(size, min_size, add_size, children_add_size,
1267 resource_size(b_res), win_align);
1269 "bridge window %pR to %pR requires relaxed alignment rules\n",
1270 b_res, &bus->busn_res);
1274 if (!size0 && !size1) {
1275 if (bus->self && (b_res->start || b_res->end))
1276 pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
1277 b_res, &bus->busn_res);
1282 resource_set_range(b_res, min_align, size0);
1283 b_res->flags |= IORESOURCE_STARTALIGN;
1284 if (bus->self && size1 > size0 && realloc_head) {
1285 add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align);
1286 pci_info(bus->self, "bridge window %pR to %pR add_size %llx add_align %llx\n",
1287 b_res, &bus->busn_res,
1288 (unsigned long long) (size1 - size0),
1289 (unsigned long long) add_align);
1294 unsigned long pci_cardbus_resource_alignment(struct resource *res)
1296 if (res->flags & IORESOURCE_IO)
1297 return pci_cardbus_io_size;
1298 if (res->flags & IORESOURCE_MEM)
1299 return pci_cardbus_mem_size;
1303 static void pci_bus_size_cardbus(struct pci_bus *bus,
1304 struct list_head *realloc_head)
1306 struct pci_dev *bridge = bus->self;
1307 struct resource *b_res;
1308 resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
1311 b_res = &bridge->resource[PCI_CB_BRIDGE_IO_0_WINDOW];
1313 goto handle_b_res_1;
1315 * Reserve some resources for CardBus. We reserve a fixed amount
1316 * of bus space for CardBus bridges.
1318 resource_set_range(b_res, pci_cardbus_io_size, pci_cardbus_io_size);
1319 b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
1321 b_res->end -= pci_cardbus_io_size;
1322 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
1323 pci_cardbus_io_size);
1327 b_res = &bridge->resource[PCI_CB_BRIDGE_IO_1_WINDOW];
1329 goto handle_b_res_2;
1330 resource_set_range(b_res, pci_cardbus_io_size, pci_cardbus_io_size);
1331 b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
1333 b_res->end -= pci_cardbus_io_size;
1334 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
1335 pci_cardbus_io_size);
1339 /* MEM1 must not be pref MMIO */
1340 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1341 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
1342 ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
1343 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1344 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1347 /* Check whether prefetchable memory is supported by this bridge. */
1348 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1349 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
1350 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
1351 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1352 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1355 b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_0_WINDOW];
1357 goto handle_b_res_3;
1359 * If we have prefetchable memory support, allocate two regions.
1360 * Otherwise, allocate one region of twice the size.
1362 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
1363 resource_set_range(b_res, pci_cardbus_mem_size,
1364 pci_cardbus_mem_size);
1365 b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
1366 IORESOURCE_STARTALIGN;
1368 b_res->end -= pci_cardbus_mem_size;
1369 add_to_list(realloc_head, bridge, b_res,
1370 pci_cardbus_mem_size, pci_cardbus_mem_size);
1373 /* Reduce that to half */
1374 b_res_3_size = pci_cardbus_mem_size;
1378 b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_1_WINDOW];
1381 resource_set_range(b_res, pci_cardbus_mem_size, b_res_3_size);
1382 b_res->flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
1384 b_res->end -= b_res_3_size;
1385 add_to_list(realloc_head, bridge, b_res, b_res_3_size,
1386 pci_cardbus_mem_size);
1393 void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
1395 struct pci_dev *dev;
1396 unsigned long mask, prefmask, type2 = 0, type3 = 0;
1397 resource_size_t additional_io_size = 0, additional_mmio_size = 0,
1398 additional_mmio_pref_size = 0;
1399 struct resource *pref;
1400 struct pci_host_bridge *host;
1403 list_for_each_entry(dev, &bus->devices, bus_list) {
1404 struct pci_bus *b = dev->subordinate;
1408 switch (dev->hdr_type) {
1409 case PCI_HEADER_TYPE_CARDBUS:
1410 pci_bus_size_cardbus(b, realloc_head);
1413 case PCI_HEADER_TYPE_BRIDGE:
1415 __pci_bus_size_bridges(b, realloc_head);
1421 if (pci_is_root_bus(bus)) {
1422 host = to_pci_host_bridge(bus->bridge);
1423 if (!host->size_windows)
1425 pci_bus_for_each_resource(bus, pref)
1426 if (pref && (pref->flags & IORESOURCE_PREFETCH))
1428 hdr_type = -1; /* Intentionally invalid - not a PCI device. */
1430 pref = &bus->self->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
1431 hdr_type = bus->self->hdr_type;
1435 case PCI_HEADER_TYPE_CARDBUS:
1436 /* Don't size CardBuses yet */
1439 case PCI_HEADER_TYPE_BRIDGE:
1440 pci_bridge_check_ranges(bus);
1441 if (bus->self->is_hotplug_bridge) {
1442 additional_io_size = pci_hotplug_io_size;
1443 additional_mmio_size = pci_hotplug_mmio_size;
1444 additional_mmio_pref_size = pci_hotplug_mmio_pref_size;
1448 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
1449 additional_io_size, realloc_head);
1452 * If there's a 64-bit prefetchable MMIO window, compute
1453 * the size required to put all 64-bit prefetchable
1456 mask = IORESOURCE_MEM;
1457 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
1458 if (pref && (pref->flags & IORESOURCE_MEM_64)) {
1459 prefmask |= IORESOURCE_MEM_64;
1460 ret = pbus_size_mem(bus, prefmask, prefmask,
1462 realloc_head ? 0 : additional_mmio_pref_size,
1463 additional_mmio_pref_size, realloc_head);
1466 * If successful, all non-prefetchable resources
1467 * and any 32-bit prefetchable resources will go in
1468 * the non-prefetchable window.
1472 type2 = prefmask & ~IORESOURCE_MEM_64;
1473 type3 = prefmask & ~IORESOURCE_PREFETCH;
1478 * If there is no 64-bit prefetchable window, compute the
1479 * size required to put all prefetchable resources in the
1480 * 32-bit prefetchable window (if there is one).
1483 prefmask &= ~IORESOURCE_MEM_64;
1484 ret = pbus_size_mem(bus, prefmask, prefmask,
1486 realloc_head ? 0 : additional_mmio_pref_size,
1487 additional_mmio_pref_size, realloc_head);
1490 * If successful, only non-prefetchable resources
1491 * will go in the non-prefetchable window.
1496 additional_mmio_size += additional_mmio_pref_size;
1498 type2 = type3 = IORESOURCE_MEM;
1502 * Compute the size required to put everything else in the
1503 * non-prefetchable window. This includes:
1505 * - all non-prefetchable resources
1506 * - 32-bit prefetchable resources if there's a 64-bit
1507 * prefetchable window or no prefetchable window at all
1508 * - 64-bit prefetchable resources if there's no prefetchable
1511 * Note that the strategy in __pci_assign_resource() must match
1512 * that used here. Specifically, we cannot put a 32-bit
1513 * prefetchable resource in a 64-bit prefetchable window.
1515 pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
1516 realloc_head ? 0 : additional_mmio_size,
1517 additional_mmio_size, realloc_head);
1522 void pci_bus_size_bridges(struct pci_bus *bus)
1524 __pci_bus_size_bridges(bus, NULL);
1526 EXPORT_SYMBOL(pci_bus_size_bridges);
1528 static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r)
1530 struct resource *parent_r;
1531 unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM |
1532 IORESOURCE_PREFETCH;
1534 pci_bus_for_each_resource(b, parent_r) {
1538 if ((r->flags & mask) == (parent_r->flags & mask) &&
1539 resource_contains(parent_r, r))
1540 request_resource(parent_r, r);
1545 * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they are
1546 * skipped by pbus_assign_resources_sorted().
1548 static void pdev_assign_fixed_resources(struct pci_dev *dev)
1552 pci_dev_for_each_resource(dev, r) {
1555 if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) ||
1556 !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
1560 while (b && !r->parent) {
1561 assign_fixed_resource_on_bus(b, r);
1567 void __pci_bus_assign_resources(const struct pci_bus *bus,
1568 struct list_head *realloc_head,
1569 struct list_head *fail_head)
1572 struct pci_dev *dev;
1574 pbus_assign_resources_sorted(bus, realloc_head, fail_head);
1576 list_for_each_entry(dev, &bus->devices, bus_list) {
1577 pdev_assign_fixed_resources(dev);
1579 b = dev->subordinate;
1583 __pci_bus_assign_resources(b, realloc_head, fail_head);
1585 switch (dev->hdr_type) {
1586 case PCI_HEADER_TYPE_BRIDGE:
1587 if (!pci_is_enabled(dev))
1588 pci_setup_bridge(b);
1591 case PCI_HEADER_TYPE_CARDBUS:
1592 pci_setup_cardbus(b);
1596 pci_info(dev, "not setting up bridge for bus %04x:%02x\n",
1597 pci_domain_nr(b), b->number);
1603 void pci_bus_assign_resources(const struct pci_bus *bus)
1605 __pci_bus_assign_resources(bus, NULL, NULL);
1607 EXPORT_SYMBOL(pci_bus_assign_resources);
1609 static void pci_claim_device_resources(struct pci_dev *dev)
1613 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
1614 struct resource *r = &dev->resource[i];
1616 if (!r->flags || r->parent)
1619 pci_claim_resource(dev, i);
1623 static void pci_claim_bridge_resources(struct pci_dev *dev)
1627 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
1628 struct resource *r = &dev->resource[i];
1630 if (!r->flags || r->parent)
1633 pci_claim_bridge_resource(dev, i);
1637 static void pci_bus_allocate_dev_resources(struct pci_bus *b)
1639 struct pci_dev *dev;
1640 struct pci_bus *child;
1642 list_for_each_entry(dev, &b->devices, bus_list) {
1643 pci_claim_device_resources(dev);
1645 child = dev->subordinate;
1647 pci_bus_allocate_dev_resources(child);
1651 static void pci_bus_allocate_resources(struct pci_bus *b)
1653 struct pci_bus *child;
1656 * Carry out a depth-first search on the PCI bus tree to allocate
1657 * bridge apertures. Read the programmed bridge bases and
1658 * recursively claim the respective bridge resources.
1661 pci_read_bridge_bases(b);
1662 pci_claim_bridge_resources(b->self);
1665 list_for_each_entry(child, &b->children, node)
1666 pci_bus_allocate_resources(child);
1669 void pci_bus_claim_resources(struct pci_bus *b)
1671 pci_bus_allocate_resources(b);
1672 pci_bus_allocate_dev_resources(b);
1674 EXPORT_SYMBOL(pci_bus_claim_resources);
1676 static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
1677 struct list_head *add_head,
1678 struct list_head *fail_head)
1682 pdev_assign_resources_sorted((struct pci_dev *)bridge,
1683 add_head, fail_head);
1685 b = bridge->subordinate;
1689 __pci_bus_assign_resources(b, add_head, fail_head);
1691 switch (bridge->class >> 8) {
1692 case PCI_CLASS_BRIDGE_PCI:
1693 pci_setup_bridge(b);
1696 case PCI_CLASS_BRIDGE_CARDBUS:
1697 pci_setup_cardbus(b);
1701 pci_info(bridge, "not setting up bridge for bus %04x:%02x\n",
1702 pci_domain_nr(b), b->number);
1707 #define PCI_RES_TYPE_MASK \
1708 (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\
1711 static void pci_bridge_release_resources(struct pci_bus *bus,
1714 struct pci_dev *dev = bus->self;
1716 unsigned int old_flags;
1717 struct resource *b_res;
1720 b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
1723 * 1. If IO port assignment fails, release bridge IO port.
1724 * 2. If non pref MMIO assignment fails, release bridge nonpref MMIO.
1725 * 3. If 64bit pref MMIO assignment fails, and bridge pref is 64bit,
1726 * release bridge pref MMIO.
1727 * 4. If pref MMIO assignment fails, and bridge pref is 32bit,
1728 * release bridge pref MMIO.
1729 * 5. If pref MMIO assignment fails, and bridge pref is not
1730 * assigned, release bridge nonpref MMIO.
1732 if (type & IORESOURCE_IO)
1734 else if (!(type & IORESOURCE_PREFETCH))
1736 else if ((type & IORESOURCE_MEM_64) &&
1737 (b_res[2].flags & IORESOURCE_MEM_64))
1739 else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
1740 (b_res[2].flags & IORESOURCE_PREFETCH))
1750 /* If there are children, release them all */
1751 release_child_resources(r);
1752 if (!release_resource(r)) {
1753 type = old_flags = r->flags & PCI_RES_TYPE_MASK;
1754 pci_info(dev, "resource %d %pR released\n",
1755 PCI_BRIDGE_RESOURCES + idx, r);
1756 /* Keep the old size */
1757 resource_set_range(r, 0, resource_size(r));
1760 /* Avoiding touch the one without PREF */
1761 if (type & IORESOURCE_PREFETCH)
1762 type = IORESOURCE_PREFETCH;
1763 __pci_setup_bridge(bus, type);
1764 /* For next child res under same bridge */
1765 r->flags = old_flags;
1775 * Try to release PCI bridge resources from leaf bridge, so we can allocate
1776 * a larger window later.
1778 static void pci_bus_release_bridge_resources(struct pci_bus *bus,
1780 enum release_type rel_type)
1782 struct pci_dev *dev;
1783 bool is_leaf_bridge = true;
1785 list_for_each_entry(dev, &bus->devices, bus_list) {
1786 struct pci_bus *b = dev->subordinate;
1790 is_leaf_bridge = false;
1792 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1795 if (rel_type == whole_subtree)
1796 pci_bus_release_bridge_resources(b, type,
1800 if (pci_is_root_bus(bus))
1803 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1806 if ((rel_type == whole_subtree) || is_leaf_bridge)
1807 pci_bridge_release_resources(bus, type);
1810 static void pci_bus_dump_res(struct pci_bus *bus)
1812 struct resource *res;
1815 pci_bus_for_each_resource(bus, res, i) {
1816 if (!res || !res->end || !res->flags)
1819 dev_info(&bus->dev, "resource %d %pR\n", i, res);
1823 static void pci_bus_dump_resources(struct pci_bus *bus)
1826 struct pci_dev *dev;
1829 pci_bus_dump_res(bus);
1831 list_for_each_entry(dev, &bus->devices, bus_list) {
1832 b = dev->subordinate;
1836 pci_bus_dump_resources(b);
1840 static int pci_bus_get_depth(struct pci_bus *bus)
1843 struct pci_bus *child_bus;
1845 list_for_each_entry(child_bus, &bus->children, node) {
1848 ret = pci_bus_get_depth(child_bus);
1849 if (ret + 1 > depth)
1857 * -1: undefined, will auto detect later
1858 * 0: disabled by user
1859 * 1: disabled by auto detect
1860 * 2: enabled by user
1861 * 3: enabled by auto detect
1871 static enum enable_type pci_realloc_enable = undefined;
1872 void __init pci_realloc_get_opt(char *str)
1874 if (!strncmp(str, "off", 3))
1875 pci_realloc_enable = user_disabled;
1876 else if (!strncmp(str, "on", 2))
1877 pci_realloc_enable = user_enabled;
1879 static bool pci_realloc_enabled(enum enable_type enable)
1881 return enable >= user_enabled;
1884 #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1885 static int iov_resources_unassigned(struct pci_dev *dev, void *data)
1888 bool *unassigned = data;
1890 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1891 struct resource *r = &dev->resource[i + PCI_IOV_RESOURCES];
1892 struct pci_bus_region region;
1894 /* Not assigned or rejected by kernel? */
1898 pcibios_resource_to_bus(dev->bus, ®ion, r);
1899 if (!region.start) {
1901 return 1; /* Return early from pci_walk_bus() */
1908 static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1909 enum enable_type enable_local)
1911 bool unassigned = false;
1912 struct pci_host_bridge *host;
1914 if (enable_local != undefined)
1915 return enable_local;
1917 host = pci_find_host_bridge(bus);
1918 if (host->preserve_config)
1919 return auto_disabled;
1921 pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
1923 return auto_enabled;
1925 return enable_local;
1928 static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1929 enum enable_type enable_local)
1931 return enable_local;
1935 static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res,
1936 struct list_head *add_list,
1937 resource_size_t new_size)
1939 resource_size_t add_size, size = resource_size(res);
1947 if (new_size > size) {
1948 add_size = new_size - size;
1949 pci_dbg(bridge, "bridge window %pR extended by %pa\n", res,
1951 } else if (new_size < size) {
1952 add_size = size - new_size;
1953 pci_dbg(bridge, "bridge window %pR shrunken by %pa\n", res,
1959 resource_set_size(res, new_size);
1961 /* If the resource is part of the add_list, remove it now */
1963 remove_from_list(add_list, res);
1966 static void remove_dev_resource(struct resource *avail, struct pci_dev *dev,
1967 struct resource *res)
1969 resource_size_t size, align, tmp;
1971 size = resource_size(res);
1975 align = pci_resource_alignment(dev, res);
1976 align = align ? ALIGN(avail->start, align) - avail->start : 0;
1978 avail->start = min(avail->start + tmp, avail->end + 1);
1981 static void remove_dev_resources(struct pci_dev *dev, struct resource *io,
1982 struct resource *mmio,
1983 struct resource *mmio_pref)
1985 struct resource *res;
1987 pci_dev_for_each_resource(dev, res) {
1988 if (resource_type(res) == IORESOURCE_IO) {
1989 remove_dev_resource(io, dev, res);
1990 } else if (resource_type(res) == IORESOURCE_MEM) {
1993 * Make sure prefetchable memory is reduced from
1994 * the correct resource. Specifically we put 32-bit
1995 * prefetchable memory in non-prefetchable window
1996 * if there is a 64-bit prefetchable window.
1998 * See comments in __pci_bus_size_bridges() for
2001 if ((res->flags & IORESOURCE_PREFETCH) &&
2002 ((res->flags & IORESOURCE_MEM_64) ==
2003 (mmio_pref->flags & IORESOURCE_MEM_64)))
2004 remove_dev_resource(mmio_pref, dev, res);
2006 remove_dev_resource(mmio, dev, res);
2011 #define ALIGN_DOWN_IF_NONZERO(addr, align) \
2012 ((align) ? ALIGN_DOWN((addr), (align)) : (addr))
2015 * io, mmio and mmio_pref contain the total amount of bridge window space
2016 * available. This includes the minimal space needed to cover all the
2017 * existing devices on the bus and the possible extra space that can be
2018 * shared with the bridges.
2020 static void pci_bus_distribute_available_resources(struct pci_bus *bus,
2021 struct list_head *add_list,
2023 struct resource mmio,
2024 struct resource mmio_pref)
2026 unsigned int normal_bridges = 0, hotplug_bridges = 0;
2027 struct resource *io_res, *mmio_res, *mmio_pref_res;
2028 struct pci_dev *dev, *bridge = bus->self;
2029 resource_size_t io_per_b, mmio_per_b, mmio_pref_per_b, align;
2031 io_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
2032 mmio_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
2033 mmio_pref_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
2036 * The alignment of this bridge is yet to be considered, hence it must
2037 * be done now before extending its bridge window.
2039 align = pci_resource_alignment(bridge, io_res);
2040 if (!io_res->parent && align)
2041 io.start = min(ALIGN(io.start, align), io.end + 1);
2043 align = pci_resource_alignment(bridge, mmio_res);
2044 if (!mmio_res->parent && align)
2045 mmio.start = min(ALIGN(mmio.start, align), mmio.end + 1);
2047 align = pci_resource_alignment(bridge, mmio_pref_res);
2048 if (!mmio_pref_res->parent && align)
2049 mmio_pref.start = min(ALIGN(mmio_pref.start, align),
2053 * Now that we have adjusted for alignment, update the bridge window
2054 * resources to fill as much remaining resource space as possible.
2056 adjust_bridge_window(bridge, io_res, add_list, resource_size(&io));
2057 adjust_bridge_window(bridge, mmio_res, add_list, resource_size(&mmio));
2058 adjust_bridge_window(bridge, mmio_pref_res, add_list,
2059 resource_size(&mmio_pref));
2062 * Calculate how many hotplug bridges and normal bridges there
2063 * are on this bus. We will distribute the additional available
2064 * resources between hotplug bridges.
2066 for_each_pci_bridge(dev, bus) {
2067 if (dev->is_hotplug_bridge)
2073 if (!(hotplug_bridges + normal_bridges))
2077 * Calculate the amount of space we can forward from "bus" to any
2078 * downstream buses, i.e., the space left over after assigning the
2079 * BARs and windows on "bus".
2081 list_for_each_entry(dev, &bus->devices, bus_list) {
2082 if (!dev->is_virtfn)
2083 remove_dev_resources(dev, &io, &mmio, &mmio_pref);
2087 * If there is at least one hotplug bridge on this bus it gets all
2088 * the extra resource space that was left after the reductions
2091 * If there are no hotplug bridges the extra resource space is
2092 * split between non-hotplug bridges. This is to allow possible
2093 * hotplug bridges below them to get the extra space as well.
2095 if (hotplug_bridges) {
2096 io_per_b = div64_ul(resource_size(&io), hotplug_bridges);
2097 mmio_per_b = div64_ul(resource_size(&mmio), hotplug_bridges);
2098 mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
2101 io_per_b = div64_ul(resource_size(&io), normal_bridges);
2102 mmio_per_b = div64_ul(resource_size(&mmio), normal_bridges);
2103 mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
2107 for_each_pci_bridge(dev, bus) {
2108 struct resource *res;
2111 b = dev->subordinate;
2114 if (hotplug_bridges && !dev->is_hotplug_bridge)
2117 res = &dev->resource[PCI_BRIDGE_IO_WINDOW];
2120 * Make sure the split resource space is properly aligned
2121 * for bridge windows (align it down to avoid going above
2122 * what is available).
2124 align = pci_resource_alignment(dev, res);
2125 resource_set_size(&io, ALIGN_DOWN_IF_NONZERO(io_per_b, align));
2128 * The x_per_b holds the extra resource space that can be
2129 * added for each bridge but there is the minimal already
2130 * reserved as well so adjust x.start down accordingly to
2131 * cover the whole space.
2133 io.start -= resource_size(res);
2135 res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
2136 align = pci_resource_alignment(dev, res);
2137 resource_set_size(&mmio,
2138 ALIGN_DOWN_IF_NONZERO(mmio_per_b,align));
2139 mmio.start -= resource_size(res);
2141 res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
2142 align = pci_resource_alignment(dev, res);
2143 resource_set_size(&mmio_pref,
2144 ALIGN_DOWN_IF_NONZERO(mmio_pref_per_b, align));
2145 mmio_pref.start -= resource_size(res);
2147 pci_bus_distribute_available_resources(b, add_list, io, mmio,
2150 io.start += io.end + 1;
2151 mmio.start += mmio.end + 1;
2152 mmio_pref.start += mmio_pref.end + 1;
2156 static void pci_bridge_distribute_available_resources(struct pci_dev *bridge,
2157 struct list_head *add_list)
2159 struct resource available_io, available_mmio, available_mmio_pref;
2161 if (!bridge->is_hotplug_bridge)
2164 pci_dbg(bridge, "distributing available resources\n");
2166 /* Take the initial extra resources from the hotplug port */
2167 available_io = bridge->resource[PCI_BRIDGE_IO_WINDOW];
2168 available_mmio = bridge->resource[PCI_BRIDGE_MEM_WINDOW];
2169 available_mmio_pref = bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
2171 pci_bus_distribute_available_resources(bridge->subordinate,
2172 add_list, available_io,
2174 available_mmio_pref);
2177 static bool pci_bridge_resources_not_assigned(struct pci_dev *dev)
2179 const struct resource *r;
2182 * If the child device's resources are not yet assigned it means we
2183 * are configuring them (not the boot firmware), so we should be
2184 * able to extend the upstream bridge resources in the same way we
2185 * do with the normal hotplug case.
2187 r = &dev->resource[PCI_BRIDGE_IO_WINDOW];
2188 if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
2190 r = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
2191 if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
2193 r = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
2194 if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
2201 pci_root_bus_distribute_available_resources(struct pci_bus *bus,
2202 struct list_head *add_list)
2204 struct pci_dev *dev, *bridge = bus->self;
2206 for_each_pci_bridge(dev, bus) {
2209 b = dev->subordinate;
2214 * Need to check "bridge" here too because it is NULL
2215 * in case of root bus.
2217 if (bridge && pci_bridge_resources_not_assigned(dev))
2218 pci_bridge_distribute_available_resources(dev, add_list);
2220 pci_root_bus_distribute_available_resources(b, add_list);
2224 static void pci_prepare_next_assign_round(struct list_head *fail_head,
2226 enum release_type rel_type)
2228 struct pci_dev_resource *fail_res;
2230 pr_info("PCI: No. %d try to assign unassigned res\n", tried_times + 1);
2233 * Try to release leaf bridge's resources that aren't big
2234 * enough to contain child device resources.
2236 list_for_each_entry(fail_res, fail_head, list) {
2237 pci_bus_release_bridge_resources(fail_res->dev->bus,
2238 fail_res->flags & PCI_RES_TYPE_MASK,
2242 /* Restore size and flags */
2243 list_for_each_entry(fail_res, fail_head, list) {
2244 struct resource *res = fail_res->res;
2245 struct pci_dev *dev = fail_res->dev;
2246 int idx = pci_resource_num(dev, res);
2248 restore_dev_resource(fail_res);
2250 if (!pci_is_bridge(dev))
2253 if (idx >= PCI_BRIDGE_RESOURCES &&
2254 idx <= PCI_BRIDGE_RESOURCE_END)
2258 free_list(fail_head);
2262 * First try will not touch PCI bridge res.
2263 * Second and later try will clear small leaf bridge res.
2264 * Will stop till to the max depth if can not find good one.
2266 void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
2268 LIST_HEAD(realloc_head);
2269 /* List of resources that want additional resources */
2270 struct list_head *add_list = NULL;
2271 int tried_times = 0;
2272 enum release_type rel_type = leaf_only;
2273 LIST_HEAD(fail_head);
2274 int pci_try_num = 1;
2275 enum enable_type enable_local;
2277 /* Don't realloc if asked to do so */
2278 enable_local = pci_realloc_detect(bus, pci_realloc_enable);
2279 if (pci_realloc_enabled(enable_local)) {
2280 int max_depth = pci_bus_get_depth(bus);
2282 pci_try_num = max_depth + 1;
2283 dev_info(&bus->dev, "max bus depth: %d pci_try_num: %d\n",
2284 max_depth, pci_try_num);
2289 * Last try will use add_list, otherwise will try good to
2290 * have as must have, so can realloc parent bridge resource
2292 if (tried_times + 1 == pci_try_num)
2293 add_list = &realloc_head;
2295 * Depth first, calculate sizes and alignments of all
2296 * subordinate buses.
2298 __pci_bus_size_bridges(bus, add_list);
2300 pci_root_bus_distribute_available_resources(bus, add_list);
2302 /* Depth last, allocate resources and update the hardware. */
2303 __pci_bus_assign_resources(bus, add_list, &fail_head);
2304 if (WARN_ON_ONCE(add_list && !list_empty(add_list)))
2305 free_list(add_list);
2308 /* Any device complain? */
2309 if (list_empty(&fail_head))
2312 if (tried_times >= pci_try_num) {
2313 if (enable_local == undefined) {
2315 "Some PCI device resources are unassigned, try booting with pci=realloc\n");
2316 } else if (enable_local == auto_enabled) {
2318 "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
2320 free_list(&fail_head);
2324 /* Third times and later will not check if it is leaf */
2325 if (tried_times + 1 > 2)
2326 rel_type = whole_subtree;
2328 pci_prepare_next_assign_round(&fail_head, tried_times, rel_type);
2331 pci_bus_dump_resources(bus);
2334 void pci_assign_unassigned_resources(void)
2336 struct pci_bus *root_bus;
2338 list_for_each_entry(root_bus, &pci_root_buses, node) {
2339 pci_assign_unassigned_root_bus_resources(root_bus);
2341 /* Make sure the root bridge has a companion ACPI device */
2342 if (ACPI_HANDLE(root_bus->bridge))
2343 acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
2347 void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
2349 struct pci_bus *parent = bridge->subordinate;
2350 /* List of resources that want additional resources */
2351 LIST_HEAD(add_list);
2352 int tried_times = 0;
2353 LIST_HEAD(fail_head);
2357 __pci_bus_size_bridges(parent, &add_list);
2360 * Distribute remaining resources (if any) equally between
2361 * hotplug bridges below. This makes it possible to extend
2362 * the hierarchy later without running out of resources.
2364 pci_bridge_distribute_available_resources(bridge, &add_list);
2366 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
2367 if (WARN_ON_ONCE(!list_empty(&add_list)))
2368 free_list(&add_list);
2371 if (list_empty(&fail_head))
2374 if (tried_times >= 2) {
2375 /* Still fail, don't need to try more */
2376 free_list(&fail_head);
2380 pci_prepare_next_assign_round(&fail_head, tried_times,
2384 ret = pci_reenable_device(bridge);
2386 pci_err(bridge, "Error reenabling bridge (%d)\n", ret);
2387 pci_set_master(bridge);
2389 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
2391 int pci_reassign_bridge_resources(struct pci_dev *bridge, unsigned long type)
2393 struct pci_dev_resource *dev_res;
2394 struct pci_dev *next;
2401 down_read(&pci_bus_sem);
2403 /* Walk to the root hub, releasing bridge BARs when possible */
2407 for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCE_END;
2409 struct resource *res = &bridge->resource[i];
2410 const char *res_name = pci_resource_name(bridge, i);
2412 if ((res->flags ^ type) & PCI_RES_TYPE_MASK)
2415 /* Ignore BARs which are still in use */
2419 ret = add_to_list(&saved, bridge, res, 0, 0);
2423 pci_info(bridge, "%s %pR: releasing\n", res_name, res);
2426 release_resource(res);
2431 if (i == PCI_BRIDGE_RESOURCE_END)
2434 next = bridge->bus ? bridge->bus->self : NULL;
2437 if (list_empty(&saved)) {
2438 up_read(&pci_bus_sem);
2442 __pci_bus_size_bridges(bridge->subordinate, &added);
2443 __pci_bridge_assign_resources(bridge, &added, &failed);
2444 if (WARN_ON_ONCE(!list_empty(&added)))
2447 if (!list_empty(&failed)) {
2452 list_for_each_entry(dev_res, &saved, list) {
2453 /* Skip the bridge we just assigned resources for */
2454 if (bridge == dev_res->dev)
2457 bridge = dev_res->dev;
2458 pci_setup_bridge(bridge->subordinate);
2462 up_read(&pci_bus_sem);
2466 /* Restore size and flags */
2467 list_for_each_entry(dev_res, &failed, list)
2468 restore_dev_resource(dev_res);
2471 /* Revert to the old configuration */
2472 list_for_each_entry(dev_res, &saved, list) {
2473 struct resource *res = dev_res->res;
2475 bridge = dev_res->dev;
2476 i = pci_resource_num(bridge, res);
2478 restore_dev_resource(dev_res);
2480 pci_claim_resource(bridge, i);
2481 pci_setup_bridge(bridge->subordinate);
2484 up_read(&pci_bus_sem);
2489 void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
2491 struct pci_dev *dev;
2492 /* List of resources that want additional resources */
2493 LIST_HEAD(add_list);
2495 down_read(&pci_bus_sem);
2496 for_each_pci_bridge(dev, bus)
2497 if (pci_has_subordinate(dev))
2498 __pci_bus_size_bridges(dev->subordinate, &add_list);
2499 up_read(&pci_bus_sem);
2500 __pci_bus_assign_resources(bus, &add_list, NULL);
2501 if (WARN_ON_ONCE(!list_empty(&add_list)))
2502 free_list(&add_list);
2504 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);