#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/ofw/openfirm.h>
#include <dev/vmm/vmm_vm.h>
#include "riscv.h"
#define VTIMER_DEFAULT_FREQ 1000000
static int
vtimer_get_timebase(uint32_t *freq)
{
phandle_t node;
int len;
node = OF_finddevice("/cpus");
if (node == -1)
return (ENXIO);
len = OF_getproplen(node, "timebase-frequency");
if (len != 4)
return (ENXIO);
OF_getencprop(node, "timebase-frequency", freq, len);
return (0);
}
void
vtimer_cpuinit(struct hypctx *hypctx)
{
struct vtimer *vtimer;
uint32_t freq;
int error;
vtimer = &hypctx->vtimer;
mtx_init(&vtimer->mtx, "vtimer callout mutex", NULL, MTX_DEF);
callout_init_mtx(&vtimer->callout, &vtimer->mtx, 0);
error = vtimer_get_timebase(&freq);
if (error)
freq = VTIMER_DEFAULT_FREQ;
vtimer->freq = freq;
}
static void
vtimer_inject_irq_callout(void *arg)
{
struct hypctx *hypctx;
struct hyp *hyp;
hypctx = arg;
hyp = hypctx->hyp;
atomic_set_32(&hypctx->interrupts_pending, HVIP_VSTIP);
vcpu_notify_event(vm_vcpu(hyp->vm, hypctx->cpu_id));
}
int
vtimer_set_timer(struct hypctx *hypctx, uint64_t next_val)
{
struct vtimer *vtimer;
sbintime_t time;
uint64_t curtime;
uint64_t delta;
vtimer = &hypctx->vtimer;
curtime = rdtime();
if (curtime < next_val) {
delta = next_val - curtime;
time = delta * SBT_1S / vtimer->freq;
atomic_clear_32(&hypctx->interrupts_pending, HVIP_VSTIP);
callout_reset_sbt(&vtimer->callout, time, 0,
vtimer_inject_irq_callout, hypctx, 0);
} else
atomic_set_32(&hypctx->interrupts_pending, HVIP_VSTIP);
return (0);
}