1 // SPDX-License-Identifier: GPL-2.0+
3 * caam - Freescale FSL CAAM support for hw_random
5 * Copyright 2011 Freescale Semiconductor, Inc.
6 * Copyright 2018-2019 NXP
8 * Based on caamalg.c crypto API driver.
10 * relationship between job descriptors to shared descriptors:
12 * --------------- --------------
13 * | JobDesc #0 |-------------------->| ShareDesc |
14 * | *(buffer 0) | |------------->| (generate) |
15 * --------------- | | (move) |
17 * --------------- | --------------
18 * | JobDesc #1 |------|
22 * A job desc looks like this:
24 * ---------------------
26 * | ShareDesc Pointer |
29 * ---------------------
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
36 #include <linux/hw_random.h>
37 #include <linux/completion.h>
38 #include <linux/atomic.h>
44 #include "desc_constr.h"
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)
52 #define RN_BUF_SIZE (0xffff / L1_CACHE_BYTES * \
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)
59 /* Buffer, its dma address and lock */
61 u8 buf
[RN_BUF_SIZE
] ____cacheline_aligned
;
63 struct completion filled
;
64 u32 hw_desc
[DESC_JOB_O_LEN
];
65 #define BUF_NOT_EMPTY 0
67 #define BUF_PENDING 2 /* Empty, but with job pending --don't submit another */
71 /* rng per-device context */
74 dma_addr_t sh_desc_dma
;
75 u32 sh_desc
[DESC_RNG_LEN
];
76 unsigned int cur_buf_idx
;
78 struct buf_data bufs
[2];
81 static struct caam_rng_ctx
*rng_ctx
;
84 * Variable used to avoid double free of resources in case
85 * algorithm registration was unsuccessful
87 static bool init_done
;
89 static inline void rng_unmap_buf(struct device
*jrdev
, struct buf_data
*bd
)
92 dma_unmap_single(jrdev
, bd
->addr
, RN_BUF_SIZE
,
96 static inline void rng_unmap_ctx(struct caam_rng_ctx
*ctx
)
98 struct device
*jrdev
= ctx
->jrdev
;
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]);
107 static void rng_done(struct device
*jrdev
, u32
*desc
, u32 err
, void *context
)
111 bd
= container_of(desc
, struct buf_data
, hw_desc
[0]);
114 caam_jr_strstatus(jrdev
, err
);
116 atomic_set(&bd
->empty
, BUF_NOT_EMPTY
);
117 complete(&bd
->filled
);
119 /* Buffer refilled, invalidate cache */
120 dma_sync_single_for_cpu(jrdev
, bd
->addr
, RN_BUF_SIZE
, DMA_FROM_DEVICE
);
122 print_hex_dump_debug("rng refreshed buf@: ", DUMP_PREFIX_ADDRESS
, 16, 4,
123 bd
->buf
, RN_BUF_SIZE
, 1);
126 static inline int submit_job(struct caam_rng_ctx
*ctx
, int to_current
)
128 struct buf_data
*bd
= &ctx
->bufs
[!(to_current
^ ctx
->current_buf
)];
129 struct device
*jrdev
= ctx
->jrdev
;
130 u32
*desc
= bd
->hw_desc
;
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
);
137 complete(&bd
->filled
); /* don't wait on failed job*/
139 atomic_inc(&bd
->empty
); /* note if pending */
144 static int caam_read(struct hwrng
*rng
, void *data
, size_t max
, bool wait
)
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
;
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 */
159 /* no immediate data, so exit if not waiting */
163 /* waiting for pending job */
164 if (atomic_read(&bd
->empty
))
165 wait_for_completion(&bd
->filled
);
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
);
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
;
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
);
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
);
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);
197 static inline int rng_create_sh_desc(struct caam_rng_ctx
*ctx
)
199 struct device
*jrdev
= ctx
->jrdev
;
200 u32
*desc
= ctx
->sh_desc
;
202 init_sh_desc(desc
, HDR_SHARE_SERIAL
);
204 /* Generate random bytes */
205 append_operation(desc
, OP_ALG_ALGSEL_RNG
| OP_TYPE_CLASS1_ALG
);
208 append_seq_fifo_store(desc
, RN_BUF_SIZE
, FIFOST_TYPE_RNGSTORE
);
210 ctx
->sh_desc_dma
= dma_map_single(jrdev
, desc
, desc_bytes(desc
),
212 if (dma_mapping_error(jrdev
, ctx
->sh_desc_dma
)) {
213 dev_err(jrdev
, "unable to map shared descriptor\n");
217 print_hex_dump_debug("rng shdesc@: ", DUMP_PREFIX_ADDRESS
, 16, 4,
218 desc
, desc_bytes(desc
), 1);
223 static inline int rng_create_job_desc(struct caam_rng_ctx
*ctx
, int buf_id
)
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
);
230 init_job_desc_shared(desc
, ctx
->sh_desc_dma
, sh_len
, HDR_SHARE_DEFER
|
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");
239 append_seq_out_ptr_intlen(desc
, bd
->addr
, RN_BUF_SIZE
, 0);
241 print_hex_dump_debug("rng job desc@: ", DUMP_PREFIX_ADDRESS
, 16, 4,
242 desc
, desc_bytes(desc
), 1);
247 static void caam_cleanup(struct hwrng
*rng
)
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
);
258 rng_unmap_ctx(rng_ctx
);
261 static int caam_init_buf(struct caam_rng_ctx
*ctx
, int buf_id
)
263 struct buf_data
*bd
= &ctx
->bufs
[buf_id
];
266 err
= rng_create_job_desc(ctx
, buf_id
);
270 atomic_set(&bd
->empty
, BUF_EMPTY
);
271 submit_job(ctx
, buf_id
== ctx
->current_buf
);
272 wait_for_completion(&bd
->filled
);
277 static int caam_init_rng(struct caam_rng_ctx
*ctx
, struct device
*jrdev
)
283 err
= rng_create_sh_desc(ctx
);
287 ctx
->current_buf
= 0;
288 ctx
->cur_buf_idx
= 0;
290 err
= caam_init_buf(ctx
, 0);
294 return caam_init_buf(ctx
, 1);
297 static struct hwrng caam_rng
= {
299 .cleanup
= caam_cleanup
,
303 void caam_rng_exit(void)
308 caam_jr_free(rng_ctx
->jrdev
);
309 hwrng_unregister(&caam_rng
);
313 int caam_rng_init(struct device
*ctrldev
)
317 struct caam_drv_private
*priv
= dev_get_drvdata(ctrldev
);
321 /* Check for an instantiated RNG before registration */
323 rng_inst
= (rd_reg32(&priv
->ctrl
->perfmon
.cha_num_ls
) &
324 CHA_ID_LS_RNG_MASK
) >> CHA_ID_LS_RNG_SHIFT
;
326 rng_inst
= rd_reg32(&priv
->ctrl
->vreg
.rng
) & CHA_VER_NUM_MASK
;
331 dev
= caam_jr_alloc();
333 pr_err("Job Ring Device allocation for transform failed\n");
336 rng_ctx
= kmalloc(sizeof(*rng_ctx
), GFP_DMA
| GFP_KERNEL
);
339 goto free_caam_alloc
;
341 err
= caam_init_rng(rng_ctx
, dev
);
345 dev_info(dev
, "registering rng-caam\n");
347 err
= hwrng_register(&caam_rng
);