powerpc/powernv: Un-Giant-ify opal_nvram driver

It may be possible to make this completely lock free, but for now it's using
a statically allocated bounce buffer in the softc, so it needs to be
guarded.
This commit is contained in:
Justin Hibbits 2020-01-10 01:24:49 +00:00
parent 8bfc473c0e
commit 03b6e7a627
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=356590

View file

@ -34,6 +34,7 @@
#include <sys/conf.h>
#include <sys/disk.h>
#include <sys/kernel.h>
#include <sys/mutex.h>
#include <sys/uio.h>
#include <dev/ofw/openfirm.h>
@ -56,6 +57,7 @@
struct opal_nvram_softc {
device_t sc_dev;
struct mtx sc_mtx;
uint32_t sc_size;
uint8_t *sc_buf;
vm_paddr_t sc_buf_phys;
@ -64,6 +66,9 @@ struct opal_nvram_softc {
int sc_isopen;
};
#define NVRAM_LOCK(sc) mtx_lock(&sc->sc_mtx)
#define NVRAM_UNLOCK(sc) mtx_unlock(&sc->sc_mtx)
/*
* Device interface.
*/
@ -105,7 +110,6 @@ static d_ioctl_t opal_nvram_ioctl;
static struct cdevsw opal_nvram_cdevsw = {
.d_version = D_VERSION,
.d_flags = D_NEEDGIANT,
.d_open = opal_nvram_open,
.d_close = opal_nvram_close,
.d_read = opal_nvram_read,
@ -153,6 +157,8 @@ opal_nvram_attach(device_t dev)
sc->sc_cdev = make_dev(&opal_nvram_cdevsw, 0, 0, 0, 0600,
"nvram");
sc->sc_cdev->si_drv1 = sc;
mtx_init(&sc->sc_mtx, "opal_nvram", 0, MTX_DEF);
return (0);
}
@ -168,6 +174,8 @@ opal_nvram_detach(device_t dev)
destroy_dev(sc->sc_cdev);
if (sc->sc_buf != NULL)
contigfree(sc->sc_buf, NVRAM_BUFSIZE, M_DEVBUF);
mtx_destroy(&sc->sc_mtx);
return (0);
}
@ -176,11 +184,18 @@ static int
opal_nvram_open(struct cdev *dev, int flags, int fmt, struct thread *td)
{
struct opal_nvram_softc *sc = dev->si_drv1;
int err;
err = 0;
NVRAM_LOCK(sc);
if (sc->sc_isopen)
return EBUSY;
sc->sc_isopen = 1;
return (0);
err = EBUSY;
else
sc->sc_isopen = 1;
NVRAM_UNLOCK(sc);
return (err);
}
static int
@ -188,7 +203,10 @@ opal_nvram_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
{
struct opal_nvram_softc *sc = dev->si_drv1;
NVRAM_LOCK(sc);
sc->sc_isopen = 0;
NVRAM_UNLOCK(sc);
return (0);
}
@ -199,6 +217,8 @@ opal_nvram_read(struct cdev *dev, struct uio *uio, int ioflag)
int rv, amnt;
rv = 0;
NVRAM_LOCK(sc);
while (uio->uio_resid > 0) {
amnt = MIN(uio->uio_resid, sc->sc_size - uio->uio_offset);
amnt = MIN(amnt, NVRAM_BUFSIZE);
@ -222,6 +242,8 @@ opal_nvram_read(struct cdev *dev, struct uio *uio, int ioflag)
if (rv != 0)
break;
}
NVRAM_UNLOCK(sc);
return (rv);
}
@ -233,6 +255,8 @@ opal_nvram_write(struct cdev *dev, struct uio *uio, int ioflag)
struct opal_nvram_softc *sc = dev->si_drv1;
rv = 0;
NVRAM_LOCK(sc);
while (uio->uio_resid > 0) {
amnt = MIN(uio->uio_resid, sc->sc_size - uio->uio_offset);
amnt = MIN(amnt, NVRAM_BUFSIZE);
@ -258,6 +282,9 @@ opal_nvram_write(struct cdev *dev, struct uio *uio, int ioflag)
break;
}
}
NVRAM_UNLOCK(sc);
return (rv);
}