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