ccp-dev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. * Author: Gary R Hook <gary.hook@amd.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/kthread.h>
  16. #include <linux/sched.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/spinlock_types.h>
  20. #include <linux/types.h>
  21. #include <linux/mutex.h>
  22. #include <linux/delay.h>
  23. #include <linux/hw_random.h>
  24. #include <linux/cpu.h>
  25. #ifdef CONFIG_X86
  26. #include <asm/cpu_device_id.h>
  27. #endif
  28. #include <linux/ccp.h>
  29. #include "ccp-dev.h"
  30. MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
  31. MODULE_LICENSE("GPL");
  32. MODULE_VERSION("1.0.0");
  33. MODULE_DESCRIPTION("AMD Cryptographic Coprocessor driver");
  34. struct ccp_tasklet_data {
  35. struct completion completion;
  36. struct ccp_cmd *cmd;
  37. };
  38. /* List of CCPs, CCP count, read-write access lock, and access functions
  39. *
  40. * Lock structure: get ccp_unit_lock for reading whenever we need to
  41. * examine the CCP list. While holding it for reading we can acquire
  42. * the RR lock to update the round-robin next-CCP pointer. The unit lock
  43. * must be acquired before the RR lock.
  44. *
  45. * If the unit-lock is acquired for writing, we have total control over
  46. * the list, so there's no value in getting the RR lock.
  47. */
  48. static DEFINE_RWLOCK(ccp_unit_lock);
  49. static LIST_HEAD(ccp_units);
  50. /* Round-robin counter */
  51. static DEFINE_SPINLOCK(ccp_rr_lock);
  52. static struct ccp_device *ccp_rr;
  53. /* Ever-increasing value to produce unique unit numbers */
  54. static atomic_t ccp_unit_ordinal;
  55. static unsigned int ccp_increment_unit_ordinal(void)
  56. {
  57. return atomic_inc_return(&ccp_unit_ordinal);
  58. }
  59. /**
  60. * ccp_add_device - add a CCP device to the list
  61. *
  62. * @ccp: ccp_device struct pointer
  63. *
  64. * Put this CCP on the unit list, which makes it available
  65. * for use.
  66. *
  67. * Returns zero if a CCP device is present, -ENODEV otherwise.
  68. */
  69. void ccp_add_device(struct ccp_device *ccp)
  70. {
  71. unsigned long flags;
  72. write_lock_irqsave(&ccp_unit_lock, flags);
  73. list_add_tail(&ccp->entry, &ccp_units);
  74. if (!ccp_rr)
  75. /* We already have the list lock (we're first) so this
  76. * pointer can't change on us. Set its initial value.
  77. */
  78. ccp_rr = ccp;
  79. write_unlock_irqrestore(&ccp_unit_lock, flags);
  80. }
  81. /**
  82. * ccp_del_device - remove a CCP device from the list
  83. *
  84. * @ccp: ccp_device struct pointer
  85. *
  86. * Remove this unit from the list of devices. If the next device
  87. * up for use is this one, adjust the pointer. If this is the last
  88. * device, NULL the pointer.
  89. */
  90. void ccp_del_device(struct ccp_device *ccp)
  91. {
  92. unsigned long flags;
  93. write_lock_irqsave(&ccp_unit_lock, flags);
  94. if (ccp_rr == ccp) {
  95. /* ccp_unit_lock is read/write; any read access
  96. * will be suspended while we make changes to the
  97. * list and RR pointer.
  98. */
  99. if (list_is_last(&ccp_rr->entry, &ccp_units))
  100. ccp_rr = list_first_entry(&ccp_units, struct ccp_device,
  101. entry);
  102. else
  103. ccp_rr = list_next_entry(ccp_rr, entry);
  104. }
  105. list_del(&ccp->entry);
  106. if (list_empty(&ccp_units))
  107. ccp_rr = NULL;
  108. write_unlock_irqrestore(&ccp_unit_lock, flags);
  109. }
  110. int ccp_register_rng(struct ccp_device *ccp)
  111. {
  112. int ret = 0;
  113. dev_dbg(ccp->dev, "Registering RNG...\n");
  114. /* Register an RNG */
  115. ccp->hwrng.name = ccp->rngname;
  116. ccp->hwrng.read = ccp_trng_read;
  117. ret = hwrng_register(&ccp->hwrng);
  118. if (ret)
  119. dev_err(ccp->dev, "error registering hwrng (%d)\n", ret);
  120. return ret;
  121. }
  122. void ccp_unregister_rng(struct ccp_device *ccp)
  123. {
  124. if (ccp->hwrng.name)
  125. hwrng_unregister(&ccp->hwrng);
  126. }
  127. static struct ccp_device *ccp_get_device(void)
  128. {
  129. unsigned long flags;
  130. struct ccp_device *dp = NULL;
  131. /* We round-robin through the unit list.
  132. * The (ccp_rr) pointer refers to the next unit to use.
  133. */
  134. read_lock_irqsave(&ccp_unit_lock, flags);
  135. if (!list_empty(&ccp_units)) {
  136. spin_lock(&ccp_rr_lock);
  137. dp = ccp_rr;
  138. if (list_is_last(&ccp_rr->entry, &ccp_units))
  139. ccp_rr = list_first_entry(&ccp_units, struct ccp_device,
  140. entry);
  141. else
  142. ccp_rr = list_next_entry(ccp_rr, entry);
  143. spin_unlock(&ccp_rr_lock);
  144. }
  145. read_unlock_irqrestore(&ccp_unit_lock, flags);
  146. return dp;
  147. }
  148. /**
  149. * ccp_present - check if a CCP device is present
  150. *
  151. * Returns zero if a CCP device is present, -ENODEV otherwise.
  152. */
  153. int ccp_present(void)
  154. {
  155. unsigned long flags;
  156. int ret;
  157. read_lock_irqsave(&ccp_unit_lock, flags);
  158. ret = list_empty(&ccp_units);
  159. read_unlock_irqrestore(&ccp_unit_lock, flags);
  160. return ret ? -ENODEV : 0;
  161. }
  162. EXPORT_SYMBOL_GPL(ccp_present);
  163. /**
  164. * ccp_version - get the version of the CCP device
  165. *
  166. * Returns the version from the first unit on the list;
  167. * otherwise a zero if no CCP device is present
  168. */
  169. unsigned int ccp_version(void)
  170. {
  171. struct ccp_device *dp;
  172. unsigned long flags;
  173. int ret = 0;
  174. read_lock_irqsave(&ccp_unit_lock, flags);
  175. if (!list_empty(&ccp_units)) {
  176. dp = list_first_entry(&ccp_units, struct ccp_device, entry);
  177. ret = dp->vdata->version;
  178. }
  179. read_unlock_irqrestore(&ccp_unit_lock, flags);
  180. return ret;
  181. }
  182. EXPORT_SYMBOL_GPL(ccp_version);
  183. /**
  184. * ccp_enqueue_cmd - queue an operation for processing by the CCP
  185. *
  186. * @cmd: ccp_cmd struct to be processed
  187. *
  188. * Queue a cmd to be processed by the CCP. If queueing the cmd
  189. * would exceed the defined length of the cmd queue the cmd will
  190. * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will
  191. * result in a return code of -EBUSY.
  192. *
  193. * The callback routine specified in the ccp_cmd struct will be
  194. * called to notify the caller of completion (if the cmd was not
  195. * backlogged) or advancement out of the backlog. If the cmd has
  196. * advanced out of the backlog the "err" value of the callback
  197. * will be -EINPROGRESS. Any other "err" value during callback is
  198. * the result of the operation.
  199. *
  200. * The cmd has been successfully queued if:
  201. * the return code is -EINPROGRESS or
  202. * the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set
  203. */
  204. int ccp_enqueue_cmd(struct ccp_cmd *cmd)
  205. {
  206. struct ccp_device *ccp = ccp_get_device();
  207. unsigned long flags;
  208. unsigned int i;
  209. int ret;
  210. if (!ccp)
  211. return -ENODEV;
  212. /* Caller must supply a callback routine */
  213. if (!cmd->callback)
  214. return -EINVAL;
  215. cmd->ccp = ccp;
  216. spin_lock_irqsave(&ccp->cmd_lock, flags);
  217. i = ccp->cmd_q_count;
  218. if (ccp->cmd_count >= MAX_CMD_QLEN) {
  219. ret = -EBUSY;
  220. if (cmd->flags & CCP_CMD_MAY_BACKLOG)
  221. list_add_tail(&cmd->entry, &ccp->backlog);
  222. } else {
  223. ret = -EINPROGRESS;
  224. ccp->cmd_count++;
  225. list_add_tail(&cmd->entry, &ccp->cmd);
  226. /* Find an idle queue */
  227. if (!ccp->suspending) {
  228. for (i = 0; i < ccp->cmd_q_count; i++) {
  229. if (ccp->cmd_q[i].active)
  230. continue;
  231. break;
  232. }
  233. }
  234. }
  235. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  236. /* If we found an idle queue, wake it up */
  237. if (i < ccp->cmd_q_count)
  238. wake_up_process(ccp->cmd_q[i].kthread);
  239. return ret;
  240. }
  241. EXPORT_SYMBOL_GPL(ccp_enqueue_cmd);
  242. static void ccp_do_cmd_backlog(struct work_struct *work)
  243. {
  244. struct ccp_cmd *cmd = container_of(work, struct ccp_cmd, work);
  245. struct ccp_device *ccp = cmd->ccp;
  246. unsigned long flags;
  247. unsigned int i;
  248. cmd->callback(cmd->data, -EINPROGRESS);
  249. spin_lock_irqsave(&ccp->cmd_lock, flags);
  250. ccp->cmd_count++;
  251. list_add_tail(&cmd->entry, &ccp->cmd);
  252. /* Find an idle queue */
  253. for (i = 0; i < ccp->cmd_q_count; i++) {
  254. if (ccp->cmd_q[i].active)
  255. continue;
  256. break;
  257. }
  258. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  259. /* If we found an idle queue, wake it up */
  260. if (i < ccp->cmd_q_count)
  261. wake_up_process(ccp->cmd_q[i].kthread);
  262. }
  263. static struct ccp_cmd *ccp_dequeue_cmd(struct ccp_cmd_queue *cmd_q)
  264. {
  265. struct ccp_device *ccp = cmd_q->ccp;
  266. struct ccp_cmd *cmd = NULL;
  267. struct ccp_cmd *backlog = NULL;
  268. unsigned long flags;
  269. spin_lock_irqsave(&ccp->cmd_lock, flags);
  270. cmd_q->active = 0;
  271. if (ccp->suspending) {
  272. cmd_q->suspended = 1;
  273. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  274. wake_up_interruptible(&ccp->suspend_queue);
  275. return NULL;
  276. }
  277. if (ccp->cmd_count) {
  278. cmd_q->active = 1;
  279. cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry);
  280. list_del(&cmd->entry);
  281. ccp->cmd_count--;
  282. }
  283. if (!list_empty(&ccp->backlog)) {
  284. backlog = list_first_entry(&ccp->backlog, struct ccp_cmd,
  285. entry);
  286. list_del(&backlog->entry);
  287. }
  288. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  289. if (backlog) {
  290. INIT_WORK(&backlog->work, ccp_do_cmd_backlog);
  291. schedule_work(&backlog->work);
  292. }
  293. return cmd;
  294. }
  295. static void ccp_do_cmd_complete(unsigned long data)
  296. {
  297. struct ccp_tasklet_data *tdata = (struct ccp_tasklet_data *)data;
  298. struct ccp_cmd *cmd = tdata->cmd;
  299. cmd->callback(cmd->data, cmd->ret);
  300. complete(&tdata->completion);
  301. }
  302. /**
  303. * ccp_cmd_queue_thread - create a kernel thread to manage a CCP queue
  304. *
  305. * @data: thread-specific data
  306. */
  307. int ccp_cmd_queue_thread(void *data)
  308. {
  309. struct ccp_cmd_queue *cmd_q = (struct ccp_cmd_queue *)data;
  310. struct ccp_cmd *cmd;
  311. struct ccp_tasklet_data tdata;
  312. struct tasklet_struct tasklet;
  313. tasklet_init(&tasklet, ccp_do_cmd_complete, (unsigned long)&tdata);
  314. set_current_state(TASK_INTERRUPTIBLE);
  315. while (!kthread_should_stop()) {
  316. schedule();
  317. set_current_state(TASK_INTERRUPTIBLE);
  318. cmd = ccp_dequeue_cmd(cmd_q);
  319. if (!cmd)
  320. continue;
  321. __set_current_state(TASK_RUNNING);
  322. /* Execute the command */
  323. cmd->ret = ccp_run_cmd(cmd_q, cmd);
  324. /* Schedule the completion callback */
  325. tdata.cmd = cmd;
  326. init_completion(&tdata.completion);
  327. tasklet_schedule(&tasklet);
  328. wait_for_completion(&tdata.completion);
  329. }
  330. __set_current_state(TASK_RUNNING);
  331. return 0;
  332. }
  333. /**
  334. * ccp_alloc_struct - allocate and initialize the ccp_device struct
  335. *
  336. * @dev: device struct of the CCP
  337. */
  338. struct ccp_device *ccp_alloc_struct(struct device *dev)
  339. {
  340. struct ccp_device *ccp;
  341. ccp = devm_kzalloc(dev, sizeof(*ccp), GFP_KERNEL);
  342. if (!ccp)
  343. return NULL;
  344. ccp->dev = dev;
  345. INIT_LIST_HEAD(&ccp->cmd);
  346. INIT_LIST_HEAD(&ccp->backlog);
  347. spin_lock_init(&ccp->cmd_lock);
  348. mutex_init(&ccp->req_mutex);
  349. mutex_init(&ccp->sb_mutex);
  350. ccp->sb_count = KSB_COUNT;
  351. ccp->sb_start = 0;
  352. ccp->ord = ccp_increment_unit_ordinal();
  353. snprintf(ccp->name, MAX_CCP_NAME_LEN, "ccp-%u", ccp->ord);
  354. snprintf(ccp->rngname, MAX_CCP_NAME_LEN, "ccp-%u-rng", ccp->ord);
  355. return ccp;
  356. }
  357. int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  358. {
  359. struct ccp_device *ccp = container_of(rng, struct ccp_device, hwrng);
  360. u32 trng_value;
  361. int len = min_t(int, sizeof(trng_value), max);
  362. /* Locking is provided by the caller so we can update device
  363. * hwrng-related fields safely
  364. */
  365. trng_value = ioread32(ccp->io_regs + TRNG_OUT_REG);
  366. if (!trng_value) {
  367. /* Zero is returned if not data is available or if a
  368. * bad-entropy error is present. Assume an error if
  369. * we exceed TRNG_RETRIES reads of zero.
  370. */
  371. if (ccp->hwrng_retries++ > TRNG_RETRIES)
  372. return -EIO;
  373. return 0;
  374. }
  375. /* Reset the counter and save the rng value */
  376. ccp->hwrng_retries = 0;
  377. memcpy(data, &trng_value, len);
  378. return len;
  379. }
  380. #ifdef CONFIG_PM
  381. bool ccp_queues_suspended(struct ccp_device *ccp)
  382. {
  383. unsigned int suspended = 0;
  384. unsigned long flags;
  385. unsigned int i;
  386. spin_lock_irqsave(&ccp->cmd_lock, flags);
  387. for (i = 0; i < ccp->cmd_q_count; i++)
  388. if (ccp->cmd_q[i].suspended)
  389. suspended++;
  390. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  391. return ccp->cmd_q_count == suspended;
  392. }
  393. #endif
  394. static int __init ccp_mod_init(void)
  395. {
  396. #ifdef CONFIG_X86
  397. int ret;
  398. ret = ccp_pci_init();
  399. if (ret)
  400. return ret;
  401. /* Don't leave the driver loaded if init failed */
  402. if (ccp_present() != 0) {
  403. ccp_pci_exit();
  404. return -ENODEV;
  405. }
  406. return 0;
  407. #endif
  408. #ifdef CONFIG_ARM64
  409. int ret;
  410. ret = ccp_platform_init();
  411. if (ret)
  412. return ret;
  413. /* Don't leave the driver loaded if init failed */
  414. if (ccp_present() != 0) {
  415. ccp_platform_exit();
  416. return -ENODEV;
  417. }
  418. return 0;
  419. #endif
  420. return -ENODEV;
  421. }
  422. static void __exit ccp_mod_exit(void)
  423. {
  424. #ifdef CONFIG_X86
  425. ccp_pci_exit();
  426. #endif
  427. #ifdef CONFIG_ARM64
  428. ccp_platform_exit();
  429. #endif
  430. }
  431. module_init(ccp_mod_init);
  432. module_exit(ccp_mod_exit);