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