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>
18*ef5a3c92SNuno Das Neves #include <hyperv/hvhdk.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 
41bcc80decSNaman Jain extern void hv_adj_sched_clock_offset(u64 offset);
42bcc80decSNaman Jain 
439397fa2eSPeter Zijlstra static __always_inline bool
hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page * tsc_pg,u64 * cur_tsc,u64 * time)449397fa2eSPeter Zijlstra hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg,
459397fa2eSPeter Zijlstra 		     u64 *cur_tsc, u64 *time)
46dd2cb348SMichael Kelley {
47dd2cb348SMichael Kelley 	u64 scale, offset;
48dd2cb348SMichael Kelley 	u32 sequence;
49dd2cb348SMichael Kelley 
50dd2cb348SMichael Kelley 	/*
51dd2cb348SMichael Kelley 	 * The protocol for reading Hyper-V TSC page is specified in Hypervisor
52dd2cb348SMichael Kelley 	 * Top-Level Functional Specification ver. 3.0 and above. To get the
53dd2cb348SMichael Kelley 	 * reference time we must do the following:
54dd2cb348SMichael Kelley 	 * - READ ReferenceTscSequence
55dd2cb348SMichael Kelley 	 *   A special '0' value indicates the time source is unreliable and we
56dd2cb348SMichael Kelley 	 *   need to use something else. The currently published specification
57dd2cb348SMichael Kelley 	 *   versions (up to 4.0b) contain a mistake and wrongly claim '-1'
58dd2cb348SMichael Kelley 	 *   instead of '0' as the special value, see commit c35b82ef0294.
59dd2cb348SMichael Kelley 	 * - ReferenceTime =
60dd2cb348SMichael Kelley 	 *        ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset
61dd2cb348SMichael Kelley 	 * - READ ReferenceTscSequence again. In case its value has changed
62dd2cb348SMichael Kelley 	 *   since our first reading we need to discard ReferenceTime and repeat
63dd2cb348SMichael Kelley 	 *   the whole sequence as the hypervisor was updating the page in
64dd2cb348SMichael Kelley 	 *   between.
65dd2cb348SMichael Kelley 	 */
66dd2cb348SMichael Kelley 	do {
67dd2cb348SMichael Kelley 		sequence = READ_ONCE(tsc_pg->tsc_sequence);
68dd2cb348SMichael Kelley 		if (!sequence)
699397fa2eSPeter Zijlstra 			return false;
70dd2cb348SMichael Kelley 		/*
71dd2cb348SMichael Kelley 		 * Make sure we read sequence before we read other values from
72dd2cb348SMichael Kelley 		 * TSC page.
73dd2cb348SMichael Kelley 		 */
74dd2cb348SMichael Kelley 		smp_rmb();
75dd2cb348SMichael Kelley 
76dd2cb348SMichael Kelley 		scale = READ_ONCE(tsc_pg->tsc_scale);
77dd2cb348SMichael Kelley 		offset = READ_ONCE(tsc_pg->tsc_offset);
78dd2cb348SMichael Kelley 		*cur_tsc = hv_get_raw_timer();
79dd2cb348SMichael Kelley 
80dd2cb348SMichael Kelley 		/*
81dd2cb348SMichael Kelley 		 * Make sure we read sequence after we read all other values
82dd2cb348SMichael Kelley 		 * from TSC page.
83dd2cb348SMichael Kelley 		 */
84dd2cb348SMichael Kelley 		smp_rmb();
85dd2cb348SMichael Kelley 
86dd2cb348SMichael Kelley 	} while (READ_ONCE(tsc_pg->tsc_sequence) != sequence);
87dd2cb348SMichael Kelley 
889397fa2eSPeter Zijlstra 	*time = mul_u64_u64_shr(*cur_tsc, scale, 64) + offset;
899397fa2eSPeter Zijlstra 	return true;
90dd2cb348SMichael Kelley }
91dd2cb348SMichael Kelley 
923e2d9453SVitaly Kuznetsov #else /* CONFIG_HYPERV_TIMER */
hv_get_tsc_pfn(void)93364adc45SStanislav Kinsburskiy static inline unsigned long hv_get_tsc_pfn(void)
94364adc45SStanislav Kinsburskiy {
95364adc45SStanislav Kinsburskiy 	return 0;
96364adc45SStanislav Kinsburskiy }
97364adc45SStanislav Kinsburskiy 
hv_get_tsc_page(void)98dd2cb348SMichael Kelley static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
99dd2cb348SMichael Kelley {
100dd2cb348SMichael Kelley 	return NULL;
101dd2cb348SMichael Kelley }
102dd2cb348SMichael Kelley 
1039397fa2eSPeter Zijlstra static __always_inline bool
hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page * tsc_pg,u64 * cur_tsc,u64 * time)1049397fa2eSPeter Zijlstra hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg, u64 *cur_tsc, u64 *time)
105dd2cb348SMichael Kelley {
1069397fa2eSPeter Zijlstra 	return false;
107dd2cb348SMichael Kelley }
10831e5e646SMichael Kelley 
hv_stimer_cleanup(unsigned int cpu)10931e5e646SMichael Kelley static inline int hv_stimer_cleanup(unsigned int cpu) { return 0; }
hv_stimer_legacy_init(unsigned int cpu,int sint)11031e5e646SMichael Kelley static inline void hv_stimer_legacy_init(unsigned int cpu, int sint) {}
hv_stimer_legacy_cleanup(unsigned int cpu)11131e5e646SMichael Kelley static inline void hv_stimer_legacy_cleanup(unsigned int cpu) {}
hv_stimer_global_cleanup(void)11231e5e646SMichael Kelley static inline void hv_stimer_global_cleanup(void) {}
hv_stimer0_isr(void)11331e5e646SMichael Kelley static inline void hv_stimer0_isr(void) {}
11431e5e646SMichael Kelley 
1153e2d9453SVitaly Kuznetsov #endif /* CONFIG_HYPERV_TIMER */
116dd2cb348SMichael Kelley 
117fd1fea68SMichael Kelley #endif
118