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