caamrng.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. /* 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. /* Propagate errors from shared to job descriptor */
  168. append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
  169. /* Generate random bytes */
  170. append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG);
  171. /* Store bytes */
  172. append_seq_fifo_store(desc, RN_BUF_SIZE, FIFOST_TYPE_RNGSTORE);
  173. ctx->sh_desc_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
  174. DMA_TO_DEVICE);
  175. if (dma_mapping_error(jrdev, ctx->sh_desc_dma)) {
  176. dev_err(jrdev, "unable to map shared descriptor\n");
  177. return -ENOMEM;
  178. }
  179. #ifdef DEBUG
  180. print_hex_dump(KERN_ERR, "rng shdesc@: ", DUMP_PREFIX_ADDRESS, 16, 4,
  181. desc, desc_bytes(desc), 1);
  182. #endif
  183. return 0;
  184. }
  185. static inline int rng_create_job_desc(struct caam_rng_ctx *ctx, int buf_id)
  186. {
  187. struct device *jrdev = ctx->jrdev;
  188. struct buf_data *bd = &ctx->bufs[buf_id];
  189. u32 *desc = bd->hw_desc;
  190. int sh_len = desc_len(ctx->sh_desc);
  191. init_job_desc_shared(desc, ctx->sh_desc_dma, sh_len, HDR_SHARE_DEFER |
  192. HDR_REVERSE);
  193. bd->addr = dma_map_single(jrdev, bd->buf, RN_BUF_SIZE, DMA_FROM_DEVICE);
  194. if (dma_mapping_error(jrdev, bd->addr)) {
  195. dev_err(jrdev, "unable to map dst\n");
  196. return -ENOMEM;
  197. }
  198. append_seq_out_ptr_intlen(desc, bd->addr, RN_BUF_SIZE, 0);
  199. #ifdef DEBUG
  200. print_hex_dump(KERN_ERR, "rng job desc@: ", DUMP_PREFIX_ADDRESS, 16, 4,
  201. desc, desc_bytes(desc), 1);
  202. #endif
  203. return 0;
  204. }
  205. static void caam_cleanup(struct hwrng *rng)
  206. {
  207. int i;
  208. struct buf_data *bd;
  209. for (i = 0; i < 2; i++) {
  210. bd = &rng_ctx->bufs[i];
  211. if (atomic_read(&bd->empty) == BUF_PENDING)
  212. wait_for_completion(&bd->filled);
  213. }
  214. rng_unmap_ctx(rng_ctx);
  215. }
  216. static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id)
  217. {
  218. struct buf_data *bd = &ctx->bufs[buf_id];
  219. int err;
  220. err = rng_create_job_desc(ctx, buf_id);
  221. if (err)
  222. return err;
  223. atomic_set(&bd->empty, BUF_EMPTY);
  224. submit_job(ctx, buf_id == ctx->current_buf);
  225. wait_for_completion(&bd->filled);
  226. return 0;
  227. }
  228. static int caam_init_rng(struct caam_rng_ctx *ctx, struct device *jrdev)
  229. {
  230. int err;
  231. ctx->jrdev = jrdev;
  232. err = rng_create_sh_desc(ctx);
  233. if (err)
  234. return err;
  235. ctx->current_buf = 0;
  236. ctx->cur_buf_idx = 0;
  237. err = caam_init_buf(ctx, 0);
  238. if (err)
  239. return err;
  240. err = caam_init_buf(ctx, 1);
  241. if (err)
  242. return err;
  243. return 0;
  244. }
  245. static struct hwrng caam_rng = {
  246. .name = "rng-caam",
  247. .cleanup = caam_cleanup,
  248. .read = caam_read,
  249. };
  250. static void __exit caam_rng_exit(void)
  251. {
  252. caam_jr_free(rng_ctx->jrdev);
  253. hwrng_unregister(&caam_rng);
  254. kfree(rng_ctx);
  255. }
  256. static int __init caam_rng_init(void)
  257. {
  258. struct device *dev;
  259. struct device_node *dev_node;
  260. struct platform_device *pdev;
  261. struct device *ctrldev;
  262. struct caam_drv_private *priv;
  263. int err;
  264. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
  265. if (!dev_node) {
  266. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
  267. if (!dev_node)
  268. return -ENODEV;
  269. }
  270. pdev = of_find_device_by_node(dev_node);
  271. if (!pdev) {
  272. of_node_put(dev_node);
  273. return -ENODEV;
  274. }
  275. ctrldev = &pdev->dev;
  276. priv = dev_get_drvdata(ctrldev);
  277. of_node_put(dev_node);
  278. /*
  279. * If priv is NULL, it's probably because the caam driver wasn't
  280. * properly initialized (e.g. RNG4 init failed). Thus, bail out here.
  281. */
  282. if (!priv)
  283. return -ENODEV;
  284. /* Check for an instantiated RNG before registration */
  285. if (!(rd_reg32(&priv->ctrl->perfmon.cha_num_ls) & CHA_ID_LS_RNG_MASK))
  286. return -ENODEV;
  287. dev = caam_jr_alloc();
  288. if (IS_ERR(dev)) {
  289. pr_err("Job Ring Device allocation for transform failed\n");
  290. return PTR_ERR(dev);
  291. }
  292. rng_ctx = kmalloc(sizeof(*rng_ctx), GFP_DMA);
  293. if (!rng_ctx) {
  294. err = -ENOMEM;
  295. goto free_caam_alloc;
  296. }
  297. err = caam_init_rng(rng_ctx, dev);
  298. if (err)
  299. goto free_rng_ctx;
  300. dev_info(dev, "registering rng-caam\n");
  301. return hwrng_register(&caam_rng);
  302. free_rng_ctx:
  303. kfree(rng_ctx);
  304. free_caam_alloc:
  305. caam_jr_free(dev);
  306. return err;
  307. }
  308. module_init(caam_rng_init);
  309. module_exit(caam_rng_exit);
  310. MODULE_LICENSE("GPL");
  311. MODULE_DESCRIPTION("FSL CAAM support for hw_random API");
  312. MODULE_AUTHOR("Freescale Semiconductor - NMG");