acpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/pci.h>
  3. #include <linux/acpi.h>
  4. #include <linux/init.h>
  5. #include <linux/irq.h>
  6. #include <linux/dmi.h>
  7. #include <linux/slab.h>
  8. #include <linux/pci-acpi.h>
  9. #include <asm/numa.h>
  10. #include <asm/pci_x86.h>
  11. struct pci_root_info {
  12. struct acpi_pci_root_info common;
  13. struct pci_sysdata sd;
  14. #ifdef CONFIG_PCI_MMCONFIG
  15. bool mcfg_added;
  16. u8 start_bus;
  17. u8 end_bus;
  18. #endif
  19. };
  20. static bool pci_use_crs = true;
  21. static bool pci_ignore_seg = false;
  22. static int __init set_use_crs(const struct dmi_system_id *id)
  23. {
  24. pci_use_crs = true;
  25. return 0;
  26. }
  27. static int __init set_nouse_crs(const struct dmi_system_id *id)
  28. {
  29. pci_use_crs = false;
  30. return 0;
  31. }
  32. static int __init set_ignore_seg(const struct dmi_system_id *id)
  33. {
  34. printk(KERN_INFO "PCI: %s detected: ignoring ACPI _SEG\n", id->ident);
  35. pci_ignore_seg = true;
  36. return 0;
  37. }
  38. static const struct dmi_system_id pci_crs_quirks[] __initconst = {
  39. /* http://bugzilla.kernel.org/show_bug.cgi?id=14183 */
  40. {
  41. .callback = set_use_crs,
  42. .ident = "IBM System x3800",
  43. .matches = {
  44. DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
  45. DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
  46. },
  47. },
  48. /* https://bugzilla.kernel.org/show_bug.cgi?id=16007 */
  49. /* 2006 AMD HT/VIA system with two host bridges */
  50. {
  51. .callback = set_use_crs,
  52. .ident = "ASRock ALiveSATA2-GLAN",
  53. .matches = {
  54. DMI_MATCH(DMI_PRODUCT_NAME, "ALiveSATA2-GLAN"),
  55. },
  56. },
  57. /* https://bugzilla.kernel.org/show_bug.cgi?id=30552 */
  58. /* 2006 AMD HT/VIA system with two host bridges */
  59. {
  60. .callback = set_use_crs,
  61. .ident = "ASUS M2V-MX SE",
  62. .matches = {
  63. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  64. DMI_MATCH(DMI_BOARD_NAME, "M2V-MX SE"),
  65. DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
  66. },
  67. },
  68. /* https://bugzilla.kernel.org/show_bug.cgi?id=42619 */
  69. {
  70. .callback = set_use_crs,
  71. .ident = "MSI MS-7253",
  72. .matches = {
  73. DMI_MATCH(DMI_BOARD_VENDOR, "MICRO-STAR INTERNATIONAL CO., LTD"),
  74. DMI_MATCH(DMI_BOARD_NAME, "MS-7253"),
  75. DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies, LTD"),
  76. },
  77. },
  78. /* https://bugs.launchpad.net/ubuntu/+source/alsa-driver/+bug/931368 */
  79. /* https://bugs.launchpad.net/ubuntu/+source/alsa-driver/+bug/1033299 */
  80. {
  81. .callback = set_use_crs,
  82. .ident = "Foxconn K8M890-8237A",
  83. .matches = {
  84. DMI_MATCH(DMI_BOARD_VENDOR, "Foxconn"),
  85. DMI_MATCH(DMI_BOARD_NAME, "K8M890-8237A"),
  86. DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies, LTD"),
  87. },
  88. },
  89. /* Now for the blacklist.. */
  90. /* https://bugzilla.redhat.com/show_bug.cgi?id=769657 */
  91. {
  92. .callback = set_nouse_crs,
  93. .ident = "Dell Studio 1557",
  94. .matches = {
  95. DMI_MATCH(DMI_BOARD_VENDOR, "Dell Inc."),
  96. DMI_MATCH(DMI_PRODUCT_NAME, "Studio 1557"),
  97. DMI_MATCH(DMI_BIOS_VERSION, "A09"),
  98. },
  99. },
  100. /* https://bugzilla.redhat.com/show_bug.cgi?id=769657 */
  101. {
  102. .callback = set_nouse_crs,
  103. .ident = "Thinkpad SL510",
  104. .matches = {
  105. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  106. DMI_MATCH(DMI_BOARD_NAME, "2847DFG"),
  107. DMI_MATCH(DMI_BIOS_VERSION, "6JET85WW (1.43 )"),
  108. },
  109. },
  110. /* https://bugzilla.kernel.org/show_bug.cgi?id=42606 */
  111. {
  112. .callback = set_nouse_crs,
  113. .ident = "Supermicro X8DTH",
  114. .matches = {
  115. DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
  116. DMI_MATCH(DMI_PRODUCT_NAME, "X8DTH-i/6/iF/6F"),
  117. DMI_MATCH(DMI_BIOS_VERSION, "2.0a"),
  118. },
  119. },
  120. /* https://bugzilla.kernel.org/show_bug.cgi?id=15362 */
  121. {
  122. .callback = set_ignore_seg,
  123. .ident = "HP xw9300",
  124. .matches = {
  125. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  126. DMI_MATCH(DMI_PRODUCT_NAME, "HP xw9300 Workstation"),
  127. },
  128. },
  129. {}
  130. };
  131. void __init pci_acpi_crs_quirks(void)
  132. {
  133. int year = dmi_get_bios_year();
  134. if (year >= 0 && year < 2008 && iomem_resource.end <= 0xffffffff)
  135. pci_use_crs = false;
  136. dmi_check_system(pci_crs_quirks);
  137. /*
  138. * If the user specifies "pci=use_crs" or "pci=nocrs" explicitly, that
  139. * takes precedence over anything we figured out above.
  140. */
  141. if (pci_probe & PCI_ROOT_NO_CRS)
  142. pci_use_crs = false;
  143. else if (pci_probe & PCI_USE__CRS)
  144. pci_use_crs = true;
  145. printk(KERN_INFO "PCI: %s host bridge windows from ACPI; "
  146. "if necessary, use \"pci=%s\" and report a bug\n",
  147. pci_use_crs ? "Using" : "Ignoring",
  148. pci_use_crs ? "nocrs" : "use_crs");
  149. }
  150. #ifdef CONFIG_PCI_MMCONFIG
  151. static int check_segment(u16 seg, struct device *dev, char *estr)
  152. {
  153. if (seg) {
  154. dev_err(dev,
  155. "%s can't access PCI configuration "
  156. "space under this host bridge.\n",
  157. estr);
  158. return -EIO;
  159. }
  160. /*
  161. * Failure in adding MMCFG information is not fatal,
  162. * just can't access extended configuration space of
  163. * devices under this host bridge.
  164. */
  165. dev_warn(dev,
  166. "%s can't access extended PCI configuration "
  167. "space under this bridge.\n",
  168. estr);
  169. return 0;
  170. }
  171. static int setup_mcfg_map(struct acpi_pci_root_info *ci)
  172. {
  173. int result, seg;
  174. struct pci_root_info *info;
  175. struct acpi_pci_root *root = ci->root;
  176. struct device *dev = &ci->bridge->dev;
  177. info = container_of(ci, struct pci_root_info, common);
  178. info->start_bus = (u8)root->secondary.start;
  179. info->end_bus = (u8)root->secondary.end;
  180. info->mcfg_added = false;
  181. seg = info->sd.domain;
  182. /* return success if MMCFG is not in use */
  183. if (raw_pci_ext_ops && raw_pci_ext_ops != &pci_mmcfg)
  184. return 0;
  185. if (!(pci_probe & PCI_PROBE_MMCONF))
  186. return check_segment(seg, dev, "MMCONFIG is disabled,");
  187. result = pci_mmconfig_insert(dev, seg, info->start_bus, info->end_bus,
  188. root->mcfg_addr);
  189. if (result == 0) {
  190. /* enable MMCFG if it hasn't been enabled yet */
  191. if (raw_pci_ext_ops == NULL)
  192. raw_pci_ext_ops = &pci_mmcfg;
  193. info->mcfg_added = true;
  194. } else if (result != -EEXIST)
  195. return check_segment(seg, dev,
  196. "fail to add MMCONFIG information,");
  197. return 0;
  198. }
  199. static void teardown_mcfg_map(struct acpi_pci_root_info *ci)
  200. {
  201. struct pci_root_info *info;
  202. info = container_of(ci, struct pci_root_info, common);
  203. if (info->mcfg_added) {
  204. pci_mmconfig_delete(info->sd.domain,
  205. info->start_bus, info->end_bus);
  206. info->mcfg_added = false;
  207. }
  208. }
  209. #else
  210. static int setup_mcfg_map(struct acpi_pci_root_info *ci)
  211. {
  212. return 0;
  213. }
  214. static void teardown_mcfg_map(struct acpi_pci_root_info *ci)
  215. {
  216. }
  217. #endif
  218. static int pci_acpi_root_get_node(struct acpi_pci_root *root)
  219. {
  220. int busnum = root->secondary.start;
  221. struct acpi_device *device = root->device;
  222. int node = acpi_get_node(device->handle);
  223. if (node == NUMA_NO_NODE) {
  224. node = x86_pci_root_bus_node(busnum);
  225. if (node != 0 && node != NUMA_NO_NODE)
  226. dev_info(&device->dev, FW_BUG "no _PXM; falling back to node %d from hardware (may be inconsistent with ACPI node numbers)\n",
  227. node);
  228. }
  229. if (node != NUMA_NO_NODE && !node_online(node))
  230. node = NUMA_NO_NODE;
  231. return node;
  232. }
  233. static int pci_acpi_root_init_info(struct acpi_pci_root_info *ci)
  234. {
  235. return setup_mcfg_map(ci);
  236. }
  237. static void pci_acpi_root_release_info(struct acpi_pci_root_info *ci)
  238. {
  239. teardown_mcfg_map(ci);
  240. kfree(container_of(ci, struct pci_root_info, common));
  241. }
  242. /*
  243. * An IO port or MMIO resource assigned to a PCI host bridge may be
  244. * consumed by the host bridge itself or available to its child
  245. * bus/devices. The ACPI specification defines a bit (Producer/Consumer)
  246. * to tell whether the resource is consumed by the host bridge itself,
  247. * but firmware hasn't used that bit consistently, so we can't rely on it.
  248. *
  249. * On x86 and IA64 platforms, all IO port and MMIO resources are assumed
  250. * to be available to child bus/devices except one special case:
  251. * IO port [0xCF8-0xCFF] is consumed by the host bridge itself
  252. * to access PCI configuration space.
  253. *
  254. * So explicitly filter out PCI CFG IO ports[0xCF8-0xCFF].
  255. */
  256. static bool resource_is_pcicfg_ioport(struct resource *res)
  257. {
  258. return (res->flags & IORESOURCE_IO) &&
  259. res->start == 0xCF8 && res->end == 0xCFF;
  260. }
  261. static int pci_acpi_root_prepare_resources(struct acpi_pci_root_info *ci)
  262. {
  263. struct acpi_device *device = ci->bridge;
  264. int busnum = ci->root->secondary.start;
  265. struct resource_entry *entry, *tmp;
  266. int status;
  267. status = acpi_pci_probe_root_resources(ci);
  268. if (pci_use_crs) {
  269. resource_list_for_each_entry_safe(entry, tmp, &ci->resources)
  270. if (resource_is_pcicfg_ioport(entry->res))
  271. resource_list_destroy_entry(entry);
  272. return status;
  273. }
  274. resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
  275. dev_printk(KERN_DEBUG, &device->dev,
  276. "host bridge window %pR (ignored)\n", entry->res);
  277. resource_list_destroy_entry(entry);
  278. }
  279. x86_pci_root_bus_resources(busnum, &ci->resources);
  280. return 0;
  281. }
  282. static struct acpi_pci_root_ops acpi_pci_root_ops = {
  283. .pci_ops = &pci_root_ops,
  284. .init_info = pci_acpi_root_init_info,
  285. .release_info = pci_acpi_root_release_info,
  286. .prepare_resources = pci_acpi_root_prepare_resources,
  287. };
  288. struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
  289. {
  290. int domain = root->segment;
  291. int busnum = root->secondary.start;
  292. int node = pci_acpi_root_get_node(root);
  293. struct pci_bus *bus;
  294. if (pci_ignore_seg)
  295. root->segment = domain = 0;
  296. if (domain && !pci_domains_supported) {
  297. printk(KERN_WARNING "pci_bus %04x:%02x: "
  298. "ignored (multiple domains not supported)\n",
  299. domain, busnum);
  300. return NULL;
  301. }
  302. bus = pci_find_bus(domain, busnum);
  303. if (bus) {
  304. /*
  305. * If the desired bus has been scanned already, replace
  306. * its bus->sysdata.
  307. */
  308. struct pci_sysdata sd = {
  309. .domain = domain,
  310. .node = node,
  311. .companion = root->device
  312. };
  313. memcpy(bus->sysdata, &sd, sizeof(sd));
  314. } else {
  315. struct pci_root_info *info;
  316. info = kzalloc_node(sizeof(*info), GFP_KERNEL, node);
  317. if (!info)
  318. dev_err(&root->device->dev,
  319. "pci_bus %04x:%02x: ignored (out of memory)\n",
  320. domain, busnum);
  321. else {
  322. info->sd.domain = domain;
  323. info->sd.node = node;
  324. info->sd.companion = root->device;
  325. bus = acpi_pci_root_create(root, &acpi_pci_root_ops,
  326. &info->common, &info->sd);
  327. }
  328. }
  329. /* After the PCI-E bus has been walked and all devices discovered,
  330. * configure any settings of the fabric that might be necessary.
  331. */
  332. if (bus) {
  333. struct pci_bus *child;
  334. list_for_each_entry(child, &bus->children, node)
  335. pcie_bus_configure_settings(child);
  336. }
  337. return bus;
  338. }
  339. int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
  340. {
  341. /*
  342. * We pass NULL as parent to pci_create_root_bus(), so if it is not NULL
  343. * here, pci_create_root_bus() has been called by someone else and
  344. * sysdata is likely to be different from what we expect. Let it go in
  345. * that case.
  346. */
  347. if (!bridge->dev.parent) {
  348. struct pci_sysdata *sd = bridge->bus->sysdata;
  349. ACPI_COMPANION_SET(&bridge->dev, sd->companion);
  350. }
  351. return 0;
  352. }
  353. int __init pci_acpi_init(void)
  354. {
  355. struct pci_dev *dev = NULL;
  356. if (acpi_noirq)
  357. return -ENODEV;
  358. printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
  359. acpi_irq_penalty_init();
  360. pcibios_enable_irq = acpi_pci_irq_enable;
  361. pcibios_disable_irq = acpi_pci_irq_disable;
  362. x86_init.pci.init_irq = x86_init_noop;
  363. if (pci_routeirq) {
  364. /*
  365. * PCI IRQ routing is set up by pci_enable_device(), but we
  366. * also do it here in case there are still broken drivers that
  367. * don't use pci_enable_device().
  368. */
  369. printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
  370. for_each_pci_dev(dev)
  371. acpi_pci_irq_enable(dev);
  372. }
  373. return 0;
  374. }