Merge tag 'for-f2fs-3.11' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk...
[linux-2.6.git] / drivers / xen / events.c
blob6a6bbe4ede92c67afe4c88efd105839f14b3a240
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 received, 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. PIRQs - Hardware interrupts.
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>
29 #include <linux/bootmem.h>
30 #include <linux/slab.h>
31 #include <linux/irqnr.h>
32 #include <linux/pci.h>
34 #ifdef CONFIG_X86
35 #include <asm/desc.h>
36 #include <asm/ptrace.h>
37 #include <asm/irq.h>
38 #include <asm/idle.h>
39 #include <asm/io_apic.h>
40 #include <asm/xen/page.h>
41 #include <asm/xen/pci.h>
42 #endif
43 #include <asm/sync_bitops.h>
44 #include <asm/xen/hypercall.h>
45 #include <asm/xen/hypervisor.h>
47 #include <xen/xen.h>
48 #include <xen/hvm.h>
49 #include <xen/xen-ops.h>
50 #include <xen/events.h>
51 #include <xen/interface/xen.h>
52 #include <xen/interface/event_channel.h>
53 #include <xen/interface/hvm/hvm_op.h>
54 #include <xen/interface/hvm/params.h>
55 #include <xen/interface/physdev.h>
56 #include <xen/interface/sched.h>
57 #include <asm/hw_irq.h>
60 * This lock protects updates to the following mapping and reference-count
61 * arrays. The lock does not need to be acquired to read the mapping tables.
63 static DEFINE_MUTEX(irq_mapping_update_lock);
65 static LIST_HEAD(xen_irq_list_head);
67 /* IRQ <-> VIRQ mapping. */
68 static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
70 /* IRQ <-> IPI mapping */
71 static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
73 /* Interrupt types. */
74 enum xen_irq_type {
75 IRQT_UNBOUND = 0,
76 IRQT_PIRQ,
77 IRQT_VIRQ,
78 IRQT_IPI,
79 IRQT_EVTCHN
83 * Packed IRQ information:
84 * type - enum xen_irq_type
85 * event channel - irq->event channel mapping
86 * cpu - cpu this event channel is bound to
87 * index - type-specific information:
88 * PIRQ - physical IRQ, GSI, flags, and owner domain
89 * VIRQ - virq number
90 * IPI - IPI vector
91 * EVTCHN -
93 struct irq_info {
94 struct list_head list;
95 int refcnt;
96 enum xen_irq_type type; /* type */
97 unsigned irq;
98 unsigned short evtchn; /* event channel */
99 unsigned short cpu; /* cpu bound */
101 union {
102 unsigned short virq;
103 enum ipi_vector ipi;
104 struct {
105 unsigned short pirq;
106 unsigned short gsi;
107 unsigned char flags;
108 uint16_t domid;
109 } pirq;
110 } u;
112 #define PIRQ_NEEDS_EOI (1 << 0)
113 #define PIRQ_SHAREABLE (1 << 1)
115 static int *evtchn_to_irq;
116 #ifdef CONFIG_X86
117 static unsigned long *pirq_eoi_map;
118 #endif
119 static bool (*pirq_needs_eoi)(unsigned irq);
122 * Note sizeof(xen_ulong_t) can be more than sizeof(unsigned long). Be
123 * careful to only use bitops which allow for this (e.g
124 * test_bit/find_first_bit and friends but not __ffs) and to pass
125 * BITS_PER_EVTCHN_WORD as the bitmask length.
127 #define BITS_PER_EVTCHN_WORD (sizeof(xen_ulong_t)*8)
129 * Make a bitmask (i.e. unsigned long *) of a xen_ulong_t
130 * array. Primarily to avoid long lines (hence the terse name).
132 #define BM(x) (unsigned long *)(x)
133 /* Find the first set bit in a evtchn mask */
134 #define EVTCHN_FIRST_BIT(w) find_first_bit(BM(&(w)), BITS_PER_EVTCHN_WORD)
136 static DEFINE_PER_CPU(xen_ulong_t [NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD],
137 cpu_evtchn_mask);
139 /* Xen will never allocate port zero for any purpose. */
140 #define VALID_EVTCHN(chn) ((chn) != 0)
142 static struct irq_chip xen_dynamic_chip;
143 static struct irq_chip xen_percpu_chip;
144 static struct irq_chip xen_pirq_chip;
145 static void enable_dynirq(struct irq_data *data);
146 static void disable_dynirq(struct irq_data *data);
148 /* Get info for IRQ */
149 static struct irq_info *info_for_irq(unsigned irq)
151 return irq_get_handler_data(irq);
154 /* Constructors for packed IRQ information. */
155 static void xen_irq_info_common_init(struct irq_info *info,
156 unsigned irq,
157 enum xen_irq_type type,
158 unsigned short evtchn,
159 unsigned short cpu)
162 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
164 info->type = type;
165 info->irq = irq;
166 info->evtchn = evtchn;
167 info->cpu = cpu;
169 evtchn_to_irq[evtchn] = irq;
171 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
174 static void xen_irq_info_evtchn_init(unsigned irq,
175 unsigned short evtchn)
177 struct irq_info *info = info_for_irq(irq);
179 xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
182 static void xen_irq_info_ipi_init(unsigned cpu,
183 unsigned irq,
184 unsigned short evtchn,
185 enum ipi_vector ipi)
187 struct irq_info *info = info_for_irq(irq);
189 xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
191 info->u.ipi = ipi;
193 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
196 static void xen_irq_info_virq_init(unsigned cpu,
197 unsigned irq,
198 unsigned short evtchn,
199 unsigned short virq)
201 struct irq_info *info = info_for_irq(irq);
203 xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
205 info->u.virq = virq;
207 per_cpu(virq_to_irq, cpu)[virq] = irq;
210 static void xen_irq_info_pirq_init(unsigned irq,
211 unsigned short evtchn,
212 unsigned short pirq,
213 unsigned short gsi,
214 uint16_t domid,
215 unsigned char flags)
217 struct irq_info *info = info_for_irq(irq);
219 xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
221 info->u.pirq.pirq = pirq;
222 info->u.pirq.gsi = gsi;
223 info->u.pirq.domid = domid;
224 info->u.pirq.flags = flags;
228 * Accessors for packed IRQ information.
230 static unsigned int evtchn_from_irq(unsigned irq)
232 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
233 return 0;
235 return info_for_irq(irq)->evtchn;
238 unsigned irq_from_evtchn(unsigned int evtchn)
240 return evtchn_to_irq[evtchn];
242 EXPORT_SYMBOL_GPL(irq_from_evtchn);
244 static enum ipi_vector ipi_from_irq(unsigned irq)
246 struct irq_info *info = info_for_irq(irq);
248 BUG_ON(info == NULL);
249 BUG_ON(info->type != IRQT_IPI);
251 return info->u.ipi;
254 static unsigned virq_from_irq(unsigned irq)
256 struct irq_info *info = info_for_irq(irq);
258 BUG_ON(info == NULL);
259 BUG_ON(info->type != IRQT_VIRQ);
261 return info->u.virq;
264 static unsigned pirq_from_irq(unsigned irq)
266 struct irq_info *info = info_for_irq(irq);
268 BUG_ON(info == NULL);
269 BUG_ON(info->type != IRQT_PIRQ);
271 return info->u.pirq.pirq;
274 static enum xen_irq_type type_from_irq(unsigned irq)
276 return info_for_irq(irq)->type;
279 static unsigned cpu_from_irq(unsigned irq)
281 return info_for_irq(irq)->cpu;
284 static unsigned int cpu_from_evtchn(unsigned int evtchn)
286 int irq = evtchn_to_irq[evtchn];
287 unsigned ret = 0;
289 if (irq != -1)
290 ret = cpu_from_irq(irq);
292 return ret;
295 #ifdef CONFIG_X86
296 static bool pirq_check_eoi_map(unsigned irq)
298 return test_bit(pirq_from_irq(irq), pirq_eoi_map);
300 #endif
302 static bool pirq_needs_eoi_flag(unsigned irq)
304 struct irq_info *info = info_for_irq(irq);
305 BUG_ON(info->type != IRQT_PIRQ);
307 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
310 static inline xen_ulong_t active_evtchns(unsigned int cpu,
311 struct shared_info *sh,
312 unsigned int idx)
314 return sh->evtchn_pending[idx] &
315 per_cpu(cpu_evtchn_mask, cpu)[idx] &
316 ~sh->evtchn_mask[idx];
319 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
321 int irq = evtchn_to_irq[chn];
323 BUG_ON(irq == -1);
324 #ifdef CONFIG_SMP
325 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
326 #endif
328 clear_bit(chn, BM(per_cpu(cpu_evtchn_mask, cpu_from_irq(irq))));
329 set_bit(chn, BM(per_cpu(cpu_evtchn_mask, cpu)));
331 info_for_irq(irq)->cpu = cpu;
334 static void init_evtchn_cpu_bindings(void)
336 int i;
337 #ifdef CONFIG_SMP
338 struct irq_info *info;
340 /* By default all event channels notify CPU#0. */
341 list_for_each_entry(info, &xen_irq_list_head, list) {
342 struct irq_desc *desc = irq_to_desc(info->irq);
343 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
345 #endif
347 for_each_possible_cpu(i)
348 memset(per_cpu(cpu_evtchn_mask, i),
349 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
352 static inline void clear_evtchn(int port)
354 struct shared_info *s = HYPERVISOR_shared_info;
355 sync_clear_bit(port, BM(&s->evtchn_pending[0]));
358 static inline void set_evtchn(int port)
360 struct shared_info *s = HYPERVISOR_shared_info;
361 sync_set_bit(port, BM(&s->evtchn_pending[0]));
364 static inline int test_evtchn(int port)
366 struct shared_info *s = HYPERVISOR_shared_info;
367 return sync_test_bit(port, BM(&s->evtchn_pending[0]));
372 * notify_remote_via_irq - send event to remote end of event channel via irq
373 * @irq: irq of event channel to send event to
375 * Unlike notify_remote_via_evtchn(), this is safe to use across
376 * save/restore. Notifications on a broken connection are silently
377 * dropped.
379 void notify_remote_via_irq(int irq)
381 int evtchn = evtchn_from_irq(irq);
383 if (VALID_EVTCHN(evtchn))
384 notify_remote_via_evtchn(evtchn);
386 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
388 static void mask_evtchn(int port)
390 struct shared_info *s = HYPERVISOR_shared_info;
391 sync_set_bit(port, BM(&s->evtchn_mask[0]));
394 static void unmask_evtchn(int port)
396 struct shared_info *s = HYPERVISOR_shared_info;
397 unsigned int cpu = get_cpu();
398 int do_hypercall = 0, evtchn_pending = 0;
400 BUG_ON(!irqs_disabled());
402 if (unlikely((cpu != cpu_from_evtchn(port))))
403 do_hypercall = 1;
404 else {
406 * Need to clear the mask before checking pending to
407 * avoid a race with an event becoming pending.
409 * EVTCHNOP_unmask will only trigger an upcall if the
410 * mask bit was set, so if a hypercall is needed
411 * remask the event.
413 sync_clear_bit(port, BM(&s->evtchn_mask[0]));
414 evtchn_pending = sync_test_bit(port, BM(&s->evtchn_pending[0]));
416 if (unlikely(evtchn_pending && xen_hvm_domain())) {
417 sync_set_bit(port, BM(&s->evtchn_mask[0]));
418 do_hypercall = 1;
422 /* Slow path (hypercall) if this is a non-local port or if this is
423 * an hvm domain and an event is pending (hvm domains don't have
424 * their own implementation of irq_enable). */
425 if (do_hypercall) {
426 struct evtchn_unmask unmask = { .port = port };
427 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
428 } else {
429 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
432 * The following is basically the equivalent of
433 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
434 * the interrupt edge' if the channel is masked.
436 if (evtchn_pending &&
437 !sync_test_and_set_bit(port / BITS_PER_EVTCHN_WORD,
438 BM(&vcpu_info->evtchn_pending_sel)))
439 vcpu_info->evtchn_upcall_pending = 1;
442 put_cpu();
445 static void xen_irq_init(unsigned irq)
447 struct irq_info *info;
448 #ifdef CONFIG_SMP
449 struct irq_desc *desc = irq_to_desc(irq);
451 /* By default all event channels notify CPU#0. */
452 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
453 #endif
455 info = kzalloc(sizeof(*info), GFP_KERNEL);
456 if (info == NULL)
457 panic("Unable to allocate metadata for IRQ%d\n", irq);
459 info->type = IRQT_UNBOUND;
460 info->refcnt = -1;
462 irq_set_handler_data(irq, info);
464 list_add_tail(&info->list, &xen_irq_list_head);
467 static int __must_check xen_allocate_irq_dynamic(void)
469 int first = 0;
470 int irq;
472 #ifdef CONFIG_X86_IO_APIC
474 * For an HVM guest or domain 0 which see "real" (emulated or
475 * actual respectively) GSIs we allocate dynamic IRQs
476 * e.g. those corresponding to event channels or MSIs
477 * etc. from the range above those "real" GSIs to avoid
478 * collisions.
480 if (xen_initial_domain() || xen_hvm_domain())
481 first = get_nr_irqs_gsi();
482 #endif
484 irq = irq_alloc_desc_from(first, -1);
486 if (irq >= 0)
487 xen_irq_init(irq);
489 return irq;
492 static int __must_check xen_allocate_irq_gsi(unsigned gsi)
494 int irq;
497 * A PV guest has no concept of a GSI (since it has no ACPI
498 * nor access to/knowledge of the physical APICs). Therefore
499 * all IRQs are dynamically allocated from the entire IRQ
500 * space.
502 if (xen_pv_domain() && !xen_initial_domain())
503 return xen_allocate_irq_dynamic();
505 /* Legacy IRQ descriptors are already allocated by the arch. */
506 if (gsi < NR_IRQS_LEGACY)
507 irq = gsi;
508 else
509 irq = irq_alloc_desc_at(gsi, -1);
511 xen_irq_init(irq);
513 return irq;
516 static void xen_free_irq(unsigned irq)
518 struct irq_info *info = irq_get_handler_data(irq);
520 if (WARN_ON(!info))
521 return;
523 list_del(&info->list);
525 irq_set_handler_data(irq, NULL);
527 WARN_ON(info->refcnt > 0);
529 kfree(info);
531 /* Legacy IRQ descriptors are managed by the arch. */
532 if (irq < NR_IRQS_LEGACY)
533 return;
535 irq_free_desc(irq);
538 static void pirq_query_unmask(int irq)
540 struct physdev_irq_status_query irq_status;
541 struct irq_info *info = info_for_irq(irq);
543 BUG_ON(info->type != IRQT_PIRQ);
545 irq_status.irq = pirq_from_irq(irq);
546 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
547 irq_status.flags = 0;
549 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
550 if (irq_status.flags & XENIRQSTAT_needs_eoi)
551 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
554 static bool probing_irq(int irq)
556 struct irq_desc *desc = irq_to_desc(irq);
558 return desc && desc->action == NULL;
561 static void eoi_pirq(struct irq_data *data)
563 int evtchn = evtchn_from_irq(data->irq);
564 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
565 int rc = 0;
567 irq_move_irq(data);
569 if (VALID_EVTCHN(evtchn))
570 clear_evtchn(evtchn);
572 if (pirq_needs_eoi(data->irq)) {
573 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
574 WARN_ON(rc);
578 static void mask_ack_pirq(struct irq_data *data)
580 disable_dynirq(data);
581 eoi_pirq(data);
584 static unsigned int __startup_pirq(unsigned int irq)
586 struct evtchn_bind_pirq bind_pirq;
587 struct irq_info *info = info_for_irq(irq);
588 int evtchn = evtchn_from_irq(irq);
589 int rc;
591 BUG_ON(info->type != IRQT_PIRQ);
593 if (VALID_EVTCHN(evtchn))
594 goto out;
596 bind_pirq.pirq = pirq_from_irq(irq);
597 /* NB. We are happy to share unless we are probing. */
598 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
599 BIND_PIRQ__WILL_SHARE : 0;
600 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
601 if (rc != 0) {
602 if (!probing_irq(irq))
603 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
604 irq);
605 return 0;
607 evtchn = bind_pirq.port;
609 pirq_query_unmask(irq);
611 evtchn_to_irq[evtchn] = irq;
612 bind_evtchn_to_cpu(evtchn, 0);
613 info->evtchn = evtchn;
615 out:
616 unmask_evtchn(evtchn);
617 eoi_pirq(irq_get_irq_data(irq));
619 return 0;
622 static unsigned int startup_pirq(struct irq_data *data)
624 return __startup_pirq(data->irq);
627 static void shutdown_pirq(struct irq_data *data)
629 struct evtchn_close close;
630 unsigned int irq = data->irq;
631 struct irq_info *info = info_for_irq(irq);
632 int evtchn = evtchn_from_irq(irq);
634 BUG_ON(info->type != IRQT_PIRQ);
636 if (!VALID_EVTCHN(evtchn))
637 return;
639 mask_evtchn(evtchn);
641 close.port = evtchn;
642 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
643 BUG();
645 bind_evtchn_to_cpu(evtchn, 0);
646 evtchn_to_irq[evtchn] = -1;
647 info->evtchn = 0;
650 static void enable_pirq(struct irq_data *data)
652 startup_pirq(data);
655 static void disable_pirq(struct irq_data *data)
657 disable_dynirq(data);
660 int xen_irq_from_gsi(unsigned gsi)
662 struct irq_info *info;
664 list_for_each_entry(info, &xen_irq_list_head, list) {
665 if (info->type != IRQT_PIRQ)
666 continue;
668 if (info->u.pirq.gsi == gsi)
669 return info->irq;
672 return -1;
674 EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
677 * Do not make any assumptions regarding the relationship between the
678 * IRQ number returned here and the Xen pirq argument.
680 * Note: We don't assign an event channel until the irq actually started
681 * up. Return an existing irq if we've already got one for the gsi.
683 * Shareable implies level triggered, not shareable implies edge
684 * triggered here.
686 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
687 unsigned pirq, int shareable, char *name)
689 int irq = -1;
690 struct physdev_irq irq_op;
692 mutex_lock(&irq_mapping_update_lock);
694 irq = xen_irq_from_gsi(gsi);
695 if (irq != -1) {
696 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
697 irq, gsi);
698 goto out;
701 irq = xen_allocate_irq_gsi(gsi);
702 if (irq < 0)
703 goto out;
705 irq_op.irq = irq;
706 irq_op.vector = 0;
708 /* Only the privileged domain can do this. For non-priv, the pcifront
709 * driver provides a PCI bus that does the call to do exactly
710 * this in the priv domain. */
711 if (xen_initial_domain() &&
712 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
713 xen_free_irq(irq);
714 irq = -ENOSPC;
715 goto out;
718 xen_irq_info_pirq_init(irq, 0, pirq, gsi, DOMID_SELF,
719 shareable ? PIRQ_SHAREABLE : 0);
721 pirq_query_unmask(irq);
722 /* We try to use the handler with the appropriate semantic for the
723 * type of interrupt: if the interrupt is an edge triggered
724 * interrupt we use handle_edge_irq.
726 * On the other hand if the interrupt is level triggered we use
727 * handle_fasteoi_irq like the native code does for this kind of
728 * interrupts.
730 * Depending on the Xen version, pirq_needs_eoi might return true
731 * not only for level triggered interrupts but for edge triggered
732 * interrupts too. In any case Xen always honors the eoi mechanism,
733 * not injecting any more pirqs of the same kind if the first one
734 * hasn't received an eoi yet. Therefore using the fasteoi handler
735 * is the right choice either way.
737 if (shareable)
738 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
739 handle_fasteoi_irq, name);
740 else
741 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
742 handle_edge_irq, name);
744 out:
745 mutex_unlock(&irq_mapping_update_lock);
747 return irq;
750 #ifdef CONFIG_PCI_MSI
751 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
753 int rc;
754 struct physdev_get_free_pirq op_get_free_pirq;
756 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
757 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
759 WARN_ONCE(rc == -ENOSYS,
760 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
762 return rc ? -1 : op_get_free_pirq.pirq;
765 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
766 int pirq, const char *name, domid_t domid)
768 int irq, ret;
770 mutex_lock(&irq_mapping_update_lock);
772 irq = xen_allocate_irq_dynamic();
773 if (irq < 0)
774 goto out;
776 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
777 name);
779 xen_irq_info_pirq_init(irq, 0, pirq, 0, domid, 0);
780 ret = irq_set_msi_desc(irq, msidesc);
781 if (ret < 0)
782 goto error_irq;
783 out:
784 mutex_unlock(&irq_mapping_update_lock);
785 return irq;
786 error_irq:
787 mutex_unlock(&irq_mapping_update_lock);
788 xen_free_irq(irq);
789 return ret;
791 #endif
793 int xen_destroy_irq(int irq)
795 struct irq_desc *desc;
796 struct physdev_unmap_pirq unmap_irq;
797 struct irq_info *info = info_for_irq(irq);
798 int rc = -ENOENT;
800 mutex_lock(&irq_mapping_update_lock);
802 desc = irq_to_desc(irq);
803 if (!desc)
804 goto out;
806 if (xen_initial_domain()) {
807 unmap_irq.pirq = info->u.pirq.pirq;
808 unmap_irq.domid = info->u.pirq.domid;
809 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
810 /* If another domain quits without making the pci_disable_msix
811 * call, the Xen hypervisor takes care of freeing the PIRQs
812 * (free_domain_pirqs).
814 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
815 printk(KERN_INFO "domain %d does not have %d anymore\n",
816 info->u.pirq.domid, info->u.pirq.pirq);
817 else if (rc) {
818 printk(KERN_WARNING "unmap irq failed %d\n", rc);
819 goto out;
823 xen_free_irq(irq);
825 out:
826 mutex_unlock(&irq_mapping_update_lock);
827 return rc;
830 int xen_irq_from_pirq(unsigned pirq)
832 int irq;
834 struct irq_info *info;
836 mutex_lock(&irq_mapping_update_lock);
838 list_for_each_entry(info, &xen_irq_list_head, list) {
839 if (info->type != IRQT_PIRQ)
840 continue;
841 irq = info->irq;
842 if (info->u.pirq.pirq == pirq)
843 goto out;
845 irq = -1;
846 out:
847 mutex_unlock(&irq_mapping_update_lock);
849 return irq;
853 int xen_pirq_from_irq(unsigned irq)
855 return pirq_from_irq(irq);
857 EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
858 int bind_evtchn_to_irq(unsigned int evtchn)
860 int irq;
862 mutex_lock(&irq_mapping_update_lock);
864 irq = evtchn_to_irq[evtchn];
866 if (irq == -1) {
867 irq = xen_allocate_irq_dynamic();
868 if (irq < 0)
869 goto out;
871 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
872 handle_edge_irq, "event");
874 xen_irq_info_evtchn_init(irq, evtchn);
875 } else {
876 struct irq_info *info = info_for_irq(irq);
877 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
880 out:
881 mutex_unlock(&irq_mapping_update_lock);
883 return irq;
885 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
887 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
889 struct evtchn_bind_ipi bind_ipi;
890 int evtchn, irq;
892 mutex_lock(&irq_mapping_update_lock);
894 irq = per_cpu(ipi_to_irq, cpu)[ipi];
896 if (irq == -1) {
897 irq = xen_allocate_irq_dynamic();
898 if (irq < 0)
899 goto out;
901 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
902 handle_percpu_irq, "ipi");
904 bind_ipi.vcpu = cpu;
905 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
906 &bind_ipi) != 0)
907 BUG();
908 evtchn = bind_ipi.port;
910 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
912 bind_evtchn_to_cpu(evtchn, cpu);
913 } else {
914 struct irq_info *info = info_for_irq(irq);
915 WARN_ON(info == NULL || info->type != IRQT_IPI);
918 out:
919 mutex_unlock(&irq_mapping_update_lock);
920 return irq;
923 static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
924 unsigned int remote_port)
926 struct evtchn_bind_interdomain bind_interdomain;
927 int err;
929 bind_interdomain.remote_dom = remote_domain;
930 bind_interdomain.remote_port = remote_port;
932 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
933 &bind_interdomain);
935 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
938 static int find_virq(unsigned int virq, unsigned int cpu)
940 struct evtchn_status status;
941 int port, rc = -ENOENT;
943 memset(&status, 0, sizeof(status));
944 for (port = 0; port <= NR_EVENT_CHANNELS; port++) {
945 status.dom = DOMID_SELF;
946 status.port = port;
947 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
948 if (rc < 0)
949 continue;
950 if (status.status != EVTCHNSTAT_virq)
951 continue;
952 if (status.u.virq == virq && status.vcpu == cpu) {
953 rc = port;
954 break;
957 return rc;
960 int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
962 struct evtchn_bind_virq bind_virq;
963 int evtchn, irq, ret;
965 mutex_lock(&irq_mapping_update_lock);
967 irq = per_cpu(virq_to_irq, cpu)[virq];
969 if (irq == -1) {
970 irq = xen_allocate_irq_dynamic();
971 if (irq < 0)
972 goto out;
974 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
975 handle_percpu_irq, "virq");
977 bind_virq.virq = virq;
978 bind_virq.vcpu = cpu;
979 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
980 &bind_virq);
981 if (ret == 0)
982 evtchn = bind_virq.port;
983 else {
984 if (ret == -EEXIST)
985 ret = find_virq(virq, cpu);
986 BUG_ON(ret < 0);
987 evtchn = ret;
990 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
992 bind_evtchn_to_cpu(evtchn, cpu);
993 } else {
994 struct irq_info *info = info_for_irq(irq);
995 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
998 out:
999 mutex_unlock(&irq_mapping_update_lock);
1001 return irq;
1004 static void unbind_from_irq(unsigned int irq)
1006 struct evtchn_close close;
1007 int evtchn = evtchn_from_irq(irq);
1008 struct irq_info *info = irq_get_handler_data(irq);
1010 if (WARN_ON(!info))
1011 return;
1013 mutex_lock(&irq_mapping_update_lock);
1015 if (info->refcnt > 0) {
1016 info->refcnt--;
1017 if (info->refcnt != 0)
1018 goto done;
1021 if (VALID_EVTCHN(evtchn)) {
1022 close.port = evtchn;
1023 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
1024 BUG();
1026 switch (type_from_irq(irq)) {
1027 case IRQT_VIRQ:
1028 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
1029 [virq_from_irq(irq)] = -1;
1030 break;
1031 case IRQT_IPI:
1032 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
1033 [ipi_from_irq(irq)] = -1;
1034 break;
1035 default:
1036 break;
1039 /* Closed ports are implicitly re-bound to VCPU0. */
1040 bind_evtchn_to_cpu(evtchn, 0);
1042 evtchn_to_irq[evtchn] = -1;
1045 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
1047 xen_free_irq(irq);
1049 done:
1050 mutex_unlock(&irq_mapping_update_lock);
1053 int bind_evtchn_to_irqhandler(unsigned int evtchn,
1054 irq_handler_t handler,
1055 unsigned long irqflags,
1056 const char *devname, void *dev_id)
1058 int irq, retval;
1060 irq = bind_evtchn_to_irq(evtchn);
1061 if (irq < 0)
1062 return irq;
1063 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1064 if (retval != 0) {
1065 unbind_from_irq(irq);
1066 return retval;
1069 return irq;
1071 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1073 int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1074 unsigned int remote_port,
1075 irq_handler_t handler,
1076 unsigned long irqflags,
1077 const char *devname,
1078 void *dev_id)
1080 int irq, retval;
1082 irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
1083 if (irq < 0)
1084 return irq;
1086 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1087 if (retval != 0) {
1088 unbind_from_irq(irq);
1089 return retval;
1092 return irq;
1094 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1096 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
1097 irq_handler_t handler,
1098 unsigned long irqflags, const char *devname, void *dev_id)
1100 int irq, retval;
1102 irq = bind_virq_to_irq(virq, cpu);
1103 if (irq < 0)
1104 return irq;
1105 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1106 if (retval != 0) {
1107 unbind_from_irq(irq);
1108 return retval;
1111 return irq;
1113 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1115 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1116 unsigned int cpu,
1117 irq_handler_t handler,
1118 unsigned long irqflags,
1119 const char *devname,
1120 void *dev_id)
1122 int irq, retval;
1124 irq = bind_ipi_to_irq(ipi, cpu);
1125 if (irq < 0)
1126 return irq;
1128 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
1129 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1130 if (retval != 0) {
1131 unbind_from_irq(irq);
1132 return retval;
1135 return irq;
1138 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1140 struct irq_info *info = irq_get_handler_data(irq);
1142 if (WARN_ON(!info))
1143 return;
1144 free_irq(irq, dev_id);
1145 unbind_from_irq(irq);
1147 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1149 int evtchn_make_refcounted(unsigned int evtchn)
1151 int irq = evtchn_to_irq[evtchn];
1152 struct irq_info *info;
1154 if (irq == -1)
1155 return -ENOENT;
1157 info = irq_get_handler_data(irq);
1159 if (!info)
1160 return -ENOENT;
1162 WARN_ON(info->refcnt != -1);
1164 info->refcnt = 1;
1166 return 0;
1168 EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1170 int evtchn_get(unsigned int evtchn)
1172 int irq;
1173 struct irq_info *info;
1174 int err = -ENOENT;
1176 if (evtchn >= NR_EVENT_CHANNELS)
1177 return -EINVAL;
1179 mutex_lock(&irq_mapping_update_lock);
1181 irq = evtchn_to_irq[evtchn];
1182 if (irq == -1)
1183 goto done;
1185 info = irq_get_handler_data(irq);
1187 if (!info)
1188 goto done;
1190 err = -EINVAL;
1191 if (info->refcnt <= 0)
1192 goto done;
1194 info->refcnt++;
1195 err = 0;
1196 done:
1197 mutex_unlock(&irq_mapping_update_lock);
1199 return err;
1201 EXPORT_SYMBOL_GPL(evtchn_get);
1203 void evtchn_put(unsigned int evtchn)
1205 int irq = evtchn_to_irq[evtchn];
1206 if (WARN_ON(irq == -1))
1207 return;
1208 unbind_from_irq(irq);
1210 EXPORT_SYMBOL_GPL(evtchn_put);
1212 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1214 int irq = per_cpu(ipi_to_irq, cpu)[vector];
1215 BUG_ON(irq < 0);
1216 notify_remote_via_irq(irq);
1219 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1221 struct shared_info *sh = HYPERVISOR_shared_info;
1222 int cpu = smp_processor_id();
1223 xen_ulong_t *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
1224 int i;
1225 unsigned long flags;
1226 static DEFINE_SPINLOCK(debug_lock);
1227 struct vcpu_info *v;
1229 spin_lock_irqsave(&debug_lock, flags);
1231 printk("\nvcpu %d\n ", cpu);
1233 for_each_online_cpu(i) {
1234 int pending;
1235 v = per_cpu(xen_vcpu, i);
1236 pending = (get_irq_regs() && i == cpu)
1237 ? xen_irqs_disabled(get_irq_regs())
1238 : v->evtchn_upcall_mask;
1239 printk("%d: masked=%d pending=%d event_sel %0*"PRI_xen_ulong"\n ", i,
1240 pending, v->evtchn_upcall_pending,
1241 (int)(sizeof(v->evtchn_pending_sel)*2),
1242 v->evtchn_pending_sel);
1244 v = per_cpu(xen_vcpu, cpu);
1246 printk("\npending:\n ");
1247 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
1248 printk("%0*"PRI_xen_ulong"%s",
1249 (int)sizeof(sh->evtchn_pending[0])*2,
1250 sh->evtchn_pending[i],
1251 i % 8 == 0 ? "\n " : " ");
1252 printk("\nglobal mask:\n ");
1253 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1254 printk("%0*"PRI_xen_ulong"%s",
1255 (int)(sizeof(sh->evtchn_mask[0])*2),
1256 sh->evtchn_mask[i],
1257 i % 8 == 0 ? "\n " : " ");
1259 printk("\nglobally unmasked:\n ");
1260 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1261 printk("%0*"PRI_xen_ulong"%s",
1262 (int)(sizeof(sh->evtchn_mask[0])*2),
1263 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1264 i % 8 == 0 ? "\n " : " ");
1266 printk("\nlocal cpu%d mask:\n ", cpu);
1267 for (i = (NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD)-1; i >= 0; i--)
1268 printk("%0*"PRI_xen_ulong"%s", (int)(sizeof(cpu_evtchn[0])*2),
1269 cpu_evtchn[i],
1270 i % 8 == 0 ? "\n " : " ");
1272 printk("\nlocally unmasked:\n ");
1273 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
1274 xen_ulong_t pending = sh->evtchn_pending[i]
1275 & ~sh->evtchn_mask[i]
1276 & cpu_evtchn[i];
1277 printk("%0*"PRI_xen_ulong"%s",
1278 (int)(sizeof(sh->evtchn_mask[0])*2),
1279 pending, i % 8 == 0 ? "\n " : " ");
1282 printk("\npending list:\n");
1283 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
1284 if (sync_test_bit(i, BM(sh->evtchn_pending))) {
1285 int word_idx = i / BITS_PER_EVTCHN_WORD;
1286 printk(" %d: event %d -> irq %d%s%s%s\n",
1287 cpu_from_evtchn(i), i,
1288 evtchn_to_irq[i],
1289 sync_test_bit(word_idx, BM(&v->evtchn_pending_sel))
1290 ? "" : " l2-clear",
1291 !sync_test_bit(i, BM(sh->evtchn_mask))
1292 ? "" : " globally-masked",
1293 sync_test_bit(i, BM(cpu_evtchn))
1294 ? "" : " locally-masked");
1298 spin_unlock_irqrestore(&debug_lock, flags);
1300 return IRQ_HANDLED;
1303 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1304 static DEFINE_PER_CPU(unsigned int, current_word_idx);
1305 static DEFINE_PER_CPU(unsigned int, current_bit_idx);
1308 * Mask out the i least significant bits of w
1310 #define MASK_LSBS(w, i) (w & ((~((xen_ulong_t)0UL)) << i))
1313 * Search the CPUs pending events bitmasks. For each one found, map
1314 * the event number to an irq, and feed it into do_IRQ() for
1315 * handling.
1317 * Xen uses a two-level bitmap to speed searching. The first level is
1318 * a bitset of words which contain pending event bits. The second
1319 * level is a bitset of pending events themselves.
1321 static void __xen_evtchn_do_upcall(void)
1323 int start_word_idx, start_bit_idx;
1324 int word_idx, bit_idx;
1325 int i, irq;
1326 int cpu = get_cpu();
1327 struct shared_info *s = HYPERVISOR_shared_info;
1328 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1329 unsigned count;
1331 do {
1332 xen_ulong_t pending_words;
1333 xen_ulong_t pending_bits;
1334 struct irq_desc *desc;
1336 vcpu_info->evtchn_upcall_pending = 0;
1338 if (__this_cpu_inc_return(xed_nesting_count) - 1)
1339 goto out;
1342 * Master flag must be cleared /before/ clearing
1343 * selector flag. xchg_xen_ulong must contain an
1344 * appropriate barrier.
1346 if ((irq = per_cpu(virq_to_irq, cpu)[VIRQ_TIMER]) != -1) {
1347 int evtchn = evtchn_from_irq(irq);
1348 word_idx = evtchn / BITS_PER_LONG;
1349 pending_bits = evtchn % BITS_PER_LONG;
1350 if (active_evtchns(cpu, s, word_idx) & (1ULL << pending_bits)) {
1351 desc = irq_to_desc(irq);
1352 if (desc)
1353 generic_handle_irq_desc(irq, desc);
1357 pending_words = xchg_xen_ulong(&vcpu_info->evtchn_pending_sel, 0);
1359 start_word_idx = __this_cpu_read(current_word_idx);
1360 start_bit_idx = __this_cpu_read(current_bit_idx);
1362 word_idx = start_word_idx;
1364 for (i = 0; pending_words != 0; i++) {
1365 xen_ulong_t words;
1367 words = MASK_LSBS(pending_words, word_idx);
1370 * If we masked out all events, wrap to beginning.
1372 if (words == 0) {
1373 word_idx = 0;
1374 bit_idx = 0;
1375 continue;
1377 word_idx = EVTCHN_FIRST_BIT(words);
1379 pending_bits = active_evtchns(cpu, s, word_idx);
1380 bit_idx = 0; /* usually scan entire word from start */
1381 if (word_idx == start_word_idx) {
1382 /* We scan the starting word in two parts */
1383 if (i == 0)
1384 /* 1st time: start in the middle */
1385 bit_idx = start_bit_idx;
1386 else
1387 /* 2nd time: mask bits done already */
1388 bit_idx &= (1UL << start_bit_idx) - 1;
1391 do {
1392 xen_ulong_t bits;
1393 int port;
1395 bits = MASK_LSBS(pending_bits, bit_idx);
1397 /* If we masked out all events, move on. */
1398 if (bits == 0)
1399 break;
1401 bit_idx = EVTCHN_FIRST_BIT(bits);
1403 /* Process port. */
1404 port = (word_idx * BITS_PER_EVTCHN_WORD) + bit_idx;
1405 irq = evtchn_to_irq[port];
1407 if (irq != -1) {
1408 desc = irq_to_desc(irq);
1409 if (desc)
1410 generic_handle_irq_desc(irq, desc);
1413 bit_idx = (bit_idx + 1) % BITS_PER_EVTCHN_WORD;
1415 /* Next caller starts at last processed + 1 */
1416 __this_cpu_write(current_word_idx,
1417 bit_idx ? word_idx :
1418 (word_idx+1) % BITS_PER_EVTCHN_WORD);
1419 __this_cpu_write(current_bit_idx, bit_idx);
1420 } while (bit_idx != 0);
1422 /* Scan start_l1i twice; all others once. */
1423 if ((word_idx != start_word_idx) || (i != 0))
1424 pending_words &= ~(1UL << word_idx);
1426 word_idx = (word_idx + 1) % BITS_PER_EVTCHN_WORD;
1429 BUG_ON(!irqs_disabled());
1431 count = __this_cpu_read(xed_nesting_count);
1432 __this_cpu_write(xed_nesting_count, 0);
1433 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1435 out:
1437 put_cpu();
1440 void xen_evtchn_do_upcall(struct pt_regs *regs)
1442 struct pt_regs *old_regs = set_irq_regs(regs);
1444 irq_enter();
1445 #ifdef CONFIG_X86
1446 exit_idle();
1447 #endif
1449 __xen_evtchn_do_upcall();
1451 irq_exit();
1452 set_irq_regs(old_regs);
1455 void xen_hvm_evtchn_do_upcall(void)
1457 __xen_evtchn_do_upcall();
1459 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1461 /* Rebind a new event channel to an existing irq. */
1462 void rebind_evtchn_irq(int evtchn, int irq)
1464 struct irq_info *info = info_for_irq(irq);
1466 if (WARN_ON(!info))
1467 return;
1469 /* Make sure the irq is masked, since the new event channel
1470 will also be masked. */
1471 disable_irq(irq);
1473 mutex_lock(&irq_mapping_update_lock);
1475 /* After resume the irq<->evtchn mappings are all cleared out */
1476 BUG_ON(evtchn_to_irq[evtchn] != -1);
1477 /* Expect irq to have been bound before,
1478 so there should be a proper type */
1479 BUG_ON(info->type == IRQT_UNBOUND);
1481 xen_irq_info_evtchn_init(irq, evtchn);
1483 mutex_unlock(&irq_mapping_update_lock);
1485 /* new event channels are always bound to cpu 0 */
1486 irq_set_affinity(irq, cpumask_of(0));
1488 /* Unmask the event channel. */
1489 enable_irq(irq);
1492 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1493 static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
1495 struct evtchn_bind_vcpu bind_vcpu;
1496 int evtchn = evtchn_from_irq(irq);
1498 if (!VALID_EVTCHN(evtchn))
1499 return -1;
1502 * Events delivered via platform PCI interrupts are always
1503 * routed to vcpu 0 and hence cannot be rebound.
1505 if (xen_hvm_domain() && !xen_have_vector_callback)
1506 return -1;
1508 /* Send future instances of this interrupt to other vcpu. */
1509 bind_vcpu.port = evtchn;
1510 bind_vcpu.vcpu = tcpu;
1513 * If this fails, it usually just indicates that we're dealing with a
1514 * virq or IPI channel, which don't actually need to be rebound. Ignore
1515 * it, but don't do the xenlinux-level rebind in that case.
1517 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1518 bind_evtchn_to_cpu(evtchn, tcpu);
1520 return 0;
1523 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1524 bool force)
1526 unsigned tcpu = cpumask_first(dest);
1528 return rebind_irq_to_cpu(data->irq, tcpu);
1531 int resend_irq_on_evtchn(unsigned int irq)
1533 int masked, evtchn = evtchn_from_irq(irq);
1534 struct shared_info *s = HYPERVISOR_shared_info;
1536 if (!VALID_EVTCHN(evtchn))
1537 return 1;
1539 masked = sync_test_and_set_bit(evtchn, BM(s->evtchn_mask));
1540 sync_set_bit(evtchn, BM(s->evtchn_pending));
1541 if (!masked)
1542 unmask_evtchn(evtchn);
1544 return 1;
1547 static void enable_dynirq(struct irq_data *data)
1549 int evtchn = evtchn_from_irq(data->irq);
1551 if (VALID_EVTCHN(evtchn))
1552 unmask_evtchn(evtchn);
1555 static void disable_dynirq(struct irq_data *data)
1557 int evtchn = evtchn_from_irq(data->irq);
1559 if (VALID_EVTCHN(evtchn))
1560 mask_evtchn(evtchn);
1563 static void ack_dynirq(struct irq_data *data)
1565 int evtchn = evtchn_from_irq(data->irq);
1567 irq_move_irq(data);
1569 if (VALID_EVTCHN(evtchn))
1570 clear_evtchn(evtchn);
1573 static void mask_ack_dynirq(struct irq_data *data)
1575 disable_dynirq(data);
1576 ack_dynirq(data);
1579 static int retrigger_dynirq(struct irq_data *data)
1581 int evtchn = evtchn_from_irq(data->irq);
1582 struct shared_info *sh = HYPERVISOR_shared_info;
1583 int ret = 0;
1585 if (VALID_EVTCHN(evtchn)) {
1586 int masked;
1588 masked = sync_test_and_set_bit(evtchn, BM(sh->evtchn_mask));
1589 sync_set_bit(evtchn, BM(sh->evtchn_pending));
1590 if (!masked)
1591 unmask_evtchn(evtchn);
1592 ret = 1;
1595 return ret;
1598 static void restore_pirqs(void)
1600 int pirq, rc, irq, gsi;
1601 struct physdev_map_pirq map_irq;
1602 struct irq_info *info;
1604 list_for_each_entry(info, &xen_irq_list_head, list) {
1605 if (info->type != IRQT_PIRQ)
1606 continue;
1608 pirq = info->u.pirq.pirq;
1609 gsi = info->u.pirq.gsi;
1610 irq = info->irq;
1612 /* save/restore of PT devices doesn't work, so at this point the
1613 * only devices present are GSI based emulated devices */
1614 if (!gsi)
1615 continue;
1617 map_irq.domid = DOMID_SELF;
1618 map_irq.type = MAP_PIRQ_TYPE_GSI;
1619 map_irq.index = gsi;
1620 map_irq.pirq = pirq;
1622 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1623 if (rc) {
1624 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1625 gsi, irq, pirq, rc);
1626 xen_free_irq(irq);
1627 continue;
1630 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1632 __startup_pirq(irq);
1636 static void restore_cpu_virqs(unsigned int cpu)
1638 struct evtchn_bind_virq bind_virq;
1639 int virq, irq, evtchn;
1641 for (virq = 0; virq < NR_VIRQS; virq++) {
1642 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1643 continue;
1645 BUG_ON(virq_from_irq(irq) != virq);
1647 /* Get a new binding from Xen. */
1648 bind_virq.virq = virq;
1649 bind_virq.vcpu = cpu;
1650 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1651 &bind_virq) != 0)
1652 BUG();
1653 evtchn = bind_virq.port;
1655 /* Record the new mapping. */
1656 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
1657 bind_evtchn_to_cpu(evtchn, cpu);
1661 static void restore_cpu_ipis(unsigned int cpu)
1663 struct evtchn_bind_ipi bind_ipi;
1664 int ipi, irq, evtchn;
1666 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1667 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1668 continue;
1670 BUG_ON(ipi_from_irq(irq) != ipi);
1672 /* Get a new binding from Xen. */
1673 bind_ipi.vcpu = cpu;
1674 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1675 &bind_ipi) != 0)
1676 BUG();
1677 evtchn = bind_ipi.port;
1679 /* Record the new mapping. */
1680 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
1681 bind_evtchn_to_cpu(evtchn, cpu);
1685 /* Clear an irq's pending state, in preparation for polling on it */
1686 void xen_clear_irq_pending(int irq)
1688 int evtchn = evtchn_from_irq(irq);
1690 if (VALID_EVTCHN(evtchn))
1691 clear_evtchn(evtchn);
1693 EXPORT_SYMBOL(xen_clear_irq_pending);
1694 void xen_set_irq_pending(int irq)
1696 int evtchn = evtchn_from_irq(irq);
1698 if (VALID_EVTCHN(evtchn))
1699 set_evtchn(evtchn);
1702 bool xen_test_irq_pending(int irq)
1704 int evtchn = evtchn_from_irq(irq);
1705 bool ret = false;
1707 if (VALID_EVTCHN(evtchn))
1708 ret = test_evtchn(evtchn);
1710 return ret;
1713 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1714 * the irq will be disabled so it won't deliver an interrupt. */
1715 void xen_poll_irq_timeout(int irq, u64 timeout)
1717 evtchn_port_t evtchn = evtchn_from_irq(irq);
1719 if (VALID_EVTCHN(evtchn)) {
1720 struct sched_poll poll;
1722 poll.nr_ports = 1;
1723 poll.timeout = timeout;
1724 set_xen_guest_handle(poll.ports, &evtchn);
1726 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1727 BUG();
1730 EXPORT_SYMBOL(xen_poll_irq_timeout);
1731 /* Poll waiting for an irq to become pending. In the usual case, the
1732 * irq will be disabled so it won't deliver an interrupt. */
1733 void xen_poll_irq(int irq)
1735 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1738 /* Check whether the IRQ line is shared with other guests. */
1739 int xen_test_irq_shared(int irq)
1741 struct irq_info *info = info_for_irq(irq);
1742 struct physdev_irq_status_query irq_status;
1744 if (WARN_ON(!info))
1745 return -ENOENT;
1747 irq_status.irq = info->u.pirq.pirq;
1749 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1750 return 0;
1751 return !(irq_status.flags & XENIRQSTAT_shared);
1753 EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1755 void xen_irq_resume(void)
1757 unsigned int cpu, evtchn;
1758 struct irq_info *info;
1760 init_evtchn_cpu_bindings();
1762 /* New event-channel space is not 'live' yet. */
1763 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1764 mask_evtchn(evtchn);
1766 /* No IRQ <-> event-channel mappings. */
1767 list_for_each_entry(info, &xen_irq_list_head, list)
1768 info->evtchn = 0; /* zap event-channel binding */
1770 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1771 evtchn_to_irq[evtchn] = -1;
1773 for_each_possible_cpu(cpu) {
1774 restore_cpu_virqs(cpu);
1775 restore_cpu_ipis(cpu);
1778 restore_pirqs();
1781 static struct irq_chip xen_dynamic_chip __read_mostly = {
1782 .name = "xen-dyn",
1784 .irq_disable = disable_dynirq,
1785 .irq_mask = disable_dynirq,
1786 .irq_unmask = enable_dynirq,
1788 .irq_ack = ack_dynirq,
1789 .irq_mask_ack = mask_ack_dynirq,
1791 .irq_set_affinity = set_affinity_irq,
1792 .irq_retrigger = retrigger_dynirq,
1795 static struct irq_chip xen_pirq_chip __read_mostly = {
1796 .name = "xen-pirq",
1798 .irq_startup = startup_pirq,
1799 .irq_shutdown = shutdown_pirq,
1800 .irq_enable = enable_pirq,
1801 .irq_disable = disable_pirq,
1803 .irq_mask = disable_dynirq,
1804 .irq_unmask = enable_dynirq,
1806 .irq_ack = eoi_pirq,
1807 .irq_eoi = eoi_pirq,
1808 .irq_mask_ack = mask_ack_pirq,
1810 .irq_set_affinity = set_affinity_irq,
1812 .irq_retrigger = retrigger_dynirq,
1815 static struct irq_chip xen_percpu_chip __read_mostly = {
1816 .name = "xen-percpu",
1818 .irq_disable = disable_dynirq,
1819 .irq_mask = disable_dynirq,
1820 .irq_unmask = enable_dynirq,
1822 .irq_ack = ack_dynirq,
1825 int xen_set_callback_via(uint64_t via)
1827 struct xen_hvm_param a;
1828 a.domid = DOMID_SELF;
1829 a.index = HVM_PARAM_CALLBACK_IRQ;
1830 a.value = via;
1831 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1833 EXPORT_SYMBOL_GPL(xen_set_callback_via);
1835 #ifdef CONFIG_XEN_PVHVM
1836 /* Vector callbacks are better than PCI interrupts to receive event
1837 * channel notifications because we can receive vector callbacks on any
1838 * vcpu and we don't need PCI support or APIC interactions. */
1839 void xen_callback_vector(void)
1841 int rc;
1842 uint64_t callback_via;
1843 if (xen_have_vector_callback) {
1844 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
1845 rc = xen_set_callback_via(callback_via);
1846 if (rc) {
1847 printk(KERN_ERR "Request for Xen HVM callback vector"
1848 " failed.\n");
1849 xen_have_vector_callback = 0;
1850 return;
1852 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1853 "enabled\n");
1854 /* in the restore case the vector has already been allocated */
1855 if (!test_bit(HYPERVISOR_CALLBACK_VECTOR, used_vectors))
1856 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
1857 xen_hvm_callback_vector);
1860 #else
1861 void xen_callback_vector(void) {}
1862 #endif
1864 void __init xen_init_IRQ(void)
1866 int i;
1868 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1869 GFP_KERNEL);
1870 BUG_ON(!evtchn_to_irq);
1871 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1872 evtchn_to_irq[i] = -1;
1874 init_evtchn_cpu_bindings();
1876 /* No event channels are 'live' right now. */
1877 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1878 mask_evtchn(i);
1880 pirq_needs_eoi = pirq_needs_eoi_flag;
1882 #ifdef CONFIG_X86
1883 if (xen_hvm_domain()) {
1884 xen_callback_vector();
1885 native_init_IRQ();
1886 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1887 * __acpi_register_gsi can point at the right function */
1888 pci_xen_hvm_init();
1889 } else {
1890 int rc;
1891 struct physdev_pirq_eoi_gmfn eoi_gmfn;
1893 irq_ctx_init(smp_processor_id());
1894 if (xen_initial_domain())
1895 pci_xen_initial_domain();
1897 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
1898 eoi_gmfn.gmfn = virt_to_mfn(pirq_eoi_map);
1899 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
1900 if (rc != 0) {
1901 free_page((unsigned long) pirq_eoi_map);
1902 pirq_eoi_map = NULL;
1903 } else
1904 pirq_needs_eoi = pirq_check_eoi_map;
1906 #endif