blob: f1e6333c227e4c412344c4b1d7d5db89b167188f [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Support routines for initializing a PCI subsystem
4 *
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)
9 *
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.
15 */
16
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>
26#include "pci.h"
27
28unsigned int pci_flags;
29
30struct pci_dev_resource {
31 struct list_head list;
32 struct resource *res;
33 struct pci_dev *dev;
34 resource_size_t start;
35 resource_size_t end;
36 resource_size_t add_size;
37 resource_size_t min_align;
38 unsigned long flags;
39};
40
41static void free_list(struct list_head *head)
42{
43 struct pci_dev_resource *dev_res, *tmp;
44
45 list_for_each_entry_safe(dev_res, tmp, head, list) {
46 list_del(&dev_res->list);
47 kfree(dev_res);
48 }
49}
50
51/**
52 * add_to_list() - Add a new resource tracker to the list
53 * @head: Head of the list
54 * @dev: Device to which the resource belongs
55 * @res: Resource to be tracked
56 * @add_size: Additional size to be optionally added to the resource
57 */
58static int add_to_list(struct list_head *head, struct pci_dev *dev,
59 struct resource *res, resource_size_t add_size,
60 resource_size_t min_align)
61{
62 struct pci_dev_resource *tmp;
63
64 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
65 if (!tmp)
66 return -ENOMEM;
67
68 tmp->res = res;
69 tmp->dev = dev;
70 tmp->start = res->start;
71 tmp->end = res->end;
72 tmp->flags = res->flags;
73 tmp->add_size = add_size;
74 tmp->min_align = min_align;
75
76 list_add(&tmp->list, head);
77
78 return 0;
79}
80
81static void remove_from_list(struct list_head *head, struct resource *res)
82{
83 struct pci_dev_resource *dev_res, *tmp;
84
85 list_for_each_entry_safe(dev_res, tmp, head, list) {
86 if (dev_res->res == res) {
87 list_del(&dev_res->list);
88 kfree(dev_res);
89 break;
90 }
91 }
92}
93
94static struct pci_dev_resource *res_to_dev_res(struct list_head *head,
95 struct resource *res)
96{
97 struct pci_dev_resource *dev_res;
98
99 list_for_each_entry(dev_res, head, list) {
100 if (dev_res->res == res)
101 return dev_res;
102 }
103
104 return NULL;
105}
106
107static resource_size_t get_res_add_size(struct list_head *head,
108 struct resource *res)
109{
110 struct pci_dev_resource *dev_res;
111
112 dev_res = res_to_dev_res(head, res);
113 return dev_res ? dev_res->add_size : 0;
114}
115
116static resource_size_t get_res_add_align(struct list_head *head,
117 struct resource *res)
118{
119 struct pci_dev_resource *dev_res;
120
121 dev_res = res_to_dev_res(head, res);
122 return dev_res ? dev_res->min_align : 0;
123}
124
125
126/* Sort resources by alignment */
127static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
128{
129 int i;
130
131 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
132 struct resource *r;
133 struct pci_dev_resource *dev_res, *tmp;
134 resource_size_t r_align;
135 struct list_head *n;
136
137 r = &dev->resource[i];
138#ifdef CONFIG_PCIE_ASR1803
139 if (r->flags == 0x14220c)
140 r->flags = 0;
141#endif
142 if (r->flags & IORESOURCE_PCI_FIXED)
143 continue;
144
145 if (!(r->flags) || r->parent)
146 continue;
147
148 r_align = pci_resource_alignment(dev, r);
149 if (!r_align) {
150 pci_warn(dev, "BAR %d: %pR has bogus alignment\n",
151 i, r);
152 continue;
153 }
154
155 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
156 if (!tmp)
157 panic("pdev_sort_resources(): kmalloc() failed!\n");
158 tmp->res = r;
159 tmp->dev = dev;
160
161 /* Fallback is smallest one or list is empty */
162 n = head;
163 list_for_each_entry(dev_res, head, list) {
164 resource_size_t align;
165
166 align = pci_resource_alignment(dev_res->dev,
167 dev_res->res);
168
169 if (r_align > align) {
170 n = &dev_res->list;
171 break;
172 }
173 }
174 /* Insert it just before n */
175 list_add_tail(&tmp->list, n);
176 }
177}
178
179static void __dev_sort_resources(struct pci_dev *dev, struct list_head *head)
180{
181 u16 class = dev->class >> 8;
182
183 /* Don't touch classless devices or host bridges or IOAPICs */
184 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
185 return;
186
187 /* Don't touch IOAPIC devices already enabled by firmware */
188 if (class == PCI_CLASS_SYSTEM_PIC) {
189 u16 command;
190 pci_read_config_word(dev, PCI_COMMAND, &command);
191 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
192 return;
193 }
194
195 pdev_sort_resources(dev, head);
196}
197
198static inline void reset_resource(struct resource *res)
199{
200 res->start = 0;
201 res->end = 0;
202 res->flags = 0;
203}
204
205/**
206 * reassign_resources_sorted() - Satisfy any additional resource requests
207 *
208 * @realloc_head: Head of the list tracking requests requiring
209 * additional resources
210 * @head: Head of the list tracking requests with allocated
211 * resources
212 *
213 * Walk through each element of the realloc_head and try to procure additional
214 * resources for the element, provided the element is in the head list.
215 */
216static void reassign_resources_sorted(struct list_head *realloc_head,
217 struct list_head *head)
218{
219 struct resource *res;
220 struct pci_dev_resource *add_res, *tmp;
221 struct pci_dev_resource *dev_res;
222 resource_size_t add_size, align;
223 int idx;
224
225 list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
226 bool found_match = false;
227
228 res = add_res->res;
229 /* Skip resource that has been reset */
230 if (!res->flags)
231 goto out;
232
233 /* Skip this resource if not found in head list */
234 list_for_each_entry(dev_res, head, list) {
235 if (dev_res->res == res) {
236 found_match = true;
237 break;
238 }
239 }
240 if (!found_match) /* Just skip */
241 continue;
242
243 idx = res - &add_res->dev->resource[0];
244 add_size = add_res->add_size;
245 align = add_res->min_align;
246 if (!resource_size(res)) {
247 res->start = align;
248 res->end = res->start + add_size - 1;
249 if (pci_assign_resource(add_res->dev, idx))
250 reset_resource(res);
251 } else {
252 res->flags |= add_res->flags &
253 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
254 if (pci_reassign_resource(add_res->dev, idx,
255 add_size, align))
256 pci_info(add_res->dev, "failed to add %llx res[%d]=%pR\n",
257 (unsigned long long) add_size, idx,
258 res);
259 }
260out:
261 list_del(&add_res->list);
262 kfree(add_res);
263 }
264}
265
266/**
267 * assign_requested_resources_sorted() - Satisfy resource requests
268 *
269 * @head: Head of the list tracking requests for resources
270 * @fail_head: Head of the list tracking requests that could not be
271 * allocated
272 *
273 * Satisfy resource requests of each element in the list. Add requests that
274 * could not be satisfied to the failed_list.
275 */
276static void assign_requested_resources_sorted(struct list_head *head,
277 struct list_head *fail_head)
278{
279 struct resource *res;
280 struct pci_dev_resource *dev_res;
281 int idx;
282
283 list_for_each_entry(dev_res, head, list) {
284 res = dev_res->res;
285 idx = res - &dev_res->dev->resource[0];
286 if (resource_size(res) &&
287 pci_assign_resource(dev_res->dev, idx)) {
288 if (fail_head) {
289 /*
290 * If the failed resource is a ROM BAR and
291 * it will be enabled later, don't add it
292 * to the list.
293 */
294 if (!((idx == PCI_ROM_RESOURCE) &&
295 (!(res->flags & IORESOURCE_ROM_ENABLE))))
296 add_to_list(fail_head,
297 dev_res->dev, res,
298 0 /* don't care */,
299 0 /* don't care */);
300 }
301 reset_resource(res);
302 }
303 }
304}
305
306static unsigned long pci_fail_res_type_mask(struct list_head *fail_head)
307{
308 struct pci_dev_resource *fail_res;
309 unsigned long mask = 0;
310
311 /* Check failed type */
312 list_for_each_entry(fail_res, fail_head, list)
313 mask |= fail_res->flags;
314
315 /*
316 * One pref failed resource will set IORESOURCE_MEM, as we can
317 * allocate pref in non-pref range. Will release all assigned
318 * non-pref sibling resources according to that bit.
319 */
320 return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
321}
322
323static bool pci_need_to_release(unsigned long mask, struct resource *res)
324{
325 if (res->flags & IORESOURCE_IO)
326 return !!(mask & IORESOURCE_IO);
327
328 /* Check pref at first */
329 if (res->flags & IORESOURCE_PREFETCH) {
330 if (mask & IORESOURCE_PREFETCH)
331 return true;
332 /* Count pref if its parent is non-pref */
333 else if ((mask & IORESOURCE_MEM) &&
334 !(res->parent->flags & IORESOURCE_PREFETCH))
335 return true;
336 else
337 return false;
338 }
339
340 if (res->flags & IORESOURCE_MEM)
341 return !!(mask & IORESOURCE_MEM);
342
343 return false; /* Should not get here */
344}
345
346static void __assign_resources_sorted(struct list_head *head,
347 struct list_head *realloc_head,
348 struct list_head *fail_head)
349{
350 /*
351 * Should not assign requested resources at first. They could be
352 * adjacent, so later reassign can not reallocate them one by one in
353 * parent resource window.
354 *
355 * Try to assign requested + add_size at beginning. If could do that,
356 * could get out early. If could not do that, we still try to assign
357 * requested at first, then try to reassign add_size for some resources.
358 *
359 * Separate three resource type checking if we need to release
360 * assigned resource after requested + add_size try.
361 *
362 * 1. If IO port assignment fails, will release assigned IO
363 * port.
364 * 2. If pref MMIO assignment fails, release assigned pref
365 * MMIO. If assigned pref MMIO's parent is non-pref MMIO
366 * and non-pref MMIO assignment fails, will release that
367 * assigned pref MMIO.
368 * 3. If non-pref MMIO assignment fails or pref MMIO
369 * assignment fails, will release assigned non-pref MMIO.
370 */
371 LIST_HEAD(save_head);
372 LIST_HEAD(local_fail_head);
373 struct pci_dev_resource *save_res;
374 struct pci_dev_resource *dev_res, *tmp_res, *dev_res2;
375 unsigned long fail_type;
376 resource_size_t add_align, align;
377
378 /* Check if optional add_size is there */
379 if (!realloc_head || list_empty(realloc_head))
380 goto requested_and_reassign;
381
382 /* Save original start, end, flags etc at first */
383 list_for_each_entry(dev_res, head, list) {
384 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
385 free_list(&save_head);
386 goto requested_and_reassign;
387 }
388 }
389
390 /* Update res in head list with add_size in realloc_head list */
391 list_for_each_entry_safe(dev_res, tmp_res, head, list) {
392 dev_res->res->end += get_res_add_size(realloc_head,
393 dev_res->res);
394
395 /*
396 * There are two kinds of additional resources in the list:
397 * 1. bridge resource -- IORESOURCE_STARTALIGN
398 * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN
399 * Here just fix the additional alignment for bridge
400 */
401 if (!(dev_res->res->flags & IORESOURCE_STARTALIGN))
402 continue;
403
404 add_align = get_res_add_align(realloc_head, dev_res->res);
405
406 /*
407 * The "head" list is sorted by alignment so resources with
408 * bigger alignment will be assigned first. After we
409 * change the alignment of a dev_res in "head" list, we
410 * need to reorder the list by alignment to make it
411 * consistent.
412 */
413 if (add_align > dev_res->res->start) {
414 resource_size_t r_size = resource_size(dev_res->res);
415
416 dev_res->res->start = add_align;
417 dev_res->res->end = add_align + r_size - 1;
418
419 list_for_each_entry(dev_res2, head, list) {
420 align = pci_resource_alignment(dev_res2->dev,
421 dev_res2->res);
422 if (add_align > align) {
423 list_move_tail(&dev_res->list,
424 &dev_res2->list);
425 break;
426 }
427 }
428 }
429
430 }
431
432 /* Try updated head list with add_size added */
433 assign_requested_resources_sorted(head, &local_fail_head);
434
435 /* All assigned with add_size? */
436 if (list_empty(&local_fail_head)) {
437 /* Remove head list from realloc_head list */
438 list_for_each_entry(dev_res, head, list)
439 remove_from_list(realloc_head, dev_res->res);
440 free_list(&save_head);
441 free_list(head);
442 return;
443 }
444
445 /* Check failed type */
446 fail_type = pci_fail_res_type_mask(&local_fail_head);
447 /* Remove not need to be released assigned res from head list etc */
448 list_for_each_entry_safe(dev_res, tmp_res, head, list)
449 if (dev_res->res->parent &&
450 !pci_need_to_release(fail_type, dev_res->res)) {
451 /* Remove it from realloc_head list */
452 remove_from_list(realloc_head, dev_res->res);
453 remove_from_list(&save_head, dev_res->res);
454 list_del(&dev_res->list);
455 kfree(dev_res);
456 }
457
458 free_list(&local_fail_head);
459 /* Release assigned resource */
460 list_for_each_entry(dev_res, head, list)
461 if (dev_res->res->parent)
462 release_resource(dev_res->res);
463 /* Restore start/end/flags from saved list */
464 list_for_each_entry(save_res, &save_head, list) {
465 struct resource *res = save_res->res;
466
467 res->start = save_res->start;
468 res->end = save_res->end;
469 res->flags = save_res->flags;
470 }
471 free_list(&save_head);
472
473requested_and_reassign:
474 /* Satisfy the must-have resource requests */
475 assign_requested_resources_sorted(head, fail_head);
476
477 /* Try to satisfy any additional optional resource requests */
478 if (realloc_head)
479 reassign_resources_sorted(realloc_head, head);
480 free_list(head);
481}
482
483static void pdev_assign_resources_sorted(struct pci_dev *dev,
484 struct list_head *add_head,
485 struct list_head *fail_head)
486{
487 LIST_HEAD(head);
488
489 __dev_sort_resources(dev, &head);
490 __assign_resources_sorted(&head, add_head, fail_head);
491
492}
493
494static void pbus_assign_resources_sorted(const struct pci_bus *bus,
495 struct list_head *realloc_head,
496 struct list_head *fail_head)
497{
498 struct pci_dev *dev;
499 LIST_HEAD(head);
500
501 list_for_each_entry(dev, &bus->devices, bus_list)
502 __dev_sort_resources(dev, &head);
503
504 __assign_resources_sorted(&head, realloc_head, fail_head);
505}
506
507void pci_setup_cardbus(struct pci_bus *bus)
508{
509 struct pci_dev *bridge = bus->self;
510 struct resource *res;
511 struct pci_bus_region region;
512
513 pci_info(bridge, "CardBus bridge to %pR\n",
514 &bus->busn_res);
515
516 res = bus->resource[0];
517 pcibios_resource_to_bus(bridge->bus, &region, res);
518 if (res->flags & IORESOURCE_IO) {
519 /*
520 * The IO resource is allocated a range twice as large as it
521 * would normally need. This allows us to set both IO regs.
522 */
523 pci_info(bridge, " bridge window %pR\n", res);
524 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
525 region.start);
526 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
527 region.end);
528 }
529
530 res = bus->resource[1];
531 pcibios_resource_to_bus(bridge->bus, &region, res);
532 if (res->flags & IORESOURCE_IO) {
533 pci_info(bridge, " bridge window %pR\n", res);
534 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
535 region.start);
536 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
537 region.end);
538 }
539
540 res = bus->resource[2];
541 pcibios_resource_to_bus(bridge->bus, &region, res);
542 if (res->flags & IORESOURCE_MEM) {
543 pci_info(bridge, " bridge window %pR\n", res);
544 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
545 region.start);
546 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
547 region.end);
548 }
549
550 res = bus->resource[3];
551 pcibios_resource_to_bus(bridge->bus, &region, res);
552 if (res->flags & IORESOURCE_MEM) {
553 pci_info(bridge, " bridge window %pR\n", res);
554 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
555 region.start);
556 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
557 region.end);
558 }
559}
560EXPORT_SYMBOL(pci_setup_cardbus);
561
562/*
563 * Initialize bridges with base/limit values we have collected. PCI-to-PCI
564 * Bridge Architecture Specification rev. 1.1 (1998) requires that if there
565 * are no I/O ports or memory behind the bridge, the corresponding range
566 * must be turned off by writing base value greater than limit to the
567 * bridge's base/limit registers.
568 *
569 * Note: care must be taken when updating I/O base/limit registers of
570 * bridges which support 32-bit I/O. This update requires two config space
571 * writes, so it's quite possible that an I/O window of the bridge will
572 * have some undesirable address (e.g. 0) after the first write. Ditto
573 * 64-bit prefetchable MMIO.
574 */
575static void pci_setup_bridge_io(struct pci_dev *bridge)
576{
577 struct resource *res;
578 struct pci_bus_region region;
579 unsigned long io_mask;
580 u8 io_base_lo, io_limit_lo;
581 u16 l;
582 u32 io_upper16;
583
584 io_mask = PCI_IO_RANGE_MASK;
585 if (bridge->io_window_1k)
586 io_mask = PCI_IO_1K_RANGE_MASK;
587
588 /* Set up the top and bottom of the PCI I/O segment for this bus */
589 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
590 pcibios_resource_to_bus(bridge->bus, &region, res);
591 if (res->flags & IORESOURCE_IO) {
592 pci_read_config_word(bridge, PCI_IO_BASE, &l);
593 io_base_lo = (region.start >> 8) & io_mask;
594 io_limit_lo = (region.end >> 8) & io_mask;
595 l = ((u16) io_limit_lo << 8) | io_base_lo;
596 /* Set up upper 16 bits of I/O base/limit */
597 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
598 pci_info(bridge, " bridge window %pR\n", res);
599 } else {
600 /* Clear upper 16 bits of I/O base/limit */
601 io_upper16 = 0;
602 l = 0x00f0;
603 }
604 /* Temporarily disable the I/O range before updating PCI_IO_BASE */
605 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
606 /* Update lower 16 bits of I/O base/limit */
607 pci_write_config_word(bridge, PCI_IO_BASE, l);
608 /* Update upper 16 bits of I/O base/limit */
609 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
610}
611
612static void pci_setup_bridge_mmio(struct pci_dev *bridge)
613{
614 struct resource *res;
615 struct pci_bus_region region;
616 u32 l;
617
618 /* Set up the top and bottom of the PCI Memory segment for this bus */
619 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
620 pcibios_resource_to_bus(bridge->bus, &region, res);
621 if (res->flags & IORESOURCE_MEM) {
622 l = (region.start >> 16) & 0xfff0;
623 l |= region.end & 0xfff00000;
624 pci_info(bridge, " bridge window %pR\n", res);
625 } else {
626 l = 0x0000fff0;
627 }
628 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
629}
630
631static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge)
632{
633 struct resource *res;
634 struct pci_bus_region region;
635 u32 l, bu, lu;
636
637 /*
638 * Clear out the upper 32 bits of PREF limit. If
639 * PCI_PREF_BASE_UPPER32 was non-zero, this temporarily disables
640 * PREF range, which is ok.
641 */
642 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
643
644 /* Set up PREF base/limit */
645 bu = lu = 0;
646 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
647 pcibios_resource_to_bus(bridge->bus, &region, res);
648 if (res->flags & IORESOURCE_PREFETCH) {
649 l = (region.start >> 16) & 0xfff0;
650 l |= region.end & 0xfff00000;
651 if (res->flags & IORESOURCE_MEM_64) {
652 bu = upper_32_bits(region.start);
653 lu = upper_32_bits(region.end);
654 }
655 pci_info(bridge, " bridge window %pR\n", res);
656 } else {
657 l = 0x0000fff0;
658 }
659 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
660
661 /* Set the upper 32 bits of PREF base & limit */
662 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
663 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
664}
665
666static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
667{
668 struct pci_dev *bridge = bus->self;
669
670 pci_info(bridge, "PCI bridge to %pR\n",
671 &bus->busn_res);
672
673 if (type & IORESOURCE_IO)
674 pci_setup_bridge_io(bridge);
675
676 if (type & IORESOURCE_MEM)
677 pci_setup_bridge_mmio(bridge);
678
679 if (type & IORESOURCE_PREFETCH)
680 pci_setup_bridge_mmio_pref(bridge);
681
682 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
683}
684
685void __weak pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
686{
687}
688
689void pci_setup_bridge(struct pci_bus *bus)
690{
691 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
692 IORESOURCE_PREFETCH;
693
694 pcibios_setup_bridge(bus, type);
695 __pci_setup_bridge(bus, type);
696}
697
698
699int pci_claim_bridge_resource(struct pci_dev *bridge, int i)
700{
701 if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END)
702 return 0;
703
704 if (pci_claim_resource(bridge, i) == 0)
705 return 0; /* Claimed the window */
706
707 if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
708 return 0;
709
710 if (!pci_bus_clip_resource(bridge, i))
711 return -EINVAL; /* Clipping didn't change anything */
712
713 switch (i - PCI_BRIDGE_RESOURCES) {
714 case 0:
715 pci_setup_bridge_io(bridge);
716 break;
717 case 1:
718 pci_setup_bridge_mmio(bridge);
719 break;
720 case 2:
721 pci_setup_bridge_mmio_pref(bridge);
722 break;
723 default:
724 return -EINVAL;
725 }
726
727 if (pci_claim_resource(bridge, i) == 0)
728 return 0; /* Claimed a smaller window */
729
730 return -EINVAL;
731}
732
733/*
734 * Check whether the bridge supports optional I/O and prefetchable memory
735 * ranges. If not, the respective base/limit registers must be read-only
736 * and read as 0.
737 */
738static void pci_bridge_check_ranges(struct pci_bus *bus)
739{
740 struct pci_dev *bridge = bus->self;
741 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
742
743 b_res[1].flags |= IORESOURCE_MEM;
744
745 if (bridge->io_window)
746 b_res[0].flags |= IORESOURCE_IO;
747
748 if (bridge->pref_window) {
749 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
750 if (bridge->pref_64_window) {
751 b_res[2].flags |= IORESOURCE_MEM_64;
752 b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
753 }
754 }
755}
756
757/*
758 * Helper function for sizing routines. Assigned resources have non-NULL
759 * parent resource.
760 *
761 * Return first unassigned resource of the correct type. If there is none,
762 * return first assigned resource of the correct type. If none of the
763 * above, return NULL.
764 *
765 * Returning an assigned resource of the correct type allows the caller to
766 * distinguish between already assigned and no resource of the correct type.
767 */
768static struct resource *find_bus_resource_of_type(struct pci_bus *bus,
769 unsigned long type_mask,
770 unsigned long type)
771{
772 struct resource *r, *r_assigned = NULL;
773 int i;
774
775 pci_bus_for_each_resource(bus, r, i) {
776 if (r == &ioport_resource || r == &iomem_resource)
777 continue;
778 if (r && (r->flags & type_mask) == type && !r->parent)
779 return r;
780 if (r && (r->flags & type_mask) == type && !r_assigned)
781 r_assigned = r;
782 }
783 return r_assigned;
784}
785
786static resource_size_t calculate_iosize(resource_size_t size,
787 resource_size_t min_size,
788 resource_size_t size1,
789 resource_size_t add_size,
790 resource_size_t children_add_size,
791 resource_size_t old_size,
792 resource_size_t align)
793{
794 if (size < min_size)
795 size = min_size;
796 if (old_size == 1)
797 old_size = 0;
798 /*
799 * To be fixed in 2.5: we should have sort of HAVE_ISA flag in the
800 * struct pci_bus.
801 */
802#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
803 size = (size & 0xff) + ((size & ~0xffUL) << 2);
804#endif
805 size = size + size1;
806 if (size < old_size)
807 size = old_size;
808
809 size = ALIGN(max(size, add_size) + children_add_size, align);
810 return size;
811}
812
813static resource_size_t calculate_memsize(resource_size_t size,
814 resource_size_t min_size,
815 resource_size_t add_size,
816 resource_size_t children_add_size,
817 resource_size_t old_size,
818 resource_size_t align)
819{
820 if (size < min_size)
821 size = min_size;
822 if (old_size == 1)
823 old_size = 0;
824
825 size = max(size, add_size) + children_add_size;
826 return ALIGN(max(size, old_size), align);
827}
828
829resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
830 unsigned long type)
831{
832 return 1;
833}
834
835#define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */
836#define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */
837#define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */
838
839static resource_size_t window_alignment(struct pci_bus *bus, unsigned long type)
840{
841 resource_size_t align = 1, arch_align;
842
843 if (type & IORESOURCE_MEM)
844 align = PCI_P2P_DEFAULT_MEM_ALIGN;
845 else if (type & IORESOURCE_IO) {
846 /*
847 * Per spec, I/O windows are 4K-aligned, but some bridges have
848 * an extension to support 1K alignment.
849 */
850 if (bus->self->io_window_1k)
851 align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
852 else
853 align = PCI_P2P_DEFAULT_IO_ALIGN;
854 }
855
856 arch_align = pcibios_window_alignment(bus, type);
857 return max(align, arch_align);
858}
859
860/**
861 * pbus_size_io() - Size the I/O window of a given bus
862 *
863 * @bus: The bus
864 * @min_size: The minimum I/O window that must be allocated
865 * @add_size: Additional optional I/O window
866 * @realloc_head: Track the additional I/O window on this list
867 *
868 * Sizing the I/O windows of the PCI-PCI bridge is trivial, since these
869 * windows have 1K or 4K granularity and the I/O ranges of non-bridge PCI
870 * devices are limited to 256 bytes. We must be careful with the ISA
871 * aliasing though.
872 */
873static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
874 resource_size_t add_size,
875 struct list_head *realloc_head)
876{
877 struct pci_dev *dev;
878 struct resource *b_res = find_bus_resource_of_type(bus, IORESOURCE_IO,
879 IORESOURCE_IO);
880 resource_size_t size = 0, size0 = 0, size1 = 0;
881 resource_size_t children_add_size = 0;
882 resource_size_t min_align, align;
883
884 if (!b_res)
885 return;
886
887 /* If resource is already assigned, nothing more to do */
888 if (b_res->parent)
889 return;
890
891 min_align = window_alignment(bus, IORESOURCE_IO);
892 list_for_each_entry(dev, &bus->devices, bus_list) {
893 int i;
894
895 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
896 struct resource *r = &dev->resource[i];
897 unsigned long r_size;
898
899 if (r->parent || !(r->flags & IORESOURCE_IO))
900 continue;
901 r_size = resource_size(r);
902
903 if (r_size < 0x400)
904 /* Might be re-aligned for ISA */
905 size += r_size;
906 else
907 size1 += r_size;
908
909 align = pci_resource_alignment(dev, r);
910 if (align > min_align)
911 min_align = align;
912
913 if (realloc_head)
914 children_add_size += get_res_add_size(realloc_head, r);
915 }
916 }
917
918 size0 = calculate_iosize(size, min_size, size1, 0, 0,
919 resource_size(b_res), min_align);
920 size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
921 calculate_iosize(size, min_size, size1, add_size, children_add_size,
922 resource_size(b_res), min_align);
923 if (!size0 && !size1) {
924 if (b_res->start || b_res->end)
925 pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
926 b_res, &bus->busn_res);
927 b_res->flags = 0;
928 return;
929 }
930
931 b_res->start = min_align;
932 b_res->end = b_res->start + size0 - 1;
933 b_res->flags |= IORESOURCE_STARTALIGN;
934 if (size1 > size0 && realloc_head) {
935 add_to_list(realloc_head, bus->self, b_res, size1-size0,
936 min_align);
937 pci_info(bus->self, "bridge window %pR to %pR add_size %llx\n",
938 b_res, &bus->busn_res,
939 (unsigned long long) size1 - size0);
940 }
941}
942
943static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
944 int max_order)
945{
946 resource_size_t align = 0;
947 resource_size_t min_align = 0;
948 int order;
949
950 for (order = 0; order <= max_order; order++) {
951 resource_size_t align1 = 1;
952
953 align1 <<= (order + 20);
954
955 if (!align)
956 min_align = align1;
957 else if (ALIGN(align + min_align, min_align) < align1)
958 min_align = align1 >> 1;
959 align += aligns[order];
960 }
961
962 return min_align;
963}
964
965/**
966 * pbus_size_mem() - Size the memory window of a given bus
967 *
968 * @bus: The bus
969 * @mask: Mask the resource flag, then compare it with type
970 * @type: The type of free resource from bridge
971 * @type2: Second match type
972 * @type3: Third match type
973 * @min_size: The minimum memory window that must be allocated
974 * @add_size: Additional optional memory window
975 * @realloc_head: Track the additional memory window on this list
976 *
977 * Calculate the size of the bus and minimal alignment which guarantees
978 * that all child resources fit in this size.
979 *
980 * Return -ENOSPC if there's no available bus resource of the desired
981 * type. Otherwise, set the bus resource start/end to indicate the
982 * required size, add things to realloc_head (if supplied), and return 0.
983 */
984static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
985 unsigned long type, unsigned long type2,
986 unsigned long type3, resource_size_t min_size,
987 resource_size_t add_size,
988 struct list_head *realloc_head)
989{
990 struct pci_dev *dev;
991 resource_size_t min_align, align, size, size0, size1;
992 resource_size_t aligns[18]; /* Alignments from 1MB to 128GB */
993 int order, max_order;
994 struct resource *b_res = find_bus_resource_of_type(bus,
995 mask | IORESOURCE_PREFETCH, type);
996 resource_size_t children_add_size = 0;
997 resource_size_t children_add_align = 0;
998 resource_size_t add_align = 0;
999
1000 if (!b_res)
1001 return -ENOSPC;
1002
1003 /* If resource is already assigned, nothing more to do */
1004 if (b_res->parent)
1005 return 0;
1006
1007 memset(aligns, 0, sizeof(aligns));
1008 max_order = 0;
1009 size = 0;
1010
1011 list_for_each_entry(dev, &bus->devices, bus_list) {
1012 int i;
1013
1014 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1015 struct resource *r = &dev->resource[i];
1016 resource_size_t r_size;
1017
1018 if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) ||
1019 ((r->flags & mask) != type &&
1020 (r->flags & mask) != type2 &&
1021 (r->flags & mask) != type3))
1022 continue;
1023 r_size = resource_size(r);
1024#ifdef CONFIG_PCI_IOV
1025 /* Put SRIOV requested res to the optional list */
1026 if (realloc_head && i >= PCI_IOV_RESOURCES &&
1027 i <= PCI_IOV_RESOURCE_END) {
1028 add_align = max(pci_resource_alignment(dev, r), add_align);
1029 r->end = r->start - 1;
1030 add_to_list(realloc_head, dev, r, r_size, 0 /* Don't care */);
1031 children_add_size += r_size;
1032 continue;
1033 }
1034#endif
1035 /*
1036 * aligns[0] is for 1MB (since bridge memory
1037 * windows are always at least 1MB aligned), so
1038 * keep "order" from being negative for smaller
1039 * resources.
1040 */
1041 align = pci_resource_alignment(dev, r);
1042 order = __ffs(align) - 20;
1043 if (order < 0)
1044 order = 0;
1045 if (order >= ARRAY_SIZE(aligns)) {
1046 pci_warn(dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
1047 i, r, (unsigned long long) align);
1048 r->flags = 0;
1049 continue;
1050 }
1051 size += max(r_size, align);
1052 /*
1053 * Exclude ranges with size > align from calculation of
1054 * the alignment.
1055 */
1056 if (r_size <= align)
1057 aligns[order] += align;
1058 if (order > max_order)
1059 max_order = order;
1060
1061 if (realloc_head) {
1062 children_add_size += get_res_add_size(realloc_head, r);
1063 children_add_align = get_res_add_align(realloc_head, r);
1064 add_align = max(add_align, children_add_align);
1065 }
1066 }
1067 }
1068
1069 min_align = calculate_mem_align(aligns, max_order);
1070 min_align = max(min_align, window_alignment(bus, b_res->flags));
1071 size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), min_align);
1072 add_align = max(min_align, add_align);
1073 size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
1074 calculate_memsize(size, min_size, add_size, children_add_size,
1075 resource_size(b_res), add_align);
1076 if (!size0 && !size1) {
1077 if (b_res->start || b_res->end)
1078 pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
1079 b_res, &bus->busn_res);
1080 b_res->flags = 0;
1081 return 0;
1082 }
1083 b_res->start = min_align;
1084 b_res->end = size0 + min_align - 1;
1085 b_res->flags |= IORESOURCE_STARTALIGN;
1086 if (size1 > size0 && realloc_head) {
1087 add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align);
1088 pci_info(bus->self, "bridge window %pR to %pR add_size %llx add_align %llx\n",
1089 b_res, &bus->busn_res,
1090 (unsigned long long) (size1 - size0),
1091 (unsigned long long) add_align);
1092 }
1093 return 0;
1094}
1095
1096unsigned long pci_cardbus_resource_alignment(struct resource *res)
1097{
1098 if (res->flags & IORESOURCE_IO)
1099 return pci_cardbus_io_size;
1100 if (res->flags & IORESOURCE_MEM)
1101 return pci_cardbus_mem_size;
1102 return 0;
1103}
1104
1105static void pci_bus_size_cardbus(struct pci_bus *bus,
1106 struct list_head *realloc_head)
1107{
1108 struct pci_dev *bridge = bus->self;
1109 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
1110 resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
1111 u16 ctrl;
1112
1113 if (b_res[0].parent)
1114 goto handle_b_res_1;
1115 /*
1116 * Reserve some resources for CardBus. We reserve a fixed amount
1117 * of bus space for CardBus bridges.
1118 */
1119 b_res[0].start = pci_cardbus_io_size;
1120 b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
1121 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
1122 if (realloc_head) {
1123 b_res[0].end -= pci_cardbus_io_size;
1124 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
1125 pci_cardbus_io_size);
1126 }
1127
1128handle_b_res_1:
1129 if (b_res[1].parent)
1130 goto handle_b_res_2;
1131 b_res[1].start = pci_cardbus_io_size;
1132 b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
1133 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
1134 if (realloc_head) {
1135 b_res[1].end -= pci_cardbus_io_size;
1136 add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
1137 pci_cardbus_io_size);
1138 }
1139
1140handle_b_res_2:
1141 /* MEM1 must not be pref MMIO */
1142 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1143 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
1144 ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
1145 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1146 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1147 }
1148
1149 /* Check whether prefetchable memory is supported by this bridge. */
1150 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1151 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
1152 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
1153 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1154 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1155 }
1156
1157 if (b_res[2].parent)
1158 goto handle_b_res_3;
1159 /*
1160 * If we have prefetchable memory support, allocate two regions.
1161 * Otherwise, allocate one region of twice the size.
1162 */
1163 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
1164 b_res[2].start = pci_cardbus_mem_size;
1165 b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
1166 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
1167 IORESOURCE_STARTALIGN;
1168 if (realloc_head) {
1169 b_res[2].end -= pci_cardbus_mem_size;
1170 add_to_list(realloc_head, bridge, b_res+2,
1171 pci_cardbus_mem_size, pci_cardbus_mem_size);
1172 }
1173
1174 /* Reduce that to half */
1175 b_res_3_size = pci_cardbus_mem_size;
1176 }
1177
1178handle_b_res_3:
1179 if (b_res[3].parent)
1180 goto handle_done;
1181 b_res[3].start = pci_cardbus_mem_size;
1182 b_res[3].end = b_res[3].start + b_res_3_size - 1;
1183 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
1184 if (realloc_head) {
1185 b_res[3].end -= b_res_3_size;
1186 add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
1187 pci_cardbus_mem_size);
1188 }
1189
1190handle_done:
1191 ;
1192}
1193
1194void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
1195{
1196 struct pci_dev *dev;
1197 unsigned long mask, prefmask, type2 = 0, type3 = 0;
1198 resource_size_t additional_mem_size = 0, additional_io_size = 0;
1199 struct resource *b_res;
1200 int ret;
1201
1202 list_for_each_entry(dev, &bus->devices, bus_list) {
1203 struct pci_bus *b = dev->subordinate;
1204 if (!b)
1205 continue;
1206
1207 switch (dev->hdr_type) {
1208 case PCI_HEADER_TYPE_CARDBUS:
1209 pci_bus_size_cardbus(b, realloc_head);
1210 break;
1211
1212 case PCI_HEADER_TYPE_BRIDGE:
1213 default:
1214 __pci_bus_size_bridges(b, realloc_head);
1215 break;
1216 }
1217 }
1218
1219 /* The root bus? */
1220 if (pci_is_root_bus(bus))
1221 return;
1222
1223 switch (bus->self->hdr_type) {
1224 case PCI_HEADER_TYPE_CARDBUS:
1225 /* Don't size CardBuses yet */
1226 break;
1227
1228 case PCI_HEADER_TYPE_BRIDGE:
1229 pci_bridge_check_ranges(bus);
1230 if (bus->self->is_hotplug_bridge) {
1231 additional_io_size = pci_hotplug_io_size;
1232 additional_mem_size = pci_hotplug_mem_size;
1233 }
1234 /* Fall through */
1235 default:
1236 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
1237 additional_io_size, realloc_head);
1238
1239 /*
1240 * If there's a 64-bit prefetchable MMIO window, compute
1241 * the size required to put all 64-bit prefetchable
1242 * resources in it.
1243 */
1244 b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES];
1245 mask = IORESOURCE_MEM;
1246 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
1247 if (b_res[2].flags & IORESOURCE_MEM_64) {
1248 prefmask |= IORESOURCE_MEM_64;
1249 ret = pbus_size_mem(bus, prefmask, prefmask,
1250 prefmask, prefmask,
1251 realloc_head ? 0 : additional_mem_size,
1252 additional_mem_size, realloc_head);
1253
1254 /*
1255 * If successful, all non-prefetchable resources
1256 * and any 32-bit prefetchable resources will go in
1257 * the non-prefetchable window.
1258 */
1259 if (ret == 0) {
1260 mask = prefmask;
1261 type2 = prefmask & ~IORESOURCE_MEM_64;
1262 type3 = prefmask & ~IORESOURCE_PREFETCH;
1263 }
1264 }
1265
1266 /*
1267 * If there is no 64-bit prefetchable window, compute the
1268 * size required to put all prefetchable resources in the
1269 * 32-bit prefetchable window (if there is one).
1270 */
1271 if (!type2) {
1272 prefmask &= ~IORESOURCE_MEM_64;
1273 ret = pbus_size_mem(bus, prefmask, prefmask,
1274 prefmask, prefmask,
1275 realloc_head ? 0 : additional_mem_size,
1276 additional_mem_size, realloc_head);
1277
1278 /*
1279 * If successful, only non-prefetchable resources
1280 * will go in the non-prefetchable window.
1281 */
1282 if (ret == 0)
1283 mask = prefmask;
1284 else
1285 additional_mem_size += additional_mem_size;
1286
1287 type2 = type3 = IORESOURCE_MEM;
1288 }
1289
1290 /*
1291 * Compute the size required to put everything else in the
1292 * non-prefetchable window. This includes:
1293 *
1294 * - all non-prefetchable resources
1295 * - 32-bit prefetchable resources if there's a 64-bit
1296 * prefetchable window or no prefetchable window at all
1297 * - 64-bit prefetchable resources if there's no prefetchable
1298 * window at all
1299 *
1300 * Note that the strategy in __pci_assign_resource() must match
1301 * that used here. Specifically, we cannot put a 32-bit
1302 * prefetchable resource in a 64-bit prefetchable window.
1303 */
1304 pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
1305 realloc_head ? 0 : additional_mem_size,
1306 additional_mem_size, realloc_head);
1307 break;
1308 }
1309}
1310
1311void pci_bus_size_bridges(struct pci_bus *bus)
1312{
1313 __pci_bus_size_bridges(bus, NULL);
1314}
1315EXPORT_SYMBOL(pci_bus_size_bridges);
1316
1317static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r)
1318{
1319 int i;
1320 struct resource *parent_r;
1321 unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM |
1322 IORESOURCE_PREFETCH;
1323
1324 pci_bus_for_each_resource(b, parent_r, i) {
1325 if (!parent_r)
1326 continue;
1327
1328 if ((r->flags & mask) == (parent_r->flags & mask) &&
1329 resource_contains(parent_r, r))
1330 request_resource(parent_r, r);
1331 }
1332}
1333
1334/*
1335 * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they are
1336 * skipped by pbus_assign_resources_sorted().
1337 */
1338static void pdev_assign_fixed_resources(struct pci_dev *dev)
1339{
1340 int i;
1341
1342 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1343 struct pci_bus *b;
1344 struct resource *r = &dev->resource[i];
1345
1346 if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) ||
1347 !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
1348 continue;
1349
1350 b = dev->bus;
1351 while (b && !r->parent) {
1352 assign_fixed_resource_on_bus(b, r);
1353 b = b->parent;
1354 }
1355 }
1356}
1357
1358void __pci_bus_assign_resources(const struct pci_bus *bus,
1359 struct list_head *realloc_head,
1360 struct list_head *fail_head)
1361{
1362 struct pci_bus *b;
1363 struct pci_dev *dev;
1364
1365 pbus_assign_resources_sorted(bus, realloc_head, fail_head);
1366
1367 list_for_each_entry(dev, &bus->devices, bus_list) {
1368 pdev_assign_fixed_resources(dev);
1369
1370 b = dev->subordinate;
1371 if (!b)
1372 continue;
1373
1374 __pci_bus_assign_resources(b, realloc_head, fail_head);
1375
1376 switch (dev->hdr_type) {
1377 case PCI_HEADER_TYPE_BRIDGE:
1378 if (!pci_is_enabled(dev))
1379 pci_setup_bridge(b);
1380 break;
1381
1382 case PCI_HEADER_TYPE_CARDBUS:
1383 pci_setup_cardbus(b);
1384 break;
1385
1386 default:
1387 pci_info(dev, "not setting up bridge for bus %04x:%02x\n",
1388 pci_domain_nr(b), b->number);
1389 break;
1390 }
1391 }
1392}
1393
1394void pci_bus_assign_resources(const struct pci_bus *bus)
1395{
1396 __pci_bus_assign_resources(bus, NULL, NULL);
1397}
1398EXPORT_SYMBOL(pci_bus_assign_resources);
1399
1400static void pci_claim_device_resources(struct pci_dev *dev)
1401{
1402 int i;
1403
1404 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
1405 struct resource *r = &dev->resource[i];
1406
1407 if (!r->flags || r->parent)
1408 continue;
1409
1410 pci_claim_resource(dev, i);
1411 }
1412}
1413
1414static void pci_claim_bridge_resources(struct pci_dev *dev)
1415{
1416 int i;
1417
1418 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
1419 struct resource *r = &dev->resource[i];
1420
1421 if (!r->flags || r->parent)
1422 continue;
1423
1424 pci_claim_bridge_resource(dev, i);
1425 }
1426}
1427
1428static void pci_bus_allocate_dev_resources(struct pci_bus *b)
1429{
1430 struct pci_dev *dev;
1431 struct pci_bus *child;
1432
1433 list_for_each_entry(dev, &b->devices, bus_list) {
1434 pci_claim_device_resources(dev);
1435
1436 child = dev->subordinate;
1437 if (child)
1438 pci_bus_allocate_dev_resources(child);
1439 }
1440}
1441
1442static void pci_bus_allocate_resources(struct pci_bus *b)
1443{
1444 struct pci_bus *child;
1445
1446 /*
1447 * Carry out a depth-first search on the PCI bus tree to allocate
1448 * bridge apertures. Read the programmed bridge bases and
1449 * recursively claim the respective bridge resources.
1450 */
1451 if (b->self) {
1452 pci_read_bridge_bases(b);
1453 pci_claim_bridge_resources(b->self);
1454 }
1455
1456 list_for_each_entry(child, &b->children, node)
1457 pci_bus_allocate_resources(child);
1458}
1459
1460void pci_bus_claim_resources(struct pci_bus *b)
1461{
1462 pci_bus_allocate_resources(b);
1463 pci_bus_allocate_dev_resources(b);
1464}
1465EXPORT_SYMBOL(pci_bus_claim_resources);
1466
1467static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
1468 struct list_head *add_head,
1469 struct list_head *fail_head)
1470{
1471 struct pci_bus *b;
1472
1473 pdev_assign_resources_sorted((struct pci_dev *)bridge,
1474 add_head, fail_head);
1475
1476 b = bridge->subordinate;
1477 if (!b)
1478 return;
1479
1480 __pci_bus_assign_resources(b, add_head, fail_head);
1481
1482 switch (bridge->class >> 8) {
1483 case PCI_CLASS_BRIDGE_PCI:
1484 pci_setup_bridge(b);
1485 break;
1486
1487 case PCI_CLASS_BRIDGE_CARDBUS:
1488 pci_setup_cardbus(b);
1489 break;
1490
1491 default:
1492 pci_info(bridge, "not setting up bridge for bus %04x:%02x\n",
1493 pci_domain_nr(b), b->number);
1494 break;
1495 }
1496}
1497
1498#define PCI_RES_TYPE_MASK \
1499 (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\
1500 IORESOURCE_MEM_64)
1501
1502static void pci_bridge_release_resources(struct pci_bus *bus,
1503 unsigned long type)
1504{
1505 struct pci_dev *dev = bus->self;
1506 struct resource *r;
1507 unsigned old_flags = 0;
1508 struct resource *b_res;
1509 int idx = 1;
1510
1511 b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
1512
1513 /*
1514 * 1. If IO port assignment fails, release bridge IO port.
1515 * 2. If non pref MMIO assignment fails, release bridge nonpref MMIO.
1516 * 3. If 64bit pref MMIO assignment fails, and bridge pref is 64bit,
1517 * release bridge pref MMIO.
1518 * 4. If pref MMIO assignment fails, and bridge pref is 32bit,
1519 * release bridge pref MMIO.
1520 * 5. If pref MMIO assignment fails, and bridge pref is not
1521 * assigned, release bridge nonpref MMIO.
1522 */
1523 if (type & IORESOURCE_IO)
1524 idx = 0;
1525 else if (!(type & IORESOURCE_PREFETCH))
1526 idx = 1;
1527 else if ((type & IORESOURCE_MEM_64) &&
1528 (b_res[2].flags & IORESOURCE_MEM_64))
1529 idx = 2;
1530 else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
1531 (b_res[2].flags & IORESOURCE_PREFETCH))
1532 idx = 2;
1533 else
1534 idx = 1;
1535
1536 r = &b_res[idx];
1537
1538 if (!r->parent)
1539 return;
1540
1541 /* If there are children, release them all */
1542 release_child_resources(r);
1543 if (!release_resource(r)) {
1544 type = old_flags = r->flags & PCI_RES_TYPE_MASK;
1545 pci_info(dev, "resource %d %pR released\n",
1546 PCI_BRIDGE_RESOURCES + idx, r);
1547 /* Keep the old size */
1548 r->end = resource_size(r) - 1;
1549 r->start = 0;
1550 r->flags = 0;
1551
1552 /* Avoiding touch the one without PREF */
1553 if (type & IORESOURCE_PREFETCH)
1554 type = IORESOURCE_PREFETCH;
1555 __pci_setup_bridge(bus, type);
1556 /* For next child res under same bridge */
1557 r->flags = old_flags;
1558 }
1559}
1560
1561enum release_type {
1562 leaf_only,
1563 whole_subtree,
1564};
1565
1566/*
1567 * Try to release PCI bridge resources from leaf bridge, so we can allocate
1568 * a larger window later.
1569 */
1570static void pci_bus_release_bridge_resources(struct pci_bus *bus,
1571 unsigned long type,
1572 enum release_type rel_type)
1573{
1574 struct pci_dev *dev;
1575 bool is_leaf_bridge = true;
1576
1577 list_for_each_entry(dev, &bus->devices, bus_list) {
1578 struct pci_bus *b = dev->subordinate;
1579 if (!b)
1580 continue;
1581
1582 is_leaf_bridge = false;
1583
1584 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1585 continue;
1586
1587 if (rel_type == whole_subtree)
1588 pci_bus_release_bridge_resources(b, type,
1589 whole_subtree);
1590 }
1591
1592 if (pci_is_root_bus(bus))
1593 return;
1594
1595 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1596 return;
1597
1598 if ((rel_type == whole_subtree) || is_leaf_bridge)
1599 pci_bridge_release_resources(bus, type);
1600}
1601
1602static void pci_bus_dump_res(struct pci_bus *bus)
1603{
1604 struct resource *res;
1605 int i;
1606
1607 pci_bus_for_each_resource(bus, res, i) {
1608 if (!res || !res->end || !res->flags)
1609 continue;
1610
1611 dev_info(&bus->dev, "resource %d %pR\n", i, res);
1612 }
1613}
1614
1615static void pci_bus_dump_resources(struct pci_bus *bus)
1616{
1617 struct pci_bus *b;
1618 struct pci_dev *dev;
1619
1620
1621 pci_bus_dump_res(bus);
1622
1623 list_for_each_entry(dev, &bus->devices, bus_list) {
1624 b = dev->subordinate;
1625 if (!b)
1626 continue;
1627
1628 pci_bus_dump_resources(b);
1629 }
1630}
1631
1632static int pci_bus_get_depth(struct pci_bus *bus)
1633{
1634 int depth = 0;
1635 struct pci_bus *child_bus;
1636
1637 list_for_each_entry(child_bus, &bus->children, node) {
1638 int ret;
1639
1640 ret = pci_bus_get_depth(child_bus);
1641 if (ret + 1 > depth)
1642 depth = ret + 1;
1643 }
1644
1645 return depth;
1646}
1647
1648/*
1649 * -1: undefined, will auto detect later
1650 * 0: disabled by user
1651 * 1: disabled by auto detect
1652 * 2: enabled by user
1653 * 3: enabled by auto detect
1654 */
1655enum enable_type {
1656 undefined = -1,
1657 user_disabled,
1658 auto_disabled,
1659 user_enabled,
1660 auto_enabled,
1661};
1662
1663static enum enable_type pci_realloc_enable = undefined;
1664void __init pci_realloc_get_opt(char *str)
1665{
1666 if (!strncmp(str, "off", 3))
1667 pci_realloc_enable = user_disabled;
1668 else if (!strncmp(str, "on", 2))
1669 pci_realloc_enable = user_enabled;
1670}
1671static bool pci_realloc_enabled(enum enable_type enable)
1672{
1673 return enable >= user_enabled;
1674}
1675
1676#if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1677static int iov_resources_unassigned(struct pci_dev *dev, void *data)
1678{
1679 int i;
1680 bool *unassigned = data;
1681
1682 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1683 struct resource *r = &dev->resource[i + PCI_IOV_RESOURCES];
1684 struct pci_bus_region region;
1685
1686 /* Not assigned or rejected by kernel? */
1687 if (!r->flags)
1688 continue;
1689
1690 pcibios_resource_to_bus(dev->bus, &region, r);
1691 if (!region.start) {
1692 *unassigned = true;
1693 return 1; /* Return early from pci_walk_bus() */
1694 }
1695 }
1696
1697 return 0;
1698}
1699
1700static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1701 enum enable_type enable_local)
1702{
1703 bool unassigned = false;
1704 struct pci_host_bridge *host;
1705
1706 if (enable_local != undefined)
1707 return enable_local;
1708
1709 host = pci_find_host_bridge(bus);
1710 if (host->preserve_config)
1711 return auto_disabled;
1712
1713 pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
1714 if (unassigned)
1715 return auto_enabled;
1716
1717 return enable_local;
1718}
1719#else
1720static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1721 enum enable_type enable_local)
1722{
1723 return enable_local;
1724}
1725#endif
1726
1727/*
1728 * First try will not touch PCI bridge res.
1729 * Second and later try will clear small leaf bridge res.
1730 * Will stop till to the max depth if can not find good one.
1731 */
1732void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
1733{
1734 LIST_HEAD(realloc_head);
1735 /* List of resources that want additional resources */
1736 struct list_head *add_list = NULL;
1737 int tried_times = 0;
1738 enum release_type rel_type = leaf_only;
1739 LIST_HEAD(fail_head);
1740 struct pci_dev_resource *fail_res;
1741 int pci_try_num = 1;
1742 enum enable_type enable_local;
1743
1744 /* Don't realloc if asked to do so */
1745 enable_local = pci_realloc_detect(bus, pci_realloc_enable);
1746 if (pci_realloc_enabled(enable_local)) {
1747 int max_depth = pci_bus_get_depth(bus);
1748
1749 pci_try_num = max_depth + 1;
1750 dev_info(&bus->dev, "max bus depth: %d pci_try_num: %d\n",
1751 max_depth, pci_try_num);
1752 }
1753
1754again:
1755 /*
1756 * Last try will use add_list, otherwise will try good to have as must
1757 * have, so can realloc parent bridge resource
1758 */
1759 if (tried_times + 1 == pci_try_num)
1760 add_list = &realloc_head;
1761 /*
1762 * Depth first, calculate sizes and alignments of all subordinate buses.
1763 */
1764 __pci_bus_size_bridges(bus, add_list);
1765
1766 /* Depth last, allocate resources and update the hardware. */
1767 __pci_bus_assign_resources(bus, add_list, &fail_head);
1768 if (add_list)
1769 BUG_ON(!list_empty(add_list));
1770 tried_times++;
1771
1772 /* Any device complain? */
1773 if (list_empty(&fail_head))
1774 goto dump;
1775
1776 if (tried_times >= pci_try_num) {
1777 if (enable_local == undefined)
1778 dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
1779 else if (enable_local == auto_enabled)
1780 dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
1781
1782 free_list(&fail_head);
1783 goto dump;
1784 }
1785
1786 dev_info(&bus->dev, "No. %d try to assign unassigned res\n",
1787 tried_times + 1);
1788
1789 /* Third times and later will not check if it is leaf */
1790 if ((tried_times + 1) > 2)
1791 rel_type = whole_subtree;
1792
1793 /*
1794 * Try to release leaf bridge's resources that doesn't fit resource of
1795 * child device under that bridge.
1796 */
1797 list_for_each_entry(fail_res, &fail_head, list)
1798 pci_bus_release_bridge_resources(fail_res->dev->bus,
1799 fail_res->flags & PCI_RES_TYPE_MASK,
1800 rel_type);
1801
1802 /* Restore size and flags */
1803 list_for_each_entry(fail_res, &fail_head, list) {
1804 struct resource *res = fail_res->res;
1805 int idx;
1806
1807 res->start = fail_res->start;
1808 res->end = fail_res->end;
1809 res->flags = fail_res->flags;
1810
1811 if (pci_is_bridge(fail_res->dev)) {
1812 idx = res - &fail_res->dev->resource[0];
1813 if (idx >= PCI_BRIDGE_RESOURCES &&
1814 idx <= PCI_BRIDGE_RESOURCE_END)
1815 res->flags = 0;
1816 }
1817 }
1818 free_list(&fail_head);
1819
1820 goto again;
1821
1822dump:
1823 /* Dump the resource on buses */
1824 pci_bus_dump_resources(bus);
1825}
1826
1827void __init pci_assign_unassigned_resources(void)
1828{
1829 struct pci_bus *root_bus;
1830
1831 list_for_each_entry(root_bus, &pci_root_buses, node) {
1832 pci_assign_unassigned_root_bus_resources(root_bus);
1833
1834 /* Make sure the root bridge has a companion ACPI device */
1835 if (ACPI_HANDLE(root_bus->bridge))
1836 acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
1837 }
1838}
1839
1840static void extend_bridge_window(struct pci_dev *bridge, struct resource *res,
1841 struct list_head *add_list,
1842 resource_size_t available)
1843{
1844 struct pci_dev_resource *dev_res;
1845
1846 if (res->parent)
1847 return;
1848
1849 if (resource_size(res) >= available)
1850 return;
1851
1852 dev_res = res_to_dev_res(add_list, res);
1853 if (!dev_res)
1854 return;
1855
1856 /* Is there room to extend the window? */
1857 if (available - resource_size(res) <= dev_res->add_size)
1858 return;
1859
1860 dev_res->add_size = available - resource_size(res);
1861 pci_dbg(bridge, "bridge window %pR extended by %pa\n", res,
1862 &dev_res->add_size);
1863}
1864
1865static void pci_bus_distribute_available_resources(struct pci_bus *bus,
1866 struct list_head *add_list,
1867 resource_size_t available_io,
1868 resource_size_t available_mmio,
1869 resource_size_t available_mmio_pref)
1870{
1871 resource_size_t remaining_io, remaining_mmio, remaining_mmio_pref;
1872 unsigned int normal_bridges = 0, hotplug_bridges = 0;
1873 struct resource *io_res, *mmio_res, *mmio_pref_res;
1874 struct pci_dev *dev, *bridge = bus->self;
1875
1876 io_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
1877 mmio_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
1878 mmio_pref_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
1879
1880 /*
1881 * Update additional resource list (add_list) to fill all the
1882 * extra resource space available for this port except the space
1883 * calculated in __pci_bus_size_bridges() which covers all the
1884 * devices currently connected to the port and below.
1885 */
1886 extend_bridge_window(bridge, io_res, add_list, available_io);
1887 extend_bridge_window(bridge, mmio_res, add_list, available_mmio);
1888 extend_bridge_window(bridge, mmio_pref_res, add_list,
1889 available_mmio_pref);
1890
1891 /*
1892 * Calculate how many hotplug bridges and normal bridges there
1893 * are on this bus. We will distribute the additional available
1894 * resources between hotplug bridges.
1895 */
1896 for_each_pci_bridge(dev, bus) {
1897 if (dev->is_hotplug_bridge)
1898 hotplug_bridges++;
1899 else
1900 normal_bridges++;
1901 }
1902
1903 /*
1904 * There is only one bridge on the bus so it gets all available
1905 * resources which it can then distribute to the possible hotplug
1906 * bridges below.
1907 */
1908 if (hotplug_bridges + normal_bridges == 1) {
1909 dev = list_first_entry(&bus->devices, struct pci_dev, bus_list);
1910 if (dev->subordinate) {
1911 pci_bus_distribute_available_resources(dev->subordinate,
1912 add_list, available_io, available_mmio,
1913 available_mmio_pref);
1914 }
1915 return;
1916 }
1917
1918 if (hotplug_bridges == 0)
1919 return;
1920
1921 /*
1922 * Calculate the total amount of extra resource space we can
1923 * pass to bridges below this one. This is basically the
1924 * extra space reduced by the minimal required space for the
1925 * non-hotplug bridges.
1926 */
1927 remaining_io = available_io;
1928 remaining_mmio = available_mmio;
1929 remaining_mmio_pref = available_mmio_pref;
1930
1931 for_each_pci_bridge(dev, bus) {
1932 const struct resource *res;
1933
1934 if (dev->is_hotplug_bridge)
1935 continue;
1936
1937 /*
1938 * Reduce the available resource space by what the
1939 * bridge and devices below it occupy.
1940 */
1941 res = &dev->resource[PCI_BRIDGE_RESOURCES + 0];
1942 if (!res->parent && available_io > resource_size(res))
1943 remaining_io -= resource_size(res);
1944
1945 res = &dev->resource[PCI_BRIDGE_RESOURCES + 1];
1946 if (!res->parent && available_mmio > resource_size(res))
1947 remaining_mmio -= resource_size(res);
1948
1949 res = &dev->resource[PCI_BRIDGE_RESOURCES + 2];
1950 if (!res->parent && available_mmio_pref > resource_size(res))
1951 remaining_mmio_pref -= resource_size(res);
1952 }
1953
1954 /*
1955 * Go over devices on this bus and distribute the remaining
1956 * resource space between hotplug bridges.
1957 */
1958 for_each_pci_bridge(dev, bus) {
1959 resource_size_t align, io, mmio, mmio_pref;
1960 struct pci_bus *b;
1961
1962 b = dev->subordinate;
1963 if (!b || !dev->is_hotplug_bridge)
1964 continue;
1965
1966 /*
1967 * Distribute available extra resources equally between
1968 * hotplug-capable downstream ports taking alignment into
1969 * account.
1970 */
1971 align = pci_resource_alignment(bridge, io_res);
1972 io = div64_ul(available_io, hotplug_bridges);
1973 io = min(ALIGN(io, align), remaining_io);
1974 remaining_io -= io;
1975
1976 align = pci_resource_alignment(bridge, mmio_res);
1977 mmio = div64_ul(available_mmio, hotplug_bridges);
1978 mmio = min(ALIGN(mmio, align), remaining_mmio);
1979 remaining_mmio -= mmio;
1980
1981 align = pci_resource_alignment(bridge, mmio_pref_res);
1982 mmio_pref = div64_ul(available_mmio_pref, hotplug_bridges);
1983 mmio_pref = min(ALIGN(mmio_pref, align), remaining_mmio_pref);
1984 remaining_mmio_pref -= mmio_pref;
1985
1986 pci_bus_distribute_available_resources(b, add_list, io, mmio,
1987 mmio_pref);
1988 }
1989}
1990
1991static void pci_bridge_distribute_available_resources(struct pci_dev *bridge,
1992 struct list_head *add_list)
1993{
1994 resource_size_t available_io, available_mmio, available_mmio_pref;
1995 const struct resource *res;
1996
1997 if (!bridge->is_hotplug_bridge)
1998 return;
1999
2000 /* Take the initial extra resources from the hotplug port */
2001 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
2002 available_io = resource_size(res);
2003 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
2004 available_mmio = resource_size(res);
2005 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
2006 available_mmio_pref = resource_size(res);
2007
2008 pci_bus_distribute_available_resources(bridge->subordinate,
2009 add_list, available_io,
2010 available_mmio,
2011 available_mmio_pref);
2012}
2013
2014void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
2015{
2016 struct pci_bus *parent = bridge->subordinate;
2017 /* List of resources that want additional resources */
2018 LIST_HEAD(add_list);
2019
2020 int tried_times = 0;
2021 LIST_HEAD(fail_head);
2022 struct pci_dev_resource *fail_res;
2023 int retval;
2024
2025again:
2026 __pci_bus_size_bridges(parent, &add_list);
2027
2028 /*
2029 * Distribute remaining resources (if any) equally between hotplug
2030 * bridges below. This makes it possible to extend the hierarchy
2031 * later without running out of resources.
2032 */
2033 pci_bridge_distribute_available_resources(bridge, &add_list);
2034
2035 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
2036 BUG_ON(!list_empty(&add_list));
2037 tried_times++;
2038
2039 if (list_empty(&fail_head))
2040 goto enable_all;
2041
2042 if (tried_times >= 2) {
2043 /* Still fail, don't need to try more */
2044 free_list(&fail_head);
2045 goto enable_all;
2046 }
2047
2048 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
2049 tried_times + 1);
2050
2051 /*
2052 * Try to release leaf bridge's resources that aren't big enough
2053 * to contain child device resources.
2054 */
2055 list_for_each_entry(fail_res, &fail_head, list)
2056 pci_bus_release_bridge_resources(fail_res->dev->bus,
2057 fail_res->flags & PCI_RES_TYPE_MASK,
2058 whole_subtree);
2059
2060 /* Restore size and flags */
2061 list_for_each_entry(fail_res, &fail_head, list) {
2062 struct resource *res = fail_res->res;
2063 int idx;
2064
2065 res->start = fail_res->start;
2066 res->end = fail_res->end;
2067 res->flags = fail_res->flags;
2068
2069 if (pci_is_bridge(fail_res->dev)) {
2070 idx = res - &fail_res->dev->resource[0];
2071 if (idx >= PCI_BRIDGE_RESOURCES &&
2072 idx <= PCI_BRIDGE_RESOURCE_END)
2073 res->flags = 0;
2074 }
2075 }
2076 free_list(&fail_head);
2077
2078 goto again;
2079
2080enable_all:
2081 retval = pci_reenable_device(bridge);
2082 if (retval)
2083 pci_err(bridge, "Error reenabling bridge (%d)\n", retval);
2084 pci_set_master(bridge);
2085}
2086EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
2087
2088int pci_reassign_bridge_resources(struct pci_dev *bridge, unsigned long type)
2089{
2090 struct pci_dev_resource *dev_res;
2091 struct pci_dev *next;
2092 LIST_HEAD(saved);
2093 LIST_HEAD(added);
2094 LIST_HEAD(failed);
2095 unsigned int i;
2096 int ret;
2097
2098 /* Walk to the root hub, releasing bridge BARs when possible */
2099 next = bridge;
2100 do {
2101 bridge = next;
2102 for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCE_END;
2103 i++) {
2104 struct resource *res = &bridge->resource[i];
2105
2106 if ((res->flags ^ type) & PCI_RES_TYPE_MASK)
2107 continue;
2108
2109 /* Ignore BARs which are still in use */
2110 if (res->child)
2111 continue;
2112
2113 ret = add_to_list(&saved, bridge, res, 0, 0);
2114 if (ret)
2115 goto cleanup;
2116
2117 pci_info(bridge, "BAR %d: releasing %pR\n",
2118 i, res);
2119
2120 if (res->parent)
2121 release_resource(res);
2122 res->start = 0;
2123 res->end = 0;
2124 break;
2125 }
2126 if (i == PCI_BRIDGE_RESOURCE_END)
2127 break;
2128
2129 next = bridge->bus ? bridge->bus->self : NULL;
2130 } while (next);
2131
2132 if (list_empty(&saved))
2133 return -ENOENT;
2134
2135 __pci_bus_size_bridges(bridge->subordinate, &added);
2136 __pci_bridge_assign_resources(bridge, &added, &failed);
2137 BUG_ON(!list_empty(&added));
2138
2139 if (!list_empty(&failed)) {
2140 ret = -ENOSPC;
2141 goto cleanup;
2142 }
2143
2144 list_for_each_entry(dev_res, &saved, list) {
2145 /* Skip the bridge we just assigned resources for */
2146 if (bridge == dev_res->dev)
2147 continue;
2148
2149 bridge = dev_res->dev;
2150 pci_setup_bridge(bridge->subordinate);
2151 }
2152
2153 free_list(&saved);
2154 return 0;
2155
2156cleanup:
2157 /* Restore size and flags */
2158 list_for_each_entry(dev_res, &failed, list) {
2159 struct resource *res = dev_res->res;
2160
2161 res->start = dev_res->start;
2162 res->end = dev_res->end;
2163 res->flags = dev_res->flags;
2164 }
2165 free_list(&failed);
2166
2167 /* Revert to the old configuration */
2168 list_for_each_entry(dev_res, &saved, list) {
2169 struct resource *res = dev_res->res;
2170
2171 bridge = dev_res->dev;
2172 i = res - bridge->resource;
2173
2174 res->start = dev_res->start;
2175 res->end = dev_res->end;
2176 res->flags = dev_res->flags;
2177
2178 pci_claim_resource(bridge, i);
2179 pci_setup_bridge(bridge->subordinate);
2180 }
2181 free_list(&saved);
2182
2183 return ret;
2184}
2185
2186void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
2187{
2188 struct pci_dev *dev;
2189 /* List of resources that want additional resources */
2190 LIST_HEAD(add_list);
2191
2192 down_read(&pci_bus_sem);
2193 for_each_pci_bridge(dev, bus)
2194 if (pci_has_subordinate(dev))
2195 __pci_bus_size_bridges(dev->subordinate, &add_list);
2196 up_read(&pci_bus_sem);
2197 __pci_bus_assign_resources(bus, &add_list, NULL);
2198 BUG_ON(!list_empty(&add_list));
2199}
2200EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);