xref: /openbmc/linux/drivers/pci/setup-bus.c (revision 3f2f4dc456e9f80849b99d79600a7257690ca4b1)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	drivers/pci/setup-bus.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Extruded from code written by
51da177e4SLinus Torvalds  *      Dave Rusling (david.rusling@reo.mts.dec.com)
61da177e4SLinus Torvalds  *      David Mosberger (davidm@cs.arizona.edu)
71da177e4SLinus Torvalds  *	David Miller (davem@redhat.com)
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Support routines for initializing a PCI subsystem.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds /*
131da177e4SLinus Torvalds  * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
141da177e4SLinus Torvalds  *	     PCI-PCI bridges cleanup, sorted resource allocation.
151da177e4SLinus Torvalds  * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
161da177e4SLinus Torvalds  *	     Converted to allocation in 3 passes, which gives
171da177e4SLinus Torvalds  *	     tighter packing. Prefetchable range support.
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/init.h>
211da177e4SLinus Torvalds #include <linux/kernel.h>
221da177e4SLinus Torvalds #include <linux/module.h>
231da177e4SLinus Torvalds #include <linux/pci.h>
241da177e4SLinus Torvalds #include <linux/errno.h>
251da177e4SLinus Torvalds #include <linux/ioport.h>
261da177e4SLinus Torvalds #include <linux/cache.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
2847087700SBjorn Helgaas #include <asm-generic/pci-bridge.h>
296faf17f6SChris Wright #include "pci.h"
301da177e4SLinus Torvalds 
31844393f4SBjorn Helgaas unsigned int pci_flags;
3247087700SBjorn Helgaas 
33bdc4abecSYinghai Lu struct pci_dev_resource {
34bdc4abecSYinghai Lu 	struct list_head list;
352934a0deSYinghai Lu 	struct resource *res;
362934a0deSYinghai Lu 	struct pci_dev *dev;
37568ddef8SYinghai Lu 	resource_size_t start;
38568ddef8SYinghai Lu 	resource_size_t end;
39c8adf9a3SRam Pai 	resource_size_t add_size;
402bbc6942SRam Pai 	resource_size_t min_align;
41568ddef8SYinghai Lu 	unsigned long flags;
42568ddef8SYinghai Lu };
43568ddef8SYinghai Lu 
44bffc56d4SYinghai Lu static void free_list(struct list_head *head)
45bffc56d4SYinghai Lu {
46bffc56d4SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
47bffc56d4SYinghai Lu 
48bffc56d4SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
49bffc56d4SYinghai Lu 		list_del(&dev_res->list);
50bffc56d4SYinghai Lu 		kfree(dev_res);
51bffc56d4SYinghai Lu 	}
52bffc56d4SYinghai Lu }
53094732a5SRam Pai 
54c8adf9a3SRam Pai /**
55c8adf9a3SRam Pai  * add_to_list() - add a new resource tracker to the list
56c8adf9a3SRam Pai  * @head:	Head of the list
57c8adf9a3SRam Pai  * @dev:	device corresponding to which the resource
58c8adf9a3SRam Pai  *		belongs
59c8adf9a3SRam Pai  * @res:	The resource to be tracked
60c8adf9a3SRam Pai  * @add_size:	additional size to be optionally added
61c8adf9a3SRam Pai  *              to the resource
62c8adf9a3SRam Pai  */
63bdc4abecSYinghai Lu static int add_to_list(struct list_head *head,
64c8adf9a3SRam Pai 		 struct pci_dev *dev, struct resource *res,
652bbc6942SRam Pai 		 resource_size_t add_size, resource_size_t min_align)
66568ddef8SYinghai Lu {
67764242a0SYinghai Lu 	struct pci_dev_resource *tmp;
68568ddef8SYinghai Lu 
69bdc4abecSYinghai Lu 	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
70568ddef8SYinghai Lu 	if (!tmp) {
713c78bc61SRyan Desfosses 		pr_warn("add_to_list: kmalloc() failed!\n");
72ef62dfefSYinghai Lu 		return -ENOMEM;
73568ddef8SYinghai Lu 	}
74568ddef8SYinghai Lu 
75568ddef8SYinghai Lu 	tmp->res = res;
76568ddef8SYinghai Lu 	tmp->dev = dev;
77568ddef8SYinghai Lu 	tmp->start = res->start;
78568ddef8SYinghai Lu 	tmp->end = res->end;
79568ddef8SYinghai Lu 	tmp->flags = res->flags;
80c8adf9a3SRam Pai 	tmp->add_size = add_size;
812bbc6942SRam Pai 	tmp->min_align = min_align;
82bdc4abecSYinghai Lu 
83bdc4abecSYinghai Lu 	list_add(&tmp->list, head);
84ef62dfefSYinghai Lu 
85ef62dfefSYinghai Lu 	return 0;
86568ddef8SYinghai Lu }
87568ddef8SYinghai Lu 
88b9b0bba9SYinghai Lu static void remove_from_list(struct list_head *head,
893e6e0d80SYinghai Lu 				 struct resource *res)
903e6e0d80SYinghai Lu {
91b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
923e6e0d80SYinghai Lu 
93b9b0bba9SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
94b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
95b9b0bba9SYinghai Lu 			list_del(&dev_res->list);
96b9b0bba9SYinghai Lu 			kfree(dev_res);
97bdc4abecSYinghai Lu 			break;
983e6e0d80SYinghai Lu 		}
993e6e0d80SYinghai Lu 	}
1003e6e0d80SYinghai Lu }
1013e6e0d80SYinghai Lu 
102b9b0bba9SYinghai Lu static resource_size_t get_res_add_size(struct list_head *head,
1031c372353SYinghai Lu 					struct resource *res)
1041c372353SYinghai Lu {
105b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res;
1061c372353SYinghai Lu 
107b9b0bba9SYinghai Lu 	list_for_each_entry(dev_res, head, list) {
108b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
109b592443dSYinghai Lu 			int idx = res - &dev_res->dev->resource[0];
110b592443dSYinghai Lu 
111b9b0bba9SYinghai Lu 			dev_printk(KERN_DEBUG, &dev_res->dev->dev,
112b592443dSYinghai Lu 				 "res[%d]=%pR get_res_add_size add_size %llx\n",
113b592443dSYinghai Lu 				 idx, dev_res->res,
114b9b0bba9SYinghai Lu 				 (unsigned long long)dev_res->add_size);
115b592443dSYinghai Lu 
116b9b0bba9SYinghai Lu 			return dev_res->add_size;
117bdc4abecSYinghai Lu 		}
1183e6e0d80SYinghai Lu 	}
1191c372353SYinghai Lu 
1201c372353SYinghai Lu 	return 0;
1211c372353SYinghai Lu }
1221c372353SYinghai Lu 
12378c3b329SYinghai Lu /* Sort resources by alignment */
124bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
12578c3b329SYinghai Lu {
12678c3b329SYinghai Lu 	int i;
12778c3b329SYinghai Lu 
12878c3b329SYinghai Lu 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
12978c3b329SYinghai Lu 		struct resource *r;
130bdc4abecSYinghai Lu 		struct pci_dev_resource *dev_res, *tmp;
13178c3b329SYinghai Lu 		resource_size_t r_align;
132bdc4abecSYinghai Lu 		struct list_head *n;
13378c3b329SYinghai Lu 
13478c3b329SYinghai Lu 		r = &dev->resource[i];
13578c3b329SYinghai Lu 
13678c3b329SYinghai Lu 		if (r->flags & IORESOURCE_PCI_FIXED)
13778c3b329SYinghai Lu 			continue;
13878c3b329SYinghai Lu 
13978c3b329SYinghai Lu 		if (!(r->flags) || r->parent)
14078c3b329SYinghai Lu 			continue;
14178c3b329SYinghai Lu 
14278c3b329SYinghai Lu 		r_align = pci_resource_alignment(dev, r);
14378c3b329SYinghai Lu 		if (!r_align) {
14478c3b329SYinghai Lu 			dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
14578c3b329SYinghai Lu 				 i, r);
14678c3b329SYinghai Lu 			continue;
14778c3b329SYinghai Lu 		}
14878c3b329SYinghai Lu 
149bdc4abecSYinghai Lu 		tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
15078c3b329SYinghai Lu 		if (!tmp)
151227f0647SRyan Desfosses 			panic("pdev_sort_resources(): kmalloc() failed!\n");
15278c3b329SYinghai Lu 		tmp->res = r;
15378c3b329SYinghai Lu 		tmp->dev = dev;
154bdc4abecSYinghai Lu 
155bdc4abecSYinghai Lu 		/* fallback is smallest one or list is empty*/
156bdc4abecSYinghai Lu 		n = head;
157bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
158bdc4abecSYinghai Lu 			resource_size_t align;
159bdc4abecSYinghai Lu 
160bdc4abecSYinghai Lu 			align = pci_resource_alignment(dev_res->dev,
161bdc4abecSYinghai Lu 							 dev_res->res);
162bdc4abecSYinghai Lu 
163bdc4abecSYinghai Lu 			if (r_align > align) {
164bdc4abecSYinghai Lu 				n = &dev_res->list;
16578c3b329SYinghai Lu 				break;
16678c3b329SYinghai Lu 			}
16778c3b329SYinghai Lu 		}
168bdc4abecSYinghai Lu 		/* Insert it just before n*/
169bdc4abecSYinghai Lu 		list_add_tail(&tmp->list, n);
17078c3b329SYinghai Lu 	}
17178c3b329SYinghai Lu }
17278c3b329SYinghai Lu 
1736841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev,
174bdc4abecSYinghai Lu 				 struct list_head *head)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	u16 class = dev->class >> 8;
1771da177e4SLinus Torvalds 
1789bded00bSKenji Kaneshige 	/* Don't touch classless devices or host bridges or ioapics.  */
1796841ec68SYinghai Lu 	if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
1806841ec68SYinghai Lu 		return;
1811da177e4SLinus Torvalds 
1829bded00bSKenji Kaneshige 	/* Don't touch ioapic devices already enabled by firmware */
18323186279SSatoru Takeuchi 	if (class == PCI_CLASS_SYSTEM_PIC) {
1849bded00bSKenji Kaneshige 		u16 command;
1859bded00bSKenji Kaneshige 		pci_read_config_word(dev, PCI_COMMAND, &command);
1869bded00bSKenji Kaneshige 		if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
1876841ec68SYinghai Lu 			return;
18823186279SSatoru Takeuchi 	}
18923186279SSatoru Takeuchi 
1906841ec68SYinghai Lu 	pdev_sort_resources(dev, head);
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
193fc075e1dSRam Pai static inline void reset_resource(struct resource *res)
194fc075e1dSRam Pai {
195fc075e1dSRam Pai 	res->start = 0;
196fc075e1dSRam Pai 	res->end = 0;
197fc075e1dSRam Pai 	res->flags = 0;
198fc075e1dSRam Pai }
199fc075e1dSRam Pai 
200c8adf9a3SRam Pai /**
2019e8bf93aSRam Pai  * reassign_resources_sorted() - satisfy any additional resource requests
202c8adf9a3SRam Pai  *
2039e8bf93aSRam Pai  * @realloc_head : head of the list tracking requests requiring additional
204c8adf9a3SRam Pai  *             resources
205c8adf9a3SRam Pai  * @head     : head of the list tracking requests with allocated
206c8adf9a3SRam Pai  *             resources
207c8adf9a3SRam Pai  *
2089e8bf93aSRam Pai  * Walk through each element of the realloc_head and try to procure
209c8adf9a3SRam Pai  * additional resources for the element, provided the element
210c8adf9a3SRam Pai  * is in the head list.
211c8adf9a3SRam Pai  */
212bdc4abecSYinghai Lu static void reassign_resources_sorted(struct list_head *realloc_head,
213bdc4abecSYinghai Lu 		struct list_head *head)
214c8adf9a3SRam Pai {
215c8adf9a3SRam Pai 	struct resource *res;
216b9b0bba9SYinghai Lu 	struct pci_dev_resource *add_res, *tmp;
217bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
218c8adf9a3SRam Pai 	resource_size_t add_size;
219c8adf9a3SRam Pai 	int idx;
220c8adf9a3SRam Pai 
221b9b0bba9SYinghai Lu 	list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
222bdc4abecSYinghai Lu 		bool found_match = false;
223bdc4abecSYinghai Lu 
224b9b0bba9SYinghai Lu 		res = add_res->res;
225c8adf9a3SRam Pai 		/* skip resource that has been reset */
226c8adf9a3SRam Pai 		if (!res->flags)
227c8adf9a3SRam Pai 			goto out;
228c8adf9a3SRam Pai 
229c8adf9a3SRam Pai 		/* skip this resource if not found in head list */
230bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
231bdc4abecSYinghai Lu 			if (dev_res->res == res) {
232bdc4abecSYinghai Lu 				found_match = true;
233bdc4abecSYinghai Lu 				break;
234c8adf9a3SRam Pai 			}
235bdc4abecSYinghai Lu 		}
236bdc4abecSYinghai Lu 		if (!found_match)/* just skip */
237bdc4abecSYinghai Lu 			continue;
238c8adf9a3SRam Pai 
239b9b0bba9SYinghai Lu 		idx = res - &add_res->dev->resource[0];
240b9b0bba9SYinghai Lu 		add_size = add_res->add_size;
2412bbc6942SRam Pai 		if (!resource_size(res)) {
242b9b0bba9SYinghai Lu 			res->start = add_res->start;
243c8adf9a3SRam Pai 			res->end = res->start + add_size - 1;
244b9b0bba9SYinghai Lu 			if (pci_assign_resource(add_res->dev, idx))
245c8adf9a3SRam Pai 				reset_resource(res);
2462bbc6942SRam Pai 		} else {
247b9b0bba9SYinghai Lu 			resource_size_t align = add_res->min_align;
248b9b0bba9SYinghai Lu 			res->flags |= add_res->flags &
249bdc4abecSYinghai Lu 				 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
250b9b0bba9SYinghai Lu 			if (pci_reassign_resource(add_res->dev, idx,
251bdc4abecSYinghai Lu 						  add_size, align))
252b9b0bba9SYinghai Lu 				dev_printk(KERN_DEBUG, &add_res->dev->dev,
253b592443dSYinghai Lu 					   "failed to add %llx res[%d]=%pR\n",
254b592443dSYinghai Lu 					   (unsigned long long)add_size,
255b592443dSYinghai Lu 					   idx, res);
256c8adf9a3SRam Pai 		}
257c8adf9a3SRam Pai out:
258b9b0bba9SYinghai Lu 		list_del(&add_res->list);
259b9b0bba9SYinghai Lu 		kfree(add_res);
260c8adf9a3SRam Pai 	}
261c8adf9a3SRam Pai }
262c8adf9a3SRam Pai 
263c8adf9a3SRam Pai /**
264c8adf9a3SRam Pai  * assign_requested_resources_sorted() - satisfy resource requests
265c8adf9a3SRam Pai  *
266c8adf9a3SRam Pai  * @head : head of the list tracking requests for resources
2678356aad4SWanpeng Li  * @fail_head : head of the list tracking requests that could
268c8adf9a3SRam Pai  *		not be allocated
269c8adf9a3SRam Pai  *
270c8adf9a3SRam Pai  * Satisfy resource requests of each element in the list. Add
271c8adf9a3SRam Pai  * requests that could not satisfied to the failed_list.
272c8adf9a3SRam Pai  */
273bdc4abecSYinghai Lu static void assign_requested_resources_sorted(struct list_head *head,
274bdc4abecSYinghai Lu 				 struct list_head *fail_head)
2756841ec68SYinghai Lu {
2766841ec68SYinghai Lu 	struct resource *res;
277bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
2786841ec68SYinghai Lu 	int idx;
2796841ec68SYinghai Lu 
280bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
281bdc4abecSYinghai Lu 		res = dev_res->res;
282bdc4abecSYinghai Lu 		idx = res - &dev_res->dev->resource[0];
283bdc4abecSYinghai Lu 		if (resource_size(res) &&
284bdc4abecSYinghai Lu 		    pci_assign_resource(dev_res->dev, idx)) {
285a3cb999dSYinghai Lu 			if (fail_head) {
2869a928660SYinghai Lu 				/*
2879a928660SYinghai Lu 				 * if the failed res is for ROM BAR, and it will
2889a928660SYinghai Lu 				 * be enabled later, don't add it to the list
2899a928660SYinghai Lu 				 */
2909a928660SYinghai Lu 				if (!((idx == PCI_ROM_RESOURCE) &&
2919a928660SYinghai Lu 				      (!(res->flags & IORESOURCE_ROM_ENABLE))))
29267cc7e26SYinghai Lu 					add_to_list(fail_head,
29367cc7e26SYinghai Lu 						    dev_res->dev, res,
294f7625980SBjorn Helgaas 						    0 /* don't care */,
295f7625980SBjorn Helgaas 						    0 /* don't care */);
2969a928660SYinghai Lu 			}
297fc075e1dSRam Pai 			reset_resource(res);
298542df5deSRajesh Shah 		}
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
302aa914f5eSYinghai Lu static unsigned long pci_fail_res_type_mask(struct list_head *fail_head)
303aa914f5eSYinghai Lu {
304aa914f5eSYinghai Lu 	struct pci_dev_resource *fail_res;
305aa914f5eSYinghai Lu 	unsigned long mask = 0;
306aa914f5eSYinghai Lu 
307aa914f5eSYinghai Lu 	/* check failed type */
308aa914f5eSYinghai Lu 	list_for_each_entry(fail_res, fail_head, list)
309aa914f5eSYinghai Lu 		mask |= fail_res->flags;
310aa914f5eSYinghai Lu 
311aa914f5eSYinghai Lu 	/*
312aa914f5eSYinghai Lu 	 * one pref failed resource will set IORESOURCE_MEM,
313aa914f5eSYinghai Lu 	 * as we can allocate pref in non-pref range.
314aa914f5eSYinghai Lu 	 * Will release all assigned non-pref sibling resources
315aa914f5eSYinghai Lu 	 * according to that bit.
316aa914f5eSYinghai Lu 	 */
317aa914f5eSYinghai Lu 	return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
318aa914f5eSYinghai Lu }
319aa914f5eSYinghai Lu 
320aa914f5eSYinghai Lu static bool pci_need_to_release(unsigned long mask, struct resource *res)
321aa914f5eSYinghai Lu {
322aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_IO)
323aa914f5eSYinghai Lu 		return !!(mask & IORESOURCE_IO);
324aa914f5eSYinghai Lu 
325aa914f5eSYinghai Lu 	/* check pref at first */
326aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_PREFETCH) {
327aa914f5eSYinghai Lu 		if (mask & IORESOURCE_PREFETCH)
328aa914f5eSYinghai Lu 			return true;
329aa914f5eSYinghai Lu 		/* count pref if its parent is non-pref */
330aa914f5eSYinghai Lu 		else if ((mask & IORESOURCE_MEM) &&
331aa914f5eSYinghai Lu 			 !(res->parent->flags & IORESOURCE_PREFETCH))
332aa914f5eSYinghai Lu 			return true;
333aa914f5eSYinghai Lu 		else
334aa914f5eSYinghai Lu 			return false;
335aa914f5eSYinghai Lu 	}
336aa914f5eSYinghai Lu 
337aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_MEM)
338aa914f5eSYinghai Lu 		return !!(mask & IORESOURCE_MEM);
339aa914f5eSYinghai Lu 
340aa914f5eSYinghai Lu 	return false;	/* should not get here */
341aa914f5eSYinghai Lu }
342aa914f5eSYinghai Lu 
343bdc4abecSYinghai Lu static void __assign_resources_sorted(struct list_head *head,
344bdc4abecSYinghai Lu 				 struct list_head *realloc_head,
345bdc4abecSYinghai Lu 				 struct list_head *fail_head)
346c8adf9a3SRam Pai {
3473e6e0d80SYinghai Lu 	/*
3483e6e0d80SYinghai Lu 	 * Should not assign requested resources at first.
3493e6e0d80SYinghai Lu 	 *   they could be adjacent, so later reassign can not reallocate
3503e6e0d80SYinghai Lu 	 *   them one by one in parent resource window.
351367fa982SMasanari Iida 	 * Try to assign requested + add_size at beginning
3523e6e0d80SYinghai Lu 	 *  if could do that, could get out early.
3533e6e0d80SYinghai Lu 	 *  if could not do that, we still try to assign requested at first,
3543e6e0d80SYinghai Lu 	 *    then try to reassign add_size for some resources.
355aa914f5eSYinghai Lu 	 *
356aa914f5eSYinghai Lu 	 * Separate three resource type checking if we need to release
357aa914f5eSYinghai Lu 	 * assigned resource after requested + add_size try.
358aa914f5eSYinghai Lu 	 *	1. if there is io port assign fail, will release assigned
359aa914f5eSYinghai Lu 	 *	   io port.
360aa914f5eSYinghai Lu 	 *	2. if there is pref mmio assign fail, release assigned
361aa914f5eSYinghai Lu 	 *	   pref mmio.
362aa914f5eSYinghai Lu 	 *	   if assigned pref mmio's parent is non-pref mmio and there
363aa914f5eSYinghai Lu 	 *	   is non-pref mmio assign fail, will release that assigned
364aa914f5eSYinghai Lu 	 *	   pref mmio.
365aa914f5eSYinghai Lu 	 *	3. if there is non-pref mmio assign fail or pref mmio
366aa914f5eSYinghai Lu 	 *	   assigned fail, will release assigned non-pref mmio.
3673e6e0d80SYinghai Lu 	 */
368bdc4abecSYinghai Lu 	LIST_HEAD(save_head);
369bdc4abecSYinghai Lu 	LIST_HEAD(local_fail_head);
370b9b0bba9SYinghai Lu 	struct pci_dev_resource *save_res;
371aa914f5eSYinghai Lu 	struct pci_dev_resource *dev_res, *tmp_res;
372aa914f5eSYinghai Lu 	unsigned long fail_type;
3733e6e0d80SYinghai Lu 
3743e6e0d80SYinghai Lu 	/* Check if optional add_size is there */
375bdc4abecSYinghai Lu 	if (!realloc_head || list_empty(realloc_head))
3763e6e0d80SYinghai Lu 		goto requested_and_reassign;
3773e6e0d80SYinghai Lu 
3783e6e0d80SYinghai Lu 	/* Save original start, end, flags etc at first */
379bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
380bdc4abecSYinghai Lu 		if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
381bffc56d4SYinghai Lu 			free_list(&save_head);
3823e6e0d80SYinghai Lu 			goto requested_and_reassign;
3833e6e0d80SYinghai Lu 		}
384bdc4abecSYinghai Lu 	}
3853e6e0d80SYinghai Lu 
3863e6e0d80SYinghai Lu 	/* Update res in head list with add_size in realloc_head list */
387bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list)
388bdc4abecSYinghai Lu 		dev_res->res->end += get_res_add_size(realloc_head,
389bdc4abecSYinghai Lu 							dev_res->res);
3903e6e0d80SYinghai Lu 
3913e6e0d80SYinghai Lu 	/* Try updated head list with add_size added */
3923e6e0d80SYinghai Lu 	assign_requested_resources_sorted(head, &local_fail_head);
3933e6e0d80SYinghai Lu 
3943e6e0d80SYinghai Lu 	/* all assigned with add_size ? */
395bdc4abecSYinghai Lu 	if (list_empty(&local_fail_head)) {
3963e6e0d80SYinghai Lu 		/* Remove head list from realloc_head list */
397bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list)
398bdc4abecSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
399bffc56d4SYinghai Lu 		free_list(&save_head);
400bffc56d4SYinghai Lu 		free_list(head);
4013e6e0d80SYinghai Lu 		return;
4023e6e0d80SYinghai Lu 	}
4033e6e0d80SYinghai Lu 
404aa914f5eSYinghai Lu 	/* check failed type */
405aa914f5eSYinghai Lu 	fail_type = pci_fail_res_type_mask(&local_fail_head);
406aa914f5eSYinghai Lu 	/* remove not need to be released assigned res from head list etc */
407aa914f5eSYinghai Lu 	list_for_each_entry_safe(dev_res, tmp_res, head, list)
408aa914f5eSYinghai Lu 		if (dev_res->res->parent &&
409aa914f5eSYinghai Lu 		    !pci_need_to_release(fail_type, dev_res->res)) {
410aa914f5eSYinghai Lu 			/* remove it from realloc_head list */
411aa914f5eSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
412aa914f5eSYinghai Lu 			remove_from_list(&save_head, dev_res->res);
413aa914f5eSYinghai Lu 			list_del(&dev_res->list);
414aa914f5eSYinghai Lu 			kfree(dev_res);
415aa914f5eSYinghai Lu 		}
416aa914f5eSYinghai Lu 
417bffc56d4SYinghai Lu 	free_list(&local_fail_head);
4183e6e0d80SYinghai Lu 	/* Release assigned resource */
419bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list)
420bdc4abecSYinghai Lu 		if (dev_res->res->parent)
421bdc4abecSYinghai Lu 			release_resource(dev_res->res);
4223e6e0d80SYinghai Lu 	/* Restore start/end/flags from saved list */
423b9b0bba9SYinghai Lu 	list_for_each_entry(save_res, &save_head, list) {
424b9b0bba9SYinghai Lu 		struct resource *res = save_res->res;
4253e6e0d80SYinghai Lu 
426b9b0bba9SYinghai Lu 		res->start = save_res->start;
427b9b0bba9SYinghai Lu 		res->end = save_res->end;
428b9b0bba9SYinghai Lu 		res->flags = save_res->flags;
4293e6e0d80SYinghai Lu 	}
430bffc56d4SYinghai Lu 	free_list(&save_head);
4313e6e0d80SYinghai Lu 
4323e6e0d80SYinghai Lu requested_and_reassign:
433c8adf9a3SRam Pai 	/* Satisfy the must-have resource requests */
434c8adf9a3SRam Pai 	assign_requested_resources_sorted(head, fail_head);
435c8adf9a3SRam Pai 
4360a2daa1cSRam Pai 	/* Try to satisfy any additional optional resource
437c8adf9a3SRam Pai 		requests */
4389e8bf93aSRam Pai 	if (realloc_head)
4399e8bf93aSRam Pai 		reassign_resources_sorted(realloc_head, head);
440bffc56d4SYinghai Lu 	free_list(head);
441c8adf9a3SRam Pai }
442c8adf9a3SRam Pai 
4436841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev,
444bdc4abecSYinghai Lu 				 struct list_head *add_head,
445bdc4abecSYinghai Lu 				 struct list_head *fail_head)
4466841ec68SYinghai Lu {
447bdc4abecSYinghai Lu 	LIST_HEAD(head);
4486841ec68SYinghai Lu 
4496841ec68SYinghai Lu 	__dev_sort_resources(dev, &head);
4508424d759SYinghai Lu 	__assign_resources_sorted(&head, add_head, fail_head);
4516841ec68SYinghai Lu 
4526841ec68SYinghai Lu }
4536841ec68SYinghai Lu 
4546841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus,
455bdc4abecSYinghai Lu 					 struct list_head *realloc_head,
456bdc4abecSYinghai Lu 					 struct list_head *fail_head)
4576841ec68SYinghai Lu {
4586841ec68SYinghai Lu 	struct pci_dev *dev;
459bdc4abecSYinghai Lu 	LIST_HEAD(head);
4606841ec68SYinghai Lu 
4616841ec68SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
4626841ec68SYinghai Lu 		__dev_sort_resources(dev, &head);
4636841ec68SYinghai Lu 
4649e8bf93aSRam Pai 	__assign_resources_sorted(&head, realloc_head, fail_head);
4656841ec68SYinghai Lu }
4666841ec68SYinghai Lu 
467b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
470c7dabef8SBjorn Helgaas 	struct resource *res;
4711da177e4SLinus Torvalds 	struct pci_bus_region region;
4721da177e4SLinus Torvalds 
473b918c62eSYinghai Lu 	dev_info(&bridge->dev, "CardBus bridge to %pR\n",
474b918c62eSYinghai Lu 		 &bus->busn_res);
4751da177e4SLinus Torvalds 
476c7dabef8SBjorn Helgaas 	res = bus->resource[0];
477fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
478c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
4791da177e4SLinus Torvalds 		/*
4801da177e4SLinus Torvalds 		 * The IO resource is allocated a range twice as large as it
4811da177e4SLinus Torvalds 		 * would normally need.  This allows us to set both IO regs.
4821da177e4SLinus Torvalds 		 */
483c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4841da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
4851da177e4SLinus Torvalds 					region.start);
4861da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
4871da177e4SLinus Torvalds 					region.end);
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 
490c7dabef8SBjorn Helgaas 	res = bus->resource[1];
491fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
492c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
493c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4941da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
4951da177e4SLinus Torvalds 					region.start);
4961da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
4971da177e4SLinus Torvalds 					region.end);
4981da177e4SLinus Torvalds 	}
4991da177e4SLinus Torvalds 
500c7dabef8SBjorn Helgaas 	res = bus->resource[2];
501fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
502c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
503c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5041da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
5051da177e4SLinus Torvalds 					region.start);
5061da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
5071da177e4SLinus Torvalds 					region.end);
5081da177e4SLinus Torvalds 	}
5091da177e4SLinus Torvalds 
510c7dabef8SBjorn Helgaas 	res = bus->resource[3];
511fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
512c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
513c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5141da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
5151da177e4SLinus Torvalds 					region.start);
5161da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
5171da177e4SLinus Torvalds 					region.end);
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds }
520b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus);
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected.
5231da177e4SLinus Torvalds    PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
5241da177e4SLinus Torvalds    requires that if there is no I/O ports or memory behind the
5251da177e4SLinus Torvalds    bridge, corresponding range must be turned off by writing base
5261da177e4SLinus Torvalds    value greater than limit to the bridge's base/limit registers.
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds    Note: care must be taken when updating I/O base/limit registers
5291da177e4SLinus Torvalds    of bridges which support 32-bit I/O. This update requires two
5301da177e4SLinus Torvalds    config space writes, so it's quite possible that an I/O window of
5311da177e4SLinus Torvalds    the bridge will have some undesirable address (e.g. 0) after the
5321da177e4SLinus Torvalds    first write. Ditto 64-bit prefetchable MMIO.  */
533*3f2f4dc4SYinghai Lu static void pci_setup_bridge_io(struct pci_dev *bridge)
5341da177e4SLinus Torvalds {
535c7dabef8SBjorn Helgaas 	struct resource *res;
5361da177e4SLinus Torvalds 	struct pci_bus_region region;
5372b28ae19SBjorn Helgaas 	unsigned long io_mask;
5382b28ae19SBjorn Helgaas 	u8 io_base_lo, io_limit_lo;
5395b764b83SBjorn Helgaas 	u16 l;
5405b764b83SBjorn Helgaas 	u32 io_upper16;
5411da177e4SLinus Torvalds 
5422b28ae19SBjorn Helgaas 	io_mask = PCI_IO_RANGE_MASK;
5432b28ae19SBjorn Helgaas 	if (bridge->io_window_1k)
5442b28ae19SBjorn Helgaas 		io_mask = PCI_IO_1K_RANGE_MASK;
5452b28ae19SBjorn Helgaas 
5461da177e4SLinus Torvalds 	/* Set up the top and bottom of the PCI I/O segment for this bus. */
547*3f2f4dc4SYinghai Lu 	res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
548fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
549c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5505b764b83SBjorn Helgaas 		pci_read_config_word(bridge, PCI_IO_BASE, &l);
5512b28ae19SBjorn Helgaas 		io_base_lo = (region.start >> 8) & io_mask;
5522b28ae19SBjorn Helgaas 		io_limit_lo = (region.end >> 8) & io_mask;
5535b764b83SBjorn Helgaas 		l = ((u16) io_limit_lo << 8) | io_base_lo;
5541da177e4SLinus Torvalds 		/* Set up upper 16 bits of I/O base/limit. */
5551da177e4SLinus Torvalds 		io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
556c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5577cc5997dSYinghai Lu 	} else {
5581da177e4SLinus Torvalds 		/* Clear upper 16 bits of I/O base/limit. */
5591da177e4SLinus Torvalds 		io_upper16 = 0;
5601da177e4SLinus Torvalds 		l = 0x00f0;
5611da177e4SLinus Torvalds 	}
5621da177e4SLinus Torvalds 	/* Temporarily disable the I/O range before updating PCI_IO_BASE. */
5631da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
5641da177e4SLinus Torvalds 	/* Update lower 16 bits of I/O base/limit. */
5655b764b83SBjorn Helgaas 	pci_write_config_word(bridge, PCI_IO_BASE, l);
5661da177e4SLinus Torvalds 	/* Update upper 16 bits of I/O base/limit. */
5671da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
5687cc5997dSYinghai Lu }
5691da177e4SLinus Torvalds 
570*3f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio(struct pci_dev *bridge)
5717cc5997dSYinghai Lu {
5727cc5997dSYinghai Lu 	struct resource *res;
5737cc5997dSYinghai Lu 	struct pci_bus_region region;
5747cc5997dSYinghai Lu 	u32 l;
5757cc5997dSYinghai Lu 
5767cc5997dSYinghai Lu 	/* Set up the top and bottom of the PCI Memory segment for this bus. */
577*3f2f4dc4SYinghai Lu 	res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
578fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
579c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
5801da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
5811da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
582c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5837cc5997dSYinghai Lu 	} else {
5841da177e4SLinus Torvalds 		l = 0x0000fff0;
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
5877cc5997dSYinghai Lu }
5887cc5997dSYinghai Lu 
589*3f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge)
5907cc5997dSYinghai Lu {
5917cc5997dSYinghai Lu 	struct resource *res;
5927cc5997dSYinghai Lu 	struct pci_bus_region region;
5937cc5997dSYinghai Lu 	u32 l, bu, lu;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	/* Clear out the upper 32 bits of PREF limit.
5961da177e4SLinus Torvalds 	   If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
5971da177e4SLinus Torvalds 	   disables PREF range, which is ok. */
5981da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	/* Set up PREF base/limit. */
601c40a22e0SBenjamin Herrenschmidt 	bu = lu = 0;
602*3f2f4dc4SYinghai Lu 	res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
603fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
604c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_PREFETCH) {
6051da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
6061da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
607c7dabef8SBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64) {
60813d36c24SAndrew Morton 			bu = upper_32_bits(region.start);
60913d36c24SAndrew Morton 			lu = upper_32_bits(region.end);
6101f82de10SYinghai Lu 		}
611c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
6127cc5997dSYinghai Lu 	} else {
6131da177e4SLinus Torvalds 		l = 0x0000fff0;
6141da177e4SLinus Torvalds 	}
6151da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
6161da177e4SLinus Torvalds 
617c40a22e0SBenjamin Herrenschmidt 	/* Set the upper 32 bits of PREF base & limit. */
618c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
619c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
6207cc5997dSYinghai Lu }
6217cc5997dSYinghai Lu 
6227cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
6237cc5997dSYinghai Lu {
6247cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
6257cc5997dSYinghai Lu 
626b918c62eSYinghai Lu 	dev_info(&bridge->dev, "PCI bridge to %pR\n",
627b918c62eSYinghai Lu 		 &bus->busn_res);
6287cc5997dSYinghai Lu 
6297cc5997dSYinghai Lu 	if (type & IORESOURCE_IO)
630*3f2f4dc4SYinghai Lu 		pci_setup_bridge_io(bridge);
6317cc5997dSYinghai Lu 
6327cc5997dSYinghai Lu 	if (type & IORESOURCE_MEM)
633*3f2f4dc4SYinghai Lu 		pci_setup_bridge_mmio(bridge);
6347cc5997dSYinghai Lu 
6357cc5997dSYinghai Lu 	if (type & IORESOURCE_PREFETCH)
636*3f2f4dc4SYinghai Lu 		pci_setup_bridge_mmio_pref(bridge);
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
641e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus)
6427cc5997dSYinghai Lu {
6437cc5997dSYinghai Lu 	unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
6447cc5997dSYinghai Lu 				  IORESOURCE_PREFETCH;
6457cc5997dSYinghai Lu 
6467cc5997dSYinghai Lu 	__pci_setup_bridge(bus, type);
6477cc5997dSYinghai Lu }
6487cc5997dSYinghai Lu 
6491da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and
6501da177e4SLinus Torvalds    prefetchable memory ranges. If not, the respective
6511da177e4SLinus Torvalds    base/limit registers must be read-only and read as 0. */
65296bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus)
6531da177e4SLinus Torvalds {
6541da177e4SLinus Torvalds 	u16 io;
6551da177e4SLinus Torvalds 	u32 pmem;
6561da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
6571da177e4SLinus Torvalds 	struct resource *b_res;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
6601da177e4SLinus Torvalds 	b_res[1].flags |= IORESOURCE_MEM;
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_IO_BASE, &io);
6631da177e4SLinus Torvalds 	if (!io) {
664d2f54d9bSBjorn Helgaas 		pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0);
6651da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_IO_BASE, &io);
6661da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
6671da177e4SLinus Torvalds 	}
6681da177e4SLinus Torvalds 	if (io)
6691da177e4SLinus Torvalds 		b_res[0].flags |= IORESOURCE_IO;
670d2f54d9bSBjorn Helgaas 
6711da177e4SLinus Torvalds 	/*  DECchip 21050 pass 2 errata: the bridge may miss an address
6721da177e4SLinus Torvalds 	    disconnect boundary by one PCI data phase.
6731da177e4SLinus Torvalds 	    Workaround: do not use prefetching on this device. */
6741da177e4SLinus Torvalds 	if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
6751da177e4SLinus Torvalds 		return;
676d2f54d9bSBjorn Helgaas 
6771da177e4SLinus Torvalds 	pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
6781da177e4SLinus Torvalds 	if (!pmem) {
6791da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
680d2f54d9bSBjorn Helgaas 					       0xffe0fff0);
6811da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
6821da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
6831da177e4SLinus Torvalds 	}
6841f82de10SYinghai Lu 	if (pmem) {
6851da177e4SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
68699586105SYinghai Lu 		if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
68799586105SYinghai Lu 		    PCI_PREF_RANGE_TYPE_64) {
6881f82de10SYinghai Lu 			b_res[2].flags |= IORESOURCE_MEM_64;
68999586105SYinghai Lu 			b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
69099586105SYinghai Lu 		}
6911f82de10SYinghai Lu 	}
6921f82de10SYinghai Lu 
6931f82de10SYinghai Lu 	/* double check if bridge does support 64 bit pref */
6941f82de10SYinghai Lu 	if (b_res[2].flags & IORESOURCE_MEM_64) {
6951f82de10SYinghai Lu 		u32 mem_base_hi, tmp;
6961f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
6971f82de10SYinghai Lu 					 &mem_base_hi);
6981f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
6991f82de10SYinghai Lu 					       0xffffffff);
7001f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
7011f82de10SYinghai Lu 		if (!tmp)
7021f82de10SYinghai Lu 			b_res[2].flags &= ~IORESOURCE_MEM_64;
7031f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
7041f82de10SYinghai Lu 				       mem_base_hi);
7051f82de10SYinghai Lu 	}
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds /* Helper function for sizing routines: find first available
7091da177e4SLinus Torvalds    bus resource of a given type. Note: we intentionally skip
7101da177e4SLinus Torvalds    the bus resources which have already been assigned (that is,
7111da177e4SLinus Torvalds    have non-NULL parent resource). */
7125b285415SYinghai Lu static struct resource *find_free_bus_resource(struct pci_bus *bus,
7135b285415SYinghai Lu 			 unsigned long type_mask, unsigned long type)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	int i;
7161da177e4SLinus Torvalds 	struct resource *r;
7171da177e4SLinus Torvalds 
71889a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, r, i) {
719299de034SIvan Kokshaysky 		if (r == &ioport_resource || r == &iomem_resource)
720299de034SIvan Kokshaysky 			continue;
72155a10984SJesse Barnes 		if (r && (r->flags & type_mask) == type && !r->parent)
7221da177e4SLinus Torvalds 			return r;
7231da177e4SLinus Torvalds 	}
7241da177e4SLinus Torvalds 	return NULL;
7251da177e4SLinus Torvalds }
7261da177e4SLinus Torvalds 
72713583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size,
72813583b16SRam Pai 		resource_size_t min_size,
72913583b16SRam Pai 		resource_size_t size1,
73013583b16SRam Pai 		resource_size_t old_size,
73113583b16SRam Pai 		resource_size_t align)
73213583b16SRam Pai {
73313583b16SRam Pai 	if (size < min_size)
73413583b16SRam Pai 		size = min_size;
73513583b16SRam Pai 	if (old_size == 1)
73613583b16SRam Pai 		old_size = 0;
73713583b16SRam Pai 	/* To be fixed in 2.5: we should have sort of HAVE_ISA
73813583b16SRam Pai 	   flag in the struct pci_bus. */
73913583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
74013583b16SRam Pai 	size = (size & 0xff) + ((size & ~0xffUL) << 2);
74113583b16SRam Pai #endif
74213583b16SRam Pai 	size = ALIGN(size + size1, align);
74313583b16SRam Pai 	if (size < old_size)
74413583b16SRam Pai 		size = old_size;
74513583b16SRam Pai 	return size;
74613583b16SRam Pai }
74713583b16SRam Pai 
74813583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size,
74913583b16SRam Pai 		resource_size_t min_size,
75013583b16SRam Pai 		resource_size_t size1,
75113583b16SRam Pai 		resource_size_t old_size,
75213583b16SRam Pai 		resource_size_t align)
75313583b16SRam Pai {
75413583b16SRam Pai 	if (size < min_size)
75513583b16SRam Pai 		size = min_size;
75613583b16SRam Pai 	if (old_size == 1)
75713583b16SRam Pai 		old_size = 0;
75813583b16SRam Pai 	if (size < old_size)
75913583b16SRam Pai 		size = old_size;
76013583b16SRam Pai 	size = ALIGN(size + size1, align);
76113583b16SRam Pai 	return size;
76213583b16SRam Pai }
76313583b16SRam Pai 
764ac5ad93eSGavin Shan resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
765ac5ad93eSGavin Shan 						unsigned long type)
766ac5ad93eSGavin Shan {
767ac5ad93eSGavin Shan 	return 1;
768ac5ad93eSGavin Shan }
769ac5ad93eSGavin Shan 
770ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_MEM_ALIGN	0x100000	/* 1MiB */
771ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN	0x1000		/* 4KiB */
772ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN_1K	0x400		/* 1KiB */
773ac5ad93eSGavin Shan 
774ac5ad93eSGavin Shan static resource_size_t window_alignment(struct pci_bus *bus,
775ac5ad93eSGavin Shan 					unsigned long type)
776ac5ad93eSGavin Shan {
777ac5ad93eSGavin Shan 	resource_size_t align = 1, arch_align;
778ac5ad93eSGavin Shan 
779ac5ad93eSGavin Shan 	if (type & IORESOURCE_MEM)
780ac5ad93eSGavin Shan 		align = PCI_P2P_DEFAULT_MEM_ALIGN;
781ac5ad93eSGavin Shan 	else if (type & IORESOURCE_IO) {
782ac5ad93eSGavin Shan 		/*
783ac5ad93eSGavin Shan 		 * Per spec, I/O windows are 4K-aligned, but some
784ac5ad93eSGavin Shan 		 * bridges have an extension to support 1K alignment.
785ac5ad93eSGavin Shan 		 */
786ac5ad93eSGavin Shan 		if (bus->self->io_window_1k)
787ac5ad93eSGavin Shan 			align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
788ac5ad93eSGavin Shan 		else
789ac5ad93eSGavin Shan 			align = PCI_P2P_DEFAULT_IO_ALIGN;
790ac5ad93eSGavin Shan 	}
791ac5ad93eSGavin Shan 
792ac5ad93eSGavin Shan 	arch_align = pcibios_window_alignment(bus, type);
793ac5ad93eSGavin Shan 	return max(align, arch_align);
794ac5ad93eSGavin Shan }
795ac5ad93eSGavin Shan 
796c8adf9a3SRam Pai /**
797c8adf9a3SRam Pai  * pbus_size_io() - size the io window of a given bus
798c8adf9a3SRam Pai  *
799c8adf9a3SRam Pai  * @bus : the bus
800c8adf9a3SRam Pai  * @min_size : the minimum io window that must to be allocated
801c8adf9a3SRam Pai  * @add_size : additional optional io window
8029e8bf93aSRam Pai  * @realloc_head : track the additional io window on this list
803c8adf9a3SRam Pai  *
804c8adf9a3SRam Pai  * Sizing the IO windows of the PCI-PCI bridge is trivial,
805fd591341SYinghai Lu  * since these windows have 1K or 4K granularity and the IO ranges
806c8adf9a3SRam Pai  * of non-bridge PCI devices are limited to 256 bytes.
807c8adf9a3SRam Pai  * We must be careful with the ISA aliasing though.
808c8adf9a3SRam Pai  */
809c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
810bdc4abecSYinghai Lu 		resource_size_t add_size, struct list_head *realloc_head)
8111da177e4SLinus Torvalds {
8121da177e4SLinus Torvalds 	struct pci_dev *dev;
8135b285415SYinghai Lu 	struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO,
8145b285415SYinghai Lu 							IORESOURCE_IO);
81511251a86SWei Yang 	resource_size_t size = 0, size0 = 0, size1 = 0;
816be768912SYinghai Lu 	resource_size_t children_add_size = 0;
8172d1d6678SBjorn Helgaas 	resource_size_t min_align, align;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	if (!b_res)
8201da177e4SLinus Torvalds 		return;
8211da177e4SLinus Torvalds 
8222d1d6678SBjorn Helgaas 	min_align = window_alignment(bus, IORESOURCE_IO);
8231da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
8241da177e4SLinus Torvalds 		int i;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
8271da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
8281da177e4SLinus Torvalds 			unsigned long r_size;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 			if (r->parent || !(r->flags & IORESOURCE_IO))
8311da177e4SLinus Torvalds 				continue;
832022edd86SZhao, Yu 			r_size = resource_size(r);
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 			if (r_size < 0x400)
8351da177e4SLinus Torvalds 				/* Might be re-aligned for ISA */
8361da177e4SLinus Torvalds 				size += r_size;
8371da177e4SLinus Torvalds 			else
8381da177e4SLinus Torvalds 				size1 += r_size;
839be768912SYinghai Lu 
840fd591341SYinghai Lu 			align = pci_resource_alignment(dev, r);
841fd591341SYinghai Lu 			if (align > min_align)
842fd591341SYinghai Lu 				min_align = align;
843fd591341SYinghai Lu 
8449e8bf93aSRam Pai 			if (realloc_head)
8459e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
8461da177e4SLinus Torvalds 		}
8471da177e4SLinus Torvalds 	}
848fd591341SYinghai Lu 
849c8adf9a3SRam Pai 	size0 = calculate_iosize(size, min_size, size1,
850fd591341SYinghai Lu 			resource_size(b_res), min_align);
851be768912SYinghai Lu 	if (children_add_size > add_size)
852be768912SYinghai Lu 		add_size = children_add_size;
8539e8bf93aSRam Pai 	size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
854a4ac9feaSYinghai Lu 		calculate_iosize(size, min_size, add_size + size1,
855fd591341SYinghai Lu 			resource_size(b_res), min_align);
856c8adf9a3SRam Pai 	if (!size0 && !size1) {
857865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
858227f0647SRyan Desfosses 			dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n",
859227f0647SRyan Desfosses 				 b_res, &bus->busn_res);
8601da177e4SLinus Torvalds 		b_res->flags = 0;
8611da177e4SLinus Torvalds 		return;
8621da177e4SLinus Torvalds 	}
863fd591341SYinghai Lu 
864fd591341SYinghai Lu 	b_res->start = min_align;
865c8adf9a3SRam Pai 	b_res->end = b_res->start + size0 - 1;
86688452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
867b592443dSYinghai Lu 	if (size1 > size0 && realloc_head) {
868fd591341SYinghai Lu 		add_to_list(realloc_head, bus->self, b_res, size1-size0,
869fd591341SYinghai Lu 			    min_align);
870227f0647SRyan Desfosses 		dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx\n",
871227f0647SRyan Desfosses 			   b_res, &bus->busn_res,
87211251a86SWei Yang 			   (unsigned long long)size1-size0);
873b592443dSYinghai Lu 	}
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
876c121504eSGavin Shan static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
877c121504eSGavin Shan 						  int max_order)
878c121504eSGavin Shan {
879c121504eSGavin Shan 	resource_size_t align = 0;
880c121504eSGavin Shan 	resource_size_t min_align = 0;
881c121504eSGavin Shan 	int order;
882c121504eSGavin Shan 
883c121504eSGavin Shan 	for (order = 0; order <= max_order; order++) {
884c121504eSGavin Shan 		resource_size_t align1 = 1;
885c121504eSGavin Shan 
886c121504eSGavin Shan 		align1 <<= (order + 20);
887c121504eSGavin Shan 
888c121504eSGavin Shan 		if (!align)
889c121504eSGavin Shan 			min_align = align1;
890c121504eSGavin Shan 		else if (ALIGN(align + min_align, min_align) < align1)
891c121504eSGavin Shan 			min_align = align1 >> 1;
892c121504eSGavin Shan 		align += aligns[order];
893c121504eSGavin Shan 	}
894c121504eSGavin Shan 
895c121504eSGavin Shan 	return min_align;
896c121504eSGavin Shan }
897c121504eSGavin Shan 
898c8adf9a3SRam Pai /**
899c8adf9a3SRam Pai  * pbus_size_mem() - size the memory window of a given bus
900c8adf9a3SRam Pai  *
901c8adf9a3SRam Pai  * @bus : the bus
902496f70cfSWei Yang  * @mask: mask the resource flag, then compare it with type
903496f70cfSWei Yang  * @type: the type of free resource from bridge
9045b285415SYinghai Lu  * @type2: second match type
9055b285415SYinghai Lu  * @type3: third match type
906c8adf9a3SRam Pai  * @min_size : the minimum memory window that must to be allocated
907c8adf9a3SRam Pai  * @add_size : additional optional memory window
9089e8bf93aSRam Pai  * @realloc_head : track the additional memory window on this list
909c8adf9a3SRam Pai  *
910c8adf9a3SRam Pai  * Calculate the size of the bus and minimal alignment which
911c8adf9a3SRam Pai  * guarantees that all child resources fit in this size.
91230afe8d0SBjorn Helgaas  *
91330afe8d0SBjorn Helgaas  * Returns -ENOSPC if there's no available bus resource of the desired type.
91430afe8d0SBjorn Helgaas  * Otherwise, sets the bus resource start/end to indicate the required
91530afe8d0SBjorn Helgaas  * size, adds things to realloc_head (if supplied), and returns 0.
916c8adf9a3SRam Pai  */
91728760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
9185b285415SYinghai Lu 			 unsigned long type, unsigned long type2,
9195b285415SYinghai Lu 			 unsigned long type3,
9205b285415SYinghai Lu 			 resource_size_t min_size, resource_size_t add_size,
921bdc4abecSYinghai Lu 			 struct list_head *realloc_head)
9221da177e4SLinus Torvalds {
9231da177e4SLinus Torvalds 	struct pci_dev *dev;
924c8adf9a3SRam Pai 	resource_size_t min_align, align, size, size0, size1;
925096d4221SYinghai Lu 	resource_size_t aligns[18];	/* Alignments from 1Mb to 128Gb */
9261da177e4SLinus Torvalds 	int order, max_order;
9275b285415SYinghai Lu 	struct resource *b_res = find_free_bus_resource(bus,
9285b285415SYinghai Lu 					mask | IORESOURCE_PREFETCH, type);
929be768912SYinghai Lu 	resource_size_t children_add_size = 0;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	if (!b_res)
93230afe8d0SBjorn Helgaas 		return -ENOSPC;
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
9351da177e4SLinus Torvalds 	max_order = 0;
9361da177e4SLinus Torvalds 	size = 0;
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
9391da177e4SLinus Torvalds 		int i;
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
9421da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
943c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
9441da177e4SLinus Torvalds 
9455b285415SYinghai Lu 			if (r->parent || ((r->flags & mask) != type &&
9465b285415SYinghai Lu 					  (r->flags & mask) != type2 &&
9475b285415SYinghai Lu 					  (r->flags & mask) != type3))
9481da177e4SLinus Torvalds 				continue;
949022edd86SZhao, Yu 			r_size = resource_size(r);
9502aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV
9512aceefcbSYinghai Lu 			/* put SRIOV requested res to the optional list */
9529e8bf93aSRam Pai 			if (realloc_head && i >= PCI_IOV_RESOURCES &&
9532aceefcbSYinghai Lu 					i <= PCI_IOV_RESOURCE_END) {
9542aceefcbSYinghai Lu 				r->end = r->start - 1;
955f7625980SBjorn Helgaas 				add_to_list(realloc_head, dev, r, r_size, 0/* don't care */);
9562aceefcbSYinghai Lu 				children_add_size += r_size;
9572aceefcbSYinghai Lu 				continue;
9582aceefcbSYinghai Lu 			}
9592aceefcbSYinghai Lu #endif
96014c8530dSAlan 			/*
96114c8530dSAlan 			 * aligns[0] is for 1MB (since bridge memory
96214c8530dSAlan 			 * windows are always at least 1MB aligned), so
96314c8530dSAlan 			 * keep "order" from being negative for smaller
96414c8530dSAlan 			 * resources.
96514c8530dSAlan 			 */
9666faf17f6SChris Wright 			align = pci_resource_alignment(dev, r);
9671da177e4SLinus Torvalds 			order = __ffs(align) - 20;
96814c8530dSAlan 			if (order < 0)
96914c8530dSAlan 				order = 0;
97014c8530dSAlan 			if (order >= ARRAY_SIZE(aligns)) {
971227f0647SRyan Desfosses 				dev_warn(&dev->dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
972227f0647SRyan Desfosses 					 i, r, (unsigned long long) align);
9731da177e4SLinus Torvalds 				r->flags = 0;
9741da177e4SLinus Torvalds 				continue;
9751da177e4SLinus Torvalds 			}
9761da177e4SLinus Torvalds 			size += r_size;
9771da177e4SLinus Torvalds 			/* Exclude ranges with size > align from
9781da177e4SLinus Torvalds 			   calculation of the alignment. */
9791da177e4SLinus Torvalds 			if (r_size == align)
9801da177e4SLinus Torvalds 				aligns[order] += align;
9811da177e4SLinus Torvalds 			if (order > max_order)
9821da177e4SLinus Torvalds 				max_order = order;
983be768912SYinghai Lu 
9849e8bf93aSRam Pai 			if (realloc_head)
9859e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
9861da177e4SLinus Torvalds 		}
9871da177e4SLinus Torvalds 	}
9888308c54dSJeremy Fitzhardinge 
989c121504eSGavin Shan 	min_align = calculate_mem_align(aligns, max_order);
9903ad94b0dSWei Yang 	min_align = max(min_align, window_alignment(bus, b_res->flags));
991b42282e5SLinus Torvalds 	size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
992be768912SYinghai Lu 	if (children_add_size > add_size)
993be768912SYinghai Lu 		add_size = children_add_size;
9949e8bf93aSRam Pai 	size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
995a4ac9feaSYinghai Lu 		calculate_memsize(size, min_size, add_size,
996b42282e5SLinus Torvalds 				resource_size(b_res), min_align);
997c8adf9a3SRam Pai 	if (!size0 && !size1) {
998865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
999227f0647SRyan Desfosses 			dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n",
1000227f0647SRyan Desfosses 				 b_res, &bus->busn_res);
10011da177e4SLinus Torvalds 		b_res->flags = 0;
100230afe8d0SBjorn Helgaas 		return 0;
10031da177e4SLinus Torvalds 	}
10041da177e4SLinus Torvalds 	b_res->start = min_align;
1005c8adf9a3SRam Pai 	b_res->end = size0 + min_align - 1;
10065b285415SYinghai Lu 	b_res->flags |= IORESOURCE_STARTALIGN;
1007b592443dSYinghai Lu 	if (size1 > size0 && realloc_head) {
10089e8bf93aSRam Pai 		add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
1009227f0647SRyan Desfosses 		dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx\n",
1010227f0647SRyan Desfosses 			   b_res, &bus->busn_res,
1011227f0647SRyan Desfosses 			   (unsigned long long)size1-size0);
1012b592443dSYinghai Lu 	}
101330afe8d0SBjorn Helgaas 	return 0;
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
10160a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res)
10170a2daa1cSRam Pai {
10180a2daa1cSRam Pai 	if (res->flags & IORESOURCE_IO)
10190a2daa1cSRam Pai 		return pci_cardbus_io_size;
10200a2daa1cSRam Pai 	if (res->flags & IORESOURCE_MEM)
10210a2daa1cSRam Pai 		return pci_cardbus_mem_size;
10220a2daa1cSRam Pai 	return 0;
10230a2daa1cSRam Pai }
10240a2daa1cSRam Pai 
10250a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus,
1026bdc4abecSYinghai Lu 			struct list_head *realloc_head)
10271da177e4SLinus Torvalds {
10281da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
10291da177e4SLinus Torvalds 	struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
103011848934SYinghai Lu 	resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
10311da177e4SLinus Torvalds 	u16 ctrl;
10321da177e4SLinus Torvalds 
10333796f1e2SYinghai Lu 	if (b_res[0].parent)
10343796f1e2SYinghai Lu 		goto handle_b_res_1;
10351da177e4SLinus Torvalds 	/*
10361da177e4SLinus Torvalds 	 * Reserve some resources for CardBus.  We reserve
10371da177e4SLinus Torvalds 	 * a fixed amount of bus space for CardBus bridges.
10381da177e4SLinus Torvalds 	 */
103911848934SYinghai Lu 	b_res[0].start = pci_cardbus_io_size;
104011848934SYinghai Lu 	b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
104111848934SYinghai Lu 	b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
104211848934SYinghai Lu 	if (realloc_head) {
104311848934SYinghai Lu 		b_res[0].end -= pci_cardbus_io_size;
104411848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
104511848934SYinghai Lu 				pci_cardbus_io_size);
104611848934SYinghai Lu 	}
10471da177e4SLinus Torvalds 
10483796f1e2SYinghai Lu handle_b_res_1:
10493796f1e2SYinghai Lu 	if (b_res[1].parent)
10503796f1e2SYinghai Lu 		goto handle_b_res_2;
105111848934SYinghai Lu 	b_res[1].start = pci_cardbus_io_size;
105211848934SYinghai Lu 	b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
105311848934SYinghai Lu 	b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
105411848934SYinghai Lu 	if (realloc_head) {
105511848934SYinghai Lu 		b_res[1].end -= pci_cardbus_io_size;
105611848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
105711848934SYinghai Lu 				 pci_cardbus_io_size);
105811848934SYinghai Lu 	}
10591da177e4SLinus Torvalds 
10603796f1e2SYinghai Lu handle_b_res_2:
1061dcef0d06SYinghai Lu 	/* MEM1 must not be pref mmio */
1062dcef0d06SYinghai Lu 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1063dcef0d06SYinghai Lu 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
1064dcef0d06SYinghai Lu 		ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
1065dcef0d06SYinghai Lu 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1066dcef0d06SYinghai Lu 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1067dcef0d06SYinghai Lu 	}
1068dcef0d06SYinghai Lu 
10691da177e4SLinus Torvalds 	/*
10701da177e4SLinus Torvalds 	 * Check whether prefetchable memory is supported
10711da177e4SLinus Torvalds 	 * by this bridge.
10721da177e4SLinus Torvalds 	 */
10731da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
10741da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
10751da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
10761da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
10771da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
10781da177e4SLinus Torvalds 	}
10791da177e4SLinus Torvalds 
10803796f1e2SYinghai Lu 	if (b_res[2].parent)
10813796f1e2SYinghai Lu 		goto handle_b_res_3;
10821da177e4SLinus Torvalds 	/*
10831da177e4SLinus Torvalds 	 * If we have prefetchable memory support, allocate
10841da177e4SLinus Torvalds 	 * two regions.  Otherwise, allocate one region of
10851da177e4SLinus Torvalds 	 * twice the size.
10861da177e4SLinus Torvalds 	 */
10871da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
108811848934SYinghai Lu 		b_res[2].start = pci_cardbus_mem_size;
108911848934SYinghai Lu 		b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
109011848934SYinghai Lu 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
109111848934SYinghai Lu 				  IORESOURCE_STARTALIGN;
109211848934SYinghai Lu 		if (realloc_head) {
109311848934SYinghai Lu 			b_res[2].end -= pci_cardbus_mem_size;
109411848934SYinghai Lu 			add_to_list(realloc_head, bridge, b_res+2,
109511848934SYinghai Lu 				 pci_cardbus_mem_size, pci_cardbus_mem_size);
10961da177e4SLinus Torvalds 		}
10970a2daa1cSRam Pai 
109811848934SYinghai Lu 		/* reduce that to half */
109911848934SYinghai Lu 		b_res_3_size = pci_cardbus_mem_size;
110011848934SYinghai Lu 	}
110111848934SYinghai Lu 
11023796f1e2SYinghai Lu handle_b_res_3:
11033796f1e2SYinghai Lu 	if (b_res[3].parent)
11043796f1e2SYinghai Lu 		goto handle_done;
110511848934SYinghai Lu 	b_res[3].start = pci_cardbus_mem_size;
110611848934SYinghai Lu 	b_res[3].end = b_res[3].start + b_res_3_size - 1;
110711848934SYinghai Lu 	b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
110811848934SYinghai Lu 	if (realloc_head) {
110911848934SYinghai Lu 		b_res[3].end -= b_res_3_size;
111011848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
111111848934SYinghai Lu 				 pci_cardbus_mem_size);
111211848934SYinghai Lu 	}
11133796f1e2SYinghai Lu 
11143796f1e2SYinghai Lu handle_done:
11153796f1e2SYinghai Lu 	;
11161da177e4SLinus Torvalds }
11171da177e4SLinus Torvalds 
111810874f5aSBjorn Helgaas void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
11191da177e4SLinus Torvalds {
11201da177e4SLinus Torvalds 	struct pci_dev *dev;
11215b285415SYinghai Lu 	unsigned long mask, prefmask, type2 = 0, type3 = 0;
1122c8adf9a3SRam Pai 	resource_size_t additional_mem_size = 0, additional_io_size = 0;
11235b285415SYinghai Lu 	struct resource *b_res;
112430afe8d0SBjorn Helgaas 	int ret;
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
11271da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
11281da177e4SLinus Torvalds 		if (!b)
11291da177e4SLinus Torvalds 			continue;
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 		switch (dev->class >> 8) {
11321da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
11339e8bf93aSRam Pai 			pci_bus_size_cardbus(b, realloc_head);
11341da177e4SLinus Torvalds 			break;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
11371da177e4SLinus Torvalds 		default:
11389e8bf93aSRam Pai 			__pci_bus_size_bridges(b, realloc_head);
11391da177e4SLinus Torvalds 			break;
11401da177e4SLinus Torvalds 		}
11411da177e4SLinus Torvalds 	}
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	/* The root bus? */
11442ba29e27SWei Yang 	if (pci_is_root_bus(bus))
11451da177e4SLinus Torvalds 		return;
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	switch (bus->self->class >> 8) {
11481da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_CARDBUS:
11491da177e4SLinus Torvalds 		/* don't size cardbuses yet. */
11501da177e4SLinus Torvalds 		break;
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_PCI:
11531da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
115428760489SEric W. Biederman 		if (bus->self->is_hotplug_bridge) {
1155c8adf9a3SRam Pai 			additional_io_size  = pci_hotplug_io_size;
1156c8adf9a3SRam Pai 			additional_mem_size = pci_hotplug_mem_size;
115728760489SEric W. Biederman 		}
115867d29b5cSBjorn Helgaas 		/* Fall through */
11591da177e4SLinus Torvalds 	default:
116019aa7ee4SYinghai Lu 		pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
116119aa7ee4SYinghai Lu 			     additional_io_size, realloc_head);
116267d29b5cSBjorn Helgaas 
116367d29b5cSBjorn Helgaas 		/*
116467d29b5cSBjorn Helgaas 		 * If there's a 64-bit prefetchable MMIO window, compute
116567d29b5cSBjorn Helgaas 		 * the size required to put all 64-bit prefetchable
116667d29b5cSBjorn Helgaas 		 * resources in it.
116767d29b5cSBjorn Helgaas 		 */
11685b285415SYinghai Lu 		b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES];
11691da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
11701da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
11715b285415SYinghai Lu 		if (b_res[2].flags & IORESOURCE_MEM_64) {
11725b285415SYinghai Lu 			prefmask |= IORESOURCE_MEM_64;
117330afe8d0SBjorn Helgaas 			ret = pbus_size_mem(bus, prefmask, prefmask,
11745b285415SYinghai Lu 				  prefmask, prefmask,
117519aa7ee4SYinghai Lu 				  realloc_head ? 0 : additional_mem_size,
117630afe8d0SBjorn Helgaas 				  additional_mem_size, realloc_head);
117767d29b5cSBjorn Helgaas 
11785b285415SYinghai Lu 			/*
117967d29b5cSBjorn Helgaas 			 * If successful, all non-prefetchable resources
118067d29b5cSBjorn Helgaas 			 * and any 32-bit prefetchable resources will go in
118167d29b5cSBjorn Helgaas 			 * the non-prefetchable window.
118267d29b5cSBjorn Helgaas 			 */
118367d29b5cSBjorn Helgaas 			if (ret == 0) {
11845b285415SYinghai Lu 				mask = prefmask;
11855b285415SYinghai Lu 				type2 = prefmask & ~IORESOURCE_MEM_64;
11865b285415SYinghai Lu 				type3 = prefmask & ~IORESOURCE_PREFETCH;
11875b285415SYinghai Lu 			}
11885b285415SYinghai Lu 		}
118967d29b5cSBjorn Helgaas 
119067d29b5cSBjorn Helgaas 		/*
119167d29b5cSBjorn Helgaas 		 * If there is no 64-bit prefetchable window, compute the
119267d29b5cSBjorn Helgaas 		 * size required to put all prefetchable resources in the
119367d29b5cSBjorn Helgaas 		 * 32-bit prefetchable window (if there is one).
119467d29b5cSBjorn Helgaas 		 */
11955b285415SYinghai Lu 		if (!type2) {
11965b285415SYinghai Lu 			prefmask &= ~IORESOURCE_MEM_64;
119730afe8d0SBjorn Helgaas 			ret = pbus_size_mem(bus, prefmask, prefmask,
11985b285415SYinghai Lu 					 prefmask, prefmask,
11995b285415SYinghai Lu 					 realloc_head ? 0 : additional_mem_size,
120030afe8d0SBjorn Helgaas 					 additional_mem_size, realloc_head);
120167d29b5cSBjorn Helgaas 
120267d29b5cSBjorn Helgaas 			/*
120367d29b5cSBjorn Helgaas 			 * If successful, only non-prefetchable resources
120467d29b5cSBjorn Helgaas 			 * will go in the non-prefetchable window.
120567d29b5cSBjorn Helgaas 			 */
120667d29b5cSBjorn Helgaas 			if (ret == 0)
12075b285415SYinghai Lu 				mask = prefmask;
120828760489SEric W. Biederman 			else
1209c8adf9a3SRam Pai 				additional_mem_size += additional_mem_size;
121067d29b5cSBjorn Helgaas 
12115b285415SYinghai Lu 			type2 = type3 = IORESOURCE_MEM;
12125b285415SYinghai Lu 		}
121367d29b5cSBjorn Helgaas 
121467d29b5cSBjorn Helgaas 		/*
121567d29b5cSBjorn Helgaas 		 * Compute the size required to put everything else in the
121667d29b5cSBjorn Helgaas 		 * non-prefetchable window.  This includes:
121767d29b5cSBjorn Helgaas 		 *
121867d29b5cSBjorn Helgaas 		 *   - all non-prefetchable resources
121967d29b5cSBjorn Helgaas 		 *   - 32-bit prefetchable resources if there's a 64-bit
122067d29b5cSBjorn Helgaas 		 *     prefetchable window or no prefetchable window at all
122167d29b5cSBjorn Helgaas 		 *   - 64-bit prefetchable resources if there's no
122267d29b5cSBjorn Helgaas 		 *     prefetchable window at all
122367d29b5cSBjorn Helgaas 		 *
122467d29b5cSBjorn Helgaas 		 * Note that the strategy in __pci_assign_resource() must
122567d29b5cSBjorn Helgaas 		 * match that used here.  Specifically, we cannot put a
122667d29b5cSBjorn Helgaas 		 * 32-bit prefetchable resource in a 64-bit prefetchable
122767d29b5cSBjorn Helgaas 		 * window.
122867d29b5cSBjorn Helgaas 		 */
12295b285415SYinghai Lu 		pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
123019aa7ee4SYinghai Lu 				realloc_head ? 0 : additional_mem_size,
123119aa7ee4SYinghai Lu 				additional_mem_size, realloc_head);
12321da177e4SLinus Torvalds 		break;
12331da177e4SLinus Torvalds 	}
12341da177e4SLinus Torvalds }
1235c8adf9a3SRam Pai 
123610874f5aSBjorn Helgaas void pci_bus_size_bridges(struct pci_bus *bus)
1237c8adf9a3SRam Pai {
1238c8adf9a3SRam Pai 	__pci_bus_size_bridges(bus, NULL);
1239c8adf9a3SRam Pai }
12401da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
12411da177e4SLinus Torvalds 
124210874f5aSBjorn Helgaas void __pci_bus_assign_resources(const struct pci_bus *bus,
1243bdc4abecSYinghai Lu 				struct list_head *realloc_head,
1244bdc4abecSYinghai Lu 				struct list_head *fail_head)
12451da177e4SLinus Torvalds {
12461da177e4SLinus Torvalds 	struct pci_bus *b;
12471da177e4SLinus Torvalds 	struct pci_dev *dev;
12481da177e4SLinus Torvalds 
12499e8bf93aSRam Pai 	pbus_assign_resources_sorted(bus, realloc_head, fail_head);
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
12521da177e4SLinus Torvalds 		b = dev->subordinate;
12531da177e4SLinus Torvalds 		if (!b)
12541da177e4SLinus Torvalds 			continue;
12551da177e4SLinus Torvalds 
12569e8bf93aSRam Pai 		__pci_bus_assign_resources(b, realloc_head, fail_head);
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 		switch (dev->class >> 8) {
12591da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
12606841ec68SYinghai Lu 			if (!pci_is_enabled(dev))
12611da177e4SLinus Torvalds 				pci_setup_bridge(b);
12621da177e4SLinus Torvalds 			break;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
12651da177e4SLinus Torvalds 			pci_setup_cardbus(b);
12661da177e4SLinus Torvalds 			break;
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 		default:
1269227f0647SRyan Desfosses 			dev_info(&dev->dev, "not setting up bridge for bus %04x:%02x\n",
1270227f0647SRyan Desfosses 				 pci_domain_nr(b), b->number);
12711da177e4SLinus Torvalds 			break;
12721da177e4SLinus Torvalds 		}
12731da177e4SLinus Torvalds 	}
12741da177e4SLinus Torvalds }
1275568ddef8SYinghai Lu 
127610874f5aSBjorn Helgaas void pci_bus_assign_resources(const struct pci_bus *bus)
1277568ddef8SYinghai Lu {
1278c8adf9a3SRam Pai 	__pci_bus_assign_resources(bus, NULL, NULL);
1279568ddef8SYinghai Lu }
12801da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
12811da177e4SLinus Torvalds 
128210874f5aSBjorn Helgaas static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
1283bdc4abecSYinghai Lu 					  struct list_head *add_head,
1284bdc4abecSYinghai Lu 					  struct list_head *fail_head)
12856841ec68SYinghai Lu {
12866841ec68SYinghai Lu 	struct pci_bus *b;
12876841ec68SYinghai Lu 
12888424d759SYinghai Lu 	pdev_assign_resources_sorted((struct pci_dev *)bridge,
12898424d759SYinghai Lu 					 add_head, fail_head);
12906841ec68SYinghai Lu 
12916841ec68SYinghai Lu 	b = bridge->subordinate;
12926841ec68SYinghai Lu 	if (!b)
12936841ec68SYinghai Lu 		return;
12946841ec68SYinghai Lu 
12958424d759SYinghai Lu 	__pci_bus_assign_resources(b, add_head, fail_head);
12966841ec68SYinghai Lu 
12976841ec68SYinghai Lu 	switch (bridge->class >> 8) {
12986841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_PCI:
12996841ec68SYinghai Lu 		pci_setup_bridge(b);
13006841ec68SYinghai Lu 		break;
13016841ec68SYinghai Lu 
13026841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_CARDBUS:
13036841ec68SYinghai Lu 		pci_setup_cardbus(b);
13046841ec68SYinghai Lu 		break;
13056841ec68SYinghai Lu 
13066841ec68SYinghai Lu 	default:
1307227f0647SRyan Desfosses 		dev_info(&bridge->dev, "not setting up bridge for bus %04x:%02x\n",
1308227f0647SRyan Desfosses 			 pci_domain_nr(b), b->number);
13096841ec68SYinghai Lu 		break;
13106841ec68SYinghai Lu 	}
13116841ec68SYinghai Lu }
13125009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus,
13135009b460SYinghai Lu 					  unsigned long type)
13145009b460SYinghai Lu {
13155b285415SYinghai Lu 	struct pci_dev *dev = bus->self;
13165009b460SYinghai Lu 	struct resource *r;
13175009b460SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
13185b285415SYinghai Lu 				  IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
13195b285415SYinghai Lu 	unsigned old_flags = 0;
13205b285415SYinghai Lu 	struct resource *b_res;
13215b285415SYinghai Lu 	int idx = 1;
13225009b460SYinghai Lu 
13235b285415SYinghai Lu 	b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
13245b285415SYinghai Lu 
13255b285415SYinghai Lu 	/*
13265b285415SYinghai Lu 	 *     1. if there is io port assign fail, will release bridge
13275b285415SYinghai Lu 	 *	  io port.
13285b285415SYinghai Lu 	 *     2. if there is non pref mmio assign fail, release bridge
13295b285415SYinghai Lu 	 *	  nonpref mmio.
13305b285415SYinghai Lu 	 *     3. if there is 64bit pref mmio assign fail, and bridge pref
13315b285415SYinghai Lu 	 *	  is 64bit, release bridge pref mmio.
13325b285415SYinghai Lu 	 *     4. if there is pref mmio assign fail, and bridge pref is
13335b285415SYinghai Lu 	 *	  32bit mmio, release bridge pref mmio
13345b285415SYinghai Lu 	 *     5. if there is pref mmio assign fail, and bridge pref is not
13355b285415SYinghai Lu 	 *	  assigned, release bridge nonpref mmio.
13365b285415SYinghai Lu 	 */
13375b285415SYinghai Lu 	if (type & IORESOURCE_IO)
13385b285415SYinghai Lu 		idx = 0;
13395b285415SYinghai Lu 	else if (!(type & IORESOURCE_PREFETCH))
13405b285415SYinghai Lu 		idx = 1;
13415b285415SYinghai Lu 	else if ((type & IORESOURCE_MEM_64) &&
13425b285415SYinghai Lu 		 (b_res[2].flags & IORESOURCE_MEM_64))
13435b285415SYinghai Lu 		idx = 2;
13445b285415SYinghai Lu 	else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
13455b285415SYinghai Lu 		 (b_res[2].flags & IORESOURCE_PREFETCH))
13465b285415SYinghai Lu 		idx = 2;
13475b285415SYinghai Lu 	else
13485b285415SYinghai Lu 		idx = 1;
13495b285415SYinghai Lu 
13505b285415SYinghai Lu 	r = &b_res[idx];
13515b285415SYinghai Lu 
13525009b460SYinghai Lu 	if (!r->parent)
13535b285415SYinghai Lu 		return;
13545b285415SYinghai Lu 
13555009b460SYinghai Lu 	/*
13565009b460SYinghai Lu 	 * if there are children under that, we should release them
13575009b460SYinghai Lu 	 *  all
13585009b460SYinghai Lu 	 */
13595009b460SYinghai Lu 	release_child_resources(r);
13605009b460SYinghai Lu 	if (!release_resource(r)) {
13615b285415SYinghai Lu 		type = old_flags = r->flags & type_mask;
13625b285415SYinghai Lu 		dev_printk(KERN_DEBUG, &dev->dev, "resource %d %pR released\n",
13635b285415SYinghai Lu 					PCI_BRIDGE_RESOURCES + idx, r);
13645009b460SYinghai Lu 		/* keep the old size */
13655009b460SYinghai Lu 		r->end = resource_size(r) - 1;
13665009b460SYinghai Lu 		r->start = 0;
13675009b460SYinghai Lu 		r->flags = 0;
13685009b460SYinghai Lu 
13695009b460SYinghai Lu 		/* avoiding touch the one without PREF */
13705009b460SYinghai Lu 		if (type & IORESOURCE_PREFETCH)
13715009b460SYinghai Lu 			type = IORESOURCE_PREFETCH;
13725009b460SYinghai Lu 		__pci_setup_bridge(bus, type);
13735b285415SYinghai Lu 		/* for next child res under same bridge */
13745b285415SYinghai Lu 		r->flags = old_flags;
13755009b460SYinghai Lu 	}
13765009b460SYinghai Lu }
13775009b460SYinghai Lu 
13785009b460SYinghai Lu enum release_type {
13795009b460SYinghai Lu 	leaf_only,
13805009b460SYinghai Lu 	whole_subtree,
13815009b460SYinghai Lu };
13825009b460SYinghai Lu /*
13835009b460SYinghai Lu  * try to release pci bridge resources that is from leaf bridge,
13845009b460SYinghai Lu  * so we can allocate big new one later
13855009b460SYinghai Lu  */
138610874f5aSBjorn Helgaas static void pci_bus_release_bridge_resources(struct pci_bus *bus,
13875009b460SYinghai Lu 					     unsigned long type,
13885009b460SYinghai Lu 					     enum release_type rel_type)
13895009b460SYinghai Lu {
13905009b460SYinghai Lu 	struct pci_dev *dev;
13915009b460SYinghai Lu 	bool is_leaf_bridge = true;
13925009b460SYinghai Lu 
13935009b460SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
13945009b460SYinghai Lu 		struct pci_bus *b = dev->subordinate;
13955009b460SYinghai Lu 		if (!b)
13965009b460SYinghai Lu 			continue;
13975009b460SYinghai Lu 
13985009b460SYinghai Lu 		is_leaf_bridge = false;
13995009b460SYinghai Lu 
14005009b460SYinghai Lu 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
14015009b460SYinghai Lu 			continue;
14025009b460SYinghai Lu 
14035009b460SYinghai Lu 		if (rel_type == whole_subtree)
14045009b460SYinghai Lu 			pci_bus_release_bridge_resources(b, type,
14055009b460SYinghai Lu 						 whole_subtree);
14065009b460SYinghai Lu 	}
14075009b460SYinghai Lu 
14085009b460SYinghai Lu 	if (pci_is_root_bus(bus))
14095009b460SYinghai Lu 		return;
14105009b460SYinghai Lu 
14115009b460SYinghai Lu 	if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
14125009b460SYinghai Lu 		return;
14135009b460SYinghai Lu 
14145009b460SYinghai Lu 	if ((rel_type == whole_subtree) || is_leaf_bridge)
14155009b460SYinghai Lu 		pci_bridge_release_resources(bus, type);
14165009b460SYinghai Lu }
14175009b460SYinghai Lu 
141876fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
141976fbc263SYinghai Lu {
142089a74eccSBjorn Helgaas 	struct resource *res;
142176fbc263SYinghai Lu 	int i;
142276fbc263SYinghai Lu 
142389a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
14247c9342b8SYinghai Lu 		if (!res || !res->end || !res->flags)
142576fbc263SYinghai Lu 			continue;
142676fbc263SYinghai Lu 
1427c7dabef8SBjorn Helgaas 		dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
142876fbc263SYinghai Lu 	}
142976fbc263SYinghai Lu }
143076fbc263SYinghai Lu 
143176fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
143276fbc263SYinghai Lu {
143376fbc263SYinghai Lu 	struct pci_bus *b;
143476fbc263SYinghai Lu 	struct pci_dev *dev;
143576fbc263SYinghai Lu 
143676fbc263SYinghai Lu 
143776fbc263SYinghai Lu 	pci_bus_dump_res(bus);
143876fbc263SYinghai Lu 
143976fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
144076fbc263SYinghai Lu 		b = dev->subordinate;
144176fbc263SYinghai Lu 		if (!b)
144276fbc263SYinghai Lu 			continue;
144376fbc263SYinghai Lu 
144476fbc263SYinghai Lu 		pci_bus_dump_resources(b);
144576fbc263SYinghai Lu 	}
144676fbc263SYinghai Lu }
144776fbc263SYinghai Lu 
1448ff35147cSYinghai Lu static int pci_bus_get_depth(struct pci_bus *bus)
1449da7822e5SYinghai Lu {
1450da7822e5SYinghai Lu 	int depth = 0;
1451f2a230bdSWei Yang 	struct pci_bus *child_bus;
1452da7822e5SYinghai Lu 
1453f2a230bdSWei Yang 	list_for_each_entry(child_bus, &bus->children, node) {
1454da7822e5SYinghai Lu 		int ret;
1455da7822e5SYinghai Lu 
1456f2a230bdSWei Yang 		ret = pci_bus_get_depth(child_bus);
1457da7822e5SYinghai Lu 		if (ret + 1 > depth)
1458da7822e5SYinghai Lu 			depth = ret + 1;
1459da7822e5SYinghai Lu 	}
1460da7822e5SYinghai Lu 
1461da7822e5SYinghai Lu 	return depth;
1462da7822e5SYinghai Lu }
1463da7822e5SYinghai Lu 
1464b55438fdSYinghai Lu /*
1465b55438fdSYinghai Lu  * -1: undefined, will auto detect later
1466b55438fdSYinghai Lu  *  0: disabled by user
1467b55438fdSYinghai Lu  *  1: disabled by auto detect
1468b55438fdSYinghai Lu  *  2: enabled by user
1469b55438fdSYinghai Lu  *  3: enabled by auto detect
1470b55438fdSYinghai Lu  */
1471b55438fdSYinghai Lu enum enable_type {
1472b55438fdSYinghai Lu 	undefined = -1,
1473b55438fdSYinghai Lu 	user_disabled,
1474b55438fdSYinghai Lu 	auto_disabled,
1475b55438fdSYinghai Lu 	user_enabled,
1476b55438fdSYinghai Lu 	auto_enabled,
1477b55438fdSYinghai Lu };
1478b55438fdSYinghai Lu 
1479ff35147cSYinghai Lu static enum enable_type pci_realloc_enable = undefined;
1480b55438fdSYinghai Lu void __init pci_realloc_get_opt(char *str)
1481b55438fdSYinghai Lu {
1482b55438fdSYinghai Lu 	if (!strncmp(str, "off", 3))
1483b55438fdSYinghai Lu 		pci_realloc_enable = user_disabled;
1484b55438fdSYinghai Lu 	else if (!strncmp(str, "on", 2))
1485b55438fdSYinghai Lu 		pci_realloc_enable = user_enabled;
1486b55438fdSYinghai Lu }
1487ff35147cSYinghai Lu static bool pci_realloc_enabled(enum enable_type enable)
1488b55438fdSYinghai Lu {
1489967260cdSYinghai Lu 	return enable >= user_enabled;
1490b55438fdSYinghai Lu }
1491f483d392SRam Pai 
1492b07f2ebcSYinghai Lu #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1493ff35147cSYinghai Lu static int iov_resources_unassigned(struct pci_dev *dev, void *data)
1494223d96fcSYinghai Lu {
1495b07f2ebcSYinghai Lu 	int i;
1496223d96fcSYinghai Lu 	bool *unassigned = data;
1497b07f2ebcSYinghai Lu 
1498b07f2ebcSYinghai Lu 	for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) {
1499b07f2ebcSYinghai Lu 		struct resource *r = &dev->resource[i];
1500fa216bf4SYinghai Lu 		struct pci_bus_region region;
1501b07f2ebcSYinghai Lu 
1502223d96fcSYinghai Lu 		/* Not assigned or rejected by kernel? */
1503fa216bf4SYinghai Lu 		if (!r->flags)
1504fa216bf4SYinghai Lu 			continue;
1505b07f2ebcSYinghai Lu 
1506fc279850SYinghai Lu 		pcibios_resource_to_bus(dev->bus, &region, r);
1507fa216bf4SYinghai Lu 		if (!region.start) {
1508223d96fcSYinghai Lu 			*unassigned = true;
1509223d96fcSYinghai Lu 			return 1; /* return early from pci_walk_bus() */
1510b07f2ebcSYinghai Lu 		}
1511b07f2ebcSYinghai Lu 	}
1512b07f2ebcSYinghai Lu 
1513223d96fcSYinghai Lu 	return 0;
1514223d96fcSYinghai Lu }
1515223d96fcSYinghai Lu 
1516ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1517967260cdSYinghai Lu 			 enum enable_type enable_local)
1518223d96fcSYinghai Lu {
1519223d96fcSYinghai Lu 	bool unassigned = false;
1520223d96fcSYinghai Lu 
1521967260cdSYinghai Lu 	if (enable_local != undefined)
1522967260cdSYinghai Lu 		return enable_local;
1523223d96fcSYinghai Lu 
1524223d96fcSYinghai Lu 	pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
1525967260cdSYinghai Lu 	if (unassigned)
1526967260cdSYinghai Lu 		return auto_enabled;
1527967260cdSYinghai Lu 
1528967260cdSYinghai Lu 	return enable_local;
1529b07f2ebcSYinghai Lu }
1530223d96fcSYinghai Lu #else
1531ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1532967260cdSYinghai Lu 			 enum enable_type enable_local)
1533967260cdSYinghai Lu {
1534967260cdSYinghai Lu 	return enable_local;
1535b07f2ebcSYinghai Lu }
1536b07f2ebcSYinghai Lu #endif
1537b07f2ebcSYinghai Lu 
1538da7822e5SYinghai Lu /*
1539da7822e5SYinghai Lu  * first try will not touch pci bridge res
1540da7822e5SYinghai Lu  * second and later try will clear small leaf bridge res
1541f7625980SBjorn Helgaas  * will stop till to the max depth if can not find good one
1542da7822e5SYinghai Lu  */
154339772038SYinghai Lu void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
15441da177e4SLinus Torvalds {
1545bdc4abecSYinghai Lu 	LIST_HEAD(realloc_head); /* list of resources that
1546c8adf9a3SRam Pai 					want additional resources */
1547bdc4abecSYinghai Lu 	struct list_head *add_list = NULL;
1548da7822e5SYinghai Lu 	int tried_times = 0;
1549da7822e5SYinghai Lu 	enum release_type rel_type = leaf_only;
1550bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
1551b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
1552da7822e5SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
15535b285415SYinghai Lu 				  IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
155419aa7ee4SYinghai Lu 	int pci_try_num = 1;
155555ed83a6SYinghai Lu 	enum enable_type enable_local;
1556da7822e5SYinghai Lu 
155719aa7ee4SYinghai Lu 	/* don't realloc if asked to do so */
155855ed83a6SYinghai Lu 	enable_local = pci_realloc_detect(bus, pci_realloc_enable);
1559967260cdSYinghai Lu 	if (pci_realloc_enabled(enable_local)) {
156055ed83a6SYinghai Lu 		int max_depth = pci_bus_get_depth(bus);
156119aa7ee4SYinghai Lu 
1562da7822e5SYinghai Lu 		pci_try_num = max_depth + 1;
156355ed83a6SYinghai Lu 		dev_printk(KERN_DEBUG, &bus->dev,
156455ed83a6SYinghai Lu 			   "max bus depth: %d pci_try_num: %d\n",
1565da7822e5SYinghai Lu 			   max_depth, pci_try_num);
156619aa7ee4SYinghai Lu 	}
1567da7822e5SYinghai Lu 
1568da7822e5SYinghai Lu again:
156919aa7ee4SYinghai Lu 	/*
157019aa7ee4SYinghai Lu 	 * last try will use add_list, otherwise will try good to have as
157119aa7ee4SYinghai Lu 	 * must have, so can realloc parent bridge resource
157219aa7ee4SYinghai Lu 	 */
157319aa7ee4SYinghai Lu 	if (tried_times + 1 == pci_try_num)
1574bdc4abecSYinghai Lu 		add_list = &realloc_head;
15751da177e4SLinus Torvalds 	/* Depth first, calculate sizes and alignments of all
15761da177e4SLinus Torvalds 	   subordinate buses. */
157719aa7ee4SYinghai Lu 	__pci_bus_size_bridges(bus, add_list);
1578c8adf9a3SRam Pai 
15791da177e4SLinus Torvalds 	/* Depth last, allocate resources and update the hardware. */
1580bdc4abecSYinghai Lu 	__pci_bus_assign_resources(bus, add_list, &fail_head);
158119aa7ee4SYinghai Lu 	if (add_list)
1582bdc4abecSYinghai Lu 		BUG_ON(!list_empty(add_list));
1583da7822e5SYinghai Lu 	tried_times++;
1584da7822e5SYinghai Lu 
1585da7822e5SYinghai Lu 	/* any device complain? */
1586bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
1587928bea96SYinghai Lu 		goto dump;
1588f483d392SRam Pai 
15890c5be0cbSYinghai Lu 	if (tried_times >= pci_try_num) {
1590967260cdSYinghai Lu 		if (enable_local == undefined)
159155ed83a6SYinghai Lu 			dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
1592967260cdSYinghai Lu 		else if (enable_local == auto_enabled)
159355ed83a6SYinghai Lu 			dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
1594eb572e7cSYinghai Lu 
1595bffc56d4SYinghai Lu 		free_list(&fail_head);
1596928bea96SYinghai Lu 		goto dump;
1597da7822e5SYinghai Lu 	}
1598da7822e5SYinghai Lu 
159955ed83a6SYinghai Lu 	dev_printk(KERN_DEBUG, &bus->dev,
160055ed83a6SYinghai Lu 		   "No. %d try to assign unassigned res\n", tried_times + 1);
1601da7822e5SYinghai Lu 
1602da7822e5SYinghai Lu 	/* third times and later will not check if it is leaf */
1603da7822e5SYinghai Lu 	if ((tried_times + 1) > 2)
1604da7822e5SYinghai Lu 		rel_type = whole_subtree;
1605da7822e5SYinghai Lu 
1606da7822e5SYinghai Lu 	/*
1607da7822e5SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
1608da7822e5SYinghai Lu 	 * child device under that bridge
1609da7822e5SYinghai Lu 	 */
161061e83cddSYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list)
161161e83cddSYinghai Lu 		pci_bus_release_bridge_resources(fail_res->dev->bus,
1612b9b0bba9SYinghai Lu 						 fail_res->flags & type_mask,
1613da7822e5SYinghai Lu 						 rel_type);
161461e83cddSYinghai Lu 
1615da7822e5SYinghai Lu 	/* restore size and flags */
1616b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1617b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
1618da7822e5SYinghai Lu 
1619b9b0bba9SYinghai Lu 		res->start = fail_res->start;
1620b9b0bba9SYinghai Lu 		res->end = fail_res->end;
1621b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
1622b9b0bba9SYinghai Lu 		if (fail_res->dev->subordinate)
1623da7822e5SYinghai Lu 			res->flags = 0;
1624da7822e5SYinghai Lu 	}
1625bffc56d4SYinghai Lu 	free_list(&fail_head);
1626da7822e5SYinghai Lu 
1627da7822e5SYinghai Lu 	goto again;
1628da7822e5SYinghai Lu 
1629928bea96SYinghai Lu dump:
163076fbc263SYinghai Lu 	/* dump the resource on buses */
163176fbc263SYinghai Lu 	pci_bus_dump_resources(bus);
163276fbc263SYinghai Lu }
16336841ec68SYinghai Lu 
163455ed83a6SYinghai Lu void __init pci_assign_unassigned_resources(void)
163555ed83a6SYinghai Lu {
163655ed83a6SYinghai Lu 	struct pci_bus *root_bus;
163755ed83a6SYinghai Lu 
163855ed83a6SYinghai Lu 	list_for_each_entry(root_bus, &pci_root_buses, node)
163955ed83a6SYinghai Lu 		pci_assign_unassigned_root_bus_resources(root_bus);
164055ed83a6SYinghai Lu }
164155ed83a6SYinghai Lu 
16426841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
16436841ec68SYinghai Lu {
16446841ec68SYinghai Lu 	struct pci_bus *parent = bridge->subordinate;
1645bdc4abecSYinghai Lu 	LIST_HEAD(add_list); /* list of resources that
16468424d759SYinghai Lu 					want additional resources */
164732180e40SYinghai Lu 	int tried_times = 0;
1648bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
1649b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
16506841ec68SYinghai Lu 	int retval;
165132180e40SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1652d61b0e87SYinghai Lu 				  IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
16536841ec68SYinghai Lu 
165432180e40SYinghai Lu again:
16558424d759SYinghai Lu 	__pci_bus_size_bridges(parent, &add_list);
1656bdc4abecSYinghai Lu 	__pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1657bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
165832180e40SYinghai Lu 	tried_times++;
165932180e40SYinghai Lu 
1660bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
16613f579c34SYinghai Lu 		goto enable_all;
166232180e40SYinghai Lu 
166332180e40SYinghai Lu 	if (tried_times >= 2) {
166432180e40SYinghai Lu 		/* still fail, don't need to try more */
1665bffc56d4SYinghai Lu 		free_list(&fail_head);
16663f579c34SYinghai Lu 		goto enable_all;
166732180e40SYinghai Lu 	}
166832180e40SYinghai Lu 
166932180e40SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
167032180e40SYinghai Lu 			 tried_times + 1);
167132180e40SYinghai Lu 
167232180e40SYinghai Lu 	/*
167332180e40SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
167432180e40SYinghai Lu 	 * child device under that bridge
167532180e40SYinghai Lu 	 */
167661e83cddSYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list)
167761e83cddSYinghai Lu 		pci_bus_release_bridge_resources(fail_res->dev->bus,
167861e83cddSYinghai Lu 						 fail_res->flags & type_mask,
167932180e40SYinghai Lu 						 whole_subtree);
168061e83cddSYinghai Lu 
168132180e40SYinghai Lu 	/* restore size and flags */
1682b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1683b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
168432180e40SYinghai Lu 
1685b9b0bba9SYinghai Lu 		res->start = fail_res->start;
1686b9b0bba9SYinghai Lu 		res->end = fail_res->end;
1687b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
1688b9b0bba9SYinghai Lu 		if (fail_res->dev->subordinate)
168932180e40SYinghai Lu 			res->flags = 0;
169032180e40SYinghai Lu 	}
1691bffc56d4SYinghai Lu 	free_list(&fail_head);
169232180e40SYinghai Lu 
169332180e40SYinghai Lu 	goto again;
16943f579c34SYinghai Lu 
16953f579c34SYinghai Lu enable_all:
16963f579c34SYinghai Lu 	retval = pci_reenable_device(bridge);
16979fc9eea0SBjorn Helgaas 	if (retval)
16989fc9eea0SBjorn Helgaas 		dev_err(&bridge->dev, "Error reenabling bridge (%d)\n", retval);
16993f579c34SYinghai Lu 	pci_set_master(bridge);
17006841ec68SYinghai Lu }
17016841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
17029b03088fSYinghai Lu 
170317787940SYinghai Lu void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
17049b03088fSYinghai Lu {
17059b03088fSYinghai Lu 	struct pci_dev *dev;
1706bdc4abecSYinghai Lu 	LIST_HEAD(add_list); /* list of resources that
17079b03088fSYinghai Lu 					want additional resources */
17089b03088fSYinghai Lu 
17099b03088fSYinghai Lu 	down_read(&pci_bus_sem);
17109b03088fSYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
17116788a51fSYijing Wang 		if (pci_is_bridge(dev) && pci_has_subordinate(dev))
17129b03088fSYinghai Lu 				__pci_bus_size_bridges(dev->subordinate,
17139b03088fSYinghai Lu 							 &add_list);
17149b03088fSYinghai Lu 	up_read(&pci_bus_sem);
17159b03088fSYinghai Lu 	__pci_bus_assign_resources(bus, &add_list, NULL);
1716bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
171717787940SYinghai Lu }
1718