]>
Commit | Line | Data |
---|---|---|
5ead97c8 JF |
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> | |
f120f13e | 23 | #include <linux/hardirq.h> |
5ead97c8 JF |
24 | |
25 | #include <asm/xen/hypercall.h> | |
26 | ||
27 | #include "multicalls.h" | |
28 | ||
d66bf8fc JF |
29 | #define MC_BATCH 32 |
30 | #define MC_ARGS (MC_BATCH * 16 / sizeof(u64)) | |
5ead97c8 JF |
31 | |
32 | struct mc_buffer { | |
33 | struct multicall_entry entries[MC_BATCH]; | |
34 | u64 args[MC_ARGS]; | |
91e0c5f3 JF |
35 | struct callback { |
36 | void (*fn)(void *); | |
37 | void *data; | |
38 | } callbacks[MC_BATCH]; | |
39 | unsigned mcidx, argidx, cbidx; | |
5ead97c8 JF |
40 | }; |
41 | ||
42 | static DEFINE_PER_CPU(struct mc_buffer, mc_buffer); | |
43 | DEFINE_PER_CPU(unsigned long, xen_mc_irq_flags); | |
44 | ||
45 | void xen_mc_flush(void) | |
46 | { | |
f120f13e | 47 | struct mc_buffer *b = &__get_cpu_var(mc_buffer); |
5ead97c8 JF |
48 | int ret = 0; |
49 | unsigned long flags; | |
91e0c5f3 | 50 | int i; |
5ead97c8 | 51 | |
f120f13e JF |
52 | BUG_ON(preemptible()); |
53 | ||
5ead97c8 JF |
54 | /* Disable interrupts in case someone comes in and queues |
55 | something in the middle */ | |
56 | local_irq_save(flags); | |
57 | ||
58 | if (b->mcidx) { | |
5ead97c8 JF |
59 | if (HYPERVISOR_multicall(b->entries, b->mcidx) != 0) |
60 | BUG(); | |
61 | for (i = 0; i < b->mcidx; i++) | |
62 | if (b->entries[i].result < 0) | |
63 | ret++; | |
64 | b->mcidx = 0; | |
65 | b->argidx = 0; | |
66 | } else | |
67 | BUG_ON(b->argidx != 0); | |
68 | ||
5ead97c8 JF |
69 | local_irq_restore(flags); |
70 | ||
91e0c5f3 JF |
71 | for(i = 0; i < b->cbidx; i++) { |
72 | struct callback *cb = &b->callbacks[i]; | |
73 | ||
74 | (*cb->fn)(cb->data); | |
75 | } | |
76 | b->cbidx = 0; | |
77 | ||
5ead97c8 JF |
78 | BUG_ON(ret); |
79 | } | |
80 | ||
81 | struct multicall_space __xen_mc_entry(size_t args) | |
82 | { | |
f120f13e | 83 | struct mc_buffer *b = &__get_cpu_var(mc_buffer); |
5ead97c8 JF |
84 | struct multicall_space ret; |
85 | unsigned argspace = (args + sizeof(u64) - 1) / sizeof(u64); | |
86 | ||
f120f13e | 87 | BUG_ON(preemptible()); |
5ead97c8 JF |
88 | BUG_ON(argspace > MC_ARGS); |
89 | ||
90 | if (b->mcidx == MC_BATCH || | |
91 | (b->argidx + argspace) > MC_ARGS) | |
92 | xen_mc_flush(); | |
93 | ||
94 | ret.mc = &b->entries[b->mcidx]; | |
95 | b->mcidx++; | |
96 | ret.args = &b->args[b->argidx]; | |
97 | b->argidx += argspace; | |
98 | ||
5ead97c8 JF |
99 | return ret; |
100 | } | |
91e0c5f3 JF |
101 | |
102 | void xen_mc_callback(void (*fn)(void *), void *data) | |
103 | { | |
104 | struct mc_buffer *b = &__get_cpu_var(mc_buffer); | |
105 | struct callback *cb; | |
106 | ||
107 | if (b->cbidx == MC_BATCH) | |
108 | xen_mc_flush(); | |
109 | ||
110 | cb = &b->callbacks[b->cbidx++]; | |
111 | cb->fn = fn; | |
112 | cb->data = data; | |
113 | } |