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