Blame view

include/clocksource/hyperv_timer.h 2.84 KB
fd1fea683   Michael Kelley   clocksource/drive...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  /* SPDX-License-Identifier: GPL-2.0 */
  
  /*
   * Definitions for the clocksource provided by the Hyper-V
   * hypervisor to guest VMs, as described in the Hyper-V Top
   * Level Functional Spec (TLFS).
   *
   * Copyright (C) 2019, Microsoft, Inc.
   *
   * Author:  Michael Kelley <mikelley@microsoft.com>
   */
  
  #ifndef __CLKSOURCE_HYPERV_TIMER_H
  #define __CLKSOURCE_HYPERV_TIMER_H
dd2cb3486   Michael Kelley   clocksource/drive...
15
16
17
  #include <linux/clocksource.h>
  #include <linux/math64.h>
  #include <asm/mshyperv.h>
fd1fea683   Michael Kelley   clocksource/drive...
18
19
20
21
22
23
24
25
26
27
  #define HV_MAX_MAX_DELTA_TICKS 0xffffffff
  #define HV_MIN_DELTA_TICKS 1
  
  /* Routines called by the VMbus driver */
  extern int hv_stimer_alloc(int sint);
  extern void hv_stimer_free(void);
  extern void hv_stimer_init(unsigned int cpu);
  extern void hv_stimer_cleanup(unsigned int cpu);
  extern void hv_stimer_global_cleanup(void);
  extern void hv_stimer0_isr(void);
3e2d94535   Vitaly Kuznetsov   clocksource/drive...
28
  #ifdef CONFIG_HYPERV_TIMER
dd2cb3486   Michael Kelley   clocksource/drive...
29
30
  extern struct clocksource *hyperv_cs;
  extern void hv_init_clocksource(void);
dd2cb3486   Michael Kelley   clocksource/drive...
31

dd2cb3486   Michael Kelley   clocksource/drive...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  extern struct ms_hyperv_tsc_page *hv_get_tsc_page(void);
  
  static inline notrace u64
  hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg, u64 *cur_tsc)
  {
  	u64 scale, offset;
  	u32 sequence;
  
  	/*
  	 * The protocol for reading Hyper-V TSC page is specified in Hypervisor
  	 * Top-Level Functional Specification ver. 3.0 and above. To get the
  	 * reference time we must do the following:
  	 * - READ ReferenceTscSequence
  	 *   A special '0' value indicates the time source is unreliable and we
  	 *   need to use something else. The currently published specification
  	 *   versions (up to 4.0b) contain a mistake and wrongly claim '-1'
  	 *   instead of '0' as the special value, see commit c35b82ef0294.
  	 * - ReferenceTime =
  	 *        ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset
  	 * - READ ReferenceTscSequence again. In case its value has changed
  	 *   since our first reading we need to discard ReferenceTime and repeat
  	 *   the whole sequence as the hypervisor was updating the page in
  	 *   between.
  	 */
  	do {
  		sequence = READ_ONCE(tsc_pg->tsc_sequence);
  		if (!sequence)
  			return U64_MAX;
  		/*
  		 * Make sure we read sequence before we read other values from
  		 * TSC page.
  		 */
  		smp_rmb();
  
  		scale = READ_ONCE(tsc_pg->tsc_scale);
  		offset = READ_ONCE(tsc_pg->tsc_offset);
  		*cur_tsc = hv_get_raw_timer();
  
  		/*
  		 * Make sure we read sequence after we read all other values
  		 * from TSC page.
  		 */
  		smp_rmb();
  
  	} while (READ_ONCE(tsc_pg->tsc_sequence) != sequence);
  
  	return mul_u64_u64_shr(*cur_tsc, scale, 64) + offset;
  }
  
  static inline notrace u64
  hv_read_tsc_page(const struct ms_hyperv_tsc_page *tsc_pg)
  {
  	u64 cur_tsc;
  
  	return hv_read_tsc_page_tsc(tsc_pg, &cur_tsc);
  }
3e2d94535   Vitaly Kuznetsov   clocksource/drive...
88
  #else /* CONFIG_HYPERV_TIMER */
dd2cb3486   Michael Kelley   clocksource/drive...
89
90
91
92
93
94
95
96
97
98
  static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
  {
  	return NULL;
  }
  
  static inline u64 hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg,
  				       u64 *cur_tsc)
  {
  	return U64_MAX;
  }
3e2d94535   Vitaly Kuznetsov   clocksource/drive...
99
  #endif /* CONFIG_HYPERV_TIMER */
dd2cb3486   Michael Kelley   clocksource/drive...
100

fd1fea683   Michael Kelley   clocksource/drive...
101
  #endif