hsmp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AMD HSMP Platform Driver
  4. * Copyright (c) 2022, AMD.
  5. * All Rights Reserved.
  6. *
  7. * This file provides a device implementation for HSMP interface
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <asm/amd_hsmp.h>
  11. #include <asm/amd_nb.h>
  12. #include <linux/delay.h>
  13. #include <linux/io.h>
  14. #include <linux/miscdevice.h>
  15. #include <linux/module.h>
  16. #include <linux/pci.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/semaphore.h>
  19. #define DRIVER_NAME "amd_hsmp"
  20. #define DRIVER_VERSION "1.0"
  21. /* HSMP Status / Error codes */
  22. #define HSMP_STATUS_NOT_READY 0x00
  23. #define HSMP_STATUS_OK 0x01
  24. #define HSMP_ERR_INVALID_MSG 0xFE
  25. #define HSMP_ERR_INVALID_INPUT 0xFF
  26. /* Timeout in millsec */
  27. #define HSMP_MSG_TIMEOUT 100
  28. #define HSMP_SHORT_SLEEP 1
  29. #define HSMP_WR true
  30. #define HSMP_RD false
  31. /*
  32. * To access specific HSMP mailbox register, s/w writes the SMN address of HSMP mailbox
  33. * register into the SMN_INDEX register, and reads/writes the SMN_DATA reg.
  34. * Below are required SMN address for HSMP Mailbox register offsets in SMU address space
  35. */
  36. #define SMN_HSMP_MSG_ID 0x3B10534
  37. #define SMN_HSMP_MSG_RESP 0x3B10980
  38. #define SMN_HSMP_MSG_DATA 0x3B109E0
  39. #define HSMP_INDEX_REG 0xc4
  40. #define HSMP_DATA_REG 0xc8
  41. static struct semaphore *hsmp_sem;
  42. static struct miscdevice hsmp_device;
  43. static int amd_hsmp_rdwr(struct pci_dev *root, u32 address,
  44. u32 *value, bool write)
  45. {
  46. int ret;
  47. ret = pci_write_config_dword(root, HSMP_INDEX_REG, address);
  48. if (ret)
  49. return ret;
  50. ret = (write ? pci_write_config_dword(root, HSMP_DATA_REG, *value)
  51. : pci_read_config_dword(root, HSMP_DATA_REG, value));
  52. return ret;
  53. }
  54. /*
  55. * Send a message to the HSMP port via PCI-e config space registers.
  56. *
  57. * The caller is expected to zero out any unused arguments.
  58. * If a response is expected, the number of response words should be greater than 0.
  59. *
  60. * Returns 0 for success and populates the requested number of arguments.
  61. * Returns a negative error code for failure.
  62. */
  63. static int __hsmp_send_message(struct pci_dev *root, struct hsmp_message *msg)
  64. {
  65. unsigned long timeout, short_sleep;
  66. u32 mbox_status;
  67. u32 index;
  68. int ret;
  69. /* Clear the status register */
  70. mbox_status = HSMP_STATUS_NOT_READY;
  71. ret = amd_hsmp_rdwr(root, SMN_HSMP_MSG_RESP, &mbox_status, HSMP_WR);
  72. if (ret) {
  73. pr_err("Error %d clearing mailbox status register\n", ret);
  74. return ret;
  75. }
  76. index = 0;
  77. /* Write any message arguments */
  78. while (index < msg->num_args) {
  79. ret = amd_hsmp_rdwr(root, SMN_HSMP_MSG_DATA + (index << 2),
  80. &msg->args[index], HSMP_WR);
  81. if (ret) {
  82. pr_err("Error %d writing message argument %d\n", ret, index);
  83. return ret;
  84. }
  85. index++;
  86. }
  87. /* Write the message ID which starts the operation */
  88. ret = amd_hsmp_rdwr(root, SMN_HSMP_MSG_ID, &msg->msg_id, HSMP_WR);
  89. if (ret) {
  90. pr_err("Error %d writing message ID %u\n", ret, msg->msg_id);
  91. return ret;
  92. }
  93. /*
  94. * Depending on when the trigger write completes relative to the SMU
  95. * firmware 1 ms cycle, the operation may take from tens of us to 1 ms
  96. * to complete. Some operations may take more. Therefore we will try
  97. * a few short duration sleeps and switch to long sleeps if we don't
  98. * succeed quickly.
  99. */
  100. short_sleep = jiffies + msecs_to_jiffies(HSMP_SHORT_SLEEP);
  101. timeout = jiffies + msecs_to_jiffies(HSMP_MSG_TIMEOUT);
  102. while (time_before(jiffies, timeout)) {
  103. ret = amd_hsmp_rdwr(root, SMN_HSMP_MSG_RESP, &mbox_status, HSMP_RD);
  104. if (ret) {
  105. pr_err("Error %d reading mailbox status\n", ret);
  106. return ret;
  107. }
  108. if (mbox_status != HSMP_STATUS_NOT_READY)
  109. break;
  110. if (time_before(jiffies, short_sleep))
  111. usleep_range(50, 100);
  112. else
  113. usleep_range(1000, 2000);
  114. }
  115. if (unlikely(mbox_status == HSMP_STATUS_NOT_READY)) {
  116. return -ETIMEDOUT;
  117. } else if (unlikely(mbox_status == HSMP_ERR_INVALID_MSG)) {
  118. return -ENOMSG;
  119. } else if (unlikely(mbox_status == HSMP_ERR_INVALID_INPUT)) {
  120. return -EINVAL;
  121. } else if (unlikely(mbox_status != HSMP_STATUS_OK)) {
  122. pr_err("Message ID %u unknown failure (status = 0x%X)\n",
  123. msg->msg_id, mbox_status);
  124. return -EIO;
  125. }
  126. /*
  127. * SMU has responded OK. Read response data.
  128. * SMU reads the input arguments from eight 32 bit registers starting
  129. * from SMN_HSMP_MSG_DATA and writes the response data to the same
  130. * SMN_HSMP_MSG_DATA address.
  131. * We copy the response data if any, back to the args[].
  132. */
  133. index = 0;
  134. while (index < msg->response_sz) {
  135. ret = amd_hsmp_rdwr(root, SMN_HSMP_MSG_DATA + (index << 2),
  136. &msg->args[index], HSMP_RD);
  137. if (ret) {
  138. pr_err("Error %d reading response %u for message ID:%u\n",
  139. ret, index, msg->msg_id);
  140. break;
  141. }
  142. index++;
  143. }
  144. return ret;
  145. }
  146. static int validate_message(struct hsmp_message *msg)
  147. {
  148. /* msg_id against valid range of message IDs */
  149. if (msg->msg_id < HSMP_TEST || msg->msg_id >= HSMP_MSG_ID_MAX)
  150. return -ENOMSG;
  151. /* msg_id is a reserved message ID */
  152. if (hsmp_msg_desc_table[msg->msg_id].type == HSMP_RSVD)
  153. return -ENOMSG;
  154. /* num_args and response_sz against the HSMP spec */
  155. if (msg->num_args != hsmp_msg_desc_table[msg->msg_id].num_args ||
  156. msg->response_sz != hsmp_msg_desc_table[msg->msg_id].response_sz)
  157. return -EINVAL;
  158. return 0;
  159. }
  160. int hsmp_send_message(struct hsmp_message *msg)
  161. {
  162. struct amd_northbridge *nb;
  163. int ret;
  164. if (!msg)
  165. return -EINVAL;
  166. nb = node_to_amd_nb(msg->sock_ind);
  167. if (!nb || !nb->root)
  168. return -ENODEV;
  169. ret = validate_message(msg);
  170. if (ret)
  171. return ret;
  172. /*
  173. * The time taken by smu operation to complete is between
  174. * 10us to 1ms. Sometime it may take more time.
  175. * In SMP system timeout of 100 millisecs should
  176. * be enough for the previous thread to finish the operation
  177. */
  178. ret = down_timeout(&hsmp_sem[msg->sock_ind],
  179. msecs_to_jiffies(HSMP_MSG_TIMEOUT));
  180. if (ret < 0)
  181. return ret;
  182. ret = __hsmp_send_message(nb->root, msg);
  183. up(&hsmp_sem[msg->sock_ind]);
  184. return ret;
  185. }
  186. EXPORT_SYMBOL_GPL(hsmp_send_message);
  187. static int hsmp_test(u16 sock_ind, u32 value)
  188. {
  189. struct hsmp_message msg = { 0 };
  190. struct amd_northbridge *nb;
  191. int ret = -ENODEV;
  192. nb = node_to_amd_nb(sock_ind);
  193. if (!nb || !nb->root)
  194. return ret;
  195. /*
  196. * Test the hsmp port by performing TEST command. The test message
  197. * takes one argument and returns the value of that argument + 1.
  198. */
  199. msg.msg_id = HSMP_TEST;
  200. msg.num_args = 1;
  201. msg.response_sz = 1;
  202. msg.args[0] = value;
  203. msg.sock_ind = sock_ind;
  204. ret = __hsmp_send_message(nb->root, &msg);
  205. if (ret)
  206. return ret;
  207. /* Check the response value */
  208. if (msg.args[0] != (value + 1)) {
  209. pr_err("Socket %d test message failed, Expected 0x%08X, received 0x%08X\n",
  210. sock_ind, (value + 1), msg.args[0]);
  211. return -EBADE;
  212. }
  213. return ret;
  214. }
  215. static long hsmp_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
  216. {
  217. int __user *arguser = (int __user *)arg;
  218. struct hsmp_message msg = { 0 };
  219. int ret;
  220. if (copy_struct_from_user(&msg, sizeof(msg), arguser, sizeof(struct hsmp_message)))
  221. return -EFAULT;
  222. /*
  223. * Check msg_id is within the range of supported msg ids
  224. * i.e within the array bounds of hsmp_msg_desc_table
  225. */
  226. if (msg.msg_id < HSMP_TEST || msg.msg_id >= HSMP_MSG_ID_MAX)
  227. return -ENOMSG;
  228. switch (fp->f_mode & (FMODE_WRITE | FMODE_READ)) {
  229. case FMODE_WRITE:
  230. /*
  231. * Device is opened in O_WRONLY mode
  232. * Execute only set/configure commands
  233. */
  234. if (hsmp_msg_desc_table[msg.msg_id].type != HSMP_SET)
  235. return -EINVAL;
  236. break;
  237. case FMODE_READ:
  238. /*
  239. * Device is opened in O_RDONLY mode
  240. * Execute only get/monitor commands
  241. */
  242. if (hsmp_msg_desc_table[msg.msg_id].type != HSMP_GET)
  243. return -EINVAL;
  244. break;
  245. case FMODE_READ | FMODE_WRITE:
  246. /*
  247. * Device is opened in O_RDWR mode
  248. * Execute both get/monitor and set/configure commands
  249. */
  250. break;
  251. default:
  252. return -EINVAL;
  253. }
  254. ret = hsmp_send_message(&msg);
  255. if (ret)
  256. return ret;
  257. if (hsmp_msg_desc_table[msg.msg_id].response_sz > 0) {
  258. /* Copy results back to user for get/monitor commands */
  259. if (copy_to_user(arguser, &msg, sizeof(struct hsmp_message)))
  260. return -EFAULT;
  261. }
  262. return 0;
  263. }
  264. static const struct file_operations hsmp_fops = {
  265. .owner = THIS_MODULE,
  266. .unlocked_ioctl = hsmp_ioctl,
  267. .compat_ioctl = hsmp_ioctl,
  268. };
  269. static int hsmp_pltdrv_probe(struct platform_device *pdev)
  270. {
  271. int i;
  272. hsmp_sem = devm_kzalloc(&pdev->dev,
  273. (amd_nb_num() * sizeof(struct semaphore)),
  274. GFP_KERNEL);
  275. if (!hsmp_sem)
  276. return -ENOMEM;
  277. for (i = 0; i < amd_nb_num(); i++)
  278. sema_init(&hsmp_sem[i], 1);
  279. hsmp_device.name = "hsmp_cdev";
  280. hsmp_device.minor = MISC_DYNAMIC_MINOR;
  281. hsmp_device.fops = &hsmp_fops;
  282. hsmp_device.parent = &pdev->dev;
  283. hsmp_device.nodename = "hsmp";
  284. hsmp_device.mode = 0644;
  285. return misc_register(&hsmp_device);
  286. }
  287. static int hsmp_pltdrv_remove(struct platform_device *pdev)
  288. {
  289. misc_deregister(&hsmp_device);
  290. return 0;
  291. }
  292. static struct platform_driver amd_hsmp_driver = {
  293. .probe = hsmp_pltdrv_probe,
  294. .remove = hsmp_pltdrv_remove,
  295. .driver = {
  296. .name = DRIVER_NAME,
  297. },
  298. };
  299. static struct platform_device *amd_hsmp_platdev;
  300. static int __init hsmp_plt_init(void)
  301. {
  302. int ret = -ENODEV;
  303. u16 num_sockets;
  304. int i;
  305. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD || boot_cpu_data.x86 < 0x19) {
  306. pr_err("HSMP is not supported on Family:%x model:%x\n",
  307. boot_cpu_data.x86, boot_cpu_data.x86_model);
  308. return ret;
  309. }
  310. /*
  311. * amd_nb_num() returns number of SMN/DF interfaces present in the system
  312. * if we have N SMN/DF interfaces that ideally means N sockets
  313. */
  314. num_sockets = amd_nb_num();
  315. if (num_sockets == 0)
  316. return ret;
  317. /* Test the hsmp interface on each socket */
  318. for (i = 0; i < num_sockets; i++) {
  319. ret = hsmp_test(i, 0xDEADBEEF);
  320. if (ret) {
  321. pr_err("HSMP is not supported on Fam:%x model:%x\n",
  322. boot_cpu_data.x86, boot_cpu_data.x86_model);
  323. pr_err("Or Is HSMP disabled in BIOS ?\n");
  324. return -EOPNOTSUPP;
  325. }
  326. }
  327. ret = platform_driver_register(&amd_hsmp_driver);
  328. if (ret)
  329. return ret;
  330. amd_hsmp_platdev = platform_device_alloc(DRIVER_NAME, PLATFORM_DEVID_NONE);
  331. if (!amd_hsmp_platdev) {
  332. ret = -ENOMEM;
  333. goto drv_unregister;
  334. }
  335. ret = platform_device_add(amd_hsmp_platdev);
  336. if (ret) {
  337. platform_device_put(amd_hsmp_platdev);
  338. goto drv_unregister;
  339. }
  340. return 0;
  341. drv_unregister:
  342. platform_driver_unregister(&amd_hsmp_driver);
  343. return ret;
  344. }
  345. static void __exit hsmp_plt_exit(void)
  346. {
  347. platform_device_unregister(amd_hsmp_platdev);
  348. platform_driver_unregister(&amd_hsmp_driver);
  349. }
  350. device_initcall(hsmp_plt_init);
  351. module_exit(hsmp_plt_exit);
  352. MODULE_DESCRIPTION("AMD HSMP Platform Interface Driver");
  353. MODULE_VERSION(DRIVER_VERSION);
  354. MODULE_LICENSE("GPL v2");