octeon_rnm.c revision 1.5 1 1.5 riastrad /* $NetBSD: octeon_rnm.c,v 1.5 2020/05/13 21:09:02 riastradh Exp $ */
2 1.1 hikaru
3 1.1 hikaru /*
4 1.1 hikaru * Copyright (c) 2007 Internet Initiative Japan, Inc.
5 1.1 hikaru * All rights reserved.
6 1.1 hikaru *
7 1.1 hikaru * Redistribution and use in source and binary forms, with or without
8 1.1 hikaru * modification, are permitted provided that the following conditions
9 1.1 hikaru * are met:
10 1.1 hikaru * 1. Redistributions of source code must retain the above copyright
11 1.1 hikaru * notice, this list of conditions and the following disclaimer.
12 1.1 hikaru * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 hikaru * notice, this list of conditions and the following disclaimer in the
14 1.1 hikaru * documentation and/or other materials provided with the distribution.
15 1.1 hikaru *
16 1.1 hikaru * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 1.1 hikaru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 hikaru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 hikaru * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 1.1 hikaru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 hikaru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 hikaru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 hikaru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 hikaru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 hikaru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 hikaru * SUCH DAMAGE.
27 1.1 hikaru */
28 1.1 hikaru
29 1.5 riastrad /*
30 1.5 riastrad * Cavium Octeon Random Number Generator / Random Number Memory `RNM'
31 1.5 riastrad *
32 1.5 riastrad * The RNM unit consists of:
33 1.5 riastrad *
34 1.5 riastrad * 1. 128 ring oscillators
35 1.5 riastrad * 2. an LFSR/SHA-1 conditioner
36 1.5 riastrad * 3. a 512-byte FIFO
37 1.5 riastrad *
38 1.5 riastrad * When the unit is enabled, there are three modes of operation:
39 1.5 riastrad *
40 1.5 riastrad * (a) deterministic: the ring oscillators are disabled and the
41 1.5 riastrad * LFSR/SHA-1 conditioner operates on fixed inputs to give
42 1.5 riastrad * reproducible results for testing,
43 1.5 riastrad *
44 1.5 riastrad * (b) conditioned entropy: the ring oscillators are enabled and
45 1.5 riastrad * samples from them are fed through the LFSR/SHA-1
46 1.5 riastrad * conditioner before being put into the FIFO, and
47 1.5 riastrad *
48 1.5 riastrad * (c) raw entropy: the ring oscillators are enabled, and a group
49 1.5 riastrad * of eight of them selected at any one time is sampled and
50 1.5 riastrad * fed into the FIFO.
51 1.5 riastrad *
52 1.5 riastrad * Details:
53 1.5 riastrad *
54 1.5 riastrad * - The FIFO is refilled whenever we read out of it, either with
55 1.5 riastrad * a load address or an IOBDMA operation.
56 1.5 riastrad *
57 1.5 riastrad * - The conditioner takes 81 cycles to produce a 64-bit block of
58 1.5 riastrad * output in the FIFO whether in deterministic or conditioned
59 1.5 riastrad * entropy mode, each block consisting of the first 64 bits of a
60 1.5 riastrad * SHA-1 hash.
61 1.5 riastrad *
62 1.5 riastrad * - A group of eight ring oscillators take 8 cycles to produce a
63 1.5 riastrad * 64-bit block of output in the FIFO in raw entropy mode, each
64 1.5 riastrad * block consisting of eight consecutive samples from each RO in
65 1.5 riastrad * parallel.
66 1.5 riastrad *
67 1.5 riastrad * The first sample of each RO always seems to be zero. Further,
68 1.5 riastrad * consecutive samples from a single ring oscillator are not
69 1.5 riastrad * independent, so naive debiasing like a von Neumann extractor
70 1.5 riastrad * falls flat on its face.
71 1.5 riastrad *
72 1.5 riastrad * We read out one FIFO's worth of raw samples from all 128 ring
73 1.5 riastrad * oscillators by going through them round-robin, and without a
74 1.5 riastrad * more detailed assessment of the jitter on the physical devices,
75 1.5 riastrad * we assume it takes a couple thousand samples of ring
76 1.5 riastrad * oscillators (one bit per sample) to reach one bit of entropy,
77 1.5 riastrad * so we read out 8 KB to get about 256 bits of entropy.
78 1.5 riastrad *
79 1.5 riastrad * We could use the on-board LFSR/SHA-1 conditioner, but it's not
80 1.5 riastrad * clear how many RO samples go into the conditioner, and our
81 1.5 riastrad * entropy pool is a perfectly good conditioner itself, so it
82 1.5 riastrad * seems there is little advantage -- other than expedience -- to
83 1.5 riastrad * using the LFSR/SHA-1 conditioner.
84 1.5 riastrad *
85 1.5 riastrad * Reference: Cavium Networks OCTEON Plus CN50XX Hardware Reference
86 1.5 riastrad * Manual, CN50XX-HM-0.99E PRELIMINARY, July 2008.
87 1.5 riastrad */
88 1.5 riastrad
89 1.1 hikaru #include <sys/cdefs.h>
90 1.5 riastrad __KERNEL_RCSID(0, "$NetBSD: octeon_rnm.c,v 1.5 2020/05/13 21:09:02 riastradh Exp $");
91 1.1 hikaru
92 1.1 hikaru #include <sys/param.h>
93 1.1 hikaru #include <sys/device.h>
94 1.1 hikaru #include <sys/kernel.h>
95 1.1 hikaru #include <sys/rndsource.h>
96 1.4 simonb #include <sys/systm.h>
97 1.1 hikaru
98 1.1 hikaru #include <mips/locore.h>
99 1.1 hikaru #include <mips/cavium/include/iobusvar.h>
100 1.1 hikaru #include <mips/cavium/dev/octeon_rnmreg.h>
101 1.1 hikaru #include <mips/cavium/dev/octeon_corereg.h>
102 1.1 hikaru #include <mips/cavium/octeonvar.h>
103 1.1 hikaru
104 1.1 hikaru #include <sys/bus.h>
105 1.1 hikaru
106 1.5 riastrad //#define OCTEON_RNM_DEBUG
107 1.5 riastrad
108 1.5 riastrad #define ENT_DELAY_CLOCK 8 /* cycles for each 64-bit RO sample batch */
109 1.5 riastrad #define RNG_DELAY_CLOCK 81 /* cycles for each SHA-1 output */
110 1.5 riastrad #define NROGROUPS 16
111 1.5 riastrad #define RNG_FIFO_WORDS (512/sizeof(uint64_t))
112 1.1 hikaru
113 1.1 hikaru struct octeon_rnm_softc {
114 1.1 hikaru bus_space_tag_t sc_bust;
115 1.1 hikaru bus_space_handle_t sc_regh;
116 1.4 simonb kmutex_t sc_lock;
117 1.1 hikaru krndsource_t sc_rndsrc; /* /dev/random source */
118 1.5 riastrad unsigned sc_rogroup;
119 1.1 hikaru };
120 1.1 hikaru
121 1.1 hikaru static int octeon_rnm_match(device_t, struct cfdata *, void *);
122 1.1 hikaru static void octeon_rnm_attach(device_t, device_t, void *);
123 1.4 simonb static void octeon_rnm_rng(size_t, void *);
124 1.5 riastrad static void octeon_rnm_reset(struct octeon_rnm_softc *);
125 1.5 riastrad static void octeon_rnm_conditioned_deterministic(struct octeon_rnm_softc *);
126 1.5 riastrad static void octeon_rnm_conditioned_entropy(struct octeon_rnm_softc *);
127 1.5 riastrad static void octeon_rnm_raw_entropy(struct octeon_rnm_softc *, unsigned);
128 1.4 simonb static uint64_t octeon_rnm_load(struct octeon_rnm_softc *);
129 1.5 riastrad static void octeon_rnm_iobdma(struct octeon_rnm_softc *, uint64_t *, unsigned);
130 1.5 riastrad static void octeon_rnm_delay(uint32_t);
131 1.1 hikaru
132 1.1 hikaru CFATTACH_DECL_NEW(octeon_rnm, sizeof(struct octeon_rnm_softc),
133 1.1 hikaru octeon_rnm_match, octeon_rnm_attach, NULL, NULL);
134 1.1 hikaru
135 1.1 hikaru static int
136 1.1 hikaru octeon_rnm_match(device_t parent, struct cfdata *cf, void *aux)
137 1.1 hikaru {
138 1.1 hikaru struct iobus_attach_args *aa = aux;
139 1.1 hikaru
140 1.1 hikaru if (strcmp(cf->cf_name, aa->aa_name) != 0)
141 1.5 riastrad return 0;
142 1.1 hikaru if (cf->cf_unit != aa->aa_unitno)
143 1.5 riastrad return 0;
144 1.5 riastrad return 1;
145 1.1 hikaru }
146 1.1 hikaru
147 1.1 hikaru static void
148 1.1 hikaru octeon_rnm_attach(device_t parent, device_t self, void *aux)
149 1.1 hikaru {
150 1.1 hikaru struct octeon_rnm_softc *sc = device_private(self);
151 1.1 hikaru struct iobus_attach_args *aa = aux;
152 1.5 riastrad uint64_t bist_status, sample, expected = UINT64_C(0xd654ff35fadf866b);
153 1.1 hikaru
154 1.1 hikaru aprint_normal("\n");
155 1.1 hikaru
156 1.5 riastrad /* Map the device registers, all two of them. */
157 1.1 hikaru sc->sc_bust = aa->aa_bust;
158 1.3 simonb if (bus_space_map(aa->aa_bust, aa->aa_unit->addr, RNM_SIZE,
159 1.3 simonb 0, &sc->sc_regh) != 0) {
160 1.3 simonb aprint_error_dev(self, "unable to map device\n");
161 1.3 simonb return;
162 1.3 simonb }
163 1.3 simonb
164 1.5 riastrad /* Verify that the built-in self-test succeeded. */
165 1.3 simonb bist_status = bus_space_read_8(sc->sc_bust, sc->sc_regh,
166 1.3 simonb RNM_BIST_STATUS_OFFSET);
167 1.3 simonb if (bist_status) {
168 1.3 simonb aprint_error_dev(self, "RNG built in self test failed: %#lx\n",
169 1.3 simonb bist_status);
170 1.3 simonb return;
171 1.3 simonb }
172 1.1 hikaru
173 1.5 riastrad /* Create a mutex to serialize access to the FIFO. */
174 1.4 simonb mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
175 1.4 simonb
176 1.4 simonb /*
177 1.5 riastrad * Reset the core, enable the RNG engine without entropy, wait
178 1.5 riastrad * 81 cycles for it to produce a single sample, and draw the
179 1.5 riastrad * deterministic sample to test.
180 1.4 simonb *
181 1.5 riastrad * XXX Verify that the output matches the SHA-1 computation
182 1.5 riastrad * described by the data sheet, not just a known answer.
183 1.4 simonb */
184 1.5 riastrad octeon_rnm_reset(sc);
185 1.5 riastrad octeon_rnm_conditioned_deterministic(sc);
186 1.5 riastrad octeon_rnm_delay(RNG_DELAY_CLOCK*1);
187 1.5 riastrad sample = octeon_rnm_load(sc);
188 1.5 riastrad if (sample != expected)
189 1.5 riastrad aprint_error_dev(self, "self-test: read %016"PRIx64","
190 1.5 riastrad " expected %016"PRIx64, sample, expected);
191 1.4 simonb
192 1.4 simonb /*
193 1.5 riastrad * Reset the core again to clear the FIFO, and enable the RNG
194 1.5 riastrad * engine with entropy exposed directly. Start from the first
195 1.5 riastrad * group of ring oscillators; as we gather samples we will
196 1.5 riastrad * rotate through the rest of them.
197 1.4 simonb */
198 1.5 riastrad octeon_rnm_reset(sc);
199 1.5 riastrad sc->sc_rogroup = 0;
200 1.5 riastrad octeon_rnm_raw_entropy(sc, sc->sc_rogroup);
201 1.5 riastrad octeon_rnm_delay(ENT_DELAY_CLOCK*RNG_FIFO_WORDS);
202 1.4 simonb
203 1.5 riastrad /* Attach the rndsource. */
204 1.4 simonb rndsource_setcb(&sc->sc_rndsrc, octeon_rnm_rng, sc);
205 1.4 simonb rnd_attach_source(&sc->sc_rndsrc, device_xname(self), RND_TYPE_RNG,
206 1.4 simonb RND_FLAG_DEFAULT | RND_FLAG_HASCB);
207 1.1 hikaru }
208 1.1 hikaru
209 1.1 hikaru static void
210 1.4 simonb octeon_rnm_rng(size_t nbytes, void *vsc)
211 1.1 hikaru {
212 1.5 riastrad /* Assume we need 2048 RO samples to get one bit of entropy. */
213 1.5 riastrad const unsigned BPB = 2048;
214 1.5 riastrad uint64_t sample[32];
215 1.1 hikaru struct octeon_rnm_softc *sc = vsc;
216 1.5 riastrad size_t needed = NBBY*nbytes;
217 1.5 riastrad unsigned i;
218 1.1 hikaru
219 1.5 riastrad /* Sample the ring oscillators round-robin. */
220 1.4 simonb mutex_enter(&sc->sc_lock);
221 1.5 riastrad while (needed) {
222 1.5 riastrad /*
223 1.5 riastrad * Switch to the next RO group once we drain the FIFO.
224 1.5 riastrad * By the time rnd_add_data is done, we will have
225 1.5 riastrad * processed all 512 bytes of the FIFO. We assume it
226 1.5 riastrad * takes at least one cycle per byte (realistically,
227 1.5 riastrad * more like ~80cpb to draw from the FIFO and then
228 1.5 riastrad * process it with rnd_add_data), so there is no need
229 1.5 riastrad * for any other delays.
230 1.5 riastrad */
231 1.5 riastrad sc->sc_rogroup++;
232 1.5 riastrad sc->sc_rogroup %= NROGROUPS;
233 1.5 riastrad octeon_rnm_raw_entropy(sc, sc->sc_rogroup);
234 1.5 riastrad
235 1.1 hikaru /*
236 1.5 riastrad * Gather half the FIFO at a time -- we are limited to
237 1.5 riastrad * 256 bytes because of limits on the CVMSEG buffer.
238 1.1 hikaru */
239 1.5 riastrad CTASSERT(sizeof sample == 256);
240 1.5 riastrad CTASSERT(2*__arraycount(sample) == RNG_FIFO_WORDS);
241 1.5 riastrad for (i = 0; i < 2; i++) {
242 1.5 riastrad octeon_rnm_iobdma(sc, sample, __arraycount(sample));
243 1.5 riastrad #ifdef OCTEON_RNM_DEBUG
244 1.5 riastrad hexdump(printf, "rnm", sample, sizeof sample);
245 1.5 riastrad #endif
246 1.5 riastrad rnd_add_data_sync(&sc->sc_rndsrc, sample,
247 1.5 riastrad sizeof sample, NBBY*sizeof(sample)/BPB);
248 1.5 riastrad needed -= MIN(needed, MAX(1, NBBY*sizeof(sample)/BPB));
249 1.5 riastrad }
250 1.5 riastrad
251 1.5 riastrad /* Yield if requested. */
252 1.5 riastrad if (__predict_false(curcpu()->ci_schedstate.spc_flags &
253 1.5 riastrad SPCF_SHOULDYIELD)) {
254 1.5 riastrad mutex_exit(&sc->sc_lock);
255 1.5 riastrad preempt();
256 1.5 riastrad mutex_enter(&sc->sc_lock);
257 1.5 riastrad }
258 1.1 hikaru }
259 1.4 simonb mutex_exit(&sc->sc_lock);
260 1.5 riastrad
261 1.5 riastrad /* Zero the sample. */
262 1.5 riastrad explicit_memset(sample, 0, sizeof sample);
263 1.1 hikaru }
264 1.1 hikaru
265 1.5 riastrad /*
266 1.5 riastrad * octeon_rnm_reset(sc)
267 1.5 riastrad *
268 1.5 riastrad * Reset the RNM unit, disabling it and clearing the FIFO.
269 1.5 riastrad */
270 1.5 riastrad static void
271 1.5 riastrad octeon_rnm_reset(struct octeon_rnm_softc *sc)
272 1.5 riastrad {
273 1.5 riastrad
274 1.5 riastrad bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
275 1.5 riastrad RNM_CTL_STATUS_RNG_RST|RNM_CTL_STATUS_RNM_RST);
276 1.5 riastrad }
277 1.5 riastrad
278 1.5 riastrad /*
279 1.5 riastrad * octeon_rnm_conditioned_deterministic(sc)
280 1.5 riastrad *
281 1.5 riastrad * Switch the RNM unit into the deterministic LFSR/SHA-1 mode with
282 1.5 riastrad * no entropy, for the next data loaded into the FIFO.
283 1.5 riastrad */
284 1.5 riastrad static void
285 1.5 riastrad octeon_rnm_conditioned_deterministic(struct octeon_rnm_softc *sc)
286 1.5 riastrad {
287 1.5 riastrad
288 1.5 riastrad bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
289 1.5 riastrad RNM_CTL_STATUS_RNG_EN);
290 1.5 riastrad }
291 1.5 riastrad
292 1.5 riastrad /*
293 1.5 riastrad * octeon_rnm_conditioned_entropy(sc)
294 1.5 riastrad *
295 1.5 riastrad * Switch the RNM unit to generate ring oscillator samples
296 1.5 riastrad * conditioned with an LFSR/SHA-1, for the next data loaded into
297 1.5 riastrad * the FIFO.
298 1.5 riastrad */
299 1.5 riastrad static void __unused
300 1.5 riastrad octeon_rnm_conditioned_entropy(struct octeon_rnm_softc *sc)
301 1.5 riastrad {
302 1.5 riastrad
303 1.5 riastrad bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
304 1.5 riastrad RNM_CTL_STATUS_RNG_EN|RNM_CTL_STATUS_ENT_EN);
305 1.5 riastrad }
306 1.5 riastrad
307 1.5 riastrad /*
308 1.5 riastrad * octeon_rnm_raw_entropy(sc, rogroup)
309 1.5 riastrad *
310 1.5 riastrad * Switch the RNM unit to generate raw ring oscillator samples
311 1.5 riastrad * from the specified group of eight ring oscillator.
312 1.5 riastrad */
313 1.5 riastrad static void
314 1.5 riastrad octeon_rnm_raw_entropy(struct octeon_rnm_softc *sc, unsigned rogroup)
315 1.5 riastrad {
316 1.5 riastrad uint64_t ctl = 0;
317 1.5 riastrad
318 1.5 riastrad ctl |= RNM_CTL_STATUS_RNG_EN; /* enable FIFO */
319 1.5 riastrad ctl |= RNM_CTL_STATUS_ENT_EN; /* enable entropy source */
320 1.5 riastrad ctl |= RNM_CTL_STATUS_EXP_ENT; /* expose entropy without LFSR/SHA-1 */
321 1.5 riastrad ctl |= __SHIFTIN(rogroup, RNM_CTL_STATUS_ENT_SEL_MASK);
322 1.5 riastrad
323 1.5 riastrad bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
324 1.5 riastrad ctl);
325 1.5 riastrad }
326 1.5 riastrad
327 1.5 riastrad /*
328 1.5 riastrad * octeon_rnm_load(sc)
329 1.5 riastrad *
330 1.5 riastrad * Load a single 64-bit word out of the FIFO.
331 1.5 riastrad */
332 1.4 simonb static uint64_t
333 1.1 hikaru octeon_rnm_load(struct octeon_rnm_softc *sc)
334 1.1 hikaru {
335 1.1 hikaru uint64_t addr =
336 1.1 hikaru RNM_OPERATION_BASE_IO_BIT |
337 1.1 hikaru __BITS64_SET(RNM_OPERATION_BASE_MAJOR_DID, 0x08) |
338 1.1 hikaru __BITS64_SET(RNM_OPERATION_BASE_SUB_DID, 0x00);
339 1.1 hikaru
340 1.1 hikaru return octeon_xkphys_read_8(addr);
341 1.1 hikaru }
342 1.5 riastrad
343 1.5 riastrad /*
344 1.5 riastrad * octeon_rnm_iobdma(sc, buf, nwords)
345 1.5 riastrad *
346 1.5 riastrad * Load nwords, at most 32, out of the FIFO into buf.
347 1.5 riastrad */
348 1.5 riastrad static void
349 1.5 riastrad octeon_rnm_iobdma(struct octeon_rnm_softc *sc, uint64_t *buf, unsigned nwords)
350 1.5 riastrad {
351 1.5 riastrad size_t scraddr = OCTEON_CVMSEG_OFFSET(csm_rnm);
352 1.5 riastrad uint64_t iobdma =
353 1.5 riastrad __SHIFTIN(scraddr/sizeof(uint64_t), IOBDMA_SCRADDR) |
354 1.5 riastrad __SHIFTIN(nwords, IOBDMA_LEN) |
355 1.5 riastrad __SHIFTIN(RNM_IOBDMA_MAJORDID, IOBDMA_MAJORDID) |
356 1.5 riastrad __SHIFTIN(RNM_IOBDMA_SUBDID, IOBDMA_SUBDID);
357 1.5 riastrad
358 1.5 riastrad KASSERT(nwords < 256); /* iobdma address restriction */
359 1.5 riastrad KASSERT(nwords <= 32); /* octeon_cvmseg_map limitation */
360 1.5 riastrad
361 1.5 riastrad octeon_iobdma_write_8(iobdma);
362 1.5 riastrad OCTEON_SYNCIOBDMA;
363 1.5 riastrad for (; nwords --> 0; scraddr += 8)
364 1.5 riastrad *buf++ = octeon_cvmseg_read_8(scraddr);
365 1.5 riastrad }
366 1.5 riastrad
367 1.5 riastrad /*
368 1.5 riastrad * octeon_rnm_delay(ncycles)
369 1.5 riastrad *
370 1.5 riastrad * Wait ncycles, at most UINT32_MAX/2 so we behave reasonably even
371 1.5 riastrad * if the cycle counter rolls over.
372 1.5 riastrad */
373 1.5 riastrad static void
374 1.5 riastrad octeon_rnm_delay(uint32_t ncycles)
375 1.5 riastrad {
376 1.5 riastrad uint32_t deadline = mips3_cp0_count_read() + ncycles;
377 1.5 riastrad
378 1.5 riastrad KASSERT(ncycles <= UINT32_MAX/2);
379 1.5 riastrad
380 1.5 riastrad while ((deadline - mips3_cp0_count_read()) < ncycles)
381 1.5 riastrad continue;
382 1.5 riastrad }
383