blob: 0d9329f203e9f9f7ec381d60196124086ca4d966 [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
Dave Jones28c3c052011-12-30 14:37:05 -050027static int __init set_nouse_crs(const struct dmi_system_id *id)
28{
29 pci_use_crs = false;
30 return 0;
31}
32
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -070033static const struct dmi_system_id pci_use_crs_table[] __initconst = {
34 /* http://bugzilla.kernel.org/show_bug.cgi?id=14183 */
35 {
36 .callback = set_use_crs,
37 .ident = "IBM System x3800",
38 .matches = {
39 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
40 DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
41 },
42 },
Bjorn Helgaas24917622010-07-23 12:53:27 -060043 /* https://bugzilla.kernel.org/show_bug.cgi?id=16007 */
44 /* 2006 AMD HT/VIA system with two host bridges */
45 {
46 .callback = set_use_crs,
47 .ident = "ASRock ALiveSATA2-GLAN",
48 .matches = {
49 DMI_MATCH(DMI_PRODUCT_NAME, "ALiveSATA2-GLAN"),
50 },
51 },
Paul Menzel29cf7a302011-08-31 17:07:10 +020052 /* https://bugzilla.kernel.org/show_bug.cgi?id=30552 */
53 /* 2006 AMD HT/VIA system with two host bridges */
54 {
55 .callback = set_use_crs,
56 .ident = "ASUS M2V-MX SE",
57 .matches = {
58 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
59 DMI_MATCH(DMI_BOARD_NAME, "M2V-MX SE"),
60 DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
61 },
62 },
Dave Jones28c3c052011-12-30 14:37:05 -050063
Dave Jonese7027812012-01-04 11:33:12 -050064 /* Now for the blacklist.. */
65
66 /* https://bugzilla.redhat.com/show_bug.cgi?id=769657 */
67 {
68 .callback = set_nouse_crs,
69 .ident = "Dell Studio 1557",
70 .matches = {
71 DMI_MATCH(DMI_BOARD_VENDOR, "Dell Inc."),
72 DMI_MATCH(DMI_PRODUCT_NAME, "Studio 1557"),
73 DMI_MATCH(DMI_BIOS_VERSION, "A09"),
74 },
75 },
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -070076 {}
77};
78
79void __init pci_acpi_crs_quirks(void)
80{
81 int year;
82
83 if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) && year < 2008)
84 pci_use_crs = false;
85
86 dmi_check_system(pci_use_crs_table);
87
88 /*
89 * If the user specifies "pci=use_crs" or "pci=nocrs" explicitly, that
90 * takes precedence over anything we figured out above.
91 */
92 if (pci_probe & PCI_ROOT_NO_CRS)
93 pci_use_crs = false;
94 else if (pci_probe & PCI_USE__CRS)
95 pci_use_crs = true;
96
97 printk(KERN_INFO "PCI: %s host bridge windows from ACPI; "
98 "if necessary, use \"pci=%s\" and report a bug\n",
99 pci_use_crs ? "Using" : "Ignoring",
100 pci_use_crs ? "nocrs" : "use_crs");
101}
102
Gary Hade62f420f2007-10-03 15:56:51 -0700103static acpi_status
104resource_to_addr(struct acpi_resource *resource,
105 struct acpi_resource_address64 *addr)
106{
107 acpi_status status;
Bjorn Helgaas66528fd2010-04-20 13:52:41 -0600108 struct acpi_resource_memory24 *memory24;
109 struct acpi_resource_memory32 *memory32;
110 struct acpi_resource_fixed_memory32 *fixed_memory32;
Gary Hade62f420f2007-10-03 15:56:51 -0700111
Bjorn Helgaas66528fd2010-04-20 13:52:41 -0600112 memset(addr, 0, sizeof(*addr));
113 switch (resource->type) {
114 case ACPI_RESOURCE_TYPE_MEMORY24:
115 memory24 = &resource->data.memory24;
116 addr->resource_type = ACPI_MEMORY_RANGE;
117 addr->minimum = memory24->minimum;
118 addr->address_length = memory24->address_length;
119 addr->maximum = addr->minimum + addr->address_length - 1;
Gary Hade62f420f2007-10-03 15:56:51 -0700120 return AE_OK;
Bjorn Helgaas66528fd2010-04-20 13:52:41 -0600121 case ACPI_RESOURCE_TYPE_MEMORY32:
122 memory32 = &resource->data.memory32;
123 addr->resource_type = ACPI_MEMORY_RANGE;
124 addr->minimum = memory32->minimum;
125 addr->address_length = memory32->address_length;
126 addr->maximum = addr->minimum + addr->address_length - 1;
127 return AE_OK;
128 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
129 fixed_memory32 = &resource->data.fixed_memory32;
130 addr->resource_type = ACPI_MEMORY_RANGE;
131 addr->minimum = fixed_memory32->address;
132 addr->address_length = fixed_memory32->address_length;
133 addr->maximum = addr->minimum + addr->address_length - 1;
134 return AE_OK;
135 case ACPI_RESOURCE_TYPE_ADDRESS16:
136 case ACPI_RESOURCE_TYPE_ADDRESS32:
137 case ACPI_RESOURCE_TYPE_ADDRESS64:
138 status = acpi_resource_to_address64(resource, addr);
139 if (ACPI_SUCCESS(status) &&
140 (addr->resource_type == ACPI_MEMORY_RANGE ||
141 addr->resource_type == ACPI_IO_RANGE) &&
142 addr->address_length > 0) {
143 return AE_OK;
144 }
145 break;
Gary Hade62f420f2007-10-03 15:56:51 -0700146 }
147 return AE_ERROR;
148}
149
150static acpi_status
151count_resource(struct acpi_resource *acpi_res, void *data)
152{
153 struct pci_root_info *info = data;
154 struct acpi_resource_address64 addr;
155 acpi_status status;
156
157 status = resource_to_addr(acpi_res, &addr);
158 if (ACPI_SUCCESS(status))
159 info->res_num++;
160 return AE_OK;
161}
162
163static acpi_status
164setup_resource(struct acpi_resource *acpi_res, void *data)
165{
166 struct pci_root_info *info = data;
167 struct resource *res;
168 struct acpi_resource_address64 addr;
169 acpi_status status;
170 unsigned long flags;
Bjorn Helgaas48728e02010-04-27 14:45:43 -0600171 u64 start, end;
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700172
Gary Hade62f420f2007-10-03 15:56:51 -0700173 status = resource_to_addr(acpi_res, &addr);
174 if (!ACPI_SUCCESS(status))
175 return AE_OK;
176
177 if (addr.resource_type == ACPI_MEMORY_RANGE) {
Gary Hade62f420f2007-10-03 15:56:51 -0700178 flags = IORESOURCE_MEM;
179 if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
180 flags |= IORESOURCE_PREFETCH;
181 } else if (addr.resource_type == ACPI_IO_RANGE) {
Gary Hade62f420f2007-10-03 15:56:51 -0700182 flags = IORESOURCE_IO;
183 } else
184 return AE_OK;
185
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700186 start = addr.minimum + addr.translation_offset;
Bjorn Helgaas48728e02010-04-27 14:45:43 -0600187 end = addr.maximum + addr.translation_offset;
Gary Hadef9cde5f2009-05-27 12:41:44 -0700188
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700189 res = &info->res[info->res_num];
190 res->name = info->name;
191 res->flags = flags;
192 res->start = start;
193 res->end = end;
194 res->child = NULL;
195
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -0700196 if (!pci_use_crs) {
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700197 dev_printk(KERN_DEBUG, &info->bridge->dev,
198 "host bridge window %pR (ignored)\n", res);
199 return AE_OK;
200 }
201
Bjorn Helgaas4723d0f2010-09-22 11:09:19 -0600202 info->res_num++;
203 if (addr.translation_offset)
204 dev_info(&info->bridge->dev, "host bridge window %pR "
205 "(PCI address [%#llx-%#llx])\n",
206 res, res->start - addr.translation_offset,
207 res->end - addr.translation_offset);
208 else
209 dev_info(&info->bridge->dev, "host bridge window %pR\n", res);
210
Gary Hade62f420f2007-10-03 15:56:51 -0700211 return AE_OK;
212}
213
Bjorn Helgaas4723d0f2010-09-22 11:09:19 -0600214static bool resource_contains(struct resource *res, resource_size_t point)
215{
216 if (res->start <= point && point <= res->end)
217 return true;
218 return false;
219}
220
Márton Németh6e33a852011-05-14 19:27:33 +0200221static void coalesce_windows(struct pci_root_info *info, unsigned long type)
Bjorn Helgaas4723d0f2010-09-22 11:09:19 -0600222{
223 int i, j;
224 struct resource *res1, *res2;
225
226 for (i = 0; i < info->res_num; i++) {
227 res1 = &info->res[i];
228 if (!(res1->flags & type))
229 continue;
230
231 for (j = i + 1; j < info->res_num; j++) {
232 res2 = &info->res[j];
233 if (!(res2->flags & type))
234 continue;
235
236 /*
237 * I don't like throwing away windows because then
238 * our resources no longer match the ACPI _CRS, but
239 * the kernel resource tree doesn't allow overlaps.
240 */
241 if (resource_contains(res1, res2->start) ||
242 resource_contains(res1, res2->end) ||
243 resource_contains(res2, res1->start) ||
244 resource_contains(res2, res1->end)) {
245 res1->start = min(res1->start, res2->start);
246 res1->end = max(res1->end, res2->end);
247 dev_info(&info->bridge->dev,
248 "host bridge window expanded to %pR; %pR ignored\n",
249 res1, res2);
250 res2->flags = 0;
251 }
252 }
253 }
254}
255
256static void add_resources(struct pci_root_info *info)
257{
258 int i;
259 struct resource *res, *root, *conflict;
260
261 if (!pci_use_crs)
262 return;
263
264 coalesce_windows(info, IORESOURCE_MEM);
265 coalesce_windows(info, IORESOURCE_IO);
266
267 for (i = 0; i < info->res_num; i++) {
268 res = &info->res[i];
269
270 if (res->flags & IORESOURCE_MEM)
271 root = &iomem_resource;
272 else if (res->flags & IORESOURCE_IO)
273 root = &ioport_resource;
274 else
275 continue;
276
277 conflict = insert_resource_conflict(root, res);
278 if (conflict)
Bjorn Helgaas43d786e2011-07-02 10:47:12 -0600279 dev_info(&info->bridge->dev,
280 "ignoring host bridge window %pR (conflicts with %s %pR)\n",
281 res, conflict->name, conflict);
Bjorn Helgaas4723d0f2010-09-22 11:09:19 -0600282 else
283 pci_bus_add_resource(info->bus, res, 0);
284 }
285}
286
Gary Hade62f420f2007-10-03 15:56:51 -0700287static void
Gary Hade62f420f2007-10-03 15:56:51 -0700288get_current_resources(struct acpi_device *device, int busnum,
Gary Hadecb3576f2008-02-08 14:00:52 -0800289 int domain, struct pci_bus *bus)
Gary Hade62f420f2007-10-03 15:56:51 -0700290{
291 struct pci_root_info info;
292 size_t size;
293
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -0700294 if (pci_use_crs)
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700295 pci_bus_remove_resources(bus);
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700296
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600297 info.bridge = device;
Gary Hade62f420f2007-10-03 15:56:51 -0700298 info.bus = bus;
299 info.res_num = 0;
300 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
301 &info);
302 if (!info.res_num)
303 return;
304
305 size = sizeof(*info.res) * info.res_num;
306 info.res = kmalloc(size, GFP_KERNEL);
307 if (!info.res)
308 goto res_alloc_fail;
309
Julia Lawallb46fc5f2010-05-24 12:13:16 -0700310 info.name = kasprintf(GFP_KERNEL, "PCI Bus %04x:%02x", domain, busnum);
Gary Hade62f420f2007-10-03 15:56:51 -0700311 if (!info.name)
312 goto name_alloc_fail;
Gary Hade62f420f2007-10-03 15:56:51 -0700313
314 info.res_num = 0;
315 acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
316 &info);
Gary Hade62f420f2007-10-03 15:56:51 -0700317
Bjorn Helgaas4723d0f2010-09-22 11:09:19 -0600318 add_resources(&info);
Gary Hade62f420f2007-10-03 15:56:51 -0700319 return;
320
321name_alloc_fail:
322 kfree(info.res);
323res_alloc_fail:
324 return;
325}
326
Bjorn Helgaas57283772010-03-11 12:20:11 -0700327struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_pci_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Bjorn Helgaas57283772010-03-11 12:20:11 -0700329 struct acpi_device *device = root->device;
330 int domain = root->segment;
331 int busnum = root->secondary.start;
Andi Kleen69e1a332005-09-12 18:49:24 +0200332 struct pci_bus *bus;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300333 struct pci_sysdata *sd;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800334 int node;
335#ifdef CONFIG_ACPI_NUMA
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300336 int pxm;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800337#endif
Andi Kleen69e1a332005-09-12 18:49:24 +0200338
Jeff Garzika79e4192007-10-11 16:58:30 -0400339 if (domain && !pci_domains_supported) {
Bjorn Helgaas2a6bed82009-11-04 10:32:47 -0700340 printk(KERN_WARNING "pci_bus %04x:%02x: "
341 "ignored (multiple domains not supported)\n",
342 domain, busnum);
Jeff Garzika79e4192007-10-11 16:58:30 -0400343 return NULL;
344 }
345
Yinghai Lu871d5f82008-02-19 03:20:09 -0800346 node = -1;
347#ifdef CONFIG_ACPI_NUMA
348 pxm = acpi_get_pxm(device->handle);
349 if (pxm >= 0)
350 node = pxm_to_node(pxm);
351 if (node != -1)
352 set_mp_bus_to_node(busnum, node);
353 else
Yinghai Lu871d5f82008-02-19 03:20:09 -0800354#endif
Yinghai Lu871d5f82008-02-19 03:20:09 -0800355 node = get_mp_bus_to_node(busnum);
Yinghai Lub755de82008-02-20 12:41:52 -0800356
357 if (node != -1 && !node_online(node))
358 node = -1;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800359
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300360 /* Allocate per-root-bus (not per bus) arch-specific data.
361 * TODO: leak; this memory is never freed.
362 * It's arguable whether it's worth the trouble to care.
363 */
364 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
365 if (!sd) {
Bjorn Helgaas2a6bed82009-11-04 10:32:47 -0700366 printk(KERN_WARNING "pci_bus %04x:%02x: "
367 "ignored (out of memory)\n", domain, busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return NULL;
369 }
370
Jeff Garzika79e4192007-10-11 16:58:30 -0400371 sd->domain = domain;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800372 sd->node = node;
yakui.zhao@intel.comb87e81e2008-04-15 14:34:49 -0700373 /*
374 * Maybe the desired pci bus has been already scanned. In such case
375 * it is unnecessary to scan the pci bus with the given domain,busnum.
376 */
377 bus = pci_find_bus(domain, busnum);
378 if (bus) {
379 /*
380 * If the desired bus exits, the content of bus->sysdata will
381 * be replaced by sd.
382 */
383 memcpy(bus->sysdata, sd, sizeof(*sd));
384 kfree(sd);
Yinghai Lu626fdfe2009-06-24 20:00:12 -0700385 } else {
386 bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
387 if (bus) {
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700388 get_current_resources(device, busnum, domain, bus);
Yinghai Lu626fdfe2009-06-24 20:00:12 -0700389 bus->subordinate = pci_scan_child_bus(bus);
390 }
391 }
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300392
Jon Masonb03e7492011-07-20 15:20:54 -0500393 /* After the PCI-E bus has been walked and all devices discovered,
394 * configure any settings of the fabric that might be necessary.
395 */
396 if (bus) {
397 struct pci_bus *child;
Shyam Iyer5307f6d2011-09-08 16:41:17 -0500398 list_for_each_entry(child, &bus->children, node) {
399 struct pci_dev *self = child->self;
400 if (!self)
401 continue;
402
403 pcie_bus_configure_settings(child, self->pcie_mpss);
404 }
Jon Masonb03e7492011-07-20 15:20:54 -0500405 }
406
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300407 if (!bus)
408 kfree(sd);
409
Yinghai Ludbb61522008-04-19 01:30:16 -0700410 if (bus && node != -1) {
Andi Kleen69e1a332005-09-12 18:49:24 +0200411#ifdef CONFIG_ACPI_NUMA
Yinghai Ludbb61522008-04-19 01:30:16 -0700412 if (pxm >= 0)
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700413 dev_printk(KERN_DEBUG, &bus->dev,
414 "on NUMA node %d (pxm %d)\n", node, pxm);
Yinghai Ludbb61522008-04-19 01:30:16 -0700415#else
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700416 dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
Andi Kleen69e1a332005-09-12 18:49:24 +0200417#endif
Yinghai Ludbb61522008-04-19 01:30:16 -0700418 }
Gary Hade62f420f2007-10-03 15:56:51 -0700419
Andi Kleen69e1a332005-09-12 18:49:24 +0200420 return bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Robert Richter8dd779b2008-07-02 22:50:29 +0200423int __init pci_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct pci_dev *dev = NULL;
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (acpi_noirq)
Thomas Gleixnerb72d0db2009-08-29 16:24:51 +0200428 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
431 acpi_irq_penalty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 pcibios_enable_irq = acpi_pci_irq_enable;
David Shaohua Li87bec662005-07-27 23:02:00 -0400433 pcibios_disable_irq = acpi_pci_irq_disable;
Thomas Gleixnerab3b3792009-08-29 17:47:33 +0200434 x86_init.pci.init_irq = x86_init_noop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 if (pci_routeirq) {
437 /*
438 * PCI IRQ routing is set up by pci_enable_device(), but we
439 * also do it here in case there are still broken drivers that
440 * don't use pci_enable_device().
441 */
442 printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
Hanna Linderfb37fb92005-11-06 23:39:36 -0800443 for_each_pci_dev(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 acpi_pci_irq_enable(dev);
Bjorn Helgaas657472e92008-02-18 09:44:13 -0700445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return 0;
448}