Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Xen event channels |
| 3 | * |
| 4 | * Xen models interrupts with abstract event channels. Because each |
| 5 | * domain gets 1024 event channels, but NR_IRQ is not that large, we |
| 6 | * must dynamically map irqs<->event channels. The event channels |
| 7 | * interface with the rest of the kernel by defining a xen interrupt |
| 8 | * chip. When an event is recieved, it is mapped to an irq and sent |
| 9 | * through the normal interrupt processing path. |
| 10 | * |
| 11 | * There are four kinds of events which can be mapped to an event |
| 12 | * channel: |
| 13 | * |
| 14 | * 1. Inter-domain notifications. This includes all the virtual |
| 15 | * device events, since they're driven by front-ends in another domain |
| 16 | * (typically dom0). |
| 17 | * 2. VIRQs, typically used for timers. These are per-cpu events. |
| 18 | * 3. IPIs. |
| 19 | * 4. Hardware interrupts. Not supported at present. |
| 20 | * |
| 21 | * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007 |
| 22 | */ |
| 23 | |
| 24 | #include <linux/linkage.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/irq.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/string.h> |
Christophe Saout | 28e0886 | 2009-01-11 11:46:23 -0800 | [diff] [blame] | 29 | #include <linux/bootmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 31 | |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 32 | #include <asm/desc.h> |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 33 | #include <asm/ptrace.h> |
| 34 | #include <asm/irq.h> |
Jeremy Fitzhardinge | 792dc4f | 2009-02-06 14:09:43 -0800 | [diff] [blame] | 35 | #include <asm/idle.h> |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 36 | #include <asm/sync_bitops.h> |
| 37 | #include <asm/xen/hypercall.h> |
Adrian Bunk | 8d1b875 | 2007-07-20 00:31:44 -0700 | [diff] [blame] | 38 | #include <asm/xen/hypervisor.h> |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 39 | |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 40 | #include <xen/xen.h> |
| 41 | #include <xen/hvm.h> |
Isaku Yamahata | e04d0d0 | 2008-04-02 10:53:55 -0700 | [diff] [blame] | 42 | #include <xen/xen-ops.h> |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 43 | #include <xen/events.h> |
| 44 | #include <xen/interface/xen.h> |
| 45 | #include <xen/interface/event_channel.h> |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 46 | #include <xen/interface/hvm/hvm_op.h> |
| 47 | #include <xen/interface/hvm/params.h> |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 48 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 49 | /* |
| 50 | * This lock protects updates to the following mapping and reference-count |
| 51 | * arrays. The lock does not need to be acquired to read the mapping tables. |
| 52 | */ |
| 53 | static DEFINE_SPINLOCK(irq_mapping_update_lock); |
| 54 | |
| 55 | /* IRQ <-> VIRQ mapping. */ |
Tejun Heo | 204fba4 | 2009-06-24 15:13:45 +0900 | [diff] [blame] | 56 | static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1}; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 57 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 58 | /* IRQ <-> IPI mapping */ |
Tejun Heo | 204fba4 | 2009-06-24 15:13:45 +0900 | [diff] [blame] | 59 | static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1}; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 60 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 61 | /* Interrupt types. */ |
| 62 | enum xen_irq_type { |
Jeremy Fitzhardinge | d77bbd4 | 2009-02-06 14:09:45 -0800 | [diff] [blame] | 63 | IRQT_UNBOUND = 0, |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 64 | IRQT_PIRQ, |
| 65 | IRQT_VIRQ, |
| 66 | IRQT_IPI, |
| 67 | IRQT_EVTCHN |
| 68 | }; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 69 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 70 | /* |
| 71 | * Packed IRQ information: |
| 72 | * type - enum xen_irq_type |
| 73 | * event channel - irq->event channel mapping |
| 74 | * cpu - cpu this event channel is bound to |
| 75 | * index - type-specific information: |
| 76 | * PIRQ - vector, with MSB being "needs EIO" |
| 77 | * VIRQ - virq number |
| 78 | * IPI - IPI vector |
| 79 | * EVTCHN - |
| 80 | */ |
| 81 | struct irq_info |
| 82 | { |
| 83 | enum xen_irq_type type; /* type */ |
| 84 | unsigned short evtchn; /* event channel */ |
| 85 | unsigned short cpu; /* cpu bound */ |
| 86 | |
| 87 | union { |
| 88 | unsigned short virq; |
| 89 | enum ipi_vector ipi; |
| 90 | struct { |
| 91 | unsigned short gsi; |
| 92 | unsigned short vector; |
| 93 | } pirq; |
| 94 | } u; |
| 95 | }; |
| 96 | |
| 97 | static struct irq_info irq_info[NR_IRQS]; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 98 | |
| 99 | static int evtchn_to_irq[NR_EVENT_CHANNELS] = { |
| 100 | [0 ... NR_EVENT_CHANNELS-1] = -1 |
| 101 | }; |
Mike Travis | c7a3589 | 2009-01-10 21:58:11 -0800 | [diff] [blame] | 102 | struct cpu_evtchn_s { |
| 103 | unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG]; |
| 104 | }; |
| 105 | static struct cpu_evtchn_s *cpu_evtchn_mask_p; |
| 106 | static inline unsigned long *cpu_evtchn_mask(int cpu) |
| 107 | { |
| 108 | return cpu_evtchn_mask_p[cpu].bits; |
| 109 | } |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 110 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 111 | /* Xen will never allocate port zero for any purpose. */ |
| 112 | #define VALID_EVTCHN(chn) ((chn) != 0) |
| 113 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 114 | static struct irq_chip xen_dynamic_chip; |
Jeremy Fitzhardinge | aaca496 | 2010-08-20 18:57:53 -0700 | [diff] [blame] | 115 | static struct irq_chip xen_percpu_chip; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 116 | |
| 117 | /* Constructor for packed IRQ information. */ |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 118 | static struct irq_info mk_unbound_info(void) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 119 | { |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 120 | return (struct irq_info) { .type = IRQT_UNBOUND }; |
| 121 | } |
| 122 | |
| 123 | static struct irq_info mk_evtchn_info(unsigned short evtchn) |
| 124 | { |
Ian Campbell | 90af951 | 2009-02-06 16:55:58 -0800 | [diff] [blame] | 125 | return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn, |
| 126 | .cpu = 0 }; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi) |
| 130 | { |
| 131 | return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn, |
Ian Campbell | 90af951 | 2009-02-06 16:55:58 -0800 | [diff] [blame] | 132 | .cpu = 0, .u.ipi = ipi }; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq) |
| 136 | { |
| 137 | return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn, |
Ian Campbell | 90af951 | 2009-02-06 16:55:58 -0800 | [diff] [blame] | 138 | .cpu = 0, .u.virq = virq }; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static struct irq_info mk_pirq_info(unsigned short evtchn, |
| 142 | unsigned short gsi, unsigned short vector) |
| 143 | { |
| 144 | return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn, |
Ian Campbell | 90af951 | 2009-02-06 16:55:58 -0800 | [diff] [blame] | 145 | .cpu = 0, .u.pirq = { .gsi = gsi, .vector = vector } }; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Accessors for packed IRQ information. |
| 150 | */ |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 151 | static struct irq_info *info_for_irq(unsigned irq) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 152 | { |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 153 | return &irq_info[irq]; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 156 | static unsigned int evtchn_from_irq(unsigned irq) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 157 | { |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 158 | return info_for_irq(irq)->evtchn; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Ian Campbell | d4c0453 | 2009-02-06 19:20:31 -0800 | [diff] [blame] | 161 | unsigned irq_from_evtchn(unsigned int evtchn) |
| 162 | { |
| 163 | return evtchn_to_irq[evtchn]; |
| 164 | } |
| 165 | EXPORT_SYMBOL_GPL(irq_from_evtchn); |
| 166 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 167 | static enum ipi_vector ipi_from_irq(unsigned irq) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 168 | { |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 169 | struct irq_info *info = info_for_irq(irq); |
| 170 | |
| 171 | BUG_ON(info == NULL); |
| 172 | BUG_ON(info->type != IRQT_IPI); |
| 173 | |
| 174 | return info->u.ipi; |
| 175 | } |
| 176 | |
| 177 | static unsigned virq_from_irq(unsigned irq) |
| 178 | { |
| 179 | struct irq_info *info = info_for_irq(irq); |
| 180 | |
| 181 | BUG_ON(info == NULL); |
| 182 | BUG_ON(info->type != IRQT_VIRQ); |
| 183 | |
| 184 | return info->u.virq; |
| 185 | } |
| 186 | |
| 187 | static unsigned gsi_from_irq(unsigned irq) |
| 188 | { |
| 189 | struct irq_info *info = info_for_irq(irq); |
| 190 | |
| 191 | BUG_ON(info == NULL); |
| 192 | BUG_ON(info->type != IRQT_PIRQ); |
| 193 | |
| 194 | return info->u.pirq.gsi; |
| 195 | } |
| 196 | |
| 197 | static unsigned vector_from_irq(unsigned irq) |
| 198 | { |
| 199 | struct irq_info *info = info_for_irq(irq); |
| 200 | |
| 201 | BUG_ON(info == NULL); |
| 202 | BUG_ON(info->type != IRQT_PIRQ); |
| 203 | |
| 204 | return info->u.pirq.vector; |
| 205 | } |
| 206 | |
| 207 | static enum xen_irq_type type_from_irq(unsigned irq) |
| 208 | { |
| 209 | return info_for_irq(irq)->type; |
| 210 | } |
| 211 | |
| 212 | static unsigned cpu_from_irq(unsigned irq) |
| 213 | { |
| 214 | return info_for_irq(irq)->cpu; |
| 215 | } |
| 216 | |
| 217 | static unsigned int cpu_from_evtchn(unsigned int evtchn) |
| 218 | { |
| 219 | int irq = evtchn_to_irq[evtchn]; |
| 220 | unsigned ret = 0; |
| 221 | |
| 222 | if (irq != -1) |
| 223 | ret = cpu_from_irq(irq); |
| 224 | |
| 225 | return ret; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static inline unsigned long active_evtchns(unsigned int cpu, |
| 229 | struct shared_info *sh, |
| 230 | unsigned int idx) |
| 231 | { |
| 232 | return (sh->evtchn_pending[idx] & |
Mike Travis | c7a3589 | 2009-01-10 21:58:11 -0800 | [diff] [blame] | 233 | cpu_evtchn_mask(cpu)[idx] & |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 234 | ~sh->evtchn_mask[idx]); |
| 235 | } |
| 236 | |
| 237 | static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu) |
| 238 | { |
| 239 | int irq = evtchn_to_irq[chn]; |
| 240 | |
| 241 | BUG_ON(irq == -1); |
| 242 | #ifdef CONFIG_SMP |
Mike Travis | 7f7ace0 | 2009-01-10 21:58:08 -0800 | [diff] [blame] | 243 | cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu)); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 244 | #endif |
| 245 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 246 | __clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq))); |
Mike Travis | c7a3589 | 2009-01-10 21:58:11 -0800 | [diff] [blame] | 247 | __set_bit(chn, cpu_evtchn_mask(cpu)); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 248 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 249 | irq_info[irq].cpu = cpu; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static void init_evtchn_cpu_bindings(void) |
| 253 | { |
| 254 | #ifdef CONFIG_SMP |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 255 | struct irq_desc *desc; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 256 | int i; |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 257 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 258 | /* By default all event channels notify CPU#0. */ |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 259 | for_each_irq_desc(i, desc) { |
Mike Travis | 7f7ace0 | 2009-01-10 21:58:08 -0800 | [diff] [blame] | 260 | cpumask_copy(desc->affinity, cpumask_of(0)); |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 261 | } |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 262 | #endif |
| 263 | |
Mike Travis | c7a3589 | 2009-01-10 21:58:11 -0800 | [diff] [blame] | 264 | memset(cpu_evtchn_mask(0), ~0, sizeof(cpu_evtchn_mask(0))); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 267 | static inline void clear_evtchn(int port) |
| 268 | { |
| 269 | struct shared_info *s = HYPERVISOR_shared_info; |
| 270 | sync_clear_bit(port, &s->evtchn_pending[0]); |
| 271 | } |
| 272 | |
| 273 | static inline void set_evtchn(int port) |
| 274 | { |
| 275 | struct shared_info *s = HYPERVISOR_shared_info; |
| 276 | sync_set_bit(port, &s->evtchn_pending[0]); |
| 277 | } |
| 278 | |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 279 | static inline int test_evtchn(int port) |
| 280 | { |
| 281 | struct shared_info *s = HYPERVISOR_shared_info; |
| 282 | return sync_test_bit(port, &s->evtchn_pending[0]); |
| 283 | } |
| 284 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 285 | |
| 286 | /** |
| 287 | * notify_remote_via_irq - send event to remote end of event channel via irq |
| 288 | * @irq: irq of event channel to send event to |
| 289 | * |
| 290 | * Unlike notify_remote_via_evtchn(), this is safe to use across |
| 291 | * save/restore. Notifications on a broken connection are silently |
| 292 | * dropped. |
| 293 | */ |
| 294 | void notify_remote_via_irq(int irq) |
| 295 | { |
| 296 | int evtchn = evtchn_from_irq(irq); |
| 297 | |
| 298 | if (VALID_EVTCHN(evtchn)) |
| 299 | notify_remote_via_evtchn(evtchn); |
| 300 | } |
| 301 | EXPORT_SYMBOL_GPL(notify_remote_via_irq); |
| 302 | |
| 303 | static void mask_evtchn(int port) |
| 304 | { |
| 305 | struct shared_info *s = HYPERVISOR_shared_info; |
| 306 | sync_set_bit(port, &s->evtchn_mask[0]); |
| 307 | } |
| 308 | |
| 309 | static void unmask_evtchn(int port) |
| 310 | { |
| 311 | struct shared_info *s = HYPERVISOR_shared_info; |
| 312 | unsigned int cpu = get_cpu(); |
| 313 | |
| 314 | BUG_ON(!irqs_disabled()); |
| 315 | |
| 316 | /* Slow path (hypercall) if this is a non-local port. */ |
| 317 | if (unlikely(cpu != cpu_from_evtchn(port))) { |
| 318 | struct evtchn_unmask unmask = { .port = port }; |
| 319 | (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask); |
| 320 | } else { |
| 321 | struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu); |
| 322 | |
| 323 | sync_clear_bit(port, &s->evtchn_mask[0]); |
| 324 | |
| 325 | /* |
| 326 | * The following is basically the equivalent of |
| 327 | * 'hw_resend_irq'. Just like a real IO-APIC we 'lose |
| 328 | * the interrupt edge' if the channel is masked. |
| 329 | */ |
| 330 | if (sync_test_bit(port, &s->evtchn_pending[0]) && |
| 331 | !sync_test_and_set_bit(port / BITS_PER_LONG, |
| 332 | &vcpu_info->evtchn_pending_sel)) |
| 333 | vcpu_info->evtchn_upcall_pending = 1; |
| 334 | } |
| 335 | |
| 336 | put_cpu(); |
| 337 | } |
| 338 | |
| 339 | static int find_unbound_irq(void) |
| 340 | { |
| 341 | int irq; |
Jeremy Fitzhardinge | 6f8a0ed | 2008-12-17 13:42:29 -0800 | [diff] [blame] | 342 | struct irq_desc *desc; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 343 | |
Stefano Stabellini | 99ad198 | 2010-05-14 12:41:20 +0100 | [diff] [blame] | 344 | for (irq = 0; irq < nr_irqs; irq++) { |
| 345 | desc = irq_to_desc(irq); |
| 346 | /* only 0->15 have init'd desc; handle irq > 16 */ |
| 347 | if (desc == NULL) |
| 348 | break; |
| 349 | if (desc->chip == &no_irq_chip) |
| 350 | break; |
| 351 | if (desc->chip != &xen_dynamic_chip) |
| 352 | continue; |
Jeremy Fitzhardinge | d77bbd4 | 2009-02-06 14:09:45 -0800 | [diff] [blame] | 353 | if (irq_info[irq].type == IRQT_UNBOUND) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 354 | break; |
Stefano Stabellini | 99ad198 | 2010-05-14 12:41:20 +0100 | [diff] [blame] | 355 | } |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 356 | |
Yinghai Lu | 5a15d7e | 2008-08-19 20:49:57 -0700 | [diff] [blame] | 357 | if (irq == nr_irqs) |
| 358 | panic("No available IRQ to bind to: increase nr_irqs!\n"); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 359 | |
Yinghai Lu | 85ac16d | 2009-04-27 18:00:38 -0700 | [diff] [blame] | 360 | desc = irq_to_desc_alloc_node(irq, 0); |
Jeremy Fitzhardinge | 6f8a0ed | 2008-12-17 13:42:29 -0800 | [diff] [blame] | 361 | if (WARN_ON(desc == NULL)) |
| 362 | return -1; |
| 363 | |
Stefano Stabellini | 99ad198 | 2010-05-14 12:41:20 +0100 | [diff] [blame] | 364 | dynamic_irq_init_keep_chip_data(irq); |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 365 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 366 | return irq; |
| 367 | } |
| 368 | |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 369 | int bind_evtchn_to_irq(unsigned int evtchn) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 370 | { |
| 371 | int irq; |
| 372 | |
| 373 | spin_lock(&irq_mapping_update_lock); |
| 374 | |
| 375 | irq = evtchn_to_irq[evtchn]; |
| 376 | |
| 377 | if (irq == -1) { |
| 378 | irq = find_unbound_irq(); |
| 379 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 380 | set_irq_chip_and_handler_name(irq, &xen_dynamic_chip, |
Jeremy Fitzhardinge | dffe2e1 | 2010-08-20 19:10:01 -0700 | [diff] [blame] | 381 | handle_edge_irq, "event"); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 382 | |
| 383 | evtchn_to_irq[evtchn] = irq; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 384 | irq_info[irq] = mk_evtchn_info(evtchn); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 387 | spin_unlock(&irq_mapping_update_lock); |
| 388 | |
| 389 | return irq; |
| 390 | } |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 391 | EXPORT_SYMBOL_GPL(bind_evtchn_to_irq); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 392 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 393 | static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu) |
| 394 | { |
| 395 | struct evtchn_bind_ipi bind_ipi; |
| 396 | int evtchn, irq; |
| 397 | |
| 398 | spin_lock(&irq_mapping_update_lock); |
| 399 | |
| 400 | irq = per_cpu(ipi_to_irq, cpu)[ipi]; |
Ian Campbell | 90af951 | 2009-02-06 16:55:58 -0800 | [diff] [blame] | 401 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 402 | if (irq == -1) { |
| 403 | irq = find_unbound_irq(); |
| 404 | if (irq < 0) |
| 405 | goto out; |
| 406 | |
Jeremy Fitzhardinge | aaca496 | 2010-08-20 18:57:53 -0700 | [diff] [blame] | 407 | set_irq_chip_and_handler_name(irq, &xen_percpu_chip, |
| 408 | handle_percpu_irq, "ipi"); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 409 | |
| 410 | bind_ipi.vcpu = cpu; |
| 411 | if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi, |
| 412 | &bind_ipi) != 0) |
| 413 | BUG(); |
| 414 | evtchn = bind_ipi.port; |
| 415 | |
| 416 | evtchn_to_irq[evtchn] = irq; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 417 | irq_info[irq] = mk_ipi_info(evtchn, ipi); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 418 | per_cpu(ipi_to_irq, cpu)[ipi] = irq; |
| 419 | |
| 420 | bind_evtchn_to_cpu(evtchn, cpu); |
| 421 | } |
| 422 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 423 | out: |
| 424 | spin_unlock(&irq_mapping_update_lock); |
| 425 | return irq; |
| 426 | } |
| 427 | |
| 428 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 429 | static int bind_virq_to_irq(unsigned int virq, unsigned int cpu) |
| 430 | { |
| 431 | struct evtchn_bind_virq bind_virq; |
| 432 | int evtchn, irq; |
| 433 | |
| 434 | spin_lock(&irq_mapping_update_lock); |
| 435 | |
| 436 | irq = per_cpu(virq_to_irq, cpu)[virq]; |
| 437 | |
| 438 | if (irq == -1) { |
| 439 | bind_virq.virq = virq; |
| 440 | bind_virq.vcpu = cpu; |
| 441 | if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq, |
| 442 | &bind_virq) != 0) |
| 443 | BUG(); |
| 444 | evtchn = bind_virq.port; |
| 445 | |
| 446 | irq = find_unbound_irq(); |
| 447 | |
Jeremy Fitzhardinge | aaca496 | 2010-08-20 18:57:53 -0700 | [diff] [blame] | 448 | set_irq_chip_and_handler_name(irq, &xen_percpu_chip, |
| 449 | handle_percpu_irq, "virq"); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 450 | |
| 451 | evtchn_to_irq[evtchn] = irq; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 452 | irq_info[irq] = mk_virq_info(evtchn, virq); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 453 | |
| 454 | per_cpu(virq_to_irq, cpu)[virq] = irq; |
| 455 | |
| 456 | bind_evtchn_to_cpu(evtchn, cpu); |
| 457 | } |
| 458 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 459 | spin_unlock(&irq_mapping_update_lock); |
| 460 | |
| 461 | return irq; |
| 462 | } |
| 463 | |
| 464 | static void unbind_from_irq(unsigned int irq) |
| 465 | { |
| 466 | struct evtchn_close close; |
| 467 | int evtchn = evtchn_from_irq(irq); |
| 468 | |
| 469 | spin_lock(&irq_mapping_update_lock); |
| 470 | |
Jeremy Fitzhardinge | d77bbd4 | 2009-02-06 14:09:45 -0800 | [diff] [blame] | 471 | if (VALID_EVTCHN(evtchn)) { |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 472 | close.port = evtchn; |
| 473 | if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0) |
| 474 | BUG(); |
| 475 | |
| 476 | switch (type_from_irq(irq)) { |
| 477 | case IRQT_VIRQ: |
| 478 | per_cpu(virq_to_irq, cpu_from_evtchn(evtchn)) |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 479 | [virq_from_irq(irq)] = -1; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 480 | break; |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 481 | case IRQT_IPI: |
| 482 | per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn)) |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 483 | [ipi_from_irq(irq)] = -1; |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 484 | break; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 485 | default: |
| 486 | break; |
| 487 | } |
| 488 | |
| 489 | /* Closed ports are implicitly re-bound to VCPU0. */ |
| 490 | bind_evtchn_to_cpu(evtchn, 0); |
| 491 | |
| 492 | evtchn_to_irq[evtchn] = -1; |
Ian Campbell | fed5ea8 | 2009-12-01 16:15:30 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | if (irq_info[irq].type != IRQT_UNBOUND) { |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 496 | irq_info[irq] = mk_unbound_info(); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 497 | |
Jeremy Fitzhardinge | 0f2287a | 2008-05-26 23:31:24 +0100 | [diff] [blame] | 498 | dynamic_irq_cleanup(irq); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | spin_unlock(&irq_mapping_update_lock); |
| 502 | } |
| 503 | |
| 504 | int bind_evtchn_to_irqhandler(unsigned int evtchn, |
Jeff Garzik | 7c23997 | 2007-10-19 03:12:20 -0400 | [diff] [blame] | 505 | irq_handler_t handler, |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 506 | unsigned long irqflags, |
| 507 | const char *devname, void *dev_id) |
| 508 | { |
| 509 | unsigned int irq; |
| 510 | int retval; |
| 511 | |
| 512 | irq = bind_evtchn_to_irq(evtchn); |
| 513 | retval = request_irq(irq, handler, irqflags, devname, dev_id); |
| 514 | if (retval != 0) { |
| 515 | unbind_from_irq(irq); |
| 516 | return retval; |
| 517 | } |
| 518 | |
| 519 | return irq; |
| 520 | } |
| 521 | EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler); |
| 522 | |
| 523 | int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu, |
Jeff Garzik | 7c23997 | 2007-10-19 03:12:20 -0400 | [diff] [blame] | 524 | irq_handler_t handler, |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 525 | unsigned long irqflags, const char *devname, void *dev_id) |
| 526 | { |
| 527 | unsigned int irq; |
| 528 | int retval; |
| 529 | |
| 530 | irq = bind_virq_to_irq(virq, cpu); |
| 531 | retval = request_irq(irq, handler, irqflags, devname, dev_id); |
| 532 | if (retval != 0) { |
| 533 | unbind_from_irq(irq); |
| 534 | return retval; |
| 535 | } |
| 536 | |
| 537 | return irq; |
| 538 | } |
| 539 | EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler); |
| 540 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 541 | int bind_ipi_to_irqhandler(enum ipi_vector ipi, |
| 542 | unsigned int cpu, |
| 543 | irq_handler_t handler, |
| 544 | unsigned long irqflags, |
| 545 | const char *devname, |
| 546 | void *dev_id) |
| 547 | { |
| 548 | int irq, retval; |
| 549 | |
| 550 | irq = bind_ipi_to_irq(ipi, cpu); |
| 551 | if (irq < 0) |
| 552 | return irq; |
| 553 | |
Ian Campbell | 4877c73 | 2010-07-29 11:16:35 +0100 | [diff] [blame] | 554 | irqflags |= IRQF_NO_SUSPEND; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 555 | retval = request_irq(irq, handler, irqflags, devname, dev_id); |
| 556 | if (retval != 0) { |
| 557 | unbind_from_irq(irq); |
| 558 | return retval; |
| 559 | } |
| 560 | |
| 561 | return irq; |
| 562 | } |
| 563 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 564 | void unbind_from_irqhandler(unsigned int irq, void *dev_id) |
| 565 | { |
| 566 | free_irq(irq, dev_id); |
| 567 | unbind_from_irq(irq); |
| 568 | } |
| 569 | EXPORT_SYMBOL_GPL(unbind_from_irqhandler); |
| 570 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 571 | void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector) |
| 572 | { |
| 573 | int irq = per_cpu(ipi_to_irq, cpu)[vector]; |
| 574 | BUG_ON(irq < 0); |
| 575 | notify_remote_via_irq(irq); |
| 576 | } |
| 577 | |
Jeremy Fitzhardinge | ee523ca | 2008-03-17 16:37:18 -0700 | [diff] [blame] | 578 | irqreturn_t xen_debug_interrupt(int irq, void *dev_id) |
| 579 | { |
| 580 | struct shared_info *sh = HYPERVISOR_shared_info; |
| 581 | int cpu = smp_processor_id(); |
| 582 | int i; |
| 583 | unsigned long flags; |
| 584 | static DEFINE_SPINLOCK(debug_lock); |
| 585 | |
| 586 | spin_lock_irqsave(&debug_lock, flags); |
| 587 | |
| 588 | printk("vcpu %d\n ", cpu); |
| 589 | |
| 590 | for_each_online_cpu(i) { |
| 591 | struct vcpu_info *v = per_cpu(xen_vcpu, i); |
| 592 | printk("%d: masked=%d pending=%d event_sel %08lx\n ", i, |
Isaku Yamahata | e849c3e | 2008-04-02 10:53:56 -0700 | [diff] [blame] | 593 | (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask, |
Jeremy Fitzhardinge | ee523ca | 2008-03-17 16:37:18 -0700 | [diff] [blame] | 594 | v->evtchn_upcall_pending, |
| 595 | v->evtchn_pending_sel); |
| 596 | } |
| 597 | printk("pending:\n "); |
| 598 | for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--) |
| 599 | printk("%08lx%s", sh->evtchn_pending[i], |
| 600 | i % 8 == 0 ? "\n " : " "); |
| 601 | printk("\nmasks:\n "); |
| 602 | for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) |
| 603 | printk("%08lx%s", sh->evtchn_mask[i], |
| 604 | i % 8 == 0 ? "\n " : " "); |
| 605 | |
| 606 | printk("\nunmasked:\n "); |
| 607 | for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) |
| 608 | printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i], |
| 609 | i % 8 == 0 ? "\n " : " "); |
| 610 | |
| 611 | printk("\npending list:\n"); |
| 612 | for(i = 0; i < NR_EVENT_CHANNELS; i++) { |
| 613 | if (sync_test_bit(i, sh->evtchn_pending)) { |
| 614 | printk(" %d: event %d -> irq %d\n", |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 615 | cpu_from_evtchn(i), i, |
| 616 | evtchn_to_irq[i]); |
Jeremy Fitzhardinge | ee523ca | 2008-03-17 16:37:18 -0700 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | |
| 620 | spin_unlock_irqrestore(&debug_lock, flags); |
| 621 | |
| 622 | return IRQ_HANDLED; |
| 623 | } |
| 624 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 625 | static DEFINE_PER_CPU(unsigned, xed_nesting_count); |
| 626 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 627 | /* |
| 628 | * Search the CPUs pending events bitmasks. For each one found, map |
| 629 | * the event number to an irq, and feed it into do_IRQ() for |
| 630 | * handling. |
| 631 | * |
| 632 | * Xen uses a two-level bitmap to speed searching. The first level is |
| 633 | * a bitset of words which contain pending event bits. The second |
| 634 | * level is a bitset of pending events themselves. |
| 635 | */ |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 636 | static void __xen_evtchn_do_upcall(void) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 637 | { |
| 638 | int cpu = get_cpu(); |
| 639 | struct shared_info *s = HYPERVISOR_shared_info; |
| 640 | struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu); |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 641 | unsigned count; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 642 | |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 643 | do { |
| 644 | unsigned long pending_words; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 645 | |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 646 | vcpu_info->evtchn_upcall_pending = 0; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 647 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 648 | if (__get_cpu_var(xed_nesting_count)++) |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 649 | goto out; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 650 | |
Isaku Yamahata | e849c3e | 2008-04-02 10:53:56 -0700 | [diff] [blame] | 651 | #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */ |
| 652 | /* Clear master flag /before/ clearing selector flag. */ |
Isaku Yamahata | 6673cf6 | 2008-06-16 14:58:13 -0700 | [diff] [blame] | 653 | wmb(); |
Isaku Yamahata | e849c3e | 2008-04-02 10:53:56 -0700 | [diff] [blame] | 654 | #endif |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 655 | pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0); |
| 656 | while (pending_words != 0) { |
| 657 | unsigned long pending_bits; |
| 658 | int word_idx = __ffs(pending_words); |
| 659 | pending_words &= ~(1UL << word_idx); |
| 660 | |
| 661 | while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) { |
| 662 | int bit_idx = __ffs(pending_bits); |
| 663 | int port = (word_idx * BITS_PER_LONG) + bit_idx; |
| 664 | int irq = evtchn_to_irq[port]; |
Eric W. Biederman | ca4dbc6 | 2010-02-17 18:49:54 -0800 | [diff] [blame] | 665 | struct irq_desc *desc; |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 666 | |
Eric W. Biederman | ca4dbc6 | 2010-02-17 18:49:54 -0800 | [diff] [blame] | 667 | if (irq != -1) { |
| 668 | desc = irq_to_desc(irq); |
| 669 | if (desc) |
| 670 | generic_handle_irq_desc(irq, desc); |
| 671 | } |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 672 | } |
| 673 | } |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 674 | |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 675 | BUG_ON(!irqs_disabled()); |
| 676 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 677 | count = __get_cpu_var(xed_nesting_count); |
| 678 | __get_cpu_var(xed_nesting_count) = 0; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 679 | } while (count != 1 || vcpu_info->evtchn_upcall_pending); |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 680 | |
| 681 | out: |
Jeremy Fitzhardinge | 3445a8f | 2009-02-06 14:09:46 -0800 | [diff] [blame] | 682 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 683 | put_cpu(); |
| 684 | } |
| 685 | |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 686 | void xen_evtchn_do_upcall(struct pt_regs *regs) |
| 687 | { |
| 688 | struct pt_regs *old_regs = set_irq_regs(regs); |
| 689 | |
| 690 | exit_idle(); |
| 691 | irq_enter(); |
| 692 | |
| 693 | __xen_evtchn_do_upcall(); |
| 694 | |
| 695 | irq_exit(); |
| 696 | set_irq_regs(old_regs); |
| 697 | } |
| 698 | |
| 699 | void xen_hvm_evtchn_do_upcall(void) |
| 700 | { |
| 701 | __xen_evtchn_do_upcall(); |
| 702 | } |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 703 | EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall); |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 704 | |
Jeremy Fitzhardinge | eb1e305 | 2008-05-26 23:31:23 +0100 | [diff] [blame] | 705 | /* Rebind a new event channel to an existing irq. */ |
| 706 | void rebind_evtchn_irq(int evtchn, int irq) |
| 707 | { |
Jeremy Fitzhardinge | d77bbd4 | 2009-02-06 14:09:45 -0800 | [diff] [blame] | 708 | struct irq_info *info = info_for_irq(irq); |
| 709 | |
Jeremy Fitzhardinge | eb1e305 | 2008-05-26 23:31:23 +0100 | [diff] [blame] | 710 | /* Make sure the irq is masked, since the new event channel |
| 711 | will also be masked. */ |
| 712 | disable_irq(irq); |
| 713 | |
| 714 | spin_lock(&irq_mapping_update_lock); |
| 715 | |
| 716 | /* After resume the irq<->evtchn mappings are all cleared out */ |
| 717 | BUG_ON(evtchn_to_irq[evtchn] != -1); |
| 718 | /* Expect irq to have been bound before, |
Jeremy Fitzhardinge | d77bbd4 | 2009-02-06 14:09:45 -0800 | [diff] [blame] | 719 | so there should be a proper type */ |
| 720 | BUG_ON(info->type == IRQT_UNBOUND); |
Jeremy Fitzhardinge | eb1e305 | 2008-05-26 23:31:23 +0100 | [diff] [blame] | 721 | |
| 722 | evtchn_to_irq[evtchn] = irq; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 723 | irq_info[irq] = mk_evtchn_info(evtchn); |
Jeremy Fitzhardinge | eb1e305 | 2008-05-26 23:31:23 +0100 | [diff] [blame] | 724 | |
| 725 | spin_unlock(&irq_mapping_update_lock); |
| 726 | |
| 727 | /* new event channels are always bound to cpu 0 */ |
Rusty Russell | 0de2652 | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 728 | irq_set_affinity(irq, cpumask_of(0)); |
Jeremy Fitzhardinge | eb1e305 | 2008-05-26 23:31:23 +0100 | [diff] [blame] | 729 | |
| 730 | /* Unmask the event channel. */ |
| 731 | enable_irq(irq); |
| 732 | } |
| 733 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 734 | /* Rebind an evtchn so that it gets delivered to a specific cpu */ |
Yinghai Lu | d5dedd4 | 2009-04-27 17:59:21 -0700 | [diff] [blame] | 735 | static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 736 | { |
| 737 | struct evtchn_bind_vcpu bind_vcpu; |
| 738 | int evtchn = evtchn_from_irq(irq); |
| 739 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 740 | /* events delivered via platform PCI interrupts are always |
| 741 | * routed to vcpu 0 */ |
| 742 | if (!VALID_EVTCHN(evtchn) || |
| 743 | (xen_hvm_domain() && !xen_have_vector_callback)) |
Yinghai Lu | d5dedd4 | 2009-04-27 17:59:21 -0700 | [diff] [blame] | 744 | return -1; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 745 | |
| 746 | /* Send future instances of this interrupt to other vcpu. */ |
| 747 | bind_vcpu.port = evtchn; |
| 748 | bind_vcpu.vcpu = tcpu; |
| 749 | |
| 750 | /* |
| 751 | * If this fails, it usually just indicates that we're dealing with a |
| 752 | * virq or IPI channel, which don't actually need to be rebound. Ignore |
| 753 | * it, but don't do the xenlinux-level rebind in that case. |
| 754 | */ |
| 755 | if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0) |
| 756 | bind_evtchn_to_cpu(evtchn, tcpu); |
Yinghai Lu | d5dedd4 | 2009-04-27 17:59:21 -0700 | [diff] [blame] | 757 | |
| 758 | return 0; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Yinghai Lu | d5dedd4 | 2009-04-27 17:59:21 -0700 | [diff] [blame] | 761 | static int set_affinity_irq(unsigned irq, const struct cpumask *dest) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 762 | { |
Rusty Russell | 0de2652 | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 763 | unsigned tcpu = cpumask_first(dest); |
Yinghai Lu | d5dedd4 | 2009-04-27 17:59:21 -0700 | [diff] [blame] | 764 | |
| 765 | return rebind_irq_to_cpu(irq, tcpu); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Isaku Yamahata | 642e0c8 | 2008-04-02 10:53:57 -0700 | [diff] [blame] | 768 | int resend_irq_on_evtchn(unsigned int irq) |
| 769 | { |
| 770 | int masked, evtchn = evtchn_from_irq(irq); |
| 771 | struct shared_info *s = HYPERVISOR_shared_info; |
| 772 | |
| 773 | if (!VALID_EVTCHN(evtchn)) |
| 774 | return 1; |
| 775 | |
| 776 | masked = sync_test_and_set_bit(evtchn, s->evtchn_mask); |
| 777 | sync_set_bit(evtchn, s->evtchn_pending); |
| 778 | if (!masked) |
| 779 | unmask_evtchn(evtchn); |
| 780 | |
| 781 | return 1; |
| 782 | } |
| 783 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 784 | static void enable_dynirq(unsigned int irq) |
| 785 | { |
| 786 | int evtchn = evtchn_from_irq(irq); |
| 787 | |
| 788 | if (VALID_EVTCHN(evtchn)) |
| 789 | unmask_evtchn(evtchn); |
| 790 | } |
| 791 | |
| 792 | static void disable_dynirq(unsigned int irq) |
| 793 | { |
| 794 | int evtchn = evtchn_from_irq(irq); |
| 795 | |
| 796 | if (VALID_EVTCHN(evtchn)) |
| 797 | mask_evtchn(evtchn); |
| 798 | } |
| 799 | |
| 800 | static void ack_dynirq(unsigned int irq) |
| 801 | { |
| 802 | int evtchn = evtchn_from_irq(irq); |
| 803 | |
| 804 | move_native_irq(irq); |
| 805 | |
| 806 | if (VALID_EVTCHN(evtchn)) |
| 807 | clear_evtchn(evtchn); |
| 808 | } |
| 809 | |
| 810 | static int retrigger_dynirq(unsigned int irq) |
| 811 | { |
| 812 | int evtchn = evtchn_from_irq(irq); |
Jeremy Fitzhardinge | ee8fa1c | 2008-03-17 16:37:19 -0700 | [diff] [blame] | 813 | struct shared_info *sh = HYPERVISOR_shared_info; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 814 | int ret = 0; |
| 815 | |
| 816 | if (VALID_EVTCHN(evtchn)) { |
Jeremy Fitzhardinge | ee8fa1c | 2008-03-17 16:37:19 -0700 | [diff] [blame] | 817 | int masked; |
| 818 | |
| 819 | masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask); |
| 820 | sync_set_bit(evtchn, sh->evtchn_pending); |
| 821 | if (!masked) |
| 822 | unmask_evtchn(evtchn); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 823 | ret = 1; |
| 824 | } |
| 825 | |
| 826 | return ret; |
| 827 | } |
| 828 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 829 | static void restore_cpu_virqs(unsigned int cpu) |
| 830 | { |
| 831 | struct evtchn_bind_virq bind_virq; |
| 832 | int virq, irq, evtchn; |
| 833 | |
| 834 | for (virq = 0; virq < NR_VIRQS; virq++) { |
| 835 | if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1) |
| 836 | continue; |
| 837 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 838 | BUG_ON(virq_from_irq(irq) != virq); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 839 | |
| 840 | /* Get a new binding from Xen. */ |
| 841 | bind_virq.virq = virq; |
| 842 | bind_virq.vcpu = cpu; |
| 843 | if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq, |
| 844 | &bind_virq) != 0) |
| 845 | BUG(); |
| 846 | evtchn = bind_virq.port; |
| 847 | |
| 848 | /* Record the new mapping. */ |
| 849 | evtchn_to_irq[evtchn] = irq; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 850 | irq_info[irq] = mk_virq_info(evtchn, virq); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 851 | bind_evtchn_to_cpu(evtchn, cpu); |
| 852 | |
| 853 | /* Ready for use. */ |
| 854 | unmask_evtchn(evtchn); |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | static void restore_cpu_ipis(unsigned int cpu) |
| 859 | { |
| 860 | struct evtchn_bind_ipi bind_ipi; |
| 861 | int ipi, irq, evtchn; |
| 862 | |
| 863 | for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) { |
| 864 | if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1) |
| 865 | continue; |
| 866 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 867 | BUG_ON(ipi_from_irq(irq) != ipi); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 868 | |
| 869 | /* Get a new binding from Xen. */ |
| 870 | bind_ipi.vcpu = cpu; |
| 871 | if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi, |
| 872 | &bind_ipi) != 0) |
| 873 | BUG(); |
| 874 | evtchn = bind_ipi.port; |
| 875 | |
| 876 | /* Record the new mapping. */ |
| 877 | evtchn_to_irq[evtchn] = irq; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 878 | irq_info[irq] = mk_ipi_info(evtchn, ipi); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 879 | bind_evtchn_to_cpu(evtchn, cpu); |
| 880 | |
| 881 | /* Ready for use. */ |
| 882 | unmask_evtchn(evtchn); |
| 883 | |
| 884 | } |
| 885 | } |
| 886 | |
Jeremy Fitzhardinge | 2d9e1e2 | 2008-07-07 12:07:53 -0700 | [diff] [blame] | 887 | /* Clear an irq's pending state, in preparation for polling on it */ |
| 888 | void xen_clear_irq_pending(int irq) |
| 889 | { |
| 890 | int evtchn = evtchn_from_irq(irq); |
| 891 | |
| 892 | if (VALID_EVTCHN(evtchn)) |
| 893 | clear_evtchn(evtchn); |
| 894 | } |
| 895 | |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 896 | void xen_set_irq_pending(int irq) |
| 897 | { |
| 898 | int evtchn = evtchn_from_irq(irq); |
| 899 | |
| 900 | if (VALID_EVTCHN(evtchn)) |
| 901 | set_evtchn(evtchn); |
| 902 | } |
| 903 | |
| 904 | bool xen_test_irq_pending(int irq) |
| 905 | { |
| 906 | int evtchn = evtchn_from_irq(irq); |
| 907 | bool ret = false; |
| 908 | |
| 909 | if (VALID_EVTCHN(evtchn)) |
| 910 | ret = test_evtchn(evtchn); |
| 911 | |
| 912 | return ret; |
| 913 | } |
| 914 | |
Jeremy Fitzhardinge | 2d9e1e2 | 2008-07-07 12:07:53 -0700 | [diff] [blame] | 915 | /* Poll waiting for an irq to become pending. In the usual case, the |
| 916 | irq will be disabled so it won't deliver an interrupt. */ |
| 917 | void xen_poll_irq(int irq) |
| 918 | { |
| 919 | evtchn_port_t evtchn = evtchn_from_irq(irq); |
| 920 | |
| 921 | if (VALID_EVTCHN(evtchn)) { |
| 922 | struct sched_poll poll; |
| 923 | |
| 924 | poll.nr_ports = 1; |
| 925 | poll.timeout = 0; |
Isaku Yamahata | ff3c536 | 2008-10-14 17:50:44 -0700 | [diff] [blame] | 926 | set_xen_guest_handle(poll.ports, &evtchn); |
Jeremy Fitzhardinge | 2d9e1e2 | 2008-07-07 12:07:53 -0700 | [diff] [blame] | 927 | |
| 928 | if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0) |
| 929 | BUG(); |
| 930 | } |
| 931 | } |
| 932 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 933 | void xen_irq_resume(void) |
| 934 | { |
| 935 | unsigned int cpu, irq, evtchn; |
| 936 | |
| 937 | init_evtchn_cpu_bindings(); |
| 938 | |
| 939 | /* New event-channel space is not 'live' yet. */ |
| 940 | for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++) |
| 941 | mask_evtchn(evtchn); |
| 942 | |
| 943 | /* No IRQ <-> event-channel mappings. */ |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 944 | for (irq = 0; irq < nr_irqs; irq++) |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 945 | irq_info[irq].evtchn = 0; /* zap event-channel binding */ |
| 946 | |
| 947 | for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++) |
| 948 | evtchn_to_irq[evtchn] = -1; |
| 949 | |
| 950 | for_each_possible_cpu(cpu) { |
| 951 | restore_cpu_virqs(cpu); |
| 952 | restore_cpu_ipis(cpu); |
| 953 | } |
| 954 | } |
| 955 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 956 | static struct irq_chip xen_dynamic_chip __read_mostly = { |
| 957 | .name = "xen-dyn", |
Jeremy Fitzhardinge | 54a353a | 2009-02-06 14:09:42 -0800 | [diff] [blame] | 958 | |
| 959 | .disable = disable_dynirq, |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 960 | .mask = disable_dynirq, |
| 961 | .unmask = enable_dynirq, |
Jeremy Fitzhardinge | 54a353a | 2009-02-06 14:09:42 -0800 | [diff] [blame] | 962 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 963 | .ack = ack_dynirq, |
| 964 | .set_affinity = set_affinity_irq, |
| 965 | .retrigger = retrigger_dynirq, |
| 966 | }; |
| 967 | |
Jeremy Fitzhardinge | aaca496 | 2010-08-20 18:57:53 -0700 | [diff] [blame] | 968 | static struct irq_chip xen_percpu_chip __read_mostly = { |
| 969 | .name = "xen-percpu", |
| 970 | |
| 971 | .disable = disable_dynirq, |
| 972 | .mask = disable_dynirq, |
| 973 | .unmask = enable_dynirq, |
| 974 | |
| 975 | .ack = ack_dynirq, |
| 976 | }; |
| 977 | |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 978 | int xen_set_callback_via(uint64_t via) |
| 979 | { |
| 980 | struct xen_hvm_param a; |
| 981 | a.domid = DOMID_SELF; |
| 982 | a.index = HVM_PARAM_CALLBACK_IRQ; |
| 983 | a.value = via; |
| 984 | return HYPERVISOR_hvm_op(HVMOP_set_param, &a); |
| 985 | } |
| 986 | EXPORT_SYMBOL_GPL(xen_set_callback_via); |
| 987 | |
Stefano Stabellini | ca65f9f | 2010-07-29 14:37:48 +0100 | [diff] [blame] | 988 | #ifdef CONFIG_XEN_PVHVM |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 989 | /* Vector callbacks are better than PCI interrupts to receive event |
| 990 | * channel notifications because we can receive vector callbacks on any |
| 991 | * vcpu and we don't need PCI support or APIC interactions. */ |
| 992 | void xen_callback_vector(void) |
| 993 | { |
| 994 | int rc; |
| 995 | uint64_t callback_via; |
| 996 | if (xen_have_vector_callback) { |
| 997 | callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK); |
| 998 | rc = xen_set_callback_via(callback_via); |
| 999 | if (rc) { |
| 1000 | printk(KERN_ERR "Request for Xen HVM callback vector" |
| 1001 | " failed.\n"); |
| 1002 | xen_have_vector_callback = 0; |
| 1003 | return; |
| 1004 | } |
| 1005 | printk(KERN_INFO "Xen HVM callback vector for event delivery is " |
| 1006 | "enabled\n"); |
| 1007 | /* in the restore case the vector has already been allocated */ |
| 1008 | if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors)) |
| 1009 | alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector); |
| 1010 | } |
| 1011 | } |
Stefano Stabellini | ca65f9f | 2010-07-29 14:37:48 +0100 | [diff] [blame] | 1012 | #else |
| 1013 | void xen_callback_vector(void) {} |
| 1014 | #endif |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 1015 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1016 | void __init xen_init_IRQ(void) |
| 1017 | { |
| 1018 | int i; |
Mike Travis | c7a3589 | 2009-01-10 21:58:11 -0800 | [diff] [blame] | 1019 | |
Pekka Enberg | a70c352 | 2009-07-01 11:51:18 +0300 | [diff] [blame] | 1020 | cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s), |
| 1021 | GFP_KERNEL); |
Christophe Saout | 28e0886 | 2009-01-11 11:46:23 -0800 | [diff] [blame] | 1022 | BUG_ON(cpu_evtchn_mask_p == NULL); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1023 | |
| 1024 | init_evtchn_cpu_bindings(); |
| 1025 | |
| 1026 | /* No event channels are 'live' right now. */ |
| 1027 | for (i = 0; i < NR_EVENT_CHANNELS; i++) |
| 1028 | mask_evtchn(i); |
| 1029 | |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 1030 | if (xen_hvm_domain()) { |
| 1031 | xen_callback_vector(); |
| 1032 | native_init_IRQ(); |
| 1033 | } else { |
| 1034 | irq_ctx_init(smp_processor_id()); |
| 1035 | } |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1036 | } |