Linux-libre 3.6.4-gnu1
[librecmc/linux-libre.git] / arch / x86 / pci / acpi.c
1 #include <linux/pci.h>
2 #include <linux/acpi.h>
3 #include <linux/init.h>
4 #include <linux/irq.h>
5 #include <linux/dmi.h>
6 #include <linux/slab.h>
7 #include <asm/numa.h>
8 #include <asm/pci_x86.h>
9
10 struct pci_root_info {
11         struct acpi_device *bridge;
12         char name[16];
13         unsigned int res_num;
14         struct resource *res;
15         struct pci_sysdata sd;
16 #ifdef  CONFIG_PCI_MMCONFIG
17         bool mcfg_added;
18         u16 segment;
19         u8 start_bus;
20         u8 end_bus;
21 #endif
22 };
23
24 static bool pci_use_crs = true;
25
26 static int __init set_use_crs(const struct dmi_system_id *id)
27 {
28         pci_use_crs = true;
29         return 0;
30 }
31
32 static int __init set_nouse_crs(const struct dmi_system_id *id)
33 {
34         pci_use_crs = false;
35         return 0;
36 }
37
38 static const struct dmi_system_id pci_use_crs_table[] __initconst = {
39         /* http://bugzilla.kernel.org/show_bug.cgi?id=14183 */
40         {
41                 .callback = set_use_crs,
42                 .ident = "IBM System x3800",
43                 .matches = {
44                         DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
45                         DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
46                 },
47         },
48         /* https://bugzilla.kernel.org/show_bug.cgi?id=16007 */
49         /* 2006 AMD HT/VIA system with two host bridges */
50         {
51                 .callback = set_use_crs,
52                 .ident = "ASRock ALiveSATA2-GLAN",
53                 .matches = {
54                         DMI_MATCH(DMI_PRODUCT_NAME, "ALiveSATA2-GLAN"),
55                 },
56         },
57         /* https://bugzilla.kernel.org/show_bug.cgi?id=30552 */
58         /* 2006 AMD HT/VIA system with two host bridges */
59         {
60                 .callback = set_use_crs,
61                 .ident = "ASUS M2V-MX SE",
62                 .matches = {
63                         DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
64                         DMI_MATCH(DMI_BOARD_NAME, "M2V-MX SE"),
65                         DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
66                 },
67         },
68         /* https://bugzilla.kernel.org/show_bug.cgi?id=42619 */
69         {
70                 .callback = set_use_crs,
71                 .ident = "MSI MS-7253",
72                 .matches = {
73                         DMI_MATCH(DMI_BOARD_VENDOR, "MICRO-STAR INTERNATIONAL CO., LTD"),
74                         DMI_MATCH(DMI_BOARD_NAME, "MS-7253"),
75                         DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies, LTD"),
76                 },
77         },
78
79         /* Now for the blacklist.. */
80
81         /* https://bugzilla.redhat.com/show_bug.cgi?id=769657 */
82         {
83                 .callback = set_nouse_crs,
84                 .ident = "Dell Studio 1557",
85                 .matches = {
86                         DMI_MATCH(DMI_BOARD_VENDOR, "Dell Inc."),
87                         DMI_MATCH(DMI_PRODUCT_NAME, "Studio 1557"),
88                         DMI_MATCH(DMI_BIOS_VERSION, "A09"),
89                 },
90         },
91         /* https://bugzilla.redhat.com/show_bug.cgi?id=769657 */
92         {
93                 .callback = set_nouse_crs,
94                 .ident = "Thinkpad SL510",
95                 .matches = {
96                         DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
97                         DMI_MATCH(DMI_BOARD_NAME, "2847DFG"),
98                         DMI_MATCH(DMI_BIOS_VERSION, "6JET85WW (1.43 )"),
99                 },
100         },
101         {}
102 };
103
104 void __init pci_acpi_crs_quirks(void)
105 {
106         int year;
107
108         if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) && year < 2008)
109                 pci_use_crs = false;
110
111         dmi_check_system(pci_use_crs_table);
112
113         /*
114          * If the user specifies "pci=use_crs" or "pci=nocrs" explicitly, that
115          * takes precedence over anything we figured out above.
116          */
117         if (pci_probe & PCI_ROOT_NO_CRS)
118                 pci_use_crs = false;
119         else if (pci_probe & PCI_USE__CRS)
120                 pci_use_crs = true;
121
122         printk(KERN_INFO "PCI: %s host bridge windows from ACPI; "
123                "if necessary, use \"pci=%s\" and report a bug\n",
124                pci_use_crs ? "Using" : "Ignoring",
125                pci_use_crs ? "nocrs" : "use_crs");
126 }
127
128 #ifdef  CONFIG_PCI_MMCONFIG
129 static int __devinit check_segment(u16 seg, struct device *dev, char *estr)
130 {
131         if (seg) {
132                 dev_err(dev,
133                         "%s can't access PCI configuration "
134                         "space under this host bridge.\n",
135                         estr);
136                 return -EIO;
137         }
138
139         /*
140          * Failure in adding MMCFG information is not fatal,
141          * just can't access extended configuration space of
142          * devices under this host bridge.
143          */
144         dev_warn(dev,
145                  "%s can't access extended PCI configuration "
146                  "space under this bridge.\n",
147                  estr);
148
149         return 0;
150 }
151
152 static int __devinit setup_mcfg_map(struct pci_root_info *info,
153                                     u16 seg, u8 start, u8 end,
154                                     phys_addr_t addr)
155 {
156         int result;
157         struct device *dev = &info->bridge->dev;
158
159         info->start_bus = start;
160         info->end_bus = end;
161         info->mcfg_added = false;
162
163         /* return success if MMCFG is not in use */
164         if (raw_pci_ext_ops && raw_pci_ext_ops != &pci_mmcfg)
165                 return 0;
166
167         if (!(pci_probe & PCI_PROBE_MMCONF))
168                 return check_segment(seg, dev, "MMCONFIG is disabled,");
169
170         result = pci_mmconfig_insert(dev, seg, start, end, addr);
171         if (result == 0) {
172                 /* enable MMCFG if it hasn't been enabled yet */
173                 if (raw_pci_ext_ops == NULL)
174                         raw_pci_ext_ops = &pci_mmcfg;
175                 info->mcfg_added = true;
176         } else if (result != -EEXIST)
177                 return check_segment(seg, dev,
178                          "fail to add MMCONFIG information,");
179
180         return 0;
181 }
182
183 static void teardown_mcfg_map(struct pci_root_info *info)
184 {
185         if (info->mcfg_added) {
186                 pci_mmconfig_delete(info->segment, info->start_bus,
187                                     info->end_bus);
188                 info->mcfg_added = false;
189         }
190 }
191 #else
192 static int __devinit setup_mcfg_map(struct pci_root_info *info,
193                                     u16 seg, u8 start, u8 end,
194                                     phys_addr_t addr)
195 {
196         return 0;
197 }
198 static void teardown_mcfg_map(struct pci_root_info *info)
199 {
200 }
201 #endif
202
203 static acpi_status
204 resource_to_addr(struct acpi_resource *resource,
205                         struct acpi_resource_address64 *addr)
206 {
207         acpi_status status;
208         struct acpi_resource_memory24 *memory24;
209         struct acpi_resource_memory32 *memory32;
210         struct acpi_resource_fixed_memory32 *fixed_memory32;
211
212         memset(addr, 0, sizeof(*addr));
213         switch (resource->type) {
214         case ACPI_RESOURCE_TYPE_MEMORY24:
215                 memory24 = &resource->data.memory24;
216                 addr->resource_type = ACPI_MEMORY_RANGE;
217                 addr->minimum = memory24->minimum;
218                 addr->address_length = memory24->address_length;
219                 addr->maximum = addr->minimum + addr->address_length - 1;
220                 return AE_OK;
221         case ACPI_RESOURCE_TYPE_MEMORY32:
222                 memory32 = &resource->data.memory32;
223                 addr->resource_type = ACPI_MEMORY_RANGE;
224                 addr->minimum = memory32->minimum;
225                 addr->address_length = memory32->address_length;
226                 addr->maximum = addr->minimum + addr->address_length - 1;
227                 return AE_OK;
228         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
229                 fixed_memory32 = &resource->data.fixed_memory32;
230                 addr->resource_type = ACPI_MEMORY_RANGE;
231                 addr->minimum = fixed_memory32->address;
232                 addr->address_length = fixed_memory32->address_length;
233                 addr->maximum = addr->minimum + addr->address_length - 1;
234                 return AE_OK;
235         case ACPI_RESOURCE_TYPE_ADDRESS16:
236         case ACPI_RESOURCE_TYPE_ADDRESS32:
237         case ACPI_RESOURCE_TYPE_ADDRESS64:
238                 status = acpi_resource_to_address64(resource, addr);
239                 if (ACPI_SUCCESS(status) &&
240                     (addr->resource_type == ACPI_MEMORY_RANGE ||
241                     addr->resource_type == ACPI_IO_RANGE) &&
242                     addr->address_length > 0) {
243                         return AE_OK;
244                 }
245                 break;
246         }
247         return AE_ERROR;
248 }
249
250 static acpi_status
251 count_resource(struct acpi_resource *acpi_res, void *data)
252 {
253         struct pci_root_info *info = data;
254         struct acpi_resource_address64 addr;
255         acpi_status status;
256
257         status = resource_to_addr(acpi_res, &addr);
258         if (ACPI_SUCCESS(status))
259                 info->res_num++;
260         return AE_OK;
261 }
262
263 static acpi_status
264 setup_resource(struct acpi_resource *acpi_res, void *data)
265 {
266         struct pci_root_info *info = data;
267         struct resource *res;
268         struct acpi_resource_address64 addr;
269         acpi_status status;
270         unsigned long flags;
271         u64 start, orig_end, end;
272
273         status = resource_to_addr(acpi_res, &addr);
274         if (!ACPI_SUCCESS(status))
275                 return AE_OK;
276
277         if (addr.resource_type == ACPI_MEMORY_RANGE) {
278                 flags = IORESOURCE_MEM;
279                 if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
280                         flags |= IORESOURCE_PREFETCH;
281         } else if (addr.resource_type == ACPI_IO_RANGE) {
282                 flags = IORESOURCE_IO;
283         } else
284                 return AE_OK;
285
286         start = addr.minimum + addr.translation_offset;
287         orig_end = end = addr.maximum + addr.translation_offset;
288
289         /* Exclude non-addressable range or non-addressable portion of range */
290         end = min(end, (u64)iomem_resource.end);
291         if (end <= start) {
292                 dev_info(&info->bridge->dev,
293                         "host bridge window [%#llx-%#llx] "
294                         "(ignored, not CPU addressable)\n", start, orig_end);
295                 return AE_OK;
296         } else if (orig_end != end) {
297                 dev_info(&info->bridge->dev,
298                         "host bridge window [%#llx-%#llx] "
299                         "([%#llx-%#llx] ignored, not CPU addressable)\n", 
300                         start, orig_end, end + 1, orig_end);
301         }
302
303         res = &info->res[info->res_num];
304         res->name = info->name;
305         res->flags = flags;
306         res->start = start;
307         res->end = end;
308         res->child = NULL;
309
310         if (!pci_use_crs) {
311                 dev_printk(KERN_DEBUG, &info->bridge->dev,
312                            "host bridge window %pR (ignored)\n", res);
313                 return AE_OK;
314         }
315
316         info->res_num++;
317
318         return AE_OK;
319 }
320
321 static void coalesce_windows(struct pci_root_info *info, unsigned long type)
322 {
323         int i, j;
324         struct resource *res1, *res2;
325
326         for (i = 0; i < info->res_num; i++) {
327                 res1 = &info->res[i];
328                 if (!(res1->flags & type))
329                         continue;
330
331                 for (j = i + 1; j < info->res_num; j++) {
332                         res2 = &info->res[j];
333                         if (!(res2->flags & type))
334                                 continue;
335
336                         /*
337                          * I don't like throwing away windows because then
338                          * our resources no longer match the ACPI _CRS, but
339                          * the kernel resource tree doesn't allow overlaps.
340                          */
341                         if (resource_overlaps(res1, res2)) {
342                                 res1->start = min(res1->start, res2->start);
343                                 res1->end = max(res1->end, res2->end);
344                                 dev_info(&info->bridge->dev,
345                                          "host bridge window expanded to %pR; %pR ignored\n",
346                                          res1, res2);
347                                 res2->flags = 0;
348                         }
349                 }
350         }
351 }
352
353 static void add_resources(struct pci_root_info *info,
354                           struct list_head *resources)
355 {
356         int i;
357         struct resource *res, *root, *conflict;
358
359         coalesce_windows(info, IORESOURCE_MEM);
360         coalesce_windows(info, IORESOURCE_IO);
361
362         for (i = 0; i < info->res_num; i++) {
363                 res = &info->res[i];
364
365                 if (res->flags & IORESOURCE_MEM)
366                         root = &iomem_resource;
367                 else if (res->flags & IORESOURCE_IO)
368                         root = &ioport_resource;
369                 else
370                         continue;
371
372                 conflict = insert_resource_conflict(root, res);
373                 if (conflict)
374                         dev_info(&info->bridge->dev,
375                                  "ignoring host bridge window %pR (conflicts with %s %pR)\n",
376                                  res, conflict->name, conflict);
377                 else
378                         pci_add_resource(resources, res);
379         }
380 }
381
382 static void free_pci_root_info_res(struct pci_root_info *info)
383 {
384         kfree(info->res);
385         info->res = NULL;
386         info->res_num = 0;
387 }
388
389 static void __release_pci_root_info(struct pci_root_info *info)
390 {
391         int i;
392         struct resource *res;
393
394         for (i = 0; i < info->res_num; i++) {
395                 res = &info->res[i];
396
397                 if (!res->parent)
398                         continue;
399
400                 if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
401                         continue;
402
403                 release_resource(res);
404         }
405
406         free_pci_root_info_res(info);
407
408         teardown_mcfg_map(info);
409
410         kfree(info);
411 }
412
413 static void release_pci_root_info(struct pci_host_bridge *bridge)
414 {
415         struct pci_root_info *info = bridge->release_data;
416
417         __release_pci_root_info(info);
418 }
419
420 static void
421 probe_pci_root_info(struct pci_root_info *info, struct acpi_device *device,
422                     int busnum, int domain)
423 {
424         size_t size;
425
426         sprintf(info->name, "PCI Bus %04x:%02x", domain, busnum);
427         info->bridge = device;
428
429         info->res_num = 0;
430         acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
431                                 info);
432         if (!info->res_num)
433                 return;
434
435         size = sizeof(*info->res) * info->res_num;
436         info->res_num = 0;
437         info->res = kmalloc(size, GFP_KERNEL);
438         if (!info->res)
439                 return;
440
441         acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
442                                 info);
443 }
444
445 struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_pci_root *root)
446 {
447         struct acpi_device *device = root->device;
448         struct pci_root_info *info = NULL;
449         int domain = root->segment;
450         int busnum = root->secondary.start;
451         LIST_HEAD(resources);
452         struct pci_bus *bus = NULL;
453         struct pci_sysdata *sd;
454         int node;
455 #ifdef CONFIG_ACPI_NUMA
456         int pxm;
457 #endif
458
459         if (domain && !pci_domains_supported) {
460                 printk(KERN_WARNING "pci_bus %04x:%02x: "
461                        "ignored (multiple domains not supported)\n",
462                        domain, busnum);
463                 return NULL;
464         }
465
466         node = -1;
467 #ifdef CONFIG_ACPI_NUMA
468         pxm = acpi_get_pxm(device->handle);
469         if (pxm >= 0)
470                 node = pxm_to_node(pxm);
471         if (node != -1)
472                 set_mp_bus_to_node(busnum, node);
473         else
474 #endif
475                 node = get_mp_bus_to_node(busnum);
476
477         if (node != -1 && !node_online(node))
478                 node = -1;
479
480         info = kzalloc(sizeof(*info), GFP_KERNEL);
481         if (!info) {
482                 printk(KERN_WARNING "pci_bus %04x:%02x: "
483                        "ignored (out of memory)\n", domain, busnum);
484                 return NULL;
485         }
486
487         sd = &info->sd;
488         sd->domain = domain;
489         sd->node = node;
490         /*
491          * Maybe the desired pci bus has been already scanned. In such case
492          * it is unnecessary to scan the pci bus with the given domain,busnum.
493          */
494         bus = pci_find_bus(domain, busnum);
495         if (bus) {
496                 /*
497                  * If the desired bus exits, the content of bus->sysdata will
498                  * be replaced by sd.
499                  */
500                 memcpy(bus->sysdata, sd, sizeof(*sd));
501                 kfree(info);
502         } else {
503                 probe_pci_root_info(info, device, busnum, domain);
504
505                 /* insert busn res at first */
506                 pci_add_resource(&resources,  &root->secondary);
507                 /*
508                  * _CRS with no apertures is normal, so only fall back to
509                  * defaults or native bridge info if we're ignoring _CRS.
510                  */
511                 if (pci_use_crs)
512                         add_resources(info, &resources);
513                 else {
514                         free_pci_root_info_res(info);
515                         x86_pci_root_bus_resources(busnum, &resources);
516                 }
517
518                 if (!setup_mcfg_map(info, domain, (u8)root->secondary.start,
519                                     (u8)root->secondary.end, root->mcfg_addr))
520                         bus = pci_create_root_bus(NULL, busnum, &pci_root_ops,
521                                                   sd, &resources);
522
523                 if (bus) {
524                         pci_scan_child_bus(bus);
525                         pci_set_host_bridge_release(
526                                 to_pci_host_bridge(bus->bridge),
527                                 release_pci_root_info, info);
528                 } else {
529                         pci_free_resource_list(&resources);
530                         __release_pci_root_info(info);
531                 }
532         }
533
534         /* After the PCI-E bus has been walked and all devices discovered,
535          * configure any settings of the fabric that might be necessary.
536          */
537         if (bus) {
538                 struct pci_bus *child;
539                 list_for_each_entry(child, &bus->children, node) {
540                         struct pci_dev *self = child->self;
541                         if (!self)
542                                 continue;
543
544                         pcie_bus_configure_settings(child, self->pcie_mpss);
545                 }
546         }
547
548         if (bus && node != -1) {
549 #ifdef CONFIG_ACPI_NUMA
550                 if (pxm >= 0)
551                         dev_printk(KERN_DEBUG, &bus->dev,
552                                    "on NUMA node %d (pxm %d)\n", node, pxm);
553 #else
554                 dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
555 #endif
556         }
557
558         return bus;
559 }
560
561 int __init pci_acpi_init(void)
562 {
563         struct pci_dev *dev = NULL;
564
565         if (acpi_noirq)
566                 return -ENODEV;
567
568         printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
569         acpi_irq_penalty_init();
570         pcibios_enable_irq = acpi_pci_irq_enable;
571         pcibios_disable_irq = acpi_pci_irq_disable;
572         x86_init.pci.init_irq = x86_init_noop;
573
574         if (pci_routeirq) {
575                 /*
576                  * PCI IRQ routing is set up by pci_enable_device(), but we
577                  * also do it here in case there are still broken drivers that
578                  * don't use pci_enable_device().
579                  */
580                 printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
581                 for_each_pci_dev(dev)
582                         acpi_pci_irq_enable(dev);
583         }
584
585         return 0;
586 }