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