blob: c7b1ebfb7da79b3ee0f7b3cdaad17ba760b9e05e [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Andi Kleen69e1a332005-09-12 18:49:24 +02007#include <asm/numa.h>
Jaswinder Singh Rajput82487712008-12-27 18:32:28 +05308#include <asm/pci_x86.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Gary Hade62f420f2007-10-03 15:56:51 -070010struct pci_root_info {
Bjorn Helgaas42887b22009-10-06 15:33:49 -060011 struct acpi_device *bridge;
Gary Hade62f420f2007-10-03 15:56:51 -070012 char *name;
13 unsigned int res_num;
14 struct resource *res;
15 struct pci_bus *bus;
16 int busnum;
17};
18
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -070019static bool pci_use_crs = true;
20
21static int __init set_use_crs(const struct dmi_system_id *id)
22{
23 pci_use_crs = true;
24 return 0;
25}
26
27static const struct dmi_system_id pci_use_crs_table[] __initconst = {
28 /* http://bugzilla.kernel.org/show_bug.cgi?id=14183 */
29 {
30 .callback = set_use_crs,
31 .ident = "IBM System x3800",
32 .matches = {
33 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
34 DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
35 },
36 },
37 {}
38};
39
40void __init pci_acpi_crs_quirks(void)
41{
42 int year;
43
44 if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) && year < 2008)
45 pci_use_crs = false;
46
47 dmi_check_system(pci_use_crs_table);
48
49 /*
50 * If the user specifies "pci=use_crs" or "pci=nocrs" explicitly, that
51 * takes precedence over anything we figured out above.
52 */
53 if (pci_probe & PCI_ROOT_NO_CRS)
54 pci_use_crs = false;
55 else if (pci_probe & PCI_USE__CRS)
56 pci_use_crs = true;
57
58 printk(KERN_INFO "PCI: %s host bridge windows from ACPI; "
59 "if necessary, use \"pci=%s\" and report a bug\n",
60 pci_use_crs ? "Using" : "Ignoring",
61 pci_use_crs ? "nocrs" : "use_crs");
62}
63
Gary Hade62f420f2007-10-03 15:56:51 -070064static acpi_status
65resource_to_addr(struct acpi_resource *resource,
66 struct acpi_resource_address64 *addr)
67{
68 acpi_status status;
69
70 status = acpi_resource_to_address64(resource, addr);
71 if (ACPI_SUCCESS(status) &&
72 (addr->resource_type == ACPI_MEMORY_RANGE ||
73 addr->resource_type == ACPI_IO_RANGE) &&
74 addr->address_length > 0 &&
75 addr->producer_consumer == ACPI_PRODUCER) {
76 return AE_OK;
77 }
78 return AE_ERROR;
79}
80
81static acpi_status
82count_resource(struct acpi_resource *acpi_res, void *data)
83{
84 struct pci_root_info *info = data;
85 struct acpi_resource_address64 addr;
86 acpi_status status;
87
88 status = resource_to_addr(acpi_res, &addr);
89 if (ACPI_SUCCESS(status))
90 info->res_num++;
91 return AE_OK;
92}
93
Bjorn Helgaas03db42a2009-11-04 10:39:18 -070094static void
95align_resource(struct acpi_device *bridge, struct resource *res)
96{
97 int align = (res->flags & IORESOURCE_MEM) ? 16 : 4;
98
99 /*
100 * Host bridge windows are not BARs, but the decoders on the PCI side
101 * that claim this address space have starting alignment and length
102 * constraints, so fix any obvious BIOS goofs.
103 */
Bjorn Helgaasea7f1b62009-11-05 11:17:11 -0600104 if (!IS_ALIGNED(res->start, align)) {
Bjorn Helgaas03db42a2009-11-04 10:39:18 -0700105 dev_printk(KERN_DEBUG, &bridge->dev,
106 "host bridge window %pR invalid; "
107 "aligning start to %d-byte boundary\n", res, align);
108 res->start &= ~(align - 1);
109 }
Bjorn Helgaasea7f1b62009-11-05 11:17:11 -0600110 if (!IS_ALIGNED(res->end + 1, align)) {
Bjorn Helgaas03db42a2009-11-04 10:39:18 -0700111 dev_printk(KERN_DEBUG, &bridge->dev,
112 "host bridge window %pR invalid; "
113 "aligning end to %d-byte boundary\n", res, align);
Bjorn Helgaasea7f1b62009-11-05 11:17:11 -0600114 res->end = ALIGN(res->end, align) - 1;
Bjorn Helgaas03db42a2009-11-04 10:39:18 -0700115 }
116}
117
Gary Hade62f420f2007-10-03 15:56:51 -0700118static acpi_status
119setup_resource(struct acpi_resource *acpi_res, void *data)
120{
121 struct pci_root_info *info = data;
122 struct resource *res;
123 struct acpi_resource_address64 addr;
124 acpi_status status;
125 unsigned long flags;
Bjorn Helgaaseb9fc8e2010-03-25 09:28:24 -0600126 struct resource *root, *conflict;
Bjorn Helgaasd558b482010-03-25 09:28:30 -0600127 u64 start, end, max_len;
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700128
Gary Hade62f420f2007-10-03 15:56:51 -0700129 status = resource_to_addr(acpi_res, &addr);
130 if (!ACPI_SUCCESS(status))
131 return AE_OK;
132
133 if (addr.resource_type == ACPI_MEMORY_RANGE) {
134 root = &iomem_resource;
135 flags = IORESOURCE_MEM;
136 if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
137 flags |= IORESOURCE_PREFETCH;
138 } else if (addr.resource_type == ACPI_IO_RANGE) {
139 root = &ioport_resource;
140 flags = IORESOURCE_IO;
141 } else
142 return AE_OK;
143
Bjorn Helgaasd558b482010-03-25 09:28:30 -0600144 max_len = addr.maximum - addr.minimum + 1;
145 if (addr.address_length > max_len) {
146 dev_printk(KERN_DEBUG, &info->bridge->dev,
147 "host bridge window length %#llx doesn't fit in "
148 "%#llx-%#llx, trimming\n",
149 (unsigned long long) addr.address_length,
150 (unsigned long long) addr.minimum,
151 (unsigned long long) addr.maximum);
152 addr.address_length = max_len;
153 }
154
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700155 start = addr.minimum + addr.translation_offset;
156 end = start + addr.address_length - 1;
Gary Hadef9cde5f2009-05-27 12:41:44 -0700157
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700158 res = &info->res[info->res_num];
159 res->name = info->name;
160 res->flags = flags;
161 res->start = start;
162 res->end = end;
163 res->child = NULL;
Bjorn Helgaas03db42a2009-11-04 10:39:18 -0700164 align_resource(info->bridge, res);
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700165
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -0700166 if (!pci_use_crs) {
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700167 dev_printk(KERN_DEBUG, &info->bridge->dev,
168 "host bridge window %pR (ignored)\n", res);
169 return AE_OK;
170 }
171
Bjorn Helgaaseb9fc8e2010-03-25 09:28:24 -0600172 conflict = insert_resource_conflict(root, res);
173 if (conflict) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600174 dev_err(&info->bridge->dev,
Bjorn Helgaaseb9fc8e2010-03-25 09:28:24 -0600175 "address space collision: host bridge window %pR "
176 "conflicts with %s %pR\n",
177 res, conflict->name, conflict);
Gary Hade62f420f2007-10-03 15:56:51 -0700178 } else {
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700179 pci_bus_add_resource(info->bus, res, 0);
Gary Hade62f420f2007-10-03 15:56:51 -0700180 info->res_num++;
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600181 if (addr.translation_offset)
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600182 dev_info(&info->bridge->dev, "host bridge window %pR "
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600183 "(PCI address [%#llx-%#llx])\n",
184 res, res->start - addr.translation_offset,
185 res->end - addr.translation_offset);
186 else
187 dev_info(&info->bridge->dev,
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600188 "host bridge window %pR\n", res);
Gary Hade62f420f2007-10-03 15:56:51 -0700189 }
190 return AE_OK;
191}
192
193static void
Gary Hade62f420f2007-10-03 15:56:51 -0700194get_current_resources(struct acpi_device *device, int busnum,
Gary Hadecb3576f2008-02-08 14:00:52 -0800195 int domain, struct pci_bus *bus)
Gary Hade62f420f2007-10-03 15:56:51 -0700196{
197 struct pci_root_info info;
198 size_t size;
199
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -0700200 if (pci_use_crs)
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700201 pci_bus_remove_resources(bus);
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700202
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600203 info.bridge = device;
Gary Hade62f420f2007-10-03 15:56:51 -0700204 info.bus = bus;
205 info.res_num = 0;
206 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
207 &info);
208 if (!info.res_num)
209 return;
210
211 size = sizeof(*info.res) * info.res_num;
212 info.res = kmalloc(size, GFP_KERNEL);
213 if (!info.res)
214 goto res_alloc_fail;
215
Gary Hadecb3576f2008-02-08 14:00:52 -0800216 info.name = kmalloc(16, GFP_KERNEL);
Gary Hade62f420f2007-10-03 15:56:51 -0700217 if (!info.name)
218 goto name_alloc_fail;
Gary Hadecb3576f2008-02-08 14:00:52 -0800219 sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
Gary Hade62f420f2007-10-03 15:56:51 -0700220
221 info.res_num = 0;
222 acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
223 &info);
Gary Hade62f420f2007-10-03 15:56:51 -0700224
225 return;
226
227name_alloc_fail:
228 kfree(info.res);
229res_alloc_fail:
230 return;
231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
234{
Andi Kleen69e1a332005-09-12 18:49:24 +0200235 struct pci_bus *bus;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300236 struct pci_sysdata *sd;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800237 int node;
238#ifdef CONFIG_ACPI_NUMA
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300239 int pxm;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800240#endif
Andi Kleen69e1a332005-09-12 18:49:24 +0200241
Jeff Garzika79e4192007-10-11 16:58:30 -0400242 if (domain && !pci_domains_supported) {
Bjorn Helgaas2a6bed82009-11-04 10:32:47 -0700243 printk(KERN_WARNING "pci_bus %04x:%02x: "
244 "ignored (multiple domains not supported)\n",
245 domain, busnum);
Jeff Garzika79e4192007-10-11 16:58:30 -0400246 return NULL;
247 }
248
Yinghai Lu871d5f82008-02-19 03:20:09 -0800249 node = -1;
250#ifdef CONFIG_ACPI_NUMA
251 pxm = acpi_get_pxm(device->handle);
252 if (pxm >= 0)
253 node = pxm_to_node(pxm);
254 if (node != -1)
255 set_mp_bus_to_node(busnum, node);
256 else
Yinghai Lu871d5f82008-02-19 03:20:09 -0800257#endif
Yinghai Lu871d5f82008-02-19 03:20:09 -0800258 node = get_mp_bus_to_node(busnum);
Yinghai Lub755de82008-02-20 12:41:52 -0800259
260 if (node != -1 && !node_online(node))
261 node = -1;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800262
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300263 /* Allocate per-root-bus (not per bus) arch-specific data.
264 * TODO: leak; this memory is never freed.
265 * It's arguable whether it's worth the trouble to care.
266 */
267 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
268 if (!sd) {
Bjorn Helgaas2a6bed82009-11-04 10:32:47 -0700269 printk(KERN_WARNING "pci_bus %04x:%02x: "
270 "ignored (out of memory)\n", domain, busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return NULL;
272 }
273
Jeff Garzika79e4192007-10-11 16:58:30 -0400274 sd->domain = domain;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800275 sd->node = node;
yakui.zhao@intel.comb87e81e2008-04-15 14:34:49 -0700276 /*
277 * Maybe the desired pci bus has been already scanned. In such case
278 * it is unnecessary to scan the pci bus with the given domain,busnum.
279 */
280 bus = pci_find_bus(domain, busnum);
281 if (bus) {
282 /*
283 * If the desired bus exits, the content of bus->sysdata will
284 * be replaced by sd.
285 */
286 memcpy(bus->sysdata, sd, sizeof(*sd));
287 kfree(sd);
Yinghai Lu626fdfe2009-06-24 20:00:12 -0700288 } else {
289 bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
290 if (bus) {
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700291 get_current_resources(device, busnum, domain, bus);
Yinghai Lu626fdfe2009-06-24 20:00:12 -0700292 bus->subordinate = pci_scan_child_bus(bus);
293 }
294 }
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300295
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300296 if (!bus)
297 kfree(sd);
298
Yinghai Ludbb61522008-04-19 01:30:16 -0700299 if (bus && node != -1) {
Andi Kleen69e1a332005-09-12 18:49:24 +0200300#ifdef CONFIG_ACPI_NUMA
Yinghai Ludbb61522008-04-19 01:30:16 -0700301 if (pxm >= 0)
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700302 dev_printk(KERN_DEBUG, &bus->dev,
303 "on NUMA node %d (pxm %d)\n", node, pxm);
Yinghai Ludbb61522008-04-19 01:30:16 -0700304#else
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700305 dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
Andi Kleen69e1a332005-09-12 18:49:24 +0200306#endif
Yinghai Ludbb61522008-04-19 01:30:16 -0700307 }
Gary Hade62f420f2007-10-03 15:56:51 -0700308
Andi Kleen69e1a332005-09-12 18:49:24 +0200309 return bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Robert Richter8dd779b2008-07-02 22:50:29 +0200312int __init pci_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct pci_dev *dev = NULL;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (acpi_noirq)
Thomas Gleixnerb72d0db2009-08-29 16:24:51 +0200317 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
320 acpi_irq_penalty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 pcibios_enable_irq = acpi_pci_irq_enable;
David Shaohua Li87bec662005-07-27 23:02:00 -0400322 pcibios_disable_irq = acpi_pci_irq_disable;
Thomas Gleixnerab3b3792009-08-29 17:47:33 +0200323 x86_init.pci.init_irq = x86_init_noop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (pci_routeirq) {
326 /*
327 * PCI IRQ routing is set up by pci_enable_device(), but we
328 * also do it here in case there are still broken drivers that
329 * don't use pci_enable_device().
330 */
331 printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
Hanna Linderfb37fb92005-11-06 23:39:36 -0800332 for_each_pci_dev(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 acpi_pci_irq_enable(dev);
Bjorn Helgaas657472e92008-02-18 09:44:13 -0700334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
337}