blob: fdcc04020636e33269d7a756162cfe852f509823 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_PVCLOCK_H
2#define _ASM_X86_PVCLOCK_H
Gerd Hoffmann7af192c2008-06-03 16:17:29 +02003
4#include <linux/clocksource.h>
5#include <asm/pvclock-abi.h>
6
Andy Lutomirski8705d602015-12-29 20:12:18 -08007#ifdef CONFIG_KVM_GUEST
Andy Lutomirskidac16fb2015-12-10 19:20:20 -08008extern struct pvclock_vsyscall_time_info *pvclock_pvti_cpu0_va(void);
9#else
10static inline struct pvclock_vsyscall_time_info *pvclock_pvti_cpu0_va(void)
11{
12 return NULL;
13}
14#endif
15
Gerd Hoffmann7af192c2008-06-03 16:17:29 +020016/* some helper functions for xen and kvm pv clock sources */
17cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src);
Marcelo Tosatti26979022012-11-27 23:28:52 -020018u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src);
Glauber Costa424c32f2010-05-11 12:17:39 -040019void pvclock_set_flags(u8 flags);
Glauber Costa3807f342008-07-28 11:47:52 -030020unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src);
Gerd Hoffmann7af192c2008-06-03 16:17:29 +020021void pvclock_read_wallclock(struct pvclock_wall_clock *wall,
22 struct pvclock_vcpu_time_info *vcpu,
23 struct timespec *ts);
Jeremy Fitzhardingee7a3481c2010-10-25 16:53:46 -070024void pvclock_resume(void);
Gerd Hoffmann7af192c2008-06-03 16:17:29 +020025
Marcelo Tosattid63285e2013-10-11 21:39:25 -030026void pvclock_touch_watchdogs(void);
27
Zachary Amsden347bb442010-08-19 22:07:29 -100028/*
29 * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
30 * yielding a 64-bit result.
31 */
32static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
33{
34 u64 product;
35#ifdef __i386__
36 u32 tmp1, tmp2;
Zachary Amsdende2d1a52011-06-15 20:50:04 -070037#else
38 ulong tmp;
Zachary Amsden347bb442010-08-19 22:07:29 -100039#endif
40
41 if (shift < 0)
42 delta >>= -shift;
43 else
44 delta <<= shift;
45
46#ifdef __i386__
47 __asm__ (
48 "mul %5 ; "
49 "mov %4,%%eax ; "
50 "mov %%edx,%4 ; "
51 "mul %5 ; "
52 "xor %5,%5 ; "
53 "add %4,%%eax ; "
54 "adc %5,%%edx ; "
55 : "=A" (product), "=r" (tmp1), "=r" (tmp2)
56 : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
57#elif defined(__x86_64__)
58 __asm__ (
Duncan Sands3b217112011-08-30 10:58:22 +020059 "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]"
Zachary Amsdende2d1a52011-06-15 20:50:04 -070060 : [lo]"=a"(product),
61 [hi]"=d"(tmp)
62 : "0"(delta),
63 [mul_frac]"rm"((u64)mul_frac));
Zachary Amsden347bb442010-08-19 22:07:29 -100064#else
65#error implement me!
66#endif
67
68 return product;
69}
70
Marcelo Tosattidce2db02012-11-27 23:28:51 -020071static __always_inline
72u64 pvclock_get_nsec_offset(const struct pvclock_vcpu_time_info *src)
73{
Andy Lutomirski502dfef2015-06-25 18:44:11 +020074 u64 delta = rdtsc_ordered() - src->tsc_timestamp;
Marcelo Tosattidce2db02012-11-27 23:28:51 -020075 return pvclock_scale_delta(delta, src->tsc_to_system_mul,
76 src->tsc_shift);
77}
78
79static __always_inline
80unsigned __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src,
81 cycle_t *cycles, u8 *flags)
82{
83 unsigned version;
84 cycle_t ret, offset;
85 u8 ret_flags;
86
87 version = src->version;
Andy Lutomirski502dfef2015-06-25 18:44:11 +020088
Marcelo Tosattidce2db02012-11-27 23:28:51 -020089 offset = pvclock_get_nsec_offset(src);
90 ret = src->system_time + offset;
91 ret_flags = src->flags;
Marcelo Tosattidce2db02012-11-27 23:28:51 -020092
93 *cycles = ret;
94 *flags = ret_flags;
95 return version;
96}
97
Marcelo Tosatti71056ae2012-11-27 23:28:55 -020098struct pvclock_vsyscall_time_info {
99 struct pvclock_vcpu_time_info pvti;
Marcelo Tosatti71056ae2012-11-27 23:28:55 -0200100} __attribute__((__aligned__(SMP_CACHE_BYTES)));
101
102#define PVTI_SIZE sizeof(struct pvclock_vsyscall_time_info)
Marcelo Tosatti71056ae2012-11-27 23:28:55 -0200103
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700104#endif /* _ASM_X86_PVCLOCK_H */