Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags   |
^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)  * Clocksource driver for the synthetic counter and timers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * provided by the Hyper-V hypervisor to guest VMs, as described
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * in the Hyper-V Top Level Functional Spec (TLFS). This driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * is instruction set architecture independent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright (C) 2019, Microsoft, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * Author:  Michael Kelley <mikelley@microsoft.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/cpumask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/clockchips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/clocksource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/sched_clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/cpuhotplug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <clocksource/hyperv_timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <asm/hyperv-tlfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <asm/mshyperv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static struct clock_event_device __percpu *hv_clock_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static u64 hv_sched_clock_offset __ro_after_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * If false, we're using the old mechanism for stimer0 interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * where it sends a VMbus message when it expires. The old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * mechanism is used when running on older versions of Hyper-V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * that don't support Direct Mode. While Hyper-V provides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * four stimer's per CPU, Linux uses only stimer0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * Because Direct Mode does not require processing a VMbus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * message, stimer interrupts can be enabled earlier in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * process of booting a CPU, and consistent with when timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * interrupts are enabled for other clocksource drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * However, for legacy versions of Hyper-V when Direct Mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * is not enabled, setting up stimer interrupts must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * delayed until VMbus is initialized and can process the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * interrupt message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static bool direct_mode_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static int stimer0_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static int stimer0_vector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static int stimer0_message_sint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * ISR for when stimer0 is operating in Direct Mode.  Direct Mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * does not use VMbus or any VMbus messages, so process here and not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * in the VMbus driver code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) void hv_stimer0_isr(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct clock_event_device *ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	ce = this_cpu_ptr(hv_clock_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	ce->event_handler(ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) EXPORT_SYMBOL_GPL(hv_stimer0_isr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static int hv_ce_set_next_event(unsigned long delta,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 				struct clock_event_device *evt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	u64 current_tick;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	current_tick = hv_read_reference_counter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	current_tick += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	hv_init_timer(0, current_tick);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	return 0;
^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) static int hv_ce_shutdown(struct clock_event_device *evt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	hv_init_timer(0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	hv_init_timer_config(0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (direct_mode_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		hv_disable_stimer0_percpu_irq(stimer0_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return 0;
^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 int hv_ce_set_oneshot(struct clock_event_device *evt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	union hv_stimer_config timer_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	timer_cfg.as_uint64 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	timer_cfg.enable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	timer_cfg.auto_enable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (direct_mode_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		 * When it expires, the timer will directly interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		 * on the specified hardware vector/IRQ.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		timer_cfg.direct_mode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		timer_cfg.apic_vector = stimer0_vector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		hv_enable_stimer0_percpu_irq(stimer0_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		 * When it expires, the timer will generate a VMbus message,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		 * to be handled by the normal VMbus interrupt handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		timer_cfg.direct_mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		timer_cfg.sintx = stimer0_message_sint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	hv_init_timer_config(0, timer_cfg.as_uint64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * hv_stimer_init - Per-cpu initialization of the clockevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int hv_stimer_init(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct clock_event_device *ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (!hv_clock_event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	ce = per_cpu_ptr(hv_clock_event, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	ce->name = "Hyper-V clockevent";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	ce->features = CLOCK_EVT_FEAT_ONESHOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	ce->cpumask = cpumask_of(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	ce->rating = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	ce->set_state_shutdown = hv_ce_shutdown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	ce->set_state_oneshot = hv_ce_set_oneshot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	ce->set_next_event = hv_ce_set_next_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	clockevents_config_and_register(ce,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 					HV_CLOCK_HZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 					HV_MIN_DELTA_TICKS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 					HV_MAX_MAX_DELTA_TICKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  * hv_stimer_cleanup - Per-cpu cleanup of the clockevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int hv_stimer_cleanup(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	struct clock_event_device *ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (!hv_clock_event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	 * In the legacy case where Direct Mode is not enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	 * (which can only be on x86/64), stimer cleanup happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	 * relatively early in the CPU offlining process. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	 * must unbind the stimer-based clockevent device so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	 * that the LAPIC timer can take over until clockevents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	 * are no longer needed in the offlining process. Note
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	 * that clockevents_unbind_device() eventually calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	 * hv_ce_shutdown().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	 * The unbind should not be done when Direct Mode is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	 * enabled because we may be on an architecture where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	 * there are no other clockevent devices to fallback to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	ce = per_cpu_ptr(hv_clock_event, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (direct_mode_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		hv_ce_shutdown(ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		clockevents_unbind_device(ce, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) EXPORT_SYMBOL_GPL(hv_stimer_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /* hv_stimer_alloc - Global initialization of the clockevent and stimer0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int hv_stimer_alloc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	 * Synthetic timers are always available except on old versions of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	 * Hyper-V on x86.  In that case, return as error as Linux will use a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	 * clockevent based on emulated LAPIC timer hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (!(ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	hv_clock_event = alloc_percpu(struct clock_event_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (!hv_clock_event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	direct_mode_enabled = ms_hyperv.misc_features &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			HV_STIMER_DIRECT_MODE_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (direct_mode_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		ret = hv_setup_stimer0_irq(&stimer0_irq, &stimer0_vector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 				hv_stimer0_isr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			goto free_percpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		 * Since we are in Direct Mode, stimer initialization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		 * can be done now with a CPUHP value in the same range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		 * as other clockevent devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		ret = cpuhp_setup_state(CPUHP_AP_HYPERV_TIMER_STARTING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 				"clockevents/hyperv/stimer:starting",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 				hv_stimer_init, hv_stimer_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			goto free_stimer0_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) free_stimer0_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	hv_remove_stimer0_irq(stimer0_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	stimer0_irq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) free_percpu:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	free_percpu(hv_clock_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	hv_clock_event = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) EXPORT_SYMBOL_GPL(hv_stimer_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  * hv_stimer_legacy_init -- Called from the VMbus driver to handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)  * the case when Direct Mode is not enabled, and the stimer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  * must be initialized late in the CPU onlining process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) void hv_stimer_legacy_init(unsigned int cpu, int sint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (direct_mode_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	 * This function gets called by each vCPU, so setting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	 * global stimer_message_sint value each time is conceptually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	 * not ideal, but the value passed in is always the same and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	 * it avoids introducing yet another interface into this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	 * clocksource driver just to set the sint in the legacy case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	stimer0_message_sint = sint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	(void)hv_stimer_init(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) EXPORT_SYMBOL_GPL(hv_stimer_legacy_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)  * hv_stimer_legacy_cleanup -- Called from the VMbus driver to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  * handle the case when Direct Mode is not enabled, and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)  * stimer must be cleaned up early in the CPU offlining
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)  * process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) void hv_stimer_legacy_cleanup(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (direct_mode_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	(void)hv_stimer_cleanup(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) EXPORT_SYMBOL_GPL(hv_stimer_legacy_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /* hv_stimer_free - Free global resources allocated by hv_stimer_alloc() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) void hv_stimer_free(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (!hv_clock_event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (direct_mode_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		cpuhp_remove_state(CPUHP_AP_HYPERV_TIMER_STARTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		hv_remove_stimer0_irq(stimer0_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		stimer0_irq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	free_percpu(hv_clock_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	hv_clock_event = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) EXPORT_SYMBOL_GPL(hv_stimer_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)  * Do a global cleanup of clockevents for the cases of kexec and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)  * vmbus exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) void hv_stimer_global_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	int	cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	 * hv_stime_legacy_cleanup() will stop the stimer if Direct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	 * Mode is not enabled, and fallback to the LAPIC timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	for_each_present_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		hv_stimer_legacy_cleanup(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	 * If Direct Mode is enabled, the cpuhp teardown callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	 * (hv_stimer_cleanup) will be run on all CPUs to stop the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	 * stimers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	hv_stimer_free();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) EXPORT_SYMBOL_GPL(hv_stimer_global_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)  * Code and definitions for the Hyper-V clocksources.  Two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)  * clocksources are defined: one that reads the Hyper-V defined MSR, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  * the other that uses the TSC reference page feature as defined in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)  * TLFS.  The MSR version is for compatibility with old versions of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)  * Hyper-V and 32-bit x86.  The TSC reference page version is preferred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)  * The Hyper-V clocksource ratings of 250 are chosen to be below the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  * TSC clocksource rating of 300.  In configurations where Hyper-V offers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  * an InvariantTSC, the TSC is not marked "unstable", so the TSC clocksource
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  * is available and preferred.  With the higher rating, it will be the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  * default.  On older hardware and Hyper-V versions, the TSC is marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)  * "unstable", so no TSC clocksource is created and the selected Hyper-V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)  * clocksource will be the default.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) u64 (*hv_read_reference_counter)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) EXPORT_SYMBOL_GPL(hv_read_reference_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	struct ms_hyperv_tsc_page page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	u8 reserved[PAGE_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) } tsc_pg __aligned(PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	return &tsc_pg.page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) EXPORT_SYMBOL_GPL(hv_get_tsc_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static u64 notrace read_hv_clock_tsc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	u64 current_tick = hv_read_tsc_page(hv_get_tsc_page());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (current_tick == U64_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		hv_get_time_ref_count(current_tick);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	return current_tick;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static u64 notrace read_hv_clock_tsc_cs(struct clocksource *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	return read_hv_clock_tsc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static u64 notrace read_hv_sched_clock_tsc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	return (read_hv_clock_tsc() - hv_sched_clock_offset) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		(NSEC_PER_SEC / HV_CLOCK_HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static void suspend_hv_clock_tsc(struct clocksource *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	u64 tsc_msr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	/* Disable the TSC page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	hv_get_reference_tsc(tsc_msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	tsc_msr &= ~BIT_ULL(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	hv_set_reference_tsc(tsc_msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static void resume_hv_clock_tsc(struct clocksource *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	phys_addr_t phys_addr = virt_to_phys(&tsc_pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	u64 tsc_msr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	/* Re-enable the TSC page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	hv_get_reference_tsc(tsc_msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	tsc_msr &= GENMASK_ULL(11, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	tsc_msr |= BIT_ULL(0) | (u64)phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	hv_set_reference_tsc(tsc_msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static int hv_cs_enable(struct clocksource *cs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	hv_enable_vdso_clocksource();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static struct clocksource hyperv_cs_tsc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	.name	= "hyperv_clocksource_tsc_page",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	.rating	= 250,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	.read	= read_hv_clock_tsc_cs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	.mask	= CLOCKSOURCE_MASK(64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	.suspend= suspend_hv_clock_tsc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	.resume	= resume_hv_clock_tsc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	.enable = hv_cs_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static u64 notrace read_hv_clock_msr(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	u64 current_tick;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	 * Read the partition counter to get the current tick count. This count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	 * is set to 0 when the partition is created and is incremented in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	 * 100 nanosecond units.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	hv_get_time_ref_count(current_tick);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	return current_tick;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) static u64 notrace read_hv_clock_msr_cs(struct clocksource *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	return read_hv_clock_msr();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static u64 notrace read_hv_sched_clock_msr(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	return (read_hv_clock_msr() - hv_sched_clock_offset) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		(NSEC_PER_SEC / HV_CLOCK_HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) static struct clocksource hyperv_cs_msr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	.name	= "hyperv_clocksource_msr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	.rating	= 250,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	.read	= read_hv_clock_msr_cs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	.mask	= CLOCKSOURCE_MASK(64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) static bool __init hv_init_tsc_clocksource(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	u64		tsc_msr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	phys_addr_t	phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	if (!(ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	hv_read_reference_counter = read_hv_clock_tsc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	phys_addr = virt_to_phys(hv_get_tsc_page());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	 * The Hyper-V TLFS specifies to preserve the value of reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	 * bits in registers. So read the existing value, preserve the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	 * low order 12 bits, and add in the guest physical address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	 * (which already has at least the low 12 bits set to zero since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	 * it is page aligned). Also set the "enable" bit, which is bit 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	hv_get_reference_tsc(tsc_msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	tsc_msr &= GENMASK_ULL(11, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	tsc_msr = tsc_msr | 0x1 | (u64)phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	hv_set_reference_tsc(tsc_msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	hv_set_clocksource_vdso(hyperv_cs_tsc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	hv_sched_clock_offset = hv_read_reference_counter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	hv_setup_sched_clock(read_hv_sched_clock_tsc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) void __init hv_init_clocksource(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	 * Try to set up the TSC page clocksource. If it succeeds, we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	 * done. Otherwise, set up the MSR clocksoruce.  At least one of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	 * these will always be available except on very old versions of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	 * Hyper-V on x86.  In that case we won't have a Hyper-V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	 * clocksource, but Linux will still run with a clocksource based
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	 * on the emulated PIT or LAPIC timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (hv_init_tsc_clocksource())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	if (!(ms_hyperv.features & HV_MSR_TIME_REF_COUNT_AVAILABLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	hv_read_reference_counter = read_hv_clock_msr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	hv_sched_clock_offset = hv_read_reference_counter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	hv_setup_sched_clock(read_hv_sched_clock_msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) EXPORT_SYMBOL_GPL(hv_init_clocksource);