1 // SPDX-License-Identifier: GPL-2.0-only
5 * Xen models interrupts with abstract event channels. Because each
6 * domain gets 1024 event channels, but NR_IRQ is not that large, we
7 * must dynamically map irqs<->event channels. The event channels
8 * interface with the rest of the kernel by defining a xen interrupt
9 * chip. When an event is received, it is mapped to an irq and sent
10 * through the normal interrupt processing path.
12 * There are four kinds of events which can be mapped to an event
15 * 1. Inter-domain notifications. This includes all the virtual
16 * device events, since they're driven by front-ends in another domain
18 * 2. VIRQs, typically used for timers. These are per-cpu events.
20 * 4. PIRQs - Hardware interrupts.
22 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
25 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
27 #include <linux/linkage.h>
28 #include <linux/interrupt.h>
29 #include <linux/irq.h>
30 #include <linux/moduleparam.h>
31 #include <linux/string.h>
32 #include <linux/memblock.h>
33 #include <linux/slab.h>
34 #include <linux/irqnr.h>
35 #include <linux/pci.h>
36 #include <linux/rcupdate.h>
37 #include <linux/spinlock.h>
38 #include <linux/cpuhotplug.h>
39 #include <linux/atomic.h>
40 #include <linux/ktime.h>
44 #include <asm/ptrace.h>
45 #include <asm/idtentry.h>
47 #include <asm/io_apic.h>
48 #include <asm/i8259.h>
49 #include <asm/xen/pci.h>
51 #include <asm/sync_bitops.h>
52 #include <asm/xen/hypercall.h>
53 #include <asm/xen/hypervisor.h>
58 #include <xen/xen-ops.h>
59 #include <xen/events.h>
60 #include <xen/interface/xen.h>
61 #include <xen/interface/event_channel.h>
62 #include <xen/interface/hvm/hvm_op.h>
63 #include <xen/interface/hvm/params.h>
64 #include <xen/interface/physdev.h>
65 #include <xen/interface/sched.h>
66 #include <xen/interface/vcpu.h>
67 #include <asm/hw_irq.h>
69 #include "events_internal.h"
71 #undef MODULE_PARAM_PREFIX
72 #define MODULE_PARAM_PREFIX "xen."
74 /* Interrupt types. */
84 * Packed IRQ information:
85 * type - enum xen_irq_type
86 * event channel - irq->event channel mapping
87 * cpu - cpu this event channel is bound to
88 * index - type-specific information:
89 * PIRQ - vector, with MSB being "needs EIO", or physical IRQ of the HVM
90 * guest, or GSI (real passthrough IRQ) of the device.
96 struct list_head list;
97 struct list_head eoi_list;
98 struct rcu_work rwork;
101 short type; /* type */
102 u8 mask_reason; /* Why is event channel masked */
103 #define EVT_MASK_REASON_EXPLICIT 0x01
104 #define EVT_MASK_REASON_TEMPORARY 0x02
105 #define EVT_MASK_REASON_EOI_PENDING 0x04
106 u8 is_active; /* Is event just being handled? */
108 evtchn_port_t evtchn; /* event channel */
109 unsigned short cpu; /* cpu bound */
110 unsigned short eoi_cpu; /* EOI must happen on this cpu-1 */
111 unsigned int irq_epoch; /* If eoi_cpu valid: irq_epoch of event */
112 u64 eoi_time; /* Time in jiffies when to EOI. */
121 unsigned char vector;
128 #define PIRQ_NEEDS_EOI (1 << 0)
129 #define PIRQ_SHAREABLE (1 << 1)
130 #define PIRQ_MSI_GROUP (1 << 2)
132 static uint __read_mostly event_loop_timeout = 2;
133 module_param(event_loop_timeout, uint, 0644);
135 static uint __read_mostly event_eoi_delay = 10;
136 module_param(event_eoi_delay, uint, 0644);
138 const struct evtchn_ops *evtchn_ops;
141 * This lock protects updates to the following mapping and reference-count
142 * arrays. The lock does not need to be acquired to read the mapping tables.
144 static DEFINE_MUTEX(irq_mapping_update_lock);
149 * irq_mapping_update_lock
151 * percpu eoi_list_lock
155 static LIST_HEAD(xen_irq_list_head);
157 /* IRQ <-> VIRQ mapping. */
158 static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
160 /* IRQ <-> IPI mapping */
161 static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
163 static int **evtchn_to_irq;
165 static unsigned long *pirq_eoi_map;
167 static bool (*pirq_needs_eoi)(unsigned irq);
169 #define EVTCHN_ROW(e) (e / (PAGE_SIZE/sizeof(**evtchn_to_irq)))
170 #define EVTCHN_COL(e) (e % (PAGE_SIZE/sizeof(**evtchn_to_irq)))
171 #define EVTCHN_PER_ROW (PAGE_SIZE / sizeof(**evtchn_to_irq))
173 /* Xen will never allocate port zero for any purpose. */
174 #define VALID_EVTCHN(chn) ((chn) != 0)
176 static struct irq_info *legacy_info_ptrs[NR_IRQS_LEGACY];
178 static struct irq_chip xen_dynamic_chip;
179 static struct irq_chip xen_lateeoi_chip;
180 static struct irq_chip xen_percpu_chip;
181 static struct irq_chip xen_pirq_chip;
182 static void enable_dynirq(struct irq_data *data);
183 static void disable_dynirq(struct irq_data *data);
185 static DEFINE_PER_CPU(unsigned int, irq_epoch);
187 static void clear_evtchn_to_irq_row(int *evtchn_row)
191 for (col = 0; col < EVTCHN_PER_ROW; col++)
192 WRITE_ONCE(evtchn_row[col], -1);
195 static void clear_evtchn_to_irq_all(void)
199 for (row = 0; row < EVTCHN_ROW(xen_evtchn_max_channels()); row++) {
200 if (evtchn_to_irq[row] == NULL)
202 clear_evtchn_to_irq_row(evtchn_to_irq[row]);
206 static int set_evtchn_to_irq(evtchn_port_t evtchn, unsigned int irq)
212 if (evtchn >= xen_evtchn_max_channels())
215 row = EVTCHN_ROW(evtchn);
216 col = EVTCHN_COL(evtchn);
218 if (evtchn_to_irq[row] == NULL) {
219 /* Unallocated irq entries return -1 anyway */
223 evtchn_row = (int *) __get_free_pages(GFP_KERNEL, 0);
224 if (evtchn_row == NULL)
227 clear_evtchn_to_irq_row(evtchn_row);
230 * We've prepared an empty row for the mapping. If a different
231 * thread was faster inserting it, we can drop ours.
233 if (cmpxchg(&evtchn_to_irq[row], NULL, evtchn_row) != NULL)
234 free_page((unsigned long) evtchn_row);
237 WRITE_ONCE(evtchn_to_irq[row][col], irq);
241 int get_evtchn_to_irq(evtchn_port_t evtchn)
243 if (evtchn >= xen_evtchn_max_channels())
245 if (evtchn_to_irq[EVTCHN_ROW(evtchn)] == NULL)
247 return READ_ONCE(evtchn_to_irq[EVTCHN_ROW(evtchn)][EVTCHN_COL(evtchn)]);
250 /* Get info for IRQ */
251 static struct irq_info *info_for_irq(unsigned irq)
253 if (irq < nr_legacy_irqs())
254 return legacy_info_ptrs[irq];
256 return irq_get_chip_data(irq);
259 static void set_info_for_irq(unsigned int irq, struct irq_info *info)
261 if (irq < nr_legacy_irqs())
262 legacy_info_ptrs[irq] = info;
264 irq_set_chip_data(irq, info);
267 static void delayed_free_irq(struct work_struct *work)
269 struct irq_info *info = container_of(to_rcu_work(work), struct irq_info,
271 unsigned int irq = info->irq;
273 /* Remove the info pointer only now, with no potential users left. */
274 set_info_for_irq(irq, NULL);
278 /* Legacy IRQ descriptors are managed by the arch. */
279 if (irq >= nr_legacy_irqs())
283 /* Constructors for packed IRQ information. */
284 static int xen_irq_info_common_setup(struct irq_info *info,
286 enum xen_irq_type type,
287 evtchn_port_t evtchn,
292 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
296 info->evtchn = evtchn;
298 info->mask_reason = EVT_MASK_REASON_EXPLICIT;
299 raw_spin_lock_init(&info->lock);
301 ret = set_evtchn_to_irq(evtchn, irq);
305 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
307 return xen_evtchn_port_setup(evtchn);
310 static int xen_irq_info_evtchn_setup(unsigned irq,
311 evtchn_port_t evtchn)
313 struct irq_info *info = info_for_irq(irq);
315 return xen_irq_info_common_setup(info, irq, IRQT_EVTCHN, evtchn, 0);
318 static int xen_irq_info_ipi_setup(unsigned cpu,
320 evtchn_port_t evtchn,
323 struct irq_info *info = info_for_irq(irq);
327 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
329 return xen_irq_info_common_setup(info, irq, IRQT_IPI, evtchn, 0);
332 static int xen_irq_info_virq_setup(unsigned cpu,
334 evtchn_port_t evtchn,
337 struct irq_info *info = info_for_irq(irq);
341 per_cpu(virq_to_irq, cpu)[virq] = irq;
343 return xen_irq_info_common_setup(info, irq, IRQT_VIRQ, evtchn, 0);
346 static int xen_irq_info_pirq_setup(unsigned irq,
347 evtchn_port_t evtchn,
353 struct irq_info *info = info_for_irq(irq);
355 info->u.pirq.pirq = pirq;
356 info->u.pirq.gsi = gsi;
357 info->u.pirq.domid = domid;
358 info->u.pirq.flags = flags;
360 return xen_irq_info_common_setup(info, irq, IRQT_PIRQ, evtchn, 0);
363 static void xen_irq_info_cleanup(struct irq_info *info)
365 set_evtchn_to_irq(info->evtchn, -1);
366 xen_evtchn_port_remove(info->evtchn, info->cpu);
371 * Accessors for packed IRQ information.
373 evtchn_port_t evtchn_from_irq(unsigned irq)
375 const struct irq_info *info = NULL;
377 if (likely(irq < nr_irqs))
378 info = info_for_irq(irq);
385 unsigned int irq_from_evtchn(evtchn_port_t evtchn)
387 return get_evtchn_to_irq(evtchn);
389 EXPORT_SYMBOL_GPL(irq_from_evtchn);
391 int irq_from_virq(unsigned int cpu, unsigned int virq)
393 return per_cpu(virq_to_irq, cpu)[virq];
396 static enum ipi_vector ipi_from_irq(unsigned irq)
398 struct irq_info *info = info_for_irq(irq);
400 BUG_ON(info == NULL);
401 BUG_ON(info->type != IRQT_IPI);
406 static unsigned virq_from_irq(unsigned irq)
408 struct irq_info *info = info_for_irq(irq);
410 BUG_ON(info == NULL);
411 BUG_ON(info->type != IRQT_VIRQ);
416 static unsigned pirq_from_irq(unsigned irq)
418 struct irq_info *info = info_for_irq(irq);
420 BUG_ON(info == NULL);
421 BUG_ON(info->type != IRQT_PIRQ);
423 return info->u.pirq.pirq;
426 static enum xen_irq_type type_from_irq(unsigned irq)
428 return info_for_irq(irq)->type;
431 static unsigned cpu_from_irq(unsigned irq)
433 return info_for_irq(irq)->cpu;
436 unsigned int cpu_from_evtchn(evtchn_port_t evtchn)
438 int irq = get_evtchn_to_irq(evtchn);
442 ret = cpu_from_irq(irq);
447 static void do_mask(struct irq_info *info, u8 reason)
451 raw_spin_lock_irqsave(&info->lock, flags);
453 if (!info->mask_reason)
454 mask_evtchn(info->evtchn);
456 info->mask_reason |= reason;
458 raw_spin_unlock_irqrestore(&info->lock, flags);
461 static void do_unmask(struct irq_info *info, u8 reason)
465 raw_spin_lock_irqsave(&info->lock, flags);
467 info->mask_reason &= ~reason;
469 if (!info->mask_reason)
470 unmask_evtchn(info->evtchn);
472 raw_spin_unlock_irqrestore(&info->lock, flags);
476 static bool pirq_check_eoi_map(unsigned irq)
478 return test_bit(pirq_from_irq(irq), pirq_eoi_map);
482 static bool pirq_needs_eoi_flag(unsigned irq)
484 struct irq_info *info = info_for_irq(irq);
485 BUG_ON(info->type != IRQT_PIRQ);
487 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
490 static void bind_evtchn_to_cpu(evtchn_port_t evtchn, unsigned int cpu)
492 int irq = get_evtchn_to_irq(evtchn);
493 struct irq_info *info = info_for_irq(irq);
497 cpumask_copy(irq_get_affinity_mask(irq), cpumask_of(cpu));
499 xen_evtchn_port_bind_to_cpu(evtchn, cpu, info->cpu);
505 * notify_remote_via_irq - send event to remote end of event channel via irq
506 * @irq: irq of event channel to send event to
508 * Unlike notify_remote_via_evtchn(), this is safe to use across
509 * save/restore. Notifications on a broken connection are silently
512 void notify_remote_via_irq(int irq)
514 evtchn_port_t evtchn = evtchn_from_irq(irq);
516 if (VALID_EVTCHN(evtchn))
517 notify_remote_via_evtchn(evtchn);
519 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
521 struct lateeoi_work {
522 struct delayed_work delayed;
523 spinlock_t eoi_list_lock;
524 struct list_head eoi_list;
527 static DEFINE_PER_CPU(struct lateeoi_work, lateeoi);
529 static void lateeoi_list_del(struct irq_info *info)
531 struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
534 spin_lock_irqsave(&eoi->eoi_list_lock, flags);
535 list_del_init(&info->eoi_list);
536 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
539 static void lateeoi_list_add(struct irq_info *info)
541 struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
542 struct irq_info *elem;
543 u64 now = get_jiffies_64();
547 if (now < info->eoi_time)
548 delay = info->eoi_time - now;
552 spin_lock_irqsave(&eoi->eoi_list_lock, flags);
554 elem = list_first_entry_or_null(&eoi->eoi_list, struct irq_info,
556 if (!elem || info->eoi_time < elem->eoi_time) {
557 list_add(&info->eoi_list, &eoi->eoi_list);
558 mod_delayed_work_on(info->eoi_cpu, system_wq,
559 &eoi->delayed, delay);
561 list_for_each_entry_reverse(elem, &eoi->eoi_list, eoi_list) {
562 if (elem->eoi_time <= info->eoi_time)
565 list_add(&info->eoi_list, &elem->eoi_list);
568 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
571 static void xen_irq_lateeoi_locked(struct irq_info *info, bool spurious)
573 evtchn_port_t evtchn;
575 unsigned int delay = 0;
577 evtchn = info->evtchn;
578 if (!VALID_EVTCHN(evtchn) || !list_empty(&info->eoi_list))
582 if ((1 << info->spurious_cnt) < (HZ << 2))
583 info->spurious_cnt++;
584 if (info->spurious_cnt > 1) {
585 delay = 1 << (info->spurious_cnt - 2);
589 info->eoi_cpu = smp_processor_id();
590 info->eoi_time = get_jiffies_64() + delay;
593 info->spurious_cnt = 0;
597 if (info->eoi_time &&
598 (info->irq_epoch == per_cpu(irq_epoch, cpu) || delay)) {
599 lateeoi_list_add(info);
605 /* is_active hasn't been reset yet, do it now. */
606 smp_store_release(&info->is_active, 0);
607 do_unmask(info, EVT_MASK_REASON_EOI_PENDING);
610 static void xen_irq_lateeoi_worker(struct work_struct *work)
612 struct lateeoi_work *eoi;
613 struct irq_info *info;
614 u64 now = get_jiffies_64();
617 eoi = container_of(to_delayed_work(work), struct lateeoi_work, delayed);
622 spin_lock_irqsave(&eoi->eoi_list_lock, flags);
624 info = list_first_entry_or_null(&eoi->eoi_list, struct irq_info,
630 if (now < info->eoi_time) {
631 mod_delayed_work_on(info->eoi_cpu, system_wq,
633 info->eoi_time - now);
637 list_del_init(&info->eoi_list);
639 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
643 xen_irq_lateeoi_locked(info, false);
646 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
651 static void xen_cpu_init_eoi(unsigned int cpu)
653 struct lateeoi_work *eoi = &per_cpu(lateeoi, cpu);
655 INIT_DELAYED_WORK(&eoi->delayed, xen_irq_lateeoi_worker);
656 spin_lock_init(&eoi->eoi_list_lock);
657 INIT_LIST_HEAD(&eoi->eoi_list);
660 void xen_irq_lateeoi(unsigned int irq, unsigned int eoi_flags)
662 struct irq_info *info;
666 info = info_for_irq(irq);
669 xen_irq_lateeoi_locked(info, eoi_flags & XEN_EOI_FLAG_SPURIOUS);
673 EXPORT_SYMBOL_GPL(xen_irq_lateeoi);
675 static void xen_irq_init(unsigned irq)
677 struct irq_info *info;
680 /* By default all event channels notify CPU#0. */
681 cpumask_copy(irq_get_affinity_mask(irq), cpumask_of(0));
684 info = kzalloc(sizeof(*info), GFP_KERNEL);
686 panic("Unable to allocate metadata for IRQ%d\n", irq);
688 info->type = IRQT_UNBOUND;
690 INIT_RCU_WORK(&info->rwork, delayed_free_irq);
692 set_info_for_irq(irq, info);
694 INIT_LIST_HEAD(&info->eoi_list);
695 list_add_tail(&info->list, &xen_irq_list_head);
698 static int __must_check xen_allocate_irqs_dynamic(int nvec)
700 int i, irq = irq_alloc_descs(-1, 0, nvec, -1);
703 for (i = 0; i < nvec; i++)
704 xen_irq_init(irq + i);
710 static inline int __must_check xen_allocate_irq_dynamic(void)
713 return xen_allocate_irqs_dynamic(1);
716 static int __must_check xen_allocate_irq_gsi(unsigned gsi)
721 * A PV guest has no concept of a GSI (since it has no ACPI
722 * nor access to/knowledge of the physical APICs). Therefore
723 * all IRQs are dynamically allocated from the entire IRQ
726 if (xen_pv_domain() && !xen_initial_domain())
727 return xen_allocate_irq_dynamic();
729 /* Legacy IRQ descriptors are already allocated by the arch. */
730 if (gsi < nr_legacy_irqs())
733 irq = irq_alloc_desc_at(gsi, -1);
740 static void xen_free_irq(unsigned irq)
742 struct irq_info *info = info_for_irq(irq);
747 if (!list_empty(&info->eoi_list))
748 lateeoi_list_del(info);
750 list_del(&info->list);
752 WARN_ON(info->refcnt > 0);
754 queue_rcu_work(system_wq, &info->rwork);
757 static void xen_evtchn_close(evtchn_port_t port)
759 struct evtchn_close close;
762 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
766 static void event_handler_exit(struct irq_info *info)
768 smp_store_release(&info->is_active, 0);
769 clear_evtchn(info->evtchn);
772 static void pirq_query_unmask(int irq)
774 struct physdev_irq_status_query irq_status;
775 struct irq_info *info = info_for_irq(irq);
777 BUG_ON(info->type != IRQT_PIRQ);
779 irq_status.irq = pirq_from_irq(irq);
780 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
781 irq_status.flags = 0;
783 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
784 if (irq_status.flags & XENIRQSTAT_needs_eoi)
785 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
788 static void eoi_pirq(struct irq_data *data)
790 struct irq_info *info = info_for_irq(data->irq);
791 evtchn_port_t evtchn = info ? info->evtchn : 0;
792 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
795 if (!VALID_EVTCHN(evtchn))
798 if (unlikely(irqd_is_setaffinity_pending(data)) &&
799 likely(!irqd_irq_disabled(data))) {
800 do_mask(info, EVT_MASK_REASON_TEMPORARY);
802 event_handler_exit(info);
804 irq_move_masked_irq(data);
806 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
808 event_handler_exit(info);
810 if (pirq_needs_eoi(data->irq)) {
811 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
816 static void mask_ack_pirq(struct irq_data *data)
818 disable_dynirq(data);
822 static unsigned int __startup_pirq(unsigned int irq)
824 struct evtchn_bind_pirq bind_pirq;
825 struct irq_info *info = info_for_irq(irq);
826 evtchn_port_t evtchn = evtchn_from_irq(irq);
829 BUG_ON(info->type != IRQT_PIRQ);
831 if (VALID_EVTCHN(evtchn))
834 bind_pirq.pirq = pirq_from_irq(irq);
835 /* NB. We are happy to share unless we are probing. */
836 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
837 BIND_PIRQ__WILL_SHARE : 0;
838 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
840 pr_warn("Failed to obtain physical IRQ %d\n", irq);
843 evtchn = bind_pirq.port;
845 pirq_query_unmask(irq);
847 rc = set_evtchn_to_irq(evtchn, irq);
851 info->evtchn = evtchn;
852 bind_evtchn_to_cpu(evtchn, 0);
854 rc = xen_evtchn_port_setup(evtchn);
859 do_unmask(info, EVT_MASK_REASON_EXPLICIT);
861 eoi_pirq(irq_get_irq_data(irq));
866 pr_err("irq%d: Failed to set port to irq mapping (%d)\n", irq, rc);
867 xen_evtchn_close(evtchn);
871 static unsigned int startup_pirq(struct irq_data *data)
873 return __startup_pirq(data->irq);
876 static void shutdown_pirq(struct irq_data *data)
878 unsigned int irq = data->irq;
879 struct irq_info *info = info_for_irq(irq);
880 evtchn_port_t evtchn = evtchn_from_irq(irq);
882 BUG_ON(info->type != IRQT_PIRQ);
884 if (!VALID_EVTCHN(evtchn))
887 do_mask(info, EVT_MASK_REASON_EXPLICIT);
888 xen_irq_info_cleanup(info);
889 xen_evtchn_close(evtchn);
892 static void enable_pirq(struct irq_data *data)
897 static void disable_pirq(struct irq_data *data)
899 disable_dynirq(data);
902 int xen_irq_from_gsi(unsigned gsi)
904 struct irq_info *info;
906 list_for_each_entry(info, &xen_irq_list_head, list) {
907 if (info->type != IRQT_PIRQ)
910 if (info->u.pirq.gsi == gsi)
916 EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
918 static void __unbind_from_irq(unsigned int irq)
920 evtchn_port_t evtchn = evtchn_from_irq(irq);
921 struct irq_info *info = info_for_irq(irq);
923 if (info->refcnt > 0) {
925 if (info->refcnt != 0)
929 if (VALID_EVTCHN(evtchn)) {
930 unsigned int cpu = cpu_from_irq(irq);
932 switch (type_from_irq(irq)) {
934 per_cpu(virq_to_irq, cpu)[virq_from_irq(irq)] = -1;
937 per_cpu(ipi_to_irq, cpu)[ipi_from_irq(irq)] = -1;
943 xen_irq_info_cleanup(info);
944 xen_evtchn_close(evtchn);
951 * Do not make any assumptions regarding the relationship between the
952 * IRQ number returned here and the Xen pirq argument.
954 * Note: We don't assign an event channel until the irq actually started
955 * up. Return an existing irq if we've already got one for the gsi.
957 * Shareable implies level triggered, not shareable implies edge
960 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
961 unsigned pirq, int shareable, char *name)
964 struct physdev_irq irq_op;
967 mutex_lock(&irq_mapping_update_lock);
969 irq = xen_irq_from_gsi(gsi);
971 pr_info("%s: returning irq %d for gsi %u\n",
976 irq = xen_allocate_irq_gsi(gsi);
983 /* Only the privileged domain can do this. For non-priv, the pcifront
984 * driver provides a PCI bus that does the call to do exactly
985 * this in the priv domain. */
986 if (xen_initial_domain() &&
987 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
993 ret = xen_irq_info_pirq_setup(irq, 0, pirq, gsi, DOMID_SELF,
994 shareable ? PIRQ_SHAREABLE : 0);
996 __unbind_from_irq(irq);
1001 pirq_query_unmask(irq);
1002 /* We try to use the handler with the appropriate semantic for the
1003 * type of interrupt: if the interrupt is an edge triggered
1004 * interrupt we use handle_edge_irq.
1006 * On the other hand if the interrupt is level triggered we use
1007 * handle_fasteoi_irq like the native code does for this kind of
1010 * Depending on the Xen version, pirq_needs_eoi might return true
1011 * not only for level triggered interrupts but for edge triggered
1012 * interrupts too. In any case Xen always honors the eoi mechanism,
1013 * not injecting any more pirqs of the same kind if the first one
1014 * hasn't received an eoi yet. Therefore using the fasteoi handler
1015 * is the right choice either way.
1018 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
1019 handle_fasteoi_irq, name);
1021 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
1022 handle_edge_irq, name);
1025 mutex_unlock(&irq_mapping_update_lock);
1030 #ifdef CONFIG_PCI_MSI
1031 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
1034 struct physdev_get_free_pirq op_get_free_pirq;
1036 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
1037 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
1039 WARN_ONCE(rc == -ENOSYS,
1040 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
1042 return rc ? -1 : op_get_free_pirq.pirq;
1045 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
1046 int pirq, int nvec, const char *name, domid_t domid)
1050 mutex_lock(&irq_mapping_update_lock);
1052 irq = xen_allocate_irqs_dynamic(nvec);
1056 for (i = 0; i < nvec; i++) {
1057 irq_set_chip_and_handler_name(irq + i, &xen_pirq_chip, handle_edge_irq, name);
1059 ret = xen_irq_info_pirq_setup(irq + i, 0, pirq + i, 0, domid,
1060 i == 0 ? 0 : PIRQ_MSI_GROUP);
1065 ret = irq_set_msi_desc(irq, msidesc);
1069 mutex_unlock(&irq_mapping_update_lock);
1073 __unbind_from_irq(irq + nvec);
1074 mutex_unlock(&irq_mapping_update_lock);
1079 int xen_destroy_irq(int irq)
1081 struct physdev_unmap_pirq unmap_irq;
1082 struct irq_info *info = info_for_irq(irq);
1085 mutex_lock(&irq_mapping_update_lock);
1088 * If trying to remove a vector in a MSI group different
1089 * than the first one skip the PIRQ unmap unless this vector
1090 * is the first one in the group.
1092 if (xen_initial_domain() && !(info->u.pirq.flags & PIRQ_MSI_GROUP)) {
1093 unmap_irq.pirq = info->u.pirq.pirq;
1094 unmap_irq.domid = info->u.pirq.domid;
1095 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
1096 /* If another domain quits without making the pci_disable_msix
1097 * call, the Xen hypervisor takes care of freeing the PIRQs
1098 * (free_domain_pirqs).
1100 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
1101 pr_info("domain %d does not have %d anymore\n",
1102 info->u.pirq.domid, info->u.pirq.pirq);
1104 pr_warn("unmap irq failed %d\n", rc);
1112 mutex_unlock(&irq_mapping_update_lock);
1116 int xen_irq_from_pirq(unsigned pirq)
1120 struct irq_info *info;
1122 mutex_lock(&irq_mapping_update_lock);
1124 list_for_each_entry(info, &xen_irq_list_head, list) {
1125 if (info->type != IRQT_PIRQ)
1128 if (info->u.pirq.pirq == pirq)
1133 mutex_unlock(&irq_mapping_update_lock);
1139 int xen_pirq_from_irq(unsigned irq)
1141 return pirq_from_irq(irq);
1143 EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
1145 static int bind_evtchn_to_irq_chip(evtchn_port_t evtchn, struct irq_chip *chip)
1150 if (evtchn >= xen_evtchn_max_channels())
1153 mutex_lock(&irq_mapping_update_lock);
1155 irq = get_evtchn_to_irq(evtchn);
1158 irq = xen_allocate_irq_dynamic();
1162 irq_set_chip_and_handler_name(irq, chip,
1163 handle_edge_irq, "event");
1165 ret = xen_irq_info_evtchn_setup(irq, evtchn);
1167 __unbind_from_irq(irq);
1171 /* New interdomain events are bound to VCPU 0. */
1172 bind_evtchn_to_cpu(evtchn, 0);
1174 struct irq_info *info = info_for_irq(irq);
1175 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
1179 mutex_unlock(&irq_mapping_update_lock);
1184 int bind_evtchn_to_irq(evtchn_port_t evtchn)
1186 return bind_evtchn_to_irq_chip(evtchn, &xen_dynamic_chip);
1188 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
1190 int bind_evtchn_to_irq_lateeoi(evtchn_port_t evtchn)
1192 return bind_evtchn_to_irq_chip(evtchn, &xen_lateeoi_chip);
1194 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq_lateeoi);
1196 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
1198 struct evtchn_bind_ipi bind_ipi;
1199 evtchn_port_t evtchn;
1202 mutex_lock(&irq_mapping_update_lock);
1204 irq = per_cpu(ipi_to_irq, cpu)[ipi];
1207 irq = xen_allocate_irq_dynamic();
1211 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
1212 handle_percpu_irq, "ipi");
1214 bind_ipi.vcpu = xen_vcpu_nr(cpu);
1215 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1218 evtchn = bind_ipi.port;
1220 ret = xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
1222 __unbind_from_irq(irq);
1226 bind_evtchn_to_cpu(evtchn, cpu);
1228 struct irq_info *info = info_for_irq(irq);
1229 WARN_ON(info == NULL || info->type != IRQT_IPI);
1233 mutex_unlock(&irq_mapping_update_lock);
1237 static int bind_interdomain_evtchn_to_irq_chip(unsigned int remote_domain,
1238 evtchn_port_t remote_port,
1239 struct irq_chip *chip)
1241 struct evtchn_bind_interdomain bind_interdomain;
1244 bind_interdomain.remote_dom = remote_domain;
1245 bind_interdomain.remote_port = remote_port;
1247 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
1250 return err ? : bind_evtchn_to_irq_chip(bind_interdomain.local_port,
1254 int bind_interdomain_evtchn_to_irq_lateeoi(unsigned int remote_domain,
1255 evtchn_port_t remote_port)
1257 return bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1260 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irq_lateeoi);
1262 static int find_virq(unsigned int virq, unsigned int cpu, evtchn_port_t *evtchn)
1264 struct evtchn_status status;
1268 memset(&status, 0, sizeof(status));
1269 for (port = 0; port < xen_evtchn_max_channels(); port++) {
1270 status.dom = DOMID_SELF;
1272 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
1275 if (status.status != EVTCHNSTAT_virq)
1277 if (status.u.virq == virq && status.vcpu == xen_vcpu_nr(cpu)) {
1286 * xen_evtchn_nr_channels - number of usable event channel ports
1288 * This may be less than the maximum supported by the current
1289 * hypervisor ABI. Use xen_evtchn_max_channels() for the maximum
1292 unsigned xen_evtchn_nr_channels(void)
1294 return evtchn_ops->nr_channels();
1296 EXPORT_SYMBOL_GPL(xen_evtchn_nr_channels);
1298 int bind_virq_to_irq(unsigned int virq, unsigned int cpu, bool percpu)
1300 struct evtchn_bind_virq bind_virq;
1301 evtchn_port_t evtchn = 0;
1304 mutex_lock(&irq_mapping_update_lock);
1306 irq = per_cpu(virq_to_irq, cpu)[virq];
1309 irq = xen_allocate_irq_dynamic();
1314 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
1315 handle_percpu_irq, "virq");
1317 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
1318 handle_edge_irq, "virq");
1320 bind_virq.virq = virq;
1321 bind_virq.vcpu = xen_vcpu_nr(cpu);
1322 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1325 evtchn = bind_virq.port;
1328 ret = find_virq(virq, cpu, &evtchn);
1332 ret = xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1334 __unbind_from_irq(irq);
1339 bind_evtchn_to_cpu(evtchn, cpu);
1341 struct irq_info *info = info_for_irq(irq);
1342 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
1346 mutex_unlock(&irq_mapping_update_lock);
1351 static void unbind_from_irq(unsigned int irq)
1353 mutex_lock(&irq_mapping_update_lock);
1354 __unbind_from_irq(irq);
1355 mutex_unlock(&irq_mapping_update_lock);
1358 static int bind_evtchn_to_irqhandler_chip(evtchn_port_t evtchn,
1359 irq_handler_t handler,
1360 unsigned long irqflags,
1361 const char *devname, void *dev_id,
1362 struct irq_chip *chip)
1366 irq = bind_evtchn_to_irq_chip(evtchn, chip);
1369 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1371 unbind_from_irq(irq);
1378 int bind_evtchn_to_irqhandler(evtchn_port_t evtchn,
1379 irq_handler_t handler,
1380 unsigned long irqflags,
1381 const char *devname, void *dev_id)
1383 return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1387 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1389 int bind_evtchn_to_irqhandler_lateeoi(evtchn_port_t evtchn,
1390 irq_handler_t handler,
1391 unsigned long irqflags,
1392 const char *devname, void *dev_id)
1394 return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1398 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler_lateeoi);
1400 static int bind_interdomain_evtchn_to_irqhandler_chip(
1401 unsigned int remote_domain, evtchn_port_t remote_port,
1402 irq_handler_t handler, unsigned long irqflags,
1403 const char *devname, void *dev_id, struct irq_chip *chip)
1407 irq = bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1412 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1414 unbind_from_irq(irq);
1421 int bind_interdomain_evtchn_to_irqhandler_lateeoi(unsigned int remote_domain,
1422 evtchn_port_t remote_port,
1423 irq_handler_t handler,
1424 unsigned long irqflags,
1425 const char *devname,
1428 return bind_interdomain_evtchn_to_irqhandler_chip(remote_domain,
1429 remote_port, handler, irqflags, devname,
1430 dev_id, &xen_lateeoi_chip);
1432 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler_lateeoi);
1434 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
1435 irq_handler_t handler,
1436 unsigned long irqflags, const char *devname, void *dev_id)
1440 irq = bind_virq_to_irq(virq, cpu, irqflags & IRQF_PERCPU);
1443 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1445 unbind_from_irq(irq);
1451 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1453 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1455 irq_handler_t handler,
1456 unsigned long irqflags,
1457 const char *devname,
1462 irq = bind_ipi_to_irq(ipi, cpu);
1466 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
1467 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1469 unbind_from_irq(irq);
1476 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1478 struct irq_info *info = info_for_irq(irq);
1482 free_irq(irq, dev_id);
1483 unbind_from_irq(irq);
1485 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1488 * xen_set_irq_priority() - set an event channel priority.
1489 * @irq:irq bound to an event channel.
1490 * @priority: priority between XEN_IRQ_PRIORITY_MAX and XEN_IRQ_PRIORITY_MIN.
1492 int xen_set_irq_priority(unsigned irq, unsigned priority)
1494 struct evtchn_set_priority set_priority;
1496 set_priority.port = evtchn_from_irq(irq);
1497 set_priority.priority = priority;
1499 return HYPERVISOR_event_channel_op(EVTCHNOP_set_priority,
1502 EXPORT_SYMBOL_GPL(xen_set_irq_priority);
1504 int evtchn_make_refcounted(evtchn_port_t evtchn)
1506 int irq = get_evtchn_to_irq(evtchn);
1507 struct irq_info *info;
1512 info = info_for_irq(irq);
1517 WARN_ON(info->refcnt != -1);
1523 EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1525 int evtchn_get(evtchn_port_t evtchn)
1528 struct irq_info *info;
1531 if (evtchn >= xen_evtchn_max_channels())
1534 mutex_lock(&irq_mapping_update_lock);
1536 irq = get_evtchn_to_irq(evtchn);
1540 info = info_for_irq(irq);
1546 if (info->refcnt <= 0 || info->refcnt == SHRT_MAX)
1552 mutex_unlock(&irq_mapping_update_lock);
1556 EXPORT_SYMBOL_GPL(evtchn_get);
1558 void evtchn_put(evtchn_port_t evtchn)
1560 int irq = get_evtchn_to_irq(evtchn);
1561 if (WARN_ON(irq == -1))
1563 unbind_from_irq(irq);
1565 EXPORT_SYMBOL_GPL(evtchn_put);
1567 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1572 if (unlikely(vector == XEN_NMI_VECTOR)) {
1573 int rc = HYPERVISOR_vcpu_op(VCPUOP_send_nmi, xen_vcpu_nr(cpu),
1576 printk(KERN_WARNING "Sending nmi to CPU%d failed (rc:%d)\n", cpu, rc);
1580 irq = per_cpu(ipi_to_irq, cpu)[vector];
1582 notify_remote_via_irq(irq);
1585 struct evtchn_loop_ctrl {
1591 void handle_irq_for_port(evtchn_port_t port, struct evtchn_loop_ctrl *ctrl)
1594 struct irq_info *info;
1596 irq = get_evtchn_to_irq(port);
1601 * Check for timeout every 256 events.
1602 * We are setting the timeout value only after the first 256
1603 * events in order to not hurt the common case of few loop
1604 * iterations. The 256 is basically an arbitrary value.
1606 * In case we are hitting the timeout we need to defer all further
1607 * EOIs in order to ensure to leave the event handling loop rather
1608 * sooner than later.
1610 if (!ctrl->defer_eoi && !(++ctrl->count & 0xff)) {
1611 ktime_t kt = ktime_get();
1613 if (!ctrl->timeout) {
1614 kt = ktime_add_ms(kt,
1615 jiffies_to_msecs(event_loop_timeout));
1617 } else if (kt > ctrl->timeout) {
1618 ctrl->defer_eoi = true;
1622 info = info_for_irq(irq);
1623 if (xchg_acquire(&info->is_active, 1))
1626 if (ctrl->defer_eoi) {
1627 info->eoi_cpu = smp_processor_id();
1628 info->irq_epoch = __this_cpu_read(irq_epoch);
1629 info->eoi_time = get_jiffies_64() + event_eoi_delay;
1632 generic_handle_irq(irq);
1635 static void __xen_evtchn_do_upcall(void)
1637 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1638 int cpu = smp_processor_id();
1639 struct evtchn_loop_ctrl ctrl = { 0 };
1642 * When closing an event channel the associated IRQ must not be freed
1643 * until all cpus have left the event handling loop. This is ensured
1644 * by taking the rcu_read_lock() while handling events, as freeing of
1645 * the IRQ is handled via queue_rcu_work() _after_ closing the event
1651 vcpu_info->evtchn_upcall_pending = 0;
1653 xen_evtchn_handle_events(cpu, &ctrl);
1655 BUG_ON(!irqs_disabled());
1657 virt_rmb(); /* Hypervisor can set upcall pending. */
1659 } while (vcpu_info->evtchn_upcall_pending);
1664 * Increment irq_epoch only now to defer EOIs only for
1665 * xen_irq_lateeoi() invocations occurring from inside the loop
1668 __this_cpu_inc(irq_epoch);
1671 void xen_evtchn_do_upcall(struct pt_regs *regs)
1673 struct pt_regs *old_regs = set_irq_regs(regs);
1677 __xen_evtchn_do_upcall();
1680 set_irq_regs(old_regs);
1683 void xen_hvm_evtchn_do_upcall(void)
1685 __xen_evtchn_do_upcall();
1687 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1689 /* Rebind a new event channel to an existing irq. */
1690 void rebind_evtchn_irq(evtchn_port_t evtchn, int irq)
1692 struct irq_info *info = info_for_irq(irq);
1697 /* Make sure the irq is masked, since the new event channel
1698 will also be masked. */
1701 mutex_lock(&irq_mapping_update_lock);
1703 /* After resume the irq<->evtchn mappings are all cleared out */
1704 BUG_ON(get_evtchn_to_irq(evtchn) != -1);
1705 /* Expect irq to have been bound before,
1706 so there should be a proper type */
1707 BUG_ON(info->type == IRQT_UNBOUND);
1709 (void)xen_irq_info_evtchn_setup(irq, evtchn);
1711 mutex_unlock(&irq_mapping_update_lock);
1713 bind_evtchn_to_cpu(evtchn, info->cpu);
1714 /* This will be deferred until interrupt is processed */
1715 irq_set_affinity(irq, cpumask_of(info->cpu));
1717 /* Unmask the event channel. */
1721 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1722 static int xen_rebind_evtchn_to_cpu(struct irq_info *info, unsigned int tcpu)
1724 struct evtchn_bind_vcpu bind_vcpu;
1725 evtchn_port_t evtchn = info ? info->evtchn : 0;
1727 if (!VALID_EVTCHN(evtchn))
1730 if (!xen_support_evtchn_rebind())
1733 /* Send future instances of this interrupt to other vcpu. */
1734 bind_vcpu.port = evtchn;
1735 bind_vcpu.vcpu = xen_vcpu_nr(tcpu);
1738 * Mask the event while changing the VCPU binding to prevent
1739 * it being delivered on an unexpected VCPU.
1741 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1744 * If this fails, it usually just indicates that we're dealing with a
1745 * virq or IPI channel, which don't actually need to be rebound. Ignore
1746 * it, but don't do the xenlinux-level rebind in that case.
1748 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1749 bind_evtchn_to_cpu(evtchn, tcpu);
1751 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1756 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1759 unsigned tcpu = cpumask_first_and(dest, cpu_online_mask);
1760 int ret = xen_rebind_evtchn_to_cpu(info_for_irq(data->irq), tcpu);
1763 irq_data_update_effective_affinity(data, cpumask_of(tcpu));
1768 /* To be called with desc->lock held. */
1769 int xen_set_affinity_evtchn(struct irq_desc *desc, unsigned int tcpu)
1771 struct irq_data *d = irq_desc_get_irq_data(desc);
1773 return set_affinity_irq(d, cpumask_of(tcpu), false);
1775 EXPORT_SYMBOL_GPL(xen_set_affinity_evtchn);
1777 static void enable_dynirq(struct irq_data *data)
1779 struct irq_info *info = info_for_irq(data->irq);
1780 evtchn_port_t evtchn = info ? info->evtchn : 0;
1782 if (VALID_EVTCHN(evtchn))
1783 do_unmask(info, EVT_MASK_REASON_EXPLICIT);
1786 static void disable_dynirq(struct irq_data *data)
1788 struct irq_info *info = info_for_irq(data->irq);
1789 evtchn_port_t evtchn = info ? info->evtchn : 0;
1791 if (VALID_EVTCHN(evtchn))
1792 do_mask(info, EVT_MASK_REASON_EXPLICIT);
1795 static void ack_dynirq(struct irq_data *data)
1797 struct irq_info *info = info_for_irq(data->irq);
1798 evtchn_port_t evtchn = info ? info->evtchn : 0;
1800 if (!VALID_EVTCHN(evtchn))
1803 if (unlikely(irqd_is_setaffinity_pending(data)) &&
1804 likely(!irqd_irq_disabled(data))) {
1805 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1807 event_handler_exit(info);
1809 irq_move_masked_irq(data);
1811 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1813 event_handler_exit(info);
1816 static void mask_ack_dynirq(struct irq_data *data)
1818 disable_dynirq(data);
1822 static void lateeoi_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))
1830 do_mask(info, EVT_MASK_REASON_EOI_PENDING);
1832 if (unlikely(irqd_is_setaffinity_pending(data)) &&
1833 likely(!irqd_irq_disabled(data))) {
1834 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1836 clear_evtchn(evtchn);
1838 irq_move_masked_irq(data);
1840 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1842 clear_evtchn(evtchn);
1845 static void lateeoi_mask_ack_dynirq(struct irq_data *data)
1847 struct irq_info *info = info_for_irq(data->irq);
1848 evtchn_port_t evtchn = info ? info->evtchn : 0;
1850 if (VALID_EVTCHN(evtchn)) {
1851 do_mask(info, EVT_MASK_REASON_EXPLICIT);
1856 static int retrigger_dynirq(struct irq_data *data)
1858 struct irq_info *info = info_for_irq(data->irq);
1859 evtchn_port_t evtchn = info ? info->evtchn : 0;
1861 if (!VALID_EVTCHN(evtchn))
1864 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1866 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1871 static void restore_pirqs(void)
1873 int pirq, rc, irq, gsi;
1874 struct physdev_map_pirq map_irq;
1875 struct irq_info *info;
1877 list_for_each_entry(info, &xen_irq_list_head, list) {
1878 if (info->type != IRQT_PIRQ)
1881 pirq = info->u.pirq.pirq;
1882 gsi = info->u.pirq.gsi;
1885 /* save/restore of PT devices doesn't work, so at this point the
1886 * only devices present are GSI based emulated devices */
1890 map_irq.domid = DOMID_SELF;
1891 map_irq.type = MAP_PIRQ_TYPE_GSI;
1892 map_irq.index = gsi;
1893 map_irq.pirq = pirq;
1895 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1897 pr_warn("xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1898 gsi, irq, pirq, rc);
1903 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1905 __startup_pirq(irq);
1909 static void restore_cpu_virqs(unsigned int cpu)
1911 struct evtchn_bind_virq bind_virq;
1912 evtchn_port_t evtchn;
1915 for (virq = 0; virq < NR_VIRQS; virq++) {
1916 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1919 BUG_ON(virq_from_irq(irq) != virq);
1921 /* Get a new binding from Xen. */
1922 bind_virq.virq = virq;
1923 bind_virq.vcpu = xen_vcpu_nr(cpu);
1924 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1927 evtchn = bind_virq.port;
1929 /* Record the new mapping. */
1930 (void)xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1931 bind_evtchn_to_cpu(evtchn, cpu);
1935 static void restore_cpu_ipis(unsigned int cpu)
1937 struct evtchn_bind_ipi bind_ipi;
1938 evtchn_port_t evtchn;
1941 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1942 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1945 BUG_ON(ipi_from_irq(irq) != ipi);
1947 /* Get a new binding from Xen. */
1948 bind_ipi.vcpu = xen_vcpu_nr(cpu);
1949 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1952 evtchn = bind_ipi.port;
1954 /* Record the new mapping. */
1955 (void)xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
1956 bind_evtchn_to_cpu(evtchn, cpu);
1960 /* Clear an irq's pending state, in preparation for polling on it */
1961 void xen_clear_irq_pending(int irq)
1963 struct irq_info *info = info_for_irq(irq);
1964 evtchn_port_t evtchn = info ? info->evtchn : 0;
1966 if (VALID_EVTCHN(evtchn))
1967 event_handler_exit(info);
1969 EXPORT_SYMBOL(xen_clear_irq_pending);
1970 void xen_set_irq_pending(int irq)
1972 evtchn_port_t evtchn = evtchn_from_irq(irq);
1974 if (VALID_EVTCHN(evtchn))
1978 bool xen_test_irq_pending(int irq)
1980 evtchn_port_t evtchn = evtchn_from_irq(irq);
1983 if (VALID_EVTCHN(evtchn))
1984 ret = test_evtchn(evtchn);
1989 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1990 * the irq will be disabled so it won't deliver an interrupt. */
1991 void xen_poll_irq_timeout(int irq, u64 timeout)
1993 evtchn_port_t evtchn = evtchn_from_irq(irq);
1995 if (VALID_EVTCHN(evtchn)) {
1996 struct sched_poll poll;
1999 poll.timeout = timeout;
2000 set_xen_guest_handle(poll.ports, &evtchn);
2002 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
2006 EXPORT_SYMBOL(xen_poll_irq_timeout);
2007 /* Poll waiting for an irq to become pending. In the usual case, the
2008 * irq will be disabled so it won't deliver an interrupt. */
2009 void xen_poll_irq(int irq)
2011 xen_poll_irq_timeout(irq, 0 /* no timeout */);
2014 /* Check whether the IRQ line is shared with other guests. */
2015 int xen_test_irq_shared(int irq)
2017 struct irq_info *info = info_for_irq(irq);
2018 struct physdev_irq_status_query irq_status;
2023 irq_status.irq = info->u.pirq.pirq;
2025 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
2027 return !(irq_status.flags & XENIRQSTAT_shared);
2029 EXPORT_SYMBOL_GPL(xen_test_irq_shared);
2031 void xen_irq_resume(void)
2034 struct irq_info *info;
2036 /* New event-channel space is not 'live' yet. */
2037 xen_evtchn_resume();
2039 /* No IRQ <-> event-channel mappings. */
2040 list_for_each_entry(info, &xen_irq_list_head, list)
2041 info->evtchn = 0; /* zap event-channel binding */
2043 clear_evtchn_to_irq_all();
2045 for_each_possible_cpu(cpu) {
2046 restore_cpu_virqs(cpu);
2047 restore_cpu_ipis(cpu);
2053 static struct irq_chip xen_dynamic_chip __read_mostly = {
2056 .irq_disable = disable_dynirq,
2057 .irq_mask = disable_dynirq,
2058 .irq_unmask = enable_dynirq,
2060 .irq_ack = ack_dynirq,
2061 .irq_mask_ack = mask_ack_dynirq,
2063 .irq_set_affinity = set_affinity_irq,
2064 .irq_retrigger = retrigger_dynirq,
2067 static struct irq_chip xen_lateeoi_chip __read_mostly = {
2068 /* The chip name needs to contain "xen-dyn" for irqbalance to work. */
2069 .name = "xen-dyn-lateeoi",
2071 .irq_disable = disable_dynirq,
2072 .irq_mask = disable_dynirq,
2073 .irq_unmask = enable_dynirq,
2075 .irq_ack = lateeoi_ack_dynirq,
2076 .irq_mask_ack = lateeoi_mask_ack_dynirq,
2078 .irq_set_affinity = set_affinity_irq,
2079 .irq_retrigger = retrigger_dynirq,
2082 static struct irq_chip xen_pirq_chip __read_mostly = {
2085 .irq_startup = startup_pirq,
2086 .irq_shutdown = shutdown_pirq,
2087 .irq_enable = enable_pirq,
2088 .irq_disable = disable_pirq,
2090 .irq_mask = disable_dynirq,
2091 .irq_unmask = enable_dynirq,
2093 .irq_ack = eoi_pirq,
2094 .irq_eoi = eoi_pirq,
2095 .irq_mask_ack = mask_ack_pirq,
2097 .irq_set_affinity = set_affinity_irq,
2099 .irq_retrigger = retrigger_dynirq,
2102 static struct irq_chip xen_percpu_chip __read_mostly = {
2103 .name = "xen-percpu",
2105 .irq_disable = disable_dynirq,
2106 .irq_mask = disable_dynirq,
2107 .irq_unmask = enable_dynirq,
2109 .irq_ack = ack_dynirq,
2112 #ifdef CONFIG_XEN_PVHVM
2113 /* Vector callbacks are better than PCI interrupts to receive event
2114 * channel notifications because we can receive vector callbacks on any
2115 * vcpu and we don't need PCI support or APIC interactions. */
2116 void xen_setup_callback_vector(void)
2118 uint64_t callback_via;
2120 if (xen_have_vector_callback) {
2121 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
2122 if (xen_set_callback_via(callback_via)) {
2123 pr_err("Request for Xen HVM callback vector failed\n");
2124 xen_have_vector_callback = 0;
2129 static __init void xen_alloc_callback_vector(void)
2131 if (!xen_have_vector_callback)
2134 pr_info("Xen HVM callback vector for event delivery is enabled\n");
2135 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_xen_hvm_callback);
2138 void xen_setup_callback_vector(void) {}
2139 static inline void xen_alloc_callback_vector(void) {}
2142 bool xen_fifo_events = true;
2143 module_param_named(fifo_events, xen_fifo_events, bool, 0);
2145 static int xen_evtchn_cpu_prepare(unsigned int cpu)
2149 xen_cpu_init_eoi(cpu);
2151 if (evtchn_ops->percpu_init)
2152 ret = evtchn_ops->percpu_init(cpu);
2157 static int xen_evtchn_cpu_dead(unsigned int cpu)
2161 if (evtchn_ops->percpu_deinit)
2162 ret = evtchn_ops->percpu_deinit(cpu);
2167 void __init xen_init_IRQ(void)
2170 evtchn_port_t evtchn;
2172 if (xen_fifo_events)
2173 ret = xen_evtchn_fifo_init();
2175 xen_evtchn_2l_init();
2176 xen_fifo_events = false;
2179 xen_cpu_init_eoi(smp_processor_id());
2181 cpuhp_setup_state_nocalls(CPUHP_XEN_EVTCHN_PREPARE,
2182 "xen/evtchn:prepare",
2183 xen_evtchn_cpu_prepare, xen_evtchn_cpu_dead);
2185 evtchn_to_irq = kcalloc(EVTCHN_ROW(xen_evtchn_max_channels()),
2186 sizeof(*evtchn_to_irq), GFP_KERNEL);
2187 BUG_ON(!evtchn_to_irq);
2189 /* No event channels are 'live' right now. */
2190 for (evtchn = 0; evtchn < xen_evtchn_nr_channels(); evtchn++)
2191 mask_evtchn(evtchn);
2193 pirq_needs_eoi = pirq_needs_eoi_flag;
2196 if (xen_pv_domain()) {
2197 if (xen_initial_domain())
2198 pci_xen_initial_domain();
2200 if (xen_feature(XENFEAT_hvm_callback_vector)) {
2201 xen_setup_callback_vector();
2202 xen_alloc_callback_vector();
2205 if (xen_hvm_domain()) {
2207 /* pci_xen_hvm_init must be called after native_init_IRQ so that
2208 * __acpi_register_gsi can point at the right function */
2212 struct physdev_pirq_eoi_gmfn eoi_gmfn;
2214 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
2215 eoi_gmfn.gmfn = virt_to_gfn(pirq_eoi_map);
2216 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
2218 free_page((unsigned long) pirq_eoi_map);
2219 pirq_eoi_map = NULL;
2221 pirq_needs_eoi = pirq_check_eoi_map;