ccp-dev-v3.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) driver
  3. *
  4. * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Tom Lendacky <thomas.lendacky@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/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/pci.h>
  15. #include <linux/kthread.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/ccp.h>
  18. #include "ccp-dev.h"
  19. static int ccp_do_cmd(struct ccp_op *op, u32 *cr, unsigned int cr_count)
  20. {
  21. struct ccp_cmd_queue *cmd_q = op->cmd_q;
  22. struct ccp_device *ccp = cmd_q->ccp;
  23. void __iomem *cr_addr;
  24. u32 cr0, cmd;
  25. unsigned int i;
  26. int ret = 0;
  27. /* We could read a status register to see how many free slots
  28. * are actually available, but reading that register resets it
  29. * and you could lose some error information.
  30. */
  31. cmd_q->free_slots--;
  32. cr0 = (cmd_q->id << REQ0_CMD_Q_SHIFT)
  33. | (op->jobid << REQ0_JOBID_SHIFT)
  34. | REQ0_WAIT_FOR_WRITE;
  35. if (op->soc)
  36. cr0 |= REQ0_STOP_ON_COMPLETE
  37. | REQ0_INT_ON_COMPLETE;
  38. if (op->ioc || !cmd_q->free_slots)
  39. cr0 |= REQ0_INT_ON_COMPLETE;
  40. /* Start at CMD_REQ1 */
  41. cr_addr = ccp->io_regs + CMD_REQ0 + CMD_REQ_INCR;
  42. mutex_lock(&ccp->req_mutex);
  43. /* Write CMD_REQ1 through CMD_REQx first */
  44. for (i = 0; i < cr_count; i++, cr_addr += CMD_REQ_INCR)
  45. iowrite32(*(cr + i), cr_addr);
  46. /* Tell the CCP to start */
  47. wmb();
  48. iowrite32(cr0, ccp->io_regs + CMD_REQ0);
  49. mutex_unlock(&ccp->req_mutex);
  50. if (cr0 & REQ0_INT_ON_COMPLETE) {
  51. /* Wait for the job to complete */
  52. ret = wait_event_interruptible(cmd_q->int_queue,
  53. cmd_q->int_rcvd);
  54. if (ret || cmd_q->cmd_error) {
  55. /* On error delete all related jobs from the queue */
  56. cmd = (cmd_q->id << DEL_Q_ID_SHIFT)
  57. | op->jobid;
  58. iowrite32(cmd, ccp->io_regs + DEL_CMD_Q_JOB);
  59. if (!ret)
  60. ret = -EIO;
  61. } else if (op->soc) {
  62. /* Delete just head job from the queue on SoC */
  63. cmd = DEL_Q_ACTIVE
  64. | (cmd_q->id << DEL_Q_ID_SHIFT)
  65. | op->jobid;
  66. iowrite32(cmd, ccp->io_regs + DEL_CMD_Q_JOB);
  67. }
  68. cmd_q->free_slots = CMD_Q_DEPTH(cmd_q->q_status);
  69. cmd_q->int_rcvd = 0;
  70. }
  71. return ret;
  72. }
  73. static int ccp_perform_aes(struct ccp_op *op)
  74. {
  75. u32 cr[6];
  76. /* Fill out the register contents for REQ1 through REQ6 */
  77. cr[0] = (CCP_ENGINE_AES << REQ1_ENGINE_SHIFT)
  78. | (op->u.aes.type << REQ1_AES_TYPE_SHIFT)
  79. | (op->u.aes.mode << REQ1_AES_MODE_SHIFT)
  80. | (op->u.aes.action << REQ1_AES_ACTION_SHIFT)
  81. | (op->ksb_key << REQ1_KEY_KSB_SHIFT);
  82. cr[1] = op->src.u.dma.length - 1;
  83. cr[2] = ccp_addr_lo(&op->src.u.dma);
  84. cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT)
  85. | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
  86. | ccp_addr_hi(&op->src.u.dma);
  87. cr[4] = ccp_addr_lo(&op->dst.u.dma);
  88. cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
  89. | ccp_addr_hi(&op->dst.u.dma);
  90. if (op->u.aes.mode == CCP_AES_MODE_CFB)
  91. cr[0] |= ((0x7f) << REQ1_AES_CFB_SIZE_SHIFT);
  92. if (op->eom)
  93. cr[0] |= REQ1_EOM;
  94. if (op->init)
  95. cr[0] |= REQ1_INIT;
  96. return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
  97. }
  98. static int ccp_perform_xts_aes(struct ccp_op *op)
  99. {
  100. u32 cr[6];
  101. /* Fill out the register contents for REQ1 through REQ6 */
  102. cr[0] = (CCP_ENGINE_XTS_AES_128 << REQ1_ENGINE_SHIFT)
  103. | (op->u.xts.action << REQ1_AES_ACTION_SHIFT)
  104. | (op->u.xts.unit_size << REQ1_XTS_AES_SIZE_SHIFT)
  105. | (op->ksb_key << REQ1_KEY_KSB_SHIFT);
  106. cr[1] = op->src.u.dma.length - 1;
  107. cr[2] = ccp_addr_lo(&op->src.u.dma);
  108. cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT)
  109. | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
  110. | ccp_addr_hi(&op->src.u.dma);
  111. cr[4] = ccp_addr_lo(&op->dst.u.dma);
  112. cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
  113. | ccp_addr_hi(&op->dst.u.dma);
  114. if (op->eom)
  115. cr[0] |= REQ1_EOM;
  116. if (op->init)
  117. cr[0] |= REQ1_INIT;
  118. return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
  119. }
  120. static int ccp_perform_sha(struct ccp_op *op)
  121. {
  122. u32 cr[6];
  123. /* Fill out the register contents for REQ1 through REQ6 */
  124. cr[0] = (CCP_ENGINE_SHA << REQ1_ENGINE_SHIFT)
  125. | (op->u.sha.type << REQ1_SHA_TYPE_SHIFT)
  126. | REQ1_INIT;
  127. cr[1] = op->src.u.dma.length - 1;
  128. cr[2] = ccp_addr_lo(&op->src.u.dma);
  129. cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT)
  130. | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
  131. | ccp_addr_hi(&op->src.u.dma);
  132. if (op->eom) {
  133. cr[0] |= REQ1_EOM;
  134. cr[4] = lower_32_bits(op->u.sha.msg_bits);
  135. cr[5] = upper_32_bits(op->u.sha.msg_bits);
  136. } else {
  137. cr[4] = 0;
  138. cr[5] = 0;
  139. }
  140. return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
  141. }
  142. static int ccp_perform_rsa(struct ccp_op *op)
  143. {
  144. u32 cr[6];
  145. /* Fill out the register contents for REQ1 through REQ6 */
  146. cr[0] = (CCP_ENGINE_RSA << REQ1_ENGINE_SHIFT)
  147. | (op->u.rsa.mod_size << REQ1_RSA_MOD_SIZE_SHIFT)
  148. | (op->ksb_key << REQ1_KEY_KSB_SHIFT)
  149. | REQ1_EOM;
  150. cr[1] = op->u.rsa.input_len - 1;
  151. cr[2] = ccp_addr_lo(&op->src.u.dma);
  152. cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT)
  153. | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
  154. | ccp_addr_hi(&op->src.u.dma);
  155. cr[4] = ccp_addr_lo(&op->dst.u.dma);
  156. cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
  157. | ccp_addr_hi(&op->dst.u.dma);
  158. return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
  159. }
  160. static int ccp_perform_passthru(struct ccp_op *op)
  161. {
  162. u32 cr[6];
  163. /* Fill out the register contents for REQ1 through REQ6 */
  164. cr[0] = (CCP_ENGINE_PASSTHRU << REQ1_ENGINE_SHIFT)
  165. | (op->u.passthru.bit_mod << REQ1_PT_BW_SHIFT)
  166. | (op->u.passthru.byte_swap << REQ1_PT_BS_SHIFT);
  167. if (op->src.type == CCP_MEMTYPE_SYSTEM)
  168. cr[1] = op->src.u.dma.length - 1;
  169. else
  170. cr[1] = op->dst.u.dma.length - 1;
  171. if (op->src.type == CCP_MEMTYPE_SYSTEM) {
  172. cr[2] = ccp_addr_lo(&op->src.u.dma);
  173. cr[3] = (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
  174. | ccp_addr_hi(&op->src.u.dma);
  175. if (op->u.passthru.bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
  176. cr[3] |= (op->ksb_key << REQ4_KSB_SHIFT);
  177. } else {
  178. cr[2] = op->src.u.ksb * CCP_KSB_BYTES;
  179. cr[3] = (CCP_MEMTYPE_KSB << REQ4_MEMTYPE_SHIFT);
  180. }
  181. if (op->dst.type == CCP_MEMTYPE_SYSTEM) {
  182. cr[4] = ccp_addr_lo(&op->dst.u.dma);
  183. cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
  184. | ccp_addr_hi(&op->dst.u.dma);
  185. } else {
  186. cr[4] = op->dst.u.ksb * CCP_KSB_BYTES;
  187. cr[5] = (CCP_MEMTYPE_KSB << REQ6_MEMTYPE_SHIFT);
  188. }
  189. if (op->eom)
  190. cr[0] |= REQ1_EOM;
  191. return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
  192. }
  193. static int ccp_perform_ecc(struct ccp_op *op)
  194. {
  195. u32 cr[6];
  196. /* Fill out the register contents for REQ1 through REQ6 */
  197. cr[0] = REQ1_ECC_AFFINE_CONVERT
  198. | (CCP_ENGINE_ECC << REQ1_ENGINE_SHIFT)
  199. | (op->u.ecc.function << REQ1_ECC_FUNCTION_SHIFT)
  200. | REQ1_EOM;
  201. cr[1] = op->src.u.dma.length - 1;
  202. cr[2] = ccp_addr_lo(&op->src.u.dma);
  203. cr[3] = (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
  204. | ccp_addr_hi(&op->src.u.dma);
  205. cr[4] = ccp_addr_lo(&op->dst.u.dma);
  206. cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
  207. | ccp_addr_hi(&op->dst.u.dma);
  208. return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
  209. }
  210. static int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  211. {
  212. struct ccp_device *ccp = container_of(rng, struct ccp_device, hwrng);
  213. u32 trng_value;
  214. int len = min_t(int, sizeof(trng_value), max);
  215. /*
  216. * Locking is provided by the caller so we can update device
  217. * hwrng-related fields safely
  218. */
  219. trng_value = ioread32(ccp->io_regs + TRNG_OUT_REG);
  220. if (!trng_value) {
  221. /* Zero is returned if not data is available or if a
  222. * bad-entropy error is present. Assume an error if
  223. * we exceed TRNG_RETRIES reads of zero.
  224. */
  225. if (ccp->hwrng_retries++ > TRNG_RETRIES)
  226. return -EIO;
  227. return 0;
  228. }
  229. /* Reset the counter and save the rng value */
  230. ccp->hwrng_retries = 0;
  231. memcpy(data, &trng_value, len);
  232. return len;
  233. }
  234. static int ccp_init(struct ccp_device *ccp)
  235. {
  236. struct device *dev = ccp->dev;
  237. struct ccp_cmd_queue *cmd_q;
  238. struct dma_pool *dma_pool;
  239. char dma_pool_name[MAX_DMAPOOL_NAME_LEN];
  240. unsigned int qmr, qim, i;
  241. int ret;
  242. /* Find available queues */
  243. qim = 0;
  244. qmr = ioread32(ccp->io_regs + Q_MASK_REG);
  245. for (i = 0; i < MAX_HW_QUEUES; i++) {
  246. if (!(qmr & (1 << i)))
  247. continue;
  248. /* Allocate a dma pool for this queue */
  249. snprintf(dma_pool_name, sizeof(dma_pool_name), "%s_q%d",
  250. ccp->name, i);
  251. dma_pool = dma_pool_create(dma_pool_name, dev,
  252. CCP_DMAPOOL_MAX_SIZE,
  253. CCP_DMAPOOL_ALIGN, 0);
  254. if (!dma_pool) {
  255. dev_err(dev, "unable to allocate dma pool\n");
  256. ret = -ENOMEM;
  257. goto e_pool;
  258. }
  259. cmd_q = &ccp->cmd_q[ccp->cmd_q_count];
  260. ccp->cmd_q_count++;
  261. cmd_q->ccp = ccp;
  262. cmd_q->id = i;
  263. cmd_q->dma_pool = dma_pool;
  264. /* Reserve 2 KSB regions for the queue */
  265. cmd_q->ksb_key = KSB_START + ccp->ksb_start++;
  266. cmd_q->ksb_ctx = KSB_START + ccp->ksb_start++;
  267. ccp->ksb_count -= 2;
  268. /* Preset some register values and masks that are queue
  269. * number dependent
  270. */
  271. cmd_q->reg_status = ccp->io_regs + CMD_Q_STATUS_BASE +
  272. (CMD_Q_STATUS_INCR * i);
  273. cmd_q->reg_int_status = ccp->io_regs + CMD_Q_INT_STATUS_BASE +
  274. (CMD_Q_STATUS_INCR * i);
  275. cmd_q->int_ok = 1 << (i * 2);
  276. cmd_q->int_err = 1 << ((i * 2) + 1);
  277. cmd_q->free_slots = CMD_Q_DEPTH(ioread32(cmd_q->reg_status));
  278. init_waitqueue_head(&cmd_q->int_queue);
  279. /* Build queue interrupt mask (two interrupts per queue) */
  280. qim |= cmd_q->int_ok | cmd_q->int_err;
  281. #ifdef CONFIG_ARM64
  282. /* For arm64 set the recommended queue cache settings */
  283. iowrite32(ccp->axcache, ccp->io_regs + CMD_Q_CACHE_BASE +
  284. (CMD_Q_CACHE_INC * i));
  285. #endif
  286. dev_dbg(dev, "queue #%u available\n", i);
  287. }
  288. if (ccp->cmd_q_count == 0) {
  289. dev_notice(dev, "no command queues available\n");
  290. ret = -EIO;
  291. goto e_pool;
  292. }
  293. dev_notice(dev, "%u command queues available\n", ccp->cmd_q_count);
  294. /* Disable and clear interrupts until ready */
  295. iowrite32(0x00, ccp->io_regs + IRQ_MASK_REG);
  296. for (i = 0; i < ccp->cmd_q_count; i++) {
  297. cmd_q = &ccp->cmd_q[i];
  298. ioread32(cmd_q->reg_int_status);
  299. ioread32(cmd_q->reg_status);
  300. }
  301. iowrite32(qim, ccp->io_regs + IRQ_STATUS_REG);
  302. /* Request an irq */
  303. ret = ccp->get_irq(ccp);
  304. if (ret) {
  305. dev_err(dev, "unable to allocate an IRQ\n");
  306. goto e_pool;
  307. }
  308. /* Initialize the queues used to wait for KSB space and suspend */
  309. init_waitqueue_head(&ccp->ksb_queue);
  310. init_waitqueue_head(&ccp->suspend_queue);
  311. /* Create a kthread for each queue */
  312. for (i = 0; i < ccp->cmd_q_count; i++) {
  313. struct task_struct *kthread;
  314. cmd_q = &ccp->cmd_q[i];
  315. kthread = kthread_create(ccp_cmd_queue_thread, cmd_q,
  316. "%s-q%u", ccp->name, cmd_q->id);
  317. if (IS_ERR(kthread)) {
  318. dev_err(dev, "error creating queue thread (%ld)\n",
  319. PTR_ERR(kthread));
  320. ret = PTR_ERR(kthread);
  321. goto e_kthread;
  322. }
  323. cmd_q->kthread = kthread;
  324. wake_up_process(kthread);
  325. }
  326. /* Register the RNG */
  327. ccp->hwrng.name = ccp->rngname;
  328. ccp->hwrng.read = ccp_trng_read;
  329. ret = hwrng_register(&ccp->hwrng);
  330. if (ret) {
  331. dev_err(dev, "error registering hwrng (%d)\n", ret);
  332. goto e_kthread;
  333. }
  334. ccp_add_device(ccp);
  335. /* Enable interrupts */
  336. iowrite32(qim, ccp->io_regs + IRQ_MASK_REG);
  337. return 0;
  338. e_kthread:
  339. for (i = 0; i < ccp->cmd_q_count; i++)
  340. if (ccp->cmd_q[i].kthread)
  341. kthread_stop(ccp->cmd_q[i].kthread);
  342. ccp->free_irq(ccp);
  343. e_pool:
  344. for (i = 0; i < ccp->cmd_q_count; i++)
  345. dma_pool_destroy(ccp->cmd_q[i].dma_pool);
  346. return ret;
  347. }
  348. static void ccp_destroy(struct ccp_device *ccp)
  349. {
  350. struct ccp_cmd_queue *cmd_q;
  351. struct ccp_cmd *cmd;
  352. unsigned int qim, i;
  353. /* Remove this device from the list of available units first */
  354. ccp_del_device(ccp);
  355. /* Unregister the RNG */
  356. hwrng_unregister(&ccp->hwrng);
  357. /* Stop the queue kthreads */
  358. for (i = 0; i < ccp->cmd_q_count; i++)
  359. if (ccp->cmd_q[i].kthread)
  360. kthread_stop(ccp->cmd_q[i].kthread);
  361. /* Build queue interrupt mask (two interrupt masks per queue) */
  362. qim = 0;
  363. for (i = 0; i < ccp->cmd_q_count; i++) {
  364. cmd_q = &ccp->cmd_q[i];
  365. qim |= cmd_q->int_ok | cmd_q->int_err;
  366. }
  367. /* Disable and clear interrupts */
  368. iowrite32(0x00, ccp->io_regs + IRQ_MASK_REG);
  369. for (i = 0; i < ccp->cmd_q_count; i++) {
  370. cmd_q = &ccp->cmd_q[i];
  371. ioread32(cmd_q->reg_int_status);
  372. ioread32(cmd_q->reg_status);
  373. }
  374. iowrite32(qim, ccp->io_regs + IRQ_STATUS_REG);
  375. ccp->free_irq(ccp);
  376. for (i = 0; i < ccp->cmd_q_count; i++)
  377. dma_pool_destroy(ccp->cmd_q[i].dma_pool);
  378. /* Flush the cmd and backlog queue */
  379. while (!list_empty(&ccp->cmd)) {
  380. /* Invoke the callback directly with an error code */
  381. cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry);
  382. list_del(&cmd->entry);
  383. cmd->callback(cmd->data, -ENODEV);
  384. }
  385. while (!list_empty(&ccp->backlog)) {
  386. /* Invoke the callback directly with an error code */
  387. cmd = list_first_entry(&ccp->backlog, struct ccp_cmd, entry);
  388. list_del(&cmd->entry);
  389. cmd->callback(cmd->data, -ENODEV);
  390. }
  391. }
  392. static irqreturn_t ccp_irq_handler(int irq, void *data)
  393. {
  394. struct device *dev = data;
  395. struct ccp_device *ccp = dev_get_drvdata(dev);
  396. struct ccp_cmd_queue *cmd_q;
  397. u32 q_int, status;
  398. unsigned int i;
  399. status = ioread32(ccp->io_regs + IRQ_STATUS_REG);
  400. for (i = 0; i < ccp->cmd_q_count; i++) {
  401. cmd_q = &ccp->cmd_q[i];
  402. q_int = status & (cmd_q->int_ok | cmd_q->int_err);
  403. if (q_int) {
  404. cmd_q->int_status = status;
  405. cmd_q->q_status = ioread32(cmd_q->reg_status);
  406. cmd_q->q_int_status = ioread32(cmd_q->reg_int_status);
  407. /* On error, only save the first error value */
  408. if ((q_int & cmd_q->int_err) && !cmd_q->cmd_error)
  409. cmd_q->cmd_error = CMD_Q_ERROR(cmd_q->q_status);
  410. cmd_q->int_rcvd = 1;
  411. /* Acknowledge the interrupt and wake the kthread */
  412. iowrite32(q_int, ccp->io_regs + IRQ_STATUS_REG);
  413. wake_up_interruptible(&cmd_q->int_queue);
  414. }
  415. }
  416. return IRQ_HANDLED;
  417. }
  418. static struct ccp_actions ccp3_actions = {
  419. .perform_aes = ccp_perform_aes,
  420. .perform_xts_aes = ccp_perform_xts_aes,
  421. .perform_sha = ccp_perform_sha,
  422. .perform_rsa = ccp_perform_rsa,
  423. .perform_passthru = ccp_perform_passthru,
  424. .perform_ecc = ccp_perform_ecc,
  425. .init = ccp_init,
  426. .destroy = ccp_destroy,
  427. .irqhandler = ccp_irq_handler,
  428. };
  429. struct ccp_vdata ccpv3 = {
  430. .version = CCP_VERSION(3, 0),
  431. .perform = &ccp3_actions,
  432. };