xref: /openbmc/linux/drivers/pci/setup-bus.c (revision 094732a520caae81ae1532af29da82a4fa953472)
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;
36568ddef8SYinghai Lu 	unsigned long flags;
37568ddef8SYinghai Lu };
38568ddef8SYinghai Lu 
39*094732a5SRam Pai #define free_list(type, head) do {                      \
40*094732a5SRam Pai 	struct type *list, *tmp;			\
41*094732a5SRam Pai 	for (list = (head)->next; list;) {		\
42*094732a5SRam Pai 		tmp = list;				\
43*094732a5SRam Pai 		list = list->next;			\
44*094732a5SRam Pai 		kfree(tmp);				\
45*094732a5SRam Pai 	}						\
46*094732a5SRam Pai 	(head)->next = NULL;				\
47*094732a5SRam Pai } while (0)
48*094732a5SRam Pai 
49568ddef8SYinghai Lu static void add_to_failed_list(struct resource_list_x *head,
50568ddef8SYinghai Lu 				 struct pci_dev *dev, struct resource *res)
51568ddef8SYinghai Lu {
52568ddef8SYinghai Lu 	struct resource_list_x *list = head;
53568ddef8SYinghai Lu 	struct resource_list_x *ln = list->next;
54568ddef8SYinghai Lu 	struct resource_list_x *tmp;
55568ddef8SYinghai Lu 
56568ddef8SYinghai Lu 	tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
57568ddef8SYinghai Lu 	if (!tmp) {
58568ddef8SYinghai Lu 		pr_warning("add_to_failed_list: kmalloc() failed!\n");
59568ddef8SYinghai Lu 		return;
60568ddef8SYinghai Lu 	}
61568ddef8SYinghai Lu 
62568ddef8SYinghai Lu 	tmp->next = ln;
63568ddef8SYinghai Lu 	tmp->res = res;
64568ddef8SYinghai Lu 	tmp->dev = dev;
65568ddef8SYinghai Lu 	tmp->start = res->start;
66568ddef8SYinghai Lu 	tmp->end = res->end;
67568ddef8SYinghai Lu 	tmp->flags = res->flags;
68568ddef8SYinghai Lu 	list->next = tmp;
69568ddef8SYinghai Lu }
70568ddef8SYinghai Lu 
716841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev,
726841ec68SYinghai Lu 				 struct resource_list *head)
731da177e4SLinus Torvalds {
741da177e4SLinus Torvalds 	u16 class = dev->class >> 8;
751da177e4SLinus Torvalds 
769bded00bSKenji Kaneshige 	/* Don't touch classless devices or host bridges or ioapics.  */
776841ec68SYinghai Lu 	if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
786841ec68SYinghai Lu 		return;
791da177e4SLinus Torvalds 
809bded00bSKenji Kaneshige 	/* Don't touch ioapic devices already enabled by firmware */
8123186279SSatoru Takeuchi 	if (class == PCI_CLASS_SYSTEM_PIC) {
829bded00bSKenji Kaneshige 		u16 command;
839bded00bSKenji Kaneshige 		pci_read_config_word(dev, PCI_COMMAND, &command);
849bded00bSKenji Kaneshige 		if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
856841ec68SYinghai Lu 			return;
8623186279SSatoru Takeuchi 	}
8723186279SSatoru Takeuchi 
886841ec68SYinghai Lu 	pdev_sort_resources(dev, head);
891da177e4SLinus Torvalds }
901da177e4SLinus Torvalds 
916841ec68SYinghai Lu static void __assign_resources_sorted(struct resource_list *head,
926841ec68SYinghai Lu 				 struct resource_list_x *fail_head)
936841ec68SYinghai Lu {
946841ec68SYinghai Lu 	struct resource *res;
956841ec68SYinghai Lu 	struct resource_list *list, *tmp;
966841ec68SYinghai Lu 	int idx;
976841ec68SYinghai Lu 
986841ec68SYinghai Lu 	for (list = head->next; list;) {
991da177e4SLinus Torvalds 		res = list->res;
1001da177e4SLinus Torvalds 		idx = res - &list->dev->resource[0];
1019a928660SYinghai Lu 
102542df5deSRajesh Shah 		if (pci_assign_resource(list->dev, idx)) {
1039a928660SYinghai Lu 			if (fail_head && !pci_is_root_bus(list->dev->bus)) {
1049a928660SYinghai Lu 				/*
1059a928660SYinghai Lu 				 * if the failed res is for ROM BAR, and it will
1069a928660SYinghai Lu 				 * be enabled later, don't add it to the list
1079a928660SYinghai Lu 				 */
1089a928660SYinghai Lu 				if (!((idx == PCI_ROM_RESOURCE) &&
1099a928660SYinghai Lu 				      (!(res->flags & IORESOURCE_ROM_ENABLE))))
110568ddef8SYinghai Lu 					add_to_failed_list(fail_head, list->dev, res);
1119a928660SYinghai Lu 			}
112542df5deSRajesh Shah 			res->start = 0;
113960b8466SIvan Kokshaysky 			res->end = 0;
114542df5deSRajesh Shah 			res->flags = 0;
115542df5deSRajesh Shah 		}
1161da177e4SLinus Torvalds 		tmp = list;
1171da177e4SLinus Torvalds 		list = list->next;
1181da177e4SLinus Torvalds 		kfree(tmp);
1191da177e4SLinus Torvalds 	}
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
1226841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev,
1236841ec68SYinghai Lu 				 struct resource_list_x *fail_head)
1246841ec68SYinghai Lu {
1256841ec68SYinghai Lu 	struct resource_list head;
1266841ec68SYinghai Lu 
1276841ec68SYinghai Lu 	head.next = NULL;
1286841ec68SYinghai Lu 	__dev_sort_resources(dev, &head);
1296841ec68SYinghai Lu 	__assign_resources_sorted(&head, fail_head);
1306841ec68SYinghai Lu 
1316841ec68SYinghai Lu }
1326841ec68SYinghai Lu 
1336841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus,
1346841ec68SYinghai Lu 					 struct resource_list_x *fail_head)
1356841ec68SYinghai Lu {
1366841ec68SYinghai Lu 	struct pci_dev *dev;
1376841ec68SYinghai Lu 	struct resource_list head;
1386841ec68SYinghai Lu 
1396841ec68SYinghai Lu 	head.next = NULL;
1406841ec68SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
1416841ec68SYinghai Lu 		__dev_sort_resources(dev, &head);
1426841ec68SYinghai Lu 
1436841ec68SYinghai Lu 	__assign_resources_sorted(&head, fail_head);
1446841ec68SYinghai Lu }
1456841ec68SYinghai Lu 
146b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus)
1471da177e4SLinus Torvalds {
1481da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
149c7dabef8SBjorn Helgaas 	struct resource *res;
1501da177e4SLinus Torvalds 	struct pci_bus_region region;
1511da177e4SLinus Torvalds 
152865df576SBjorn Helgaas 	dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
153865df576SBjorn Helgaas 		 bus->secondary, bus->subordinate);
1541da177e4SLinus Torvalds 
155c7dabef8SBjorn Helgaas 	res = bus->resource[0];
156c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
157c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
1581da177e4SLinus Torvalds 		/*
1591da177e4SLinus Torvalds 		 * The IO resource is allocated a range twice as large as it
1601da177e4SLinus Torvalds 		 * would normally need.  This allows us to set both IO regs.
1611da177e4SLinus Torvalds 		 */
162c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
1631da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
1641da177e4SLinus Torvalds 					region.start);
1651da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
1661da177e4SLinus Torvalds 					region.end);
1671da177e4SLinus Torvalds 	}
1681da177e4SLinus Torvalds 
169c7dabef8SBjorn Helgaas 	res = bus->resource[1];
170c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
171c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
172c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
1731da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
1741da177e4SLinus Torvalds 					region.start);
1751da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
1761da177e4SLinus Torvalds 					region.end);
1771da177e4SLinus Torvalds 	}
1781da177e4SLinus Torvalds 
179c7dabef8SBjorn Helgaas 	res = bus->resource[2];
180c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
181c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
182c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
1831da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
1841da177e4SLinus Torvalds 					region.start);
1851da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
1861da177e4SLinus Torvalds 					region.end);
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds 
189c7dabef8SBjorn Helgaas 	res = bus->resource[3];
190c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
191c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
192c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
1931da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
1941da177e4SLinus Torvalds 					region.start);
1951da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
1961da177e4SLinus Torvalds 					region.end);
1971da177e4SLinus Torvalds 	}
1981da177e4SLinus Torvalds }
199b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected.
2021da177e4SLinus Torvalds    PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
2031da177e4SLinus Torvalds    requires that if there is no I/O ports or memory behind the
2041da177e4SLinus Torvalds    bridge, corresponding range must be turned off by writing base
2051da177e4SLinus Torvalds    value greater than limit to the bridge's base/limit registers.
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds    Note: care must be taken when updating I/O base/limit registers
2081da177e4SLinus Torvalds    of bridges which support 32-bit I/O. This update requires two
2091da177e4SLinus Torvalds    config space writes, so it's quite possible that an I/O window of
2101da177e4SLinus Torvalds    the bridge will have some undesirable address (e.g. 0) after the
2111da177e4SLinus Torvalds    first write. Ditto 64-bit prefetchable MMIO.  */
2127cc5997dSYinghai Lu static void pci_setup_bridge_io(struct pci_bus *bus)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
215c7dabef8SBjorn Helgaas 	struct resource *res;
2161da177e4SLinus Torvalds 	struct pci_bus_region region;
2177cc5997dSYinghai Lu 	u32 l, io_upper16;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	/* Set up the top and bottom of the PCI I/O segment for this bus. */
220c7dabef8SBjorn Helgaas 	res = bus->resource[0];
221c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
222c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
2231da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_IO_BASE, &l);
2241da177e4SLinus Torvalds 		l &= 0xffff0000;
2251da177e4SLinus Torvalds 		l |= (region.start >> 8) & 0x00f0;
2261da177e4SLinus Torvalds 		l |= region.end & 0xf000;
2271da177e4SLinus Torvalds 		/* Set up upper 16 bits of I/O base/limit. */
2281da177e4SLinus Torvalds 		io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
229c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
2307cc5997dSYinghai Lu 	} else {
2311da177e4SLinus Torvalds 		/* Clear upper 16 bits of I/O base/limit. */
2321da177e4SLinus Torvalds 		io_upper16 = 0;
2331da177e4SLinus Torvalds 		l = 0x00f0;
234c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window [io  disabled]\n");
2351da177e4SLinus Torvalds 	}
2361da177e4SLinus Torvalds 	/* Temporarily disable the I/O range before updating PCI_IO_BASE. */
2371da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
2381da177e4SLinus Torvalds 	/* Update lower 16 bits of I/O base/limit. */
2391da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE, l);
2401da177e4SLinus Torvalds 	/* Update upper 16 bits of I/O base/limit. */
2411da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
2427cc5997dSYinghai Lu }
2431da177e4SLinus Torvalds 
2447cc5997dSYinghai Lu static void pci_setup_bridge_mmio(struct pci_bus *bus)
2457cc5997dSYinghai Lu {
2467cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
2477cc5997dSYinghai Lu 	struct resource *res;
2487cc5997dSYinghai Lu 	struct pci_bus_region region;
2497cc5997dSYinghai Lu 	u32 l;
2507cc5997dSYinghai Lu 
2517cc5997dSYinghai Lu 	/* Set up the top and bottom of the PCI Memory segment for this bus. */
252c7dabef8SBjorn Helgaas 	res = bus->resource[1];
253c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
254c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
2551da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
2561da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
257c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
2587cc5997dSYinghai Lu 	} else {
2591da177e4SLinus Torvalds 		l = 0x0000fff0;
260c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window [mem disabled]\n");
2611da177e4SLinus Torvalds 	}
2621da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
2637cc5997dSYinghai Lu }
2647cc5997dSYinghai Lu 
2657cc5997dSYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
2667cc5997dSYinghai Lu {
2677cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
2687cc5997dSYinghai Lu 	struct resource *res;
2697cc5997dSYinghai Lu 	struct pci_bus_region region;
2707cc5997dSYinghai Lu 	u32 l, bu, lu;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	/* Clear out the upper 32 bits of PREF limit.
2731da177e4SLinus Torvalds 	   If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
2741da177e4SLinus Torvalds 	   disables PREF range, which is ok. */
2751da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	/* Set up PREF base/limit. */
278c40a22e0SBenjamin Herrenschmidt 	bu = lu = 0;
279c7dabef8SBjorn Helgaas 	res = bus->resource[2];
280c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
281c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_PREFETCH) {
2821da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
2831da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
284c7dabef8SBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64) {
28513d36c24SAndrew Morton 			bu = upper_32_bits(region.start);
28613d36c24SAndrew Morton 			lu = upper_32_bits(region.end);
2871f82de10SYinghai Lu 		}
288c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
2897cc5997dSYinghai Lu 	} else {
2901da177e4SLinus Torvalds 		l = 0x0000fff0;
291c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window [mem pref disabled]\n");
2921da177e4SLinus Torvalds 	}
2931da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
2941da177e4SLinus Torvalds 
295c40a22e0SBenjamin Herrenschmidt 	/* Set the upper 32 bits of PREF base & limit. */
296c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
297c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
2987cc5997dSYinghai Lu }
2997cc5997dSYinghai Lu 
3007cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
3017cc5997dSYinghai Lu {
3027cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
3037cc5997dSYinghai Lu 
3047cc5997dSYinghai Lu 	dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
3057cc5997dSYinghai Lu 		 bus->secondary, bus->subordinate);
3067cc5997dSYinghai Lu 
3077cc5997dSYinghai Lu 	if (type & IORESOURCE_IO)
3087cc5997dSYinghai Lu 		pci_setup_bridge_io(bus);
3097cc5997dSYinghai Lu 
3107cc5997dSYinghai Lu 	if (type & IORESOURCE_MEM)
3117cc5997dSYinghai Lu 		pci_setup_bridge_mmio(bus);
3127cc5997dSYinghai Lu 
3137cc5997dSYinghai Lu 	if (type & IORESOURCE_PREFETCH)
3147cc5997dSYinghai Lu 		pci_setup_bridge_mmio_pref(bus);
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
3197cc5997dSYinghai Lu static void pci_setup_bridge(struct pci_bus *bus)
3207cc5997dSYinghai Lu {
3217cc5997dSYinghai Lu 	unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
3227cc5997dSYinghai Lu 				  IORESOURCE_PREFETCH;
3237cc5997dSYinghai Lu 
3247cc5997dSYinghai Lu 	__pci_setup_bridge(bus, type);
3257cc5997dSYinghai Lu }
3267cc5997dSYinghai Lu 
3271da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and
3281da177e4SLinus Torvalds    prefetchable memory ranges. If not, the respective
3291da177e4SLinus Torvalds    base/limit registers must be read-only and read as 0. */
33096bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	u16 io;
3331da177e4SLinus Torvalds 	u32 pmem;
3341da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
3351da177e4SLinus Torvalds 	struct resource *b_res;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
3381da177e4SLinus Torvalds 	b_res[1].flags |= IORESOURCE_MEM;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_IO_BASE, &io);
3411da177e4SLinus Torvalds 	if (!io) {
3421da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
3431da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_IO_BASE, &io);
3441da177e4SLinus Torvalds  		pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
3451da177e4SLinus Torvalds  	}
3461da177e4SLinus Torvalds  	if (io)
3471da177e4SLinus Torvalds 		b_res[0].flags |= IORESOURCE_IO;
3481da177e4SLinus Torvalds 	/*  DECchip 21050 pass 2 errata: the bridge may miss an address
3491da177e4SLinus Torvalds 	    disconnect boundary by one PCI data phase.
3501da177e4SLinus Torvalds 	    Workaround: do not use prefetching on this device. */
3511da177e4SLinus Torvalds 	if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
3521da177e4SLinus Torvalds 		return;
3531da177e4SLinus Torvalds 	pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
3541da177e4SLinus Torvalds 	if (!pmem) {
3551da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
3561da177e4SLinus Torvalds 					       0xfff0fff0);
3571da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
3581da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
3591da177e4SLinus Torvalds 	}
3601f82de10SYinghai Lu 	if (pmem) {
3611da177e4SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
36299586105SYinghai Lu 		if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
36399586105SYinghai Lu 		    PCI_PREF_RANGE_TYPE_64) {
3641f82de10SYinghai Lu 			b_res[2].flags |= IORESOURCE_MEM_64;
36599586105SYinghai Lu 			b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
36699586105SYinghai Lu 		}
3671f82de10SYinghai Lu 	}
3681f82de10SYinghai Lu 
3691f82de10SYinghai Lu 	/* double check if bridge does support 64 bit pref */
3701f82de10SYinghai Lu 	if (b_res[2].flags & IORESOURCE_MEM_64) {
3711f82de10SYinghai Lu 		u32 mem_base_hi, tmp;
3721f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
3731f82de10SYinghai Lu 					 &mem_base_hi);
3741f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
3751f82de10SYinghai Lu 					       0xffffffff);
3761f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
3771f82de10SYinghai Lu 		if (!tmp)
3781f82de10SYinghai Lu 			b_res[2].flags &= ~IORESOURCE_MEM_64;
3791f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
3801f82de10SYinghai Lu 				       mem_base_hi);
3811f82de10SYinghai Lu 	}
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds /* Helper function for sizing routines: find first available
3851da177e4SLinus Torvalds    bus resource of a given type. Note: we intentionally skip
3861da177e4SLinus Torvalds    the bus resources which have already been assigned (that is,
3871da177e4SLinus Torvalds    have non-NULL parent resource). */
38896bde06aSSam Ravnborg static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	int i;
3911da177e4SLinus Torvalds 	struct resource *r;
3921da177e4SLinus Torvalds 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
3931da177e4SLinus Torvalds 				  IORESOURCE_PREFETCH;
3941da177e4SLinus Torvalds 
39589a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, r, i) {
396299de034SIvan Kokshaysky 		if (r == &ioport_resource || r == &iomem_resource)
397299de034SIvan Kokshaysky 			continue;
39855a10984SJesse Barnes 		if (r && (r->flags & type_mask) == type && !r->parent)
3991da177e4SLinus Torvalds 			return r;
4001da177e4SLinus Torvalds 	}
4011da177e4SLinus Torvalds 	return NULL;
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
40413583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size,
40513583b16SRam Pai 		resource_size_t min_size,
40613583b16SRam Pai 		resource_size_t size1,
40713583b16SRam Pai 		resource_size_t old_size,
40813583b16SRam Pai 		resource_size_t align)
40913583b16SRam Pai {
41013583b16SRam Pai 	if (size < min_size)
41113583b16SRam Pai 		size = min_size;
41213583b16SRam Pai 	if (old_size == 1 )
41313583b16SRam Pai 		old_size = 0;
41413583b16SRam Pai 	/* To be fixed in 2.5: we should have sort of HAVE_ISA
41513583b16SRam Pai 	   flag in the struct pci_bus. */
41613583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
41713583b16SRam Pai 	size = (size & 0xff) + ((size & ~0xffUL) << 2);
41813583b16SRam Pai #endif
41913583b16SRam Pai 	size = ALIGN(size + size1, align);
42013583b16SRam Pai 	if (size < old_size)
42113583b16SRam Pai 		size = old_size;
42213583b16SRam Pai 	return size;
42313583b16SRam Pai }
42413583b16SRam Pai 
42513583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size,
42613583b16SRam Pai 		resource_size_t min_size,
42713583b16SRam Pai 		resource_size_t size1,
42813583b16SRam Pai 		resource_size_t old_size,
42913583b16SRam Pai 		resource_size_t align)
43013583b16SRam Pai {
43113583b16SRam Pai 	if (size < min_size)
43213583b16SRam Pai 		size = min_size;
43313583b16SRam Pai 	if (old_size == 1 )
43413583b16SRam Pai 		old_size = 0;
43513583b16SRam Pai 	if (size < old_size)
43613583b16SRam Pai 		size = old_size;
43713583b16SRam Pai 	size = ALIGN(size + size1, align);
43813583b16SRam Pai 	return size;
43913583b16SRam Pai }
44013583b16SRam Pai 
4411da177e4SLinus Torvalds /* Sizing the IO windows of the PCI-PCI bridge is trivial,
4421da177e4SLinus Torvalds    since these windows have 4K granularity and the IO ranges
4431da177e4SLinus Torvalds    of non-bridge PCI devices are limited to 256 bytes.
4441da177e4SLinus Torvalds    We must be careful with the ISA aliasing though. */
44528760489SEric W. Biederman static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	struct pci_dev *dev;
4481da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
44913583b16SRam Pai 	unsigned long size = 0, size1 = 0;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	if (!b_res)
4521da177e4SLinus Torvalds  		return;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
4551da177e4SLinus Torvalds 		int i;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
4581da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
4591da177e4SLinus Torvalds 			unsigned long r_size;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 			if (r->parent || !(r->flags & IORESOURCE_IO))
4621da177e4SLinus Torvalds 				continue;
463022edd86SZhao, Yu 			r_size = resource_size(r);
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 			if (r_size < 0x400)
4661da177e4SLinus Torvalds 				/* Might be re-aligned for ISA */
4671da177e4SLinus Torvalds 				size += r_size;
4681da177e4SLinus Torvalds 			else
4691da177e4SLinus Torvalds 				size1 += r_size;
4701da177e4SLinus Torvalds 		}
4711da177e4SLinus Torvalds 	}
47213583b16SRam Pai 	size = calculate_iosize(size, min_size, size1,
47313583b16SRam Pai 			resource_size(b_res), 4096);
4741da177e4SLinus Torvalds 	if (!size) {
475865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
476865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
477865df576SBjorn Helgaas 				 "%pR to [bus %02x-%02x] (unused)\n", b_res,
478865df576SBjorn Helgaas 				 bus->secondary, bus->subordinate);
4791da177e4SLinus Torvalds 		b_res->flags = 0;
4801da177e4SLinus Torvalds 		return;
4811da177e4SLinus Torvalds 	}
4821da177e4SLinus Torvalds 	/* Alignment of the IO window is always 4K */
4831da177e4SLinus Torvalds 	b_res->start = 4096;
4841da177e4SLinus Torvalds 	b_res->end = b_res->start + size - 1;
48588452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds /* Calculate the size of the bus and minimal alignment which
4891da177e4SLinus Torvalds    guarantees that all child resources fit in this size. */
49028760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
49128760489SEric W. Biederman 			 unsigned long type, resource_size_t min_size)
4921da177e4SLinus Torvalds {
4931da177e4SLinus Torvalds 	struct pci_dev *dev;
49413583b16SRam Pai 	resource_size_t min_align, align, size;
495c40a22e0SBenjamin Herrenschmidt 	resource_size_t aligns[12];	/* Alignments from 1Mb to 2Gb */
4961da177e4SLinus Torvalds 	int order, max_order;
4971da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, type);
4981f82de10SYinghai Lu 	unsigned int mem64_mask = 0;
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	if (!b_res)
5011da177e4SLinus Torvalds 		return 0;
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
5041da177e4SLinus Torvalds 	max_order = 0;
5051da177e4SLinus Torvalds 	size = 0;
5061da177e4SLinus Torvalds 
5071f82de10SYinghai Lu 	mem64_mask = b_res->flags & IORESOURCE_MEM_64;
5081f82de10SYinghai Lu 	b_res->flags &= ~IORESOURCE_MEM_64;
5091f82de10SYinghai Lu 
5101da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
5111da177e4SLinus Torvalds 		int i;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
5141da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
515c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 			if (r->parent || (r->flags & mask) != type)
5181da177e4SLinus Torvalds 				continue;
519022edd86SZhao, Yu 			r_size = resource_size(r);
5201da177e4SLinus Torvalds 			/* For bridges size != alignment */
5216faf17f6SChris Wright 			align = pci_resource_alignment(dev, r);
5221da177e4SLinus Torvalds 			order = __ffs(align) - 20;
5231da177e4SLinus Torvalds 			if (order > 11) {
524865df576SBjorn Helgaas 				dev_warn(&dev->dev, "disabling BAR %d: %pR "
525865df576SBjorn Helgaas 					 "(bad alignment %#llx)\n", i, r,
526865df576SBjorn Helgaas 					 (unsigned long long) align);
5271da177e4SLinus Torvalds 				r->flags = 0;
5281da177e4SLinus Torvalds 				continue;
5291da177e4SLinus Torvalds 			}
5301da177e4SLinus Torvalds 			size += r_size;
5311da177e4SLinus Torvalds 			if (order < 0)
5321da177e4SLinus Torvalds 				order = 0;
5331da177e4SLinus Torvalds 			/* Exclude ranges with size > align from
5341da177e4SLinus Torvalds 			   calculation of the alignment. */
5351da177e4SLinus Torvalds 			if (r_size == align)
5361da177e4SLinus Torvalds 				aligns[order] += align;
5371da177e4SLinus Torvalds 			if (order > max_order)
5381da177e4SLinus Torvalds 				max_order = order;
5391f82de10SYinghai Lu 			mem64_mask &= r->flags & IORESOURCE_MEM_64;
5401da177e4SLinus Torvalds 		}
5411da177e4SLinus Torvalds 	}
5421da177e4SLinus Torvalds 	align = 0;
5431da177e4SLinus Torvalds 	min_align = 0;
5441da177e4SLinus Torvalds 	for (order = 0; order <= max_order; order++) {
5458308c54dSJeremy Fitzhardinge 		resource_size_t align1 = 1;
5468308c54dSJeremy Fitzhardinge 
5478308c54dSJeremy Fitzhardinge 		align1 <<= (order + 20);
5488308c54dSJeremy Fitzhardinge 
5491da177e4SLinus Torvalds 		if (!align)
5501da177e4SLinus Torvalds 			min_align = align1;
5516f6f8c2fSMilind Arun Choudhary 		else if (ALIGN(align + min_align, min_align) < align1)
5521da177e4SLinus Torvalds 			min_align = align1 >> 1;
5531da177e4SLinus Torvalds 		align += aligns[order];
5541da177e4SLinus Torvalds 	}
55513583b16SRam Pai 	size = calculate_memsize(size, min_size, 0, resource_size(b_res), align);
5561da177e4SLinus Torvalds 	if (!size) {
557865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
558865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
559865df576SBjorn Helgaas 				 "%pR to [bus %02x-%02x] (unused)\n", b_res,
560865df576SBjorn Helgaas 				 bus->secondary, bus->subordinate);
5611da177e4SLinus Torvalds 		b_res->flags = 0;
5621da177e4SLinus Torvalds 		return 1;
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds 	b_res->start = min_align;
5651da177e4SLinus Torvalds 	b_res->end = size + min_align - 1;
56688452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
5671f82de10SYinghai Lu 	b_res->flags |= mem64_mask;
5681da177e4SLinus Torvalds 	return 1;
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5715468ae61SAdrian Bunk static void pci_bus_size_cardbus(struct pci_bus *bus)
5721da177e4SLinus Torvalds {
5731da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
5741da177e4SLinus Torvalds 	struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
5751da177e4SLinus Torvalds 	u16 ctrl;
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	/*
5781da177e4SLinus Torvalds 	 * Reserve some resources for CardBus.  We reserve
5791da177e4SLinus Torvalds 	 * a fixed amount of bus space for CardBus bridges.
5801da177e4SLinus Torvalds 	 */
581934b7024SLinus Torvalds 	b_res[0].start = 0;
582934b7024SLinus Torvalds 	b_res[0].end = pci_cardbus_io_size - 1;
583934b7024SLinus Torvalds 	b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
5841da177e4SLinus Torvalds 
585934b7024SLinus Torvalds 	b_res[1].start = 0;
586934b7024SLinus Torvalds 	b_res[1].end = pci_cardbus_io_size - 1;
587934b7024SLinus Torvalds 	b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	/*
5901da177e4SLinus Torvalds 	 * Check whether prefetchable memory is supported
5911da177e4SLinus Torvalds 	 * by this bridge.
5921da177e4SLinus Torvalds 	 */
5931da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
5941da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
5951da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
5961da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
5971da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
5981da177e4SLinus Torvalds 	}
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	/*
6011da177e4SLinus Torvalds 	 * If we have prefetchable memory support, allocate
6021da177e4SLinus Torvalds 	 * two regions.  Otherwise, allocate one region of
6031da177e4SLinus Torvalds 	 * twice the size.
6041da177e4SLinus Torvalds 	 */
6051da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
606934b7024SLinus Torvalds 		b_res[2].start = 0;
607934b7024SLinus Torvalds 		b_res[2].end = pci_cardbus_mem_size - 1;
608934b7024SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
6091da177e4SLinus Torvalds 
610934b7024SLinus Torvalds 		b_res[3].start = 0;
611934b7024SLinus Torvalds 		b_res[3].end = pci_cardbus_mem_size - 1;
612934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
6131da177e4SLinus Torvalds 	} else {
614934b7024SLinus Torvalds 		b_res[3].start = 0;
615934b7024SLinus Torvalds 		b_res[3].end = pci_cardbus_mem_size * 2 - 1;
616934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
6171da177e4SLinus Torvalds 	}
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
620451124a7SSam Ravnborg void __ref pci_bus_size_bridges(struct pci_bus *bus)
6211da177e4SLinus Torvalds {
6221da177e4SLinus Torvalds 	struct pci_dev *dev;
6231da177e4SLinus Torvalds 	unsigned long mask, prefmask;
62428760489SEric W. Biederman 	resource_size_t min_mem_size = 0, min_io_size = 0;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
6271da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
6281da177e4SLinus Torvalds 		if (!b)
6291da177e4SLinus Torvalds 			continue;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 		switch (dev->class >> 8) {
6321da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
6331da177e4SLinus Torvalds 			pci_bus_size_cardbus(b);
6341da177e4SLinus Torvalds 			break;
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
6371da177e4SLinus Torvalds 		default:
6381da177e4SLinus Torvalds 			pci_bus_size_bridges(b);
6391da177e4SLinus Torvalds 			break;
6401da177e4SLinus Torvalds 		}
6411da177e4SLinus Torvalds 	}
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	/* The root bus? */
6441da177e4SLinus Torvalds 	if (!bus->self)
6451da177e4SLinus Torvalds 		return;
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	switch (bus->self->class >> 8) {
6481da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_CARDBUS:
6491da177e4SLinus Torvalds 		/* don't size cardbuses yet. */
6501da177e4SLinus Torvalds 		break;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_PCI:
6531da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
65428760489SEric W. Biederman 		if (bus->self->is_hotplug_bridge) {
65528760489SEric W. Biederman 			min_io_size  = pci_hotplug_io_size;
65628760489SEric W. Biederman 			min_mem_size = pci_hotplug_mem_size;
65728760489SEric W. Biederman 		}
6581da177e4SLinus Torvalds 	default:
65928760489SEric W. Biederman 		pbus_size_io(bus, min_io_size);
6601da177e4SLinus Torvalds 		/* If the bridge supports prefetchable range, size it
6611da177e4SLinus Torvalds 		   separately. If it doesn't, or its prefetchable window
6621da177e4SLinus Torvalds 		   has already been allocated by arch code, try
6631da177e4SLinus Torvalds 		   non-prefetchable range for both types of PCI memory
6641da177e4SLinus Torvalds 		   resources. */
6651da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
6661da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
66728760489SEric W. Biederman 		if (pbus_size_mem(bus, prefmask, prefmask, min_mem_size))
6681da177e4SLinus Torvalds 			mask = prefmask; /* Success, size non-prefetch only. */
66928760489SEric W. Biederman 		else
67028760489SEric W. Biederman 			min_mem_size += min_mem_size;
67128760489SEric W. Biederman 		pbus_size_mem(bus, mask, IORESOURCE_MEM, min_mem_size);
6721da177e4SLinus Torvalds 		break;
6731da177e4SLinus Torvalds 	}
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
6761da177e4SLinus Torvalds 
677568ddef8SYinghai Lu static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
678568ddef8SYinghai Lu 					 struct resource_list_x *fail_head)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	struct pci_bus *b;
6811da177e4SLinus Torvalds 	struct pci_dev *dev;
6821da177e4SLinus Torvalds 
683568ddef8SYinghai Lu 	pbus_assign_resources_sorted(bus, fail_head);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
6861da177e4SLinus Torvalds 		b = dev->subordinate;
6871da177e4SLinus Torvalds 		if (!b)
6881da177e4SLinus Torvalds 			continue;
6891da177e4SLinus Torvalds 
690568ddef8SYinghai Lu 		__pci_bus_assign_resources(b, fail_head);
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 		switch (dev->class >> 8) {
6931da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
6946841ec68SYinghai Lu 			if (!pci_is_enabled(dev))
6951da177e4SLinus Torvalds 				pci_setup_bridge(b);
6961da177e4SLinus Torvalds 			break;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
6991da177e4SLinus Torvalds 			pci_setup_cardbus(b);
7001da177e4SLinus Torvalds 			break;
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 		default:
70380ccba11SBjorn Helgaas 			dev_info(&dev->dev, "not setting up bridge for bus "
70480ccba11SBjorn Helgaas 				 "%04x:%02x\n", pci_domain_nr(b), b->number);
7051da177e4SLinus Torvalds 			break;
7061da177e4SLinus Torvalds 		}
7071da177e4SLinus Torvalds 	}
7081da177e4SLinus Torvalds }
709568ddef8SYinghai Lu 
710568ddef8SYinghai Lu void __ref pci_bus_assign_resources(const struct pci_bus *bus)
711568ddef8SYinghai Lu {
712568ddef8SYinghai Lu 	__pci_bus_assign_resources(bus, NULL);
713568ddef8SYinghai Lu }
7141da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
7151da177e4SLinus Torvalds 
7166841ec68SYinghai Lu static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
7176841ec68SYinghai Lu 					 struct resource_list_x *fail_head)
7186841ec68SYinghai Lu {
7196841ec68SYinghai Lu 	struct pci_bus *b;
7206841ec68SYinghai Lu 
7216841ec68SYinghai Lu 	pdev_assign_resources_sorted((struct pci_dev *)bridge, fail_head);
7226841ec68SYinghai Lu 
7236841ec68SYinghai Lu 	b = bridge->subordinate;
7246841ec68SYinghai Lu 	if (!b)
7256841ec68SYinghai Lu 		return;
7266841ec68SYinghai Lu 
7276841ec68SYinghai Lu 	__pci_bus_assign_resources(b, fail_head);
7286841ec68SYinghai Lu 
7296841ec68SYinghai Lu 	switch (bridge->class >> 8) {
7306841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_PCI:
7316841ec68SYinghai Lu 		pci_setup_bridge(b);
7326841ec68SYinghai Lu 		break;
7336841ec68SYinghai Lu 
7346841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_CARDBUS:
7356841ec68SYinghai Lu 		pci_setup_cardbus(b);
7366841ec68SYinghai Lu 		break;
7376841ec68SYinghai Lu 
7386841ec68SYinghai Lu 	default:
7396841ec68SYinghai Lu 		dev_info(&bridge->dev, "not setting up bridge for bus "
7406841ec68SYinghai Lu 			 "%04x:%02x\n", pci_domain_nr(b), b->number);
7416841ec68SYinghai Lu 		break;
7426841ec68SYinghai Lu 	}
7436841ec68SYinghai Lu }
7445009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus,
7455009b460SYinghai Lu 					  unsigned long type)
7465009b460SYinghai Lu {
7475009b460SYinghai Lu 	int idx;
7485009b460SYinghai Lu 	bool changed = false;
7495009b460SYinghai Lu 	struct pci_dev *dev;
7505009b460SYinghai Lu 	struct resource *r;
7515009b460SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
7525009b460SYinghai Lu 				  IORESOURCE_PREFETCH;
7535009b460SYinghai Lu 
7545009b460SYinghai Lu 	dev = bus->self;
7555009b460SYinghai Lu 	for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
7565009b460SYinghai Lu 	     idx++) {
7575009b460SYinghai Lu 		r = &dev->resource[idx];
7585009b460SYinghai Lu 		if ((r->flags & type_mask) != type)
7595009b460SYinghai Lu 			continue;
7605009b460SYinghai Lu 		if (!r->parent)
7615009b460SYinghai Lu 			continue;
7625009b460SYinghai Lu 		/*
7635009b460SYinghai Lu 		 * if there are children under that, we should release them
7645009b460SYinghai Lu 		 *  all
7655009b460SYinghai Lu 		 */
7665009b460SYinghai Lu 		release_child_resources(r);
7675009b460SYinghai Lu 		if (!release_resource(r)) {
7685009b460SYinghai Lu 			dev_printk(KERN_DEBUG, &dev->dev,
7695009b460SYinghai Lu 				 "resource %d %pR released\n", idx, r);
7705009b460SYinghai Lu 			/* keep the old size */
7715009b460SYinghai Lu 			r->end = resource_size(r) - 1;
7725009b460SYinghai Lu 			r->start = 0;
7735009b460SYinghai Lu 			r->flags = 0;
7745009b460SYinghai Lu 			changed = true;
7755009b460SYinghai Lu 		}
7765009b460SYinghai Lu 	}
7775009b460SYinghai Lu 
7785009b460SYinghai Lu 	if (changed) {
7795009b460SYinghai Lu 		/* avoiding touch the one without PREF */
7805009b460SYinghai Lu 		if (type & IORESOURCE_PREFETCH)
7815009b460SYinghai Lu 			type = IORESOURCE_PREFETCH;
7825009b460SYinghai Lu 		__pci_setup_bridge(bus, type);
7835009b460SYinghai Lu 	}
7845009b460SYinghai Lu }
7855009b460SYinghai Lu 
7865009b460SYinghai Lu enum release_type {
7875009b460SYinghai Lu 	leaf_only,
7885009b460SYinghai Lu 	whole_subtree,
7895009b460SYinghai Lu };
7905009b460SYinghai Lu /*
7915009b460SYinghai Lu  * try to release pci bridge resources that is from leaf bridge,
7925009b460SYinghai Lu  * so we can allocate big new one later
7935009b460SYinghai Lu  */
7945009b460SYinghai Lu static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
7955009b460SYinghai Lu 						   unsigned long type,
7965009b460SYinghai Lu 						   enum release_type rel_type)
7975009b460SYinghai Lu {
7985009b460SYinghai Lu 	struct pci_dev *dev;
7995009b460SYinghai Lu 	bool is_leaf_bridge = true;
8005009b460SYinghai Lu 
8015009b460SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
8025009b460SYinghai Lu 		struct pci_bus *b = dev->subordinate;
8035009b460SYinghai Lu 		if (!b)
8045009b460SYinghai Lu 			continue;
8055009b460SYinghai Lu 
8065009b460SYinghai Lu 		is_leaf_bridge = false;
8075009b460SYinghai Lu 
8085009b460SYinghai Lu 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
8095009b460SYinghai Lu 			continue;
8105009b460SYinghai Lu 
8115009b460SYinghai Lu 		if (rel_type == whole_subtree)
8125009b460SYinghai Lu 			pci_bus_release_bridge_resources(b, type,
8135009b460SYinghai Lu 						 whole_subtree);
8145009b460SYinghai Lu 	}
8155009b460SYinghai Lu 
8165009b460SYinghai Lu 	if (pci_is_root_bus(bus))
8175009b460SYinghai Lu 		return;
8185009b460SYinghai Lu 
8195009b460SYinghai Lu 	if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
8205009b460SYinghai Lu 		return;
8215009b460SYinghai Lu 
8225009b460SYinghai Lu 	if ((rel_type == whole_subtree) || is_leaf_bridge)
8235009b460SYinghai Lu 		pci_bridge_release_resources(bus, type);
8245009b460SYinghai Lu }
8255009b460SYinghai Lu 
82676fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
82776fbc263SYinghai Lu {
82889a74eccSBjorn Helgaas 	struct resource *res;
82976fbc263SYinghai Lu 	int i;
83076fbc263SYinghai Lu 
83189a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
8327c9342b8SYinghai Lu 		if (!res || !res->end || !res->flags)
83376fbc263SYinghai Lu                         continue;
83476fbc263SYinghai Lu 
835c7dabef8SBjorn Helgaas 		dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
83676fbc263SYinghai Lu         }
83776fbc263SYinghai Lu }
83876fbc263SYinghai Lu 
83976fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
84076fbc263SYinghai Lu {
84176fbc263SYinghai Lu 	struct pci_bus *b;
84276fbc263SYinghai Lu 	struct pci_dev *dev;
84376fbc263SYinghai Lu 
84476fbc263SYinghai Lu 
84576fbc263SYinghai Lu 	pci_bus_dump_res(bus);
84676fbc263SYinghai Lu 
84776fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
84876fbc263SYinghai Lu 		b = dev->subordinate;
84976fbc263SYinghai Lu 		if (!b)
85076fbc263SYinghai Lu 			continue;
85176fbc263SYinghai Lu 
85276fbc263SYinghai Lu 		pci_bus_dump_resources(b);
85376fbc263SYinghai Lu 	}
85476fbc263SYinghai Lu }
85576fbc263SYinghai Lu 
8561da177e4SLinus Torvalds void __init
8571da177e4SLinus Torvalds pci_assign_unassigned_resources(void)
8581da177e4SLinus Torvalds {
8591da177e4SLinus Torvalds 	struct pci_bus *bus;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	/* Depth first, calculate sizes and alignments of all
8621da177e4SLinus Torvalds 	   subordinate buses. */
8631da177e4SLinus Torvalds 	list_for_each_entry(bus, &pci_root_buses, node) {
8641da177e4SLinus Torvalds 		pci_bus_size_bridges(bus);
8651da177e4SLinus Torvalds 	}
8661da177e4SLinus Torvalds 	/* Depth last, allocate resources and update the hardware. */
8671da177e4SLinus Torvalds 	list_for_each_entry(bus, &pci_root_buses, node) {
868769d9968SLinus Torvalds 		pci_bus_assign_resources(bus);
869977d17bbSYinghai Lu 		pci_enable_bridges(bus);
870769d9968SLinus Torvalds 	}
87176fbc263SYinghai Lu 
87276fbc263SYinghai Lu 	/* dump the resource on buses */
87376fbc263SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node) {
87476fbc263SYinghai Lu 		pci_bus_dump_resources(bus);
87576fbc263SYinghai Lu 	}
8761da177e4SLinus Torvalds }
8776841ec68SYinghai Lu 
8786841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
8796841ec68SYinghai Lu {
8806841ec68SYinghai Lu 	struct pci_bus *parent = bridge->subordinate;
88132180e40SYinghai Lu 	int tried_times = 0;
88232180e40SYinghai Lu 	struct resource_list_x head, *list;
8836841ec68SYinghai Lu 	int retval;
88432180e40SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
88532180e40SYinghai Lu 				  IORESOURCE_PREFETCH;
8866841ec68SYinghai Lu 
88732180e40SYinghai Lu 	head.next = NULL;
88832180e40SYinghai Lu 
88932180e40SYinghai Lu again:
8906841ec68SYinghai Lu 	pci_bus_size_bridges(parent);
89132180e40SYinghai Lu 	__pci_bridge_assign_resources(bridge, &head);
89232180e40SYinghai Lu 
89332180e40SYinghai Lu 	tried_times++;
89432180e40SYinghai Lu 
89532180e40SYinghai Lu 	if (!head.next)
8963f579c34SYinghai Lu 		goto enable_all;
89732180e40SYinghai Lu 
89832180e40SYinghai Lu 	if (tried_times >= 2) {
89932180e40SYinghai Lu 		/* still fail, don't need to try more */
900*094732a5SRam Pai 		free_list(resource_list_x, &head);
9013f579c34SYinghai Lu 		goto enable_all;
90232180e40SYinghai Lu 	}
90332180e40SYinghai Lu 
90432180e40SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
90532180e40SYinghai Lu 			 tried_times + 1);
90632180e40SYinghai Lu 
90732180e40SYinghai Lu 	/*
90832180e40SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
90932180e40SYinghai Lu 	 * child device under that bridge
91032180e40SYinghai Lu 	 */
91132180e40SYinghai Lu 	for (list = head.next; list;) {
91232180e40SYinghai Lu 		struct pci_bus *bus = list->dev->bus;
91332180e40SYinghai Lu 		unsigned long flags = list->flags;
91432180e40SYinghai Lu 
91532180e40SYinghai Lu 		pci_bus_release_bridge_resources(bus, flags & type_mask,
91632180e40SYinghai Lu 						 whole_subtree);
91732180e40SYinghai Lu 		list = list->next;
91832180e40SYinghai Lu 	}
91932180e40SYinghai Lu 	/* restore size and flags */
92032180e40SYinghai Lu 	for (list = head.next; list;) {
92132180e40SYinghai Lu 		struct resource *res = list->res;
92232180e40SYinghai Lu 
92332180e40SYinghai Lu 		res->start = list->start;
92432180e40SYinghai Lu 		res->end = list->end;
92532180e40SYinghai Lu 		res->flags = list->flags;
92632180e40SYinghai Lu 		if (list->dev->subordinate)
92732180e40SYinghai Lu 			res->flags = 0;
92832180e40SYinghai Lu 
92932180e40SYinghai Lu 		list = list->next;
93032180e40SYinghai Lu 	}
931*094732a5SRam Pai 	free_list(resource_list_x, &head);
93232180e40SYinghai Lu 
93332180e40SYinghai Lu 	goto again;
9343f579c34SYinghai Lu 
9353f579c34SYinghai Lu enable_all:
9363f579c34SYinghai Lu 	retval = pci_reenable_device(bridge);
9373f579c34SYinghai Lu 	pci_set_master(bridge);
9383f579c34SYinghai Lu 	pci_enable_bridges(parent);
9396841ec68SYinghai Lu }
9406841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
941