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
59 static int add_to_list(struct list_head
*head
, struct pci_dev
*dev
,
60 struct resource
*res
, resource_size_t add_size
,
61 resource_size_t min_align
)
63 struct pci_dev_resource
*tmp
;
65 tmp
= kzalloc(sizeof(*tmp
), GFP_KERNEL
);
71 tmp
->start
= res
->start
;
73 tmp
->flags
= res
->flags
;
74 tmp
->add_size
= add_size
;
75 tmp
->min_align
= min_align
;
77 list_add(&tmp
->list
, head
);
82 static void remove_from_list(struct list_head
*head
, struct resource
*res
)
84 struct pci_dev_resource
*dev_res
, *tmp
;
86 list_for_each_entry_safe(dev_res
, tmp
, head
, list
) {
87 if (dev_res
->res
== res
) {
88 list_del(&dev_res
->list
);
95 static struct pci_dev_resource
*res_to_dev_res(struct list_head
*head
,
98 struct pci_dev_resource
*dev_res
;
100 list_for_each_entry(dev_res
, head
, list
) {
101 if (dev_res
->res
== res
)
108 static resource_size_t
get_res_add_size(struct list_head
*head
,
109 struct resource
*res
)
111 struct pci_dev_resource
*dev_res
;
113 dev_res
= res_to_dev_res(head
, res
);
114 return dev_res
? dev_res
->add_size
: 0;
117 static resource_size_t
get_res_add_align(struct list_head
*head
,
118 struct resource
*res
)
120 struct pci_dev_resource
*dev_res
;
122 dev_res
= res_to_dev_res(head
, res
);
123 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
)
132 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
134 struct pci_dev_resource
*dev_res
, *tmp
;
135 resource_size_t r_align
;
138 r
= &dev
->resource
[i
];
140 if (r
->flags
& IORESOURCE_PCI_FIXED
)
143 if (!(r
->flags
) || r
->parent
)
146 r_align
= pci_resource_alignment(dev
, r
);
148 pci_warn(dev
, "BAR %d: %pR has bogus alignment\n",
153 tmp
= kzalloc(sizeof(*tmp
), GFP_KERNEL
);
155 panic("pdev_sort_resources(): kmalloc() failed!\n");
159 /* Fallback is smallest one or list is empty */
161 list_for_each_entry(dev_res
, head
, list
) {
162 resource_size_t align
;
164 align
= pci_resource_alignment(dev_res
->dev
,
167 if (r_align
> align
) {
172 /* Insert it just before n */
173 list_add_tail(&tmp
->list
, n
);
177 static void __dev_sort_resources(struct pci_dev
*dev
, struct list_head
*head
)
179 u16
class = dev
->class >> 8;
181 /* Don't touch classless devices or host bridges or IOAPICs */
182 if (class == PCI_CLASS_NOT_DEFINED
|| class == PCI_CLASS_BRIDGE_HOST
)
185 /* Don't touch IOAPIC devices already enabled by firmware */
186 if (class == PCI_CLASS_SYSTEM_PIC
) {
188 pci_read_config_word(dev
, PCI_COMMAND
, &command
);
189 if (command
& (PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
))
193 pdev_sort_resources(dev
, head
);
196 static inline void reset_resource(struct resource
*res
)
204 * reassign_resources_sorted() - Satisfy any additional resource requests
206 * @realloc_head: Head of the list tracking requests requiring
207 * additional resources
208 * @head: Head of the list tracking requests with allocated
211 * Walk through each element of the realloc_head and try to procure additional
212 * resources for the element, provided the element is in the head list.
214 static void reassign_resources_sorted(struct list_head
*realloc_head
,
215 struct list_head
*head
)
217 struct resource
*res
;
218 struct pci_dev_resource
*add_res
, *tmp
;
219 struct pci_dev_resource
*dev_res
;
220 resource_size_t add_size
, align
;
223 list_for_each_entry_safe(add_res
, tmp
, realloc_head
, list
) {
224 bool found_match
= false;
227 /* Skip resource that has been reset */
231 /* Skip this resource if not found in head list */
232 list_for_each_entry(dev_res
, head
, list
) {
233 if (dev_res
->res
== res
) {
238 if (!found_match
) /* Just skip */
241 idx
= res
- &add_res
->dev
->resource
[0];
242 add_size
= add_res
->add_size
;
243 align
= add_res
->min_align
;
244 if (!resource_size(res
)) {
246 res
->end
= res
->start
+ add_size
- 1;
247 if (pci_assign_resource(add_res
->dev
, idx
))
250 res
->flags
|= add_res
->flags
&
251 (IORESOURCE_STARTALIGN
|IORESOURCE_SIZEALIGN
);
252 if (pci_reassign_resource(add_res
->dev
, idx
,
254 pci_info(add_res
->dev
, "failed to add %llx res[%d]=%pR\n",
255 (unsigned long long) add_size
, idx
,
259 list_del(&add_res
->list
);
265 * assign_requested_resources_sorted() - Satisfy resource requests
267 * @head: Head of the list tracking requests for resources
268 * @fail_head: Head of the list tracking requests that could not be
271 * Satisfy resource requests of each element in the list. Add requests that
272 * could not be satisfied to the failed_list.
274 static void assign_requested_resources_sorted(struct list_head
*head
,
275 struct list_head
*fail_head
)
277 struct resource
*res
;
278 struct pci_dev_resource
*dev_res
;
281 list_for_each_entry(dev_res
, head
, list
) {
283 idx
= res
- &dev_res
->dev
->resource
[0];
284 if (resource_size(res
) &&
285 pci_assign_resource(dev_res
->dev
, idx
)) {
288 * If the failed resource is a ROM BAR and
289 * it will be enabled later, don't add it
292 if (!((idx
== PCI_ROM_RESOURCE
) &&
293 (!(res
->flags
& IORESOURCE_ROM_ENABLE
))))
294 add_to_list(fail_head
,
304 static unsigned long pci_fail_res_type_mask(struct list_head
*fail_head
)
306 struct pci_dev_resource
*fail_res
;
307 unsigned long mask
= 0;
309 /* Check failed type */
310 list_for_each_entry(fail_res
, fail_head
, list
)
311 mask
|= fail_res
->flags
;
314 * One pref failed resource will set IORESOURCE_MEM, as we can
315 * allocate pref in non-pref range. Will release all assigned
316 * non-pref sibling resources according to that bit.
318 return mask
& (IORESOURCE_IO
| IORESOURCE_MEM
| IORESOURCE_PREFETCH
);
321 static bool pci_need_to_release(unsigned long mask
, struct resource
*res
)
323 if (res
->flags
& IORESOURCE_IO
)
324 return !!(mask
& IORESOURCE_IO
);
326 /* Check pref at first */
327 if (res
->flags
& IORESOURCE_PREFETCH
) {
328 if (mask
& IORESOURCE_PREFETCH
)
330 /* Count pref if its parent is non-pref */
331 else if ((mask
& IORESOURCE_MEM
) &&
332 !(res
->parent
->flags
& IORESOURCE_PREFETCH
))
338 if (res
->flags
& IORESOURCE_MEM
)
339 return !!(mask
& IORESOURCE_MEM
);
341 return false; /* Should not get here */
344 static void __assign_resources_sorted(struct list_head
*head
,
345 struct list_head
*realloc_head
,
346 struct list_head
*fail_head
)
349 * Should not assign requested resources at first. They could be
350 * adjacent, so later reassign can not reallocate them one by one in
351 * parent resource window.
353 * Try to assign requested + add_size at beginning. If could do that,
354 * could get out early. If could not do that, we still try to assign
355 * requested at first, then try to reassign add_size for some resources.
357 * Separate three resource type checking if we need to release
358 * assigned resource after requested + add_size try.
360 * 1. If IO port assignment fails, will release assigned IO
362 * 2. If pref MMIO assignment fails, release assigned pref
363 * MMIO. If assigned pref MMIO's parent is non-pref MMIO
364 * and non-pref MMIO assignment fails, will release that
365 * assigned pref MMIO.
366 * 3. If non-pref MMIO assignment fails or pref MMIO
367 * assignment fails, will release assigned non-pref MMIO.
369 LIST_HEAD(save_head
);
370 LIST_HEAD(local_fail_head
);
371 struct pci_dev_resource
*save_res
;
372 struct pci_dev_resource
*dev_res
, *tmp_res
, *dev_res2
;
373 unsigned long fail_type
;
374 resource_size_t add_align
, align
;
376 /* Check if optional add_size is there */
377 if (!realloc_head
|| list_empty(realloc_head
))
378 goto requested_and_reassign
;
380 /* Save original start, end, flags etc at first */
381 list_for_each_entry(dev_res
, head
, list
) {
382 if (add_to_list(&save_head
, dev_res
->dev
, dev_res
->res
, 0, 0)) {
383 free_list(&save_head
);
384 goto requested_and_reassign
;
388 /* Update res in head list with add_size in realloc_head list */
389 list_for_each_entry_safe(dev_res
, tmp_res
, head
, list
) {
390 dev_res
->res
->end
+= get_res_add_size(realloc_head
,
394 * There are two kinds of additional resources in the list:
395 * 1. bridge resource -- IORESOURCE_STARTALIGN
396 * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN
397 * Here just fix the additional alignment for bridge
399 if (!(dev_res
->res
->flags
& IORESOURCE_STARTALIGN
))
402 add_align
= get_res_add_align(realloc_head
, dev_res
->res
);
405 * The "head" list is sorted by alignment so resources with
406 * bigger alignment will be assigned first. After we
407 * change the alignment of a dev_res in "head" list, we
408 * need to reorder the list by alignment to make it
411 if (add_align
> dev_res
->res
->start
) {
412 resource_size_t r_size
= resource_size(dev_res
->res
);
414 dev_res
->res
->start
= add_align
;
415 dev_res
->res
->end
= add_align
+ r_size
- 1;
417 list_for_each_entry(dev_res2
, head
, list
) {
418 align
= pci_resource_alignment(dev_res2
->dev
,
420 if (add_align
> align
) {
421 list_move_tail(&dev_res
->list
,
430 /* Try updated head list with add_size added */
431 assign_requested_resources_sorted(head
, &local_fail_head
);
433 /* All assigned with add_size? */
434 if (list_empty(&local_fail_head
)) {
435 /* Remove head list from realloc_head list */
436 list_for_each_entry(dev_res
, head
, list
)
437 remove_from_list(realloc_head
, dev_res
->res
);
438 free_list(&save_head
);
443 /* Check failed type */
444 fail_type
= pci_fail_res_type_mask(&local_fail_head
);
445 /* Remove not need to be released assigned res from head list etc */
446 list_for_each_entry_safe(dev_res
, tmp_res
, head
, list
)
447 if (dev_res
->res
->parent
&&
448 !pci_need_to_release(fail_type
, dev_res
->res
)) {
449 /* Remove it from realloc_head list */
450 remove_from_list(realloc_head
, dev_res
->res
);
451 remove_from_list(&save_head
, dev_res
->res
);
452 list_del(&dev_res
->list
);
456 free_list(&local_fail_head
);
457 /* Release assigned resource */
458 list_for_each_entry(dev_res
, head
, list
)
459 if (dev_res
->res
->parent
)
460 release_resource(dev_res
->res
);
461 /* Restore start/end/flags from saved list */
462 list_for_each_entry(save_res
, &save_head
, list
) {
463 struct resource
*res
= save_res
->res
;
465 res
->start
= save_res
->start
;
466 res
->end
= save_res
->end
;
467 res
->flags
= save_res
->flags
;
469 free_list(&save_head
);
471 requested_and_reassign
:
472 /* Satisfy the must-have resource requests */
473 assign_requested_resources_sorted(head
, fail_head
);
475 /* Try to satisfy any additional optional resource requests */
477 reassign_resources_sorted(realloc_head
, head
);
481 static void pdev_assign_resources_sorted(struct pci_dev
*dev
,
482 struct list_head
*add_head
,
483 struct list_head
*fail_head
)
487 __dev_sort_resources(dev
, &head
);
488 __assign_resources_sorted(&head
, add_head
, fail_head
);
492 static void pbus_assign_resources_sorted(const struct pci_bus
*bus
,
493 struct list_head
*realloc_head
,
494 struct list_head
*fail_head
)
499 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
500 __dev_sort_resources(dev
, &head
);
502 __assign_resources_sorted(&head
, realloc_head
, fail_head
);
505 void pci_setup_cardbus(struct pci_bus
*bus
)
507 struct pci_dev
*bridge
= bus
->self
;
508 struct resource
*res
;
509 struct pci_bus_region region
;
511 pci_info(bridge
, "CardBus bridge to %pR\n",
514 res
= bus
->resource
[0];
515 pcibios_resource_to_bus(bridge
->bus
, ®ion
, res
);
516 if (res
->flags
& IORESOURCE_IO
) {
518 * The IO resource is allocated a range twice as large as it
519 * would normally need. This allows us to set both IO regs.
521 pci_info(bridge
, " bridge window %pR\n", res
);
522 pci_write_config_dword(bridge
, PCI_CB_IO_BASE_0
,
524 pci_write_config_dword(bridge
, PCI_CB_IO_LIMIT_0
,
528 res
= bus
->resource
[1];
529 pcibios_resource_to_bus(bridge
->bus
, ®ion
, res
);
530 if (res
->flags
& IORESOURCE_IO
) {
531 pci_info(bridge
, " bridge window %pR\n", res
);
532 pci_write_config_dword(bridge
, PCI_CB_IO_BASE_1
,
534 pci_write_config_dword(bridge
, PCI_CB_IO_LIMIT_1
,
538 res
= bus
->resource
[2];
539 pcibios_resource_to_bus(bridge
->bus
, ®ion
, res
);
540 if (res
->flags
& IORESOURCE_MEM
) {
541 pci_info(bridge
, " bridge window %pR\n", res
);
542 pci_write_config_dword(bridge
, PCI_CB_MEMORY_BASE_0
,
544 pci_write_config_dword(bridge
, PCI_CB_MEMORY_LIMIT_0
,
548 res
= bus
->resource
[3];
549 pcibios_resource_to_bus(bridge
->bus
, ®ion
, res
);
550 if (res
->flags
& IORESOURCE_MEM
) {
551 pci_info(bridge
, " bridge window %pR\n", res
);
552 pci_write_config_dword(bridge
, PCI_CB_MEMORY_BASE_1
,
554 pci_write_config_dword(bridge
, PCI_CB_MEMORY_LIMIT_1
,
558 EXPORT_SYMBOL(pci_setup_cardbus
);
561 * Initialize bridges with base/limit values we have collected. PCI-to-PCI
562 * Bridge Architecture Specification rev. 1.1 (1998) requires that if there
563 * are no I/O ports or memory behind the bridge, the corresponding range
564 * must be turned off by writing base value greater than limit to the
565 * bridge's base/limit registers.
567 * Note: care must be taken when updating I/O base/limit registers of
568 * bridges which support 32-bit I/O. This update requires two config space
569 * writes, so it's quite possible that an I/O window of the bridge will
570 * have some undesirable address (e.g. 0) after the first write. Ditto
571 * 64-bit prefetchable MMIO.
573 static void pci_setup_bridge_io(struct pci_dev
*bridge
)
575 struct resource
*res
;
576 struct pci_bus_region region
;
577 unsigned long io_mask
;
578 u8 io_base_lo
, io_limit_lo
;
582 io_mask
= PCI_IO_RANGE_MASK
;
583 if (bridge
->io_window_1k
)
584 io_mask
= PCI_IO_1K_RANGE_MASK
;
586 /* Set up the top and bottom of the PCI I/O segment for this bus */
587 res
= &bridge
->resource
[PCI_BRIDGE_IO_WINDOW
];
588 pcibios_resource_to_bus(bridge
->bus
, ®ion
, res
);
589 if (res
->flags
& IORESOURCE_IO
) {
590 pci_read_config_word(bridge
, PCI_IO_BASE
, &l
);
591 io_base_lo
= (region
.start
>> 8) & io_mask
;
592 io_limit_lo
= (region
.end
>> 8) & io_mask
;
593 l
= ((u16
) io_limit_lo
<< 8) | io_base_lo
;
594 /* Set up upper 16 bits of I/O base/limit */
595 io_upper16
= (region
.end
& 0xffff0000) | (region
.start
>> 16);
596 pci_info(bridge
, " bridge window %pR\n", res
);
598 /* Clear upper 16 bits of I/O base/limit */
602 /* Temporarily disable the I/O range before updating PCI_IO_BASE */
603 pci_write_config_dword(bridge
, PCI_IO_BASE_UPPER16
, 0x0000ffff);
604 /* Update lower 16 bits of I/O base/limit */
605 pci_write_config_word(bridge
, PCI_IO_BASE
, l
);
606 /* Update upper 16 bits of I/O base/limit */
607 pci_write_config_dword(bridge
, PCI_IO_BASE_UPPER16
, io_upper16
);
610 static void pci_setup_bridge_mmio(struct pci_dev
*bridge
)
612 struct resource
*res
;
613 struct pci_bus_region region
;
616 /* Set up the top and bottom of the PCI Memory segment for this bus */
617 res
= &bridge
->resource
[PCI_BRIDGE_MEM_WINDOW
];
618 pcibios_resource_to_bus(bridge
->bus
, ®ion
, res
);
619 if (res
->flags
& IORESOURCE_MEM
) {
620 l
= (region
.start
>> 16) & 0xfff0;
621 l
|= region
.end
& 0xfff00000;
622 pci_info(bridge
, " bridge window %pR\n", res
);
626 pci_write_config_dword(bridge
, PCI_MEMORY_BASE
, l
);
629 static void pci_setup_bridge_mmio_pref(struct pci_dev
*bridge
)
631 struct resource
*res
;
632 struct pci_bus_region region
;
636 * Clear out the upper 32 bits of PREF limit. If
637 * PCI_PREF_BASE_UPPER32 was non-zero, this temporarily disables
638 * PREF range, which is ok.
640 pci_write_config_dword(bridge
, PCI_PREF_LIMIT_UPPER32
, 0);
642 /* Set up PREF base/limit */
644 res
= &bridge
->resource
[PCI_BRIDGE_PREF_MEM_WINDOW
];
645 pcibios_resource_to_bus(bridge
->bus
, ®ion
, res
);
646 if (res
->flags
& IORESOURCE_PREFETCH
) {
647 l
= (region
.start
>> 16) & 0xfff0;
648 l
|= region
.end
& 0xfff00000;
649 if (res
->flags
& IORESOURCE_MEM_64
) {
650 bu
= upper_32_bits(region
.start
);
651 lu
= upper_32_bits(region
.end
);
653 pci_info(bridge
, " bridge window %pR\n", res
);
657 pci_write_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, l
);
659 /* Set the upper 32 bits of PREF base & limit */
660 pci_write_config_dword(bridge
, PCI_PREF_BASE_UPPER32
, bu
);
661 pci_write_config_dword(bridge
, PCI_PREF_LIMIT_UPPER32
, lu
);
664 static void __pci_setup_bridge(struct pci_bus
*bus
, unsigned long type
)
666 struct pci_dev
*bridge
= bus
->self
;
668 pci_info(bridge
, "PCI bridge to %pR\n",
671 if (type
& IORESOURCE_IO
)
672 pci_setup_bridge_io(bridge
);
674 if (type
& IORESOURCE_MEM
)
675 pci_setup_bridge_mmio(bridge
);
677 if (type
& IORESOURCE_PREFETCH
)
678 pci_setup_bridge_mmio_pref(bridge
);
680 pci_write_config_word(bridge
, PCI_BRIDGE_CONTROL
, bus
->bridge_ctl
);
683 void __weak
pcibios_setup_bridge(struct pci_bus
*bus
, unsigned long type
)
687 void pci_setup_bridge(struct pci_bus
*bus
)
689 unsigned long type
= IORESOURCE_IO
| IORESOURCE_MEM
|
692 pcibios_setup_bridge(bus
, type
);
693 __pci_setup_bridge(bus
, type
);
697 int pci_claim_bridge_resource(struct pci_dev
*bridge
, int i
)
699 if (i
< PCI_BRIDGE_RESOURCES
|| i
> PCI_BRIDGE_RESOURCE_END
)
702 if (pci_claim_resource(bridge
, i
) == 0)
703 return 0; /* Claimed the window */
705 if ((bridge
->class >> 8) != PCI_CLASS_BRIDGE_PCI
)
708 if (!pci_bus_clip_resource(bridge
, i
))
709 return -EINVAL
; /* Clipping didn't change anything */
712 case PCI_BRIDGE_IO_WINDOW
:
713 pci_setup_bridge_io(bridge
);
715 case PCI_BRIDGE_MEM_WINDOW
:
716 pci_setup_bridge_mmio(bridge
);
718 case PCI_BRIDGE_PREF_MEM_WINDOW
:
719 pci_setup_bridge_mmio_pref(bridge
);
725 if (pci_claim_resource(bridge
, i
) == 0)
726 return 0; /* Claimed a smaller window */
732 * Check whether the bridge supports optional I/O and prefetchable memory
733 * ranges. If not, the respective base/limit registers must be read-only
736 static void pci_bridge_check_ranges(struct pci_bus
*bus
)
738 struct pci_dev
*bridge
= bus
->self
;
739 struct resource
*b_res
;
741 b_res
= &bridge
->resource
[PCI_BRIDGE_MEM_WINDOW
];
742 b_res
->flags
|= IORESOURCE_MEM
;
744 if (bridge
->io_window
) {
745 b_res
= &bridge
->resource
[PCI_BRIDGE_IO_WINDOW
];
746 b_res
->flags
|= IORESOURCE_IO
;
749 if (bridge
->pref_window
) {
750 b_res
= &bridge
->resource
[PCI_BRIDGE_PREF_MEM_WINDOW
];
751 b_res
->flags
|= IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
752 if (bridge
->pref_64_window
) {
753 b_res
->flags
|= IORESOURCE_MEM_64
|
754 PCI_PREF_RANGE_TYPE_64
;
760 * Helper function for sizing routines. Assigned resources have non-NULL
763 * Return first unassigned resource of the correct type. If there is none,
764 * return first assigned resource of the correct type. If none of the
765 * above, return NULL.
767 * Returning an assigned resource of the correct type allows the caller to
768 * distinguish between already assigned and no resource of the correct type.
770 static struct resource
*find_bus_resource_of_type(struct pci_bus
*bus
,
771 unsigned long type_mask
,
774 struct resource
*r
, *r_assigned
= NULL
;
777 pci_bus_for_each_resource(bus
, r
, i
) {
778 if (r
== &ioport_resource
|| r
== &iomem_resource
)
780 if (r
&& (r
->flags
& type_mask
) == type
&& !r
->parent
)
782 if (r
&& (r
->flags
& type_mask
) == type
&& !r_assigned
)
788 static resource_size_t
calculate_iosize(resource_size_t size
,
789 resource_size_t min_size
,
790 resource_size_t size1
,
791 resource_size_t add_size
,
792 resource_size_t children_add_size
,
793 resource_size_t old_size
,
794 resource_size_t align
)
801 * To be fixed in 2.5: we should have sort of HAVE_ISA flag in the
804 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
805 size
= (size
& 0xff) + ((size
& ~0xffUL
) << 2);
811 size
= ALIGN(max(size
, add_size
) + children_add_size
, align
);
815 static resource_size_t
calculate_memsize(resource_size_t size
,
816 resource_size_t min_size
,
817 resource_size_t add_size
,
818 resource_size_t children_add_size
,
819 resource_size_t old_size
,
820 resource_size_t align
)
829 size
= ALIGN(max(size
, add_size
) + children_add_size
, align
);
833 resource_size_t __weak
pcibios_window_alignment(struct pci_bus
*bus
,
839 #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */
840 #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */
841 #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */
843 static resource_size_t
window_alignment(struct pci_bus
*bus
, unsigned long type
)
845 resource_size_t align
= 1, arch_align
;
847 if (type
& IORESOURCE_MEM
)
848 align
= PCI_P2P_DEFAULT_MEM_ALIGN
;
849 else if (type
& IORESOURCE_IO
) {
851 * Per spec, I/O windows are 4K-aligned, but some bridges have
852 * an extension to support 1K alignment.
854 if (bus
->self
&& bus
->self
->io_window_1k
)
855 align
= PCI_P2P_DEFAULT_IO_ALIGN_1K
;
857 align
= PCI_P2P_DEFAULT_IO_ALIGN
;
860 arch_align
= pcibios_window_alignment(bus
, type
);
861 return max(align
, arch_align
);
865 * pbus_size_io() - Size the I/O window of a given bus
868 * @min_size: The minimum I/O window that must be allocated
869 * @add_size: Additional optional I/O window
870 * @realloc_head: Track the additional I/O window on this list
872 * Sizing the I/O windows of the PCI-PCI bridge is trivial, since these
873 * windows have 1K or 4K granularity and the I/O ranges of non-bridge PCI
874 * devices are limited to 256 bytes. We must be careful with the ISA
877 static void pbus_size_io(struct pci_bus
*bus
, resource_size_t min_size
,
878 resource_size_t add_size
,
879 struct list_head
*realloc_head
)
882 struct resource
*b_res
= find_bus_resource_of_type(bus
, IORESOURCE_IO
,
884 resource_size_t size
= 0, size0
= 0, size1
= 0;
885 resource_size_t children_add_size
= 0;
886 resource_size_t min_align
, align
;
891 /* If resource is already assigned, nothing more to do */
895 min_align
= window_alignment(bus
, IORESOURCE_IO
);
896 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
899 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
900 struct resource
*r
= &dev
->resource
[i
];
901 unsigned long r_size
;
903 if (r
->parent
|| !(r
->flags
& IORESOURCE_IO
))
905 r_size
= resource_size(r
);
908 /* Might be re-aligned for ISA */
913 align
= pci_resource_alignment(dev
, r
);
914 if (align
> min_align
)
918 children_add_size
+= get_res_add_size(realloc_head
, r
);
922 size0
= calculate_iosize(size
, min_size
, size1
, 0, 0,
923 resource_size(b_res
), min_align
);
924 size1
= (!realloc_head
|| (realloc_head
&& !add_size
&& !children_add_size
)) ? size0
:
925 calculate_iosize(size
, min_size
, size1
, add_size
, children_add_size
,
926 resource_size(b_res
), min_align
);
927 if (!size0
&& !size1
) {
928 if (bus
->self
&& (b_res
->start
|| b_res
->end
))
929 pci_info(bus
->self
, "disabling bridge window %pR to %pR (unused)\n",
930 b_res
, &bus
->busn_res
);
935 b_res
->start
= min_align
;
936 b_res
->end
= b_res
->start
+ size0
- 1;
937 b_res
->flags
|= IORESOURCE_STARTALIGN
;
938 if (bus
->self
&& size1
> size0
&& realloc_head
) {
939 add_to_list(realloc_head
, bus
->self
, b_res
, size1
-size0
,
941 pci_info(bus
->self
, "bridge window %pR to %pR add_size %llx\n",
942 b_res
, &bus
->busn_res
,
943 (unsigned long long) size1
- size0
);
947 static inline resource_size_t
calculate_mem_align(resource_size_t
*aligns
,
950 resource_size_t align
= 0;
951 resource_size_t min_align
= 0;
954 for (order
= 0; order
<= max_order
; order
++) {
955 resource_size_t align1
= 1;
957 align1
<<= (order
+ 20);
961 else if (ALIGN(align
+ min_align
, min_align
) < align1
)
962 min_align
= align1
>> 1;
963 align
+= aligns
[order
];
970 * pbus_size_mem() - Size the memory window of a given bus
973 * @mask: Mask the resource flag, then compare it with type
974 * @type: The type of free resource from bridge
975 * @type2: Second match type
976 * @type3: Third match type
977 * @min_size: The minimum memory window that must be allocated
978 * @add_size: Additional optional memory window
979 * @realloc_head: Track the additional memory window on this list
981 * Calculate the size of the bus and minimal alignment which guarantees
982 * that all child resources fit in this size.
984 * Return -ENOSPC if there's no available bus resource of the desired
985 * type. Otherwise, set the bus resource start/end to indicate the
986 * required size, add things to realloc_head (if supplied), and return 0.
988 static int pbus_size_mem(struct pci_bus
*bus
, unsigned long mask
,
989 unsigned long type
, unsigned long type2
,
990 unsigned long type3
, resource_size_t min_size
,
991 resource_size_t add_size
,
992 struct list_head
*realloc_head
)
995 resource_size_t min_align
, align
, size
, size0
, size1
;
996 resource_size_t aligns
[18]; /* Alignments from 1MB to 128GB */
997 int order
, max_order
;
998 struct resource
*b_res
= find_bus_resource_of_type(bus
,
999 mask
| IORESOURCE_PREFETCH
, type
);
1000 resource_size_t children_add_size
= 0;
1001 resource_size_t children_add_align
= 0;
1002 resource_size_t add_align
= 0;
1007 /* If resource is already assigned, nothing more to do */
1011 memset(aligns
, 0, sizeof(aligns
));
1015 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1018 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
1019 struct resource
*r
= &dev
->resource
[i
];
1020 resource_size_t r_size
;
1022 if (r
->parent
|| (r
->flags
& IORESOURCE_PCI_FIXED
) ||
1023 ((r
->flags
& mask
) != type
&&
1024 (r
->flags
& mask
) != type2
&&
1025 (r
->flags
& mask
) != type3
))
1027 r_size
= resource_size(r
);
1028 #ifdef CONFIG_PCI_IOV
1029 /* Put SRIOV requested res to the optional list */
1030 if (realloc_head
&& i
>= PCI_IOV_RESOURCES
&&
1031 i
<= PCI_IOV_RESOURCE_END
) {
1032 add_align
= max(pci_resource_alignment(dev
, r
), add_align
);
1033 r
->end
= r
->start
- 1;
1034 add_to_list(realloc_head
, dev
, r
, r_size
, 0 /* Don't care */);
1035 children_add_size
+= r_size
;
1040 * aligns[0] is for 1MB (since bridge memory
1041 * windows are always at least 1MB aligned), so
1042 * keep "order" from being negative for smaller
1045 align
= pci_resource_alignment(dev
, r
);
1046 order
= __ffs(align
) - 20;
1049 if (order
>= ARRAY_SIZE(aligns
)) {
1050 pci_warn(dev
, "disabling BAR %d: %pR (bad alignment %#llx)\n",
1051 i
, r
, (unsigned long long) align
);
1055 size
+= max(r_size
, align
);
1057 * Exclude ranges with size > align from calculation of
1060 if (r_size
<= align
)
1061 aligns
[order
] += align
;
1062 if (order
> max_order
)
1066 children_add_size
+= get_res_add_size(realloc_head
, r
);
1067 children_add_align
= get_res_add_align(realloc_head
, r
);
1068 add_align
= max(add_align
, children_add_align
);
1073 min_align
= calculate_mem_align(aligns
, max_order
);
1074 min_align
= max(min_align
, window_alignment(bus
, b_res
->flags
));
1075 size0
= calculate_memsize(size
, min_size
, 0, 0, resource_size(b_res
), min_align
);
1076 add_align
= max(min_align
, add_align
);
1077 size1
= (!realloc_head
|| (realloc_head
&& !add_size
&& !children_add_size
)) ? size0
:
1078 calculate_memsize(size
, min_size
, add_size
, children_add_size
,
1079 resource_size(b_res
), add_align
);
1080 if (!size0
&& !size1
) {
1081 if (bus
->self
&& (b_res
->start
|| b_res
->end
))
1082 pci_info(bus
->self
, "disabling bridge window %pR to %pR (unused)\n",
1083 b_res
, &bus
->busn_res
);
1087 b_res
->start
= min_align
;
1088 b_res
->end
= size0
+ min_align
- 1;
1089 b_res
->flags
|= IORESOURCE_STARTALIGN
;
1090 if (bus
->self
&& size1
> size0
&& realloc_head
) {
1091 add_to_list(realloc_head
, bus
->self
, b_res
, size1
-size0
, add_align
);
1092 pci_info(bus
->self
, "bridge window %pR to %pR add_size %llx add_align %llx\n",
1093 b_res
, &bus
->busn_res
,
1094 (unsigned long long) (size1
- size0
),
1095 (unsigned long long) add_align
);
1100 unsigned long pci_cardbus_resource_alignment(struct resource
*res
)
1102 if (res
->flags
& IORESOURCE_IO
)
1103 return pci_cardbus_io_size
;
1104 if (res
->flags
& IORESOURCE_MEM
)
1105 return pci_cardbus_mem_size
;
1109 static void pci_bus_size_cardbus(struct pci_bus
*bus
,
1110 struct list_head
*realloc_head
)
1112 struct pci_dev
*bridge
= bus
->self
;
1113 struct resource
*b_res
;
1114 resource_size_t b_res_3_size
= pci_cardbus_mem_size
* 2;
1117 b_res
= &bridge
->resource
[PCI_CB_BRIDGE_IO_0_WINDOW
];
1119 goto handle_b_res_1
;
1121 * Reserve some resources for CardBus. We reserve a fixed amount
1122 * of bus space for CardBus bridges.
1124 b_res
->start
= pci_cardbus_io_size
;
1125 b_res
->end
= b_res
->start
+ pci_cardbus_io_size
- 1;
1126 b_res
->flags
|= IORESOURCE_IO
| IORESOURCE_STARTALIGN
;
1128 b_res
->end
-= pci_cardbus_io_size
;
1129 add_to_list(realloc_head
, bridge
, b_res
, pci_cardbus_io_size
,
1130 pci_cardbus_io_size
);
1134 b_res
= &bridge
->resource
[PCI_CB_BRIDGE_IO_1_WINDOW
];
1136 goto handle_b_res_2
;
1137 b_res
->start
= pci_cardbus_io_size
;
1138 b_res
->end
= b_res
->start
+ pci_cardbus_io_size
- 1;
1139 b_res
->flags
|= IORESOURCE_IO
| IORESOURCE_STARTALIGN
;
1141 b_res
->end
-= pci_cardbus_io_size
;
1142 add_to_list(realloc_head
, bridge
, b_res
, pci_cardbus_io_size
,
1143 pci_cardbus_io_size
);
1147 /* MEM1 must not be pref MMIO */
1148 pci_read_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, &ctrl
);
1149 if (ctrl
& PCI_CB_BRIDGE_CTL_PREFETCH_MEM1
) {
1150 ctrl
&= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1
;
1151 pci_write_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, ctrl
);
1152 pci_read_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, &ctrl
);
1155 /* Check whether prefetchable memory is supported by this bridge. */
1156 pci_read_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, &ctrl
);
1157 if (!(ctrl
& PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
)) {
1158 ctrl
|= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
;
1159 pci_write_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, ctrl
);
1160 pci_read_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, &ctrl
);
1163 b_res
= &bridge
->resource
[PCI_CB_BRIDGE_MEM_0_WINDOW
];
1165 goto handle_b_res_3
;
1167 * If we have prefetchable memory support, allocate two regions.
1168 * Otherwise, allocate one region of twice the size.
1170 if (ctrl
& PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
) {
1171 b_res
->start
= pci_cardbus_mem_size
;
1172 b_res
->end
= b_res
->start
+ pci_cardbus_mem_size
- 1;
1173 b_res
->flags
|= IORESOURCE_MEM
| IORESOURCE_PREFETCH
|
1174 IORESOURCE_STARTALIGN
;
1176 b_res
->end
-= pci_cardbus_mem_size
;
1177 add_to_list(realloc_head
, bridge
, b_res
,
1178 pci_cardbus_mem_size
, pci_cardbus_mem_size
);
1181 /* Reduce that to half */
1182 b_res_3_size
= pci_cardbus_mem_size
;
1186 b_res
= &bridge
->resource
[PCI_CB_BRIDGE_MEM_1_WINDOW
];
1189 b_res
->start
= pci_cardbus_mem_size
;
1190 b_res
->end
= b_res
->start
+ b_res_3_size
- 1;
1191 b_res
->flags
|= IORESOURCE_MEM
| IORESOURCE_STARTALIGN
;
1193 b_res
->end
-= b_res_3_size
;
1194 add_to_list(realloc_head
, bridge
, b_res
, b_res_3_size
,
1195 pci_cardbus_mem_size
);
1202 void __pci_bus_size_bridges(struct pci_bus
*bus
, struct list_head
*realloc_head
)
1204 struct pci_dev
*dev
;
1205 unsigned long mask
, prefmask
, type2
= 0, type3
= 0;
1206 resource_size_t additional_io_size
= 0, additional_mmio_size
= 0,
1207 additional_mmio_pref_size
= 0;
1208 struct resource
*pref
;
1209 struct pci_host_bridge
*host
;
1210 int hdr_type
, i
, ret
;
1212 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1213 struct pci_bus
*b
= dev
->subordinate
;
1217 switch (dev
->hdr_type
) {
1218 case PCI_HEADER_TYPE_CARDBUS
:
1219 pci_bus_size_cardbus(b
, realloc_head
);
1222 case PCI_HEADER_TYPE_BRIDGE
:
1224 __pci_bus_size_bridges(b
, realloc_head
);
1230 if (pci_is_root_bus(bus
)) {
1231 host
= to_pci_host_bridge(bus
->bridge
);
1232 if (!host
->size_windows
)
1234 pci_bus_for_each_resource(bus
, pref
, i
)
1235 if (pref
&& (pref
->flags
& IORESOURCE_PREFETCH
))
1237 hdr_type
= -1; /* Intentionally invalid - not a PCI device. */
1239 pref
= &bus
->self
->resource
[PCI_BRIDGE_PREF_MEM_WINDOW
];
1240 hdr_type
= bus
->self
->hdr_type
;
1244 case PCI_HEADER_TYPE_CARDBUS
:
1245 /* Don't size CardBuses yet */
1248 case PCI_HEADER_TYPE_BRIDGE
:
1249 pci_bridge_check_ranges(bus
);
1250 if (bus
->self
->is_hotplug_bridge
) {
1251 additional_io_size
= pci_hotplug_io_size
;
1252 additional_mmio_size
= pci_hotplug_mmio_size
;
1253 additional_mmio_pref_size
= pci_hotplug_mmio_pref_size
;
1257 pbus_size_io(bus
, realloc_head
? 0 : additional_io_size
,
1258 additional_io_size
, realloc_head
);
1261 * If there's a 64-bit prefetchable MMIO window, compute
1262 * the size required to put all 64-bit prefetchable
1265 mask
= IORESOURCE_MEM
;
1266 prefmask
= IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
1267 if (pref
&& (pref
->flags
& IORESOURCE_MEM_64
)) {
1268 prefmask
|= IORESOURCE_MEM_64
;
1269 ret
= pbus_size_mem(bus
, prefmask
, prefmask
,
1271 realloc_head
? 0 : additional_mmio_pref_size
,
1272 additional_mmio_pref_size
, realloc_head
);
1275 * If successful, all non-prefetchable resources
1276 * and any 32-bit prefetchable resources will go in
1277 * the non-prefetchable window.
1281 type2
= prefmask
& ~IORESOURCE_MEM_64
;
1282 type3
= prefmask
& ~IORESOURCE_PREFETCH
;
1287 * If there is no 64-bit prefetchable window, compute the
1288 * size required to put all prefetchable resources in the
1289 * 32-bit prefetchable window (if there is one).
1292 prefmask
&= ~IORESOURCE_MEM_64
;
1293 ret
= pbus_size_mem(bus
, prefmask
, prefmask
,
1295 realloc_head
? 0 : additional_mmio_pref_size
,
1296 additional_mmio_pref_size
, realloc_head
);
1299 * If successful, only non-prefetchable resources
1300 * will go in the non-prefetchable window.
1305 additional_mmio_size
+= additional_mmio_pref_size
;
1307 type2
= type3
= IORESOURCE_MEM
;
1311 * Compute the size required to put everything else in the
1312 * non-prefetchable window. This includes:
1314 * - all non-prefetchable resources
1315 * - 32-bit prefetchable resources if there's a 64-bit
1316 * prefetchable window or no prefetchable window at all
1317 * - 64-bit prefetchable resources if there's no prefetchable
1320 * Note that the strategy in __pci_assign_resource() must match
1321 * that used here. Specifically, we cannot put a 32-bit
1322 * prefetchable resource in a 64-bit prefetchable window.
1324 pbus_size_mem(bus
, mask
, IORESOURCE_MEM
, type2
, type3
,
1325 realloc_head
? 0 : additional_mmio_size
,
1326 additional_mmio_size
, realloc_head
);
1331 void pci_bus_size_bridges(struct pci_bus
*bus
)
1333 __pci_bus_size_bridges(bus
, NULL
);
1335 EXPORT_SYMBOL(pci_bus_size_bridges
);
1337 static void assign_fixed_resource_on_bus(struct pci_bus
*b
, struct resource
*r
)
1340 struct resource
*parent_r
;
1341 unsigned long mask
= IORESOURCE_IO
| IORESOURCE_MEM
|
1342 IORESOURCE_PREFETCH
;
1344 pci_bus_for_each_resource(b
, parent_r
, i
) {
1348 if ((r
->flags
& mask
) == (parent_r
->flags
& mask
) &&
1349 resource_contains(parent_r
, r
))
1350 request_resource(parent_r
, r
);
1355 * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they are
1356 * skipped by pbus_assign_resources_sorted().
1358 static void pdev_assign_fixed_resources(struct pci_dev
*dev
)
1362 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
1364 struct resource
*r
= &dev
->resource
[i
];
1366 if (r
->parent
|| !(r
->flags
& IORESOURCE_PCI_FIXED
) ||
1367 !(r
->flags
& (IORESOURCE_IO
| IORESOURCE_MEM
)))
1371 while (b
&& !r
->parent
) {
1372 assign_fixed_resource_on_bus(b
, r
);
1378 void __pci_bus_assign_resources(const struct pci_bus
*bus
,
1379 struct list_head
*realloc_head
,
1380 struct list_head
*fail_head
)
1383 struct pci_dev
*dev
;
1385 pbus_assign_resources_sorted(bus
, realloc_head
, fail_head
);
1387 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1388 pdev_assign_fixed_resources(dev
);
1390 b
= dev
->subordinate
;
1394 __pci_bus_assign_resources(b
, realloc_head
, fail_head
);
1396 switch (dev
->hdr_type
) {
1397 case PCI_HEADER_TYPE_BRIDGE
:
1398 if (!pci_is_enabled(dev
))
1399 pci_setup_bridge(b
);
1402 case PCI_HEADER_TYPE_CARDBUS
:
1403 pci_setup_cardbus(b
);
1407 pci_info(dev
, "not setting up bridge for bus %04x:%02x\n",
1408 pci_domain_nr(b
), b
->number
);
1414 void pci_bus_assign_resources(const struct pci_bus
*bus
)
1416 __pci_bus_assign_resources(bus
, NULL
, NULL
);
1418 EXPORT_SYMBOL(pci_bus_assign_resources
);
1420 static void pci_claim_device_resources(struct pci_dev
*dev
)
1424 for (i
= 0; i
< PCI_BRIDGE_RESOURCES
; i
++) {
1425 struct resource
*r
= &dev
->resource
[i
];
1427 if (!r
->flags
|| r
->parent
)
1430 pci_claim_resource(dev
, i
);
1434 static void pci_claim_bridge_resources(struct pci_dev
*dev
)
1438 for (i
= PCI_BRIDGE_RESOURCES
; i
< PCI_NUM_RESOURCES
; i
++) {
1439 struct resource
*r
= &dev
->resource
[i
];
1441 if (!r
->flags
|| r
->parent
)
1444 pci_claim_bridge_resource(dev
, i
);
1448 static void pci_bus_allocate_dev_resources(struct pci_bus
*b
)
1450 struct pci_dev
*dev
;
1451 struct pci_bus
*child
;
1453 list_for_each_entry(dev
, &b
->devices
, bus_list
) {
1454 pci_claim_device_resources(dev
);
1456 child
= dev
->subordinate
;
1458 pci_bus_allocate_dev_resources(child
);
1462 static void pci_bus_allocate_resources(struct pci_bus
*b
)
1464 struct pci_bus
*child
;
1467 * Carry out a depth-first search on the PCI bus tree to allocate
1468 * bridge apertures. Read the programmed bridge bases and
1469 * recursively claim the respective bridge resources.
1472 pci_read_bridge_bases(b
);
1473 pci_claim_bridge_resources(b
->self
);
1476 list_for_each_entry(child
, &b
->children
, node
)
1477 pci_bus_allocate_resources(child
);
1480 void pci_bus_claim_resources(struct pci_bus
*b
)
1482 pci_bus_allocate_resources(b
);
1483 pci_bus_allocate_dev_resources(b
);
1485 EXPORT_SYMBOL(pci_bus_claim_resources
);
1487 static void __pci_bridge_assign_resources(const struct pci_dev
*bridge
,
1488 struct list_head
*add_head
,
1489 struct list_head
*fail_head
)
1493 pdev_assign_resources_sorted((struct pci_dev
*)bridge
,
1494 add_head
, fail_head
);
1496 b
= bridge
->subordinate
;
1500 __pci_bus_assign_resources(b
, add_head
, fail_head
);
1502 switch (bridge
->class >> 8) {
1503 case PCI_CLASS_BRIDGE_PCI
:
1504 pci_setup_bridge(b
);
1507 case PCI_CLASS_BRIDGE_CARDBUS
:
1508 pci_setup_cardbus(b
);
1512 pci_info(bridge
, "not setting up bridge for bus %04x:%02x\n",
1513 pci_domain_nr(b
), b
->number
);
1518 #define PCI_RES_TYPE_MASK \
1519 (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\
1522 static void pci_bridge_release_resources(struct pci_bus
*bus
,
1525 struct pci_dev
*dev
= bus
->self
;
1527 unsigned old_flags
= 0;
1528 struct resource
*b_res
;
1531 b_res
= &dev
->resource
[PCI_BRIDGE_RESOURCES
];
1534 * 1. If IO port assignment fails, release bridge IO port.
1535 * 2. If non pref MMIO assignment fails, release bridge nonpref MMIO.
1536 * 3. If 64bit pref MMIO assignment fails, and bridge pref is 64bit,
1537 * release bridge pref MMIO.
1538 * 4. If pref MMIO assignment fails, and bridge pref is 32bit,
1539 * release bridge pref MMIO.
1540 * 5. If pref MMIO assignment fails, and bridge pref is not
1541 * assigned, release bridge nonpref MMIO.
1543 if (type
& IORESOURCE_IO
)
1545 else if (!(type
& IORESOURCE_PREFETCH
))
1547 else if ((type
& IORESOURCE_MEM_64
) &&
1548 (b_res
[2].flags
& IORESOURCE_MEM_64
))
1550 else if (!(b_res
[2].flags
& IORESOURCE_MEM_64
) &&
1551 (b_res
[2].flags
& IORESOURCE_PREFETCH
))
1561 /* If there are children, release them all */
1562 release_child_resources(r
);
1563 if (!release_resource(r
)) {
1564 type
= old_flags
= r
->flags
& PCI_RES_TYPE_MASK
;
1565 pci_info(dev
, "resource %d %pR released\n",
1566 PCI_BRIDGE_RESOURCES
+ idx
, r
);
1567 /* Keep the old size */
1568 r
->end
= resource_size(r
) - 1;
1572 /* Avoiding touch the one without PREF */
1573 if (type
& IORESOURCE_PREFETCH
)
1574 type
= IORESOURCE_PREFETCH
;
1575 __pci_setup_bridge(bus
, type
);
1576 /* For next child res under same bridge */
1577 r
->flags
= old_flags
;
1587 * Try to release PCI bridge resources from leaf bridge, so we can allocate
1588 * a larger window later.
1590 static void pci_bus_release_bridge_resources(struct pci_bus
*bus
,
1592 enum release_type rel_type
)
1594 struct pci_dev
*dev
;
1595 bool is_leaf_bridge
= true;
1597 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1598 struct pci_bus
*b
= dev
->subordinate
;
1602 is_leaf_bridge
= false;
1604 if ((dev
->class >> 8) != PCI_CLASS_BRIDGE_PCI
)
1607 if (rel_type
== whole_subtree
)
1608 pci_bus_release_bridge_resources(b
, type
,
1612 if (pci_is_root_bus(bus
))
1615 if ((bus
->self
->class >> 8) != PCI_CLASS_BRIDGE_PCI
)
1618 if ((rel_type
== whole_subtree
) || is_leaf_bridge
)
1619 pci_bridge_release_resources(bus
, type
);
1622 static void pci_bus_dump_res(struct pci_bus
*bus
)
1624 struct resource
*res
;
1627 pci_bus_for_each_resource(bus
, res
, i
) {
1628 if (!res
|| !res
->end
|| !res
->flags
)
1631 dev_info(&bus
->dev
, "resource %d %pR\n", i
, res
);
1635 static void pci_bus_dump_resources(struct pci_bus
*bus
)
1638 struct pci_dev
*dev
;
1641 pci_bus_dump_res(bus
);
1643 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1644 b
= dev
->subordinate
;
1648 pci_bus_dump_resources(b
);
1652 static int pci_bus_get_depth(struct pci_bus
*bus
)
1655 struct pci_bus
*child_bus
;
1657 list_for_each_entry(child_bus
, &bus
->children
, node
) {
1660 ret
= pci_bus_get_depth(child_bus
);
1661 if (ret
+ 1 > depth
)
1669 * -1: undefined, will auto detect later
1670 * 0: disabled by user
1671 * 1: disabled by auto detect
1672 * 2: enabled by user
1673 * 3: enabled by auto detect
1683 static enum enable_type pci_realloc_enable
= undefined
;
1684 void __init
pci_realloc_get_opt(char *str
)
1686 if (!strncmp(str
, "off", 3))
1687 pci_realloc_enable
= user_disabled
;
1688 else if (!strncmp(str
, "on", 2))
1689 pci_realloc_enable
= user_enabled
;
1691 static bool pci_realloc_enabled(enum enable_type enable
)
1693 return enable
>= user_enabled
;
1696 #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1697 static int iov_resources_unassigned(struct pci_dev
*dev
, void *data
)
1700 bool *unassigned
= data
;
1702 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
1703 struct resource
*r
= &dev
->resource
[i
+ PCI_IOV_RESOURCES
];
1704 struct pci_bus_region region
;
1706 /* Not assigned or rejected by kernel? */
1710 pcibios_resource_to_bus(dev
->bus
, ®ion
, r
);
1711 if (!region
.start
) {
1713 return 1; /* Return early from pci_walk_bus() */
1720 static enum enable_type
pci_realloc_detect(struct pci_bus
*bus
,
1721 enum enable_type enable_local
)
1723 bool unassigned
= false;
1724 struct pci_host_bridge
*host
;
1726 if (enable_local
!= undefined
)
1727 return enable_local
;
1729 host
= pci_find_host_bridge(bus
);
1730 if (host
->preserve_config
)
1731 return auto_disabled
;
1733 pci_walk_bus(bus
, iov_resources_unassigned
, &unassigned
);
1735 return auto_enabled
;
1737 return enable_local
;
1740 static enum enable_type
pci_realloc_detect(struct pci_bus
*bus
,
1741 enum enable_type enable_local
)
1743 return enable_local
;
1748 * First try will not touch PCI bridge res.
1749 * Second and later try will clear small leaf bridge res.
1750 * Will stop till to the max depth if can not find good one.
1752 void pci_assign_unassigned_root_bus_resources(struct pci_bus
*bus
)
1754 LIST_HEAD(realloc_head
);
1755 /* List of resources that want additional resources */
1756 struct list_head
*add_list
= NULL
;
1757 int tried_times
= 0;
1758 enum release_type rel_type
= leaf_only
;
1759 LIST_HEAD(fail_head
);
1760 struct pci_dev_resource
*fail_res
;
1761 int pci_try_num
= 1;
1762 enum enable_type enable_local
;
1764 /* Don't realloc if asked to do so */
1765 enable_local
= pci_realloc_detect(bus
, pci_realloc_enable
);
1766 if (pci_realloc_enabled(enable_local
)) {
1767 int max_depth
= pci_bus_get_depth(bus
);
1769 pci_try_num
= max_depth
+ 1;
1770 dev_info(&bus
->dev
, "max bus depth: %d pci_try_num: %d\n",
1771 max_depth
, pci_try_num
);
1776 * Last try will use add_list, otherwise will try good to have as must
1777 * have, so can realloc parent bridge resource
1779 if (tried_times
+ 1 == pci_try_num
)
1780 add_list
= &realloc_head
;
1782 * Depth first, calculate sizes and alignments of all subordinate buses.
1784 __pci_bus_size_bridges(bus
, add_list
);
1786 /* Depth last, allocate resources and update the hardware. */
1787 __pci_bus_assign_resources(bus
, add_list
, &fail_head
);
1789 BUG_ON(!list_empty(add_list
));
1792 /* Any device complain? */
1793 if (list_empty(&fail_head
))
1796 if (tried_times
>= pci_try_num
) {
1797 if (enable_local
== undefined
)
1798 dev_info(&bus
->dev
, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
1799 else if (enable_local
== auto_enabled
)
1800 dev_info(&bus
->dev
, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
1802 free_list(&fail_head
);
1806 dev_info(&bus
->dev
, "No. %d try to assign unassigned res\n",
1809 /* Third times and later will not check if it is leaf */
1810 if ((tried_times
+ 1) > 2)
1811 rel_type
= whole_subtree
;
1814 * Try to release leaf bridge's resources that doesn't fit resource of
1815 * child device under that bridge.
1817 list_for_each_entry(fail_res
, &fail_head
, list
)
1818 pci_bus_release_bridge_resources(fail_res
->dev
->bus
,
1819 fail_res
->flags
& PCI_RES_TYPE_MASK
,
1822 /* Restore size and flags */
1823 list_for_each_entry(fail_res
, &fail_head
, list
) {
1824 struct resource
*res
= fail_res
->res
;
1827 res
->start
= fail_res
->start
;
1828 res
->end
= fail_res
->end
;
1829 res
->flags
= fail_res
->flags
;
1831 if (pci_is_bridge(fail_res
->dev
)) {
1832 idx
= res
- &fail_res
->dev
->resource
[0];
1833 if (idx
>= PCI_BRIDGE_RESOURCES
&&
1834 idx
<= PCI_BRIDGE_RESOURCE_END
)
1838 free_list(&fail_head
);
1843 /* Dump the resource on buses */
1844 pci_bus_dump_resources(bus
);
1847 void __init
pci_assign_unassigned_resources(void)
1849 struct pci_bus
*root_bus
;
1851 list_for_each_entry(root_bus
, &pci_root_buses
, node
) {
1852 pci_assign_unassigned_root_bus_resources(root_bus
);
1854 /* Make sure the root bridge has a companion ACPI device */
1855 if (ACPI_HANDLE(root_bus
->bridge
))
1856 acpi_ioapic_add(ACPI_HANDLE(root_bus
->bridge
));
1860 static void adjust_bridge_window(struct pci_dev
*bridge
, struct resource
*res
,
1861 struct list_head
*add_list
,
1862 resource_size_t new_size
)
1864 resource_size_t add_size
, size
= resource_size(res
);
1872 if (new_size
> size
) {
1873 add_size
= new_size
- size
;
1874 pci_dbg(bridge
, "bridge window %pR extended by %pa\n", res
,
1876 } else if (new_size
< size
) {
1877 add_size
= size
- new_size
;
1878 pci_dbg(bridge
, "bridge window %pR shrunken by %pa\n", res
,
1882 res
->end
= res
->start
+ new_size
- 1;
1883 remove_from_list(add_list
, res
);
1886 static void pci_bus_distribute_available_resources(struct pci_bus
*bus
,
1887 struct list_head
*add_list
,
1889 struct resource mmio
,
1890 struct resource mmio_pref
)
1892 unsigned int normal_bridges
= 0, hotplug_bridges
= 0;
1893 struct resource
*io_res
, *mmio_res
, *mmio_pref_res
;
1894 struct pci_dev
*dev
, *bridge
= bus
->self
;
1895 resource_size_t io_per_hp
, mmio_per_hp
, mmio_pref_per_hp
, align
;
1897 io_res
= &bridge
->resource
[PCI_BRIDGE_IO_WINDOW
];
1898 mmio_res
= &bridge
->resource
[PCI_BRIDGE_MEM_WINDOW
];
1899 mmio_pref_res
= &bridge
->resource
[PCI_BRIDGE_PREF_MEM_WINDOW
];
1902 * The alignment of this bridge is yet to be considered, hence it must
1903 * be done now before extending its bridge window.
1905 align
= pci_resource_alignment(bridge
, io_res
);
1906 if (!io_res
->parent
&& align
)
1907 io
.start
= min(ALIGN(io
.start
, align
), io
.end
+ 1);
1909 align
= pci_resource_alignment(bridge
, mmio_res
);
1910 if (!mmio_res
->parent
&& align
)
1911 mmio
.start
= min(ALIGN(mmio
.start
, align
), mmio
.end
+ 1);
1913 align
= pci_resource_alignment(bridge
, mmio_pref_res
);
1914 if (!mmio_pref_res
->parent
&& align
)
1915 mmio_pref
.start
= min(ALIGN(mmio_pref
.start
, align
),
1919 * Now that we have adjusted for alignment, update the bridge window
1920 * resources to fill as much remaining resource space as possible.
1922 adjust_bridge_window(bridge
, io_res
, add_list
, resource_size(&io
));
1923 adjust_bridge_window(bridge
, mmio_res
, add_list
, resource_size(&mmio
));
1924 adjust_bridge_window(bridge
, mmio_pref_res
, add_list
,
1925 resource_size(&mmio_pref
));
1928 * Calculate how many hotplug bridges and normal bridges there
1929 * are on this bus. We will distribute the additional available
1930 * resources between hotplug bridges.
1932 for_each_pci_bridge(dev
, bus
) {
1933 if (dev
->is_hotplug_bridge
)
1940 * There is only one bridge on the bus so it gets all available
1941 * resources which it can then distribute to the possible hotplug
1944 if (hotplug_bridges
+ normal_bridges
== 1) {
1945 dev
= list_first_entry(&bus
->devices
, struct pci_dev
, bus_list
);
1946 if (dev
->subordinate
)
1947 pci_bus_distribute_available_resources(dev
->subordinate
,
1948 add_list
, io
, mmio
, mmio_pref
);
1952 if (hotplug_bridges
== 0)
1956 * Calculate the total amount of extra resource space we can
1957 * pass to bridges below this one. This is basically the
1958 * extra space reduced by the minimal required space for the
1959 * non-hotplug bridges.
1961 for_each_pci_bridge(dev
, bus
) {
1962 resource_size_t used_size
;
1963 struct resource
*res
;
1965 if (dev
->is_hotplug_bridge
)
1969 * Reduce the available resource space by what the
1970 * bridge and devices below it occupy.
1972 res
= &dev
->resource
[PCI_BRIDGE_IO_WINDOW
];
1973 align
= pci_resource_alignment(dev
, res
);
1974 align
= align
? ALIGN(io
.start
, align
) - io
.start
: 0;
1975 used_size
= align
+ resource_size(res
);
1977 io
.start
= min(io
.start
+ used_size
, io
.end
+ 1);
1979 res
= &dev
->resource
[PCI_BRIDGE_MEM_WINDOW
];
1980 align
= pci_resource_alignment(dev
, res
);
1981 align
= align
? ALIGN(mmio
.start
, align
) - mmio
.start
: 0;
1982 used_size
= align
+ resource_size(res
);
1984 mmio
.start
= min(mmio
.start
+ used_size
, mmio
.end
+ 1);
1986 res
= &dev
->resource
[PCI_BRIDGE_PREF_MEM_WINDOW
];
1987 align
= pci_resource_alignment(dev
, res
);
1988 align
= align
? ALIGN(mmio_pref
.start
, align
) -
1989 mmio_pref
.start
: 0;
1990 used_size
= align
+ resource_size(res
);
1992 mmio_pref
.start
= min(mmio_pref
.start
+ used_size
,
1996 io_per_hp
= div64_ul(resource_size(&io
), hotplug_bridges
);
1997 mmio_per_hp
= div64_ul(resource_size(&mmio
), hotplug_bridges
);
1998 mmio_pref_per_hp
= div64_ul(resource_size(&mmio_pref
),
2002 * Go over devices on this bus and distribute the remaining
2003 * resource space between hotplug bridges.
2005 for_each_pci_bridge(dev
, bus
) {
2008 b
= dev
->subordinate
;
2009 if (!b
|| !dev
->is_hotplug_bridge
)
2013 * Distribute available extra resources equally between
2014 * hotplug-capable downstream ports taking alignment into
2017 io
.end
= io
.start
+ io_per_hp
- 1;
2018 mmio
.end
= mmio
.start
+ mmio_per_hp
- 1;
2019 mmio_pref
.end
= mmio_pref
.start
+ mmio_pref_per_hp
- 1;
2021 pci_bus_distribute_available_resources(b
, add_list
, io
, mmio
,
2024 io
.start
+= io_per_hp
;
2025 mmio
.start
+= mmio_per_hp
;
2026 mmio_pref
.start
+= mmio_pref_per_hp
;
2030 static void pci_bridge_distribute_available_resources(struct pci_dev
*bridge
,
2031 struct list_head
*add_list
)
2033 struct resource available_io
, available_mmio
, available_mmio_pref
;
2035 if (!bridge
->is_hotplug_bridge
)
2038 /* Take the initial extra resources from the hotplug port */
2039 available_io
= bridge
->resource
[PCI_BRIDGE_IO_WINDOW
];
2040 available_mmio
= bridge
->resource
[PCI_BRIDGE_MEM_WINDOW
];
2041 available_mmio_pref
= bridge
->resource
[PCI_BRIDGE_PREF_MEM_WINDOW
];
2043 pci_bus_distribute_available_resources(bridge
->subordinate
,
2044 add_list
, available_io
,
2046 available_mmio_pref
);
2049 void pci_assign_unassigned_bridge_resources(struct pci_dev
*bridge
)
2051 struct pci_bus
*parent
= bridge
->subordinate
;
2052 /* List of resources that want additional resources */
2053 LIST_HEAD(add_list
);
2055 int tried_times
= 0;
2056 LIST_HEAD(fail_head
);
2057 struct pci_dev_resource
*fail_res
;
2061 __pci_bus_size_bridges(parent
, &add_list
);
2064 * Distribute remaining resources (if any) equally between hotplug
2065 * bridges below. This makes it possible to extend the hierarchy
2066 * later without running out of resources.
2068 pci_bridge_distribute_available_resources(bridge
, &add_list
);
2070 __pci_bridge_assign_resources(bridge
, &add_list
, &fail_head
);
2071 BUG_ON(!list_empty(&add_list
));
2074 if (list_empty(&fail_head
))
2077 if (tried_times
>= 2) {
2078 /* Still fail, don't need to try more */
2079 free_list(&fail_head
);
2083 printk(KERN_DEBUG
"PCI: No. %d try to assign unassigned res\n",
2087 * Try to release leaf bridge's resources that aren't big enough
2088 * to contain child device resources.
2090 list_for_each_entry(fail_res
, &fail_head
, list
)
2091 pci_bus_release_bridge_resources(fail_res
->dev
->bus
,
2092 fail_res
->flags
& PCI_RES_TYPE_MASK
,
2095 /* Restore size and flags */
2096 list_for_each_entry(fail_res
, &fail_head
, list
) {
2097 struct resource
*res
= fail_res
->res
;
2100 res
->start
= fail_res
->start
;
2101 res
->end
= fail_res
->end
;
2102 res
->flags
= fail_res
->flags
;
2104 if (pci_is_bridge(fail_res
->dev
)) {
2105 idx
= res
- &fail_res
->dev
->resource
[0];
2106 if (idx
>= PCI_BRIDGE_RESOURCES
&&
2107 idx
<= PCI_BRIDGE_RESOURCE_END
)
2111 free_list(&fail_head
);
2116 retval
= pci_reenable_device(bridge
);
2118 pci_err(bridge
, "Error reenabling bridge (%d)\n", retval
);
2119 pci_set_master(bridge
);
2121 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources
);
2123 int pci_reassign_bridge_resources(struct pci_dev
*bridge
, unsigned long type
)
2125 struct pci_dev_resource
*dev_res
;
2126 struct pci_dev
*next
;
2133 down_read(&pci_bus_sem
);
2135 /* Walk to the root hub, releasing bridge BARs when possible */
2139 for (i
= PCI_BRIDGE_RESOURCES
; i
< PCI_BRIDGE_RESOURCE_END
;
2141 struct resource
*res
= &bridge
->resource
[i
];
2143 if ((res
->flags
^ type
) & PCI_RES_TYPE_MASK
)
2146 /* Ignore BARs which are still in use */
2150 ret
= add_to_list(&saved
, bridge
, res
, 0, 0);
2154 pci_info(bridge
, "BAR %d: releasing %pR\n",
2158 release_resource(res
);
2163 if (i
== PCI_BRIDGE_RESOURCE_END
)
2166 next
= bridge
->bus
? bridge
->bus
->self
: NULL
;
2169 if (list_empty(&saved
)) {
2170 up_read(&pci_bus_sem
);
2174 __pci_bus_size_bridges(bridge
->subordinate
, &added
);
2175 __pci_bridge_assign_resources(bridge
, &added
, &failed
);
2176 BUG_ON(!list_empty(&added
));
2178 if (!list_empty(&failed
)) {
2183 list_for_each_entry(dev_res
, &saved
, list
) {
2184 /* Skip the bridge we just assigned resources for */
2185 if (bridge
== dev_res
->dev
)
2188 bridge
= dev_res
->dev
;
2189 pci_setup_bridge(bridge
->subordinate
);
2193 up_read(&pci_bus_sem
);
2197 /* Restore size and flags */
2198 list_for_each_entry(dev_res
, &failed
, list
) {
2199 struct resource
*res
= dev_res
->res
;
2201 res
->start
= dev_res
->start
;
2202 res
->end
= dev_res
->end
;
2203 res
->flags
= dev_res
->flags
;
2207 /* Revert to the old configuration */
2208 list_for_each_entry(dev_res
, &saved
, list
) {
2209 struct resource
*res
= dev_res
->res
;
2211 bridge
= dev_res
->dev
;
2212 i
= res
- bridge
->resource
;
2214 res
->start
= dev_res
->start
;
2215 res
->end
= dev_res
->end
;
2216 res
->flags
= dev_res
->flags
;
2218 pci_claim_resource(bridge
, i
);
2219 pci_setup_bridge(bridge
->subordinate
);
2222 up_read(&pci_bus_sem
);
2227 void pci_assign_unassigned_bus_resources(struct pci_bus
*bus
)
2229 struct pci_dev
*dev
;
2230 /* List of resources that want additional resources */
2231 LIST_HEAD(add_list
);
2233 down_read(&pci_bus_sem
);
2234 for_each_pci_bridge(dev
, bus
)
2235 if (pci_has_subordinate(dev
))
2236 __pci_bus_size_bridges(dev
->subordinate
, &add_list
);
2237 up_read(&pci_bus_sem
);
2238 __pci_bus_assign_resources(bus
, &add_list
, NULL
);
2239 BUG_ON(!list_empty(&add_list
));
2241 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources
);