caamrng.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * caam - Freescale FSL CAAM support for hw_random
  3. *
  4. * Copyright 2011 Freescale Semiconductor, Inc.
  5. *
  6. * Based on caamalg.c crypto API driver.
  7. *
  8. * relationship between job descriptors to shared descriptors:
  9. *
  10. * --------------- --------------
  11. * | JobDesc #0 |-------------------->| ShareDesc |
  12. * | *(buffer 0) | |------------->| (generate) |
  13. * --------------- | | (move) |
  14. * | | (store) |
  15. * --------------- | --------------
  16. * | JobDesc #1 |------|
  17. * | *(buffer 1) |
  18. * ---------------
  19. *
  20. * A job desc looks like this:
  21. *
  22. * ---------------------
  23. * | Header |
  24. * | ShareDesc Pointer |
  25. * | SEQ_OUT_PTR |
  26. * | (output buffer) |
  27. * ---------------------
  28. *
  29. * The SharedDesc never changes, and each job descriptor points to one of two
  30. * buffers for each device, from which the data will be copied into the
  31. * requested destination
  32. */
  33. #include <linux/hw_random.h>
  34. #include <linux/completion.h>
  35. #include <linux/atomic.h>
  36. #include "compat.h"
  37. #include "regs.h"
  38. #include "intern.h"
  39. #include "desc_constr.h"
  40. #include "jr.h"
  41. #include "error.h"
  42. /*
  43. * Maximum buffer size: maximum number of random, cache-aligned bytes that
  44. * will be generated and moved to seq out ptr (extlen not allowed)
  45. */
  46. #define RN_BUF_SIZE (0xffff / L1_CACHE_BYTES * \
  47. L1_CACHE_BYTES)
  48. /* length of descriptors */
  49. #define DESC_JOB_O_LEN (CAAM_CMD_SZ * 2 + CAAM_PTR_SZ * 2)
  50. #define DESC_RNG_LEN (4 * CAAM_CMD_SZ)
  51. /* Buffer, its dma address and lock */
  52. struct buf_data {
  53. u8 buf[RN_BUF_SIZE] ____cacheline_aligned;
  54. dma_addr_t addr;
  55. struct completion filled;
  56. u32 hw_desc[DESC_JOB_O_LEN];
  57. #define BUF_NOT_EMPTY 0
  58. #define BUF_EMPTY 1
  59. #define BUF_PENDING 2 /* Empty, but with job pending --don't submit another */
  60. atomic_t empty;
  61. };
  62. /* rng per-device context */
  63. struct caam_rng_ctx {
  64. struct device *jrdev;
  65. dma_addr_t sh_desc_dma;
  66. u32 sh_desc[DESC_RNG_LEN];
  67. unsigned int cur_buf_idx;
  68. int current_buf;
  69. struct buf_data bufs[2];
  70. };
  71. static struct caam_rng_ctx *rng_ctx;
  72. static inline void rng_unmap_buf(struct device *jrdev, struct buf_data *bd)
  73. {
  74. if (bd->addr)
  75. dma_unmap_single(jrdev, bd->addr, RN_BUF_SIZE,
  76. DMA_FROM_DEVICE);
  77. }
  78. static inline void rng_unmap_ctx(struct caam_rng_ctx *ctx)
  79. {
  80. struct device *jrdev = ctx->jrdev;
  81. if (ctx->sh_desc_dma)
  82. dma_unmap_single(jrdev, ctx->sh_desc_dma,
  83. desc_bytes(ctx->sh_desc), DMA_TO_DEVICE);
  84. rng_unmap_buf(jrdev, &ctx->bufs[0]);
  85. rng_unmap_buf(jrdev, &ctx->bufs[1]);
  86. }
  87. static void rng_done(struct device *jrdev, u32 *desc, u32 err, void *context)
  88. {
  89. struct buf_data *bd;
  90. bd = (struct buf_data *)((char *)desc -
  91. offsetof(struct buf_data, hw_desc));
  92. if (err)
  93. caam_jr_strstatus(jrdev, err);
  94. atomic_set(&bd->empty, BUF_NOT_EMPTY);
  95. complete(&bd->filled);
  96. #ifdef DEBUG
  97. print_hex_dump(KERN_ERR, "rng refreshed buf@: ",
  98. DUMP_PREFIX_ADDRESS, 16, 4, bd->buf, RN_BUF_SIZE, 1);
  99. #endif
  100. }
  101. static inline int submit_job(struct caam_rng_ctx *ctx, int to_current)
  102. {
  103. struct buf_data *bd = &ctx->bufs[!(to_current ^ ctx->current_buf)];
  104. struct device *jrdev = ctx->jrdev;
  105. u32 *desc = bd->hw_desc;
  106. int err;
  107. dev_dbg(jrdev, "submitting job %d\n", !(to_current ^ ctx->current_buf));
  108. init_completion(&bd->filled);
  109. err = caam_jr_enqueue(jrdev, desc, rng_done, ctx);
  110. if (err)
  111. complete(&bd->filled); /* don't wait on failed job*/
  112. else
  113. atomic_inc(&bd->empty); /* note if pending */
  114. return err;
  115. }
  116. static int caam_read(struct hwrng *rng, void *data, size_t max, bool wait)
  117. {
  118. struct caam_rng_ctx *ctx = rng_ctx;
  119. struct buf_data *bd = &ctx->bufs[ctx->current_buf];
  120. int next_buf_idx, copied_idx;
  121. int err;
  122. if (atomic_read(&bd->empty)) {
  123. /* try to submit job if there wasn't one */
  124. if (atomic_read(&bd->empty) == BUF_EMPTY) {
  125. err = submit_job(ctx, 1);
  126. /* if can't submit job, can't even wait */
  127. if (err)
  128. return 0;
  129. }
  130. /* no immediate data, so exit if not waiting */
  131. if (!wait)
  132. return 0;
  133. /* waiting for pending job */
  134. if (atomic_read(&bd->empty))
  135. wait_for_completion(&bd->filled);
  136. }
  137. next_buf_idx = ctx->cur_buf_idx + max;
  138. dev_dbg(ctx->jrdev, "%s: start reading at buffer %d, idx %d\n",
  139. __func__, ctx->current_buf, ctx->cur_buf_idx);
  140. /* if enough data in current buffer */
  141. if (next_buf_idx < RN_BUF_SIZE) {
  142. memcpy(data, bd->buf + ctx->cur_buf_idx, max);
  143. ctx->cur_buf_idx = next_buf_idx;
  144. return max;
  145. }
  146. /* else, copy what's left... */
  147. copied_idx = RN_BUF_SIZE - ctx->cur_buf_idx;
  148. memcpy(data, bd->buf + ctx->cur_buf_idx, copied_idx);
  149. ctx->cur_buf_idx = 0;
  150. atomic_set(&bd->empty, BUF_EMPTY);
  151. /* ...refill... */
  152. submit_job(ctx, 1);
  153. /* and use next buffer */
  154. ctx->current_buf = !ctx->current_buf;
  155. dev_dbg(ctx->jrdev, "switched to buffer %d\n", ctx->current_buf);
  156. /* since there already is some data read, don't wait */
  157. return copied_idx + caam_read(rng, data + copied_idx,
  158. max - copied_idx, false);
  159. }
  160. static inline int rng_create_sh_desc(struct caam_rng_ctx *ctx)
  161. {
  162. struct device *jrdev = ctx->jrdev;
  163. u32 *desc = ctx->sh_desc;
  164. init_sh_desc(desc, HDR_SHARE_SERIAL);
  165. /* Propagate errors from shared to job descriptor */
  166. append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
  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. err = caam_init_buf(ctx, 1);
  239. if (err)
  240. return err;
  241. return 0;
  242. }
  243. static struct hwrng caam_rng = {
  244. .name = "rng-caam",
  245. .cleanup = caam_cleanup,
  246. .read = caam_read,
  247. };
  248. static void __exit caam_rng_exit(void)
  249. {
  250. caam_jr_free(rng_ctx->jrdev);
  251. hwrng_unregister(&caam_rng);
  252. kfree(rng_ctx);
  253. }
  254. static int __init caam_rng_init(void)
  255. {
  256. struct device *dev;
  257. struct device_node *dev_node;
  258. struct platform_device *pdev;
  259. struct device *ctrldev;
  260. void *priv;
  261. int err;
  262. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
  263. if (!dev_node) {
  264. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
  265. if (!dev_node)
  266. return -ENODEV;
  267. }
  268. pdev = of_find_device_by_node(dev_node);
  269. if (!pdev) {
  270. of_node_put(dev_node);
  271. return -ENODEV;
  272. }
  273. ctrldev = &pdev->dev;
  274. priv = dev_get_drvdata(ctrldev);
  275. of_node_put(dev_node);
  276. /*
  277. * If priv is NULL, it's probably because the caam driver wasn't
  278. * properly initialized (e.g. RNG4 init failed). Thus, bail out here.
  279. */
  280. if (!priv)
  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(struct caam_rng_ctx), GFP_DMA);
  288. if (!rng_ctx)
  289. return -ENOMEM;
  290. err = caam_init_rng(rng_ctx, dev);
  291. if (err)
  292. return err;
  293. dev_info(dev, "registering rng-caam\n");
  294. return hwrng_register(&caam_rng);
  295. }
  296. module_init(caam_rng_init);
  297. module_exit(caam_rng_exit);
  298. MODULE_LICENSE("GPL");
  299. MODULE_DESCRIPTION("FSL CAAM support for hw_random API");
  300. MODULE_AUTHOR("Freescale Semiconductor - NMG");