pci_root.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * pci_root.c - ACPI PCI Root Bridge Driver ($Revision: 40 $)
  4. *
  5. * Copyright (C) 2001, 2002 Andy Grover <[email protected]>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <[email protected]>
  7. */
  8. #define pr_fmt(fmt) "ACPI: " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/types.h>
  13. #include <linux/mutex.h>
  14. #include <linux/pm.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/pci.h>
  17. #include <linux/pci-acpi.h>
  18. #include <linux/dmar.h>
  19. #include <linux/acpi.h>
  20. #include <linux/slab.h>
  21. #include <linux/dmi.h>
  22. #include <linux/platform_data/x86/apple.h>
  23. #include "internal.h"
  24. #define ACPI_PCI_ROOT_CLASS "pci_bridge"
  25. #define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
  26. static int acpi_pci_root_add(struct acpi_device *device,
  27. const struct acpi_device_id *not_used);
  28. static void acpi_pci_root_remove(struct acpi_device *device);
  29. static int acpi_pci_root_scan_dependent(struct acpi_device *adev)
  30. {
  31. acpiphp_check_host_bridge(adev);
  32. return 0;
  33. }
  34. #define ACPI_PCIE_REQ_SUPPORT (OSC_PCI_EXT_CONFIG_SUPPORT \
  35. | OSC_PCI_ASPM_SUPPORT \
  36. | OSC_PCI_CLOCK_PM_SUPPORT \
  37. | OSC_PCI_MSI_SUPPORT)
  38. static const struct acpi_device_id root_device_ids[] = {
  39. {"PNP0A03", 0},
  40. {"", 0},
  41. };
  42. static struct acpi_scan_handler pci_root_handler = {
  43. .ids = root_device_ids,
  44. .attach = acpi_pci_root_add,
  45. .detach = acpi_pci_root_remove,
  46. .hotplug = {
  47. .enabled = true,
  48. .scan_dependent = acpi_pci_root_scan_dependent,
  49. },
  50. };
  51. /**
  52. * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
  53. * @handle: the ACPI CA node in question.
  54. *
  55. * Note: we could make this API take a struct acpi_device * instead, but
  56. * for now, it's more convenient to operate on an acpi_handle.
  57. */
  58. int acpi_is_root_bridge(acpi_handle handle)
  59. {
  60. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  61. int ret;
  62. if (!device)
  63. return 0;
  64. ret = acpi_match_device_ids(device, root_device_ids);
  65. if (ret)
  66. return 0;
  67. else
  68. return 1;
  69. }
  70. EXPORT_SYMBOL_GPL(acpi_is_root_bridge);
  71. static acpi_status
  72. get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
  73. {
  74. struct resource *res = data;
  75. struct acpi_resource_address64 address;
  76. acpi_status status;
  77. status = acpi_resource_to_address64(resource, &address);
  78. if (ACPI_FAILURE(status))
  79. return AE_OK;
  80. if ((address.address.address_length > 0) &&
  81. (address.resource_type == ACPI_BUS_NUMBER_RANGE)) {
  82. res->start = address.address.minimum;
  83. res->end = address.address.minimum + address.address.address_length - 1;
  84. }
  85. return AE_OK;
  86. }
  87. static acpi_status try_get_root_bridge_busnr(acpi_handle handle,
  88. struct resource *res)
  89. {
  90. acpi_status status;
  91. res->start = -1;
  92. status =
  93. acpi_walk_resources(handle, METHOD_NAME__CRS,
  94. get_root_bridge_busnr_callback, res);
  95. if (ACPI_FAILURE(status))
  96. return status;
  97. if (res->start == -1)
  98. return AE_ERROR;
  99. return AE_OK;
  100. }
  101. struct pci_osc_bit_struct {
  102. u32 bit;
  103. char *desc;
  104. };
  105. static struct pci_osc_bit_struct pci_osc_support_bit[] = {
  106. { OSC_PCI_EXT_CONFIG_SUPPORT, "ExtendedConfig" },
  107. { OSC_PCI_ASPM_SUPPORT, "ASPM" },
  108. { OSC_PCI_CLOCK_PM_SUPPORT, "ClockPM" },
  109. { OSC_PCI_SEGMENT_GROUPS_SUPPORT, "Segments" },
  110. { OSC_PCI_MSI_SUPPORT, "MSI" },
  111. { OSC_PCI_EDR_SUPPORT, "EDR" },
  112. { OSC_PCI_HPX_TYPE_3_SUPPORT, "HPX-Type3" },
  113. };
  114. static struct pci_osc_bit_struct pci_osc_control_bit[] = {
  115. { OSC_PCI_EXPRESS_NATIVE_HP_CONTROL, "PCIeHotplug" },
  116. { OSC_PCI_SHPC_NATIVE_HP_CONTROL, "SHPCHotplug" },
  117. { OSC_PCI_EXPRESS_PME_CONTROL, "PME" },
  118. { OSC_PCI_EXPRESS_AER_CONTROL, "AER" },
  119. { OSC_PCI_EXPRESS_CAPABILITY_CONTROL, "PCIeCapability" },
  120. { OSC_PCI_EXPRESS_LTR_CONTROL, "LTR" },
  121. { OSC_PCI_EXPRESS_DPC_CONTROL, "DPC" },
  122. };
  123. static struct pci_osc_bit_struct cxl_osc_support_bit[] = {
  124. { OSC_CXL_1_1_PORT_REG_ACCESS_SUPPORT, "CXL11PortRegAccess" },
  125. { OSC_CXL_2_0_PORT_DEV_REG_ACCESS_SUPPORT, "CXL20PortDevRegAccess" },
  126. { OSC_CXL_PROTOCOL_ERR_REPORTING_SUPPORT, "CXLProtocolErrorReporting" },
  127. { OSC_CXL_NATIVE_HP_SUPPORT, "CXLNativeHotPlug" },
  128. };
  129. static struct pci_osc_bit_struct cxl_osc_control_bit[] = {
  130. { OSC_CXL_ERROR_REPORTING_CONTROL, "CXLMemErrorReporting" },
  131. };
  132. static void decode_osc_bits(struct acpi_pci_root *root, char *msg, u32 word,
  133. struct pci_osc_bit_struct *table, int size)
  134. {
  135. char buf[80];
  136. int i, len = 0;
  137. struct pci_osc_bit_struct *entry;
  138. buf[0] = '\0';
  139. for (i = 0, entry = table; i < size; i++, entry++)
  140. if (word & entry->bit)
  141. len += scnprintf(buf + len, sizeof(buf) - len, "%s%s",
  142. len ? " " : "", entry->desc);
  143. dev_info(&root->device->dev, "_OSC: %s [%s]\n", msg, buf);
  144. }
  145. static void decode_osc_support(struct acpi_pci_root *root, char *msg, u32 word)
  146. {
  147. decode_osc_bits(root, msg, word, pci_osc_support_bit,
  148. ARRAY_SIZE(pci_osc_support_bit));
  149. }
  150. static void decode_osc_control(struct acpi_pci_root *root, char *msg, u32 word)
  151. {
  152. decode_osc_bits(root, msg, word, pci_osc_control_bit,
  153. ARRAY_SIZE(pci_osc_control_bit));
  154. }
  155. static void decode_cxl_osc_support(struct acpi_pci_root *root, char *msg, u32 word)
  156. {
  157. decode_osc_bits(root, msg, word, cxl_osc_support_bit,
  158. ARRAY_SIZE(cxl_osc_support_bit));
  159. }
  160. static void decode_cxl_osc_control(struct acpi_pci_root *root, char *msg, u32 word)
  161. {
  162. decode_osc_bits(root, msg, word, cxl_osc_control_bit,
  163. ARRAY_SIZE(cxl_osc_control_bit));
  164. }
  165. static inline bool is_pcie(struct acpi_pci_root *root)
  166. {
  167. return root->bridge_type == ACPI_BRIDGE_TYPE_PCIE;
  168. }
  169. static inline bool is_cxl(struct acpi_pci_root *root)
  170. {
  171. return root->bridge_type == ACPI_BRIDGE_TYPE_CXL;
  172. }
  173. static u8 pci_osc_uuid_str[] = "33DB4D5B-1FF7-401C-9657-7441C03DD766";
  174. static u8 cxl_osc_uuid_str[] = "68F2D50B-C469-4d8A-BD3D-941A103FD3FC";
  175. static char *to_uuid(struct acpi_pci_root *root)
  176. {
  177. if (is_cxl(root))
  178. return cxl_osc_uuid_str;
  179. return pci_osc_uuid_str;
  180. }
  181. static int cap_length(struct acpi_pci_root *root)
  182. {
  183. if (is_cxl(root))
  184. return sizeof(u32) * OSC_CXL_CAPABILITY_DWORDS;
  185. return sizeof(u32) * OSC_PCI_CAPABILITY_DWORDS;
  186. }
  187. static acpi_status acpi_pci_run_osc(struct acpi_pci_root *root,
  188. const u32 *capbuf, u32 *pci_control,
  189. u32 *cxl_control)
  190. {
  191. struct acpi_osc_context context = {
  192. .uuid_str = to_uuid(root),
  193. .rev = 1,
  194. .cap.length = cap_length(root),
  195. .cap.pointer = (void *)capbuf,
  196. };
  197. acpi_status status;
  198. status = acpi_run_osc(root->device->handle, &context);
  199. if (ACPI_SUCCESS(status)) {
  200. *pci_control = acpi_osc_ctx_get_pci_control(&context);
  201. if (is_cxl(root))
  202. *cxl_control = acpi_osc_ctx_get_cxl_control(&context);
  203. kfree(context.ret.pointer);
  204. }
  205. return status;
  206. }
  207. static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root, u32 support,
  208. u32 *control, u32 cxl_support,
  209. u32 *cxl_control)
  210. {
  211. acpi_status status;
  212. u32 pci_result, cxl_result, capbuf[OSC_CXL_CAPABILITY_DWORDS];
  213. support |= root->osc_support_set;
  214. capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
  215. capbuf[OSC_SUPPORT_DWORD] = support;
  216. capbuf[OSC_CONTROL_DWORD] = *control | root->osc_control_set;
  217. if (is_cxl(root)) {
  218. cxl_support |= root->osc_ext_support_set;
  219. capbuf[OSC_EXT_SUPPORT_DWORD] = cxl_support;
  220. capbuf[OSC_EXT_CONTROL_DWORD] = *cxl_control | root->osc_ext_control_set;
  221. }
  222. retry:
  223. status = acpi_pci_run_osc(root, capbuf, &pci_result, &cxl_result);
  224. if (ACPI_SUCCESS(status)) {
  225. root->osc_support_set = support;
  226. *control = pci_result;
  227. if (is_cxl(root)) {
  228. root->osc_ext_support_set = cxl_support;
  229. *cxl_control = cxl_result;
  230. }
  231. } else if (is_cxl(root)) {
  232. /*
  233. * CXL _OSC is optional on CXL 1.1 hosts. Fall back to PCIe _OSC
  234. * upon any failure using CXL _OSC.
  235. */
  236. root->bridge_type = ACPI_BRIDGE_TYPE_PCIE;
  237. goto retry;
  238. }
  239. return status;
  240. }
  241. struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
  242. {
  243. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  244. struct acpi_pci_root *root;
  245. if (!device || acpi_match_device_ids(device, root_device_ids))
  246. return NULL;
  247. root = acpi_driver_data(device);
  248. return root;
  249. }
  250. EXPORT_SYMBOL_GPL(acpi_pci_find_root);
  251. struct acpi_handle_node {
  252. struct list_head node;
  253. acpi_handle handle;
  254. };
  255. /**
  256. * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
  257. * @handle: the handle in question
  258. *
  259. * Given an ACPI CA handle, the desired PCI device is located in the
  260. * list of PCI devices.
  261. *
  262. * If the device is found, its reference count is increased and this
  263. * function returns a pointer to its data structure. The caller must
  264. * decrement the reference count by calling pci_dev_put().
  265. * If no device is found, %NULL is returned.
  266. */
  267. struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
  268. {
  269. struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
  270. struct acpi_device_physical_node *pn;
  271. struct pci_dev *pci_dev = NULL;
  272. if (!adev)
  273. return NULL;
  274. mutex_lock(&adev->physical_node_lock);
  275. list_for_each_entry(pn, &adev->physical_node_list, node) {
  276. if (dev_is_pci(pn->dev)) {
  277. get_device(pn->dev);
  278. pci_dev = to_pci_dev(pn->dev);
  279. break;
  280. }
  281. }
  282. mutex_unlock(&adev->physical_node_lock);
  283. return pci_dev;
  284. }
  285. EXPORT_SYMBOL_GPL(acpi_get_pci_dev);
  286. /**
  287. * acpi_pci_osc_control_set - Request control of PCI root _OSC features.
  288. * @handle: ACPI handle of a PCI root bridge (or PCIe Root Complex).
  289. * @mask: Mask of _OSC bits to request control of, place to store control mask.
  290. * @support: _OSC supported capability.
  291. * @cxl_mask: Mask of CXL _OSC control bits, place to store control mask.
  292. * @cxl_support: CXL _OSC supported capability.
  293. *
  294. * Run _OSC query for @mask and if that is successful, compare the returned
  295. * mask of control bits with @req. If all of the @req bits are set in the
  296. * returned mask, run _OSC request for it.
  297. *
  298. * The variable at the @mask address may be modified regardless of whether or
  299. * not the function returns success. On success it will contain the mask of
  300. * _OSC bits the BIOS has granted control of, but its contents are meaningless
  301. * on failure.
  302. **/
  303. static acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 *mask,
  304. u32 support, u32 *cxl_mask,
  305. u32 cxl_support)
  306. {
  307. u32 req = OSC_PCI_EXPRESS_CAPABILITY_CONTROL;
  308. struct acpi_pci_root *root;
  309. acpi_status status;
  310. u32 ctrl, cxl_ctrl = 0, capbuf[OSC_CXL_CAPABILITY_DWORDS];
  311. if (!mask)
  312. return AE_BAD_PARAMETER;
  313. root = acpi_pci_find_root(handle);
  314. if (!root)
  315. return AE_NOT_EXIST;
  316. ctrl = *mask;
  317. *mask |= root->osc_control_set;
  318. if (is_cxl(root)) {
  319. cxl_ctrl = *cxl_mask;
  320. *cxl_mask |= root->osc_ext_control_set;
  321. }
  322. /* Need to check the available controls bits before requesting them. */
  323. do {
  324. u32 pci_missing = 0, cxl_missing = 0;
  325. status = acpi_pci_query_osc(root, support, mask, cxl_support,
  326. cxl_mask);
  327. if (ACPI_FAILURE(status))
  328. return status;
  329. if (is_cxl(root)) {
  330. if (ctrl == *mask && cxl_ctrl == *cxl_mask)
  331. break;
  332. pci_missing = ctrl & ~(*mask);
  333. cxl_missing = cxl_ctrl & ~(*cxl_mask);
  334. } else {
  335. if (ctrl == *mask)
  336. break;
  337. pci_missing = ctrl & ~(*mask);
  338. }
  339. if (pci_missing)
  340. decode_osc_control(root, "platform does not support",
  341. pci_missing);
  342. if (cxl_missing)
  343. decode_cxl_osc_control(root, "CXL platform does not support",
  344. cxl_missing);
  345. ctrl = *mask;
  346. cxl_ctrl = *cxl_mask;
  347. } while (*mask || *cxl_mask);
  348. /* No need to request _OSC if the control was already granted. */
  349. if ((root->osc_control_set & ctrl) == ctrl &&
  350. (root->osc_ext_control_set & cxl_ctrl) == cxl_ctrl)
  351. return AE_OK;
  352. if ((ctrl & req) != req) {
  353. decode_osc_control(root, "not requesting control; platform does not support",
  354. req & ~(ctrl));
  355. return AE_SUPPORT;
  356. }
  357. capbuf[OSC_QUERY_DWORD] = 0;
  358. capbuf[OSC_SUPPORT_DWORD] = root->osc_support_set;
  359. capbuf[OSC_CONTROL_DWORD] = ctrl;
  360. if (is_cxl(root)) {
  361. capbuf[OSC_EXT_SUPPORT_DWORD] = root->osc_ext_support_set;
  362. capbuf[OSC_EXT_CONTROL_DWORD] = cxl_ctrl;
  363. }
  364. status = acpi_pci_run_osc(root, capbuf, mask, cxl_mask);
  365. if (ACPI_FAILURE(status))
  366. return status;
  367. root->osc_control_set = *mask;
  368. root->osc_ext_control_set = *cxl_mask;
  369. return AE_OK;
  370. }
  371. static u32 calculate_support(void)
  372. {
  373. u32 support;
  374. /*
  375. * All supported architectures that use ACPI have support for
  376. * PCI domains, so we indicate this in _OSC support capabilities.
  377. */
  378. support = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
  379. support |= OSC_PCI_HPX_TYPE_3_SUPPORT;
  380. if (pci_ext_cfg_avail())
  381. support |= OSC_PCI_EXT_CONFIG_SUPPORT;
  382. if (pcie_aspm_support_enabled())
  383. support |= OSC_PCI_ASPM_SUPPORT | OSC_PCI_CLOCK_PM_SUPPORT;
  384. if (pci_msi_enabled())
  385. support |= OSC_PCI_MSI_SUPPORT;
  386. if (IS_ENABLED(CONFIG_PCIE_EDR))
  387. support |= OSC_PCI_EDR_SUPPORT;
  388. return support;
  389. }
  390. /*
  391. * Background on hotplug support, and making it depend on only
  392. * CONFIG_HOTPLUG_PCI_PCIE vs. also considering CONFIG_MEMORY_HOTPLUG:
  393. *
  394. * CONFIG_ACPI_HOTPLUG_MEMORY does depend on CONFIG_MEMORY_HOTPLUG, but
  395. * there is no existing _OSC for memory hotplug support. The reason is that
  396. * ACPI memory hotplug requires the OS to acknowledge / coordinate with
  397. * memory plug events via a scan handler. On the CXL side the equivalent
  398. * would be if Linux supported the Mechanical Retention Lock [1], or
  399. * otherwise had some coordination for the driver of a PCI device
  400. * undergoing hotplug to be consulted on whether the hotplug should
  401. * proceed or not.
  402. *
  403. * The concern is that if Linux says no to supporting CXL hotplug then
  404. * the BIOS may say no to giving the OS hotplug control of any other PCIe
  405. * device. So the question here is not whether hotplug is enabled, it's
  406. * whether it is handled natively by the at all OS, and if
  407. * CONFIG_HOTPLUG_PCI_PCIE is enabled then the answer is "yes".
  408. *
  409. * Otherwise, the plan for CXL coordinated remove, since the kernel does
  410. * not support blocking hotplug, is to require the memory device to be
  411. * disabled before hotplug is attempted. When CONFIG_MEMORY_HOTPLUG is
  412. * disabled that step will fail and the remove attempt cancelled by the
  413. * user. If that is not honored and the card is removed anyway then it
  414. * does not matter if CONFIG_MEMORY_HOTPLUG is enabled or not, it will
  415. * cause a crash and other badness.
  416. *
  417. * Therefore, just say yes to CXL hotplug and require removal to
  418. * be coordinated by userspace unless and until the kernel grows better
  419. * mechanisms for doing "managed" removal of devices in consultation with
  420. * the driver.
  421. *
  422. * [1]: https://lore.kernel.org/all/[email protected]/
  423. */
  424. static u32 calculate_cxl_support(void)
  425. {
  426. u32 support;
  427. support = OSC_CXL_2_0_PORT_DEV_REG_ACCESS_SUPPORT;
  428. if (pci_aer_available())
  429. support |= OSC_CXL_PROTOCOL_ERR_REPORTING_SUPPORT;
  430. if (IS_ENABLED(CONFIG_HOTPLUG_PCI_PCIE))
  431. support |= OSC_CXL_NATIVE_HP_SUPPORT;
  432. return support;
  433. }
  434. static u32 calculate_control(void)
  435. {
  436. u32 control;
  437. control = OSC_PCI_EXPRESS_CAPABILITY_CONTROL
  438. | OSC_PCI_EXPRESS_PME_CONTROL;
  439. if (IS_ENABLED(CONFIG_PCIEASPM))
  440. control |= OSC_PCI_EXPRESS_LTR_CONTROL;
  441. if (IS_ENABLED(CONFIG_HOTPLUG_PCI_PCIE))
  442. control |= OSC_PCI_EXPRESS_NATIVE_HP_CONTROL;
  443. if (IS_ENABLED(CONFIG_HOTPLUG_PCI_SHPC))
  444. control |= OSC_PCI_SHPC_NATIVE_HP_CONTROL;
  445. if (pci_aer_available())
  446. control |= OSC_PCI_EXPRESS_AER_CONTROL;
  447. /*
  448. * Per the Downstream Port Containment Related Enhancements ECN to
  449. * the PCI Firmware Spec, r3.2, sec 4.5.1, table 4-5,
  450. * OSC_PCI_EXPRESS_DPC_CONTROL indicates the OS supports both DPC
  451. * and EDR.
  452. */
  453. if (IS_ENABLED(CONFIG_PCIE_DPC) && IS_ENABLED(CONFIG_PCIE_EDR))
  454. control |= OSC_PCI_EXPRESS_DPC_CONTROL;
  455. return control;
  456. }
  457. static u32 calculate_cxl_control(void)
  458. {
  459. u32 control = 0;
  460. if (IS_ENABLED(CONFIG_MEMORY_FAILURE))
  461. control |= OSC_CXL_ERROR_REPORTING_CONTROL;
  462. return control;
  463. }
  464. static bool os_control_query_checks(struct acpi_pci_root *root, u32 support)
  465. {
  466. struct acpi_device *device = root->device;
  467. if (pcie_ports_disabled) {
  468. dev_info(&device->dev, "PCIe port services disabled; not requesting _OSC control\n");
  469. return false;
  470. }
  471. if ((support & ACPI_PCIE_REQ_SUPPORT) != ACPI_PCIE_REQ_SUPPORT) {
  472. decode_osc_support(root, "not requesting OS control; OS requires",
  473. ACPI_PCIE_REQ_SUPPORT);
  474. return false;
  475. }
  476. return true;
  477. }
  478. static void negotiate_os_control(struct acpi_pci_root *root, int *no_aspm)
  479. {
  480. u32 support, control = 0, requested = 0;
  481. u32 cxl_support = 0, cxl_control = 0, cxl_requested = 0;
  482. acpi_status status;
  483. struct acpi_device *device = root->device;
  484. acpi_handle handle = device->handle;
  485. /*
  486. * Apple always return failure on _OSC calls when _OSI("Darwin") has
  487. * been called successfully. We know the feature set supported by the
  488. * platform, so avoid calling _OSC at all
  489. */
  490. if (x86_apple_machine) {
  491. root->osc_control_set = ~OSC_PCI_EXPRESS_PME_CONTROL;
  492. decode_osc_control(root, "OS assumes control of",
  493. root->osc_control_set);
  494. return;
  495. }
  496. support = calculate_support();
  497. decode_osc_support(root, "OS supports", support);
  498. if (os_control_query_checks(root, support))
  499. requested = control = calculate_control();
  500. if (is_cxl(root)) {
  501. cxl_support = calculate_cxl_support();
  502. decode_cxl_osc_support(root, "OS supports", cxl_support);
  503. cxl_requested = cxl_control = calculate_cxl_control();
  504. }
  505. status = acpi_pci_osc_control_set(handle, &control, support,
  506. &cxl_control, cxl_support);
  507. if (ACPI_SUCCESS(status)) {
  508. if (control)
  509. decode_osc_control(root, "OS now controls", control);
  510. if (cxl_control)
  511. decode_cxl_osc_control(root, "OS now controls",
  512. cxl_control);
  513. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
  514. /*
  515. * We have ASPM control, but the FADT indicates that
  516. * it's unsupported. Leave existing configuration
  517. * intact and prevent the OS from touching it.
  518. */
  519. dev_info(&device->dev, "FADT indicates ASPM is unsupported, using BIOS configuration\n");
  520. *no_aspm = 1;
  521. }
  522. } else {
  523. /*
  524. * We want to disable ASPM here, but aspm_disabled
  525. * needs to remain in its state from boot so that we
  526. * properly handle PCIe 1.1 devices. So we set this
  527. * flag here, to defer the action until after the ACPI
  528. * root scan.
  529. */
  530. *no_aspm = 1;
  531. /* _OSC is optional for PCI host bridges */
  532. if (status == AE_NOT_FOUND && !is_pcie(root))
  533. return;
  534. if (control) {
  535. decode_osc_control(root, "OS requested", requested);
  536. decode_osc_control(root, "platform willing to grant", control);
  537. }
  538. if (cxl_control) {
  539. decode_cxl_osc_control(root, "OS requested", cxl_requested);
  540. decode_cxl_osc_control(root, "platform willing to grant",
  541. cxl_control);
  542. }
  543. dev_info(&device->dev, "_OSC: platform retains control of PCIe features (%s)\n",
  544. acpi_format_exception(status));
  545. }
  546. }
  547. static int acpi_pci_root_add(struct acpi_device *device,
  548. const struct acpi_device_id *not_used)
  549. {
  550. unsigned long long segment, bus;
  551. acpi_status status;
  552. int result;
  553. struct acpi_pci_root *root;
  554. acpi_handle handle = device->handle;
  555. int no_aspm = 0;
  556. bool hotadd = system_state == SYSTEM_RUNNING;
  557. const char *acpi_hid;
  558. root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
  559. if (!root)
  560. return -ENOMEM;
  561. segment = 0;
  562. status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL,
  563. &segment);
  564. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  565. dev_err(&device->dev, "can't evaluate _SEG\n");
  566. result = -ENODEV;
  567. goto end;
  568. }
  569. /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
  570. root->secondary.flags = IORESOURCE_BUS;
  571. status = try_get_root_bridge_busnr(handle, &root->secondary);
  572. if (ACPI_FAILURE(status)) {
  573. /*
  574. * We need both the start and end of the downstream bus range
  575. * to interpret _CBA (MMCONFIG base address), so it really is
  576. * supposed to be in _CRS. If we don't find it there, all we
  577. * can do is assume [_BBN-0xFF] or [0-0xFF].
  578. */
  579. root->secondary.end = 0xFF;
  580. dev_warn(&device->dev,
  581. FW_BUG "no secondary bus range in _CRS\n");
  582. status = acpi_evaluate_integer(handle, METHOD_NAME__BBN,
  583. NULL, &bus);
  584. if (ACPI_SUCCESS(status))
  585. root->secondary.start = bus;
  586. else if (status == AE_NOT_FOUND)
  587. root->secondary.start = 0;
  588. else {
  589. dev_err(&device->dev, "can't evaluate _BBN\n");
  590. result = -ENODEV;
  591. goto end;
  592. }
  593. }
  594. root->device = device;
  595. root->segment = segment & 0xFFFF;
  596. strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
  597. strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
  598. device->driver_data = root;
  599. if (hotadd && dmar_device_add(handle)) {
  600. result = -ENXIO;
  601. goto end;
  602. }
  603. pr_info("%s [%s] (domain %04x %pR)\n",
  604. acpi_device_name(device), acpi_device_bid(device),
  605. root->segment, &root->secondary);
  606. root->mcfg_addr = acpi_pci_root_get_mcfg_addr(handle);
  607. acpi_hid = acpi_device_hid(root->device);
  608. if (strcmp(acpi_hid, "PNP0A08") == 0)
  609. root->bridge_type = ACPI_BRIDGE_TYPE_PCIE;
  610. else if (strcmp(acpi_hid, "ACPI0016") == 0)
  611. root->bridge_type = ACPI_BRIDGE_TYPE_CXL;
  612. else
  613. dev_dbg(&device->dev, "Assuming non-PCIe host bridge\n");
  614. negotiate_os_control(root, &no_aspm);
  615. /*
  616. * TBD: Need PCI interface for enumeration/configuration of roots.
  617. */
  618. /*
  619. * Scan the Root Bridge
  620. * --------------------
  621. * Must do this prior to any attempt to bind the root device, as the
  622. * PCI namespace does not get created until this call is made (and
  623. * thus the root bridge's pci_dev does not exist).
  624. */
  625. root->bus = pci_acpi_scan_root(root);
  626. if (!root->bus) {
  627. dev_err(&device->dev,
  628. "Bus %04x:%02x not present in PCI namespace\n",
  629. root->segment, (unsigned int)root->secondary.start);
  630. device->driver_data = NULL;
  631. result = -ENODEV;
  632. goto remove_dmar;
  633. }
  634. if (no_aspm)
  635. pcie_no_aspm();
  636. pci_acpi_add_bus_pm_notifier(device);
  637. device_set_wakeup_capable(root->bus->bridge, device->wakeup.flags.valid);
  638. if (hotadd) {
  639. pcibios_resource_survey_bus(root->bus);
  640. pci_assign_unassigned_root_bus_resources(root->bus);
  641. /*
  642. * This is only called for the hotadd case. For the boot-time
  643. * case, we need to wait until after PCI initialization in
  644. * order to deal with IOAPICs mapped in on a PCI BAR.
  645. *
  646. * This is currently x86-specific, because acpi_ioapic_add()
  647. * is an empty function without CONFIG_ACPI_HOTPLUG_IOAPIC.
  648. * And CONFIG_ACPI_HOTPLUG_IOAPIC depends on CONFIG_X86_IO_APIC
  649. * (see drivers/acpi/Kconfig).
  650. */
  651. acpi_ioapic_add(root->device->handle);
  652. }
  653. pci_lock_rescan_remove();
  654. pci_bus_add_devices(root->bus);
  655. pci_unlock_rescan_remove();
  656. return 1;
  657. remove_dmar:
  658. if (hotadd)
  659. dmar_device_remove(handle);
  660. end:
  661. kfree(root);
  662. return result;
  663. }
  664. static void acpi_pci_root_remove(struct acpi_device *device)
  665. {
  666. struct acpi_pci_root *root = acpi_driver_data(device);
  667. pci_lock_rescan_remove();
  668. pci_stop_root_bus(root->bus);
  669. pci_ioapic_remove(root);
  670. device_set_wakeup_capable(root->bus->bridge, false);
  671. pci_acpi_remove_bus_pm_notifier(device);
  672. pci_remove_root_bus(root->bus);
  673. WARN_ON(acpi_ioapic_remove(root));
  674. dmar_device_remove(device->handle);
  675. pci_unlock_rescan_remove();
  676. kfree(root);
  677. }
  678. /*
  679. * Following code to support acpi_pci_root_create() is copied from
  680. * arch/x86/pci/acpi.c and modified so it could be reused by x86, IA64
  681. * and ARM64.
  682. */
  683. static void acpi_pci_root_validate_resources(struct device *dev,
  684. struct list_head *resources,
  685. unsigned long type)
  686. {
  687. LIST_HEAD(list);
  688. struct resource *res1, *res2, *root = NULL;
  689. struct resource_entry *tmp, *entry, *entry2;
  690. BUG_ON((type & (IORESOURCE_MEM | IORESOURCE_IO)) == 0);
  691. root = (type & IORESOURCE_MEM) ? &iomem_resource : &ioport_resource;
  692. list_splice_init(resources, &list);
  693. resource_list_for_each_entry_safe(entry, tmp, &list) {
  694. bool free = false;
  695. resource_size_t end;
  696. res1 = entry->res;
  697. if (!(res1->flags & type))
  698. goto next;
  699. /* Exclude non-addressable range or non-addressable portion */
  700. end = min(res1->end, root->end);
  701. if (end <= res1->start) {
  702. dev_info(dev, "host bridge window %pR (ignored, not CPU addressable)\n",
  703. res1);
  704. free = true;
  705. goto next;
  706. } else if (res1->end != end) {
  707. dev_info(dev, "host bridge window %pR ([%#llx-%#llx] ignored, not CPU addressable)\n",
  708. res1, (unsigned long long)end + 1,
  709. (unsigned long long)res1->end);
  710. res1->end = end;
  711. }
  712. resource_list_for_each_entry(entry2, resources) {
  713. res2 = entry2->res;
  714. if (!(res2->flags & type))
  715. continue;
  716. /*
  717. * I don't like throwing away windows because then
  718. * our resources no longer match the ACPI _CRS, but
  719. * the kernel resource tree doesn't allow overlaps.
  720. */
  721. if (resource_union(res1, res2, res2)) {
  722. dev_info(dev, "host bridge window expanded to %pR; %pR ignored\n",
  723. res2, res1);
  724. free = true;
  725. goto next;
  726. }
  727. }
  728. next:
  729. resource_list_del(entry);
  730. if (free)
  731. resource_list_free_entry(entry);
  732. else
  733. resource_list_add_tail(entry, resources);
  734. }
  735. }
  736. static void acpi_pci_root_remap_iospace(struct fwnode_handle *fwnode,
  737. struct resource_entry *entry)
  738. {
  739. #ifdef PCI_IOBASE
  740. struct resource *res = entry->res;
  741. resource_size_t cpu_addr = res->start;
  742. resource_size_t pci_addr = cpu_addr - entry->offset;
  743. resource_size_t length = resource_size(res);
  744. unsigned long port;
  745. if (pci_register_io_range(fwnode, cpu_addr, length))
  746. goto err;
  747. port = pci_address_to_pio(cpu_addr);
  748. if (port == (unsigned long)-1)
  749. goto err;
  750. res->start = port;
  751. res->end = port + length - 1;
  752. entry->offset = port - pci_addr;
  753. if (pci_remap_iospace(res, cpu_addr) < 0)
  754. goto err;
  755. pr_info("Remapped I/O %pa to %pR\n", &cpu_addr, res);
  756. return;
  757. err:
  758. res->flags |= IORESOURCE_DISABLED;
  759. #endif
  760. }
  761. int acpi_pci_probe_root_resources(struct acpi_pci_root_info *info)
  762. {
  763. int ret;
  764. struct list_head *list = &info->resources;
  765. struct acpi_device *device = info->bridge;
  766. struct resource_entry *entry, *tmp;
  767. unsigned long flags;
  768. flags = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_MEM_8AND16BIT;
  769. ret = acpi_dev_get_resources(device, list,
  770. acpi_dev_filter_resource_type_cb,
  771. (void *)flags);
  772. if (ret < 0)
  773. dev_warn(&device->dev,
  774. "failed to parse _CRS method, error code %d\n", ret);
  775. else if (ret == 0)
  776. dev_dbg(&device->dev,
  777. "no IO and memory resources present in _CRS\n");
  778. else {
  779. resource_list_for_each_entry_safe(entry, tmp, list) {
  780. if (entry->res->flags & IORESOURCE_IO)
  781. acpi_pci_root_remap_iospace(&device->fwnode,
  782. entry);
  783. if (entry->res->flags & IORESOURCE_DISABLED)
  784. resource_list_destroy_entry(entry);
  785. else
  786. entry->res->name = info->name;
  787. }
  788. acpi_pci_root_validate_resources(&device->dev, list,
  789. IORESOURCE_MEM);
  790. acpi_pci_root_validate_resources(&device->dev, list,
  791. IORESOURCE_IO);
  792. }
  793. return ret;
  794. }
  795. static void pci_acpi_root_add_resources(struct acpi_pci_root_info *info)
  796. {
  797. struct resource_entry *entry, *tmp;
  798. struct resource *res, *conflict, *root = NULL;
  799. resource_list_for_each_entry_safe(entry, tmp, &info->resources) {
  800. res = entry->res;
  801. if (res->flags & IORESOURCE_MEM)
  802. root = &iomem_resource;
  803. else if (res->flags & IORESOURCE_IO)
  804. root = &ioport_resource;
  805. else
  806. continue;
  807. /*
  808. * Some legacy x86 host bridge drivers use iomem_resource and
  809. * ioport_resource as default resource pool, skip it.
  810. */
  811. if (res == root)
  812. continue;
  813. conflict = insert_resource_conflict(root, res);
  814. if (conflict) {
  815. dev_info(&info->bridge->dev,
  816. "ignoring host bridge window %pR (conflicts with %s %pR)\n",
  817. res, conflict->name, conflict);
  818. resource_list_destroy_entry(entry);
  819. }
  820. }
  821. }
  822. static void __acpi_pci_root_release_info(struct acpi_pci_root_info *info)
  823. {
  824. struct resource *res;
  825. struct resource_entry *entry, *tmp;
  826. if (!info)
  827. return;
  828. resource_list_for_each_entry_safe(entry, tmp, &info->resources) {
  829. res = entry->res;
  830. if (res->parent &&
  831. (res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
  832. release_resource(res);
  833. resource_list_destroy_entry(entry);
  834. }
  835. info->ops->release_info(info);
  836. }
  837. static void acpi_pci_root_release_info(struct pci_host_bridge *bridge)
  838. {
  839. struct resource *res;
  840. struct resource_entry *entry;
  841. resource_list_for_each_entry(entry, &bridge->windows) {
  842. res = entry->res;
  843. if (res->flags & IORESOURCE_IO)
  844. pci_unmap_iospace(res);
  845. if (res->parent &&
  846. (res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
  847. release_resource(res);
  848. }
  849. __acpi_pci_root_release_info(bridge->release_data);
  850. }
  851. struct pci_bus *acpi_pci_root_create(struct acpi_pci_root *root,
  852. struct acpi_pci_root_ops *ops,
  853. struct acpi_pci_root_info *info,
  854. void *sysdata)
  855. {
  856. int ret, busnum = root->secondary.start;
  857. struct acpi_device *device = root->device;
  858. int node = acpi_get_node(device->handle);
  859. struct pci_bus *bus;
  860. struct pci_host_bridge *host_bridge;
  861. union acpi_object *obj;
  862. info->root = root;
  863. info->bridge = device;
  864. info->ops = ops;
  865. INIT_LIST_HEAD(&info->resources);
  866. snprintf(info->name, sizeof(info->name), "PCI Bus %04x:%02x",
  867. root->segment, busnum);
  868. if (ops->init_info && ops->init_info(info))
  869. goto out_release_info;
  870. if (ops->prepare_resources)
  871. ret = ops->prepare_resources(info);
  872. else
  873. ret = acpi_pci_probe_root_resources(info);
  874. if (ret < 0)
  875. goto out_release_info;
  876. pci_acpi_root_add_resources(info);
  877. pci_add_resource(&info->resources, &root->secondary);
  878. bus = pci_create_root_bus(NULL, busnum, ops->pci_ops,
  879. sysdata, &info->resources);
  880. if (!bus)
  881. goto out_release_info;
  882. host_bridge = to_pci_host_bridge(bus->bridge);
  883. if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
  884. host_bridge->native_pcie_hotplug = 0;
  885. if (!(root->osc_control_set & OSC_PCI_SHPC_NATIVE_HP_CONTROL))
  886. host_bridge->native_shpc_hotplug = 0;
  887. if (!(root->osc_control_set & OSC_PCI_EXPRESS_AER_CONTROL))
  888. host_bridge->native_aer = 0;
  889. if (!(root->osc_control_set & OSC_PCI_EXPRESS_PME_CONTROL))
  890. host_bridge->native_pme = 0;
  891. if (!(root->osc_control_set & OSC_PCI_EXPRESS_LTR_CONTROL))
  892. host_bridge->native_ltr = 0;
  893. if (!(root->osc_control_set & OSC_PCI_EXPRESS_DPC_CONTROL))
  894. host_bridge->native_dpc = 0;
  895. /*
  896. * Evaluate the "PCI Boot Configuration" _DSM Function. If it
  897. * exists and returns 0, we must preserve any PCI resource
  898. * assignments made by firmware for this host bridge.
  899. */
  900. obj = acpi_evaluate_dsm(ACPI_HANDLE(bus->bridge), &pci_acpi_dsm_guid, 1,
  901. DSM_PCI_PRESERVE_BOOT_CONFIG, NULL);
  902. if (obj && obj->type == ACPI_TYPE_INTEGER && obj->integer.value == 0)
  903. host_bridge->preserve_config = 1;
  904. ACPI_FREE(obj);
  905. acpi_dev_power_up_children_with_adr(device);
  906. pci_scan_child_bus(bus);
  907. pci_set_host_bridge_release(host_bridge, acpi_pci_root_release_info,
  908. info);
  909. if (node != NUMA_NO_NODE)
  910. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  911. return bus;
  912. out_release_info:
  913. __acpi_pci_root_release_info(info);
  914. return NULL;
  915. }
  916. void __init acpi_pci_root_init(void)
  917. {
  918. if (acpi_pci_disabled)
  919. return;
  920. pci_acpi_crs_quirks();
  921. acpi_scan_add_handler_with_hotplug(&pci_root_handler, "pci_root");
  922. }