blob: e0f1bcf01d639a6e845c1f25eb6ac85ccfac66c0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -07002/*
3 * Xen time implementation.
4 *
5 * This is implemented in terms of a clocksource driver which uses
6 * the hypervisor clock as a nanosecond timebase, and a clockevent
7 * driver which uses the hypervisor's timer mechanism.
8 *
9 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
10 */
11#include <linux/kernel.h>
12#include <linux/interrupt.h>
13#include <linux/clocksource.h>
14#include <linux/clockchips.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Konrad Rzeszutek Wilkc9d76a22013-06-04 17:09:36 -040016#include <linux/slab.h>
David Vrabel55848802013-06-27 11:35:47 +010017#include <linux/pvclock_gtod.h>
Stefano Stabellini76096862015-11-23 10:42:12 +000018#include <linux/timekeeper_internal.h>
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070019
Gerd Hoffmann1c7b67f2008-06-03 16:17:30 +020020#include <asm/pvclock.h>
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070021#include <asm/xen/hypervisor.h>
22#include <asm/xen/hypercall.h>
23
24#include <xen/events.h>
Stefano Stabellini409771d2010-05-14 12:48:19 +010025#include <xen/features.h>
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070026#include <xen/interface/xen.h>
27#include <xen/interface/vcpu.h>
28
29#include "xen-ops.h"
30
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070031/* Xen may fire a timer up to this many ns early */
32#define TIMER_SLOP 100000
Jeremy Fitzhardingef91a8b42007-07-17 18:37:05 -070033
Alok Katariae93ef942008-07-01 11:43:36 -070034/* Get the TSC speed from Xen */
Stefano Stabellini409771d2010-05-14 12:48:19 +010035static unsigned long xen_tsc_khz(void)
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070036{
Glauber Costa3807f342008-07-28 11:47:52 -030037 struct pvclock_vcpu_time_info *info =
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070038 &HYPERVISOR_shared_info->vcpu_info[0].time;
39
Glauber Costa3807f342008-07-28 11:47:52 -030040 return pvclock_tsc_khz(info);
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070041}
42
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010043u64 xen_clocksource_read(void)
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070044{
Gerd Hoffmann1c7b67f2008-06-03 16:17:30 +020045 struct pvclock_vcpu_time_info *src;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010046 u64 ret;
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070047
Jeremy Fitzhardingef1c39622011-08-24 09:54:24 -070048 preempt_disable_notrace();
Boris Ostrovsky3251f202014-10-16 17:02:15 -040049 src = &__this_cpu_read(xen_vcpu)->time;
Gerd Hoffmann1c7b67f2008-06-03 16:17:30 +020050 ret = pvclock_clocksource_read(src);
Jeremy Fitzhardingef1c39622011-08-24 09:54:24 -070051 preempt_enable_notrace();
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070052 return ret;
53}
54
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010055static u64 xen_clocksource_get_cycles(struct clocksource *cs)
Magnus Damm8e196082009-04-21 12:24:00 -070056{
57 return xen_clocksource_read();
58}
59
Arnd Bergmanne27c4922018-04-27 22:13:23 +020060static void xen_read_wallclock(struct timespec64 *ts)
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070061{
Gerd Hoffmann1c7b67f2008-06-03 16:17:30 +020062 struct shared_info *s = HYPERVISOR_shared_info;
63 struct pvclock_wall_clock *wall_clock = &(s->wc);
64 struct pvclock_vcpu_time_info *vcpu_time;
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070065
Gerd Hoffmann1c7b67f2008-06-03 16:17:30 +020066 vcpu_time = &get_cpu_var(xen_vcpu)->time;
67 pvclock_read_wallclock(wall_clock, vcpu_time, ts);
68 put_cpu_var(xen_vcpu);
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070069}
70
Arnd Bergmanne27c4922018-04-27 22:13:23 +020071static void xen_get_wallclock(struct timespec64 *now)
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070072{
David Vrabel35651842013-05-13 18:56:06 +010073 xen_read_wallclock(now);
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070074}
75
Arnd Bergmanne27c4922018-04-27 22:13:23 +020076static int xen_set_wallclock(const struct timespec64 *now)
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070077{
Boris Ostrovskyb5494ad2017-11-02 18:18:03 -040078 return -ENODEV;
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070079}
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -070080
David Vrabel47433b82013-06-27 11:35:48 +010081static int xen_pvclock_gtod_notify(struct notifier_block *nb,
82 unsigned long was_set, void *priv)
David Vrabel55848802013-06-27 11:35:47 +010083{
David Vrabel47433b82013-06-27 11:35:48 +010084 /* Protected by the calling core code serialization */
Stefano Stabellini187b26a2015-11-24 14:53:02 +000085 static struct timespec64 next_sync;
David Vrabel55848802013-06-27 11:35:47 +010086
David Vrabel47433b82013-06-27 11:35:48 +010087 struct xen_platform_op op;
Stefano Stabellini76096862015-11-23 10:42:12 +000088 struct timespec64 now;
89 struct timekeeper *tk = priv;
90 static bool settime64_supported = true;
91 int ret;
David Vrabel55848802013-06-27 11:35:47 +010092
Stefano Stabellini76096862015-11-23 10:42:12 +000093 now.tv_sec = tk->xtime_sec;
94 now.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
David Vrabel55848802013-06-27 11:35:47 +010095
David Vrabel47433b82013-06-27 11:35:48 +010096 /*
97 * We only take the expensive HV call when the clock was set
98 * or when the 11 minutes RTC synchronization time elapsed.
99 */
Stefano Stabellini187b26a2015-11-24 14:53:02 +0000100 if (!was_set && timespec64_compare(&now, &next_sync) < 0)
David Vrabel47433b82013-06-27 11:35:48 +0100101 return NOTIFY_OK;
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700102
Stefano Stabellini76096862015-11-23 10:42:12 +0000103again:
104 if (settime64_supported) {
105 op.cmd = XENPF_settime64;
106 op.u.settime64.mbz = 0;
107 op.u.settime64.secs = now.tv_sec;
108 op.u.settime64.nsecs = now.tv_nsec;
109 op.u.settime64.system_time = xen_clocksource_read();
110 } else {
111 op.cmd = XENPF_settime32;
112 op.u.settime32.secs = now.tv_sec;
113 op.u.settime32.nsecs = now.tv_nsec;
114 op.u.settime32.system_time = xen_clocksource_read();
115 }
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700116
Stefano Stabellini76096862015-11-23 10:42:12 +0000117 ret = HYPERVISOR_platform_op(&op);
118
119 if (ret == -ENOSYS && settime64_supported) {
120 settime64_supported = false;
121 goto again;
122 }
123 if (ret < 0)
124 return NOTIFY_BAD;
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700125
David Vrabel47433b82013-06-27 11:35:48 +0100126 /*
127 * Move the next drift compensation time 11 minutes
128 * ahead. That's emulating the sync_cmos_clock() update for
129 * the hardware RTC.
130 */
131 next_sync = now;
132 next_sync.tv_sec += 11 * 60;
133
David Vrabel55848802013-06-27 11:35:47 +0100134 return NOTIFY_OK;
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700135}
136
David Vrabel55848802013-06-27 11:35:47 +0100137static struct notifier_block xen_pvclock_gtod_notifier = {
138 .notifier_call = xen_pvclock_gtod_notify,
139};
140
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700141static struct clocksource xen_clocksource __read_mostly = {
142 .name = "xen",
143 .rating = 400,
144 .read = xen_clocksource_get_cycles,
145 .mask = ~0,
146 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
147};
148
149/*
150 Xen clockevent implementation
151
152 Xen has two clockevent implementations:
153
154 The old timer_op one works with all released versions of Xen prior
155 to version 3.0.4. This version of the hypervisor provides a
156 single-shot timer with nanosecond resolution. However, sharing the
157 same event channel is a 100Hz tick which is delivered while the
158 vcpu is running. We don't care about or use this tick, but it will
159 cause the core time code to think the timer fired too soon, and
160 will end up resetting it each time. It could be filtered, but
161 doing so has complications when the ktime clocksource is not yet
162 the xen clocksource (ie, at boot time).
163
164 The new vcpu_op-based timer interface allows the tick timer period
165 to be changed or turned off. The tick timer is not useful as a
166 periodic timer because events are only delivered to running vcpus.
167 The one-shot timer can report when a timeout is in the past, so
168 set_next_event is capable of returning -ETIME when appropriate.
169 This interface is used when available.
170*/
171
172
173/*
174 Get a hypervisor absolute time. In theory we could maintain an
175 offset between the kernel's time and the hypervisor's time, and
176 apply that to a kernel's absolute timeout. Unfortunately the
177 hypervisor and kernel times can drift even if the kernel is using
178 the Xen clocksource, because ntp can warp the kernel's clocksource.
179*/
180static s64 get_abs_timeout(unsigned long delta)
181{
182 return xen_clocksource_read() + delta;
183}
184
Viresh Kumar955381d2015-07-16 16:28:48 +0530185static int xen_timerop_shutdown(struct clock_event_device *evt)
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700186{
Viresh Kumar955381d2015-07-16 16:28:48 +0530187 /* cancel timeout */
188 HYPERVISOR_set_timer_op(0);
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700189
Viresh Kumar955381d2015-07-16 16:28:48 +0530190 return 0;
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700191}
192
193static int xen_timerop_set_next_event(unsigned long delta,
194 struct clock_event_device *evt)
195{
Viresh Kumar955381d2015-07-16 16:28:48 +0530196 WARN_ON(!clockevent_state_oneshot(evt));
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700197
198 if (HYPERVISOR_set_timer_op(get_abs_timeout(delta)) < 0)
199 BUG();
200
201 /* We may have missed the deadline, but there's no real way of
202 knowing for sure. If the event was in the past, then we'll
203 get an immediate interrupt. */
204
205 return 0;
206}
207
208static const struct clock_event_device xen_timerop_clockevent = {
Viresh Kumar955381d2015-07-16 16:28:48 +0530209 .name = "xen",
210 .features = CLOCK_EVT_FEAT_ONESHOT,
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700211
Viresh Kumar955381d2015-07-16 16:28:48 +0530212 .max_delta_ns = 0xffffffff,
Nicolai Stange3d18d662017-03-30 22:06:42 +0200213 .max_delta_ticks = 0xffffffff,
Viresh Kumar955381d2015-07-16 16:28:48 +0530214 .min_delta_ns = TIMER_SLOP,
Nicolai Stange3d18d662017-03-30 22:06:42 +0200215 .min_delta_ticks = TIMER_SLOP,
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700216
Viresh Kumar955381d2015-07-16 16:28:48 +0530217 .mult = 1,
218 .shift = 0,
219 .rating = 500,
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700220
Viresh Kumar955381d2015-07-16 16:28:48 +0530221 .set_state_shutdown = xen_timerop_shutdown,
222 .set_next_event = xen_timerop_set_next_event,
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700223};
224
Viresh Kumar955381d2015-07-16 16:28:48 +0530225static int xen_vcpuop_shutdown(struct clock_event_device *evt)
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700226{
227 int cpu = smp_processor_id();
228
Vitaly Kuznetsovad5475f2016-06-30 17:56:38 +0200229 if (HYPERVISOR_vcpu_op(VCPUOP_stop_singleshot_timer, xen_vcpu_nr(cpu),
230 NULL) ||
231 HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
232 NULL))
Viresh Kumar955381d2015-07-16 16:28:48 +0530233 BUG();
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700234
Viresh Kumar955381d2015-07-16 16:28:48 +0530235 return 0;
236}
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700237
Viresh Kumar955381d2015-07-16 16:28:48 +0530238static int xen_vcpuop_set_oneshot(struct clock_event_device *evt)
239{
240 int cpu = smp_processor_id();
241
Vitaly Kuznetsovad5475f2016-06-30 17:56:38 +0200242 if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
243 NULL))
Viresh Kumar955381d2015-07-16 16:28:48 +0530244 BUG();
245
246 return 0;
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700247}
248
249static int xen_vcpuop_set_next_event(unsigned long delta,
250 struct clock_event_device *evt)
251{
252 int cpu = smp_processor_id();
253 struct vcpu_set_singleshot_timer single;
254 int ret;
255
Viresh Kumar955381d2015-07-16 16:28:48 +0530256 WARN_ON(!clockevent_state_oneshot(evt));
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700257
258 single.timeout_abs_ns = get_abs_timeout(delta);
Stefano Stabellinic06b6d72016-04-15 18:23:00 -0700259 /* Get an event anyway, even if the timeout is already expired */
260 single.flags = 0;
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700261
Vitaly Kuznetsovad5475f2016-06-30 17:56:38 +0200262 ret = HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, xen_vcpu_nr(cpu),
263 &single);
Stefano Stabellinic06b6d72016-04-15 18:23:00 -0700264 BUG_ON(ret != 0);
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700265
266 return ret;
267}
268
269static const struct clock_event_device xen_vcpuop_clockevent = {
270 .name = "xen",
271 .features = CLOCK_EVT_FEAT_ONESHOT,
272
273 .max_delta_ns = 0xffffffff,
Nicolai Stange3d18d662017-03-30 22:06:42 +0200274 .max_delta_ticks = 0xffffffff,
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700275 .min_delta_ns = TIMER_SLOP,
Nicolai Stange3d18d662017-03-30 22:06:42 +0200276 .min_delta_ticks = TIMER_SLOP,
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700277
278 .mult = 1,
279 .shift = 0,
280 .rating = 500,
281
Viresh Kumar955381d2015-07-16 16:28:48 +0530282 .set_state_shutdown = xen_vcpuop_shutdown,
283 .set_state_oneshot = xen_vcpuop_set_oneshot,
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700284 .set_next_event = xen_vcpuop_set_next_event,
285};
286
287static const struct clock_event_device *xen_clockevent =
288 &xen_timerop_clockevent;
Konrad Rzeszutek Wilk31620a12013-06-04 17:06:36 -0400289
290struct xen_clock_event_device {
291 struct clock_event_device evt;
Vitaly Kuznetsov7be07722015-01-05 16:27:51 +0100292 char name[16];
Konrad Rzeszutek Wilk31620a12013-06-04 17:06:36 -0400293};
294static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 };
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700295
296static irqreturn_t xen_timer_interrupt(int irq, void *dev_id)
297{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500298 struct clock_event_device *evt = this_cpu_ptr(&xen_clock_events.evt);
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700299 irqreturn_t ret;
300
301 ret = IRQ_NONE;
302 if (evt->event_handler) {
303 evt->event_handler(evt);
304 ret = IRQ_HANDLED;
305 }
306
307 return ret;
308}
309
Konrad Rzeszutek Wilk09e99da2013-06-04 17:13:29 -0400310void xen_teardown_timer(int cpu)
311{
312 struct clock_event_device *evt;
Konrad Rzeszutek Wilk09e99da2013-06-04 17:13:29 -0400313 evt = &per_cpu(xen_clock_events, cpu).evt;
314
315 if (evt->irq >= 0) {
316 unbind_from_irqhandler(evt->irq, NULL);
317 evt->irq = -1;
Konrad Rzeszutek Wilk09e99da2013-06-04 17:13:29 -0400318 }
319}
320
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700321void xen_setup_timer(int cpu)
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700322{
Vitaly Kuznetsov7be07722015-01-05 16:27:51 +0100323 struct xen_clock_event_device *xevt = &per_cpu(xen_clock_events, cpu);
324 struct clock_event_device *evt = &xevt->evt;
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700325 int irq;
326
Konrad Rzeszutek Wilkef35a4e2013-04-08 21:05:15 -0400327 WARN(evt->irq >= 0, "IRQ%d for CPU%d is already allocated\n", evt->irq, cpu);
Konrad Rzeszutek Wilk09e99da2013-06-04 17:13:29 -0400328 if (evt->irq >= 0)
329 xen_teardown_timer(cpu);
Konrad Rzeszutek Wilkef35a4e2013-04-08 21:05:15 -0400330
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700331 printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu);
332
Vitaly Kuznetsov7be07722015-01-05 16:27:51 +0100333 snprintf(xevt->name, sizeof(xevt->name), "timer%d", cpu);
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700334
335 irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt,
Michael Opdenacker9d71cee2013-09-07 08:46:49 +0200336 IRQF_PERCPU|IRQF_NOBALANCING|IRQF_TIMER|
David Vrabel8d5999d2014-08-07 17:06:06 +0100337 IRQF_FORCE_RESUME|IRQF_EARLY_RESUME,
Vitaly Kuznetsov7be07722015-01-05 16:27:51 +0100338 xevt->name, NULL);
David Vrabel8785c672013-09-23 12:52:21 +0100339 (void)xen_set_irq_priority(irq, XEN_IRQ_PRIORITY_MAX);
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700340
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700341 memcpy(evt, xen_clockevent, sizeof(*evt));
342
Rusty Russell320ab2b2008-12-13 21:20:26 +1030343 evt->cpumask = cpumask_of(cpu);
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700344 evt->irq = irq;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700345}
Jeremy Fitzhardingef91a8b42007-07-17 18:37:05 -0700346
Alex Nixond68d82a2008-08-22 11:52:15 +0100347
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700348void xen_setup_cpu_clockevents(void)
349{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500350 clockevents_register_device(this_cpu_ptr(&xen_clock_events.evt));
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700351}
352
Jeremy Fitzhardinged07af1f2008-05-31 01:33:03 +0100353void xen_timer_resume(void)
354{
355 int cpu;
356
Jeremy Fitzhardingee7a3481c2010-10-25 16:53:46 -0700357 pvclock_resume();
358
Jeremy Fitzhardinged07af1f2008-05-31 01:33:03 +0100359 if (xen_clockevent != &xen_vcpuop_clockevent)
360 return;
361
362 for_each_online_cpu(cpu) {
Vitaly Kuznetsovad5475f2016-06-30 17:56:38 +0200363 if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer,
364 xen_vcpu_nr(cpu), NULL))
Jeremy Fitzhardinged07af1f2008-05-31 01:33:03 +0100365 BUG();
366 }
367}
368
Daniel Kiperfb6ce5d2011-05-04 20:18:45 +0200369static const struct pv_time_ops xen_time_ops __initconst = {
Jeremy Fitzhardingeca50a5f2010-08-04 14:49:16 -0700370 .sched_clock = xen_clocksource_read,
Juergen Grossd34c30c2016-07-26 14:15:11 +0200371 .steal_clock = xen_steal_clock,
Stefano Stabellini409771d2010-05-14 12:48:19 +0100372};
373
Joao Martins2229f702017-11-08 17:19:57 +0000374static struct pvclock_vsyscall_time_info *xen_clock __read_mostly;
375
376void xen_save_time_memory_area(void)
377{
378 struct vcpu_register_time_memory_area t;
379 int ret;
380
381 if (!xen_clock)
382 return;
383
384 t.addr.v = NULL;
385
386 ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area, 0, &t);
387 if (ret != 0)
388 pr_notice("Cannot save secondary vcpu_time_info (err %d)",
389 ret);
390 else
391 clear_page(xen_clock);
392}
393
394void xen_restore_time_memory_area(void)
395{
396 struct vcpu_register_time_memory_area t;
397 int ret;
398
399 if (!xen_clock)
400 return;
401
402 t.addr.v = &xen_clock->pvti;
403
404 ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area, 0, &t);
405
406 /*
407 * We don't disable VCLOCK_PVCLOCK entirely if it fails to register the
408 * secondary time info with Xen or if we migrated to a host without the
409 * necessary flags. On both of these cases what happens is either
410 * process seeing a zeroed out pvti or seeing no PVCLOCK_TSC_STABLE_BIT
411 * bit set. Userspace checks the latter and if 0, it discards the data
412 * in pvti and fallbacks to a system call for a reliable timestamp.
413 */
414 if (ret != 0)
415 pr_notice("Cannot restore secondary vcpu_time_info (err %d)",
416 ret);
417}
418
419static void xen_setup_vsyscall_time_info(void)
420{
421 struct vcpu_register_time_memory_area t;
422 struct pvclock_vsyscall_time_info *ti;
423 int ret;
424
425 ti = (struct pvclock_vsyscall_time_info *)get_zeroed_page(GFP_KERNEL);
426 if (!ti)
427 return;
428
429 t.addr.v = &ti->pvti;
430
431 ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area, 0, &t);
432 if (ret) {
433 pr_notice("xen: VCLOCK_PVCLOCK not supported (err %d)\n", ret);
434 free_page((unsigned long)ti);
435 return;
436 }
437
438 /*
439 * If primary time info had this bit set, secondary should too since
440 * it's the same data on both just different memory regions. But we
441 * still check it in case hypervisor is buggy.
442 */
443 if (!(ti->pvti.flags & PVCLOCK_TSC_STABLE_BIT)) {
444 t.addr.v = NULL;
445 ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area,
446 0, &t);
447 if (!ret)
448 free_page((unsigned long)ti);
449
450 pr_notice("xen: VCLOCK_PVCLOCK not supported (tsc unstable)\n");
451 return;
452 }
453
454 xen_clock = ti;
455 pvclock_set_pvti_cpu0_va(xen_clock);
456
457 xen_clocksource.archdata.vclock_mode = VCLOCK_PVCLOCK;
458}
459
Daniel Kiperfb6ce5d2011-05-04 20:18:45 +0200460static void __init xen_time_init(void)
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700461{
Joao Martinsb8888082017-11-08 17:19:56 +0000462 struct pvclock_vcpu_time_info *pvti;
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700463 int cpu = smp_processor_id();
Arnd Bergmanne27c4922018-04-27 22:13:23 +0200464 struct timespec64 tp;
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700465
Palik, Imre94dd85f2015-01-13 09:14:22 +0100466 /* As Dom0 is never moved, no penalty on using TSC there */
467 if (xen_initial_domain())
468 xen_clocksource.rating = 275;
469
John Stultzb01cc1b2010-04-26 19:03:05 -0700470 clocksource_register_hz(&xen_clocksource, NSEC_PER_SEC);
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700471
Vitaly Kuznetsovad5475f2016-06-30 17:56:38 +0200472 if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
473 NULL) == 0) {
Jeremy Fitzhardingef91a8b42007-07-17 18:37:05 -0700474 /* Successfully turned off 100Hz tick, so we have the
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700475 vcpuop-based timer interface */
476 printk(KERN_DEBUG "Xen: using vcpuop timer interface\n");
477 xen_clockevent = &xen_vcpuop_clockevent;
478 }
479
480 /* Set initial system time with full resolution */
John Stultzc4507252010-03-11 14:04:47 -0800481 xen_read_wallclock(&tp);
Arnd Bergmanne27c4922018-04-27 22:13:23 +0200482 do_settimeofday64(&tp);
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700483
Andi Kleen404ee5b2008-01-30 13:33:20 +0100484 setup_force_cpu_cap(X86_FEATURE_TSC);
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700485
Joao Martinsb8888082017-11-08 17:19:56 +0000486 /*
487 * We check ahead on the primary time info if this
488 * bit is supported hence speeding up Xen clocksource.
489 */
490 pvti = &__this_cpu_read(xen_vcpu)->time;
Joao Martins2229f702017-11-08 17:19:57 +0000491 if (pvti->flags & PVCLOCK_TSC_STABLE_BIT) {
Joao Martinsb8888082017-11-08 17:19:56 +0000492 pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
Joao Martins2229f702017-11-08 17:19:57 +0000493 xen_setup_vsyscall_time_info();
494 }
Joao Martinsb8888082017-11-08 17:19:56 +0000495
Ian Campbellbe0129202009-11-21 08:35:55 +0800496 xen_setup_runstate_info(cpu);
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700497 xen_setup_timer(cpu);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700498 xen_setup_cpu_clockevents();
David Vrabel55848802013-06-27 11:35:47 +0100499
Juergen Grossecb23dc2016-05-20 09:26:48 +0200500 xen_time_setup_guest();
501
David Vrabel55848802013-06-27 11:35:47 +0100502 if (xen_initial_domain())
503 pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier);
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700504}
Stefano Stabellini409771d2010-05-14 12:48:19 +0100505
Boris Ostrovskyd1628092017-05-03 16:20:51 -0400506void __ref xen_init_time_ops(void)
Stefano Stabellini409771d2010-05-14 12:48:19 +0100507{
508 pv_time_ops = xen_time_ops;
509
510 x86_init.timers.timer_init = xen_time_init;
511 x86_init.timers.setup_percpu_clockev = x86_init_noop;
512 x86_cpuinit.setup_percpu_clockev = x86_init_noop;
513
514 x86_platform.calibrate_tsc = xen_tsc_khz;
515 x86_platform.get_wallclock = xen_get_wallclock;
David Vrabel47433b82013-06-27 11:35:48 +0100516 /* Dom0 uses the native method to set the hardware RTC. */
517 if (!xen_initial_domain())
518 x86_platform.set_wallclock = xen_set_wallclock;
Stefano Stabellini409771d2010-05-14 12:48:19 +0100519}
520
Stefano Stabellinica65f9f2010-07-29 14:37:48 +0100521#ifdef CONFIG_XEN_PVHVM
Stefano Stabellini409771d2010-05-14 12:48:19 +0100522static void xen_hvm_setup_cpu_clockevents(void)
523{
524 int cpu = smp_processor_id();
525 xen_setup_runstate_info(cpu);
Konrad Rzeszutek Wilk7918c922013-04-16 15:18:00 -0400526 /*
527 * xen_setup_timer(cpu) - snprintf is bad in atomic context. Hence
528 * doing it xen_hvm_cpu_notify (which gets called by smp_init during
529 * early bootup and also during CPU hotplug events).
530 */
Stefano Stabellini409771d2010-05-14 12:48:19 +0100531 xen_setup_cpu_clockevents();
532}
533
Daniel Kiperfb6ce5d2011-05-04 20:18:45 +0200534void __init xen_hvm_init_time_ops(void)
Stefano Stabellini409771d2010-05-14 12:48:19 +0100535{
Boris Ostrovsky84d582d2017-04-24 15:04:53 -0400536 /*
537 * vector callback is needed otherwise we cannot receive interrupts
538 * on cpu > 0 and at this point we don't know how many cpus are
539 * available.
540 */
541 if (!xen_have_vector_callback)
542 return;
543
Stefano Stabellini409771d2010-05-14 12:48:19 +0100544 if (!xen_feature(XENFEAT_hvm_safe_pvclock)) {
545 printk(KERN_INFO "Xen doesn't support pvclock on HVM,"
546 "disable pv timer\n");
547 return;
548 }
549
550 pv_time_ops = xen_time_ops;
551 x86_init.timers.setup_percpu_clockev = xen_time_init;
552 x86_cpuinit.setup_percpu_clockev = xen_hvm_setup_cpu_clockevents;
553
554 x86_platform.calibrate_tsc = xen_tsc_khz;
555 x86_platform.get_wallclock = xen_get_wallclock;
556 x86_platform.set_wallclock = xen_set_wallclock;
557}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +0100558#endif