xref: /openbmc/linux/drivers/pci/setup-bus.c (revision bffc56d41102705d809f88c29918a9c33d2900f7)
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>
286faf17f6SChris Wright #include "pci.h"
291da177e4SLinus Torvalds 
30bdc4abecSYinghai Lu struct pci_dev_resource {
31bdc4abecSYinghai Lu 	struct list_head list;
322934a0deSYinghai Lu 	struct resource *res;
332934a0deSYinghai Lu 	struct pci_dev *dev;
34568ddef8SYinghai Lu 	resource_size_t start;
35568ddef8SYinghai Lu 	resource_size_t end;
36c8adf9a3SRam Pai 	resource_size_t add_size;
372bbc6942SRam Pai 	resource_size_t min_align;
38568ddef8SYinghai Lu 	unsigned long flags;
39568ddef8SYinghai Lu };
40568ddef8SYinghai Lu 
41*bffc56d4SYinghai Lu static void free_list(struct list_head *head)
42*bffc56d4SYinghai Lu {
43*bffc56d4SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
44*bffc56d4SYinghai Lu 
45*bffc56d4SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
46*bffc56d4SYinghai Lu 		list_del(&dev_res->list);
47*bffc56d4SYinghai Lu 		kfree(dev_res);
48*bffc56d4SYinghai Lu 	}
49*bffc56d4SYinghai Lu }
50094732a5SRam Pai 
51f483d392SRam Pai int pci_realloc_enable = 0;
52f483d392SRam Pai #define pci_realloc_enabled() pci_realloc_enable
53f483d392SRam Pai void pci_realloc(void)
54f483d392SRam Pai {
55f483d392SRam Pai 	pci_realloc_enable = 1;
56f483d392SRam Pai }
57f483d392SRam Pai 
58c8adf9a3SRam Pai /**
59c8adf9a3SRam Pai  * add_to_list() - add a new resource tracker to the list
60c8adf9a3SRam Pai  * @head:	Head of the list
61c8adf9a3SRam Pai  * @dev:	device corresponding to which the resource
62c8adf9a3SRam Pai  *		belongs
63c8adf9a3SRam Pai  * @res:	The resource to be tracked
64c8adf9a3SRam Pai  * @add_size:	additional size to be optionally added
65c8adf9a3SRam Pai  *              to the resource
66c8adf9a3SRam Pai  */
67bdc4abecSYinghai Lu static int add_to_list(struct list_head *head,
68c8adf9a3SRam Pai 		 struct pci_dev *dev, struct resource *res,
692bbc6942SRam Pai 		 resource_size_t add_size, resource_size_t min_align)
70568ddef8SYinghai Lu {
71764242a0SYinghai Lu 	struct pci_dev_resource *tmp;
72568ddef8SYinghai Lu 
73bdc4abecSYinghai Lu 	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
74568ddef8SYinghai Lu 	if (!tmp) {
75c8adf9a3SRam Pai 		pr_warning("add_to_list: kmalloc() failed!\n");
76ef62dfefSYinghai Lu 		return -ENOMEM;
77568ddef8SYinghai Lu 	}
78568ddef8SYinghai Lu 
79568ddef8SYinghai Lu 	tmp->res = res;
80568ddef8SYinghai Lu 	tmp->dev = dev;
81568ddef8SYinghai Lu 	tmp->start = res->start;
82568ddef8SYinghai Lu 	tmp->end = res->end;
83568ddef8SYinghai Lu 	tmp->flags = res->flags;
84c8adf9a3SRam Pai 	tmp->add_size = add_size;
852bbc6942SRam Pai 	tmp->min_align = min_align;
86bdc4abecSYinghai Lu 
87bdc4abecSYinghai Lu 	list_add(&tmp->list, head);
88ef62dfefSYinghai Lu 
89ef62dfefSYinghai Lu 	return 0;
90568ddef8SYinghai Lu }
91568ddef8SYinghai Lu 
92bdc4abecSYinghai Lu static void add_to_failed_list(struct list_head *head,
93c8adf9a3SRam Pai 				struct pci_dev *dev, struct resource *res)
94c8adf9a3SRam Pai {
952bbc6942SRam Pai 	add_to_list(head, dev, res,
962bbc6942SRam Pai 			0 /* dont care */,
972bbc6942SRam Pai 			0 /* dont care */);
98c8adf9a3SRam Pai }
99c8adf9a3SRam Pai 
100b9b0bba9SYinghai Lu static void remove_from_list(struct list_head *head,
1013e6e0d80SYinghai Lu 				 struct resource *res)
1023e6e0d80SYinghai Lu {
103b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
1043e6e0d80SYinghai Lu 
105b9b0bba9SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
106b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
107b9b0bba9SYinghai Lu 			list_del(&dev_res->list);
108b9b0bba9SYinghai Lu 			kfree(dev_res);
109bdc4abecSYinghai Lu 			break;
1103e6e0d80SYinghai Lu 		}
1113e6e0d80SYinghai Lu 	}
1123e6e0d80SYinghai Lu }
1133e6e0d80SYinghai Lu 
114b9b0bba9SYinghai Lu static resource_size_t get_res_add_size(struct list_head *head,
1151c372353SYinghai Lu 					struct resource *res)
1161c372353SYinghai Lu {
117b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res;
1181c372353SYinghai Lu 
119b9b0bba9SYinghai Lu 	list_for_each_entry(dev_res, head, list) {
120b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
121b9b0bba9SYinghai Lu 			dev_printk(KERN_DEBUG, &dev_res->dev->dev,
1223e6e0d80SYinghai Lu 				 "%pR get_res_add_size  add_size %llx\n",
123b9b0bba9SYinghai Lu 				 dev_res->res,
124b9b0bba9SYinghai Lu 				 (unsigned long long)dev_res->add_size);
125b9b0bba9SYinghai Lu 			return dev_res->add_size;
126bdc4abecSYinghai Lu 		}
1273e6e0d80SYinghai Lu 	}
1281c372353SYinghai Lu 
1291c372353SYinghai Lu 	return 0;
1301c372353SYinghai Lu }
1311c372353SYinghai Lu 
13278c3b329SYinghai Lu /* Sort resources by alignment */
133bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
13478c3b329SYinghai Lu {
13578c3b329SYinghai Lu 	int i;
13678c3b329SYinghai Lu 
13778c3b329SYinghai Lu 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
13878c3b329SYinghai Lu 		struct resource *r;
139bdc4abecSYinghai Lu 		struct pci_dev_resource *dev_res, *tmp;
14078c3b329SYinghai Lu 		resource_size_t r_align;
141bdc4abecSYinghai Lu 		struct list_head *n;
14278c3b329SYinghai Lu 
14378c3b329SYinghai Lu 		r = &dev->resource[i];
14478c3b329SYinghai Lu 
14578c3b329SYinghai Lu 		if (r->flags & IORESOURCE_PCI_FIXED)
14678c3b329SYinghai Lu 			continue;
14778c3b329SYinghai Lu 
14878c3b329SYinghai Lu 		if (!(r->flags) || r->parent)
14978c3b329SYinghai Lu 			continue;
15078c3b329SYinghai Lu 
15178c3b329SYinghai Lu 		r_align = pci_resource_alignment(dev, r);
15278c3b329SYinghai Lu 		if (!r_align) {
15378c3b329SYinghai Lu 			dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
15478c3b329SYinghai Lu 				 i, r);
15578c3b329SYinghai Lu 			continue;
15678c3b329SYinghai Lu 		}
15778c3b329SYinghai Lu 
158bdc4abecSYinghai Lu 		tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
15978c3b329SYinghai Lu 		if (!tmp)
16078c3b329SYinghai Lu 			panic("pdev_sort_resources(): "
16178c3b329SYinghai Lu 			      "kmalloc() failed!\n");
16278c3b329SYinghai Lu 		tmp->res = r;
16378c3b329SYinghai Lu 		tmp->dev = dev;
164bdc4abecSYinghai Lu 
165bdc4abecSYinghai Lu 		/* fallback is smallest one or list is empty*/
166bdc4abecSYinghai Lu 		n = head;
167bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
168bdc4abecSYinghai Lu 			resource_size_t align;
169bdc4abecSYinghai Lu 
170bdc4abecSYinghai Lu 			align = pci_resource_alignment(dev_res->dev,
171bdc4abecSYinghai Lu 							 dev_res->res);
172bdc4abecSYinghai Lu 
173bdc4abecSYinghai Lu 			if (r_align > align) {
174bdc4abecSYinghai Lu 				n = &dev_res->list;
17578c3b329SYinghai Lu 				break;
17678c3b329SYinghai Lu 			}
17778c3b329SYinghai Lu 		}
178bdc4abecSYinghai Lu 		/* Insert it just before n*/
179bdc4abecSYinghai Lu 		list_add_tail(&tmp->list, n);
18078c3b329SYinghai Lu 	}
18178c3b329SYinghai Lu }
18278c3b329SYinghai Lu 
1836841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev,
184bdc4abecSYinghai Lu 				 struct list_head *head)
1851da177e4SLinus Torvalds {
1861da177e4SLinus Torvalds 	u16 class = dev->class >> 8;
1871da177e4SLinus Torvalds 
1889bded00bSKenji Kaneshige 	/* Don't touch classless devices or host bridges or ioapics.  */
1896841ec68SYinghai Lu 	if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
1906841ec68SYinghai Lu 		return;
1911da177e4SLinus Torvalds 
1929bded00bSKenji Kaneshige 	/* Don't touch ioapic devices already enabled by firmware */
19323186279SSatoru Takeuchi 	if (class == PCI_CLASS_SYSTEM_PIC) {
1949bded00bSKenji Kaneshige 		u16 command;
1959bded00bSKenji Kaneshige 		pci_read_config_word(dev, PCI_COMMAND, &command);
1969bded00bSKenji Kaneshige 		if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
1976841ec68SYinghai Lu 			return;
19823186279SSatoru Takeuchi 	}
19923186279SSatoru Takeuchi 
2006841ec68SYinghai Lu 	pdev_sort_resources(dev, head);
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
203fc075e1dSRam Pai static inline void reset_resource(struct resource *res)
204fc075e1dSRam Pai {
205fc075e1dSRam Pai 	res->start = 0;
206fc075e1dSRam Pai 	res->end = 0;
207fc075e1dSRam Pai 	res->flags = 0;
208fc075e1dSRam Pai }
209fc075e1dSRam Pai 
210c8adf9a3SRam Pai /**
2119e8bf93aSRam Pai  * reassign_resources_sorted() - satisfy any additional resource requests
212c8adf9a3SRam Pai  *
2139e8bf93aSRam Pai  * @realloc_head : head of the list tracking requests requiring additional
214c8adf9a3SRam Pai  *             resources
215c8adf9a3SRam Pai  * @head     : head of the list tracking requests with allocated
216c8adf9a3SRam Pai  *             resources
217c8adf9a3SRam Pai  *
2189e8bf93aSRam Pai  * Walk through each element of the realloc_head and try to procure
219c8adf9a3SRam Pai  * additional resources for the element, provided the element
220c8adf9a3SRam Pai  * is in the head list.
221c8adf9a3SRam Pai  */
222bdc4abecSYinghai Lu static void reassign_resources_sorted(struct list_head *realloc_head,
223bdc4abecSYinghai Lu 		struct list_head *head)
224c8adf9a3SRam Pai {
225c8adf9a3SRam Pai 	struct resource *res;
226b9b0bba9SYinghai Lu 	struct pci_dev_resource *add_res, *tmp;
227bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
228c8adf9a3SRam Pai 	resource_size_t add_size;
229c8adf9a3SRam Pai 	int idx;
230c8adf9a3SRam Pai 
231b9b0bba9SYinghai Lu 	list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
232bdc4abecSYinghai Lu 		bool found_match = false;
233bdc4abecSYinghai Lu 
234b9b0bba9SYinghai Lu 		res = add_res->res;
235c8adf9a3SRam Pai 		/* skip resource that has been reset */
236c8adf9a3SRam Pai 		if (!res->flags)
237c8adf9a3SRam Pai 			goto out;
238c8adf9a3SRam Pai 
239c8adf9a3SRam Pai 		/* skip this resource if not found in head list */
240bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
241bdc4abecSYinghai Lu 			if (dev_res->res == res) {
242bdc4abecSYinghai Lu 				found_match = true;
243bdc4abecSYinghai Lu 				break;
244c8adf9a3SRam Pai 			}
245bdc4abecSYinghai Lu 		}
246bdc4abecSYinghai Lu 		if (!found_match)/* just skip */
247bdc4abecSYinghai Lu 			continue;
248c8adf9a3SRam Pai 
249b9b0bba9SYinghai Lu 		idx = res - &add_res->dev->resource[0];
250b9b0bba9SYinghai Lu 		add_size = add_res->add_size;
2512bbc6942SRam Pai 		if (!resource_size(res)) {
252b9b0bba9SYinghai Lu 			res->start = add_res->start;
253c8adf9a3SRam Pai 			res->end = res->start + add_size - 1;
254b9b0bba9SYinghai Lu 			if (pci_assign_resource(add_res->dev, idx))
255c8adf9a3SRam Pai 				reset_resource(res);
2562bbc6942SRam Pai 		} else {
257b9b0bba9SYinghai Lu 			resource_size_t align = add_res->min_align;
258b9b0bba9SYinghai Lu 			res->flags |= add_res->flags &
259bdc4abecSYinghai Lu 				 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
260b9b0bba9SYinghai Lu 			if (pci_reassign_resource(add_res->dev, idx,
261bdc4abecSYinghai Lu 						  add_size, align))
262b9b0bba9SYinghai Lu 				dev_printk(KERN_DEBUG, &add_res->dev->dev,
263bdc4abecSYinghai Lu 				   "failed to add optional resources res=%pR\n",
2642bbc6942SRam Pai 							res);
265c8adf9a3SRam Pai 		}
266c8adf9a3SRam Pai out:
267b9b0bba9SYinghai Lu 		list_del(&add_res->list);
268b9b0bba9SYinghai Lu 		kfree(add_res);
269c8adf9a3SRam Pai 	}
270c8adf9a3SRam Pai }
271c8adf9a3SRam Pai 
272c8adf9a3SRam Pai /**
273c8adf9a3SRam Pai  * assign_requested_resources_sorted() - satisfy resource requests
274c8adf9a3SRam Pai  *
275c8adf9a3SRam Pai  * @head : head of the list tracking requests for resources
276c8adf9a3SRam Pai  * @failed_list : head of the list tracking requests that could
277c8adf9a3SRam Pai  *		not be allocated
278c8adf9a3SRam Pai  *
279c8adf9a3SRam Pai  * Satisfy resource requests of each element in the list. Add
280c8adf9a3SRam Pai  * requests that could not satisfied to the failed_list.
281c8adf9a3SRam Pai  */
282bdc4abecSYinghai Lu static void assign_requested_resources_sorted(struct list_head *head,
283bdc4abecSYinghai Lu 				 struct list_head *fail_head)
2846841ec68SYinghai Lu {
2856841ec68SYinghai Lu 	struct resource *res;
286bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
2876841ec68SYinghai Lu 	int idx;
2886841ec68SYinghai Lu 
289bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
290bdc4abecSYinghai Lu 		res = dev_res->res;
291bdc4abecSYinghai Lu 		idx = res - &dev_res->dev->resource[0];
292bdc4abecSYinghai Lu 		if (resource_size(res) &&
293bdc4abecSYinghai Lu 		    pci_assign_resource(dev_res->dev, idx)) {
294bdc4abecSYinghai Lu 			if (fail_head && !pci_is_root_bus(dev_res->dev->bus)) {
2959a928660SYinghai Lu 				/*
2969a928660SYinghai Lu 				 * if the failed res is for ROM BAR, and it will
2979a928660SYinghai Lu 				 * be enabled later, don't add it to the list
2989a928660SYinghai Lu 				 */
2999a928660SYinghai Lu 				if (!((idx == PCI_ROM_RESOURCE) &&
3009a928660SYinghai Lu 				      (!(res->flags & IORESOURCE_ROM_ENABLE))))
301bdc4abecSYinghai Lu 					add_to_failed_list(fail_head,
302bdc4abecSYinghai Lu 							   dev_res->dev, res);
3039a928660SYinghai Lu 			}
304fc075e1dSRam Pai 			reset_resource(res);
305542df5deSRajesh Shah 		}
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
309bdc4abecSYinghai Lu static void __assign_resources_sorted(struct list_head *head,
310bdc4abecSYinghai Lu 				 struct list_head *realloc_head,
311bdc4abecSYinghai Lu 				 struct list_head *fail_head)
312c8adf9a3SRam Pai {
3133e6e0d80SYinghai Lu 	/*
3143e6e0d80SYinghai Lu 	 * Should not assign requested resources at first.
3153e6e0d80SYinghai Lu 	 *   they could be adjacent, so later reassign can not reallocate
3163e6e0d80SYinghai Lu 	 *   them one by one in parent resource window.
3173e6e0d80SYinghai Lu 	 * Try to assign requested + add_size at begining
3183e6e0d80SYinghai Lu 	 *  if could do that, could get out early.
3193e6e0d80SYinghai Lu 	 *  if could not do that, we still try to assign requested at first,
3203e6e0d80SYinghai Lu 	 *    then try to reassign add_size for some resources.
3213e6e0d80SYinghai Lu 	 */
322bdc4abecSYinghai Lu 	LIST_HEAD(save_head);
323bdc4abecSYinghai Lu 	LIST_HEAD(local_fail_head);
324b9b0bba9SYinghai Lu 	struct pci_dev_resource *save_res;
325bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
3263e6e0d80SYinghai Lu 
3273e6e0d80SYinghai Lu 	/* Check if optional add_size is there */
328bdc4abecSYinghai Lu 	if (!realloc_head || list_empty(realloc_head))
3293e6e0d80SYinghai Lu 		goto requested_and_reassign;
3303e6e0d80SYinghai Lu 
3313e6e0d80SYinghai Lu 	/* Save original start, end, flags etc at first */
332bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
333bdc4abecSYinghai Lu 		if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
334*bffc56d4SYinghai Lu 			free_list(&save_head);
3353e6e0d80SYinghai Lu 			goto requested_and_reassign;
3363e6e0d80SYinghai Lu 		}
337bdc4abecSYinghai Lu 	}
3383e6e0d80SYinghai Lu 
3393e6e0d80SYinghai Lu 	/* Update res in head list with add_size in realloc_head list */
340bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list)
341bdc4abecSYinghai Lu 		dev_res->res->end += get_res_add_size(realloc_head,
342bdc4abecSYinghai Lu 							dev_res->res);
3433e6e0d80SYinghai Lu 
3443e6e0d80SYinghai Lu 	/* Try updated head list with add_size added */
3453e6e0d80SYinghai Lu 	assign_requested_resources_sorted(head, &local_fail_head);
3463e6e0d80SYinghai Lu 
3473e6e0d80SYinghai Lu 	/* all assigned with add_size ? */
348bdc4abecSYinghai Lu 	if (list_empty(&local_fail_head)) {
3493e6e0d80SYinghai Lu 		/* Remove head list from realloc_head list */
350bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list)
351bdc4abecSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
352*bffc56d4SYinghai Lu 		free_list(&save_head);
353*bffc56d4SYinghai Lu 		free_list(head);
3543e6e0d80SYinghai Lu 		return;
3553e6e0d80SYinghai Lu 	}
3563e6e0d80SYinghai Lu 
357*bffc56d4SYinghai Lu 	free_list(&local_fail_head);
3583e6e0d80SYinghai Lu 	/* Release assigned resource */
359bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list)
360bdc4abecSYinghai Lu 		if (dev_res->res->parent)
361bdc4abecSYinghai Lu 			release_resource(dev_res->res);
3623e6e0d80SYinghai Lu 	/* Restore start/end/flags from saved list */
363b9b0bba9SYinghai Lu 	list_for_each_entry(save_res, &save_head, list) {
364b9b0bba9SYinghai Lu 		struct resource *res = save_res->res;
3653e6e0d80SYinghai Lu 
366b9b0bba9SYinghai Lu 		res->start = save_res->start;
367b9b0bba9SYinghai Lu 		res->end = save_res->end;
368b9b0bba9SYinghai Lu 		res->flags = save_res->flags;
3693e6e0d80SYinghai Lu 	}
370*bffc56d4SYinghai Lu 	free_list(&save_head);
3713e6e0d80SYinghai Lu 
3723e6e0d80SYinghai Lu requested_and_reassign:
373c8adf9a3SRam Pai 	/* Satisfy the must-have resource requests */
374c8adf9a3SRam Pai 	assign_requested_resources_sorted(head, fail_head);
375c8adf9a3SRam Pai 
3760a2daa1cSRam Pai 	/* Try to satisfy any additional optional resource
377c8adf9a3SRam Pai 		requests */
3789e8bf93aSRam Pai 	if (realloc_head)
3799e8bf93aSRam Pai 		reassign_resources_sorted(realloc_head, head);
380*bffc56d4SYinghai Lu 	free_list(head);
381c8adf9a3SRam Pai }
382c8adf9a3SRam Pai 
3836841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev,
384bdc4abecSYinghai Lu 				 struct list_head *add_head,
385bdc4abecSYinghai Lu 				 struct list_head *fail_head)
3866841ec68SYinghai Lu {
387bdc4abecSYinghai Lu 	LIST_HEAD(head);
3886841ec68SYinghai Lu 
3896841ec68SYinghai Lu 	__dev_sort_resources(dev, &head);
3908424d759SYinghai Lu 	__assign_resources_sorted(&head, add_head, fail_head);
3916841ec68SYinghai Lu 
3926841ec68SYinghai Lu }
3936841ec68SYinghai Lu 
3946841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus,
395bdc4abecSYinghai Lu 					 struct list_head *realloc_head,
396bdc4abecSYinghai Lu 					 struct list_head *fail_head)
3976841ec68SYinghai Lu {
3986841ec68SYinghai Lu 	struct pci_dev *dev;
399bdc4abecSYinghai Lu 	LIST_HEAD(head);
4006841ec68SYinghai Lu 
4016841ec68SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
4026841ec68SYinghai Lu 		__dev_sort_resources(dev, &head);
4036841ec68SYinghai Lu 
4049e8bf93aSRam Pai 	__assign_resources_sorted(&head, realloc_head, fail_head);
4056841ec68SYinghai Lu }
4066841ec68SYinghai Lu 
407b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus)
4081da177e4SLinus Torvalds {
4091da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
410c7dabef8SBjorn Helgaas 	struct resource *res;
4111da177e4SLinus Torvalds 	struct pci_bus_region region;
4121da177e4SLinus Torvalds 
413865df576SBjorn Helgaas 	dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
414865df576SBjorn Helgaas 		 bus->secondary, bus->subordinate);
4151da177e4SLinus Torvalds 
416c7dabef8SBjorn Helgaas 	res = bus->resource[0];
417c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
418c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
4191da177e4SLinus Torvalds 		/*
4201da177e4SLinus Torvalds 		 * The IO resource is allocated a range twice as large as it
4211da177e4SLinus Torvalds 		 * would normally need.  This allows us to set both IO regs.
4221da177e4SLinus Torvalds 		 */
423c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4241da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
4251da177e4SLinus Torvalds 					region.start);
4261da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
4271da177e4SLinus Torvalds 					region.end);
4281da177e4SLinus Torvalds 	}
4291da177e4SLinus Torvalds 
430c7dabef8SBjorn Helgaas 	res = bus->resource[1];
431c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
432c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
433c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4341da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
4351da177e4SLinus Torvalds 					region.start);
4361da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
4371da177e4SLinus Torvalds 					region.end);
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds 
440c7dabef8SBjorn Helgaas 	res = bus->resource[2];
441c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
442c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
443c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4441da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
4451da177e4SLinus Torvalds 					region.start);
4461da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
4471da177e4SLinus Torvalds 					region.end);
4481da177e4SLinus Torvalds 	}
4491da177e4SLinus Torvalds 
450c7dabef8SBjorn Helgaas 	res = bus->resource[3];
451c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
452c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
453c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4541da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
4551da177e4SLinus Torvalds 					region.start);
4561da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
4571da177e4SLinus Torvalds 					region.end);
4581da177e4SLinus Torvalds 	}
4591da177e4SLinus Torvalds }
460b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus);
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected.
4631da177e4SLinus Torvalds    PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
4641da177e4SLinus Torvalds    requires that if there is no I/O ports or memory behind the
4651da177e4SLinus Torvalds    bridge, corresponding range must be turned off by writing base
4661da177e4SLinus Torvalds    value greater than limit to the bridge's base/limit registers.
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds    Note: care must be taken when updating I/O base/limit registers
4691da177e4SLinus Torvalds    of bridges which support 32-bit I/O. This update requires two
4701da177e4SLinus Torvalds    config space writes, so it's quite possible that an I/O window of
4711da177e4SLinus Torvalds    the bridge will have some undesirable address (e.g. 0) after the
4721da177e4SLinus Torvalds    first write. Ditto 64-bit prefetchable MMIO.  */
4737cc5997dSYinghai Lu static void pci_setup_bridge_io(struct pci_bus *bus)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
476c7dabef8SBjorn Helgaas 	struct resource *res;
4771da177e4SLinus Torvalds 	struct pci_bus_region region;
4787cc5997dSYinghai Lu 	u32 l, io_upper16;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	/* Set up the top and bottom of the PCI I/O segment for this bus. */
481c7dabef8SBjorn Helgaas 	res = bus->resource[0];
482c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
483c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
4841da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_IO_BASE, &l);
4851da177e4SLinus Torvalds 		l &= 0xffff0000;
4861da177e4SLinus Torvalds 		l |= (region.start >> 8) & 0x00f0;
4871da177e4SLinus Torvalds 		l |= region.end & 0xf000;
4881da177e4SLinus Torvalds 		/* Set up upper 16 bits of I/O base/limit. */
4891da177e4SLinus Torvalds 		io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
490c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4917cc5997dSYinghai Lu 	} else {
4921da177e4SLinus Torvalds 		/* Clear upper 16 bits of I/O base/limit. */
4931da177e4SLinus Torvalds 		io_upper16 = 0;
4941da177e4SLinus Torvalds 		l = 0x00f0;
4951da177e4SLinus Torvalds 	}
4961da177e4SLinus Torvalds 	/* Temporarily disable the I/O range before updating PCI_IO_BASE. */
4971da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
4981da177e4SLinus Torvalds 	/* Update lower 16 bits of I/O base/limit. */
4991da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE, l);
5001da177e4SLinus Torvalds 	/* Update upper 16 bits of I/O base/limit. */
5011da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
5027cc5997dSYinghai Lu }
5031da177e4SLinus Torvalds 
5047cc5997dSYinghai Lu static void pci_setup_bridge_mmio(struct pci_bus *bus)
5057cc5997dSYinghai Lu {
5067cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
5077cc5997dSYinghai Lu 	struct resource *res;
5087cc5997dSYinghai Lu 	struct pci_bus_region region;
5097cc5997dSYinghai Lu 	u32 l;
5107cc5997dSYinghai Lu 
5117cc5997dSYinghai Lu 	/* Set up the top and bottom of the PCI Memory segment for this bus. */
512c7dabef8SBjorn Helgaas 	res = bus->resource[1];
513c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
514c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
5151da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
5161da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
517c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5187cc5997dSYinghai Lu 	} else {
5191da177e4SLinus Torvalds 		l = 0x0000fff0;
5201da177e4SLinus Torvalds 	}
5211da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
5227cc5997dSYinghai Lu }
5237cc5997dSYinghai Lu 
5247cc5997dSYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
5257cc5997dSYinghai Lu {
5267cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
5277cc5997dSYinghai Lu 	struct resource *res;
5287cc5997dSYinghai Lu 	struct pci_bus_region region;
5297cc5997dSYinghai Lu 	u32 l, bu, lu;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	/* Clear out the upper 32 bits of PREF limit.
5321da177e4SLinus Torvalds 	   If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
5331da177e4SLinus Torvalds 	   disables PREF range, which is ok. */
5341da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	/* Set up PREF base/limit. */
537c40a22e0SBenjamin Herrenschmidt 	bu = lu = 0;
538c7dabef8SBjorn Helgaas 	res = bus->resource[2];
539c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
540c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_PREFETCH) {
5411da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
5421da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
543c7dabef8SBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64) {
54413d36c24SAndrew Morton 			bu = upper_32_bits(region.start);
54513d36c24SAndrew Morton 			lu = upper_32_bits(region.end);
5461f82de10SYinghai Lu 		}
547c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5487cc5997dSYinghai Lu 	} else {
5491da177e4SLinus Torvalds 		l = 0x0000fff0;
5501da177e4SLinus Torvalds 	}
5511da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
5521da177e4SLinus Torvalds 
553c40a22e0SBenjamin Herrenschmidt 	/* Set the upper 32 bits of PREF base & limit. */
554c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
555c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
5567cc5997dSYinghai Lu }
5577cc5997dSYinghai Lu 
5587cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
5597cc5997dSYinghai Lu {
5607cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
5617cc5997dSYinghai Lu 
5627cc5997dSYinghai Lu 	dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
5637cc5997dSYinghai Lu 		 bus->secondary, bus->subordinate);
5647cc5997dSYinghai Lu 
5657cc5997dSYinghai Lu 	if (type & IORESOURCE_IO)
5667cc5997dSYinghai Lu 		pci_setup_bridge_io(bus);
5677cc5997dSYinghai Lu 
5687cc5997dSYinghai Lu 	if (type & IORESOURCE_MEM)
5697cc5997dSYinghai Lu 		pci_setup_bridge_mmio(bus);
5707cc5997dSYinghai Lu 
5717cc5997dSYinghai Lu 	if (type & IORESOURCE_PREFETCH)
5727cc5997dSYinghai Lu 		pci_setup_bridge_mmio_pref(bus);
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
5751da177e4SLinus Torvalds }
5761da177e4SLinus Torvalds 
577e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus)
5787cc5997dSYinghai Lu {
5797cc5997dSYinghai Lu 	unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
5807cc5997dSYinghai Lu 				  IORESOURCE_PREFETCH;
5817cc5997dSYinghai Lu 
5827cc5997dSYinghai Lu 	__pci_setup_bridge(bus, type);
5837cc5997dSYinghai Lu }
5847cc5997dSYinghai Lu 
5851da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and
5861da177e4SLinus Torvalds    prefetchable memory ranges. If not, the respective
5871da177e4SLinus Torvalds    base/limit registers must be read-only and read as 0. */
58896bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus)
5891da177e4SLinus Torvalds {
5901da177e4SLinus Torvalds 	u16 io;
5911da177e4SLinus Torvalds 	u32 pmem;
5921da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
5931da177e4SLinus Torvalds 	struct resource *b_res;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
5961da177e4SLinus Torvalds 	b_res[1].flags |= IORESOURCE_MEM;
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_IO_BASE, &io);
5991da177e4SLinus Torvalds 	if (!io) {
6001da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
6011da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_IO_BASE, &io);
6021da177e4SLinus Torvalds  		pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
6031da177e4SLinus Torvalds  	}
6041da177e4SLinus Torvalds  	if (io)
6051da177e4SLinus Torvalds 		b_res[0].flags |= IORESOURCE_IO;
6061da177e4SLinus Torvalds 	/*  DECchip 21050 pass 2 errata: the bridge may miss an address
6071da177e4SLinus Torvalds 	    disconnect boundary by one PCI data phase.
6081da177e4SLinus Torvalds 	    Workaround: do not use prefetching on this device. */
6091da177e4SLinus Torvalds 	if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
6101da177e4SLinus Torvalds 		return;
6111da177e4SLinus Torvalds 	pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
6121da177e4SLinus Torvalds 	if (!pmem) {
6131da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
6141da177e4SLinus Torvalds 					       0xfff0fff0);
6151da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
6161da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
6171da177e4SLinus Torvalds 	}
6181f82de10SYinghai Lu 	if (pmem) {
6191da177e4SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
62099586105SYinghai Lu 		if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
62199586105SYinghai Lu 		    PCI_PREF_RANGE_TYPE_64) {
6221f82de10SYinghai Lu 			b_res[2].flags |= IORESOURCE_MEM_64;
62399586105SYinghai Lu 			b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
62499586105SYinghai Lu 		}
6251f82de10SYinghai Lu 	}
6261f82de10SYinghai Lu 
6271f82de10SYinghai Lu 	/* double check if bridge does support 64 bit pref */
6281f82de10SYinghai Lu 	if (b_res[2].flags & IORESOURCE_MEM_64) {
6291f82de10SYinghai Lu 		u32 mem_base_hi, tmp;
6301f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
6311f82de10SYinghai Lu 					 &mem_base_hi);
6321f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
6331f82de10SYinghai Lu 					       0xffffffff);
6341f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
6351f82de10SYinghai Lu 		if (!tmp)
6361f82de10SYinghai Lu 			b_res[2].flags &= ~IORESOURCE_MEM_64;
6371f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
6381f82de10SYinghai Lu 				       mem_base_hi);
6391f82de10SYinghai Lu 	}
6401da177e4SLinus Torvalds }
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds /* Helper function for sizing routines: find first available
6431da177e4SLinus Torvalds    bus resource of a given type. Note: we intentionally skip
6441da177e4SLinus Torvalds    the bus resources which have already been assigned (that is,
6451da177e4SLinus Torvalds    have non-NULL parent resource). */
64696bde06aSSam Ravnborg static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
6471da177e4SLinus Torvalds {
6481da177e4SLinus Torvalds 	int i;
6491da177e4SLinus Torvalds 	struct resource *r;
6501da177e4SLinus Torvalds 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
6511da177e4SLinus Torvalds 				  IORESOURCE_PREFETCH;
6521da177e4SLinus Torvalds 
65389a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, r, i) {
654299de034SIvan Kokshaysky 		if (r == &ioport_resource || r == &iomem_resource)
655299de034SIvan Kokshaysky 			continue;
65655a10984SJesse Barnes 		if (r && (r->flags & type_mask) == type && !r->parent)
6571da177e4SLinus Torvalds 			return r;
6581da177e4SLinus Torvalds 	}
6591da177e4SLinus Torvalds 	return NULL;
6601da177e4SLinus Torvalds }
6611da177e4SLinus Torvalds 
66213583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size,
66313583b16SRam Pai 		resource_size_t min_size,
66413583b16SRam Pai 		resource_size_t size1,
66513583b16SRam Pai 		resource_size_t old_size,
66613583b16SRam Pai 		resource_size_t align)
66713583b16SRam Pai {
66813583b16SRam Pai 	if (size < min_size)
66913583b16SRam Pai 		size = min_size;
67013583b16SRam Pai 	if (old_size == 1 )
67113583b16SRam Pai 		old_size = 0;
67213583b16SRam Pai 	/* To be fixed in 2.5: we should have sort of HAVE_ISA
67313583b16SRam Pai 	   flag in the struct pci_bus. */
67413583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
67513583b16SRam Pai 	size = (size & 0xff) + ((size & ~0xffUL) << 2);
67613583b16SRam Pai #endif
67713583b16SRam Pai 	size = ALIGN(size + size1, align);
67813583b16SRam Pai 	if (size < old_size)
67913583b16SRam Pai 		size = old_size;
68013583b16SRam Pai 	return size;
68113583b16SRam Pai }
68213583b16SRam Pai 
68313583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size,
68413583b16SRam Pai 		resource_size_t min_size,
68513583b16SRam Pai 		resource_size_t size1,
68613583b16SRam Pai 		resource_size_t old_size,
68713583b16SRam Pai 		resource_size_t align)
68813583b16SRam Pai {
68913583b16SRam Pai 	if (size < min_size)
69013583b16SRam Pai 		size = min_size;
69113583b16SRam Pai 	if (old_size == 1 )
69213583b16SRam Pai 		old_size = 0;
69313583b16SRam Pai 	if (size < old_size)
69413583b16SRam Pai 		size = old_size;
69513583b16SRam Pai 	size = ALIGN(size + size1, align);
69613583b16SRam Pai 	return size;
69713583b16SRam Pai }
69813583b16SRam Pai 
699c8adf9a3SRam Pai /**
700c8adf9a3SRam Pai  * pbus_size_io() - size the io window of a given bus
701c8adf9a3SRam Pai  *
702c8adf9a3SRam Pai  * @bus : the bus
703c8adf9a3SRam Pai  * @min_size : the minimum io window that must to be allocated
704c8adf9a3SRam Pai  * @add_size : additional optional io window
7059e8bf93aSRam Pai  * @realloc_head : track the additional io window on this list
706c8adf9a3SRam Pai  *
707c8adf9a3SRam Pai  * Sizing the IO windows of the PCI-PCI bridge is trivial,
708c8adf9a3SRam Pai  * since these windows have 4K granularity and the IO ranges
709c8adf9a3SRam Pai  * of non-bridge PCI devices are limited to 256 bytes.
710c8adf9a3SRam Pai  * We must be careful with the ISA aliasing though.
711c8adf9a3SRam Pai  */
712c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
713bdc4abecSYinghai Lu 		resource_size_t add_size, struct list_head *realloc_head)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	struct pci_dev *dev;
7161da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
717c8adf9a3SRam Pai 	unsigned long size = 0, size0 = 0, size1 = 0;
718be768912SYinghai Lu 	resource_size_t children_add_size = 0;
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	if (!b_res)
7211da177e4SLinus Torvalds  		return;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
7241da177e4SLinus Torvalds 		int i;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
7271da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
7281da177e4SLinus Torvalds 			unsigned long r_size;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 			if (r->parent || !(r->flags & IORESOURCE_IO))
7311da177e4SLinus Torvalds 				continue;
732022edd86SZhao, Yu 			r_size = resource_size(r);
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 			if (r_size < 0x400)
7351da177e4SLinus Torvalds 				/* Might be re-aligned for ISA */
7361da177e4SLinus Torvalds 				size += r_size;
7371da177e4SLinus Torvalds 			else
7381da177e4SLinus Torvalds 				size1 += r_size;
739be768912SYinghai Lu 
7409e8bf93aSRam Pai 			if (realloc_head)
7419e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
7421da177e4SLinus Torvalds 		}
7431da177e4SLinus Torvalds 	}
744c8adf9a3SRam Pai 	size0 = calculate_iosize(size, min_size, size1,
74513583b16SRam Pai 			resource_size(b_res), 4096);
746be768912SYinghai Lu 	if (children_add_size > add_size)
747be768912SYinghai Lu 		add_size = children_add_size;
7489e8bf93aSRam Pai 	size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
749a4ac9feaSYinghai Lu 		calculate_iosize(size, min_size, add_size + size1,
750c8adf9a3SRam Pai 			resource_size(b_res), 4096);
751c8adf9a3SRam Pai 	if (!size0 && !size1) {
752865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
753865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
754865df576SBjorn Helgaas 				 "%pR to [bus %02x-%02x] (unused)\n", b_res,
755865df576SBjorn Helgaas 				 bus->secondary, bus->subordinate);
7561da177e4SLinus Torvalds 		b_res->flags = 0;
7571da177e4SLinus Torvalds 		return;
7581da177e4SLinus Torvalds 	}
7591da177e4SLinus Torvalds 	/* Alignment of the IO window is always 4K */
7601da177e4SLinus Torvalds 	b_res->start = 4096;
761c8adf9a3SRam Pai 	b_res->end = b_res->start + size0 - 1;
76288452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
7639e8bf93aSRam Pai 	if (size1 > size0 && realloc_head)
7649e8bf93aSRam Pai 		add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096);
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds 
767c8adf9a3SRam Pai /**
768c8adf9a3SRam Pai  * pbus_size_mem() - size the memory window of a given bus
769c8adf9a3SRam Pai  *
770c8adf9a3SRam Pai  * @bus : the bus
771c8adf9a3SRam Pai  * @min_size : the minimum memory window that must to be allocated
772c8adf9a3SRam Pai  * @add_size : additional optional memory window
7739e8bf93aSRam Pai  * @realloc_head : track the additional memory window on this list
774c8adf9a3SRam Pai  *
775c8adf9a3SRam Pai  * Calculate the size of the bus and minimal alignment which
776c8adf9a3SRam Pai  * guarantees that all child resources fit in this size.
777c8adf9a3SRam Pai  */
77828760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
779c8adf9a3SRam Pai 			 unsigned long type, resource_size_t min_size,
780c8adf9a3SRam Pai 			resource_size_t add_size,
781bdc4abecSYinghai Lu 			struct list_head *realloc_head)
7821da177e4SLinus Torvalds {
7831da177e4SLinus Torvalds 	struct pci_dev *dev;
784c8adf9a3SRam Pai 	resource_size_t min_align, align, size, size0, size1;
785c40a22e0SBenjamin Herrenschmidt 	resource_size_t aligns[12];	/* Alignments from 1Mb to 2Gb */
7861da177e4SLinus Torvalds 	int order, max_order;
7871da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, type);
7881f82de10SYinghai Lu 	unsigned int mem64_mask = 0;
789be768912SYinghai Lu 	resource_size_t children_add_size = 0;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	if (!b_res)
7921da177e4SLinus Torvalds 		return 0;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
7951da177e4SLinus Torvalds 	max_order = 0;
7961da177e4SLinus Torvalds 	size = 0;
7971da177e4SLinus Torvalds 
7981f82de10SYinghai Lu 	mem64_mask = b_res->flags & IORESOURCE_MEM_64;
7991f82de10SYinghai Lu 	b_res->flags &= ~IORESOURCE_MEM_64;
8001f82de10SYinghai Lu 
8011da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
8021da177e4SLinus Torvalds 		int i;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
8051da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
806c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 			if (r->parent || (r->flags & mask) != type)
8091da177e4SLinus Torvalds 				continue;
810022edd86SZhao, Yu 			r_size = resource_size(r);
8112aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV
8122aceefcbSYinghai Lu 			/* put SRIOV requested res to the optional list */
8139e8bf93aSRam Pai 			if (realloc_head && i >= PCI_IOV_RESOURCES &&
8142aceefcbSYinghai Lu 					i <= PCI_IOV_RESOURCE_END) {
8152aceefcbSYinghai Lu 				r->end = r->start - 1;
8169e8bf93aSRam Pai 				add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */);
8172aceefcbSYinghai Lu 				children_add_size += r_size;
8182aceefcbSYinghai Lu 				continue;
8192aceefcbSYinghai Lu 			}
8202aceefcbSYinghai Lu #endif
8211da177e4SLinus Torvalds 			/* For bridges size != alignment */
8226faf17f6SChris Wright 			align = pci_resource_alignment(dev, r);
8231da177e4SLinus Torvalds 			order = __ffs(align) - 20;
8241da177e4SLinus Torvalds 			if (order > 11) {
825865df576SBjorn Helgaas 				dev_warn(&dev->dev, "disabling BAR %d: %pR "
826865df576SBjorn Helgaas 					 "(bad alignment %#llx)\n", i, r,
827865df576SBjorn Helgaas 					 (unsigned long long) align);
8281da177e4SLinus Torvalds 				r->flags = 0;
8291da177e4SLinus Torvalds 				continue;
8301da177e4SLinus Torvalds 			}
8311da177e4SLinus Torvalds 			size += r_size;
8321da177e4SLinus Torvalds 			if (order < 0)
8331da177e4SLinus Torvalds 				order = 0;
8341da177e4SLinus Torvalds 			/* Exclude ranges with size > align from
8351da177e4SLinus Torvalds 			   calculation of the alignment. */
8361da177e4SLinus Torvalds 			if (r_size == align)
8371da177e4SLinus Torvalds 				aligns[order] += align;
8381da177e4SLinus Torvalds 			if (order > max_order)
8391da177e4SLinus Torvalds 				max_order = order;
8401f82de10SYinghai Lu 			mem64_mask &= r->flags & IORESOURCE_MEM_64;
841be768912SYinghai Lu 
8429e8bf93aSRam Pai 			if (realloc_head)
8439e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
8441da177e4SLinus Torvalds 		}
8451da177e4SLinus Torvalds 	}
8461da177e4SLinus Torvalds 	align = 0;
8471da177e4SLinus Torvalds 	min_align = 0;
8481da177e4SLinus Torvalds 	for (order = 0; order <= max_order; order++) {
8498308c54dSJeremy Fitzhardinge 		resource_size_t align1 = 1;
8508308c54dSJeremy Fitzhardinge 
8518308c54dSJeremy Fitzhardinge 		align1 <<= (order + 20);
8528308c54dSJeremy Fitzhardinge 
8531da177e4SLinus Torvalds 		if (!align)
8541da177e4SLinus Torvalds 			min_align = align1;
8556f6f8c2fSMilind Arun Choudhary 		else if (ALIGN(align + min_align, min_align) < align1)
8561da177e4SLinus Torvalds 			min_align = align1 >> 1;
8571da177e4SLinus Torvalds 		align += aligns[order];
8581da177e4SLinus Torvalds 	}
859b42282e5SLinus Torvalds 	size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
860be768912SYinghai Lu 	if (children_add_size > add_size)
861be768912SYinghai Lu 		add_size = children_add_size;
8629e8bf93aSRam Pai 	size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
863a4ac9feaSYinghai Lu 		calculate_memsize(size, min_size, add_size,
864b42282e5SLinus Torvalds 				resource_size(b_res), min_align);
865c8adf9a3SRam Pai 	if (!size0 && !size1) {
866865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
867865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
868865df576SBjorn Helgaas 				 "%pR to [bus %02x-%02x] (unused)\n", b_res,
869865df576SBjorn Helgaas 				 bus->secondary, bus->subordinate);
8701da177e4SLinus Torvalds 		b_res->flags = 0;
8711da177e4SLinus Torvalds 		return 1;
8721da177e4SLinus Torvalds 	}
8731da177e4SLinus Torvalds 	b_res->start = min_align;
874c8adf9a3SRam Pai 	b_res->end = size0 + min_align - 1;
875c8adf9a3SRam Pai 	b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
8769e8bf93aSRam Pai 	if (size1 > size0 && realloc_head)
8779e8bf93aSRam Pai 		add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
8781da177e4SLinus Torvalds 	return 1;
8791da177e4SLinus Torvalds }
8801da177e4SLinus Torvalds 
8810a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res)
8820a2daa1cSRam Pai {
8830a2daa1cSRam Pai 	if (res->flags & IORESOURCE_IO)
8840a2daa1cSRam Pai 		return pci_cardbus_io_size;
8850a2daa1cSRam Pai 	if (res->flags & IORESOURCE_MEM)
8860a2daa1cSRam Pai 		return pci_cardbus_mem_size;
8870a2daa1cSRam Pai 	return 0;
8880a2daa1cSRam Pai }
8890a2daa1cSRam Pai 
8900a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus,
891bdc4abecSYinghai Lu 			struct list_head *realloc_head)
8921da177e4SLinus Torvalds {
8931da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
8941da177e4SLinus Torvalds 	struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
8951da177e4SLinus Torvalds 	u16 ctrl;
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	/*
8981da177e4SLinus Torvalds 	 * Reserve some resources for CardBus.  We reserve
8991da177e4SLinus Torvalds 	 * a fixed amount of bus space for CardBus bridges.
9001da177e4SLinus Torvalds 	 */
901934b7024SLinus Torvalds 	b_res[0].start = 0;
902934b7024SLinus Torvalds 	b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
9039e8bf93aSRam Pai 	if (realloc_head)
9049e8bf93aSRam Pai 		add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */);
9051da177e4SLinus Torvalds 
906934b7024SLinus Torvalds 	b_res[1].start = 0;
907934b7024SLinus Torvalds 	b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
9089e8bf93aSRam Pai 	if (realloc_head)
9099e8bf93aSRam Pai 		add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */);
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	/*
9121da177e4SLinus Torvalds 	 * Check whether prefetchable memory is supported
9131da177e4SLinus Torvalds 	 * by this bridge.
9141da177e4SLinus Torvalds 	 */
9151da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
9161da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
9171da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
9181da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
9191da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
9201da177e4SLinus Torvalds 	}
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	/*
9231da177e4SLinus Torvalds 	 * If we have prefetchable memory support, allocate
9241da177e4SLinus Torvalds 	 * two regions.  Otherwise, allocate one region of
9251da177e4SLinus Torvalds 	 * twice the size.
9261da177e4SLinus Torvalds 	 */
9271da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
928934b7024SLinus Torvalds 		b_res[2].start = 0;
929934b7024SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
9309e8bf93aSRam Pai 		if (realloc_head)
9319e8bf93aSRam Pai 			add_to_list(realloc_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */);
9321da177e4SLinus Torvalds 
933934b7024SLinus Torvalds 		b_res[3].start = 0;
934934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
9359e8bf93aSRam Pai 		if (realloc_head)
9369e8bf93aSRam Pai 			add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */);
9371da177e4SLinus Torvalds 	} else {
938934b7024SLinus Torvalds 		b_res[3].start = 0;
939934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
9409e8bf93aSRam Pai 		if (realloc_head)
9419e8bf93aSRam Pai 			add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size * 2, 0 /* dont care */);
9421da177e4SLinus Torvalds 	}
9430a2daa1cSRam Pai 
9440a2daa1cSRam Pai 	/* set the size of the resource to zero, so that the resource does not
9450a2daa1cSRam Pai 	 * get assigned during required-resource allocation cycle but gets assigned
9460a2daa1cSRam Pai 	 * during the optional-resource allocation cycle.
9470a2daa1cSRam Pai  	 */
9480a2daa1cSRam Pai 	b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1;
9490a2daa1cSRam Pai 	b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0;
9501da177e4SLinus Torvalds }
9511da177e4SLinus Torvalds 
952c8adf9a3SRam Pai void __ref __pci_bus_size_bridges(struct pci_bus *bus,
953bdc4abecSYinghai Lu 			struct list_head *realloc_head)
9541da177e4SLinus Torvalds {
9551da177e4SLinus Torvalds 	struct pci_dev *dev;
9561da177e4SLinus Torvalds 	unsigned long mask, prefmask;
957c8adf9a3SRam Pai 	resource_size_t additional_mem_size = 0, additional_io_size = 0;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
9601da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
9611da177e4SLinus Torvalds 		if (!b)
9621da177e4SLinus Torvalds 			continue;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 		switch (dev->class >> 8) {
9651da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
9669e8bf93aSRam Pai 			pci_bus_size_cardbus(b, realloc_head);
9671da177e4SLinus Torvalds 			break;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
9701da177e4SLinus Torvalds 		default:
9719e8bf93aSRam Pai 			__pci_bus_size_bridges(b, realloc_head);
9721da177e4SLinus Torvalds 			break;
9731da177e4SLinus Torvalds 		}
9741da177e4SLinus Torvalds 	}
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	/* The root bus? */
9771da177e4SLinus Torvalds 	if (!bus->self)
9781da177e4SLinus Torvalds 		return;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	switch (bus->self->class >> 8) {
9811da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_CARDBUS:
9821da177e4SLinus Torvalds 		/* don't size cardbuses yet. */
9831da177e4SLinus Torvalds 		break;
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_PCI:
9861da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
98728760489SEric W. Biederman 		if (bus->self->is_hotplug_bridge) {
988c8adf9a3SRam Pai 			additional_io_size  = pci_hotplug_io_size;
989c8adf9a3SRam Pai 			additional_mem_size = pci_hotplug_mem_size;
99028760489SEric W. Biederman 		}
991c8adf9a3SRam Pai 		/*
992c8adf9a3SRam Pai 		 * Follow thru
993c8adf9a3SRam Pai 		 */
9941da177e4SLinus Torvalds 	default:
99519aa7ee4SYinghai Lu 		pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
99619aa7ee4SYinghai Lu 			     additional_io_size, realloc_head);
9971da177e4SLinus Torvalds 		/* If the bridge supports prefetchable range, size it
9981da177e4SLinus Torvalds 		   separately. If it doesn't, or its prefetchable window
9991da177e4SLinus Torvalds 		   has already been allocated by arch code, try
10001da177e4SLinus Torvalds 		   non-prefetchable range for both types of PCI memory
10011da177e4SLinus Torvalds 		   resources. */
10021da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
10031da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
100419aa7ee4SYinghai Lu 		if (pbus_size_mem(bus, prefmask, prefmask,
100519aa7ee4SYinghai Lu 				  realloc_head ? 0 : additional_mem_size,
100619aa7ee4SYinghai Lu 				  additional_mem_size, realloc_head))
10071da177e4SLinus Torvalds 			mask = prefmask; /* Success, size non-prefetch only. */
100828760489SEric W. Biederman 		else
1009c8adf9a3SRam Pai 			additional_mem_size += additional_mem_size;
101019aa7ee4SYinghai Lu 		pbus_size_mem(bus, mask, IORESOURCE_MEM,
101119aa7ee4SYinghai Lu 				realloc_head ? 0 : additional_mem_size,
101219aa7ee4SYinghai Lu 				additional_mem_size, realloc_head);
10131da177e4SLinus Torvalds 		break;
10141da177e4SLinus Torvalds 	}
10151da177e4SLinus Torvalds }
1016c8adf9a3SRam Pai 
1017c8adf9a3SRam Pai void __ref pci_bus_size_bridges(struct pci_bus *bus)
1018c8adf9a3SRam Pai {
1019c8adf9a3SRam Pai 	__pci_bus_size_bridges(bus, NULL);
1020c8adf9a3SRam Pai }
10211da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
10221da177e4SLinus Torvalds 
1023568ddef8SYinghai Lu static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
1024bdc4abecSYinghai Lu 					 struct list_head *realloc_head,
1025bdc4abecSYinghai Lu 					 struct list_head *fail_head)
10261da177e4SLinus Torvalds {
10271da177e4SLinus Torvalds 	struct pci_bus *b;
10281da177e4SLinus Torvalds 	struct pci_dev *dev;
10291da177e4SLinus Torvalds 
10309e8bf93aSRam Pai 	pbus_assign_resources_sorted(bus, realloc_head, fail_head);
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
10331da177e4SLinus Torvalds 		b = dev->subordinate;
10341da177e4SLinus Torvalds 		if (!b)
10351da177e4SLinus Torvalds 			continue;
10361da177e4SLinus Torvalds 
10379e8bf93aSRam Pai 		__pci_bus_assign_resources(b, realloc_head, fail_head);
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 		switch (dev->class >> 8) {
10401da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
10416841ec68SYinghai Lu 			if (!pci_is_enabled(dev))
10421da177e4SLinus Torvalds 				pci_setup_bridge(b);
10431da177e4SLinus Torvalds 			break;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
10461da177e4SLinus Torvalds 			pci_setup_cardbus(b);
10471da177e4SLinus Torvalds 			break;
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 		default:
105080ccba11SBjorn Helgaas 			dev_info(&dev->dev, "not setting up bridge for bus "
105180ccba11SBjorn Helgaas 				 "%04x:%02x\n", pci_domain_nr(b), b->number);
10521da177e4SLinus Torvalds 			break;
10531da177e4SLinus Torvalds 		}
10541da177e4SLinus Torvalds 	}
10551da177e4SLinus Torvalds }
1056568ddef8SYinghai Lu 
1057568ddef8SYinghai Lu void __ref pci_bus_assign_resources(const struct pci_bus *bus)
1058568ddef8SYinghai Lu {
1059c8adf9a3SRam Pai 	__pci_bus_assign_resources(bus, NULL, NULL);
1060568ddef8SYinghai Lu }
10611da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
10621da177e4SLinus Torvalds 
10636841ec68SYinghai Lu static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
1064bdc4abecSYinghai Lu 					 struct list_head *add_head,
1065bdc4abecSYinghai Lu 					 struct list_head *fail_head)
10666841ec68SYinghai Lu {
10676841ec68SYinghai Lu 	struct pci_bus *b;
10686841ec68SYinghai Lu 
10698424d759SYinghai Lu 	pdev_assign_resources_sorted((struct pci_dev *)bridge,
10708424d759SYinghai Lu 					 add_head, fail_head);
10716841ec68SYinghai Lu 
10726841ec68SYinghai Lu 	b = bridge->subordinate;
10736841ec68SYinghai Lu 	if (!b)
10746841ec68SYinghai Lu 		return;
10756841ec68SYinghai Lu 
10768424d759SYinghai Lu 	__pci_bus_assign_resources(b, add_head, fail_head);
10776841ec68SYinghai Lu 
10786841ec68SYinghai Lu 	switch (bridge->class >> 8) {
10796841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_PCI:
10806841ec68SYinghai Lu 		pci_setup_bridge(b);
10816841ec68SYinghai Lu 		break;
10826841ec68SYinghai Lu 
10836841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_CARDBUS:
10846841ec68SYinghai Lu 		pci_setup_cardbus(b);
10856841ec68SYinghai Lu 		break;
10866841ec68SYinghai Lu 
10876841ec68SYinghai Lu 	default:
10886841ec68SYinghai Lu 		dev_info(&bridge->dev, "not setting up bridge for bus "
10896841ec68SYinghai Lu 			 "%04x:%02x\n", pci_domain_nr(b), b->number);
10906841ec68SYinghai Lu 		break;
10916841ec68SYinghai Lu 	}
10926841ec68SYinghai Lu }
10935009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus,
10945009b460SYinghai Lu 					  unsigned long type)
10955009b460SYinghai Lu {
10965009b460SYinghai Lu 	int idx;
10975009b460SYinghai Lu 	bool changed = false;
10985009b460SYinghai Lu 	struct pci_dev *dev;
10995009b460SYinghai Lu 	struct resource *r;
11005009b460SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
11015009b460SYinghai Lu 				  IORESOURCE_PREFETCH;
11025009b460SYinghai Lu 
11035009b460SYinghai Lu 	dev = bus->self;
11045009b460SYinghai Lu 	for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
11055009b460SYinghai Lu 	     idx++) {
11065009b460SYinghai Lu 		r = &dev->resource[idx];
11075009b460SYinghai Lu 		if ((r->flags & type_mask) != type)
11085009b460SYinghai Lu 			continue;
11095009b460SYinghai Lu 		if (!r->parent)
11105009b460SYinghai Lu 			continue;
11115009b460SYinghai Lu 		/*
11125009b460SYinghai Lu 		 * if there are children under that, we should release them
11135009b460SYinghai Lu 		 *  all
11145009b460SYinghai Lu 		 */
11155009b460SYinghai Lu 		release_child_resources(r);
11165009b460SYinghai Lu 		if (!release_resource(r)) {
11175009b460SYinghai Lu 			dev_printk(KERN_DEBUG, &dev->dev,
11185009b460SYinghai Lu 				 "resource %d %pR released\n", idx, r);
11195009b460SYinghai Lu 			/* keep the old size */
11205009b460SYinghai Lu 			r->end = resource_size(r) - 1;
11215009b460SYinghai Lu 			r->start = 0;
11225009b460SYinghai Lu 			r->flags = 0;
11235009b460SYinghai Lu 			changed = true;
11245009b460SYinghai Lu 		}
11255009b460SYinghai Lu 	}
11265009b460SYinghai Lu 
11275009b460SYinghai Lu 	if (changed) {
11285009b460SYinghai Lu 		/* avoiding touch the one without PREF */
11295009b460SYinghai Lu 		if (type & IORESOURCE_PREFETCH)
11305009b460SYinghai Lu 			type = IORESOURCE_PREFETCH;
11315009b460SYinghai Lu 		__pci_setup_bridge(bus, type);
11325009b460SYinghai Lu 	}
11335009b460SYinghai Lu }
11345009b460SYinghai Lu 
11355009b460SYinghai Lu enum release_type {
11365009b460SYinghai Lu 	leaf_only,
11375009b460SYinghai Lu 	whole_subtree,
11385009b460SYinghai Lu };
11395009b460SYinghai Lu /*
11405009b460SYinghai Lu  * try to release pci bridge resources that is from leaf bridge,
11415009b460SYinghai Lu  * so we can allocate big new one later
11425009b460SYinghai Lu  */
11435009b460SYinghai Lu static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
11445009b460SYinghai Lu 						   unsigned long type,
11455009b460SYinghai Lu 						   enum release_type rel_type)
11465009b460SYinghai Lu {
11475009b460SYinghai Lu 	struct pci_dev *dev;
11485009b460SYinghai Lu 	bool is_leaf_bridge = true;
11495009b460SYinghai Lu 
11505009b460SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
11515009b460SYinghai Lu 		struct pci_bus *b = dev->subordinate;
11525009b460SYinghai Lu 		if (!b)
11535009b460SYinghai Lu 			continue;
11545009b460SYinghai Lu 
11555009b460SYinghai Lu 		is_leaf_bridge = false;
11565009b460SYinghai Lu 
11575009b460SYinghai Lu 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
11585009b460SYinghai Lu 			continue;
11595009b460SYinghai Lu 
11605009b460SYinghai Lu 		if (rel_type == whole_subtree)
11615009b460SYinghai Lu 			pci_bus_release_bridge_resources(b, type,
11625009b460SYinghai Lu 						 whole_subtree);
11635009b460SYinghai Lu 	}
11645009b460SYinghai Lu 
11655009b460SYinghai Lu 	if (pci_is_root_bus(bus))
11665009b460SYinghai Lu 		return;
11675009b460SYinghai Lu 
11685009b460SYinghai Lu 	if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
11695009b460SYinghai Lu 		return;
11705009b460SYinghai Lu 
11715009b460SYinghai Lu 	if ((rel_type == whole_subtree) || is_leaf_bridge)
11725009b460SYinghai Lu 		pci_bridge_release_resources(bus, type);
11735009b460SYinghai Lu }
11745009b460SYinghai Lu 
117576fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
117676fbc263SYinghai Lu {
117789a74eccSBjorn Helgaas 	struct resource *res;
117876fbc263SYinghai Lu 	int i;
117976fbc263SYinghai Lu 
118089a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
11817c9342b8SYinghai Lu 		if (!res || !res->end || !res->flags)
118276fbc263SYinghai Lu                         continue;
118376fbc263SYinghai Lu 
1184c7dabef8SBjorn Helgaas 		dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
118576fbc263SYinghai Lu         }
118676fbc263SYinghai Lu }
118776fbc263SYinghai Lu 
118876fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
118976fbc263SYinghai Lu {
119076fbc263SYinghai Lu 	struct pci_bus *b;
119176fbc263SYinghai Lu 	struct pci_dev *dev;
119276fbc263SYinghai Lu 
119376fbc263SYinghai Lu 
119476fbc263SYinghai Lu 	pci_bus_dump_res(bus);
119576fbc263SYinghai Lu 
119676fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
119776fbc263SYinghai Lu 		b = dev->subordinate;
119876fbc263SYinghai Lu 		if (!b)
119976fbc263SYinghai Lu 			continue;
120076fbc263SYinghai Lu 
120176fbc263SYinghai Lu 		pci_bus_dump_resources(b);
120276fbc263SYinghai Lu 	}
120376fbc263SYinghai Lu }
120476fbc263SYinghai Lu 
1205da7822e5SYinghai Lu static int __init pci_bus_get_depth(struct pci_bus *bus)
1206da7822e5SYinghai Lu {
1207da7822e5SYinghai Lu 	int depth = 0;
1208da7822e5SYinghai Lu 	struct pci_dev *dev;
1209da7822e5SYinghai Lu 
1210da7822e5SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
1211da7822e5SYinghai Lu 		int ret;
1212da7822e5SYinghai Lu 		struct pci_bus *b = dev->subordinate;
1213da7822e5SYinghai Lu 		if (!b)
1214da7822e5SYinghai Lu 			continue;
1215da7822e5SYinghai Lu 
1216da7822e5SYinghai Lu 		ret = pci_bus_get_depth(b);
1217da7822e5SYinghai Lu 		if (ret + 1 > depth)
1218da7822e5SYinghai Lu 			depth = ret + 1;
1219da7822e5SYinghai Lu 	}
1220da7822e5SYinghai Lu 
1221da7822e5SYinghai Lu 	return depth;
1222da7822e5SYinghai Lu }
1223da7822e5SYinghai Lu static int __init pci_get_max_depth(void)
1224da7822e5SYinghai Lu {
1225da7822e5SYinghai Lu 	int depth = 0;
1226da7822e5SYinghai Lu 	struct pci_bus *bus;
1227da7822e5SYinghai Lu 
1228da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node) {
1229da7822e5SYinghai Lu 		int ret;
1230da7822e5SYinghai Lu 
1231da7822e5SYinghai Lu 		ret = pci_bus_get_depth(bus);
1232da7822e5SYinghai Lu 		if (ret > depth)
1233da7822e5SYinghai Lu 			depth = ret;
1234da7822e5SYinghai Lu 	}
1235da7822e5SYinghai Lu 
1236da7822e5SYinghai Lu 	return depth;
1237da7822e5SYinghai Lu }
1238da7822e5SYinghai Lu 
1239f483d392SRam Pai 
1240da7822e5SYinghai Lu /*
1241da7822e5SYinghai Lu  * first try will not touch pci bridge res
1242da7822e5SYinghai Lu  * second  and later try will clear small leaf bridge res
1243da7822e5SYinghai Lu  * will stop till to the max  deepth if can not find good one
1244da7822e5SYinghai Lu  */
12451da177e4SLinus Torvalds void __init
12461da177e4SLinus Torvalds pci_assign_unassigned_resources(void)
12471da177e4SLinus Torvalds {
12481da177e4SLinus Torvalds 	struct pci_bus *bus;
1249bdc4abecSYinghai Lu 	LIST_HEAD(realloc_head); /* list of resources that
1250c8adf9a3SRam Pai 					want additional resources */
1251bdc4abecSYinghai Lu 	struct list_head *add_list = NULL;
1252da7822e5SYinghai Lu 	int tried_times = 0;
1253da7822e5SYinghai Lu 	enum release_type rel_type = leaf_only;
1254bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
1255b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
1256da7822e5SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1257da7822e5SYinghai Lu 				  IORESOURCE_PREFETCH;
1258da7822e5SYinghai Lu 	unsigned long failed_type;
125919aa7ee4SYinghai Lu 	int pci_try_num = 1;
1260da7822e5SYinghai Lu 
126119aa7ee4SYinghai Lu 	/* don't realloc if asked to do so */
126219aa7ee4SYinghai Lu 	if (pci_realloc_enabled()) {
126319aa7ee4SYinghai Lu 		int max_depth = pci_get_max_depth();
126419aa7ee4SYinghai Lu 
1265da7822e5SYinghai Lu 		pci_try_num = max_depth + 1;
1266da7822e5SYinghai Lu 		printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1267da7822e5SYinghai Lu 			 max_depth, pci_try_num);
126819aa7ee4SYinghai Lu 	}
1269da7822e5SYinghai Lu 
1270da7822e5SYinghai Lu again:
127119aa7ee4SYinghai Lu 	/*
127219aa7ee4SYinghai Lu 	 * last try will use add_list, otherwise will try good to have as
127319aa7ee4SYinghai Lu 	 * must have, so can realloc parent bridge resource
127419aa7ee4SYinghai Lu 	 */
127519aa7ee4SYinghai Lu 	if (tried_times + 1 == pci_try_num)
1276bdc4abecSYinghai Lu 		add_list = &realloc_head;
12771da177e4SLinus Torvalds 	/* Depth first, calculate sizes and alignments of all
12781da177e4SLinus Torvalds 	   subordinate buses. */
1279da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
128019aa7ee4SYinghai Lu 		__pci_bus_size_bridges(bus, add_list);
1281c8adf9a3SRam Pai 
12821da177e4SLinus Torvalds 	/* Depth last, allocate resources and update the hardware. */
1283da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
1284bdc4abecSYinghai Lu 		__pci_bus_assign_resources(bus, add_list, &fail_head);
128519aa7ee4SYinghai Lu 	if (add_list)
1286bdc4abecSYinghai Lu 		BUG_ON(!list_empty(add_list));
1287da7822e5SYinghai Lu 	tried_times++;
1288da7822e5SYinghai Lu 
1289da7822e5SYinghai Lu 	/* any device complain? */
1290bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
1291da7822e5SYinghai Lu 		goto enable_and_dump;
1292f483d392SRam Pai 
1293da7822e5SYinghai Lu 	failed_type = 0;
1294b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list)
1295b9b0bba9SYinghai Lu 		failed_type |= fail_res->flags;
1296bdc4abecSYinghai Lu 
1297da7822e5SYinghai Lu 	/*
1298da7822e5SYinghai Lu 	 * io port are tight, don't try extra
1299da7822e5SYinghai Lu 	 * or if reach the limit, don't want to try more
1300da7822e5SYinghai Lu 	 */
1301da7822e5SYinghai Lu 	failed_type &= type_mask;
1302da7822e5SYinghai Lu 	if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
1303*bffc56d4SYinghai Lu 		free_list(&fail_head);
1304da7822e5SYinghai Lu 		goto enable_and_dump;
1305da7822e5SYinghai Lu 	}
1306da7822e5SYinghai Lu 
1307da7822e5SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1308da7822e5SYinghai Lu 			 tried_times + 1);
1309da7822e5SYinghai Lu 
1310da7822e5SYinghai Lu 	/* third times and later will not check if it is leaf */
1311da7822e5SYinghai Lu 	if ((tried_times + 1) > 2)
1312da7822e5SYinghai Lu 		rel_type = whole_subtree;
1313da7822e5SYinghai Lu 
1314da7822e5SYinghai Lu 	/*
1315da7822e5SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
1316da7822e5SYinghai Lu 	 * child device under that bridge
1317da7822e5SYinghai Lu 	 */
1318b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1319b9b0bba9SYinghai Lu 		bus = fail_res->dev->bus;
1320bdc4abecSYinghai Lu 		pci_bus_release_bridge_resources(bus,
1321b9b0bba9SYinghai Lu 						 fail_res->flags & type_mask,
1322da7822e5SYinghai Lu 						 rel_type);
1323da7822e5SYinghai Lu 	}
1324da7822e5SYinghai Lu 	/* restore size and flags */
1325b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1326b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
1327da7822e5SYinghai Lu 
1328b9b0bba9SYinghai Lu 		res->start = fail_res->start;
1329b9b0bba9SYinghai Lu 		res->end = fail_res->end;
1330b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
1331b9b0bba9SYinghai Lu 		if (fail_res->dev->subordinate)
1332da7822e5SYinghai Lu 			res->flags = 0;
1333da7822e5SYinghai Lu 	}
1334*bffc56d4SYinghai Lu 	free_list(&fail_head);
1335da7822e5SYinghai Lu 
1336da7822e5SYinghai Lu 	goto again;
1337da7822e5SYinghai Lu 
1338da7822e5SYinghai Lu enable_and_dump:
1339da7822e5SYinghai Lu 	/* Depth last, update the hardware. */
1340da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
1341da7822e5SYinghai Lu 		pci_enable_bridges(bus);
134276fbc263SYinghai Lu 
134376fbc263SYinghai Lu 	/* dump the resource on buses */
1344da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
134576fbc263SYinghai Lu 		pci_bus_dump_resources(bus);
134676fbc263SYinghai Lu }
13476841ec68SYinghai Lu 
13486841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
13496841ec68SYinghai Lu {
13506841ec68SYinghai Lu 	struct pci_bus *parent = bridge->subordinate;
1351bdc4abecSYinghai Lu 	LIST_HEAD(add_list); /* list of resources that
13528424d759SYinghai Lu 					want additional resources */
135332180e40SYinghai Lu 	int tried_times = 0;
1354bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
1355b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
13566841ec68SYinghai Lu 	int retval;
135732180e40SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
135832180e40SYinghai Lu 				  IORESOURCE_PREFETCH;
13596841ec68SYinghai Lu 
136032180e40SYinghai Lu again:
13618424d759SYinghai Lu 	__pci_bus_size_bridges(parent, &add_list);
1362bdc4abecSYinghai Lu 	__pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1363bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
136432180e40SYinghai Lu 	tried_times++;
136532180e40SYinghai Lu 
1366bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
13673f579c34SYinghai Lu 		goto enable_all;
136832180e40SYinghai Lu 
136932180e40SYinghai Lu 	if (tried_times >= 2) {
137032180e40SYinghai Lu 		/* still fail, don't need to try more */
1371*bffc56d4SYinghai Lu 		free_list(&fail_head);
13723f579c34SYinghai Lu 		goto enable_all;
137332180e40SYinghai Lu 	}
137432180e40SYinghai Lu 
137532180e40SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
137632180e40SYinghai Lu 			 tried_times + 1);
137732180e40SYinghai Lu 
137832180e40SYinghai Lu 	/*
137932180e40SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
138032180e40SYinghai Lu 	 * child device under that bridge
138132180e40SYinghai Lu 	 */
1382b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1383b9b0bba9SYinghai Lu 		struct pci_bus *bus = fail_res->dev->bus;
1384b9b0bba9SYinghai Lu 		unsigned long flags = fail_res->flags;
138532180e40SYinghai Lu 
138632180e40SYinghai Lu 		pci_bus_release_bridge_resources(bus, flags & type_mask,
138732180e40SYinghai Lu 						 whole_subtree);
138832180e40SYinghai Lu 	}
138932180e40SYinghai Lu 	/* restore size and flags */
1390b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1391b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
139232180e40SYinghai Lu 
1393b9b0bba9SYinghai Lu 		res->start = fail_res->start;
1394b9b0bba9SYinghai Lu 		res->end = fail_res->end;
1395b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
1396b9b0bba9SYinghai Lu 		if (fail_res->dev->subordinate)
139732180e40SYinghai Lu 			res->flags = 0;
139832180e40SYinghai Lu 	}
1399*bffc56d4SYinghai Lu 	free_list(&fail_head);
140032180e40SYinghai Lu 
140132180e40SYinghai Lu 	goto again;
14023f579c34SYinghai Lu 
14033f579c34SYinghai Lu enable_all:
14043f579c34SYinghai Lu 	retval = pci_reenable_device(bridge);
14053f579c34SYinghai Lu 	pci_set_master(bridge);
14063f579c34SYinghai Lu 	pci_enable_bridges(parent);
14076841ec68SYinghai Lu }
14086841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
14099b03088fSYinghai Lu 
14109b03088fSYinghai Lu #ifdef CONFIG_HOTPLUG
14119b03088fSYinghai Lu /**
14129b03088fSYinghai Lu  * pci_rescan_bus - scan a PCI bus for devices.
14139b03088fSYinghai Lu  * @bus: PCI bus to scan
14149b03088fSYinghai Lu  *
14159b03088fSYinghai Lu  * Scan a PCI bus and child buses for new devices, adds them,
14169b03088fSYinghai Lu  * and enables them.
14179b03088fSYinghai Lu  *
14189b03088fSYinghai Lu  * Returns the max number of subordinate bus discovered.
14199b03088fSYinghai Lu  */
14209b03088fSYinghai Lu unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
14219b03088fSYinghai Lu {
14229b03088fSYinghai Lu 	unsigned int max;
14239b03088fSYinghai Lu 	struct pci_dev *dev;
1424bdc4abecSYinghai Lu 	LIST_HEAD(add_list); /* list of resources that
14259b03088fSYinghai Lu 					want additional resources */
14269b03088fSYinghai Lu 
14279b03088fSYinghai Lu 	max = pci_scan_child_bus(bus);
14289b03088fSYinghai Lu 
14299b03088fSYinghai Lu 	down_read(&pci_bus_sem);
14309b03088fSYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
14319b03088fSYinghai Lu 		if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
14329b03088fSYinghai Lu 		    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
14339b03088fSYinghai Lu 			if (dev->subordinate)
14349b03088fSYinghai Lu 				__pci_bus_size_bridges(dev->subordinate,
14359b03088fSYinghai Lu 							 &add_list);
14369b03088fSYinghai Lu 	up_read(&pci_bus_sem);
14379b03088fSYinghai Lu 	__pci_bus_assign_resources(bus, &add_list, NULL);
1438bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
14399b03088fSYinghai Lu 
14409b03088fSYinghai Lu 	pci_enable_bridges(bus);
14419b03088fSYinghai Lu 	pci_bus_add_devices(bus);
14429b03088fSYinghai Lu 
14439b03088fSYinghai Lu 	return max;
14449b03088fSYinghai Lu }
14459b03088fSYinghai Lu EXPORT_SYMBOL_GPL(pci_rescan_bus);
14469b03088fSYinghai Lu #endif
1447