xref: /openbmc/linux/arch/x86/xen/spinlock.c (revision 3cded41794818d788aa1dc028ede4a1c1222d937)
1d5de8841SJeremy Fitzhardinge /*
2d5de8841SJeremy Fitzhardinge  * Split spinlock implementation out into its own file, so it can be
3d5de8841SJeremy Fitzhardinge  * compiled in a FTRACE-compatible way.
4d5de8841SJeremy Fitzhardinge  */
5d5de8841SJeremy Fitzhardinge #include <linux/kernel_stat.h>
6d5de8841SJeremy Fitzhardinge #include <linux/spinlock.h>
7994025caSJeremy Fitzhardinge #include <linux/debugfs.h>
8994025caSJeremy Fitzhardinge #include <linux/log2.h>
95a0e3ad6STejun Heo #include <linux/gfp.h>
10354e7b76SKonrad Rzeszutek Wilk #include <linux/slab.h>
11d5de8841SJeremy Fitzhardinge 
12d5de8841SJeremy Fitzhardinge #include <asm/paravirt.h>
13d5de8841SJeremy Fitzhardinge 
14d5de8841SJeremy Fitzhardinge #include <xen/interface/xen.h>
15d5de8841SJeremy Fitzhardinge #include <xen/events.h>
16d5de8841SJeremy Fitzhardinge 
17d5de8841SJeremy Fitzhardinge #include "xen-ops.h"
18994025caSJeremy Fitzhardinge #include "debugfs.h"
19994025caSJeremy Fitzhardinge 
20e95e6f17SDavid Vrabel static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
21e95e6f17SDavid Vrabel static DEFINE_PER_CPU(char *, irq_name);
22e95e6f17SDavid Vrabel static bool xen_pvspin = true;
23e95e6f17SDavid Vrabel 
24e95e6f17SDavid Vrabel #include <asm/qspinlock.h>
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 
833310bbedSKonrad Rzeszutek Wilk 	if (!xen_pvspin)
843310bbedSKonrad Rzeszutek Wilk 		return;
853310bbedSKonrad Rzeszutek Wilk 
86cb91f8f4SKonrad Rzeszutek Wilk 	WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
87cb9c6f15SKonrad Rzeszutek Wilk 	     cpu, per_cpu(lock_kicker_irq, cpu));
88cb9c6f15SKonrad Rzeszutek Wilk 
89d5de8841SJeremy Fitzhardinge 	name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
90d5de8841SJeremy Fitzhardinge 	irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
91d5de8841SJeremy Fitzhardinge 				     cpu,
92d5de8841SJeremy Fitzhardinge 				     dummy_handler,
939d71cee6SMichael Opdenacker 				     IRQF_PERCPU|IRQF_NOBALANCING,
94d5de8841SJeremy Fitzhardinge 				     name,
95d5de8841SJeremy Fitzhardinge 				     NULL);
96d5de8841SJeremy Fitzhardinge 
97d5de8841SJeremy Fitzhardinge 	if (irq >= 0) {
98d5de8841SJeremy Fitzhardinge 		disable_irq(irq); /* make sure it's never delivered */
99d5de8841SJeremy Fitzhardinge 		per_cpu(lock_kicker_irq, cpu) = irq;
100354e7b76SKonrad Rzeszutek Wilk 		per_cpu(irq_name, cpu) = name;
101d5de8841SJeremy Fitzhardinge 	}
102d5de8841SJeremy Fitzhardinge 
103d5de8841SJeremy Fitzhardinge 	printk("cpu %d spinlock event irq %d\n", cpu, irq);
104d5de8841SJeremy Fitzhardinge }
105d5de8841SJeremy Fitzhardinge 
106d68d82afSAlex Nixon void xen_uninit_lock_cpu(int cpu)
107d68d82afSAlex Nixon {
1083310bbedSKonrad Rzeszutek Wilk 	if (!xen_pvspin)
1093310bbedSKonrad Rzeszutek Wilk 		return;
1103310bbedSKonrad Rzeszutek Wilk 
111d68d82afSAlex Nixon 	unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL);
112cb9c6f15SKonrad Rzeszutek Wilk 	per_cpu(lock_kicker_irq, cpu) = -1;
113354e7b76SKonrad Rzeszutek Wilk 	kfree(per_cpu(irq_name, cpu));
114354e7b76SKonrad Rzeszutek Wilk 	per_cpu(irq_name, cpu) = NULL;
115d68d82afSAlex Nixon }
116d68d82afSAlex Nixon 
117*3cded417SPeter Zijlstra PV_CALLEE_SAVE_REGS_THUNK(xen_vcpu_stolen);
118*3cded417SPeter Zijlstra 
119a945928eSKonrad Rzeszutek Wilk /*
120a945928eSKonrad Rzeszutek Wilk  * Our init of PV spinlocks is split in two init functions due to us
121a945928eSKonrad Rzeszutek Wilk  * using paravirt patching and jump labels patching and having to do
122a945928eSKonrad Rzeszutek Wilk  * all of this before SMP code is invoked.
123a945928eSKonrad Rzeszutek Wilk  *
124a945928eSKonrad Rzeszutek Wilk  * The paravirt patching needs to be done _before_ the alternative asm code
125a945928eSKonrad Rzeszutek Wilk  * is started, otherwise we would not patch the core kernel code.
126a945928eSKonrad Rzeszutek Wilk  */
127d5de8841SJeremy Fitzhardinge void __init xen_init_spinlocks(void)
128d5de8841SJeremy Fitzhardinge {
12970dd4998SKonrad Rzeszutek Wilk 
130b8fa70b5SJeremy Fitzhardinge 	if (!xen_pvspin) {
131b8fa70b5SJeremy Fitzhardinge 		printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
132b8fa70b5SJeremy Fitzhardinge 		return;
133b8fa70b5SJeremy Fitzhardinge 	}
134e0fc17a9SKonrad Rzeszutek Wilk 	printk(KERN_DEBUG "xen: PV spinlocks enabled\n");
135cfd8983fSPeter Zijlstra 
136e95e6f17SDavid Vrabel 	__pv_init_lock_hash();
137e95e6f17SDavid Vrabel 	pv_lock_ops.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
138e95e6f17SDavid Vrabel 	pv_lock_ops.queued_spin_unlock = PV_CALLEE_SAVE(__pv_queued_spin_unlock);
139e95e6f17SDavid Vrabel 	pv_lock_ops.wait = xen_qlock_wait;
140e95e6f17SDavid Vrabel 	pv_lock_ops.kick = xen_qlock_kick;
141*3cded417SPeter Zijlstra 	pv_lock_ops.vcpu_is_preempted = PV_CALLEE_SAVE(xen_vcpu_stolen);
142d5de8841SJeremy Fitzhardinge }
143994025caSJeremy Fitzhardinge 
144a945928eSKonrad Rzeszutek Wilk /*
145a945928eSKonrad Rzeszutek Wilk  * While the jump_label init code needs to happend _after_ the jump labels are
146a945928eSKonrad Rzeszutek Wilk  * enabled and before SMP is started. Hence we use pre-SMP initcall level
147a945928eSKonrad Rzeszutek Wilk  * init. We cannot do it in xen_init_spinlocks as that is done before
148a945928eSKonrad Rzeszutek Wilk  * jump labels are activated.
149a945928eSKonrad Rzeszutek Wilk  */
150a945928eSKonrad Rzeszutek Wilk static __init int xen_init_spinlocks_jump(void)
151a945928eSKonrad Rzeszutek Wilk {
152a945928eSKonrad Rzeszutek Wilk 	if (!xen_pvspin)
153a945928eSKonrad Rzeszutek Wilk 		return 0;
154a945928eSKonrad Rzeszutek Wilk 
155e0fc17a9SKonrad Rzeszutek Wilk 	if (!xen_domain())
156e0fc17a9SKonrad Rzeszutek Wilk 		return 0;
157e0fc17a9SKonrad Rzeszutek Wilk 
158a945928eSKonrad Rzeszutek Wilk 	static_key_slow_inc(&paravirt_ticketlocks_enabled);
159a945928eSKonrad Rzeszutek Wilk 	return 0;
160a945928eSKonrad Rzeszutek Wilk }
161a945928eSKonrad Rzeszutek Wilk early_initcall(xen_init_spinlocks_jump);
162a945928eSKonrad Rzeszutek Wilk 
163b8fa70b5SJeremy Fitzhardinge static __init int xen_parse_nopvspin(char *arg)
164b8fa70b5SJeremy Fitzhardinge {
165b8fa70b5SJeremy Fitzhardinge 	xen_pvspin = false;
166b8fa70b5SJeremy Fitzhardinge 	return 0;
167b8fa70b5SJeremy Fitzhardinge }
168b8fa70b5SJeremy Fitzhardinge early_param("xen_nopvspin", xen_parse_nopvspin);
169b8fa70b5SJeremy Fitzhardinge 
170