xref: /freebsd/sys/dev/xen/timer/xen_timer.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009 Adrian Chadd
5  * Copyright (c) 2012 Spectra Logic Corporation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 /**
32  * \file dev/xen/timer/xen_timer.c
33  * \brief A timer driver for the Xen hypervisor's PV clock.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/bus.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/time.h>
45 #include <sys/timetc.h>
46 #include <sys/timeet.h>
47 #include <sys/smp.h>
48 #include <sys/limits.h>
49 #include <sys/clock.h>
50 #include <sys/proc.h>
51 
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>
58 #include <xen/error.h>
59 
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>
66 
67 #include <dev/xen/timer/timer.h>
68 
69 #include "clock_if.h"
70 
71 #define	NSEC_IN_SEC	1000000000ULL
72 #define	NSEC_IN_USEC	1000ULL
73 /* 18446744073 = int(2^64 / NSEC_IN_SC) = 1 ns in 64-bit fractions */
74 #define	FRAC_IN_NSEC	18446744073LL
75 
76 /* Xen timers may fire up to 100us off */
77 #define	XENTIMER_MIN_PERIOD_IN_NSEC	100*NSEC_IN_USEC
78 
79 /*
80  * The real resolution of the PV clock is 1ns, but the highest
81  * resolution that FreeBSD supports is 1us, so just use that.
82  */
83 #define	XENCLOCK_RESOLUTION		1
84 
85 #define	XENTIMER_QUALITY	950
86 
87 struct xentimer_pcpu_data {
88 	uint64_t timer;
89 	uint64_t last_processed;
90 	void *irq_handle;
91 };
92 
93 DPCPU_DEFINE(struct xentimer_pcpu_data, xentimer_pcpu);
94 
95 DPCPU_DECLARE(struct vcpu_info *, vcpu_info);
96 
97 struct xentimer_softc {
98 	device_t dev;
99 	struct timecounter tc;
100 	struct eventtimer et;
101 };
102 
103 static void
104 xentimer_identify(driver_t *driver, device_t parent)
105 {
106 	if (!xen_domain())
107 		return;
108 
109 	/* Handle all Xen PV timers in one device instance. */
110 	if (devclass_get_device(devclass_find(driver->name), 0))
111 		return;
112 
113 	BUS_ADD_CHILD(parent, 0, driver->name, 0);
114 }
115 
116 static int
117 xentimer_probe(device_t dev)
118 {
119 	KASSERT((xen_domain()), ("Trying to use Xen timer on bare metal"));
120 	/*
121 	 * In order to attach, this driver requires the following:
122 	 * - Vector callback support by the hypervisor, in order to deliver
123 	 *   timer interrupts to the correct CPU for CPUs other than 0.
124 	 * - Access to the hypervisor shared info page, in order to look up
125 	 *   each VCPU's timer information and the Xen wallclock time.
126 	 * - The hypervisor must say its PV clock is "safe" to use.
127 	 * - The hypervisor must support VCPUOP hypercalls.
128 	 * - The maximum number of CPUs supported by FreeBSD must not exceed
129 	 *   the number of VCPUs supported by the hypervisor.
130 	 */
131 #define	XTREQUIRES(condition, reason...)	\
132 	if (!(condition)) {			\
133 		device_printf(dev, ## reason);	\
134 		device_detach(dev);		\
135 		return (ENXIO);			\
136 	}
137 
138 	if (xen_hvm_domain()) {
139 		XTREQUIRES(xen_vector_callback_enabled,
140 		           "vector callbacks unavailable\n");
141 		XTREQUIRES(xen_feature(XENFEAT_hvm_safe_pvclock),
142 		           "HVM safe pvclock unavailable\n");
143 	}
144 	XTREQUIRES(HYPERVISOR_shared_info != NULL,
145 	           "shared info page unavailable\n");
146 	XTREQUIRES(HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, 0, NULL) == 0,
147 	           "VCPUOPs interface unavailable\n");
148 #undef XTREQUIRES
149 	device_set_desc(dev, "Xen PV Clock");
150 	return (BUS_PROBE_NOWILDCARD);
151 }
152 
153 /**
154  * \brief Get the current time, in nanoseconds, since the hypervisor booted.
155  *
156  * \param vcpu		vcpu_info structure to fetch the time from.
157  *
158  */
159 static uint64_t
160 xen_fetch_vcpu_time(struct vcpu_info *vcpu)
161 {
162 	struct pvclock_vcpu_time_info *time;
163 
164 	time = (struct pvclock_vcpu_time_info *) &vcpu->time;
165 
166 	return (pvclock_get_timecount(time));
167 }
168 
169 static uint32_t
170 xentimer_get_timecount(struct timecounter *tc)
171 {
172 	uint64_t vcpu_time;
173 
174 	/*
175 	 * We don't disable preemption here because the worst that can
176 	 * happen is reading the vcpu_info area of a different CPU than
177 	 * the one we are currently running on, but that would also
178 	 * return a valid tc (and we avoid the overhead of
179 	 * critical_{enter/exit} calls).
180 	 */
181 	vcpu_time = xen_fetch_vcpu_time(DPCPU_GET(vcpu_info));
182 
183 	return (vcpu_time & UINT32_MAX);
184 }
185 
186 /**
187  * \brief Fetch the hypervisor boot time, known as the "Xen wallclock".
188  *
189  * \param ts		Timespec to store the current stable value.
190  * \param version	Pointer to store the corresponding wallclock version.
191  *
192  * \note This value is updated when Domain-0 shifts its clock to follow
193  *       clock drift, e.g. as detected by NTP.
194  */
195 static void
196 xen_fetch_wallclock(struct timespec *ts)
197 {
198 	shared_info_t *src = HYPERVISOR_shared_info;
199 	struct pvclock_wall_clock *wc;
200 
201 	wc = (struct pvclock_wall_clock *) &src->wc_version;
202 
203 	pvclock_get_wallclock(wc, ts);
204 }
205 
206 static void
207 xen_fetch_uptime(struct timespec *ts)
208 {
209 	uint64_t uptime;
210 
211 	uptime = xen_fetch_vcpu_time(DPCPU_GET(vcpu_info));
212 
213 	ts->tv_sec = uptime / NSEC_IN_SEC;
214 	ts->tv_nsec = uptime % NSEC_IN_SEC;
215 }
216 
217 static int
218 xentimer_settime(device_t dev __unused, struct timespec *ts)
219 {
220 	struct xen_platform_op settime;
221 	int ret;
222 
223 	/*
224 	 * Don't return EINVAL here; just silently fail if the domain isn't
225 	 * privileged enough to set the TOD.
226 	 */
227 	if (!xen_initial_domain())
228 		return (0);
229 
230 	settime.cmd = XENPF_settime64;
231 	settime.u.settime64.mbz = 0;
232 	settime.u.settime64.secs = ts->tv_sec;
233 	settime.u.settime64.nsecs = ts->tv_nsec;
234 	settime.u.settime64.system_time =
235 		xen_fetch_vcpu_time(DPCPU_GET(vcpu_info));
236 
237 	ret = HYPERVISOR_platform_op(&settime);
238 	ret = ret != 0 ? xen_translate_error(ret) : 0;
239 	if (ret != 0 && bootverbose)
240 		device_printf(dev, "failed to set Xen PV clock: %d\n", ret);
241 
242 	return (ret);
243 }
244 
245 /**
246  * \brief Return current time according to the Xen Hypervisor wallclock.
247  *
248  * \param dev	Xentimer device.
249  * \param ts	Pointer to store the wallclock time.
250  *
251  * \note  The Xen time structures document the hypervisor start time and the
252  *        uptime-since-hypervisor-start (in nsec.) They need to be combined
253  *        in order to calculate a TOD clock.
254  */
255 static int
256 xentimer_gettime(device_t dev, struct timespec *ts)
257 {
258 	struct timespec u_ts;
259 
260 	timespecclear(ts);
261 	xen_fetch_wallclock(ts);
262 	xen_fetch_uptime(&u_ts);
263 	timespecadd(ts, &u_ts, ts);
264 
265 	return (0);
266 }
267 
268 /**
269  * \brief Handle a timer interrupt for the Xen PV timer driver.
270  *
271  * \param arg	Xen timer driver softc that is expecting the interrupt.
272  */
273 static int
274 xentimer_intr(void *arg)
275 {
276 	struct xentimer_softc *sc = (struct xentimer_softc *)arg;
277 	struct xentimer_pcpu_data *pcpu = DPCPU_PTR(xentimer_pcpu);
278 
279 	pcpu->last_processed = xen_fetch_vcpu_time(DPCPU_GET(vcpu_info));
280 	if (pcpu->timer != 0 && sc->et.et_active)
281 		sc->et.et_event_cb(&sc->et, sc->et.et_arg);
282 
283 	return (FILTER_HANDLED);
284 }
285 
286 static int
287 xentimer_vcpu_start_timer(int vcpu, uint64_t next_time)
288 {
289 	struct vcpu_set_singleshot_timer single;
290 
291 	single.timeout_abs_ns = next_time;
292 	/* Get an event anyway, even if the timeout is already expired */
293 	single.flags          = 0;
294 	return (HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, vcpu, &single));
295 }
296 
297 static int
298 xentimer_vcpu_stop_timer(int vcpu)
299 {
300 
301 	return (HYPERVISOR_vcpu_op(VCPUOP_stop_singleshot_timer, vcpu, NULL));
302 }
303 
304 /**
305  * \brief Set the next oneshot time for the current CPU.
306  *
307  * \param et	Xen timer driver event timer to schedule on.
308  * \param first	Delta to the next time to schedule the interrupt for.
309  * \param period Not used.
310  *
311  * \note See eventtimers(9) for more information.
312  * \note
313  *
314  * \returns 0
315  */
316 static int
317 xentimer_et_start(struct eventtimer *et,
318     sbintime_t first, sbintime_t period)
319 {
320 	int error;
321 	struct xentimer_softc *sc = et->et_priv;
322 	int cpu = PCPU_GET(vcpu_id);
323 	struct xentimer_pcpu_data *pcpu = DPCPU_PTR(xentimer_pcpu);
324 	struct vcpu_info *vcpu = DPCPU_GET(vcpu_info);
325 	uint64_t first_in_ns, next_time;
326 #ifdef INVARIANTS
327 	struct thread *td = curthread;
328 #endif
329 
330 	KASSERT(td->td_critnest != 0,
331 	    ("xentimer_et_start called without preemption disabled"));
332 
333 	/* See sbttots() for this formula. */
334 	first_in_ns = (((first >> 32) * NSEC_IN_SEC) +
335 	               (((uint64_t)NSEC_IN_SEC * (uint32_t)first) >> 32));
336 
337 	next_time = xen_fetch_vcpu_time(vcpu) + first_in_ns;
338 	error = xentimer_vcpu_start_timer(cpu, next_time);
339 	if (error)
340 		panic("%s: Error %d setting singleshot timer to %"PRIu64"\n",
341 		    device_get_nameunit(sc->dev), error, next_time);
342 
343 	pcpu->timer = next_time;
344 	return (error);
345 }
346 
347 /**
348  * \brief Cancel the event timer's currently running timer, if any.
349  */
350 static int
351 xentimer_et_stop(struct eventtimer *et)
352 {
353 	int cpu = PCPU_GET(vcpu_id);
354 	struct xentimer_pcpu_data *pcpu = DPCPU_PTR(xentimer_pcpu);
355 
356 	pcpu->timer = 0;
357 	return (xentimer_vcpu_stop_timer(cpu));
358 }
359 
360 /**
361  * \brief Attach a Xen PV timer driver instance.
362  *
363  * \param dev	Bus device object to attach.
364  *
365  * \note
366  * \returns EINVAL
367  */
368 static int
369 xentimer_attach(device_t dev)
370 {
371 	struct xentimer_softc *sc = device_get_softc(dev);
372 	int error, i;
373 
374 	sc->dev = dev;
375 
376 	/* Bind an event channel to a VIRQ on each VCPU. */
377 	CPU_FOREACH(i) {
378 		struct xentimer_pcpu_data *pcpu;
379 
380 		pcpu = DPCPU_ID_PTR(i, xentimer_pcpu);
381 		error = HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, i, NULL);
382 		if (error) {
383 			device_printf(dev, "Error disabling Xen periodic timer "
384 			                   "on CPU %d\n", i);
385 			return (error);
386 		}
387 
388 		error = xen_intr_bind_virq(dev, VIRQ_TIMER, i, xentimer_intr,
389 		    NULL, sc, INTR_TYPE_CLK, &pcpu->irq_handle);
390 		if (error) {
391 			device_printf(dev, "Error %d binding VIRQ_TIMER "
392 			    "to VCPU %d\n", error, i);
393 			return (error);
394 		}
395 		xen_intr_describe(pcpu->irq_handle, "c%d", i);
396 	}
397 
398 	/* Register the event timer. */
399 	sc->et.et_name = "XENTIMER";
400 	sc->et.et_quality = XENTIMER_QUALITY;
401 	sc->et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU;
402 	sc->et.et_frequency = NSEC_IN_SEC;
403 	/* See tstosbt() for this formula */
404 	sc->et.et_min_period = (XENTIMER_MIN_PERIOD_IN_NSEC *
405 	                        (((uint64_t)1 << 63) / 500000000) >> 32);
406 	sc->et.et_max_period = ((sbintime_t)4 << 32);
407 	sc->et.et_start = xentimer_et_start;
408 	sc->et.et_stop = xentimer_et_stop;
409 	sc->et.et_priv = sc;
410 	et_register(&sc->et);
411 
412 	/* Register the timecounter. */
413 	sc->tc.tc_name = "XENTIMER";
414 	sc->tc.tc_quality = XENTIMER_QUALITY;
415 	/*
416 	 * FIXME: due to the lack of ordering during resume, FreeBSD cannot
417 	 * guarantee that the Xen PV timer is resumed before any other device
418 	 * attempts to make use of it, so mark it as not safe for suspension
419 	 * (ie: remove the TC_FLAGS_SUSPEND_SAFE flag).
420 	 *
421 	 * NB: This was not a problem in previous FreeBSD versions because the
422 	 * timer was directly attached to the nexus, but it is an issue now
423 	 * that the timer is attached to the xenpv bus, and thus resumed
424 	 * later.
425 	 *
426 	 * sc->tc.tc_flags = TC_FLAGS_SUSPEND_SAFE;
427 	 */
428     	/*
429 	 * The underlying resolution is in nanoseconds, since the timer info
430 	 * scales TSC frequencies using a fraction that represents time in
431 	 * terms of nanoseconds.
432 	 */
433 	sc->tc.tc_frequency = NSEC_IN_SEC;
434 	sc->tc.tc_counter_mask = ~0u;
435 	sc->tc.tc_get_timecount = xentimer_get_timecount;
436 	sc->tc.tc_priv = sc;
437 	tc_init(&sc->tc);
438 
439 	/* Register the Hypervisor wall clock */
440 	clock_register(dev, XENCLOCK_RESOLUTION);
441 
442 	return (0);
443 }
444 
445 static int
446 xentimer_detach(device_t dev)
447 {
448 
449 	/* Implement Xen PV clock teardown - XXX see hpet_detach ? */
450 	/* If possible:
451 	 * 1. need to deregister timecounter
452 	 * 2. need to deregister event timer
453 	 * 3. need to deregister virtual IRQ event channels
454 	 */
455 	return (EBUSY);
456 }
457 
458 static void
459 xentimer_percpu_resume(void *arg)
460 {
461 	device_t dev = (device_t) arg;
462 	struct xentimer_softc *sc = device_get_softc(dev);
463 
464 	xentimer_et_start(&sc->et, sc->et.et_min_period, 0);
465 }
466 
467 static int
468 xentimer_resume(device_t dev)
469 {
470 	int error;
471 	int i;
472 
473 	/* Disable the periodic timer */
474 	CPU_FOREACH(i) {
475 		error = HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, i, NULL);
476 		if (error != 0) {
477 			device_printf(dev,
478 			    "Error disabling Xen periodic timer on CPU %d\n",
479 			    i);
480 			return (error);
481 		}
482 	}
483 
484 	/* Reset the last uptime value */
485 	pvclock_resume();
486 
487 	/* Reset the RTC clock */
488 	inittodr(time_second);
489 
490 	/* Kick the timers on all CPUs */
491 	smp_rendezvous(NULL, xentimer_percpu_resume, NULL, dev);
492 
493 	if (bootverbose)
494 		device_printf(dev, "resumed operation after suspension\n");
495 
496 	return (0);
497 }
498 
499 static int
500 xentimer_suspend(device_t dev)
501 {
502 	return (0);
503 }
504 
505 /*
506  * Xen early clock init
507  */
508 void
509 xen_clock_init(void)
510 {
511 }
512 
513 /*
514  * Xen PV DELAY function
515  *
516  * When running on PVH mode we don't have an emulated i8524, so
517  * make use of the Xen time info in order to code a simple DELAY
518  * function that can be used during early boot.
519  */
520 void
521 xen_delay(int n)
522 {
523 	struct vcpu_info *vcpu = &HYPERVISOR_shared_info->vcpu_info[0];
524 	uint64_t end_ns;
525 	uint64_t current;
526 
527 	end_ns = xen_fetch_vcpu_time(vcpu);
528 	end_ns += n * NSEC_IN_USEC;
529 
530 	for (;;) {
531 		current = xen_fetch_vcpu_time(vcpu);
532 		if (current >= end_ns)
533 			break;
534 	}
535 }
536 
537 static device_method_t xentimer_methods[] = {
538 	DEVMETHOD(device_identify, xentimer_identify),
539 	DEVMETHOD(device_probe, xentimer_probe),
540 	DEVMETHOD(device_attach, xentimer_attach),
541 	DEVMETHOD(device_detach, xentimer_detach),
542 	DEVMETHOD(device_suspend, xentimer_suspend),
543 	DEVMETHOD(device_resume, xentimer_resume),
544 	/* clock interface */
545 	DEVMETHOD(clock_gettime, xentimer_gettime),
546 	DEVMETHOD(clock_settime, xentimer_settime),
547 	DEVMETHOD_END
548 };
549 
550 static driver_t xentimer_driver = {
551 	"xen_et",
552 	xentimer_methods,
553 	sizeof(struct xentimer_softc),
554 };
555 
556 DRIVER_MODULE(xentimer, xenpv, xentimer_driver, 0, 0);
557 MODULE_DEPEND(xentimer, xenpv, 1, 1, 1);
558