1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * sun8i-ce-trng.c - hardware cryptographic offloader for
4  * Allwinner H3/A64/H5/H2+/H6/R40 SoC
5  *
6  * Copyright (C) 2015-2020 Corentin Labbe <clabbe@baylibre.com>
7  *
8  * This file handle the TRNG
9  *
10  * You could find a link for the datasheet in Documentation/arch/arm/sunxi.rst
11  */
12 #include "sun8i-ce.h"
13 #include <linux/dma-mapping.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/hw_random.h>
16 /*
17  * Note that according to the algorithm ID, 2 versions of the TRNG exists,
18  * The first present in H3/H5/R40/A64 and the second present in H6.
19  * This file adds support for both, but only the second is working
20  * reliabily according to rngtest.
21  **/
22 
23 static int sun8i_ce_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
24 {
25 	struct sun8i_ce_dev *ce;
26 	dma_addr_t dma_dst;
27 	int err = 0;
28 	int flow = 3;
29 	unsigned int todo;
30 	struct sun8i_ce_flow *chan;
31 	struct ce_task *cet;
32 	u32 common;
33 	void *d;
34 
35 	ce = container_of(rng, struct sun8i_ce_dev, trng);
36 
37 	/* round the data length to a multiple of 32*/
38 	todo = max + 32;
39 	todo -= todo % 32;
40 
41 	d = kzalloc(todo, GFP_KERNEL | GFP_DMA);
42 	if (!d)
43 		return -ENOMEM;
44 
45 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
46 	ce->hwrng_stat_req++;
47 	ce->hwrng_stat_bytes += todo;
48 #endif
49 
50 	dma_dst = dma_map_single(ce->dev, d, todo, DMA_FROM_DEVICE);
51 	if (dma_mapping_error(ce->dev, dma_dst)) {
52 		dev_err(ce->dev, "Cannot DMA MAP DST\n");
53 		err = -EFAULT;
54 		goto err_dst;
55 	}
56 
57 	err = pm_runtime_resume_and_get(ce->dev);
58 	if (err < 0)
59 		goto err_pm;
60 
61 	mutex_lock(&ce->rnglock);
62 	chan = &ce->chanlist[flow];
63 
64 	cet = &chan->tl[0];
65 	memset(cet, 0, sizeof(struct ce_task));
66 
67 	cet->t_id = cpu_to_le32(flow);
68 	common = ce->variant->trng | CE_COMM_INT;
69 	cet->t_common_ctl = cpu_to_le32(common);
70 
71 	/* recent CE (H6) need length in bytes, in word otherwise */
72 	if (ce->variant->trng_t_dlen_in_bytes)
73 		cet->t_dlen = cpu_to_le32(todo);
74 	else
75 		cet->t_dlen = cpu_to_le32(todo / 4);
76 
77 	cet->t_sym_ctl = 0;
78 	cet->t_asym_ctl = 0;
79 
80 	cet->t_dst[0].addr = cpu_to_le32(dma_dst);
81 	cet->t_dst[0].len = cpu_to_le32(todo / 4);
82 	ce->chanlist[flow].timeout = todo;
83 
84 	err = sun8i_ce_run_task(ce, 3, "TRNG");
85 	mutex_unlock(&ce->rnglock);
86 
87 	pm_runtime_put(ce->dev);
88 
89 err_pm:
90 	dma_unmap_single(ce->dev, dma_dst, todo, DMA_FROM_DEVICE);
91 
92 	if (!err) {
93 		memcpy(data, d, max);
94 		err = max;
95 	}
96 err_dst:
97 	kfree_sensitive(d);
98 	return err;
99 }
100 
101 int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce)
102 {
103 	int ret;
104 
105 	if (ce->variant->trng == CE_ID_NOTSUPP) {
106 		dev_info(ce->dev, "TRNG not supported\n");
107 		return 0;
108 	}
109 	ce->trng.name = "sun8i Crypto Engine TRNG";
110 	ce->trng.read = sun8i_ce_trng_read;
111 
112 	ret = hwrng_register(&ce->trng);
113 	if (ret)
114 		dev_err(ce->dev, "Fail to register the TRNG\n");
115 	return ret;
116 }
117 
118 void sun8i_ce_hwrng_unregister(struct sun8i_ce_dev *ce)
119 {
120 	if (ce->variant->trng == CE_ID_NOTSUPP)
121 		return;
122 	hwrng_unregister(&ce->trng);
123 }
124