caamrng.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 (3 * 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 = container_of(desc, struct buf_data, hw_desc[0]);
  91. if (err)
  92. caam_jr_strstatus(jrdev, err);
  93. atomic_set(&bd->empty, BUF_NOT_EMPTY);
  94. complete(&bd->filled);
  95. /* Buffer refilled, invalidate cache */
  96. dma_sync_single_for_cpu(jrdev, bd->addr, RN_BUF_SIZE, DMA_FROM_DEVICE);
  97. #ifdef DEBUG
  98. print_hex_dump(KERN_ERR, "rng refreshed buf@: ",
  99. DUMP_PREFIX_ADDRESS, 16, 4, bd->buf, RN_BUF_SIZE, 1);
  100. #endif
  101. }
  102. static inline int submit_job(struct caam_rng_ctx *ctx, int to_current)
  103. {
  104. struct buf_data *bd = &ctx->bufs[!(to_current ^ ctx->current_buf)];
  105. struct device *jrdev = ctx->jrdev;
  106. u32 *desc = bd->hw_desc;
  107. int err;
  108. dev_dbg(jrdev, "submitting job %d\n", !(to_current ^ ctx->current_buf));
  109. init_completion(&bd->filled);
  110. err = caam_jr_enqueue(jrdev, desc, rng_done, ctx);
  111. if (err)
  112. complete(&bd->filled); /* don't wait on failed job*/
  113. else
  114. atomic_inc(&bd->empty); /* note if pending */
  115. return err;
  116. }
  117. static int caam_read(struct hwrng *rng, void *data, size_t max, bool wait)
  118. {
  119. struct caam_rng_ctx *ctx = rng_ctx;
  120. struct buf_data *bd = &ctx->bufs[ctx->current_buf];
  121. int next_buf_idx, copied_idx;
  122. int err;
  123. if (atomic_read(&bd->empty)) {
  124. /* try to submit job if there wasn't one */
  125. if (atomic_read(&bd->empty) == BUF_EMPTY) {
  126. err = submit_job(ctx, 1);
  127. /* if can't submit job, can't even wait */
  128. if (err)
  129. return 0;
  130. }
  131. /* no immediate data, so exit if not waiting */
  132. if (!wait)
  133. return 0;
  134. /* waiting for pending job */
  135. if (atomic_read(&bd->empty))
  136. wait_for_completion(&bd->filled);
  137. }
  138. next_buf_idx = ctx->cur_buf_idx + max;
  139. dev_dbg(ctx->jrdev, "%s: start reading at buffer %d, idx %d\n",
  140. __func__, ctx->current_buf, ctx->cur_buf_idx);
  141. /* if enough data in current buffer */
  142. if (next_buf_idx < RN_BUF_SIZE) {
  143. memcpy(data, bd->buf + ctx->cur_buf_idx, max);
  144. ctx->cur_buf_idx = next_buf_idx;
  145. return max;
  146. }
  147. /* else, copy what's left... */
  148. copied_idx = RN_BUF_SIZE - ctx->cur_buf_idx;
  149. memcpy(data, bd->buf + ctx->cur_buf_idx, copied_idx);
  150. ctx->cur_buf_idx = 0;
  151. atomic_set(&bd->empty, BUF_EMPTY);
  152. /* ...refill... */
  153. submit_job(ctx, 1);
  154. /* and use next buffer */
  155. ctx->current_buf = !ctx->current_buf;
  156. dev_dbg(ctx->jrdev, "switched to buffer %d\n", ctx->current_buf);
  157. /* since there already is some data read, don't wait */
  158. return copied_idx + caam_read(rng, data + copied_idx,
  159. max - copied_idx, false);
  160. }
  161. static inline int rng_create_sh_desc(struct caam_rng_ctx *ctx)
  162. {
  163. struct device *jrdev = ctx->jrdev;
  164. u32 *desc = ctx->sh_desc;
  165. init_sh_desc(desc, HDR_SHARE_SERIAL);
  166. /* Generate random bytes */
  167. append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG);
  168. /* Store bytes */
  169. append_seq_fifo_store(desc, RN_BUF_SIZE, FIFOST_TYPE_RNGSTORE);
  170. ctx->sh_desc_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
  171. DMA_TO_DEVICE);
  172. if (dma_mapping_error(jrdev, ctx->sh_desc_dma)) {
  173. dev_err(jrdev, "unable to map shared descriptor\n");
  174. return -ENOMEM;
  175. }
  176. #ifdef DEBUG
  177. print_hex_dump(KERN_ERR, "rng shdesc@: ", DUMP_PREFIX_ADDRESS, 16, 4,
  178. desc, desc_bytes(desc), 1);
  179. #endif
  180. return 0;
  181. }
  182. static inline int rng_create_job_desc(struct caam_rng_ctx *ctx, int buf_id)
  183. {
  184. struct device *jrdev = ctx->jrdev;
  185. struct buf_data *bd = &ctx->bufs[buf_id];
  186. u32 *desc = bd->hw_desc;
  187. int sh_len = desc_len(ctx->sh_desc);
  188. init_job_desc_shared(desc, ctx->sh_desc_dma, sh_len, HDR_SHARE_DEFER |
  189. HDR_REVERSE);
  190. bd->addr = dma_map_single(jrdev, bd->buf, RN_BUF_SIZE, DMA_FROM_DEVICE);
  191. if (dma_mapping_error(jrdev, bd->addr)) {
  192. dev_err(jrdev, "unable to map dst\n");
  193. return -ENOMEM;
  194. }
  195. append_seq_out_ptr_intlen(desc, bd->addr, RN_BUF_SIZE, 0);
  196. #ifdef DEBUG
  197. print_hex_dump(KERN_ERR, "rng job desc@: ", DUMP_PREFIX_ADDRESS, 16, 4,
  198. desc, desc_bytes(desc), 1);
  199. #endif
  200. return 0;
  201. }
  202. static void caam_cleanup(struct hwrng *rng)
  203. {
  204. int i;
  205. struct buf_data *bd;
  206. for (i = 0; i < 2; i++) {
  207. bd = &rng_ctx->bufs[i];
  208. if (atomic_read(&bd->empty) == BUF_PENDING)
  209. wait_for_completion(&bd->filled);
  210. }
  211. rng_unmap_ctx(rng_ctx);
  212. }
  213. static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id)
  214. {
  215. struct buf_data *bd = &ctx->bufs[buf_id];
  216. int err;
  217. err = rng_create_job_desc(ctx, buf_id);
  218. if (err)
  219. return err;
  220. atomic_set(&bd->empty, BUF_EMPTY);
  221. submit_job(ctx, buf_id == ctx->current_buf);
  222. wait_for_completion(&bd->filled);
  223. return 0;
  224. }
  225. static int caam_init_rng(struct caam_rng_ctx *ctx, struct device *jrdev)
  226. {
  227. int err;
  228. ctx->jrdev = jrdev;
  229. err = rng_create_sh_desc(ctx);
  230. if (err)
  231. return err;
  232. ctx->current_buf = 0;
  233. ctx->cur_buf_idx = 0;
  234. err = caam_init_buf(ctx, 0);
  235. if (err)
  236. return err;
  237. return caam_init_buf(ctx, 1);
  238. }
  239. static struct hwrng caam_rng = {
  240. .name = "rng-caam",
  241. .cleanup = caam_cleanup,
  242. .read = caam_read,
  243. };
  244. static void __exit caam_rng_exit(void)
  245. {
  246. caam_jr_free(rng_ctx->jrdev);
  247. hwrng_unregister(&caam_rng);
  248. kfree(rng_ctx);
  249. }
  250. static int __init caam_rng_init(void)
  251. {
  252. struct device *dev;
  253. struct device_node *dev_node;
  254. struct platform_device *pdev;
  255. struct device *ctrldev;
  256. struct caam_drv_private *priv;
  257. int err;
  258. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
  259. if (!dev_node) {
  260. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
  261. if (!dev_node)
  262. return -ENODEV;
  263. }
  264. pdev = of_find_device_by_node(dev_node);
  265. if (!pdev) {
  266. of_node_put(dev_node);
  267. return -ENODEV;
  268. }
  269. ctrldev = &pdev->dev;
  270. priv = dev_get_drvdata(ctrldev);
  271. of_node_put(dev_node);
  272. /*
  273. * If priv is NULL, it's probably because the caam driver wasn't
  274. * properly initialized (e.g. RNG4 init failed). Thus, bail out here.
  275. */
  276. if (!priv)
  277. return -ENODEV;
  278. /* Check for an instantiated RNG before registration */
  279. if (!(rd_reg32(&priv->ctrl->perfmon.cha_num_ls) & CHA_ID_LS_RNG_MASK))
  280. return -ENODEV;
  281. dev = caam_jr_alloc();
  282. if (IS_ERR(dev)) {
  283. pr_err("Job Ring Device allocation for transform failed\n");
  284. return PTR_ERR(dev);
  285. }
  286. rng_ctx = kmalloc(sizeof(*rng_ctx), GFP_DMA | GFP_KERNEL);
  287. if (!rng_ctx) {
  288. err = -ENOMEM;
  289. goto free_caam_alloc;
  290. }
  291. err = caam_init_rng(rng_ctx, dev);
  292. if (err)
  293. goto free_rng_ctx;
  294. dev_info(dev, "registering rng-caam\n");
  295. err = hwrng_register(&caam_rng);
  296. if (!err)
  297. return err;
  298. free_rng_ctx:
  299. kfree(rng_ctx);
  300. free_caam_alloc:
  301. caam_jr_free(dev);
  302. return err;
  303. }
  304. module_init(caam_rng_init);
  305. module_exit(caam_rng_exit);
  306. MODULE_LICENSE("GPL");
  307. MODULE_DESCRIPTION("FSL CAAM support for hw_random API");
  308. MODULE_AUTHOR("Freescale Semiconductor - NMG");