xref: /openbmc/linux/drivers/pci/setup-bus.c (revision 9db0b9b6a14249ef65a5f1e5e3b37762af96f425)
17328c8f4SBjorn Helgaas // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
3df62ab5eSBjorn Helgaas  * Support routines for initializing a PCI subsystem
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Extruded from code written by
61da177e4SLinus Torvalds  *      Dave Rusling (david.rusling@reo.mts.dec.com)
71da177e4SLinus Torvalds  *      David Mosberger (davidm@cs.arizona.edu)
81da177e4SLinus Torvalds  *	David Miller (davem@redhat.com)
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
111da177e4SLinus Torvalds  *	     PCI-PCI bridges cleanup, sorted resource allocation.
121da177e4SLinus Torvalds  * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
131da177e4SLinus Torvalds  *	     Converted to allocation in 3 passes, which gives
141da177e4SLinus Torvalds  *	     tighter packing. Prefetchable range support.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/kernel.h>
191da177e4SLinus Torvalds #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/pci.h>
211da177e4SLinus Torvalds #include <linux/errno.h>
221da177e4SLinus Torvalds #include <linux/ioport.h>
231da177e4SLinus Torvalds #include <linux/cache.h>
241da177e4SLinus Torvalds #include <linux/slab.h>
25584c5c42SRui Wang #include <linux/acpi.h>
266faf17f6SChris Wright #include "pci.h"
271da177e4SLinus Torvalds 
28844393f4SBjorn Helgaas unsigned int pci_flags;
290c59c06aSRob Herring EXPORT_SYMBOL_GPL(pci_flags);
3047087700SBjorn Helgaas 
31bdc4abecSYinghai Lu struct pci_dev_resource {
32bdc4abecSYinghai Lu 	struct list_head list;
332934a0deSYinghai Lu 	struct resource *res;
342934a0deSYinghai Lu 	struct pci_dev *dev;
35568ddef8SYinghai Lu 	resource_size_t start;
36568ddef8SYinghai Lu 	resource_size_t end;
37c8adf9a3SRam Pai 	resource_size_t add_size;
382bbc6942SRam Pai 	resource_size_t min_align;
39568ddef8SYinghai Lu 	unsigned long flags;
40568ddef8SYinghai Lu };
41568ddef8SYinghai Lu 
42bffc56d4SYinghai Lu static void free_list(struct list_head *head)
43bffc56d4SYinghai Lu {
44bffc56d4SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
45bffc56d4SYinghai Lu 
46bffc56d4SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
47bffc56d4SYinghai Lu 		list_del(&dev_res->list);
48bffc56d4SYinghai Lu 		kfree(dev_res);
49bffc56d4SYinghai Lu 	}
50bffc56d4SYinghai Lu }
51094732a5SRam Pai 
52c8adf9a3SRam Pai /**
530d607618SNicholas Johnson  * add_to_list() - Add a new resource tracker to the list
54c8adf9a3SRam Pai  * @head:	Head of the list
550d607618SNicholas Johnson  * @dev:	Device to which the resource belongs
560d607618SNicholas Johnson  * @res:	Resource to be tracked
570d607618SNicholas Johnson  * @add_size:	Additional size to be optionally added to the resource
589b41d19aSKrzysztof Kozlowski  * @min_align:	Minimum memory window alignment
59c8adf9a3SRam Pai  */
600d607618SNicholas Johnson static int add_to_list(struct list_head *head, struct pci_dev *dev,
610d607618SNicholas Johnson 		       struct resource *res, resource_size_t add_size,
620d607618SNicholas Johnson 		       resource_size_t min_align)
63568ddef8SYinghai Lu {
64764242a0SYinghai Lu 	struct pci_dev_resource *tmp;
65568ddef8SYinghai Lu 
66bdc4abecSYinghai Lu 	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
67c7abb235SMarkus Elfring 	if (!tmp)
68ef62dfefSYinghai Lu 		return -ENOMEM;
69568ddef8SYinghai Lu 
70568ddef8SYinghai Lu 	tmp->res = res;
71568ddef8SYinghai Lu 	tmp->dev = dev;
72568ddef8SYinghai Lu 	tmp->start = res->start;
73568ddef8SYinghai Lu 	tmp->end = res->end;
74568ddef8SYinghai Lu 	tmp->flags = res->flags;
75c8adf9a3SRam Pai 	tmp->add_size = add_size;
762bbc6942SRam Pai 	tmp->min_align = min_align;
77bdc4abecSYinghai Lu 
78bdc4abecSYinghai Lu 	list_add(&tmp->list, head);
79ef62dfefSYinghai Lu 
80ef62dfefSYinghai Lu 	return 0;
81568ddef8SYinghai Lu }
82568ddef8SYinghai Lu 
830d607618SNicholas Johnson static void remove_from_list(struct list_head *head, struct resource *res)
843e6e0d80SYinghai Lu {
85b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
863e6e0d80SYinghai Lu 
87b9b0bba9SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
88b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
89b9b0bba9SYinghai Lu 			list_del(&dev_res->list);
90b9b0bba9SYinghai Lu 			kfree(dev_res);
91bdc4abecSYinghai Lu 			break;
923e6e0d80SYinghai Lu 		}
933e6e0d80SYinghai Lu 	}
943e6e0d80SYinghai Lu }
953e6e0d80SYinghai Lu 
96d74b9027SWei Yang static struct pci_dev_resource *res_to_dev_res(struct list_head *head,
971c372353SYinghai Lu 					       struct resource *res)
981c372353SYinghai Lu {
99b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res;
1001c372353SYinghai Lu 
101b9b0bba9SYinghai Lu 	list_for_each_entry(dev_res, head, list) {
10225e77388SBjorn Helgaas 		if (dev_res->res == res)
103d74b9027SWei Yang 			return dev_res;
104bdc4abecSYinghai Lu 	}
1051c372353SYinghai Lu 
106d74b9027SWei Yang 	return NULL;
1071c372353SYinghai Lu }
1081c372353SYinghai Lu 
109d74b9027SWei Yang static resource_size_t get_res_add_size(struct list_head *head,
110d74b9027SWei Yang 					struct resource *res)
111d74b9027SWei Yang {
112d74b9027SWei Yang 	struct pci_dev_resource *dev_res;
113d74b9027SWei Yang 
114d74b9027SWei Yang 	dev_res = res_to_dev_res(head, res);
115d74b9027SWei Yang 	return dev_res ? dev_res->add_size : 0;
116d74b9027SWei Yang }
117d74b9027SWei Yang 
118d74b9027SWei Yang static resource_size_t get_res_add_align(struct list_head *head,
119d74b9027SWei Yang 					 struct resource *res)
120d74b9027SWei Yang {
121d74b9027SWei Yang 	struct pci_dev_resource *dev_res;
122d74b9027SWei Yang 
123d74b9027SWei Yang 	dev_res = res_to_dev_res(head, res);
124d74b9027SWei Yang 	return dev_res ? dev_res->min_align : 0;
125d74b9027SWei Yang }
126d74b9027SWei Yang 
127d74b9027SWei Yang 
12878c3b329SYinghai Lu /* Sort resources by alignment */
129bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
13078c3b329SYinghai Lu {
13178c3b329SYinghai Lu 	int i;
13278c3b329SYinghai Lu 
13378c3b329SYinghai Lu 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
13478c3b329SYinghai Lu 		struct resource *r;
135bdc4abecSYinghai Lu 		struct pci_dev_resource *dev_res, *tmp;
13678c3b329SYinghai Lu 		resource_size_t r_align;
137bdc4abecSYinghai Lu 		struct list_head *n;
13878c3b329SYinghai Lu 
13978c3b329SYinghai Lu 		r = &dev->resource[i];
14078c3b329SYinghai Lu 
14178c3b329SYinghai Lu 		if (r->flags & IORESOURCE_PCI_FIXED)
14278c3b329SYinghai Lu 			continue;
14378c3b329SYinghai Lu 
14478c3b329SYinghai Lu 		if (!(r->flags) || r->parent)
14578c3b329SYinghai Lu 			continue;
14678c3b329SYinghai Lu 
14778c3b329SYinghai Lu 		r_align = pci_resource_alignment(dev, r);
14878c3b329SYinghai Lu 		if (!r_align) {
1497506dc79SFrederick Lawler 			pci_warn(dev, "BAR %d: %pR has bogus alignment\n",
15078c3b329SYinghai Lu 				 i, r);
15178c3b329SYinghai Lu 			continue;
15278c3b329SYinghai Lu 		}
15378c3b329SYinghai Lu 
154bdc4abecSYinghai Lu 		tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
15578c3b329SYinghai Lu 		if (!tmp)
156c7c337c5SLiao Pingfang 			panic("%s: kzalloc() failed!\n", __func__);
15778c3b329SYinghai Lu 		tmp->res = r;
15878c3b329SYinghai Lu 		tmp->dev = dev;
159bdc4abecSYinghai Lu 
1600d607618SNicholas Johnson 		/* Fallback is smallest one or list is empty */
161bdc4abecSYinghai Lu 		n = head;
162bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
163bdc4abecSYinghai Lu 			resource_size_t align;
164bdc4abecSYinghai Lu 
165bdc4abecSYinghai Lu 			align = pci_resource_alignment(dev_res->dev,
166bdc4abecSYinghai Lu 							 dev_res->res);
167bdc4abecSYinghai Lu 
168bdc4abecSYinghai Lu 			if (r_align > align) {
169bdc4abecSYinghai Lu 				n = &dev_res->list;
17078c3b329SYinghai Lu 				break;
17178c3b329SYinghai Lu 			}
17278c3b329SYinghai Lu 		}
173bdc4abecSYinghai Lu 		/* Insert it just before n */
174bdc4abecSYinghai Lu 		list_add_tail(&tmp->list, n);
17578c3b329SYinghai Lu 	}
17678c3b329SYinghai Lu }
17778c3b329SYinghai Lu 
1780d607618SNicholas Johnson static void __dev_sort_resources(struct pci_dev *dev, struct list_head *head)
1791da177e4SLinus Torvalds {
1801da177e4SLinus Torvalds 	u16 class = dev->class >> 8;
1811da177e4SLinus Torvalds 
1820d607618SNicholas Johnson 	/* Don't touch classless devices or host bridges or IOAPICs */
1836841ec68SYinghai Lu 	if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
1846841ec68SYinghai Lu 		return;
1851da177e4SLinus Torvalds 
1860d607618SNicholas Johnson 	/* Don't touch IOAPIC devices already enabled by firmware */
18723186279SSatoru Takeuchi 	if (class == PCI_CLASS_SYSTEM_PIC) {
1889bded00bSKenji Kaneshige 		u16 command;
1899bded00bSKenji Kaneshige 		pci_read_config_word(dev, PCI_COMMAND, &command);
1909bded00bSKenji Kaneshige 		if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
1916841ec68SYinghai Lu 			return;
19223186279SSatoru Takeuchi 	}
19323186279SSatoru Takeuchi 
1946841ec68SYinghai Lu 	pdev_sort_resources(dev, head);
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
197fc075e1dSRam Pai static inline void reset_resource(struct resource *res)
198fc075e1dSRam Pai {
199fc075e1dSRam Pai 	res->start = 0;
200fc075e1dSRam Pai 	res->end = 0;
201fc075e1dSRam Pai 	res->flags = 0;
202fc075e1dSRam Pai }
203fc075e1dSRam Pai 
204c8adf9a3SRam Pai /**
2050d607618SNicholas Johnson  * reassign_resources_sorted() - Satisfy any additional resource requests
206c8adf9a3SRam Pai  *
2070d607618SNicholas Johnson  * @realloc_head:	Head of the list tracking requests requiring
2080d607618SNicholas Johnson  *			additional resources
2090d607618SNicholas Johnson  * @head:		Head of the list tracking requests with allocated
210c8adf9a3SRam Pai  *			resources
211c8adf9a3SRam Pai  *
2120d607618SNicholas Johnson  * Walk through each element of the realloc_head and try to procure additional
2130d607618SNicholas Johnson  * resources for the element, provided the element is in the head list.
214c8adf9a3SRam Pai  */
215bdc4abecSYinghai Lu static void reassign_resources_sorted(struct list_head *realloc_head,
216bdc4abecSYinghai Lu 				      struct list_head *head)
217c8adf9a3SRam Pai {
218c8adf9a3SRam Pai 	struct resource *res;
219b9b0bba9SYinghai Lu 	struct pci_dev_resource *add_res, *tmp;
220bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
221d74b9027SWei Yang 	resource_size_t add_size, align;
222c8adf9a3SRam Pai 	int idx;
223c8adf9a3SRam Pai 
224b9b0bba9SYinghai Lu 	list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
225bdc4abecSYinghai Lu 		bool found_match = false;
226bdc4abecSYinghai Lu 
227b9b0bba9SYinghai Lu 		res = add_res->res;
2280d607618SNicholas Johnson 		/* Skip resource that has been reset */
229c8adf9a3SRam Pai 		if (!res->flags)
230c8adf9a3SRam Pai 			goto out;
231c8adf9a3SRam Pai 
2320d607618SNicholas Johnson 		/* Skip this resource if not found in head list */
233bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
234bdc4abecSYinghai Lu 			if (dev_res->res == res) {
235bdc4abecSYinghai Lu 				found_match = true;
236bdc4abecSYinghai Lu 				break;
237c8adf9a3SRam Pai 			}
238bdc4abecSYinghai Lu 		}
2390d607618SNicholas Johnson 		if (!found_match) /* Just skip */
240bdc4abecSYinghai Lu 			continue;
241c8adf9a3SRam Pai 
242b9b0bba9SYinghai Lu 		idx = res - &add_res->dev->resource[0];
243b9b0bba9SYinghai Lu 		add_size = add_res->add_size;
244d74b9027SWei Yang 		align = add_res->min_align;
2452bbc6942SRam Pai 		if (!resource_size(res)) {
246d74b9027SWei Yang 			res->start = align;
247c8adf9a3SRam Pai 			res->end = res->start + add_size - 1;
248b9b0bba9SYinghai Lu 			if (pci_assign_resource(add_res->dev, idx))
249c8adf9a3SRam Pai 				reset_resource(res);
2502bbc6942SRam Pai 		} else {
251b9b0bba9SYinghai Lu 			res->flags |= add_res->flags &
252bdc4abecSYinghai Lu 				 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
253b9b0bba9SYinghai Lu 			if (pci_reassign_resource(add_res->dev, idx,
254bdc4abecSYinghai Lu 						  add_size, align))
25534c6b710SMohan Kumar 				pci_info(add_res->dev, "failed to add %llx res[%d]=%pR\n",
25634c6b710SMohan Kumar 					 (unsigned long long) add_size, idx,
25734c6b710SMohan Kumar 					 res);
258c8adf9a3SRam Pai 		}
259c8adf9a3SRam Pai out:
260b9b0bba9SYinghai Lu 		list_del(&add_res->list);
261b9b0bba9SYinghai Lu 		kfree(add_res);
262c8adf9a3SRam Pai 	}
263c8adf9a3SRam Pai }
264c8adf9a3SRam Pai 
265c8adf9a3SRam Pai /**
2660d607618SNicholas Johnson  * assign_requested_resources_sorted() - Satisfy resource requests
267c8adf9a3SRam Pai  *
2680d607618SNicholas Johnson  * @head:	Head of the list tracking requests for resources
2690d607618SNicholas Johnson  * @fail_head:	Head of the list tracking requests that could not be
2700d607618SNicholas Johnson  *		allocated
271c8adf9a3SRam Pai  *
2720d607618SNicholas Johnson  * Satisfy resource requests of each element in the list.  Add requests that
2730d607618SNicholas Johnson  * could not be satisfied to the failed_list.
274c8adf9a3SRam Pai  */
275bdc4abecSYinghai Lu static void assign_requested_resources_sorted(struct list_head *head,
276bdc4abecSYinghai Lu 				 struct list_head *fail_head)
2776841ec68SYinghai Lu {
2786841ec68SYinghai Lu 	struct resource *res;
279bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
2806841ec68SYinghai Lu 	int idx;
2816841ec68SYinghai Lu 
282bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
283bdc4abecSYinghai Lu 		res = dev_res->res;
284bdc4abecSYinghai Lu 		idx = res - &dev_res->dev->resource[0];
285bdc4abecSYinghai Lu 		if (resource_size(res) &&
286bdc4abecSYinghai Lu 		    pci_assign_resource(dev_res->dev, idx)) {
287a3cb999dSYinghai Lu 			if (fail_head) {
2889a928660SYinghai Lu 				/*
2890d607618SNicholas Johnson 				 * If the failed resource is a ROM BAR and
2900d607618SNicholas Johnson 				 * it will be enabled later, don't add it
2910d607618SNicholas Johnson 				 * to the list.
2929a928660SYinghai Lu 				 */
2939a928660SYinghai Lu 				if (!((idx == PCI_ROM_RESOURCE) &&
2949a928660SYinghai Lu 				      (!(res->flags & IORESOURCE_ROM_ENABLE))))
29567cc7e26SYinghai Lu 					add_to_list(fail_head,
29667cc7e26SYinghai Lu 						    dev_res->dev, res,
297f7625980SBjorn Helgaas 						    0 /* don't care */,
298f7625980SBjorn Helgaas 						    0 /* don't care */);
2999a928660SYinghai Lu 			}
300fc075e1dSRam Pai 			reset_resource(res);
301542df5deSRajesh Shah 		}
3021da177e4SLinus Torvalds 	}
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
305aa914f5eSYinghai Lu static unsigned long pci_fail_res_type_mask(struct list_head *fail_head)
306aa914f5eSYinghai Lu {
307aa914f5eSYinghai Lu 	struct pci_dev_resource *fail_res;
308aa914f5eSYinghai Lu 	unsigned long mask = 0;
309aa914f5eSYinghai Lu 
3100d607618SNicholas Johnson 	/* Check failed type */
311aa914f5eSYinghai Lu 	list_for_each_entry(fail_res, fail_head, list)
312aa914f5eSYinghai Lu 		mask |= fail_res->flags;
313aa914f5eSYinghai Lu 
314aa914f5eSYinghai Lu 	/*
3150d607618SNicholas Johnson 	 * One pref failed resource will set IORESOURCE_MEM, as we can
3160d607618SNicholas Johnson 	 * allocate pref in non-pref range.  Will release all assigned
3170d607618SNicholas Johnson 	 * non-pref sibling resources according to that bit.
318aa914f5eSYinghai Lu 	 */
319aa914f5eSYinghai Lu 	return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
320aa914f5eSYinghai Lu }
321aa914f5eSYinghai Lu 
322aa914f5eSYinghai Lu static bool pci_need_to_release(unsigned long mask, struct resource *res)
323aa914f5eSYinghai Lu {
324aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_IO)
325aa914f5eSYinghai Lu 		return !!(mask & IORESOURCE_IO);
326aa914f5eSYinghai Lu 
3270d607618SNicholas Johnson 	/* Check pref at first */
328aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_PREFETCH) {
329aa914f5eSYinghai Lu 		if (mask & IORESOURCE_PREFETCH)
330aa914f5eSYinghai Lu 			return true;
3310d607618SNicholas Johnson 		/* Count pref if its parent is non-pref */
332aa914f5eSYinghai Lu 		else if ((mask & IORESOURCE_MEM) &&
333aa914f5eSYinghai Lu 			 !(res->parent->flags & IORESOURCE_PREFETCH))
334aa914f5eSYinghai Lu 			return true;
335aa914f5eSYinghai Lu 		else
336aa914f5eSYinghai Lu 			return false;
337aa914f5eSYinghai Lu 	}
338aa914f5eSYinghai Lu 
339aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_MEM)
340aa914f5eSYinghai Lu 		return !!(mask & IORESOURCE_MEM);
341aa914f5eSYinghai Lu 
3420d607618SNicholas Johnson 	return false;	/* Should not get here */
343aa914f5eSYinghai Lu }
344aa914f5eSYinghai Lu 
345bdc4abecSYinghai Lu static void __assign_resources_sorted(struct list_head *head,
346bdc4abecSYinghai Lu 				      struct list_head *realloc_head,
347bdc4abecSYinghai Lu 				      struct list_head *fail_head)
348c8adf9a3SRam Pai {
3493e6e0d80SYinghai Lu 	/*
3500d607618SNicholas Johnson 	 * Should not assign requested resources at first.  They could be
3510d607618SNicholas Johnson 	 * adjacent, so later reassign can not reallocate them one by one in
3520d607618SNicholas Johnson 	 * parent resource window.
3530d607618SNicholas Johnson 	 *
3540d607618SNicholas Johnson 	 * Try to assign requested + add_size at beginning.  If could do that,
3550d607618SNicholas Johnson 	 * could get out early.  If could not do that, we still try to assign
3560d607618SNicholas Johnson 	 * requested at first, then try to reassign add_size for some resources.
357aa914f5eSYinghai Lu 	 *
358aa914f5eSYinghai Lu 	 * Separate three resource type checking if we need to release
359aa914f5eSYinghai Lu 	 * assigned resource after requested + add_size try.
3600d607618SNicholas Johnson 	 *
3610d607618SNicholas Johnson 	 *	1. If IO port assignment fails, will release assigned IO
3620d607618SNicholas Johnson 	 *	   port.
3630d607618SNicholas Johnson 	 *	2. If pref MMIO assignment fails, release assigned pref
3640d607618SNicholas Johnson 	 *	   MMIO.  If assigned pref MMIO's parent is non-pref MMIO
3650d607618SNicholas Johnson 	 *	   and non-pref MMIO assignment fails, will release that
3660d607618SNicholas Johnson 	 *	   assigned pref MMIO.
3670d607618SNicholas Johnson 	 *	3. If non-pref MMIO assignment fails or pref MMIO
3680d607618SNicholas Johnson 	 *	   assignment fails, will release assigned non-pref MMIO.
3693e6e0d80SYinghai Lu 	 */
370bdc4abecSYinghai Lu 	LIST_HEAD(save_head);
371bdc4abecSYinghai Lu 	LIST_HEAD(local_fail_head);
372b9b0bba9SYinghai Lu 	struct pci_dev_resource *save_res;
373d74b9027SWei Yang 	struct pci_dev_resource *dev_res, *tmp_res, *dev_res2;
374aa914f5eSYinghai Lu 	unsigned long fail_type;
375d74b9027SWei Yang 	resource_size_t add_align, align;
3763e6e0d80SYinghai Lu 
3773e6e0d80SYinghai Lu 	/* Check if optional add_size is there */
378bdc4abecSYinghai Lu 	if (!realloc_head || list_empty(realloc_head))
3793e6e0d80SYinghai Lu 		goto requested_and_reassign;
3803e6e0d80SYinghai Lu 
3813e6e0d80SYinghai Lu 	/* Save original start, end, flags etc at first */
382bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
383bdc4abecSYinghai Lu 		if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
384bffc56d4SYinghai Lu 			free_list(&save_head);
3853e6e0d80SYinghai Lu 			goto requested_and_reassign;
3863e6e0d80SYinghai Lu 		}
387bdc4abecSYinghai Lu 	}
3883e6e0d80SYinghai Lu 
3893e6e0d80SYinghai Lu 	/* Update res in head list with add_size in realloc_head list */
390d74b9027SWei Yang 	list_for_each_entry_safe(dev_res, tmp_res, head, list) {
391bdc4abecSYinghai Lu 		dev_res->res->end += get_res_add_size(realloc_head,
392bdc4abecSYinghai Lu 							dev_res->res);
3933e6e0d80SYinghai Lu 
394d74b9027SWei Yang 		/*
395d74b9027SWei Yang 		 * There are two kinds of additional resources in the list:
396d74b9027SWei Yang 		 * 1. bridge resource  -- IORESOURCE_STARTALIGN
397d74b9027SWei Yang 		 * 2. SR-IOV resource  -- IORESOURCE_SIZEALIGN
398d74b9027SWei Yang 		 * Here just fix the additional alignment for bridge
399d74b9027SWei Yang 		 */
400d74b9027SWei Yang 		if (!(dev_res->res->flags & IORESOURCE_STARTALIGN))
401d74b9027SWei Yang 			continue;
402d74b9027SWei Yang 
403d74b9027SWei Yang 		add_align = get_res_add_align(realloc_head, dev_res->res);
404d74b9027SWei Yang 
405d74b9027SWei Yang 		/*
4060d607618SNicholas Johnson 		 * The "head" list is sorted by alignment so resources with
4070d607618SNicholas Johnson 		 * bigger alignment will be assigned first.  After we
4080d607618SNicholas Johnson 		 * change the alignment of a dev_res in "head" list, we
4090d607618SNicholas Johnson 		 * need to reorder the list by alignment to make it
410d74b9027SWei Yang 		 * consistent.
411d74b9027SWei Yang 		 */
412d74b9027SWei Yang 		if (add_align > dev_res->res->start) {
413552bc94eSYinghai Lu 			resource_size_t r_size = resource_size(dev_res->res);
414552bc94eSYinghai Lu 
415d74b9027SWei Yang 			dev_res->res->start = add_align;
416552bc94eSYinghai Lu 			dev_res->res->end = add_align + r_size - 1;
417d74b9027SWei Yang 
418d74b9027SWei Yang 			list_for_each_entry(dev_res2, head, list) {
419d74b9027SWei Yang 				align = pci_resource_alignment(dev_res2->dev,
420d74b9027SWei Yang 							       dev_res2->res);
421a6b65983SWei Yang 				if (add_align > align) {
422d74b9027SWei Yang 					list_move_tail(&dev_res->list,
423d74b9027SWei Yang 						       &dev_res2->list);
424a6b65983SWei Yang 					break;
425a6b65983SWei Yang 				}
426d74b9027SWei Yang 			}
427d74b9027SWei Yang 		}
428d74b9027SWei Yang 
429d74b9027SWei Yang 	}
430d74b9027SWei Yang 
4313e6e0d80SYinghai Lu 	/* Try updated head list with add_size added */
4323e6e0d80SYinghai Lu 	assign_requested_resources_sorted(head, &local_fail_head);
4333e6e0d80SYinghai Lu 
4340d607618SNicholas Johnson 	/* All assigned with add_size? */
435bdc4abecSYinghai Lu 	if (list_empty(&local_fail_head)) {
4363e6e0d80SYinghai Lu 		/* Remove head list from realloc_head list */
437bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list)
438bdc4abecSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
439bffc56d4SYinghai Lu 		free_list(&save_head);
440bffc56d4SYinghai Lu 		free_list(head);
4413e6e0d80SYinghai Lu 		return;
4423e6e0d80SYinghai Lu 	}
4433e6e0d80SYinghai Lu 
4440d607618SNicholas Johnson 	/* Check failed type */
445aa914f5eSYinghai Lu 	fail_type = pci_fail_res_type_mask(&local_fail_head);
4460d607618SNicholas Johnson 	/* Remove not need to be released assigned res from head list etc */
447aa914f5eSYinghai Lu 	list_for_each_entry_safe(dev_res, tmp_res, head, list)
448aa914f5eSYinghai Lu 		if (dev_res->res->parent &&
449aa914f5eSYinghai Lu 		    !pci_need_to_release(fail_type, dev_res->res)) {
4500d607618SNicholas Johnson 			/* Remove it from realloc_head list */
451aa914f5eSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
452aa914f5eSYinghai Lu 			remove_from_list(&save_head, dev_res->res);
453aa914f5eSYinghai Lu 			list_del(&dev_res->list);
454aa914f5eSYinghai Lu 			kfree(dev_res);
455aa914f5eSYinghai Lu 		}
456aa914f5eSYinghai Lu 
457bffc56d4SYinghai Lu 	free_list(&local_fail_head);
4583e6e0d80SYinghai Lu 	/* Release assigned resource */
459bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list)
460bdc4abecSYinghai Lu 		if (dev_res->res->parent)
461bdc4abecSYinghai Lu 			release_resource(dev_res->res);
4623e6e0d80SYinghai Lu 	/* Restore start/end/flags from saved list */
463b9b0bba9SYinghai Lu 	list_for_each_entry(save_res, &save_head, list) {
464b9b0bba9SYinghai Lu 		struct resource *res = save_res->res;
4653e6e0d80SYinghai Lu 
466b9b0bba9SYinghai Lu 		res->start = save_res->start;
467b9b0bba9SYinghai Lu 		res->end = save_res->end;
468b9b0bba9SYinghai Lu 		res->flags = save_res->flags;
4693e6e0d80SYinghai Lu 	}
470bffc56d4SYinghai Lu 	free_list(&save_head);
4713e6e0d80SYinghai Lu 
4723e6e0d80SYinghai Lu requested_and_reassign:
473c8adf9a3SRam Pai 	/* Satisfy the must-have resource requests */
474c8adf9a3SRam Pai 	assign_requested_resources_sorted(head, fail_head);
475c8adf9a3SRam Pai 
4760d607618SNicholas Johnson 	/* Try to satisfy any additional optional resource requests */
4779e8bf93aSRam Pai 	if (realloc_head)
4789e8bf93aSRam Pai 		reassign_resources_sorted(realloc_head, head);
479bffc56d4SYinghai Lu 	free_list(head);
480c8adf9a3SRam Pai }
481c8adf9a3SRam Pai 
4826841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev,
483bdc4abecSYinghai Lu 					 struct list_head *add_head,
484bdc4abecSYinghai Lu 					 struct list_head *fail_head)
4856841ec68SYinghai Lu {
486bdc4abecSYinghai Lu 	LIST_HEAD(head);
4876841ec68SYinghai Lu 
4886841ec68SYinghai Lu 	__dev_sort_resources(dev, &head);
4898424d759SYinghai Lu 	__assign_resources_sorted(&head, add_head, fail_head);
4906841ec68SYinghai Lu 
4916841ec68SYinghai Lu }
4926841ec68SYinghai Lu 
4936841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus,
494bdc4abecSYinghai Lu 					 struct list_head *realloc_head,
495bdc4abecSYinghai Lu 					 struct list_head *fail_head)
4966841ec68SYinghai Lu {
4976841ec68SYinghai Lu 	struct pci_dev *dev;
498bdc4abecSYinghai Lu 	LIST_HEAD(head);
4996841ec68SYinghai Lu 
5006841ec68SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
5016841ec68SYinghai Lu 		__dev_sort_resources(dev, &head);
5026841ec68SYinghai Lu 
5039e8bf93aSRam Pai 	__assign_resources_sorted(&head, realloc_head, fail_head);
5046841ec68SYinghai Lu }
5056841ec68SYinghai Lu 
506b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus)
5071da177e4SLinus Torvalds {
5081da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
509c7dabef8SBjorn Helgaas 	struct resource *res;
5101da177e4SLinus Torvalds 	struct pci_bus_region region;
5111da177e4SLinus Torvalds 
5127506dc79SFrederick Lawler 	pci_info(bridge, "CardBus bridge to %pR\n",
513b918c62eSYinghai Lu 		 &bus->busn_res);
5141da177e4SLinus Torvalds 
515c7dabef8SBjorn Helgaas 	res = bus->resource[0];
516fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
517c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5181da177e4SLinus Torvalds 		/*
5191da177e4SLinus Torvalds 		 * The IO resource is allocated a range twice as large as it
5201da177e4SLinus Torvalds 		 * would normally need.  This allows us to set both IO regs.
5211da177e4SLinus Torvalds 		 */
5227506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5231da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
5241da177e4SLinus Torvalds 					region.start);
5251da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
5261da177e4SLinus Torvalds 					region.end);
5271da177e4SLinus Torvalds 	}
5281da177e4SLinus Torvalds 
529c7dabef8SBjorn Helgaas 	res = bus->resource[1];
530fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
531c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5327506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5331da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
5341da177e4SLinus Torvalds 					region.start);
5351da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
5361da177e4SLinus Torvalds 					region.end);
5371da177e4SLinus Torvalds 	}
5381da177e4SLinus Torvalds 
539c7dabef8SBjorn Helgaas 	res = bus->resource[2];
540fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
541c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
5427506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5431da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
5441da177e4SLinus Torvalds 					region.start);
5451da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
5461da177e4SLinus Torvalds 					region.end);
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 
549c7dabef8SBjorn Helgaas 	res = bus->resource[3];
550fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
551c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
5527506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5531da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
5541da177e4SLinus Torvalds 					region.start);
5551da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
5561da177e4SLinus Torvalds 					region.end);
5571da177e4SLinus Torvalds 	}
5581da177e4SLinus Torvalds }
559b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus);
5601da177e4SLinus Torvalds 
5610d607618SNicholas Johnson /*
5620d607618SNicholas Johnson  * Initialize bridges with base/limit values we have collected.  PCI-to-PCI
5630d607618SNicholas Johnson  * Bridge Architecture Specification rev. 1.1 (1998) requires that if there
5640d607618SNicholas Johnson  * are no I/O ports or memory behind the bridge, the corresponding range
5650d607618SNicholas Johnson  * must be turned off by writing base value greater than limit to the
5660d607618SNicholas Johnson  * bridge's base/limit registers.
5670d607618SNicholas Johnson  *
5680d607618SNicholas Johnson  * Note: care must be taken when updating I/O base/limit registers of
5690d607618SNicholas Johnson  * bridges which support 32-bit I/O.  This update requires two config space
5700d607618SNicholas Johnson  * writes, so it's quite possible that an I/O window of the bridge will
5710d607618SNicholas Johnson  * have some undesirable address (e.g. 0) after the first write.  Ditto
5720d607618SNicholas Johnson  * 64-bit prefetchable MMIO.
5730d607618SNicholas Johnson  */
5743f2f4dc4SYinghai Lu static void pci_setup_bridge_io(struct pci_dev *bridge)
5751da177e4SLinus Torvalds {
576c7dabef8SBjorn Helgaas 	struct resource *res;
5771da177e4SLinus Torvalds 	struct pci_bus_region region;
5782b28ae19SBjorn Helgaas 	unsigned long io_mask;
5792b28ae19SBjorn Helgaas 	u8 io_base_lo, io_limit_lo;
5805b764b83SBjorn Helgaas 	u16 l;
5815b764b83SBjorn Helgaas 	u32 io_upper16;
5821da177e4SLinus Torvalds 
5832b28ae19SBjorn Helgaas 	io_mask = PCI_IO_RANGE_MASK;
5842b28ae19SBjorn Helgaas 	if (bridge->io_window_1k)
5852b28ae19SBjorn Helgaas 		io_mask = PCI_IO_1K_RANGE_MASK;
5862b28ae19SBjorn Helgaas 
5870d607618SNicholas Johnson 	/* Set up the top and bottom of the PCI I/O segment for this bus */
5886e0688dbSKrzysztof Wilczynski 	res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
589fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
590c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5915b764b83SBjorn Helgaas 		pci_read_config_word(bridge, PCI_IO_BASE, &l);
5922b28ae19SBjorn Helgaas 		io_base_lo = (region.start >> 8) & io_mask;
5932b28ae19SBjorn Helgaas 		io_limit_lo = (region.end >> 8) & io_mask;
5945b764b83SBjorn Helgaas 		l = ((u16) io_limit_lo << 8) | io_base_lo;
5950d607618SNicholas Johnson 		/* Set up upper 16 bits of I/O base/limit */
5961da177e4SLinus Torvalds 		io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
5977506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5987cc5997dSYinghai Lu 	} else {
5990d607618SNicholas Johnson 		/* Clear upper 16 bits of I/O base/limit */
6001da177e4SLinus Torvalds 		io_upper16 = 0;
6011da177e4SLinus Torvalds 		l = 0x00f0;
6021da177e4SLinus Torvalds 	}
6030d607618SNicholas Johnson 	/* Temporarily disable the I/O range before updating PCI_IO_BASE */
6041da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
6050d607618SNicholas Johnson 	/* Update lower 16 bits of I/O base/limit */
6065b764b83SBjorn Helgaas 	pci_write_config_word(bridge, PCI_IO_BASE, l);
6070d607618SNicholas Johnson 	/* Update upper 16 bits of I/O base/limit */
6081da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
6097cc5997dSYinghai Lu }
6101da177e4SLinus Torvalds 
6113f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio(struct pci_dev *bridge)
6127cc5997dSYinghai Lu {
6137cc5997dSYinghai Lu 	struct resource *res;
6147cc5997dSYinghai Lu 	struct pci_bus_region region;
6157cc5997dSYinghai Lu 	u32 l;
6167cc5997dSYinghai Lu 
6170d607618SNicholas Johnson 	/* Set up the top and bottom of the PCI Memory segment for this bus */
6186e0688dbSKrzysztof Wilczynski 	res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
619fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
620c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
6211da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
6221da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
6237506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
6247cc5997dSYinghai Lu 	} else {
6251da177e4SLinus Torvalds 		l = 0x0000fff0;
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
6287cc5997dSYinghai Lu }
6297cc5997dSYinghai Lu 
6303f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge)
6317cc5997dSYinghai Lu {
6327cc5997dSYinghai Lu 	struct resource *res;
6337cc5997dSYinghai Lu 	struct pci_bus_region region;
6347cc5997dSYinghai Lu 	u32 l, bu, lu;
6351da177e4SLinus Torvalds 
6360d607618SNicholas Johnson 	/*
6370d607618SNicholas Johnson 	 * Clear out the upper 32 bits of PREF limit.  If
6380d607618SNicholas Johnson 	 * PCI_PREF_BASE_UPPER32 was non-zero, this temporarily disables
6390d607618SNicholas Johnson 	 * PREF range, which is ok.
6400d607618SNicholas Johnson 	 */
6411da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
6421da177e4SLinus Torvalds 
6430d607618SNicholas Johnson 	/* Set up PREF base/limit */
644c40a22e0SBenjamin Herrenschmidt 	bu = lu = 0;
6456e0688dbSKrzysztof Wilczynski 	res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
646fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
647c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_PREFETCH) {
6481da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
6491da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
650c7dabef8SBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64) {
65113d36c24SAndrew Morton 			bu = upper_32_bits(region.start);
65213d36c24SAndrew Morton 			lu = upper_32_bits(region.end);
6531f82de10SYinghai Lu 		}
6547506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
6557cc5997dSYinghai Lu 	} else {
6561da177e4SLinus Torvalds 		l = 0x0000fff0;
6571da177e4SLinus Torvalds 	}
6581da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
6591da177e4SLinus Torvalds 
6600d607618SNicholas Johnson 	/* Set the upper 32 bits of PREF base & limit */
661c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
662c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
6637cc5997dSYinghai Lu }
6647cc5997dSYinghai Lu 
6657cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
6667cc5997dSYinghai Lu {
6677cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
6687cc5997dSYinghai Lu 
6697506dc79SFrederick Lawler 	pci_info(bridge, "PCI bridge to %pR\n",
670b918c62eSYinghai Lu 		 &bus->busn_res);
6717cc5997dSYinghai Lu 
6727cc5997dSYinghai Lu 	if (type & IORESOURCE_IO)
6733f2f4dc4SYinghai Lu 		pci_setup_bridge_io(bridge);
6747cc5997dSYinghai Lu 
6757cc5997dSYinghai Lu 	if (type & IORESOURCE_MEM)
6763f2f4dc4SYinghai Lu 		pci_setup_bridge_mmio(bridge);
6777cc5997dSYinghai Lu 
6787cc5997dSYinghai Lu 	if (type & IORESOURCE_PREFETCH)
6793f2f4dc4SYinghai Lu 		pci_setup_bridge_mmio_pref(bridge);
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds 
684d366d28cSGavin Shan void __weak pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
685d366d28cSGavin Shan {
686d366d28cSGavin Shan }
687d366d28cSGavin Shan 
688e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus)
6897cc5997dSYinghai Lu {
6907cc5997dSYinghai Lu 	unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
6917cc5997dSYinghai Lu 				  IORESOURCE_PREFETCH;
6927cc5997dSYinghai Lu 
693d366d28cSGavin Shan 	pcibios_setup_bridge(bus, type);
6947cc5997dSYinghai Lu 	__pci_setup_bridge(bus, type);
6957cc5997dSYinghai Lu }
6967cc5997dSYinghai Lu 
6978505e729SYinghai Lu 
6988505e729SYinghai Lu int pci_claim_bridge_resource(struct pci_dev *bridge, int i)
6998505e729SYinghai Lu {
7008505e729SYinghai Lu 	if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END)
7018505e729SYinghai Lu 		return 0;
7028505e729SYinghai Lu 
7038505e729SYinghai Lu 	if (pci_claim_resource(bridge, i) == 0)
7040d607618SNicholas Johnson 		return 0;	/* Claimed the window */
7058505e729SYinghai Lu 
7068505e729SYinghai Lu 	if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
7078505e729SYinghai Lu 		return 0;
7088505e729SYinghai Lu 
7098505e729SYinghai Lu 	if (!pci_bus_clip_resource(bridge, i))
7100d607618SNicholas Johnson 		return -EINVAL;	/* Clipping didn't change anything */
7118505e729SYinghai Lu 
7126e0688dbSKrzysztof Wilczynski 	switch (i) {
7136e0688dbSKrzysztof Wilczynski 	case PCI_BRIDGE_IO_WINDOW:
7148505e729SYinghai Lu 		pci_setup_bridge_io(bridge);
7158505e729SYinghai Lu 		break;
7166e0688dbSKrzysztof Wilczynski 	case PCI_BRIDGE_MEM_WINDOW:
7178505e729SYinghai Lu 		pci_setup_bridge_mmio(bridge);
7188505e729SYinghai Lu 		break;
7196e0688dbSKrzysztof Wilczynski 	case PCI_BRIDGE_PREF_MEM_WINDOW:
7208505e729SYinghai Lu 		pci_setup_bridge_mmio_pref(bridge);
7218505e729SYinghai Lu 		break;
7228505e729SYinghai Lu 	default:
7238505e729SYinghai Lu 		return -EINVAL;
7248505e729SYinghai Lu 	}
7258505e729SYinghai Lu 
7268505e729SYinghai Lu 	if (pci_claim_resource(bridge, i) == 0)
7270d607618SNicholas Johnson 		return 0;	/* Claimed a smaller window */
7288505e729SYinghai Lu 
7298505e729SYinghai Lu 	return -EINVAL;
7308505e729SYinghai Lu }
7318505e729SYinghai Lu 
7320d607618SNicholas Johnson /*
7330d607618SNicholas Johnson  * Check whether the bridge supports optional I/O and prefetchable memory
7340d607618SNicholas Johnson  * ranges.  If not, the respective base/limit registers must be read-only
7350d607618SNicholas Johnson  * and read as 0.
7360d607618SNicholas Johnson  */
73796bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus)
7381da177e4SLinus Torvalds {
7391da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
7406e0688dbSKrzysztof Wilczynski 	struct resource *b_res;
7411da177e4SLinus Torvalds 
7426e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
7436e0688dbSKrzysztof Wilczynski 	b_res->flags |= IORESOURCE_MEM;
7441da177e4SLinus Torvalds 
7456e0688dbSKrzysztof Wilczynski 	if (bridge->io_window) {
7466e0688dbSKrzysztof Wilczynski 		b_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
7476e0688dbSKrzysztof Wilczynski 		b_res->flags |= IORESOURCE_IO;
7486e0688dbSKrzysztof Wilczynski 	}
749d2f54d9bSBjorn Helgaas 
75051c48b31SBjorn Helgaas 	if (bridge->pref_window) {
7516e0688dbSKrzysztof Wilczynski 		b_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
7526e0688dbSKrzysztof Wilczynski 		b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
75351c48b31SBjorn Helgaas 		if (bridge->pref_64_window) {
7546e0688dbSKrzysztof Wilczynski 			b_res->flags |= IORESOURCE_MEM_64 |
7556e0688dbSKrzysztof Wilczynski 					PCI_PREF_RANGE_TYPE_64;
75699586105SYinghai Lu 		}
7571f82de10SYinghai Lu 	}
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds 
7600d607618SNicholas Johnson /*
761c13704f5SNicholas Johnson  * Helper function for sizing routines.  Assigned resources have non-NULL
762c13704f5SNicholas Johnson  * parent resource.
763c13704f5SNicholas Johnson  *
764c13704f5SNicholas Johnson  * Return first unassigned resource of the correct type.  If there is none,
765c13704f5SNicholas Johnson  * return first assigned resource of the correct type.  If none of the
766c13704f5SNicholas Johnson  * above, return NULL.
767c13704f5SNicholas Johnson  *
768c13704f5SNicholas Johnson  * Returning an assigned resource of the correct type allows the caller to
769c13704f5SNicholas Johnson  * distinguish between already assigned and no resource of the correct type.
7700d607618SNicholas Johnson  */
771c13704f5SNicholas Johnson static struct resource *find_bus_resource_of_type(struct pci_bus *bus,
7720d607618SNicholas Johnson 						  unsigned long type_mask,
7730d607618SNicholas Johnson 						  unsigned long type)
7741da177e4SLinus Torvalds {
775c13704f5SNicholas Johnson 	struct resource *r, *r_assigned = NULL;
7761da177e4SLinus Torvalds 	int i;
7771da177e4SLinus Torvalds 
77889a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, r, i) {
779299de034SIvan Kokshaysky 		if (r == &ioport_resource || r == &iomem_resource)
780299de034SIvan Kokshaysky 			continue;
78155a10984SJesse Barnes 		if (r && (r->flags & type_mask) == type && !r->parent)
7821da177e4SLinus Torvalds 			return r;
783c13704f5SNicholas Johnson 		if (r && (r->flags & type_mask) == type && !r_assigned)
784c13704f5SNicholas Johnson 			r_assigned = r;
7851da177e4SLinus Torvalds 	}
786c13704f5SNicholas Johnson 	return r_assigned;
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
78913583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size,
79013583b16SRam Pai 					resource_size_t min_size,
79113583b16SRam Pai 					resource_size_t size1,
792de3ffa30SJon Derrick 					resource_size_t add_size,
793de3ffa30SJon Derrick 					resource_size_t children_add_size,
79413583b16SRam Pai 					resource_size_t old_size,
79513583b16SRam Pai 					resource_size_t align)
79613583b16SRam Pai {
79713583b16SRam Pai 	if (size < min_size)
79813583b16SRam Pai 		size = min_size;
79913583b16SRam Pai 	if (old_size == 1)
80013583b16SRam Pai 		old_size = 0;
8010d607618SNicholas Johnson 	/*
8020d607618SNicholas Johnson 	 * To be fixed in 2.5: we should have sort of HAVE_ISA flag in the
8030d607618SNicholas Johnson 	 * struct pci_bus.
8040d607618SNicholas Johnson 	 */
80513583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
80613583b16SRam Pai 	size = (size & 0xff) + ((size & ~0xffUL) << 2);
80713583b16SRam Pai #endif
808de3ffa30SJon Derrick 	size = size + size1;
80913583b16SRam Pai 	if (size < old_size)
81013583b16SRam Pai 		size = old_size;
811de3ffa30SJon Derrick 
812de3ffa30SJon Derrick 	size = ALIGN(max(size, add_size) + children_add_size, align);
81313583b16SRam Pai 	return size;
81413583b16SRam Pai }
81513583b16SRam Pai 
81613583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size,
81713583b16SRam Pai 					 resource_size_t min_size,
818de3ffa30SJon Derrick 					 resource_size_t add_size,
819de3ffa30SJon Derrick 					 resource_size_t children_add_size,
82013583b16SRam Pai 					 resource_size_t old_size,
82113583b16SRam Pai 					 resource_size_t align)
82213583b16SRam Pai {
82313583b16SRam Pai 	if (size < min_size)
82413583b16SRam Pai 		size = min_size;
82513583b16SRam Pai 	if (old_size == 1)
82613583b16SRam Pai 		old_size = 0;
82713583b16SRam Pai 	if (size < old_size)
82813583b16SRam Pai 		size = old_size;
829de3ffa30SJon Derrick 
830de3ffa30SJon Derrick 	size = ALIGN(max(size, add_size) + children_add_size, align);
83113583b16SRam Pai 	return size;
83213583b16SRam Pai }
83313583b16SRam Pai 
834ac5ad93eSGavin Shan resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
835ac5ad93eSGavin Shan 						unsigned long type)
836ac5ad93eSGavin Shan {
837ac5ad93eSGavin Shan 	return 1;
838ac5ad93eSGavin Shan }
839ac5ad93eSGavin Shan 
840ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_MEM_ALIGN	0x100000	/* 1MiB */
841ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN	0x1000		/* 4KiB */
842ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN_1K	0x400		/* 1KiB */
843ac5ad93eSGavin Shan 
8440d607618SNicholas Johnson static resource_size_t window_alignment(struct pci_bus *bus, unsigned long type)
845ac5ad93eSGavin Shan {
846ac5ad93eSGavin Shan 	resource_size_t align = 1, arch_align;
847ac5ad93eSGavin Shan 
848ac5ad93eSGavin Shan 	if (type & IORESOURCE_MEM)
849ac5ad93eSGavin Shan 		align = PCI_P2P_DEFAULT_MEM_ALIGN;
850ac5ad93eSGavin Shan 	else if (type & IORESOURCE_IO) {
851ac5ad93eSGavin Shan 		/*
8520d607618SNicholas Johnson 		 * Per spec, I/O windows are 4K-aligned, but some bridges have
8530d607618SNicholas Johnson 		 * an extension to support 1K alignment.
854ac5ad93eSGavin Shan 		 */
8552c8d5a2dSIvan Kokshaysky 		if (bus->self && bus->self->io_window_1k)
856ac5ad93eSGavin Shan 			align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
857ac5ad93eSGavin Shan 		else
858ac5ad93eSGavin Shan 			align = PCI_P2P_DEFAULT_IO_ALIGN;
859ac5ad93eSGavin Shan 	}
860ac5ad93eSGavin Shan 
861ac5ad93eSGavin Shan 	arch_align = pcibios_window_alignment(bus, type);
862ac5ad93eSGavin Shan 	return max(align, arch_align);
863ac5ad93eSGavin Shan }
864ac5ad93eSGavin Shan 
865c8adf9a3SRam Pai /**
8660d607618SNicholas Johnson  * pbus_size_io() - Size the I/O window of a given bus
867c8adf9a3SRam Pai  *
8680d607618SNicholas Johnson  * @bus:		The bus
8690d607618SNicholas Johnson  * @min_size:		The minimum I/O window that must be allocated
8700d607618SNicholas Johnson  * @add_size:		Additional optional I/O window
8710d607618SNicholas Johnson  * @realloc_head:	Track the additional I/O window on this list
872c8adf9a3SRam Pai  *
8730d607618SNicholas Johnson  * Sizing the I/O windows of the PCI-PCI bridge is trivial, since these
8740d607618SNicholas Johnson  * windows have 1K or 4K granularity and the I/O ranges of non-bridge PCI
8750d607618SNicholas Johnson  * devices are limited to 256 bytes.  We must be careful with the ISA
8760d607618SNicholas Johnson  * aliasing though.
877c8adf9a3SRam Pai  */
878c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
8790d607618SNicholas Johnson 			 resource_size_t add_size,
8800d607618SNicholas Johnson 			 struct list_head *realloc_head)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds 	struct pci_dev *dev;
883c13704f5SNicholas Johnson 	struct resource *b_res = find_bus_resource_of_type(bus, IORESOURCE_IO,
8845b285415SYinghai Lu 							   IORESOURCE_IO);
88511251a86SWei Yang 	resource_size_t size = 0, size0 = 0, size1 = 0;
886be768912SYinghai Lu 	resource_size_t children_add_size = 0;
8872d1d6678SBjorn Helgaas 	resource_size_t min_align, align;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	if (!b_res)
8901da177e4SLinus Torvalds 		return;
8911da177e4SLinus Torvalds 
892c13704f5SNicholas Johnson 	/* If resource is already assigned, nothing more to do */
893c13704f5SNicholas Johnson 	if (b_res->parent)
894c13704f5SNicholas Johnson 		return;
895c13704f5SNicholas Johnson 
8962d1d6678SBjorn Helgaas 	min_align = window_alignment(bus, IORESOURCE_IO);
8971da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
8981da177e4SLinus Torvalds 		int i;
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
9011da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
9021da177e4SLinus Torvalds 			unsigned long r_size;
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 			if (r->parent || !(r->flags & IORESOURCE_IO))
9051da177e4SLinus Torvalds 				continue;
906022edd86SZhao, Yu 			r_size = resource_size(r);
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 			if (r_size < 0x400)
9091da177e4SLinus Torvalds 				/* Might be re-aligned for ISA */
9101da177e4SLinus Torvalds 				size += r_size;
9111da177e4SLinus Torvalds 			else
9121da177e4SLinus Torvalds 				size1 += r_size;
913be768912SYinghai Lu 
914fd591341SYinghai Lu 			align = pci_resource_alignment(dev, r);
915fd591341SYinghai Lu 			if (align > min_align)
916fd591341SYinghai Lu 				min_align = align;
917fd591341SYinghai Lu 
9189e8bf93aSRam Pai 			if (realloc_head)
9199e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
9201da177e4SLinus Torvalds 		}
9211da177e4SLinus Torvalds 	}
922fd591341SYinghai Lu 
923de3ffa30SJon Derrick 	size0 = calculate_iosize(size, min_size, size1, 0, 0,
924fd591341SYinghai Lu 			resource_size(b_res), min_align);
925de3ffa30SJon Derrick 	size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
926de3ffa30SJon Derrick 		calculate_iosize(size, min_size, size1, add_size, children_add_size,
927fd591341SYinghai Lu 			resource_size(b_res), min_align);
928c8adf9a3SRam Pai 	if (!size0 && !size1) {
9292c8d5a2dSIvan Kokshaysky 		if (bus->self && (b_res->start || b_res->end))
9307506dc79SFrederick Lawler 			pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
931227f0647SRyan Desfosses 				 b_res, &bus->busn_res);
9321da177e4SLinus Torvalds 		b_res->flags = 0;
9331da177e4SLinus Torvalds 		return;
9341da177e4SLinus Torvalds 	}
935fd591341SYinghai Lu 
936fd591341SYinghai Lu 	b_res->start = min_align;
937c8adf9a3SRam Pai 	b_res->end = b_res->start + size0 - 1;
93888452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
9392c8d5a2dSIvan Kokshaysky 	if (bus->self && size1 > size0 && realloc_head) {
940fd591341SYinghai Lu 		add_to_list(realloc_head, bus->self, b_res, size1-size0,
941fd591341SYinghai Lu 			    min_align);
94234c6b710SMohan Kumar 		pci_info(bus->self, "bridge window %pR to %pR add_size %llx\n",
943227f0647SRyan Desfosses 			 b_res, &bus->busn_res,
94411251a86SWei Yang 			 (unsigned long long) size1 - size0);
945b592443dSYinghai Lu 	}
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
948c121504eSGavin Shan static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
949c121504eSGavin Shan 						  int max_order)
950c121504eSGavin Shan {
951c121504eSGavin Shan 	resource_size_t align = 0;
952c121504eSGavin Shan 	resource_size_t min_align = 0;
953c121504eSGavin Shan 	int order;
954c121504eSGavin Shan 
955c121504eSGavin Shan 	for (order = 0; order <= max_order; order++) {
956c121504eSGavin Shan 		resource_size_t align1 = 1;
957c121504eSGavin Shan 
958c121504eSGavin Shan 		align1 <<= (order + 20);
959c121504eSGavin Shan 
960c121504eSGavin Shan 		if (!align)
961c121504eSGavin Shan 			min_align = align1;
962c121504eSGavin Shan 		else if (ALIGN(align + min_align, min_align) < align1)
963c121504eSGavin Shan 			min_align = align1 >> 1;
964c121504eSGavin Shan 		align += aligns[order];
965c121504eSGavin Shan 	}
966c121504eSGavin Shan 
967c121504eSGavin Shan 	return min_align;
968c121504eSGavin Shan }
969c121504eSGavin Shan 
970c8adf9a3SRam Pai /**
9710d607618SNicholas Johnson  * pbus_size_mem() - Size the memory window of a given bus
972c8adf9a3SRam Pai  *
9730d607618SNicholas Johnson  * @bus:		The bus
9740d607618SNicholas Johnson  * @mask:		Mask the resource flag, then compare it with type
9750d607618SNicholas Johnson  * @type:		The type of free resource from bridge
9760d607618SNicholas Johnson  * @type2:		Second match type
9770d607618SNicholas Johnson  * @type3:		Third match type
9780d607618SNicholas Johnson  * @min_size:		The minimum memory window that must be allocated
9790d607618SNicholas Johnson  * @add_size:		Additional optional memory window
9800d607618SNicholas Johnson  * @realloc_head:	Track the additional memory window on this list
981c8adf9a3SRam Pai  *
9820d607618SNicholas Johnson  * Calculate the size of the bus and minimal alignment which guarantees
9830d607618SNicholas Johnson  * that all child resources fit in this size.
98430afe8d0SBjorn Helgaas  *
9850d607618SNicholas Johnson  * Return -ENOSPC if there's no available bus resource of the desired
9860d607618SNicholas Johnson  * type.  Otherwise, set the bus resource start/end to indicate the
9870d607618SNicholas Johnson  * required size, add things to realloc_head (if supplied), and return 0.
988c8adf9a3SRam Pai  */
98928760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
9905b285415SYinghai Lu 			 unsigned long type, unsigned long type2,
9910d607618SNicholas Johnson 			 unsigned long type3, resource_size_t min_size,
9920d607618SNicholas Johnson 			 resource_size_t add_size,
993bdc4abecSYinghai Lu 			 struct list_head *realloc_head)
9941da177e4SLinus Torvalds {
9951da177e4SLinus Torvalds 	struct pci_dev *dev;
996c8adf9a3SRam Pai 	resource_size_t min_align, align, size, size0, size1;
9973dc8a1f6SDongdong Liu 	resource_size_t aligns[24]; /* Alignments from 1MB to 8TB */
9981da177e4SLinus Torvalds 	int order, max_order;
999c13704f5SNicholas Johnson 	struct resource *b_res = find_bus_resource_of_type(bus,
10005b285415SYinghai Lu 					mask | IORESOURCE_PREFETCH, type);
1001be768912SYinghai Lu 	resource_size_t children_add_size = 0;
1002d74b9027SWei Yang 	resource_size_t children_add_align = 0;
1003d74b9027SWei Yang 	resource_size_t add_align = 0;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	if (!b_res)
100630afe8d0SBjorn Helgaas 		return -ENOSPC;
10071da177e4SLinus Torvalds 
1008c13704f5SNicholas Johnson 	/* If resource is already assigned, nothing more to do */
1009c13704f5SNicholas Johnson 	if (b_res->parent)
1010c13704f5SNicholas Johnson 		return 0;
1011c13704f5SNicholas Johnson 
10121da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
10131da177e4SLinus Torvalds 	max_order = 0;
10141da177e4SLinus Torvalds 	size = 0;
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
10171da177e4SLinus Torvalds 		int i;
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
10201da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
1021c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
10221da177e4SLinus Torvalds 
1023a2220d80SDavid Daney 			if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) ||
1024a2220d80SDavid Daney 			    ((r->flags & mask) != type &&
10255b285415SYinghai Lu 			     (r->flags & mask) != type2 &&
10265b285415SYinghai Lu 			     (r->flags & mask) != type3))
10271da177e4SLinus Torvalds 				continue;
1028022edd86SZhao, Yu 			r_size = resource_size(r);
10292aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV
10300d607618SNicholas Johnson 			/* Put SRIOV requested res to the optional list */
10319e8bf93aSRam Pai 			if (realloc_head && i >= PCI_IOV_RESOURCES &&
10322aceefcbSYinghai Lu 					i <= PCI_IOV_RESOURCE_END) {
1033d74b9027SWei Yang 				add_align = max(pci_resource_alignment(dev, r), add_align);
10342aceefcbSYinghai Lu 				r->end = r->start - 1;
10350d607618SNicholas Johnson 				add_to_list(realloc_head, dev, r, r_size, 0 /* Don't care */);
10362aceefcbSYinghai Lu 				children_add_size += r_size;
10372aceefcbSYinghai Lu 				continue;
10382aceefcbSYinghai Lu 			}
10392aceefcbSYinghai Lu #endif
104014c8530dSAlan 			/*
104114c8530dSAlan 			 * aligns[0] is for 1MB (since bridge memory
104214c8530dSAlan 			 * windows are always at least 1MB aligned), so
104314c8530dSAlan 			 * keep "order" from being negative for smaller
104414c8530dSAlan 			 * resources.
104514c8530dSAlan 			 */
10466faf17f6SChris Wright 			align = pci_resource_alignment(dev, r);
10471da177e4SLinus Torvalds 			order = __ffs(align) - 20;
104814c8530dSAlan 			if (order < 0)
104914c8530dSAlan 				order = 0;
105014c8530dSAlan 			if (order >= ARRAY_SIZE(aligns)) {
10517506dc79SFrederick Lawler 				pci_warn(dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
1052227f0647SRyan Desfosses 					 i, r, (unsigned long long) align);
10531da177e4SLinus Torvalds 				r->flags = 0;
10541da177e4SLinus Torvalds 				continue;
10551da177e4SLinus Torvalds 			}
1056c9c75143SYongji Xie 			size += max(r_size, align);
10570d607618SNicholas Johnson 			/*
10580d607618SNicholas Johnson 			 * Exclude ranges with size > align from calculation of
10590d607618SNicholas Johnson 			 * the alignment.
10600d607618SNicholas Johnson 			 */
1061c9c75143SYongji Xie 			if (r_size <= align)
10621da177e4SLinus Torvalds 				aligns[order] += align;
10631da177e4SLinus Torvalds 			if (order > max_order)
10641da177e4SLinus Torvalds 				max_order = order;
1065be768912SYinghai Lu 
1066d74b9027SWei Yang 			if (realloc_head) {
10679e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
1068d74b9027SWei Yang 				children_add_align = get_res_add_align(realloc_head, r);
1069d74b9027SWei Yang 				add_align = max(add_align, children_add_align);
1070d74b9027SWei Yang 			}
10711da177e4SLinus Torvalds 		}
10721da177e4SLinus Torvalds 	}
10738308c54dSJeremy Fitzhardinge 
1074c121504eSGavin Shan 	min_align = calculate_mem_align(aligns, max_order);
10753ad94b0dSWei Yang 	min_align = max(min_align, window_alignment(bus, b_res->flags));
1076de3ffa30SJon Derrick 	size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), min_align);
1077d74b9027SWei Yang 	add_align = max(min_align, add_align);
1078de3ffa30SJon Derrick 	size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
1079de3ffa30SJon Derrick 		calculate_memsize(size, min_size, add_size, children_add_size,
1080d74b9027SWei Yang 				resource_size(b_res), add_align);
1081c8adf9a3SRam Pai 	if (!size0 && !size1) {
10822c8d5a2dSIvan Kokshaysky 		if (bus->self && (b_res->start || b_res->end))
10837506dc79SFrederick Lawler 			pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
1084227f0647SRyan Desfosses 				 b_res, &bus->busn_res);
10851da177e4SLinus Torvalds 		b_res->flags = 0;
108630afe8d0SBjorn Helgaas 		return 0;
10871da177e4SLinus Torvalds 	}
10881da177e4SLinus Torvalds 	b_res->start = min_align;
1089c8adf9a3SRam Pai 	b_res->end = size0 + min_align - 1;
10905b285415SYinghai Lu 	b_res->flags |= IORESOURCE_STARTALIGN;
10912c8d5a2dSIvan Kokshaysky 	if (bus->self && size1 > size0 && realloc_head) {
1092d74b9027SWei Yang 		add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align);
109334c6b710SMohan Kumar 		pci_info(bus->self, "bridge window %pR to %pR add_size %llx add_align %llx\n",
1094227f0647SRyan Desfosses 			   b_res, &bus->busn_res,
1095d74b9027SWei Yang 			   (unsigned long long) (size1 - size0),
1096d74b9027SWei Yang 			   (unsigned long long) add_align);
1097b592443dSYinghai Lu 	}
109830afe8d0SBjorn Helgaas 	return 0;
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds 
11010a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res)
11020a2daa1cSRam Pai {
11030a2daa1cSRam Pai 	if (res->flags & IORESOURCE_IO)
11040a2daa1cSRam Pai 		return pci_cardbus_io_size;
11050a2daa1cSRam Pai 	if (res->flags & IORESOURCE_MEM)
11060a2daa1cSRam Pai 		return pci_cardbus_mem_size;
11070a2daa1cSRam Pai 	return 0;
11080a2daa1cSRam Pai }
11090a2daa1cSRam Pai 
11100a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus,
1111bdc4abecSYinghai Lu 				 struct list_head *realloc_head)
11121da177e4SLinus Torvalds {
11131da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
11146e0688dbSKrzysztof Wilczynski 	struct resource *b_res;
111511848934SYinghai Lu 	resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
11161da177e4SLinus Torvalds 	u16 ctrl;
11171da177e4SLinus Torvalds 
11186e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_CB_BRIDGE_IO_0_WINDOW];
11196e0688dbSKrzysztof Wilczynski 	if (b_res->parent)
11203796f1e2SYinghai Lu 		goto handle_b_res_1;
11211da177e4SLinus Torvalds 	/*
11220d607618SNicholas Johnson 	 * Reserve some resources for CardBus.  We reserve a fixed amount
11230d607618SNicholas Johnson 	 * of bus space for CardBus bridges.
11241da177e4SLinus Torvalds 	 */
11256e0688dbSKrzysztof Wilczynski 	b_res->start = pci_cardbus_io_size;
11266e0688dbSKrzysztof Wilczynski 	b_res->end = b_res->start + pci_cardbus_io_size - 1;
11276e0688dbSKrzysztof Wilczynski 	b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
112811848934SYinghai Lu 	if (realloc_head) {
11296e0688dbSKrzysztof Wilczynski 		b_res->end -= pci_cardbus_io_size;
113011848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
113111848934SYinghai Lu 			    pci_cardbus_io_size);
113211848934SYinghai Lu 	}
11331da177e4SLinus Torvalds 
11343796f1e2SYinghai Lu handle_b_res_1:
11356e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_CB_BRIDGE_IO_1_WINDOW];
11366e0688dbSKrzysztof Wilczynski 	if (b_res->parent)
11373796f1e2SYinghai Lu 		goto handle_b_res_2;
11386e0688dbSKrzysztof Wilczynski 	b_res->start = pci_cardbus_io_size;
11396e0688dbSKrzysztof Wilczynski 	b_res->end = b_res->start + pci_cardbus_io_size - 1;
11406e0688dbSKrzysztof Wilczynski 	b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
114111848934SYinghai Lu 	if (realloc_head) {
11426e0688dbSKrzysztof Wilczynski 		b_res->end -= pci_cardbus_io_size;
11436e0688dbSKrzysztof Wilczynski 		add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
114411848934SYinghai Lu 			    pci_cardbus_io_size);
114511848934SYinghai Lu 	}
11461da177e4SLinus Torvalds 
11473796f1e2SYinghai Lu handle_b_res_2:
11480d607618SNicholas Johnson 	/* MEM1 must not be pref MMIO */
1149dcef0d06SYinghai Lu 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1150dcef0d06SYinghai Lu 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
1151dcef0d06SYinghai Lu 		ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
1152dcef0d06SYinghai Lu 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1153dcef0d06SYinghai Lu 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1154dcef0d06SYinghai Lu 	}
1155dcef0d06SYinghai Lu 
11560d607618SNicholas Johnson 	/* Check whether prefetchable memory is supported by this bridge. */
11571da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
11581da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
11591da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
11601da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
11611da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
11621da177e4SLinus Torvalds 	}
11631da177e4SLinus Torvalds 
11646e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_0_WINDOW];
11656e0688dbSKrzysztof Wilczynski 	if (b_res->parent)
11663796f1e2SYinghai Lu 		goto handle_b_res_3;
11671da177e4SLinus Torvalds 	/*
11680d607618SNicholas Johnson 	 * If we have prefetchable memory support, allocate two regions.
11690d607618SNicholas Johnson 	 * Otherwise, allocate one region of twice the size.
11701da177e4SLinus Torvalds 	 */
11711da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
11726e0688dbSKrzysztof Wilczynski 		b_res->start = pci_cardbus_mem_size;
11736e0688dbSKrzysztof Wilczynski 		b_res->end = b_res->start + pci_cardbus_mem_size - 1;
11746e0688dbSKrzysztof Wilczynski 		b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
117511848934SYinghai Lu 				    IORESOURCE_STARTALIGN;
117611848934SYinghai Lu 		if (realloc_head) {
11776e0688dbSKrzysztof Wilczynski 			b_res->end -= pci_cardbus_mem_size;
11786e0688dbSKrzysztof Wilczynski 			add_to_list(realloc_head, bridge, b_res,
117911848934SYinghai Lu 				    pci_cardbus_mem_size, pci_cardbus_mem_size);
11801da177e4SLinus Torvalds 		}
11810a2daa1cSRam Pai 
11820d607618SNicholas Johnson 		/* Reduce that to half */
118311848934SYinghai Lu 		b_res_3_size = pci_cardbus_mem_size;
118411848934SYinghai Lu 	}
118511848934SYinghai Lu 
11863796f1e2SYinghai Lu handle_b_res_3:
11876e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_1_WINDOW];
11886e0688dbSKrzysztof Wilczynski 	if (b_res->parent)
11893796f1e2SYinghai Lu 		goto handle_done;
11906e0688dbSKrzysztof Wilczynski 	b_res->start = pci_cardbus_mem_size;
11916e0688dbSKrzysztof Wilczynski 	b_res->end = b_res->start + b_res_3_size - 1;
11926e0688dbSKrzysztof Wilczynski 	b_res->flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
119311848934SYinghai Lu 	if (realloc_head) {
11946e0688dbSKrzysztof Wilczynski 		b_res->end -= b_res_3_size;
11956e0688dbSKrzysztof Wilczynski 		add_to_list(realloc_head, bridge, b_res, b_res_3_size,
119611848934SYinghai Lu 			    pci_cardbus_mem_size);
119711848934SYinghai Lu 	}
11983796f1e2SYinghai Lu 
11993796f1e2SYinghai Lu handle_done:
12003796f1e2SYinghai Lu 	;
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
120310874f5aSBjorn Helgaas void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
12041da177e4SLinus Torvalds {
12051da177e4SLinus Torvalds 	struct pci_dev *dev;
12065b285415SYinghai Lu 	unsigned long mask, prefmask, type2 = 0, type3 = 0;
1207d7b8a217SNicholas Johnson 	resource_size_t additional_io_size = 0, additional_mmio_size = 0,
1208d7b8a217SNicholas Johnson 			additional_mmio_pref_size = 0;
12092c8d5a2dSIvan Kokshaysky 	struct resource *pref;
12102c8d5a2dSIvan Kokshaysky 	struct pci_host_bridge *host;
12112c8d5a2dSIvan Kokshaysky 	int hdr_type, i, ret;
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
12141da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
12151da177e4SLinus Torvalds 		if (!b)
12161da177e4SLinus Torvalds 			continue;
12171da177e4SLinus Torvalds 
1218b2fb5cc5SHonghui Zhang 		switch (dev->hdr_type) {
1219b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_CARDBUS:
12209e8bf93aSRam Pai 			pci_bus_size_cardbus(b, realloc_head);
12211da177e4SLinus Torvalds 			break;
12221da177e4SLinus Torvalds 
1223b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_BRIDGE:
12241da177e4SLinus Torvalds 		default:
12259e8bf93aSRam Pai 			__pci_bus_size_bridges(b, realloc_head);
12261da177e4SLinus Torvalds 			break;
12271da177e4SLinus Torvalds 		}
12281da177e4SLinus Torvalds 	}
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	/* The root bus? */
12312c8d5a2dSIvan Kokshaysky 	if (pci_is_root_bus(bus)) {
12322c8d5a2dSIvan Kokshaysky 		host = to_pci_host_bridge(bus->bridge);
12332c8d5a2dSIvan Kokshaysky 		if (!host->size_windows)
12341da177e4SLinus Torvalds 			return;
12352c8d5a2dSIvan Kokshaysky 		pci_bus_for_each_resource(bus, pref, i)
12362c8d5a2dSIvan Kokshaysky 			if (pref && (pref->flags & IORESOURCE_PREFETCH))
12372c8d5a2dSIvan Kokshaysky 				break;
12382c8d5a2dSIvan Kokshaysky 		hdr_type = -1;	/* Intentionally invalid - not a PCI device. */
12392c8d5a2dSIvan Kokshaysky 	} else {
12406e0688dbSKrzysztof Wilczynski 		pref = &bus->self->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
12412c8d5a2dSIvan Kokshaysky 		hdr_type = bus->self->hdr_type;
12422c8d5a2dSIvan Kokshaysky 	}
12431da177e4SLinus Torvalds 
12442c8d5a2dSIvan Kokshaysky 	switch (hdr_type) {
1245b2fb5cc5SHonghui Zhang 	case PCI_HEADER_TYPE_CARDBUS:
12460d607618SNicholas Johnson 		/* Don't size CardBuses yet */
12471da177e4SLinus Torvalds 		break;
12481da177e4SLinus Torvalds 
1249b2fb5cc5SHonghui Zhang 	case PCI_HEADER_TYPE_BRIDGE:
12501da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
125128760489SEric W. Biederman 		if (bus->self->is_hotplug_bridge) {
1252c8adf9a3SRam Pai 			additional_io_size  = pci_hotplug_io_size;
1253d7b8a217SNicholas Johnson 			additional_mmio_size = pci_hotplug_mmio_size;
1254d7b8a217SNicholas Johnson 			additional_mmio_pref_size = pci_hotplug_mmio_pref_size;
125528760489SEric W. Biederman 		}
1256df561f66SGustavo A. R. Silva 		fallthrough;
12571da177e4SLinus Torvalds 	default:
125819aa7ee4SYinghai Lu 		pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
125919aa7ee4SYinghai Lu 			     additional_io_size, realloc_head);
126067d29b5cSBjorn Helgaas 
126167d29b5cSBjorn Helgaas 		/*
126267d29b5cSBjorn Helgaas 		 * If there's a 64-bit prefetchable MMIO window, compute
126367d29b5cSBjorn Helgaas 		 * the size required to put all 64-bit prefetchable
126467d29b5cSBjorn Helgaas 		 * resources in it.
126567d29b5cSBjorn Helgaas 		 */
12661da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
12671da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
12682c8d5a2dSIvan Kokshaysky 		if (pref && (pref->flags & IORESOURCE_MEM_64)) {
12695b285415SYinghai Lu 			prefmask |= IORESOURCE_MEM_64;
127030afe8d0SBjorn Helgaas 			ret = pbus_size_mem(bus, prefmask, prefmask,
12715b285415SYinghai Lu 				prefmask, prefmask,
1272d7b8a217SNicholas Johnson 				realloc_head ? 0 : additional_mmio_pref_size,
1273d7b8a217SNicholas Johnson 				additional_mmio_pref_size, realloc_head);
127467d29b5cSBjorn Helgaas 
12755b285415SYinghai Lu 			/*
127667d29b5cSBjorn Helgaas 			 * If successful, all non-prefetchable resources
127767d29b5cSBjorn Helgaas 			 * and any 32-bit prefetchable resources will go in
127867d29b5cSBjorn Helgaas 			 * the non-prefetchable window.
127967d29b5cSBjorn Helgaas 			 */
128067d29b5cSBjorn Helgaas 			if (ret == 0) {
12815b285415SYinghai Lu 				mask = prefmask;
12825b285415SYinghai Lu 				type2 = prefmask & ~IORESOURCE_MEM_64;
12835b285415SYinghai Lu 				type3 = prefmask & ~IORESOURCE_PREFETCH;
12845b285415SYinghai Lu 			}
12855b285415SYinghai Lu 		}
128667d29b5cSBjorn Helgaas 
128767d29b5cSBjorn Helgaas 		/*
128867d29b5cSBjorn Helgaas 		 * If there is no 64-bit prefetchable window, compute the
128967d29b5cSBjorn Helgaas 		 * size required to put all prefetchable resources in the
129067d29b5cSBjorn Helgaas 		 * 32-bit prefetchable window (if there is one).
129167d29b5cSBjorn Helgaas 		 */
12925b285415SYinghai Lu 		if (!type2) {
12935b285415SYinghai Lu 			prefmask &= ~IORESOURCE_MEM_64;
129430afe8d0SBjorn Helgaas 			ret = pbus_size_mem(bus, prefmask, prefmask,
12955b285415SYinghai Lu 				prefmask, prefmask,
1296d7b8a217SNicholas Johnson 				realloc_head ? 0 : additional_mmio_pref_size,
1297d7b8a217SNicholas Johnson 				additional_mmio_pref_size, realloc_head);
129867d29b5cSBjorn Helgaas 
129967d29b5cSBjorn Helgaas 			/*
130067d29b5cSBjorn Helgaas 			 * If successful, only non-prefetchable resources
130167d29b5cSBjorn Helgaas 			 * will go in the non-prefetchable window.
130267d29b5cSBjorn Helgaas 			 */
130367d29b5cSBjorn Helgaas 			if (ret == 0)
13045b285415SYinghai Lu 				mask = prefmask;
130528760489SEric W. Biederman 			else
1306d7b8a217SNicholas Johnson 				additional_mmio_size += additional_mmio_pref_size;
130767d29b5cSBjorn Helgaas 
13085b285415SYinghai Lu 			type2 = type3 = IORESOURCE_MEM;
13095b285415SYinghai Lu 		}
131067d29b5cSBjorn Helgaas 
131167d29b5cSBjorn Helgaas 		/*
131267d29b5cSBjorn Helgaas 		 * Compute the size required to put everything else in the
131367d29b5cSBjorn Helgaas 		 * non-prefetchable window. This includes:
131467d29b5cSBjorn Helgaas 		 *
131567d29b5cSBjorn Helgaas 		 *   - all non-prefetchable resources
131667d29b5cSBjorn Helgaas 		 *   - 32-bit prefetchable resources if there's a 64-bit
131767d29b5cSBjorn Helgaas 		 *     prefetchable window or no prefetchable window at all
13180d607618SNicholas Johnson 		 *   - 64-bit prefetchable resources if there's no prefetchable
13190d607618SNicholas Johnson 		 *     window at all
132067d29b5cSBjorn Helgaas 		 *
13210d607618SNicholas Johnson 		 * Note that the strategy in __pci_assign_resource() must match
13220d607618SNicholas Johnson 		 * that used here. Specifically, we cannot put a 32-bit
13230d607618SNicholas Johnson 		 * prefetchable resource in a 64-bit prefetchable window.
132467d29b5cSBjorn Helgaas 		 */
13255b285415SYinghai Lu 		pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
1326d7b8a217SNicholas Johnson 			      realloc_head ? 0 : additional_mmio_size,
1327d7b8a217SNicholas Johnson 			      additional_mmio_size, realloc_head);
13281da177e4SLinus Torvalds 		break;
13291da177e4SLinus Torvalds 	}
13301da177e4SLinus Torvalds }
1331c8adf9a3SRam Pai 
133210874f5aSBjorn Helgaas void pci_bus_size_bridges(struct pci_bus *bus)
1333c8adf9a3SRam Pai {
1334c8adf9a3SRam Pai 	__pci_bus_size_bridges(bus, NULL);
1335c8adf9a3SRam Pai }
13361da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
13371da177e4SLinus Torvalds 
1338d04d0111SDavid Daney static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r)
1339d04d0111SDavid Daney {
1340d04d0111SDavid Daney 	int i;
1341d04d0111SDavid Daney 	struct resource *parent_r;
1342d04d0111SDavid Daney 	unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM |
1343d04d0111SDavid Daney 			     IORESOURCE_PREFETCH;
1344d04d0111SDavid Daney 
1345d04d0111SDavid Daney 	pci_bus_for_each_resource(b, parent_r, i) {
1346d04d0111SDavid Daney 		if (!parent_r)
1347d04d0111SDavid Daney 			continue;
1348d04d0111SDavid Daney 
1349d04d0111SDavid Daney 		if ((r->flags & mask) == (parent_r->flags & mask) &&
1350d04d0111SDavid Daney 		    resource_contains(parent_r, r))
1351d04d0111SDavid Daney 			request_resource(parent_r, r);
1352d04d0111SDavid Daney 	}
1353d04d0111SDavid Daney }
1354d04d0111SDavid Daney 
1355d04d0111SDavid Daney /*
13560d607618SNicholas Johnson  * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they are
13570d607618SNicholas Johnson  * skipped by pbus_assign_resources_sorted().
1358d04d0111SDavid Daney  */
1359d04d0111SDavid Daney static void pdev_assign_fixed_resources(struct pci_dev *dev)
1360d04d0111SDavid Daney {
1361d04d0111SDavid Daney 	int i;
1362d04d0111SDavid Daney 
1363d04d0111SDavid Daney 	for (i = 0; i <  PCI_NUM_RESOURCES; i++) {
1364d04d0111SDavid Daney 		struct pci_bus *b;
1365d04d0111SDavid Daney 		struct resource *r = &dev->resource[i];
1366d04d0111SDavid Daney 
1367d04d0111SDavid Daney 		if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) ||
1368d04d0111SDavid Daney 		    !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
1369d04d0111SDavid Daney 			continue;
1370d04d0111SDavid Daney 
1371d04d0111SDavid Daney 		b = dev->bus;
1372d04d0111SDavid Daney 		while (b && !r->parent) {
1373d04d0111SDavid Daney 			assign_fixed_resource_on_bus(b, r);
1374d04d0111SDavid Daney 			b = b->parent;
1375d04d0111SDavid Daney 		}
1376d04d0111SDavid Daney 	}
1377d04d0111SDavid Daney }
1378d04d0111SDavid Daney 
137910874f5aSBjorn Helgaas void __pci_bus_assign_resources(const struct pci_bus *bus,
1380bdc4abecSYinghai Lu 				struct list_head *realloc_head,
1381bdc4abecSYinghai Lu 				struct list_head *fail_head)
13821da177e4SLinus Torvalds {
13831da177e4SLinus Torvalds 	struct pci_bus *b;
13841da177e4SLinus Torvalds 	struct pci_dev *dev;
13851da177e4SLinus Torvalds 
13869e8bf93aSRam Pai 	pbus_assign_resources_sorted(bus, realloc_head, fail_head);
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
1389d04d0111SDavid Daney 		pdev_assign_fixed_resources(dev);
1390d04d0111SDavid Daney 
13911da177e4SLinus Torvalds 		b = dev->subordinate;
13921da177e4SLinus Torvalds 		if (!b)
13931da177e4SLinus Torvalds 			continue;
13941da177e4SLinus Torvalds 
13959e8bf93aSRam Pai 		__pci_bus_assign_resources(b, realloc_head, fail_head);
13961da177e4SLinus Torvalds 
1397b2fb5cc5SHonghui Zhang 		switch (dev->hdr_type) {
1398b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_BRIDGE:
13996841ec68SYinghai Lu 			if (!pci_is_enabled(dev))
14001da177e4SLinus Torvalds 				pci_setup_bridge(b);
14011da177e4SLinus Torvalds 			break;
14021da177e4SLinus Torvalds 
1403b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_CARDBUS:
14041da177e4SLinus Torvalds 			pci_setup_cardbus(b);
14051da177e4SLinus Torvalds 			break;
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 		default:
14087506dc79SFrederick Lawler 			pci_info(dev, "not setting up bridge for bus %04x:%02x\n",
1409227f0647SRyan Desfosses 				 pci_domain_nr(b), b->number);
14101da177e4SLinus Torvalds 			break;
14111da177e4SLinus Torvalds 		}
14121da177e4SLinus Torvalds 	}
14131da177e4SLinus Torvalds }
1414568ddef8SYinghai Lu 
141510874f5aSBjorn Helgaas void pci_bus_assign_resources(const struct pci_bus *bus)
1416568ddef8SYinghai Lu {
1417c8adf9a3SRam Pai 	__pci_bus_assign_resources(bus, NULL, NULL);
1418568ddef8SYinghai Lu }
14191da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
14201da177e4SLinus Torvalds 
1421765bf9b7SLorenzo Pieralisi static void pci_claim_device_resources(struct pci_dev *dev)
1422765bf9b7SLorenzo Pieralisi {
1423765bf9b7SLorenzo Pieralisi 	int i;
1424765bf9b7SLorenzo Pieralisi 
1425765bf9b7SLorenzo Pieralisi 	for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
1426765bf9b7SLorenzo Pieralisi 		struct resource *r = &dev->resource[i];
1427765bf9b7SLorenzo Pieralisi 
1428765bf9b7SLorenzo Pieralisi 		if (!r->flags || r->parent)
1429765bf9b7SLorenzo Pieralisi 			continue;
1430765bf9b7SLorenzo Pieralisi 
1431765bf9b7SLorenzo Pieralisi 		pci_claim_resource(dev, i);
1432765bf9b7SLorenzo Pieralisi 	}
1433765bf9b7SLorenzo Pieralisi }
1434765bf9b7SLorenzo Pieralisi 
1435765bf9b7SLorenzo Pieralisi static void pci_claim_bridge_resources(struct pci_dev *dev)
1436765bf9b7SLorenzo Pieralisi {
1437765bf9b7SLorenzo Pieralisi 	int i;
1438765bf9b7SLorenzo Pieralisi 
1439765bf9b7SLorenzo Pieralisi 	for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
1440765bf9b7SLorenzo Pieralisi 		struct resource *r = &dev->resource[i];
1441765bf9b7SLorenzo Pieralisi 
1442765bf9b7SLorenzo Pieralisi 		if (!r->flags || r->parent)
1443765bf9b7SLorenzo Pieralisi 			continue;
1444765bf9b7SLorenzo Pieralisi 
1445765bf9b7SLorenzo Pieralisi 		pci_claim_bridge_resource(dev, i);
1446765bf9b7SLorenzo Pieralisi 	}
1447765bf9b7SLorenzo Pieralisi }
1448765bf9b7SLorenzo Pieralisi 
1449765bf9b7SLorenzo Pieralisi static void pci_bus_allocate_dev_resources(struct pci_bus *b)
1450765bf9b7SLorenzo Pieralisi {
1451765bf9b7SLorenzo Pieralisi 	struct pci_dev *dev;
1452765bf9b7SLorenzo Pieralisi 	struct pci_bus *child;
1453765bf9b7SLorenzo Pieralisi 
1454765bf9b7SLorenzo Pieralisi 	list_for_each_entry(dev, &b->devices, bus_list) {
1455765bf9b7SLorenzo Pieralisi 		pci_claim_device_resources(dev);
1456765bf9b7SLorenzo Pieralisi 
1457765bf9b7SLorenzo Pieralisi 		child = dev->subordinate;
1458765bf9b7SLorenzo Pieralisi 		if (child)
1459765bf9b7SLorenzo Pieralisi 			pci_bus_allocate_dev_resources(child);
1460765bf9b7SLorenzo Pieralisi 	}
1461765bf9b7SLorenzo Pieralisi }
1462765bf9b7SLorenzo Pieralisi 
1463765bf9b7SLorenzo Pieralisi static void pci_bus_allocate_resources(struct pci_bus *b)
1464765bf9b7SLorenzo Pieralisi {
1465765bf9b7SLorenzo Pieralisi 	struct pci_bus *child;
1466765bf9b7SLorenzo Pieralisi 
1467765bf9b7SLorenzo Pieralisi 	/*
14680d607618SNicholas Johnson 	 * Carry out a depth-first search on the PCI bus tree to allocate
14690d607618SNicholas Johnson 	 * bridge apertures.  Read the programmed bridge bases and
14700d607618SNicholas Johnson 	 * recursively claim the respective bridge resources.
1471765bf9b7SLorenzo Pieralisi 	 */
1472765bf9b7SLorenzo Pieralisi 	if (b->self) {
1473765bf9b7SLorenzo Pieralisi 		pci_read_bridge_bases(b);
1474765bf9b7SLorenzo Pieralisi 		pci_claim_bridge_resources(b->self);
1475765bf9b7SLorenzo Pieralisi 	}
1476765bf9b7SLorenzo Pieralisi 
1477765bf9b7SLorenzo Pieralisi 	list_for_each_entry(child, &b->children, node)
1478765bf9b7SLorenzo Pieralisi 		pci_bus_allocate_resources(child);
1479765bf9b7SLorenzo Pieralisi }
1480765bf9b7SLorenzo Pieralisi 
1481765bf9b7SLorenzo Pieralisi void pci_bus_claim_resources(struct pci_bus *b)
1482765bf9b7SLorenzo Pieralisi {
1483765bf9b7SLorenzo Pieralisi 	pci_bus_allocate_resources(b);
1484765bf9b7SLorenzo Pieralisi 	pci_bus_allocate_dev_resources(b);
1485765bf9b7SLorenzo Pieralisi }
1486765bf9b7SLorenzo Pieralisi EXPORT_SYMBOL(pci_bus_claim_resources);
1487765bf9b7SLorenzo Pieralisi 
148810874f5aSBjorn Helgaas static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
1489bdc4abecSYinghai Lu 					  struct list_head *add_head,
1490bdc4abecSYinghai Lu 					  struct list_head *fail_head)
14916841ec68SYinghai Lu {
14926841ec68SYinghai Lu 	struct pci_bus *b;
14936841ec68SYinghai Lu 
14948424d759SYinghai Lu 	pdev_assign_resources_sorted((struct pci_dev *)bridge,
14958424d759SYinghai Lu 					 add_head, fail_head);
14966841ec68SYinghai Lu 
14976841ec68SYinghai Lu 	b = bridge->subordinate;
14986841ec68SYinghai Lu 	if (!b)
14996841ec68SYinghai Lu 		return;
15006841ec68SYinghai Lu 
15018424d759SYinghai Lu 	__pci_bus_assign_resources(b, add_head, fail_head);
15026841ec68SYinghai Lu 
15036841ec68SYinghai Lu 	switch (bridge->class >> 8) {
15046841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_PCI:
15056841ec68SYinghai Lu 		pci_setup_bridge(b);
15066841ec68SYinghai Lu 		break;
15076841ec68SYinghai Lu 
15086841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_CARDBUS:
15096841ec68SYinghai Lu 		pci_setup_cardbus(b);
15106841ec68SYinghai Lu 		break;
15116841ec68SYinghai Lu 
15126841ec68SYinghai Lu 	default:
15137506dc79SFrederick Lawler 		pci_info(bridge, "not setting up bridge for bus %04x:%02x\n",
1514227f0647SRyan Desfosses 			 pci_domain_nr(b), b->number);
15156841ec68SYinghai Lu 		break;
15166841ec68SYinghai Lu 	}
15176841ec68SYinghai Lu }
1518cb21bc94SChristian König 
1519cb21bc94SChristian König #define PCI_RES_TYPE_MASK \
1520cb21bc94SChristian König 	(IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\
1521cb21bc94SChristian König 	 IORESOURCE_MEM_64)
1522cb21bc94SChristian König 
15235009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus,
15245009b460SYinghai Lu 					 unsigned long type)
15255009b460SYinghai Lu {
15265b285415SYinghai Lu 	struct pci_dev *dev = bus->self;
15275009b460SYinghai Lu 	struct resource *r;
1528c50762a8SBjorn Helgaas 	unsigned int old_flags;
15295b285415SYinghai Lu 	struct resource *b_res;
15305b285415SYinghai Lu 	int idx = 1;
15315009b460SYinghai Lu 
15325b285415SYinghai Lu 	b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
15335b285415SYinghai Lu 
15345b285415SYinghai Lu 	/*
15350d607618SNicholas Johnson 	 * 1. If IO port assignment fails, release bridge IO port.
15360d607618SNicholas Johnson 	 * 2. If non pref MMIO assignment fails, release bridge nonpref MMIO.
15370d607618SNicholas Johnson 	 * 3. If 64bit pref MMIO assignment fails, and bridge pref is 64bit,
15380d607618SNicholas Johnson 	 *    release bridge pref MMIO.
15390d607618SNicholas Johnson 	 * 4. If pref MMIO assignment fails, and bridge pref is 32bit,
15400d607618SNicholas Johnson 	 *    release bridge pref MMIO.
15410d607618SNicholas Johnson 	 * 5. If pref MMIO assignment fails, and bridge pref is not
15420d607618SNicholas Johnson 	 *    assigned, release bridge nonpref MMIO.
15435b285415SYinghai Lu 	 */
15445b285415SYinghai Lu 	if (type & IORESOURCE_IO)
15455b285415SYinghai Lu 		idx = 0;
15465b285415SYinghai Lu 	else if (!(type & IORESOURCE_PREFETCH))
15475b285415SYinghai Lu 		idx = 1;
15485b285415SYinghai Lu 	else if ((type & IORESOURCE_MEM_64) &&
15495b285415SYinghai Lu 		 (b_res[2].flags & IORESOURCE_MEM_64))
15505b285415SYinghai Lu 		idx = 2;
15515b285415SYinghai Lu 	else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
15525b285415SYinghai Lu 		 (b_res[2].flags & IORESOURCE_PREFETCH))
15535b285415SYinghai Lu 		idx = 2;
15545b285415SYinghai Lu 	else
15555b285415SYinghai Lu 		idx = 1;
15565b285415SYinghai Lu 
15575b285415SYinghai Lu 	r = &b_res[idx];
15585b285415SYinghai Lu 
15595009b460SYinghai Lu 	if (!r->parent)
15605b285415SYinghai Lu 		return;
15615b285415SYinghai Lu 
15620d607618SNicholas Johnson 	/* If there are children, release them all */
15635009b460SYinghai Lu 	release_child_resources(r);
15645009b460SYinghai Lu 	if (!release_resource(r)) {
1565cb21bc94SChristian König 		type = old_flags = r->flags & PCI_RES_TYPE_MASK;
156634c6b710SMohan Kumar 		pci_info(dev, "resource %d %pR released\n",
15675b285415SYinghai Lu 			 PCI_BRIDGE_RESOURCES + idx, r);
15680d607618SNicholas Johnson 		/* Keep the old size */
15695009b460SYinghai Lu 		r->end = resource_size(r) - 1;
15705009b460SYinghai Lu 		r->start = 0;
15715009b460SYinghai Lu 		r->flags = 0;
15725009b460SYinghai Lu 
15730d607618SNicholas Johnson 		/* Avoiding touch the one without PREF */
15745009b460SYinghai Lu 		if (type & IORESOURCE_PREFETCH)
15755009b460SYinghai Lu 			type = IORESOURCE_PREFETCH;
15765009b460SYinghai Lu 		__pci_setup_bridge(bus, type);
15770d607618SNicholas Johnson 		/* For next child res under same bridge */
15785b285415SYinghai Lu 		r->flags = old_flags;
15795009b460SYinghai Lu 	}
15805009b460SYinghai Lu }
15815009b460SYinghai Lu 
15825009b460SYinghai Lu enum release_type {
15835009b460SYinghai Lu 	leaf_only,
15845009b460SYinghai Lu 	whole_subtree,
15855009b460SYinghai Lu };
15860d607618SNicholas Johnson 
15875009b460SYinghai Lu /*
15880d607618SNicholas Johnson  * Try to release PCI bridge resources from leaf bridge, so we can allocate
15890d607618SNicholas Johnson  * a larger window later.
15905009b460SYinghai Lu  */
159110874f5aSBjorn Helgaas static void pci_bus_release_bridge_resources(struct pci_bus *bus,
15925009b460SYinghai Lu 					     unsigned long type,
15935009b460SYinghai Lu 					     enum release_type rel_type)
15945009b460SYinghai Lu {
15955009b460SYinghai Lu 	struct pci_dev *dev;
15965009b460SYinghai Lu 	bool is_leaf_bridge = true;
15975009b460SYinghai Lu 
15985009b460SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
15995009b460SYinghai Lu 		struct pci_bus *b = dev->subordinate;
16005009b460SYinghai Lu 		if (!b)
16015009b460SYinghai Lu 			continue;
16025009b460SYinghai Lu 
16035009b460SYinghai Lu 		is_leaf_bridge = false;
16045009b460SYinghai Lu 
16055009b460SYinghai Lu 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
16065009b460SYinghai Lu 			continue;
16075009b460SYinghai Lu 
16085009b460SYinghai Lu 		if (rel_type == whole_subtree)
16095009b460SYinghai Lu 			pci_bus_release_bridge_resources(b, type,
16105009b460SYinghai Lu 						 whole_subtree);
16115009b460SYinghai Lu 	}
16125009b460SYinghai Lu 
16135009b460SYinghai Lu 	if (pci_is_root_bus(bus))
16145009b460SYinghai Lu 		return;
16155009b460SYinghai Lu 
16165009b460SYinghai Lu 	if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
16175009b460SYinghai Lu 		return;
16185009b460SYinghai Lu 
16195009b460SYinghai Lu 	if ((rel_type == whole_subtree) || is_leaf_bridge)
16205009b460SYinghai Lu 		pci_bridge_release_resources(bus, type);
16215009b460SYinghai Lu }
16225009b460SYinghai Lu 
162376fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
162476fbc263SYinghai Lu {
162589a74eccSBjorn Helgaas 	struct resource *res;
162676fbc263SYinghai Lu 	int i;
162776fbc263SYinghai Lu 
162889a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
16297c9342b8SYinghai Lu 		if (!res || !res->end || !res->flags)
163076fbc263SYinghai Lu 			continue;
163176fbc263SYinghai Lu 
163234c6b710SMohan Kumar 		dev_info(&bus->dev, "resource %d %pR\n", i, res);
163376fbc263SYinghai Lu 	}
163476fbc263SYinghai Lu }
163576fbc263SYinghai Lu 
163676fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
163776fbc263SYinghai Lu {
163876fbc263SYinghai Lu 	struct pci_bus *b;
163976fbc263SYinghai Lu 	struct pci_dev *dev;
164076fbc263SYinghai Lu 
164176fbc263SYinghai Lu 
164276fbc263SYinghai Lu 	pci_bus_dump_res(bus);
164376fbc263SYinghai Lu 
164476fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
164576fbc263SYinghai Lu 		b = dev->subordinate;
164676fbc263SYinghai Lu 		if (!b)
164776fbc263SYinghai Lu 			continue;
164876fbc263SYinghai Lu 
164976fbc263SYinghai Lu 		pci_bus_dump_resources(b);
165076fbc263SYinghai Lu 	}
165176fbc263SYinghai Lu }
165276fbc263SYinghai Lu 
1653ff35147cSYinghai Lu static int pci_bus_get_depth(struct pci_bus *bus)
1654da7822e5SYinghai Lu {
1655da7822e5SYinghai Lu 	int depth = 0;
1656f2a230bdSWei Yang 	struct pci_bus *child_bus;
1657da7822e5SYinghai Lu 
1658f2a230bdSWei Yang 	list_for_each_entry(child_bus, &bus->children, node) {
1659da7822e5SYinghai Lu 		int ret;
1660da7822e5SYinghai Lu 
1661f2a230bdSWei Yang 		ret = pci_bus_get_depth(child_bus);
1662da7822e5SYinghai Lu 		if (ret + 1 > depth)
1663da7822e5SYinghai Lu 			depth = ret + 1;
1664da7822e5SYinghai Lu 	}
1665da7822e5SYinghai Lu 
1666da7822e5SYinghai Lu 	return depth;
1667da7822e5SYinghai Lu }
1668da7822e5SYinghai Lu 
1669b55438fdSYinghai Lu /*
1670b55438fdSYinghai Lu  * -1: undefined, will auto detect later
1671b55438fdSYinghai Lu  *  0: disabled by user
1672b55438fdSYinghai Lu  *  1: disabled by auto detect
1673b55438fdSYinghai Lu  *  2: enabled by user
1674b55438fdSYinghai Lu  *  3: enabled by auto detect
1675b55438fdSYinghai Lu  */
1676b55438fdSYinghai Lu enum enable_type {
1677b55438fdSYinghai Lu 	undefined = -1,
1678b55438fdSYinghai Lu 	user_disabled,
1679b55438fdSYinghai Lu 	auto_disabled,
1680b55438fdSYinghai Lu 	user_enabled,
1681b55438fdSYinghai Lu 	auto_enabled,
1682b55438fdSYinghai Lu };
1683b55438fdSYinghai Lu 
1684ff35147cSYinghai Lu static enum enable_type pci_realloc_enable = undefined;
1685b55438fdSYinghai Lu void __init pci_realloc_get_opt(char *str)
1686b55438fdSYinghai Lu {
1687b55438fdSYinghai Lu 	if (!strncmp(str, "off", 3))
1688b55438fdSYinghai Lu 		pci_realloc_enable = user_disabled;
1689b55438fdSYinghai Lu 	else if (!strncmp(str, "on", 2))
1690b55438fdSYinghai Lu 		pci_realloc_enable = user_enabled;
1691b55438fdSYinghai Lu }
1692ff35147cSYinghai Lu static bool pci_realloc_enabled(enum enable_type enable)
1693b55438fdSYinghai Lu {
1694967260cdSYinghai Lu 	return enable >= user_enabled;
1695b55438fdSYinghai Lu }
1696f483d392SRam Pai 
1697b07f2ebcSYinghai Lu #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1698ff35147cSYinghai Lu static int iov_resources_unassigned(struct pci_dev *dev, void *data)
1699223d96fcSYinghai Lu {
1700b07f2ebcSYinghai Lu 	int i;
1701223d96fcSYinghai Lu 	bool *unassigned = data;
1702b07f2ebcSYinghai Lu 
170339098edbSDenis Efremov 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
170439098edbSDenis Efremov 		struct resource *r = &dev->resource[i + PCI_IOV_RESOURCES];
1705fa216bf4SYinghai Lu 		struct pci_bus_region region;
1706b07f2ebcSYinghai Lu 
1707223d96fcSYinghai Lu 		/* Not assigned or rejected by kernel? */
1708fa216bf4SYinghai Lu 		if (!r->flags)
1709fa216bf4SYinghai Lu 			continue;
1710b07f2ebcSYinghai Lu 
1711fc279850SYinghai Lu 		pcibios_resource_to_bus(dev->bus, &region, r);
1712fa216bf4SYinghai Lu 		if (!region.start) {
1713223d96fcSYinghai Lu 			*unassigned = true;
17140d607618SNicholas Johnson 			return 1; /* Return early from pci_walk_bus() */
1715b07f2ebcSYinghai Lu 		}
1716b07f2ebcSYinghai Lu 	}
1717b07f2ebcSYinghai Lu 
1718223d96fcSYinghai Lu 	return 0;
1719223d96fcSYinghai Lu }
1720223d96fcSYinghai Lu 
1721ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1722967260cdSYinghai Lu 					   enum enable_type enable_local)
1723223d96fcSYinghai Lu {
1724223d96fcSYinghai Lu 	bool unassigned = false;
17257ac0d094SBenjamin Herrenschmidt 	struct pci_host_bridge *host;
1726223d96fcSYinghai Lu 
1727967260cdSYinghai Lu 	if (enable_local != undefined)
1728967260cdSYinghai Lu 		return enable_local;
1729223d96fcSYinghai Lu 
17307ac0d094SBenjamin Herrenschmidt 	host = pci_find_host_bridge(bus);
17317ac0d094SBenjamin Herrenschmidt 	if (host->preserve_config)
17327ac0d094SBenjamin Herrenschmidt 		return auto_disabled;
17337ac0d094SBenjamin Herrenschmidt 
1734223d96fcSYinghai Lu 	pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
1735967260cdSYinghai Lu 	if (unassigned)
1736967260cdSYinghai Lu 		return auto_enabled;
1737967260cdSYinghai Lu 
1738967260cdSYinghai Lu 	return enable_local;
1739b07f2ebcSYinghai Lu }
1740223d96fcSYinghai Lu #else
1741ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1742967260cdSYinghai Lu 					   enum enable_type enable_local)
1743967260cdSYinghai Lu {
1744967260cdSYinghai Lu 	return enable_local;
1745b07f2ebcSYinghai Lu }
1746b07f2ebcSYinghai Lu #endif
1747b07f2ebcSYinghai Lu 
17481e58f4e1SNicholas Johnson static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res,
17490d607618SNicholas Johnson 				 struct list_head *add_list,
17503d264da9SNicholas Johnson 				 resource_size_t new_size)
17511a576772SMika Westerberg {
175294867573SNicholas Johnson 	resource_size_t add_size, size = resource_size(res);
17531a576772SMika Westerberg 
17541a576772SMika Westerberg 	if (res->parent)
17551a576772SMika Westerberg 		return;
17561a576772SMika Westerberg 
175794867573SNicholas Johnson 	if (!new_size)
17581a576772SMika Westerberg 		return;
17591a576772SMika Westerberg 
176094867573SNicholas Johnson 	if (new_size > size) {
176194867573SNicholas Johnson 		add_size = new_size - size;
176294867573SNicholas Johnson 		pci_dbg(bridge, "bridge window %pR extended by %pa\n", res,
176394867573SNicholas Johnson 			&add_size);
176494867573SNicholas Johnson 	} else if (new_size < size) {
176594867573SNicholas Johnson 		add_size = size - new_size;
176694867573SNicholas Johnson 		pci_dbg(bridge, "bridge window %pR shrunken by %pa\n", res,
176794867573SNicholas Johnson 			&add_size);
1768*9db0b9b6SMika Westerberg 	} else {
1769*9db0b9b6SMika Westerberg 		return;
177094867573SNicholas Johnson 	}
177194867573SNicholas Johnson 
1772ae4611f1SNicholas Johnson 	res->end = res->start + new_size - 1;
1773ae4611f1SNicholas Johnson 	remove_from_list(add_list, res);
17741a576772SMika Westerberg }
17751a576772SMika Westerberg 
1776*9db0b9b6SMika Westerberg static void remove_dev_resource(struct resource *avail, struct pci_dev *dev,
1777*9db0b9b6SMika Westerberg 				struct resource *res)
1778*9db0b9b6SMika Westerberg {
1779*9db0b9b6SMika Westerberg 	resource_size_t size, align, tmp;
1780*9db0b9b6SMika Westerberg 
1781*9db0b9b6SMika Westerberg 	size = resource_size(res);
1782*9db0b9b6SMika Westerberg 	if (!size)
1783*9db0b9b6SMika Westerberg 		return;
1784*9db0b9b6SMika Westerberg 
1785*9db0b9b6SMika Westerberg 	align = pci_resource_alignment(dev, res);
1786*9db0b9b6SMika Westerberg 	align = align ? ALIGN(avail->start, align) - avail->start : 0;
1787*9db0b9b6SMika Westerberg 	tmp = align + size;
1788*9db0b9b6SMika Westerberg 	avail->start = min(avail->start + tmp, avail->end + 1);
1789*9db0b9b6SMika Westerberg }
1790*9db0b9b6SMika Westerberg 
1791*9db0b9b6SMika Westerberg static void remove_dev_resources(struct pci_dev *dev, struct resource *io,
1792*9db0b9b6SMika Westerberg 				 struct resource *mmio,
1793*9db0b9b6SMika Westerberg 				 struct resource *mmio_pref)
1794*9db0b9b6SMika Westerberg {
1795*9db0b9b6SMika Westerberg 	int i;
1796*9db0b9b6SMika Westerberg 
1797*9db0b9b6SMika Westerberg 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1798*9db0b9b6SMika Westerberg 		struct resource *res = &dev->resource[i];
1799*9db0b9b6SMika Westerberg 
1800*9db0b9b6SMika Westerberg 		if (resource_type(res) == IORESOURCE_IO) {
1801*9db0b9b6SMika Westerberg 			remove_dev_resource(io, dev, res);
1802*9db0b9b6SMika Westerberg 		} else if (resource_type(res) == IORESOURCE_MEM) {
1803*9db0b9b6SMika Westerberg 
1804*9db0b9b6SMika Westerberg 			/*
1805*9db0b9b6SMika Westerberg 			 * Make sure prefetchable memory is reduced from
1806*9db0b9b6SMika Westerberg 			 * the correct resource. Specifically we put 32-bit
1807*9db0b9b6SMika Westerberg 			 * prefetchable memory in non-prefetchable window
1808*9db0b9b6SMika Westerberg 			 * if there is an 64-bit pretchable window.
1809*9db0b9b6SMika Westerberg 			 *
1810*9db0b9b6SMika Westerberg 			 * See comments in __pci_bus_size_bridges() for
1811*9db0b9b6SMika Westerberg 			 * more information.
1812*9db0b9b6SMika Westerberg 			 */
1813*9db0b9b6SMika Westerberg 			if ((res->flags & IORESOURCE_PREFETCH) &&
1814*9db0b9b6SMika Westerberg 			    ((res->flags & IORESOURCE_MEM_64) ==
1815*9db0b9b6SMika Westerberg 			     (mmio_pref->flags & IORESOURCE_MEM_64)))
1816*9db0b9b6SMika Westerberg 				remove_dev_resource(mmio_pref, dev, res);
1817*9db0b9b6SMika Westerberg 			else
1818*9db0b9b6SMika Westerberg 				remove_dev_resource(mmio, dev, res);
1819*9db0b9b6SMika Westerberg 		}
1820*9db0b9b6SMika Westerberg 	}
1821*9db0b9b6SMika Westerberg }
1822*9db0b9b6SMika Westerberg 
1823*9db0b9b6SMika Westerberg /*
1824*9db0b9b6SMika Westerberg  * io, mmio and mmio_pref contain the total amount of bridge window space
1825*9db0b9b6SMika Westerberg  * available. This includes the minimal space needed to cover all the
1826*9db0b9b6SMika Westerberg  * existing devices on the bus and the possible extra space that can be
1827*9db0b9b6SMika Westerberg  * shared with the bridges.
1828*9db0b9b6SMika Westerberg  */
18291a576772SMika Westerberg static void pci_bus_distribute_available_resources(struct pci_bus *bus,
18300d607618SNicholas Johnson 					    struct list_head *add_list,
1831d555a50fSNicholas Johnson 					    struct resource io,
1832d555a50fSNicholas Johnson 					    struct resource mmio,
1833d555a50fSNicholas Johnson 					    struct resource mmio_pref)
18341a576772SMika Westerberg {
18351a576772SMika Westerberg 	unsigned int normal_bridges = 0, hotplug_bridges = 0;
18361a576772SMika Westerberg 	struct resource *io_res, *mmio_res, *mmio_pref_res;
18371a576772SMika Westerberg 	struct pci_dev *dev, *bridge = bus->self;
1838*9db0b9b6SMika Westerberg 	resource_size_t io_per_b, mmio_per_b, mmio_pref_per_b, align;
18391a576772SMika Westerberg 
18406e0688dbSKrzysztof Wilczynski 	io_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
18416e0688dbSKrzysztof Wilczynski 	mmio_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
18426e0688dbSKrzysztof Wilczynski 	mmio_pref_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
18431a576772SMika Westerberg 
18441a576772SMika Westerberg 	/*
1845f924c26eSNicholas Johnson 	 * The alignment of this bridge is yet to be considered, hence it must
1846f924c26eSNicholas Johnson 	 * be done now before extending its bridge window.
1847f924c26eSNicholas Johnson 	 */
1848f924c26eSNicholas Johnson 	align = pci_resource_alignment(bridge, io_res);
1849f924c26eSNicholas Johnson 	if (!io_res->parent && align)
1850f924c26eSNicholas Johnson 		io.start = min(ALIGN(io.start, align), io.end + 1);
1851f924c26eSNicholas Johnson 
1852f924c26eSNicholas Johnson 	align = pci_resource_alignment(bridge, mmio_res);
1853f924c26eSNicholas Johnson 	if (!mmio_res->parent && align)
1854f924c26eSNicholas Johnson 		mmio.start = min(ALIGN(mmio.start, align), mmio.end + 1);
1855f924c26eSNicholas Johnson 
1856f924c26eSNicholas Johnson 	align = pci_resource_alignment(bridge, mmio_pref_res);
1857f924c26eSNicholas Johnson 	if (!mmio_pref_res->parent && align)
1858f924c26eSNicholas Johnson 		mmio_pref.start = min(ALIGN(mmio_pref.start, align),
1859f924c26eSNicholas Johnson 			mmio_pref.end + 1);
1860f924c26eSNicholas Johnson 
1861f924c26eSNicholas Johnson 	/*
1862ae4611f1SNicholas Johnson 	 * Now that we have adjusted for alignment, update the bridge window
1863ae4611f1SNicholas Johnson 	 * resources to fill as much remaining resource space as possible.
18641a576772SMika Westerberg 	 */
18651e58f4e1SNicholas Johnson 	adjust_bridge_window(bridge, io_res, add_list, resource_size(&io));
18661e58f4e1SNicholas Johnson 	adjust_bridge_window(bridge, mmio_res, add_list, resource_size(&mmio));
18671e58f4e1SNicholas Johnson 	adjust_bridge_window(bridge, mmio_pref_res, add_list,
186877793854SNicholas Johnson 			     resource_size(&mmio_pref));
18691a576772SMika Westerberg 
18701a576772SMika Westerberg 	/*
18711a576772SMika Westerberg 	 * Calculate how many hotplug bridges and normal bridges there
18721a576772SMika Westerberg 	 * are on this bus.  We will distribute the additional available
18731a576772SMika Westerberg 	 * resources between hotplug bridges.
18741a576772SMika Westerberg 	 */
18751a576772SMika Westerberg 	for_each_pci_bridge(dev, bus) {
18761a576772SMika Westerberg 		if (dev->is_hotplug_bridge)
18771a576772SMika Westerberg 			hotplug_bridges++;
18781a576772SMika Westerberg 		else
18791a576772SMika Westerberg 			normal_bridges++;
18801a576772SMika Westerberg 	}
18811a576772SMika Westerberg 
1882*9db0b9b6SMika Westerberg 	if (!(hotplug_bridges + normal_bridges))
18836a381ea6SNicholas Johnson 		return;
18846a381ea6SNicholas Johnson 
18855c6bcc34SNicholas Johnson 	/*
1886*9db0b9b6SMika Westerberg 	 * Calculate the amount of space we can forward from "bus" to any
1887*9db0b9b6SMika Westerberg 	 * downstream buses, i.e., the space left over after assigning the
1888*9db0b9b6SMika Westerberg 	 * BARs and windows on "bus".
18895c6bcc34SNicholas Johnson 	 */
1890*9db0b9b6SMika Westerberg 	list_for_each_entry(dev, &bus->devices, bus_list) {
1891*9db0b9b6SMika Westerberg 		if (!dev->is_virtfn)
1892*9db0b9b6SMika Westerberg 			remove_dev_resources(dev, &io, &mmio, &mmio_pref);
18931a576772SMika Westerberg 	}
18941a576772SMika Westerberg 
1895*9db0b9b6SMika Westerberg 	/*
1896*9db0b9b6SMika Westerberg 	 * If there is at least one hotplug bridge on this bus it gets all
1897*9db0b9b6SMika Westerberg 	 * the extra resource space that was left after the reductions
1898*9db0b9b6SMika Westerberg 	 * above.
1899*9db0b9b6SMika Westerberg 	 *
1900*9db0b9b6SMika Westerberg 	 * If there are no hotplug bridges the extra resource space is
1901*9db0b9b6SMika Westerberg 	 * split between non-hotplug bridges. This is to allow possible
1902*9db0b9b6SMika Westerberg 	 * hotplug bridges below them to get the extra space as well.
1903*9db0b9b6SMika Westerberg 	 */
1904*9db0b9b6SMika Westerberg 	if (hotplug_bridges) {
1905*9db0b9b6SMika Westerberg 		io_per_b = div64_ul(resource_size(&io), hotplug_bridges);
1906*9db0b9b6SMika Westerberg 		mmio_per_b = div64_ul(resource_size(&mmio), hotplug_bridges);
1907*9db0b9b6SMika Westerberg 		mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
1908f924c26eSNicholas Johnson 					   hotplug_bridges);
1909*9db0b9b6SMika Westerberg 	} else {
1910*9db0b9b6SMika Westerberg 		io_per_b = div64_ul(resource_size(&io), normal_bridges);
1911*9db0b9b6SMika Westerberg 		mmio_per_b = div64_ul(resource_size(&mmio), normal_bridges);
1912*9db0b9b6SMika Westerberg 		mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
1913*9db0b9b6SMika Westerberg 					   normal_bridges);
1914*9db0b9b6SMika Westerberg 	}
1915f924c26eSNicholas Johnson 
19161a576772SMika Westerberg 	for_each_pci_bridge(dev, bus) {
191708f0a15eSMika Westerberg 		struct resource *res;
19181a576772SMika Westerberg 		struct pci_bus *b;
19191a576772SMika Westerberg 
19201a576772SMika Westerberg 		b = dev->subordinate;
1921*9db0b9b6SMika Westerberg 		if (!b)
1922*9db0b9b6SMika Westerberg 			continue;
1923*9db0b9b6SMika Westerberg 		if (hotplug_bridges && !dev->is_hotplug_bridge)
19241a576772SMika Westerberg 			continue;
19251a576772SMika Westerberg 
192608f0a15eSMika Westerberg 		res = &dev->resource[PCI_BRIDGE_IO_WINDOW];
1927*9db0b9b6SMika Westerberg 
1928*9db0b9b6SMika Westerberg 		/*
1929*9db0b9b6SMika Westerberg 		 * Make sure the split resource space is properly aligned
1930*9db0b9b6SMika Westerberg 		 * for bridge windows (align it down to avoid going above
1931*9db0b9b6SMika Westerberg 		 * what is available).
1932*9db0b9b6SMika Westerberg 		 */
193308f0a15eSMika Westerberg 		align = pci_resource_alignment(dev, res);
1934*9db0b9b6SMika Westerberg 		io.end = align ? io.start + ALIGN_DOWN(io_per_b, align) - 1
1935*9db0b9b6SMika Westerberg 			       : io.start + io_per_b - 1;
1936*9db0b9b6SMika Westerberg 
1937*9db0b9b6SMika Westerberg 		/*
1938*9db0b9b6SMika Westerberg 		 * The x_per_b holds the extra resource space that can be
1939*9db0b9b6SMika Westerberg 		 * added for each bridge but there is the minimal already
1940*9db0b9b6SMika Westerberg 		 * reserved as well so adjust x.start down accordingly to
1941*9db0b9b6SMika Westerberg 		 * cover the whole space.
1942*9db0b9b6SMika Westerberg 		 */
1943*9db0b9b6SMika Westerberg 		io.start -= resource_size(res);
194408f0a15eSMika Westerberg 
194508f0a15eSMika Westerberg 		res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
194608f0a15eSMika Westerberg 		align = pci_resource_alignment(dev, res);
1947*9db0b9b6SMika Westerberg 		mmio.end = align ? mmio.start + ALIGN_DOWN(mmio_per_b, align) - 1
1948*9db0b9b6SMika Westerberg 				 : mmio.start + mmio_per_b - 1;
1949*9db0b9b6SMika Westerberg 		mmio.start -= resource_size(res);
195008f0a15eSMika Westerberg 
195108f0a15eSMika Westerberg 		res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
195208f0a15eSMika Westerberg 		align = pci_resource_alignment(dev, res);
195308f0a15eSMika Westerberg 		mmio_pref.end = align ? mmio_pref.start +
1954*9db0b9b6SMika Westerberg 					ALIGN_DOWN(mmio_pref_per_b, align) - 1
1955*9db0b9b6SMika Westerberg 				      : mmio_pref.start + mmio_pref_per_b - 1;
1956*9db0b9b6SMika Westerberg 		mmio_pref.start -= resource_size(res);
1957d555a50fSNicholas Johnson 
1958d555a50fSNicholas Johnson 		pci_bus_distribute_available_resources(b, add_list, io, mmio,
1959d555a50fSNicholas Johnson 						       mmio_pref);
1960f924c26eSNicholas Johnson 
196108f0a15eSMika Westerberg 		io.start += io.end + 1;
196208f0a15eSMika Westerberg 		mmio.start += mmio.end + 1;
196308f0a15eSMika Westerberg 		mmio_pref.start += mmio_pref.end + 1;
19641a576772SMika Westerberg 	}
19651a576772SMika Westerberg }
19661a576772SMika Westerberg 
19670d607618SNicholas Johnson static void pci_bridge_distribute_available_resources(struct pci_dev *bridge,
19681a576772SMika Westerberg 						      struct list_head *add_list)
19691a576772SMika Westerberg {
1970d555a50fSNicholas Johnson 	struct resource available_io, available_mmio, available_mmio_pref;
19711a576772SMika Westerberg 
19721a576772SMika Westerberg 	if (!bridge->is_hotplug_bridge)
19731a576772SMika Westerberg 		return;
19741a576772SMika Westerberg 
19751a576772SMika Westerberg 	/* Take the initial extra resources from the hotplug port */
19766e0688dbSKrzysztof Wilczynski 	available_io = bridge->resource[PCI_BRIDGE_IO_WINDOW];
19776e0688dbSKrzysztof Wilczynski 	available_mmio = bridge->resource[PCI_BRIDGE_MEM_WINDOW];
19786e0688dbSKrzysztof Wilczynski 	available_mmio_pref = bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
19791a576772SMika Westerberg 
19801a576772SMika Westerberg 	pci_bus_distribute_available_resources(bridge->subordinate,
19810d607618SNicholas Johnson 					       add_list, available_io,
19820d607618SNicholas Johnson 					       available_mmio,
19830d607618SNicholas Johnson 					       available_mmio_pref);
19841a576772SMika Westerberg }
19851a576772SMika Westerberg 
1986d1caf229SMika Westerberg /*
1987d1caf229SMika Westerberg  * First try will not touch PCI bridge res.
1988d1caf229SMika Westerberg  * Second and later try will clear small leaf bridge res.
1989d1caf229SMika Westerberg  * Will stop till to the max depth if can not find good one.
1990d1caf229SMika Westerberg  */
1991d1caf229SMika Westerberg void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
1992d1caf229SMika Westerberg {
1993d1caf229SMika Westerberg 	LIST_HEAD(realloc_head);
1994d1caf229SMika Westerberg 	/* List of resources that want additional resources */
1995d1caf229SMika Westerberg 	struct list_head *add_list = NULL;
1996d1caf229SMika Westerberg 	int tried_times = 0;
1997d1caf229SMika Westerberg 	enum release_type rel_type = leaf_only;
1998d1caf229SMika Westerberg 	LIST_HEAD(fail_head);
1999d1caf229SMika Westerberg 	struct pci_dev_resource *fail_res;
2000d1caf229SMika Westerberg 	int pci_try_num = 1;
2001d1caf229SMika Westerberg 	enum enable_type enable_local;
2002d1caf229SMika Westerberg 
2003d1caf229SMika Westerberg 	/* Don't realloc if asked to do so */
2004d1caf229SMika Westerberg 	enable_local = pci_realloc_detect(bus, pci_realloc_enable);
2005d1caf229SMika Westerberg 	if (pci_realloc_enabled(enable_local)) {
2006d1caf229SMika Westerberg 		int max_depth = pci_bus_get_depth(bus);
2007d1caf229SMika Westerberg 
2008d1caf229SMika Westerberg 		pci_try_num = max_depth + 1;
2009d1caf229SMika Westerberg 		dev_info(&bus->dev, "max bus depth: %d pci_try_num: %d\n",
2010d1caf229SMika Westerberg 			 max_depth, pci_try_num);
2011d1caf229SMika Westerberg 	}
2012d1caf229SMika Westerberg 
2013d1caf229SMika Westerberg again:
2014d1caf229SMika Westerberg 	/*
2015d1caf229SMika Westerberg 	 * Last try will use add_list, otherwise will try good to have as must
2016d1caf229SMika Westerberg 	 * have, so can realloc parent bridge resource
2017d1caf229SMika Westerberg 	 */
2018d1caf229SMika Westerberg 	if (tried_times + 1 == pci_try_num)
2019d1caf229SMika Westerberg 		add_list = &realloc_head;
2020d1caf229SMika Westerberg 	/*
2021d1caf229SMika Westerberg 	 * Depth first, calculate sizes and alignments of all subordinate buses.
2022d1caf229SMika Westerberg 	 */
2023d1caf229SMika Westerberg 	__pci_bus_size_bridges(bus, add_list);
2024d1caf229SMika Westerberg 
2025d1caf229SMika Westerberg 	/* Depth last, allocate resources and update the hardware. */
2026d1caf229SMika Westerberg 	__pci_bus_assign_resources(bus, add_list, &fail_head);
2027d1caf229SMika Westerberg 	if (add_list)
2028d1caf229SMika Westerberg 		BUG_ON(!list_empty(add_list));
2029d1caf229SMika Westerberg 	tried_times++;
2030d1caf229SMika Westerberg 
2031d1caf229SMika Westerberg 	/* Any device complain? */
2032d1caf229SMika Westerberg 	if (list_empty(&fail_head))
2033d1caf229SMika Westerberg 		goto dump;
2034d1caf229SMika Westerberg 
2035d1caf229SMika Westerberg 	if (tried_times >= pci_try_num) {
2036d1caf229SMika Westerberg 		if (enable_local == undefined)
2037d1caf229SMika Westerberg 			dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
2038d1caf229SMika Westerberg 		else if (enable_local == auto_enabled)
2039d1caf229SMika Westerberg 			dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
2040d1caf229SMika Westerberg 
2041d1caf229SMika Westerberg 		free_list(&fail_head);
2042d1caf229SMika Westerberg 		goto dump;
2043d1caf229SMika Westerberg 	}
2044d1caf229SMika Westerberg 
2045d1caf229SMika Westerberg 	dev_info(&bus->dev, "No. %d try to assign unassigned res\n",
2046d1caf229SMika Westerberg 		 tried_times + 1);
2047d1caf229SMika Westerberg 
2048d1caf229SMika Westerberg 	/* Third times and later will not check if it is leaf */
2049d1caf229SMika Westerberg 	if ((tried_times + 1) > 2)
2050d1caf229SMika Westerberg 		rel_type = whole_subtree;
2051d1caf229SMika Westerberg 
2052d1caf229SMika Westerberg 	/*
2053d1caf229SMika Westerberg 	 * Try to release leaf bridge's resources that doesn't fit resource of
2054d1caf229SMika Westerberg 	 * child device under that bridge.
2055d1caf229SMika Westerberg 	 */
2056d1caf229SMika Westerberg 	list_for_each_entry(fail_res, &fail_head, list)
2057d1caf229SMika Westerberg 		pci_bus_release_bridge_resources(fail_res->dev->bus,
2058d1caf229SMika Westerberg 						 fail_res->flags & PCI_RES_TYPE_MASK,
2059d1caf229SMika Westerberg 						 rel_type);
2060d1caf229SMika Westerberg 
2061d1caf229SMika Westerberg 	/* Restore size and flags */
2062d1caf229SMika Westerberg 	list_for_each_entry(fail_res, &fail_head, list) {
2063d1caf229SMika Westerberg 		struct resource *res = fail_res->res;
2064d1caf229SMika Westerberg 		int idx;
2065d1caf229SMika Westerberg 
2066d1caf229SMika Westerberg 		res->start = fail_res->start;
2067d1caf229SMika Westerberg 		res->end = fail_res->end;
2068d1caf229SMika Westerberg 		res->flags = fail_res->flags;
2069d1caf229SMika Westerberg 
2070d1caf229SMika Westerberg 		if (pci_is_bridge(fail_res->dev)) {
2071d1caf229SMika Westerberg 			idx = res - &fail_res->dev->resource[0];
2072d1caf229SMika Westerberg 			if (idx >= PCI_BRIDGE_RESOURCES &&
2073d1caf229SMika Westerberg 			    idx <= PCI_BRIDGE_RESOURCE_END)
2074d1caf229SMika Westerberg 				res->flags = 0;
2075d1caf229SMika Westerberg 		}
2076d1caf229SMika Westerberg 	}
2077d1caf229SMika Westerberg 	free_list(&fail_head);
2078d1caf229SMika Westerberg 
2079d1caf229SMika Westerberg 	goto again;
2080d1caf229SMika Westerberg 
2081d1caf229SMika Westerberg dump:
2082d1caf229SMika Westerberg 	/* Dump the resource on buses */
2083d1caf229SMika Westerberg 	pci_bus_dump_resources(bus);
2084d1caf229SMika Westerberg }
2085d1caf229SMika Westerberg 
2086d1caf229SMika Westerberg void __init pci_assign_unassigned_resources(void)
2087d1caf229SMika Westerberg {
2088d1caf229SMika Westerberg 	struct pci_bus *root_bus;
2089d1caf229SMika Westerberg 
2090d1caf229SMika Westerberg 	list_for_each_entry(root_bus, &pci_root_buses, node) {
2091d1caf229SMika Westerberg 		pci_assign_unassigned_root_bus_resources(root_bus);
2092d1caf229SMika Westerberg 
2093d1caf229SMika Westerberg 		/* Make sure the root bridge has a companion ACPI device */
2094d1caf229SMika Westerberg 		if (ACPI_HANDLE(root_bus->bridge))
2095d1caf229SMika Westerberg 			acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
2096d1caf229SMika Westerberg 	}
2097d1caf229SMika Westerberg }
2098d1caf229SMika Westerberg 
20996841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
21006841ec68SYinghai Lu {
21016841ec68SYinghai Lu 	struct pci_bus *parent = bridge->subordinate;
21020d607618SNicholas Johnson 	/* List of resources that want additional resources */
21030d607618SNicholas Johnson 	LIST_HEAD(add_list);
21040d607618SNicholas Johnson 
210532180e40SYinghai Lu 	int tried_times = 0;
2106bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
2107b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
21086841ec68SYinghai Lu 	int retval;
21096841ec68SYinghai Lu 
211032180e40SYinghai Lu again:
21118424d759SYinghai Lu 	__pci_bus_size_bridges(parent, &add_list);
21121a576772SMika Westerberg 
21131a576772SMika Westerberg 	/*
21140d607618SNicholas Johnson 	 * Distribute remaining resources (if any) equally between hotplug
21150d607618SNicholas Johnson 	 * bridges below.  This makes it possible to extend the hierarchy
21160d607618SNicholas Johnson 	 * later without running out of resources.
21171a576772SMika Westerberg 	 */
21181a576772SMika Westerberg 	pci_bridge_distribute_available_resources(bridge, &add_list);
21191a576772SMika Westerberg 
2120bdc4abecSYinghai Lu 	__pci_bridge_assign_resources(bridge, &add_list, &fail_head);
2121bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
212232180e40SYinghai Lu 	tried_times++;
212332180e40SYinghai Lu 
2124bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
21253f579c34SYinghai Lu 		goto enable_all;
212632180e40SYinghai Lu 
212732180e40SYinghai Lu 	if (tried_times >= 2) {
21280d607618SNicholas Johnson 		/* Still fail, don't need to try more */
2129bffc56d4SYinghai Lu 		free_list(&fail_head);
21303f579c34SYinghai Lu 		goto enable_all;
213132180e40SYinghai Lu 	}
213232180e40SYinghai Lu 
213332180e40SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
213432180e40SYinghai Lu 			 tried_times + 1);
213532180e40SYinghai Lu 
213632180e40SYinghai Lu 	/*
21370d607618SNicholas Johnson 	 * Try to release leaf bridge's resources that aren't big enough
21380d607618SNicholas Johnson 	 * to contain child device resources.
213932180e40SYinghai Lu 	 */
214061e83cddSYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list)
214161e83cddSYinghai Lu 		pci_bus_release_bridge_resources(fail_res->dev->bus,
2142cb21bc94SChristian König 						 fail_res->flags & PCI_RES_TYPE_MASK,
214332180e40SYinghai Lu 						 whole_subtree);
214461e83cddSYinghai Lu 
21450d607618SNicholas Johnson 	/* Restore size and flags */
2146b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
2147b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
21489db8dc6dSLogan Gunthorpe 		int idx;
214932180e40SYinghai Lu 
2150b9b0bba9SYinghai Lu 		res->start = fail_res->start;
2151b9b0bba9SYinghai Lu 		res->end = fail_res->end;
2152b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
21539db8dc6dSLogan Gunthorpe 
21549db8dc6dSLogan Gunthorpe 		if (pci_is_bridge(fail_res->dev)) {
21559db8dc6dSLogan Gunthorpe 			idx = res - &fail_res->dev->resource[0];
21569db8dc6dSLogan Gunthorpe 			if (idx >= PCI_BRIDGE_RESOURCES &&
21579db8dc6dSLogan Gunthorpe 			    idx <= PCI_BRIDGE_RESOURCE_END)
215832180e40SYinghai Lu 				res->flags = 0;
215932180e40SYinghai Lu 		}
21609db8dc6dSLogan Gunthorpe 	}
2161bffc56d4SYinghai Lu 	free_list(&fail_head);
216232180e40SYinghai Lu 
216332180e40SYinghai Lu 	goto again;
21643f579c34SYinghai Lu 
21653f579c34SYinghai Lu enable_all:
21663f579c34SYinghai Lu 	retval = pci_reenable_device(bridge);
21679fc9eea0SBjorn Helgaas 	if (retval)
21687506dc79SFrederick Lawler 		pci_err(bridge, "Error reenabling bridge (%d)\n", retval);
21693f579c34SYinghai Lu 	pci_set_master(bridge);
21706841ec68SYinghai Lu }
21716841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
21729b03088fSYinghai Lu 
21738bb705e3SChristian König int pci_reassign_bridge_resources(struct pci_dev *bridge, unsigned long type)
21748bb705e3SChristian König {
21758bb705e3SChristian König 	struct pci_dev_resource *dev_res;
21768bb705e3SChristian König 	struct pci_dev *next;
21778bb705e3SChristian König 	LIST_HEAD(saved);
21788bb705e3SChristian König 	LIST_HEAD(added);
21798bb705e3SChristian König 	LIST_HEAD(failed);
21808bb705e3SChristian König 	unsigned int i;
21818bb705e3SChristian König 	int ret;
21828bb705e3SChristian König 
2183fb794a70SBenjamin Herrenschmidt 	down_read(&pci_bus_sem);
2184fb794a70SBenjamin Herrenschmidt 
21858bb705e3SChristian König 	/* Walk to the root hub, releasing bridge BARs when possible */
21868bb705e3SChristian König 	next = bridge;
21878bb705e3SChristian König 	do {
21888bb705e3SChristian König 		bridge = next;
21898bb705e3SChristian König 		for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCE_END;
21908bb705e3SChristian König 		     i++) {
21918bb705e3SChristian König 			struct resource *res = &bridge->resource[i];
21928bb705e3SChristian König 
21938bb705e3SChristian König 			if ((res->flags ^ type) & PCI_RES_TYPE_MASK)
21948bb705e3SChristian König 				continue;
21958bb705e3SChristian König 
21968bb705e3SChristian König 			/* Ignore BARs which are still in use */
21978bb705e3SChristian König 			if (res->child)
21988bb705e3SChristian König 				continue;
21998bb705e3SChristian König 
22008bb705e3SChristian König 			ret = add_to_list(&saved, bridge, res, 0, 0);
22018bb705e3SChristian König 			if (ret)
22028bb705e3SChristian König 				goto cleanup;
22038bb705e3SChristian König 
22047506dc79SFrederick Lawler 			pci_info(bridge, "BAR %d: releasing %pR\n",
22058bb705e3SChristian König 				 i, res);
22068bb705e3SChristian König 
22078bb705e3SChristian König 			if (res->parent)
22088bb705e3SChristian König 				release_resource(res);
22098bb705e3SChristian König 			res->start = 0;
22108bb705e3SChristian König 			res->end = 0;
22118bb705e3SChristian König 			break;
22128bb705e3SChristian König 		}
22138bb705e3SChristian König 		if (i == PCI_BRIDGE_RESOURCE_END)
22148bb705e3SChristian König 			break;
22158bb705e3SChristian König 
22168bb705e3SChristian König 		next = bridge->bus ? bridge->bus->self : NULL;
22178bb705e3SChristian König 	} while (next);
22188bb705e3SChristian König 
2219fb794a70SBenjamin Herrenschmidt 	if (list_empty(&saved)) {
2220fb794a70SBenjamin Herrenschmidt 		up_read(&pci_bus_sem);
22218bb705e3SChristian König 		return -ENOENT;
2222fb794a70SBenjamin Herrenschmidt 	}
22238bb705e3SChristian König 
22248bb705e3SChristian König 	__pci_bus_size_bridges(bridge->subordinate, &added);
22258bb705e3SChristian König 	__pci_bridge_assign_resources(bridge, &added, &failed);
22268bb705e3SChristian König 	BUG_ON(!list_empty(&added));
22278bb705e3SChristian König 
22288bb705e3SChristian König 	if (!list_empty(&failed)) {
22298bb705e3SChristian König 		ret = -ENOSPC;
22308bb705e3SChristian König 		goto cleanup;
22318bb705e3SChristian König 	}
22328bb705e3SChristian König 
22338bb705e3SChristian König 	list_for_each_entry(dev_res, &saved, list) {
22340d607618SNicholas Johnson 		/* Skip the bridge we just assigned resources for */
22358bb705e3SChristian König 		if (bridge == dev_res->dev)
22368bb705e3SChristian König 			continue;
22378bb705e3SChristian König 
22388bb705e3SChristian König 		bridge = dev_res->dev;
22398bb705e3SChristian König 		pci_setup_bridge(bridge->subordinate);
22408bb705e3SChristian König 	}
22418bb705e3SChristian König 
22428bb705e3SChristian König 	free_list(&saved);
2243fb794a70SBenjamin Herrenschmidt 	up_read(&pci_bus_sem);
22448bb705e3SChristian König 	return 0;
22458bb705e3SChristian König 
22468bb705e3SChristian König cleanup:
22470d607618SNicholas Johnson 	/* Restore size and flags */
22488bb705e3SChristian König 	list_for_each_entry(dev_res, &failed, list) {
22498bb705e3SChristian König 		struct resource *res = dev_res->res;
22508bb705e3SChristian König 
22518bb705e3SChristian König 		res->start = dev_res->start;
22528bb705e3SChristian König 		res->end = dev_res->end;
22538bb705e3SChristian König 		res->flags = dev_res->flags;
22548bb705e3SChristian König 	}
22558bb705e3SChristian König 	free_list(&failed);
22568bb705e3SChristian König 
22578bb705e3SChristian König 	/* Revert to the old configuration */
22588bb705e3SChristian König 	list_for_each_entry(dev_res, &saved, list) {
22598bb705e3SChristian König 		struct resource *res = dev_res->res;
22608bb705e3SChristian König 
22618bb705e3SChristian König 		bridge = dev_res->dev;
22628bb705e3SChristian König 		i = res - bridge->resource;
22638bb705e3SChristian König 
22648bb705e3SChristian König 		res->start = dev_res->start;
22658bb705e3SChristian König 		res->end = dev_res->end;
22668bb705e3SChristian König 		res->flags = dev_res->flags;
22678bb705e3SChristian König 
22688bb705e3SChristian König 		pci_claim_resource(bridge, i);
22698bb705e3SChristian König 		pci_setup_bridge(bridge->subordinate);
22708bb705e3SChristian König 	}
22718bb705e3SChristian König 	free_list(&saved);
2272fb794a70SBenjamin Herrenschmidt 	up_read(&pci_bus_sem);
22738bb705e3SChristian König 
22748bb705e3SChristian König 	return ret;
22758bb705e3SChristian König }
22768bb705e3SChristian König 
227717787940SYinghai Lu void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
22789b03088fSYinghai Lu {
22799b03088fSYinghai Lu 	struct pci_dev *dev;
22800d607618SNicholas Johnson 	/* List of resources that want additional resources */
22810d607618SNicholas Johnson 	LIST_HEAD(add_list);
22829b03088fSYinghai Lu 
22839b03088fSYinghai Lu 	down_read(&pci_bus_sem);
228424a0c654SAndy Shevchenko 	for_each_pci_bridge(dev, bus)
228524a0c654SAndy Shevchenko 		if (pci_has_subordinate(dev))
228624a0c654SAndy Shevchenko 			__pci_bus_size_bridges(dev->subordinate, &add_list);
22879b03088fSYinghai Lu 	up_read(&pci_bus_sem);
22889b03088fSYinghai Lu 	__pci_bus_assign_resources(bus, &add_list, NULL);
2289bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
229017787940SYinghai Lu }
2291e6b29deaSRay Jui EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);
2292