]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/xen/time.c
Merge tag 'for-linus-20170825' of git://git.infradead.org/linux-mtd
[mirror_ubuntu-artful-kernel.git] / arch / x86 / xen / time.c
CommitLineData
15c84731
JF
1/*
2 * Xen time implementation.
3 *
4 * This is implemented in terms of a clocksource driver which uses
5 * the hypervisor clock as a nanosecond timebase, and a clockevent
6 * driver which uses the hypervisor's timer mechanism.
7 *
8 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
9 */
10#include <linux/kernel.h>
11#include <linux/interrupt.h>
12#include <linux/clocksource.h>
13#include <linux/clockchips.h>
5a0e3ad6 14#include <linux/gfp.h>
c9d76a24 15#include <linux/slab.h>
5584880e 16#include <linux/pvclock_gtod.h>
76096863 17#include <linux/timekeeper_internal.h>
15c84731 18
1c7b67f7 19#include <asm/pvclock.h>
15c84731
JF
20#include <asm/xen/hypervisor.h>
21#include <asm/xen/hypercall.h>
22
23#include <xen/events.h>
409771d2 24#include <xen/features.h>
15c84731
JF
25#include <xen/interface/xen.h>
26#include <xen/interface/vcpu.h>
27
28#include "xen-ops.h"
29
15c84731
JF
30/* Xen may fire a timer up to this many ns early */
31#define TIMER_SLOP 100000
f91a8b44 32
e93ef949 33/* Get the TSC speed from Xen */
409771d2 34static unsigned long xen_tsc_khz(void)
15c84731 35{
3807f345 36 struct pvclock_vcpu_time_info *info =
15c84731
JF
37 &HYPERVISOR_shared_info->vcpu_info[0].time;
38
3807f345 39 return pvclock_tsc_khz(info);
15c84731
JF
40}
41
a5a1d1c2 42u64 xen_clocksource_read(void)
15c84731 43{
1c7b67f7 44 struct pvclock_vcpu_time_info *src;
a5a1d1c2 45 u64 ret;
15c84731 46
f1c39625 47 preempt_disable_notrace();
3251f20b 48 src = &__this_cpu_read(xen_vcpu)->time;
1c7b67f7 49 ret = pvclock_clocksource_read(src);
f1c39625 50 preempt_enable_notrace();
15c84731
JF
51 return ret;
52}
53
a5a1d1c2 54static u64 xen_clocksource_get_cycles(struct clocksource *cs)
8e19608e
MD
55{
56 return xen_clocksource_read();
57}
58
15c84731
JF
59static void xen_read_wallclock(struct timespec *ts)
60{
1c7b67f7
GH
61 struct shared_info *s = HYPERVISOR_shared_info;
62 struct pvclock_wall_clock *wall_clock = &(s->wc);
63 struct pvclock_vcpu_time_info *vcpu_time;
15c84731 64
1c7b67f7
GH
65 vcpu_time = &get_cpu_var(xen_vcpu)->time;
66 pvclock_read_wallclock(wall_clock, vcpu_time, ts);
67 put_cpu_var(xen_vcpu);
15c84731
JF
68}
69
3565184e 70static void xen_get_wallclock(struct timespec *now)
15c84731 71{
3565184e 72 xen_read_wallclock(now);
15c84731 73}
15c84731 74
3565184e 75static int xen_set_wallclock(const struct timespec *now)
15c84731 76{
47433b8c 77 return -1;
15c84731
JF
78}
79
47433b8c
DV
80static int xen_pvclock_gtod_notify(struct notifier_block *nb,
81 unsigned long was_set, void *priv)
15c84731 82{
47433b8c 83 /* Protected by the calling core code serialization */
187b26a9 84 static struct timespec64 next_sync;
5584880e 85
fdb9eb9f 86 struct xen_platform_op op;
76096863
SS
87 struct timespec64 now;
88 struct timekeeper *tk = priv;
89 static bool settime64_supported = true;
90 int ret;
fdb9eb9f 91
76096863
SS
92 now.tv_sec = tk->xtime_sec;
93 now.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
5584880e 94
47433b8c
DV
95 /*
96 * We only take the expensive HV call when the clock was set
97 * or when the 11 minutes RTC synchronization time elapsed.
98 */
187b26a9 99 if (!was_set && timespec64_compare(&now, &next_sync) < 0)
47433b8c 100 return NOTIFY_OK;
fdb9eb9f 101
76096863
SS
102again:
103 if (settime64_supported) {
104 op.cmd = XENPF_settime64;
105 op.u.settime64.mbz = 0;
106 op.u.settime64.secs = now.tv_sec;
107 op.u.settime64.nsecs = now.tv_nsec;
108 op.u.settime64.system_time = xen_clocksource_read();
109 } else {
110 op.cmd = XENPF_settime32;
111 op.u.settime32.secs = now.tv_sec;
112 op.u.settime32.nsecs = now.tv_nsec;
113 op.u.settime32.system_time = xen_clocksource_read();
114 }
115
116 ret = HYPERVISOR_platform_op(&op);
117
118 if (ret == -ENOSYS && settime64_supported) {
119 settime64_supported = false;
120 goto again;
121 }
122 if (ret < 0)
123 return NOTIFY_BAD;
fdb9eb9f 124
47433b8c
DV
125 /*
126 * Move the next drift compensation time 11 minutes
127 * ahead. That's emulating the sync_cmos_clock() update for
128 * the hardware RTC.
129 */
130 next_sync = now;
131 next_sync.tv_sec += 11 * 60;
132
5584880e 133 return NOTIFY_OK;
15c84731
JF
134}
135
5584880e
DV
136static struct notifier_block xen_pvclock_gtod_notifier = {
137 .notifier_call = xen_pvclock_gtod_notify,
138};
139
15c84731
JF
140static struct clocksource xen_clocksource __read_mostly = {
141 .name = "xen",
142 .rating = 400,
8e19608e 143 .read = xen_clocksource_get_cycles,
15c84731 144 .mask = ~0,
15c84731
JF
145 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
146};
147
148/*
149 Xen clockevent implementation
150
151 Xen has two clockevent implementations:
152
153 The old timer_op one works with all released versions of Xen prior
154 to version 3.0.4. This version of the hypervisor provides a
155 single-shot timer with nanosecond resolution. However, sharing the
156 same event channel is a 100Hz tick which is delivered while the
157 vcpu is running. We don't care about or use this tick, but it will
158 cause the core time code to think the timer fired too soon, and
159 will end up resetting it each time. It could be filtered, but
160 doing so has complications when the ktime clocksource is not yet
161 the xen clocksource (ie, at boot time).
162
163 The new vcpu_op-based timer interface allows the tick timer period
164 to be changed or turned off. The tick timer is not useful as a
165 periodic timer because events are only delivered to running vcpus.
166 The one-shot timer can report when a timeout is in the past, so
167 set_next_event is capable of returning -ETIME when appropriate.
168 This interface is used when available.
169*/
170
171
172/*
173 Get a hypervisor absolute time. In theory we could maintain an
174 offset between the kernel's time and the hypervisor's time, and
175 apply that to a kernel's absolute timeout. Unfortunately the
176 hypervisor and kernel times can drift even if the kernel is using
177 the Xen clocksource, because ntp can warp the kernel's clocksource.
178*/
179static s64 get_abs_timeout(unsigned long delta)
180{
181 return xen_clocksource_read() + delta;
182}
183
955381dd 184static int xen_timerop_shutdown(struct clock_event_device *evt)
15c84731 185{
955381dd
VK
186 /* cancel timeout */
187 HYPERVISOR_set_timer_op(0);
188
189 return 0;
15c84731
JF
190}
191
192static int xen_timerop_set_next_event(unsigned long delta,
193 struct clock_event_device *evt)
194{
955381dd 195 WARN_ON(!clockevent_state_oneshot(evt));
15c84731
JF
196
197 if (HYPERVISOR_set_timer_op(get_abs_timeout(delta)) < 0)
198 BUG();
199
200 /* We may have missed the deadline, but there's no real way of
201 knowing for sure. If the event was in the past, then we'll
202 get an immediate interrupt. */
203
204 return 0;
205}
206
207static const struct clock_event_device xen_timerop_clockevent = {
955381dd
VK
208 .name = "xen",
209 .features = CLOCK_EVT_FEAT_ONESHOT,
15c84731 210
955381dd 211 .max_delta_ns = 0xffffffff,
3d18d661 212 .max_delta_ticks = 0xffffffff,
955381dd 213 .min_delta_ns = TIMER_SLOP,
3d18d661 214 .min_delta_ticks = TIMER_SLOP,
15c84731 215
955381dd
VK
216 .mult = 1,
217 .shift = 0,
218 .rating = 500,
15c84731 219
955381dd
VK
220 .set_state_shutdown = xen_timerop_shutdown,
221 .set_next_event = xen_timerop_set_next_event,
15c84731
JF
222};
223
955381dd
VK
224static int xen_vcpuop_shutdown(struct clock_event_device *evt)
225{
226 int cpu = smp_processor_id();
15c84731 227
ad5475f9
VK
228 if (HYPERVISOR_vcpu_op(VCPUOP_stop_singleshot_timer, xen_vcpu_nr(cpu),
229 NULL) ||
230 HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
231 NULL))
955381dd 232 BUG();
15c84731 233
955381dd
VK
234 return 0;
235}
236
237static int xen_vcpuop_set_oneshot(struct clock_event_device *evt)
15c84731
JF
238{
239 int cpu = smp_processor_id();
240
ad5475f9
VK
241 if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
242 NULL))
955381dd
VK
243 BUG();
244
245 return 0;
15c84731
JF
246}
247
248static int xen_vcpuop_set_next_event(unsigned long delta,
249 struct clock_event_device *evt)
250{
251 int cpu = smp_processor_id();
252 struct vcpu_set_singleshot_timer single;
253 int ret;
254
955381dd 255 WARN_ON(!clockevent_state_oneshot(evt));
15c84731
JF
256
257 single.timeout_abs_ns = get_abs_timeout(delta);
c06b6d70
SS
258 /* Get an event anyway, even if the timeout is already expired */
259 single.flags = 0;
15c84731 260
ad5475f9
VK
261 ret = HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, xen_vcpu_nr(cpu),
262 &single);
c06b6d70 263 BUG_ON(ret != 0);
15c84731
JF
264
265 return ret;
266}
267
268static const struct clock_event_device xen_vcpuop_clockevent = {
269 .name = "xen",
270 .features = CLOCK_EVT_FEAT_ONESHOT,
271
272 .max_delta_ns = 0xffffffff,
3d18d661 273 .max_delta_ticks = 0xffffffff,
15c84731 274 .min_delta_ns = TIMER_SLOP,
3d18d661 275 .min_delta_ticks = TIMER_SLOP,
15c84731
JF
276
277 .mult = 1,
278 .shift = 0,
279 .rating = 500,
280
955381dd
VK
281 .set_state_shutdown = xen_vcpuop_shutdown,
282 .set_state_oneshot = xen_vcpuop_set_oneshot,
15c84731
JF
283 .set_next_event = xen_vcpuop_set_next_event,
284};
285
286static const struct clock_event_device *xen_clockevent =
287 &xen_timerop_clockevent;
31620a19
KRW
288
289struct xen_clock_event_device {
290 struct clock_event_device evt;
7be0772d 291 char name[16];
31620a19
KRW
292};
293static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 };
15c84731
JF
294
295static irqreturn_t xen_timer_interrupt(int irq, void *dev_id)
296{
89cbc767 297 struct clock_event_device *evt = this_cpu_ptr(&xen_clock_events.evt);
15c84731
JF
298 irqreturn_t ret;
299
300 ret = IRQ_NONE;
301 if (evt->event_handler) {
302 evt->event_handler(evt);
303 ret = IRQ_HANDLED;
304 }
305
306 return ret;
307}
308
09e99da7
KRW
309void xen_teardown_timer(int cpu)
310{
311 struct clock_event_device *evt;
09e99da7
KRW
312 evt = &per_cpu(xen_clock_events, cpu).evt;
313
314 if (evt->irq >= 0) {
315 unbind_from_irqhandler(evt->irq, NULL);
316 evt->irq = -1;
09e99da7
KRW
317 }
318}
319
f87e4cac 320void xen_setup_timer(int cpu)
15c84731 321{
7be0772d
VK
322 struct xen_clock_event_device *xevt = &per_cpu(xen_clock_events, cpu);
323 struct clock_event_device *evt = &xevt->evt;
15c84731
JF
324 int irq;
325
ef35a4e6 326 WARN(evt->irq >= 0, "IRQ%d for CPU%d is already allocated\n", evt->irq, cpu);
09e99da7
KRW
327 if (evt->irq >= 0)
328 xen_teardown_timer(cpu);
ef35a4e6 329
15c84731
JF
330 printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu);
331
7be0772d 332 snprintf(xevt->name, sizeof(xevt->name), "timer%d", cpu);
15c84731
JF
333
334 irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt,
9d71cee6 335 IRQF_PERCPU|IRQF_NOBALANCING|IRQF_TIMER|
8d5999df 336 IRQF_FORCE_RESUME|IRQF_EARLY_RESUME,
7be0772d 337 xevt->name, NULL);
8785c676 338 (void)xen_set_irq_priority(irq, XEN_IRQ_PRIORITY_MAX);
15c84731 339
15c84731
JF
340 memcpy(evt, xen_clockevent, sizeof(*evt));
341
320ab2b0 342 evt->cpumask = cpumask_of(cpu);
15c84731 343 evt->irq = irq;
f87e4cac
JF
344}
345
d68d82af 346
f87e4cac
JF
347void xen_setup_cpu_clockevents(void)
348{
89cbc767 349 clockevents_register_device(this_cpu_ptr(&xen_clock_events.evt));
15c84731
JF
350}
351
d07af1f0
JF
352void xen_timer_resume(void)
353{
354 int cpu;
355
e7a3481c
JF
356 pvclock_resume();
357
d07af1f0
JF
358 if (xen_clockevent != &xen_vcpuop_clockevent)
359 return;
360
361 for_each_online_cpu(cpu) {
ad5475f9
VK
362 if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer,
363 xen_vcpu_nr(cpu), NULL))
d07af1f0
JF
364 BUG();
365 }
366}
367
fb6ce5de 368static const struct pv_time_ops xen_time_ops __initconst = {
ca50a5f3 369 .sched_clock = xen_clocksource_read,
d34c30cc 370 .steal_clock = xen_steal_clock,
409771d2
SS
371};
372
fb6ce5de 373static void __init xen_time_init(void)
15c84731
JF
374{
375 int cpu = smp_processor_id();
c4507257 376 struct timespec tp;
15c84731 377
94dd85f6
PI
378 /* As Dom0 is never moved, no penalty on using TSC there */
379 if (xen_initial_domain())
380 xen_clocksource.rating = 275;
381
b01cc1b0 382 clocksource_register_hz(&xen_clocksource, NSEC_PER_SEC);
15c84731 383
ad5475f9
VK
384 if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
385 NULL) == 0) {
f91a8b44 386 /* Successfully turned off 100Hz tick, so we have the
15c84731
JF
387 vcpuop-based timer interface */
388 printk(KERN_DEBUG "Xen: using vcpuop timer interface\n");
389 xen_clockevent = &xen_vcpuop_clockevent;
390 }
391
392 /* Set initial system time with full resolution */
c4507257
JS
393 xen_read_wallclock(&tp);
394 do_settimeofday(&tp);
15c84731 395
404ee5b1 396 setup_force_cpu_cap(X86_FEATURE_TSC);
15c84731 397
be012920 398 xen_setup_runstate_info(cpu);
15c84731 399 xen_setup_timer(cpu);
f87e4cac 400 xen_setup_cpu_clockevents();
5584880e 401
ecb23dc6
JG
402 xen_time_setup_guest();
403
5584880e
DV
404 if (xen_initial_domain())
405 pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier);
15c84731 406}
409771d2 407
d162809f 408void __ref xen_init_time_ops(void)
409771d2
SS
409{
410 pv_time_ops = xen_time_ops;
411
412 x86_init.timers.timer_init = xen_time_init;
413 x86_init.timers.setup_percpu_clockev = x86_init_noop;
414 x86_cpuinit.setup_percpu_clockev = x86_init_noop;
415
416 x86_platform.calibrate_tsc = xen_tsc_khz;
417 x86_platform.get_wallclock = xen_get_wallclock;
47433b8c
DV
418 /* Dom0 uses the native method to set the hardware RTC. */
419 if (!xen_initial_domain())
420 x86_platform.set_wallclock = xen_set_wallclock;
409771d2
SS
421}
422
ca65f9fc 423#ifdef CONFIG_XEN_PVHVM
409771d2
SS
424static void xen_hvm_setup_cpu_clockevents(void)
425{
426 int cpu = smp_processor_id();
427 xen_setup_runstate_info(cpu);
7918c92a
KRW
428 /*
429 * xen_setup_timer(cpu) - snprintf is bad in atomic context. Hence
430 * doing it xen_hvm_cpu_notify (which gets called by smp_init during
431 * early bootup and also during CPU hotplug events).
432 */
409771d2
SS
433 xen_setup_cpu_clockevents();
434}
435
fb6ce5de 436void __init xen_hvm_init_time_ops(void)
409771d2 437{
84d582d2
BO
438 /*
439 * vector callback is needed otherwise we cannot receive interrupts
440 * on cpu > 0 and at this point we don't know how many cpus are
441 * available.
442 */
443 if (!xen_have_vector_callback)
444 return;
445
409771d2
SS
446 if (!xen_feature(XENFEAT_hvm_safe_pvclock)) {
447 printk(KERN_INFO "Xen doesn't support pvclock on HVM,"
448 "disable pv timer\n");
449 return;
450 }
451
452 pv_time_ops = xen_time_ops;
453 x86_init.timers.setup_percpu_clockev = xen_time_init;
454 x86_cpuinit.setup_percpu_clockev = xen_hvm_setup_cpu_clockevents;
455
456 x86_platform.calibrate_tsc = xen_tsc_khz;
457 x86_platform.get_wallclock = xen_get_wallclock;
458 x86_platform.set_wallclock = xen_set_wallclock;
459}
ca65f9fc 460#endif