time.c 15.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Xen time implementation.
 *
 * This is implemented in terms of a clocksource driver which uses
 * the hypervisor clock as a nanosecond timebase, and a clockevent
 * driver which uses the hypervisor's timer mechanism.
 *
 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
 */
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/clocksource.h>
#include <linux/clockchips.h>
15
#include <linux/gfp.h>
16
#include <linux/slab.h>
17
#include <linux/pvclock_gtod.h>
18
#include <linux/timekeeper_internal.h>
19

20
#include <asm/pvclock.h>
21 22 23 24
#include <asm/xen/hypervisor.h>
#include <asm/xen/hypercall.h>

#include <xen/events.h>
25
#include <xen/features.h>
26 27 28 29 30
#include <xen/interface/xen.h>
#include <xen/interface/vcpu.h>

#include "xen-ops.h"

31
/* Minimum amount of time until next clock event fires */
32
#define TIMER_SLOP	100000
33

34 35
static u64 xen_sched_clock_offset __read_mostly;

36
/* Get the TSC speed from Xen */
37
static unsigned long xen_tsc_khz(void)
38
{
39
	struct pvclock_vcpu_time_info *info =
40 41
		&HYPERVISOR_shared_info->vcpu_info[0].time;

42
	setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
43
	return pvclock_tsc_khz(info);
44 45
}

46
static u64 xen_clocksource_read(void)
47
{
48
        struct pvclock_vcpu_time_info *src;
49
	u64 ret;
50

51
	preempt_disable_notrace();
52
	src = &__this_cpu_read(xen_vcpu)->time;
53
	ret = pvclock_clocksource_read(src);
54
	preempt_enable_notrace();
55 56 57
	return ret;
}

58
static u64 xen_clocksource_get_cycles(struct clocksource *cs)
59 60 61 62
{
	return xen_clocksource_read();
}

63 64 65 66 67
static u64 xen_sched_clock(void)
{
	return xen_clocksource_read() - xen_sched_clock_offset;
}

68
static void xen_read_wallclock(struct timespec64 *ts)
69
{
70 71 72
	struct shared_info *s = HYPERVISOR_shared_info;
	struct pvclock_wall_clock *wall_clock = &(s->wc);
        struct pvclock_vcpu_time_info *vcpu_time;
73

74 75 76
	vcpu_time = &get_cpu_var(xen_vcpu)->time;
	pvclock_read_wallclock(wall_clock, vcpu_time, ts);
	put_cpu_var(xen_vcpu);
77 78
}

79
static void xen_get_wallclock(struct timespec64 *now)
80
{
81
	xen_read_wallclock(now);
82 83
}

84
static int xen_set_wallclock(const struct timespec64 *now)
85
{
86
	return -ENODEV;
87 88
}

89 90
static int xen_pvclock_gtod_notify(struct notifier_block *nb,
				   unsigned long was_set, void *priv)
91
{
92
	/* Protected by the calling core code serialization */
93
	static struct timespec64 next_sync;
94

95
	struct xen_platform_op op;
96 97 98 99
	struct timespec64 now;
	struct timekeeper *tk = priv;
	static bool settime64_supported = true;
	int ret;
100

101 102
	now.tv_sec = tk->xtime_sec;
	now.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
103

104 105 106 107
	/*
	 * We only take the expensive HV call when the clock was set
	 * or when the 11 minutes RTC synchronization time elapsed.
	 */
108
	if (!was_set && timespec64_compare(&now, &next_sync) < 0)
109
		return NOTIFY_OK;
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
again:
	if (settime64_supported) {
		op.cmd = XENPF_settime64;
		op.u.settime64.mbz = 0;
		op.u.settime64.secs = now.tv_sec;
		op.u.settime64.nsecs = now.tv_nsec;
		op.u.settime64.system_time = xen_clocksource_read();
	} else {
		op.cmd = XENPF_settime32;
		op.u.settime32.secs = now.tv_sec;
		op.u.settime32.nsecs = now.tv_nsec;
		op.u.settime32.system_time = xen_clocksource_read();
	}

	ret = HYPERVISOR_platform_op(&op);

	if (ret == -ENOSYS && settime64_supported) {
		settime64_supported = false;
		goto again;
	}
	if (ret < 0)
		return NOTIFY_BAD;
133

134 135 136 137 138 139 140 141
	/*
	 * Move the next drift compensation time 11 minutes
	 * ahead. That's emulating the sync_cmos_clock() update for
	 * the hardware RTC.
	 */
	next_sync = now;
	next_sync.tv_sec += 11 * 60;

142
	return NOTIFY_OK;
143 144
}

145 146 147 148
static struct notifier_block xen_pvclock_gtod_notifier = {
	.notifier_call = xen_pvclock_gtod_notify,
};

149 150
static int xen_cs_enable(struct clocksource *cs)
{
151
	vclocks_set_used(VDSO_CLOCKMODE_PVCLOCK);
152 153 154
	return 0;
}

155
static struct clocksource xen_clocksource __read_mostly = {
156 157 158 159 160 161
	.name	= "xen",
	.rating	= 400,
	.read	= xen_clocksource_get_cycles,
	.mask	= CLOCKSOURCE_MASK(64),
	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
	.enable = xen_cs_enable,
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
};

/*
   Xen clockevent implementation

   Xen has two clockevent implementations:

   The old timer_op one works with all released versions of Xen prior
   to version 3.0.4.  This version of the hypervisor provides a
   single-shot timer with nanosecond resolution.  However, sharing the
   same event channel is a 100Hz tick which is delivered while the
   vcpu is running.  We don't care about or use this tick, but it will
   cause the core time code to think the timer fired too soon, and
   will end up resetting it each time.  It could be filtered, but
   doing so has complications when the ktime clocksource is not yet
   the xen clocksource (ie, at boot time).

   The new vcpu_op-based timer interface allows the tick timer period
   to be changed or turned off.  The tick timer is not useful as a
   periodic timer because events are only delivered to running vcpus.
   The one-shot timer can report when a timeout is in the past, so
   set_next_event is capable of returning -ETIME when appropriate.
   This interface is used when available.
*/


/*
  Get a hypervisor absolute time.  In theory we could maintain an
  offset between the kernel's time and the hypervisor's time, and
  apply that to a kernel's absolute timeout.  Unfortunately the
  hypervisor and kernel times can drift even if the kernel is using
  the Xen clocksource, because ntp can warp the kernel's clocksource.
*/
static s64 get_abs_timeout(unsigned long delta)
{
	return xen_clocksource_read() + delta;
}

200
static int xen_timerop_shutdown(struct clock_event_device *evt)
201
{
202 203 204 205
	/* cancel timeout */
	HYPERVISOR_set_timer_op(0);

	return 0;
206 207 208 209 210
}

static int xen_timerop_set_next_event(unsigned long delta,
				      struct clock_event_device *evt)
{
211
	WARN_ON(!clockevent_state_oneshot(evt));
212 213 214 215 216 217 218 219 220 221 222

	if (HYPERVISOR_set_timer_op(get_abs_timeout(delta)) < 0)
		BUG();

	/* We may have missed the deadline, but there's no real way of
	   knowing for sure.  If the event was in the past, then we'll
	   get an immediate interrupt. */

	return 0;
}

223
static struct clock_event_device xen_timerop_clockevent __ro_after_init = {
224 225
	.name			= "xen",
	.features		= CLOCK_EVT_FEAT_ONESHOT,
226

227
	.max_delta_ns		= 0xffffffff,
228
	.max_delta_ticks	= 0xffffffff,
229
	.min_delta_ns		= TIMER_SLOP,
230
	.min_delta_ticks	= TIMER_SLOP,
231

232 233 234
	.mult			= 1,
	.shift			= 0,
	.rating			= 500,
235

236 237
	.set_state_shutdown	= xen_timerop_shutdown,
	.set_next_event		= xen_timerop_set_next_event,
238 239
};

240 241 242
static int xen_vcpuop_shutdown(struct clock_event_device *evt)
{
	int cpu = smp_processor_id();
243

244 245 246 247
	if (HYPERVISOR_vcpu_op(VCPUOP_stop_singleshot_timer, xen_vcpu_nr(cpu),
			       NULL) ||
	    HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
			       NULL))
248
		BUG();
249

250 251 252 253
	return 0;
}

static int xen_vcpuop_set_oneshot(struct clock_event_device *evt)
254 255 256
{
	int cpu = smp_processor_id();

257 258
	if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
			       NULL))
259 260 261
		BUG();

	return 0;
262 263 264 265 266 267 268 269 270
}

static int xen_vcpuop_set_next_event(unsigned long delta,
				     struct clock_event_device *evt)
{
	int cpu = smp_processor_id();
	struct vcpu_set_singleshot_timer single;
	int ret;

271
	WARN_ON(!clockevent_state_oneshot(evt));
272 273

	single.timeout_abs_ns = get_abs_timeout(delta);
274 275
	/* Get an event anyway, even if the timeout is already expired */
	single.flags = 0;
276

277 278
	ret = HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, xen_vcpu_nr(cpu),
				 &single);
279
	BUG_ON(ret != 0);
280 281 282 283

	return ret;
}

284
static struct clock_event_device xen_vcpuop_clockevent __ro_after_init = {
285 286 287 288
	.name = "xen",
	.features = CLOCK_EVT_FEAT_ONESHOT,

	.max_delta_ns = 0xffffffff,
289
	.max_delta_ticks = 0xffffffff,
290
	.min_delta_ns = TIMER_SLOP,
291
	.min_delta_ticks = TIMER_SLOP,
292 293 294 295 296

	.mult = 1,
	.shift = 0,
	.rating = 500,

297 298
	.set_state_shutdown = xen_vcpuop_shutdown,
	.set_state_oneshot = xen_vcpuop_set_oneshot,
299 300 301 302 303
	.set_next_event = xen_vcpuop_set_next_event,
};

static const struct clock_event_device *xen_clockevent =
	&xen_timerop_clockevent;
304 305 306

struct xen_clock_event_device {
	struct clock_event_device evt;
307
	char name[16];
308 309
};
static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 };
310 311 312

static irqreturn_t xen_timer_interrupt(int irq, void *dev_id)
{
313
	struct clock_event_device *evt = this_cpu_ptr(&xen_clock_events.evt);
314 315 316 317 318 319 320 321 322 323 324
	irqreturn_t ret;

	ret = IRQ_NONE;
	if (evt->event_handler) {
		evt->event_handler(evt);
		ret = IRQ_HANDLED;
	}

	return ret;
}

325 326 327 328 329 330 331 332 333 334 335
void xen_teardown_timer(int cpu)
{
	struct clock_event_device *evt;
	evt = &per_cpu(xen_clock_events, cpu).evt;

	if (evt->irq >= 0) {
		unbind_from_irqhandler(evt->irq, NULL);
		evt->irq = -1;
	}
}

Jeremy Fitzhardinge's avatar
Jeremy Fitzhardinge committed
336
void xen_setup_timer(int cpu)
337
{
338 339
	struct xen_clock_event_device *xevt = &per_cpu(xen_clock_events, cpu);
	struct clock_event_device *evt = &xevt->evt;
340 341
	int irq;

342
	WARN(evt->irq >= 0, "IRQ%d for CPU%d is already allocated\n", evt->irq, cpu);
343 344
	if (evt->irq >= 0)
		xen_teardown_timer(cpu);
345

346 347
	printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu);

348
	snprintf(xevt->name, sizeof(xevt->name), "timer%d", cpu);
349 350

	irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt,
351
				      IRQF_PERCPU|IRQF_NOBALANCING|IRQF_TIMER|
352
				      IRQF_FORCE_RESUME|IRQF_EARLY_RESUME,
353
				      xevt->name, NULL);
354
	(void)xen_set_irq_priority(irq, XEN_IRQ_PRIORITY_MAX);
355 356 357

	memcpy(evt, xen_clockevent, sizeof(*evt));

358
	evt->cpumask = cpumask_of(cpu);
359
	evt->irq = irq;
Jeremy Fitzhardinge's avatar
Jeremy Fitzhardinge committed
360 361
}

Alex Nixon's avatar
Alex Nixon committed
362

Jeremy Fitzhardinge's avatar
Jeremy Fitzhardinge committed
363 364
void xen_setup_cpu_clockevents(void)
{
365
	clockevents_register_device(this_cpu_ptr(&xen_clock_events.evt));
366 367
}

368 369 370 371 372 373 374 375
void xen_timer_resume(void)
{
	int cpu;

	if (xen_clockevent != &xen_vcpuop_clockevent)
		return;

	for_each_online_cpu(cpu) {
376 377
		if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer,
				       xen_vcpu_nr(cpu), NULL))
378 379 380 381
			BUG();
	}
}

382
static const struct pv_time_ops xen_time_ops __initconst = {
383
	.sched_clock = xen_sched_clock,
384
	.steal_clock = xen_steal_clock,
385 386
};

387
static struct pvclock_vsyscall_time_info *xen_clock __read_mostly;
388
static u64 xen_clock_value_saved;
389 390 391 392 393 394

void xen_save_time_memory_area(void)
{
	struct vcpu_register_time_memory_area t;
	int ret;

395 396
	xen_clock_value_saved = xen_clocksource_read() - xen_sched_clock_offset;

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	if (!xen_clock)
		return;

	t.addr.v = NULL;

	ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area, 0, &t);
	if (ret != 0)
		pr_notice("Cannot save secondary vcpu_time_info (err %d)",
			  ret);
	else
		clear_page(xen_clock);
}

void xen_restore_time_memory_area(void)
{
	struct vcpu_register_time_memory_area t;
	int ret;

	if (!xen_clock)
416
		goto out;
417 418 419 420 421 422

	t.addr.v = &xen_clock->pvti;

	ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area, 0, &t);

	/*
423 424 425 426 427 428 429
	 * We don't disable VDSO_CLOCKMODE_PVCLOCK entirely if it fails to
	 * register the secondary time info with Xen or if we migrated to a
	 * host without the necessary flags. On both of these cases what
	 * happens is either process seeing a zeroed out pvti or seeing no
	 * PVCLOCK_TSC_STABLE_BIT bit set. Userspace checks the latter and
	 * if 0, it discards the data in pvti and fallbacks to a system
	 * call for a reliable timestamp.
430 431 432 433
	 */
	if (ret != 0)
		pr_notice("Cannot restore secondary vcpu_time_info (err %d)",
			  ret);
434 435 436 437 438

out:
	/* Need pvclock_resume() before using xen_clocksource_read(). */
	pvclock_resume();
	xen_sched_clock_offset = xen_clocksource_read() - xen_clock_value_saved;
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
}

static void xen_setup_vsyscall_time_info(void)
{
	struct vcpu_register_time_memory_area t;
	struct pvclock_vsyscall_time_info *ti;
	int ret;

	ti = (struct pvclock_vsyscall_time_info *)get_zeroed_page(GFP_KERNEL);
	if (!ti)
		return;

	t.addr.v = &ti->pvti;

	ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area, 0, &t);
	if (ret) {
455
		pr_notice("xen: VDSO_CLOCKMODE_PVCLOCK not supported (err %d)\n", ret);
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
		free_page((unsigned long)ti);
		return;
	}

	/*
	 * If primary time info had this bit set, secondary should too since
	 * it's the same data on both just different memory regions. But we
	 * still check it in case hypervisor is buggy.
	 */
	if (!(ti->pvti.flags & PVCLOCK_TSC_STABLE_BIT)) {
		t.addr.v = NULL;
		ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area,
					 0, &t);
		if (!ret)
			free_page((unsigned long)ti);

472
		pr_notice("xen: VDSO_CLOCKMODE_PVCLOCK not supported (tsc unstable)\n");
473 474 475 476 477 478
		return;
	}

	xen_clock = ti;
	pvclock_set_pvti_cpu0_va(xen_clock);

479
	xen_clocksource.vdso_clock_mode = VDSO_CLOCKMODE_PVCLOCK;
480 481
}

482
static void __init xen_time_init(void)
483
{
484
	struct pvclock_vcpu_time_info *pvti;
485
	int cpu = smp_processor_id();
486
	struct timespec64 tp;
487

488 489 490 491
	/* As Dom0 is never moved, no penalty on using TSC there */
	if (xen_initial_domain())
		xen_clocksource.rating = 275;

492
	clocksource_register_hz(&xen_clocksource, NSEC_PER_SEC);
493

494 495
	if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
			       NULL) == 0) {
496
		/* Successfully turned off 100Hz tick, so we have the
497 498 499 500 501 502
		   vcpuop-based timer interface */
		printk(KERN_DEBUG "Xen: using vcpuop timer interface\n");
		xen_clockevent = &xen_vcpuop_clockevent;
	}

	/* Set initial system time with full resolution */
503
	xen_read_wallclock(&tp);
504
	do_settimeofday64(&tp);
505

506
	setup_force_cpu_cap(X86_FEATURE_TSC);
507

508 509 510 511 512
	/*
	 * We check ahead on the primary time info if this
	 * bit is supported hence speeding up Xen clocksource.
	 */
	pvti = &__this_cpu_read(xen_vcpu)->time;
513
	if (pvti->flags & PVCLOCK_TSC_STABLE_BIT) {
514
		pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
515 516
		xen_setup_vsyscall_time_info();
	}
517

518
	xen_setup_runstate_info(cpu);
519
	xen_setup_timer(cpu);
Jeremy Fitzhardinge's avatar
Jeremy Fitzhardinge committed
520
	xen_setup_cpu_clockevents();
521

522 523
	xen_time_setup_guest();

524 525
	if (xen_initial_domain())
		pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier);
526
}
527

528
void __init xen_init_time_ops(void)
529
{
530
	xen_sched_clock_offset = xen_clocksource_read();
531
	pv_ops.time = xen_time_ops;
532 533 534 535 536 537 538

	x86_init.timers.timer_init = xen_time_init;
	x86_init.timers.setup_percpu_clockev = x86_init_noop;
	x86_cpuinit.setup_percpu_clockev = x86_init_noop;

	x86_platform.calibrate_tsc = xen_tsc_khz;
	x86_platform.get_wallclock = xen_get_wallclock;
539 540 541
	/* Dom0 uses the native method to set the hardware RTC. */
	if (!xen_initial_domain())
		x86_platform.set_wallclock = xen_set_wallclock;
542 543
}

544
#ifdef CONFIG_XEN_PVHVM
545 546 547 548
static void xen_hvm_setup_cpu_clockevents(void)
{
	int cpu = smp_processor_id();
	xen_setup_runstate_info(cpu);
549 550 551 552 553
	/*
	 * xen_setup_timer(cpu) - snprintf is bad in atomic context. Hence
	 * doing it xen_hvm_cpu_notify (which gets called by smp_init during
	 * early bootup and also during CPU hotplug events).
	 */
554 555 556
	xen_setup_cpu_clockevents();
}

557
void __init xen_hvm_init_time_ops(void)
558
{
559 560 561 562 563 564 565 566
	/*
	 * vector callback is needed otherwise we cannot receive interrupts
	 * on cpu > 0 and at this point we don't know how many cpus are
	 * available.
	 */
	if (!xen_have_vector_callback)
		return;

567
	if (!xen_feature(XENFEAT_hvm_safe_pvclock)) {
568
		pr_info("Xen doesn't support pvclock on HVM, disable pv timer");
569 570 571
		return;
	}

572
	xen_sched_clock_offset = xen_clocksource_read();
573
	pv_ops.time = xen_time_ops;
574 575 576 577 578 579 580
	x86_init.timers.setup_percpu_clockev = xen_time_init;
	x86_cpuinit.setup_percpu_clockev = xen_hvm_setup_cpu_clockevents;

	x86_platform.calibrate_tsc = xen_tsc_khz;
	x86_platform.get_wallclock = xen_get_wallclock;
	x86_platform.set_wallclock = xen_set_wallclock;
}
581
#endif
582 583 584 585 586 587 588 589 590 591 592 593 594 595

/* Kernel parameter to specify Xen timer slop */
static int __init parse_xen_timer_slop(char *ptr)
{
	unsigned long slop = memparse(ptr, NULL);

	xen_timerop_clockevent.min_delta_ns = slop;
	xen_timerop_clockevent.min_delta_ticks = slop;
	xen_vcpuop_clockevent.min_delta_ns = slop;
	xen_vcpuop_clockevent.min_delta_ticks = slop;

	return 0;
}
early_param("xen_timer_slop", parse_xen_timer_slop);