Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / x86 / xen / events.c
blobdcf613e17581448926794286ac9c1fad05011857
1 /*
2 * Xen event channels
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.
11 * There are four kinds of events which can be mapped to an event
12 * channel:
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.
21 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
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>
30 #include <asm/ptrace.h>
31 #include <asm/irq.h>
32 #include <asm/sync_bitops.h>
33 #include <asm/xen/hypercall.h>
34 #include <asm/xen/hypervisor.h>
36 #include <xen/events.h>
37 #include <xen/interface/xen.h>
38 #include <xen/interface/event_channel.h>
40 #include "xen-ops.h"
43 * This lock protects updates to the following mapping and reference-count
44 * arrays. The lock does not need to be acquired to read the mapping tables.
46 static DEFINE_SPINLOCK(irq_mapping_update_lock);
48 /* IRQ <-> VIRQ mapping. */
49 static DEFINE_PER_CPU(int, virq_to_irq[NR_VIRQS]) = {[0 ... NR_VIRQS-1] = -1};
51 /* IRQ <-> IPI mapping */
52 static DEFINE_PER_CPU(int, ipi_to_irq[XEN_NR_IPIS]) = {[0 ... XEN_NR_IPIS-1] = -1};
54 /* Packed IRQ information: binding type, sub-type index, and event channel. */
55 struct packed_irq
57 unsigned short evtchn;
58 unsigned char index;
59 unsigned char type;
62 static struct packed_irq irq_info[NR_IRQS];
64 /* Binding types. */
65 enum {
66 IRQT_UNBOUND,
67 IRQT_PIRQ,
68 IRQT_VIRQ,
69 IRQT_IPI,
70 IRQT_EVTCHN
73 /* Convenient shorthand for packed representation of an unbound IRQ. */
74 #define IRQ_UNBOUND mk_irq_info(IRQT_UNBOUND, 0, 0)
76 static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
77 [0 ... NR_EVENT_CHANNELS-1] = -1
79 static unsigned long cpu_evtchn_mask[NR_CPUS][NR_EVENT_CHANNELS/BITS_PER_LONG];
80 static u8 cpu_evtchn[NR_EVENT_CHANNELS];
82 /* Reference counts for bindings to IRQs. */
83 static int irq_bindcount[NR_IRQS];
85 /* Xen will never allocate port zero for any purpose. */
86 #define VALID_EVTCHN(chn) ((chn) != 0)
89 * Force a proper event-channel callback from Xen after clearing the
90 * callback mask. We do this in a very simple manner, by making a call
91 * down into Xen. The pending flag will be checked by Xen on return.
93 void force_evtchn_callback(void)
95 (void)HYPERVISOR_xen_version(0, NULL);
97 EXPORT_SYMBOL_GPL(force_evtchn_callback);
99 static struct irq_chip xen_dynamic_chip;
101 /* Constructor for packed IRQ information. */
102 static inline struct packed_irq mk_irq_info(u32 type, u32 index, u32 evtchn)
104 return (struct packed_irq) { evtchn, index, type };
108 * Accessors for packed IRQ information.
110 static inline unsigned int evtchn_from_irq(int irq)
112 return irq_info[irq].evtchn;
115 static inline unsigned int index_from_irq(int irq)
117 return irq_info[irq].index;
120 static inline unsigned int type_from_irq(int irq)
122 return irq_info[irq].type;
125 static inline unsigned long active_evtchns(unsigned int cpu,
126 struct shared_info *sh,
127 unsigned int idx)
129 return (sh->evtchn_pending[idx] &
130 cpu_evtchn_mask[cpu][idx] &
131 ~sh->evtchn_mask[idx]);
134 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
136 int irq = evtchn_to_irq[chn];
138 BUG_ON(irq == -1);
139 #ifdef CONFIG_SMP
140 irq_desc[irq].affinity = cpumask_of_cpu(cpu);
141 #endif
143 __clear_bit(chn, cpu_evtchn_mask[cpu_evtchn[chn]]);
144 __set_bit(chn, cpu_evtchn_mask[cpu]);
146 cpu_evtchn[chn] = cpu;
149 static void init_evtchn_cpu_bindings(void)
151 #ifdef CONFIG_SMP
152 int i;
153 /* By default all event channels notify CPU#0. */
154 for (i = 0; i < NR_IRQS; i++)
155 irq_desc[i].affinity = cpumask_of_cpu(0);
156 #endif
158 memset(cpu_evtchn, 0, sizeof(cpu_evtchn));
159 memset(cpu_evtchn_mask[0], ~0, sizeof(cpu_evtchn_mask[0]));
162 static inline unsigned int cpu_from_evtchn(unsigned int evtchn)
164 return cpu_evtchn[evtchn];
167 static inline void clear_evtchn(int port)
169 struct shared_info *s = HYPERVISOR_shared_info;
170 sync_clear_bit(port, &s->evtchn_pending[0]);
173 static inline void set_evtchn(int port)
175 struct shared_info *s = HYPERVISOR_shared_info;
176 sync_set_bit(port, &s->evtchn_pending[0]);
181 * notify_remote_via_irq - send event to remote end of event channel via irq
182 * @irq: irq of event channel to send event to
184 * Unlike notify_remote_via_evtchn(), this is safe to use across
185 * save/restore. Notifications on a broken connection are silently
186 * dropped.
188 void notify_remote_via_irq(int irq)
190 int evtchn = evtchn_from_irq(irq);
192 if (VALID_EVTCHN(evtchn))
193 notify_remote_via_evtchn(evtchn);
195 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
197 static void mask_evtchn(int port)
199 struct shared_info *s = HYPERVISOR_shared_info;
200 sync_set_bit(port, &s->evtchn_mask[0]);
203 static void unmask_evtchn(int port)
205 struct shared_info *s = HYPERVISOR_shared_info;
206 unsigned int cpu = get_cpu();
208 BUG_ON(!irqs_disabled());
210 /* Slow path (hypercall) if this is a non-local port. */
211 if (unlikely(cpu != cpu_from_evtchn(port))) {
212 struct evtchn_unmask unmask = { .port = port };
213 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
214 } else {
215 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
217 sync_clear_bit(port, &s->evtchn_mask[0]);
220 * The following is basically the equivalent of
221 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
222 * the interrupt edge' if the channel is masked.
224 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
225 !sync_test_and_set_bit(port / BITS_PER_LONG,
226 &vcpu_info->evtchn_pending_sel))
227 vcpu_info->evtchn_upcall_pending = 1;
230 put_cpu();
233 static int find_unbound_irq(void)
235 int irq;
237 /* Only allocate from dynirq range */
238 for (irq = 0; irq < NR_IRQS; irq++)
239 if (irq_bindcount[irq] == 0)
240 break;
242 if (irq == NR_IRQS)
243 panic("No available IRQ to bind to: increase NR_IRQS!\n");
245 return irq;
248 int bind_evtchn_to_irq(unsigned int evtchn)
250 int irq;
252 spin_lock(&irq_mapping_update_lock);
254 irq = evtchn_to_irq[evtchn];
256 if (irq == -1) {
257 irq = find_unbound_irq();
259 dynamic_irq_init(irq);
260 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
261 handle_level_irq, "event");
263 evtchn_to_irq[evtchn] = irq;
264 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
267 irq_bindcount[irq]++;
269 spin_unlock(&irq_mapping_update_lock);
271 return irq;
273 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
275 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
277 struct evtchn_bind_ipi bind_ipi;
278 int evtchn, irq;
280 spin_lock(&irq_mapping_update_lock);
282 irq = per_cpu(ipi_to_irq, cpu)[ipi];
283 if (irq == -1) {
284 irq = find_unbound_irq();
285 if (irq < 0)
286 goto out;
288 dynamic_irq_init(irq);
289 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
290 handle_level_irq, "ipi");
292 bind_ipi.vcpu = cpu;
293 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
294 &bind_ipi) != 0)
295 BUG();
296 evtchn = bind_ipi.port;
298 evtchn_to_irq[evtchn] = irq;
299 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
301 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
303 bind_evtchn_to_cpu(evtchn, cpu);
306 irq_bindcount[irq]++;
308 out:
309 spin_unlock(&irq_mapping_update_lock);
310 return irq;
314 static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
316 struct evtchn_bind_virq bind_virq;
317 int evtchn, irq;
319 spin_lock(&irq_mapping_update_lock);
321 irq = per_cpu(virq_to_irq, cpu)[virq];
323 if (irq == -1) {
324 bind_virq.virq = virq;
325 bind_virq.vcpu = cpu;
326 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
327 &bind_virq) != 0)
328 BUG();
329 evtchn = bind_virq.port;
331 irq = find_unbound_irq();
333 dynamic_irq_init(irq);
334 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
335 handle_level_irq, "virq");
337 evtchn_to_irq[evtchn] = irq;
338 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
340 per_cpu(virq_to_irq, cpu)[virq] = irq;
342 bind_evtchn_to_cpu(evtchn, cpu);
345 irq_bindcount[irq]++;
347 spin_unlock(&irq_mapping_update_lock);
349 return irq;
352 static void unbind_from_irq(unsigned int irq)
354 struct evtchn_close close;
355 int evtchn = evtchn_from_irq(irq);
357 spin_lock(&irq_mapping_update_lock);
359 if (VALID_EVTCHN(evtchn) && (--irq_bindcount[irq] == 0)) {
360 close.port = evtchn;
361 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
362 BUG();
364 switch (type_from_irq(irq)) {
365 case IRQT_VIRQ:
366 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
367 [index_from_irq(irq)] = -1;
368 break;
369 default:
370 break;
373 /* Closed ports are implicitly re-bound to VCPU0. */
374 bind_evtchn_to_cpu(evtchn, 0);
376 evtchn_to_irq[evtchn] = -1;
377 irq_info[irq] = IRQ_UNBOUND;
379 dynamic_irq_init(irq);
382 spin_unlock(&irq_mapping_update_lock);
385 int bind_evtchn_to_irqhandler(unsigned int evtchn,
386 irq_handler_t handler,
387 unsigned long irqflags,
388 const char *devname, void *dev_id)
390 unsigned int irq;
391 int retval;
393 irq = bind_evtchn_to_irq(evtchn);
394 retval = request_irq(irq, handler, irqflags, devname, dev_id);
395 if (retval != 0) {
396 unbind_from_irq(irq);
397 return retval;
400 return irq;
402 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
404 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
405 irq_handler_t handler,
406 unsigned long irqflags, const char *devname, void *dev_id)
408 unsigned int irq;
409 int retval;
411 irq = bind_virq_to_irq(virq, cpu);
412 retval = request_irq(irq, handler, irqflags, devname, dev_id);
413 if (retval != 0) {
414 unbind_from_irq(irq);
415 return retval;
418 return irq;
420 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
422 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
423 unsigned int cpu,
424 irq_handler_t handler,
425 unsigned long irqflags,
426 const char *devname,
427 void *dev_id)
429 int irq, retval;
431 irq = bind_ipi_to_irq(ipi, cpu);
432 if (irq < 0)
433 return irq;
435 retval = request_irq(irq, handler, irqflags, devname, dev_id);
436 if (retval != 0) {
437 unbind_from_irq(irq);
438 return retval;
441 return irq;
444 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
446 free_irq(irq, dev_id);
447 unbind_from_irq(irq);
449 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
451 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
453 int irq = per_cpu(ipi_to_irq, cpu)[vector];
454 BUG_ON(irq < 0);
455 notify_remote_via_irq(irq);
460 * Search the CPUs pending events bitmasks. For each one found, map
461 * the event number to an irq, and feed it into do_IRQ() for
462 * handling.
464 * Xen uses a two-level bitmap to speed searching. The first level is
465 * a bitset of words which contain pending event bits. The second
466 * level is a bitset of pending events themselves.
468 void xen_evtchn_do_upcall(struct pt_regs *regs)
470 int cpu = get_cpu();
471 struct shared_info *s = HYPERVISOR_shared_info;
472 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
473 unsigned long pending_words;
475 vcpu_info->evtchn_upcall_pending = 0;
477 /* NB. No need for a barrier here -- XCHG is a barrier on x86. */
478 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
479 while (pending_words != 0) {
480 unsigned long pending_bits;
481 int word_idx = __ffs(pending_words);
482 pending_words &= ~(1UL << word_idx);
484 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
485 int bit_idx = __ffs(pending_bits);
486 int port = (word_idx * BITS_PER_LONG) + bit_idx;
487 int irq = evtchn_to_irq[port];
489 if (irq != -1) {
490 regs->orig_ax = ~irq;
491 do_IRQ(regs);
496 put_cpu();
499 /* Rebind an evtchn so that it gets delivered to a specific cpu */
500 static void rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
502 struct evtchn_bind_vcpu bind_vcpu;
503 int evtchn = evtchn_from_irq(irq);
505 if (!VALID_EVTCHN(evtchn))
506 return;
508 /* Send future instances of this interrupt to other vcpu. */
509 bind_vcpu.port = evtchn;
510 bind_vcpu.vcpu = tcpu;
513 * If this fails, it usually just indicates that we're dealing with a
514 * virq or IPI channel, which don't actually need to be rebound. Ignore
515 * it, but don't do the xenlinux-level rebind in that case.
517 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
518 bind_evtchn_to_cpu(evtchn, tcpu);
522 static void set_affinity_irq(unsigned irq, cpumask_t dest)
524 unsigned tcpu = first_cpu(dest);
525 rebind_irq_to_cpu(irq, tcpu);
528 static void enable_dynirq(unsigned int irq)
530 int evtchn = evtchn_from_irq(irq);
532 if (VALID_EVTCHN(evtchn))
533 unmask_evtchn(evtchn);
536 static void disable_dynirq(unsigned int irq)
538 int evtchn = evtchn_from_irq(irq);
540 if (VALID_EVTCHN(evtchn))
541 mask_evtchn(evtchn);
544 static void ack_dynirq(unsigned int irq)
546 int evtchn = evtchn_from_irq(irq);
548 move_native_irq(irq);
550 if (VALID_EVTCHN(evtchn))
551 clear_evtchn(evtchn);
554 static int retrigger_dynirq(unsigned int irq)
556 int evtchn = evtchn_from_irq(irq);
557 int ret = 0;
559 if (VALID_EVTCHN(evtchn)) {
560 set_evtchn(evtchn);
561 ret = 1;
564 return ret;
567 static struct irq_chip xen_dynamic_chip __read_mostly = {
568 .name = "xen-dyn",
569 .mask = disable_dynirq,
570 .unmask = enable_dynirq,
571 .ack = ack_dynirq,
572 .set_affinity = set_affinity_irq,
573 .retrigger = retrigger_dynirq,
576 void __init xen_init_IRQ(void)
578 int i;
580 init_evtchn_cpu_bindings();
582 /* No event channels are 'live' right now. */
583 for (i = 0; i < NR_EVENT_CHANNELS; i++)
584 mask_evtchn(i);
586 /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
587 for (i = 0; i < NR_IRQS; i++)
588 irq_bindcount[i] = 0;
590 irq_ctx_init(smp_processor_id());