Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/gfp.h> |
Konrad Rzeszutek Wilk | c9d76a2 | 2013-06-04 17:09:36 -0400 | [diff] [blame] | 15 | #include <linux/slab.h> |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 16 | #include <linux/pvclock_gtod.h> |
Stefano Stabellini | 7609686 | 2015-11-23 10:42:12 +0000 | [diff] [blame] | 17 | #include <linux/timekeeper_internal.h> |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 18 | |
Gerd Hoffmann | 1c7b67f | 2008-06-03 16:17:30 +0200 | [diff] [blame] | 19 | #include <asm/pvclock.h> |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 20 | #include <asm/xen/hypervisor.h> |
| 21 | #include <asm/xen/hypercall.h> |
| 22 | |
| 23 | #include <xen/events.h> |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 24 | #include <xen/features.h> |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 25 | #include <xen/interface/xen.h> |
| 26 | #include <xen/interface/vcpu.h> |
| 27 | |
| 28 | #include "xen-ops.h" |
| 29 | |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 30 | /* Xen may fire a timer up to this many ns early */ |
| 31 | #define TIMER_SLOP 100000 |
Jeremy Fitzhardinge | f91a8b4 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 32 | |
Alok Kataria | e93ef94 | 2008-07-01 11:43:36 -0700 | [diff] [blame] | 33 | /* Get the TSC speed from Xen */ |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 34 | static unsigned long xen_tsc_khz(void) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 35 | { |
Glauber Costa | 3807f34 | 2008-07-28 11:47:52 -0300 | [diff] [blame] | 36 | struct pvclock_vcpu_time_info *info = |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 37 | &HYPERVISOR_shared_info->vcpu_info[0].time; |
| 38 | |
Glauber Costa | 3807f34 | 2008-07-28 11:47:52 -0300 | [diff] [blame] | 39 | return pvclock_tsc_khz(info); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Jeremy Fitzhardinge | ee7686b | 2008-08-21 13:17:56 -0700 | [diff] [blame] | 42 | cycle_t xen_clocksource_read(void) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 43 | { |
Gerd Hoffmann | 1c7b67f | 2008-06-03 16:17:30 +0200 | [diff] [blame] | 44 | struct pvclock_vcpu_time_info *src; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 45 | cycle_t ret; |
| 46 | |
Jeremy Fitzhardinge | f1c3962 | 2011-08-24 09:54:24 -0700 | [diff] [blame] | 47 | preempt_disable_notrace(); |
Boris Ostrovsky | 3251f20 | 2014-10-16 17:02:15 -0400 | [diff] [blame] | 48 | src = &__this_cpu_read(xen_vcpu)->time; |
Gerd Hoffmann | 1c7b67f | 2008-06-03 16:17:30 +0200 | [diff] [blame] | 49 | ret = pvclock_clocksource_read(src); |
Jeremy Fitzhardinge | f1c3962 | 2011-08-24 09:54:24 -0700 | [diff] [blame] | 50 | preempt_enable_notrace(); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 51 | return ret; |
| 52 | } |
| 53 | |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 54 | static cycle_t xen_clocksource_get_cycles(struct clocksource *cs) |
| 55 | { |
| 56 | return xen_clocksource_read(); |
| 57 | } |
| 58 | |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 59 | static void xen_read_wallclock(struct timespec *ts) |
| 60 | { |
Gerd Hoffmann | 1c7b67f | 2008-06-03 16:17:30 +0200 | [diff] [blame] | 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; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 64 | |
Gerd Hoffmann | 1c7b67f | 2008-06-03 16:17:30 +0200 | [diff] [blame] | 65 | vcpu_time = &get_cpu_var(xen_vcpu)->time; |
| 66 | pvclock_read_wallclock(wall_clock, vcpu_time, ts); |
| 67 | put_cpu_var(xen_vcpu); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 68 | } |
| 69 | |
David Vrabel | 3565184 | 2013-05-13 18:56:06 +0100 | [diff] [blame] | 70 | static void xen_get_wallclock(struct timespec *now) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 71 | { |
David Vrabel | 3565184 | 2013-05-13 18:56:06 +0100 | [diff] [blame] | 72 | xen_read_wallclock(now); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 73 | } |
| 74 | |
David Vrabel | 3565184 | 2013-05-13 18:56:06 +0100 | [diff] [blame] | 75 | static int xen_set_wallclock(const struct timespec *now) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 76 | { |
David Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 77 | return -1; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 78 | } |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 79 | |
David Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 80 | static int xen_pvclock_gtod_notify(struct notifier_block *nb, |
| 81 | unsigned long was_set, void *priv) |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 82 | { |
David Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 83 | /* Protected by the calling core code serialization */ |
Stefano Stabellini | 187b26a | 2015-11-24 14:53:02 +0000 | [diff] [blame] | 84 | static struct timespec64 next_sync; |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 85 | |
David Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 86 | struct xen_platform_op op; |
Stefano Stabellini | 7609686 | 2015-11-23 10:42:12 +0000 | [diff] [blame] | 87 | struct timespec64 now; |
| 88 | struct timekeeper *tk = priv; |
| 89 | static bool settime64_supported = true; |
| 90 | int ret; |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 91 | |
Stefano Stabellini | 7609686 | 2015-11-23 10:42:12 +0000 | [diff] [blame] | 92 | now.tv_sec = tk->xtime_sec; |
| 93 | 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] | 94 | |
David Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 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 | */ |
Stefano Stabellini | 187b26a | 2015-11-24 14:53:02 +0000 | [diff] [blame] | 99 | if (!was_set && timespec64_compare(&now, &next_sync) < 0) |
David Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 100 | return NOTIFY_OK; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 101 | |
Stefano Stabellini | 7609686 | 2015-11-23 10:42:12 +0000 | [diff] [blame] | 102 | again: |
| 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 | } |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 115 | |
Stefano Stabellini | 7609686 | 2015-11-23 10:42:12 +0000 | [diff] [blame] | 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; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 124 | |
David Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 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 | |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 133 | return NOTIFY_OK; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 134 | } |
| 135 | |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 136 | static struct notifier_block xen_pvclock_gtod_notifier = { |
| 137 | .notifier_call = xen_pvclock_gtod_notify, |
| 138 | }; |
| 139 | |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 140 | static struct clocksource xen_clocksource __read_mostly = { |
| 141 | .name = "xen", |
| 142 | .rating = 400, |
| 143 | .read = xen_clocksource_get_cycles, |
| 144 | .mask = ~0, |
| 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 | */ |
| 179 | static s64 get_abs_timeout(unsigned long delta) |
| 180 | { |
| 181 | return xen_clocksource_read() + delta; |
| 182 | } |
| 183 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 184 | static int xen_timerop_shutdown(struct clock_event_device *evt) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 185 | { |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 186 | /* cancel timeout */ |
| 187 | HYPERVISOR_set_timer_op(0); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 188 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 189 | return 0; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static int xen_timerop_set_next_event(unsigned long delta, |
| 193 | struct clock_event_device *evt) |
| 194 | { |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 195 | WARN_ON(!clockevent_state_oneshot(evt)); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 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 | |
| 207 | static const struct clock_event_device xen_timerop_clockevent = { |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 208 | .name = "xen", |
| 209 | .features = CLOCK_EVT_FEAT_ONESHOT, |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 210 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 211 | .max_delta_ns = 0xffffffff, |
| 212 | .min_delta_ns = TIMER_SLOP, |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 213 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 214 | .mult = 1, |
| 215 | .shift = 0, |
| 216 | .rating = 500, |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 217 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 218 | .set_state_shutdown = xen_timerop_shutdown, |
| 219 | .set_next_event = xen_timerop_set_next_event, |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 222 | static int xen_vcpuop_shutdown(struct clock_event_device *evt) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 223 | { |
| 224 | int cpu = smp_processor_id(); |
| 225 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 226 | if (HYPERVISOR_vcpu_op(VCPUOP_stop_singleshot_timer, cpu, NULL) || |
| 227 | HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL)) |
| 228 | BUG(); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 229 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 230 | return 0; |
| 231 | } |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 232 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 233 | static int xen_vcpuop_set_oneshot(struct clock_event_device *evt) |
| 234 | { |
| 235 | int cpu = smp_processor_id(); |
| 236 | |
| 237 | if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL)) |
| 238 | BUG(); |
| 239 | |
| 240 | return 0; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static int xen_vcpuop_set_next_event(unsigned long delta, |
| 244 | struct clock_event_device *evt) |
| 245 | { |
| 246 | int cpu = smp_processor_id(); |
| 247 | struct vcpu_set_singleshot_timer single; |
| 248 | int ret; |
| 249 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 250 | WARN_ON(!clockevent_state_oneshot(evt)); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 251 | |
| 252 | single.timeout_abs_ns = get_abs_timeout(delta); |
Stefano Stabellini | c06b6d7 | 2016-04-15 18:23:00 -0700 | [diff] [blame] | 253 | /* Get an event anyway, even if the timeout is already expired */ |
| 254 | single.flags = 0; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 255 | |
| 256 | ret = HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, cpu, &single); |
Stefano Stabellini | c06b6d7 | 2016-04-15 18:23:00 -0700 | [diff] [blame] | 257 | BUG_ON(ret != 0); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 258 | |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | static const struct clock_event_device xen_vcpuop_clockevent = { |
| 263 | .name = "xen", |
| 264 | .features = CLOCK_EVT_FEAT_ONESHOT, |
| 265 | |
| 266 | .max_delta_ns = 0xffffffff, |
| 267 | .min_delta_ns = TIMER_SLOP, |
| 268 | |
| 269 | .mult = 1, |
| 270 | .shift = 0, |
| 271 | .rating = 500, |
| 272 | |
Viresh Kumar | 955381d | 2015-07-16 16:28:48 +0530 | [diff] [blame] | 273 | .set_state_shutdown = xen_vcpuop_shutdown, |
| 274 | .set_state_oneshot = xen_vcpuop_set_oneshot, |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 275 | .set_next_event = xen_vcpuop_set_next_event, |
| 276 | }; |
| 277 | |
| 278 | static const struct clock_event_device *xen_clockevent = |
| 279 | &xen_timerop_clockevent; |
Konrad Rzeszutek Wilk | 31620a1 | 2013-06-04 17:06:36 -0400 | [diff] [blame] | 280 | |
| 281 | struct xen_clock_event_device { |
| 282 | struct clock_event_device evt; |
Vitaly Kuznetsov | 7be0772 | 2015-01-05 16:27:51 +0100 | [diff] [blame] | 283 | char name[16]; |
Konrad Rzeszutek Wilk | 31620a1 | 2013-06-04 17:06:36 -0400 | [diff] [blame] | 284 | }; |
| 285 | 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] | 286 | |
| 287 | static irqreturn_t xen_timer_interrupt(int irq, void *dev_id) |
| 288 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 289 | struct clock_event_device *evt = this_cpu_ptr(&xen_clock_events.evt); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 290 | irqreturn_t ret; |
| 291 | |
| 292 | ret = IRQ_NONE; |
| 293 | if (evt->event_handler) { |
| 294 | evt->event_handler(evt); |
| 295 | ret = IRQ_HANDLED; |
| 296 | } |
| 297 | |
| 298 | return ret; |
| 299 | } |
| 300 | |
Konrad Rzeszutek Wilk | 09e99da | 2013-06-04 17:13:29 -0400 | [diff] [blame] | 301 | void xen_teardown_timer(int cpu) |
| 302 | { |
| 303 | struct clock_event_device *evt; |
| 304 | BUG_ON(cpu == 0); |
| 305 | evt = &per_cpu(xen_clock_events, cpu).evt; |
| 306 | |
| 307 | if (evt->irq >= 0) { |
| 308 | unbind_from_irqhandler(evt->irq, NULL); |
| 309 | evt->irq = -1; |
Konrad Rzeszutek Wilk | 09e99da | 2013-06-04 17:13:29 -0400 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 313 | void xen_setup_timer(int cpu) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 314 | { |
Vitaly Kuznetsov | 7be0772 | 2015-01-05 16:27:51 +0100 | [diff] [blame] | 315 | struct xen_clock_event_device *xevt = &per_cpu(xen_clock_events, cpu); |
| 316 | struct clock_event_device *evt = &xevt->evt; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 317 | int irq; |
| 318 | |
Konrad Rzeszutek Wilk | ef35a4e | 2013-04-08 21:05:15 -0400 | [diff] [blame] | 319 | 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] | 320 | if (evt->irq >= 0) |
| 321 | xen_teardown_timer(cpu); |
Konrad Rzeszutek Wilk | ef35a4e | 2013-04-08 21:05:15 -0400 | [diff] [blame] | 322 | |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 323 | printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu); |
| 324 | |
Vitaly Kuznetsov | 7be0772 | 2015-01-05 16:27:51 +0100 | [diff] [blame] | 325 | snprintf(xevt->name, sizeof(xevt->name), "timer%d", cpu); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 326 | |
| 327 | irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt, |
Michael Opdenacker | 9d71cee | 2013-09-07 08:46:49 +0200 | [diff] [blame] | 328 | IRQF_PERCPU|IRQF_NOBALANCING|IRQF_TIMER| |
David Vrabel | 8d5999d | 2014-08-07 17:06:06 +0100 | [diff] [blame] | 329 | IRQF_FORCE_RESUME|IRQF_EARLY_RESUME, |
Vitaly Kuznetsov | 7be0772 | 2015-01-05 16:27:51 +0100 | [diff] [blame] | 330 | xevt->name, NULL); |
David Vrabel | 8785c67 | 2013-09-23 12:52:21 +0100 | [diff] [blame] | 331 | (void)xen_set_irq_priority(irq, XEN_IRQ_PRIORITY_MAX); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 332 | |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 333 | memcpy(evt, xen_clockevent, sizeof(*evt)); |
| 334 | |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 335 | evt->cpumask = cpumask_of(cpu); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 336 | evt->irq = irq; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 337 | } |
Jeremy Fitzhardinge | f91a8b4 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 338 | |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 339 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 340 | void xen_setup_cpu_clockevents(void) |
| 341 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 342 | clockevents_register_device(this_cpu_ptr(&xen_clock_events.evt)); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Jeremy Fitzhardinge | d07af1f | 2008-05-31 01:33:03 +0100 | [diff] [blame] | 345 | void xen_timer_resume(void) |
| 346 | { |
| 347 | int cpu; |
| 348 | |
Jeremy Fitzhardinge | e7a3481c | 2010-10-25 16:53:46 -0700 | [diff] [blame] | 349 | pvclock_resume(); |
| 350 | |
Jeremy Fitzhardinge | d07af1f | 2008-05-31 01:33:03 +0100 | [diff] [blame] | 351 | if (xen_clockevent != &xen_vcpuop_clockevent) |
| 352 | return; |
| 353 | |
| 354 | for_each_online_cpu(cpu) { |
| 355 | if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL)) |
| 356 | BUG(); |
| 357 | } |
| 358 | } |
| 359 | |
Daniel Kiper | fb6ce5d | 2011-05-04 20:18:45 +0200 | [diff] [blame] | 360 | static const struct pv_time_ops xen_time_ops __initconst = { |
Jeremy Fitzhardinge | ca50a5f | 2010-08-04 14:49:16 -0700 | [diff] [blame] | 361 | .sched_clock = xen_clocksource_read, |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 362 | }; |
| 363 | |
Daniel Kiper | fb6ce5d | 2011-05-04 20:18:45 +0200 | [diff] [blame] | 364 | static void __init xen_time_init(void) |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 365 | { |
| 366 | int cpu = smp_processor_id(); |
John Stultz | c450725 | 2010-03-11 14:04:47 -0800 | [diff] [blame] | 367 | struct timespec tp; |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 368 | |
Palik, Imre | 94dd85f | 2015-01-13 09:14:22 +0100 | [diff] [blame] | 369 | /* As Dom0 is never moved, no penalty on using TSC there */ |
| 370 | if (xen_initial_domain()) |
| 371 | xen_clocksource.rating = 275; |
| 372 | |
John Stultz | b01cc1b | 2010-04-26 19:03:05 -0700 | [diff] [blame] | 373 | clocksource_register_hz(&xen_clocksource, NSEC_PER_SEC); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 374 | |
| 375 | if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL) == 0) { |
Jeremy Fitzhardinge | f91a8b4 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 376 | /* Successfully turned off 100Hz tick, so we have the |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 377 | vcpuop-based timer interface */ |
| 378 | printk(KERN_DEBUG "Xen: using vcpuop timer interface\n"); |
| 379 | xen_clockevent = &xen_vcpuop_clockevent; |
| 380 | } |
| 381 | |
| 382 | /* Set initial system time with full resolution */ |
John Stultz | c450725 | 2010-03-11 14:04:47 -0800 | [diff] [blame] | 383 | xen_read_wallclock(&tp); |
| 384 | do_settimeofday(&tp); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 385 | |
Andi Kleen | 404ee5b | 2008-01-30 13:33:20 +0100 | [diff] [blame] | 386 | setup_force_cpu_cap(X86_FEATURE_TSC); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 387 | |
Ian Campbell | be01292 | 2009-11-21 08:35:55 +0800 | [diff] [blame] | 388 | xen_setup_runstate_info(cpu); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 389 | xen_setup_timer(cpu); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 390 | xen_setup_cpu_clockevents(); |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 391 | |
Juergen Gross | ecb23dc | 2016-05-20 09:26:48 +0200 | [diff] [blame] | 392 | xen_time_setup_guest(); |
| 393 | |
David Vrabel | 5584880 | 2013-06-27 11:35:47 +0100 | [diff] [blame] | 394 | if (xen_initial_domain()) |
| 395 | pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier); |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 396 | } |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 397 | |
Daniel Kiper | fb6ce5d | 2011-05-04 20:18:45 +0200 | [diff] [blame] | 398 | void __init xen_init_time_ops(void) |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 399 | { |
| 400 | pv_time_ops = xen_time_ops; |
| 401 | |
| 402 | x86_init.timers.timer_init = xen_time_init; |
| 403 | x86_init.timers.setup_percpu_clockev = x86_init_noop; |
| 404 | x86_cpuinit.setup_percpu_clockev = x86_init_noop; |
| 405 | |
| 406 | x86_platform.calibrate_tsc = xen_tsc_khz; |
| 407 | x86_platform.get_wallclock = xen_get_wallclock; |
David Vrabel | 47433b8 | 2013-06-27 11:35:48 +0100 | [diff] [blame] | 408 | /* Dom0 uses the native method to set the hardware RTC. */ |
| 409 | if (!xen_initial_domain()) |
| 410 | x86_platform.set_wallclock = xen_set_wallclock; |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Stefano Stabellini | ca65f9f | 2010-07-29 14:37:48 +0100 | [diff] [blame] | 413 | #ifdef CONFIG_XEN_PVHVM |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 414 | static void xen_hvm_setup_cpu_clockevents(void) |
| 415 | { |
| 416 | int cpu = smp_processor_id(); |
| 417 | xen_setup_runstate_info(cpu); |
Konrad Rzeszutek Wilk | 7918c92 | 2013-04-16 15:18:00 -0400 | [diff] [blame] | 418 | /* |
| 419 | * xen_setup_timer(cpu) - snprintf is bad in atomic context. Hence |
| 420 | * doing it xen_hvm_cpu_notify (which gets called by smp_init during |
| 421 | * early bootup and also during CPU hotplug events). |
| 422 | */ |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 423 | xen_setup_cpu_clockevents(); |
| 424 | } |
| 425 | |
Daniel Kiper | fb6ce5d | 2011-05-04 20:18:45 +0200 | [diff] [blame] | 426 | void __init xen_hvm_init_time_ops(void) |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 427 | { |
| 428 | /* vector callback is needed otherwise we cannot receive interrupts |
Stefano Stabellini | 31e7e93 | 2010-10-01 17:35:46 +0100 | [diff] [blame] | 429 | * on cpu > 0 and at this point we don't know how many cpus are |
| 430 | * available */ |
| 431 | if (!xen_have_vector_callback) |
Stefano Stabellini | 409771d | 2010-05-14 12:48:19 +0100 | [diff] [blame] | 432 | return; |
| 433 | if (!xen_feature(XENFEAT_hvm_safe_pvclock)) { |
| 434 | printk(KERN_INFO "Xen doesn't support pvclock on HVM," |
| 435 | "disable pv timer\n"); |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | pv_time_ops = xen_time_ops; |
| 440 | x86_init.timers.setup_percpu_clockev = xen_time_init; |
| 441 | x86_cpuinit.setup_percpu_clockev = xen_hvm_setup_cpu_clockevents; |
| 442 | |
| 443 | x86_platform.calibrate_tsc = xen_tsc_khz; |
| 444 | x86_platform.get_wallclock = xen_get_wallclock; |
| 445 | x86_platform.set_wallclock = xen_set_wallclock; |
| 446 | } |
Stefano Stabellini | ca65f9f | 2010-07-29 14:37:48 +0100 | [diff] [blame] | 447 | #endif |