acpi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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[16];
  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_res(struct pci_root_info *info)
  283. {
  284. kfree(info->res);
  285. info->res = NULL;
  286. info->res_num = 0;
  287. }
  288. static void __release_pci_root_info(struct pci_root_info *info)
  289. {
  290. int i;
  291. struct resource *res;
  292. for (i = 0; i < info->res_num; i++) {
  293. res = &info->res[i];
  294. if (!res->parent)
  295. continue;
  296. if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
  297. continue;
  298. release_resource(res);
  299. }
  300. free_pci_root_info_res(info);
  301. kfree(info);
  302. }
  303. static void release_pci_root_info(struct pci_host_bridge *bridge)
  304. {
  305. struct pci_root_info *info = bridge->release_data;
  306. __release_pci_root_info(info);
  307. }
  308. static void
  309. probe_pci_root_info(struct pci_root_info *info, struct acpi_device *device,
  310. int busnum, int domain)
  311. {
  312. size_t size;
  313. info->bridge = device;
  314. info->res_num = 0;
  315. acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
  316. info);
  317. if (!info->res_num)
  318. return;
  319. size = sizeof(*info->res) * info->res_num;
  320. info->res_num = 0;
  321. info->res = kmalloc(size, GFP_KERNEL);
  322. if (!info->res)
  323. return;
  324. sprintf(info->name, "PCI Bus %04x:%02x", domain, busnum);
  325. acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
  326. info);
  327. }
  328. struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_pci_root *root)
  329. {
  330. struct acpi_device *device = root->device;
  331. struct pci_root_info *info = NULL;
  332. int domain = root->segment;
  333. int busnum = root->secondary.start;
  334. LIST_HEAD(resources);
  335. struct pci_bus *bus;
  336. struct pci_sysdata *sd;
  337. int node;
  338. #ifdef CONFIG_ACPI_NUMA
  339. int pxm;
  340. #endif
  341. if (domain && !pci_domains_supported) {
  342. printk(KERN_WARNING "pci_bus %04x:%02x: "
  343. "ignored (multiple domains not supported)\n",
  344. domain, busnum);
  345. return NULL;
  346. }
  347. node = -1;
  348. #ifdef CONFIG_ACPI_NUMA
  349. pxm = acpi_get_pxm(device->handle);
  350. if (pxm >= 0)
  351. node = pxm_to_node(pxm);
  352. if (node != -1)
  353. set_mp_bus_to_node(busnum, node);
  354. else
  355. #endif
  356. node = get_mp_bus_to_node(busnum);
  357. if (node != -1 && !node_online(node))
  358. node = -1;
  359. /* Allocate per-root-bus (not per bus) arch-specific data.
  360. * TODO: leak; this memory is never freed.
  361. * It's arguable whether it's worth the trouble to care.
  362. */
  363. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  364. if (!sd) {
  365. printk(KERN_WARNING "pci_bus %04x:%02x: "
  366. "ignored (out of memory)\n", domain, busnum);
  367. return NULL;
  368. }
  369. sd->domain = domain;
  370. sd->node = node;
  371. info = kzalloc(sizeof(*info), GFP_KERNEL);
  372. if (!info) {
  373. kfree(sd);
  374. printk(KERN_WARNING "pci_bus %04x:%02x: "
  375. "ignored (out of memory)\n", domain, busnum);
  376. return NULL;
  377. }
  378. /*
  379. * Maybe the desired pci bus has been already scanned. In such case
  380. * it is unnecessary to scan the pci bus with the given domain,busnum.
  381. */
  382. bus = pci_find_bus(domain, busnum);
  383. if (bus) {
  384. /*
  385. * If the desired bus exits, the content of bus->sysdata will
  386. * be replaced by sd.
  387. */
  388. memcpy(bus->sysdata, sd, sizeof(*sd));
  389. kfree(info);
  390. kfree(sd);
  391. } else {
  392. probe_pci_root_info(info, device, busnum, domain);
  393. /*
  394. * _CRS with no apertures is normal, so only fall back to
  395. * defaults or native bridge info if we're ignoring _CRS.
  396. */
  397. if (pci_use_crs)
  398. add_resources(info, &resources);
  399. else {
  400. free_pci_root_info_res(info);
  401. x86_pci_root_bus_resources(busnum, &resources);
  402. }
  403. bus = pci_create_root_bus(NULL, busnum, &pci_root_ops, sd,
  404. &resources);
  405. if (bus) {
  406. bus->subordinate = pci_scan_child_bus(bus);
  407. pci_set_host_bridge_release(
  408. to_pci_host_bridge(bus->bridge),
  409. release_pci_root_info, info);
  410. } else {
  411. pci_free_resource_list(&resources);
  412. __release_pci_root_info(info);
  413. }
  414. }
  415. /* After the PCI-E bus has been walked and all devices discovered,
  416. * configure any settings of the fabric that might be necessary.
  417. */
  418. if (bus) {
  419. struct pci_bus *child;
  420. list_for_each_entry(child, &bus->children, node) {
  421. struct pci_dev *self = child->self;
  422. if (!self)
  423. continue;
  424. pcie_bus_configure_settings(child, self->pcie_mpss);
  425. }
  426. }
  427. if (!bus)
  428. kfree(sd);
  429. if (bus && node != -1) {
  430. #ifdef CONFIG_ACPI_NUMA
  431. if (pxm >= 0)
  432. dev_printk(KERN_DEBUG, &bus->dev,
  433. "on NUMA node %d (pxm %d)\n", node, pxm);
  434. #else
  435. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  436. #endif
  437. }
  438. return bus;
  439. }
  440. int __init pci_acpi_init(void)
  441. {
  442. struct pci_dev *dev = NULL;
  443. if (acpi_noirq)
  444. return -ENODEV;
  445. printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
  446. acpi_irq_penalty_init();
  447. pcibios_enable_irq = acpi_pci_irq_enable;
  448. pcibios_disable_irq = acpi_pci_irq_disable;
  449. x86_init.pci.init_irq = x86_init_noop;
  450. if (pci_routeirq) {
  451. /*
  452. * PCI IRQ routing is set up by pci_enable_device(), but we
  453. * also do it here in case there are still broken drivers that
  454. * don't use pci_enable_device().
  455. */
  456. printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
  457. for_each_pci_dev(dev)
  458. acpi_pci_irq_enable(dev);
  459. }
  460. return 0;
  461. }