time.c 11.3 KB
Newer Older
J
Jeremy Fitzhardinge 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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>
14
#include <linux/gfp.h>
15
#include <linux/slab.h>
16
#include <linux/pvclock_gtod.h>
17
#include <linux/timekeeper_internal.h>
J
Jeremy Fitzhardinge 已提交
18

19
#include <asm/pvclock.h>
J
Jeremy Fitzhardinge 已提交
20 21 22 23
#include <asm/xen/hypervisor.h>
#include <asm/xen/hypercall.h>

#include <xen/events.h>
24
#include <xen/features.h>
J
Jeremy Fitzhardinge 已提交
25 26 27 28 29 30 31
#include <xen/interface/xen.h>
#include <xen/interface/vcpu.h>

#include "xen-ops.h"

/* Xen may fire a timer up to this many ns early */
#define TIMER_SLOP	100000
32

33
/* Get the TSC speed from Xen */
34
static unsigned long xen_tsc_khz(void)
J
Jeremy Fitzhardinge 已提交
35
{
36
	struct pvclock_vcpu_time_info *info =
J
Jeremy Fitzhardinge 已提交
37 38
		&HYPERVISOR_shared_info->vcpu_info[0].time;

39
	return pvclock_tsc_khz(info);
J
Jeremy Fitzhardinge 已提交
40 41
}

42
u64 xen_clocksource_read(void)
J
Jeremy Fitzhardinge 已提交
43
{
44
        struct pvclock_vcpu_time_info *src;
45
	u64 ret;
J
Jeremy Fitzhardinge 已提交
46

47
	preempt_disable_notrace();
48
	src = &__this_cpu_read(xen_vcpu)->time;
49
	ret = pvclock_clocksource_read(src);
50
	preempt_enable_notrace();
J
Jeremy Fitzhardinge 已提交
51 52 53
	return ret;
}

54
static u64 xen_clocksource_get_cycles(struct clocksource *cs)
55 56 57 58
{
	return xen_clocksource_read();
}

J
Jeremy Fitzhardinge 已提交
59 60
static void xen_read_wallclock(struct timespec *ts)
{
61 62 63
	struct shared_info *s = HYPERVISOR_shared_info;
	struct pvclock_wall_clock *wall_clock = &(s->wc);
        struct pvclock_vcpu_time_info *vcpu_time;
J
Jeremy Fitzhardinge 已提交
64

65 66 67
	vcpu_time = &get_cpu_var(xen_vcpu)->time;
	pvclock_read_wallclock(wall_clock, vcpu_time, ts);
	put_cpu_var(xen_vcpu);
J
Jeremy Fitzhardinge 已提交
68 69
}

70
static void xen_get_wallclock(struct timespec *now)
J
Jeremy Fitzhardinge 已提交
71
{
72
	xen_read_wallclock(now);
J
Jeremy Fitzhardinge 已提交
73 74
}

75
static int xen_set_wallclock(const struct timespec *now)
J
Jeremy Fitzhardinge 已提交
76
{
77
	return -1;
J
Jeremy Fitzhardinge 已提交
78 79
}

80 81
static int xen_pvclock_gtod_notify(struct notifier_block *nb,
				   unsigned long was_set, void *priv)
J
Jeremy Fitzhardinge 已提交
82
{
83
	/* Protected by the calling core code serialization */
84
	static struct timespec64 next_sync;
85

86
	struct xen_platform_op op;
87 88 89 90
	struct timespec64 now;
	struct timekeeper *tk = priv;
	static bool settime64_supported = true;
	int ret;
91

92 93
	now.tv_sec = tk->xtime_sec;
	now.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
94

95 96 97 98
	/*
	 * We only take the expensive HV call when the clock was set
	 * or when the 11 minutes RTC synchronization time elapsed.
	 */
99
	if (!was_set && timespec64_compare(&now, &next_sync) < 0)
100
		return NOTIFY_OK;
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
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;
124

125 126 127 128 129 130 131 132
	/*
	 * 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;

133
	return NOTIFY_OK;
J
Jeremy Fitzhardinge 已提交
134 135
}

136 137 138 139
static struct notifier_block xen_pvclock_gtod_notifier = {
	.notifier_call = xen_pvclock_gtod_notify,
};

J
Jeremy Fitzhardinge 已提交
140 141 142
static struct clocksource xen_clocksource __read_mostly = {
	.name = "xen",
	.rating = 400,
143
	.read = xen_clocksource_get_cycles,
J
Jeremy Fitzhardinge 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	.mask = ~0,
	.flags = CLOCK_SOURCE_IS_CONTINUOUS,
};

/*
   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;
}

184
static int xen_timerop_shutdown(struct clock_event_device *evt)
J
Jeremy Fitzhardinge 已提交
185
{
186 187 188 189
	/* cancel timeout */
	HYPERVISOR_set_timer_op(0);

	return 0;
J
Jeremy Fitzhardinge 已提交
190 191 192 193 194
}

static int xen_timerop_set_next_event(unsigned long delta,
				      struct clock_event_device *evt)
{
195
	WARN_ON(!clockevent_state_oneshot(evt));
J
Jeremy Fitzhardinge 已提交
196 197 198 199 200 201 202 203 204 205 206 207

	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;
}

static const struct clock_event_device xen_timerop_clockevent = {
208 209
	.name			= "xen",
	.features		= CLOCK_EVT_FEAT_ONESHOT,
J
Jeremy Fitzhardinge 已提交
210

211
	.max_delta_ns		= 0xffffffff,
212
	.max_delta_ticks	= 0xffffffff,
213
	.min_delta_ns		= TIMER_SLOP,
214
	.min_delta_ticks	= TIMER_SLOP,
J
Jeremy Fitzhardinge 已提交
215

216 217 218
	.mult			= 1,
	.shift			= 0,
	.rating			= 500,
J
Jeremy Fitzhardinge 已提交
219

220 221
	.set_state_shutdown	= xen_timerop_shutdown,
	.set_next_event		= xen_timerop_set_next_event,
J
Jeremy Fitzhardinge 已提交
222 223
};

224 225 226
static int xen_vcpuop_shutdown(struct clock_event_device *evt)
{
	int cpu = smp_processor_id();
J
Jeremy Fitzhardinge 已提交
227

228 229 230 231
	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))
232
		BUG();
J
Jeremy Fitzhardinge 已提交
233

234 235 236 237
	return 0;
}

static int xen_vcpuop_set_oneshot(struct clock_event_device *evt)
J
Jeremy Fitzhardinge 已提交
238 239 240
{
	int cpu = smp_processor_id();

241 242
	if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
			       NULL))
243 244 245
		BUG();

	return 0;
J
Jeremy Fitzhardinge 已提交
246 247 248 249 250 251 252 253 254
}

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;

255
	WARN_ON(!clockevent_state_oneshot(evt));
J
Jeremy Fitzhardinge 已提交
256 257

	single.timeout_abs_ns = get_abs_timeout(delta);
258 259
	/* Get an event anyway, even if the timeout is already expired */
	single.flags = 0;
J
Jeremy Fitzhardinge 已提交
260

261 262
	ret = HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, xen_vcpu_nr(cpu),
				 &single);
263
	BUG_ON(ret != 0);
J
Jeremy Fitzhardinge 已提交
264 265 266 267 268 269 270 271 272

	return ret;
}

static const struct clock_event_device xen_vcpuop_clockevent = {
	.name = "xen",
	.features = CLOCK_EVT_FEAT_ONESHOT,

	.max_delta_ns = 0xffffffff,
273
	.max_delta_ticks = 0xffffffff,
J
Jeremy Fitzhardinge 已提交
274
	.min_delta_ns = TIMER_SLOP,
275
	.min_delta_ticks = TIMER_SLOP,
J
Jeremy Fitzhardinge 已提交
276 277 278 279 280

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

281 282
	.set_state_shutdown = xen_vcpuop_shutdown,
	.set_state_oneshot = xen_vcpuop_set_oneshot,
J
Jeremy Fitzhardinge 已提交
283 284 285 286 287
	.set_next_event = xen_vcpuop_set_next_event,
};

static const struct clock_event_device *xen_clockevent =
	&xen_timerop_clockevent;
288 289 290

struct xen_clock_event_device {
	struct clock_event_device evt;
291
	char name[16];
292 293
};
static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 };
J
Jeremy Fitzhardinge 已提交
294 295 296

static irqreturn_t xen_timer_interrupt(int irq, void *dev_id)
{
297
	struct clock_event_device *evt = this_cpu_ptr(&xen_clock_events.evt);
J
Jeremy Fitzhardinge 已提交
298 299 300 301 302 303 304 305 306 307 308
	irqreturn_t ret;

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

	return ret;
}

309 310 311 312 313 314 315 316 317 318 319 320
void xen_teardown_timer(int cpu)
{
	struct clock_event_device *evt;
	BUG_ON(cpu == 0);
	evt = &per_cpu(xen_clock_events, cpu).evt;

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

J
Jeremy Fitzhardinge 已提交
321
void xen_setup_timer(int cpu)
J
Jeremy Fitzhardinge 已提交
322
{
323 324
	struct xen_clock_event_device *xevt = &per_cpu(xen_clock_events, cpu);
	struct clock_event_device *evt = &xevt->evt;
J
Jeremy Fitzhardinge 已提交
325 326
	int irq;

327
	WARN(evt->irq >= 0, "IRQ%d for CPU%d is already allocated\n", evt->irq, cpu);
328 329
	if (evt->irq >= 0)
		xen_teardown_timer(cpu);
330

J
Jeremy Fitzhardinge 已提交
331 332
	printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu);

333
	snprintf(xevt->name, sizeof(xevt->name), "timer%d", cpu);
J
Jeremy Fitzhardinge 已提交
334 335

	irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt,
336
				      IRQF_PERCPU|IRQF_NOBALANCING|IRQF_TIMER|
D
David Vrabel 已提交
337
				      IRQF_FORCE_RESUME|IRQF_EARLY_RESUME,
338
				      xevt->name, NULL);
339
	(void)xen_set_irq_priority(irq, XEN_IRQ_PRIORITY_MAX);
J
Jeremy Fitzhardinge 已提交
340 341 342

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

343
	evt->cpumask = cpumask_of(cpu);
J
Jeremy Fitzhardinge 已提交
344
	evt->irq = irq;
J
Jeremy Fitzhardinge 已提交
345 346
}

A
Alex Nixon 已提交
347

J
Jeremy Fitzhardinge 已提交
348 349
void xen_setup_cpu_clockevents(void)
{
350
	clockevents_register_device(this_cpu_ptr(&xen_clock_events.evt));
J
Jeremy Fitzhardinge 已提交
351 352
}

353 354 355 356
void xen_timer_resume(void)
{
	int cpu;

357 358
	pvclock_resume();

359 360 361 362
	if (xen_clockevent != &xen_vcpuop_clockevent)
		return;

	for_each_online_cpu(cpu) {
363 364
		if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer,
				       xen_vcpu_nr(cpu), NULL))
365 366 367 368
			BUG();
	}
}

369
static const struct pv_time_ops xen_time_ops __initconst = {
370
	.sched_clock = xen_clocksource_read,
371
	.steal_clock = xen_steal_clock,
372 373
};

374
static void __init xen_time_init(void)
J
Jeremy Fitzhardinge 已提交
375 376
{
	int cpu = smp_processor_id();
377
	struct timespec tp;
J
Jeremy Fitzhardinge 已提交
378

379 380 381 382
	/* As Dom0 is never moved, no penalty on using TSC there */
	if (xen_initial_domain())
		xen_clocksource.rating = 275;

383
	clocksource_register_hz(&xen_clocksource, NSEC_PER_SEC);
J
Jeremy Fitzhardinge 已提交
384

385 386
	if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
			       NULL) == 0) {
387
		/* Successfully turned off 100Hz tick, so we have the
J
Jeremy Fitzhardinge 已提交
388 389 390 391 392 393
		   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 */
394 395
	xen_read_wallclock(&tp);
	do_settimeofday(&tp);
J
Jeremy Fitzhardinge 已提交
396

397
	setup_force_cpu_cap(X86_FEATURE_TSC);
J
Jeremy Fitzhardinge 已提交
398

399
	xen_setup_runstate_info(cpu);
J
Jeremy Fitzhardinge 已提交
400
	xen_setup_timer(cpu);
J
Jeremy Fitzhardinge 已提交
401
	xen_setup_cpu_clockevents();
402

403 404
	xen_time_setup_guest();

405 406
	if (xen_initial_domain())
		pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier);
J
Jeremy Fitzhardinge 已提交
407
}
408

409
void __init xen_init_time_ops(void)
410 411 412 413 414 415 416 417 418
{
	pv_time_ops = xen_time_ops;

	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;
419 420 421
	/* Dom0 uses the native method to set the hardware RTC. */
	if (!xen_initial_domain())
		x86_platform.set_wallclock = xen_set_wallclock;
422 423
}

424
#ifdef CONFIG_XEN_PVHVM
425 426 427 428
static void xen_hvm_setup_cpu_clockevents(void)
{
	int cpu = smp_processor_id();
	xen_setup_runstate_info(cpu);
429 430 431 432 433
	/*
	 * 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).
	 */
434 435 436
	xen_setup_cpu_clockevents();
}

437
void __init xen_hvm_init_time_ops(void)
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
{
	if (!xen_feature(XENFEAT_hvm_safe_pvclock)) {
		printk(KERN_INFO "Xen doesn't support pvclock on HVM,"
				"disable pv timer\n");
		return;
	}

	pv_time_ops = xen_time_ops;
	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;
}
453
#endif