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