hc
2024-05-10 9999e48639b3cecb08ffb37358bcba3b48161b29
kernel/drivers/pci/setup-bus.c
....@@ -26,6 +26,7 @@
2626 #include "pci.h"
2727
2828 unsigned int pci_flags;
29
+EXPORT_SYMBOL_GPL(pci_flags);
2930
3031 struct pci_dev_resource {
3132 struct list_head list;
....@@ -49,17 +50,16 @@
4950 }
5051
5152 /**
52
- * add_to_list() - add a new resource tracker to the list
53
+ * add_to_list() - Add a new resource tracker to the list
5354 * @head: Head of the list
54
- * @dev: device corresponding to which the resource
55
- * belongs
56
- * @res: The resource to be tracked
57
- * @add_size: additional size to be optionally added
58
- * to the resource
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
5959 */
60
-static int add_to_list(struct list_head *head,
61
- struct pci_dev *dev, struct resource *res,
62
- 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)
6363 {
6464 struct pci_dev_resource *tmp;
6565
....@@ -80,8 +80,7 @@
8080 return 0;
8181 }
8282
83
-static void remove_from_list(struct list_head *head,
84
- struct resource *res)
83
+static void remove_from_list(struct list_head *head, struct resource *res)
8584 {
8685 struct pci_dev_resource *dev_res, *tmp;
8786
....@@ -154,11 +153,11 @@
154153
155154 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
156155 if (!tmp)
157
- panic("pdev_sort_resources(): kmalloc() failed!\n");
156
+ panic("%s: kzalloc() failed!\n", __func__);
158157 tmp->res = r;
159158 tmp->dev = dev;
160159
161
- /* fallback is smallest one or list is empty*/
160
+ /* Fallback is smallest one or list is empty */
162161 n = head;
163162 list_for_each_entry(dev_res, head, list) {
164163 resource_size_t align;
....@@ -171,21 +170,20 @@
171170 break;
172171 }
173172 }
174
- /* Insert it just before n*/
173
+ /* Insert it just before n */
175174 list_add_tail(&tmp->list, n);
176175 }
177176 }
178177
179
-static void __dev_sort_resources(struct pci_dev *dev,
180
- struct list_head *head)
178
+static void __dev_sort_resources(struct pci_dev *dev, struct list_head *head)
181179 {
182180 u16 class = dev->class >> 8;
183181
184
- /* Don't touch classless devices or host bridges or ioapics. */
182
+ /* Don't touch classless devices or host bridges or IOAPICs */
185183 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
186184 return;
187185
188
- /* Don't touch ioapic devices already enabled by firmware */
186
+ /* Don't touch IOAPIC devices already enabled by firmware */
189187 if (class == PCI_CLASS_SYSTEM_PIC) {
190188 u16 command;
191189 pci_read_config_word(dev, PCI_COMMAND, &command);
....@@ -204,19 +202,18 @@
204202 }
205203
206204 /**
207
- * reassign_resources_sorted() - satisfy any additional resource requests
205
+ * reassign_resources_sorted() - Satisfy any additional resource requests
208206 *
209
- * @realloc_head : head of the list tracking requests requiring additional
210
- * resources
211
- * @head : head of the list tracking requests with allocated
212
- * resources
207
+ * @realloc_head: Head of the list tracking requests requiring
208
+ * additional resources
209
+ * @head: Head of the list tracking requests with allocated
210
+ * resources
213211 *
214
- * Walk through each element of the realloc_head and try to procure
215
- * additional resources for the element, provided the element
216
- * is in the head list.
212
+ * Walk through each element of the realloc_head and try to procure additional
213
+ * resources for the element, provided the element is in the head list.
217214 */
218215 static void reassign_resources_sorted(struct list_head *realloc_head,
219
- struct list_head *head)
216
+ struct list_head *head)
220217 {
221218 struct resource *res;
222219 struct pci_dev_resource *add_res, *tmp;
....@@ -228,18 +225,18 @@
228225 bool found_match = false;
229226
230227 res = add_res->res;
231
- /* skip resource that has been reset */
228
+ /* Skip resource that has been reset */
232229 if (!res->flags)
233230 goto out;
234231
235
- /* skip this resource if not found in head list */
232
+ /* Skip this resource if not found in head list */
236233 list_for_each_entry(dev_res, head, list) {
237234 if (dev_res->res == res) {
238235 found_match = true;
239236 break;
240237 }
241238 }
242
- if (!found_match)/* just skip */
239
+ if (!found_match) /* Just skip */
243240 continue;
244241
245242 idx = res - &add_res->dev->resource[0];
....@@ -255,10 +252,9 @@
255252 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
256253 if (pci_reassign_resource(add_res->dev, idx,
257254 add_size, align))
258
- pci_printk(KERN_DEBUG, add_res->dev,
259
- "failed to add %llx res[%d]=%pR\n",
260
- (unsigned long long)add_size,
261
- idx, res);
255
+ pci_info(add_res->dev, "failed to add %llx res[%d]=%pR\n",
256
+ (unsigned long long) add_size, idx,
257
+ res);
262258 }
263259 out:
264260 list_del(&add_res->list);
....@@ -267,14 +263,14 @@
267263 }
268264
269265 /**
270
- * assign_requested_resources_sorted() - satisfy resource requests
266
+ * assign_requested_resources_sorted() - Satisfy resource requests
271267 *
272
- * @head : head of the list tracking requests for resources
273
- * @fail_head : head of the list tracking requests that could
274
- * not be allocated
268
+ * @head: Head of the list tracking requests for resources
269
+ * @fail_head: Head of the list tracking requests that could not be
270
+ * allocated
275271 *
276
- * Satisfy resource requests of each element in the list. Add
277
- * requests that could not satisfied to the failed_list.
272
+ * Satisfy resource requests of each element in the list. Add requests that
273
+ * could not be satisfied to the failed_list.
278274 */
279275 static void assign_requested_resources_sorted(struct list_head *head,
280276 struct list_head *fail_head)
....@@ -290,8 +286,9 @@
290286 pci_assign_resource(dev_res->dev, idx)) {
291287 if (fail_head) {
292288 /*
293
- * if the failed res is for ROM BAR, and it will
294
- * be enabled later, don't add it to the list
289
+ * If the failed resource is a ROM BAR and
290
+ * it will be enabled later, don't add it
291
+ * to the list.
295292 */
296293 if (!((idx == PCI_ROM_RESOURCE) &&
297294 (!(res->flags & IORESOURCE_ROM_ENABLE))))
....@@ -310,15 +307,14 @@
310307 struct pci_dev_resource *fail_res;
311308 unsigned long mask = 0;
312309
313
- /* check failed type */
310
+ /* Check failed type */
314311 list_for_each_entry(fail_res, fail_head, list)
315312 mask |= fail_res->flags;
316313
317314 /*
318
- * one pref failed resource will set IORESOURCE_MEM,
319
- * as we can allocate pref in non-pref range.
320
- * Will release all assigned non-pref sibling resources
321
- * according to that bit.
315
+ * One pref failed resource will set IORESOURCE_MEM, as we can
316
+ * allocate pref in non-pref range. Will release all assigned
317
+ * non-pref sibling resources according to that bit.
322318 */
323319 return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
324320 }
....@@ -328,11 +324,11 @@
328324 if (res->flags & IORESOURCE_IO)
329325 return !!(mask & IORESOURCE_IO);
330326
331
- /* check pref at first */
327
+ /* Check pref at first */
332328 if (res->flags & IORESOURCE_PREFETCH) {
333329 if (mask & IORESOURCE_PREFETCH)
334330 return true;
335
- /* count pref if its parent is non-pref */
331
+ /* Count pref if its parent is non-pref */
336332 else if ((mask & IORESOURCE_MEM) &&
337333 !(res->parent->flags & IORESOURCE_PREFETCH))
338334 return true;
....@@ -343,33 +339,33 @@
343339 if (res->flags & IORESOURCE_MEM)
344340 return !!(mask & IORESOURCE_MEM);
345341
346
- return false; /* should not get here */
342
+ return false; /* Should not get here */
347343 }
348344
349345 static void __assign_resources_sorted(struct list_head *head,
350
- struct list_head *realloc_head,
351
- struct list_head *fail_head)
346
+ struct list_head *realloc_head,
347
+ struct list_head *fail_head)
352348 {
353349 /*
354
- * Should not assign requested resources at first.
355
- * they could be adjacent, so later reassign can not reallocate
356
- * them one by one in parent resource window.
357
- * Try to assign requested + add_size at beginning
358
- * if could do that, could get out early.
359
- * if could not do that, we still try to assign requested at first,
360
- * then try to reassign add_size for some resources.
350
+ * Should not assign requested resources at first. They could be
351
+ * adjacent, so later reassign can not reallocate them one by one in
352
+ * parent resource window.
353
+ *
354
+ * Try to assign requested + add_size at beginning. If could do that,
355
+ * could get out early. If could not do that, we still try to assign
356
+ * requested at first, then try to reassign add_size for some resources.
361357 *
362358 * Separate three resource type checking if we need to release
363359 * assigned resource after requested + add_size try.
364
- * 1. if there is io port assign fail, will release assigned
365
- * io port.
366
- * 2. if there is pref mmio assign fail, release assigned
367
- * pref mmio.
368
- * if assigned pref mmio's parent is non-pref mmio and there
369
- * is non-pref mmio assign fail, will release that assigned
370
- * pref mmio.
371
- * 3. if there is non-pref mmio assign fail or pref mmio
372
- * assigned fail, will release assigned non-pref mmio.
360
+ *
361
+ * 1. If IO port assignment fails, will release assigned IO
362
+ * port.
363
+ * 2. If pref MMIO assignment fails, release assigned pref
364
+ * MMIO. If assigned pref MMIO's parent is non-pref MMIO
365
+ * and non-pref MMIO assignment fails, will release that
366
+ * assigned pref MMIO.
367
+ * 3. If non-pref MMIO assignment fails or pref MMIO
368
+ * assignment fails, will release assigned non-pref MMIO.
373369 */
374370 LIST_HEAD(save_head);
375371 LIST_HEAD(local_fail_head);
....@@ -398,7 +394,7 @@
398394 /*
399395 * There are two kinds of additional resources in the list:
400396 * 1. bridge resource -- IORESOURCE_STARTALIGN
401
- * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN
397
+ * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN
402398 * Here just fix the additional alignment for bridge
403399 */
404400 if (!(dev_res->res->flags & IORESOURCE_STARTALIGN))
....@@ -407,10 +403,10 @@
407403 add_align = get_res_add_align(realloc_head, dev_res->res);
408404
409405 /*
410
- * The "head" list is sorted by the alignment to make sure
411
- * resources with bigger alignment will be assigned first.
412
- * After we change the alignment of a dev_res in "head" list,
413
- * we need to reorder the list by alignment to make it
406
+ * The "head" list is sorted by alignment so resources with
407
+ * bigger alignment will be assigned first. After we
408
+ * change the alignment of a dev_res in "head" list, we
409
+ * need to reorder the list by alignment to make it
414410 * consistent.
415411 */
416412 if (add_align > dev_res->res->start) {
....@@ -435,7 +431,7 @@
435431 /* Try updated head list with add_size added */
436432 assign_requested_resources_sorted(head, &local_fail_head);
437433
438
- /* all assigned with add_size ? */
434
+ /* All assigned with add_size? */
439435 if (list_empty(&local_fail_head)) {
440436 /* Remove head list from realloc_head list */
441437 list_for_each_entry(dev_res, head, list)
....@@ -445,13 +441,13 @@
445441 return;
446442 }
447443
448
- /* check failed type */
444
+ /* Check failed type */
449445 fail_type = pci_fail_res_type_mask(&local_fail_head);
450
- /* remove not need to be released assigned res from head list etc */
446
+ /* Remove not need to be released assigned res from head list etc */
451447 list_for_each_entry_safe(dev_res, tmp_res, head, list)
452448 if (dev_res->res->parent &&
453449 !pci_need_to_release(fail_type, dev_res->res)) {
454
- /* remove it from realloc_head list */
450
+ /* Remove it from realloc_head list */
455451 remove_from_list(realloc_head, dev_res->res);
456452 remove_from_list(&save_head, dev_res->res);
457453 list_del(&dev_res->list);
....@@ -477,16 +473,15 @@
477473 /* Satisfy the must-have resource requests */
478474 assign_requested_resources_sorted(head, fail_head);
479475
480
- /* Try to satisfy any additional optional resource
481
- requests */
476
+ /* Try to satisfy any additional optional resource requests */
482477 if (realloc_head)
483478 reassign_resources_sorted(realloc_head, head);
484479 free_list(head);
485480 }
486481
487482 static void pdev_assign_resources_sorted(struct pci_dev *dev,
488
- struct list_head *add_head,
489
- struct list_head *fail_head)
483
+ struct list_head *add_head,
484
+ struct list_head *fail_head)
490485 {
491486 LIST_HEAD(head);
492487
....@@ -563,17 +558,19 @@
563558 }
564559 EXPORT_SYMBOL(pci_setup_cardbus);
565560
566
-/* Initialize bridges with base/limit values we have collected.
567
- PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
568
- requires that if there is no I/O ports or memory behind the
569
- bridge, corresponding range must be turned off by writing base
570
- value greater than limit to the bridge's base/limit registers.
571
-
572
- Note: care must be taken when updating I/O base/limit registers
573
- of bridges which support 32-bit I/O. This update requires two
574
- config space writes, so it's quite possible that an I/O window of
575
- the bridge will have some undesirable address (e.g. 0) after the
576
- first write. Ditto 64-bit prefetchable MMIO. */
561
+/*
562
+ * Initialize bridges with base/limit values we have collected. PCI-to-PCI
563
+ * Bridge Architecture Specification rev. 1.1 (1998) requires that if there
564
+ * are no I/O ports or memory behind the bridge, the corresponding range
565
+ * must be turned off by writing base value greater than limit to the
566
+ * bridge's base/limit registers.
567
+ *
568
+ * Note: care must be taken when updating I/O base/limit registers of
569
+ * bridges which support 32-bit I/O. This update requires two config space
570
+ * writes, so it's quite possible that an I/O window of the bridge will
571
+ * have some undesirable address (e.g. 0) after the first write. Ditto
572
+ * 64-bit prefetchable MMIO.
573
+ */
577574 static void pci_setup_bridge_io(struct pci_dev *bridge)
578575 {
579576 struct resource *res;
....@@ -587,27 +584,27 @@
587584 if (bridge->io_window_1k)
588585 io_mask = PCI_IO_1K_RANGE_MASK;
589586
590
- /* Set up the top and bottom of the PCI I/O segment for this bus. */
591
- res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
587
+ /* Set up the top and bottom of the PCI I/O segment for this bus */
588
+ res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
592589 pcibios_resource_to_bus(bridge->bus, &region, res);
593590 if (res->flags & IORESOURCE_IO) {
594591 pci_read_config_word(bridge, PCI_IO_BASE, &l);
595592 io_base_lo = (region.start >> 8) & io_mask;
596593 io_limit_lo = (region.end >> 8) & io_mask;
597594 l = ((u16) io_limit_lo << 8) | io_base_lo;
598
- /* Set up upper 16 bits of I/O base/limit. */
595
+ /* Set up upper 16 bits of I/O base/limit */
599596 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
600597 pci_info(bridge, " bridge window %pR\n", res);
601598 } else {
602
- /* Clear upper 16 bits of I/O base/limit. */
599
+ /* Clear upper 16 bits of I/O base/limit */
603600 io_upper16 = 0;
604601 l = 0x00f0;
605602 }
606
- /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
603
+ /* Temporarily disable the I/O range before updating PCI_IO_BASE */
607604 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
608
- /* Update lower 16 bits of I/O base/limit. */
605
+ /* Update lower 16 bits of I/O base/limit */
609606 pci_write_config_word(bridge, PCI_IO_BASE, l);
610
- /* Update upper 16 bits of I/O base/limit. */
607
+ /* Update upper 16 bits of I/O base/limit */
611608 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
612609 }
613610
....@@ -617,8 +614,8 @@
617614 struct pci_bus_region region;
618615 u32 l;
619616
620
- /* Set up the top and bottom of the PCI Memory segment for this bus. */
621
- res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
617
+ /* Set up the top and bottom of the PCI Memory segment for this bus */
618
+ res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
622619 pcibios_resource_to_bus(bridge->bus, &region, res);
623620 if (res->flags & IORESOURCE_MEM) {
624621 l = (region.start >> 16) & 0xfff0;
....@@ -636,14 +633,16 @@
636633 struct pci_bus_region region;
637634 u32 l, bu, lu;
638635
639
- /* Clear out the upper 32 bits of PREF limit.
640
- If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
641
- disables PREF range, which is ok. */
636
+ /*
637
+ * Clear out the upper 32 bits of PREF limit. If
638
+ * PCI_PREF_BASE_UPPER32 was non-zero, this temporarily disables
639
+ * PREF range, which is ok.
640
+ */
642641 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
643642
644
- /* Set up PREF base/limit. */
643
+ /* Set up PREF base/limit */
645644 bu = lu = 0;
646
- res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
645
+ res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
647646 pcibios_resource_to_bus(bridge->bus, &region, res);
648647 if (res->flags & IORESOURCE_PREFETCH) {
649648 l = (region.start >> 16) & 0xfff0;
....@@ -658,7 +657,7 @@
658657 }
659658 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
660659
661
- /* Set the upper 32 bits of PREF base & limit. */
660
+ /* Set the upper 32 bits of PREF base & limit */
662661 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
663662 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
664663 }
....@@ -702,22 +701,22 @@
702701 return 0;
703702
704703 if (pci_claim_resource(bridge, i) == 0)
705
- return 0; /* claimed the window */
704
+ return 0; /* Claimed the window */
706705
707706 if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
708707 return 0;
709708
710709 if (!pci_bus_clip_resource(bridge, i))
711
- return -EINVAL; /* clipping didn't change anything */
710
+ return -EINVAL; /* Clipping didn't change anything */
712711
713
- switch (i - PCI_BRIDGE_RESOURCES) {
714
- case 0:
712
+ switch (i) {
713
+ case PCI_BRIDGE_IO_WINDOW:
715714 pci_setup_bridge_io(bridge);
716715 break;
717
- case 1:
716
+ case PCI_BRIDGE_MEM_WINDOW:
718717 pci_setup_bridge_mmio(bridge);
719718 break;
720
- case 2:
719
+ case PCI_BRIDGE_PREF_MEM_WINDOW:
721720 pci_setup_bridge_mmio_pref(bridge);
722721 break;
723722 default:
....@@ -725,115 +724,101 @@
725724 }
726725
727726 if (pci_claim_resource(bridge, i) == 0)
728
- return 0; /* claimed a smaller window */
727
+ return 0; /* Claimed a smaller window */
729728
730729 return -EINVAL;
731730 }
732731
733
-/* Check whether the bridge supports optional I/O and
734
- prefetchable memory ranges. If not, the respective
735
- base/limit registers must be read-only and read as 0. */
732
+/*
733
+ * Check whether the bridge supports optional I/O and prefetchable memory
734
+ * ranges. If not, the respective base/limit registers must be read-only
735
+ * and read as 0.
736
+ */
736737 static void pci_bridge_check_ranges(struct pci_bus *bus)
737738 {
738
- u16 io;
739
- u32 pmem;
740739 struct pci_dev *bridge = bus->self;
741740 struct resource *b_res;
742741
743
- b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
744
- b_res[1].flags |= IORESOURCE_MEM;
742
+ b_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
743
+ b_res->flags |= IORESOURCE_MEM;
745744
746
- pci_read_config_word(bridge, PCI_IO_BASE, &io);
747
- if (!io) {
748
- pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0);
749
- pci_read_config_word(bridge, PCI_IO_BASE, &io);
750
- pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
745
+ if (bridge->io_window) {
746
+ b_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
747
+ b_res->flags |= IORESOURCE_IO;
751748 }
752
- if (io)
753
- b_res[0].flags |= IORESOURCE_IO;
754749
755
- /* DECchip 21050 pass 2 errata: the bridge may miss an address
756
- disconnect boundary by one PCI data phase.
757
- Workaround: do not use prefetching on this device. */
758
- if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
759
- return;
760
-
761
- pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
762
- if (!pmem) {
763
- pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
764
- 0xffe0fff0);
765
- pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
766
- pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
767
- }
768
- if (pmem) {
769
- b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
770
- if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
771
- PCI_PREF_RANGE_TYPE_64) {
772
- b_res[2].flags |= IORESOURCE_MEM_64;
773
- b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
750
+ if (bridge->pref_window) {
751
+ b_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
752
+ b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
753
+ if (bridge->pref_64_window) {
754
+ b_res->flags |= IORESOURCE_MEM_64 |
755
+ PCI_PREF_RANGE_TYPE_64;
774756 }
775
- }
776
-
777
- /* double check if bridge does support 64 bit pref */
778
- if (b_res[2].flags & IORESOURCE_MEM_64) {
779
- u32 mem_base_hi, tmp;
780
- pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
781
- &mem_base_hi);
782
- pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
783
- 0xffffffff);
784
- pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
785
- if (!tmp)
786
- b_res[2].flags &= ~IORESOURCE_MEM_64;
787
- pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
788
- mem_base_hi);
789757 }
790758 }
791759
792
-/* Helper function for sizing routines: find first available
793
- bus resource of a given type. Note: we intentionally skip
794
- the bus resources which have already been assigned (that is,
795
- have non-NULL parent resource). */
796
-static struct resource *find_free_bus_resource(struct pci_bus *bus,
797
- unsigned long type_mask, unsigned long type)
760
+/*
761
+ * Helper function for sizing routines. Assigned resources have non-NULL
762
+ * parent resource.
763
+ *
764
+ * Return first unassigned resource of the correct type. If there is none,
765
+ * return first assigned resource of the correct type. If none of the
766
+ * above, return NULL.
767
+ *
768
+ * Returning an assigned resource of the correct type allows the caller to
769
+ * distinguish between already assigned and no resource of the correct type.
770
+ */
771
+static struct resource *find_bus_resource_of_type(struct pci_bus *bus,
772
+ unsigned long type_mask,
773
+ unsigned long type)
798774 {
775
+ struct resource *r, *r_assigned = NULL;
799776 int i;
800
- struct resource *r;
801777
802778 pci_bus_for_each_resource(bus, r, i) {
803779 if (r == &ioport_resource || r == &iomem_resource)
804780 continue;
805781 if (r && (r->flags & type_mask) == type && !r->parent)
806782 return r;
783
+ if (r && (r->flags & type_mask) == type && !r_assigned)
784
+ r_assigned = r;
807785 }
808
- return NULL;
786
+ return r_assigned;
809787 }
810788
811789 static resource_size_t calculate_iosize(resource_size_t size,
812
- resource_size_t min_size,
813
- resource_size_t size1,
814
- resource_size_t old_size,
815
- resource_size_t align)
790
+ resource_size_t min_size,
791
+ resource_size_t size1,
792
+ resource_size_t add_size,
793
+ resource_size_t children_add_size,
794
+ resource_size_t old_size,
795
+ resource_size_t align)
816796 {
817797 if (size < min_size)
818798 size = min_size;
819799 if (old_size == 1)
820800 old_size = 0;
821
- /* To be fixed in 2.5: we should have sort of HAVE_ISA
822
- flag in the struct pci_bus. */
801
+ /*
802
+ * To be fixed in 2.5: we should have sort of HAVE_ISA flag in the
803
+ * struct pci_bus.
804
+ */
823805 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
824806 size = (size & 0xff) + ((size & ~0xffUL) << 2);
825807 #endif
826
- size = ALIGN(size + size1, align);
808
+ size = size + size1;
827809 if (size < old_size)
828810 size = old_size;
811
+
812
+ size = ALIGN(max(size, add_size) + children_add_size, align);
829813 return size;
830814 }
831815
832816 static resource_size_t calculate_memsize(resource_size_t size,
833
- resource_size_t min_size,
834
- resource_size_t size1,
835
- resource_size_t old_size,
836
- resource_size_t align)
817
+ resource_size_t min_size,
818
+ resource_size_t add_size,
819
+ resource_size_t children_add_size,
820
+ resource_size_t old_size,
821
+ resource_size_t align)
837822 {
838823 if (size < min_size)
839824 size = min_size;
....@@ -841,7 +826,8 @@
841826 old_size = 0;
842827 if (size < old_size)
843828 size = old_size;
844
- size = ALIGN(size + size1, align);
829
+
830
+ size = ALIGN(max(size, add_size) + children_add_size, align);
845831 return size;
846832 }
847833
....@@ -855,8 +841,7 @@
855841 #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */
856842 #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */
857843
858
-static resource_size_t window_alignment(struct pci_bus *bus,
859
- unsigned long type)
844
+static resource_size_t window_alignment(struct pci_bus *bus, unsigned long type)
860845 {
861846 resource_size_t align = 1, arch_align;
862847
....@@ -864,10 +849,10 @@
864849 align = PCI_P2P_DEFAULT_MEM_ALIGN;
865850 else if (type & IORESOURCE_IO) {
866851 /*
867
- * Per spec, I/O windows are 4K-aligned, but some
868
- * bridges have an extension to support 1K alignment.
852
+ * Per spec, I/O windows are 4K-aligned, but some bridges have
853
+ * an extension to support 1K alignment.
869854 */
870
- if (bus->self->io_window_1k)
855
+ if (bus->self && bus->self->io_window_1k)
871856 align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
872857 else
873858 align = PCI_P2P_DEFAULT_IO_ALIGN;
....@@ -878,29 +863,34 @@
878863 }
879864
880865 /**
881
- * pbus_size_io() - size the io window of a given bus
866
+ * pbus_size_io() - Size the I/O window of a given bus
882867 *
883
- * @bus : the bus
884
- * @min_size : the minimum io window that must to be allocated
885
- * @add_size : additional optional io window
886
- * @realloc_head : track the additional io window on this list
868
+ * @bus: The bus
869
+ * @min_size: The minimum I/O window that must be allocated
870
+ * @add_size: Additional optional I/O window
871
+ * @realloc_head: Track the additional I/O window on this list
887872 *
888
- * Sizing the IO windows of the PCI-PCI bridge is trivial,
889
- * since these windows have 1K or 4K granularity and the IO ranges
890
- * of non-bridge PCI devices are limited to 256 bytes.
891
- * We must be careful with the ISA aliasing though.
873
+ * Sizing the I/O windows of the PCI-PCI bridge is trivial, since these
874
+ * windows have 1K or 4K granularity and the I/O ranges of non-bridge PCI
875
+ * devices are limited to 256 bytes. We must be careful with the ISA
876
+ * aliasing though.
892877 */
893878 static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
894
- resource_size_t add_size, struct list_head *realloc_head)
879
+ resource_size_t add_size,
880
+ struct list_head *realloc_head)
895881 {
896882 struct pci_dev *dev;
897
- struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO,
898
- IORESOURCE_IO);
883
+ struct resource *b_res = find_bus_resource_of_type(bus, IORESOURCE_IO,
884
+ IORESOURCE_IO);
899885 resource_size_t size = 0, size0 = 0, size1 = 0;
900886 resource_size_t children_add_size = 0;
901887 resource_size_t min_align, align;
902888
903889 if (!b_res)
890
+ return;
891
+
892
+ /* If resource is already assigned, nothing more to do */
893
+ if (b_res->parent)
904894 return;
905895
906896 min_align = window_alignment(bus, IORESOURCE_IO);
....@@ -930,15 +920,13 @@
930920 }
931921 }
932922
933
- size0 = calculate_iosize(size, min_size, size1,
923
+ size0 = calculate_iosize(size, min_size, size1, 0, 0,
934924 resource_size(b_res), min_align);
935
- if (children_add_size > add_size)
936
- add_size = children_add_size;
937
- size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
938
- calculate_iosize(size, min_size, add_size + size1,
925
+ size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
926
+ calculate_iosize(size, min_size, size1, add_size, children_add_size,
939927 resource_size(b_res), min_align);
940928 if (!size0 && !size1) {
941
- if (b_res->start || b_res->end)
929
+ if (bus->self && (b_res->start || b_res->end))
942930 pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
943931 b_res, &bus->busn_res);
944932 b_res->flags = 0;
....@@ -948,12 +936,12 @@
948936 b_res->start = min_align;
949937 b_res->end = b_res->start + size0 - 1;
950938 b_res->flags |= IORESOURCE_STARTALIGN;
951
- if (size1 > size0 && realloc_head) {
939
+ if (bus->self && size1 > size0 && realloc_head) {
952940 add_to_list(realloc_head, bus->self, b_res, size1-size0,
953941 min_align);
954
- pci_printk(KERN_DEBUG, bus->self, "bridge window %pR to %pR add_size %llx\n",
955
- b_res, &bus->busn_res,
956
- (unsigned long long)size1-size0);
942
+ pci_info(bus->self, "bridge window %pR to %pR add_size %llx\n",
943
+ b_res, &bus->busn_res,
944
+ (unsigned long long) size1 - size0);
957945 }
958946 }
959947
....@@ -980,35 +968,35 @@
980968 }
981969
982970 /**
983
- * pbus_size_mem() - size the memory window of a given bus
971
+ * pbus_size_mem() - Size the memory window of a given bus
984972 *
985
- * @bus : the bus
986
- * @mask: mask the resource flag, then compare it with type
987
- * @type: the type of free resource from bridge
988
- * @type2: second match type
989
- * @type3: third match type
990
- * @min_size : the minimum memory window that must to be allocated
991
- * @add_size : additional optional memory window
992
- * @realloc_head : track the additional memory window on this list
973
+ * @bus: The bus
974
+ * @mask: Mask the resource flag, then compare it with type
975
+ * @type: The type of free resource from bridge
976
+ * @type2: Second match type
977
+ * @type3: Third match type
978
+ * @min_size: The minimum memory window that must be allocated
979
+ * @add_size: Additional optional memory window
980
+ * @realloc_head: Track the additional memory window on this list
993981 *
994
- * Calculate the size of the bus and minimal alignment which
995
- * guarantees that all child resources fit in this size.
982
+ * Calculate the size of the bus and minimal alignment which guarantees
983
+ * that all child resources fit in this size.
996984 *
997
- * Returns -ENOSPC if there's no available bus resource of the desired type.
998
- * Otherwise, sets the bus resource start/end to indicate the required
999
- * size, adds things to realloc_head (if supplied), and returns 0.
985
+ * Return -ENOSPC if there's no available bus resource of the desired
986
+ * type. Otherwise, set the bus resource start/end to indicate the
987
+ * required size, add things to realloc_head (if supplied), and return 0.
1000988 */
1001989 static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
1002990 unsigned long type, unsigned long type2,
1003
- unsigned long type3,
1004
- resource_size_t min_size, resource_size_t add_size,
991
+ unsigned long type3, resource_size_t min_size,
992
+ resource_size_t add_size,
1005993 struct list_head *realloc_head)
1006994 {
1007995 struct pci_dev *dev;
1008996 resource_size_t min_align, align, size, size0, size1;
1009
- resource_size_t aligns[18]; /* Alignments from 1Mb to 128Gb */
997
+ resource_size_t aligns[18]; /* Alignments from 1MB to 128GB */
1010998 int order, max_order;
1011
- struct resource *b_res = find_free_bus_resource(bus,
999
+ struct resource *b_res = find_bus_resource_of_type(bus,
10121000 mask | IORESOURCE_PREFETCH, type);
10131001 resource_size_t children_add_size = 0;
10141002 resource_size_t children_add_align = 0;
....@@ -1016,6 +1004,10 @@
10161004
10171005 if (!b_res)
10181006 return -ENOSPC;
1007
+
1008
+ /* If resource is already assigned, nothing more to do */
1009
+ if (b_res->parent)
1010
+ return 0;
10191011
10201012 memset(aligns, 0, sizeof(aligns));
10211013 max_order = 0;
....@@ -1035,12 +1027,12 @@
10351027 continue;
10361028 r_size = resource_size(r);
10371029 #ifdef CONFIG_PCI_IOV
1038
- /* put SRIOV requested res to the optional list */
1030
+ /* Put SRIOV requested res to the optional list */
10391031 if (realloc_head && i >= PCI_IOV_RESOURCES &&
10401032 i <= PCI_IOV_RESOURCE_END) {
10411033 add_align = max(pci_resource_alignment(dev, r), add_align);
10421034 r->end = r->start - 1;
1043
- add_to_list(realloc_head, dev, r, r_size, 0/* don't care */);
1035
+ add_to_list(realloc_head, dev, r, r_size, 0 /* Don't care */);
10441036 children_add_size += r_size;
10451037 continue;
10461038 }
....@@ -1062,8 +1054,10 @@
10621054 continue;
10631055 }
10641056 size += max(r_size, align);
1065
- /* Exclude ranges with size > align from
1066
- calculation of the alignment. */
1057
+ /*
1058
+ * Exclude ranges with size > align from calculation of
1059
+ * the alignment.
1060
+ */
10671061 if (r_size <= align)
10681062 aligns[order] += align;
10691063 if (order > max_order)
....@@ -1079,15 +1073,13 @@
10791073
10801074 min_align = calculate_mem_align(aligns, max_order);
10811075 min_align = max(min_align, window_alignment(bus, b_res->flags));
1082
- size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
1076
+ size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), min_align);
10831077 add_align = max(min_align, add_align);
1084
- if (children_add_size > add_size)
1085
- add_size = children_add_size;
1086
- size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
1087
- calculate_memsize(size, min_size, add_size,
1078
+ size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
1079
+ calculate_memsize(size, min_size, add_size, children_add_size,
10881080 resource_size(b_res), add_align);
10891081 if (!size0 && !size1) {
1090
- if (b_res->start || b_res->end)
1082
+ if (bus->self && (b_res->start || b_res->end))
10911083 pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
10921084 b_res, &bus->busn_res);
10931085 b_res->flags = 0;
....@@ -1096,9 +1088,9 @@
10961088 b_res->start = min_align;
10971089 b_res->end = size0 + min_align - 1;
10981090 b_res->flags |= IORESOURCE_STARTALIGN;
1099
- if (size1 > size0 && realloc_head) {
1091
+ if (bus->self && size1 > size0 && realloc_head) {
11001092 add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align);
1101
- pci_printk(KERN_DEBUG, bus->self, "bridge window %pR to %pR add_size %llx add_align %llx\n",
1093
+ pci_info(bus->self, "bridge window %pR to %pR add_size %llx add_align %llx\n",
11021094 b_res, &bus->busn_res,
11031095 (unsigned long long) (size1 - size0),
11041096 (unsigned long long) add_align);
....@@ -1116,42 +1108,44 @@
11161108 }
11171109
11181110 static void pci_bus_size_cardbus(struct pci_bus *bus,
1119
- struct list_head *realloc_head)
1111
+ struct list_head *realloc_head)
11201112 {
11211113 struct pci_dev *bridge = bus->self;
1122
- struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
1114
+ struct resource *b_res;
11231115 resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
11241116 u16 ctrl;
11251117
1126
- if (b_res[0].parent)
1118
+ b_res = &bridge->resource[PCI_CB_BRIDGE_IO_0_WINDOW];
1119
+ if (b_res->parent)
11271120 goto handle_b_res_1;
11281121 /*
1129
- * Reserve some resources for CardBus. We reserve
1130
- * a fixed amount of bus space for CardBus bridges.
1122
+ * Reserve some resources for CardBus. We reserve a fixed amount
1123
+ * of bus space for CardBus bridges.
11311124 */
1132
- b_res[0].start = pci_cardbus_io_size;
1133
- b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
1134
- b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
1125
+ b_res->start = pci_cardbus_io_size;
1126
+ b_res->end = b_res->start + pci_cardbus_io_size - 1;
1127
+ b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
11351128 if (realloc_head) {
1136
- b_res[0].end -= pci_cardbus_io_size;
1129
+ b_res->end -= pci_cardbus_io_size;
11371130 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
1138
- pci_cardbus_io_size);
1131
+ pci_cardbus_io_size);
11391132 }
11401133
11411134 handle_b_res_1:
1142
- if (b_res[1].parent)
1135
+ b_res = &bridge->resource[PCI_CB_BRIDGE_IO_1_WINDOW];
1136
+ if (b_res->parent)
11431137 goto handle_b_res_2;
1144
- b_res[1].start = pci_cardbus_io_size;
1145
- b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
1146
- b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
1138
+ b_res->start = pci_cardbus_io_size;
1139
+ b_res->end = b_res->start + pci_cardbus_io_size - 1;
1140
+ b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
11471141 if (realloc_head) {
1148
- b_res[1].end -= pci_cardbus_io_size;
1149
- add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
1150
- pci_cardbus_io_size);
1142
+ b_res->end -= pci_cardbus_io_size;
1143
+ add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
1144
+ pci_cardbus_io_size);
11511145 }
11521146
11531147 handle_b_res_2:
1154
- /* MEM1 must not be pref mmio */
1148
+ /* MEM1 must not be pref MMIO */
11551149 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
11561150 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
11571151 ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
....@@ -1159,10 +1153,7 @@
11591153 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
11601154 }
11611155
1162
- /*
1163
- * Check whether prefetchable memory is supported
1164
- * by this bridge.
1165
- */
1156
+ /* Check whether prefetchable memory is supported by this bridge. */
11661157 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
11671158 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
11681159 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
....@@ -1170,38 +1161,39 @@
11701161 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
11711162 }
11721163
1173
- if (b_res[2].parent)
1164
+ b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_0_WINDOW];
1165
+ if (b_res->parent)
11741166 goto handle_b_res_3;
11751167 /*
1176
- * If we have prefetchable memory support, allocate
1177
- * two regions. Otherwise, allocate one region of
1178
- * twice the size.
1168
+ * If we have prefetchable memory support, allocate two regions.
1169
+ * Otherwise, allocate one region of twice the size.
11791170 */
11801171 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
1181
- b_res[2].start = pci_cardbus_mem_size;
1182
- b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
1183
- b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
1184
- IORESOURCE_STARTALIGN;
1172
+ b_res->start = pci_cardbus_mem_size;
1173
+ b_res->end = b_res->start + pci_cardbus_mem_size - 1;
1174
+ b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
1175
+ IORESOURCE_STARTALIGN;
11851176 if (realloc_head) {
1186
- b_res[2].end -= pci_cardbus_mem_size;
1187
- add_to_list(realloc_head, bridge, b_res+2,
1188
- pci_cardbus_mem_size, pci_cardbus_mem_size);
1177
+ b_res->end -= pci_cardbus_mem_size;
1178
+ add_to_list(realloc_head, bridge, b_res,
1179
+ pci_cardbus_mem_size, pci_cardbus_mem_size);
11891180 }
11901181
1191
- /* reduce that to half */
1182
+ /* Reduce that to half */
11921183 b_res_3_size = pci_cardbus_mem_size;
11931184 }
11941185
11951186 handle_b_res_3:
1196
- if (b_res[3].parent)
1187
+ b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_1_WINDOW];
1188
+ if (b_res->parent)
11971189 goto handle_done;
1198
- b_res[3].start = pci_cardbus_mem_size;
1199
- b_res[3].end = b_res[3].start + b_res_3_size - 1;
1200
- b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
1190
+ b_res->start = pci_cardbus_mem_size;
1191
+ b_res->end = b_res->start + b_res_3_size - 1;
1192
+ b_res->flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
12011193 if (realloc_head) {
1202
- b_res[3].end -= b_res_3_size;
1203
- add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
1204
- pci_cardbus_mem_size);
1194
+ b_res->end -= b_res_3_size;
1195
+ add_to_list(realloc_head, bridge, b_res, b_res_3_size,
1196
+ pci_cardbus_mem_size);
12051197 }
12061198
12071199 handle_done:
....@@ -1212,21 +1204,23 @@
12121204 {
12131205 struct pci_dev *dev;
12141206 unsigned long mask, prefmask, type2 = 0, type3 = 0;
1215
- resource_size_t additional_mem_size = 0, additional_io_size = 0;
1216
- struct resource *b_res;
1217
- int ret;
1207
+ resource_size_t additional_io_size = 0, additional_mmio_size = 0,
1208
+ additional_mmio_pref_size = 0;
1209
+ struct resource *pref;
1210
+ struct pci_host_bridge *host;
1211
+ int hdr_type, i, ret;
12181212
12191213 list_for_each_entry(dev, &bus->devices, bus_list) {
12201214 struct pci_bus *b = dev->subordinate;
12211215 if (!b)
12221216 continue;
12231217
1224
- switch (dev->class >> 8) {
1225
- case PCI_CLASS_BRIDGE_CARDBUS:
1218
+ switch (dev->hdr_type) {
1219
+ case PCI_HEADER_TYPE_CARDBUS:
12261220 pci_bus_size_cardbus(b, realloc_head);
12271221 break;
12281222
1229
- case PCI_CLASS_BRIDGE_PCI:
1223
+ case PCI_HEADER_TYPE_BRIDGE:
12301224 default:
12311225 __pci_bus_size_bridges(b, realloc_head);
12321226 break;
....@@ -1234,21 +1228,32 @@
12341228 }
12351229
12361230 /* The root bus? */
1237
- if (pci_is_root_bus(bus))
1238
- return;
1231
+ if (pci_is_root_bus(bus)) {
1232
+ host = to_pci_host_bridge(bus->bridge);
1233
+ if (!host->size_windows)
1234
+ return;
1235
+ pci_bus_for_each_resource(bus, pref, i)
1236
+ if (pref && (pref->flags & IORESOURCE_PREFETCH))
1237
+ break;
1238
+ hdr_type = -1; /* Intentionally invalid - not a PCI device. */
1239
+ } else {
1240
+ pref = &bus->self->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
1241
+ hdr_type = bus->self->hdr_type;
1242
+ }
12391243
1240
- switch (bus->self->class >> 8) {
1241
- case PCI_CLASS_BRIDGE_CARDBUS:
1242
- /* don't size cardbuses yet. */
1244
+ switch (hdr_type) {
1245
+ case PCI_HEADER_TYPE_CARDBUS:
1246
+ /* Don't size CardBuses yet */
12431247 break;
12441248
1245
- case PCI_CLASS_BRIDGE_PCI:
1249
+ case PCI_HEADER_TYPE_BRIDGE:
12461250 pci_bridge_check_ranges(bus);
12471251 if (bus->self->is_hotplug_bridge) {
12481252 additional_io_size = pci_hotplug_io_size;
1249
- additional_mem_size = pci_hotplug_mem_size;
1253
+ additional_mmio_size = pci_hotplug_mmio_size;
1254
+ additional_mmio_pref_size = pci_hotplug_mmio_pref_size;
12501255 }
1251
- /* Fall through */
1256
+ fallthrough;
12521257 default:
12531258 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
12541259 additional_io_size, realloc_head);
....@@ -1258,15 +1263,14 @@
12581263 * the size required to put all 64-bit prefetchable
12591264 * resources in it.
12601265 */
1261
- b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES];
12621266 mask = IORESOURCE_MEM;
12631267 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
1264
- if (b_res[2].flags & IORESOURCE_MEM_64) {
1268
+ if (pref && (pref->flags & IORESOURCE_MEM_64)) {
12651269 prefmask |= IORESOURCE_MEM_64;
12661270 ret = pbus_size_mem(bus, prefmask, prefmask,
1267
- prefmask, prefmask,
1268
- realloc_head ? 0 : additional_mem_size,
1269
- additional_mem_size, realloc_head);
1271
+ prefmask, prefmask,
1272
+ realloc_head ? 0 : additional_mmio_pref_size,
1273
+ additional_mmio_pref_size, realloc_head);
12701274
12711275 /*
12721276 * If successful, all non-prefetchable resources
....@@ -1288,9 +1292,9 @@
12881292 if (!type2) {
12891293 prefmask &= ~IORESOURCE_MEM_64;
12901294 ret = pbus_size_mem(bus, prefmask, prefmask,
1291
- prefmask, prefmask,
1292
- realloc_head ? 0 : additional_mem_size,
1293
- additional_mem_size, realloc_head);
1295
+ prefmask, prefmask,
1296
+ realloc_head ? 0 : additional_mmio_pref_size,
1297
+ additional_mmio_pref_size, realloc_head);
12941298
12951299 /*
12961300 * If successful, only non-prefetchable resources
....@@ -1299,29 +1303,28 @@
12991303 if (ret == 0)
13001304 mask = prefmask;
13011305 else
1302
- additional_mem_size += additional_mem_size;
1306
+ additional_mmio_size += additional_mmio_pref_size;
13031307
13041308 type2 = type3 = IORESOURCE_MEM;
13051309 }
13061310
13071311 /*
13081312 * Compute the size required to put everything else in the
1309
- * non-prefetchable window. This includes:
1313
+ * non-prefetchable window. This includes:
13101314 *
13111315 * - all non-prefetchable resources
13121316 * - 32-bit prefetchable resources if there's a 64-bit
13131317 * prefetchable window or no prefetchable window at all
1314
- * - 64-bit prefetchable resources if there's no
1315
- * prefetchable window at all
1318
+ * - 64-bit prefetchable resources if there's no prefetchable
1319
+ * window at all
13161320 *
1317
- * Note that the strategy in __pci_assign_resource() must
1318
- * match that used here. Specifically, we cannot put a
1319
- * 32-bit prefetchable resource in a 64-bit prefetchable
1320
- * window.
1321
+ * Note that the strategy in __pci_assign_resource() must match
1322
+ * that used here. Specifically, we cannot put a 32-bit
1323
+ * prefetchable resource in a 64-bit prefetchable window.
13211324 */
13221325 pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
1323
- realloc_head ? 0 : additional_mem_size,
1324
- additional_mem_size, realloc_head);
1326
+ realloc_head ? 0 : additional_mmio_size,
1327
+ additional_mmio_size, realloc_head);
13251328 break;
13261329 }
13271330 }
....@@ -1350,8 +1353,8 @@
13501353 }
13511354
13521355 /*
1353
- * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they
1354
- * are skipped by pbus_assign_resources_sorted().
1356
+ * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they are
1357
+ * skipped by pbus_assign_resources_sorted().
13551358 */
13561359 static void pdev_assign_fixed_resources(struct pci_dev *dev)
13571360 {
....@@ -1391,13 +1394,13 @@
13911394
13921395 __pci_bus_assign_resources(b, realloc_head, fail_head);
13931396
1394
- switch (dev->class >> 8) {
1395
- case PCI_CLASS_BRIDGE_PCI:
1397
+ switch (dev->hdr_type) {
1398
+ case PCI_HEADER_TYPE_BRIDGE:
13961399 if (!pci_is_enabled(dev))
13971400 pci_setup_bridge(b);
13981401 break;
13991402
1400
- case PCI_CLASS_BRIDGE_CARDBUS:
1403
+ case PCI_HEADER_TYPE_CARDBUS:
14011404 pci_setup_cardbus(b);
14021405 break;
14031406
....@@ -1462,10 +1465,9 @@
14621465 struct pci_bus *child;
14631466
14641467 /*
1465
- * Carry out a depth-first search on the PCI bus
1466
- * tree to allocate bridge apertures. Read the
1467
- * programmed bridge bases and recursively claim
1468
- * the respective bridge resources.
1468
+ * Carry out a depth-first search on the PCI bus tree to allocate
1469
+ * bridge apertures. Read the programmed bridge bases and
1470
+ * recursively claim the respective bridge resources.
14691471 */
14701472 if (b->self) {
14711473 pci_read_bridge_bases(b);
....@@ -1519,7 +1521,7 @@
15191521 IORESOURCE_MEM_64)
15201522
15211523 static void pci_bridge_release_resources(struct pci_bus *bus,
1522
- unsigned long type)
1524
+ unsigned long type)
15231525 {
15241526 struct pci_dev *dev = bus->self;
15251527 struct resource *r;
....@@ -1530,16 +1532,14 @@
15301532 b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
15311533
15321534 /*
1533
- * 1. if there is io port assign fail, will release bridge
1534
- * io port.
1535
- * 2. if there is non pref mmio assign fail, release bridge
1536
- * nonpref mmio.
1537
- * 3. if there is 64bit pref mmio assign fail, and bridge pref
1538
- * is 64bit, release bridge pref mmio.
1539
- * 4. if there is pref mmio assign fail, and bridge pref is
1540
- * 32bit mmio, release bridge pref mmio
1541
- * 5. if there is pref mmio assign fail, and bridge pref is not
1542
- * assigned, release bridge nonpref mmio.
1535
+ * 1. If IO port assignment fails, release bridge IO port.
1536
+ * 2. If non pref MMIO assignment fails, release bridge nonpref MMIO.
1537
+ * 3. If 64bit pref MMIO assignment fails, and bridge pref is 64bit,
1538
+ * release bridge pref MMIO.
1539
+ * 4. If pref MMIO assignment fails, and bridge pref is 32bit,
1540
+ * release bridge pref MMIO.
1541
+ * 5. If pref MMIO assignment fails, and bridge pref is not
1542
+ * assigned, release bridge nonpref MMIO.
15431543 */
15441544 if (type & IORESOURCE_IO)
15451545 idx = 0;
....@@ -1559,25 +1559,22 @@
15591559 if (!r->parent)
15601560 return;
15611561
1562
- /*
1563
- * if there are children under that, we should release them
1564
- * all
1565
- */
1562
+ /* If there are children, release them all */
15661563 release_child_resources(r);
15671564 if (!release_resource(r)) {
15681565 type = old_flags = r->flags & PCI_RES_TYPE_MASK;
1569
- pci_printk(KERN_DEBUG, dev, "resource %d %pR released\n",
1570
- PCI_BRIDGE_RESOURCES + idx, r);
1571
- /* keep the old size */
1566
+ pci_info(dev, "resource %d %pR released\n",
1567
+ PCI_BRIDGE_RESOURCES + idx, r);
1568
+ /* Keep the old size */
15721569 r->end = resource_size(r) - 1;
15731570 r->start = 0;
15741571 r->flags = 0;
15751572
1576
- /* avoiding touch the one without PREF */
1573
+ /* Avoiding touch the one without PREF */
15771574 if (type & IORESOURCE_PREFETCH)
15781575 type = IORESOURCE_PREFETCH;
15791576 __pci_setup_bridge(bus, type);
1580
- /* for next child res under same bridge */
1577
+ /* For next child res under same bridge */
15811578 r->flags = old_flags;
15821579 }
15831580 }
....@@ -1586,9 +1583,10 @@
15861583 leaf_only,
15871584 whole_subtree,
15881585 };
1586
+
15891587 /*
1590
- * try to release pci bridge resources that is from leaf bridge,
1591
- * so we can allocate big new one later
1588
+ * Try to release PCI bridge resources from leaf bridge, so we can allocate
1589
+ * a larger window later.
15921590 */
15931591 static void pci_bus_release_bridge_resources(struct pci_bus *bus,
15941592 unsigned long type,
....@@ -1631,7 +1629,7 @@
16311629 if (!res || !res->end || !res->flags)
16321630 continue;
16331631
1634
- dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
1632
+ dev_info(&bus->dev, "resource %d %pR\n", i, res);
16351633 }
16361634 }
16371635
....@@ -1702,8 +1700,8 @@
17021700 int i;
17031701 bool *unassigned = data;
17041702
1705
- for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) {
1706
- struct resource *r = &dev->resource[i];
1703
+ for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1704
+ struct resource *r = &dev->resource[i + PCI_IOV_RESOURCES];
17071705 struct pci_bus_region region;
17081706
17091707 /* Not assigned or rejected by kernel? */
....@@ -1713,7 +1711,7 @@
17131711 pcibios_resource_to_bus(dev->bus, &region, r);
17141712 if (!region.start) {
17151713 *unassigned = true;
1716
- return 1; /* return early from pci_walk_bus() */
1714
+ return 1; /* Return early from pci_walk_bus() */
17171715 }
17181716 }
17191717
....@@ -1721,12 +1719,17 @@
17211719 }
17221720
17231721 static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1724
- enum enable_type enable_local)
1722
+ enum enable_type enable_local)
17251723 {
17261724 bool unassigned = false;
1725
+ struct pci_host_bridge *host;
17271726
17281727 if (enable_local != undefined)
17291728 return enable_local;
1729
+
1730
+ host = pci_find_host_bridge(bus);
1731
+ if (host->preserve_config)
1732
+ return auto_disabled;
17301733
17311734 pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
17321735 if (unassigned)
....@@ -1736,21 +1739,21 @@
17361739 }
17371740 #else
17381741 static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1739
- enum enable_type enable_local)
1742
+ enum enable_type enable_local)
17401743 {
17411744 return enable_local;
17421745 }
17431746 #endif
17441747
17451748 /*
1746
- * first try will not touch pci bridge res
1747
- * second and later try will clear small leaf bridge res
1748
- * will stop till to the max depth if can not find good one
1749
+ * First try will not touch PCI bridge res.
1750
+ * Second and later try will clear small leaf bridge res.
1751
+ * Will stop till to the max depth if can not find good one.
17491752 */
17501753 void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
17511754 {
1752
- LIST_HEAD(realloc_head); /* list of resources that
1753
- want additional resources */
1755
+ LIST_HEAD(realloc_head);
1756
+ /* List of resources that want additional resources */
17541757 struct list_head *add_list = NULL;
17551758 int tried_times = 0;
17561759 enum release_type rel_type = leaf_only;
....@@ -1759,26 +1762,26 @@
17591762 int pci_try_num = 1;
17601763 enum enable_type enable_local;
17611764
1762
- /* don't realloc if asked to do so */
1765
+ /* Don't realloc if asked to do so */
17631766 enable_local = pci_realloc_detect(bus, pci_realloc_enable);
17641767 if (pci_realloc_enabled(enable_local)) {
17651768 int max_depth = pci_bus_get_depth(bus);
17661769
17671770 pci_try_num = max_depth + 1;
1768
- dev_printk(KERN_DEBUG, &bus->dev,
1769
- "max bus depth: %d pci_try_num: %d\n",
1770
- max_depth, pci_try_num);
1771
+ dev_info(&bus->dev, "max bus depth: %d pci_try_num: %d\n",
1772
+ max_depth, pci_try_num);
17711773 }
17721774
17731775 again:
17741776 /*
1775
- * last try will use add_list, otherwise will try good to have as
1776
- * must have, so can realloc parent bridge resource
1777
+ * Last try will use add_list, otherwise will try good to have as must
1778
+ * have, so can realloc parent bridge resource
17771779 */
17781780 if (tried_times + 1 == pci_try_num)
17791781 add_list = &realloc_head;
1780
- /* Depth first, calculate sizes and alignments of all
1781
- subordinate buses. */
1782
+ /*
1783
+ * Depth first, calculate sizes and alignments of all subordinate buses.
1784
+ */
17821785 __pci_bus_size_bridges(bus, add_list);
17831786
17841787 /* Depth last, allocate resources and update the hardware. */
....@@ -1787,7 +1790,7 @@
17871790 BUG_ON(!list_empty(add_list));
17881791 tried_times++;
17891792
1790
- /* any device complain? */
1793
+ /* Any device complain? */
17911794 if (list_empty(&fail_head))
17921795 goto dump;
17931796
....@@ -1801,23 +1804,23 @@
18011804 goto dump;
18021805 }
18031806
1804
- dev_printk(KERN_DEBUG, &bus->dev,
1805
- "No. %d try to assign unassigned res\n", tried_times + 1);
1807
+ dev_info(&bus->dev, "No. %d try to assign unassigned res\n",
1808
+ tried_times + 1);
18061809
1807
- /* third times and later will not check if it is leaf */
1810
+ /* Third times and later will not check if it is leaf */
18081811 if ((tried_times + 1) > 2)
18091812 rel_type = whole_subtree;
18101813
18111814 /*
18121815 * Try to release leaf bridge's resources that doesn't fit resource of
1813
- * child device under that bridge
1816
+ * child device under that bridge.
18141817 */
18151818 list_for_each_entry(fail_res, &fail_head, list)
18161819 pci_bus_release_bridge_resources(fail_res->dev->bus,
18171820 fail_res->flags & PCI_RES_TYPE_MASK,
18181821 rel_type);
18191822
1820
- /* restore size and flags */
1823
+ /* Restore size and flags */
18211824 list_for_each_entry(fail_res, &fail_head, list) {
18221825 struct resource *res = fail_res->res;
18231826 int idx;
....@@ -1838,7 +1841,7 @@
18381841 goto again;
18391842
18401843 dump:
1841
- /* dump the resource on buses */
1844
+ /* Dump the resource on buses */
18421845 pci_bus_dump_resources(bus);
18431846 }
18441847
....@@ -1849,73 +1852,137 @@
18491852 list_for_each_entry(root_bus, &pci_root_buses, node) {
18501853 pci_assign_unassigned_root_bus_resources(root_bus);
18511854
1852
- /* Make sure the root bridge has a companion ACPI device: */
1855
+ /* Make sure the root bridge has a companion ACPI device */
18531856 if (ACPI_HANDLE(root_bus->bridge))
18541857 acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
18551858 }
18561859 }
18571860
1858
-static void extend_bridge_window(struct pci_dev *bridge, struct resource *res,
1859
- struct list_head *add_list, resource_size_t available)
1861
+static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res,
1862
+ struct list_head *add_list,
1863
+ resource_size_t new_size)
18601864 {
1861
- struct pci_dev_resource *dev_res;
1865
+ resource_size_t add_size, size = resource_size(res);
18621866
18631867 if (res->parent)
18641868 return;
18651869
1866
- if (resource_size(res) >= available)
1870
+ if (!new_size)
18671871 return;
18681872
1869
- dev_res = res_to_dev_res(add_list, res);
1870
- if (!dev_res)
1873
+ if (new_size > size) {
1874
+ add_size = new_size - size;
1875
+ pci_dbg(bridge, "bridge window %pR extended by %pa\n", res,
1876
+ &add_size);
1877
+ } else if (new_size < size) {
1878
+ add_size = size - new_size;
1879
+ pci_dbg(bridge, "bridge window %pR shrunken by %pa\n", res,
1880
+ &add_size);
1881
+ } else {
18711882 return;
1883
+ }
18721884
1873
- /* Is there room to extend the window? */
1874
- if (available - resource_size(res) <= dev_res->add_size)
1875
- return;
1876
-
1877
- dev_res->add_size = available - resource_size(res);
1878
- pci_dbg(bridge, "bridge window %pR extended by %pa\n", res,
1879
- &dev_res->add_size);
1885
+ res->end = res->start + new_size - 1;
1886
+ remove_from_list(add_list, res);
18801887 }
18811888
1882
-static void pci_bus_distribute_available_resources(struct pci_bus *bus,
1883
- struct list_head *add_list, resource_size_t available_io,
1884
- resource_size_t available_mmio, resource_size_t available_mmio_pref)
1889
+static void remove_dev_resource(struct resource *avail, struct pci_dev *dev,
1890
+ struct resource *res)
18851891 {
1886
- resource_size_t remaining_io, remaining_mmio, remaining_mmio_pref;
1892
+ resource_size_t size, align, tmp;
1893
+
1894
+ size = resource_size(res);
1895
+ if (!size)
1896
+ return;
1897
+
1898
+ align = pci_resource_alignment(dev, res);
1899
+ align = align ? ALIGN(avail->start, align) - avail->start : 0;
1900
+ tmp = align + size;
1901
+ avail->start = min(avail->start + tmp, avail->end + 1);
1902
+}
1903
+
1904
+static void remove_dev_resources(struct pci_dev *dev, struct resource *io,
1905
+ struct resource *mmio,
1906
+ struct resource *mmio_pref)
1907
+{
1908
+ int i;
1909
+
1910
+ for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1911
+ struct resource *res = &dev->resource[i];
1912
+
1913
+ if (resource_type(res) == IORESOURCE_IO) {
1914
+ remove_dev_resource(io, dev, res);
1915
+ } else if (resource_type(res) == IORESOURCE_MEM) {
1916
+
1917
+ /*
1918
+ * Make sure prefetchable memory is reduced from
1919
+ * the correct resource. Specifically we put 32-bit
1920
+ * prefetchable memory in non-prefetchable window
1921
+ * if there is an 64-bit pretchable window.
1922
+ *
1923
+ * See comments in __pci_bus_size_bridges() for
1924
+ * more information.
1925
+ */
1926
+ if ((res->flags & IORESOURCE_PREFETCH) &&
1927
+ ((res->flags & IORESOURCE_MEM_64) ==
1928
+ (mmio_pref->flags & IORESOURCE_MEM_64)))
1929
+ remove_dev_resource(mmio_pref, dev, res);
1930
+ else
1931
+ remove_dev_resource(mmio, dev, res);
1932
+ }
1933
+ }
1934
+}
1935
+
1936
+/*
1937
+ * io, mmio and mmio_pref contain the total amount of bridge window space
1938
+ * available. This includes the minimal space needed to cover all the
1939
+ * existing devices on the bus and the possible extra space that can be
1940
+ * shared with the bridges.
1941
+ */
1942
+static void pci_bus_distribute_available_resources(struct pci_bus *bus,
1943
+ struct list_head *add_list,
1944
+ struct resource io,
1945
+ struct resource mmio,
1946
+ struct resource mmio_pref)
1947
+{
18871948 unsigned int normal_bridges = 0, hotplug_bridges = 0;
18881949 struct resource *io_res, *mmio_res, *mmio_pref_res;
18891950 struct pci_dev *dev, *bridge = bus->self;
1951
+ resource_size_t io_per_b, mmio_per_b, mmio_pref_per_b, align;
18901952
1891
- io_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
1892
- mmio_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
1893
- mmio_pref_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
1953
+ io_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
1954
+ mmio_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
1955
+ mmio_pref_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
18941956
18951957 /*
1896
- * Update additional resource list (add_list) to fill all the
1897
- * extra resource space available for this port except the space
1898
- * calculated in __pci_bus_size_bridges() which covers all the
1899
- * devices currently connected to the port and below.
1958
+ * The alignment of this bridge is yet to be considered, hence it must
1959
+ * be done now before extending its bridge window.
19001960 */
1901
- extend_bridge_window(bridge, io_res, add_list, available_io);
1902
- extend_bridge_window(bridge, mmio_res, add_list, available_mmio);
1903
- extend_bridge_window(bridge, mmio_pref_res, add_list,
1904
- available_mmio_pref);
1961
+ align = pci_resource_alignment(bridge, io_res);
1962
+ if (!io_res->parent && align)
1963
+ io.start = min(ALIGN(io.start, align), io.end + 1);
1964
+
1965
+ align = pci_resource_alignment(bridge, mmio_res);
1966
+ if (!mmio_res->parent && align)
1967
+ mmio.start = min(ALIGN(mmio.start, align), mmio.end + 1);
1968
+
1969
+ align = pci_resource_alignment(bridge, mmio_pref_res);
1970
+ if (!mmio_pref_res->parent && align)
1971
+ mmio_pref.start = min(ALIGN(mmio_pref.start, align),
1972
+ mmio_pref.end + 1);
19051973
19061974 /*
1907
- * Calculate the total amount of extra resource space we can
1908
- * pass to bridges below this one. This is basically the
1909
- * extra space reduced by the minimal required space for the
1910
- * non-hotplug bridges.
1975
+ * Now that we have adjusted for alignment, update the bridge window
1976
+ * resources to fill as much remaining resource space as possible.
19111977 */
1912
- remaining_io = available_io;
1913
- remaining_mmio = available_mmio;
1914
- remaining_mmio_pref = available_mmio_pref;
1978
+ adjust_bridge_window(bridge, io_res, add_list, resource_size(&io));
1979
+ adjust_bridge_window(bridge, mmio_res, add_list, resource_size(&mmio));
1980
+ adjust_bridge_window(bridge, mmio_pref_res, add_list,
1981
+ resource_size(&mmio_pref));
19151982
19161983 /*
19171984 * Calculate how many hotplug bridges and normal bridges there
1918
- * are on this bus. We will distribute the additional available
1985
+ * are on this bus. We will distribute the additional available
19191986 * resources between hotplug bridges.
19201987 */
19211988 for_each_pci_bridge(dev, bus) {
....@@ -1925,110 +1992,116 @@
19251992 normal_bridges++;
19261993 }
19271994
1928
- for_each_pci_bridge(dev, bus) {
1929
- const struct resource *res;
1930
-
1931
- if (dev->is_hotplug_bridge)
1932
- continue;
1933
-
1934
- /*
1935
- * Reduce the available resource space by what the
1936
- * bridge and devices below it occupy.
1937
- */
1938
- res = &dev->resource[PCI_BRIDGE_RESOURCES + 0];
1939
- if (!res->parent && available_io > resource_size(res))
1940
- remaining_io -= resource_size(res);
1941
-
1942
- res = &dev->resource[PCI_BRIDGE_RESOURCES + 1];
1943
- if (!res->parent && available_mmio > resource_size(res))
1944
- remaining_mmio -= resource_size(res);
1945
-
1946
- res = &dev->resource[PCI_BRIDGE_RESOURCES + 2];
1947
- if (!res->parent && available_mmio_pref > resource_size(res))
1948
- remaining_mmio_pref -= resource_size(res);
1949
- }
1950
-
1951
- /*
1952
- * There is only one bridge on the bus so it gets all available
1953
- * resources which it can then distribute to the possible
1954
- * hotplug bridges below.
1955
- */
1956
- if (hotplug_bridges + normal_bridges == 1) {
1957
- dev = list_first_entry(&bus->devices, struct pci_dev, bus_list);
1958
- if (dev->subordinate) {
1959
- pci_bus_distribute_available_resources(dev->subordinate,
1960
- add_list, available_io, available_mmio,
1961
- available_mmio_pref);
1962
- }
1995
+ if (!(hotplug_bridges + normal_bridges))
19631996 return;
1997
+
1998
+ /*
1999
+ * Calculate the amount of space we can forward from "bus" to any
2000
+ * downstream buses, i.e., the space left over after assigning the
2001
+ * BARs and windows on "bus".
2002
+ */
2003
+ list_for_each_entry(dev, &bus->devices, bus_list) {
2004
+ if (!dev->is_virtfn)
2005
+ remove_dev_resources(dev, &io, &mmio, &mmio_pref);
19642006 }
19652007
19662008 /*
1967
- * Go over devices on this bus and distribute the remaining
1968
- * resource space between hotplug bridges.
2009
+ * If there is at least one hotplug bridge on this bus it gets all
2010
+ * the extra resource space that was left after the reductions
2011
+ * above.
2012
+ *
2013
+ * If there are no hotplug bridges the extra resource space is
2014
+ * split between non-hotplug bridges. This is to allow possible
2015
+ * hotplug bridges below them to get the extra space as well.
19692016 */
2017
+ if (hotplug_bridges) {
2018
+ io_per_b = div64_ul(resource_size(&io), hotplug_bridges);
2019
+ mmio_per_b = div64_ul(resource_size(&mmio), hotplug_bridges);
2020
+ mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
2021
+ hotplug_bridges);
2022
+ } else {
2023
+ io_per_b = div64_ul(resource_size(&io), normal_bridges);
2024
+ mmio_per_b = div64_ul(resource_size(&mmio), normal_bridges);
2025
+ mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
2026
+ normal_bridges);
2027
+ }
2028
+
19702029 for_each_pci_bridge(dev, bus) {
1971
- resource_size_t align, io, mmio, mmio_pref;
2030
+ struct resource *res;
19722031 struct pci_bus *b;
19732032
19742033 b = dev->subordinate;
1975
- if (!b || !dev->is_hotplug_bridge)
2034
+ if (!b)
2035
+ continue;
2036
+ if (hotplug_bridges && !dev->is_hotplug_bridge)
19762037 continue;
19772038
2039
+ res = &dev->resource[PCI_BRIDGE_IO_WINDOW];
2040
+
19782041 /*
1979
- * Distribute available extra resources equally between
1980
- * hotplug-capable downstream ports taking alignment into
1981
- * account.
1982
- *
1983
- * Here hotplug_bridges is always != 0.
2042
+ * Make sure the split resource space is properly aligned
2043
+ * for bridge windows (align it down to avoid going above
2044
+ * what is available).
19842045 */
1985
- align = pci_resource_alignment(bridge, io_res);
1986
- io = div64_ul(available_io, hotplug_bridges);
1987
- io = min(ALIGN(io, align), remaining_io);
1988
- remaining_io -= io;
2046
+ align = pci_resource_alignment(dev, res);
2047
+ io.end = align ? io.start + ALIGN_DOWN(io_per_b, align) - 1
2048
+ : io.start + io_per_b - 1;
19892049
1990
- align = pci_resource_alignment(bridge, mmio_res);
1991
- mmio = div64_ul(available_mmio, hotplug_bridges);
1992
- mmio = min(ALIGN(mmio, align), remaining_mmio);
1993
- remaining_mmio -= mmio;
2050
+ /*
2051
+ * The x_per_b holds the extra resource space that can be
2052
+ * added for each bridge but there is the minimal already
2053
+ * reserved as well so adjust x.start down accordingly to
2054
+ * cover the whole space.
2055
+ */
2056
+ io.start -= resource_size(res);
19942057
1995
- align = pci_resource_alignment(bridge, mmio_pref_res);
1996
- mmio_pref = div64_ul(available_mmio_pref, hotplug_bridges);
1997
- mmio_pref = min(ALIGN(mmio_pref, align), remaining_mmio_pref);
1998
- remaining_mmio_pref -= mmio_pref;
2058
+ res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
2059
+ align = pci_resource_alignment(dev, res);
2060
+ mmio.end = align ? mmio.start + ALIGN_DOWN(mmio_per_b, align) - 1
2061
+ : mmio.start + mmio_per_b - 1;
2062
+ mmio.start -= resource_size(res);
2063
+
2064
+ res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
2065
+ align = pci_resource_alignment(dev, res);
2066
+ mmio_pref.end = align ? mmio_pref.start +
2067
+ ALIGN_DOWN(mmio_pref_per_b, align) - 1
2068
+ : mmio_pref.start + mmio_pref_per_b - 1;
2069
+ mmio_pref.start -= resource_size(res);
19992070
20002071 pci_bus_distribute_available_resources(b, add_list, io, mmio,
20012072 mmio_pref);
2073
+
2074
+ io.start += io.end + 1;
2075
+ mmio.start += mmio.end + 1;
2076
+ mmio_pref.start += mmio_pref.end + 1;
20022077 }
20032078 }
20042079
2005
-static void
2006
-pci_bridge_distribute_available_resources(struct pci_dev *bridge,
2007
- struct list_head *add_list)
2080
+static void pci_bridge_distribute_available_resources(struct pci_dev *bridge,
2081
+ struct list_head *add_list)
20082082 {
2009
- resource_size_t available_io, available_mmio, available_mmio_pref;
2010
- const struct resource *res;
2083
+ struct resource available_io, available_mmio, available_mmio_pref;
20112084
20122085 if (!bridge->is_hotplug_bridge)
20132086 return;
20142087
20152088 /* Take the initial extra resources from the hotplug port */
2016
- res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
2017
- available_io = resource_size(res);
2018
- res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
2019
- available_mmio = resource_size(res);
2020
- res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
2021
- available_mmio_pref = resource_size(res);
2089
+ available_io = bridge->resource[PCI_BRIDGE_IO_WINDOW];
2090
+ available_mmio = bridge->resource[PCI_BRIDGE_MEM_WINDOW];
2091
+ available_mmio_pref = bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
20222092
20232093 pci_bus_distribute_available_resources(bridge->subordinate,
2024
- add_list, available_io, available_mmio, available_mmio_pref);
2094
+ add_list, available_io,
2095
+ available_mmio,
2096
+ available_mmio_pref);
20252097 }
20262098
20272099 void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
20282100 {
20292101 struct pci_bus *parent = bridge->subordinate;
2030
- LIST_HEAD(add_list); /* list of resources that
2031
- want additional resources */
2102
+ /* List of resources that want additional resources */
2103
+ LIST_HEAD(add_list);
2104
+
20322105 int tried_times = 0;
20332106 LIST_HEAD(fail_head);
20342107 struct pci_dev_resource *fail_res;
....@@ -2038,9 +2111,9 @@
20382111 __pci_bus_size_bridges(parent, &add_list);
20392112
20402113 /*
2041
- * Distribute remaining resources (if any) equally between
2042
- * hotplug bridges below. This makes it possible to extend the
2043
- * hierarchy later without running out of resources.
2114
+ * Distribute remaining resources (if any) equally between hotplug
2115
+ * bridges below. This makes it possible to extend the hierarchy
2116
+ * later without running out of resources.
20442117 */
20452118 pci_bridge_distribute_available_resources(bridge, &add_list);
20462119
....@@ -2052,7 +2125,7 @@
20522125 goto enable_all;
20532126
20542127 if (tried_times >= 2) {
2055
- /* still fail, don't need to try more */
2128
+ /* Still fail, don't need to try more */
20562129 free_list(&fail_head);
20572130 goto enable_all;
20582131 }
....@@ -2061,15 +2134,15 @@
20612134 tried_times + 1);
20622135
20632136 /*
2064
- * Try to release leaf bridge's resources that doesn't fit resource of
2065
- * child device under that bridge
2137
+ * Try to release leaf bridge's resources that aren't big enough
2138
+ * to contain child device resources.
20662139 */
20672140 list_for_each_entry(fail_res, &fail_head, list)
20682141 pci_bus_release_bridge_resources(fail_res->dev->bus,
20692142 fail_res->flags & PCI_RES_TYPE_MASK,
20702143 whole_subtree);
20712144
2072
- /* restore size and flags */
2145
+ /* Restore size and flags */
20732146 list_for_each_entry(fail_res, &fail_head, list) {
20742147 struct resource *res = fail_res->res;
20752148 int idx;
....@@ -2107,6 +2180,8 @@
21072180 unsigned int i;
21082181 int ret;
21092182
2183
+ down_read(&pci_bus_sem);
2184
+
21102185 /* Walk to the root hub, releasing bridge BARs when possible */
21112186 next = bridge;
21122187 do {
....@@ -2141,8 +2216,10 @@
21412216 next = bridge->bus ? bridge->bus->self : NULL;
21422217 } while (next);
21432218
2144
- if (list_empty(&saved))
2219
+ if (list_empty(&saved)) {
2220
+ up_read(&pci_bus_sem);
21452221 return -ENOENT;
2222
+ }
21462223
21472224 __pci_bus_size_bridges(bridge->subordinate, &added);
21482225 __pci_bridge_assign_resources(bridge, &added, &failed);
....@@ -2154,7 +2231,7 @@
21542231 }
21552232
21562233 list_for_each_entry(dev_res, &saved, list) {
2157
- /* Skip the bridge we just assigned resources for. */
2234
+ /* Skip the bridge we just assigned resources for */
21582235 if (bridge == dev_res->dev)
21592236 continue;
21602237
....@@ -2163,10 +2240,11 @@
21632240 }
21642241
21652242 free_list(&saved);
2243
+ up_read(&pci_bus_sem);
21662244 return 0;
21672245
21682246 cleanup:
2169
- /* restore size and flags */
2247
+ /* Restore size and flags */
21702248 list_for_each_entry(dev_res, &failed, list) {
21712249 struct resource *res = dev_res->res;
21722250
....@@ -2191,6 +2269,7 @@
21912269 pci_setup_bridge(bridge->subordinate);
21922270 }
21932271 free_list(&saved);
2272
+ up_read(&pci_bus_sem);
21942273
21952274 return ret;
21962275 }
....@@ -2198,8 +2277,8 @@
21982277 void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
21992278 {
22002279 struct pci_dev *dev;
2201
- LIST_HEAD(add_list); /* list of resources that
2202
- want additional resources */
2280
+ /* List of resources that want additional resources */
2281
+ LIST_HEAD(add_list);
22032282
22042283 down_read(&pci_bus_sem);
22052284 for_each_pci_bridge(dev, bus)