xref: /openbmc/linux/drivers/pci/setup-bus.c (revision 096e6f673dc02a6394dc9a7d8f8735c6978f5b91)
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>
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds 
3096bde06aSSam Ravnborg static void pbus_assign_resources_sorted(struct pci_bus *bus)
311da177e4SLinus Torvalds {
321da177e4SLinus Torvalds 	struct pci_dev *dev;
331da177e4SLinus Torvalds 	struct resource *res;
341da177e4SLinus Torvalds 	struct resource_list head, *list, *tmp;
351da177e4SLinus Torvalds 	int idx;
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds 	head.next = NULL;
381da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
391da177e4SLinus Torvalds 		u16 class = dev->class >> 8;
401da177e4SLinus Torvalds 
419bded00bSKenji Kaneshige 		/* Don't touch classless devices or host bridges or ioapics.  */
421da177e4SLinus Torvalds 		if (class == PCI_CLASS_NOT_DEFINED ||
4323186279SSatoru Takeuchi 		    class == PCI_CLASS_BRIDGE_HOST)
441da177e4SLinus Torvalds 			continue;
451da177e4SLinus Torvalds 
469bded00bSKenji Kaneshige 		/* Don't touch ioapic devices already enabled by firmware */
4723186279SSatoru Takeuchi 		if (class == PCI_CLASS_SYSTEM_PIC) {
489bded00bSKenji Kaneshige 			u16 command;
499bded00bSKenji Kaneshige 			pci_read_config_word(dev, PCI_COMMAND, &command);
509bded00bSKenji Kaneshige 			if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
5123186279SSatoru Takeuchi 				continue;
5223186279SSatoru Takeuchi 		}
5323186279SSatoru Takeuchi 
541da177e4SLinus Torvalds 		pdev_sort_resources(dev, &head);
551da177e4SLinus Torvalds 	}
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	for (list = head.next; list;) {
581da177e4SLinus Torvalds 		res = list->res;
591da177e4SLinus Torvalds 		idx = res - &list->dev->resource[0];
60542df5deSRajesh Shah 		if (pci_assign_resource(list->dev, idx)) {
6188452565SIvan Kokshaysky 			/* FIXME: get rid of this */
62542df5deSRajesh Shah 			res->start = 0;
63960b8466SIvan Kokshaysky 			res->end = 0;
64542df5deSRajesh Shah 			res->flags = 0;
65542df5deSRajesh Shah 		}
661da177e4SLinus Torvalds 		tmp = list;
671da177e4SLinus Torvalds 		list = list->next;
681da177e4SLinus Torvalds 		kfree(tmp);
691da177e4SLinus Torvalds 	}
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
72b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus)
731da177e4SLinus Torvalds {
741da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
751da177e4SLinus Torvalds 	struct pci_bus_region region;
761da177e4SLinus Torvalds 
7780ccba11SBjorn Helgaas 	dev_info(&bridge->dev, "CardBus bridge, secondary bus %04x:%02x\n",
7880ccba11SBjorn Helgaas 		 pci_domain_nr(bus), bus->number);
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
811da177e4SLinus Torvalds 	if (bus->resource[0]->flags & IORESOURCE_IO) {
821da177e4SLinus Torvalds 		/*
831da177e4SLinus Torvalds 		 * The IO resource is allocated a range twice as large as it
841da177e4SLinus Torvalds 		 * would normally need.  This allows us to set both IO regs.
851da177e4SLinus Torvalds 		 */
8680ccba11SBjorn Helgaas 		dev_info(&bridge->dev, "  IO window: %#08lx-%#08lx\n",
87c40a22e0SBenjamin Herrenschmidt 		       (unsigned long)region.start,
88c40a22e0SBenjamin Herrenschmidt 		       (unsigned long)region.end);
891da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
901da177e4SLinus Torvalds 					region.start);
911da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
921da177e4SLinus Torvalds 					region.end);
931da177e4SLinus Torvalds 	}
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
961da177e4SLinus Torvalds 	if (bus->resource[1]->flags & IORESOURCE_IO) {
9780ccba11SBjorn Helgaas 		dev_info(&bridge->dev, "  IO window: %#08lx-%#08lx\n",
98c40a22e0SBenjamin Herrenschmidt 		       (unsigned long)region.start,
99c40a22e0SBenjamin Herrenschmidt 		       (unsigned long)region.end);
1001da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
1011da177e4SLinus Torvalds 					region.start);
1021da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
1031da177e4SLinus Torvalds 					region.end);
1041da177e4SLinus Torvalds 	}
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	pcibios_resource_to_bus(bridge, &region, bus->resource[2]);
1071da177e4SLinus Torvalds 	if (bus->resource[2]->flags & IORESOURCE_MEM) {
10880ccba11SBjorn Helgaas 		dev_info(&bridge->dev, "  PREFETCH window: %#08lx-%#08lx\n",
109c40a22e0SBenjamin Herrenschmidt 		       (unsigned long)region.start,
110c40a22e0SBenjamin Herrenschmidt 		       (unsigned long)region.end);
1111da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
1121da177e4SLinus Torvalds 					region.start);
1131da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
1141da177e4SLinus Torvalds 					region.end);
1151da177e4SLinus Torvalds 	}
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	pcibios_resource_to_bus(bridge, &region, bus->resource[3]);
1181da177e4SLinus Torvalds 	if (bus->resource[3]->flags & IORESOURCE_MEM) {
11980ccba11SBjorn Helgaas 		dev_info(&bridge->dev, "  MEM window: %#08lx-%#08lx\n",
120c40a22e0SBenjamin Herrenschmidt 		       (unsigned long)region.start,
121c40a22e0SBenjamin Herrenschmidt 		       (unsigned long)region.end);
1221da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
1231da177e4SLinus Torvalds 					region.start);
1241da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
1251da177e4SLinus Torvalds 					region.end);
1261da177e4SLinus Torvalds 	}
1271da177e4SLinus Torvalds }
128b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus);
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected.
1311da177e4SLinus Torvalds    PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
1321da177e4SLinus Torvalds    requires that if there is no I/O ports or memory behind the
1331da177e4SLinus Torvalds    bridge, corresponding range must be turned off by writing base
1341da177e4SLinus Torvalds    value greater than limit to the bridge's base/limit registers.
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds    Note: care must be taken when updating I/O base/limit registers
1371da177e4SLinus Torvalds    of bridges which support 32-bit I/O. This update requires two
1381da177e4SLinus Torvalds    config space writes, so it's quite possible that an I/O window of
1391da177e4SLinus Torvalds    the bridge will have some undesirable address (e.g. 0) after the
1401da177e4SLinus Torvalds    first write. Ditto 64-bit prefetchable MMIO.  */
141a391f197SAdrian Bunk static void pci_setup_bridge(struct pci_bus *bus)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
1441da177e4SLinus Torvalds 	struct pci_bus_region region;
145c40a22e0SBenjamin Herrenschmidt 	u32 l, bu, lu, io_upper16;
1461da177e4SLinus Torvalds 
14780ccba11SBjorn Helgaas 	dev_info(&bridge->dev, "PCI bridge, secondary bus %04x:%02x\n",
14880ccba11SBjorn Helgaas 		 pci_domain_nr(bus), bus->number);
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	/* Set up the top and bottom of the PCI I/O segment for this bus. */
1511da177e4SLinus Torvalds 	pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
1521da177e4SLinus Torvalds 	if (bus->resource[0]->flags & IORESOURCE_IO) {
1531da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_IO_BASE, &l);
1541da177e4SLinus Torvalds 		l &= 0xffff0000;
1551da177e4SLinus Torvalds 		l |= (region.start >> 8) & 0x00f0;
1561da177e4SLinus Torvalds 		l |= region.end & 0xf000;
1571da177e4SLinus Torvalds 		/* Set up upper 16 bits of I/O base/limit. */
1581da177e4SLinus Torvalds 		io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
15980ccba11SBjorn Helgaas 		dev_info(&bridge->dev, "  IO window: %#04lx-%#04lx\n",
160c40a22e0SBenjamin Herrenschmidt 		    (unsigned long)region.start,
161c40a22e0SBenjamin Herrenschmidt 		    (unsigned long)region.end);
1621da177e4SLinus Torvalds 	}
1631da177e4SLinus Torvalds 	else {
1641da177e4SLinus Torvalds 		/* Clear upper 16 bits of I/O base/limit. */
1651da177e4SLinus Torvalds 		io_upper16 = 0;
1661da177e4SLinus Torvalds 		l = 0x00f0;
16780ccba11SBjorn Helgaas 		dev_info(&bridge->dev, "  IO window: disabled\n");
1681da177e4SLinus Torvalds 	}
1691da177e4SLinus Torvalds 	/* Temporarily disable the I/O range before updating PCI_IO_BASE. */
1701da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
1711da177e4SLinus Torvalds 	/* Update lower 16 bits of I/O base/limit. */
1721da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE, l);
1731da177e4SLinus Torvalds 	/* Update upper 16 bits of I/O base/limit. */
1741da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	/* Set up the top and bottom of the PCI Memory segment
1771da177e4SLinus Torvalds 	   for this bus. */
1781da177e4SLinus Torvalds 	pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
1791da177e4SLinus Torvalds 	if (bus->resource[1]->flags & IORESOURCE_MEM) {
1801da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
1811da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
18280ccba11SBjorn Helgaas 		dev_info(&bridge->dev, "  MEM window: %#08lx-%#08lx\n",
183c40a22e0SBenjamin Herrenschmidt 		    (unsigned long)region.start,
184c40a22e0SBenjamin Herrenschmidt 		    (unsigned long)region.end);
1851da177e4SLinus Torvalds 	}
1861da177e4SLinus Torvalds 	else {
1871da177e4SLinus Torvalds 		l = 0x0000fff0;
18880ccba11SBjorn Helgaas 		dev_info(&bridge->dev, "  MEM window: disabled\n");
1891da177e4SLinus Torvalds 	}
1901da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	/* Clear out the upper 32 bits of PREF limit.
1931da177e4SLinus Torvalds 	   If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
1941da177e4SLinus Torvalds 	   disables PREF range, which is ok. */
1951da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	/* Set up PREF base/limit. */
198c40a22e0SBenjamin Herrenschmidt 	bu = lu = 0;
1991da177e4SLinus Torvalds 	pcibios_resource_to_bus(bridge, &region, bus->resource[2]);
2001da177e4SLinus Torvalds 	if (bus->resource[2]->flags & IORESOURCE_PREFETCH) {
2011da177e4SLinus Torvalds 		l = (region.start >> 16) & 0xfff0;
2021da177e4SLinus Torvalds 		l |= region.end & 0xfff00000;
20313d36c24SAndrew Morton 		bu = upper_32_bits(region.start);
20413d36c24SAndrew Morton 		lu = upper_32_bits(region.end);
20580ccba11SBjorn Helgaas 		dev_info(&bridge->dev, "  PREFETCH window: %#016llx-%#016llx\n",
206c40a22e0SBenjamin Herrenschmidt 		    (unsigned long long)region.start,
207c40a22e0SBenjamin Herrenschmidt 		    (unsigned long long)region.end);
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 	else {
2101da177e4SLinus Torvalds 		l = 0x0000fff0;
21180ccba11SBjorn Helgaas 		dev_info(&bridge->dev, "  PREFETCH window: disabled\n");
2121da177e4SLinus Torvalds 	}
2131da177e4SLinus Torvalds 	pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
2141da177e4SLinus Torvalds 
215c40a22e0SBenjamin Herrenschmidt 	/* Set the upper 32 bits of PREF base & limit. */
216c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
217c40a22e0SBenjamin Herrenschmidt 	pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and
2231da177e4SLinus Torvalds    prefetchable memory ranges. If not, the respective
2241da177e4SLinus Torvalds    base/limit registers must be read-only and read as 0. */
22596bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus)
2261da177e4SLinus Torvalds {
2271da177e4SLinus Torvalds 	u16 io;
2281da177e4SLinus Torvalds 	u32 pmem;
2291da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
2301da177e4SLinus Torvalds 	struct resource *b_res;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
2331da177e4SLinus Torvalds 	b_res[1].flags |= IORESOURCE_MEM;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_IO_BASE, &io);
2361da177e4SLinus Torvalds 	if (!io) {
2371da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
2381da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_IO_BASE, &io);
2391da177e4SLinus Torvalds  		pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
2401da177e4SLinus Torvalds  	}
2411da177e4SLinus Torvalds  	if (io)
2421da177e4SLinus Torvalds 		b_res[0].flags |= IORESOURCE_IO;
2431da177e4SLinus Torvalds 	/*  DECchip 21050 pass 2 errata: the bridge may miss an address
2441da177e4SLinus Torvalds 	    disconnect boundary by one PCI data phase.
2451da177e4SLinus Torvalds 	    Workaround: do not use prefetching on this device. */
2461da177e4SLinus Torvalds 	if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
2471da177e4SLinus Torvalds 		return;
2481da177e4SLinus Torvalds 	pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
2491da177e4SLinus Torvalds 	if (!pmem) {
2501da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
2511da177e4SLinus Torvalds 					       0xfff0fff0);
2521da177e4SLinus Torvalds 		pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
2531da177e4SLinus Torvalds 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds 	if (pmem)
2561da177e4SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds /* Helper function for sizing routines: find first available
2601da177e4SLinus Torvalds    bus resource of a given type. Note: we intentionally skip
2611da177e4SLinus Torvalds    the bus resources which have already been assigned (that is,
2621da177e4SLinus Torvalds    have non-NULL parent resource). */
26396bde06aSSam Ravnborg static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
2641da177e4SLinus Torvalds {
2651da177e4SLinus Torvalds 	int i;
2661da177e4SLinus Torvalds 	struct resource *r;
2671da177e4SLinus Torvalds 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
2681da177e4SLinus Torvalds 				  IORESOURCE_PREFETCH;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
2711da177e4SLinus Torvalds 		r = bus->resource[i];
272299de034SIvan Kokshaysky 		if (r == &ioport_resource || r == &iomem_resource)
273299de034SIvan Kokshaysky 			continue;
2741da177e4SLinus Torvalds 		if (r && (r->flags & type_mask) == type && !r->parent)
2751da177e4SLinus Torvalds 			return r;
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 	return NULL;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds /* Sizing the IO windows of the PCI-PCI bridge is trivial,
2811da177e4SLinus Torvalds    since these windows have 4K granularity and the IO ranges
2821da177e4SLinus Torvalds    of non-bridge PCI devices are limited to 256 bytes.
2831da177e4SLinus Torvalds    We must be careful with the ISA aliasing though. */
28496bde06aSSam Ravnborg static void pbus_size_io(struct pci_bus *bus)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	struct pci_dev *dev;
2871da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
2881da177e4SLinus Torvalds 	unsigned long size = 0, size1 = 0;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	if (!b_res)
2911da177e4SLinus Torvalds  		return;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
2941da177e4SLinus Torvalds 		int i;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
2971da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
2981da177e4SLinus Torvalds 			unsigned long r_size;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 			if (r->parent || !(r->flags & IORESOURCE_IO))
3011da177e4SLinus Torvalds 				continue;
3021da177e4SLinus Torvalds 			r_size = r->end - r->start + 1;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 			if (r_size < 0x400)
3051da177e4SLinus Torvalds 				/* Might be re-aligned for ISA */
3061da177e4SLinus Torvalds 				size += r_size;
3071da177e4SLinus Torvalds 			else
3081da177e4SLinus Torvalds 				size1 += r_size;
3091da177e4SLinus Torvalds 		}
3101da177e4SLinus Torvalds 	}
3111da177e4SLinus Torvalds /* To be fixed in 2.5: we should have sort of HAVE_ISA
3121da177e4SLinus Torvalds    flag in the struct pci_bus. */
3131da177e4SLinus Torvalds #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
3141da177e4SLinus Torvalds 	size = (size & 0xff) + ((size & ~0xffUL) << 2);
3151da177e4SLinus Torvalds #endif
3166f6f8c2fSMilind Arun Choudhary 	size = ALIGN(size + size1, 4096);
3171da177e4SLinus Torvalds 	if (!size) {
3181da177e4SLinus Torvalds 		b_res->flags = 0;
3191da177e4SLinus Torvalds 		return;
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds 	/* Alignment of the IO window is always 4K */
3221da177e4SLinus Torvalds 	b_res->start = 4096;
3231da177e4SLinus Torvalds 	b_res->end = b_res->start + size - 1;
32488452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds /* Calculate the size of the bus and minimal alignment which
3281da177e4SLinus Torvalds    guarantees that all child resources fit in this size. */
32996bde06aSSam Ravnborg static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, unsigned long type)
3301da177e4SLinus Torvalds {
3311da177e4SLinus Torvalds 	struct pci_dev *dev;
332c40a22e0SBenjamin Herrenschmidt 	resource_size_t min_align, align, size;
333c40a22e0SBenjamin Herrenschmidt 	resource_size_t aligns[12];	/* Alignments from 1Mb to 2Gb */
3341da177e4SLinus Torvalds 	int order, max_order;
3351da177e4SLinus Torvalds 	struct resource *b_res = find_free_bus_resource(bus, type);
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	if (!b_res)
3381da177e4SLinus Torvalds 		return 0;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	memset(aligns, 0, sizeof(aligns));
3411da177e4SLinus Torvalds 	max_order = 0;
3421da177e4SLinus Torvalds 	size = 0;
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
3451da177e4SLinus Torvalds 		int i;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
3481da177e4SLinus Torvalds 			struct resource *r = &dev->resource[i];
349c40a22e0SBenjamin Herrenschmidt 			resource_size_t r_size;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 			if (r->parent || (r->flags & mask) != type)
3521da177e4SLinus Torvalds 				continue;
3531da177e4SLinus Torvalds 			r_size = r->end - r->start + 1;
3541da177e4SLinus Torvalds 			/* For bridges size != alignment */
3555f17cfceSLinus Torvalds 			align = resource_alignment(r);
3561da177e4SLinus Torvalds 			order = __ffs(align) - 20;
3571da177e4SLinus Torvalds 			if (order > 11) {
3585f17cfceSLinus Torvalds 				dev_warn(&dev->dev, "BAR %d bad alignment %llx: "
359*096e6f67SBenjamin Herrenschmidt 					 "%pR\n", i, (unsigned long long)align, r);
3601da177e4SLinus Torvalds 				r->flags = 0;
3611da177e4SLinus Torvalds 				continue;
3621da177e4SLinus Torvalds 			}
3631da177e4SLinus Torvalds 			size += r_size;
3641da177e4SLinus Torvalds 			if (order < 0)
3651da177e4SLinus Torvalds 				order = 0;
3661da177e4SLinus Torvalds 			/* Exclude ranges with size > align from
3671da177e4SLinus Torvalds 			   calculation of the alignment. */
3681da177e4SLinus Torvalds 			if (r_size == align)
3691da177e4SLinus Torvalds 				aligns[order] += align;
3701da177e4SLinus Torvalds 			if (order > max_order)
3711da177e4SLinus Torvalds 				max_order = order;
3721da177e4SLinus Torvalds 		}
3731da177e4SLinus Torvalds 	}
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	align = 0;
3761da177e4SLinus Torvalds 	min_align = 0;
3771da177e4SLinus Torvalds 	for (order = 0; order <= max_order; order++) {
3788308c54dSJeremy Fitzhardinge 		resource_size_t align1 = 1;
3798308c54dSJeremy Fitzhardinge 
3808308c54dSJeremy Fitzhardinge 		align1 <<= (order + 20);
3818308c54dSJeremy Fitzhardinge 
3821da177e4SLinus Torvalds 		if (!align)
3831da177e4SLinus Torvalds 			min_align = align1;
3846f6f8c2fSMilind Arun Choudhary 		else if (ALIGN(align + min_align, min_align) < align1)
3851da177e4SLinus Torvalds 			min_align = align1 >> 1;
3861da177e4SLinus Torvalds 		align += aligns[order];
3871da177e4SLinus Torvalds 	}
3886f6f8c2fSMilind Arun Choudhary 	size = ALIGN(size, min_align);
3891da177e4SLinus Torvalds 	if (!size) {
3901da177e4SLinus Torvalds 		b_res->flags = 0;
3911da177e4SLinus Torvalds 		return 1;
3921da177e4SLinus Torvalds 	}
3931da177e4SLinus Torvalds 	b_res->start = min_align;
3941da177e4SLinus Torvalds 	b_res->end = size + min_align - 1;
39588452565SIvan Kokshaysky 	b_res->flags |= IORESOURCE_STARTALIGN;
3961da177e4SLinus Torvalds 	return 1;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3995468ae61SAdrian Bunk static void pci_bus_size_cardbus(struct pci_bus *bus)
4001da177e4SLinus Torvalds {
4011da177e4SLinus Torvalds 	struct pci_dev *bridge = bus->self;
4021da177e4SLinus Torvalds 	struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
4031da177e4SLinus Torvalds 	u16 ctrl;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	/*
4061da177e4SLinus Torvalds 	 * Reserve some resources for CardBus.  We reserve
4071da177e4SLinus Torvalds 	 * a fixed amount of bus space for CardBus bridges.
4081da177e4SLinus Torvalds 	 */
409934b7024SLinus Torvalds 	b_res[0].start = 0;
410934b7024SLinus Torvalds 	b_res[0].end = pci_cardbus_io_size - 1;
411934b7024SLinus Torvalds 	b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
4121da177e4SLinus Torvalds 
413934b7024SLinus Torvalds 	b_res[1].start = 0;
414934b7024SLinus Torvalds 	b_res[1].end = pci_cardbus_io_size - 1;
415934b7024SLinus Torvalds 	b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	/*
4181da177e4SLinus Torvalds 	 * Check whether prefetchable memory is supported
4191da177e4SLinus Torvalds 	 * by this bridge.
4201da177e4SLinus Torvalds 	 */
4211da177e4SLinus Torvalds 	pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
4221da177e4SLinus Torvalds 	if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
4231da177e4SLinus Torvalds 		ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
4241da177e4SLinus Torvalds 		pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
4251da177e4SLinus Torvalds 		pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	/*
4291da177e4SLinus Torvalds 	 * If we have prefetchable memory support, allocate
4301da177e4SLinus Torvalds 	 * two regions.  Otherwise, allocate one region of
4311da177e4SLinus Torvalds 	 * twice the size.
4321da177e4SLinus Torvalds 	 */
4331da177e4SLinus Torvalds 	if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
434934b7024SLinus Torvalds 		b_res[2].start = 0;
435934b7024SLinus Torvalds 		b_res[2].end = pci_cardbus_mem_size - 1;
436934b7024SLinus Torvalds 		b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
4371da177e4SLinus Torvalds 
438934b7024SLinus Torvalds 		b_res[3].start = 0;
439934b7024SLinus Torvalds 		b_res[3].end = pci_cardbus_mem_size - 1;
440934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
4411da177e4SLinus Torvalds 	} else {
442934b7024SLinus Torvalds 		b_res[3].start = 0;
443934b7024SLinus Torvalds 		b_res[3].end = pci_cardbus_mem_size * 2 - 1;
444934b7024SLinus Torvalds 		b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
448451124a7SSam Ravnborg void __ref pci_bus_size_bridges(struct pci_bus *bus)
4491da177e4SLinus Torvalds {
4501da177e4SLinus Torvalds 	struct pci_dev *dev;
4511da177e4SLinus Torvalds 	unsigned long mask, prefmask;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
4541da177e4SLinus Torvalds 		struct pci_bus *b = dev->subordinate;
4551da177e4SLinus Torvalds 		if (!b)
4561da177e4SLinus Torvalds 			continue;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 		switch (dev->class >> 8) {
4591da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
4601da177e4SLinus Torvalds 			pci_bus_size_cardbus(b);
4611da177e4SLinus Torvalds 			break;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
4641da177e4SLinus Torvalds 		default:
4651da177e4SLinus Torvalds 			pci_bus_size_bridges(b);
4661da177e4SLinus Torvalds 			break;
4671da177e4SLinus Torvalds 		}
4681da177e4SLinus Torvalds 	}
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	/* The root bus? */
4711da177e4SLinus Torvalds 	if (!bus->self)
4721da177e4SLinus Torvalds 		return;
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	switch (bus->self->class >> 8) {
4751da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_CARDBUS:
4761da177e4SLinus Torvalds 		/* don't size cardbuses yet. */
4771da177e4SLinus Torvalds 		break;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	case PCI_CLASS_BRIDGE_PCI:
4801da177e4SLinus Torvalds 		pci_bridge_check_ranges(bus);
4811da177e4SLinus Torvalds 	default:
4821da177e4SLinus Torvalds 		pbus_size_io(bus);
4831da177e4SLinus Torvalds 		/* If the bridge supports prefetchable range, size it
4841da177e4SLinus Torvalds 		   separately. If it doesn't, or its prefetchable window
4851da177e4SLinus Torvalds 		   has already been allocated by arch code, try
4861da177e4SLinus Torvalds 		   non-prefetchable range for both types of PCI memory
4871da177e4SLinus Torvalds 		   resources. */
4881da177e4SLinus Torvalds 		mask = IORESOURCE_MEM;
4891da177e4SLinus Torvalds 		prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
4901da177e4SLinus Torvalds 		if (pbus_size_mem(bus, prefmask, prefmask))
4911da177e4SLinus Torvalds 			mask = prefmask; /* Success, size non-prefetch only. */
4921da177e4SLinus Torvalds 		pbus_size_mem(bus, mask, IORESOURCE_MEM);
4931da177e4SLinus Torvalds 		break;
4941da177e4SLinus Torvalds 	}
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges);
4971da177e4SLinus Torvalds 
498451124a7SSam Ravnborg void __ref pci_bus_assign_resources(struct pci_bus *bus)
4991da177e4SLinus Torvalds {
5001da177e4SLinus Torvalds 	struct pci_bus *b;
5011da177e4SLinus Torvalds 	struct pci_dev *dev;
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	pbus_assign_resources_sorted(bus);
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	list_for_each_entry(dev, &bus->devices, bus_list) {
5061da177e4SLinus Torvalds 		b = dev->subordinate;
5071da177e4SLinus Torvalds 		if (!b)
5081da177e4SLinus Torvalds 			continue;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 		pci_bus_assign_resources(b);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 		switch (dev->class >> 8) {
5131da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_PCI:
5141da177e4SLinus Torvalds 			pci_setup_bridge(b);
5151da177e4SLinus Torvalds 			break;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 		case PCI_CLASS_BRIDGE_CARDBUS:
5181da177e4SLinus Torvalds 			pci_setup_cardbus(b);
5191da177e4SLinus Torvalds 			break;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 		default:
52280ccba11SBjorn Helgaas 			dev_info(&dev->dev, "not setting up bridge for bus "
52380ccba11SBjorn Helgaas 				 "%04x:%02x\n", pci_domain_nr(b), b->number);
5241da177e4SLinus Torvalds 			break;
5251da177e4SLinus Torvalds 		}
5261da177e4SLinus Torvalds 	}
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources);
5291da177e4SLinus Torvalds 
53076fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus)
53176fbc263SYinghai Lu {
53276fbc263SYinghai Lu         int i;
53376fbc263SYinghai Lu 
53476fbc263SYinghai Lu         for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
53576fbc263SYinghai Lu                 struct resource *res = bus->resource[i];
53676fbc263SYinghai Lu                 if (!res)
53776fbc263SYinghai Lu                         continue;
53876fbc263SYinghai Lu 
539*096e6f67SBenjamin Herrenschmidt 		printk(KERN_INFO "bus: %02x index %x %s: %pR\n",
54053bc8870SJohann Felix Soden 		       bus->number, i,
541*096e6f67SBenjamin Herrenschmidt 		       (res->flags & IORESOURCE_IO) ? "io port" : "mmio", res);
54276fbc263SYinghai Lu         }
54376fbc263SYinghai Lu }
54476fbc263SYinghai Lu 
54576fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus)
54676fbc263SYinghai Lu {
54776fbc263SYinghai Lu 	struct pci_bus *b;
54876fbc263SYinghai Lu 	struct pci_dev *dev;
54976fbc263SYinghai Lu 
55076fbc263SYinghai Lu 
55176fbc263SYinghai Lu 	pci_bus_dump_res(bus);
55276fbc263SYinghai Lu 
55376fbc263SYinghai Lu 	list_for_each_entry(dev, &bus->devices, bus_list) {
55476fbc263SYinghai Lu 		b = dev->subordinate;
55576fbc263SYinghai Lu 		if (!b)
55676fbc263SYinghai Lu 			continue;
55776fbc263SYinghai Lu 
55876fbc263SYinghai Lu 		pci_bus_dump_resources(b);
55976fbc263SYinghai Lu 	}
56076fbc263SYinghai Lu }
56176fbc263SYinghai Lu 
5621da177e4SLinus Torvalds void __init
5631da177e4SLinus Torvalds pci_assign_unassigned_resources(void)
5641da177e4SLinus Torvalds {
5651da177e4SLinus Torvalds 	struct pci_bus *bus;
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	/* Depth first, calculate sizes and alignments of all
5681da177e4SLinus Torvalds 	   subordinate buses. */
5691da177e4SLinus Torvalds 	list_for_each_entry(bus, &pci_root_buses, node) {
5701da177e4SLinus Torvalds 		pci_bus_size_bridges(bus);
5711da177e4SLinus Torvalds 	}
5721da177e4SLinus Torvalds 	/* Depth last, allocate resources and update the hardware. */
5731da177e4SLinus Torvalds 	list_for_each_entry(bus, &pci_root_buses, node) {
5741da177e4SLinus Torvalds 		pci_bus_assign_resources(bus);
5751da177e4SLinus Torvalds 		pci_enable_bridges(bus);
5761da177e4SLinus Torvalds 	}
57776fbc263SYinghai Lu 
57876fbc263SYinghai Lu 	/* dump the resource on buses */
57976fbc263SYinghai Lu 	list_for_each_entry(bus, &pci_root_buses, node) {
58076fbc263SYinghai Lu 		pci_bus_dump_resources(bus);
58176fbc263SYinghai Lu 	}
5821da177e4SLinus Torvalds }
583