caamrng.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * caam - Freescale FSL CAAM support for hw_random
  4. *
  5. * Copyright 2011 Freescale Semiconductor, Inc.
  6. *
  7. * Based on caamalg.c crypto API driver.
  8. *
  9. * relationship between job descriptors to shared descriptors:
  10. *
  11. * --------------- --------------
  12. * | JobDesc #0 |-------------------->| ShareDesc |
  13. * | *(buffer 0) | |------------->| (generate) |
  14. * --------------- | | (move) |
  15. * | | (store) |
  16. * --------------- | --------------
  17. * | JobDesc #1 |------|
  18. * | *(buffer 1) |
  19. * ---------------
  20. *
  21. * A job desc looks like this:
  22. *
  23. * ---------------------
  24. * | Header |
  25. * | ShareDesc Pointer |
  26. * | SEQ_OUT_PTR |
  27. * | (output buffer) |
  28. * ---------------------
  29. *
  30. * The SharedDesc never changes, and each job descriptor points to one of two
  31. * buffers for each device, from which the data will be copied into the
  32. * requested destination
  33. */
  34. #include <linux/hw_random.h>
  35. #include <linux/completion.h>
  36. #include <linux/atomic.h>
  37. #include "compat.h"
  38. #include "regs.h"
  39. #include "intern.h"
  40. #include "desc_constr.h"
  41. #include "jr.h"
  42. #include "error.h"
  43. /*
  44. * Maximum buffer size: maximum number of random, cache-aligned bytes that
  45. * will be generated and moved to seq out ptr (extlen not allowed)
  46. */
  47. #define RN_BUF_SIZE (0xffff / L1_CACHE_BYTES * \
  48. L1_CACHE_BYTES)
  49. /* length of descriptors */
  50. #define DESC_JOB_O_LEN (CAAM_CMD_SZ * 2 + CAAM_PTR_SZ * 2)
  51. #define DESC_RNG_LEN (3 * CAAM_CMD_SZ)
  52. /* Buffer, its dma address and lock */
  53. struct buf_data {
  54. u8 buf[RN_BUF_SIZE] ____cacheline_aligned;
  55. dma_addr_t addr;
  56. struct completion filled;
  57. u32 hw_desc[DESC_JOB_O_LEN];
  58. #define BUF_NOT_EMPTY 0
  59. #define BUF_EMPTY 1
  60. #define BUF_PENDING 2 /* Empty, but with job pending --don't submit another */
  61. atomic_t empty;
  62. };
  63. /* rng per-device context */
  64. struct caam_rng_ctx {
  65. struct device *jrdev;
  66. dma_addr_t sh_desc_dma;
  67. u32 sh_desc[DESC_RNG_LEN];
  68. unsigned int cur_buf_idx;
  69. int current_buf;
  70. struct buf_data bufs[2];
  71. };
  72. static struct caam_rng_ctx *rng_ctx;
  73. static inline void rng_unmap_buf(struct device *jrdev, struct buf_data *bd)
  74. {
  75. if (bd->addr)
  76. dma_unmap_single(jrdev, bd->addr, RN_BUF_SIZE,
  77. DMA_FROM_DEVICE);
  78. }
  79. static inline void rng_unmap_ctx(struct caam_rng_ctx *ctx)
  80. {
  81. struct device *jrdev = ctx->jrdev;
  82. if (ctx->sh_desc_dma)
  83. dma_unmap_single(jrdev, ctx->sh_desc_dma,
  84. desc_bytes(ctx->sh_desc), DMA_TO_DEVICE);
  85. rng_unmap_buf(jrdev, &ctx->bufs[0]);
  86. rng_unmap_buf(jrdev, &ctx->bufs[1]);
  87. }
  88. static void rng_done(struct device *jrdev, u32 *desc, u32 err, void *context)
  89. {
  90. struct buf_data *bd;
  91. bd = container_of(desc, struct buf_data, hw_desc[0]);
  92. if (err)
  93. caam_jr_strstatus(jrdev, err);
  94. atomic_set(&bd->empty, BUF_NOT_EMPTY);
  95. complete(&bd->filled);
  96. /* Buffer refilled, invalidate cache */
  97. dma_sync_single_for_cpu(jrdev, bd->addr, RN_BUF_SIZE, DMA_FROM_DEVICE);
  98. #ifdef DEBUG
  99. print_hex_dump(KERN_ERR, "rng refreshed buf@: ",
  100. DUMP_PREFIX_ADDRESS, 16, 4, bd->buf, RN_BUF_SIZE, 1);
  101. #endif
  102. }
  103. static inline int submit_job(struct caam_rng_ctx *ctx, int to_current)
  104. {
  105. struct buf_data *bd = &ctx->bufs[!(to_current ^ ctx->current_buf)];
  106. struct device *jrdev = ctx->jrdev;
  107. u32 *desc = bd->hw_desc;
  108. int err;
  109. dev_dbg(jrdev, "submitting job %d\n", !(to_current ^ ctx->current_buf));
  110. init_completion(&bd->filled);
  111. err = caam_jr_enqueue(jrdev, desc, rng_done, ctx);
  112. if (err)
  113. complete(&bd->filled); /* don't wait on failed job*/
  114. else
  115. atomic_inc(&bd->empty); /* note if pending */
  116. return err;
  117. }
  118. static int caam_read(struct hwrng *rng, void *data, size_t max, bool wait)
  119. {
  120. struct caam_rng_ctx *ctx = rng_ctx;
  121. struct buf_data *bd = &ctx->bufs[ctx->current_buf];
  122. int next_buf_idx, copied_idx;
  123. int err;
  124. if (atomic_read(&bd->empty)) {
  125. /* try to submit job if there wasn't one */
  126. if (atomic_read(&bd->empty) == BUF_EMPTY) {
  127. err = submit_job(ctx, 1);
  128. /* if can't submit job, can't even wait */
  129. if (err)
  130. return 0;
  131. }
  132. /* no immediate data, so exit if not waiting */
  133. if (!wait)
  134. return 0;
  135. /* waiting for pending job */
  136. if (atomic_read(&bd->empty))
  137. wait_for_completion(&bd->filled);
  138. }
  139. next_buf_idx = ctx->cur_buf_idx + max;
  140. dev_dbg(ctx->jrdev, "%s: start reading at buffer %d, idx %d\n",
  141. __func__, ctx->current_buf, ctx->cur_buf_idx);
  142. /* if enough data in current buffer */
  143. if (next_buf_idx < RN_BUF_SIZE) {
  144. memcpy(data, bd->buf + ctx->cur_buf_idx, max);
  145. ctx->cur_buf_idx = next_buf_idx;
  146. return max;
  147. }
  148. /* else, copy what's left... */
  149. copied_idx = RN_BUF_SIZE - ctx->cur_buf_idx;
  150. memcpy(data, bd->buf + ctx->cur_buf_idx, copied_idx);
  151. ctx->cur_buf_idx = 0;
  152. atomic_set(&bd->empty, BUF_EMPTY);
  153. /* ...refill... */
  154. submit_job(ctx, 1);
  155. /* and use next buffer */
  156. ctx->current_buf = !ctx->current_buf;
  157. dev_dbg(ctx->jrdev, "switched to buffer %d\n", ctx->current_buf);
  158. /* since there already is some data read, don't wait */
  159. return copied_idx + caam_read(rng, data + copied_idx,
  160. max - copied_idx, false);
  161. }
  162. static inline int rng_create_sh_desc(struct caam_rng_ctx *ctx)
  163. {
  164. struct device *jrdev = ctx->jrdev;
  165. u32 *desc = ctx->sh_desc;
  166. init_sh_desc(desc, HDR_SHARE_SERIAL);
  167. /* Generate random bytes */
  168. append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG);
  169. /* Store bytes */
  170. append_seq_fifo_store(desc, RN_BUF_SIZE, FIFOST_TYPE_RNGSTORE);
  171. ctx->sh_desc_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
  172. DMA_TO_DEVICE);
  173. if (dma_mapping_error(jrdev, ctx->sh_desc_dma)) {
  174. dev_err(jrdev, "unable to map shared descriptor\n");
  175. return -ENOMEM;
  176. }
  177. #ifdef DEBUG
  178. print_hex_dump(KERN_ERR, "rng shdesc@: ", DUMP_PREFIX_ADDRESS, 16, 4,
  179. desc, desc_bytes(desc), 1);
  180. #endif
  181. return 0;
  182. }
  183. static inline int rng_create_job_desc(struct caam_rng_ctx *ctx, int buf_id)
  184. {
  185. struct device *jrdev = ctx->jrdev;
  186. struct buf_data *bd = &ctx->bufs[buf_id];
  187. u32 *desc = bd->hw_desc;
  188. int sh_len = desc_len(ctx->sh_desc);
  189. init_job_desc_shared(desc, ctx->sh_desc_dma, sh_len, HDR_SHARE_DEFER |
  190. HDR_REVERSE);
  191. bd->addr = dma_map_single(jrdev, bd->buf, RN_BUF_SIZE, DMA_FROM_DEVICE);
  192. if (dma_mapping_error(jrdev, bd->addr)) {
  193. dev_err(jrdev, "unable to map dst\n");
  194. return -ENOMEM;
  195. }
  196. append_seq_out_ptr_intlen(desc, bd->addr, RN_BUF_SIZE, 0);
  197. #ifdef DEBUG
  198. print_hex_dump(KERN_ERR, "rng job desc@: ", DUMP_PREFIX_ADDRESS, 16, 4,
  199. desc, desc_bytes(desc), 1);
  200. #endif
  201. return 0;
  202. }
  203. static void caam_cleanup(struct hwrng *rng)
  204. {
  205. int i;
  206. struct buf_data *bd;
  207. for (i = 0; i < 2; i++) {
  208. bd = &rng_ctx->bufs[i];
  209. if (atomic_read(&bd->empty) == BUF_PENDING)
  210. wait_for_completion(&bd->filled);
  211. }
  212. rng_unmap_ctx(rng_ctx);
  213. }
  214. static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id)
  215. {
  216. struct buf_data *bd = &ctx->bufs[buf_id];
  217. int err;
  218. err = rng_create_job_desc(ctx, buf_id);
  219. if (err)
  220. return err;
  221. atomic_set(&bd->empty, BUF_EMPTY);
  222. submit_job(ctx, buf_id == ctx->current_buf);
  223. wait_for_completion(&bd->filled);
  224. return 0;
  225. }
  226. static int caam_init_rng(struct caam_rng_ctx *ctx, struct device *jrdev)
  227. {
  228. int err;
  229. ctx->jrdev = jrdev;
  230. err = rng_create_sh_desc(ctx);
  231. if (err)
  232. return err;
  233. ctx->current_buf = 0;
  234. ctx->cur_buf_idx = 0;
  235. err = caam_init_buf(ctx, 0);
  236. if (err)
  237. return err;
  238. return caam_init_buf(ctx, 1);
  239. }
  240. static struct hwrng caam_rng = {
  241. .name = "rng-caam",
  242. .cleanup = caam_cleanup,
  243. .read = caam_read,
  244. };
  245. static void __exit caam_rng_exit(void)
  246. {
  247. caam_jr_free(rng_ctx->jrdev);
  248. hwrng_unregister(&caam_rng);
  249. kfree(rng_ctx);
  250. }
  251. static int __init caam_rng_init(void)
  252. {
  253. struct device *dev;
  254. struct device_node *dev_node;
  255. struct platform_device *pdev;
  256. struct device *ctrldev;
  257. struct caam_drv_private *priv;
  258. int err;
  259. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
  260. if (!dev_node) {
  261. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
  262. if (!dev_node)
  263. return -ENODEV;
  264. }
  265. pdev = of_find_device_by_node(dev_node);
  266. if (!pdev) {
  267. of_node_put(dev_node);
  268. return -ENODEV;
  269. }
  270. ctrldev = &pdev->dev;
  271. priv = dev_get_drvdata(ctrldev);
  272. of_node_put(dev_node);
  273. /*
  274. * If priv is NULL, it's probably because the caam driver wasn't
  275. * properly initialized (e.g. RNG4 init failed). Thus, bail out here.
  276. */
  277. if (!priv)
  278. return -ENODEV;
  279. /* Check for an instantiated RNG before registration */
  280. if (!(rd_reg32(&priv->ctrl->perfmon.cha_num_ls) & CHA_ID_LS_RNG_MASK))
  281. return -ENODEV;
  282. dev = caam_jr_alloc();
  283. if (IS_ERR(dev)) {
  284. pr_err("Job Ring Device allocation for transform failed\n");
  285. return PTR_ERR(dev);
  286. }
  287. rng_ctx = kmalloc(sizeof(*rng_ctx), GFP_DMA | GFP_KERNEL);
  288. if (!rng_ctx) {
  289. err = -ENOMEM;
  290. goto free_caam_alloc;
  291. }
  292. err = caam_init_rng(rng_ctx, dev);
  293. if (err)
  294. goto free_rng_ctx;
  295. dev_info(dev, "registering rng-caam\n");
  296. return hwrng_register(&caam_rng);
  297. free_rng_ctx:
  298. kfree(rng_ctx);
  299. free_caam_alloc:
  300. caam_jr_free(dev);
  301. return err;
  302. }
  303. module_init(caam_rng_init);
  304. module_exit(caam_rng_exit);
  305. MODULE_LICENSE("GPL");
  306. MODULE_DESCRIPTION("FSL CAAM support for hw_random API");
  307. MODULE_AUTHOR("Freescale Semiconductor - NMG");