1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/pci/setup-bus.c
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 * Support routines for initializing a PCI subsystem.
14 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
15 * PCI-PCI bridges cleanup, sorted resource allocation.
16 * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
17 * Converted to allocation in 3 passes, which gives
18 * tighter packing. Prefetchable range support.
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/pci.h>
25 #include <linux/errno.h>
26 #include <linux/ioport.h>
27 #include <linux/cache.h>
28 #include <linux/slab.h>
29 #include <linux/acpi.h>
32 unsigned int 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 corresponding to which the resource
60 * @res: The resource to be tracked
61 * @add_size: additional size to be optionally added
64 static int add_to_list(struct list_head
*head
,
65 struct pci_dev
*dev
, struct resource
*res
,
66 resource_size_t add_size
, resource_size_t min_align
)
68 struct pci_dev_resource
*tmp
;
70 tmp
= kzalloc(sizeof(*tmp
), GFP_KERNEL
);
76 tmp
->start
= res
->start
;
78 tmp
->flags
= res
->flags
;
79 tmp
->add_size
= add_size
;
80 tmp
->min_align
= min_align
;
82 list_add(&tmp
->list
, head
);
87 static void remove_from_list(struct list_head
*head
,
90 struct pci_dev_resource
*dev_res
, *tmp
;
92 list_for_each_entry_safe(dev_res
, tmp
, head
, list
) {
93 if (dev_res
->res
== res
) {
94 list_del(&dev_res
->list
);
101 static struct pci_dev_resource
*res_to_dev_res(struct list_head
*head
,
102 struct resource
*res
)
104 struct pci_dev_resource
*dev_res
;
106 list_for_each_entry(dev_res
, head
, list
) {
107 if (dev_res
->res
== res
)
114 static resource_size_t
get_res_add_size(struct list_head
*head
,
115 struct resource
*res
)
117 struct pci_dev_resource
*dev_res
;
119 dev_res
= res_to_dev_res(head
, res
);
120 return dev_res
? dev_res
->add_size
: 0;
123 static resource_size_t
get_res_add_align(struct list_head
*head
,
124 struct resource
*res
)
126 struct pci_dev_resource
*dev_res
;
128 dev_res
= res_to_dev_res(head
, res
);
129 return dev_res
? dev_res
->min_align
: 0;
133 /* Sort resources by alignment */
134 static void pdev_sort_resources(struct pci_dev
*dev
, struct list_head
*head
)
138 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
140 struct pci_dev_resource
*dev_res
, *tmp
;
141 resource_size_t r_align
;
144 r
= &dev
->resource
[i
];
146 if (r
->flags
& IORESOURCE_PCI_FIXED
)
149 if (!(r
->flags
) || r
->parent
)
152 r_align
= pci_resource_alignment(dev
, r
);
154 pci_warn(dev
, "BAR %d: %pR has bogus alignment\n",
159 tmp
= kzalloc(sizeof(*tmp
), GFP_KERNEL
);
161 panic("pdev_sort_resources(): kmalloc() failed!\n");
165 /* fallback is smallest one or list is empty*/
167 list_for_each_entry(dev_res
, head
, list
) {
168 resource_size_t align
;
170 align
= pci_resource_alignment(dev_res
->dev
,
173 if (r_align
> align
) {
178 /* Insert it just before n*/
179 list_add_tail(&tmp
->list
, n
);
183 static void __dev_sort_resources(struct pci_dev
*dev
,
184 struct list_head
*head
)
186 u16
class = dev
->class >> 8;
188 /* Don't touch classless devices or host bridges or ioapics. */
189 if (class == PCI_CLASS_NOT_DEFINED
|| class == PCI_CLASS_BRIDGE_HOST
)
192 /* Don't touch ioapic devices already enabled by firmware */
193 if (class == PCI_CLASS_SYSTEM_PIC
) {
195 pci_read_config_word(dev
, PCI_COMMAND
, &command
);
196 if (command
& (PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
))
200 pdev_sort_resources(dev
, head
);
203 static inline void reset_resource(struct resource
*res
)
211 * reassign_resources_sorted() - satisfy any additional resource requests
213 * @realloc_head : head of the list tracking requests requiring additional
215 * @head : head of the list tracking requests with allocated
218 * Walk through each element of the realloc_head and try to procure
219 * additional resources for the element, provided the element
220 * is in the head list.
222 static void reassign_resources_sorted(struct list_head
*realloc_head
,
223 struct list_head
*head
)
225 struct resource
*res
;
226 struct pci_dev_resource
*add_res
, *tmp
;
227 struct pci_dev_resource
*dev_res
;
228 resource_size_t add_size
, align
;
231 list_for_each_entry_safe(add_res
, tmp
, realloc_head
, list
) {
232 bool found_match
= false;
235 /* skip resource that has been reset */
239 /* skip this resource if not found in head list */
240 list_for_each_entry(dev_res
, head
, list
) {
241 if (dev_res
->res
== res
) {
246 if (!found_match
)/* just skip */
249 idx
= res
- &add_res
->dev
->resource
[0];
250 add_size
= add_res
->add_size
;
251 align
= add_res
->min_align
;
252 if (!resource_size(res
)) {
254 res
->end
= res
->start
+ add_size
- 1;
255 if (pci_assign_resource(add_res
->dev
, idx
))
258 res
->flags
|= add_res
->flags
&
259 (IORESOURCE_STARTALIGN
|IORESOURCE_SIZEALIGN
);
260 if (pci_reassign_resource(add_res
->dev
, idx
,
262 pci_printk(KERN_DEBUG
, add_res
->dev
,
263 "failed to add %llx res[%d]=%pR\n",
264 (unsigned long long)add_size
,
268 list_del(&add_res
->list
);
274 * assign_requested_resources_sorted() - satisfy resource requests
276 * @head : head of the list tracking requests for resources
277 * @fail_head : head of the list tracking requests that could
280 * Satisfy resource requests of each element in the list. Add
281 * requests that could not satisfied to the failed_list.
283 static void assign_requested_resources_sorted(struct list_head
*head
,
284 struct list_head
*fail_head
)
286 struct resource
*res
;
287 struct pci_dev_resource
*dev_res
;
290 list_for_each_entry(dev_res
, head
, list
) {
292 idx
= res
- &dev_res
->dev
->resource
[0];
293 if (resource_size(res
) &&
294 pci_assign_resource(dev_res
->dev
, idx
)) {
297 * if the failed res is for ROM BAR, and it will
298 * be enabled later, don't add it to the list
300 if (!((idx
== PCI_ROM_RESOURCE
) &&
301 (!(res
->flags
& IORESOURCE_ROM_ENABLE
))))
302 add_to_list(fail_head
,
312 static unsigned long pci_fail_res_type_mask(struct list_head
*fail_head
)
314 struct pci_dev_resource
*fail_res
;
315 unsigned long mask
= 0;
317 /* check failed type */
318 list_for_each_entry(fail_res
, fail_head
, list
)
319 mask
|= fail_res
->flags
;
322 * one pref failed resource will set IORESOURCE_MEM,
323 * as we can allocate pref in non-pref range.
324 * Will release all assigned non-pref sibling resources
325 * according to that bit.
327 return mask
& (IORESOURCE_IO
| IORESOURCE_MEM
| IORESOURCE_PREFETCH
);
330 static bool pci_need_to_release(unsigned long mask
, struct resource
*res
)
332 if (res
->flags
& IORESOURCE_IO
)
333 return !!(mask
& IORESOURCE_IO
);
335 /* check pref at first */
336 if (res
->flags
& IORESOURCE_PREFETCH
) {
337 if (mask
& IORESOURCE_PREFETCH
)
339 /* count pref if its parent is non-pref */
340 else if ((mask
& IORESOURCE_MEM
) &&
341 !(res
->parent
->flags
& IORESOURCE_PREFETCH
))
347 if (res
->flags
& IORESOURCE_MEM
)
348 return !!(mask
& IORESOURCE_MEM
);
350 return false; /* should not get here */
353 static void __assign_resources_sorted(struct list_head
*head
,
354 struct list_head
*realloc_head
,
355 struct list_head
*fail_head
)
358 * Should not assign requested resources at first.
359 * they could be adjacent, so later reassign can not reallocate
360 * them one by one in parent resource window.
361 * Try to assign requested + add_size at beginning
362 * if could do that, could get out early.
363 * if could not do that, we still try to assign requested at first,
364 * then try to reassign add_size for some resources.
366 * Separate three resource type checking if we need to release
367 * assigned resource after requested + add_size try.
368 * 1. if there is io port assign fail, will release assigned
370 * 2. if there is pref mmio assign fail, release assigned
372 * if assigned pref mmio's parent is non-pref mmio and there
373 * is non-pref mmio assign fail, will release that assigned
375 * 3. if there is non-pref mmio assign fail or pref mmio
376 * assigned fail, will release assigned non-pref mmio.
378 LIST_HEAD(save_head
);
379 LIST_HEAD(local_fail_head
);
380 struct pci_dev_resource
*save_res
;
381 struct pci_dev_resource
*dev_res
, *tmp_res
, *dev_res2
;
382 unsigned long fail_type
;
383 resource_size_t add_align
, align
;
385 /* Check if optional add_size is there */
386 if (!realloc_head
|| list_empty(realloc_head
))
387 goto requested_and_reassign
;
389 /* Save original start, end, flags etc at first */
390 list_for_each_entry(dev_res
, head
, list
) {
391 if (add_to_list(&save_head
, dev_res
->dev
, dev_res
->res
, 0, 0)) {
392 free_list(&save_head
);
393 goto requested_and_reassign
;
397 /* Update res in head list with add_size in realloc_head list */
398 list_for_each_entry_safe(dev_res
, tmp_res
, head
, list
) {
399 dev_res
->res
->end
+= get_res_add_size(realloc_head
,
403 * There are two kinds of additional resources in the list:
404 * 1. bridge resource -- IORESOURCE_STARTALIGN
405 * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN
406 * Here just fix the additional alignment for bridge
408 if (!(dev_res
->res
->flags
& IORESOURCE_STARTALIGN
))
411 add_align
= get_res_add_align(realloc_head
, dev_res
->res
);
414 * The "head" list is sorted by the alignment to make sure
415 * resources with bigger alignment will be assigned first.
416 * After we change the alignment of a dev_res in "head" list,
417 * we need to reorder the list by alignment to make it
420 if (add_align
> dev_res
->res
->start
) {
421 resource_size_t r_size
= resource_size(dev_res
->res
);
423 dev_res
->res
->start
= add_align
;
424 dev_res
->res
->end
= add_align
+ r_size
- 1;
426 list_for_each_entry(dev_res2
, head
, list
) {
427 align
= pci_resource_alignment(dev_res2
->dev
,
429 if (add_align
> align
) {
430 list_move_tail(&dev_res
->list
,
439 /* Try updated head list with add_size added */
440 assign_requested_resources_sorted(head
, &local_fail_head
);
442 /* all assigned with add_size ? */
443 if (list_empty(&local_fail_head
)) {
444 /* Remove head list from realloc_head list */
445 list_for_each_entry(dev_res
, head
, list
)
446 remove_from_list(realloc_head
, dev_res
->res
);
447 free_list(&save_head
);
452 /* check failed type */
453 fail_type
= pci_fail_res_type_mask(&local_fail_head
);
454 /* remove not need to be released assigned res from head list etc */
455 list_for_each_entry_safe(dev_res
, tmp_res
, head
, list
)
456 if (dev_res
->res
->parent
&&
457 !pci_need_to_release(fail_type
, dev_res
->res
)) {
458 /* remove it from realloc_head list */
459 remove_from_list(realloc_head
, dev_res
->res
);
460 remove_from_list(&save_head
, dev_res
->res
);
461 list_del(&dev_res
->list
);
465 free_list(&local_fail_head
);
466 /* Release assigned resource */
467 list_for_each_entry(dev_res
, head
, list
)
468 if (dev_res
->res
->parent
)
469 release_resource(dev_res
->res
);
470 /* Restore start/end/flags from saved list */
471 list_for_each_entry(save_res
, &save_head
, list
) {
472 struct resource
*res
= save_res
->res
;
474 res
->start
= save_res
->start
;
475 res
->end
= save_res
->end
;
476 res
->flags
= save_res
->flags
;
478 free_list(&save_head
);
480 requested_and_reassign
:
481 /* Satisfy the must-have resource requests */
482 assign_requested_resources_sorted(head
, fail_head
);
484 /* Try to satisfy any additional optional resource
487 reassign_resources_sorted(realloc_head
, head
);
491 static void pdev_assign_resources_sorted(struct pci_dev
*dev
,
492 struct list_head
*add_head
,
493 struct list_head
*fail_head
)
497 __dev_sort_resources(dev
, &head
);
498 __assign_resources_sorted(&head
, add_head
, fail_head
);
502 static void pbus_assign_resources_sorted(const struct pci_bus
*bus
,
503 struct list_head
*realloc_head
,
504 struct list_head
*fail_head
)
509 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
510 __dev_sort_resources(dev
, &head
);
512 __assign_resources_sorted(&head
, realloc_head
, fail_head
);
515 void pci_setup_cardbus(struct pci_bus
*bus
)
517 struct pci_dev
*bridge
= bus
->self
;
518 struct resource
*res
;
519 struct pci_bus_region region
;
521 pci_info(bridge
, "CardBus bridge to %pR\n",
524 res
= bus
->resource
[0];
525 pcibios_resource_to_bus(bridge
->bus
, ®ion
, res
);
526 if (res
->flags
& IORESOURCE_IO
) {
528 * The IO resource is allocated a range twice as large as it
529 * would normally need. This allows us to set both IO regs.
531 pci_info(bridge
, " bridge window %pR\n", res
);
532 pci_write_config_dword(bridge
, PCI_CB_IO_BASE_0
,
534 pci_write_config_dword(bridge
, PCI_CB_IO_LIMIT_0
,
538 res
= bus
->resource
[1];
539 pcibios_resource_to_bus(bridge
->bus
, ®ion
, res
);
540 if (res
->flags
& IORESOURCE_IO
) {
541 pci_info(bridge
, " bridge window %pR\n", res
);
542 pci_write_config_dword(bridge
, PCI_CB_IO_BASE_1
,
544 pci_write_config_dword(bridge
, PCI_CB_IO_LIMIT_1
,
548 res
= bus
->resource
[2];
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_0
,
554 pci_write_config_dword(bridge
, PCI_CB_MEMORY_LIMIT_0
,
558 res
= bus
->resource
[3];
559 pcibios_resource_to_bus(bridge
->bus
, ®ion
, res
);
560 if (res
->flags
& IORESOURCE_MEM
) {
561 pci_info(bridge
, " bridge window %pR\n", res
);
562 pci_write_config_dword(bridge
, PCI_CB_MEMORY_BASE_1
,
564 pci_write_config_dword(bridge
, PCI_CB_MEMORY_LIMIT_1
,
568 EXPORT_SYMBOL(pci_setup_cardbus
);
570 /* Initialize bridges with base/limit values we have collected.
571 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
572 requires that if there is no I/O ports or memory behind the
573 bridge, corresponding range must be turned off by writing base
574 value greater than limit to the bridge's base/limit registers.
576 Note: care must be taken when updating I/O base/limit registers
577 of bridges which support 32-bit I/O. This update requires two
578 config space writes, so it's quite possible that an I/O window of
579 the bridge will have some undesirable address (e.g. 0) after the
580 first write. Ditto 64-bit prefetchable MMIO. */
581 static void pci_setup_bridge_io(struct pci_dev
*bridge
)
583 struct resource
*res
;
584 struct pci_bus_region region
;
585 unsigned long io_mask
;
586 u8 io_base_lo
, io_limit_lo
;
590 io_mask
= PCI_IO_RANGE_MASK
;
591 if (bridge
->io_window_1k
)
592 io_mask
= PCI_IO_1K_RANGE_MASK
;
594 /* Set up the top and bottom of the PCI I/O segment for this bus. */
595 res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
+ 0];
596 pcibios_resource_to_bus(bridge
->bus
, ®ion
, res
);
597 if (res
->flags
& IORESOURCE_IO
) {
598 pci_read_config_word(bridge
, PCI_IO_BASE
, &l
);
599 io_base_lo
= (region
.start
>> 8) & io_mask
;
600 io_limit_lo
= (region
.end
>> 8) & io_mask
;
601 l
= ((u16
) io_limit_lo
<< 8) | io_base_lo
;
602 /* Set up upper 16 bits of I/O base/limit. */
603 io_upper16
= (region
.end
& 0xffff0000) | (region
.start
>> 16);
604 pci_info(bridge
, " bridge window %pR\n", res
);
606 /* Clear upper 16 bits of I/O base/limit. */
610 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
611 pci_write_config_dword(bridge
, PCI_IO_BASE_UPPER16
, 0x0000ffff);
612 /* Update lower 16 bits of I/O base/limit. */
613 pci_write_config_word(bridge
, PCI_IO_BASE
, l
);
614 /* Update upper 16 bits of I/O base/limit. */
615 pci_write_config_dword(bridge
, PCI_IO_BASE_UPPER16
, io_upper16
);
618 static void pci_setup_bridge_mmio(struct pci_dev
*bridge
)
620 struct resource
*res
;
621 struct pci_bus_region region
;
624 /* Set up the top and bottom of the PCI Memory segment for this bus. */
625 res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
+ 1];
626 pcibios_resource_to_bus(bridge
->bus
, ®ion
, res
);
627 if (res
->flags
& IORESOURCE_MEM
) {
628 l
= (region
.start
>> 16) & 0xfff0;
629 l
|= region
.end
& 0xfff00000;
630 pci_info(bridge
, " bridge window %pR\n", res
);
634 pci_write_config_dword(bridge
, PCI_MEMORY_BASE
, l
);
637 static void pci_setup_bridge_mmio_pref(struct pci_dev
*bridge
)
639 struct resource
*res
;
640 struct pci_bus_region region
;
643 /* Clear out the upper 32 bits of PREF limit.
644 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
645 disables PREF range, which is ok. */
646 pci_write_config_dword(bridge
, PCI_PREF_LIMIT_UPPER32
, 0);
648 /* Set up PREF base/limit. */
650 res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
+ 2];
651 pcibios_resource_to_bus(bridge
->bus
, ®ion
, res
);
652 if (res
->flags
& IORESOURCE_PREFETCH
) {
653 l
= (region
.start
>> 16) & 0xfff0;
654 l
|= region
.end
& 0xfff00000;
655 if (res
->flags
& IORESOURCE_MEM_64
) {
656 bu
= upper_32_bits(region
.start
);
657 lu
= upper_32_bits(region
.end
);
659 pci_info(bridge
, " bridge window %pR\n", res
);
663 pci_write_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, l
);
665 /* Set the upper 32 bits of PREF base & limit. */
666 pci_write_config_dword(bridge
, PCI_PREF_BASE_UPPER32
, bu
);
667 pci_write_config_dword(bridge
, PCI_PREF_LIMIT_UPPER32
, lu
);
670 static void __pci_setup_bridge(struct pci_bus
*bus
, unsigned long type
)
672 struct pci_dev
*bridge
= bus
->self
;
674 pci_info(bridge
, "PCI bridge to %pR\n",
677 if (type
& IORESOURCE_IO
)
678 pci_setup_bridge_io(bridge
);
680 if (type
& IORESOURCE_MEM
)
681 pci_setup_bridge_mmio(bridge
);
683 if (type
& IORESOURCE_PREFETCH
)
684 pci_setup_bridge_mmio_pref(bridge
);
686 pci_write_config_word(bridge
, PCI_BRIDGE_CONTROL
, bus
->bridge_ctl
);
689 void __weak
pcibios_setup_bridge(struct pci_bus
*bus
, unsigned long type
)
693 void pci_setup_bridge(struct pci_bus
*bus
)
695 unsigned long type
= IORESOURCE_IO
| IORESOURCE_MEM
|
698 pcibios_setup_bridge(bus
, type
);
699 __pci_setup_bridge(bus
, type
);
703 int pci_claim_bridge_resource(struct pci_dev
*bridge
, int i
)
705 if (i
< PCI_BRIDGE_RESOURCES
|| i
> PCI_BRIDGE_RESOURCE_END
)
708 if (pci_claim_resource(bridge
, i
) == 0)
709 return 0; /* claimed the window */
711 if ((bridge
->class >> 8) != PCI_CLASS_BRIDGE_PCI
)
714 if (!pci_bus_clip_resource(bridge
, i
))
715 return -EINVAL
; /* clipping didn't change anything */
717 switch (i
- PCI_BRIDGE_RESOURCES
) {
719 pci_setup_bridge_io(bridge
);
722 pci_setup_bridge_mmio(bridge
);
725 pci_setup_bridge_mmio_pref(bridge
);
731 if (pci_claim_resource(bridge
, i
) == 0)
732 return 0; /* claimed a smaller window */
737 /* Check whether the bridge supports optional I/O and
738 prefetchable memory ranges. If not, the respective
739 base/limit registers must be read-only and read as 0. */
740 static void pci_bridge_check_ranges(struct pci_bus
*bus
)
744 struct pci_dev
*bridge
= bus
->self
;
745 struct resource
*b_res
;
747 b_res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
];
748 b_res
[1].flags
|= IORESOURCE_MEM
;
750 pci_read_config_word(bridge
, PCI_IO_BASE
, &io
);
752 pci_write_config_word(bridge
, PCI_IO_BASE
, 0xe0f0);
753 pci_read_config_word(bridge
, PCI_IO_BASE
, &io
);
754 pci_write_config_word(bridge
, PCI_IO_BASE
, 0x0);
757 b_res
[0].flags
|= IORESOURCE_IO
;
759 /* DECchip 21050 pass 2 errata: the bridge may miss an address
760 disconnect boundary by one PCI data phase.
761 Workaround: do not use prefetching on this device. */
762 if (bridge
->vendor
== PCI_VENDOR_ID_DEC
&& bridge
->device
== 0x0001)
765 pci_read_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, &pmem
);
767 pci_write_config_dword(bridge
, PCI_PREF_MEMORY_BASE
,
769 pci_read_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, &pmem
);
770 pci_write_config_dword(bridge
, PCI_PREF_MEMORY_BASE
, 0x0);
773 b_res
[2].flags
|= IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
774 if ((pmem
& PCI_PREF_RANGE_TYPE_MASK
) ==
775 PCI_PREF_RANGE_TYPE_64
) {
776 b_res
[2].flags
|= IORESOURCE_MEM_64
;
777 b_res
[2].flags
|= PCI_PREF_RANGE_TYPE_64
;
781 /* double check if bridge does support 64 bit pref */
782 if (b_res
[2].flags
& IORESOURCE_MEM_64
) {
783 u32 mem_base_hi
, tmp
;
784 pci_read_config_dword(bridge
, PCI_PREF_BASE_UPPER32
,
786 pci_write_config_dword(bridge
, PCI_PREF_BASE_UPPER32
,
788 pci_read_config_dword(bridge
, PCI_PREF_BASE_UPPER32
, &tmp
);
790 b_res
[2].flags
&= ~IORESOURCE_MEM_64
;
791 pci_write_config_dword(bridge
, PCI_PREF_BASE_UPPER32
,
796 /* Helper function for sizing routines: find first available
797 bus resource of a given type. Note: we intentionally skip
798 the bus resources which have already been assigned (that is,
799 have non-NULL parent resource). */
800 static struct resource
*find_free_bus_resource(struct pci_bus
*bus
,
801 unsigned long type_mask
, unsigned long type
)
806 pci_bus_for_each_resource(bus
, r
, i
) {
807 if (r
== &ioport_resource
|| r
== &iomem_resource
)
809 if (r
&& (r
->flags
& type_mask
) == type
&& !r
->parent
)
815 static resource_size_t
calculate_iosize(resource_size_t size
,
816 resource_size_t min_size
,
817 resource_size_t size1
,
818 resource_size_t old_size
,
819 resource_size_t align
)
825 /* To be fixed in 2.5: we should have sort of HAVE_ISA
826 flag in the struct pci_bus. */
827 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
828 size
= (size
& 0xff) + ((size
& ~0xffUL
) << 2);
830 size
= ALIGN(size
+ size1
, align
);
836 static resource_size_t
calculate_memsize(resource_size_t size
,
837 resource_size_t min_size
,
838 resource_size_t size1
,
839 resource_size_t old_size
,
840 resource_size_t align
)
848 size
= ALIGN(size
+ size1
, align
);
852 resource_size_t __weak
pcibios_window_alignment(struct pci_bus
*bus
,
858 #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */
859 #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */
860 #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */
862 static resource_size_t
window_alignment(struct pci_bus
*bus
,
865 resource_size_t align
= 1, arch_align
;
867 if (type
& IORESOURCE_MEM
)
868 align
= PCI_P2P_DEFAULT_MEM_ALIGN
;
869 else if (type
& IORESOURCE_IO
) {
871 * Per spec, I/O windows are 4K-aligned, but some
872 * bridges have an extension to support 1K alignment.
874 if (bus
->self
->io_window_1k
)
875 align
= PCI_P2P_DEFAULT_IO_ALIGN_1K
;
877 align
= PCI_P2P_DEFAULT_IO_ALIGN
;
880 arch_align
= pcibios_window_alignment(bus
, type
);
881 return max(align
, arch_align
);
885 * pbus_size_io() - size the io window of a given bus
888 * @min_size : the minimum io window that must to be allocated
889 * @add_size : additional optional io window
890 * @realloc_head : track the additional io window on this list
892 * Sizing the IO windows of the PCI-PCI bridge is trivial,
893 * since these windows have 1K or 4K granularity and the IO ranges
894 * of non-bridge PCI devices are limited to 256 bytes.
895 * We must be careful with the ISA aliasing though.
897 static void pbus_size_io(struct pci_bus
*bus
, resource_size_t min_size
,
898 resource_size_t add_size
, struct list_head
*realloc_head
)
901 struct resource
*b_res
= find_free_bus_resource(bus
, IORESOURCE_IO
,
903 resource_size_t size
= 0, size0
= 0, size1
= 0;
904 resource_size_t children_add_size
= 0;
905 resource_size_t min_align
, align
;
910 min_align
= window_alignment(bus
, IORESOURCE_IO
);
911 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
914 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
915 struct resource
*r
= &dev
->resource
[i
];
916 unsigned long r_size
;
918 if (r
->parent
|| !(r
->flags
& IORESOURCE_IO
))
920 r_size
= resource_size(r
);
923 /* Might be re-aligned for ISA */
928 align
= pci_resource_alignment(dev
, r
);
929 if (align
> min_align
)
933 children_add_size
+= get_res_add_size(realloc_head
, r
);
937 size0
= calculate_iosize(size
, min_size
, size1
,
938 resource_size(b_res
), min_align
);
939 if (children_add_size
> add_size
)
940 add_size
= children_add_size
;
941 size1
= (!realloc_head
|| (realloc_head
&& !add_size
)) ? size0
:
942 calculate_iosize(size
, min_size
, add_size
+ size1
,
943 resource_size(b_res
), min_align
);
944 if (!size0
&& !size1
) {
945 if (b_res
->start
|| b_res
->end
)
946 pci_info(bus
->self
, "disabling bridge window %pR to %pR (unused)\n",
947 b_res
, &bus
->busn_res
);
952 b_res
->start
= min_align
;
953 b_res
->end
= b_res
->start
+ size0
- 1;
954 b_res
->flags
|= IORESOURCE_STARTALIGN
;
955 if (size1
> size0
&& realloc_head
) {
956 add_to_list(realloc_head
, bus
->self
, b_res
, size1
-size0
,
958 pci_printk(KERN_DEBUG
, bus
->self
, "bridge window %pR to %pR add_size %llx\n",
959 b_res
, &bus
->busn_res
,
960 (unsigned long long)size1
-size0
);
964 static inline resource_size_t
calculate_mem_align(resource_size_t
*aligns
,
967 resource_size_t align
= 0;
968 resource_size_t min_align
= 0;
971 for (order
= 0; order
<= max_order
; order
++) {
972 resource_size_t align1
= 1;
974 align1
<<= (order
+ 20);
978 else if (ALIGN(align
+ min_align
, min_align
) < align1
)
979 min_align
= align1
>> 1;
980 align
+= aligns
[order
];
987 * pbus_size_mem() - size the memory window of a given bus
990 * @mask: mask the resource flag, then compare it with type
991 * @type: the type of free resource from bridge
992 * @type2: second match type
993 * @type3: third match type
994 * @min_size : the minimum memory window that must to be allocated
995 * @add_size : additional optional memory window
996 * @realloc_head : track the additional memory window on this list
998 * Calculate the size of the bus and minimal alignment which
999 * guarantees that all child resources fit in this size.
1001 * Returns -ENOSPC if there's no available bus resource of the desired type.
1002 * Otherwise, sets the bus resource start/end to indicate the required
1003 * size, adds things to realloc_head (if supplied), and returns 0.
1005 static int pbus_size_mem(struct pci_bus
*bus
, unsigned long mask
,
1006 unsigned long type
, unsigned long type2
,
1007 unsigned long type3
,
1008 resource_size_t min_size
, resource_size_t add_size
,
1009 struct list_head
*realloc_head
)
1011 struct pci_dev
*dev
;
1012 resource_size_t min_align
, align
, size
, size0
, size1
;
1013 resource_size_t aligns
[18]; /* Alignments from 1Mb to 128Gb */
1014 int order
, max_order
;
1015 struct resource
*b_res
= find_free_bus_resource(bus
,
1016 mask
| IORESOURCE_PREFETCH
, type
);
1017 resource_size_t children_add_size
= 0;
1018 resource_size_t children_add_align
= 0;
1019 resource_size_t add_align
= 0;
1024 memset(aligns
, 0, sizeof(aligns
));
1028 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1031 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
1032 struct resource
*r
= &dev
->resource
[i
];
1033 resource_size_t r_size
;
1035 if (r
->parent
|| (r
->flags
& IORESOURCE_PCI_FIXED
) ||
1036 ((r
->flags
& mask
) != type
&&
1037 (r
->flags
& mask
) != type2
&&
1038 (r
->flags
& mask
) != type3
))
1040 r_size
= resource_size(r
);
1041 #ifdef CONFIG_PCI_IOV
1042 /* put SRIOV requested res to the optional list */
1043 if (realloc_head
&& i
>= PCI_IOV_RESOURCES
&&
1044 i
<= PCI_IOV_RESOURCE_END
) {
1045 add_align
= max(pci_resource_alignment(dev
, r
), add_align
);
1046 r
->end
= r
->start
- 1;
1047 add_to_list(realloc_head
, dev
, r
, r_size
, 0/* don't care */);
1048 children_add_size
+= r_size
;
1053 * aligns[0] is for 1MB (since bridge memory
1054 * windows are always at least 1MB aligned), so
1055 * keep "order" from being negative for smaller
1058 align
= pci_resource_alignment(dev
, r
);
1059 order
= __ffs(align
) - 20;
1062 if (order
>= ARRAY_SIZE(aligns
)) {
1063 pci_warn(dev
, "disabling BAR %d: %pR (bad alignment %#llx)\n",
1064 i
, r
, (unsigned long long) align
);
1068 size
+= max(r_size
, align
);
1069 /* Exclude ranges with size > align from
1070 calculation of the alignment. */
1071 if (r_size
<= align
)
1072 aligns
[order
] += align
;
1073 if (order
> max_order
)
1077 children_add_size
+= get_res_add_size(realloc_head
, r
);
1078 children_add_align
= get_res_add_align(realloc_head
, r
);
1079 add_align
= max(add_align
, children_add_align
);
1084 min_align
= calculate_mem_align(aligns
, max_order
);
1085 min_align
= max(min_align
, window_alignment(bus
, b_res
->flags
));
1086 size0
= calculate_memsize(size
, min_size
, 0, resource_size(b_res
), min_align
);
1087 add_align
= max(min_align
, add_align
);
1088 if (children_add_size
> add_size
)
1089 add_size
= children_add_size
;
1090 size1
= (!realloc_head
|| (realloc_head
&& !add_size
)) ? size0
:
1091 calculate_memsize(size
, min_size
, add_size
,
1092 resource_size(b_res
), add_align
);
1093 if (!size0
&& !size1
) {
1094 if (b_res
->start
|| b_res
->end
)
1095 pci_info(bus
->self
, "disabling bridge window %pR to %pR (unused)\n",
1096 b_res
, &bus
->busn_res
);
1100 b_res
->start
= min_align
;
1101 b_res
->end
= size0
+ min_align
- 1;
1102 b_res
->flags
|= IORESOURCE_STARTALIGN
;
1103 if (size1
> size0
&& realloc_head
) {
1104 add_to_list(realloc_head
, bus
->self
, b_res
, size1
-size0
, add_align
);
1105 pci_printk(KERN_DEBUG
, bus
->self
, "bridge window %pR to %pR add_size %llx add_align %llx\n",
1106 b_res
, &bus
->busn_res
,
1107 (unsigned long long) (size1
- size0
),
1108 (unsigned long long) add_align
);
1113 unsigned long pci_cardbus_resource_alignment(struct resource
*res
)
1115 if (res
->flags
& IORESOURCE_IO
)
1116 return pci_cardbus_io_size
;
1117 if (res
->flags
& IORESOURCE_MEM
)
1118 return pci_cardbus_mem_size
;
1122 static void pci_bus_size_cardbus(struct pci_bus
*bus
,
1123 struct list_head
*realloc_head
)
1125 struct pci_dev
*bridge
= bus
->self
;
1126 struct resource
*b_res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
];
1127 resource_size_t b_res_3_size
= pci_cardbus_mem_size
* 2;
1130 if (b_res
[0].parent
)
1131 goto handle_b_res_1
;
1133 * Reserve some resources for CardBus. We reserve
1134 * a fixed amount of bus space for CardBus bridges.
1136 b_res
[0].start
= pci_cardbus_io_size
;
1137 b_res
[0].end
= b_res
[0].start
+ pci_cardbus_io_size
- 1;
1138 b_res
[0].flags
|= IORESOURCE_IO
| IORESOURCE_STARTALIGN
;
1140 b_res
[0].end
-= pci_cardbus_io_size
;
1141 add_to_list(realloc_head
, bridge
, b_res
, pci_cardbus_io_size
,
1142 pci_cardbus_io_size
);
1146 if (b_res
[1].parent
)
1147 goto handle_b_res_2
;
1148 b_res
[1].start
= pci_cardbus_io_size
;
1149 b_res
[1].end
= b_res
[1].start
+ pci_cardbus_io_size
- 1;
1150 b_res
[1].flags
|= IORESOURCE_IO
| IORESOURCE_STARTALIGN
;
1152 b_res
[1].end
-= pci_cardbus_io_size
;
1153 add_to_list(realloc_head
, bridge
, b_res
+1, pci_cardbus_io_size
,
1154 pci_cardbus_io_size
);
1158 /* MEM1 must not be pref mmio */
1159 pci_read_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, &ctrl
);
1160 if (ctrl
& PCI_CB_BRIDGE_CTL_PREFETCH_MEM1
) {
1161 ctrl
&= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1
;
1162 pci_write_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, ctrl
);
1163 pci_read_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, &ctrl
);
1167 * Check whether prefetchable memory is supported
1170 pci_read_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, &ctrl
);
1171 if (!(ctrl
& PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
)) {
1172 ctrl
|= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
;
1173 pci_write_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, ctrl
);
1174 pci_read_config_word(bridge
, PCI_CB_BRIDGE_CONTROL
, &ctrl
);
1177 if (b_res
[2].parent
)
1178 goto handle_b_res_3
;
1180 * If we have prefetchable memory support, allocate
1181 * two regions. Otherwise, allocate one region of
1184 if (ctrl
& PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
) {
1185 b_res
[2].start
= pci_cardbus_mem_size
;
1186 b_res
[2].end
= b_res
[2].start
+ pci_cardbus_mem_size
- 1;
1187 b_res
[2].flags
|= IORESOURCE_MEM
| IORESOURCE_PREFETCH
|
1188 IORESOURCE_STARTALIGN
;
1190 b_res
[2].end
-= pci_cardbus_mem_size
;
1191 add_to_list(realloc_head
, bridge
, b_res
+2,
1192 pci_cardbus_mem_size
, pci_cardbus_mem_size
);
1195 /* reduce that to half */
1196 b_res_3_size
= pci_cardbus_mem_size
;
1200 if (b_res
[3].parent
)
1202 b_res
[3].start
= pci_cardbus_mem_size
;
1203 b_res
[3].end
= b_res
[3].start
+ b_res_3_size
- 1;
1204 b_res
[3].flags
|= IORESOURCE_MEM
| IORESOURCE_STARTALIGN
;
1206 b_res
[3].end
-= b_res_3_size
;
1207 add_to_list(realloc_head
, bridge
, b_res
+3, b_res_3_size
,
1208 pci_cardbus_mem_size
);
1215 void __pci_bus_size_bridges(struct pci_bus
*bus
, struct list_head
*realloc_head
)
1217 struct pci_dev
*dev
;
1218 unsigned long mask
, prefmask
, type2
= 0, type3
= 0;
1219 resource_size_t additional_mem_size
= 0, additional_io_size
= 0;
1220 struct resource
*b_res
;
1223 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1224 struct pci_bus
*b
= dev
->subordinate
;
1228 switch (dev
->class >> 8) {
1229 case PCI_CLASS_BRIDGE_CARDBUS
:
1230 pci_bus_size_cardbus(b
, realloc_head
);
1233 case PCI_CLASS_BRIDGE_PCI
:
1235 __pci_bus_size_bridges(b
, realloc_head
);
1241 if (pci_is_root_bus(bus
))
1244 switch (bus
->self
->class >> 8) {
1245 case PCI_CLASS_BRIDGE_CARDBUS
:
1246 /* don't size cardbuses yet. */
1249 case PCI_CLASS_BRIDGE_PCI
:
1250 pci_bridge_check_ranges(bus
);
1251 if (bus
->self
->is_hotplug_bridge
) {
1252 additional_io_size
= pci_hotplug_io_size
;
1253 additional_mem_size
= pci_hotplug_mem_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 b_res
= &bus
->self
->resource
[PCI_BRIDGE_RESOURCES
];
1266 mask
= IORESOURCE_MEM
;
1267 prefmask
= IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
1268 if (b_res
[2].flags
& IORESOURCE_MEM_64
) {
1269 prefmask
|= IORESOURCE_MEM_64
;
1270 ret
= pbus_size_mem(bus
, prefmask
, prefmask
,
1272 realloc_head
? 0 : additional_mem_size
,
1273 additional_mem_size
, realloc_head
);
1276 * If successful, all non-prefetchable resources
1277 * and any 32-bit prefetchable resources will go in
1278 * the non-prefetchable window.
1282 type2
= prefmask
& ~IORESOURCE_MEM_64
;
1283 type3
= prefmask
& ~IORESOURCE_PREFETCH
;
1288 * If there is no 64-bit prefetchable window, compute the
1289 * size required to put all prefetchable resources in the
1290 * 32-bit prefetchable window (if there is one).
1293 prefmask
&= ~IORESOURCE_MEM_64
;
1294 ret
= pbus_size_mem(bus
, prefmask
, prefmask
,
1296 realloc_head
? 0 : additional_mem_size
,
1297 additional_mem_size
, realloc_head
);
1300 * If successful, only non-prefetchable resources
1301 * will go in the non-prefetchable window.
1306 additional_mem_size
+= additional_mem_size
;
1308 type2
= type3
= IORESOURCE_MEM
;
1312 * Compute the size required to put everything else in the
1313 * non-prefetchable window. This includes:
1315 * - all non-prefetchable resources
1316 * - 32-bit prefetchable resources if there's a 64-bit
1317 * prefetchable window or no prefetchable window at all
1318 * - 64-bit prefetchable resources if there's no
1319 * prefetchable window at all
1321 * Note that the strategy in __pci_assign_resource() must
1322 * match that used here. Specifically, we cannot put a
1323 * 32-bit prefetchable resource in a 64-bit prefetchable
1326 pbus_size_mem(bus
, mask
, IORESOURCE_MEM
, type2
, type3
,
1327 realloc_head
? 0 : additional_mem_size
,
1328 additional_mem_size
, realloc_head
);
1333 void pci_bus_size_bridges(struct pci_bus
*bus
)
1335 __pci_bus_size_bridges(bus
, NULL
);
1337 EXPORT_SYMBOL(pci_bus_size_bridges
);
1339 static void assign_fixed_resource_on_bus(struct pci_bus
*b
, struct resource
*r
)
1342 struct resource
*parent_r
;
1343 unsigned long mask
= IORESOURCE_IO
| IORESOURCE_MEM
|
1344 IORESOURCE_PREFETCH
;
1346 pci_bus_for_each_resource(b
, parent_r
, i
) {
1350 if ((r
->flags
& mask
) == (parent_r
->flags
& mask
) &&
1351 resource_contains(parent_r
, r
))
1352 request_resource(parent_r
, r
);
1357 * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they
1358 * are skipped by pbus_assign_resources_sorted().
1360 static void pdev_assign_fixed_resources(struct pci_dev
*dev
)
1364 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
1366 struct resource
*r
= &dev
->resource
[i
];
1368 if (r
->parent
|| !(r
->flags
& IORESOURCE_PCI_FIXED
) ||
1369 !(r
->flags
& (IORESOURCE_IO
| IORESOURCE_MEM
)))
1373 while (b
&& !r
->parent
) {
1374 assign_fixed_resource_on_bus(b
, r
);
1380 void __pci_bus_assign_resources(const struct pci_bus
*bus
,
1381 struct list_head
*realloc_head
,
1382 struct list_head
*fail_head
)
1385 struct pci_dev
*dev
;
1387 pbus_assign_resources_sorted(bus
, realloc_head
, fail_head
);
1389 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1390 pdev_assign_fixed_resources(dev
);
1392 b
= dev
->subordinate
;
1396 __pci_bus_assign_resources(b
, realloc_head
, fail_head
);
1398 switch (dev
->class >> 8) {
1399 case PCI_CLASS_BRIDGE_PCI
:
1400 if (!pci_is_enabled(dev
))
1401 pci_setup_bridge(b
);
1404 case PCI_CLASS_BRIDGE_CARDBUS
:
1405 pci_setup_cardbus(b
);
1409 pci_info(dev
, "not setting up bridge for bus %04x:%02x\n",
1410 pci_domain_nr(b
), b
->number
);
1416 void pci_bus_assign_resources(const struct pci_bus
*bus
)
1418 __pci_bus_assign_resources(bus
, NULL
, NULL
);
1420 EXPORT_SYMBOL(pci_bus_assign_resources
);
1422 static void pci_claim_device_resources(struct pci_dev
*dev
)
1426 for (i
= 0; i
< PCI_BRIDGE_RESOURCES
; i
++) {
1427 struct resource
*r
= &dev
->resource
[i
];
1429 if (!r
->flags
|| r
->parent
)
1432 pci_claim_resource(dev
, i
);
1436 static void pci_claim_bridge_resources(struct pci_dev
*dev
)
1440 for (i
= PCI_BRIDGE_RESOURCES
; i
< PCI_NUM_RESOURCES
; i
++) {
1441 struct resource
*r
= &dev
->resource
[i
];
1443 if (!r
->flags
|| r
->parent
)
1446 pci_claim_bridge_resource(dev
, i
);
1450 static void pci_bus_allocate_dev_resources(struct pci_bus
*b
)
1452 struct pci_dev
*dev
;
1453 struct pci_bus
*child
;
1455 list_for_each_entry(dev
, &b
->devices
, bus_list
) {
1456 pci_claim_device_resources(dev
);
1458 child
= dev
->subordinate
;
1460 pci_bus_allocate_dev_resources(child
);
1464 static void pci_bus_allocate_resources(struct pci_bus
*b
)
1466 struct pci_bus
*child
;
1469 * Carry out a depth-first search on the PCI bus
1470 * tree to allocate bridge apertures. Read the
1471 * programmed bridge bases and recursively claim
1472 * the respective bridge resources.
1475 pci_read_bridge_bases(b
);
1476 pci_claim_bridge_resources(b
->self
);
1479 list_for_each_entry(child
, &b
->children
, node
)
1480 pci_bus_allocate_resources(child
);
1483 void pci_bus_claim_resources(struct pci_bus
*b
)
1485 pci_bus_allocate_resources(b
);
1486 pci_bus_allocate_dev_resources(b
);
1488 EXPORT_SYMBOL(pci_bus_claim_resources
);
1490 static void __pci_bridge_assign_resources(const struct pci_dev
*bridge
,
1491 struct list_head
*add_head
,
1492 struct list_head
*fail_head
)
1496 pdev_assign_resources_sorted((struct pci_dev
*)bridge
,
1497 add_head
, fail_head
);
1499 b
= bridge
->subordinate
;
1503 __pci_bus_assign_resources(b
, add_head
, fail_head
);
1505 switch (bridge
->class >> 8) {
1506 case PCI_CLASS_BRIDGE_PCI
:
1507 pci_setup_bridge(b
);
1510 case PCI_CLASS_BRIDGE_CARDBUS
:
1511 pci_setup_cardbus(b
);
1515 pci_info(bridge
, "not setting up bridge for bus %04x:%02x\n",
1516 pci_domain_nr(b
), b
->number
);
1521 #define PCI_RES_TYPE_MASK \
1522 (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\
1525 static void pci_bridge_release_resources(struct pci_bus
*bus
,
1528 struct pci_dev
*dev
= bus
->self
;
1530 unsigned old_flags
= 0;
1531 struct resource
*b_res
;
1534 b_res
= &dev
->resource
[PCI_BRIDGE_RESOURCES
];
1537 * 1. if there is io port assign fail, will release bridge
1539 * 2. if there is non pref mmio assign fail, release bridge
1541 * 3. if there is 64bit pref mmio assign fail, and bridge pref
1542 * is 64bit, release bridge pref mmio.
1543 * 4. if there is pref mmio assign fail, and bridge pref is
1544 * 32bit mmio, release bridge pref mmio
1545 * 5. if there is pref mmio assign fail, and bridge pref is not
1546 * assigned, release bridge nonpref mmio.
1548 if (type
& IORESOURCE_IO
)
1550 else if (!(type
& IORESOURCE_PREFETCH
))
1552 else if ((type
& IORESOURCE_MEM_64
) &&
1553 (b_res
[2].flags
& IORESOURCE_MEM_64
))
1555 else if (!(b_res
[2].flags
& IORESOURCE_MEM_64
) &&
1556 (b_res
[2].flags
& IORESOURCE_PREFETCH
))
1567 * if there are children under that, we should release them
1570 release_child_resources(r
);
1571 if (!release_resource(r
)) {
1572 type
= old_flags
= r
->flags
& PCI_RES_TYPE_MASK
;
1573 pci_printk(KERN_DEBUG
, dev
, "resource %d %pR released\n",
1574 PCI_BRIDGE_RESOURCES
+ idx
, r
);
1575 /* keep the old size */
1576 r
->end
= resource_size(r
) - 1;
1580 /* avoiding touch the one without PREF */
1581 if (type
& IORESOURCE_PREFETCH
)
1582 type
= IORESOURCE_PREFETCH
;
1583 __pci_setup_bridge(bus
, type
);
1584 /* for next child res under same bridge */
1585 r
->flags
= old_flags
;
1594 * try to release pci bridge resources that is from leaf bridge,
1595 * so we can allocate big new one later
1597 static void pci_bus_release_bridge_resources(struct pci_bus
*bus
,
1599 enum release_type rel_type
)
1601 struct pci_dev
*dev
;
1602 bool is_leaf_bridge
= true;
1604 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1605 struct pci_bus
*b
= dev
->subordinate
;
1609 is_leaf_bridge
= false;
1611 if ((dev
->class >> 8) != PCI_CLASS_BRIDGE_PCI
)
1614 if (rel_type
== whole_subtree
)
1615 pci_bus_release_bridge_resources(b
, type
,
1619 if (pci_is_root_bus(bus
))
1622 if ((bus
->self
->class >> 8) != PCI_CLASS_BRIDGE_PCI
)
1625 if ((rel_type
== whole_subtree
) || is_leaf_bridge
)
1626 pci_bridge_release_resources(bus
, type
);
1629 static void pci_bus_dump_res(struct pci_bus
*bus
)
1631 struct resource
*res
;
1634 pci_bus_for_each_resource(bus
, res
, i
) {
1635 if (!res
|| !res
->end
|| !res
->flags
)
1638 dev_printk(KERN_DEBUG
, &bus
->dev
, "resource %d %pR\n", i
, res
);
1642 static void pci_bus_dump_resources(struct pci_bus
*bus
)
1645 struct pci_dev
*dev
;
1648 pci_bus_dump_res(bus
);
1650 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1651 b
= dev
->subordinate
;
1655 pci_bus_dump_resources(b
);
1659 static int pci_bus_get_depth(struct pci_bus
*bus
)
1662 struct pci_bus
*child_bus
;
1664 list_for_each_entry(child_bus
, &bus
->children
, node
) {
1667 ret
= pci_bus_get_depth(child_bus
);
1668 if (ret
+ 1 > depth
)
1676 * -1: undefined, will auto detect later
1677 * 0: disabled by user
1678 * 1: disabled by auto detect
1679 * 2: enabled by user
1680 * 3: enabled by auto detect
1690 static enum enable_type pci_realloc_enable
= undefined
;
1691 void __init
pci_realloc_get_opt(char *str
)
1693 if (!strncmp(str
, "off", 3))
1694 pci_realloc_enable
= user_disabled
;
1695 else if (!strncmp(str
, "on", 2))
1696 pci_realloc_enable
= user_enabled
;
1698 static bool pci_realloc_enabled(enum enable_type enable
)
1700 return enable
>= user_enabled
;
1703 #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1704 static int iov_resources_unassigned(struct pci_dev
*dev
, void *data
)
1707 bool *unassigned
= data
;
1709 for (i
= PCI_IOV_RESOURCES
; i
<= PCI_IOV_RESOURCE_END
; i
++) {
1710 struct resource
*r
= &dev
->resource
[i
];
1711 struct pci_bus_region region
;
1713 /* Not assigned or rejected by kernel? */
1717 pcibios_resource_to_bus(dev
->bus
, ®ion
, r
);
1718 if (!region
.start
) {
1720 return 1; /* return early from pci_walk_bus() */
1727 static enum enable_type
pci_realloc_detect(struct pci_bus
*bus
,
1728 enum enable_type enable_local
)
1730 bool unassigned
= false;
1732 if (enable_local
!= undefined
)
1733 return enable_local
;
1735 pci_walk_bus(bus
, iov_resources_unassigned
, &unassigned
);
1737 return auto_enabled
;
1739 return enable_local
;
1742 static enum enable_type
pci_realloc_detect(struct pci_bus
*bus
,
1743 enum enable_type enable_local
)
1745 return enable_local
;
1750 * first try will not touch pci bridge res
1751 * second and later try will clear small leaf bridge res
1752 * will stop till to the max depth if can not find good one
1754 void pci_assign_unassigned_root_bus_resources(struct pci_bus
*bus
)
1756 LIST_HEAD(realloc_head
); /* list of resources that
1757 want additional resources */
1758 struct list_head
*add_list
= NULL
;
1759 int tried_times
= 0;
1760 enum release_type rel_type
= leaf_only
;
1761 LIST_HEAD(fail_head
);
1762 struct pci_dev_resource
*fail_res
;
1763 int pci_try_num
= 1;
1764 enum enable_type enable_local
;
1766 /* don't realloc if asked to do so */
1767 enable_local
= pci_realloc_detect(bus
, pci_realloc_enable
);
1768 if (pci_realloc_enabled(enable_local
)) {
1769 int max_depth
= pci_bus_get_depth(bus
);
1771 pci_try_num
= max_depth
+ 1;
1772 dev_printk(KERN_DEBUG
, &bus
->dev
,
1773 "max bus depth: %d pci_try_num: %d\n",
1774 max_depth
, pci_try_num
);
1779 * last try will use add_list, otherwise will try good to have as
1780 * must have, so can realloc parent bridge resource
1782 if (tried_times
+ 1 == pci_try_num
)
1783 add_list
= &realloc_head
;
1784 /* Depth first, calculate sizes and alignments of all
1785 subordinate buses. */
1786 __pci_bus_size_bridges(bus
, add_list
);
1788 /* Depth last, allocate resources and update the hardware. */
1789 __pci_bus_assign_resources(bus
, add_list
, &fail_head
);
1791 BUG_ON(!list_empty(add_list
));
1794 /* any device complain? */
1795 if (list_empty(&fail_head
))
1798 if (tried_times
>= pci_try_num
) {
1799 if (enable_local
== undefined
)
1800 dev_info(&bus
->dev
, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
1801 else if (enable_local
== auto_enabled
)
1802 dev_info(&bus
->dev
, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
1804 free_list(&fail_head
);
1808 dev_printk(KERN_DEBUG
, &bus
->dev
,
1809 "No. %d try to assign unassigned res\n", tried_times
+ 1);
1811 /* third times and later will not check if it is leaf */
1812 if ((tried_times
+ 1) > 2)
1813 rel_type
= whole_subtree
;
1816 * Try to release leaf bridge's resources that doesn't fit resource of
1817 * child device under that bridge
1819 list_for_each_entry(fail_res
, &fail_head
, list
)
1820 pci_bus_release_bridge_resources(fail_res
->dev
->bus
,
1821 fail_res
->flags
& PCI_RES_TYPE_MASK
,
1824 /* restore size and flags */
1825 list_for_each_entry(fail_res
, &fail_head
, list
) {
1826 struct resource
*res
= fail_res
->res
;
1828 res
->start
= fail_res
->start
;
1829 res
->end
= fail_res
->end
;
1830 res
->flags
= fail_res
->flags
;
1831 if (fail_res
->dev
->subordinate
)
1834 free_list(&fail_head
);
1839 /* dump the resource on buses */
1840 pci_bus_dump_resources(bus
);
1843 void __init
pci_assign_unassigned_resources(void)
1845 struct pci_bus
*root_bus
;
1847 list_for_each_entry(root_bus
, &pci_root_buses
, node
) {
1848 pci_assign_unassigned_root_bus_resources(root_bus
);
1850 /* Make sure the root bridge has a companion ACPI device: */
1851 if (ACPI_HANDLE(root_bus
->bridge
))
1852 acpi_ioapic_add(ACPI_HANDLE(root_bus
->bridge
));
1856 static void extend_bridge_window(struct pci_dev
*bridge
, struct resource
*res
,
1857 struct list_head
*add_list
, resource_size_t available
)
1859 struct pci_dev_resource
*dev_res
;
1864 if (resource_size(res
) >= available
)
1867 dev_res
= res_to_dev_res(add_list
, res
);
1871 /* Is there room to extend the window? */
1872 if (available
- resource_size(res
) <= dev_res
->add_size
)
1875 dev_res
->add_size
= available
- resource_size(res
);
1876 pci_dbg(bridge
, "bridge window %pR extended by %pa\n", res
,
1877 &dev_res
->add_size
);
1880 static void pci_bus_distribute_available_resources(struct pci_bus
*bus
,
1881 struct list_head
*add_list
, resource_size_t available_io
,
1882 resource_size_t available_mmio
, resource_size_t available_mmio_pref
)
1884 resource_size_t remaining_io
, remaining_mmio
, remaining_mmio_pref
;
1885 unsigned int normal_bridges
= 0, hotplug_bridges
= 0;
1886 struct resource
*io_res
, *mmio_res
, *mmio_pref_res
;
1887 struct pci_dev
*dev
, *bridge
= bus
->self
;
1889 io_res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
+ 0];
1890 mmio_res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
+ 1];
1891 mmio_pref_res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
+ 2];
1894 * Update additional resource list (add_list) to fill all the
1895 * extra resource space available for this port except the space
1896 * calculated in __pci_bus_size_bridges() which covers all the
1897 * devices currently connected to the port and below.
1899 extend_bridge_window(bridge
, io_res
, add_list
, available_io
);
1900 extend_bridge_window(bridge
, mmio_res
, add_list
, available_mmio
);
1901 extend_bridge_window(bridge
, mmio_pref_res
, add_list
,
1902 available_mmio_pref
);
1905 * Calculate the total amount of extra resource space we can
1906 * pass to bridges below this one. This is basically the
1907 * extra space reduced by the minimal required space for the
1908 * non-hotplug bridges.
1910 remaining_io
= available_io
;
1911 remaining_mmio
= available_mmio
;
1912 remaining_mmio_pref
= available_mmio_pref
;
1915 * Calculate how many hotplug bridges and normal bridges there
1916 * are on this bus. We will distribute the additional available
1917 * resources between hotplug bridges.
1919 for_each_pci_bridge(dev
, bus
) {
1920 if (dev
->is_hotplug_bridge
)
1926 for_each_pci_bridge(dev
, bus
) {
1927 const struct resource
*res
;
1929 if (dev
->is_hotplug_bridge
)
1933 * Reduce the available resource space by what the
1934 * bridge and devices below it occupy.
1936 res
= &dev
->resource
[PCI_BRIDGE_RESOURCES
+ 0];
1937 if (!res
->parent
&& available_io
> resource_size(res
))
1938 remaining_io
-= resource_size(res
);
1940 res
= &dev
->resource
[PCI_BRIDGE_RESOURCES
+ 1];
1941 if (!res
->parent
&& available_mmio
> resource_size(res
))
1942 remaining_mmio
-= resource_size(res
);
1944 res
= &dev
->resource
[PCI_BRIDGE_RESOURCES
+ 2];
1945 if (!res
->parent
&& available_mmio_pref
> resource_size(res
))
1946 remaining_mmio_pref
-= resource_size(res
);
1950 * Go over devices on this bus and distribute the remaining
1951 * resource space between hotplug bridges.
1953 for_each_pci_bridge(dev
, bus
) {
1956 b
= dev
->subordinate
;
1960 if (!hotplug_bridges
&& normal_bridges
== 1) {
1962 * There is only one bridge on the bus (upstream
1963 * port) so it gets all available resources
1964 * which it can then distribute to the possible
1965 * hotplug bridges below.
1967 pci_bus_distribute_available_resources(b
, add_list
,
1968 available_io
, available_mmio
,
1969 available_mmio_pref
);
1970 } else if (dev
->is_hotplug_bridge
) {
1971 resource_size_t align
, io
, mmio
, mmio_pref
;
1974 * Distribute available extra resources equally
1975 * between hotplug-capable downstream ports
1976 * taking alignment into account.
1978 * Here hotplug_bridges is always != 0.
1980 align
= pci_resource_alignment(bridge
, io_res
);
1981 io
= div64_ul(available_io
, hotplug_bridges
);
1982 io
= min(ALIGN(io
, align
), remaining_io
);
1985 align
= pci_resource_alignment(bridge
, mmio_res
);
1986 mmio
= div64_ul(available_mmio
, hotplug_bridges
);
1987 mmio
= min(ALIGN(mmio
, align
), remaining_mmio
);
1988 remaining_mmio
-= mmio
;
1990 align
= pci_resource_alignment(bridge
, mmio_pref_res
);
1991 mmio_pref
= div64_ul(available_mmio_pref
,
1993 mmio_pref
= min(ALIGN(mmio_pref
, align
),
1994 remaining_mmio_pref
);
1995 remaining_mmio_pref
-= mmio_pref
;
1997 pci_bus_distribute_available_resources(b
, add_list
, io
,
2004 pci_bridge_distribute_available_resources(struct pci_dev
*bridge
,
2005 struct list_head
*add_list
)
2007 resource_size_t available_io
, available_mmio
, available_mmio_pref
;
2008 const struct resource
*res
;
2010 if (!bridge
->is_hotplug_bridge
)
2013 /* Take the initial extra resources from the hotplug port */
2014 res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
+ 0];
2015 available_io
= resource_size(res
);
2016 res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
+ 1];
2017 available_mmio
= resource_size(res
);
2018 res
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
+ 2];
2019 available_mmio_pref
= resource_size(res
);
2021 pci_bus_distribute_available_resources(bridge
->subordinate
,
2022 add_list
, available_io
, available_mmio
, available_mmio_pref
);
2025 void pci_assign_unassigned_bridge_resources(struct pci_dev
*bridge
)
2027 struct pci_bus
*parent
= bridge
->subordinate
;
2028 LIST_HEAD(add_list
); /* list of resources that
2029 want additional resources */
2030 int tried_times
= 0;
2031 LIST_HEAD(fail_head
);
2032 struct pci_dev_resource
*fail_res
;
2036 __pci_bus_size_bridges(parent
, &add_list
);
2039 * Distribute remaining resources (if any) equally between
2040 * hotplug bridges below. This makes it possible to extend the
2041 * hierarchy later without running out of resources.
2043 pci_bridge_distribute_available_resources(bridge
, &add_list
);
2045 __pci_bridge_assign_resources(bridge
, &add_list
, &fail_head
);
2046 BUG_ON(!list_empty(&add_list
));
2049 if (list_empty(&fail_head
))
2052 if (tried_times
>= 2) {
2053 /* still fail, don't need to try more */
2054 free_list(&fail_head
);
2058 printk(KERN_DEBUG
"PCI: No. %d try to assign unassigned res\n",
2062 * Try to release leaf bridge's resources that doesn't fit resource of
2063 * child device under that bridge
2065 list_for_each_entry(fail_res
, &fail_head
, list
)
2066 pci_bus_release_bridge_resources(fail_res
->dev
->bus
,
2067 fail_res
->flags
& PCI_RES_TYPE_MASK
,
2070 /* restore size and flags */
2071 list_for_each_entry(fail_res
, &fail_head
, list
) {
2072 struct resource
*res
= fail_res
->res
;
2074 res
->start
= fail_res
->start
;
2075 res
->end
= fail_res
->end
;
2076 res
->flags
= fail_res
->flags
;
2077 if (fail_res
->dev
->subordinate
)
2080 free_list(&fail_head
);
2085 retval
= pci_reenable_device(bridge
);
2087 pci_err(bridge
, "Error reenabling bridge (%d)\n", retval
);
2088 pci_set_master(bridge
);
2090 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources
);
2092 int pci_reassign_bridge_resources(struct pci_dev
*bridge
, unsigned long type
)
2094 struct pci_dev_resource
*dev_res
;
2095 struct pci_dev
*next
;
2102 /* Walk to the root hub, releasing bridge BARs when possible */
2106 for (i
= PCI_BRIDGE_RESOURCES
; i
< PCI_BRIDGE_RESOURCE_END
;
2108 struct resource
*res
= &bridge
->resource
[i
];
2110 if ((res
->flags
^ type
) & PCI_RES_TYPE_MASK
)
2113 /* Ignore BARs which are still in use */
2117 ret
= add_to_list(&saved
, bridge
, res
, 0, 0);
2121 pci_info(bridge
, "BAR %d: releasing %pR\n",
2125 release_resource(res
);
2130 if (i
== PCI_BRIDGE_RESOURCE_END
)
2133 next
= bridge
->bus
? bridge
->bus
->self
: NULL
;
2136 if (list_empty(&saved
))
2139 __pci_bus_size_bridges(bridge
->subordinate
, &added
);
2140 __pci_bridge_assign_resources(bridge
, &added
, &failed
);
2141 BUG_ON(!list_empty(&added
));
2143 if (!list_empty(&failed
)) {
2148 list_for_each_entry(dev_res
, &saved
, list
) {
2149 /* Skip the bridge we just assigned resources for. */
2150 if (bridge
== dev_res
->dev
)
2153 bridge
= dev_res
->dev
;
2154 pci_setup_bridge(bridge
->subordinate
);
2161 /* restore size and flags */
2162 list_for_each_entry(dev_res
, &failed
, list
) {
2163 struct resource
*res
= dev_res
->res
;
2165 res
->start
= dev_res
->start
;
2166 res
->end
= dev_res
->end
;
2167 res
->flags
= dev_res
->flags
;
2171 /* Revert to the old configuration */
2172 list_for_each_entry(dev_res
, &saved
, list
) {
2173 struct resource
*res
= dev_res
->res
;
2175 bridge
= dev_res
->dev
;
2176 i
= res
- bridge
->resource
;
2178 res
->start
= dev_res
->start
;
2179 res
->end
= dev_res
->end
;
2180 res
->flags
= dev_res
->flags
;
2182 pci_claim_resource(bridge
, i
);
2183 pci_setup_bridge(bridge
->subordinate
);
2190 void pci_assign_unassigned_bus_resources(struct pci_bus
*bus
)
2192 struct pci_dev
*dev
;
2193 LIST_HEAD(add_list
); /* list of resources that
2194 want additional resources */
2196 down_read(&pci_bus_sem
);
2197 for_each_pci_bridge(dev
, bus
)
2198 if (pci_has_subordinate(dev
))
2199 __pci_bus_size_bridges(dev
->subordinate
, &add_list
);
2200 up_read(&pci_bus_sem
);
2201 __pci_bus_assign_resources(bus
, &add_list
, NULL
);
2202 BUG_ON(!list_empty(&add_list
));
2204 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources
);