Adapt CI20 HWRNG to synchronous on-demand callback.

Omit needless softint/locking dance.
from riastradh@
This commit is contained in:
macallan 2016-02-17 20:12:42 +00:00
parent f91581fb8e
commit 677061ce2a
1 changed files with 16 additions and 60 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: ingenic_rng.c,v 1.3 2015/11/17 16:53:21 macallan Exp $ */
/* $NetBSD: ingenic_rng.c,v 1.4 2016/02/17 20:12:42 macallan Exp $ */
/*-
* Copyright (c) 2015 Michael McConville
@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ingenic_rng.c,v 1.3 2015/11/17 16:53:21 macallan Exp $");
__KERNEL_RCSID(0, "$NetBSD: ingenic_rng.c,v 1.4 2016/02/17 20:12:42 macallan Exp $");
/*
* adapted from Jared McNeill's amlogic_rng.c
@ -39,9 +39,7 @@ __KERNEL_RCSID(0, "$NetBSD: ingenic_rng.c,v 1.3 2015/11/17 16:53:21 macallan Exp
#include <sys/mutex.h>
#include <sys/kernel.h>
#include <sys/mutex.h>
#include <sys/callout.h>
#include <sys/bus.h>
#include <sys/workqueue.h>
#include <sys/rndpool.h>
#include <sys/rndsource.h>
@ -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,32 @@ ingenic_rng_attach(device_t parent, device_t self, void *aux)
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);
rndsource_setcb(&sc->sc_rndsource, ingenic_rng_get, 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),
delay(1);
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);
}