acpi.c 13 KB

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