xref: /openbmc/linux/drivers/pci/setup-bus.c (revision 67cc7e26a5c46508ee00b9fe169aad833b798025)
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 
41bffc56d4SYinghai Lu static void free_list(struct list_head *head)
42bffc56d4SYinghai Lu {
43bffc56d4SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
44bffc56d4SYinghai Lu 
45bffc56d4SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
46bffc56d4SYinghai Lu 		list_del(&dev_res->list);
47bffc56d4SYinghai Lu 		kfree(dev_res);
48bffc56d4SYinghai Lu 	}
49bffc56d4SYinghai Lu }
50094732a5SRam Pai 
51f483d392SRam Pai int pci_realloc_enable = 0;
52f483d392SRam Pai #define pci_realloc_enabled() pci_realloc_enable
53f483d392SRam Pai void pci_realloc(void)
54f483d392SRam Pai {
55f483d392SRam Pai 	pci_realloc_enable = 1;
56f483d392SRam Pai }
57f483d392SRam Pai 
58c8adf9a3SRam Pai /**
59c8adf9a3SRam Pai  * add_to_list() - add a new resource tracker to the list
60c8adf9a3SRam Pai  * @head:	Head of the list
61c8adf9a3SRam Pai  * @dev:	device corresponding to which the resource
62c8adf9a3SRam Pai  *		belongs
63c8adf9a3SRam Pai  * @res:	The resource to be tracked
64c8adf9a3SRam Pai  * @add_size:	additional size to be optionally added
65c8adf9a3SRam Pai  *              to the resource
66c8adf9a3SRam Pai  */
67bdc4abecSYinghai Lu static int add_to_list(struct list_head *head,
68c8adf9a3SRam Pai 		 struct pci_dev *dev, struct resource *res,
692bbc6942SRam Pai 		 resource_size_t add_size, resource_size_t min_align)
70568ddef8SYinghai Lu {
71764242a0SYinghai Lu 	struct pci_dev_resource *tmp;
72568ddef8SYinghai Lu 
73bdc4abecSYinghai Lu 	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
74568ddef8SYinghai Lu 	if (!tmp) {
75c8adf9a3SRam Pai 		pr_warning("add_to_list: kmalloc() failed!\n");
76ef62dfefSYinghai Lu 		return -ENOMEM;
77568ddef8SYinghai Lu 	}
78568ddef8SYinghai Lu 
79568ddef8SYinghai Lu 	tmp->res = res;
80568ddef8SYinghai Lu 	tmp->dev = dev;
81568ddef8SYinghai Lu 	tmp->start = res->start;
82568ddef8SYinghai Lu 	tmp->end = res->end;
83568ddef8SYinghai Lu 	tmp->flags = res->flags;
84c8adf9a3SRam Pai 	tmp->add_size = add_size;
852bbc6942SRam Pai 	tmp->min_align = min_align;
86bdc4abecSYinghai Lu 
87bdc4abecSYinghai Lu 	list_add(&tmp->list, head);
88ef62dfefSYinghai Lu 
89ef62dfefSYinghai Lu 	return 0;
90568ddef8SYinghai Lu }
91568ddef8SYinghai Lu 
92b9b0bba9SYinghai Lu static void remove_from_list(struct list_head *head,
933e6e0d80SYinghai Lu 				 struct resource *res)
943e6e0d80SYinghai Lu {
95b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
963e6e0d80SYinghai Lu 
97b9b0bba9SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
98b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
99b9b0bba9SYinghai Lu 			list_del(&dev_res->list);
100b9b0bba9SYinghai Lu 			kfree(dev_res);
101bdc4abecSYinghai Lu 			break;
1023e6e0d80SYinghai Lu 		}
1033e6e0d80SYinghai Lu 	}
1043e6e0d80SYinghai Lu }
1053e6e0d80SYinghai Lu 
106b9b0bba9SYinghai Lu static resource_size_t get_res_add_size(struct list_head *head,
1071c372353SYinghai Lu 					struct resource *res)
1081c372353SYinghai Lu {
109b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res;
1101c372353SYinghai Lu 
111b9b0bba9SYinghai Lu 	list_for_each_entry(dev_res, head, list) {
112b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
113b592443dSYinghai Lu 			int idx = res - &dev_res->dev->resource[0];
114b592443dSYinghai Lu 
115b9b0bba9SYinghai Lu 			dev_printk(KERN_DEBUG, &dev_res->dev->dev,
116b592443dSYinghai Lu 				 "res[%d]=%pR get_res_add_size add_size %llx\n",
117b592443dSYinghai Lu 				 idx, dev_res->res,
118b9b0bba9SYinghai Lu 				 (unsigned long long)dev_res->add_size);
119b592443dSYinghai Lu 
120b9b0bba9SYinghai Lu 			return dev_res->add_size;
121bdc4abecSYinghai Lu 		}
1223e6e0d80SYinghai Lu 	}
1231c372353SYinghai Lu 
1241c372353SYinghai Lu 	return 0;
1251c372353SYinghai Lu }
1261c372353SYinghai Lu 
12778c3b329SYinghai Lu /* Sort resources by alignment */
128bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
12978c3b329SYinghai Lu {
13078c3b329SYinghai Lu 	int i;
13178c3b329SYinghai Lu 
13278c3b329SYinghai Lu 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
13378c3b329SYinghai Lu 		struct resource *r;
134bdc4abecSYinghai Lu 		struct pci_dev_resource *dev_res, *tmp;
13578c3b329SYinghai Lu 		resource_size_t r_align;
136bdc4abecSYinghai Lu 		struct list_head *n;
13778c3b329SYinghai Lu 
13878c3b329SYinghai Lu 		r = &dev->resource[i];
13978c3b329SYinghai Lu 
14078c3b329SYinghai Lu 		if (r->flags & IORESOURCE_PCI_FIXED)
14178c3b329SYinghai Lu 			continue;
14278c3b329SYinghai Lu 
14378c3b329SYinghai Lu 		if (!(r->flags) || r->parent)
14478c3b329SYinghai Lu 			continue;
14578c3b329SYinghai Lu 
14678c3b329SYinghai Lu 		r_align = pci_resource_alignment(dev, r);
14778c3b329SYinghai Lu 		if (!r_align) {
14878c3b329SYinghai Lu 			dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
14978c3b329SYinghai Lu 				 i, r);
15078c3b329SYinghai Lu 			continue;
15178c3b329SYinghai Lu 		}
15278c3b329SYinghai Lu 
153bdc4abecSYinghai Lu 		tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
15478c3b329SYinghai Lu 		if (!tmp)
15578c3b329SYinghai Lu 			panic("pdev_sort_resources(): "
15678c3b329SYinghai Lu 			      "kmalloc() failed!\n");
15778c3b329SYinghai Lu 		tmp->res = r;
15878c3b329SYinghai Lu 		tmp->dev = dev;
159bdc4abecSYinghai Lu 
160bdc4abecSYinghai Lu 		/* fallback is smallest one or list is empty*/
161bdc4abecSYinghai Lu 		n = head;
162bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
163bdc4abecSYinghai Lu 			resource_size_t align;
164bdc4abecSYinghai Lu 
165bdc4abecSYinghai Lu 			align = pci_resource_alignment(dev_res->dev,
166bdc4abecSYinghai Lu 							 dev_res->res);
167bdc4abecSYinghai Lu 
168bdc4abecSYinghai Lu 			if (r_align > align) {
169bdc4abecSYinghai Lu 				n = &dev_res->list;
17078c3b329SYinghai Lu 				break;
17178c3b329SYinghai Lu 			}
17278c3b329SYinghai Lu 		}
173bdc4abecSYinghai Lu 		/* Insert it just before n*/
174bdc4abecSYinghai Lu 		list_add_tail(&tmp->list, n);
17578c3b329SYinghai Lu 	}
17678c3b329SYinghai Lu }
17778c3b329SYinghai Lu 
1786841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev,
179bdc4abecSYinghai Lu 				 struct list_head *head)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	u16 class = dev->class >> 8;
1821da177e4SLinus Torvalds 
1839bded00bSKenji Kaneshige 	/* Don't touch classless devices or host bridges or ioapics.  */
1846841ec68SYinghai Lu 	if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
1856841ec68SYinghai Lu 		return;
1861da177e4SLinus Torvalds 
1879bded00bSKenji Kaneshige 	/* Don't touch ioapic devices already enabled by firmware */
18823186279SSatoru Takeuchi 	if (class == PCI_CLASS_SYSTEM_PIC) {
1899bded00bSKenji Kaneshige 		u16 command;
1909bded00bSKenji Kaneshige 		pci_read_config_word(dev, PCI_COMMAND, &command);
1919bded00bSKenji Kaneshige 		if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
1926841ec68SYinghai Lu 			return;
19323186279SSatoru Takeuchi 	}
19423186279SSatoru Takeuchi 
1956841ec68SYinghai Lu 	pdev_sort_resources(dev, head);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
198fc075e1dSRam Pai static inline void reset_resource(struct resource *res)
199fc075e1dSRam Pai {
200fc075e1dSRam Pai 	res->start = 0;
201fc075e1dSRam Pai 	res->end = 0;
202fc075e1dSRam Pai 	res->flags = 0;
203fc075e1dSRam Pai }
204fc075e1dSRam Pai 
205c8adf9a3SRam Pai /**
2069e8bf93aSRam Pai  * reassign_resources_sorted() - satisfy any additional resource requests
207c8adf9a3SRam Pai  *
2089e8bf93aSRam Pai  * @realloc_head : head of the list tracking requests requiring additional
209c8adf9a3SRam Pai  *             resources
210c8adf9a3SRam Pai  * @head     : head of the list tracking requests with allocated
211c8adf9a3SRam Pai  *             resources
212c8adf9a3SRam Pai  *
2139e8bf93aSRam Pai  * Walk through each element of the realloc_head and try to procure
214c8adf9a3SRam Pai  * additional resources for the element, provided the element
215c8adf9a3SRam Pai  * is in the head list.
216c8adf9a3SRam Pai  */
217bdc4abecSYinghai Lu static void reassign_resources_sorted(struct list_head *realloc_head,
218bdc4abecSYinghai Lu 		struct list_head *head)
219c8adf9a3SRam Pai {
220c8adf9a3SRam Pai 	struct resource *res;
221b9b0bba9SYinghai Lu 	struct pci_dev_resource *add_res, *tmp;
222bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
223c8adf9a3SRam Pai 	resource_size_t add_size;
224c8adf9a3SRam Pai 	int idx;
225c8adf9a3SRam Pai 
226b9b0bba9SYinghai Lu 	list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
227bdc4abecSYinghai Lu 		bool found_match = false;
228bdc4abecSYinghai Lu 
229b9b0bba9SYinghai Lu 		res = add_res->res;
230c8adf9a3SRam Pai 		/* skip resource that has been reset */
231c8adf9a3SRam Pai 		if (!res->flags)
232c8adf9a3SRam Pai 			goto out;
233c8adf9a3SRam Pai 
234c8adf9a3SRam Pai 		/* skip this resource if not found in head list */
235bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
236bdc4abecSYinghai Lu 			if (dev_res->res == res) {
237bdc4abecSYinghai Lu 				found_match = true;
238bdc4abecSYinghai Lu 				break;
239c8adf9a3SRam Pai 			}
240bdc4abecSYinghai Lu 		}
241bdc4abecSYinghai Lu 		if (!found_match)/* just skip */
242bdc4abecSYinghai Lu 			continue;
243c8adf9a3SRam Pai 
244b9b0bba9SYinghai Lu 		idx = res - &add_res->dev->resource[0];
245b9b0bba9SYinghai Lu 		add_size = add_res->add_size;
2462bbc6942SRam Pai 		if (!resource_size(res)) {
247b9b0bba9SYinghai Lu 			res->start = add_res->start;
248c8adf9a3SRam Pai 			res->end = res->start + add_size - 1;
249b9b0bba9SYinghai Lu 			if (pci_assign_resource(add_res->dev, idx))
250c8adf9a3SRam Pai 				reset_resource(res);
2512bbc6942SRam Pai 		} else {
252b9b0bba9SYinghai Lu 			resource_size_t align = add_res->min_align;
253b9b0bba9SYinghai Lu 			res->flags |= add_res->flags &
254bdc4abecSYinghai Lu 				 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
255b9b0bba9SYinghai Lu 			if (pci_reassign_resource(add_res->dev, idx,
256bdc4abecSYinghai Lu 						  add_size, align))
257b9b0bba9SYinghai Lu 				dev_printk(KERN_DEBUG, &add_res->dev->dev,
258b592443dSYinghai Lu 					   "failed to add %llx res[%d]=%pR\n",
259b592443dSYinghai Lu 					   (unsigned long long)add_size,
260b592443dSYinghai Lu 					   idx, res);
261c8adf9a3SRam Pai 		}
262c8adf9a3SRam Pai out:
263b9b0bba9SYinghai Lu 		list_del(&add_res->list);
264b9b0bba9SYinghai Lu 		kfree(add_res);
265c8adf9a3SRam Pai 	}
266c8adf9a3SRam Pai }
267c8adf9a3SRam Pai 
268c8adf9a3SRam Pai /**
269c8adf9a3SRam Pai  * assign_requested_resources_sorted() - satisfy resource requests
270c8adf9a3SRam Pai  *
271c8adf9a3SRam Pai  * @head : head of the list tracking requests for resources
272c8adf9a3SRam Pai  * @failed_list : head of the list tracking requests that could
273c8adf9a3SRam Pai  *		not be allocated
274c8adf9a3SRam Pai  *
275c8adf9a3SRam Pai  * Satisfy resource requests of each element in the list. Add
276c8adf9a3SRam Pai  * requests that could not satisfied to the failed_list.
277c8adf9a3SRam Pai  */
278bdc4abecSYinghai Lu static void assign_requested_resources_sorted(struct list_head *head,
279bdc4abecSYinghai Lu 				 struct list_head *fail_head)
2806841ec68SYinghai Lu {
2816841ec68SYinghai Lu 	struct resource *res;
282bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
2836841ec68SYinghai Lu 	int idx;
2846841ec68SYinghai Lu 
285bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
286bdc4abecSYinghai Lu 		res = dev_res->res;
287bdc4abecSYinghai Lu 		idx = res - &dev_res->dev->resource[0];
288bdc4abecSYinghai Lu 		if (resource_size(res) &&
289bdc4abecSYinghai Lu 		    pci_assign_resource(dev_res->dev, idx)) {
290bdc4abecSYinghai Lu 			if (fail_head && !pci_is_root_bus(dev_res->dev->bus)) {
2919a928660SYinghai Lu 				/*
2929a928660SYinghai Lu 				 * if the failed res is for ROM BAR, and it will
2939a928660SYinghai Lu 				 * be enabled later, don't add it to the list
2949a928660SYinghai Lu 				 */
2959a928660SYinghai Lu 				if (!((idx == PCI_ROM_RESOURCE) &&
2969a928660SYinghai Lu 				      (!(res->flags & IORESOURCE_ROM_ENABLE))))
297*67cc7e26SYinghai Lu 					add_to_list(fail_head,
298*67cc7e26SYinghai Lu 						    dev_res->dev, res,
299*67cc7e26SYinghai Lu 						    0 /* dont care */,
300*67cc7e26SYinghai Lu 						    0 /* dont care */);
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);
322b9b0bba9SYinghai 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)) {
332bffc56d4SYinghai Lu 			free_list(&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);
350bffc56d4SYinghai Lu 		free_list(&save_head);
351bffc56d4SYinghai Lu 		free_list(head);
3523e6e0d80SYinghai Lu 		return;
3533e6e0d80SYinghai Lu 	}
3543e6e0d80SYinghai Lu 
355bffc56d4SYinghai Lu 	free_list(&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 */
361b9b0bba9SYinghai Lu 	list_for_each_entry(save_res, &save_head, list) {
362b9b0bba9SYinghai Lu 		struct resource *res = save_res->res;
3633e6e0d80SYinghai Lu 
364b9b0bba9SYinghai Lu 		res->start = save_res->start;
365b9b0bba9SYinghai Lu 		res->end = save_res->end;
366b9b0bba9SYinghai Lu 		res->flags = save_res->flags;
3673e6e0d80SYinghai Lu 	}
368bffc56d4SYinghai Lu 	free_list(&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);
378bffc56d4SYinghai Lu 	free_list(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;
761b592443dSYinghai Lu 	if (size1 > size0 && realloc_head) {
7629e8bf93aSRam Pai 		add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096);
763b592443dSYinghai Lu 		dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
764b592443dSYinghai Lu 				 "%pR to [bus %02x-%02x] add_size %lx\n", b_res,
765b592443dSYinghai Lu 				 bus->secondary, bus->subordinate, size1-size0);
766b592443dSYinghai Lu 	}
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
769c8adf9a3SRam Pai /**
770c8adf9a3SRam Pai  * pbus_size_mem() - size the memory window of a given bus
771c8adf9a3SRam Pai  *
772c8adf9a3SRam Pai  * @bus : the bus
773c8adf9a3SRam Pai  * @min_size : the minimum memory window that must to be allocated
774c8adf9a3SRam Pai  * @add_size : additional optional memory window
7759e8bf93aSRam Pai  * @realloc_head : track the additional memory window on this list
776c8adf9a3SRam Pai  *
777c8adf9a3SRam Pai  * Calculate the size of the bus and minimal alignment which
778c8adf9a3SRam Pai  * guarantees that all child resources fit in this size.
779c8adf9a3SRam Pai  */
78028760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
781c8adf9a3SRam Pai 			 unsigned long type, resource_size_t min_size,
782c8adf9a3SRam Pai 			resource_size_t add_size,
783bdc4abecSYinghai Lu 			struct list_head *realloc_head)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	struct pci_dev *dev;
786c8adf9a3SRam Pai 	resource_size_t min_align, align, size, size0, size1;
787c40a22e0SBenjamin Herrenschmidt 	resource_size_t aligns[12];	/* Alignments from 1Mb to 2Gb */
7881da177e4SLinus Torvalds 	int order, max_order;
7891da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, type);
7901f82de10SYinghai Lu 	unsigned int mem64_mask = 0;
791be768912SYinghai Lu 	resource_size_t children_add_size = 0;
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	if (!b_res)
7941da177e4SLinus Torvalds 		return 0;
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
7971da177e4SLinus Torvalds 	max_order = 0;
7981da177e4SLinus Torvalds 	size = 0;
7991da177e4SLinus Torvalds 
8001f82de10SYinghai Lu 	mem64_mask = b_res->flags & IORESOURCE_MEM_64;
8011f82de10SYinghai Lu 	b_res->flags &= ~IORESOURCE_MEM_64;
8021f82de10SYinghai Lu 
8031da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
8041da177e4SLinus Torvalds 		int i;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
8071da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
808c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 			if (r->parent || (r->flags & mask) != type)
8111da177e4SLinus Torvalds 				continue;
812022edd86SZhao, Yu 			r_size = resource_size(r);
8132aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV
8142aceefcbSYinghai Lu 			/* put SRIOV requested res to the optional list */
8159e8bf93aSRam Pai 			if (realloc_head && i >= PCI_IOV_RESOURCES &&
8162aceefcbSYinghai Lu 					i <= PCI_IOV_RESOURCE_END) {
8172aceefcbSYinghai Lu 				r->end = r->start - 1;
8189e8bf93aSRam Pai 				add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */);
8192aceefcbSYinghai Lu 				children_add_size += r_size;
8202aceefcbSYinghai Lu 				continue;
8212aceefcbSYinghai Lu 			}
8222aceefcbSYinghai Lu #endif
8231da177e4SLinus Torvalds 			/* For bridges size != alignment */
8246faf17f6SChris Wright 			align = pci_resource_alignment(dev, r);
8251da177e4SLinus Torvalds 			order = __ffs(align) - 20;
8261da177e4SLinus Torvalds 			if (order > 11) {
827865df576SBjorn Helgaas 				dev_warn(&dev->dev, "disabling BAR %d: %pR "
828865df576SBjorn Helgaas 					 "(bad alignment %#llx)\n", i, r,
829865df576SBjorn Helgaas 					 (unsigned long long) align);
8301da177e4SLinus Torvalds 				r->flags = 0;
8311da177e4SLinus Torvalds 				continue;
8321da177e4SLinus Torvalds 			}
8331da177e4SLinus Torvalds 			size += r_size;
8341da177e4SLinus Torvalds 			if (order < 0)
8351da177e4SLinus Torvalds 				order = 0;
8361da177e4SLinus Torvalds 			/* Exclude ranges with size > align from
8371da177e4SLinus Torvalds 			   calculation of the alignment. */
8381da177e4SLinus Torvalds 			if (r_size == align)
8391da177e4SLinus Torvalds 				aligns[order] += align;
8401da177e4SLinus Torvalds 			if (order > max_order)
8411da177e4SLinus Torvalds 				max_order = order;
8421f82de10SYinghai Lu 			mem64_mask &= r->flags & IORESOURCE_MEM_64;
843be768912SYinghai Lu 
8449e8bf93aSRam Pai 			if (realloc_head)
8459e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
8461da177e4SLinus Torvalds 		}
8471da177e4SLinus Torvalds 	}
8481da177e4SLinus Torvalds 	align = 0;
8491da177e4SLinus Torvalds 	min_align = 0;
8501da177e4SLinus Torvalds 	for (order = 0; order <= max_order; order++) {
8518308c54dSJeremy Fitzhardinge 		resource_size_t align1 = 1;
8528308c54dSJeremy Fitzhardinge 
8538308c54dSJeremy Fitzhardinge 		align1 <<= (order + 20);
8548308c54dSJeremy Fitzhardinge 
8551da177e4SLinus Torvalds 		if (!align)
8561da177e4SLinus Torvalds 			min_align = align1;
8576f6f8c2fSMilind Arun Choudhary 		else if (ALIGN(align + min_align, min_align) < align1)
8581da177e4SLinus Torvalds 			min_align = align1 >> 1;
8591da177e4SLinus Torvalds 		align += aligns[order];
8601da177e4SLinus Torvalds 	}
861b42282e5SLinus Torvalds 	size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
862be768912SYinghai Lu 	if (children_add_size > add_size)
863be768912SYinghai Lu 		add_size = children_add_size;
8649e8bf93aSRam Pai 	size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
865a4ac9feaSYinghai Lu 		calculate_memsize(size, min_size, add_size,
866b42282e5SLinus Torvalds 				resource_size(b_res), min_align);
867c8adf9a3SRam Pai 	if (!size0 && !size1) {
868865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
869865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
870865df576SBjorn Helgaas 				 "%pR to [bus %02x-%02x] (unused)\n", b_res,
871865df576SBjorn Helgaas 				 bus->secondary, bus->subordinate);
8721da177e4SLinus Torvalds 		b_res->flags = 0;
8731da177e4SLinus Torvalds 		return 1;
8741da177e4SLinus Torvalds 	}
8751da177e4SLinus Torvalds 	b_res->start = min_align;
876c8adf9a3SRam Pai 	b_res->end = size0 + min_align - 1;
877c8adf9a3SRam Pai 	b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
878b592443dSYinghai Lu 	if (size1 > size0 && realloc_head) {
8799e8bf93aSRam Pai 		add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
880b592443dSYinghai Lu 		dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
881b592443dSYinghai Lu 				 "%pR to [bus %02x-%02x] add_size %llx\n", b_res,
882b592443dSYinghai Lu 				 bus->secondary, bus->subordinate, (unsigned long long)size1-size0);
883b592443dSYinghai Lu 	}
8841da177e4SLinus Torvalds 	return 1;
8851da177e4SLinus Torvalds }
8861da177e4SLinus Torvalds 
8870a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res)
8880a2daa1cSRam Pai {
8890a2daa1cSRam Pai 	if (res->flags & IORESOURCE_IO)
8900a2daa1cSRam Pai 		return pci_cardbus_io_size;
8910a2daa1cSRam Pai 	if (res->flags & IORESOURCE_MEM)
8920a2daa1cSRam Pai 		return pci_cardbus_mem_size;
8930a2daa1cSRam Pai 	return 0;
8940a2daa1cSRam Pai }
8950a2daa1cSRam Pai 
8960a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus,
897bdc4abecSYinghai Lu 			struct list_head *realloc_head)
8981da177e4SLinus Torvalds {
8991da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
9001da177e4SLinus Torvalds 	struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
9011da177e4SLinus Torvalds 	u16 ctrl;
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	/*
9041da177e4SLinus Torvalds 	 * Reserve some resources for CardBus.  We reserve
9051da177e4SLinus Torvalds 	 * a fixed amount of bus space for CardBus bridges.
9061da177e4SLinus Torvalds 	 */
907934b7024SLinus Torvalds 	b_res[0].start = 0;
908934b7024SLinus Torvalds 	b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
9099e8bf93aSRam Pai 	if (realloc_head)
9109e8bf93aSRam Pai 		add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */);
9111da177e4SLinus Torvalds 
912934b7024SLinus Torvalds 	b_res[1].start = 0;
913934b7024SLinus Torvalds 	b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
9149e8bf93aSRam Pai 	if (realloc_head)
9159e8bf93aSRam Pai 		add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */);
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	/*
9181da177e4SLinus Torvalds 	 * Check whether prefetchable memory is supported
9191da177e4SLinus Torvalds 	 * by this bridge.
9201da177e4SLinus Torvalds 	 */
9211da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
9221da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
9231da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
9241da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
9251da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
9261da177e4SLinus Torvalds 	}
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	/*
9291da177e4SLinus Torvalds 	 * If we have prefetchable memory support, allocate
9301da177e4SLinus Torvalds 	 * two regions.  Otherwise, allocate one region of
9311da177e4SLinus Torvalds 	 * twice the size.
9321da177e4SLinus Torvalds 	 */
9331da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
934934b7024SLinus Torvalds 		b_res[2].start = 0;
935934b7024SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
9369e8bf93aSRam Pai 		if (realloc_head)
9379e8bf93aSRam Pai 			add_to_list(realloc_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */);
9381da177e4SLinus Torvalds 
939934b7024SLinus Torvalds 		b_res[3].start = 0;
940934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
9419e8bf93aSRam Pai 		if (realloc_head)
9429e8bf93aSRam Pai 			add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */);
9431da177e4SLinus Torvalds 	} else {
944934b7024SLinus Torvalds 		b_res[3].start = 0;
945934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
9469e8bf93aSRam Pai 		if (realloc_head)
9479e8bf93aSRam Pai 			add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size * 2, 0 /* dont care */);
9481da177e4SLinus Torvalds 	}
9490a2daa1cSRam Pai 
9500a2daa1cSRam Pai 	/* set the size of the resource to zero, so that the resource does not
9510a2daa1cSRam Pai 	 * get assigned during required-resource allocation cycle but gets assigned
9520a2daa1cSRam Pai 	 * during the optional-resource allocation cycle.
9530a2daa1cSRam Pai  	 */
9540a2daa1cSRam Pai 	b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1;
9550a2daa1cSRam Pai 	b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0;
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
958c8adf9a3SRam Pai void __ref __pci_bus_size_bridges(struct pci_bus *bus,
959bdc4abecSYinghai Lu 			struct list_head *realloc_head)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	struct pci_dev *dev;
9621da177e4SLinus Torvalds 	unsigned long mask, prefmask;
963c8adf9a3SRam Pai 	resource_size_t additional_mem_size = 0, additional_io_size = 0;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
9661da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
9671da177e4SLinus Torvalds 		if (!b)
9681da177e4SLinus Torvalds 			continue;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 		switch (dev->class >> 8) {
9711da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
9729e8bf93aSRam Pai 			pci_bus_size_cardbus(b, realloc_head);
9731da177e4SLinus Torvalds 			break;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
9761da177e4SLinus Torvalds 		default:
9779e8bf93aSRam Pai 			__pci_bus_size_bridges(b, realloc_head);
9781da177e4SLinus Torvalds 			break;
9791da177e4SLinus Torvalds 		}
9801da177e4SLinus Torvalds 	}
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	/* The root bus? */
9831da177e4SLinus Torvalds 	if (!bus->self)
9841da177e4SLinus Torvalds 		return;
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	switch (bus->self->class >> 8) {
9871da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_CARDBUS:
9881da177e4SLinus Torvalds 		/* don't size cardbuses yet. */
9891da177e4SLinus Torvalds 		break;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_PCI:
9921da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
99328760489SEric W. Biederman 		if (bus->self->is_hotplug_bridge) {
994c8adf9a3SRam Pai 			additional_io_size  = pci_hotplug_io_size;
995c8adf9a3SRam Pai 			additional_mem_size = pci_hotplug_mem_size;
99628760489SEric W. Biederman 		}
997c8adf9a3SRam Pai 		/*
998c8adf9a3SRam Pai 		 * Follow thru
999c8adf9a3SRam Pai 		 */
10001da177e4SLinus Torvalds 	default:
100119aa7ee4SYinghai Lu 		pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
100219aa7ee4SYinghai Lu 			     additional_io_size, realloc_head);
10031da177e4SLinus Torvalds 		/* If the bridge supports prefetchable range, size it
10041da177e4SLinus Torvalds 		   separately. If it doesn't, or its prefetchable window
10051da177e4SLinus Torvalds 		   has already been allocated by arch code, try
10061da177e4SLinus Torvalds 		   non-prefetchable range for both types of PCI memory
10071da177e4SLinus Torvalds 		   resources. */
10081da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
10091da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
101019aa7ee4SYinghai Lu 		if (pbus_size_mem(bus, prefmask, prefmask,
101119aa7ee4SYinghai Lu 				  realloc_head ? 0 : additional_mem_size,
101219aa7ee4SYinghai Lu 				  additional_mem_size, realloc_head))
10131da177e4SLinus Torvalds 			mask = prefmask; /* Success, size non-prefetch only. */
101428760489SEric W. Biederman 		else
1015c8adf9a3SRam Pai 			additional_mem_size += additional_mem_size;
101619aa7ee4SYinghai Lu 		pbus_size_mem(bus, mask, IORESOURCE_MEM,
101719aa7ee4SYinghai Lu 				realloc_head ? 0 : additional_mem_size,
101819aa7ee4SYinghai Lu 				additional_mem_size, realloc_head);
10191da177e4SLinus Torvalds 		break;
10201da177e4SLinus Torvalds 	}
10211da177e4SLinus Torvalds }
1022c8adf9a3SRam Pai 
1023c8adf9a3SRam Pai void __ref pci_bus_size_bridges(struct pci_bus *bus)
1024c8adf9a3SRam Pai {
1025c8adf9a3SRam Pai 	__pci_bus_size_bridges(bus, NULL);
1026c8adf9a3SRam Pai }
10271da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
10281da177e4SLinus Torvalds 
1029568ddef8SYinghai Lu static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
1030bdc4abecSYinghai Lu 					 struct list_head *realloc_head,
1031bdc4abecSYinghai Lu 					 struct list_head *fail_head)
10321da177e4SLinus Torvalds {
10331da177e4SLinus Torvalds 	struct pci_bus *b;
10341da177e4SLinus Torvalds 	struct pci_dev *dev;
10351da177e4SLinus Torvalds 
10369e8bf93aSRam Pai 	pbus_assign_resources_sorted(bus, realloc_head, fail_head);
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
10391da177e4SLinus Torvalds 		b = dev->subordinate;
10401da177e4SLinus Torvalds 		if (!b)
10411da177e4SLinus Torvalds 			continue;
10421da177e4SLinus Torvalds 
10439e8bf93aSRam Pai 		__pci_bus_assign_resources(b, realloc_head, fail_head);
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 		switch (dev->class >> 8) {
10461da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
10476841ec68SYinghai Lu 			if (!pci_is_enabled(dev))
10481da177e4SLinus Torvalds 				pci_setup_bridge(b);
10491da177e4SLinus Torvalds 			break;
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
10521da177e4SLinus Torvalds 			pci_setup_cardbus(b);
10531da177e4SLinus Torvalds 			break;
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 		default:
105680ccba11SBjorn Helgaas 			dev_info(&dev->dev, "not setting up bridge for bus "
105780ccba11SBjorn Helgaas 				 "%04x:%02x\n", pci_domain_nr(b), b->number);
10581da177e4SLinus Torvalds 			break;
10591da177e4SLinus Torvalds 		}
10601da177e4SLinus Torvalds 	}
10611da177e4SLinus Torvalds }
1062568ddef8SYinghai Lu 
1063568ddef8SYinghai Lu void __ref pci_bus_assign_resources(const struct pci_bus *bus)
1064568ddef8SYinghai Lu {
1065c8adf9a3SRam Pai 	__pci_bus_assign_resources(bus, NULL, NULL);
1066568ddef8SYinghai Lu }
10671da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
10681da177e4SLinus Torvalds 
10696841ec68SYinghai Lu static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
1070bdc4abecSYinghai Lu 					 struct list_head *add_head,
1071bdc4abecSYinghai Lu 					 struct list_head *fail_head)
10726841ec68SYinghai Lu {
10736841ec68SYinghai Lu 	struct pci_bus *b;
10746841ec68SYinghai Lu 
10758424d759SYinghai Lu 	pdev_assign_resources_sorted((struct pci_dev *)bridge,
10768424d759SYinghai Lu 					 add_head, fail_head);
10776841ec68SYinghai Lu 
10786841ec68SYinghai Lu 	b = bridge->subordinate;
10796841ec68SYinghai Lu 	if (!b)
10806841ec68SYinghai Lu 		return;
10816841ec68SYinghai Lu 
10828424d759SYinghai Lu 	__pci_bus_assign_resources(b, add_head, fail_head);
10836841ec68SYinghai Lu 
10846841ec68SYinghai Lu 	switch (bridge->class >> 8) {
10856841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_PCI:
10866841ec68SYinghai Lu 		pci_setup_bridge(b);
10876841ec68SYinghai Lu 		break;
10886841ec68SYinghai Lu 
10896841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_CARDBUS:
10906841ec68SYinghai Lu 		pci_setup_cardbus(b);
10916841ec68SYinghai Lu 		break;
10926841ec68SYinghai Lu 
10936841ec68SYinghai Lu 	default:
10946841ec68SYinghai Lu 		dev_info(&bridge->dev, "not setting up bridge for bus "
10956841ec68SYinghai Lu 			 "%04x:%02x\n", pci_domain_nr(b), b->number);
10966841ec68SYinghai Lu 		break;
10976841ec68SYinghai Lu 	}
10986841ec68SYinghai Lu }
10995009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus,
11005009b460SYinghai Lu 					  unsigned long type)
11015009b460SYinghai Lu {
11025009b460SYinghai Lu 	int idx;
11035009b460SYinghai Lu 	bool changed = false;
11045009b460SYinghai Lu 	struct pci_dev *dev;
11055009b460SYinghai Lu 	struct resource *r;
11065009b460SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
11075009b460SYinghai Lu 				  IORESOURCE_PREFETCH;
11085009b460SYinghai Lu 
11095009b460SYinghai Lu 	dev = bus->self;
11105009b460SYinghai Lu 	for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
11115009b460SYinghai Lu 	     idx++) {
11125009b460SYinghai Lu 		r = &dev->resource[idx];
11135009b460SYinghai Lu 		if ((r->flags & type_mask) != type)
11145009b460SYinghai Lu 			continue;
11155009b460SYinghai Lu 		if (!r->parent)
11165009b460SYinghai Lu 			continue;
11175009b460SYinghai Lu 		/*
11185009b460SYinghai Lu 		 * if there are children under that, we should release them
11195009b460SYinghai Lu 		 *  all
11205009b460SYinghai Lu 		 */
11215009b460SYinghai Lu 		release_child_resources(r);
11225009b460SYinghai Lu 		if (!release_resource(r)) {
11235009b460SYinghai Lu 			dev_printk(KERN_DEBUG, &dev->dev,
11245009b460SYinghai Lu 				 "resource %d %pR released\n", idx, r);
11255009b460SYinghai Lu 			/* keep the old size */
11265009b460SYinghai Lu 			r->end = resource_size(r) - 1;
11275009b460SYinghai Lu 			r->start = 0;
11285009b460SYinghai Lu 			r->flags = 0;
11295009b460SYinghai Lu 			changed = true;
11305009b460SYinghai Lu 		}
11315009b460SYinghai Lu 	}
11325009b460SYinghai Lu 
11335009b460SYinghai Lu 	if (changed) {
11345009b460SYinghai Lu 		/* avoiding touch the one without PREF */
11355009b460SYinghai Lu 		if (type & IORESOURCE_PREFETCH)
11365009b460SYinghai Lu 			type = IORESOURCE_PREFETCH;
11375009b460SYinghai Lu 		__pci_setup_bridge(bus, type);
11385009b460SYinghai Lu 	}
11395009b460SYinghai Lu }
11405009b460SYinghai Lu 
11415009b460SYinghai Lu enum release_type {
11425009b460SYinghai Lu 	leaf_only,
11435009b460SYinghai Lu 	whole_subtree,
11445009b460SYinghai Lu };
11455009b460SYinghai Lu /*
11465009b460SYinghai Lu  * try to release pci bridge resources that is from leaf bridge,
11475009b460SYinghai Lu  * so we can allocate big new one later
11485009b460SYinghai Lu  */
11495009b460SYinghai Lu static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
11505009b460SYinghai Lu 						   unsigned long type,
11515009b460SYinghai Lu 						   enum release_type rel_type)
11525009b460SYinghai Lu {
11535009b460SYinghai Lu 	struct pci_dev *dev;
11545009b460SYinghai Lu 	bool is_leaf_bridge = true;
11555009b460SYinghai Lu 
11565009b460SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
11575009b460SYinghai Lu 		struct pci_bus *b = dev->subordinate;
11585009b460SYinghai Lu 		if (!b)
11595009b460SYinghai Lu 			continue;
11605009b460SYinghai Lu 
11615009b460SYinghai Lu 		is_leaf_bridge = false;
11625009b460SYinghai Lu 
11635009b460SYinghai Lu 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
11645009b460SYinghai Lu 			continue;
11655009b460SYinghai Lu 
11665009b460SYinghai Lu 		if (rel_type == whole_subtree)
11675009b460SYinghai Lu 			pci_bus_release_bridge_resources(b, type,
11685009b460SYinghai Lu 						 whole_subtree);
11695009b460SYinghai Lu 	}
11705009b460SYinghai Lu 
11715009b460SYinghai Lu 	if (pci_is_root_bus(bus))
11725009b460SYinghai Lu 		return;
11735009b460SYinghai Lu 
11745009b460SYinghai Lu 	if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
11755009b460SYinghai Lu 		return;
11765009b460SYinghai Lu 
11775009b460SYinghai Lu 	if ((rel_type == whole_subtree) || is_leaf_bridge)
11785009b460SYinghai Lu 		pci_bridge_release_resources(bus, type);
11795009b460SYinghai Lu }
11805009b460SYinghai Lu 
118176fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
118276fbc263SYinghai Lu {
118389a74eccSBjorn Helgaas 	struct resource *res;
118476fbc263SYinghai Lu 	int i;
118576fbc263SYinghai Lu 
118689a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
11877c9342b8SYinghai Lu 		if (!res || !res->end || !res->flags)
118876fbc263SYinghai Lu                         continue;
118976fbc263SYinghai Lu 
1190c7dabef8SBjorn Helgaas 		dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
119176fbc263SYinghai Lu         }
119276fbc263SYinghai Lu }
119376fbc263SYinghai Lu 
119476fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
119576fbc263SYinghai Lu {
119676fbc263SYinghai Lu 	struct pci_bus *b;
119776fbc263SYinghai Lu 	struct pci_dev *dev;
119876fbc263SYinghai Lu 
119976fbc263SYinghai Lu 
120076fbc263SYinghai Lu 	pci_bus_dump_res(bus);
120176fbc263SYinghai Lu 
120276fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
120376fbc263SYinghai Lu 		b = dev->subordinate;
120476fbc263SYinghai Lu 		if (!b)
120576fbc263SYinghai Lu 			continue;
120676fbc263SYinghai Lu 
120776fbc263SYinghai Lu 		pci_bus_dump_resources(b);
120876fbc263SYinghai Lu 	}
120976fbc263SYinghai Lu }
121076fbc263SYinghai Lu 
1211da7822e5SYinghai Lu static int __init pci_bus_get_depth(struct pci_bus *bus)
1212da7822e5SYinghai Lu {
1213da7822e5SYinghai Lu 	int depth = 0;
1214da7822e5SYinghai Lu 	struct pci_dev *dev;
1215da7822e5SYinghai Lu 
1216da7822e5SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
1217da7822e5SYinghai Lu 		int ret;
1218da7822e5SYinghai Lu 		struct pci_bus *b = dev->subordinate;
1219da7822e5SYinghai Lu 		if (!b)
1220da7822e5SYinghai Lu 			continue;
1221da7822e5SYinghai Lu 
1222da7822e5SYinghai Lu 		ret = pci_bus_get_depth(b);
1223da7822e5SYinghai Lu 		if (ret + 1 > depth)
1224da7822e5SYinghai Lu 			depth = ret + 1;
1225da7822e5SYinghai Lu 	}
1226da7822e5SYinghai Lu 
1227da7822e5SYinghai Lu 	return depth;
1228da7822e5SYinghai Lu }
1229da7822e5SYinghai Lu static int __init pci_get_max_depth(void)
1230da7822e5SYinghai Lu {
1231da7822e5SYinghai Lu 	int depth = 0;
1232da7822e5SYinghai Lu 	struct pci_bus *bus;
1233da7822e5SYinghai Lu 
1234da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node) {
1235da7822e5SYinghai Lu 		int ret;
1236da7822e5SYinghai Lu 
1237da7822e5SYinghai Lu 		ret = pci_bus_get_depth(bus);
1238da7822e5SYinghai Lu 		if (ret > depth)
1239da7822e5SYinghai Lu 			depth = ret;
1240da7822e5SYinghai Lu 	}
1241da7822e5SYinghai Lu 
1242da7822e5SYinghai Lu 	return depth;
1243da7822e5SYinghai Lu }
1244da7822e5SYinghai Lu 
1245f483d392SRam Pai 
1246da7822e5SYinghai Lu /*
1247da7822e5SYinghai Lu  * first try will not touch pci bridge res
1248da7822e5SYinghai Lu  * second  and later try will clear small leaf bridge res
1249da7822e5SYinghai Lu  * will stop till to the max  deepth if can not find good one
1250da7822e5SYinghai Lu  */
12511da177e4SLinus Torvalds void __init
12521da177e4SLinus Torvalds pci_assign_unassigned_resources(void)
12531da177e4SLinus Torvalds {
12541da177e4SLinus Torvalds 	struct pci_bus *bus;
1255bdc4abecSYinghai Lu 	LIST_HEAD(realloc_head); /* list of resources that
1256c8adf9a3SRam Pai 					want additional resources */
1257bdc4abecSYinghai Lu 	struct list_head *add_list = NULL;
1258da7822e5SYinghai Lu 	int tried_times = 0;
1259da7822e5SYinghai Lu 	enum release_type rel_type = leaf_only;
1260bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
1261b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
1262da7822e5SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1263da7822e5SYinghai Lu 				  IORESOURCE_PREFETCH;
1264da7822e5SYinghai Lu 	unsigned long failed_type;
126519aa7ee4SYinghai Lu 	int pci_try_num = 1;
1266da7822e5SYinghai Lu 
126719aa7ee4SYinghai Lu 	/* don't realloc if asked to do so */
126819aa7ee4SYinghai Lu 	if (pci_realloc_enabled()) {
126919aa7ee4SYinghai Lu 		int max_depth = pci_get_max_depth();
127019aa7ee4SYinghai Lu 
1271da7822e5SYinghai Lu 		pci_try_num = max_depth + 1;
1272da7822e5SYinghai Lu 		printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1273da7822e5SYinghai Lu 			 max_depth, pci_try_num);
127419aa7ee4SYinghai Lu 	}
1275da7822e5SYinghai Lu 
1276da7822e5SYinghai Lu again:
127719aa7ee4SYinghai Lu 	/*
127819aa7ee4SYinghai Lu 	 * last try will use add_list, otherwise will try good to have as
127919aa7ee4SYinghai Lu 	 * must have, so can realloc parent bridge resource
128019aa7ee4SYinghai Lu 	 */
128119aa7ee4SYinghai Lu 	if (tried_times + 1 == pci_try_num)
1282bdc4abecSYinghai Lu 		add_list = &realloc_head;
12831da177e4SLinus Torvalds 	/* Depth first, calculate sizes and alignments of all
12841da177e4SLinus Torvalds 	   subordinate buses. */
1285da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
128619aa7ee4SYinghai Lu 		__pci_bus_size_bridges(bus, add_list);
1287c8adf9a3SRam Pai 
12881da177e4SLinus Torvalds 	/* Depth last, allocate resources and update the hardware. */
1289da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
1290bdc4abecSYinghai Lu 		__pci_bus_assign_resources(bus, add_list, &fail_head);
129119aa7ee4SYinghai Lu 	if (add_list)
1292bdc4abecSYinghai Lu 		BUG_ON(!list_empty(add_list));
1293da7822e5SYinghai Lu 	tried_times++;
1294da7822e5SYinghai Lu 
1295da7822e5SYinghai Lu 	/* any device complain? */
1296bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
1297da7822e5SYinghai Lu 		goto enable_and_dump;
1298f483d392SRam Pai 
1299da7822e5SYinghai Lu 	failed_type = 0;
1300b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list)
1301b9b0bba9SYinghai Lu 		failed_type |= fail_res->flags;
1302bdc4abecSYinghai Lu 
1303da7822e5SYinghai Lu 	/*
1304da7822e5SYinghai Lu 	 * io port are tight, don't try extra
1305da7822e5SYinghai Lu 	 * or if reach the limit, don't want to try more
1306da7822e5SYinghai Lu 	 */
1307da7822e5SYinghai Lu 	failed_type &= type_mask;
1308da7822e5SYinghai Lu 	if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
1309bffc56d4SYinghai Lu 		free_list(&fail_head);
1310da7822e5SYinghai Lu 		goto enable_and_dump;
1311da7822e5SYinghai Lu 	}
1312da7822e5SYinghai Lu 
1313da7822e5SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1314da7822e5SYinghai Lu 			 tried_times + 1);
1315da7822e5SYinghai Lu 
1316da7822e5SYinghai Lu 	/* third times and later will not check if it is leaf */
1317da7822e5SYinghai Lu 	if ((tried_times + 1) > 2)
1318da7822e5SYinghai Lu 		rel_type = whole_subtree;
1319da7822e5SYinghai Lu 
1320da7822e5SYinghai Lu 	/*
1321da7822e5SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
1322da7822e5SYinghai Lu 	 * child device under that bridge
1323da7822e5SYinghai Lu 	 */
1324b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1325b9b0bba9SYinghai Lu 		bus = fail_res->dev->bus;
1326bdc4abecSYinghai Lu 		pci_bus_release_bridge_resources(bus,
1327b9b0bba9SYinghai Lu 						 fail_res->flags & type_mask,
1328da7822e5SYinghai Lu 						 rel_type);
1329da7822e5SYinghai Lu 	}
1330da7822e5SYinghai Lu 	/* restore size and flags */
1331b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1332b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
1333da7822e5SYinghai Lu 
1334b9b0bba9SYinghai Lu 		res->start = fail_res->start;
1335b9b0bba9SYinghai Lu 		res->end = fail_res->end;
1336b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
1337b9b0bba9SYinghai Lu 		if (fail_res->dev->subordinate)
1338da7822e5SYinghai Lu 			res->flags = 0;
1339da7822e5SYinghai Lu 	}
1340bffc56d4SYinghai Lu 	free_list(&fail_head);
1341da7822e5SYinghai Lu 
1342da7822e5SYinghai Lu 	goto again;
1343da7822e5SYinghai Lu 
1344da7822e5SYinghai Lu enable_and_dump:
1345da7822e5SYinghai Lu 	/* Depth last, update the hardware. */
1346da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
1347da7822e5SYinghai Lu 		pci_enable_bridges(bus);
134876fbc263SYinghai Lu 
134976fbc263SYinghai Lu 	/* dump the resource on buses */
1350da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
135176fbc263SYinghai Lu 		pci_bus_dump_resources(bus);
135276fbc263SYinghai Lu }
13536841ec68SYinghai Lu 
13546841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
13556841ec68SYinghai Lu {
13566841ec68SYinghai Lu 	struct pci_bus *parent = bridge->subordinate;
1357bdc4abecSYinghai Lu 	LIST_HEAD(add_list); /* list of resources that
13588424d759SYinghai Lu 					want additional resources */
135932180e40SYinghai Lu 	int tried_times = 0;
1360bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
1361b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
13626841ec68SYinghai Lu 	int retval;
136332180e40SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
136432180e40SYinghai Lu 				  IORESOURCE_PREFETCH;
13656841ec68SYinghai Lu 
136632180e40SYinghai Lu again:
13678424d759SYinghai Lu 	__pci_bus_size_bridges(parent, &add_list);
1368bdc4abecSYinghai Lu 	__pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1369bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
137032180e40SYinghai Lu 	tried_times++;
137132180e40SYinghai Lu 
1372bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
13733f579c34SYinghai Lu 		goto enable_all;
137432180e40SYinghai Lu 
137532180e40SYinghai Lu 	if (tried_times >= 2) {
137632180e40SYinghai Lu 		/* still fail, don't need to try more */
1377bffc56d4SYinghai Lu 		free_list(&fail_head);
13783f579c34SYinghai Lu 		goto enable_all;
137932180e40SYinghai Lu 	}
138032180e40SYinghai Lu 
138132180e40SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
138232180e40SYinghai Lu 			 tried_times + 1);
138332180e40SYinghai Lu 
138432180e40SYinghai Lu 	/*
138532180e40SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
138632180e40SYinghai Lu 	 * child device under that bridge
138732180e40SYinghai Lu 	 */
1388b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1389b9b0bba9SYinghai Lu 		struct pci_bus *bus = fail_res->dev->bus;
1390b9b0bba9SYinghai Lu 		unsigned long flags = fail_res->flags;
139132180e40SYinghai Lu 
139232180e40SYinghai Lu 		pci_bus_release_bridge_resources(bus, flags & type_mask,
139332180e40SYinghai Lu 						 whole_subtree);
139432180e40SYinghai Lu 	}
139532180e40SYinghai Lu 	/* restore size and flags */
1396b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1397b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
139832180e40SYinghai Lu 
1399b9b0bba9SYinghai Lu 		res->start = fail_res->start;
1400b9b0bba9SYinghai Lu 		res->end = fail_res->end;
1401b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
1402b9b0bba9SYinghai Lu 		if (fail_res->dev->subordinate)
140332180e40SYinghai Lu 			res->flags = 0;
140432180e40SYinghai Lu 	}
1405bffc56d4SYinghai Lu 	free_list(&fail_head);
140632180e40SYinghai Lu 
140732180e40SYinghai Lu 	goto again;
14083f579c34SYinghai Lu 
14093f579c34SYinghai Lu enable_all:
14103f579c34SYinghai Lu 	retval = pci_reenable_device(bridge);
14113f579c34SYinghai Lu 	pci_set_master(bridge);
14123f579c34SYinghai Lu 	pci_enable_bridges(parent);
14136841ec68SYinghai Lu }
14146841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
14159b03088fSYinghai Lu 
14169b03088fSYinghai Lu #ifdef CONFIG_HOTPLUG
14179b03088fSYinghai Lu /**
14189b03088fSYinghai Lu  * pci_rescan_bus - scan a PCI bus for devices.
14199b03088fSYinghai Lu  * @bus: PCI bus to scan
14209b03088fSYinghai Lu  *
14219b03088fSYinghai Lu  * Scan a PCI bus and child buses for new devices, adds them,
14229b03088fSYinghai Lu  * and enables them.
14239b03088fSYinghai Lu  *
14249b03088fSYinghai Lu  * Returns the max number of subordinate bus discovered.
14259b03088fSYinghai Lu  */
14269b03088fSYinghai Lu unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
14279b03088fSYinghai Lu {
14289b03088fSYinghai Lu 	unsigned int max;
14299b03088fSYinghai Lu 	struct pci_dev *dev;
1430bdc4abecSYinghai Lu 	LIST_HEAD(add_list); /* list of resources that
14319b03088fSYinghai Lu 					want additional resources */
14329b03088fSYinghai Lu 
14339b03088fSYinghai Lu 	max = pci_scan_child_bus(bus);
14349b03088fSYinghai Lu 
14359b03088fSYinghai Lu 	down_read(&pci_bus_sem);
14369b03088fSYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
14379b03088fSYinghai Lu 		if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
14389b03088fSYinghai Lu 		    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
14399b03088fSYinghai Lu 			if (dev->subordinate)
14409b03088fSYinghai Lu 				__pci_bus_size_bridges(dev->subordinate,
14419b03088fSYinghai Lu 							 &add_list);
14429b03088fSYinghai Lu 	up_read(&pci_bus_sem);
14439b03088fSYinghai Lu 	__pci_bus_assign_resources(bus, &add_list, NULL);
1444bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
14459b03088fSYinghai Lu 
14469b03088fSYinghai Lu 	pci_enable_bridges(bus);
14479b03088fSYinghai Lu 	pci_bus_add_devices(bus);
14489b03088fSYinghai Lu 
14499b03088fSYinghai Lu 	return max;
14509b03088fSYinghai Lu }
14519b03088fSYinghai Lu EXPORT_SYMBOL_GPL(pci_rescan_bus);
14529b03088fSYinghai Lu #endif
1453