xen: event channels
[safe/jmp/linux-2.6] / arch / i386 / xen / multicalls.c
1 /*
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>
23
24 #include <asm/xen/hypercall.h>
25
26 #include "multicalls.h"
27
28 #define MC_BATCH        8
29 #define MC_ARGS         (MC_BATCH * 32 / sizeof(u64))
30
31 struct mc_buffer {
32         struct multicall_entry entries[MC_BATCH];
33         u64 args[MC_ARGS];
34         unsigned mcidx, argidx;
35 };
36
37 static DEFINE_PER_CPU(struct mc_buffer, mc_buffer);
38 DEFINE_PER_CPU(unsigned long, xen_mc_irq_flags);
39
40 void xen_mc_flush(void)
41 {
42         struct mc_buffer *b = &get_cpu_var(mc_buffer);
43         int ret = 0;
44         unsigned long flags;
45
46         /* Disable interrupts in case someone comes in and queues
47            something in the middle */
48         local_irq_save(flags);
49
50         if (b->mcidx) {
51                 int i;
52
53                 if (HYPERVISOR_multicall(b->entries, b->mcidx) != 0)
54                         BUG();
55                 for (i = 0; i < b->mcidx; i++)
56                         if (b->entries[i].result < 0)
57                                 ret++;
58                 b->mcidx = 0;
59                 b->argidx = 0;
60         } else
61                 BUG_ON(b->argidx != 0);
62
63         put_cpu_var(mc_buffer);
64         local_irq_restore(flags);
65
66         BUG_ON(ret);
67 }
68
69 struct multicall_space __xen_mc_entry(size_t args)
70 {
71         struct mc_buffer *b = &get_cpu_var(mc_buffer);
72         struct multicall_space ret;
73         unsigned argspace = (args + sizeof(u64) - 1) / sizeof(u64);
74
75         BUG_ON(argspace > MC_ARGS);
76
77         if (b->mcidx == MC_BATCH ||
78             (b->argidx + argspace) > MC_ARGS)
79                 xen_mc_flush();
80
81         ret.mc = &b->entries[b->mcidx];
82         b->mcidx++;
83         ret.args = &b->args[b->argidx];
84         b->argidx += argspace;
85
86         put_cpu_var(mc_buffer);
87
88         return ret;
89 }