acpi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. bool pci_use_e820 = true;
  21. static bool pci_use_crs = true;
  22. static bool pci_ignore_seg;
  23. static int __init set_use_crs(const struct dmi_system_id *id)
  24. {
  25. pci_use_crs = true;
  26. return 0;
  27. }
  28. static int __init set_nouse_crs(const struct dmi_system_id *id)
  29. {
  30. pci_use_crs = false;
  31. return 0;
  32. }
  33. static int __init set_ignore_seg(const struct dmi_system_id *id)
  34. {
  35. printk(KERN_INFO "PCI: %s detected: ignoring ACPI _SEG\n", id->ident);
  36. pci_ignore_seg = true;
  37. return 0;
  38. }
  39. static int __init set_no_e820(const struct dmi_system_id *id)
  40. {
  41. printk(KERN_INFO "PCI: %s detected: not clipping E820 regions from _CRS\n",
  42. id->ident);
  43. pci_use_e820 = false;
  44. return 0;
  45. }
  46. static const struct dmi_system_id pci_crs_quirks[] __initconst = {
  47. /* http://bugzilla.kernel.org/show_bug.cgi?id=14183 */
  48. {
  49. .callback = set_use_crs,
  50. .ident = "IBM System x3800",
  51. .matches = {
  52. DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
  53. DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
  54. },
  55. },
  56. /* https://bugzilla.kernel.org/show_bug.cgi?id=16007 */
  57. /* 2006 AMD HT/VIA system with two host bridges */
  58. {
  59. .callback = set_use_crs,
  60. .ident = "ASRock ALiveSATA2-GLAN",
  61. .matches = {
  62. DMI_MATCH(DMI_PRODUCT_NAME, "ALiveSATA2-GLAN"),
  63. },
  64. },
  65. /* https://bugzilla.kernel.org/show_bug.cgi?id=30552 */
  66. /* 2006 AMD HT/VIA system with two host bridges */
  67. {
  68. .callback = set_use_crs,
  69. .ident = "ASUS M2V-MX SE",
  70. .matches = {
  71. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  72. DMI_MATCH(DMI_BOARD_NAME, "M2V-MX SE"),
  73. DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
  74. },
  75. },
  76. /* https://bugzilla.kernel.org/show_bug.cgi?id=42619 */
  77. {
  78. .callback = set_use_crs,
  79. .ident = "MSI MS-7253",
  80. .matches = {
  81. DMI_MATCH(DMI_BOARD_VENDOR, "MICRO-STAR INTERNATIONAL CO., LTD"),
  82. DMI_MATCH(DMI_BOARD_NAME, "MS-7253"),
  83. DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies, LTD"),
  84. },
  85. },
  86. /* https://bugs.launchpad.net/ubuntu/+source/alsa-driver/+bug/931368 */
  87. /* https://bugs.launchpad.net/ubuntu/+source/alsa-driver/+bug/1033299 */
  88. {
  89. .callback = set_use_crs,
  90. .ident = "Foxconn K8M890-8237A",
  91. .matches = {
  92. DMI_MATCH(DMI_BOARD_VENDOR, "Foxconn"),
  93. DMI_MATCH(DMI_BOARD_NAME, "K8M890-8237A"),
  94. DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies, LTD"),
  95. },
  96. },
  97. /* Now for the blacklist.. */
  98. /* https://bugzilla.redhat.com/show_bug.cgi?id=769657 */
  99. {
  100. .callback = set_nouse_crs,
  101. .ident = "Dell Studio 1557",
  102. .matches = {
  103. DMI_MATCH(DMI_BOARD_VENDOR, "Dell Inc."),
  104. DMI_MATCH(DMI_PRODUCT_NAME, "Studio 1557"),
  105. DMI_MATCH(DMI_BIOS_VERSION, "A09"),
  106. },
  107. },
  108. /* https://bugzilla.redhat.com/show_bug.cgi?id=769657 */
  109. {
  110. .callback = set_nouse_crs,
  111. .ident = "Thinkpad SL510",
  112. .matches = {
  113. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  114. DMI_MATCH(DMI_BOARD_NAME, "2847DFG"),
  115. DMI_MATCH(DMI_BIOS_VERSION, "6JET85WW (1.43 )"),
  116. },
  117. },
  118. /* https://bugzilla.kernel.org/show_bug.cgi?id=42606 */
  119. {
  120. .callback = set_nouse_crs,
  121. .ident = "Supermicro X8DTH",
  122. .matches = {
  123. DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
  124. DMI_MATCH(DMI_PRODUCT_NAME, "X8DTH-i/6/iF/6F"),
  125. DMI_MATCH(DMI_BIOS_VERSION, "2.0a"),
  126. },
  127. },
  128. /* https://bugzilla.kernel.org/show_bug.cgi?id=15362 */
  129. {
  130. .callback = set_ignore_seg,
  131. .ident = "HP xw9300",
  132. .matches = {
  133. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  134. DMI_MATCH(DMI_PRODUCT_NAME, "HP xw9300 Workstation"),
  135. },
  136. },
  137. /*
  138. * Many Lenovo models with "IIL" in their DMI_PRODUCT_VERSION have
  139. * an E820 reserved region that covers the entire 32-bit host
  140. * bridge memory window from _CRS. Using the E820 region to clip
  141. * _CRS means no space is available for hot-added or uninitialized
  142. * PCI devices. This typically breaks I2C controllers for touchpads
  143. * and hot-added Thunderbolt devices. See the commit log for
  144. * models known to require this quirk and related bug reports.
  145. */
  146. {
  147. .callback = set_no_e820,
  148. .ident = "Lenovo *IIL* product version",
  149. .matches = {
  150. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  151. DMI_MATCH(DMI_PRODUCT_VERSION, "IIL"),
  152. },
  153. },
  154. /*
  155. * The Acer Spin 5 (SP513-54N) has the same E820 reservation covering
  156. * the entire _CRS 32-bit window issue as the Lenovo *IIL* models.
  157. * See https://bugs.launchpad.net/bugs/1884232
  158. */
  159. {
  160. .callback = set_no_e820,
  161. .ident = "Acer Spin 5 (SP513-54N)",
  162. .matches = {
  163. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  164. DMI_MATCH(DMI_PRODUCT_NAME, "Spin SP513-54N"),
  165. },
  166. },
  167. /*
  168. * Clevo X170KM-G barebones have the same E820 reservation covering
  169. * the entire _CRS 32-bit window issue as the Lenovo *IIL* models.
  170. * See https://bugzilla.kernel.org/show_bug.cgi?id=214259
  171. */
  172. {
  173. .callback = set_no_e820,
  174. .ident = "Clevo X170KM-G Barebone",
  175. .matches = {
  176. DMI_MATCH(DMI_BOARD_NAME, "X170KM-G"),
  177. },
  178. },
  179. {}
  180. };
  181. void __init pci_acpi_crs_quirks(void)
  182. {
  183. int year = dmi_get_bios_year();
  184. if (year >= 0 && year < 2008 && iomem_resource.end <= 0xffffffff)
  185. pci_use_crs = false;
  186. /*
  187. * Some firmware includes unusable space (host bridge registers,
  188. * hidden PCI device BARs, etc) in PCI host bridge _CRS. This is a
  189. * firmware defect, and 4dc2287c1805 ("x86: avoid E820 regions when
  190. * allocating address space") has clipped out the unusable space in
  191. * the past.
  192. *
  193. * But other firmware supplies E820 reserved regions that cover
  194. * entire _CRS windows, so clipping throws away the entire window,
  195. * leaving none for hot-added or uninitialized devices. These E820
  196. * entries are probably *not* a firmware defect, so disable the
  197. * clipping by default for post-2022 machines.
  198. *
  199. * We already have quirks to disable clipping for pre-2023
  200. * machines, and we'll likely need quirks to *enable* clipping for
  201. * post-2022 machines that incorrectly include unusable space in
  202. * _CRS.
  203. */
  204. if (year >= 2023)
  205. pci_use_e820 = false;
  206. dmi_check_system(pci_crs_quirks);
  207. /*
  208. * If the user specifies "pci=use_crs" or "pci=nocrs" explicitly, that
  209. * takes precedence over anything we figured out above.
  210. */
  211. if (pci_probe & PCI_ROOT_NO_CRS)
  212. pci_use_crs = false;
  213. else if (pci_probe & PCI_USE__CRS)
  214. pci_use_crs = true;
  215. printk(KERN_INFO "PCI: %s host bridge windows from ACPI; "
  216. "if necessary, use \"pci=%s\" and report a bug\n",
  217. pci_use_crs ? "Using" : "Ignoring",
  218. pci_use_crs ? "nocrs" : "use_crs");
  219. /* "pci=use_e820"/"pci=no_e820" on the kernel cmdline takes precedence */
  220. if (pci_probe & PCI_NO_E820)
  221. pci_use_e820 = false;
  222. else if (pci_probe & PCI_USE_E820)
  223. pci_use_e820 = true;
  224. printk(KERN_INFO "PCI: %s E820 reservations for host bridge windows\n",
  225. pci_use_e820 ? "Using" : "Ignoring");
  226. if (pci_probe & (PCI_NO_E820 | PCI_USE_E820))
  227. printk(KERN_INFO "PCI: Please notify [email protected] so future kernels can this automatically\n");
  228. }
  229. #ifdef CONFIG_PCI_MMCONFIG
  230. static int check_segment(u16 seg, struct device *dev, char *estr)
  231. {
  232. if (seg) {
  233. dev_err(dev,
  234. "%s can't access PCI configuration "
  235. "space under this host bridge.\n",
  236. estr);
  237. return -EIO;
  238. }
  239. /*
  240. * Failure in adding MMCFG information is not fatal,
  241. * just can't access extended configuration space of
  242. * devices under this host bridge.
  243. */
  244. dev_warn(dev,
  245. "%s can't access extended PCI configuration "
  246. "space under this bridge.\n",
  247. estr);
  248. return 0;
  249. }
  250. static int setup_mcfg_map(struct acpi_pci_root_info *ci)
  251. {
  252. int result, seg;
  253. struct pci_root_info *info;
  254. struct acpi_pci_root *root = ci->root;
  255. struct device *dev = &ci->bridge->dev;
  256. info = container_of(ci, struct pci_root_info, common);
  257. info->start_bus = (u8)root->secondary.start;
  258. info->end_bus = (u8)root->secondary.end;
  259. info->mcfg_added = false;
  260. seg = info->sd.domain;
  261. /* return success if MMCFG is not in use */
  262. if (raw_pci_ext_ops && raw_pci_ext_ops != &pci_mmcfg)
  263. return 0;
  264. if (!(pci_probe & PCI_PROBE_MMCONF))
  265. return check_segment(seg, dev, "MMCONFIG is disabled,");
  266. result = pci_mmconfig_insert(dev, seg, info->start_bus, info->end_bus,
  267. root->mcfg_addr);
  268. if (result == 0) {
  269. /* enable MMCFG if it hasn't been enabled yet */
  270. if (raw_pci_ext_ops == NULL)
  271. raw_pci_ext_ops = &pci_mmcfg;
  272. info->mcfg_added = true;
  273. } else if (result != -EEXIST)
  274. return check_segment(seg, dev,
  275. "fail to add MMCONFIG information,");
  276. return 0;
  277. }
  278. static void teardown_mcfg_map(struct acpi_pci_root_info *ci)
  279. {
  280. struct pci_root_info *info;
  281. info = container_of(ci, struct pci_root_info, common);
  282. if (info->mcfg_added) {
  283. pci_mmconfig_delete(info->sd.domain,
  284. info->start_bus, info->end_bus);
  285. info->mcfg_added = false;
  286. }
  287. }
  288. #else
  289. static int setup_mcfg_map(struct acpi_pci_root_info *ci)
  290. {
  291. return 0;
  292. }
  293. static void teardown_mcfg_map(struct acpi_pci_root_info *ci)
  294. {
  295. }
  296. #endif
  297. static int pci_acpi_root_get_node(struct acpi_pci_root *root)
  298. {
  299. int busnum = root->secondary.start;
  300. struct acpi_device *device = root->device;
  301. int node = acpi_get_node(device->handle);
  302. if (node == NUMA_NO_NODE) {
  303. node = x86_pci_root_bus_node(busnum);
  304. if (node != 0 && node != NUMA_NO_NODE)
  305. dev_info(&device->dev, FW_BUG "no _PXM; falling back to node %d from hardware (may be inconsistent with ACPI node numbers)\n",
  306. node);
  307. }
  308. if (node != NUMA_NO_NODE && !node_online(node))
  309. node = NUMA_NO_NODE;
  310. return node;
  311. }
  312. static int pci_acpi_root_init_info(struct acpi_pci_root_info *ci)
  313. {
  314. return setup_mcfg_map(ci);
  315. }
  316. static void pci_acpi_root_release_info(struct acpi_pci_root_info *ci)
  317. {
  318. teardown_mcfg_map(ci);
  319. kfree(container_of(ci, struct pci_root_info, common));
  320. }
  321. /*
  322. * An IO port or MMIO resource assigned to a PCI host bridge may be
  323. * consumed by the host bridge itself or available to its child
  324. * bus/devices. The ACPI specification defines a bit (Producer/Consumer)
  325. * to tell whether the resource is consumed by the host bridge itself,
  326. * but firmware hasn't used that bit consistently, so we can't rely on it.
  327. *
  328. * On x86 and IA64 platforms, all IO port and MMIO resources are assumed
  329. * to be available to child bus/devices except one special case:
  330. * IO port [0xCF8-0xCFF] is consumed by the host bridge itself
  331. * to access PCI configuration space.
  332. *
  333. * So explicitly filter out PCI CFG IO ports[0xCF8-0xCFF].
  334. */
  335. static bool resource_is_pcicfg_ioport(struct resource *res)
  336. {
  337. return (res->flags & IORESOURCE_IO) &&
  338. res->start == 0xCF8 && res->end == 0xCFF;
  339. }
  340. static int pci_acpi_root_prepare_resources(struct acpi_pci_root_info *ci)
  341. {
  342. struct acpi_device *device = ci->bridge;
  343. int busnum = ci->root->secondary.start;
  344. struct resource_entry *entry, *tmp;
  345. int status;
  346. status = acpi_pci_probe_root_resources(ci);
  347. if (pci_use_crs) {
  348. resource_list_for_each_entry_safe(entry, tmp, &ci->resources)
  349. if (resource_is_pcicfg_ioport(entry->res))
  350. resource_list_destroy_entry(entry);
  351. return status;
  352. }
  353. resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
  354. dev_printk(KERN_DEBUG, &device->dev,
  355. "host bridge window %pR (ignored)\n", entry->res);
  356. resource_list_destroy_entry(entry);
  357. }
  358. x86_pci_root_bus_resources(busnum, &ci->resources);
  359. return 0;
  360. }
  361. static struct acpi_pci_root_ops acpi_pci_root_ops = {
  362. .pci_ops = &pci_root_ops,
  363. .init_info = pci_acpi_root_init_info,
  364. .release_info = pci_acpi_root_release_info,
  365. .prepare_resources = pci_acpi_root_prepare_resources,
  366. };
  367. struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
  368. {
  369. int domain = root->segment;
  370. int busnum = root->secondary.start;
  371. int node = pci_acpi_root_get_node(root);
  372. struct pci_bus *bus;
  373. if (pci_ignore_seg)
  374. root->segment = domain = 0;
  375. if (domain && !pci_domains_supported) {
  376. printk(KERN_WARNING "pci_bus %04x:%02x: "
  377. "ignored (multiple domains not supported)\n",
  378. domain, busnum);
  379. return NULL;
  380. }
  381. bus = pci_find_bus(domain, busnum);
  382. if (bus) {
  383. /*
  384. * If the desired bus has been scanned already, replace
  385. * its bus->sysdata.
  386. */
  387. struct pci_sysdata sd = {
  388. .domain = domain,
  389. .node = node,
  390. .companion = root->device
  391. };
  392. memcpy(bus->sysdata, &sd, sizeof(sd));
  393. } else {
  394. struct pci_root_info *info;
  395. info = kzalloc(sizeof(*info), GFP_KERNEL);
  396. if (!info)
  397. dev_err(&root->device->dev,
  398. "pci_bus %04x:%02x: ignored (out of memory)\n",
  399. domain, busnum);
  400. else {
  401. info->sd.domain = domain;
  402. info->sd.node = node;
  403. info->sd.companion = root->device;
  404. bus = acpi_pci_root_create(root, &acpi_pci_root_ops,
  405. &info->common, &info->sd);
  406. }
  407. }
  408. /* After the PCI-E bus has been walked and all devices discovered,
  409. * configure any settings of the fabric that might be necessary.
  410. */
  411. if (bus) {
  412. struct pci_bus *child;
  413. list_for_each_entry(child, &bus->children, node)
  414. pcie_bus_configure_settings(child);
  415. }
  416. return bus;
  417. }
  418. int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
  419. {
  420. /*
  421. * We pass NULL as parent to pci_create_root_bus(), so if it is not NULL
  422. * here, pci_create_root_bus() has been called by someone else and
  423. * sysdata is likely to be different from what we expect. Let it go in
  424. * that case.
  425. */
  426. if (!bridge->dev.parent) {
  427. struct pci_sysdata *sd = bridge->bus->sysdata;
  428. ACPI_COMPANION_SET(&bridge->dev, sd->companion);
  429. }
  430. return 0;
  431. }
  432. int __init pci_acpi_init(void)
  433. {
  434. struct pci_dev *dev = NULL;
  435. if (acpi_noirq)
  436. return -ENODEV;
  437. printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
  438. acpi_irq_penalty_init();
  439. pcibios_enable_irq = acpi_pci_irq_enable;
  440. pcibios_disable_irq = acpi_pci_irq_disable;
  441. x86_init.pci.init_irq = x86_init_noop;
  442. if (pci_routeirq) {
  443. /*
  444. * PCI IRQ routing is set up by pci_enable_device(), but we
  445. * also do it here in case there are still broken drivers that
  446. * don't use pci_enable_device().
  447. */
  448. printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
  449. for_each_pci_dev(dev)
  450. acpi_pci_irq_enable(dev);
  451. }
  452. return 0;
  453. }