acpi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. struct pci_root_info {
  10. struct acpi_device *bridge;
  11. char *name;
  12. unsigned int res_num;
  13. struct resource *res;
  14. struct list_head *resources;
  15. int busnum;
  16. };
  17. static bool pci_use_crs = true;
  18. static int __init set_use_crs(const struct dmi_system_id *id)
  19. {
  20. pci_use_crs = true;
  21. return 0;
  22. }
  23. static int __init set_nouse_crs(const struct dmi_system_id *id)
  24. {
  25. pci_use_crs = false;
  26. return 0;
  27. }
  28. static const struct dmi_system_id pci_use_crs_table[] __initconst = {
  29. /* http://bugzilla.kernel.org/show_bug.cgi?id=14183 */
  30. {
  31. .callback = set_use_crs,
  32. .ident = "IBM System x3800",
  33. .matches = {
  34. DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
  35. DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
  36. },
  37. },
  38. /* https://bugzilla.kernel.org/show_bug.cgi?id=16007 */
  39. /* 2006 AMD HT/VIA system with two host bridges */
  40. {
  41. .callback = set_use_crs,
  42. .ident = "ASRock ALiveSATA2-GLAN",
  43. .matches = {
  44. DMI_MATCH(DMI_PRODUCT_NAME, "ALiveSATA2-GLAN"),
  45. },
  46. },
  47. /* https://bugzilla.kernel.org/show_bug.cgi?id=30552 */
  48. /* 2006 AMD HT/VIA system with two host bridges */
  49. {
  50. .callback = set_use_crs,
  51. .ident = "ASUS M2V-MX SE",
  52. .matches = {
  53. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  54. DMI_MATCH(DMI_BOARD_NAME, "M2V-MX SE"),
  55. DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
  56. },
  57. },
  58. /* https://bugzilla.kernel.org/show_bug.cgi?id=42619 */
  59. {
  60. .callback = set_use_crs,
  61. .ident = "MSI MS-7253",
  62. .matches = {
  63. DMI_MATCH(DMI_BOARD_VENDOR, "MICRO-STAR INTERNATIONAL CO., LTD"),
  64. DMI_MATCH(DMI_BOARD_NAME, "MS-7253"),
  65. DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies, LTD"),
  66. DMI_MATCH(DMI_BIOS_VERSION, "V1.6"),
  67. },
  68. },
  69. /* Now for the blacklist.. */
  70. /* https://bugzilla.redhat.com/show_bug.cgi?id=769657 */
  71. {
  72. .callback = set_nouse_crs,
  73. .ident = "Dell Studio 1557",
  74. .matches = {
  75. DMI_MATCH(DMI_BOARD_VENDOR, "Dell Inc."),
  76. DMI_MATCH(DMI_PRODUCT_NAME, "Studio 1557"),
  77. DMI_MATCH(DMI_BIOS_VERSION, "A09"),
  78. },
  79. },
  80. /* https://bugzilla.redhat.com/show_bug.cgi?id=769657 */
  81. {
  82. .callback = set_nouse_crs,
  83. .ident = "Thinkpad SL510",
  84. .matches = {
  85. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  86. DMI_MATCH(DMI_BOARD_NAME, "2847DFG"),
  87. DMI_MATCH(DMI_BIOS_VERSION, "6JET85WW (1.43 )"),
  88. },
  89. },
  90. {}
  91. };
  92. void __init pci_acpi_crs_quirks(void)
  93. {
  94. int year;
  95. if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) && year < 2008)
  96. pci_use_crs = false;
  97. dmi_check_system(pci_use_crs_table);
  98. /*
  99. * If the user specifies "pci=use_crs" or "pci=nocrs" explicitly, that
  100. * takes precedence over anything we figured out above.
  101. */
  102. if (pci_probe & PCI_ROOT_NO_CRS)
  103. pci_use_crs = false;
  104. else if (pci_probe & PCI_USE__CRS)
  105. pci_use_crs = true;
  106. printk(KERN_INFO "PCI: %s host bridge windows from ACPI; "
  107. "if necessary, use \"pci=%s\" and report a bug\n",
  108. pci_use_crs ? "Using" : "Ignoring",
  109. pci_use_crs ? "nocrs" : "use_crs");
  110. }
  111. static acpi_status
  112. resource_to_addr(struct acpi_resource *resource,
  113. struct acpi_resource_address64 *addr)
  114. {
  115. acpi_status status;
  116. struct acpi_resource_memory24 *memory24;
  117. struct acpi_resource_memory32 *memory32;
  118. struct acpi_resource_fixed_memory32 *fixed_memory32;
  119. memset(addr, 0, sizeof(*addr));
  120. switch (resource->type) {
  121. case ACPI_RESOURCE_TYPE_MEMORY24:
  122. memory24 = &resource->data.memory24;
  123. addr->resource_type = ACPI_MEMORY_RANGE;
  124. addr->minimum = memory24->minimum;
  125. addr->address_length = memory24->address_length;
  126. addr->maximum = addr->minimum + addr->address_length - 1;
  127. return AE_OK;
  128. case ACPI_RESOURCE_TYPE_MEMORY32:
  129. memory32 = &resource->data.memory32;
  130. addr->resource_type = ACPI_MEMORY_RANGE;
  131. addr->minimum = memory32->minimum;
  132. addr->address_length = memory32->address_length;
  133. addr->maximum = addr->minimum + addr->address_length - 1;
  134. return AE_OK;
  135. case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
  136. fixed_memory32 = &resource->data.fixed_memory32;
  137. addr->resource_type = ACPI_MEMORY_RANGE;
  138. addr->minimum = fixed_memory32->address;
  139. addr->address_length = fixed_memory32->address_length;
  140. addr->maximum = addr->minimum + addr->address_length - 1;
  141. return AE_OK;
  142. case ACPI_RESOURCE_TYPE_ADDRESS16:
  143. case ACPI_RESOURCE_TYPE_ADDRESS32:
  144. case ACPI_RESOURCE_TYPE_ADDRESS64:
  145. status = acpi_resource_to_address64(resource, addr);
  146. if (ACPI_SUCCESS(status) &&
  147. (addr->resource_type == ACPI_MEMORY_RANGE ||
  148. addr->resource_type == ACPI_IO_RANGE) &&
  149. addr->address_length > 0) {
  150. return AE_OK;
  151. }
  152. break;
  153. }
  154. return AE_ERROR;
  155. }
  156. static acpi_status
  157. count_resource(struct acpi_resource *acpi_res, void *data)
  158. {
  159. struct pci_root_info *info = data;
  160. struct acpi_resource_address64 addr;
  161. acpi_status status;
  162. status = resource_to_addr(acpi_res, &addr);
  163. if (ACPI_SUCCESS(status))
  164. info->res_num++;
  165. return AE_OK;
  166. }
  167. static acpi_status
  168. setup_resource(struct acpi_resource *acpi_res, void *data)
  169. {
  170. struct pci_root_info *info = data;
  171. struct resource *res;
  172. struct acpi_resource_address64 addr;
  173. acpi_status status;
  174. unsigned long flags;
  175. u64 start, orig_end, end;
  176. status = resource_to_addr(acpi_res, &addr);
  177. if (!ACPI_SUCCESS(status))
  178. return AE_OK;
  179. if (addr.resource_type == ACPI_MEMORY_RANGE) {
  180. flags = IORESOURCE_MEM;
  181. if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
  182. flags |= IORESOURCE_PREFETCH;
  183. } else if (addr.resource_type == ACPI_IO_RANGE) {
  184. flags = IORESOURCE_IO;
  185. } else
  186. return AE_OK;
  187. start = addr.minimum + addr.translation_offset;
  188. orig_end = end = addr.maximum + addr.translation_offset;
  189. /* Exclude non-addressable range or non-addressable portion of range */
  190. end = min(end, (u64)iomem_resource.end);
  191. if (end <= start) {
  192. dev_info(&info->bridge->dev,
  193. "host bridge window [%#llx-%#llx] "
  194. "(ignored, not CPU addressable)\n", start, orig_end);
  195. return AE_OK;
  196. } else if (orig_end != end) {
  197. dev_info(&info->bridge->dev,
  198. "host bridge window [%#llx-%#llx] "
  199. "([%#llx-%#llx] ignored, not CPU addressable)\n",
  200. start, orig_end, end + 1, orig_end);
  201. }
  202. res = &info->res[info->res_num];
  203. res->name = info->name;
  204. res->flags = flags;
  205. res->start = start;
  206. res->end = end;
  207. res->child = NULL;
  208. if (!pci_use_crs) {
  209. dev_printk(KERN_DEBUG, &info->bridge->dev,
  210. "host bridge window %pR (ignored)\n", res);
  211. return AE_OK;
  212. }
  213. info->res_num++;
  214. if (addr.translation_offset)
  215. dev_info(&info->bridge->dev, "host bridge window %pR "
  216. "(PCI address [%#llx-%#llx])\n",
  217. res, res->start - addr.translation_offset,
  218. res->end - addr.translation_offset);
  219. else
  220. dev_info(&info->bridge->dev, "host bridge window %pR\n", res);
  221. return AE_OK;
  222. }
  223. static bool resource_contains(struct resource *res, resource_size_t point)
  224. {
  225. if (res->start <= point && point <= res->end)
  226. return true;
  227. return false;
  228. }
  229. static void coalesce_windows(struct pci_root_info *info, unsigned long type)
  230. {
  231. int i, j;
  232. struct resource *res1, *res2;
  233. for (i = 0; i < info->res_num; i++) {
  234. res1 = &info->res[i];
  235. if (!(res1->flags & type))
  236. continue;
  237. for (j = i + 1; j < info->res_num; j++) {
  238. res2 = &info->res[j];
  239. if (!(res2->flags & type))
  240. continue;
  241. /*
  242. * I don't like throwing away windows because then
  243. * our resources no longer match the ACPI _CRS, but
  244. * the kernel resource tree doesn't allow overlaps.
  245. */
  246. if (resource_contains(res1, res2->start) ||
  247. resource_contains(res1, res2->end) ||
  248. resource_contains(res2, res1->start) ||
  249. resource_contains(res2, res1->end)) {
  250. res1->start = min(res1->start, res2->start);
  251. res1->end = max(res1->end, res2->end);
  252. dev_info(&info->bridge->dev,
  253. "host bridge window expanded to %pR; %pR ignored\n",
  254. res1, res2);
  255. res2->flags = 0;
  256. }
  257. }
  258. }
  259. }
  260. static void add_resources(struct pci_root_info *info)
  261. {
  262. int i;
  263. struct resource *res, *root, *conflict;
  264. coalesce_windows(info, IORESOURCE_MEM);
  265. coalesce_windows(info, IORESOURCE_IO);
  266. for (i = 0; i < info->res_num; i++) {
  267. res = &info->res[i];
  268. if (res->flags & IORESOURCE_MEM)
  269. root = &iomem_resource;
  270. else if (res->flags & IORESOURCE_IO)
  271. root = &ioport_resource;
  272. else
  273. continue;
  274. conflict = insert_resource_conflict(root, res);
  275. if (conflict)
  276. dev_info(&info->bridge->dev,
  277. "ignoring host bridge window %pR (conflicts with %s %pR)\n",
  278. res, conflict->name, conflict);
  279. else
  280. pci_add_resource(info->resources, res);
  281. }
  282. }
  283. static void
  284. get_current_resources(struct acpi_device *device, int busnum,
  285. int domain, struct list_head *resources)
  286. {
  287. struct pci_root_info info;
  288. size_t size;
  289. info.bridge = device;
  290. info.res_num = 0;
  291. info.resources = resources;
  292. acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
  293. &info);
  294. if (!info.res_num)
  295. return;
  296. size = sizeof(*info.res) * info.res_num;
  297. info.res = kmalloc(size, GFP_KERNEL);
  298. if (!info.res)
  299. return;
  300. info.name = kasprintf(GFP_KERNEL, "PCI Bus %04x:%02x", domain, busnum);
  301. if (!info.name)
  302. goto name_alloc_fail;
  303. info.res_num = 0;
  304. acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
  305. &info);
  306. if (pci_use_crs) {
  307. add_resources(&info);
  308. return;
  309. }
  310. kfree(info.name);
  311. name_alloc_fail:
  312. kfree(info.res);
  313. }
  314. struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_pci_root *root)
  315. {
  316. struct acpi_device *device = root->device;
  317. int domain = root->segment;
  318. int busnum = root->secondary.start;
  319. LIST_HEAD(resources);
  320. struct pci_bus *bus;
  321. struct pci_sysdata *sd;
  322. int node;
  323. #ifdef CONFIG_ACPI_NUMA
  324. int pxm;
  325. #endif
  326. if (domain && !pci_domains_supported) {
  327. printk(KERN_WARNING "pci_bus %04x:%02x: "
  328. "ignored (multiple domains not supported)\n",
  329. domain, busnum);
  330. return NULL;
  331. }
  332. node = -1;
  333. #ifdef CONFIG_ACPI_NUMA
  334. pxm = acpi_get_pxm(device->handle);
  335. if (pxm >= 0)
  336. node = pxm_to_node(pxm);
  337. if (node != -1)
  338. set_mp_bus_to_node(busnum, node);
  339. else
  340. #endif
  341. node = get_mp_bus_to_node(busnum);
  342. if (node != -1 && !node_online(node))
  343. node = -1;
  344. /* Allocate per-root-bus (not per bus) arch-specific data.
  345. * TODO: leak; this memory is never freed.
  346. * It's arguable whether it's worth the trouble to care.
  347. */
  348. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  349. if (!sd) {
  350. printk(KERN_WARNING "pci_bus %04x:%02x: "
  351. "ignored (out of memory)\n", domain, busnum);
  352. return NULL;
  353. }
  354. sd->domain = domain;
  355. sd->node = node;
  356. /*
  357. * Maybe the desired pci bus has been already scanned. In such case
  358. * it is unnecessary to scan the pci bus with the given domain,busnum.
  359. */
  360. bus = pci_find_bus(domain, busnum);
  361. if (bus) {
  362. /*
  363. * If the desired bus exits, the content of bus->sysdata will
  364. * be replaced by sd.
  365. */
  366. memcpy(bus->sysdata, sd, sizeof(*sd));
  367. kfree(sd);
  368. } else {
  369. get_current_resources(device, busnum, domain, &resources);
  370. if (list_empty(&resources))
  371. x86_pci_root_bus_resources(busnum, &resources);
  372. bus = pci_create_root_bus(NULL, busnum, &pci_root_ops, sd,
  373. &resources);
  374. if (bus)
  375. bus->subordinate = pci_scan_child_bus(bus);
  376. else
  377. pci_free_resource_list(&resources);
  378. }
  379. /* After the PCI-E bus has been walked and all devices discovered,
  380. * configure any settings of the fabric that might be necessary.
  381. */
  382. if (bus) {
  383. struct pci_bus *child;
  384. list_for_each_entry(child, &bus->children, node) {
  385. struct pci_dev *self = child->self;
  386. if (!self)
  387. continue;
  388. pcie_bus_configure_settings(child, self->pcie_mpss);
  389. }
  390. }
  391. if (!bus)
  392. kfree(sd);
  393. if (bus && node != -1) {
  394. #ifdef CONFIG_ACPI_NUMA
  395. if (pxm >= 0)
  396. dev_printk(KERN_DEBUG, &bus->dev,
  397. "on NUMA node %d (pxm %d)\n", node, pxm);
  398. #else
  399. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  400. #endif
  401. }
  402. return bus;
  403. }
  404. int __init pci_acpi_init(void)
  405. {
  406. struct pci_dev *dev = NULL;
  407. if (acpi_noirq)
  408. return -ENODEV;
  409. printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
  410. acpi_irq_penalty_init();
  411. pcibios_enable_irq = acpi_pci_irq_enable;
  412. pcibios_disable_irq = acpi_pci_irq_disable;
  413. x86_init.pci.init_irq = x86_init_noop;
  414. if (pci_routeirq) {
  415. /*
  416. * PCI IRQ routing is set up by pci_enable_device(), but we
  417. * also do it here in case there are still broken drivers that
  418. * don't use pci_enable_device().
  419. */
  420. printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
  421. for_each_pci_dev(dev)
  422. acpi_pci_irq_enable(dev);
  423. }
  424. return 0;
  425. }