#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/time.h>
#include <sys/timetc.h>
#include <sys/timeet.h>
#include <sys/smp.h>
#include <sys/limits.h>
#include <sys/clock.h>
#include <sys/proc.h>
#include <xen/xen-os.h>
#include <xen/features.h>
#include <xen/xen_intr.h>
#include <xen/hypervisor.h>
#include <contrib/xen/io/xenbus.h>
#include <contrib/xen/vcpu.h>
#include <xen/error.h>
#include <machine/cpu.h>
#include <machine/cpufunc.h>
#include <machine/clock.h>
#include <machine/_inttypes.h>
#include <machine/smp.h>
#include <machine/pvclock.h>
#include <dev/xen/timer/timer.h>
#include "clock_if.h"
#define NSEC_IN_SEC 1000000000ULL
#define NSEC_IN_USEC 1000ULL
#define FRAC_IN_NSEC 18446744073LL
#define XENTIMER_MIN_PERIOD_IN_NSEC 100*NSEC_IN_USEC
#define XENCLOCK_RESOLUTION 1
#define XENTIMER_QUALITY 950
struct xentimer_pcpu_data {
uint64_t timer;
uint64_t last_processed;
xen_intr_handle_t irq_handle;
};
DPCPU_DEFINE(struct xentimer_pcpu_data, xentimer_pcpu);
DPCPU_DECLARE(struct vcpu_info *, vcpu_info);
struct xentimer_softc {
device_t dev;
struct timecounter tc;
struct eventtimer et;
};
static void
xentimer_identify(driver_t *driver, device_t parent)
{
if (!xen_domain())
return;
if (devclass_get_device(devclass_find(driver->name), 0))
return;
BUS_ADD_CHILD(parent, 0, driver->name, 0);
}
static int
xentimer_probe(device_t dev)
{
KASSERT((xen_domain()), ("Trying to use Xen timer on bare metal"));
#define XTREQUIRES(condition, reason...) \
if (!(condition)) { \
device_printf(dev, ## reason); \
device_detach(dev); \
return (ENXIO); \
}
if (xen_hvm_domain()) {
XTREQUIRES(xen_vector_callback_enabled,
"vector callbacks unavailable\n");
XTREQUIRES(xen_feature(XENFEAT_hvm_safe_pvclock),
"HVM safe pvclock unavailable\n");
}
XTREQUIRES(HYPERVISOR_shared_info != NULL,
"shared info page unavailable\n");
XTREQUIRES(HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, 0, NULL) == 0,
"VCPUOPs interface unavailable\n");
#undef XTREQUIRES
device_set_desc(dev, "Xen PV Clock");
return (BUS_PROBE_NOWILDCARD);
}
static uint64_t
xen_fetch_vcpu_time(struct vcpu_info *vcpu)
{
struct pvclock_vcpu_time_info *time;
time = (struct pvclock_vcpu_time_info *) &vcpu->time;
return (pvclock_get_timecount(time));
}
static uint32_t
xentimer_get_timecount(struct timecounter *tc)
{
uint64_t vcpu_time;
vcpu_time = xen_fetch_vcpu_time(DPCPU_GET(vcpu_info));
return (vcpu_time & UINT32_MAX);
}
static void
xen_fetch_wallclock(struct timespec *ts)
{
shared_info_t *src = HYPERVISOR_shared_info;
struct pvclock_wall_clock *wc;
wc = (struct pvclock_wall_clock *) &src->wc_version;
pvclock_get_wallclock(wc, ts);
}
static void
xen_fetch_uptime(struct timespec *ts)
{
uint64_t uptime;
uptime = xen_fetch_vcpu_time(DPCPU_GET(vcpu_info));
ts->tv_sec = uptime / NSEC_IN_SEC;
ts->tv_nsec = uptime % NSEC_IN_SEC;
}
static int
xentimer_settime(device_t dev __unused, struct timespec *ts)
{
struct xen_platform_op settime;
int ret;
if (!xen_initial_domain())
return (0);
settime.cmd = XENPF_settime64;
settime.u.settime64.mbz = 0;
settime.u.settime64.secs = ts->tv_sec;
settime.u.settime64.nsecs = ts->tv_nsec;
settime.u.settime64.system_time =
xen_fetch_vcpu_time(DPCPU_GET(vcpu_info));
ret = HYPERVISOR_platform_op(&settime);
ret = ret != 0 ? xen_translate_error(ret) : 0;
if (ret != 0 && bootverbose)
device_printf(dev, "failed to set Xen PV clock: %d\n", ret);
return (ret);
}
static int
xentimer_gettime(device_t dev, struct timespec *ts)
{
struct timespec u_ts;
timespecclear(ts);
xen_fetch_wallclock(ts);
xen_fetch_uptime(&u_ts);
timespecadd(ts, &u_ts, ts);
return (0);
}
static int
xentimer_intr(void *arg)
{
struct xentimer_softc *sc = (struct xentimer_softc *)arg;
struct xentimer_pcpu_data *pcpu = DPCPU_PTR(xentimer_pcpu);
pcpu->last_processed = xen_fetch_vcpu_time(DPCPU_GET(vcpu_info));
if (pcpu->timer != 0 && sc->et.et_active)
sc->et.et_event_cb(&sc->et, sc->et.et_arg);
return (FILTER_HANDLED);
}
static int
xentimer_vcpu_start_timer(int vcpu, uint64_t next_time)
{
struct vcpu_set_singleshot_timer single;
single.timeout_abs_ns = next_time;
single.flags = 0;
return (HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, vcpu, &single));
}
static int
xentimer_vcpu_stop_timer(int vcpu)
{
return (HYPERVISOR_vcpu_op(VCPUOP_stop_singleshot_timer, vcpu, NULL));
}
static int
xentimer_et_start(struct eventtimer *et,
sbintime_t first, sbintime_t period)
{
int error;
struct xentimer_softc *sc = et->et_priv;
int cpu = PCPU_GET(vcpu_id);
struct xentimer_pcpu_data *pcpu = DPCPU_PTR(xentimer_pcpu);
struct vcpu_info *vcpu = DPCPU_GET(vcpu_info);
uint64_t first_in_ns, next_time;
#ifdef INVARIANTS
struct thread *td = curthread;
#endif
KASSERT(td->td_critnest != 0,
("xentimer_et_start called without preemption disabled"));
first_in_ns = (((first >> 32) * NSEC_IN_SEC) +
(((uint64_t)NSEC_IN_SEC * (uint32_t)first) >> 32));
next_time = xen_fetch_vcpu_time(vcpu) + first_in_ns;
error = xentimer_vcpu_start_timer(cpu, next_time);
if (error)
panic("%s: Error %d setting singleshot timer to %"PRIu64"\n",
device_get_nameunit(sc->dev), error, next_time);
pcpu->timer = next_time;
return (error);
}
static int
xentimer_et_stop(struct eventtimer *et)
{
int cpu = PCPU_GET(vcpu_id);
struct xentimer_pcpu_data *pcpu = DPCPU_PTR(xentimer_pcpu);
pcpu->timer = 0;
return (xentimer_vcpu_stop_timer(cpu));
}
static int
xentimer_attach(device_t dev)
{
struct xentimer_softc *sc = device_get_softc(dev);
int error, i;
sc->dev = dev;
CPU_FOREACH(i) {
struct xentimer_pcpu_data *pcpu;
pcpu = DPCPU_ID_PTR(i, xentimer_pcpu);
error = HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, i, NULL);
if (error) {
device_printf(dev, "Error disabling Xen periodic timer "
"on CPU %d\n", i);
return (error);
}
error = xen_intr_bind_virq(dev, VIRQ_TIMER, i, xentimer_intr,
NULL, sc, INTR_TYPE_CLK, &pcpu->irq_handle);
if (error) {
device_printf(dev, "Error %d binding VIRQ_TIMER "
"to VCPU %d\n", error, i);
return (error);
}
xen_intr_describe(pcpu->irq_handle, "c%d", i);
}
sc->et.et_name = "XENTIMER";
sc->et.et_quality = XENTIMER_QUALITY;
sc->et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU;
sc->et.et_frequency = NSEC_IN_SEC;
sc->et.et_min_period = (XENTIMER_MIN_PERIOD_IN_NSEC *
(((uint64_t)1 << 63) / 500000000) >> 32);
sc->et.et_max_period = ((sbintime_t)4 << 32);
sc->et.et_start = xentimer_et_start;
sc->et.et_stop = xentimer_et_stop;
sc->et.et_priv = sc;
et_register(&sc->et);
sc->tc.tc_name = "XENTIMER";
sc->tc.tc_quality = XENTIMER_QUALITY;
sc->tc.tc_frequency = NSEC_IN_SEC;
sc->tc.tc_counter_mask = ~0u;
sc->tc.tc_get_timecount = xentimer_get_timecount;
sc->tc.tc_priv = sc;
tc_init(&sc->tc);
clock_register(dev, XENCLOCK_RESOLUTION);
return (0);
}
static int
xentimer_detach(device_t dev)
{
return (EBUSY);
}
static void
xentimer_percpu_resume(void *arg)
{
device_t dev = (device_t) arg;
struct xentimer_softc *sc = device_get_softc(dev);
xentimer_et_start(&sc->et, sc->et.et_min_period, 0);
}
static int
xentimer_resume(device_t dev)
{
int error;
int i;
CPU_FOREACH(i) {
error = HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, i, NULL);
if (error != 0) {
device_printf(dev,
"Error disabling Xen periodic timer on CPU %d\n",
i);
return (error);
}
}
pvclock_resume();
inittodr(time_second);
smp_rendezvous(NULL, xentimer_percpu_resume, NULL, dev);
if (bootverbose)
device_printf(dev, "resumed operation after suspension\n");
return (0);
}
static int
xentimer_suspend(device_t dev)
{
return (0);
}
void
xen_clock_init(void)
{
}
void
xen_delay(int n)
{
struct vcpu_info *vcpu = &HYPERVISOR_shared_info->vcpu_info[0];
uint64_t end_ns;
uint64_t current;
end_ns = xen_fetch_vcpu_time(vcpu);
end_ns += n * NSEC_IN_USEC;
for (;;) {
current = xen_fetch_vcpu_time(vcpu);
if (current >= end_ns)
break;
}
}
static device_method_t xentimer_methods[] = {
DEVMETHOD(device_identify, xentimer_identify),
DEVMETHOD(device_probe, xentimer_probe),
DEVMETHOD(device_attach, xentimer_attach),
DEVMETHOD(device_detach, xentimer_detach),
DEVMETHOD(device_suspend, xentimer_suspend),
DEVMETHOD(device_resume, xentimer_resume),
DEVMETHOD(clock_gettime, xentimer_gettime),
DEVMETHOD(clock_settime, xentimer_settime),
DEVMETHOD_END
};
static driver_t xentimer_driver = {
"xen_et",
xentimer_methods,
sizeof(struct xentimer_softc),
};
DRIVER_MODULE(xentimer, xenpv, xentimer_driver, 0, 0);
MODULE_DEPEND(xentimer, xenpv, 1, 1, 1);