Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 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> |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 23 | #include <linux/hardirq.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 24 | |
| 25 | #include <asm/xen/hypercall.h> |
| 26 | |
| 27 | #include "multicalls.h" |
| 28 | |
Jeremy Fitzhardinge | a122d62 | 2007-10-16 11:51:31 -0700 | [diff] [blame] | 29 | #define MC_DEBUG 1 |
| 30 | |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 31 | #define MC_BATCH 32 |
| 32 | #define MC_ARGS (MC_BATCH * 16 / sizeof(u64)) |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 33 | |
| 34 | struct mc_buffer { |
| 35 | struct multicall_entry entries[MC_BATCH]; |
Jeremy Fitzhardinge | a122d62 | 2007-10-16 11:51:31 -0700 | [diff] [blame] | 36 | #if MC_DEBUG |
| 37 | struct multicall_entry debug[MC_BATCH]; |
| 38 | #endif |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 39 | u64 args[MC_ARGS]; |
Jeremy Fitzhardinge | 91e0c5f | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 40 | struct callback { |
| 41 | void (*fn)(void *); |
| 42 | void *data; |
| 43 | } callbacks[MC_BATCH]; |
| 44 | unsigned mcidx, argidx, cbidx; |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static DEFINE_PER_CPU(struct mc_buffer, mc_buffer); |
| 48 | DEFINE_PER_CPU(unsigned long, xen_mc_irq_flags); |
| 49 | |
| 50 | void xen_mc_flush(void) |
| 51 | { |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 52 | struct mc_buffer *b = &__get_cpu_var(mc_buffer); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 53 | int ret = 0; |
| 54 | unsigned long flags; |
Jeremy Fitzhardinge | 91e0c5f | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 55 | int i; |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 56 | |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 57 | BUG_ON(preemptible()); |
| 58 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 59 | /* 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 Fitzhardinge | a122d62 | 2007-10-16 11:51:31 -0700 | [diff] [blame] | 64 | #if MC_DEBUG |
| 65 | memcpy(b->debug, b->entries, |
| 66 | b->mcidx * sizeof(struct multicall_entry)); |
| 67 | #endif |
| 68 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 69 | 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 Fitzhardinge | a122d62 | 2007-10-16 11:51:31 -0700 | [diff] [blame] | 74 | |
| 75 | #if MC_DEBUG |
| 76 | if (ret) { |
| 77 | printk(KERN_ERR "%d multicall(s) failed: cpu %d\n", |
| 78 | ret, smp_processor_id()); |
| 79 | for(i = 0; i < b->mcidx; i++) { |
| 80 | printk(" call %2d/%d: op=%lu arg=[%lx] result=%ld\n", |
| 81 | i+1, b->mcidx, |
| 82 | b->debug[i].op, |
| 83 | b->debug[i].args[0], |
| 84 | b->entries[i].result); |
| 85 | } |
| 86 | } |
| 87 | #endif |
| 88 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 89 | b->mcidx = 0; |
| 90 | b->argidx = 0; |
| 91 | } else |
| 92 | BUG_ON(b->argidx != 0); |
| 93 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 94 | local_irq_restore(flags); |
| 95 | |
Jeremy Fitzhardinge | 91e0c5f | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 96 | for(i = 0; i < b->cbidx; i++) { |
| 97 | struct callback *cb = &b->callbacks[i]; |
| 98 | |
| 99 | (*cb->fn)(cb->data); |
| 100 | } |
| 101 | b->cbidx = 0; |
| 102 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 103 | BUG_ON(ret); |
| 104 | } |
| 105 | |
| 106 | struct multicall_space __xen_mc_entry(size_t args) |
| 107 | { |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 108 | struct mc_buffer *b = &__get_cpu_var(mc_buffer); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 109 | struct multicall_space ret; |
| 110 | unsigned argspace = (args + sizeof(u64) - 1) / sizeof(u64); |
| 111 | |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 112 | BUG_ON(preemptible()); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 113 | BUG_ON(argspace > MC_ARGS); |
| 114 | |
| 115 | if (b->mcidx == MC_BATCH || |
| 116 | (b->argidx + argspace) > MC_ARGS) |
| 117 | xen_mc_flush(); |
| 118 | |
| 119 | ret.mc = &b->entries[b->mcidx]; |
| 120 | b->mcidx++; |
| 121 | ret.args = &b->args[b->argidx]; |
| 122 | b->argidx += argspace; |
| 123 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 124 | return ret; |
| 125 | } |
Jeremy Fitzhardinge | 91e0c5f | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 126 | |
| 127 | void xen_mc_callback(void (*fn)(void *), void *data) |
| 128 | { |
| 129 | struct mc_buffer *b = &__get_cpu_var(mc_buffer); |
| 130 | struct callback *cb; |
| 131 | |
| 132 | if (b->cbidx == MC_BATCH) |
| 133 | xen_mc_flush(); |
| 134 | |
| 135 | cb = &b->callbacks[b->cbidx++]; |
| 136 | cb->fn = fn; |
| 137 | cb->data = data; |
| 138 | } |