This source file includes following definitions.
- rng_unmap_buf
- rng_unmap_ctx
- rng_done
- submit_job
- caam_read
- rng_create_sh_desc
- rng_create_job_desc
- caam_cleanup
- caam_init_buf
- caam_init_rng
- caam_rng_exit
- caam_rng_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
50
51
52 #define RN_BUF_SIZE (0xffff / L1_CACHE_BYTES * \
53 L1_CACHE_BYTES)
54
55
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
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
68 atomic_t empty;
69 };
70
71
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
85
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
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);
138 else
139 atomic_inc(&bd->empty);
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
153 if (atomic_read(&bd->empty) == BUF_EMPTY) {
154 err = submit_job(ctx, 1);
155
156 if (err)
157 return 0;
158 }
159
160 if (!wait)
161 return 0;
162
163
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
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
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
186 submit_job(ctx, 1);
187
188
189 ctx->current_buf = !ctx->current_buf;
190 dev_dbg(ctx->jrdev, "switched to buffer %d\n", ctx->current_buf);
191
192
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
205 append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG);
206
207
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
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 }