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