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
14 * 1. Inter-domain notifications. This includes all the virtual
15 * device events, since they're driven by front-ends in another domain
17 * 2. VIRQs, typically used for timers. These are per-cpu events.
19 * 4. PIRQs - Hardware interrupts.
21 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
24 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
26 #include <linux/linkage.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29 #include <linux/moduleparam.h>
30 #include <linux/string.h>
31 #include <linux/bootmem.h>
32 #include <linux/slab.h>
33 #include <linux/irqnr.h>
34 #include <linux/pci.h>
35 #include <linux/spinlock.h>
36 #include <linux/cpuhotplug.h>
37 #include <linux/atomic.h>
38 #include <linux/ktime.h>
42 #include <asm/ptrace.h>
44 #include <asm/io_apic.h>
45 #include <asm/i8259.h>
46 #include <asm/xen/pci.h>
48 #include <asm/sync_bitops.h>
49 #include <asm/xen/hypercall.h>
50 #include <asm/xen/hypervisor.h>
55 #include <xen/xen-ops.h>
56 #include <xen/events.h>
57 #include <xen/interface/xen.h>
58 #include <xen/interface/event_channel.h>
59 #include <xen/interface/hvm/hvm_op.h>
60 #include <xen/interface/hvm/params.h>
61 #include <xen/interface/physdev.h>
62 #include <xen/interface/sched.h>
63 #include <xen/interface/vcpu.h>
64 #include <asm/hw_irq.h>
66 #include "events_internal.h"
68 #undef MODULE_PARAM_PREFIX
69 #define MODULE_PARAM_PREFIX "xen."
71 static uint __read_mostly event_loop_timeout = 2;
72 module_param(event_loop_timeout, uint, 0644);
74 static uint __read_mostly event_eoi_delay = 10;
75 module_param(event_eoi_delay, uint, 0644);
77 const struct evtchn_ops *evtchn_ops;
80 * This lock protects updates to the following mapping and reference-count
81 * arrays. The lock does not need to be acquired to read the mapping tables.
83 static DEFINE_MUTEX(irq_mapping_update_lock);
88 * irq_mapping_update_lock
90 * percpu eoi_list_lock
94 static LIST_HEAD(xen_irq_list_head);
96 /* IRQ <-> VIRQ mapping. */
97 static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
99 /* IRQ <-> IPI mapping */
100 static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
104 static unsigned long *pirq_eoi_map;
106 static bool (*pirq_needs_eoi)(unsigned irq);
108 #define EVTCHN_ROW(e) (e / (PAGE_SIZE/sizeof(**evtchn_to_irq)))
109 #define EVTCHN_COL(e) (e % (PAGE_SIZE/sizeof(**evtchn_to_irq)))
110 #define EVTCHN_PER_ROW (PAGE_SIZE / sizeof(**evtchn_to_irq))
112 /* Xen will never allocate port zero for any purpose. */
113 #define VALID_EVTCHN(chn) ((chn) != 0)
115 static struct irq_info *legacy_info_ptrs[NR_IRQS_LEGACY];
117 static struct irq_chip xen_dynamic_chip;
118 static struct irq_chip xen_lateeoi_chip;
119 static struct irq_chip xen_percpu_chip;
120 static struct irq_chip xen_pirq_chip;
121 static void enable_dynirq(struct irq_data *data);
122 static void disable_dynirq(struct irq_data *data);
124 static DEFINE_PER_CPU(unsigned int, irq_epoch);
126 static void clear_evtchn_to_irq_row(int *evtchn_row)
130 for (col = 0; col < EVTCHN_PER_ROW; col++)
131 WRITE_ONCE(evtchn_row[col], -1);
134 static void clear_evtchn_to_irq_all(void)
138 for (row = 0; row < EVTCHN_ROW(xen_evtchn_max_channels()); row++) {
139 if (evtchn_to_irq[row] == NULL)
141 clear_evtchn_to_irq_row(evtchn_to_irq[row]);
145 static int set_evtchn_to_irq(unsigned evtchn, unsigned irq)
151 if (evtchn >= xen_evtchn_max_channels())
154 row = EVTCHN_ROW(evtchn);
155 col = EVTCHN_COL(evtchn);
157 if (evtchn_to_irq[row] == NULL) {
158 /* Unallocated irq entries return -1 anyway */
162 evtchn_row = (int *) __get_free_pages(GFP_KERNEL, 0);
163 if (evtchn_row == NULL)
166 clear_evtchn_to_irq_row(evtchn_row);
169 * We've prepared an empty row for the mapping. If a different
170 * thread was faster inserting it, we can drop ours.
172 if (cmpxchg(&evtchn_to_irq[row], NULL, evtchn_row) != NULL)
173 free_page((unsigned long) evtchn_row);
176 WRITE_ONCE(evtchn_to_irq[row][col], irq);
180 int get_evtchn_to_irq(unsigned evtchn)
182 if (evtchn >= xen_evtchn_max_channels())
184 if (evtchn_to_irq[EVTCHN_ROW(evtchn)] == NULL)
186 return READ_ONCE(evtchn_to_irq[EVTCHN_ROW(evtchn)][EVTCHN_COL(evtchn)]);
189 /* Get info for IRQ */
190 struct irq_info *info_for_irq(unsigned irq)
192 if (irq < nr_legacy_irqs())
193 return legacy_info_ptrs[irq];
195 return irq_get_chip_data(irq);
198 static void set_info_for_irq(unsigned int irq, struct irq_info *info)
200 if (irq < nr_legacy_irqs())
201 legacy_info_ptrs[irq] = info;
203 irq_set_chip_data(irq, info);
206 static void delayed_free_irq(struct work_struct *work)
208 struct irq_info *info = container_of(work, struct irq_info,
210 unsigned int irq = info->irq;
212 /* Remove the info pointer only now, with no potential users left. */
213 set_info_for_irq(irq, NULL);
217 /* Legacy IRQ descriptors are managed by the arch. */
218 if (irq >= nr_legacy_irqs())
222 static void rcu_free_irq(struct rcu_head *rcu_head)
224 struct irq_info *info = container_of(rcu_head, struct irq_info, rcu);
226 queue_work(system_wq, &info->work);
229 /* Constructors for packed IRQ information. */
230 static int xen_irq_info_common_setup(struct irq_info *info,
232 enum xen_irq_type type,
238 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
242 info->evtchn = evtchn;
244 info->mask_reason = EVT_MASK_REASON_EXPLICIT;
245 raw_spin_lock_init(&info->lock);
247 ret = set_evtchn_to_irq(evtchn, irq);
251 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
253 return xen_evtchn_port_setup(info);
256 static int xen_irq_info_evtchn_setup(unsigned irq,
259 struct irq_info *info = info_for_irq(irq);
261 return xen_irq_info_common_setup(info, irq, IRQT_EVTCHN, evtchn, 0);
264 static int xen_irq_info_ipi_setup(unsigned cpu,
269 struct irq_info *info = info_for_irq(irq);
273 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
275 return xen_irq_info_common_setup(info, irq, IRQT_IPI, evtchn, 0);
278 static int xen_irq_info_virq_setup(unsigned cpu,
283 struct irq_info *info = info_for_irq(irq);
287 per_cpu(virq_to_irq, cpu)[virq] = irq;
289 return xen_irq_info_common_setup(info, irq, IRQT_VIRQ, evtchn, 0);
292 static int xen_irq_info_pirq_setup(unsigned irq,
299 struct irq_info *info = info_for_irq(irq);
301 info->u.pirq.pirq = pirq;
302 info->u.pirq.gsi = gsi;
303 info->u.pirq.domid = domid;
304 info->u.pirq.flags = flags;
306 return xen_irq_info_common_setup(info, irq, IRQT_PIRQ, evtchn, 0);
309 static void xen_irq_info_cleanup(struct irq_info *info)
311 set_evtchn_to_irq(info->evtchn, -1);
312 xen_evtchn_port_remove(info->evtchn, info->cpu);
317 * Accessors for packed IRQ information.
319 unsigned int evtchn_from_irq(unsigned irq)
321 const struct irq_info *info = NULL;
323 if (likely(irq < nr_irqs))
324 info = info_for_irq(irq);
331 unsigned irq_from_evtchn(unsigned int evtchn)
333 return get_evtchn_to_irq(evtchn);
335 EXPORT_SYMBOL_GPL(irq_from_evtchn);
337 int irq_from_virq(unsigned int cpu, unsigned int virq)
339 return per_cpu(virq_to_irq, cpu)[virq];
342 static enum ipi_vector ipi_from_irq(unsigned irq)
344 struct irq_info *info = info_for_irq(irq);
346 BUG_ON(info == NULL);
347 BUG_ON(info->type != IRQT_IPI);
352 static unsigned virq_from_irq(unsigned irq)
354 struct irq_info *info = info_for_irq(irq);
356 BUG_ON(info == NULL);
357 BUG_ON(info->type != IRQT_VIRQ);
362 static unsigned pirq_from_irq(unsigned irq)
364 struct irq_info *info = info_for_irq(irq);
366 BUG_ON(info == NULL);
367 BUG_ON(info->type != IRQT_PIRQ);
369 return info->u.pirq.pirq;
372 static enum xen_irq_type type_from_irq(unsigned irq)
374 return info_for_irq(irq)->type;
377 unsigned cpu_from_irq(unsigned irq)
379 return info_for_irq(irq)->cpu;
382 unsigned int cpu_from_evtchn(unsigned int evtchn)
384 int irq = get_evtchn_to_irq(evtchn);
388 ret = cpu_from_irq(irq);
393 static void do_mask(struct irq_info *info, u8 reason)
397 raw_spin_lock_irqsave(&info->lock, flags);
399 if (!info->mask_reason)
400 mask_evtchn(info->evtchn);
402 info->mask_reason |= reason;
404 raw_spin_unlock_irqrestore(&info->lock, flags);
407 static void do_unmask(struct irq_info *info, u8 reason)
411 raw_spin_lock_irqsave(&info->lock, flags);
413 info->mask_reason &= ~reason;
415 if (!info->mask_reason)
416 unmask_evtchn(info->evtchn);
418 raw_spin_unlock_irqrestore(&info->lock, flags);
422 static bool pirq_check_eoi_map(unsigned irq)
424 return test_bit(pirq_from_irq(irq), pirq_eoi_map);
428 static bool pirq_needs_eoi_flag(unsigned irq)
430 struct irq_info *info = info_for_irq(irq);
431 BUG_ON(info->type != IRQT_PIRQ);
433 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
436 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
438 int irq = get_evtchn_to_irq(chn);
439 struct irq_info *info = info_for_irq(irq);
443 cpumask_copy(irq_get_affinity_mask(irq), cpumask_of(cpu));
445 xen_evtchn_port_bind_to_cpu(info, cpu);
451 * notify_remote_via_irq - send event to remote end of event channel via irq
452 * @irq: irq of event channel to send event to
454 * Unlike notify_remote_via_evtchn(), this is safe to use across
455 * save/restore. Notifications on a broken connection are silently
458 void notify_remote_via_irq(int irq)
460 int evtchn = evtchn_from_irq(irq);
462 if (VALID_EVTCHN(evtchn))
463 notify_remote_via_evtchn(evtchn);
465 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
467 struct lateeoi_work {
468 struct delayed_work delayed;
469 spinlock_t eoi_list_lock;
470 struct list_head eoi_list;
473 static DEFINE_PER_CPU(struct lateeoi_work, lateeoi);
475 static void lateeoi_list_del(struct irq_info *info)
477 struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
480 spin_lock_irqsave(&eoi->eoi_list_lock, flags);
481 list_del_init(&info->eoi_list);
482 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
485 static void lateeoi_list_add(struct irq_info *info)
487 struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
488 struct irq_info *elem;
489 u64 now = get_jiffies_64();
493 if (now < info->eoi_time)
494 delay = info->eoi_time - now;
498 spin_lock_irqsave(&eoi->eoi_list_lock, flags);
500 if (list_empty(&eoi->eoi_list)) {
501 list_add(&info->eoi_list, &eoi->eoi_list);
502 mod_delayed_work_on(info->eoi_cpu, system_wq,
503 &eoi->delayed, delay);
505 list_for_each_entry_reverse(elem, &eoi->eoi_list, eoi_list) {
506 if (elem->eoi_time <= info->eoi_time)
509 list_add(&info->eoi_list, &elem->eoi_list);
512 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
515 static void xen_irq_lateeoi_locked(struct irq_info *info, bool spurious)
517 evtchn_port_t evtchn;
519 unsigned int delay = 0;
521 evtchn = info->evtchn;
522 if (!VALID_EVTCHN(evtchn) || !list_empty(&info->eoi_list))
526 if ((1 << info->spurious_cnt) < (HZ << 2))
527 info->spurious_cnt++;
528 if (info->spurious_cnt > 1) {
529 delay = 1 << (info->spurious_cnt - 2);
533 info->eoi_cpu = smp_processor_id();
534 info->eoi_time = get_jiffies_64() + delay;
537 info->spurious_cnt = 0;
541 if (info->eoi_time &&
542 (info->irq_epoch == per_cpu(irq_epoch, cpu) || delay)) {
543 lateeoi_list_add(info);
549 /* is_active hasn't been reset yet, do it now. */
550 smp_store_release(&info->is_active, 0);
551 do_unmask(info, EVT_MASK_REASON_EOI_PENDING);
554 static void xen_irq_lateeoi_worker(struct work_struct *work)
556 struct lateeoi_work *eoi;
557 struct irq_info *info;
558 u64 now = get_jiffies_64();
561 eoi = container_of(to_delayed_work(work), struct lateeoi_work, delayed);
566 spin_lock_irqsave(&eoi->eoi_list_lock, flags);
568 info = list_first_entry_or_null(&eoi->eoi_list, struct irq_info,
574 if (now < info->eoi_time) {
575 mod_delayed_work_on(info->eoi_cpu, system_wq,
577 info->eoi_time - now);
581 list_del_init(&info->eoi_list);
583 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
587 xen_irq_lateeoi_locked(info, false);
590 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
595 static void xen_cpu_init_eoi(unsigned int cpu)
597 struct lateeoi_work *eoi = &per_cpu(lateeoi, cpu);
599 INIT_DELAYED_WORK(&eoi->delayed, xen_irq_lateeoi_worker);
600 spin_lock_init(&eoi->eoi_list_lock);
601 INIT_LIST_HEAD(&eoi->eoi_list);
604 void xen_irq_lateeoi(unsigned int irq, unsigned int eoi_flags)
606 struct irq_info *info;
610 info = info_for_irq(irq);
613 xen_irq_lateeoi_locked(info, eoi_flags & XEN_EOI_FLAG_SPURIOUS);
617 EXPORT_SYMBOL_GPL(xen_irq_lateeoi);
619 static void xen_irq_init(unsigned irq)
621 struct irq_info *info;
624 /* By default all event channels notify CPU#0. */
625 cpumask_copy(irq_get_affinity_mask(irq), cpumask_of(0));
628 info = kzalloc(sizeof(*info), GFP_KERNEL);
630 panic("Unable to allocate metadata for IRQ%d\n", irq);
632 info->type = IRQT_UNBOUND;
634 INIT_WORK(&info->work, delayed_free_irq);
636 set_info_for_irq(irq, info);
638 INIT_LIST_HEAD(&info->eoi_list);
639 list_add_tail(&info->list, &xen_irq_list_head);
642 static int __must_check xen_allocate_irqs_dynamic(int nvec)
644 int i, irq = irq_alloc_descs(-1, 0, nvec, -1);
647 for (i = 0; i < nvec; i++)
648 xen_irq_init(irq + i);
654 static inline int __must_check xen_allocate_irq_dynamic(void)
657 return xen_allocate_irqs_dynamic(1);
660 static int __must_check xen_allocate_irq_gsi(unsigned gsi)
665 * A PV guest has no concept of a GSI (since it has no ACPI
666 * nor access to/knowledge of the physical APICs). Therefore
667 * all IRQs are dynamically allocated from the entire IRQ
670 if (xen_pv_domain() && !xen_initial_domain())
671 return xen_allocate_irq_dynamic();
673 /* Legacy IRQ descriptors are already allocated by the arch. */
674 if (gsi < nr_legacy_irqs())
677 irq = irq_alloc_desc_at(gsi, -1);
684 static void xen_free_irq(unsigned irq)
686 struct irq_info *info = info_for_irq(irq);
691 if (!list_empty(&info->eoi_list))
692 lateeoi_list_del(info);
694 list_del(&info->list);
696 WARN_ON(info->refcnt > 0);
698 call_rcu(&info->rcu, rcu_free_irq);
701 static void xen_evtchn_close(unsigned int port)
703 struct evtchn_close close;
706 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
710 static void event_handler_exit(struct irq_info *info)
712 smp_store_release(&info->is_active, 0);
713 clear_evtchn(info->evtchn);
716 static void pirq_query_unmask(int irq)
718 struct physdev_irq_status_query irq_status;
719 struct irq_info *info = info_for_irq(irq);
721 BUG_ON(info->type != IRQT_PIRQ);
723 irq_status.irq = pirq_from_irq(irq);
724 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
725 irq_status.flags = 0;
727 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
728 if (irq_status.flags & XENIRQSTAT_needs_eoi)
729 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
732 static void eoi_pirq(struct irq_data *data)
734 struct irq_info *info = info_for_irq(data->irq);
735 int evtchn = info ? info->evtchn : 0;
736 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
739 if (!VALID_EVTCHN(evtchn))
742 if (unlikely(irqd_is_setaffinity_pending(data)) &&
743 likely(!irqd_irq_disabled(data))) {
744 do_mask(info, EVT_MASK_REASON_TEMPORARY);
746 event_handler_exit(info);
748 irq_move_masked_irq(data);
750 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
752 event_handler_exit(info);
754 if (pirq_needs_eoi(data->irq)) {
755 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
760 static void mask_ack_pirq(struct irq_data *data)
762 disable_dynirq(data);
766 static unsigned int __startup_pirq(unsigned int irq)
768 struct evtchn_bind_pirq bind_pirq;
769 struct irq_info *info = info_for_irq(irq);
770 int evtchn = evtchn_from_irq(irq);
773 BUG_ON(info->type != IRQT_PIRQ);
775 if (VALID_EVTCHN(evtchn))
778 bind_pirq.pirq = pirq_from_irq(irq);
779 /* NB. We are happy to share unless we are probing. */
780 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
781 BIND_PIRQ__WILL_SHARE : 0;
782 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
784 pr_warn("Failed to obtain physical IRQ %d\n", irq);
787 evtchn = bind_pirq.port;
789 pirq_query_unmask(irq);
791 rc = set_evtchn_to_irq(evtchn, irq);
795 info->evtchn = evtchn;
796 bind_evtchn_to_cpu(evtchn, 0);
798 rc = xen_evtchn_port_setup(info);
803 do_unmask(info, EVT_MASK_REASON_EXPLICIT);
805 eoi_pirq(irq_get_irq_data(irq));
810 pr_err("irq%d: Failed to set port to irq mapping (%d)\n", irq, rc);
811 xen_evtchn_close(evtchn);
815 static unsigned int startup_pirq(struct irq_data *data)
817 return __startup_pirq(data->irq);
820 static void shutdown_pirq(struct irq_data *data)
822 unsigned int irq = data->irq;
823 struct irq_info *info = info_for_irq(irq);
824 unsigned evtchn = evtchn_from_irq(irq);
826 BUG_ON(info->type != IRQT_PIRQ);
828 if (!VALID_EVTCHN(evtchn))
831 do_mask(info, EVT_MASK_REASON_EXPLICIT);
832 xen_evtchn_close(evtchn);
833 xen_irq_info_cleanup(info);
836 static void enable_pirq(struct irq_data *data)
841 static void disable_pirq(struct irq_data *data)
843 disable_dynirq(data);
846 int xen_irq_from_gsi(unsigned gsi)
848 struct irq_info *info;
850 list_for_each_entry(info, &xen_irq_list_head, list) {
851 if (info->type != IRQT_PIRQ)
854 if (info->u.pirq.gsi == gsi)
860 EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
862 static void __unbind_from_irq(unsigned int irq)
864 int evtchn = evtchn_from_irq(irq);
865 struct irq_info *info = info_for_irq(irq);
867 if (info->refcnt > 0) {
869 if (info->refcnt != 0)
873 if (VALID_EVTCHN(evtchn)) {
874 unsigned int cpu = cpu_from_irq(irq);
876 xen_evtchn_close(evtchn);
878 switch (type_from_irq(irq)) {
880 per_cpu(virq_to_irq, cpu)[virq_from_irq(irq)] = -1;
883 per_cpu(ipi_to_irq, cpu)[ipi_from_irq(irq)] = -1;
889 xen_irq_info_cleanup(info);
896 * Do not make any assumptions regarding the relationship between the
897 * IRQ number returned here and the Xen pirq argument.
899 * Note: We don't assign an event channel until the irq actually started
900 * up. Return an existing irq if we've already got one for the gsi.
902 * Shareable implies level triggered, not shareable implies edge
905 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
906 unsigned pirq, int shareable, char *name)
909 struct physdev_irq irq_op;
912 mutex_lock(&irq_mapping_update_lock);
914 irq = xen_irq_from_gsi(gsi);
916 pr_info("%s: returning irq %d for gsi %u\n",
921 irq = xen_allocate_irq_gsi(gsi);
928 /* Only the privileged domain can do this. For non-priv, the pcifront
929 * driver provides a PCI bus that does the call to do exactly
930 * this in the priv domain. */
931 if (xen_initial_domain() &&
932 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
938 ret = xen_irq_info_pirq_setup(irq, 0, pirq, gsi, DOMID_SELF,
939 shareable ? PIRQ_SHAREABLE : 0);
941 __unbind_from_irq(irq);
946 pirq_query_unmask(irq);
947 /* We try to use the handler with the appropriate semantic for the
948 * type of interrupt: if the interrupt is an edge triggered
949 * interrupt we use handle_edge_irq.
951 * On the other hand if the interrupt is level triggered we use
952 * handle_fasteoi_irq like the native code does for this kind of
955 * Depending on the Xen version, pirq_needs_eoi might return true
956 * not only for level triggered interrupts but for edge triggered
957 * interrupts too. In any case Xen always honors the eoi mechanism,
958 * not injecting any more pirqs of the same kind if the first one
959 * hasn't received an eoi yet. Therefore using the fasteoi handler
960 * is the right choice either way.
963 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
964 handle_fasteoi_irq, name);
966 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
967 handle_edge_irq, name);
970 mutex_unlock(&irq_mapping_update_lock);
975 #ifdef CONFIG_PCI_MSI
976 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
979 struct physdev_get_free_pirq op_get_free_pirq;
981 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
982 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
984 WARN_ONCE(rc == -ENOSYS,
985 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
987 return rc ? -1 : op_get_free_pirq.pirq;
990 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
991 int pirq, int nvec, const char *name, domid_t domid)
995 mutex_lock(&irq_mapping_update_lock);
997 irq = xen_allocate_irqs_dynamic(nvec);
1001 for (i = 0; i < nvec; i++) {
1002 irq_set_chip_and_handler_name(irq + i, &xen_pirq_chip, handle_edge_irq, name);
1004 ret = xen_irq_info_pirq_setup(irq + i, 0, pirq + i, 0, domid,
1005 i == 0 ? 0 : PIRQ_MSI_GROUP);
1010 ret = irq_set_msi_desc(irq, msidesc);
1014 mutex_unlock(&irq_mapping_update_lock);
1018 __unbind_from_irq(irq + nvec);
1019 mutex_unlock(&irq_mapping_update_lock);
1024 int xen_destroy_irq(int irq)
1026 struct physdev_unmap_pirq unmap_irq;
1027 struct irq_info *info = info_for_irq(irq);
1030 mutex_lock(&irq_mapping_update_lock);
1033 * If trying to remove a vector in a MSI group different
1034 * than the first one skip the PIRQ unmap unless this vector
1035 * is the first one in the group.
1037 if (xen_initial_domain() && !(info->u.pirq.flags & PIRQ_MSI_GROUP)) {
1038 unmap_irq.pirq = info->u.pirq.pirq;
1039 unmap_irq.domid = info->u.pirq.domid;
1040 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
1041 /* If another domain quits without making the pci_disable_msix
1042 * call, the Xen hypervisor takes care of freeing the PIRQs
1043 * (free_domain_pirqs).
1045 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
1046 pr_info("domain %d does not have %d anymore\n",
1047 info->u.pirq.domid, info->u.pirq.pirq);
1049 pr_warn("unmap irq failed %d\n", rc);
1057 mutex_unlock(&irq_mapping_update_lock);
1061 int xen_irq_from_pirq(unsigned pirq)
1065 struct irq_info *info;
1067 mutex_lock(&irq_mapping_update_lock);
1069 list_for_each_entry(info, &xen_irq_list_head, list) {
1070 if (info->type != IRQT_PIRQ)
1073 if (info->u.pirq.pirq == pirq)
1078 mutex_unlock(&irq_mapping_update_lock);
1084 int xen_pirq_from_irq(unsigned irq)
1086 return pirq_from_irq(irq);
1088 EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
1090 static int bind_evtchn_to_irq_chip(evtchn_port_t evtchn, struct irq_chip *chip)
1095 if (evtchn >= xen_evtchn_max_channels())
1098 mutex_lock(&irq_mapping_update_lock);
1100 irq = get_evtchn_to_irq(evtchn);
1103 irq = xen_allocate_irq_dynamic();
1107 irq_set_chip_and_handler_name(irq, chip,
1108 handle_edge_irq, "event");
1110 ret = xen_irq_info_evtchn_setup(irq, evtchn);
1112 __unbind_from_irq(irq);
1116 /* New interdomain events are bound to VCPU 0. */
1117 bind_evtchn_to_cpu(evtchn, 0);
1119 struct irq_info *info = info_for_irq(irq);
1120 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
1124 mutex_unlock(&irq_mapping_update_lock);
1129 int bind_evtchn_to_irq(evtchn_port_t evtchn)
1131 return bind_evtchn_to_irq_chip(evtchn, &xen_dynamic_chip);
1133 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
1135 int bind_evtchn_to_irq_lateeoi(evtchn_port_t evtchn)
1137 return bind_evtchn_to_irq_chip(evtchn, &xen_lateeoi_chip);
1139 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq_lateeoi);
1141 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
1143 struct evtchn_bind_ipi bind_ipi;
1147 mutex_lock(&irq_mapping_update_lock);
1149 irq = per_cpu(ipi_to_irq, cpu)[ipi];
1152 irq = xen_allocate_irq_dynamic();
1156 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
1157 handle_percpu_irq, "ipi");
1159 bind_ipi.vcpu = xen_vcpu_nr(cpu);
1160 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1163 evtchn = bind_ipi.port;
1165 ret = xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
1167 __unbind_from_irq(irq);
1171 bind_evtchn_to_cpu(evtchn, cpu);
1173 struct irq_info *info = info_for_irq(irq);
1174 WARN_ON(info == NULL || info->type != IRQT_IPI);
1178 mutex_unlock(&irq_mapping_update_lock);
1182 static int bind_interdomain_evtchn_to_irq_chip(unsigned int remote_domain,
1183 evtchn_port_t remote_port,
1184 struct irq_chip *chip)
1186 struct evtchn_bind_interdomain bind_interdomain;
1189 bind_interdomain.remote_dom = remote_domain;
1190 bind_interdomain.remote_port = remote_port;
1192 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
1195 return err ? : bind_evtchn_to_irq_chip(bind_interdomain.local_port,
1199 int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
1200 evtchn_port_t remote_port)
1202 return bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1205 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irq);
1207 int bind_interdomain_evtchn_to_irq_lateeoi(unsigned int remote_domain,
1208 evtchn_port_t remote_port)
1210 return bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1213 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irq_lateeoi);
1215 static int find_virq(unsigned int virq, unsigned int cpu)
1217 struct evtchn_status status;
1218 int port, rc = -ENOENT;
1220 memset(&status, 0, sizeof(status));
1221 for (port = 0; port < xen_evtchn_max_channels(); port++) {
1222 status.dom = DOMID_SELF;
1224 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
1227 if (status.status != EVTCHNSTAT_virq)
1229 if (status.u.virq == virq && status.vcpu == xen_vcpu_nr(cpu)) {
1238 * xen_evtchn_nr_channels - number of usable event channel ports
1240 * This may be less than the maximum supported by the current
1241 * hypervisor ABI. Use xen_evtchn_max_channels() for the maximum
1244 unsigned xen_evtchn_nr_channels(void)
1246 return evtchn_ops->nr_channels();
1248 EXPORT_SYMBOL_GPL(xen_evtchn_nr_channels);
1250 int bind_virq_to_irq(unsigned int virq, unsigned int cpu, bool percpu)
1252 struct evtchn_bind_virq bind_virq;
1253 int evtchn, irq, ret;
1255 mutex_lock(&irq_mapping_update_lock);
1257 irq = per_cpu(virq_to_irq, cpu)[virq];
1260 irq = xen_allocate_irq_dynamic();
1265 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
1266 handle_percpu_irq, "virq");
1268 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
1269 handle_edge_irq, "virq");
1271 bind_virq.virq = virq;
1272 bind_virq.vcpu = xen_vcpu_nr(cpu);
1273 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1276 evtchn = bind_virq.port;
1279 ret = find_virq(virq, cpu);
1284 ret = xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1286 __unbind_from_irq(irq);
1291 bind_evtchn_to_cpu(evtchn, cpu);
1293 struct irq_info *info = info_for_irq(irq);
1294 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
1298 mutex_unlock(&irq_mapping_update_lock);
1303 static void unbind_from_irq(unsigned int irq)
1305 mutex_lock(&irq_mapping_update_lock);
1306 __unbind_from_irq(irq);
1307 mutex_unlock(&irq_mapping_update_lock);
1310 static int bind_evtchn_to_irqhandler_chip(evtchn_port_t evtchn,
1311 irq_handler_t handler,
1312 unsigned long irqflags,
1313 const char *devname, void *dev_id,
1314 struct irq_chip *chip)
1318 irq = bind_evtchn_to_irq_chip(evtchn, chip);
1321 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1323 unbind_from_irq(irq);
1330 int bind_evtchn_to_irqhandler(evtchn_port_t evtchn,
1331 irq_handler_t handler,
1332 unsigned long irqflags,
1333 const char *devname, void *dev_id)
1335 return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1339 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1341 int bind_evtchn_to_irqhandler_lateeoi(evtchn_port_t evtchn,
1342 irq_handler_t handler,
1343 unsigned long irqflags,
1344 const char *devname, void *dev_id)
1346 return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1350 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler_lateeoi);
1352 static int bind_interdomain_evtchn_to_irqhandler_chip(
1353 unsigned int remote_domain, evtchn_port_t remote_port,
1354 irq_handler_t handler, unsigned long irqflags,
1355 const char *devname, void *dev_id, struct irq_chip *chip)
1359 irq = bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1364 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1366 unbind_from_irq(irq);
1373 int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1374 evtchn_port_t remote_port,
1375 irq_handler_t handler,
1376 unsigned long irqflags,
1377 const char *devname,
1380 return bind_interdomain_evtchn_to_irqhandler_chip(remote_domain,
1381 remote_port, handler, irqflags, devname,
1382 dev_id, &xen_dynamic_chip);
1384 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1386 int bind_interdomain_evtchn_to_irqhandler_lateeoi(unsigned int remote_domain,
1387 evtchn_port_t remote_port,
1388 irq_handler_t handler,
1389 unsigned long irqflags,
1390 const char *devname,
1393 return bind_interdomain_evtchn_to_irqhandler_chip(remote_domain,
1394 remote_port, handler, irqflags, devname,
1395 dev_id, &xen_lateeoi_chip);
1397 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler_lateeoi);
1399 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
1400 irq_handler_t handler,
1401 unsigned long irqflags, const char *devname, void *dev_id)
1405 irq = bind_virq_to_irq(virq, cpu, irqflags & IRQF_PERCPU);
1408 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1410 unbind_from_irq(irq);
1416 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1418 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1420 irq_handler_t handler,
1421 unsigned long irqflags,
1422 const char *devname,
1427 irq = bind_ipi_to_irq(ipi, cpu);
1431 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
1432 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1434 unbind_from_irq(irq);
1441 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1443 struct irq_info *info = info_for_irq(irq);
1447 free_irq(irq, dev_id);
1448 unbind_from_irq(irq);
1450 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1453 * xen_set_irq_priority() - set an event channel priority.
1454 * @irq:irq bound to an event channel.
1455 * @priority: priority between XEN_IRQ_PRIORITY_MAX and XEN_IRQ_PRIORITY_MIN.
1457 int xen_set_irq_priority(unsigned irq, unsigned priority)
1459 struct evtchn_set_priority set_priority;
1461 set_priority.port = evtchn_from_irq(irq);
1462 set_priority.priority = priority;
1464 return HYPERVISOR_event_channel_op(EVTCHNOP_set_priority,
1467 EXPORT_SYMBOL_GPL(xen_set_irq_priority);
1469 int evtchn_make_refcounted(unsigned int evtchn)
1471 int irq = get_evtchn_to_irq(evtchn);
1472 struct irq_info *info;
1477 info = info_for_irq(irq);
1482 WARN_ON(info->refcnt != -1);
1488 EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1490 int evtchn_get(unsigned int evtchn)
1493 struct irq_info *info;
1496 if (evtchn >= xen_evtchn_max_channels())
1499 mutex_lock(&irq_mapping_update_lock);
1501 irq = get_evtchn_to_irq(evtchn);
1505 info = info_for_irq(irq);
1511 if (info->refcnt <= 0 || info->refcnt == SHRT_MAX)
1517 mutex_unlock(&irq_mapping_update_lock);
1521 EXPORT_SYMBOL_GPL(evtchn_get);
1523 void evtchn_put(unsigned int evtchn)
1525 int irq = get_evtchn_to_irq(evtchn);
1526 if (WARN_ON(irq == -1))
1528 unbind_from_irq(irq);
1530 EXPORT_SYMBOL_GPL(evtchn_put);
1532 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1537 if (unlikely(vector == XEN_NMI_VECTOR)) {
1538 int rc = HYPERVISOR_vcpu_op(VCPUOP_send_nmi, xen_vcpu_nr(cpu),
1541 printk(KERN_WARNING "Sending nmi to CPU%d failed (rc:%d)\n", cpu, rc);
1545 irq = per_cpu(ipi_to_irq, cpu)[vector];
1547 notify_remote_via_irq(irq);
1550 struct evtchn_loop_ctrl {
1556 void handle_irq_for_port(evtchn_port_t port, struct evtchn_loop_ctrl *ctrl)
1559 struct irq_info *info;
1561 irq = get_evtchn_to_irq(port);
1566 * Check for timeout every 256 events.
1567 * We are setting the timeout value only after the first 256
1568 * events in order to not hurt the common case of few loop
1569 * iterations. The 256 is basically an arbitrary value.
1571 * In case we are hitting the timeout we need to defer all further
1572 * EOIs in order to ensure to leave the event handling loop rather
1573 * sooner than later.
1575 if (!ctrl->defer_eoi && !(++ctrl->count & 0xff)) {
1576 ktime_t kt = ktime_get();
1578 if (!ctrl->timeout) {
1579 kt = ktime_add_ms(kt,
1580 jiffies_to_msecs(event_loop_timeout));
1582 } else if (kt > ctrl->timeout) {
1583 ctrl->defer_eoi = true;
1587 info = info_for_irq(irq);
1588 if (xchg_acquire(&info->is_active, 1))
1591 if (ctrl->defer_eoi) {
1592 info->eoi_cpu = smp_processor_id();
1593 info->irq_epoch = __this_cpu_read(irq_epoch);
1594 info->eoi_time = get_jiffies_64() + event_eoi_delay;
1597 generic_handle_irq(irq);
1600 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1602 static void __xen_evtchn_do_upcall(void)
1604 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1605 int cpu = get_cpu();
1607 struct evtchn_loop_ctrl ctrl = { 0 };
1610 * When closing an event channel the associated IRQ must not be freed
1611 * until all cpus have left the event handling loop. This is ensured
1612 * by taking the rcu_read_lock() while handling events, as freeing of
1613 * the IRQ is handled via queue_rcu_work() _after_ closing the event
1619 vcpu_info->evtchn_upcall_pending = 0;
1621 if (__this_cpu_inc_return(xed_nesting_count) - 1)
1624 xen_evtchn_handle_events(cpu, &ctrl);
1626 BUG_ON(!irqs_disabled());
1628 count = __this_cpu_read(xed_nesting_count);
1629 __this_cpu_write(xed_nesting_count, 0);
1630 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1636 * Increment irq_epoch only now to defer EOIs only for
1637 * xen_irq_lateeoi() invocations occurring from inside the loop
1640 __this_cpu_inc(irq_epoch);
1645 void xen_evtchn_do_upcall(struct pt_regs *regs)
1647 struct pt_regs *old_regs = set_irq_regs(regs);
1651 inc_irq_stat(irq_hv_callback_count);
1654 __xen_evtchn_do_upcall();
1657 set_irq_regs(old_regs);
1660 void xen_hvm_evtchn_do_upcall(void)
1662 __xen_evtchn_do_upcall();
1664 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1666 /* Rebind a new event channel to an existing irq. */
1667 void rebind_evtchn_irq(int evtchn, int irq)
1669 struct irq_info *info = info_for_irq(irq);
1674 /* Make sure the irq is masked, since the new event channel
1675 will also be masked. */
1678 mutex_lock(&irq_mapping_update_lock);
1680 /* After resume the irq<->evtchn mappings are all cleared out */
1681 BUG_ON(get_evtchn_to_irq(evtchn) != -1);
1682 /* Expect irq to have been bound before,
1683 so there should be a proper type */
1684 BUG_ON(info->type == IRQT_UNBOUND);
1686 (void)xen_irq_info_evtchn_setup(irq, evtchn);
1688 mutex_unlock(&irq_mapping_update_lock);
1690 bind_evtchn_to_cpu(evtchn, info->cpu);
1691 /* This will be deferred until interrupt is processed */
1692 irq_set_affinity(irq, cpumask_of(info->cpu));
1694 /* Unmask the event channel. */
1698 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1699 static int xen_rebind_evtchn_to_cpu(struct irq_info *info, unsigned int tcpu)
1701 struct evtchn_bind_vcpu bind_vcpu;
1702 evtchn_port_t evtchn = info ? info->evtchn : 0;
1704 if (!VALID_EVTCHN(evtchn))
1707 if (!xen_support_evtchn_rebind())
1710 /* Send future instances of this interrupt to other vcpu. */
1711 bind_vcpu.port = evtchn;
1712 bind_vcpu.vcpu = xen_vcpu_nr(tcpu);
1715 * Mask the event while changing the VCPU binding to prevent
1716 * it being delivered on an unexpected VCPU.
1718 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1721 * If this fails, it usually just indicates that we're dealing with a
1722 * virq or IPI channel, which don't actually need to be rebound. Ignore
1723 * it, but don't do the xenlinux-level rebind in that case.
1725 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1726 bind_evtchn_to_cpu(evtchn, tcpu);
1728 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1733 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1736 unsigned tcpu = cpumask_first_and(dest, cpu_online_mask);
1737 int ret = xen_rebind_evtchn_to_cpu(info_for_irq(data->irq), tcpu);
1740 irq_data_update_effective_affinity(data, cpumask_of(tcpu));
1745 /* To be called with desc->lock held. */
1746 int xen_set_affinity_evtchn(struct irq_desc *desc, unsigned int tcpu)
1748 struct irq_data *d = irq_desc_get_irq_data(desc);
1750 return set_affinity_irq(d, cpumask_of(tcpu), false);
1752 EXPORT_SYMBOL_GPL(xen_set_affinity_evtchn);
1754 static void enable_dynirq(struct irq_data *data)
1756 struct irq_info *info = info_for_irq(data->irq);
1757 evtchn_port_t evtchn = info ? info->evtchn : 0;
1759 if (VALID_EVTCHN(evtchn))
1760 do_unmask(info, EVT_MASK_REASON_EXPLICIT);
1763 static void disable_dynirq(struct irq_data *data)
1765 struct irq_info *info = info_for_irq(data->irq);
1766 evtchn_port_t evtchn = info ? info->evtchn : 0;
1768 if (VALID_EVTCHN(evtchn))
1769 do_mask(info, EVT_MASK_REASON_EXPLICIT);
1772 static void ack_dynirq(struct irq_data *data)
1774 struct irq_info *info = info_for_irq(data->irq);
1775 evtchn_port_t evtchn = info ? info->evtchn : 0;
1777 if (!VALID_EVTCHN(evtchn))
1780 if (unlikely(irqd_is_setaffinity_pending(data)) &&
1781 likely(!irqd_irq_disabled(data))) {
1782 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1784 event_handler_exit(info);
1786 irq_move_masked_irq(data);
1788 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1790 event_handler_exit(info);
1793 static void mask_ack_dynirq(struct irq_data *data)
1795 disable_dynirq(data);
1799 static void lateeoi_ack_dynirq(struct irq_data *data)
1801 struct irq_info *info = info_for_irq(data->irq);
1802 evtchn_port_t evtchn = info ? info->evtchn : 0;
1804 if (!VALID_EVTCHN(evtchn))
1807 do_mask(info, EVT_MASK_REASON_EOI_PENDING);
1809 if (unlikely(irqd_is_setaffinity_pending(data)) &&
1810 likely(!irqd_irq_disabled(data))) {
1811 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1813 clear_evtchn(evtchn);
1815 irq_move_masked_irq(data);
1817 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1819 clear_evtchn(evtchn);
1822 static void lateeoi_mask_ack_dynirq(struct irq_data *data)
1824 struct irq_info *info = info_for_irq(data->irq);
1825 evtchn_port_t evtchn = info ? info->evtchn : 0;
1827 if (VALID_EVTCHN(evtchn)) {
1828 do_mask(info, EVT_MASK_REASON_EXPLICIT);
1833 static int retrigger_dynirq(struct irq_data *data)
1835 struct irq_info *info = info_for_irq(data->irq);
1836 evtchn_port_t evtchn = info ? info->evtchn : 0;
1838 if (!VALID_EVTCHN(evtchn))
1841 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1843 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1848 static void restore_pirqs(void)
1850 int pirq, rc, irq, gsi;
1851 struct physdev_map_pirq map_irq;
1852 struct irq_info *info;
1854 list_for_each_entry(info, &xen_irq_list_head, list) {
1855 if (info->type != IRQT_PIRQ)
1858 pirq = info->u.pirq.pirq;
1859 gsi = info->u.pirq.gsi;
1862 /* save/restore of PT devices doesn't work, so at this point the
1863 * only devices present are GSI based emulated devices */
1867 map_irq.domid = DOMID_SELF;
1868 map_irq.type = MAP_PIRQ_TYPE_GSI;
1869 map_irq.index = gsi;
1870 map_irq.pirq = pirq;
1872 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1874 pr_warn("xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1875 gsi, irq, pirq, rc);
1880 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1882 __startup_pirq(irq);
1886 static void restore_cpu_virqs(unsigned int cpu)
1888 struct evtchn_bind_virq bind_virq;
1889 int virq, irq, evtchn;
1891 for (virq = 0; virq < NR_VIRQS; virq++) {
1892 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1895 BUG_ON(virq_from_irq(irq) != virq);
1897 /* Get a new binding from Xen. */
1898 bind_virq.virq = virq;
1899 bind_virq.vcpu = xen_vcpu_nr(cpu);
1900 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1903 evtchn = bind_virq.port;
1905 /* Record the new mapping. */
1906 (void)xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1907 bind_evtchn_to_cpu(evtchn, cpu);
1911 static void restore_cpu_ipis(unsigned int cpu)
1913 struct evtchn_bind_ipi bind_ipi;
1914 int ipi, irq, evtchn;
1916 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1917 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1920 BUG_ON(ipi_from_irq(irq) != ipi);
1922 /* Get a new binding from Xen. */
1923 bind_ipi.vcpu = xen_vcpu_nr(cpu);
1924 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1927 evtchn = bind_ipi.port;
1929 /* Record the new mapping. */
1930 (void)xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
1931 bind_evtchn_to_cpu(evtchn, cpu);
1935 /* Clear an irq's pending state, in preparation for polling on it */
1936 void xen_clear_irq_pending(int irq)
1938 struct irq_info *info = info_for_irq(irq);
1939 evtchn_port_t evtchn = info ? info->evtchn : 0;
1941 if (VALID_EVTCHN(evtchn))
1942 event_handler_exit(info);
1944 EXPORT_SYMBOL(xen_clear_irq_pending);
1945 void xen_set_irq_pending(int irq)
1947 int evtchn = evtchn_from_irq(irq);
1949 if (VALID_EVTCHN(evtchn))
1953 bool xen_test_irq_pending(int irq)
1955 int evtchn = evtchn_from_irq(irq);
1958 if (VALID_EVTCHN(evtchn))
1959 ret = test_evtchn(evtchn);
1964 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1965 * the irq will be disabled so it won't deliver an interrupt. */
1966 void xen_poll_irq_timeout(int irq, u64 timeout)
1968 evtchn_port_t evtchn = evtchn_from_irq(irq);
1970 if (VALID_EVTCHN(evtchn)) {
1971 struct sched_poll poll;
1974 poll.timeout = timeout;
1975 set_xen_guest_handle(poll.ports, &evtchn);
1977 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1981 EXPORT_SYMBOL(xen_poll_irq_timeout);
1982 /* Poll waiting for an irq to become pending. In the usual case, the
1983 * irq will be disabled so it won't deliver an interrupt. */
1984 void xen_poll_irq(int irq)
1986 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1989 /* Check whether the IRQ line is shared with other guests. */
1990 int xen_test_irq_shared(int irq)
1992 struct irq_info *info = info_for_irq(irq);
1993 struct physdev_irq_status_query irq_status;
1998 irq_status.irq = info->u.pirq.pirq;
2000 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
2002 return !(irq_status.flags & XENIRQSTAT_shared);
2004 EXPORT_SYMBOL_GPL(xen_test_irq_shared);
2006 void xen_irq_resume(void)
2009 struct irq_info *info;
2011 /* New event-channel space is not 'live' yet. */
2012 xen_evtchn_resume();
2014 /* No IRQ <-> event-channel mappings. */
2015 list_for_each_entry(info, &xen_irq_list_head, list)
2016 info->evtchn = 0; /* zap event-channel binding */
2018 clear_evtchn_to_irq_all();
2020 for_each_possible_cpu(cpu) {
2021 restore_cpu_virqs(cpu);
2022 restore_cpu_ipis(cpu);
2028 static struct irq_chip xen_dynamic_chip __read_mostly = {
2031 .irq_disable = disable_dynirq,
2032 .irq_mask = disable_dynirq,
2033 .irq_unmask = enable_dynirq,
2035 .irq_ack = ack_dynirq,
2036 .irq_mask_ack = mask_ack_dynirq,
2038 .irq_set_affinity = set_affinity_irq,
2039 .irq_retrigger = retrigger_dynirq,
2042 static struct irq_chip xen_lateeoi_chip __read_mostly = {
2043 /* The chip name needs to contain "xen-dyn" for irqbalance to work. */
2044 .name = "xen-dyn-lateeoi",
2046 .irq_disable = disable_dynirq,
2047 .irq_mask = disable_dynirq,
2048 .irq_unmask = enable_dynirq,
2050 .irq_ack = lateeoi_ack_dynirq,
2051 .irq_mask_ack = lateeoi_mask_ack_dynirq,
2053 .irq_set_affinity = set_affinity_irq,
2054 .irq_retrigger = retrigger_dynirq,
2057 static struct irq_chip xen_pirq_chip __read_mostly = {
2060 .irq_startup = startup_pirq,
2061 .irq_shutdown = shutdown_pirq,
2062 .irq_enable = enable_pirq,
2063 .irq_disable = disable_pirq,
2065 .irq_mask = disable_dynirq,
2066 .irq_unmask = enable_dynirq,
2068 .irq_ack = eoi_pirq,
2069 .irq_eoi = eoi_pirq,
2070 .irq_mask_ack = mask_ack_pirq,
2072 .irq_set_affinity = set_affinity_irq,
2074 .irq_retrigger = retrigger_dynirq,
2077 static struct irq_chip xen_percpu_chip __read_mostly = {
2078 .name = "xen-percpu",
2080 .irq_disable = disable_dynirq,
2081 .irq_mask = disable_dynirq,
2082 .irq_unmask = enable_dynirq,
2084 .irq_ack = ack_dynirq,
2087 #ifdef CONFIG_XEN_PVHVM
2088 /* Vector callbacks are better than PCI interrupts to receive event
2089 * channel notifications because we can receive vector callbacks on any
2090 * vcpu and we don't need PCI support or APIC interactions. */
2091 void xen_callback_vector(void)
2094 uint64_t callback_via;
2096 if (xen_have_vector_callback) {
2097 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
2098 rc = xen_set_callback_via(callback_via);
2100 pr_err("Request for Xen HVM callback vector failed\n");
2101 xen_have_vector_callback = 0;
2104 pr_info_once("Xen HVM callback vector for event delivery is enabled\n");
2105 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
2106 xen_hvm_callback_vector);
2110 void xen_callback_vector(void) {}
2113 static bool fifo_events = true;
2114 module_param(fifo_events, bool, 0);
2116 static int xen_evtchn_cpu_prepare(unsigned int cpu)
2120 xen_cpu_init_eoi(cpu);
2122 if (evtchn_ops->percpu_init)
2123 ret = evtchn_ops->percpu_init(cpu);
2128 static int xen_evtchn_cpu_dead(unsigned int cpu)
2132 if (evtchn_ops->percpu_deinit)
2133 ret = evtchn_ops->percpu_deinit(cpu);
2138 void __init xen_init_IRQ(void)
2141 unsigned int evtchn;
2144 ret = xen_evtchn_fifo_init();
2146 xen_evtchn_2l_init();
2148 xen_cpu_init_eoi(smp_processor_id());
2150 cpuhp_setup_state_nocalls(CPUHP_XEN_EVTCHN_PREPARE,
2151 "xen/evtchn:prepare",
2152 xen_evtchn_cpu_prepare, xen_evtchn_cpu_dead);
2154 evtchn_to_irq = kcalloc(EVTCHN_ROW(xen_evtchn_max_channels()),
2155 sizeof(*evtchn_to_irq), GFP_KERNEL);
2156 BUG_ON(!evtchn_to_irq);
2158 /* No event channels are 'live' right now. */
2159 for (evtchn = 0; evtchn < xen_evtchn_nr_channels(); evtchn++)
2160 mask_evtchn(evtchn);
2162 pirq_needs_eoi = pirq_needs_eoi_flag;
2165 if (xen_pv_domain()) {
2166 irq_ctx_init(smp_processor_id());
2167 if (xen_initial_domain())
2168 pci_xen_initial_domain();
2170 if (xen_feature(XENFEAT_hvm_callback_vector))
2171 xen_callback_vector();
2173 if (xen_hvm_domain()) {
2175 /* pci_xen_hvm_init must be called after native_init_IRQ so that
2176 * __acpi_register_gsi can point at the right function */
2180 struct physdev_pirq_eoi_gmfn eoi_gmfn;
2182 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
2183 eoi_gmfn.gmfn = virt_to_gfn(pirq_eoi_map);
2184 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
2186 free_page((unsigned long) pirq_eoi_map);
2187 pirq_eoi_map = NULL;
2189 pirq_needs_eoi = pirq_check_eoi_map;