ccp-debugfs.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Cryptographic Coprocessor (CCP) driver
  4. *
  5. * Copyright (C) 2017 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Gary R Hook <gary.hook@amd.com>
  8. */
  9. #include <linux/debugfs.h>
  10. #include <linux/ccp.h>
  11. #include "ccp-dev.h"
  12. /* DebugFS helpers */
  13. #define OBUFP (obuf + oboff)
  14. #define OBUFLEN 512
  15. #define OBUFSPC (OBUFLEN - oboff)
  16. #define OSCNPRINTF(fmt, ...) \
  17. scnprintf(OBUFP, OBUFSPC, fmt, ## __VA_ARGS__)
  18. #define BUFLEN 63
  19. #define RI_VERSION_NUM 0x0000003F
  20. #define RI_AES_PRESENT 0x00000040
  21. #define RI_3DES_PRESENT 0x00000080
  22. #define RI_SHA_PRESENT 0x00000100
  23. #define RI_RSA_PRESENT 0x00000200
  24. #define RI_ECC_PRESENT 0x00000400
  25. #define RI_ZDE_PRESENT 0x00000800
  26. #define RI_ZCE_PRESENT 0x00001000
  27. #define RI_TRNG_PRESENT 0x00002000
  28. #define RI_ELFC_PRESENT 0x00004000
  29. #define RI_ELFC_SHIFT 14
  30. #define RI_NUM_VQM 0x00078000
  31. #define RI_NVQM_SHIFT 15
  32. #define RI_NVQM(r) (((r) * RI_NUM_VQM) >> RI_NVQM_SHIFT)
  33. #define RI_LSB_ENTRIES 0x0FF80000
  34. #define RI_NLSB_SHIFT 19
  35. #define RI_NLSB(r) (((r) * RI_LSB_ENTRIES) >> RI_NLSB_SHIFT)
  36. static ssize_t ccp5_debugfs_info_read(struct file *filp, char __user *ubuf,
  37. size_t count, loff_t *offp)
  38. {
  39. struct ccp_device *ccp = filp->private_data;
  40. unsigned int oboff = 0;
  41. unsigned int regval;
  42. ssize_t ret;
  43. char *obuf;
  44. if (!ccp)
  45. return 0;
  46. obuf = kmalloc(OBUFLEN, GFP_KERNEL);
  47. if (!obuf)
  48. return -ENOMEM;
  49. oboff += OSCNPRINTF("Device name: %s\n", ccp->name);
  50. oboff += OSCNPRINTF(" RNG name: %s\n", ccp->rngname);
  51. oboff += OSCNPRINTF(" # Queues: %d\n", ccp->cmd_q_count);
  52. oboff += OSCNPRINTF(" # Cmds: %d\n", ccp->cmd_count);
  53. regval = ioread32(ccp->io_regs + CMD5_PSP_CCP_VERSION);
  54. oboff += OSCNPRINTF(" Version: %d\n", regval & RI_VERSION_NUM);
  55. oboff += OSCNPRINTF(" Engines:");
  56. if (regval & RI_AES_PRESENT)
  57. oboff += OSCNPRINTF(" AES");
  58. if (regval & RI_3DES_PRESENT)
  59. oboff += OSCNPRINTF(" 3DES");
  60. if (regval & RI_SHA_PRESENT)
  61. oboff += OSCNPRINTF(" SHA");
  62. if (regval & RI_RSA_PRESENT)
  63. oboff += OSCNPRINTF(" RSA");
  64. if (regval & RI_ECC_PRESENT)
  65. oboff += OSCNPRINTF(" ECC");
  66. if (regval & RI_ZDE_PRESENT)
  67. oboff += OSCNPRINTF(" ZDE");
  68. if (regval & RI_ZCE_PRESENT)
  69. oboff += OSCNPRINTF(" ZCE");
  70. if (regval & RI_TRNG_PRESENT)
  71. oboff += OSCNPRINTF(" TRNG");
  72. oboff += OSCNPRINTF("\n");
  73. oboff += OSCNPRINTF(" Queues: %d\n",
  74. (regval & RI_NUM_VQM) >> RI_NVQM_SHIFT);
  75. oboff += OSCNPRINTF("LSB Entries: %d\n",
  76. (regval & RI_LSB_ENTRIES) >> RI_NLSB_SHIFT);
  77. ret = simple_read_from_buffer(ubuf, count, offp, obuf, oboff);
  78. kfree(obuf);
  79. return ret;
  80. }
  81. /* Return a formatted buffer containing the current
  82. * statistics across all queues for a CCP.
  83. */
  84. static ssize_t ccp5_debugfs_stats_read(struct file *filp, char __user *ubuf,
  85. size_t count, loff_t *offp)
  86. {
  87. struct ccp_device *ccp = filp->private_data;
  88. unsigned long total_xts_aes_ops = 0;
  89. unsigned long total_3des_ops = 0;
  90. unsigned long total_aes_ops = 0;
  91. unsigned long total_sha_ops = 0;
  92. unsigned long total_rsa_ops = 0;
  93. unsigned long total_ecc_ops = 0;
  94. unsigned long total_pt_ops = 0;
  95. unsigned long total_ops = 0;
  96. unsigned int oboff = 0;
  97. ssize_t ret = 0;
  98. unsigned int i;
  99. char *obuf;
  100. for (i = 0; i < ccp->cmd_q_count; i++) {
  101. struct ccp_cmd_queue *cmd_q = &ccp->cmd_q[i];
  102. total_ops += cmd_q->total_ops;
  103. total_aes_ops += cmd_q->total_aes_ops;
  104. total_xts_aes_ops += cmd_q->total_xts_aes_ops;
  105. total_3des_ops += cmd_q->total_3des_ops;
  106. total_sha_ops += cmd_q->total_sha_ops;
  107. total_rsa_ops += cmd_q->total_rsa_ops;
  108. total_pt_ops += cmd_q->total_pt_ops;
  109. total_ecc_ops += cmd_q->total_ecc_ops;
  110. }
  111. obuf = kmalloc(OBUFLEN, GFP_KERNEL);
  112. if (!obuf)
  113. return -ENOMEM;
  114. oboff += OSCNPRINTF("Total Interrupts Handled: %ld\n",
  115. ccp->total_interrupts);
  116. oboff += OSCNPRINTF(" Total Operations: %ld\n",
  117. total_ops);
  118. oboff += OSCNPRINTF(" AES: %ld\n",
  119. total_aes_ops);
  120. oboff += OSCNPRINTF(" XTS AES: %ld\n",
  121. total_xts_aes_ops);
  122. oboff += OSCNPRINTF(" SHA: %ld\n",
  123. total_3des_ops);
  124. oboff += OSCNPRINTF(" SHA: %ld\n",
  125. total_sha_ops);
  126. oboff += OSCNPRINTF(" RSA: %ld\n",
  127. total_rsa_ops);
  128. oboff += OSCNPRINTF(" Pass-Thru: %ld\n",
  129. total_pt_ops);
  130. oboff += OSCNPRINTF(" ECC: %ld\n",
  131. total_ecc_ops);
  132. ret = simple_read_from_buffer(ubuf, count, offp, obuf, oboff);
  133. kfree(obuf);
  134. return ret;
  135. }
  136. /* Reset the counters in a queue
  137. */
  138. static void ccp5_debugfs_reset_queue_stats(struct ccp_cmd_queue *cmd_q)
  139. {
  140. cmd_q->total_ops = 0L;
  141. cmd_q->total_aes_ops = 0L;
  142. cmd_q->total_xts_aes_ops = 0L;
  143. cmd_q->total_3des_ops = 0L;
  144. cmd_q->total_sha_ops = 0L;
  145. cmd_q->total_rsa_ops = 0L;
  146. cmd_q->total_pt_ops = 0L;
  147. cmd_q->total_ecc_ops = 0L;
  148. }
  149. /* A value was written to the stats variable, which
  150. * should be used to reset the queue counters across
  151. * that device.
  152. */
  153. static ssize_t ccp5_debugfs_stats_write(struct file *filp,
  154. const char __user *ubuf,
  155. size_t count, loff_t *offp)
  156. {
  157. struct ccp_device *ccp = filp->private_data;
  158. int i;
  159. for (i = 0; i < ccp->cmd_q_count; i++)
  160. ccp5_debugfs_reset_queue_stats(&ccp->cmd_q[i]);
  161. ccp->total_interrupts = 0L;
  162. return count;
  163. }
  164. /* Return a formatted buffer containing the current information
  165. * for that queue
  166. */
  167. static ssize_t ccp5_debugfs_queue_read(struct file *filp, char __user *ubuf,
  168. size_t count, loff_t *offp)
  169. {
  170. struct ccp_cmd_queue *cmd_q = filp->private_data;
  171. unsigned int oboff = 0;
  172. unsigned int regval;
  173. ssize_t ret;
  174. char *obuf;
  175. if (!cmd_q)
  176. return 0;
  177. obuf = kmalloc(OBUFLEN, GFP_KERNEL);
  178. if (!obuf)
  179. return -ENOMEM;
  180. oboff += OSCNPRINTF(" Total Queue Operations: %ld\n",
  181. cmd_q->total_ops);
  182. oboff += OSCNPRINTF(" AES: %ld\n",
  183. cmd_q->total_aes_ops);
  184. oboff += OSCNPRINTF(" XTS AES: %ld\n",
  185. cmd_q->total_xts_aes_ops);
  186. oboff += OSCNPRINTF(" SHA: %ld\n",
  187. cmd_q->total_3des_ops);
  188. oboff += OSCNPRINTF(" SHA: %ld\n",
  189. cmd_q->total_sha_ops);
  190. oboff += OSCNPRINTF(" RSA: %ld\n",
  191. cmd_q->total_rsa_ops);
  192. oboff += OSCNPRINTF(" Pass-Thru: %ld\n",
  193. cmd_q->total_pt_ops);
  194. oboff += OSCNPRINTF(" ECC: %ld\n",
  195. cmd_q->total_ecc_ops);
  196. regval = ioread32(cmd_q->reg_int_enable);
  197. oboff += OSCNPRINTF(" Enabled Interrupts:");
  198. if (regval & INT_EMPTY_QUEUE)
  199. oboff += OSCNPRINTF(" EMPTY");
  200. if (regval & INT_QUEUE_STOPPED)
  201. oboff += OSCNPRINTF(" STOPPED");
  202. if (regval & INT_ERROR)
  203. oboff += OSCNPRINTF(" ERROR");
  204. if (regval & INT_COMPLETION)
  205. oboff += OSCNPRINTF(" COMPLETION");
  206. oboff += OSCNPRINTF("\n");
  207. ret = simple_read_from_buffer(ubuf, count, offp, obuf, oboff);
  208. kfree(obuf);
  209. return ret;
  210. }
  211. /* A value was written to the stats variable for a
  212. * queue. Reset the queue counters to this value.
  213. */
  214. static ssize_t ccp5_debugfs_queue_write(struct file *filp,
  215. const char __user *ubuf,
  216. size_t count, loff_t *offp)
  217. {
  218. struct ccp_cmd_queue *cmd_q = filp->private_data;
  219. ccp5_debugfs_reset_queue_stats(cmd_q);
  220. return count;
  221. }
  222. static const struct file_operations ccp_debugfs_info_ops = {
  223. .owner = THIS_MODULE,
  224. .open = simple_open,
  225. .read = ccp5_debugfs_info_read,
  226. .write = NULL,
  227. };
  228. static const struct file_operations ccp_debugfs_queue_ops = {
  229. .owner = THIS_MODULE,
  230. .open = simple_open,
  231. .read = ccp5_debugfs_queue_read,
  232. .write = ccp5_debugfs_queue_write,
  233. };
  234. static const struct file_operations ccp_debugfs_stats_ops = {
  235. .owner = THIS_MODULE,
  236. .open = simple_open,
  237. .read = ccp5_debugfs_stats_read,
  238. .write = ccp5_debugfs_stats_write,
  239. };
  240. static struct dentry *ccp_debugfs_dir;
  241. static DEFINE_MUTEX(ccp_debugfs_lock);
  242. #define MAX_NAME_LEN 20
  243. void ccp5_debugfs_setup(struct ccp_device *ccp)
  244. {
  245. struct ccp_cmd_queue *cmd_q;
  246. char name[MAX_NAME_LEN + 1];
  247. struct dentry *debugfs_q_instance;
  248. int i;
  249. if (!debugfs_initialized())
  250. return;
  251. mutex_lock(&ccp_debugfs_lock);
  252. if (!ccp_debugfs_dir)
  253. ccp_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  254. mutex_unlock(&ccp_debugfs_lock);
  255. ccp->debugfs_instance = debugfs_create_dir(ccp->name, ccp_debugfs_dir);
  256. debugfs_create_file("info", 0400, ccp->debugfs_instance, ccp,
  257. &ccp_debugfs_info_ops);
  258. debugfs_create_file("stats", 0600, ccp->debugfs_instance, ccp,
  259. &ccp_debugfs_stats_ops);
  260. for (i = 0; i < ccp->cmd_q_count; i++) {
  261. cmd_q = &ccp->cmd_q[i];
  262. snprintf(name, MAX_NAME_LEN - 1, "q%d", cmd_q->id);
  263. debugfs_q_instance =
  264. debugfs_create_dir(name, ccp->debugfs_instance);
  265. debugfs_create_file("stats", 0600, debugfs_q_instance, cmd_q,
  266. &ccp_debugfs_queue_ops);
  267. }
  268. return;
  269. }
  270. void ccp5_debugfs_destroy(void)
  271. {
  272. debugfs_remove_recursive(ccp_debugfs_dir);
  273. }