1fd1fea68SMichael Kelley // SPDX-License-Identifier: GPL-2.0
2fd1fea68SMichael Kelley
3fd1fea68SMichael Kelley /*
4fd1fea68SMichael Kelley * Clocksource driver for the synthetic counter and timers
5fd1fea68SMichael Kelley * provided by the Hyper-V hypervisor to guest VMs, as described
6fd1fea68SMichael Kelley * in the Hyper-V Top Level Functional Spec (TLFS). This driver
7fd1fea68SMichael Kelley * is instruction set architecture independent.
8fd1fea68SMichael Kelley *
9fd1fea68SMichael Kelley * Copyright (C) 2019, Microsoft, Inc.
10fd1fea68SMichael Kelley *
11fd1fea68SMichael Kelley * Author: Michael Kelley <[email protected]>
12fd1fea68SMichael Kelley */
13fd1fea68SMichael Kelley
14fd1fea68SMichael Kelley #include <linux/percpu.h>
15fd1fea68SMichael Kelley #include <linux/cpumask.h>
16fd1fea68SMichael Kelley #include <linux/clockchips.h>
17dd2cb348SMichael Kelley #include <linux/clocksource.h>
18dd2cb348SMichael Kelley #include <linux/sched_clock.h>
19fd1fea68SMichael Kelley #include <linux/mm.h>
204df4cb9eSMichael Kelley #include <linux/cpuhotplug.h>
21ec866be6SMichael Kelley #include <linux/interrupt.h>
22ec866be6SMichael Kelley #include <linux/irq.h>
23ec866be6SMichael Kelley #include <linux/acpi.h>
244ad1aa57SAnirudh Rayabharam #include <linux/hyperv.h>
25fd1fea68SMichael Kelley #include <clocksource/hyperv_timer.h>
26ef5a3c92SNuno Das Neves #include <hyperv/hvhdk.h>
27fd1fea68SMichael Kelley #include <asm/mshyperv.h>
28fd1fea68SMichael Kelley
29fd1fea68SMichael Kelley static struct clock_event_device __percpu *hv_clock_event;
30bcc80decSNaman Jain /* Note: offset can hold negative values after hibernation. */
31bcc80decSNaman Jain static u64 hv_sched_clock_offset __read_mostly;
32fd1fea68SMichael Kelley
33fd1fea68SMichael Kelley /*
34fd1fea68SMichael Kelley * If false, we're using the old mechanism for stimer0 interrupts
35fd1fea68SMichael Kelley * where it sends a VMbus message when it expires. The old
36fd1fea68SMichael Kelley * mechanism is used when running on older versions of Hyper-V
37fd1fea68SMichael Kelley * that don't support Direct Mode. While Hyper-V provides
38fd1fea68SMichael Kelley * four stimer's per CPU, Linux uses only stimer0.
394df4cb9eSMichael Kelley *
404df4cb9eSMichael Kelley * Because Direct Mode does not require processing a VMbus
414df4cb9eSMichael Kelley * message, stimer interrupts can be enabled earlier in the
424df4cb9eSMichael Kelley * process of booting a CPU, and consistent with when timer
434df4cb9eSMichael Kelley * interrupts are enabled for other clocksource drivers.
444df4cb9eSMichael Kelley * However, for legacy versions of Hyper-V when Direct Mode
454df4cb9eSMichael Kelley * is not enabled, setting up stimer interrupts must be
464df4cb9eSMichael Kelley * delayed until VMbus is initialized and can process the
474df4cb9eSMichael Kelley * interrupt message.
48fd1fea68SMichael Kelley */
49fd1fea68SMichael Kelley static bool direct_mode_enabled;
50fd1fea68SMichael Kelley
51ec866be6SMichael Kelley static int stimer0_irq = -1;
52fd1fea68SMichael Kelley static int stimer0_message_sint;
53a4fea9b7SSaurabh Sengar static __maybe_unused DEFINE_PER_CPU(long, stimer0_evt);
54fd1fea68SMichael Kelley
55fd1fea68SMichael Kelley /*
56ec866be6SMichael Kelley * Common code for stimer0 interrupts coming via Direct Mode or
57ec866be6SMichael Kelley * as a VMbus message.
58fd1fea68SMichael Kelley */
hv_stimer0_isr(void)59fd1fea68SMichael Kelley void hv_stimer0_isr(void)
60fd1fea68SMichael Kelley {
61fd1fea68SMichael Kelley struct clock_event_device *ce;
62fd1fea68SMichael Kelley
63fd1fea68SMichael Kelley ce = this_cpu_ptr(hv_clock_event);
64fd1fea68SMichael Kelley ce->event_handler(ce);
65fd1fea68SMichael Kelley }
66fd1fea68SMichael Kelley EXPORT_SYMBOL_GPL(hv_stimer0_isr);
67fd1fea68SMichael Kelley
68ec866be6SMichael Kelley /*
69ec866be6SMichael Kelley * stimer0 interrupt handler for architectures that support
70ec866be6SMichael Kelley * per-cpu interrupts, which also implies Direct Mode.
71ec866be6SMichael Kelley */
hv_stimer0_percpu_isr(int irq,void * dev_id)72a4fea9b7SSaurabh Sengar static irqreturn_t __maybe_unused hv_stimer0_percpu_isr(int irq, void *dev_id)
73ec866be6SMichael Kelley {
74ec866be6SMichael Kelley hv_stimer0_isr();
75ec866be6SMichael Kelley return IRQ_HANDLED;
76ec866be6SMichael Kelley }
77ec866be6SMichael Kelley
hv_ce_set_next_event(unsigned long delta,struct clock_event_device * evt)78fd1fea68SMichael Kelley static int hv_ce_set_next_event(unsigned long delta,
79fd1fea68SMichael Kelley struct clock_event_device *evt)
80fd1fea68SMichael Kelley {
81fd1fea68SMichael Kelley u64 current_tick;
82fd1fea68SMichael Kelley
830af3e137SAndrea Parri current_tick = hv_read_reference_counter();
84fd1fea68SMichael Kelley current_tick += delta;
850e3f7d12SNuno Das Neves hv_set_msr(HV_MSR_STIMER0_COUNT, current_tick);
86fd1fea68SMichael Kelley return 0;
87fd1fea68SMichael Kelley }
88fd1fea68SMichael Kelley
hv_ce_shutdown(struct clock_event_device * evt)89fd1fea68SMichael Kelley static int hv_ce_shutdown(struct clock_event_device *evt)
90fd1fea68SMichael Kelley {
910e3f7d12SNuno Das Neves hv_set_msr(HV_MSR_STIMER0_COUNT, 0);
920e3f7d12SNuno Das Neves hv_set_msr(HV_MSR_STIMER0_CONFIG, 0);
93ec866be6SMichael Kelley if (direct_mode_enabled && stimer0_irq >= 0)
94ec866be6SMichael Kelley disable_percpu_irq(stimer0_irq);
95fd1fea68SMichael Kelley
96fd1fea68SMichael Kelley return 0;
97fd1fea68SMichael Kelley }
98fd1fea68SMichael Kelley
hv_ce_set_oneshot(struct clock_event_device * evt)99fd1fea68SMichael Kelley static int hv_ce_set_oneshot(struct clock_event_device *evt)
100fd1fea68SMichael Kelley {
101fd1fea68SMichael Kelley union hv_stimer_config timer_cfg;
102fd1fea68SMichael Kelley
103fd1fea68SMichael Kelley timer_cfg.as_uint64 = 0;
104fd1fea68SMichael Kelley timer_cfg.enable = 1;
105fd1fea68SMichael Kelley timer_cfg.auto_enable = 1;
106fd1fea68SMichael Kelley if (direct_mode_enabled) {
107fd1fea68SMichael Kelley /*
108fd1fea68SMichael Kelley * When it expires, the timer will directly interrupt
109fd1fea68SMichael Kelley * on the specified hardware vector/IRQ.
110fd1fea68SMichael Kelley */
111fd1fea68SMichael Kelley timer_cfg.direct_mode = 1;
112ec866be6SMichael Kelley timer_cfg.apic_vector = HYPERV_STIMER0_VECTOR;
113ec866be6SMichael Kelley if (stimer0_irq >= 0)
114ec866be6SMichael Kelley enable_percpu_irq(stimer0_irq, IRQ_TYPE_NONE);
115fd1fea68SMichael Kelley } else {
116fd1fea68SMichael Kelley /*
117fd1fea68SMichael Kelley * When it expires, the timer will generate a VMbus message,
118fd1fea68SMichael Kelley * to be handled by the normal VMbus interrupt handler.
119fd1fea68SMichael Kelley */
120fd1fea68SMichael Kelley timer_cfg.direct_mode = 0;
121fd1fea68SMichael Kelley timer_cfg.sintx = stimer0_message_sint;
122fd1fea68SMichael Kelley }
1230e3f7d12SNuno Das Neves hv_set_msr(HV_MSR_STIMER0_CONFIG, timer_cfg.as_uint64);
124fd1fea68SMichael Kelley return 0;
125fd1fea68SMichael Kelley }
126fd1fea68SMichael Kelley
127fd1fea68SMichael Kelley /*
128fd1fea68SMichael Kelley * hv_stimer_init - Per-cpu initialization of the clockevent
129fd1fea68SMichael Kelley */
hv_stimer_init(unsigned int cpu)1304df4cb9eSMichael Kelley static int hv_stimer_init(unsigned int cpu)
131fd1fea68SMichael Kelley {
132fd1fea68SMichael Kelley struct clock_event_device *ce;
133fd1fea68SMichael Kelley
1344df4cb9eSMichael Kelley if (!hv_clock_event)
1354df4cb9eSMichael Kelley return 0;
136fd1fea68SMichael Kelley
137fd1fea68SMichael Kelley ce = per_cpu_ptr(hv_clock_event, cpu);
138fd1fea68SMichael Kelley ce->name = "Hyper-V clockevent";
139fd1fea68SMichael Kelley ce->features = CLOCK_EVT_FEAT_ONESHOT;
140fd1fea68SMichael Kelley ce->cpumask = cpumask_of(cpu);
1417f828d5fSDexuan Cui
1427f828d5fSDexuan Cui /*
1437f828d5fSDexuan Cui * Lower the rating of the Hyper-V timer in a TDX VM without paravisor,
1447f828d5fSDexuan Cui * so the local APIC timer (lapic_clockevent) is the default timer in
1457f828d5fSDexuan Cui * such a VM. The Hyper-V timer is not preferred in such a VM because
1467f828d5fSDexuan Cui * it depends on the slow VM Reference Counter MSR (the Hyper-V TSC
1477f828d5fSDexuan Cui * page is not enbled in such a VM because the VM uses Invariant TSC
1487f828d5fSDexuan Cui * as a better clocksource and it's challenging to mark the Hyper-V
1497f828d5fSDexuan Cui * TSC page shared in very early boot).
1507f828d5fSDexuan Cui */
1517f828d5fSDexuan Cui if (!ms_hyperv.paravisor_present && hv_isolation_type_tdx())
1527f828d5fSDexuan Cui ce->rating = 90;
1537f828d5fSDexuan Cui else
154fd1fea68SMichael Kelley ce->rating = 1000;
1557f828d5fSDexuan Cui
156fd1fea68SMichael Kelley ce->set_state_shutdown = hv_ce_shutdown;
157fd1fea68SMichael Kelley ce->set_state_oneshot = hv_ce_set_oneshot;
158fd1fea68SMichael Kelley ce->set_next_event = hv_ce_set_next_event;
159fd1fea68SMichael Kelley
160fd1fea68SMichael Kelley clockevents_config_and_register(ce,
161fd1fea68SMichael Kelley HV_CLOCK_HZ,
162fd1fea68SMichael Kelley HV_MIN_DELTA_TICKS,
163fd1fea68SMichael Kelley HV_MAX_MAX_DELTA_TICKS);
1644df4cb9eSMichael Kelley return 0;
165fd1fea68SMichael Kelley }
166fd1fea68SMichael Kelley
167fd1fea68SMichael Kelley /*
168fd1fea68SMichael Kelley * hv_stimer_cleanup - Per-cpu cleanup of the clockevent
169fd1fea68SMichael Kelley */
hv_stimer_cleanup(unsigned int cpu)1704df4cb9eSMichael Kelley int hv_stimer_cleanup(unsigned int cpu)
171fd1fea68SMichael Kelley {
172fd1fea68SMichael Kelley struct clock_event_device *ce;
173fd1fea68SMichael Kelley
1744df4cb9eSMichael Kelley if (!hv_clock_event)
1754df4cb9eSMichael Kelley return 0;
1764df4cb9eSMichael Kelley
1774df4cb9eSMichael Kelley /*
1784df4cb9eSMichael Kelley * In the legacy case where Direct Mode is not enabled
1794df4cb9eSMichael Kelley * (which can only be on x86/64), stimer cleanup happens
1804df4cb9eSMichael Kelley * relatively early in the CPU offlining process. We
1814df4cb9eSMichael Kelley * must unbind the stimer-based clockevent device so
1824df4cb9eSMichael Kelley * that the LAPIC timer can take over until clockevents
1834df4cb9eSMichael Kelley * are no longer needed in the offlining process. Note
1844df4cb9eSMichael Kelley * that clockevents_unbind_device() eventually calls
1854df4cb9eSMichael Kelley * hv_ce_shutdown().
1864df4cb9eSMichael Kelley *
1874df4cb9eSMichael Kelley * The unbind should not be done when Direct Mode is
1884df4cb9eSMichael Kelley * enabled because we may be on an architecture where
1894df4cb9eSMichael Kelley * there are no other clockevent devices to fallback to.
1904df4cb9eSMichael Kelley */
191fd1fea68SMichael Kelley ce = per_cpu_ptr(hv_clock_event, cpu);
1924df4cb9eSMichael Kelley if (direct_mode_enabled)
193fd1fea68SMichael Kelley hv_ce_shutdown(ce);
1944df4cb9eSMichael Kelley else
1954df4cb9eSMichael Kelley clockevents_unbind_device(ce, cpu);
1964df4cb9eSMichael Kelley
1974df4cb9eSMichael Kelley return 0;
198fd1fea68SMichael Kelley }
199fd1fea68SMichael Kelley EXPORT_SYMBOL_GPL(hv_stimer_cleanup);
200fd1fea68SMichael Kelley
201ec866be6SMichael Kelley /*
202ec866be6SMichael Kelley * These placeholders are overridden by arch specific code on
203ec866be6SMichael Kelley * architectures that need special setup of the stimer0 IRQ because
204ec866be6SMichael Kelley * they don't support per-cpu IRQs (such as x86/x64).
205ec866be6SMichael Kelley */
hv_setup_stimer0_handler(void (* handler)(void))206ec866be6SMichael Kelley void __weak hv_setup_stimer0_handler(void (*handler)(void))
207fd1fea68SMichael Kelley {
208ec866be6SMichael Kelley };
209ec866be6SMichael Kelley
hv_remove_stimer0_handler(void)210ec866be6SMichael Kelley void __weak hv_remove_stimer0_handler(void)
211ec866be6SMichael Kelley {
212ec866be6SMichael Kelley };
213ec866be6SMichael Kelley
214a4fea9b7SSaurabh Sengar #ifdef CONFIG_ACPI
215ec866be6SMichael Kelley /* Called only on architectures with per-cpu IRQs (i.e., not x86/x64) */
hv_setup_stimer0_irq(void)216ec866be6SMichael Kelley static int hv_setup_stimer0_irq(void)
217ec866be6SMichael Kelley {
218ec866be6SMichael Kelley int ret;
219ec866be6SMichael Kelley
220ec866be6SMichael Kelley ret = acpi_register_gsi(NULL, HYPERV_STIMER0_VECTOR,
221ec866be6SMichael Kelley ACPI_EDGE_SENSITIVE, ACPI_ACTIVE_HIGH);
222ec866be6SMichael Kelley if (ret < 0) {
223ec866be6SMichael Kelley pr_err("Can't register Hyper-V stimer0 GSI. Error %d", ret);
224ec866be6SMichael Kelley return ret;
225ec866be6SMichael Kelley }
226ec866be6SMichael Kelley stimer0_irq = ret;
227ec866be6SMichael Kelley
228ec866be6SMichael Kelley ret = request_percpu_irq(stimer0_irq, hv_stimer0_percpu_isr,
229ec866be6SMichael Kelley "Hyper-V stimer0", &stimer0_evt);
230ec866be6SMichael Kelley if (ret) {
231ec866be6SMichael Kelley pr_err("Can't request Hyper-V stimer0 IRQ %d. Error %d",
232ec866be6SMichael Kelley stimer0_irq, ret);
233ec866be6SMichael Kelley acpi_unregister_gsi(stimer0_irq);
234ec866be6SMichael Kelley stimer0_irq = -1;
235ec866be6SMichael Kelley }
236ec866be6SMichael Kelley return ret;
237ec866be6SMichael Kelley }
238ec866be6SMichael Kelley
hv_remove_stimer0_irq(void)239ec866be6SMichael Kelley static void hv_remove_stimer0_irq(void)
240ec866be6SMichael Kelley {
241ec866be6SMichael Kelley if (stimer0_irq == -1) {
242ec866be6SMichael Kelley hv_remove_stimer0_handler();
243ec866be6SMichael Kelley } else {
244ec866be6SMichael Kelley free_percpu_irq(stimer0_irq, &stimer0_evt);
245ec866be6SMichael Kelley acpi_unregister_gsi(stimer0_irq);
246ec866be6SMichael Kelley stimer0_irq = -1;
247ec866be6SMichael Kelley }
248ec866be6SMichael Kelley }
249a4fea9b7SSaurabh Sengar #else
hv_setup_stimer0_irq(void)250a4fea9b7SSaurabh Sengar static int hv_setup_stimer0_irq(void)
251a4fea9b7SSaurabh Sengar {
252a4fea9b7SSaurabh Sengar return 0;
253a4fea9b7SSaurabh Sengar }
254a4fea9b7SSaurabh Sengar
hv_remove_stimer0_irq(void)255a4fea9b7SSaurabh Sengar static void hv_remove_stimer0_irq(void)
256a4fea9b7SSaurabh Sengar {
257a4fea9b7SSaurabh Sengar }
258a4fea9b7SSaurabh Sengar #endif
259ec866be6SMichael Kelley
260ec866be6SMichael Kelley /* hv_stimer_alloc - Global initialization of the clockevent and stimer0 */
hv_stimer_alloc(bool have_percpu_irqs)261ec866be6SMichael Kelley int hv_stimer_alloc(bool have_percpu_irqs)
262ec866be6SMichael Kelley {
263ec866be6SMichael Kelley int ret;
2644df4cb9eSMichael Kelley
2654df4cb9eSMichael Kelley /*
2664df4cb9eSMichael Kelley * Synthetic timers are always available except on old versions of
2674df4cb9eSMichael Kelley * Hyper-V on x86. In that case, return as error as Linux will use a
2684df4cb9eSMichael Kelley * clockevent based on emulated LAPIC timer hardware.
2694df4cb9eSMichael Kelley */
2704df4cb9eSMichael Kelley if (!(ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE))
2714df4cb9eSMichael Kelley return -EINVAL;
272fd1fea68SMichael Kelley
273fd1fea68SMichael Kelley hv_clock_event = alloc_percpu(struct clock_event_device);
274fd1fea68SMichael Kelley if (!hv_clock_event)
275fd1fea68SMichael Kelley return -ENOMEM;
276fd1fea68SMichael Kelley
277fd1fea68SMichael Kelley direct_mode_enabled = ms_hyperv.misc_features &
278fd1fea68SMichael Kelley HV_STIMER_DIRECT_MODE_AVAILABLE;
279ec866be6SMichael Kelley
280ec866be6SMichael Kelley /*
281ec866be6SMichael Kelley * If Direct Mode isn't enabled, the remainder of the initialization
282ec866be6SMichael Kelley * is done later by hv_stimer_legacy_init()
283ec866be6SMichael Kelley */
284ec866be6SMichael Kelley if (!direct_mode_enabled)
285ec866be6SMichael Kelley return 0;
286ec866be6SMichael Kelley
287ec866be6SMichael Kelley if (have_percpu_irqs) {
288ec866be6SMichael Kelley ret = hv_setup_stimer0_irq();
2894df4cb9eSMichael Kelley if (ret)
290ec866be6SMichael Kelley goto free_clock_event;
291ec866be6SMichael Kelley } else {
292ec866be6SMichael Kelley hv_setup_stimer0_handler(hv_stimer0_isr);
293ec866be6SMichael Kelley }
2944df4cb9eSMichael Kelley
2954df4cb9eSMichael Kelley /*
2964df4cb9eSMichael Kelley * Since we are in Direct Mode, stimer initialization
2974df4cb9eSMichael Kelley * can be done now with a CPUHP value in the same range
2984df4cb9eSMichael Kelley * as other clockevent devices.
2994df4cb9eSMichael Kelley */
3004df4cb9eSMichael Kelley ret = cpuhp_setup_state(CPUHP_AP_HYPERV_TIMER_STARTING,
3014df4cb9eSMichael Kelley "clockevents/hyperv/stimer:starting",
3024df4cb9eSMichael Kelley hv_stimer_init, hv_stimer_cleanup);
303ec866be6SMichael Kelley if (ret < 0) {
304ec866be6SMichael Kelley hv_remove_stimer0_irq();
305ec866be6SMichael Kelley goto free_clock_event;
3064df4cb9eSMichael Kelley }
3074df4cb9eSMichael Kelley return ret;
3084df4cb9eSMichael Kelley
309ec866be6SMichael Kelley free_clock_event:
310fd1fea68SMichael Kelley free_percpu(hv_clock_event);
311fd1fea68SMichael Kelley hv_clock_event = NULL;
312fd1fea68SMichael Kelley return ret;
313fd1fea68SMichael Kelley }
314fd1fea68SMichael Kelley EXPORT_SYMBOL_GPL(hv_stimer_alloc);
315fd1fea68SMichael Kelley
3164df4cb9eSMichael Kelley /*
3174df4cb9eSMichael Kelley * hv_stimer_legacy_init -- Called from the VMbus driver to handle
3184df4cb9eSMichael Kelley * the case when Direct Mode is not enabled, and the stimer
3194df4cb9eSMichael Kelley * must be initialized late in the CPU onlining process.
3204df4cb9eSMichael Kelley *
3214df4cb9eSMichael Kelley */
hv_stimer_legacy_init(unsigned int cpu,int sint)3224df4cb9eSMichael Kelley void hv_stimer_legacy_init(unsigned int cpu, int sint)
3234df4cb9eSMichael Kelley {
3244df4cb9eSMichael Kelley if (direct_mode_enabled)
3254df4cb9eSMichael Kelley return;
3264df4cb9eSMichael Kelley
3274df4cb9eSMichael Kelley /*
3284df4cb9eSMichael Kelley * This function gets called by each vCPU, so setting the
3294df4cb9eSMichael Kelley * global stimer_message_sint value each time is conceptually
3304df4cb9eSMichael Kelley * not ideal, but the value passed in is always the same and
3314df4cb9eSMichael Kelley * it avoids introducing yet another interface into this
3324df4cb9eSMichael Kelley * clocksource driver just to set the sint in the legacy case.
3334df4cb9eSMichael Kelley */
3344df4cb9eSMichael Kelley stimer0_message_sint = sint;
3354df4cb9eSMichael Kelley (void)hv_stimer_init(cpu);
3364df4cb9eSMichael Kelley }
3374df4cb9eSMichael Kelley EXPORT_SYMBOL_GPL(hv_stimer_legacy_init);
3384df4cb9eSMichael Kelley
3394df4cb9eSMichael Kelley /*
3404df4cb9eSMichael Kelley * hv_stimer_legacy_cleanup -- Called from the VMbus driver to
3414df4cb9eSMichael Kelley * handle the case when Direct Mode is not enabled, and the
3424df4cb9eSMichael Kelley * stimer must be cleaned up early in the CPU offlining
3434df4cb9eSMichael Kelley * process.
3444df4cb9eSMichael Kelley */
hv_stimer_legacy_cleanup(unsigned int cpu)3454df4cb9eSMichael Kelley void hv_stimer_legacy_cleanup(unsigned int cpu)
3464df4cb9eSMichael Kelley {
3474df4cb9eSMichael Kelley if (direct_mode_enabled)
3484df4cb9eSMichael Kelley return;
3494df4cb9eSMichael Kelley (void)hv_stimer_cleanup(cpu);
3504df4cb9eSMichael Kelley }
3514df4cb9eSMichael Kelley EXPORT_SYMBOL_GPL(hv_stimer_legacy_cleanup);
3524df4cb9eSMichael Kelley
353fd1fea68SMichael Kelley /*
354fd1fea68SMichael Kelley * Do a global cleanup of clockevents for the cases of kexec and
355fd1fea68SMichael Kelley * vmbus exit
356fd1fea68SMichael Kelley */
hv_stimer_global_cleanup(void)357fd1fea68SMichael Kelley void hv_stimer_global_cleanup(void)
358fd1fea68SMichael Kelley {
359fd1fea68SMichael Kelley int cpu;
360fd1fea68SMichael Kelley
3614df4cb9eSMichael Kelley /*
3624df4cb9eSMichael Kelley * hv_stime_legacy_cleanup() will stop the stimer if Direct
3634df4cb9eSMichael Kelley * Mode is not enabled, and fallback to the LAPIC timer.
3644df4cb9eSMichael Kelley */
365fd1fea68SMichael Kelley for_each_present_cpu(cpu) {
3664df4cb9eSMichael Kelley hv_stimer_legacy_cleanup(cpu);
367fd1fea68SMichael Kelley }
3684df4cb9eSMichael Kelley
369ec866be6SMichael Kelley if (!hv_clock_event)
370ec866be6SMichael Kelley return;
371ec866be6SMichael Kelley
372ec866be6SMichael Kelley if (direct_mode_enabled) {
373ec866be6SMichael Kelley cpuhp_remove_state(CPUHP_AP_HYPERV_TIMER_STARTING);
374ec866be6SMichael Kelley hv_remove_stimer0_irq();
375ec866be6SMichael Kelley stimer0_irq = -1;
376ec866be6SMichael Kelley }
377ec866be6SMichael Kelley free_percpu(hv_clock_event);
378ec866be6SMichael Kelley hv_clock_event = NULL;
379ec866be6SMichael Kelley
380fd1fea68SMichael Kelley }
381fd1fea68SMichael Kelley EXPORT_SYMBOL_GPL(hv_stimer_global_cleanup);
382dd2cb348SMichael Kelley
read_hv_clock_msr(void)383e39acc37SPeter Zijlstra static __always_inline u64 read_hv_clock_msr(void)
384e39acc37SPeter Zijlstra {
385e39acc37SPeter Zijlstra /*
386e39acc37SPeter Zijlstra * Read the partition counter to get the current tick count. This count
387e39acc37SPeter Zijlstra * is set to 0 when the partition is created and is incremented in 100
388e39acc37SPeter Zijlstra * nanosecond units.
389e39acc37SPeter Zijlstra *
3900e3f7d12SNuno Das Neves * Use hv_raw_get_msr() because this function is used from
3910e3f7d12SNuno Das Neves * noinstr. Notable; while HV_MSR_TIME_REF_COUNT is a synthetic
392e39acc37SPeter Zijlstra * register it doesn't need the GHCB path.
393e39acc37SPeter Zijlstra */
3940e3f7d12SNuno Das Neves return hv_raw_get_msr(HV_MSR_TIME_REF_COUNT);
395e39acc37SPeter Zijlstra }
396e39acc37SPeter Zijlstra
397dd2cb348SMichael Kelley /*
398dd2cb348SMichael Kelley * Code and definitions for the Hyper-V clocksources. Two
399dd2cb348SMichael Kelley * clocksources are defined: one that reads the Hyper-V defined MSR, and
400dd2cb348SMichael Kelley * the other that uses the TSC reference page feature as defined in the
401dd2cb348SMichael Kelley * TLFS. The MSR version is for compatibility with old versions of
402dd2cb348SMichael Kelley * Hyper-V and 32-bit x86. The TSC reference page version is preferred.
403dd2cb348SMichael Kelley */
404dd2cb348SMichael Kelley
405ddc61bbcSBoqun Feng static union {
406ddc61bbcSBoqun Feng struct ms_hyperv_tsc_page page;
407ddc61bbcSBoqun Feng u8 reserved[PAGE_SIZE];
40845f46b1aSTianyu Lan } tsc_pg __bss_decrypted __aligned(PAGE_SIZE);
409dd2cb348SMichael Kelley
41076be6331SStanislav Kinsburskiy static struct ms_hyperv_tsc_page *tsc_page = &tsc_pg.page;
411e1f5c66dSStanislav Kinsburskiy static unsigned long tsc_pfn;
412e1f5c66dSStanislav Kinsburskiy
hv_get_tsc_pfn(void)413364adc45SStanislav Kinsburskiy unsigned long hv_get_tsc_pfn(void)
414e1f5c66dSStanislav Kinsburskiy {
415e1f5c66dSStanislav Kinsburskiy return tsc_pfn;
416e1f5c66dSStanislav Kinsburskiy }
417364adc45SStanislav Kinsburskiy EXPORT_SYMBOL_GPL(hv_get_tsc_pfn);
41876be6331SStanislav Kinsburskiy
hv_get_tsc_page(void)419dd2cb348SMichael Kelley struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
420dd2cb348SMichael Kelley {
42176be6331SStanislav Kinsburskiy return tsc_page;
422dd2cb348SMichael Kelley }
423dd2cb348SMichael Kelley EXPORT_SYMBOL_GPL(hv_get_tsc_page);
424dd2cb348SMichael Kelley
read_hv_clock_tsc(void)425e39acc37SPeter Zijlstra static __always_inline u64 read_hv_clock_tsc(void)
426dd2cb348SMichael Kelley {
4279397fa2eSPeter Zijlstra u64 cur_tsc, time;
428dd2cb348SMichael Kelley
4299397fa2eSPeter Zijlstra /*
4309397fa2eSPeter Zijlstra * The Hyper-V Top-Level Function Spec (TLFS), section Timers,
4319397fa2eSPeter Zijlstra * subsection Refererence Counter, guarantees that the TSC and MSR
4329397fa2eSPeter Zijlstra * times are in sync and monotonic. Therefore we can fall back
4339397fa2eSPeter Zijlstra * to the MSR in case the TSC page indicates unavailability.
4349397fa2eSPeter Zijlstra */
4359397fa2eSPeter Zijlstra if (!hv_read_tsc_page_tsc(tsc_page, &cur_tsc, &time))
436e39acc37SPeter Zijlstra time = read_hv_clock_msr();
437dd2cb348SMichael Kelley
4389397fa2eSPeter Zijlstra return time;
439dd2cb348SMichael Kelley }
440dd2cb348SMichael Kelley
read_hv_clock_tsc_cs(struct clocksource * arg)4410af3e137SAndrea Parri static u64 notrace read_hv_clock_tsc_cs(struct clocksource *arg)
4420af3e137SAndrea Parri {
4430af3e137SAndrea Parri return read_hv_clock_tsc();
4440af3e137SAndrea Parri }
4450af3e137SAndrea Parri
read_hv_sched_clock_tsc(void)446e39acc37SPeter Zijlstra static u64 noinstr read_hv_sched_clock_tsc(void)
447dd2cb348SMichael Kelley {
448749da8caSYubo Xie return (read_hv_clock_tsc() - hv_sched_clock_offset) *
449749da8caSYubo Xie (NSEC_PER_SEC / HV_CLOCK_HZ);
450dd2cb348SMichael Kelley }
451dd2cb348SMichael Kelley
suspend_hv_clock_tsc(struct clocksource * arg)4521349401fSDexuan Cui static void suspend_hv_clock_tsc(struct clocksource *arg)
4531349401fSDexuan Cui {
4544ad1aa57SAnirudh Rayabharam union hv_reference_tsc_msr tsc_msr;
4551349401fSDexuan Cui
4561349401fSDexuan Cui /* Disable the TSC page */
4570e3f7d12SNuno Das Neves tsc_msr.as_uint64 = hv_get_msr(HV_MSR_REFERENCE_TSC);
4584ad1aa57SAnirudh Rayabharam tsc_msr.enable = 0;
4590e3f7d12SNuno Das Neves hv_set_msr(HV_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
4601349401fSDexuan Cui }
4611349401fSDexuan Cui
4621349401fSDexuan Cui
resume_hv_clock_tsc(struct clocksource * arg)4631349401fSDexuan Cui static void resume_hv_clock_tsc(struct clocksource *arg)
4641349401fSDexuan Cui {
4654ad1aa57SAnirudh Rayabharam union hv_reference_tsc_msr tsc_msr;
4661349401fSDexuan Cui
4671349401fSDexuan Cui /* Re-enable the TSC page */
4680e3f7d12SNuno Das Neves tsc_msr.as_uint64 = hv_get_msr(HV_MSR_REFERENCE_TSC);
4694ad1aa57SAnirudh Rayabharam tsc_msr.enable = 1;
470e1f5c66dSStanislav Kinsburskiy tsc_msr.pfn = tsc_pfn;
4710e3f7d12SNuno Das Neves hv_set_msr(HV_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
4721349401fSDexuan Cui }
4731349401fSDexuan Cui
474bcc80decSNaman Jain /*
475bcc80decSNaman Jain * Called during resume from hibernation, from overridden
476bcc80decSNaman Jain * x86_platform.restore_sched_clock_state routine. This is to adjust offsets
477bcc80decSNaman Jain * used to calculate time for hv tsc page based sched_clock, to account for
478bcc80decSNaman Jain * time spent before hibernation.
479bcc80decSNaman Jain */
hv_adj_sched_clock_offset(u64 offset)480bcc80decSNaman Jain void hv_adj_sched_clock_offset(u64 offset)
481bcc80decSNaman Jain {
482bcc80decSNaman Jain hv_sched_clock_offset -= offset;
483bcc80decSNaman Jain }
484bcc80decSNaman Jain
4853486d2c9SVitaly Kuznetsov #ifdef HAVE_VDSO_CLOCKMODE_HVCLOCK
hv_cs_enable(struct clocksource * cs)486eec399ddSThomas Gleixner static int hv_cs_enable(struct clocksource *cs)
487eec399ddSThomas Gleixner {
488e4ab4658SMichael Kelley vclocks_set_used(VDSO_CLOCKMODE_HVCLOCK);
489eec399ddSThomas Gleixner return 0;
490eec399ddSThomas Gleixner }
491e4ab4658SMichael Kelley #endif
492eec399ddSThomas Gleixner
493dd2cb348SMichael Kelley static struct clocksource hyperv_cs_tsc = {
494dd2cb348SMichael Kelley .name = "hyperv_clocksource_tsc_page",
4954c78738eSMichael Kelley .rating = 500,
4960af3e137SAndrea Parri .read = read_hv_clock_tsc_cs,
497dd2cb348SMichael Kelley .mask = CLOCKSOURCE_MASK(64),
498dd2cb348SMichael Kelley .flags = CLOCK_SOURCE_IS_CONTINUOUS,
4991349401fSDexuan Cui .suspend= suspend_hv_clock_tsc,
5001349401fSDexuan Cui .resume = resume_hv_clock_tsc,
5013486d2c9SVitaly Kuznetsov #ifdef HAVE_VDSO_CLOCKMODE_HVCLOCK
502eec399ddSThomas Gleixner .enable = hv_cs_enable,
503e4ab4658SMichael Kelley .vdso_clock_mode = VDSO_CLOCKMODE_HVCLOCK,
504e4ab4658SMichael Kelley #else
505e4ab4658SMichael Kelley .vdso_clock_mode = VDSO_CLOCKMODE_NONE,
506e4ab4658SMichael Kelley #endif
507dd2cb348SMichael Kelley };
508dd2cb348SMichael Kelley
read_hv_clock_msr_cs(struct clocksource * arg)5090af3e137SAndrea Parri static u64 notrace read_hv_clock_msr_cs(struct clocksource *arg)
5100af3e137SAndrea Parri {
5110af3e137SAndrea Parri return read_hv_clock_msr();
5120af3e137SAndrea Parri }
5130af3e137SAndrea Parri
514dd2cb348SMichael Kelley static struct clocksource hyperv_cs_msr = {
515dd2cb348SMichael Kelley .name = "hyperv_clocksource_msr",
516e5313f1cSMichael Kelley .rating = 495,
5170af3e137SAndrea Parri .read = read_hv_clock_msr_cs,
518dd2cb348SMichael Kelley .mask = CLOCKSOURCE_MASK(64),
519dd2cb348SMichael Kelley .flags = CLOCK_SOURCE_IS_CONTINUOUS,
520dd2cb348SMichael Kelley };
521dd2cb348SMichael Kelley
522eb3e1d37SMichael Kelley /*
523eb3e1d37SMichael Kelley * Reference to pv_ops must be inline so objtool
524eb3e1d37SMichael Kelley * detection of noinstr violations can work correctly.
525eb3e1d37SMichael Kelley */
526eb3e1d37SMichael Kelley #ifdef CONFIG_GENERIC_SCHED_CLOCK
hv_setup_sched_clock(void * sched_clock)527eb3e1d37SMichael Kelley static __always_inline void hv_setup_sched_clock(void *sched_clock)
528eb3e1d37SMichael Kelley {
529eb3e1d37SMichael Kelley /*
530eb3e1d37SMichael Kelley * We're on an architecture with generic sched clock (not x86/x64).
531eb3e1d37SMichael Kelley * The Hyper-V sched clock read function returns nanoseconds, not
532eb3e1d37SMichael Kelley * the normal 100ns units of the Hyper-V synthetic clock.
533eb3e1d37SMichael Kelley */
534eb3e1d37SMichael Kelley sched_clock_register(sched_clock, 64, NSEC_PER_SEC);
535eb3e1d37SMichael Kelley }
536eb3e1d37SMichael Kelley #elif defined CONFIG_PARAVIRT
hv_setup_sched_clock(void * sched_clock)537eb3e1d37SMichael Kelley static __always_inline void hv_setup_sched_clock(void *sched_clock)
538eb3e1d37SMichael Kelley {
539eb3e1d37SMichael Kelley /* We're on x86/x64 *and* using PV ops */
5404d480dbfSLinus Torvalds paravirt_set_sched_clock(sched_clock);
541eb3e1d37SMichael Kelley }
542eb3e1d37SMichael Kelley #else /* !CONFIG_GENERIC_SCHED_CLOCK && !CONFIG_PARAVIRT */
hv_setup_sched_clock(void * sched_clock)543eb3e1d37SMichael Kelley static __always_inline void hv_setup_sched_clock(void *sched_clock) {}
544eb3e1d37SMichael Kelley #endif /* CONFIG_GENERIC_SCHED_CLOCK */
545eb3e1d37SMichael Kelley
hv_init_tsc_clocksource(void)546e5313f1cSMichael Kelley static void __init hv_init_tsc_clocksource(void)
547dd2cb348SMichael Kelley {
5484ad1aa57SAnirudh Rayabharam union hv_reference_tsc_msr tsc_msr;
549dd2cb348SMichael Kelley
5504c78738eSMichael Kelley /*
5514c78738eSMichael Kelley * If Hyper-V offers TSC_INVARIANT, then the virtualized TSC correctly
5524c78738eSMichael Kelley * handles frequency and offset changes due to live migration,
5534c78738eSMichael Kelley * pause/resume, and other VM management operations. So lower the
5544c78738eSMichael Kelley * Hyper-V Reference TSC rating, causing the generic TSC to be used.
5554c78738eSMichael Kelley * TSC_INVARIANT is not offered on ARM64, so the Hyper-V Reference
5564c78738eSMichael Kelley * TSC will be preferred over the virtualized ARM64 arch counter.
5574c78738eSMichael Kelley */
558ec866be6SMichael Kelley if (ms_hyperv.features & HV_ACCESS_TSC_INVARIANT) {
5594c78738eSMichael Kelley hyperv_cs_tsc.rating = 250;
560e5313f1cSMichael Kelley hyperv_cs_msr.rating = 245;
561ec866be6SMichael Kelley }
5624c78738eSMichael Kelley
563c0e96acfSDexuan Cui if (!(ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE))
564e5313f1cSMichael Kelley return;
565c0e96acfSDexuan Cui
5660af3e137SAndrea Parri hv_read_reference_counter = read_hv_clock_tsc;
567dd2cb348SMichael Kelley
568dd2cb348SMichael Kelley /*
5690408f16bSStanislav Kinsburskiy * TSC page mapping works differently in root compared to guest.
5700408f16bSStanislav Kinsburskiy * - In guest partition the guest PFN has to be passed to the
5710408f16bSStanislav Kinsburskiy * hypervisor.
5720408f16bSStanislav Kinsburskiy * - In root partition it's other way around: it has to map the PFN
5730408f16bSStanislav Kinsburskiy * provided by the hypervisor.
5740408f16bSStanislav Kinsburskiy * But it can't be mapped right here as it's too early and MMU isn't
5750408f16bSStanislav Kinsburskiy * ready yet. So, we only set the enable bit here and will remap the
5760408f16bSStanislav Kinsburskiy * page later in hv_remap_tsc_clocksource().
5770408f16bSStanislav Kinsburskiy *
5780408f16bSStanislav Kinsburskiy * It worth mentioning, that TSC clocksource read function
5790408f16bSStanislav Kinsburskiy * (read_hv_clock_tsc) has a MSR-based fallback mechanism, used when
5800408f16bSStanislav Kinsburskiy * TSC page is zeroed (which is the case until the PFN is remapped) and
5810408f16bSStanislav Kinsburskiy * thus TSC clocksource will work even without the real TSC page
5820408f16bSStanislav Kinsburskiy * mapped.
583dd2cb348SMichael Kelley */
5840e3f7d12SNuno Das Neves tsc_msr.as_uint64 = hv_get_msr(HV_MSR_REFERENCE_TSC);
585*db912b89SNuno Das Neves if (hv_root_partition())
5860408f16bSStanislav Kinsburskiy tsc_pfn = tsc_msr.pfn;
5870408f16bSStanislav Kinsburskiy else
5880408f16bSStanislav Kinsburskiy tsc_pfn = HVPFN_DOWN(virt_to_phys(tsc_page));
5894ad1aa57SAnirudh Rayabharam tsc_msr.enable = 1;
590e1f5c66dSStanislav Kinsburskiy tsc_msr.pfn = tsc_pfn;
5910e3f7d12SNuno Das Neves hv_set_msr(HV_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
592dd2cb348SMichael Kelley
593dd2cb348SMichael Kelley clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
594dd2cb348SMichael Kelley
595e5313f1cSMichael Kelley /*
596e5313f1cSMichael Kelley * If TSC is invariant, then let it stay as the sched clock since it
597e5313f1cSMichael Kelley * will be faster than reading the TSC page. But if not invariant, use
598e5313f1cSMichael Kelley * the TSC page so that live migrations across hosts with different
599e5313f1cSMichael Kelley * frequencies is handled correctly.
600e5313f1cSMichael Kelley */
601e5313f1cSMichael Kelley if (!(ms_hyperv.features & HV_ACCESS_TSC_INVARIANT)) {
6020af3e137SAndrea Parri hv_sched_clock_offset = hv_read_reference_counter();
603bd00cd52STianyu Lan hv_setup_sched_clock(read_hv_sched_clock_tsc);
604e5313f1cSMichael Kelley }
605dd2cb348SMichael Kelley }
606dd2cb348SMichael Kelley
hv_init_clocksource(void)607dd2cb348SMichael Kelley void __init hv_init_clocksource(void)
608dd2cb348SMichael Kelley {
609dd2cb348SMichael Kelley /*
610e5313f1cSMichael Kelley * Try to set up the TSC page clocksource, then the MSR clocksource.
611e5313f1cSMichael Kelley * At least one of these will always be available except on very old
612e5313f1cSMichael Kelley * versions of Hyper-V on x86. In that case we won't have a Hyper-V
613dd2cb348SMichael Kelley * clocksource, but Linux will still run with a clocksource based
614dd2cb348SMichael Kelley * on the emulated PIT or LAPIC timer.
615e5313f1cSMichael Kelley *
616e5313f1cSMichael Kelley * Never use the MSR clocksource as sched clock. It's too slow.
617e5313f1cSMichael Kelley * Better to use the native sched clock as the fallback.
618dd2cb348SMichael Kelley */
619e5313f1cSMichael Kelley hv_init_tsc_clocksource();
620dd2cb348SMichael Kelley
621e5313f1cSMichael Kelley if (ms_hyperv.features & HV_MSR_TIME_REF_COUNT_AVAILABLE)
622dd2cb348SMichael Kelley clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
623dd2cb348SMichael Kelley }
6240408f16bSStanislav Kinsburskiy
hv_remap_tsc_clocksource(void)6250408f16bSStanislav Kinsburskiy void __init hv_remap_tsc_clocksource(void)
6260408f16bSStanislav Kinsburskiy {
6270408f16bSStanislav Kinsburskiy if (!(ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE))
6280408f16bSStanislav Kinsburskiy return;
6290408f16bSStanislav Kinsburskiy
630*db912b89SNuno Das Neves if (!hv_root_partition()) {
6310408f16bSStanislav Kinsburskiy WARN(1, "%s: attempt to remap TSC page in guest partition\n",
6320408f16bSStanislav Kinsburskiy __func__);
6330408f16bSStanislav Kinsburskiy return;
6340408f16bSStanislav Kinsburskiy }
6350408f16bSStanislav Kinsburskiy
6360408f16bSStanislav Kinsburskiy tsc_page = memremap(tsc_pfn << HV_HYP_PAGE_SHIFT, sizeof(tsc_pg),
6370408f16bSStanislav Kinsburskiy MEMREMAP_WB);
6380408f16bSStanislav Kinsburskiy if (!tsc_page)
6390408f16bSStanislav Kinsburskiy pr_err("Failed to remap Hyper-V TSC page.\n");
6400408f16bSStanislav Kinsburskiy }
641