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 */
6f2a5fef1SJuergen Gross #include <linux/kernel.h>
7d5de8841SJeremy Fitzhardinge #include <linux/spinlock.h>
8354e7b76SKonrad Rzeszutek Wilk #include <linux/slab.h>
9d3132b38SJuergen Gross #include <linux/atomic.h>
10d5de8841SJeremy Fitzhardinge
11d5de8841SJeremy Fitzhardinge #include <asm/paravirt.h>
12e6fd28ebSJuergen Gross #include <asm/qspinlock.h>
13d5de8841SJeremy Fitzhardinge
14d5de8841SJeremy Fitzhardinge #include <xen/events.h>
15d5de8841SJeremy Fitzhardinge
16d5de8841SJeremy Fitzhardinge #include "xen-ops.h"
17994025caSJeremy Fitzhardinge
18e95e6f17SDavid Vrabel static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
19e95e6f17SDavid Vrabel static DEFINE_PER_CPU(char *, irq_name);
20d3132b38SJuergen Gross static DEFINE_PER_CPU(atomic_t, xen_qlock_wait_nest);
21e95e6f17SDavid Vrabel static bool xen_pvspin = true;
22e95e6f17SDavid Vrabel
xen_qlock_kick(int cpu)23e95e6f17SDavid Vrabel static void xen_qlock_kick(int cpu)
24e95e6f17SDavid Vrabel {
25707e59baSRoss Lagerwall int irq = per_cpu(lock_kicker_irq, cpu);
26707e59baSRoss Lagerwall
27707e59baSRoss Lagerwall /* Don't kick if the target's kicker interrupt is not initialized. */
28707e59baSRoss Lagerwall if (irq == -1)
29707e59baSRoss Lagerwall return;
30707e59baSRoss Lagerwall
31e95e6f17SDavid Vrabel xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
32e95e6f17SDavid Vrabel }
33e95e6f17SDavid Vrabel
34e95e6f17SDavid Vrabel /*
35e95e6f17SDavid Vrabel * Halt the current CPU & release it back to the host
36e95e6f17SDavid Vrabel */
xen_qlock_wait(u8 * byte,u8 val)37e95e6f17SDavid Vrabel static void xen_qlock_wait(u8 *byte, u8 val)
38e95e6f17SDavid Vrabel {
39e95e6f17SDavid Vrabel int irq = __this_cpu_read(lock_kicker_irq);
40d3132b38SJuergen Gross atomic_t *nest_cnt = this_cpu_ptr(&xen_qlock_wait_nest);
41e95e6f17SDavid Vrabel
42e95e6f17SDavid Vrabel /* If kicker interrupts not initialized yet, just spin */
43a8565319SJuergen Gross if (irq == -1 || in_nmi())
44e95e6f17SDavid Vrabel return;
45e95e6f17SDavid Vrabel
46d3132b38SJuergen Gross /* Detect reentry. */
47d3132b38SJuergen Gross atomic_inc(nest_cnt);
48a8565319SJuergen Gross
49d3132b38SJuergen Gross /* If irq pending already and no nested call clear it. */
50d3132b38SJuergen Gross if (atomic_read(nest_cnt) == 1 && xen_test_irq_pending(irq)) {
51e95e6f17SDavid Vrabel xen_clear_irq_pending(irq);
52a8565319SJuergen Gross } else if (READ_ONCE(*byte) == val) {
53a8565319SJuergen Gross /* Block until irq becomes pending (or a spurious wakeup) */
54a8565319SJuergen Gross xen_poll_irq(irq);
552ac2a7d4SJuergen Gross }
56e95e6f17SDavid Vrabel
57d3132b38SJuergen Gross atomic_dec(nest_cnt);
58e95e6f17SDavid Vrabel }
59e95e6f17SDavid Vrabel
dummy_handler(int irq,void * dev_id)60d5de8841SJeremy Fitzhardinge static irqreturn_t dummy_handler(int irq, void *dev_id)
61d5de8841SJeremy Fitzhardinge {
62d5de8841SJeremy Fitzhardinge BUG();
63d5de8841SJeremy Fitzhardinge return IRQ_HANDLED;
64d5de8841SJeremy Fitzhardinge }
65d5de8841SJeremy Fitzhardinge
xen_init_lock_cpu(int cpu)66148f9bb8SPaul Gortmaker void xen_init_lock_cpu(int cpu)
67d5de8841SJeremy Fitzhardinge {
68d5de8841SJeremy Fitzhardinge int irq;
69354e7b76SKonrad Rzeszutek Wilk char *name;
70d5de8841SJeremy Fitzhardinge
71090d54bcSZhenzhong Duan if (!xen_pvspin)
723310bbedSKonrad Rzeszutek Wilk return;
733310bbedSKonrad Rzeszutek Wilk
74cb91f8f4SKonrad Rzeszutek Wilk WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
75cb9c6f15SKonrad Rzeszutek Wilk cpu, per_cpu(lock_kicker_irq, cpu));
76cb9c6f15SKonrad Rzeszutek Wilk
77d5de8841SJeremy Fitzhardinge name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
78*ca84ce15SXiu Jianfeng per_cpu(irq_name, cpu) = name;
79d5de8841SJeremy Fitzhardinge irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
80d5de8841SJeremy Fitzhardinge cpu,
81d5de8841SJeremy Fitzhardinge dummy_handler,
829d71cee6SMichael Opdenacker IRQF_PERCPU|IRQF_NOBALANCING,
83d5de8841SJeremy Fitzhardinge name,
84d5de8841SJeremy Fitzhardinge NULL);
85d5de8841SJeremy Fitzhardinge
86d5de8841SJeremy Fitzhardinge if (irq >= 0) {
87d5de8841SJeremy Fitzhardinge disable_irq(irq); /* make sure it's never delivered */
88d5de8841SJeremy Fitzhardinge per_cpu(lock_kicker_irq, cpu) = irq;
89d5de8841SJeremy Fitzhardinge }
90d5de8841SJeremy Fitzhardinge
91d5de8841SJeremy Fitzhardinge printk("cpu %d spinlock event irq %d\n", cpu, irq);
92d5de8841SJeremy Fitzhardinge }
93d5de8841SJeremy Fitzhardinge
xen_uninit_lock_cpu(int cpu)94d68d82afSAlex Nixon void xen_uninit_lock_cpu(int cpu)
95d68d82afSAlex Nixon {
9665cae188SBrian Masney int irq;
9765cae188SBrian Masney
983310bbedSKonrad Rzeszutek Wilk if (!xen_pvspin)
993310bbedSKonrad Rzeszutek Wilk return;
1003310bbedSKonrad Rzeszutek Wilk
101*ca84ce15SXiu Jianfeng kfree(per_cpu(irq_name, cpu));
102*ca84ce15SXiu Jianfeng per_cpu(irq_name, cpu) = NULL;
10365cae188SBrian Masney /*
10465cae188SBrian Masney * When booting the kernel with 'mitigations=auto,nosmt', the secondary
10565cae188SBrian Masney * CPUs are not activated, and lock_kicker_irq is not initialized.
10665cae188SBrian Masney */
10765cae188SBrian Masney irq = per_cpu(lock_kicker_irq, cpu);
10865cae188SBrian Masney if (irq == -1)
10965cae188SBrian Masney return;
11065cae188SBrian Masney
11165cae188SBrian Masney unbind_from_irqhandler(irq, NULL);
112cb9c6f15SKonrad Rzeszutek Wilk per_cpu(lock_kicker_irq, cpu) = -1;
113d68d82afSAlex Nixon }
114d68d82afSAlex Nixon
1153cded417SPeter Zijlstra PV_CALLEE_SAVE_REGS_THUNK(xen_vcpu_stolen);
1163cded417SPeter Zijlstra
117a945928eSKonrad Rzeszutek Wilk /*
118a945928eSKonrad Rzeszutek Wilk * Our init of PV spinlocks is split in two init functions due to us
119a945928eSKonrad Rzeszutek Wilk * using paravirt patching and jump labels patching and having to do
120a945928eSKonrad Rzeszutek Wilk * all of this before SMP code is invoked.
121a945928eSKonrad Rzeszutek Wilk *
122a945928eSKonrad Rzeszutek Wilk * The paravirt patching needs to be done _before_ the alternative asm code
123a945928eSKonrad Rzeszutek Wilk * is started, otherwise we would not patch the core kernel code.
124a945928eSKonrad Rzeszutek Wilk */
xen_init_spinlocks(void)125d5de8841SJeremy Fitzhardinge void __init xen_init_spinlocks(void)
126d5de8841SJeremy Fitzhardinge {
12747b428d1SWaiman Long /* Don't need to use pvqspinlock code if there is only 1 vCPU. */
1289a3c05e6SZhenzhong Duan if (num_possible_cpus() == 1 || nopvspin)
12947b428d1SWaiman Long xen_pvspin = false;
13047b428d1SWaiman Long
131b8fa70b5SJeremy Fitzhardinge if (!xen_pvspin) {
132b8fa70b5SJeremy Fitzhardinge printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
133090d54bcSZhenzhong Duan static_branch_disable(&virt_spin_lock_key);
134b8fa70b5SJeremy Fitzhardinge return;
135b8fa70b5SJeremy Fitzhardinge }
136e0fc17a9SKonrad Rzeszutek Wilk printk(KERN_DEBUG "xen: PV spinlocks enabled\n");
137cfd8983fSPeter Zijlstra
138e95e6f17SDavid Vrabel __pv_init_lock_hash();
1395c83511bSJuergen Gross pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
1405c83511bSJuergen Gross pv_ops.lock.queued_spin_unlock =
1415c83511bSJuergen Gross PV_CALLEE_SAVE(__pv_queued_spin_unlock);
1425c83511bSJuergen Gross pv_ops.lock.wait = xen_qlock_wait;
1435c83511bSJuergen Gross pv_ops.lock.kick = xen_qlock_kick;
1445c83511bSJuergen Gross pv_ops.lock.vcpu_is_preempted = PV_CALLEE_SAVE(xen_vcpu_stolen);
145d5de8841SJeremy Fitzhardinge }
146994025caSJeremy Fitzhardinge
xen_parse_nopvspin(char * arg)147b8fa70b5SJeremy Fitzhardinge static __init int xen_parse_nopvspin(char *arg)
148b8fa70b5SJeremy Fitzhardinge {
1499a3c05e6SZhenzhong Duan pr_notice("\"xen_nopvspin\" is deprecated, please use \"nopvspin\" instead\n");
150b8fa70b5SJeremy Fitzhardinge xen_pvspin = false;
151b8fa70b5SJeremy Fitzhardinge return 0;
152b8fa70b5SJeremy Fitzhardinge }
153b8fa70b5SJeremy Fitzhardinge early_param("xen_nopvspin", xen_parse_nopvspin);
154b8fa70b5SJeremy Fitzhardinge
155