xref: /openbmc/linux/drivers/pci/setup-bus.c (revision a6b65983dabceb7ccb1801ee7f5bd421c2704d16)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	drivers/pci/setup-bus.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Extruded from code written by
51da177e4SLinus Torvalds  *      Dave Rusling (david.rusling@reo.mts.dec.com)
61da177e4SLinus Torvalds  *      David Mosberger (davidm@cs.arizona.edu)
71da177e4SLinus Torvalds  *	David Miller (davem@redhat.com)
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Support routines for initializing a PCI subsystem.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds /*
131da177e4SLinus Torvalds  * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
141da177e4SLinus Torvalds  *	     PCI-PCI bridges cleanup, sorted resource allocation.
151da177e4SLinus Torvalds  * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
161da177e4SLinus Torvalds  *	     Converted to allocation in 3 passes, which gives
171da177e4SLinus Torvalds  *	     tighter packing. Prefetchable range support.
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/init.h>
211da177e4SLinus Torvalds #include <linux/kernel.h>
221da177e4SLinus Torvalds #include <linux/module.h>
231da177e4SLinus Torvalds #include <linux/pci.h>
241da177e4SLinus Torvalds #include <linux/errno.h>
251da177e4SLinus Torvalds #include <linux/ioport.h>
261da177e4SLinus Torvalds #include <linux/cache.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
2847087700SBjorn Helgaas #include <asm-generic/pci-bridge.h>
296faf17f6SChris Wright #include "pci.h"
301da177e4SLinus Torvalds 
31844393f4SBjorn Helgaas unsigned int pci_flags;
3247087700SBjorn Helgaas 
33bdc4abecSYinghai Lu struct pci_dev_resource {
34bdc4abecSYinghai Lu 	struct list_head list;
352934a0deSYinghai Lu 	struct resource *res;
362934a0deSYinghai Lu 	struct pci_dev *dev;
37568ddef8SYinghai Lu 	resource_size_t start;
38568ddef8SYinghai Lu 	resource_size_t end;
39c8adf9a3SRam Pai 	resource_size_t add_size;
402bbc6942SRam Pai 	resource_size_t min_align;
41568ddef8SYinghai Lu 	unsigned long flags;
42568ddef8SYinghai Lu };
43568ddef8SYinghai Lu 
44bffc56d4SYinghai Lu static void free_list(struct list_head *head)
45bffc56d4SYinghai Lu {
46bffc56d4SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
47bffc56d4SYinghai Lu 
48bffc56d4SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
49bffc56d4SYinghai Lu 		list_del(&dev_res->list);
50bffc56d4SYinghai Lu 		kfree(dev_res);
51bffc56d4SYinghai Lu 	}
52bffc56d4SYinghai Lu }
53094732a5SRam Pai 
54c8adf9a3SRam Pai /**
55c8adf9a3SRam Pai  * add_to_list() - add a new resource tracker to the list
56c8adf9a3SRam Pai  * @head:	Head of the list
57c8adf9a3SRam Pai  * @dev:	device corresponding to which the resource
58c8adf9a3SRam Pai  *		belongs
59c8adf9a3SRam Pai  * @res:	The resource to be tracked
60c8adf9a3SRam Pai  * @add_size:	additional size to be optionally added
61c8adf9a3SRam Pai  *              to the resource
62c8adf9a3SRam Pai  */
63bdc4abecSYinghai Lu static int add_to_list(struct list_head *head,
64c8adf9a3SRam Pai 		 struct pci_dev *dev, struct resource *res,
652bbc6942SRam Pai 		 resource_size_t add_size, resource_size_t min_align)
66568ddef8SYinghai Lu {
67764242a0SYinghai Lu 	struct pci_dev_resource *tmp;
68568ddef8SYinghai Lu 
69bdc4abecSYinghai Lu 	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
70568ddef8SYinghai Lu 	if (!tmp) {
713c78bc61SRyan Desfosses 		pr_warn("add_to_list: kmalloc() failed!\n");
72ef62dfefSYinghai Lu 		return -ENOMEM;
73568ddef8SYinghai Lu 	}
74568ddef8SYinghai Lu 
75568ddef8SYinghai Lu 	tmp->res = res;
76568ddef8SYinghai Lu 	tmp->dev = dev;
77568ddef8SYinghai Lu 	tmp->start = res->start;
78568ddef8SYinghai Lu 	tmp->end = res->end;
79568ddef8SYinghai Lu 	tmp->flags = res->flags;
80c8adf9a3SRam Pai 	tmp->add_size = add_size;
812bbc6942SRam Pai 	tmp->min_align = min_align;
82bdc4abecSYinghai Lu 
83bdc4abecSYinghai Lu 	list_add(&tmp->list, head);
84ef62dfefSYinghai Lu 
85ef62dfefSYinghai Lu 	return 0;
86568ddef8SYinghai Lu }
87568ddef8SYinghai Lu 
88b9b0bba9SYinghai Lu static void remove_from_list(struct list_head *head,
893e6e0d80SYinghai Lu 				 struct resource *res)
903e6e0d80SYinghai Lu {
91b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
923e6e0d80SYinghai Lu 
93b9b0bba9SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
94b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
95b9b0bba9SYinghai Lu 			list_del(&dev_res->list);
96b9b0bba9SYinghai Lu 			kfree(dev_res);
97bdc4abecSYinghai Lu 			break;
983e6e0d80SYinghai Lu 		}
993e6e0d80SYinghai Lu 	}
1003e6e0d80SYinghai Lu }
1013e6e0d80SYinghai Lu 
102d74b9027SWei Yang static struct pci_dev_resource *res_to_dev_res(struct list_head *head,
1031c372353SYinghai Lu 					       struct resource *res)
1041c372353SYinghai Lu {
105b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res;
1061c372353SYinghai Lu 
107b9b0bba9SYinghai Lu 	list_for_each_entry(dev_res, head, list) {
108b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
109b592443dSYinghai Lu 			int idx = res - &dev_res->dev->resource[0];
110b592443dSYinghai Lu 
111b9b0bba9SYinghai Lu 			dev_printk(KERN_DEBUG, &dev_res->dev->dev,
112d74b9027SWei Yang 				 "res[%d]=%pR res_to_dev_res add_size %llx min_align %llx\n",
113b592443dSYinghai Lu 				 idx, dev_res->res,
114d74b9027SWei Yang 				 (unsigned long long)dev_res->add_size,
115d74b9027SWei Yang 				 (unsigned long long)dev_res->min_align);
116b592443dSYinghai Lu 
117d74b9027SWei Yang 			return dev_res;
118bdc4abecSYinghai Lu 		}
1193e6e0d80SYinghai Lu 	}
1201c372353SYinghai Lu 
121d74b9027SWei Yang 	return NULL;
1221c372353SYinghai Lu }
1231c372353SYinghai Lu 
124d74b9027SWei Yang static resource_size_t get_res_add_size(struct list_head *head,
125d74b9027SWei Yang 					struct resource *res)
126d74b9027SWei Yang {
127d74b9027SWei Yang 	struct pci_dev_resource *dev_res;
128d74b9027SWei Yang 
129d74b9027SWei Yang 	dev_res = res_to_dev_res(head, res);
130d74b9027SWei Yang 	return dev_res ? dev_res->add_size : 0;
131d74b9027SWei Yang }
132d74b9027SWei Yang 
133d74b9027SWei Yang static resource_size_t get_res_add_align(struct list_head *head,
134d74b9027SWei Yang 					 struct resource *res)
135d74b9027SWei Yang {
136d74b9027SWei Yang 	struct pci_dev_resource *dev_res;
137d74b9027SWei Yang 
138d74b9027SWei Yang 	dev_res = res_to_dev_res(head, res);
139d74b9027SWei Yang 	return dev_res ? dev_res->min_align : 0;
140d74b9027SWei Yang }
141d74b9027SWei Yang 
142d74b9027SWei Yang 
14378c3b329SYinghai Lu /* Sort resources by alignment */
144bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
14578c3b329SYinghai Lu {
14678c3b329SYinghai Lu 	int i;
14778c3b329SYinghai Lu 
14878c3b329SYinghai Lu 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
14978c3b329SYinghai Lu 		struct resource *r;
150bdc4abecSYinghai Lu 		struct pci_dev_resource *dev_res, *tmp;
15178c3b329SYinghai Lu 		resource_size_t r_align;
152bdc4abecSYinghai Lu 		struct list_head *n;
15378c3b329SYinghai Lu 
15478c3b329SYinghai Lu 		r = &dev->resource[i];
15578c3b329SYinghai Lu 
15678c3b329SYinghai Lu 		if (r->flags & IORESOURCE_PCI_FIXED)
15778c3b329SYinghai Lu 			continue;
15878c3b329SYinghai Lu 
15978c3b329SYinghai Lu 		if (!(r->flags) || r->parent)
16078c3b329SYinghai Lu 			continue;
16178c3b329SYinghai Lu 
16278c3b329SYinghai Lu 		r_align = pci_resource_alignment(dev, r);
16378c3b329SYinghai Lu 		if (!r_align) {
16478c3b329SYinghai Lu 			dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
16578c3b329SYinghai Lu 				 i, r);
16678c3b329SYinghai Lu 			continue;
16778c3b329SYinghai Lu 		}
16878c3b329SYinghai Lu 
169bdc4abecSYinghai Lu 		tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
17078c3b329SYinghai Lu 		if (!tmp)
171227f0647SRyan Desfosses 			panic("pdev_sort_resources(): kmalloc() failed!\n");
17278c3b329SYinghai Lu 		tmp->res = r;
17378c3b329SYinghai Lu 		tmp->dev = dev;
174bdc4abecSYinghai Lu 
175bdc4abecSYinghai Lu 		/* fallback is smallest one or list is empty*/
176bdc4abecSYinghai Lu 		n = head;
177bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
178bdc4abecSYinghai Lu 			resource_size_t align;
179bdc4abecSYinghai Lu 
180bdc4abecSYinghai Lu 			align = pci_resource_alignment(dev_res->dev,
181bdc4abecSYinghai Lu 							 dev_res->res);
182bdc4abecSYinghai Lu 
183bdc4abecSYinghai Lu 			if (r_align > align) {
184bdc4abecSYinghai Lu 				n = &dev_res->list;
18578c3b329SYinghai Lu 				break;
18678c3b329SYinghai Lu 			}
18778c3b329SYinghai Lu 		}
188bdc4abecSYinghai Lu 		/* Insert it just before n*/
189bdc4abecSYinghai Lu 		list_add_tail(&tmp->list, n);
19078c3b329SYinghai Lu 	}
19178c3b329SYinghai Lu }
19278c3b329SYinghai Lu 
1936841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev,
194bdc4abecSYinghai Lu 				 struct list_head *head)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	u16 class = dev->class >> 8;
1971da177e4SLinus Torvalds 
1989bded00bSKenji Kaneshige 	/* Don't touch classless devices or host bridges or ioapics.  */
1996841ec68SYinghai Lu 	if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
2006841ec68SYinghai Lu 		return;
2011da177e4SLinus Torvalds 
2029bded00bSKenji Kaneshige 	/* Don't touch ioapic devices already enabled by firmware */
20323186279SSatoru Takeuchi 	if (class == PCI_CLASS_SYSTEM_PIC) {
2049bded00bSKenji Kaneshige 		u16 command;
2059bded00bSKenji Kaneshige 		pci_read_config_word(dev, PCI_COMMAND, &command);
2069bded00bSKenji Kaneshige 		if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
2076841ec68SYinghai Lu 			return;
20823186279SSatoru Takeuchi 	}
20923186279SSatoru Takeuchi 
2106841ec68SYinghai Lu 	pdev_sort_resources(dev, head);
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
213fc075e1dSRam Pai static inline void reset_resource(struct resource *res)
214fc075e1dSRam Pai {
215fc075e1dSRam Pai 	res->start = 0;
216fc075e1dSRam Pai 	res->end = 0;
217fc075e1dSRam Pai 	res->flags = 0;
218fc075e1dSRam Pai }
219fc075e1dSRam Pai 
220c8adf9a3SRam Pai /**
2219e8bf93aSRam Pai  * reassign_resources_sorted() - satisfy any additional resource requests
222c8adf9a3SRam Pai  *
2239e8bf93aSRam Pai  * @realloc_head : head of the list tracking requests requiring additional
224c8adf9a3SRam Pai  *             resources
225c8adf9a3SRam Pai  * @head     : head of the list tracking requests with allocated
226c8adf9a3SRam Pai  *             resources
227c8adf9a3SRam Pai  *
2289e8bf93aSRam Pai  * Walk through each element of the realloc_head and try to procure
229c8adf9a3SRam Pai  * additional resources for the element, provided the element
230c8adf9a3SRam Pai  * is in the head list.
231c8adf9a3SRam Pai  */
232bdc4abecSYinghai Lu static void reassign_resources_sorted(struct list_head *realloc_head,
233bdc4abecSYinghai Lu 		struct list_head *head)
234c8adf9a3SRam Pai {
235c8adf9a3SRam Pai 	struct resource *res;
236b9b0bba9SYinghai Lu 	struct pci_dev_resource *add_res, *tmp;
237bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
238d74b9027SWei Yang 	resource_size_t add_size, align;
239c8adf9a3SRam Pai 	int idx;
240c8adf9a3SRam Pai 
241b9b0bba9SYinghai Lu 	list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
242bdc4abecSYinghai Lu 		bool found_match = false;
243bdc4abecSYinghai Lu 
244b9b0bba9SYinghai Lu 		res = add_res->res;
245c8adf9a3SRam Pai 		/* skip resource that has been reset */
246c8adf9a3SRam Pai 		if (!res->flags)
247c8adf9a3SRam Pai 			goto out;
248c8adf9a3SRam Pai 
249c8adf9a3SRam Pai 		/* skip this resource if not found in head list */
250bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
251bdc4abecSYinghai Lu 			if (dev_res->res == res) {
252bdc4abecSYinghai Lu 				found_match = true;
253bdc4abecSYinghai Lu 				break;
254c8adf9a3SRam Pai 			}
255bdc4abecSYinghai Lu 		}
256bdc4abecSYinghai Lu 		if (!found_match)/* just skip */
257bdc4abecSYinghai Lu 			continue;
258c8adf9a3SRam Pai 
259b9b0bba9SYinghai Lu 		idx = res - &add_res->dev->resource[0];
260b9b0bba9SYinghai Lu 		add_size = add_res->add_size;
261d74b9027SWei Yang 		align = add_res->min_align;
2622bbc6942SRam Pai 		if (!resource_size(res)) {
263d74b9027SWei Yang 			res->start = align;
264c8adf9a3SRam Pai 			res->end = res->start + add_size - 1;
265b9b0bba9SYinghai Lu 			if (pci_assign_resource(add_res->dev, idx))
266c8adf9a3SRam Pai 				reset_resource(res);
2672bbc6942SRam Pai 		} else {
268b9b0bba9SYinghai Lu 			res->flags |= add_res->flags &
269bdc4abecSYinghai Lu 				 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
270b9b0bba9SYinghai Lu 			if (pci_reassign_resource(add_res->dev, idx,
271bdc4abecSYinghai Lu 						  add_size, align))
272b9b0bba9SYinghai Lu 				dev_printk(KERN_DEBUG, &add_res->dev->dev,
273b592443dSYinghai Lu 					   "failed to add %llx res[%d]=%pR\n",
274b592443dSYinghai Lu 					   (unsigned long long)add_size,
275b592443dSYinghai Lu 					   idx, res);
276c8adf9a3SRam Pai 		}
277c8adf9a3SRam Pai out:
278b9b0bba9SYinghai Lu 		list_del(&add_res->list);
279b9b0bba9SYinghai Lu 		kfree(add_res);
280c8adf9a3SRam Pai 	}
281c8adf9a3SRam Pai }
282c8adf9a3SRam Pai 
283c8adf9a3SRam Pai /**
284c8adf9a3SRam Pai  * assign_requested_resources_sorted() - satisfy resource requests
285c8adf9a3SRam Pai  *
286c8adf9a3SRam Pai  * @head : head of the list tracking requests for resources
2878356aad4SWanpeng Li  * @fail_head : head of the list tracking requests that could
288c8adf9a3SRam Pai  *		not be allocated
289c8adf9a3SRam Pai  *
290c8adf9a3SRam Pai  * Satisfy resource requests of each element in the list. Add
291c8adf9a3SRam Pai  * requests that could not satisfied to the failed_list.
292c8adf9a3SRam Pai  */
293bdc4abecSYinghai Lu static void assign_requested_resources_sorted(struct list_head *head,
294bdc4abecSYinghai Lu 				 struct list_head *fail_head)
2956841ec68SYinghai Lu {
2966841ec68SYinghai Lu 	struct resource *res;
297bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
2986841ec68SYinghai Lu 	int idx;
2996841ec68SYinghai Lu 
300bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
301bdc4abecSYinghai Lu 		res = dev_res->res;
302bdc4abecSYinghai Lu 		idx = res - &dev_res->dev->resource[0];
303bdc4abecSYinghai Lu 		if (resource_size(res) &&
304bdc4abecSYinghai Lu 		    pci_assign_resource(dev_res->dev, idx)) {
305a3cb999dSYinghai Lu 			if (fail_head) {
3069a928660SYinghai Lu 				/*
3079a928660SYinghai Lu 				 * if the failed res is for ROM BAR, and it will
3089a928660SYinghai Lu 				 * be enabled later, don't add it to the list
3099a928660SYinghai Lu 				 */
3109a928660SYinghai Lu 				if (!((idx == PCI_ROM_RESOURCE) &&
3119a928660SYinghai Lu 				      (!(res->flags & IORESOURCE_ROM_ENABLE))))
31267cc7e26SYinghai Lu 					add_to_list(fail_head,
31367cc7e26SYinghai Lu 						    dev_res->dev, res,
314f7625980SBjorn Helgaas 						    0 /* don't care */,
315f7625980SBjorn Helgaas 						    0 /* don't care */);
3169a928660SYinghai Lu 			}
317fc075e1dSRam Pai 			reset_resource(res);
318542df5deSRajesh Shah 		}
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
322aa914f5eSYinghai Lu static unsigned long pci_fail_res_type_mask(struct list_head *fail_head)
323aa914f5eSYinghai Lu {
324aa914f5eSYinghai Lu 	struct pci_dev_resource *fail_res;
325aa914f5eSYinghai Lu 	unsigned long mask = 0;
326aa914f5eSYinghai Lu 
327aa914f5eSYinghai Lu 	/* check failed type */
328aa914f5eSYinghai Lu 	list_for_each_entry(fail_res, fail_head, list)
329aa914f5eSYinghai Lu 		mask |= fail_res->flags;
330aa914f5eSYinghai Lu 
331aa914f5eSYinghai Lu 	/*
332aa914f5eSYinghai Lu 	 * one pref failed resource will set IORESOURCE_MEM,
333aa914f5eSYinghai Lu 	 * as we can allocate pref in non-pref range.
334aa914f5eSYinghai Lu 	 * Will release all assigned non-pref sibling resources
335aa914f5eSYinghai Lu 	 * according to that bit.
336aa914f5eSYinghai Lu 	 */
337aa914f5eSYinghai Lu 	return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
338aa914f5eSYinghai Lu }
339aa914f5eSYinghai Lu 
340aa914f5eSYinghai Lu static bool pci_need_to_release(unsigned long mask, struct resource *res)
341aa914f5eSYinghai Lu {
342aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_IO)
343aa914f5eSYinghai Lu 		return !!(mask & IORESOURCE_IO);
344aa914f5eSYinghai Lu 
345aa914f5eSYinghai Lu 	/* check pref at first */
346aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_PREFETCH) {
347aa914f5eSYinghai Lu 		if (mask & IORESOURCE_PREFETCH)
348aa914f5eSYinghai Lu 			return true;
349aa914f5eSYinghai Lu 		/* count pref if its parent is non-pref */
350aa914f5eSYinghai Lu 		else if ((mask & IORESOURCE_MEM) &&
351aa914f5eSYinghai Lu 			 !(res->parent->flags & IORESOURCE_PREFETCH))
352aa914f5eSYinghai Lu 			return true;
353aa914f5eSYinghai Lu 		else
354aa914f5eSYinghai Lu 			return false;
355aa914f5eSYinghai Lu 	}
356aa914f5eSYinghai Lu 
357aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_MEM)
358aa914f5eSYinghai Lu 		return !!(mask & IORESOURCE_MEM);
359aa914f5eSYinghai Lu 
360aa914f5eSYinghai Lu 	return false;	/* should not get here */
361aa914f5eSYinghai Lu }
362aa914f5eSYinghai Lu 
363bdc4abecSYinghai Lu static void __assign_resources_sorted(struct list_head *head,
364bdc4abecSYinghai Lu 				 struct list_head *realloc_head,
365bdc4abecSYinghai Lu 				 struct list_head *fail_head)
366c8adf9a3SRam Pai {
3673e6e0d80SYinghai Lu 	/*
3683e6e0d80SYinghai Lu 	 * Should not assign requested resources at first.
3693e6e0d80SYinghai Lu 	 *   they could be adjacent, so later reassign can not reallocate
3703e6e0d80SYinghai Lu 	 *   them one by one in parent resource window.
371367fa982SMasanari Iida 	 * Try to assign requested + add_size at beginning
3723e6e0d80SYinghai Lu 	 *  if could do that, could get out early.
3733e6e0d80SYinghai Lu 	 *  if could not do that, we still try to assign requested at first,
3743e6e0d80SYinghai Lu 	 *    then try to reassign add_size for some resources.
375aa914f5eSYinghai Lu 	 *
376aa914f5eSYinghai Lu 	 * Separate three resource type checking if we need to release
377aa914f5eSYinghai Lu 	 * assigned resource after requested + add_size try.
378aa914f5eSYinghai Lu 	 *	1. if there is io port assign fail, will release assigned
379aa914f5eSYinghai Lu 	 *	   io port.
380aa914f5eSYinghai Lu 	 *	2. if there is pref mmio assign fail, release assigned
381aa914f5eSYinghai Lu 	 *	   pref mmio.
382aa914f5eSYinghai Lu 	 *	   if assigned pref mmio's parent is non-pref mmio and there
383aa914f5eSYinghai Lu 	 *	   is non-pref mmio assign fail, will release that assigned
384aa914f5eSYinghai Lu 	 *	   pref mmio.
385aa914f5eSYinghai Lu 	 *	3. if there is non-pref mmio assign fail or pref mmio
386aa914f5eSYinghai Lu 	 *	   assigned fail, will release assigned non-pref mmio.
3873e6e0d80SYinghai Lu 	 */
388bdc4abecSYinghai Lu 	LIST_HEAD(save_head);
389bdc4abecSYinghai Lu 	LIST_HEAD(local_fail_head);
390b9b0bba9SYinghai Lu 	struct pci_dev_resource *save_res;
391d74b9027SWei Yang 	struct pci_dev_resource *dev_res, *tmp_res, *dev_res2;
392aa914f5eSYinghai Lu 	unsigned long fail_type;
393d74b9027SWei Yang 	resource_size_t add_align, align;
3943e6e0d80SYinghai Lu 
3953e6e0d80SYinghai Lu 	/* Check if optional add_size is there */
396bdc4abecSYinghai Lu 	if (!realloc_head || list_empty(realloc_head))
3973e6e0d80SYinghai Lu 		goto requested_and_reassign;
3983e6e0d80SYinghai Lu 
3993e6e0d80SYinghai Lu 	/* Save original start, end, flags etc at first */
400bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
401bdc4abecSYinghai Lu 		if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
402bffc56d4SYinghai Lu 			free_list(&save_head);
4033e6e0d80SYinghai Lu 			goto requested_and_reassign;
4043e6e0d80SYinghai Lu 		}
405bdc4abecSYinghai Lu 	}
4063e6e0d80SYinghai Lu 
4073e6e0d80SYinghai Lu 	/* Update res in head list with add_size in realloc_head list */
408d74b9027SWei Yang 	list_for_each_entry_safe(dev_res, tmp_res, head, list) {
409bdc4abecSYinghai Lu 		dev_res->res->end += get_res_add_size(realloc_head,
410bdc4abecSYinghai Lu 							dev_res->res);
4113e6e0d80SYinghai Lu 
412d74b9027SWei Yang 		/*
413d74b9027SWei Yang 		 * There are two kinds of additional resources in the list:
414d74b9027SWei Yang 		 * 1. bridge resource  -- IORESOURCE_STARTALIGN
415d74b9027SWei Yang 		 * 2. SR-IOV resource   -- IORESOURCE_SIZEALIGN
416d74b9027SWei Yang 		 * Here just fix the additional alignment for bridge
417d74b9027SWei Yang 		 */
418d74b9027SWei Yang 		if (!(dev_res->res->flags & IORESOURCE_STARTALIGN))
419d74b9027SWei Yang 			continue;
420d74b9027SWei Yang 
421d74b9027SWei Yang 		add_align = get_res_add_align(realloc_head, dev_res->res);
422d74b9027SWei Yang 
423d74b9027SWei Yang 		/*
424d74b9027SWei Yang 		 * The "head" list is sorted by the alignment to make sure
425d74b9027SWei Yang 		 * resources with bigger alignment will be assigned first.
426d74b9027SWei Yang 		 * After we change the alignment of a dev_res in "head" list,
427d74b9027SWei Yang 		 * we need to reorder the list by alignment to make it
428d74b9027SWei Yang 		 * consistent.
429d74b9027SWei Yang 		 */
430d74b9027SWei Yang 		if (add_align > dev_res->res->start) {
431d74b9027SWei Yang 			dev_res->res->start = add_align;
432d74b9027SWei Yang 			dev_res->res->end = add_align +
433d74b9027SWei Yang 				            resource_size(dev_res->res);
434d74b9027SWei Yang 
435d74b9027SWei Yang 			list_for_each_entry(dev_res2, head, list) {
436d74b9027SWei Yang 				align = pci_resource_alignment(dev_res2->dev,
437d74b9027SWei Yang 							       dev_res2->res);
438*a6b65983SWei Yang 				if (add_align > align) {
439d74b9027SWei Yang 					list_move_tail(&dev_res->list,
440d74b9027SWei Yang 						       &dev_res2->list);
441*a6b65983SWei Yang 					break;
442*a6b65983SWei Yang 				}
443d74b9027SWei Yang 			}
444d74b9027SWei Yang                }
445d74b9027SWei Yang 
446d74b9027SWei Yang 	}
447d74b9027SWei Yang 
4483e6e0d80SYinghai Lu 	/* Try updated head list with add_size added */
4493e6e0d80SYinghai Lu 	assign_requested_resources_sorted(head, &local_fail_head);
4503e6e0d80SYinghai Lu 
4513e6e0d80SYinghai Lu 	/* all assigned with add_size ? */
452bdc4abecSYinghai Lu 	if (list_empty(&local_fail_head)) {
4533e6e0d80SYinghai Lu 		/* Remove head list from realloc_head list */
454bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list)
455bdc4abecSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
456bffc56d4SYinghai Lu 		free_list(&save_head);
457bffc56d4SYinghai Lu 		free_list(head);
4583e6e0d80SYinghai Lu 		return;
4593e6e0d80SYinghai Lu 	}
4603e6e0d80SYinghai Lu 
461aa914f5eSYinghai Lu 	/* check failed type */
462aa914f5eSYinghai Lu 	fail_type = pci_fail_res_type_mask(&local_fail_head);
463aa914f5eSYinghai Lu 	/* remove not need to be released assigned res from head list etc */
464aa914f5eSYinghai Lu 	list_for_each_entry_safe(dev_res, tmp_res, head, list)
465aa914f5eSYinghai Lu 		if (dev_res->res->parent &&
466aa914f5eSYinghai Lu 		    !pci_need_to_release(fail_type, dev_res->res)) {
467aa914f5eSYinghai Lu 			/* remove it from realloc_head list */
468aa914f5eSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
469aa914f5eSYinghai Lu 			remove_from_list(&save_head, dev_res->res);
470aa914f5eSYinghai Lu 			list_del(&dev_res->list);
471aa914f5eSYinghai Lu 			kfree(dev_res);
472aa914f5eSYinghai Lu 		}
473aa914f5eSYinghai Lu 
474bffc56d4SYinghai Lu 	free_list(&local_fail_head);
4753e6e0d80SYinghai Lu 	/* Release assigned resource */
476bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list)
477bdc4abecSYinghai Lu 		if (dev_res->res->parent)
478bdc4abecSYinghai Lu 			release_resource(dev_res->res);
4793e6e0d80SYinghai Lu 	/* Restore start/end/flags from saved list */
480b9b0bba9SYinghai Lu 	list_for_each_entry(save_res, &save_head, list) {
481b9b0bba9SYinghai Lu 		struct resource *res = save_res->res;
4823e6e0d80SYinghai Lu 
483b9b0bba9SYinghai Lu 		res->start = save_res->start;
484b9b0bba9SYinghai Lu 		res->end = save_res->end;
485b9b0bba9SYinghai Lu 		res->flags = save_res->flags;
4863e6e0d80SYinghai Lu 	}
487bffc56d4SYinghai Lu 	free_list(&save_head);
4883e6e0d80SYinghai Lu 
4893e6e0d80SYinghai Lu requested_and_reassign:
490c8adf9a3SRam Pai 	/* Satisfy the must-have resource requests */
491c8adf9a3SRam Pai 	assign_requested_resources_sorted(head, fail_head);
492c8adf9a3SRam Pai 
4930a2daa1cSRam Pai 	/* Try to satisfy any additional optional resource
494c8adf9a3SRam Pai 		requests */
4959e8bf93aSRam Pai 	if (realloc_head)
4969e8bf93aSRam Pai 		reassign_resources_sorted(realloc_head, head);
497bffc56d4SYinghai Lu 	free_list(head);
498c8adf9a3SRam Pai }
499c8adf9a3SRam Pai 
5006841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev,
501bdc4abecSYinghai Lu 				 struct list_head *add_head,
502bdc4abecSYinghai Lu 				 struct list_head *fail_head)
5036841ec68SYinghai Lu {
504bdc4abecSYinghai Lu 	LIST_HEAD(head);
5056841ec68SYinghai Lu 
5066841ec68SYinghai Lu 	__dev_sort_resources(dev, &head);
5078424d759SYinghai Lu 	__assign_resources_sorted(&head, add_head, fail_head);
5086841ec68SYinghai Lu 
5096841ec68SYinghai Lu }
5106841ec68SYinghai Lu 
5116841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus,
512bdc4abecSYinghai Lu 					 struct list_head *realloc_head,
513bdc4abecSYinghai Lu 					 struct list_head *fail_head)
5146841ec68SYinghai Lu {
5156841ec68SYinghai Lu 	struct pci_dev *dev;
516bdc4abecSYinghai Lu 	LIST_HEAD(head);
5176841ec68SYinghai Lu 
5186841ec68SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
5196841ec68SYinghai Lu 		__dev_sort_resources(dev, &head);
5206841ec68SYinghai Lu 
5219e8bf93aSRam Pai 	__assign_resources_sorted(&head, realloc_head, fail_head);
5226841ec68SYinghai Lu }
5236841ec68SYinghai Lu 
524b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus)
5251da177e4SLinus Torvalds {
5261da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
527c7dabef8SBjorn Helgaas 	struct resource *res;
5281da177e4SLinus Torvalds 	struct pci_bus_region region;
5291da177e4SLinus Torvalds 
530b918c62eSYinghai Lu 	dev_info(&bridge->dev, "CardBus bridge to %pR\n",
531b918c62eSYinghai Lu 		 &bus->busn_res);
5321da177e4SLinus Torvalds 
533c7dabef8SBjorn Helgaas 	res = bus->resource[0];
534fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
535c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5361da177e4SLinus Torvalds 		/*
5371da177e4SLinus Torvalds 		 * The IO resource is allocated a range twice as large as it
5381da177e4SLinus Torvalds 		 * would normally need.  This allows us to set both IO regs.
5391da177e4SLinus Torvalds 		 */
540c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5411da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
5421da177e4SLinus Torvalds 					region.start);
5431da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
5441da177e4SLinus Torvalds 					region.end);
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 
547c7dabef8SBjorn Helgaas 	res = bus->resource[1];
548fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
549c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
550c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5511da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
5521da177e4SLinus Torvalds 					region.start);
5531da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
5541da177e4SLinus Torvalds 					region.end);
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 
557c7dabef8SBjorn Helgaas 	res = bus->resource[2];
558fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
559c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
560c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5611da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
5621da177e4SLinus Torvalds 					region.start);
5631da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
5641da177e4SLinus Torvalds 					region.end);
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 
567c7dabef8SBjorn Helgaas 	res = bus->resource[3];
568fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
569c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
570c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5711da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
5721da177e4SLinus Torvalds 					region.start);
5731da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
5741da177e4SLinus Torvalds 					region.end);
5751da177e4SLinus Torvalds 	}
5761da177e4SLinus Torvalds }
577b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus);
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected.
5801da177e4SLinus Torvalds    PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
5811da177e4SLinus Torvalds    requires that if there is no I/O ports or memory behind the
5821da177e4SLinus Torvalds    bridge, corresponding range must be turned off by writing base
5831da177e4SLinus Torvalds    value greater than limit to the bridge's base/limit registers.
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds    Note: care must be taken when updating I/O base/limit registers
5861da177e4SLinus Torvalds    of bridges which support 32-bit I/O. This update requires two
5871da177e4SLinus Torvalds    config space writes, so it's quite possible that an I/O window of
5881da177e4SLinus Torvalds    the bridge will have some undesirable address (e.g. 0) after the
5891da177e4SLinus Torvalds    first write. Ditto 64-bit prefetchable MMIO.  */
5903f2f4dc4SYinghai Lu static void pci_setup_bridge_io(struct pci_dev *bridge)
5911da177e4SLinus Torvalds {
592c7dabef8SBjorn Helgaas 	struct resource *res;
5931da177e4SLinus Torvalds 	struct pci_bus_region region;
5942b28ae19SBjorn Helgaas 	unsigned long io_mask;
5952b28ae19SBjorn Helgaas 	u8 io_base_lo, io_limit_lo;
5965b764b83SBjorn Helgaas 	u16 l;
5975b764b83SBjorn Helgaas 	u32 io_upper16;
5981da177e4SLinus Torvalds 
5992b28ae19SBjorn Helgaas 	io_mask = PCI_IO_RANGE_MASK;
6002b28ae19SBjorn Helgaas 	if (bridge->io_window_1k)
6012b28ae19SBjorn Helgaas 		io_mask = PCI_IO_1K_RANGE_MASK;
6022b28ae19SBjorn Helgaas 
6031da177e4SLinus Torvalds 	/* Set up the top and bottom of the PCI I/O segment for this bus. */
6043f2f4dc4SYinghai Lu 	res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
605fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
606c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
6075b764b83SBjorn Helgaas 		pci_read_config_word(bridge, PCI_IO_BASE, &l);
6082b28ae19SBjorn Helgaas 		io_base_lo = (region.start >> 8) & io_mask;
6092b28ae19SBjorn Helgaas 		io_limit_lo = (region.end >> 8) & io_mask;
6105b764b83SBjorn Helgaas 		l = ((u16) io_limit_lo << 8) | io_base_lo;
6111da177e4SLinus Torvalds 		/* Set up upper 16 bits of I/O base/limit. */
6121da177e4SLinus Torvalds 		io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
613c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
6147cc5997dSYinghai Lu 	} else {
6151da177e4SLinus Torvalds 		/* Clear upper 16 bits of I/O base/limit. */
6161da177e4SLinus Torvalds 		io_upper16 = 0;
6171da177e4SLinus Torvalds 		l = 0x00f0;
6181da177e4SLinus Torvalds 	}
6191da177e4SLinus Torvalds 	/* Temporarily disable the I/O range before updating PCI_IO_BASE. */
6201da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
6211da177e4SLinus Torvalds 	/* Update lower 16 bits of I/O base/limit. */
6225b764b83SBjorn Helgaas 	pci_write_config_word(bridge, PCI_IO_BASE, l);
6231da177e4SLinus Torvalds 	/* Update upper 16 bits of I/O base/limit. */
6241da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
6257cc5997dSYinghai Lu }
6261da177e4SLinus Torvalds 
6273f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio(struct pci_dev *bridge)
6287cc5997dSYinghai Lu {
6297cc5997dSYinghai Lu 	struct resource *res;
6307cc5997dSYinghai Lu 	struct pci_bus_region region;
6317cc5997dSYinghai Lu 	u32 l;
6327cc5997dSYinghai Lu 
6337cc5997dSYinghai Lu 	/* Set up the top and bottom of the PCI Memory segment for this bus. */
6343f2f4dc4SYinghai Lu 	res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
635fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
636c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
6371da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
6381da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
639c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
6407cc5997dSYinghai Lu 	} else {
6411da177e4SLinus Torvalds 		l = 0x0000fff0;
6421da177e4SLinus Torvalds 	}
6431da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
6447cc5997dSYinghai Lu }
6457cc5997dSYinghai Lu 
6463f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge)
6477cc5997dSYinghai Lu {
6487cc5997dSYinghai Lu 	struct resource *res;
6497cc5997dSYinghai Lu 	struct pci_bus_region region;
6507cc5997dSYinghai Lu 	u32 l, bu, lu;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	/* Clear out the upper 32 bits of PREF limit.
6531da177e4SLinus Torvalds 	   If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
6541da177e4SLinus Torvalds 	   disables PREF range, which is ok. */
6551da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	/* Set up PREF base/limit. */
658c40a22e0SBenjamin Herrenschmidt 	bu = lu = 0;
6593f2f4dc4SYinghai Lu 	res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
660fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
661c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_PREFETCH) {
6621da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
6631da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
664c7dabef8SBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64) {
66513d36c24SAndrew Morton 			bu = upper_32_bits(region.start);
66613d36c24SAndrew Morton 			lu = upper_32_bits(region.end);
6671f82de10SYinghai Lu 		}
668c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
6697cc5997dSYinghai Lu 	} else {
6701da177e4SLinus Torvalds 		l = 0x0000fff0;
6711da177e4SLinus Torvalds 	}
6721da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
6731da177e4SLinus Torvalds 
674c40a22e0SBenjamin Herrenschmidt 	/* Set the upper 32 bits of PREF base & limit. */
675c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
676c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
6777cc5997dSYinghai Lu }
6787cc5997dSYinghai Lu 
6797cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
6807cc5997dSYinghai Lu {
6817cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
6827cc5997dSYinghai Lu 
683b918c62eSYinghai Lu 	dev_info(&bridge->dev, "PCI bridge to %pR\n",
684b918c62eSYinghai Lu 		 &bus->busn_res);
6857cc5997dSYinghai Lu 
6867cc5997dSYinghai Lu 	if (type & IORESOURCE_IO)
6873f2f4dc4SYinghai Lu 		pci_setup_bridge_io(bridge);
6887cc5997dSYinghai Lu 
6897cc5997dSYinghai Lu 	if (type & IORESOURCE_MEM)
6903f2f4dc4SYinghai Lu 		pci_setup_bridge_mmio(bridge);
6917cc5997dSYinghai Lu 
6927cc5997dSYinghai Lu 	if (type & IORESOURCE_PREFETCH)
6933f2f4dc4SYinghai Lu 		pci_setup_bridge_mmio_pref(bridge);
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
698e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus)
6997cc5997dSYinghai Lu {
7007cc5997dSYinghai Lu 	unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
7017cc5997dSYinghai Lu 				  IORESOURCE_PREFETCH;
7027cc5997dSYinghai Lu 
7037cc5997dSYinghai Lu 	__pci_setup_bridge(bus, type);
7047cc5997dSYinghai Lu }
7057cc5997dSYinghai Lu 
7068505e729SYinghai Lu 
7078505e729SYinghai Lu int pci_claim_bridge_resource(struct pci_dev *bridge, int i)
7088505e729SYinghai Lu {
7098505e729SYinghai Lu 	if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END)
7108505e729SYinghai Lu 		return 0;
7118505e729SYinghai Lu 
7128505e729SYinghai Lu 	if (pci_claim_resource(bridge, i) == 0)
7138505e729SYinghai Lu 		return 0;	/* claimed the window */
7148505e729SYinghai Lu 
7158505e729SYinghai Lu 	if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
7168505e729SYinghai Lu 		return 0;
7178505e729SYinghai Lu 
7188505e729SYinghai Lu 	if (!pci_bus_clip_resource(bridge, i))
7198505e729SYinghai Lu 		return -EINVAL;	/* clipping didn't change anything */
7208505e729SYinghai Lu 
7218505e729SYinghai Lu 	switch (i - PCI_BRIDGE_RESOURCES) {
7228505e729SYinghai Lu 	case 0:
7238505e729SYinghai Lu 		pci_setup_bridge_io(bridge);
7248505e729SYinghai Lu 		break;
7258505e729SYinghai Lu 	case 1:
7268505e729SYinghai Lu 		pci_setup_bridge_mmio(bridge);
7278505e729SYinghai Lu 		break;
7288505e729SYinghai Lu 	case 2:
7298505e729SYinghai Lu 		pci_setup_bridge_mmio_pref(bridge);
7308505e729SYinghai Lu 		break;
7318505e729SYinghai Lu 	default:
7328505e729SYinghai Lu 		return -EINVAL;
7338505e729SYinghai Lu 	}
7348505e729SYinghai Lu 
7358505e729SYinghai Lu 	if (pci_claim_resource(bridge, i) == 0)
7368505e729SYinghai Lu 		return 0;	/* claimed a smaller window */
7378505e729SYinghai Lu 
7388505e729SYinghai Lu 	return -EINVAL;
7398505e729SYinghai Lu }
7408505e729SYinghai Lu 
7411da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and
7421da177e4SLinus Torvalds    prefetchable memory ranges. If not, the respective
7431da177e4SLinus Torvalds    base/limit registers must be read-only and read as 0. */
74496bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus)
7451da177e4SLinus Torvalds {
7461da177e4SLinus Torvalds 	u16 io;
7471da177e4SLinus Torvalds 	u32 pmem;
7481da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
7491da177e4SLinus Torvalds 	struct resource *b_res;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
7521da177e4SLinus Torvalds 	b_res[1].flags |= IORESOURCE_MEM;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_IO_BASE, &io);
7551da177e4SLinus Torvalds 	if (!io) {
756d2f54d9bSBjorn Helgaas 		pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0);
7571da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_IO_BASE, &io);
7581da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
7591da177e4SLinus Torvalds 	}
7601da177e4SLinus Torvalds 	if (io)
7611da177e4SLinus Torvalds 		b_res[0].flags |= IORESOURCE_IO;
762d2f54d9bSBjorn Helgaas 
7631da177e4SLinus Torvalds 	/*  DECchip 21050 pass 2 errata: the bridge may miss an address
7641da177e4SLinus Torvalds 	    disconnect boundary by one PCI data phase.
7651da177e4SLinus Torvalds 	    Workaround: do not use prefetching on this device. */
7661da177e4SLinus Torvalds 	if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
7671da177e4SLinus Torvalds 		return;
768d2f54d9bSBjorn Helgaas 
7691da177e4SLinus Torvalds 	pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
7701da177e4SLinus Torvalds 	if (!pmem) {
7711da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
772d2f54d9bSBjorn Helgaas 					       0xffe0fff0);
7731da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
7741da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
7751da177e4SLinus Torvalds 	}
7761f82de10SYinghai Lu 	if (pmem) {
7771da177e4SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
77899586105SYinghai Lu 		if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
77999586105SYinghai Lu 		    PCI_PREF_RANGE_TYPE_64) {
7801f82de10SYinghai Lu 			b_res[2].flags |= IORESOURCE_MEM_64;
78199586105SYinghai Lu 			b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
78299586105SYinghai Lu 		}
7831f82de10SYinghai Lu 	}
7841f82de10SYinghai Lu 
7851f82de10SYinghai Lu 	/* double check if bridge does support 64 bit pref */
7861f82de10SYinghai Lu 	if (b_res[2].flags & IORESOURCE_MEM_64) {
7871f82de10SYinghai Lu 		u32 mem_base_hi, tmp;
7881f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
7891f82de10SYinghai Lu 					 &mem_base_hi);
7901f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
7911f82de10SYinghai Lu 					       0xffffffff);
7921f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
7931f82de10SYinghai Lu 		if (!tmp)
7941f82de10SYinghai Lu 			b_res[2].flags &= ~IORESOURCE_MEM_64;
7951f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
7961f82de10SYinghai Lu 				       mem_base_hi);
7971f82de10SYinghai Lu 	}
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds /* Helper function for sizing routines: find first available
8011da177e4SLinus Torvalds    bus resource of a given type. Note: we intentionally skip
8021da177e4SLinus Torvalds    the bus resources which have already been assigned (that is,
8031da177e4SLinus Torvalds    have non-NULL parent resource). */
8045b285415SYinghai Lu static struct resource *find_free_bus_resource(struct pci_bus *bus,
8055b285415SYinghai Lu 			 unsigned long type_mask, unsigned long type)
8061da177e4SLinus Torvalds {
8071da177e4SLinus Torvalds 	int i;
8081da177e4SLinus Torvalds 	struct resource *r;
8091da177e4SLinus Torvalds 
81089a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, r, i) {
811299de034SIvan Kokshaysky 		if (r == &ioport_resource || r == &iomem_resource)
812299de034SIvan Kokshaysky 			continue;
81355a10984SJesse Barnes 		if (r && (r->flags & type_mask) == type && !r->parent)
8141da177e4SLinus Torvalds 			return r;
8151da177e4SLinus Torvalds 	}
8161da177e4SLinus Torvalds 	return NULL;
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
81913583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size,
82013583b16SRam Pai 		resource_size_t min_size,
82113583b16SRam Pai 		resource_size_t size1,
82213583b16SRam Pai 		resource_size_t old_size,
82313583b16SRam Pai 		resource_size_t align)
82413583b16SRam Pai {
82513583b16SRam Pai 	if (size < min_size)
82613583b16SRam Pai 		size = min_size;
82713583b16SRam Pai 	if (old_size == 1)
82813583b16SRam Pai 		old_size = 0;
82913583b16SRam Pai 	/* To be fixed in 2.5: we should have sort of HAVE_ISA
83013583b16SRam Pai 	   flag in the struct pci_bus. */
83113583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
83213583b16SRam Pai 	size = (size & 0xff) + ((size & ~0xffUL) << 2);
83313583b16SRam Pai #endif
83413583b16SRam Pai 	size = ALIGN(size + size1, align);
83513583b16SRam Pai 	if (size < old_size)
83613583b16SRam Pai 		size = old_size;
83713583b16SRam Pai 	return size;
83813583b16SRam Pai }
83913583b16SRam Pai 
84013583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size,
84113583b16SRam Pai 		resource_size_t min_size,
84213583b16SRam Pai 		resource_size_t size1,
84313583b16SRam Pai 		resource_size_t old_size,
84413583b16SRam Pai 		resource_size_t align)
84513583b16SRam Pai {
84613583b16SRam Pai 	if (size < min_size)
84713583b16SRam Pai 		size = min_size;
84813583b16SRam Pai 	if (old_size == 1)
84913583b16SRam Pai 		old_size = 0;
85013583b16SRam Pai 	if (size < old_size)
85113583b16SRam Pai 		size = old_size;
85213583b16SRam Pai 	size = ALIGN(size + size1, align);
85313583b16SRam Pai 	return size;
85413583b16SRam Pai }
85513583b16SRam Pai 
856ac5ad93eSGavin Shan resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
857ac5ad93eSGavin Shan 						unsigned long type)
858ac5ad93eSGavin Shan {
859ac5ad93eSGavin Shan 	return 1;
860ac5ad93eSGavin Shan }
861ac5ad93eSGavin Shan 
862ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_MEM_ALIGN	0x100000	/* 1MiB */
863ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN	0x1000		/* 4KiB */
864ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN_1K	0x400		/* 1KiB */
865ac5ad93eSGavin Shan 
866ac5ad93eSGavin Shan static resource_size_t window_alignment(struct pci_bus *bus,
867ac5ad93eSGavin Shan 					unsigned long type)
868ac5ad93eSGavin Shan {
869ac5ad93eSGavin Shan 	resource_size_t align = 1, arch_align;
870ac5ad93eSGavin Shan 
871ac5ad93eSGavin Shan 	if (type & IORESOURCE_MEM)
872ac5ad93eSGavin Shan 		align = PCI_P2P_DEFAULT_MEM_ALIGN;
873ac5ad93eSGavin Shan 	else if (type & IORESOURCE_IO) {
874ac5ad93eSGavin Shan 		/*
875ac5ad93eSGavin Shan 		 * Per spec, I/O windows are 4K-aligned, but some
876ac5ad93eSGavin Shan 		 * bridges have an extension to support 1K alignment.
877ac5ad93eSGavin Shan 		 */
878ac5ad93eSGavin Shan 		if (bus->self->io_window_1k)
879ac5ad93eSGavin Shan 			align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
880ac5ad93eSGavin Shan 		else
881ac5ad93eSGavin Shan 			align = PCI_P2P_DEFAULT_IO_ALIGN;
882ac5ad93eSGavin Shan 	}
883ac5ad93eSGavin Shan 
884ac5ad93eSGavin Shan 	arch_align = pcibios_window_alignment(bus, type);
885ac5ad93eSGavin Shan 	return max(align, arch_align);
886ac5ad93eSGavin Shan }
887ac5ad93eSGavin Shan 
888c8adf9a3SRam Pai /**
889c8adf9a3SRam Pai  * pbus_size_io() - size the io window of a given bus
890c8adf9a3SRam Pai  *
891c8adf9a3SRam Pai  * @bus : the bus
892c8adf9a3SRam Pai  * @min_size : the minimum io window that must to be allocated
893c8adf9a3SRam Pai  * @add_size : additional optional io window
8949e8bf93aSRam Pai  * @realloc_head : track the additional io window on this list
895c8adf9a3SRam Pai  *
896c8adf9a3SRam Pai  * Sizing the IO windows of the PCI-PCI bridge is trivial,
897fd591341SYinghai Lu  * since these windows have 1K or 4K granularity and the IO ranges
898c8adf9a3SRam Pai  * of non-bridge PCI devices are limited to 256 bytes.
899c8adf9a3SRam Pai  * We must be careful with the ISA aliasing though.
900c8adf9a3SRam Pai  */
901c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
902bdc4abecSYinghai Lu 		resource_size_t add_size, struct list_head *realloc_head)
9031da177e4SLinus Torvalds {
9041da177e4SLinus Torvalds 	struct pci_dev *dev;
9055b285415SYinghai Lu 	struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO,
9065b285415SYinghai Lu 							IORESOURCE_IO);
90711251a86SWei Yang 	resource_size_t size = 0, size0 = 0, size1 = 0;
908be768912SYinghai Lu 	resource_size_t children_add_size = 0;
9092d1d6678SBjorn Helgaas 	resource_size_t min_align, align;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	if (!b_res)
9121da177e4SLinus Torvalds 		return;
9131da177e4SLinus Torvalds 
9142d1d6678SBjorn Helgaas 	min_align = window_alignment(bus, IORESOURCE_IO);
9151da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
9161da177e4SLinus Torvalds 		int i;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
9191da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
9201da177e4SLinus Torvalds 			unsigned long r_size;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 			if (r->parent || !(r->flags & IORESOURCE_IO))
9231da177e4SLinus Torvalds 				continue;
924022edd86SZhao, Yu 			r_size = resource_size(r);
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 			if (r_size < 0x400)
9271da177e4SLinus Torvalds 				/* Might be re-aligned for ISA */
9281da177e4SLinus Torvalds 				size += r_size;
9291da177e4SLinus Torvalds 			else
9301da177e4SLinus Torvalds 				size1 += r_size;
931be768912SYinghai Lu 
932fd591341SYinghai Lu 			align = pci_resource_alignment(dev, r);
933fd591341SYinghai Lu 			if (align > min_align)
934fd591341SYinghai Lu 				min_align = align;
935fd591341SYinghai Lu 
9369e8bf93aSRam Pai 			if (realloc_head)
9379e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
9381da177e4SLinus Torvalds 		}
9391da177e4SLinus Torvalds 	}
940fd591341SYinghai Lu 
941c8adf9a3SRam Pai 	size0 = calculate_iosize(size, min_size, size1,
942fd591341SYinghai Lu 			resource_size(b_res), min_align);
943be768912SYinghai Lu 	if (children_add_size > add_size)
944be768912SYinghai Lu 		add_size = children_add_size;
9459e8bf93aSRam Pai 	size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
946a4ac9feaSYinghai Lu 		calculate_iosize(size, min_size, add_size + size1,
947fd591341SYinghai Lu 			resource_size(b_res), min_align);
948c8adf9a3SRam Pai 	if (!size0 && !size1) {
949865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
950227f0647SRyan Desfosses 			dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n",
951227f0647SRyan Desfosses 				 b_res, &bus->busn_res);
9521da177e4SLinus Torvalds 		b_res->flags = 0;
9531da177e4SLinus Torvalds 		return;
9541da177e4SLinus Torvalds 	}
955fd591341SYinghai Lu 
956fd591341SYinghai Lu 	b_res->start = min_align;
957c8adf9a3SRam Pai 	b_res->end = b_res->start + size0 - 1;
95888452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
959b592443dSYinghai Lu 	if (size1 > size0 && realloc_head) {
960fd591341SYinghai Lu 		add_to_list(realloc_head, bus->self, b_res, size1-size0,
961fd591341SYinghai Lu 			    min_align);
962227f0647SRyan Desfosses 		dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx\n",
963227f0647SRyan Desfosses 			   b_res, &bus->busn_res,
96411251a86SWei Yang 			   (unsigned long long)size1-size0);
965b592443dSYinghai Lu 	}
9661da177e4SLinus Torvalds }
9671da177e4SLinus Torvalds 
968c121504eSGavin Shan static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
969c121504eSGavin Shan 						  int max_order)
970c121504eSGavin Shan {
971c121504eSGavin Shan 	resource_size_t align = 0;
972c121504eSGavin Shan 	resource_size_t min_align = 0;
973c121504eSGavin Shan 	int order;
974c121504eSGavin Shan 
975c121504eSGavin Shan 	for (order = 0; order <= max_order; order++) {
976c121504eSGavin Shan 		resource_size_t align1 = 1;
977c121504eSGavin Shan 
978c121504eSGavin Shan 		align1 <<= (order + 20);
979c121504eSGavin Shan 
980c121504eSGavin Shan 		if (!align)
981c121504eSGavin Shan 			min_align = align1;
982c121504eSGavin Shan 		else if (ALIGN(align + min_align, min_align) < align1)
983c121504eSGavin Shan 			min_align = align1 >> 1;
984c121504eSGavin Shan 		align += aligns[order];
985c121504eSGavin Shan 	}
986c121504eSGavin Shan 
987c121504eSGavin Shan 	return min_align;
988c121504eSGavin Shan }
989c121504eSGavin Shan 
990c8adf9a3SRam Pai /**
991c8adf9a3SRam Pai  * pbus_size_mem() - size the memory window of a given bus
992c8adf9a3SRam Pai  *
993c8adf9a3SRam Pai  * @bus : the bus
994496f70cfSWei Yang  * @mask: mask the resource flag, then compare it with type
995496f70cfSWei Yang  * @type: the type of free resource from bridge
9965b285415SYinghai Lu  * @type2: second match type
9975b285415SYinghai Lu  * @type3: third match type
998c8adf9a3SRam Pai  * @min_size : the minimum memory window that must to be allocated
999c8adf9a3SRam Pai  * @add_size : additional optional memory window
10009e8bf93aSRam Pai  * @realloc_head : track the additional memory window on this list
1001c8adf9a3SRam Pai  *
1002c8adf9a3SRam Pai  * Calculate the size of the bus and minimal alignment which
1003c8adf9a3SRam Pai  * guarantees that all child resources fit in this size.
100430afe8d0SBjorn Helgaas  *
100530afe8d0SBjorn Helgaas  * Returns -ENOSPC if there's no available bus resource of the desired type.
100630afe8d0SBjorn Helgaas  * Otherwise, sets the bus resource start/end to indicate the required
100730afe8d0SBjorn Helgaas  * size, adds things to realloc_head (if supplied), and returns 0.
1008c8adf9a3SRam Pai  */
100928760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
10105b285415SYinghai Lu 			 unsigned long type, unsigned long type2,
10115b285415SYinghai Lu 			 unsigned long type3,
10125b285415SYinghai Lu 			 resource_size_t min_size, resource_size_t add_size,
1013bdc4abecSYinghai Lu 			 struct list_head *realloc_head)
10141da177e4SLinus Torvalds {
10151da177e4SLinus Torvalds 	struct pci_dev *dev;
1016c8adf9a3SRam Pai 	resource_size_t min_align, align, size, size0, size1;
1017096d4221SYinghai Lu 	resource_size_t aligns[18];	/* Alignments from 1Mb to 128Gb */
10181da177e4SLinus Torvalds 	int order, max_order;
10195b285415SYinghai Lu 	struct resource *b_res = find_free_bus_resource(bus,
10205b285415SYinghai Lu 					mask | IORESOURCE_PREFETCH, type);
1021be768912SYinghai Lu 	resource_size_t children_add_size = 0;
1022d74b9027SWei Yang 	resource_size_t children_add_align = 0;
1023d74b9027SWei Yang 	resource_size_t add_align = 0;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	if (!b_res)
102630afe8d0SBjorn Helgaas 		return -ENOSPC;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
10291da177e4SLinus Torvalds 	max_order = 0;
10301da177e4SLinus Torvalds 	size = 0;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
10331da177e4SLinus Torvalds 		int i;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
10361da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
1037c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
10381da177e4SLinus Torvalds 
10395b285415SYinghai Lu 			if (r->parent || ((r->flags & mask) != type &&
10405b285415SYinghai Lu 					  (r->flags & mask) != type2 &&
10415b285415SYinghai Lu 					  (r->flags & mask) != type3))
10421da177e4SLinus Torvalds 				continue;
1043022edd86SZhao, Yu 			r_size = resource_size(r);
10442aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV
10452aceefcbSYinghai Lu 			/* put SRIOV requested res to the optional list */
10469e8bf93aSRam Pai 			if (realloc_head && i >= PCI_IOV_RESOURCES &&
10472aceefcbSYinghai Lu 					i <= PCI_IOV_RESOURCE_END) {
1048d74b9027SWei Yang 				add_align = max(pci_resource_alignment(dev, r), add_align);
10492aceefcbSYinghai Lu 				r->end = r->start - 1;
1050f7625980SBjorn Helgaas 				add_to_list(realloc_head, dev, r, r_size, 0/* don't care */);
10512aceefcbSYinghai Lu 				children_add_size += r_size;
10522aceefcbSYinghai Lu 				continue;
10532aceefcbSYinghai Lu 			}
10542aceefcbSYinghai Lu #endif
105514c8530dSAlan 			/*
105614c8530dSAlan 			 * aligns[0] is for 1MB (since bridge memory
105714c8530dSAlan 			 * windows are always at least 1MB aligned), so
105814c8530dSAlan 			 * keep "order" from being negative for smaller
105914c8530dSAlan 			 * resources.
106014c8530dSAlan 			 */
10616faf17f6SChris Wright 			align = pci_resource_alignment(dev, r);
10621da177e4SLinus Torvalds 			order = __ffs(align) - 20;
106314c8530dSAlan 			if (order < 0)
106414c8530dSAlan 				order = 0;
106514c8530dSAlan 			if (order >= ARRAY_SIZE(aligns)) {
1066227f0647SRyan Desfosses 				dev_warn(&dev->dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
1067227f0647SRyan Desfosses 					 i, r, (unsigned long long) align);
10681da177e4SLinus Torvalds 				r->flags = 0;
10691da177e4SLinus Torvalds 				continue;
10701da177e4SLinus Torvalds 			}
10711da177e4SLinus Torvalds 			size += r_size;
10721da177e4SLinus Torvalds 			/* Exclude ranges with size > align from
10731da177e4SLinus Torvalds 			   calculation of the alignment. */
10741da177e4SLinus Torvalds 			if (r_size == align)
10751da177e4SLinus Torvalds 				aligns[order] += align;
10761da177e4SLinus Torvalds 			if (order > max_order)
10771da177e4SLinus Torvalds 				max_order = order;
1078be768912SYinghai Lu 
1079d74b9027SWei Yang 			if (realloc_head) {
10809e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
1081d74b9027SWei Yang 				children_add_align = get_res_add_align(realloc_head, r);
1082d74b9027SWei Yang 				add_align = max(add_align, children_add_align);
1083d74b9027SWei Yang 			}
10841da177e4SLinus Torvalds 		}
10851da177e4SLinus Torvalds 	}
10868308c54dSJeremy Fitzhardinge 
1087c121504eSGavin Shan 	min_align = calculate_mem_align(aligns, max_order);
10883ad94b0dSWei Yang 	min_align = max(min_align, window_alignment(bus, b_res->flags));
1089b42282e5SLinus Torvalds 	size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
1090d74b9027SWei Yang 	add_align = max(min_align, add_align);
1091be768912SYinghai Lu 	if (children_add_size > add_size)
1092be768912SYinghai Lu 		add_size = children_add_size;
10939e8bf93aSRam Pai 	size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
1094a4ac9feaSYinghai Lu 		calculate_memsize(size, min_size, add_size,
1095d74b9027SWei Yang 				resource_size(b_res), add_align);
1096c8adf9a3SRam Pai 	if (!size0 && !size1) {
1097865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
1098227f0647SRyan Desfosses 			dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n",
1099227f0647SRyan Desfosses 				 b_res, &bus->busn_res);
11001da177e4SLinus Torvalds 		b_res->flags = 0;
110130afe8d0SBjorn Helgaas 		return 0;
11021da177e4SLinus Torvalds 	}
11031da177e4SLinus Torvalds 	b_res->start = min_align;
1104c8adf9a3SRam Pai 	b_res->end = size0 + min_align - 1;
11055b285415SYinghai Lu 	b_res->flags |= IORESOURCE_STARTALIGN;
1106b592443dSYinghai Lu 	if (size1 > size0 && realloc_head) {
1107d74b9027SWei Yang 		add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align);
1108d74b9027SWei Yang 		dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx add_align %llx\n",
1109227f0647SRyan Desfosses 			   b_res, &bus->busn_res,
1110d74b9027SWei Yang 			   (unsigned long long) (size1 - size0),
1111d74b9027SWei Yang 			   (unsigned long long) add_align);
1112b592443dSYinghai Lu 	}
111330afe8d0SBjorn Helgaas 	return 0;
11141da177e4SLinus Torvalds }
11151da177e4SLinus Torvalds 
11160a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res)
11170a2daa1cSRam Pai {
11180a2daa1cSRam Pai 	if (res->flags & IORESOURCE_IO)
11190a2daa1cSRam Pai 		return pci_cardbus_io_size;
11200a2daa1cSRam Pai 	if (res->flags & IORESOURCE_MEM)
11210a2daa1cSRam Pai 		return pci_cardbus_mem_size;
11220a2daa1cSRam Pai 	return 0;
11230a2daa1cSRam Pai }
11240a2daa1cSRam Pai 
11250a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus,
1126bdc4abecSYinghai Lu 			struct list_head *realloc_head)
11271da177e4SLinus Torvalds {
11281da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
11291da177e4SLinus Torvalds 	struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
113011848934SYinghai Lu 	resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
11311da177e4SLinus Torvalds 	u16 ctrl;
11321da177e4SLinus Torvalds 
11333796f1e2SYinghai Lu 	if (b_res[0].parent)
11343796f1e2SYinghai Lu 		goto handle_b_res_1;
11351da177e4SLinus Torvalds 	/*
11361da177e4SLinus Torvalds 	 * Reserve some resources for CardBus.  We reserve
11371da177e4SLinus Torvalds 	 * a fixed amount of bus space for CardBus bridges.
11381da177e4SLinus Torvalds 	 */
113911848934SYinghai Lu 	b_res[0].start = pci_cardbus_io_size;
114011848934SYinghai Lu 	b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
114111848934SYinghai Lu 	b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
114211848934SYinghai Lu 	if (realloc_head) {
114311848934SYinghai Lu 		b_res[0].end -= pci_cardbus_io_size;
114411848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
114511848934SYinghai Lu 				pci_cardbus_io_size);
114611848934SYinghai Lu 	}
11471da177e4SLinus Torvalds 
11483796f1e2SYinghai Lu handle_b_res_1:
11493796f1e2SYinghai Lu 	if (b_res[1].parent)
11503796f1e2SYinghai Lu 		goto handle_b_res_2;
115111848934SYinghai Lu 	b_res[1].start = pci_cardbus_io_size;
115211848934SYinghai Lu 	b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
115311848934SYinghai Lu 	b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
115411848934SYinghai Lu 	if (realloc_head) {
115511848934SYinghai Lu 		b_res[1].end -= pci_cardbus_io_size;
115611848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
115711848934SYinghai Lu 				 pci_cardbus_io_size);
115811848934SYinghai Lu 	}
11591da177e4SLinus Torvalds 
11603796f1e2SYinghai Lu handle_b_res_2:
1161dcef0d06SYinghai Lu 	/* MEM1 must not be pref mmio */
1162dcef0d06SYinghai Lu 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1163dcef0d06SYinghai Lu 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
1164dcef0d06SYinghai Lu 		ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
1165dcef0d06SYinghai Lu 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1166dcef0d06SYinghai Lu 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1167dcef0d06SYinghai Lu 	}
1168dcef0d06SYinghai Lu 
11691da177e4SLinus Torvalds 	/*
11701da177e4SLinus Torvalds 	 * Check whether prefetchable memory is supported
11711da177e4SLinus Torvalds 	 * by this bridge.
11721da177e4SLinus Torvalds 	 */
11731da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
11741da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
11751da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
11761da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
11771da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
11781da177e4SLinus Torvalds 	}
11791da177e4SLinus Torvalds 
11803796f1e2SYinghai Lu 	if (b_res[2].parent)
11813796f1e2SYinghai Lu 		goto handle_b_res_3;
11821da177e4SLinus Torvalds 	/*
11831da177e4SLinus Torvalds 	 * If we have prefetchable memory support, allocate
11841da177e4SLinus Torvalds 	 * two regions.  Otherwise, allocate one region of
11851da177e4SLinus Torvalds 	 * twice the size.
11861da177e4SLinus Torvalds 	 */
11871da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
118811848934SYinghai Lu 		b_res[2].start = pci_cardbus_mem_size;
118911848934SYinghai Lu 		b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
119011848934SYinghai Lu 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
119111848934SYinghai Lu 				  IORESOURCE_STARTALIGN;
119211848934SYinghai Lu 		if (realloc_head) {
119311848934SYinghai Lu 			b_res[2].end -= pci_cardbus_mem_size;
119411848934SYinghai Lu 			add_to_list(realloc_head, bridge, b_res+2,
119511848934SYinghai Lu 				 pci_cardbus_mem_size, pci_cardbus_mem_size);
11961da177e4SLinus Torvalds 		}
11970a2daa1cSRam Pai 
119811848934SYinghai Lu 		/* reduce that to half */
119911848934SYinghai Lu 		b_res_3_size = pci_cardbus_mem_size;
120011848934SYinghai Lu 	}
120111848934SYinghai Lu 
12023796f1e2SYinghai Lu handle_b_res_3:
12033796f1e2SYinghai Lu 	if (b_res[3].parent)
12043796f1e2SYinghai Lu 		goto handle_done;
120511848934SYinghai Lu 	b_res[3].start = pci_cardbus_mem_size;
120611848934SYinghai Lu 	b_res[3].end = b_res[3].start + b_res_3_size - 1;
120711848934SYinghai Lu 	b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
120811848934SYinghai Lu 	if (realloc_head) {
120911848934SYinghai Lu 		b_res[3].end -= b_res_3_size;
121011848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
121111848934SYinghai Lu 				 pci_cardbus_mem_size);
121211848934SYinghai Lu 	}
12133796f1e2SYinghai Lu 
12143796f1e2SYinghai Lu handle_done:
12153796f1e2SYinghai Lu 	;
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
121810874f5aSBjorn Helgaas void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
12191da177e4SLinus Torvalds {
12201da177e4SLinus Torvalds 	struct pci_dev *dev;
12215b285415SYinghai Lu 	unsigned long mask, prefmask, type2 = 0, type3 = 0;
1222c8adf9a3SRam Pai 	resource_size_t additional_mem_size = 0, additional_io_size = 0;
12235b285415SYinghai Lu 	struct resource *b_res;
122430afe8d0SBjorn Helgaas 	int ret;
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
12271da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
12281da177e4SLinus Torvalds 		if (!b)
12291da177e4SLinus Torvalds 			continue;
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 		switch (dev->class >> 8) {
12321da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
12339e8bf93aSRam Pai 			pci_bus_size_cardbus(b, realloc_head);
12341da177e4SLinus Torvalds 			break;
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
12371da177e4SLinus Torvalds 		default:
12389e8bf93aSRam Pai 			__pci_bus_size_bridges(b, realloc_head);
12391da177e4SLinus Torvalds 			break;
12401da177e4SLinus Torvalds 		}
12411da177e4SLinus Torvalds 	}
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	/* The root bus? */
12442ba29e27SWei Yang 	if (pci_is_root_bus(bus))
12451da177e4SLinus Torvalds 		return;
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	switch (bus->self->class >> 8) {
12481da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_CARDBUS:
12491da177e4SLinus Torvalds 		/* don't size cardbuses yet. */
12501da177e4SLinus Torvalds 		break;
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_PCI:
12531da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
125428760489SEric W. Biederman 		if (bus->self->is_hotplug_bridge) {
1255c8adf9a3SRam Pai 			additional_io_size  = pci_hotplug_io_size;
1256c8adf9a3SRam Pai 			additional_mem_size = pci_hotplug_mem_size;
125728760489SEric W. Biederman 		}
125867d29b5cSBjorn Helgaas 		/* Fall through */
12591da177e4SLinus Torvalds 	default:
126019aa7ee4SYinghai Lu 		pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
126119aa7ee4SYinghai Lu 			     additional_io_size, realloc_head);
126267d29b5cSBjorn Helgaas 
126367d29b5cSBjorn Helgaas 		/*
126467d29b5cSBjorn Helgaas 		 * If there's a 64-bit prefetchable MMIO window, compute
126567d29b5cSBjorn Helgaas 		 * the size required to put all 64-bit prefetchable
126667d29b5cSBjorn Helgaas 		 * resources in it.
126767d29b5cSBjorn Helgaas 		 */
12685b285415SYinghai Lu 		b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES];
12691da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
12701da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
12715b285415SYinghai Lu 		if (b_res[2].flags & IORESOURCE_MEM_64) {
12725b285415SYinghai Lu 			prefmask |= IORESOURCE_MEM_64;
127330afe8d0SBjorn Helgaas 			ret = pbus_size_mem(bus, prefmask, prefmask,
12745b285415SYinghai Lu 				  prefmask, prefmask,
127519aa7ee4SYinghai Lu 				  realloc_head ? 0 : additional_mem_size,
127630afe8d0SBjorn Helgaas 				  additional_mem_size, realloc_head);
127767d29b5cSBjorn Helgaas 
12785b285415SYinghai Lu 			/*
127967d29b5cSBjorn Helgaas 			 * If successful, all non-prefetchable resources
128067d29b5cSBjorn Helgaas 			 * and any 32-bit prefetchable resources will go in
128167d29b5cSBjorn Helgaas 			 * the non-prefetchable window.
128267d29b5cSBjorn Helgaas 			 */
128367d29b5cSBjorn Helgaas 			if (ret == 0) {
12845b285415SYinghai Lu 				mask = prefmask;
12855b285415SYinghai Lu 				type2 = prefmask & ~IORESOURCE_MEM_64;
12865b285415SYinghai Lu 				type3 = prefmask & ~IORESOURCE_PREFETCH;
12875b285415SYinghai Lu 			}
12885b285415SYinghai Lu 		}
128967d29b5cSBjorn Helgaas 
129067d29b5cSBjorn Helgaas 		/*
129167d29b5cSBjorn Helgaas 		 * If there is no 64-bit prefetchable window, compute the
129267d29b5cSBjorn Helgaas 		 * size required to put all prefetchable resources in the
129367d29b5cSBjorn Helgaas 		 * 32-bit prefetchable window (if there is one).
129467d29b5cSBjorn Helgaas 		 */
12955b285415SYinghai Lu 		if (!type2) {
12965b285415SYinghai Lu 			prefmask &= ~IORESOURCE_MEM_64;
129730afe8d0SBjorn Helgaas 			ret = pbus_size_mem(bus, prefmask, prefmask,
12985b285415SYinghai Lu 					 prefmask, prefmask,
12995b285415SYinghai Lu 					 realloc_head ? 0 : additional_mem_size,
130030afe8d0SBjorn Helgaas 					 additional_mem_size, realloc_head);
130167d29b5cSBjorn Helgaas 
130267d29b5cSBjorn Helgaas 			/*
130367d29b5cSBjorn Helgaas 			 * If successful, only non-prefetchable resources
130467d29b5cSBjorn Helgaas 			 * will go in the non-prefetchable window.
130567d29b5cSBjorn Helgaas 			 */
130667d29b5cSBjorn Helgaas 			if (ret == 0)
13075b285415SYinghai Lu 				mask = prefmask;
130828760489SEric W. Biederman 			else
1309c8adf9a3SRam Pai 				additional_mem_size += additional_mem_size;
131067d29b5cSBjorn Helgaas 
13115b285415SYinghai Lu 			type2 = type3 = IORESOURCE_MEM;
13125b285415SYinghai Lu 		}
131367d29b5cSBjorn Helgaas 
131467d29b5cSBjorn Helgaas 		/*
131567d29b5cSBjorn Helgaas 		 * Compute the size required to put everything else in the
131667d29b5cSBjorn Helgaas 		 * non-prefetchable window.  This includes:
131767d29b5cSBjorn Helgaas 		 *
131867d29b5cSBjorn Helgaas 		 *   - all non-prefetchable resources
131967d29b5cSBjorn Helgaas 		 *   - 32-bit prefetchable resources if there's a 64-bit
132067d29b5cSBjorn Helgaas 		 *     prefetchable window or no prefetchable window at all
132167d29b5cSBjorn Helgaas 		 *   - 64-bit prefetchable resources if there's no
132267d29b5cSBjorn Helgaas 		 *     prefetchable window at all
132367d29b5cSBjorn Helgaas 		 *
132467d29b5cSBjorn Helgaas 		 * Note that the strategy in __pci_assign_resource() must
132567d29b5cSBjorn Helgaas 		 * match that used here.  Specifically, we cannot put a
132667d29b5cSBjorn Helgaas 		 * 32-bit prefetchable resource in a 64-bit prefetchable
132767d29b5cSBjorn Helgaas 		 * window.
132867d29b5cSBjorn Helgaas 		 */
13295b285415SYinghai Lu 		pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
133019aa7ee4SYinghai Lu 				realloc_head ? 0 : additional_mem_size,
133119aa7ee4SYinghai Lu 				additional_mem_size, realloc_head);
13321da177e4SLinus Torvalds 		break;
13331da177e4SLinus Torvalds 	}
13341da177e4SLinus Torvalds }
1335c8adf9a3SRam Pai 
133610874f5aSBjorn Helgaas void pci_bus_size_bridges(struct pci_bus *bus)
1337c8adf9a3SRam Pai {
1338c8adf9a3SRam Pai 	__pci_bus_size_bridges(bus, NULL);
1339c8adf9a3SRam Pai }
13401da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
13411da177e4SLinus Torvalds 
134210874f5aSBjorn Helgaas void __pci_bus_assign_resources(const struct pci_bus *bus,
1343bdc4abecSYinghai Lu 				struct list_head *realloc_head,
1344bdc4abecSYinghai Lu 				struct list_head *fail_head)
13451da177e4SLinus Torvalds {
13461da177e4SLinus Torvalds 	struct pci_bus *b;
13471da177e4SLinus Torvalds 	struct pci_dev *dev;
13481da177e4SLinus Torvalds 
13499e8bf93aSRam Pai 	pbus_assign_resources_sorted(bus, realloc_head, fail_head);
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
13521da177e4SLinus Torvalds 		b = dev->subordinate;
13531da177e4SLinus Torvalds 		if (!b)
13541da177e4SLinus Torvalds 			continue;
13551da177e4SLinus Torvalds 
13569e8bf93aSRam Pai 		__pci_bus_assign_resources(b, realloc_head, fail_head);
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 		switch (dev->class >> 8) {
13591da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
13606841ec68SYinghai Lu 			if (!pci_is_enabled(dev))
13611da177e4SLinus Torvalds 				pci_setup_bridge(b);
13621da177e4SLinus Torvalds 			break;
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
13651da177e4SLinus Torvalds 			pci_setup_cardbus(b);
13661da177e4SLinus Torvalds 			break;
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 		default:
1369227f0647SRyan Desfosses 			dev_info(&dev->dev, "not setting up bridge for bus %04x:%02x\n",
1370227f0647SRyan Desfosses 				 pci_domain_nr(b), b->number);
13711da177e4SLinus Torvalds 			break;
13721da177e4SLinus Torvalds 		}
13731da177e4SLinus Torvalds 	}
13741da177e4SLinus Torvalds }
1375568ddef8SYinghai Lu 
137610874f5aSBjorn Helgaas void pci_bus_assign_resources(const struct pci_bus *bus)
1377568ddef8SYinghai Lu {
1378c8adf9a3SRam Pai 	__pci_bus_assign_resources(bus, NULL, NULL);
1379568ddef8SYinghai Lu }
13801da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
13811da177e4SLinus Torvalds 
138210874f5aSBjorn Helgaas static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
1383bdc4abecSYinghai Lu 					  struct list_head *add_head,
1384bdc4abecSYinghai Lu 					  struct list_head *fail_head)
13856841ec68SYinghai Lu {
13866841ec68SYinghai Lu 	struct pci_bus *b;
13876841ec68SYinghai Lu 
13888424d759SYinghai Lu 	pdev_assign_resources_sorted((struct pci_dev *)bridge,
13898424d759SYinghai Lu 					 add_head, fail_head);
13906841ec68SYinghai Lu 
13916841ec68SYinghai Lu 	b = bridge->subordinate;
13926841ec68SYinghai Lu 	if (!b)
13936841ec68SYinghai Lu 		return;
13946841ec68SYinghai Lu 
13958424d759SYinghai Lu 	__pci_bus_assign_resources(b, add_head, fail_head);
13966841ec68SYinghai Lu 
13976841ec68SYinghai Lu 	switch (bridge->class >> 8) {
13986841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_PCI:
13996841ec68SYinghai Lu 		pci_setup_bridge(b);
14006841ec68SYinghai Lu 		break;
14016841ec68SYinghai Lu 
14026841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_CARDBUS:
14036841ec68SYinghai Lu 		pci_setup_cardbus(b);
14046841ec68SYinghai Lu 		break;
14056841ec68SYinghai Lu 
14066841ec68SYinghai Lu 	default:
1407227f0647SRyan Desfosses 		dev_info(&bridge->dev, "not setting up bridge for bus %04x:%02x\n",
1408227f0647SRyan Desfosses 			 pci_domain_nr(b), b->number);
14096841ec68SYinghai Lu 		break;
14106841ec68SYinghai Lu 	}
14116841ec68SYinghai Lu }
14125009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus,
14135009b460SYinghai Lu 					  unsigned long type)
14145009b460SYinghai Lu {
14155b285415SYinghai Lu 	struct pci_dev *dev = bus->self;
14165009b460SYinghai Lu 	struct resource *r;
14175009b460SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
14185b285415SYinghai Lu 				  IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
14195b285415SYinghai Lu 	unsigned old_flags = 0;
14205b285415SYinghai Lu 	struct resource *b_res;
14215b285415SYinghai Lu 	int idx = 1;
14225009b460SYinghai Lu 
14235b285415SYinghai Lu 	b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
14245b285415SYinghai Lu 
14255b285415SYinghai Lu 	/*
14265b285415SYinghai Lu 	 *     1. if there is io port assign fail, will release bridge
14275b285415SYinghai Lu 	 *	  io port.
14285b285415SYinghai Lu 	 *     2. if there is non pref mmio assign fail, release bridge
14295b285415SYinghai Lu 	 *	  nonpref mmio.
14305b285415SYinghai Lu 	 *     3. if there is 64bit pref mmio assign fail, and bridge pref
14315b285415SYinghai Lu 	 *	  is 64bit, release bridge pref mmio.
14325b285415SYinghai Lu 	 *     4. if there is pref mmio assign fail, and bridge pref is
14335b285415SYinghai Lu 	 *	  32bit mmio, release bridge pref mmio
14345b285415SYinghai Lu 	 *     5. if there is pref mmio assign fail, and bridge pref is not
14355b285415SYinghai Lu 	 *	  assigned, release bridge nonpref mmio.
14365b285415SYinghai Lu 	 */
14375b285415SYinghai Lu 	if (type & IORESOURCE_IO)
14385b285415SYinghai Lu 		idx = 0;
14395b285415SYinghai Lu 	else if (!(type & IORESOURCE_PREFETCH))
14405b285415SYinghai Lu 		idx = 1;
14415b285415SYinghai Lu 	else if ((type & IORESOURCE_MEM_64) &&
14425b285415SYinghai Lu 		 (b_res[2].flags & IORESOURCE_MEM_64))
14435b285415SYinghai Lu 		idx = 2;
14445b285415SYinghai Lu 	else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
14455b285415SYinghai Lu 		 (b_res[2].flags & IORESOURCE_PREFETCH))
14465b285415SYinghai Lu 		idx = 2;
14475b285415SYinghai Lu 	else
14485b285415SYinghai Lu 		idx = 1;
14495b285415SYinghai Lu 
14505b285415SYinghai Lu 	r = &b_res[idx];
14515b285415SYinghai Lu 
14525009b460SYinghai Lu 	if (!r->parent)
14535b285415SYinghai Lu 		return;
14545b285415SYinghai Lu 
14555009b460SYinghai Lu 	/*
14565009b460SYinghai Lu 	 * if there are children under that, we should release them
14575009b460SYinghai Lu 	 *  all
14585009b460SYinghai Lu 	 */
14595009b460SYinghai Lu 	release_child_resources(r);
14605009b460SYinghai Lu 	if (!release_resource(r)) {
14615b285415SYinghai Lu 		type = old_flags = r->flags & type_mask;
14625b285415SYinghai Lu 		dev_printk(KERN_DEBUG, &dev->dev, "resource %d %pR released\n",
14635b285415SYinghai Lu 					PCI_BRIDGE_RESOURCES + idx, r);
14645009b460SYinghai Lu 		/* keep the old size */
14655009b460SYinghai Lu 		r->end = resource_size(r) - 1;
14665009b460SYinghai Lu 		r->start = 0;
14675009b460SYinghai Lu 		r->flags = 0;
14685009b460SYinghai Lu 
14695009b460SYinghai Lu 		/* avoiding touch the one without PREF */
14705009b460SYinghai Lu 		if (type & IORESOURCE_PREFETCH)
14715009b460SYinghai Lu 			type = IORESOURCE_PREFETCH;
14725009b460SYinghai Lu 		__pci_setup_bridge(bus, type);
14735b285415SYinghai Lu 		/* for next child res under same bridge */
14745b285415SYinghai Lu 		r->flags = old_flags;
14755009b460SYinghai Lu 	}
14765009b460SYinghai Lu }
14775009b460SYinghai Lu 
14785009b460SYinghai Lu enum release_type {
14795009b460SYinghai Lu 	leaf_only,
14805009b460SYinghai Lu 	whole_subtree,
14815009b460SYinghai Lu };
14825009b460SYinghai Lu /*
14835009b460SYinghai Lu  * try to release pci bridge resources that is from leaf bridge,
14845009b460SYinghai Lu  * so we can allocate big new one later
14855009b460SYinghai Lu  */
148610874f5aSBjorn Helgaas static void pci_bus_release_bridge_resources(struct pci_bus *bus,
14875009b460SYinghai Lu 					     unsigned long type,
14885009b460SYinghai Lu 					     enum release_type rel_type)
14895009b460SYinghai Lu {
14905009b460SYinghai Lu 	struct pci_dev *dev;
14915009b460SYinghai Lu 	bool is_leaf_bridge = true;
14925009b460SYinghai Lu 
14935009b460SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
14945009b460SYinghai Lu 		struct pci_bus *b = dev->subordinate;
14955009b460SYinghai Lu 		if (!b)
14965009b460SYinghai Lu 			continue;
14975009b460SYinghai Lu 
14985009b460SYinghai Lu 		is_leaf_bridge = false;
14995009b460SYinghai Lu 
15005009b460SYinghai Lu 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
15015009b460SYinghai Lu 			continue;
15025009b460SYinghai Lu 
15035009b460SYinghai Lu 		if (rel_type == whole_subtree)
15045009b460SYinghai Lu 			pci_bus_release_bridge_resources(b, type,
15055009b460SYinghai Lu 						 whole_subtree);
15065009b460SYinghai Lu 	}
15075009b460SYinghai Lu 
15085009b460SYinghai Lu 	if (pci_is_root_bus(bus))
15095009b460SYinghai Lu 		return;
15105009b460SYinghai Lu 
15115009b460SYinghai Lu 	if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
15125009b460SYinghai Lu 		return;
15135009b460SYinghai Lu 
15145009b460SYinghai Lu 	if ((rel_type == whole_subtree) || is_leaf_bridge)
15155009b460SYinghai Lu 		pci_bridge_release_resources(bus, type);
15165009b460SYinghai Lu }
15175009b460SYinghai Lu 
151876fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
151976fbc263SYinghai Lu {
152089a74eccSBjorn Helgaas 	struct resource *res;
152176fbc263SYinghai Lu 	int i;
152276fbc263SYinghai Lu 
152389a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
15247c9342b8SYinghai Lu 		if (!res || !res->end || !res->flags)
152576fbc263SYinghai Lu 			continue;
152676fbc263SYinghai Lu 
1527c7dabef8SBjorn Helgaas 		dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
152876fbc263SYinghai Lu 	}
152976fbc263SYinghai Lu }
153076fbc263SYinghai Lu 
153176fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
153276fbc263SYinghai Lu {
153376fbc263SYinghai Lu 	struct pci_bus *b;
153476fbc263SYinghai Lu 	struct pci_dev *dev;
153576fbc263SYinghai Lu 
153676fbc263SYinghai Lu 
153776fbc263SYinghai Lu 	pci_bus_dump_res(bus);
153876fbc263SYinghai Lu 
153976fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
154076fbc263SYinghai Lu 		b = dev->subordinate;
154176fbc263SYinghai Lu 		if (!b)
154276fbc263SYinghai Lu 			continue;
154376fbc263SYinghai Lu 
154476fbc263SYinghai Lu 		pci_bus_dump_resources(b);
154576fbc263SYinghai Lu 	}
154676fbc263SYinghai Lu }
154776fbc263SYinghai Lu 
1548ff35147cSYinghai Lu static int pci_bus_get_depth(struct pci_bus *bus)
1549da7822e5SYinghai Lu {
1550da7822e5SYinghai Lu 	int depth = 0;
1551f2a230bdSWei Yang 	struct pci_bus *child_bus;
1552da7822e5SYinghai Lu 
1553f2a230bdSWei Yang 	list_for_each_entry(child_bus, &bus->children, node) {
1554da7822e5SYinghai Lu 		int ret;
1555da7822e5SYinghai Lu 
1556f2a230bdSWei Yang 		ret = pci_bus_get_depth(child_bus);
1557da7822e5SYinghai Lu 		if (ret + 1 > depth)
1558da7822e5SYinghai Lu 			depth = ret + 1;
1559da7822e5SYinghai Lu 	}
1560da7822e5SYinghai Lu 
1561da7822e5SYinghai Lu 	return depth;
1562da7822e5SYinghai Lu }
1563da7822e5SYinghai Lu 
1564b55438fdSYinghai Lu /*
1565b55438fdSYinghai Lu  * -1: undefined, will auto detect later
1566b55438fdSYinghai Lu  *  0: disabled by user
1567b55438fdSYinghai Lu  *  1: disabled by auto detect
1568b55438fdSYinghai Lu  *  2: enabled by user
1569b55438fdSYinghai Lu  *  3: enabled by auto detect
1570b55438fdSYinghai Lu  */
1571b55438fdSYinghai Lu enum enable_type {
1572b55438fdSYinghai Lu 	undefined = -1,
1573b55438fdSYinghai Lu 	user_disabled,
1574b55438fdSYinghai Lu 	auto_disabled,
1575b55438fdSYinghai Lu 	user_enabled,
1576b55438fdSYinghai Lu 	auto_enabled,
1577b55438fdSYinghai Lu };
1578b55438fdSYinghai Lu 
1579ff35147cSYinghai Lu static enum enable_type pci_realloc_enable = undefined;
1580b55438fdSYinghai Lu void __init pci_realloc_get_opt(char *str)
1581b55438fdSYinghai Lu {
1582b55438fdSYinghai Lu 	if (!strncmp(str, "off", 3))
1583b55438fdSYinghai Lu 		pci_realloc_enable = user_disabled;
1584b55438fdSYinghai Lu 	else if (!strncmp(str, "on", 2))
1585b55438fdSYinghai Lu 		pci_realloc_enable = user_enabled;
1586b55438fdSYinghai Lu }
1587ff35147cSYinghai Lu static bool pci_realloc_enabled(enum enable_type enable)
1588b55438fdSYinghai Lu {
1589967260cdSYinghai Lu 	return enable >= user_enabled;
1590b55438fdSYinghai Lu }
1591f483d392SRam Pai 
1592b07f2ebcSYinghai Lu #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1593ff35147cSYinghai Lu static int iov_resources_unassigned(struct pci_dev *dev, void *data)
1594223d96fcSYinghai Lu {
1595b07f2ebcSYinghai Lu 	int i;
1596223d96fcSYinghai Lu 	bool *unassigned = data;
1597b07f2ebcSYinghai Lu 
1598b07f2ebcSYinghai Lu 	for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) {
1599b07f2ebcSYinghai Lu 		struct resource *r = &dev->resource[i];
1600fa216bf4SYinghai Lu 		struct pci_bus_region region;
1601b07f2ebcSYinghai Lu 
1602223d96fcSYinghai Lu 		/* Not assigned or rejected by kernel? */
1603fa216bf4SYinghai Lu 		if (!r->flags)
1604fa216bf4SYinghai Lu 			continue;
1605b07f2ebcSYinghai Lu 
1606fc279850SYinghai Lu 		pcibios_resource_to_bus(dev->bus, &region, r);
1607fa216bf4SYinghai Lu 		if (!region.start) {
1608223d96fcSYinghai Lu 			*unassigned = true;
1609223d96fcSYinghai Lu 			return 1; /* return early from pci_walk_bus() */
1610b07f2ebcSYinghai Lu 		}
1611b07f2ebcSYinghai Lu 	}
1612b07f2ebcSYinghai Lu 
1613223d96fcSYinghai Lu 	return 0;
1614223d96fcSYinghai Lu }
1615223d96fcSYinghai Lu 
1616ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1617967260cdSYinghai Lu 			 enum enable_type enable_local)
1618223d96fcSYinghai Lu {
1619223d96fcSYinghai Lu 	bool unassigned = false;
1620223d96fcSYinghai Lu 
1621967260cdSYinghai Lu 	if (enable_local != undefined)
1622967260cdSYinghai Lu 		return enable_local;
1623223d96fcSYinghai Lu 
1624223d96fcSYinghai Lu 	pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
1625967260cdSYinghai Lu 	if (unassigned)
1626967260cdSYinghai Lu 		return auto_enabled;
1627967260cdSYinghai Lu 
1628967260cdSYinghai Lu 	return enable_local;
1629b07f2ebcSYinghai Lu }
1630223d96fcSYinghai Lu #else
1631ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1632967260cdSYinghai Lu 			 enum enable_type enable_local)
1633967260cdSYinghai Lu {
1634967260cdSYinghai Lu 	return enable_local;
1635b07f2ebcSYinghai Lu }
1636b07f2ebcSYinghai Lu #endif
1637b07f2ebcSYinghai Lu 
1638da7822e5SYinghai Lu /*
1639da7822e5SYinghai Lu  * first try will not touch pci bridge res
1640da7822e5SYinghai Lu  * second and later try will clear small leaf bridge res
1641f7625980SBjorn Helgaas  * will stop till to the max depth if can not find good one
1642da7822e5SYinghai Lu  */
164339772038SYinghai Lu void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
16441da177e4SLinus Torvalds {
1645bdc4abecSYinghai Lu 	LIST_HEAD(realloc_head); /* list of resources that
1646c8adf9a3SRam Pai 					want additional resources */
1647bdc4abecSYinghai Lu 	struct list_head *add_list = NULL;
1648da7822e5SYinghai Lu 	int tried_times = 0;
1649da7822e5SYinghai Lu 	enum release_type rel_type = leaf_only;
1650bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
1651b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
1652da7822e5SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
16535b285415SYinghai Lu 				  IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
165419aa7ee4SYinghai Lu 	int pci_try_num = 1;
165555ed83a6SYinghai Lu 	enum enable_type enable_local;
1656da7822e5SYinghai Lu 
165719aa7ee4SYinghai Lu 	/* don't realloc if asked to do so */
165855ed83a6SYinghai Lu 	enable_local = pci_realloc_detect(bus, pci_realloc_enable);
1659967260cdSYinghai Lu 	if (pci_realloc_enabled(enable_local)) {
166055ed83a6SYinghai Lu 		int max_depth = pci_bus_get_depth(bus);
166119aa7ee4SYinghai Lu 
1662da7822e5SYinghai Lu 		pci_try_num = max_depth + 1;
166355ed83a6SYinghai Lu 		dev_printk(KERN_DEBUG, &bus->dev,
166455ed83a6SYinghai Lu 			   "max bus depth: %d pci_try_num: %d\n",
1665da7822e5SYinghai Lu 			   max_depth, pci_try_num);
166619aa7ee4SYinghai Lu 	}
1667da7822e5SYinghai Lu 
1668da7822e5SYinghai Lu again:
166919aa7ee4SYinghai Lu 	/*
167019aa7ee4SYinghai Lu 	 * last try will use add_list, otherwise will try good to have as
167119aa7ee4SYinghai Lu 	 * must have, so can realloc parent bridge resource
167219aa7ee4SYinghai Lu 	 */
167319aa7ee4SYinghai Lu 	if (tried_times + 1 == pci_try_num)
1674bdc4abecSYinghai Lu 		add_list = &realloc_head;
16751da177e4SLinus Torvalds 	/* Depth first, calculate sizes and alignments of all
16761da177e4SLinus Torvalds 	   subordinate buses. */
167719aa7ee4SYinghai Lu 	__pci_bus_size_bridges(bus, add_list);
1678c8adf9a3SRam Pai 
16791da177e4SLinus Torvalds 	/* Depth last, allocate resources and update the hardware. */
1680bdc4abecSYinghai Lu 	__pci_bus_assign_resources(bus, add_list, &fail_head);
168119aa7ee4SYinghai Lu 	if (add_list)
1682bdc4abecSYinghai Lu 		BUG_ON(!list_empty(add_list));
1683da7822e5SYinghai Lu 	tried_times++;
1684da7822e5SYinghai Lu 
1685da7822e5SYinghai Lu 	/* any device complain? */
1686bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
1687928bea96SYinghai Lu 		goto dump;
1688f483d392SRam Pai 
16890c5be0cbSYinghai Lu 	if (tried_times >= pci_try_num) {
1690967260cdSYinghai Lu 		if (enable_local == undefined)
169155ed83a6SYinghai Lu 			dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
1692967260cdSYinghai Lu 		else if (enable_local == auto_enabled)
169355ed83a6SYinghai Lu 			dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
1694eb572e7cSYinghai Lu 
1695bffc56d4SYinghai Lu 		free_list(&fail_head);
1696928bea96SYinghai Lu 		goto dump;
1697da7822e5SYinghai Lu 	}
1698da7822e5SYinghai Lu 
169955ed83a6SYinghai Lu 	dev_printk(KERN_DEBUG, &bus->dev,
170055ed83a6SYinghai Lu 		   "No. %d try to assign unassigned res\n", tried_times + 1);
1701da7822e5SYinghai Lu 
1702da7822e5SYinghai Lu 	/* third times and later will not check if it is leaf */
1703da7822e5SYinghai Lu 	if ((tried_times + 1) > 2)
1704da7822e5SYinghai Lu 		rel_type = whole_subtree;
1705da7822e5SYinghai Lu 
1706da7822e5SYinghai Lu 	/*
1707da7822e5SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
1708da7822e5SYinghai Lu 	 * child device under that bridge
1709da7822e5SYinghai Lu 	 */
171061e83cddSYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list)
171161e83cddSYinghai Lu 		pci_bus_release_bridge_resources(fail_res->dev->bus,
1712b9b0bba9SYinghai Lu 						 fail_res->flags & type_mask,
1713da7822e5SYinghai Lu 						 rel_type);
171461e83cddSYinghai Lu 
1715da7822e5SYinghai Lu 	/* restore size and flags */
1716b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1717b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
1718da7822e5SYinghai Lu 
1719b9b0bba9SYinghai Lu 		res->start = fail_res->start;
1720b9b0bba9SYinghai Lu 		res->end = fail_res->end;
1721b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
1722b9b0bba9SYinghai Lu 		if (fail_res->dev->subordinate)
1723da7822e5SYinghai Lu 			res->flags = 0;
1724da7822e5SYinghai Lu 	}
1725bffc56d4SYinghai Lu 	free_list(&fail_head);
1726da7822e5SYinghai Lu 
1727da7822e5SYinghai Lu 	goto again;
1728da7822e5SYinghai Lu 
1729928bea96SYinghai Lu dump:
173076fbc263SYinghai Lu 	/* dump the resource on buses */
173176fbc263SYinghai Lu 	pci_bus_dump_resources(bus);
173276fbc263SYinghai Lu }
17336841ec68SYinghai Lu 
173455ed83a6SYinghai Lu void __init pci_assign_unassigned_resources(void)
173555ed83a6SYinghai Lu {
173655ed83a6SYinghai Lu 	struct pci_bus *root_bus;
173755ed83a6SYinghai Lu 
173855ed83a6SYinghai Lu 	list_for_each_entry(root_bus, &pci_root_buses, node)
173955ed83a6SYinghai Lu 		pci_assign_unassigned_root_bus_resources(root_bus);
174055ed83a6SYinghai Lu }
174155ed83a6SYinghai Lu 
17426841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
17436841ec68SYinghai Lu {
17446841ec68SYinghai Lu 	struct pci_bus *parent = bridge->subordinate;
1745bdc4abecSYinghai Lu 	LIST_HEAD(add_list); /* list of resources that
17468424d759SYinghai Lu 					want additional resources */
174732180e40SYinghai Lu 	int tried_times = 0;
1748bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
1749b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
17506841ec68SYinghai Lu 	int retval;
175132180e40SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1752d61b0e87SYinghai Lu 				  IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
17536841ec68SYinghai Lu 
175432180e40SYinghai Lu again:
17558424d759SYinghai Lu 	__pci_bus_size_bridges(parent, &add_list);
1756bdc4abecSYinghai Lu 	__pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1757bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
175832180e40SYinghai Lu 	tried_times++;
175932180e40SYinghai Lu 
1760bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
17613f579c34SYinghai Lu 		goto enable_all;
176232180e40SYinghai Lu 
176332180e40SYinghai Lu 	if (tried_times >= 2) {
176432180e40SYinghai Lu 		/* still fail, don't need to try more */
1765bffc56d4SYinghai Lu 		free_list(&fail_head);
17663f579c34SYinghai Lu 		goto enable_all;
176732180e40SYinghai Lu 	}
176832180e40SYinghai Lu 
176932180e40SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
177032180e40SYinghai Lu 			 tried_times + 1);
177132180e40SYinghai Lu 
177232180e40SYinghai Lu 	/*
177332180e40SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
177432180e40SYinghai Lu 	 * child device under that bridge
177532180e40SYinghai Lu 	 */
177661e83cddSYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list)
177761e83cddSYinghai Lu 		pci_bus_release_bridge_resources(fail_res->dev->bus,
177861e83cddSYinghai Lu 						 fail_res->flags & type_mask,
177932180e40SYinghai Lu 						 whole_subtree);
178061e83cddSYinghai Lu 
178132180e40SYinghai Lu 	/* restore size and flags */
1782b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1783b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
178432180e40SYinghai Lu 
1785b9b0bba9SYinghai Lu 		res->start = fail_res->start;
1786b9b0bba9SYinghai Lu 		res->end = fail_res->end;
1787b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
1788b9b0bba9SYinghai Lu 		if (fail_res->dev->subordinate)
178932180e40SYinghai Lu 			res->flags = 0;
179032180e40SYinghai Lu 	}
1791bffc56d4SYinghai Lu 	free_list(&fail_head);
179232180e40SYinghai Lu 
179332180e40SYinghai Lu 	goto again;
17943f579c34SYinghai Lu 
17953f579c34SYinghai Lu enable_all:
17963f579c34SYinghai Lu 	retval = pci_reenable_device(bridge);
17979fc9eea0SBjorn Helgaas 	if (retval)
17989fc9eea0SBjorn Helgaas 		dev_err(&bridge->dev, "Error reenabling bridge (%d)\n", retval);
17993f579c34SYinghai Lu 	pci_set_master(bridge);
18006841ec68SYinghai Lu }
18016841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
18029b03088fSYinghai Lu 
180317787940SYinghai Lu void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
18049b03088fSYinghai Lu {
18059b03088fSYinghai Lu 	struct pci_dev *dev;
1806bdc4abecSYinghai Lu 	LIST_HEAD(add_list); /* list of resources that
18079b03088fSYinghai Lu 					want additional resources */
18089b03088fSYinghai Lu 
18099b03088fSYinghai Lu 	down_read(&pci_bus_sem);
18109b03088fSYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
18116788a51fSYijing Wang 		if (pci_is_bridge(dev) && pci_has_subordinate(dev))
18129b03088fSYinghai Lu 				__pci_bus_size_bridges(dev->subordinate,
18139b03088fSYinghai Lu 							 &add_list);
18149b03088fSYinghai Lu 	up_read(&pci_bus_sem);
18159b03088fSYinghai Lu 	__pci_bus_assign_resources(bus, &add_list, NULL);
1816bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
181717787940SYinghai Lu }
1818e6b29deaSRay Jui EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);
1819