ccp-dev.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) driver
  3. *
  4. * Copyright (C) 2013 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/kthread.h>
  15. #include <linux/sched.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/mutex.h>
  19. #include <linux/delay.h>
  20. #include <linux/hw_random.h>
  21. #include <linux/cpu.h>
  22. #ifdef CONFIG_X86
  23. #include <asm/cpu_device_id.h>
  24. #endif
  25. #include <linux/ccp.h>
  26. #include "ccp-dev.h"
  27. MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
  28. MODULE_LICENSE("GPL");
  29. MODULE_VERSION("1.0.0");
  30. MODULE_DESCRIPTION("AMD Cryptographic Coprocessor driver");
  31. struct ccp_tasklet_data {
  32. struct completion completion;
  33. struct ccp_cmd *cmd;
  34. };
  35. static struct ccp_device *ccp_dev;
  36. static inline struct ccp_device *ccp_get_device(void)
  37. {
  38. return ccp_dev;
  39. }
  40. static inline void ccp_add_device(struct ccp_device *ccp)
  41. {
  42. ccp_dev = ccp;
  43. }
  44. static inline void ccp_del_device(struct ccp_device *ccp)
  45. {
  46. ccp_dev = NULL;
  47. }
  48. /**
  49. * ccp_enqueue_cmd - queue an operation for processing by the CCP
  50. *
  51. * @cmd: ccp_cmd struct to be processed
  52. *
  53. * Queue a cmd to be processed by the CCP. If queueing the cmd
  54. * would exceed the defined length of the cmd queue the cmd will
  55. * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will
  56. * result in a return code of -EBUSY.
  57. *
  58. * The callback routine specified in the ccp_cmd struct will be
  59. * called to notify the caller of completion (if the cmd was not
  60. * backlogged) or advancement out of the backlog. If the cmd has
  61. * advanced out of the backlog the "err" value of the callback
  62. * will be -EINPROGRESS. Any other "err" value during callback is
  63. * the result of the operation.
  64. *
  65. * The cmd has been successfully queued if:
  66. * the return code is -EINPROGRESS or
  67. * the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set
  68. */
  69. int ccp_enqueue_cmd(struct ccp_cmd *cmd)
  70. {
  71. struct ccp_device *ccp = ccp_get_device();
  72. unsigned long flags;
  73. unsigned int i;
  74. int ret;
  75. if (!ccp)
  76. return -ENODEV;
  77. /* Caller must supply a callback routine */
  78. if (!cmd->callback)
  79. return -EINVAL;
  80. cmd->ccp = ccp;
  81. spin_lock_irqsave(&ccp->cmd_lock, flags);
  82. i = ccp->cmd_q_count;
  83. if (ccp->cmd_count >= MAX_CMD_QLEN) {
  84. ret = -EBUSY;
  85. if (cmd->flags & CCP_CMD_MAY_BACKLOG)
  86. list_add_tail(&cmd->entry, &ccp->backlog);
  87. } else {
  88. ret = -EINPROGRESS;
  89. ccp->cmd_count++;
  90. list_add_tail(&cmd->entry, &ccp->cmd);
  91. /* Find an idle queue */
  92. if (!ccp->suspending) {
  93. for (i = 0; i < ccp->cmd_q_count; i++) {
  94. if (ccp->cmd_q[i].active)
  95. continue;
  96. break;
  97. }
  98. }
  99. }
  100. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  101. /* If we found an idle queue, wake it up */
  102. if (i < ccp->cmd_q_count)
  103. wake_up_process(ccp->cmd_q[i].kthread);
  104. return ret;
  105. }
  106. EXPORT_SYMBOL_GPL(ccp_enqueue_cmd);
  107. static void ccp_do_cmd_backlog(struct work_struct *work)
  108. {
  109. struct ccp_cmd *cmd = container_of(work, struct ccp_cmd, work);
  110. struct ccp_device *ccp = cmd->ccp;
  111. unsigned long flags;
  112. unsigned int i;
  113. cmd->callback(cmd->data, -EINPROGRESS);
  114. spin_lock_irqsave(&ccp->cmd_lock, flags);
  115. ccp->cmd_count++;
  116. list_add_tail(&cmd->entry, &ccp->cmd);
  117. /* Find an idle queue */
  118. for (i = 0; i < ccp->cmd_q_count; i++) {
  119. if (ccp->cmd_q[i].active)
  120. continue;
  121. break;
  122. }
  123. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  124. /* If we found an idle queue, wake it up */
  125. if (i < ccp->cmd_q_count)
  126. wake_up_process(ccp->cmd_q[i].kthread);
  127. }
  128. static struct ccp_cmd *ccp_dequeue_cmd(struct ccp_cmd_queue *cmd_q)
  129. {
  130. struct ccp_device *ccp = cmd_q->ccp;
  131. struct ccp_cmd *cmd = NULL;
  132. struct ccp_cmd *backlog = NULL;
  133. unsigned long flags;
  134. spin_lock_irqsave(&ccp->cmd_lock, flags);
  135. cmd_q->active = 0;
  136. if (ccp->suspending) {
  137. cmd_q->suspended = 1;
  138. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  139. wake_up_interruptible(&ccp->suspend_queue);
  140. return NULL;
  141. }
  142. if (ccp->cmd_count) {
  143. cmd_q->active = 1;
  144. cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry);
  145. list_del(&cmd->entry);
  146. ccp->cmd_count--;
  147. }
  148. if (!list_empty(&ccp->backlog)) {
  149. backlog = list_first_entry(&ccp->backlog, struct ccp_cmd,
  150. entry);
  151. list_del(&backlog->entry);
  152. }
  153. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  154. if (backlog) {
  155. INIT_WORK(&backlog->work, ccp_do_cmd_backlog);
  156. schedule_work(&backlog->work);
  157. }
  158. return cmd;
  159. }
  160. static void ccp_do_cmd_complete(unsigned long data)
  161. {
  162. struct ccp_tasklet_data *tdata = (struct ccp_tasklet_data *)data;
  163. struct ccp_cmd *cmd = tdata->cmd;
  164. cmd->callback(cmd->data, cmd->ret);
  165. complete(&tdata->completion);
  166. }
  167. static int ccp_cmd_queue_thread(void *data)
  168. {
  169. struct ccp_cmd_queue *cmd_q = (struct ccp_cmd_queue *)data;
  170. struct ccp_cmd *cmd;
  171. struct ccp_tasklet_data tdata;
  172. struct tasklet_struct tasklet;
  173. tasklet_init(&tasklet, ccp_do_cmd_complete, (unsigned long)&tdata);
  174. set_current_state(TASK_INTERRUPTIBLE);
  175. while (!kthread_should_stop()) {
  176. schedule();
  177. set_current_state(TASK_INTERRUPTIBLE);
  178. cmd = ccp_dequeue_cmd(cmd_q);
  179. if (!cmd)
  180. continue;
  181. __set_current_state(TASK_RUNNING);
  182. /* Execute the command */
  183. cmd->ret = ccp_run_cmd(cmd_q, cmd);
  184. /* Schedule the completion callback */
  185. tdata.cmd = cmd;
  186. init_completion(&tdata.completion);
  187. tasklet_schedule(&tasklet);
  188. wait_for_completion(&tdata.completion);
  189. }
  190. __set_current_state(TASK_RUNNING);
  191. return 0;
  192. }
  193. static int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  194. {
  195. struct ccp_device *ccp = container_of(rng, struct ccp_device, hwrng);
  196. u32 trng_value;
  197. int len = min_t(int, sizeof(trng_value), max);
  198. /*
  199. * Locking is provided by the caller so we can update device
  200. * hwrng-related fields safely
  201. */
  202. trng_value = ioread32(ccp->io_regs + TRNG_OUT_REG);
  203. if (!trng_value) {
  204. /* Zero is returned if not data is available or if a
  205. * bad-entropy error is present. Assume an error if
  206. * we exceed TRNG_RETRIES reads of zero.
  207. */
  208. if (ccp->hwrng_retries++ > TRNG_RETRIES)
  209. return -EIO;
  210. return 0;
  211. }
  212. /* Reset the counter and save the rng value */
  213. ccp->hwrng_retries = 0;
  214. memcpy(data, &trng_value, len);
  215. return len;
  216. }
  217. /**
  218. * ccp_alloc_struct - allocate and initialize the ccp_device struct
  219. *
  220. * @dev: device struct of the CCP
  221. */
  222. struct ccp_device *ccp_alloc_struct(struct device *dev)
  223. {
  224. struct ccp_device *ccp;
  225. ccp = kzalloc(sizeof(*ccp), GFP_KERNEL);
  226. if (ccp == NULL) {
  227. dev_err(dev, "unable to allocate device struct\n");
  228. return NULL;
  229. }
  230. ccp->dev = dev;
  231. INIT_LIST_HEAD(&ccp->cmd);
  232. INIT_LIST_HEAD(&ccp->backlog);
  233. spin_lock_init(&ccp->cmd_lock);
  234. mutex_init(&ccp->req_mutex);
  235. mutex_init(&ccp->ksb_mutex);
  236. ccp->ksb_count = KSB_COUNT;
  237. ccp->ksb_start = 0;
  238. return ccp;
  239. }
  240. /**
  241. * ccp_init - initialize the CCP device
  242. *
  243. * @ccp: ccp_device struct
  244. */
  245. int ccp_init(struct ccp_device *ccp)
  246. {
  247. struct device *dev = ccp->dev;
  248. struct ccp_cmd_queue *cmd_q;
  249. struct dma_pool *dma_pool;
  250. char dma_pool_name[MAX_DMAPOOL_NAME_LEN];
  251. unsigned int qmr, qim, i;
  252. int ret;
  253. /* Find available queues */
  254. qim = 0;
  255. qmr = ioread32(ccp->io_regs + Q_MASK_REG);
  256. for (i = 0; i < MAX_HW_QUEUES; i++) {
  257. if (!(qmr & (1 << i)))
  258. continue;
  259. /* Allocate a dma pool for this queue */
  260. snprintf(dma_pool_name, sizeof(dma_pool_name), "ccp_q%d", i);
  261. dma_pool = dma_pool_create(dma_pool_name, dev,
  262. CCP_DMAPOOL_MAX_SIZE,
  263. CCP_DMAPOOL_ALIGN, 0);
  264. if (!dma_pool) {
  265. dev_err(dev, "unable to allocate dma pool\n");
  266. ret = -ENOMEM;
  267. goto e_pool;
  268. }
  269. cmd_q = &ccp->cmd_q[ccp->cmd_q_count];
  270. ccp->cmd_q_count++;
  271. cmd_q->ccp = ccp;
  272. cmd_q->id = i;
  273. cmd_q->dma_pool = dma_pool;
  274. /* Reserve 2 KSB regions for the queue */
  275. cmd_q->ksb_key = KSB_START + ccp->ksb_start++;
  276. cmd_q->ksb_ctx = KSB_START + ccp->ksb_start++;
  277. ccp->ksb_count -= 2;
  278. /* Preset some register values and masks that are queue
  279. * number dependent
  280. */
  281. cmd_q->reg_status = ccp->io_regs + CMD_Q_STATUS_BASE +
  282. (CMD_Q_STATUS_INCR * i);
  283. cmd_q->reg_int_status = ccp->io_regs + CMD_Q_INT_STATUS_BASE +
  284. (CMD_Q_STATUS_INCR * i);
  285. cmd_q->int_ok = 1 << (i * 2);
  286. cmd_q->int_err = 1 << ((i * 2) + 1);
  287. cmd_q->free_slots = CMD_Q_DEPTH(ioread32(cmd_q->reg_status));
  288. init_waitqueue_head(&cmd_q->int_queue);
  289. /* Build queue interrupt mask (two interrupts per queue) */
  290. qim |= cmd_q->int_ok | cmd_q->int_err;
  291. #ifdef CONFIG_ARM64
  292. /* For arm64 set the recommended queue cache settings */
  293. iowrite32(ccp->axcache, ccp->io_regs + CMD_Q_CACHE_BASE +
  294. (CMD_Q_CACHE_INC * i));
  295. #endif
  296. dev_dbg(dev, "queue #%u available\n", i);
  297. }
  298. if (ccp->cmd_q_count == 0) {
  299. dev_notice(dev, "no command queues available\n");
  300. ret = -EIO;
  301. goto e_pool;
  302. }
  303. dev_notice(dev, "%u command queues available\n", ccp->cmd_q_count);
  304. /* Disable and clear interrupts until ready */
  305. iowrite32(0x00, ccp->io_regs + IRQ_MASK_REG);
  306. for (i = 0; i < ccp->cmd_q_count; i++) {
  307. cmd_q = &ccp->cmd_q[i];
  308. ioread32(cmd_q->reg_int_status);
  309. ioread32(cmd_q->reg_status);
  310. }
  311. iowrite32(qim, ccp->io_regs + IRQ_STATUS_REG);
  312. /* Request an irq */
  313. ret = ccp->get_irq(ccp);
  314. if (ret) {
  315. dev_err(dev, "unable to allocate an IRQ\n");
  316. goto e_pool;
  317. }
  318. /* Initialize the queues used to wait for KSB space and suspend */
  319. init_waitqueue_head(&ccp->ksb_queue);
  320. init_waitqueue_head(&ccp->suspend_queue);
  321. /* Create a kthread for each queue */
  322. for (i = 0; i < ccp->cmd_q_count; i++) {
  323. struct task_struct *kthread;
  324. cmd_q = &ccp->cmd_q[i];
  325. kthread = kthread_create(ccp_cmd_queue_thread, cmd_q,
  326. "ccp-q%u", cmd_q->id);
  327. if (IS_ERR(kthread)) {
  328. dev_err(dev, "error creating queue thread (%ld)\n",
  329. PTR_ERR(kthread));
  330. ret = PTR_ERR(kthread);
  331. goto e_kthread;
  332. }
  333. cmd_q->kthread = kthread;
  334. wake_up_process(kthread);
  335. }
  336. /* Register the RNG */
  337. ccp->hwrng.name = "ccp-rng";
  338. ccp->hwrng.read = ccp_trng_read;
  339. ret = hwrng_register(&ccp->hwrng);
  340. if (ret) {
  341. dev_err(dev, "error registering hwrng (%d)\n", ret);
  342. goto e_kthread;
  343. }
  344. /* Make the device struct available before enabling interrupts */
  345. ccp_add_device(ccp);
  346. /* Enable interrupts */
  347. iowrite32(qim, ccp->io_regs + IRQ_MASK_REG);
  348. return 0;
  349. e_kthread:
  350. for (i = 0; i < ccp->cmd_q_count; i++)
  351. if (ccp->cmd_q[i].kthread)
  352. kthread_stop(ccp->cmd_q[i].kthread);
  353. ccp->free_irq(ccp);
  354. e_pool:
  355. for (i = 0; i < ccp->cmd_q_count; i++)
  356. dma_pool_destroy(ccp->cmd_q[i].dma_pool);
  357. return ret;
  358. }
  359. /**
  360. * ccp_destroy - tear down the CCP device
  361. *
  362. * @ccp: ccp_device struct
  363. */
  364. void ccp_destroy(struct ccp_device *ccp)
  365. {
  366. struct ccp_cmd_queue *cmd_q;
  367. struct ccp_cmd *cmd;
  368. unsigned int qim, i;
  369. /* Remove general access to the device struct */
  370. ccp_del_device(ccp);
  371. /* Unregister the RNG */
  372. hwrng_unregister(&ccp->hwrng);
  373. /* Stop the queue kthreads */
  374. for (i = 0; i < ccp->cmd_q_count; i++)
  375. if (ccp->cmd_q[i].kthread)
  376. kthread_stop(ccp->cmd_q[i].kthread);
  377. /* Build queue interrupt mask (two interrupt masks per queue) */
  378. qim = 0;
  379. for (i = 0; i < ccp->cmd_q_count; i++) {
  380. cmd_q = &ccp->cmd_q[i];
  381. qim |= cmd_q->int_ok | cmd_q->int_err;
  382. }
  383. /* Disable and clear interrupts */
  384. iowrite32(0x00, ccp->io_regs + IRQ_MASK_REG);
  385. for (i = 0; i < ccp->cmd_q_count; i++) {
  386. cmd_q = &ccp->cmd_q[i];
  387. ioread32(cmd_q->reg_int_status);
  388. ioread32(cmd_q->reg_status);
  389. }
  390. iowrite32(qim, ccp->io_regs + IRQ_STATUS_REG);
  391. ccp->free_irq(ccp);
  392. for (i = 0; i < ccp->cmd_q_count; i++)
  393. dma_pool_destroy(ccp->cmd_q[i].dma_pool);
  394. /* Flush the cmd and backlog queue */
  395. while (!list_empty(&ccp->cmd)) {
  396. /* Invoke the callback directly with an error code */
  397. cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry);
  398. list_del(&cmd->entry);
  399. cmd->callback(cmd->data, -ENODEV);
  400. }
  401. while (!list_empty(&ccp->backlog)) {
  402. /* Invoke the callback directly with an error code */
  403. cmd = list_first_entry(&ccp->backlog, struct ccp_cmd, entry);
  404. list_del(&cmd->entry);
  405. cmd->callback(cmd->data, -ENODEV);
  406. }
  407. }
  408. /**
  409. * ccp_irq_handler - handle interrupts generated by the CCP device
  410. *
  411. * @irq: the irq associated with the interrupt
  412. * @data: the data value supplied when the irq was created
  413. */
  414. irqreturn_t ccp_irq_handler(int irq, void *data)
  415. {
  416. struct device *dev = data;
  417. struct ccp_device *ccp = dev_get_drvdata(dev);
  418. struct ccp_cmd_queue *cmd_q;
  419. u32 q_int, status;
  420. unsigned int i;
  421. status = ioread32(ccp->io_regs + IRQ_STATUS_REG);
  422. for (i = 0; i < ccp->cmd_q_count; i++) {
  423. cmd_q = &ccp->cmd_q[i];
  424. q_int = status & (cmd_q->int_ok | cmd_q->int_err);
  425. if (q_int) {
  426. cmd_q->int_status = status;
  427. cmd_q->q_status = ioread32(cmd_q->reg_status);
  428. cmd_q->q_int_status = ioread32(cmd_q->reg_int_status);
  429. /* On error, only save the first error value */
  430. if ((q_int & cmd_q->int_err) && !cmd_q->cmd_error)
  431. cmd_q->cmd_error = CMD_Q_ERROR(cmd_q->q_status);
  432. cmd_q->int_rcvd = 1;
  433. /* Acknowledge the interrupt and wake the kthread */
  434. iowrite32(q_int, ccp->io_regs + IRQ_STATUS_REG);
  435. wake_up_interruptible(&cmd_q->int_queue);
  436. }
  437. }
  438. return IRQ_HANDLED;
  439. }
  440. #ifdef CONFIG_PM
  441. bool ccp_queues_suspended(struct ccp_device *ccp)
  442. {
  443. unsigned int suspended = 0;
  444. unsigned long flags;
  445. unsigned int i;
  446. spin_lock_irqsave(&ccp->cmd_lock, flags);
  447. for (i = 0; i < ccp->cmd_q_count; i++)
  448. if (ccp->cmd_q[i].suspended)
  449. suspended++;
  450. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  451. return ccp->cmd_q_count == suspended;
  452. }
  453. #endif
  454. #ifdef CONFIG_X86
  455. static const struct x86_cpu_id ccp_support[] = {
  456. { X86_VENDOR_AMD, 22, },
  457. };
  458. #endif
  459. static int __init ccp_mod_init(void)
  460. {
  461. #ifdef CONFIG_X86
  462. struct cpuinfo_x86 *cpuinfo = &boot_cpu_data;
  463. int ret;
  464. if (!x86_match_cpu(ccp_support))
  465. return -ENODEV;
  466. switch (cpuinfo->x86) {
  467. case 22:
  468. if ((cpuinfo->x86_model < 48) || (cpuinfo->x86_model > 63))
  469. return -ENODEV;
  470. ret = ccp_pci_init();
  471. if (ret)
  472. return ret;
  473. /* Don't leave the driver loaded if init failed */
  474. if (!ccp_get_device()) {
  475. ccp_pci_exit();
  476. return -ENODEV;
  477. }
  478. return 0;
  479. break;
  480. }
  481. #endif
  482. #ifdef CONFIG_ARM64
  483. int ret;
  484. ret = ccp_platform_init();
  485. if (ret)
  486. return ret;
  487. /* Don't leave the driver loaded if init failed */
  488. if (!ccp_get_device()) {
  489. ccp_platform_exit();
  490. return -ENODEV;
  491. }
  492. return 0;
  493. #endif
  494. return -ENODEV;
  495. }
  496. static void __exit ccp_mod_exit(void)
  497. {
  498. #ifdef CONFIG_X86
  499. struct cpuinfo_x86 *cpuinfo = &boot_cpu_data;
  500. switch (cpuinfo->x86) {
  501. case 22:
  502. ccp_pci_exit();
  503. break;
  504. }
  505. #endif
  506. #ifdef CONFIG_ARM64
  507. ccp_platform_exit();
  508. #endif
  509. }
  510. module_init(ccp_mod_init);
  511. module_exit(ccp_mod_exit);