Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 2 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/gfp.h> |
Konrad Rzeszutek Wilk | c9d76a2 | 2013-06-04 17:09:36 -0400 | [diff] [blame] | 16 | #include <linux/slab.h> |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 17 | #include <linux/pvclock_gtod.h> |
Stefano Stabellini | 7609686 | 2015-11-23 10:42:12 +0000 | [diff] [blame] | 18 | #include <linux/timekeeper_internal.h> |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 19 | |
Gerd Hoffmann | 1c7b67f | 2008-06-03 16:17:30 +0200 | [diff] [blame] | 20 | #include <asm/pvclock.h> |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 21 | #include <asm/xen/hypervisor.h> |
| 22 | #include <asm/xen/hypercall.h> |
| 23 | |
| 24 | #include <xen/events.h> |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 25 | #include <xen/features.h> |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 26 | #include <xen/interface/xen.h> |
| 27 | #include <xen/interface/vcpu.h> |
| 28 | |
| 29 | #include "xen-ops.h" |
| 30 | |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 31 | /* Xen may fire a timer up to this many ns early */ |
| 32 | #define TIMER_SLOP 100000 |
Jeremy Fitzhardinge | f91a8b4 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 33 | |
Alok Kataria | e93ef94 | 2008-07-01 11:43:36 -0700 | [diff] [blame] | 34 | /* Get the TSC speed from Xen */ |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 35 | static unsigned long xen_tsc_khz(void) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 36 | { |
Glauber Costa | 3807f34 | 2008-07-28 11:47:52 -0300 | [diff] [blame] | 37 | struct pvclock_vcpu_time_info *info = |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 38 | &HYPERVISOR_shared_info->vcpu_info[0].time; |
| 39 | |
Glauber Costa | 3807f34 | 2008-07-28 11:47:52 -0300 | [diff] [blame] | 40 | return pvclock_tsc_khz(info); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 43 | u64 xen_clocksource_read(void) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 44 | { |
Gerd Hoffmann | 1c7b67f | 2008-06-03 16:17:30 +0200 | [diff] [blame] | 45 | struct pvclock_vcpu_time_info *src; |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 46 | u64 ret; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 47 | |
Jeremy Fitzhardinge | f1c3962 | 2011-08-24 09:54:24 -0700 | [diff] [blame] | 48 | preempt_disable_notrace(); |
Boris Ostrovsky | 3251f20 | 2014-10-16 17:02:15 -0400 | [diff] [blame] | 49 | src = &__this_cpu_read(xen_vcpu)->time; |
Gerd Hoffmann | 1c7b67f | 2008-06-03 16:17:30 +0200 | [diff] [blame] | 50 | ret = pvclock_clocksource_read(src); |
Jeremy Fitzhardinge | f1c3962 | 2011-08-24 09:54:24 -0700 | [diff] [blame] | 51 | preempt_enable_notrace(); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 52 | return ret; |
| 53 | } |
| 54 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 55 | static u64 xen_clocksource_get_cycles(struct clocksource *cs) |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 56 | { |
| 57 | return xen_clocksource_read(); |
| 58 | } |
| 59 | |
Arnd Bergmann | e27c492 | 2018-04-27 22:13:23 +0200 | [diff] [blame] | 60 | static void xen_read_wallclock(struct timespec64 *ts) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 61 | { |
Gerd Hoffmann | 1c7b67f | 2008-06-03 16:17:30 +0200 | [diff] [blame] | 62 | 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 Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 65 | |
Gerd Hoffmann | 1c7b67f | 2008-06-03 16:17:30 +0200 | [diff] [blame] | 66 | vcpu_time = &get_cpu_var(xen_vcpu)->time; |
| 67 | pvclock_read_wallclock(wall_clock, vcpu_time, ts); |
| 68 | put_cpu_var(xen_vcpu); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Arnd Bergmann | e27c492 | 2018-04-27 22:13:23 +0200 | [diff] [blame] | 71 | static void xen_get_wallclock(struct timespec64 *now) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 72 | { |
David Vrabel | 3565184 | 2013-05-13 18:56:06 +0100 | [diff] [blame] | 73 | xen_read_wallclock(now); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Arnd Bergmann | e27c492 | 2018-04-27 22:13:23 +0200 | [diff] [blame] | 76 | static int xen_set_wallclock(const struct timespec64 *now) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 77 | { |
Boris Ostrovsky | b5494ad | 2017-11-02 18:18:03 -0400 | [diff] [blame] | 78 | return -ENODEV; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 79 | } |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 80 | |
David Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 81 | static int xen_pvclock_gtod_notify(struct notifier_block *nb, |
| 82 | unsigned long was_set, void *priv) |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 83 | { |
David Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 84 | /* Protected by the calling core code serialization */ |
Stefano Stabellini | 187b26a | 2015-11-24 14:53:02 +0000 | [diff] [blame] | 85 | static struct timespec64 next_sync; |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 86 | |
David Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 87 | struct xen_platform_op op; |
Stefano Stabellini | 7609686 | 2015-11-23 10:42:12 +0000 | [diff] [blame] | 88 | struct timespec64 now; |
| 89 | struct timekeeper *tk = priv; |
| 90 | static bool settime64_supported = true; |
| 91 | int ret; |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 92 | |
Stefano Stabellini | 7609686 | 2015-11-23 10:42:12 +0000 | [diff] [blame] | 93 | now.tv_sec = tk->xtime_sec; |
| 94 | now.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift); |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 95 | |
David Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 96 | /* |
| 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 Stabellini | 187b26a | 2015-11-24 14:53:02 +0000 | [diff] [blame] | 100 | if (!was_set && timespec64_compare(&now, &next_sync) < 0) |
David Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 101 | return NOTIFY_OK; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 102 | |
Stefano Stabellini | 7609686 | 2015-11-23 10:42:12 +0000 | [diff] [blame] | 103 | again: |
| 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 Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 116 | |
Stefano Stabellini | 7609686 | 2015-11-23 10:42:12 +0000 | [diff] [blame] | 117 | 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 Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 125 | |
David Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 126 | /* |
| 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 Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 134 | return NOTIFY_OK; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 135 | } |
| 136 | |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 137 | static struct notifier_block xen_pvclock_gtod_notifier = { |
| 138 | .notifier_call = xen_pvclock_gtod_notify, |
| 139 | }; |
| 140 | |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 141 | static 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 | */ |
| 180 | static s64 get_abs_timeout(unsigned long delta) |
| 181 | { |
| 182 | return xen_clocksource_read() + delta; |
| 183 | } |
| 184 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 185 | static int xen_timerop_shutdown(struct clock_event_device *evt) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 186 | { |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 187 | /* cancel timeout */ |
| 188 | HYPERVISOR_set_timer_op(0); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 189 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 190 | return 0; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static int xen_timerop_set_next_event(unsigned long delta, |
| 194 | struct clock_event_device *evt) |
| 195 | { |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 196 | WARN_ON(!clockevent_state_oneshot(evt)); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 197 | |
| 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 | |
| 208 | static const struct clock_event_device xen_timerop_clockevent = { |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 209 | .name = "xen", |
| 210 | .features = CLOCK_EVT_FEAT_ONESHOT, |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 211 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 212 | .max_delta_ns = 0xffffffff, |
Nicolai Stange | 3d18d66 | 2017-03-30 22:06:42 +0200 | [diff] [blame] | 213 | .max_delta_ticks = 0xffffffff, |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 214 | .min_delta_ns = TIMER_SLOP, |
Nicolai Stange | 3d18d66 | 2017-03-30 22:06:42 +0200 | [diff] [blame] | 215 | .min_delta_ticks = TIMER_SLOP, |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 216 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 217 | .mult = 1, |
| 218 | .shift = 0, |
| 219 | .rating = 500, |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 220 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 221 | .set_state_shutdown = xen_timerop_shutdown, |
| 222 | .set_next_event = xen_timerop_set_next_event, |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 223 | }; |
| 224 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 225 | static int xen_vcpuop_shutdown(struct clock_event_device *evt) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 226 | { |
| 227 | int cpu = smp_processor_id(); |
| 228 | |
Vitaly Kuznetsov | ad5475f | 2016-06-30 17:56:38 +0200 | [diff] [blame] | 229 | 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 Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 233 | BUG(); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 234 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 235 | return 0; |
| 236 | } |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 237 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 238 | static int xen_vcpuop_set_oneshot(struct clock_event_device *evt) |
| 239 | { |
| 240 | int cpu = smp_processor_id(); |
| 241 | |
Vitaly Kuznetsov | ad5475f | 2016-06-30 17:56:38 +0200 | [diff] [blame] | 242 | if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu), |
| 243 | NULL)) |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 244 | BUG(); |
| 245 | |
| 246 | return 0; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static 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 Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 256 | WARN_ON(!clockevent_state_oneshot(evt)); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 257 | |
| 258 | single.timeout_abs_ns = get_abs_timeout(delta); |
Stefano Stabellini | c06b6d7 | 2016-04-15 18:23:00 -0700 | [diff] [blame] | 259 | /* Get an event anyway, even if the timeout is already expired */ |
| 260 | single.flags = 0; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 261 | |
Vitaly Kuznetsov | ad5475f | 2016-06-30 17:56:38 +0200 | [diff] [blame] | 262 | ret = HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, xen_vcpu_nr(cpu), |
| 263 | &single); |
Stefano Stabellini | c06b6d7 | 2016-04-15 18:23:00 -0700 | [diff] [blame] | 264 | BUG_ON(ret != 0); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 265 | |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | static const struct clock_event_device xen_vcpuop_clockevent = { |
| 270 | .name = "xen", |
| 271 | .features = CLOCK_EVT_FEAT_ONESHOT, |
| 272 | |
| 273 | .max_delta_ns = 0xffffffff, |
Nicolai Stange | 3d18d66 | 2017-03-30 22:06:42 +0200 | [diff] [blame] | 274 | .max_delta_ticks = 0xffffffff, |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 275 | .min_delta_ns = TIMER_SLOP, |
Nicolai Stange | 3d18d66 | 2017-03-30 22:06:42 +0200 | [diff] [blame] | 276 | .min_delta_ticks = TIMER_SLOP, |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 277 | |
| 278 | .mult = 1, |
| 279 | .shift = 0, |
| 280 | .rating = 500, |
| 281 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 282 | .set_state_shutdown = xen_vcpuop_shutdown, |
| 283 | .set_state_oneshot = xen_vcpuop_set_oneshot, |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 284 | .set_next_event = xen_vcpuop_set_next_event, |
| 285 | }; |
| 286 | |
| 287 | static const struct clock_event_device *xen_clockevent = |
| 288 | &xen_timerop_clockevent; |
Konrad Rzeszutek Wilk | 31620a1 | 2013-06-04 17:06:36 -0400 | [diff] [blame] | 289 | |
| 290 | struct xen_clock_event_device { |
| 291 | struct clock_event_device evt; |
Vitaly Kuznetsov | 7be0772 | 2015-01-05 16:27:51 +0100 | [diff] [blame] | 292 | char name[16]; |
Konrad Rzeszutek Wilk | 31620a1 | 2013-06-04 17:06:36 -0400 | [diff] [blame] | 293 | }; |
| 294 | static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 }; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 295 | |
| 296 | static irqreturn_t xen_timer_interrupt(int irq, void *dev_id) |
| 297 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 298 | struct clock_event_device *evt = this_cpu_ptr(&xen_clock_events.evt); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 299 | 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 Wilk | 09e99da | 2013-06-04 17:13:29 -0400 | [diff] [blame] | 310 | void xen_teardown_timer(int cpu) |
| 311 | { |
| 312 | struct clock_event_device *evt; |
Konrad Rzeszutek Wilk | 09e99da | 2013-06-04 17:13:29 -0400 | [diff] [blame] | 313 | 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 Wilk | 09e99da | 2013-06-04 17:13:29 -0400 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 321 | void xen_setup_timer(int cpu) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 322 | { |
Vitaly Kuznetsov | 7be0772 | 2015-01-05 16:27:51 +0100 | [diff] [blame] | 323 | struct xen_clock_event_device *xevt = &per_cpu(xen_clock_events, cpu); |
| 324 | struct clock_event_device *evt = &xevt->evt; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 325 | int irq; |
| 326 | |
Konrad Rzeszutek Wilk | ef35a4e | 2013-04-08 21:05:15 -0400 | [diff] [blame] | 327 | WARN(evt->irq >= 0, "IRQ%d for CPU%d is already allocated\n", evt->irq, cpu); |
Konrad Rzeszutek Wilk | 09e99da | 2013-06-04 17:13:29 -0400 | [diff] [blame] | 328 | if (evt->irq >= 0) |
| 329 | xen_teardown_timer(cpu); |
Konrad Rzeszutek Wilk | ef35a4e | 2013-04-08 21:05:15 -0400 | [diff] [blame] | 330 | |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 331 | printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu); |
| 332 | |
Vitaly Kuznetsov | 7be0772 | 2015-01-05 16:27:51 +0100 | [diff] [blame] | 333 | snprintf(xevt->name, sizeof(xevt->name), "timer%d", cpu); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 334 | |
| 335 | irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt, |
Michael Opdenacker | 9d71cee | 2013-09-07 08:46:49 +0200 | [diff] [blame] | 336 | IRQF_PERCPU|IRQF_NOBALANCING|IRQF_TIMER| |
David Vrabel | 8d5999d | 2014-08-07 17:06:06 +0100 | [diff] [blame] | 337 | IRQF_FORCE_RESUME|IRQF_EARLY_RESUME, |
Vitaly Kuznetsov | 7be0772 | 2015-01-05 16:27:51 +0100 | [diff] [blame] | 338 | xevt->name, NULL); |
David Vrabel | 8785c67 | 2013-09-23 12:52:21 +0100 | [diff] [blame] | 339 | (void)xen_set_irq_priority(irq, XEN_IRQ_PRIORITY_MAX); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 340 | |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 341 | memcpy(evt, xen_clockevent, sizeof(*evt)); |
| 342 | |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 343 | evt->cpumask = cpumask_of(cpu); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 344 | evt->irq = irq; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 345 | } |
Jeremy Fitzhardinge | f91a8b4 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 346 | |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 347 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 348 | void xen_setup_cpu_clockevents(void) |
| 349 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 350 | clockevents_register_device(this_cpu_ptr(&xen_clock_events.evt)); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Jeremy Fitzhardinge | d07af1f | 2008-05-31 01:33:03 +0100 | [diff] [blame] | 353 | void xen_timer_resume(void) |
| 354 | { |
| 355 | int cpu; |
| 356 | |
Jeremy Fitzhardinge | e7a3481c | 2010-10-25 16:53:46 -0700 | [diff] [blame] | 357 | pvclock_resume(); |
| 358 | |
Jeremy Fitzhardinge | d07af1f | 2008-05-31 01:33:03 +0100 | [diff] [blame] | 359 | if (xen_clockevent != &xen_vcpuop_clockevent) |
| 360 | return; |
| 361 | |
| 362 | for_each_online_cpu(cpu) { |
Vitaly Kuznetsov | ad5475f | 2016-06-30 17:56:38 +0200 | [diff] [blame] | 363 | if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, |
| 364 | xen_vcpu_nr(cpu), NULL)) |
Jeremy Fitzhardinge | d07af1f | 2008-05-31 01:33:03 +0100 | [diff] [blame] | 365 | BUG(); |
| 366 | } |
| 367 | } |
| 368 | |
Daniel Kiper | fb6ce5d | 2011-05-04 20:18:45 +0200 | [diff] [blame] | 369 | static const struct pv_time_ops xen_time_ops __initconst = { |
Jeremy Fitzhardinge | ca50a5f | 2010-08-04 14:49:16 -0700 | [diff] [blame] | 370 | .sched_clock = xen_clocksource_read, |
Juergen Gross | d34c30c | 2016-07-26 14:15:11 +0200 | [diff] [blame] | 371 | .steal_clock = xen_steal_clock, |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 372 | }; |
| 373 | |
Joao Martins | 2229f70 | 2017-11-08 17:19:57 +0000 | [diff] [blame] | 374 | static struct pvclock_vsyscall_time_info *xen_clock __read_mostly; |
| 375 | |
| 376 | void 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 | |
| 394 | void 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 | |
| 419 | static 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 Kiper | fb6ce5d | 2011-05-04 20:18:45 +0200 | [diff] [blame] | 460 | static void __init xen_time_init(void) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 461 | { |
Joao Martins | b888808 | 2017-11-08 17:19:56 +0000 | [diff] [blame] | 462 | struct pvclock_vcpu_time_info *pvti; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 463 | int cpu = smp_processor_id(); |
Arnd Bergmann | e27c492 | 2018-04-27 22:13:23 +0200 | [diff] [blame] | 464 | struct timespec64 tp; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 465 | |
Palik, Imre | 94dd85f | 2015-01-13 09:14:22 +0100 | [diff] [blame] | 466 | /* As Dom0 is never moved, no penalty on using TSC there */ |
| 467 | if (xen_initial_domain()) |
| 468 | xen_clocksource.rating = 275; |
| 469 | |
John Stultz | b01cc1b | 2010-04-26 19:03:05 -0700 | [diff] [blame] | 470 | clocksource_register_hz(&xen_clocksource, NSEC_PER_SEC); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 471 | |
Vitaly Kuznetsov | ad5475f | 2016-06-30 17:56:38 +0200 | [diff] [blame] | 472 | if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu), |
| 473 | NULL) == 0) { |
Jeremy Fitzhardinge | f91a8b4 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 474 | /* Successfully turned off 100Hz tick, so we have the |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 475 | 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 Stultz | c450725 | 2010-03-11 14:04:47 -0800 | [diff] [blame] | 481 | xen_read_wallclock(&tp); |
Arnd Bergmann | e27c492 | 2018-04-27 22:13:23 +0200 | [diff] [blame] | 482 | do_settimeofday64(&tp); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 483 | |
Andi Kleen | 404ee5b | 2008-01-30 13:33:20 +0100 | [diff] [blame] | 484 | setup_force_cpu_cap(X86_FEATURE_TSC); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 485 | |
Joao Martins | b888808 | 2017-11-08 17:19:56 +0000 | [diff] [blame] | 486 | /* |
| 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 Martins | 2229f70 | 2017-11-08 17:19:57 +0000 | [diff] [blame] | 491 | if (pvti->flags & PVCLOCK_TSC_STABLE_BIT) { |
Joao Martins | b888808 | 2017-11-08 17:19:56 +0000 | [diff] [blame] | 492 | pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT); |
Joao Martins | 2229f70 | 2017-11-08 17:19:57 +0000 | [diff] [blame] | 493 | xen_setup_vsyscall_time_info(); |
| 494 | } |
Joao Martins | b888808 | 2017-11-08 17:19:56 +0000 | [diff] [blame] | 495 | |
Ian Campbell | be012920 | 2009-11-21 08:35:55 +0800 | [diff] [blame] | 496 | xen_setup_runstate_info(cpu); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 497 | xen_setup_timer(cpu); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 498 | xen_setup_cpu_clockevents(); |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 499 | |
Juergen Gross | ecb23dc | 2016-05-20 09:26:48 +0200 | [diff] [blame] | 500 | xen_time_setup_guest(); |
| 501 | |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 502 | if (xen_initial_domain()) |
| 503 | pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 504 | } |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 505 | |
Boris Ostrovsky | d162809 | 2017-05-03 16:20:51 -0400 | [diff] [blame] | 506 | void __ref xen_init_time_ops(void) |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 507 | { |
| 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 Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 516 | /* Dom0 uses the native method to set the hardware RTC. */ |
| 517 | if (!xen_initial_domain()) |
| 518 | x86_platform.set_wallclock = xen_set_wallclock; |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Stefano Stabellini | ca65f9f | 2010-07-29 14:37:48 +0100 | [diff] [blame] | 521 | #ifdef CONFIG_XEN_PVHVM |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 522 | static void xen_hvm_setup_cpu_clockevents(void) |
| 523 | { |
| 524 | int cpu = smp_processor_id(); |
| 525 | xen_setup_runstate_info(cpu); |
Konrad Rzeszutek Wilk | 7918c92 | 2013-04-16 15:18:00 -0400 | [diff] [blame] | 526 | /* |
| 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 Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 531 | xen_setup_cpu_clockevents(); |
| 532 | } |
| 533 | |
Daniel Kiper | fb6ce5d | 2011-05-04 20:18:45 +0200 | [diff] [blame] | 534 | void __init xen_hvm_init_time_ops(void) |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 535 | { |
Boris Ostrovsky | 84d582d | 2017-04-24 15:04:53 -0400 | [diff] [blame] | 536 | /* |
| 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 Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 544 | 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 Stabellini | ca65f9f | 2010-07-29 14:37:48 +0100 | [diff] [blame] | 558 | #endif |