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