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/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_get_sync(ce->dev);
58 	if (err < 0) {
59 		pm_runtime_put_noidle(ce->dev);
60 		goto err_pm;
61 	}
62 
63 	mutex_lock(&ce->rnglock);
64 	chan = &ce->chanlist[flow];
65 
66 	cet = &chan->tl[0];
67 	memset(cet, 0, sizeof(struct ce_task));
68 
69 	cet->t_id = cpu_to_le32(flow);
70 	common = ce->variant->trng | CE_COMM_INT;
71 	cet->t_common_ctl = cpu_to_le32(common);
72 
73 	/* recent CE (H6) need length in bytes, in word otherwise */
74 	if (ce->variant->trng_t_dlen_in_bytes)
75 		cet->t_dlen = cpu_to_le32(todo);
76 	else
77 		cet->t_dlen = cpu_to_le32(todo / 4);
78 
79 	cet->t_sym_ctl = 0;
80 	cet->t_asym_ctl = 0;
81 
82 	cet->t_dst[0].addr = cpu_to_le32(dma_dst);
83 	cet->t_dst[0].len = cpu_to_le32(todo / 4);
84 	ce->chanlist[flow].timeout = todo;
85 
86 	err = sun8i_ce_run_task(ce, 3, "TRNG");
87 	mutex_unlock(&ce->rnglock);
88 
89 	pm_runtime_put(ce->dev);
90 
91 err_pm:
92 	dma_unmap_single(ce->dev, dma_dst, todo, DMA_FROM_DEVICE);
93 
94 	if (!err) {
95 		memcpy(data, d, max);
96 		err = max;
97 	}
98 err_dst:
99 	kfree_sensitive(d);
100 	return err;
101 }
102 
103 int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce)
104 {
105 	int ret;
106 
107 	if (ce->variant->trng == CE_ID_NOTSUPP) {
108 		dev_info(ce->dev, "TRNG not supported\n");
109 		return 0;
110 	}
111 	ce->trng.name = "sun8i Crypto Engine TRNG";
112 	ce->trng.read = sun8i_ce_trng_read;
113 	ce->trng.quality = 1000;
114 
115 	ret = hwrng_register(&ce->trng);
116 	if (ret)
117 		dev_err(ce->dev, "Fail to register the TRNG\n");
118 	return ret;
119 }
120 
121 void sun8i_ce_hwrng_unregister(struct sun8i_ce_dev *ce)
122 {
123 	if (ce->variant->trng == CE_ID_NOTSUPP)
124 		return;
125 	hwrng_unregister(&ce->trng);
126 }
127