pci_root.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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 <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  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. /**
  252. * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
  253. * @handle: the handle in question
  254. *
  255. * Given an ACPI CA handle, the desired PCI device is located in the
  256. * list of PCI devices.
  257. *
  258. * If the device is found, its reference count is increased and this
  259. * function returns a pointer to its data structure. The caller must
  260. * decrement the reference count by calling pci_dev_put().
  261. * If no device is found, %NULL is returned.
  262. */
  263. struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
  264. {
  265. struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
  266. struct acpi_device_physical_node *pn;
  267. struct pci_dev *pci_dev = NULL;
  268. if (!adev)
  269. return NULL;
  270. mutex_lock(&adev->physical_node_lock);
  271. list_for_each_entry(pn, &adev->physical_node_list, node) {
  272. if (dev_is_pci(pn->dev)) {
  273. get_device(pn->dev);
  274. pci_dev = to_pci_dev(pn->dev);
  275. break;
  276. }
  277. }
  278. mutex_unlock(&adev->physical_node_lock);
  279. return pci_dev;
  280. }
  281. EXPORT_SYMBOL_GPL(acpi_get_pci_dev);
  282. /**
  283. * acpi_pci_osc_control_set - Request control of PCI root _OSC features.
  284. * @handle: ACPI handle of a PCI root bridge (or PCIe Root Complex).
  285. * @mask: Mask of _OSC bits to request control of, place to store control mask.
  286. * @support: _OSC supported capability.
  287. * @cxl_mask: Mask of CXL _OSC control bits, place to store control mask.
  288. * @cxl_support: CXL _OSC supported capability.
  289. *
  290. * Run _OSC query for @mask and if that is successful, compare the returned
  291. * mask of control bits with @req. If all of the @req bits are set in the
  292. * returned mask, run _OSC request for it.
  293. *
  294. * The variable at the @mask address may be modified regardless of whether or
  295. * not the function returns success. On success it will contain the mask of
  296. * _OSC bits the BIOS has granted control of, but its contents are meaningless
  297. * on failure.
  298. **/
  299. static acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 *mask,
  300. u32 support, u32 *cxl_mask,
  301. u32 cxl_support)
  302. {
  303. u32 req = OSC_PCI_EXPRESS_CAPABILITY_CONTROL;
  304. struct acpi_pci_root *root;
  305. acpi_status status;
  306. u32 ctrl, cxl_ctrl = 0, capbuf[OSC_CXL_CAPABILITY_DWORDS];
  307. if (!mask)
  308. return AE_BAD_PARAMETER;
  309. root = acpi_pci_find_root(handle);
  310. if (!root)
  311. return AE_NOT_EXIST;
  312. ctrl = *mask;
  313. *mask |= root->osc_control_set;
  314. if (is_cxl(root)) {
  315. cxl_ctrl = *cxl_mask;
  316. *cxl_mask |= root->osc_ext_control_set;
  317. }
  318. /* Need to check the available controls bits before requesting them. */
  319. do {
  320. u32 pci_missing = 0, cxl_missing = 0;
  321. status = acpi_pci_query_osc(root, support, mask, cxl_support,
  322. cxl_mask);
  323. if (ACPI_FAILURE(status))
  324. return status;
  325. if (is_cxl(root)) {
  326. if (ctrl == *mask && cxl_ctrl == *cxl_mask)
  327. break;
  328. pci_missing = ctrl & ~(*mask);
  329. cxl_missing = cxl_ctrl & ~(*cxl_mask);
  330. } else {
  331. if (ctrl == *mask)
  332. break;
  333. pci_missing = ctrl & ~(*mask);
  334. }
  335. if (pci_missing)
  336. decode_osc_control(root, "platform does not support",
  337. pci_missing);
  338. if (cxl_missing)
  339. decode_cxl_osc_control(root, "CXL platform does not support",
  340. cxl_missing);
  341. ctrl = *mask;
  342. cxl_ctrl = *cxl_mask;
  343. } while (*mask || *cxl_mask);
  344. /* No need to request _OSC if the control was already granted. */
  345. if ((root->osc_control_set & ctrl) == ctrl &&
  346. (root->osc_ext_control_set & cxl_ctrl) == cxl_ctrl)
  347. return AE_OK;
  348. if ((ctrl & req) != req) {
  349. decode_osc_control(root, "not requesting control; platform does not support",
  350. req & ~(ctrl));
  351. return AE_SUPPORT;
  352. }
  353. capbuf[OSC_QUERY_DWORD] = 0;
  354. capbuf[OSC_SUPPORT_DWORD] = root->osc_support_set;
  355. capbuf[OSC_CONTROL_DWORD] = ctrl;
  356. if (is_cxl(root)) {
  357. capbuf[OSC_EXT_SUPPORT_DWORD] = root->osc_ext_support_set;
  358. capbuf[OSC_EXT_CONTROL_DWORD] = cxl_ctrl;
  359. }
  360. status = acpi_pci_run_osc(root, capbuf, mask, cxl_mask);
  361. if (ACPI_FAILURE(status))
  362. return status;
  363. root->osc_control_set = *mask;
  364. root->osc_ext_control_set = *cxl_mask;
  365. return AE_OK;
  366. }
  367. static u32 calculate_support(void)
  368. {
  369. u32 support;
  370. /*
  371. * All supported architectures that use ACPI have support for
  372. * PCI domains, so we indicate this in _OSC support capabilities.
  373. */
  374. support = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
  375. support |= OSC_PCI_HPX_TYPE_3_SUPPORT;
  376. if (pci_ext_cfg_avail())
  377. support |= OSC_PCI_EXT_CONFIG_SUPPORT;
  378. if (pcie_aspm_support_enabled())
  379. support |= OSC_PCI_ASPM_SUPPORT | OSC_PCI_CLOCK_PM_SUPPORT;
  380. if (pci_msi_enabled())
  381. support |= OSC_PCI_MSI_SUPPORT;
  382. if (IS_ENABLED(CONFIG_PCIE_EDR))
  383. support |= OSC_PCI_EDR_SUPPORT;
  384. return support;
  385. }
  386. /*
  387. * Background on hotplug support, and making it depend on only
  388. * CONFIG_HOTPLUG_PCI_PCIE vs. also considering CONFIG_MEMORY_HOTPLUG:
  389. *
  390. * CONFIG_ACPI_HOTPLUG_MEMORY does depend on CONFIG_MEMORY_HOTPLUG, but
  391. * there is no existing _OSC for memory hotplug support. The reason is that
  392. * ACPI memory hotplug requires the OS to acknowledge / coordinate with
  393. * memory plug events via a scan handler. On the CXL side the equivalent
  394. * would be if Linux supported the Mechanical Retention Lock [1], or
  395. * otherwise had some coordination for the driver of a PCI device
  396. * undergoing hotplug to be consulted on whether the hotplug should
  397. * proceed or not.
  398. *
  399. * The concern is that if Linux says no to supporting CXL hotplug then
  400. * the BIOS may say no to giving the OS hotplug control of any other PCIe
  401. * device. So the question here is not whether hotplug is enabled, it's
  402. * whether it is handled natively by the at all OS, and if
  403. * CONFIG_HOTPLUG_PCI_PCIE is enabled then the answer is "yes".
  404. *
  405. * Otherwise, the plan for CXL coordinated remove, since the kernel does
  406. * not support blocking hotplug, is to require the memory device to be
  407. * disabled before hotplug is attempted. When CONFIG_MEMORY_HOTPLUG is
  408. * disabled that step will fail and the remove attempt cancelled by the
  409. * user. If that is not honored and the card is removed anyway then it
  410. * does not matter if CONFIG_MEMORY_HOTPLUG is enabled or not, it will
  411. * cause a crash and other badness.
  412. *
  413. * Therefore, just say yes to CXL hotplug and require removal to
  414. * be coordinated by userspace unless and until the kernel grows better
  415. * mechanisms for doing "managed" removal of devices in consultation with
  416. * the driver.
  417. *
  418. * [1]: https://lore.kernel.org/all/20201122014203.4706-1-ashok.raj@intel.com/
  419. */
  420. static u32 calculate_cxl_support(void)
  421. {
  422. u32 support;
  423. support = OSC_CXL_2_0_PORT_DEV_REG_ACCESS_SUPPORT;
  424. support |= OSC_CXL_1_1_PORT_REG_ACCESS_SUPPORT;
  425. if (pci_aer_available())
  426. support |= OSC_CXL_PROTOCOL_ERR_REPORTING_SUPPORT;
  427. if (IS_ENABLED(CONFIG_HOTPLUG_PCI_PCIE))
  428. support |= OSC_CXL_NATIVE_HP_SUPPORT;
  429. return support;
  430. }
  431. static u32 calculate_control(void)
  432. {
  433. u32 control;
  434. control = OSC_PCI_EXPRESS_CAPABILITY_CONTROL
  435. | OSC_PCI_EXPRESS_PME_CONTROL;
  436. if (IS_ENABLED(CONFIG_PCIEASPM))
  437. control |= OSC_PCI_EXPRESS_LTR_CONTROL;
  438. if (IS_ENABLED(CONFIG_HOTPLUG_PCI_PCIE))
  439. control |= OSC_PCI_EXPRESS_NATIVE_HP_CONTROL;
  440. if (IS_ENABLED(CONFIG_HOTPLUG_PCI_SHPC))
  441. control |= OSC_PCI_SHPC_NATIVE_HP_CONTROL;
  442. if (pci_aer_available())
  443. control |= OSC_PCI_EXPRESS_AER_CONTROL;
  444. /*
  445. * Per the Downstream Port Containment Related Enhancements ECN to
  446. * the PCI Firmware Spec, r3.2, sec 4.5.1, table 4-5,
  447. * OSC_PCI_EXPRESS_DPC_CONTROL indicates the OS supports both DPC
  448. * and EDR.
  449. */
  450. if (IS_ENABLED(CONFIG_PCIE_DPC) && IS_ENABLED(CONFIG_PCIE_EDR))
  451. control |= OSC_PCI_EXPRESS_DPC_CONTROL;
  452. return control;
  453. }
  454. static u32 calculate_cxl_control(void)
  455. {
  456. u32 control = 0;
  457. if (IS_ENABLED(CONFIG_MEMORY_FAILURE))
  458. control |= OSC_CXL_ERROR_REPORTING_CONTROL;
  459. return control;
  460. }
  461. static bool os_control_query_checks(struct acpi_pci_root *root, u32 support)
  462. {
  463. struct acpi_device *device = root->device;
  464. if (pcie_ports_disabled) {
  465. dev_info(&device->dev, "PCIe port services disabled; not requesting _OSC control\n");
  466. return false;
  467. }
  468. if ((support & ACPI_PCIE_REQ_SUPPORT) != ACPI_PCIE_REQ_SUPPORT) {
  469. decode_osc_support(root, "not requesting OS control; OS requires",
  470. ACPI_PCIE_REQ_SUPPORT);
  471. return false;
  472. }
  473. return true;
  474. }
  475. static void negotiate_os_control(struct acpi_pci_root *root, int *no_aspm)
  476. {
  477. u32 support, control = 0, requested = 0;
  478. u32 cxl_support = 0, cxl_control = 0, cxl_requested = 0;
  479. acpi_status status;
  480. struct acpi_device *device = root->device;
  481. acpi_handle handle = device->handle;
  482. /*
  483. * Apple always return failure on _OSC calls when _OSI("Darwin") has
  484. * been called successfully. We know the feature set supported by the
  485. * platform, so avoid calling _OSC at all
  486. */
  487. if (x86_apple_machine) {
  488. root->osc_control_set = ~OSC_PCI_EXPRESS_PME_CONTROL;
  489. decode_osc_control(root, "OS assumes control of",
  490. root->osc_control_set);
  491. return;
  492. }
  493. support = calculate_support();
  494. decode_osc_support(root, "OS supports", support);
  495. if (os_control_query_checks(root, support))
  496. requested = control = calculate_control();
  497. if (is_cxl(root)) {
  498. cxl_support = calculate_cxl_support();
  499. decode_cxl_osc_support(root, "OS supports", cxl_support);
  500. cxl_requested = cxl_control = calculate_cxl_control();
  501. }
  502. status = acpi_pci_osc_control_set(handle, &control, support,
  503. &cxl_control, cxl_support);
  504. if (ACPI_SUCCESS(status)) {
  505. if (control)
  506. decode_osc_control(root, "OS now controls", control);
  507. if (cxl_control)
  508. decode_cxl_osc_control(root, "OS now controls",
  509. cxl_control);
  510. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
  511. /*
  512. * We have ASPM control, but the FADT indicates that
  513. * it's unsupported. Leave existing configuration
  514. * intact and prevent the OS from touching it.
  515. */
  516. dev_info(&device->dev, "FADT indicates ASPM is unsupported, using BIOS configuration\n");
  517. *no_aspm = 1;
  518. }
  519. } else {
  520. /*
  521. * We want to disable ASPM here, but aspm_disabled
  522. * needs to remain in its state from boot so that we
  523. * properly handle PCIe 1.1 devices. So we set this
  524. * flag here, to defer the action until after the ACPI
  525. * root scan.
  526. */
  527. *no_aspm = 1;
  528. /* _OSC is optional for PCI host bridges */
  529. if (status == AE_NOT_FOUND && !is_pcie(root))
  530. return;
  531. if (control) {
  532. decode_osc_control(root, "OS requested", requested);
  533. decode_osc_control(root, "platform willing to grant", control);
  534. }
  535. if (cxl_control) {
  536. decode_cxl_osc_control(root, "OS requested", cxl_requested);
  537. decode_cxl_osc_control(root, "platform willing to grant",
  538. cxl_control);
  539. }
  540. dev_info(&device->dev, "_OSC: platform retains control of PCIe features (%s)\n",
  541. acpi_format_exception(status));
  542. }
  543. }
  544. static int acpi_pci_root_add(struct acpi_device *device,
  545. const struct acpi_device_id *not_used)
  546. {
  547. unsigned long long segment, bus;
  548. acpi_status status;
  549. int result;
  550. struct acpi_pci_root *root;
  551. acpi_handle handle = device->handle;
  552. int no_aspm = 0;
  553. bool hotadd = system_state == SYSTEM_RUNNING;
  554. const char *acpi_hid;
  555. root = kzalloc_obj(struct acpi_pci_root);
  556. if (!root)
  557. return -ENOMEM;
  558. segment = 0;
  559. status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL,
  560. &segment);
  561. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  562. dev_err(&device->dev, "can't evaluate _SEG\n");
  563. result = -ENODEV;
  564. goto end;
  565. }
  566. /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
  567. root->secondary.flags = IORESOURCE_BUS;
  568. status = try_get_root_bridge_busnr(handle, &root->secondary);
  569. if (ACPI_FAILURE(status)) {
  570. /*
  571. * We need both the start and end of the downstream bus range
  572. * to interpret _CBA (MMCONFIG base address), so it really is
  573. * supposed to be in _CRS. If we don't find it there, all we
  574. * can do is assume [_BBN-0xFF] or [0-0xFF].
  575. */
  576. root->secondary.end = 0xFF;
  577. dev_warn(&device->dev,
  578. FW_BUG "no secondary bus range in _CRS\n");
  579. status = acpi_evaluate_integer(handle, METHOD_NAME__BBN,
  580. NULL, &bus);
  581. if (ACPI_SUCCESS(status))
  582. root->secondary.start = bus;
  583. else if (status == AE_NOT_FOUND)
  584. root->secondary.start = 0;
  585. else {
  586. dev_err(&device->dev, "can't evaluate _BBN\n");
  587. result = -ENODEV;
  588. goto end;
  589. }
  590. }
  591. root->device = device;
  592. root->segment = segment & 0xFFFF;
  593. strscpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
  594. strscpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
  595. device->driver_data = root;
  596. if (hotadd && dmar_device_add(handle)) {
  597. result = -ENXIO;
  598. goto end;
  599. }
  600. pr_info("%s [%s] (domain %04x %pR)\n",
  601. acpi_device_name(device), acpi_device_bid(device),
  602. root->segment, &root->secondary);
  603. root->mcfg_addr = acpi_pci_root_get_mcfg_addr(handle);
  604. acpi_hid = acpi_device_hid(root->device);
  605. if (strcmp(acpi_hid, "PNP0A08") == 0)
  606. root->bridge_type = ACPI_BRIDGE_TYPE_PCIE;
  607. else if (strcmp(acpi_hid, "ACPI0016") == 0)
  608. root->bridge_type = ACPI_BRIDGE_TYPE_CXL;
  609. else
  610. dev_dbg(&device->dev, "Assuming non-PCIe host bridge\n");
  611. negotiate_os_control(root, &no_aspm);
  612. /*
  613. * TBD: Need PCI interface for enumeration/configuration of roots.
  614. */
  615. /*
  616. * Scan the Root Bridge
  617. * --------------------
  618. * Must do this prior to any attempt to bind the root device, as the
  619. * PCI namespace does not get created until this call is made (and
  620. * thus the root bridge's pci_dev does not exist).
  621. */
  622. root->bus = pci_acpi_scan_root(root);
  623. if (!root->bus) {
  624. dev_err(&device->dev,
  625. "Bus %04x:%02x not present in PCI namespace\n",
  626. root->segment, (unsigned int)root->secondary.start);
  627. device->driver_data = NULL;
  628. result = -ENODEV;
  629. goto remove_dmar;
  630. }
  631. if (no_aspm)
  632. pcie_no_aspm();
  633. pci_acpi_add_root_pm_notifier(device, root);
  634. device_set_wakeup_capable(root->bus->bridge, device->wakeup.flags.valid);
  635. if (hotadd) {
  636. pcibios_resource_survey_bus(root->bus);
  637. pci_assign_unassigned_root_bus_resources(root->bus);
  638. /*
  639. * This is only called for the hotadd case. For the boot-time
  640. * case, we need to wait until after PCI initialization in
  641. * order to deal with IOAPICs mapped in on a PCI BAR.
  642. *
  643. * This is currently x86-specific, because acpi_ioapic_add()
  644. * is an empty function without CONFIG_ACPI_HOTPLUG_IOAPIC.
  645. * And CONFIG_ACPI_HOTPLUG_IOAPIC depends on CONFIG_X86_IO_APIC
  646. * (see drivers/acpi/Kconfig).
  647. */
  648. acpi_ioapic_add(root->device->handle);
  649. }
  650. pci_lock_rescan_remove();
  651. pci_bus_add_devices(root->bus);
  652. pci_unlock_rescan_remove();
  653. return 1;
  654. remove_dmar:
  655. if (hotadd)
  656. dmar_device_remove(handle);
  657. end:
  658. kfree(root);
  659. return result;
  660. }
  661. static void acpi_pci_root_remove(struct acpi_device *device)
  662. {
  663. struct acpi_pci_root *root = acpi_driver_data(device);
  664. pci_lock_rescan_remove();
  665. pci_stop_root_bus(root->bus);
  666. pci_ioapic_remove(root);
  667. device_set_wakeup_capable(root->bus->bridge, false);
  668. pci_acpi_remove_bus_pm_notifier(device);
  669. pci_remove_root_bus(root->bus);
  670. WARN_ON(acpi_ioapic_remove(root));
  671. dmar_device_remove(device->handle);
  672. pci_unlock_rescan_remove();
  673. kfree(root);
  674. }
  675. /*
  676. * Following code to support acpi_pci_root_create() is copied from
  677. * arch/x86/pci/acpi.c and modified so it could be reused by x86, IA64
  678. * and ARM64.
  679. */
  680. static void acpi_pci_root_validate_resources(struct device *dev,
  681. struct list_head *resources,
  682. unsigned long type)
  683. {
  684. LIST_HEAD(list);
  685. struct resource *res1, *res2, *root = NULL;
  686. struct resource_entry *tmp, *entry, *entry2;
  687. BUG_ON((type & (IORESOURCE_MEM | IORESOURCE_IO)) == 0);
  688. root = (type & IORESOURCE_MEM) ? &iomem_resource : &ioport_resource;
  689. list_splice_init(resources, &list);
  690. resource_list_for_each_entry_safe(entry, tmp, &list) {
  691. bool free = false;
  692. resource_size_t end;
  693. res1 = entry->res;
  694. if (!(res1->flags & type))
  695. goto next;
  696. /* Exclude non-addressable range or non-addressable portion */
  697. end = min(res1->end, root->end);
  698. if (end <= res1->start) {
  699. dev_info(dev, "host bridge window %pR (ignored, not CPU addressable)\n",
  700. res1);
  701. free = true;
  702. goto next;
  703. } else if (res1->end != end) {
  704. dev_info(dev, "host bridge window %pR ([%#llx-%#llx] ignored, not CPU addressable)\n",
  705. res1, (unsigned long long)end + 1,
  706. (unsigned long long)res1->end);
  707. res1->end = end;
  708. }
  709. resource_list_for_each_entry(entry2, resources) {
  710. res2 = entry2->res;
  711. if (!(res2->flags & type))
  712. continue;
  713. /*
  714. * I don't like throwing away windows because then
  715. * our resources no longer match the ACPI _CRS, but
  716. * the kernel resource tree doesn't allow overlaps.
  717. */
  718. if (resource_union(res1, res2, res2)) {
  719. dev_info(dev, "host bridge window expanded to %pR; %pR ignored\n",
  720. res2, res1);
  721. free = true;
  722. goto next;
  723. }
  724. }
  725. next:
  726. resource_list_del(entry);
  727. if (free)
  728. resource_list_free_entry(entry);
  729. else
  730. resource_list_add_tail(entry, resources);
  731. }
  732. }
  733. static void acpi_pci_root_remap_iospace(const struct fwnode_handle *fwnode,
  734. struct resource_entry *entry)
  735. {
  736. #ifdef PCI_IOBASE
  737. struct resource *res = entry->res;
  738. resource_size_t cpu_addr = res->start;
  739. resource_size_t pci_addr = cpu_addr - entry->offset;
  740. resource_size_t length = resource_size(res);
  741. unsigned long port;
  742. if (pci_register_io_range(fwnode, cpu_addr, length))
  743. goto err;
  744. port = pci_address_to_pio(cpu_addr);
  745. if (port == (unsigned long)-1)
  746. goto err;
  747. res->start = port;
  748. res->end = port + length - 1;
  749. entry->offset = port - pci_addr;
  750. if (pci_remap_iospace(res, cpu_addr) < 0)
  751. goto err;
  752. pr_info("Remapped I/O %pa to %pR\n", &cpu_addr, res);
  753. return;
  754. err:
  755. res->flags |= IORESOURCE_DISABLED;
  756. #endif
  757. }
  758. int acpi_pci_probe_root_resources(struct acpi_pci_root_info *info)
  759. {
  760. int ret;
  761. struct list_head *list = &info->resources;
  762. struct acpi_device *device = info->bridge;
  763. struct resource_entry *entry, *tmp;
  764. unsigned long flags;
  765. flags = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_MEM_8AND16BIT;
  766. ret = acpi_dev_get_resources(device, list,
  767. acpi_dev_filter_resource_type_cb,
  768. (void *)flags);
  769. if (ret < 0)
  770. dev_warn(&device->dev,
  771. "failed to parse _CRS method, error code %d\n", ret);
  772. else if (ret == 0)
  773. dev_dbg(&device->dev,
  774. "no IO and memory resources present in _CRS\n");
  775. else {
  776. resource_list_for_each_entry_safe(entry, tmp, list) {
  777. if (entry->res->flags & IORESOURCE_IO)
  778. acpi_pci_root_remap_iospace(&device->fwnode,
  779. entry);
  780. if (entry->res->flags & IORESOURCE_DISABLED)
  781. resource_list_destroy_entry(entry);
  782. else
  783. entry->res->name = info->name;
  784. }
  785. acpi_pci_root_validate_resources(&device->dev, list,
  786. IORESOURCE_MEM);
  787. acpi_pci_root_validate_resources(&device->dev, list,
  788. IORESOURCE_IO);
  789. }
  790. return ret;
  791. }
  792. static void pci_acpi_root_add_resources(struct acpi_pci_root_info *info)
  793. {
  794. struct resource_entry *entry, *tmp;
  795. struct resource *res, *conflict, *root = NULL;
  796. resource_list_for_each_entry_safe(entry, tmp, &info->resources) {
  797. res = entry->res;
  798. if (res->flags & IORESOURCE_MEM)
  799. root = &iomem_resource;
  800. else if (res->flags & IORESOURCE_IO)
  801. root = &ioport_resource;
  802. else
  803. continue;
  804. /*
  805. * Some legacy x86 host bridge drivers use iomem_resource and
  806. * ioport_resource as default resource pool, skip it.
  807. */
  808. if (res == root)
  809. continue;
  810. conflict = insert_resource_conflict(root, res);
  811. if (conflict) {
  812. dev_info(&info->bridge->dev,
  813. "ignoring host bridge window %pR (conflicts with %s %pR)\n",
  814. res, conflict->name, conflict);
  815. resource_list_destroy_entry(entry);
  816. }
  817. }
  818. }
  819. static void __acpi_pci_root_release_info(struct acpi_pci_root_info *info)
  820. {
  821. struct resource *res;
  822. struct resource_entry *entry, *tmp;
  823. if (!info)
  824. return;
  825. resource_list_for_each_entry_safe(entry, tmp, &info->resources) {
  826. res = entry->res;
  827. if (res->parent &&
  828. (res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
  829. release_resource(res);
  830. resource_list_destroy_entry(entry);
  831. }
  832. info->ops->release_info(info);
  833. }
  834. static void acpi_pci_root_release_info(struct pci_host_bridge *bridge)
  835. {
  836. struct resource *res;
  837. struct resource_entry *entry;
  838. resource_list_for_each_entry(entry, &bridge->windows) {
  839. res = entry->res;
  840. if (res->flags & IORESOURCE_IO)
  841. pci_unmap_iospace(res);
  842. if (res->parent &&
  843. (res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
  844. release_resource(res);
  845. }
  846. __acpi_pci_root_release_info(bridge->release_data);
  847. }
  848. struct pci_bus *acpi_pci_root_create(struct acpi_pci_root *root,
  849. struct acpi_pci_root_ops *ops,
  850. struct acpi_pci_root_info *info,
  851. void *sysdata)
  852. {
  853. int ret, busnum = root->secondary.start;
  854. struct acpi_device *device = root->device;
  855. int node = acpi_get_node(device->handle);
  856. struct pci_bus *bus;
  857. struct pci_host_bridge *host_bridge;
  858. info->root = root;
  859. info->bridge = device;
  860. info->ops = ops;
  861. INIT_LIST_HEAD(&info->resources);
  862. snprintf(info->name, sizeof(info->name), "PCI Bus %04x:%02x",
  863. root->segment, busnum);
  864. if (ops->init_info && ops->init_info(info))
  865. goto out_release_info;
  866. if (ops->prepare_resources)
  867. ret = ops->prepare_resources(info);
  868. else
  869. ret = acpi_pci_probe_root_resources(info);
  870. if (ret < 0)
  871. goto out_release_info;
  872. pci_acpi_root_add_resources(info);
  873. pci_add_resource(&info->resources, &root->secondary);
  874. bus = pci_create_root_bus(NULL, busnum, ops->pci_ops,
  875. sysdata, &info->resources);
  876. if (!bus)
  877. goto out_release_info;
  878. host_bridge = to_pci_host_bridge(bus->bridge);
  879. if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
  880. host_bridge->native_pcie_hotplug = 0;
  881. if (!(root->osc_control_set & OSC_PCI_SHPC_NATIVE_HP_CONTROL))
  882. host_bridge->native_shpc_hotplug = 0;
  883. if (!(root->osc_control_set & OSC_PCI_EXPRESS_AER_CONTROL))
  884. host_bridge->native_aer = 0;
  885. if (!(root->osc_control_set & OSC_PCI_EXPRESS_PME_CONTROL))
  886. host_bridge->native_pme = 0;
  887. if (!(root->osc_control_set & OSC_PCI_EXPRESS_LTR_CONTROL))
  888. host_bridge->native_ltr = 0;
  889. if (!(root->osc_control_set & OSC_PCI_EXPRESS_DPC_CONTROL))
  890. host_bridge->native_dpc = 0;
  891. if (!(root->osc_ext_control_set & OSC_CXL_ERROR_REPORTING_CONTROL))
  892. host_bridge->native_cxl_error = 0;
  893. acpi_dev_power_up_children_with_adr(device);
  894. pci_scan_child_bus(bus);
  895. pci_set_host_bridge_release(host_bridge, acpi_pci_root_release_info,
  896. info);
  897. if (node != NUMA_NO_NODE)
  898. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  899. return bus;
  900. out_release_info:
  901. __acpi_pci_root_release_info(info);
  902. return NULL;
  903. }
  904. void __init acpi_pci_root_init(void)
  905. {
  906. if (acpi_pci_disabled)
  907. return;
  908. pci_acpi_crs_quirks();
  909. acpi_scan_add_handler_with_hotplug(&pci_root_handler, "pci_root");
  910. }