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