xref: /openbmc/linux/drivers/pci/setup-bus.c (revision 9db8dc6d0785225c42a37be7b44d1b07b31b8957)
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;
2947087700SBjorn Helgaas 
30bdc4abecSYinghai Lu struct pci_dev_resource {
31bdc4abecSYinghai Lu 	struct list_head list;
322934a0deSYinghai Lu 	struct resource *res;
332934a0deSYinghai Lu 	struct pci_dev *dev;
34568ddef8SYinghai Lu 	resource_size_t start;
35568ddef8SYinghai Lu 	resource_size_t end;
36c8adf9a3SRam Pai 	resource_size_t add_size;
372bbc6942SRam Pai 	resource_size_t min_align;
38568ddef8SYinghai Lu 	unsigned long flags;
39568ddef8SYinghai Lu };
40568ddef8SYinghai Lu 
41bffc56d4SYinghai Lu static void free_list(struct list_head *head)
42bffc56d4SYinghai Lu {
43bffc56d4SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
44bffc56d4SYinghai Lu 
45bffc56d4SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
46bffc56d4SYinghai Lu 		list_del(&dev_res->list);
47bffc56d4SYinghai Lu 		kfree(dev_res);
48bffc56d4SYinghai Lu 	}
49bffc56d4SYinghai Lu }
50094732a5SRam Pai 
51c8adf9a3SRam Pai /**
520d607618SNicholas Johnson  * add_to_list() - Add a new resource tracker to the list
53c8adf9a3SRam Pai  * @head:	Head of the list
540d607618SNicholas Johnson  * @dev:	Device to which the resource belongs
550d607618SNicholas Johnson  * @res:	Resource to be tracked
560d607618SNicholas Johnson  * @add_size:	Additional size to be optionally added to the resource
57c8adf9a3SRam Pai  */
580d607618SNicholas Johnson static int add_to_list(struct list_head *head, struct pci_dev *dev,
590d607618SNicholas Johnson 		       struct resource *res, resource_size_t add_size,
600d607618SNicholas Johnson 		       resource_size_t min_align)
61568ddef8SYinghai Lu {
62764242a0SYinghai Lu 	struct pci_dev_resource *tmp;
63568ddef8SYinghai Lu 
64bdc4abecSYinghai Lu 	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
65c7abb235SMarkus Elfring 	if (!tmp)
66ef62dfefSYinghai Lu 		return -ENOMEM;
67568ddef8SYinghai Lu 
68568ddef8SYinghai Lu 	tmp->res = res;
69568ddef8SYinghai Lu 	tmp->dev = dev;
70568ddef8SYinghai Lu 	tmp->start = res->start;
71568ddef8SYinghai Lu 	tmp->end = res->end;
72568ddef8SYinghai Lu 	tmp->flags = res->flags;
73c8adf9a3SRam Pai 	tmp->add_size = add_size;
742bbc6942SRam Pai 	tmp->min_align = min_align;
75bdc4abecSYinghai Lu 
76bdc4abecSYinghai Lu 	list_add(&tmp->list, head);
77ef62dfefSYinghai Lu 
78ef62dfefSYinghai Lu 	return 0;
79568ddef8SYinghai Lu }
80568ddef8SYinghai Lu 
810d607618SNicholas Johnson static void remove_from_list(struct list_head *head, struct resource *res)
823e6e0d80SYinghai Lu {
83b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
843e6e0d80SYinghai Lu 
85b9b0bba9SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
86b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
87b9b0bba9SYinghai Lu 			list_del(&dev_res->list);
88b9b0bba9SYinghai Lu 			kfree(dev_res);
89bdc4abecSYinghai Lu 			break;
903e6e0d80SYinghai Lu 		}
913e6e0d80SYinghai Lu 	}
923e6e0d80SYinghai Lu }
933e6e0d80SYinghai Lu 
94d74b9027SWei Yang static struct pci_dev_resource *res_to_dev_res(struct list_head *head,
951c372353SYinghai Lu 					       struct resource *res)
961c372353SYinghai Lu {
97b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res;
981c372353SYinghai Lu 
99b9b0bba9SYinghai Lu 	list_for_each_entry(dev_res, head, list) {
10025e77388SBjorn Helgaas 		if (dev_res->res == res)
101d74b9027SWei Yang 			return dev_res;
102bdc4abecSYinghai Lu 	}
1031c372353SYinghai Lu 
104d74b9027SWei Yang 	return NULL;
1051c372353SYinghai Lu }
1061c372353SYinghai Lu 
107d74b9027SWei Yang static resource_size_t get_res_add_size(struct list_head *head,
108d74b9027SWei Yang 					struct resource *res)
109d74b9027SWei Yang {
110d74b9027SWei Yang 	struct pci_dev_resource *dev_res;
111d74b9027SWei Yang 
112d74b9027SWei Yang 	dev_res = res_to_dev_res(head, res);
113d74b9027SWei Yang 	return dev_res ? dev_res->add_size : 0;
114d74b9027SWei Yang }
115d74b9027SWei Yang 
116d74b9027SWei Yang static resource_size_t get_res_add_align(struct list_head *head,
117d74b9027SWei Yang 					 struct resource *res)
118d74b9027SWei Yang {
119d74b9027SWei Yang 	struct pci_dev_resource *dev_res;
120d74b9027SWei Yang 
121d74b9027SWei Yang 	dev_res = res_to_dev_res(head, res);
122d74b9027SWei Yang 	return dev_res ? dev_res->min_align : 0;
123d74b9027SWei Yang }
124d74b9027SWei Yang 
125d74b9027SWei Yang 
12678c3b329SYinghai Lu /* Sort resources by alignment */
127bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
12878c3b329SYinghai Lu {
12978c3b329SYinghai Lu 	int i;
13078c3b329SYinghai Lu 
13178c3b329SYinghai Lu 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
13278c3b329SYinghai Lu 		struct resource *r;
133bdc4abecSYinghai Lu 		struct pci_dev_resource *dev_res, *tmp;
13478c3b329SYinghai Lu 		resource_size_t r_align;
135bdc4abecSYinghai Lu 		struct list_head *n;
13678c3b329SYinghai Lu 
13778c3b329SYinghai Lu 		r = &dev->resource[i];
13878c3b329SYinghai Lu 
13978c3b329SYinghai Lu 		if (r->flags & IORESOURCE_PCI_FIXED)
14078c3b329SYinghai Lu 			continue;
14178c3b329SYinghai Lu 
14278c3b329SYinghai Lu 		if (!(r->flags) || r->parent)
14378c3b329SYinghai Lu 			continue;
14478c3b329SYinghai Lu 
14578c3b329SYinghai Lu 		r_align = pci_resource_alignment(dev, r);
14678c3b329SYinghai Lu 		if (!r_align) {
1477506dc79SFrederick Lawler 			pci_warn(dev, "BAR %d: %pR has bogus alignment\n",
14878c3b329SYinghai Lu 				 i, r);
14978c3b329SYinghai Lu 			continue;
15078c3b329SYinghai Lu 		}
15178c3b329SYinghai Lu 
152bdc4abecSYinghai Lu 		tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
15378c3b329SYinghai Lu 		if (!tmp)
154227f0647SRyan Desfosses 			panic("pdev_sort_resources(): kmalloc() failed!\n");
15578c3b329SYinghai Lu 		tmp->res = r;
15678c3b329SYinghai Lu 		tmp->dev = dev;
157bdc4abecSYinghai Lu 
1580d607618SNicholas Johnson 		/* Fallback is smallest one or list is empty */
159bdc4abecSYinghai Lu 		n = head;
160bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
161bdc4abecSYinghai Lu 			resource_size_t align;
162bdc4abecSYinghai Lu 
163bdc4abecSYinghai Lu 			align = pci_resource_alignment(dev_res->dev,
164bdc4abecSYinghai Lu 							 dev_res->res);
165bdc4abecSYinghai Lu 
166bdc4abecSYinghai Lu 			if (r_align > align) {
167bdc4abecSYinghai Lu 				n = &dev_res->list;
16878c3b329SYinghai Lu 				break;
16978c3b329SYinghai Lu 			}
17078c3b329SYinghai Lu 		}
171bdc4abecSYinghai Lu 		/* Insert it just before n */
172bdc4abecSYinghai Lu 		list_add_tail(&tmp->list, n);
17378c3b329SYinghai Lu 	}
17478c3b329SYinghai Lu }
17578c3b329SYinghai Lu 
1760d607618SNicholas Johnson static void __dev_sort_resources(struct pci_dev *dev, struct list_head *head)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	u16 class = dev->class >> 8;
1791da177e4SLinus Torvalds 
1800d607618SNicholas Johnson 	/* Don't touch classless devices or host bridges or IOAPICs */
1816841ec68SYinghai Lu 	if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
1826841ec68SYinghai Lu 		return;
1831da177e4SLinus Torvalds 
1840d607618SNicholas Johnson 	/* Don't touch IOAPIC devices already enabled by firmware */
18523186279SSatoru Takeuchi 	if (class == PCI_CLASS_SYSTEM_PIC) {
1869bded00bSKenji Kaneshige 		u16 command;
1879bded00bSKenji Kaneshige 		pci_read_config_word(dev, PCI_COMMAND, &command);
1889bded00bSKenji Kaneshige 		if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
1896841ec68SYinghai Lu 			return;
19023186279SSatoru Takeuchi 	}
19123186279SSatoru Takeuchi 
1926841ec68SYinghai Lu 	pdev_sort_resources(dev, head);
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
195fc075e1dSRam Pai static inline void reset_resource(struct resource *res)
196fc075e1dSRam Pai {
197fc075e1dSRam Pai 	res->start = 0;
198fc075e1dSRam Pai 	res->end = 0;
199fc075e1dSRam Pai 	res->flags = 0;
200fc075e1dSRam Pai }
201fc075e1dSRam Pai 
202c8adf9a3SRam Pai /**
2030d607618SNicholas Johnson  * reassign_resources_sorted() - Satisfy any additional resource requests
204c8adf9a3SRam Pai  *
2050d607618SNicholas Johnson  * @realloc_head:	Head of the list tracking requests requiring
2060d607618SNicholas Johnson  *			additional resources
2070d607618SNicholas Johnson  * @head:		Head of the list tracking requests with allocated
208c8adf9a3SRam Pai  *			resources
209c8adf9a3SRam Pai  *
2100d607618SNicholas Johnson  * Walk through each element of the realloc_head and try to procure additional
2110d607618SNicholas Johnson  * resources for the element, provided the element is in the head list.
212c8adf9a3SRam Pai  */
213bdc4abecSYinghai Lu static void reassign_resources_sorted(struct list_head *realloc_head,
214bdc4abecSYinghai Lu 				      struct list_head *head)
215c8adf9a3SRam Pai {
216c8adf9a3SRam Pai 	struct resource *res;
217b9b0bba9SYinghai Lu 	struct pci_dev_resource *add_res, *tmp;
218bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
219d74b9027SWei Yang 	resource_size_t add_size, align;
220c8adf9a3SRam Pai 	int idx;
221c8adf9a3SRam Pai 
222b9b0bba9SYinghai Lu 	list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
223bdc4abecSYinghai Lu 		bool found_match = false;
224bdc4abecSYinghai Lu 
225b9b0bba9SYinghai Lu 		res = add_res->res;
2260d607618SNicholas Johnson 		/* Skip resource that has been reset */
227c8adf9a3SRam Pai 		if (!res->flags)
228c8adf9a3SRam Pai 			goto out;
229c8adf9a3SRam Pai 
2300d607618SNicholas Johnson 		/* Skip this resource if not found in head list */
231bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
232bdc4abecSYinghai Lu 			if (dev_res->res == res) {
233bdc4abecSYinghai Lu 				found_match = true;
234bdc4abecSYinghai Lu 				break;
235c8adf9a3SRam Pai 			}
236bdc4abecSYinghai Lu 		}
2370d607618SNicholas Johnson 		if (!found_match) /* Just skip */
238bdc4abecSYinghai Lu 			continue;
239c8adf9a3SRam Pai 
240b9b0bba9SYinghai Lu 		idx = res - &add_res->dev->resource[0];
241b9b0bba9SYinghai Lu 		add_size = add_res->add_size;
242d74b9027SWei Yang 		align = add_res->min_align;
2432bbc6942SRam Pai 		if (!resource_size(res)) {
244d74b9027SWei Yang 			res->start = align;
245c8adf9a3SRam Pai 			res->end = res->start + add_size - 1;
246b9b0bba9SYinghai Lu 			if (pci_assign_resource(add_res->dev, idx))
247c8adf9a3SRam Pai 				reset_resource(res);
2482bbc6942SRam Pai 		} else {
249b9b0bba9SYinghai Lu 			res->flags |= add_res->flags &
250bdc4abecSYinghai Lu 				 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
251b9b0bba9SYinghai Lu 			if (pci_reassign_resource(add_res->dev, idx,
252bdc4abecSYinghai Lu 						  add_size, align))
25334c6b710SMohan Kumar 				pci_info(add_res->dev, "failed to add %llx res[%d]=%pR\n",
25434c6b710SMohan Kumar 					 (unsigned long long) add_size, idx,
25534c6b710SMohan Kumar 					 res);
256c8adf9a3SRam Pai 		}
257c8adf9a3SRam Pai out:
258b9b0bba9SYinghai Lu 		list_del(&add_res->list);
259b9b0bba9SYinghai Lu 		kfree(add_res);
260c8adf9a3SRam Pai 	}
261c8adf9a3SRam Pai }
262c8adf9a3SRam Pai 
263c8adf9a3SRam Pai /**
2640d607618SNicholas Johnson  * assign_requested_resources_sorted() - Satisfy resource requests
265c8adf9a3SRam Pai  *
2660d607618SNicholas Johnson  * @head:	Head of the list tracking requests for resources
2670d607618SNicholas Johnson  * @fail_head:	Head of the list tracking requests that could not be
2680d607618SNicholas Johnson  *		allocated
269c8adf9a3SRam Pai  *
2700d607618SNicholas Johnson  * Satisfy resource requests of each element in the list.  Add requests that
2710d607618SNicholas Johnson  * could not be satisfied to the failed_list.
272c8adf9a3SRam Pai  */
273bdc4abecSYinghai Lu static void assign_requested_resources_sorted(struct list_head *head,
274bdc4abecSYinghai Lu 				 struct list_head *fail_head)
2756841ec68SYinghai Lu {
2766841ec68SYinghai Lu 	struct resource *res;
277bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
2786841ec68SYinghai Lu 	int idx;
2796841ec68SYinghai Lu 
280bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
281bdc4abecSYinghai Lu 		res = dev_res->res;
282bdc4abecSYinghai Lu 		idx = res - &dev_res->dev->resource[0];
283bdc4abecSYinghai Lu 		if (resource_size(res) &&
284bdc4abecSYinghai Lu 		    pci_assign_resource(dev_res->dev, idx)) {
285a3cb999dSYinghai Lu 			if (fail_head) {
2869a928660SYinghai Lu 				/*
2870d607618SNicholas Johnson 				 * If the failed resource is a ROM BAR and
2880d607618SNicholas Johnson 				 * it will be enabled later, don't add it
2890d607618SNicholas Johnson 				 * to the list.
2909a928660SYinghai Lu 				 */
2919a928660SYinghai Lu 				if (!((idx == PCI_ROM_RESOURCE) &&
2929a928660SYinghai Lu 				      (!(res->flags & IORESOURCE_ROM_ENABLE))))
29367cc7e26SYinghai Lu 					add_to_list(fail_head,
29467cc7e26SYinghai Lu 						    dev_res->dev, res,
295f7625980SBjorn Helgaas 						    0 /* don't care */,
296f7625980SBjorn Helgaas 						    0 /* don't care */);
2979a928660SYinghai Lu 			}
298fc075e1dSRam Pai 			reset_resource(res);
299542df5deSRajesh Shah 		}
3001da177e4SLinus Torvalds 	}
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
303aa914f5eSYinghai Lu static unsigned long pci_fail_res_type_mask(struct list_head *fail_head)
304aa914f5eSYinghai Lu {
305aa914f5eSYinghai Lu 	struct pci_dev_resource *fail_res;
306aa914f5eSYinghai Lu 	unsigned long mask = 0;
307aa914f5eSYinghai Lu 
3080d607618SNicholas Johnson 	/* Check failed type */
309aa914f5eSYinghai Lu 	list_for_each_entry(fail_res, fail_head, list)
310aa914f5eSYinghai Lu 		mask |= fail_res->flags;
311aa914f5eSYinghai Lu 
312aa914f5eSYinghai Lu 	/*
3130d607618SNicholas Johnson 	 * One pref failed resource will set IORESOURCE_MEM, as we can
3140d607618SNicholas Johnson 	 * allocate pref in non-pref range.  Will release all assigned
3150d607618SNicholas Johnson 	 * non-pref sibling resources according to that bit.
316aa914f5eSYinghai Lu 	 */
317aa914f5eSYinghai Lu 	return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
318aa914f5eSYinghai Lu }
319aa914f5eSYinghai Lu 
320aa914f5eSYinghai Lu static bool pci_need_to_release(unsigned long mask, struct resource *res)
321aa914f5eSYinghai Lu {
322aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_IO)
323aa914f5eSYinghai Lu 		return !!(mask & IORESOURCE_IO);
324aa914f5eSYinghai Lu 
3250d607618SNicholas Johnson 	/* Check pref at first */
326aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_PREFETCH) {
327aa914f5eSYinghai Lu 		if (mask & IORESOURCE_PREFETCH)
328aa914f5eSYinghai Lu 			return true;
3290d607618SNicholas Johnson 		/* Count pref if its parent is non-pref */
330aa914f5eSYinghai Lu 		else if ((mask & IORESOURCE_MEM) &&
331aa914f5eSYinghai Lu 			 !(res->parent->flags & IORESOURCE_PREFETCH))
332aa914f5eSYinghai Lu 			return true;
333aa914f5eSYinghai Lu 		else
334aa914f5eSYinghai Lu 			return false;
335aa914f5eSYinghai Lu 	}
336aa914f5eSYinghai Lu 
337aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_MEM)
338aa914f5eSYinghai Lu 		return !!(mask & IORESOURCE_MEM);
339aa914f5eSYinghai Lu 
3400d607618SNicholas Johnson 	return false;	/* Should not get here */
341aa914f5eSYinghai Lu }
342aa914f5eSYinghai Lu 
343bdc4abecSYinghai Lu static void __assign_resources_sorted(struct list_head *head,
344bdc4abecSYinghai Lu 				      struct list_head *realloc_head,
345bdc4abecSYinghai Lu 				      struct list_head *fail_head)
346c8adf9a3SRam Pai {
3473e6e0d80SYinghai Lu 	/*
3480d607618SNicholas Johnson 	 * Should not assign requested resources at first.  They could be
3490d607618SNicholas Johnson 	 * adjacent, so later reassign can not reallocate them one by one in
3500d607618SNicholas Johnson 	 * parent resource window.
3510d607618SNicholas Johnson 	 *
3520d607618SNicholas Johnson 	 * Try to assign requested + add_size at beginning.  If could do that,
3530d607618SNicholas Johnson 	 * could get out early.  If could not do that, we still try to assign
3540d607618SNicholas Johnson 	 * requested at first, then try to reassign add_size for some resources.
355aa914f5eSYinghai Lu 	 *
356aa914f5eSYinghai Lu 	 * Separate three resource type checking if we need to release
357aa914f5eSYinghai Lu 	 * assigned resource after requested + add_size try.
3580d607618SNicholas Johnson 	 *
3590d607618SNicholas Johnson 	 *	1. If IO port assignment fails, will release assigned IO
3600d607618SNicholas Johnson 	 *	   port.
3610d607618SNicholas Johnson 	 *	2. If pref MMIO assignment fails, release assigned pref
3620d607618SNicholas Johnson 	 *	   MMIO.  If assigned pref MMIO's parent is non-pref MMIO
3630d607618SNicholas Johnson 	 *	   and non-pref MMIO assignment fails, will release that
3640d607618SNicholas Johnson 	 *	   assigned pref MMIO.
3650d607618SNicholas Johnson 	 *	3. If non-pref MMIO assignment fails or pref MMIO
3660d607618SNicholas Johnson 	 *	   assignment fails, will release assigned non-pref MMIO.
3673e6e0d80SYinghai Lu 	 */
368bdc4abecSYinghai Lu 	LIST_HEAD(save_head);
369bdc4abecSYinghai Lu 	LIST_HEAD(local_fail_head);
370b9b0bba9SYinghai Lu 	struct pci_dev_resource *save_res;
371d74b9027SWei Yang 	struct pci_dev_resource *dev_res, *tmp_res, *dev_res2;
372aa914f5eSYinghai Lu 	unsigned long fail_type;
373d74b9027SWei Yang 	resource_size_t add_align, align;
3743e6e0d80SYinghai Lu 
3753e6e0d80SYinghai Lu 	/* Check if optional add_size is there */
376bdc4abecSYinghai Lu 	if (!realloc_head || list_empty(realloc_head))
3773e6e0d80SYinghai Lu 		goto requested_and_reassign;
3783e6e0d80SYinghai Lu 
3793e6e0d80SYinghai Lu 	/* Save original start, end, flags etc at first */
380bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
381bdc4abecSYinghai Lu 		if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
382bffc56d4SYinghai Lu 			free_list(&save_head);
3833e6e0d80SYinghai Lu 			goto requested_and_reassign;
3843e6e0d80SYinghai Lu 		}
385bdc4abecSYinghai Lu 	}
3863e6e0d80SYinghai Lu 
3873e6e0d80SYinghai Lu 	/* Update res in head list with add_size in realloc_head list */
388d74b9027SWei Yang 	list_for_each_entry_safe(dev_res, tmp_res, head, list) {
389bdc4abecSYinghai Lu 		dev_res->res->end += get_res_add_size(realloc_head,
390bdc4abecSYinghai Lu 							dev_res->res);
3913e6e0d80SYinghai Lu 
392d74b9027SWei Yang 		/*
393d74b9027SWei Yang 		 * There are two kinds of additional resources in the list:
394d74b9027SWei Yang 		 * 1. bridge resource  -- IORESOURCE_STARTALIGN
395d74b9027SWei Yang 		 * 2. SR-IOV resource  -- IORESOURCE_SIZEALIGN
396d74b9027SWei Yang 		 * Here just fix the additional alignment for bridge
397d74b9027SWei Yang 		 */
398d74b9027SWei Yang 		if (!(dev_res->res->flags & IORESOURCE_STARTALIGN))
399d74b9027SWei Yang 			continue;
400d74b9027SWei Yang 
401d74b9027SWei Yang 		add_align = get_res_add_align(realloc_head, dev_res->res);
402d74b9027SWei Yang 
403d74b9027SWei Yang 		/*
4040d607618SNicholas Johnson 		 * The "head" list is sorted by alignment so resources with
4050d607618SNicholas Johnson 		 * bigger alignment will be assigned first.  After we
4060d607618SNicholas Johnson 		 * change the alignment of a dev_res in "head" list, we
4070d607618SNicholas Johnson 		 * need to reorder the list by alignment to make it
408d74b9027SWei Yang 		 * consistent.
409d74b9027SWei Yang 		 */
410d74b9027SWei Yang 		if (add_align > dev_res->res->start) {
411552bc94eSYinghai Lu 			resource_size_t r_size = resource_size(dev_res->res);
412552bc94eSYinghai Lu 
413d74b9027SWei Yang 			dev_res->res->start = add_align;
414552bc94eSYinghai Lu 			dev_res->res->end = add_align + r_size - 1;
415d74b9027SWei Yang 
416d74b9027SWei Yang 			list_for_each_entry(dev_res2, head, list) {
417d74b9027SWei Yang 				align = pci_resource_alignment(dev_res2->dev,
418d74b9027SWei Yang 							       dev_res2->res);
419a6b65983SWei Yang 				if (add_align > align) {
420d74b9027SWei Yang 					list_move_tail(&dev_res->list,
421d74b9027SWei Yang 						       &dev_res2->list);
422a6b65983SWei Yang 					break;
423a6b65983SWei Yang 				}
424d74b9027SWei Yang 			}
425d74b9027SWei Yang 		}
426d74b9027SWei Yang 
427d74b9027SWei Yang 	}
428d74b9027SWei Yang 
4293e6e0d80SYinghai Lu 	/* Try updated head list with add_size added */
4303e6e0d80SYinghai Lu 	assign_requested_resources_sorted(head, &local_fail_head);
4313e6e0d80SYinghai Lu 
4320d607618SNicholas Johnson 	/* All assigned with add_size? */
433bdc4abecSYinghai Lu 	if (list_empty(&local_fail_head)) {
4343e6e0d80SYinghai Lu 		/* Remove head list from realloc_head list */
435bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list)
436bdc4abecSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
437bffc56d4SYinghai Lu 		free_list(&save_head);
438bffc56d4SYinghai Lu 		free_list(head);
4393e6e0d80SYinghai Lu 		return;
4403e6e0d80SYinghai Lu 	}
4413e6e0d80SYinghai Lu 
4420d607618SNicholas Johnson 	/* Check failed type */
443aa914f5eSYinghai Lu 	fail_type = pci_fail_res_type_mask(&local_fail_head);
4440d607618SNicholas Johnson 	/* Remove not need to be released assigned res from head list etc */
445aa914f5eSYinghai Lu 	list_for_each_entry_safe(dev_res, tmp_res, head, list)
446aa914f5eSYinghai Lu 		if (dev_res->res->parent &&
447aa914f5eSYinghai Lu 		    !pci_need_to_release(fail_type, dev_res->res)) {
4480d607618SNicholas Johnson 			/* Remove it from realloc_head list */
449aa914f5eSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
450aa914f5eSYinghai Lu 			remove_from_list(&save_head, dev_res->res);
451aa914f5eSYinghai Lu 			list_del(&dev_res->list);
452aa914f5eSYinghai Lu 			kfree(dev_res);
453aa914f5eSYinghai Lu 		}
454aa914f5eSYinghai Lu 
455bffc56d4SYinghai Lu 	free_list(&local_fail_head);
4563e6e0d80SYinghai Lu 	/* Release assigned resource */
457bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list)
458bdc4abecSYinghai Lu 		if (dev_res->res->parent)
459bdc4abecSYinghai Lu 			release_resource(dev_res->res);
4603e6e0d80SYinghai Lu 	/* Restore start/end/flags from saved list */
461b9b0bba9SYinghai Lu 	list_for_each_entry(save_res, &save_head, list) {
462b9b0bba9SYinghai Lu 		struct resource *res = save_res->res;
4633e6e0d80SYinghai Lu 
464b9b0bba9SYinghai Lu 		res->start = save_res->start;
465b9b0bba9SYinghai Lu 		res->end = save_res->end;
466b9b0bba9SYinghai Lu 		res->flags = save_res->flags;
4673e6e0d80SYinghai Lu 	}
468bffc56d4SYinghai Lu 	free_list(&save_head);
4693e6e0d80SYinghai Lu 
4703e6e0d80SYinghai Lu requested_and_reassign:
471c8adf9a3SRam Pai 	/* Satisfy the must-have resource requests */
472c8adf9a3SRam Pai 	assign_requested_resources_sorted(head, fail_head);
473c8adf9a3SRam Pai 
4740d607618SNicholas Johnson 	/* Try to satisfy any additional optional resource requests */
4759e8bf93aSRam Pai 	if (realloc_head)
4769e8bf93aSRam Pai 		reassign_resources_sorted(realloc_head, head);
477bffc56d4SYinghai Lu 	free_list(head);
478c8adf9a3SRam Pai }
479c8adf9a3SRam Pai 
4806841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev,
481bdc4abecSYinghai Lu 					 struct list_head *add_head,
482bdc4abecSYinghai Lu 					 struct list_head *fail_head)
4836841ec68SYinghai Lu {
484bdc4abecSYinghai Lu 	LIST_HEAD(head);
4856841ec68SYinghai Lu 
4866841ec68SYinghai Lu 	__dev_sort_resources(dev, &head);
4878424d759SYinghai Lu 	__assign_resources_sorted(&head, add_head, fail_head);
4886841ec68SYinghai Lu 
4896841ec68SYinghai Lu }
4906841ec68SYinghai Lu 
4916841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus,
492bdc4abecSYinghai Lu 					 struct list_head *realloc_head,
493bdc4abecSYinghai Lu 					 struct list_head *fail_head)
4946841ec68SYinghai Lu {
4956841ec68SYinghai Lu 	struct pci_dev *dev;
496bdc4abecSYinghai Lu 	LIST_HEAD(head);
4976841ec68SYinghai Lu 
4986841ec68SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
4996841ec68SYinghai Lu 		__dev_sort_resources(dev, &head);
5006841ec68SYinghai Lu 
5019e8bf93aSRam Pai 	__assign_resources_sorted(&head, realloc_head, fail_head);
5026841ec68SYinghai Lu }
5036841ec68SYinghai Lu 
504b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus)
5051da177e4SLinus Torvalds {
5061da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
507c7dabef8SBjorn Helgaas 	struct resource *res;
5081da177e4SLinus Torvalds 	struct pci_bus_region region;
5091da177e4SLinus Torvalds 
5107506dc79SFrederick Lawler 	pci_info(bridge, "CardBus bridge to %pR\n",
511b918c62eSYinghai Lu 		 &bus->busn_res);
5121da177e4SLinus Torvalds 
513c7dabef8SBjorn Helgaas 	res = bus->resource[0];
514fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
515c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5161da177e4SLinus Torvalds 		/*
5171da177e4SLinus Torvalds 		 * The IO resource is allocated a range twice as large as it
5181da177e4SLinus Torvalds 		 * would normally need.  This allows us to set both IO regs.
5191da177e4SLinus Torvalds 		 */
5207506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5211da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
5221da177e4SLinus Torvalds 					region.start);
5231da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
5241da177e4SLinus Torvalds 					region.end);
5251da177e4SLinus Torvalds 	}
5261da177e4SLinus Torvalds 
527c7dabef8SBjorn Helgaas 	res = bus->resource[1];
528fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
529c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5307506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5311da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
5321da177e4SLinus Torvalds 					region.start);
5331da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
5341da177e4SLinus Torvalds 					region.end);
5351da177e4SLinus Torvalds 	}
5361da177e4SLinus Torvalds 
537c7dabef8SBjorn Helgaas 	res = bus->resource[2];
538fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
539c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
5407506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5411da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
5421da177e4SLinus Torvalds 					region.start);
5431da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
5441da177e4SLinus Torvalds 					region.end);
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 
547c7dabef8SBjorn Helgaas 	res = bus->resource[3];
548fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
549c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
5507506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5511da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
5521da177e4SLinus Torvalds 					region.start);
5531da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
5541da177e4SLinus Torvalds 					region.end);
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds }
557b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus);
5581da177e4SLinus Torvalds 
5590d607618SNicholas Johnson /*
5600d607618SNicholas Johnson  * Initialize bridges with base/limit values we have collected.  PCI-to-PCI
5610d607618SNicholas Johnson  * Bridge Architecture Specification rev. 1.1 (1998) requires that if there
5620d607618SNicholas Johnson  * are no I/O ports or memory behind the bridge, the corresponding range
5630d607618SNicholas Johnson  * must be turned off by writing base value greater than limit to the
5640d607618SNicholas Johnson  * bridge's base/limit registers.
5650d607618SNicholas Johnson  *
5660d607618SNicholas Johnson  * Note: care must be taken when updating I/O base/limit registers of
5670d607618SNicholas Johnson  * bridges which support 32-bit I/O.  This update requires two config space
5680d607618SNicholas Johnson  * writes, so it's quite possible that an I/O window of the bridge will
5690d607618SNicholas Johnson  * have some undesirable address (e.g. 0) after the first write.  Ditto
5700d607618SNicholas Johnson  * 64-bit prefetchable MMIO.
5710d607618SNicholas Johnson  */
5723f2f4dc4SYinghai Lu static void pci_setup_bridge_io(struct pci_dev *bridge)
5731da177e4SLinus Torvalds {
574c7dabef8SBjorn Helgaas 	struct resource *res;
5751da177e4SLinus Torvalds 	struct pci_bus_region region;
5762b28ae19SBjorn Helgaas 	unsigned long io_mask;
5772b28ae19SBjorn Helgaas 	u8 io_base_lo, io_limit_lo;
5785b764b83SBjorn Helgaas 	u16 l;
5795b764b83SBjorn Helgaas 	u32 io_upper16;
5801da177e4SLinus Torvalds 
5812b28ae19SBjorn Helgaas 	io_mask = PCI_IO_RANGE_MASK;
5822b28ae19SBjorn Helgaas 	if (bridge->io_window_1k)
5832b28ae19SBjorn Helgaas 		io_mask = PCI_IO_1K_RANGE_MASK;
5842b28ae19SBjorn Helgaas 
5850d607618SNicholas Johnson 	/* Set up the top and bottom of the PCI I/O segment for this bus */
5863f2f4dc4SYinghai Lu 	res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
587fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
588c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5895b764b83SBjorn Helgaas 		pci_read_config_word(bridge, PCI_IO_BASE, &l);
5902b28ae19SBjorn Helgaas 		io_base_lo = (region.start >> 8) & io_mask;
5912b28ae19SBjorn Helgaas 		io_limit_lo = (region.end >> 8) & io_mask;
5925b764b83SBjorn Helgaas 		l = ((u16) io_limit_lo << 8) | io_base_lo;
5930d607618SNicholas Johnson 		/* Set up upper 16 bits of I/O base/limit */
5941da177e4SLinus Torvalds 		io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
5957506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5967cc5997dSYinghai Lu 	} else {
5970d607618SNicholas Johnson 		/* Clear upper 16 bits of I/O base/limit */
5981da177e4SLinus Torvalds 		io_upper16 = 0;
5991da177e4SLinus Torvalds 		l = 0x00f0;
6001da177e4SLinus Torvalds 	}
6010d607618SNicholas Johnson 	/* Temporarily disable the I/O range before updating PCI_IO_BASE */
6021da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
6030d607618SNicholas Johnson 	/* Update lower 16 bits of I/O base/limit */
6045b764b83SBjorn Helgaas 	pci_write_config_word(bridge, PCI_IO_BASE, l);
6050d607618SNicholas Johnson 	/* Update upper 16 bits of I/O base/limit */
6061da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
6077cc5997dSYinghai Lu }
6081da177e4SLinus Torvalds 
6093f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio(struct pci_dev *bridge)
6107cc5997dSYinghai Lu {
6117cc5997dSYinghai Lu 	struct resource *res;
6127cc5997dSYinghai Lu 	struct pci_bus_region region;
6137cc5997dSYinghai Lu 	u32 l;
6147cc5997dSYinghai Lu 
6150d607618SNicholas Johnson 	/* Set up the top and bottom of the PCI Memory segment for this bus */
6163f2f4dc4SYinghai Lu 	res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
617fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
618c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
6191da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
6201da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
6217506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
6227cc5997dSYinghai Lu 	} else {
6231da177e4SLinus Torvalds 		l = 0x0000fff0;
6241da177e4SLinus Torvalds 	}
6251da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
6267cc5997dSYinghai Lu }
6277cc5997dSYinghai Lu 
6283f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge)
6297cc5997dSYinghai Lu {
6307cc5997dSYinghai Lu 	struct resource *res;
6317cc5997dSYinghai Lu 	struct pci_bus_region region;
6327cc5997dSYinghai Lu 	u32 l, bu, lu;
6331da177e4SLinus Torvalds 
6340d607618SNicholas Johnson 	/*
6350d607618SNicholas Johnson 	 * Clear out the upper 32 bits of PREF limit.  If
6360d607618SNicholas Johnson 	 * PCI_PREF_BASE_UPPER32 was non-zero, this temporarily disables
6370d607618SNicholas Johnson 	 * PREF range, which is ok.
6380d607618SNicholas Johnson 	 */
6391da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
6401da177e4SLinus Torvalds 
6410d607618SNicholas Johnson 	/* Set up PREF base/limit */
642c40a22e0SBenjamin Herrenschmidt 	bu = lu = 0;
6433f2f4dc4SYinghai Lu 	res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
644fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
645c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_PREFETCH) {
6461da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
6471da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
648c7dabef8SBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64) {
64913d36c24SAndrew Morton 			bu = upper_32_bits(region.start);
65013d36c24SAndrew Morton 			lu = upper_32_bits(region.end);
6511f82de10SYinghai Lu 		}
6527506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
6537cc5997dSYinghai Lu 	} else {
6541da177e4SLinus Torvalds 		l = 0x0000fff0;
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
6571da177e4SLinus Torvalds 
6580d607618SNicholas Johnson 	/* Set the upper 32 bits of PREF base & limit */
659c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
660c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
6617cc5997dSYinghai Lu }
6627cc5997dSYinghai Lu 
6637cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
6647cc5997dSYinghai Lu {
6657cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
6667cc5997dSYinghai Lu 
6677506dc79SFrederick Lawler 	pci_info(bridge, "PCI bridge to %pR\n",
668b918c62eSYinghai Lu 		 &bus->busn_res);
6697cc5997dSYinghai Lu 
6707cc5997dSYinghai Lu 	if (type & IORESOURCE_IO)
6713f2f4dc4SYinghai Lu 		pci_setup_bridge_io(bridge);
6727cc5997dSYinghai Lu 
6737cc5997dSYinghai Lu 	if (type & IORESOURCE_MEM)
6743f2f4dc4SYinghai Lu 		pci_setup_bridge_mmio(bridge);
6757cc5997dSYinghai Lu 
6767cc5997dSYinghai Lu 	if (type & IORESOURCE_PREFETCH)
6773f2f4dc4SYinghai Lu 		pci_setup_bridge_mmio_pref(bridge);
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
682d366d28cSGavin Shan void __weak pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
683d366d28cSGavin Shan {
684d366d28cSGavin Shan }
685d366d28cSGavin Shan 
686e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus)
6877cc5997dSYinghai Lu {
6887cc5997dSYinghai Lu 	unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
6897cc5997dSYinghai Lu 				  IORESOURCE_PREFETCH;
6907cc5997dSYinghai Lu 
691d366d28cSGavin Shan 	pcibios_setup_bridge(bus, type);
6927cc5997dSYinghai Lu 	__pci_setup_bridge(bus, type);
6937cc5997dSYinghai Lu }
6947cc5997dSYinghai Lu 
6958505e729SYinghai Lu 
6968505e729SYinghai Lu int pci_claim_bridge_resource(struct pci_dev *bridge, int i)
6978505e729SYinghai Lu {
6988505e729SYinghai Lu 	if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END)
6998505e729SYinghai Lu 		return 0;
7008505e729SYinghai Lu 
7018505e729SYinghai Lu 	if (pci_claim_resource(bridge, i) == 0)
7020d607618SNicholas Johnson 		return 0;	/* Claimed the window */
7038505e729SYinghai Lu 
7048505e729SYinghai Lu 	if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
7058505e729SYinghai Lu 		return 0;
7068505e729SYinghai Lu 
7078505e729SYinghai Lu 	if (!pci_bus_clip_resource(bridge, i))
7080d607618SNicholas Johnson 		return -EINVAL;	/* Clipping didn't change anything */
7098505e729SYinghai Lu 
7108505e729SYinghai Lu 	switch (i - PCI_BRIDGE_RESOURCES) {
7118505e729SYinghai Lu 	case 0:
7128505e729SYinghai Lu 		pci_setup_bridge_io(bridge);
7138505e729SYinghai Lu 		break;
7148505e729SYinghai Lu 	case 1:
7158505e729SYinghai Lu 		pci_setup_bridge_mmio(bridge);
7168505e729SYinghai Lu 		break;
7178505e729SYinghai Lu 	case 2:
7188505e729SYinghai Lu 		pci_setup_bridge_mmio_pref(bridge);
7198505e729SYinghai Lu 		break;
7208505e729SYinghai Lu 	default:
7218505e729SYinghai Lu 		return -EINVAL;
7228505e729SYinghai Lu 	}
7238505e729SYinghai Lu 
7248505e729SYinghai Lu 	if (pci_claim_resource(bridge, i) == 0)
7250d607618SNicholas Johnson 		return 0;	/* Claimed a smaller window */
7268505e729SYinghai Lu 
7278505e729SYinghai Lu 	return -EINVAL;
7288505e729SYinghai Lu }
7298505e729SYinghai Lu 
7300d607618SNicholas Johnson /*
7310d607618SNicholas Johnson  * Check whether the bridge supports optional I/O and prefetchable memory
7320d607618SNicholas Johnson  * ranges.  If not, the respective base/limit registers must be read-only
7330d607618SNicholas Johnson  * and read as 0.
7340d607618SNicholas Johnson  */
73596bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus)
7361da177e4SLinus Torvalds {
7371da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
73851c48b31SBjorn Helgaas 	struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	b_res[1].flags |= IORESOURCE_MEM;
7411da177e4SLinus Torvalds 
74251c48b31SBjorn Helgaas 	if (bridge->io_window)
7431da177e4SLinus Torvalds 		b_res[0].flags |= IORESOURCE_IO;
744d2f54d9bSBjorn Helgaas 
74551c48b31SBjorn Helgaas 	if (bridge->pref_window) {
7461da177e4SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
74751c48b31SBjorn Helgaas 		if (bridge->pref_64_window) {
7481f82de10SYinghai Lu 			b_res[2].flags |= IORESOURCE_MEM_64;
74999586105SYinghai Lu 			b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
75099586105SYinghai Lu 		}
7511f82de10SYinghai Lu 	}
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
7540d607618SNicholas Johnson /*
755c13704f5SNicholas Johnson  * Helper function for sizing routines.  Assigned resources have non-NULL
756c13704f5SNicholas Johnson  * parent resource.
757c13704f5SNicholas Johnson  *
758c13704f5SNicholas Johnson  * Return first unassigned resource of the correct type.  If there is none,
759c13704f5SNicholas Johnson  * return first assigned resource of the correct type.  If none of the
760c13704f5SNicholas Johnson  * above, return NULL.
761c13704f5SNicholas Johnson  *
762c13704f5SNicholas Johnson  * Returning an assigned resource of the correct type allows the caller to
763c13704f5SNicholas Johnson  * distinguish between already assigned and no resource of the correct type.
7640d607618SNicholas Johnson  */
765c13704f5SNicholas Johnson static struct resource *find_bus_resource_of_type(struct pci_bus *bus,
7660d607618SNicholas Johnson 						  unsigned long type_mask,
7670d607618SNicholas Johnson 						  unsigned long type)
7681da177e4SLinus Torvalds {
769c13704f5SNicholas Johnson 	struct resource *r, *r_assigned = NULL;
7701da177e4SLinus Torvalds 	int i;
7711da177e4SLinus Torvalds 
77289a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, r, i) {
773299de034SIvan Kokshaysky 		if (r == &ioport_resource || r == &iomem_resource)
774299de034SIvan Kokshaysky 			continue;
77555a10984SJesse Barnes 		if (r && (r->flags & type_mask) == type && !r->parent)
7761da177e4SLinus Torvalds 			return r;
777c13704f5SNicholas Johnson 		if (r && (r->flags & type_mask) == type && !r_assigned)
778c13704f5SNicholas Johnson 			r_assigned = r;
7791da177e4SLinus Torvalds 	}
780c13704f5SNicholas Johnson 	return r_assigned;
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
78313583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size,
78413583b16SRam Pai 					resource_size_t min_size,
78513583b16SRam Pai 					resource_size_t size1,
786de3ffa30SJon Derrick 					resource_size_t add_size,
787de3ffa30SJon Derrick 					resource_size_t children_add_size,
78813583b16SRam Pai 					resource_size_t old_size,
78913583b16SRam Pai 					resource_size_t align)
79013583b16SRam Pai {
79113583b16SRam Pai 	if (size < min_size)
79213583b16SRam Pai 		size = min_size;
79313583b16SRam Pai 	if (old_size == 1)
79413583b16SRam Pai 		old_size = 0;
7950d607618SNicholas Johnson 	/*
7960d607618SNicholas Johnson 	 * To be fixed in 2.5: we should have sort of HAVE_ISA flag in the
7970d607618SNicholas Johnson 	 * struct pci_bus.
7980d607618SNicholas Johnson 	 */
79913583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
80013583b16SRam Pai 	size = (size & 0xff) + ((size & ~0xffUL) << 2);
80113583b16SRam Pai #endif
802de3ffa30SJon Derrick 	size = size + size1;
80313583b16SRam Pai 	if (size < old_size)
80413583b16SRam Pai 		size = old_size;
805de3ffa30SJon Derrick 
806de3ffa30SJon Derrick 	size = ALIGN(max(size, add_size) + children_add_size, align);
80713583b16SRam Pai 	return size;
80813583b16SRam Pai }
80913583b16SRam Pai 
81013583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size,
81113583b16SRam Pai 					 resource_size_t min_size,
812de3ffa30SJon Derrick 					 resource_size_t add_size,
813de3ffa30SJon Derrick 					 resource_size_t children_add_size,
81413583b16SRam Pai 					 resource_size_t old_size,
81513583b16SRam Pai 					 resource_size_t align)
81613583b16SRam Pai {
81713583b16SRam Pai 	if (size < min_size)
81813583b16SRam Pai 		size = min_size;
81913583b16SRam Pai 	if (old_size == 1)
82013583b16SRam Pai 		old_size = 0;
82113583b16SRam Pai 	if (size < old_size)
82213583b16SRam Pai 		size = old_size;
823de3ffa30SJon Derrick 
824de3ffa30SJon Derrick 	size = ALIGN(max(size, add_size) + children_add_size, align);
82513583b16SRam Pai 	return size;
82613583b16SRam Pai }
82713583b16SRam Pai 
828ac5ad93eSGavin Shan resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
829ac5ad93eSGavin Shan 						unsigned long type)
830ac5ad93eSGavin Shan {
831ac5ad93eSGavin Shan 	return 1;
832ac5ad93eSGavin Shan }
833ac5ad93eSGavin Shan 
834ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_MEM_ALIGN	0x100000	/* 1MiB */
835ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN	0x1000		/* 4KiB */
836ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN_1K	0x400		/* 1KiB */
837ac5ad93eSGavin Shan 
8380d607618SNicholas Johnson static resource_size_t window_alignment(struct pci_bus *bus, unsigned long type)
839ac5ad93eSGavin Shan {
840ac5ad93eSGavin Shan 	resource_size_t align = 1, arch_align;
841ac5ad93eSGavin Shan 
842ac5ad93eSGavin Shan 	if (type & IORESOURCE_MEM)
843ac5ad93eSGavin Shan 		align = PCI_P2P_DEFAULT_MEM_ALIGN;
844ac5ad93eSGavin Shan 	else if (type & IORESOURCE_IO) {
845ac5ad93eSGavin Shan 		/*
8460d607618SNicholas Johnson 		 * Per spec, I/O windows are 4K-aligned, but some bridges have
8470d607618SNicholas Johnson 		 * an extension to support 1K alignment.
848ac5ad93eSGavin Shan 		 */
849ac5ad93eSGavin Shan 		if (bus->self->io_window_1k)
850ac5ad93eSGavin Shan 			align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
851ac5ad93eSGavin Shan 		else
852ac5ad93eSGavin Shan 			align = PCI_P2P_DEFAULT_IO_ALIGN;
853ac5ad93eSGavin Shan 	}
854ac5ad93eSGavin Shan 
855ac5ad93eSGavin Shan 	arch_align = pcibios_window_alignment(bus, type);
856ac5ad93eSGavin Shan 	return max(align, arch_align);
857ac5ad93eSGavin Shan }
858ac5ad93eSGavin Shan 
859c8adf9a3SRam Pai /**
8600d607618SNicholas Johnson  * pbus_size_io() - Size the I/O window of a given bus
861c8adf9a3SRam Pai  *
8620d607618SNicholas Johnson  * @bus:		The bus
8630d607618SNicholas Johnson  * @min_size:		The minimum I/O window that must be allocated
8640d607618SNicholas Johnson  * @add_size:		Additional optional I/O window
8650d607618SNicholas Johnson  * @realloc_head:	Track the additional I/O window on this list
866c8adf9a3SRam Pai  *
8670d607618SNicholas Johnson  * Sizing the I/O windows of the PCI-PCI bridge is trivial, since these
8680d607618SNicholas Johnson  * windows have 1K or 4K granularity and the I/O ranges of non-bridge PCI
8690d607618SNicholas Johnson  * devices are limited to 256 bytes.  We must be careful with the ISA
8700d607618SNicholas Johnson  * aliasing though.
871c8adf9a3SRam Pai  */
872c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
8730d607618SNicholas Johnson 			 resource_size_t add_size,
8740d607618SNicholas Johnson 			 struct list_head *realloc_head)
8751da177e4SLinus Torvalds {
8761da177e4SLinus Torvalds 	struct pci_dev *dev;
877c13704f5SNicholas Johnson 	struct resource *b_res = find_bus_resource_of_type(bus, IORESOURCE_IO,
8785b285415SYinghai Lu 							   IORESOURCE_IO);
87911251a86SWei Yang 	resource_size_t size = 0, size0 = 0, size1 = 0;
880be768912SYinghai Lu 	resource_size_t children_add_size = 0;
8812d1d6678SBjorn Helgaas 	resource_size_t min_align, align;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	if (!b_res)
8841da177e4SLinus Torvalds 		return;
8851da177e4SLinus Torvalds 
886c13704f5SNicholas Johnson 	/* If resource is already assigned, nothing more to do */
887c13704f5SNicholas Johnson 	if (b_res->parent)
888c13704f5SNicholas Johnson 		return;
889c13704f5SNicholas Johnson 
8902d1d6678SBjorn Helgaas 	min_align = window_alignment(bus, IORESOURCE_IO);
8911da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
8921da177e4SLinus Torvalds 		int i;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
8951da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
8961da177e4SLinus Torvalds 			unsigned long r_size;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 			if (r->parent || !(r->flags & IORESOURCE_IO))
8991da177e4SLinus Torvalds 				continue;
900022edd86SZhao, Yu 			r_size = resource_size(r);
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 			if (r_size < 0x400)
9031da177e4SLinus Torvalds 				/* Might be re-aligned for ISA */
9041da177e4SLinus Torvalds 				size += r_size;
9051da177e4SLinus Torvalds 			else
9061da177e4SLinus Torvalds 				size1 += r_size;
907be768912SYinghai Lu 
908fd591341SYinghai Lu 			align = pci_resource_alignment(dev, r);
909fd591341SYinghai Lu 			if (align > min_align)
910fd591341SYinghai Lu 				min_align = align;
911fd591341SYinghai Lu 
9129e8bf93aSRam Pai 			if (realloc_head)
9139e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
9141da177e4SLinus Torvalds 		}
9151da177e4SLinus Torvalds 	}
916fd591341SYinghai Lu 
917de3ffa30SJon Derrick 	size0 = calculate_iosize(size, min_size, size1, 0, 0,
918fd591341SYinghai Lu 			resource_size(b_res), min_align);
919de3ffa30SJon Derrick 	size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
920de3ffa30SJon Derrick 		calculate_iosize(size, min_size, size1, add_size, children_add_size,
921fd591341SYinghai Lu 			resource_size(b_res), min_align);
922c8adf9a3SRam Pai 	if (!size0 && !size1) {
923865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
9247506dc79SFrederick Lawler 			pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
925227f0647SRyan Desfosses 				 b_res, &bus->busn_res);
9261da177e4SLinus Torvalds 		b_res->flags = 0;
9271da177e4SLinus Torvalds 		return;
9281da177e4SLinus Torvalds 	}
929fd591341SYinghai Lu 
930fd591341SYinghai Lu 	b_res->start = min_align;
931c8adf9a3SRam Pai 	b_res->end = b_res->start + size0 - 1;
93288452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
933b592443dSYinghai Lu 	if (size1 > size0 && realloc_head) {
934fd591341SYinghai Lu 		add_to_list(realloc_head, bus->self, b_res, size1-size0,
935fd591341SYinghai Lu 			    min_align);
93634c6b710SMohan Kumar 		pci_info(bus->self, "bridge window %pR to %pR add_size %llx\n",
937227f0647SRyan Desfosses 			 b_res, &bus->busn_res,
93811251a86SWei Yang 			 (unsigned long long) size1 - size0);
939b592443dSYinghai Lu 	}
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds 
942c121504eSGavin Shan static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
943c121504eSGavin Shan 						  int max_order)
944c121504eSGavin Shan {
945c121504eSGavin Shan 	resource_size_t align = 0;
946c121504eSGavin Shan 	resource_size_t min_align = 0;
947c121504eSGavin Shan 	int order;
948c121504eSGavin Shan 
949c121504eSGavin Shan 	for (order = 0; order <= max_order; order++) {
950c121504eSGavin Shan 		resource_size_t align1 = 1;
951c121504eSGavin Shan 
952c121504eSGavin Shan 		align1 <<= (order + 20);
953c121504eSGavin Shan 
954c121504eSGavin Shan 		if (!align)
955c121504eSGavin Shan 			min_align = align1;
956c121504eSGavin Shan 		else if (ALIGN(align + min_align, min_align) < align1)
957c121504eSGavin Shan 			min_align = align1 >> 1;
958c121504eSGavin Shan 		align += aligns[order];
959c121504eSGavin Shan 	}
960c121504eSGavin Shan 
961c121504eSGavin Shan 	return min_align;
962c121504eSGavin Shan }
963c121504eSGavin Shan 
964c8adf9a3SRam Pai /**
9650d607618SNicholas Johnson  * pbus_size_mem() - Size the memory window of a given bus
966c8adf9a3SRam Pai  *
9670d607618SNicholas Johnson  * @bus:		The bus
9680d607618SNicholas Johnson  * @mask:		Mask the resource flag, then compare it with type
9690d607618SNicholas Johnson  * @type:		The type of free resource from bridge
9700d607618SNicholas Johnson  * @type2:		Second match type
9710d607618SNicholas Johnson  * @type3:		Third match type
9720d607618SNicholas Johnson  * @min_size:		The minimum memory window that must be allocated
9730d607618SNicholas Johnson  * @add_size:		Additional optional memory window
9740d607618SNicholas Johnson  * @realloc_head:	Track the additional memory window on this list
975c8adf9a3SRam Pai  *
9760d607618SNicholas Johnson  * Calculate the size of the bus and minimal alignment which guarantees
9770d607618SNicholas Johnson  * that all child resources fit in this size.
97830afe8d0SBjorn Helgaas  *
9790d607618SNicholas Johnson  * Return -ENOSPC if there's no available bus resource of the desired
9800d607618SNicholas Johnson  * type.  Otherwise, set the bus resource start/end to indicate the
9810d607618SNicholas Johnson  * required size, add things to realloc_head (if supplied), and return 0.
982c8adf9a3SRam Pai  */
98328760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
9845b285415SYinghai Lu 			 unsigned long type, unsigned long type2,
9850d607618SNicholas Johnson 			 unsigned long type3, resource_size_t min_size,
9860d607618SNicholas Johnson 			 resource_size_t add_size,
987bdc4abecSYinghai Lu 			 struct list_head *realloc_head)
9881da177e4SLinus Torvalds {
9891da177e4SLinus Torvalds 	struct pci_dev *dev;
990c8adf9a3SRam Pai 	resource_size_t min_align, align, size, size0, size1;
9910d607618SNicholas Johnson 	resource_size_t aligns[18]; /* Alignments from 1MB to 128GB */
9921da177e4SLinus Torvalds 	int order, max_order;
993c13704f5SNicholas Johnson 	struct resource *b_res = find_bus_resource_of_type(bus,
9945b285415SYinghai Lu 					mask | IORESOURCE_PREFETCH, type);
995be768912SYinghai Lu 	resource_size_t children_add_size = 0;
996d74b9027SWei Yang 	resource_size_t children_add_align = 0;
997d74b9027SWei Yang 	resource_size_t add_align = 0;
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	if (!b_res)
100030afe8d0SBjorn Helgaas 		return -ENOSPC;
10011da177e4SLinus Torvalds 
1002c13704f5SNicholas Johnson 	/* If resource is already assigned, nothing more to do */
1003c13704f5SNicholas Johnson 	if (b_res->parent)
1004c13704f5SNicholas Johnson 		return 0;
1005c13704f5SNicholas Johnson 
10061da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
10071da177e4SLinus Torvalds 	max_order = 0;
10081da177e4SLinus Torvalds 	size = 0;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
10111da177e4SLinus Torvalds 		int i;
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
10141da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
1015c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
10161da177e4SLinus Torvalds 
1017a2220d80SDavid Daney 			if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) ||
1018a2220d80SDavid Daney 			    ((r->flags & mask) != type &&
10195b285415SYinghai Lu 			     (r->flags & mask) != type2 &&
10205b285415SYinghai Lu 			     (r->flags & mask) != type3))
10211da177e4SLinus Torvalds 				continue;
1022022edd86SZhao, Yu 			r_size = resource_size(r);
10232aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV
10240d607618SNicholas Johnson 			/* Put SRIOV requested res to the optional list */
10259e8bf93aSRam Pai 			if (realloc_head && i >= PCI_IOV_RESOURCES &&
10262aceefcbSYinghai Lu 					i <= PCI_IOV_RESOURCE_END) {
1027d74b9027SWei Yang 				add_align = max(pci_resource_alignment(dev, r), add_align);
10282aceefcbSYinghai Lu 				r->end = r->start - 1;
10290d607618SNicholas Johnson 				add_to_list(realloc_head, dev, r, r_size, 0 /* Don't care */);
10302aceefcbSYinghai Lu 				children_add_size += r_size;
10312aceefcbSYinghai Lu 				continue;
10322aceefcbSYinghai Lu 			}
10332aceefcbSYinghai Lu #endif
103414c8530dSAlan 			/*
103514c8530dSAlan 			 * aligns[0] is for 1MB (since bridge memory
103614c8530dSAlan 			 * windows are always at least 1MB aligned), so
103714c8530dSAlan 			 * keep "order" from being negative for smaller
103814c8530dSAlan 			 * resources.
103914c8530dSAlan 			 */
10406faf17f6SChris Wright 			align = pci_resource_alignment(dev, r);
10411da177e4SLinus Torvalds 			order = __ffs(align) - 20;
104214c8530dSAlan 			if (order < 0)
104314c8530dSAlan 				order = 0;
104414c8530dSAlan 			if (order >= ARRAY_SIZE(aligns)) {
10457506dc79SFrederick Lawler 				pci_warn(dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
1046227f0647SRyan Desfosses 					 i, r, (unsigned long long) align);
10471da177e4SLinus Torvalds 				r->flags = 0;
10481da177e4SLinus Torvalds 				continue;
10491da177e4SLinus Torvalds 			}
1050c9c75143SYongji Xie 			size += max(r_size, align);
10510d607618SNicholas Johnson 			/*
10520d607618SNicholas Johnson 			 * Exclude ranges with size > align from calculation of
10530d607618SNicholas Johnson 			 * the alignment.
10540d607618SNicholas Johnson 			 */
1055c9c75143SYongji Xie 			if (r_size <= align)
10561da177e4SLinus Torvalds 				aligns[order] += align;
10571da177e4SLinus Torvalds 			if (order > max_order)
10581da177e4SLinus Torvalds 				max_order = order;
1059be768912SYinghai Lu 
1060d74b9027SWei Yang 			if (realloc_head) {
10619e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
1062d74b9027SWei Yang 				children_add_align = get_res_add_align(realloc_head, r);
1063d74b9027SWei Yang 				add_align = max(add_align, children_add_align);
1064d74b9027SWei Yang 			}
10651da177e4SLinus Torvalds 		}
10661da177e4SLinus Torvalds 	}
10678308c54dSJeremy Fitzhardinge 
1068c121504eSGavin Shan 	min_align = calculate_mem_align(aligns, max_order);
10693ad94b0dSWei Yang 	min_align = max(min_align, window_alignment(bus, b_res->flags));
1070de3ffa30SJon Derrick 	size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), min_align);
1071d74b9027SWei Yang 	add_align = max(min_align, add_align);
1072de3ffa30SJon Derrick 	size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
1073de3ffa30SJon Derrick 		calculate_memsize(size, min_size, add_size, children_add_size,
1074d74b9027SWei Yang 				resource_size(b_res), add_align);
1075c8adf9a3SRam Pai 	if (!size0 && !size1) {
1076865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
10777506dc79SFrederick Lawler 			pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
1078227f0647SRyan Desfosses 				 b_res, &bus->busn_res);
10791da177e4SLinus Torvalds 		b_res->flags = 0;
108030afe8d0SBjorn Helgaas 		return 0;
10811da177e4SLinus Torvalds 	}
10821da177e4SLinus Torvalds 	b_res->start = min_align;
1083c8adf9a3SRam Pai 	b_res->end = size0 + min_align - 1;
10845b285415SYinghai Lu 	b_res->flags |= IORESOURCE_STARTALIGN;
1085b592443dSYinghai Lu 	if (size1 > size0 && realloc_head) {
1086d74b9027SWei Yang 		add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align);
108734c6b710SMohan Kumar 		pci_info(bus->self, "bridge window %pR to %pR add_size %llx add_align %llx\n",
1088227f0647SRyan Desfosses 			   b_res, &bus->busn_res,
1089d74b9027SWei Yang 			   (unsigned long long) (size1 - size0),
1090d74b9027SWei Yang 			   (unsigned long long) add_align);
1091b592443dSYinghai Lu 	}
109230afe8d0SBjorn Helgaas 	return 0;
10931da177e4SLinus Torvalds }
10941da177e4SLinus Torvalds 
10950a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res)
10960a2daa1cSRam Pai {
10970a2daa1cSRam Pai 	if (res->flags & IORESOURCE_IO)
10980a2daa1cSRam Pai 		return pci_cardbus_io_size;
10990a2daa1cSRam Pai 	if (res->flags & IORESOURCE_MEM)
11000a2daa1cSRam Pai 		return pci_cardbus_mem_size;
11010a2daa1cSRam Pai 	return 0;
11020a2daa1cSRam Pai }
11030a2daa1cSRam Pai 
11040a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus,
1105bdc4abecSYinghai Lu 				 struct list_head *realloc_head)
11061da177e4SLinus Torvalds {
11071da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
11081da177e4SLinus Torvalds 	struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
110911848934SYinghai Lu 	resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
11101da177e4SLinus Torvalds 	u16 ctrl;
11111da177e4SLinus Torvalds 
11123796f1e2SYinghai Lu 	if (b_res[0].parent)
11133796f1e2SYinghai Lu 		goto handle_b_res_1;
11141da177e4SLinus Torvalds 	/*
11150d607618SNicholas Johnson 	 * Reserve some resources for CardBus.  We reserve a fixed amount
11160d607618SNicholas Johnson 	 * of bus space for CardBus bridges.
11171da177e4SLinus Torvalds 	 */
111811848934SYinghai Lu 	b_res[0].start = pci_cardbus_io_size;
111911848934SYinghai Lu 	b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
112011848934SYinghai Lu 	b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
112111848934SYinghai Lu 	if (realloc_head) {
112211848934SYinghai Lu 		b_res[0].end -= pci_cardbus_io_size;
112311848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
112411848934SYinghai Lu 				pci_cardbus_io_size);
112511848934SYinghai Lu 	}
11261da177e4SLinus Torvalds 
11273796f1e2SYinghai Lu handle_b_res_1:
11283796f1e2SYinghai Lu 	if (b_res[1].parent)
11293796f1e2SYinghai Lu 		goto handle_b_res_2;
113011848934SYinghai Lu 	b_res[1].start = pci_cardbus_io_size;
113111848934SYinghai Lu 	b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
113211848934SYinghai Lu 	b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
113311848934SYinghai Lu 	if (realloc_head) {
113411848934SYinghai Lu 		b_res[1].end -= pci_cardbus_io_size;
113511848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
113611848934SYinghai Lu 				 pci_cardbus_io_size);
113711848934SYinghai Lu 	}
11381da177e4SLinus Torvalds 
11393796f1e2SYinghai Lu handle_b_res_2:
11400d607618SNicholas Johnson 	/* MEM1 must not be pref MMIO */
1141dcef0d06SYinghai Lu 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1142dcef0d06SYinghai Lu 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
1143dcef0d06SYinghai Lu 		ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
1144dcef0d06SYinghai Lu 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1145dcef0d06SYinghai Lu 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1146dcef0d06SYinghai Lu 	}
1147dcef0d06SYinghai Lu 
11480d607618SNicholas Johnson 	/* Check whether prefetchable memory is supported by this bridge. */
11491da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
11501da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
11511da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
11521da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
11531da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
11541da177e4SLinus Torvalds 	}
11551da177e4SLinus Torvalds 
11563796f1e2SYinghai Lu 	if (b_res[2].parent)
11573796f1e2SYinghai Lu 		goto handle_b_res_3;
11581da177e4SLinus Torvalds 	/*
11590d607618SNicholas Johnson 	 * If we have prefetchable memory support, allocate two regions.
11600d607618SNicholas Johnson 	 * Otherwise, allocate one region of twice the size.
11611da177e4SLinus Torvalds 	 */
11621da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
116311848934SYinghai Lu 		b_res[2].start = pci_cardbus_mem_size;
116411848934SYinghai Lu 		b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
116511848934SYinghai Lu 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
116611848934SYinghai Lu 				  IORESOURCE_STARTALIGN;
116711848934SYinghai Lu 		if (realloc_head) {
116811848934SYinghai Lu 			b_res[2].end -= pci_cardbus_mem_size;
116911848934SYinghai Lu 			add_to_list(realloc_head, bridge, b_res+2,
117011848934SYinghai Lu 				 pci_cardbus_mem_size, pci_cardbus_mem_size);
11711da177e4SLinus Torvalds 		}
11720a2daa1cSRam Pai 
11730d607618SNicholas Johnson 		/* Reduce that to half */
117411848934SYinghai Lu 		b_res_3_size = pci_cardbus_mem_size;
117511848934SYinghai Lu 	}
117611848934SYinghai Lu 
11773796f1e2SYinghai Lu handle_b_res_3:
11783796f1e2SYinghai Lu 	if (b_res[3].parent)
11793796f1e2SYinghai Lu 		goto handle_done;
118011848934SYinghai Lu 	b_res[3].start = pci_cardbus_mem_size;
118111848934SYinghai Lu 	b_res[3].end = b_res[3].start + b_res_3_size - 1;
118211848934SYinghai Lu 	b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
118311848934SYinghai Lu 	if (realloc_head) {
118411848934SYinghai Lu 		b_res[3].end -= b_res_3_size;
118511848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
118611848934SYinghai Lu 				 pci_cardbus_mem_size);
118711848934SYinghai Lu 	}
11883796f1e2SYinghai Lu 
11893796f1e2SYinghai Lu handle_done:
11903796f1e2SYinghai Lu 	;
11911da177e4SLinus Torvalds }
11921da177e4SLinus Torvalds 
119310874f5aSBjorn Helgaas void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
11941da177e4SLinus Torvalds {
11951da177e4SLinus Torvalds 	struct pci_dev *dev;
11965b285415SYinghai Lu 	unsigned long mask, prefmask, type2 = 0, type3 = 0;
1197d7b8a217SNicholas Johnson 	resource_size_t additional_io_size = 0, additional_mmio_size = 0,
1198d7b8a217SNicholas Johnson 			additional_mmio_pref_size = 0;
11995b285415SYinghai Lu 	struct resource *b_res;
120030afe8d0SBjorn Helgaas 	int ret;
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
12031da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
12041da177e4SLinus Torvalds 		if (!b)
12051da177e4SLinus Torvalds 			continue;
12061da177e4SLinus Torvalds 
1207b2fb5cc5SHonghui Zhang 		switch (dev->hdr_type) {
1208b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_CARDBUS:
12099e8bf93aSRam Pai 			pci_bus_size_cardbus(b, realloc_head);
12101da177e4SLinus Torvalds 			break;
12111da177e4SLinus Torvalds 
1212b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_BRIDGE:
12131da177e4SLinus Torvalds 		default:
12149e8bf93aSRam Pai 			__pci_bus_size_bridges(b, realloc_head);
12151da177e4SLinus Torvalds 			break;
12161da177e4SLinus Torvalds 		}
12171da177e4SLinus Torvalds 	}
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 	/* The root bus? */
12202ba29e27SWei Yang 	if (pci_is_root_bus(bus))
12211da177e4SLinus Torvalds 		return;
12221da177e4SLinus Torvalds 
1223b2fb5cc5SHonghui Zhang 	switch (bus->self->hdr_type) {
1224b2fb5cc5SHonghui Zhang 	case PCI_HEADER_TYPE_CARDBUS:
12250d607618SNicholas Johnson 		/* Don't size CardBuses yet */
12261da177e4SLinus Torvalds 		break;
12271da177e4SLinus Torvalds 
1228b2fb5cc5SHonghui Zhang 	case PCI_HEADER_TYPE_BRIDGE:
12291da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
123028760489SEric W. Biederman 		if (bus->self->is_hotplug_bridge) {
1231c8adf9a3SRam Pai 			additional_io_size  = pci_hotplug_io_size;
1232d7b8a217SNicholas Johnson 			additional_mmio_size = pci_hotplug_mmio_size;
1233d7b8a217SNicholas Johnson 			additional_mmio_pref_size = pci_hotplug_mmio_pref_size;
123428760489SEric W. Biederman 		}
123567d29b5cSBjorn Helgaas 		/* Fall through */
12361da177e4SLinus Torvalds 	default:
123719aa7ee4SYinghai Lu 		pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
123819aa7ee4SYinghai Lu 			     additional_io_size, realloc_head);
123967d29b5cSBjorn Helgaas 
124067d29b5cSBjorn Helgaas 		/*
124167d29b5cSBjorn Helgaas 		 * If there's a 64-bit prefetchable MMIO window, compute
124267d29b5cSBjorn Helgaas 		 * the size required to put all 64-bit prefetchable
124367d29b5cSBjorn Helgaas 		 * resources in it.
124467d29b5cSBjorn Helgaas 		 */
12455b285415SYinghai Lu 		b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES];
12461da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
12471da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
12485b285415SYinghai Lu 		if (b_res[2].flags & IORESOURCE_MEM_64) {
12495b285415SYinghai Lu 			prefmask |= IORESOURCE_MEM_64;
125030afe8d0SBjorn Helgaas 			ret = pbus_size_mem(bus, prefmask, prefmask,
12515b285415SYinghai Lu 				prefmask, prefmask,
1252d7b8a217SNicholas Johnson 				realloc_head ? 0 : additional_mmio_pref_size,
1253d7b8a217SNicholas Johnson 				additional_mmio_pref_size, realloc_head);
125467d29b5cSBjorn Helgaas 
12555b285415SYinghai Lu 			/*
125667d29b5cSBjorn Helgaas 			 * If successful, all non-prefetchable resources
125767d29b5cSBjorn Helgaas 			 * and any 32-bit prefetchable resources will go in
125867d29b5cSBjorn Helgaas 			 * the non-prefetchable window.
125967d29b5cSBjorn Helgaas 			 */
126067d29b5cSBjorn Helgaas 			if (ret == 0) {
12615b285415SYinghai Lu 				mask = prefmask;
12625b285415SYinghai Lu 				type2 = prefmask & ~IORESOURCE_MEM_64;
12635b285415SYinghai Lu 				type3 = prefmask & ~IORESOURCE_PREFETCH;
12645b285415SYinghai Lu 			}
12655b285415SYinghai Lu 		}
126667d29b5cSBjorn Helgaas 
126767d29b5cSBjorn Helgaas 		/*
126867d29b5cSBjorn Helgaas 		 * If there is no 64-bit prefetchable window, compute the
126967d29b5cSBjorn Helgaas 		 * size required to put all prefetchable resources in the
127067d29b5cSBjorn Helgaas 		 * 32-bit prefetchable window (if there is one).
127167d29b5cSBjorn Helgaas 		 */
12725b285415SYinghai Lu 		if (!type2) {
12735b285415SYinghai Lu 			prefmask &= ~IORESOURCE_MEM_64;
127430afe8d0SBjorn Helgaas 			ret = pbus_size_mem(bus, prefmask, prefmask,
12755b285415SYinghai Lu 				prefmask, prefmask,
1276d7b8a217SNicholas Johnson 				realloc_head ? 0 : additional_mmio_pref_size,
1277d7b8a217SNicholas Johnson 				additional_mmio_pref_size, realloc_head);
127867d29b5cSBjorn Helgaas 
127967d29b5cSBjorn Helgaas 			/*
128067d29b5cSBjorn Helgaas 			 * If successful, only non-prefetchable resources
128167d29b5cSBjorn Helgaas 			 * will go in the non-prefetchable window.
128267d29b5cSBjorn Helgaas 			 */
128367d29b5cSBjorn Helgaas 			if (ret == 0)
12845b285415SYinghai Lu 				mask = prefmask;
128528760489SEric W. Biederman 			else
1286d7b8a217SNicholas Johnson 				additional_mmio_size += additional_mmio_pref_size;
128767d29b5cSBjorn Helgaas 
12885b285415SYinghai Lu 			type2 = type3 = IORESOURCE_MEM;
12895b285415SYinghai Lu 		}
129067d29b5cSBjorn Helgaas 
129167d29b5cSBjorn Helgaas 		/*
129267d29b5cSBjorn Helgaas 		 * Compute the size required to put everything else in the
129367d29b5cSBjorn Helgaas 		 * non-prefetchable window. This includes:
129467d29b5cSBjorn Helgaas 		 *
129567d29b5cSBjorn Helgaas 		 *   - all non-prefetchable resources
129667d29b5cSBjorn Helgaas 		 *   - 32-bit prefetchable resources if there's a 64-bit
129767d29b5cSBjorn Helgaas 		 *     prefetchable window or no prefetchable window at all
12980d607618SNicholas Johnson 		 *   - 64-bit prefetchable resources if there's no prefetchable
12990d607618SNicholas Johnson 		 *     window at all
130067d29b5cSBjorn Helgaas 		 *
13010d607618SNicholas Johnson 		 * Note that the strategy in __pci_assign_resource() must match
13020d607618SNicholas Johnson 		 * that used here. Specifically, we cannot put a 32-bit
13030d607618SNicholas Johnson 		 * prefetchable resource in a 64-bit prefetchable window.
130467d29b5cSBjorn Helgaas 		 */
13055b285415SYinghai Lu 		pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
1306d7b8a217SNicholas Johnson 			      realloc_head ? 0 : additional_mmio_size,
1307d7b8a217SNicholas Johnson 			      additional_mmio_size, realloc_head);
13081da177e4SLinus Torvalds 		break;
13091da177e4SLinus Torvalds 	}
13101da177e4SLinus Torvalds }
1311c8adf9a3SRam Pai 
131210874f5aSBjorn Helgaas void pci_bus_size_bridges(struct pci_bus *bus)
1313c8adf9a3SRam Pai {
1314c8adf9a3SRam Pai 	__pci_bus_size_bridges(bus, NULL);
1315c8adf9a3SRam Pai }
13161da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
13171da177e4SLinus Torvalds 
1318d04d0111SDavid Daney static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r)
1319d04d0111SDavid Daney {
1320d04d0111SDavid Daney 	int i;
1321d04d0111SDavid Daney 	struct resource *parent_r;
1322d04d0111SDavid Daney 	unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM |
1323d04d0111SDavid Daney 			     IORESOURCE_PREFETCH;
1324d04d0111SDavid Daney 
1325d04d0111SDavid Daney 	pci_bus_for_each_resource(b, parent_r, i) {
1326d04d0111SDavid Daney 		if (!parent_r)
1327d04d0111SDavid Daney 			continue;
1328d04d0111SDavid Daney 
1329d04d0111SDavid Daney 		if ((r->flags & mask) == (parent_r->flags & mask) &&
1330d04d0111SDavid Daney 		    resource_contains(parent_r, r))
1331d04d0111SDavid Daney 			request_resource(parent_r, r);
1332d04d0111SDavid Daney 	}
1333d04d0111SDavid Daney }
1334d04d0111SDavid Daney 
1335d04d0111SDavid Daney /*
13360d607618SNicholas Johnson  * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they are
13370d607618SNicholas Johnson  * skipped by pbus_assign_resources_sorted().
1338d04d0111SDavid Daney  */
1339d04d0111SDavid Daney static void pdev_assign_fixed_resources(struct pci_dev *dev)
1340d04d0111SDavid Daney {
1341d04d0111SDavid Daney 	int i;
1342d04d0111SDavid Daney 
1343d04d0111SDavid Daney 	for (i = 0; i <  PCI_NUM_RESOURCES; i++) {
1344d04d0111SDavid Daney 		struct pci_bus *b;
1345d04d0111SDavid Daney 		struct resource *r = &dev->resource[i];
1346d04d0111SDavid Daney 
1347d04d0111SDavid Daney 		if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) ||
1348d04d0111SDavid Daney 		    !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
1349d04d0111SDavid Daney 			continue;
1350d04d0111SDavid Daney 
1351d04d0111SDavid Daney 		b = dev->bus;
1352d04d0111SDavid Daney 		while (b && !r->parent) {
1353d04d0111SDavid Daney 			assign_fixed_resource_on_bus(b, r);
1354d04d0111SDavid Daney 			b = b->parent;
1355d04d0111SDavid Daney 		}
1356d04d0111SDavid Daney 	}
1357d04d0111SDavid Daney }
1358d04d0111SDavid Daney 
135910874f5aSBjorn Helgaas void __pci_bus_assign_resources(const struct pci_bus *bus,
1360bdc4abecSYinghai Lu 				struct list_head *realloc_head,
1361bdc4abecSYinghai Lu 				struct list_head *fail_head)
13621da177e4SLinus Torvalds {
13631da177e4SLinus Torvalds 	struct pci_bus *b;
13641da177e4SLinus Torvalds 	struct pci_dev *dev;
13651da177e4SLinus Torvalds 
13669e8bf93aSRam Pai 	pbus_assign_resources_sorted(bus, realloc_head, fail_head);
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
1369d04d0111SDavid Daney 		pdev_assign_fixed_resources(dev);
1370d04d0111SDavid Daney 
13711da177e4SLinus Torvalds 		b = dev->subordinate;
13721da177e4SLinus Torvalds 		if (!b)
13731da177e4SLinus Torvalds 			continue;
13741da177e4SLinus Torvalds 
13759e8bf93aSRam Pai 		__pci_bus_assign_resources(b, realloc_head, fail_head);
13761da177e4SLinus Torvalds 
1377b2fb5cc5SHonghui Zhang 		switch (dev->hdr_type) {
1378b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_BRIDGE:
13796841ec68SYinghai Lu 			if (!pci_is_enabled(dev))
13801da177e4SLinus Torvalds 				pci_setup_bridge(b);
13811da177e4SLinus Torvalds 			break;
13821da177e4SLinus Torvalds 
1383b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_CARDBUS:
13841da177e4SLinus Torvalds 			pci_setup_cardbus(b);
13851da177e4SLinus Torvalds 			break;
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 		default:
13887506dc79SFrederick Lawler 			pci_info(dev, "not setting up bridge for bus %04x:%02x\n",
1389227f0647SRyan Desfosses 				 pci_domain_nr(b), b->number);
13901da177e4SLinus Torvalds 			break;
13911da177e4SLinus Torvalds 		}
13921da177e4SLinus Torvalds 	}
13931da177e4SLinus Torvalds }
1394568ddef8SYinghai Lu 
139510874f5aSBjorn Helgaas void pci_bus_assign_resources(const struct pci_bus *bus)
1396568ddef8SYinghai Lu {
1397c8adf9a3SRam Pai 	__pci_bus_assign_resources(bus, NULL, NULL);
1398568ddef8SYinghai Lu }
13991da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
14001da177e4SLinus Torvalds 
1401765bf9b7SLorenzo Pieralisi static void pci_claim_device_resources(struct pci_dev *dev)
1402765bf9b7SLorenzo Pieralisi {
1403765bf9b7SLorenzo Pieralisi 	int i;
1404765bf9b7SLorenzo Pieralisi 
1405765bf9b7SLorenzo Pieralisi 	for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
1406765bf9b7SLorenzo Pieralisi 		struct resource *r = &dev->resource[i];
1407765bf9b7SLorenzo Pieralisi 
1408765bf9b7SLorenzo Pieralisi 		if (!r->flags || r->parent)
1409765bf9b7SLorenzo Pieralisi 			continue;
1410765bf9b7SLorenzo Pieralisi 
1411765bf9b7SLorenzo Pieralisi 		pci_claim_resource(dev, i);
1412765bf9b7SLorenzo Pieralisi 	}
1413765bf9b7SLorenzo Pieralisi }
1414765bf9b7SLorenzo Pieralisi 
1415765bf9b7SLorenzo Pieralisi static void pci_claim_bridge_resources(struct pci_dev *dev)
1416765bf9b7SLorenzo Pieralisi {
1417765bf9b7SLorenzo Pieralisi 	int i;
1418765bf9b7SLorenzo Pieralisi 
1419765bf9b7SLorenzo Pieralisi 	for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
1420765bf9b7SLorenzo Pieralisi 		struct resource *r = &dev->resource[i];
1421765bf9b7SLorenzo Pieralisi 
1422765bf9b7SLorenzo Pieralisi 		if (!r->flags || r->parent)
1423765bf9b7SLorenzo Pieralisi 			continue;
1424765bf9b7SLorenzo Pieralisi 
1425765bf9b7SLorenzo Pieralisi 		pci_claim_bridge_resource(dev, i);
1426765bf9b7SLorenzo Pieralisi 	}
1427765bf9b7SLorenzo Pieralisi }
1428765bf9b7SLorenzo Pieralisi 
1429765bf9b7SLorenzo Pieralisi static void pci_bus_allocate_dev_resources(struct pci_bus *b)
1430765bf9b7SLorenzo Pieralisi {
1431765bf9b7SLorenzo Pieralisi 	struct pci_dev *dev;
1432765bf9b7SLorenzo Pieralisi 	struct pci_bus *child;
1433765bf9b7SLorenzo Pieralisi 
1434765bf9b7SLorenzo Pieralisi 	list_for_each_entry(dev, &b->devices, bus_list) {
1435765bf9b7SLorenzo Pieralisi 		pci_claim_device_resources(dev);
1436765bf9b7SLorenzo Pieralisi 
1437765bf9b7SLorenzo Pieralisi 		child = dev->subordinate;
1438765bf9b7SLorenzo Pieralisi 		if (child)
1439765bf9b7SLorenzo Pieralisi 			pci_bus_allocate_dev_resources(child);
1440765bf9b7SLorenzo Pieralisi 	}
1441765bf9b7SLorenzo Pieralisi }
1442765bf9b7SLorenzo Pieralisi 
1443765bf9b7SLorenzo Pieralisi static void pci_bus_allocate_resources(struct pci_bus *b)
1444765bf9b7SLorenzo Pieralisi {
1445765bf9b7SLorenzo Pieralisi 	struct pci_bus *child;
1446765bf9b7SLorenzo Pieralisi 
1447765bf9b7SLorenzo Pieralisi 	/*
14480d607618SNicholas Johnson 	 * Carry out a depth-first search on the PCI bus tree to allocate
14490d607618SNicholas Johnson 	 * bridge apertures.  Read the programmed bridge bases and
14500d607618SNicholas Johnson 	 * recursively claim the respective bridge resources.
1451765bf9b7SLorenzo Pieralisi 	 */
1452765bf9b7SLorenzo Pieralisi 	if (b->self) {
1453765bf9b7SLorenzo Pieralisi 		pci_read_bridge_bases(b);
1454765bf9b7SLorenzo Pieralisi 		pci_claim_bridge_resources(b->self);
1455765bf9b7SLorenzo Pieralisi 	}
1456765bf9b7SLorenzo Pieralisi 
1457765bf9b7SLorenzo Pieralisi 	list_for_each_entry(child, &b->children, node)
1458765bf9b7SLorenzo Pieralisi 		pci_bus_allocate_resources(child);
1459765bf9b7SLorenzo Pieralisi }
1460765bf9b7SLorenzo Pieralisi 
1461765bf9b7SLorenzo Pieralisi void pci_bus_claim_resources(struct pci_bus *b)
1462765bf9b7SLorenzo Pieralisi {
1463765bf9b7SLorenzo Pieralisi 	pci_bus_allocate_resources(b);
1464765bf9b7SLorenzo Pieralisi 	pci_bus_allocate_dev_resources(b);
1465765bf9b7SLorenzo Pieralisi }
1466765bf9b7SLorenzo Pieralisi EXPORT_SYMBOL(pci_bus_claim_resources);
1467765bf9b7SLorenzo Pieralisi 
146810874f5aSBjorn Helgaas static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
1469bdc4abecSYinghai Lu 					  struct list_head *add_head,
1470bdc4abecSYinghai Lu 					  struct list_head *fail_head)
14716841ec68SYinghai Lu {
14726841ec68SYinghai Lu 	struct pci_bus *b;
14736841ec68SYinghai Lu 
14748424d759SYinghai Lu 	pdev_assign_resources_sorted((struct pci_dev *)bridge,
14758424d759SYinghai Lu 					 add_head, fail_head);
14766841ec68SYinghai Lu 
14776841ec68SYinghai Lu 	b = bridge->subordinate;
14786841ec68SYinghai Lu 	if (!b)
14796841ec68SYinghai Lu 		return;
14806841ec68SYinghai Lu 
14818424d759SYinghai Lu 	__pci_bus_assign_resources(b, add_head, fail_head);
14826841ec68SYinghai Lu 
14836841ec68SYinghai Lu 	switch (bridge->class >> 8) {
14846841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_PCI:
14856841ec68SYinghai Lu 		pci_setup_bridge(b);
14866841ec68SYinghai Lu 		break;
14876841ec68SYinghai Lu 
14886841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_CARDBUS:
14896841ec68SYinghai Lu 		pci_setup_cardbus(b);
14906841ec68SYinghai Lu 		break;
14916841ec68SYinghai Lu 
14926841ec68SYinghai Lu 	default:
14937506dc79SFrederick Lawler 		pci_info(bridge, "not setting up bridge for bus %04x:%02x\n",
1494227f0647SRyan Desfosses 			 pci_domain_nr(b), b->number);
14956841ec68SYinghai Lu 		break;
14966841ec68SYinghai Lu 	}
14976841ec68SYinghai Lu }
1498cb21bc94SChristian König 
1499cb21bc94SChristian König #define PCI_RES_TYPE_MASK \
1500cb21bc94SChristian König 	(IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\
1501cb21bc94SChristian König 	 IORESOURCE_MEM_64)
1502cb21bc94SChristian König 
15035009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus,
15045009b460SYinghai Lu 					 unsigned long type)
15055009b460SYinghai Lu {
15065b285415SYinghai Lu 	struct pci_dev *dev = bus->self;
15075009b460SYinghai Lu 	struct resource *r;
15085b285415SYinghai Lu 	unsigned old_flags = 0;
15095b285415SYinghai Lu 	struct resource *b_res;
15105b285415SYinghai Lu 	int idx = 1;
15115009b460SYinghai Lu 
15125b285415SYinghai Lu 	b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
15135b285415SYinghai Lu 
15145b285415SYinghai Lu 	/*
15150d607618SNicholas Johnson 	 * 1. If IO port assignment fails, release bridge IO port.
15160d607618SNicholas Johnson 	 * 2. If non pref MMIO assignment fails, release bridge nonpref MMIO.
15170d607618SNicholas Johnson 	 * 3. If 64bit pref MMIO assignment fails, and bridge pref is 64bit,
15180d607618SNicholas Johnson 	 *    release bridge pref MMIO.
15190d607618SNicholas Johnson 	 * 4. If pref MMIO assignment fails, and bridge pref is 32bit,
15200d607618SNicholas Johnson 	 *    release bridge pref MMIO.
15210d607618SNicholas Johnson 	 * 5. If pref MMIO assignment fails, and bridge pref is not
15220d607618SNicholas Johnson 	 *    assigned, release bridge nonpref MMIO.
15235b285415SYinghai Lu 	 */
15245b285415SYinghai Lu 	if (type & IORESOURCE_IO)
15255b285415SYinghai Lu 		idx = 0;
15265b285415SYinghai Lu 	else if (!(type & IORESOURCE_PREFETCH))
15275b285415SYinghai Lu 		idx = 1;
15285b285415SYinghai Lu 	else if ((type & IORESOURCE_MEM_64) &&
15295b285415SYinghai Lu 		 (b_res[2].flags & IORESOURCE_MEM_64))
15305b285415SYinghai Lu 		idx = 2;
15315b285415SYinghai Lu 	else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
15325b285415SYinghai Lu 		 (b_res[2].flags & IORESOURCE_PREFETCH))
15335b285415SYinghai Lu 		idx = 2;
15345b285415SYinghai Lu 	else
15355b285415SYinghai Lu 		idx = 1;
15365b285415SYinghai Lu 
15375b285415SYinghai Lu 	r = &b_res[idx];
15385b285415SYinghai Lu 
15395009b460SYinghai Lu 	if (!r->parent)
15405b285415SYinghai Lu 		return;
15415b285415SYinghai Lu 
15420d607618SNicholas Johnson 	/* If there are children, release them all */
15435009b460SYinghai Lu 	release_child_resources(r);
15445009b460SYinghai Lu 	if (!release_resource(r)) {
1545cb21bc94SChristian König 		type = old_flags = r->flags & PCI_RES_TYPE_MASK;
154634c6b710SMohan Kumar 		pci_info(dev, "resource %d %pR released\n",
15475b285415SYinghai Lu 			 PCI_BRIDGE_RESOURCES + idx, r);
15480d607618SNicholas Johnson 		/* Keep the old size */
15495009b460SYinghai Lu 		r->end = resource_size(r) - 1;
15505009b460SYinghai Lu 		r->start = 0;
15515009b460SYinghai Lu 		r->flags = 0;
15525009b460SYinghai Lu 
15530d607618SNicholas Johnson 		/* Avoiding touch the one without PREF */
15545009b460SYinghai Lu 		if (type & IORESOURCE_PREFETCH)
15555009b460SYinghai Lu 			type = IORESOURCE_PREFETCH;
15565009b460SYinghai Lu 		__pci_setup_bridge(bus, type);
15570d607618SNicholas Johnson 		/* For next child res under same bridge */
15585b285415SYinghai Lu 		r->flags = old_flags;
15595009b460SYinghai Lu 	}
15605009b460SYinghai Lu }
15615009b460SYinghai Lu 
15625009b460SYinghai Lu enum release_type {
15635009b460SYinghai Lu 	leaf_only,
15645009b460SYinghai Lu 	whole_subtree,
15655009b460SYinghai Lu };
15660d607618SNicholas Johnson 
15675009b460SYinghai Lu /*
15680d607618SNicholas Johnson  * Try to release PCI bridge resources from leaf bridge, so we can allocate
15690d607618SNicholas Johnson  * a larger window later.
15705009b460SYinghai Lu  */
157110874f5aSBjorn Helgaas static void pci_bus_release_bridge_resources(struct pci_bus *bus,
15725009b460SYinghai Lu 					     unsigned long type,
15735009b460SYinghai Lu 					     enum release_type rel_type)
15745009b460SYinghai Lu {
15755009b460SYinghai Lu 	struct pci_dev *dev;
15765009b460SYinghai Lu 	bool is_leaf_bridge = true;
15775009b460SYinghai Lu 
15785009b460SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
15795009b460SYinghai Lu 		struct pci_bus *b = dev->subordinate;
15805009b460SYinghai Lu 		if (!b)
15815009b460SYinghai Lu 			continue;
15825009b460SYinghai Lu 
15835009b460SYinghai Lu 		is_leaf_bridge = false;
15845009b460SYinghai Lu 
15855009b460SYinghai Lu 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
15865009b460SYinghai Lu 			continue;
15875009b460SYinghai Lu 
15885009b460SYinghai Lu 		if (rel_type == whole_subtree)
15895009b460SYinghai Lu 			pci_bus_release_bridge_resources(b, type,
15905009b460SYinghai Lu 						 whole_subtree);
15915009b460SYinghai Lu 	}
15925009b460SYinghai Lu 
15935009b460SYinghai Lu 	if (pci_is_root_bus(bus))
15945009b460SYinghai Lu 		return;
15955009b460SYinghai Lu 
15965009b460SYinghai Lu 	if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
15975009b460SYinghai Lu 		return;
15985009b460SYinghai Lu 
15995009b460SYinghai Lu 	if ((rel_type == whole_subtree) || is_leaf_bridge)
16005009b460SYinghai Lu 		pci_bridge_release_resources(bus, type);
16015009b460SYinghai Lu }
16025009b460SYinghai Lu 
160376fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
160476fbc263SYinghai Lu {
160589a74eccSBjorn Helgaas 	struct resource *res;
160676fbc263SYinghai Lu 	int i;
160776fbc263SYinghai Lu 
160889a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
16097c9342b8SYinghai Lu 		if (!res || !res->end || !res->flags)
161076fbc263SYinghai Lu 			continue;
161176fbc263SYinghai Lu 
161234c6b710SMohan Kumar 		dev_info(&bus->dev, "resource %d %pR\n", i, res);
161376fbc263SYinghai Lu 	}
161476fbc263SYinghai Lu }
161576fbc263SYinghai Lu 
161676fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
161776fbc263SYinghai Lu {
161876fbc263SYinghai Lu 	struct pci_bus *b;
161976fbc263SYinghai Lu 	struct pci_dev *dev;
162076fbc263SYinghai Lu 
162176fbc263SYinghai Lu 
162276fbc263SYinghai Lu 	pci_bus_dump_res(bus);
162376fbc263SYinghai Lu 
162476fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
162576fbc263SYinghai Lu 		b = dev->subordinate;
162676fbc263SYinghai Lu 		if (!b)
162776fbc263SYinghai Lu 			continue;
162876fbc263SYinghai Lu 
162976fbc263SYinghai Lu 		pci_bus_dump_resources(b);
163076fbc263SYinghai Lu 	}
163176fbc263SYinghai Lu }
163276fbc263SYinghai Lu 
1633ff35147cSYinghai Lu static int pci_bus_get_depth(struct pci_bus *bus)
1634da7822e5SYinghai Lu {
1635da7822e5SYinghai Lu 	int depth = 0;
1636f2a230bdSWei Yang 	struct pci_bus *child_bus;
1637da7822e5SYinghai Lu 
1638f2a230bdSWei Yang 	list_for_each_entry(child_bus, &bus->children, node) {
1639da7822e5SYinghai Lu 		int ret;
1640da7822e5SYinghai Lu 
1641f2a230bdSWei Yang 		ret = pci_bus_get_depth(child_bus);
1642da7822e5SYinghai Lu 		if (ret + 1 > depth)
1643da7822e5SYinghai Lu 			depth = ret + 1;
1644da7822e5SYinghai Lu 	}
1645da7822e5SYinghai Lu 
1646da7822e5SYinghai Lu 	return depth;
1647da7822e5SYinghai Lu }
1648da7822e5SYinghai Lu 
1649b55438fdSYinghai Lu /*
1650b55438fdSYinghai Lu  * -1: undefined, will auto detect later
1651b55438fdSYinghai Lu  *  0: disabled by user
1652b55438fdSYinghai Lu  *  1: disabled by auto detect
1653b55438fdSYinghai Lu  *  2: enabled by user
1654b55438fdSYinghai Lu  *  3: enabled by auto detect
1655b55438fdSYinghai Lu  */
1656b55438fdSYinghai Lu enum enable_type {
1657b55438fdSYinghai Lu 	undefined = -1,
1658b55438fdSYinghai Lu 	user_disabled,
1659b55438fdSYinghai Lu 	auto_disabled,
1660b55438fdSYinghai Lu 	user_enabled,
1661b55438fdSYinghai Lu 	auto_enabled,
1662b55438fdSYinghai Lu };
1663b55438fdSYinghai Lu 
1664ff35147cSYinghai Lu static enum enable_type pci_realloc_enable = undefined;
1665b55438fdSYinghai Lu void __init pci_realloc_get_opt(char *str)
1666b55438fdSYinghai Lu {
1667b55438fdSYinghai Lu 	if (!strncmp(str, "off", 3))
1668b55438fdSYinghai Lu 		pci_realloc_enable = user_disabled;
1669b55438fdSYinghai Lu 	else if (!strncmp(str, "on", 2))
1670b55438fdSYinghai Lu 		pci_realloc_enable = user_enabled;
1671b55438fdSYinghai Lu }
1672ff35147cSYinghai Lu static bool pci_realloc_enabled(enum enable_type enable)
1673b55438fdSYinghai Lu {
1674967260cdSYinghai Lu 	return enable >= user_enabled;
1675b55438fdSYinghai Lu }
1676f483d392SRam Pai 
1677b07f2ebcSYinghai Lu #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1678ff35147cSYinghai Lu static int iov_resources_unassigned(struct pci_dev *dev, void *data)
1679223d96fcSYinghai Lu {
1680b07f2ebcSYinghai Lu 	int i;
1681223d96fcSYinghai Lu 	bool *unassigned = data;
1682b07f2ebcSYinghai Lu 
168339098edbSDenis Efremov 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
168439098edbSDenis Efremov 		struct resource *r = &dev->resource[i + PCI_IOV_RESOURCES];
1685fa216bf4SYinghai Lu 		struct pci_bus_region region;
1686b07f2ebcSYinghai Lu 
1687223d96fcSYinghai Lu 		/* Not assigned or rejected by kernel? */
1688fa216bf4SYinghai Lu 		if (!r->flags)
1689fa216bf4SYinghai Lu 			continue;
1690b07f2ebcSYinghai Lu 
1691fc279850SYinghai Lu 		pcibios_resource_to_bus(dev->bus, &region, r);
1692fa216bf4SYinghai Lu 		if (!region.start) {
1693223d96fcSYinghai Lu 			*unassigned = true;
16940d607618SNicholas Johnson 			return 1; /* Return early from pci_walk_bus() */
1695b07f2ebcSYinghai Lu 		}
1696b07f2ebcSYinghai Lu 	}
1697b07f2ebcSYinghai Lu 
1698223d96fcSYinghai Lu 	return 0;
1699223d96fcSYinghai Lu }
1700223d96fcSYinghai Lu 
1701ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1702967260cdSYinghai Lu 					   enum enable_type enable_local)
1703223d96fcSYinghai Lu {
1704223d96fcSYinghai Lu 	bool unassigned = false;
17057ac0d094SBenjamin Herrenschmidt 	struct pci_host_bridge *host;
1706223d96fcSYinghai Lu 
1707967260cdSYinghai Lu 	if (enable_local != undefined)
1708967260cdSYinghai Lu 		return enable_local;
1709223d96fcSYinghai Lu 
17107ac0d094SBenjamin Herrenschmidt 	host = pci_find_host_bridge(bus);
17117ac0d094SBenjamin Herrenschmidt 	if (host->preserve_config)
17127ac0d094SBenjamin Herrenschmidt 		return auto_disabled;
17137ac0d094SBenjamin Herrenschmidt 
1714223d96fcSYinghai Lu 	pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
1715967260cdSYinghai Lu 	if (unassigned)
1716967260cdSYinghai Lu 		return auto_enabled;
1717967260cdSYinghai Lu 
1718967260cdSYinghai Lu 	return enable_local;
1719b07f2ebcSYinghai Lu }
1720223d96fcSYinghai Lu #else
1721ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1722967260cdSYinghai Lu 					   enum enable_type enable_local)
1723967260cdSYinghai Lu {
1724967260cdSYinghai Lu 	return enable_local;
1725b07f2ebcSYinghai Lu }
1726b07f2ebcSYinghai Lu #endif
1727b07f2ebcSYinghai Lu 
1728da7822e5SYinghai Lu /*
17290d607618SNicholas Johnson  * First try will not touch PCI bridge res.
17300d607618SNicholas Johnson  * Second and later try will clear small leaf bridge res.
17310d607618SNicholas Johnson  * Will stop till to the max depth if can not find good one.
1732da7822e5SYinghai Lu  */
173339772038SYinghai Lu void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
17341da177e4SLinus Torvalds {
17350d607618SNicholas Johnson 	LIST_HEAD(realloc_head);
17360d607618SNicholas Johnson 	/* List of resources that want additional resources */
1737bdc4abecSYinghai Lu 	struct list_head *add_list = NULL;
1738da7822e5SYinghai Lu 	int tried_times = 0;
1739da7822e5SYinghai Lu 	enum release_type rel_type = leaf_only;
1740bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
1741b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
174219aa7ee4SYinghai Lu 	int pci_try_num = 1;
174355ed83a6SYinghai Lu 	enum enable_type enable_local;
1744da7822e5SYinghai Lu 
17450d607618SNicholas Johnson 	/* Don't realloc if asked to do so */
174655ed83a6SYinghai Lu 	enable_local = pci_realloc_detect(bus, pci_realloc_enable);
1747967260cdSYinghai Lu 	if (pci_realloc_enabled(enable_local)) {
174855ed83a6SYinghai Lu 		int max_depth = pci_bus_get_depth(bus);
174919aa7ee4SYinghai Lu 
1750da7822e5SYinghai Lu 		pci_try_num = max_depth + 1;
175134c6b710SMohan Kumar 		dev_info(&bus->dev, "max bus depth: %d pci_try_num: %d\n",
1752da7822e5SYinghai Lu 			 max_depth, pci_try_num);
175319aa7ee4SYinghai Lu 	}
1754da7822e5SYinghai Lu 
1755da7822e5SYinghai Lu again:
175619aa7ee4SYinghai Lu 	/*
17570d607618SNicholas Johnson 	 * Last try will use add_list, otherwise will try good to have as must
17580d607618SNicholas Johnson 	 * have, so can realloc parent bridge resource
175919aa7ee4SYinghai Lu 	 */
176019aa7ee4SYinghai Lu 	if (tried_times + 1 == pci_try_num)
1761bdc4abecSYinghai Lu 		add_list = &realloc_head;
17620d607618SNicholas Johnson 	/*
17630d607618SNicholas Johnson 	 * Depth first, calculate sizes and alignments of all subordinate buses.
17640d607618SNicholas Johnson 	 */
176519aa7ee4SYinghai Lu 	__pci_bus_size_bridges(bus, add_list);
1766c8adf9a3SRam Pai 
17671da177e4SLinus Torvalds 	/* Depth last, allocate resources and update the hardware. */
1768bdc4abecSYinghai Lu 	__pci_bus_assign_resources(bus, add_list, &fail_head);
176919aa7ee4SYinghai Lu 	if (add_list)
1770bdc4abecSYinghai Lu 		BUG_ON(!list_empty(add_list));
1771da7822e5SYinghai Lu 	tried_times++;
1772da7822e5SYinghai Lu 
17730d607618SNicholas Johnson 	/* Any device complain? */
1774bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
1775928bea96SYinghai Lu 		goto dump;
1776f483d392SRam Pai 
17770c5be0cbSYinghai Lu 	if (tried_times >= pci_try_num) {
1778967260cdSYinghai Lu 		if (enable_local == undefined)
177955ed83a6SYinghai Lu 			dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
1780967260cdSYinghai Lu 		else if (enable_local == auto_enabled)
178155ed83a6SYinghai Lu 			dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
1782eb572e7cSYinghai Lu 
1783bffc56d4SYinghai Lu 		free_list(&fail_head);
1784928bea96SYinghai Lu 		goto dump;
1785da7822e5SYinghai Lu 	}
1786da7822e5SYinghai Lu 
178734c6b710SMohan Kumar 	dev_info(&bus->dev, "No. %d try to assign unassigned res\n",
178834c6b710SMohan Kumar 		 tried_times + 1);
1789da7822e5SYinghai Lu 
17900d607618SNicholas Johnson 	/* Third times and later will not check if it is leaf */
1791da7822e5SYinghai Lu 	if ((tried_times + 1) > 2)
1792da7822e5SYinghai Lu 		rel_type = whole_subtree;
1793da7822e5SYinghai Lu 
1794da7822e5SYinghai Lu 	/*
1795da7822e5SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
17960d607618SNicholas Johnson 	 * child device under that bridge.
1797da7822e5SYinghai Lu 	 */
179861e83cddSYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list)
179961e83cddSYinghai Lu 		pci_bus_release_bridge_resources(fail_res->dev->bus,
1800cb21bc94SChristian König 						 fail_res->flags & PCI_RES_TYPE_MASK,
1801da7822e5SYinghai Lu 						 rel_type);
180261e83cddSYinghai Lu 
18030d607618SNicholas Johnson 	/* Restore size and flags */
1804b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1805b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
1806*9db8dc6dSLogan Gunthorpe 		int idx;
1807da7822e5SYinghai Lu 
1808b9b0bba9SYinghai Lu 		res->start = fail_res->start;
1809b9b0bba9SYinghai Lu 		res->end = fail_res->end;
1810b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
1811*9db8dc6dSLogan Gunthorpe 
1812*9db8dc6dSLogan Gunthorpe 		if (pci_is_bridge(fail_res->dev)) {
1813*9db8dc6dSLogan Gunthorpe 			idx = res - &fail_res->dev->resource[0];
1814*9db8dc6dSLogan Gunthorpe 			if (idx >= PCI_BRIDGE_RESOURCES &&
1815*9db8dc6dSLogan Gunthorpe 			    idx <= PCI_BRIDGE_RESOURCE_END)
1816da7822e5SYinghai Lu 				res->flags = 0;
1817da7822e5SYinghai Lu 		}
1818*9db8dc6dSLogan Gunthorpe 	}
1819bffc56d4SYinghai Lu 	free_list(&fail_head);
1820da7822e5SYinghai Lu 
1821da7822e5SYinghai Lu 	goto again;
1822da7822e5SYinghai Lu 
1823928bea96SYinghai Lu dump:
18240d607618SNicholas Johnson 	/* Dump the resource on buses */
182576fbc263SYinghai Lu 	pci_bus_dump_resources(bus);
182676fbc263SYinghai Lu }
18276841ec68SYinghai Lu 
182855ed83a6SYinghai Lu void __init pci_assign_unassigned_resources(void)
182955ed83a6SYinghai Lu {
183055ed83a6SYinghai Lu 	struct pci_bus *root_bus;
183155ed83a6SYinghai Lu 
1832584c5c42SRui Wang 	list_for_each_entry(root_bus, &pci_root_buses, node) {
183355ed83a6SYinghai Lu 		pci_assign_unassigned_root_bus_resources(root_bus);
1834d9c149d6SRui Wang 
18350d607618SNicholas Johnson 		/* Make sure the root bridge has a companion ACPI device */
1836d9c149d6SRui Wang 		if (ACPI_HANDLE(root_bus->bridge))
1837584c5c42SRui Wang 			acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
1838584c5c42SRui Wang 	}
183955ed83a6SYinghai Lu }
184055ed83a6SYinghai Lu 
18411a576772SMika Westerberg static void extend_bridge_window(struct pci_dev *bridge, struct resource *res,
18420d607618SNicholas Johnson 				 struct list_head *add_list,
18430d607618SNicholas Johnson 				 resource_size_t available)
18441a576772SMika Westerberg {
18451a576772SMika Westerberg 	struct pci_dev_resource *dev_res;
18461a576772SMika Westerberg 
18471a576772SMika Westerberg 	if (res->parent)
18481a576772SMika Westerberg 		return;
18491a576772SMika Westerberg 
18501a576772SMika Westerberg 	if (resource_size(res) >= available)
18511a576772SMika Westerberg 		return;
18521a576772SMika Westerberg 
18531a576772SMika Westerberg 	dev_res = res_to_dev_res(add_list, res);
18541a576772SMika Westerberg 	if (!dev_res)
18551a576772SMika Westerberg 		return;
18561a576772SMika Westerberg 
18571a576772SMika Westerberg 	/* Is there room to extend the window? */
18581a576772SMika Westerberg 	if (available - resource_size(res) <= dev_res->add_size)
18591a576772SMika Westerberg 		return;
18601a576772SMika Westerberg 
18611a576772SMika Westerberg 	dev_res->add_size = available - resource_size(res);
18627506dc79SFrederick Lawler 	pci_dbg(bridge, "bridge window %pR extended by %pa\n", res,
18631a576772SMika Westerberg 		&dev_res->add_size);
18641a576772SMika Westerberg }
18651a576772SMika Westerberg 
18661a576772SMika Westerberg static void pci_bus_distribute_available_resources(struct pci_bus *bus,
18670d607618SNicholas Johnson 					    struct list_head *add_list,
18680d607618SNicholas Johnson 					    resource_size_t available_io,
18690d607618SNicholas Johnson 					    resource_size_t available_mmio,
18700d607618SNicholas Johnson 					    resource_size_t available_mmio_pref)
18711a576772SMika Westerberg {
18721a576772SMika Westerberg 	resource_size_t remaining_io, remaining_mmio, remaining_mmio_pref;
18731a576772SMika Westerberg 	unsigned int normal_bridges = 0, hotplug_bridges = 0;
18741a576772SMika Westerberg 	struct resource *io_res, *mmio_res, *mmio_pref_res;
18751a576772SMika Westerberg 	struct pci_dev *dev, *bridge = bus->self;
18761a576772SMika Westerberg 
18771a576772SMika Westerberg 	io_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
18781a576772SMika Westerberg 	mmio_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
18791a576772SMika Westerberg 	mmio_pref_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
18801a576772SMika Westerberg 
18811a576772SMika Westerberg 	/*
18821a576772SMika Westerberg 	 * Update additional resource list (add_list) to fill all the
18831a576772SMika Westerberg 	 * extra resource space available for this port except the space
18841a576772SMika Westerberg 	 * calculated in __pci_bus_size_bridges() which covers all the
18851a576772SMika Westerberg 	 * devices currently connected to the port and below.
18861a576772SMika Westerberg 	 */
18871a576772SMika Westerberg 	extend_bridge_window(bridge, io_res, add_list, available_io);
18881a576772SMika Westerberg 	extend_bridge_window(bridge, mmio_res, add_list, available_mmio);
18891a576772SMika Westerberg 	extend_bridge_window(bridge, mmio_pref_res, add_list,
18901a576772SMika Westerberg 			     available_mmio_pref);
18911a576772SMika Westerberg 
18921a576772SMika Westerberg 	/*
18931a576772SMika Westerberg 	 * Calculate how many hotplug bridges and normal bridges there
18941a576772SMika Westerberg 	 * are on this bus.  We will distribute the additional available
18951a576772SMika Westerberg 	 * resources between hotplug bridges.
18961a576772SMika Westerberg 	 */
18971a576772SMika Westerberg 	for_each_pci_bridge(dev, bus) {
18981a576772SMika Westerberg 		if (dev->is_hotplug_bridge)
18991a576772SMika Westerberg 			hotplug_bridges++;
19001a576772SMika Westerberg 		else
19011a576772SMika Westerberg 			normal_bridges++;
19021a576772SMika Westerberg 	}
19031a576772SMika Westerberg 
19045c6bcc34SNicholas Johnson 	/*
19055c6bcc34SNicholas Johnson 	 * There is only one bridge on the bus so it gets all available
19065c6bcc34SNicholas Johnson 	 * resources which it can then distribute to the possible hotplug
19075c6bcc34SNicholas Johnson 	 * bridges below.
19085c6bcc34SNicholas Johnson 	 */
19095c6bcc34SNicholas Johnson 	if (hotplug_bridges + normal_bridges == 1) {
19105c6bcc34SNicholas Johnson 		dev = list_first_entry(&bus->devices, struct pci_dev, bus_list);
19115c6bcc34SNicholas Johnson 		if (dev->subordinate) {
19125c6bcc34SNicholas Johnson 			pci_bus_distribute_available_resources(dev->subordinate,
19135c6bcc34SNicholas Johnson 				add_list, available_io, available_mmio,
19145c6bcc34SNicholas Johnson 				available_mmio_pref);
19155c6bcc34SNicholas Johnson 		}
19165c6bcc34SNicholas Johnson 		return;
19175c6bcc34SNicholas Johnson 	}
19185c6bcc34SNicholas Johnson 
19196a381ea6SNicholas Johnson 	if (hotplug_bridges == 0)
19206a381ea6SNicholas Johnson 		return;
19216a381ea6SNicholas Johnson 
19225c6bcc34SNicholas Johnson 	/*
19235c6bcc34SNicholas Johnson 	 * Calculate the total amount of extra resource space we can
19245c6bcc34SNicholas Johnson 	 * pass to bridges below this one.  This is basically the
19255c6bcc34SNicholas Johnson 	 * extra space reduced by the minimal required space for the
19265c6bcc34SNicholas Johnson 	 * non-hotplug bridges.
19275c6bcc34SNicholas Johnson 	 */
19285c6bcc34SNicholas Johnson 	remaining_io = available_io;
19295c6bcc34SNicholas Johnson 	remaining_mmio = available_mmio;
19305c6bcc34SNicholas Johnson 	remaining_mmio_pref = available_mmio_pref;
19315c6bcc34SNicholas Johnson 
19321a576772SMika Westerberg 	for_each_pci_bridge(dev, bus) {
19331a576772SMika Westerberg 		const struct resource *res;
19341a576772SMika Westerberg 
19351a576772SMika Westerberg 		if (dev->is_hotplug_bridge)
19361a576772SMika Westerberg 			continue;
19371a576772SMika Westerberg 
19381a576772SMika Westerberg 		/*
19391a576772SMika Westerberg 		 * Reduce the available resource space by what the
19401a576772SMika Westerberg 		 * bridge and devices below it occupy.
19411a576772SMika Westerberg 		 */
19421a576772SMika Westerberg 		res = &dev->resource[PCI_BRIDGE_RESOURCES + 0];
19431a576772SMika Westerberg 		if (!res->parent && available_io > resource_size(res))
19441a576772SMika Westerberg 			remaining_io -= resource_size(res);
19451a576772SMika Westerberg 
19461a576772SMika Westerberg 		res = &dev->resource[PCI_BRIDGE_RESOURCES + 1];
19471a576772SMika Westerberg 		if (!res->parent && available_mmio > resource_size(res))
19481a576772SMika Westerberg 			remaining_mmio -= resource_size(res);
19491a576772SMika Westerberg 
19501a576772SMika Westerberg 		res = &dev->resource[PCI_BRIDGE_RESOURCES + 2];
19511a576772SMika Westerberg 		if (!res->parent && available_mmio_pref > resource_size(res))
19521a576772SMika Westerberg 			remaining_mmio_pref -= resource_size(res);
19531a576772SMika Westerberg 	}
19541a576772SMika Westerberg 
19551a576772SMika Westerberg 	/*
19561a576772SMika Westerberg 	 * Go over devices on this bus and distribute the remaining
19571a576772SMika Westerberg 	 * resource space between hotplug bridges.
19581a576772SMika Westerberg 	 */
19591a576772SMika Westerberg 	for_each_pci_bridge(dev, bus) {
196014fe5951SMika Westerberg 		resource_size_t align, io, mmio, mmio_pref;
19611a576772SMika Westerberg 		struct pci_bus *b;
19621a576772SMika Westerberg 
19631a576772SMika Westerberg 		b = dev->subordinate;
196414fe5951SMika Westerberg 		if (!b || !dev->is_hotplug_bridge)
19651a576772SMika Westerberg 			continue;
19661a576772SMika Westerberg 
19671a576772SMika Westerberg 		/*
196814fe5951SMika Westerberg 		 * Distribute available extra resources equally between
196914fe5951SMika Westerberg 		 * hotplug-capable downstream ports taking alignment into
197014fe5951SMika Westerberg 		 * account.
19711a576772SMika Westerberg 		 */
19721a576772SMika Westerberg 		align = pci_resource_alignment(bridge, io_res);
19731a576772SMika Westerberg 		io = div64_ul(available_io, hotplug_bridges);
19741a576772SMika Westerberg 		io = min(ALIGN(io, align), remaining_io);
19751a576772SMika Westerberg 		remaining_io -= io;
19761a576772SMika Westerberg 
19771a576772SMika Westerberg 		align = pci_resource_alignment(bridge, mmio_res);
19781a576772SMika Westerberg 		mmio = div64_ul(available_mmio, hotplug_bridges);
19791a576772SMika Westerberg 		mmio = min(ALIGN(mmio, align), remaining_mmio);
19801a576772SMika Westerberg 		remaining_mmio -= mmio;
19811a576772SMika Westerberg 
19821a576772SMika Westerberg 		align = pci_resource_alignment(bridge, mmio_pref_res);
198314fe5951SMika Westerberg 		mmio_pref = div64_ul(available_mmio_pref, hotplug_bridges);
198414fe5951SMika Westerberg 		mmio_pref = min(ALIGN(mmio_pref, align), remaining_mmio_pref);
19851a576772SMika Westerberg 		remaining_mmio_pref -= mmio_pref;
19861a576772SMika Westerberg 
198714fe5951SMika Westerberg 		pci_bus_distribute_available_resources(b, add_list, io, mmio,
198814fe5951SMika Westerberg 						       mmio_pref);
19891a576772SMika Westerberg 	}
19901a576772SMika Westerberg }
19911a576772SMika Westerberg 
19920d607618SNicholas Johnson static void pci_bridge_distribute_available_resources(struct pci_dev *bridge,
19931a576772SMika Westerberg 						     struct list_head *add_list)
19941a576772SMika Westerberg {
19951a576772SMika Westerberg 	resource_size_t available_io, available_mmio, available_mmio_pref;
19961a576772SMika Westerberg 	const struct resource *res;
19971a576772SMika Westerberg 
19981a576772SMika Westerberg 	if (!bridge->is_hotplug_bridge)
19991a576772SMika Westerberg 		return;
20001a576772SMika Westerberg 
20011a576772SMika Westerberg 	/* Take the initial extra resources from the hotplug port */
20021a576772SMika Westerberg 	res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
20031a576772SMika Westerberg 	available_io = resource_size(res);
20041a576772SMika Westerberg 	res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
20051a576772SMika Westerberg 	available_mmio = resource_size(res);
20061a576772SMika Westerberg 	res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
20071a576772SMika Westerberg 	available_mmio_pref = resource_size(res);
20081a576772SMika Westerberg 
20091a576772SMika Westerberg 	pci_bus_distribute_available_resources(bridge->subordinate,
20100d607618SNicholas Johnson 					       add_list, available_io,
20110d607618SNicholas Johnson 					       available_mmio,
20120d607618SNicholas Johnson 					       available_mmio_pref);
20131a576772SMika Westerberg }
20141a576772SMika Westerberg 
20156841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
20166841ec68SYinghai Lu {
20176841ec68SYinghai Lu 	struct pci_bus *parent = bridge->subordinate;
20180d607618SNicholas Johnson 	/* List of resources that want additional resources */
20190d607618SNicholas Johnson 	LIST_HEAD(add_list);
20200d607618SNicholas Johnson 
202132180e40SYinghai Lu 	int tried_times = 0;
2022bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
2023b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
20246841ec68SYinghai Lu 	int retval;
20256841ec68SYinghai Lu 
202632180e40SYinghai Lu again:
20278424d759SYinghai Lu 	__pci_bus_size_bridges(parent, &add_list);
20281a576772SMika Westerberg 
20291a576772SMika Westerberg 	/*
20300d607618SNicholas Johnson 	 * Distribute remaining resources (if any) equally between hotplug
20310d607618SNicholas Johnson 	 * bridges below.  This makes it possible to extend the hierarchy
20320d607618SNicholas Johnson 	 * later without running out of resources.
20331a576772SMika Westerberg 	 */
20341a576772SMika Westerberg 	pci_bridge_distribute_available_resources(bridge, &add_list);
20351a576772SMika Westerberg 
2036bdc4abecSYinghai Lu 	__pci_bridge_assign_resources(bridge, &add_list, &fail_head);
2037bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
203832180e40SYinghai Lu 	tried_times++;
203932180e40SYinghai Lu 
2040bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
20413f579c34SYinghai Lu 		goto enable_all;
204232180e40SYinghai Lu 
204332180e40SYinghai Lu 	if (tried_times >= 2) {
20440d607618SNicholas Johnson 		/* Still fail, don't need to try more */
2045bffc56d4SYinghai Lu 		free_list(&fail_head);
20463f579c34SYinghai Lu 		goto enable_all;
204732180e40SYinghai Lu 	}
204832180e40SYinghai Lu 
204932180e40SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
205032180e40SYinghai Lu 			 tried_times + 1);
205132180e40SYinghai Lu 
205232180e40SYinghai Lu 	/*
20530d607618SNicholas Johnson 	 * Try to release leaf bridge's resources that aren't big enough
20540d607618SNicholas Johnson 	 * to contain child device resources.
205532180e40SYinghai Lu 	 */
205661e83cddSYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list)
205761e83cddSYinghai Lu 		pci_bus_release_bridge_resources(fail_res->dev->bus,
2058cb21bc94SChristian König 						 fail_res->flags & PCI_RES_TYPE_MASK,
205932180e40SYinghai Lu 						 whole_subtree);
206061e83cddSYinghai Lu 
20610d607618SNicholas Johnson 	/* Restore size and flags */
2062b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
2063b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
2064*9db8dc6dSLogan Gunthorpe 		int idx;
206532180e40SYinghai Lu 
2066b9b0bba9SYinghai Lu 		res->start = fail_res->start;
2067b9b0bba9SYinghai Lu 		res->end = fail_res->end;
2068b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
2069*9db8dc6dSLogan Gunthorpe 
2070*9db8dc6dSLogan Gunthorpe 		if (pci_is_bridge(fail_res->dev)) {
2071*9db8dc6dSLogan Gunthorpe 			idx = res - &fail_res->dev->resource[0];
2072*9db8dc6dSLogan Gunthorpe 			if (idx >= PCI_BRIDGE_RESOURCES &&
2073*9db8dc6dSLogan Gunthorpe 			    idx <= PCI_BRIDGE_RESOURCE_END)
207432180e40SYinghai Lu 				res->flags = 0;
207532180e40SYinghai Lu 		}
2076*9db8dc6dSLogan Gunthorpe 	}
2077bffc56d4SYinghai Lu 	free_list(&fail_head);
207832180e40SYinghai Lu 
207932180e40SYinghai Lu 	goto again;
20803f579c34SYinghai Lu 
20813f579c34SYinghai Lu enable_all:
20823f579c34SYinghai Lu 	retval = pci_reenable_device(bridge);
20839fc9eea0SBjorn Helgaas 	if (retval)
20847506dc79SFrederick Lawler 		pci_err(bridge, "Error reenabling bridge (%d)\n", retval);
20853f579c34SYinghai Lu 	pci_set_master(bridge);
20866841ec68SYinghai Lu }
20876841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
20889b03088fSYinghai Lu 
20898bb705e3SChristian König int pci_reassign_bridge_resources(struct pci_dev *bridge, unsigned long type)
20908bb705e3SChristian König {
20918bb705e3SChristian König 	struct pci_dev_resource *dev_res;
20928bb705e3SChristian König 	struct pci_dev *next;
20938bb705e3SChristian König 	LIST_HEAD(saved);
20948bb705e3SChristian König 	LIST_HEAD(added);
20958bb705e3SChristian König 	LIST_HEAD(failed);
20968bb705e3SChristian König 	unsigned int i;
20978bb705e3SChristian König 	int ret;
20988bb705e3SChristian König 
2099fb794a70SBenjamin Herrenschmidt 	down_read(&pci_bus_sem);
2100fb794a70SBenjamin Herrenschmidt 
21018bb705e3SChristian König 	/* Walk to the root hub, releasing bridge BARs when possible */
21028bb705e3SChristian König 	next = bridge;
21038bb705e3SChristian König 	do {
21048bb705e3SChristian König 		bridge = next;
21058bb705e3SChristian König 		for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCE_END;
21068bb705e3SChristian König 		     i++) {
21078bb705e3SChristian König 			struct resource *res = &bridge->resource[i];
21088bb705e3SChristian König 
21098bb705e3SChristian König 			if ((res->flags ^ type) & PCI_RES_TYPE_MASK)
21108bb705e3SChristian König 				continue;
21118bb705e3SChristian König 
21128bb705e3SChristian König 			/* Ignore BARs which are still in use */
21138bb705e3SChristian König 			if (res->child)
21148bb705e3SChristian König 				continue;
21158bb705e3SChristian König 
21168bb705e3SChristian König 			ret = add_to_list(&saved, bridge, res, 0, 0);
21178bb705e3SChristian König 			if (ret)
21188bb705e3SChristian König 				goto cleanup;
21198bb705e3SChristian König 
21207506dc79SFrederick Lawler 			pci_info(bridge, "BAR %d: releasing %pR\n",
21218bb705e3SChristian König 				 i, res);
21228bb705e3SChristian König 
21238bb705e3SChristian König 			if (res->parent)
21248bb705e3SChristian König 				release_resource(res);
21258bb705e3SChristian König 			res->start = 0;
21268bb705e3SChristian König 			res->end = 0;
21278bb705e3SChristian König 			break;
21288bb705e3SChristian König 		}
21298bb705e3SChristian König 		if (i == PCI_BRIDGE_RESOURCE_END)
21308bb705e3SChristian König 			break;
21318bb705e3SChristian König 
21328bb705e3SChristian König 		next = bridge->bus ? bridge->bus->self : NULL;
21338bb705e3SChristian König 	} while (next);
21348bb705e3SChristian König 
2135fb794a70SBenjamin Herrenschmidt 	if (list_empty(&saved)) {
2136fb794a70SBenjamin Herrenschmidt 		up_read(&pci_bus_sem);
21378bb705e3SChristian König 		return -ENOENT;
2138fb794a70SBenjamin Herrenschmidt 	}
21398bb705e3SChristian König 
21408bb705e3SChristian König 	__pci_bus_size_bridges(bridge->subordinate, &added);
21418bb705e3SChristian König 	__pci_bridge_assign_resources(bridge, &added, &failed);
21428bb705e3SChristian König 	BUG_ON(!list_empty(&added));
21438bb705e3SChristian König 
21448bb705e3SChristian König 	if (!list_empty(&failed)) {
21458bb705e3SChristian König 		ret = -ENOSPC;
21468bb705e3SChristian König 		goto cleanup;
21478bb705e3SChristian König 	}
21488bb705e3SChristian König 
21498bb705e3SChristian König 	list_for_each_entry(dev_res, &saved, list) {
21500d607618SNicholas Johnson 		/* Skip the bridge we just assigned resources for */
21518bb705e3SChristian König 		if (bridge == dev_res->dev)
21528bb705e3SChristian König 			continue;
21538bb705e3SChristian König 
21548bb705e3SChristian König 		bridge = dev_res->dev;
21558bb705e3SChristian König 		pci_setup_bridge(bridge->subordinate);
21568bb705e3SChristian König 	}
21578bb705e3SChristian König 
21588bb705e3SChristian König 	free_list(&saved);
2159fb794a70SBenjamin Herrenschmidt 	up_read(&pci_bus_sem);
21608bb705e3SChristian König 	return 0;
21618bb705e3SChristian König 
21628bb705e3SChristian König cleanup:
21630d607618SNicholas Johnson 	/* Restore size and flags */
21648bb705e3SChristian König 	list_for_each_entry(dev_res, &failed, list) {
21658bb705e3SChristian König 		struct resource *res = dev_res->res;
21668bb705e3SChristian König 
21678bb705e3SChristian König 		res->start = dev_res->start;
21688bb705e3SChristian König 		res->end = dev_res->end;
21698bb705e3SChristian König 		res->flags = dev_res->flags;
21708bb705e3SChristian König 	}
21718bb705e3SChristian König 	free_list(&failed);
21728bb705e3SChristian König 
21738bb705e3SChristian König 	/* Revert to the old configuration */
21748bb705e3SChristian König 	list_for_each_entry(dev_res, &saved, list) {
21758bb705e3SChristian König 		struct resource *res = dev_res->res;
21768bb705e3SChristian König 
21778bb705e3SChristian König 		bridge = dev_res->dev;
21788bb705e3SChristian König 		i = res - bridge->resource;
21798bb705e3SChristian König 
21808bb705e3SChristian König 		res->start = dev_res->start;
21818bb705e3SChristian König 		res->end = dev_res->end;
21828bb705e3SChristian König 		res->flags = dev_res->flags;
21838bb705e3SChristian König 
21848bb705e3SChristian König 		pci_claim_resource(bridge, i);
21858bb705e3SChristian König 		pci_setup_bridge(bridge->subordinate);
21868bb705e3SChristian König 	}
21878bb705e3SChristian König 	free_list(&saved);
2188fb794a70SBenjamin Herrenschmidt 	up_read(&pci_bus_sem);
21898bb705e3SChristian König 
21908bb705e3SChristian König 	return ret;
21918bb705e3SChristian König }
21928bb705e3SChristian König 
219317787940SYinghai Lu void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
21949b03088fSYinghai Lu {
21959b03088fSYinghai Lu 	struct pci_dev *dev;
21960d607618SNicholas Johnson 	/* List of resources that want additional resources */
21970d607618SNicholas Johnson 	LIST_HEAD(add_list);
21989b03088fSYinghai Lu 
21999b03088fSYinghai Lu 	down_read(&pci_bus_sem);
220024a0c654SAndy Shevchenko 	for_each_pci_bridge(dev, bus)
220124a0c654SAndy Shevchenko 		if (pci_has_subordinate(dev))
220224a0c654SAndy Shevchenko 			__pci_bus_size_bridges(dev->subordinate, &add_list);
22039b03088fSYinghai Lu 	up_read(&pci_bus_sem);
22049b03088fSYinghai Lu 	__pci_bus_assign_resources(bus, &add_list, NULL);
2205bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
220617787940SYinghai Lu }
2207e6b29deaSRay Jui EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);
2208