blob: 3001162c4bce1495a10c3d17806b67e871c02536 [file] [log] [blame]
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001/*
2 * Xen hypercall batching.
3 *
4 * Xen allows multiple hypercalls to be issued at once, using the
5 * multicall interface. This allows the cost of trapping into the
6 * hypervisor to be amortized over several calls.
7 *
8 * This file implements a simple interface for multicalls. There's a
9 * per-cpu buffer of outstanding multicalls. When you want to queue a
10 * multicall for issuing, you can allocate a multicall slot for the
11 * call and its arguments, along with storage for space which is
12 * pointed to by the arguments (for passing pointers to structures,
13 * etc). When the multicall is actually issued, all the space for the
14 * commands and allocated memory is freed for reuse.
15 *
16 * Multicalls are flushed whenever any of the buffers get full, or
17 * when explicitly requested. There's no way to get per-multicall
18 * return results back. It will BUG if any of the multicalls fail.
19 *
20 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
21 */
22#include <linux/percpu.h>
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -070023#include <linux/hardirq.h>
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070024#include <linux/debugfs.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070025
26#include <asm/xen/hypercall.h>
27
28#include "multicalls.h"
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070029#include "debugfs.h"
30
31#define MC_BATCH 32
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070032
Jeremy Fitzhardingeffc78762010-12-17 17:19:42 -080033#define MC_DEBUG 0
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070034
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -070035#define MC_ARGS (MC_BATCH * 16)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070036
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070037
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070038struct mc_buffer {
39 struct multicall_entry entries[MC_BATCH];
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070040#if MC_DEBUG
41 struct multicall_entry debug[MC_BATCH];
Ian Campbellb93d51d2009-02-06 13:35:57 -080042 void *caller[MC_BATCH];
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070043#endif
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -070044 unsigned char args[MC_ARGS];
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -070045 struct callback {
46 void (*fn)(void *);
47 void *data;
48 } callbacks[MC_BATCH];
49 unsigned mcidx, argidx, cbidx;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070050};
51
52static DEFINE_PER_CPU(struct mc_buffer, mc_buffer);
53DEFINE_PER_CPU(unsigned long, xen_mc_irq_flags);
54
55void xen_mc_flush(void)
56{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -070057 struct mc_buffer *b = &__get_cpu_var(mc_buffer);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070058 int ret = 0;
59 unsigned long flags;
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -070060 int i;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070061
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -070062 BUG_ON(preemptible());
63
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070064 /* Disable interrupts in case someone comes in and queues
65 something in the middle */
66 local_irq_save(flags);
67
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -080068 trace_xen_mc_flush(b->mcidx, b->argidx, b->cbidx);
69
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070070 if (b->mcidx) {
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070071#if MC_DEBUG
72 memcpy(b->debug, b->entries,
73 b->mcidx * sizeof(struct multicall_entry));
74#endif
75
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070076 if (HYPERVISOR_multicall(b->entries, b->mcidx) != 0)
77 BUG();
78 for (i = 0; i < b->mcidx; i++)
79 if (b->entries[i].result < 0)
80 ret++;
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070081
82#if MC_DEBUG
83 if (ret) {
84 printk(KERN_ERR "%d multicall(s) failed: cpu %d\n",
85 ret, smp_processor_id());
Jeremy Fitzhardinge8ba6c2b2008-07-08 15:06:30 -070086 dump_stack();
Paolo Ciarrocchi7ebed392008-02-22 23:11:59 +010087 for (i = 0; i < b->mcidx; i++) {
Ian Campbellb93d51d2009-02-06 13:35:57 -080088 printk(KERN_DEBUG " call %2d/%d: op=%lu arg=[%lx] result=%ld\t%pF\n",
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070089 i+1, b->mcidx,
90 b->debug[i].op,
91 b->debug[i].args[0],
Ian Campbellb93d51d2009-02-06 13:35:57 -080092 b->entries[i].result,
93 b->caller[i]);
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070094 }
95 }
96#endif
97
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070098 b->mcidx = 0;
99 b->argidx = 0;
100 } else
101 BUG_ON(b->argidx != 0);
102
Paolo Ciarrocchi7ebed392008-02-22 23:11:59 +0100103 for (i = 0; i < b->cbidx; i++) {
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -0700104 struct callback *cb = &b->callbacks[i];
105
106 (*cb->fn)(cb->data);
107 }
108 b->cbidx = 0;
109
Jeremy Fitzhardingec9960862009-02-06 13:38:56 -0800110 local_irq_restore(flags);
111
Jeremy Fitzhardinge3d39e9d2009-02-06 13:38:51 -0800112 WARN_ON(ret);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700113}
114
115struct multicall_space __xen_mc_entry(size_t args)
116{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700117 struct mc_buffer *b = &__get_cpu_var(mc_buffer);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700118 struct multicall_space ret;
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700119 unsigned argidx = roundup(b->argidx, sizeof(u64));
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700120
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -0800121 trace_xen_mc_entry_alloc(args);
122
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700123 BUG_ON(preemptible());
Dan Carpenterf124c6a2011-06-03 07:45:28 +0300124 BUG_ON(b->argidx >= MC_ARGS);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700125
126 if (b->mcidx == MC_BATCH ||
Dan Carpenterf124c6a2011-06-03 07:45:28 +0300127 (argidx + args) >= MC_ARGS) {
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -0800128 trace_xen_mc_flush_reason((b->mcidx == MC_BATCH) ?
129 XEN_MC_FL_BATCH : XEN_MC_FL_ARGS);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700130 xen_mc_flush();
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700131 argidx = roundup(b->argidx, sizeof(u64));
132 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700133
134 ret.mc = &b->entries[b->mcidx];
Jeremy Fitzhardingeffc78762010-12-17 17:19:42 -0800135#if MC_DEBUG
Ian Campbellb93d51d2009-02-06 13:35:57 -0800136 b->caller[b->mcidx] = __builtin_return_address(0);
137#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700138 b->mcidx++;
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700139 ret.args = &b->args[argidx];
140 b->argidx = argidx + args;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700141
Dan Carpenterf124c6a2011-06-03 07:45:28 +0300142 BUG_ON(b->argidx >= MC_ARGS);
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700143 return ret;
144}
145
146struct multicall_space xen_mc_extend_args(unsigned long op, size_t size)
147{
148 struct mc_buffer *b = &__get_cpu_var(mc_buffer);
149 struct multicall_space ret = { NULL, NULL };
150
151 BUG_ON(preemptible());
Dan Carpenterf124c6a2011-06-03 07:45:28 +0300152 BUG_ON(b->argidx >= MC_ARGS);
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700153
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -0800154 if (unlikely(b->mcidx == 0 ||
155 b->entries[b->mcidx - 1].op != op)) {
156 trace_xen_mc_extend_args(op, size, XEN_MC_XE_BAD_OP);
157 goto out;
158 }
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700159
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -0800160 if (unlikely((b->argidx + size) >= MC_ARGS)) {
161 trace_xen_mc_extend_args(op, size, XEN_MC_XE_NO_SPACE);
162 goto out;
163 }
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700164
165 ret.mc = &b->entries[b->mcidx - 1];
166 ret.args = &b->args[b->argidx];
167 b->argidx += size;
168
Dan Carpenterf124c6a2011-06-03 07:45:28 +0300169 BUG_ON(b->argidx >= MC_ARGS);
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -0800170
171 trace_xen_mc_extend_args(op, size, XEN_MC_XE_OK);
172out:
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700173 return ret;
174}
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -0700175
176void xen_mc_callback(void (*fn)(void *), void *data)
177{
178 struct mc_buffer *b = &__get_cpu_var(mc_buffer);
179 struct callback *cb;
180
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -0800181 if (b->cbidx == MC_BATCH) {
182 trace_xen_mc_flush_reason(XEN_MC_FL_CALLBACK);
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -0700183 xen_mc_flush();
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -0800184 }
185
186 trace_xen_mc_callback(fn, data);
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -0700187
188 cb = &b->callbacks[b->cbidx++];
189 cb->fn = fn;
190 cb->data = data;
191}