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