xref: /openbmc/linux/arch/ia64/pci/pci.c (revision fab3fb0a)
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 #include <linux/config.h>
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include <linux/acpi.h>
161da177e4SLinus Torvalds #include <linux/types.h>
171da177e4SLinus Torvalds #include <linux/kernel.h>
181da177e4SLinus Torvalds #include <linux/pci.h>
191da177e4SLinus Torvalds #include <linux/init.h>
201da177e4SLinus Torvalds #include <linux/ioport.h>
211da177e4SLinus Torvalds #include <linux/slab.h>
221da177e4SLinus Torvalds #include <linux/smp_lock.h>
231da177e4SLinus Torvalds #include <linux/spinlock.h>
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #include <asm/machvec.h>
261da177e4SLinus Torvalds #include <asm/page.h>
271da177e4SLinus Torvalds #include <asm/segment.h>
281da177e4SLinus Torvalds #include <asm/system.h>
291da177e4SLinus Torvalds #include <asm/io.h>
301da177e4SLinus Torvalds #include <asm/sal.h>
311da177e4SLinus Torvalds #include <asm/smp.h>
321da177e4SLinus Torvalds #include <asm/irq.h>
331da177e4SLinus Torvalds #include <asm/hw_irq.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /*
371da177e4SLinus Torvalds  * Low-level SAL-based PCI configuration access functions. Note that SAL
381da177e4SLinus Torvalds  * calls are already serialized (via sal_lock), so we don't need another
391da177e4SLinus Torvalds  * synchronization mechanism here.
401da177e4SLinus Torvalds  */
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #define PCI_SAL_ADDRESS(seg, bus, devfn, reg)		\
431da177e4SLinus Torvalds 	(((u64) seg << 24) | (bus << 16) | (devfn << 8) | (reg))
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /* SAL 3.2 adds support for extended config space. */
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #define PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg)	\
481da177e4SLinus Torvalds 	(((u64) seg << 28) | (bus << 20) | (devfn << 12) | (reg))
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds static int
511da177e4SLinus Torvalds pci_sal_read (unsigned int seg, unsigned int bus, unsigned int devfn,
521da177e4SLinus Torvalds 	      int reg, int len, u32 *value)
531da177e4SLinus Torvalds {
541da177e4SLinus Torvalds 	u64 addr, data = 0;
551da177e4SLinus Torvalds 	int mode, result;
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	if (!value || (seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
581da177e4SLinus Torvalds 		return -EINVAL;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	if ((seg | reg) <= 255) {
611da177e4SLinus Torvalds 		addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
621da177e4SLinus Torvalds 		mode = 0;
631da177e4SLinus Torvalds 	} else {
641da177e4SLinus Torvalds 		addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
651da177e4SLinus Torvalds 		mode = 1;
661da177e4SLinus Torvalds 	}
671da177e4SLinus Torvalds 	result = ia64_sal_pci_config_read(addr, mode, len, &data);
681da177e4SLinus Torvalds 	if (result != 0)
691da177e4SLinus Torvalds 		return -EINVAL;
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	*value = (u32) data;
721da177e4SLinus Torvalds 	return 0;
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static int
761da177e4SLinus Torvalds pci_sal_write (unsigned int seg, unsigned int bus, unsigned int devfn,
771da177e4SLinus Torvalds 	       int reg, int len, u32 value)
781da177e4SLinus Torvalds {
791da177e4SLinus Torvalds 	u64 addr;
801da177e4SLinus Torvalds 	int mode, result;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	if ((seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
831da177e4SLinus Torvalds 		return -EINVAL;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	if ((seg | reg) <= 255) {
861da177e4SLinus Torvalds 		addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
871da177e4SLinus Torvalds 		mode = 0;
881da177e4SLinus Torvalds 	} else {
891da177e4SLinus Torvalds 		addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
901da177e4SLinus Torvalds 		mode = 1;
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 
981da177e4SLinus Torvalds static struct pci_raw_ops pci_sal_ops = {
991da177e4SLinus Torvalds 	.read = 	pci_sal_read,
1001da177e4SLinus Torvalds 	.write =	pci_sal_write
1011da177e4SLinus Torvalds };
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds struct pci_raw_ops *raw_pci_ops = &pci_sal_ops;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds static int
1061da177e4SLinus Torvalds pci_read (struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	return raw_pci_ops->read(pci_domain_nr(bus), bus->number,
1091da177e4SLinus Torvalds 				 devfn, where, size, value);
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds static int
1131da177e4SLinus Torvalds pci_write (struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds 	return raw_pci_ops->write(pci_domain_nr(bus), bus->number,
1161da177e4SLinus Torvalds 				  devfn, where, size, value);
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds struct pci_ops pci_root_ops = {
1201da177e4SLinus Torvalds 	.read = pci_read,
1211da177e4SLinus Torvalds 	.write = pci_write,
1221da177e4SLinus Torvalds };
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1251da177e4SLinus Torvalds extern acpi_status acpi_map_iosapic(acpi_handle, u32, void *, void **);
1261da177e4SLinus Torvalds static void acpi_map_iosapics(void)
1271da177e4SLinus Torvalds {
1281da177e4SLinus Torvalds 	acpi_get_devices(NULL, acpi_map_iosapic, NULL, NULL);
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds #else
1311da177e4SLinus Torvalds static void acpi_map_iosapics(void)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	return;
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds #endif /* CONFIG_NUMA */
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds static int __init
1381da177e4SLinus Torvalds pci_acpi_init (void)
1391da177e4SLinus Torvalds {
1401da177e4SLinus Torvalds 	acpi_map_iosapics();
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	return 0;
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds subsys_initcall(pci_acpi_init);
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds /* Called by ACPI when it finds a new root bus.  */
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static struct pci_controller * __devinit
1501da177e4SLinus Torvalds alloc_pci_controller (int seg)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	struct pci_controller *controller;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	controller = kmalloc(sizeof(*controller), GFP_KERNEL);
1551da177e4SLinus Torvalds 	if (!controller)
1561da177e4SLinus Torvalds 		return NULL;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	memset(controller, 0, sizeof(*controller));
1591da177e4SLinus Torvalds 	controller->segment = seg;
1601da177e4SLinus Torvalds 	return controller;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds static u64 __devinit
1641da177e4SLinus Torvalds add_io_space (struct acpi_resource_address64 *addr)
1651da177e4SLinus Torvalds {
1661da177e4SLinus Torvalds 	u64 offset;
1671da177e4SLinus Torvalds 	int sparse = 0;
1681da177e4SLinus Torvalds 	int i;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	if (addr->address_translation_offset == 0)
1711da177e4SLinus Torvalds 		return IO_SPACE_BASE(0);	/* part of legacy IO space */
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	if (addr->attribute.io.translation_attribute == ACPI_SPARSE_TRANSLATION)
1741da177e4SLinus Torvalds 		sparse = 1;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	offset = (u64) ioremap(addr->address_translation_offset, 0);
1771da177e4SLinus Torvalds 	for (i = 0; i < num_io_spaces; i++)
1781da177e4SLinus Torvalds 		if (io_space[i].mmio_base == offset &&
1791da177e4SLinus Torvalds 		    io_space[i].sparse == sparse)
1801da177e4SLinus Torvalds 			return IO_SPACE_BASE(i);
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	if (num_io_spaces == MAX_IO_SPACES) {
1831da177e4SLinus Torvalds 		printk("Too many IO port spaces\n");
1841da177e4SLinus Torvalds 		return ~0;
1851da177e4SLinus Torvalds 	}
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	i = num_io_spaces++;
1881da177e4SLinus Torvalds 	io_space[i].mmio_base = offset;
1891da177e4SLinus Torvalds 	io_space[i].sparse = sparse;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	return IO_SPACE_BASE(i);
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds static acpi_status __devinit
1951da177e4SLinus Torvalds count_window (struct acpi_resource *resource, void *data)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	unsigned int *windows = (unsigned int *) data;
1981da177e4SLinus Torvalds 	struct acpi_resource_address64 addr;
1991da177e4SLinus Torvalds 	acpi_status status;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	status = acpi_resource_to_address64(resource, &addr);
2021da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
2031da177e4SLinus Torvalds 		if (addr.resource_type == ACPI_MEMORY_RANGE ||
2041da177e4SLinus Torvalds 		    addr.resource_type == ACPI_IO_RANGE)
2051da177e4SLinus Torvalds 			(*windows)++;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	return AE_OK;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds struct pci_root_info {
2111da177e4SLinus Torvalds 	struct pci_controller *controller;
2121da177e4SLinus Torvalds 	char *name;
2131da177e4SLinus Torvalds };
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds static __devinit acpi_status add_window(struct acpi_resource *res, void *data)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	struct pci_root_info *info = data;
2181da177e4SLinus Torvalds 	struct pci_window *window;
2191da177e4SLinus Torvalds 	struct acpi_resource_address64 addr;
2201da177e4SLinus Torvalds 	acpi_status status;
2211da177e4SLinus Torvalds 	unsigned long flags, offset = 0;
2221da177e4SLinus Torvalds 	struct resource *root;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	status = acpi_resource_to_address64(res, &addr);
2251da177e4SLinus Torvalds 	if (!ACPI_SUCCESS(status))
2261da177e4SLinus Torvalds 		return AE_OK;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	if (!addr.address_length)
2291da177e4SLinus Torvalds 		return AE_OK;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	if (addr.resource_type == ACPI_MEMORY_RANGE) {
2321da177e4SLinus Torvalds 		flags = IORESOURCE_MEM;
2331da177e4SLinus Torvalds 		root = &iomem_resource;
2341da177e4SLinus Torvalds 		offset = addr.address_translation_offset;
2351da177e4SLinus Torvalds 	} else if (addr.resource_type == ACPI_IO_RANGE) {
2361da177e4SLinus Torvalds 		flags = IORESOURCE_IO;
2371da177e4SLinus Torvalds 		root = &ioport_resource;
2381da177e4SLinus Torvalds 		offset = add_io_space(&addr);
2391da177e4SLinus Torvalds 		if (offset == ~0)
2401da177e4SLinus Torvalds 			return AE_OK;
2411da177e4SLinus Torvalds 	} else
2421da177e4SLinus Torvalds 		return AE_OK;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	window = &info->controller->window[info->controller->windows++];
2451da177e4SLinus Torvalds 	window->resource.name = info->name;
2461da177e4SLinus Torvalds 	window->resource.flags = flags;
2471da177e4SLinus Torvalds 	window->resource.start = addr.min_address_range + offset;
2481da177e4SLinus Torvalds 	window->resource.end = addr.max_address_range + offset;
2491da177e4SLinus Torvalds 	window->resource.child = NULL;
2501da177e4SLinus Torvalds 	window->offset = offset;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	if (insert_resource(root, &window->resource)) {
2531da177e4SLinus Torvalds 		printk(KERN_ERR "alloc 0x%lx-0x%lx from %s for %s failed\n",
2541da177e4SLinus Torvalds 			window->resource.start, window->resource.end,
2551da177e4SLinus Torvalds 			root->name, info->name);
2561da177e4SLinus Torvalds 	}
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	return AE_OK;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds static void __devinit
2621da177e4SLinus Torvalds pcibios_setup_root_windows(struct pci_bus *bus, struct pci_controller *ctrl)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	int i, j;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	j = 0;
2671da177e4SLinus Torvalds 	for (i = 0; i < ctrl->windows; i++) {
2681da177e4SLinus Torvalds 		struct resource *res = &ctrl->window[i].resource;
2691da177e4SLinus Torvalds 		/* HP's firmware has a hack to work around a Windows bug.
2701da177e4SLinus Torvalds 		 * Ignore these tiny memory ranges */
2711da177e4SLinus Torvalds 		if ((res->flags & IORESOURCE_MEM) &&
2721da177e4SLinus Torvalds 		    (res->end - res->start < 16))
2731da177e4SLinus Torvalds 			continue;
2741da177e4SLinus Torvalds 		if (j >= PCI_BUS_NUM_RESOURCES) {
2751da177e4SLinus Torvalds 			printk("Ignoring range [%lx-%lx] (%lx)\n", res->start,
2761da177e4SLinus Torvalds 					res->end, res->flags);
2771da177e4SLinus Torvalds 			continue;
2781da177e4SLinus Torvalds 		}
2791da177e4SLinus Torvalds 		bus->resource[j++] = res;
2801da177e4SLinus Torvalds 	}
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds struct pci_bus * __devinit
2841da177e4SLinus Torvalds pci_acpi_scan_root(struct acpi_device *device, int domain, int bus)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	struct pci_root_info info;
2871da177e4SLinus Torvalds 	struct pci_controller *controller;
2881da177e4SLinus Torvalds 	unsigned int windows = 0;
2891da177e4SLinus Torvalds 	struct pci_bus *pbus;
2901da177e4SLinus Torvalds 	char *name;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	controller = alloc_pci_controller(domain);
2931da177e4SLinus Torvalds 	if (!controller)
2941da177e4SLinus Torvalds 		goto out1;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	controller->acpi_handle = device->handle;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_window,
2991da177e4SLinus Torvalds 			&windows);
3001da177e4SLinus Torvalds 	controller->window = kmalloc(sizeof(*controller->window) * windows,
3011da177e4SLinus Torvalds 			GFP_KERNEL);
3021da177e4SLinus Torvalds 	if (!controller->window)
3031da177e4SLinus Torvalds 		goto out2;
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	name = kmalloc(16, GFP_KERNEL);
3061da177e4SLinus Torvalds 	if (!name)
3071da177e4SLinus Torvalds 		goto out3;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	sprintf(name, "PCI Bus %04x:%02x", domain, bus);
3101da177e4SLinus Torvalds 	info.controller = controller;
3111da177e4SLinus Torvalds 	info.name = name;
3121da177e4SLinus Torvalds 	acpi_walk_resources(device->handle, METHOD_NAME__CRS, add_window,
3131da177e4SLinus Torvalds 			&info);
3141da177e4SLinus Torvalds 
315c431ada4SRajesh Shah 	pbus = pci_scan_bus_parented(NULL, bus, &pci_root_ops, controller);
3161da177e4SLinus Torvalds 	if (pbus)
3171da177e4SLinus Torvalds 		pcibios_setup_root_windows(pbus, controller);
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	return pbus;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds out3:
3221da177e4SLinus Torvalds 	kfree(controller->window);
3231da177e4SLinus Torvalds out2:
3241da177e4SLinus Torvalds 	kfree(controller);
3251da177e4SLinus Torvalds out1:
3261da177e4SLinus Torvalds 	return NULL;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds void pcibios_resource_to_bus(struct pci_dev *dev,
3301da177e4SLinus Torvalds 		struct pci_bus_region *region, struct resource *res)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	struct pci_controller *controller = PCI_CONTROLLER(dev);
3331da177e4SLinus Torvalds 	unsigned long offset = 0;
3341da177e4SLinus Torvalds 	int i;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	for (i = 0; i < controller->windows; i++) {
3371da177e4SLinus Torvalds 		struct pci_window *window = &controller->window[i];
3381da177e4SLinus Torvalds 		if (!(window->resource.flags & res->flags))
3391da177e4SLinus Torvalds 			continue;
3401da177e4SLinus Torvalds 		if (window->resource.start > res->start)
3411da177e4SLinus Torvalds 			continue;
3421da177e4SLinus Torvalds 		if (window->resource.end < res->end)
3431da177e4SLinus Torvalds 			continue;
3441da177e4SLinus Torvalds 		offset = window->offset;
3451da177e4SLinus Torvalds 		break;
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	region->start = res->start - offset;
3491da177e4SLinus Torvalds 	region->end = res->end - offset;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds EXPORT_SYMBOL(pcibios_resource_to_bus);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds void pcibios_bus_to_resource(struct pci_dev *dev,
3541da177e4SLinus Torvalds 		struct resource *res, struct pci_bus_region *region)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	struct pci_controller *controller = PCI_CONTROLLER(dev);
3571da177e4SLinus Torvalds 	unsigned long offset = 0;
3581da177e4SLinus Torvalds 	int i;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	for (i = 0; i < controller->windows; i++) {
3611da177e4SLinus Torvalds 		struct pci_window *window = &controller->window[i];
3621da177e4SLinus Torvalds 		if (!(window->resource.flags & res->flags))
3631da177e4SLinus Torvalds 			continue;
3641da177e4SLinus Torvalds 		if (window->resource.start - window->offset > region->start)
3651da177e4SLinus Torvalds 			continue;
3661da177e4SLinus Torvalds 		if (window->resource.end - window->offset < region->end)
3671da177e4SLinus Torvalds 			continue;
3681da177e4SLinus Torvalds 		offset = window->offset;
3691da177e4SLinus Torvalds 		break;
3701da177e4SLinus Torvalds 	}
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	res->start = region->start + offset;
3731da177e4SLinus Torvalds 	res->end = region->end + offset;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds static void __devinit pcibios_fixup_device_resources(struct pci_dev *dev)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	struct pci_bus_region region;
3791da177e4SLinus Torvalds 	int i;
3801da177e4SLinus Torvalds 	int limit = (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) ? \
3811da177e4SLinus Torvalds 		PCI_BRIDGE_RESOURCES : PCI_NUM_RESOURCES;
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	for (i = 0; i < limit; i++) {
3841da177e4SLinus Torvalds 		if (!dev->resource[i].flags)
3851da177e4SLinus Torvalds 			continue;
3861da177e4SLinus Torvalds 		region.start = dev->resource[i].start;
3871da177e4SLinus Torvalds 		region.end = dev->resource[i].end;
3881da177e4SLinus Torvalds 		pcibios_bus_to_resource(dev, &dev->resource[i], &region);
3891da177e4SLinus Torvalds 		pci_claim_resource(dev, i);
3901da177e4SLinus Torvalds 	}
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds /*
3941da177e4SLinus Torvalds  *  Called after each bus is probed, but before its children are examined.
3951da177e4SLinus Torvalds  */
3961da177e4SLinus Torvalds void __devinit
3971da177e4SLinus Torvalds pcibios_fixup_bus (struct pci_bus *b)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	struct pci_dev *dev;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	list_for_each_entry(dev, &b->devices, bus_list)
4021da177e4SLinus Torvalds 		pcibios_fixup_device_resources(dev);
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	return;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds void __devinit
4081da177e4SLinus Torvalds pcibios_update_irq (struct pci_dev *dev, int irq)
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	/* ??? FIXME -- record old value for shutdown.  */
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds static inline int
4161da177e4SLinus Torvalds pcibios_enable_resources (struct pci_dev *dev, int mask)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds 	u16 cmd, old_cmd;
4191da177e4SLinus Torvalds 	int idx;
4201da177e4SLinus Torvalds 	struct resource *r;
421fab3fb0aSRajesh Shah 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	if (!dev)
4241da177e4SLinus Torvalds 		return -EINVAL;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	pci_read_config_word(dev, PCI_COMMAND, &cmd);
4271da177e4SLinus Torvalds 	old_cmd = cmd;
428fab3fb0aSRajesh Shah 	for (idx=0; idx<PCI_NUM_RESOURCES; idx++) {
4291da177e4SLinus Torvalds 		/* Only set up the desired resources.  */
4301da177e4SLinus Torvalds 		if (!(mask & (1 << idx)))
4311da177e4SLinus Torvalds 			continue;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 		r = &dev->resource[idx];
434fab3fb0aSRajesh Shah 		if (!(r->flags & type_mask))
435fab3fb0aSRajesh Shah 			continue;
436fab3fb0aSRajesh Shah 		if ((idx == PCI_ROM_RESOURCE) &&
437fab3fb0aSRajesh Shah 				(!(r->flags & IORESOURCE_ROM_ENABLE)))
438fab3fb0aSRajesh Shah 			continue;
4391da177e4SLinus Torvalds 		if (!r->start && r->end) {
4401da177e4SLinus Torvalds 			printk(KERN_ERR
4411da177e4SLinus Torvalds 			       "PCI: Device %s not available because of resource collisions\n",
4421da177e4SLinus Torvalds 			       pci_name(dev));
4431da177e4SLinus Torvalds 			return -EINVAL;
4441da177e4SLinus Torvalds 		}
4451da177e4SLinus Torvalds 		if (r->flags & IORESOURCE_IO)
4461da177e4SLinus Torvalds 			cmd |= PCI_COMMAND_IO;
4471da177e4SLinus Torvalds 		if (r->flags & IORESOURCE_MEM)
4481da177e4SLinus Torvalds 			cmd |= PCI_COMMAND_MEMORY;
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds 	if (cmd != old_cmd) {
4511da177e4SLinus Torvalds 		printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
4521da177e4SLinus Torvalds 		pci_write_config_word(dev, PCI_COMMAND, cmd);
4531da177e4SLinus Torvalds 	}
4541da177e4SLinus Torvalds 	return 0;
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds int
4581da177e4SLinus Torvalds pcibios_enable_device (struct pci_dev *dev, int mask)
4591da177e4SLinus Torvalds {
4601da177e4SLinus Torvalds 	int ret;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	ret = pcibios_enable_resources(dev, mask);
4631da177e4SLinus Torvalds 	if (ret < 0)
4641da177e4SLinus Torvalds 		return ret;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	return acpi_pci_irq_enable(dev);
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds #ifdef CONFIG_ACPI_DEALLOCATE_IRQ
4701da177e4SLinus Torvalds void
4711da177e4SLinus Torvalds pcibios_disable_device (struct pci_dev *dev)
4721da177e4SLinus Torvalds {
4731da177e4SLinus Torvalds 	acpi_pci_irq_disable(dev);
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds #endif /* CONFIG_ACPI_DEALLOCATE_IRQ */
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds void
4781da177e4SLinus Torvalds pcibios_align_resource (void *data, struct resource *res,
4791da177e4SLinus Torvalds 		        unsigned long size, unsigned long align)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds /*
4841da177e4SLinus Torvalds  * PCI BIOS setup, always defaults to SAL interface
4851da177e4SLinus Torvalds  */
4861da177e4SLinus Torvalds char * __init
4871da177e4SLinus Torvalds pcibios_setup (char *str)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	return NULL;
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds int
4931da177e4SLinus Torvalds pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
4941da177e4SLinus Torvalds 		     enum pci_mmap_state mmap_state, int write_combine)
4951da177e4SLinus Torvalds {
4961da177e4SLinus Torvalds 	/*
4971da177e4SLinus Torvalds 	 * I/O space cannot be accessed via normal processor loads and
4981da177e4SLinus Torvalds 	 * stores on this platform.
4991da177e4SLinus Torvalds 	 */
5001da177e4SLinus Torvalds 	if (mmap_state == pci_mmap_io)
5011da177e4SLinus Torvalds 		/*
5021da177e4SLinus Torvalds 		 * XXX we could relax this for I/O spaces for which ACPI
5031da177e4SLinus Torvalds 		 * indicates that the space is 1-to-1 mapped.  But at the
5041da177e4SLinus Torvalds 		 * moment, we don't support multiple PCI address spaces and
5051da177e4SLinus Torvalds 		 * the legacy I/O space is not 1-to-1 mapped, so this is moot.
5061da177e4SLinus Torvalds 		 */
5071da177e4SLinus Torvalds 		return -EINVAL;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	/*
5101da177e4SLinus Torvalds 	 * Leave vm_pgoff as-is, the PCI space address is the physical
5111da177e4SLinus Torvalds 	 * address on this platform.
5121da177e4SLinus Torvalds 	 */
5131da177e4SLinus Torvalds 	vma->vm_flags |= (VM_SHM | VM_RESERVED | VM_IO);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	if (write_combine && efi_range_is_wc(vma->vm_start,
5161da177e4SLinus Torvalds 					     vma->vm_end - vma->vm_start))
5171da177e4SLinus Torvalds 		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
5181da177e4SLinus Torvalds 	else
5191da177e4SLinus Torvalds 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
5221da177e4SLinus Torvalds 			     vma->vm_end - vma->vm_start, vma->vm_page_prot))
5231da177e4SLinus Torvalds 		return -EAGAIN;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	return 0;
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds /**
5291da177e4SLinus Torvalds  * ia64_pci_get_legacy_mem - generic legacy mem routine
5301da177e4SLinus Torvalds  * @bus: bus to get legacy memory base address for
5311da177e4SLinus Torvalds  *
5321da177e4SLinus Torvalds  * Find the base of legacy memory for @bus.  This is typically the first
5331da177e4SLinus Torvalds  * megabyte of bus address space for @bus or is simply 0 on platforms whose
5341da177e4SLinus Torvalds  * chipsets support legacy I/O and memory routing.  Returns the base address
5351da177e4SLinus Torvalds  * or an error pointer if an error occurred.
5361da177e4SLinus Torvalds  *
5371da177e4SLinus Torvalds  * This is the ia64 generic version of this routine.  Other platforms
5381da177e4SLinus Torvalds  * are free to override it with a machine vector.
5391da177e4SLinus Torvalds  */
5401da177e4SLinus Torvalds char *ia64_pci_get_legacy_mem(struct pci_bus *bus)
5411da177e4SLinus Torvalds {
5421da177e4SLinus Torvalds 	return (char *)__IA64_UNCACHED_OFFSET;
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds /**
5461da177e4SLinus Torvalds  * pci_mmap_legacy_page_range - map legacy memory space to userland
5471da177e4SLinus Torvalds  * @bus: bus whose legacy space we're mapping
5481da177e4SLinus Torvalds  * @vma: vma passed in by mmap
5491da177e4SLinus Torvalds  *
5501da177e4SLinus Torvalds  * Map legacy memory space for this device back to userspace using a machine
5511da177e4SLinus Torvalds  * vector to get the base address.
5521da177e4SLinus Torvalds  */
5531da177e4SLinus Torvalds int
5541da177e4SLinus Torvalds pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds 	char *addr;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	addr = pci_get_legacy_mem(bus);
5591da177e4SLinus Torvalds 	if (IS_ERR(addr))
5601da177e4SLinus Torvalds 		return PTR_ERR(addr);
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	vma->vm_pgoff += (unsigned long)addr >> PAGE_SHIFT;
5631da177e4SLinus Torvalds 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
5641da177e4SLinus Torvalds 	vma->vm_flags |= (VM_SHM | VM_RESERVED | VM_IO);
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
5671da177e4SLinus Torvalds 			    vma->vm_end - vma->vm_start, vma->vm_page_prot))
5681da177e4SLinus Torvalds 		return -EAGAIN;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	return 0;
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds /**
5741da177e4SLinus Torvalds  * ia64_pci_legacy_read - read from legacy I/O space
5751da177e4SLinus Torvalds  * @bus: bus to read
5761da177e4SLinus Torvalds  * @port: legacy port value
5771da177e4SLinus Torvalds  * @val: caller allocated storage for returned value
5781da177e4SLinus Torvalds  * @size: number of bytes to read
5791da177e4SLinus Torvalds  *
5801da177e4SLinus Torvalds  * Simply reads @size bytes from @port and puts the result in @val.
5811da177e4SLinus Torvalds  *
5821da177e4SLinus Torvalds  * Again, this (and the write routine) are generic versions that can be
5831da177e4SLinus Torvalds  * overridden by the platform.  This is necessary on platforms that don't
5841da177e4SLinus Torvalds  * support legacy I/O routing or that hard fail on legacy I/O timeouts.
5851da177e4SLinus Torvalds  */
5861da177e4SLinus Torvalds int ia64_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
5871da177e4SLinus Torvalds {
5881da177e4SLinus Torvalds 	int ret = size;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	switch (size) {
5911da177e4SLinus Torvalds 	case 1:
5921da177e4SLinus Torvalds 		*val = inb(port);
5931da177e4SLinus Torvalds 		break;
5941da177e4SLinus Torvalds 	case 2:
5951da177e4SLinus Torvalds 		*val = inw(port);
5961da177e4SLinus Torvalds 		break;
5971da177e4SLinus Torvalds 	case 4:
5981da177e4SLinus Torvalds 		*val = inl(port);
5991da177e4SLinus Torvalds 		break;
6001da177e4SLinus Torvalds 	default:
6011da177e4SLinus Torvalds 		ret = -EINVAL;
6021da177e4SLinus Torvalds 		break;
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	return ret;
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds /**
6091da177e4SLinus Torvalds  * ia64_pci_legacy_write - perform a legacy I/O write
6101da177e4SLinus Torvalds  * @bus: bus pointer
6111da177e4SLinus Torvalds  * @port: port to write
6121da177e4SLinus Torvalds  * @val: value to write
6131da177e4SLinus Torvalds  * @size: number of bytes to write from @val
6141da177e4SLinus Torvalds  *
6151da177e4SLinus Torvalds  * Simply writes @size bytes of @val to @port.
6161da177e4SLinus Torvalds  */
6171da177e4SLinus Torvalds int ia64_pci_legacy_write(struct pci_dev *bus, u16 port, u32 val, u8 size)
6181da177e4SLinus Torvalds {
6191da177e4SLinus Torvalds 	int ret = 0;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	switch (size) {
6221da177e4SLinus Torvalds 	case 1:
6231da177e4SLinus Torvalds 		outb(val, port);
6241da177e4SLinus Torvalds 		break;
6251da177e4SLinus Torvalds 	case 2:
6261da177e4SLinus Torvalds 		outw(val, port);
6271da177e4SLinus Torvalds 		break;
6281da177e4SLinus Torvalds 	case 4:
6291da177e4SLinus Torvalds 		outl(val, port);
6301da177e4SLinus Torvalds 		break;
6311da177e4SLinus Torvalds 	default:
6321da177e4SLinus Torvalds 		ret = -EINVAL;
6331da177e4SLinus Torvalds 		break;
6341da177e4SLinus Torvalds 	}
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	return ret;
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds /**
6401da177e4SLinus Torvalds  * pci_cacheline_size - determine cacheline size for PCI devices
6411da177e4SLinus Torvalds  * @dev: void
6421da177e4SLinus Torvalds  *
6431da177e4SLinus Torvalds  * We want to use the line-size of the outer-most cache.  We assume
6441da177e4SLinus Torvalds  * that this line-size is the same for all CPUs.
6451da177e4SLinus Torvalds  *
6461da177e4SLinus Torvalds  * Code mostly taken from arch/ia64/kernel/palinfo.c:cache_info().
6471da177e4SLinus Torvalds  *
6481da177e4SLinus Torvalds  * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
6491da177e4SLinus Torvalds  */
6501da177e4SLinus Torvalds static unsigned long
6511da177e4SLinus Torvalds pci_cacheline_size (void)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	u64 levels, unique_caches;
6541da177e4SLinus Torvalds 	s64 status;
6551da177e4SLinus Torvalds 	pal_cache_config_info_t cci;
6561da177e4SLinus Torvalds 	static u8 cacheline_size;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	if (cacheline_size)
6591da177e4SLinus Torvalds 		return cacheline_size;
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	status = ia64_pal_cache_summary(&levels, &unique_caches);
6621da177e4SLinus Torvalds 	if (status != 0) {
6631da177e4SLinus Torvalds 		printk(KERN_ERR "%s: ia64_pal_cache_summary() failed (status=%ld)\n",
6641da177e4SLinus Torvalds 		       __FUNCTION__, status);
6651da177e4SLinus Torvalds 		return SMP_CACHE_BYTES;
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	status = ia64_pal_cache_config_info(levels - 1, /* cache_type (data_or_unified)= */ 2,
6691da177e4SLinus Torvalds 					    &cci);
6701da177e4SLinus Torvalds 	if (status != 0) {
6711da177e4SLinus Torvalds 		printk(KERN_ERR "%s: ia64_pal_cache_config_info() failed (status=%ld)\n",
6721da177e4SLinus Torvalds 		       __FUNCTION__, status);
6731da177e4SLinus Torvalds 		return SMP_CACHE_BYTES;
6741da177e4SLinus Torvalds 	}
6751da177e4SLinus Torvalds 	cacheline_size = 1 << cci.pcci_line_size;
6761da177e4SLinus Torvalds 	return cacheline_size;
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds /**
6801da177e4SLinus Torvalds  * pcibios_prep_mwi - helper function for drivers/pci/pci.c:pci_set_mwi()
6811da177e4SLinus Torvalds  * @dev: the PCI device for which MWI is enabled
6821da177e4SLinus Torvalds  *
6831da177e4SLinus Torvalds  * For ia64, we can get the cacheline sizes from PAL.
6841da177e4SLinus Torvalds  *
6851da177e4SLinus Torvalds  * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
6861da177e4SLinus Torvalds  */
6871da177e4SLinus Torvalds int
6881da177e4SLinus Torvalds pcibios_prep_mwi (struct pci_dev *dev)
6891da177e4SLinus Torvalds {
6901da177e4SLinus Torvalds 	unsigned long desired_linesize, current_linesize;
6911da177e4SLinus Torvalds 	int rc = 0;
6921da177e4SLinus Torvalds 	u8 pci_linesize;
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	desired_linesize = pci_cacheline_size();
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &pci_linesize);
6971da177e4SLinus Torvalds 	current_linesize = 4 * pci_linesize;
6981da177e4SLinus Torvalds 	if (desired_linesize != current_linesize) {
6991da177e4SLinus Torvalds 		printk(KERN_WARNING "PCI: slot %s has incorrect PCI cache line size of %lu bytes,",
7001da177e4SLinus Torvalds 		       pci_name(dev), current_linesize);
7011da177e4SLinus Torvalds 		if (current_linesize > desired_linesize) {
7021da177e4SLinus Torvalds 			printk(" expected %lu bytes instead\n", desired_linesize);
7031da177e4SLinus Torvalds 			rc = -EINVAL;
7041da177e4SLinus Torvalds 		} else {
7051da177e4SLinus Torvalds 			printk(" correcting to %lu\n", desired_linesize);
7061da177e4SLinus Torvalds 			pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, desired_linesize / 4);
7071da177e4SLinus Torvalds 		}
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 	return rc;
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds int pci_vector_resources(int last, int nr_released)
7131da177e4SLinus Torvalds {
7141da177e4SLinus Torvalds 	int count = nr_released;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds  	count += (IA64_LAST_DEVICE_VECTOR - last);
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	return count;
7191da177e4SLinus Torvalds }
720