xref: /freebsd/sys/dev/qcom_rnd/qcom_rnd.c (revision 4fbb9c43aa44d9145151bb5f77d302ba01fb7551)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021, Adrian Chadd <adrian@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /* Driver for Qualcomm MSM entropy device. */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 #include <sys/sglist.h>
36 #include <sys/random.h>
37 #include <sys/stdatomic.h>
38 
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/bus.h>
42 
43 #include <dev/fdt/fdt_common.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 
47 #include <dev/random/randomdev.h>
48 #include <dev/random/random_harvestq.h>
49 
50 #include <dev/qcom_rnd/qcom_rnd_reg.h>
51 
52 struct qcom_rnd_softc {
53 	device_t	 dev;
54 	int		reg_rid;
55 	struct resource	*reg;
56 };
57 
58 static int	qcom_rnd_modevent(module_t, int, void *);
59 
60 static int	qcom_rnd_probe(device_t);
61 static int	qcom_rnd_attach(device_t);
62 static int	qcom_rnd_detach(device_t);
63 
64 static int	qcom_rnd_harvest(struct qcom_rnd_softc *, void *, size_t *);
65 static unsigned	qcom_rnd_read(void *, unsigned);
66 
67 static struct random_source random_qcom_rnd = {
68 	.rs_ident = "Qualcomm Entropy Adapter",
69 	.rs_source = RANDOM_PURE_QUALCOMM,
70 	.rs_read = qcom_rnd_read,
71 };
72 
73 /* Kludge for API limitations of random(4). */
74 static _Atomic(struct qcom_rnd_softc *) g_qcom_rnd_softc;
75 
76 static int
77 qcom_rnd_modevent(module_t mod, int type, void *unused)
78 {
79 	int error;
80 
81 	switch (type) {
82 	case MOD_LOAD:
83 	case MOD_QUIESCE:
84 	case MOD_UNLOAD:
85 	case MOD_SHUTDOWN:
86 		error = 0;
87 		break;
88 	default:
89 		error = EOPNOTSUPP;
90 		break;
91 	}
92 
93 	return (error);
94 }
95 
96 static int
97 qcom_rnd_probe(device_t dev)
98 {
99 	if (! ofw_bus_status_okay(dev)) {
100 		return (ENXIO);
101 	}
102 
103 	if (ofw_bus_is_compatible(dev, "qcom,prng") == 0) {
104 		return (ENXIO);
105 	}
106 
107 	return (0);
108 }
109 
110 static int
111 qcom_rnd_attach(device_t dev)
112 {
113 	struct qcom_rnd_softc *sc, *exp;
114 	uint32_t reg;
115 
116 	sc = device_get_softc(dev);
117 
118 
119 	/* Found a compatible device! */
120 
121 	sc->dev = dev;
122 
123 	exp = NULL;
124 	if (!atomic_compare_exchange_strong_explicit(&g_qcom_rnd_softc, &exp,
125 	    sc, memory_order_release, memory_order_acquire)) {
126 		return (ENXIO);
127 	}
128 
129 	sc->reg_rid = 0;
130 	sc->reg = bus_alloc_resource_anywhere(dev, SYS_RES_MEMORY,
131 	    &sc->reg_rid, 0x140, RF_ACTIVE);
132 	if (sc->reg == NULL) {
133 		device_printf(dev, "Couldn't allocate memory resource!\n");
134 		return (ENXIO);
135 	}
136 
137 	device_set_desc(dev, "Qualcomm PRNG");
138 
139 	/*
140 	 * Check to see whether the PRNG has already been setup or not.
141 	 */
142 	bus_barrier(sc->reg, 0, 0x120, BUS_SPACE_BARRIER_READ);
143 	reg = bus_read_4(sc->reg, QCOM_RND_PRNG_CONFIG);
144 	if (reg & QCOM_RND_PRNG_CONFIG_HW_ENABLE) {
145 		device_printf(dev, "PRNG HW already enabled\n");
146 	} else {
147 		/*
148 		 * Do PRNG setup and then enable it.
149 		 */
150 		reg = bus_read_4(sc->reg, QCOM_RND_PRNG_LFSR_CFG);
151 		reg &= QCOM_RND_PRNG_LFSR_CFG_MASK;
152 		reg |= QCOM_RND_PRNG_LFSR_CFG_CLOCKS;
153 		bus_write_4(sc->reg, QCOM_RND_PRNG_LFSR_CFG, reg);
154 		bus_barrier(sc->reg, 0, 0x120, BUS_SPACE_BARRIER_WRITE);
155 
156 		reg = bus_read_4(sc->reg, QCOM_RND_PRNG_CONFIG);
157 		reg |= QCOM_RND_PRNG_CONFIG_HW_ENABLE;
158 		bus_write_4(sc->reg, QCOM_RND_PRNG_CONFIG, reg);
159 		bus_barrier(sc->reg, 0, 0x120, BUS_SPACE_BARRIER_WRITE);
160 	}
161 
162 	random_source_register(&random_qcom_rnd);
163 	return (0);
164 
165 }
166 
167 static int
168 qcom_rnd_detach(device_t dev)
169 {
170 	struct qcom_rnd_softc *sc;
171 
172 	sc = device_get_softc(dev);
173 	KASSERT(
174 	    atomic_load_explicit(&g_qcom_rnd_softc, memory_order_acquire) == sc,
175 	    ("only one global instance at a time"));
176 
177 	random_source_deregister(&random_qcom_rnd);
178 	if (sc->reg != NULL) {
179 		bus_release_resource(sc->dev, SYS_RES_MEMORY, sc->reg_rid, sc->reg);
180 	}
181 	atomic_store_explicit(&g_qcom_rnd_softc, NULL, memory_order_release);
182 	return (0);
183 }
184 
185 static int
186 qcom_rnd_harvest(struct qcom_rnd_softc *sc, void *buf, size_t *sz)
187 {
188 	/*
189 	 * Add data to buf until we either run out of entropy or we
190 	 * fill the buffer.
191 	 *
192 	 * Note - be mindful of the provided buffer size; we're reading
193 	 * 4 bytes at a time but we only want to supply up to the max
194 	 * buffer size, so don't write past it!
195 	 */
196 	size_t rz = 0;
197 	uint32_t reg;
198 
199 	while (rz < *sz) {
200 		bus_barrier(sc->reg, 0, 0x120, BUS_SPACE_BARRIER_READ);
201 		reg = bus_read_4(sc->reg, QCOM_RND_PRNG_STATUS);
202 		if ((reg & QCOM_RND_PRNG_STATUS_DATA_AVAIL) == 0)
203 			break;
204 		reg = bus_read_4(sc->reg, QCOM_RND_PRNG_DATA_OUT);
205 		memcpy(((char *) buf) + rz, &reg, sizeof(uint32_t));
206 		rz += sizeof(uint32_t);
207 	}
208 
209 	if (rz == 0)
210 		return (EAGAIN);
211 	*sz = rz;
212 	return (0);
213 }
214 
215 static unsigned
216 qcom_rnd_read(void *buf, unsigned usz)
217 {
218 	struct qcom_rnd_softc *sc;
219 	size_t sz;
220 	int error;
221 
222 	sc = g_qcom_rnd_softc;
223 	if (sc == NULL)
224 		return (0);
225 
226 	sz = usz;
227 	error = qcom_rnd_harvest(sc, buf, &sz);
228 	if (error != 0)
229 		return (0);
230 
231 	return (sz);
232 }
233 
234 static device_method_t qcom_rnd_methods[] = {
235 	/* Device methods. */
236 	DEVMETHOD(device_probe,		qcom_rnd_probe),
237 	DEVMETHOD(device_attach,	qcom_rnd_attach),
238 	DEVMETHOD(device_detach,	qcom_rnd_detach),
239 
240 	DEVMETHOD_END
241 };
242 
243 static driver_t qcom_rnd_driver = {
244 	"qcom_rnd",
245 	qcom_rnd_methods,
246 	sizeof(struct qcom_rnd_softc)
247 };
248 
249 DRIVER_MODULE(qcom_rnd_random, simplebus, qcom_rnd_driver,
250     qcom_rnd_modevent, 0);
251 DRIVER_MODULE(qcom_rnd_random, ofwbus, qcom_rnd_driver,
252     qcom_rnd_modevent, 0);
253 MODULE_DEPEND(qcom_rnd_random, random_device, 1, 1, 1);
254 MODULE_VERSION(qcom_rnd_random, 1);
255