diff options
Diffstat (limited to 'arch/x86/xen')
-rw-r--r-- | arch/x86/xen/Kconfig | 1 | ||||
-rw-r--r-- | arch/x86/xen/time.c | 132 |
2 files changed, 13 insertions, 120 deletions
diff --git a/arch/x86/xen/Kconfig b/arch/x86/xen/Kconfig index d0f1c7c5dc3d..c2cc99580871 100644 --- a/arch/x86/xen/Kconfig +++ b/arch/x86/xen/Kconfig | |||
@@ -5,6 +5,7 @@ | |||
5 | config XEN | 5 | config XEN |
6 | bool "Xen guest support" | 6 | bool "Xen guest support" |
7 | select PARAVIRT | 7 | select PARAVIRT |
8 | select PARAVIRT_CLOCK | ||
8 | depends on X86_32 | 9 | depends on X86_32 |
9 | depends on X86_CMPXCHG && X86_TSC && X86_PAE && !(X86_VISWS || X86_VOYAGER) | 10 | depends on X86_CMPXCHG && X86_TSC && X86_PAE && !(X86_VISWS || X86_VOYAGER) |
10 | help | 11 | help |
diff --git a/arch/x86/xen/time.c b/arch/x86/xen/time.c index 0da249dbdc49..64f0038b9558 100644 --- a/arch/x86/xen/time.c +++ b/arch/x86/xen/time.c | |||
@@ -14,6 +14,7 @@ | |||
14 | #include <linux/kernel_stat.h> | 14 | #include <linux/kernel_stat.h> |
15 | #include <linux/math64.h> | 15 | #include <linux/math64.h> |
16 | 16 | ||
17 | #include <asm/pvclock.h> | ||
17 | #include <asm/xen/hypervisor.h> | 18 | #include <asm/xen/hypervisor.h> |
18 | #include <asm/xen/hypercall.h> | 19 | #include <asm/xen/hypercall.h> |
19 | 20 | ||
@@ -31,17 +32,6 @@ | |||
31 | 32 | ||
32 | static cycle_t xen_clocksource_read(void); | 33 | static cycle_t xen_clocksource_read(void); |
33 | 34 | ||
34 | /* These are perodically updated in shared_info, and then copied here. */ | ||
35 | struct shadow_time_info { | ||
36 | u64 tsc_timestamp; /* TSC at last update of time vals. */ | ||
37 | u64 system_timestamp; /* Time, in nanosecs, since boot. */ | ||
38 | u32 tsc_to_nsec_mul; | ||
39 | int tsc_shift; | ||
40 | u32 version; | ||
41 | }; | ||
42 | |||
43 | static DEFINE_PER_CPU(struct shadow_time_info, shadow_time); | ||
44 | |||
45 | /* runstate info updated by Xen */ | 35 | /* runstate info updated by Xen */ |
46 | static DEFINE_PER_CPU(struct vcpu_runstate_info, runstate); | 36 | static DEFINE_PER_CPU(struct vcpu_runstate_info, runstate); |
47 | 37 | ||
@@ -211,7 +201,7 @@ unsigned long long xen_sched_clock(void) | |||
211 | unsigned long xen_cpu_khz(void) | 201 | unsigned long xen_cpu_khz(void) |
212 | { | 202 | { |
213 | u64 xen_khz = 1000000ULL << 32; | 203 | u64 xen_khz = 1000000ULL << 32; |
214 | const struct vcpu_time_info *info = | 204 | const struct pvclock_vcpu_time_info *info = |
215 | &HYPERVISOR_shared_info->vcpu_info[0].time; | 205 | &HYPERVISOR_shared_info->vcpu_info[0].time; |
216 | 206 | ||
217 | do_div(xen_khz, info->tsc_to_system_mul); | 207 | do_div(xen_khz, info->tsc_to_system_mul); |
@@ -223,121 +213,26 @@ unsigned long xen_cpu_khz(void) | |||
223 | return xen_khz; | 213 | return xen_khz; |
224 | } | 214 | } |
225 | 215 | ||
226 | /* | ||
227 | * Reads a consistent set of time-base values from Xen, into a shadow data | ||
228 | * area. | ||
229 | */ | ||
230 | static unsigned get_time_values_from_xen(void) | ||
231 | { | ||
232 | struct vcpu_time_info *src; | ||
233 | struct shadow_time_info *dst; | ||
234 | |||
235 | /* src is shared memory with the hypervisor, so we need to | ||
236 | make sure we get a consistent snapshot, even in the face of | ||
237 | being preempted. */ | ||
238 | src = &__get_cpu_var(xen_vcpu)->time; | ||
239 | dst = &__get_cpu_var(shadow_time); | ||
240 | |||
241 | do { | ||
242 | dst->version = src->version; | ||
243 | rmb(); /* fetch version before data */ | ||
244 | dst->tsc_timestamp = src->tsc_timestamp; | ||
245 | dst->system_timestamp = src->system_time; | ||
246 | dst->tsc_to_nsec_mul = src->tsc_to_system_mul; | ||
247 | dst->tsc_shift = src->tsc_shift; | ||
248 | rmb(); /* test version after fetching data */ | ||
249 | } while ((src->version & 1) | (dst->version ^ src->version)); | ||
250 | |||
251 | return dst->version; | ||
252 | } | ||
253 | |||
254 | /* | ||
255 | * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction, | ||
256 | * yielding a 64-bit result. | ||
257 | */ | ||
258 | static inline u64 scale_delta(u64 delta, u32 mul_frac, int shift) | ||
259 | { | ||
260 | u64 product; | ||
261 | #ifdef __i386__ | ||
262 | u32 tmp1, tmp2; | ||
263 | #endif | ||
264 | |||
265 | if (shift < 0) | ||
266 | delta >>= -shift; | ||
267 | else | ||
268 | delta <<= shift; | ||
269 | |||
270 | #ifdef __i386__ | ||
271 | __asm__ ( | ||
272 | "mul %5 ; " | ||
273 | "mov %4,%%eax ; " | ||
274 | "mov %%edx,%4 ; " | ||
275 | "mul %5 ; " | ||
276 | "xor %5,%5 ; " | ||
277 | "add %4,%%eax ; " | ||
278 | "adc %5,%%edx ; " | ||
279 | : "=A" (product), "=r" (tmp1), "=r" (tmp2) | ||
280 | : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) ); | ||
281 | #elif __x86_64__ | ||
282 | __asm__ ( | ||
283 | "mul %%rdx ; shrd $32,%%rdx,%%rax" | ||
284 | : "=a" (product) : "0" (delta), "d" ((u64)mul_frac) ); | ||
285 | #else | ||
286 | #error implement me! | ||
287 | #endif | ||
288 | |||
289 | return product; | ||
290 | } | ||
291 | |||
292 | static u64 get_nsec_offset(struct shadow_time_info *shadow) | ||
293 | { | ||
294 | u64 now, delta; | ||
295 | now = native_read_tsc(); | ||
296 | delta = now - shadow->tsc_timestamp; | ||
297 | return scale_delta(delta, shadow->tsc_to_nsec_mul, shadow->tsc_shift); | ||
298 | } | ||
299 | |||
300 | static cycle_t xen_clocksource_read(void) | 216 | static cycle_t xen_clocksource_read(void) |
301 | { | 217 | { |
302 | struct shadow_time_info *shadow = &get_cpu_var(shadow_time); | 218 | struct pvclock_vcpu_time_info *src; |
303 | cycle_t ret; | 219 | cycle_t ret; |
304 | unsigned version; | ||
305 | |||
306 | do { | ||
307 | version = get_time_values_from_xen(); | ||
308 | barrier(); | ||
309 | ret = shadow->system_timestamp + get_nsec_offset(shadow); | ||
310 | barrier(); | ||
311 | } while (version != __get_cpu_var(xen_vcpu)->time.version); | ||
312 | |||
313 | put_cpu_var(shadow_time); | ||
314 | 220 | ||
221 | src = &get_cpu_var(xen_vcpu)->time; | ||
222 | ret = pvclock_clocksource_read(src); | ||
223 | put_cpu_var(xen_vcpu); | ||
315 | return ret; | 224 | return ret; |
316 | } | 225 | } |
317 | 226 | ||
318 | static void xen_read_wallclock(struct timespec *ts) | 227 | static void xen_read_wallclock(struct timespec *ts) |
319 | { | 228 | { |
320 | const struct shared_info *s = HYPERVISOR_shared_info; | 229 | struct shared_info *s = HYPERVISOR_shared_info; |
321 | u32 version; | 230 | struct pvclock_wall_clock *wall_clock = &(s->wc); |
322 | u64 delta; | 231 | struct pvclock_vcpu_time_info *vcpu_time; |
323 | struct timespec now; | ||
324 | |||
325 | /* get wallclock at system boot */ | ||
326 | do { | ||
327 | version = s->wc_version; | ||
328 | rmb(); /* fetch version before time */ | ||
329 | now.tv_sec = s->wc_sec; | ||
330 | now.tv_nsec = s->wc_nsec; | ||
331 | rmb(); /* fetch time before checking version */ | ||
332 | } while ((s->wc_version & 1) | (version ^ s->wc_version)); | ||
333 | 232 | ||
334 | delta = xen_clocksource_read(); /* time since system boot */ | 233 | vcpu_time = &get_cpu_var(xen_vcpu)->time; |
335 | delta += now.tv_sec * (u64)NSEC_PER_SEC + now.tv_nsec; | 234 | pvclock_read_wallclock(wall_clock, vcpu_time, ts); |
336 | 235 | put_cpu_var(xen_vcpu); | |
337 | now.tv_nsec = do_div(delta, NSEC_PER_SEC); | ||
338 | now.tv_sec = delta; | ||
339 | |||
340 | set_normalized_timespec(ts, now.tv_sec, now.tv_nsec); | ||
341 | } | 236 | } |
342 | 237 | ||
343 | unsigned long xen_get_wallclock(void) | 238 | unsigned long xen_get_wallclock(void) |
@@ -345,7 +240,6 @@ unsigned long xen_get_wallclock(void) | |||
345 | struct timespec ts; | 240 | struct timespec ts; |
346 | 241 | ||
347 | xen_read_wallclock(&ts); | 242 | xen_read_wallclock(&ts); |
348 | |||
349 | return ts.tv_sec; | 243 | return ts.tv_sec; |
350 | } | 244 | } |
351 | 245 | ||
@@ -582,8 +476,6 @@ __init void xen_time_init(void) | |||
582 | { | 476 | { |
583 | int cpu = smp_processor_id(); | 477 | int cpu = smp_processor_id(); |
584 | 478 | ||
585 | get_time_values_from_xen(); | ||
586 | |||
587 | clocksource_register(&xen_clocksource); | 479 | clocksource_register(&xen_clocksource); |
588 | 480 | ||
589 | if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL) == 0) { | 481 | if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL) == 0) { |