Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/arch/arm Simplify bcm2835, tegra, and am335x hardware RN...
details: https://anonhg.NetBSD.org/src/rev/27f1d9bc293a
branches: trunk
changeset: 819795:27f1d9bc293a
user: riastradh <riastradh%NetBSD.org@localhost>
date: Sat Dec 17 15:24:35 2016 +0000
description:
Simplify bcm2835, tegra, and am335x hardware RNG drivers.
Tested by nick@.
diffstat:
sys/arch/arm/broadcom/bcm2835_rng.c | 90 ++++++------------------------------
sys/arch/arm/nvidia/tegra124_car.c | 70 +++++++--------------------
sys/arch/arm/omap/am335x_trng.c | 55 ++++------------------
3 files changed, 45 insertions(+), 170 deletions(-)
diffs (truncated from 388 to 300 lines):
diff -r 6487bf1178df -r 27f1d9bc293a sys/arch/arm/broadcom/bcm2835_rng.c
--- a/sys/arch/arm/broadcom/bcm2835_rng.c Sat Dec 17 15:23:08 2016 +0000
+++ b/sys/arch/arm/broadcom/bcm2835_rng.c Sat Dec 17 15:24:35 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: bcm2835_rng.c,v 1.11 2015/04/13 21:18:40 riastradh Exp $ */
+/* $NetBSD: bcm2835_rng.c,v 1.12 2016/12/17 15:24:35 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.11 2015/04/13 21:18:40 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bcm2835_rng.c,v 1.12 2016/12/17 15:24:35 riastradh Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@@ -39,8 +39,6 @@
#include <sys/bus.h>
#include <sys/rndpool.h>
#include <sys/rndsource.h>
-#include <sys/atomic.h>
-#include <sys/intr.h>
#include <arm/broadcom/bcm_amba.h>
#include <arm/broadcom/bcm2835reg.h>
@@ -60,19 +58,13 @@
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
- kmutex_t sc_intr_lock;
- unsigned int sc_bytes_wanted;
- void *sc_sih;
-
- kmutex_t sc_rnd_lock;
+ kmutex_t sc_lock;
krndsource_t sc_rndsource;
};
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 *);
+static void bcmrng_get(size_t, void *);
CFATTACH_DECL_NEW(bcmrng_amba, sizeof(struct bcm2835rng_softc),
bcmrng_match, bcmrng_attach, NULL, NULL);
@@ -105,7 +97,7 @@
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");
- goto fail0;
+ return;
}
/* discard initial numbers, broadcom says they are "less random" */
@@ -116,41 +108,25 @@
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);
+ mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
+ rndsource_setcb(&sc->sc_rndsource, &bcmrng_get, sc);
rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
/* get some initial entropy ASAP */
- 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;
+ bcmrng_get(RND_POOLBITS / NBBY, sc);
}
static void
-bcmrng_get(struct bcm2835rng_softc *sc)
+bcmrng_get(size_t bytes_wanted, void *arg)
{
+ struct bcm2835rng_softc *sc = arg;
uint32_t status, cnt;
uint32_t buf[RNG_DATA_MAX]; /* 1k on the stack */
- mutex_spin_enter(&sc->sc_intr_lock);
- while (sc->sc_bytes_wanted) {
+ mutex_spin_enter(&sc->sc_lock);
+ while (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);
@@ -158,46 +134,10 @@
continue; /* XXX Busy-waiting seems wrong... */
bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, RNG_DATA, buf,
cnt);
-
- /*
- * 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),
+ rnd_add_data_sync(&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));
+ bytes_wanted -= MIN(bytes_wanted, (cnt * 4));
}
explicit_memset(buf, 0, sizeof(buf));
- mutex_spin_exit(&sc->sc_intr_lock);
+ mutex_spin_exit(&sc->sc_lock);
}
-
-static void
-bcmrng_get_cb(size_t bytes_wanted, void *arg)
-{
- struct bcm2835rng_softc *sc = arg;
-
- /*
- * 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);
-}
diff -r 6487bf1178df -r 27f1d9bc293a sys/arch/arm/nvidia/tegra124_car.c
--- a/sys/arch/arm/nvidia/tegra124_car.c Sat Dec 17 15:23:08 2016 +0000
+++ b/sys/arch/arm/nvidia/tegra124_car.c Sat Dec 17 15:24:35 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: tegra124_car.c,v 1.6 2016/09/08 00:38:23 jakllsch Exp $ */
+/* $NetBSD: tegra124_car.c,v 1.7 2016/12/17 15:24:35 riastradh Exp $ */
/*-
* Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tegra124_car.c,v 1.6 2016/09/08 00:38:23 jakllsch Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tegra124_car.c,v 1.7 2016/12/17 15:24:35 riastradh Exp $");
#include <sys/param.h>
#include <sys/bus.h>
@@ -676,10 +676,7 @@
u_int sc_clock_cells;
u_int sc_reset_cells;
- kmutex_t sc_intr_lock;
- kmutex_t sc_rnd_lock;
- u_int sc_bytes_wanted;
- void *sc_sih;
+ kmutex_t sc_rndlock;
krndsource_t sc_rndsource;
};
@@ -688,7 +685,6 @@
static void tegra124_car_xusb_init(struct tegra124_car_softc *);
static void tegra124_car_rnd_attach(device_t);
-static void tegra124_car_rnd_intr(void *);
static void tegra124_car_rnd_callback(size_t, void *);
CFATTACH_DECL_NEW(tegra124_car, sizeof(struct tegra124_car_softc),
@@ -842,61 +838,33 @@
{
struct tegra124_car_softc * const sc = device_private(self);
- mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SERIAL);
- mutex_init(&sc->sc_rnd_lock, MUTEX_DEFAULT, IPL_SERIAL);
- sc->sc_bytes_wanted = 0;
- sc->sc_sih = softint_establish(SOFTINT_SERIAL|SOFTINT_MPSAFE,
- tegra124_car_rnd_intr, sc);
- if (sc->sc_sih == NULL) {
- aprint_error_dev(sc->sc_dev, "couldn't establish softint\n");
- return;
- }
-
+ mutex_init(&sc->sc_rndlock, MUTEX_DEFAULT, IPL_VM);
rndsource_setcb(&sc->sc_rndsource, tegra124_car_rnd_callback, sc);
rnd_attach_source(&sc->sc_rndsource, device_xname(sc->sc_dev),
RND_TYPE_RNG, RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
-}
-
-static void
-tegra124_car_rnd_intr(void *priv)
-{
- struct tegra124_car_softc * const sc = priv;
- uint16_t buf[512];
- uint32_t cnt;
-
- mutex_enter(&sc->sc_intr_lock);
- while (sc->sc_bytes_wanted) {
- const u_int nbytes = MIN(sc->sc_bytes_wanted, 1024);
- for (cnt = 0; cnt < sc->sc_bytes_wanted / 2; cnt++) {
- buf[cnt] = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
- CAR_PLL_LFSR_REG) & 0xffff;
- }
- mutex_exit(&sc->sc_intr_lock);
- mutex_enter(&sc->sc_rnd_lock);
- rnd_add_data(&sc->sc_rndsource, buf, nbytes, nbytes * NBBY);
- mutex_exit(&sc->sc_rnd_lock);
- mutex_enter(&sc->sc_intr_lock);
- sc->sc_bytes_wanted -= MIN(sc->sc_bytes_wanted, nbytes);
- }
- explicit_memset(buf, 0, sizeof(buf));
- mutex_exit(&sc->sc_intr_lock);
+ tegra124_car_rnd_callback(RND_POOLBITS / NBBY, sc);
}
static void
tegra124_car_rnd_callback(size_t bytes_wanted, void *priv)
{
struct tegra124_car_softc * const sc = priv;
+ uint16_t buf[512];
+ uint32_t cnt;
- mutex_enter(&sc->sc_intr_lock);
- if (sc->sc_bytes_wanted == 0) {
- softint_schedule(sc->sc_sih);
+ mutex_enter(&sc->sc_rndlock);
+ while (bytes_wanted) {
+ const u_int nbytes = MIN(bytes_wanted, 1024);
+ for (cnt = 0; cnt < bytes_wanted / 2; cnt++) {
+ buf[cnt] = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
+ CAR_PLL_LFSR_REG) & 0xffff;
+ }
+ rnd_add_data_sync(&sc->sc_rndsource, buf, nbytes,
+ nbytes * NBBY);
+ bytes_wanted -= MIN(bytes_wanted, nbytes);
}
- if (bytes_wanted > (UINT_MAX - sc->sc_bytes_wanted)) {
- sc->sc_bytes_wanted = UINT_MAX;
- } else {
- sc->sc_bytes_wanted += bytes_wanted;
- }
- mutex_exit(&sc->sc_intr_lock);
+ explicit_memset(buf, 0, sizeof(buf));
+ mutex_exit(&sc->sc_rndlock);
}
static struct tegra_clk *
diff -r 6487bf1178df -r 27f1d9bc293a sys/arch/arm/omap/am335x_trng.c
--- a/sys/arch/arm/omap/am335x_trng.c Sat Dec 17 15:23:08 2016 +0000
+++ b/sys/arch/arm/omap/am335x_trng.c Sat Dec 17 15:24:35 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: am335x_trng.c,v 1.1 2015/06/06 14:00:32 jmcneill Exp $ */
+/* $NetBSD: am335x_trng.c,v 1.2 2016/12/17 15:24:35 riastradh Exp $ */
/*-
* Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -26,7 +26,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: am335x_trng.c,v 1.1 2015/06/06 14:00:32 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: am335x_trng.c,v 1.2 2016/12/17 15:24:35 riastradh Exp $");
#include "opt_omap.h"
@@ -34,7 +34,6 @@
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/conf.h>
-#include <sys/intr.h>
#include <sys/mutex.h>
#include <sys/bus.h>
#include <sys/rndpool.h>
@@ -58,16 +57,12 @@
Home |
Main Index |
Thread Index |
Old Index