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/spinlock.h>
37 #include <linux/cpuhotplug.h>
38 #include <linux/atomic.h>
39 #include <linux/ktime.h>
43 #include <asm/ptrace.h>
45 #include <asm/io_apic.h>
46 #include <asm/i8259.h>
47 #include <asm/xen/pci.h>
49 #include <asm/sync_bitops.h>
50 #include <asm/xen/hypercall.h>
51 #include <asm/xen/hypervisor.h>
56 #include <xen/xen-ops.h>
57 #include <xen/events.h>
58 #include <xen/interface/xen.h>
59 #include <xen/interface/event_channel.h>
60 #include <xen/interface/hvm/hvm_op.h>
61 #include <xen/interface/hvm/params.h>
62 #include <xen/interface/physdev.h>
63 #include <xen/interface/sched.h>
64 #include <xen/interface/vcpu.h>
65 #include <asm/hw_irq.h>
67 #include "events_internal.h"
69 #undef MODULE_PARAM_PREFIX
70 #define MODULE_PARAM_PREFIX "xen."
72 static uint __read_mostly event_loop_timeout = 2;
73 module_param(event_loop_timeout, uint, 0644);
75 static uint __read_mostly event_eoi_delay = 10;
76 module_param(event_eoi_delay, uint, 0644);
78 const struct evtchn_ops *evtchn_ops;
81 * This lock protects updates to the following mapping and reference-count
82 * arrays. The lock does not need to be acquired to read the mapping tables.
84 static DEFINE_MUTEX(irq_mapping_update_lock);
87 * Lock protecting event handling loop against removing event channels.
88 * Adding of event channels is no issue as the associated IRQ becomes active
89 * only after everything is setup (before request_[threaded_]irq() the handler
90 * can't be entered for an event, as the event channel will be unmasked only
93 static DEFINE_RWLOCK(evtchn_rwlock);
98 * irq_mapping_update_lock
101 * percpu eoi_list_lock
105 static LIST_HEAD(xen_irq_list_head);
107 /* IRQ <-> VIRQ mapping. */
108 static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
110 /* IRQ <-> IPI mapping */
111 static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
115 static unsigned long *pirq_eoi_map;
117 static bool (*pirq_needs_eoi)(unsigned irq);
119 #define EVTCHN_ROW(e) (e / (PAGE_SIZE/sizeof(**evtchn_to_irq)))
120 #define EVTCHN_COL(e) (e % (PAGE_SIZE/sizeof(**evtchn_to_irq)))
121 #define EVTCHN_PER_ROW (PAGE_SIZE / sizeof(**evtchn_to_irq))
123 /* Xen will never allocate port zero for any purpose. */
124 #define VALID_EVTCHN(chn) ((chn) != 0)
126 static struct irq_info *legacy_info_ptrs[NR_IRQS_LEGACY];
128 static struct irq_chip xen_dynamic_chip;
129 static struct irq_chip xen_lateeoi_chip;
130 static struct irq_chip xen_percpu_chip;
131 static struct irq_chip xen_pirq_chip;
132 static void enable_dynirq(struct irq_data *data);
133 static void disable_dynirq(struct irq_data *data);
135 static DEFINE_PER_CPU(unsigned int, irq_epoch);
137 static void clear_evtchn_to_irq_row(int *evtchn_row)
141 for (col = 0; col < EVTCHN_PER_ROW; col++)
142 WRITE_ONCE(evtchn_row[col], -1);
145 static void clear_evtchn_to_irq_all(void)
149 for (row = 0; row < EVTCHN_ROW(xen_evtchn_max_channels()); row++) {
150 if (evtchn_to_irq[row] == NULL)
152 clear_evtchn_to_irq_row(evtchn_to_irq[row]);
156 static int set_evtchn_to_irq(unsigned evtchn, unsigned irq)
162 if (evtchn >= xen_evtchn_max_channels())
165 row = EVTCHN_ROW(evtchn);
166 col = EVTCHN_COL(evtchn);
168 if (evtchn_to_irq[row] == NULL) {
169 /* Unallocated irq entries return -1 anyway */
173 evtchn_row = (int *) __get_free_pages(GFP_KERNEL, 0);
174 if (evtchn_row == NULL)
177 clear_evtchn_to_irq_row(evtchn_row);
180 * We've prepared an empty row for the mapping. If a different
181 * thread was faster inserting it, we can drop ours.
183 if (cmpxchg(&evtchn_to_irq[row], NULL, evtchn_row) != NULL)
184 free_page((unsigned long) evtchn_row);
187 WRITE_ONCE(evtchn_to_irq[row][col], irq);
191 int get_evtchn_to_irq(unsigned evtchn)
193 if (evtchn >= xen_evtchn_max_channels())
195 if (evtchn_to_irq[EVTCHN_ROW(evtchn)] == NULL)
197 return READ_ONCE(evtchn_to_irq[EVTCHN_ROW(evtchn)][EVTCHN_COL(evtchn)]);
200 /* Get info for IRQ */
201 struct irq_info *info_for_irq(unsigned irq)
203 if (irq < nr_legacy_irqs())
204 return legacy_info_ptrs[irq];
206 return irq_get_chip_data(irq);
209 static void set_info_for_irq(unsigned int irq, struct irq_info *info)
211 if (irq < nr_legacy_irqs())
212 legacy_info_ptrs[irq] = info;
214 irq_set_chip_data(irq, info);
217 /* Constructors for packed IRQ information. */
218 static int xen_irq_info_common_setup(struct irq_info *info,
220 enum xen_irq_type type,
226 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
230 info->evtchn = evtchn;
232 info->mask_reason = EVT_MASK_REASON_EXPLICIT;
233 raw_spin_lock_init(&info->lock);
235 ret = set_evtchn_to_irq(evtchn, irq);
239 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
241 return xen_evtchn_port_setup(info);
244 static int xen_irq_info_evtchn_setup(unsigned irq,
247 struct irq_info *info = info_for_irq(irq);
249 return xen_irq_info_common_setup(info, irq, IRQT_EVTCHN, evtchn, 0);
252 static int xen_irq_info_ipi_setup(unsigned cpu,
257 struct irq_info *info = info_for_irq(irq);
261 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
263 return xen_irq_info_common_setup(info, irq, IRQT_IPI, evtchn, 0);
266 static int xen_irq_info_virq_setup(unsigned cpu,
271 struct irq_info *info = info_for_irq(irq);
275 per_cpu(virq_to_irq, cpu)[virq] = irq;
277 return xen_irq_info_common_setup(info, irq, IRQT_VIRQ, evtchn, 0);
280 static int xen_irq_info_pirq_setup(unsigned irq,
287 struct irq_info *info = info_for_irq(irq);
289 info->u.pirq.pirq = pirq;
290 info->u.pirq.gsi = gsi;
291 info->u.pirq.domid = domid;
292 info->u.pirq.flags = flags;
294 return xen_irq_info_common_setup(info, irq, IRQT_PIRQ, evtchn, 0);
297 static void xen_irq_info_cleanup(struct irq_info *info)
299 set_evtchn_to_irq(info->evtchn, -1);
300 xen_evtchn_port_remove(info->evtchn, info->cpu);
305 * Accessors for packed IRQ information.
307 unsigned int evtchn_from_irq(unsigned irq)
309 const struct irq_info *info = NULL;
311 if (likely(irq < nr_irqs))
312 info = info_for_irq(irq);
319 unsigned irq_from_evtchn(unsigned int evtchn)
321 return get_evtchn_to_irq(evtchn);
323 EXPORT_SYMBOL_GPL(irq_from_evtchn);
325 int irq_from_virq(unsigned int cpu, unsigned int virq)
327 return per_cpu(virq_to_irq, cpu)[virq];
330 static enum ipi_vector ipi_from_irq(unsigned irq)
332 struct irq_info *info = info_for_irq(irq);
334 BUG_ON(info == NULL);
335 BUG_ON(info->type != IRQT_IPI);
340 static unsigned virq_from_irq(unsigned irq)
342 struct irq_info *info = info_for_irq(irq);
344 BUG_ON(info == NULL);
345 BUG_ON(info->type != IRQT_VIRQ);
350 static unsigned pirq_from_irq(unsigned irq)
352 struct irq_info *info = info_for_irq(irq);
354 BUG_ON(info == NULL);
355 BUG_ON(info->type != IRQT_PIRQ);
357 return info->u.pirq.pirq;
360 static enum xen_irq_type type_from_irq(unsigned irq)
362 return info_for_irq(irq)->type;
365 unsigned cpu_from_irq(unsigned irq)
367 return info_for_irq(irq)->cpu;
370 unsigned int cpu_from_evtchn(unsigned int evtchn)
372 int irq = get_evtchn_to_irq(evtchn);
376 ret = cpu_from_irq(irq);
381 static void do_mask(struct irq_info *info, u8 reason)
385 raw_spin_lock_irqsave(&info->lock, flags);
387 if (!info->mask_reason)
388 mask_evtchn(info->evtchn);
390 info->mask_reason |= reason;
392 raw_spin_unlock_irqrestore(&info->lock, flags);
395 static void do_unmask(struct irq_info *info, u8 reason)
399 raw_spin_lock_irqsave(&info->lock, flags);
401 info->mask_reason &= ~reason;
403 if (!info->mask_reason)
404 unmask_evtchn(info->evtchn);
406 raw_spin_unlock_irqrestore(&info->lock, flags);
410 static bool pirq_check_eoi_map(unsigned irq)
412 return test_bit(pirq_from_irq(irq), pirq_eoi_map);
416 static bool pirq_needs_eoi_flag(unsigned irq)
418 struct irq_info *info = info_for_irq(irq);
419 BUG_ON(info->type != IRQT_PIRQ);
421 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
424 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
426 int irq = get_evtchn_to_irq(chn);
427 struct irq_info *info = info_for_irq(irq);
431 cpumask_copy(irq_get_affinity_mask(irq), cpumask_of(cpu));
433 xen_evtchn_port_bind_to_cpu(info, cpu);
439 * notify_remote_via_irq - send event to remote end of event channel via irq
440 * @irq: irq of event channel to send event to
442 * Unlike notify_remote_via_evtchn(), this is safe to use across
443 * save/restore. Notifications on a broken connection are silently
446 void notify_remote_via_irq(int irq)
448 int evtchn = evtchn_from_irq(irq);
450 if (VALID_EVTCHN(evtchn))
451 notify_remote_via_evtchn(evtchn);
453 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
455 struct lateeoi_work {
456 struct delayed_work delayed;
457 spinlock_t eoi_list_lock;
458 struct list_head eoi_list;
461 static DEFINE_PER_CPU(struct lateeoi_work, lateeoi);
463 static void lateeoi_list_del(struct irq_info *info)
465 struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
468 spin_lock_irqsave(&eoi->eoi_list_lock, flags);
469 list_del_init(&info->eoi_list);
470 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
473 static void lateeoi_list_add(struct irq_info *info)
475 struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
476 struct irq_info *elem;
477 u64 now = get_jiffies_64();
481 if (now < info->eoi_time)
482 delay = info->eoi_time - now;
486 spin_lock_irqsave(&eoi->eoi_list_lock, flags);
488 if (list_empty(&eoi->eoi_list)) {
489 list_add(&info->eoi_list, &eoi->eoi_list);
490 mod_delayed_work_on(info->eoi_cpu, system_wq,
491 &eoi->delayed, delay);
493 list_for_each_entry_reverse(elem, &eoi->eoi_list, eoi_list) {
494 if (elem->eoi_time <= info->eoi_time)
497 list_add(&info->eoi_list, &elem->eoi_list);
500 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
503 static void xen_irq_lateeoi_locked(struct irq_info *info, bool spurious)
505 evtchn_port_t evtchn;
507 unsigned int delay = 0;
509 evtchn = info->evtchn;
510 if (!VALID_EVTCHN(evtchn) || !list_empty(&info->eoi_list))
514 if ((1 << info->spurious_cnt) < (HZ << 2))
515 info->spurious_cnt++;
516 if (info->spurious_cnt > 1) {
517 delay = 1 << (info->spurious_cnt - 2);
521 info->eoi_cpu = smp_processor_id();
522 info->eoi_time = get_jiffies_64() + delay;
525 info->spurious_cnt = 0;
529 if (info->eoi_time &&
530 (info->irq_epoch == per_cpu(irq_epoch, cpu) || delay)) {
531 lateeoi_list_add(info);
537 /* is_active hasn't been reset yet, do it now. */
538 smp_store_release(&info->is_active, 0);
539 do_unmask(info, EVT_MASK_REASON_EOI_PENDING);
542 static void xen_irq_lateeoi_worker(struct work_struct *work)
544 struct lateeoi_work *eoi;
545 struct irq_info *info;
546 u64 now = get_jiffies_64();
549 eoi = container_of(to_delayed_work(work), struct lateeoi_work, delayed);
551 read_lock_irqsave(&evtchn_rwlock, flags);
554 spin_lock(&eoi->eoi_list_lock);
556 info = list_first_entry_or_null(&eoi->eoi_list, struct irq_info,
559 if (info == NULL || now < info->eoi_time) {
560 spin_unlock(&eoi->eoi_list_lock);
564 list_del_init(&info->eoi_list);
566 spin_unlock(&eoi->eoi_list_lock);
570 xen_irq_lateeoi_locked(info, false);
574 mod_delayed_work_on(info->eoi_cpu, system_wq,
575 &eoi->delayed, info->eoi_time - now);
577 read_unlock_irqrestore(&evtchn_rwlock, flags);
580 static void xen_cpu_init_eoi(unsigned int cpu)
582 struct lateeoi_work *eoi = &per_cpu(lateeoi, cpu);
584 INIT_DELAYED_WORK(&eoi->delayed, xen_irq_lateeoi_worker);
585 spin_lock_init(&eoi->eoi_list_lock);
586 INIT_LIST_HEAD(&eoi->eoi_list);
589 void xen_irq_lateeoi(unsigned int irq, unsigned int eoi_flags)
591 struct irq_info *info;
594 read_lock_irqsave(&evtchn_rwlock, flags);
596 info = info_for_irq(irq);
599 xen_irq_lateeoi_locked(info, eoi_flags & XEN_EOI_FLAG_SPURIOUS);
601 read_unlock_irqrestore(&evtchn_rwlock, flags);
603 EXPORT_SYMBOL_GPL(xen_irq_lateeoi);
605 static void xen_irq_init(unsigned irq)
607 struct irq_info *info;
610 /* By default all event channels notify CPU#0. */
611 cpumask_copy(irq_get_affinity_mask(irq), cpumask_of(0));
614 info = kzalloc(sizeof(*info), GFP_KERNEL);
616 panic("Unable to allocate metadata for IRQ%d\n", irq);
618 info->type = IRQT_UNBOUND;
621 set_info_for_irq(irq, info);
623 INIT_LIST_HEAD(&info->eoi_list);
624 list_add_tail(&info->list, &xen_irq_list_head);
627 static int __must_check xen_allocate_irqs_dynamic(int nvec)
629 int i, irq = irq_alloc_descs(-1, 0, nvec, -1);
632 for (i = 0; i < nvec; i++)
633 xen_irq_init(irq + i);
639 static inline int __must_check xen_allocate_irq_dynamic(void)
642 return xen_allocate_irqs_dynamic(1);
645 static int __must_check xen_allocate_irq_gsi(unsigned gsi)
650 * A PV guest has no concept of a GSI (since it has no ACPI
651 * nor access to/knowledge of the physical APICs). Therefore
652 * all IRQs are dynamically allocated from the entire IRQ
655 if (xen_pv_domain() && !xen_initial_domain())
656 return xen_allocate_irq_dynamic();
658 /* Legacy IRQ descriptors are already allocated by the arch. */
659 if (gsi < nr_legacy_irqs())
662 irq = irq_alloc_desc_at(gsi, -1);
669 static void xen_free_irq(unsigned irq)
671 struct irq_info *info = info_for_irq(irq);
677 write_lock_irqsave(&evtchn_rwlock, flags);
679 if (!list_empty(&info->eoi_list))
680 lateeoi_list_del(info);
682 list_del(&info->list);
684 set_info_for_irq(irq, NULL);
686 WARN_ON(info->refcnt > 0);
688 write_unlock_irqrestore(&evtchn_rwlock, flags);
692 /* Legacy IRQ descriptors are managed by the arch. */
693 if (irq < nr_legacy_irqs())
699 static void xen_evtchn_close(unsigned int port)
701 struct evtchn_close close;
704 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
708 static void event_handler_exit(struct irq_info *info)
710 smp_store_release(&info->is_active, 0);
711 clear_evtchn(info->evtchn);
714 static void pirq_query_unmask(int irq)
716 struct physdev_irq_status_query irq_status;
717 struct irq_info *info = info_for_irq(irq);
719 BUG_ON(info->type != IRQT_PIRQ);
721 irq_status.irq = pirq_from_irq(irq);
722 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
723 irq_status.flags = 0;
725 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
726 if (irq_status.flags & XENIRQSTAT_needs_eoi)
727 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
730 static void eoi_pirq(struct irq_data *data)
732 struct irq_info *info = info_for_irq(data->irq);
733 int evtchn = info ? info->evtchn : 0;
734 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
737 if (!VALID_EVTCHN(evtchn))
740 if (unlikely(irqd_is_setaffinity_pending(data)) &&
741 likely(!irqd_irq_disabled(data))) {
742 do_mask(info, EVT_MASK_REASON_TEMPORARY);
744 event_handler_exit(info);
746 irq_move_masked_irq(data);
748 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
750 event_handler_exit(info);
752 if (pirq_needs_eoi(data->irq)) {
753 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
758 static void mask_ack_pirq(struct irq_data *data)
760 disable_dynirq(data);
764 static unsigned int __startup_pirq(unsigned int irq)
766 struct evtchn_bind_pirq bind_pirq;
767 struct irq_info *info = info_for_irq(irq);
768 int evtchn = evtchn_from_irq(irq);
771 BUG_ON(info->type != IRQT_PIRQ);
773 if (VALID_EVTCHN(evtchn))
776 bind_pirq.pirq = pirq_from_irq(irq);
777 /* NB. We are happy to share unless we are probing. */
778 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
779 BIND_PIRQ__WILL_SHARE : 0;
780 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
782 pr_warn("Failed to obtain physical IRQ %d\n", irq);
785 evtchn = bind_pirq.port;
787 pirq_query_unmask(irq);
789 rc = set_evtchn_to_irq(evtchn, irq);
793 info->evtchn = evtchn;
794 bind_evtchn_to_cpu(evtchn, 0);
796 rc = xen_evtchn_port_setup(info);
801 do_unmask(info, EVT_MASK_REASON_EXPLICIT);
803 eoi_pirq(irq_get_irq_data(irq));
808 pr_err("irq%d: Failed to set port to irq mapping (%d)\n", irq, rc);
809 xen_evtchn_close(evtchn);
813 static unsigned int startup_pirq(struct irq_data *data)
815 return __startup_pirq(data->irq);
818 static void shutdown_pirq(struct irq_data *data)
820 unsigned int irq = data->irq;
821 struct irq_info *info = info_for_irq(irq);
822 unsigned evtchn = evtchn_from_irq(irq);
824 BUG_ON(info->type != IRQT_PIRQ);
826 if (!VALID_EVTCHN(evtchn))
829 do_mask(info, EVT_MASK_REASON_EXPLICIT);
830 xen_evtchn_close(evtchn);
831 xen_irq_info_cleanup(info);
834 static void enable_pirq(struct irq_data *data)
839 static void disable_pirq(struct irq_data *data)
841 disable_dynirq(data);
844 int xen_irq_from_gsi(unsigned gsi)
846 struct irq_info *info;
848 list_for_each_entry(info, &xen_irq_list_head, list) {
849 if (info->type != IRQT_PIRQ)
852 if (info->u.pirq.gsi == gsi)
858 EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
860 static void __unbind_from_irq(unsigned int irq)
862 int evtchn = evtchn_from_irq(irq);
863 struct irq_info *info = info_for_irq(irq);
865 if (info->refcnt > 0) {
867 if (info->refcnt != 0)
871 if (VALID_EVTCHN(evtchn)) {
872 unsigned int cpu = cpu_from_irq(irq);
874 xen_evtchn_close(evtchn);
876 switch (type_from_irq(irq)) {
878 per_cpu(virq_to_irq, cpu)[virq_from_irq(irq)] = -1;
881 per_cpu(ipi_to_irq, cpu)[ipi_from_irq(irq)] = -1;
887 xen_irq_info_cleanup(info);
894 * Do not make any assumptions regarding the relationship between the
895 * IRQ number returned here and the Xen pirq argument.
897 * Note: We don't assign an event channel until the irq actually started
898 * up. Return an existing irq if we've already got one for the gsi.
900 * Shareable implies level triggered, not shareable implies edge
903 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
904 unsigned pirq, int shareable, char *name)
907 struct physdev_irq irq_op;
910 mutex_lock(&irq_mapping_update_lock);
912 irq = xen_irq_from_gsi(gsi);
914 pr_info("%s: returning irq %d for gsi %u\n",
919 irq = xen_allocate_irq_gsi(gsi);
926 /* Only the privileged domain can do this. For non-priv, the pcifront
927 * driver provides a PCI bus that does the call to do exactly
928 * this in the priv domain. */
929 if (xen_initial_domain() &&
930 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
936 ret = xen_irq_info_pirq_setup(irq, 0, pirq, gsi, DOMID_SELF,
937 shareable ? PIRQ_SHAREABLE : 0);
939 __unbind_from_irq(irq);
944 pirq_query_unmask(irq);
945 /* We try to use the handler with the appropriate semantic for the
946 * type of interrupt: if the interrupt is an edge triggered
947 * interrupt we use handle_edge_irq.
949 * On the other hand if the interrupt is level triggered we use
950 * handle_fasteoi_irq like the native code does for this kind of
953 * Depending on the Xen version, pirq_needs_eoi might return true
954 * not only for level triggered interrupts but for edge triggered
955 * interrupts too. In any case Xen always honors the eoi mechanism,
956 * not injecting any more pirqs of the same kind if the first one
957 * hasn't received an eoi yet. Therefore using the fasteoi handler
958 * is the right choice either way.
961 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
962 handle_fasteoi_irq, name);
964 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
965 handle_edge_irq, name);
968 mutex_unlock(&irq_mapping_update_lock);
973 #ifdef CONFIG_PCI_MSI
974 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
977 struct physdev_get_free_pirq op_get_free_pirq;
979 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
980 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
982 WARN_ONCE(rc == -ENOSYS,
983 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
985 return rc ? -1 : op_get_free_pirq.pirq;
988 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
989 int pirq, int nvec, const char *name, domid_t domid)
993 mutex_lock(&irq_mapping_update_lock);
995 irq = xen_allocate_irqs_dynamic(nvec);
999 for (i = 0; i < nvec; i++) {
1000 irq_set_chip_and_handler_name(irq + i, &xen_pirq_chip, handle_edge_irq, name);
1002 ret = xen_irq_info_pirq_setup(irq + i, 0, pirq + i, 0, domid,
1003 i == 0 ? 0 : PIRQ_MSI_GROUP);
1008 ret = irq_set_msi_desc(irq, msidesc);
1012 mutex_unlock(&irq_mapping_update_lock);
1016 __unbind_from_irq(irq + nvec);
1017 mutex_unlock(&irq_mapping_update_lock);
1022 int xen_destroy_irq(int irq)
1024 struct physdev_unmap_pirq unmap_irq;
1025 struct irq_info *info = info_for_irq(irq);
1028 mutex_lock(&irq_mapping_update_lock);
1031 * If trying to remove a vector in a MSI group different
1032 * than the first one skip the PIRQ unmap unless this vector
1033 * is the first one in the group.
1035 if (xen_initial_domain() && !(info->u.pirq.flags & PIRQ_MSI_GROUP)) {
1036 unmap_irq.pirq = info->u.pirq.pirq;
1037 unmap_irq.domid = info->u.pirq.domid;
1038 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
1039 /* If another domain quits without making the pci_disable_msix
1040 * call, the Xen hypervisor takes care of freeing the PIRQs
1041 * (free_domain_pirqs).
1043 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
1044 pr_info("domain %d does not have %d anymore\n",
1045 info->u.pirq.domid, info->u.pirq.pirq);
1047 pr_warn("unmap irq failed %d\n", rc);
1055 mutex_unlock(&irq_mapping_update_lock);
1059 int xen_irq_from_pirq(unsigned pirq)
1063 struct irq_info *info;
1065 mutex_lock(&irq_mapping_update_lock);
1067 list_for_each_entry(info, &xen_irq_list_head, list) {
1068 if (info->type != IRQT_PIRQ)
1071 if (info->u.pirq.pirq == pirq)
1076 mutex_unlock(&irq_mapping_update_lock);
1082 int xen_pirq_from_irq(unsigned irq)
1084 return pirq_from_irq(irq);
1086 EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
1088 static int bind_evtchn_to_irq_chip(evtchn_port_t evtchn, struct irq_chip *chip)
1093 if (evtchn >= xen_evtchn_max_channels())
1096 mutex_lock(&irq_mapping_update_lock);
1098 irq = get_evtchn_to_irq(evtchn);
1101 irq = xen_allocate_irq_dynamic();
1105 irq_set_chip_and_handler_name(irq, chip,
1106 handle_edge_irq, "event");
1108 ret = xen_irq_info_evtchn_setup(irq, evtchn);
1110 __unbind_from_irq(irq);
1114 /* New interdomain events are bound to VCPU 0. */
1115 bind_evtchn_to_cpu(evtchn, 0);
1117 struct irq_info *info = info_for_irq(irq);
1118 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
1122 mutex_unlock(&irq_mapping_update_lock);
1127 int bind_evtchn_to_irq(evtchn_port_t evtchn)
1129 return bind_evtchn_to_irq_chip(evtchn, &xen_dynamic_chip);
1131 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
1133 int bind_evtchn_to_irq_lateeoi(evtchn_port_t evtchn)
1135 return bind_evtchn_to_irq_chip(evtchn, &xen_lateeoi_chip);
1137 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq_lateeoi);
1139 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
1141 struct evtchn_bind_ipi bind_ipi;
1145 mutex_lock(&irq_mapping_update_lock);
1147 irq = per_cpu(ipi_to_irq, cpu)[ipi];
1150 irq = xen_allocate_irq_dynamic();
1154 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
1155 handle_percpu_irq, "ipi");
1157 bind_ipi.vcpu = xen_vcpu_nr(cpu);
1158 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1161 evtchn = bind_ipi.port;
1163 ret = xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
1165 __unbind_from_irq(irq);
1169 bind_evtchn_to_cpu(evtchn, cpu);
1171 struct irq_info *info = info_for_irq(irq);
1172 WARN_ON(info == NULL || info->type != IRQT_IPI);
1176 mutex_unlock(&irq_mapping_update_lock);
1180 static int bind_interdomain_evtchn_to_irq_chip(unsigned int remote_domain,
1181 evtchn_port_t remote_port,
1182 struct irq_chip *chip)
1184 struct evtchn_bind_interdomain bind_interdomain;
1187 bind_interdomain.remote_dom = remote_domain;
1188 bind_interdomain.remote_port = remote_port;
1190 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
1193 return err ? : bind_evtchn_to_irq_chip(bind_interdomain.local_port,
1197 int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
1198 evtchn_port_t remote_port)
1200 return bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1203 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irq);
1205 int bind_interdomain_evtchn_to_irq_lateeoi(unsigned int remote_domain,
1206 evtchn_port_t remote_port)
1208 return bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1211 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irq_lateeoi);
1213 static int find_virq(unsigned int virq, unsigned int cpu)
1215 struct evtchn_status status;
1216 int port, rc = -ENOENT;
1218 memset(&status, 0, sizeof(status));
1219 for (port = 0; port < xen_evtchn_max_channels(); port++) {
1220 status.dom = DOMID_SELF;
1222 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
1225 if (status.status != EVTCHNSTAT_virq)
1227 if (status.u.virq == virq && status.vcpu == xen_vcpu_nr(cpu)) {
1236 * xen_evtchn_nr_channels - number of usable event channel ports
1238 * This may be less than the maximum supported by the current
1239 * hypervisor ABI. Use xen_evtchn_max_channels() for the maximum
1242 unsigned xen_evtchn_nr_channels(void)
1244 return evtchn_ops->nr_channels();
1246 EXPORT_SYMBOL_GPL(xen_evtchn_nr_channels);
1248 int bind_virq_to_irq(unsigned int virq, unsigned int cpu, bool percpu)
1250 struct evtchn_bind_virq bind_virq;
1251 int evtchn, irq, ret;
1253 mutex_lock(&irq_mapping_update_lock);
1255 irq = per_cpu(virq_to_irq, cpu)[virq];
1258 irq = xen_allocate_irq_dynamic();
1263 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
1264 handle_percpu_irq, "virq");
1266 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
1267 handle_edge_irq, "virq");
1269 bind_virq.virq = virq;
1270 bind_virq.vcpu = xen_vcpu_nr(cpu);
1271 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1274 evtchn = bind_virq.port;
1277 ret = find_virq(virq, cpu);
1282 ret = xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1284 __unbind_from_irq(irq);
1289 bind_evtchn_to_cpu(evtchn, cpu);
1291 struct irq_info *info = info_for_irq(irq);
1292 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
1296 mutex_unlock(&irq_mapping_update_lock);
1301 static void unbind_from_irq(unsigned int irq)
1303 mutex_lock(&irq_mapping_update_lock);
1304 __unbind_from_irq(irq);
1305 mutex_unlock(&irq_mapping_update_lock);
1308 static int bind_evtchn_to_irqhandler_chip(evtchn_port_t evtchn,
1309 irq_handler_t handler,
1310 unsigned long irqflags,
1311 const char *devname, void *dev_id,
1312 struct irq_chip *chip)
1316 irq = bind_evtchn_to_irq_chip(evtchn, chip);
1319 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1321 unbind_from_irq(irq);
1328 int bind_evtchn_to_irqhandler(evtchn_port_t evtchn,
1329 irq_handler_t handler,
1330 unsigned long irqflags,
1331 const char *devname, void *dev_id)
1333 return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1337 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1339 int bind_evtchn_to_irqhandler_lateeoi(evtchn_port_t evtchn,
1340 irq_handler_t handler,
1341 unsigned long irqflags,
1342 const char *devname, void *dev_id)
1344 return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1348 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler_lateeoi);
1350 static int bind_interdomain_evtchn_to_irqhandler_chip(
1351 unsigned int remote_domain, evtchn_port_t remote_port,
1352 irq_handler_t handler, unsigned long irqflags,
1353 const char *devname, void *dev_id, struct irq_chip *chip)
1357 irq = bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1362 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1364 unbind_from_irq(irq);
1371 int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1372 evtchn_port_t remote_port,
1373 irq_handler_t handler,
1374 unsigned long irqflags,
1375 const char *devname,
1378 return bind_interdomain_evtchn_to_irqhandler_chip(remote_domain,
1379 remote_port, handler, irqflags, devname,
1380 dev_id, &xen_dynamic_chip);
1382 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1384 int bind_interdomain_evtchn_to_irqhandler_lateeoi(unsigned int remote_domain,
1385 evtchn_port_t remote_port,
1386 irq_handler_t handler,
1387 unsigned long irqflags,
1388 const char *devname,
1391 return bind_interdomain_evtchn_to_irqhandler_chip(remote_domain,
1392 remote_port, handler, irqflags, devname,
1393 dev_id, &xen_lateeoi_chip);
1395 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler_lateeoi);
1397 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
1398 irq_handler_t handler,
1399 unsigned long irqflags, const char *devname, void *dev_id)
1403 irq = bind_virq_to_irq(virq, cpu, irqflags & IRQF_PERCPU);
1406 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1408 unbind_from_irq(irq);
1414 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1416 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1418 irq_handler_t handler,
1419 unsigned long irqflags,
1420 const char *devname,
1425 irq = bind_ipi_to_irq(ipi, cpu);
1429 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
1430 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1432 unbind_from_irq(irq);
1439 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1441 struct irq_info *info = info_for_irq(irq);
1445 free_irq(irq, dev_id);
1446 unbind_from_irq(irq);
1448 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1451 * xen_set_irq_priority() - set an event channel priority.
1452 * @irq:irq bound to an event channel.
1453 * @priority: priority between XEN_IRQ_PRIORITY_MAX and XEN_IRQ_PRIORITY_MIN.
1455 int xen_set_irq_priority(unsigned irq, unsigned priority)
1457 struct evtchn_set_priority set_priority;
1459 set_priority.port = evtchn_from_irq(irq);
1460 set_priority.priority = priority;
1462 return HYPERVISOR_event_channel_op(EVTCHNOP_set_priority,
1465 EXPORT_SYMBOL_GPL(xen_set_irq_priority);
1467 int evtchn_make_refcounted(unsigned int evtchn)
1469 int irq = get_evtchn_to_irq(evtchn);
1470 struct irq_info *info;
1475 info = info_for_irq(irq);
1480 WARN_ON(info->refcnt != -1);
1486 EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1488 int evtchn_get(unsigned int evtchn)
1491 struct irq_info *info;
1494 if (evtchn >= xen_evtchn_max_channels())
1497 mutex_lock(&irq_mapping_update_lock);
1499 irq = get_evtchn_to_irq(evtchn);
1503 info = info_for_irq(irq);
1509 if (info->refcnt <= 0 || info->refcnt == SHRT_MAX)
1515 mutex_unlock(&irq_mapping_update_lock);
1519 EXPORT_SYMBOL_GPL(evtchn_get);
1521 void evtchn_put(unsigned int evtchn)
1523 int irq = get_evtchn_to_irq(evtchn);
1524 if (WARN_ON(irq == -1))
1526 unbind_from_irq(irq);
1528 EXPORT_SYMBOL_GPL(evtchn_put);
1530 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1535 if (unlikely(vector == XEN_NMI_VECTOR)) {
1536 int rc = HYPERVISOR_vcpu_op(VCPUOP_send_nmi, xen_vcpu_nr(cpu),
1539 printk(KERN_WARNING "Sending nmi to CPU%d failed (rc:%d)\n", cpu, rc);
1543 irq = per_cpu(ipi_to_irq, cpu)[vector];
1545 notify_remote_via_irq(irq);
1548 struct evtchn_loop_ctrl {
1554 void handle_irq_for_port(evtchn_port_t port, struct evtchn_loop_ctrl *ctrl)
1557 struct irq_info *info;
1559 irq = get_evtchn_to_irq(port);
1564 * Check for timeout every 256 events.
1565 * We are setting the timeout value only after the first 256
1566 * events in order to not hurt the common case of few loop
1567 * iterations. The 256 is basically an arbitrary value.
1569 * In case we are hitting the timeout we need to defer all further
1570 * EOIs in order to ensure to leave the event handling loop rather
1571 * sooner than later.
1573 if (!ctrl->defer_eoi && !(++ctrl->count & 0xff)) {
1574 ktime_t kt = ktime_get();
1576 if (!ctrl->timeout) {
1577 kt = ktime_add_ms(kt,
1578 jiffies_to_msecs(event_loop_timeout));
1580 } else if (kt > ctrl->timeout) {
1581 ctrl->defer_eoi = true;
1585 info = info_for_irq(irq);
1586 if (xchg_acquire(&info->is_active, 1))
1589 if (ctrl->defer_eoi) {
1590 info->eoi_cpu = smp_processor_id();
1591 info->irq_epoch = __this_cpu_read(irq_epoch);
1592 info->eoi_time = get_jiffies_64() + event_eoi_delay;
1595 generic_handle_irq(irq);
1598 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1600 static void __xen_evtchn_do_upcall(void)
1602 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1603 int cpu = get_cpu();
1605 struct evtchn_loop_ctrl ctrl = { 0 };
1607 read_lock(&evtchn_rwlock);
1610 vcpu_info->evtchn_upcall_pending = 0;
1612 if (__this_cpu_inc_return(xed_nesting_count) - 1)
1615 xen_evtchn_handle_events(cpu, &ctrl);
1617 BUG_ON(!irqs_disabled());
1619 count = __this_cpu_read(xed_nesting_count);
1620 __this_cpu_write(xed_nesting_count, 0);
1621 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1624 read_unlock(&evtchn_rwlock);
1627 * Increment irq_epoch only now to defer EOIs only for
1628 * xen_irq_lateeoi() invocations occurring from inside the loop
1631 __this_cpu_inc(irq_epoch);
1636 void xen_evtchn_do_upcall(struct pt_regs *regs)
1638 struct pt_regs *old_regs = set_irq_regs(regs);
1642 inc_irq_stat(irq_hv_callback_count);
1645 __xen_evtchn_do_upcall();
1648 set_irq_regs(old_regs);
1651 void xen_hvm_evtchn_do_upcall(void)
1653 __xen_evtchn_do_upcall();
1655 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1657 /* Rebind a new event channel to an existing irq. */
1658 void rebind_evtchn_irq(int evtchn, int irq)
1660 struct irq_info *info = info_for_irq(irq);
1665 /* Make sure the irq is masked, since the new event channel
1666 will also be masked. */
1669 mutex_lock(&irq_mapping_update_lock);
1671 /* After resume the irq<->evtchn mappings are all cleared out */
1672 BUG_ON(get_evtchn_to_irq(evtchn) != -1);
1673 /* Expect irq to have been bound before,
1674 so there should be a proper type */
1675 BUG_ON(info->type == IRQT_UNBOUND);
1677 (void)xen_irq_info_evtchn_setup(irq, evtchn);
1679 mutex_unlock(&irq_mapping_update_lock);
1681 bind_evtchn_to_cpu(evtchn, info->cpu);
1682 /* This will be deferred until interrupt is processed */
1683 irq_set_affinity(irq, cpumask_of(info->cpu));
1685 /* Unmask the event channel. */
1689 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1690 static int xen_rebind_evtchn_to_cpu(struct irq_info *info, unsigned int tcpu)
1692 struct evtchn_bind_vcpu bind_vcpu;
1693 evtchn_port_t evtchn = info ? info->evtchn : 0;
1695 if (!VALID_EVTCHN(evtchn))
1698 if (!xen_support_evtchn_rebind())
1701 /* Send future instances of this interrupt to other vcpu. */
1702 bind_vcpu.port = evtchn;
1703 bind_vcpu.vcpu = xen_vcpu_nr(tcpu);
1706 * Mask the event while changing the VCPU binding to prevent
1707 * it being delivered on an unexpected VCPU.
1709 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1712 * If this fails, it usually just indicates that we're dealing with a
1713 * virq or IPI channel, which don't actually need to be rebound. Ignore
1714 * it, but don't do the xenlinux-level rebind in that case.
1716 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1717 bind_evtchn_to_cpu(evtchn, tcpu);
1719 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1724 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1727 unsigned tcpu = cpumask_first_and(dest, cpu_online_mask);
1728 int ret = xen_rebind_evtchn_to_cpu(info_for_irq(data->irq), tcpu);
1731 irq_data_update_effective_affinity(data, cpumask_of(tcpu));
1736 /* To be called with desc->lock held. */
1737 int xen_set_affinity_evtchn(struct irq_desc *desc, unsigned int tcpu)
1739 struct irq_data *d = irq_desc_get_irq_data(desc);
1741 return set_affinity_irq(d, cpumask_of(tcpu), false);
1743 EXPORT_SYMBOL_GPL(xen_set_affinity_evtchn);
1745 static void enable_dynirq(struct irq_data *data)
1747 struct irq_info *info = info_for_irq(data->irq);
1748 evtchn_port_t evtchn = info ? info->evtchn : 0;
1750 if (VALID_EVTCHN(evtchn))
1751 do_unmask(info, EVT_MASK_REASON_EXPLICIT);
1754 static void disable_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_mask(info, EVT_MASK_REASON_EXPLICIT);
1763 static void ack_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))
1771 if (unlikely(irqd_is_setaffinity_pending(data)) &&
1772 likely(!irqd_irq_disabled(data))) {
1773 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1775 event_handler_exit(info);
1777 irq_move_masked_irq(data);
1779 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1781 event_handler_exit(info);
1784 static void mask_ack_dynirq(struct irq_data *data)
1786 disable_dynirq(data);
1790 static void lateeoi_ack_dynirq(struct irq_data *data)
1792 struct irq_info *info = info_for_irq(data->irq);
1793 evtchn_port_t evtchn = info ? info->evtchn : 0;
1795 if (!VALID_EVTCHN(evtchn))
1798 do_mask(info, EVT_MASK_REASON_EOI_PENDING);
1800 if (unlikely(irqd_is_setaffinity_pending(data)) &&
1801 likely(!irqd_irq_disabled(data))) {
1802 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1804 clear_evtchn(evtchn);
1806 irq_move_masked_irq(data);
1808 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1810 clear_evtchn(evtchn);
1813 static void lateeoi_mask_ack_dynirq(struct irq_data *data)
1815 struct irq_info *info = info_for_irq(data->irq);
1816 evtchn_port_t evtchn = info ? info->evtchn : 0;
1818 if (VALID_EVTCHN(evtchn)) {
1819 do_mask(info, EVT_MASK_REASON_EXPLICIT);
1824 static int retrigger_dynirq(struct irq_data *data)
1826 struct irq_info *info = info_for_irq(data->irq);
1827 evtchn_port_t evtchn = info ? info->evtchn : 0;
1829 if (!VALID_EVTCHN(evtchn))
1832 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1834 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1839 static void restore_pirqs(void)
1841 int pirq, rc, irq, gsi;
1842 struct physdev_map_pirq map_irq;
1843 struct irq_info *info;
1845 list_for_each_entry(info, &xen_irq_list_head, list) {
1846 if (info->type != IRQT_PIRQ)
1849 pirq = info->u.pirq.pirq;
1850 gsi = info->u.pirq.gsi;
1853 /* save/restore of PT devices doesn't work, so at this point the
1854 * only devices present are GSI based emulated devices */
1858 map_irq.domid = DOMID_SELF;
1859 map_irq.type = MAP_PIRQ_TYPE_GSI;
1860 map_irq.index = gsi;
1861 map_irq.pirq = pirq;
1863 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1865 pr_warn("xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1866 gsi, irq, pirq, rc);
1871 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1873 __startup_pirq(irq);
1877 static void restore_cpu_virqs(unsigned int cpu)
1879 struct evtchn_bind_virq bind_virq;
1880 int virq, irq, evtchn;
1882 for (virq = 0; virq < NR_VIRQS; virq++) {
1883 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1886 BUG_ON(virq_from_irq(irq) != virq);
1888 /* Get a new binding from Xen. */
1889 bind_virq.virq = virq;
1890 bind_virq.vcpu = xen_vcpu_nr(cpu);
1891 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1894 evtchn = bind_virq.port;
1896 /* Record the new mapping. */
1897 (void)xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1898 bind_evtchn_to_cpu(evtchn, cpu);
1902 static void restore_cpu_ipis(unsigned int cpu)
1904 struct evtchn_bind_ipi bind_ipi;
1905 int ipi, irq, evtchn;
1907 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1908 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1911 BUG_ON(ipi_from_irq(irq) != ipi);
1913 /* Get a new binding from Xen. */
1914 bind_ipi.vcpu = xen_vcpu_nr(cpu);
1915 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1918 evtchn = bind_ipi.port;
1920 /* Record the new mapping. */
1921 (void)xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
1922 bind_evtchn_to_cpu(evtchn, cpu);
1926 /* Clear an irq's pending state, in preparation for polling on it */
1927 void xen_clear_irq_pending(int irq)
1929 struct irq_info *info = info_for_irq(irq);
1930 evtchn_port_t evtchn = info ? info->evtchn : 0;
1932 if (VALID_EVTCHN(evtchn))
1933 event_handler_exit(info);
1935 EXPORT_SYMBOL(xen_clear_irq_pending);
1936 void xen_set_irq_pending(int irq)
1938 int evtchn = evtchn_from_irq(irq);
1940 if (VALID_EVTCHN(evtchn))
1944 bool xen_test_irq_pending(int irq)
1946 int evtchn = evtchn_from_irq(irq);
1949 if (VALID_EVTCHN(evtchn))
1950 ret = test_evtchn(evtchn);
1955 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1956 * the irq will be disabled so it won't deliver an interrupt. */
1957 void xen_poll_irq_timeout(int irq, u64 timeout)
1959 evtchn_port_t evtchn = evtchn_from_irq(irq);
1961 if (VALID_EVTCHN(evtchn)) {
1962 struct sched_poll poll;
1965 poll.timeout = timeout;
1966 set_xen_guest_handle(poll.ports, &evtchn);
1968 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1972 EXPORT_SYMBOL(xen_poll_irq_timeout);
1973 /* Poll waiting for an irq to become pending. In the usual case, the
1974 * irq will be disabled so it won't deliver an interrupt. */
1975 void xen_poll_irq(int irq)
1977 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1980 /* Check whether the IRQ line is shared with other guests. */
1981 int xen_test_irq_shared(int irq)
1983 struct irq_info *info = info_for_irq(irq);
1984 struct physdev_irq_status_query irq_status;
1989 irq_status.irq = info->u.pirq.pirq;
1991 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1993 return !(irq_status.flags & XENIRQSTAT_shared);
1995 EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1997 void xen_irq_resume(void)
2000 struct irq_info *info;
2002 /* New event-channel space is not 'live' yet. */
2003 xen_evtchn_resume();
2005 /* No IRQ <-> event-channel mappings. */
2006 list_for_each_entry(info, &xen_irq_list_head, list)
2007 info->evtchn = 0; /* zap event-channel binding */
2009 clear_evtchn_to_irq_all();
2011 for_each_possible_cpu(cpu) {
2012 restore_cpu_virqs(cpu);
2013 restore_cpu_ipis(cpu);
2019 static struct irq_chip xen_dynamic_chip __read_mostly = {
2022 .irq_disable = disable_dynirq,
2023 .irq_mask = disable_dynirq,
2024 .irq_unmask = enable_dynirq,
2026 .irq_ack = ack_dynirq,
2027 .irq_mask_ack = mask_ack_dynirq,
2029 .irq_set_affinity = set_affinity_irq,
2030 .irq_retrigger = retrigger_dynirq,
2033 static struct irq_chip xen_lateeoi_chip __read_mostly = {
2034 /* The chip name needs to contain "xen-dyn" for irqbalance to work. */
2035 .name = "xen-dyn-lateeoi",
2037 .irq_disable = disable_dynirq,
2038 .irq_mask = disable_dynirq,
2039 .irq_unmask = enable_dynirq,
2041 .irq_ack = lateeoi_ack_dynirq,
2042 .irq_mask_ack = lateeoi_mask_ack_dynirq,
2044 .irq_set_affinity = set_affinity_irq,
2045 .irq_retrigger = retrigger_dynirq,
2048 static struct irq_chip xen_pirq_chip __read_mostly = {
2051 .irq_startup = startup_pirq,
2052 .irq_shutdown = shutdown_pirq,
2053 .irq_enable = enable_pirq,
2054 .irq_disable = disable_pirq,
2056 .irq_mask = disable_dynirq,
2057 .irq_unmask = enable_dynirq,
2059 .irq_ack = eoi_pirq,
2060 .irq_eoi = eoi_pirq,
2061 .irq_mask_ack = mask_ack_pirq,
2063 .irq_set_affinity = set_affinity_irq,
2065 .irq_retrigger = retrigger_dynirq,
2068 static struct irq_chip xen_percpu_chip __read_mostly = {
2069 .name = "xen-percpu",
2071 .irq_disable = disable_dynirq,
2072 .irq_mask = disable_dynirq,
2073 .irq_unmask = enable_dynirq,
2075 .irq_ack = ack_dynirq,
2078 #ifdef CONFIG_XEN_PVHVM
2079 /* Vector callbacks are better than PCI interrupts to receive event
2080 * channel notifications because we can receive vector callbacks on any
2081 * vcpu and we don't need PCI support or APIC interactions. */
2082 void xen_callback_vector(void)
2085 uint64_t callback_via;
2087 if (xen_have_vector_callback) {
2088 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
2089 rc = xen_set_callback_via(callback_via);
2091 pr_err("Request for Xen HVM callback vector failed\n");
2092 xen_have_vector_callback = 0;
2095 pr_info_once("Xen HVM callback vector for event delivery is enabled\n");
2096 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
2097 xen_hvm_callback_vector);
2101 void xen_callback_vector(void) {}
2104 static bool fifo_events = true;
2105 module_param(fifo_events, bool, 0);
2107 static int xen_evtchn_cpu_prepare(unsigned int cpu)
2111 xen_cpu_init_eoi(cpu);
2113 if (evtchn_ops->percpu_init)
2114 ret = evtchn_ops->percpu_init(cpu);
2119 static int xen_evtchn_cpu_dead(unsigned int cpu)
2123 if (evtchn_ops->percpu_deinit)
2124 ret = evtchn_ops->percpu_deinit(cpu);
2129 void __init xen_init_IRQ(void)
2132 unsigned int evtchn;
2135 ret = xen_evtchn_fifo_init();
2137 xen_evtchn_2l_init();
2139 xen_cpu_init_eoi(smp_processor_id());
2141 cpuhp_setup_state_nocalls(CPUHP_XEN_EVTCHN_PREPARE,
2142 "xen/evtchn:prepare",
2143 xen_evtchn_cpu_prepare, xen_evtchn_cpu_dead);
2145 evtchn_to_irq = kcalloc(EVTCHN_ROW(xen_evtchn_max_channels()),
2146 sizeof(*evtchn_to_irq), GFP_KERNEL);
2147 BUG_ON(!evtchn_to_irq);
2149 /* No event channels are 'live' right now. */
2150 for (evtchn = 0; evtchn < xen_evtchn_nr_channels(); evtchn++)
2151 mask_evtchn(evtchn);
2153 pirq_needs_eoi = pirq_needs_eoi_flag;
2156 if (xen_pv_domain()) {
2157 if (xen_initial_domain())
2158 pci_xen_initial_domain();
2160 if (xen_feature(XENFEAT_hvm_callback_vector))
2161 xen_callback_vector();
2163 if (xen_hvm_domain()) {
2165 /* pci_xen_hvm_init must be called after native_init_IRQ so that
2166 * __acpi_register_gsi can point at the right function */
2170 struct physdev_pirq_eoi_gmfn eoi_gmfn;
2172 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
2173 eoi_gmfn.gmfn = virt_to_gfn(pirq_eoi_map);
2174 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
2176 free_page((unsigned long) pirq_eoi_map);
2177 pirq_eoi_map = NULL;
2179 pirq_needs_eoi = pirq_check_eoi_map;