xref: /openbmc/linux/drivers/pci/setup-bus.c (revision 9a928660c9dcaff568c9d379655c5aa16fb981f8)
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 
39568ddef8SYinghai Lu static void add_to_failed_list(struct resource_list_x *head,
40568ddef8SYinghai Lu 				 struct pci_dev *dev, struct resource *res)
41568ddef8SYinghai Lu {
42568ddef8SYinghai Lu 	struct resource_list_x *list = head;
43568ddef8SYinghai Lu 	struct resource_list_x *ln = list->next;
44568ddef8SYinghai Lu 	struct resource_list_x *tmp;
45568ddef8SYinghai Lu 
46568ddef8SYinghai Lu 	tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
47568ddef8SYinghai Lu 	if (!tmp) {
48568ddef8SYinghai Lu 		pr_warning("add_to_failed_list: kmalloc() failed!\n");
49568ddef8SYinghai Lu 		return;
50568ddef8SYinghai Lu 	}
51568ddef8SYinghai Lu 
52568ddef8SYinghai Lu 	tmp->next = ln;
53568ddef8SYinghai Lu 	tmp->res = res;
54568ddef8SYinghai Lu 	tmp->dev = dev;
55568ddef8SYinghai Lu 	tmp->start = res->start;
56568ddef8SYinghai Lu 	tmp->end = res->end;
57568ddef8SYinghai Lu 	tmp->flags = res->flags;
58568ddef8SYinghai Lu 	list->next = tmp;
59568ddef8SYinghai Lu }
60568ddef8SYinghai Lu 
61568ddef8SYinghai Lu static void free_failed_list(struct resource_list_x *head)
62568ddef8SYinghai Lu {
63568ddef8SYinghai Lu 	struct resource_list_x *list, *tmp;
64568ddef8SYinghai Lu 
65568ddef8SYinghai Lu 	for (list = head->next; list;) {
66568ddef8SYinghai Lu 		tmp = list;
67568ddef8SYinghai Lu 		list = list->next;
68568ddef8SYinghai Lu 		kfree(tmp);
69568ddef8SYinghai Lu 	}
70568ddef8SYinghai Lu 
71568ddef8SYinghai Lu 	head->next = NULL;
72568ddef8SYinghai Lu }
73568ddef8SYinghai Lu 
746841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev,
756841ec68SYinghai Lu 				 struct resource_list *head)
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds 	u16 class = dev->class >> 8;
781da177e4SLinus Torvalds 
799bded00bSKenji Kaneshige 	/* Don't touch classless devices or host bridges or ioapics.  */
806841ec68SYinghai Lu 	if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
816841ec68SYinghai Lu 		return;
821da177e4SLinus Torvalds 
839bded00bSKenji Kaneshige 	/* Don't touch ioapic devices already enabled by firmware */
8423186279SSatoru Takeuchi 	if (class == PCI_CLASS_SYSTEM_PIC) {
859bded00bSKenji Kaneshige 		u16 command;
869bded00bSKenji Kaneshige 		pci_read_config_word(dev, PCI_COMMAND, &command);
879bded00bSKenji Kaneshige 		if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
886841ec68SYinghai Lu 			return;
8923186279SSatoru Takeuchi 	}
9023186279SSatoru Takeuchi 
916841ec68SYinghai Lu 	pdev_sort_resources(dev, head);
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
946841ec68SYinghai Lu static void __assign_resources_sorted(struct resource_list *head,
956841ec68SYinghai Lu 				 struct resource_list_x *fail_head)
966841ec68SYinghai Lu {
976841ec68SYinghai Lu 	struct resource *res;
986841ec68SYinghai Lu 	struct resource_list *list, *tmp;
996841ec68SYinghai Lu 	int idx;
1006841ec68SYinghai Lu 
1016841ec68SYinghai Lu 	for (list = head->next; list;) {
1021da177e4SLinus Torvalds 		res = list->res;
1031da177e4SLinus Torvalds 		idx = res - &list->dev->resource[0];
104*9a928660SYinghai Lu 
105542df5deSRajesh Shah 		if (pci_assign_resource(list->dev, idx)) {
106*9a928660SYinghai Lu 			if (fail_head && !pci_is_root_bus(list->dev->bus)) {
107*9a928660SYinghai Lu 				/*
108*9a928660SYinghai Lu 				 * if the failed res is for ROM BAR, and it will
109*9a928660SYinghai Lu 				 * be enabled later, don't add it to the list
110*9a928660SYinghai Lu 				 */
111*9a928660SYinghai Lu 				if (!((idx == PCI_ROM_RESOURCE) &&
112*9a928660SYinghai Lu 				      (!(res->flags & IORESOURCE_ROM_ENABLE))))
113568ddef8SYinghai Lu 					add_to_failed_list(fail_head, list->dev, res);
114*9a928660SYinghai Lu 			}
115542df5deSRajesh Shah 			res->start = 0;
116960b8466SIvan Kokshaysky 			res->end = 0;
117542df5deSRajesh Shah 			res->flags = 0;
118542df5deSRajesh Shah 		}
1191da177e4SLinus Torvalds 		tmp = list;
1201da177e4SLinus Torvalds 		list = list->next;
1211da177e4SLinus Torvalds 		kfree(tmp);
1221da177e4SLinus Torvalds 	}
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1256841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev,
1266841ec68SYinghai Lu 				 struct resource_list_x *fail_head)
1276841ec68SYinghai Lu {
1286841ec68SYinghai Lu 	struct resource_list head;
1296841ec68SYinghai Lu 
1306841ec68SYinghai Lu 	head.next = NULL;
1316841ec68SYinghai Lu 	__dev_sort_resources(dev, &head);
1326841ec68SYinghai Lu 	__assign_resources_sorted(&head, fail_head);
1336841ec68SYinghai Lu 
1346841ec68SYinghai Lu }
1356841ec68SYinghai Lu 
1366841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus,
1376841ec68SYinghai Lu 					 struct resource_list_x *fail_head)
1386841ec68SYinghai Lu {
1396841ec68SYinghai Lu 	struct pci_dev *dev;
1406841ec68SYinghai Lu 	struct resource_list head;
1416841ec68SYinghai Lu 
1426841ec68SYinghai Lu 	head.next = NULL;
1436841ec68SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list)
1446841ec68SYinghai Lu 		__dev_sort_resources(dev, &head);
1456841ec68SYinghai Lu 
1466841ec68SYinghai Lu 	__assign_resources_sorted(&head, fail_head);
1476841ec68SYinghai Lu }
1486841ec68SYinghai Lu 
149b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
152c7dabef8SBjorn Helgaas 	struct resource *res;
1531da177e4SLinus Torvalds 	struct pci_bus_region region;
1541da177e4SLinus Torvalds 
155865df576SBjorn Helgaas 	dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
156865df576SBjorn Helgaas 		 bus->secondary, bus->subordinate);
1571da177e4SLinus Torvalds 
158c7dabef8SBjorn Helgaas 	res = bus->resource[0];
159c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
160c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
1611da177e4SLinus Torvalds 		/*
1621da177e4SLinus Torvalds 		 * The IO resource is allocated a range twice as large as it
1631da177e4SLinus Torvalds 		 * would normally need.  This allows us to set both IO regs.
1641da177e4SLinus Torvalds 		 */
165c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
1661da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
1671da177e4SLinus Torvalds 					region.start);
1681da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
1691da177e4SLinus Torvalds 					region.end);
1701da177e4SLinus Torvalds 	}
1711da177e4SLinus Torvalds 
172c7dabef8SBjorn Helgaas 	res = bus->resource[1];
173c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
174c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
175c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
1761da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
1771da177e4SLinus Torvalds 					region.start);
1781da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
1791da177e4SLinus Torvalds 					region.end);
1801da177e4SLinus Torvalds 	}
1811da177e4SLinus Torvalds 
182c7dabef8SBjorn Helgaas 	res = bus->resource[2];
183c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
184c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
185c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
1861da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
1871da177e4SLinus Torvalds 					region.start);
1881da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
1891da177e4SLinus Torvalds 					region.end);
1901da177e4SLinus Torvalds 	}
1911da177e4SLinus Torvalds 
192c7dabef8SBjorn Helgaas 	res = bus->resource[3];
193c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
194c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
195c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
1961da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
1971da177e4SLinus Torvalds 					region.start);
1981da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
1991da177e4SLinus Torvalds 					region.end);
2001da177e4SLinus Torvalds 	}
2011da177e4SLinus Torvalds }
202b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus);
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected.
2051da177e4SLinus Torvalds    PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
2061da177e4SLinus Torvalds    requires that if there is no I/O ports or memory behind the
2071da177e4SLinus Torvalds    bridge, corresponding range must be turned off by writing base
2081da177e4SLinus Torvalds    value greater than limit to the bridge's base/limit registers.
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds    Note: care must be taken when updating I/O base/limit registers
2111da177e4SLinus Torvalds    of bridges which support 32-bit I/O. This update requires two
2121da177e4SLinus Torvalds    config space writes, so it's quite possible that an I/O window of
2131da177e4SLinus Torvalds    the bridge will have some undesirable address (e.g. 0) after the
2141da177e4SLinus Torvalds    first write. Ditto 64-bit prefetchable MMIO.  */
2157cc5997dSYinghai Lu static void pci_setup_bridge_io(struct pci_bus *bus)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
218c7dabef8SBjorn Helgaas 	struct resource *res;
2191da177e4SLinus Torvalds 	struct pci_bus_region region;
2207cc5997dSYinghai Lu 	u32 l, io_upper16;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	/* Set up the top and bottom of the PCI I/O segment for this bus. */
223c7dabef8SBjorn Helgaas 	res = bus->resource[0];
224c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
225c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
2261da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_IO_BASE, &l);
2271da177e4SLinus Torvalds 		l &= 0xffff0000;
2281da177e4SLinus Torvalds 		l |= (region.start >> 8) & 0x00f0;
2291da177e4SLinus Torvalds 		l |= region.end & 0xf000;
2301da177e4SLinus Torvalds 		/* Set up upper 16 bits of I/O base/limit. */
2311da177e4SLinus Torvalds 		io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
232c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
2337cc5997dSYinghai Lu 	} else {
2341da177e4SLinus Torvalds 		/* Clear upper 16 bits of I/O base/limit. */
2351da177e4SLinus Torvalds 		io_upper16 = 0;
2361da177e4SLinus Torvalds 		l = 0x00f0;
237c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window [io  disabled]\n");
2381da177e4SLinus Torvalds 	}
2391da177e4SLinus Torvalds 	/* Temporarily disable the I/O range before updating PCI_IO_BASE. */
2401da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
2411da177e4SLinus Torvalds 	/* Update lower 16 bits of I/O base/limit. */
2421da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE, l);
2431da177e4SLinus Torvalds 	/* Update upper 16 bits of I/O base/limit. */
2441da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
2457cc5997dSYinghai Lu }
2461da177e4SLinus Torvalds 
2477cc5997dSYinghai Lu static void pci_setup_bridge_mmio(struct pci_bus *bus)
2487cc5997dSYinghai Lu {
2497cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
2507cc5997dSYinghai Lu 	struct resource *res;
2517cc5997dSYinghai Lu 	struct pci_bus_region region;
2527cc5997dSYinghai Lu 	u32 l;
2537cc5997dSYinghai Lu 
2547cc5997dSYinghai Lu 	/* Set up the top and bottom of the PCI Memory segment for this bus. */
255c7dabef8SBjorn Helgaas 	res = bus->resource[1];
256c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
257c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM) {
2581da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
2591da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
260c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
2617cc5997dSYinghai Lu 	} else {
2621da177e4SLinus Torvalds 		l = 0x0000fff0;
263c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window [mem disabled]\n");
2641da177e4SLinus Torvalds 	}
2651da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
2667cc5997dSYinghai Lu }
2677cc5997dSYinghai Lu 
2687cc5997dSYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
2697cc5997dSYinghai Lu {
2707cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
2717cc5997dSYinghai Lu 	struct resource *res;
2727cc5997dSYinghai Lu 	struct pci_bus_region region;
2737cc5997dSYinghai Lu 	u32 l, bu, lu;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	/* Clear out the upper 32 bits of PREF limit.
2761da177e4SLinus Torvalds 	   If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
2771da177e4SLinus Torvalds 	   disables PREF range, which is ok. */
2781da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	/* Set up PREF base/limit. */
281c40a22e0SBenjamin Herrenschmidt 	bu = lu = 0;
282c7dabef8SBjorn Helgaas 	res = bus->resource[2];
283c7dabef8SBjorn Helgaas 	pcibios_resource_to_bus(bridge, &region, res);
284c7dabef8SBjorn Helgaas 	if (res->flags & IORESOURCE_PREFETCH) {
2851da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
2861da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
287c7dabef8SBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64) {
28813d36c24SAndrew Morton 			bu = upper_32_bits(region.start);
28913d36c24SAndrew Morton 			lu = upper_32_bits(region.end);
2901f82de10SYinghai Lu 		}
291c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window %pR\n", res);
2927cc5997dSYinghai Lu 	} else {
2931da177e4SLinus Torvalds 		l = 0x0000fff0;
294c7dabef8SBjorn Helgaas 		dev_info(&bridge->dev, "  bridge window [mem pref disabled]\n");
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
2971da177e4SLinus Torvalds 
298c40a22e0SBenjamin Herrenschmidt 	/* Set the upper 32 bits of PREF base & limit. */
299c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
300c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
3017cc5997dSYinghai Lu }
3027cc5997dSYinghai Lu 
3037cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
3047cc5997dSYinghai Lu {
3057cc5997dSYinghai Lu 	struct pci_dev *bridge = bus->self;
3067cc5997dSYinghai Lu 
3077cc5997dSYinghai Lu 	dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
3087cc5997dSYinghai Lu 		 bus->secondary, bus->subordinate);
3097cc5997dSYinghai Lu 
3107cc5997dSYinghai Lu 	if (type & IORESOURCE_IO)
3117cc5997dSYinghai Lu 		pci_setup_bridge_io(bus);
3127cc5997dSYinghai Lu 
3137cc5997dSYinghai Lu 	if (type & IORESOURCE_MEM)
3147cc5997dSYinghai Lu 		pci_setup_bridge_mmio(bus);
3157cc5997dSYinghai Lu 
3167cc5997dSYinghai Lu 	if (type & IORESOURCE_PREFETCH)
3177cc5997dSYinghai Lu 		pci_setup_bridge_mmio_pref(bus);
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
3227cc5997dSYinghai Lu static void pci_setup_bridge(struct pci_bus *bus)
3237cc5997dSYinghai Lu {
3247cc5997dSYinghai Lu 	unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
3257cc5997dSYinghai Lu 				  IORESOURCE_PREFETCH;
3267cc5997dSYinghai Lu 
3277cc5997dSYinghai Lu 	__pci_setup_bridge(bus, type);
3287cc5997dSYinghai Lu }
3297cc5997dSYinghai Lu 
3301da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and
3311da177e4SLinus Torvalds    prefetchable memory ranges. If not, the respective
3321da177e4SLinus Torvalds    base/limit registers must be read-only and read as 0. */
33396bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus)
3341da177e4SLinus Torvalds {
3351da177e4SLinus Torvalds 	u16 io;
3361da177e4SLinus Torvalds 	u32 pmem;
3371da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
3381da177e4SLinus Torvalds 	struct resource *b_res;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
3411da177e4SLinus Torvalds 	b_res[1].flags |= IORESOURCE_MEM;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_IO_BASE, &io);
3441da177e4SLinus Torvalds 	if (!io) {
3451da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
3461da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_IO_BASE, &io);
3471da177e4SLinus Torvalds  		pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
3481da177e4SLinus Torvalds  	}
3491da177e4SLinus Torvalds  	if (io)
3501da177e4SLinus Torvalds 		b_res[0].flags |= IORESOURCE_IO;
3511da177e4SLinus Torvalds 	/*  DECchip 21050 pass 2 errata: the bridge may miss an address
3521da177e4SLinus Torvalds 	    disconnect boundary by one PCI data phase.
3531da177e4SLinus Torvalds 	    Workaround: do not use prefetching on this device. */
3541da177e4SLinus Torvalds 	if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
3551da177e4SLinus Torvalds 		return;
3561da177e4SLinus Torvalds 	pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
3571da177e4SLinus Torvalds 	if (!pmem) {
3581da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
3591da177e4SLinus Torvalds 					       0xfff0fff0);
3601da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
3611da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
3621da177e4SLinus Torvalds 	}
3631f82de10SYinghai Lu 	if (pmem) {
3641da177e4SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
36599586105SYinghai Lu 		if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
36699586105SYinghai Lu 		    PCI_PREF_RANGE_TYPE_64) {
3671f82de10SYinghai Lu 			b_res[2].flags |= IORESOURCE_MEM_64;
36899586105SYinghai Lu 			b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
36999586105SYinghai Lu 		}
3701f82de10SYinghai Lu 	}
3711f82de10SYinghai Lu 
3721f82de10SYinghai Lu 	/* double check if bridge does support 64 bit pref */
3731f82de10SYinghai Lu 	if (b_res[2].flags & IORESOURCE_MEM_64) {
3741f82de10SYinghai Lu 		u32 mem_base_hi, tmp;
3751f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
3761f82de10SYinghai Lu 					 &mem_base_hi);
3771f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
3781f82de10SYinghai Lu 					       0xffffffff);
3791f82de10SYinghai Lu 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
3801f82de10SYinghai Lu 		if (!tmp)
3811f82de10SYinghai Lu 			b_res[2].flags &= ~IORESOURCE_MEM_64;
3821f82de10SYinghai Lu 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
3831f82de10SYinghai Lu 				       mem_base_hi);
3841f82de10SYinghai Lu 	}
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds /* Helper function for sizing routines: find first available
3881da177e4SLinus Torvalds    bus resource of a given type. Note: we intentionally skip
3891da177e4SLinus Torvalds    the bus resources which have already been assigned (that is,
3901da177e4SLinus Torvalds    have non-NULL parent resource). */
39196bde06aSSam Ravnborg static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	int i;
3941da177e4SLinus Torvalds 	struct resource *r;
3951da177e4SLinus Torvalds 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
3961da177e4SLinus Torvalds 				  IORESOURCE_PREFETCH;
3971da177e4SLinus Torvalds 
39889a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, r, i) {
399299de034SIvan Kokshaysky 		if (r == &ioport_resource || r == &iomem_resource)
400299de034SIvan Kokshaysky 			continue;
40155a10984SJesse Barnes 		if (r && (r->flags & type_mask) == type && !r->parent)
4021da177e4SLinus Torvalds 			return r;
4031da177e4SLinus Torvalds 	}
4041da177e4SLinus Torvalds 	return NULL;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds /* Sizing the IO windows of the PCI-PCI bridge is trivial,
4081da177e4SLinus Torvalds    since these windows have 4K granularity and the IO ranges
4091da177e4SLinus Torvalds    of non-bridge PCI devices are limited to 256 bytes.
4101da177e4SLinus Torvalds    We must be careful with the ISA aliasing though. */
41128760489SEric W. Biederman static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	struct pci_dev *dev;
4141da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
415d65245c3SYinghai Lu 	unsigned long size = 0, size1 = 0, old_size;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	if (!b_res)
4181da177e4SLinus Torvalds  		return;
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
4211da177e4SLinus Torvalds 		int i;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
4241da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
4251da177e4SLinus Torvalds 			unsigned long r_size;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 			if (r->parent || !(r->flags & IORESOURCE_IO))
4281da177e4SLinus Torvalds 				continue;
429022edd86SZhao, Yu 			r_size = resource_size(r);
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 			if (r_size < 0x400)
4321da177e4SLinus Torvalds 				/* Might be re-aligned for ISA */
4331da177e4SLinus Torvalds 				size += r_size;
4341da177e4SLinus Torvalds 			else
4351da177e4SLinus Torvalds 				size1 += r_size;
4361da177e4SLinus Torvalds 		}
4371da177e4SLinus Torvalds 	}
43828760489SEric W. Biederman 	if (size < min_size)
43928760489SEric W. Biederman 		size = min_size;
440d65245c3SYinghai Lu 	old_size = resource_size(b_res);
441d65245c3SYinghai Lu 	if (old_size == 1)
442d65245c3SYinghai Lu 		old_size = 0;
4431da177e4SLinus Torvalds /* To be fixed in 2.5: we should have sort of HAVE_ISA
4441da177e4SLinus Torvalds    flag in the struct pci_bus. */
4451da177e4SLinus Torvalds #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
4461da177e4SLinus Torvalds 	size = (size & 0xff) + ((size & ~0xffUL) << 2);
4471da177e4SLinus Torvalds #endif
4486f6f8c2fSMilind Arun Choudhary 	size = ALIGN(size + size1, 4096);
449d65245c3SYinghai Lu 	if (size < old_size)
450d65245c3SYinghai Lu 		size = old_size;
4511da177e4SLinus Torvalds 	if (!size) {
452865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
453865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
454865df576SBjorn Helgaas 				 "%pR to [bus %02x-%02x] (unused)\n", b_res,
455865df576SBjorn Helgaas 				 bus->secondary, bus->subordinate);
4561da177e4SLinus Torvalds 		b_res->flags = 0;
4571da177e4SLinus Torvalds 		return;
4581da177e4SLinus Torvalds 	}
4591da177e4SLinus Torvalds 	/* Alignment of the IO window is always 4K */
4601da177e4SLinus Torvalds 	b_res->start = 4096;
4611da177e4SLinus Torvalds 	b_res->end = b_res->start + size - 1;
46288452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds /* Calculate the size of the bus and minimal alignment which
4661da177e4SLinus Torvalds    guarantees that all child resources fit in this size. */
46728760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
46828760489SEric W. Biederman 			 unsigned long type, resource_size_t min_size)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	struct pci_dev *dev;
471d65245c3SYinghai Lu 	resource_size_t min_align, align, size, old_size;
472c40a22e0SBenjamin Herrenschmidt 	resource_size_t aligns[12];	/* Alignments from 1Mb to 2Gb */
4731da177e4SLinus Torvalds 	int order, max_order;
4741da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, type);
4751f82de10SYinghai Lu 	unsigned int mem64_mask = 0;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	if (!b_res)
4781da177e4SLinus Torvalds 		return 0;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
4811da177e4SLinus Torvalds 	max_order = 0;
4821da177e4SLinus Torvalds 	size = 0;
4831da177e4SLinus Torvalds 
4841f82de10SYinghai Lu 	mem64_mask = b_res->flags & IORESOURCE_MEM_64;
4851f82de10SYinghai Lu 	b_res->flags &= ~IORESOURCE_MEM_64;
4861f82de10SYinghai Lu 
4871da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
4881da177e4SLinus Torvalds 		int i;
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
4911da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
492c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 			if (r->parent || (r->flags & mask) != type)
4951da177e4SLinus Torvalds 				continue;
496022edd86SZhao, Yu 			r_size = resource_size(r);
4971da177e4SLinus Torvalds 			/* For bridges size != alignment */
4986faf17f6SChris Wright 			align = pci_resource_alignment(dev, r);
4991da177e4SLinus Torvalds 			order = __ffs(align) - 20;
5001da177e4SLinus Torvalds 			if (order > 11) {
501865df576SBjorn Helgaas 				dev_warn(&dev->dev, "disabling BAR %d: %pR "
502865df576SBjorn Helgaas 					 "(bad alignment %#llx)\n", i, r,
503865df576SBjorn Helgaas 					 (unsigned long long) align);
5041da177e4SLinus Torvalds 				r->flags = 0;
5051da177e4SLinus Torvalds 				continue;
5061da177e4SLinus Torvalds 			}
5071da177e4SLinus Torvalds 			size += r_size;
5081da177e4SLinus Torvalds 			if (order < 0)
5091da177e4SLinus Torvalds 				order = 0;
5101da177e4SLinus Torvalds 			/* Exclude ranges with size > align from
5111da177e4SLinus Torvalds 			   calculation of the alignment. */
5121da177e4SLinus Torvalds 			if (r_size == align)
5131da177e4SLinus Torvalds 				aligns[order] += align;
5141da177e4SLinus Torvalds 			if (order > max_order)
5151da177e4SLinus Torvalds 				max_order = order;
5161f82de10SYinghai Lu 			mem64_mask &= r->flags & IORESOURCE_MEM_64;
5171da177e4SLinus Torvalds 		}
5181da177e4SLinus Torvalds 	}
51928760489SEric W. Biederman 	if (size < min_size)
52028760489SEric W. Biederman 		size = min_size;
521d65245c3SYinghai Lu 	old_size = resource_size(b_res);
522d65245c3SYinghai Lu 	if (old_size == 1)
523d65245c3SYinghai Lu 		old_size = 0;
524d65245c3SYinghai Lu 	if (size < old_size)
525d65245c3SYinghai Lu 		size = old_size;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	align = 0;
5281da177e4SLinus Torvalds 	min_align = 0;
5291da177e4SLinus Torvalds 	for (order = 0; order <= max_order; order++) {
5308308c54dSJeremy Fitzhardinge 		resource_size_t align1 = 1;
5318308c54dSJeremy Fitzhardinge 
5328308c54dSJeremy Fitzhardinge 		align1 <<= (order + 20);
5338308c54dSJeremy Fitzhardinge 
5341da177e4SLinus Torvalds 		if (!align)
5351da177e4SLinus Torvalds 			min_align = align1;
5366f6f8c2fSMilind Arun Choudhary 		else if (ALIGN(align + min_align, min_align) < align1)
5371da177e4SLinus Torvalds 			min_align = align1 >> 1;
5381da177e4SLinus Torvalds 		align += aligns[order];
5391da177e4SLinus Torvalds 	}
5406f6f8c2fSMilind Arun Choudhary 	size = ALIGN(size, min_align);
5411da177e4SLinus Torvalds 	if (!size) {
542865df576SBjorn Helgaas 		if (b_res->start || b_res->end)
543865df576SBjorn Helgaas 			dev_info(&bus->self->dev, "disabling bridge window "
544865df576SBjorn Helgaas 				 "%pR to [bus %02x-%02x] (unused)\n", b_res,
545865df576SBjorn Helgaas 				 bus->secondary, bus->subordinate);
5461da177e4SLinus Torvalds 		b_res->flags = 0;
5471da177e4SLinus Torvalds 		return 1;
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 	b_res->start = min_align;
5501da177e4SLinus Torvalds 	b_res->end = size + min_align - 1;
55188452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
5521f82de10SYinghai Lu 	b_res->flags |= mem64_mask;
5531da177e4SLinus Torvalds 	return 1;
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
5565468ae61SAdrian Bunk static void pci_bus_size_cardbus(struct pci_bus *bus)
5571da177e4SLinus Torvalds {
5581da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
5591da177e4SLinus Torvalds 	struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
5601da177e4SLinus Torvalds 	u16 ctrl;
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	/*
5631da177e4SLinus Torvalds 	 * Reserve some resources for CardBus.  We reserve
5641da177e4SLinus Torvalds 	 * a fixed amount of bus space for CardBus bridges.
5651da177e4SLinus Torvalds 	 */
566934b7024SLinus Torvalds 	b_res[0].start = 0;
567934b7024SLinus Torvalds 	b_res[0].end = pci_cardbus_io_size - 1;
568934b7024SLinus Torvalds 	b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
5691da177e4SLinus Torvalds 
570934b7024SLinus Torvalds 	b_res[1].start = 0;
571934b7024SLinus Torvalds 	b_res[1].end = pci_cardbus_io_size - 1;
572934b7024SLinus Torvalds 	b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	/*
5751da177e4SLinus Torvalds 	 * Check whether prefetchable memory is supported
5761da177e4SLinus Torvalds 	 * by this bridge.
5771da177e4SLinus Torvalds 	 */
5781da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
5791da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
5801da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
5811da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
5821da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
5831da177e4SLinus Torvalds 	}
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	/*
5861da177e4SLinus Torvalds 	 * If we have prefetchable memory support, allocate
5871da177e4SLinus Torvalds 	 * two regions.  Otherwise, allocate one region of
5881da177e4SLinus Torvalds 	 * twice the size.
5891da177e4SLinus Torvalds 	 */
5901da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
591934b7024SLinus Torvalds 		b_res[2].start = 0;
592934b7024SLinus Torvalds 		b_res[2].end = pci_cardbus_mem_size - 1;
593934b7024SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
5941da177e4SLinus Torvalds 
595934b7024SLinus Torvalds 		b_res[3].start = 0;
596934b7024SLinus Torvalds 		b_res[3].end = pci_cardbus_mem_size - 1;
597934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
5981da177e4SLinus Torvalds 	} else {
599934b7024SLinus Torvalds 		b_res[3].start = 0;
600934b7024SLinus Torvalds 		b_res[3].end = pci_cardbus_mem_size * 2 - 1;
601934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
6021da177e4SLinus Torvalds 	}
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
605451124a7SSam Ravnborg void __ref pci_bus_size_bridges(struct pci_bus *bus)
6061da177e4SLinus Torvalds {
6071da177e4SLinus Torvalds 	struct pci_dev *dev;
6081da177e4SLinus Torvalds 	unsigned long mask, prefmask;
60928760489SEric W. Biederman 	resource_size_t min_mem_size = 0, min_io_size = 0;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
6121da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
6131da177e4SLinus Torvalds 		if (!b)
6141da177e4SLinus Torvalds 			continue;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 		switch (dev->class >> 8) {
6171da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
6181da177e4SLinus Torvalds 			pci_bus_size_cardbus(b);
6191da177e4SLinus Torvalds 			break;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
6221da177e4SLinus Torvalds 		default:
6231da177e4SLinus Torvalds 			pci_bus_size_bridges(b);
6241da177e4SLinus Torvalds 			break;
6251da177e4SLinus Torvalds 		}
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	/* The root bus? */
6291da177e4SLinus Torvalds 	if (!bus->self)
6301da177e4SLinus Torvalds 		return;
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	switch (bus->self->class >> 8) {
6331da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_CARDBUS:
6341da177e4SLinus Torvalds 		/* don't size cardbuses yet. */
6351da177e4SLinus Torvalds 		break;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_PCI:
6381da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
63928760489SEric W. Biederman 		if (bus->self->is_hotplug_bridge) {
64028760489SEric W. Biederman 			min_io_size  = pci_hotplug_io_size;
64128760489SEric W. Biederman 			min_mem_size = pci_hotplug_mem_size;
64228760489SEric W. Biederman 		}
6431da177e4SLinus Torvalds 	default:
64428760489SEric W. Biederman 		pbus_size_io(bus, min_io_size);
6451da177e4SLinus Torvalds 		/* If the bridge supports prefetchable range, size it
6461da177e4SLinus Torvalds 		   separately. If it doesn't, or its prefetchable window
6471da177e4SLinus Torvalds 		   has already been allocated by arch code, try
6481da177e4SLinus Torvalds 		   non-prefetchable range for both types of PCI memory
6491da177e4SLinus Torvalds 		   resources. */
6501da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
6511da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
65228760489SEric W. Biederman 		if (pbus_size_mem(bus, prefmask, prefmask, min_mem_size))
6531da177e4SLinus Torvalds 			mask = prefmask; /* Success, size non-prefetch only. */
65428760489SEric W. Biederman 		else
65528760489SEric W. Biederman 			min_mem_size += min_mem_size;
65628760489SEric W. Biederman 		pbus_size_mem(bus, mask, IORESOURCE_MEM, min_mem_size);
6571da177e4SLinus Torvalds 		break;
6581da177e4SLinus Torvalds 	}
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
6611da177e4SLinus Torvalds 
662568ddef8SYinghai Lu static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
663568ddef8SYinghai Lu 					 struct resource_list_x *fail_head)
6641da177e4SLinus Torvalds {
6651da177e4SLinus Torvalds 	struct pci_bus *b;
6661da177e4SLinus Torvalds 	struct pci_dev *dev;
6671da177e4SLinus Torvalds 
668568ddef8SYinghai Lu 	pbus_assign_resources_sorted(bus, fail_head);
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
6711da177e4SLinus Torvalds 		b = dev->subordinate;
6721da177e4SLinus Torvalds 		if (!b)
6731da177e4SLinus Torvalds 			continue;
6741da177e4SLinus Torvalds 
675568ddef8SYinghai Lu 		__pci_bus_assign_resources(b, fail_head);
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 		switch (dev->class >> 8) {
6781da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
6796841ec68SYinghai Lu 			if (!pci_is_enabled(dev))
6801da177e4SLinus Torvalds 				pci_setup_bridge(b);
6811da177e4SLinus Torvalds 			break;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
6841da177e4SLinus Torvalds 			pci_setup_cardbus(b);
6851da177e4SLinus Torvalds 			break;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 		default:
68880ccba11SBjorn Helgaas 			dev_info(&dev->dev, "not setting up bridge for bus "
68980ccba11SBjorn Helgaas 				 "%04x:%02x\n", pci_domain_nr(b), b->number);
6901da177e4SLinus Torvalds 			break;
6911da177e4SLinus Torvalds 		}
6921da177e4SLinus Torvalds 	}
6931da177e4SLinus Torvalds }
694568ddef8SYinghai Lu 
695568ddef8SYinghai Lu void __ref pci_bus_assign_resources(const struct pci_bus *bus)
696568ddef8SYinghai Lu {
697568ddef8SYinghai Lu 	__pci_bus_assign_resources(bus, NULL);
698568ddef8SYinghai Lu }
6991da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
7001da177e4SLinus Torvalds 
7016841ec68SYinghai Lu static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
7026841ec68SYinghai Lu 					 struct resource_list_x *fail_head)
7036841ec68SYinghai Lu {
7046841ec68SYinghai Lu 	struct pci_bus *b;
7056841ec68SYinghai Lu 
7066841ec68SYinghai Lu 	pdev_assign_resources_sorted((struct pci_dev *)bridge, fail_head);
7076841ec68SYinghai Lu 
7086841ec68SYinghai Lu 	b = bridge->subordinate;
7096841ec68SYinghai Lu 	if (!b)
7106841ec68SYinghai Lu 		return;
7116841ec68SYinghai Lu 
7126841ec68SYinghai Lu 	__pci_bus_assign_resources(b, fail_head);
7136841ec68SYinghai Lu 
7146841ec68SYinghai Lu 	switch (bridge->class >> 8) {
7156841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_PCI:
7166841ec68SYinghai Lu 		pci_setup_bridge(b);
7176841ec68SYinghai Lu 		break;
7186841ec68SYinghai Lu 
7196841ec68SYinghai Lu 	case PCI_CLASS_BRIDGE_CARDBUS:
7206841ec68SYinghai Lu 		pci_setup_cardbus(b);
7216841ec68SYinghai Lu 		break;
7226841ec68SYinghai Lu 
7236841ec68SYinghai Lu 	default:
7246841ec68SYinghai Lu 		dev_info(&bridge->dev, "not setting up bridge for bus "
7256841ec68SYinghai Lu 			 "%04x:%02x\n", pci_domain_nr(b), b->number);
7266841ec68SYinghai Lu 		break;
7276841ec68SYinghai Lu 	}
7286841ec68SYinghai Lu }
7295009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus,
7305009b460SYinghai Lu 					  unsigned long type)
7315009b460SYinghai Lu {
7325009b460SYinghai Lu 	int idx;
7335009b460SYinghai Lu 	bool changed = false;
7345009b460SYinghai Lu 	struct pci_dev *dev;
7355009b460SYinghai Lu 	struct resource *r;
7365009b460SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
7375009b460SYinghai Lu 				  IORESOURCE_PREFETCH;
7385009b460SYinghai Lu 
7395009b460SYinghai Lu 	dev = bus->self;
7405009b460SYinghai Lu 	for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
7415009b460SYinghai Lu 	     idx++) {
7425009b460SYinghai Lu 		r = &dev->resource[idx];
7435009b460SYinghai Lu 		if ((r->flags & type_mask) != type)
7445009b460SYinghai Lu 			continue;
7455009b460SYinghai Lu 		if (!r->parent)
7465009b460SYinghai Lu 			continue;
7475009b460SYinghai Lu 		/*
7485009b460SYinghai Lu 		 * if there are children under that, we should release them
7495009b460SYinghai Lu 		 *  all
7505009b460SYinghai Lu 		 */
7515009b460SYinghai Lu 		release_child_resources(r);
7525009b460SYinghai Lu 		if (!release_resource(r)) {
7535009b460SYinghai Lu 			dev_printk(KERN_DEBUG, &dev->dev,
7545009b460SYinghai Lu 				 "resource %d %pR released\n", idx, r);
7555009b460SYinghai Lu 			/* keep the old size */
7565009b460SYinghai Lu 			r->end = resource_size(r) - 1;
7575009b460SYinghai Lu 			r->start = 0;
7585009b460SYinghai Lu 			r->flags = 0;
7595009b460SYinghai Lu 			changed = true;
7605009b460SYinghai Lu 		}
7615009b460SYinghai Lu 	}
7625009b460SYinghai Lu 
7635009b460SYinghai Lu 	if (changed) {
7645009b460SYinghai Lu 		/* avoiding touch the one without PREF */
7655009b460SYinghai Lu 		if (type & IORESOURCE_PREFETCH)
7665009b460SYinghai Lu 			type = IORESOURCE_PREFETCH;
7675009b460SYinghai Lu 		__pci_setup_bridge(bus, type);
7685009b460SYinghai Lu 	}
7695009b460SYinghai Lu }
7705009b460SYinghai Lu 
7715009b460SYinghai Lu enum release_type {
7725009b460SYinghai Lu 	leaf_only,
7735009b460SYinghai Lu 	whole_subtree,
7745009b460SYinghai Lu };
7755009b460SYinghai Lu /*
7765009b460SYinghai Lu  * try to release pci bridge resources that is from leaf bridge,
7775009b460SYinghai Lu  * so we can allocate big new one later
7785009b460SYinghai Lu  */
7795009b460SYinghai Lu static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
7805009b460SYinghai Lu 						   unsigned long type,
7815009b460SYinghai Lu 						   enum release_type rel_type)
7825009b460SYinghai Lu {
7835009b460SYinghai Lu 	struct pci_dev *dev;
7845009b460SYinghai Lu 	bool is_leaf_bridge = true;
7855009b460SYinghai Lu 
7865009b460SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
7875009b460SYinghai Lu 		struct pci_bus *b = dev->subordinate;
7885009b460SYinghai Lu 		if (!b)
7895009b460SYinghai Lu 			continue;
7905009b460SYinghai Lu 
7915009b460SYinghai Lu 		is_leaf_bridge = false;
7925009b460SYinghai Lu 
7935009b460SYinghai Lu 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
7945009b460SYinghai Lu 			continue;
7955009b460SYinghai Lu 
7965009b460SYinghai Lu 		if (rel_type == whole_subtree)
7975009b460SYinghai Lu 			pci_bus_release_bridge_resources(b, type,
7985009b460SYinghai Lu 						 whole_subtree);
7995009b460SYinghai Lu 	}
8005009b460SYinghai Lu 
8015009b460SYinghai Lu 	if (pci_is_root_bus(bus))
8025009b460SYinghai Lu 		return;
8035009b460SYinghai Lu 
8045009b460SYinghai Lu 	if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
8055009b460SYinghai Lu 		return;
8065009b460SYinghai Lu 
8075009b460SYinghai Lu 	if ((rel_type == whole_subtree) || is_leaf_bridge)
8085009b460SYinghai Lu 		pci_bridge_release_resources(bus, type);
8095009b460SYinghai Lu }
8105009b460SYinghai Lu 
81176fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
81276fbc263SYinghai Lu {
81389a74eccSBjorn Helgaas 	struct resource *res;
81476fbc263SYinghai Lu 	int i;
81576fbc263SYinghai Lu 
81689a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
8177c9342b8SYinghai Lu 		if (!res || !res->end || !res->flags)
81876fbc263SYinghai Lu                         continue;
81976fbc263SYinghai Lu 
820c7dabef8SBjorn Helgaas 		dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
82176fbc263SYinghai Lu         }
82276fbc263SYinghai Lu }
82376fbc263SYinghai Lu 
82476fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
82576fbc263SYinghai Lu {
82676fbc263SYinghai Lu 	struct pci_bus *b;
82776fbc263SYinghai Lu 	struct pci_dev *dev;
82876fbc263SYinghai Lu 
82976fbc263SYinghai Lu 
83076fbc263SYinghai Lu 	pci_bus_dump_res(bus);
83176fbc263SYinghai Lu 
83276fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
83376fbc263SYinghai Lu 		b = dev->subordinate;
83476fbc263SYinghai Lu 		if (!b)
83576fbc263SYinghai Lu 			continue;
83676fbc263SYinghai Lu 
83776fbc263SYinghai Lu 		pci_bus_dump_resources(b);
83876fbc263SYinghai Lu 	}
83976fbc263SYinghai Lu }
84076fbc263SYinghai Lu 
841977d17bbSYinghai Lu static int __init pci_bus_get_depth(struct pci_bus *bus)
842977d17bbSYinghai Lu {
843977d17bbSYinghai Lu 	int depth = 0;
844977d17bbSYinghai Lu 	struct pci_dev *dev;
845977d17bbSYinghai Lu 
846977d17bbSYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
847977d17bbSYinghai Lu 		int ret;
848977d17bbSYinghai Lu 		struct pci_bus *b = dev->subordinate;
849977d17bbSYinghai Lu 		if (!b)
850977d17bbSYinghai Lu 			continue;
851977d17bbSYinghai Lu 
852977d17bbSYinghai Lu 		ret = pci_bus_get_depth(b);
853977d17bbSYinghai Lu 		if (ret + 1 > depth)
854977d17bbSYinghai Lu 			depth = ret + 1;
855977d17bbSYinghai Lu 	}
856977d17bbSYinghai Lu 
857977d17bbSYinghai Lu 	return depth;
858977d17bbSYinghai Lu }
859977d17bbSYinghai Lu static int __init pci_get_max_depth(void)
860977d17bbSYinghai Lu {
861977d17bbSYinghai Lu 	int depth = 0;
862977d17bbSYinghai Lu 	struct pci_bus *bus;
863977d17bbSYinghai Lu 
864977d17bbSYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node) {
865977d17bbSYinghai Lu 		int ret;
866977d17bbSYinghai Lu 
867977d17bbSYinghai Lu 		ret = pci_bus_get_depth(bus);
868977d17bbSYinghai Lu 		if (ret > depth)
869977d17bbSYinghai Lu 			depth = ret;
870977d17bbSYinghai Lu 	}
871977d17bbSYinghai Lu 
872977d17bbSYinghai Lu 	return depth;
873977d17bbSYinghai Lu }
874977d17bbSYinghai Lu 
875977d17bbSYinghai Lu /*
876977d17bbSYinghai Lu  * first try will not touch pci bridge res
877977d17bbSYinghai Lu  * second  and later try will clear small leaf bridge res
878977d17bbSYinghai Lu  * will stop till to the max  deepth if can not find good one
879977d17bbSYinghai Lu  */
8801da177e4SLinus Torvalds void __init
8811da177e4SLinus Torvalds pci_assign_unassigned_resources(void)
8821da177e4SLinus Torvalds {
8831da177e4SLinus Torvalds 	struct pci_bus *bus;
884977d17bbSYinghai Lu 	int tried_times = 0;
885977d17bbSYinghai Lu 	enum release_type rel_type = leaf_only;
886977d17bbSYinghai Lu 	struct resource_list_x head, *list;
887977d17bbSYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
888977d17bbSYinghai Lu 				  IORESOURCE_PREFETCH;
889977d17bbSYinghai Lu 	unsigned long failed_type;
890977d17bbSYinghai Lu 	int max_depth = pci_get_max_depth();
891977d17bbSYinghai Lu 	int pci_try_num;
8921da177e4SLinus Torvalds 
893977d17bbSYinghai Lu 	head.next = NULL;
894977d17bbSYinghai Lu 
895977d17bbSYinghai Lu 	pci_try_num = max_depth + 1;
896977d17bbSYinghai Lu 	printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
897977d17bbSYinghai Lu 		 max_depth, pci_try_num);
898977d17bbSYinghai Lu 
899977d17bbSYinghai Lu again:
9001da177e4SLinus Torvalds 	/* Depth first, calculate sizes and alignments of all
9011da177e4SLinus Torvalds 	   subordinate buses. */
9021da177e4SLinus Torvalds 	list_for_each_entry(bus, &pci_root_buses, node) {
9031da177e4SLinus Torvalds 		pci_bus_size_bridges(bus);
9041da177e4SLinus Torvalds 	}
9051da177e4SLinus Torvalds 	/* Depth last, allocate resources and update the hardware. */
9061da177e4SLinus Torvalds 	list_for_each_entry(bus, &pci_root_buses, node) {
907977d17bbSYinghai Lu 		__pci_bus_assign_resources(bus, &head);
9081da177e4SLinus Torvalds 	}
909977d17bbSYinghai Lu 	tried_times++;
910977d17bbSYinghai Lu 
911977d17bbSYinghai Lu 	/* any device complain? */
912977d17bbSYinghai Lu 	if (!head.next)
913977d17bbSYinghai Lu 		goto enable_and_dump;
914977d17bbSYinghai Lu 	failed_type = 0;
915977d17bbSYinghai Lu 	for (list = head.next; list;) {
916977d17bbSYinghai Lu 		failed_type |= list->flags;
917977d17bbSYinghai Lu 		list = list->next;
918977d17bbSYinghai Lu 	}
919977d17bbSYinghai Lu 	/*
920977d17bbSYinghai Lu 	 * io port are tight, don't try extra
921977d17bbSYinghai Lu 	 * or if reach the limit, don't want to try more
922977d17bbSYinghai Lu 	 */
923977d17bbSYinghai Lu 	failed_type &= type_mask;
924977d17bbSYinghai Lu 	if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
925977d17bbSYinghai Lu 		free_failed_list(&head);
926977d17bbSYinghai Lu 		goto enable_and_dump;
927977d17bbSYinghai Lu 	}
928977d17bbSYinghai Lu 
929977d17bbSYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
930977d17bbSYinghai Lu 			 tried_times + 1);
931977d17bbSYinghai Lu 
932977d17bbSYinghai Lu 	/* third times and later will not check if it is leaf */
933977d17bbSYinghai Lu 	if ((tried_times + 1) > 2)
934977d17bbSYinghai Lu 		rel_type = whole_subtree;
935977d17bbSYinghai Lu 
936977d17bbSYinghai Lu 	/*
937977d17bbSYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
938977d17bbSYinghai Lu 	 * child device under that bridge
939977d17bbSYinghai Lu 	 */
940977d17bbSYinghai Lu 	for (list = head.next; list;) {
941977d17bbSYinghai Lu 		bus = list->dev->bus;
942977d17bbSYinghai Lu 		pci_bus_release_bridge_resources(bus, list->flags & type_mask,
943977d17bbSYinghai Lu 						  rel_type);
944977d17bbSYinghai Lu 		list = list->next;
945977d17bbSYinghai Lu 	}
946977d17bbSYinghai Lu 	/* restore size and flags */
947977d17bbSYinghai Lu 	for (list = head.next; list;) {
948977d17bbSYinghai Lu 		struct resource *res = list->res;
949977d17bbSYinghai Lu 
950977d17bbSYinghai Lu 		res->start = list->start;
951977d17bbSYinghai Lu 		res->end = list->end;
952977d17bbSYinghai Lu 		res->flags = list->flags;
953977d17bbSYinghai Lu 		if (list->dev->subordinate)
954977d17bbSYinghai Lu 			res->flags = 0;
955977d17bbSYinghai Lu 
956977d17bbSYinghai Lu 		list = list->next;
957977d17bbSYinghai Lu 	}
958977d17bbSYinghai Lu 	free_failed_list(&head);
959977d17bbSYinghai Lu 
960977d17bbSYinghai Lu 	goto again;
961977d17bbSYinghai Lu 
962977d17bbSYinghai Lu enable_and_dump:
963977d17bbSYinghai Lu 	/* Depth last, update the hardware. */
964977d17bbSYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node)
965977d17bbSYinghai Lu 		pci_enable_bridges(bus);
96676fbc263SYinghai Lu 
96776fbc263SYinghai Lu 	/* dump the resource on buses */
96876fbc263SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node) {
96976fbc263SYinghai Lu 		pci_bus_dump_resources(bus);
97076fbc263SYinghai Lu 	}
9711da177e4SLinus Torvalds }
9726841ec68SYinghai Lu 
9736841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
9746841ec68SYinghai Lu {
9756841ec68SYinghai Lu 	struct pci_bus *parent = bridge->subordinate;
97632180e40SYinghai Lu 	int tried_times = 0;
97732180e40SYinghai Lu 	struct resource_list_x head, *list;
9786841ec68SYinghai Lu 	int retval;
97932180e40SYinghai Lu 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
98032180e40SYinghai Lu 				  IORESOURCE_PREFETCH;
9816841ec68SYinghai Lu 
98232180e40SYinghai Lu 	head.next = NULL;
98332180e40SYinghai Lu 
98432180e40SYinghai Lu again:
9856841ec68SYinghai Lu 	pci_bus_size_bridges(parent);
98632180e40SYinghai Lu 	__pci_bridge_assign_resources(bridge, &head);
9876841ec68SYinghai Lu 	retval = pci_reenable_device(bridge);
9886841ec68SYinghai Lu 	pci_set_master(bridge);
9896841ec68SYinghai Lu 	pci_enable_bridges(parent);
99032180e40SYinghai Lu 
99132180e40SYinghai Lu 	tried_times++;
99232180e40SYinghai Lu 
99332180e40SYinghai Lu 	if (!head.next)
99432180e40SYinghai Lu 		return;
99532180e40SYinghai Lu 
99632180e40SYinghai Lu 	if (tried_times >= 2) {
99732180e40SYinghai Lu 		/* still fail, don't need to try more */
99832180e40SYinghai Lu 		free_failed_list(&head);
99932180e40SYinghai Lu 		return;
100032180e40SYinghai Lu 	}
100132180e40SYinghai Lu 
100232180e40SYinghai Lu 	printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
100332180e40SYinghai Lu 			 tried_times + 1);
100432180e40SYinghai Lu 
100532180e40SYinghai Lu 	/*
100632180e40SYinghai Lu 	 * Try to release leaf bridge's resources that doesn't fit resource of
100732180e40SYinghai Lu 	 * child device under that bridge
100832180e40SYinghai Lu 	 */
100932180e40SYinghai Lu 	for (list = head.next; list;) {
101032180e40SYinghai Lu 		struct pci_bus *bus = list->dev->bus;
101132180e40SYinghai Lu 		unsigned long flags = list->flags;
101232180e40SYinghai Lu 
101332180e40SYinghai Lu 		pci_bus_release_bridge_resources(bus, flags & type_mask,
101432180e40SYinghai Lu 						 whole_subtree);
101532180e40SYinghai Lu 		list = list->next;
101632180e40SYinghai Lu 	}
101732180e40SYinghai Lu 	/* restore size and flags */
101832180e40SYinghai Lu 	for (list = head.next; list;) {
101932180e40SYinghai Lu 		struct resource *res = list->res;
102032180e40SYinghai Lu 
102132180e40SYinghai Lu 		res->start = list->start;
102232180e40SYinghai Lu 		res->end = list->end;
102332180e40SYinghai Lu 		res->flags = list->flags;
102432180e40SYinghai Lu 		if (list->dev->subordinate)
102532180e40SYinghai Lu 			res->flags = 0;
102632180e40SYinghai Lu 
102732180e40SYinghai Lu 		list = list->next;
102832180e40SYinghai Lu 	}
102932180e40SYinghai Lu 	free_failed_list(&head);
103032180e40SYinghai Lu 
103132180e40SYinghai Lu 	goto again;
10326841ec68SYinghai Lu }
10336841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
1034