xref: /openbmc/linux/drivers/pci/setup-bus.c (revision da7822e5ad71ec9b745b412639f1e5e0ba795a20)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	drivers/pci/setup-bus.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Extruded from code written by
51da177e4SLinus Torvalds  *      Dave Rusling (david.rusling@reo.mts.dec.com)
61da177e4SLinus Torvalds  *      David Mosberger (davidm@cs.arizona.edu)
71da177e4SLinus Torvalds  *	David Miller (davem@redhat.com)
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Support routines for initializing a PCI subsystem.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds /*
131da177e4SLinus Torvalds  * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
141da177e4SLinus Torvalds  *	     PCI-PCI bridges cleanup, sorted resource allocation.
151da177e4SLinus Torvalds  * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
161da177e4SLinus Torvalds  *	     Converted to allocation in 3 passes, which gives
171da177e4SLinus Torvalds  *	     tighter packing. Prefetchable range support.
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/init.h>
211da177e4SLinus Torvalds #include <linux/kernel.h>
221da177e4SLinus Torvalds #include <linux/module.h>
231da177e4SLinus Torvalds #include <linux/pci.h>
241da177e4SLinus Torvalds #include <linux/errno.h>
251da177e4SLinus Torvalds #include <linux/ioport.h>
261da177e4SLinus Torvalds #include <linux/cache.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
286faf17f6SChris Wright #include "pci.h"
291da177e4SLinus Torvalds 
30568ddef8SYinghai Lu struct resource_list_x {
31568ddef8SYinghai Lu 	struct resource_list_x *next;
32568ddef8SYinghai Lu 	struct resource *res;
33568ddef8SYinghai Lu 	struct pci_dev *dev;
34568ddef8SYinghai Lu 	resource_size_t start;
35568ddef8SYinghai Lu 	resource_size_t end;
36c8adf9a3SRam Pai 	resource_size_t add_size;
37568ddef8SYinghai Lu 	unsigned long flags;
38568ddef8SYinghai Lu };
39568ddef8SYinghai Lu 
40094732a5SRam Pai #define free_list(type, head) do {                      \
41094732a5SRam Pai 	struct type *list, *tmp;			\
42094732a5SRam Pai 	for (list = (head)->next; list;) {		\
43094732a5SRam Pai 		tmp = list;				\
44094732a5SRam Pai 		list = list->next;			\
45094732a5SRam Pai 		kfree(tmp);				\
46094732a5SRam Pai 	}						\
47094732a5SRam Pai 	(head)->next = NULL;				\
48094732a5SRam Pai } while (0)
49094732a5SRam Pai 
50c8adf9a3SRam Pai /**
51c8adf9a3SRam Pai  * add_to_list() - add a new resource tracker to the list
52c8adf9a3SRam Pai  * @head:	Head of the list
53c8adf9a3SRam Pai  * @dev:	device corresponding to which the resource
54c8adf9a3SRam Pai  *		belongs
55c8adf9a3SRam Pai  * @res:	The resource to be tracked
56c8adf9a3SRam Pai  * @add_size:	additional size to be optionally added
57c8adf9a3SRam Pai  *              to the resource
58c8adf9a3SRam Pai  */
59c8adf9a3SRam Pai static void add_to_list(struct resource_list_x *head,
60c8adf9a3SRam Pai 		 struct pci_dev *dev, struct resource *res,
61c8adf9a3SRam Pai 		 resource_size_t add_size)
62568ddef8SYinghai Lu {
63568ddef8SYinghai Lu 	struct resource_list_x *list = head;
64568ddef8SYinghai Lu 	struct resource_list_x *ln = list->next;
65568ddef8SYinghai Lu 	struct resource_list_x *tmp;
66568ddef8SYinghai Lu 
67568ddef8SYinghai Lu 	tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
68568ddef8SYinghai Lu 	if (!tmp) {
69c8adf9a3SRam Pai 		pr_warning("add_to_list: kmalloc() failed!\n");
70568ddef8SYinghai Lu 		return;
71568ddef8SYinghai Lu 	}
72568ddef8SYinghai Lu 
73568ddef8SYinghai Lu 	tmp->next = ln;
74568ddef8SYinghai Lu 	tmp->res = res;
75568ddef8SYinghai Lu 	tmp->dev = dev;
76568ddef8SYinghai Lu 	tmp->start = res->start;
77568ddef8SYinghai Lu 	tmp->end = res->end;
78568ddef8SYinghai Lu 	tmp->flags = res->flags;
79c8adf9a3SRam Pai 	tmp->add_size = add_size;
80568ddef8SYinghai Lu 	list->next = tmp;
81568ddef8SYinghai Lu }
82568ddef8SYinghai Lu 
83c8adf9a3SRam Pai static void add_to_failed_list(struct resource_list_x *head,
84c8adf9a3SRam Pai 				struct pci_dev *dev, struct resource *res)
85c8adf9a3SRam Pai {
86c8adf9a3SRam Pai 	add_to_list(head, dev, res, 0);
87c8adf9a3SRam Pai }
88c8adf9a3SRam Pai 
896841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev,
906841ec68SYinghai Lu 				 struct resource_list *head)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	u16 class = dev->class >> 8;
931da177e4SLinus Torvalds 
949bded00bSKenji Kaneshige 	/* Don't touch classless devices or host bridges or ioapics.  */
956841ec68SYinghai Lu 	if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
966841ec68SYinghai Lu 		return;
971da177e4SLinus Torvalds 
989bded00bSKenji Kaneshige 	/* Don't touch ioapic devices already enabled by firmware */
9923186279SSatoru Takeuchi 	if (class == PCI_CLASS_SYSTEM_PIC) {
1009bded00bSKenji Kaneshige 		u16 command;
1019bded00bSKenji Kaneshige 		pci_read_config_word(dev, PCI_COMMAND, &command);
1029bded00bSKenji Kaneshige 		if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
1036841ec68SYinghai Lu 			return;
10423186279SSatoru Takeuchi 	}
10523186279SSatoru Takeuchi 
1066841ec68SYinghai Lu 	pdev_sort_resources(dev, head);
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds 
109fc075e1dSRam Pai static inline void reset_resource(struct resource *res)
110fc075e1dSRam Pai {
111fc075e1dSRam Pai 	res->start = 0;
112fc075e1dSRam Pai 	res->end = 0;
113fc075e1dSRam Pai 	res->flags = 0;
114fc075e1dSRam Pai }
115fc075e1dSRam Pai 
116c8adf9a3SRam Pai /**
117c8adf9a3SRam Pai  * adjust_resources_sorted() - satisfy any additional resource requests
118c8adf9a3SRam Pai  *
119c8adf9a3SRam Pai  * @add_head : head of the list tracking requests requiring additional
120c8adf9a3SRam Pai  *             resources
121c8adf9a3SRam Pai  * @head     : head of the list tracking requests with allocated
122c8adf9a3SRam Pai  *             resources
123c8adf9a3SRam Pai  *
124c8adf9a3SRam Pai  * Walk through each element of the add_head and try to procure
125c8adf9a3SRam Pai  * additional resources for the element, provided the element
126c8adf9a3SRam Pai  * is in the head list.
127c8adf9a3SRam Pai  */
128c8adf9a3SRam Pai static void adjust_resources_sorted(struct resource_list_x *add_head,
129c8adf9a3SRam Pai 		struct resource_list *head)
130c8adf9a3SRam Pai {
131c8adf9a3SRam Pai 	struct resource *res;
132c8adf9a3SRam Pai 	struct resource_list_x *list, *tmp, *prev;
133c8adf9a3SRam Pai 	struct resource_list *hlist;
134c8adf9a3SRam Pai 	resource_size_t add_size;
135c8adf9a3SRam Pai 	int idx;
136c8adf9a3SRam Pai 
137c8adf9a3SRam Pai 	prev = add_head;
138c8adf9a3SRam Pai 	for (list = add_head->next; list;) {
139c8adf9a3SRam Pai 		res = list->res;
140c8adf9a3SRam Pai 		/* skip resource that has been reset */
141c8adf9a3SRam Pai 		if (!res->flags)
142c8adf9a3SRam Pai 			goto out;
143c8adf9a3SRam Pai 
144c8adf9a3SRam Pai 		/* skip this resource if not found in head list */
145c8adf9a3SRam Pai 		for (hlist = head->next; hlist && hlist->res != res;
146c8adf9a3SRam Pai 				hlist = hlist->next);
147c8adf9a3SRam Pai 		if (!hlist) { /* just skip */
148c8adf9a3SRam Pai 			prev = list;
149c8adf9a3SRam Pai 			list = list->next;
150c8adf9a3SRam Pai 			continue;
151c8adf9a3SRam Pai 		}
152c8adf9a3SRam Pai 
153c8adf9a3SRam Pai 		idx = res - &list->dev->resource[0];
154c8adf9a3SRam Pai 		add_size=list->add_size;
155c8adf9a3SRam Pai 		if (!resource_size(res) && add_size) {
156c8adf9a3SRam Pai 			 res->end = res->start + add_size - 1;
157c8adf9a3SRam Pai 			 if(pci_assign_resource(list->dev, idx))
158c8adf9a3SRam Pai 				reset_resource(res);
159c8adf9a3SRam Pai 		} else if (add_size) {
160c8adf9a3SRam Pai 			adjust_resource(res, res->start,
161c8adf9a3SRam Pai 				resource_size(res) + add_size);
162c8adf9a3SRam Pai 		}
163c8adf9a3SRam Pai out:
164c8adf9a3SRam Pai 		tmp = list;
165c8adf9a3SRam Pai 		prev->next = list = list->next;
166c8adf9a3SRam Pai 		kfree(tmp);
167c8adf9a3SRam Pai 	}
168c8adf9a3SRam Pai }
169c8adf9a3SRam Pai 
170c8adf9a3SRam Pai /**
171c8adf9a3SRam Pai  * assign_requested_resources_sorted() - satisfy resource requests
172c8adf9a3SRam Pai  *
173c8adf9a3SRam Pai  * @head : head of the list tracking requests for resources
174c8adf9a3SRam Pai  * @failed_list : head of the list tracking requests that could
175c8adf9a3SRam Pai  *		not be allocated
176c8adf9a3SRam Pai  *
177c8adf9a3SRam Pai  * Satisfy resource requests of each element in the list. Add
178c8adf9a3SRam Pai  * requests that could not satisfied to the failed_list.
179c8adf9a3SRam Pai  */
180c8adf9a3SRam Pai static void assign_requested_resources_sorted(struct resource_list *head,
1816841ec68SYinghai Lu 				 struct resource_list_x *fail_head)
1826841ec68SYinghai Lu {
1836841ec68SYinghai Lu 	struct resource *res;
184c8adf9a3SRam Pai 	struct resource_list *list;
1856841ec68SYinghai Lu 	int idx;
1866841ec68SYinghai Lu 
187c8adf9a3SRam Pai 	for (list = head->next; list; list = list->next) {
1881da177e4SLinus Torvalds 		res = list->res;
1891da177e4SLinus Torvalds 		idx = res - &list->dev->resource[0];
190c8adf9a3SRam Pai 		if (resource_size(res) && pci_assign_resource(list->dev, idx)) {
1919a928660SYinghai Lu 			if (fail_head && !pci_is_root_bus(list->dev->bus)) {
1929a928660SYinghai Lu 				/*
1939a928660SYinghai Lu 				 * if the failed res is for ROM BAR, and it will
1949a928660SYinghai Lu 				 * be enabled later, don't add it to the list
1959a928660SYinghai Lu 				 */
1969a928660SYinghai Lu 				if (!((idx == PCI_ROM_RESOURCE) &&
1979a928660SYinghai Lu 				      (!(res->flags & IORESOURCE_ROM_ENABLE))))
198568ddef8SYinghai Lu 					add_to_failed_list(fail_head, list->dev, res);
1999a928660SYinghai Lu 			}
200fc075e1dSRam Pai 			reset_resource(res);
201542df5deSRajesh Shah 		}
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
205c8adf9a3SRam Pai static void __assign_resources_sorted(struct resource_list *head,
206c8adf9a3SRam Pai 				 struct resource_list_x *add_head,
207c8adf9a3SRam Pai 				 struct resource_list_x *fail_head)
208c8adf9a3SRam Pai {
209c8adf9a3SRam Pai 	/* Satisfy the must-have resource requests */
210c8adf9a3SRam Pai 	assign_requested_resources_sorted(head, fail_head);
211c8adf9a3SRam Pai 
212c8adf9a3SRam Pai 	/* Try to satisfy any additional nice-to-have resource
213c8adf9a3SRam Pai 		requests */
214c8adf9a3SRam Pai 	if (add_head)
215c8adf9a3SRam Pai 		adjust_resources_sorted(add_head, head);
216c8adf9a3SRam Pai 	free_list(resource_list, head);
217c8adf9a3SRam Pai }
218c8adf9a3SRam Pai 
2196841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev,
2206841ec68SYinghai Lu 				 struct resource_list_x *fail_head)
2216841ec68SYinghai Lu {
2226841ec68SYinghai Lu 	struct resource_list head;
2236841ec68SYinghai Lu 
2246841ec68SYinghai Lu 	head.next = NULL;
2256841ec68SYinghai Lu 	__dev_sort_resources(dev, &head);
226c8adf9a3SRam Pai 	__assign_resources_sorted(&head, NULL, fail_head);
2276841ec68SYinghai Lu 
2286841ec68SYinghai Lu }
2296841ec68SYinghai Lu 
2306841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus,
231c8adf9a3SRam Pai 					 struct resource_list_x *add_head,
2326841ec68SYinghai Lu 					 struct resource_list_x *fail_head)
2336841ec68SYinghai Lu {
2346841ec68SYinghai Lu 	struct pci_dev *dev;
2356841ec68SYinghai Lu 	struct resource_list head;
2366841ec68SYinghai Lu 
2376841ec68SYinghai Lu 	head.next = NULL;
2386841ec68SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
2396841ec68SYinghai Lu 		__dev_sort_resources(dev, &head);
2406841ec68SYinghai Lu 
241c8adf9a3SRam Pai 	__assign_resources_sorted(&head, add_head, fail_head);
2426841ec68SYinghai Lu }
2436841ec68SYinghai Lu 
244b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus)
2451da177e4SLinus Torvalds {
2461da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
247c7dabef8SBjorn Helgaas 	struct resource *res;
2481da177e4SLinus Torvalds 	struct pci_bus_region region;
2491da177e4SLinus Torvalds 
250865df576SBjorn Helgaas 	dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
251865df576SBjorn Helgaas 		 bus->secondary, bus->subordinate);
2521da177e4SLinus Torvalds 
253c7dabef8SBjorn Helgaas 	res = bus->resource[0];
254c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
255c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
2561da177e4SLinus Torvalds 		/*
2571da177e4SLinus Torvalds 		 * The IO resource is allocated a range twice as large as it
2581da177e4SLinus Torvalds 		 * would normally need.  This allows us to set both IO regs.
2591da177e4SLinus Torvalds 		 */
260c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
2611da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
2621da177e4SLinus Torvalds 					region.start);
2631da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
2641da177e4SLinus Torvalds 					region.end);
2651da177e4SLinus Torvalds 	}
2661da177e4SLinus Torvalds 
267c7dabef8SBjorn Helgaas 	res = bus->resource[1];
268c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
269c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
270c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
2711da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
2721da177e4SLinus Torvalds 					region.start);
2731da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
2741da177e4SLinus Torvalds 					region.end);
2751da177e4SLinus Torvalds 	}
2761da177e4SLinus Torvalds 
277c7dabef8SBjorn Helgaas 	res = bus->resource[2];
278c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
279c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
280c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
2811da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
2821da177e4SLinus Torvalds 					region.start);
2831da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
2841da177e4SLinus Torvalds 					region.end);
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 
287c7dabef8SBjorn Helgaas 	res = bus->resource[3];
288c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
289c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
290c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
2911da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
2921da177e4SLinus Torvalds 					region.start);
2931da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
2941da177e4SLinus Torvalds 					region.end);
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds }
297b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected.
3001da177e4SLinus Torvalds    PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
3011da177e4SLinus Torvalds    requires that if there is no I/O ports or memory behind the
3021da177e4SLinus Torvalds    bridge, corresponding range must be turned off by writing base
3031da177e4SLinus Torvalds    value greater than limit to the bridge's base/limit registers.
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds    Note: care must be taken when updating I/O base/limit registers
3061da177e4SLinus Torvalds    of bridges which support 32-bit I/O. This update requires two
3071da177e4SLinus Torvalds    config space writes, so it's quite possible that an I/O window of
3081da177e4SLinus Torvalds    the bridge will have some undesirable address (e.g. 0) after the
3091da177e4SLinus Torvalds    first write. Ditto 64-bit prefetchable MMIO.  */
3107cc5997dSYinghai Lu static void pci_setup_bridge_io(struct pci_bus *bus)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
313c7dabef8SBjorn Helgaas 	struct resource *res;
3141da177e4SLinus Torvalds 	struct pci_bus_region region;
3157cc5997dSYinghai Lu 	u32 l, io_upper16;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	/* Set up the top and bottom of the PCI I/O segment for this bus. */
318c7dabef8SBjorn Helgaas 	res = bus->resource[0];
319c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
320c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
3211da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_IO_BASE, &l);
3221da177e4SLinus Torvalds 		l &= 0xffff0000;
3231da177e4SLinus Torvalds 		l |= (region.start >> 8) & 0x00f0;
3241da177e4SLinus Torvalds 		l |= region.end & 0xf000;
3251da177e4SLinus Torvalds 		/* Set up upper 16 bits of I/O base/limit. */
3261da177e4SLinus Torvalds 		io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
327c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
3287cc5997dSYinghai Lu 	} else {
3291da177e4SLinus Torvalds 		/* Clear upper 16 bits of I/O base/limit. */
3301da177e4SLinus Torvalds 		io_upper16 = 0;
3311da177e4SLinus Torvalds 		l = 0x00f0;
332c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window [io  disabled]\n");
3331da177e4SLinus Torvalds 	}
3341da177e4SLinus Torvalds 	/* Temporarily disable the I/O range before updating PCI_IO_BASE. */
3351da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
3361da177e4SLinus Torvalds 	/* Update lower 16 bits of I/O base/limit. */
3371da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE, l);
3381da177e4SLinus Torvalds 	/* Update upper 16 bits of I/O base/limit. */
3391da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
3407cc5997dSYinghai Lu }
3411da177e4SLinus Torvalds 
3427cc5997dSYinghai Lu static void pci_setup_bridge_mmio(struct pci_bus *bus)
3437cc5997dSYinghai Lu {
3447cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
3457cc5997dSYinghai Lu 	struct resource *res;
3467cc5997dSYinghai Lu 	struct pci_bus_region region;
3477cc5997dSYinghai Lu 	u32 l;
3487cc5997dSYinghai Lu 
3497cc5997dSYinghai Lu 	/* Set up the top and bottom of the PCI Memory segment for this bus. */
350c7dabef8SBjorn Helgaas 	res = bus->resource[1];
351c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
352c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
3531da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
3541da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
355c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
3567cc5997dSYinghai Lu 	} else {
3571da177e4SLinus Torvalds 		l = 0x0000fff0;
358c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window [mem disabled]\n");
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
3617cc5997dSYinghai Lu }
3627cc5997dSYinghai Lu 
3637cc5997dSYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
3647cc5997dSYinghai Lu {
3657cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
3667cc5997dSYinghai Lu 	struct resource *res;
3677cc5997dSYinghai Lu 	struct pci_bus_region region;
3687cc5997dSYinghai Lu 	u32 l, bu, lu;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	/* Clear out the upper 32 bits of PREF limit.
3711da177e4SLinus Torvalds 	   If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
3721da177e4SLinus Torvalds 	   disables PREF range, which is ok. */
3731da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	/* Set up PREF base/limit. */
376c40a22e0SBenjamin Herrenschmidt 	bu = lu = 0;
377c7dabef8SBjorn Helgaas 	res = bus->resource[2];
378c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
379c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_PREFETCH) {
3801da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
3811da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
382c7dabef8SBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64) {
38313d36c24SAndrew Morton 			bu = upper_32_bits(region.start);
38413d36c24SAndrew Morton 			lu = upper_32_bits(region.end);
3851f82de10SYinghai Lu 		}
386c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
3877cc5997dSYinghai Lu 	} else {
3881da177e4SLinus Torvalds 		l = 0x0000fff0;
389c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window [mem pref disabled]\n");
3901da177e4SLinus Torvalds 	}
3911da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
3921da177e4SLinus Torvalds 
393c40a22e0SBenjamin Herrenschmidt 	/* Set the upper 32 bits of PREF base & limit. */
394c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
395c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
3967cc5997dSYinghai Lu }
3977cc5997dSYinghai Lu 
3987cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
3997cc5997dSYinghai Lu {
4007cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
4017cc5997dSYinghai Lu 
4027cc5997dSYinghai Lu 	dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
4037cc5997dSYinghai Lu 		 bus->secondary, bus->subordinate);
4047cc5997dSYinghai Lu 
4057cc5997dSYinghai Lu 	if (type & IORESOURCE_IO)
4067cc5997dSYinghai Lu 		pci_setup_bridge_io(bus);
4077cc5997dSYinghai Lu 
4087cc5997dSYinghai Lu 	if (type & IORESOURCE_MEM)
4097cc5997dSYinghai Lu 		pci_setup_bridge_mmio(bus);
4107cc5997dSYinghai Lu 
4117cc5997dSYinghai Lu 	if (type & IORESOURCE_PREFETCH)
4127cc5997dSYinghai Lu 		pci_setup_bridge_mmio_pref(bus);
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
4151da177e4SLinus Torvalds }
4161da177e4SLinus Torvalds 
4177cc5997dSYinghai Lu static void pci_setup_bridge(struct pci_bus *bus)
4187cc5997dSYinghai Lu {
4197cc5997dSYinghai Lu 	unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
4207cc5997dSYinghai Lu 				  IORESOURCE_PREFETCH;
4217cc5997dSYinghai Lu 
4227cc5997dSYinghai Lu 	__pci_setup_bridge(bus, type);
4237cc5997dSYinghai Lu }
4247cc5997dSYinghai Lu 
4251da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and
4261da177e4SLinus Torvalds    prefetchable memory ranges. If not, the respective
4271da177e4SLinus Torvalds    base/limit registers must be read-only and read as 0. */
42896bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	u16 io;
4311da177e4SLinus Torvalds 	u32 pmem;
4321da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
4331da177e4SLinus Torvalds 	struct resource *b_res;
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
4361da177e4SLinus Torvalds 	b_res[1].flags |= IORESOURCE_MEM;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_IO_BASE, &io);
4391da177e4SLinus Torvalds 	if (!io) {
4401da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
4411da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_IO_BASE, &io);
4421da177e4SLinus Torvalds  		pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
4431da177e4SLinus Torvalds  	}
4441da177e4SLinus Torvalds  	if (io)
4451da177e4SLinus Torvalds 		b_res[0].flags |= IORESOURCE_IO;
4461da177e4SLinus Torvalds 	/*  DECchip 21050 pass 2 errata: the bridge may miss an address
4471da177e4SLinus Torvalds 	    disconnect boundary by one PCI data phase.
4481da177e4SLinus Torvalds 	    Workaround: do not use prefetching on this device. */
4491da177e4SLinus Torvalds 	if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
4501da177e4SLinus Torvalds 		return;
4511da177e4SLinus Torvalds 	pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
4521da177e4SLinus Torvalds 	if (!pmem) {
4531da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
4541da177e4SLinus Torvalds 					       0xfff0fff0);
4551da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
4561da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
4571da177e4SLinus Torvalds 	}
4581f82de10SYinghai Lu 	if (pmem) {
4591da177e4SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
46099586105SYinghai Lu 		if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
46199586105SYinghai Lu 		    PCI_PREF_RANGE_TYPE_64) {
4621f82de10SYinghai Lu 			b_res[2].flags |= IORESOURCE_MEM_64;
46399586105SYinghai Lu 			b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
46499586105SYinghai Lu 		}
4651f82de10SYinghai Lu 	}
4661f82de10SYinghai Lu 
4671f82de10SYinghai Lu 	/* double check if bridge does support 64 bit pref */
4681f82de10SYinghai Lu 	if (b_res[2].flags & IORESOURCE_MEM_64) {
4691f82de10SYinghai Lu 		u32 mem_base_hi, tmp;
4701f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
4711f82de10SYinghai Lu 					 &mem_base_hi);
4721f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
4731f82de10SYinghai Lu 					       0xffffffff);
4741f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
4751f82de10SYinghai Lu 		if (!tmp)
4761f82de10SYinghai Lu 			b_res[2].flags &= ~IORESOURCE_MEM_64;
4771f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
4781f82de10SYinghai Lu 				       mem_base_hi);
4791f82de10SYinghai Lu 	}
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds /* Helper function for sizing routines: find first available
4831da177e4SLinus Torvalds    bus resource of a given type. Note: we intentionally skip
4841da177e4SLinus Torvalds    the bus resources which have already been assigned (that is,
4851da177e4SLinus Torvalds    have non-NULL parent resource). */
48696bde06aSSam Ravnborg static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
4871da177e4SLinus Torvalds {
4881da177e4SLinus Torvalds 	int i;
4891da177e4SLinus Torvalds 	struct resource *r;
4901da177e4SLinus Torvalds 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
4911da177e4SLinus Torvalds 				  IORESOURCE_PREFETCH;
4921da177e4SLinus Torvalds 
49389a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, r, i) {
494299de034SIvan Kokshaysky 		if (r == &ioport_resource || r == &iomem_resource)
495299de034SIvan Kokshaysky 			continue;
49655a10984SJesse Barnes 		if (r && (r->flags & type_mask) == type && !r->parent)
4971da177e4SLinus Torvalds 			return r;
4981da177e4SLinus Torvalds 	}
4991da177e4SLinus Torvalds 	return NULL;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
50213583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size,
50313583b16SRam Pai 		resource_size_t min_size,
50413583b16SRam Pai 		resource_size_t size1,
50513583b16SRam Pai 		resource_size_t old_size,
50613583b16SRam Pai 		resource_size_t align)
50713583b16SRam Pai {
50813583b16SRam Pai 	if (size < min_size)
50913583b16SRam Pai 		size = min_size;
51013583b16SRam Pai 	if (old_size == 1 )
51113583b16SRam Pai 		old_size = 0;
51213583b16SRam Pai 	/* To be fixed in 2.5: we should have sort of HAVE_ISA
51313583b16SRam Pai 	   flag in the struct pci_bus. */
51413583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
51513583b16SRam Pai 	size = (size & 0xff) + ((size & ~0xffUL) << 2);
51613583b16SRam Pai #endif
51713583b16SRam Pai 	size = ALIGN(size + size1, align);
51813583b16SRam Pai 	if (size < old_size)
51913583b16SRam Pai 		size = old_size;
52013583b16SRam Pai 	return size;
52113583b16SRam Pai }
52213583b16SRam Pai 
52313583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size,
52413583b16SRam Pai 		resource_size_t min_size,
52513583b16SRam Pai 		resource_size_t size1,
52613583b16SRam Pai 		resource_size_t old_size,
52713583b16SRam Pai 		resource_size_t align)
52813583b16SRam Pai {
52913583b16SRam Pai 	if (size < min_size)
53013583b16SRam Pai 		size = min_size;
53113583b16SRam Pai 	if (old_size == 1 )
53213583b16SRam Pai 		old_size = 0;
53313583b16SRam Pai 	if (size < old_size)
53413583b16SRam Pai 		size = old_size;
53513583b16SRam Pai 	size = ALIGN(size + size1, align);
53613583b16SRam Pai 	return size;
53713583b16SRam Pai }
53813583b16SRam Pai 
539c8adf9a3SRam Pai /**
540c8adf9a3SRam Pai  * pbus_size_io() - size the io window of a given bus
541c8adf9a3SRam Pai  *
542c8adf9a3SRam Pai  * @bus : the bus
543c8adf9a3SRam Pai  * @min_size : the minimum io window that must to be allocated
544c8adf9a3SRam Pai  * @add_size : additional optional io window
545c8adf9a3SRam Pai  * @add_head : track the additional io window on this list
546c8adf9a3SRam Pai  *
547c8adf9a3SRam Pai  * Sizing the IO windows of the PCI-PCI bridge is trivial,
548c8adf9a3SRam Pai  * since these windows have 4K granularity and the IO ranges
549c8adf9a3SRam Pai  * of non-bridge PCI devices are limited to 256 bytes.
550c8adf9a3SRam Pai  * We must be careful with the ISA aliasing though.
551c8adf9a3SRam Pai  */
552c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
553c8adf9a3SRam Pai 		resource_size_t add_size, struct resource_list_x *add_head)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	struct pci_dev *dev;
5561da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
557c8adf9a3SRam Pai 	unsigned long size = 0, size0 = 0, size1 = 0;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	if (!b_res)
5601da177e4SLinus Torvalds  		return;
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
5631da177e4SLinus Torvalds 		int i;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
5661da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
5671da177e4SLinus Torvalds 			unsigned long r_size;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 			if (r->parent || !(r->flags & IORESOURCE_IO))
5701da177e4SLinus Torvalds 				continue;
571022edd86SZhao, Yu 			r_size = resource_size(r);
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 			if (r_size < 0x400)
5741da177e4SLinus Torvalds 				/* Might be re-aligned for ISA */
5751da177e4SLinus Torvalds 				size += r_size;
5761da177e4SLinus Torvalds 			else
5771da177e4SLinus Torvalds 				size1 += r_size;
5781da177e4SLinus Torvalds 		}
5791da177e4SLinus Torvalds 	}
580c8adf9a3SRam Pai 	size0 = calculate_iosize(size, min_size, size1,
58113583b16SRam Pai 			resource_size(b_res), 4096);
582c8adf9a3SRam Pai 	size1 = !add_size? size0:
583c8adf9a3SRam Pai 		calculate_iosize(size, min_size+add_size, size1,
584c8adf9a3SRam Pai 			resource_size(b_res), 4096);
585c8adf9a3SRam Pai 	if (!size0 && !size1) {
586865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
587865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
588865df576SBjorn Helgaas 				 "%pR to [bus %02x-%02x] (unused)\n", b_res,
589865df576SBjorn Helgaas 				 bus->secondary, bus->subordinate);
5901da177e4SLinus Torvalds 		b_res->flags = 0;
5911da177e4SLinus Torvalds 		return;
5921da177e4SLinus Torvalds 	}
5931da177e4SLinus Torvalds 	/* Alignment of the IO window is always 4K */
5941da177e4SLinus Torvalds 	b_res->start = 4096;
595c8adf9a3SRam Pai 	b_res->end = b_res->start + size0 - 1;
59688452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
597c8adf9a3SRam Pai 	if (size1 > size0 && add_head)
598c8adf9a3SRam Pai 		add_to_list(add_head, bus->self, b_res, size1-size0);
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds 
601c8adf9a3SRam Pai /**
602c8adf9a3SRam Pai  * pbus_size_mem() - size the memory window of a given bus
603c8adf9a3SRam Pai  *
604c8adf9a3SRam Pai  * @bus : the bus
605c8adf9a3SRam Pai  * @min_size : the minimum memory window that must to be allocated
606c8adf9a3SRam Pai  * @add_size : additional optional memory window
607c8adf9a3SRam Pai  * @add_head : track the additional memory window on this list
608c8adf9a3SRam Pai  *
609c8adf9a3SRam Pai  * Calculate the size of the bus and minimal alignment which
610c8adf9a3SRam Pai  * guarantees that all child resources fit in this size.
611c8adf9a3SRam Pai  */
61228760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
613c8adf9a3SRam Pai 			 unsigned long type, resource_size_t min_size,
614c8adf9a3SRam Pai 			resource_size_t add_size,
615c8adf9a3SRam Pai 			struct resource_list_x *add_head)
6161da177e4SLinus Torvalds {
6171da177e4SLinus Torvalds 	struct pci_dev *dev;
618c8adf9a3SRam Pai 	resource_size_t min_align, align, size, size0, size1;
619c40a22e0SBenjamin Herrenschmidt 	resource_size_t aligns[12];	/* Alignments from 1Mb to 2Gb */
6201da177e4SLinus Torvalds 	int order, max_order;
6211da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, type);
6221f82de10SYinghai Lu 	unsigned int mem64_mask = 0;
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	if (!b_res)
6251da177e4SLinus Torvalds 		return 0;
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
6281da177e4SLinus Torvalds 	max_order = 0;
6291da177e4SLinus Torvalds 	size = 0;
6301da177e4SLinus Torvalds 
6311f82de10SYinghai Lu 	mem64_mask = b_res->flags & IORESOURCE_MEM_64;
6321f82de10SYinghai Lu 	b_res->flags &= ~IORESOURCE_MEM_64;
6331f82de10SYinghai Lu 
6341da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
6351da177e4SLinus Torvalds 		int i;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
6381da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
639c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 			if (r->parent || (r->flags & mask) != type)
6421da177e4SLinus Torvalds 				continue;
643022edd86SZhao, Yu 			r_size = resource_size(r);
6441da177e4SLinus Torvalds 			/* For bridges size != alignment */
6456faf17f6SChris Wright 			align = pci_resource_alignment(dev, r);
6461da177e4SLinus Torvalds 			order = __ffs(align) - 20;
6471da177e4SLinus Torvalds 			if (order > 11) {
648865df576SBjorn Helgaas 				dev_warn(&dev->dev, "disabling BAR %d: %pR "
649865df576SBjorn Helgaas 					 "(bad alignment %#llx)\n", i, r,
650865df576SBjorn Helgaas 					 (unsigned long long) align);
6511da177e4SLinus Torvalds 				r->flags = 0;
6521da177e4SLinus Torvalds 				continue;
6531da177e4SLinus Torvalds 			}
6541da177e4SLinus Torvalds 			size += r_size;
6551da177e4SLinus Torvalds 			if (order < 0)
6561da177e4SLinus Torvalds 				order = 0;
6571da177e4SLinus Torvalds 			/* Exclude ranges with size > align from
6581da177e4SLinus Torvalds 			   calculation of the alignment. */
6591da177e4SLinus Torvalds 			if (r_size == align)
6601da177e4SLinus Torvalds 				aligns[order] += align;
6611da177e4SLinus Torvalds 			if (order > max_order)
6621da177e4SLinus Torvalds 				max_order = order;
6631f82de10SYinghai Lu 			mem64_mask &= r->flags & IORESOURCE_MEM_64;
6641da177e4SLinus Torvalds 		}
6651da177e4SLinus Torvalds 	}
6661da177e4SLinus Torvalds 	align = 0;
6671da177e4SLinus Torvalds 	min_align = 0;
6681da177e4SLinus Torvalds 	for (order = 0; order <= max_order; order++) {
6698308c54dSJeremy Fitzhardinge 		resource_size_t align1 = 1;
6708308c54dSJeremy Fitzhardinge 
6718308c54dSJeremy Fitzhardinge 		align1 <<= (order + 20);
6728308c54dSJeremy Fitzhardinge 
6731da177e4SLinus Torvalds 		if (!align)
6741da177e4SLinus Torvalds 			min_align = align1;
6756f6f8c2fSMilind Arun Choudhary 		else if (ALIGN(align + min_align, min_align) < align1)
6761da177e4SLinus Torvalds 			min_align = align1 >> 1;
6771da177e4SLinus Torvalds 		align += aligns[order];
6781da177e4SLinus Torvalds 	}
679b42282e5SLinus Torvalds 	size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
680c8adf9a3SRam Pai 	size1 = !add_size ? size :
681c8adf9a3SRam Pai 		calculate_memsize(size, min_size+add_size, 0,
682b42282e5SLinus Torvalds 				resource_size(b_res), min_align);
683c8adf9a3SRam Pai 	if (!size0 && !size1) {
684865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
685865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
686865df576SBjorn Helgaas 				 "%pR to [bus %02x-%02x] (unused)\n", b_res,
687865df576SBjorn Helgaas 				 bus->secondary, bus->subordinate);
6881da177e4SLinus Torvalds 		b_res->flags = 0;
6891da177e4SLinus Torvalds 		return 1;
6901da177e4SLinus Torvalds 	}
6911da177e4SLinus Torvalds 	b_res->start = min_align;
692c8adf9a3SRam Pai 	b_res->end = size0 + min_align - 1;
693c8adf9a3SRam Pai 	b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
694c8adf9a3SRam Pai 	if (size1 > size0 && add_head)
695c8adf9a3SRam Pai 		add_to_list(add_head, bus->self, b_res, size1-size0);
6961da177e4SLinus Torvalds 	return 1;
6971da177e4SLinus Torvalds }
6981da177e4SLinus Torvalds 
6995468ae61SAdrian Bunk static void pci_bus_size_cardbus(struct pci_bus *bus)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
7021da177e4SLinus Torvalds 	struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
7031da177e4SLinus Torvalds 	u16 ctrl;
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	/*
7061da177e4SLinus Torvalds 	 * Reserve some resources for CardBus.  We reserve
7071da177e4SLinus Torvalds 	 * a fixed amount of bus space for CardBus bridges.
7081da177e4SLinus Torvalds 	 */
709934b7024SLinus Torvalds 	b_res[0].start = 0;
710934b7024SLinus Torvalds 	b_res[0].end = pci_cardbus_io_size - 1;
711934b7024SLinus Torvalds 	b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
7121da177e4SLinus Torvalds 
713934b7024SLinus Torvalds 	b_res[1].start = 0;
714934b7024SLinus Torvalds 	b_res[1].end = pci_cardbus_io_size - 1;
715934b7024SLinus Torvalds 	b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	/*
7181da177e4SLinus Torvalds 	 * Check whether prefetchable memory is supported
7191da177e4SLinus Torvalds 	 * by this bridge.
7201da177e4SLinus Torvalds 	 */
7211da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
7221da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
7231da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
7241da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
7251da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
7261da177e4SLinus Torvalds 	}
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	/*
7291da177e4SLinus Torvalds 	 * If we have prefetchable memory support, allocate
7301da177e4SLinus Torvalds 	 * two regions.  Otherwise, allocate one region of
7311da177e4SLinus Torvalds 	 * twice the size.
7321da177e4SLinus Torvalds 	 */
7331da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
734934b7024SLinus Torvalds 		b_res[2].start = 0;
735934b7024SLinus Torvalds 		b_res[2].end = pci_cardbus_mem_size - 1;
736934b7024SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
7371da177e4SLinus Torvalds 
738934b7024SLinus Torvalds 		b_res[3].start = 0;
739934b7024SLinus Torvalds 		b_res[3].end = pci_cardbus_mem_size - 1;
740934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
7411da177e4SLinus Torvalds 	} else {
742934b7024SLinus Torvalds 		b_res[3].start = 0;
743934b7024SLinus Torvalds 		b_res[3].end = pci_cardbus_mem_size * 2 - 1;
744934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
7451da177e4SLinus Torvalds 	}
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds 
748c8adf9a3SRam Pai void __ref __pci_bus_size_bridges(struct pci_bus *bus,
749c8adf9a3SRam Pai 			struct resource_list_x *add_head)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	struct pci_dev *dev;
7521da177e4SLinus Torvalds 	unsigned long mask, prefmask;
753c8adf9a3SRam Pai 	resource_size_t additional_mem_size = 0, additional_io_size = 0;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
7561da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
7571da177e4SLinus Torvalds 		if (!b)
7581da177e4SLinus Torvalds 			continue;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 		switch (dev->class >> 8) {
7611da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
7621da177e4SLinus Torvalds 			pci_bus_size_cardbus(b);
7631da177e4SLinus Torvalds 			break;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
7661da177e4SLinus Torvalds 		default:
767c8adf9a3SRam Pai 			__pci_bus_size_bridges(b, add_head);
7681da177e4SLinus Torvalds 			break;
7691da177e4SLinus Torvalds 		}
7701da177e4SLinus Torvalds 	}
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	/* The root bus? */
7731da177e4SLinus Torvalds 	if (!bus->self)
7741da177e4SLinus Torvalds 		return;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	switch (bus->self->class >> 8) {
7771da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_CARDBUS:
7781da177e4SLinus Torvalds 		/* don't size cardbuses yet. */
7791da177e4SLinus Torvalds 		break;
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_PCI:
7821da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
78328760489SEric W. Biederman 		if (bus->self->is_hotplug_bridge) {
784c8adf9a3SRam Pai 			additional_io_size  = pci_hotplug_io_size;
785c8adf9a3SRam Pai 			additional_mem_size = pci_hotplug_mem_size;
78628760489SEric W. Biederman 		}
787c8adf9a3SRam Pai 		/*
788c8adf9a3SRam Pai 		 * Follow thru
789c8adf9a3SRam Pai 		 */
7901da177e4SLinus Torvalds 	default:
791c8adf9a3SRam Pai 		pbus_size_io(bus, 0, additional_io_size, add_head);
7921da177e4SLinus Torvalds 		/* If the bridge supports prefetchable range, size it
7931da177e4SLinus Torvalds 		   separately. If it doesn't, or its prefetchable window
7941da177e4SLinus Torvalds 		   has already been allocated by arch code, try
7951da177e4SLinus Torvalds 		   non-prefetchable range for both types of PCI memory
7961da177e4SLinus Torvalds 		   resources. */
7971da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
7981da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
799c8adf9a3SRam Pai 		if (pbus_size_mem(bus, prefmask, prefmask, 0, additional_mem_size, add_head))
8001da177e4SLinus Torvalds 			mask = prefmask; /* Success, size non-prefetch only. */
80128760489SEric W. Biederman 		else
802c8adf9a3SRam Pai 			additional_mem_size += additional_mem_size;
803c8adf9a3SRam Pai 		pbus_size_mem(bus, mask, IORESOURCE_MEM, 0, additional_mem_size, add_head);
8041da177e4SLinus Torvalds 		break;
8051da177e4SLinus Torvalds 	}
8061da177e4SLinus Torvalds }
807c8adf9a3SRam Pai 
808c8adf9a3SRam Pai void __ref pci_bus_size_bridges(struct pci_bus *bus)
809c8adf9a3SRam Pai {
810c8adf9a3SRam Pai 	__pci_bus_size_bridges(bus, NULL);
811c8adf9a3SRam Pai }
8121da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
8131da177e4SLinus Torvalds 
814568ddef8SYinghai Lu static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
815c8adf9a3SRam Pai 					 struct resource_list_x *add_head,
816568ddef8SYinghai Lu 					 struct resource_list_x *fail_head)
8171da177e4SLinus Torvalds {
8181da177e4SLinus Torvalds 	struct pci_bus *b;
8191da177e4SLinus Torvalds 	struct pci_dev *dev;
8201da177e4SLinus Torvalds 
821c8adf9a3SRam Pai 	pbus_assign_resources_sorted(bus, add_head, fail_head);
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
8241da177e4SLinus Torvalds 		b = dev->subordinate;
8251da177e4SLinus Torvalds 		if (!b)
8261da177e4SLinus Torvalds 			continue;
8271da177e4SLinus Torvalds 
828c8adf9a3SRam Pai 		__pci_bus_assign_resources(b, add_head, fail_head);
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 		switch (dev->class >> 8) {
8311da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
8326841ec68SYinghai Lu 			if (!pci_is_enabled(dev))
8331da177e4SLinus Torvalds 				pci_setup_bridge(b);
8341da177e4SLinus Torvalds 			break;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
8371da177e4SLinus Torvalds 			pci_setup_cardbus(b);
8381da177e4SLinus Torvalds 			break;
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 		default:
84180ccba11SBjorn Helgaas 			dev_info(&dev->dev, "not setting up bridge for bus "
84280ccba11SBjorn Helgaas 				 "%04x:%02x\n", pci_domain_nr(b), b->number);
8431da177e4SLinus Torvalds 			break;
8441da177e4SLinus Torvalds 		}
8451da177e4SLinus Torvalds 	}
8461da177e4SLinus Torvalds }
847568ddef8SYinghai Lu 
848568ddef8SYinghai Lu void __ref pci_bus_assign_resources(const struct pci_bus *bus)
849568ddef8SYinghai Lu {
850c8adf9a3SRam Pai 	__pci_bus_assign_resources(bus, NULL, NULL);
851568ddef8SYinghai Lu }
8521da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
8531da177e4SLinus Torvalds 
8546841ec68SYinghai Lu static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
8556841ec68SYinghai Lu 					 struct resource_list_x *fail_head)
8566841ec68SYinghai Lu {
8576841ec68SYinghai Lu 	struct pci_bus *b;
8586841ec68SYinghai Lu 
8596841ec68SYinghai Lu 	pdev_assign_resources_sorted((struct pci_dev *)bridge, fail_head);
8606841ec68SYinghai Lu 
8616841ec68SYinghai Lu 	b = bridge->subordinate;
8626841ec68SYinghai Lu 	if (!b)
8636841ec68SYinghai Lu 		return;
8646841ec68SYinghai Lu 
865c8adf9a3SRam Pai 	__pci_bus_assign_resources(b, NULL, fail_head);
8666841ec68SYinghai Lu 
8676841ec68SYinghai Lu 	switch (bridge->class >> 8) {
8686841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_PCI:
8696841ec68SYinghai Lu 		pci_setup_bridge(b);
8706841ec68SYinghai Lu 		break;
8716841ec68SYinghai Lu 
8726841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_CARDBUS:
8736841ec68SYinghai Lu 		pci_setup_cardbus(b);
8746841ec68SYinghai Lu 		break;
8756841ec68SYinghai Lu 
8766841ec68SYinghai Lu 	default:
8776841ec68SYinghai Lu 		dev_info(&bridge->dev, "not setting up bridge for bus "
8786841ec68SYinghai Lu 			 "%04x:%02x\n", pci_domain_nr(b), b->number);
8796841ec68SYinghai Lu 		break;
8806841ec68SYinghai Lu 	}
8816841ec68SYinghai Lu }
8825009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus,
8835009b460SYinghai Lu 					  unsigned long type)
8845009b460SYinghai Lu {
8855009b460SYinghai Lu 	int idx;
8865009b460SYinghai Lu 	bool changed = false;
8875009b460SYinghai Lu 	struct pci_dev *dev;
8885009b460SYinghai Lu 	struct resource *r;
8895009b460SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
8905009b460SYinghai Lu 				  IORESOURCE_PREFETCH;
8915009b460SYinghai Lu 
8925009b460SYinghai Lu 	dev = bus->self;
8935009b460SYinghai Lu 	for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
8945009b460SYinghai Lu 	     idx++) {
8955009b460SYinghai Lu 		r = &dev->resource[idx];
8965009b460SYinghai Lu 		if ((r->flags & type_mask) != type)
8975009b460SYinghai Lu 			continue;
8985009b460SYinghai Lu 		if (!r->parent)
8995009b460SYinghai Lu 			continue;
9005009b460SYinghai Lu 		/*
9015009b460SYinghai Lu 		 * if there are children under that, we should release them
9025009b460SYinghai Lu 		 *  all
9035009b460SYinghai Lu 		 */
9045009b460SYinghai Lu 		release_child_resources(r);
9055009b460SYinghai Lu 		if (!release_resource(r)) {
9065009b460SYinghai Lu 			dev_printk(KERN_DEBUG, &dev->dev,
9075009b460SYinghai Lu 				 "resource %d %pR released\n", idx, r);
9085009b460SYinghai Lu 			/* keep the old size */
9095009b460SYinghai Lu 			r->end = resource_size(r) - 1;
9105009b460SYinghai Lu 			r->start = 0;
9115009b460SYinghai Lu 			r->flags = 0;
9125009b460SYinghai Lu 			changed = true;
9135009b460SYinghai Lu 		}
9145009b460SYinghai Lu 	}
9155009b460SYinghai Lu 
9165009b460SYinghai Lu 	if (changed) {
9175009b460SYinghai Lu 		/* avoiding touch the one without PREF */
9185009b460SYinghai Lu 		if (type & IORESOURCE_PREFETCH)
9195009b460SYinghai Lu 			type = IORESOURCE_PREFETCH;
9205009b460SYinghai Lu 		__pci_setup_bridge(bus, type);
9215009b460SYinghai Lu 	}
9225009b460SYinghai Lu }
9235009b460SYinghai Lu 
9245009b460SYinghai Lu enum release_type {
9255009b460SYinghai Lu 	leaf_only,
9265009b460SYinghai Lu 	whole_subtree,
9275009b460SYinghai Lu };
9285009b460SYinghai Lu /*
9295009b460SYinghai Lu  * try to release pci bridge resources that is from leaf bridge,
9305009b460SYinghai Lu  * so we can allocate big new one later
9315009b460SYinghai Lu  */
9325009b460SYinghai Lu static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
9335009b460SYinghai Lu 						   unsigned long type,
9345009b460SYinghai Lu 						   enum release_type rel_type)
9355009b460SYinghai Lu {
9365009b460SYinghai Lu 	struct pci_dev *dev;
9375009b460SYinghai Lu 	bool is_leaf_bridge = true;
9385009b460SYinghai Lu 
9395009b460SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
9405009b460SYinghai Lu 		struct pci_bus *b = dev->subordinate;
9415009b460SYinghai Lu 		if (!b)
9425009b460SYinghai Lu 			continue;
9435009b460SYinghai Lu 
9445009b460SYinghai Lu 		is_leaf_bridge = false;
9455009b460SYinghai Lu 
9465009b460SYinghai Lu 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
9475009b460SYinghai Lu 			continue;
9485009b460SYinghai Lu 
9495009b460SYinghai Lu 		if (rel_type == whole_subtree)
9505009b460SYinghai Lu 			pci_bus_release_bridge_resources(b, type,
9515009b460SYinghai Lu 						 whole_subtree);
9525009b460SYinghai Lu 	}
9535009b460SYinghai Lu 
9545009b460SYinghai Lu 	if (pci_is_root_bus(bus))
9555009b460SYinghai Lu 		return;
9565009b460SYinghai Lu 
9575009b460SYinghai Lu 	if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
9585009b460SYinghai Lu 		return;
9595009b460SYinghai Lu 
9605009b460SYinghai Lu 	if ((rel_type == whole_subtree) || is_leaf_bridge)
9615009b460SYinghai Lu 		pci_bridge_release_resources(bus, type);
9625009b460SYinghai Lu }
9635009b460SYinghai Lu 
96476fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
96576fbc263SYinghai Lu {
96689a74eccSBjorn Helgaas 	struct resource *res;
96776fbc263SYinghai Lu 	int i;
96876fbc263SYinghai Lu 
96989a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
9707c9342b8SYinghai Lu 		if (!res || !res->end || !res->flags)
97176fbc263SYinghai Lu                         continue;
97276fbc263SYinghai Lu 
973c7dabef8SBjorn Helgaas 		dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
97476fbc263SYinghai Lu         }
97576fbc263SYinghai Lu }
97676fbc263SYinghai Lu 
97776fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
97876fbc263SYinghai Lu {
97976fbc263SYinghai Lu 	struct pci_bus *b;
98076fbc263SYinghai Lu 	struct pci_dev *dev;
98176fbc263SYinghai Lu 
98276fbc263SYinghai Lu 
98376fbc263SYinghai Lu 	pci_bus_dump_res(bus);
98476fbc263SYinghai Lu 
98576fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
98676fbc263SYinghai Lu 		b = dev->subordinate;
98776fbc263SYinghai Lu 		if (!b)
98876fbc263SYinghai Lu 			continue;
98976fbc263SYinghai Lu 
99076fbc263SYinghai Lu 		pci_bus_dump_resources(b);
99176fbc263SYinghai Lu 	}
99276fbc263SYinghai Lu }
99376fbc263SYinghai Lu 
994*da7822e5SYinghai Lu static int __init pci_bus_get_depth(struct pci_bus *bus)
995*da7822e5SYinghai Lu {
996*da7822e5SYinghai Lu 	int depth = 0;
997*da7822e5SYinghai Lu 	struct pci_dev *dev;
998*da7822e5SYinghai Lu 
999*da7822e5SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
1000*da7822e5SYinghai Lu 		int ret;
1001*da7822e5SYinghai Lu 		struct pci_bus *b = dev->subordinate;
1002*da7822e5SYinghai Lu 		if (!b)
1003*da7822e5SYinghai Lu 			continue;
1004*da7822e5SYinghai Lu 
1005*da7822e5SYinghai Lu 		ret = pci_bus_get_depth(b);
1006*da7822e5SYinghai Lu 		if (ret + 1 > depth)
1007*da7822e5SYinghai Lu 			depth = ret + 1;
1008*da7822e5SYinghai Lu 	}
1009*da7822e5SYinghai Lu 
1010*da7822e5SYinghai Lu 	return depth;
1011*da7822e5SYinghai Lu }
1012*da7822e5SYinghai Lu static int __init pci_get_max_depth(void)
1013*da7822e5SYinghai Lu {
1014*da7822e5SYinghai Lu 	int depth = 0;
1015*da7822e5SYinghai Lu 	struct pci_bus *bus;
1016*da7822e5SYinghai Lu 
1017*da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node) {
1018*da7822e5SYinghai Lu 		int ret;
1019*da7822e5SYinghai Lu 
1020*da7822e5SYinghai Lu 		ret = pci_bus_get_depth(bus);
1021*da7822e5SYinghai Lu 		if (ret > depth)
1022*da7822e5SYinghai Lu 			depth = ret;
1023*da7822e5SYinghai Lu 	}
1024*da7822e5SYinghai Lu 
1025*da7822e5SYinghai Lu 	return depth;
1026*da7822e5SYinghai Lu }
1027*da7822e5SYinghai Lu 
1028*da7822e5SYinghai Lu /*
1029*da7822e5SYinghai Lu  * first try will not touch pci bridge res
1030*da7822e5SYinghai Lu  * second  and later try will clear small leaf bridge res
1031*da7822e5SYinghai Lu  * will stop till to the max  deepth if can not find good one
1032*da7822e5SYinghai Lu  */
10331da177e4SLinus Torvalds void __init
10341da177e4SLinus Torvalds pci_assign_unassigned_resources(void)
10351da177e4SLinus Torvalds {
10361da177e4SLinus Torvalds 	struct pci_bus *bus;
1037c8adf9a3SRam Pai 	struct resource_list_x add_list; /* list of resources that
1038c8adf9a3SRam Pai 					want additional resources */
1039*da7822e5SYinghai Lu 	int tried_times = 0;
1040*da7822e5SYinghai Lu 	enum release_type rel_type = leaf_only;
1041*da7822e5SYinghai Lu 	struct resource_list_x head, *list;
1042*da7822e5SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1043*da7822e5SYinghai Lu 				  IORESOURCE_PREFETCH;
1044*da7822e5SYinghai Lu 	unsigned long failed_type;
1045*da7822e5SYinghai Lu 	int max_depth = pci_get_max_depth();
1046*da7822e5SYinghai Lu 	int pci_try_num;
1047*da7822e5SYinghai Lu 
1048*da7822e5SYinghai Lu 
1049*da7822e5SYinghai Lu 	head.next = NULL;
1050c8adf9a3SRam Pai 	add_list.next = NULL;
1051*da7822e5SYinghai Lu 
1052*da7822e5SYinghai Lu 	pci_try_num = max_depth + 1;
1053*da7822e5SYinghai Lu 	printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1054*da7822e5SYinghai Lu 		 max_depth, pci_try_num);
1055*da7822e5SYinghai Lu 
1056*da7822e5SYinghai Lu again:
10571da177e4SLinus Torvalds 	/* Depth first, calculate sizes and alignments of all
10581da177e4SLinus Torvalds 	   subordinate buses. */
1059*da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
1060c8adf9a3SRam Pai 		__pci_bus_size_bridges(bus, &add_list);
1061c8adf9a3SRam Pai 
10621da177e4SLinus Torvalds 	/* Depth last, allocate resources and update the hardware. */
1063*da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
1064*da7822e5SYinghai Lu 		__pci_bus_assign_resources(bus, &add_list, &head);
1065c8adf9a3SRam Pai 	BUG_ON(add_list.next);
1066*da7822e5SYinghai Lu 	tried_times++;
1067*da7822e5SYinghai Lu 
1068*da7822e5SYinghai Lu 	/* any device complain? */
1069*da7822e5SYinghai Lu 	if (!head.next)
1070*da7822e5SYinghai Lu 		goto enable_and_dump;
1071*da7822e5SYinghai Lu 	failed_type = 0;
1072*da7822e5SYinghai Lu 	for (list = head.next; list;) {
1073*da7822e5SYinghai Lu 		failed_type |= list->flags;
1074*da7822e5SYinghai Lu 		list = list->next;
1075*da7822e5SYinghai Lu 	}
1076*da7822e5SYinghai Lu 	/*
1077*da7822e5SYinghai Lu 	 * io port are tight, don't try extra
1078*da7822e5SYinghai Lu 	 * or if reach the limit, don't want to try more
1079*da7822e5SYinghai Lu 	 */
1080*da7822e5SYinghai Lu 	failed_type &= type_mask;
1081*da7822e5SYinghai Lu 	if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
1082*da7822e5SYinghai Lu 		free_list(resource_list_x, &head);
1083*da7822e5SYinghai Lu 		goto enable_and_dump;
1084*da7822e5SYinghai Lu 	}
1085*da7822e5SYinghai Lu 
1086*da7822e5SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1087*da7822e5SYinghai Lu 			 tried_times + 1);
1088*da7822e5SYinghai Lu 
1089*da7822e5SYinghai Lu 	/* third times and later will not check if it is leaf */
1090*da7822e5SYinghai Lu 	if ((tried_times + 1) > 2)
1091*da7822e5SYinghai Lu 		rel_type = whole_subtree;
1092*da7822e5SYinghai Lu 
1093*da7822e5SYinghai Lu 	/*
1094*da7822e5SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
1095*da7822e5SYinghai Lu 	 * child device under that bridge
1096*da7822e5SYinghai Lu 	 */
1097*da7822e5SYinghai Lu 	for (list = head.next; list;) {
1098*da7822e5SYinghai Lu 		bus = list->dev->bus;
1099*da7822e5SYinghai Lu 		pci_bus_release_bridge_resources(bus, list->flags & type_mask,
1100*da7822e5SYinghai Lu 						  rel_type);
1101*da7822e5SYinghai Lu 		list = list->next;
1102*da7822e5SYinghai Lu 	}
1103*da7822e5SYinghai Lu 	/* restore size and flags */
1104*da7822e5SYinghai Lu 	for (list = head.next; list;) {
1105*da7822e5SYinghai Lu 		struct resource *res = list->res;
1106*da7822e5SYinghai Lu 
1107*da7822e5SYinghai Lu 		res->start = list->start;
1108*da7822e5SYinghai Lu 		res->end = list->end;
1109*da7822e5SYinghai Lu 		res->flags = list->flags;
1110*da7822e5SYinghai Lu 		if (list->dev->subordinate)
1111*da7822e5SYinghai Lu 			res->flags = 0;
1112*da7822e5SYinghai Lu 
1113*da7822e5SYinghai Lu 		list = list->next;
1114*da7822e5SYinghai Lu 	}
1115*da7822e5SYinghai Lu 	free_list(resource_list_x, &head);
1116*da7822e5SYinghai Lu 
1117*da7822e5SYinghai Lu 	goto again;
1118*da7822e5SYinghai Lu 
1119*da7822e5SYinghai Lu enable_and_dump:
1120*da7822e5SYinghai Lu 	/* Depth last, update the hardware. */
1121*da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
1122*da7822e5SYinghai Lu 		pci_enable_bridges(bus);
112376fbc263SYinghai Lu 
112476fbc263SYinghai Lu 	/* dump the resource on buses */
1125*da7822e5SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
112676fbc263SYinghai Lu 		pci_bus_dump_resources(bus);
112776fbc263SYinghai Lu }
11286841ec68SYinghai Lu 
11296841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
11306841ec68SYinghai Lu {
11316841ec68SYinghai Lu 	struct pci_bus *parent = bridge->subordinate;
113232180e40SYinghai Lu 	int tried_times = 0;
113332180e40SYinghai Lu 	struct resource_list_x head, *list;
11346841ec68SYinghai Lu 	int retval;
113532180e40SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
113632180e40SYinghai Lu 				  IORESOURCE_PREFETCH;
11376841ec68SYinghai Lu 
113832180e40SYinghai Lu 	head.next = NULL;
113932180e40SYinghai Lu 
114032180e40SYinghai Lu again:
11416841ec68SYinghai Lu 	pci_bus_size_bridges(parent);
114232180e40SYinghai Lu 	__pci_bridge_assign_resources(bridge, &head);
114332180e40SYinghai Lu 
114432180e40SYinghai Lu 	tried_times++;
114532180e40SYinghai Lu 
114632180e40SYinghai Lu 	if (!head.next)
11473f579c34SYinghai Lu 		goto enable_all;
114832180e40SYinghai Lu 
114932180e40SYinghai Lu 	if (tried_times >= 2) {
115032180e40SYinghai Lu 		/* still fail, don't need to try more */
1151094732a5SRam Pai 		free_list(resource_list_x, &head);
11523f579c34SYinghai Lu 		goto enable_all;
115332180e40SYinghai Lu 	}
115432180e40SYinghai Lu 
115532180e40SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
115632180e40SYinghai Lu 			 tried_times + 1);
115732180e40SYinghai Lu 
115832180e40SYinghai Lu 	/*
115932180e40SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
116032180e40SYinghai Lu 	 * child device under that bridge
116132180e40SYinghai Lu 	 */
116232180e40SYinghai Lu 	for (list = head.next; list;) {
116332180e40SYinghai Lu 		struct pci_bus *bus = list->dev->bus;
116432180e40SYinghai Lu 		unsigned long flags = list->flags;
116532180e40SYinghai Lu 
116632180e40SYinghai Lu 		pci_bus_release_bridge_resources(bus, flags & type_mask,
116732180e40SYinghai Lu 						 whole_subtree);
116832180e40SYinghai Lu 		list = list->next;
116932180e40SYinghai Lu 	}
117032180e40SYinghai Lu 	/* restore size and flags */
117132180e40SYinghai Lu 	for (list = head.next; list;) {
117232180e40SYinghai Lu 		struct resource *res = list->res;
117332180e40SYinghai Lu 
117432180e40SYinghai Lu 		res->start = list->start;
117532180e40SYinghai Lu 		res->end = list->end;
117632180e40SYinghai Lu 		res->flags = list->flags;
117732180e40SYinghai Lu 		if (list->dev->subordinate)
117832180e40SYinghai Lu 			res->flags = 0;
117932180e40SYinghai Lu 
118032180e40SYinghai Lu 		list = list->next;
118132180e40SYinghai Lu 	}
1182094732a5SRam Pai 	free_list(resource_list_x, &head);
118332180e40SYinghai Lu 
118432180e40SYinghai Lu 	goto again;
11853f579c34SYinghai Lu 
11863f579c34SYinghai Lu enable_all:
11873f579c34SYinghai Lu 	retval = pci_reenable_device(bridge);
11883f579c34SYinghai Lu 	pci_set_master(bridge);
11893f579c34SYinghai Lu 	pci_enable_bridges(parent);
11906841ec68SYinghai Lu }
11916841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
1192