blob: a2f8cdb8c1d5d10cceb424e70e6053716f4d3906 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/pci.h>
2#include <linux/acpi.h>
3#include <linux/init.h>
Nick Pigginb33fa1f2005-10-01 02:34:42 +10004#include <linux/irq.h>
Gary Hade036fff42007-10-03 15:56:14 -07005#include <linux/dmi.h>
Andi Kleen69e1a332005-09-12 18:49:24 +02006#include <asm/numa.h>
Jaswinder Singh Rajput82487712008-12-27 18:32:28 +05307#include <asm/pci_x86.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Gary Hade62f420f2007-10-03 15:56:51 -07009struct pci_root_info {
Bjorn Helgaas42887b22009-10-06 15:33:49 -060010 struct acpi_device *bridge;
Gary Hade62f420f2007-10-03 15:56:51 -070011 char *name;
12 unsigned int res_num;
13 struct resource *res;
14 struct pci_bus *bus;
15 int busnum;
16};
17
18static acpi_status
19resource_to_addr(struct acpi_resource *resource,
20 struct acpi_resource_address64 *addr)
21{
22 acpi_status status;
23
24 status = acpi_resource_to_address64(resource, addr);
25 if (ACPI_SUCCESS(status) &&
26 (addr->resource_type == ACPI_MEMORY_RANGE ||
27 addr->resource_type == ACPI_IO_RANGE) &&
28 addr->address_length > 0 &&
29 addr->producer_consumer == ACPI_PRODUCER) {
30 return AE_OK;
31 }
32 return AE_ERROR;
33}
34
35static acpi_status
36count_resource(struct acpi_resource *acpi_res, void *data)
37{
38 struct pci_root_info *info = data;
39 struct acpi_resource_address64 addr;
40 acpi_status status;
41
42 status = resource_to_addr(acpi_res, &addr);
43 if (ACPI_SUCCESS(status))
44 info->res_num++;
45 return AE_OK;
46}
47
Bjorn Helgaas03db42a2009-11-04 10:39:18 -070048static void
49align_resource(struct acpi_device *bridge, struct resource *res)
50{
51 int align = (res->flags & IORESOURCE_MEM) ? 16 : 4;
52
53 /*
54 * Host bridge windows are not BARs, but the decoders on the PCI side
55 * that claim this address space have starting alignment and length
56 * constraints, so fix any obvious BIOS goofs.
57 */
Bjorn Helgaasea7f1b62009-11-05 11:17:11 -060058 if (!IS_ALIGNED(res->start, align)) {
Bjorn Helgaas03db42a2009-11-04 10:39:18 -070059 dev_printk(KERN_DEBUG, &bridge->dev,
60 "host bridge window %pR invalid; "
61 "aligning start to %d-byte boundary\n", res, align);
62 res->start &= ~(align - 1);
63 }
Bjorn Helgaasea7f1b62009-11-05 11:17:11 -060064 if (!IS_ALIGNED(res->end + 1, align)) {
Bjorn Helgaas03db42a2009-11-04 10:39:18 -070065 dev_printk(KERN_DEBUG, &bridge->dev,
66 "host bridge window %pR invalid; "
67 "aligning end to %d-byte boundary\n", res, align);
Bjorn Helgaasea7f1b62009-11-05 11:17:11 -060068 res->end = ALIGN(res->end, align) - 1;
Bjorn Helgaas03db42a2009-11-04 10:39:18 -070069 }
70}
71
Gary Hade62f420f2007-10-03 15:56:51 -070072static acpi_status
73setup_resource(struct acpi_resource *acpi_res, void *data)
74{
75 struct pci_root_info *info = data;
76 struct resource *res;
77 struct acpi_resource_address64 addr;
78 acpi_status status;
79 unsigned long flags;
80 struct resource *root;
Yinghai Lu2cdb3f12009-06-24 19:01:19 -070081 u64 start, end;
82
Gary Hade62f420f2007-10-03 15:56:51 -070083 status = resource_to_addr(acpi_res, &addr);
84 if (!ACPI_SUCCESS(status))
85 return AE_OK;
86
87 if (addr.resource_type == ACPI_MEMORY_RANGE) {
88 root = &iomem_resource;
89 flags = IORESOURCE_MEM;
90 if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
91 flags |= IORESOURCE_PREFETCH;
92 } else if (addr.resource_type == ACPI_IO_RANGE) {
93 root = &ioport_resource;
94 flags = IORESOURCE_IO;
95 } else
96 return AE_OK;
97
Yinghai Lu2cdb3f12009-06-24 19:01:19 -070098 start = addr.minimum + addr.translation_offset;
99 end = start + addr.address_length - 1;
Gary Hadef9cde5f2009-05-27 12:41:44 -0700100
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700101 res = &info->res[info->res_num];
102 res->name = info->name;
103 res->flags = flags;
104 res->start = start;
105 res->end = end;
106 res->child = NULL;
Bjorn Helgaas03db42a2009-11-04 10:39:18 -0700107 align_resource(info->bridge, res);
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700108
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700109 if (!(pci_probe & PCI_USE__CRS)) {
110 dev_printk(KERN_DEBUG, &info->bridge->dev,
111 "host bridge window %pR (ignored)\n", res);
112 return AE_OK;
113 }
114
Gary Hade62f420f2007-10-03 15:56:51 -0700115 if (insert_resource(root, res)) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600116 dev_err(&info->bridge->dev,
117 "can't allocate host bridge window %pR\n", res);
Gary Hade62f420f2007-10-03 15:56:51 -0700118 } else {
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700119 pci_bus_add_resource(info->bus, res, 0);
Gary Hade62f420f2007-10-03 15:56:51 -0700120 info->res_num++;
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600121 if (addr.translation_offset)
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600122 dev_info(&info->bridge->dev, "host bridge window %pR "
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600123 "(PCI address [%#llx-%#llx])\n",
124 res, res->start - addr.translation_offset,
125 res->end - addr.translation_offset);
126 else
127 dev_info(&info->bridge->dev,
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600128 "host bridge window %pR\n", res);
Gary Hade62f420f2007-10-03 15:56:51 -0700129 }
130 return AE_OK;
131}
132
133static void
Gary Hade62f420f2007-10-03 15:56:51 -0700134get_current_resources(struct acpi_device *device, int busnum,
Gary Hadecb3576f2008-02-08 14:00:52 -0800135 int domain, struct pci_bus *bus)
Gary Hade62f420f2007-10-03 15:56:51 -0700136{
137 struct pci_root_info info;
138 size_t size;
139
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700140 if (pci_probe & PCI_USE__CRS)
141 pci_bus_remove_resources(bus);
142 else
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700143 dev_info(&device->dev,
144 "ignoring host bridge windows from ACPI; "
145 "boot with \"pci=use_crs\" to use them\n");
146
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600147 info.bridge = device;
Gary Hade62f420f2007-10-03 15:56:51 -0700148 info.bus = bus;
149 info.res_num = 0;
150 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
151 &info);
152 if (!info.res_num)
153 return;
154
155 size = sizeof(*info.res) * info.res_num;
156 info.res = kmalloc(size, GFP_KERNEL);
157 if (!info.res)
158 goto res_alloc_fail;
159
Gary Hadecb3576f2008-02-08 14:00:52 -0800160 info.name = kmalloc(16, GFP_KERNEL);
Gary Hade62f420f2007-10-03 15:56:51 -0700161 if (!info.name)
162 goto name_alloc_fail;
Gary Hadecb3576f2008-02-08 14:00:52 -0800163 sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
Gary Hade62f420f2007-10-03 15:56:51 -0700164
165 info.res_num = 0;
166 acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
167 &info);
Gary Hade62f420f2007-10-03 15:56:51 -0700168
169 return;
170
171name_alloc_fail:
172 kfree(info.res);
173res_alloc_fail:
174 return;
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
178{
Andi Kleen69e1a332005-09-12 18:49:24 +0200179 struct pci_bus *bus;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300180 struct pci_sysdata *sd;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800181 int node;
182#ifdef CONFIG_ACPI_NUMA
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300183 int pxm;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800184#endif
Andi Kleen69e1a332005-09-12 18:49:24 +0200185
Jeff Garzika79e4192007-10-11 16:58:30 -0400186 if (domain && !pci_domains_supported) {
Bjorn Helgaas2a6bed82009-11-04 10:32:47 -0700187 printk(KERN_WARNING "pci_bus %04x:%02x: "
188 "ignored (multiple domains not supported)\n",
189 domain, busnum);
Jeff Garzika79e4192007-10-11 16:58:30 -0400190 return NULL;
191 }
192
Yinghai Lu871d5f82008-02-19 03:20:09 -0800193 node = -1;
194#ifdef CONFIG_ACPI_NUMA
195 pxm = acpi_get_pxm(device->handle);
196 if (pxm >= 0)
197 node = pxm_to_node(pxm);
198 if (node != -1)
199 set_mp_bus_to_node(busnum, node);
200 else
Yinghai Lu871d5f82008-02-19 03:20:09 -0800201#endif
Yinghai Lu871d5f82008-02-19 03:20:09 -0800202 node = get_mp_bus_to_node(busnum);
Yinghai Lub755de82008-02-20 12:41:52 -0800203
204 if (node != -1 && !node_online(node))
205 node = -1;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800206
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300207 /* Allocate per-root-bus (not per bus) arch-specific data.
208 * TODO: leak; this memory is never freed.
209 * It's arguable whether it's worth the trouble to care.
210 */
211 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
212 if (!sd) {
Bjorn Helgaas2a6bed82009-11-04 10:32:47 -0700213 printk(KERN_WARNING "pci_bus %04x:%02x: "
214 "ignored (out of memory)\n", domain, busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return NULL;
216 }
217
Jeff Garzika79e4192007-10-11 16:58:30 -0400218 sd->domain = domain;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800219 sd->node = node;
yakui.zhao@intel.comb87e81e2008-04-15 14:34:49 -0700220 /*
221 * Maybe the desired pci bus has been already scanned. In such case
222 * it is unnecessary to scan the pci bus with the given domain,busnum.
223 */
224 bus = pci_find_bus(domain, busnum);
225 if (bus) {
226 /*
227 * If the desired bus exits, the content of bus->sysdata will
228 * be replaced by sd.
229 */
230 memcpy(bus->sysdata, sd, sizeof(*sd));
231 kfree(sd);
Yinghai Lu626fdfe2009-06-24 20:00:12 -0700232 } else {
233 bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
234 if (bus) {
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700235 get_current_resources(device, busnum, domain, bus);
Yinghai Lu626fdfe2009-06-24 20:00:12 -0700236 bus->subordinate = pci_scan_child_bus(bus);
237 }
238 }
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300239
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300240 if (!bus)
241 kfree(sd);
242
Yinghai Ludbb61522008-04-19 01:30:16 -0700243 if (bus && node != -1) {
Andi Kleen69e1a332005-09-12 18:49:24 +0200244#ifdef CONFIG_ACPI_NUMA
Yinghai Ludbb61522008-04-19 01:30:16 -0700245 if (pxm >= 0)
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700246 dev_printk(KERN_DEBUG, &bus->dev,
247 "on NUMA node %d (pxm %d)\n", node, pxm);
Yinghai Ludbb61522008-04-19 01:30:16 -0700248#else
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700249 dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
Andi Kleen69e1a332005-09-12 18:49:24 +0200250#endif
Yinghai Ludbb61522008-04-19 01:30:16 -0700251 }
Gary Hade62f420f2007-10-03 15:56:51 -0700252
Andi Kleen69e1a332005-09-12 18:49:24 +0200253 return bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Robert Richter8dd779b2008-07-02 22:50:29 +0200256int __init pci_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct pci_dev *dev = NULL;
259
260 if (pcibios_scanned)
261 return 0;
262
263 if (acpi_noirq)
264 return 0;
265
266 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
267 acpi_irq_penalty_init();
268 pcibios_scanned++;
269 pcibios_enable_irq = acpi_pci_irq_enable;
David Shaohua Li87bec662005-07-27 23:02:00 -0400270 pcibios_disable_irq = acpi_pci_irq_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (pci_routeirq) {
273 /*
274 * PCI IRQ routing is set up by pci_enable_device(), but we
275 * also do it here in case there are still broken drivers that
276 * don't use pci_enable_device().
277 */
278 printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
Hanna Linderfb37fb92005-11-06 23:39:36 -0800279 for_each_pci_dev(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 acpi_pci_irq_enable(dev);
Bjorn Helgaas657472e92008-02-18 09:44:13 -0700281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284}