Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch/arm/broadcom Make the rpi hwrng feed data to the en...



details:   https://anonhg.NetBSD.org/src/rev/e084b7b088e9
branches:  trunk
changeset: 789646:e084b7b088e9
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Thu Aug 29 21:54:11 2013 +0000

description:
Make the rpi hwrng feed data to the entropy pool in a softint.

The two-lock scheme here is excessive but will do provisionally until
rnd(9) gets disentangled.

Tested by Aymeric Vincent.

diffstat:

 sys/arch/arm/broadcom/bcm2835_rng.c |  134 ++++++++++++++++++++++++-----------
 1 files changed, 92 insertions(+), 42 deletions(-)

diffs (196 lines):

diff -r 0f809507d394 -r e084b7b088e9 sys/arch/arm/broadcom/bcm2835_rng.c
--- a/sys/arch/arm/broadcom/bcm2835_rng.c       Thu Aug 29 20:02:35 2013 +0000
+++ b/sys/arch/arm/broadcom/bcm2835_rng.c       Thu Aug 29 21:54:11 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bcm2835_rng.c,v 1.8 2013/08/28 18:32:45 riastradh Exp $ */
+/*     $NetBSD: bcm2835_rng.c,v 1.9 2013/08/29 21:54:11 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bcm2835_rng.c,v 1.8 2013/08/28 18:32:45 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bcm2835_rng.c,v 1.9 2013/08/29 21:54:11 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -39,6 +39,7 @@
 #include <sys/bus.h>
 #include <sys/rnd.h>
 #include <sys/atomic.h>
+#include <sys/intr.h>
 
 #include <arm/broadcom/bcm_amba.h>
 #include <arm/broadcom/bcm2835reg.h>
@@ -47,28 +48,30 @@
 #define RNG_CTRL               0x00
 #define  RNG_CTRL_EN           __BIT(0)
 #define RNG_STATUS             0x04
-#define  RNG_STATUS_CNT_MASK   __BITS(31,24)
-#define  RNG_STATUS_CNT_SHIFT  24
+#define  RNG_STATUS_CNT                __BITS(31,24)
 #define RNG_DATA               0x08
 
 #define RNG_DATA_MAX           256
 
 struct bcm2835rng_softc {
-       device_t sc_dev;
+       device_t                sc_dev;
 
-       bus_space_tag_t sc_iot;
-       bus_space_handle_t sc_ioh;
+       bus_space_tag_t         sc_iot;
+       bus_space_handle_t      sc_ioh;
 
-       krndsource_t sc_rnd;
+       kmutex_t                sc_intr_lock;
+       unsigned int            sc_bytes_wanted;
+       void                    *sc_sih;
 
-       kmutex_t sc_mutex;
-
-       uint32_t sc_data[RNG_DATA_MAX];
+       kmutex_t                sc_rnd_lock;
+       krndsource_t            sc_rndsource;
 };
 
-static void bcmrng_get(size_t, void *);
 static int bcmrng_match(device_t, cfdata_t, void *);
 static void bcmrng_attach(device_t, device_t, void *);
+static void bcmrng_get(struct bcm2835rng_softc *);
+static void bcmrng_get_cb(size_t, void *);
+static void bcmrng_get_intr(void *);
 
 CFATTACH_DECL_NEW(bcmrng_amba, sizeof(struct bcm2835rng_softc),
     bcmrng_match, bcmrng_attach, NULL, NULL);
@@ -101,15 +104,9 @@
        if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, BCM2835_RNG_SIZE, 0,
            &sc->sc_ioh)) {
                aprint_error_dev(sc->sc_dev, "unable to map device\n");
-               return;
+               goto fail0;
        }
 
-       mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
-
-       rndsource_setcb(&sc->sc_rnd, bcmrng_get, sc);
-       rnd_attach_source(&sc->sc_rnd, device_xname(self), RND_TYPE_RNG,
-           RND_FLAG_NO_ESTIMATE|RND_FLAG_HASCB);
-
        /* discard initial numbers, broadcom says they are "less random" */
        bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS, 0x40000);
 
@@ -118,35 +115,88 @@
        ctrl |= RNG_CTRL_EN;
        bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL, ctrl);
 
+       /* set up a softint for adding data */
+       mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SERIAL);
+       sc->sc_bytes_wanted = 0;
+       sc->sc_sih = softint_establish(SOFTINT_SERIAL|SOFTINT_MPSAFE,
+           &bcmrng_get_intr, sc);
+       if (sc->sc_sih == NULL) {
+               aprint_error_dev(sc->sc_dev, "unable to establish softint");
+               goto fail1;
+       }
+
+       /* set up an rndsource */
+       mutex_init(&sc->sc_rnd_lock, MUTEX_DEFAULT, IPL_SERIAL);
+       rndsource_setcb(&sc->sc_rndsource, &bcmrng_get_cb, sc);
+       rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
+           RND_FLAG_NO_ESTIMATE|RND_FLAG_HASCB);
+
        /* get some initial entropy ASAP */
-       bcmrng_get(RND_POOLBITS / NBBY, sc);
+       bcmrng_get_cb(RND_POOLBITS / NBBY, sc);
+
+       /* Success!  */
+       return;
+
+fail1: mutex_destroy(&sc->sc_intr_lock);
+       bus_space_unmap(aaa->aaa_iot, sc->sc_ioh, BCM2835_RNG_SIZE);
+fail0: return;
 }
 
 static void
-bcmrng_get(size_t bytes, void *priv)
+bcmrng_get(struct bcm2835rng_softc *sc)
 {
-       struct bcm2835rng_softc *sc = priv;
-       uint32_t status;
-       int need = bytes, cnt;
+       uint32_t status, cnt;
+       uint32_t buf[RNG_DATA_MAX]; /* 1k on the stack */
 
-       mutex_spin_enter(&sc->sc_mutex);
-
-       if (__predict_false(need < 1)) {
-               mutex_spin_exit(&sc->sc_mutex);
-               return;
-       }
+       mutex_spin_enter(&sc->sc_intr_lock);
+       while (sc->sc_bytes_wanted) {
+               status = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS);
+               cnt = __SHIFTOUT(status, RNG_STATUS_CNT);
+               KASSERT(cnt < RNG_DATA_MAX);
+               if (cnt == 0)
+                       continue;       /* XXX Busy-waiting seems wrong...  */
+               bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, RNG_DATA, buf,
+                   cnt);
 
-       while (need > 0) {
-               status = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS);
-               cnt = (status & RNG_STATUS_CNT_MASK) >> RNG_STATUS_CNT_SHIFT;
-               if (cnt > 0) {
-                       bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh,
-                                              RNG_DATA, sc->sc_data, cnt);
-                       rnd_add_data(&sc->sc_rnd, sc->sc_data,
-                                    cnt * 4, cnt * 4 * NBBY);
-               }
+               /*
+                * This lock dance is necessary because rnd_add_data
+                * may call bcmrng_get_cb which takes the intr lock.
+                */
+               mutex_spin_exit(&sc->sc_intr_lock);
+               mutex_spin_enter(&sc->sc_rnd_lock);
+               rnd_add_data(&sc->sc_rndsource, buf, (cnt * 4),
+                   (cnt * 4 * NBBY));
+               mutex_spin_exit(&sc->sc_rnd_lock);
+               mutex_spin_enter(&sc->sc_intr_lock);
+               sc->sc_bytes_wanted -= MIN(sc->sc_bytes_wanted, (cnt * 4));
+       }
+       explicit_memset(buf, 0, sizeof(buf));
+       mutex_spin_exit(&sc->sc_intr_lock);
+}
+
+static void
+bcmrng_get_cb(size_t bytes_wanted, void *arg)
+{
+       struct bcm2835rng_softc *sc = arg;
 
-               need -= cnt * 4;
-       }
-       mutex_spin_exit(&sc->sc_mutex);
+       /*
+        * Deferring to a softint is necessary until the rnd(9) locking
+        * is fixed.
+        */
+       mutex_spin_enter(&sc->sc_intr_lock);
+       if (sc->sc_bytes_wanted == 0)
+               softint_schedule(sc->sc_sih);
+       if (bytes_wanted > (UINT_MAX - sc->sc_bytes_wanted))
+               sc->sc_bytes_wanted = UINT_MAX;
+       else
+               sc->sc_bytes_wanted += bytes_wanted;
+       mutex_spin_exit(&sc->sc_intr_lock);
 }
+
+static void
+bcmrng_get_intr(void *arg)
+{
+       struct bcm2835rng_softc *const sc = arg;
+
+       bcmrng_get(sc);
+}



Home | Main Index | Thread Index | Old Index