#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/limits.h>
#include <sys/module.h>
#include <sys/rman.h>
#include <sys/machintr.h>
#include "virtio_mmio.h"
#define VTMMIO_KENV_MAXDEV 32
static device_t vtmmio_kenv_children[VTMMIO_KENV_MAXDEV];
static int vtmmio_kenv_nchildren;
static void
vtmmio_parsearg(driver_t *driver, device_t parent, char *arg)
{
device_t child;
char *p;
unsigned long sz;
unsigned long baseaddr;
unsigned long irq;
unsigned long unit;
sz = strtoul(arg, &p, 0);
if ((sz == 0) || (sz == ULONG_MAX))
goto bad;
switch (*p) {
case 'E': case 'e':
sz <<= 10;
case 'P': case 'p':
sz <<= 10;
case 'T': case 't':
sz <<= 10;
case 'G': case 'g':
sz <<= 10;
case 'M': case 'm':
sz <<= 10;
case 'K': case 'k':
sz <<= 10;
p++;
break;
}
if (*p++ != '@')
goto bad;
baseaddr = strtoul(p, &p, 0);
if ((baseaddr == 0) || (baseaddr == ULONG_MAX))
goto bad;
if (*p++ != ':')
goto bad;
irq = strtoul(p, &p, 0);
if ((irq == 0) || (irq == ULONG_MAX))
goto bad;
if (*p) {
if (*p++ != ':')
goto bad;
unit = strtoul(p, &p, 0);
if ((unit == 0) || (unit == ULONG_MAX))
goto bad;
} else {
unit = 0;
}
if (*p)
goto bad;
child = BUS_ADD_CHILD(parent, parent, 0, "virtio_mmio",
unit ? (int)unit : -1);
if (child == NULL)
return;
device_set_driver(child, driver);
bus_set_resource(child, SYS_RES_MEMORY, 0, baseaddr, sz, -1);
bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1,
machintr_legacy_intr_cpuid(irq));
if (vtmmio_kenv_nchildren < VTMMIO_KENV_MAXDEV)
vtmmio_kenv_children[vtmmio_kenv_nchildren++] = child;
return;
bad:
kprintf("Error parsing hw.virtio.mmio.device parameter: %s\n", arg);
}
static void
vtmmio_kenv_identify(driver_t *driver, device_t parent)
{
char name[32];
char *val;
size_t n;
if ((val = kgetenv("hw.virtio.mmio.device")) == NULL)
return;
vtmmio_parsearg(driver, parent, val);
kfreeenv(val);
for (n = 1; n <= 9999; n++) {
ksnprintf(name, sizeof(name), "hw.virtio.mmio.device_%zu", n);
if ((val = kgetenv(name)) == NULL)
return;
vtmmio_parsearg(driver, parent, val);
kfreeenv(val);
}
}
static device_method_t vtmmio_kenv_methods[] = {
DEVMETHOD(device_identify, vtmmio_kenv_identify),
DEVMETHOD(device_probe, vtmmio_probe),
DEVMETHOD_END
};
DEFINE_CLASS_1(virtio_mmio, vtmmio_kenv_driver, vtmmio_kenv_methods,
sizeof(struct vtmmio_softc), vtmmio_driver);
static devclass_t vtmmio_kenv_devclass;
static int
vtmmio_kenv_modevent(module_t mod __unused, int type, void *arg __unused)
{
int i;
if (type == MOD_UNLOAD) {
for (i = 0; i < vtmmio_kenv_nchildren; i++) {
if (vtmmio_kenv_children[i] == NULL)
continue;
device_delete_child(
device_get_parent(vtmmio_kenv_children[i]),
vtmmio_kenv_children[i]);
vtmmio_kenv_children[i] = NULL;
}
vtmmio_kenv_nchildren = 0;
}
return (0);
}
DRIVER_MODULE(virtio_mmio_kenv, nexus, vtmmio_kenv_driver, vtmmio_kenv_devclass,
vtmmio_kenv_modevent, NULL);