blob: 109a9dd5d454f197414e2449617cf9cb73ab1220 [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
7/* some helper functions for xen and kvm pv clock sources */
8cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src);
Marcelo Tosatti26979022012-11-27 23:28:52 -02009u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src);
Glauber Costa424c32f2010-05-11 12:17:39 -040010void pvclock_set_flags(u8 flags);
Glauber Costa3807f342008-07-28 11:47:52 -030011unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src);
Gerd Hoffmann7af192c2008-06-03 16:17:29 +020012void pvclock_read_wallclock(struct pvclock_wall_clock *wall,
13 struct pvclock_vcpu_time_info *vcpu,
14 struct timespec *ts);
Jeremy Fitzhardingee7a3481c2010-10-25 16:53:46 -070015void pvclock_resume(void);
Gerd Hoffmann7af192c2008-06-03 16:17:29 +020016
Zachary Amsden347bb442010-08-19 22:07:29 -100017/*
18 * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
19 * yielding a 64-bit result.
20 */
21static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
22{
23 u64 product;
24#ifdef __i386__
25 u32 tmp1, tmp2;
Zachary Amsdende2d1a52011-06-15 20:50:04 -070026#else
27 ulong tmp;
Zachary Amsden347bb442010-08-19 22:07:29 -100028#endif
29
30 if (shift < 0)
31 delta >>= -shift;
32 else
33 delta <<= shift;
34
35#ifdef __i386__
36 __asm__ (
37 "mul %5 ; "
38 "mov %4,%%eax ; "
39 "mov %%edx,%4 ; "
40 "mul %5 ; "
41 "xor %5,%5 ; "
42 "add %4,%%eax ; "
43 "adc %5,%%edx ; "
44 : "=A" (product), "=r" (tmp1), "=r" (tmp2)
45 : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
46#elif defined(__x86_64__)
47 __asm__ (
Duncan Sands3b217112011-08-30 10:58:22 +020048 "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]"
Zachary Amsdende2d1a52011-06-15 20:50:04 -070049 : [lo]"=a"(product),
50 [hi]"=d"(tmp)
51 : "0"(delta),
52 [mul_frac]"rm"((u64)mul_frac));
Zachary Amsden347bb442010-08-19 22:07:29 -100053#else
54#error implement me!
55#endif
56
57 return product;
58}
59
Marcelo Tosattidce2db02012-11-27 23:28:51 -020060static __always_inline
61u64 pvclock_get_nsec_offset(const struct pvclock_vcpu_time_info *src)
62{
63 u64 delta = __native_read_tsc() - src->tsc_timestamp;
64 return pvclock_scale_delta(delta, src->tsc_to_system_mul,
65 src->tsc_shift);
66}
67
68static __always_inline
69unsigned __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src,
70 cycle_t *cycles, u8 *flags)
71{
72 unsigned version;
73 cycle_t ret, offset;
74 u8 ret_flags;
75
76 version = src->version;
Marcelo Tosatti189e1172012-11-27 23:28:53 -020077 /* Note: emulated platforms which do not advertise SSE2 support
78 * result in kvmclock not using the necessary RDTSC barriers.
79 * Without barriers, it is possible that RDTSC instruction reads from
80 * the time stamp counter outside rdtsc_barrier protected section
81 * below, resulting in violation of monotonicity.
82 */
Marcelo Tosattidce2db02012-11-27 23:28:51 -020083 rdtsc_barrier();
84 offset = pvclock_get_nsec_offset(src);
85 ret = src->system_time + offset;
86 ret_flags = src->flags;
87 rdtsc_barrier();
88
89 *cycles = ret;
90 *flags = ret_flags;
91 return version;
92}
93
Marcelo Tosatti71056ae2012-11-27 23:28:55 -020094struct pvclock_vsyscall_time_info {
95 struct pvclock_vcpu_time_info pvti;
96 u32 migrate_count;
97} __attribute__((__aligned__(SMP_CACHE_BYTES)));
98
99#define PVTI_SIZE sizeof(struct pvclock_vsyscall_time_info)
100#define PVCLOCK_VSYSCALL_NR_PAGES (((NR_CPUS-1)/(PAGE_SIZE/PVTI_SIZE))+1)
101
102int __init pvclock_init_vsyscall(struct pvclock_vsyscall_time_info *i,
103 int size);
104struct pvclock_vcpu_time_info *pvclock_get_vsyscall_time_info(int cpu);
105
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700106#endif /* _ASM_X86_PVCLOCK_H */