Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2
  3/*
  4 * Clocksource driver for the synthetic counter and timers
  5 * provided by the Hyper-V hypervisor to guest VMs, as described
  6 * in the Hyper-V Top Level Functional Spec (TLFS). This driver
  7 * is instruction set architecture independent.
  8 *
  9 * Copyright (C) 2019, Microsoft, Inc.
 10 *
 11 * Author:  Michael Kelley <mikelley@microsoft.com>
 12 */
 13
 14#include <linux/percpu.h>
 15#include <linux/cpumask.h>
 16#include <linux/clockchips.h>
 17#include <linux/clocksource.h>
 18#include <linux/sched_clock.h>
 19#include <linux/mm.h>
 20#include <linux/cpuhotplug.h>
 
 
 
 
 21#include <clocksource/hyperv_timer.h>
 22#include <asm/hyperv-tlfs.h>
 23#include <asm/mshyperv.h>
 24
 25static struct clock_event_device __percpu *hv_clock_event;
 26static u64 hv_sched_clock_offset __ro_after_init;
 
 27
 28/*
 29 * If false, we're using the old mechanism for stimer0 interrupts
 30 * where it sends a VMbus message when it expires. The old
 31 * mechanism is used when running on older versions of Hyper-V
 32 * that don't support Direct Mode. While Hyper-V provides
 33 * four stimer's per CPU, Linux uses only stimer0.
 34 *
 35 * Because Direct Mode does not require processing a VMbus
 36 * message, stimer interrupts can be enabled earlier in the
 37 * process of booting a CPU, and consistent with when timer
 38 * interrupts are enabled for other clocksource drivers.
 39 * However, for legacy versions of Hyper-V when Direct Mode
 40 * is not enabled, setting up stimer interrupts must be
 41 * delayed until VMbus is initialized and can process the
 42 * interrupt message.
 43 */
 44static bool direct_mode_enabled;
 45
 46static int stimer0_irq;
 47static int stimer0_vector;
 48static int stimer0_message_sint;
 
 49
 50/*
 51 * ISR for when stimer0 is operating in Direct Mode.  Direct Mode
 52 * does not use VMbus or any VMbus messages, so process here and not
 53 * in the VMbus driver code.
 54 */
 55void hv_stimer0_isr(void)
 56{
 57	struct clock_event_device *ce;
 58
 59	ce = this_cpu_ptr(hv_clock_event);
 60	ce->event_handler(ce);
 61}
 62EXPORT_SYMBOL_GPL(hv_stimer0_isr);
 63
 
 
 
 
 
 
 
 
 
 
 64static int hv_ce_set_next_event(unsigned long delta,
 65				struct clock_event_device *evt)
 66{
 67	u64 current_tick;
 68
 69	current_tick = hv_read_reference_counter();
 70	current_tick += delta;
 71	hv_init_timer(0, current_tick);
 72	return 0;
 73}
 74
 75static int hv_ce_shutdown(struct clock_event_device *evt)
 76{
 77	hv_init_timer(0, 0);
 78	hv_init_timer_config(0, 0);
 79	if (direct_mode_enabled)
 80		hv_disable_stimer0_percpu_irq(stimer0_irq);
 81
 82	return 0;
 83}
 84
 85static int hv_ce_set_oneshot(struct clock_event_device *evt)
 86{
 87	union hv_stimer_config timer_cfg;
 88
 89	timer_cfg.as_uint64 = 0;
 90	timer_cfg.enable = 1;
 91	timer_cfg.auto_enable = 1;
 92	if (direct_mode_enabled) {
 93		/*
 94		 * When it expires, the timer will directly interrupt
 95		 * on the specified hardware vector/IRQ.
 96		 */
 97		timer_cfg.direct_mode = 1;
 98		timer_cfg.apic_vector = stimer0_vector;
 99		hv_enable_stimer0_percpu_irq(stimer0_irq);
 
100	} else {
101		/*
102		 * When it expires, the timer will generate a VMbus message,
103		 * to be handled by the normal VMbus interrupt handler.
104		 */
105		timer_cfg.direct_mode = 0;
106		timer_cfg.sintx = stimer0_message_sint;
107	}
108	hv_init_timer_config(0, timer_cfg.as_uint64);
109	return 0;
110}
111
112/*
113 * hv_stimer_init - Per-cpu initialization of the clockevent
114 */
115static int hv_stimer_init(unsigned int cpu)
116{
117	struct clock_event_device *ce;
118
119	if (!hv_clock_event)
120		return 0;
121
122	ce = per_cpu_ptr(hv_clock_event, cpu);
123	ce->name = "Hyper-V clockevent";
124	ce->features = CLOCK_EVT_FEAT_ONESHOT;
125	ce->cpumask = cpumask_of(cpu);
126	ce->rating = 1000;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127	ce->set_state_shutdown = hv_ce_shutdown;
128	ce->set_state_oneshot = hv_ce_set_oneshot;
129	ce->set_next_event = hv_ce_set_next_event;
130
131	clockevents_config_and_register(ce,
132					HV_CLOCK_HZ,
133					HV_MIN_DELTA_TICKS,
134					HV_MAX_MAX_DELTA_TICKS);
135	return 0;
136}
137
138/*
139 * hv_stimer_cleanup - Per-cpu cleanup of the clockevent
140 */
141int hv_stimer_cleanup(unsigned int cpu)
142{
143	struct clock_event_device *ce;
144
145	if (!hv_clock_event)
146		return 0;
147
148	/*
149	 * In the legacy case where Direct Mode is not enabled
150	 * (which can only be on x86/64), stimer cleanup happens
151	 * relatively early in the CPU offlining process. We
152	 * must unbind the stimer-based clockevent device so
153	 * that the LAPIC timer can take over until clockevents
154	 * are no longer needed in the offlining process. Note
155	 * that clockevents_unbind_device() eventually calls
156	 * hv_ce_shutdown().
157	 *
158	 * The unbind should not be done when Direct Mode is
159	 * enabled because we may be on an architecture where
160	 * there are no other clockevent devices to fallback to.
161	 */
162	ce = per_cpu_ptr(hv_clock_event, cpu);
163	if (direct_mode_enabled)
164		hv_ce_shutdown(ce);
165	else
166		clockevents_unbind_device(ce, cpu);
167
168	return 0;
169}
170EXPORT_SYMBOL_GPL(hv_stimer_cleanup);
171
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172/* hv_stimer_alloc - Global initialization of the clockevent and stimer0 */
173int hv_stimer_alloc(void)
174{
175	int ret = 0;
176
177	/*
178	 * Synthetic timers are always available except on old versions of
179	 * Hyper-V on x86.  In that case, return as error as Linux will use a
180	 * clockevent based on emulated LAPIC timer hardware.
181	 */
182	if (!(ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE))
183		return -EINVAL;
184
185	hv_clock_event = alloc_percpu(struct clock_event_device);
186	if (!hv_clock_event)
187		return -ENOMEM;
188
189	direct_mode_enabled = ms_hyperv.misc_features &
190			HV_STIMER_DIRECT_MODE_AVAILABLE;
191	if (direct_mode_enabled) {
192		ret = hv_setup_stimer0_irq(&stimer0_irq, &stimer0_vector,
193				hv_stimer0_isr);
 
 
 
 
 
 
 
194		if (ret)
195			goto free_percpu;
 
 
 
196
197		/*
198		 * Since we are in Direct Mode, stimer initialization
199		 * can be done now with a CPUHP value in the same range
200		 * as other clockevent devices.
201		 */
202		ret = cpuhp_setup_state(CPUHP_AP_HYPERV_TIMER_STARTING,
203				"clockevents/hyperv/stimer:starting",
204				hv_stimer_init, hv_stimer_cleanup);
205		if (ret < 0)
206			goto free_stimer0_irq;
 
207	}
208	return ret;
209
210free_stimer0_irq:
211	hv_remove_stimer0_irq(stimer0_irq);
212	stimer0_irq = 0;
213free_percpu:
214	free_percpu(hv_clock_event);
215	hv_clock_event = NULL;
216	return ret;
217}
218EXPORT_SYMBOL_GPL(hv_stimer_alloc);
219
220/*
221 * hv_stimer_legacy_init -- Called from the VMbus driver to handle
222 * the case when Direct Mode is not enabled, and the stimer
223 * must be initialized late in the CPU onlining process.
224 *
225 */
226void hv_stimer_legacy_init(unsigned int cpu, int sint)
227{
228	if (direct_mode_enabled)
229		return;
230
231	/*
232	 * This function gets called by each vCPU, so setting the
233	 * global stimer_message_sint value each time is conceptually
234	 * not ideal, but the value passed in is always the same and
235	 * it avoids introducing yet another interface into this
236	 * clocksource driver just to set the sint in the legacy case.
237	 */
238	stimer0_message_sint = sint;
239	(void)hv_stimer_init(cpu);
240}
241EXPORT_SYMBOL_GPL(hv_stimer_legacy_init);
242
243/*
244 * hv_stimer_legacy_cleanup -- Called from the VMbus driver to
245 * handle the case when Direct Mode is not enabled, and the
246 * stimer must be cleaned up early in the CPU offlining
247 * process.
248 */
249void hv_stimer_legacy_cleanup(unsigned int cpu)
250{
251	if (direct_mode_enabled)
252		return;
253	(void)hv_stimer_cleanup(cpu);
254}
255EXPORT_SYMBOL_GPL(hv_stimer_legacy_cleanup);
256
257
258/* hv_stimer_free - Free global resources allocated by hv_stimer_alloc() */
259void hv_stimer_free(void)
260{
261	if (!hv_clock_event)
262		return;
263
264	if (direct_mode_enabled) {
265		cpuhp_remove_state(CPUHP_AP_HYPERV_TIMER_STARTING);
266		hv_remove_stimer0_irq(stimer0_irq);
267		stimer0_irq = 0;
268	}
269	free_percpu(hv_clock_event);
270	hv_clock_event = NULL;
271}
272EXPORT_SYMBOL_GPL(hv_stimer_free);
273
274/*
275 * Do a global cleanup of clockevents for the cases of kexec and
276 * vmbus exit
277 */
278void hv_stimer_global_cleanup(void)
279{
280	int	cpu;
281
282	/*
283	 * hv_stime_legacy_cleanup() will stop the stimer if Direct
284	 * Mode is not enabled, and fallback to the LAPIC timer.
285	 */
286	for_each_present_cpu(cpu) {
287		hv_stimer_legacy_cleanup(cpu);
288	}
289
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
290	/*
291	 * If Direct Mode is enabled, the cpuhp teardown callback
292	 * (hv_stimer_cleanup) will be run on all CPUs to stop the
293	 * stimers.
 
 
 
 
294	 */
295	hv_stimer_free();
296}
297EXPORT_SYMBOL_GPL(hv_stimer_global_cleanup);
298
299/*
300 * Code and definitions for the Hyper-V clocksources.  Two
301 * clocksources are defined: one that reads the Hyper-V defined MSR, and
302 * the other that uses the TSC reference page feature as defined in the
303 * TLFS.  The MSR version is for compatibility with old versions of
304 * Hyper-V and 32-bit x86.  The TSC reference page version is preferred.
305 *
306 * The Hyper-V clocksource ratings of 250 are chosen to be below the
307 * TSC clocksource rating of 300.  In configurations where Hyper-V offers
308 * an InvariantTSC, the TSC is not marked "unstable", so the TSC clocksource
309 * is available and preferred.  With the higher rating, it will be the
310 * default.  On older hardware and Hyper-V versions, the TSC is marked
311 * "unstable", so no TSC clocksource is created and the selected Hyper-V
312 * clocksource will be the default.
313 */
314
315u64 (*hv_read_reference_counter)(void);
316EXPORT_SYMBOL_GPL(hv_read_reference_counter);
317
318static union {
319	struct ms_hyperv_tsc_page page;
320	u8 reserved[PAGE_SIZE];
321} tsc_pg __aligned(PAGE_SIZE);
 
 
 
 
 
 
 
 
 
322
323struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
324{
325	return &tsc_pg.page;
326}
327EXPORT_SYMBOL_GPL(hv_get_tsc_page);
328
329static u64 notrace read_hv_clock_tsc(void)
330{
331	u64 current_tick = hv_read_tsc_page(hv_get_tsc_page());
332
333	if (current_tick == U64_MAX)
334		hv_get_time_ref_count(current_tick);
 
 
 
 
 
 
335
336	return current_tick;
337}
338
339static u64 notrace read_hv_clock_tsc_cs(struct clocksource *arg)
340{
341	return read_hv_clock_tsc();
342}
343
344static u64 read_hv_sched_clock_tsc(void)
345{
346	return (read_hv_clock_tsc() - hv_sched_clock_offset) *
347		(NSEC_PER_SEC / HV_CLOCK_HZ);
348}
349
350static void suspend_hv_clock_tsc(struct clocksource *arg)
351{
352	u64 tsc_msr;
353
354	/* Disable the TSC page */
355	hv_get_reference_tsc(tsc_msr);
356	tsc_msr &= ~BIT_ULL(0);
357	hv_set_reference_tsc(tsc_msr);
358}
359
360
361static void resume_hv_clock_tsc(struct clocksource *arg)
362{
363	phys_addr_t phys_addr = virt_to_phys(&tsc_pg);
364	u64 tsc_msr;
365
366	/* Re-enable the TSC page */
367	hv_get_reference_tsc(tsc_msr);
368	tsc_msr &= GENMASK_ULL(11, 0);
369	tsc_msr |= BIT_ULL(0) | (u64)phys_addr;
370	hv_set_reference_tsc(tsc_msr);
 
 
 
 
 
 
 
 
 
 
 
371}
372
 
373static int hv_cs_enable(struct clocksource *cs)
374{
375	hv_enable_vdso_clocksource();
376	return 0;
377}
 
378
379static struct clocksource hyperv_cs_tsc = {
380	.name	= "hyperv_clocksource_tsc_page",
381	.rating	= 250,
382	.read	= read_hv_clock_tsc_cs,
383	.mask	= CLOCKSOURCE_MASK(64),
384	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
385	.suspend= suspend_hv_clock_tsc,
386	.resume	= resume_hv_clock_tsc,
 
387	.enable = hv_cs_enable,
 
 
 
 
388};
389
390static u64 notrace read_hv_clock_msr(void)
391{
392	u64 current_tick;
393	/*
394	 * Read the partition counter to get the current tick count. This count
395	 * is set to 0 when the partition is created and is incremented in
396	 * 100 nanosecond units.
397	 */
398	hv_get_time_ref_count(current_tick);
399	return current_tick;
400}
401
402static u64 notrace read_hv_clock_msr_cs(struct clocksource *arg)
403{
404	return read_hv_clock_msr();
405}
406
407static u64 read_hv_sched_clock_msr(void)
408{
409	return (read_hv_clock_msr() - hv_sched_clock_offset) *
410		(NSEC_PER_SEC / HV_CLOCK_HZ);
411}
412
413static struct clocksource hyperv_cs_msr = {
414	.name	= "hyperv_clocksource_msr",
415	.rating	= 250,
416	.read	= read_hv_clock_msr_cs,
417	.mask	= CLOCKSOURCE_MASK(64),
418	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
419};
420
421static bool __init hv_init_tsc_clocksource(void)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422{
423	u64		tsc_msr;
424	phys_addr_t	phys_addr;
 
 
 
 
 
 
 
 
 
 
 
 
425
426	if (!(ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE))
427		return false;
428
429	hv_read_reference_counter = read_hv_clock_tsc;
430	phys_addr = virt_to_phys(hv_get_tsc_page());
431
432	/*
433	 * The Hyper-V TLFS specifies to preserve the value of reserved
434	 * bits in registers. So read the existing value, preserve the
435	 * low order 12 bits, and add in the guest physical address
436	 * (which already has at least the low 12 bits set to zero since
437	 * it is page aligned). Also set the "enable" bit, which is bit 0.
 
 
 
 
 
 
 
 
 
438	 */
439	hv_get_reference_tsc(tsc_msr);
440	tsc_msr &= GENMASK_ULL(11, 0);
441	tsc_msr = tsc_msr | 0x1 | (u64)phys_addr;
442	hv_set_reference_tsc(tsc_msr);
 
 
 
 
443
444	hv_set_clocksource_vdso(hyperv_cs_tsc);
445	clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
446
447	hv_sched_clock_offset = hv_read_reference_counter();
448	hv_setup_sched_clock(read_hv_sched_clock_tsc);
449
450	return true;
 
 
 
 
 
 
451}
452
453void __init hv_init_clocksource(void)
454{
455	/*
456	 * Try to set up the TSC page clocksource. If it succeeds, we're
457	 * done. Otherwise, set up the MSR clocksoruce.  At least one of
458	 * these will always be available except on very old versions of
459	 * Hyper-V on x86.  In that case we won't have a Hyper-V
460	 * clocksource, but Linux will still run with a clocksource based
461	 * on the emulated PIT or LAPIC timer.
 
 
 
462	 */
463	if (hv_init_tsc_clocksource())
464		return;
 
 
 
465
466	if (!(ms_hyperv.features & HV_MSR_TIME_REF_COUNT_AVAILABLE))
 
 
467		return;
468
469	hv_read_reference_counter = read_hv_clock_msr;
470	clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
 
 
 
471
472	hv_sched_clock_offset = hv_read_reference_counter();
473	hv_setup_sched_clock(read_hv_sched_clock_msr);
 
 
474}
475EXPORT_SYMBOL_GPL(hv_init_clocksource);
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2
  3/*
  4 * Clocksource driver for the synthetic counter and timers
  5 * provided by the Hyper-V hypervisor to guest VMs, as described
  6 * in the Hyper-V Top Level Functional Spec (TLFS). This driver
  7 * is instruction set architecture independent.
  8 *
  9 * Copyright (C) 2019, Microsoft, Inc.
 10 *
 11 * Author:  Michael Kelley <mikelley@microsoft.com>
 12 */
 13
 14#include <linux/percpu.h>
 15#include <linux/cpumask.h>
 16#include <linux/clockchips.h>
 17#include <linux/clocksource.h>
 18#include <linux/sched_clock.h>
 19#include <linux/mm.h>
 20#include <linux/cpuhotplug.h>
 21#include <linux/interrupt.h>
 22#include <linux/irq.h>
 23#include <linux/acpi.h>
 24#include <linux/hyperv.h>
 25#include <clocksource/hyperv_timer.h>
 26#include <asm/hyperv-tlfs.h>
 27#include <asm/mshyperv.h>
 28
 29static struct clock_event_device __percpu *hv_clock_event;
 30/* Note: offset can hold negative values after hibernation. */
 31static u64 hv_sched_clock_offset __read_mostly;
 32
 33/*
 34 * If false, we're using the old mechanism for stimer0 interrupts
 35 * where it sends a VMbus message when it expires. The old
 36 * mechanism is used when running on older versions of Hyper-V
 37 * that don't support Direct Mode. While Hyper-V provides
 38 * four stimer's per CPU, Linux uses only stimer0.
 39 *
 40 * Because Direct Mode does not require processing a VMbus
 41 * message, stimer interrupts can be enabled earlier in the
 42 * process of booting a CPU, and consistent with when timer
 43 * interrupts are enabled for other clocksource drivers.
 44 * However, for legacy versions of Hyper-V when Direct Mode
 45 * is not enabled, setting up stimer interrupts must be
 46 * delayed until VMbus is initialized and can process the
 47 * interrupt message.
 48 */
 49static bool direct_mode_enabled;
 50
 51static int stimer0_irq = -1;
 
 52static int stimer0_message_sint;
 53static __maybe_unused DEFINE_PER_CPU(long, stimer0_evt);
 54
 55/*
 56 * Common code for stimer0 interrupts coming via Direct Mode or
 57 * as a VMbus message.
 
 58 */
 59void hv_stimer0_isr(void)
 60{
 61	struct clock_event_device *ce;
 62
 63	ce = this_cpu_ptr(hv_clock_event);
 64	ce->event_handler(ce);
 65}
 66EXPORT_SYMBOL_GPL(hv_stimer0_isr);
 67
 68/*
 69 * stimer0 interrupt handler for architectures that support
 70 * per-cpu interrupts, which also implies Direct Mode.
 71 */
 72static irqreturn_t __maybe_unused hv_stimer0_percpu_isr(int irq, void *dev_id)
 73{
 74	hv_stimer0_isr();
 75	return IRQ_HANDLED;
 76}
 77
 78static int hv_ce_set_next_event(unsigned long delta,
 79				struct clock_event_device *evt)
 80{
 81	u64 current_tick;
 82
 83	current_tick = hv_read_reference_counter();
 84	current_tick += delta;
 85	hv_set_msr(HV_MSR_STIMER0_COUNT, current_tick);
 86	return 0;
 87}
 88
 89static int hv_ce_shutdown(struct clock_event_device *evt)
 90{
 91	hv_set_msr(HV_MSR_STIMER0_COUNT, 0);
 92	hv_set_msr(HV_MSR_STIMER0_CONFIG, 0);
 93	if (direct_mode_enabled && stimer0_irq >= 0)
 94		disable_percpu_irq(stimer0_irq);
 95
 96	return 0;
 97}
 98
 99static int hv_ce_set_oneshot(struct clock_event_device *evt)
100{
101	union hv_stimer_config timer_cfg;
102
103	timer_cfg.as_uint64 = 0;
104	timer_cfg.enable = 1;
105	timer_cfg.auto_enable = 1;
106	if (direct_mode_enabled) {
107		/*
108		 * When it expires, the timer will directly interrupt
109		 * on the specified hardware vector/IRQ.
110		 */
111		timer_cfg.direct_mode = 1;
112		timer_cfg.apic_vector = HYPERV_STIMER0_VECTOR;
113		if (stimer0_irq >= 0)
114			enable_percpu_irq(stimer0_irq, IRQ_TYPE_NONE);
115	} else {
116		/*
117		 * When it expires, the timer will generate a VMbus message,
118		 * to be handled by the normal VMbus interrupt handler.
119		 */
120		timer_cfg.direct_mode = 0;
121		timer_cfg.sintx = stimer0_message_sint;
122	}
123	hv_set_msr(HV_MSR_STIMER0_CONFIG, timer_cfg.as_uint64);
124	return 0;
125}
126
127/*
128 * hv_stimer_init - Per-cpu initialization of the clockevent
129 */
130static int hv_stimer_init(unsigned int cpu)
131{
132	struct clock_event_device *ce;
133
134	if (!hv_clock_event)
135		return 0;
136
137	ce = per_cpu_ptr(hv_clock_event, cpu);
138	ce->name = "Hyper-V clockevent";
139	ce->features = CLOCK_EVT_FEAT_ONESHOT;
140	ce->cpumask = cpumask_of(cpu);
141
142	/*
143	 * Lower the rating of the Hyper-V timer in a TDX VM without paravisor,
144	 * so the local APIC timer (lapic_clockevent) is the default timer in
145	 * such a VM. The Hyper-V timer is not preferred in such a VM because
146	 * it depends on the slow VM Reference Counter MSR (the Hyper-V TSC
147	 * page is not enbled in such a VM because the VM uses Invariant TSC
148	 * as a better clocksource and it's challenging to mark the Hyper-V
149	 * TSC page shared in very early boot).
150	 */
151	if (!ms_hyperv.paravisor_present && hv_isolation_type_tdx())
152		ce->rating = 90;
153	else
154		ce->rating = 1000;
155
156	ce->set_state_shutdown = hv_ce_shutdown;
157	ce->set_state_oneshot = hv_ce_set_oneshot;
158	ce->set_next_event = hv_ce_set_next_event;
159
160	clockevents_config_and_register(ce,
161					HV_CLOCK_HZ,
162					HV_MIN_DELTA_TICKS,
163					HV_MAX_MAX_DELTA_TICKS);
164	return 0;
165}
166
167/*
168 * hv_stimer_cleanup - Per-cpu cleanup of the clockevent
169 */
170int hv_stimer_cleanup(unsigned int cpu)
171{
172	struct clock_event_device *ce;
173
174	if (!hv_clock_event)
175		return 0;
176
177	/*
178	 * In the legacy case where Direct Mode is not enabled
179	 * (which can only be on x86/64), stimer cleanup happens
180	 * relatively early in the CPU offlining process. We
181	 * must unbind the stimer-based clockevent device so
182	 * that the LAPIC timer can take over until clockevents
183	 * are no longer needed in the offlining process. Note
184	 * that clockevents_unbind_device() eventually calls
185	 * hv_ce_shutdown().
186	 *
187	 * The unbind should not be done when Direct Mode is
188	 * enabled because we may be on an architecture where
189	 * there are no other clockevent devices to fallback to.
190	 */
191	ce = per_cpu_ptr(hv_clock_event, cpu);
192	if (direct_mode_enabled)
193		hv_ce_shutdown(ce);
194	else
195		clockevents_unbind_device(ce, cpu);
196
197	return 0;
198}
199EXPORT_SYMBOL_GPL(hv_stimer_cleanup);
200
201/*
202 * These placeholders are overridden by arch specific code on
203 * architectures that need special setup of the stimer0 IRQ because
204 * they don't support per-cpu IRQs (such as x86/x64).
205 */
206void __weak hv_setup_stimer0_handler(void (*handler)(void))
207{
208};
209
210void __weak hv_remove_stimer0_handler(void)
211{
212};
213
214#ifdef CONFIG_ACPI
215/* Called only on architectures with per-cpu IRQs (i.e., not x86/x64) */
216static int hv_setup_stimer0_irq(void)
217{
218	int ret;
219
220	ret = acpi_register_gsi(NULL, HYPERV_STIMER0_VECTOR,
221			ACPI_EDGE_SENSITIVE, ACPI_ACTIVE_HIGH);
222	if (ret < 0) {
223		pr_err("Can't register Hyper-V stimer0 GSI. Error %d", ret);
224		return ret;
225	}
226	stimer0_irq = ret;
227
228	ret = request_percpu_irq(stimer0_irq, hv_stimer0_percpu_isr,
229		"Hyper-V stimer0", &stimer0_evt);
230	if (ret) {
231		pr_err("Can't request Hyper-V stimer0 IRQ %d. Error %d",
232			stimer0_irq, ret);
233		acpi_unregister_gsi(stimer0_irq);
234		stimer0_irq = -1;
235	}
236	return ret;
237}
238
239static void hv_remove_stimer0_irq(void)
240{
241	if (stimer0_irq == -1) {
242		hv_remove_stimer0_handler();
243	} else {
244		free_percpu_irq(stimer0_irq, &stimer0_evt);
245		acpi_unregister_gsi(stimer0_irq);
246		stimer0_irq = -1;
247	}
248}
249#else
250static int hv_setup_stimer0_irq(void)
251{
252	return 0;
253}
254
255static void hv_remove_stimer0_irq(void)
256{
257}
258#endif
259
260/* hv_stimer_alloc - Global initialization of the clockevent and stimer0 */
261int hv_stimer_alloc(bool have_percpu_irqs)
262{
263	int ret;
264
265	/*
266	 * Synthetic timers are always available except on old versions of
267	 * Hyper-V on x86.  In that case, return as error as Linux will use a
268	 * clockevent based on emulated LAPIC timer hardware.
269	 */
270	if (!(ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE))
271		return -EINVAL;
272
273	hv_clock_event = alloc_percpu(struct clock_event_device);
274	if (!hv_clock_event)
275		return -ENOMEM;
276
277	direct_mode_enabled = ms_hyperv.misc_features &
278			HV_STIMER_DIRECT_MODE_AVAILABLE;
279
280	/*
281	 * If Direct Mode isn't enabled, the remainder of the initialization
282	 * is done later by hv_stimer_legacy_init()
283	 */
284	if (!direct_mode_enabled)
285		return 0;
286
287	if (have_percpu_irqs) {
288		ret = hv_setup_stimer0_irq();
289		if (ret)
290			goto free_clock_event;
291	} else {
292		hv_setup_stimer0_handler(hv_stimer0_isr);
293	}
294
295	/*
296	 * Since we are in Direct Mode, stimer initialization
297	 * can be done now with a CPUHP value in the same range
298	 * as other clockevent devices.
299	 */
300	ret = cpuhp_setup_state(CPUHP_AP_HYPERV_TIMER_STARTING,
301			"clockevents/hyperv/stimer:starting",
302			hv_stimer_init, hv_stimer_cleanup);
303	if (ret < 0) {
304		hv_remove_stimer0_irq();
305		goto free_clock_event;
306	}
307	return ret;
308
309free_clock_event:
 
 
 
310	free_percpu(hv_clock_event);
311	hv_clock_event = NULL;
312	return ret;
313}
314EXPORT_SYMBOL_GPL(hv_stimer_alloc);
315
316/*
317 * hv_stimer_legacy_init -- Called from the VMbus driver to handle
318 * the case when Direct Mode is not enabled, and the stimer
319 * must be initialized late in the CPU onlining process.
320 *
321 */
322void hv_stimer_legacy_init(unsigned int cpu, int sint)
323{
324	if (direct_mode_enabled)
325		return;
326
327	/*
328	 * This function gets called by each vCPU, so setting the
329	 * global stimer_message_sint value each time is conceptually
330	 * not ideal, but the value passed in is always the same and
331	 * it avoids introducing yet another interface into this
332	 * clocksource driver just to set the sint in the legacy case.
333	 */
334	stimer0_message_sint = sint;
335	(void)hv_stimer_init(cpu);
336}
337EXPORT_SYMBOL_GPL(hv_stimer_legacy_init);
338
339/*
340 * hv_stimer_legacy_cleanup -- Called from the VMbus driver to
341 * handle the case when Direct Mode is not enabled, and the
342 * stimer must be cleaned up early in the CPU offlining
343 * process.
344 */
345void hv_stimer_legacy_cleanup(unsigned int cpu)
346{
347	if (direct_mode_enabled)
348		return;
349	(void)hv_stimer_cleanup(cpu);
350}
351EXPORT_SYMBOL_GPL(hv_stimer_legacy_cleanup);
352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353/*
354 * Do a global cleanup of clockevents for the cases of kexec and
355 * vmbus exit
356 */
357void hv_stimer_global_cleanup(void)
358{
359	int	cpu;
360
361	/*
362	 * hv_stime_legacy_cleanup() will stop the stimer if Direct
363	 * Mode is not enabled, and fallback to the LAPIC timer.
364	 */
365	for_each_present_cpu(cpu) {
366		hv_stimer_legacy_cleanup(cpu);
367	}
368
369	if (!hv_clock_event)
370		return;
371
372	if (direct_mode_enabled) {
373		cpuhp_remove_state(CPUHP_AP_HYPERV_TIMER_STARTING);
374		hv_remove_stimer0_irq();
375		stimer0_irq = -1;
376	}
377	free_percpu(hv_clock_event);
378	hv_clock_event = NULL;
379
380}
381EXPORT_SYMBOL_GPL(hv_stimer_global_cleanup);
382
383static __always_inline u64 read_hv_clock_msr(void)
384{
385	/*
386	 * Read the partition counter to get the current tick count. This count
387	 * is set to 0 when the partition is created and is incremented in 100
388	 * nanosecond units.
389	 *
390	 * Use hv_raw_get_msr() because this function is used from
391	 * noinstr. Notable; while HV_MSR_TIME_REF_COUNT is a synthetic
392	 * register it doesn't need the GHCB path.
393	 */
394	return hv_raw_get_msr(HV_MSR_TIME_REF_COUNT);
395}
 
396
397/*
398 * Code and definitions for the Hyper-V clocksources.  Two
399 * clocksources are defined: one that reads the Hyper-V defined MSR, and
400 * the other that uses the TSC reference page feature as defined in the
401 * TLFS.  The MSR version is for compatibility with old versions of
402 * Hyper-V and 32-bit x86.  The TSC reference page version is preferred.
 
 
 
 
 
 
 
 
403 */
404
 
 
 
405static union {
406	struct ms_hyperv_tsc_page page;
407	u8 reserved[PAGE_SIZE];
408} tsc_pg __bss_decrypted __aligned(PAGE_SIZE);
409
410static struct ms_hyperv_tsc_page *tsc_page = &tsc_pg.page;
411static unsigned long tsc_pfn;
412
413unsigned long hv_get_tsc_pfn(void)
414{
415	return tsc_pfn;
416}
417EXPORT_SYMBOL_GPL(hv_get_tsc_pfn);
418
419struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
420{
421	return tsc_page;
422}
423EXPORT_SYMBOL_GPL(hv_get_tsc_page);
424
425static __always_inline u64 read_hv_clock_tsc(void)
426{
427	u64 cur_tsc, time;
428
429	/*
430	 * The Hyper-V Top-Level Function Spec (TLFS), section Timers,
431	 * subsection Refererence Counter, guarantees that the TSC and MSR
432	 * times are in sync and monotonic. Therefore we can fall back
433	 * to the MSR in case the TSC page indicates unavailability.
434	 */
435	if (!hv_read_tsc_page_tsc(tsc_page, &cur_tsc, &time))
436		time = read_hv_clock_msr();
437
438	return time;
439}
440
441static u64 notrace read_hv_clock_tsc_cs(struct clocksource *arg)
442{
443	return read_hv_clock_tsc();
444}
445
446static u64 noinstr read_hv_sched_clock_tsc(void)
447{
448	return (read_hv_clock_tsc() - hv_sched_clock_offset) *
449		(NSEC_PER_SEC / HV_CLOCK_HZ);
450}
451
452static void suspend_hv_clock_tsc(struct clocksource *arg)
453{
454	union hv_reference_tsc_msr tsc_msr;
455
456	/* Disable the TSC page */
457	tsc_msr.as_uint64 = hv_get_msr(HV_MSR_REFERENCE_TSC);
458	tsc_msr.enable = 0;
459	hv_set_msr(HV_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
460}
461
462
463static void resume_hv_clock_tsc(struct clocksource *arg)
464{
465	union hv_reference_tsc_msr tsc_msr;
 
466
467	/* Re-enable the TSC page */
468	tsc_msr.as_uint64 = hv_get_msr(HV_MSR_REFERENCE_TSC);
469	tsc_msr.enable = 1;
470	tsc_msr.pfn = tsc_pfn;
471	hv_set_msr(HV_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
472}
473
474/*
475 * Called during resume from hibernation, from overridden
476 * x86_platform.restore_sched_clock_state routine. This is to adjust offsets
477 * used to calculate time for hv tsc page based sched_clock, to account for
478 * time spent before hibernation.
479 */
480void hv_adj_sched_clock_offset(u64 offset)
481{
482	hv_sched_clock_offset -= offset;
483}
484
485#ifdef HAVE_VDSO_CLOCKMODE_HVCLOCK
486static int hv_cs_enable(struct clocksource *cs)
487{
488	vclocks_set_used(VDSO_CLOCKMODE_HVCLOCK);
489	return 0;
490}
491#endif
492
493static struct clocksource hyperv_cs_tsc = {
494	.name	= "hyperv_clocksource_tsc_page",
495	.rating	= 500,
496	.read	= read_hv_clock_tsc_cs,
497	.mask	= CLOCKSOURCE_MASK(64),
498	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
499	.suspend= suspend_hv_clock_tsc,
500	.resume	= resume_hv_clock_tsc,
501#ifdef HAVE_VDSO_CLOCKMODE_HVCLOCK
502	.enable = hv_cs_enable,
503	.vdso_clock_mode = VDSO_CLOCKMODE_HVCLOCK,
504#else
505	.vdso_clock_mode = VDSO_CLOCKMODE_NONE,
506#endif
507};
508
 
 
 
 
 
 
 
 
 
 
 
 
509static u64 notrace read_hv_clock_msr_cs(struct clocksource *arg)
510{
511	return read_hv_clock_msr();
512}
513
 
 
 
 
 
 
514static struct clocksource hyperv_cs_msr = {
515	.name	= "hyperv_clocksource_msr",
516	.rating	= 495,
517	.read	= read_hv_clock_msr_cs,
518	.mask	= CLOCKSOURCE_MASK(64),
519	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
520};
521
522/*
523 * Reference to pv_ops must be inline so objtool
524 * detection of noinstr violations can work correctly.
525 */
526#ifdef CONFIG_GENERIC_SCHED_CLOCK
527static __always_inline void hv_setup_sched_clock(void *sched_clock)
528{
529	/*
530	 * We're on an architecture with generic sched clock (not x86/x64).
531	 * The Hyper-V sched clock read function returns nanoseconds, not
532	 * the normal 100ns units of the Hyper-V synthetic clock.
533	 */
534	sched_clock_register(sched_clock, 64, NSEC_PER_SEC);
535}
536#elif defined CONFIG_PARAVIRT
537static __always_inline void hv_setup_sched_clock(void *sched_clock)
538{
539	/* We're on x86/x64 *and* using PV ops */
540	paravirt_set_sched_clock(sched_clock);
541}
542#else /* !CONFIG_GENERIC_SCHED_CLOCK && !CONFIG_PARAVIRT */
543static __always_inline void hv_setup_sched_clock(void *sched_clock) {}
544#endif /* CONFIG_GENERIC_SCHED_CLOCK */
545
546static void __init hv_init_tsc_clocksource(void)
547{
548	union hv_reference_tsc_msr tsc_msr;
549
550	/*
551	 * If Hyper-V offers TSC_INVARIANT, then the virtualized TSC correctly
552	 * handles frequency and offset changes due to live migration,
553	 * pause/resume, and other VM management operations.  So lower the
554	 * Hyper-V Reference TSC rating, causing the generic TSC to be used.
555	 * TSC_INVARIANT is not offered on ARM64, so the Hyper-V Reference
556	 * TSC will be preferred over the virtualized ARM64 arch counter.
557	 */
558	if (ms_hyperv.features & HV_ACCESS_TSC_INVARIANT) {
559		hyperv_cs_tsc.rating = 250;
560		hyperv_cs_msr.rating = 245;
561	}
562
563	if (!(ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE))
564		return;
565
566	hv_read_reference_counter = read_hv_clock_tsc;
 
567
568	/*
569	 * TSC page mapping works differently in root compared to guest.
570	 * - In guest partition the guest PFN has to be passed to the
571	 *   hypervisor.
572	 * - In root partition it's other way around: it has to map the PFN
573	 *   provided by the hypervisor.
574	 *   But it can't be mapped right here as it's too early and MMU isn't
575	 *   ready yet. So, we only set the enable bit here and will remap the
576	 *   page later in hv_remap_tsc_clocksource().
577	 *
578	 * It worth mentioning, that TSC clocksource read function
579	 * (read_hv_clock_tsc) has a MSR-based fallback mechanism, used when
580	 * TSC page is zeroed (which is the case until the PFN is remapped) and
581	 * thus TSC clocksource will work even without the real TSC page
582	 * mapped.
583	 */
584	tsc_msr.as_uint64 = hv_get_msr(HV_MSR_REFERENCE_TSC);
585	if (hv_root_partition)
586		tsc_pfn = tsc_msr.pfn;
587	else
588		tsc_pfn = HVPFN_DOWN(virt_to_phys(tsc_page));
589	tsc_msr.enable = 1;
590	tsc_msr.pfn = tsc_pfn;
591	hv_set_msr(HV_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
592
 
593	clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
594
595	/*
596	 * If TSC is invariant, then let it stay as the sched clock since it
597	 * will be faster than reading the TSC page. But if not invariant, use
598	 * the TSC page so that live migrations across hosts with different
599	 * frequencies is handled correctly.
600	 */
601	if (!(ms_hyperv.features & HV_ACCESS_TSC_INVARIANT)) {
602		hv_sched_clock_offset = hv_read_reference_counter();
603		hv_setup_sched_clock(read_hv_sched_clock_tsc);
604	}
605}
606
607void __init hv_init_clocksource(void)
608{
609	/*
610	 * Try to set up the TSC page clocksource, then the MSR clocksource.
611	 * At least one of these will always be available except on very old
612	 * versions of Hyper-V on x86.  In that case we won't have a Hyper-V
 
613	 * clocksource, but Linux will still run with a clocksource based
614	 * on the emulated PIT or LAPIC timer.
615	 *
616	 * Never use the MSR clocksource as sched clock.  It's too slow.
617	 * Better to use the native sched clock as the fallback.
618	 */
619	hv_init_tsc_clocksource();
620
621	if (ms_hyperv.features & HV_MSR_TIME_REF_COUNT_AVAILABLE)
622		clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
623}
624
625void __init hv_remap_tsc_clocksource(void)
626{
627	if (!(ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE))
628		return;
629
630	if (!hv_root_partition) {
631		WARN(1, "%s: attempt to remap TSC page in guest partition\n",
632		     __func__);
633		return;
634	}
635
636	tsc_page = memremap(tsc_pfn << HV_HYP_PAGE_SHIFT, sizeof(tsc_pg),
637			    MEMREMAP_WB);
638	if (!tsc_page)
639		pr_err("Failed to remap Hyper-V TSC page.\n");
640}