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>
18dd2cb348SMichael Kelley #include <asm/mshyperv.h>
19dd2cb348SMichael Kelley 
20fd1fea68SMichael Kelley #define HV_MAX_MAX_DELTA_TICKS 0xffffffff
21fd1fea68SMichael Kelley #define HV_MIN_DELTA_TICKS 1
22fd1fea68SMichael Kelley 
23fd1fea68SMichael Kelley /* Routines called by the VMbus driver */
244df4cb9eSMichael Kelley extern int hv_stimer_alloc(void);
25fd1fea68SMichael Kelley extern void hv_stimer_free(void);
264df4cb9eSMichael Kelley extern int hv_stimer_cleanup(unsigned int cpu);
274df4cb9eSMichael Kelley extern void hv_stimer_legacy_init(unsigned int cpu, int sint);
284df4cb9eSMichael Kelley extern void hv_stimer_legacy_cleanup(unsigned int cpu);
29fd1fea68SMichael Kelley extern void hv_stimer_global_cleanup(void);
30fd1fea68SMichael Kelley extern void hv_stimer0_isr(void);
31fd1fea68SMichael Kelley 
323e2d9453SVitaly Kuznetsov #ifdef CONFIG_HYPERV_TIMER
33*0af3e137SAndrea Parri extern u64 (*hv_read_reference_counter)(void);
34dd2cb348SMichael Kelley extern void hv_init_clocksource(void);
35dd2cb348SMichael Kelley 
36dd2cb348SMichael Kelley extern struct ms_hyperv_tsc_page *hv_get_tsc_page(void);
37dd2cb348SMichael Kelley 
38dd2cb348SMichael Kelley static inline notrace u64
39dd2cb348SMichael Kelley hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg, u64 *cur_tsc)
40dd2cb348SMichael Kelley {
41dd2cb348SMichael Kelley 	u64 scale, offset;
42dd2cb348SMichael Kelley 	u32 sequence;
43dd2cb348SMichael Kelley 
44dd2cb348SMichael Kelley 	/*
45dd2cb348SMichael Kelley 	 * The protocol for reading Hyper-V TSC page is specified in Hypervisor
46dd2cb348SMichael Kelley 	 * Top-Level Functional Specification ver. 3.0 and above. To get the
47dd2cb348SMichael Kelley 	 * reference time we must do the following:
48dd2cb348SMichael Kelley 	 * - READ ReferenceTscSequence
49dd2cb348SMichael Kelley 	 *   A special '0' value indicates the time source is unreliable and we
50dd2cb348SMichael Kelley 	 *   need to use something else. The currently published specification
51dd2cb348SMichael Kelley 	 *   versions (up to 4.0b) contain a mistake and wrongly claim '-1'
52dd2cb348SMichael Kelley 	 *   instead of '0' as the special value, see commit c35b82ef0294.
53dd2cb348SMichael Kelley 	 * - ReferenceTime =
54dd2cb348SMichael Kelley 	 *        ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset
55dd2cb348SMichael Kelley 	 * - READ ReferenceTscSequence again. In case its value has changed
56dd2cb348SMichael Kelley 	 *   since our first reading we need to discard ReferenceTime and repeat
57dd2cb348SMichael Kelley 	 *   the whole sequence as the hypervisor was updating the page in
58dd2cb348SMichael Kelley 	 *   between.
59dd2cb348SMichael Kelley 	 */
60dd2cb348SMichael Kelley 	do {
61dd2cb348SMichael Kelley 		sequence = READ_ONCE(tsc_pg->tsc_sequence);
62dd2cb348SMichael Kelley 		if (!sequence)
63dd2cb348SMichael Kelley 			return U64_MAX;
64dd2cb348SMichael Kelley 		/*
65dd2cb348SMichael Kelley 		 * Make sure we read sequence before we read other values from
66dd2cb348SMichael Kelley 		 * TSC page.
67dd2cb348SMichael Kelley 		 */
68dd2cb348SMichael Kelley 		smp_rmb();
69dd2cb348SMichael Kelley 
70dd2cb348SMichael Kelley 		scale = READ_ONCE(tsc_pg->tsc_scale);
71dd2cb348SMichael Kelley 		offset = READ_ONCE(tsc_pg->tsc_offset);
72dd2cb348SMichael Kelley 		*cur_tsc = hv_get_raw_timer();
73dd2cb348SMichael Kelley 
74dd2cb348SMichael Kelley 		/*
75dd2cb348SMichael Kelley 		 * Make sure we read sequence after we read all other values
76dd2cb348SMichael Kelley 		 * from TSC page.
77dd2cb348SMichael Kelley 		 */
78dd2cb348SMichael Kelley 		smp_rmb();
79dd2cb348SMichael Kelley 
80dd2cb348SMichael Kelley 	} while (READ_ONCE(tsc_pg->tsc_sequence) != sequence);
81dd2cb348SMichael Kelley 
82dd2cb348SMichael Kelley 	return mul_u64_u64_shr(*cur_tsc, scale, 64) + offset;
83dd2cb348SMichael Kelley }
84dd2cb348SMichael Kelley 
85dd2cb348SMichael Kelley static inline notrace u64
86dd2cb348SMichael Kelley hv_read_tsc_page(const struct ms_hyperv_tsc_page *tsc_pg)
87dd2cb348SMichael Kelley {
88dd2cb348SMichael Kelley 	u64 cur_tsc;
89dd2cb348SMichael Kelley 
90dd2cb348SMichael Kelley 	return hv_read_tsc_page_tsc(tsc_pg, &cur_tsc);
91dd2cb348SMichael Kelley }
92dd2cb348SMichael Kelley 
933e2d9453SVitaly Kuznetsov #else /* CONFIG_HYPERV_TIMER */
94dd2cb348SMichael Kelley static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
95dd2cb348SMichael Kelley {
96dd2cb348SMichael Kelley 	return NULL;
97dd2cb348SMichael Kelley }
98dd2cb348SMichael Kelley 
99dd2cb348SMichael Kelley static inline u64 hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg,
100dd2cb348SMichael Kelley 				       u64 *cur_tsc)
101dd2cb348SMichael Kelley {
102dd2cb348SMichael Kelley 	return U64_MAX;
103dd2cb348SMichael Kelley }
1043e2d9453SVitaly Kuznetsov #endif /* CONFIG_HYPERV_TIMER */
105dd2cb348SMichael Kelley 
106fd1fea68SMichael Kelley #endif
107