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