xref: /openbmc/linux/arch/ia64/pci/pci.c (revision adcd7403)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * pci.c - Low-Level PCI Access in IA-64
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Derived from bios32.c of i386 tree.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * (c) Copyright 2002, 2005 Hewlett-Packard Development Company, L.P.
71da177e4SLinus Torvalds  *	David Mosberger-Tang <davidm@hpl.hp.com>
81da177e4SLinus Torvalds  *	Bjorn Helgaas <bjorn.helgaas@hp.com>
91da177e4SLinus Torvalds  * Copyright (C) 2004 Silicon Graphics, Inc.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * Note: Above list of copyright holders is incomplete...
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/acpi.h>
151da177e4SLinus Torvalds #include <linux/types.h>
161da177e4SLinus Torvalds #include <linux/kernel.h>
171da177e4SLinus Torvalds #include <linux/pci.h>
181da177e4SLinus Torvalds #include <linux/init.h>
191da177e4SLinus Torvalds #include <linux/ioport.h>
201da177e4SLinus Torvalds #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/spinlock.h>
22175add19SJohn Keller #include <linux/bootmem.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <asm/machvec.h>
251da177e4SLinus Torvalds #include <asm/page.h>
261da177e4SLinus Torvalds #include <asm/system.h>
271da177e4SLinus Torvalds #include <asm/io.h>
281da177e4SLinus Torvalds #include <asm/sal.h>
291da177e4SLinus Torvalds #include <asm/smp.h>
301da177e4SLinus Torvalds #include <asm/irq.h>
311da177e4SLinus Torvalds #include <asm/hw_irq.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds /*
341da177e4SLinus Torvalds  * Low-level SAL-based PCI configuration access functions. Note that SAL
351da177e4SLinus Torvalds  * calls are already serialized (via sal_lock), so we don't need another
361da177e4SLinus Torvalds  * synchronization mechanism here.
371da177e4SLinus Torvalds  */
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #define PCI_SAL_ADDRESS(seg, bus, devfn, reg)		\
401da177e4SLinus Torvalds 	(((u64) seg << 24) | (bus << 16) | (devfn << 8) | (reg))
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds /* SAL 3.2 adds support for extended config space. */
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #define PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg)	\
451da177e4SLinus Torvalds 	(((u64) seg << 28) | (bus << 20) | (devfn << 12) | (reg))
461da177e4SLinus Torvalds 
47b6ce068aSMatthew Wilcox int raw_pci_read(unsigned int seg, unsigned int bus, unsigned int devfn,
481da177e4SLinus Torvalds 	      int reg, int len, u32 *value)
491da177e4SLinus Torvalds {
501da177e4SLinus Torvalds 	u64 addr, data = 0;
511da177e4SLinus Torvalds 	int mode, result;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	if (!value || (seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
541da177e4SLinus Torvalds 		return -EINVAL;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	if ((seg | reg) <= 255) {
571da177e4SLinus Torvalds 		addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
581da177e4SLinus Torvalds 		mode = 0;
59adcd7403SMatthew Wilcox 	} else if (sal_revision >= SAL_VERSION_CODE(3,2)) {
601da177e4SLinus Torvalds 		addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
611da177e4SLinus Torvalds 		mode = 1;
62adcd7403SMatthew Wilcox 	} else {
63adcd7403SMatthew Wilcox 		return -EINVAL;
641da177e4SLinus Torvalds 	}
65adcd7403SMatthew Wilcox 
661da177e4SLinus Torvalds 	result = ia64_sal_pci_config_read(addr, mode, len, &data);
671da177e4SLinus Torvalds 	if (result != 0)
681da177e4SLinus Torvalds 		return -EINVAL;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 	*value = (u32) data;
711da177e4SLinus Torvalds 	return 0;
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds 
74b6ce068aSMatthew Wilcox int raw_pci_write(unsigned int seg, unsigned int bus, unsigned int devfn,
751da177e4SLinus Torvalds 	       int reg, int len, u32 value)
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds 	u64 addr;
781da177e4SLinus Torvalds 	int mode, result;
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	if ((seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
811da177e4SLinus Torvalds 		return -EINVAL;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	if ((seg | reg) <= 255) {
841da177e4SLinus Torvalds 		addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
851da177e4SLinus Torvalds 		mode = 0;
86adcd7403SMatthew Wilcox 	} else if (sal_revision >= SAL_VERSION_CODE(3,2)) {
871da177e4SLinus Torvalds 		addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
881da177e4SLinus Torvalds 		mode = 1;
89adcd7403SMatthew Wilcox 	} else {
90adcd7403SMatthew Wilcox 		return -EINVAL;
911da177e4SLinus Torvalds 	}
921da177e4SLinus Torvalds 	result = ia64_sal_pci_config_write(addr, mode, len, value);
931da177e4SLinus Torvalds 	if (result != 0)
941da177e4SLinus Torvalds 		return -EINVAL;
951da177e4SLinus Torvalds 	return 0;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
98b6ce068aSMatthew Wilcox static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
99b6ce068aSMatthew Wilcox 							int size, u32 *value)
1001da177e4SLinus Torvalds {
101b6ce068aSMatthew Wilcox 	return raw_pci_read(pci_domain_nr(bus), bus->number,
1021da177e4SLinus Torvalds 				 devfn, where, size, value);
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
105b6ce068aSMatthew Wilcox static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
106b6ce068aSMatthew Wilcox 							int size, u32 value)
1071da177e4SLinus Torvalds {
108b6ce068aSMatthew Wilcox 	return raw_pci_write(pci_domain_nr(bus), bus->number,
1091da177e4SLinus Torvalds 				  devfn, where, size, value);
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds struct pci_ops pci_root_ops = {
1131da177e4SLinus Torvalds 	.read = pci_read,
1141da177e4SLinus Torvalds 	.write = pci_write,
1151da177e4SLinus Torvalds };
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* Called by ACPI when it finds a new root bus.  */
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds static struct pci_controller * __devinit
1201da177e4SLinus Torvalds alloc_pci_controller (int seg)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	struct pci_controller *controller;
1231da177e4SLinus Torvalds 
12452fd9108SYan Burman 	controller = kzalloc(sizeof(*controller), GFP_KERNEL);
1251da177e4SLinus Torvalds 	if (!controller)
1261da177e4SLinus Torvalds 		return NULL;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	controller->segment = seg;
129514604c6SChristoph Lameter 	controller->node = -1;
1301da177e4SLinus Torvalds 	return controller;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1334f41d5a4SBjorn Helgaas struct pci_root_info {
1344f41d5a4SBjorn Helgaas 	struct pci_controller *controller;
1354f41d5a4SBjorn Helgaas 	char *name;
1364f41d5a4SBjorn Helgaas };
1374f41d5a4SBjorn Helgaas 
1384f41d5a4SBjorn Helgaas static unsigned int
1394f41d5a4SBjorn Helgaas new_space (u64 phys_base, int sparse)
1401da177e4SLinus Torvalds {
1414f41d5a4SBjorn Helgaas 	u64 mmio_base;
1421da177e4SLinus Torvalds 	int i;
1431da177e4SLinus Torvalds 
1444f41d5a4SBjorn Helgaas 	if (phys_base == 0)
1454f41d5a4SBjorn Helgaas 		return 0;	/* legacy I/O port space */
1461da177e4SLinus Torvalds 
1474f41d5a4SBjorn Helgaas 	mmio_base = (u64) ioremap(phys_base, 0);
1481da177e4SLinus Torvalds 	for (i = 0; i < num_io_spaces; i++)
1494f41d5a4SBjorn Helgaas 		if (io_space[i].mmio_base == mmio_base &&
1501da177e4SLinus Torvalds 		    io_space[i].sparse == sparse)
1514f41d5a4SBjorn Helgaas 			return i;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	if (num_io_spaces == MAX_IO_SPACES) {
1544f41d5a4SBjorn Helgaas 		printk(KERN_ERR "PCI: Too many IO port spaces "
1554f41d5a4SBjorn Helgaas 			"(MAX_IO_SPACES=%lu)\n", MAX_IO_SPACES);
1561da177e4SLinus Torvalds 		return ~0;
1571da177e4SLinus Torvalds 	}
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	i = num_io_spaces++;
1604f41d5a4SBjorn Helgaas 	io_space[i].mmio_base = mmio_base;
1611da177e4SLinus Torvalds 	io_space[i].sparse = sparse;
1621da177e4SLinus Torvalds 
1634f41d5a4SBjorn Helgaas 	return i;
1644f41d5a4SBjorn Helgaas }
1654f41d5a4SBjorn Helgaas 
1664f41d5a4SBjorn Helgaas static u64 __devinit
1674f41d5a4SBjorn Helgaas add_io_space (struct pci_root_info *info, struct acpi_resource_address64 *addr)
1684f41d5a4SBjorn Helgaas {
1694f41d5a4SBjorn Helgaas 	struct resource *resource;
1704f41d5a4SBjorn Helgaas 	char *name;
171e088a4adSMatthew Wilcox 	unsigned long base, min, max, base_port;
1724f41d5a4SBjorn Helgaas 	unsigned int sparse = 0, space_nr, len;
1734f41d5a4SBjorn Helgaas 
1744f41d5a4SBjorn Helgaas 	resource = kzalloc(sizeof(*resource), GFP_KERNEL);
1754f41d5a4SBjorn Helgaas 	if (!resource) {
1764f41d5a4SBjorn Helgaas 		printk(KERN_ERR "PCI: No memory for %s I/O port space\n",
1774f41d5a4SBjorn Helgaas 			info->name);
1784f41d5a4SBjorn Helgaas 		goto out;
1794f41d5a4SBjorn Helgaas 	}
1804f41d5a4SBjorn Helgaas 
1814f41d5a4SBjorn Helgaas 	len = strlen(info->name) + 32;
1824f41d5a4SBjorn Helgaas 	name = kzalloc(len, GFP_KERNEL);
1834f41d5a4SBjorn Helgaas 	if (!name) {
1844f41d5a4SBjorn Helgaas 		printk(KERN_ERR "PCI: No memory for %s I/O port space name\n",
1854f41d5a4SBjorn Helgaas 			info->name);
1864f41d5a4SBjorn Helgaas 		goto free_resource;
1874f41d5a4SBjorn Helgaas 	}
1884f41d5a4SBjorn Helgaas 
18950eca3ebSBob Moore 	min = addr->minimum;
1904f41d5a4SBjorn Helgaas 	max = min + addr->address_length - 1;
1910897831bSBob Moore 	if (addr->info.io.translation_type == ACPI_SPARSE_TRANSLATION)
1924f41d5a4SBjorn Helgaas 		sparse = 1;
1934f41d5a4SBjorn Helgaas 
19450eca3ebSBob Moore 	space_nr = new_space(addr->translation_offset, sparse);
1954f41d5a4SBjorn Helgaas 	if (space_nr == ~0)
1964f41d5a4SBjorn Helgaas 		goto free_name;
1974f41d5a4SBjorn Helgaas 
1984f41d5a4SBjorn Helgaas 	base = __pa(io_space[space_nr].mmio_base);
1994f41d5a4SBjorn Helgaas 	base_port = IO_SPACE_BASE(space_nr);
2004f41d5a4SBjorn Helgaas 	snprintf(name, len, "%s I/O Ports %08lx-%08lx", info->name,
2014f41d5a4SBjorn Helgaas 		base_port + min, base_port + max);
2024f41d5a4SBjorn Helgaas 
2034f41d5a4SBjorn Helgaas 	/*
2044f41d5a4SBjorn Helgaas 	 * The SDM guarantees the legacy 0-64K space is sparse, but if the
2054f41d5a4SBjorn Helgaas 	 * mapping is done by the processor (not the bridge), ACPI may not
2064f41d5a4SBjorn Helgaas 	 * mark it as sparse.
2074f41d5a4SBjorn Helgaas 	 */
2084f41d5a4SBjorn Helgaas 	if (space_nr == 0)
2094f41d5a4SBjorn Helgaas 		sparse = 1;
2104f41d5a4SBjorn Helgaas 
2114f41d5a4SBjorn Helgaas 	resource->name  = name;
2124f41d5a4SBjorn Helgaas 	resource->flags = IORESOURCE_MEM;
2134f41d5a4SBjorn Helgaas 	resource->start = base + (sparse ? IO_SPACE_SPARSE_ENCODING(min) : min);
2144f41d5a4SBjorn Helgaas 	resource->end   = base + (sparse ? IO_SPACE_SPARSE_ENCODING(max) : max);
2154f41d5a4SBjorn Helgaas 	insert_resource(&iomem_resource, resource);
2164f41d5a4SBjorn Helgaas 
2174f41d5a4SBjorn Helgaas 	return base_port;
2184f41d5a4SBjorn Helgaas 
2194f41d5a4SBjorn Helgaas free_name:
2204f41d5a4SBjorn Helgaas 	kfree(name);
2214f41d5a4SBjorn Helgaas free_resource:
2224f41d5a4SBjorn Helgaas 	kfree(resource);
2234f41d5a4SBjorn Helgaas out:
2244f41d5a4SBjorn Helgaas 	return ~0;
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
227463eb297SBjorn Helgaas static acpi_status __devinit resource_to_window(struct acpi_resource *resource,
228463eb297SBjorn Helgaas 	struct acpi_resource_address64 *addr)
229463eb297SBjorn Helgaas {
230463eb297SBjorn Helgaas 	acpi_status status;
231463eb297SBjorn Helgaas 
232463eb297SBjorn Helgaas 	/*
233463eb297SBjorn Helgaas 	 * We're only interested in _CRS descriptors that are
234463eb297SBjorn Helgaas 	 *	- address space descriptors for memory or I/O space
235463eb297SBjorn Helgaas 	 *	- non-zero size
236463eb297SBjorn Helgaas 	 *	- producers, i.e., the address space is routed downstream,
237463eb297SBjorn Helgaas 	 *	  not consumed by the bridge itself
238463eb297SBjorn Helgaas 	 */
239463eb297SBjorn Helgaas 	status = acpi_resource_to_address64(resource, addr);
240463eb297SBjorn Helgaas 	if (ACPI_SUCCESS(status) &&
241463eb297SBjorn Helgaas 	    (addr->resource_type == ACPI_MEMORY_RANGE ||
242463eb297SBjorn Helgaas 	     addr->resource_type == ACPI_IO_RANGE) &&
243463eb297SBjorn Helgaas 	    addr->address_length &&
244463eb297SBjorn Helgaas 	    addr->producer_consumer == ACPI_PRODUCER)
245463eb297SBjorn Helgaas 		return AE_OK;
246463eb297SBjorn Helgaas 
247463eb297SBjorn Helgaas 	return AE_ERROR;
248463eb297SBjorn Helgaas }
249463eb297SBjorn Helgaas 
2501da177e4SLinus Torvalds static acpi_status __devinit
2511da177e4SLinus Torvalds count_window (struct acpi_resource *resource, void *data)
2521da177e4SLinus Torvalds {
2531da177e4SLinus Torvalds 	unsigned int *windows = (unsigned int *) data;
2541da177e4SLinus Torvalds 	struct acpi_resource_address64 addr;
2551da177e4SLinus Torvalds 	acpi_status status;
2561da177e4SLinus Torvalds 
257463eb297SBjorn Helgaas 	status = resource_to_window(resource, &addr);
2581da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
2591da177e4SLinus Torvalds 		(*windows)++;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	return AE_OK;
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds static __devinit acpi_status add_window(struct acpi_resource *res, void *data)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	struct pci_root_info *info = data;
2671da177e4SLinus Torvalds 	struct pci_window *window;
2681da177e4SLinus Torvalds 	struct acpi_resource_address64 addr;
2691da177e4SLinus Torvalds 	acpi_status status;
2701da177e4SLinus Torvalds 	unsigned long flags, offset = 0;
2711da177e4SLinus Torvalds 	struct resource *root;
2721da177e4SLinus Torvalds 
273463eb297SBjorn Helgaas 	/* Return AE_OK for non-window resources to keep scanning for more */
274463eb297SBjorn Helgaas 	status = resource_to_window(res, &addr);
2751da177e4SLinus Torvalds 	if (!ACPI_SUCCESS(status))
2761da177e4SLinus Torvalds 		return AE_OK;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	if (addr.resource_type == ACPI_MEMORY_RANGE) {
2791da177e4SLinus Torvalds 		flags = IORESOURCE_MEM;
2801da177e4SLinus Torvalds 		root = &iomem_resource;
28150eca3ebSBob Moore 		offset = addr.translation_offset;
2821da177e4SLinus Torvalds 	} else if (addr.resource_type == ACPI_IO_RANGE) {
2831da177e4SLinus Torvalds 		flags = IORESOURCE_IO;
2841da177e4SLinus Torvalds 		root = &ioport_resource;
2854f41d5a4SBjorn Helgaas 		offset = add_io_space(info, &addr);
2861da177e4SLinus Torvalds 		if (offset == ~0)
2871da177e4SLinus Torvalds 			return AE_OK;
2881da177e4SLinus Torvalds 	} else
2891da177e4SLinus Torvalds 		return AE_OK;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	window = &info->controller->window[info->controller->windows++];
2921da177e4SLinus Torvalds 	window->resource.name = info->name;
2931da177e4SLinus Torvalds 	window->resource.flags = flags;
29450eca3ebSBob Moore 	window->resource.start = addr.minimum + offset;
2954f41d5a4SBjorn Helgaas 	window->resource.end = window->resource.start + addr.address_length - 1;
2961da177e4SLinus Torvalds 	window->resource.child = NULL;
2971da177e4SLinus Torvalds 	window->offset = offset;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	if (insert_resource(root, &window->resource)) {
300e088a4adSMatthew Wilcox 		printk(KERN_ERR "alloc 0x%llx-0x%llx from %s for %s failed\n",
3011da177e4SLinus Torvalds 			window->resource.start, window->resource.end,
3021da177e4SLinus Torvalds 			root->name, info->name);
3031da177e4SLinus Torvalds 	}
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	return AE_OK;
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds static void __devinit
3091da177e4SLinus Torvalds pcibios_setup_root_windows(struct pci_bus *bus, struct pci_controller *ctrl)
3101da177e4SLinus Torvalds {
3111da177e4SLinus Torvalds 	int i, j;
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	j = 0;
3141da177e4SLinus Torvalds 	for (i = 0; i < ctrl->windows; i++) {
3151da177e4SLinus Torvalds 		struct resource *res = &ctrl->window[i].resource;
3161da177e4SLinus Torvalds 		/* HP's firmware has a hack to work around a Windows bug.
3171da177e4SLinus Torvalds 		 * Ignore these tiny memory ranges */
3181da177e4SLinus Torvalds 		if ((res->flags & IORESOURCE_MEM) &&
3191da177e4SLinus Torvalds 		    (res->end - res->start < 16))
3201da177e4SLinus Torvalds 			continue;
3211da177e4SLinus Torvalds 		if (j >= PCI_BUS_NUM_RESOURCES) {
322e088a4adSMatthew Wilcox 			printk("Ignoring range [%#llx-%#llx] (%lx)\n",
323e088a4adSMatthew Wilcox 					res->start, res->end, res->flags);
3241da177e4SLinus Torvalds 			continue;
3251da177e4SLinus Torvalds 		}
3261da177e4SLinus Torvalds 		bus->resource[j++] = res;
3271da177e4SLinus Torvalds 	}
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds struct pci_bus * __devinit
3311da177e4SLinus Torvalds pci_acpi_scan_root(struct acpi_device *device, int domain, int bus)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	struct pci_controller *controller;
3341da177e4SLinus Torvalds 	unsigned int windows = 0;
3351da177e4SLinus Torvalds 	struct pci_bus *pbus;
3361da177e4SLinus Torvalds 	char *name;
337514604c6SChristoph Lameter 	int pxm;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	controller = alloc_pci_controller(domain);
3401da177e4SLinus Torvalds 	if (!controller)
3411da177e4SLinus Torvalds 		goto out1;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	controller->acpi_handle = device->handle;
3441da177e4SLinus Torvalds 
345514604c6SChristoph Lameter 	pxm = acpi_get_pxm(controller->acpi_handle);
346514604c6SChristoph Lameter #ifdef CONFIG_NUMA
347514604c6SChristoph Lameter 	if (pxm >= 0)
348762834e8SYasunori Goto 		controller->node = pxm_to_node(pxm);
349514604c6SChristoph Lameter #endif
350514604c6SChristoph Lameter 
3511da177e4SLinus Torvalds 	acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_window,
3521da177e4SLinus Torvalds 			&windows);
353a66aa704SKenji Kaneshige 	if (windows) {
3548a20fd52SLuck, Tony 		struct pci_root_info info;
3558a20fd52SLuck, Tony 
356a66aa704SKenji Kaneshige 		controller->window =
357a66aa704SKenji Kaneshige 			kmalloc_node(sizeof(*controller->window) * windows,
358514604c6SChristoph Lameter 				     GFP_KERNEL, controller->node);
3591da177e4SLinus Torvalds 		if (!controller->window)
3601da177e4SLinus Torvalds 			goto out2;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 		name = kmalloc(16, GFP_KERNEL);
3631da177e4SLinus Torvalds 		if (!name)
3641da177e4SLinus Torvalds 			goto out3;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 		sprintf(name, "PCI Bus %04x:%02x", domain, bus);
3671da177e4SLinus Torvalds 		info.controller = controller;
3681da177e4SLinus Torvalds 		info.name = name;
3698a20fd52SLuck, Tony 		acpi_walk_resources(device->handle, METHOD_NAME__CRS,
3708a20fd52SLuck, Tony 			add_window, &info);
3718a20fd52SLuck, Tony 	}
372b87e81e5Syakui.zhao@intel.com 	/*
373b87e81e5Syakui.zhao@intel.com 	 * See arch/x86/pci/acpi.c.
374b87e81e5Syakui.zhao@intel.com 	 * The desired pci bus might already be scanned in a quirk. We
375b87e81e5Syakui.zhao@intel.com 	 * should handle the case here, but it appears that IA64 hasn't
376b87e81e5Syakui.zhao@intel.com 	 * such quirk. So we just ignore the case now.
377b87e81e5Syakui.zhao@intel.com 	 */
378c431ada4SRajesh Shah 	pbus = pci_scan_bus_parented(NULL, bus, &pci_root_ops, controller);
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	return pbus;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds out3:
3831da177e4SLinus Torvalds 	kfree(controller->window);
3841da177e4SLinus Torvalds out2:
3851da177e4SLinus Torvalds 	kfree(controller);
3861da177e4SLinus Torvalds out1:
3871da177e4SLinus Torvalds 	return NULL;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds void pcibios_resource_to_bus(struct pci_dev *dev,
3911da177e4SLinus Torvalds 		struct pci_bus_region *region, struct resource *res)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	struct pci_controller *controller = PCI_CONTROLLER(dev);
3941da177e4SLinus Torvalds 	unsigned long offset = 0;
3951da177e4SLinus Torvalds 	int i;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	for (i = 0; i < controller->windows; i++) {
3981da177e4SLinus Torvalds 		struct pci_window *window = &controller->window[i];
3991da177e4SLinus Torvalds 		if (!(window->resource.flags & res->flags))
4001da177e4SLinus Torvalds 			continue;
4011da177e4SLinus Torvalds 		if (window->resource.start > res->start)
4021da177e4SLinus Torvalds 			continue;
4031da177e4SLinus Torvalds 		if (window->resource.end < res->end)
4041da177e4SLinus Torvalds 			continue;
4051da177e4SLinus Torvalds 		offset = window->offset;
4061da177e4SLinus Torvalds 		break;
4071da177e4SLinus Torvalds 	}
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	region->start = res->start - offset;
4101da177e4SLinus Torvalds 	region->end = res->end - offset;
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds EXPORT_SYMBOL(pcibios_resource_to_bus);
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds void pcibios_bus_to_resource(struct pci_dev *dev,
4151da177e4SLinus Torvalds 		struct resource *res, struct pci_bus_region *region)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	struct pci_controller *controller = PCI_CONTROLLER(dev);
4181da177e4SLinus Torvalds 	unsigned long offset = 0;
4191da177e4SLinus Torvalds 	int i;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	for (i = 0; i < controller->windows; i++) {
4221da177e4SLinus Torvalds 		struct pci_window *window = &controller->window[i];
4231da177e4SLinus Torvalds 		if (!(window->resource.flags & res->flags))
4241da177e4SLinus Torvalds 			continue;
4251da177e4SLinus Torvalds 		if (window->resource.start - window->offset > region->start)
4261da177e4SLinus Torvalds 			continue;
4271da177e4SLinus Torvalds 		if (window->resource.end - window->offset < region->end)
4281da177e4SLinus Torvalds 			continue;
4291da177e4SLinus Torvalds 		offset = window->offset;
4301da177e4SLinus Torvalds 		break;
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	res->start = region->start + offset;
4341da177e4SLinus Torvalds 	res->end = region->end + offset;
4351da177e4SLinus Torvalds }
43641290c14SKeith Owens EXPORT_SYMBOL(pcibios_bus_to_resource);
4371da177e4SLinus Torvalds 
43871c3511cSRajesh Shah static int __devinit is_valid_resource(struct pci_dev *dev, int idx)
43971c3511cSRajesh Shah {
44071c3511cSRajesh Shah 	unsigned int i, type_mask = IORESOURCE_IO | IORESOURCE_MEM;
44171c3511cSRajesh Shah 	struct resource *devr = &dev->resource[idx];
44271c3511cSRajesh Shah 
44371c3511cSRajesh Shah 	if (!dev->bus)
44471c3511cSRajesh Shah 		return 0;
44571c3511cSRajesh Shah 	for (i=0; i<PCI_BUS_NUM_RESOURCES; i++) {
44671c3511cSRajesh Shah 		struct resource *busr = dev->bus->resource[i];
44771c3511cSRajesh Shah 
44871c3511cSRajesh Shah 		if (!busr || ((busr->flags ^ devr->flags) & type_mask))
44971c3511cSRajesh Shah 			continue;
45071c3511cSRajesh Shah 		if ((devr->start) && (devr->start >= busr->start) &&
45171c3511cSRajesh Shah 				(devr->end <= busr->end))
45271c3511cSRajesh Shah 			return 1;
45371c3511cSRajesh Shah 	}
45471c3511cSRajesh Shah 	return 0;
45571c3511cSRajesh Shah }
45671c3511cSRajesh Shah 
4577b9c8ba2SKenji Kaneshige static void __devinit
4587b9c8ba2SKenji Kaneshige pcibios_fixup_resources(struct pci_dev *dev, int start, int limit)
4591da177e4SLinus Torvalds {
4601da177e4SLinus Torvalds 	struct pci_bus_region region;
4611da177e4SLinus Torvalds 	int i;
4621da177e4SLinus Torvalds 
4637b9c8ba2SKenji Kaneshige 	for (i = start; i < limit; i++) {
4641da177e4SLinus Torvalds 		if (!dev->resource[i].flags)
4651da177e4SLinus Torvalds 			continue;
4661da177e4SLinus Torvalds 		region.start = dev->resource[i].start;
4671da177e4SLinus Torvalds 		region.end = dev->resource[i].end;
4681da177e4SLinus Torvalds 		pcibios_bus_to_resource(dev, &dev->resource[i], &region);
46971c3511cSRajesh Shah 		if ((is_valid_resource(dev, i)))
4701da177e4SLinus Torvalds 			pci_claim_resource(dev, i);
4711da177e4SLinus Torvalds 	}
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds 
4748ea6091fSJohn Keller void __devinit pcibios_fixup_device_resources(struct pci_dev *dev)
4757b9c8ba2SKenji Kaneshige {
4767b9c8ba2SKenji Kaneshige 	pcibios_fixup_resources(dev, 0, PCI_BRIDGE_RESOURCES);
4777b9c8ba2SKenji Kaneshige }
4788ea6091fSJohn Keller EXPORT_SYMBOL_GPL(pcibios_fixup_device_resources);
4797b9c8ba2SKenji Kaneshige 
4807b9c8ba2SKenji Kaneshige static void __devinit pcibios_fixup_bridge_resources(struct pci_dev *dev)
4817b9c8ba2SKenji Kaneshige {
4827b9c8ba2SKenji Kaneshige 	pcibios_fixup_resources(dev, PCI_BRIDGE_RESOURCES, PCI_NUM_RESOURCES);
4837b9c8ba2SKenji Kaneshige }
4847b9c8ba2SKenji Kaneshige 
4851da177e4SLinus Torvalds /*
4861da177e4SLinus Torvalds  *  Called after each bus is probed, but before its children are examined.
4871da177e4SLinus Torvalds  */
4881da177e4SLinus Torvalds void __devinit
4891da177e4SLinus Torvalds pcibios_fixup_bus (struct pci_bus *b)
4901da177e4SLinus Torvalds {
4911da177e4SLinus Torvalds 	struct pci_dev *dev;
4921da177e4SLinus Torvalds 
493f7d473d9SRajesh Shah 	if (b->self) {
494f7d473d9SRajesh Shah 		pci_read_bridge_bases(b);
4957b9c8ba2SKenji Kaneshige 		pcibios_fixup_bridge_resources(b->self);
4961d89b30cSMatthew Wilcox 	} else {
4971d89b30cSMatthew Wilcox 		pcibios_setup_root_windows(b, b->sysdata);
498f7d473d9SRajesh Shah 	}
4991da177e4SLinus Torvalds 	list_for_each_entry(dev, &b->devices, bus_list)
5001da177e4SLinus Torvalds 		pcibios_fixup_device_resources(dev);
5018ea6091fSJohn Keller 	platform_pci_fixup_bus(b);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	return;
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds void __devinit
5071da177e4SLinus Torvalds pcibios_update_irq (struct pci_dev *dev, int irq)
5081da177e4SLinus Torvalds {
5091da177e4SLinus Torvalds 	pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	/* ??? FIXME -- record old value for shutdown.  */
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds int
5151da177e4SLinus Torvalds pcibios_enable_device (struct pci_dev *dev, int mask)
5161da177e4SLinus Torvalds {
5171da177e4SLinus Torvalds 	int ret;
5181da177e4SLinus Torvalds 
519d981f163SBjorn Helgaas 	ret = pci_enable_resources(dev, mask);
5201da177e4SLinus Torvalds 	if (ret < 0)
5211da177e4SLinus Torvalds 		return ret;
5221da177e4SLinus Torvalds 
523bba6f6fcSEric W. Biederman 	if (!dev->msi_enabled)
5241da177e4SLinus Torvalds 		return acpi_pci_irq_enable(dev);
525bba6f6fcSEric W. Biederman 	return 0;
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds void
5291da177e4SLinus Torvalds pcibios_disable_device (struct pci_dev *dev)
5301da177e4SLinus Torvalds {
531c7f570a5SPeter Chubb 	BUG_ON(atomic_read(&dev->enable_cnt));
532bba6f6fcSEric W. Biederman 	if (!dev->msi_enabled)
5331da177e4SLinus Torvalds 		acpi_pci_irq_disable(dev);
5341da177e4SLinus Torvalds }
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds void
5371da177e4SLinus Torvalds pcibios_align_resource (void *data, struct resource *res,
538e31dd6e4SGreg Kroah-Hartman 		        resource_size_t size, resource_size_t align)
5391da177e4SLinus Torvalds {
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds /*
5431da177e4SLinus Torvalds  * PCI BIOS setup, always defaults to SAL interface
5441da177e4SLinus Torvalds  */
545944c54e7SIngo Molnar char * __init
5461da177e4SLinus Torvalds pcibios_setup (char *str)
5471da177e4SLinus Torvalds {
548ac311ac2SMatthew Wilcox 	return str;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds int
5521da177e4SLinus Torvalds pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
5531da177e4SLinus Torvalds 		     enum pci_mmap_state mmap_state, int write_combine)
5541da177e4SLinus Torvalds {
555012b7105SAlex Chiang 	unsigned long size = vma->vm_end - vma->vm_start;
556012b7105SAlex Chiang 	pgprot_t prot;
557012b7105SAlex Chiang 
5581da177e4SLinus Torvalds 	/*
5591da177e4SLinus Torvalds 	 * I/O space cannot be accessed via normal processor loads and
5601da177e4SLinus Torvalds 	 * stores on this platform.
5611da177e4SLinus Torvalds 	 */
5621da177e4SLinus Torvalds 	if (mmap_state == pci_mmap_io)
5631da177e4SLinus Torvalds 		/*
5641da177e4SLinus Torvalds 		 * XXX we could relax this for I/O spaces for which ACPI
5651da177e4SLinus Torvalds 		 * indicates that the space is 1-to-1 mapped.  But at the
5661da177e4SLinus Torvalds 		 * moment, we don't support multiple PCI address spaces and
5671da177e4SLinus Torvalds 		 * the legacy I/O space is not 1-to-1 mapped, so this is moot.
5681da177e4SLinus Torvalds 		 */
5691da177e4SLinus Torvalds 		return -EINVAL;
5701da177e4SLinus Torvalds 
571012b7105SAlex Chiang 	if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
572012b7105SAlex Chiang 		return -EINVAL;
573012b7105SAlex Chiang 
574012b7105SAlex Chiang 	prot = phys_mem_access_prot(NULL, vma->vm_pgoff, size,
575012b7105SAlex Chiang 				    vma->vm_page_prot);
576012b7105SAlex Chiang 
5771da177e4SLinus Torvalds 	/*
578012b7105SAlex Chiang 	 * If the user requested WC, the kernel uses UC or WC for this region,
579012b7105SAlex Chiang 	 * and the chipset supports WC, we can use WC. Otherwise, we have to
580012b7105SAlex Chiang 	 * use the same attribute the kernel uses.
5811da177e4SLinus Torvalds 	 */
582012b7105SAlex Chiang 	if (write_combine &&
583012b7105SAlex Chiang 	    ((pgprot_val(prot) & _PAGE_MA_MASK) == _PAGE_MA_UC ||
584012b7105SAlex Chiang 	     (pgprot_val(prot) & _PAGE_MA_MASK) == _PAGE_MA_WC) &&
585012b7105SAlex Chiang 	    efi_range_is_wc(vma->vm_start, vma->vm_end - vma->vm_start))
5861da177e4SLinus Torvalds 		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
5871da177e4SLinus Torvalds 	else
588012b7105SAlex Chiang 		vma->vm_page_prot = prot;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
5911da177e4SLinus Torvalds 			     vma->vm_end - vma->vm_start, vma->vm_page_prot))
5921da177e4SLinus Torvalds 		return -EAGAIN;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	return 0;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds /**
5981da177e4SLinus Torvalds  * ia64_pci_get_legacy_mem - generic legacy mem routine
5991da177e4SLinus Torvalds  * @bus: bus to get legacy memory base address for
6001da177e4SLinus Torvalds  *
6011da177e4SLinus Torvalds  * Find the base of legacy memory for @bus.  This is typically the first
6021da177e4SLinus Torvalds  * megabyte of bus address space for @bus or is simply 0 on platforms whose
6031da177e4SLinus Torvalds  * chipsets support legacy I/O and memory routing.  Returns the base address
6041da177e4SLinus Torvalds  * or an error pointer if an error occurred.
6051da177e4SLinus Torvalds  *
6061da177e4SLinus Torvalds  * This is the ia64 generic version of this routine.  Other platforms
6071da177e4SLinus Torvalds  * are free to override it with a machine vector.
6081da177e4SLinus Torvalds  */
6091da177e4SLinus Torvalds char *ia64_pci_get_legacy_mem(struct pci_bus *bus)
6101da177e4SLinus Torvalds {
6111da177e4SLinus Torvalds 	return (char *)__IA64_UNCACHED_OFFSET;
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds /**
6151da177e4SLinus Torvalds  * pci_mmap_legacy_page_range - map legacy memory space to userland
6161da177e4SLinus Torvalds  * @bus: bus whose legacy space we're mapping
6171da177e4SLinus Torvalds  * @vma: vma passed in by mmap
6181da177e4SLinus Torvalds  *
6191da177e4SLinus Torvalds  * Map legacy memory space for this device back to userspace using a machine
6201da177e4SLinus Torvalds  * vector to get the base address.
6211da177e4SLinus Torvalds  */
6221da177e4SLinus Torvalds int
623f19aeb1fSBenjamin Herrenschmidt pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma,
624f19aeb1fSBenjamin Herrenschmidt 			   enum pci_mmap_state mmap_state)
6251da177e4SLinus Torvalds {
62632e62c63SBjorn Helgaas 	unsigned long size = vma->vm_end - vma->vm_start;
62732e62c63SBjorn Helgaas 	pgprot_t prot;
6281da177e4SLinus Torvalds 	char *addr;
6291da177e4SLinus Torvalds 
630f19aeb1fSBenjamin Herrenschmidt 	/* We only support mmap'ing of legacy memory space */
631f19aeb1fSBenjamin Herrenschmidt 	if (mmap_state != pci_mmap_mem)
632f19aeb1fSBenjamin Herrenschmidt 		return -ENOSYS;
633f19aeb1fSBenjamin Herrenschmidt 
63432e62c63SBjorn Helgaas 	/*
63532e62c63SBjorn Helgaas 	 * Avoid attribute aliasing.  See Documentation/ia64/aliasing.txt
63632e62c63SBjorn Helgaas 	 * for more details.
63732e62c63SBjorn Helgaas 	 */
63806c67befSLennert Buytenhek 	if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
63932e62c63SBjorn Helgaas 		return -EINVAL;
64032e62c63SBjorn Helgaas 	prot = phys_mem_access_prot(NULL, vma->vm_pgoff, size,
64132e62c63SBjorn Helgaas 				    vma->vm_page_prot);
64232e62c63SBjorn Helgaas 
6431da177e4SLinus Torvalds 	addr = pci_get_legacy_mem(bus);
6441da177e4SLinus Torvalds 	if (IS_ERR(addr))
6451da177e4SLinus Torvalds 		return PTR_ERR(addr);
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	vma->vm_pgoff += (unsigned long)addr >> PAGE_SHIFT;
64832e62c63SBjorn Helgaas 	vma->vm_page_prot = prot;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
65132e62c63SBjorn Helgaas 			    size, vma->vm_page_prot))
6521da177e4SLinus Torvalds 		return -EAGAIN;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	return 0;
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds /**
6581da177e4SLinus Torvalds  * ia64_pci_legacy_read - read from legacy I/O space
6591da177e4SLinus Torvalds  * @bus: bus to read
6601da177e4SLinus Torvalds  * @port: legacy port value
6611da177e4SLinus Torvalds  * @val: caller allocated storage for returned value
6621da177e4SLinus Torvalds  * @size: number of bytes to read
6631da177e4SLinus Torvalds  *
6641da177e4SLinus Torvalds  * Simply reads @size bytes from @port and puts the result in @val.
6651da177e4SLinus Torvalds  *
6661da177e4SLinus Torvalds  * Again, this (and the write routine) are generic versions that can be
6671da177e4SLinus Torvalds  * overridden by the platform.  This is necessary on platforms that don't
6681da177e4SLinus Torvalds  * support legacy I/O routing or that hard fail on legacy I/O timeouts.
6691da177e4SLinus Torvalds  */
6701da177e4SLinus Torvalds int ia64_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
6711da177e4SLinus Torvalds {
6721da177e4SLinus Torvalds 	int ret = size;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	switch (size) {
6751da177e4SLinus Torvalds 	case 1:
6761da177e4SLinus Torvalds 		*val = inb(port);
6771da177e4SLinus Torvalds 		break;
6781da177e4SLinus Torvalds 	case 2:
6791da177e4SLinus Torvalds 		*val = inw(port);
6801da177e4SLinus Torvalds 		break;
6811da177e4SLinus Torvalds 	case 4:
6821da177e4SLinus Torvalds 		*val = inl(port);
6831da177e4SLinus Torvalds 		break;
6841da177e4SLinus Torvalds 	default:
6851da177e4SLinus Torvalds 		ret = -EINVAL;
6861da177e4SLinus Torvalds 		break;
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	return ret;
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds /**
6931da177e4SLinus Torvalds  * ia64_pci_legacy_write - perform a legacy I/O write
6941da177e4SLinus Torvalds  * @bus: bus pointer
6951da177e4SLinus Torvalds  * @port: port to write
6961da177e4SLinus Torvalds  * @val: value to write
6971da177e4SLinus Torvalds  * @size: number of bytes to write from @val
6981da177e4SLinus Torvalds  *
6991da177e4SLinus Torvalds  * Simply writes @size bytes of @val to @port.
7001da177e4SLinus Torvalds  */
701a72391e4SSatoru Takeuchi int ia64_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
7021da177e4SLinus Torvalds {
703408045afSAlex Williamson 	int ret = size;
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	switch (size) {
7061da177e4SLinus Torvalds 	case 1:
7071da177e4SLinus Torvalds 		outb(val, port);
7081da177e4SLinus Torvalds 		break;
7091da177e4SLinus Torvalds 	case 2:
7101da177e4SLinus Torvalds 		outw(val, port);
7111da177e4SLinus Torvalds 		break;
7121da177e4SLinus Torvalds 	case 4:
7131da177e4SLinus Torvalds 		outl(val, port);
7141da177e4SLinus Torvalds 		break;
7151da177e4SLinus Torvalds 	default:
7161da177e4SLinus Torvalds 		ret = -EINVAL;
7171da177e4SLinus Torvalds 		break;
7181da177e4SLinus Torvalds 	}
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	return ret;
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
7233efe2d84SMatthew Wilcox /* It's defined in drivers/pci/pci.c */
7243efe2d84SMatthew Wilcox extern u8 pci_cache_line_size;
7253efe2d84SMatthew Wilcox 
7261da177e4SLinus Torvalds /**
7273efe2d84SMatthew Wilcox  * set_pci_cacheline_size - determine cacheline size for PCI devices
7281da177e4SLinus Torvalds  *
7291da177e4SLinus Torvalds  * We want to use the line-size of the outer-most cache.  We assume
7301da177e4SLinus Torvalds  * that this line-size is the same for all CPUs.
7311da177e4SLinus Torvalds  *
7321da177e4SLinus Torvalds  * Code mostly taken from arch/ia64/kernel/palinfo.c:cache_info().
7331da177e4SLinus Torvalds  */
7343efe2d84SMatthew Wilcox static void __init set_pci_cacheline_size(void)
7351da177e4SLinus Torvalds {
736e088a4adSMatthew Wilcox 	unsigned long levels, unique_caches;
737e088a4adSMatthew Wilcox 	long status;
7381da177e4SLinus Torvalds 	pal_cache_config_info_t cci;
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	status = ia64_pal_cache_summary(&levels, &unique_caches);
7411da177e4SLinus Torvalds 	if (status != 0) {
7423efe2d84SMatthew Wilcox 		printk(KERN_ERR "%s: ia64_pal_cache_summary() failed "
743d4ed8084SHarvey Harrison 			"(status=%ld)\n", __func__, status);
7443efe2d84SMatthew Wilcox 		return;
7451da177e4SLinus Torvalds 	}
7461da177e4SLinus Torvalds 
7473efe2d84SMatthew Wilcox 	status = ia64_pal_cache_config_info(levels - 1,
7483efe2d84SMatthew Wilcox 				/* cache_type (data_or_unified)= */ 2, &cci);
7491da177e4SLinus Torvalds 	if (status != 0) {
7503efe2d84SMatthew Wilcox 		printk(KERN_ERR "%s: ia64_pal_cache_config_info() failed "
751d4ed8084SHarvey Harrison 			"(status=%ld)\n", __func__, status);
7523efe2d84SMatthew Wilcox 		return;
7531da177e4SLinus Torvalds 	}
7543efe2d84SMatthew Wilcox 	pci_cache_line_size = (1 << cci.pcci_line_size) / 4;
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
757175add19SJohn Keller u64 ia64_dma_get_required_mask(struct device *dev)
758175add19SJohn Keller {
759175add19SJohn Keller 	u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
760175add19SJohn Keller 	u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
761175add19SJohn Keller 	u64 mask;
762175add19SJohn Keller 
763175add19SJohn Keller 	if (!high_totalram) {
764175add19SJohn Keller 		/* convert to mask just covering totalram */
765175add19SJohn Keller 		low_totalram = (1 << (fls(low_totalram) - 1));
766175add19SJohn Keller 		low_totalram += low_totalram - 1;
767175add19SJohn Keller 		mask = low_totalram;
768175add19SJohn Keller 	} else {
769175add19SJohn Keller 		high_totalram = (1 << (fls(high_totalram) - 1));
770175add19SJohn Keller 		high_totalram += high_totalram - 1;
771175add19SJohn Keller 		mask = (((u64)high_totalram) << 32) + 0xffffffff;
772175add19SJohn Keller 	}
773175add19SJohn Keller 	return mask;
774175add19SJohn Keller }
775175add19SJohn Keller EXPORT_SYMBOL_GPL(ia64_dma_get_required_mask);
776175add19SJohn Keller 
777175add19SJohn Keller u64 dma_get_required_mask(struct device *dev)
778175add19SJohn Keller {
779175add19SJohn Keller 	return platform_dma_get_required_mask(dev);
780175add19SJohn Keller }
781175add19SJohn Keller EXPORT_SYMBOL_GPL(dma_get_required_mask);
782175add19SJohn Keller 
7833efe2d84SMatthew Wilcox static int __init pcibios_init(void)
7841da177e4SLinus Torvalds {
7853efe2d84SMatthew Wilcox 	set_pci_cacheline_size();
7863efe2d84SMatthew Wilcox 	return 0;
7873efe2d84SMatthew Wilcox }
7881da177e4SLinus Torvalds 
7893efe2d84SMatthew Wilcox subsys_initcall(pcibios_init);
790