]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - arch/x86/kernel/kvmclock.c
Merge remote-tracking branch 'asoc/topic/doc' into asoc-next
[mirror_ubuntu-zesty-kernel.git] / arch / x86 / kernel / kvmclock.c
CommitLineData
790c73f6
GOC
1/* KVM paravirtual clock driver. A clocksource implementation
2 Copyright (C) 2008 Glauber de Oliveira Costa, Red Hat Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include <linux/clocksource.h>
20#include <linux/kvm_para.h>
f6e16d5a 21#include <asm/pvclock.h>
790c73f6
GOC
22#include <asm/msr.h>
23#include <asm/apic.h>
24#include <linux/percpu.h>
3b5d56b9 25#include <linux/hardirq.h>
7069ed67 26#include <linux/memblock.h>
0ad83caa 27#include <linux/sched.h>
736decac
TG
28
29#include <asm/x86_init.h>
1e977aa1 30#include <asm/reboot.h>
790c73f6 31
790c73f6 32static int kvmclock = 1;
838815a7
GC
33static int msr_kvm_system_time = MSR_KVM_SYSTEM_TIME;
34static int msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK;
790c73f6
GOC
35
36static int parse_no_kvmclock(char *arg)
37{
38 kvmclock = 0;
39 return 0;
40}
41early_param("no-kvmclock", parse_no_kvmclock);
42
43/* The hypervisor will put information about time periodically here */
3dc4f7cf 44static struct pvclock_vsyscall_time_info *hv_clock;
f6e16d5a 45static struct pvclock_wall_clock wall_clock;
790c73f6 46
790c73f6
GOC
47/*
48 * The wallclock is the time of day when we booted. Since then, some time may
49 * have elapsed since the hypervisor wrote the data. So we try to account for
50 * that with system time
51 */
3565184e 52static void kvm_get_wallclock(struct timespec *now)
790c73f6 53{
f6e16d5a 54 struct pvclock_vcpu_time_info *vcpu_time;
790c73f6 55 int low, high;
7069ed67 56 int cpu;
790c73f6 57
a20316d2
GC
58 low = (int)__pa_symbol(&wall_clock);
59 high = ((u64)__pa_symbol(&wall_clock) >> 32);
838815a7
GC
60
61 native_write_msr(msr_kvm_wall_clock, low, high);
790c73f6 62
c6338ce4 63 cpu = get_cpu();
7069ed67 64
3dc4f7cf 65 vcpu_time = &hv_clock[cpu].pvti;
3565184e 66 pvclock_read_wallclock(&wall_clock, vcpu_time, now);
7069ed67 67
c6338ce4 68 put_cpu();
790c73f6
GOC
69}
70
3565184e 71static int kvm_set_wallclock(const struct timespec *now)
790c73f6 72{
f6e16d5a 73 return -1;
790c73f6
GOC
74}
75
790c73f6
GOC
76static cycle_t kvm_clock_read(void)
77{
f6e16d5a
GH
78 struct pvclock_vcpu_time_info *src;
79 cycle_t ret;
7069ed67 80 int cpu;
790c73f6 81
95ef1e52 82 preempt_disable_notrace();
7069ed67 83 cpu = smp_processor_id();
3dc4f7cf 84 src = &hv_clock[cpu].pvti;
f6e16d5a 85 ret = pvclock_clocksource_read(src);
95ef1e52 86 preempt_enable_notrace();
f6e16d5a 87 return ret;
790c73f6 88}
f6e16d5a 89
8e19608e
MD
90static cycle_t kvm_clock_get_cycles(struct clocksource *cs)
91{
92 return kvm_clock_read();
93}
94
0293615f
GC
95/*
96 * If we don't do that, there is the possibility that the guest
97 * will calibrate under heavy load - thus, getting a lower lpj -
98 * and execute the delays themselves without load. This is wrong,
99 * because no delay loop can finish beforehand.
100 * Any heuristics is subject to fail, because ultimately, a large
101 * poll of guests can be running and trouble each other. So we preset
102 * lpj here
103 */
104static unsigned long kvm_get_tsc_khz(void)
105{
e93353c9 106 struct pvclock_vcpu_time_info *src;
7069ed67
MT
107 int cpu;
108 unsigned long tsc_khz;
109
c6338ce4 110 cpu = get_cpu();
3dc4f7cf 111 src = &hv_clock[cpu].pvti;
7069ed67 112 tsc_khz = pvclock_tsc_khz(src);
c6338ce4 113 put_cpu();
7069ed67 114 return tsc_khz;
0293615f
GC
115}
116
117static void kvm_get_preset_lpj(void)
118{
0293615f
GC
119 unsigned long khz;
120 u64 lpj;
121
e93353c9 122 khz = kvm_get_tsc_khz();
0293615f
GC
123
124 lpj = ((u64)khz * 1000);
125 do_div(lpj, HZ);
126 preset_lpj = lpj;
127}
128
3b5d56b9
EM
129bool kvm_check_and_clear_guest_paused(void)
130{
131 bool ret = false;
132 struct pvclock_vcpu_time_info *src;
7069ed67
MT
133 int cpu = smp_processor_id();
134
135 if (!hv_clock)
136 return ret;
3b5d56b9 137
3dc4f7cf 138 src = &hv_clock[cpu].pvti;
3b5d56b9 139 if ((src->flags & PVCLOCK_GUEST_STOPPED) != 0) {
7069ed67 140 src->flags &= ~PVCLOCK_GUEST_STOPPED;
d63285e9 141 pvclock_touch_watchdogs();
3b5d56b9
EM
142 ret = true;
143 }
144
145 return ret;
146}
3b5d56b9 147
790c73f6
GOC
148static struct clocksource kvm_clock = {
149 .name = "kvm-clock",
8e19608e 150 .read = kvm_clock_get_cycles,
790c73f6
GOC
151 .rating = 400,
152 .mask = CLOCKSOURCE_MASK(64),
790c73f6
GOC
153 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
154};
155
ca3f1017 156int kvm_register_clock(char *txt)
790c73f6
GOC
157{
158 int cpu = smp_processor_id();
19b6a85b 159 int low, high, ret;
fe1140cc
JK
160 struct pvclock_vcpu_time_info *src;
161
162 if (!hv_clock)
163 return 0;
19b6a85b 164
fe1140cc 165 src = &hv_clock[cpu].pvti;
5dfd486c
DH
166 low = (int)slow_virt_to_phys(src) | 1;
167 high = ((u64)slow_virt_to_phys(src) >> 32);
19b6a85b 168 ret = native_write_msr_safe(msr_kvm_system_time, low, high);
f6e16d5a
GH
169 printk(KERN_INFO "kvm-clock: cpu %d, msr %x:%x, %s\n",
170 cpu, high, low, txt);
838815a7 171
19b6a85b 172 return ret;
790c73f6
GOC
173}
174
b74f05d6
MT
175static void kvm_save_sched_clock_state(void)
176{
177}
178
179static void kvm_restore_sched_clock_state(void)
180{
181 kvm_register_clock("primary cpu clock, resume");
182}
183
b8ba5f10 184#ifdef CONFIG_X86_LOCAL_APIC
148f9bb8 185static void kvm_setup_secondary_clock(void)
790c73f6
GOC
186{
187 /*
188 * Now that the first cpu already had this clocksource initialized,
189 * we shouldn't fail.
190 */
f6e16d5a 191 WARN_ON(kvm_register_clock("secondary cpu clock"));
790c73f6 192}
b8ba5f10 193#endif
790c73f6 194
1e977aa1
GC
195/*
196 * After the clock is registered, the host will keep writing to the
197 * registered memory location. If the guest happens to shutdown, this memory
198 * won't be valid. In cases like kexec, in which you install a new kernel, this
199 * means a random memory location will be kept being written. So before any
200 * kind of shutdown from our side, we unregister the clock by writting anything
201 * that does not have the 'enable' bit set in the msr
202 */
2965faa5 203#ifdef CONFIG_KEXEC_CORE
1e977aa1
GC
204static void kvm_crash_shutdown(struct pt_regs *regs)
205{
838815a7 206 native_write_msr(msr_kvm_system_time, 0, 0);
d910f5c1 207 kvm_disable_steal_time();
1e977aa1
GC
208 native_machine_crash_shutdown(regs);
209}
210#endif
211
212static void kvm_shutdown(void)
213{
838815a7 214 native_write_msr(msr_kvm_system_time, 0, 0);
d910f5c1 215 kvm_disable_steal_time();
1e977aa1
GC
216 native_machine_shutdown();
217}
218
790c73f6
GOC
219void __init kvmclock_init(void)
220{
0ad83caa 221 struct pvclock_vcpu_time_info *vcpu_time;
7069ed67 222 unsigned long mem;
0ad83caa
LC
223 int size, cpu;
224 u8 flags;
ed55705d
MT
225
226 size = PAGE_ALIGN(sizeof(struct pvclock_vsyscall_time_info)*NR_CPUS);
7069ed67 227
790c73f6
GOC
228 if (!kvm_para_available())
229 return;
230
838815a7
GC
231 if (kvmclock && kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2)) {
232 msr_kvm_system_time = MSR_KVM_SYSTEM_TIME_NEW;
233 msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK_NEW;
234 } else if (!(kvmclock && kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)))
235 return;
236
237 printk(KERN_INFO "kvm-clock: Using msrs %x and %x",
238 msr_kvm_system_time, msr_kvm_wall_clock);
239
ed55705d 240 mem = memblock_alloc(size, PAGE_SIZE);
7069ed67
MT
241 if (!mem)
242 return;
243 hv_clock = __va(mem);
07868fc6 244 memset(hv_clock, 0, size);
7069ed67 245
0d75de4a 246 if (kvm_register_clock("primary cpu clock")) {
7069ed67 247 hv_clock = NULL;
ed55705d 248 memblock_free(mem, size);
838815a7 249 return;
7069ed67 250 }
838815a7
GC
251 pv_time_ops.sched_clock = kvm_clock_read;
252 x86_platform.calibrate_tsc = kvm_get_tsc_khz;
253 x86_platform.get_wallclock = kvm_get_wallclock;
254 x86_platform.set_wallclock = kvm_set_wallclock;
b8ba5f10 255#ifdef CONFIG_X86_LOCAL_APIC
df156f90 256 x86_cpuinit.early_percpu_clock_init =
838815a7 257 kvm_setup_secondary_clock;
b8ba5f10 258#endif
b74f05d6
MT
259 x86_platform.save_sched_clock_state = kvm_save_sched_clock_state;
260 x86_platform.restore_sched_clock_state = kvm_restore_sched_clock_state;
838815a7 261 machine_ops.shutdown = kvm_shutdown;
2965faa5 262#ifdef CONFIG_KEXEC_CORE
838815a7 263 machine_ops.crash_shutdown = kvm_crash_shutdown;
1e977aa1 264#endif
838815a7 265 kvm_get_preset_lpj();
b01cc1b0 266 clocksource_register_hz(&kvm_clock, NSEC_PER_SEC);
838815a7 267 pv_info.name = "KVM";
3a0d7256
GC
268
269 if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
0ad83caa
LC
270 pvclock_set_flags(~0);
271
272 cpu = get_cpu();
273 vcpu_time = &hv_clock[cpu].pvti;
274 flags = pvclock_read_flags(vcpu_time);
275 if (flags & PVCLOCK_COUNTS_FROM_ZERO)
276 set_sched_clock_stable();
277 put_cpu();
790c73f6 278}
3dc4f7cf
MT
279
280int __init kvm_setup_vsyscall_timeinfo(void)
281{
282#ifdef CONFIG_X86_64
283 int cpu;
284 int ret;
285 u8 flags;
286 struct pvclock_vcpu_time_info *vcpu_time;
287 unsigned int size;
288
fe1140cc
JK
289 if (!hv_clock)
290 return 0;
291
ed55705d 292 size = PAGE_ALIGN(sizeof(struct pvclock_vsyscall_time_info)*NR_CPUS);
3dc4f7cf 293
c6338ce4 294 cpu = get_cpu();
3dc4f7cf
MT
295
296 vcpu_time = &hv_clock[cpu].pvti;
297 flags = pvclock_read_flags(vcpu_time);
298
299 if (!(flags & PVCLOCK_TSC_STABLE_BIT)) {
c6338ce4 300 put_cpu();
3dc4f7cf
MT
301 return 1;
302 }
303
304 if ((ret = pvclock_init_vsyscall(hv_clock, size))) {
c6338ce4 305 put_cpu();
3dc4f7cf
MT
306 return ret;
307 }
308
c6338ce4 309 put_cpu();
3dc4f7cf
MT
310
311 kvm_clock.archdata.vclock_mode = VCLOCK_PVCLOCK;
312#endif
313 return 0;
314}