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