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