tph.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * TPH (TLP Processing Hints) support
  4. *
  5. * Copyright (C) 2024 Advanced Micro Devices, Inc.
  6. * Eric Van Tassell <Eric.VanTassell@amd.com>
  7. * Wei Huang <wei.huang2@amd.com>
  8. */
  9. #include <linux/pci.h>
  10. #include <linux/pci-acpi.h>
  11. #include <linux/msi.h>
  12. #include <linux/bitfield.h>
  13. #include <linux/pci-tph.h>
  14. #include "pci.h"
  15. /* System-wide TPH disabled */
  16. static bool pci_tph_disabled;
  17. #ifdef CONFIG_ACPI
  18. /*
  19. * The st_info struct defines the Steering Tag (ST) info returned by the
  20. * firmware PCI ACPI _DSM method (rev=0x7, func=0xF, "_DSM to Query Cache
  21. * Locality TPH Features"), as specified in the approved ECN for PCI Firmware
  22. * Spec and available at https://members.pcisig.com/wg/PCI-SIG/document/15470.
  23. *
  24. * @vm_st_valid: 8-bit ST for volatile memory is valid
  25. * @vm_xst_valid: 16-bit extended ST for volatile memory is valid
  26. * @vm_ph_ignore: 1 => PH was and will be ignored, 0 => PH should be supplied
  27. * @vm_st: 8-bit ST for volatile mem
  28. * @vm_xst: 16-bit extended ST for volatile mem
  29. * @pm_st_valid: 8-bit ST for persistent memory is valid
  30. * @pm_xst_valid: 16-bit extended ST for persistent memory is valid
  31. * @pm_ph_ignore: 1 => PH was and will be ignored, 0 => PH should be supplied
  32. * @pm_st: 8-bit ST for persistent mem
  33. * @pm_xst: 16-bit extended ST for persistent mem
  34. */
  35. union st_info {
  36. struct {
  37. u64 vm_st_valid : 1;
  38. u64 vm_xst_valid : 1;
  39. u64 vm_ph_ignore : 1;
  40. u64 rsvd1 : 5;
  41. u64 vm_st : 8;
  42. u64 vm_xst : 16;
  43. u64 pm_st_valid : 1;
  44. u64 pm_xst_valid : 1;
  45. u64 pm_ph_ignore : 1;
  46. u64 rsvd2 : 5;
  47. u64 pm_st : 8;
  48. u64 pm_xst : 16;
  49. };
  50. u64 value;
  51. };
  52. static u16 tph_extract_tag(enum tph_mem_type mem_type, u8 req_type,
  53. union st_info *info)
  54. {
  55. switch (req_type) {
  56. case PCI_TPH_REQ_TPH_ONLY: /* 8-bit tag */
  57. switch (mem_type) {
  58. case TPH_MEM_TYPE_VM:
  59. if (info->vm_st_valid)
  60. return info->vm_st;
  61. break;
  62. case TPH_MEM_TYPE_PM:
  63. if (info->pm_st_valid)
  64. return info->pm_st;
  65. break;
  66. }
  67. break;
  68. case PCI_TPH_REQ_EXT_TPH: /* 16-bit tag */
  69. switch (mem_type) {
  70. case TPH_MEM_TYPE_VM:
  71. if (info->vm_xst_valid)
  72. return info->vm_xst;
  73. break;
  74. case TPH_MEM_TYPE_PM:
  75. if (info->pm_xst_valid)
  76. return info->pm_xst;
  77. break;
  78. }
  79. break;
  80. default:
  81. return 0;
  82. }
  83. return 0;
  84. }
  85. #define TPH_ST_DSM_FUNC_INDEX 0xF
  86. static acpi_status tph_invoke_dsm(acpi_handle handle, u32 cpu_uid,
  87. union st_info *st_out)
  88. {
  89. union acpi_object arg3[3], in_obj, *out_obj;
  90. if (!acpi_check_dsm(handle, &pci_acpi_dsm_guid, 7,
  91. BIT(TPH_ST_DSM_FUNC_INDEX)))
  92. return AE_ERROR;
  93. /* DWORD: feature ID (0 for processor cache ST query) */
  94. arg3[0].integer.type = ACPI_TYPE_INTEGER;
  95. arg3[0].integer.value = 0;
  96. /* DWORD: target UID */
  97. arg3[1].integer.type = ACPI_TYPE_INTEGER;
  98. arg3[1].integer.value = cpu_uid;
  99. /* QWORD: properties, all 0's */
  100. arg3[2].integer.type = ACPI_TYPE_INTEGER;
  101. arg3[2].integer.value = 0;
  102. in_obj.type = ACPI_TYPE_PACKAGE;
  103. in_obj.package.count = ARRAY_SIZE(arg3);
  104. in_obj.package.elements = arg3;
  105. out_obj = acpi_evaluate_dsm(handle, &pci_acpi_dsm_guid, 7,
  106. TPH_ST_DSM_FUNC_INDEX, &in_obj);
  107. if (!out_obj)
  108. return AE_ERROR;
  109. if (out_obj->type != ACPI_TYPE_BUFFER) {
  110. ACPI_FREE(out_obj);
  111. return AE_ERROR;
  112. }
  113. st_out->value = *((u64 *)(out_obj->buffer.pointer));
  114. ACPI_FREE(out_obj);
  115. return AE_OK;
  116. }
  117. #endif
  118. /* Update the TPH Requester Enable field of TPH Control Register */
  119. static void set_ctrl_reg_req_en(struct pci_dev *pdev, u8 req_type)
  120. {
  121. u32 reg;
  122. pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CTRL, &reg);
  123. reg &= ~PCI_TPH_CTRL_REQ_EN_MASK;
  124. reg |= FIELD_PREP(PCI_TPH_CTRL_REQ_EN_MASK, req_type);
  125. pci_write_config_dword(pdev, pdev->tph_cap + PCI_TPH_CTRL, reg);
  126. }
  127. static u8 get_st_modes(struct pci_dev *pdev)
  128. {
  129. u32 reg;
  130. pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CAP, &reg);
  131. reg &= PCI_TPH_CAP_ST_NS | PCI_TPH_CAP_ST_IV | PCI_TPH_CAP_ST_DS;
  132. return reg;
  133. }
  134. /**
  135. * pcie_tph_get_st_table_loc - Return the device's ST table location
  136. * @pdev: PCI device to query
  137. *
  138. * Return:
  139. * PCI_TPH_LOC_NONE - Not present
  140. * PCI_TPH_LOC_CAP - Located in the TPH Requester Extended Capability
  141. * PCI_TPH_LOC_MSIX - Located in the MSI-X Table
  142. */
  143. u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev)
  144. {
  145. u32 reg;
  146. pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CAP, &reg);
  147. return FIELD_GET(PCI_TPH_CAP_LOC_MASK, reg);
  148. }
  149. EXPORT_SYMBOL(pcie_tph_get_st_table_loc);
  150. /*
  151. * Return the size of ST table. If ST table is not in TPH Requester Extended
  152. * Capability space, return 0. Otherwise return the ST Table Size + 1.
  153. */
  154. u16 pcie_tph_get_st_table_size(struct pci_dev *pdev)
  155. {
  156. u32 reg;
  157. u32 loc;
  158. /* Check ST table location first */
  159. loc = pcie_tph_get_st_table_loc(pdev);
  160. /* Convert loc to match with PCI_TPH_LOC_* defined in pci_regs.h */
  161. loc = FIELD_PREP(PCI_TPH_CAP_LOC_MASK, loc);
  162. if (loc != PCI_TPH_LOC_CAP)
  163. return 0;
  164. pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CAP, &reg);
  165. return FIELD_GET(PCI_TPH_CAP_ST_MASK, reg) + 1;
  166. }
  167. EXPORT_SYMBOL(pcie_tph_get_st_table_size);
  168. /* Return device's Root Port completer capability */
  169. static u8 get_rp_completer_type(struct pci_dev *pdev)
  170. {
  171. struct pci_dev *rp;
  172. u32 reg;
  173. int ret;
  174. rp = pcie_find_root_port(pdev);
  175. if (!rp)
  176. return 0;
  177. ret = pcie_capability_read_dword(rp, PCI_EXP_DEVCAP2, &reg);
  178. if (ret)
  179. return 0;
  180. return FIELD_GET(PCI_EXP_DEVCAP2_TPH_COMP_MASK, reg);
  181. }
  182. /* Write tag to ST table - Return 0 if OK, otherwise -errno */
  183. static int write_tag_to_st_table(struct pci_dev *pdev, int index, u16 tag)
  184. {
  185. int st_table_size;
  186. int offset;
  187. /* Check if index is out of bound */
  188. st_table_size = pcie_tph_get_st_table_size(pdev);
  189. if (index >= st_table_size)
  190. return -ENXIO;
  191. offset = pdev->tph_cap + PCI_TPH_BASE_SIZEOF + index * sizeof(u16);
  192. return pci_write_config_word(pdev, offset, tag);
  193. }
  194. /**
  195. * pcie_tph_get_cpu_st() - Retrieve Steering Tag for a target memory associated
  196. * with a specific CPU
  197. * @pdev: PCI device
  198. * @mem_type: target memory type (volatile or persistent RAM)
  199. * @cpu_uid: associated CPU id
  200. * @tag: Steering Tag to be returned
  201. *
  202. * Return the Steering Tag for a target memory that is associated with a
  203. * specific CPU as indicated by cpu_uid.
  204. *
  205. * Return: 0 if success, otherwise negative value (-errno)
  206. */
  207. int pcie_tph_get_cpu_st(struct pci_dev *pdev, enum tph_mem_type mem_type,
  208. unsigned int cpu_uid, u16 *tag)
  209. {
  210. #ifdef CONFIG_ACPI
  211. struct pci_dev *rp;
  212. acpi_handle rp_acpi_handle;
  213. union st_info info;
  214. rp = pcie_find_root_port(pdev);
  215. if (!rp || !rp->bus || !rp->bus->bridge)
  216. return -ENODEV;
  217. rp_acpi_handle = ACPI_HANDLE(rp->bus->bridge);
  218. if (tph_invoke_dsm(rp_acpi_handle, cpu_uid, &info) != AE_OK) {
  219. *tag = 0;
  220. return -EINVAL;
  221. }
  222. *tag = tph_extract_tag(mem_type, pdev->tph_req_type, &info);
  223. pci_dbg(pdev, "get steering tag: mem_type=%s, cpu_uid=%d, tag=%#04x\n",
  224. (mem_type == TPH_MEM_TYPE_VM) ? "volatile" : "persistent",
  225. cpu_uid, *tag);
  226. return 0;
  227. #else
  228. return -ENODEV;
  229. #endif
  230. }
  231. EXPORT_SYMBOL(pcie_tph_get_cpu_st);
  232. /**
  233. * pcie_tph_set_st_entry() - Set Steering Tag in the ST table entry
  234. * @pdev: PCI device
  235. * @index: ST table entry index
  236. * @tag: Steering Tag to be written
  237. *
  238. * Figure out the proper location of ST table, either in the MSI-X table or
  239. * in the TPH Extended Capability space, and write the Steering Tag into
  240. * the ST entry pointed by index.
  241. *
  242. * Return: 0 if success, otherwise negative value (-errno)
  243. */
  244. int pcie_tph_set_st_entry(struct pci_dev *pdev, unsigned int index, u16 tag)
  245. {
  246. u32 loc;
  247. int err = 0;
  248. if (!pdev->tph_cap)
  249. return -EINVAL;
  250. if (!pdev->tph_enabled)
  251. return -EINVAL;
  252. /* No need to write tag if device is in "No ST Mode" */
  253. if (pdev->tph_mode == PCI_TPH_ST_NS_MODE)
  254. return 0;
  255. /*
  256. * Disable TPH before updating ST to avoid potential instability as
  257. * cautioned in PCIe r6.2, sec 6.17.3, "ST Modes of Operation"
  258. */
  259. set_ctrl_reg_req_en(pdev, PCI_TPH_REQ_DISABLE);
  260. loc = pcie_tph_get_st_table_loc(pdev);
  261. /* Convert loc to match with PCI_TPH_LOC_* */
  262. loc = FIELD_PREP(PCI_TPH_CAP_LOC_MASK, loc);
  263. switch (loc) {
  264. case PCI_TPH_LOC_MSIX:
  265. err = pci_msix_write_tph_tag(pdev, index, tag);
  266. break;
  267. case PCI_TPH_LOC_CAP:
  268. err = write_tag_to_st_table(pdev, index, tag);
  269. break;
  270. default:
  271. err = -EINVAL;
  272. }
  273. if (err) {
  274. pcie_disable_tph(pdev);
  275. return err;
  276. }
  277. set_ctrl_reg_req_en(pdev, pdev->tph_req_type);
  278. pci_dbg(pdev, "set steering tag: %s table, index=%d, tag=%#04x\n",
  279. (loc == PCI_TPH_LOC_MSIX) ? "MSI-X" : "ST", index, tag);
  280. return 0;
  281. }
  282. EXPORT_SYMBOL(pcie_tph_set_st_entry);
  283. /**
  284. * pcie_disable_tph - Turn off TPH support for device
  285. * @pdev: PCI device
  286. *
  287. * Return: none
  288. */
  289. void pcie_disable_tph(struct pci_dev *pdev)
  290. {
  291. if (!pdev->tph_cap)
  292. return;
  293. if (!pdev->tph_enabled)
  294. return;
  295. pci_write_config_dword(pdev, pdev->tph_cap + PCI_TPH_CTRL, 0);
  296. pdev->tph_mode = 0;
  297. pdev->tph_req_type = 0;
  298. pdev->tph_enabled = 0;
  299. }
  300. EXPORT_SYMBOL(pcie_disable_tph);
  301. /**
  302. * pcie_enable_tph - Enable TPH support for device using a specific ST mode
  303. * @pdev: PCI device
  304. * @mode: ST mode to enable. Current supported modes include:
  305. *
  306. * - PCI_TPH_ST_NS_MODE: NO ST Mode
  307. * - PCI_TPH_ST_IV_MODE: Interrupt Vector Mode
  308. * - PCI_TPH_ST_DS_MODE: Device Specific Mode
  309. *
  310. * Check whether the mode is actually supported by the device before enabling
  311. * and return an error if not. Additionally determine what types of requests,
  312. * TPH or extended TPH, can be issued by the device based on its TPH requester
  313. * capability and the Root Port's completer capability.
  314. *
  315. * Return: 0 on success, otherwise negative value (-errno)
  316. */
  317. int pcie_enable_tph(struct pci_dev *pdev, int mode)
  318. {
  319. u32 reg;
  320. u8 dev_modes;
  321. u8 rp_req_type;
  322. /* Honor "notph" kernel parameter */
  323. if (pci_tph_disabled)
  324. return -EINVAL;
  325. if (!pdev->tph_cap)
  326. return -EINVAL;
  327. if (pdev->tph_enabled)
  328. return -EBUSY;
  329. /* Sanitize and check ST mode compatibility */
  330. mode &= PCI_TPH_CTRL_MODE_SEL_MASK;
  331. dev_modes = get_st_modes(pdev);
  332. if (!((1 << mode) & dev_modes))
  333. return -EINVAL;
  334. pdev->tph_mode = mode;
  335. /* Get req_type supported by device and its Root Port */
  336. pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CAP, &reg);
  337. if (FIELD_GET(PCI_TPH_CAP_EXT_TPH, reg))
  338. pdev->tph_req_type = PCI_TPH_REQ_EXT_TPH;
  339. else
  340. pdev->tph_req_type = PCI_TPH_REQ_TPH_ONLY;
  341. rp_req_type = get_rp_completer_type(pdev);
  342. /* Final req_type is the smallest value of two */
  343. pdev->tph_req_type = min(pdev->tph_req_type, rp_req_type);
  344. if (pdev->tph_req_type == PCI_TPH_REQ_DISABLE)
  345. return -EINVAL;
  346. /* Write them into TPH control register */
  347. pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CTRL, &reg);
  348. reg &= ~PCI_TPH_CTRL_MODE_SEL_MASK;
  349. reg |= FIELD_PREP(PCI_TPH_CTRL_MODE_SEL_MASK, pdev->tph_mode);
  350. reg &= ~PCI_TPH_CTRL_REQ_EN_MASK;
  351. reg |= FIELD_PREP(PCI_TPH_CTRL_REQ_EN_MASK, pdev->tph_req_type);
  352. pci_write_config_dword(pdev, pdev->tph_cap + PCI_TPH_CTRL, reg);
  353. pdev->tph_enabled = 1;
  354. return 0;
  355. }
  356. EXPORT_SYMBOL(pcie_enable_tph);
  357. void pci_restore_tph_state(struct pci_dev *pdev)
  358. {
  359. struct pci_cap_saved_state *save_state;
  360. int num_entries, i, offset;
  361. u16 *st_entry;
  362. u32 *cap;
  363. if (!pdev->tph_cap)
  364. return;
  365. if (!pdev->tph_enabled)
  366. return;
  367. save_state = pci_find_saved_ext_cap(pdev, PCI_EXT_CAP_ID_TPH);
  368. if (!save_state)
  369. return;
  370. /* Restore control register and all ST entries */
  371. cap = &save_state->cap.data[0];
  372. pci_write_config_dword(pdev, pdev->tph_cap + PCI_TPH_CTRL, *cap++);
  373. st_entry = (u16 *)cap;
  374. offset = PCI_TPH_BASE_SIZEOF;
  375. num_entries = pcie_tph_get_st_table_size(pdev);
  376. for (i = 0; i < num_entries; i++) {
  377. pci_write_config_word(pdev, pdev->tph_cap + offset,
  378. *st_entry++);
  379. offset += sizeof(u16);
  380. }
  381. }
  382. void pci_save_tph_state(struct pci_dev *pdev)
  383. {
  384. struct pci_cap_saved_state *save_state;
  385. int num_entries, i, offset;
  386. u16 *st_entry;
  387. u32 *cap;
  388. if (!pdev->tph_cap)
  389. return;
  390. if (!pdev->tph_enabled)
  391. return;
  392. save_state = pci_find_saved_ext_cap(pdev, PCI_EXT_CAP_ID_TPH);
  393. if (!save_state)
  394. return;
  395. /* Save control register */
  396. cap = &save_state->cap.data[0];
  397. pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CTRL, cap++);
  398. /* Save all ST entries in extended capability structure */
  399. st_entry = (u16 *)cap;
  400. offset = PCI_TPH_BASE_SIZEOF;
  401. num_entries = pcie_tph_get_st_table_size(pdev);
  402. for (i = 0; i < num_entries; i++) {
  403. pci_read_config_word(pdev, pdev->tph_cap + offset,
  404. st_entry++);
  405. offset += sizeof(u16);
  406. }
  407. }
  408. void pci_no_tph(void)
  409. {
  410. pci_tph_disabled = true;
  411. pr_info("PCIe TPH is disabled\n");
  412. }
  413. void pci_tph_init(struct pci_dev *pdev)
  414. {
  415. int num_entries;
  416. u32 save_size;
  417. pdev->tph_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_TPH);
  418. if (!pdev->tph_cap)
  419. return;
  420. num_entries = pcie_tph_get_st_table_size(pdev);
  421. save_size = sizeof(u32) + num_entries * sizeof(u16);
  422. pci_add_ext_cap_save_buffer(pdev, PCI_EXT_CAP_ID_TPH, save_size);
  423. }