xref: /openbmc/linux/drivers/pci/setup-bus.c (revision 09cc900632400079619e9154604fd299c2cc9a5a)
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 
12778c3b329SYinghai Lu /* Sort resources by alignment */
128bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
12978c3b329SYinghai Lu {
130*09cc9006SMika Westerberg 	struct resource *r;
13178c3b329SYinghai Lu 	int i;
13278c3b329SYinghai Lu 
133*09cc9006SMika Westerberg 	pci_dev_for_each_resource(dev, r, i) {
134bdc4abecSYinghai Lu 		struct pci_dev_resource *dev_res, *tmp;
13578c3b329SYinghai Lu 		resource_size_t r_align;
136bdc4abecSYinghai Lu 		struct list_head *n;
13778c3b329SYinghai Lu 
13878c3b329SYinghai Lu 		if (r->flags & IORESOURCE_PCI_FIXED)
13978c3b329SYinghai Lu 			continue;
14078c3b329SYinghai Lu 
14178c3b329SYinghai Lu 		if (!(r->flags) || r->parent)
14278c3b329SYinghai Lu 			continue;
14378c3b329SYinghai Lu 
14478c3b329SYinghai Lu 		r_align = pci_resource_alignment(dev, r);
14578c3b329SYinghai Lu 		if (!r_align) {
1467506dc79SFrederick Lawler 			pci_warn(dev, "BAR %d: %pR has bogus alignment\n",
14778c3b329SYinghai Lu 				 i, r);
14878c3b329SYinghai Lu 			continue;
14978c3b329SYinghai Lu 		}
15078c3b329SYinghai Lu 
151bdc4abecSYinghai Lu 		tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
15278c3b329SYinghai Lu 		if (!tmp)
153c7c337c5SLiao Pingfang 			panic("%s: kzalloc() failed!\n", __func__);
15478c3b329SYinghai Lu 		tmp->res = r;
15578c3b329SYinghai Lu 		tmp->dev = dev;
156bdc4abecSYinghai Lu 
1570d607618SNicholas Johnson 		/* Fallback is smallest one or list is empty */
158bdc4abecSYinghai Lu 		n = head;
159bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
160bdc4abecSYinghai Lu 			resource_size_t align;
161bdc4abecSYinghai Lu 
162bdc4abecSYinghai Lu 			align = pci_resource_alignment(dev_res->dev,
163bdc4abecSYinghai Lu 							 dev_res->res);
164bdc4abecSYinghai Lu 
165bdc4abecSYinghai Lu 			if (r_align > align) {
166bdc4abecSYinghai Lu 				n = &dev_res->list;
16778c3b329SYinghai Lu 				break;
16878c3b329SYinghai Lu 			}
16978c3b329SYinghai Lu 		}
170bdc4abecSYinghai Lu 		/* Insert it just before n */
171bdc4abecSYinghai Lu 		list_add_tail(&tmp->list, n);
17278c3b329SYinghai Lu 	}
17378c3b329SYinghai Lu }
17478c3b329SYinghai Lu 
1750d607618SNicholas Johnson static void __dev_sort_resources(struct pci_dev *dev, struct list_head *head)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	u16 class = dev->class >> 8;
1781da177e4SLinus Torvalds 
1790d607618SNicholas Johnson 	/* Don't touch classless devices or host bridges or IOAPICs */
1806841ec68SYinghai Lu 	if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
1816841ec68SYinghai Lu 		return;
1821da177e4SLinus Torvalds 
1830d607618SNicholas Johnson 	/* Don't touch IOAPIC devices already enabled by firmware */
18423186279SSatoru Takeuchi 	if (class == PCI_CLASS_SYSTEM_PIC) {
1859bded00bSKenji Kaneshige 		u16 command;
1869bded00bSKenji Kaneshige 		pci_read_config_word(dev, PCI_COMMAND, &command);
1879bded00bSKenji Kaneshige 		if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
1886841ec68SYinghai Lu 			return;
18923186279SSatoru Takeuchi 	}
19023186279SSatoru Takeuchi 
1916841ec68SYinghai Lu 	pdev_sort_resources(dev, head);
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
194fc075e1dSRam Pai static inline void reset_resource(struct resource *res)
195fc075e1dSRam Pai {
196fc075e1dSRam Pai 	res->start = 0;
197fc075e1dSRam Pai 	res->end = 0;
198fc075e1dSRam Pai 	res->flags = 0;
199fc075e1dSRam Pai }
200fc075e1dSRam Pai 
201c8adf9a3SRam Pai /**
2020d607618SNicholas Johnson  * reassign_resources_sorted() - Satisfy any additional resource requests
203c8adf9a3SRam Pai  *
2040d607618SNicholas Johnson  * @realloc_head:	Head of the list tracking requests requiring
2050d607618SNicholas Johnson  *			additional resources
2060d607618SNicholas Johnson  * @head:		Head of the list tracking requests with allocated
207c8adf9a3SRam Pai  *			resources
208c8adf9a3SRam Pai  *
2090d607618SNicholas Johnson  * Walk through each element of the realloc_head and try to procure additional
2100d607618SNicholas Johnson  * resources for the element, provided the element is in the head list.
211c8adf9a3SRam Pai  */
212bdc4abecSYinghai Lu static void reassign_resources_sorted(struct list_head *realloc_head,
213bdc4abecSYinghai Lu 				      struct list_head *head)
214c8adf9a3SRam Pai {
215c8adf9a3SRam Pai 	struct resource *res;
216b9b0bba9SYinghai Lu 	struct pci_dev_resource *add_res, *tmp;
217bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
218d74b9027SWei Yang 	resource_size_t add_size, align;
219c8adf9a3SRam Pai 	int idx;
220c8adf9a3SRam Pai 
221b9b0bba9SYinghai Lu 	list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
222bdc4abecSYinghai Lu 		bool found_match = false;
223bdc4abecSYinghai Lu 
224b9b0bba9SYinghai Lu 		res = add_res->res;
2250d607618SNicholas Johnson 		/* Skip resource that has been reset */
226c8adf9a3SRam Pai 		if (!res->flags)
227c8adf9a3SRam Pai 			goto out;
228c8adf9a3SRam Pai 
2290d607618SNicholas Johnson 		/* Skip this resource if not found in head list */
230bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
231bdc4abecSYinghai Lu 			if (dev_res->res == res) {
232bdc4abecSYinghai Lu 				found_match = true;
233bdc4abecSYinghai Lu 				break;
234c8adf9a3SRam Pai 			}
235bdc4abecSYinghai Lu 		}
2360d607618SNicholas Johnson 		if (!found_match) /* Just skip */
237bdc4abecSYinghai Lu 			continue;
238c8adf9a3SRam Pai 
239b9b0bba9SYinghai Lu 		idx = res - &add_res->dev->resource[0];
240b9b0bba9SYinghai Lu 		add_size = add_res->add_size;
241d74b9027SWei Yang 		align = add_res->min_align;
2422bbc6942SRam Pai 		if (!resource_size(res)) {
243d74b9027SWei Yang 			res->start = align;
244c8adf9a3SRam Pai 			res->end = res->start + add_size - 1;
245b9b0bba9SYinghai Lu 			if (pci_assign_resource(add_res->dev, idx))
246c8adf9a3SRam Pai 				reset_resource(res);
2472bbc6942SRam Pai 		} else {
248b9b0bba9SYinghai Lu 			res->flags |= add_res->flags &
249bdc4abecSYinghai Lu 				 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
250b9b0bba9SYinghai Lu 			if (pci_reassign_resource(add_res->dev, idx,
251bdc4abecSYinghai Lu 						  add_size, align))
25234c6b710SMohan Kumar 				pci_info(add_res->dev, "failed to add %llx res[%d]=%pR\n",
25334c6b710SMohan Kumar 					 (unsigned long long) add_size, idx,
25434c6b710SMohan Kumar 					 res);
255c8adf9a3SRam Pai 		}
256c8adf9a3SRam Pai out:
257b9b0bba9SYinghai Lu 		list_del(&add_res->list);
258b9b0bba9SYinghai Lu 		kfree(add_res);
259c8adf9a3SRam Pai 	}
260c8adf9a3SRam Pai }
261c8adf9a3SRam Pai 
262c8adf9a3SRam Pai /**
2630d607618SNicholas Johnson  * assign_requested_resources_sorted() - Satisfy resource requests
264c8adf9a3SRam Pai  *
2650d607618SNicholas Johnson  * @head:	Head of the list tracking requests for resources
2660d607618SNicholas Johnson  * @fail_head:	Head of the list tracking requests that could not be
2670d607618SNicholas Johnson  *		allocated
268c8adf9a3SRam Pai  *
2690d607618SNicholas Johnson  * Satisfy resource requests of each element in the list.  Add requests that
2700d607618SNicholas Johnson  * could not be satisfied to the failed_list.
271c8adf9a3SRam Pai  */
272bdc4abecSYinghai Lu static void assign_requested_resources_sorted(struct list_head *head,
273bdc4abecSYinghai Lu 				 struct list_head *fail_head)
2746841ec68SYinghai Lu {
2756841ec68SYinghai Lu 	struct resource *res;
276bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
2776841ec68SYinghai Lu 	int idx;
2786841ec68SYinghai Lu 
279bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
280bdc4abecSYinghai Lu 		res = dev_res->res;
281bdc4abecSYinghai Lu 		idx = res - &dev_res->dev->resource[0];
282bdc4abecSYinghai Lu 		if (resource_size(res) &&
283bdc4abecSYinghai Lu 		    pci_assign_resource(dev_res->dev, idx)) {
284a3cb999dSYinghai Lu 			if (fail_head) {
2859a928660SYinghai Lu 				/*
2860d607618SNicholas Johnson 				 * If the failed resource is a ROM BAR and
2870d607618SNicholas Johnson 				 * it will be enabled later, don't add it
2880d607618SNicholas Johnson 				 * to the list.
2899a928660SYinghai Lu 				 */
2909a928660SYinghai Lu 				if (!((idx == PCI_ROM_RESOURCE) &&
2919a928660SYinghai Lu 				      (!(res->flags & IORESOURCE_ROM_ENABLE))))
29267cc7e26SYinghai Lu 					add_to_list(fail_head,
29367cc7e26SYinghai Lu 						    dev_res->dev, res,
294f7625980SBjorn Helgaas 						    0 /* don't care */,
295f7625980SBjorn Helgaas 						    0 /* don't care */);
2969a928660SYinghai Lu 			}
297fc075e1dSRam Pai 			reset_resource(res);
298542df5deSRajesh Shah 		}
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
302aa914f5eSYinghai Lu static unsigned long pci_fail_res_type_mask(struct list_head *fail_head)
303aa914f5eSYinghai Lu {
304aa914f5eSYinghai Lu 	struct pci_dev_resource *fail_res;
305aa914f5eSYinghai Lu 	unsigned long mask = 0;
306aa914f5eSYinghai Lu 
3070d607618SNicholas Johnson 	/* Check failed type */
308aa914f5eSYinghai Lu 	list_for_each_entry(fail_res, fail_head, list)
309aa914f5eSYinghai Lu 		mask |= fail_res->flags;
310aa914f5eSYinghai Lu 
311aa914f5eSYinghai Lu 	/*
3120d607618SNicholas Johnson 	 * One pref failed resource will set IORESOURCE_MEM, as we can
3130d607618SNicholas Johnson 	 * allocate pref in non-pref range.  Will release all assigned
3140d607618SNicholas Johnson 	 * non-pref sibling resources according to that bit.
315aa914f5eSYinghai Lu 	 */
316aa914f5eSYinghai Lu 	return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
317aa914f5eSYinghai Lu }
318aa914f5eSYinghai Lu 
319aa914f5eSYinghai Lu static bool pci_need_to_release(unsigned long mask, struct resource *res)
320aa914f5eSYinghai Lu {
321aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_IO)
322aa914f5eSYinghai Lu 		return !!(mask & IORESOURCE_IO);
323aa914f5eSYinghai Lu 
3240d607618SNicholas Johnson 	/* Check pref at first */
325aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_PREFETCH) {
326aa914f5eSYinghai Lu 		if (mask & IORESOURCE_PREFETCH)
327aa914f5eSYinghai Lu 			return true;
3280d607618SNicholas Johnson 		/* Count pref if its parent is non-pref */
329aa914f5eSYinghai Lu 		else if ((mask & IORESOURCE_MEM) &&
330aa914f5eSYinghai Lu 			 !(res->parent->flags & IORESOURCE_PREFETCH))
331aa914f5eSYinghai Lu 			return true;
332aa914f5eSYinghai Lu 		else
333aa914f5eSYinghai Lu 			return false;
334aa914f5eSYinghai Lu 	}
335aa914f5eSYinghai Lu 
336aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_MEM)
337aa914f5eSYinghai Lu 		return !!(mask & IORESOURCE_MEM);
338aa914f5eSYinghai Lu 
3390d607618SNicholas Johnson 	return false;	/* Should not get here */
340aa914f5eSYinghai Lu }
341aa914f5eSYinghai Lu 
342bdc4abecSYinghai Lu static void __assign_resources_sorted(struct list_head *head,
343bdc4abecSYinghai Lu 				      struct list_head *realloc_head,
344bdc4abecSYinghai Lu 				      struct list_head *fail_head)
345c8adf9a3SRam Pai {
3463e6e0d80SYinghai Lu 	/*
3470d607618SNicholas Johnson 	 * Should not assign requested resources at first.  They could be
3480d607618SNicholas Johnson 	 * adjacent, so later reassign can not reallocate them one by one in
3490d607618SNicholas Johnson 	 * parent resource window.
3500d607618SNicholas Johnson 	 *
3510d607618SNicholas Johnson 	 * Try to assign requested + add_size at beginning.  If could do that,
3520d607618SNicholas Johnson 	 * could get out early.  If could not do that, we still try to assign
3530d607618SNicholas Johnson 	 * requested at first, then try to reassign add_size for some resources.
354aa914f5eSYinghai Lu 	 *
355aa914f5eSYinghai Lu 	 * Separate three resource type checking if we need to release
356aa914f5eSYinghai Lu 	 * assigned resource after requested + add_size try.
3570d607618SNicholas Johnson 	 *
3580d607618SNicholas Johnson 	 *	1. If IO port assignment fails, will release assigned IO
3590d607618SNicholas Johnson 	 *	   port.
3600d607618SNicholas Johnson 	 *	2. If pref MMIO assignment fails, release assigned pref
3610d607618SNicholas Johnson 	 *	   MMIO.  If assigned pref MMIO's parent is non-pref MMIO
3620d607618SNicholas Johnson 	 *	   and non-pref MMIO assignment fails, will release that
3630d607618SNicholas Johnson 	 *	   assigned pref MMIO.
3640d607618SNicholas Johnson 	 *	3. If non-pref MMIO assignment fails or pref MMIO
3650d607618SNicholas Johnson 	 *	   assignment fails, will release assigned non-pref MMIO.
3663e6e0d80SYinghai Lu 	 */
367bdc4abecSYinghai Lu 	LIST_HEAD(save_head);
368bdc4abecSYinghai Lu 	LIST_HEAD(local_fail_head);
369b9b0bba9SYinghai Lu 	struct pci_dev_resource *save_res;
370d74b9027SWei Yang 	struct pci_dev_resource *dev_res, *tmp_res, *dev_res2;
371aa914f5eSYinghai Lu 	unsigned long fail_type;
372d74b9027SWei Yang 	resource_size_t add_align, align;
3733e6e0d80SYinghai Lu 
3743e6e0d80SYinghai Lu 	/* Check if optional add_size is there */
375bdc4abecSYinghai Lu 	if (!realloc_head || list_empty(realloc_head))
3763e6e0d80SYinghai Lu 		goto requested_and_reassign;
3773e6e0d80SYinghai Lu 
3783e6e0d80SYinghai Lu 	/* Save original start, end, flags etc at first */
379bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
380bdc4abecSYinghai Lu 		if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
381bffc56d4SYinghai Lu 			free_list(&save_head);
3823e6e0d80SYinghai Lu 			goto requested_and_reassign;
3833e6e0d80SYinghai Lu 		}
384bdc4abecSYinghai Lu 	}
3853e6e0d80SYinghai Lu 
3863e6e0d80SYinghai Lu 	/* Update res in head list with add_size in realloc_head list */
387d74b9027SWei Yang 	list_for_each_entry_safe(dev_res, tmp_res, head, list) {
388bdc4abecSYinghai Lu 		dev_res->res->end += get_res_add_size(realloc_head,
389bdc4abecSYinghai Lu 							dev_res->res);
3903e6e0d80SYinghai Lu 
391d74b9027SWei Yang 		/*
392d74b9027SWei Yang 		 * There are two kinds of additional resources in the list:
393d74b9027SWei Yang 		 * 1. bridge resource  -- IORESOURCE_STARTALIGN
394d74b9027SWei Yang 		 * 2. SR-IOV resource  -- IORESOURCE_SIZEALIGN
395d74b9027SWei Yang 		 * Here just fix the additional alignment for bridge
396d74b9027SWei Yang 		 */
397d74b9027SWei Yang 		if (!(dev_res->res->flags & IORESOURCE_STARTALIGN))
398d74b9027SWei Yang 			continue;
399d74b9027SWei Yang 
400d74b9027SWei Yang 		add_align = get_res_add_align(realloc_head, dev_res->res);
401d74b9027SWei Yang 
402d74b9027SWei Yang 		/*
4030d607618SNicholas Johnson 		 * The "head" list is sorted by alignment so resources with
4040d607618SNicholas Johnson 		 * bigger alignment will be assigned first.  After we
4050d607618SNicholas Johnson 		 * change the alignment of a dev_res in "head" list, we
4060d607618SNicholas Johnson 		 * need to reorder the list by alignment to make it
407d74b9027SWei Yang 		 * consistent.
408d74b9027SWei Yang 		 */
409d74b9027SWei Yang 		if (add_align > dev_res->res->start) {
410552bc94eSYinghai Lu 			resource_size_t r_size = resource_size(dev_res->res);
411552bc94eSYinghai Lu 
412d74b9027SWei Yang 			dev_res->res->start = add_align;
413552bc94eSYinghai Lu 			dev_res->res->end = add_align + r_size - 1;
414d74b9027SWei Yang 
415d74b9027SWei Yang 			list_for_each_entry(dev_res2, head, list) {
416d74b9027SWei Yang 				align = pci_resource_alignment(dev_res2->dev,
417d74b9027SWei Yang 							       dev_res2->res);
418a6b65983SWei Yang 				if (add_align > align) {
419d74b9027SWei Yang 					list_move_tail(&dev_res->list,
420d74b9027SWei Yang 						       &dev_res2->list);
421a6b65983SWei Yang 					break;
422a6b65983SWei Yang 				}
423d74b9027SWei Yang 			}
424d74b9027SWei Yang 		}
425d74b9027SWei Yang 
426d74b9027SWei Yang 	}
427d74b9027SWei Yang 
4283e6e0d80SYinghai Lu 	/* Try updated head list with add_size added */
4293e6e0d80SYinghai Lu 	assign_requested_resources_sorted(head, &local_fail_head);
4303e6e0d80SYinghai Lu 
4310d607618SNicholas Johnson 	/* All assigned with add_size? */
432bdc4abecSYinghai Lu 	if (list_empty(&local_fail_head)) {
4333e6e0d80SYinghai Lu 		/* Remove head list from realloc_head list */
434bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list)
435bdc4abecSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
436bffc56d4SYinghai Lu 		free_list(&save_head);
437bffc56d4SYinghai Lu 		free_list(head);
4383e6e0d80SYinghai Lu 		return;
4393e6e0d80SYinghai Lu 	}
4403e6e0d80SYinghai Lu 
4410d607618SNicholas Johnson 	/* Check failed type */
442aa914f5eSYinghai Lu 	fail_type = pci_fail_res_type_mask(&local_fail_head);
4430d607618SNicholas Johnson 	/* Remove not need to be released assigned res from head list etc */
444aa914f5eSYinghai Lu 	list_for_each_entry_safe(dev_res, tmp_res, head, list)
445aa914f5eSYinghai Lu 		if (dev_res->res->parent &&
446aa914f5eSYinghai Lu 		    !pci_need_to_release(fail_type, dev_res->res)) {
4470d607618SNicholas Johnson 			/* Remove it from realloc_head list */
448aa914f5eSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
449aa914f5eSYinghai Lu 			remove_from_list(&save_head, dev_res->res);
450aa914f5eSYinghai Lu 			list_del(&dev_res->list);
451aa914f5eSYinghai Lu 			kfree(dev_res);
452aa914f5eSYinghai Lu 		}
453aa914f5eSYinghai Lu 
454bffc56d4SYinghai Lu 	free_list(&local_fail_head);
4553e6e0d80SYinghai Lu 	/* Release assigned resource */
456bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list)
457bdc4abecSYinghai Lu 		if (dev_res->res->parent)
458bdc4abecSYinghai Lu 			release_resource(dev_res->res);
4593e6e0d80SYinghai Lu 	/* Restore start/end/flags from saved list */
460b9b0bba9SYinghai Lu 	list_for_each_entry(save_res, &save_head, list) {
461b9b0bba9SYinghai Lu 		struct resource *res = save_res->res;
4623e6e0d80SYinghai Lu 
463b9b0bba9SYinghai Lu 		res->start = save_res->start;
464b9b0bba9SYinghai Lu 		res->end = save_res->end;
465b9b0bba9SYinghai Lu 		res->flags = save_res->flags;
4663e6e0d80SYinghai Lu 	}
467bffc56d4SYinghai Lu 	free_list(&save_head);
4683e6e0d80SYinghai Lu 
4693e6e0d80SYinghai Lu requested_and_reassign:
470c8adf9a3SRam Pai 	/* Satisfy the must-have resource requests */
471c8adf9a3SRam Pai 	assign_requested_resources_sorted(head, fail_head);
472c8adf9a3SRam Pai 
4730d607618SNicholas Johnson 	/* Try to satisfy any additional optional resource requests */
4749e8bf93aSRam Pai 	if (realloc_head)
4759e8bf93aSRam Pai 		reassign_resources_sorted(realloc_head, head);
476bffc56d4SYinghai Lu 	free_list(head);
477c8adf9a3SRam Pai }
478c8adf9a3SRam Pai 
4796841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev,
480bdc4abecSYinghai Lu 					 struct list_head *add_head,
481bdc4abecSYinghai Lu 					 struct list_head *fail_head)
4826841ec68SYinghai Lu {
483bdc4abecSYinghai Lu 	LIST_HEAD(head);
4846841ec68SYinghai Lu 
4856841ec68SYinghai Lu 	__dev_sort_resources(dev, &head);
4868424d759SYinghai Lu 	__assign_resources_sorted(&head, add_head, fail_head);
4876841ec68SYinghai Lu 
4886841ec68SYinghai Lu }
4896841ec68SYinghai Lu 
4906841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus,
491bdc4abecSYinghai Lu 					 struct list_head *realloc_head,
492bdc4abecSYinghai Lu 					 struct list_head *fail_head)
4936841ec68SYinghai Lu {
4946841ec68SYinghai Lu 	struct pci_dev *dev;
495bdc4abecSYinghai Lu 	LIST_HEAD(head);
4966841ec68SYinghai Lu 
4976841ec68SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
4986841ec68SYinghai Lu 		__dev_sort_resources(dev, &head);
4996841ec68SYinghai Lu 
5009e8bf93aSRam Pai 	__assign_resources_sorted(&head, realloc_head, fail_head);
5016841ec68SYinghai Lu }
5026841ec68SYinghai Lu 
503b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
506c7dabef8SBjorn Helgaas 	struct resource *res;
5071da177e4SLinus Torvalds 	struct pci_bus_region region;
5081da177e4SLinus Torvalds 
5097506dc79SFrederick Lawler 	pci_info(bridge, "CardBus bridge to %pR\n",
510b918c62eSYinghai Lu 		 &bus->busn_res);
5111da177e4SLinus Torvalds 
512c7dabef8SBjorn Helgaas 	res = bus->resource[0];
513fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
514c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5151da177e4SLinus Torvalds 		/*
5161da177e4SLinus Torvalds 		 * The IO resource is allocated a range twice as large as it
5171da177e4SLinus Torvalds 		 * would normally need.  This allows us to set both IO regs.
5181da177e4SLinus Torvalds 		 */
5197506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5201da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
5211da177e4SLinus Torvalds 					region.start);
5221da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
5231da177e4SLinus Torvalds 					region.end);
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds 
526c7dabef8SBjorn Helgaas 	res = bus->resource[1];
527fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
528c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5297506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5301da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
5311da177e4SLinus Torvalds 					region.start);
5321da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
5331da177e4SLinus Torvalds 					region.end);
5341da177e4SLinus Torvalds 	}
5351da177e4SLinus Torvalds 
536c7dabef8SBjorn Helgaas 	res = bus->resource[2];
537fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
538c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
5397506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5401da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
5411da177e4SLinus Torvalds 					region.start);
5421da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
5431da177e4SLinus Torvalds 					region.end);
5441da177e4SLinus Torvalds 	}
5451da177e4SLinus Torvalds 
546c7dabef8SBjorn Helgaas 	res = bus->resource[3];
547fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
548c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
5497506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5501da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
5511da177e4SLinus Torvalds 					region.start);
5521da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
5531da177e4SLinus Torvalds 					region.end);
5541da177e4SLinus Torvalds 	}
5551da177e4SLinus Torvalds }
556b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus);
5571da177e4SLinus Torvalds 
5580d607618SNicholas Johnson /*
5590d607618SNicholas Johnson  * Initialize bridges with base/limit values we have collected.  PCI-to-PCI
5600d607618SNicholas Johnson  * Bridge Architecture Specification rev. 1.1 (1998) requires that if there
5610d607618SNicholas Johnson  * are no I/O ports or memory behind the bridge, the corresponding range
5620d607618SNicholas Johnson  * must be turned off by writing base value greater than limit to the
5630d607618SNicholas Johnson  * bridge's base/limit registers.
5640d607618SNicholas Johnson  *
5650d607618SNicholas Johnson  * Note: care must be taken when updating I/O base/limit registers of
5660d607618SNicholas Johnson  * bridges which support 32-bit I/O.  This update requires two config space
5670d607618SNicholas Johnson  * writes, so it's quite possible that an I/O window of the bridge will
5680d607618SNicholas Johnson  * have some undesirable address (e.g. 0) after the first write.  Ditto
5690d607618SNicholas Johnson  * 64-bit prefetchable MMIO.
5700d607618SNicholas Johnson  */
5713f2f4dc4SYinghai Lu static void pci_setup_bridge_io(struct pci_dev *bridge)
5721da177e4SLinus Torvalds {
573c7dabef8SBjorn Helgaas 	struct resource *res;
5741da177e4SLinus Torvalds 	struct pci_bus_region region;
5752b28ae19SBjorn Helgaas 	unsigned long io_mask;
5762b28ae19SBjorn Helgaas 	u8 io_base_lo, io_limit_lo;
5775b764b83SBjorn Helgaas 	u16 l;
5785b764b83SBjorn Helgaas 	u32 io_upper16;
5791da177e4SLinus Torvalds 
5802b28ae19SBjorn Helgaas 	io_mask = PCI_IO_RANGE_MASK;
5812b28ae19SBjorn Helgaas 	if (bridge->io_window_1k)
5822b28ae19SBjorn Helgaas 		io_mask = PCI_IO_1K_RANGE_MASK;
5832b28ae19SBjorn Helgaas 
5840d607618SNicholas Johnson 	/* Set up the top and bottom of the PCI I/O segment for this bus */
5856e0688dbSKrzysztof Wilczynski 	res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
586fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
587c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5885b764b83SBjorn Helgaas 		pci_read_config_word(bridge, PCI_IO_BASE, &l);
5892b28ae19SBjorn Helgaas 		io_base_lo = (region.start >> 8) & io_mask;
5902b28ae19SBjorn Helgaas 		io_limit_lo = (region.end >> 8) & io_mask;
5915b764b83SBjorn Helgaas 		l = ((u16) io_limit_lo << 8) | io_base_lo;
5920d607618SNicholas Johnson 		/* Set up upper 16 bits of I/O base/limit */
5931da177e4SLinus Torvalds 		io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
5947506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5957cc5997dSYinghai Lu 	} else {
5960d607618SNicholas Johnson 		/* Clear upper 16 bits of I/O base/limit */
5971da177e4SLinus Torvalds 		io_upper16 = 0;
5981da177e4SLinus Torvalds 		l = 0x00f0;
5991da177e4SLinus Torvalds 	}
6000d607618SNicholas Johnson 	/* Temporarily disable the I/O range before updating PCI_IO_BASE */
6011da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
6020d607618SNicholas Johnson 	/* Update lower 16 bits of I/O base/limit */
6035b764b83SBjorn Helgaas 	pci_write_config_word(bridge, PCI_IO_BASE, l);
6040d607618SNicholas Johnson 	/* Update upper 16 bits of I/O base/limit */
6051da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
6067cc5997dSYinghai Lu }
6071da177e4SLinus Torvalds 
6083f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio(struct pci_dev *bridge)
6097cc5997dSYinghai Lu {
6107cc5997dSYinghai Lu 	struct resource *res;
6117cc5997dSYinghai Lu 	struct pci_bus_region region;
6127cc5997dSYinghai Lu 	u32 l;
6137cc5997dSYinghai Lu 
6140d607618SNicholas Johnson 	/* Set up the top and bottom of the PCI Memory segment for this bus */
6156e0688dbSKrzysztof Wilczynski 	res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
616fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
617c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
6181da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
6191da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
6207506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
6217cc5997dSYinghai Lu 	} else {
6221da177e4SLinus Torvalds 		l = 0x0000fff0;
6231da177e4SLinus Torvalds 	}
6241da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
6257cc5997dSYinghai Lu }
6267cc5997dSYinghai Lu 
6273f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge)
6287cc5997dSYinghai Lu {
6297cc5997dSYinghai Lu 	struct resource *res;
6307cc5997dSYinghai Lu 	struct pci_bus_region region;
6317cc5997dSYinghai Lu 	u32 l, bu, lu;
6321da177e4SLinus Torvalds 
6330d607618SNicholas Johnson 	/*
6340d607618SNicholas Johnson 	 * Clear out the upper 32 bits of PREF limit.  If
6350d607618SNicholas Johnson 	 * PCI_PREF_BASE_UPPER32 was non-zero, this temporarily disables
6360d607618SNicholas Johnson 	 * PREF range, which is ok.
6370d607618SNicholas Johnson 	 */
6381da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
6391da177e4SLinus Torvalds 
6400d607618SNicholas Johnson 	/* Set up PREF base/limit */
641c40a22e0SBenjamin Herrenschmidt 	bu = lu = 0;
6426e0688dbSKrzysztof Wilczynski 	res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
643fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
644c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_PREFETCH) {
6451da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
6461da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
647c7dabef8SBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64) {
64813d36c24SAndrew Morton 			bu = upper_32_bits(region.start);
64913d36c24SAndrew Morton 			lu = upper_32_bits(region.end);
6501f82de10SYinghai Lu 		}
6517506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
6527cc5997dSYinghai Lu 	} else {
6531da177e4SLinus Torvalds 		l = 0x0000fff0;
6541da177e4SLinus Torvalds 	}
6551da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
6561da177e4SLinus Torvalds 
6570d607618SNicholas Johnson 	/* Set the upper 32 bits of PREF base & limit */
658c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
659c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
6607cc5997dSYinghai Lu }
6617cc5997dSYinghai Lu 
6627cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
6637cc5997dSYinghai Lu {
6647cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
6657cc5997dSYinghai Lu 
6667506dc79SFrederick Lawler 	pci_info(bridge, "PCI bridge to %pR\n",
667b918c62eSYinghai Lu 		 &bus->busn_res);
6687cc5997dSYinghai Lu 
6697cc5997dSYinghai Lu 	if (type & IORESOURCE_IO)
6703f2f4dc4SYinghai Lu 		pci_setup_bridge_io(bridge);
6717cc5997dSYinghai Lu 
6727cc5997dSYinghai Lu 	if (type & IORESOURCE_MEM)
6733f2f4dc4SYinghai Lu 		pci_setup_bridge_mmio(bridge);
6747cc5997dSYinghai Lu 
6757cc5997dSYinghai Lu 	if (type & IORESOURCE_PREFETCH)
6763f2f4dc4SYinghai Lu 		pci_setup_bridge_mmio_pref(bridge);
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
681d366d28cSGavin Shan void __weak pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
682d366d28cSGavin Shan {
683d366d28cSGavin Shan }
684d366d28cSGavin Shan 
685e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus)
6867cc5997dSYinghai Lu {
6877cc5997dSYinghai Lu 	unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
6887cc5997dSYinghai Lu 				  IORESOURCE_PREFETCH;
6897cc5997dSYinghai Lu 
690d366d28cSGavin Shan 	pcibios_setup_bridge(bus, type);
6917cc5997dSYinghai Lu 	__pci_setup_bridge(bus, type);
6927cc5997dSYinghai Lu }
6937cc5997dSYinghai Lu 
6948505e729SYinghai Lu 
6958505e729SYinghai Lu int pci_claim_bridge_resource(struct pci_dev *bridge, int i)
6968505e729SYinghai Lu {
6978505e729SYinghai Lu 	if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END)
6988505e729SYinghai Lu 		return 0;
6998505e729SYinghai Lu 
7008505e729SYinghai Lu 	if (pci_claim_resource(bridge, i) == 0)
7010d607618SNicholas Johnson 		return 0;	/* Claimed the window */
7028505e729SYinghai Lu 
7038505e729SYinghai Lu 	if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
7048505e729SYinghai Lu 		return 0;
7058505e729SYinghai Lu 
7068505e729SYinghai Lu 	if (!pci_bus_clip_resource(bridge, i))
7070d607618SNicholas Johnson 		return -EINVAL;	/* Clipping didn't change anything */
7088505e729SYinghai Lu 
7096e0688dbSKrzysztof Wilczynski 	switch (i) {
7106e0688dbSKrzysztof Wilczynski 	case PCI_BRIDGE_IO_WINDOW:
7118505e729SYinghai Lu 		pci_setup_bridge_io(bridge);
7128505e729SYinghai Lu 		break;
7136e0688dbSKrzysztof Wilczynski 	case PCI_BRIDGE_MEM_WINDOW:
7148505e729SYinghai Lu 		pci_setup_bridge_mmio(bridge);
7158505e729SYinghai Lu 		break;
7166e0688dbSKrzysztof Wilczynski 	case PCI_BRIDGE_PREF_MEM_WINDOW:
7178505e729SYinghai Lu 		pci_setup_bridge_mmio_pref(bridge);
7188505e729SYinghai Lu 		break;
7198505e729SYinghai Lu 	default:
7208505e729SYinghai Lu 		return -EINVAL;
7218505e729SYinghai Lu 	}
7228505e729SYinghai Lu 
7238505e729SYinghai Lu 	if (pci_claim_resource(bridge, i) == 0)
7240d607618SNicholas Johnson 		return 0;	/* Claimed a smaller window */
7258505e729SYinghai Lu 
7268505e729SYinghai Lu 	return -EINVAL;
7278505e729SYinghai Lu }
7288505e729SYinghai Lu 
7290d607618SNicholas Johnson /*
7300d607618SNicholas Johnson  * Check whether the bridge supports optional I/O and prefetchable memory
7310d607618SNicholas Johnson  * ranges.  If not, the respective base/limit registers must be read-only
7320d607618SNicholas Johnson  * and read as 0.
7330d607618SNicholas Johnson  */
73496bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus)
7351da177e4SLinus Torvalds {
7361da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
7376e0688dbSKrzysztof Wilczynski 	struct resource *b_res;
7381da177e4SLinus Torvalds 
7396e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
7406e0688dbSKrzysztof Wilczynski 	b_res->flags |= IORESOURCE_MEM;
7411da177e4SLinus Torvalds 
7426e0688dbSKrzysztof Wilczynski 	if (bridge->io_window) {
7436e0688dbSKrzysztof Wilczynski 		b_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
7446e0688dbSKrzysztof Wilczynski 		b_res->flags |= IORESOURCE_IO;
7456e0688dbSKrzysztof Wilczynski 	}
746d2f54d9bSBjorn Helgaas 
74751c48b31SBjorn Helgaas 	if (bridge->pref_window) {
7486e0688dbSKrzysztof Wilczynski 		b_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
7496e0688dbSKrzysztof Wilczynski 		b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
75051c48b31SBjorn Helgaas 		if (bridge->pref_64_window) {
7516e0688dbSKrzysztof Wilczynski 			b_res->flags |= IORESOURCE_MEM_64 |
7526e0688dbSKrzysztof Wilczynski 					PCI_PREF_RANGE_TYPE_64;
75399586105SYinghai Lu 		}
7541f82de10SYinghai Lu 	}
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7570d607618SNicholas Johnson /*
758c13704f5SNicholas Johnson  * Helper function for sizing routines.  Assigned resources have non-NULL
759c13704f5SNicholas Johnson  * parent resource.
760c13704f5SNicholas Johnson  *
761c13704f5SNicholas Johnson  * Return first unassigned resource of the correct type.  If there is none,
762c13704f5SNicholas Johnson  * return first assigned resource of the correct type.  If none of the
763c13704f5SNicholas Johnson  * above, return NULL.
764c13704f5SNicholas Johnson  *
765c13704f5SNicholas Johnson  * Returning an assigned resource of the correct type allows the caller to
766c13704f5SNicholas Johnson  * distinguish between already assigned and no resource of the correct type.
7670d607618SNicholas Johnson  */
768c13704f5SNicholas Johnson static struct resource *find_bus_resource_of_type(struct pci_bus *bus,
7690d607618SNicholas Johnson 						  unsigned long type_mask,
7700d607618SNicholas Johnson 						  unsigned long type)
7711da177e4SLinus Torvalds {
772c13704f5SNicholas Johnson 	struct resource *r, *r_assigned = NULL;
7731da177e4SLinus Torvalds 	int i;
7741da177e4SLinus Torvalds 
77589a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, r, i) {
776299de034SIvan Kokshaysky 		if (r == &ioport_resource || r == &iomem_resource)
777299de034SIvan Kokshaysky 			continue;
77855a10984SJesse Barnes 		if (r && (r->flags & type_mask) == type && !r->parent)
7791da177e4SLinus Torvalds 			return r;
780c13704f5SNicholas Johnson 		if (r && (r->flags & type_mask) == type && !r_assigned)
781c13704f5SNicholas Johnson 			r_assigned = r;
7821da177e4SLinus Torvalds 	}
783c13704f5SNicholas Johnson 	return r_assigned;
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
78613583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size,
78713583b16SRam Pai 					resource_size_t min_size,
78813583b16SRam Pai 					resource_size_t size1,
789de3ffa30SJon Derrick 					resource_size_t add_size,
790de3ffa30SJon Derrick 					resource_size_t children_add_size,
79113583b16SRam Pai 					resource_size_t old_size,
79213583b16SRam Pai 					resource_size_t align)
79313583b16SRam Pai {
79413583b16SRam Pai 	if (size < min_size)
79513583b16SRam Pai 		size = min_size;
79613583b16SRam Pai 	if (old_size == 1)
79713583b16SRam Pai 		old_size = 0;
7980d607618SNicholas Johnson 	/*
7990d607618SNicholas Johnson 	 * To be fixed in 2.5: we should have sort of HAVE_ISA flag in the
8000d607618SNicholas Johnson 	 * struct pci_bus.
8010d607618SNicholas Johnson 	 */
80213583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
80313583b16SRam Pai 	size = (size & 0xff) + ((size & ~0xffUL) << 2);
80413583b16SRam Pai #endif
805de3ffa30SJon Derrick 	size = size + size1;
80613583b16SRam Pai 	if (size < old_size)
80713583b16SRam Pai 		size = old_size;
808de3ffa30SJon Derrick 
809de3ffa30SJon Derrick 	size = ALIGN(max(size, add_size) + children_add_size, align);
81013583b16SRam Pai 	return size;
81113583b16SRam Pai }
81213583b16SRam Pai 
81313583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size,
81413583b16SRam Pai 					 resource_size_t min_size,
815de3ffa30SJon Derrick 					 resource_size_t add_size,
816de3ffa30SJon Derrick 					 resource_size_t children_add_size,
81713583b16SRam Pai 					 resource_size_t old_size,
81813583b16SRam Pai 					 resource_size_t align)
81913583b16SRam Pai {
82013583b16SRam Pai 	if (size < min_size)
82113583b16SRam Pai 		size = min_size;
82213583b16SRam Pai 	if (old_size == 1)
82313583b16SRam Pai 		old_size = 0;
82413583b16SRam Pai 	if (size < old_size)
82513583b16SRam Pai 		size = old_size;
826de3ffa30SJon Derrick 
827de3ffa30SJon Derrick 	size = ALIGN(max(size, add_size) + children_add_size, align);
82813583b16SRam Pai 	return size;
82913583b16SRam Pai }
83013583b16SRam Pai 
831ac5ad93eSGavin Shan resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
832ac5ad93eSGavin Shan 						unsigned long type)
833ac5ad93eSGavin Shan {
834ac5ad93eSGavin Shan 	return 1;
835ac5ad93eSGavin Shan }
836ac5ad93eSGavin Shan 
837ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_MEM_ALIGN	0x100000	/* 1MiB */
838ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN	0x1000		/* 4KiB */
839ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN_1K	0x400		/* 1KiB */
840ac5ad93eSGavin Shan 
8410d607618SNicholas Johnson static resource_size_t window_alignment(struct pci_bus *bus, unsigned long type)
842ac5ad93eSGavin Shan {
843ac5ad93eSGavin Shan 	resource_size_t align = 1, arch_align;
844ac5ad93eSGavin Shan 
845ac5ad93eSGavin Shan 	if (type & IORESOURCE_MEM)
846ac5ad93eSGavin Shan 		align = PCI_P2P_DEFAULT_MEM_ALIGN;
847ac5ad93eSGavin Shan 	else if (type & IORESOURCE_IO) {
848ac5ad93eSGavin Shan 		/*
8490d607618SNicholas Johnson 		 * Per spec, I/O windows are 4K-aligned, but some bridges have
8500d607618SNicholas Johnson 		 * an extension to support 1K alignment.
851ac5ad93eSGavin Shan 		 */
8522c8d5a2dSIvan Kokshaysky 		if (bus->self && bus->self->io_window_1k)
853ac5ad93eSGavin Shan 			align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
854ac5ad93eSGavin Shan 		else
855ac5ad93eSGavin Shan 			align = PCI_P2P_DEFAULT_IO_ALIGN;
856ac5ad93eSGavin Shan 	}
857ac5ad93eSGavin Shan 
858ac5ad93eSGavin Shan 	arch_align = pcibios_window_alignment(bus, type);
859ac5ad93eSGavin Shan 	return max(align, arch_align);
860ac5ad93eSGavin Shan }
861ac5ad93eSGavin Shan 
862c8adf9a3SRam Pai /**
8630d607618SNicholas Johnson  * pbus_size_io() - Size the I/O window of a given bus
864c8adf9a3SRam Pai  *
8650d607618SNicholas Johnson  * @bus:		The bus
8660d607618SNicholas Johnson  * @min_size:		The minimum I/O window that must be allocated
8670d607618SNicholas Johnson  * @add_size:		Additional optional I/O window
8680d607618SNicholas Johnson  * @realloc_head:	Track the additional I/O window on this list
869c8adf9a3SRam Pai  *
8700d607618SNicholas Johnson  * Sizing the I/O windows of the PCI-PCI bridge is trivial, since these
8710d607618SNicholas Johnson  * windows have 1K or 4K granularity and the I/O ranges of non-bridge PCI
8720d607618SNicholas Johnson  * devices are limited to 256 bytes.  We must be careful with the ISA
8730d607618SNicholas Johnson  * aliasing though.
874c8adf9a3SRam Pai  */
875c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
8760d607618SNicholas Johnson 			 resource_size_t add_size,
8770d607618SNicholas Johnson 			 struct list_head *realloc_head)
8781da177e4SLinus Torvalds {
8791da177e4SLinus Torvalds 	struct pci_dev *dev;
880c13704f5SNicholas Johnson 	struct resource *b_res = find_bus_resource_of_type(bus, IORESOURCE_IO,
8815b285415SYinghai Lu 							   IORESOURCE_IO);
88211251a86SWei Yang 	resource_size_t size = 0, size0 = 0, size1 = 0;
883be768912SYinghai Lu 	resource_size_t children_add_size = 0;
8842d1d6678SBjorn Helgaas 	resource_size_t min_align, align;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	if (!b_res)
8871da177e4SLinus Torvalds 		return;
8881da177e4SLinus Torvalds 
889c13704f5SNicholas Johnson 	/* If resource is already assigned, nothing more to do */
890c13704f5SNicholas Johnson 	if (b_res->parent)
891c13704f5SNicholas Johnson 		return;
892c13704f5SNicholas Johnson 
8932d1d6678SBjorn Helgaas 	min_align = window_alignment(bus, IORESOURCE_IO);
8941da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
895*09cc9006SMika Westerberg 		struct resource *r;
8961da177e4SLinus Torvalds 
897*09cc9006SMika Westerberg 		pci_dev_for_each_resource(dev, r) {
8981da177e4SLinus Torvalds 			unsigned long r_size;
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 			if (r->parent || !(r->flags & IORESOURCE_IO))
9011da177e4SLinus Torvalds 				continue;
902022edd86SZhao, Yu 			r_size = resource_size(r);
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 			if (r_size < 0x400)
9051da177e4SLinus Torvalds 				/* Might be re-aligned for ISA */
9061da177e4SLinus Torvalds 				size += r_size;
9071da177e4SLinus Torvalds 			else
9081da177e4SLinus Torvalds 				size1 += r_size;
909be768912SYinghai Lu 
910fd591341SYinghai Lu 			align = pci_resource_alignment(dev, r);
911fd591341SYinghai Lu 			if (align > min_align)
912fd591341SYinghai Lu 				min_align = align;
913fd591341SYinghai Lu 
9149e8bf93aSRam Pai 			if (realloc_head)
9159e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
9161da177e4SLinus Torvalds 		}
9171da177e4SLinus Torvalds 	}
918fd591341SYinghai Lu 
919de3ffa30SJon Derrick 	size0 = calculate_iosize(size, min_size, size1, 0, 0,
920fd591341SYinghai Lu 			resource_size(b_res), min_align);
921de3ffa30SJon Derrick 	size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
922de3ffa30SJon Derrick 		calculate_iosize(size, min_size, size1, add_size, children_add_size,
923fd591341SYinghai Lu 			resource_size(b_res), min_align);
924c8adf9a3SRam Pai 	if (!size0 && !size1) {
9252c8d5a2dSIvan Kokshaysky 		if (bus->self && (b_res->start || b_res->end))
9267506dc79SFrederick Lawler 			pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
927227f0647SRyan Desfosses 				 b_res, &bus->busn_res);
9281da177e4SLinus Torvalds 		b_res->flags = 0;
9291da177e4SLinus Torvalds 		return;
9301da177e4SLinus Torvalds 	}
931fd591341SYinghai Lu 
932fd591341SYinghai Lu 	b_res->start = min_align;
933c8adf9a3SRam Pai 	b_res->end = b_res->start + size0 - 1;
93488452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
9352c8d5a2dSIvan Kokshaysky 	if (bus->self && size1 > size0 && realloc_head) {
936fd591341SYinghai Lu 		add_to_list(realloc_head, bus->self, b_res, size1-size0,
937fd591341SYinghai Lu 			    min_align);
93834c6b710SMohan Kumar 		pci_info(bus->self, "bridge window %pR to %pR add_size %llx\n",
939227f0647SRyan Desfosses 			 b_res, &bus->busn_res,
94011251a86SWei Yang 			 (unsigned long long) size1 - size0);
941b592443dSYinghai Lu 	}
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds 
944c121504eSGavin Shan static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
945c121504eSGavin Shan 						  int max_order)
946c121504eSGavin Shan {
947c121504eSGavin Shan 	resource_size_t align = 0;
948c121504eSGavin Shan 	resource_size_t min_align = 0;
949c121504eSGavin Shan 	int order;
950c121504eSGavin Shan 
951c121504eSGavin Shan 	for (order = 0; order <= max_order; order++) {
952c121504eSGavin Shan 		resource_size_t align1 = 1;
953c121504eSGavin Shan 
954c121504eSGavin Shan 		align1 <<= (order + 20);
955c121504eSGavin Shan 
956c121504eSGavin Shan 		if (!align)
957c121504eSGavin Shan 			min_align = align1;
958c121504eSGavin Shan 		else if (ALIGN(align + min_align, min_align) < align1)
959c121504eSGavin Shan 			min_align = align1 >> 1;
960c121504eSGavin Shan 		align += aligns[order];
961c121504eSGavin Shan 	}
962c121504eSGavin Shan 
963c121504eSGavin Shan 	return min_align;
964c121504eSGavin Shan }
965c121504eSGavin Shan 
966c8adf9a3SRam Pai /**
9670d607618SNicholas Johnson  * pbus_size_mem() - Size the memory window of a given bus
968c8adf9a3SRam Pai  *
9690d607618SNicholas Johnson  * @bus:		The bus
9700d607618SNicholas Johnson  * @mask:		Mask the resource flag, then compare it with type
9710d607618SNicholas Johnson  * @type:		The type of free resource from bridge
9720d607618SNicholas Johnson  * @type2:		Second match type
9730d607618SNicholas Johnson  * @type3:		Third match type
9740d607618SNicholas Johnson  * @min_size:		The minimum memory window that must be allocated
9750d607618SNicholas Johnson  * @add_size:		Additional optional memory window
9760d607618SNicholas Johnson  * @realloc_head:	Track the additional memory window on this list
977c8adf9a3SRam Pai  *
9780d607618SNicholas Johnson  * Calculate the size of the bus and minimal alignment which guarantees
9790d607618SNicholas Johnson  * that all child resources fit in this size.
98030afe8d0SBjorn Helgaas  *
9810d607618SNicholas Johnson  * Return -ENOSPC if there's no available bus resource of the desired
9820d607618SNicholas Johnson  * type.  Otherwise, set the bus resource start/end to indicate the
9830d607618SNicholas Johnson  * required size, add things to realloc_head (if supplied), and return 0.
984c8adf9a3SRam Pai  */
98528760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
9865b285415SYinghai Lu 			 unsigned long type, unsigned long type2,
9870d607618SNicholas Johnson 			 unsigned long type3, resource_size_t min_size,
9880d607618SNicholas Johnson 			 resource_size_t add_size,
989bdc4abecSYinghai Lu 			 struct list_head *realloc_head)
9901da177e4SLinus Torvalds {
9911da177e4SLinus Torvalds 	struct pci_dev *dev;
992c8adf9a3SRam Pai 	resource_size_t min_align, align, size, size0, size1;
9933dc8a1f6SDongdong Liu 	resource_size_t aligns[24]; /* Alignments from 1MB to 8TB */
9941da177e4SLinus Torvalds 	int order, max_order;
995c13704f5SNicholas Johnson 	struct resource *b_res = find_bus_resource_of_type(bus,
9965b285415SYinghai Lu 					mask | IORESOURCE_PREFETCH, type);
997be768912SYinghai Lu 	resource_size_t children_add_size = 0;
998d74b9027SWei Yang 	resource_size_t children_add_align = 0;
999d74b9027SWei Yang 	resource_size_t add_align = 0;
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	if (!b_res)
100230afe8d0SBjorn Helgaas 		return -ENOSPC;
10031da177e4SLinus Torvalds 
1004c13704f5SNicholas Johnson 	/* If resource is already assigned, nothing more to do */
1005c13704f5SNicholas Johnson 	if (b_res->parent)
1006c13704f5SNicholas Johnson 		return 0;
1007c13704f5SNicholas Johnson 
10081da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
10091da177e4SLinus Torvalds 	max_order = 0;
10101da177e4SLinus Torvalds 	size = 0;
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
1013*09cc9006SMika Westerberg 		struct resource *r;
10141da177e4SLinus Torvalds 		int i;
10151da177e4SLinus Torvalds 
1016*09cc9006SMika Westerberg 		pci_dev_for_each_resource(dev, r, i) {
1017c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
10181da177e4SLinus Torvalds 
1019a2220d80SDavid Daney 			if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) ||
1020a2220d80SDavid Daney 			    ((r->flags & mask) != type &&
10215b285415SYinghai Lu 			     (r->flags & mask) != type2 &&
10225b285415SYinghai Lu 			     (r->flags & mask) != type3))
10231da177e4SLinus Torvalds 				continue;
1024022edd86SZhao, Yu 			r_size = resource_size(r);
10252aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV
10260d607618SNicholas Johnson 			/* Put SRIOV requested res to the optional list */
10279e8bf93aSRam Pai 			if (realloc_head && i >= PCI_IOV_RESOURCES &&
10282aceefcbSYinghai Lu 					i <= PCI_IOV_RESOURCE_END) {
1029d74b9027SWei Yang 				add_align = max(pci_resource_alignment(dev, r), add_align);
10302aceefcbSYinghai Lu 				r->end = r->start - 1;
10310d607618SNicholas Johnson 				add_to_list(realloc_head, dev, r, r_size, 0 /* Don't care */);
10322aceefcbSYinghai Lu 				children_add_size += r_size;
10332aceefcbSYinghai Lu 				continue;
10342aceefcbSYinghai Lu 			}
10352aceefcbSYinghai Lu #endif
103614c8530dSAlan 			/*
103714c8530dSAlan 			 * aligns[0] is for 1MB (since bridge memory
103814c8530dSAlan 			 * windows are always at least 1MB aligned), so
103914c8530dSAlan 			 * keep "order" from being negative for smaller
104014c8530dSAlan 			 * resources.
104114c8530dSAlan 			 */
10426faf17f6SChris Wright 			align = pci_resource_alignment(dev, r);
10431da177e4SLinus Torvalds 			order = __ffs(align) - 20;
104414c8530dSAlan 			if (order < 0)
104514c8530dSAlan 				order = 0;
104614c8530dSAlan 			if (order >= ARRAY_SIZE(aligns)) {
10477506dc79SFrederick Lawler 				pci_warn(dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
1048227f0647SRyan Desfosses 					 i, r, (unsigned long long) align);
10491da177e4SLinus Torvalds 				r->flags = 0;
10501da177e4SLinus Torvalds 				continue;
10511da177e4SLinus Torvalds 			}
1052c9c75143SYongji Xie 			size += max(r_size, align);
10530d607618SNicholas Johnson 			/*
10540d607618SNicholas Johnson 			 * Exclude ranges with size > align from calculation of
10550d607618SNicholas Johnson 			 * the alignment.
10560d607618SNicholas Johnson 			 */
1057c9c75143SYongji Xie 			if (r_size <= align)
10581da177e4SLinus Torvalds 				aligns[order] += align;
10591da177e4SLinus Torvalds 			if (order > max_order)
10601da177e4SLinus Torvalds 				max_order = order;
1061be768912SYinghai Lu 
1062d74b9027SWei Yang 			if (realloc_head) {
10639e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
1064d74b9027SWei Yang 				children_add_align = get_res_add_align(realloc_head, r);
1065d74b9027SWei Yang 				add_align = max(add_align, children_add_align);
1066d74b9027SWei Yang 			}
10671da177e4SLinus Torvalds 		}
10681da177e4SLinus Torvalds 	}
10698308c54dSJeremy Fitzhardinge 
1070c121504eSGavin Shan 	min_align = calculate_mem_align(aligns, max_order);
10713ad94b0dSWei Yang 	min_align = max(min_align, window_alignment(bus, b_res->flags));
1072de3ffa30SJon Derrick 	size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), min_align);
1073d74b9027SWei Yang 	add_align = max(min_align, add_align);
1074de3ffa30SJon Derrick 	size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
1075de3ffa30SJon Derrick 		calculate_memsize(size, min_size, add_size, children_add_size,
1076d74b9027SWei Yang 				resource_size(b_res), add_align);
1077c8adf9a3SRam Pai 	if (!size0 && !size1) {
10782c8d5a2dSIvan Kokshaysky 		if (bus->self && (b_res->start || b_res->end))
10797506dc79SFrederick Lawler 			pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
1080227f0647SRyan Desfosses 				 b_res, &bus->busn_res);
10811da177e4SLinus Torvalds 		b_res->flags = 0;
108230afe8d0SBjorn Helgaas 		return 0;
10831da177e4SLinus Torvalds 	}
10841da177e4SLinus Torvalds 	b_res->start = min_align;
1085c8adf9a3SRam Pai 	b_res->end = size0 + min_align - 1;
10865b285415SYinghai Lu 	b_res->flags |= IORESOURCE_STARTALIGN;
10872c8d5a2dSIvan Kokshaysky 	if (bus->self && size1 > size0 && realloc_head) {
1088d74b9027SWei Yang 		add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align);
108934c6b710SMohan Kumar 		pci_info(bus->self, "bridge window %pR to %pR add_size %llx add_align %llx\n",
1090227f0647SRyan Desfosses 			   b_res, &bus->busn_res,
1091d74b9027SWei Yang 			   (unsigned long long) (size1 - size0),
1092d74b9027SWei Yang 			   (unsigned long long) add_align);
1093b592443dSYinghai Lu 	}
109430afe8d0SBjorn Helgaas 	return 0;
10951da177e4SLinus Torvalds }
10961da177e4SLinus Torvalds 
10970a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res)
10980a2daa1cSRam Pai {
10990a2daa1cSRam Pai 	if (res->flags & IORESOURCE_IO)
11000a2daa1cSRam Pai 		return pci_cardbus_io_size;
11010a2daa1cSRam Pai 	if (res->flags & IORESOURCE_MEM)
11020a2daa1cSRam Pai 		return pci_cardbus_mem_size;
11030a2daa1cSRam Pai 	return 0;
11040a2daa1cSRam Pai }
11050a2daa1cSRam Pai 
11060a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus,
1107bdc4abecSYinghai Lu 				 struct list_head *realloc_head)
11081da177e4SLinus Torvalds {
11091da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
11106e0688dbSKrzysztof Wilczynski 	struct resource *b_res;
111111848934SYinghai Lu 	resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
11121da177e4SLinus Torvalds 	u16 ctrl;
11131da177e4SLinus Torvalds 
11146e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_CB_BRIDGE_IO_0_WINDOW];
11156e0688dbSKrzysztof Wilczynski 	if (b_res->parent)
11163796f1e2SYinghai Lu 		goto handle_b_res_1;
11171da177e4SLinus Torvalds 	/*
11180d607618SNicholas Johnson 	 * Reserve some resources for CardBus.  We reserve a fixed amount
11190d607618SNicholas Johnson 	 * of bus space for CardBus bridges.
11201da177e4SLinus Torvalds 	 */
11216e0688dbSKrzysztof Wilczynski 	b_res->start = pci_cardbus_io_size;
11226e0688dbSKrzysztof Wilczynski 	b_res->end = b_res->start + pci_cardbus_io_size - 1;
11236e0688dbSKrzysztof Wilczynski 	b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
112411848934SYinghai Lu 	if (realloc_head) {
11256e0688dbSKrzysztof Wilczynski 		b_res->end -= pci_cardbus_io_size;
112611848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
112711848934SYinghai Lu 			    pci_cardbus_io_size);
112811848934SYinghai Lu 	}
11291da177e4SLinus Torvalds 
11303796f1e2SYinghai Lu handle_b_res_1:
11316e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_CB_BRIDGE_IO_1_WINDOW];
11326e0688dbSKrzysztof Wilczynski 	if (b_res->parent)
11333796f1e2SYinghai Lu 		goto handle_b_res_2;
11346e0688dbSKrzysztof Wilczynski 	b_res->start = pci_cardbus_io_size;
11356e0688dbSKrzysztof Wilczynski 	b_res->end = b_res->start + pci_cardbus_io_size - 1;
11366e0688dbSKrzysztof Wilczynski 	b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
113711848934SYinghai Lu 	if (realloc_head) {
11386e0688dbSKrzysztof Wilczynski 		b_res->end -= pci_cardbus_io_size;
11396e0688dbSKrzysztof Wilczynski 		add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
114011848934SYinghai Lu 			    pci_cardbus_io_size);
114111848934SYinghai Lu 	}
11421da177e4SLinus Torvalds 
11433796f1e2SYinghai Lu handle_b_res_2:
11440d607618SNicholas Johnson 	/* MEM1 must not be pref MMIO */
1145dcef0d06SYinghai Lu 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1146dcef0d06SYinghai Lu 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
1147dcef0d06SYinghai Lu 		ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
1148dcef0d06SYinghai Lu 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1149dcef0d06SYinghai Lu 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1150dcef0d06SYinghai Lu 	}
1151dcef0d06SYinghai Lu 
11520d607618SNicholas Johnson 	/* Check whether prefetchable memory is supported by this bridge. */
11531da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
11541da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
11551da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
11561da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
11571da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
11581da177e4SLinus Torvalds 	}
11591da177e4SLinus Torvalds 
11606e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_0_WINDOW];
11616e0688dbSKrzysztof Wilczynski 	if (b_res->parent)
11623796f1e2SYinghai Lu 		goto handle_b_res_3;
11631da177e4SLinus Torvalds 	/*
11640d607618SNicholas Johnson 	 * If we have prefetchable memory support, allocate two regions.
11650d607618SNicholas Johnson 	 * Otherwise, allocate one region of twice the size.
11661da177e4SLinus Torvalds 	 */
11671da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
11686e0688dbSKrzysztof Wilczynski 		b_res->start = pci_cardbus_mem_size;
11696e0688dbSKrzysztof Wilczynski 		b_res->end = b_res->start + pci_cardbus_mem_size - 1;
11706e0688dbSKrzysztof Wilczynski 		b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
117111848934SYinghai Lu 				    IORESOURCE_STARTALIGN;
117211848934SYinghai Lu 		if (realloc_head) {
11736e0688dbSKrzysztof Wilczynski 			b_res->end -= pci_cardbus_mem_size;
11746e0688dbSKrzysztof Wilczynski 			add_to_list(realloc_head, bridge, b_res,
117511848934SYinghai Lu 				    pci_cardbus_mem_size, pci_cardbus_mem_size);
11761da177e4SLinus Torvalds 		}
11770a2daa1cSRam Pai 
11780d607618SNicholas Johnson 		/* Reduce that to half */
117911848934SYinghai Lu 		b_res_3_size = pci_cardbus_mem_size;
118011848934SYinghai Lu 	}
118111848934SYinghai Lu 
11823796f1e2SYinghai Lu handle_b_res_3:
11836e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_1_WINDOW];
11846e0688dbSKrzysztof Wilczynski 	if (b_res->parent)
11853796f1e2SYinghai Lu 		goto handle_done;
11866e0688dbSKrzysztof Wilczynski 	b_res->start = pci_cardbus_mem_size;
11876e0688dbSKrzysztof Wilczynski 	b_res->end = b_res->start + b_res_3_size - 1;
11886e0688dbSKrzysztof Wilczynski 	b_res->flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
118911848934SYinghai Lu 	if (realloc_head) {
11906e0688dbSKrzysztof Wilczynski 		b_res->end -= b_res_3_size;
11916e0688dbSKrzysztof Wilczynski 		add_to_list(realloc_head, bridge, b_res, b_res_3_size,
119211848934SYinghai Lu 			    pci_cardbus_mem_size);
119311848934SYinghai Lu 	}
11943796f1e2SYinghai Lu 
11953796f1e2SYinghai Lu handle_done:
11963796f1e2SYinghai Lu 	;
11971da177e4SLinus Torvalds }
11981da177e4SLinus Torvalds 
119910874f5aSBjorn Helgaas void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
12001da177e4SLinus Torvalds {
12011da177e4SLinus Torvalds 	struct pci_dev *dev;
12025b285415SYinghai Lu 	unsigned long mask, prefmask, type2 = 0, type3 = 0;
1203d7b8a217SNicholas Johnson 	resource_size_t additional_io_size = 0, additional_mmio_size = 0,
1204d7b8a217SNicholas Johnson 			additional_mmio_pref_size = 0;
12052c8d5a2dSIvan Kokshaysky 	struct resource *pref;
12062c8d5a2dSIvan Kokshaysky 	struct pci_host_bridge *host;
12072c8d5a2dSIvan Kokshaysky 	int hdr_type, i, ret;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
12101da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
12111da177e4SLinus Torvalds 		if (!b)
12121da177e4SLinus Torvalds 			continue;
12131da177e4SLinus Torvalds 
1214b2fb5cc5SHonghui Zhang 		switch (dev->hdr_type) {
1215b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_CARDBUS:
12169e8bf93aSRam Pai 			pci_bus_size_cardbus(b, realloc_head);
12171da177e4SLinus Torvalds 			break;
12181da177e4SLinus Torvalds 
1219b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_BRIDGE:
12201da177e4SLinus Torvalds 		default:
12219e8bf93aSRam Pai 			__pci_bus_size_bridges(b, realloc_head);
12221da177e4SLinus Torvalds 			break;
12231da177e4SLinus Torvalds 		}
12241da177e4SLinus Torvalds 	}
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	/* The root bus? */
12272c8d5a2dSIvan Kokshaysky 	if (pci_is_root_bus(bus)) {
12282c8d5a2dSIvan Kokshaysky 		host = to_pci_host_bridge(bus->bridge);
12292c8d5a2dSIvan Kokshaysky 		if (!host->size_windows)
12301da177e4SLinus Torvalds 			return;
12312c8d5a2dSIvan Kokshaysky 		pci_bus_for_each_resource(bus, pref, i)
12322c8d5a2dSIvan Kokshaysky 			if (pref && (pref->flags & IORESOURCE_PREFETCH))
12332c8d5a2dSIvan Kokshaysky 				break;
12342c8d5a2dSIvan Kokshaysky 		hdr_type = -1;	/* Intentionally invalid - not a PCI device. */
12352c8d5a2dSIvan Kokshaysky 	} else {
12366e0688dbSKrzysztof Wilczynski 		pref = &bus->self->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
12372c8d5a2dSIvan Kokshaysky 		hdr_type = bus->self->hdr_type;
12382c8d5a2dSIvan Kokshaysky 	}
12391da177e4SLinus Torvalds 
12402c8d5a2dSIvan Kokshaysky 	switch (hdr_type) {
1241b2fb5cc5SHonghui Zhang 	case PCI_HEADER_TYPE_CARDBUS:
12420d607618SNicholas Johnson 		/* Don't size CardBuses yet */
12431da177e4SLinus Torvalds 		break;
12441da177e4SLinus Torvalds 
1245b2fb5cc5SHonghui Zhang 	case PCI_HEADER_TYPE_BRIDGE:
12461da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
124728760489SEric W. Biederman 		if (bus->self->is_hotplug_bridge) {
1248c8adf9a3SRam Pai 			additional_io_size  = pci_hotplug_io_size;
1249d7b8a217SNicholas Johnson 			additional_mmio_size = pci_hotplug_mmio_size;
1250d7b8a217SNicholas Johnson 			additional_mmio_pref_size = pci_hotplug_mmio_pref_size;
125128760489SEric W. Biederman 		}
1252df561f66SGustavo A. R. Silva 		fallthrough;
12531da177e4SLinus Torvalds 	default:
125419aa7ee4SYinghai Lu 		pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
125519aa7ee4SYinghai Lu 			     additional_io_size, realloc_head);
125667d29b5cSBjorn Helgaas 
125767d29b5cSBjorn Helgaas 		/*
125867d29b5cSBjorn Helgaas 		 * If there's a 64-bit prefetchable MMIO window, compute
125967d29b5cSBjorn Helgaas 		 * the size required to put all 64-bit prefetchable
126067d29b5cSBjorn Helgaas 		 * resources in it.
126167d29b5cSBjorn Helgaas 		 */
12621da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
12631da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
12642c8d5a2dSIvan Kokshaysky 		if (pref && (pref->flags & IORESOURCE_MEM_64)) {
12655b285415SYinghai Lu 			prefmask |= IORESOURCE_MEM_64;
126630afe8d0SBjorn Helgaas 			ret = pbus_size_mem(bus, prefmask, prefmask,
12675b285415SYinghai Lu 				prefmask, prefmask,
1268d7b8a217SNicholas Johnson 				realloc_head ? 0 : additional_mmio_pref_size,
1269d7b8a217SNicholas Johnson 				additional_mmio_pref_size, realloc_head);
127067d29b5cSBjorn Helgaas 
12715b285415SYinghai Lu 			/*
127267d29b5cSBjorn Helgaas 			 * If successful, all non-prefetchable resources
127367d29b5cSBjorn Helgaas 			 * and any 32-bit prefetchable resources will go in
127467d29b5cSBjorn Helgaas 			 * the non-prefetchable window.
127567d29b5cSBjorn Helgaas 			 */
127667d29b5cSBjorn Helgaas 			if (ret == 0) {
12775b285415SYinghai Lu 				mask = prefmask;
12785b285415SYinghai Lu 				type2 = prefmask & ~IORESOURCE_MEM_64;
12795b285415SYinghai Lu 				type3 = prefmask & ~IORESOURCE_PREFETCH;
12805b285415SYinghai Lu 			}
12815b285415SYinghai Lu 		}
128267d29b5cSBjorn Helgaas 
128367d29b5cSBjorn Helgaas 		/*
128467d29b5cSBjorn Helgaas 		 * If there is no 64-bit prefetchable window, compute the
128567d29b5cSBjorn Helgaas 		 * size required to put all prefetchable resources in the
128667d29b5cSBjorn Helgaas 		 * 32-bit prefetchable window (if there is one).
128767d29b5cSBjorn Helgaas 		 */
12885b285415SYinghai Lu 		if (!type2) {
12895b285415SYinghai Lu 			prefmask &= ~IORESOURCE_MEM_64;
129030afe8d0SBjorn Helgaas 			ret = pbus_size_mem(bus, prefmask, prefmask,
12915b285415SYinghai Lu 				prefmask, prefmask,
1292d7b8a217SNicholas Johnson 				realloc_head ? 0 : additional_mmio_pref_size,
1293d7b8a217SNicholas Johnson 				additional_mmio_pref_size, realloc_head);
129467d29b5cSBjorn Helgaas 
129567d29b5cSBjorn Helgaas 			/*
129667d29b5cSBjorn Helgaas 			 * If successful, only non-prefetchable resources
129767d29b5cSBjorn Helgaas 			 * will go in the non-prefetchable window.
129867d29b5cSBjorn Helgaas 			 */
129967d29b5cSBjorn Helgaas 			if (ret == 0)
13005b285415SYinghai Lu 				mask = prefmask;
130128760489SEric W. Biederman 			else
1302d7b8a217SNicholas Johnson 				additional_mmio_size += additional_mmio_pref_size;
130367d29b5cSBjorn Helgaas 
13045b285415SYinghai Lu 			type2 = type3 = IORESOURCE_MEM;
13055b285415SYinghai Lu 		}
130667d29b5cSBjorn Helgaas 
130767d29b5cSBjorn Helgaas 		/*
130867d29b5cSBjorn Helgaas 		 * Compute the size required to put everything else in the
130967d29b5cSBjorn Helgaas 		 * non-prefetchable window. This includes:
131067d29b5cSBjorn Helgaas 		 *
131167d29b5cSBjorn Helgaas 		 *   - all non-prefetchable resources
131267d29b5cSBjorn Helgaas 		 *   - 32-bit prefetchable resources if there's a 64-bit
131367d29b5cSBjorn Helgaas 		 *     prefetchable window or no prefetchable window at all
13140d607618SNicholas Johnson 		 *   - 64-bit prefetchable resources if there's no prefetchable
13150d607618SNicholas Johnson 		 *     window at all
131667d29b5cSBjorn Helgaas 		 *
13170d607618SNicholas Johnson 		 * Note that the strategy in __pci_assign_resource() must match
13180d607618SNicholas Johnson 		 * that used here. Specifically, we cannot put a 32-bit
13190d607618SNicholas Johnson 		 * prefetchable resource in a 64-bit prefetchable window.
132067d29b5cSBjorn Helgaas 		 */
13215b285415SYinghai Lu 		pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
1322d7b8a217SNicholas Johnson 			      realloc_head ? 0 : additional_mmio_size,
1323d7b8a217SNicholas Johnson 			      additional_mmio_size, realloc_head);
13241da177e4SLinus Torvalds 		break;
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds }
1327c8adf9a3SRam Pai 
132810874f5aSBjorn Helgaas void pci_bus_size_bridges(struct pci_bus *bus)
1329c8adf9a3SRam Pai {
1330c8adf9a3SRam Pai 	__pci_bus_size_bridges(bus, NULL);
1331c8adf9a3SRam Pai }
13321da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
13331da177e4SLinus Torvalds 
1334d04d0111SDavid Daney static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r)
1335d04d0111SDavid Daney {
1336d04d0111SDavid Daney 	int i;
1337d04d0111SDavid Daney 	struct resource *parent_r;
1338d04d0111SDavid Daney 	unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM |
1339d04d0111SDavid Daney 			     IORESOURCE_PREFETCH;
1340d04d0111SDavid Daney 
1341d04d0111SDavid Daney 	pci_bus_for_each_resource(b, parent_r, i) {
1342d04d0111SDavid Daney 		if (!parent_r)
1343d04d0111SDavid Daney 			continue;
1344d04d0111SDavid Daney 
1345d04d0111SDavid Daney 		if ((r->flags & mask) == (parent_r->flags & mask) &&
1346d04d0111SDavid Daney 		    resource_contains(parent_r, r))
1347d04d0111SDavid Daney 			request_resource(parent_r, r);
1348d04d0111SDavid Daney 	}
1349d04d0111SDavid Daney }
1350d04d0111SDavid Daney 
1351d04d0111SDavid Daney /*
13520d607618SNicholas Johnson  * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they are
13530d607618SNicholas Johnson  * skipped by pbus_assign_resources_sorted().
1354d04d0111SDavid Daney  */
1355d04d0111SDavid Daney static void pdev_assign_fixed_resources(struct pci_dev *dev)
1356d04d0111SDavid Daney {
1357*09cc9006SMika Westerberg 	struct resource *r;
1358d04d0111SDavid Daney 
1359*09cc9006SMika Westerberg 	pci_dev_for_each_resource(dev, r) {
1360d04d0111SDavid Daney 		struct pci_bus *b;
1361d04d0111SDavid Daney 
1362d04d0111SDavid Daney 		if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) ||
1363d04d0111SDavid Daney 		    !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
1364d04d0111SDavid Daney 			continue;
1365d04d0111SDavid Daney 
1366d04d0111SDavid Daney 		b = dev->bus;
1367d04d0111SDavid Daney 		while (b && !r->parent) {
1368d04d0111SDavid Daney 			assign_fixed_resource_on_bus(b, r);
1369d04d0111SDavid Daney 			b = b->parent;
1370d04d0111SDavid Daney 		}
1371d04d0111SDavid Daney 	}
1372d04d0111SDavid Daney }
1373d04d0111SDavid Daney 
137410874f5aSBjorn Helgaas void __pci_bus_assign_resources(const struct pci_bus *bus,
1375bdc4abecSYinghai Lu 				struct list_head *realloc_head,
1376bdc4abecSYinghai Lu 				struct list_head *fail_head)
13771da177e4SLinus Torvalds {
13781da177e4SLinus Torvalds 	struct pci_bus *b;
13791da177e4SLinus Torvalds 	struct pci_dev *dev;
13801da177e4SLinus Torvalds 
13819e8bf93aSRam Pai 	pbus_assign_resources_sorted(bus, realloc_head, fail_head);
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
1384d04d0111SDavid Daney 		pdev_assign_fixed_resources(dev);
1385d04d0111SDavid Daney 
13861da177e4SLinus Torvalds 		b = dev->subordinate;
13871da177e4SLinus Torvalds 		if (!b)
13881da177e4SLinus Torvalds 			continue;
13891da177e4SLinus Torvalds 
13909e8bf93aSRam Pai 		__pci_bus_assign_resources(b, realloc_head, fail_head);
13911da177e4SLinus Torvalds 
1392b2fb5cc5SHonghui Zhang 		switch (dev->hdr_type) {
1393b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_BRIDGE:
13946841ec68SYinghai Lu 			if (!pci_is_enabled(dev))
13951da177e4SLinus Torvalds 				pci_setup_bridge(b);
13961da177e4SLinus Torvalds 			break;
13971da177e4SLinus Torvalds 
1398b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_CARDBUS:
13991da177e4SLinus Torvalds 			pci_setup_cardbus(b);
14001da177e4SLinus Torvalds 			break;
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 		default:
14037506dc79SFrederick Lawler 			pci_info(dev, "not setting up bridge for bus %04x:%02x\n",
1404227f0647SRyan Desfosses 				 pci_domain_nr(b), b->number);
14051da177e4SLinus Torvalds 			break;
14061da177e4SLinus Torvalds 		}
14071da177e4SLinus Torvalds 	}
14081da177e4SLinus Torvalds }
1409568ddef8SYinghai Lu 
141010874f5aSBjorn Helgaas void pci_bus_assign_resources(const struct pci_bus *bus)
1411568ddef8SYinghai Lu {
1412c8adf9a3SRam Pai 	__pci_bus_assign_resources(bus, NULL, NULL);
1413568ddef8SYinghai Lu }
14141da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
14151da177e4SLinus Torvalds 
1416765bf9b7SLorenzo Pieralisi static void pci_claim_device_resources(struct pci_dev *dev)
1417765bf9b7SLorenzo Pieralisi {
1418765bf9b7SLorenzo Pieralisi 	int i;
1419765bf9b7SLorenzo Pieralisi 
1420765bf9b7SLorenzo Pieralisi 	for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
1421765bf9b7SLorenzo Pieralisi 		struct resource *r = &dev->resource[i];
1422765bf9b7SLorenzo Pieralisi 
1423765bf9b7SLorenzo Pieralisi 		if (!r->flags || r->parent)
1424765bf9b7SLorenzo Pieralisi 			continue;
1425765bf9b7SLorenzo Pieralisi 
1426765bf9b7SLorenzo Pieralisi 		pci_claim_resource(dev, i);
1427765bf9b7SLorenzo Pieralisi 	}
1428765bf9b7SLorenzo Pieralisi }
1429765bf9b7SLorenzo Pieralisi 
1430765bf9b7SLorenzo Pieralisi static void pci_claim_bridge_resources(struct pci_dev *dev)
1431765bf9b7SLorenzo Pieralisi {
1432765bf9b7SLorenzo Pieralisi 	int i;
1433765bf9b7SLorenzo Pieralisi 
1434765bf9b7SLorenzo Pieralisi 	for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
1435765bf9b7SLorenzo Pieralisi 		struct resource *r = &dev->resource[i];
1436765bf9b7SLorenzo Pieralisi 
1437765bf9b7SLorenzo Pieralisi 		if (!r->flags || r->parent)
1438765bf9b7SLorenzo Pieralisi 			continue;
1439765bf9b7SLorenzo Pieralisi 
1440765bf9b7SLorenzo Pieralisi 		pci_claim_bridge_resource(dev, i);
1441765bf9b7SLorenzo Pieralisi 	}
1442765bf9b7SLorenzo Pieralisi }
1443765bf9b7SLorenzo Pieralisi 
1444765bf9b7SLorenzo Pieralisi static void pci_bus_allocate_dev_resources(struct pci_bus *b)
1445765bf9b7SLorenzo Pieralisi {
1446765bf9b7SLorenzo Pieralisi 	struct pci_dev *dev;
1447765bf9b7SLorenzo Pieralisi 	struct pci_bus *child;
1448765bf9b7SLorenzo Pieralisi 
1449765bf9b7SLorenzo Pieralisi 	list_for_each_entry(dev, &b->devices, bus_list) {
1450765bf9b7SLorenzo Pieralisi 		pci_claim_device_resources(dev);
1451765bf9b7SLorenzo Pieralisi 
1452765bf9b7SLorenzo Pieralisi 		child = dev->subordinate;
1453765bf9b7SLorenzo Pieralisi 		if (child)
1454765bf9b7SLorenzo Pieralisi 			pci_bus_allocate_dev_resources(child);
1455765bf9b7SLorenzo Pieralisi 	}
1456765bf9b7SLorenzo Pieralisi }
1457765bf9b7SLorenzo Pieralisi 
1458765bf9b7SLorenzo Pieralisi static void pci_bus_allocate_resources(struct pci_bus *b)
1459765bf9b7SLorenzo Pieralisi {
1460765bf9b7SLorenzo Pieralisi 	struct pci_bus *child;
1461765bf9b7SLorenzo Pieralisi 
1462765bf9b7SLorenzo Pieralisi 	/*
14630d607618SNicholas Johnson 	 * Carry out a depth-first search on the PCI bus tree to allocate
14640d607618SNicholas Johnson 	 * bridge apertures.  Read the programmed bridge bases and
14650d607618SNicholas Johnson 	 * recursively claim the respective bridge resources.
1466765bf9b7SLorenzo Pieralisi 	 */
1467765bf9b7SLorenzo Pieralisi 	if (b->self) {
1468765bf9b7SLorenzo Pieralisi 		pci_read_bridge_bases(b);
1469765bf9b7SLorenzo Pieralisi 		pci_claim_bridge_resources(b->self);
1470765bf9b7SLorenzo Pieralisi 	}
1471765bf9b7SLorenzo Pieralisi 
1472765bf9b7SLorenzo Pieralisi 	list_for_each_entry(child, &b->children, node)
1473765bf9b7SLorenzo Pieralisi 		pci_bus_allocate_resources(child);
1474765bf9b7SLorenzo Pieralisi }
1475765bf9b7SLorenzo Pieralisi 
1476765bf9b7SLorenzo Pieralisi void pci_bus_claim_resources(struct pci_bus *b)
1477765bf9b7SLorenzo Pieralisi {
1478765bf9b7SLorenzo Pieralisi 	pci_bus_allocate_resources(b);
1479765bf9b7SLorenzo Pieralisi 	pci_bus_allocate_dev_resources(b);
1480765bf9b7SLorenzo Pieralisi }
1481765bf9b7SLorenzo Pieralisi EXPORT_SYMBOL(pci_bus_claim_resources);
1482765bf9b7SLorenzo Pieralisi 
148310874f5aSBjorn Helgaas static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
1484bdc4abecSYinghai Lu 					  struct list_head *add_head,
1485bdc4abecSYinghai Lu 					  struct list_head *fail_head)
14866841ec68SYinghai Lu {
14876841ec68SYinghai Lu 	struct pci_bus *b;
14886841ec68SYinghai Lu 
14898424d759SYinghai Lu 	pdev_assign_resources_sorted((struct pci_dev *)bridge,
14908424d759SYinghai Lu 					 add_head, fail_head);
14916841ec68SYinghai Lu 
14926841ec68SYinghai Lu 	b = bridge->subordinate;
14936841ec68SYinghai Lu 	if (!b)
14946841ec68SYinghai Lu 		return;
14956841ec68SYinghai Lu 
14968424d759SYinghai Lu 	__pci_bus_assign_resources(b, add_head, fail_head);
14976841ec68SYinghai Lu 
14986841ec68SYinghai Lu 	switch (bridge->class >> 8) {
14996841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_PCI:
15006841ec68SYinghai Lu 		pci_setup_bridge(b);
15016841ec68SYinghai Lu 		break;
15026841ec68SYinghai Lu 
15036841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_CARDBUS:
15046841ec68SYinghai Lu 		pci_setup_cardbus(b);
15056841ec68SYinghai Lu 		break;
15066841ec68SYinghai Lu 
15076841ec68SYinghai Lu 	default:
15087506dc79SFrederick Lawler 		pci_info(bridge, "not setting up bridge for bus %04x:%02x\n",
1509227f0647SRyan Desfosses 			 pci_domain_nr(b), b->number);
15106841ec68SYinghai Lu 		break;
15116841ec68SYinghai Lu 	}
15126841ec68SYinghai Lu }
1513cb21bc94SChristian König 
1514cb21bc94SChristian König #define PCI_RES_TYPE_MASK \
1515cb21bc94SChristian König 	(IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\
1516cb21bc94SChristian König 	 IORESOURCE_MEM_64)
1517cb21bc94SChristian König 
15185009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus,
15195009b460SYinghai Lu 					 unsigned long type)
15205009b460SYinghai Lu {
15215b285415SYinghai Lu 	struct pci_dev *dev = bus->self;
15225009b460SYinghai Lu 	struct resource *r;
1523c50762a8SBjorn Helgaas 	unsigned int old_flags;
15245b285415SYinghai Lu 	struct resource *b_res;
15255b285415SYinghai Lu 	int idx = 1;
15265009b460SYinghai Lu 
15275b285415SYinghai Lu 	b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
15285b285415SYinghai Lu 
15295b285415SYinghai Lu 	/*
15300d607618SNicholas Johnson 	 * 1. If IO port assignment fails, release bridge IO port.
15310d607618SNicholas Johnson 	 * 2. If non pref MMIO assignment fails, release bridge nonpref MMIO.
15320d607618SNicholas Johnson 	 * 3. If 64bit pref MMIO assignment fails, and bridge pref is 64bit,
15330d607618SNicholas Johnson 	 *    release bridge pref MMIO.
15340d607618SNicholas Johnson 	 * 4. If pref MMIO assignment fails, and bridge pref is 32bit,
15350d607618SNicholas Johnson 	 *    release bridge pref MMIO.
15360d607618SNicholas Johnson 	 * 5. If pref MMIO assignment fails, and bridge pref is not
15370d607618SNicholas Johnson 	 *    assigned, release bridge nonpref MMIO.
15385b285415SYinghai Lu 	 */
15395b285415SYinghai Lu 	if (type & IORESOURCE_IO)
15405b285415SYinghai Lu 		idx = 0;
15415b285415SYinghai Lu 	else if (!(type & IORESOURCE_PREFETCH))
15425b285415SYinghai Lu 		idx = 1;
15435b285415SYinghai Lu 	else if ((type & IORESOURCE_MEM_64) &&
15445b285415SYinghai Lu 		 (b_res[2].flags & IORESOURCE_MEM_64))
15455b285415SYinghai Lu 		idx = 2;
15465b285415SYinghai Lu 	else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
15475b285415SYinghai Lu 		 (b_res[2].flags & IORESOURCE_PREFETCH))
15485b285415SYinghai Lu 		idx = 2;
15495b285415SYinghai Lu 	else
15505b285415SYinghai Lu 		idx = 1;
15515b285415SYinghai Lu 
15525b285415SYinghai Lu 	r = &b_res[idx];
15535b285415SYinghai Lu 
15545009b460SYinghai Lu 	if (!r->parent)
15555b285415SYinghai Lu 		return;
15565b285415SYinghai Lu 
15570d607618SNicholas Johnson 	/* If there are children, release them all */
15585009b460SYinghai Lu 	release_child_resources(r);
15595009b460SYinghai Lu 	if (!release_resource(r)) {
1560cb21bc94SChristian König 		type = old_flags = r->flags & PCI_RES_TYPE_MASK;
156134c6b710SMohan Kumar 		pci_info(dev, "resource %d %pR released\n",
15625b285415SYinghai Lu 			 PCI_BRIDGE_RESOURCES + idx, r);
15630d607618SNicholas Johnson 		/* Keep the old size */
15645009b460SYinghai Lu 		r->end = resource_size(r) - 1;
15655009b460SYinghai Lu 		r->start = 0;
15665009b460SYinghai Lu 		r->flags = 0;
15675009b460SYinghai Lu 
15680d607618SNicholas Johnson 		/* Avoiding touch the one without PREF */
15695009b460SYinghai Lu 		if (type & IORESOURCE_PREFETCH)
15705009b460SYinghai Lu 			type = IORESOURCE_PREFETCH;
15715009b460SYinghai Lu 		__pci_setup_bridge(bus, type);
15720d607618SNicholas Johnson 		/* For next child res under same bridge */
15735b285415SYinghai Lu 		r->flags = old_flags;
15745009b460SYinghai Lu 	}
15755009b460SYinghai Lu }
15765009b460SYinghai Lu 
15775009b460SYinghai Lu enum release_type {
15785009b460SYinghai Lu 	leaf_only,
15795009b460SYinghai Lu 	whole_subtree,
15805009b460SYinghai Lu };
15810d607618SNicholas Johnson 
15825009b460SYinghai Lu /*
15830d607618SNicholas Johnson  * Try to release PCI bridge resources from leaf bridge, so we can allocate
15840d607618SNicholas Johnson  * a larger window later.
15855009b460SYinghai Lu  */
158610874f5aSBjorn Helgaas static void pci_bus_release_bridge_resources(struct pci_bus *bus,
15875009b460SYinghai Lu 					     unsigned long type,
15885009b460SYinghai Lu 					     enum release_type rel_type)
15895009b460SYinghai Lu {
15905009b460SYinghai Lu 	struct pci_dev *dev;
15915009b460SYinghai Lu 	bool is_leaf_bridge = true;
15925009b460SYinghai Lu 
15935009b460SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
15945009b460SYinghai Lu 		struct pci_bus *b = dev->subordinate;
15955009b460SYinghai Lu 		if (!b)
15965009b460SYinghai Lu 			continue;
15975009b460SYinghai Lu 
15985009b460SYinghai Lu 		is_leaf_bridge = false;
15995009b460SYinghai Lu 
16005009b460SYinghai Lu 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
16015009b460SYinghai Lu 			continue;
16025009b460SYinghai Lu 
16035009b460SYinghai Lu 		if (rel_type == whole_subtree)
16045009b460SYinghai Lu 			pci_bus_release_bridge_resources(b, type,
16055009b460SYinghai Lu 						 whole_subtree);
16065009b460SYinghai Lu 	}
16075009b460SYinghai Lu 
16085009b460SYinghai Lu 	if (pci_is_root_bus(bus))
16095009b460SYinghai Lu 		return;
16105009b460SYinghai Lu 
16115009b460SYinghai Lu 	if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
16125009b460SYinghai Lu 		return;
16135009b460SYinghai Lu 
16145009b460SYinghai Lu 	if ((rel_type == whole_subtree) || is_leaf_bridge)
16155009b460SYinghai Lu 		pci_bridge_release_resources(bus, type);
16165009b460SYinghai Lu }
16175009b460SYinghai Lu 
161876fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
161976fbc263SYinghai Lu {
162089a74eccSBjorn Helgaas 	struct resource *res;
162176fbc263SYinghai Lu 	int i;
162276fbc263SYinghai Lu 
162389a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
16247c9342b8SYinghai Lu 		if (!res || !res->end || !res->flags)
162576fbc263SYinghai Lu 			continue;
162676fbc263SYinghai Lu 
162734c6b710SMohan Kumar 		dev_info(&bus->dev, "resource %d %pR\n", i, res);
162876fbc263SYinghai Lu 	}
162976fbc263SYinghai Lu }
163076fbc263SYinghai Lu 
163176fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
163276fbc263SYinghai Lu {
163376fbc263SYinghai Lu 	struct pci_bus *b;
163476fbc263SYinghai Lu 	struct pci_dev *dev;
163576fbc263SYinghai Lu 
163676fbc263SYinghai Lu 
163776fbc263SYinghai Lu 	pci_bus_dump_res(bus);
163876fbc263SYinghai Lu 
163976fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
164076fbc263SYinghai Lu 		b = dev->subordinate;
164176fbc263SYinghai Lu 		if (!b)
164276fbc263SYinghai Lu 			continue;
164376fbc263SYinghai Lu 
164476fbc263SYinghai Lu 		pci_bus_dump_resources(b);
164576fbc263SYinghai Lu 	}
164676fbc263SYinghai Lu }
164776fbc263SYinghai Lu 
1648ff35147cSYinghai Lu static int pci_bus_get_depth(struct pci_bus *bus)
1649da7822e5SYinghai Lu {
1650da7822e5SYinghai Lu 	int depth = 0;
1651f2a230bdSWei Yang 	struct pci_bus *child_bus;
1652da7822e5SYinghai Lu 
1653f2a230bdSWei Yang 	list_for_each_entry(child_bus, &bus->children, node) {
1654da7822e5SYinghai Lu 		int ret;
1655da7822e5SYinghai Lu 
1656f2a230bdSWei Yang 		ret = pci_bus_get_depth(child_bus);
1657da7822e5SYinghai Lu 		if (ret + 1 > depth)
1658da7822e5SYinghai Lu 			depth = ret + 1;
1659da7822e5SYinghai Lu 	}
1660da7822e5SYinghai Lu 
1661da7822e5SYinghai Lu 	return depth;
1662da7822e5SYinghai Lu }
1663da7822e5SYinghai Lu 
1664b55438fdSYinghai Lu /*
1665b55438fdSYinghai Lu  * -1: undefined, will auto detect later
1666b55438fdSYinghai Lu  *  0: disabled by user
1667b55438fdSYinghai Lu  *  1: disabled by auto detect
1668b55438fdSYinghai Lu  *  2: enabled by user
1669b55438fdSYinghai Lu  *  3: enabled by auto detect
1670b55438fdSYinghai Lu  */
1671b55438fdSYinghai Lu enum enable_type {
1672b55438fdSYinghai Lu 	undefined = -1,
1673b55438fdSYinghai Lu 	user_disabled,
1674b55438fdSYinghai Lu 	auto_disabled,
1675b55438fdSYinghai Lu 	user_enabled,
1676b55438fdSYinghai Lu 	auto_enabled,
1677b55438fdSYinghai Lu };
1678b55438fdSYinghai Lu 
1679ff35147cSYinghai Lu static enum enable_type pci_realloc_enable = undefined;
1680b55438fdSYinghai Lu void __init pci_realloc_get_opt(char *str)
1681b55438fdSYinghai Lu {
1682b55438fdSYinghai Lu 	if (!strncmp(str, "off", 3))
1683b55438fdSYinghai Lu 		pci_realloc_enable = user_disabled;
1684b55438fdSYinghai Lu 	else if (!strncmp(str, "on", 2))
1685b55438fdSYinghai Lu 		pci_realloc_enable = user_enabled;
1686b55438fdSYinghai Lu }
1687ff35147cSYinghai Lu static bool pci_realloc_enabled(enum enable_type enable)
1688b55438fdSYinghai Lu {
1689967260cdSYinghai Lu 	return enable >= user_enabled;
1690b55438fdSYinghai Lu }
1691f483d392SRam Pai 
1692b07f2ebcSYinghai Lu #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1693ff35147cSYinghai Lu static int iov_resources_unassigned(struct pci_dev *dev, void *data)
1694223d96fcSYinghai Lu {
1695b07f2ebcSYinghai Lu 	int i;
1696223d96fcSYinghai Lu 	bool *unassigned = data;
1697b07f2ebcSYinghai Lu 
169839098edbSDenis Efremov 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
169939098edbSDenis Efremov 		struct resource *r = &dev->resource[i + PCI_IOV_RESOURCES];
1700fa216bf4SYinghai Lu 		struct pci_bus_region region;
1701b07f2ebcSYinghai Lu 
1702223d96fcSYinghai Lu 		/* Not assigned or rejected by kernel? */
1703fa216bf4SYinghai Lu 		if (!r->flags)
1704fa216bf4SYinghai Lu 			continue;
1705b07f2ebcSYinghai Lu 
1706fc279850SYinghai Lu 		pcibios_resource_to_bus(dev->bus, &region, r);
1707fa216bf4SYinghai Lu 		if (!region.start) {
1708223d96fcSYinghai Lu 			*unassigned = true;
17090d607618SNicholas Johnson 			return 1; /* Return early from pci_walk_bus() */
1710b07f2ebcSYinghai Lu 		}
1711b07f2ebcSYinghai Lu 	}
1712b07f2ebcSYinghai Lu 
1713223d96fcSYinghai Lu 	return 0;
1714223d96fcSYinghai Lu }
1715223d96fcSYinghai Lu 
1716ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1717967260cdSYinghai Lu 					   enum enable_type enable_local)
1718223d96fcSYinghai Lu {
1719223d96fcSYinghai Lu 	bool unassigned = false;
17207ac0d094SBenjamin Herrenschmidt 	struct pci_host_bridge *host;
1721223d96fcSYinghai Lu 
1722967260cdSYinghai Lu 	if (enable_local != undefined)
1723967260cdSYinghai Lu 		return enable_local;
1724223d96fcSYinghai Lu 
17257ac0d094SBenjamin Herrenschmidt 	host = pci_find_host_bridge(bus);
17267ac0d094SBenjamin Herrenschmidt 	if (host->preserve_config)
17277ac0d094SBenjamin Herrenschmidt 		return auto_disabled;
17287ac0d094SBenjamin Herrenschmidt 
1729223d96fcSYinghai Lu 	pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
1730967260cdSYinghai Lu 	if (unassigned)
1731967260cdSYinghai Lu 		return auto_enabled;
1732967260cdSYinghai Lu 
1733967260cdSYinghai Lu 	return enable_local;
1734b07f2ebcSYinghai Lu }
1735223d96fcSYinghai Lu #else
1736ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1737967260cdSYinghai Lu 					   enum enable_type enable_local)
1738967260cdSYinghai Lu {
1739967260cdSYinghai Lu 	return enable_local;
1740b07f2ebcSYinghai Lu }
1741b07f2ebcSYinghai Lu #endif
1742b07f2ebcSYinghai Lu 
17431e58f4e1SNicholas Johnson static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res,
17440d607618SNicholas Johnson 				 struct list_head *add_list,
17453d264da9SNicholas Johnson 				 resource_size_t new_size)
17461a576772SMika Westerberg {
174794867573SNicholas Johnson 	resource_size_t add_size, size = resource_size(res);
17481a576772SMika Westerberg 
17491a576772SMika Westerberg 	if (res->parent)
17501a576772SMika Westerberg 		return;
17511a576772SMika Westerberg 
175294867573SNicholas Johnson 	if (!new_size)
17531a576772SMika Westerberg 		return;
17541a576772SMika Westerberg 
175594867573SNicholas Johnson 	if (new_size > size) {
175694867573SNicholas Johnson 		add_size = new_size - size;
175794867573SNicholas Johnson 		pci_dbg(bridge, "bridge window %pR extended by %pa\n", res,
175894867573SNicholas Johnson 			&add_size);
175994867573SNicholas Johnson 	} else if (new_size < size) {
176094867573SNicholas Johnson 		add_size = size - new_size;
176194867573SNicholas Johnson 		pci_dbg(bridge, "bridge window %pR shrunken by %pa\n", res,
176294867573SNicholas Johnson 			&add_size);
17639db0b9b6SMika Westerberg 	} else {
17649db0b9b6SMika Westerberg 		return;
176594867573SNicholas Johnson 	}
176694867573SNicholas Johnson 
1767ae4611f1SNicholas Johnson 	res->end = res->start + new_size - 1;
17687180c1d0SMika Westerberg 
17697180c1d0SMika Westerberg 	/* If the resource is part of the add_list, remove it now */
17707180c1d0SMika Westerberg 	if (add_list)
1771ae4611f1SNicholas Johnson 		remove_from_list(add_list, res);
17721a576772SMika Westerberg }
17731a576772SMika Westerberg 
17749db0b9b6SMika Westerberg static void remove_dev_resource(struct resource *avail, struct pci_dev *dev,
17759db0b9b6SMika Westerberg 				struct resource *res)
17769db0b9b6SMika Westerberg {
17779db0b9b6SMika Westerberg 	resource_size_t size, align, tmp;
17789db0b9b6SMika Westerberg 
17799db0b9b6SMika Westerberg 	size = resource_size(res);
17809db0b9b6SMika Westerberg 	if (!size)
17819db0b9b6SMika Westerberg 		return;
17829db0b9b6SMika Westerberg 
17839db0b9b6SMika Westerberg 	align = pci_resource_alignment(dev, res);
17849db0b9b6SMika Westerberg 	align = align ? ALIGN(avail->start, align) - avail->start : 0;
17859db0b9b6SMika Westerberg 	tmp = align + size;
17869db0b9b6SMika Westerberg 	avail->start = min(avail->start + tmp, avail->end + 1);
17879db0b9b6SMika Westerberg }
17889db0b9b6SMika Westerberg 
17899db0b9b6SMika Westerberg static void remove_dev_resources(struct pci_dev *dev, struct resource *io,
17909db0b9b6SMika Westerberg 				 struct resource *mmio,
17919db0b9b6SMika Westerberg 				 struct resource *mmio_pref)
17929db0b9b6SMika Westerberg {
1793*09cc9006SMika Westerberg 	struct resource *res;
17949db0b9b6SMika Westerberg 
1795*09cc9006SMika Westerberg 	pci_dev_for_each_resource(dev, res) {
17969db0b9b6SMika Westerberg 		if (resource_type(res) == IORESOURCE_IO) {
17979db0b9b6SMika Westerberg 			remove_dev_resource(io, dev, res);
17989db0b9b6SMika Westerberg 		} else if (resource_type(res) == IORESOURCE_MEM) {
17999db0b9b6SMika Westerberg 
18009db0b9b6SMika Westerberg 			/*
18019db0b9b6SMika Westerberg 			 * Make sure prefetchable memory is reduced from
18029db0b9b6SMika Westerberg 			 * the correct resource. Specifically we put 32-bit
18039db0b9b6SMika Westerberg 			 * prefetchable memory in non-prefetchable window
18049db0b9b6SMika Westerberg 			 * if there is an 64-bit pretchable window.
18059db0b9b6SMika Westerberg 			 *
18069db0b9b6SMika Westerberg 			 * See comments in __pci_bus_size_bridges() for
18079db0b9b6SMika Westerberg 			 * more information.
18089db0b9b6SMika Westerberg 			 */
18099db0b9b6SMika Westerberg 			if ((res->flags & IORESOURCE_PREFETCH) &&
18109db0b9b6SMika Westerberg 			    ((res->flags & IORESOURCE_MEM_64) ==
18119db0b9b6SMika Westerberg 			     (mmio_pref->flags & IORESOURCE_MEM_64)))
18129db0b9b6SMika Westerberg 				remove_dev_resource(mmio_pref, dev, res);
18139db0b9b6SMika Westerberg 			else
18149db0b9b6SMika Westerberg 				remove_dev_resource(mmio, dev, res);
18159db0b9b6SMika Westerberg 		}
18169db0b9b6SMika Westerberg 	}
18179db0b9b6SMika Westerberg }
18189db0b9b6SMika Westerberg 
18199db0b9b6SMika Westerberg /*
18209db0b9b6SMika Westerberg  * io, mmio and mmio_pref contain the total amount of bridge window space
18219db0b9b6SMika Westerberg  * available. This includes the minimal space needed to cover all the
18229db0b9b6SMika Westerberg  * existing devices on the bus and the possible extra space that can be
18239db0b9b6SMika Westerberg  * shared with the bridges.
18249db0b9b6SMika Westerberg  */
18251a576772SMika Westerberg static void pci_bus_distribute_available_resources(struct pci_bus *bus,
18260d607618SNicholas Johnson 					    struct list_head *add_list,
1827d555a50fSNicholas Johnson 					    struct resource io,
1828d555a50fSNicholas Johnson 					    struct resource mmio,
1829d555a50fSNicholas Johnson 					    struct resource mmio_pref)
18301a576772SMika Westerberg {
18311a576772SMika Westerberg 	unsigned int normal_bridges = 0, hotplug_bridges = 0;
18321a576772SMika Westerberg 	struct resource *io_res, *mmio_res, *mmio_pref_res;
18331a576772SMika Westerberg 	struct pci_dev *dev, *bridge = bus->self;
18349db0b9b6SMika Westerberg 	resource_size_t io_per_b, mmio_per_b, mmio_pref_per_b, align;
18351a576772SMika Westerberg 
18366e0688dbSKrzysztof Wilczynski 	io_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
18376e0688dbSKrzysztof Wilczynski 	mmio_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
18386e0688dbSKrzysztof Wilczynski 	mmio_pref_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
18391a576772SMika Westerberg 
18401a576772SMika Westerberg 	/*
1841f924c26eSNicholas Johnson 	 * The alignment of this bridge is yet to be considered, hence it must
1842f924c26eSNicholas Johnson 	 * be done now before extending its bridge window.
1843f924c26eSNicholas Johnson 	 */
1844f924c26eSNicholas Johnson 	align = pci_resource_alignment(bridge, io_res);
1845f924c26eSNicholas Johnson 	if (!io_res->parent && align)
1846f924c26eSNicholas Johnson 		io.start = min(ALIGN(io.start, align), io.end + 1);
1847f924c26eSNicholas Johnson 
1848f924c26eSNicholas Johnson 	align = pci_resource_alignment(bridge, mmio_res);
1849f924c26eSNicholas Johnson 	if (!mmio_res->parent && align)
1850f924c26eSNicholas Johnson 		mmio.start = min(ALIGN(mmio.start, align), mmio.end + 1);
1851f924c26eSNicholas Johnson 
1852f924c26eSNicholas Johnson 	align = pci_resource_alignment(bridge, mmio_pref_res);
1853f924c26eSNicholas Johnson 	if (!mmio_pref_res->parent && align)
1854f924c26eSNicholas Johnson 		mmio_pref.start = min(ALIGN(mmio_pref.start, align),
1855f924c26eSNicholas Johnson 			mmio_pref.end + 1);
1856f924c26eSNicholas Johnson 
1857f924c26eSNicholas Johnson 	/*
1858ae4611f1SNicholas Johnson 	 * Now that we have adjusted for alignment, update the bridge window
1859ae4611f1SNicholas Johnson 	 * resources to fill as much remaining resource space as possible.
18601a576772SMika Westerberg 	 */
18611e58f4e1SNicholas Johnson 	adjust_bridge_window(bridge, io_res, add_list, resource_size(&io));
18621e58f4e1SNicholas Johnson 	adjust_bridge_window(bridge, mmio_res, add_list, resource_size(&mmio));
18631e58f4e1SNicholas Johnson 	adjust_bridge_window(bridge, mmio_pref_res, add_list,
186477793854SNicholas Johnson 			     resource_size(&mmio_pref));
18651a576772SMika Westerberg 
18661a576772SMika Westerberg 	/*
18671a576772SMika Westerberg 	 * Calculate how many hotplug bridges and normal bridges there
18681a576772SMika Westerberg 	 * are on this bus.  We will distribute the additional available
18691a576772SMika Westerberg 	 * resources between hotplug bridges.
18701a576772SMika Westerberg 	 */
18711a576772SMika Westerberg 	for_each_pci_bridge(dev, bus) {
18721a576772SMika Westerberg 		if (dev->is_hotplug_bridge)
18731a576772SMika Westerberg 			hotplug_bridges++;
18741a576772SMika Westerberg 		else
18751a576772SMika Westerberg 			normal_bridges++;
18761a576772SMika Westerberg 	}
18771a576772SMika Westerberg 
18789db0b9b6SMika Westerberg 	if (!(hotplug_bridges + normal_bridges))
18796a381ea6SNicholas Johnson 		return;
18806a381ea6SNicholas Johnson 
18815c6bcc34SNicholas Johnson 	/*
18829db0b9b6SMika Westerberg 	 * Calculate the amount of space we can forward from "bus" to any
18839db0b9b6SMika Westerberg 	 * downstream buses, i.e., the space left over after assigning the
18849db0b9b6SMika Westerberg 	 * BARs and windows on "bus".
18855c6bcc34SNicholas Johnson 	 */
18869db0b9b6SMika Westerberg 	list_for_each_entry(dev, &bus->devices, bus_list) {
18879db0b9b6SMika Westerberg 		if (!dev->is_virtfn)
18889db0b9b6SMika Westerberg 			remove_dev_resources(dev, &io, &mmio, &mmio_pref);
18891a576772SMika Westerberg 	}
18901a576772SMika Westerberg 
18919db0b9b6SMika Westerberg 	/*
18929db0b9b6SMika Westerberg 	 * If there is at least one hotplug bridge on this bus it gets all
18939db0b9b6SMika Westerberg 	 * the extra resource space that was left after the reductions
18949db0b9b6SMika Westerberg 	 * above.
18959db0b9b6SMika Westerberg 	 *
18969db0b9b6SMika Westerberg 	 * If there are no hotplug bridges the extra resource space is
18979db0b9b6SMika Westerberg 	 * split between non-hotplug bridges. This is to allow possible
18989db0b9b6SMika Westerberg 	 * hotplug bridges below them to get the extra space as well.
18999db0b9b6SMika Westerberg 	 */
19009db0b9b6SMika Westerberg 	if (hotplug_bridges) {
19019db0b9b6SMika Westerberg 		io_per_b = div64_ul(resource_size(&io), hotplug_bridges);
19029db0b9b6SMika Westerberg 		mmio_per_b = div64_ul(resource_size(&mmio), hotplug_bridges);
19039db0b9b6SMika Westerberg 		mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
1904f924c26eSNicholas Johnson 					   hotplug_bridges);
19059db0b9b6SMika Westerberg 	} else {
19069db0b9b6SMika Westerberg 		io_per_b = div64_ul(resource_size(&io), normal_bridges);
19079db0b9b6SMika Westerberg 		mmio_per_b = div64_ul(resource_size(&mmio), normal_bridges);
19089db0b9b6SMika Westerberg 		mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
19099db0b9b6SMika Westerberg 					   normal_bridges);
19109db0b9b6SMika Westerberg 	}
1911f924c26eSNicholas Johnson 
19121a576772SMika Westerberg 	for_each_pci_bridge(dev, bus) {
191308f0a15eSMika Westerberg 		struct resource *res;
19141a576772SMika Westerberg 		struct pci_bus *b;
19151a576772SMika Westerberg 
19161a576772SMika Westerberg 		b = dev->subordinate;
19179db0b9b6SMika Westerberg 		if (!b)
19189db0b9b6SMika Westerberg 			continue;
19199db0b9b6SMika Westerberg 		if (hotplug_bridges && !dev->is_hotplug_bridge)
19201a576772SMika Westerberg 			continue;
19211a576772SMika Westerberg 
192208f0a15eSMika Westerberg 		res = &dev->resource[PCI_BRIDGE_IO_WINDOW];
19239db0b9b6SMika Westerberg 
19249db0b9b6SMika Westerberg 		/*
19259db0b9b6SMika Westerberg 		 * Make sure the split resource space is properly aligned
19269db0b9b6SMika Westerberg 		 * for bridge windows (align it down to avoid going above
19279db0b9b6SMika Westerberg 		 * what is available).
19289db0b9b6SMika Westerberg 		 */
192908f0a15eSMika Westerberg 		align = pci_resource_alignment(dev, res);
19309db0b9b6SMika Westerberg 		io.end = align ? io.start + ALIGN_DOWN(io_per_b, align) - 1
19319db0b9b6SMika Westerberg 			       : io.start + io_per_b - 1;
19329db0b9b6SMika Westerberg 
19339db0b9b6SMika Westerberg 		/*
19349db0b9b6SMika Westerberg 		 * The x_per_b holds the extra resource space that can be
19359db0b9b6SMika Westerberg 		 * added for each bridge but there is the minimal already
19369db0b9b6SMika Westerberg 		 * reserved as well so adjust x.start down accordingly to
19379db0b9b6SMika Westerberg 		 * cover the whole space.
19389db0b9b6SMika Westerberg 		 */
19399db0b9b6SMika Westerberg 		io.start -= resource_size(res);
194008f0a15eSMika Westerberg 
194108f0a15eSMika Westerberg 		res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
194208f0a15eSMika Westerberg 		align = pci_resource_alignment(dev, res);
19439db0b9b6SMika Westerberg 		mmio.end = align ? mmio.start + ALIGN_DOWN(mmio_per_b, align) - 1
19449db0b9b6SMika Westerberg 				 : mmio.start + mmio_per_b - 1;
19459db0b9b6SMika Westerberg 		mmio.start -= resource_size(res);
194608f0a15eSMika Westerberg 
194708f0a15eSMika Westerberg 		res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
194808f0a15eSMika Westerberg 		align = pci_resource_alignment(dev, res);
194908f0a15eSMika Westerberg 		mmio_pref.end = align ? mmio_pref.start +
19509db0b9b6SMika Westerberg 					ALIGN_DOWN(mmio_pref_per_b, align) - 1
19519db0b9b6SMika Westerberg 				      : mmio_pref.start + mmio_pref_per_b - 1;
19529db0b9b6SMika Westerberg 		mmio_pref.start -= resource_size(res);
1953d555a50fSNicholas Johnson 
1954d555a50fSNicholas Johnson 		pci_bus_distribute_available_resources(b, add_list, io, mmio,
1955d555a50fSNicholas Johnson 						       mmio_pref);
1956f924c26eSNicholas Johnson 
195708f0a15eSMika Westerberg 		io.start += io.end + 1;
195808f0a15eSMika Westerberg 		mmio.start += mmio.end + 1;
195908f0a15eSMika Westerberg 		mmio_pref.start += mmio_pref.end + 1;
19601a576772SMika Westerberg 	}
19611a576772SMika Westerberg }
19621a576772SMika Westerberg 
19630d607618SNicholas Johnson static void pci_bridge_distribute_available_resources(struct pci_dev *bridge,
19641a576772SMika Westerberg 						      struct list_head *add_list)
19651a576772SMika Westerberg {
1966d555a50fSNicholas Johnson 	struct resource available_io, available_mmio, available_mmio_pref;
19671a576772SMika Westerberg 
19681a576772SMika Westerberg 	if (!bridge->is_hotplug_bridge)
19691a576772SMika Westerberg 		return;
19701a576772SMika Westerberg 
19717180c1d0SMika Westerberg 	pci_dbg(bridge, "distributing available resources\n");
19727180c1d0SMika Westerberg 
19731a576772SMika Westerberg 	/* Take the initial extra resources from the hotplug port */
19746e0688dbSKrzysztof Wilczynski 	available_io = bridge->resource[PCI_BRIDGE_IO_WINDOW];
19756e0688dbSKrzysztof Wilczynski 	available_mmio = bridge->resource[PCI_BRIDGE_MEM_WINDOW];
19766e0688dbSKrzysztof Wilczynski 	available_mmio_pref = bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
19771a576772SMika Westerberg 
19781a576772SMika Westerberg 	pci_bus_distribute_available_resources(bridge->subordinate,
19790d607618SNicholas Johnson 					       add_list, available_io,
19800d607618SNicholas Johnson 					       available_mmio,
19810d607618SNicholas Johnson 					       available_mmio_pref);
19821a576772SMika Westerberg }
19831a576772SMika Westerberg 
19847180c1d0SMika Westerberg static bool pci_bridge_resources_not_assigned(struct pci_dev *dev)
19857180c1d0SMika Westerberg {
19867180c1d0SMika Westerberg 	const struct resource *r;
19877180c1d0SMika Westerberg 
19887180c1d0SMika Westerberg 	/*
19897180c1d0SMika Westerberg 	 * If the child device's resources are not yet assigned it means we
19907180c1d0SMika Westerberg 	 * are configuring them (not the boot firmware), so we should be
19917180c1d0SMika Westerberg 	 * able to extend the upstream bridge resources in the same way we
19927180c1d0SMika Westerberg 	 * do with the normal hotplug case.
19937180c1d0SMika Westerberg 	 */
19947180c1d0SMika Westerberg 	r = &dev->resource[PCI_BRIDGE_IO_WINDOW];
19957180c1d0SMika Westerberg 	if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
19967180c1d0SMika Westerberg 		return false;
19977180c1d0SMika Westerberg 	r = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
19987180c1d0SMika Westerberg 	if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
19997180c1d0SMika Westerberg 		return false;
20007180c1d0SMika Westerberg 	r = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
20017180c1d0SMika Westerberg 	if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
20027180c1d0SMika Westerberg 		return false;
20037180c1d0SMika Westerberg 
20047180c1d0SMika Westerberg 	return true;
20057180c1d0SMika Westerberg }
20067180c1d0SMika Westerberg 
20077180c1d0SMika Westerberg static void
20087180c1d0SMika Westerberg pci_root_bus_distribute_available_resources(struct pci_bus *bus,
20097180c1d0SMika Westerberg 					    struct list_head *add_list)
20107180c1d0SMika Westerberg {
20117180c1d0SMika Westerberg 	struct pci_dev *dev, *bridge = bus->self;
20127180c1d0SMika Westerberg 
20137180c1d0SMika Westerberg 	for_each_pci_bridge(dev, bus) {
20147180c1d0SMika Westerberg 		struct pci_bus *b;
20157180c1d0SMika Westerberg 
20167180c1d0SMika Westerberg 		b = dev->subordinate;
20177180c1d0SMika Westerberg 		if (!b)
20187180c1d0SMika Westerberg 			continue;
20197180c1d0SMika Westerberg 
20207180c1d0SMika Westerberg 		/*
20217180c1d0SMika Westerberg 		 * Need to check "bridge" here too because it is NULL
20227180c1d0SMika Westerberg 		 * in case of root bus.
20237180c1d0SMika Westerberg 		 */
20247180c1d0SMika Westerberg 		if (bridge && pci_bridge_resources_not_assigned(dev))
20257180c1d0SMika Westerberg 			pci_bridge_distribute_available_resources(bridge,
20267180c1d0SMika Westerberg 								  add_list);
20277180c1d0SMika Westerberg 		else
20287180c1d0SMika Westerberg 			pci_root_bus_distribute_available_resources(b, add_list);
20297180c1d0SMika Westerberg 	}
20307180c1d0SMika Westerberg }
20317180c1d0SMika Westerberg 
2032d1caf229SMika Westerberg /*
2033d1caf229SMika Westerberg  * First try will not touch PCI bridge res.
2034d1caf229SMika Westerberg  * Second and later try will clear small leaf bridge res.
2035d1caf229SMika Westerberg  * Will stop till to the max depth if can not find good one.
2036d1caf229SMika Westerberg  */
2037d1caf229SMika Westerberg void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
2038d1caf229SMika Westerberg {
2039d1caf229SMika Westerberg 	LIST_HEAD(realloc_head);
2040d1caf229SMika Westerberg 	/* List of resources that want additional resources */
2041d1caf229SMika Westerberg 	struct list_head *add_list = NULL;
2042d1caf229SMika Westerberg 	int tried_times = 0;
2043d1caf229SMika Westerberg 	enum release_type rel_type = leaf_only;
2044d1caf229SMika Westerberg 	LIST_HEAD(fail_head);
2045d1caf229SMika Westerberg 	struct pci_dev_resource *fail_res;
2046d1caf229SMika Westerberg 	int pci_try_num = 1;
2047d1caf229SMika Westerberg 	enum enable_type enable_local;
2048d1caf229SMika Westerberg 
2049d1caf229SMika Westerberg 	/* Don't realloc if asked to do so */
2050d1caf229SMika Westerberg 	enable_local = pci_realloc_detect(bus, pci_realloc_enable);
2051d1caf229SMika Westerberg 	if (pci_realloc_enabled(enable_local)) {
2052d1caf229SMika Westerberg 		int max_depth = pci_bus_get_depth(bus);
2053d1caf229SMika Westerberg 
2054d1caf229SMika Westerberg 		pci_try_num = max_depth + 1;
2055d1caf229SMika Westerberg 		dev_info(&bus->dev, "max bus depth: %d pci_try_num: %d\n",
2056d1caf229SMika Westerberg 			 max_depth, pci_try_num);
2057d1caf229SMika Westerberg 	}
2058d1caf229SMika Westerberg 
2059d1caf229SMika Westerberg again:
2060d1caf229SMika Westerberg 	/*
2061d1caf229SMika Westerberg 	 * Last try will use add_list, otherwise will try good to have as must
2062d1caf229SMika Westerberg 	 * have, so can realloc parent bridge resource
2063d1caf229SMika Westerberg 	 */
2064d1caf229SMika Westerberg 	if (tried_times + 1 == pci_try_num)
2065d1caf229SMika Westerberg 		add_list = &realloc_head;
2066d1caf229SMika Westerberg 	/*
2067d1caf229SMika Westerberg 	 * Depth first, calculate sizes and alignments of all subordinate buses.
2068d1caf229SMika Westerberg 	 */
2069d1caf229SMika Westerberg 	__pci_bus_size_bridges(bus, add_list);
2070d1caf229SMika Westerberg 
20717180c1d0SMika Westerberg 	pci_root_bus_distribute_available_resources(bus, add_list);
20727180c1d0SMika Westerberg 
2073d1caf229SMika Westerberg 	/* Depth last, allocate resources and update the hardware. */
2074d1caf229SMika Westerberg 	__pci_bus_assign_resources(bus, add_list, &fail_head);
2075d1caf229SMika Westerberg 	if (add_list)
2076d1caf229SMika Westerberg 		BUG_ON(!list_empty(add_list));
2077d1caf229SMika Westerberg 	tried_times++;
2078d1caf229SMika Westerberg 
2079d1caf229SMika Westerberg 	/* Any device complain? */
2080d1caf229SMika Westerberg 	if (list_empty(&fail_head))
2081d1caf229SMika Westerberg 		goto dump;
2082d1caf229SMika Westerberg 
2083d1caf229SMika Westerberg 	if (tried_times >= pci_try_num) {
2084d1caf229SMika Westerberg 		if (enable_local == undefined)
2085d1caf229SMika Westerberg 			dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
2086d1caf229SMika Westerberg 		else if (enable_local == auto_enabled)
2087d1caf229SMika Westerberg 			dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
2088d1caf229SMika Westerberg 
2089d1caf229SMika Westerberg 		free_list(&fail_head);
2090d1caf229SMika Westerberg 		goto dump;
2091d1caf229SMika Westerberg 	}
2092d1caf229SMika Westerberg 
2093d1caf229SMika Westerberg 	dev_info(&bus->dev, "No. %d try to assign unassigned res\n",
2094d1caf229SMika Westerberg 		 tried_times + 1);
2095d1caf229SMika Westerberg 
2096d1caf229SMika Westerberg 	/* Third times and later will not check if it is leaf */
2097d1caf229SMika Westerberg 	if ((tried_times + 1) > 2)
2098d1caf229SMika Westerberg 		rel_type = whole_subtree;
2099d1caf229SMika Westerberg 
2100d1caf229SMika Westerberg 	/*
2101d1caf229SMika Westerberg 	 * Try to release leaf bridge's resources that doesn't fit resource of
2102d1caf229SMika Westerberg 	 * child device under that bridge.
2103d1caf229SMika Westerberg 	 */
2104d1caf229SMika Westerberg 	list_for_each_entry(fail_res, &fail_head, list)
2105d1caf229SMika Westerberg 		pci_bus_release_bridge_resources(fail_res->dev->bus,
2106d1caf229SMika Westerberg 						 fail_res->flags & PCI_RES_TYPE_MASK,
2107d1caf229SMika Westerberg 						 rel_type);
2108d1caf229SMika Westerberg 
2109d1caf229SMika Westerberg 	/* Restore size and flags */
2110d1caf229SMika Westerberg 	list_for_each_entry(fail_res, &fail_head, list) {
2111d1caf229SMika Westerberg 		struct resource *res = fail_res->res;
2112d1caf229SMika Westerberg 		int idx;
2113d1caf229SMika Westerberg 
2114d1caf229SMika Westerberg 		res->start = fail_res->start;
2115d1caf229SMika Westerberg 		res->end = fail_res->end;
2116d1caf229SMika Westerberg 		res->flags = fail_res->flags;
2117d1caf229SMika Westerberg 
2118d1caf229SMika Westerberg 		if (pci_is_bridge(fail_res->dev)) {
2119d1caf229SMika Westerberg 			idx = res - &fail_res->dev->resource[0];
2120d1caf229SMika Westerberg 			if (idx >= PCI_BRIDGE_RESOURCES &&
2121d1caf229SMika Westerberg 			    idx <= PCI_BRIDGE_RESOURCE_END)
2122d1caf229SMika Westerberg 				res->flags = 0;
2123d1caf229SMika Westerberg 		}
2124d1caf229SMika Westerberg 	}
2125d1caf229SMika Westerberg 	free_list(&fail_head);
2126d1caf229SMika Westerberg 
2127d1caf229SMika Westerberg 	goto again;
2128d1caf229SMika Westerberg 
2129d1caf229SMika Westerberg dump:
2130d1caf229SMika Westerberg 	/* Dump the resource on buses */
2131d1caf229SMika Westerberg 	pci_bus_dump_resources(bus);
2132d1caf229SMika Westerberg }
2133d1caf229SMika Westerberg 
2134d1caf229SMika Westerberg void __init pci_assign_unassigned_resources(void)
2135d1caf229SMika Westerberg {
2136d1caf229SMika Westerberg 	struct pci_bus *root_bus;
2137d1caf229SMika Westerberg 
2138d1caf229SMika Westerberg 	list_for_each_entry(root_bus, &pci_root_buses, node) {
2139d1caf229SMika Westerberg 		pci_assign_unassigned_root_bus_resources(root_bus);
2140d1caf229SMika Westerberg 
2141d1caf229SMika Westerberg 		/* Make sure the root bridge has a companion ACPI device */
2142d1caf229SMika Westerberg 		if (ACPI_HANDLE(root_bus->bridge))
2143d1caf229SMika Westerberg 			acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
2144d1caf229SMika Westerberg 	}
2145d1caf229SMika Westerberg }
2146d1caf229SMika Westerberg 
21476841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
21486841ec68SYinghai Lu {
21496841ec68SYinghai Lu 	struct pci_bus *parent = bridge->subordinate;
21500d607618SNicholas Johnson 	/* List of resources that want additional resources */
21510d607618SNicholas Johnson 	LIST_HEAD(add_list);
21520d607618SNicholas Johnson 
215332180e40SYinghai Lu 	int tried_times = 0;
2154bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
2155b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
21566841ec68SYinghai Lu 	int retval;
21576841ec68SYinghai Lu 
215832180e40SYinghai Lu again:
21598424d759SYinghai Lu 	__pci_bus_size_bridges(parent, &add_list);
21601a576772SMika Westerberg 
21611a576772SMika Westerberg 	/*
21620d607618SNicholas Johnson 	 * Distribute remaining resources (if any) equally between hotplug
21630d607618SNicholas Johnson 	 * bridges below.  This makes it possible to extend the hierarchy
21640d607618SNicholas Johnson 	 * later without running out of resources.
21651a576772SMika Westerberg 	 */
21661a576772SMika Westerberg 	pci_bridge_distribute_available_resources(bridge, &add_list);
21671a576772SMika Westerberg 
2168bdc4abecSYinghai Lu 	__pci_bridge_assign_resources(bridge, &add_list, &fail_head);
2169bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
217032180e40SYinghai Lu 	tried_times++;
217132180e40SYinghai Lu 
2172bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
21733f579c34SYinghai Lu 		goto enable_all;
217432180e40SYinghai Lu 
217532180e40SYinghai Lu 	if (tried_times >= 2) {
21760d607618SNicholas Johnson 		/* Still fail, don't need to try more */
2177bffc56d4SYinghai Lu 		free_list(&fail_head);
21783f579c34SYinghai Lu 		goto enable_all;
217932180e40SYinghai Lu 	}
218032180e40SYinghai Lu 
218132180e40SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
218232180e40SYinghai Lu 			 tried_times + 1);
218332180e40SYinghai Lu 
218432180e40SYinghai Lu 	/*
21850d607618SNicholas Johnson 	 * Try to release leaf bridge's resources that aren't big enough
21860d607618SNicholas Johnson 	 * to contain child device resources.
218732180e40SYinghai Lu 	 */
218861e83cddSYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list)
218961e83cddSYinghai Lu 		pci_bus_release_bridge_resources(fail_res->dev->bus,
2190cb21bc94SChristian König 						 fail_res->flags & PCI_RES_TYPE_MASK,
219132180e40SYinghai Lu 						 whole_subtree);
219261e83cddSYinghai Lu 
21930d607618SNicholas Johnson 	/* Restore size and flags */
2194b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
2195b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
21969db8dc6dSLogan Gunthorpe 		int idx;
219732180e40SYinghai Lu 
2198b9b0bba9SYinghai Lu 		res->start = fail_res->start;
2199b9b0bba9SYinghai Lu 		res->end = fail_res->end;
2200b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
22019db8dc6dSLogan Gunthorpe 
22029db8dc6dSLogan Gunthorpe 		if (pci_is_bridge(fail_res->dev)) {
22039db8dc6dSLogan Gunthorpe 			idx = res - &fail_res->dev->resource[0];
22049db8dc6dSLogan Gunthorpe 			if (idx >= PCI_BRIDGE_RESOURCES &&
22059db8dc6dSLogan Gunthorpe 			    idx <= PCI_BRIDGE_RESOURCE_END)
220632180e40SYinghai Lu 				res->flags = 0;
220732180e40SYinghai Lu 		}
22089db8dc6dSLogan Gunthorpe 	}
2209bffc56d4SYinghai Lu 	free_list(&fail_head);
221032180e40SYinghai Lu 
221132180e40SYinghai Lu 	goto again;
22123f579c34SYinghai Lu 
22133f579c34SYinghai Lu enable_all:
22143f579c34SYinghai Lu 	retval = pci_reenable_device(bridge);
22159fc9eea0SBjorn Helgaas 	if (retval)
22167506dc79SFrederick Lawler 		pci_err(bridge, "Error reenabling bridge (%d)\n", retval);
22173f579c34SYinghai Lu 	pci_set_master(bridge);
22186841ec68SYinghai Lu }
22196841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
22209b03088fSYinghai Lu 
22218bb705e3SChristian König int pci_reassign_bridge_resources(struct pci_dev *bridge, unsigned long type)
22228bb705e3SChristian König {
22238bb705e3SChristian König 	struct pci_dev_resource *dev_res;
22248bb705e3SChristian König 	struct pci_dev *next;
22258bb705e3SChristian König 	LIST_HEAD(saved);
22268bb705e3SChristian König 	LIST_HEAD(added);
22278bb705e3SChristian König 	LIST_HEAD(failed);
22288bb705e3SChristian König 	unsigned int i;
22298bb705e3SChristian König 	int ret;
22308bb705e3SChristian König 
2231fb794a70SBenjamin Herrenschmidt 	down_read(&pci_bus_sem);
2232fb794a70SBenjamin Herrenschmidt 
22338bb705e3SChristian König 	/* Walk to the root hub, releasing bridge BARs when possible */
22348bb705e3SChristian König 	next = bridge;
22358bb705e3SChristian König 	do {
22368bb705e3SChristian König 		bridge = next;
22378bb705e3SChristian König 		for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCE_END;
22388bb705e3SChristian König 		     i++) {
22398bb705e3SChristian König 			struct resource *res = &bridge->resource[i];
22408bb705e3SChristian König 
22418bb705e3SChristian König 			if ((res->flags ^ type) & PCI_RES_TYPE_MASK)
22428bb705e3SChristian König 				continue;
22438bb705e3SChristian König 
22448bb705e3SChristian König 			/* Ignore BARs which are still in use */
22458bb705e3SChristian König 			if (res->child)
22468bb705e3SChristian König 				continue;
22478bb705e3SChristian König 
22488bb705e3SChristian König 			ret = add_to_list(&saved, bridge, res, 0, 0);
22498bb705e3SChristian König 			if (ret)
22508bb705e3SChristian König 				goto cleanup;
22518bb705e3SChristian König 
22527506dc79SFrederick Lawler 			pci_info(bridge, "BAR %d: releasing %pR\n",
22538bb705e3SChristian König 				 i, res);
22548bb705e3SChristian König 
22558bb705e3SChristian König 			if (res->parent)
22568bb705e3SChristian König 				release_resource(res);
22578bb705e3SChristian König 			res->start = 0;
22588bb705e3SChristian König 			res->end = 0;
22598bb705e3SChristian König 			break;
22608bb705e3SChristian König 		}
22618bb705e3SChristian König 		if (i == PCI_BRIDGE_RESOURCE_END)
22628bb705e3SChristian König 			break;
22638bb705e3SChristian König 
22648bb705e3SChristian König 		next = bridge->bus ? bridge->bus->self : NULL;
22658bb705e3SChristian König 	} while (next);
22668bb705e3SChristian König 
2267fb794a70SBenjamin Herrenschmidt 	if (list_empty(&saved)) {
2268fb794a70SBenjamin Herrenschmidt 		up_read(&pci_bus_sem);
22698bb705e3SChristian König 		return -ENOENT;
2270fb794a70SBenjamin Herrenschmidt 	}
22718bb705e3SChristian König 
22728bb705e3SChristian König 	__pci_bus_size_bridges(bridge->subordinate, &added);
22738bb705e3SChristian König 	__pci_bridge_assign_resources(bridge, &added, &failed);
22748bb705e3SChristian König 	BUG_ON(!list_empty(&added));
22758bb705e3SChristian König 
22768bb705e3SChristian König 	if (!list_empty(&failed)) {
22778bb705e3SChristian König 		ret = -ENOSPC;
22788bb705e3SChristian König 		goto cleanup;
22798bb705e3SChristian König 	}
22808bb705e3SChristian König 
22818bb705e3SChristian König 	list_for_each_entry(dev_res, &saved, list) {
22820d607618SNicholas Johnson 		/* Skip the bridge we just assigned resources for */
22838bb705e3SChristian König 		if (bridge == dev_res->dev)
22848bb705e3SChristian König 			continue;
22858bb705e3SChristian König 
22868bb705e3SChristian König 		bridge = dev_res->dev;
22878bb705e3SChristian König 		pci_setup_bridge(bridge->subordinate);
22888bb705e3SChristian König 	}
22898bb705e3SChristian König 
22908bb705e3SChristian König 	free_list(&saved);
2291fb794a70SBenjamin Herrenschmidt 	up_read(&pci_bus_sem);
22928bb705e3SChristian König 	return 0;
22938bb705e3SChristian König 
22948bb705e3SChristian König cleanup:
22950d607618SNicholas Johnson 	/* Restore size and flags */
22968bb705e3SChristian König 	list_for_each_entry(dev_res, &failed, list) {
22978bb705e3SChristian König 		struct resource *res = dev_res->res;
22988bb705e3SChristian König 
22998bb705e3SChristian König 		res->start = dev_res->start;
23008bb705e3SChristian König 		res->end = dev_res->end;
23018bb705e3SChristian König 		res->flags = dev_res->flags;
23028bb705e3SChristian König 	}
23038bb705e3SChristian König 	free_list(&failed);
23048bb705e3SChristian König 
23058bb705e3SChristian König 	/* Revert to the old configuration */
23068bb705e3SChristian König 	list_for_each_entry(dev_res, &saved, list) {
23078bb705e3SChristian König 		struct resource *res = dev_res->res;
23088bb705e3SChristian König 
23098bb705e3SChristian König 		bridge = dev_res->dev;
23108bb705e3SChristian König 		i = res - bridge->resource;
23118bb705e3SChristian König 
23128bb705e3SChristian König 		res->start = dev_res->start;
23138bb705e3SChristian König 		res->end = dev_res->end;
23148bb705e3SChristian König 		res->flags = dev_res->flags;
23158bb705e3SChristian König 
23168bb705e3SChristian König 		pci_claim_resource(bridge, i);
23178bb705e3SChristian König 		pci_setup_bridge(bridge->subordinate);
23188bb705e3SChristian König 	}
23198bb705e3SChristian König 	free_list(&saved);
2320fb794a70SBenjamin Herrenschmidt 	up_read(&pci_bus_sem);
23218bb705e3SChristian König 
23228bb705e3SChristian König 	return ret;
23238bb705e3SChristian König }
23248bb705e3SChristian König 
232517787940SYinghai Lu void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
23269b03088fSYinghai Lu {
23279b03088fSYinghai Lu 	struct pci_dev *dev;
23280d607618SNicholas Johnson 	/* List of resources that want additional resources */
23290d607618SNicholas Johnson 	LIST_HEAD(add_list);
23309b03088fSYinghai Lu 
23319b03088fSYinghai Lu 	down_read(&pci_bus_sem);
233224a0c654SAndy Shevchenko 	for_each_pci_bridge(dev, bus)
233324a0c654SAndy Shevchenko 		if (pci_has_subordinate(dev))
233424a0c654SAndy Shevchenko 			__pci_bus_size_bridges(dev->subordinate, &add_list);
23359b03088fSYinghai Lu 	up_read(&pci_bus_sem);
23369b03088fSYinghai Lu 	__pci_bus_assign_resources(bus, &add_list, NULL);
2337bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
233817787940SYinghai Lu }
2339e6b29deaSRay Jui EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);
2340