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