Wed Aug 28 18:32:45 2013 UTC ()
Get some entropy early on bcm2835_rng attach.


(riastradh)
diff -r1.7 -r1.8 src/sys/arch/arm/broadcom/bcm2835_rng.c

cvs diff -r1.7 -r1.8 src/sys/arch/arm/broadcom/bcm2835_rng.c (expand / switch to unified diff)

--- src/sys/arch/arm/broadcom/bcm2835_rng.c 2013/08/25 17:08:41 1.7
+++ src/sys/arch/arm/broadcom/bcm2835_rng.c 2013/08/28 18:32:45 1.8
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: bcm2835_rng.c,v 1.7 2013/08/25 17:08:41 tls Exp $ */ 1/* $NetBSD: bcm2835_rng.c,v 1.8 2013/08/28 18:32:45 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jared D. McNeill 8 * by Jared D. McNeill
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -20,27 +20,27 @@ @@ -20,27 +20,27 @@
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: bcm2835_rng.c,v 1.7 2013/08/25 17:08:41 tls Exp $"); 33__KERNEL_RCSID(0, "$NetBSD: bcm2835_rng.c,v 1.8 2013/08/28 18:32:45 riastradh Exp $");
34 34
35#include <sys/param.h> 35#include <sys/param.h>
36#include <sys/systm.h> 36#include <sys/systm.h>
37#include <sys/device.h> 37#include <sys/device.h>
38#include <sys/kernel.h> 38#include <sys/kernel.h>
39#include <sys/bus.h> 39#include <sys/bus.h>
40#include <sys/rnd.h> 40#include <sys/rnd.h>
41#include <sys/atomic.h> 41#include <sys/atomic.h>
42 42
43#include <arm/broadcom/bcm_amba.h> 43#include <arm/broadcom/bcm_amba.h>
44#include <arm/broadcom/bcm2835reg.h> 44#include <arm/broadcom/bcm2835reg.h>
45#include <arm/broadcom/bcm2835_intr.h> 45#include <arm/broadcom/bcm2835_intr.h>
46 46
@@ -107,26 +107,29 @@ bcmrng_attach(device_t parent, device_t  @@ -107,26 +107,29 @@ bcmrng_attach(device_t parent, device_t
107 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM); 107 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
108 108
109 rndsource_setcb(&sc->sc_rnd, bcmrng_get, sc); 109 rndsource_setcb(&sc->sc_rnd, bcmrng_get, sc);
110 rnd_attach_source(&sc->sc_rnd, device_xname(self), RND_TYPE_RNG, 110 rnd_attach_source(&sc->sc_rnd, device_xname(self), RND_TYPE_RNG,
111 RND_FLAG_NO_ESTIMATE|RND_FLAG_HASCB); 111 RND_FLAG_NO_ESTIMATE|RND_FLAG_HASCB);
112 112
113 /* discard initial numbers, broadcom says they are "less random" */ 113 /* discard initial numbers, broadcom says they are "less random" */
114 bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS, 0x40000); 114 bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS, 0x40000);
115 115
116 /* enable rng */ 116 /* enable rng */
117 ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL); 117 ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL);
118 ctrl |= RNG_CTRL_EN; 118 ctrl |= RNG_CTRL_EN;
119 bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL, ctrl); 119 bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL, ctrl);
 120
 121 /* get some initial entropy ASAP */
 122 bcmrng_get(RND_POOLBITS / NBBY, sc);
120} 123}
121 124
122static void 125static void
123bcmrng_get(size_t bytes, void *priv) 126bcmrng_get(size_t bytes, void *priv)
124{ 127{
125 struct bcm2835rng_softc *sc = priv; 128 struct bcm2835rng_softc *sc = priv;
126 uint32_t status; 129 uint32_t status;
127 int need = bytes, cnt; 130 int need = bytes, cnt;
128 131
129 mutex_spin_enter(&sc->sc_mutex); 132 mutex_spin_enter(&sc->sc_mutex);
130 133
131 if (__predict_false(need < 1)) { 134 if (__predict_false(need < 1)) {
132 mutex_spin_exit(&sc->sc_mutex); 135 mutex_spin_exit(&sc->sc_mutex);