13a025de6SYi Sun // SPDX-License-Identifier: GPL-2.0
23a025de6SYi Sun
33a025de6SYi Sun /*
43a025de6SYi Sun * Hyper-V specific spinlock code.
53a025de6SYi Sun *
63a025de6SYi Sun * Copyright (C) 2018, Intel, Inc.
73a025de6SYi Sun *
83a025de6SYi Sun * Author : Yi Sun <yi.y.sun@intel.com>
93a025de6SYi Sun */
103a025de6SYi Sun
113a025de6SYi Sun #define pr_fmt(fmt) "Hyper-V: " fmt
123a025de6SYi Sun
133a025de6SYi Sun #include <linux/spinlock.h>
143a025de6SYi Sun
153a025de6SYi Sun #include <asm/mshyperv.h>
163a025de6SYi Sun #include <asm/paravirt.h>
173a025de6SYi Sun #include <asm/apic.h>
183a025de6SYi Sun
193a025de6SYi Sun static bool __initdata hv_pvspin = true;
203a025de6SYi Sun
hv_qlock_kick(int cpu)213a025de6SYi Sun static void hv_qlock_kick(int cpu)
223a025de6SYi Sun {
23*28b82352SDave Hansen __apic_send_IPI(cpu, X86_PLATFORM_IPI_VECTOR);
243a025de6SYi Sun }
253a025de6SYi Sun
hv_qlock_wait(u8 * byte,u8 val)263a025de6SYi Sun static void hv_qlock_wait(u8 *byte, u8 val)
273a025de6SYi Sun {
283a025de6SYi Sun unsigned long flags;
293a025de6SYi Sun
303a025de6SYi Sun if (in_nmi())
313a025de6SYi Sun return;
323a025de6SYi Sun
333a025de6SYi Sun /*
343a025de6SYi Sun * Reading HV_X64_MSR_GUEST_IDLE MSR tells the hypervisor that the
353a025de6SYi Sun * vCPU can be put into 'idle' state. This 'idle' state is
363a025de6SYi Sun * terminated by an IPI, usually from hv_qlock_kick(), even if
373a025de6SYi Sun * interrupts are disabled on the vCPU.
383a025de6SYi Sun *
393a025de6SYi Sun * To prevent a race against the unlock path it is required to
403a025de6SYi Sun * disable interrupts before accessing the HV_X64_MSR_GUEST_IDLE
413a025de6SYi Sun * MSR. Otherwise, if the IPI from hv_qlock_kick() arrives between
423a025de6SYi Sun * the lock value check and the rdmsrl() then the vCPU might be put
433a025de6SYi Sun * into 'idle' state by the hypervisor and kept in that state for
443a025de6SYi Sun * an unspecified amount of time.
453a025de6SYi Sun */
463a025de6SYi Sun local_irq_save(flags);
473a025de6SYi Sun /*
483a025de6SYi Sun * Only issue the rdmsrl() when the lock state has not changed.
493a025de6SYi Sun */
5013c4d462SXu Yihang if (READ_ONCE(*byte) == val) {
5113c4d462SXu Yihang unsigned long msr_val;
5213c4d462SXu Yihang
533a025de6SYi Sun rdmsrl(HV_X64_MSR_GUEST_IDLE, msr_val);
5413c4d462SXu Yihang
5513c4d462SXu Yihang (void)msr_val;
5613c4d462SXu Yihang }
573a025de6SYi Sun local_irq_restore(flags);
583a025de6SYi Sun }
593a025de6SYi Sun
603a025de6SYi Sun /*
613a025de6SYi Sun * Hyper-V does not support this so far.
623a025de6SYi Sun */
hv_vcpu_is_preempted(int vcpu)6302143c29SAndi Kleen __visible bool hv_vcpu_is_preempted(int vcpu)
643a025de6SYi Sun {
653a025de6SYi Sun return false;
663a025de6SYi Sun }
673a025de6SYi Sun PV_CALLEE_SAVE_REGS_THUNK(hv_vcpu_is_preempted);
683a025de6SYi Sun
hv_init_spinlocks(void)693a025de6SYi Sun void __init hv_init_spinlocks(void)
703a025de6SYi Sun {
713a025de6SYi Sun if (!hv_pvspin || !apic ||
723a025de6SYi Sun !(ms_hyperv.hints & HV_X64_CLUSTER_IPI_RECOMMENDED) ||
73dfc53baaSJoseph Salisbury !(ms_hyperv.features & HV_MSR_GUEST_IDLE_AVAILABLE)) {
743a025de6SYi Sun pr_info("PV spinlocks disabled\n");
753a025de6SYi Sun return;
763a025de6SYi Sun }
773a025de6SYi Sun pr_info("PV spinlocks enabled\n");
783a025de6SYi Sun
793a025de6SYi Sun __pv_init_lock_hash();
803a025de6SYi Sun pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
813a025de6SYi Sun pv_ops.lock.queued_spin_unlock = PV_CALLEE_SAVE(__pv_queued_spin_unlock);
823a025de6SYi Sun pv_ops.lock.wait = hv_qlock_wait;
833a025de6SYi Sun pv_ops.lock.kick = hv_qlock_kick;
843a025de6SYi Sun pv_ops.lock.vcpu_is_preempted = PV_CALLEE_SAVE(hv_vcpu_is_preempted);
853a025de6SYi Sun }
863a025de6SYi Sun
hv_parse_nopvspin(char * arg)873a025de6SYi Sun static __init int hv_parse_nopvspin(char *arg)
883a025de6SYi Sun {
893a025de6SYi Sun hv_pvspin = false;
903a025de6SYi Sun return 0;
913a025de6SYi Sun }
923a025de6SYi Sun early_param("hv_nopvspin", hv_parse_nopvspin);
93