xref: /openbmc/linux/drivers/pci/setup-bus.c (revision 7e24a55b2122746c2eef192296fc84624354f895)
17328c8f4SBjorn Helgaas // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
3df62ab5eSBjorn Helgaas  * Support routines for initializing a PCI subsystem
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Extruded from code written by
61da177e4SLinus Torvalds  *      Dave Rusling (david.rusling@reo.mts.dec.com)
71da177e4SLinus Torvalds  *      David Mosberger (davidm@cs.arizona.edu)
81da177e4SLinus Torvalds  *	David Miller (davem@redhat.com)
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
111da177e4SLinus Torvalds  *	     PCI-PCI bridges cleanup, sorted resource allocation.
121da177e4SLinus Torvalds  * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
131da177e4SLinus Torvalds  *	     Converted to allocation in 3 passes, which gives
141da177e4SLinus Torvalds  *	     tighter packing. Prefetchable range support.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/kernel.h>
191da177e4SLinus Torvalds #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/pci.h>
211da177e4SLinus Torvalds #include <linux/errno.h>
221da177e4SLinus Torvalds #include <linux/ioport.h>
231da177e4SLinus Torvalds #include <linux/cache.h>
241da177e4SLinus Torvalds #include <linux/slab.h>
25584c5c42SRui Wang #include <linux/acpi.h>
266faf17f6SChris Wright #include "pci.h"
271da177e4SLinus Torvalds 
28844393f4SBjorn Helgaas unsigned int pci_flags;
290c59c06aSRob Herring EXPORT_SYMBOL_GPL(pci_flags);
3047087700SBjorn Helgaas 
31bdc4abecSYinghai Lu struct pci_dev_resource {
32bdc4abecSYinghai Lu 	struct list_head list;
332934a0deSYinghai Lu 	struct resource *res;
342934a0deSYinghai Lu 	struct pci_dev *dev;
35568ddef8SYinghai Lu 	resource_size_t start;
36568ddef8SYinghai Lu 	resource_size_t end;
37c8adf9a3SRam Pai 	resource_size_t add_size;
382bbc6942SRam Pai 	resource_size_t min_align;
39568ddef8SYinghai Lu 	unsigned long flags;
40568ddef8SYinghai Lu };
41568ddef8SYinghai Lu 
free_list(struct list_head * head)42bffc56d4SYinghai Lu static void free_list(struct list_head *head)
43bffc56d4SYinghai Lu {
44bffc56d4SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
45bffc56d4SYinghai Lu 
46bffc56d4SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
47bffc56d4SYinghai Lu 		list_del(&dev_res->list);
48bffc56d4SYinghai Lu 		kfree(dev_res);
49bffc56d4SYinghai Lu 	}
50bffc56d4SYinghai Lu }
51094732a5SRam Pai 
52c8adf9a3SRam Pai /**
530d607618SNicholas Johnson  * add_to_list() - Add a new resource tracker to the list
54c8adf9a3SRam Pai  * @head:	Head of the list
550d607618SNicholas Johnson  * @dev:	Device to which the resource belongs
560d607618SNicholas Johnson  * @res:	Resource to be tracked
570d607618SNicholas Johnson  * @add_size:	Additional size to be optionally added to the resource
589b41d19aSKrzysztof Kozlowski  * @min_align:	Minimum memory window alignment
59c8adf9a3SRam Pai  */
add_to_list(struct list_head * head,struct pci_dev * dev,struct resource * res,resource_size_t add_size,resource_size_t min_align)600d607618SNicholas Johnson static int add_to_list(struct list_head *head, struct pci_dev *dev,
610d607618SNicholas Johnson 		       struct resource *res, resource_size_t add_size,
620d607618SNicholas Johnson 		       resource_size_t min_align)
63568ddef8SYinghai Lu {
64764242a0SYinghai Lu 	struct pci_dev_resource *tmp;
65568ddef8SYinghai Lu 
66bdc4abecSYinghai Lu 	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
67c7abb235SMarkus Elfring 	if (!tmp)
68ef62dfefSYinghai Lu 		return -ENOMEM;
69568ddef8SYinghai Lu 
70568ddef8SYinghai Lu 	tmp->res = res;
71568ddef8SYinghai Lu 	tmp->dev = dev;
72568ddef8SYinghai Lu 	tmp->start = res->start;
73568ddef8SYinghai Lu 	tmp->end = res->end;
74568ddef8SYinghai Lu 	tmp->flags = res->flags;
75c8adf9a3SRam Pai 	tmp->add_size = add_size;
762bbc6942SRam Pai 	tmp->min_align = min_align;
77bdc4abecSYinghai Lu 
78bdc4abecSYinghai Lu 	list_add(&tmp->list, head);
79ef62dfefSYinghai Lu 
80ef62dfefSYinghai Lu 	return 0;
81568ddef8SYinghai Lu }
82568ddef8SYinghai Lu 
remove_from_list(struct list_head * head,struct resource * res)830d607618SNicholas Johnson static void remove_from_list(struct list_head *head, struct resource *res)
843e6e0d80SYinghai Lu {
85b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
863e6e0d80SYinghai Lu 
87b9b0bba9SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
88b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
89b9b0bba9SYinghai Lu 			list_del(&dev_res->list);
90b9b0bba9SYinghai Lu 			kfree(dev_res);
91bdc4abecSYinghai Lu 			break;
923e6e0d80SYinghai Lu 		}
933e6e0d80SYinghai Lu 	}
943e6e0d80SYinghai Lu }
953e6e0d80SYinghai Lu 
res_to_dev_res(struct list_head * head,struct resource * res)96d74b9027SWei Yang static struct pci_dev_resource *res_to_dev_res(struct list_head *head,
971c372353SYinghai Lu 					       struct resource *res)
981c372353SYinghai Lu {
99b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res;
1001c372353SYinghai Lu 
101b9b0bba9SYinghai Lu 	list_for_each_entry(dev_res, head, list) {
10225e77388SBjorn Helgaas 		if (dev_res->res == res)
103d74b9027SWei Yang 			return dev_res;
104bdc4abecSYinghai Lu 	}
1051c372353SYinghai Lu 
106d74b9027SWei Yang 	return NULL;
1071c372353SYinghai Lu }
1081c372353SYinghai Lu 
get_res_add_size(struct list_head * head,struct resource * res)109d74b9027SWei Yang static resource_size_t get_res_add_size(struct list_head *head,
110d74b9027SWei Yang 					struct resource *res)
111d74b9027SWei Yang {
112d74b9027SWei Yang 	struct pci_dev_resource *dev_res;
113d74b9027SWei Yang 
114d74b9027SWei Yang 	dev_res = res_to_dev_res(head, res);
115d74b9027SWei Yang 	return dev_res ? dev_res->add_size : 0;
116d74b9027SWei Yang }
117d74b9027SWei Yang 
get_res_add_align(struct list_head * head,struct resource * res)118d74b9027SWei Yang static resource_size_t get_res_add_align(struct list_head *head,
119d74b9027SWei Yang 					 struct resource *res)
120d74b9027SWei Yang {
121d74b9027SWei Yang 	struct pci_dev_resource *dev_res;
122d74b9027SWei Yang 
123d74b9027SWei Yang 	dev_res = res_to_dev_res(head, res);
124d74b9027SWei Yang 	return dev_res ? dev_res->min_align : 0;
125d74b9027SWei Yang }
126d74b9027SWei Yang 
12778c3b329SYinghai Lu /* Sort resources by alignment */
pdev_sort_resources(struct pci_dev * dev,struct list_head * head)128bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
12978c3b329SYinghai Lu {
13009cc9006SMika Westerberg 	struct resource *r;
13178c3b329SYinghai Lu 	int i;
13278c3b329SYinghai Lu 
13309cc9006SMika Westerberg 	pci_dev_for_each_resource(dev, r, i) {
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 		if (r->flags & IORESOURCE_PCI_FIXED)
13978c3b329SYinghai Lu 			continue;
14078c3b329SYinghai Lu 
14178c3b329SYinghai Lu 		if (!(r->flags) || r->parent)
14278c3b329SYinghai Lu 			continue;
14378c3b329SYinghai Lu 
14478c3b329SYinghai Lu 		r_align = pci_resource_alignment(dev, r);
14578c3b329SYinghai Lu 		if (!r_align) {
1467506dc79SFrederick Lawler 			pci_warn(dev, "BAR %d: %pR has bogus alignment\n",
14778c3b329SYinghai Lu 				 i, r);
14878c3b329SYinghai Lu 			continue;
14978c3b329SYinghai Lu 		}
15078c3b329SYinghai Lu 
151bdc4abecSYinghai Lu 		tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
15278c3b329SYinghai Lu 		if (!tmp)
153c7c337c5SLiao Pingfang 			panic("%s: kzalloc() failed!\n", __func__);
15478c3b329SYinghai Lu 		tmp->res = r;
15578c3b329SYinghai Lu 		tmp->dev = dev;
156bdc4abecSYinghai Lu 
1570d607618SNicholas Johnson 		/* Fallback is smallest one or list is empty */
158bdc4abecSYinghai Lu 		n = head;
159bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
160bdc4abecSYinghai Lu 			resource_size_t align;
161bdc4abecSYinghai Lu 
162bdc4abecSYinghai Lu 			align = pci_resource_alignment(dev_res->dev,
163bdc4abecSYinghai Lu 							 dev_res->res);
164bdc4abecSYinghai Lu 
165bdc4abecSYinghai Lu 			if (r_align > align) {
166bdc4abecSYinghai Lu 				n = &dev_res->list;
16778c3b329SYinghai Lu 				break;
16878c3b329SYinghai Lu 			}
16978c3b329SYinghai Lu 		}
170bdc4abecSYinghai Lu 		/* Insert it just before n */
171bdc4abecSYinghai Lu 		list_add_tail(&tmp->list, n);
17278c3b329SYinghai Lu 	}
17378c3b329SYinghai Lu }
17478c3b329SYinghai Lu 
__dev_sort_resources(struct pci_dev * dev,struct list_head * head)1750d607618SNicholas Johnson static void __dev_sort_resources(struct pci_dev *dev, struct list_head *head)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	u16 class = dev->class >> 8;
1781da177e4SLinus Torvalds 
1790d607618SNicholas Johnson 	/* Don't touch classless devices or host bridges or IOAPICs */
1806841ec68SYinghai Lu 	if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
1816841ec68SYinghai Lu 		return;
1821da177e4SLinus Torvalds 
1830d607618SNicholas Johnson 	/* Don't touch IOAPIC devices already enabled by firmware */
18423186279SSatoru Takeuchi 	if (class == PCI_CLASS_SYSTEM_PIC) {
1859bded00bSKenji Kaneshige 		u16 command;
1869bded00bSKenji Kaneshige 		pci_read_config_word(dev, PCI_COMMAND, &command);
1879bded00bSKenji Kaneshige 		if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
1886841ec68SYinghai Lu 			return;
18923186279SSatoru Takeuchi 	}
19023186279SSatoru Takeuchi 
1916841ec68SYinghai Lu 	pdev_sort_resources(dev, head);
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
reset_resource(struct resource * res)194fc075e1dSRam Pai static inline void reset_resource(struct resource *res)
195fc075e1dSRam Pai {
196fc075e1dSRam Pai 	res->start = 0;
197fc075e1dSRam Pai 	res->end = 0;
198fc075e1dSRam Pai 	res->flags = 0;
199fc075e1dSRam Pai }
200fc075e1dSRam Pai 
201c8adf9a3SRam Pai /**
2020d607618SNicholas Johnson  * reassign_resources_sorted() - Satisfy any additional resource requests
203c8adf9a3SRam Pai  *
2040d607618SNicholas Johnson  * @realloc_head:	Head of the list tracking requests requiring
2050d607618SNicholas Johnson  *			additional resources
2060d607618SNicholas Johnson  * @head:		Head of the list tracking requests with allocated
207c8adf9a3SRam Pai  *			resources
208c8adf9a3SRam Pai  *
2090d607618SNicholas Johnson  * Walk through each element of the realloc_head and try to procure additional
2100d607618SNicholas Johnson  * resources for the element, provided the element is in the head list.
211c8adf9a3SRam Pai  */
reassign_resources_sorted(struct list_head * realloc_head,struct list_head * head)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;
218d74b9027SWei Yang 	resource_size_t add_size, align;
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;
2250d607618SNicholas Johnson 		/* Skip resource that has been reset */
226c8adf9a3SRam Pai 		if (!res->flags)
227c8adf9a3SRam Pai 			goto out;
228c8adf9a3SRam Pai 
2290d607618SNicholas Johnson 		/* 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 		}
2360d607618SNicholas Johnson 		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;
241d74b9027SWei Yang 		align = add_res->min_align;
2422bbc6942SRam Pai 		if (!resource_size(res)) {
243d74b9027SWei Yang 			res->start = align;
244c8adf9a3SRam Pai 			res->end = res->start + add_size - 1;
245b9b0bba9SYinghai Lu 			if (pci_assign_resource(add_res->dev, idx))
246c8adf9a3SRam Pai 				reset_resource(res);
2472bbc6942SRam Pai 		} else {
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))
25234c6b710SMohan Kumar 				pci_info(add_res->dev, "failed to add %llx res[%d]=%pR\n",
25334c6b710SMohan Kumar 					 (unsigned long long) add_size, idx,
25434c6b710SMohan Kumar 					 res);
255c8adf9a3SRam Pai 		}
256c8adf9a3SRam Pai out:
257b9b0bba9SYinghai Lu 		list_del(&add_res->list);
258b9b0bba9SYinghai Lu 		kfree(add_res);
259c8adf9a3SRam Pai 	}
260c8adf9a3SRam Pai }
261c8adf9a3SRam Pai 
262c8adf9a3SRam Pai /**
2630d607618SNicholas Johnson  * assign_requested_resources_sorted() - Satisfy resource requests
264c8adf9a3SRam Pai  *
2650d607618SNicholas Johnson  * @head:	Head of the list tracking requests for resources
2660d607618SNicholas Johnson  * @fail_head:	Head of the list tracking requests that could not be
2670d607618SNicholas Johnson  *		allocated
268c8adf9a3SRam Pai  *
2690d607618SNicholas Johnson  * Satisfy resource requests of each element in the list.  Add requests that
2700d607618SNicholas Johnson  * could not be satisfied to the failed_list.
271c8adf9a3SRam Pai  */
assign_requested_resources_sorted(struct list_head * head,struct list_head * fail_head)272bdc4abecSYinghai Lu static void assign_requested_resources_sorted(struct list_head *head,
273bdc4abecSYinghai Lu 				 struct list_head *fail_head)
2746841ec68SYinghai Lu {
2756841ec68SYinghai Lu 	struct resource *res;
276bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
2776841ec68SYinghai Lu 	int idx;
2786841ec68SYinghai Lu 
279bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
280bdc4abecSYinghai Lu 		res = dev_res->res;
281bdc4abecSYinghai Lu 		idx = res - &dev_res->dev->resource[0];
282bdc4abecSYinghai Lu 		if (resource_size(res) &&
283bdc4abecSYinghai Lu 		    pci_assign_resource(dev_res->dev, idx)) {
284a3cb999dSYinghai Lu 			if (fail_head) {
2859a928660SYinghai Lu 				/*
2860d607618SNicholas Johnson 				 * If the failed resource is a ROM BAR and
2870d607618SNicholas Johnson 				 * it will be enabled later, don't add it
2880d607618SNicholas Johnson 				 * 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 
pci_fail_res_type_mask(struct list_head * fail_head)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 
3070d607618SNicholas Johnson 	/* Check failed type */
308aa914f5eSYinghai Lu 	list_for_each_entry(fail_res, fail_head, list)
309aa914f5eSYinghai Lu 		mask |= fail_res->flags;
310aa914f5eSYinghai Lu 
311aa914f5eSYinghai Lu 	/*
3120d607618SNicholas Johnson 	 * One pref failed resource will set IORESOURCE_MEM, as we can
3130d607618SNicholas Johnson 	 * allocate pref in non-pref range.  Will release all assigned
3140d607618SNicholas Johnson 	 * non-pref sibling resources according to that bit.
315aa914f5eSYinghai Lu 	 */
316aa914f5eSYinghai Lu 	return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
317aa914f5eSYinghai Lu }
318aa914f5eSYinghai Lu 
pci_need_to_release(unsigned long mask,struct resource * res)319aa914f5eSYinghai Lu static bool pci_need_to_release(unsigned long mask, struct resource *res)
320aa914f5eSYinghai Lu {
321aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_IO)
322aa914f5eSYinghai Lu 		return !!(mask & IORESOURCE_IO);
323aa914f5eSYinghai Lu 
3240d607618SNicholas Johnson 	/* Check pref at first */
325aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_PREFETCH) {
326aa914f5eSYinghai Lu 		if (mask & IORESOURCE_PREFETCH)
327aa914f5eSYinghai Lu 			return true;
3280d607618SNicholas Johnson 		/* Count pref if its parent is non-pref */
329aa914f5eSYinghai Lu 		else if ((mask & IORESOURCE_MEM) &&
330aa914f5eSYinghai Lu 			 !(res->parent->flags & IORESOURCE_PREFETCH))
331aa914f5eSYinghai Lu 			return true;
332aa914f5eSYinghai Lu 		else
333aa914f5eSYinghai Lu 			return false;
334aa914f5eSYinghai Lu 	}
335aa914f5eSYinghai Lu 
336aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_MEM)
337aa914f5eSYinghai Lu 		return !!(mask & IORESOURCE_MEM);
338aa914f5eSYinghai Lu 
3390d607618SNicholas Johnson 	return false;	/* Should not get here */
340aa914f5eSYinghai Lu }
341aa914f5eSYinghai Lu 
__assign_resources_sorted(struct list_head * head,struct list_head * realloc_head,struct list_head * fail_head)342bdc4abecSYinghai Lu static void __assign_resources_sorted(struct list_head *head,
343bdc4abecSYinghai Lu 				      struct list_head *realloc_head,
344bdc4abecSYinghai Lu 				      struct list_head *fail_head)
345c8adf9a3SRam Pai {
3463e6e0d80SYinghai Lu 	/*
3470d607618SNicholas Johnson 	 * Should not assign requested resources at first.  They could be
3480d607618SNicholas Johnson 	 * adjacent, so later reassign can not reallocate them one by one in
3490d607618SNicholas Johnson 	 * parent resource window.
3500d607618SNicholas Johnson 	 *
3510d607618SNicholas Johnson 	 * Try to assign requested + add_size at beginning.  If could do that,
3520d607618SNicholas Johnson 	 * could get out early.  If could not do that, we still try to assign
3530d607618SNicholas Johnson 	 * requested at first, then try to reassign add_size for some resources.
354aa914f5eSYinghai Lu 	 *
355aa914f5eSYinghai Lu 	 * Separate three resource type checking if we need to release
356aa914f5eSYinghai Lu 	 * assigned resource after requested + add_size try.
3570d607618SNicholas Johnson 	 *
3580d607618SNicholas Johnson 	 *	1. If IO port assignment fails, will release assigned IO
3590d607618SNicholas Johnson 	 *	   port.
3600d607618SNicholas Johnson 	 *	2. If pref MMIO assignment fails, release assigned pref
3610d607618SNicholas Johnson 	 *	   MMIO.  If assigned pref MMIO's parent is non-pref MMIO
3620d607618SNicholas Johnson 	 *	   and non-pref MMIO assignment fails, will release that
3630d607618SNicholas Johnson 	 *	   assigned pref MMIO.
3640d607618SNicholas Johnson 	 *	3. If non-pref MMIO assignment fails or pref MMIO
3650d607618SNicholas Johnson 	 *	   assignment fails, will release assigned non-pref MMIO.
3663e6e0d80SYinghai Lu 	 */
367bdc4abecSYinghai Lu 	LIST_HEAD(save_head);
368bdc4abecSYinghai Lu 	LIST_HEAD(local_fail_head);
369b9b0bba9SYinghai Lu 	struct pci_dev_resource *save_res;
370d74b9027SWei Yang 	struct pci_dev_resource *dev_res, *tmp_res, *dev_res2;
371aa914f5eSYinghai Lu 	unsigned long fail_type;
372d74b9027SWei Yang 	resource_size_t add_align, align;
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 */
387d74b9027SWei Yang 	list_for_each_entry_safe(dev_res, tmp_res, head, list) {
388bdc4abecSYinghai Lu 		dev_res->res->end += get_res_add_size(realloc_head,
389bdc4abecSYinghai Lu 							dev_res->res);
3903e6e0d80SYinghai Lu 
391d74b9027SWei Yang 		/*
392d74b9027SWei Yang 		 * There are two kinds of additional resources in the list:
393d74b9027SWei Yang 		 * 1. bridge resource  -- IORESOURCE_STARTALIGN
394d74b9027SWei Yang 		 * 2. SR-IOV resource  -- IORESOURCE_SIZEALIGN
395d74b9027SWei Yang 		 * Here just fix the additional alignment for bridge
396d74b9027SWei Yang 		 */
397d74b9027SWei Yang 		if (!(dev_res->res->flags & IORESOURCE_STARTALIGN))
398d74b9027SWei Yang 			continue;
399d74b9027SWei Yang 
400d74b9027SWei Yang 		add_align = get_res_add_align(realloc_head, dev_res->res);
401d74b9027SWei Yang 
402d74b9027SWei Yang 		/*
4030d607618SNicholas Johnson 		 * The "head" list is sorted by alignment so resources with
4040d607618SNicholas Johnson 		 * bigger alignment will be assigned first.  After we
4050d607618SNicholas Johnson 		 * change the alignment of a dev_res in "head" list, we
4060d607618SNicholas Johnson 		 * need to reorder the list by alignment to make it
407d74b9027SWei Yang 		 * consistent.
408d74b9027SWei Yang 		 */
409d74b9027SWei Yang 		if (add_align > dev_res->res->start) {
410552bc94eSYinghai Lu 			resource_size_t r_size = resource_size(dev_res->res);
411552bc94eSYinghai Lu 
412d74b9027SWei Yang 			dev_res->res->start = add_align;
413552bc94eSYinghai Lu 			dev_res->res->end = add_align + r_size - 1;
414d74b9027SWei Yang 
415d74b9027SWei Yang 			list_for_each_entry(dev_res2, head, list) {
416d74b9027SWei Yang 				align = pci_resource_alignment(dev_res2->dev,
417d74b9027SWei Yang 							       dev_res2->res);
418a6b65983SWei Yang 				if (add_align > align) {
419d74b9027SWei Yang 					list_move_tail(&dev_res->list,
420d74b9027SWei Yang 						       &dev_res2->list);
421a6b65983SWei Yang 					break;
422a6b65983SWei Yang 				}
423d74b9027SWei Yang 			}
424d74b9027SWei Yang 		}
425d74b9027SWei Yang 
426d74b9027SWei Yang 	}
427d74b9027SWei Yang 
4283e6e0d80SYinghai Lu 	/* Try updated head list with add_size added */
4293e6e0d80SYinghai Lu 	assign_requested_resources_sorted(head, &local_fail_head);
4303e6e0d80SYinghai Lu 
4310d607618SNicholas Johnson 	/* All assigned with add_size? */
432bdc4abecSYinghai Lu 	if (list_empty(&local_fail_head)) {
4333e6e0d80SYinghai Lu 		/* Remove head list from realloc_head list */
434bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list)
435bdc4abecSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
436bffc56d4SYinghai Lu 		free_list(&save_head);
437bffc56d4SYinghai Lu 		free_list(head);
4383e6e0d80SYinghai Lu 		return;
4393e6e0d80SYinghai Lu 	}
4403e6e0d80SYinghai Lu 
4410d607618SNicholas Johnson 	/* Check failed type */
442aa914f5eSYinghai Lu 	fail_type = pci_fail_res_type_mask(&local_fail_head);
4430d607618SNicholas Johnson 	/* Remove not need to be released assigned res from head list etc */
444aa914f5eSYinghai Lu 	list_for_each_entry_safe(dev_res, tmp_res, head, list)
445aa914f5eSYinghai Lu 		if (dev_res->res->parent &&
446aa914f5eSYinghai Lu 		    !pci_need_to_release(fail_type, dev_res->res)) {
4470d607618SNicholas Johnson 			/* Remove it from realloc_head list */
448aa914f5eSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
449aa914f5eSYinghai Lu 			remove_from_list(&save_head, dev_res->res);
450aa914f5eSYinghai Lu 			list_del(&dev_res->list);
451aa914f5eSYinghai Lu 			kfree(dev_res);
452aa914f5eSYinghai Lu 		}
453aa914f5eSYinghai Lu 
454bffc56d4SYinghai Lu 	free_list(&local_fail_head);
4553e6e0d80SYinghai Lu 	/* Release assigned resource */
456bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list)
457bdc4abecSYinghai Lu 		if (dev_res->res->parent)
458bdc4abecSYinghai Lu 			release_resource(dev_res->res);
4593e6e0d80SYinghai Lu 	/* Restore start/end/flags from saved list */
460b9b0bba9SYinghai Lu 	list_for_each_entry(save_res, &save_head, list) {
461b9b0bba9SYinghai Lu 		struct resource *res = save_res->res;
4623e6e0d80SYinghai Lu 
463b9b0bba9SYinghai Lu 		res->start = save_res->start;
464b9b0bba9SYinghai Lu 		res->end = save_res->end;
465b9b0bba9SYinghai Lu 		res->flags = save_res->flags;
4663e6e0d80SYinghai Lu 	}
467bffc56d4SYinghai Lu 	free_list(&save_head);
4683e6e0d80SYinghai Lu 
4693e6e0d80SYinghai Lu requested_and_reassign:
470c8adf9a3SRam Pai 	/* Satisfy the must-have resource requests */
471c8adf9a3SRam Pai 	assign_requested_resources_sorted(head, fail_head);
472c8adf9a3SRam Pai 
4730d607618SNicholas Johnson 	/* Try to satisfy any additional optional resource requests */
4749e8bf93aSRam Pai 	if (realloc_head)
4759e8bf93aSRam Pai 		reassign_resources_sorted(realloc_head, head);
476bffc56d4SYinghai Lu 	free_list(head);
477c8adf9a3SRam Pai }
478c8adf9a3SRam Pai 
pdev_assign_resources_sorted(struct pci_dev * dev,struct list_head * add_head,struct list_head * fail_head)4796841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev,
480bdc4abecSYinghai Lu 					 struct list_head *add_head,
481bdc4abecSYinghai Lu 					 struct list_head *fail_head)
4826841ec68SYinghai Lu {
483bdc4abecSYinghai Lu 	LIST_HEAD(head);
4846841ec68SYinghai Lu 
4856841ec68SYinghai Lu 	__dev_sort_resources(dev, &head);
4868424d759SYinghai Lu 	__assign_resources_sorted(&head, add_head, fail_head);
4876841ec68SYinghai Lu 
4886841ec68SYinghai Lu }
4896841ec68SYinghai Lu 
pbus_assign_resources_sorted(const struct pci_bus * bus,struct list_head * realloc_head,struct list_head * fail_head)4906841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus,
491bdc4abecSYinghai Lu 					 struct list_head *realloc_head,
492bdc4abecSYinghai Lu 					 struct list_head *fail_head)
4936841ec68SYinghai Lu {
4946841ec68SYinghai Lu 	struct pci_dev *dev;
495bdc4abecSYinghai Lu 	LIST_HEAD(head);
4966841ec68SYinghai Lu 
4976841ec68SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
4986841ec68SYinghai Lu 		__dev_sort_resources(dev, &head);
4996841ec68SYinghai Lu 
5009e8bf93aSRam Pai 	__assign_resources_sorted(&head, realloc_head, fail_head);
5016841ec68SYinghai Lu }
5026841ec68SYinghai Lu 
pci_setup_cardbus(struct pci_bus * bus)503b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
506c7dabef8SBjorn Helgaas 	struct resource *res;
5071da177e4SLinus Torvalds 	struct pci_bus_region region;
5081da177e4SLinus Torvalds 
5097506dc79SFrederick Lawler 	pci_info(bridge, "CardBus bridge to %pR\n",
510b918c62eSYinghai Lu 		 &bus->busn_res);
5111da177e4SLinus Torvalds 
512c7dabef8SBjorn Helgaas 	res = bus->resource[0];
513fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
514c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5151da177e4SLinus Torvalds 		/*
5161da177e4SLinus Torvalds 		 * The IO resource is allocated a range twice as large as it
5171da177e4SLinus Torvalds 		 * would normally need.  This allows us to set both IO regs.
5181da177e4SLinus Torvalds 		 */
5197506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5201da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
5211da177e4SLinus Torvalds 					region.start);
5221da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
5231da177e4SLinus Torvalds 					region.end);
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds 
526c7dabef8SBjorn Helgaas 	res = bus->resource[1];
527fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
528c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5297506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5301da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
5311da177e4SLinus Torvalds 					region.start);
5321da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
5331da177e4SLinus Torvalds 					region.end);
5341da177e4SLinus Torvalds 	}
5351da177e4SLinus Torvalds 
536c7dabef8SBjorn Helgaas 	res = bus->resource[2];
537fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
538c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
5397506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5401da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
5411da177e4SLinus Torvalds 					region.start);
5421da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
5431da177e4SLinus Torvalds 					region.end);
5441da177e4SLinus Torvalds 	}
5451da177e4SLinus Torvalds 
546c7dabef8SBjorn Helgaas 	res = bus->resource[3];
547fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
548c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
5497506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5501da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
5511da177e4SLinus Torvalds 					region.start);
5521da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
5531da177e4SLinus Torvalds 					region.end);
5541da177e4SLinus Torvalds 	}
5551da177e4SLinus Torvalds }
556b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus);
5571da177e4SLinus Torvalds 
5580d607618SNicholas Johnson /*
5590d607618SNicholas Johnson  * Initialize bridges with base/limit values we have collected.  PCI-to-PCI
5600d607618SNicholas Johnson  * Bridge Architecture Specification rev. 1.1 (1998) requires that if there
5610d607618SNicholas Johnson  * are no I/O ports or memory behind the bridge, the corresponding range
5620d607618SNicholas Johnson  * must be turned off by writing base value greater than limit to the
5630d607618SNicholas Johnson  * bridge's base/limit registers.
5640d607618SNicholas Johnson  *
5650d607618SNicholas Johnson  * Note: care must be taken when updating I/O base/limit registers of
5660d607618SNicholas Johnson  * bridges which support 32-bit I/O.  This update requires two config space
5670d607618SNicholas Johnson  * writes, so it's quite possible that an I/O window of the bridge will
5680d607618SNicholas Johnson  * have some undesirable address (e.g. 0) after the first write.  Ditto
5690d607618SNicholas Johnson  * 64-bit prefetchable MMIO.
5700d607618SNicholas Johnson  */
pci_setup_bridge_io(struct pci_dev * bridge)5713f2f4dc4SYinghai Lu static void pci_setup_bridge_io(struct pci_dev *bridge)
5721da177e4SLinus Torvalds {
573c7dabef8SBjorn Helgaas 	struct resource *res;
5741da177e4SLinus Torvalds 	struct pci_bus_region region;
5752b28ae19SBjorn Helgaas 	unsigned long io_mask;
5762b28ae19SBjorn Helgaas 	u8 io_base_lo, io_limit_lo;
5775b764b83SBjorn Helgaas 	u16 l;
5785b764b83SBjorn Helgaas 	u32 io_upper16;
5791da177e4SLinus Torvalds 
5802b28ae19SBjorn Helgaas 	io_mask = PCI_IO_RANGE_MASK;
5812b28ae19SBjorn Helgaas 	if (bridge->io_window_1k)
5822b28ae19SBjorn Helgaas 		io_mask = PCI_IO_1K_RANGE_MASK;
5832b28ae19SBjorn Helgaas 
5840d607618SNicholas Johnson 	/* Set up the top and bottom of the PCI I/O segment for this bus */
5856e0688dbSKrzysztof Wilczynski 	res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
586fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
587c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5885b764b83SBjorn Helgaas 		pci_read_config_word(bridge, PCI_IO_BASE, &l);
5892b28ae19SBjorn Helgaas 		io_base_lo = (region.start >> 8) & io_mask;
5902b28ae19SBjorn Helgaas 		io_limit_lo = (region.end >> 8) & io_mask;
5915b764b83SBjorn Helgaas 		l = ((u16) io_limit_lo << 8) | io_base_lo;
5920d607618SNicholas Johnson 		/* Set up upper 16 bits of I/O base/limit */
5931da177e4SLinus Torvalds 		io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
5947506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
5957cc5997dSYinghai Lu 	} else {
5960d607618SNicholas Johnson 		/* Clear upper 16 bits of I/O base/limit */
5971da177e4SLinus Torvalds 		io_upper16 = 0;
5981da177e4SLinus Torvalds 		l = 0x00f0;
5991da177e4SLinus Torvalds 	}
6000d607618SNicholas Johnson 	/* Temporarily disable the I/O range before updating PCI_IO_BASE */
6011da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
6020d607618SNicholas Johnson 	/* Update lower 16 bits of I/O base/limit */
6035b764b83SBjorn Helgaas 	pci_write_config_word(bridge, PCI_IO_BASE, l);
6040d607618SNicholas Johnson 	/* Update upper 16 bits of I/O base/limit */
6051da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
6067cc5997dSYinghai Lu }
6071da177e4SLinus Torvalds 
pci_setup_bridge_mmio(struct pci_dev * bridge)6083f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio(struct pci_dev *bridge)
6097cc5997dSYinghai Lu {
6107cc5997dSYinghai Lu 	struct resource *res;
6117cc5997dSYinghai Lu 	struct pci_bus_region region;
6127cc5997dSYinghai Lu 	u32 l;
6137cc5997dSYinghai Lu 
6140d607618SNicholas Johnson 	/* Set up the top and bottom of the PCI Memory segment for this bus */
6156e0688dbSKrzysztof Wilczynski 	res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
616fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
617c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
6181da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
6191da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
6207506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
6217cc5997dSYinghai Lu 	} else {
6221da177e4SLinus Torvalds 		l = 0x0000fff0;
6231da177e4SLinus Torvalds 	}
6241da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
6257cc5997dSYinghai Lu }
6267cc5997dSYinghai Lu 
pci_setup_bridge_mmio_pref(struct pci_dev * bridge)6273f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge)
6287cc5997dSYinghai Lu {
6297cc5997dSYinghai Lu 	struct resource *res;
6307cc5997dSYinghai Lu 	struct pci_bus_region region;
6317cc5997dSYinghai Lu 	u32 l, bu, lu;
6321da177e4SLinus Torvalds 
6330d607618SNicholas Johnson 	/*
6340d607618SNicholas Johnson 	 * Clear out the upper 32 bits of PREF limit.  If
6350d607618SNicholas Johnson 	 * PCI_PREF_BASE_UPPER32 was non-zero, this temporarily disables
6360d607618SNicholas Johnson 	 * PREF range, which is ok.
6370d607618SNicholas Johnson 	 */
6381da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
6391da177e4SLinus Torvalds 
6400d607618SNicholas Johnson 	/* Set up PREF base/limit */
641c40a22e0SBenjamin Herrenschmidt 	bu = lu = 0;
6426e0688dbSKrzysztof Wilczynski 	res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
643fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
644c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_PREFETCH) {
6451da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
6461da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
647c7dabef8SBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64) {
64813d36c24SAndrew Morton 			bu = upper_32_bits(region.start);
64913d36c24SAndrew Morton 			lu = upper_32_bits(region.end);
6501f82de10SYinghai Lu 		}
6517506dc79SFrederick Lawler 		pci_info(bridge, "  bridge window %pR\n", res);
6527cc5997dSYinghai Lu 	} else {
6531da177e4SLinus Torvalds 		l = 0x0000fff0;
6541da177e4SLinus Torvalds 	}
6551da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
6561da177e4SLinus Torvalds 
6570d607618SNicholas Johnson 	/* Set the upper 32 bits of PREF base & limit */
658c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
659c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
6607cc5997dSYinghai Lu }
6617cc5997dSYinghai Lu 
__pci_setup_bridge(struct pci_bus * bus,unsigned long type)6627cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
6637cc5997dSYinghai Lu {
6647cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
6657cc5997dSYinghai Lu 
6667506dc79SFrederick Lawler 	pci_info(bridge, "PCI bridge to %pR\n",
667b918c62eSYinghai Lu 		 &bus->busn_res);
6687cc5997dSYinghai Lu 
6697cc5997dSYinghai Lu 	if (type & IORESOURCE_IO)
6703f2f4dc4SYinghai Lu 		pci_setup_bridge_io(bridge);
6717cc5997dSYinghai Lu 
6727cc5997dSYinghai Lu 	if (type & IORESOURCE_MEM)
6733f2f4dc4SYinghai Lu 		pci_setup_bridge_mmio(bridge);
6747cc5997dSYinghai Lu 
6757cc5997dSYinghai Lu 	if (type & IORESOURCE_PREFETCH)
6763f2f4dc4SYinghai Lu 		pci_setup_bridge_mmio_pref(bridge);
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
pcibios_setup_bridge(struct pci_bus * bus,unsigned long type)681d366d28cSGavin Shan void __weak pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
682d366d28cSGavin Shan {
683d366d28cSGavin Shan }
684d366d28cSGavin Shan 
pci_setup_bridge(struct pci_bus * bus)685e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus)
6867cc5997dSYinghai Lu {
6877cc5997dSYinghai Lu 	unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
6887cc5997dSYinghai Lu 				  IORESOURCE_PREFETCH;
6897cc5997dSYinghai Lu 
690d366d28cSGavin Shan 	pcibios_setup_bridge(bus, type);
6917cc5997dSYinghai Lu 	__pci_setup_bridge(bus, type);
6927cc5997dSYinghai Lu }
6937cc5997dSYinghai Lu 
6948505e729SYinghai Lu 
pci_claim_bridge_resource(struct pci_dev * bridge,int i)6958505e729SYinghai Lu int pci_claim_bridge_resource(struct pci_dev *bridge, int i)
6968505e729SYinghai Lu {
6978505e729SYinghai Lu 	if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END)
6988505e729SYinghai Lu 		return 0;
6998505e729SYinghai Lu 
7008505e729SYinghai Lu 	if (pci_claim_resource(bridge, i) == 0)
7010d607618SNicholas Johnson 		return 0;	/* Claimed the window */
7028505e729SYinghai Lu 
7038505e729SYinghai Lu 	if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
7048505e729SYinghai Lu 		return 0;
7058505e729SYinghai Lu 
7068505e729SYinghai Lu 	if (!pci_bus_clip_resource(bridge, i))
7070d607618SNicholas Johnson 		return -EINVAL;	/* Clipping didn't change anything */
7088505e729SYinghai Lu 
7096e0688dbSKrzysztof Wilczynski 	switch (i) {
7106e0688dbSKrzysztof Wilczynski 	case PCI_BRIDGE_IO_WINDOW:
7118505e729SYinghai Lu 		pci_setup_bridge_io(bridge);
7128505e729SYinghai Lu 		break;
7136e0688dbSKrzysztof Wilczynski 	case PCI_BRIDGE_MEM_WINDOW:
7148505e729SYinghai Lu 		pci_setup_bridge_mmio(bridge);
7158505e729SYinghai Lu 		break;
7166e0688dbSKrzysztof Wilczynski 	case PCI_BRIDGE_PREF_MEM_WINDOW:
7178505e729SYinghai Lu 		pci_setup_bridge_mmio_pref(bridge);
7188505e729SYinghai Lu 		break;
7198505e729SYinghai Lu 	default:
7208505e729SYinghai Lu 		return -EINVAL;
7218505e729SYinghai Lu 	}
7228505e729SYinghai Lu 
7238505e729SYinghai Lu 	if (pci_claim_resource(bridge, i) == 0)
7240d607618SNicholas Johnson 		return 0;	/* Claimed a smaller window */
7258505e729SYinghai Lu 
7268505e729SYinghai Lu 	return -EINVAL;
7278505e729SYinghai Lu }
7288505e729SYinghai Lu 
7290d607618SNicholas Johnson /*
7300d607618SNicholas Johnson  * Check whether the bridge supports optional I/O and prefetchable memory
7310d607618SNicholas Johnson  * ranges.  If not, the respective base/limit registers must be read-only
7320d607618SNicholas Johnson  * and read as 0.
7330d607618SNicholas Johnson  */
pci_bridge_check_ranges(struct pci_bus * bus)73496bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus)
7351da177e4SLinus Torvalds {
7361da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
7376e0688dbSKrzysztof Wilczynski 	struct resource *b_res;
7381da177e4SLinus Torvalds 
7396e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
7406e0688dbSKrzysztof Wilczynski 	b_res->flags |= IORESOURCE_MEM;
7411da177e4SLinus Torvalds 
7426e0688dbSKrzysztof Wilczynski 	if (bridge->io_window) {
7436e0688dbSKrzysztof Wilczynski 		b_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
7446e0688dbSKrzysztof Wilczynski 		b_res->flags |= IORESOURCE_IO;
7456e0688dbSKrzysztof Wilczynski 	}
746d2f54d9bSBjorn Helgaas 
74751c48b31SBjorn Helgaas 	if (bridge->pref_window) {
7486e0688dbSKrzysztof Wilczynski 		b_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
7496e0688dbSKrzysztof Wilczynski 		b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
75051c48b31SBjorn Helgaas 		if (bridge->pref_64_window) {
7516e0688dbSKrzysztof Wilczynski 			b_res->flags |= IORESOURCE_MEM_64 |
7526e0688dbSKrzysztof Wilczynski 					PCI_PREF_RANGE_TYPE_64;
75399586105SYinghai Lu 		}
7541f82de10SYinghai Lu 	}
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7570d607618SNicholas Johnson /*
758c13704f5SNicholas Johnson  * Helper function for sizing routines.  Assigned resources have non-NULL
759c13704f5SNicholas Johnson  * parent resource.
760c13704f5SNicholas Johnson  *
761c13704f5SNicholas Johnson  * Return first unassigned resource of the correct type.  If there is none,
762c13704f5SNicholas Johnson  * return first assigned resource of the correct type.  If none of the
763c13704f5SNicholas Johnson  * above, return NULL.
764c13704f5SNicholas Johnson  *
765c13704f5SNicholas Johnson  * Returning an assigned resource of the correct type allows the caller to
766c13704f5SNicholas Johnson  * distinguish between already assigned and no resource of the correct type.
7670d607618SNicholas Johnson  */
find_bus_resource_of_type(struct pci_bus * bus,unsigned long type_mask,unsigned long type)768c13704f5SNicholas Johnson static struct resource *find_bus_resource_of_type(struct pci_bus *bus,
7690d607618SNicholas Johnson 						  unsigned long type_mask,
7700d607618SNicholas Johnson 						  unsigned long type)
7711da177e4SLinus Torvalds {
772c13704f5SNicholas Johnson 	struct resource *r, *r_assigned = NULL;
7731da177e4SLinus Torvalds 
77402992064SAndy Shevchenko 	pci_bus_for_each_resource(bus, r) {
775299de034SIvan Kokshaysky 		if (r == &ioport_resource || r == &iomem_resource)
776299de034SIvan Kokshaysky 			continue;
77755a10984SJesse Barnes 		if (r && (r->flags & type_mask) == type && !r->parent)
7781da177e4SLinus Torvalds 			return r;
779c13704f5SNicholas Johnson 		if (r && (r->flags & type_mask) == type && !r_assigned)
780c13704f5SNicholas Johnson 			r_assigned = r;
7811da177e4SLinus Torvalds 	}
782c13704f5SNicholas Johnson 	return r_assigned;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
calculate_iosize(resource_size_t size,resource_size_t min_size,resource_size_t size1,resource_size_t add_size,resource_size_t children_add_size,resource_size_t old_size,resource_size_t align)78513583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size,
78613583b16SRam Pai 					resource_size_t min_size,
78713583b16SRam Pai 					resource_size_t size1,
788de3ffa30SJon Derrick 					resource_size_t add_size,
789de3ffa30SJon Derrick 					resource_size_t children_add_size,
79013583b16SRam Pai 					resource_size_t old_size,
79113583b16SRam Pai 					resource_size_t align)
79213583b16SRam Pai {
79313583b16SRam Pai 	if (size < min_size)
79413583b16SRam Pai 		size = min_size;
79513583b16SRam Pai 	if (old_size == 1)
79613583b16SRam Pai 		old_size = 0;
7970d607618SNicholas Johnson 	/*
7980d607618SNicholas Johnson 	 * To be fixed in 2.5: we should have sort of HAVE_ISA flag in the
7990d607618SNicholas Johnson 	 * struct pci_bus.
8000d607618SNicholas Johnson 	 */
80113583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
80213583b16SRam Pai 	size = (size & 0xff) + ((size & ~0xffUL) << 2);
80313583b16SRam Pai #endif
804de3ffa30SJon Derrick 	size = size + size1;
80513583b16SRam Pai 	if (size < old_size)
80613583b16SRam Pai 		size = old_size;
807de3ffa30SJon Derrick 
808de3ffa30SJon Derrick 	size = ALIGN(max(size, add_size) + children_add_size, align);
80913583b16SRam Pai 	return size;
81013583b16SRam Pai }
81113583b16SRam Pai 
calculate_memsize(resource_size_t size,resource_size_t min_size,resource_size_t add_size,resource_size_t children_add_size,resource_size_t old_size,resource_size_t align)81213583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size,
81313583b16SRam Pai 					 resource_size_t min_size,
814de3ffa30SJon Derrick 					 resource_size_t add_size,
815de3ffa30SJon Derrick 					 resource_size_t children_add_size,
81613583b16SRam Pai 					 resource_size_t old_size,
81713583b16SRam Pai 					 resource_size_t align)
81813583b16SRam Pai {
81913583b16SRam Pai 	if (size < min_size)
82013583b16SRam Pai 		size = min_size;
82113583b16SRam Pai 	if (old_size == 1)
82213583b16SRam Pai 		old_size = 0;
823de3ffa30SJon Derrick 
824*4cd2eca0SIlpo Järvinen 	size = max(size, add_size) + children_add_size;
825*4cd2eca0SIlpo Järvinen 	return ALIGN(max(size, old_size), align);
82613583b16SRam Pai }
82713583b16SRam Pai 
pcibios_window_alignment(struct pci_bus * bus,unsigned long type)828ac5ad93eSGavin Shan resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
829ac5ad93eSGavin Shan 						unsigned long type)
830ac5ad93eSGavin Shan {
831ac5ad93eSGavin Shan 	return 1;
832ac5ad93eSGavin Shan }
833ac5ad93eSGavin Shan 
834ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_MEM_ALIGN	0x100000	/* 1MiB */
835ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN	0x1000		/* 4KiB */
836ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN_1K	0x400		/* 1KiB */
837ac5ad93eSGavin Shan 
window_alignment(struct pci_bus * bus,unsigned long type)8380d607618SNicholas Johnson static resource_size_t window_alignment(struct pci_bus *bus, unsigned long type)
839ac5ad93eSGavin Shan {
840ac5ad93eSGavin Shan 	resource_size_t align = 1, arch_align;
841ac5ad93eSGavin Shan 
842ac5ad93eSGavin Shan 	if (type & IORESOURCE_MEM)
843ac5ad93eSGavin Shan 		align = PCI_P2P_DEFAULT_MEM_ALIGN;
844ac5ad93eSGavin Shan 	else if (type & IORESOURCE_IO) {
845ac5ad93eSGavin Shan 		/*
8460d607618SNicholas Johnson 		 * Per spec, I/O windows are 4K-aligned, but some bridges have
8470d607618SNicholas Johnson 		 * an extension to support 1K alignment.
848ac5ad93eSGavin Shan 		 */
8492c8d5a2dSIvan Kokshaysky 		if (bus->self && bus->self->io_window_1k)
850ac5ad93eSGavin Shan 			align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
851ac5ad93eSGavin Shan 		else
852ac5ad93eSGavin Shan 			align = PCI_P2P_DEFAULT_IO_ALIGN;
853ac5ad93eSGavin Shan 	}
854ac5ad93eSGavin Shan 
855ac5ad93eSGavin Shan 	arch_align = pcibios_window_alignment(bus, type);
856ac5ad93eSGavin Shan 	return max(align, arch_align);
857ac5ad93eSGavin Shan }
858ac5ad93eSGavin Shan 
859c8adf9a3SRam Pai /**
8600d607618SNicholas Johnson  * pbus_size_io() - Size the I/O window of a given bus
861c8adf9a3SRam Pai  *
8620d607618SNicholas Johnson  * @bus:		The bus
8630d607618SNicholas Johnson  * @min_size:		The minimum I/O window that must be allocated
8640d607618SNicholas Johnson  * @add_size:		Additional optional I/O window
8650d607618SNicholas Johnson  * @realloc_head:	Track the additional I/O window on this list
866c8adf9a3SRam Pai  *
8670d607618SNicholas Johnson  * Sizing the I/O windows of the PCI-PCI bridge is trivial, since these
8680d607618SNicholas Johnson  * windows have 1K or 4K granularity and the I/O ranges of non-bridge PCI
8690d607618SNicholas Johnson  * devices are limited to 256 bytes.  We must be careful with the ISA
8700d607618SNicholas Johnson  * aliasing though.
871c8adf9a3SRam Pai  */
pbus_size_io(struct pci_bus * bus,resource_size_t min_size,resource_size_t add_size,struct list_head * realloc_head)872c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
8730d607618SNicholas Johnson 			 resource_size_t add_size,
8740d607618SNicholas Johnson 			 struct list_head *realloc_head)
8751da177e4SLinus Torvalds {
8761da177e4SLinus Torvalds 	struct pci_dev *dev;
877c13704f5SNicholas Johnson 	struct resource *b_res = find_bus_resource_of_type(bus, IORESOURCE_IO,
8785b285415SYinghai Lu 							   IORESOURCE_IO);
87911251a86SWei Yang 	resource_size_t size = 0, size0 = 0, size1 = 0;
880be768912SYinghai Lu 	resource_size_t children_add_size = 0;
8812d1d6678SBjorn Helgaas 	resource_size_t min_align, align;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	if (!b_res)
8841da177e4SLinus Torvalds 		return;
8851da177e4SLinus Torvalds 
886c13704f5SNicholas Johnson 	/* If resource is already assigned, nothing more to do */
887c13704f5SNicholas Johnson 	if (b_res->parent)
888c13704f5SNicholas Johnson 		return;
889c13704f5SNicholas Johnson 
8902d1d6678SBjorn Helgaas 	min_align = window_alignment(bus, IORESOURCE_IO);
8911da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
89209cc9006SMika Westerberg 		struct resource *r;
8931da177e4SLinus Torvalds 
89409cc9006SMika Westerberg 		pci_dev_for_each_resource(dev, r) {
8951da177e4SLinus Torvalds 			unsigned long r_size;
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 			if (r->parent || !(r->flags & IORESOURCE_IO))
8981da177e4SLinus Torvalds 				continue;
899022edd86SZhao, Yu 			r_size = resource_size(r);
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 			if (r_size < 0x400)
9021da177e4SLinus Torvalds 				/* Might be re-aligned for ISA */
9031da177e4SLinus Torvalds 				size += r_size;
9041da177e4SLinus Torvalds 			else
9051da177e4SLinus Torvalds 				size1 += r_size;
906be768912SYinghai Lu 
907fd591341SYinghai Lu 			align = pci_resource_alignment(dev, r);
908fd591341SYinghai Lu 			if (align > min_align)
909fd591341SYinghai Lu 				min_align = align;
910fd591341SYinghai Lu 
9119e8bf93aSRam Pai 			if (realloc_head)
9129e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
9131da177e4SLinus Torvalds 		}
9141da177e4SLinus Torvalds 	}
915fd591341SYinghai Lu 
916de3ffa30SJon Derrick 	size0 = calculate_iosize(size, min_size, size1, 0, 0,
917fd591341SYinghai Lu 			resource_size(b_res), min_align);
918de3ffa30SJon Derrick 	size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
919de3ffa30SJon Derrick 		calculate_iosize(size, min_size, size1, add_size, children_add_size,
920fd591341SYinghai Lu 			resource_size(b_res), min_align);
921c8adf9a3SRam Pai 	if (!size0 && !size1) {
9222c8d5a2dSIvan Kokshaysky 		if (bus->self && (b_res->start || b_res->end))
9237506dc79SFrederick Lawler 			pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
924227f0647SRyan Desfosses 				 b_res, &bus->busn_res);
9251da177e4SLinus Torvalds 		b_res->flags = 0;
9261da177e4SLinus Torvalds 		return;
9271da177e4SLinus Torvalds 	}
928fd591341SYinghai Lu 
929fd591341SYinghai Lu 	b_res->start = min_align;
930c8adf9a3SRam Pai 	b_res->end = b_res->start + size0 - 1;
93188452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
9322c8d5a2dSIvan Kokshaysky 	if (bus->self && size1 > size0 && realloc_head) {
933fd591341SYinghai Lu 		add_to_list(realloc_head, bus->self, b_res, size1-size0,
934fd591341SYinghai Lu 			    min_align);
93534c6b710SMohan Kumar 		pci_info(bus->self, "bridge window %pR to %pR add_size %llx\n",
936227f0647SRyan Desfosses 			 b_res, &bus->busn_res,
93711251a86SWei Yang 			 (unsigned long long) size1 - size0);
938b592443dSYinghai Lu 	}
9391da177e4SLinus Torvalds }
9401da177e4SLinus Torvalds 
calculate_mem_align(resource_size_t * aligns,int max_order)941c121504eSGavin Shan static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
942c121504eSGavin Shan 						  int max_order)
943c121504eSGavin Shan {
944c121504eSGavin Shan 	resource_size_t align = 0;
945c121504eSGavin Shan 	resource_size_t min_align = 0;
946c121504eSGavin Shan 	int order;
947c121504eSGavin Shan 
948c121504eSGavin Shan 	for (order = 0; order <= max_order; order++) {
949c121504eSGavin Shan 		resource_size_t align1 = 1;
950c121504eSGavin Shan 
951c121504eSGavin Shan 		align1 <<= (order + 20);
952c121504eSGavin Shan 
953c121504eSGavin Shan 		if (!align)
954c121504eSGavin Shan 			min_align = align1;
955c121504eSGavin Shan 		else if (ALIGN(align + min_align, min_align) < align1)
956c121504eSGavin Shan 			min_align = align1 >> 1;
957c121504eSGavin Shan 		align += aligns[order];
958c121504eSGavin Shan 	}
959c121504eSGavin Shan 
960c121504eSGavin Shan 	return min_align;
961c121504eSGavin Shan }
962c121504eSGavin Shan 
963c8adf9a3SRam Pai /**
9640d607618SNicholas Johnson  * pbus_size_mem() - Size the memory window of a given bus
965c8adf9a3SRam Pai  *
9660d607618SNicholas Johnson  * @bus:		The bus
9670d607618SNicholas Johnson  * @mask:		Mask the resource flag, then compare it with type
9680d607618SNicholas Johnson  * @type:		The type of free resource from bridge
9690d607618SNicholas Johnson  * @type2:		Second match type
9700d607618SNicholas Johnson  * @type3:		Third match type
9710d607618SNicholas Johnson  * @min_size:		The minimum memory window that must be allocated
9720d607618SNicholas Johnson  * @add_size:		Additional optional memory window
9730d607618SNicholas Johnson  * @realloc_head:	Track the additional memory window on this list
974c8adf9a3SRam Pai  *
9750d607618SNicholas Johnson  * Calculate the size of the bus and minimal alignment which guarantees
9760d607618SNicholas Johnson  * that all child resources fit in this size.
97730afe8d0SBjorn Helgaas  *
9780d607618SNicholas Johnson  * Return -ENOSPC if there's no available bus resource of the desired
9790d607618SNicholas Johnson  * type.  Otherwise, set the bus resource start/end to indicate the
9800d607618SNicholas Johnson  * required size, add things to realloc_head (if supplied), and return 0.
981c8adf9a3SRam Pai  */
pbus_size_mem(struct pci_bus * bus,unsigned long mask,unsigned long type,unsigned long type2,unsigned long type3,resource_size_t min_size,resource_size_t add_size,struct list_head * realloc_head)98228760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
9835b285415SYinghai Lu 			 unsigned long type, unsigned long type2,
9840d607618SNicholas Johnson 			 unsigned long type3, resource_size_t min_size,
9850d607618SNicholas Johnson 			 resource_size_t add_size,
986bdc4abecSYinghai Lu 			 struct list_head *realloc_head)
9871da177e4SLinus Torvalds {
9881da177e4SLinus Torvalds 	struct pci_dev *dev;
989c8adf9a3SRam Pai 	resource_size_t min_align, align, size, size0, size1;
9903dc8a1f6SDongdong Liu 	resource_size_t aligns[24]; /* Alignments from 1MB to 8TB */
9911da177e4SLinus Torvalds 	int order, max_order;
992c13704f5SNicholas Johnson 	struct resource *b_res = find_bus_resource_of_type(bus,
9935b285415SYinghai Lu 					mask | IORESOURCE_PREFETCH, type);
994be768912SYinghai Lu 	resource_size_t children_add_size = 0;
995d74b9027SWei Yang 	resource_size_t children_add_align = 0;
996d74b9027SWei Yang 	resource_size_t add_align = 0;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	if (!b_res)
99930afe8d0SBjorn Helgaas 		return -ENOSPC;
10001da177e4SLinus Torvalds 
1001c13704f5SNicholas Johnson 	/* If resource is already assigned, nothing more to do */
1002c13704f5SNicholas Johnson 	if (b_res->parent)
1003c13704f5SNicholas Johnson 		return 0;
1004c13704f5SNicholas Johnson 
10051da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
10061da177e4SLinus Torvalds 	max_order = 0;
10071da177e4SLinus Torvalds 	size = 0;
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
101009cc9006SMika Westerberg 		struct resource *r;
10111da177e4SLinus Torvalds 		int i;
10121da177e4SLinus Torvalds 
101309cc9006SMika Westerberg 		pci_dev_for_each_resource(dev, r, i) {
1014c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
10151da177e4SLinus Torvalds 
1016a2220d80SDavid Daney 			if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) ||
1017a2220d80SDavid Daney 			    ((r->flags & mask) != type &&
10185b285415SYinghai Lu 			     (r->flags & mask) != type2 &&
10195b285415SYinghai Lu 			     (r->flags & mask) != type3))
10201da177e4SLinus Torvalds 				continue;
1021022edd86SZhao, Yu 			r_size = resource_size(r);
10222aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV
10230d607618SNicholas Johnson 			/* Put SRIOV requested res to the optional list */
10249e8bf93aSRam Pai 			if (realloc_head && i >= PCI_IOV_RESOURCES &&
10252aceefcbSYinghai Lu 					i <= PCI_IOV_RESOURCE_END) {
1026d74b9027SWei Yang 				add_align = max(pci_resource_alignment(dev, r), add_align);
10272aceefcbSYinghai Lu 				r->end = r->start - 1;
10280d607618SNicholas Johnson 				add_to_list(realloc_head, dev, r, r_size, 0 /* Don't care */);
10292aceefcbSYinghai Lu 				children_add_size += r_size;
10302aceefcbSYinghai Lu 				continue;
10312aceefcbSYinghai Lu 			}
10322aceefcbSYinghai Lu #endif
103314c8530dSAlan 			/*
103414c8530dSAlan 			 * aligns[0] is for 1MB (since bridge memory
103514c8530dSAlan 			 * windows are always at least 1MB aligned), so
103614c8530dSAlan 			 * keep "order" from being negative for smaller
103714c8530dSAlan 			 * resources.
103814c8530dSAlan 			 */
10396faf17f6SChris Wright 			align = pci_resource_alignment(dev, r);
10401da177e4SLinus Torvalds 			order = __ffs(align) - 20;
104114c8530dSAlan 			if (order < 0)
104214c8530dSAlan 				order = 0;
104314c8530dSAlan 			if (order >= ARRAY_SIZE(aligns)) {
10447506dc79SFrederick Lawler 				pci_warn(dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
1045227f0647SRyan Desfosses 					 i, r, (unsigned long long) align);
10461da177e4SLinus Torvalds 				r->flags = 0;
10471da177e4SLinus Torvalds 				continue;
10481da177e4SLinus Torvalds 			}
1049c9c75143SYongji Xie 			size += max(r_size, align);
10500d607618SNicholas Johnson 			/*
10510d607618SNicholas Johnson 			 * Exclude ranges with size > align from calculation of
10520d607618SNicholas Johnson 			 * the alignment.
10530d607618SNicholas Johnson 			 */
1054c9c75143SYongji Xie 			if (r_size <= align)
10551da177e4SLinus Torvalds 				aligns[order] += align;
10561da177e4SLinus Torvalds 			if (order > max_order)
10571da177e4SLinus Torvalds 				max_order = order;
1058be768912SYinghai Lu 
1059d74b9027SWei Yang 			if (realloc_head) {
10609e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
1061d74b9027SWei Yang 				children_add_align = get_res_add_align(realloc_head, r);
1062d74b9027SWei Yang 				add_align = max(add_align, children_add_align);
1063d74b9027SWei Yang 			}
10641da177e4SLinus Torvalds 		}
10651da177e4SLinus Torvalds 	}
10668308c54dSJeremy Fitzhardinge 
1067c121504eSGavin Shan 	min_align = calculate_mem_align(aligns, max_order);
10683ad94b0dSWei Yang 	min_align = max(min_align, window_alignment(bus, b_res->flags));
1069de3ffa30SJon Derrick 	size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), min_align);
1070d74b9027SWei Yang 	add_align = max(min_align, add_align);
1071de3ffa30SJon Derrick 	size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
1072de3ffa30SJon Derrick 		calculate_memsize(size, min_size, add_size, children_add_size,
1073d74b9027SWei Yang 				resource_size(b_res), add_align);
1074c8adf9a3SRam Pai 	if (!size0 && !size1) {
10752c8d5a2dSIvan Kokshaysky 		if (bus->self && (b_res->start || b_res->end))
10767506dc79SFrederick Lawler 			pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
1077227f0647SRyan Desfosses 				 b_res, &bus->busn_res);
10781da177e4SLinus Torvalds 		b_res->flags = 0;
107930afe8d0SBjorn Helgaas 		return 0;
10801da177e4SLinus Torvalds 	}
10811da177e4SLinus Torvalds 	b_res->start = min_align;
1082c8adf9a3SRam Pai 	b_res->end = size0 + min_align - 1;
10835b285415SYinghai Lu 	b_res->flags |= IORESOURCE_STARTALIGN;
10842c8d5a2dSIvan Kokshaysky 	if (bus->self && size1 > size0 && realloc_head) {
1085d74b9027SWei Yang 		add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align);
108634c6b710SMohan Kumar 		pci_info(bus->self, "bridge window %pR to %pR add_size %llx add_align %llx\n",
1087227f0647SRyan Desfosses 			   b_res, &bus->busn_res,
1088d74b9027SWei Yang 			   (unsigned long long) (size1 - size0),
1089d74b9027SWei Yang 			   (unsigned long long) add_align);
1090b592443dSYinghai Lu 	}
109130afe8d0SBjorn Helgaas 	return 0;
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
pci_cardbus_resource_alignment(struct resource * res)10940a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res)
10950a2daa1cSRam Pai {
10960a2daa1cSRam Pai 	if (res->flags & IORESOURCE_IO)
10970a2daa1cSRam Pai 		return pci_cardbus_io_size;
10980a2daa1cSRam Pai 	if (res->flags & IORESOURCE_MEM)
10990a2daa1cSRam Pai 		return pci_cardbus_mem_size;
11000a2daa1cSRam Pai 	return 0;
11010a2daa1cSRam Pai }
11020a2daa1cSRam Pai 
pci_bus_size_cardbus(struct pci_bus * bus,struct list_head * realloc_head)11030a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus,
1104bdc4abecSYinghai Lu 				 struct list_head *realloc_head)
11051da177e4SLinus Torvalds {
11061da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
11076e0688dbSKrzysztof Wilczynski 	struct resource *b_res;
110811848934SYinghai Lu 	resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
11091da177e4SLinus Torvalds 	u16 ctrl;
11101da177e4SLinus Torvalds 
11116e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_CB_BRIDGE_IO_0_WINDOW];
11126e0688dbSKrzysztof Wilczynski 	if (b_res->parent)
11133796f1e2SYinghai Lu 		goto handle_b_res_1;
11141da177e4SLinus Torvalds 	/*
11150d607618SNicholas Johnson 	 * Reserve some resources for CardBus.  We reserve a fixed amount
11160d607618SNicholas Johnson 	 * of bus space for CardBus bridges.
11171da177e4SLinus Torvalds 	 */
11186e0688dbSKrzysztof Wilczynski 	b_res->start = pci_cardbus_io_size;
11196e0688dbSKrzysztof Wilczynski 	b_res->end = b_res->start + pci_cardbus_io_size - 1;
11206e0688dbSKrzysztof Wilczynski 	b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
112111848934SYinghai Lu 	if (realloc_head) {
11226e0688dbSKrzysztof Wilczynski 		b_res->end -= pci_cardbus_io_size;
112311848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
112411848934SYinghai Lu 			    pci_cardbus_io_size);
112511848934SYinghai Lu 	}
11261da177e4SLinus Torvalds 
11273796f1e2SYinghai Lu handle_b_res_1:
11286e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_CB_BRIDGE_IO_1_WINDOW];
11296e0688dbSKrzysztof Wilczynski 	if (b_res->parent)
11303796f1e2SYinghai Lu 		goto handle_b_res_2;
11316e0688dbSKrzysztof Wilczynski 	b_res->start = pci_cardbus_io_size;
11326e0688dbSKrzysztof Wilczynski 	b_res->end = b_res->start + pci_cardbus_io_size - 1;
11336e0688dbSKrzysztof Wilczynski 	b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
113411848934SYinghai Lu 	if (realloc_head) {
11356e0688dbSKrzysztof Wilczynski 		b_res->end -= pci_cardbus_io_size;
11366e0688dbSKrzysztof Wilczynski 		add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
113711848934SYinghai Lu 			    pci_cardbus_io_size);
113811848934SYinghai Lu 	}
11391da177e4SLinus Torvalds 
11403796f1e2SYinghai Lu handle_b_res_2:
11410d607618SNicholas Johnson 	/* MEM1 must not be pref MMIO */
1142dcef0d06SYinghai Lu 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1143dcef0d06SYinghai Lu 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
1144dcef0d06SYinghai Lu 		ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
1145dcef0d06SYinghai Lu 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1146dcef0d06SYinghai Lu 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1147dcef0d06SYinghai Lu 	}
1148dcef0d06SYinghai Lu 
11490d607618SNicholas Johnson 	/* Check whether prefetchable memory is supported by this bridge. */
11501da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
11511da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
11521da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
11531da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
11541da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
11551da177e4SLinus Torvalds 	}
11561da177e4SLinus Torvalds 
11576e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_0_WINDOW];
11586e0688dbSKrzysztof Wilczynski 	if (b_res->parent)
11593796f1e2SYinghai Lu 		goto handle_b_res_3;
11601da177e4SLinus Torvalds 	/*
11610d607618SNicholas Johnson 	 * If we have prefetchable memory support, allocate two regions.
11620d607618SNicholas Johnson 	 * Otherwise, allocate one region of twice the size.
11631da177e4SLinus Torvalds 	 */
11641da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
11656e0688dbSKrzysztof Wilczynski 		b_res->start = pci_cardbus_mem_size;
11666e0688dbSKrzysztof Wilczynski 		b_res->end = b_res->start + pci_cardbus_mem_size - 1;
11676e0688dbSKrzysztof Wilczynski 		b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
116811848934SYinghai Lu 				    IORESOURCE_STARTALIGN;
116911848934SYinghai Lu 		if (realloc_head) {
11706e0688dbSKrzysztof Wilczynski 			b_res->end -= pci_cardbus_mem_size;
11716e0688dbSKrzysztof Wilczynski 			add_to_list(realloc_head, bridge, b_res,
117211848934SYinghai Lu 				    pci_cardbus_mem_size, pci_cardbus_mem_size);
11731da177e4SLinus Torvalds 		}
11740a2daa1cSRam Pai 
11750d607618SNicholas Johnson 		/* Reduce that to half */
117611848934SYinghai Lu 		b_res_3_size = pci_cardbus_mem_size;
117711848934SYinghai Lu 	}
117811848934SYinghai Lu 
11793796f1e2SYinghai Lu handle_b_res_3:
11806e0688dbSKrzysztof Wilczynski 	b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_1_WINDOW];
11816e0688dbSKrzysztof Wilczynski 	if (b_res->parent)
11823796f1e2SYinghai Lu 		goto handle_done;
11836e0688dbSKrzysztof Wilczynski 	b_res->start = pci_cardbus_mem_size;
11846e0688dbSKrzysztof Wilczynski 	b_res->end = b_res->start + b_res_3_size - 1;
11856e0688dbSKrzysztof Wilczynski 	b_res->flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
118611848934SYinghai Lu 	if (realloc_head) {
11876e0688dbSKrzysztof Wilczynski 		b_res->end -= b_res_3_size;
11886e0688dbSKrzysztof Wilczynski 		add_to_list(realloc_head, bridge, b_res, b_res_3_size,
118911848934SYinghai Lu 			    pci_cardbus_mem_size);
119011848934SYinghai Lu 	}
11913796f1e2SYinghai Lu 
11923796f1e2SYinghai Lu handle_done:
11933796f1e2SYinghai Lu 	;
11941da177e4SLinus Torvalds }
11951da177e4SLinus Torvalds 
__pci_bus_size_bridges(struct pci_bus * bus,struct list_head * realloc_head)119610874f5aSBjorn Helgaas void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
11971da177e4SLinus Torvalds {
11981da177e4SLinus Torvalds 	struct pci_dev *dev;
11995b285415SYinghai Lu 	unsigned long mask, prefmask, type2 = 0, type3 = 0;
1200d7b8a217SNicholas Johnson 	resource_size_t additional_io_size = 0, additional_mmio_size = 0,
1201d7b8a217SNicholas Johnson 			additional_mmio_pref_size = 0;
12022c8d5a2dSIvan Kokshaysky 	struct resource *pref;
12032c8d5a2dSIvan Kokshaysky 	struct pci_host_bridge *host;
120402992064SAndy Shevchenko 	int hdr_type, ret;
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
12071da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
12081da177e4SLinus Torvalds 		if (!b)
12091da177e4SLinus Torvalds 			continue;
12101da177e4SLinus Torvalds 
1211b2fb5cc5SHonghui Zhang 		switch (dev->hdr_type) {
1212b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_CARDBUS:
12139e8bf93aSRam Pai 			pci_bus_size_cardbus(b, realloc_head);
12141da177e4SLinus Torvalds 			break;
12151da177e4SLinus Torvalds 
1216b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_BRIDGE:
12171da177e4SLinus Torvalds 		default:
12189e8bf93aSRam Pai 			__pci_bus_size_bridges(b, realloc_head);
12191da177e4SLinus Torvalds 			break;
12201da177e4SLinus Torvalds 		}
12211da177e4SLinus Torvalds 	}
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 	/* The root bus? */
12242c8d5a2dSIvan Kokshaysky 	if (pci_is_root_bus(bus)) {
12252c8d5a2dSIvan Kokshaysky 		host = to_pci_host_bridge(bus->bridge);
12262c8d5a2dSIvan Kokshaysky 		if (!host->size_windows)
12271da177e4SLinus Torvalds 			return;
122802992064SAndy Shevchenko 		pci_bus_for_each_resource(bus, pref)
12292c8d5a2dSIvan Kokshaysky 			if (pref && (pref->flags & IORESOURCE_PREFETCH))
12302c8d5a2dSIvan Kokshaysky 				break;
12312c8d5a2dSIvan Kokshaysky 		hdr_type = -1;	/* Intentionally invalid - not a PCI device. */
12322c8d5a2dSIvan Kokshaysky 	} else {
12336e0688dbSKrzysztof Wilczynski 		pref = &bus->self->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
12342c8d5a2dSIvan Kokshaysky 		hdr_type = bus->self->hdr_type;
12352c8d5a2dSIvan Kokshaysky 	}
12361da177e4SLinus Torvalds 
12372c8d5a2dSIvan Kokshaysky 	switch (hdr_type) {
1238b2fb5cc5SHonghui Zhang 	case PCI_HEADER_TYPE_CARDBUS:
12390d607618SNicholas Johnson 		/* Don't size CardBuses yet */
12401da177e4SLinus Torvalds 		break;
12411da177e4SLinus Torvalds 
1242b2fb5cc5SHonghui Zhang 	case PCI_HEADER_TYPE_BRIDGE:
12431da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
124428760489SEric W. Biederman 		if (bus->self->is_hotplug_bridge) {
1245c8adf9a3SRam Pai 			additional_io_size  = pci_hotplug_io_size;
1246d7b8a217SNicholas Johnson 			additional_mmio_size = pci_hotplug_mmio_size;
1247d7b8a217SNicholas Johnson 			additional_mmio_pref_size = pci_hotplug_mmio_pref_size;
124828760489SEric W. Biederman 		}
1249df561f66SGustavo A. R. Silva 		fallthrough;
12501da177e4SLinus Torvalds 	default:
125119aa7ee4SYinghai Lu 		pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
125219aa7ee4SYinghai Lu 			     additional_io_size, realloc_head);
125367d29b5cSBjorn Helgaas 
125467d29b5cSBjorn Helgaas 		/*
125567d29b5cSBjorn Helgaas 		 * If there's a 64-bit prefetchable MMIO window, compute
125667d29b5cSBjorn Helgaas 		 * the size required to put all 64-bit prefetchable
125767d29b5cSBjorn Helgaas 		 * resources in it.
125867d29b5cSBjorn Helgaas 		 */
12591da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
12601da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
12612c8d5a2dSIvan Kokshaysky 		if (pref && (pref->flags & IORESOURCE_MEM_64)) {
12625b285415SYinghai Lu 			prefmask |= IORESOURCE_MEM_64;
126330afe8d0SBjorn Helgaas 			ret = pbus_size_mem(bus, prefmask, prefmask,
12645b285415SYinghai Lu 				prefmask, prefmask,
1265d7b8a217SNicholas Johnson 				realloc_head ? 0 : additional_mmio_pref_size,
1266d7b8a217SNicholas Johnson 				additional_mmio_pref_size, realloc_head);
126767d29b5cSBjorn Helgaas 
12685b285415SYinghai Lu 			/*
126967d29b5cSBjorn Helgaas 			 * If successful, all non-prefetchable resources
127067d29b5cSBjorn Helgaas 			 * and any 32-bit prefetchable resources will go in
127167d29b5cSBjorn Helgaas 			 * the non-prefetchable window.
127267d29b5cSBjorn Helgaas 			 */
127367d29b5cSBjorn Helgaas 			if (ret == 0) {
12745b285415SYinghai Lu 				mask = prefmask;
12755b285415SYinghai Lu 				type2 = prefmask & ~IORESOURCE_MEM_64;
12765b285415SYinghai Lu 				type3 = prefmask & ~IORESOURCE_PREFETCH;
12775b285415SYinghai Lu 			}
12785b285415SYinghai Lu 		}
127967d29b5cSBjorn Helgaas 
128067d29b5cSBjorn Helgaas 		/*
128167d29b5cSBjorn Helgaas 		 * If there is no 64-bit prefetchable window, compute the
128267d29b5cSBjorn Helgaas 		 * size required to put all prefetchable resources in the
128367d29b5cSBjorn Helgaas 		 * 32-bit prefetchable window (if there is one).
128467d29b5cSBjorn Helgaas 		 */
12855b285415SYinghai Lu 		if (!type2) {
12865b285415SYinghai Lu 			prefmask &= ~IORESOURCE_MEM_64;
128730afe8d0SBjorn Helgaas 			ret = pbus_size_mem(bus, prefmask, prefmask,
12885b285415SYinghai Lu 				prefmask, prefmask,
1289d7b8a217SNicholas Johnson 				realloc_head ? 0 : additional_mmio_pref_size,
1290d7b8a217SNicholas Johnson 				additional_mmio_pref_size, realloc_head);
129167d29b5cSBjorn Helgaas 
129267d29b5cSBjorn Helgaas 			/*
129367d29b5cSBjorn Helgaas 			 * If successful, only non-prefetchable resources
129467d29b5cSBjorn Helgaas 			 * will go in the non-prefetchable window.
129567d29b5cSBjorn Helgaas 			 */
129667d29b5cSBjorn Helgaas 			if (ret == 0)
12975b285415SYinghai Lu 				mask = prefmask;
129828760489SEric W. Biederman 			else
1299d7b8a217SNicholas Johnson 				additional_mmio_size += additional_mmio_pref_size;
130067d29b5cSBjorn Helgaas 
13015b285415SYinghai Lu 			type2 = type3 = IORESOURCE_MEM;
13025b285415SYinghai Lu 		}
130367d29b5cSBjorn Helgaas 
130467d29b5cSBjorn Helgaas 		/*
130567d29b5cSBjorn Helgaas 		 * Compute the size required to put everything else in the
130667d29b5cSBjorn Helgaas 		 * non-prefetchable window. This includes:
130767d29b5cSBjorn Helgaas 		 *
130867d29b5cSBjorn Helgaas 		 *   - all non-prefetchable resources
130967d29b5cSBjorn Helgaas 		 *   - 32-bit prefetchable resources if there's a 64-bit
131067d29b5cSBjorn Helgaas 		 *     prefetchable window or no prefetchable window at all
13110d607618SNicholas Johnson 		 *   - 64-bit prefetchable resources if there's no prefetchable
13120d607618SNicholas Johnson 		 *     window at all
131367d29b5cSBjorn Helgaas 		 *
13140d607618SNicholas Johnson 		 * Note that the strategy in __pci_assign_resource() must match
13150d607618SNicholas Johnson 		 * that used here. Specifically, we cannot put a 32-bit
13160d607618SNicholas Johnson 		 * prefetchable resource in a 64-bit prefetchable window.
131767d29b5cSBjorn Helgaas 		 */
13185b285415SYinghai Lu 		pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
1319d7b8a217SNicholas Johnson 			      realloc_head ? 0 : additional_mmio_size,
1320d7b8a217SNicholas Johnson 			      additional_mmio_size, realloc_head);
13211da177e4SLinus Torvalds 		break;
13221da177e4SLinus Torvalds 	}
13231da177e4SLinus Torvalds }
1324c8adf9a3SRam Pai 
pci_bus_size_bridges(struct pci_bus * bus)132510874f5aSBjorn Helgaas void pci_bus_size_bridges(struct pci_bus *bus)
1326c8adf9a3SRam Pai {
1327c8adf9a3SRam Pai 	__pci_bus_size_bridges(bus, NULL);
1328c8adf9a3SRam Pai }
13291da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
13301da177e4SLinus Torvalds 
assign_fixed_resource_on_bus(struct pci_bus * b,struct resource * r)1331d04d0111SDavid Daney static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r)
1332d04d0111SDavid Daney {
1333d04d0111SDavid Daney 	struct resource *parent_r;
1334d04d0111SDavid Daney 	unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM |
1335d04d0111SDavid Daney 			     IORESOURCE_PREFETCH;
1336d04d0111SDavid Daney 
133702992064SAndy Shevchenko 	pci_bus_for_each_resource(b, parent_r) {
1338d04d0111SDavid Daney 		if (!parent_r)
1339d04d0111SDavid Daney 			continue;
1340d04d0111SDavid Daney 
1341d04d0111SDavid Daney 		if ((r->flags & mask) == (parent_r->flags & mask) &&
1342d04d0111SDavid Daney 		    resource_contains(parent_r, r))
1343d04d0111SDavid Daney 			request_resource(parent_r, r);
1344d04d0111SDavid Daney 	}
1345d04d0111SDavid Daney }
1346d04d0111SDavid Daney 
1347d04d0111SDavid Daney /*
13480d607618SNicholas Johnson  * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they are
13490d607618SNicholas Johnson  * skipped by pbus_assign_resources_sorted().
1350d04d0111SDavid Daney  */
pdev_assign_fixed_resources(struct pci_dev * dev)1351d04d0111SDavid Daney static void pdev_assign_fixed_resources(struct pci_dev *dev)
1352d04d0111SDavid Daney {
135309cc9006SMika Westerberg 	struct resource *r;
1354d04d0111SDavid Daney 
135509cc9006SMika Westerberg 	pci_dev_for_each_resource(dev, r) {
1356d04d0111SDavid Daney 		struct pci_bus *b;
1357d04d0111SDavid Daney 
1358d04d0111SDavid Daney 		if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) ||
1359d04d0111SDavid Daney 		    !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
1360d04d0111SDavid Daney 			continue;
1361d04d0111SDavid Daney 
1362d04d0111SDavid Daney 		b = dev->bus;
1363d04d0111SDavid Daney 		while (b && !r->parent) {
1364d04d0111SDavid Daney 			assign_fixed_resource_on_bus(b, r);
1365d04d0111SDavid Daney 			b = b->parent;
1366d04d0111SDavid Daney 		}
1367d04d0111SDavid Daney 	}
1368d04d0111SDavid Daney }
1369d04d0111SDavid Daney 
__pci_bus_assign_resources(const struct pci_bus * bus,struct list_head * realloc_head,struct list_head * fail_head)137010874f5aSBjorn Helgaas void __pci_bus_assign_resources(const struct pci_bus *bus,
1371bdc4abecSYinghai Lu 				struct list_head *realloc_head,
1372bdc4abecSYinghai Lu 				struct list_head *fail_head)
13731da177e4SLinus Torvalds {
13741da177e4SLinus Torvalds 	struct pci_bus *b;
13751da177e4SLinus Torvalds 	struct pci_dev *dev;
13761da177e4SLinus Torvalds 
13779e8bf93aSRam Pai 	pbus_assign_resources_sorted(bus, realloc_head, fail_head);
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
1380d04d0111SDavid Daney 		pdev_assign_fixed_resources(dev);
1381d04d0111SDavid Daney 
13821da177e4SLinus Torvalds 		b = dev->subordinate;
13831da177e4SLinus Torvalds 		if (!b)
13841da177e4SLinus Torvalds 			continue;
13851da177e4SLinus Torvalds 
13869e8bf93aSRam Pai 		__pci_bus_assign_resources(b, realloc_head, fail_head);
13871da177e4SLinus Torvalds 
1388b2fb5cc5SHonghui Zhang 		switch (dev->hdr_type) {
1389b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_BRIDGE:
13906841ec68SYinghai Lu 			if (!pci_is_enabled(dev))
13911da177e4SLinus Torvalds 				pci_setup_bridge(b);
13921da177e4SLinus Torvalds 			break;
13931da177e4SLinus Torvalds 
1394b2fb5cc5SHonghui Zhang 		case PCI_HEADER_TYPE_CARDBUS:
13951da177e4SLinus Torvalds 			pci_setup_cardbus(b);
13961da177e4SLinus Torvalds 			break;
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds 		default:
13997506dc79SFrederick Lawler 			pci_info(dev, "not setting up bridge for bus %04x:%02x\n",
1400227f0647SRyan Desfosses 				 pci_domain_nr(b), b->number);
14011da177e4SLinus Torvalds 			break;
14021da177e4SLinus Torvalds 		}
14031da177e4SLinus Torvalds 	}
14041da177e4SLinus Torvalds }
1405568ddef8SYinghai Lu 
pci_bus_assign_resources(const struct pci_bus * bus)140610874f5aSBjorn Helgaas void pci_bus_assign_resources(const struct pci_bus *bus)
1407568ddef8SYinghai Lu {
1408c8adf9a3SRam Pai 	__pci_bus_assign_resources(bus, NULL, NULL);
1409568ddef8SYinghai Lu }
14101da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
14111da177e4SLinus Torvalds 
pci_claim_device_resources(struct pci_dev * dev)1412765bf9b7SLorenzo Pieralisi static void pci_claim_device_resources(struct pci_dev *dev)
1413765bf9b7SLorenzo Pieralisi {
1414765bf9b7SLorenzo Pieralisi 	int i;
1415765bf9b7SLorenzo Pieralisi 
1416765bf9b7SLorenzo Pieralisi 	for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
1417765bf9b7SLorenzo Pieralisi 		struct resource *r = &dev->resource[i];
1418765bf9b7SLorenzo Pieralisi 
1419765bf9b7SLorenzo Pieralisi 		if (!r->flags || r->parent)
1420765bf9b7SLorenzo Pieralisi 			continue;
1421765bf9b7SLorenzo Pieralisi 
1422765bf9b7SLorenzo Pieralisi 		pci_claim_resource(dev, i);
1423765bf9b7SLorenzo Pieralisi 	}
1424765bf9b7SLorenzo Pieralisi }
1425765bf9b7SLorenzo Pieralisi 
pci_claim_bridge_resources(struct pci_dev * dev)1426765bf9b7SLorenzo Pieralisi static void pci_claim_bridge_resources(struct pci_dev *dev)
1427765bf9b7SLorenzo Pieralisi {
1428765bf9b7SLorenzo Pieralisi 	int i;
1429765bf9b7SLorenzo Pieralisi 
1430765bf9b7SLorenzo Pieralisi 	for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
1431765bf9b7SLorenzo Pieralisi 		struct resource *r = &dev->resource[i];
1432765bf9b7SLorenzo Pieralisi 
1433765bf9b7SLorenzo Pieralisi 		if (!r->flags || r->parent)
1434765bf9b7SLorenzo Pieralisi 			continue;
1435765bf9b7SLorenzo Pieralisi 
1436765bf9b7SLorenzo Pieralisi 		pci_claim_bridge_resource(dev, i);
1437765bf9b7SLorenzo Pieralisi 	}
1438765bf9b7SLorenzo Pieralisi }
1439765bf9b7SLorenzo Pieralisi 
pci_bus_allocate_dev_resources(struct pci_bus * b)1440765bf9b7SLorenzo Pieralisi static void pci_bus_allocate_dev_resources(struct pci_bus *b)
1441765bf9b7SLorenzo Pieralisi {
1442765bf9b7SLorenzo Pieralisi 	struct pci_dev *dev;
1443765bf9b7SLorenzo Pieralisi 	struct pci_bus *child;
1444765bf9b7SLorenzo Pieralisi 
1445765bf9b7SLorenzo Pieralisi 	list_for_each_entry(dev, &b->devices, bus_list) {
1446765bf9b7SLorenzo Pieralisi 		pci_claim_device_resources(dev);
1447765bf9b7SLorenzo Pieralisi 
1448765bf9b7SLorenzo Pieralisi 		child = dev->subordinate;
1449765bf9b7SLorenzo Pieralisi 		if (child)
1450765bf9b7SLorenzo Pieralisi 			pci_bus_allocate_dev_resources(child);
1451765bf9b7SLorenzo Pieralisi 	}
1452765bf9b7SLorenzo Pieralisi }
1453765bf9b7SLorenzo Pieralisi 
pci_bus_allocate_resources(struct pci_bus * b)1454765bf9b7SLorenzo Pieralisi static void pci_bus_allocate_resources(struct pci_bus *b)
1455765bf9b7SLorenzo Pieralisi {
1456765bf9b7SLorenzo Pieralisi 	struct pci_bus *child;
1457765bf9b7SLorenzo Pieralisi 
1458765bf9b7SLorenzo Pieralisi 	/*
14590d607618SNicholas Johnson 	 * Carry out a depth-first search on the PCI bus tree to allocate
14600d607618SNicholas Johnson 	 * bridge apertures.  Read the programmed bridge bases and
14610d607618SNicholas Johnson 	 * recursively claim the respective bridge resources.
1462765bf9b7SLorenzo Pieralisi 	 */
1463765bf9b7SLorenzo Pieralisi 	if (b->self) {
1464765bf9b7SLorenzo Pieralisi 		pci_read_bridge_bases(b);
1465765bf9b7SLorenzo Pieralisi 		pci_claim_bridge_resources(b->self);
1466765bf9b7SLorenzo Pieralisi 	}
1467765bf9b7SLorenzo Pieralisi 
1468765bf9b7SLorenzo Pieralisi 	list_for_each_entry(child, &b->children, node)
1469765bf9b7SLorenzo Pieralisi 		pci_bus_allocate_resources(child);
1470765bf9b7SLorenzo Pieralisi }
1471765bf9b7SLorenzo Pieralisi 
pci_bus_claim_resources(struct pci_bus * b)1472765bf9b7SLorenzo Pieralisi void pci_bus_claim_resources(struct pci_bus *b)
1473765bf9b7SLorenzo Pieralisi {
1474765bf9b7SLorenzo Pieralisi 	pci_bus_allocate_resources(b);
1475765bf9b7SLorenzo Pieralisi 	pci_bus_allocate_dev_resources(b);
1476765bf9b7SLorenzo Pieralisi }
1477765bf9b7SLorenzo Pieralisi EXPORT_SYMBOL(pci_bus_claim_resources);
1478765bf9b7SLorenzo Pieralisi 
__pci_bridge_assign_resources(const struct pci_dev * bridge,struct list_head * add_head,struct list_head * fail_head)147910874f5aSBjorn Helgaas static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
1480bdc4abecSYinghai Lu 					  struct list_head *add_head,
1481bdc4abecSYinghai Lu 					  struct list_head *fail_head)
14826841ec68SYinghai Lu {
14836841ec68SYinghai Lu 	struct pci_bus *b;
14846841ec68SYinghai Lu 
14858424d759SYinghai Lu 	pdev_assign_resources_sorted((struct pci_dev *)bridge,
14868424d759SYinghai Lu 					 add_head, fail_head);
14876841ec68SYinghai Lu 
14886841ec68SYinghai Lu 	b = bridge->subordinate;
14896841ec68SYinghai Lu 	if (!b)
14906841ec68SYinghai Lu 		return;
14916841ec68SYinghai Lu 
14928424d759SYinghai Lu 	__pci_bus_assign_resources(b, add_head, fail_head);
14936841ec68SYinghai Lu 
14946841ec68SYinghai Lu 	switch (bridge->class >> 8) {
14956841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_PCI:
14966841ec68SYinghai Lu 		pci_setup_bridge(b);
14976841ec68SYinghai Lu 		break;
14986841ec68SYinghai Lu 
14996841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_CARDBUS:
15006841ec68SYinghai Lu 		pci_setup_cardbus(b);
15016841ec68SYinghai Lu 		break;
15026841ec68SYinghai Lu 
15036841ec68SYinghai Lu 	default:
15047506dc79SFrederick Lawler 		pci_info(bridge, "not setting up bridge for bus %04x:%02x\n",
1505227f0647SRyan Desfosses 			 pci_domain_nr(b), b->number);
15066841ec68SYinghai Lu 		break;
15076841ec68SYinghai Lu 	}
15086841ec68SYinghai Lu }
1509cb21bc94SChristian König 
1510cb21bc94SChristian König #define PCI_RES_TYPE_MASK \
1511cb21bc94SChristian König 	(IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\
1512cb21bc94SChristian König 	 IORESOURCE_MEM_64)
1513cb21bc94SChristian König 
pci_bridge_release_resources(struct pci_bus * bus,unsigned long type)15145009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus,
15155009b460SYinghai Lu 					 unsigned long type)
15165009b460SYinghai Lu {
15175b285415SYinghai Lu 	struct pci_dev *dev = bus->self;
15185009b460SYinghai Lu 	struct resource *r;
1519c50762a8SBjorn Helgaas 	unsigned int old_flags;
15205b285415SYinghai Lu 	struct resource *b_res;
15215b285415SYinghai Lu 	int idx = 1;
15225009b460SYinghai Lu 
15235b285415SYinghai Lu 	b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
15245b285415SYinghai Lu 
15255b285415SYinghai Lu 	/*
15260d607618SNicholas Johnson 	 * 1. If IO port assignment fails, release bridge IO port.
15270d607618SNicholas Johnson 	 * 2. If non pref MMIO assignment fails, release bridge nonpref MMIO.
15280d607618SNicholas Johnson 	 * 3. If 64bit pref MMIO assignment fails, and bridge pref is 64bit,
15290d607618SNicholas Johnson 	 *    release bridge pref MMIO.
15300d607618SNicholas Johnson 	 * 4. If pref MMIO assignment fails, and bridge pref is 32bit,
15310d607618SNicholas Johnson 	 *    release bridge pref MMIO.
15320d607618SNicholas Johnson 	 * 5. If pref MMIO assignment fails, and bridge pref is not
15330d607618SNicholas Johnson 	 *    assigned, release bridge nonpref MMIO.
15345b285415SYinghai Lu 	 */
15355b285415SYinghai Lu 	if (type & IORESOURCE_IO)
15365b285415SYinghai Lu 		idx = 0;
15375b285415SYinghai Lu 	else if (!(type & IORESOURCE_PREFETCH))
15385b285415SYinghai Lu 		idx = 1;
15395b285415SYinghai Lu 	else if ((type & IORESOURCE_MEM_64) &&
15405b285415SYinghai Lu 		 (b_res[2].flags & IORESOURCE_MEM_64))
15415b285415SYinghai Lu 		idx = 2;
15425b285415SYinghai Lu 	else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
15435b285415SYinghai Lu 		 (b_res[2].flags & IORESOURCE_PREFETCH))
15445b285415SYinghai Lu 		idx = 2;
15455b285415SYinghai Lu 	else
15465b285415SYinghai Lu 		idx = 1;
15475b285415SYinghai Lu 
15485b285415SYinghai Lu 	r = &b_res[idx];
15495b285415SYinghai Lu 
15505009b460SYinghai Lu 	if (!r->parent)
15515b285415SYinghai Lu 		return;
15525b285415SYinghai Lu 
15530d607618SNicholas Johnson 	/* If there are children, release them all */
15545009b460SYinghai Lu 	release_child_resources(r);
15555009b460SYinghai Lu 	if (!release_resource(r)) {
1556cb21bc94SChristian König 		type = old_flags = r->flags & PCI_RES_TYPE_MASK;
155734c6b710SMohan Kumar 		pci_info(dev, "resource %d %pR released\n",
15585b285415SYinghai Lu 			 PCI_BRIDGE_RESOURCES + idx, r);
15590d607618SNicholas Johnson 		/* Keep the old size */
15605009b460SYinghai Lu 		r->end = resource_size(r) - 1;
15615009b460SYinghai Lu 		r->start = 0;
15625009b460SYinghai Lu 		r->flags = 0;
15635009b460SYinghai Lu 
15640d607618SNicholas Johnson 		/* Avoiding touch the one without PREF */
15655009b460SYinghai Lu 		if (type & IORESOURCE_PREFETCH)
15665009b460SYinghai Lu 			type = IORESOURCE_PREFETCH;
15675009b460SYinghai Lu 		__pci_setup_bridge(bus, type);
15680d607618SNicholas Johnson 		/* For next child res under same bridge */
15695b285415SYinghai Lu 		r->flags = old_flags;
15705009b460SYinghai Lu 	}
15715009b460SYinghai Lu }
15725009b460SYinghai Lu 
15735009b460SYinghai Lu enum release_type {
15745009b460SYinghai Lu 	leaf_only,
15755009b460SYinghai Lu 	whole_subtree,
15765009b460SYinghai Lu };
15770d607618SNicholas Johnson 
15785009b460SYinghai Lu /*
15790d607618SNicholas Johnson  * Try to release PCI bridge resources from leaf bridge, so we can allocate
15800d607618SNicholas Johnson  * a larger window later.
15815009b460SYinghai Lu  */
pci_bus_release_bridge_resources(struct pci_bus * bus,unsigned long type,enum release_type rel_type)158210874f5aSBjorn Helgaas static void pci_bus_release_bridge_resources(struct pci_bus *bus,
15835009b460SYinghai Lu 					     unsigned long type,
15845009b460SYinghai Lu 					     enum release_type rel_type)
15855009b460SYinghai Lu {
15865009b460SYinghai Lu 	struct pci_dev *dev;
15875009b460SYinghai Lu 	bool is_leaf_bridge = true;
15885009b460SYinghai Lu 
15895009b460SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
15905009b460SYinghai Lu 		struct pci_bus *b = dev->subordinate;
15915009b460SYinghai Lu 		if (!b)
15925009b460SYinghai Lu 			continue;
15935009b460SYinghai Lu 
15945009b460SYinghai Lu 		is_leaf_bridge = false;
15955009b460SYinghai Lu 
15965009b460SYinghai Lu 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
15975009b460SYinghai Lu 			continue;
15985009b460SYinghai Lu 
15995009b460SYinghai Lu 		if (rel_type == whole_subtree)
16005009b460SYinghai Lu 			pci_bus_release_bridge_resources(b, type,
16015009b460SYinghai Lu 						 whole_subtree);
16025009b460SYinghai Lu 	}
16035009b460SYinghai Lu 
16045009b460SYinghai Lu 	if (pci_is_root_bus(bus))
16055009b460SYinghai Lu 		return;
16065009b460SYinghai Lu 
16075009b460SYinghai Lu 	if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
16085009b460SYinghai Lu 		return;
16095009b460SYinghai Lu 
16105009b460SYinghai Lu 	if ((rel_type == whole_subtree) || is_leaf_bridge)
16115009b460SYinghai Lu 		pci_bridge_release_resources(bus, type);
16125009b460SYinghai Lu }
16135009b460SYinghai Lu 
pci_bus_dump_res(struct pci_bus * bus)161476fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
161576fbc263SYinghai Lu {
161689a74eccSBjorn Helgaas 	struct resource *res;
161776fbc263SYinghai Lu 	int i;
161876fbc263SYinghai Lu 
161989a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
16207c9342b8SYinghai Lu 		if (!res || !res->end || !res->flags)
162176fbc263SYinghai Lu 			continue;
162276fbc263SYinghai Lu 
162334c6b710SMohan Kumar 		dev_info(&bus->dev, "resource %d %pR\n", i, res);
162476fbc263SYinghai Lu 	}
162576fbc263SYinghai Lu }
162676fbc263SYinghai Lu 
pci_bus_dump_resources(struct pci_bus * bus)162776fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
162876fbc263SYinghai Lu {
162976fbc263SYinghai Lu 	struct pci_bus *b;
163076fbc263SYinghai Lu 	struct pci_dev *dev;
163176fbc263SYinghai Lu 
163276fbc263SYinghai Lu 
163376fbc263SYinghai Lu 	pci_bus_dump_res(bus);
163476fbc263SYinghai Lu 
163576fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
163676fbc263SYinghai Lu 		b = dev->subordinate;
163776fbc263SYinghai Lu 		if (!b)
163876fbc263SYinghai Lu 			continue;
163976fbc263SYinghai Lu 
164076fbc263SYinghai Lu 		pci_bus_dump_resources(b);
164176fbc263SYinghai Lu 	}
164276fbc263SYinghai Lu }
164376fbc263SYinghai Lu 
pci_bus_get_depth(struct pci_bus * bus)1644ff35147cSYinghai Lu static int pci_bus_get_depth(struct pci_bus *bus)
1645da7822e5SYinghai Lu {
1646da7822e5SYinghai Lu 	int depth = 0;
1647f2a230bdSWei Yang 	struct pci_bus *child_bus;
1648da7822e5SYinghai Lu 
1649f2a230bdSWei Yang 	list_for_each_entry(child_bus, &bus->children, node) {
1650da7822e5SYinghai Lu 		int ret;
1651da7822e5SYinghai Lu 
1652f2a230bdSWei Yang 		ret = pci_bus_get_depth(child_bus);
1653da7822e5SYinghai Lu 		if (ret + 1 > depth)
1654da7822e5SYinghai Lu 			depth = ret + 1;
1655da7822e5SYinghai Lu 	}
1656da7822e5SYinghai Lu 
1657da7822e5SYinghai Lu 	return depth;
1658da7822e5SYinghai Lu }
1659da7822e5SYinghai Lu 
1660b55438fdSYinghai Lu /*
1661b55438fdSYinghai Lu  * -1: undefined, will auto detect later
1662b55438fdSYinghai Lu  *  0: disabled by user
1663b55438fdSYinghai Lu  *  1: disabled by auto detect
1664b55438fdSYinghai Lu  *  2: enabled by user
1665b55438fdSYinghai Lu  *  3: enabled by auto detect
1666b55438fdSYinghai Lu  */
1667b55438fdSYinghai Lu enum enable_type {
1668b55438fdSYinghai Lu 	undefined = -1,
1669b55438fdSYinghai Lu 	user_disabled,
1670b55438fdSYinghai Lu 	auto_disabled,
1671b55438fdSYinghai Lu 	user_enabled,
1672b55438fdSYinghai Lu 	auto_enabled,
1673b55438fdSYinghai Lu };
1674b55438fdSYinghai Lu 
1675ff35147cSYinghai Lu static enum enable_type pci_realloc_enable = undefined;
pci_realloc_get_opt(char * str)1676b55438fdSYinghai Lu void __init pci_realloc_get_opt(char *str)
1677b55438fdSYinghai Lu {
1678b55438fdSYinghai Lu 	if (!strncmp(str, "off", 3))
1679b55438fdSYinghai Lu 		pci_realloc_enable = user_disabled;
1680b55438fdSYinghai Lu 	else if (!strncmp(str, "on", 2))
1681b55438fdSYinghai Lu 		pci_realloc_enable = user_enabled;
1682b55438fdSYinghai Lu }
pci_realloc_enabled(enum enable_type enable)1683ff35147cSYinghai Lu static bool pci_realloc_enabled(enum enable_type enable)
1684b55438fdSYinghai Lu {
1685967260cdSYinghai Lu 	return enable >= user_enabled;
1686b55438fdSYinghai Lu }
1687f483d392SRam Pai 
1688b07f2ebcSYinghai Lu #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
iov_resources_unassigned(struct pci_dev * dev,void * data)1689ff35147cSYinghai Lu static int iov_resources_unassigned(struct pci_dev *dev, void *data)
1690223d96fcSYinghai Lu {
1691b07f2ebcSYinghai Lu 	int i;
1692223d96fcSYinghai Lu 	bool *unassigned = data;
1693b07f2ebcSYinghai Lu 
169439098edbSDenis Efremov 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
169539098edbSDenis Efremov 		struct resource *r = &dev->resource[i + PCI_IOV_RESOURCES];
1696fa216bf4SYinghai Lu 		struct pci_bus_region region;
1697b07f2ebcSYinghai Lu 
1698223d96fcSYinghai Lu 		/* Not assigned or rejected by kernel? */
1699fa216bf4SYinghai Lu 		if (!r->flags)
1700fa216bf4SYinghai Lu 			continue;
1701b07f2ebcSYinghai Lu 
1702fc279850SYinghai Lu 		pcibios_resource_to_bus(dev->bus, &region, r);
1703fa216bf4SYinghai Lu 		if (!region.start) {
1704223d96fcSYinghai Lu 			*unassigned = true;
17050d607618SNicholas Johnson 			return 1; /* Return early from pci_walk_bus() */
1706b07f2ebcSYinghai Lu 		}
1707b07f2ebcSYinghai Lu 	}
1708b07f2ebcSYinghai Lu 
1709223d96fcSYinghai Lu 	return 0;
1710223d96fcSYinghai Lu }
1711223d96fcSYinghai Lu 
pci_realloc_detect(struct pci_bus * bus,enum enable_type enable_local)1712ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1713967260cdSYinghai Lu 					   enum enable_type enable_local)
1714223d96fcSYinghai Lu {
1715223d96fcSYinghai Lu 	bool unassigned = false;
17167ac0d094SBenjamin Herrenschmidt 	struct pci_host_bridge *host;
1717223d96fcSYinghai Lu 
1718967260cdSYinghai Lu 	if (enable_local != undefined)
1719967260cdSYinghai Lu 		return enable_local;
1720223d96fcSYinghai Lu 
17217ac0d094SBenjamin Herrenschmidt 	host = pci_find_host_bridge(bus);
17227ac0d094SBenjamin Herrenschmidt 	if (host->preserve_config)
17237ac0d094SBenjamin Herrenschmidt 		return auto_disabled;
17247ac0d094SBenjamin Herrenschmidt 
1725223d96fcSYinghai Lu 	pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
1726967260cdSYinghai Lu 	if (unassigned)
1727967260cdSYinghai Lu 		return auto_enabled;
1728967260cdSYinghai Lu 
1729967260cdSYinghai Lu 	return enable_local;
1730b07f2ebcSYinghai Lu }
1731223d96fcSYinghai Lu #else
pci_realloc_detect(struct pci_bus * bus,enum enable_type enable_local)1732ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1733967260cdSYinghai Lu 					   enum enable_type enable_local)
1734967260cdSYinghai Lu {
1735967260cdSYinghai Lu 	return enable_local;
1736b07f2ebcSYinghai Lu }
1737b07f2ebcSYinghai Lu #endif
1738b07f2ebcSYinghai Lu 
adjust_bridge_window(struct pci_dev * bridge,struct resource * res,struct list_head * add_list,resource_size_t new_size)17391e58f4e1SNicholas Johnson static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res,
17400d607618SNicholas Johnson 				 struct list_head *add_list,
17413d264da9SNicholas Johnson 				 resource_size_t new_size)
17421a576772SMika Westerberg {
174394867573SNicholas Johnson 	resource_size_t add_size, size = resource_size(res);
17441a576772SMika Westerberg 
17451a576772SMika Westerberg 	if (res->parent)
17461a576772SMika Westerberg 		return;
17471a576772SMika Westerberg 
174894867573SNicholas Johnson 	if (!new_size)
17491a576772SMika Westerberg 		return;
17501a576772SMika Westerberg 
175194867573SNicholas Johnson 	if (new_size > size) {
175294867573SNicholas Johnson 		add_size = new_size - size;
175394867573SNicholas Johnson 		pci_dbg(bridge, "bridge window %pR extended by %pa\n", res,
175494867573SNicholas Johnson 			&add_size);
175594867573SNicholas Johnson 	} else if (new_size < size) {
175694867573SNicholas Johnson 		add_size = size - new_size;
175794867573SNicholas Johnson 		pci_dbg(bridge, "bridge window %pR shrunken by %pa\n", res,
175894867573SNicholas Johnson 			&add_size);
17599db0b9b6SMika Westerberg 	} else {
17609db0b9b6SMika Westerberg 		return;
176194867573SNicholas Johnson 	}
176294867573SNicholas Johnson 
1763ae4611f1SNicholas Johnson 	res->end = res->start + new_size - 1;
17647180c1d0SMika Westerberg 
17657180c1d0SMika Westerberg 	/* If the resource is part of the add_list, remove it now */
17667180c1d0SMika Westerberg 	if (add_list)
1767ae4611f1SNicholas Johnson 		remove_from_list(add_list, res);
17681a576772SMika Westerberg }
17691a576772SMika Westerberg 
remove_dev_resource(struct resource * avail,struct pci_dev * dev,struct resource * res)17709db0b9b6SMika Westerberg static void remove_dev_resource(struct resource *avail, struct pci_dev *dev,
17719db0b9b6SMika Westerberg 				struct resource *res)
17729db0b9b6SMika Westerberg {
17739db0b9b6SMika Westerberg 	resource_size_t size, align, tmp;
17749db0b9b6SMika Westerberg 
17759db0b9b6SMika Westerberg 	size = resource_size(res);
17769db0b9b6SMika Westerberg 	if (!size)
17779db0b9b6SMika Westerberg 		return;
17789db0b9b6SMika Westerberg 
17799db0b9b6SMika Westerberg 	align = pci_resource_alignment(dev, res);
17809db0b9b6SMika Westerberg 	align = align ? ALIGN(avail->start, align) - avail->start : 0;
17819db0b9b6SMika Westerberg 	tmp = align + size;
17829db0b9b6SMika Westerberg 	avail->start = min(avail->start + tmp, avail->end + 1);
17839db0b9b6SMika Westerberg }
17849db0b9b6SMika Westerberg 
remove_dev_resources(struct pci_dev * dev,struct resource * io,struct resource * mmio,struct resource * mmio_pref)17859db0b9b6SMika Westerberg static void remove_dev_resources(struct pci_dev *dev, struct resource *io,
17869db0b9b6SMika Westerberg 				 struct resource *mmio,
17879db0b9b6SMika Westerberg 				 struct resource *mmio_pref)
17889db0b9b6SMika Westerberg {
178909cc9006SMika Westerberg 	struct resource *res;
17909db0b9b6SMika Westerberg 
179109cc9006SMika Westerberg 	pci_dev_for_each_resource(dev, res) {
17929db0b9b6SMika Westerberg 		if (resource_type(res) == IORESOURCE_IO) {
17939db0b9b6SMika Westerberg 			remove_dev_resource(io, dev, res);
17949db0b9b6SMika Westerberg 		} else if (resource_type(res) == IORESOURCE_MEM) {
17959db0b9b6SMika Westerberg 
17969db0b9b6SMika Westerberg 			/*
17979db0b9b6SMika Westerberg 			 * Make sure prefetchable memory is reduced from
17989db0b9b6SMika Westerberg 			 * the correct resource. Specifically we put 32-bit
17999db0b9b6SMika Westerberg 			 * prefetchable memory in non-prefetchable window
180086b4ad7dSBjorn Helgaas 			 * if there is an 64-bit prefetchable window.
18019db0b9b6SMika Westerberg 			 *
18029db0b9b6SMika Westerberg 			 * See comments in __pci_bus_size_bridges() for
18039db0b9b6SMika Westerberg 			 * more information.
18049db0b9b6SMika Westerberg 			 */
18059db0b9b6SMika Westerberg 			if ((res->flags & IORESOURCE_PREFETCH) &&
18069db0b9b6SMika Westerberg 			    ((res->flags & IORESOURCE_MEM_64) ==
18079db0b9b6SMika Westerberg 			     (mmio_pref->flags & IORESOURCE_MEM_64)))
18089db0b9b6SMika Westerberg 				remove_dev_resource(mmio_pref, dev, res);
18099db0b9b6SMika Westerberg 			else
18109db0b9b6SMika Westerberg 				remove_dev_resource(mmio, dev, res);
18119db0b9b6SMika Westerberg 		}
18129db0b9b6SMika Westerberg 	}
18139db0b9b6SMika Westerberg }
18149db0b9b6SMika Westerberg 
18159db0b9b6SMika Westerberg /*
18169db0b9b6SMika Westerberg  * io, mmio and mmio_pref contain the total amount of bridge window space
18179db0b9b6SMika Westerberg  * available. This includes the minimal space needed to cover all the
18189db0b9b6SMika Westerberg  * existing devices on the bus and the possible extra space that can be
18199db0b9b6SMika Westerberg  * shared with the bridges.
18209db0b9b6SMika Westerberg  */
pci_bus_distribute_available_resources(struct pci_bus * bus,struct list_head * add_list,struct resource io,struct resource mmio,struct resource mmio_pref)18211a576772SMika Westerberg static void pci_bus_distribute_available_resources(struct pci_bus *bus,
18220d607618SNicholas Johnson 					    struct list_head *add_list,
1823d555a50fSNicholas Johnson 					    struct resource io,
1824d555a50fSNicholas Johnson 					    struct resource mmio,
1825d555a50fSNicholas Johnson 					    struct resource mmio_pref)
18261a576772SMika Westerberg {
18271a576772SMika Westerberg 	unsigned int normal_bridges = 0, hotplug_bridges = 0;
18281a576772SMika Westerberg 	struct resource *io_res, *mmio_res, *mmio_pref_res;
18291a576772SMika Westerberg 	struct pci_dev *dev, *bridge = bus->self;
18309db0b9b6SMika Westerberg 	resource_size_t io_per_b, mmio_per_b, mmio_pref_per_b, align;
18311a576772SMika Westerberg 
18326e0688dbSKrzysztof Wilczynski 	io_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
18336e0688dbSKrzysztof Wilczynski 	mmio_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
18346e0688dbSKrzysztof Wilczynski 	mmio_pref_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
18351a576772SMika Westerberg 
18361a576772SMika Westerberg 	/*
1837f924c26eSNicholas Johnson 	 * The alignment of this bridge is yet to be considered, hence it must
1838f924c26eSNicholas Johnson 	 * be done now before extending its bridge window.
1839f924c26eSNicholas Johnson 	 */
1840f924c26eSNicholas Johnson 	align = pci_resource_alignment(bridge, io_res);
1841f924c26eSNicholas Johnson 	if (!io_res->parent && align)
1842f924c26eSNicholas Johnson 		io.start = min(ALIGN(io.start, align), io.end + 1);
1843f924c26eSNicholas Johnson 
1844f924c26eSNicholas Johnson 	align = pci_resource_alignment(bridge, mmio_res);
1845f924c26eSNicholas Johnson 	if (!mmio_res->parent && align)
1846f924c26eSNicholas Johnson 		mmio.start = min(ALIGN(mmio.start, align), mmio.end + 1);
1847f924c26eSNicholas Johnson 
1848f924c26eSNicholas Johnson 	align = pci_resource_alignment(bridge, mmio_pref_res);
1849f924c26eSNicholas Johnson 	if (!mmio_pref_res->parent && align)
1850f924c26eSNicholas Johnson 		mmio_pref.start = min(ALIGN(mmio_pref.start, align),
1851f924c26eSNicholas Johnson 			mmio_pref.end + 1);
1852f924c26eSNicholas Johnson 
1853f924c26eSNicholas Johnson 	/*
1854ae4611f1SNicholas Johnson 	 * Now that we have adjusted for alignment, update the bridge window
1855ae4611f1SNicholas Johnson 	 * resources to fill as much remaining resource space as possible.
18561a576772SMika Westerberg 	 */
18571e58f4e1SNicholas Johnson 	adjust_bridge_window(bridge, io_res, add_list, resource_size(&io));
18581e58f4e1SNicholas Johnson 	adjust_bridge_window(bridge, mmio_res, add_list, resource_size(&mmio));
18591e58f4e1SNicholas Johnson 	adjust_bridge_window(bridge, mmio_pref_res, add_list,
186077793854SNicholas Johnson 			     resource_size(&mmio_pref));
18611a576772SMika Westerberg 
18621a576772SMika Westerberg 	/*
18631a576772SMika Westerberg 	 * Calculate how many hotplug bridges and normal bridges there
18641a576772SMika Westerberg 	 * are on this bus.  We will distribute the additional available
18651a576772SMika Westerberg 	 * resources between hotplug bridges.
18661a576772SMika Westerberg 	 */
18671a576772SMika Westerberg 	for_each_pci_bridge(dev, bus) {
18681a576772SMika Westerberg 		if (dev->is_hotplug_bridge)
18691a576772SMika Westerberg 			hotplug_bridges++;
18701a576772SMika Westerberg 		else
18711a576772SMika Westerberg 			normal_bridges++;
18721a576772SMika Westerberg 	}
18731a576772SMika Westerberg 
18749db0b9b6SMika Westerberg 	if (!(hotplug_bridges + normal_bridges))
18756a381ea6SNicholas Johnson 		return;
18766a381ea6SNicholas Johnson 
18775c6bcc34SNicholas Johnson 	/*
18789db0b9b6SMika Westerberg 	 * Calculate the amount of space we can forward from "bus" to any
18799db0b9b6SMika Westerberg 	 * downstream buses, i.e., the space left over after assigning the
18809db0b9b6SMika Westerberg 	 * BARs and windows on "bus".
18815c6bcc34SNicholas Johnson 	 */
18829db0b9b6SMika Westerberg 	list_for_each_entry(dev, &bus->devices, bus_list) {
18839db0b9b6SMika Westerberg 		if (!dev->is_virtfn)
18849db0b9b6SMika Westerberg 			remove_dev_resources(dev, &io, &mmio, &mmio_pref);
18851a576772SMika Westerberg 	}
18861a576772SMika Westerberg 
18879db0b9b6SMika Westerberg 	/*
18889db0b9b6SMika Westerberg 	 * If there is at least one hotplug bridge on this bus it gets all
18899db0b9b6SMika Westerberg 	 * the extra resource space that was left after the reductions
18909db0b9b6SMika Westerberg 	 * above.
18919db0b9b6SMika Westerberg 	 *
18929db0b9b6SMika Westerberg 	 * If there are no hotplug bridges the extra resource space is
18939db0b9b6SMika Westerberg 	 * split between non-hotplug bridges. This is to allow possible
18949db0b9b6SMika Westerberg 	 * hotplug bridges below them to get the extra space as well.
18959db0b9b6SMika Westerberg 	 */
18969db0b9b6SMika Westerberg 	if (hotplug_bridges) {
18979db0b9b6SMika Westerberg 		io_per_b = div64_ul(resource_size(&io), hotplug_bridges);
18989db0b9b6SMika Westerberg 		mmio_per_b = div64_ul(resource_size(&mmio), hotplug_bridges);
18999db0b9b6SMika Westerberg 		mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
1900f924c26eSNicholas Johnson 					   hotplug_bridges);
19019db0b9b6SMika Westerberg 	} else {
19029db0b9b6SMika Westerberg 		io_per_b = div64_ul(resource_size(&io), normal_bridges);
19039db0b9b6SMika Westerberg 		mmio_per_b = div64_ul(resource_size(&mmio), normal_bridges);
19049db0b9b6SMika Westerberg 		mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
19059db0b9b6SMika Westerberg 					   normal_bridges);
19069db0b9b6SMika Westerberg 	}
1907f924c26eSNicholas Johnson 
19081a576772SMika Westerberg 	for_each_pci_bridge(dev, bus) {
190908f0a15eSMika Westerberg 		struct resource *res;
19101a576772SMika Westerberg 		struct pci_bus *b;
19111a576772SMika Westerberg 
19121a576772SMika Westerberg 		b = dev->subordinate;
19139db0b9b6SMika Westerberg 		if (!b)
19149db0b9b6SMika Westerberg 			continue;
19159db0b9b6SMika Westerberg 		if (hotplug_bridges && !dev->is_hotplug_bridge)
19161a576772SMika Westerberg 			continue;
19171a576772SMika Westerberg 
191808f0a15eSMika Westerberg 		res = &dev->resource[PCI_BRIDGE_IO_WINDOW];
19199db0b9b6SMika Westerberg 
19209db0b9b6SMika Westerberg 		/*
19219db0b9b6SMika Westerberg 		 * Make sure the split resource space is properly aligned
19229db0b9b6SMika Westerberg 		 * for bridge windows (align it down to avoid going above
19239db0b9b6SMika Westerberg 		 * what is available).
19249db0b9b6SMika Westerberg 		 */
192508f0a15eSMika Westerberg 		align = pci_resource_alignment(dev, res);
19269db0b9b6SMika Westerberg 		io.end = align ? io.start + ALIGN_DOWN(io_per_b, align) - 1
19279db0b9b6SMika Westerberg 			       : io.start + io_per_b - 1;
19289db0b9b6SMika Westerberg 
19299db0b9b6SMika Westerberg 		/*
19309db0b9b6SMika Westerberg 		 * The x_per_b holds the extra resource space that can be
19319db0b9b6SMika Westerberg 		 * added for each bridge but there is the minimal already
19329db0b9b6SMika Westerberg 		 * reserved as well so adjust x.start down accordingly to
19339db0b9b6SMika Westerberg 		 * cover the whole space.
19349db0b9b6SMika Westerberg 		 */
19359db0b9b6SMika Westerberg 		io.start -= resource_size(res);
193608f0a15eSMika Westerberg 
193708f0a15eSMika Westerberg 		res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
193808f0a15eSMika Westerberg 		align = pci_resource_alignment(dev, res);
19399db0b9b6SMika Westerberg 		mmio.end = align ? mmio.start + ALIGN_DOWN(mmio_per_b, align) - 1
19409db0b9b6SMika Westerberg 				 : mmio.start + mmio_per_b - 1;
19419db0b9b6SMika Westerberg 		mmio.start -= resource_size(res);
194208f0a15eSMika Westerberg 
194308f0a15eSMika Westerberg 		res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
194408f0a15eSMika Westerberg 		align = pci_resource_alignment(dev, res);
194508f0a15eSMika Westerberg 		mmio_pref.end = align ? mmio_pref.start +
19469db0b9b6SMika Westerberg 					ALIGN_DOWN(mmio_pref_per_b, align) - 1
19479db0b9b6SMika Westerberg 				      : mmio_pref.start + mmio_pref_per_b - 1;
19489db0b9b6SMika Westerberg 		mmio_pref.start -= resource_size(res);
1949d555a50fSNicholas Johnson 
1950d555a50fSNicholas Johnson 		pci_bus_distribute_available_resources(b, add_list, io, mmio,
1951d555a50fSNicholas Johnson 						       mmio_pref);
1952f924c26eSNicholas Johnson 
195308f0a15eSMika Westerberg 		io.start += io.end + 1;
195408f0a15eSMika Westerberg 		mmio.start += mmio.end + 1;
195508f0a15eSMika Westerberg 		mmio_pref.start += mmio_pref.end + 1;
19561a576772SMika Westerberg 	}
19571a576772SMika Westerberg }
19581a576772SMika Westerberg 
pci_bridge_distribute_available_resources(struct pci_dev * bridge,struct list_head * add_list)19590d607618SNicholas Johnson static void pci_bridge_distribute_available_resources(struct pci_dev *bridge,
19601a576772SMika Westerberg 						      struct list_head *add_list)
19611a576772SMika Westerberg {
1962d555a50fSNicholas Johnson 	struct resource available_io, available_mmio, available_mmio_pref;
19631a576772SMika Westerberg 
19641a576772SMika Westerberg 	if (!bridge->is_hotplug_bridge)
19651a576772SMika Westerberg 		return;
19661a576772SMika Westerberg 
19677180c1d0SMika Westerberg 	pci_dbg(bridge, "distributing available resources\n");
19687180c1d0SMika Westerberg 
19691a576772SMika Westerberg 	/* Take the initial extra resources from the hotplug port */
19706e0688dbSKrzysztof Wilczynski 	available_io = bridge->resource[PCI_BRIDGE_IO_WINDOW];
19716e0688dbSKrzysztof Wilczynski 	available_mmio = bridge->resource[PCI_BRIDGE_MEM_WINDOW];
19726e0688dbSKrzysztof Wilczynski 	available_mmio_pref = bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
19731a576772SMika Westerberg 
19741a576772SMika Westerberg 	pci_bus_distribute_available_resources(bridge->subordinate,
19750d607618SNicholas Johnson 					       add_list, available_io,
19760d607618SNicholas Johnson 					       available_mmio,
19770d607618SNicholas Johnson 					       available_mmio_pref);
19781a576772SMika Westerberg }
19791a576772SMika Westerberg 
pci_bridge_resources_not_assigned(struct pci_dev * dev)19807180c1d0SMika Westerberg static bool pci_bridge_resources_not_assigned(struct pci_dev *dev)
19817180c1d0SMika Westerberg {
19827180c1d0SMika Westerberg 	const struct resource *r;
19837180c1d0SMika Westerberg 
19847180c1d0SMika Westerberg 	/*
19857180c1d0SMika Westerberg 	 * If the child device's resources are not yet assigned it means we
19867180c1d0SMika Westerberg 	 * are configuring them (not the boot firmware), so we should be
19877180c1d0SMika Westerberg 	 * able to extend the upstream bridge resources in the same way we
19887180c1d0SMika Westerberg 	 * do with the normal hotplug case.
19897180c1d0SMika Westerberg 	 */
19907180c1d0SMika Westerberg 	r = &dev->resource[PCI_BRIDGE_IO_WINDOW];
19917180c1d0SMika Westerberg 	if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
19927180c1d0SMika Westerberg 		return false;
19937180c1d0SMika Westerberg 	r = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
19947180c1d0SMika Westerberg 	if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
19957180c1d0SMika Westerberg 		return false;
19967180c1d0SMika Westerberg 	r = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
19977180c1d0SMika Westerberg 	if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
19987180c1d0SMika Westerberg 		return false;
19997180c1d0SMika Westerberg 
20007180c1d0SMika Westerberg 	return true;
20017180c1d0SMika Westerberg }
20027180c1d0SMika Westerberg 
20037180c1d0SMika Westerberg static void
pci_root_bus_distribute_available_resources(struct pci_bus * bus,struct list_head * add_list)20047180c1d0SMika Westerberg pci_root_bus_distribute_available_resources(struct pci_bus *bus,
20057180c1d0SMika Westerberg 					    struct list_head *add_list)
20067180c1d0SMika Westerberg {
20077180c1d0SMika Westerberg 	struct pci_dev *dev, *bridge = bus->self;
20087180c1d0SMika Westerberg 
20097180c1d0SMika Westerberg 	for_each_pci_bridge(dev, bus) {
20107180c1d0SMika Westerberg 		struct pci_bus *b;
20117180c1d0SMika Westerberg 
20127180c1d0SMika Westerberg 		b = dev->subordinate;
20137180c1d0SMika Westerberg 		if (!b)
20147180c1d0SMika Westerberg 			continue;
20157180c1d0SMika Westerberg 
20167180c1d0SMika Westerberg 		/*
20177180c1d0SMika Westerberg 		 * Need to check "bridge" here too because it is NULL
20187180c1d0SMika Westerberg 		 * in case of root bus.
20197180c1d0SMika Westerberg 		 */
20207180c1d0SMika Westerberg 		if (bridge && pci_bridge_resources_not_assigned(dev))
20217180c1d0SMika Westerberg 			pci_bridge_distribute_available_resources(bridge,
20227180c1d0SMika Westerberg 								  add_list);
20237180c1d0SMika Westerberg 		else
20247180c1d0SMika Westerberg 			pci_root_bus_distribute_available_resources(b, add_list);
20257180c1d0SMika Westerberg 	}
20267180c1d0SMika Westerberg }
20277180c1d0SMika Westerberg 
2028d1caf229SMika Westerberg /*
2029d1caf229SMika Westerberg  * First try will not touch PCI bridge res.
2030d1caf229SMika Westerberg  * Second and later try will clear small leaf bridge res.
2031d1caf229SMika Westerberg  * Will stop till to the max depth if can not find good one.
2032d1caf229SMika Westerberg  */
pci_assign_unassigned_root_bus_resources(struct pci_bus * bus)2033d1caf229SMika Westerberg void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
2034d1caf229SMika Westerberg {
2035d1caf229SMika Westerberg 	LIST_HEAD(realloc_head);
2036d1caf229SMika Westerberg 	/* List of resources that want additional resources */
2037d1caf229SMika Westerberg 	struct list_head *add_list = NULL;
2038d1caf229SMika Westerberg 	int tried_times = 0;
2039d1caf229SMika Westerberg 	enum release_type rel_type = leaf_only;
2040d1caf229SMika Westerberg 	LIST_HEAD(fail_head);
2041d1caf229SMika Westerberg 	struct pci_dev_resource *fail_res;
2042d1caf229SMika Westerberg 	int pci_try_num = 1;
2043d1caf229SMika Westerberg 	enum enable_type enable_local;
2044d1caf229SMika Westerberg 
2045d1caf229SMika Westerberg 	/* Don't realloc if asked to do so */
2046d1caf229SMika Westerberg 	enable_local = pci_realloc_detect(bus, pci_realloc_enable);
2047d1caf229SMika Westerberg 	if (pci_realloc_enabled(enable_local)) {
2048d1caf229SMika Westerberg 		int max_depth = pci_bus_get_depth(bus);
2049d1caf229SMika Westerberg 
2050d1caf229SMika Westerberg 		pci_try_num = max_depth + 1;
2051d1caf229SMika Westerberg 		dev_info(&bus->dev, "max bus depth: %d pci_try_num: %d\n",
2052d1caf229SMika Westerberg 			 max_depth, pci_try_num);
2053d1caf229SMika Westerberg 	}
2054d1caf229SMika Westerberg 
2055d1caf229SMika Westerberg again:
2056d1caf229SMika Westerberg 	/*
2057d1caf229SMika Westerberg 	 * Last try will use add_list, otherwise will try good to have as must
2058d1caf229SMika Westerberg 	 * have, so can realloc parent bridge resource
2059d1caf229SMika Westerberg 	 */
2060d1caf229SMika Westerberg 	if (tried_times + 1 == pci_try_num)
2061d1caf229SMika Westerberg 		add_list = &realloc_head;
2062d1caf229SMika Westerberg 	/*
2063d1caf229SMika Westerberg 	 * Depth first, calculate sizes and alignments of all subordinate buses.
2064d1caf229SMika Westerberg 	 */
2065d1caf229SMika Westerberg 	__pci_bus_size_bridges(bus, add_list);
2066d1caf229SMika Westerberg 
20677180c1d0SMika Westerberg 	pci_root_bus_distribute_available_resources(bus, add_list);
20687180c1d0SMika Westerberg 
2069d1caf229SMika Westerberg 	/* Depth last, allocate resources and update the hardware. */
2070d1caf229SMika Westerberg 	__pci_bus_assign_resources(bus, add_list, &fail_head);
2071d1caf229SMika Westerberg 	if (add_list)
2072d1caf229SMika Westerberg 		BUG_ON(!list_empty(add_list));
2073d1caf229SMika Westerberg 	tried_times++;
2074d1caf229SMika Westerberg 
2075d1caf229SMika Westerberg 	/* Any device complain? */
2076d1caf229SMika Westerberg 	if (list_empty(&fail_head))
2077d1caf229SMika Westerberg 		goto dump;
2078d1caf229SMika Westerberg 
2079d1caf229SMika Westerberg 	if (tried_times >= pci_try_num) {
2080d1caf229SMika Westerberg 		if (enable_local == undefined)
2081d1caf229SMika Westerberg 			dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
2082d1caf229SMika Westerberg 		else if (enable_local == auto_enabled)
2083d1caf229SMika Westerberg 			dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
2084d1caf229SMika Westerberg 
2085d1caf229SMika Westerberg 		free_list(&fail_head);
2086d1caf229SMika Westerberg 		goto dump;
2087d1caf229SMika Westerberg 	}
2088d1caf229SMika Westerberg 
2089d1caf229SMika Westerberg 	dev_info(&bus->dev, "No. %d try to assign unassigned res\n",
2090d1caf229SMika Westerberg 		 tried_times + 1);
2091d1caf229SMika Westerberg 
2092d1caf229SMika Westerberg 	/* Third times and later will not check if it is leaf */
2093d1caf229SMika Westerberg 	if ((tried_times + 1) > 2)
2094d1caf229SMika Westerberg 		rel_type = whole_subtree;
2095d1caf229SMika Westerberg 
2096d1caf229SMika Westerberg 	/*
2097d1caf229SMika Westerberg 	 * Try to release leaf bridge's resources that doesn't fit resource of
2098d1caf229SMika Westerberg 	 * child device under that bridge.
2099d1caf229SMika Westerberg 	 */
2100d1caf229SMika Westerberg 	list_for_each_entry(fail_res, &fail_head, list)
2101d1caf229SMika Westerberg 		pci_bus_release_bridge_resources(fail_res->dev->bus,
2102d1caf229SMika Westerberg 						 fail_res->flags & PCI_RES_TYPE_MASK,
2103d1caf229SMika Westerberg 						 rel_type);
2104d1caf229SMika Westerberg 
2105d1caf229SMika Westerberg 	/* Restore size and flags */
2106d1caf229SMika Westerberg 	list_for_each_entry(fail_res, &fail_head, list) {
2107d1caf229SMika Westerberg 		struct resource *res = fail_res->res;
2108d1caf229SMika Westerberg 		int idx;
2109d1caf229SMika Westerberg 
2110d1caf229SMika Westerberg 		res->start = fail_res->start;
2111d1caf229SMika Westerberg 		res->end = fail_res->end;
2112d1caf229SMika Westerberg 		res->flags = fail_res->flags;
2113d1caf229SMika Westerberg 
2114d1caf229SMika Westerberg 		if (pci_is_bridge(fail_res->dev)) {
2115d1caf229SMika Westerberg 			idx = res - &fail_res->dev->resource[0];
2116d1caf229SMika Westerberg 			if (idx >= PCI_BRIDGE_RESOURCES &&
2117d1caf229SMika Westerberg 			    idx <= PCI_BRIDGE_RESOURCE_END)
2118d1caf229SMika Westerberg 				res->flags = 0;
2119d1caf229SMika Westerberg 		}
2120d1caf229SMika Westerberg 	}
2121d1caf229SMika Westerberg 	free_list(&fail_head);
2122d1caf229SMika Westerberg 
2123d1caf229SMika Westerberg 	goto again;
2124d1caf229SMika Westerberg 
2125d1caf229SMika Westerberg dump:
2126d1caf229SMika Westerberg 	/* Dump the resource on buses */
2127d1caf229SMika Westerberg 	pci_bus_dump_resources(bus);
2128d1caf229SMika Westerberg }
2129d1caf229SMika Westerberg 
pci_assign_unassigned_resources(void)2130d1caf229SMika Westerberg void __init pci_assign_unassigned_resources(void)
2131d1caf229SMika Westerberg {
2132d1caf229SMika Westerberg 	struct pci_bus *root_bus;
2133d1caf229SMika Westerberg 
2134d1caf229SMika Westerberg 	list_for_each_entry(root_bus, &pci_root_buses, node) {
2135d1caf229SMika Westerberg 		pci_assign_unassigned_root_bus_resources(root_bus);
2136d1caf229SMika Westerberg 
2137d1caf229SMika Westerberg 		/* Make sure the root bridge has a companion ACPI device */
2138d1caf229SMika Westerberg 		if (ACPI_HANDLE(root_bus->bridge))
2139d1caf229SMika Westerberg 			acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
2140d1caf229SMika Westerberg 	}
2141d1caf229SMika Westerberg }
2142d1caf229SMika Westerberg 
pci_assign_unassigned_bridge_resources(struct pci_dev * bridge)21436841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
21446841ec68SYinghai Lu {
21456841ec68SYinghai Lu 	struct pci_bus *parent = bridge->subordinate;
21460d607618SNicholas Johnson 	/* List of resources that want additional resources */
21470d607618SNicholas Johnson 	LIST_HEAD(add_list);
21480d607618SNicholas Johnson 
214932180e40SYinghai Lu 	int tried_times = 0;
2150bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
2151b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
21526841ec68SYinghai Lu 	int retval;
21536841ec68SYinghai Lu 
215432180e40SYinghai Lu again:
21558424d759SYinghai Lu 	__pci_bus_size_bridges(parent, &add_list);
21561a576772SMika Westerberg 
21571a576772SMika Westerberg 	/*
21580d607618SNicholas Johnson 	 * Distribute remaining resources (if any) equally between hotplug
21590d607618SNicholas Johnson 	 * bridges below.  This makes it possible to extend the hierarchy
21600d607618SNicholas Johnson 	 * later without running out of resources.
21611a576772SMika Westerberg 	 */
21621a576772SMika Westerberg 	pci_bridge_distribute_available_resources(bridge, &add_list);
21631a576772SMika Westerberg 
2164bdc4abecSYinghai Lu 	__pci_bridge_assign_resources(bridge, &add_list, &fail_head);
2165bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
216632180e40SYinghai Lu 	tried_times++;
216732180e40SYinghai Lu 
2168bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
21693f579c34SYinghai Lu 		goto enable_all;
217032180e40SYinghai Lu 
217132180e40SYinghai Lu 	if (tried_times >= 2) {
21720d607618SNicholas Johnson 		/* Still fail, don't need to try more */
2173bffc56d4SYinghai Lu 		free_list(&fail_head);
21743f579c34SYinghai Lu 		goto enable_all;
217532180e40SYinghai Lu 	}
217632180e40SYinghai Lu 
217732180e40SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
217832180e40SYinghai Lu 			 tried_times + 1);
217932180e40SYinghai Lu 
218032180e40SYinghai Lu 	/*
21810d607618SNicholas Johnson 	 * Try to release leaf bridge's resources that aren't big enough
21820d607618SNicholas Johnson 	 * to contain child device resources.
218332180e40SYinghai Lu 	 */
218461e83cddSYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list)
218561e83cddSYinghai Lu 		pci_bus_release_bridge_resources(fail_res->dev->bus,
2186cb21bc94SChristian König 						 fail_res->flags & PCI_RES_TYPE_MASK,
218732180e40SYinghai Lu 						 whole_subtree);
218861e83cddSYinghai Lu 
21890d607618SNicholas Johnson 	/* Restore size and flags */
2190b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
2191b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
21929db8dc6dSLogan Gunthorpe 		int idx;
219332180e40SYinghai Lu 
2194b9b0bba9SYinghai Lu 		res->start = fail_res->start;
2195b9b0bba9SYinghai Lu 		res->end = fail_res->end;
2196b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
21979db8dc6dSLogan Gunthorpe 
21989db8dc6dSLogan Gunthorpe 		if (pci_is_bridge(fail_res->dev)) {
21999db8dc6dSLogan Gunthorpe 			idx = res - &fail_res->dev->resource[0];
22009db8dc6dSLogan Gunthorpe 			if (idx >= PCI_BRIDGE_RESOURCES &&
22019db8dc6dSLogan Gunthorpe 			    idx <= PCI_BRIDGE_RESOURCE_END)
220232180e40SYinghai Lu 				res->flags = 0;
220332180e40SYinghai Lu 		}
22049db8dc6dSLogan Gunthorpe 	}
2205bffc56d4SYinghai Lu 	free_list(&fail_head);
220632180e40SYinghai Lu 
220732180e40SYinghai Lu 	goto again;
22083f579c34SYinghai Lu 
22093f579c34SYinghai Lu enable_all:
22103f579c34SYinghai Lu 	retval = pci_reenable_device(bridge);
22119fc9eea0SBjorn Helgaas 	if (retval)
22127506dc79SFrederick Lawler 		pci_err(bridge, "Error reenabling bridge (%d)\n", retval);
22133f579c34SYinghai Lu 	pci_set_master(bridge);
22146841ec68SYinghai Lu }
22156841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
22169b03088fSYinghai Lu 
pci_reassign_bridge_resources(struct pci_dev * bridge,unsigned long type)22178bb705e3SChristian König int pci_reassign_bridge_resources(struct pci_dev *bridge, unsigned long type)
22188bb705e3SChristian König {
22198bb705e3SChristian König 	struct pci_dev_resource *dev_res;
22208bb705e3SChristian König 	struct pci_dev *next;
22218bb705e3SChristian König 	LIST_HEAD(saved);
22228bb705e3SChristian König 	LIST_HEAD(added);
22238bb705e3SChristian König 	LIST_HEAD(failed);
22248bb705e3SChristian König 	unsigned int i;
22258bb705e3SChristian König 	int ret;
22268bb705e3SChristian König 
2227fb794a70SBenjamin Herrenschmidt 	down_read(&pci_bus_sem);
2228fb794a70SBenjamin Herrenschmidt 
22298bb705e3SChristian König 	/* Walk to the root hub, releasing bridge BARs when possible */
22308bb705e3SChristian König 	next = bridge;
22318bb705e3SChristian König 	do {
22328bb705e3SChristian König 		bridge = next;
22338bb705e3SChristian König 		for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCE_END;
22348bb705e3SChristian König 		     i++) {
22358bb705e3SChristian König 			struct resource *res = &bridge->resource[i];
22368bb705e3SChristian König 
22378bb705e3SChristian König 			if ((res->flags ^ type) & PCI_RES_TYPE_MASK)
22388bb705e3SChristian König 				continue;
22398bb705e3SChristian König 
22408bb705e3SChristian König 			/* Ignore BARs which are still in use */
22418bb705e3SChristian König 			if (res->child)
22428bb705e3SChristian König 				continue;
22438bb705e3SChristian König 
22448bb705e3SChristian König 			ret = add_to_list(&saved, bridge, res, 0, 0);
22458bb705e3SChristian König 			if (ret)
22468bb705e3SChristian König 				goto cleanup;
22478bb705e3SChristian König 
22487506dc79SFrederick Lawler 			pci_info(bridge, "BAR %d: releasing %pR\n",
22498bb705e3SChristian König 				 i, res);
22508bb705e3SChristian König 
22518bb705e3SChristian König 			if (res->parent)
22528bb705e3SChristian König 				release_resource(res);
22538bb705e3SChristian König 			res->start = 0;
22548bb705e3SChristian König 			res->end = 0;
22558bb705e3SChristian König 			break;
22568bb705e3SChristian König 		}
22578bb705e3SChristian König 		if (i == PCI_BRIDGE_RESOURCE_END)
22588bb705e3SChristian König 			break;
22598bb705e3SChristian König 
22608bb705e3SChristian König 		next = bridge->bus ? bridge->bus->self : NULL;
22618bb705e3SChristian König 	} while (next);
22628bb705e3SChristian König 
2263fb794a70SBenjamin Herrenschmidt 	if (list_empty(&saved)) {
2264fb794a70SBenjamin Herrenschmidt 		up_read(&pci_bus_sem);
22658bb705e3SChristian König 		return -ENOENT;
2266fb794a70SBenjamin Herrenschmidt 	}
22678bb705e3SChristian König 
22688bb705e3SChristian König 	__pci_bus_size_bridges(bridge->subordinate, &added);
22698bb705e3SChristian König 	__pci_bridge_assign_resources(bridge, &added, &failed);
22708bb705e3SChristian König 	BUG_ON(!list_empty(&added));
22718bb705e3SChristian König 
22728bb705e3SChristian König 	if (!list_empty(&failed)) {
22738bb705e3SChristian König 		ret = -ENOSPC;
22748bb705e3SChristian König 		goto cleanup;
22758bb705e3SChristian König 	}
22768bb705e3SChristian König 
22778bb705e3SChristian König 	list_for_each_entry(dev_res, &saved, list) {
22780d607618SNicholas Johnson 		/* Skip the bridge we just assigned resources for */
22798bb705e3SChristian König 		if (bridge == dev_res->dev)
22808bb705e3SChristian König 			continue;
22818bb705e3SChristian König 
22828bb705e3SChristian König 		bridge = dev_res->dev;
22838bb705e3SChristian König 		pci_setup_bridge(bridge->subordinate);
22848bb705e3SChristian König 	}
22858bb705e3SChristian König 
22868bb705e3SChristian König 	free_list(&saved);
2287fb794a70SBenjamin Herrenschmidt 	up_read(&pci_bus_sem);
22888bb705e3SChristian König 	return 0;
22898bb705e3SChristian König 
22908bb705e3SChristian König cleanup:
22910d607618SNicholas Johnson 	/* Restore size and flags */
22928bb705e3SChristian König 	list_for_each_entry(dev_res, &failed, list) {
22938bb705e3SChristian König 		struct resource *res = dev_res->res;
22948bb705e3SChristian König 
22958bb705e3SChristian König 		res->start = dev_res->start;
22968bb705e3SChristian König 		res->end = dev_res->end;
22978bb705e3SChristian König 		res->flags = dev_res->flags;
22988bb705e3SChristian König 	}
22998bb705e3SChristian König 	free_list(&failed);
23008bb705e3SChristian König 
23018bb705e3SChristian König 	/* Revert to the old configuration */
23028bb705e3SChristian König 	list_for_each_entry(dev_res, &saved, list) {
23038bb705e3SChristian König 		struct resource *res = dev_res->res;
23048bb705e3SChristian König 
23058bb705e3SChristian König 		bridge = dev_res->dev;
23068bb705e3SChristian König 		i = res - bridge->resource;
23078bb705e3SChristian König 
23088bb705e3SChristian König 		res->start = dev_res->start;
23098bb705e3SChristian König 		res->end = dev_res->end;
23108bb705e3SChristian König 		res->flags = dev_res->flags;
23118bb705e3SChristian König 
23128bb705e3SChristian König 		pci_claim_resource(bridge, i);
23138bb705e3SChristian König 		pci_setup_bridge(bridge->subordinate);
23148bb705e3SChristian König 	}
23158bb705e3SChristian König 	free_list(&saved);
2316fb794a70SBenjamin Herrenschmidt 	up_read(&pci_bus_sem);
23178bb705e3SChristian König 
23188bb705e3SChristian König 	return ret;
23198bb705e3SChristian König }
23208bb705e3SChristian König 
pci_assign_unassigned_bus_resources(struct pci_bus * bus)232117787940SYinghai Lu void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
23229b03088fSYinghai Lu {
23239b03088fSYinghai Lu 	struct pci_dev *dev;
23240d607618SNicholas Johnson 	/* List of resources that want additional resources */
23250d607618SNicholas Johnson 	LIST_HEAD(add_list);
23269b03088fSYinghai Lu 
23279b03088fSYinghai Lu 	down_read(&pci_bus_sem);
232824a0c654SAndy Shevchenko 	for_each_pci_bridge(dev, bus)
232924a0c654SAndy Shevchenko 		if (pci_has_subordinate(dev))
233024a0c654SAndy Shevchenko 			__pci_bus_size_bridges(dev->subordinate, &add_list);
23319b03088fSYinghai Lu 	up_read(&pci_bus_sem);
23329b03088fSYinghai Lu 	__pci_bus_assign_resources(bus, &add_list, NULL);
2333bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
233417787940SYinghai Lu }
2335e6b29deaSRay Jui EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);
2336