ccp-dev.c 11 KB

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