42#include <sys/kernel.h>
43#include <sys/module.h>
45#include <sys/timetc.h>
46#include <sys/timeet.h>
48#include <sys/limits.h>
52#include <xen/xen-os.h>
53#include <xen/features.h>
54#include <xen/xen_intr.h>
55#include <xen/hypervisor.h>
56#include <contrib/xen/io/xenbus.h>
57#include <contrib/xen/vcpu.h>
60#include <machine/cpu.h>
61#include <machine/cpufunc.h>
62#include <machine/clock.h>
63#include <machine/_inttypes.h>
64#include <machine/smp.h>
65#include <machine/pvclock.h>
73#define NSEC_IN_SEC 1000000000ULL
74#define NSEC_IN_USEC 1000ULL
76#define FRAC_IN_NSEC 18446744073LL
79#define XENTIMER_MIN_PERIOD_IN_NSEC 100*NSEC_IN_USEC
85#define XENCLOCK_RESOLUTION 1
87#define XENTIMER_QUALITY 950
101 struct timecounter
tc;
102 struct eventtimer
et;
115 BUS_ADD_CHILD(parent, 0,
"xen_et", 0);
121 KASSERT((xen_domain()), (
"Trying to use Xen timer on bare metal"));
133#define XTREQUIRES(condition, reason...) \
134 if (!(condition)) { \
135 device_printf(dev, ## reason); \
136 device_detach(dev); \
140 if (xen_hvm_domain()) {
142 "vector callbacks unavailable\n");
143 XTREQUIRES(xen_feature(XENFEAT_hvm_safe_pvclock),
144 "HVM safe pvclock unavailable\n");
147 "shared info page unavailable\n");
148 XTREQUIRES(HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, 0, NULL) == 0,
149 "VCPUOPs interface unavailable\n");
151 device_set_desc(dev,
"Xen PV Clock");
152 return (BUS_PROBE_NOWILDCARD);
164 struct pvclock_vcpu_time_info *time;
166 time = (
struct pvclock_vcpu_time_info *) &vcpu->time;
168 return (pvclock_get_timecount(time));
185 return (vcpu_time & UINT32_MAX);
200 shared_info_t *src = HYPERVISOR_shared_info;
201 struct pvclock_wall_clock *wc;
203 wc = (
struct pvclock_wall_clock *) &src->wc_version;
205 pvclock_get_wallclock(wc, ts);
222 struct xen_platform_op settime;
229 if (!xen_initial_domain())
232 settime.cmd = XENPF_settime64;
233 settime.u.settime64.mbz = 0;
234 settime.u.settime64.secs = ts->tv_sec;
235 settime.u.settime64.nsecs = ts->tv_nsec;
236 settime.u.settime64.system_time =
239 ret = HYPERVISOR_platform_op(&settime);
240 ret = ret != 0 ? xen_translate_error(ret) : 0;
241 if (ret != 0 && bootverbose)
242 device_printf(dev,
"failed to set Xen PV clock: %d\n", ret);
260 struct timespec u_ts;
265 timespecadd(ts, &u_ts, ts);
282 if (pcpu->
timer != 0 && sc->
et.et_active)
283 sc->
et.et_event_cb(&sc->
et, sc->
et.et_arg);
285 return (FILTER_HANDLED);
291 struct vcpu_set_singleshot_timer single;
293 single.timeout_abs_ns = next_time;
296 return (HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, vcpu, &single));
303 return (HYPERVISOR_vcpu_op(VCPUOP_stop_singleshot_timer, vcpu, NULL));
320 sbintime_t first, sbintime_t period)
324 int cpu = PCPU_GET(vcpu_id);
326 struct vcpu_info *vcpu = DPCPU_GET(vcpu_info);
327 uint64_t first_in_ns, next_time;
329 struct thread *td = curthread;
332 KASSERT(td->td_critnest != 0,
333 (
"xentimer_et_start called without preemption disabled"));
337 (((uint64_t)
NSEC_IN_SEC * (uint32_t)first) >> 32));
342 panic(
"%s: Error %d setting singleshot timer to %"PRIu64
"\n",
343 device_get_nameunit(sc->
dev), error, next_time);
345 pcpu->
timer = next_time;
355 int cpu = PCPU_GET(vcpu_id);
382 pcpu = DPCPU_ID_PTR(i, xentimer_pcpu);
383 error = HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, i, NULL);
385 device_printf(dev,
"Error disabling Xen periodic timer "
390 error = xen_intr_bind_virq(dev, VIRQ_TIMER, i,
xentimer_intr,
393 device_printf(dev,
"Error %d binding VIRQ_TIMER "
394 "to VCPU %d\n", error, i);
397 xen_intr_describe(pcpu->
irq_handle,
"c%d", i);
401 sc->
et.et_name =
"XENTIMER";
403 sc->
et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU;
407 (((uint64_t)1 << 63) / 500000000) >> 32);
408 sc->
et.et_max_period = ((sbintime_t)4 << 32);
412 et_register(&sc->
et);
415 sc->
tc.tc_name =
"XENTIMER";
436 sc->
tc.tc_counter_mask = ~0u;
463 device_t dev = (device_t) arg;
477 error = HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, i, NULL);
480 "Error disabling Xen periodic timer on CPU %d\n",
490 inittodr(time_second);
496 device_printf(
dev,
"resumed operation after suspension\n");
525 struct vcpu_info *vcpu = &HYPERVISOR_shared_info->vcpu_info[0];
534 if (current >= end_ns)
static int xentimer_et_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
Set the next oneshot time for the current CPU.
#define XTREQUIRES(condition, reason...)
static int xentimer_detach(device_t dev)
static uint32_t xentimer_get_timecount(struct timecounter *tc)
void xen_clock_init(void)
static int xentimer_probe(device_t dev)
static driver_t xentimer_driver
static device_method_t xentimer_methods[]
static uint64_t xen_fetch_vcpu_time(struct vcpu_info *vcpu)
Get the current time, in nanoseconds, since the hypervisor booted.
MODULE_DEPEND(xentimer, xenpv, 1, 1, 1)
#define XENTIMER_MIN_PERIOD_IN_NSEC
static void xen_fetch_wallclock(struct timespec *ts)
Fetch the hypervisor boot time, known as the "Xen wallclock".
static int xentimer_vcpu_stop_timer(int vcpu)
static int xentimer_attach(device_t dev)
Attach a Xen PV timer driver instance.
static int xentimer_settime(device_t dev __unused, struct timespec *ts)
static int xentimer_gettime(device_t dev, struct timespec *ts)
Return current time according to the Xen Hypervisor wallclock.
static int xentimer_vcpu_start_timer(int vcpu, uint64_t next_time)
static int xentimer_suspend(device_t dev)
static void xentimer_percpu_resume(void *arg)
static void xen_fetch_uptime(struct timespec *ts)
#define XENCLOCK_RESOLUTION
static int xentimer_et_stop(struct eventtimer *et)
Cancel the event timer's currently running timer, if any.
DPCPU_DECLARE(struct vcpu_info *, vcpu_info)
DPCPU_DEFINE(struct xentimer_pcpu_data, xentimer_pcpu)
static int xentimer_resume(device_t dev)
static int xentimer_intr(void *arg)
Handle a timer interrupt for the Xen PV timer driver.
static devclass_t xentimer_devclass
DRIVER_MODULE(xentimer, xenpv, xentimer_driver, xentimer_devclass, 0, 0)
static void xentimer_identify(driver_t *driver, device_t parent)