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