1fd1fea68SMichael Kelley /* SPDX-License-Identifier: GPL-2.0 */
2fd1fea68SMichael Kelley 
3fd1fea68SMichael Kelley /*
4fd1fea68SMichael Kelley  * Definitions for the clocksource provided by the Hyper-V
5fd1fea68SMichael Kelley  * hypervisor to guest VMs, as described in the Hyper-V Top
6fd1fea68SMichael Kelley  * Level Functional Spec (TLFS).
7fd1fea68SMichael Kelley  *
8fd1fea68SMichael Kelley  * Copyright (C) 2019, Microsoft, Inc.
9fd1fea68SMichael Kelley  *
10fd1fea68SMichael Kelley  * Author:  Michael Kelley <[email protected]>
11fd1fea68SMichael Kelley  */
12fd1fea68SMichael Kelley 
13fd1fea68SMichael Kelley #ifndef __CLKSOURCE_HYPERV_TIMER_H
14fd1fea68SMichael Kelley #define __CLKSOURCE_HYPERV_TIMER_H
15fd1fea68SMichael Kelley 
16dd2cb348SMichael Kelley #include <linux/clocksource.h>
17dd2cb348SMichael Kelley #include <linux/math64.h>
18e5dfd093SThomas Gleixner #include <asm/hyperv-tlfs.h>
19dd2cb348SMichael Kelley 
20fd1fea68SMichael Kelley #define HV_MAX_MAX_DELTA_TICKS 0xffffffff
21fd1fea68SMichael Kelley #define HV_MIN_DELTA_TICKS 1
22fd1fea68SMichael Kelley 
2331e5e646SMichael Kelley #ifdef CONFIG_HYPERV_TIMER
2431e5e646SMichael Kelley 
25e5dfd093SThomas Gleixner #include <asm/hyperv_timer.h>
26e5dfd093SThomas Gleixner 
27fd1fea68SMichael Kelley /* Routines called by the VMbus driver */
28ec866be6SMichael Kelley extern int hv_stimer_alloc(bool have_percpu_irqs);
294df4cb9eSMichael Kelley extern int hv_stimer_cleanup(unsigned int cpu);
304df4cb9eSMichael Kelley extern void hv_stimer_legacy_init(unsigned int cpu, int sint);
314df4cb9eSMichael Kelley extern void hv_stimer_legacy_cleanup(unsigned int cpu);
32fd1fea68SMichael Kelley extern void hv_stimer_global_cleanup(void);
33fd1fea68SMichael Kelley extern void hv_stimer0_isr(void);
34fd1fea68SMichael Kelley 
35dd2cb348SMichael Kelley extern void hv_init_clocksource(void);
360408f16bSStanislav Kinsburskiy extern void hv_remap_tsc_clocksource(void);
37dd2cb348SMichael Kelley 
38364adc45SStanislav Kinsburskiy extern unsigned long hv_get_tsc_pfn(void);
39dd2cb348SMichael Kelley extern struct ms_hyperv_tsc_page *hv_get_tsc_page(void);
40dd2cb348SMichael Kelley 
41*9397fa2eSPeter Zijlstra static __always_inline bool
42*9397fa2eSPeter Zijlstra hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg,
43*9397fa2eSPeter Zijlstra 		     u64 *cur_tsc, u64 *time)
44dd2cb348SMichael Kelley {
45dd2cb348SMichael Kelley 	u64 scale, offset;
46dd2cb348SMichael Kelley 	u32 sequence;
47dd2cb348SMichael Kelley 
48dd2cb348SMichael Kelley 	/*
49dd2cb348SMichael Kelley 	 * The protocol for reading Hyper-V TSC page is specified in Hypervisor
50dd2cb348SMichael Kelley 	 * Top-Level Functional Specification ver. 3.0 and above. To get the
51dd2cb348SMichael Kelley 	 * reference time we must do the following:
52dd2cb348SMichael Kelley 	 * - READ ReferenceTscSequence
53dd2cb348SMichael Kelley 	 *   A special '0' value indicates the time source is unreliable and we
54dd2cb348SMichael Kelley 	 *   need to use something else. The currently published specification
55dd2cb348SMichael Kelley 	 *   versions (up to 4.0b) contain a mistake and wrongly claim '-1'
56dd2cb348SMichael Kelley 	 *   instead of '0' as the special value, see commit c35b82ef0294.
57dd2cb348SMichael Kelley 	 * - ReferenceTime =
58dd2cb348SMichael Kelley 	 *        ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset
59dd2cb348SMichael Kelley 	 * - READ ReferenceTscSequence again. In case its value has changed
60dd2cb348SMichael Kelley 	 *   since our first reading we need to discard ReferenceTime and repeat
61dd2cb348SMichael Kelley 	 *   the whole sequence as the hypervisor was updating the page in
62dd2cb348SMichael Kelley 	 *   between.
63dd2cb348SMichael Kelley 	 */
64dd2cb348SMichael Kelley 	do {
65dd2cb348SMichael Kelley 		sequence = READ_ONCE(tsc_pg->tsc_sequence);
66dd2cb348SMichael Kelley 		if (!sequence)
67*9397fa2eSPeter Zijlstra 			return false;
68dd2cb348SMichael Kelley 		/*
69dd2cb348SMichael Kelley 		 * Make sure we read sequence before we read other values from
70dd2cb348SMichael Kelley 		 * TSC page.
71dd2cb348SMichael Kelley 		 */
72dd2cb348SMichael Kelley 		smp_rmb();
73dd2cb348SMichael Kelley 
74dd2cb348SMichael Kelley 		scale = READ_ONCE(tsc_pg->tsc_scale);
75dd2cb348SMichael Kelley 		offset = READ_ONCE(tsc_pg->tsc_offset);
76dd2cb348SMichael Kelley 		*cur_tsc = hv_get_raw_timer();
77dd2cb348SMichael Kelley 
78dd2cb348SMichael Kelley 		/*
79dd2cb348SMichael Kelley 		 * Make sure we read sequence after we read all other values
80dd2cb348SMichael Kelley 		 * from TSC page.
81dd2cb348SMichael Kelley 		 */
82dd2cb348SMichael Kelley 		smp_rmb();
83dd2cb348SMichael Kelley 
84dd2cb348SMichael Kelley 	} while (READ_ONCE(tsc_pg->tsc_sequence) != sequence);
85dd2cb348SMichael Kelley 
86*9397fa2eSPeter Zijlstra 	*time = mul_u64_u64_shr(*cur_tsc, scale, 64) + offset;
87*9397fa2eSPeter Zijlstra 	return true;
88dd2cb348SMichael Kelley }
89dd2cb348SMichael Kelley 
903e2d9453SVitaly Kuznetsov #else /* CONFIG_HYPERV_TIMER */
91364adc45SStanislav Kinsburskiy static inline unsigned long hv_get_tsc_pfn(void)
92364adc45SStanislav Kinsburskiy {
93364adc45SStanislav Kinsburskiy 	return 0;
94364adc45SStanislav Kinsburskiy }
95364adc45SStanislav Kinsburskiy 
96dd2cb348SMichael Kelley static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
97dd2cb348SMichael Kelley {
98dd2cb348SMichael Kelley 	return NULL;
99dd2cb348SMichael Kelley }
100dd2cb348SMichael Kelley 
101*9397fa2eSPeter Zijlstra static __always_inline bool
102*9397fa2eSPeter Zijlstra hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg, u64 *cur_tsc, u64 *time)
103dd2cb348SMichael Kelley {
104*9397fa2eSPeter Zijlstra 	return false;
105dd2cb348SMichael Kelley }
10631e5e646SMichael Kelley 
10731e5e646SMichael Kelley static inline int hv_stimer_cleanup(unsigned int cpu) { return 0; }
10831e5e646SMichael Kelley static inline void hv_stimer_legacy_init(unsigned int cpu, int sint) {}
10931e5e646SMichael Kelley static inline void hv_stimer_legacy_cleanup(unsigned int cpu) {}
11031e5e646SMichael Kelley static inline void hv_stimer_global_cleanup(void) {}
11131e5e646SMichael Kelley static inline void hv_stimer0_isr(void) {}
11231e5e646SMichael Kelley 
1133e2d9453SVitaly Kuznetsov #endif /* CONFIG_HYPERV_TIMER */
114dd2cb348SMichael Kelley 
115fd1fea68SMichael Kelley #endif
116