acpi.c 12 KB

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