Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / pci / setup-bus.c
1 /*
2  *      drivers/pci/setup-bus.c
3  *
4  * Extruded from code written by
5  *      Dave Rusling (david.rusling@reo.mts.dec.com)
6  *      David Mosberger (davidm@cs.arizona.edu)
7  *      David Miller (davem@redhat.com)
8  *
9  * Support routines for initializing a PCI subsystem.
10  */
11
12 /*
13  * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
14  *           PCI-PCI bridges cleanup, sorted resource allocation.
15  * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
16  *           Converted to allocation in 3 passes, which gives
17  *           tighter packing. Prefetchable range support.
18  */
19
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/pci.h>
24 #include <linux/errno.h>
25 #include <linux/ioport.h>
26 #include <linux/cache.h>
27 #include <linux/slab.h>
28 #include <asm-generic/pci-bridge.h>
29 #include "pci.h"
30
31 unsigned int pci_flags;
32
33 struct pci_dev_resource {
34         struct list_head list;
35         struct resource *res;
36         struct pci_dev *dev;
37         resource_size_t start;
38         resource_size_t end;
39         resource_size_t add_size;
40         resource_size_t min_align;
41         unsigned long flags;
42 };
43
44 static void free_list(struct list_head *head)
45 {
46         struct pci_dev_resource *dev_res, *tmp;
47
48         list_for_each_entry_safe(dev_res, tmp, head, list) {
49                 list_del(&dev_res->list);
50                 kfree(dev_res);
51         }
52 }
53
54 /**
55  * add_to_list() - add a new resource tracker to the list
56  * @head:       Head of the list
57  * @dev:        device corresponding to which the resource
58  *              belongs
59  * @res:        The resource to be tracked
60  * @add_size:   additional size to be optionally added
61  *              to the resource
62  */
63 static int add_to_list(struct list_head *head,
64                  struct pci_dev *dev, struct resource *res,
65                  resource_size_t add_size, resource_size_t min_align)
66 {
67         struct pci_dev_resource *tmp;
68
69         tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
70         if (!tmp) {
71                 pr_warning("add_to_list: kmalloc() failed!\n");
72                 return -ENOMEM;
73         }
74
75         tmp->res = res;
76         tmp->dev = dev;
77         tmp->start = res->start;
78         tmp->end = res->end;
79         tmp->flags = res->flags;
80         tmp->add_size = add_size;
81         tmp->min_align = min_align;
82
83         list_add(&tmp->list, head);
84
85         return 0;
86 }
87
88 static void remove_from_list(struct list_head *head,
89                                  struct resource *res)
90 {
91         struct pci_dev_resource *dev_res, *tmp;
92
93         list_for_each_entry_safe(dev_res, tmp, head, list) {
94                 if (dev_res->res == res) {
95                         list_del(&dev_res->list);
96                         kfree(dev_res);
97                         break;
98                 }
99         }
100 }
101
102 static resource_size_t get_res_add_size(struct list_head *head,
103                                         struct resource *res)
104 {
105         struct pci_dev_resource *dev_res;
106
107         list_for_each_entry(dev_res, head, list) {
108                 if (dev_res->res == res) {
109                         int idx = res - &dev_res->dev->resource[0];
110
111                         dev_printk(KERN_DEBUG, &dev_res->dev->dev,
112                                  "res[%d]=%pR get_res_add_size add_size %llx\n",
113                                  idx, dev_res->res,
114                                  (unsigned long long)dev_res->add_size);
115
116                         return dev_res->add_size;
117                 }
118         }
119
120         return 0;
121 }
122
123 /* Sort resources by alignment */
124 static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
125 {
126         int i;
127
128         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
129                 struct resource *r;
130                 struct pci_dev_resource *dev_res, *tmp;
131                 resource_size_t r_align;
132                 struct list_head *n;
133
134                 r = &dev->resource[i];
135
136                 if (r->flags & IORESOURCE_PCI_FIXED)
137                         continue;
138
139                 if (!(r->flags) || r->parent)
140                         continue;
141
142                 r_align = pci_resource_alignment(dev, r);
143                 if (!r_align) {
144                         dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
145                                  i, r);
146                         continue;
147                 }
148
149                 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
150                 if (!tmp)
151                         panic("pdev_sort_resources(): "
152                               "kmalloc() failed!\n");
153                 tmp->res = r;
154                 tmp->dev = dev;
155
156                 /* fallback is smallest one or list is empty*/
157                 n = head;
158                 list_for_each_entry(dev_res, head, list) {
159                         resource_size_t align;
160
161                         align = pci_resource_alignment(dev_res->dev,
162                                                          dev_res->res);
163
164                         if (r_align > align) {
165                                 n = &dev_res->list;
166                                 break;
167                         }
168                 }
169                 /* Insert it just before n*/
170                 list_add_tail(&tmp->list, n);
171         }
172 }
173
174 static void __dev_sort_resources(struct pci_dev *dev,
175                                  struct list_head *head)
176 {
177         u16 class = dev->class >> 8;
178
179         /* Don't touch classless devices or host bridges or ioapics.  */
180         if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
181                 return;
182
183         /* Don't touch ioapic devices already enabled by firmware */
184         if (class == PCI_CLASS_SYSTEM_PIC) {
185                 u16 command;
186                 pci_read_config_word(dev, PCI_COMMAND, &command);
187                 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
188                         return;
189         }
190
191         pdev_sort_resources(dev, head);
192 }
193
194 static inline void reset_resource(struct resource *res)
195 {
196         res->start = 0;
197         res->end = 0;
198         res->flags = 0;
199 }
200
201 /**
202  * reassign_resources_sorted() - satisfy any additional resource requests
203  *
204  * @realloc_head : head of the list tracking requests requiring additional
205  *             resources
206  * @head     : head of the list tracking requests with allocated
207  *             resources
208  *
209  * Walk through each element of the realloc_head and try to procure
210  * additional resources for the element, provided the element
211  * is in the head list.
212  */
213 static void reassign_resources_sorted(struct list_head *realloc_head,
214                 struct list_head *head)
215 {
216         struct resource *res;
217         struct pci_dev_resource *add_res, *tmp;
218         struct pci_dev_resource *dev_res;
219         resource_size_t add_size;
220         int idx;
221
222         list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
223                 bool found_match = false;
224
225                 res = add_res->res;
226                 /* skip resource that has been reset */
227                 if (!res->flags)
228                         goto out;
229
230                 /* skip this resource if not found in head list */
231                 list_for_each_entry(dev_res, head, list) {
232                         if (dev_res->res == res) {
233                                 found_match = true;
234                                 break;
235                         }
236                 }
237                 if (!found_match)/* just skip */
238                         continue;
239
240                 idx = res - &add_res->dev->resource[0];
241                 add_size = add_res->add_size;
242                 if (!resource_size(res)) {
243                         res->start = add_res->start;
244                         res->end = res->start + add_size - 1;
245                         if (pci_assign_resource(add_res->dev, idx))
246                                 reset_resource(res);
247                 } else {
248                         resource_size_t align = add_res->min_align;
249                         res->flags |= add_res->flags &
250                                  (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
251                         if (pci_reassign_resource(add_res->dev, idx,
252                                                   add_size, align))
253                                 dev_printk(KERN_DEBUG, &add_res->dev->dev,
254                                            "failed to add %llx res[%d]=%pR\n",
255                                            (unsigned long long)add_size,
256                                            idx, 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  * @failed_list : head of the list tracking requests that could
269  *              not be allocated
270  *
271  * Satisfy resource requests of each element in the list. Add
272  * requests that could not 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 && !pci_is_root_bus(dev_res->dev->bus)) {
287                                 /*
288                                  * if the failed res is for ROM BAR, and it will
289                                  * be enabled later, don't add it to the list
290                                  */
291                                 if (!((idx == PCI_ROM_RESOURCE) &&
292                                       (!(res->flags & IORESOURCE_ROM_ENABLE))))
293                                         add_to_list(fail_head,
294                                                     dev_res->dev, res,
295                                                     0 /* dont care */,
296                                                     0 /* dont care */);
297                         }
298                         reset_resource(res);
299                 }
300         }
301 }
302
303 static void __assign_resources_sorted(struct list_head *head,
304                                  struct list_head *realloc_head,
305                                  struct list_head *fail_head)
306 {
307         /*
308          * Should not assign requested resources at first.
309          *   they could be adjacent, so later reassign can not reallocate
310          *   them one by one in parent resource window.
311          * Try to assign requested + add_size at begining
312          *  if could do that, could get out early.
313          *  if could not do that, we still try to assign requested at first,
314          *    then try to reassign add_size for some resources.
315          */
316         LIST_HEAD(save_head);
317         LIST_HEAD(local_fail_head);
318         struct pci_dev_resource *save_res;
319         struct pci_dev_resource *dev_res;
320
321         /* Check if optional add_size is there */
322         if (!realloc_head || list_empty(realloc_head))
323                 goto requested_and_reassign;
324
325         /* Save original start, end, flags etc at first */
326         list_for_each_entry(dev_res, head, list) {
327                 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
328                         free_list(&save_head);
329                         goto requested_and_reassign;
330                 }
331         }
332
333         /* Update res in head list with add_size in realloc_head list */
334         list_for_each_entry(dev_res, head, list)
335                 dev_res->res->end += get_res_add_size(realloc_head,
336                                                         dev_res->res);
337
338         /* Try updated head list with add_size added */
339         assign_requested_resources_sorted(head, &local_fail_head);
340
341         /* all assigned with add_size ? */
342         if (list_empty(&local_fail_head)) {
343                 /* Remove head list from realloc_head list */
344                 list_for_each_entry(dev_res, head, list)
345                         remove_from_list(realloc_head, dev_res->res);
346                 free_list(&save_head);
347                 free_list(head);
348                 return;
349         }
350
351         free_list(&local_fail_head);
352         /* Release assigned resource */
353         list_for_each_entry(dev_res, head, list)
354                 if (dev_res->res->parent)
355                         release_resource(dev_res->res);
356         /* Restore start/end/flags from saved list */
357         list_for_each_entry(save_res, &save_head, list) {
358                 struct resource *res = save_res->res;
359
360                 res->start = save_res->start;
361                 res->end = save_res->end;
362                 res->flags = save_res->flags;
363         }
364         free_list(&save_head);
365
366 requested_and_reassign:
367         /* Satisfy the must-have resource requests */
368         assign_requested_resources_sorted(head, fail_head);
369
370         /* Try to satisfy any additional optional resource
371                 requests */
372         if (realloc_head)
373                 reassign_resources_sorted(realloc_head, head);
374         free_list(head);
375 }
376
377 static void pdev_assign_resources_sorted(struct pci_dev *dev,
378                                  struct list_head *add_head,
379                                  struct list_head *fail_head)
380 {
381         LIST_HEAD(head);
382
383         __dev_sort_resources(dev, &head);
384         __assign_resources_sorted(&head, add_head, fail_head);
385
386 }
387
388 static void pbus_assign_resources_sorted(const struct pci_bus *bus,
389                                          struct list_head *realloc_head,
390                                          struct list_head *fail_head)
391 {
392         struct pci_dev *dev;
393         LIST_HEAD(head);
394
395         list_for_each_entry(dev, &bus->devices, bus_list)
396                 __dev_sort_resources(dev, &head);
397
398         __assign_resources_sorted(&head, realloc_head, fail_head);
399 }
400
401 void pci_setup_cardbus(struct pci_bus *bus)
402 {
403         struct pci_dev *bridge = bus->self;
404         struct resource *res;
405         struct pci_bus_region region;
406
407         dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
408                  bus->secondary, bus->subordinate);
409
410         res = bus->resource[0];
411         pcibios_resource_to_bus(bridge, &region, res);
412         if (res->flags & IORESOURCE_IO) {
413                 /*
414                  * The IO resource is allocated a range twice as large as it
415                  * would normally need.  This allows us to set both IO regs.
416                  */
417                 dev_info(&bridge->dev, "  bridge window %pR\n", res);
418                 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
419                                         region.start);
420                 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
421                                         region.end);
422         }
423
424         res = bus->resource[1];
425         pcibios_resource_to_bus(bridge, &region, res);
426         if (res->flags & IORESOURCE_IO) {
427                 dev_info(&bridge->dev, "  bridge window %pR\n", res);
428                 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
429                                         region.start);
430                 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
431                                         region.end);
432         }
433
434         res = bus->resource[2];
435         pcibios_resource_to_bus(bridge, &region, res);
436         if (res->flags & IORESOURCE_MEM) {
437                 dev_info(&bridge->dev, "  bridge window %pR\n", res);
438                 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
439                                         region.start);
440                 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
441                                         region.end);
442         }
443
444         res = bus->resource[3];
445         pcibios_resource_to_bus(bridge, &region, res);
446         if (res->flags & IORESOURCE_MEM) {
447                 dev_info(&bridge->dev, "  bridge window %pR\n", res);
448                 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
449                                         region.start);
450                 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
451                                         region.end);
452         }
453 }
454 EXPORT_SYMBOL(pci_setup_cardbus);
455
456 /* Initialize bridges with base/limit values we have collected.
457    PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
458    requires that if there is no I/O ports or memory behind the
459    bridge, corresponding range must be turned off by writing base
460    value greater than limit to the bridge's base/limit registers.
461
462    Note: care must be taken when updating I/O base/limit registers
463    of bridges which support 32-bit I/O. This update requires two
464    config space writes, so it's quite possible that an I/O window of
465    the bridge will have some undesirable address (e.g. 0) after the
466    first write. Ditto 64-bit prefetchable MMIO.  */
467 static void pci_setup_bridge_io(struct pci_bus *bus)
468 {
469         struct pci_dev *bridge = bus->self;
470         struct resource *res;
471         struct pci_bus_region region;
472         u32 l, io_upper16;
473
474         /* Set up the top and bottom of the PCI I/O segment for this bus. */
475         res = bus->resource[0];
476         pcibios_resource_to_bus(bridge, &region, res);
477         if (res->flags & IORESOURCE_IO) {
478                 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
479                 l &= 0xffff0000;
480                 l |= (region.start >> 8) & 0x00f0;
481                 l |= region.end & 0xf000;
482                 /* Set up upper 16 bits of I/O base/limit. */
483                 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
484                 dev_info(&bridge->dev, "  bridge window %pR\n", res);
485         } else {
486                 /* Clear upper 16 bits of I/O base/limit. */
487                 io_upper16 = 0;
488                 l = 0x00f0;
489         }
490         /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
491         pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
492         /* Update lower 16 bits of I/O base/limit. */
493         pci_write_config_dword(bridge, PCI_IO_BASE, l);
494         /* Update upper 16 bits of I/O base/limit. */
495         pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
496 }
497
498 static void pci_setup_bridge_mmio(struct pci_bus *bus)
499 {
500         struct pci_dev *bridge = bus->self;
501         struct resource *res;
502         struct pci_bus_region region;
503         u32 l;
504
505         /* Set up the top and bottom of the PCI Memory segment for this bus. */
506         res = bus->resource[1];
507         pcibios_resource_to_bus(bridge, &region, res);
508         if (res->flags & IORESOURCE_MEM) {
509                 l = (region.start >> 16) & 0xfff0;
510                 l |= region.end & 0xfff00000;
511                 dev_info(&bridge->dev, "  bridge window %pR\n", res);
512         } else {
513                 l = 0x0000fff0;
514         }
515         pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
516 }
517
518 static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
519 {
520         struct pci_dev *bridge = bus->self;
521         struct resource *res;
522         struct pci_bus_region region;
523         u32 l, bu, lu;
524
525         /* Clear out the upper 32 bits of PREF limit.
526            If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
527            disables PREF range, which is ok. */
528         pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
529
530         /* Set up PREF base/limit. */
531         bu = lu = 0;
532         res = bus->resource[2];
533         pcibios_resource_to_bus(bridge, &region, res);
534         if (res->flags & IORESOURCE_PREFETCH) {
535                 l = (region.start >> 16) & 0xfff0;
536                 l |= region.end & 0xfff00000;
537                 if (res->flags & IORESOURCE_MEM_64) {
538                         bu = upper_32_bits(region.start);
539                         lu = upper_32_bits(region.end);
540                 }
541                 dev_info(&bridge->dev, "  bridge window %pR\n", res);
542         } else {
543                 l = 0x0000fff0;
544         }
545         pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
546
547         /* Set the upper 32 bits of PREF base & limit. */
548         pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
549         pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
550 }
551
552 static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
553 {
554         struct pci_dev *bridge = bus->self;
555
556         dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
557                  bus->secondary, bus->subordinate);
558
559         if (type & IORESOURCE_IO)
560                 pci_setup_bridge_io(bus);
561
562         if (type & IORESOURCE_MEM)
563                 pci_setup_bridge_mmio(bus);
564
565         if (type & IORESOURCE_PREFETCH)
566                 pci_setup_bridge_mmio_pref(bus);
567
568         pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
569 }
570
571 void pci_setup_bridge(struct pci_bus *bus)
572 {
573         unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
574                                   IORESOURCE_PREFETCH;
575
576         __pci_setup_bridge(bus, type);
577 }
578
579 /* Check whether the bridge supports optional I/O and
580    prefetchable memory ranges. If not, the respective
581    base/limit registers must be read-only and read as 0. */
582 static void pci_bridge_check_ranges(struct pci_bus *bus)
583 {
584         u16 io;
585         u32 pmem;
586         struct pci_dev *bridge = bus->self;
587         struct resource *b_res;
588
589         b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
590         b_res[1].flags |= IORESOURCE_MEM;
591
592         pci_read_config_word(bridge, PCI_IO_BASE, &io);
593         if (!io) {
594                 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
595                 pci_read_config_word(bridge, PCI_IO_BASE, &io);
596                 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
597         }
598         if (io)
599                 b_res[0].flags |= IORESOURCE_IO;
600         /*  DECchip 21050 pass 2 errata: the bridge may miss an address
601             disconnect boundary by one PCI data phase.
602             Workaround: do not use prefetching on this device. */
603         if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
604                 return;
605         pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
606         if (!pmem) {
607                 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
608                                                0xfff0fff0);
609                 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
610                 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
611         }
612         if (pmem) {
613                 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
614                 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
615                     PCI_PREF_RANGE_TYPE_64) {
616                         b_res[2].flags |= IORESOURCE_MEM_64;
617                         b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
618                 }
619         }
620
621         /* double check if bridge does support 64 bit pref */
622         if (b_res[2].flags & IORESOURCE_MEM_64) {
623                 u32 mem_base_hi, tmp;
624                 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
625                                          &mem_base_hi);
626                 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
627                                                0xffffffff);
628                 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
629                 if (!tmp)
630                         b_res[2].flags &= ~IORESOURCE_MEM_64;
631                 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
632                                        mem_base_hi);
633         }
634 }
635
636 /* Helper function for sizing routines: find first available
637    bus resource of a given type. Note: we intentionally skip
638    the bus resources which have already been assigned (that is,
639    have non-NULL parent resource). */
640 static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
641 {
642         int i;
643         struct resource *r;
644         unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
645                                   IORESOURCE_PREFETCH;
646
647         pci_bus_for_each_resource(bus, r, i) {
648                 if (r == &ioport_resource || r == &iomem_resource)
649                         continue;
650                 if (r && (r->flags & type_mask) == type && !r->parent)
651                         return r;
652         }
653         return NULL;
654 }
655
656 static resource_size_t calculate_iosize(resource_size_t size,
657                 resource_size_t min_size,
658                 resource_size_t size1,
659                 resource_size_t old_size,
660                 resource_size_t align)
661 {
662         if (size < min_size)
663                 size = min_size;
664         if (old_size == 1 )
665                 old_size = 0;
666         /* To be fixed in 2.5: we should have sort of HAVE_ISA
667            flag in the struct pci_bus. */
668 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
669         size = (size & 0xff) + ((size & ~0xffUL) << 2);
670 #endif
671         size = ALIGN(size + size1, align);
672         if (size < old_size)
673                 size = old_size;
674         return size;
675 }
676
677 static resource_size_t calculate_memsize(resource_size_t size,
678                 resource_size_t min_size,
679                 resource_size_t size1,
680                 resource_size_t old_size,
681                 resource_size_t align)
682 {
683         if (size < min_size)
684                 size = min_size;
685         if (old_size == 1 )
686                 old_size = 0;
687         if (size < old_size)
688                 size = old_size;
689         size = ALIGN(size + size1, align);
690         return size;
691 }
692
693 /**
694  * pbus_size_io() - size the io window of a given bus
695  *
696  * @bus : the bus
697  * @min_size : the minimum io window that must to be allocated
698  * @add_size : additional optional io window
699  * @realloc_head : track the additional io window on this list
700  *
701  * Sizing the IO windows of the PCI-PCI bridge is trivial,
702  * since these windows have 4K granularity and the IO ranges
703  * of non-bridge PCI devices are limited to 256 bytes.
704  * We must be careful with the ISA aliasing though.
705  */
706 static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
707                 resource_size_t add_size, struct list_head *realloc_head)
708 {
709         struct pci_dev *dev;
710         struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
711         unsigned long size = 0, size0 = 0, size1 = 0, res_size;
712         resource_size_t children_add_size = 0;
713
714         if (!b_res)
715                 return;
716
717         list_for_each_entry(dev, &bus->devices, bus_list) {
718                 int i;
719
720                 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
721                         struct resource *r = &dev->resource[i];
722                         unsigned long r_size;
723
724                         if (r->parent || !(r->flags & IORESOURCE_IO))
725                                 continue;
726                         r_size = resource_size(r);
727
728                         if (r_size < 0x400)
729                                 /* Might be re-aligned for ISA */
730                                 size += r_size;
731                         else
732                                 size1 += r_size;
733
734                         if (realloc_head)
735                                 children_add_size += get_res_add_size(realloc_head, r);
736                 }
737         }
738         size0 = calculate_iosize(size, min_size, size1,
739                         resource_size(b_res), 4096);
740         if (children_add_size > add_size)
741                 add_size = children_add_size;
742         size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
743                 calculate_iosize(size, min_size, add_size + size1,
744                         resource_size(b_res), 4096);
745         res_size = pci_reserve_size_io(bus);
746         if (size0 < res_size)
747                 size0 = ALIGN(res_size, 4096);
748         if (size1 < res_size)
749                 size1 = ALIGN(res_size, 4096);
750         if (!size0 && !size1) {
751                 if (b_res->start || b_res->end)
752                         dev_info(&bus->self->dev, "disabling bridge window "
753                                  "%pR to [bus %02x-%02x] (unused)\n", b_res,
754                                  bus->secondary, bus->subordinate);
755                 b_res->flags = 0;
756                 return;
757         }
758         /* Alignment of the IO window is always 4K */
759         b_res->start = 4096;
760         b_res->end = b_res->start + size0 - 1;
761         b_res->flags |= IORESOURCE_STARTALIGN;
762         if (size1 > size0 && realloc_head) {
763                 add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096);
764                 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
765                                  "%pR to [bus %02x-%02x] add_size %lx\n", b_res,
766                                  bus->secondary, bus->subordinate, size1-size0);
767         }
768 }
769
770 /**
771  * pbus_size_mem() - size the memory window of a given bus
772  *
773  * @bus : the bus
774  * @min_size : the minimum memory window that must to be allocated
775  * @add_size : additional optional memory window
776  * @realloc_head : track the additional memory window on this list
777  *
778  * Calculate the size of the bus and minimal alignment which
779  * guarantees that all child resources fit in this size.
780  */
781 static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
782                          unsigned long type, resource_size_t min_size,
783                         resource_size_t add_size,
784                         struct list_head *realloc_head)
785 {
786         struct pci_dev *dev;
787         resource_size_t min_align, align, size, size0, size1;
788         resource_size_t aligns[12];     /* Alignments from 1Mb to 2Gb */
789         int order, max_order;
790         struct resource *b_res = find_free_bus_resource(bus, type);
791         unsigned int mem64_mask = 0;
792         resource_size_t children_add_size = 0;
793
794         if (!b_res)
795                 return 0;
796
797         memset(aligns, 0, sizeof(aligns));
798         max_order = 0;
799         size = 0;
800
801         mem64_mask = b_res->flags & IORESOURCE_MEM_64;
802         b_res->flags &= ~IORESOURCE_MEM_64;
803
804         list_for_each_entry(dev, &bus->devices, bus_list) {
805                 int i;
806
807                 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
808                         struct resource *r = &dev->resource[i];
809                         resource_size_t r_size;
810
811                         if (r->parent || (r->flags & mask) != type)
812                                 continue;
813                         r_size = resource_size(r);
814 #ifdef CONFIG_PCI_IOV
815                         /* put SRIOV requested res to the optional list */
816                         if (realloc_head && i >= PCI_IOV_RESOURCES &&
817                                         i <= PCI_IOV_RESOURCE_END) {
818                                 r->end = r->start - 1;
819                                 add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */);
820                                 children_add_size += r_size;
821                                 continue;
822                         }
823 #endif
824                         /* For bridges size != alignment */
825                         align = pci_resource_alignment(dev, r);
826                         order = __ffs(align) - 20;
827                         if (order > 11) {
828                                 dev_warn(&dev->dev, "disabling BAR %d: %pR "
829                                          "(bad alignment %#llx)\n", i, r,
830                                          (unsigned long long) align);
831                                 r->flags = 0;
832                                 continue;
833                         }
834                         size += r_size;
835                         if (order < 0)
836                                 order = 0;
837                         /* Exclude ranges with size > align from
838                            calculation of the alignment. */
839                         if (r_size == align)
840                                 aligns[order] += align;
841                         if (order > max_order)
842                                 max_order = order;
843                         mem64_mask &= r->flags & IORESOURCE_MEM_64;
844
845                         if (realloc_head)
846                                 children_add_size += get_res_add_size(realloc_head, r);
847                 }
848         }
849         align = 0;
850         min_align = 0;
851         for (order = 0; order <= max_order; order++) {
852                 resource_size_t align1 = 1;
853
854                 align1 <<= (order + 20);
855
856                 if (!align)
857                         min_align = align1;
858                 else if (ALIGN(align + min_align, min_align) < align1)
859                         min_align = align1 >> 1;
860                 align += aligns[order];
861         }
862         size = max(size, (resource_size_t)pci_reserve_size_mem(bus));
863         size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
864         if (children_add_size > add_size)
865                 add_size = children_add_size;
866         size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
867                 calculate_memsize(size, min_size, add_size,
868                                 resource_size(b_res), min_align);
869         if (!size0 && !size1) {
870                 if (b_res->start || b_res->end)
871                         dev_info(&bus->self->dev, "disabling bridge window "
872                                  "%pR to [bus %02x-%02x] (unused)\n", b_res,
873                                  bus->secondary, bus->subordinate);
874                 b_res->flags = 0;
875                 return 1;
876         }
877         b_res->start = min_align;
878         b_res->end = size0 + min_align - 1;
879         b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
880         if (size1 > size0 && realloc_head) {
881                 add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
882                 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
883                                  "%pR to [bus %02x-%02x] add_size %llx\n", b_res,
884                                  bus->secondary, bus->subordinate, (unsigned long long)size1-size0);
885         }
886         return 1;
887 }
888
889 unsigned long pci_cardbus_resource_alignment(struct resource *res)
890 {
891         if (res->flags & IORESOURCE_IO)
892                 return pci_cardbus_io_size;
893         if (res->flags & IORESOURCE_MEM)
894                 return pci_cardbus_mem_size;
895         return 0;
896 }
897
898 static void pci_bus_size_cardbus(struct pci_bus *bus,
899                         struct list_head *realloc_head)
900 {
901         struct pci_dev *bridge = bus->self;
902         struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
903         resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
904         u16 ctrl;
905
906         if (b_res[0].parent)
907                 goto handle_b_res_1;
908         /*
909          * Reserve some resources for CardBus.  We reserve
910          * a fixed amount of bus space for CardBus bridges.
911          */
912         b_res[0].start = pci_cardbus_io_size;
913         b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
914         b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
915         if (realloc_head) {
916                 b_res[0].end -= pci_cardbus_io_size;
917                 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
918                                 pci_cardbus_io_size);
919         }
920
921 handle_b_res_1:
922         if (b_res[1].parent)
923                 goto handle_b_res_2;
924         b_res[1].start = pci_cardbus_io_size;
925         b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
926         b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
927         if (realloc_head) {
928                 b_res[1].end -= pci_cardbus_io_size;
929                 add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
930                                  pci_cardbus_io_size);
931         }
932
933 handle_b_res_2:
934         /* MEM1 must not be pref mmio */
935         pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
936         if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
937                 ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
938                 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
939                 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
940         }
941
942         /*
943          * Check whether prefetchable memory is supported
944          * by this bridge.
945          */
946         pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
947         if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
948                 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
949                 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
950                 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
951         }
952
953         if (b_res[2].parent)
954                 goto handle_b_res_3;
955         /*
956          * If we have prefetchable memory support, allocate
957          * two regions.  Otherwise, allocate one region of
958          * twice the size.
959          */
960         if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
961                 b_res[2].start = pci_cardbus_mem_size;
962                 b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
963                 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
964                                   IORESOURCE_STARTALIGN;
965                 if (realloc_head) {
966                         b_res[2].end -= pci_cardbus_mem_size;
967                         add_to_list(realloc_head, bridge, b_res+2,
968                                  pci_cardbus_mem_size, pci_cardbus_mem_size);
969                 }
970
971                 /* reduce that to half */
972                 b_res_3_size = pci_cardbus_mem_size;
973         }
974
975 handle_b_res_3:
976         if (b_res[3].parent)
977                 goto handle_done;
978         b_res[3].start = pci_cardbus_mem_size;
979         b_res[3].end = b_res[3].start + b_res_3_size - 1;
980         b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
981         if (realloc_head) {
982                 b_res[3].end -= b_res_3_size;
983                 add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
984                                  pci_cardbus_mem_size);
985         }
986
987 handle_done:
988         ;
989 }
990
991 void __ref __pci_bus_size_bridges(struct pci_bus *bus,
992                         struct list_head *realloc_head)
993 {
994         struct pci_dev *dev;
995         unsigned long mask, prefmask;
996         resource_size_t additional_mem_size = 0, additional_io_size = 0;
997
998         list_for_each_entry(dev, &bus->devices, bus_list) {
999                 struct pci_bus *b = dev->subordinate;
1000                 if (!b)
1001                         continue;
1002
1003                 switch (dev->class >> 8) {
1004                 case PCI_CLASS_BRIDGE_CARDBUS:
1005                         pci_bus_size_cardbus(b, realloc_head);
1006                         break;
1007
1008                 case PCI_CLASS_BRIDGE_PCI:
1009                 default:
1010                         __pci_bus_size_bridges(b, realloc_head);
1011                         break;
1012                 }
1013         }
1014
1015         /* The root bus? */
1016         if (!bus->self)
1017                 return;
1018
1019         switch (bus->self->class >> 8) {
1020         case PCI_CLASS_BRIDGE_CARDBUS:
1021                 /* don't size cardbuses yet. */
1022                 break;
1023
1024         case PCI_CLASS_BRIDGE_PCI:
1025                 pci_bridge_check_ranges(bus);
1026                 if (bus->self->is_hotplug_bridge) {
1027                         additional_io_size  = pci_hotplug_io_size;
1028                         additional_mem_size = pci_hotplug_mem_size;
1029                 }
1030                 /*
1031                  * Follow thru
1032                  */
1033         default:
1034                 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
1035                              additional_io_size, realloc_head);
1036                 /* If the bridge supports prefetchable range, size it
1037                    separately. If it doesn't, or its prefetchable window
1038                    has already been allocated by arch code, try
1039                    non-prefetchable range for both types of PCI memory
1040                    resources. */
1041                 mask = IORESOURCE_MEM;
1042                 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
1043                 if (pbus_size_mem(bus, prefmask, prefmask,
1044                                   realloc_head ? 0 : additional_mem_size,
1045                                   additional_mem_size, realloc_head))
1046                         mask = prefmask; /* Success, size non-prefetch only. */
1047                 else
1048                         additional_mem_size += additional_mem_size;
1049                 pbus_size_mem(bus, mask, IORESOURCE_MEM,
1050                                 realloc_head ? 0 : additional_mem_size,
1051                                 additional_mem_size, realloc_head);
1052                 break;
1053         }
1054 }
1055
1056 void __ref pci_bus_size_bridges(struct pci_bus *bus)
1057 {
1058         __pci_bus_size_bridges(bus, NULL);
1059 }
1060 EXPORT_SYMBOL(pci_bus_size_bridges);
1061
1062 static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
1063                                          struct list_head *realloc_head,
1064                                          struct list_head *fail_head)
1065 {
1066         struct pci_bus *b;
1067         struct pci_dev *dev;
1068
1069         pbus_assign_resources_sorted(bus, realloc_head, fail_head);
1070
1071         list_for_each_entry(dev, &bus->devices, bus_list) {
1072                 b = dev->subordinate;
1073                 if (!b)
1074                         continue;
1075
1076                 __pci_bus_assign_resources(b, realloc_head, fail_head);
1077
1078                 switch (dev->class >> 8) {
1079                 case PCI_CLASS_BRIDGE_PCI:
1080                         if (!pci_is_enabled(dev))
1081                                 pci_setup_bridge(b);
1082                         break;
1083
1084                 case PCI_CLASS_BRIDGE_CARDBUS:
1085                         pci_setup_cardbus(b);
1086                         break;
1087
1088                 default:
1089                         dev_info(&dev->dev, "not setting up bridge for bus "
1090                                  "%04x:%02x\n", pci_domain_nr(b), b->number);
1091                         break;
1092                 }
1093         }
1094 }
1095
1096 void __ref pci_bus_assign_resources(const struct pci_bus *bus)
1097 {
1098         __pci_bus_assign_resources(bus, NULL, NULL);
1099 }
1100 EXPORT_SYMBOL(pci_bus_assign_resources);
1101
1102 static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
1103                                          struct list_head *add_head,
1104                                          struct list_head *fail_head)
1105 {
1106         struct pci_bus *b;
1107
1108         pdev_assign_resources_sorted((struct pci_dev *)bridge,
1109                                          add_head, fail_head);
1110
1111         b = bridge->subordinate;
1112         if (!b)
1113                 return;
1114
1115         __pci_bus_assign_resources(b, add_head, fail_head);
1116
1117         switch (bridge->class >> 8) {
1118         case PCI_CLASS_BRIDGE_PCI:
1119                 pci_setup_bridge(b);
1120                 break;
1121
1122         case PCI_CLASS_BRIDGE_CARDBUS:
1123                 pci_setup_cardbus(b);
1124                 break;
1125
1126         default:
1127                 dev_info(&bridge->dev, "not setting up bridge for bus "
1128                          "%04x:%02x\n", pci_domain_nr(b), b->number);
1129                 break;
1130         }
1131 }
1132 static void pci_bridge_release_resources(struct pci_bus *bus,
1133                                           unsigned long type)
1134 {
1135         int idx;
1136         bool changed = false;
1137         struct pci_dev *dev;
1138         struct resource *r;
1139         unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1140                                   IORESOURCE_PREFETCH;
1141
1142         dev = bus->self;
1143         for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
1144              idx++) {
1145                 r = &dev->resource[idx];
1146                 if ((r->flags & type_mask) != type)
1147                         continue;
1148                 if (!r->parent)
1149                         continue;
1150                 /*
1151                  * if there are children under that, we should release them
1152                  *  all
1153                  */
1154                 release_child_resources(r);
1155                 if (!release_resource(r)) {
1156                         dev_printk(KERN_DEBUG, &dev->dev,
1157                                  "resource %d %pR released\n", idx, r);
1158                         /* keep the old size */
1159                         r->end = resource_size(r) - 1;
1160                         r->start = 0;
1161                         r->flags = 0;
1162                         changed = true;
1163                 }
1164         }
1165
1166         if (changed) {
1167                 /* avoiding touch the one without PREF */
1168                 if (type & IORESOURCE_PREFETCH)
1169                         type = IORESOURCE_PREFETCH;
1170                 __pci_setup_bridge(bus, type);
1171         }
1172 }
1173
1174 enum release_type {
1175         leaf_only,
1176         whole_subtree,
1177 };
1178 /*
1179  * try to release pci bridge resources that is from leaf bridge,
1180  * so we can allocate big new one later
1181  */
1182 static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
1183                                                    unsigned long type,
1184                                                    enum release_type rel_type)
1185 {
1186         struct pci_dev *dev;
1187         bool is_leaf_bridge = true;
1188
1189         list_for_each_entry(dev, &bus->devices, bus_list) {
1190                 struct pci_bus *b = dev->subordinate;
1191                 if (!b)
1192                         continue;
1193
1194                 is_leaf_bridge = false;
1195
1196                 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1197                         continue;
1198
1199                 if (rel_type == whole_subtree)
1200                         pci_bus_release_bridge_resources(b, type,
1201                                                  whole_subtree);
1202         }
1203
1204         if (pci_is_root_bus(bus))
1205                 return;
1206
1207         if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1208                 return;
1209
1210         if ((rel_type == whole_subtree) || is_leaf_bridge)
1211                 pci_bridge_release_resources(bus, type);
1212 }
1213
1214 static void pci_bus_dump_res(struct pci_bus *bus)
1215 {
1216         struct resource *res;
1217         int i;
1218
1219         pci_bus_for_each_resource(bus, res, i) {
1220                 if (!res || !res->end || !res->flags)
1221                         continue;
1222
1223                 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
1224         }
1225 }
1226
1227 static void pci_bus_dump_resources(struct pci_bus *bus)
1228 {
1229         struct pci_bus *b;
1230         struct pci_dev *dev;
1231
1232
1233         pci_bus_dump_res(bus);
1234
1235         list_for_each_entry(dev, &bus->devices, bus_list) {
1236                 b = dev->subordinate;
1237                 if (!b)
1238                         continue;
1239
1240                 pci_bus_dump_resources(b);
1241         }
1242 }
1243
1244 static int __init pci_bus_get_depth(struct pci_bus *bus)
1245 {
1246         int depth = 0;
1247         struct pci_dev *dev;
1248
1249         list_for_each_entry(dev, &bus->devices, bus_list) {
1250                 int ret;
1251                 struct pci_bus *b = dev->subordinate;
1252                 if (!b)
1253                         continue;
1254
1255                 ret = pci_bus_get_depth(b);
1256                 if (ret + 1 > depth)
1257                         depth = ret + 1;
1258         }
1259
1260         return depth;
1261 }
1262 static int __init pci_get_max_depth(void)
1263 {
1264         int depth = 0;
1265         struct pci_bus *bus;
1266
1267         list_for_each_entry(bus, &pci_root_buses, node) {
1268                 int ret;
1269
1270                 ret = pci_bus_get_depth(bus);
1271                 if (ret > depth)
1272                         depth = ret;
1273         }
1274
1275         return depth;
1276 }
1277
1278 /*
1279  * -1: undefined, will auto detect later
1280  *  0: disabled by user
1281  *  1: disabled by auto detect
1282  *  2: enabled by user
1283  *  3: enabled by auto detect
1284  */
1285 enum enable_type {
1286         undefined = -1,
1287         user_disabled,
1288         auto_disabled,
1289         user_enabled,
1290         auto_enabled,
1291 };
1292
1293 static enum enable_type pci_realloc_enable __initdata = undefined;
1294 void __init pci_realloc_get_opt(char *str)
1295 {
1296         if (!strncmp(str, "off", 3))
1297                 pci_realloc_enable = user_disabled;
1298         else if (!strncmp(str, "on", 2))
1299                 pci_realloc_enable = user_enabled;
1300 }
1301 static bool __init pci_realloc_enabled(void)
1302 {
1303         return pci_realloc_enable >= user_enabled;
1304 }
1305
1306 static void __init pci_realloc_detect(void)
1307 {
1308 #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1309         struct pci_dev *dev = NULL;
1310
1311         if (pci_realloc_enable != undefined)
1312                 return;
1313
1314         for_each_pci_dev(dev) {
1315                 int i;
1316
1317                 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) {
1318                         struct resource *r = &dev->resource[i];
1319
1320                         /* Not assigned, or rejected by kernel ? */
1321                         if (r->flags && !r->start) {
1322                                 pci_realloc_enable = auto_enabled;
1323
1324                                 return;
1325                         }
1326                 }
1327         }
1328 #endif
1329 }
1330
1331 /*
1332  * first try will not touch pci bridge res
1333  * second  and later try will clear small leaf bridge res
1334  * will stop till to the max  deepth if can not find good one
1335  */
1336 void __init
1337 pci_assign_unassigned_resources(void)
1338 {
1339         struct pci_bus *bus;
1340         LIST_HEAD(realloc_head); /* list of resources that
1341                                         want additional resources */
1342         struct list_head *add_list = NULL;
1343         int tried_times = 0;
1344         enum release_type rel_type = leaf_only;
1345         LIST_HEAD(fail_head);
1346         struct pci_dev_resource *fail_res;
1347         unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1348                                   IORESOURCE_PREFETCH;
1349         int pci_try_num = 1;
1350
1351         /* don't realloc if asked to do so */
1352         pci_realloc_detect();
1353         if (pci_realloc_enabled()) {
1354                 int max_depth = pci_get_max_depth();
1355
1356                 pci_try_num = max_depth + 1;
1357                 printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1358                          max_depth, pci_try_num);
1359         }
1360
1361 again:
1362         /*
1363          * last try will use add_list, otherwise will try good to have as
1364          * must have, so can realloc parent bridge resource
1365          */
1366         if (tried_times + 1 == pci_try_num)
1367                 add_list = &realloc_head;
1368         /* Depth first, calculate sizes and alignments of all
1369            subordinate buses. */
1370         list_for_each_entry(bus, &pci_root_buses, node)
1371                 __pci_bus_size_bridges(bus, add_list);
1372
1373         /* Depth last, allocate resources and update the hardware. */
1374         list_for_each_entry(bus, &pci_root_buses, node)
1375                 __pci_bus_assign_resources(bus, add_list, &fail_head);
1376         if (add_list)
1377                 BUG_ON(!list_empty(add_list));
1378         tried_times++;
1379
1380         /* any device complain? */
1381         if (list_empty(&fail_head))
1382                 goto enable_and_dump;
1383
1384         if (tried_times >= pci_try_num) {
1385                 if (pci_realloc_enable == undefined)
1386                         printk(KERN_INFO "Some PCI device resources are unassigned, try booting with pci=realloc\n");
1387                 else if (pci_realloc_enable == auto_enabled)
1388                         printk(KERN_INFO "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
1389
1390                 free_list(&fail_head);
1391                 goto enable_and_dump;
1392         }
1393
1394         printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1395                          tried_times + 1);
1396
1397         /* third times and later will not check if it is leaf */
1398         if ((tried_times + 1) > 2)
1399                 rel_type = whole_subtree;
1400
1401         /*
1402          * Try to release leaf bridge's resources that doesn't fit resource of
1403          * child device under that bridge
1404          */
1405         list_for_each_entry(fail_res, &fail_head, list) {
1406                 bus = fail_res->dev->bus;
1407                 pci_bus_release_bridge_resources(bus,
1408                                                  fail_res->flags & type_mask,
1409                                                  rel_type);
1410         }
1411         /* restore size and flags */
1412         list_for_each_entry(fail_res, &fail_head, list) {
1413                 struct resource *res = fail_res->res;
1414
1415                 res->start = fail_res->start;
1416                 res->end = fail_res->end;
1417                 res->flags = fail_res->flags;
1418                 if (fail_res->dev->subordinate)
1419                         res->flags = 0;
1420         }
1421         free_list(&fail_head);
1422
1423         goto again;
1424
1425 enable_and_dump:
1426         /* Depth last, update the hardware. */
1427         list_for_each_entry(bus, &pci_root_buses, node)
1428                 pci_enable_bridges(bus);
1429
1430         /* dump the resource on buses */
1431         list_for_each_entry(bus, &pci_root_buses, node)
1432                 pci_bus_dump_resources(bus);
1433 }
1434
1435 void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
1436 {
1437         struct pci_bus *parent = bridge->subordinate;
1438         LIST_HEAD(add_list); /* list of resources that
1439                                         want additional resources */
1440         int tried_times = 0;
1441         LIST_HEAD(fail_head);
1442         struct pci_dev_resource *fail_res;
1443         int retval;
1444         unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1445                                   IORESOURCE_PREFETCH;
1446
1447 again:
1448         __pci_bus_size_bridges(parent, &add_list);
1449         __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1450         BUG_ON(!list_empty(&add_list));
1451         tried_times++;
1452
1453         if (list_empty(&fail_head))
1454                 goto enable_all;
1455
1456         if (tried_times >= 2) {
1457                 /* still fail, don't need to try more */
1458                 free_list(&fail_head);
1459                 goto enable_all;
1460         }
1461
1462         printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1463                          tried_times + 1);
1464
1465         /*
1466          * Try to release leaf bridge's resources that doesn't fit resource of
1467          * child device under that bridge
1468          */
1469         list_for_each_entry(fail_res, &fail_head, list) {
1470                 struct pci_bus *bus = fail_res->dev->bus;
1471                 unsigned long flags = fail_res->flags;
1472
1473                 pci_bus_release_bridge_resources(bus, flags & type_mask,
1474                                                  whole_subtree);
1475         }
1476         /* restore size and flags */
1477         list_for_each_entry(fail_res, &fail_head, list) {
1478                 struct resource *res = fail_res->res;
1479
1480                 res->start = fail_res->start;
1481                 res->end = fail_res->end;
1482                 res->flags = fail_res->flags;
1483                 if (fail_res->dev->subordinate)
1484                         res->flags = 0;
1485         }
1486         free_list(&fail_head);
1487
1488         goto again;
1489
1490 enable_all:
1491         retval = pci_reenable_device(bridge);
1492         pci_set_master(bridge);
1493         pci_enable_bridges(parent);
1494 }
1495 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
1496
1497 #ifdef CONFIG_HOTPLUG
1498 /**
1499  * pci_rescan_bus - scan a PCI bus for devices.
1500  * @bus: PCI bus to scan
1501  *
1502  * Scan a PCI bus and child buses for new devices, adds them,
1503  * and enables them.
1504  *
1505  * Returns the max number of subordinate bus discovered.
1506  */
1507 unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
1508 {
1509         unsigned int max;
1510         struct pci_dev *dev;
1511         LIST_HEAD(add_list); /* list of resources that
1512                                         want additional resources */
1513
1514         max = pci_scan_child_bus(bus);
1515
1516         down_read(&pci_bus_sem);
1517         list_for_each_entry(dev, &bus->devices, bus_list)
1518                 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1519                     dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1520                         if (dev->subordinate)
1521                                 __pci_bus_size_bridges(dev->subordinate,
1522                                                          &add_list);
1523         up_read(&pci_bus_sem);
1524         __pci_bus_assign_resources(bus, &add_list, NULL);
1525         BUG_ON(!list_empty(&add_list));
1526
1527         pci_enable_bridges(bus);
1528         pci_bus_add_devices(bus);
1529
1530         return max;
1531 }
1532 EXPORT_SYMBOL_GPL(pci_rescan_bus);
1533 #endif