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