xref: /openbmc/linux/drivers/pci/setup-bus.c (revision 5b28541552ef5eeffc41d6936105f38c2508e566)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	drivers/pci/setup-bus.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Extruded from code written by
51da177e4SLinus Torvalds  *      Dave Rusling (david.rusling@reo.mts.dec.com)
61da177e4SLinus Torvalds  *      David Mosberger (davidm@cs.arizona.edu)
71da177e4SLinus Torvalds  *	David Miller (davem@redhat.com)
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Support routines for initializing a PCI subsystem.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds /*
131da177e4SLinus Torvalds  * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
141da177e4SLinus Torvalds  *	     PCI-PCI bridges cleanup, sorted resource allocation.
151da177e4SLinus Torvalds  * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
161da177e4SLinus Torvalds  *	     Converted to allocation in 3 passes, which gives
171da177e4SLinus Torvalds  *	     tighter packing. Prefetchable range support.
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/init.h>
211da177e4SLinus Torvalds #include <linux/kernel.h>
221da177e4SLinus Torvalds #include <linux/module.h>
231da177e4SLinus Torvalds #include <linux/pci.h>
241da177e4SLinus Torvalds #include <linux/errno.h>
251da177e4SLinus Torvalds #include <linux/ioport.h>
261da177e4SLinus Torvalds #include <linux/cache.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
2847087700SBjorn Helgaas #include <asm-generic/pci-bridge.h>
296faf17f6SChris Wright #include "pci.h"
301da177e4SLinus Torvalds 
31844393f4SBjorn Helgaas unsigned int pci_flags;
3247087700SBjorn Helgaas 
33bdc4abecSYinghai Lu struct pci_dev_resource {
34bdc4abecSYinghai Lu 	struct list_head list;
352934a0deSYinghai Lu 	struct resource *res;
362934a0deSYinghai Lu 	struct pci_dev *dev;
37568ddef8SYinghai Lu 	resource_size_t start;
38568ddef8SYinghai Lu 	resource_size_t end;
39c8adf9a3SRam Pai 	resource_size_t add_size;
402bbc6942SRam Pai 	resource_size_t min_align;
41568ddef8SYinghai Lu 	unsigned long flags;
42568ddef8SYinghai Lu };
43568ddef8SYinghai Lu 
44bffc56d4SYinghai Lu static void free_list(struct list_head *head)
45bffc56d4SYinghai Lu {
46bffc56d4SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
47bffc56d4SYinghai Lu 
48bffc56d4SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
49bffc56d4SYinghai Lu 		list_del(&dev_res->list);
50bffc56d4SYinghai Lu 		kfree(dev_res);
51bffc56d4SYinghai Lu 	}
52bffc56d4SYinghai Lu }
53094732a5SRam Pai 
54c8adf9a3SRam Pai /**
55c8adf9a3SRam Pai  * add_to_list() - add a new resource tracker to the list
56c8adf9a3SRam Pai  * @head:	Head of the list
57c8adf9a3SRam Pai  * @dev:	device corresponding to which the resource
58c8adf9a3SRam Pai  *		belongs
59c8adf9a3SRam Pai  * @res:	The resource to be tracked
60c8adf9a3SRam Pai  * @add_size:	additional size to be optionally added
61c8adf9a3SRam Pai  *              to the resource
62c8adf9a3SRam Pai  */
63bdc4abecSYinghai Lu static int add_to_list(struct list_head *head,
64c8adf9a3SRam Pai 		 struct pci_dev *dev, struct resource *res,
652bbc6942SRam Pai 		 resource_size_t add_size, resource_size_t min_align)
66568ddef8SYinghai Lu {
67764242a0SYinghai Lu 	struct pci_dev_resource *tmp;
68568ddef8SYinghai Lu 
69bdc4abecSYinghai Lu 	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
70568ddef8SYinghai Lu 	if (!tmp) {
71c8adf9a3SRam Pai 		pr_warning("add_to_list: kmalloc() failed!\n");
72ef62dfefSYinghai Lu 		return -ENOMEM;
73568ddef8SYinghai Lu 	}
74568ddef8SYinghai Lu 
75568ddef8SYinghai Lu 	tmp->res = res;
76568ddef8SYinghai Lu 	tmp->dev = dev;
77568ddef8SYinghai Lu 	tmp->start = res->start;
78568ddef8SYinghai Lu 	tmp->end = res->end;
79568ddef8SYinghai Lu 	tmp->flags = res->flags;
80c8adf9a3SRam Pai 	tmp->add_size = add_size;
812bbc6942SRam Pai 	tmp->min_align = min_align;
82bdc4abecSYinghai Lu 
83bdc4abecSYinghai Lu 	list_add(&tmp->list, head);
84ef62dfefSYinghai Lu 
85ef62dfefSYinghai Lu 	return 0;
86568ddef8SYinghai Lu }
87568ddef8SYinghai Lu 
88b9b0bba9SYinghai Lu static void remove_from_list(struct list_head *head,
893e6e0d80SYinghai Lu 				 struct resource *res)
903e6e0d80SYinghai Lu {
91b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res, *tmp;
923e6e0d80SYinghai Lu 
93b9b0bba9SYinghai Lu 	list_for_each_entry_safe(dev_res, tmp, head, list) {
94b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
95b9b0bba9SYinghai Lu 			list_del(&dev_res->list);
96b9b0bba9SYinghai Lu 			kfree(dev_res);
97bdc4abecSYinghai Lu 			break;
983e6e0d80SYinghai Lu 		}
993e6e0d80SYinghai Lu 	}
1003e6e0d80SYinghai Lu }
1013e6e0d80SYinghai Lu 
102b9b0bba9SYinghai Lu static resource_size_t get_res_add_size(struct list_head *head,
1031c372353SYinghai Lu 					struct resource *res)
1041c372353SYinghai Lu {
105b9b0bba9SYinghai Lu 	struct pci_dev_resource *dev_res;
1061c372353SYinghai Lu 
107b9b0bba9SYinghai Lu 	list_for_each_entry(dev_res, head, list) {
108b9b0bba9SYinghai Lu 		if (dev_res->res == res) {
109b592443dSYinghai Lu 			int idx = res - &dev_res->dev->resource[0];
110b592443dSYinghai Lu 
111b9b0bba9SYinghai Lu 			dev_printk(KERN_DEBUG, &dev_res->dev->dev,
112b592443dSYinghai Lu 				 "res[%d]=%pR get_res_add_size add_size %llx\n",
113b592443dSYinghai Lu 				 idx, dev_res->res,
114b9b0bba9SYinghai Lu 				 (unsigned long long)dev_res->add_size);
115b592443dSYinghai Lu 
116b9b0bba9SYinghai Lu 			return dev_res->add_size;
117bdc4abecSYinghai Lu 		}
1183e6e0d80SYinghai Lu 	}
1191c372353SYinghai Lu 
1201c372353SYinghai Lu 	return 0;
1211c372353SYinghai Lu }
1221c372353SYinghai Lu 
12378c3b329SYinghai Lu /* Sort resources by alignment */
124bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
12578c3b329SYinghai Lu {
12678c3b329SYinghai Lu 	int i;
12778c3b329SYinghai Lu 
12878c3b329SYinghai Lu 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
12978c3b329SYinghai Lu 		struct resource *r;
130bdc4abecSYinghai Lu 		struct pci_dev_resource *dev_res, *tmp;
13178c3b329SYinghai Lu 		resource_size_t r_align;
132bdc4abecSYinghai Lu 		struct list_head *n;
13378c3b329SYinghai Lu 
13478c3b329SYinghai Lu 		r = &dev->resource[i];
13578c3b329SYinghai Lu 
13678c3b329SYinghai Lu 		if (r->flags & IORESOURCE_PCI_FIXED)
13778c3b329SYinghai Lu 			continue;
13878c3b329SYinghai Lu 
13978c3b329SYinghai Lu 		if (!(r->flags) || r->parent)
14078c3b329SYinghai Lu 			continue;
14178c3b329SYinghai Lu 
14278c3b329SYinghai Lu 		r_align = pci_resource_alignment(dev, r);
14378c3b329SYinghai Lu 		if (!r_align) {
14478c3b329SYinghai Lu 			dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
14578c3b329SYinghai Lu 				 i, r);
14678c3b329SYinghai Lu 			continue;
14778c3b329SYinghai Lu 		}
14878c3b329SYinghai Lu 
149bdc4abecSYinghai Lu 		tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
15078c3b329SYinghai Lu 		if (!tmp)
15178c3b329SYinghai Lu 			panic("pdev_sort_resources(): "
15278c3b329SYinghai Lu 			      "kmalloc() failed!\n");
15378c3b329SYinghai Lu 		tmp->res = r;
15478c3b329SYinghai Lu 		tmp->dev = dev;
155bdc4abecSYinghai Lu 
156bdc4abecSYinghai Lu 		/* fallback is smallest one or list is empty*/
157bdc4abecSYinghai Lu 		n = head;
158bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
159bdc4abecSYinghai Lu 			resource_size_t align;
160bdc4abecSYinghai Lu 
161bdc4abecSYinghai Lu 			align = pci_resource_alignment(dev_res->dev,
162bdc4abecSYinghai Lu 							 dev_res->res);
163bdc4abecSYinghai Lu 
164bdc4abecSYinghai Lu 			if (r_align > align) {
165bdc4abecSYinghai Lu 				n = &dev_res->list;
16678c3b329SYinghai Lu 				break;
16778c3b329SYinghai Lu 			}
16878c3b329SYinghai Lu 		}
169bdc4abecSYinghai Lu 		/* Insert it just before n*/
170bdc4abecSYinghai Lu 		list_add_tail(&tmp->list, n);
17178c3b329SYinghai Lu 	}
17278c3b329SYinghai Lu }
17378c3b329SYinghai Lu 
1746841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev,
175bdc4abecSYinghai Lu 				 struct list_head *head)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	u16 class = dev->class >> 8;
1781da177e4SLinus Torvalds 
1799bded00bSKenji Kaneshige 	/* 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 
1839bded00bSKenji Kaneshige 	/* 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 
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 /**
2029e8bf93aSRam Pai  * reassign_resources_sorted() - satisfy any additional resource requests
203c8adf9a3SRam Pai  *
2049e8bf93aSRam Pai  * @realloc_head : head of the list tracking requests requiring additional
205c8adf9a3SRam Pai  *             resources
206c8adf9a3SRam Pai  * @head     : head of the list tracking requests with allocated
207c8adf9a3SRam Pai  *             resources
208c8adf9a3SRam Pai  *
2099e8bf93aSRam Pai  * Walk through each element of the realloc_head and try to procure
210c8adf9a3SRam Pai  * additional resources for the element, provided the element
211c8adf9a3SRam Pai  * is in the head list.
212c8adf9a3SRam Pai  */
213bdc4abecSYinghai Lu static void reassign_resources_sorted(struct list_head *realloc_head,
214bdc4abecSYinghai Lu 		struct list_head *head)
215c8adf9a3SRam Pai {
216c8adf9a3SRam Pai 	struct resource *res;
217b9b0bba9SYinghai Lu 	struct pci_dev_resource *add_res, *tmp;
218bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
219c8adf9a3SRam Pai 	resource_size_t add_size;
220c8adf9a3SRam Pai 	int idx;
221c8adf9a3SRam Pai 
222b9b0bba9SYinghai Lu 	list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
223bdc4abecSYinghai Lu 		bool found_match = false;
224bdc4abecSYinghai Lu 
225b9b0bba9SYinghai Lu 		res = add_res->res;
226c8adf9a3SRam Pai 		/* skip resource that has been reset */
227c8adf9a3SRam Pai 		if (!res->flags)
228c8adf9a3SRam Pai 			goto out;
229c8adf9a3SRam Pai 
230c8adf9a3SRam Pai 		/* skip this resource if not found in head list */
231bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list) {
232bdc4abecSYinghai Lu 			if (dev_res->res == res) {
233bdc4abecSYinghai Lu 				found_match = true;
234bdc4abecSYinghai Lu 				break;
235c8adf9a3SRam Pai 			}
236bdc4abecSYinghai Lu 		}
237bdc4abecSYinghai Lu 		if (!found_match)/* just skip */
238bdc4abecSYinghai Lu 			continue;
239c8adf9a3SRam Pai 
240b9b0bba9SYinghai Lu 		idx = res - &add_res->dev->resource[0];
241b9b0bba9SYinghai Lu 		add_size = add_res->add_size;
2422bbc6942SRam Pai 		if (!resource_size(res)) {
243b9b0bba9SYinghai Lu 			res->start = add_res->start;
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 			resource_size_t align = add_res->min_align;
249b9b0bba9SYinghai Lu 			res->flags |= add_res->flags &
250bdc4abecSYinghai Lu 				 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
251b9b0bba9SYinghai Lu 			if (pci_reassign_resource(add_res->dev, idx,
252bdc4abecSYinghai Lu 						  add_size, align))
253b9b0bba9SYinghai Lu 				dev_printk(KERN_DEBUG, &add_res->dev->dev,
254b592443dSYinghai Lu 					   "failed to add %llx res[%d]=%pR\n",
255b592443dSYinghai Lu 					   (unsigned long long)add_size,
256b592443dSYinghai Lu 					   idx, res);
257c8adf9a3SRam Pai 		}
258c8adf9a3SRam Pai out:
259b9b0bba9SYinghai Lu 		list_del(&add_res->list);
260b9b0bba9SYinghai Lu 		kfree(add_res);
261c8adf9a3SRam Pai 	}
262c8adf9a3SRam Pai }
263c8adf9a3SRam Pai 
264c8adf9a3SRam Pai /**
265c8adf9a3SRam Pai  * assign_requested_resources_sorted() - satisfy resource requests
266c8adf9a3SRam Pai  *
267c8adf9a3SRam Pai  * @head : head of the list tracking requests for resources
2688356aad4SWanpeng Li  * @fail_head : head of the list tracking requests that could
269c8adf9a3SRam Pai  *		not be allocated
270c8adf9a3SRam Pai  *
271c8adf9a3SRam Pai  * Satisfy resource requests of each element in the list. Add
272c8adf9a3SRam Pai  * requests that could not satisfied to the failed_list.
273c8adf9a3SRam Pai  */
274bdc4abecSYinghai Lu static void assign_requested_resources_sorted(struct list_head *head,
275bdc4abecSYinghai Lu 				 struct list_head *fail_head)
2766841ec68SYinghai Lu {
2776841ec68SYinghai Lu 	struct resource *res;
278bdc4abecSYinghai Lu 	struct pci_dev_resource *dev_res;
2796841ec68SYinghai Lu 	int idx;
2806841ec68SYinghai Lu 
281bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
282bdc4abecSYinghai Lu 		res = dev_res->res;
283bdc4abecSYinghai Lu 		idx = res - &dev_res->dev->resource[0];
284bdc4abecSYinghai Lu 		if (resource_size(res) &&
285bdc4abecSYinghai Lu 		    pci_assign_resource(dev_res->dev, idx)) {
286a3cb999dSYinghai Lu 			if (fail_head) {
2879a928660SYinghai Lu 				/*
2889a928660SYinghai Lu 				 * if the failed res is for ROM BAR, and it will
2899a928660SYinghai Lu 				 * be enabled later, don't add it to the list
2909a928660SYinghai Lu 				 */
2919a928660SYinghai Lu 				if (!((idx == PCI_ROM_RESOURCE) &&
2929a928660SYinghai Lu 				      (!(res->flags & IORESOURCE_ROM_ENABLE))))
29367cc7e26SYinghai Lu 					add_to_list(fail_head,
29467cc7e26SYinghai Lu 						    dev_res->dev, res,
295f7625980SBjorn Helgaas 						    0 /* don't care */,
296f7625980SBjorn Helgaas 						    0 /* don't care */);
2979a928660SYinghai Lu 			}
298fc075e1dSRam Pai 			reset_resource(res);
299542df5deSRajesh Shah 		}
3001da177e4SLinus Torvalds 	}
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
303aa914f5eSYinghai Lu static unsigned long pci_fail_res_type_mask(struct list_head *fail_head)
304aa914f5eSYinghai Lu {
305aa914f5eSYinghai Lu 	struct pci_dev_resource *fail_res;
306aa914f5eSYinghai Lu 	unsigned long mask = 0;
307aa914f5eSYinghai Lu 
308aa914f5eSYinghai Lu 	/* check failed type */
309aa914f5eSYinghai Lu 	list_for_each_entry(fail_res, fail_head, list)
310aa914f5eSYinghai Lu 		mask |= fail_res->flags;
311aa914f5eSYinghai Lu 
312aa914f5eSYinghai Lu 	/*
313aa914f5eSYinghai Lu 	 * one pref failed resource will set IORESOURCE_MEM,
314aa914f5eSYinghai Lu 	 * as we can allocate pref in non-pref range.
315aa914f5eSYinghai Lu 	 * Will release all assigned non-pref sibling resources
316aa914f5eSYinghai Lu 	 * according to that bit.
317aa914f5eSYinghai Lu 	 */
318aa914f5eSYinghai Lu 	return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
319aa914f5eSYinghai Lu }
320aa914f5eSYinghai Lu 
321aa914f5eSYinghai Lu static bool pci_need_to_release(unsigned long mask, struct resource *res)
322aa914f5eSYinghai Lu {
323aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_IO)
324aa914f5eSYinghai Lu 		return !!(mask & IORESOURCE_IO);
325aa914f5eSYinghai Lu 
326aa914f5eSYinghai Lu 	/* check pref at first */
327aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_PREFETCH) {
328aa914f5eSYinghai Lu 		if (mask & IORESOURCE_PREFETCH)
329aa914f5eSYinghai Lu 			return true;
330aa914f5eSYinghai Lu 		/* count pref if its parent is non-pref */
331aa914f5eSYinghai Lu 		else if ((mask & IORESOURCE_MEM) &&
332aa914f5eSYinghai Lu 			 !(res->parent->flags & IORESOURCE_PREFETCH))
333aa914f5eSYinghai Lu 			return true;
334aa914f5eSYinghai Lu 		else
335aa914f5eSYinghai Lu 			return false;
336aa914f5eSYinghai Lu 	}
337aa914f5eSYinghai Lu 
338aa914f5eSYinghai Lu 	if (res->flags & IORESOURCE_MEM)
339aa914f5eSYinghai Lu 		return !!(mask & IORESOURCE_MEM);
340aa914f5eSYinghai Lu 
341aa914f5eSYinghai Lu 	return false;	/* should not get here */
342aa914f5eSYinghai Lu }
343aa914f5eSYinghai Lu 
344bdc4abecSYinghai Lu static void __assign_resources_sorted(struct list_head *head,
345bdc4abecSYinghai Lu 				 struct list_head *realloc_head,
346bdc4abecSYinghai Lu 				 struct list_head *fail_head)
347c8adf9a3SRam Pai {
3483e6e0d80SYinghai Lu 	/*
3493e6e0d80SYinghai Lu 	 * Should not assign requested resources at first.
3503e6e0d80SYinghai Lu 	 *   they could be adjacent, so later reassign can not reallocate
3513e6e0d80SYinghai Lu 	 *   them one by one in parent resource window.
352367fa982SMasanari Iida 	 * Try to assign requested + add_size at beginning
3533e6e0d80SYinghai Lu 	 *  if could do that, could get out early.
3543e6e0d80SYinghai Lu 	 *  if could not do that, we still try to assign requested at first,
3553e6e0d80SYinghai Lu 	 *    then try to reassign add_size for some resources.
356aa914f5eSYinghai Lu 	 *
357aa914f5eSYinghai Lu 	 * Separate three resource type checking if we need to release
358aa914f5eSYinghai Lu 	 * assigned resource after requested + add_size try.
359aa914f5eSYinghai Lu 	 *	1. if there is io port assign fail, will release assigned
360aa914f5eSYinghai Lu 	 *	   io port.
361aa914f5eSYinghai Lu 	 *	2. if there is pref mmio assign fail, release assigned
362aa914f5eSYinghai Lu 	 *	   pref mmio.
363aa914f5eSYinghai Lu 	 *	   if assigned pref mmio's parent is non-pref mmio and there
364aa914f5eSYinghai Lu 	 *	   is non-pref mmio assign fail, will release that assigned
365aa914f5eSYinghai Lu 	 *	   pref mmio.
366aa914f5eSYinghai Lu 	 *	3. if there is non-pref mmio assign fail or pref mmio
367aa914f5eSYinghai Lu 	 *	   assigned fail, will release assigned non-pref mmio.
3683e6e0d80SYinghai Lu 	 */
369bdc4abecSYinghai Lu 	LIST_HEAD(save_head);
370bdc4abecSYinghai Lu 	LIST_HEAD(local_fail_head);
371b9b0bba9SYinghai Lu 	struct pci_dev_resource *save_res;
372aa914f5eSYinghai Lu 	struct pci_dev_resource *dev_res, *tmp_res;
373aa914f5eSYinghai Lu 	unsigned long fail_type;
3743e6e0d80SYinghai Lu 
3753e6e0d80SYinghai Lu 	/* Check if optional add_size is there */
376bdc4abecSYinghai Lu 	if (!realloc_head || list_empty(realloc_head))
3773e6e0d80SYinghai Lu 		goto requested_and_reassign;
3783e6e0d80SYinghai Lu 
3793e6e0d80SYinghai Lu 	/* Save original start, end, flags etc at first */
380bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list) {
381bdc4abecSYinghai Lu 		if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
382bffc56d4SYinghai Lu 			free_list(&save_head);
3833e6e0d80SYinghai Lu 			goto requested_and_reassign;
3843e6e0d80SYinghai Lu 		}
385bdc4abecSYinghai Lu 	}
3863e6e0d80SYinghai Lu 
3873e6e0d80SYinghai Lu 	/* Update res in head list with add_size in realloc_head list */
388bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list)
389bdc4abecSYinghai Lu 		dev_res->res->end += get_res_add_size(realloc_head,
390bdc4abecSYinghai Lu 							dev_res->res);
3913e6e0d80SYinghai Lu 
3923e6e0d80SYinghai Lu 	/* Try updated head list with add_size added */
3933e6e0d80SYinghai Lu 	assign_requested_resources_sorted(head, &local_fail_head);
3943e6e0d80SYinghai Lu 
3953e6e0d80SYinghai Lu 	/* all assigned with add_size ? */
396bdc4abecSYinghai Lu 	if (list_empty(&local_fail_head)) {
3973e6e0d80SYinghai Lu 		/* Remove head list from realloc_head list */
398bdc4abecSYinghai Lu 		list_for_each_entry(dev_res, head, list)
399bdc4abecSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
400bffc56d4SYinghai Lu 		free_list(&save_head);
401bffc56d4SYinghai Lu 		free_list(head);
4023e6e0d80SYinghai Lu 		return;
4033e6e0d80SYinghai Lu 	}
4043e6e0d80SYinghai Lu 
405aa914f5eSYinghai Lu 	/* check failed type */
406aa914f5eSYinghai Lu 	fail_type = pci_fail_res_type_mask(&local_fail_head);
407aa914f5eSYinghai Lu 	/* remove not need to be released assigned res from head list etc */
408aa914f5eSYinghai Lu 	list_for_each_entry_safe(dev_res, tmp_res, head, list)
409aa914f5eSYinghai Lu 		if (dev_res->res->parent &&
410aa914f5eSYinghai Lu 		    !pci_need_to_release(fail_type, dev_res->res)) {
411aa914f5eSYinghai Lu 			/* remove it from realloc_head list */
412aa914f5eSYinghai Lu 			remove_from_list(realloc_head, dev_res->res);
413aa914f5eSYinghai Lu 			remove_from_list(&save_head, dev_res->res);
414aa914f5eSYinghai Lu 			list_del(&dev_res->list);
415aa914f5eSYinghai Lu 			kfree(dev_res);
416aa914f5eSYinghai Lu 		}
417aa914f5eSYinghai Lu 
418bffc56d4SYinghai Lu 	free_list(&local_fail_head);
4193e6e0d80SYinghai Lu 	/* Release assigned resource */
420bdc4abecSYinghai Lu 	list_for_each_entry(dev_res, head, list)
421bdc4abecSYinghai Lu 		if (dev_res->res->parent)
422bdc4abecSYinghai Lu 			release_resource(dev_res->res);
4233e6e0d80SYinghai Lu 	/* Restore start/end/flags from saved list */
424b9b0bba9SYinghai Lu 	list_for_each_entry(save_res, &save_head, list) {
425b9b0bba9SYinghai Lu 		struct resource *res = save_res->res;
4263e6e0d80SYinghai Lu 
427b9b0bba9SYinghai Lu 		res->start = save_res->start;
428b9b0bba9SYinghai Lu 		res->end = save_res->end;
429b9b0bba9SYinghai Lu 		res->flags = save_res->flags;
4303e6e0d80SYinghai Lu 	}
431bffc56d4SYinghai Lu 	free_list(&save_head);
4323e6e0d80SYinghai Lu 
4333e6e0d80SYinghai Lu requested_and_reassign:
434c8adf9a3SRam Pai 	/* Satisfy the must-have resource requests */
435c8adf9a3SRam Pai 	assign_requested_resources_sorted(head, fail_head);
436c8adf9a3SRam Pai 
4370a2daa1cSRam Pai 	/* Try to satisfy any additional optional resource
438c8adf9a3SRam Pai 		requests */
4399e8bf93aSRam Pai 	if (realloc_head)
4409e8bf93aSRam Pai 		reassign_resources_sorted(realloc_head, head);
441bffc56d4SYinghai Lu 	free_list(head);
442c8adf9a3SRam Pai }
443c8adf9a3SRam Pai 
4446841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev,
445bdc4abecSYinghai Lu 				 struct list_head *add_head,
446bdc4abecSYinghai Lu 				 struct list_head *fail_head)
4476841ec68SYinghai Lu {
448bdc4abecSYinghai Lu 	LIST_HEAD(head);
4496841ec68SYinghai Lu 
4506841ec68SYinghai Lu 	__dev_sort_resources(dev, &head);
4518424d759SYinghai Lu 	__assign_resources_sorted(&head, add_head, fail_head);
4526841ec68SYinghai Lu 
4536841ec68SYinghai Lu }
4546841ec68SYinghai Lu 
4556841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus,
456bdc4abecSYinghai Lu 					 struct list_head *realloc_head,
457bdc4abecSYinghai Lu 					 struct list_head *fail_head)
4586841ec68SYinghai Lu {
4596841ec68SYinghai Lu 	struct pci_dev *dev;
460bdc4abecSYinghai Lu 	LIST_HEAD(head);
4616841ec68SYinghai Lu 
4626841ec68SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
4636841ec68SYinghai Lu 		__dev_sort_resources(dev, &head);
4646841ec68SYinghai Lu 
4659e8bf93aSRam Pai 	__assign_resources_sorted(&head, realloc_head, fail_head);
4666841ec68SYinghai Lu }
4676841ec68SYinghai Lu 
468b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
471c7dabef8SBjorn Helgaas 	struct resource *res;
4721da177e4SLinus Torvalds 	struct pci_bus_region region;
4731da177e4SLinus Torvalds 
474b918c62eSYinghai Lu 	dev_info(&bridge->dev, "CardBus bridge to %pR\n",
475b918c62eSYinghai Lu 		 &bus->busn_res);
4761da177e4SLinus Torvalds 
477c7dabef8SBjorn Helgaas 	res = bus->resource[0];
478fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
479c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
4801da177e4SLinus Torvalds 		/*
4811da177e4SLinus Torvalds 		 * The IO resource is allocated a range twice as large as it
4821da177e4SLinus Torvalds 		 * would normally need.  This allows us to set both IO regs.
4831da177e4SLinus Torvalds 		 */
484c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4851da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
4861da177e4SLinus Torvalds 					region.start);
4871da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
4881da177e4SLinus Torvalds 					region.end);
4891da177e4SLinus Torvalds 	}
4901da177e4SLinus Torvalds 
491c7dabef8SBjorn Helgaas 	res = bus->resource[1];
492fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
493c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
494c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
4951da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
4961da177e4SLinus Torvalds 					region.start);
4971da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
4981da177e4SLinus Torvalds 					region.end);
4991da177e4SLinus Torvalds 	}
5001da177e4SLinus Torvalds 
501c7dabef8SBjorn Helgaas 	res = bus->resource[2];
502fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
503c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
504c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5051da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
5061da177e4SLinus Torvalds 					region.start);
5071da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
5081da177e4SLinus Torvalds 					region.end);
5091da177e4SLinus Torvalds 	}
5101da177e4SLinus Torvalds 
511c7dabef8SBjorn Helgaas 	res = bus->resource[3];
512fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
513c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
514c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5151da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
5161da177e4SLinus Torvalds 					region.start);
5171da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
5181da177e4SLinus Torvalds 					region.end);
5191da177e4SLinus Torvalds 	}
5201da177e4SLinus Torvalds }
521b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus);
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected.
5241da177e4SLinus Torvalds    PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
5251da177e4SLinus Torvalds    requires that if there is no I/O ports or memory behind the
5261da177e4SLinus Torvalds    bridge, corresponding range must be turned off by writing base
5271da177e4SLinus Torvalds    value greater than limit to the bridge's base/limit registers.
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds    Note: care must be taken when updating I/O base/limit registers
5301da177e4SLinus Torvalds    of bridges which support 32-bit I/O. This update requires two
5311da177e4SLinus Torvalds    config space writes, so it's quite possible that an I/O window of
5321da177e4SLinus Torvalds    the bridge will have some undesirable address (e.g. 0) after the
5331da177e4SLinus Torvalds    first write. Ditto 64-bit prefetchable MMIO.  */
5347cc5997dSYinghai Lu static void pci_setup_bridge_io(struct pci_bus *bus)
5351da177e4SLinus Torvalds {
5361da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
537c7dabef8SBjorn Helgaas 	struct resource *res;
5381da177e4SLinus Torvalds 	struct pci_bus_region region;
5392b28ae19SBjorn Helgaas 	unsigned long io_mask;
5402b28ae19SBjorn Helgaas 	u8 io_base_lo, io_limit_lo;
5415b764b83SBjorn Helgaas 	u16 l;
5425b764b83SBjorn Helgaas 	u32 io_upper16;
5431da177e4SLinus Torvalds 
5442b28ae19SBjorn Helgaas 	io_mask = PCI_IO_RANGE_MASK;
5452b28ae19SBjorn Helgaas 	if (bridge->io_window_1k)
5462b28ae19SBjorn Helgaas 		io_mask = PCI_IO_1K_RANGE_MASK;
5472b28ae19SBjorn Helgaas 
5481da177e4SLinus Torvalds 	/* Set up the top and bottom of the PCI I/O segment for this bus. */
549c7dabef8SBjorn Helgaas 	res = bus->resource[0];
550fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
551c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
5525b764b83SBjorn Helgaas 		pci_read_config_word(bridge, PCI_IO_BASE, &l);
5532b28ae19SBjorn Helgaas 		io_base_lo = (region.start >> 8) & io_mask;
5542b28ae19SBjorn Helgaas 		io_limit_lo = (region.end >> 8) & io_mask;
5555b764b83SBjorn Helgaas 		l = ((u16) io_limit_lo << 8) | io_base_lo;
5561da177e4SLinus Torvalds 		/* Set up upper 16 bits of I/O base/limit. */
5571da177e4SLinus Torvalds 		io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
558c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5597cc5997dSYinghai Lu 	} else {
5601da177e4SLinus Torvalds 		/* Clear upper 16 bits of I/O base/limit. */
5611da177e4SLinus Torvalds 		io_upper16 = 0;
5621da177e4SLinus Torvalds 		l = 0x00f0;
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds 	/* Temporarily disable the I/O range before updating PCI_IO_BASE. */
5651da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
5661da177e4SLinus Torvalds 	/* Update lower 16 bits of I/O base/limit. */
5675b764b83SBjorn Helgaas 	pci_write_config_word(bridge, PCI_IO_BASE, l);
5681da177e4SLinus Torvalds 	/* Update upper 16 bits of I/O base/limit. */
5691da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
5707cc5997dSYinghai Lu }
5711da177e4SLinus Torvalds 
5727cc5997dSYinghai Lu static void pci_setup_bridge_mmio(struct pci_bus *bus)
5737cc5997dSYinghai Lu {
5747cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
5757cc5997dSYinghai Lu 	struct resource *res;
5767cc5997dSYinghai Lu 	struct pci_bus_region region;
5777cc5997dSYinghai Lu 	u32 l;
5787cc5997dSYinghai Lu 
5797cc5997dSYinghai Lu 	/* Set up the top and bottom of the PCI Memory segment for this bus. */
580c7dabef8SBjorn Helgaas 	res = bus->resource[1];
581fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
582c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
5831da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
5841da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
585c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
5867cc5997dSYinghai Lu 	} else {
5871da177e4SLinus Torvalds 		l = 0x0000fff0;
5881da177e4SLinus Torvalds 	}
5891da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
5907cc5997dSYinghai Lu }
5917cc5997dSYinghai Lu 
5927cc5997dSYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
5937cc5997dSYinghai Lu {
5947cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
5957cc5997dSYinghai Lu 	struct resource *res;
5967cc5997dSYinghai Lu 	struct pci_bus_region region;
5977cc5997dSYinghai Lu 	u32 l, bu, lu;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	/* Clear out the upper 32 bits of PREF limit.
6001da177e4SLinus Torvalds 	   If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
6011da177e4SLinus Torvalds 	   disables PREF range, which is ok. */
6021da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	/* Set up PREF base/limit. */
605c40a22e0SBenjamin Herrenschmidt 	bu = lu = 0;
606c7dabef8SBjorn Helgaas 	res = bus->resource[2];
607fc279850SYinghai Lu 	pcibios_resource_to_bus(bridge->bus, &region, res);
608c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_PREFETCH) {
6091da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
6101da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
611c7dabef8SBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64) {
61213d36c24SAndrew Morton 			bu = upper_32_bits(region.start);
61313d36c24SAndrew Morton 			lu = upper_32_bits(region.end);
6141f82de10SYinghai Lu 		}
615c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
6167cc5997dSYinghai Lu 	} else {
6171da177e4SLinus Torvalds 		l = 0x0000fff0;
6181da177e4SLinus Torvalds 	}
6191da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
6201da177e4SLinus Torvalds 
621c40a22e0SBenjamin Herrenschmidt 	/* Set the upper 32 bits of PREF base & limit. */
622c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
623c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
6247cc5997dSYinghai Lu }
6257cc5997dSYinghai Lu 
6267cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
6277cc5997dSYinghai Lu {
6287cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
6297cc5997dSYinghai Lu 
630b918c62eSYinghai Lu 	dev_info(&bridge->dev, "PCI bridge to %pR\n",
631b918c62eSYinghai Lu 		 &bus->busn_res);
6327cc5997dSYinghai Lu 
6337cc5997dSYinghai Lu 	if (type & IORESOURCE_IO)
6347cc5997dSYinghai Lu 		pci_setup_bridge_io(bus);
6357cc5997dSYinghai Lu 
6367cc5997dSYinghai Lu 	if (type & IORESOURCE_MEM)
6377cc5997dSYinghai Lu 		pci_setup_bridge_mmio(bus);
6387cc5997dSYinghai Lu 
6397cc5997dSYinghai Lu 	if (type & IORESOURCE_PREFETCH)
6407cc5997dSYinghai Lu 		pci_setup_bridge_mmio_pref(bus);
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
6431da177e4SLinus Torvalds }
6441da177e4SLinus Torvalds 
645e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus)
6467cc5997dSYinghai Lu {
6477cc5997dSYinghai Lu 	unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
6487cc5997dSYinghai Lu 				  IORESOURCE_PREFETCH;
6497cc5997dSYinghai Lu 
6507cc5997dSYinghai Lu 	__pci_setup_bridge(bus, type);
6517cc5997dSYinghai Lu }
6527cc5997dSYinghai Lu 
6531da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and
6541da177e4SLinus Torvalds    prefetchable memory ranges. If not, the respective
6551da177e4SLinus Torvalds    base/limit registers must be read-only and read as 0. */
65696bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus)
6571da177e4SLinus Torvalds {
6581da177e4SLinus Torvalds 	u16 io;
6591da177e4SLinus Torvalds 	u32 pmem;
6601da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
6611da177e4SLinus Torvalds 	struct resource *b_res;
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
6641da177e4SLinus Torvalds 	b_res[1].flags |= IORESOURCE_MEM;
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_IO_BASE, &io);
6671da177e4SLinus Torvalds 	if (!io) {
668d2f54d9bSBjorn Helgaas 		pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0);
6691da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_IO_BASE, &io);
6701da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
6711da177e4SLinus Torvalds 	}
6721da177e4SLinus Torvalds 	if (io)
6731da177e4SLinus Torvalds 		b_res[0].flags |= IORESOURCE_IO;
674d2f54d9bSBjorn Helgaas 
6751da177e4SLinus Torvalds 	/*  DECchip 21050 pass 2 errata: the bridge may miss an address
6761da177e4SLinus Torvalds 	    disconnect boundary by one PCI data phase.
6771da177e4SLinus Torvalds 	    Workaround: do not use prefetching on this device. */
6781da177e4SLinus Torvalds 	if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
6791da177e4SLinus Torvalds 		return;
680d2f54d9bSBjorn Helgaas 
6811da177e4SLinus Torvalds 	pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
6821da177e4SLinus Torvalds 	if (!pmem) {
6831da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
684d2f54d9bSBjorn Helgaas 					       0xffe0fff0);
6851da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
6861da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
6871da177e4SLinus Torvalds 	}
6881f82de10SYinghai Lu 	if (pmem) {
6891da177e4SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
69099586105SYinghai Lu 		if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
69199586105SYinghai Lu 		    PCI_PREF_RANGE_TYPE_64) {
6921f82de10SYinghai Lu 			b_res[2].flags |= IORESOURCE_MEM_64;
69399586105SYinghai Lu 			b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
69499586105SYinghai Lu 		}
6951f82de10SYinghai Lu 	}
6961f82de10SYinghai Lu 
6971f82de10SYinghai Lu 	/* double check if bridge does support 64 bit pref */
6981f82de10SYinghai Lu 	if (b_res[2].flags & IORESOURCE_MEM_64) {
6991f82de10SYinghai Lu 		u32 mem_base_hi, tmp;
7001f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
7011f82de10SYinghai Lu 					 &mem_base_hi);
7021f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
7031f82de10SYinghai Lu 					       0xffffffff);
7041f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
7051f82de10SYinghai Lu 		if (!tmp)
7061f82de10SYinghai Lu 			b_res[2].flags &= ~IORESOURCE_MEM_64;
7071f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
7081f82de10SYinghai Lu 				       mem_base_hi);
7091f82de10SYinghai Lu 	}
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds /* Helper function for sizing routines: find first available
7131da177e4SLinus Torvalds    bus resource of a given type. Note: we intentionally skip
7141da177e4SLinus Torvalds    the bus resources which have already been assigned (that is,
7151da177e4SLinus Torvalds    have non-NULL parent resource). */
716*5b285415SYinghai Lu static struct resource *find_free_bus_resource(struct pci_bus *bus,
717*5b285415SYinghai Lu 			 unsigned long type_mask, unsigned long type)
7181da177e4SLinus Torvalds {
7191da177e4SLinus Torvalds 	int i;
7201da177e4SLinus Torvalds 	struct resource *r;
7211da177e4SLinus Torvalds 
72289a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, r, i) {
723299de034SIvan Kokshaysky 		if (r == &ioport_resource || r == &iomem_resource)
724299de034SIvan Kokshaysky 			continue;
72555a10984SJesse Barnes 		if (r && (r->flags & type_mask) == type && !r->parent)
7261da177e4SLinus Torvalds 			return r;
7271da177e4SLinus Torvalds 	}
7281da177e4SLinus Torvalds 	return NULL;
7291da177e4SLinus Torvalds }
7301da177e4SLinus Torvalds 
73113583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size,
73213583b16SRam Pai 		resource_size_t min_size,
73313583b16SRam Pai 		resource_size_t size1,
73413583b16SRam Pai 		resource_size_t old_size,
73513583b16SRam Pai 		resource_size_t align)
73613583b16SRam Pai {
73713583b16SRam Pai 	if (size < min_size)
73813583b16SRam Pai 		size = min_size;
73913583b16SRam Pai 	if (old_size == 1 )
74013583b16SRam Pai 		old_size = 0;
74113583b16SRam Pai 	/* To be fixed in 2.5: we should have sort of HAVE_ISA
74213583b16SRam Pai 	   flag in the struct pci_bus. */
74313583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
74413583b16SRam Pai 	size = (size & 0xff) + ((size & ~0xffUL) << 2);
74513583b16SRam Pai #endif
74613583b16SRam Pai 	size = ALIGN(size + size1, align);
74713583b16SRam Pai 	if (size < old_size)
74813583b16SRam Pai 		size = old_size;
74913583b16SRam Pai 	return size;
75013583b16SRam Pai }
75113583b16SRam Pai 
75213583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size,
75313583b16SRam Pai 		resource_size_t min_size,
75413583b16SRam Pai 		resource_size_t size1,
75513583b16SRam Pai 		resource_size_t old_size,
75613583b16SRam Pai 		resource_size_t align)
75713583b16SRam Pai {
75813583b16SRam Pai 	if (size < min_size)
75913583b16SRam Pai 		size = min_size;
76013583b16SRam Pai 	if (old_size == 1 )
76113583b16SRam Pai 		old_size = 0;
76213583b16SRam Pai 	if (size < old_size)
76313583b16SRam Pai 		size = old_size;
76413583b16SRam Pai 	size = ALIGN(size + size1, align);
76513583b16SRam Pai 	return size;
76613583b16SRam Pai }
76713583b16SRam Pai 
768ac5ad93eSGavin Shan resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
769ac5ad93eSGavin Shan 						unsigned long type)
770ac5ad93eSGavin Shan {
771ac5ad93eSGavin Shan 	return 1;
772ac5ad93eSGavin Shan }
773ac5ad93eSGavin Shan 
774ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_MEM_ALIGN	0x100000	/* 1MiB */
775ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN	0x1000		/* 4KiB */
776ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN_1K	0x400		/* 1KiB */
777ac5ad93eSGavin Shan 
778ac5ad93eSGavin Shan static resource_size_t window_alignment(struct pci_bus *bus,
779ac5ad93eSGavin Shan 					unsigned long type)
780ac5ad93eSGavin Shan {
781ac5ad93eSGavin Shan 	resource_size_t align = 1, arch_align;
782ac5ad93eSGavin Shan 
783ac5ad93eSGavin Shan 	if (type & IORESOURCE_MEM)
784ac5ad93eSGavin Shan 		align = PCI_P2P_DEFAULT_MEM_ALIGN;
785ac5ad93eSGavin Shan 	else if (type & IORESOURCE_IO) {
786ac5ad93eSGavin Shan 		/*
787ac5ad93eSGavin Shan 		 * Per spec, I/O windows are 4K-aligned, but some
788ac5ad93eSGavin Shan 		 * bridges have an extension to support 1K alignment.
789ac5ad93eSGavin Shan 		 */
790ac5ad93eSGavin Shan 		if (bus->self->io_window_1k)
791ac5ad93eSGavin Shan 			align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
792ac5ad93eSGavin Shan 		else
793ac5ad93eSGavin Shan 			align = PCI_P2P_DEFAULT_IO_ALIGN;
794ac5ad93eSGavin Shan 	}
795ac5ad93eSGavin Shan 
796ac5ad93eSGavin Shan 	arch_align = pcibios_window_alignment(bus, type);
797ac5ad93eSGavin Shan 	return max(align, arch_align);
798ac5ad93eSGavin Shan }
799ac5ad93eSGavin Shan 
800c8adf9a3SRam Pai /**
801c8adf9a3SRam Pai  * pbus_size_io() - size the io window of a given bus
802c8adf9a3SRam Pai  *
803c8adf9a3SRam Pai  * @bus : the bus
804c8adf9a3SRam Pai  * @min_size : the minimum io window that must to be allocated
805c8adf9a3SRam Pai  * @add_size : additional optional io window
8069e8bf93aSRam Pai  * @realloc_head : track the additional io window on this list
807c8adf9a3SRam Pai  *
808c8adf9a3SRam Pai  * Sizing the IO windows of the PCI-PCI bridge is trivial,
809fd591341SYinghai Lu  * since these windows have 1K or 4K granularity and the IO ranges
810c8adf9a3SRam Pai  * of non-bridge PCI devices are limited to 256 bytes.
811c8adf9a3SRam Pai  * We must be careful with the ISA aliasing though.
812c8adf9a3SRam Pai  */
813c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
814bdc4abecSYinghai Lu 		resource_size_t add_size, struct list_head *realloc_head)
8151da177e4SLinus Torvalds {
8161da177e4SLinus Torvalds 	struct pci_dev *dev;
817*5b285415SYinghai Lu 	struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO,
818*5b285415SYinghai Lu 							IORESOURCE_IO);
81911251a86SWei Yang 	resource_size_t size = 0, size0 = 0, size1 = 0;
820be768912SYinghai Lu 	resource_size_t children_add_size = 0;
8212d1d6678SBjorn Helgaas 	resource_size_t min_align, align;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	if (!b_res)
8241da177e4SLinus Torvalds 		return;
8251da177e4SLinus Torvalds 
8262d1d6678SBjorn Helgaas 	min_align = window_alignment(bus, IORESOURCE_IO);
8271da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
8281da177e4SLinus Torvalds 		int i;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
8311da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
8321da177e4SLinus Torvalds 			unsigned long r_size;
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 			if (r->parent || !(r->flags & IORESOURCE_IO))
8351da177e4SLinus Torvalds 				continue;
836022edd86SZhao, Yu 			r_size = resource_size(r);
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 			if (r_size < 0x400)
8391da177e4SLinus Torvalds 				/* Might be re-aligned for ISA */
8401da177e4SLinus Torvalds 				size += r_size;
8411da177e4SLinus Torvalds 			else
8421da177e4SLinus Torvalds 				size1 += r_size;
843be768912SYinghai Lu 
844fd591341SYinghai Lu 			align = pci_resource_alignment(dev, r);
845fd591341SYinghai Lu 			if (align > min_align)
846fd591341SYinghai Lu 				min_align = align;
847fd591341SYinghai Lu 
8489e8bf93aSRam Pai 			if (realloc_head)
8499e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
8501da177e4SLinus Torvalds 		}
8511da177e4SLinus Torvalds 	}
852fd591341SYinghai Lu 
853c8adf9a3SRam Pai 	size0 = calculate_iosize(size, min_size, size1,
854fd591341SYinghai Lu 			resource_size(b_res), min_align);
855be768912SYinghai Lu 	if (children_add_size > add_size)
856be768912SYinghai Lu 		add_size = children_add_size;
8579e8bf93aSRam Pai 	size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
858a4ac9feaSYinghai Lu 		calculate_iosize(size, min_size, add_size + size1,
859fd591341SYinghai Lu 			resource_size(b_res), min_align);
860c8adf9a3SRam Pai 	if (!size0 && !size1) {
861865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
862865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
863b918c62eSYinghai Lu 				 "%pR to %pR (unused)\n", b_res,
864b918c62eSYinghai Lu 				 &bus->busn_res);
8651da177e4SLinus Torvalds 		b_res->flags = 0;
8661da177e4SLinus Torvalds 		return;
8671da177e4SLinus Torvalds 	}
868fd591341SYinghai Lu 
869fd591341SYinghai Lu 	b_res->start = min_align;
870c8adf9a3SRam Pai 	b_res->end = b_res->start + size0 - 1;
87188452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
872b592443dSYinghai Lu 	if (size1 > size0 && realloc_head) {
873fd591341SYinghai Lu 		add_to_list(realloc_head, bus->self, b_res, size1-size0,
874fd591341SYinghai Lu 			    min_align);
875b592443dSYinghai Lu 		dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
87611251a86SWei Yang 				 "%pR to %pR add_size %llx\n", b_res,
87711251a86SWei Yang 				 &bus->busn_res,
87811251a86SWei Yang 				 (unsigned long long)size1-size0);
879b592443dSYinghai Lu 	}
8801da177e4SLinus Torvalds }
8811da177e4SLinus Torvalds 
882c121504eSGavin Shan static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
883c121504eSGavin Shan 						  int max_order)
884c121504eSGavin Shan {
885c121504eSGavin Shan 	resource_size_t align = 0;
886c121504eSGavin Shan 	resource_size_t min_align = 0;
887c121504eSGavin Shan 	int order;
888c121504eSGavin Shan 
889c121504eSGavin Shan 	for (order = 0; order <= max_order; order++) {
890c121504eSGavin Shan 		resource_size_t align1 = 1;
891c121504eSGavin Shan 
892c121504eSGavin Shan 		align1 <<= (order + 20);
893c121504eSGavin Shan 
894c121504eSGavin Shan 		if (!align)
895c121504eSGavin Shan 			min_align = align1;
896c121504eSGavin Shan 		else if (ALIGN(align + min_align, min_align) < align1)
897c121504eSGavin Shan 			min_align = align1 >> 1;
898c121504eSGavin Shan 		align += aligns[order];
899c121504eSGavin Shan 	}
900c121504eSGavin Shan 
901c121504eSGavin Shan 	return min_align;
902c121504eSGavin Shan }
903c121504eSGavin Shan 
904c8adf9a3SRam Pai /**
905c8adf9a3SRam Pai  * pbus_size_mem() - size the memory window of a given bus
906c8adf9a3SRam Pai  *
907c8adf9a3SRam Pai  * @bus : the bus
908496f70cfSWei Yang  * @mask: mask the resource flag, then compare it with type
909496f70cfSWei Yang  * @type: the type of free resource from bridge
910*5b285415SYinghai Lu  * @type2: second match type
911*5b285415SYinghai Lu  * @type3: third match type
912c8adf9a3SRam Pai  * @min_size : the minimum memory window that must to be allocated
913c8adf9a3SRam Pai  * @add_size : additional optional memory window
9149e8bf93aSRam Pai  * @realloc_head : track the additional memory window on this list
915c8adf9a3SRam Pai  *
916c8adf9a3SRam Pai  * Calculate the size of the bus and minimal alignment which
917c8adf9a3SRam Pai  * guarantees that all child resources fit in this size.
918c8adf9a3SRam Pai  */
91928760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
920*5b285415SYinghai Lu 			 unsigned long type, unsigned long type2,
921*5b285415SYinghai Lu 			 unsigned long type3,
922*5b285415SYinghai Lu 			 resource_size_t min_size, resource_size_t add_size,
923bdc4abecSYinghai Lu 			 struct list_head *realloc_head)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	struct pci_dev *dev;
926c8adf9a3SRam Pai 	resource_size_t min_align, align, size, size0, size1;
92714c8530dSAlan 	resource_size_t aligns[14];	/* Alignments from 1Mb to 8Gb */
9281da177e4SLinus Torvalds 	int order, max_order;
929*5b285415SYinghai Lu 	struct resource *b_res = find_free_bus_resource(bus,
930*5b285415SYinghai Lu 					mask | IORESOURCE_PREFETCH, type);
931be768912SYinghai Lu 	resource_size_t children_add_size = 0;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	if (!b_res)
9341da177e4SLinus Torvalds 		return 0;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
9371da177e4SLinus Torvalds 	max_order = 0;
9381da177e4SLinus Torvalds 	size = 0;
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
9411da177e4SLinus Torvalds 		int i;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
9441da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
945c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
9461da177e4SLinus Torvalds 
947*5b285415SYinghai Lu 			if (r->parent || ((r->flags & mask) != type &&
948*5b285415SYinghai Lu 					  (r->flags & mask) != type2 &&
949*5b285415SYinghai Lu 					  (r->flags & mask) != type3))
9501da177e4SLinus Torvalds 				continue;
951022edd86SZhao, Yu 			r_size = resource_size(r);
9522aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV
9532aceefcbSYinghai Lu 			/* put SRIOV requested res to the optional list */
9549e8bf93aSRam Pai 			if (realloc_head && i >= PCI_IOV_RESOURCES &&
9552aceefcbSYinghai Lu 					i <= PCI_IOV_RESOURCE_END) {
9562aceefcbSYinghai Lu 				r->end = r->start - 1;
957f7625980SBjorn Helgaas 				add_to_list(realloc_head, dev, r, r_size, 0/* don't care */);
9582aceefcbSYinghai Lu 				children_add_size += r_size;
9592aceefcbSYinghai Lu 				continue;
9602aceefcbSYinghai Lu 			}
9612aceefcbSYinghai Lu #endif
96214c8530dSAlan 			/*
96314c8530dSAlan 			 * aligns[0] is for 1MB (since bridge memory
96414c8530dSAlan 			 * windows are always at least 1MB aligned), so
96514c8530dSAlan 			 * keep "order" from being negative for smaller
96614c8530dSAlan 			 * resources.
96714c8530dSAlan 			 */
9686faf17f6SChris Wright 			align = pci_resource_alignment(dev, r);
9691da177e4SLinus Torvalds 			order = __ffs(align) - 20;
97014c8530dSAlan 			if (order < 0)
97114c8530dSAlan 				order = 0;
97214c8530dSAlan 			if (order >= ARRAY_SIZE(aligns)) {
973865df576SBjorn Helgaas 				dev_warn(&dev->dev, "disabling BAR %d: %pR "
974865df576SBjorn Helgaas 					 "(bad alignment %#llx)\n", i, r,
975865df576SBjorn Helgaas 					 (unsigned long long) align);
9761da177e4SLinus Torvalds 				r->flags = 0;
9771da177e4SLinus Torvalds 				continue;
9781da177e4SLinus Torvalds 			}
9791da177e4SLinus Torvalds 			size += r_size;
9801da177e4SLinus Torvalds 			/* Exclude ranges with size > align from
9811da177e4SLinus Torvalds 			   calculation of the alignment. */
9821da177e4SLinus Torvalds 			if (r_size == align)
9831da177e4SLinus Torvalds 				aligns[order] += align;
9841da177e4SLinus Torvalds 			if (order > max_order)
9851da177e4SLinus Torvalds 				max_order = order;
986be768912SYinghai Lu 
9879e8bf93aSRam Pai 			if (realloc_head)
9889e8bf93aSRam Pai 				children_add_size += get_res_add_size(realloc_head, r);
9891da177e4SLinus Torvalds 		}
9901da177e4SLinus Torvalds 	}
9918308c54dSJeremy Fitzhardinge 
992c121504eSGavin Shan 	min_align = calculate_mem_align(aligns, max_order);
9933ad94b0dSWei Yang 	min_align = max(min_align, window_alignment(bus, b_res->flags));
994b42282e5SLinus Torvalds 	size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
995be768912SYinghai Lu 	if (children_add_size > add_size)
996be768912SYinghai Lu 		add_size = children_add_size;
9979e8bf93aSRam Pai 	size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
998a4ac9feaSYinghai Lu 		calculate_memsize(size, min_size, add_size,
999b42282e5SLinus Torvalds 				resource_size(b_res), min_align);
1000c8adf9a3SRam Pai 	if (!size0 && !size1) {
1001865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
1002865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
1003b918c62eSYinghai Lu 				 "%pR to %pR (unused)\n", b_res,
1004b918c62eSYinghai Lu 				 &bus->busn_res);
10051da177e4SLinus Torvalds 		b_res->flags = 0;
10061da177e4SLinus Torvalds 		return 1;
10071da177e4SLinus Torvalds 	}
10081da177e4SLinus Torvalds 	b_res->start = min_align;
1009c8adf9a3SRam Pai 	b_res->end = size0 + min_align - 1;
1010*5b285415SYinghai Lu 	b_res->flags |= IORESOURCE_STARTALIGN;
1011b592443dSYinghai Lu 	if (size1 > size0 && realloc_head) {
10129e8bf93aSRam Pai 		add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
1013b592443dSYinghai Lu 		dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
1014b918c62eSYinghai Lu 				 "%pR to %pR add_size %llx\n", b_res,
1015b918c62eSYinghai Lu 				 &bus->busn_res, (unsigned long long)size1-size0);
1016b592443dSYinghai Lu 	}
10171da177e4SLinus Torvalds 	return 1;
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
10200a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res)
10210a2daa1cSRam Pai {
10220a2daa1cSRam Pai 	if (res->flags & IORESOURCE_IO)
10230a2daa1cSRam Pai 		return pci_cardbus_io_size;
10240a2daa1cSRam Pai 	if (res->flags & IORESOURCE_MEM)
10250a2daa1cSRam Pai 		return pci_cardbus_mem_size;
10260a2daa1cSRam Pai 	return 0;
10270a2daa1cSRam Pai }
10280a2daa1cSRam Pai 
10290a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus,
1030bdc4abecSYinghai Lu 			struct list_head *realloc_head)
10311da177e4SLinus Torvalds {
10321da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
10331da177e4SLinus Torvalds 	struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
103411848934SYinghai Lu 	resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
10351da177e4SLinus Torvalds 	u16 ctrl;
10361da177e4SLinus Torvalds 
10373796f1e2SYinghai Lu 	if (b_res[0].parent)
10383796f1e2SYinghai Lu 		goto handle_b_res_1;
10391da177e4SLinus Torvalds 	/*
10401da177e4SLinus Torvalds 	 * Reserve some resources for CardBus.  We reserve
10411da177e4SLinus Torvalds 	 * a fixed amount of bus space for CardBus bridges.
10421da177e4SLinus Torvalds 	 */
104311848934SYinghai Lu 	b_res[0].start = pci_cardbus_io_size;
104411848934SYinghai Lu 	b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
104511848934SYinghai Lu 	b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
104611848934SYinghai Lu 	if (realloc_head) {
104711848934SYinghai Lu 		b_res[0].end -= pci_cardbus_io_size;
104811848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
104911848934SYinghai Lu 				pci_cardbus_io_size);
105011848934SYinghai Lu 	}
10511da177e4SLinus Torvalds 
10523796f1e2SYinghai Lu handle_b_res_1:
10533796f1e2SYinghai Lu 	if (b_res[1].parent)
10543796f1e2SYinghai Lu 		goto handle_b_res_2;
105511848934SYinghai Lu 	b_res[1].start = pci_cardbus_io_size;
105611848934SYinghai Lu 	b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
105711848934SYinghai Lu 	b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
105811848934SYinghai Lu 	if (realloc_head) {
105911848934SYinghai Lu 		b_res[1].end -= pci_cardbus_io_size;
106011848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
106111848934SYinghai Lu 				 pci_cardbus_io_size);
106211848934SYinghai Lu 	}
10631da177e4SLinus Torvalds 
10643796f1e2SYinghai Lu handle_b_res_2:
1065dcef0d06SYinghai Lu 	/* MEM1 must not be pref mmio */
1066dcef0d06SYinghai Lu 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1067dcef0d06SYinghai Lu 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
1068dcef0d06SYinghai Lu 		ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
1069dcef0d06SYinghai Lu 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1070dcef0d06SYinghai Lu 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1071dcef0d06SYinghai Lu 	}
1072dcef0d06SYinghai Lu 
10731da177e4SLinus Torvalds 	/*
10741da177e4SLinus Torvalds 	 * Check whether prefetchable memory is supported
10751da177e4SLinus Torvalds 	 * by this bridge.
10761da177e4SLinus Torvalds 	 */
10771da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
10781da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
10791da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
10801da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
10811da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
10821da177e4SLinus Torvalds 	}
10831da177e4SLinus Torvalds 
10843796f1e2SYinghai Lu 	if (b_res[2].parent)
10853796f1e2SYinghai Lu 		goto handle_b_res_3;
10861da177e4SLinus Torvalds 	/*
10871da177e4SLinus Torvalds 	 * If we have prefetchable memory support, allocate
10881da177e4SLinus Torvalds 	 * two regions.  Otherwise, allocate one region of
10891da177e4SLinus Torvalds 	 * twice the size.
10901da177e4SLinus Torvalds 	 */
10911da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
109211848934SYinghai Lu 		b_res[2].start = pci_cardbus_mem_size;
109311848934SYinghai Lu 		b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
109411848934SYinghai Lu 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
109511848934SYinghai Lu 				  IORESOURCE_STARTALIGN;
109611848934SYinghai Lu 		if (realloc_head) {
109711848934SYinghai Lu 			b_res[2].end -= pci_cardbus_mem_size;
109811848934SYinghai Lu 			add_to_list(realloc_head, bridge, b_res+2,
109911848934SYinghai Lu 				 pci_cardbus_mem_size, pci_cardbus_mem_size);
11001da177e4SLinus Torvalds 		}
11010a2daa1cSRam Pai 
110211848934SYinghai Lu 		/* reduce that to half */
110311848934SYinghai Lu 		b_res_3_size = pci_cardbus_mem_size;
110411848934SYinghai Lu 	}
110511848934SYinghai Lu 
11063796f1e2SYinghai Lu handle_b_res_3:
11073796f1e2SYinghai Lu 	if (b_res[3].parent)
11083796f1e2SYinghai Lu 		goto handle_done;
110911848934SYinghai Lu 	b_res[3].start = pci_cardbus_mem_size;
111011848934SYinghai Lu 	b_res[3].end = b_res[3].start + b_res_3_size - 1;
111111848934SYinghai Lu 	b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
111211848934SYinghai Lu 	if (realloc_head) {
111311848934SYinghai Lu 		b_res[3].end -= b_res_3_size;
111411848934SYinghai Lu 		add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
111511848934SYinghai Lu 				 pci_cardbus_mem_size);
111611848934SYinghai Lu 	}
11173796f1e2SYinghai Lu 
11183796f1e2SYinghai Lu handle_done:
11193796f1e2SYinghai Lu 	;
11201da177e4SLinus Torvalds }
11211da177e4SLinus Torvalds 
1122d66ecb72SJiang Liu void __ref __pci_bus_size_bridges(struct pci_bus *bus,
1123bdc4abecSYinghai Lu 			struct list_head *realloc_head)
11241da177e4SLinus Torvalds {
11251da177e4SLinus Torvalds 	struct pci_dev *dev;
1126*5b285415SYinghai Lu 	unsigned long mask, prefmask, type2 = 0, type3 = 0;
1127c8adf9a3SRam Pai 	resource_size_t additional_mem_size = 0, additional_io_size = 0;
1128*5b285415SYinghai Lu 	struct resource *b_res;
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
11311da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
11321da177e4SLinus Torvalds 		if (!b)
11331da177e4SLinus Torvalds 			continue;
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 		switch (dev->class >> 8) {
11361da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
11379e8bf93aSRam Pai 			pci_bus_size_cardbus(b, realloc_head);
11381da177e4SLinus Torvalds 			break;
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
11411da177e4SLinus Torvalds 		default:
11429e8bf93aSRam Pai 			__pci_bus_size_bridges(b, realloc_head);
11431da177e4SLinus Torvalds 			break;
11441da177e4SLinus Torvalds 		}
11451da177e4SLinus Torvalds 	}
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	/* The root bus? */
11482ba29e27SWei Yang 	if (pci_is_root_bus(bus))
11491da177e4SLinus Torvalds 		return;
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	switch (bus->self->class >> 8) {
11521da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_CARDBUS:
11531da177e4SLinus Torvalds 		/* don't size cardbuses yet. */
11541da177e4SLinus Torvalds 		break;
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_PCI:
11571da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
115828760489SEric W. Biederman 		if (bus->self->is_hotplug_bridge) {
1159c8adf9a3SRam Pai 			additional_io_size  = pci_hotplug_io_size;
1160c8adf9a3SRam Pai 			additional_mem_size = pci_hotplug_mem_size;
116128760489SEric W. Biederman 		}
1162c8adf9a3SRam Pai 		/*
1163c8adf9a3SRam Pai 		 * Follow thru
1164c8adf9a3SRam Pai 		 */
11651da177e4SLinus Torvalds 	default:
116619aa7ee4SYinghai Lu 		pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
116719aa7ee4SYinghai Lu 			     additional_io_size, realloc_head);
11681da177e4SLinus Torvalds 		/* If the bridge supports prefetchable range, size it
11691da177e4SLinus Torvalds 		   separately. If it doesn't, or its prefetchable window
11701da177e4SLinus Torvalds 		   has already been allocated by arch code, try
11711da177e4SLinus Torvalds 		   non-prefetchable range for both types of PCI memory
11721da177e4SLinus Torvalds 		   resources. */
1173*5b285415SYinghai Lu 		b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES];
11741da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
11751da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
1176*5b285415SYinghai Lu 		if (b_res[2].flags & IORESOURCE_MEM_64) {
1177*5b285415SYinghai Lu 			prefmask |= IORESOURCE_MEM_64;
117819aa7ee4SYinghai Lu 			if (pbus_size_mem(bus, prefmask, prefmask,
1179*5b285415SYinghai Lu 				  prefmask, prefmask,
118019aa7ee4SYinghai Lu 				  realloc_head ? 0 : additional_mem_size,
1181*5b285415SYinghai Lu 				  additional_mem_size, realloc_head)) {
1182*5b285415SYinghai Lu 					/*
1183*5b285415SYinghai Lu 					 * Success, with pref mmio64,
1184*5b285415SYinghai Lu 					 * next will size non-pref or
1185*5b285415SYinghai Lu 					 * non-mmio64 */
1186*5b285415SYinghai Lu 					mask = prefmask;
1187*5b285415SYinghai Lu 					type2 = prefmask & ~IORESOURCE_MEM_64;
1188*5b285415SYinghai Lu 					type3 = prefmask & ~IORESOURCE_PREFETCH;
1189*5b285415SYinghai Lu 			}
1190*5b285415SYinghai Lu 		}
1191*5b285415SYinghai Lu 		if (!type2) {
1192*5b285415SYinghai Lu 			prefmask &= ~IORESOURCE_MEM_64;
1193*5b285415SYinghai Lu 			if (pbus_size_mem(bus, prefmask, prefmask,
1194*5b285415SYinghai Lu 					 prefmask, prefmask,
1195*5b285415SYinghai Lu 					 realloc_head ? 0 : additional_mem_size,
1196*5b285415SYinghai Lu 					 additional_mem_size, realloc_head)) {
1197*5b285415SYinghai Lu 				/* Success, next will size non-prefetch. */
1198*5b285415SYinghai Lu 				mask = prefmask;
1199*5b285415SYinghai Lu 			} else
1200c8adf9a3SRam Pai 				additional_mem_size += additional_mem_size;
1201*5b285415SYinghai Lu 			type2 = type3 = IORESOURCE_MEM;
1202*5b285415SYinghai Lu 		}
1203*5b285415SYinghai Lu 		pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
120419aa7ee4SYinghai Lu 				realloc_head ? 0 : additional_mem_size,
120519aa7ee4SYinghai Lu 				additional_mem_size, realloc_head);
12061da177e4SLinus Torvalds 		break;
12071da177e4SLinus Torvalds 	}
12081da177e4SLinus Torvalds }
1209c8adf9a3SRam Pai 
1210c8adf9a3SRam Pai void __ref pci_bus_size_bridges(struct pci_bus *bus)
1211c8adf9a3SRam Pai {
1212c8adf9a3SRam Pai 	__pci_bus_size_bridges(bus, NULL);
1213c8adf9a3SRam Pai }
12141da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
12151da177e4SLinus Torvalds 
1216d66ecb72SJiang Liu void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
1217bdc4abecSYinghai Lu 				      struct list_head *realloc_head,
1218bdc4abecSYinghai Lu 				      struct list_head *fail_head)
12191da177e4SLinus Torvalds {
12201da177e4SLinus Torvalds 	struct pci_bus *b;
12211da177e4SLinus Torvalds 	struct pci_dev *dev;
12221da177e4SLinus Torvalds 
12239e8bf93aSRam Pai 	pbus_assign_resources_sorted(bus, realloc_head, fail_head);
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
12261da177e4SLinus Torvalds 		b = dev->subordinate;
12271da177e4SLinus Torvalds 		if (!b)
12281da177e4SLinus Torvalds 			continue;
12291da177e4SLinus Torvalds 
12309e8bf93aSRam Pai 		__pci_bus_assign_resources(b, realloc_head, fail_head);
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 		switch (dev->class >> 8) {
12331da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
12346841ec68SYinghai Lu 			if (!pci_is_enabled(dev))
12351da177e4SLinus Torvalds 				pci_setup_bridge(b);
12361da177e4SLinus Torvalds 			break;
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
12391da177e4SLinus Torvalds 			pci_setup_cardbus(b);
12401da177e4SLinus Torvalds 			break;
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds 		default:
124380ccba11SBjorn Helgaas 			dev_info(&dev->dev, "not setting up bridge for bus "
124480ccba11SBjorn Helgaas 				 "%04x:%02x\n", pci_domain_nr(b), b->number);
12451da177e4SLinus Torvalds 			break;
12461da177e4SLinus Torvalds 		}
12471da177e4SLinus Torvalds 	}
12481da177e4SLinus Torvalds }
1249568ddef8SYinghai Lu 
1250568ddef8SYinghai Lu void __ref pci_bus_assign_resources(const struct pci_bus *bus)
1251568ddef8SYinghai Lu {
1252c8adf9a3SRam Pai 	__pci_bus_assign_resources(bus, NULL, NULL);
1253568ddef8SYinghai Lu }
12541da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
12551da177e4SLinus Torvalds 
12566841ec68SYinghai Lu static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
1257bdc4abecSYinghai Lu 					 struct list_head *add_head,
1258bdc4abecSYinghai Lu 					 struct list_head *fail_head)
12596841ec68SYinghai Lu {
12606841ec68SYinghai Lu 	struct pci_bus *b;
12616841ec68SYinghai Lu 
12628424d759SYinghai Lu 	pdev_assign_resources_sorted((struct pci_dev *)bridge,
12638424d759SYinghai Lu 					 add_head, fail_head);
12646841ec68SYinghai Lu 
12656841ec68SYinghai Lu 	b = bridge->subordinate;
12666841ec68SYinghai Lu 	if (!b)
12676841ec68SYinghai Lu 		return;
12686841ec68SYinghai Lu 
12698424d759SYinghai Lu 	__pci_bus_assign_resources(b, add_head, fail_head);
12706841ec68SYinghai Lu 
12716841ec68SYinghai Lu 	switch (bridge->class >> 8) {
12726841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_PCI:
12736841ec68SYinghai Lu 		pci_setup_bridge(b);
12746841ec68SYinghai Lu 		break;
12756841ec68SYinghai Lu 
12766841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_CARDBUS:
12776841ec68SYinghai Lu 		pci_setup_cardbus(b);
12786841ec68SYinghai Lu 		break;
12796841ec68SYinghai Lu 
12806841ec68SYinghai Lu 	default:
12816841ec68SYinghai Lu 		dev_info(&bridge->dev, "not setting up bridge for bus "
12826841ec68SYinghai Lu 			 "%04x:%02x\n", pci_domain_nr(b), b->number);
12836841ec68SYinghai Lu 		break;
12846841ec68SYinghai Lu 	}
12856841ec68SYinghai Lu }
12865009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus,
12875009b460SYinghai Lu 					  unsigned long type)
12885009b460SYinghai Lu {
1289*5b285415SYinghai Lu 	struct pci_dev *dev = bus->self;
12905009b460SYinghai Lu 	struct resource *r;
12915009b460SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1292*5b285415SYinghai Lu 				  IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
1293*5b285415SYinghai Lu 	unsigned old_flags = 0;
1294*5b285415SYinghai Lu 	struct resource *b_res;
1295*5b285415SYinghai Lu 	int idx = 1;
12965009b460SYinghai Lu 
1297*5b285415SYinghai Lu 	b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
1298*5b285415SYinghai Lu 
1299*5b285415SYinghai Lu 	/*
1300*5b285415SYinghai Lu 	 *     1. if there is io port assign fail, will release bridge
1301*5b285415SYinghai Lu 	 *	  io port.
1302*5b285415SYinghai Lu 	 *     2. if there is non pref mmio assign fail, release bridge
1303*5b285415SYinghai Lu 	 *	  nonpref mmio.
1304*5b285415SYinghai Lu 	 *     3. if there is 64bit pref mmio assign fail, and bridge pref
1305*5b285415SYinghai Lu 	 *	  is 64bit, release bridge pref mmio.
1306*5b285415SYinghai Lu 	 *     4. if there is pref mmio assign fail, and bridge pref is
1307*5b285415SYinghai Lu 	 *	  32bit mmio, release bridge pref mmio
1308*5b285415SYinghai Lu 	 *     5. if there is pref mmio assign fail, and bridge pref is not
1309*5b285415SYinghai Lu 	 *	  assigned, release bridge nonpref mmio.
1310*5b285415SYinghai Lu 	 */
1311*5b285415SYinghai Lu 	if (type & IORESOURCE_IO)
1312*5b285415SYinghai Lu 		idx = 0;
1313*5b285415SYinghai Lu 	else if (!(type & IORESOURCE_PREFETCH))
1314*5b285415SYinghai Lu 		idx = 1;
1315*5b285415SYinghai Lu 	else if ((type & IORESOURCE_MEM_64) &&
1316*5b285415SYinghai Lu 		 (b_res[2].flags & IORESOURCE_MEM_64))
1317*5b285415SYinghai Lu 		idx = 2;
1318*5b285415SYinghai Lu 	else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
1319*5b285415SYinghai Lu 		 (b_res[2].flags & IORESOURCE_PREFETCH))
1320*5b285415SYinghai Lu 		idx = 2;
1321*5b285415SYinghai Lu 	else
1322*5b285415SYinghai Lu 		idx = 1;
1323*5b285415SYinghai Lu 
1324*5b285415SYinghai Lu 	r = &b_res[idx];
1325*5b285415SYinghai Lu 
13265009b460SYinghai Lu 	if (!r->parent)
1327*5b285415SYinghai Lu 		return;
1328*5b285415SYinghai Lu 
13295009b460SYinghai Lu 	/*
13305009b460SYinghai Lu 	 * if there are children under that, we should release them
13315009b460SYinghai Lu 	 *  all
13325009b460SYinghai Lu 	 */
13335009b460SYinghai Lu 	release_child_resources(r);
13345009b460SYinghai Lu 	if (!release_resource(r)) {
1335*5b285415SYinghai Lu 		type = old_flags = r->flags & type_mask;
1336*5b285415SYinghai Lu 		dev_printk(KERN_DEBUG, &dev->dev, "resource %d %pR released\n",
1337*5b285415SYinghai Lu 					PCI_BRIDGE_RESOURCES + idx, r);
13385009b460SYinghai Lu 		/* keep the old size */
13395009b460SYinghai Lu 		r->end = resource_size(r) - 1;
13405009b460SYinghai Lu 		r->start = 0;
13415009b460SYinghai Lu 		r->flags = 0;
13425009b460SYinghai Lu 
13435009b460SYinghai Lu 		/* avoiding touch the one without PREF */
13445009b460SYinghai Lu 		if (type & IORESOURCE_PREFETCH)
13455009b460SYinghai Lu 			type = IORESOURCE_PREFETCH;
13465009b460SYinghai Lu 		__pci_setup_bridge(bus, type);
1347*5b285415SYinghai Lu 		/* for next child res under same bridge */
1348*5b285415SYinghai Lu 		r->flags = old_flags;
13495009b460SYinghai Lu 	}
13505009b460SYinghai Lu }
13515009b460SYinghai Lu 
13525009b460SYinghai Lu enum release_type {
13535009b460SYinghai Lu 	leaf_only,
13545009b460SYinghai Lu 	whole_subtree,
13555009b460SYinghai Lu };
13565009b460SYinghai Lu /*
13575009b460SYinghai Lu  * try to release pci bridge resources that is from leaf bridge,
13585009b460SYinghai Lu  * so we can allocate big new one later
13595009b460SYinghai Lu  */
13605009b460SYinghai Lu static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
13615009b460SYinghai Lu 						   unsigned long type,
13625009b460SYinghai Lu 						   enum release_type rel_type)
13635009b460SYinghai Lu {
13645009b460SYinghai Lu 	struct pci_dev *dev;
13655009b460SYinghai Lu 	bool is_leaf_bridge = true;
13665009b460SYinghai Lu 
13675009b460SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
13685009b460SYinghai Lu 		struct pci_bus *b = dev->subordinate;
13695009b460SYinghai Lu 		if (!b)
13705009b460SYinghai Lu 			continue;
13715009b460SYinghai Lu 
13725009b460SYinghai Lu 		is_leaf_bridge = false;
13735009b460SYinghai Lu 
13745009b460SYinghai Lu 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
13755009b460SYinghai Lu 			continue;
13765009b460SYinghai Lu 
13775009b460SYinghai Lu 		if (rel_type == whole_subtree)
13785009b460SYinghai Lu 			pci_bus_release_bridge_resources(b, type,
13795009b460SYinghai Lu 						 whole_subtree);
13805009b460SYinghai Lu 	}
13815009b460SYinghai Lu 
13825009b460SYinghai Lu 	if (pci_is_root_bus(bus))
13835009b460SYinghai Lu 		return;
13845009b460SYinghai Lu 
13855009b460SYinghai Lu 	if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
13865009b460SYinghai Lu 		return;
13875009b460SYinghai Lu 
13885009b460SYinghai Lu 	if ((rel_type == whole_subtree) || is_leaf_bridge)
13895009b460SYinghai Lu 		pci_bridge_release_resources(bus, type);
13905009b460SYinghai Lu }
13915009b460SYinghai Lu 
139276fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
139376fbc263SYinghai Lu {
139489a74eccSBjorn Helgaas 	struct resource *res;
139576fbc263SYinghai Lu 	int i;
139676fbc263SYinghai Lu 
139789a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
13987c9342b8SYinghai Lu 		if (!res || !res->end || !res->flags)
139976fbc263SYinghai Lu                         continue;
140076fbc263SYinghai Lu 
1401c7dabef8SBjorn Helgaas 		dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
140276fbc263SYinghai Lu         }
140376fbc263SYinghai Lu }
140476fbc263SYinghai Lu 
140576fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
140676fbc263SYinghai Lu {
140776fbc263SYinghai Lu 	struct pci_bus *b;
140876fbc263SYinghai Lu 	struct pci_dev *dev;
140976fbc263SYinghai Lu 
141076fbc263SYinghai Lu 
141176fbc263SYinghai Lu 	pci_bus_dump_res(bus);
141276fbc263SYinghai Lu 
141376fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
141476fbc263SYinghai Lu 		b = dev->subordinate;
141576fbc263SYinghai Lu 		if (!b)
141676fbc263SYinghai Lu 			continue;
141776fbc263SYinghai Lu 
141876fbc263SYinghai Lu 		pci_bus_dump_resources(b);
141976fbc263SYinghai Lu 	}
142076fbc263SYinghai Lu }
142176fbc263SYinghai Lu 
1422ff35147cSYinghai Lu static int pci_bus_get_depth(struct pci_bus *bus)
1423da7822e5SYinghai Lu {
1424da7822e5SYinghai Lu 	int depth = 0;
1425f2a230bdSWei Yang 	struct pci_bus *child_bus;
1426da7822e5SYinghai Lu 
1427f2a230bdSWei Yang 	list_for_each_entry(child_bus, &bus->children, node){
1428da7822e5SYinghai Lu 		int ret;
1429da7822e5SYinghai Lu 
1430f2a230bdSWei Yang 		ret = pci_bus_get_depth(child_bus);
1431da7822e5SYinghai Lu 		if (ret + 1 > depth)
1432da7822e5SYinghai Lu 			depth = ret + 1;
1433da7822e5SYinghai Lu 	}
1434da7822e5SYinghai Lu 
1435da7822e5SYinghai Lu 	return depth;
1436da7822e5SYinghai Lu }
1437da7822e5SYinghai Lu 
1438b55438fdSYinghai Lu /*
1439b55438fdSYinghai Lu  * -1: undefined, will auto detect later
1440b55438fdSYinghai Lu  *  0: disabled by user
1441b55438fdSYinghai Lu  *  1: disabled by auto detect
1442b55438fdSYinghai Lu  *  2: enabled by user
1443b55438fdSYinghai Lu  *  3: enabled by auto detect
1444b55438fdSYinghai Lu  */
1445b55438fdSYinghai Lu enum enable_type {
1446b55438fdSYinghai Lu 	undefined = -1,
1447b55438fdSYinghai Lu 	user_disabled,
1448b55438fdSYinghai Lu 	auto_disabled,
1449b55438fdSYinghai Lu 	user_enabled,
1450b55438fdSYinghai Lu 	auto_enabled,
1451b55438fdSYinghai Lu };
1452b55438fdSYinghai Lu 
1453ff35147cSYinghai Lu static enum enable_type pci_realloc_enable = undefined;
1454b55438fdSYinghai Lu void __init pci_realloc_get_opt(char *str)
1455b55438fdSYinghai Lu {
1456b55438fdSYinghai Lu 	if (!strncmp(str, "off", 3))
1457b55438fdSYinghai Lu 		pci_realloc_enable = user_disabled;
1458b55438fdSYinghai Lu 	else if (!strncmp(str, "on", 2))
1459b55438fdSYinghai Lu 		pci_realloc_enable = user_enabled;
1460b55438fdSYinghai Lu }
1461ff35147cSYinghai Lu static bool pci_realloc_enabled(enum enable_type enable)
1462b55438fdSYinghai Lu {
1463967260cdSYinghai Lu 	return enable >= user_enabled;
1464b55438fdSYinghai Lu }
1465f483d392SRam Pai 
1466b07f2ebcSYinghai Lu #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1467ff35147cSYinghai Lu static int iov_resources_unassigned(struct pci_dev *dev, void *data)
1468223d96fcSYinghai Lu {
1469b07f2ebcSYinghai Lu 	int i;
1470223d96fcSYinghai Lu 	bool *unassigned = data;
1471b07f2ebcSYinghai Lu 
1472b07f2ebcSYinghai Lu 	for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) {
1473b07f2ebcSYinghai Lu 		struct resource *r = &dev->resource[i];
1474fa216bf4SYinghai Lu 		struct pci_bus_region region;
1475b07f2ebcSYinghai Lu 
1476223d96fcSYinghai Lu 		/* Not assigned or rejected by kernel? */
1477fa216bf4SYinghai Lu 		if (!r->flags)
1478fa216bf4SYinghai Lu 			continue;
1479b07f2ebcSYinghai Lu 
1480fc279850SYinghai Lu 		pcibios_resource_to_bus(dev->bus, &region, r);
1481fa216bf4SYinghai Lu 		if (!region.start) {
1482223d96fcSYinghai Lu 			*unassigned = true;
1483223d96fcSYinghai Lu 			return 1; /* return early from pci_walk_bus() */
1484b07f2ebcSYinghai Lu 		}
1485b07f2ebcSYinghai Lu 	}
1486b07f2ebcSYinghai Lu 
1487223d96fcSYinghai Lu 	return 0;
1488223d96fcSYinghai Lu }
1489223d96fcSYinghai Lu 
1490ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1491967260cdSYinghai Lu 			 enum enable_type enable_local)
1492223d96fcSYinghai Lu {
1493223d96fcSYinghai Lu 	bool unassigned = false;
1494223d96fcSYinghai Lu 
1495967260cdSYinghai Lu 	if (enable_local != undefined)
1496967260cdSYinghai Lu 		return enable_local;
1497223d96fcSYinghai Lu 
1498223d96fcSYinghai Lu 	pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
1499967260cdSYinghai Lu 	if (unassigned)
1500967260cdSYinghai Lu 		return auto_enabled;
1501967260cdSYinghai Lu 
1502967260cdSYinghai Lu 	return enable_local;
1503b07f2ebcSYinghai Lu }
1504223d96fcSYinghai Lu #else
1505ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus,
1506967260cdSYinghai Lu 			 enum enable_type enable_local)
1507967260cdSYinghai Lu {
1508967260cdSYinghai Lu 	return enable_local;
1509b07f2ebcSYinghai Lu }
1510b07f2ebcSYinghai Lu #endif
1511b07f2ebcSYinghai Lu 
1512da7822e5SYinghai Lu /*
1513da7822e5SYinghai Lu  * first try will not touch pci bridge res
1514da7822e5SYinghai Lu  * second and later try will clear small leaf bridge res
1515f7625980SBjorn Helgaas  * will stop till to the max depth if can not find good one
1516da7822e5SYinghai Lu  */
151739772038SYinghai Lu void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
15181da177e4SLinus Torvalds {
1519bdc4abecSYinghai Lu 	LIST_HEAD(realloc_head); /* list of resources that
1520c8adf9a3SRam Pai 					want additional resources */
1521bdc4abecSYinghai Lu 	struct list_head *add_list = NULL;
1522da7822e5SYinghai Lu 	int tried_times = 0;
1523da7822e5SYinghai Lu 	enum release_type rel_type = leaf_only;
1524bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
1525b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
1526da7822e5SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1527*5b285415SYinghai Lu 				  IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
152819aa7ee4SYinghai Lu 	int pci_try_num = 1;
152955ed83a6SYinghai Lu 	enum enable_type enable_local;
1530da7822e5SYinghai Lu 
153119aa7ee4SYinghai Lu 	/* don't realloc if asked to do so */
153255ed83a6SYinghai Lu 	enable_local = pci_realloc_detect(bus, pci_realloc_enable);
1533967260cdSYinghai Lu 	if (pci_realloc_enabled(enable_local)) {
153455ed83a6SYinghai Lu 		int max_depth = pci_bus_get_depth(bus);
153519aa7ee4SYinghai Lu 
1536da7822e5SYinghai Lu 		pci_try_num = max_depth + 1;
153755ed83a6SYinghai Lu 		dev_printk(KERN_DEBUG, &bus->dev,
153855ed83a6SYinghai Lu 			   "max bus depth: %d pci_try_num: %d\n",
1539da7822e5SYinghai Lu 			   max_depth, pci_try_num);
154019aa7ee4SYinghai Lu 	}
1541da7822e5SYinghai Lu 
1542da7822e5SYinghai Lu again:
154319aa7ee4SYinghai Lu 	/*
154419aa7ee4SYinghai Lu 	 * last try will use add_list, otherwise will try good to have as
154519aa7ee4SYinghai Lu 	 * must have, so can realloc parent bridge resource
154619aa7ee4SYinghai Lu 	 */
154719aa7ee4SYinghai Lu 	if (tried_times + 1 == pci_try_num)
1548bdc4abecSYinghai Lu 		add_list = &realloc_head;
15491da177e4SLinus Torvalds 	/* Depth first, calculate sizes and alignments of all
15501da177e4SLinus Torvalds 	   subordinate buses. */
155119aa7ee4SYinghai Lu 	__pci_bus_size_bridges(bus, add_list);
1552c8adf9a3SRam Pai 
15531da177e4SLinus Torvalds 	/* Depth last, allocate resources and update the hardware. */
1554bdc4abecSYinghai Lu 	__pci_bus_assign_resources(bus, add_list, &fail_head);
155519aa7ee4SYinghai Lu 	if (add_list)
1556bdc4abecSYinghai Lu 		BUG_ON(!list_empty(add_list));
1557da7822e5SYinghai Lu 	tried_times++;
1558da7822e5SYinghai Lu 
1559da7822e5SYinghai Lu 	/* any device complain? */
1560bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
1561928bea96SYinghai Lu 		goto dump;
1562f483d392SRam Pai 
15630c5be0cbSYinghai Lu 	if (tried_times >= pci_try_num) {
1564967260cdSYinghai Lu 		if (enable_local == undefined)
156555ed83a6SYinghai Lu 			dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
1566967260cdSYinghai Lu 		else if (enable_local == auto_enabled)
156755ed83a6SYinghai Lu 			dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
1568eb572e7cSYinghai Lu 
1569bffc56d4SYinghai Lu 		free_list(&fail_head);
1570928bea96SYinghai Lu 		goto dump;
1571da7822e5SYinghai Lu 	}
1572da7822e5SYinghai Lu 
157355ed83a6SYinghai Lu 	dev_printk(KERN_DEBUG, &bus->dev,
157455ed83a6SYinghai Lu 		   "No. %d try to assign unassigned res\n", tried_times + 1);
1575da7822e5SYinghai Lu 
1576da7822e5SYinghai Lu 	/* third times and later will not check if it is leaf */
1577da7822e5SYinghai Lu 	if ((tried_times + 1) > 2)
1578da7822e5SYinghai Lu 		rel_type = whole_subtree;
1579da7822e5SYinghai Lu 
1580da7822e5SYinghai Lu 	/*
1581da7822e5SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
1582da7822e5SYinghai Lu 	 * child device under that bridge
1583da7822e5SYinghai Lu 	 */
158461e83cddSYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list)
158561e83cddSYinghai Lu 		pci_bus_release_bridge_resources(fail_res->dev->bus,
1586b9b0bba9SYinghai Lu 						 fail_res->flags & type_mask,
1587da7822e5SYinghai Lu 						 rel_type);
158861e83cddSYinghai Lu 
1589da7822e5SYinghai Lu 	/* restore size and flags */
1590b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1591b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
1592da7822e5SYinghai Lu 
1593b9b0bba9SYinghai Lu 		res->start = fail_res->start;
1594b9b0bba9SYinghai Lu 		res->end = fail_res->end;
1595b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
1596b9b0bba9SYinghai Lu 		if (fail_res->dev->subordinate)
1597da7822e5SYinghai Lu 			res->flags = 0;
1598da7822e5SYinghai Lu 	}
1599bffc56d4SYinghai Lu 	free_list(&fail_head);
1600da7822e5SYinghai Lu 
1601da7822e5SYinghai Lu 	goto again;
1602da7822e5SYinghai Lu 
1603928bea96SYinghai Lu dump:
160476fbc263SYinghai Lu 	/* dump the resource on buses */
160576fbc263SYinghai Lu 	pci_bus_dump_resources(bus);
160676fbc263SYinghai Lu }
16076841ec68SYinghai Lu 
160855ed83a6SYinghai Lu void __init pci_assign_unassigned_resources(void)
160955ed83a6SYinghai Lu {
161055ed83a6SYinghai Lu 	struct pci_bus *root_bus;
161155ed83a6SYinghai Lu 
161255ed83a6SYinghai Lu 	list_for_each_entry(root_bus, &pci_root_buses, node)
161355ed83a6SYinghai Lu 		pci_assign_unassigned_root_bus_resources(root_bus);
161455ed83a6SYinghai Lu }
161555ed83a6SYinghai Lu 
16166841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
16176841ec68SYinghai Lu {
16186841ec68SYinghai Lu 	struct pci_bus *parent = bridge->subordinate;
1619bdc4abecSYinghai Lu 	LIST_HEAD(add_list); /* list of resources that
16208424d759SYinghai Lu 					want additional resources */
162132180e40SYinghai Lu 	int tried_times = 0;
1622bdc4abecSYinghai Lu 	LIST_HEAD(fail_head);
1623b9b0bba9SYinghai Lu 	struct pci_dev_resource *fail_res;
16246841ec68SYinghai Lu 	int retval;
162532180e40SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
162632180e40SYinghai Lu 				  IORESOURCE_PREFETCH;
16276841ec68SYinghai Lu 
162832180e40SYinghai Lu again:
16298424d759SYinghai Lu 	__pci_bus_size_bridges(parent, &add_list);
1630bdc4abecSYinghai Lu 	__pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1631bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
163232180e40SYinghai Lu 	tried_times++;
163332180e40SYinghai Lu 
1634bdc4abecSYinghai Lu 	if (list_empty(&fail_head))
16353f579c34SYinghai Lu 		goto enable_all;
163632180e40SYinghai Lu 
163732180e40SYinghai Lu 	if (tried_times >= 2) {
163832180e40SYinghai Lu 		/* still fail, don't need to try more */
1639bffc56d4SYinghai Lu 		free_list(&fail_head);
16403f579c34SYinghai Lu 		goto enable_all;
164132180e40SYinghai Lu 	}
164232180e40SYinghai Lu 
164332180e40SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
164432180e40SYinghai Lu 			 tried_times + 1);
164532180e40SYinghai Lu 
164632180e40SYinghai Lu 	/*
164732180e40SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
164832180e40SYinghai Lu 	 * child device under that bridge
164932180e40SYinghai Lu 	 */
165061e83cddSYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list)
165161e83cddSYinghai Lu 		pci_bus_release_bridge_resources(fail_res->dev->bus,
165261e83cddSYinghai Lu 						 fail_res->flags & type_mask,
165332180e40SYinghai Lu 						 whole_subtree);
165461e83cddSYinghai Lu 
165532180e40SYinghai Lu 	/* restore size and flags */
1656b9b0bba9SYinghai Lu 	list_for_each_entry(fail_res, &fail_head, list) {
1657b9b0bba9SYinghai Lu 		struct resource *res = fail_res->res;
165832180e40SYinghai Lu 
1659b9b0bba9SYinghai Lu 		res->start = fail_res->start;
1660b9b0bba9SYinghai Lu 		res->end = fail_res->end;
1661b9b0bba9SYinghai Lu 		res->flags = fail_res->flags;
1662b9b0bba9SYinghai Lu 		if (fail_res->dev->subordinate)
166332180e40SYinghai Lu 			res->flags = 0;
166432180e40SYinghai Lu 	}
1665bffc56d4SYinghai Lu 	free_list(&fail_head);
166632180e40SYinghai Lu 
166732180e40SYinghai Lu 	goto again;
16683f579c34SYinghai Lu 
16693f579c34SYinghai Lu enable_all:
16703f579c34SYinghai Lu 	retval = pci_reenable_device(bridge);
16719fc9eea0SBjorn Helgaas 	if (retval)
16729fc9eea0SBjorn Helgaas 		dev_err(&bridge->dev, "Error reenabling bridge (%d)\n", retval);
16733f579c34SYinghai Lu 	pci_set_master(bridge);
16746841ec68SYinghai Lu }
16756841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
16769b03088fSYinghai Lu 
167717787940SYinghai Lu void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
16789b03088fSYinghai Lu {
16799b03088fSYinghai Lu 	struct pci_dev *dev;
1680bdc4abecSYinghai Lu 	LIST_HEAD(add_list); /* list of resources that
16819b03088fSYinghai Lu 					want additional resources */
16829b03088fSYinghai Lu 
16839b03088fSYinghai Lu 	down_read(&pci_bus_sem);
16849b03088fSYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
16859b03088fSYinghai Lu 		if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
16869b03088fSYinghai Lu 		    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
16879b03088fSYinghai Lu 			if (dev->subordinate)
16889b03088fSYinghai Lu 				__pci_bus_size_bridges(dev->subordinate,
16899b03088fSYinghai Lu 							 &add_list);
16909b03088fSYinghai Lu 	up_read(&pci_bus_sem);
16919b03088fSYinghai Lu 	__pci_bus_assign_resources(bus, &add_list, NULL);
1692bdc4abecSYinghai Lu 	BUG_ON(!list_empty(&add_list));
169317787940SYinghai Lu }
1694