4 * Xen models interrupts with abstract event channels. Because each
5 * domain gets 1024 event channels, but NR_IRQ is not that large, we
6 * must dynamically map irqs<->event channels. The event channels
7 * interface with the rest of the kernel by defining a xen interrupt
8 * chip. When an event is received, it is mapped to an irq and sent
9 * through the normal interrupt processing path.
11 * There are four kinds of events which can be mapped to an event
14 * 1. Inter-domain notifications. This includes all the virtual
15 * device events, since they're driven by front-ends in another domain
17 * 2. VIRQs, typically used for timers. These are per-cpu events.
19 * 4. PIRQs - Hardware interrupts.
21 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
24 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
26 #include <linux/linkage.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29 #include <linux/moduleparam.h>
30 #include <linux/string.h>
31 #include <linux/bootmem.h>
32 #include <linux/slab.h>
33 #include <linux/irqnr.h>
34 #include <linux/pci.h>
35 #include <linux/spinlock.h>
36 #include <linux/cpuhotplug.h>
37 #include <linux/atomic.h>
38 #include <linux/ktime.h>
42 #include <asm/ptrace.h>
44 #include <asm/io_apic.h>
45 #include <asm/i8259.h>
46 #include <asm/xen/pci.h>
48 #include <asm/sync_bitops.h>
49 #include <asm/xen/hypercall.h>
50 #include <asm/xen/hypervisor.h>
55 #include <xen/xen-ops.h>
56 #include <xen/events.h>
57 #include <xen/interface/xen.h>
58 #include <xen/interface/event_channel.h>
59 #include <xen/interface/hvm/hvm_op.h>
60 #include <xen/interface/hvm/params.h>
61 #include <xen/interface/physdev.h>
62 #include <xen/interface/sched.h>
63 #include <xen/interface/vcpu.h>
64 #include <asm/hw_irq.h>
66 #include "events_internal.h"
68 #undef MODULE_PARAM_PREFIX
69 #define MODULE_PARAM_PREFIX "xen."
71 static uint __read_mostly event_loop_timeout = 2;
72 module_param(event_loop_timeout, uint, 0644);
74 static uint __read_mostly event_eoi_delay = 10;
75 module_param(event_eoi_delay, uint, 0644);
77 const struct evtchn_ops *evtchn_ops;
80 * This lock protects updates to the following mapping and reference-count
81 * arrays. The lock does not need to be acquired to read the mapping tables.
83 static DEFINE_MUTEX(irq_mapping_update_lock);
86 * Lock protecting event handling loop against removing event channels.
87 * Adding of event channels is no issue as the associated IRQ becomes active
88 * only after everything is setup (before request_[threaded_]irq() the handler
89 * can't be entered for an event, as the event channel will be unmasked only
92 static DEFINE_RWLOCK(evtchn_rwlock);
97 * irq_mapping_update_lock
100 * percpu eoi_list_lock
104 static LIST_HEAD(xen_irq_list_head);
106 /* IRQ <-> VIRQ mapping. */
107 static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
109 /* IRQ <-> IPI mapping */
110 static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
114 static unsigned long *pirq_eoi_map;
116 static bool (*pirq_needs_eoi)(unsigned irq);
118 #define EVTCHN_ROW(e) (e / (PAGE_SIZE/sizeof(**evtchn_to_irq)))
119 #define EVTCHN_COL(e) (e % (PAGE_SIZE/sizeof(**evtchn_to_irq)))
120 #define EVTCHN_PER_ROW (PAGE_SIZE / sizeof(**evtchn_to_irq))
122 /* Xen will never allocate port zero for any purpose. */
123 #define VALID_EVTCHN(chn) ((chn) != 0)
125 static struct irq_info *legacy_info_ptrs[NR_IRQS_LEGACY];
127 static struct irq_chip xen_dynamic_chip;
128 static struct irq_chip xen_lateeoi_chip;
129 static struct irq_chip xen_percpu_chip;
130 static struct irq_chip xen_pirq_chip;
131 static void enable_dynirq(struct irq_data *data);
132 static void disable_dynirq(struct irq_data *data);
134 static DEFINE_PER_CPU(unsigned int, irq_epoch);
136 static void clear_evtchn_to_irq_row(int *evtchn_row)
140 for (col = 0; col < EVTCHN_PER_ROW; col++)
141 WRITE_ONCE(evtchn_row[col], -1);
144 static void clear_evtchn_to_irq_all(void)
148 for (row = 0; row < EVTCHN_ROW(xen_evtchn_max_channels()); row++) {
149 if (evtchn_to_irq[row] == NULL)
151 clear_evtchn_to_irq_row(evtchn_to_irq[row]);
155 static int set_evtchn_to_irq(unsigned evtchn, unsigned irq)
161 if (evtchn >= xen_evtchn_max_channels())
164 row = EVTCHN_ROW(evtchn);
165 col = EVTCHN_COL(evtchn);
167 if (evtchn_to_irq[row] == NULL) {
168 /* Unallocated irq entries return -1 anyway */
172 evtchn_row = (int *) __get_free_pages(GFP_KERNEL, 0);
173 if (evtchn_row == NULL)
176 clear_evtchn_to_irq_row(evtchn_row);
179 * We've prepared an empty row for the mapping. If a different
180 * thread was faster inserting it, we can drop ours.
182 if (cmpxchg(&evtchn_to_irq[row], NULL, evtchn_row) != NULL)
183 free_page((unsigned long) evtchn_row);
186 WRITE_ONCE(evtchn_to_irq[row][col], irq);
190 int get_evtchn_to_irq(unsigned evtchn)
192 if (evtchn >= xen_evtchn_max_channels())
194 if (evtchn_to_irq[EVTCHN_ROW(evtchn)] == NULL)
196 return READ_ONCE(evtchn_to_irq[EVTCHN_ROW(evtchn)][EVTCHN_COL(evtchn)]);
199 /* Get info for IRQ */
200 struct irq_info *info_for_irq(unsigned irq)
202 if (irq < nr_legacy_irqs())
203 return legacy_info_ptrs[irq];
205 return irq_get_chip_data(irq);
208 static void set_info_for_irq(unsigned int irq, struct irq_info *info)
210 if (irq < nr_legacy_irqs())
211 legacy_info_ptrs[irq] = info;
213 irq_set_chip_data(irq, info);
216 /* Constructors for packed IRQ information. */
217 static int xen_irq_info_common_setup(struct irq_info *info,
219 enum xen_irq_type type,
225 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
229 info->evtchn = evtchn;
231 info->mask_reason = EVT_MASK_REASON_EXPLICIT;
232 raw_spin_lock_init(&info->lock);
234 ret = set_evtchn_to_irq(evtchn, irq);
238 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
240 return xen_evtchn_port_setup(info);
243 static int xen_irq_info_evtchn_setup(unsigned irq,
246 struct irq_info *info = info_for_irq(irq);
248 return xen_irq_info_common_setup(info, irq, IRQT_EVTCHN, evtchn, 0);
251 static int xen_irq_info_ipi_setup(unsigned cpu,
256 struct irq_info *info = info_for_irq(irq);
260 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
262 return xen_irq_info_common_setup(info, irq, IRQT_IPI, evtchn, 0);
265 static int xen_irq_info_virq_setup(unsigned cpu,
270 struct irq_info *info = info_for_irq(irq);
274 per_cpu(virq_to_irq, cpu)[virq] = irq;
276 return xen_irq_info_common_setup(info, irq, IRQT_VIRQ, evtchn, 0);
279 static int xen_irq_info_pirq_setup(unsigned irq,
286 struct irq_info *info = info_for_irq(irq);
288 info->u.pirq.pirq = pirq;
289 info->u.pirq.gsi = gsi;
290 info->u.pirq.domid = domid;
291 info->u.pirq.flags = flags;
293 return xen_irq_info_common_setup(info, irq, IRQT_PIRQ, evtchn, 0);
296 static void xen_irq_info_cleanup(struct irq_info *info)
298 set_evtchn_to_irq(info->evtchn, -1);
299 xen_evtchn_port_remove(info->evtchn, info->cpu);
304 * Accessors for packed IRQ information.
306 unsigned int evtchn_from_irq(unsigned irq)
308 const struct irq_info *info = NULL;
310 if (likely(irq < nr_irqs))
311 info = info_for_irq(irq);
318 unsigned irq_from_evtchn(unsigned int evtchn)
320 return get_evtchn_to_irq(evtchn);
322 EXPORT_SYMBOL_GPL(irq_from_evtchn);
324 int irq_from_virq(unsigned int cpu, unsigned int virq)
326 return per_cpu(virq_to_irq, cpu)[virq];
329 static enum ipi_vector ipi_from_irq(unsigned irq)
331 struct irq_info *info = info_for_irq(irq);
333 BUG_ON(info == NULL);
334 BUG_ON(info->type != IRQT_IPI);
339 static unsigned virq_from_irq(unsigned irq)
341 struct irq_info *info = info_for_irq(irq);
343 BUG_ON(info == NULL);
344 BUG_ON(info->type != IRQT_VIRQ);
349 static unsigned pirq_from_irq(unsigned irq)
351 struct irq_info *info = info_for_irq(irq);
353 BUG_ON(info == NULL);
354 BUG_ON(info->type != IRQT_PIRQ);
356 return info->u.pirq.pirq;
359 static enum xen_irq_type type_from_irq(unsigned irq)
361 return info_for_irq(irq)->type;
364 unsigned cpu_from_irq(unsigned irq)
366 return info_for_irq(irq)->cpu;
369 unsigned int cpu_from_evtchn(unsigned int evtchn)
371 int irq = get_evtchn_to_irq(evtchn);
375 ret = cpu_from_irq(irq);
380 static void do_mask(struct irq_info *info, u8 reason)
384 raw_spin_lock_irqsave(&info->lock, flags);
386 if (!info->mask_reason)
387 mask_evtchn(info->evtchn);
389 info->mask_reason |= reason;
391 raw_spin_unlock_irqrestore(&info->lock, flags);
394 static void do_unmask(struct irq_info *info, u8 reason)
398 raw_spin_lock_irqsave(&info->lock, flags);
400 info->mask_reason &= ~reason;
402 if (!info->mask_reason)
403 unmask_evtchn(info->evtchn);
405 raw_spin_unlock_irqrestore(&info->lock, flags);
409 static bool pirq_check_eoi_map(unsigned irq)
411 return test_bit(pirq_from_irq(irq), pirq_eoi_map);
415 static bool pirq_needs_eoi_flag(unsigned irq)
417 struct irq_info *info = info_for_irq(irq);
418 BUG_ON(info->type != IRQT_PIRQ);
420 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
423 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
425 int irq = get_evtchn_to_irq(chn);
426 struct irq_info *info = info_for_irq(irq);
430 cpumask_copy(irq_get_affinity_mask(irq), cpumask_of(cpu));
432 xen_evtchn_port_bind_to_cpu(info, cpu);
438 * notify_remote_via_irq - send event to remote end of event channel via irq
439 * @irq: irq of event channel to send event to
441 * Unlike notify_remote_via_evtchn(), this is safe to use across
442 * save/restore. Notifications on a broken connection are silently
445 void notify_remote_via_irq(int irq)
447 int evtchn = evtchn_from_irq(irq);
449 if (VALID_EVTCHN(evtchn))
450 notify_remote_via_evtchn(evtchn);
452 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
454 struct lateeoi_work {
455 struct delayed_work delayed;
456 spinlock_t eoi_list_lock;
457 struct list_head eoi_list;
460 static DEFINE_PER_CPU(struct lateeoi_work, lateeoi);
462 static void lateeoi_list_del(struct irq_info *info)
464 struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
467 spin_lock_irqsave(&eoi->eoi_list_lock, flags);
468 list_del_init(&info->eoi_list);
469 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
472 static void lateeoi_list_add(struct irq_info *info)
474 struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
475 struct irq_info *elem;
476 u64 now = get_jiffies_64();
480 if (now < info->eoi_time)
481 delay = info->eoi_time - now;
485 spin_lock_irqsave(&eoi->eoi_list_lock, flags);
487 if (list_empty(&eoi->eoi_list)) {
488 list_add(&info->eoi_list, &eoi->eoi_list);
489 mod_delayed_work_on(info->eoi_cpu, system_wq,
490 &eoi->delayed, delay);
492 list_for_each_entry_reverse(elem, &eoi->eoi_list, eoi_list) {
493 if (elem->eoi_time <= info->eoi_time)
496 list_add(&info->eoi_list, &elem->eoi_list);
499 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
502 static void xen_irq_lateeoi_locked(struct irq_info *info, bool spurious)
504 evtchn_port_t evtchn;
506 unsigned int delay = 0;
508 evtchn = info->evtchn;
509 if (!VALID_EVTCHN(evtchn) || !list_empty(&info->eoi_list))
513 if ((1 << info->spurious_cnt) < (HZ << 2))
514 info->spurious_cnt++;
515 if (info->spurious_cnt > 1) {
516 delay = 1 << (info->spurious_cnt - 2);
520 info->eoi_cpu = smp_processor_id();
521 info->eoi_time = get_jiffies_64() + delay;
524 info->spurious_cnt = 0;
528 if (info->eoi_time &&
529 (info->irq_epoch == per_cpu(irq_epoch, cpu) || delay)) {
530 lateeoi_list_add(info);
536 /* is_active hasn't been reset yet, do it now. */
537 smp_store_release(&info->is_active, 0);
538 do_unmask(info, EVT_MASK_REASON_EOI_PENDING);
541 static void xen_irq_lateeoi_worker(struct work_struct *work)
543 struct lateeoi_work *eoi;
544 struct irq_info *info;
545 u64 now = get_jiffies_64();
548 eoi = container_of(to_delayed_work(work), struct lateeoi_work, delayed);
550 read_lock_irqsave(&evtchn_rwlock, flags);
553 spin_lock(&eoi->eoi_list_lock);
555 info = list_first_entry_or_null(&eoi->eoi_list, struct irq_info,
558 if (info == NULL || now < info->eoi_time) {
559 spin_unlock(&eoi->eoi_list_lock);
563 list_del_init(&info->eoi_list);
565 spin_unlock(&eoi->eoi_list_lock);
569 xen_irq_lateeoi_locked(info, false);
573 mod_delayed_work_on(info->eoi_cpu, system_wq,
574 &eoi->delayed, info->eoi_time - now);
576 read_unlock_irqrestore(&evtchn_rwlock, flags);
579 static void xen_cpu_init_eoi(unsigned int cpu)
581 struct lateeoi_work *eoi = &per_cpu(lateeoi, cpu);
583 INIT_DELAYED_WORK(&eoi->delayed, xen_irq_lateeoi_worker);
584 spin_lock_init(&eoi->eoi_list_lock);
585 INIT_LIST_HEAD(&eoi->eoi_list);
588 void xen_irq_lateeoi(unsigned int irq, unsigned int eoi_flags)
590 struct irq_info *info;
593 read_lock_irqsave(&evtchn_rwlock, flags);
595 info = info_for_irq(irq);
598 xen_irq_lateeoi_locked(info, eoi_flags & XEN_EOI_FLAG_SPURIOUS);
600 read_unlock_irqrestore(&evtchn_rwlock, flags);
602 EXPORT_SYMBOL_GPL(xen_irq_lateeoi);
604 static void xen_irq_init(unsigned irq)
606 struct irq_info *info;
609 /* By default all event channels notify CPU#0. */
610 cpumask_copy(irq_get_affinity_mask(irq), cpumask_of(0));
613 info = kzalloc(sizeof(*info), GFP_KERNEL);
615 panic("Unable to allocate metadata for IRQ%d\n", irq);
617 info->type = IRQT_UNBOUND;
620 set_info_for_irq(irq, info);
622 INIT_LIST_HEAD(&info->eoi_list);
623 list_add_tail(&info->list, &xen_irq_list_head);
626 static int __must_check xen_allocate_irqs_dynamic(int nvec)
628 int i, irq = irq_alloc_descs(-1, 0, nvec, -1);
631 for (i = 0; i < nvec; i++)
632 xen_irq_init(irq + i);
638 static inline int __must_check xen_allocate_irq_dynamic(void)
641 return xen_allocate_irqs_dynamic(1);
644 static int __must_check xen_allocate_irq_gsi(unsigned gsi)
649 * A PV guest has no concept of a GSI (since it has no ACPI
650 * nor access to/knowledge of the physical APICs). Therefore
651 * all IRQs are dynamically allocated from the entire IRQ
654 if (xen_pv_domain() && !xen_initial_domain())
655 return xen_allocate_irq_dynamic();
657 /* Legacy IRQ descriptors are already allocated by the arch. */
658 if (gsi < nr_legacy_irqs())
661 irq = irq_alloc_desc_at(gsi, -1);
668 static void xen_free_irq(unsigned irq)
670 struct irq_info *info = info_for_irq(irq);
676 write_lock_irqsave(&evtchn_rwlock, flags);
678 if (!list_empty(&info->eoi_list))
679 lateeoi_list_del(info);
681 list_del(&info->list);
683 set_info_for_irq(irq, NULL);
685 WARN_ON(info->refcnt > 0);
687 write_unlock_irqrestore(&evtchn_rwlock, flags);
691 /* Legacy IRQ descriptors are managed by the arch. */
692 if (irq < nr_legacy_irqs())
698 static void xen_evtchn_close(unsigned int port)
700 struct evtchn_close close;
703 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
707 static void event_handler_exit(struct irq_info *info)
709 smp_store_release(&info->is_active, 0);
710 clear_evtchn(info->evtchn);
713 static void pirq_query_unmask(int irq)
715 struct physdev_irq_status_query irq_status;
716 struct irq_info *info = info_for_irq(irq);
718 BUG_ON(info->type != IRQT_PIRQ);
720 irq_status.irq = pirq_from_irq(irq);
721 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
722 irq_status.flags = 0;
724 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
725 if (irq_status.flags & XENIRQSTAT_needs_eoi)
726 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
729 static void eoi_pirq(struct irq_data *data)
731 struct irq_info *info = info_for_irq(data->irq);
732 int evtchn = info ? info->evtchn : 0;
733 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
736 if (!VALID_EVTCHN(evtchn))
739 if (unlikely(irqd_is_setaffinity_pending(data)) &&
740 likely(!irqd_irq_disabled(data))) {
741 do_mask(info, EVT_MASK_REASON_TEMPORARY);
743 event_handler_exit(info);
745 irq_move_masked_irq(data);
747 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
749 event_handler_exit(info);
751 if (pirq_needs_eoi(data->irq)) {
752 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
757 static void mask_ack_pirq(struct irq_data *data)
759 disable_dynirq(data);
763 static unsigned int __startup_pirq(unsigned int irq)
765 struct evtchn_bind_pirq bind_pirq;
766 struct irq_info *info = info_for_irq(irq);
767 int evtchn = evtchn_from_irq(irq);
770 BUG_ON(info->type != IRQT_PIRQ);
772 if (VALID_EVTCHN(evtchn))
775 bind_pirq.pirq = pirq_from_irq(irq);
776 /* NB. We are happy to share unless we are probing. */
777 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
778 BIND_PIRQ__WILL_SHARE : 0;
779 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
781 pr_warn("Failed to obtain physical IRQ %d\n", irq);
784 evtchn = bind_pirq.port;
786 pirq_query_unmask(irq);
788 rc = set_evtchn_to_irq(evtchn, irq);
792 info->evtchn = evtchn;
793 bind_evtchn_to_cpu(evtchn, 0);
795 rc = xen_evtchn_port_setup(info);
800 do_unmask(info, EVT_MASK_REASON_EXPLICIT);
802 eoi_pirq(irq_get_irq_data(irq));
807 pr_err("irq%d: Failed to set port to irq mapping (%d)\n", irq, rc);
808 xen_evtchn_close(evtchn);
812 static unsigned int startup_pirq(struct irq_data *data)
814 return __startup_pirq(data->irq);
817 static void shutdown_pirq(struct irq_data *data)
819 unsigned int irq = data->irq;
820 struct irq_info *info = info_for_irq(irq);
821 unsigned evtchn = evtchn_from_irq(irq);
823 BUG_ON(info->type != IRQT_PIRQ);
825 if (!VALID_EVTCHN(evtchn))
828 do_mask(info, EVT_MASK_REASON_EXPLICIT);
829 xen_evtchn_close(evtchn);
830 xen_irq_info_cleanup(info);
833 static void enable_pirq(struct irq_data *data)
838 static void disable_pirq(struct irq_data *data)
840 disable_dynirq(data);
843 int xen_irq_from_gsi(unsigned gsi)
845 struct irq_info *info;
847 list_for_each_entry(info, &xen_irq_list_head, list) {
848 if (info->type != IRQT_PIRQ)
851 if (info->u.pirq.gsi == gsi)
857 EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
859 static void __unbind_from_irq(unsigned int irq)
861 int evtchn = evtchn_from_irq(irq);
862 struct irq_info *info = info_for_irq(irq);
864 if (info->refcnt > 0) {
866 if (info->refcnt != 0)
870 if (VALID_EVTCHN(evtchn)) {
871 unsigned int cpu = cpu_from_irq(irq);
873 xen_evtchn_close(evtchn);
875 switch (type_from_irq(irq)) {
877 per_cpu(virq_to_irq, cpu)[virq_from_irq(irq)] = -1;
880 per_cpu(ipi_to_irq, cpu)[ipi_from_irq(irq)] = -1;
886 xen_irq_info_cleanup(info);
893 * Do not make any assumptions regarding the relationship between the
894 * IRQ number returned here and the Xen pirq argument.
896 * Note: We don't assign an event channel until the irq actually started
897 * up. Return an existing irq if we've already got one for the gsi.
899 * Shareable implies level triggered, not shareable implies edge
902 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
903 unsigned pirq, int shareable, char *name)
906 struct physdev_irq irq_op;
909 mutex_lock(&irq_mapping_update_lock);
911 irq = xen_irq_from_gsi(gsi);
913 pr_info("%s: returning irq %d for gsi %u\n",
918 irq = xen_allocate_irq_gsi(gsi);
925 /* Only the privileged domain can do this. For non-priv, the pcifront
926 * driver provides a PCI bus that does the call to do exactly
927 * this in the priv domain. */
928 if (xen_initial_domain() &&
929 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
935 ret = xen_irq_info_pirq_setup(irq, 0, pirq, gsi, DOMID_SELF,
936 shareable ? PIRQ_SHAREABLE : 0);
938 __unbind_from_irq(irq);
943 pirq_query_unmask(irq);
944 /* We try to use the handler with the appropriate semantic for the
945 * type of interrupt: if the interrupt is an edge triggered
946 * interrupt we use handle_edge_irq.
948 * On the other hand if the interrupt is level triggered we use
949 * handle_fasteoi_irq like the native code does for this kind of
952 * Depending on the Xen version, pirq_needs_eoi might return true
953 * not only for level triggered interrupts but for edge triggered
954 * interrupts too. In any case Xen always honors the eoi mechanism,
955 * not injecting any more pirqs of the same kind if the first one
956 * hasn't received an eoi yet. Therefore using the fasteoi handler
957 * is the right choice either way.
960 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
961 handle_fasteoi_irq, name);
963 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
964 handle_edge_irq, name);
967 mutex_unlock(&irq_mapping_update_lock);
972 #ifdef CONFIG_PCI_MSI
973 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
976 struct physdev_get_free_pirq op_get_free_pirq;
978 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
979 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
981 WARN_ONCE(rc == -ENOSYS,
982 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
984 return rc ? -1 : op_get_free_pirq.pirq;
987 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
988 int pirq, int nvec, const char *name, domid_t domid)
992 mutex_lock(&irq_mapping_update_lock);
994 irq = xen_allocate_irqs_dynamic(nvec);
998 for (i = 0; i < nvec; i++) {
999 irq_set_chip_and_handler_name(irq + i, &xen_pirq_chip, handle_edge_irq, name);
1001 ret = xen_irq_info_pirq_setup(irq + i, 0, pirq + i, 0, domid,
1002 i == 0 ? 0 : PIRQ_MSI_GROUP);
1007 ret = irq_set_msi_desc(irq, msidesc);
1011 mutex_unlock(&irq_mapping_update_lock);
1015 __unbind_from_irq(irq + nvec);
1016 mutex_unlock(&irq_mapping_update_lock);
1021 int xen_destroy_irq(int irq)
1023 struct physdev_unmap_pirq unmap_irq;
1024 struct irq_info *info = info_for_irq(irq);
1027 mutex_lock(&irq_mapping_update_lock);
1030 * If trying to remove a vector in a MSI group different
1031 * than the first one skip the PIRQ unmap unless this vector
1032 * is the first one in the group.
1034 if (xen_initial_domain() && !(info->u.pirq.flags & PIRQ_MSI_GROUP)) {
1035 unmap_irq.pirq = info->u.pirq.pirq;
1036 unmap_irq.domid = info->u.pirq.domid;
1037 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
1038 /* If another domain quits without making the pci_disable_msix
1039 * call, the Xen hypervisor takes care of freeing the PIRQs
1040 * (free_domain_pirqs).
1042 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
1043 pr_info("domain %d does not have %d anymore\n",
1044 info->u.pirq.domid, info->u.pirq.pirq);
1046 pr_warn("unmap irq failed %d\n", rc);
1054 mutex_unlock(&irq_mapping_update_lock);
1058 int xen_irq_from_pirq(unsigned pirq)
1062 struct irq_info *info;
1064 mutex_lock(&irq_mapping_update_lock);
1066 list_for_each_entry(info, &xen_irq_list_head, list) {
1067 if (info->type != IRQT_PIRQ)
1070 if (info->u.pirq.pirq == pirq)
1075 mutex_unlock(&irq_mapping_update_lock);
1081 int xen_pirq_from_irq(unsigned irq)
1083 return pirq_from_irq(irq);
1085 EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
1087 static int bind_evtchn_to_irq_chip(evtchn_port_t evtchn, struct irq_chip *chip)
1092 if (evtchn >= xen_evtchn_max_channels())
1095 mutex_lock(&irq_mapping_update_lock);
1097 irq = get_evtchn_to_irq(evtchn);
1100 irq = xen_allocate_irq_dynamic();
1104 irq_set_chip_and_handler_name(irq, chip,
1105 handle_edge_irq, "event");
1107 ret = xen_irq_info_evtchn_setup(irq, evtchn);
1109 __unbind_from_irq(irq);
1113 /* New interdomain events are bound to VCPU 0. */
1114 bind_evtchn_to_cpu(evtchn, 0);
1116 struct irq_info *info = info_for_irq(irq);
1117 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
1121 mutex_unlock(&irq_mapping_update_lock);
1126 int bind_evtchn_to_irq(evtchn_port_t evtchn)
1128 return bind_evtchn_to_irq_chip(evtchn, &xen_dynamic_chip);
1130 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
1132 int bind_evtchn_to_irq_lateeoi(evtchn_port_t evtchn)
1134 return bind_evtchn_to_irq_chip(evtchn, &xen_lateeoi_chip);
1136 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq_lateeoi);
1138 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
1140 struct evtchn_bind_ipi bind_ipi;
1144 mutex_lock(&irq_mapping_update_lock);
1146 irq = per_cpu(ipi_to_irq, cpu)[ipi];
1149 irq = xen_allocate_irq_dynamic();
1153 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
1154 handle_percpu_irq, "ipi");
1156 bind_ipi.vcpu = xen_vcpu_nr(cpu);
1157 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1160 evtchn = bind_ipi.port;
1162 ret = xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
1164 __unbind_from_irq(irq);
1168 bind_evtchn_to_cpu(evtchn, cpu);
1170 struct irq_info *info = info_for_irq(irq);
1171 WARN_ON(info == NULL || info->type != IRQT_IPI);
1175 mutex_unlock(&irq_mapping_update_lock);
1179 static int bind_interdomain_evtchn_to_irq_chip(unsigned int remote_domain,
1180 evtchn_port_t remote_port,
1181 struct irq_chip *chip)
1183 struct evtchn_bind_interdomain bind_interdomain;
1186 bind_interdomain.remote_dom = remote_domain;
1187 bind_interdomain.remote_port = remote_port;
1189 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
1192 return err ? : bind_evtchn_to_irq_chip(bind_interdomain.local_port,
1196 int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
1197 evtchn_port_t remote_port)
1199 return bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1202 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irq);
1204 int bind_interdomain_evtchn_to_irq_lateeoi(unsigned int remote_domain,
1205 evtchn_port_t remote_port)
1207 return bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1210 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irq_lateeoi);
1212 static int find_virq(unsigned int virq, unsigned int cpu)
1214 struct evtchn_status status;
1215 int port, rc = -ENOENT;
1217 memset(&status, 0, sizeof(status));
1218 for (port = 0; port < xen_evtchn_max_channels(); port++) {
1219 status.dom = DOMID_SELF;
1221 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
1224 if (status.status != EVTCHNSTAT_virq)
1226 if (status.u.virq == virq && status.vcpu == xen_vcpu_nr(cpu)) {
1235 * xen_evtchn_nr_channels - number of usable event channel ports
1237 * This may be less than the maximum supported by the current
1238 * hypervisor ABI. Use xen_evtchn_max_channels() for the maximum
1241 unsigned xen_evtchn_nr_channels(void)
1243 return evtchn_ops->nr_channels();
1245 EXPORT_SYMBOL_GPL(xen_evtchn_nr_channels);
1247 int bind_virq_to_irq(unsigned int virq, unsigned int cpu, bool percpu)
1249 struct evtchn_bind_virq bind_virq;
1250 int evtchn, irq, ret;
1252 mutex_lock(&irq_mapping_update_lock);
1254 irq = per_cpu(virq_to_irq, cpu)[virq];
1257 irq = xen_allocate_irq_dynamic();
1262 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
1263 handle_percpu_irq, "virq");
1265 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
1266 handle_edge_irq, "virq");
1268 bind_virq.virq = virq;
1269 bind_virq.vcpu = xen_vcpu_nr(cpu);
1270 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1273 evtchn = bind_virq.port;
1276 ret = find_virq(virq, cpu);
1281 ret = xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1283 __unbind_from_irq(irq);
1288 bind_evtchn_to_cpu(evtchn, cpu);
1290 struct irq_info *info = info_for_irq(irq);
1291 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
1295 mutex_unlock(&irq_mapping_update_lock);
1300 static void unbind_from_irq(unsigned int irq)
1302 mutex_lock(&irq_mapping_update_lock);
1303 __unbind_from_irq(irq);
1304 mutex_unlock(&irq_mapping_update_lock);
1307 static int bind_evtchn_to_irqhandler_chip(evtchn_port_t evtchn,
1308 irq_handler_t handler,
1309 unsigned long irqflags,
1310 const char *devname, void *dev_id,
1311 struct irq_chip *chip)
1315 irq = bind_evtchn_to_irq_chip(evtchn, chip);
1318 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1320 unbind_from_irq(irq);
1327 int bind_evtchn_to_irqhandler(evtchn_port_t evtchn,
1328 irq_handler_t handler,
1329 unsigned long irqflags,
1330 const char *devname, void *dev_id)
1332 return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1336 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1338 int bind_evtchn_to_irqhandler_lateeoi(evtchn_port_t evtchn,
1339 irq_handler_t handler,
1340 unsigned long irqflags,
1341 const char *devname, void *dev_id)
1343 return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1347 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler_lateeoi);
1349 static int bind_interdomain_evtchn_to_irqhandler_chip(
1350 unsigned int remote_domain, evtchn_port_t remote_port,
1351 irq_handler_t handler, unsigned long irqflags,
1352 const char *devname, void *dev_id, struct irq_chip *chip)
1356 irq = bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1361 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1363 unbind_from_irq(irq);
1370 int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1371 evtchn_port_t remote_port,
1372 irq_handler_t handler,
1373 unsigned long irqflags,
1374 const char *devname,
1377 return bind_interdomain_evtchn_to_irqhandler_chip(remote_domain,
1378 remote_port, handler, irqflags, devname,
1379 dev_id, &xen_dynamic_chip);
1381 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1383 int bind_interdomain_evtchn_to_irqhandler_lateeoi(unsigned int remote_domain,
1384 evtchn_port_t remote_port,
1385 irq_handler_t handler,
1386 unsigned long irqflags,
1387 const char *devname,
1390 return bind_interdomain_evtchn_to_irqhandler_chip(remote_domain,
1391 remote_port, handler, irqflags, devname,
1392 dev_id, &xen_lateeoi_chip);
1394 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler_lateeoi);
1396 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
1397 irq_handler_t handler,
1398 unsigned long irqflags, const char *devname, void *dev_id)
1402 irq = bind_virq_to_irq(virq, cpu, irqflags & IRQF_PERCPU);
1405 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1407 unbind_from_irq(irq);
1413 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1415 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1417 irq_handler_t handler,
1418 unsigned long irqflags,
1419 const char *devname,
1424 irq = bind_ipi_to_irq(ipi, cpu);
1428 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
1429 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1431 unbind_from_irq(irq);
1438 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1440 struct irq_info *info = info_for_irq(irq);
1444 free_irq(irq, dev_id);
1445 unbind_from_irq(irq);
1447 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1450 * xen_set_irq_priority() - set an event channel priority.
1451 * @irq:irq bound to an event channel.
1452 * @priority: priority between XEN_IRQ_PRIORITY_MAX and XEN_IRQ_PRIORITY_MIN.
1454 int xen_set_irq_priority(unsigned irq, unsigned priority)
1456 struct evtchn_set_priority set_priority;
1458 set_priority.port = evtchn_from_irq(irq);
1459 set_priority.priority = priority;
1461 return HYPERVISOR_event_channel_op(EVTCHNOP_set_priority,
1464 EXPORT_SYMBOL_GPL(xen_set_irq_priority);
1466 int evtchn_make_refcounted(unsigned int evtchn)
1468 int irq = get_evtchn_to_irq(evtchn);
1469 struct irq_info *info;
1474 info = info_for_irq(irq);
1479 WARN_ON(info->refcnt != -1);
1485 EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1487 int evtchn_get(unsigned int evtchn)
1490 struct irq_info *info;
1493 if (evtchn >= xen_evtchn_max_channels())
1496 mutex_lock(&irq_mapping_update_lock);
1498 irq = get_evtchn_to_irq(evtchn);
1502 info = info_for_irq(irq);
1508 if (info->refcnt <= 0 || info->refcnt == SHRT_MAX)
1514 mutex_unlock(&irq_mapping_update_lock);
1518 EXPORT_SYMBOL_GPL(evtchn_get);
1520 void evtchn_put(unsigned int evtchn)
1522 int irq = get_evtchn_to_irq(evtchn);
1523 if (WARN_ON(irq == -1))
1525 unbind_from_irq(irq);
1527 EXPORT_SYMBOL_GPL(evtchn_put);
1529 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1534 if (unlikely(vector == XEN_NMI_VECTOR)) {
1535 int rc = HYPERVISOR_vcpu_op(VCPUOP_send_nmi, xen_vcpu_nr(cpu),
1538 printk(KERN_WARNING "Sending nmi to CPU%d failed (rc:%d)\n", cpu, rc);
1542 irq = per_cpu(ipi_to_irq, cpu)[vector];
1544 notify_remote_via_irq(irq);
1547 struct evtchn_loop_ctrl {
1553 void handle_irq_for_port(evtchn_port_t port, struct evtchn_loop_ctrl *ctrl)
1556 struct irq_info *info;
1558 irq = get_evtchn_to_irq(port);
1563 * Check for timeout every 256 events.
1564 * We are setting the timeout value only after the first 256
1565 * events in order to not hurt the common case of few loop
1566 * iterations. The 256 is basically an arbitrary value.
1568 * In case we are hitting the timeout we need to defer all further
1569 * EOIs in order to ensure to leave the event handling loop rather
1570 * sooner than later.
1572 if (!ctrl->defer_eoi && !(++ctrl->count & 0xff)) {
1573 ktime_t kt = ktime_get();
1575 if (!ctrl->timeout) {
1576 kt = ktime_add_ms(kt,
1577 jiffies_to_msecs(event_loop_timeout));
1579 } else if (kt > ctrl->timeout) {
1580 ctrl->defer_eoi = true;
1584 info = info_for_irq(irq);
1585 if (xchg_acquire(&info->is_active, 1))
1588 if (ctrl->defer_eoi) {
1589 info->eoi_cpu = smp_processor_id();
1590 info->irq_epoch = __this_cpu_read(irq_epoch);
1591 info->eoi_time = get_jiffies_64() + event_eoi_delay;
1594 generic_handle_irq(irq);
1597 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1599 static void __xen_evtchn_do_upcall(void)
1601 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1602 int cpu = get_cpu();
1604 struct evtchn_loop_ctrl ctrl = { 0 };
1606 read_lock(&evtchn_rwlock);
1609 vcpu_info->evtchn_upcall_pending = 0;
1611 if (__this_cpu_inc_return(xed_nesting_count) - 1)
1614 xen_evtchn_handle_events(cpu, &ctrl);
1616 BUG_ON(!irqs_disabled());
1618 count = __this_cpu_read(xed_nesting_count);
1619 __this_cpu_write(xed_nesting_count, 0);
1620 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1623 read_unlock(&evtchn_rwlock);
1626 * Increment irq_epoch only now to defer EOIs only for
1627 * xen_irq_lateeoi() invocations occurring from inside the loop
1630 __this_cpu_inc(irq_epoch);
1635 void xen_evtchn_do_upcall(struct pt_regs *regs)
1637 struct pt_regs *old_regs = set_irq_regs(regs);
1641 inc_irq_stat(irq_hv_callback_count);
1644 __xen_evtchn_do_upcall();
1647 set_irq_regs(old_regs);
1650 void xen_hvm_evtchn_do_upcall(void)
1652 __xen_evtchn_do_upcall();
1654 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1656 /* Rebind a new event channel to an existing irq. */
1657 void rebind_evtchn_irq(int evtchn, int irq)
1659 struct irq_info *info = info_for_irq(irq);
1664 /* Make sure the irq is masked, since the new event channel
1665 will also be masked. */
1668 mutex_lock(&irq_mapping_update_lock);
1670 /* After resume the irq<->evtchn mappings are all cleared out */
1671 BUG_ON(get_evtchn_to_irq(evtchn) != -1);
1672 /* Expect irq to have been bound before,
1673 so there should be a proper type */
1674 BUG_ON(info->type == IRQT_UNBOUND);
1676 (void)xen_irq_info_evtchn_setup(irq, evtchn);
1678 mutex_unlock(&irq_mapping_update_lock);
1680 bind_evtchn_to_cpu(evtchn, info->cpu);
1681 /* This will be deferred until interrupt is processed */
1682 irq_set_affinity(irq, cpumask_of(info->cpu));
1684 /* Unmask the event channel. */
1688 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1689 static int xen_rebind_evtchn_to_cpu(struct irq_info *info, unsigned int tcpu)
1691 struct evtchn_bind_vcpu bind_vcpu;
1692 evtchn_port_t evtchn = info ? info->evtchn : 0;
1694 if (!VALID_EVTCHN(evtchn))
1697 if (!xen_support_evtchn_rebind())
1700 /* Send future instances of this interrupt to other vcpu. */
1701 bind_vcpu.port = evtchn;
1702 bind_vcpu.vcpu = xen_vcpu_nr(tcpu);
1705 * Mask the event while changing the VCPU binding to prevent
1706 * it being delivered on an unexpected VCPU.
1708 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1711 * If this fails, it usually just indicates that we're dealing with a
1712 * virq or IPI channel, which don't actually need to be rebound. Ignore
1713 * it, but don't do the xenlinux-level rebind in that case.
1715 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1716 bind_evtchn_to_cpu(evtchn, tcpu);
1718 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1723 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1726 unsigned tcpu = cpumask_first_and(dest, cpu_online_mask);
1727 int ret = xen_rebind_evtchn_to_cpu(info_for_irq(data->irq), tcpu);
1730 irq_data_update_effective_affinity(data, cpumask_of(tcpu));
1735 /* To be called with desc->lock held. */
1736 int xen_set_affinity_evtchn(struct irq_desc *desc, unsigned int tcpu)
1738 struct irq_data *d = irq_desc_get_irq_data(desc);
1740 return set_affinity_irq(d, cpumask_of(tcpu), false);
1742 EXPORT_SYMBOL_GPL(xen_set_affinity_evtchn);
1744 static void enable_dynirq(struct irq_data *data)
1746 struct irq_info *info = info_for_irq(data->irq);
1747 evtchn_port_t evtchn = info ? info->evtchn : 0;
1749 if (VALID_EVTCHN(evtchn))
1750 do_unmask(info, EVT_MASK_REASON_EXPLICIT);
1753 static void disable_dynirq(struct irq_data *data)
1755 struct irq_info *info = info_for_irq(data->irq);
1756 evtchn_port_t evtchn = info ? info->evtchn : 0;
1758 if (VALID_EVTCHN(evtchn))
1759 do_mask(info, EVT_MASK_REASON_EXPLICIT);
1762 static void ack_dynirq(struct irq_data *data)
1764 struct irq_info *info = info_for_irq(data->irq);
1765 evtchn_port_t evtchn = info ? info->evtchn : 0;
1767 if (!VALID_EVTCHN(evtchn))
1770 if (unlikely(irqd_is_setaffinity_pending(data)) &&
1771 likely(!irqd_irq_disabled(data))) {
1772 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1774 event_handler_exit(info);
1776 irq_move_masked_irq(data);
1778 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1780 event_handler_exit(info);
1783 static void mask_ack_dynirq(struct irq_data *data)
1785 disable_dynirq(data);
1789 static void lateeoi_ack_dynirq(struct irq_data *data)
1791 struct irq_info *info = info_for_irq(data->irq);
1792 evtchn_port_t evtchn = info ? info->evtchn : 0;
1794 if (!VALID_EVTCHN(evtchn))
1797 do_mask(info, EVT_MASK_REASON_EOI_PENDING);
1799 if (unlikely(irqd_is_setaffinity_pending(data)) &&
1800 likely(!irqd_irq_disabled(data))) {
1801 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1803 clear_evtchn(evtchn);
1805 irq_move_masked_irq(data);
1807 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1809 clear_evtchn(evtchn);
1812 static void lateeoi_mask_ack_dynirq(struct irq_data *data)
1814 struct irq_info *info = info_for_irq(data->irq);
1815 evtchn_port_t evtchn = info ? info->evtchn : 0;
1817 if (VALID_EVTCHN(evtchn)) {
1818 do_mask(info, EVT_MASK_REASON_EXPLICIT);
1823 static int retrigger_dynirq(struct irq_data *data)
1825 struct irq_info *info = info_for_irq(data->irq);
1826 evtchn_port_t evtchn = info ? info->evtchn : 0;
1828 if (!VALID_EVTCHN(evtchn))
1831 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1833 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1838 static void restore_pirqs(void)
1840 int pirq, rc, irq, gsi;
1841 struct physdev_map_pirq map_irq;
1842 struct irq_info *info;
1844 list_for_each_entry(info, &xen_irq_list_head, list) {
1845 if (info->type != IRQT_PIRQ)
1848 pirq = info->u.pirq.pirq;
1849 gsi = info->u.pirq.gsi;
1852 /* save/restore of PT devices doesn't work, so at this point the
1853 * only devices present are GSI based emulated devices */
1857 map_irq.domid = DOMID_SELF;
1858 map_irq.type = MAP_PIRQ_TYPE_GSI;
1859 map_irq.index = gsi;
1860 map_irq.pirq = pirq;
1862 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1864 pr_warn("xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1865 gsi, irq, pirq, rc);
1870 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1872 __startup_pirq(irq);
1876 static void restore_cpu_virqs(unsigned int cpu)
1878 struct evtchn_bind_virq bind_virq;
1879 int virq, irq, evtchn;
1881 for (virq = 0; virq < NR_VIRQS; virq++) {
1882 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1885 BUG_ON(virq_from_irq(irq) != virq);
1887 /* Get a new binding from Xen. */
1888 bind_virq.virq = virq;
1889 bind_virq.vcpu = xen_vcpu_nr(cpu);
1890 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1893 evtchn = bind_virq.port;
1895 /* Record the new mapping. */
1896 (void)xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1897 bind_evtchn_to_cpu(evtchn, cpu);
1901 static void restore_cpu_ipis(unsigned int cpu)
1903 struct evtchn_bind_ipi bind_ipi;
1904 int ipi, irq, evtchn;
1906 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1907 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1910 BUG_ON(ipi_from_irq(irq) != ipi);
1912 /* Get a new binding from Xen. */
1913 bind_ipi.vcpu = xen_vcpu_nr(cpu);
1914 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1917 evtchn = bind_ipi.port;
1919 /* Record the new mapping. */
1920 (void)xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
1921 bind_evtchn_to_cpu(evtchn, cpu);
1925 /* Clear an irq's pending state, in preparation for polling on it */
1926 void xen_clear_irq_pending(int irq)
1928 struct irq_info *info = info_for_irq(irq);
1929 evtchn_port_t evtchn = info ? info->evtchn : 0;
1931 if (VALID_EVTCHN(evtchn))
1932 event_handler_exit(info);
1934 EXPORT_SYMBOL(xen_clear_irq_pending);
1935 void xen_set_irq_pending(int irq)
1937 int evtchn = evtchn_from_irq(irq);
1939 if (VALID_EVTCHN(evtchn))
1943 bool xen_test_irq_pending(int irq)
1945 int evtchn = evtchn_from_irq(irq);
1948 if (VALID_EVTCHN(evtchn))
1949 ret = test_evtchn(evtchn);
1954 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1955 * the irq will be disabled so it won't deliver an interrupt. */
1956 void xen_poll_irq_timeout(int irq, u64 timeout)
1958 evtchn_port_t evtchn = evtchn_from_irq(irq);
1960 if (VALID_EVTCHN(evtchn)) {
1961 struct sched_poll poll;
1964 poll.timeout = timeout;
1965 set_xen_guest_handle(poll.ports, &evtchn);
1967 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1971 EXPORT_SYMBOL(xen_poll_irq_timeout);
1972 /* Poll waiting for an irq to become pending. In the usual case, the
1973 * irq will be disabled so it won't deliver an interrupt. */
1974 void xen_poll_irq(int irq)
1976 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1979 /* Check whether the IRQ line is shared with other guests. */
1980 int xen_test_irq_shared(int irq)
1982 struct irq_info *info = info_for_irq(irq);
1983 struct physdev_irq_status_query irq_status;
1988 irq_status.irq = info->u.pirq.pirq;
1990 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1992 return !(irq_status.flags & XENIRQSTAT_shared);
1994 EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1996 void xen_irq_resume(void)
1999 struct irq_info *info;
2001 /* New event-channel space is not 'live' yet. */
2002 xen_evtchn_resume();
2004 /* No IRQ <-> event-channel mappings. */
2005 list_for_each_entry(info, &xen_irq_list_head, list)
2006 info->evtchn = 0; /* zap event-channel binding */
2008 clear_evtchn_to_irq_all();
2010 for_each_possible_cpu(cpu) {
2011 restore_cpu_virqs(cpu);
2012 restore_cpu_ipis(cpu);
2018 static struct irq_chip xen_dynamic_chip __read_mostly = {
2021 .irq_disable = disable_dynirq,
2022 .irq_mask = disable_dynirq,
2023 .irq_unmask = enable_dynirq,
2025 .irq_ack = ack_dynirq,
2026 .irq_mask_ack = mask_ack_dynirq,
2028 .irq_set_affinity = set_affinity_irq,
2029 .irq_retrigger = retrigger_dynirq,
2032 static struct irq_chip xen_lateeoi_chip __read_mostly = {
2033 /* The chip name needs to contain "xen-dyn" for irqbalance to work. */
2034 .name = "xen-dyn-lateeoi",
2036 .irq_disable = disable_dynirq,
2037 .irq_mask = disable_dynirq,
2038 .irq_unmask = enable_dynirq,
2040 .irq_ack = lateeoi_ack_dynirq,
2041 .irq_mask_ack = lateeoi_mask_ack_dynirq,
2043 .irq_set_affinity = set_affinity_irq,
2044 .irq_retrigger = retrigger_dynirq,
2047 static struct irq_chip xen_pirq_chip __read_mostly = {
2050 .irq_startup = startup_pirq,
2051 .irq_shutdown = shutdown_pirq,
2052 .irq_enable = enable_pirq,
2053 .irq_disable = disable_pirq,
2055 .irq_mask = disable_dynirq,
2056 .irq_unmask = enable_dynirq,
2058 .irq_ack = eoi_pirq,
2059 .irq_eoi = eoi_pirq,
2060 .irq_mask_ack = mask_ack_pirq,
2062 .irq_set_affinity = set_affinity_irq,
2064 .irq_retrigger = retrigger_dynirq,
2067 static struct irq_chip xen_percpu_chip __read_mostly = {
2068 .name = "xen-percpu",
2070 .irq_disable = disable_dynirq,
2071 .irq_mask = disable_dynirq,
2072 .irq_unmask = enable_dynirq,
2074 .irq_ack = ack_dynirq,
2077 #ifdef CONFIG_XEN_PVHVM
2078 /* Vector callbacks are better than PCI interrupts to receive event
2079 * channel notifications because we can receive vector callbacks on any
2080 * vcpu and we don't need PCI support or APIC interactions. */
2081 void xen_callback_vector(void)
2084 uint64_t callback_via;
2086 if (xen_have_vector_callback) {
2087 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
2088 rc = xen_set_callback_via(callback_via);
2090 pr_err("Request for Xen HVM callback vector failed\n");
2091 xen_have_vector_callback = 0;
2094 pr_info_once("Xen HVM callback vector for event delivery is enabled\n");
2095 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
2096 xen_hvm_callback_vector);
2100 void xen_callback_vector(void) {}
2103 static bool fifo_events = true;
2104 module_param(fifo_events, bool, 0);
2106 static int xen_evtchn_cpu_prepare(unsigned int cpu)
2110 xen_cpu_init_eoi(cpu);
2112 if (evtchn_ops->percpu_init)
2113 ret = evtchn_ops->percpu_init(cpu);
2118 static int xen_evtchn_cpu_dead(unsigned int cpu)
2122 if (evtchn_ops->percpu_deinit)
2123 ret = evtchn_ops->percpu_deinit(cpu);
2128 void __init xen_init_IRQ(void)
2131 unsigned int evtchn;
2134 ret = xen_evtchn_fifo_init();
2136 xen_evtchn_2l_init();
2138 xen_cpu_init_eoi(smp_processor_id());
2140 cpuhp_setup_state_nocalls(CPUHP_XEN_EVTCHN_PREPARE,
2141 "xen/evtchn:prepare",
2142 xen_evtchn_cpu_prepare, xen_evtchn_cpu_dead);
2144 evtchn_to_irq = kcalloc(EVTCHN_ROW(xen_evtchn_max_channels()),
2145 sizeof(*evtchn_to_irq), GFP_KERNEL);
2146 BUG_ON(!evtchn_to_irq);
2148 /* No event channels are 'live' right now. */
2149 for (evtchn = 0; evtchn < xen_evtchn_nr_channels(); evtchn++)
2150 mask_evtchn(evtchn);
2152 pirq_needs_eoi = pirq_needs_eoi_flag;
2155 if (xen_pv_domain()) {
2156 irq_ctx_init(smp_processor_id());
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;