xref: /openbmc/linux/drivers/pci/setup-bus.c (revision 78c3b329b9dd7097781cb900146e503e499cccfe)
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 
30568ddef8SYinghai Lu struct resource_list_x {
31568ddef8SYinghai Lu 	struct resource_list_x *next;
32568ddef8SYinghai Lu 	struct resource *res;
33568ddef8SYinghai 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 
41094732a5SRam Pai #define free_list(type, head) do {                      \
42094732a5SRam Pai 	struct type *list, *tmp;			\
43094732a5SRam Pai 	for (list = (head)->next; list;) {		\
44094732a5SRam Pai 		tmp = list;				\
45094732a5SRam Pai 		list = list->next;			\
46094732a5SRam Pai 		kfree(tmp);				\
47094732a5SRam Pai 	}						\
48094732a5SRam Pai 	(head)->next = NULL;				\
49094732a5SRam Pai } while (0)
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  */
67ef62dfefSYinghai Lu static int add_to_list(struct resource_list_x *head,
68c8adf9a3SRam Pai 		 struct pci_dev *dev, struct resource *res,
692bbc6942SRam Pai 		 resource_size_t add_size, resource_size_t min_align)
70568ddef8SYinghai Lu {
71568ddef8SYinghai Lu 	struct resource_list_x *list = head;
72568ddef8SYinghai Lu 	struct resource_list_x *ln = list->next;
73568ddef8SYinghai Lu 	struct resource_list_x *tmp;
74568ddef8SYinghai Lu 
75568ddef8SYinghai Lu 	tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
76568ddef8SYinghai Lu 	if (!tmp) {
77c8adf9a3SRam Pai 		pr_warning("add_to_list: kmalloc() failed!\n");
78ef62dfefSYinghai Lu 		return -ENOMEM;
79568ddef8SYinghai Lu 	}
80568ddef8SYinghai Lu 
81568ddef8SYinghai Lu 	tmp->next = ln;
82568ddef8SYinghai Lu 	tmp->res = res;
83568ddef8SYinghai Lu 	tmp->dev = dev;
84568ddef8SYinghai Lu 	tmp->start = res->start;
85568ddef8SYinghai Lu 	tmp->end = res->end;
86568ddef8SYinghai Lu 	tmp->flags = res->flags;
87c8adf9a3SRam Pai 	tmp->add_size = add_size;
882bbc6942SRam Pai 	tmp->min_align = min_align;
89568ddef8SYinghai Lu 	list->next = tmp;
90ef62dfefSYinghai Lu 
91ef62dfefSYinghai Lu 	return 0;
92568ddef8SYinghai Lu }
93568ddef8SYinghai Lu 
94c8adf9a3SRam Pai static void add_to_failed_list(struct resource_list_x *head,
95c8adf9a3SRam Pai 				struct pci_dev *dev, struct resource *res)
96c8adf9a3SRam Pai {
972bbc6942SRam Pai 	add_to_list(head, dev, res,
982bbc6942SRam Pai 			0 /* dont care */,
992bbc6942SRam Pai 			0 /* dont care */);
100c8adf9a3SRam Pai }
101c8adf9a3SRam Pai 
1023e6e0d80SYinghai Lu static void remove_from_list(struct resource_list_x *realloc_head,
1033e6e0d80SYinghai Lu 				 struct resource *res)
1043e6e0d80SYinghai Lu {
1053e6e0d80SYinghai Lu 	struct resource_list_x *prev, *tmp, *list;
1063e6e0d80SYinghai Lu 
1073e6e0d80SYinghai Lu 	prev = realloc_head;
1083e6e0d80SYinghai Lu 	for (list = realloc_head->next; list;) {
1093e6e0d80SYinghai Lu 		if (list->res != res) {
1103e6e0d80SYinghai Lu 			prev = list;
1113e6e0d80SYinghai Lu 			list = list->next;
1123e6e0d80SYinghai Lu 			continue;
1133e6e0d80SYinghai Lu 		}
1143e6e0d80SYinghai Lu 		tmp = list;
1153e6e0d80SYinghai Lu 		prev->next = list = list->next;
1163e6e0d80SYinghai Lu 		kfree(tmp);
1173e6e0d80SYinghai Lu 	}
1183e6e0d80SYinghai Lu }
1193e6e0d80SYinghai Lu 
1201c372353SYinghai Lu static resource_size_t get_res_add_size(struct resource_list_x *realloc_head,
1211c372353SYinghai Lu 					struct resource *res)
1221c372353SYinghai Lu {
1231c372353SYinghai Lu 	struct resource_list_x *list;
1241c372353SYinghai Lu 
1251c372353SYinghai Lu 	/* check if it is in realloc_head list */
1261c372353SYinghai Lu 	for (list = realloc_head->next; list && list->res != res;
1271c372353SYinghai Lu 			list = list->next)
1281c372353SYinghai Lu 		;
1293e6e0d80SYinghai Lu 
1303e6e0d80SYinghai Lu 	if (list) {
1313e6e0d80SYinghai Lu 		dev_printk(KERN_DEBUG, &list->dev->dev,
1323e6e0d80SYinghai Lu 			 "%pR get_res_add_size  add_size %llx\n",
1333e6e0d80SYinghai Lu 			 list->res, (unsigned long long)list->add_size);
1341c372353SYinghai Lu 		return list->add_size;
1353e6e0d80SYinghai Lu 	}
1361c372353SYinghai Lu 
1371c372353SYinghai Lu 	return 0;
1381c372353SYinghai Lu }
1391c372353SYinghai Lu 
140*78c3b329SYinghai Lu /* Sort resources by alignment */
141*78c3b329SYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
142*78c3b329SYinghai Lu {
143*78c3b329SYinghai Lu 	int i;
144*78c3b329SYinghai Lu 
145*78c3b329SYinghai Lu 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
146*78c3b329SYinghai Lu 		struct resource *r;
147*78c3b329SYinghai Lu 		struct resource_list *list, *tmp;
148*78c3b329SYinghai Lu 		resource_size_t r_align;
149*78c3b329SYinghai Lu 
150*78c3b329SYinghai Lu 		r = &dev->resource[i];
151*78c3b329SYinghai Lu 
152*78c3b329SYinghai Lu 		if (r->flags & IORESOURCE_PCI_FIXED)
153*78c3b329SYinghai Lu 			continue;
154*78c3b329SYinghai Lu 
155*78c3b329SYinghai Lu 		if (!(r->flags) || r->parent)
156*78c3b329SYinghai Lu 			continue;
157*78c3b329SYinghai Lu 
158*78c3b329SYinghai Lu 		r_align = pci_resource_alignment(dev, r);
159*78c3b329SYinghai Lu 		if (!r_align) {
160*78c3b329SYinghai Lu 			dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
161*78c3b329SYinghai Lu 				 i, r);
162*78c3b329SYinghai Lu 			continue;
163*78c3b329SYinghai Lu 		}
164*78c3b329SYinghai Lu 		for (list = head; ; list = list->next) {
165*78c3b329SYinghai Lu 			resource_size_t align = 0;
166*78c3b329SYinghai Lu 			struct resource_list *ln = list->next;
167*78c3b329SYinghai Lu 
168*78c3b329SYinghai Lu 			if (ln)
169*78c3b329SYinghai Lu 				align = pci_resource_alignment(ln->dev, ln->res);
170*78c3b329SYinghai Lu 
171*78c3b329SYinghai Lu 			if (r_align > align) {
172*78c3b329SYinghai Lu 				tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
173*78c3b329SYinghai Lu 				if (!tmp)
174*78c3b329SYinghai Lu 					panic("pdev_sort_resources(): "
175*78c3b329SYinghai Lu 					      "kmalloc() failed!\n");
176*78c3b329SYinghai Lu 				tmp->next = ln;
177*78c3b329SYinghai Lu 				tmp->res = r;
178*78c3b329SYinghai Lu 				tmp->dev = dev;
179*78c3b329SYinghai Lu 				list->next = tmp;
180*78c3b329SYinghai Lu 				break;
181*78c3b329SYinghai Lu 			}
182*78c3b329SYinghai Lu 		}
183*78c3b329SYinghai Lu 	}
184*78c3b329SYinghai Lu }
185*78c3b329SYinghai Lu 
1866841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev,
1876841ec68SYinghai Lu 				 struct resource_list *head)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	u16 class = dev->class >> 8;
1901da177e4SLinus Torvalds 
1919bded00bSKenji Kaneshige 	/* Don't touch classless devices or host bridges or ioapics.  */
1926841ec68SYinghai Lu 	if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
1936841ec68SYinghai Lu 		return;
1941da177e4SLinus Torvalds 
1959bded00bSKenji Kaneshige 	/* Don't touch ioapic devices already enabled by firmware */
19623186279SSatoru Takeuchi 	if (class == PCI_CLASS_SYSTEM_PIC) {
1979bded00bSKenji Kaneshige 		u16 command;
1989bded00bSKenji Kaneshige 		pci_read_config_word(dev, PCI_COMMAND, &command);
1999bded00bSKenji Kaneshige 		if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
2006841ec68SYinghai Lu 			return;
20123186279SSatoru Takeuchi 	}
20223186279SSatoru Takeuchi 
2036841ec68SYinghai Lu 	pdev_sort_resources(dev, head);
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
206fc075e1dSRam Pai static inline void reset_resource(struct resource *res)
207fc075e1dSRam Pai {
208fc075e1dSRam Pai 	res->start = 0;
209fc075e1dSRam Pai 	res->end = 0;
210fc075e1dSRam Pai 	res->flags = 0;
211fc075e1dSRam Pai }
212fc075e1dSRam Pai 
213c8adf9a3SRam Pai /**
2149e8bf93aSRam Pai  * reassign_resources_sorted() - satisfy any additional resource requests
215c8adf9a3SRam Pai  *
2169e8bf93aSRam Pai  * @realloc_head : head of the list tracking requests requiring additional
217c8adf9a3SRam Pai  *             resources
218c8adf9a3SRam Pai  * @head     : head of the list tracking requests with allocated
219c8adf9a3SRam Pai  *             resources
220c8adf9a3SRam Pai  *
2219e8bf93aSRam Pai  * Walk through each element of the realloc_head and try to procure
222c8adf9a3SRam Pai  * additional resources for the element, provided the element
223c8adf9a3SRam Pai  * is in the head list.
224c8adf9a3SRam Pai  */
2259e8bf93aSRam Pai static void reassign_resources_sorted(struct resource_list_x *realloc_head,
226c8adf9a3SRam Pai 		struct resource_list *head)
227c8adf9a3SRam Pai {
228c8adf9a3SRam Pai 	struct resource *res;
229c8adf9a3SRam Pai 	struct resource_list_x *list, *tmp, *prev;
230c8adf9a3SRam Pai 	struct resource_list *hlist;
231c8adf9a3SRam Pai 	resource_size_t add_size;
232c8adf9a3SRam Pai 	int idx;
233c8adf9a3SRam Pai 
2349e8bf93aSRam Pai 	prev = realloc_head;
2359e8bf93aSRam Pai 	for (list = realloc_head->next; list;) {
236c8adf9a3SRam Pai 		res = list->res;
237c8adf9a3SRam Pai 		/* skip resource that has been reset */
238c8adf9a3SRam Pai 		if (!res->flags)
239c8adf9a3SRam Pai 			goto out;
240c8adf9a3SRam Pai 
241c8adf9a3SRam Pai 		/* skip this resource if not found in head list */
242c8adf9a3SRam Pai 		for (hlist = head->next; hlist && hlist->res != res;
243c8adf9a3SRam Pai 				hlist = hlist->next);
244c8adf9a3SRam Pai 		if (!hlist) { /* just skip */
245c8adf9a3SRam Pai 			prev = list;
246c8adf9a3SRam Pai 			list = list->next;
247c8adf9a3SRam Pai 			continue;
248c8adf9a3SRam Pai 		}
249c8adf9a3SRam Pai 
250c8adf9a3SRam Pai 		idx = res - &list->dev->resource[0];
251c8adf9a3SRam Pai 		add_size=list->add_size;
2522bbc6942SRam Pai 		if (!resource_size(res)) {
2530a2daa1cSRam Pai 			res->start = list->start;
254c8adf9a3SRam Pai 			res->end = res->start + add_size - 1;
255c8adf9a3SRam Pai 			if(pci_assign_resource(list->dev, idx))
256c8adf9a3SRam Pai 				reset_resource(res);
2572bbc6942SRam Pai 		} else {
2582bbc6942SRam Pai 			resource_size_t align = list->min_align;
2592bbc6942SRam Pai 			res->flags |= list->flags & (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
2602bbc6942SRam Pai 			if (pci_reassign_resource(list->dev, idx, add_size, align))
2612bbc6942SRam Pai 				dev_printk(KERN_DEBUG, &list->dev->dev, "failed to add optional resources res=%pR\n",
2622bbc6942SRam Pai 							res);
263c8adf9a3SRam Pai 		}
264c8adf9a3SRam Pai out:
265c8adf9a3SRam Pai 		tmp = list;
266c8adf9a3SRam Pai 		prev->next = list = list->next;
267c8adf9a3SRam Pai 		kfree(tmp);
268c8adf9a3SRam Pai 	}
269c8adf9a3SRam Pai }
270c8adf9a3SRam Pai 
271c8adf9a3SRam Pai /**
272c8adf9a3SRam Pai  * assign_requested_resources_sorted() - satisfy resource requests
273c8adf9a3SRam Pai  *
274c8adf9a3SRam Pai  * @head : head of the list tracking requests for resources
275c8adf9a3SRam Pai  * @failed_list : head of the list tracking requests that could
276c8adf9a3SRam Pai  *		not be allocated
277c8adf9a3SRam Pai  *
278c8adf9a3SRam Pai  * Satisfy resource requests of each element in the list. Add
279c8adf9a3SRam Pai  * requests that could not satisfied to the failed_list.
280c8adf9a3SRam Pai  */
281c8adf9a3SRam Pai static void assign_requested_resources_sorted(struct resource_list *head,
2826841ec68SYinghai Lu 				 struct resource_list_x *fail_head)
2836841ec68SYinghai Lu {
2846841ec68SYinghai Lu 	struct resource *res;
285c8adf9a3SRam Pai 	struct resource_list *list;
2866841ec68SYinghai Lu 	int idx;
2876841ec68SYinghai Lu 
288c8adf9a3SRam Pai 	for (list = head->next; list; list = list->next) {
2891da177e4SLinus Torvalds 		res = list->res;
2901da177e4SLinus Torvalds 		idx = res - &list->dev->resource[0];
291c8adf9a3SRam Pai 		if (resource_size(res) && pci_assign_resource(list->dev, idx)) {
2929a928660SYinghai Lu 			if (fail_head && !pci_is_root_bus(list->dev->bus)) {
2939a928660SYinghai Lu 				/*
2949a928660SYinghai Lu 				 * if the failed res is for ROM BAR, and it will
2959a928660SYinghai Lu 				 * be enabled later, don't add it to the list
2969a928660SYinghai Lu 				 */
2979a928660SYinghai Lu 				if (!((idx == PCI_ROM_RESOURCE) &&
2989a928660SYinghai Lu 				      (!(res->flags & IORESOURCE_ROM_ENABLE))))
299568ddef8SYinghai Lu 					add_to_failed_list(fail_head, list->dev, res);
3009a928660SYinghai Lu 			}
301fc075e1dSRam Pai 			reset_resource(res);
302542df5deSRajesh Shah 		}
3031da177e4SLinus Torvalds 	}
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
306c8adf9a3SRam Pai static void __assign_resources_sorted(struct resource_list *head,
3079e8bf93aSRam Pai 				 struct resource_list_x *realloc_head,
308c8adf9a3SRam Pai 				 struct resource_list_x *fail_head)
309c8adf9a3SRam Pai {
3103e6e0d80SYinghai Lu 	/*
3113e6e0d80SYinghai Lu 	 * Should not assign requested resources at first.
3123e6e0d80SYinghai Lu 	 *   they could be adjacent, so later reassign can not reallocate
3133e6e0d80SYinghai Lu 	 *   them one by one in parent resource window.
3143e6e0d80SYinghai Lu 	 * Try to assign requested + add_size at begining
3153e6e0d80SYinghai Lu 	 *  if could do that, could get out early.
3163e6e0d80SYinghai Lu 	 *  if could not do that, we still try to assign requested at first,
3173e6e0d80SYinghai Lu 	 *    then try to reassign add_size for some resources.
3183e6e0d80SYinghai Lu 	 */
3193e6e0d80SYinghai Lu 	struct resource_list_x save_head, local_fail_head, *list;
3203e6e0d80SYinghai Lu 	struct resource_list *l;
3213e6e0d80SYinghai Lu 
3223e6e0d80SYinghai Lu 	/* Check if optional add_size is there */
3233e6e0d80SYinghai Lu 	if (!realloc_head || !realloc_head->next)
3243e6e0d80SYinghai Lu 		goto requested_and_reassign;
3253e6e0d80SYinghai Lu 
3263e6e0d80SYinghai Lu 	/* Save original start, end, flags etc at first */
3273e6e0d80SYinghai Lu 	save_head.next = NULL;
3283e6e0d80SYinghai Lu 	for (l = head->next; l; l = l->next)
3293e6e0d80SYinghai Lu 		if (add_to_list(&save_head, l->dev, l->res, 0, 0)) {
3303e6e0d80SYinghai Lu 			free_list(resource_list_x, &save_head);
3313e6e0d80SYinghai Lu 			goto requested_and_reassign;
3323e6e0d80SYinghai Lu 		}
3333e6e0d80SYinghai Lu 
3343e6e0d80SYinghai Lu 	/* Update res in head list with add_size in realloc_head list */
3353e6e0d80SYinghai Lu 	for (l = head->next; l; l = l->next)
3363e6e0d80SYinghai Lu 		l->res->end += get_res_add_size(realloc_head, l->res);
3373e6e0d80SYinghai Lu 
3383e6e0d80SYinghai Lu 	/* Try updated head list with add_size added */
3393e6e0d80SYinghai Lu 	local_fail_head.next = NULL;
3403e6e0d80SYinghai Lu 	assign_requested_resources_sorted(head, &local_fail_head);
3413e6e0d80SYinghai Lu 
3423e6e0d80SYinghai Lu 	/* all assigned with add_size ? */
3433e6e0d80SYinghai Lu 	if (!local_fail_head.next) {
3443e6e0d80SYinghai Lu 		/* Remove head list from realloc_head list */
3453e6e0d80SYinghai Lu 		for (l = head->next; l; l = l->next)
3463e6e0d80SYinghai Lu 			remove_from_list(realloc_head, l->res);
3473e6e0d80SYinghai Lu 		free_list(resource_list_x, &save_head);
3483e6e0d80SYinghai Lu 		free_list(resource_list, head);
3493e6e0d80SYinghai Lu 		return;
3503e6e0d80SYinghai Lu 	}
3513e6e0d80SYinghai Lu 
3523e6e0d80SYinghai Lu 	free_list(resource_list_x, &local_fail_head);
3533e6e0d80SYinghai Lu 	/* Release assigned resource */
3543e6e0d80SYinghai Lu 	for (l = head->next; l; l = l->next)
3553e6e0d80SYinghai Lu 		if (l->res->parent)
3563e6e0d80SYinghai Lu 			release_resource(l->res);
3573e6e0d80SYinghai Lu 	/* Restore start/end/flags from saved list */
3583e6e0d80SYinghai Lu 	for (list = save_head.next; list; list = list->next) {
3593e6e0d80SYinghai Lu 		struct resource *res = list->res;
3603e6e0d80SYinghai Lu 
3613e6e0d80SYinghai Lu 		res->start = list->start;
3623e6e0d80SYinghai Lu 		res->end = list->end;
3633e6e0d80SYinghai Lu 		res->flags = list->flags;
3643e6e0d80SYinghai Lu 	}
3653e6e0d80SYinghai Lu 	free_list(resource_list_x, &save_head);
3663e6e0d80SYinghai Lu 
3673e6e0d80SYinghai Lu requested_and_reassign:
368c8adf9a3SRam Pai 	/* Satisfy the must-have resource requests */
369c8adf9a3SRam Pai 	assign_requested_resources_sorted(head, fail_head);
370c8adf9a3SRam Pai 
3710a2daa1cSRam Pai 	/* Try to satisfy any additional optional resource
372c8adf9a3SRam Pai 		requests */
3739e8bf93aSRam Pai 	if (realloc_head)
3749e8bf93aSRam Pai 		reassign_resources_sorted(realloc_head, head);
375c8adf9a3SRam Pai 	free_list(resource_list, head);
376c8adf9a3SRam Pai }
377c8adf9a3SRam Pai 
3786841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev,
3798424d759SYinghai Lu 				 struct resource_list_x *add_head,
3806841ec68SYinghai Lu 				 struct resource_list_x *fail_head)
3816841ec68SYinghai Lu {
3826841ec68SYinghai Lu 	struct resource_list head;
3836841ec68SYinghai Lu 
3846841ec68SYinghai Lu 	head.next = NULL;
3856841ec68SYinghai Lu 	__dev_sort_resources(dev, &head);
3868424d759SYinghai Lu 	__assign_resources_sorted(&head, add_head, fail_head);
3876841ec68SYinghai Lu 
3886841ec68SYinghai Lu }
3896841ec68SYinghai Lu 
3906841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus,
3919e8bf93aSRam Pai 					 struct resource_list_x *realloc_head,
3926841ec68SYinghai Lu 					 struct resource_list_x *fail_head)
3936841ec68SYinghai Lu {
3946841ec68SYinghai Lu 	struct pci_dev *dev;
3956841ec68SYinghai Lu 	struct resource_list head;
3966841ec68SYinghai Lu 
3976841ec68SYinghai Lu 	head.next = NULL;
3986841ec68SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
3996841ec68SYinghai Lu 		__dev_sort_resources(dev, &head);
4006841ec68SYinghai Lu 
4019e8bf93aSRam Pai 	__assign_resources_sorted(&head, realloc_head, fail_head);
4026841ec68SYinghai Lu }
4036841ec68SYinghai Lu 
404b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus)
4051da177e4SLinus Torvalds {
4061da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
407c7dabef8SBjorn Helgaas 	struct resource *res;
4081da177e4SLinus Torvalds 	struct pci_bus_region region;
4091da177e4SLinus Torvalds 
410865df576SBjorn Helgaas 	dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
411865df576SBjorn Helgaas 		 bus->secondary, bus->subordinate);
4121da177e4SLinus Torvalds 
413c7dabef8SBjorn Helgaas 	res = bus->resource[0];
414c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
415c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
4161da177e4SLinus Torvalds 		/*
4171da177e4SLinus Torvalds 		 * The IO resource is allocated a range twice as large as it
4181da177e4SLinus Torvalds 		 * would normally need.  This allows us to set both IO regs.
4191da177e4SLinus Torvalds 		 */
420c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4211da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
4221da177e4SLinus Torvalds 					region.start);
4231da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
4241da177e4SLinus Torvalds 					region.end);
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
427c7dabef8SBjorn Helgaas 	res = bus->resource[1];
428c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
429c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
430c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4311da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
4321da177e4SLinus Torvalds 					region.start);
4331da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
4341da177e4SLinus Torvalds 					region.end);
4351da177e4SLinus Torvalds 	}
4361da177e4SLinus Torvalds 
437c7dabef8SBjorn Helgaas 	res = bus->resource[2];
438c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
439c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
440c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4411da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
4421da177e4SLinus Torvalds 					region.start);
4431da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
4441da177e4SLinus Torvalds 					region.end);
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds 
447c7dabef8SBjorn Helgaas 	res = bus->resource[3];
448c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
449c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
450c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4511da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
4521da177e4SLinus Torvalds 					region.start);
4531da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
4541da177e4SLinus Torvalds 					region.end);
4551da177e4SLinus Torvalds 	}
4561da177e4SLinus Torvalds }
457b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus);
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected.
4601da177e4SLinus Torvalds    PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
4611da177e4SLinus Torvalds    requires that if there is no I/O ports or memory behind the
4621da177e4SLinus Torvalds    bridge, corresponding range must be turned off by writing base
4631da177e4SLinus Torvalds    value greater than limit to the bridge's base/limit registers.
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds    Note: care must be taken when updating I/O base/limit registers
4661da177e4SLinus Torvalds    of bridges which support 32-bit I/O. This update requires two
4671da177e4SLinus Torvalds    config space writes, so it's quite possible that an I/O window of
4681da177e4SLinus Torvalds    the bridge will have some undesirable address (e.g. 0) after the
4691da177e4SLinus Torvalds    first write. Ditto 64-bit prefetchable MMIO.  */
4707cc5997dSYinghai Lu static void pci_setup_bridge_io(struct pci_bus *bus)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
473c7dabef8SBjorn Helgaas 	struct resource *res;
4741da177e4SLinus Torvalds 	struct pci_bus_region region;
4757cc5997dSYinghai Lu 	u32 l, io_upper16;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	/* Set up the top and bottom of the PCI I/O segment for this bus. */
478c7dabef8SBjorn Helgaas 	res = bus->resource[0];
479c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
480c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
4811da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_IO_BASE, &l);
4821da177e4SLinus Torvalds 		l &= 0xffff0000;
4831da177e4SLinus Torvalds 		l |= (region.start >> 8) & 0x00f0;
4841da177e4SLinus Torvalds 		l |= region.end & 0xf000;
4851da177e4SLinus Torvalds 		/* Set up upper 16 bits of I/O base/limit. */
4861da177e4SLinus Torvalds 		io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
487c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4887cc5997dSYinghai Lu 	} else {
4891da177e4SLinus Torvalds 		/* Clear upper 16 bits of I/O base/limit. */
4901da177e4SLinus Torvalds 		io_upper16 = 0;
4911da177e4SLinus Torvalds 		l = 0x00f0;
4921da177e4SLinus Torvalds 	}
4931da177e4SLinus Torvalds 	/* Temporarily disable the I/O range before updating PCI_IO_BASE. */
4941da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
4951da177e4SLinus Torvalds 	/* Update lower 16 bits of I/O base/limit. */
4961da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE, l);
4971da177e4SLinus Torvalds 	/* Update upper 16 bits of I/O base/limit. */
4981da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
4997cc5997dSYinghai Lu }
5001da177e4SLinus Torvalds 
5017cc5997dSYinghai Lu static void pci_setup_bridge_mmio(struct pci_bus *bus)
5027cc5997dSYinghai Lu {
5037cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
5047cc5997dSYinghai Lu 	struct resource *res;
5057cc5997dSYinghai Lu 	struct pci_bus_region region;
5067cc5997dSYinghai Lu 	u32 l;
5077cc5997dSYinghai Lu 
5087cc5997dSYinghai Lu 	/* Set up the top and bottom of the PCI Memory segment for this bus. */
509c7dabef8SBjorn Helgaas 	res = bus->resource[1];
510c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
511c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
5121da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
5131da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
514c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5157cc5997dSYinghai Lu 	} else {
5161da177e4SLinus Torvalds 		l = 0x0000fff0;
5171da177e4SLinus Torvalds 	}
5181da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
5197cc5997dSYinghai Lu }
5207cc5997dSYinghai Lu 
5217cc5997dSYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
5227cc5997dSYinghai Lu {
5237cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
5247cc5997dSYinghai Lu 	struct resource *res;
5257cc5997dSYinghai Lu 	struct pci_bus_region region;
5267cc5997dSYinghai Lu 	u32 l, bu, lu;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	/* Clear out the upper 32 bits of PREF limit.
5291da177e4SLinus Torvalds 	   If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
5301da177e4SLinus Torvalds 	   disables PREF range, which is ok. */
5311da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	/* Set up PREF base/limit. */
534c40a22e0SBenjamin Herrenschmidt 	bu = lu = 0;
535c7dabef8SBjorn Helgaas 	res = bus->resource[2];
536c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
537c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_PREFETCH) {
5381da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
5391da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
540c7dabef8SBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64) {
54113d36c24SAndrew Morton 			bu = upper_32_bits(region.start);
54213d36c24SAndrew Morton 			lu = upper_32_bits(region.end);
5431f82de10SYinghai Lu 		}
544c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5457cc5997dSYinghai Lu 	} else {
5461da177e4SLinus Torvalds 		l = 0x0000fff0;
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
5491da177e4SLinus Torvalds 
550c40a22e0SBenjamin Herrenschmidt 	/* Set the upper 32 bits of PREF base & limit. */
551c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
552c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
5537cc5997dSYinghai Lu }
5547cc5997dSYinghai Lu 
5557cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
5567cc5997dSYinghai Lu {
5577cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
5587cc5997dSYinghai Lu 
5597cc5997dSYinghai Lu 	dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
5607cc5997dSYinghai Lu 		 bus->secondary, bus->subordinate);
5617cc5997dSYinghai Lu 
5627cc5997dSYinghai Lu 	if (type & IORESOURCE_IO)
5637cc5997dSYinghai Lu 		pci_setup_bridge_io(bus);
5647cc5997dSYinghai Lu 
5657cc5997dSYinghai Lu 	if (type & IORESOURCE_MEM)
5667cc5997dSYinghai Lu 		pci_setup_bridge_mmio(bus);
5677cc5997dSYinghai Lu 
5687cc5997dSYinghai Lu 	if (type & IORESOURCE_PREFETCH)
5697cc5997dSYinghai Lu 		pci_setup_bridge_mmio_pref(bus);
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
574e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus)
5757cc5997dSYinghai Lu {
5767cc5997dSYinghai Lu 	unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
5777cc5997dSYinghai Lu 				  IORESOURCE_PREFETCH;
5787cc5997dSYinghai Lu 
5797cc5997dSYinghai Lu 	__pci_setup_bridge(bus, type);
5807cc5997dSYinghai Lu }
5817cc5997dSYinghai Lu 
5821da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and
5831da177e4SLinus Torvalds    prefetchable memory ranges. If not, the respective
5841da177e4SLinus Torvalds    base/limit registers must be read-only and read as 0. */
58596bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	u16 io;
5881da177e4SLinus Torvalds 	u32 pmem;
5891da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
5901da177e4SLinus Torvalds 	struct resource *b_res;
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
5931da177e4SLinus Torvalds 	b_res[1].flags |= IORESOURCE_MEM;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_IO_BASE, &io);
5961da177e4SLinus Torvalds 	if (!io) {
5971da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
5981da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_IO_BASE, &io);
5991da177e4SLinus Torvalds  		pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
6001da177e4SLinus Torvalds  	}
6011da177e4SLinus Torvalds  	if (io)
6021da177e4SLinus Torvalds 		b_res[0].flags |= IORESOURCE_IO;
6031da177e4SLinus Torvalds 	/*  DECchip 21050 pass 2 errata: the bridge may miss an address
6041da177e4SLinus Torvalds 	    disconnect boundary by one PCI data phase.
6051da177e4SLinus Torvalds 	    Workaround: do not use prefetching on this device. */
6061da177e4SLinus Torvalds 	if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
6071da177e4SLinus Torvalds 		return;
6081da177e4SLinus Torvalds 	pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
6091da177e4SLinus Torvalds 	if (!pmem) {
6101da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
6111da177e4SLinus Torvalds 					       0xfff0fff0);
6121da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
6131da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
6141da177e4SLinus Torvalds 	}
6151f82de10SYinghai Lu 	if (pmem) {
6161da177e4SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
61799586105SYinghai Lu 		if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
61899586105SYinghai Lu 		    PCI_PREF_RANGE_TYPE_64) {
6191f82de10SYinghai Lu 			b_res[2].flags |= IORESOURCE_MEM_64;
62099586105SYinghai Lu 			b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
62199586105SYinghai Lu 		}
6221f82de10SYinghai Lu 	}
6231f82de10SYinghai Lu 
6241f82de10SYinghai Lu 	/* double check if bridge does support 64 bit pref */
6251f82de10SYinghai Lu 	if (b_res[2].flags & IORESOURCE_MEM_64) {
6261f82de10SYinghai Lu 		u32 mem_base_hi, tmp;
6271f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
6281f82de10SYinghai Lu 					 &mem_base_hi);
6291f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
6301f82de10SYinghai Lu 					       0xffffffff);
6311f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
6321f82de10SYinghai Lu 		if (!tmp)
6331f82de10SYinghai Lu 			b_res[2].flags &= ~IORESOURCE_MEM_64;
6341f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
6351f82de10SYinghai Lu 				       mem_base_hi);
6361f82de10SYinghai Lu 	}
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds /* Helper function for sizing routines: find first available
6401da177e4SLinus Torvalds    bus resource of a given type. Note: we intentionally skip
6411da177e4SLinus Torvalds    the bus resources which have already been assigned (that is,
6421da177e4SLinus Torvalds    have non-NULL parent resource). */
64396bde06aSSam Ravnborg static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
6441da177e4SLinus Torvalds {
6451da177e4SLinus Torvalds 	int i;
6461da177e4SLinus Torvalds 	struct resource *r;
6471da177e4SLinus Torvalds 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
6481da177e4SLinus Torvalds 				  IORESOURCE_PREFETCH;
6491da177e4SLinus Torvalds 
65089a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, r, i) {
651299de034SIvan Kokshaysky 		if (r == &ioport_resource || r == &iomem_resource)
652299de034SIvan Kokshaysky 			continue;
65355a10984SJesse Barnes 		if (r && (r->flags & type_mask) == type && !r->parent)
6541da177e4SLinus Torvalds 			return r;
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 	return NULL;
6571da177e4SLinus Torvalds }
6581da177e4SLinus Torvalds 
65913583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size,
66013583b16SRam Pai 		resource_size_t min_size,
66113583b16SRam Pai 		resource_size_t size1,
66213583b16SRam Pai 		resource_size_t old_size,
66313583b16SRam Pai 		resource_size_t align)
66413583b16SRam Pai {
66513583b16SRam Pai 	if (size < min_size)
66613583b16SRam Pai 		size = min_size;
66713583b16SRam Pai 	if (old_size == 1 )
66813583b16SRam Pai 		old_size = 0;
66913583b16SRam Pai 	/* To be fixed in 2.5: we should have sort of HAVE_ISA
67013583b16SRam Pai 	   flag in the struct pci_bus. */
67113583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
67213583b16SRam Pai 	size = (size & 0xff) + ((size & ~0xffUL) << 2);
67313583b16SRam Pai #endif
67413583b16SRam Pai 	size = ALIGN(size + size1, align);
67513583b16SRam Pai 	if (size < old_size)
67613583b16SRam Pai 		size = old_size;
67713583b16SRam Pai 	return size;
67813583b16SRam Pai }
67913583b16SRam Pai 
68013583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size,
68113583b16SRam Pai 		resource_size_t min_size,
68213583b16SRam Pai 		resource_size_t size1,
68313583b16SRam Pai 		resource_size_t old_size,
68413583b16SRam Pai 		resource_size_t align)
68513583b16SRam Pai {
68613583b16SRam Pai 	if (size < min_size)
68713583b16SRam Pai 		size = min_size;
68813583b16SRam Pai 	if (old_size == 1 )
68913583b16SRam Pai 		old_size = 0;
69013583b16SRam Pai 	if (size < old_size)
69113583b16SRam Pai 		size = old_size;
69213583b16SRam Pai 	size = ALIGN(size + size1, align);
69313583b16SRam Pai 	return size;
69413583b16SRam Pai }
69513583b16SRam Pai 
696c8adf9a3SRam Pai /**
697c8adf9a3SRam Pai  * pbus_size_io() - size the io window of a given bus
698c8adf9a3SRam Pai  *
699c8adf9a3SRam Pai  * @bus : the bus
700c8adf9a3SRam Pai  * @min_size : the minimum io window that must to be allocated
701c8adf9a3SRam Pai  * @add_size : additional optional io window
7029e8bf93aSRam Pai  * @realloc_head : track the additional io window on this list
703c8adf9a3SRam Pai  *
704c8adf9a3SRam Pai  * Sizing the IO windows of the PCI-PCI bridge is trivial,
705c8adf9a3SRam Pai  * since these windows have 4K granularity and the IO ranges
706c8adf9a3SRam Pai  * of non-bridge PCI devices are limited to 256 bytes.
707c8adf9a3SRam Pai  * We must be careful with the ISA aliasing though.
708c8adf9a3SRam Pai  */
709c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
7109e8bf93aSRam Pai 		resource_size_t add_size, struct resource_list_x *realloc_head)
7111da177e4SLinus Torvalds {
7121da177e4SLinus Torvalds 	struct pci_dev *dev;
7131da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
714c8adf9a3SRam Pai 	unsigned long size = 0, size0 = 0, size1 = 0;
715be768912SYinghai Lu 	resource_size_t children_add_size = 0;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	if (!b_res)
7181da177e4SLinus Torvalds  		return;
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
7211da177e4SLinus Torvalds 		int i;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
7241da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
7251da177e4SLinus Torvalds 			unsigned long r_size;
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 			if (r->parent || !(r->flags & IORESOURCE_IO))
7281da177e4SLinus Torvalds 				continue;
729022edd86SZhao, Yu 			r_size = resource_size(r);
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 			if (r_size < 0x400)
7321da177e4SLinus Torvalds 				/* Might be re-aligned for ISA */
7331da177e4SLinus Torvalds 				size += r_size;
7341da177e4SLinus Torvalds 			else
7351da177e4SLinus Torvalds 				size1 += r_size;
736be768912SYinghai Lu 
7379e8bf93aSRam Pai 			if (realloc_head)
7389e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
7391da177e4SLinus Torvalds 		}
7401da177e4SLinus Torvalds 	}
741c8adf9a3SRam Pai 	size0 = calculate_iosize(size, min_size, size1,
74213583b16SRam Pai 			resource_size(b_res), 4096);
743be768912SYinghai Lu 	if (children_add_size > add_size)
744be768912SYinghai Lu 		add_size = children_add_size;
7459e8bf93aSRam Pai 	size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
746a4ac9feaSYinghai Lu 		calculate_iosize(size, min_size, add_size + size1,
747c8adf9a3SRam Pai 			resource_size(b_res), 4096);
748c8adf9a3SRam Pai 	if (!size0 && !size1) {
749865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
750865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
751865df576SBjorn Helgaas 				 "%pR to [bus %02x-%02x] (unused)\n", b_res,
752865df576SBjorn Helgaas 				 bus->secondary, bus->subordinate);
7531da177e4SLinus Torvalds 		b_res->flags = 0;
7541da177e4SLinus Torvalds 		return;
7551da177e4SLinus Torvalds 	}
7561da177e4SLinus Torvalds 	/* Alignment of the IO window is always 4K */
7571da177e4SLinus Torvalds 	b_res->start = 4096;
758c8adf9a3SRam Pai 	b_res->end = b_res->start + size0 - 1;
75988452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
7609e8bf93aSRam Pai 	if (size1 > size0 && realloc_head)
7619e8bf93aSRam Pai 		add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096);
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
764c8adf9a3SRam Pai /**
765c8adf9a3SRam Pai  * pbus_size_mem() - size the memory window of a given bus
766c8adf9a3SRam Pai  *
767c8adf9a3SRam Pai  * @bus : the bus
768c8adf9a3SRam Pai  * @min_size : the minimum memory window that must to be allocated
769c8adf9a3SRam Pai  * @add_size : additional optional memory window
7709e8bf93aSRam Pai  * @realloc_head : track the additional memory window on this list
771c8adf9a3SRam Pai  *
772c8adf9a3SRam Pai  * Calculate the size of the bus and minimal alignment which
773c8adf9a3SRam Pai  * guarantees that all child resources fit in this size.
774c8adf9a3SRam Pai  */
77528760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
776c8adf9a3SRam Pai 			 unsigned long type, resource_size_t min_size,
777c8adf9a3SRam Pai 			resource_size_t add_size,
7789e8bf93aSRam Pai 			struct resource_list_x *realloc_head)
7791da177e4SLinus Torvalds {
7801da177e4SLinus Torvalds 	struct pci_dev *dev;
781c8adf9a3SRam Pai 	resource_size_t min_align, align, size, size0, size1;
782c40a22e0SBenjamin Herrenschmidt 	resource_size_t aligns[12];	/* Alignments from 1Mb to 2Gb */
7831da177e4SLinus Torvalds 	int order, max_order;
7841da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, type);
7851f82de10SYinghai Lu 	unsigned int mem64_mask = 0;
786be768912SYinghai Lu 	resource_size_t children_add_size = 0;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	if (!b_res)
7891da177e4SLinus Torvalds 		return 0;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
7921da177e4SLinus Torvalds 	max_order = 0;
7931da177e4SLinus Torvalds 	size = 0;
7941da177e4SLinus Torvalds 
7951f82de10SYinghai Lu 	mem64_mask = b_res->flags & IORESOURCE_MEM_64;
7961f82de10SYinghai Lu 	b_res->flags &= ~IORESOURCE_MEM_64;
7971f82de10SYinghai Lu 
7981da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
7991da177e4SLinus Torvalds 		int i;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
8021da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
803c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 			if (r->parent || (r->flags & mask) != type)
8061da177e4SLinus Torvalds 				continue;
807022edd86SZhao, Yu 			r_size = resource_size(r);
8082aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV
8092aceefcbSYinghai Lu 			/* put SRIOV requested res to the optional list */
8109e8bf93aSRam Pai 			if (realloc_head && i >= PCI_IOV_RESOURCES &&
8112aceefcbSYinghai Lu 					i <= PCI_IOV_RESOURCE_END) {
8122aceefcbSYinghai Lu 				r->end = r->start - 1;
8139e8bf93aSRam Pai 				add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */);
8142aceefcbSYinghai Lu 				children_add_size += r_size;
8152aceefcbSYinghai Lu 				continue;
8162aceefcbSYinghai Lu 			}
8172aceefcbSYinghai Lu #endif
8181da177e4SLinus Torvalds 			/* For bridges size != alignment */
8196faf17f6SChris Wright 			align = pci_resource_alignment(dev, r);
8201da177e4SLinus Torvalds 			order = __ffs(align) - 20;
8211da177e4SLinus Torvalds 			if (order > 11) {
822865df576SBjorn Helgaas 				dev_warn(&dev->dev, "disabling BAR %d: %pR "
823865df576SBjorn Helgaas 					 "(bad alignment %#llx)\n", i, r,
824865df576SBjorn Helgaas 					 (unsigned long long) align);
8251da177e4SLinus Torvalds 				r->flags = 0;
8261da177e4SLinus Torvalds 				continue;
8271da177e4SLinus Torvalds 			}
8281da177e4SLinus Torvalds 			size += r_size;
8291da177e4SLinus Torvalds 			if (order < 0)
8301da177e4SLinus Torvalds 				order = 0;
8311da177e4SLinus Torvalds 			/* Exclude ranges with size > align from
8321da177e4SLinus Torvalds 			   calculation of the alignment. */
8331da177e4SLinus Torvalds 			if (r_size == align)
8341da177e4SLinus Torvalds 				aligns[order] += align;
8351da177e4SLinus Torvalds 			if (order > max_order)
8361da177e4SLinus Torvalds 				max_order = order;
8371f82de10SYinghai Lu 			mem64_mask &= r->flags & IORESOURCE_MEM_64;
838be768912SYinghai Lu 
8399e8bf93aSRam Pai 			if (realloc_head)
8409e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
8411da177e4SLinus Torvalds 		}
8421da177e4SLinus Torvalds 	}
8431da177e4SLinus Torvalds 	align = 0;
8441da177e4SLinus Torvalds 	min_align = 0;
8451da177e4SLinus Torvalds 	for (order = 0; order <= max_order; order++) {
8468308c54dSJeremy Fitzhardinge 		resource_size_t align1 = 1;
8478308c54dSJeremy Fitzhardinge 
8488308c54dSJeremy Fitzhardinge 		align1 <<= (order + 20);
8498308c54dSJeremy Fitzhardinge 
8501da177e4SLinus Torvalds 		if (!align)
8511da177e4SLinus Torvalds 			min_align = align1;
8526f6f8c2fSMilind Arun Choudhary 		else if (ALIGN(align + min_align, min_align) < align1)
8531da177e4SLinus Torvalds 			min_align = align1 >> 1;
8541da177e4SLinus Torvalds 		align += aligns[order];
8551da177e4SLinus Torvalds 	}
856b42282e5SLinus Torvalds 	size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
857be768912SYinghai Lu 	if (children_add_size > add_size)
858be768912SYinghai Lu 		add_size = children_add_size;
8599e8bf93aSRam Pai 	size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
860a4ac9feaSYinghai Lu 		calculate_memsize(size, min_size, add_size,
861b42282e5SLinus Torvalds 				resource_size(b_res), min_align);
862c8adf9a3SRam Pai 	if (!size0 && !size1) {
863865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
864865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
865865df576SBjorn Helgaas 				 "%pR to [bus %02x-%02x] (unused)\n", b_res,
866865df576SBjorn Helgaas 				 bus->secondary, bus->subordinate);
8671da177e4SLinus Torvalds 		b_res->flags = 0;
8681da177e4SLinus Torvalds 		return 1;
8691da177e4SLinus Torvalds 	}
8701da177e4SLinus Torvalds 	b_res->start = min_align;
871c8adf9a3SRam Pai 	b_res->end = size0 + min_align - 1;
872c8adf9a3SRam Pai 	b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
8739e8bf93aSRam Pai 	if (size1 > size0 && realloc_head)
8749e8bf93aSRam Pai 		add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
8751da177e4SLinus Torvalds 	return 1;
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
8780a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res)
8790a2daa1cSRam Pai {
8800a2daa1cSRam Pai 	if (res->flags & IORESOURCE_IO)
8810a2daa1cSRam Pai 		return pci_cardbus_io_size;
8820a2daa1cSRam Pai 	if (res->flags & IORESOURCE_MEM)
8830a2daa1cSRam Pai 		return pci_cardbus_mem_size;
8840a2daa1cSRam Pai 	return 0;
8850a2daa1cSRam Pai }
8860a2daa1cSRam Pai 
8870a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus,
8889e8bf93aSRam Pai 			struct resource_list_x *realloc_head)
8891da177e4SLinus Torvalds {
8901da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
8911da177e4SLinus Torvalds 	struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
8921da177e4SLinus Torvalds 	u16 ctrl;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	/*
8951da177e4SLinus Torvalds 	 * Reserve some resources for CardBus.  We reserve
8961da177e4SLinus Torvalds 	 * a fixed amount of bus space for CardBus bridges.
8971da177e4SLinus Torvalds 	 */
898934b7024SLinus Torvalds 	b_res[0].start = 0;
899934b7024SLinus Torvalds 	b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
9009e8bf93aSRam Pai 	if (realloc_head)
9019e8bf93aSRam Pai 		add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */);
9021da177e4SLinus Torvalds 
903934b7024SLinus Torvalds 	b_res[1].start = 0;
904934b7024SLinus Torvalds 	b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
9059e8bf93aSRam Pai 	if (realloc_head)
9069e8bf93aSRam Pai 		add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */);
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	/*
9091da177e4SLinus Torvalds 	 * Check whether prefetchable memory is supported
9101da177e4SLinus Torvalds 	 * by this bridge.
9111da177e4SLinus Torvalds 	 */
9121da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
9131da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
9141da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
9151da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
9161da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
9171da177e4SLinus Torvalds 	}
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	/*
9201da177e4SLinus Torvalds 	 * If we have prefetchable memory support, allocate
9211da177e4SLinus Torvalds 	 * two regions.  Otherwise, allocate one region of
9221da177e4SLinus Torvalds 	 * twice the size.
9231da177e4SLinus Torvalds 	 */
9241da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
925934b7024SLinus Torvalds 		b_res[2].start = 0;
926934b7024SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
9279e8bf93aSRam Pai 		if (realloc_head)
9289e8bf93aSRam Pai 			add_to_list(realloc_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */);
9291da177e4SLinus Torvalds 
930934b7024SLinus Torvalds 		b_res[3].start = 0;
931934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
9329e8bf93aSRam Pai 		if (realloc_head)
9339e8bf93aSRam Pai 			add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */);
9341da177e4SLinus Torvalds 	} else {
935934b7024SLinus Torvalds 		b_res[3].start = 0;
936934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
9379e8bf93aSRam Pai 		if (realloc_head)
9389e8bf93aSRam Pai 			add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size * 2, 0 /* dont care */);
9391da177e4SLinus Torvalds 	}
9400a2daa1cSRam Pai 
9410a2daa1cSRam Pai 	/* set the size of the resource to zero, so that the resource does not
9420a2daa1cSRam Pai 	 * get assigned during required-resource allocation cycle but gets assigned
9430a2daa1cSRam Pai 	 * during the optional-resource allocation cycle.
9440a2daa1cSRam Pai  	 */
9450a2daa1cSRam Pai 	b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1;
9460a2daa1cSRam Pai 	b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0;
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
949c8adf9a3SRam Pai void __ref __pci_bus_size_bridges(struct pci_bus *bus,
9509e8bf93aSRam Pai 			struct resource_list_x *realloc_head)
9511da177e4SLinus Torvalds {
9521da177e4SLinus Torvalds 	struct pci_dev *dev;
9531da177e4SLinus Torvalds 	unsigned long mask, prefmask;
954c8adf9a3SRam Pai 	resource_size_t additional_mem_size = 0, additional_io_size = 0;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
9571da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
9581da177e4SLinus Torvalds 		if (!b)
9591da177e4SLinus Torvalds 			continue;
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 		switch (dev->class >> 8) {
9621da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
9639e8bf93aSRam Pai 			pci_bus_size_cardbus(b, realloc_head);
9641da177e4SLinus Torvalds 			break;
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
9671da177e4SLinus Torvalds 		default:
9689e8bf93aSRam Pai 			__pci_bus_size_bridges(b, realloc_head);
9691da177e4SLinus Torvalds 			break;
9701da177e4SLinus Torvalds 		}
9711da177e4SLinus Torvalds 	}
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	/* The root bus? */
9741da177e4SLinus Torvalds 	if (!bus->self)
9751da177e4SLinus Torvalds 		return;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	switch (bus->self->class >> 8) {
9781da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_CARDBUS:
9791da177e4SLinus Torvalds 		/* don't size cardbuses yet. */
9801da177e4SLinus Torvalds 		break;
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_PCI:
9831da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
98428760489SEric W. Biederman 		if (bus->self->is_hotplug_bridge) {
985c8adf9a3SRam Pai 			additional_io_size  = pci_hotplug_io_size;
986c8adf9a3SRam Pai 			additional_mem_size = pci_hotplug_mem_size;
98728760489SEric W. Biederman 		}
988c8adf9a3SRam Pai 		/*
989c8adf9a3SRam Pai 		 * Follow thru
990c8adf9a3SRam Pai 		 */
9911da177e4SLinus Torvalds 	default:
99219aa7ee4SYinghai Lu 		pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
99319aa7ee4SYinghai Lu 			     additional_io_size, realloc_head);
9941da177e4SLinus Torvalds 		/* If the bridge supports prefetchable range, size it
9951da177e4SLinus Torvalds 		   separately. If it doesn't, or its prefetchable window
9961da177e4SLinus Torvalds 		   has already been allocated by arch code, try
9971da177e4SLinus Torvalds 		   non-prefetchable range for both types of PCI memory
9981da177e4SLinus Torvalds 		   resources. */
9991da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
10001da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
100119aa7ee4SYinghai Lu 		if (pbus_size_mem(bus, prefmask, prefmask,
100219aa7ee4SYinghai Lu 				  realloc_head ? 0 : additional_mem_size,
100319aa7ee4SYinghai Lu 				  additional_mem_size, realloc_head))
10041da177e4SLinus Torvalds 			mask = prefmask; /* Success, size non-prefetch only. */
100528760489SEric W. Biederman 		else
1006c8adf9a3SRam Pai 			additional_mem_size += additional_mem_size;
100719aa7ee4SYinghai Lu 		pbus_size_mem(bus, mask, IORESOURCE_MEM,
100819aa7ee4SYinghai Lu 				realloc_head ? 0 : additional_mem_size,
100919aa7ee4SYinghai Lu 				additional_mem_size, realloc_head);
10101da177e4SLinus Torvalds 		break;
10111da177e4SLinus Torvalds 	}
10121da177e4SLinus Torvalds }
1013c8adf9a3SRam Pai 
1014c8adf9a3SRam Pai void __ref pci_bus_size_bridges(struct pci_bus *bus)
1015c8adf9a3SRam Pai {
1016c8adf9a3SRam Pai 	__pci_bus_size_bridges(bus, NULL);
1017c8adf9a3SRam Pai }
10181da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
10191da177e4SLinus Torvalds 
1020568ddef8SYinghai Lu static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
10219e8bf93aSRam Pai 					 struct resource_list_x *realloc_head,
1022568ddef8SYinghai Lu 					 struct resource_list_x *fail_head)
10231da177e4SLinus Torvalds {
10241da177e4SLinus Torvalds 	struct pci_bus *b;
10251da177e4SLinus Torvalds 	struct pci_dev *dev;
10261da177e4SLinus Torvalds 
10279e8bf93aSRam Pai 	pbus_assign_resources_sorted(bus, realloc_head, fail_head);
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
10301da177e4SLinus Torvalds 		b = dev->subordinate;
10311da177e4SLinus Torvalds 		if (!b)
10321da177e4SLinus Torvalds 			continue;
10331da177e4SLinus Torvalds 
10349e8bf93aSRam Pai 		__pci_bus_assign_resources(b, realloc_head, fail_head);
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 		switch (dev->class >> 8) {
10371da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
10386841ec68SYinghai Lu 			if (!pci_is_enabled(dev))
10391da177e4SLinus Torvalds 				pci_setup_bridge(b);
10401da177e4SLinus Torvalds 			break;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
10431da177e4SLinus Torvalds 			pci_setup_cardbus(b);
10441da177e4SLinus Torvalds 			break;
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 		default:
104780ccba11SBjorn Helgaas 			dev_info(&dev->dev, "not setting up bridge for bus "
104880ccba11SBjorn Helgaas 				 "%04x:%02x\n", pci_domain_nr(b), b->number);
10491da177e4SLinus Torvalds 			break;
10501da177e4SLinus Torvalds 		}
10511da177e4SLinus Torvalds 	}
10521da177e4SLinus Torvalds }
1053568ddef8SYinghai Lu 
1054568ddef8SYinghai Lu void __ref pci_bus_assign_resources(const struct pci_bus *bus)
1055568ddef8SYinghai Lu {
1056c8adf9a3SRam Pai 	__pci_bus_assign_resources(bus, NULL, NULL);
1057568ddef8SYinghai Lu }
10581da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
10591da177e4SLinus Torvalds 
10606841ec68SYinghai Lu static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
10618424d759SYinghai Lu 					 struct resource_list_x *add_head,
10626841ec68SYinghai Lu 					 struct resource_list_x *fail_head)
10636841ec68SYinghai Lu {
10646841ec68SYinghai Lu 	struct pci_bus *b;
10656841ec68SYinghai Lu 
10668424d759SYinghai Lu 	pdev_assign_resources_sorted((struct pci_dev *)bridge,
10678424d759SYinghai Lu 					 add_head, fail_head);
10686841ec68SYinghai Lu 
10696841ec68SYinghai Lu 	b = bridge->subordinate;
10706841ec68SYinghai Lu 	if (!b)
10716841ec68SYinghai Lu 		return;
10726841ec68SYinghai Lu 
10738424d759SYinghai Lu 	__pci_bus_assign_resources(b, add_head, fail_head);
10746841ec68SYinghai Lu 
10756841ec68SYinghai Lu 	switch (bridge->class >> 8) {
10766841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_PCI:
10776841ec68SYinghai Lu 		pci_setup_bridge(b);
10786841ec68SYinghai Lu 		break;
10796841ec68SYinghai Lu 
10806841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_CARDBUS:
10816841ec68SYinghai Lu 		pci_setup_cardbus(b);
10826841ec68SYinghai Lu 		break;
10836841ec68SYinghai Lu 
10846841ec68SYinghai Lu 	default:
10856841ec68SYinghai Lu 		dev_info(&bridge->dev, "not setting up bridge for bus "
10866841ec68SYinghai Lu 			 "%04x:%02x\n", pci_domain_nr(b), b->number);
10876841ec68SYinghai Lu 		break;
10886841ec68SYinghai Lu 	}
10896841ec68SYinghai Lu }
10905009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus,
10915009b460SYinghai Lu 					  unsigned long type)
10925009b460SYinghai Lu {
10935009b460SYinghai Lu 	int idx;
10945009b460SYinghai Lu 	bool changed = false;
10955009b460SYinghai Lu 	struct pci_dev *dev;
10965009b460SYinghai Lu 	struct resource *r;
10975009b460SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
10985009b460SYinghai Lu 				  IORESOURCE_PREFETCH;
10995009b460SYinghai Lu 
11005009b460SYinghai Lu 	dev = bus->self;
11015009b460SYinghai Lu 	for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
11025009b460SYinghai Lu 	     idx++) {
11035009b460SYinghai Lu 		r = &dev->resource[idx];
11045009b460SYinghai Lu 		if ((r->flags & type_mask) != type)
11055009b460SYinghai Lu 			continue;
11065009b460SYinghai Lu 		if (!r->parent)
11075009b460SYinghai Lu 			continue;
11085009b460SYinghai Lu 		/*
11095009b460SYinghai Lu 		 * if there are children under that, we should release them
11105009b460SYinghai Lu 		 *  all
11115009b460SYinghai Lu 		 */
11125009b460SYinghai Lu 		release_child_resources(r);
11135009b460SYinghai Lu 		if (!release_resource(r)) {
11145009b460SYinghai Lu 			dev_printk(KERN_DEBUG, &dev->dev,
11155009b460SYinghai Lu 				 "resource %d %pR released\n", idx, r);
11165009b460SYinghai Lu 			/* keep the old size */
11175009b460SYinghai Lu 			r->end = resource_size(r) - 1;
11185009b460SYinghai Lu 			r->start = 0;
11195009b460SYinghai Lu 			r->flags = 0;
11205009b460SYinghai Lu 			changed = true;
11215009b460SYinghai Lu 		}
11225009b460SYinghai Lu 	}
11235009b460SYinghai Lu 
11245009b460SYinghai Lu 	if (changed) {
11255009b460SYinghai Lu 		/* avoiding touch the one without PREF */
11265009b460SYinghai Lu 		if (type & IORESOURCE_PREFETCH)
11275009b460SYinghai Lu 			type = IORESOURCE_PREFETCH;
11285009b460SYinghai Lu 		__pci_setup_bridge(bus, type);
11295009b460SYinghai Lu 	}
11305009b460SYinghai Lu }
11315009b460SYinghai Lu 
11325009b460SYinghai Lu enum release_type {
11335009b460SYinghai Lu 	leaf_only,
11345009b460SYinghai Lu 	whole_subtree,
11355009b460SYinghai Lu };
11365009b460SYinghai Lu /*
11375009b460SYinghai Lu  * try to release pci bridge resources that is from leaf bridge,
11385009b460SYinghai Lu  * so we can allocate big new one later
11395009b460SYinghai Lu  */
11405009b460SYinghai Lu static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
11415009b460SYinghai Lu 						   unsigned long type,
11425009b460SYinghai Lu 						   enum release_type rel_type)
11435009b460SYinghai Lu {
11445009b460SYinghai Lu 	struct pci_dev *dev;
11455009b460SYinghai Lu 	bool is_leaf_bridge = true;
11465009b460SYinghai Lu 
11475009b460SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
11485009b460SYinghai Lu 		struct pci_bus *b = dev->subordinate;
11495009b460SYinghai Lu 		if (!b)
11505009b460SYinghai Lu 			continue;
11515009b460SYinghai Lu 
11525009b460SYinghai Lu 		is_leaf_bridge = false;
11535009b460SYinghai Lu 
11545009b460SYinghai Lu 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
11555009b460SYinghai Lu 			continue;
11565009b460SYinghai Lu 
11575009b460SYinghai Lu 		if (rel_type == whole_subtree)
11585009b460SYinghai Lu 			pci_bus_release_bridge_resources(b, type,
11595009b460SYinghai Lu 						 whole_subtree);
11605009b460SYinghai Lu 	}
11615009b460SYinghai Lu 
11625009b460SYinghai Lu 	if (pci_is_root_bus(bus))
11635009b460SYinghai Lu 		return;
11645009b460SYinghai Lu 
11655009b460SYinghai Lu 	if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
11665009b460SYinghai Lu 		return;
11675009b460SYinghai Lu 
11685009b460SYinghai Lu 	if ((rel_type == whole_subtree) || is_leaf_bridge)
11695009b460SYinghai Lu 		pci_bridge_release_resources(bus, type);
11705009b460SYinghai Lu }
11715009b460SYinghai Lu 
117276fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
117376fbc263SYinghai Lu {
117489a74eccSBjorn Helgaas 	struct resource *res;
117576fbc263SYinghai Lu 	int i;
117676fbc263SYinghai Lu 
117789a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
11787c9342b8SYinghai Lu 		if (!res || !res->end || !res->flags)
117976fbc263SYinghai Lu                         continue;
118076fbc263SYinghai Lu 
1181c7dabef8SBjorn Helgaas 		dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
118276fbc263SYinghai Lu         }
118376fbc263SYinghai Lu }
118476fbc263SYinghai Lu 
118576fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
118676fbc263SYinghai Lu {
118776fbc263SYinghai Lu 	struct pci_bus *b;
118876fbc263SYinghai Lu 	struct pci_dev *dev;
118976fbc263SYinghai Lu 
119076fbc263SYinghai Lu 
119176fbc263SYinghai Lu 	pci_bus_dump_res(bus);
119276fbc263SYinghai Lu 
119376fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
119476fbc263SYinghai Lu 		b = dev->subordinate;
119576fbc263SYinghai Lu 		if (!b)
119676fbc263SYinghai Lu 			continue;
119776fbc263SYinghai Lu 
119876fbc263SYinghai Lu 		pci_bus_dump_resources(b);
119976fbc263SYinghai Lu 	}
120076fbc263SYinghai Lu }
120176fbc263SYinghai Lu 
1202da7822e5SYinghai Lu static int __init pci_bus_get_depth(struct pci_bus *bus)
1203da7822e5SYinghai Lu {
1204da7822e5SYinghai Lu 	int depth = 0;
1205da7822e5SYinghai Lu 	struct pci_dev *dev;
1206da7822e5SYinghai Lu 
1207da7822e5SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
1208da7822e5SYinghai Lu 		int ret;
1209da7822e5SYinghai Lu 		struct pci_bus *b = dev->subordinate;
1210da7822e5SYinghai Lu 		if (!b)
1211da7822e5SYinghai Lu 			continue;
1212da7822e5SYinghai Lu 
1213da7822e5SYinghai Lu 		ret = pci_bus_get_depth(b);
1214da7822e5SYinghai Lu 		if (ret + 1 > depth)
1215da7822e5SYinghai Lu 			depth = ret + 1;
1216da7822e5SYinghai Lu 	}
1217da7822e5SYinghai Lu 
1218da7822e5SYinghai Lu 	return depth;
1219da7822e5SYinghai Lu }
1220da7822e5SYinghai Lu static int __init pci_get_max_depth(void)
1221da7822e5SYinghai Lu {
1222da7822e5SYinghai Lu 	int depth = 0;
1223da7822e5SYinghai Lu 	struct pci_bus *bus;
1224da7822e5SYinghai Lu 
1225da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node) {
1226da7822e5SYinghai Lu 		int ret;
1227da7822e5SYinghai Lu 
1228da7822e5SYinghai Lu 		ret = pci_bus_get_depth(bus);
1229da7822e5SYinghai Lu 		if (ret > depth)
1230da7822e5SYinghai Lu 			depth = ret;
1231da7822e5SYinghai Lu 	}
1232da7822e5SYinghai Lu 
1233da7822e5SYinghai Lu 	return depth;
1234da7822e5SYinghai Lu }
1235da7822e5SYinghai Lu 
1236f483d392SRam Pai 
1237da7822e5SYinghai Lu /*
1238da7822e5SYinghai Lu  * first try will not touch pci bridge res
1239da7822e5SYinghai Lu  * second  and later try will clear small leaf bridge res
1240da7822e5SYinghai Lu  * will stop till to the max  deepth if can not find good one
1241da7822e5SYinghai Lu  */
12421da177e4SLinus Torvalds void __init
12431da177e4SLinus Torvalds pci_assign_unassigned_resources(void)
12441da177e4SLinus Torvalds {
12451da177e4SLinus Torvalds 	struct pci_bus *bus;
12469e8bf93aSRam Pai 	struct resource_list_x realloc_list; /* list of resources that
1247c8adf9a3SRam Pai 					want additional resources */
124819aa7ee4SYinghai Lu 	struct resource_list_x *add_list = NULL;
1249da7822e5SYinghai Lu 	int tried_times = 0;
1250da7822e5SYinghai Lu 	enum release_type rel_type = leaf_only;
1251da7822e5SYinghai Lu 	struct resource_list_x head, *list;
1252da7822e5SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1253da7822e5SYinghai Lu 				  IORESOURCE_PREFETCH;
1254da7822e5SYinghai Lu 	unsigned long failed_type;
125519aa7ee4SYinghai Lu 	int pci_try_num = 1;
1256da7822e5SYinghai Lu 
1257da7822e5SYinghai Lu 	head.next = NULL;
12589e8bf93aSRam Pai 	realloc_list.next = NULL;
1259da7822e5SYinghai Lu 
126019aa7ee4SYinghai Lu 	/* don't realloc if asked to do so */
126119aa7ee4SYinghai Lu 	if (pci_realloc_enabled()) {
126219aa7ee4SYinghai Lu 		int max_depth = pci_get_max_depth();
126319aa7ee4SYinghai Lu 
1264da7822e5SYinghai Lu 		pci_try_num = max_depth + 1;
1265da7822e5SYinghai Lu 		printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1266da7822e5SYinghai Lu 			 max_depth, pci_try_num);
126719aa7ee4SYinghai Lu 	}
1268da7822e5SYinghai Lu 
1269da7822e5SYinghai Lu again:
127019aa7ee4SYinghai Lu 	/*
127119aa7ee4SYinghai Lu 	 * last try will use add_list, otherwise will try good to have as
127219aa7ee4SYinghai Lu 	 * must have, so can realloc parent bridge resource
127319aa7ee4SYinghai Lu 	 */
127419aa7ee4SYinghai Lu 	if (tried_times + 1 == pci_try_num)
127519aa7ee4SYinghai Lu 		add_list = &realloc_list;
12761da177e4SLinus Torvalds 	/* Depth first, calculate sizes and alignments of all
12771da177e4SLinus Torvalds 	   subordinate buses. */
1278da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
127919aa7ee4SYinghai Lu 		__pci_bus_size_bridges(bus, add_list);
1280c8adf9a3SRam Pai 
12811da177e4SLinus Torvalds 	/* Depth last, allocate resources and update the hardware. */
1282da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
128319aa7ee4SYinghai Lu 		__pci_bus_assign_resources(bus, add_list, &head);
128419aa7ee4SYinghai Lu 	if (add_list)
128519aa7ee4SYinghai Lu 		BUG_ON(add_list->next);
1286da7822e5SYinghai Lu 	tried_times++;
1287da7822e5SYinghai Lu 
1288da7822e5SYinghai Lu 	/* any device complain? */
1289da7822e5SYinghai Lu 	if (!head.next)
1290da7822e5SYinghai Lu 		goto enable_and_dump;
1291f483d392SRam Pai 
1292da7822e5SYinghai Lu 	failed_type = 0;
1293da7822e5SYinghai Lu 	for (list = head.next; list;) {
1294da7822e5SYinghai Lu 		failed_type |= list->flags;
1295da7822e5SYinghai Lu 		list = list->next;
1296da7822e5SYinghai 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)) {
1303da7822e5SYinghai Lu 		free_list(resource_list_x, &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 	 */
1318da7822e5SYinghai Lu 	for (list = head.next; list;) {
1319da7822e5SYinghai Lu 		bus = list->dev->bus;
1320da7822e5SYinghai Lu 		pci_bus_release_bridge_resources(bus, list->flags & type_mask,
1321da7822e5SYinghai Lu 						  rel_type);
1322da7822e5SYinghai Lu 		list = list->next;
1323da7822e5SYinghai Lu 	}
1324da7822e5SYinghai Lu 	/* restore size and flags */
1325da7822e5SYinghai Lu 	for (list = head.next; list;) {
1326da7822e5SYinghai Lu 		struct resource *res = list->res;
1327da7822e5SYinghai Lu 
1328da7822e5SYinghai Lu 		res->start = list->start;
1329da7822e5SYinghai Lu 		res->end = list->end;
1330da7822e5SYinghai Lu 		res->flags = list->flags;
1331da7822e5SYinghai Lu 		if (list->dev->subordinate)
1332da7822e5SYinghai Lu 			res->flags = 0;
1333da7822e5SYinghai Lu 
1334da7822e5SYinghai Lu 		list = list->next;
1335da7822e5SYinghai Lu 	}
1336da7822e5SYinghai Lu 	free_list(resource_list_x, &head);
1337da7822e5SYinghai Lu 
1338da7822e5SYinghai Lu 	goto again;
1339da7822e5SYinghai Lu 
1340da7822e5SYinghai Lu enable_and_dump:
1341da7822e5SYinghai Lu 	/* Depth last, update the hardware. */
1342da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
1343da7822e5SYinghai Lu 		pci_enable_bridges(bus);
134476fbc263SYinghai Lu 
134576fbc263SYinghai Lu 	/* dump the resource on buses */
1346da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
134776fbc263SYinghai Lu 		pci_bus_dump_resources(bus);
134876fbc263SYinghai Lu }
13496841ec68SYinghai Lu 
13506841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
13516841ec68SYinghai Lu {
13526841ec68SYinghai Lu 	struct pci_bus *parent = bridge->subordinate;
13538424d759SYinghai Lu 	struct resource_list_x add_list; /* list of resources that
13548424d759SYinghai Lu 					want additional resources */
135532180e40SYinghai Lu 	int tried_times = 0;
135632180e40SYinghai Lu 	struct resource_list_x head, *list;
13576841ec68SYinghai Lu 	int retval;
135832180e40SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
135932180e40SYinghai Lu 				  IORESOURCE_PREFETCH;
13606841ec68SYinghai Lu 
136132180e40SYinghai Lu 	head.next = NULL;
13628424d759SYinghai Lu 	add_list.next = NULL;
136332180e40SYinghai Lu 
136432180e40SYinghai Lu again:
13658424d759SYinghai Lu 	__pci_bus_size_bridges(parent, &add_list);
13668424d759SYinghai Lu 	__pci_bridge_assign_resources(bridge, &add_list, &head);
13678424d759SYinghai Lu 	BUG_ON(add_list.next);
136832180e40SYinghai Lu 	tried_times++;
136932180e40SYinghai Lu 
137032180e40SYinghai Lu 	if (!head.next)
13713f579c34SYinghai Lu 		goto enable_all;
137232180e40SYinghai Lu 
137332180e40SYinghai Lu 	if (tried_times >= 2) {
137432180e40SYinghai Lu 		/* still fail, don't need to try more */
1375094732a5SRam Pai 		free_list(resource_list_x, &head);
13763f579c34SYinghai Lu 		goto enable_all;
137732180e40SYinghai Lu 	}
137832180e40SYinghai Lu 
137932180e40SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
138032180e40SYinghai Lu 			 tried_times + 1);
138132180e40SYinghai Lu 
138232180e40SYinghai Lu 	/*
138332180e40SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
138432180e40SYinghai Lu 	 * child device under that bridge
138532180e40SYinghai Lu 	 */
138632180e40SYinghai Lu 	for (list = head.next; list;) {
138732180e40SYinghai Lu 		struct pci_bus *bus = list->dev->bus;
138832180e40SYinghai Lu 		unsigned long flags = list->flags;
138932180e40SYinghai Lu 
139032180e40SYinghai Lu 		pci_bus_release_bridge_resources(bus, flags & type_mask,
139132180e40SYinghai Lu 						 whole_subtree);
139232180e40SYinghai Lu 		list = list->next;
139332180e40SYinghai Lu 	}
139432180e40SYinghai Lu 	/* restore size and flags */
139532180e40SYinghai Lu 	for (list = head.next; list;) {
139632180e40SYinghai Lu 		struct resource *res = list->res;
139732180e40SYinghai Lu 
139832180e40SYinghai Lu 		res->start = list->start;
139932180e40SYinghai Lu 		res->end = list->end;
140032180e40SYinghai Lu 		res->flags = list->flags;
140132180e40SYinghai Lu 		if (list->dev->subordinate)
140232180e40SYinghai Lu 			res->flags = 0;
140332180e40SYinghai Lu 
140432180e40SYinghai Lu 		list = list->next;
140532180e40SYinghai Lu 	}
1406094732a5SRam Pai 	free_list(resource_list_x, &head);
140732180e40SYinghai Lu 
140832180e40SYinghai Lu 	goto again;
14093f579c34SYinghai Lu 
14103f579c34SYinghai Lu enable_all:
14113f579c34SYinghai Lu 	retval = pci_reenable_device(bridge);
14123f579c34SYinghai Lu 	pci_set_master(bridge);
14133f579c34SYinghai Lu 	pci_enable_bridges(parent);
14146841ec68SYinghai Lu }
14156841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
14169b03088fSYinghai Lu 
14179b03088fSYinghai Lu #ifdef CONFIG_HOTPLUG
14189b03088fSYinghai Lu /**
14199b03088fSYinghai Lu  * pci_rescan_bus - scan a PCI bus for devices.
14209b03088fSYinghai Lu  * @bus: PCI bus to scan
14219b03088fSYinghai Lu  *
14229b03088fSYinghai Lu  * Scan a PCI bus and child buses for new devices, adds them,
14239b03088fSYinghai Lu  * and enables them.
14249b03088fSYinghai Lu  *
14259b03088fSYinghai Lu  * Returns the max number of subordinate bus discovered.
14269b03088fSYinghai Lu  */
14279b03088fSYinghai Lu unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
14289b03088fSYinghai Lu {
14299b03088fSYinghai Lu 	unsigned int max;
14309b03088fSYinghai Lu 	struct pci_dev *dev;
14319b03088fSYinghai Lu 	struct resource_list_x add_list; /* list of resources that
14329b03088fSYinghai Lu 					want additional resources */
14339b03088fSYinghai Lu 
14349b03088fSYinghai Lu 	max = pci_scan_child_bus(bus);
14359b03088fSYinghai Lu 
14369b03088fSYinghai Lu 	add_list.next = NULL;
14379b03088fSYinghai Lu 	down_read(&pci_bus_sem);
14389b03088fSYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
14399b03088fSYinghai Lu 		if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
14409b03088fSYinghai Lu 		    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
14419b03088fSYinghai Lu 			if (dev->subordinate)
14429b03088fSYinghai Lu 				__pci_bus_size_bridges(dev->subordinate,
14439b03088fSYinghai Lu 							 &add_list);
14449b03088fSYinghai Lu 	up_read(&pci_bus_sem);
14459b03088fSYinghai Lu 	__pci_bus_assign_resources(bus, &add_list, NULL);
14469b03088fSYinghai Lu 	BUG_ON(add_list.next);
14479b03088fSYinghai Lu 
14489b03088fSYinghai Lu 	pci_enable_bridges(bus);
14499b03088fSYinghai Lu 	pci_bus_add_devices(bus);
14509b03088fSYinghai Lu 
14519b03088fSYinghai Lu 	return max;
14529b03088fSYinghai Lu }
14539b03088fSYinghai Lu EXPORT_SYMBOL_GPL(pci_rescan_bus);
14549b03088fSYinghai Lu #endif
1455