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