2 * Copyright (c) 2009 Adrian Chadd
3 * Copyright (c) 2012 Spectra Logic Corporation
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * \file dev/xen/timer/timer.c
31 * \brief A timer driver for the Xen hypervisor's PV clock.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/timetc.h>
44 #include <sys/timeet.h>
46 #include <sys/limits.h>
47 #include <sys/clock.h>
50 #include <xen/xen-os.h>
51 #include <xen/features.h>
52 #include <xen/xen_intr.h>
53 #include <xen/hypervisor.h>
54 #include <xen/interface/io/xenbus.h>
55 #include <xen/interface/vcpu.h>
57 #include <machine/cpu.h>
58 #include <machine/cpufunc.h>
59 #include <machine/clock.h>
60 #include <machine/_inttypes.h>
61 #include <machine/smp.h>
62 #include <machine/pvclock.h>
64 #include <dev/xen/timer/timer.h>
68 static devclass_t xentimer_devclass;
70 #define NSEC_IN_SEC 1000000000ULL
71 #define NSEC_IN_USEC 1000ULL
72 /* 18446744073 = int(2^64 / NSEC_IN_SC) = 1 ns in 64-bit fractions */
73 #define FRAC_IN_NSEC 18446744073LL
75 /* Xen timers may fire up to 100us off */
76 #define XENTIMER_MIN_PERIOD_IN_NSEC 100*NSEC_IN_USEC
77 #define XENCLOCK_RESOLUTION 10000000
79 #define ETIME 62 /* Xen "bad time" error */
81 #define XENTIMER_QUALITY 950
83 struct xentimer_pcpu_data {
85 uint64_t last_processed;
89 DPCPU_DEFINE(struct xentimer_pcpu_data, xentimer_pcpu);
91 DPCPU_DECLARE(struct vcpu_info *, vcpu_info);
93 struct xentimer_softc {
95 struct timecounter tc;
100 xentimer_identify(driver_t *driver, device_t parent)
105 /* Handle all Xen PV timers in one device instance. */
106 if (devclass_get_device(xentimer_devclass, 0))
109 BUS_ADD_CHILD(parent, 0, "xen_et", 0);
113 xentimer_probe(device_t dev)
115 KASSERT((xen_domain()), ("Trying to use Xen timer on bare metal"));
117 * In order to attach, this driver requires the following:
118 * - Vector callback support by the hypervisor, in order to deliver
119 * timer interrupts to the correct CPU for CPUs other than 0.
120 * - Access to the hypervisor shared info page, in order to look up
121 * each VCPU's timer information and the Xen wallclock time.
122 * - The hypervisor must say its PV clock is "safe" to use.
123 * - The hypervisor must support VCPUOP hypercalls.
124 * - The maximum number of CPUs supported by FreeBSD must not exceed
125 * the number of VCPUs supported by the hypervisor.
127 #define XTREQUIRES(condition, reason...) \
128 if (!(condition)) { \
129 device_printf(dev, ## reason); \
130 device_detach(dev); \
134 if (xen_hvm_domain()) {
135 XTREQUIRES(xen_vector_callback_enabled,
136 "vector callbacks unavailable\n");
137 XTREQUIRES(xen_feature(XENFEAT_hvm_safe_pvclock),
138 "HVM safe pvclock unavailable\n");
140 XTREQUIRES(HYPERVISOR_shared_info != NULL,
141 "shared info page unavailable\n");
142 XTREQUIRES(HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, 0, NULL) == 0,
143 "VCPUOPs interface unavailable\n");
145 device_set_desc(dev, "Xen PV Clock");
146 return (BUS_PROBE_NOWILDCARD);
150 * \brief Get the current time, in nanoseconds, since the hypervisor booted.
152 * \param vcpu vcpu_info structure to fetch the time from.
156 xen_fetch_vcpu_time(struct vcpu_info *vcpu)
158 struct pvclock_vcpu_time_info *time;
160 time = (struct pvclock_vcpu_time_info *) &vcpu->time;
162 return (pvclock_get_timecount(time));
166 xentimer_get_timecount(struct timecounter *tc)
171 * We don't disable preemption here because the worst that can
172 * happen is reading the vcpu_info area of a different CPU than
173 * the one we are currently running on, but that would also
174 * return a valid tc (and we avoid the overhead of
175 * critical_{enter/exit} calls).
177 vcpu_time = xen_fetch_vcpu_time(DPCPU_GET(vcpu_info));
179 return (vcpu_time & UINT32_MAX);
183 * \brief Fetch the hypervisor boot time, known as the "Xen wallclock".
185 * \param ts Timespec to store the current stable value.
186 * \param version Pointer to store the corresponding wallclock version.
188 * \note This value is updated when Domain-0 shifts its clock to follow
189 * clock drift, e.g. as detected by NTP.
192 xen_fetch_wallclock(struct timespec *ts)
194 shared_info_t *src = HYPERVISOR_shared_info;
195 struct pvclock_wall_clock *wc;
197 wc = (struct pvclock_wall_clock *) &src->wc_version;
199 pvclock_get_wallclock(wc, ts);
203 xen_fetch_uptime(struct timespec *ts)
207 uptime = xen_fetch_vcpu_time(DPCPU_GET(vcpu_info));
209 ts->tv_sec = uptime / NSEC_IN_SEC;
210 ts->tv_nsec = uptime % NSEC_IN_SEC;
214 xentimer_settime(device_t dev __unused, struct timespec *ts)
217 * Don't return EINVAL here; just silently fail if the domain isn't
218 * privileged enough to set the TOD.
224 * \brief Return current time according to the Xen Hypervisor wallclock.
226 * \param dev Xentimer device.
227 * \param ts Pointer to store the wallclock time.
229 * \note The Xen time structures document the hypervisor start time and the
230 * uptime-since-hypervisor-start (in nsec.) They need to be combined
231 * in order to calculate a TOD clock.
234 xentimer_gettime(device_t dev, struct timespec *ts)
236 struct timespec u_ts;
239 xen_fetch_wallclock(ts);
240 xen_fetch_uptime(&u_ts);
241 timespecadd(ts, &u_ts);
247 * \brief Handle a timer interrupt for the Xen PV timer driver.
249 * \param arg Xen timer driver softc that is expecting the interrupt.
252 xentimer_intr(void *arg)
254 struct xentimer_softc *sc = (struct xentimer_softc *)arg;
255 struct xentimer_pcpu_data *pcpu = DPCPU_PTR(xentimer_pcpu);
257 pcpu->last_processed = xen_fetch_vcpu_time(DPCPU_GET(vcpu_info));
258 if (pcpu->timer != 0 && sc->et.et_active)
259 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
261 return (FILTER_HANDLED);
265 xentimer_vcpu_start_timer(int vcpu, uint64_t next_time)
267 struct vcpu_set_singleshot_timer single;
269 single.timeout_abs_ns = next_time;
270 single.flags = VCPU_SSHOTTMR_future;
271 return (HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, vcpu, &single));
275 xentimer_vcpu_stop_timer(int vcpu)
278 return (HYPERVISOR_vcpu_op(VCPUOP_stop_singleshot_timer, vcpu, NULL));
282 * \brief Set the next oneshot time for the current CPU.
284 * \param et Xen timer driver event timer to schedule on.
285 * \param first Delta to the next time to schedule the interrupt for.
286 * \param period Not used.
288 * \note See eventtimers(9) for more information.
294 xentimer_et_start(struct eventtimer *et,
295 sbintime_t first, sbintime_t period)
297 int error = 0, i = 0;
298 struct xentimer_softc *sc = et->et_priv;
299 int cpu = PCPU_GET(vcpu_id);
300 struct xentimer_pcpu_data *pcpu = DPCPU_PTR(xentimer_pcpu);
301 struct vcpu_info *vcpu = DPCPU_GET(vcpu_info);
302 uint64_t first_in_ns, next_time;
304 struct thread *td = curthread;
307 KASSERT(td->td_critnest != 0,
308 ("xentimer_et_start called without preemption disabled"));
310 /* See sbttots() for this formula. */
311 first_in_ns = (((first >> 32) * NSEC_IN_SEC) +
312 (((uint64_t)NSEC_IN_SEC * (uint32_t)first) >> 32));
315 * Retry any timer scheduling failures, where the hypervisor
316 * returns -ETIME. Sometimes even a 100us timer period isn't large
317 * enough, but larger period instances are relatively uncommon.
319 * XXX Remove the panics once et_start() and its consumers are
320 * equipped to deal with start failures.
324 panic("can't schedule timer");
325 next_time = xen_fetch_vcpu_time(vcpu) + first_in_ns;
326 error = xentimer_vcpu_start_timer(cpu, next_time);
327 } while (error == -ETIME);
330 panic("%s: Error %d setting singleshot timer to %"PRIu64"\n",
331 device_get_nameunit(sc->dev), error, next_time);
333 pcpu->timer = next_time;
338 * \brief Cancel the event timer's currently running timer, if any.
341 xentimer_et_stop(struct eventtimer *et)
343 int cpu = PCPU_GET(vcpu_id);
344 struct xentimer_pcpu_data *pcpu = DPCPU_PTR(xentimer_pcpu);
347 return (xentimer_vcpu_stop_timer(cpu));
351 * \brief Attach a Xen PV timer driver instance.
353 * \param dev Bus device object to attach.
359 xentimer_attach(device_t dev)
361 struct xentimer_softc *sc = device_get_softc(dev);
366 /* Bind an event channel to a VIRQ on each VCPU. */
368 struct xentimer_pcpu_data *pcpu;
370 pcpu = DPCPU_ID_PTR(i, xentimer_pcpu);
371 error = HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, i, NULL);
373 device_printf(dev, "Error disabling Xen periodic timer "
378 error = xen_intr_bind_virq(dev, VIRQ_TIMER, i, xentimer_intr,
379 NULL, sc, INTR_TYPE_CLK, &pcpu->irq_handle);
381 device_printf(dev, "Error %d binding VIRQ_TIMER "
382 "to VCPU %d\n", error, i);
385 xen_intr_describe(pcpu->irq_handle, "c%d", i);
388 /* Register the event timer. */
389 sc->et.et_name = "XENTIMER";
390 sc->et.et_quality = XENTIMER_QUALITY;
391 sc->et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU;
392 sc->et.et_frequency = NSEC_IN_SEC;
393 /* See tstosbt() for this formula */
394 sc->et.et_min_period = (XENTIMER_MIN_PERIOD_IN_NSEC *
395 (((uint64_t)1 << 63) / 500000000) >> 32);
396 sc->et.et_max_period = ((sbintime_t)4 << 32);
397 sc->et.et_start = xentimer_et_start;
398 sc->et.et_stop = xentimer_et_stop;
400 et_register(&sc->et);
402 /* Register the timecounter. */
403 sc->tc.tc_name = "XENTIMER";
404 sc->tc.tc_quality = XENTIMER_QUALITY;
405 sc->tc.tc_flags = TC_FLAGS_SUSPEND_SAFE;
407 * The underlying resolution is in nanoseconds, since the timer info
408 * scales TSC frequencies using a fraction that represents time in
409 * terms of nanoseconds.
411 sc->tc.tc_frequency = NSEC_IN_SEC;
412 sc->tc.tc_counter_mask = ~0u;
413 sc->tc.tc_get_timecount = xentimer_get_timecount;
417 /* Register the Hypervisor wall clock */
418 clock_register(dev, XENCLOCK_RESOLUTION);
424 xentimer_detach(device_t dev)
427 /* Implement Xen PV clock teardown - XXX see hpet_detach ? */
429 * 1. need to deregister timecounter
430 * 2. need to deregister event timer
431 * 3. need to deregister virtual IRQ event channels
437 xentimer_percpu_resume(void *arg)
439 device_t dev = (device_t) arg;
440 struct xentimer_softc *sc = device_get_softc(dev);
442 xentimer_et_start(&sc->et, sc->et.et_min_period, 0);
446 xentimer_resume(device_t dev)
451 /* Disable the periodic timer */
453 error = HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, i, NULL);
456 "Error disabling Xen periodic timer on CPU %d\n",
462 /* Reset the last uptime value */
465 /* Reset the RTC clock */
466 inittodr(time_second);
468 /* Kick the timers on all CPUs */
469 smp_rendezvous(NULL, xentimer_percpu_resume, NULL, dev);
472 device_printf(dev, "resumed operation after suspension\n");
478 xentimer_suspend(device_t dev)
484 * Xen early clock init
492 * Xen PV DELAY function
494 * When running on PVH mode we don't have an emulated i8524, so
495 * make use of the Xen time info in order to code a simple DELAY
496 * function that can be used during early boot.
501 struct vcpu_info *vcpu = &HYPERVISOR_shared_info->vcpu_info[0];
505 end_ns = xen_fetch_vcpu_time(vcpu);
506 end_ns += n * NSEC_IN_USEC;
509 current = xen_fetch_vcpu_time(vcpu);
510 if (current >= end_ns)
515 static device_method_t xentimer_methods[] = {
516 DEVMETHOD(device_identify, xentimer_identify),
517 DEVMETHOD(device_probe, xentimer_probe),
518 DEVMETHOD(device_attach, xentimer_attach),
519 DEVMETHOD(device_detach, xentimer_detach),
520 DEVMETHOD(device_suspend, xentimer_suspend),
521 DEVMETHOD(device_resume, xentimer_resume),
522 /* clock interface */
523 DEVMETHOD(clock_gettime, xentimer_gettime),
524 DEVMETHOD(clock_settime, xentimer_settime),
528 static driver_t xentimer_driver = {
531 sizeof(struct xentimer_softc),
534 DRIVER_MODULE(xentimer, xenpv, xentimer_driver, xentimer_devclass, 0, 0);
535 MODULE_DEPEND(xentimer, xenpv, 1, 1, 1);