1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2d5de8841SJeremy Fitzhardinge /* 3d5de8841SJeremy Fitzhardinge * Split spinlock implementation out into its own file, so it can be 4d5de8841SJeremy Fitzhardinge * compiled in a FTRACE-compatible way. 5d5de8841SJeremy Fitzhardinge */ 6d5de8841SJeremy Fitzhardinge #include <linux/kernel_stat.h> 7d5de8841SJeremy Fitzhardinge #include <linux/spinlock.h> 8994025caSJeremy Fitzhardinge #include <linux/debugfs.h> 9994025caSJeremy Fitzhardinge #include <linux/log2.h> 105a0e3ad6STejun Heo #include <linux/gfp.h> 11354e7b76SKonrad Rzeszutek Wilk #include <linux/slab.h> 12d5de8841SJeremy Fitzhardinge 13d5de8841SJeremy Fitzhardinge #include <asm/paravirt.h> 14e6fd28ebSJuergen Gross #include <asm/qspinlock.h> 15d5de8841SJeremy Fitzhardinge 16d5de8841SJeremy Fitzhardinge #include <xen/interface/xen.h> 17d5de8841SJeremy Fitzhardinge #include <xen/events.h> 18d5de8841SJeremy Fitzhardinge 19d5de8841SJeremy Fitzhardinge #include "xen-ops.h" 20994025caSJeremy Fitzhardinge #include "debugfs.h" 21994025caSJeremy Fitzhardinge 22e95e6f17SDavid Vrabel static DEFINE_PER_CPU(int, lock_kicker_irq) = -1; 23e95e6f17SDavid Vrabel static DEFINE_PER_CPU(char *, irq_name); 24e95e6f17SDavid Vrabel static bool xen_pvspin = true; 25e95e6f17SDavid Vrabel 26e95e6f17SDavid Vrabel static void xen_qlock_kick(int cpu) 27e95e6f17SDavid Vrabel { 28707e59baSRoss Lagerwall int irq = per_cpu(lock_kicker_irq, cpu); 29707e59baSRoss Lagerwall 30707e59baSRoss Lagerwall /* Don't kick if the target's kicker interrupt is not initialized. */ 31707e59baSRoss Lagerwall if (irq == -1) 32707e59baSRoss Lagerwall return; 33707e59baSRoss Lagerwall 34e95e6f17SDavid Vrabel xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR); 35e95e6f17SDavid Vrabel } 36e95e6f17SDavid Vrabel 37e95e6f17SDavid Vrabel /* 38e95e6f17SDavid Vrabel * Halt the current CPU & release it back to the host 39e95e6f17SDavid Vrabel */ 40e95e6f17SDavid Vrabel static void xen_qlock_wait(u8 *byte, u8 val) 41e95e6f17SDavid Vrabel { 42e95e6f17SDavid Vrabel int irq = __this_cpu_read(lock_kicker_irq); 43e95e6f17SDavid Vrabel 44e95e6f17SDavid Vrabel /* If kicker interrupts not initialized yet, just spin */ 45e95e6f17SDavid Vrabel if (irq == -1) 46e95e6f17SDavid Vrabel return; 47e95e6f17SDavid Vrabel 48e95e6f17SDavid Vrabel /* clear pending */ 49e95e6f17SDavid Vrabel xen_clear_irq_pending(irq); 50e95e6f17SDavid Vrabel barrier(); 51e95e6f17SDavid Vrabel 52e95e6f17SDavid Vrabel /* 53e95e6f17SDavid Vrabel * We check the byte value after clearing pending IRQ to make sure 54e95e6f17SDavid Vrabel * that we won't miss a wakeup event because of the clearing. 55e95e6f17SDavid Vrabel * 56e95e6f17SDavid Vrabel * The sync_clear_bit() call in xen_clear_irq_pending() is atomic. 57e95e6f17SDavid Vrabel * So it is effectively a memory barrier for x86. 58e95e6f17SDavid Vrabel */ 59e95e6f17SDavid Vrabel if (READ_ONCE(*byte) != val) 60e95e6f17SDavid Vrabel return; 61e95e6f17SDavid Vrabel 62e95e6f17SDavid Vrabel /* 63e95e6f17SDavid Vrabel * If an interrupt happens here, it will leave the wakeup irq 64e95e6f17SDavid Vrabel * pending, which will cause xen_poll_irq() to return 65e95e6f17SDavid Vrabel * immediately. 66e95e6f17SDavid Vrabel */ 67e95e6f17SDavid Vrabel 68e95e6f17SDavid Vrabel /* Block until irq becomes pending (or perhaps a spurious wakeup) */ 69e95e6f17SDavid Vrabel xen_poll_irq(irq); 70e95e6f17SDavid Vrabel } 71e95e6f17SDavid Vrabel 72d5de8841SJeremy Fitzhardinge static irqreturn_t dummy_handler(int irq, void *dev_id) 73d5de8841SJeremy Fitzhardinge { 74d5de8841SJeremy Fitzhardinge BUG(); 75d5de8841SJeremy Fitzhardinge return IRQ_HANDLED; 76d5de8841SJeremy Fitzhardinge } 77d5de8841SJeremy Fitzhardinge 78148f9bb8SPaul Gortmaker void xen_init_lock_cpu(int cpu) 79d5de8841SJeremy Fitzhardinge { 80d5de8841SJeremy Fitzhardinge int irq; 81354e7b76SKonrad Rzeszutek Wilk char *name; 82d5de8841SJeremy Fitzhardinge 83ca5d376eSDou Liyang if (!xen_pvspin) { 84ca5d376eSDou Liyang if (cpu == 0) 85ca5d376eSDou Liyang static_branch_disable(&virt_spin_lock_key); 863310bbedSKonrad Rzeszutek Wilk return; 87ca5d376eSDou Liyang } 883310bbedSKonrad Rzeszutek Wilk 89cb91f8f4SKonrad Rzeszutek Wilk WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n", 90cb9c6f15SKonrad Rzeszutek Wilk cpu, per_cpu(lock_kicker_irq, cpu)); 91cb9c6f15SKonrad Rzeszutek Wilk 92d5de8841SJeremy Fitzhardinge name = kasprintf(GFP_KERNEL, "spinlock%d", cpu); 93d5de8841SJeremy Fitzhardinge irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR, 94d5de8841SJeremy Fitzhardinge cpu, 95d5de8841SJeremy Fitzhardinge dummy_handler, 969d71cee6SMichael Opdenacker IRQF_PERCPU|IRQF_NOBALANCING, 97d5de8841SJeremy Fitzhardinge name, 98d5de8841SJeremy Fitzhardinge NULL); 99d5de8841SJeremy Fitzhardinge 100d5de8841SJeremy Fitzhardinge if (irq >= 0) { 101d5de8841SJeremy Fitzhardinge disable_irq(irq); /* make sure it's never delivered */ 102d5de8841SJeremy Fitzhardinge per_cpu(lock_kicker_irq, cpu) = irq; 103354e7b76SKonrad Rzeszutek Wilk per_cpu(irq_name, cpu) = name; 104d5de8841SJeremy Fitzhardinge } 105d5de8841SJeremy Fitzhardinge 106d5de8841SJeremy Fitzhardinge printk("cpu %d spinlock event irq %d\n", cpu, irq); 107d5de8841SJeremy Fitzhardinge } 108d5de8841SJeremy Fitzhardinge 109d68d82afSAlex Nixon void xen_uninit_lock_cpu(int cpu) 110d68d82afSAlex Nixon { 1113310bbedSKonrad Rzeszutek Wilk if (!xen_pvspin) 1123310bbedSKonrad Rzeszutek Wilk return; 1133310bbedSKonrad Rzeszutek Wilk 114d68d82afSAlex Nixon unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL); 115cb9c6f15SKonrad Rzeszutek Wilk per_cpu(lock_kicker_irq, cpu) = -1; 116354e7b76SKonrad Rzeszutek Wilk kfree(per_cpu(irq_name, cpu)); 117354e7b76SKonrad Rzeszutek Wilk per_cpu(irq_name, cpu) = NULL; 118d68d82afSAlex Nixon } 119d68d82afSAlex Nixon 1203cded417SPeter Zijlstra PV_CALLEE_SAVE_REGS_THUNK(xen_vcpu_stolen); 1213cded417SPeter Zijlstra 122a945928eSKonrad Rzeszutek Wilk /* 123a945928eSKonrad Rzeszutek Wilk * Our init of PV spinlocks is split in two init functions due to us 124a945928eSKonrad Rzeszutek Wilk * using paravirt patching and jump labels patching and having to do 125a945928eSKonrad Rzeszutek Wilk * all of this before SMP code is invoked. 126a945928eSKonrad Rzeszutek Wilk * 127a945928eSKonrad Rzeszutek Wilk * The paravirt patching needs to be done _before_ the alternative asm code 128a945928eSKonrad Rzeszutek Wilk * is started, otherwise we would not patch the core kernel code. 129a945928eSKonrad Rzeszutek Wilk */ 130d5de8841SJeremy Fitzhardinge void __init xen_init_spinlocks(void) 131d5de8841SJeremy Fitzhardinge { 13270dd4998SKonrad Rzeszutek Wilk 133*47b428d1SWaiman Long /* Don't need to use pvqspinlock code if there is only 1 vCPU. */ 134*47b428d1SWaiman Long if (num_possible_cpus() == 1) 135*47b428d1SWaiman Long xen_pvspin = false; 136*47b428d1SWaiman Long 137b8fa70b5SJeremy Fitzhardinge if (!xen_pvspin) { 138b8fa70b5SJeremy Fitzhardinge printk(KERN_DEBUG "xen: PV spinlocks disabled\n"); 139b8fa70b5SJeremy Fitzhardinge return; 140b8fa70b5SJeremy Fitzhardinge } 141e0fc17a9SKonrad Rzeszutek Wilk printk(KERN_DEBUG "xen: PV spinlocks enabled\n"); 142cfd8983fSPeter Zijlstra 143e95e6f17SDavid Vrabel __pv_init_lock_hash(); 144e95e6f17SDavid Vrabel pv_lock_ops.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath; 145e95e6f17SDavid Vrabel pv_lock_ops.queued_spin_unlock = PV_CALLEE_SAVE(__pv_queued_spin_unlock); 146e95e6f17SDavid Vrabel pv_lock_ops.wait = xen_qlock_wait; 147e95e6f17SDavid Vrabel pv_lock_ops.kick = xen_qlock_kick; 1483cded417SPeter Zijlstra pv_lock_ops.vcpu_is_preempted = PV_CALLEE_SAVE(xen_vcpu_stolen); 149d5de8841SJeremy Fitzhardinge } 150994025caSJeremy Fitzhardinge 151b8fa70b5SJeremy Fitzhardinge static __init int xen_parse_nopvspin(char *arg) 152b8fa70b5SJeremy Fitzhardinge { 153b8fa70b5SJeremy Fitzhardinge xen_pvspin = false; 154b8fa70b5SJeremy Fitzhardinge return 0; 155b8fa70b5SJeremy Fitzhardinge } 156b8fa70b5SJeremy Fitzhardinge early_param("xen_nopvspin", xen_parse_nopvspin); 157b8fa70b5SJeremy Fitzhardinge 158