| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/pci.h> | 
 | 2 | #include <linux/acpi.h> | 
 | 3 | #include <linux/init.h> | 
| Nick Piggin | b33fa1f | 2005-10-01 02:34:42 +1000 | [diff] [blame] | 4 | #include <linux/irq.h> | 
| Gary Hade | 036fff4 | 2007-10-03 15:56:14 -0700 | [diff] [blame] | 5 | #include <linux/dmi.h> | 
| Andi Kleen | 69e1a33 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 6 | #include <asm/numa.h> | 
| Jaswinder Singh Rajput | 8248771 | 2008-12-27 18:32:28 +0530 | [diff] [blame] | 7 | #include <asm/pci_x86.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 |  | 
| Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 9 | struct pci_root_info { | 
 | 10 | 	char *name; | 
 | 11 | 	unsigned int res_num; | 
 | 12 | 	struct resource *res; | 
 | 13 | 	struct pci_bus *bus; | 
 | 14 | 	int busnum; | 
 | 15 | }; | 
 | 16 |  | 
 | 17 | static acpi_status | 
 | 18 | resource_to_addr(struct acpi_resource *resource, | 
 | 19 | 			struct acpi_resource_address64 *addr) | 
 | 20 | { | 
 | 21 | 	acpi_status status; | 
 | 22 |  | 
 | 23 | 	status = acpi_resource_to_address64(resource, addr); | 
 | 24 | 	if (ACPI_SUCCESS(status) && | 
 | 25 | 	    (addr->resource_type == ACPI_MEMORY_RANGE || | 
 | 26 | 	    addr->resource_type == ACPI_IO_RANGE) && | 
 | 27 | 	    addr->address_length > 0 && | 
 | 28 | 	    addr->producer_consumer == ACPI_PRODUCER) { | 
 | 29 | 		return AE_OK; | 
 | 30 | 	} | 
 | 31 | 	return AE_ERROR; | 
 | 32 | } | 
 | 33 |  | 
 | 34 | static acpi_status | 
 | 35 | count_resource(struct acpi_resource *acpi_res, void *data) | 
 | 36 | { | 
 | 37 | 	struct pci_root_info *info = data; | 
 | 38 | 	struct acpi_resource_address64 addr; | 
 | 39 | 	acpi_status status; | 
 | 40 |  | 
| Yinghai Lu | 3d9befd | 2007-11-17 16:27:01 +0100 | [diff] [blame] | 41 | 	if (info->res_num >= PCI_BUS_NUM_RESOURCES) | 
 | 42 | 		return AE_OK; | 
 | 43 |  | 
| Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 44 | 	status = resource_to_addr(acpi_res, &addr); | 
 | 45 | 	if (ACPI_SUCCESS(status)) | 
 | 46 | 		info->res_num++; | 
 | 47 | 	return AE_OK; | 
 | 48 | } | 
 | 49 |  | 
 | 50 | static acpi_status | 
 | 51 | setup_resource(struct acpi_resource *acpi_res, void *data) | 
 | 52 | { | 
 | 53 | 	struct pci_root_info *info = data; | 
 | 54 | 	struct resource *res; | 
 | 55 | 	struct acpi_resource_address64 addr; | 
 | 56 | 	acpi_status status; | 
 | 57 | 	unsigned long flags; | 
 | 58 | 	struct resource *root; | 
 | 59 |  | 
| Yinghai Lu | 3d9befd | 2007-11-17 16:27:01 +0100 | [diff] [blame] | 60 | 	if (info->res_num >= PCI_BUS_NUM_RESOURCES) | 
 | 61 | 		return AE_OK; | 
 | 62 |  | 
| Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 63 | 	status = resource_to_addr(acpi_res, &addr); | 
 | 64 | 	if (!ACPI_SUCCESS(status)) | 
 | 65 | 		return AE_OK; | 
 | 66 |  | 
 | 67 | 	if (addr.resource_type == ACPI_MEMORY_RANGE) { | 
 | 68 | 		root = &iomem_resource; | 
 | 69 | 		flags = IORESOURCE_MEM; | 
 | 70 | 		if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY) | 
 | 71 | 			flags |= IORESOURCE_PREFETCH; | 
 | 72 | 	} else if (addr.resource_type == ACPI_IO_RANGE) { | 
 | 73 | 		root = &ioport_resource; | 
 | 74 | 		flags = IORESOURCE_IO; | 
 | 75 | 	} else | 
 | 76 | 		return AE_OK; | 
 | 77 |  | 
 | 78 | 	res = &info->res[info->res_num]; | 
 | 79 | 	res->name = info->name; | 
 | 80 | 	res->flags = flags; | 
 | 81 | 	res->start = addr.minimum + addr.translation_offset; | 
 | 82 | 	res->end = res->start + addr.address_length - 1; | 
 | 83 | 	res->child = NULL; | 
 | 84 |  | 
 | 85 | 	if (insert_resource(root, res)) { | 
 | 86 | 		printk(KERN_ERR "PCI: Failed to allocate 0x%lx-0x%lx " | 
 | 87 | 			"from %s for %s\n", (unsigned long) res->start, | 
 | 88 | 			(unsigned long) res->end, root->name, info->name); | 
 | 89 | 	} else { | 
 | 90 | 		info->bus->resource[info->res_num] = res; | 
 | 91 | 		info->res_num++; | 
 | 92 | 	} | 
 | 93 | 	return AE_OK; | 
 | 94 | } | 
 | 95 |  | 
 | 96 | static void | 
 | 97 | adjust_transparent_bridge_resources(struct pci_bus *bus) | 
 | 98 | { | 
 | 99 | 	struct pci_dev *dev; | 
 | 100 |  | 
 | 101 | 	list_for_each_entry(dev, &bus->devices, bus_list) { | 
 | 102 | 		int i; | 
 | 103 | 		u16 class = dev->class >> 8; | 
 | 104 |  | 
 | 105 | 		if (class == PCI_CLASS_BRIDGE_PCI && dev->transparent) { | 
 | 106 | 			for(i = 3; i < PCI_BUS_NUM_RESOURCES; i++) | 
 | 107 | 				dev->subordinate->resource[i] = | 
 | 108 | 						dev->bus->resource[i - 3]; | 
 | 109 | 		} | 
 | 110 | 	} | 
 | 111 | } | 
 | 112 |  | 
 | 113 | static void | 
 | 114 | get_current_resources(struct acpi_device *device, int busnum, | 
| Gary Hade | cb3576f | 2008-02-08 14:00:52 -0800 | [diff] [blame] | 115 | 			int domain, struct pci_bus *bus) | 
| Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 116 | { | 
 | 117 | 	struct pci_root_info info; | 
 | 118 | 	size_t size; | 
 | 119 |  | 
 | 120 | 	info.bus = bus; | 
 | 121 | 	info.res_num = 0; | 
 | 122 | 	acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource, | 
 | 123 | 				&info); | 
 | 124 | 	if (!info.res_num) | 
 | 125 | 		return; | 
 | 126 |  | 
 | 127 | 	size = sizeof(*info.res) * info.res_num; | 
 | 128 | 	info.res = kmalloc(size, GFP_KERNEL); | 
 | 129 | 	if (!info.res) | 
 | 130 | 		goto res_alloc_fail; | 
 | 131 |  | 
| Gary Hade | cb3576f | 2008-02-08 14:00:52 -0800 | [diff] [blame] | 132 | 	info.name = kmalloc(16, GFP_KERNEL); | 
| Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 133 | 	if (!info.name) | 
 | 134 | 		goto name_alloc_fail; | 
| Gary Hade | cb3576f | 2008-02-08 14:00:52 -0800 | [diff] [blame] | 135 | 	sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum); | 
| Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 136 |  | 
 | 137 | 	info.res_num = 0; | 
 | 138 | 	acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource, | 
 | 139 | 				&info); | 
 | 140 | 	if (info.res_num) | 
 | 141 | 		adjust_transparent_bridge_resources(bus); | 
 | 142 |  | 
 | 143 | 	return; | 
 | 144 |  | 
 | 145 | name_alloc_fail: | 
 | 146 | 	kfree(info.res); | 
 | 147 | res_alloc_fail: | 
 | 148 | 	return; | 
 | 149 | } | 
 | 150 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum) | 
 | 152 | { | 
| Andi Kleen | 69e1a33 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 153 | 	struct pci_bus *bus; | 
| Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 154 | 	struct pci_sysdata *sd; | 
| Yinghai Lu | 871d5f8 | 2008-02-19 03:20:09 -0800 | [diff] [blame] | 155 | 	int node; | 
 | 156 | #ifdef CONFIG_ACPI_NUMA | 
| Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 157 | 	int pxm; | 
| Yinghai Lu | 871d5f8 | 2008-02-19 03:20:09 -0800 | [diff] [blame] | 158 | #endif | 
| Andi Kleen | 69e1a33 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 159 |  | 
| Jeff Garzik | a79e419 | 2007-10-11 16:58:30 -0400 | [diff] [blame] | 160 | 	if (domain && !pci_domains_supported) { | 
 | 161 | 		printk(KERN_WARNING "PCI: Multiple domains not supported " | 
 | 162 | 		       "(dom %d, bus %d)\n", domain, busnum); | 
 | 163 | 		return NULL; | 
 | 164 | 	} | 
 | 165 |  | 
| Yinghai Lu | 871d5f8 | 2008-02-19 03:20:09 -0800 | [diff] [blame] | 166 | 	node = -1; | 
 | 167 | #ifdef CONFIG_ACPI_NUMA | 
 | 168 | 	pxm = acpi_get_pxm(device->handle); | 
 | 169 | 	if (pxm >= 0) | 
 | 170 | 		node = pxm_to_node(pxm); | 
 | 171 | 	if (node != -1) | 
 | 172 | 		set_mp_bus_to_node(busnum, node); | 
 | 173 | 	else | 
| Yinghai Lu | 871d5f8 | 2008-02-19 03:20:09 -0800 | [diff] [blame] | 174 | #endif | 
| Yinghai Lu | 871d5f8 | 2008-02-19 03:20:09 -0800 | [diff] [blame] | 175 | 		node = get_mp_bus_to_node(busnum); | 
| Yinghai Lu | b755de8 | 2008-02-20 12:41:52 -0800 | [diff] [blame] | 176 |  | 
 | 177 | 	if (node != -1 && !node_online(node)) | 
 | 178 | 		node = -1; | 
| Yinghai Lu | 871d5f8 | 2008-02-19 03:20:09 -0800 | [diff] [blame] | 179 |  | 
| Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 180 | 	/* Allocate per-root-bus (not per bus) arch-specific data. | 
 | 181 | 	 * TODO: leak; this memory is never freed. | 
 | 182 | 	 * It's arguable whether it's worth the trouble to care. | 
 | 183 | 	 */ | 
 | 184 | 	sd = kzalloc(sizeof(*sd), GFP_KERNEL); | 
 | 185 | 	if (!sd) { | 
 | 186 | 		printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | 		return NULL; | 
 | 188 | 	} | 
 | 189 |  | 
| Jeff Garzik | a79e419 | 2007-10-11 16:58:30 -0400 | [diff] [blame] | 190 | 	sd->domain = domain; | 
| Yinghai Lu | 871d5f8 | 2008-02-19 03:20:09 -0800 | [diff] [blame] | 191 | 	sd->node = node; | 
| yakui.zhao@intel.com | b87e81e | 2008-04-15 14:34:49 -0700 | [diff] [blame] | 192 | 	/* | 
 | 193 | 	 * Maybe the desired pci bus has been already scanned. In such case | 
 | 194 | 	 * it is unnecessary to scan the pci bus with the given domain,busnum. | 
 | 195 | 	 */ | 
 | 196 | 	bus = pci_find_bus(domain, busnum); | 
 | 197 | 	if (bus) { | 
 | 198 | 		/* | 
 | 199 | 		 * If the desired bus exits, the content of bus->sysdata will | 
 | 200 | 		 * be replaced by sd. | 
 | 201 | 		 */ | 
 | 202 | 		memcpy(bus->sysdata, sd, sizeof(*sd)); | 
 | 203 | 		kfree(sd); | 
 | 204 | 	} else | 
 | 205 | 		bus = pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd); | 
| Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 206 |  | 
| Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 207 | 	if (!bus) | 
 | 208 | 		kfree(sd); | 
 | 209 |  | 
| Yinghai Lu | dbb6152 | 2008-04-19 01:30:16 -0700 | [diff] [blame] | 210 | 	if (bus && node != -1) { | 
| Andi Kleen | 69e1a33 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 211 | #ifdef CONFIG_ACPI_NUMA | 
| Yinghai Lu | dbb6152 | 2008-04-19 01:30:16 -0700 | [diff] [blame] | 212 | 		if (pxm >= 0) | 
| Bjorn Helgaas | 2b8c2ef | 2008-12-18 16:34:51 -0700 | [diff] [blame] | 213 | 			dev_printk(KERN_DEBUG, &bus->dev, | 
 | 214 | 				   "on NUMA node %d (pxm %d)\n", node, pxm); | 
| Yinghai Lu | dbb6152 | 2008-04-19 01:30:16 -0700 | [diff] [blame] | 215 | #else | 
| Bjorn Helgaas | 2b8c2ef | 2008-12-18 16:34:51 -0700 | [diff] [blame] | 216 | 		dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node); | 
| Andi Kleen | 69e1a33 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 217 | #endif | 
| Yinghai Lu | dbb6152 | 2008-04-19 01:30:16 -0700 | [diff] [blame] | 218 | 	} | 
| Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 219 |  | 
 | 220 | 	if (bus && (pci_probe & PCI_USE__CRS)) | 
| Gary Hade | cb3576f | 2008-02-08 14:00:52 -0800 | [diff] [blame] | 221 | 		get_current_resources(device, busnum, domain, bus); | 
| Andi Kleen | 69e1a33 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 222 | 	return bus; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } | 
 | 224 |  | 
| Robert Richter | 8dd779b | 2008-07-02 22:50:29 +0200 | [diff] [blame] | 225 | int __init pci_acpi_init(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { | 
 | 227 | 	struct pci_dev *dev = NULL; | 
 | 228 |  | 
 | 229 | 	if (pcibios_scanned) | 
 | 230 | 		return 0; | 
 | 231 |  | 
 | 232 | 	if (acpi_noirq) | 
 | 233 | 		return 0; | 
 | 234 |  | 
 | 235 | 	printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n"); | 
 | 236 | 	acpi_irq_penalty_init(); | 
 | 237 | 	pcibios_scanned++; | 
 | 238 | 	pcibios_enable_irq = acpi_pci_irq_enable; | 
| David Shaohua Li | 87bec66 | 2005-07-27 23:02:00 -0400 | [diff] [blame] | 239 | 	pcibios_disable_irq = acpi_pci_irq_disable; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 |  | 
 | 241 | 	if (pci_routeirq) { | 
 | 242 | 		/* | 
 | 243 | 		 * PCI IRQ routing is set up by pci_enable_device(), but we | 
 | 244 | 		 * also do it here in case there are still broken drivers that | 
 | 245 | 		 * don't use pci_enable_device(). | 
 | 246 | 		 */ | 
 | 247 | 		printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n"); | 
| Hanna Linder | fb37fb9 | 2005-11-06 23:39:36 -0800 | [diff] [blame] | 248 | 		for_each_pci_dev(dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | 			acpi_pci_irq_enable(dev); | 
| Bjorn Helgaas | 657472e | 2008-02-18 09:44:13 -0700 | [diff] [blame] | 250 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | 	return 0; | 
 | 253 | } |