xref: /openbmc/linux/drivers/pci/setup-bus.c (revision b9b0bba96cf5acbf025f7829fbf6c09e74323b41)
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 
41094732a5SRam Pai #define free_list(type, head) do {				\
42bdc4abecSYinghai Lu 	struct type *dev_res, *tmp;				\
43bdc4abecSYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {	\
44bdc4abecSYinghai Lu 		list_del(&dev_res->list);			\
45bdc4abecSYinghai Lu 		kfree(dev_res);					\
46094732a5SRam Pai 	}							\
47094732a5SRam Pai } while (0)
48094732a5SRam Pai 
49f483d392SRam Pai int pci_realloc_enable = 0;
50f483d392SRam Pai #define pci_realloc_enabled() pci_realloc_enable
51f483d392SRam Pai void pci_realloc(void)
52f483d392SRam Pai {
53f483d392SRam Pai 	pci_realloc_enable = 1;
54f483d392SRam Pai }
55f483d392SRam Pai 
56c8adf9a3SRam Pai /**
57c8adf9a3SRam Pai  * add_to_list() - add a new resource tracker to the list
58c8adf9a3SRam Pai  * @head:	Head of the list
59c8adf9a3SRam Pai  * @dev:	device corresponding to which the resource
60c8adf9a3SRam Pai  *		belongs
61c8adf9a3SRam Pai  * @res:	The resource to be tracked
62c8adf9a3SRam Pai  * @add_size:	additional size to be optionally added
63c8adf9a3SRam Pai  *              to the resource
64c8adf9a3SRam Pai  */
65bdc4abecSYinghai Lu static int add_to_list(struct list_head *head,
66c8adf9a3SRam Pai 		 struct pci_dev *dev, struct resource *res,
672bbc6942SRam Pai 		 resource_size_t add_size, resource_size_t min_align)
68568ddef8SYinghai Lu {
69764242a0SYinghai Lu 	struct pci_dev_resource *tmp;
70568ddef8SYinghai Lu 
71bdc4abecSYinghai Lu 	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
72568ddef8SYinghai Lu 	if (!tmp) {
73c8adf9a3SRam Pai 		pr_warning("add_to_list: kmalloc() failed!\n");
74ef62dfefSYinghai Lu 		return -ENOMEM;
75568ddef8SYinghai Lu 	}
76568ddef8SYinghai Lu 
77568ddef8SYinghai Lu 	tmp->res = res;
78568ddef8SYinghai Lu 	tmp->dev = dev;
79568ddef8SYinghai Lu 	tmp->start = res->start;
80568ddef8SYinghai Lu 	tmp->end = res->end;
81568ddef8SYinghai Lu 	tmp->flags = res->flags;
82c8adf9a3SRam Pai 	tmp->add_size = add_size;
832bbc6942SRam Pai 	tmp->min_align = min_align;
84bdc4abecSYinghai Lu 
85bdc4abecSYinghai Lu 	list_add(&tmp->list, head);
86ef62dfefSYinghai Lu 
87ef62dfefSYinghai Lu 	return 0;
88568ddef8SYinghai Lu }
89568ddef8SYinghai Lu 
90bdc4abecSYinghai Lu static void add_to_failed_list(struct list_head *head,
91c8adf9a3SRam Pai 				struct pci_dev *dev, struct resource *res)
92c8adf9a3SRam Pai {
932bbc6942SRam Pai 	add_to_list(head, dev, res,
942bbc6942SRam Pai 			0 /* dont care */,
952bbc6942SRam Pai 			0 /* dont care */);
96c8adf9a3SRam Pai }
97c8adf9a3SRam Pai 
98*b9b0bba9SYinghai Lu static void remove_from_list(struct list_head *head,
993e6e0d80SYinghai Lu 				 struct resource *res)
1003e6e0d80SYinghai Lu {
101*b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
1023e6e0d80SYinghai Lu 
103*b9b0bba9SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
104*b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
105*b9b0bba9SYinghai Lu 			list_del(&dev_res->list);
106*b9b0bba9SYinghai Lu 			kfree(dev_res);
107bdc4abecSYinghai Lu 			break;
1083e6e0d80SYinghai Lu 		}
1093e6e0d80SYinghai Lu 	}
1103e6e0d80SYinghai Lu }
1113e6e0d80SYinghai Lu 
112*b9b0bba9SYinghai Lu static resource_size_t get_res_add_size(struct list_head *head,
1131c372353SYinghai Lu 					struct resource *res)
1141c372353SYinghai Lu {
115*b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res;
1161c372353SYinghai Lu 
117*b9b0bba9SYinghai Lu 	list_for_each_entry(dev_res, head, list) {
118*b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
119*b9b0bba9SYinghai Lu 			dev_printk(KERN_DEBUG, &dev_res->dev->dev,
1203e6e0d80SYinghai Lu 				 "%pR get_res_add_size  add_size %llx\n",
121*b9b0bba9SYinghai Lu 				 dev_res->res,
122*b9b0bba9SYinghai Lu 				 (unsigned long long)dev_res->add_size);
123*b9b0bba9SYinghai Lu 			return dev_res->add_size;
124bdc4abecSYinghai Lu 		}
1253e6e0d80SYinghai Lu 	}
1261c372353SYinghai Lu 
1271c372353SYinghai Lu 	return 0;
1281c372353SYinghai Lu }
1291c372353SYinghai Lu 
13078c3b329SYinghai Lu /* Sort resources by alignment */
131bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
13278c3b329SYinghai Lu {
13378c3b329SYinghai Lu 	int i;
13478c3b329SYinghai Lu 
13578c3b329SYinghai Lu 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
13678c3b329SYinghai Lu 		struct resource *r;
137bdc4abecSYinghai Lu 		struct pci_dev_resource *dev_res, *tmp;
13878c3b329SYinghai Lu 		resource_size_t r_align;
139bdc4abecSYinghai Lu 		struct list_head *n;
14078c3b329SYinghai Lu 
14178c3b329SYinghai Lu 		r = &dev->resource[i];
14278c3b329SYinghai Lu 
14378c3b329SYinghai Lu 		if (r->flags & IORESOURCE_PCI_FIXED)
14478c3b329SYinghai Lu 			continue;
14578c3b329SYinghai Lu 
14678c3b329SYinghai Lu 		if (!(r->flags) || r->parent)
14778c3b329SYinghai Lu 			continue;
14878c3b329SYinghai Lu 
14978c3b329SYinghai Lu 		r_align = pci_resource_alignment(dev, r);
15078c3b329SYinghai Lu 		if (!r_align) {
15178c3b329SYinghai Lu 			dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
15278c3b329SYinghai Lu 				 i, r);
15378c3b329SYinghai Lu 			continue;
15478c3b329SYinghai Lu 		}
15578c3b329SYinghai Lu 
156bdc4abecSYinghai Lu 		tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
15778c3b329SYinghai Lu 		if (!tmp)
15878c3b329SYinghai Lu 			panic("pdev_sort_resources(): "
15978c3b329SYinghai Lu 			      "kmalloc() failed!\n");
16078c3b329SYinghai Lu 		tmp->res = r;
16178c3b329SYinghai Lu 		tmp->dev = dev;
162bdc4abecSYinghai Lu 
163bdc4abecSYinghai Lu 		/* fallback is smallest one or list is empty*/
164bdc4abecSYinghai Lu 		n = head;
165bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
166bdc4abecSYinghai Lu 			resource_size_t align;
167bdc4abecSYinghai Lu 
168bdc4abecSYinghai Lu 			align = pci_resource_alignment(dev_res->dev,
169bdc4abecSYinghai Lu 							 dev_res->res);
170bdc4abecSYinghai Lu 
171bdc4abecSYinghai Lu 			if (r_align > align) {
172bdc4abecSYinghai Lu 				n = &dev_res->list;
17378c3b329SYinghai Lu 				break;
17478c3b329SYinghai Lu 			}
17578c3b329SYinghai Lu 		}
176bdc4abecSYinghai Lu 		/* Insert it just before n*/
177bdc4abecSYinghai Lu 		list_add_tail(&tmp->list, n);
17878c3b329SYinghai Lu 	}
17978c3b329SYinghai Lu }
18078c3b329SYinghai Lu 
1816841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev,
182bdc4abecSYinghai Lu 				 struct list_head *head)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	u16 class = dev->class >> 8;
1851da177e4SLinus Torvalds 
1869bded00bSKenji Kaneshige 	/* Don't touch classless devices or host bridges or ioapics.  */
1876841ec68SYinghai Lu 	if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
1886841ec68SYinghai Lu 		return;
1891da177e4SLinus Torvalds 
1909bded00bSKenji Kaneshige 	/* Don't touch ioapic devices already enabled by firmware */
19123186279SSatoru Takeuchi 	if (class == PCI_CLASS_SYSTEM_PIC) {
1929bded00bSKenji Kaneshige 		u16 command;
1939bded00bSKenji Kaneshige 		pci_read_config_word(dev, PCI_COMMAND, &command);
1949bded00bSKenji Kaneshige 		if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
1956841ec68SYinghai Lu 			return;
19623186279SSatoru Takeuchi 	}
19723186279SSatoru Takeuchi 
1986841ec68SYinghai Lu 	pdev_sort_resources(dev, head);
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
201fc075e1dSRam Pai static inline void reset_resource(struct resource *res)
202fc075e1dSRam Pai {
203fc075e1dSRam Pai 	res->start = 0;
204fc075e1dSRam Pai 	res->end = 0;
205fc075e1dSRam Pai 	res->flags = 0;
206fc075e1dSRam Pai }
207fc075e1dSRam Pai 
208c8adf9a3SRam Pai /**
2099e8bf93aSRam Pai  * reassign_resources_sorted() - satisfy any additional resource requests
210c8adf9a3SRam Pai  *
2119e8bf93aSRam Pai  * @realloc_head : head of the list tracking requests requiring additional
212c8adf9a3SRam Pai  *             resources
213c8adf9a3SRam Pai  * @head     : head of the list tracking requests with allocated
214c8adf9a3SRam Pai  *             resources
215c8adf9a3SRam Pai  *
2169e8bf93aSRam Pai  * Walk through each element of the realloc_head and try to procure
217c8adf9a3SRam Pai  * additional resources for the element, provided the element
218c8adf9a3SRam Pai  * is in the head list.
219c8adf9a3SRam Pai  */
220bdc4abecSYinghai Lu static void reassign_resources_sorted(struct list_head *realloc_head,
221bdc4abecSYinghai Lu 		struct list_head *head)
222c8adf9a3SRam Pai {
223c8adf9a3SRam Pai 	struct resource *res;
224*b9b0bba9SYinghai Lu 	struct pci_dev_resource *add_res, *tmp;
225bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
226c8adf9a3SRam Pai 	resource_size_t add_size;
227c8adf9a3SRam Pai 	int idx;
228c8adf9a3SRam Pai 
229*b9b0bba9SYinghai Lu 	list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
230bdc4abecSYinghai Lu 		bool found_match = false;
231bdc4abecSYinghai Lu 
232*b9b0bba9SYinghai Lu 		res = add_res->res;
233c8adf9a3SRam Pai 		/* skip resource that has been reset */
234c8adf9a3SRam Pai 		if (!res->flags)
235c8adf9a3SRam Pai 			goto out;
236c8adf9a3SRam Pai 
237c8adf9a3SRam Pai 		/* skip this resource if not found in head list */
238bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
239bdc4abecSYinghai Lu 			if (dev_res->res == res) {
240bdc4abecSYinghai Lu 				found_match = true;
241bdc4abecSYinghai Lu 				break;
242c8adf9a3SRam Pai 			}
243bdc4abecSYinghai Lu 		}
244bdc4abecSYinghai Lu 		if (!found_match)/* just skip */
245bdc4abecSYinghai Lu 			continue;
246c8adf9a3SRam Pai 
247*b9b0bba9SYinghai Lu 		idx = res - &add_res->dev->resource[0];
248*b9b0bba9SYinghai Lu 		add_size = add_res->add_size;
2492bbc6942SRam Pai 		if (!resource_size(res)) {
250*b9b0bba9SYinghai Lu 			res->start = add_res->start;
251c8adf9a3SRam Pai 			res->end = res->start + add_size - 1;
252*b9b0bba9SYinghai Lu 			if (pci_assign_resource(add_res->dev, idx))
253c8adf9a3SRam Pai 				reset_resource(res);
2542bbc6942SRam Pai 		} else {
255*b9b0bba9SYinghai Lu 			resource_size_t align = add_res->min_align;
256*b9b0bba9SYinghai Lu 			res->flags |= add_res->flags &
257bdc4abecSYinghai Lu 				 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
258*b9b0bba9SYinghai Lu 			if (pci_reassign_resource(add_res->dev, idx,
259bdc4abecSYinghai Lu 						  add_size, align))
260*b9b0bba9SYinghai Lu 				dev_printk(KERN_DEBUG, &add_res->dev->dev,
261bdc4abecSYinghai Lu 				   "failed to add optional resources res=%pR\n",
2622bbc6942SRam Pai 							res);
263c8adf9a3SRam Pai 		}
264c8adf9a3SRam Pai out:
265*b9b0bba9SYinghai Lu 		list_del(&add_res->list);
266*b9b0bba9SYinghai Lu 		kfree(add_res);
267c8adf9a3SRam Pai 	}
268c8adf9a3SRam Pai }
269c8adf9a3SRam Pai 
270c8adf9a3SRam Pai /**
271c8adf9a3SRam Pai  * assign_requested_resources_sorted() - satisfy resource requests
272c8adf9a3SRam Pai  *
273c8adf9a3SRam Pai  * @head : head of the list tracking requests for resources
274c8adf9a3SRam Pai  * @failed_list : head of the list tracking requests that could
275c8adf9a3SRam Pai  *		not be allocated
276c8adf9a3SRam Pai  *
277c8adf9a3SRam Pai  * Satisfy resource requests of each element in the list. Add
278c8adf9a3SRam Pai  * requests that could not satisfied to the failed_list.
279c8adf9a3SRam Pai  */
280bdc4abecSYinghai Lu static void assign_requested_resources_sorted(struct list_head *head,
281bdc4abecSYinghai Lu 				 struct list_head *fail_head)
2826841ec68SYinghai Lu {
2836841ec68SYinghai Lu 	struct resource *res;
284bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
2856841ec68SYinghai Lu 	int idx;
2866841ec68SYinghai Lu 
287bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
288bdc4abecSYinghai Lu 		res = dev_res->res;
289bdc4abecSYinghai Lu 		idx = res - &dev_res->dev->resource[0];
290bdc4abecSYinghai Lu 		if (resource_size(res) &&
291bdc4abecSYinghai Lu 		    pci_assign_resource(dev_res->dev, idx)) {
292bdc4abecSYinghai Lu 			if (fail_head && !pci_is_root_bus(dev_res->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))))
299bdc4abecSYinghai Lu 					add_to_failed_list(fail_head,
300bdc4abecSYinghai Lu 							   dev_res->dev, res);
3019a928660SYinghai Lu 			}
302fc075e1dSRam Pai 			reset_resource(res);
303542df5deSRajesh Shah 		}
3041da177e4SLinus Torvalds 	}
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
307bdc4abecSYinghai Lu static void __assign_resources_sorted(struct list_head *head,
308bdc4abecSYinghai Lu 				 struct list_head *realloc_head,
309bdc4abecSYinghai Lu 				 struct list_head *fail_head)
310c8adf9a3SRam Pai {
3113e6e0d80SYinghai Lu 	/*
3123e6e0d80SYinghai Lu 	 * Should not assign requested resources at first.
3133e6e0d80SYinghai Lu 	 *   they could be adjacent, so later reassign can not reallocate
3143e6e0d80SYinghai Lu 	 *   them one by one in parent resource window.
3153e6e0d80SYinghai Lu 	 * Try to assign requested + add_size at begining
3163e6e0d80SYinghai Lu 	 *  if could do that, could get out early.
3173e6e0d80SYinghai Lu 	 *  if could not do that, we still try to assign requested at first,
3183e6e0d80SYinghai Lu 	 *    then try to reassign add_size for some resources.
3193e6e0d80SYinghai Lu 	 */
320bdc4abecSYinghai Lu 	LIST_HEAD(save_head);
321bdc4abecSYinghai Lu 	LIST_HEAD(local_fail_head);
322*b9b0bba9SYinghai Lu 	struct pci_dev_resource *save_res;
323bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
3243e6e0d80SYinghai Lu 
3253e6e0d80SYinghai Lu 	/* Check if optional add_size is there */
326bdc4abecSYinghai Lu 	if (!realloc_head || list_empty(realloc_head))
3273e6e0d80SYinghai Lu 		goto requested_and_reassign;
3283e6e0d80SYinghai Lu 
3293e6e0d80SYinghai Lu 	/* Save original start, end, flags etc at first */
330bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
331bdc4abecSYinghai Lu 		if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
332764242a0SYinghai Lu 			free_list(pci_dev_resource, &save_head);
3333e6e0d80SYinghai Lu 			goto requested_and_reassign;
3343e6e0d80SYinghai Lu 		}
335bdc4abecSYinghai Lu 	}
3363e6e0d80SYinghai Lu 
3373e6e0d80SYinghai Lu 	/* Update res in head list with add_size in realloc_head list */
338bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list)
339bdc4abecSYinghai Lu 		dev_res->res->end += get_res_add_size(realloc_head,
340bdc4abecSYinghai Lu 							dev_res->res);
3413e6e0d80SYinghai Lu 
3423e6e0d80SYinghai Lu 	/* Try updated head list with add_size added */
3433e6e0d80SYinghai Lu 	assign_requested_resources_sorted(head, &local_fail_head);
3443e6e0d80SYinghai Lu 
3453e6e0d80SYinghai Lu 	/* all assigned with add_size ? */
346bdc4abecSYinghai Lu 	if (list_empty(&local_fail_head)) {
3473e6e0d80SYinghai Lu 		/* Remove head list from realloc_head list */
348bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list)
349bdc4abecSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
350764242a0SYinghai Lu 		free_list(pci_dev_resource, &save_head);
351bdc4abecSYinghai Lu 		free_list(pci_dev_resource, head);
3523e6e0d80SYinghai Lu 		return;
3533e6e0d80SYinghai Lu 	}
3543e6e0d80SYinghai Lu 
355764242a0SYinghai Lu 	free_list(pci_dev_resource, &local_fail_head);
3563e6e0d80SYinghai Lu 	/* Release assigned resource */
357bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list)
358bdc4abecSYinghai Lu 		if (dev_res->res->parent)
359bdc4abecSYinghai Lu 			release_resource(dev_res->res);
3603e6e0d80SYinghai Lu 	/* Restore start/end/flags from saved list */
361*b9b0bba9SYinghai Lu 	list_for_each_entry(save_res, &save_head, list) {
362*b9b0bba9SYinghai Lu 		struct resource *res = save_res->res;
3633e6e0d80SYinghai Lu 
364*b9b0bba9SYinghai Lu 		res->start = save_res->start;
365*b9b0bba9SYinghai Lu 		res->end = save_res->end;
366*b9b0bba9SYinghai Lu 		res->flags = save_res->flags;
3673e6e0d80SYinghai Lu 	}
368764242a0SYinghai Lu 	free_list(pci_dev_resource, &save_head);
3693e6e0d80SYinghai Lu 
3703e6e0d80SYinghai Lu requested_and_reassign:
371c8adf9a3SRam Pai 	/* Satisfy the must-have resource requests */
372c8adf9a3SRam Pai 	assign_requested_resources_sorted(head, fail_head);
373c8adf9a3SRam Pai 
3740a2daa1cSRam Pai 	/* Try to satisfy any additional optional resource
375c8adf9a3SRam Pai 		requests */
3769e8bf93aSRam Pai 	if (realloc_head)
3779e8bf93aSRam Pai 		reassign_resources_sorted(realloc_head, head);
378bdc4abecSYinghai Lu 	free_list(pci_dev_resource, head);
379c8adf9a3SRam Pai }
380c8adf9a3SRam Pai 
3816841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev,
382bdc4abecSYinghai Lu 				 struct list_head *add_head,
383bdc4abecSYinghai Lu 				 struct list_head *fail_head)
3846841ec68SYinghai Lu {
385bdc4abecSYinghai Lu 	LIST_HEAD(head);
3866841ec68SYinghai Lu 
3876841ec68SYinghai Lu 	__dev_sort_resources(dev, &head);
3888424d759SYinghai Lu 	__assign_resources_sorted(&head, add_head, fail_head);
3896841ec68SYinghai Lu 
3906841ec68SYinghai Lu }
3916841ec68SYinghai Lu 
3926841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus,
393bdc4abecSYinghai Lu 					 struct list_head *realloc_head,
394bdc4abecSYinghai Lu 					 struct list_head *fail_head)
3956841ec68SYinghai Lu {
3966841ec68SYinghai Lu 	struct pci_dev *dev;
397bdc4abecSYinghai Lu 	LIST_HEAD(head);
3986841ec68SYinghai Lu 
3996841ec68SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
4006841ec68SYinghai Lu 		__dev_sort_resources(dev, &head);
4016841ec68SYinghai Lu 
4029e8bf93aSRam Pai 	__assign_resources_sorted(&head, realloc_head, fail_head);
4036841ec68SYinghai Lu }
4046841ec68SYinghai Lu 
405b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
408c7dabef8SBjorn Helgaas 	struct resource *res;
4091da177e4SLinus Torvalds 	struct pci_bus_region region;
4101da177e4SLinus Torvalds 
411865df576SBjorn Helgaas 	dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
412865df576SBjorn Helgaas 		 bus->secondary, bus->subordinate);
4131da177e4SLinus Torvalds 
414c7dabef8SBjorn Helgaas 	res = bus->resource[0];
415c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
416c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
4171da177e4SLinus Torvalds 		/*
4181da177e4SLinus Torvalds 		 * The IO resource is allocated a range twice as large as it
4191da177e4SLinus Torvalds 		 * would normally need.  This allows us to set both IO regs.
4201da177e4SLinus Torvalds 		 */
421c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4221da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
4231da177e4SLinus Torvalds 					region.start);
4241da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
4251da177e4SLinus Torvalds 					region.end);
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 
428c7dabef8SBjorn Helgaas 	res = bus->resource[1];
429c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
430c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
431c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4321da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
4331da177e4SLinus Torvalds 					region.start);
4341da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
4351da177e4SLinus Torvalds 					region.end);
4361da177e4SLinus Torvalds 	}
4371da177e4SLinus Torvalds 
438c7dabef8SBjorn Helgaas 	res = bus->resource[2];
439c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
440c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
441c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4421da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
4431da177e4SLinus Torvalds 					region.start);
4441da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
4451da177e4SLinus Torvalds 					region.end);
4461da177e4SLinus Torvalds 	}
4471da177e4SLinus Torvalds 
448c7dabef8SBjorn Helgaas 	res = bus->resource[3];
449c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
450c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
451c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4521da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
4531da177e4SLinus Torvalds 					region.start);
4541da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
4551da177e4SLinus Torvalds 					region.end);
4561da177e4SLinus Torvalds 	}
4571da177e4SLinus Torvalds }
458b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus);
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected.
4611da177e4SLinus Torvalds    PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
4621da177e4SLinus Torvalds    requires that if there is no I/O ports or memory behind the
4631da177e4SLinus Torvalds    bridge, corresponding range must be turned off by writing base
4641da177e4SLinus Torvalds    value greater than limit to the bridge's base/limit registers.
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds    Note: care must be taken when updating I/O base/limit registers
4671da177e4SLinus Torvalds    of bridges which support 32-bit I/O. This update requires two
4681da177e4SLinus Torvalds    config space writes, so it's quite possible that an I/O window of
4691da177e4SLinus Torvalds    the bridge will have some undesirable address (e.g. 0) after the
4701da177e4SLinus Torvalds    first write. Ditto 64-bit prefetchable MMIO.  */
4717cc5997dSYinghai Lu static void pci_setup_bridge_io(struct pci_bus *bus)
4721da177e4SLinus Torvalds {
4731da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
474c7dabef8SBjorn Helgaas 	struct resource *res;
4751da177e4SLinus Torvalds 	struct pci_bus_region region;
4767cc5997dSYinghai Lu 	u32 l, io_upper16;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	/* Set up the top and bottom of the PCI I/O segment for this bus. */
479c7dabef8SBjorn Helgaas 	res = bus->resource[0];
480c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
481c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
4821da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_IO_BASE, &l);
4831da177e4SLinus Torvalds 		l &= 0xffff0000;
4841da177e4SLinus Torvalds 		l |= (region.start >> 8) & 0x00f0;
4851da177e4SLinus Torvalds 		l |= region.end & 0xf000;
4861da177e4SLinus Torvalds 		/* Set up upper 16 bits of I/O base/limit. */
4871da177e4SLinus Torvalds 		io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
488c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4897cc5997dSYinghai Lu 	} else {
4901da177e4SLinus Torvalds 		/* Clear upper 16 bits of I/O base/limit. */
4911da177e4SLinus Torvalds 		io_upper16 = 0;
4921da177e4SLinus Torvalds 		l = 0x00f0;
4931da177e4SLinus Torvalds 	}
4941da177e4SLinus Torvalds 	/* Temporarily disable the I/O range before updating PCI_IO_BASE. */
4951da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
4961da177e4SLinus Torvalds 	/* Update lower 16 bits of I/O base/limit. */
4971da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE, l);
4981da177e4SLinus Torvalds 	/* Update upper 16 bits of I/O base/limit. */
4991da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
5007cc5997dSYinghai Lu }
5011da177e4SLinus Torvalds 
5027cc5997dSYinghai Lu static void pci_setup_bridge_mmio(struct pci_bus *bus)
5037cc5997dSYinghai Lu {
5047cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
5057cc5997dSYinghai Lu 	struct resource *res;
5067cc5997dSYinghai Lu 	struct pci_bus_region region;
5077cc5997dSYinghai Lu 	u32 l;
5087cc5997dSYinghai Lu 
5097cc5997dSYinghai Lu 	/* Set up the top and bottom of the PCI Memory segment for this bus. */
510c7dabef8SBjorn Helgaas 	res = bus->resource[1];
511c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
512c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
5131da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
5141da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
515c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5167cc5997dSYinghai Lu 	} else {
5171da177e4SLinus Torvalds 		l = 0x0000fff0;
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
5207cc5997dSYinghai Lu }
5217cc5997dSYinghai Lu 
5227cc5997dSYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
5237cc5997dSYinghai Lu {
5247cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
5257cc5997dSYinghai Lu 	struct resource *res;
5267cc5997dSYinghai Lu 	struct pci_bus_region region;
5277cc5997dSYinghai Lu 	u32 l, bu, lu;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	/* Clear out the upper 32 bits of PREF limit.
5301da177e4SLinus Torvalds 	   If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
5311da177e4SLinus Torvalds 	   disables PREF range, which is ok. */
5321da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	/* Set up PREF base/limit. */
535c40a22e0SBenjamin Herrenschmidt 	bu = lu = 0;
536c7dabef8SBjorn Helgaas 	res = bus->resource[2];
537c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
538c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_PREFETCH) {
5391da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
5401da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
541c7dabef8SBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64) {
54213d36c24SAndrew Morton 			bu = upper_32_bits(region.start);
54313d36c24SAndrew Morton 			lu = upper_32_bits(region.end);
5441f82de10SYinghai Lu 		}
545c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5467cc5997dSYinghai Lu 	} else {
5471da177e4SLinus Torvalds 		l = 0x0000fff0;
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
5501da177e4SLinus Torvalds 
551c40a22e0SBenjamin Herrenschmidt 	/* Set the upper 32 bits of PREF base & limit. */
552c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
553c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
5547cc5997dSYinghai Lu }
5557cc5997dSYinghai Lu 
5567cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
5577cc5997dSYinghai Lu {
5587cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
5597cc5997dSYinghai Lu 
5607cc5997dSYinghai Lu 	dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
5617cc5997dSYinghai Lu 		 bus->secondary, bus->subordinate);
5627cc5997dSYinghai Lu 
5637cc5997dSYinghai Lu 	if (type & IORESOURCE_IO)
5647cc5997dSYinghai Lu 		pci_setup_bridge_io(bus);
5657cc5997dSYinghai Lu 
5667cc5997dSYinghai Lu 	if (type & IORESOURCE_MEM)
5677cc5997dSYinghai Lu 		pci_setup_bridge_mmio(bus);
5687cc5997dSYinghai Lu 
5697cc5997dSYinghai Lu 	if (type & IORESOURCE_PREFETCH)
5707cc5997dSYinghai Lu 		pci_setup_bridge_mmio_pref(bus);
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
5731da177e4SLinus Torvalds }
5741da177e4SLinus Torvalds 
575e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus)
5767cc5997dSYinghai Lu {
5777cc5997dSYinghai Lu 	unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
5787cc5997dSYinghai Lu 				  IORESOURCE_PREFETCH;
5797cc5997dSYinghai Lu 
5807cc5997dSYinghai Lu 	__pci_setup_bridge(bus, type);
5817cc5997dSYinghai Lu }
5827cc5997dSYinghai Lu 
5831da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and
5841da177e4SLinus Torvalds    prefetchable memory ranges. If not, the respective
5851da177e4SLinus Torvalds    base/limit registers must be read-only and read as 0. */
58696bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus)
5871da177e4SLinus Torvalds {
5881da177e4SLinus Torvalds 	u16 io;
5891da177e4SLinus Torvalds 	u32 pmem;
5901da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
5911da177e4SLinus Torvalds 	struct resource *b_res;
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
5941da177e4SLinus Torvalds 	b_res[1].flags |= IORESOURCE_MEM;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_IO_BASE, &io);
5971da177e4SLinus Torvalds 	if (!io) {
5981da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
5991da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_IO_BASE, &io);
6001da177e4SLinus Torvalds  		pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
6011da177e4SLinus Torvalds  	}
6021da177e4SLinus Torvalds  	if (io)
6031da177e4SLinus Torvalds 		b_res[0].flags |= IORESOURCE_IO;
6041da177e4SLinus Torvalds 	/*  DECchip 21050 pass 2 errata: the bridge may miss an address
6051da177e4SLinus Torvalds 	    disconnect boundary by one PCI data phase.
6061da177e4SLinus Torvalds 	    Workaround: do not use prefetching on this device. */
6071da177e4SLinus Torvalds 	if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
6081da177e4SLinus Torvalds 		return;
6091da177e4SLinus Torvalds 	pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
6101da177e4SLinus Torvalds 	if (!pmem) {
6111da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
6121da177e4SLinus Torvalds 					       0xfff0fff0);
6131da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
6141da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
6151da177e4SLinus Torvalds 	}
6161f82de10SYinghai Lu 	if (pmem) {
6171da177e4SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
61899586105SYinghai Lu 		if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
61999586105SYinghai Lu 		    PCI_PREF_RANGE_TYPE_64) {
6201f82de10SYinghai Lu 			b_res[2].flags |= IORESOURCE_MEM_64;
62199586105SYinghai Lu 			b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
62299586105SYinghai Lu 		}
6231f82de10SYinghai Lu 	}
6241f82de10SYinghai Lu 
6251f82de10SYinghai Lu 	/* double check if bridge does support 64 bit pref */
6261f82de10SYinghai Lu 	if (b_res[2].flags & IORESOURCE_MEM_64) {
6271f82de10SYinghai Lu 		u32 mem_base_hi, tmp;
6281f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
6291f82de10SYinghai Lu 					 &mem_base_hi);
6301f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
6311f82de10SYinghai Lu 					       0xffffffff);
6321f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
6331f82de10SYinghai Lu 		if (!tmp)
6341f82de10SYinghai Lu 			b_res[2].flags &= ~IORESOURCE_MEM_64;
6351f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
6361f82de10SYinghai Lu 				       mem_base_hi);
6371f82de10SYinghai Lu 	}
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds /* Helper function for sizing routines: find first available
6411da177e4SLinus Torvalds    bus resource of a given type. Note: we intentionally skip
6421da177e4SLinus Torvalds    the bus resources which have already been assigned (that is,
6431da177e4SLinus Torvalds    have non-NULL parent resource). */
64496bde06aSSam Ravnborg static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
6451da177e4SLinus Torvalds {
6461da177e4SLinus Torvalds 	int i;
6471da177e4SLinus Torvalds 	struct resource *r;
6481da177e4SLinus Torvalds 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
6491da177e4SLinus Torvalds 				  IORESOURCE_PREFETCH;
6501da177e4SLinus Torvalds 
65189a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, r, i) {
652299de034SIvan Kokshaysky 		if (r == &ioport_resource || r == &iomem_resource)
653299de034SIvan Kokshaysky 			continue;
65455a10984SJesse Barnes 		if (r && (r->flags & type_mask) == type && !r->parent)
6551da177e4SLinus Torvalds 			return r;
6561da177e4SLinus Torvalds 	}
6571da177e4SLinus Torvalds 	return NULL;
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
66013583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size,
66113583b16SRam Pai 		resource_size_t min_size,
66213583b16SRam Pai 		resource_size_t size1,
66313583b16SRam Pai 		resource_size_t old_size,
66413583b16SRam Pai 		resource_size_t align)
66513583b16SRam Pai {
66613583b16SRam Pai 	if (size < min_size)
66713583b16SRam Pai 		size = min_size;
66813583b16SRam Pai 	if (old_size == 1 )
66913583b16SRam Pai 		old_size = 0;
67013583b16SRam Pai 	/* To be fixed in 2.5: we should have sort of HAVE_ISA
67113583b16SRam Pai 	   flag in the struct pci_bus. */
67213583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
67313583b16SRam Pai 	size = (size & 0xff) + ((size & ~0xffUL) << 2);
67413583b16SRam Pai #endif
67513583b16SRam Pai 	size = ALIGN(size + size1, align);
67613583b16SRam Pai 	if (size < old_size)
67713583b16SRam Pai 		size = old_size;
67813583b16SRam Pai 	return size;
67913583b16SRam Pai }
68013583b16SRam Pai 
68113583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size,
68213583b16SRam Pai 		resource_size_t min_size,
68313583b16SRam Pai 		resource_size_t size1,
68413583b16SRam Pai 		resource_size_t old_size,
68513583b16SRam Pai 		resource_size_t align)
68613583b16SRam Pai {
68713583b16SRam Pai 	if (size < min_size)
68813583b16SRam Pai 		size = min_size;
68913583b16SRam Pai 	if (old_size == 1 )
69013583b16SRam Pai 		old_size = 0;
69113583b16SRam Pai 	if (size < old_size)
69213583b16SRam Pai 		size = old_size;
69313583b16SRam Pai 	size = ALIGN(size + size1, align);
69413583b16SRam Pai 	return size;
69513583b16SRam Pai }
69613583b16SRam Pai 
697c8adf9a3SRam Pai /**
698c8adf9a3SRam Pai  * pbus_size_io() - size the io window of a given bus
699c8adf9a3SRam Pai  *
700c8adf9a3SRam Pai  * @bus : the bus
701c8adf9a3SRam Pai  * @min_size : the minimum io window that must to be allocated
702c8adf9a3SRam Pai  * @add_size : additional optional io window
7039e8bf93aSRam Pai  * @realloc_head : track the additional io window on this list
704c8adf9a3SRam Pai  *
705c8adf9a3SRam Pai  * Sizing the IO windows of the PCI-PCI bridge is trivial,
706c8adf9a3SRam Pai  * since these windows have 4K granularity and the IO ranges
707c8adf9a3SRam Pai  * of non-bridge PCI devices are limited to 256 bytes.
708c8adf9a3SRam Pai  * We must be careful with the ISA aliasing though.
709c8adf9a3SRam Pai  */
710c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
711bdc4abecSYinghai Lu 		resource_size_t add_size, struct list_head *realloc_head)
7121da177e4SLinus Torvalds {
7131da177e4SLinus Torvalds 	struct pci_dev *dev;
7141da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
715c8adf9a3SRam Pai 	unsigned long size = 0, size0 = 0, size1 = 0;
716be768912SYinghai Lu 	resource_size_t children_add_size = 0;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	if (!b_res)
7191da177e4SLinus Torvalds  		return;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
7221da177e4SLinus Torvalds 		int i;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
7251da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
7261da177e4SLinus Torvalds 			unsigned long r_size;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 			if (r->parent || !(r->flags & IORESOURCE_IO))
7291da177e4SLinus Torvalds 				continue;
730022edd86SZhao, Yu 			r_size = resource_size(r);
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 			if (r_size < 0x400)
7331da177e4SLinus Torvalds 				/* Might be re-aligned for ISA */
7341da177e4SLinus Torvalds 				size += r_size;
7351da177e4SLinus Torvalds 			else
7361da177e4SLinus Torvalds 				size1 += r_size;
737be768912SYinghai Lu 
7389e8bf93aSRam Pai 			if (realloc_head)
7399e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
7401da177e4SLinus Torvalds 		}
7411da177e4SLinus Torvalds 	}
742c8adf9a3SRam Pai 	size0 = calculate_iosize(size, min_size, size1,
74313583b16SRam Pai 			resource_size(b_res), 4096);
744be768912SYinghai Lu 	if (children_add_size > add_size)
745be768912SYinghai Lu 		add_size = children_add_size;
7469e8bf93aSRam Pai 	size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
747a4ac9feaSYinghai Lu 		calculate_iosize(size, min_size, add_size + size1,
748c8adf9a3SRam Pai 			resource_size(b_res), 4096);
749c8adf9a3SRam Pai 	if (!size0 && !size1) {
750865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
751865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
752865df576SBjorn Helgaas 				 "%pR to [bus %02x-%02x] (unused)\n", b_res,
753865df576SBjorn Helgaas 				 bus->secondary, bus->subordinate);
7541da177e4SLinus Torvalds 		b_res->flags = 0;
7551da177e4SLinus Torvalds 		return;
7561da177e4SLinus Torvalds 	}
7571da177e4SLinus Torvalds 	/* Alignment of the IO window is always 4K */
7581da177e4SLinus Torvalds 	b_res->start = 4096;
759c8adf9a3SRam Pai 	b_res->end = b_res->start + size0 - 1;
76088452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
7619e8bf93aSRam Pai 	if (size1 > size0 && realloc_head)
7629e8bf93aSRam Pai 		add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096);
7631da177e4SLinus Torvalds }
7641da177e4SLinus Torvalds 
765c8adf9a3SRam Pai /**
766c8adf9a3SRam Pai  * pbus_size_mem() - size the memory window of a given bus
767c8adf9a3SRam Pai  *
768c8adf9a3SRam Pai  * @bus : the bus
769c8adf9a3SRam Pai  * @min_size : the minimum memory window that must to be allocated
770c8adf9a3SRam Pai  * @add_size : additional optional memory window
7719e8bf93aSRam Pai  * @realloc_head : track the additional memory window on this list
772c8adf9a3SRam Pai  *
773c8adf9a3SRam Pai  * Calculate the size of the bus and minimal alignment which
774c8adf9a3SRam Pai  * guarantees that all child resources fit in this size.
775c8adf9a3SRam Pai  */
77628760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
777c8adf9a3SRam Pai 			 unsigned long type, resource_size_t min_size,
778c8adf9a3SRam Pai 			resource_size_t add_size,
779bdc4abecSYinghai Lu 			struct list_head *realloc_head)
7801da177e4SLinus Torvalds {
7811da177e4SLinus Torvalds 	struct pci_dev *dev;
782c8adf9a3SRam Pai 	resource_size_t min_align, align, size, size0, size1;
783c40a22e0SBenjamin Herrenschmidt 	resource_size_t aligns[12];	/* Alignments from 1Mb to 2Gb */
7841da177e4SLinus Torvalds 	int order, max_order;
7851da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, type);
7861f82de10SYinghai Lu 	unsigned int mem64_mask = 0;
787be768912SYinghai Lu 	resource_size_t children_add_size = 0;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	if (!b_res)
7901da177e4SLinus Torvalds 		return 0;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
7931da177e4SLinus Torvalds 	max_order = 0;
7941da177e4SLinus Torvalds 	size = 0;
7951da177e4SLinus Torvalds 
7961f82de10SYinghai Lu 	mem64_mask = b_res->flags & IORESOURCE_MEM_64;
7971f82de10SYinghai Lu 	b_res->flags &= ~IORESOURCE_MEM_64;
7981f82de10SYinghai Lu 
7991da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
8001da177e4SLinus Torvalds 		int i;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
8031da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
804c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 			if (r->parent || (r->flags & mask) != type)
8071da177e4SLinus Torvalds 				continue;
808022edd86SZhao, Yu 			r_size = resource_size(r);
8092aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV
8102aceefcbSYinghai Lu 			/* put SRIOV requested res to the optional list */
8119e8bf93aSRam Pai 			if (realloc_head && i >= PCI_IOV_RESOURCES &&
8122aceefcbSYinghai Lu 					i <= PCI_IOV_RESOURCE_END) {
8132aceefcbSYinghai Lu 				r->end = r->start - 1;
8149e8bf93aSRam Pai 				add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */);
8152aceefcbSYinghai Lu 				children_add_size += r_size;
8162aceefcbSYinghai Lu 				continue;
8172aceefcbSYinghai Lu 			}
8182aceefcbSYinghai Lu #endif
8191da177e4SLinus Torvalds 			/* For bridges size != alignment */
8206faf17f6SChris Wright 			align = pci_resource_alignment(dev, r);
8211da177e4SLinus Torvalds 			order = __ffs(align) - 20;
8221da177e4SLinus Torvalds 			if (order > 11) {
823865df576SBjorn Helgaas 				dev_warn(&dev->dev, "disabling BAR %d: %pR "
824865df576SBjorn Helgaas 					 "(bad alignment %#llx)\n", i, r,
825865df576SBjorn Helgaas 					 (unsigned long long) align);
8261da177e4SLinus Torvalds 				r->flags = 0;
8271da177e4SLinus Torvalds 				continue;
8281da177e4SLinus Torvalds 			}
8291da177e4SLinus Torvalds 			size += r_size;
8301da177e4SLinus Torvalds 			if (order < 0)
8311da177e4SLinus Torvalds 				order = 0;
8321da177e4SLinus Torvalds 			/* Exclude ranges with size > align from
8331da177e4SLinus Torvalds 			   calculation of the alignment. */
8341da177e4SLinus Torvalds 			if (r_size == align)
8351da177e4SLinus Torvalds 				aligns[order] += align;
8361da177e4SLinus Torvalds 			if (order > max_order)
8371da177e4SLinus Torvalds 				max_order = order;
8381f82de10SYinghai Lu 			mem64_mask &= r->flags & IORESOURCE_MEM_64;
839be768912SYinghai Lu 
8409e8bf93aSRam Pai 			if (realloc_head)
8419e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
8421da177e4SLinus Torvalds 		}
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds 	align = 0;
8451da177e4SLinus Torvalds 	min_align = 0;
8461da177e4SLinus Torvalds 	for (order = 0; order <= max_order; order++) {
8478308c54dSJeremy Fitzhardinge 		resource_size_t align1 = 1;
8488308c54dSJeremy Fitzhardinge 
8498308c54dSJeremy Fitzhardinge 		align1 <<= (order + 20);
8508308c54dSJeremy Fitzhardinge 
8511da177e4SLinus Torvalds 		if (!align)
8521da177e4SLinus Torvalds 			min_align = align1;
8536f6f8c2fSMilind Arun Choudhary 		else if (ALIGN(align + min_align, min_align) < align1)
8541da177e4SLinus Torvalds 			min_align = align1 >> 1;
8551da177e4SLinus Torvalds 		align += aligns[order];
8561da177e4SLinus Torvalds 	}
857b42282e5SLinus Torvalds 	size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
858be768912SYinghai Lu 	if (children_add_size > add_size)
859be768912SYinghai Lu 		add_size = children_add_size;
8609e8bf93aSRam Pai 	size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
861a4ac9feaSYinghai Lu 		calculate_memsize(size, min_size, add_size,
862b42282e5SLinus Torvalds 				resource_size(b_res), min_align);
863c8adf9a3SRam Pai 	if (!size0 && !size1) {
864865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
865865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
866865df576SBjorn Helgaas 				 "%pR to [bus %02x-%02x] (unused)\n", b_res,
867865df576SBjorn Helgaas 				 bus->secondary, bus->subordinate);
8681da177e4SLinus Torvalds 		b_res->flags = 0;
8691da177e4SLinus Torvalds 		return 1;
8701da177e4SLinus Torvalds 	}
8711da177e4SLinus Torvalds 	b_res->start = min_align;
872c8adf9a3SRam Pai 	b_res->end = size0 + min_align - 1;
873c8adf9a3SRam Pai 	b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
8749e8bf93aSRam Pai 	if (size1 > size0 && realloc_head)
8759e8bf93aSRam Pai 		add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
8761da177e4SLinus Torvalds 	return 1;
8771da177e4SLinus Torvalds }
8781da177e4SLinus Torvalds 
8790a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res)
8800a2daa1cSRam Pai {
8810a2daa1cSRam Pai 	if (res->flags & IORESOURCE_IO)
8820a2daa1cSRam Pai 		return pci_cardbus_io_size;
8830a2daa1cSRam Pai 	if (res->flags & IORESOURCE_MEM)
8840a2daa1cSRam Pai 		return pci_cardbus_mem_size;
8850a2daa1cSRam Pai 	return 0;
8860a2daa1cSRam Pai }
8870a2daa1cSRam Pai 
8880a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus,
889bdc4abecSYinghai Lu 			struct list_head *realloc_head)
8901da177e4SLinus Torvalds {
8911da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
8921da177e4SLinus Torvalds 	struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
8931da177e4SLinus Torvalds 	u16 ctrl;
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	/*
8961da177e4SLinus Torvalds 	 * Reserve some resources for CardBus.  We reserve
8971da177e4SLinus Torvalds 	 * a fixed amount of bus space for CardBus bridges.
8981da177e4SLinus Torvalds 	 */
899934b7024SLinus Torvalds 	b_res[0].start = 0;
900934b7024SLinus Torvalds 	b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
9019e8bf93aSRam Pai 	if (realloc_head)
9029e8bf93aSRam Pai 		add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */);
9031da177e4SLinus Torvalds 
904934b7024SLinus Torvalds 	b_res[1].start = 0;
905934b7024SLinus Torvalds 	b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
9069e8bf93aSRam Pai 	if (realloc_head)
9079e8bf93aSRam Pai 		add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */);
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	/*
9101da177e4SLinus Torvalds 	 * Check whether prefetchable memory is supported
9111da177e4SLinus Torvalds 	 * by this bridge.
9121da177e4SLinus Torvalds 	 */
9131da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
9141da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
9151da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
9161da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
9171da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
9181da177e4SLinus Torvalds 	}
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	/*
9211da177e4SLinus Torvalds 	 * If we have prefetchable memory support, allocate
9221da177e4SLinus Torvalds 	 * two regions.  Otherwise, allocate one region of
9231da177e4SLinus Torvalds 	 * twice the size.
9241da177e4SLinus Torvalds 	 */
9251da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
926934b7024SLinus Torvalds 		b_res[2].start = 0;
927934b7024SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
9289e8bf93aSRam Pai 		if (realloc_head)
9299e8bf93aSRam Pai 			add_to_list(realloc_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */);
9301da177e4SLinus Torvalds 
931934b7024SLinus Torvalds 		b_res[3].start = 0;
932934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
9339e8bf93aSRam Pai 		if (realloc_head)
9349e8bf93aSRam Pai 			add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */);
9351da177e4SLinus Torvalds 	} else {
936934b7024SLinus Torvalds 		b_res[3].start = 0;
937934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
9389e8bf93aSRam Pai 		if (realloc_head)
9399e8bf93aSRam Pai 			add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size * 2, 0 /* dont care */);
9401da177e4SLinus Torvalds 	}
9410a2daa1cSRam Pai 
9420a2daa1cSRam Pai 	/* set the size of the resource to zero, so that the resource does not
9430a2daa1cSRam Pai 	 * get assigned during required-resource allocation cycle but gets assigned
9440a2daa1cSRam Pai 	 * during the optional-resource allocation cycle.
9450a2daa1cSRam Pai  	 */
9460a2daa1cSRam Pai 	b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1;
9470a2daa1cSRam Pai 	b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0;
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
950c8adf9a3SRam Pai void __ref __pci_bus_size_bridges(struct pci_bus *bus,
951bdc4abecSYinghai Lu 			struct list_head *realloc_head)
9521da177e4SLinus Torvalds {
9531da177e4SLinus Torvalds 	struct pci_dev *dev;
9541da177e4SLinus Torvalds 	unsigned long mask, prefmask;
955c8adf9a3SRam Pai 	resource_size_t additional_mem_size = 0, additional_io_size = 0;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
9581da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
9591da177e4SLinus Torvalds 		if (!b)
9601da177e4SLinus Torvalds 			continue;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 		switch (dev->class >> 8) {
9631da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
9649e8bf93aSRam Pai 			pci_bus_size_cardbus(b, realloc_head);
9651da177e4SLinus Torvalds 			break;
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
9681da177e4SLinus Torvalds 		default:
9699e8bf93aSRam Pai 			__pci_bus_size_bridges(b, realloc_head);
9701da177e4SLinus Torvalds 			break;
9711da177e4SLinus Torvalds 		}
9721da177e4SLinus Torvalds 	}
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	/* The root bus? */
9751da177e4SLinus Torvalds 	if (!bus->self)
9761da177e4SLinus Torvalds 		return;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	switch (bus->self->class >> 8) {
9791da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_CARDBUS:
9801da177e4SLinus Torvalds 		/* don't size cardbuses yet. */
9811da177e4SLinus Torvalds 		break;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_PCI:
9841da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
98528760489SEric W. Biederman 		if (bus->self->is_hotplug_bridge) {
986c8adf9a3SRam Pai 			additional_io_size  = pci_hotplug_io_size;
987c8adf9a3SRam Pai 			additional_mem_size = pci_hotplug_mem_size;
98828760489SEric W. Biederman 		}
989c8adf9a3SRam Pai 		/*
990c8adf9a3SRam Pai 		 * Follow thru
991c8adf9a3SRam Pai 		 */
9921da177e4SLinus Torvalds 	default:
99319aa7ee4SYinghai Lu 		pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
99419aa7ee4SYinghai Lu 			     additional_io_size, realloc_head);
9951da177e4SLinus Torvalds 		/* If the bridge supports prefetchable range, size it
9961da177e4SLinus Torvalds 		   separately. If it doesn't, or its prefetchable window
9971da177e4SLinus Torvalds 		   has already been allocated by arch code, try
9981da177e4SLinus Torvalds 		   non-prefetchable range for both types of PCI memory
9991da177e4SLinus Torvalds 		   resources. */
10001da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
10011da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
100219aa7ee4SYinghai Lu 		if (pbus_size_mem(bus, prefmask, prefmask,
100319aa7ee4SYinghai Lu 				  realloc_head ? 0 : additional_mem_size,
100419aa7ee4SYinghai Lu 				  additional_mem_size, realloc_head))
10051da177e4SLinus Torvalds 			mask = prefmask; /* Success, size non-prefetch only. */
100628760489SEric W. Biederman 		else
1007c8adf9a3SRam Pai 			additional_mem_size += additional_mem_size;
100819aa7ee4SYinghai Lu 		pbus_size_mem(bus, mask, IORESOURCE_MEM,
100919aa7ee4SYinghai Lu 				realloc_head ? 0 : additional_mem_size,
101019aa7ee4SYinghai Lu 				additional_mem_size, realloc_head);
10111da177e4SLinus Torvalds 		break;
10121da177e4SLinus Torvalds 	}
10131da177e4SLinus Torvalds }
1014c8adf9a3SRam Pai 
1015c8adf9a3SRam Pai void __ref pci_bus_size_bridges(struct pci_bus *bus)
1016c8adf9a3SRam Pai {
1017c8adf9a3SRam Pai 	__pci_bus_size_bridges(bus, NULL);
1018c8adf9a3SRam Pai }
10191da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
10201da177e4SLinus Torvalds 
1021568ddef8SYinghai Lu static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
1022bdc4abecSYinghai Lu 					 struct list_head *realloc_head,
1023bdc4abecSYinghai Lu 					 struct list_head *fail_head)
10241da177e4SLinus Torvalds {
10251da177e4SLinus Torvalds 	struct pci_bus *b;
10261da177e4SLinus Torvalds 	struct pci_dev *dev;
10271da177e4SLinus Torvalds 
10289e8bf93aSRam Pai 	pbus_assign_resources_sorted(bus, realloc_head, fail_head);
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
10311da177e4SLinus Torvalds 		b = dev->subordinate;
10321da177e4SLinus Torvalds 		if (!b)
10331da177e4SLinus Torvalds 			continue;
10341da177e4SLinus Torvalds 
10359e8bf93aSRam Pai 		__pci_bus_assign_resources(b, realloc_head, fail_head);
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 		switch (dev->class >> 8) {
10381da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
10396841ec68SYinghai Lu 			if (!pci_is_enabled(dev))
10401da177e4SLinus Torvalds 				pci_setup_bridge(b);
10411da177e4SLinus Torvalds 			break;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
10441da177e4SLinus Torvalds 			pci_setup_cardbus(b);
10451da177e4SLinus Torvalds 			break;
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 		default:
104880ccba11SBjorn Helgaas 			dev_info(&dev->dev, "not setting up bridge for bus "
104980ccba11SBjorn Helgaas 				 "%04x:%02x\n", pci_domain_nr(b), b->number);
10501da177e4SLinus Torvalds 			break;
10511da177e4SLinus Torvalds 		}
10521da177e4SLinus Torvalds 	}
10531da177e4SLinus Torvalds }
1054568ddef8SYinghai Lu 
1055568ddef8SYinghai Lu void __ref pci_bus_assign_resources(const struct pci_bus *bus)
1056568ddef8SYinghai Lu {
1057c8adf9a3SRam Pai 	__pci_bus_assign_resources(bus, NULL, NULL);
1058568ddef8SYinghai Lu }
10591da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
10601da177e4SLinus Torvalds 
10616841ec68SYinghai Lu static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
1062bdc4abecSYinghai Lu 					 struct list_head *add_head,
1063bdc4abecSYinghai Lu 					 struct list_head *fail_head)
10646841ec68SYinghai Lu {
10656841ec68SYinghai Lu 	struct pci_bus *b;
10666841ec68SYinghai Lu 
10678424d759SYinghai Lu 	pdev_assign_resources_sorted((struct pci_dev *)bridge,
10688424d759SYinghai Lu 					 add_head, fail_head);
10696841ec68SYinghai Lu 
10706841ec68SYinghai Lu 	b = bridge->subordinate;
10716841ec68SYinghai Lu 	if (!b)
10726841ec68SYinghai Lu 		return;
10736841ec68SYinghai Lu 
10748424d759SYinghai Lu 	__pci_bus_assign_resources(b, add_head, fail_head);
10756841ec68SYinghai Lu 
10766841ec68SYinghai Lu 	switch (bridge->class >> 8) {
10776841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_PCI:
10786841ec68SYinghai Lu 		pci_setup_bridge(b);
10796841ec68SYinghai Lu 		break;
10806841ec68SYinghai Lu 
10816841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_CARDBUS:
10826841ec68SYinghai Lu 		pci_setup_cardbus(b);
10836841ec68SYinghai Lu 		break;
10846841ec68SYinghai Lu 
10856841ec68SYinghai Lu 	default:
10866841ec68SYinghai Lu 		dev_info(&bridge->dev, "not setting up bridge for bus "
10876841ec68SYinghai Lu 			 "%04x:%02x\n", pci_domain_nr(b), b->number);
10886841ec68SYinghai Lu 		break;
10896841ec68SYinghai Lu 	}
10906841ec68SYinghai Lu }
10915009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus,
10925009b460SYinghai Lu 					  unsigned long type)
10935009b460SYinghai Lu {
10945009b460SYinghai Lu 	int idx;
10955009b460SYinghai Lu 	bool changed = false;
10965009b460SYinghai Lu 	struct pci_dev *dev;
10975009b460SYinghai Lu 	struct resource *r;
10985009b460SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
10995009b460SYinghai Lu 				  IORESOURCE_PREFETCH;
11005009b460SYinghai Lu 
11015009b460SYinghai Lu 	dev = bus->self;
11025009b460SYinghai Lu 	for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
11035009b460SYinghai Lu 	     idx++) {
11045009b460SYinghai Lu 		r = &dev->resource[idx];
11055009b460SYinghai Lu 		if ((r->flags & type_mask) != type)
11065009b460SYinghai Lu 			continue;
11075009b460SYinghai Lu 		if (!r->parent)
11085009b460SYinghai Lu 			continue;
11095009b460SYinghai Lu 		/*
11105009b460SYinghai Lu 		 * if there are children under that, we should release them
11115009b460SYinghai Lu 		 *  all
11125009b460SYinghai Lu 		 */
11135009b460SYinghai Lu 		release_child_resources(r);
11145009b460SYinghai Lu 		if (!release_resource(r)) {
11155009b460SYinghai Lu 			dev_printk(KERN_DEBUG, &dev->dev,
11165009b460SYinghai Lu 				 "resource %d %pR released\n", idx, r);
11175009b460SYinghai Lu 			/* keep the old size */
11185009b460SYinghai Lu 			r->end = resource_size(r) - 1;
11195009b460SYinghai Lu 			r->start = 0;
11205009b460SYinghai Lu 			r->flags = 0;
11215009b460SYinghai Lu 			changed = true;
11225009b460SYinghai Lu 		}
11235009b460SYinghai Lu 	}
11245009b460SYinghai Lu 
11255009b460SYinghai Lu 	if (changed) {
11265009b460SYinghai Lu 		/* avoiding touch the one without PREF */
11275009b460SYinghai Lu 		if (type & IORESOURCE_PREFETCH)
11285009b460SYinghai Lu 			type = IORESOURCE_PREFETCH;
11295009b460SYinghai Lu 		__pci_setup_bridge(bus, type);
11305009b460SYinghai Lu 	}
11315009b460SYinghai Lu }
11325009b460SYinghai Lu 
11335009b460SYinghai Lu enum release_type {
11345009b460SYinghai Lu 	leaf_only,
11355009b460SYinghai Lu 	whole_subtree,
11365009b460SYinghai Lu };
11375009b460SYinghai Lu /*
11385009b460SYinghai Lu  * try to release pci bridge resources that is from leaf bridge,
11395009b460SYinghai Lu  * so we can allocate big new one later
11405009b460SYinghai Lu  */
11415009b460SYinghai Lu static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
11425009b460SYinghai Lu 						   unsigned long type,
11435009b460SYinghai Lu 						   enum release_type rel_type)
11445009b460SYinghai Lu {
11455009b460SYinghai Lu 	struct pci_dev *dev;
11465009b460SYinghai Lu 	bool is_leaf_bridge = true;
11475009b460SYinghai Lu 
11485009b460SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
11495009b460SYinghai Lu 		struct pci_bus *b = dev->subordinate;
11505009b460SYinghai Lu 		if (!b)
11515009b460SYinghai Lu 			continue;
11525009b460SYinghai Lu 
11535009b460SYinghai Lu 		is_leaf_bridge = false;
11545009b460SYinghai Lu 
11555009b460SYinghai Lu 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
11565009b460SYinghai Lu 			continue;
11575009b460SYinghai Lu 
11585009b460SYinghai Lu 		if (rel_type == whole_subtree)
11595009b460SYinghai Lu 			pci_bus_release_bridge_resources(b, type,
11605009b460SYinghai Lu 						 whole_subtree);
11615009b460SYinghai Lu 	}
11625009b460SYinghai Lu 
11635009b460SYinghai Lu 	if (pci_is_root_bus(bus))
11645009b460SYinghai Lu 		return;
11655009b460SYinghai Lu 
11665009b460SYinghai Lu 	if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
11675009b460SYinghai Lu 		return;
11685009b460SYinghai Lu 
11695009b460SYinghai Lu 	if ((rel_type == whole_subtree) || is_leaf_bridge)
11705009b460SYinghai Lu 		pci_bridge_release_resources(bus, type);
11715009b460SYinghai Lu }
11725009b460SYinghai Lu 
117376fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
117476fbc263SYinghai Lu {
117589a74eccSBjorn Helgaas 	struct resource *res;
117676fbc263SYinghai Lu 	int i;
117776fbc263SYinghai Lu 
117889a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
11797c9342b8SYinghai Lu 		if (!res || !res->end || !res->flags)
118076fbc263SYinghai Lu                         continue;
118176fbc263SYinghai Lu 
1182c7dabef8SBjorn Helgaas 		dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
118376fbc263SYinghai Lu         }
118476fbc263SYinghai Lu }
118576fbc263SYinghai Lu 
118676fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
118776fbc263SYinghai Lu {
118876fbc263SYinghai Lu 	struct pci_bus *b;
118976fbc263SYinghai Lu 	struct pci_dev *dev;
119076fbc263SYinghai Lu 
119176fbc263SYinghai Lu 
119276fbc263SYinghai Lu 	pci_bus_dump_res(bus);
119376fbc263SYinghai Lu 
119476fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
119576fbc263SYinghai Lu 		b = dev->subordinate;
119676fbc263SYinghai Lu 		if (!b)
119776fbc263SYinghai Lu 			continue;
119876fbc263SYinghai Lu 
119976fbc263SYinghai Lu 		pci_bus_dump_resources(b);
120076fbc263SYinghai Lu 	}
120176fbc263SYinghai Lu }
120276fbc263SYinghai Lu 
1203da7822e5SYinghai Lu static int __init pci_bus_get_depth(struct pci_bus *bus)
1204da7822e5SYinghai Lu {
1205da7822e5SYinghai Lu 	int depth = 0;
1206da7822e5SYinghai Lu 	struct pci_dev *dev;
1207da7822e5SYinghai Lu 
1208da7822e5SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
1209da7822e5SYinghai Lu 		int ret;
1210da7822e5SYinghai Lu 		struct pci_bus *b = dev->subordinate;
1211da7822e5SYinghai Lu 		if (!b)
1212da7822e5SYinghai Lu 			continue;
1213da7822e5SYinghai Lu 
1214da7822e5SYinghai Lu 		ret = pci_bus_get_depth(b);
1215da7822e5SYinghai Lu 		if (ret + 1 > depth)
1216da7822e5SYinghai Lu 			depth = ret + 1;
1217da7822e5SYinghai Lu 	}
1218da7822e5SYinghai Lu 
1219da7822e5SYinghai Lu 	return depth;
1220da7822e5SYinghai Lu }
1221da7822e5SYinghai Lu static int __init pci_get_max_depth(void)
1222da7822e5SYinghai Lu {
1223da7822e5SYinghai Lu 	int depth = 0;
1224da7822e5SYinghai Lu 	struct pci_bus *bus;
1225da7822e5SYinghai Lu 
1226da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node) {
1227da7822e5SYinghai Lu 		int ret;
1228da7822e5SYinghai Lu 
1229da7822e5SYinghai Lu 		ret = pci_bus_get_depth(bus);
1230da7822e5SYinghai Lu 		if (ret > depth)
1231da7822e5SYinghai Lu 			depth = ret;
1232da7822e5SYinghai Lu 	}
1233da7822e5SYinghai Lu 
1234da7822e5SYinghai Lu 	return depth;
1235da7822e5SYinghai Lu }
1236da7822e5SYinghai Lu 
1237f483d392SRam Pai 
1238da7822e5SYinghai Lu /*
1239da7822e5SYinghai Lu  * first try will not touch pci bridge res
1240da7822e5SYinghai Lu  * second  and later try will clear small leaf bridge res
1241da7822e5SYinghai Lu  * will stop till to the max  deepth if can not find good one
1242da7822e5SYinghai Lu  */
12431da177e4SLinus Torvalds void __init
12441da177e4SLinus Torvalds pci_assign_unassigned_resources(void)
12451da177e4SLinus Torvalds {
12461da177e4SLinus Torvalds 	struct pci_bus *bus;
1247bdc4abecSYinghai Lu 	LIST_HEAD(realloc_head); /* list of resources that
1248c8adf9a3SRam Pai 					want additional resources */
1249bdc4abecSYinghai Lu 	struct list_head *add_list = NULL;
1250da7822e5SYinghai Lu 	int tried_times = 0;
1251da7822e5SYinghai Lu 	enum release_type rel_type = leaf_only;
1252bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
1253*b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
1254da7822e5SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1255da7822e5SYinghai Lu 				  IORESOURCE_PREFETCH;
1256da7822e5SYinghai Lu 	unsigned long failed_type;
125719aa7ee4SYinghai Lu 	int pci_try_num = 1;
1258da7822e5SYinghai Lu 
125919aa7ee4SYinghai Lu 	/* don't realloc if asked to do so */
126019aa7ee4SYinghai Lu 	if (pci_realloc_enabled()) {
126119aa7ee4SYinghai Lu 		int max_depth = pci_get_max_depth();
126219aa7ee4SYinghai Lu 
1263da7822e5SYinghai Lu 		pci_try_num = max_depth + 1;
1264da7822e5SYinghai Lu 		printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1265da7822e5SYinghai Lu 			 max_depth, pci_try_num);
126619aa7ee4SYinghai Lu 	}
1267da7822e5SYinghai Lu 
1268da7822e5SYinghai Lu again:
126919aa7ee4SYinghai Lu 	/*
127019aa7ee4SYinghai Lu 	 * last try will use add_list, otherwise will try good to have as
127119aa7ee4SYinghai Lu 	 * must have, so can realloc parent bridge resource
127219aa7ee4SYinghai Lu 	 */
127319aa7ee4SYinghai Lu 	if (tried_times + 1 == pci_try_num)
1274bdc4abecSYinghai Lu 		add_list = &realloc_head;
12751da177e4SLinus Torvalds 	/* Depth first, calculate sizes and alignments of all
12761da177e4SLinus Torvalds 	   subordinate buses. */
1277da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
127819aa7ee4SYinghai Lu 		__pci_bus_size_bridges(bus, add_list);
1279c8adf9a3SRam Pai 
12801da177e4SLinus Torvalds 	/* Depth last, allocate resources and update the hardware. */
1281da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
1282bdc4abecSYinghai Lu 		__pci_bus_assign_resources(bus, add_list, &fail_head);
128319aa7ee4SYinghai Lu 	if (add_list)
1284bdc4abecSYinghai Lu 		BUG_ON(!list_empty(add_list));
1285da7822e5SYinghai Lu 	tried_times++;
1286da7822e5SYinghai Lu 
1287da7822e5SYinghai Lu 	/* any device complain? */
1288bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
1289da7822e5SYinghai Lu 		goto enable_and_dump;
1290f483d392SRam Pai 
1291da7822e5SYinghai Lu 	failed_type = 0;
1292*b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list)
1293*b9b0bba9SYinghai Lu 		failed_type |= fail_res->flags;
1294bdc4abecSYinghai Lu 
1295da7822e5SYinghai Lu 	/*
1296da7822e5SYinghai Lu 	 * io port are tight, don't try extra
1297da7822e5SYinghai Lu 	 * or if reach the limit, don't want to try more
1298da7822e5SYinghai Lu 	 */
1299da7822e5SYinghai Lu 	failed_type &= type_mask;
1300da7822e5SYinghai Lu 	if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
1301764242a0SYinghai Lu 		free_list(pci_dev_resource, &fail_head);
1302da7822e5SYinghai Lu 		goto enable_and_dump;
1303da7822e5SYinghai Lu 	}
1304da7822e5SYinghai Lu 
1305da7822e5SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1306da7822e5SYinghai Lu 			 tried_times + 1);
1307da7822e5SYinghai Lu 
1308da7822e5SYinghai Lu 	/* third times and later will not check if it is leaf */
1309da7822e5SYinghai Lu 	if ((tried_times + 1) > 2)
1310da7822e5SYinghai Lu 		rel_type = whole_subtree;
1311da7822e5SYinghai Lu 
1312da7822e5SYinghai Lu 	/*
1313da7822e5SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
1314da7822e5SYinghai Lu 	 * child device under that bridge
1315da7822e5SYinghai Lu 	 */
1316*b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1317*b9b0bba9SYinghai Lu 		bus = fail_res->dev->bus;
1318bdc4abecSYinghai Lu 		pci_bus_release_bridge_resources(bus,
1319*b9b0bba9SYinghai Lu 						 fail_res->flags & type_mask,
1320da7822e5SYinghai Lu 						 rel_type);
1321da7822e5SYinghai Lu 	}
1322da7822e5SYinghai Lu 	/* restore size and flags */
1323*b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1324*b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
1325da7822e5SYinghai Lu 
1326*b9b0bba9SYinghai Lu 		res->start = fail_res->start;
1327*b9b0bba9SYinghai Lu 		res->end = fail_res->end;
1328*b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
1329*b9b0bba9SYinghai Lu 		if (fail_res->dev->subordinate)
1330da7822e5SYinghai Lu 			res->flags = 0;
1331da7822e5SYinghai Lu 	}
1332764242a0SYinghai Lu 	free_list(pci_dev_resource, &fail_head);
1333da7822e5SYinghai Lu 
1334da7822e5SYinghai Lu 	goto again;
1335da7822e5SYinghai Lu 
1336da7822e5SYinghai Lu enable_and_dump:
1337da7822e5SYinghai Lu 	/* Depth last, update the hardware. */
1338da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
1339da7822e5SYinghai Lu 		pci_enable_bridges(bus);
134076fbc263SYinghai Lu 
134176fbc263SYinghai Lu 	/* dump the resource on buses */
1342da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
134376fbc263SYinghai Lu 		pci_bus_dump_resources(bus);
134476fbc263SYinghai Lu }
13456841ec68SYinghai Lu 
13466841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
13476841ec68SYinghai Lu {
13486841ec68SYinghai Lu 	struct pci_bus *parent = bridge->subordinate;
1349bdc4abecSYinghai Lu 	LIST_HEAD(add_list); /* list of resources that
13508424d759SYinghai Lu 					want additional resources */
135132180e40SYinghai Lu 	int tried_times = 0;
1352bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
1353*b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
13546841ec68SYinghai Lu 	int retval;
135532180e40SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
135632180e40SYinghai Lu 				  IORESOURCE_PREFETCH;
13576841ec68SYinghai Lu 
135832180e40SYinghai Lu again:
13598424d759SYinghai Lu 	__pci_bus_size_bridges(parent, &add_list);
1360bdc4abecSYinghai Lu 	__pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1361bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
136232180e40SYinghai Lu 	tried_times++;
136332180e40SYinghai Lu 
1364bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
13653f579c34SYinghai Lu 		goto enable_all;
136632180e40SYinghai Lu 
136732180e40SYinghai Lu 	if (tried_times >= 2) {
136832180e40SYinghai Lu 		/* still fail, don't need to try more */
1369764242a0SYinghai Lu 		free_list(pci_dev_resource, &fail_head);
13703f579c34SYinghai Lu 		goto enable_all;
137132180e40SYinghai Lu 	}
137232180e40SYinghai Lu 
137332180e40SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
137432180e40SYinghai Lu 			 tried_times + 1);
137532180e40SYinghai Lu 
137632180e40SYinghai Lu 	/*
137732180e40SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
137832180e40SYinghai Lu 	 * child device under that bridge
137932180e40SYinghai Lu 	 */
1380*b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1381*b9b0bba9SYinghai Lu 		struct pci_bus *bus = fail_res->dev->bus;
1382*b9b0bba9SYinghai Lu 		unsigned long flags = fail_res->flags;
138332180e40SYinghai Lu 
138432180e40SYinghai Lu 		pci_bus_release_bridge_resources(bus, flags & type_mask,
138532180e40SYinghai Lu 						 whole_subtree);
138632180e40SYinghai Lu 	}
138732180e40SYinghai Lu 	/* restore size and flags */
1388*b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1389*b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
139032180e40SYinghai Lu 
1391*b9b0bba9SYinghai Lu 		res->start = fail_res->start;
1392*b9b0bba9SYinghai Lu 		res->end = fail_res->end;
1393*b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
1394*b9b0bba9SYinghai Lu 		if (fail_res->dev->subordinate)
139532180e40SYinghai Lu 			res->flags = 0;
139632180e40SYinghai Lu 	}
1397764242a0SYinghai Lu 	free_list(pci_dev_resource, &fail_head);
139832180e40SYinghai Lu 
139932180e40SYinghai Lu 	goto again;
14003f579c34SYinghai Lu 
14013f579c34SYinghai Lu enable_all:
14023f579c34SYinghai Lu 	retval = pci_reenable_device(bridge);
14033f579c34SYinghai Lu 	pci_set_master(bridge);
14043f579c34SYinghai Lu 	pci_enable_bridges(parent);
14056841ec68SYinghai Lu }
14066841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
14079b03088fSYinghai Lu 
14089b03088fSYinghai Lu #ifdef CONFIG_HOTPLUG
14099b03088fSYinghai Lu /**
14109b03088fSYinghai Lu  * pci_rescan_bus - scan a PCI bus for devices.
14119b03088fSYinghai Lu  * @bus: PCI bus to scan
14129b03088fSYinghai Lu  *
14139b03088fSYinghai Lu  * Scan a PCI bus and child buses for new devices, adds them,
14149b03088fSYinghai Lu  * and enables them.
14159b03088fSYinghai Lu  *
14169b03088fSYinghai Lu  * Returns the max number of subordinate bus discovered.
14179b03088fSYinghai Lu  */
14189b03088fSYinghai Lu unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
14199b03088fSYinghai Lu {
14209b03088fSYinghai Lu 	unsigned int max;
14219b03088fSYinghai Lu 	struct pci_dev *dev;
1422bdc4abecSYinghai Lu 	LIST_HEAD(add_list); /* list of resources that
14239b03088fSYinghai Lu 					want additional resources */
14249b03088fSYinghai Lu 
14259b03088fSYinghai Lu 	max = pci_scan_child_bus(bus);
14269b03088fSYinghai Lu 
14279b03088fSYinghai Lu 	down_read(&pci_bus_sem);
14289b03088fSYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
14299b03088fSYinghai Lu 		if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
14309b03088fSYinghai Lu 		    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
14319b03088fSYinghai Lu 			if (dev->subordinate)
14329b03088fSYinghai Lu 				__pci_bus_size_bridges(dev->subordinate,
14339b03088fSYinghai Lu 							 &add_list);
14349b03088fSYinghai Lu 	up_read(&pci_bus_sem);
14359b03088fSYinghai Lu 	__pci_bus_assign_resources(bus, &add_list, NULL);
1436bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
14379b03088fSYinghai Lu 
14389b03088fSYinghai Lu 	pci_enable_bridges(bus);
14399b03088fSYinghai Lu 	pci_bus_add_devices(bus);
14409b03088fSYinghai Lu 
14419b03088fSYinghai Lu 	return max;
14429b03088fSYinghai Lu }
14439b03088fSYinghai Lu EXPORT_SYMBOL_GPL(pci_rescan_bus);
14449b03088fSYinghai Lu #endif
1445