xref: /openbmc/linux/arch/x86/xen/multicalls.c (revision c39f2d9db0fd81ea20bb5cce9b3f082ca63753e2)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
29702785aSThomas Gleixner /*
39702785aSThomas Gleixner  * Xen hypercall batching.
49702785aSThomas Gleixner  *
59702785aSThomas Gleixner  * Xen allows multiple hypercalls to be issued at once, using the
69702785aSThomas Gleixner  * multicall interface.  This allows the cost of trapping into the
79702785aSThomas Gleixner  * hypervisor to be amortized over several calls.
89702785aSThomas Gleixner  *
99702785aSThomas Gleixner  * This file implements a simple interface for multicalls.  There's a
109702785aSThomas Gleixner  * per-cpu buffer of outstanding multicalls.  When you want to queue a
119702785aSThomas Gleixner  * multicall for issuing, you can allocate a multicall slot for the
129702785aSThomas Gleixner  * call and its arguments, along with storage for space which is
139702785aSThomas Gleixner  * pointed to by the arguments (for passing pointers to structures,
149702785aSThomas Gleixner  * etc).  When the multicall is actually issued, all the space for the
159702785aSThomas Gleixner  * commands and allocated memory is freed for reuse.
169702785aSThomas Gleixner  *
179702785aSThomas Gleixner  * Multicalls are flushed whenever any of the buffers get full, or
189702785aSThomas Gleixner  * when explicitly requested.  There's no way to get per-multicall
199702785aSThomas Gleixner  * return results back.  It will BUG if any of the multicalls fail.
209702785aSThomas Gleixner  *
219702785aSThomas Gleixner  * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
229702785aSThomas Gleixner  */
239702785aSThomas Gleixner #include <linux/percpu.h>
249702785aSThomas Gleixner #include <linux/hardirq.h>
25994025caSJeremy Fitzhardinge #include <linux/debugfs.h>
269702785aSThomas Gleixner 
279702785aSThomas Gleixner #include <asm/xen/hypercall.h>
289702785aSThomas Gleixner 
299702785aSThomas Gleixner #include "multicalls.h"
30994025caSJeremy Fitzhardinge #include "debugfs.h"
31994025caSJeremy Fitzhardinge 
32994025caSJeremy Fitzhardinge #define MC_BATCH	32
339702785aSThomas Gleixner 
34ffc78767SJeremy Fitzhardinge #define MC_DEBUG	0
35a122d623SJeremy Fitzhardinge 
36400d3494SJeremy Fitzhardinge #define MC_ARGS		(MC_BATCH * 16)
379702785aSThomas Gleixner 
38994025caSJeremy Fitzhardinge 
399702785aSThomas Gleixner struct mc_buffer {
402a6f6d09SJeremy Fitzhardinge 	unsigned mcidx, argidx, cbidx;
419702785aSThomas Gleixner 	struct multicall_entry entries[MC_BATCH];
42a122d623SJeremy Fitzhardinge #if MC_DEBUG
43a122d623SJeremy Fitzhardinge 	struct multicall_entry debug[MC_BATCH];
44b93d51dcSIan Campbell 	void *caller[MC_BATCH];
45a122d623SJeremy Fitzhardinge #endif
46400d3494SJeremy Fitzhardinge 	unsigned char args[MC_ARGS];
4791e0c5f3SJeremy Fitzhardinge 	struct callback {
4891e0c5f3SJeremy Fitzhardinge 		void (*fn)(void *);
4991e0c5f3SJeremy Fitzhardinge 		void *data;
5091e0c5f3SJeremy Fitzhardinge 	} callbacks[MC_BATCH];
519702785aSThomas Gleixner };
529702785aSThomas Gleixner 
539702785aSThomas Gleixner static DEFINE_PER_CPU(struct mc_buffer, mc_buffer);
549702785aSThomas Gleixner DEFINE_PER_CPU(unsigned long, xen_mc_irq_flags);
559702785aSThomas Gleixner 
xen_mc_flush(void)569702785aSThomas Gleixner void xen_mc_flush(void)
579702785aSThomas Gleixner {
5889cbc767SChristoph Lameter 	struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
59eac303bfSJeremy Fitzhardinge 	struct multicall_entry *mc;
609702785aSThomas Gleixner 	int ret = 0;
619702785aSThomas Gleixner 	unsigned long flags;
6291e0c5f3SJeremy Fitzhardinge 	int i;
639702785aSThomas Gleixner 
649702785aSThomas Gleixner 	BUG_ON(preemptible());
659702785aSThomas Gleixner 
669702785aSThomas Gleixner 	/* Disable interrupts in case someone comes in and queues
679702785aSThomas Gleixner 	   something in the middle */
689702785aSThomas Gleixner 	local_irq_save(flags);
699702785aSThomas Gleixner 
70c796f213SJeremy Fitzhardinge 	trace_xen_mc_flush(b->mcidx, b->argidx, b->cbidx);
71c796f213SJeremy Fitzhardinge 
72a7b40310SJuergen Gross #if MC_DEBUG
73a7b40310SJuergen Gross 	memcpy(b->debug, b->entries,
74a7b40310SJuergen Gross 	       b->mcidx * sizeof(struct multicall_entry));
75a7b40310SJuergen Gross #endif
76a7b40310SJuergen Gross 
77eac303bfSJeremy Fitzhardinge 	switch (b->mcidx) {
78eac303bfSJeremy Fitzhardinge 	case 0:
79eac303bfSJeremy Fitzhardinge 		/* no-op */
80eac303bfSJeremy Fitzhardinge 		BUG_ON(b->argidx != 0);
81eac303bfSJeremy Fitzhardinge 		break;
82eac303bfSJeremy Fitzhardinge 
83eac303bfSJeremy Fitzhardinge 	case 1:
84eac303bfSJeremy Fitzhardinge 		/* Singleton multicall - bypass multicall machinery
85eac303bfSJeremy Fitzhardinge 		   and just do the call directly. */
86eac303bfSJeremy Fitzhardinge 		mc = &b->entries[0];
87eac303bfSJeremy Fitzhardinge 
88cd913922SJuergen Gross 		mc->result = xen_single_call(mc->op, mc->args[0], mc->args[1],
89cd913922SJuergen Gross 					     mc->args[2], mc->args[3],
90cd913922SJuergen Gross 					     mc->args[4]);
91eac303bfSJeremy Fitzhardinge 		ret = mc->result < 0;
92eac303bfSJeremy Fitzhardinge 		break;
93eac303bfSJeremy Fitzhardinge 
94eac303bfSJeremy Fitzhardinge 	default:
959702785aSThomas Gleixner 		if (HYPERVISOR_multicall(b->entries, b->mcidx) != 0)
969702785aSThomas Gleixner 			BUG();
979702785aSThomas Gleixner 		for (i = 0; i < b->mcidx; i++)
989702785aSThomas Gleixner 			if (b->entries[i].result < 0)
999702785aSThomas Gleixner 				ret++;
100a7b40310SJuergen Gross 	}
101a122d623SJeremy Fitzhardinge 
102a7b40310SJuergen Gross 	if (WARN_ON(ret)) {
103a7b40310SJuergen Gross 		pr_err("%d of %d multicall(s) failed: cpu %d\n",
104a7b40310SJuergen Gross 		       ret, b->mcidx, smp_processor_id());
105a122d623SJeremy Fitzhardinge 		for (i = 0; i < b->mcidx; i++) {
106a7b40310SJuergen Gross 			if (b->entries[i].result < 0) {
107a7b40310SJuergen Gross #if MC_DEBUG
108*d75f773cSSakari Ailus 				pr_err("  call %2d: op=%lu arg=[%lx] result=%ld\t%pS\n",
109a7b40310SJuergen Gross 				       i + 1,
110a122d623SJeremy Fitzhardinge 				       b->debug[i].op,
111a122d623SJeremy Fitzhardinge 				       b->debug[i].args[0],
112b93d51dcSIan Campbell 				       b->entries[i].result,
113b93d51dcSIan Campbell 				       b->caller[i]);
114a7b40310SJuergen Gross #else
115a7b40310SJuergen Gross 				pr_err("  call %2d: op=%lu arg=[%lx] result=%ld\n",
116a7b40310SJuergen Gross 				       i + 1,
117a7b40310SJuergen Gross 				       b->entries[i].op,
118a7b40310SJuergen Gross 				       b->entries[i].args[0],
119a7b40310SJuergen Gross 				       b->entries[i].result);
120a122d623SJeremy Fitzhardinge #endif
121eac303bfSJeremy Fitzhardinge 			}
122a7b40310SJuergen Gross 		}
123a7b40310SJuergen Gross 	}
124a122d623SJeremy Fitzhardinge 
1259702785aSThomas Gleixner 	b->mcidx = 0;
1269702785aSThomas Gleixner 	b->argidx = 0;
1279702785aSThomas Gleixner 
12891e0c5f3SJeremy Fitzhardinge 	for (i = 0; i < b->cbidx; i++) {
12991e0c5f3SJeremy Fitzhardinge 		struct callback *cb = &b->callbacks[i];
13091e0c5f3SJeremy Fitzhardinge 
13191e0c5f3SJeremy Fitzhardinge 		(*cb->fn)(cb->data);
13291e0c5f3SJeremy Fitzhardinge 	}
13391e0c5f3SJeremy Fitzhardinge 	b->cbidx = 0;
13491e0c5f3SJeremy Fitzhardinge 
135c9960863SJeremy Fitzhardinge 	local_irq_restore(flags);
1369702785aSThomas Gleixner }
1379702785aSThomas Gleixner 
__xen_mc_entry(size_t args)1389702785aSThomas Gleixner struct multicall_space __xen_mc_entry(size_t args)
1399702785aSThomas Gleixner {
14089cbc767SChristoph Lameter 	struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
1419702785aSThomas Gleixner 	struct multicall_space ret;
142400d3494SJeremy Fitzhardinge 	unsigned argidx = roundup(b->argidx, sizeof(u64));
1439702785aSThomas Gleixner 
144c796f213SJeremy Fitzhardinge 	trace_xen_mc_entry_alloc(args);
145c796f213SJeremy Fitzhardinge 
1469702785aSThomas Gleixner 	BUG_ON(preemptible());
147f124c6aeSDan Carpenter 	BUG_ON(b->argidx >= MC_ARGS);
1489702785aSThomas Gleixner 
1494a7b005dSJeremy Fitzhardinge 	if (unlikely(b->mcidx == MC_BATCH ||
1504a7b005dSJeremy Fitzhardinge 		     (argidx + args) >= MC_ARGS)) {
151c796f213SJeremy Fitzhardinge 		trace_xen_mc_flush_reason((b->mcidx == MC_BATCH) ?
152c796f213SJeremy Fitzhardinge 					  XEN_MC_FL_BATCH : XEN_MC_FL_ARGS);
1539702785aSThomas Gleixner 		xen_mc_flush();
154400d3494SJeremy Fitzhardinge 		argidx = roundup(b->argidx, sizeof(u64));
155400d3494SJeremy Fitzhardinge 	}
1569702785aSThomas Gleixner 
1579702785aSThomas Gleixner 	ret.mc = &b->entries[b->mcidx];
158ffc78767SJeremy Fitzhardinge #if MC_DEBUG
159b93d51dcSIan Campbell 	b->caller[b->mcidx] = __builtin_return_address(0);
160b93d51dcSIan Campbell #endif
1619702785aSThomas Gleixner 	b->mcidx++;
162400d3494SJeremy Fitzhardinge 	ret.args = &b->args[argidx];
163400d3494SJeremy Fitzhardinge 	b->argidx = argidx + args;
1649702785aSThomas Gleixner 
165f124c6aeSDan Carpenter 	BUG_ON(b->argidx >= MC_ARGS);
166400d3494SJeremy Fitzhardinge 	return ret;
167400d3494SJeremy Fitzhardinge }
168400d3494SJeremy Fitzhardinge 
xen_mc_extend_args(unsigned long op,size_t size)169400d3494SJeremy Fitzhardinge struct multicall_space xen_mc_extend_args(unsigned long op, size_t size)
170400d3494SJeremy Fitzhardinge {
17189cbc767SChristoph Lameter 	struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
172400d3494SJeremy Fitzhardinge 	struct multicall_space ret = { NULL, NULL };
173400d3494SJeremy Fitzhardinge 
174400d3494SJeremy Fitzhardinge 	BUG_ON(preemptible());
175f124c6aeSDan Carpenter 	BUG_ON(b->argidx >= MC_ARGS);
176400d3494SJeremy Fitzhardinge 
177c796f213SJeremy Fitzhardinge 	if (unlikely(b->mcidx == 0 ||
178c796f213SJeremy Fitzhardinge 		     b->entries[b->mcidx - 1].op != op)) {
179c796f213SJeremy Fitzhardinge 		trace_xen_mc_extend_args(op, size, XEN_MC_XE_BAD_OP);
180c796f213SJeremy Fitzhardinge 		goto out;
181c796f213SJeremy Fitzhardinge 	}
182400d3494SJeremy Fitzhardinge 
183c796f213SJeremy Fitzhardinge 	if (unlikely((b->argidx + size) >= MC_ARGS)) {
184c796f213SJeremy Fitzhardinge 		trace_xen_mc_extend_args(op, size, XEN_MC_XE_NO_SPACE);
185c796f213SJeremy Fitzhardinge 		goto out;
186c796f213SJeremy Fitzhardinge 	}
187400d3494SJeremy Fitzhardinge 
188400d3494SJeremy Fitzhardinge 	ret.mc = &b->entries[b->mcidx - 1];
189400d3494SJeremy Fitzhardinge 	ret.args = &b->args[b->argidx];
190400d3494SJeremy Fitzhardinge 	b->argidx += size;
191400d3494SJeremy Fitzhardinge 
192f124c6aeSDan Carpenter 	BUG_ON(b->argidx >= MC_ARGS);
193c796f213SJeremy Fitzhardinge 
194c796f213SJeremy Fitzhardinge 	trace_xen_mc_extend_args(op, size, XEN_MC_XE_OK);
195c796f213SJeremy Fitzhardinge out:
1969702785aSThomas Gleixner 	return ret;
1979702785aSThomas Gleixner }
19891e0c5f3SJeremy Fitzhardinge 
xen_mc_callback(void (* fn)(void *),void * data)19991e0c5f3SJeremy Fitzhardinge void xen_mc_callback(void (*fn)(void *), void *data)
20091e0c5f3SJeremy Fitzhardinge {
20189cbc767SChristoph Lameter 	struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
20291e0c5f3SJeremy Fitzhardinge 	struct callback *cb;
20391e0c5f3SJeremy Fitzhardinge 
204c796f213SJeremy Fitzhardinge 	if (b->cbidx == MC_BATCH) {
205c796f213SJeremy Fitzhardinge 		trace_xen_mc_flush_reason(XEN_MC_FL_CALLBACK);
20691e0c5f3SJeremy Fitzhardinge 		xen_mc_flush();
207c796f213SJeremy Fitzhardinge 	}
208c796f213SJeremy Fitzhardinge 
209c796f213SJeremy Fitzhardinge 	trace_xen_mc_callback(fn, data);
21091e0c5f3SJeremy Fitzhardinge 
21191e0c5f3SJeremy Fitzhardinge 	cb = &b->callbacks[b->cbidx++];
21291e0c5f3SJeremy Fitzhardinge 	cb->fn = fn;
21391e0c5f3SJeremy Fitzhardinge 	cb->data = data;
21491e0c5f3SJeremy Fitzhardinge }
215