blob: 9efd1c6c9776c5f9e8973026dd03b111c6e9e585 [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 Fitzhardinge5ead97c2007-07-17 18:37:04 -070024
25#include <asm/xen/hypercall.h>
26
27#include "multicalls.h"
28
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070029#define MC_DEBUG 1
30
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -070031#define MC_BATCH 32
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -070032#define MC_ARGS (MC_BATCH * 16)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070033
34struct mc_buffer {
35 struct multicall_entry entries[MC_BATCH];
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070036#if MC_DEBUG
37 struct multicall_entry debug[MC_BATCH];
38#endif
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -070039 unsigned char args[MC_ARGS];
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -070040 struct callback {
41 void (*fn)(void *);
42 void *data;
43 } callbacks[MC_BATCH];
44 unsigned mcidx, argidx, cbidx;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070045};
46
47static DEFINE_PER_CPU(struct mc_buffer, mc_buffer);
48DEFINE_PER_CPU(unsigned long, xen_mc_irq_flags);
49
50void xen_mc_flush(void)
51{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -070052 struct mc_buffer *b = &__get_cpu_var(mc_buffer);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070053 int ret = 0;
54 unsigned long flags;
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -070055 int i;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070056
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -070057 BUG_ON(preemptible());
58
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070059 /* Disable interrupts in case someone comes in and queues
60 something in the middle */
61 local_irq_save(flags);
62
63 if (b->mcidx) {
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070064#if MC_DEBUG
65 memcpy(b->debug, b->entries,
66 b->mcidx * sizeof(struct multicall_entry));
67#endif
68
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070069 if (HYPERVISOR_multicall(b->entries, b->mcidx) != 0)
70 BUG();
71 for (i = 0; i < b->mcidx; i++)
72 if (b->entries[i].result < 0)
73 ret++;
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070074
75#if MC_DEBUG
76 if (ret) {
77 printk(KERN_ERR "%d multicall(s) failed: cpu %d\n",
78 ret, smp_processor_id());
Jeremy Fitzhardinge8ba6c2b2008-07-08 15:06:30 -070079 dump_stack();
Paolo Ciarrocchi7ebed392008-02-22 23:11:59 +010080 for (i = 0; i < b->mcidx; i++) {
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070081 printk(" call %2d/%d: op=%lu arg=[%lx] result=%ld\n",
82 i+1, b->mcidx,
83 b->debug[i].op,
84 b->debug[i].args[0],
85 b->entries[i].result);
86 }
87 }
88#endif
89
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070090 b->mcidx = 0;
91 b->argidx = 0;
92 } else
93 BUG_ON(b->argidx != 0);
94
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070095 local_irq_restore(flags);
96
Paolo Ciarrocchi7ebed392008-02-22 23:11:59 +010097 for (i = 0; i < b->cbidx; i++) {
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -070098 struct callback *cb = &b->callbacks[i];
99
100 (*cb->fn)(cb->data);
101 }
102 b->cbidx = 0;
103
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700104 BUG_ON(ret);
105}
106
107struct multicall_space __xen_mc_entry(size_t args)
108{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700109 struct mc_buffer *b = &__get_cpu_var(mc_buffer);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700110 struct multicall_space ret;
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700111 unsigned argidx = roundup(b->argidx, sizeof(u64));
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700112
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700113 BUG_ON(preemptible());
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700114 BUG_ON(b->argidx > MC_ARGS);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700115
116 if (b->mcidx == MC_BATCH ||
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700117 (argidx + args) > MC_ARGS) {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700118 xen_mc_flush();
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700119 argidx = roundup(b->argidx, sizeof(u64));
120 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700121
122 ret.mc = &b->entries[b->mcidx];
123 b->mcidx++;
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700124 ret.args = &b->args[argidx];
125 b->argidx = argidx + args;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700126
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700127 BUG_ON(b->argidx > MC_ARGS);
128 return ret;
129}
130
131struct multicall_space xen_mc_extend_args(unsigned long op, size_t size)
132{
133 struct mc_buffer *b = &__get_cpu_var(mc_buffer);
134 struct multicall_space ret = { NULL, NULL };
135
136 BUG_ON(preemptible());
137 BUG_ON(b->argidx > MC_ARGS);
138
139 if (b->mcidx == 0)
140 return ret;
141
142 if (b->entries[b->mcidx - 1].op != op)
143 return ret;
144
145 if ((b->argidx + size) > MC_ARGS)
146 return ret;
147
148 ret.mc = &b->entries[b->mcidx - 1];
149 ret.args = &b->args[b->argidx];
150 b->argidx += size;
151
152 BUG_ON(b->argidx > MC_ARGS);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700153 return ret;
154}
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -0700155
156void xen_mc_callback(void (*fn)(void *), void *data)
157{
158 struct mc_buffer *b = &__get_cpu_var(mc_buffer);
159 struct callback *cb;
160
161 if (b->cbidx == MC_BATCH)
162 xen_mc_flush();
163
164 cb = &b->callbacks[b->cbidx++];
165 cb->fn = fn;
166 cb->data = data;
167}