Index: sys/arch/arm/amlogic/amlogic_rng.c =================================================================== RCS file: /cvsroot/src/sys/arch/arm/amlogic/amlogic_rng.c,v retrieving revision 1.3 diff -p -u -r1.3 amlogic_rng.c --- sys/arch/arm/amlogic/amlogic_rng.c 13 Apr 2015 21:18:40 -0000 1.3 +++ sys/arch/arm/amlogic/amlogic_rng.c 17 Feb 2016 02:33:49 -0000 @@ -37,7 +37,6 @@ __KERNEL_RCSID(0, "$NetBSD: amlogic_rng. #include #include #include -#include #include #include @@ -49,22 +48,15 @@ struct amlogic_rng_softc; static int amlogic_rng_match(device_t, cfdata_t, void *); static void amlogic_rng_attach(device_t, device_t, void *); -static void amlogic_rng_get(struct amlogic_rng_softc *); -static void amlogic_rng_get_intr(void *); -static void amlogic_rng_get_cb(size_t, void *); +static void amlogic_rng_get(size_t, void *); struct amlogic_rng_softc { device_t sc_dev; bus_space_tag_t sc_bst; bus_space_handle_t sc_bsh; - void * sc_sih; - + kmutex_t sc_lock; krndsource_t sc_rndsource; - size_t sc_bytes_wanted; - - kmutex_t sc_intr_lock; - kmutex_t sc_rnd_lock; }; CFATTACH_DECL_NEW(amlogic_rng, sizeof(struct amlogic_rng_softc), @@ -88,69 +80,33 @@ amlogic_rng_attach(device_t parent, devi bus_space_subregion(aio->aio_core_bst, aio->aio_bsh, loc->loc_offset, loc->loc_size, &sc->sc_bsh); - mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SERIAL); - mutex_init(&sc->sc_rnd_lock, MUTEX_DEFAULT, IPL_SERIAL); - + mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); + amlogic_rng_init(); aprint_naive("\n"); aprint_normal("\n"); - sc->sc_sih = softint_establish(SOFTINT_SERIAL|SOFTINT_MPSAFE, - amlogic_rng_get_intr, sc); - if (sc->sc_sih == NULL) { - aprint_error_dev(self, "couldn't establish softint\n"); - return; - } - - rndsource_setcb(&sc->sc_rndsource, amlogic_rng_get_cb, sc); + rndsource_setcb(&sc->sc_rndsource, amlogic_rng_get, sc); rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG, RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB); - amlogic_rng_get_cb(RND_POOLBITS / NBBY, sc); + amlogic_rng_get(RND_POOLBITS / NBBY, sc); } static void -amlogic_rng_get(struct amlogic_rng_softc *sc) +amlogic_rng_get(size_t bytes_wanted, void *priv) { + struct amlogic_rng_softc * const sc = priv; uint32_t data[2]; - mutex_spin_enter(&sc->sc_intr_lock); - while (sc->sc_bytes_wanted) { + mutex_spin_enter(&sc->sc_lock); + while (bytes_wanted) { bus_space_read_region_4(sc->sc_bst, sc->sc_bsh, 0, data, 2); - mutex_spin_exit(&sc->sc_intr_lock); - mutex_spin_enter(&sc->sc_rnd_lock); - rnd_add_data(&sc->sc_rndsource, data, sizeof(data), + rnd_add_data_sync(&sc->sc_rndsource, data, sizeof(data), sizeof(data) * NBBY); - mutex_spin_exit(&sc->sc_rnd_lock); - mutex_spin_enter(&sc->sc_intr_lock); - sc->sc_bytes_wanted -= MIN(sc->sc_bytes_wanted, sizeof(data)); + bytes_wanted -= MIN(bytes_wanted, sizeof(data)); } explicit_memset(data, 0, sizeof(data)); - mutex_spin_exit(&sc->sc_intr_lock); -} - -static void -amlogic_rng_get_cb(size_t bytes_wanted, void *priv) -{ - struct amlogic_rng_softc * const sc = priv; - - 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 -amlogic_rng_get_intr(void *priv) -{ - struct amlogic_rng_softc * const sc = priv; - - amlogic_rng_get(sc); + mutex_spin_exit(&sc->sc_lock); } Index: sys/arch/arm/broadcom/bcm2835_rng.c =================================================================== RCS file: /cvsroot/src/sys/arch/arm/broadcom/bcm2835_rng.c,v retrieving revision 1.11 diff -p -u -r1.11 bcm2835_rng.c --- sys/arch/arm/broadcom/bcm2835_rng.c 13 Apr 2015 21:18:40 -0000 1.11 +++ sys/arch/arm/broadcom/bcm2835_rng.c 17 Feb 2016 02:33:49 -0000 @@ -39,8 +39,6 @@ __KERNEL_RCSID(0, "$NetBSD: bcm2835_rng. #include #include #include -#include -#include #include #include @@ -60,19 +58,13 @@ struct bcm2835rng_softc { 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 @@ bcmrng_attach(device_t parent, device_t 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 @@ bcmrng_attach(device_t parent, device_t 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_rnd_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 @@ bcmrng_get(struct bcm2835rng_softc *sc) 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); -} - -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); + mutex_spin_exit(&sc->sc_lock); } Index: sys/arch/arm/nvidia/tegra124_car.c =================================================================== RCS file: /cvsroot/src/sys/arch/arm/nvidia/tegra124_car.c,v retrieving revision 1.2 diff -p -u -r1.2 tegra124_car.c --- sys/arch/arm/nvidia/tegra124_car.c 23 Dec 2015 12:43:25 -0000 1.2 +++ sys/arch/arm/nvidia/tegra124_car.c 17 Feb 2016 02:33:50 -0000 @@ -616,10 +616,7 @@ struct tegra124_car_softc { 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; }; @@ -627,7 +624,6 @@ static void tegra124_car_init(struct teg static void tegra124_car_utmip_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), @@ -735,61 +731,33 @@ tegra124_car_rnd_attach(device_t self) { 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); + tegra124_car_rnd_callback(RND_POOLBITS / NBBY, sc); } static void -tegra124_car_rnd_intr(void *priv) +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); - 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++) { + mutex_enter(&sc->sc_rndlock); + while (bytes_wanted) { + const u_int nbytes = MIN(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; } - 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); + rnd_add_data_sync(&sc->sc_rndsource, buf, nbytes, + nbytes * NBBY); + bytes_wanted -= MIN(bytes_wanted, nbytes); } explicit_memset(buf, 0, sizeof(buf)); - mutex_exit(&sc->sc_intr_lock); -} - -static void -tegra124_car_rnd_callback(size_t bytes_wanted, void *priv) -{ - struct tegra124_car_softc * const sc = priv; - - mutex_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_exit(&sc->sc_intr_lock); + mutex_exit(&sc->sc_rndlock); } static struct tegra_clk * Index: sys/arch/arm/omap/am335x_trng.c =================================================================== RCS file: /cvsroot/src/sys/arch/arm/omap/am335x_trng.c,v retrieving revision 1.1 diff -p -u -r1.1 am335x_trng.c --- sys/arch/arm/omap/am335x_trng.c 6 Jun 2015 14:00:32 -0000 1.1 +++ sys/arch/arm/omap/am335x_trng.c 17 Feb 2016 02:33:50 -0000 @@ -34,7 +34,6 @@ __KERNEL_RCSID(0, "$NetBSD: am335x_trng. #include #include #include -#include #include #include #include @@ -58,16 +57,12 @@ struct trng_softc { bus_space_tag_t sc_iot; bus_space_handle_t sc_ioh; - kmutex_t sc_intr_lock; - kmutex_t sc_rnd_lock; - u_int sc_bytes_wanted; - void *sc_sih; + kmutex_t sc_lock; krndsource_t sc_rndsource; }; static int trng_match(device_t, cfdata_t, void *); static void trng_attach(device_t, device_t, void *); -static void trng_softintr(void *); static void trng_callback(size_t, void *); CFATTACH_DECL_NEW(trng, sizeof(struct trng_softc), @@ -104,15 +99,8 @@ trng_attach(device_t parent, device_t se aprint_error(": couldn't map address spcae\n"); return; } - 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, - trng_softintr, sc); - if (sc->sc_sih == NULL) { - aprint_error(": couldn't establish softint\n"); - return; - } + + mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); prcm_module_enable(&rng_module); @@ -136,14 +124,14 @@ trng_attach(device_t parent, device_t se } static void -trng_softintr(void *priv) +trng_callback(size_t bytes_wanted, void *priv) { struct trng_softc * const sc = priv; uint32_t buf[2]; u_int retry; - mutex_enter(&sc->sc_intr_lock); - while (sc->sc_bytes_wanted) { + mutex_enter(&sc->sc_lock); + while (bytes_wanted) { for (retry = 10; retry > 0; retry--) { if (TRNG_READ(sc, TRNG_STATUS_REG) & TRNG_STATUS_READY) break; @@ -154,31 +142,10 @@ trng_softintr(void *priv) buf[0] = TRNG_READ(sc, TRNG_OUTPUT_L_REG); buf[1] = TRNG_READ(sc, TRNG_OUTPUT_H_REG); TRNG_WRITE(sc, TRNG_INTACK_REG, TRNG_INTACK_READY); - mutex_exit(&sc->sc_intr_lock); - mutex_enter(&sc->sc_rnd_lock); - rnd_add_data(&sc->sc_rndsource, buf, sizeof(buf), + rnd_add_data_sync(&sc->sc_rndsource, buf, sizeof(buf), sizeof(buf) * NBBY); - mutex_exit(&sc->sc_rnd_lock); - mutex_enter(&sc->sc_intr_lock); - sc->sc_bytes_wanted -= MIN(sc->sc_bytes_wanted, sizeof(buf)); + bytes_wanted -= MIN(bytes_wanted, sizeof(buf)); } explicit_memset(buf, 0, sizeof(buf)); - mutex_exit(&sc->sc_intr_lock); -} - -static void -trng_callback(size_t bytes_wanted, void *priv) -{ - struct trng_softc * const sc = priv; - - mutex_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_exit(&sc->sc_intr_lock); + mutex_exit(&sc->sc_lock); } Index: sys/arch/mips/ingenic/ingenic_rng.c =================================================================== RCS file: /cvsroot/src/sys/arch/mips/ingenic/ingenic_rng.c,v retrieving revision 1.3 diff -p -u -r1.3 ingenic_rng.c --- sys/arch/mips/ingenic/ingenic_rng.c 17 Nov 2015 16:53:21 -0000 1.3 +++ sys/arch/mips/ingenic/ingenic_rng.c 17 Feb 2016 02:33:50 -0000 @@ -39,9 +39,7 @@ __KERNEL_RCSID(0, "$NetBSD: ingenic_rng. #include #include #include -#include #include -#include #include #include @@ -55,22 +53,15 @@ struct ingenic_rng_softc; static int ingenic_rng_match(device_t, cfdata_t, void *); static void ingenic_rng_attach(device_t, device_t, void *); -static void ingenic_rng_get(struct ingenic_rng_softc *); -static void ingenic_rng_get_intr(void *); -static void ingenic_rng_get_cb(size_t, void *); +static void ingenic_rng_get(size_t, void *); struct ingenic_rng_softc { device_t sc_dev; bus_space_tag_t sc_bst; bus_space_handle_t sc_bsh; - void * sc_sih; - + kmutex_t sc_lock; krndsource_t sc_rndsource; - size_t sc_bytes_wanted; - - kmutex_t sc_intr_lock; - kmutex_t sc_rnd_lock; }; CFATTACH_DECL_NEW(ingenic_rng, sizeof(struct ingenic_rng_softc), @@ -104,67 +95,31 @@ ingenic_rng_attach(device_t parent, devi return; } - mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SERIAL); - mutex_init(&sc->sc_rnd_lock, MUTEX_DEFAULT, IPL_SERIAL); - + mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); + aprint_naive(": Ingenic random number generator\n"); aprint_normal(": Ingenic random number generator\n"); - sc->sc_sih = softint_establish(SOFTINT_SERIAL|SOFTINT_MPSAFE, - ingenic_rng_get_intr, sc); - if (sc->sc_sih == NULL) { - aprint_error_dev(self, "couldn't establish softint\n"); - return; - } - rndsource_setcb(&sc->sc_rndsource, ingenic_rng_get_cb, sc); rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG, RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB); - ingenic_rng_get_cb(RND_POOLBITS / NBBY, sc); + ingenic_rng_get(RND_POOLBITS / NBBY, sc); } static void -ingenic_rng_get(struct ingenic_rng_softc *sc) +ingenic_rng_get(size_t bytes_wanted, void *priv) { + struct ingenic_rng_softc * const sc = priv; uint32_t data; - mutex_spin_enter(&sc->sc_intr_lock); - while (sc->sc_bytes_wanted) { + mutex_spin_enter(&sc->sc_lock); + while (bytes_wanted) { data = bus_space_read_4(sc->sc_bst, sc->sc_bsh, 0); - mutex_spin_exit(&sc->sc_intr_lock); - mutex_spin_enter(&sc->sc_rnd_lock); - rnd_add_data(&sc->sc_rndsource, &data, sizeof(data), + rnd_add_data_sync(&sc->sc_rndsource, &data, sizeof(data), sizeof(data) * NBBY); - mutex_spin_exit(&sc->sc_rnd_lock); - mutex_spin_enter(&sc->sc_intr_lock); - sc->sc_bytes_wanted -= MIN(sc->sc_bytes_wanted, sizeof(data)); + bytes_wanted -= MIN(bytes_wanted, sizeof(data)); } explicit_memset(&data, 0, sizeof(data)); - mutex_spin_exit(&sc->sc_intr_lock); -} - -static void -ingenic_rng_get_cb(size_t bytes_wanted, void *priv) -{ - struct ingenic_rng_softc * const sc = priv; - - 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 -ingenic_rng_get_intr(void *priv) -{ - struct ingenic_rng_softc * const sc = priv; - - ingenic_rng_get(sc); + mutex_spin_exit(&sc->sc_lock); }