xref: /openbmc/linux/arch/x86/xen/spinlock.c (revision 20a2742e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Split spinlock implementation out into its own file, so it can be
4  * compiled in a FTRACE-compatible way.
5  */
6 #include <linux/kernel_stat.h>
7 #include <linux/spinlock.h>
8 #include <linux/debugfs.h>
9 #include <linux/log2.h>
10 #include <linux/gfp.h>
11 #include <linux/slab.h>
12 
13 #include <asm/paravirt.h>
14 #include <asm/qspinlock.h>
15 
16 #include <xen/interface/xen.h>
17 #include <xen/events.h>
18 
19 #include "xen-ops.h"
20 #include "debugfs.h"
21 
22 static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
23 static DEFINE_PER_CPU(char *, irq_name);
24 static bool xen_pvspin = true;
25 
26 #include <asm/qspinlock.h>
27 
28 static void xen_qlock_kick(int cpu)
29 {
30 	int irq = per_cpu(lock_kicker_irq, cpu);
31 
32 	/* Don't kick if the target's kicker interrupt is not initialized. */
33 	if (irq == -1)
34 		return;
35 
36 	xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
37 }
38 
39 /*
40  * Halt the current CPU & release it back to the host
41  */
42 static void xen_qlock_wait(u8 *byte, u8 val)
43 {
44 	int irq = __this_cpu_read(lock_kicker_irq);
45 
46 	/* If kicker interrupts not initialized yet, just spin */
47 	if (irq == -1)
48 		return;
49 
50 	/* clear pending */
51 	xen_clear_irq_pending(irq);
52 	barrier();
53 
54 	/*
55 	 * We check the byte value after clearing pending IRQ to make sure
56 	 * that we won't miss a wakeup event because of the clearing.
57 	 *
58 	 * The sync_clear_bit() call in xen_clear_irq_pending() is atomic.
59 	 * So it is effectively a memory barrier for x86.
60 	 */
61 	if (READ_ONCE(*byte) != val)
62 		return;
63 
64 	/*
65 	 * If an interrupt happens here, it will leave the wakeup irq
66 	 * pending, which will cause xen_poll_irq() to return
67 	 * immediately.
68 	 */
69 
70 	/* Block until irq becomes pending (or perhaps a spurious wakeup) */
71 	xen_poll_irq(irq);
72 }
73 
74 static irqreturn_t dummy_handler(int irq, void *dev_id)
75 {
76 	BUG();
77 	return IRQ_HANDLED;
78 }
79 
80 void xen_init_lock_cpu(int cpu)
81 {
82 	int irq;
83 	char *name;
84 
85 	if (!xen_pvspin) {
86 		if (cpu == 0)
87 			static_branch_disable(&virt_spin_lock_key);
88 		return;
89 	}
90 
91 	WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
92 	     cpu, per_cpu(lock_kicker_irq, cpu));
93 
94 	name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
95 	irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
96 				     cpu,
97 				     dummy_handler,
98 				     IRQF_PERCPU|IRQF_NOBALANCING,
99 				     name,
100 				     NULL);
101 
102 	if (irq >= 0) {
103 		disable_irq(irq); /* make sure it's never delivered */
104 		per_cpu(lock_kicker_irq, cpu) = irq;
105 		per_cpu(irq_name, cpu) = name;
106 	}
107 
108 	printk("cpu %d spinlock event irq %d\n", cpu, irq);
109 }
110 
111 void xen_uninit_lock_cpu(int cpu)
112 {
113 	if (!xen_pvspin)
114 		return;
115 
116 	unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL);
117 	per_cpu(lock_kicker_irq, cpu) = -1;
118 	kfree(per_cpu(irq_name, cpu));
119 	per_cpu(irq_name, cpu) = NULL;
120 }
121 
122 PV_CALLEE_SAVE_REGS_THUNK(xen_vcpu_stolen);
123 
124 /*
125  * Our init of PV spinlocks is split in two init functions due to us
126  * using paravirt patching and jump labels patching and having to do
127  * all of this before SMP code is invoked.
128  *
129  * The paravirt patching needs to be done _before_ the alternative asm code
130  * is started, otherwise we would not patch the core kernel code.
131  */
132 void __init xen_init_spinlocks(void)
133 {
134 
135 	if (!xen_pvspin) {
136 		printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
137 		return;
138 	}
139 	printk(KERN_DEBUG "xen: PV spinlocks enabled\n");
140 
141 	__pv_init_lock_hash();
142 	pv_lock_ops.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
143 	pv_lock_ops.queued_spin_unlock = PV_CALLEE_SAVE(__pv_queued_spin_unlock);
144 	pv_lock_ops.wait = xen_qlock_wait;
145 	pv_lock_ops.kick = xen_qlock_kick;
146 	pv_lock_ops.vcpu_is_preempted = PV_CALLEE_SAVE(xen_vcpu_stolen);
147 }
148 
149 static __init int xen_parse_nopvspin(char *arg)
150 {
151 	xen_pvspin = false;
152 	return 0;
153 }
154 early_param("xen_nopvspin", xen_parse_nopvspin);
155 
156