blob: b6033680d458e1f5db37fe355e7f50ae0bd1f5db [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
H. Peter Anvin1965aae2008-10-22 22:26:29 -07002#ifndef _ASM_X86_PVCLOCK_H
3#define _ASM_X86_PVCLOCK_H
Gerd Hoffmann7af192c2008-06-03 16:17:29 +02004
5#include <linux/clocksource.h>
6#include <asm/pvclock-abi.h>
7
8/* some helper functions for xen and kvm pv clock sources */
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01009u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src);
Marcelo Tosatti26979022012-11-27 23:28:52 -020010u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src);
Glauber Costa424c32f2010-05-11 12:17:39 -040011void pvclock_set_flags(u8 flags);
Glauber Costa3807f342008-07-28 11:47:52 -030012unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src);
Gerd Hoffmann7af192c2008-06-03 16:17:29 +020013void pvclock_read_wallclock(struct pvclock_wall_clock *wall,
14 struct pvclock_vcpu_time_info *vcpu,
Arnd Bergmanne27c4922018-04-27 22:13:23 +020015 struct timespec64 *ts);
Jeremy Fitzhardingee7a3481c2010-10-25 16:53:46 -070016void pvclock_resume(void);
Gerd Hoffmann7af192c2008-06-03 16:17:29 +020017
Marcelo Tosattid63285e2013-10-11 21:39:25 -030018void pvclock_touch_watchdogs(void);
19
Paolo Bonzini3aed64f2016-06-09 13:06:08 +020020static __always_inline
21unsigned pvclock_read_begin(const struct pvclock_vcpu_time_info *src)
22{
23 unsigned version = src->version & ~1;
24 /* Make sure that the version is read before the data. */
25 virt_rmb();
26 return version;
27}
28
29static __always_inline
30bool pvclock_read_retry(const struct pvclock_vcpu_time_info *src,
31 unsigned version)
32{
33 /* Make sure that the version is re-read after the data. */
34 virt_rmb();
35 return unlikely(version != src->version);
36}
37
Zachary Amsden347bb442010-08-19 22:07:29 -100038/*
39 * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
40 * yielding a 64-bit result.
41 */
42static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
43{
44 u64 product;
45#ifdef __i386__
46 u32 tmp1, tmp2;
Zachary Amsdende2d1a52011-06-15 20:50:04 -070047#else
48 ulong tmp;
Zachary Amsden347bb442010-08-19 22:07:29 -100049#endif
50
51 if (shift < 0)
52 delta >>= -shift;
53 else
54 delta <<= shift;
55
56#ifdef __i386__
57 __asm__ (
58 "mul %5 ; "
59 "mov %4,%%eax ; "
60 "mov %%edx,%4 ; "
61 "mul %5 ; "
62 "xor %5,%5 ; "
63 "add %4,%%eax ; "
64 "adc %5,%%edx ; "
65 : "=A" (product), "=r" (tmp1), "=r" (tmp2)
66 : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
67#elif defined(__x86_64__)
68 __asm__ (
Duncan Sands3b217112011-08-30 10:58:22 +020069 "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]"
Zachary Amsdende2d1a52011-06-15 20:50:04 -070070 : [lo]"=a"(product),
71 [hi]"=d"(tmp)
72 : "0"(delta),
73 [mul_frac]"rm"((u64)mul_frac));
Zachary Amsden347bb442010-08-19 22:07:29 -100074#else
75#error implement me!
76#endif
77
78 return product;
79}
80
Marcelo Tosattidce2db02012-11-27 23:28:51 -020081static __always_inline
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010082u64 __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src, u64 tsc)
Marcelo Tosattidce2db02012-11-27 23:28:51 -020083{
Paolo Bonzini108b2492016-09-01 14:21:03 +020084 u64 delta = tsc - src->tsc_timestamp;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010085 u64 offset = pvclock_scale_delta(delta, src->tsc_to_system_mul,
Paolo Bonzini3aed64f2016-06-09 13:06:08 +020086 src->tsc_shift);
87 return src->system_time + offset;
Marcelo Tosattidce2db02012-11-27 23:28:51 -020088}
89
Marcelo Tosatti71056ae2012-11-27 23:28:55 -020090struct pvclock_vsyscall_time_info {
91 struct pvclock_vcpu_time_info pvti;
Marcelo Tosatti71056ae2012-11-27 23:28:55 -020092} __attribute__((__aligned__(SMP_CACHE_BYTES)));
93
94#define PVTI_SIZE sizeof(struct pvclock_vsyscall_time_info)
Marcelo Tosatti71056ae2012-11-27 23:28:55 -020095
Joao Martins9f08890a2017-11-08 17:19:55 +000096#ifdef CONFIG_PARAVIRT_CLOCK
97void pvclock_set_pvti_cpu0_va(struct pvclock_vsyscall_time_info *pvti);
98struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void);
99#else
100static inline struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void)
101{
102 return NULL;
103}
104#endif
105
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700106#endif /* _ASM_X86_PVCLOCK_H */