ccp-debugfs.c 9.2 KB

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