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*e5dfd093SThomas 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 25*e5dfd093SThomas Gleixner #include <asm/hyperv_timer.h> 26*e5dfd093SThomas 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); 36dd2cb348SMichael Kelley 37dd2cb348SMichael Kelley extern struct ms_hyperv_tsc_page *hv_get_tsc_page(void); 38dd2cb348SMichael Kelley 39dd2cb348SMichael Kelley static inline notrace u64 40dd2cb348SMichael Kelley hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg, u64 *cur_tsc) 41dd2cb348SMichael Kelley { 42dd2cb348SMichael Kelley u64 scale, offset; 43dd2cb348SMichael Kelley u32 sequence; 44dd2cb348SMichael Kelley 45dd2cb348SMichael Kelley /* 46dd2cb348SMichael Kelley * The protocol for reading Hyper-V TSC page is specified in Hypervisor 47dd2cb348SMichael Kelley * Top-Level Functional Specification ver. 3.0 and above. To get the 48dd2cb348SMichael Kelley * reference time we must do the following: 49dd2cb348SMichael Kelley * - READ ReferenceTscSequence 50dd2cb348SMichael Kelley * A special '0' value indicates the time source is unreliable and we 51dd2cb348SMichael Kelley * need to use something else. The currently published specification 52dd2cb348SMichael Kelley * versions (up to 4.0b) contain a mistake and wrongly claim '-1' 53dd2cb348SMichael Kelley * instead of '0' as the special value, see commit c35b82ef0294. 54dd2cb348SMichael Kelley * - ReferenceTime = 55dd2cb348SMichael Kelley * ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset 56dd2cb348SMichael Kelley * - READ ReferenceTscSequence again. In case its value has changed 57dd2cb348SMichael Kelley * since our first reading we need to discard ReferenceTime and repeat 58dd2cb348SMichael Kelley * the whole sequence as the hypervisor was updating the page in 59dd2cb348SMichael Kelley * between. 60dd2cb348SMichael Kelley */ 61dd2cb348SMichael Kelley do { 62dd2cb348SMichael Kelley sequence = READ_ONCE(tsc_pg->tsc_sequence); 63dd2cb348SMichael Kelley if (!sequence) 64dd2cb348SMichael Kelley return U64_MAX; 65dd2cb348SMichael Kelley /* 66dd2cb348SMichael Kelley * Make sure we read sequence before we read other values from 67dd2cb348SMichael Kelley * TSC page. 68dd2cb348SMichael Kelley */ 69dd2cb348SMichael Kelley smp_rmb(); 70dd2cb348SMichael Kelley 71dd2cb348SMichael Kelley scale = READ_ONCE(tsc_pg->tsc_scale); 72dd2cb348SMichael Kelley offset = READ_ONCE(tsc_pg->tsc_offset); 73dd2cb348SMichael Kelley *cur_tsc = hv_get_raw_timer(); 74dd2cb348SMichael Kelley 75dd2cb348SMichael Kelley /* 76dd2cb348SMichael Kelley * Make sure we read sequence after we read all other values 77dd2cb348SMichael Kelley * from TSC page. 78dd2cb348SMichael Kelley */ 79dd2cb348SMichael Kelley smp_rmb(); 80dd2cb348SMichael Kelley 81dd2cb348SMichael Kelley } while (READ_ONCE(tsc_pg->tsc_sequence) != sequence); 82dd2cb348SMichael Kelley 83dd2cb348SMichael Kelley return mul_u64_u64_shr(*cur_tsc, scale, 64) + offset; 84dd2cb348SMichael Kelley } 85dd2cb348SMichael Kelley 86dd2cb348SMichael Kelley static inline notrace u64 87dd2cb348SMichael Kelley hv_read_tsc_page(const struct ms_hyperv_tsc_page *tsc_pg) 88dd2cb348SMichael Kelley { 89dd2cb348SMichael Kelley u64 cur_tsc; 90dd2cb348SMichael Kelley 91dd2cb348SMichael Kelley return hv_read_tsc_page_tsc(tsc_pg, &cur_tsc); 92dd2cb348SMichael Kelley } 93dd2cb348SMichael Kelley 943e2d9453SVitaly Kuznetsov #else /* CONFIG_HYPERV_TIMER */ 95dd2cb348SMichael Kelley static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void) 96dd2cb348SMichael Kelley { 97dd2cb348SMichael Kelley return NULL; 98dd2cb348SMichael Kelley } 99dd2cb348SMichael Kelley 100dd2cb348SMichael Kelley static inline u64 hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg, 101dd2cb348SMichael Kelley u64 *cur_tsc) 102dd2cb348SMichael Kelley { 103dd2cb348SMichael Kelley return U64_MAX; 104dd2cb348SMichael Kelley } 10531e5e646SMichael Kelley 10631e5e646SMichael Kelley static inline int hv_stimer_cleanup(unsigned int cpu) { return 0; } 10731e5e646SMichael Kelley static inline void hv_stimer_legacy_init(unsigned int cpu, int sint) {} 10831e5e646SMichael Kelley static inline void hv_stimer_legacy_cleanup(unsigned int cpu) {} 10931e5e646SMichael Kelley static inline void hv_stimer_global_cleanup(void) {} 11031e5e646SMichael Kelley static inline void hv_stimer0_isr(void) {} 11131e5e646SMichael Kelley 1123e2d9453SVitaly Kuznetsov #endif /* CONFIG_HYPERV_TIMER */ 113dd2cb348SMichael Kelley 114fd1fea68SMichael Kelley #endif 115