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/module.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/cpu.h>
37 #include <linux/atomic.h>
38 #include <linux/ktime.h>
42 #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);
438 static void xen_evtchn_mask_all(void)
442 for (evtchn = 0; evtchn < xen_evtchn_nr_channels(); evtchn++)
447 * notify_remote_via_irq - send event to remote end of event channel via irq
448 * @irq: irq of event channel to send event to
450 * Unlike notify_remote_via_evtchn(), this is safe to use across
451 * save/restore. Notifications on a broken connection are silently
454 void notify_remote_via_irq(int irq)
456 int evtchn = evtchn_from_irq(irq);
458 if (VALID_EVTCHN(evtchn))
459 notify_remote_via_evtchn(evtchn);
461 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
463 struct lateeoi_work {
464 struct delayed_work delayed;
465 spinlock_t eoi_list_lock;
466 struct list_head eoi_list;
469 static DEFINE_PER_CPU(struct lateeoi_work, lateeoi);
471 static void lateeoi_list_del(struct irq_info *info)
473 struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
476 spin_lock_irqsave(&eoi->eoi_list_lock, flags);
477 list_del_init(&info->eoi_list);
478 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
481 static void lateeoi_list_add(struct irq_info *info)
483 struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
484 struct irq_info *elem;
485 u64 now = get_jiffies_64();
489 if (now < info->eoi_time)
490 delay = info->eoi_time - now;
494 spin_lock_irqsave(&eoi->eoi_list_lock, flags);
496 if (list_empty(&eoi->eoi_list)) {
497 list_add(&info->eoi_list, &eoi->eoi_list);
498 mod_delayed_work_on(info->eoi_cpu, system_wq,
499 &eoi->delayed, delay);
501 list_for_each_entry_reverse(elem, &eoi->eoi_list, eoi_list) {
502 if (elem->eoi_time <= info->eoi_time)
505 list_add(&info->eoi_list, &elem->eoi_list);
508 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
511 static void xen_irq_lateeoi_locked(struct irq_info *info, bool spurious)
513 evtchn_port_t evtchn;
515 unsigned int delay = 0;
517 evtchn = info->evtchn;
518 if (!VALID_EVTCHN(evtchn) || !list_empty(&info->eoi_list))
522 if ((1 << info->spurious_cnt) < (HZ << 2))
523 info->spurious_cnt++;
524 if (info->spurious_cnt > 1) {
525 delay = 1 << (info->spurious_cnt - 2);
529 info->eoi_cpu = smp_processor_id();
530 info->eoi_time = get_jiffies_64() + delay;
533 info->spurious_cnt = 0;
537 if (info->eoi_time &&
538 (info->irq_epoch == per_cpu(irq_epoch, cpu) || delay)) {
539 lateeoi_list_add(info);
545 /* is_active hasn't been reset yet, do it now. */
546 smp_store_release(&info->is_active, 0);
547 do_unmask(info, EVT_MASK_REASON_EOI_PENDING);
550 static void xen_irq_lateeoi_worker(struct work_struct *work)
552 struct lateeoi_work *eoi;
553 struct irq_info *info;
554 u64 now = get_jiffies_64();
557 eoi = container_of(to_delayed_work(work), struct lateeoi_work, delayed);
559 read_lock_irqsave(&evtchn_rwlock, flags);
562 spin_lock(&eoi->eoi_list_lock);
564 info = list_first_entry_or_null(&eoi->eoi_list, struct irq_info,
567 if (info == NULL || now < info->eoi_time) {
568 spin_unlock(&eoi->eoi_list_lock);
572 list_del_init(&info->eoi_list);
574 spin_unlock(&eoi->eoi_list_lock);
578 xen_irq_lateeoi_locked(info, false);
582 mod_delayed_work_on(info->eoi_cpu, system_wq,
583 &eoi->delayed, info->eoi_time - now);
585 read_unlock_irqrestore(&evtchn_rwlock, flags);
588 static void xen_cpu_init_eoi(unsigned int cpu)
590 struct lateeoi_work *eoi = &per_cpu(lateeoi, cpu);
592 INIT_DELAYED_WORK(&eoi->delayed, xen_irq_lateeoi_worker);
593 spin_lock_init(&eoi->eoi_list_lock);
594 INIT_LIST_HEAD(&eoi->eoi_list);
597 void xen_irq_lateeoi(unsigned int irq, unsigned int eoi_flags)
599 struct irq_info *info;
602 read_lock_irqsave(&evtchn_rwlock, flags);
604 info = info_for_irq(irq);
607 xen_irq_lateeoi_locked(info, eoi_flags & XEN_EOI_FLAG_SPURIOUS);
609 read_unlock_irqrestore(&evtchn_rwlock, flags);
611 EXPORT_SYMBOL_GPL(xen_irq_lateeoi);
613 static void xen_irq_init(unsigned irq)
615 struct irq_info *info;
618 /* By default all event channels notify CPU#0. */
619 cpumask_copy(irq_get_affinity_mask(irq), cpumask_of(0));
622 info = kzalloc(sizeof(*info), GFP_KERNEL);
624 panic("Unable to allocate metadata for IRQ%d\n", irq);
626 info->type = IRQT_UNBOUND;
629 set_info_for_irq(irq, info);
631 INIT_LIST_HEAD(&info->eoi_list);
632 list_add_tail(&info->list, &xen_irq_list_head);
635 static int __must_check xen_allocate_irqs_dynamic(int nvec)
637 int i, irq = irq_alloc_descs(-1, 0, nvec, -1);
640 for (i = 0; i < nvec; i++)
641 xen_irq_init(irq + i);
647 static inline int __must_check xen_allocate_irq_dynamic(void)
650 return xen_allocate_irqs_dynamic(1);
653 static int __must_check xen_allocate_irq_gsi(unsigned gsi)
658 * A PV guest has no concept of a GSI (since it has no ACPI
659 * nor access to/knowledge of the physical APICs). Therefore
660 * all IRQs are dynamically allocated from the entire IRQ
663 if (xen_pv_domain() && !xen_initial_domain())
664 return xen_allocate_irq_dynamic();
666 /* Legacy IRQ descriptors are already allocated by the arch. */
667 if (gsi < nr_legacy_irqs())
670 irq = irq_alloc_desc_at(gsi, -1);
677 static void xen_free_irq(unsigned irq)
679 struct irq_info *info = info_for_irq(irq);
685 write_lock_irqsave(&evtchn_rwlock, flags);
687 if (!list_empty(&info->eoi_list))
688 lateeoi_list_del(info);
690 list_del(&info->list);
692 set_info_for_irq(irq, NULL);
694 WARN_ON(info->refcnt > 0);
696 write_unlock_irqrestore(&evtchn_rwlock, flags);
700 /* Legacy IRQ descriptors are managed by the arch. */
701 if (irq < nr_legacy_irqs())
707 static void xen_evtchn_close(unsigned int port)
709 struct evtchn_close close;
712 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
716 static void event_handler_exit(struct irq_info *info)
718 smp_store_release(&info->is_active, 0);
719 clear_evtchn(info->evtchn);
722 static void pirq_query_unmask(int irq)
724 struct physdev_irq_status_query irq_status;
725 struct irq_info *info = info_for_irq(irq);
727 BUG_ON(info->type != IRQT_PIRQ);
729 irq_status.irq = pirq_from_irq(irq);
730 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
731 irq_status.flags = 0;
733 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
734 if (irq_status.flags & XENIRQSTAT_needs_eoi)
735 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
738 static void eoi_pirq(struct irq_data *data)
740 struct irq_info *info = info_for_irq(data->irq);
741 int evtchn = info ? info->evtchn : 0;
742 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
745 if (!VALID_EVTCHN(evtchn))
748 if (unlikely(irqd_is_setaffinity_pending(data)) &&
749 likely(!irqd_irq_disabled(data))) {
750 do_mask(info, EVT_MASK_REASON_TEMPORARY);
752 event_handler_exit(info);
754 irq_move_masked_irq(data);
756 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
758 event_handler_exit(info);
760 if (pirq_needs_eoi(data->irq)) {
761 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
766 static void mask_ack_pirq(struct irq_data *data)
768 disable_dynirq(data);
772 static unsigned int __startup_pirq(unsigned int irq)
774 struct evtchn_bind_pirq bind_pirq;
775 struct irq_info *info = info_for_irq(irq);
776 int evtchn = evtchn_from_irq(irq);
779 BUG_ON(info->type != IRQT_PIRQ);
781 if (VALID_EVTCHN(evtchn))
784 bind_pirq.pirq = pirq_from_irq(irq);
785 /* NB. We are happy to share unless we are probing. */
786 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
787 BIND_PIRQ__WILL_SHARE : 0;
788 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
790 pr_warn("Failed to obtain physical IRQ %d\n", irq);
793 evtchn = bind_pirq.port;
795 pirq_query_unmask(irq);
797 rc = set_evtchn_to_irq(evtchn, irq);
801 info->evtchn = evtchn;
802 bind_evtchn_to_cpu(evtchn, 0);
804 rc = xen_evtchn_port_setup(info);
809 do_unmask(info, EVT_MASK_REASON_EXPLICIT);
811 eoi_pirq(irq_get_irq_data(irq));
816 pr_err("irq%d: Failed to set port to irq mapping (%d)\n", irq, rc);
817 xen_evtchn_close(evtchn);
821 static unsigned int startup_pirq(struct irq_data *data)
823 return __startup_pirq(data->irq);
826 static void shutdown_pirq(struct irq_data *data)
828 unsigned int irq = data->irq;
829 struct irq_info *info = info_for_irq(irq);
830 unsigned evtchn = evtchn_from_irq(irq);
832 BUG_ON(info->type != IRQT_PIRQ);
834 if (!VALID_EVTCHN(evtchn))
837 do_mask(info, EVT_MASK_REASON_EXPLICIT);
838 xen_evtchn_close(evtchn);
839 xen_irq_info_cleanup(info);
842 static void enable_pirq(struct irq_data *data)
847 static void disable_pirq(struct irq_data *data)
849 disable_dynirq(data);
852 int xen_irq_from_gsi(unsigned gsi)
854 struct irq_info *info;
856 list_for_each_entry(info, &xen_irq_list_head, list) {
857 if (info->type != IRQT_PIRQ)
860 if (info->u.pirq.gsi == gsi)
866 EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
868 static void __unbind_from_irq(unsigned int irq)
870 int evtchn = evtchn_from_irq(irq);
871 struct irq_info *info = info_for_irq(irq);
873 if (info->refcnt > 0) {
875 if (info->refcnt != 0)
879 if (VALID_EVTCHN(evtchn)) {
880 unsigned int cpu = cpu_from_irq(irq);
882 xen_evtchn_close(evtchn);
884 switch (type_from_irq(irq)) {
886 per_cpu(virq_to_irq, cpu)[virq_from_irq(irq)] = -1;
889 per_cpu(ipi_to_irq, cpu)[ipi_from_irq(irq)] = -1;
895 xen_irq_info_cleanup(info);
902 * Do not make any assumptions regarding the relationship between the
903 * IRQ number returned here and the Xen pirq argument.
905 * Note: We don't assign an event channel until the irq actually started
906 * up. Return an existing irq if we've already got one for the gsi.
908 * Shareable implies level triggered, not shareable implies edge
911 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
912 unsigned pirq, int shareable, char *name)
915 struct physdev_irq irq_op;
918 mutex_lock(&irq_mapping_update_lock);
920 irq = xen_irq_from_gsi(gsi);
922 pr_info("%s: returning irq %d for gsi %u\n",
927 irq = xen_allocate_irq_gsi(gsi);
934 /* Only the privileged domain can do this. For non-priv, the pcifront
935 * driver provides a PCI bus that does the call to do exactly
936 * this in the priv domain. */
937 if (xen_initial_domain() &&
938 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
944 ret = xen_irq_info_pirq_setup(irq, 0, pirq, gsi, DOMID_SELF,
945 shareable ? PIRQ_SHAREABLE : 0);
947 __unbind_from_irq(irq);
952 pirq_query_unmask(irq);
953 /* We try to use the handler with the appropriate semantic for the
954 * type of interrupt: if the interrupt is an edge triggered
955 * interrupt we use handle_edge_irq.
957 * On the other hand if the interrupt is level triggered we use
958 * handle_fasteoi_irq like the native code does for this kind of
961 * Depending on the Xen version, pirq_needs_eoi might return true
962 * not only for level triggered interrupts but for edge triggered
963 * interrupts too. In any case Xen always honors the eoi mechanism,
964 * not injecting any more pirqs of the same kind if the first one
965 * hasn't received an eoi yet. Therefore using the fasteoi handler
966 * is the right choice either way.
969 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
970 handle_fasteoi_irq, name);
972 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
973 handle_edge_irq, name);
976 mutex_unlock(&irq_mapping_update_lock);
981 #ifdef CONFIG_PCI_MSI
982 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
985 struct physdev_get_free_pirq op_get_free_pirq;
987 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
988 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
990 WARN_ONCE(rc == -ENOSYS,
991 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
993 return rc ? -1 : op_get_free_pirq.pirq;
996 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
997 int pirq, int nvec, const char *name, domid_t domid)
1001 mutex_lock(&irq_mapping_update_lock);
1003 irq = xen_allocate_irqs_dynamic(nvec);
1007 for (i = 0; i < nvec; i++) {
1008 irq_set_chip_and_handler_name(irq + i, &xen_pirq_chip, handle_edge_irq, name);
1010 ret = xen_irq_info_pirq_setup(irq + i, 0, pirq + i, 0, domid,
1011 i == 0 ? 0 : PIRQ_MSI_GROUP);
1016 ret = irq_set_msi_desc(irq, msidesc);
1020 mutex_unlock(&irq_mapping_update_lock);
1024 __unbind_from_irq(irq + nvec);
1025 mutex_unlock(&irq_mapping_update_lock);
1030 int xen_destroy_irq(int irq)
1032 struct physdev_unmap_pirq unmap_irq;
1033 struct irq_info *info = info_for_irq(irq);
1036 mutex_lock(&irq_mapping_update_lock);
1039 * If trying to remove a vector in a MSI group different
1040 * than the first one skip the PIRQ unmap unless this vector
1041 * is the first one in the group.
1043 if (xen_initial_domain() && !(info->u.pirq.flags & PIRQ_MSI_GROUP)) {
1044 unmap_irq.pirq = info->u.pirq.pirq;
1045 unmap_irq.domid = info->u.pirq.domid;
1046 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
1047 /* If another domain quits without making the pci_disable_msix
1048 * call, the Xen hypervisor takes care of freeing the PIRQs
1049 * (free_domain_pirqs).
1051 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
1052 pr_info("domain %d does not have %d anymore\n",
1053 info->u.pirq.domid, info->u.pirq.pirq);
1055 pr_warn("unmap irq failed %d\n", rc);
1063 mutex_unlock(&irq_mapping_update_lock);
1067 int xen_irq_from_pirq(unsigned pirq)
1071 struct irq_info *info;
1073 mutex_lock(&irq_mapping_update_lock);
1075 list_for_each_entry(info, &xen_irq_list_head, list) {
1076 if (info->type != IRQT_PIRQ)
1079 if (info->u.pirq.pirq == pirq)
1084 mutex_unlock(&irq_mapping_update_lock);
1090 int xen_pirq_from_irq(unsigned irq)
1092 return pirq_from_irq(irq);
1094 EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
1096 static int bind_evtchn_to_irq_chip(evtchn_port_t evtchn, struct irq_chip *chip)
1101 if (evtchn >= xen_evtchn_max_channels())
1104 mutex_lock(&irq_mapping_update_lock);
1106 irq = get_evtchn_to_irq(evtchn);
1109 irq = xen_allocate_irq_dynamic();
1113 irq_set_chip_and_handler_name(irq, chip,
1114 handle_edge_irq, "event");
1116 ret = xen_irq_info_evtchn_setup(irq, evtchn);
1118 __unbind_from_irq(irq);
1122 /* New interdomain events are bound to VCPU 0. */
1123 bind_evtchn_to_cpu(evtchn, 0);
1125 struct irq_info *info = info_for_irq(irq);
1126 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
1130 mutex_unlock(&irq_mapping_update_lock);
1135 int bind_evtchn_to_irq(evtchn_port_t evtchn)
1137 return bind_evtchn_to_irq_chip(evtchn, &xen_dynamic_chip);
1139 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
1141 int bind_evtchn_to_irq_lateeoi(evtchn_port_t evtchn)
1143 return bind_evtchn_to_irq_chip(evtchn, &xen_lateeoi_chip);
1145 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq_lateeoi);
1147 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
1149 struct evtchn_bind_ipi bind_ipi;
1153 mutex_lock(&irq_mapping_update_lock);
1155 irq = per_cpu(ipi_to_irq, cpu)[ipi];
1158 irq = xen_allocate_irq_dynamic();
1162 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
1163 handle_percpu_irq, "ipi");
1165 bind_ipi.vcpu = cpu;
1166 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1169 evtchn = bind_ipi.port;
1171 ret = xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
1173 __unbind_from_irq(irq);
1177 bind_evtchn_to_cpu(evtchn, cpu);
1179 struct irq_info *info = info_for_irq(irq);
1180 WARN_ON(info == NULL || info->type != IRQT_IPI);
1184 mutex_unlock(&irq_mapping_update_lock);
1188 static int bind_interdomain_evtchn_to_irq_chip(unsigned int remote_domain,
1189 evtchn_port_t remote_port,
1190 struct irq_chip *chip)
1192 struct evtchn_bind_interdomain bind_interdomain;
1195 bind_interdomain.remote_dom = remote_domain;
1196 bind_interdomain.remote_port = remote_port;
1198 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
1201 return err ? : bind_evtchn_to_irq_chip(bind_interdomain.local_port,
1205 int bind_interdomain_evtchn_to_irq(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);
1213 int bind_interdomain_evtchn_to_irq_lateeoi(unsigned int remote_domain,
1214 evtchn_port_t remote_port)
1216 return bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1219 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irq_lateeoi);
1221 static int find_virq(unsigned int virq, unsigned int cpu)
1223 struct evtchn_status status;
1224 int port, rc = -ENOENT;
1226 memset(&status, 0, sizeof(status));
1227 for (port = 0; port < xen_evtchn_max_channels(); port++) {
1228 status.dom = DOMID_SELF;
1230 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
1233 if (status.status != EVTCHNSTAT_virq)
1235 if (status.u.virq == virq && status.vcpu == cpu) {
1244 * xen_evtchn_nr_channels - number of usable event channel ports
1246 * This may be less than the maximum supported by the current
1247 * hypervisor ABI. Use xen_evtchn_max_channels() for the maximum
1250 unsigned xen_evtchn_nr_channels(void)
1252 return evtchn_ops->nr_channels();
1254 EXPORT_SYMBOL_GPL(xen_evtchn_nr_channels);
1256 int bind_virq_to_irq(unsigned int virq, unsigned int cpu, bool percpu)
1258 struct evtchn_bind_virq bind_virq;
1259 int evtchn, irq, ret;
1261 mutex_lock(&irq_mapping_update_lock);
1263 irq = per_cpu(virq_to_irq, cpu)[virq];
1266 irq = xen_allocate_irq_dynamic();
1271 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
1272 handle_percpu_irq, "virq");
1274 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
1275 handle_edge_irq, "virq");
1277 bind_virq.virq = virq;
1278 bind_virq.vcpu = cpu;
1279 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1282 evtchn = bind_virq.port;
1285 ret = find_virq(virq, cpu);
1290 ret = xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1292 __unbind_from_irq(irq);
1297 bind_evtchn_to_cpu(evtchn, cpu);
1299 struct irq_info *info = info_for_irq(irq);
1300 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
1304 mutex_unlock(&irq_mapping_update_lock);
1309 static void unbind_from_irq(unsigned int irq)
1311 mutex_lock(&irq_mapping_update_lock);
1312 __unbind_from_irq(irq);
1313 mutex_unlock(&irq_mapping_update_lock);
1316 static int bind_evtchn_to_irqhandler_chip(evtchn_port_t evtchn,
1317 irq_handler_t handler,
1318 unsigned long irqflags,
1319 const char *devname, void *dev_id,
1320 struct irq_chip *chip)
1324 irq = bind_evtchn_to_irq_chip(evtchn, chip);
1327 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1329 unbind_from_irq(irq);
1336 int bind_evtchn_to_irqhandler(evtchn_port_t evtchn,
1337 irq_handler_t handler,
1338 unsigned long irqflags,
1339 const char *devname, void *dev_id)
1341 return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1345 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1347 int bind_evtchn_to_irqhandler_lateeoi(evtchn_port_t evtchn,
1348 irq_handler_t handler,
1349 unsigned long irqflags,
1350 const char *devname, void *dev_id)
1352 return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1356 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler_lateeoi);
1358 static int bind_interdomain_evtchn_to_irqhandler_chip(
1359 unsigned int remote_domain, evtchn_port_t remote_port,
1360 irq_handler_t handler, unsigned long irqflags,
1361 const char *devname, void *dev_id, struct irq_chip *chip)
1365 irq = bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1370 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1372 unbind_from_irq(irq);
1379 int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1380 evtchn_port_t remote_port,
1381 irq_handler_t handler,
1382 unsigned long irqflags,
1383 const char *devname,
1386 return bind_interdomain_evtchn_to_irqhandler_chip(remote_domain,
1387 remote_port, handler, irqflags, devname,
1388 dev_id, &xen_dynamic_chip);
1390 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1392 int bind_interdomain_evtchn_to_irqhandler_lateeoi(unsigned int remote_domain,
1393 evtchn_port_t remote_port,
1394 irq_handler_t handler,
1395 unsigned long irqflags,
1396 const char *devname,
1399 return bind_interdomain_evtchn_to_irqhandler_chip(remote_domain,
1400 remote_port, handler, irqflags, devname,
1401 dev_id, &xen_lateeoi_chip);
1403 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler_lateeoi);
1405 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
1406 irq_handler_t handler,
1407 unsigned long irqflags, const char *devname, void *dev_id)
1411 irq = bind_virq_to_irq(virq, cpu, irqflags & IRQF_PERCPU);
1414 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1416 unbind_from_irq(irq);
1422 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1424 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1426 irq_handler_t handler,
1427 unsigned long irqflags,
1428 const char *devname,
1433 irq = bind_ipi_to_irq(ipi, cpu);
1437 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
1438 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1440 unbind_from_irq(irq);
1447 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1449 struct irq_info *info = info_for_irq(irq);
1453 free_irq(irq, dev_id);
1454 unbind_from_irq(irq);
1456 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1459 * xen_set_irq_priority() - set an event channel priority.
1460 * @irq:irq bound to an event channel.
1461 * @priority: priority between XEN_IRQ_PRIORITY_MAX and XEN_IRQ_PRIORITY_MIN.
1463 int xen_set_irq_priority(unsigned irq, unsigned priority)
1465 struct evtchn_set_priority set_priority;
1467 set_priority.port = evtchn_from_irq(irq);
1468 set_priority.priority = priority;
1470 return HYPERVISOR_event_channel_op(EVTCHNOP_set_priority,
1473 EXPORT_SYMBOL_GPL(xen_set_irq_priority);
1475 int evtchn_make_refcounted(unsigned int evtchn)
1477 int irq = get_evtchn_to_irq(evtchn);
1478 struct irq_info *info;
1483 info = info_for_irq(irq);
1488 WARN_ON(info->refcnt != -1);
1494 EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1496 int evtchn_get(unsigned int evtchn)
1499 struct irq_info *info;
1502 if (evtchn >= xen_evtchn_max_channels())
1505 mutex_lock(&irq_mapping_update_lock);
1507 irq = get_evtchn_to_irq(evtchn);
1511 info = info_for_irq(irq);
1517 if (info->refcnt <= 0 || info->refcnt == SHRT_MAX)
1523 mutex_unlock(&irq_mapping_update_lock);
1527 EXPORT_SYMBOL_GPL(evtchn_get);
1529 void evtchn_put(unsigned int evtchn)
1531 int irq = get_evtchn_to_irq(evtchn);
1532 if (WARN_ON(irq == -1))
1534 unbind_from_irq(irq);
1536 EXPORT_SYMBOL_GPL(evtchn_put);
1538 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1543 if (unlikely(vector == XEN_NMI_VECTOR)) {
1544 int rc = HYPERVISOR_vcpu_op(VCPUOP_send_nmi, cpu, NULL);
1546 printk(KERN_WARNING "Sending nmi to CPU%d failed (rc:%d)\n", cpu, rc);
1550 irq = per_cpu(ipi_to_irq, cpu)[vector];
1552 notify_remote_via_irq(irq);
1555 struct evtchn_loop_ctrl {
1561 void handle_irq_for_port(evtchn_port_t port, struct evtchn_loop_ctrl *ctrl)
1564 struct irq_info *info;
1566 irq = get_evtchn_to_irq(port);
1571 * Check for timeout every 256 events.
1572 * We are setting the timeout value only after the first 256
1573 * events in order to not hurt the common case of few loop
1574 * iterations. The 256 is basically an arbitrary value.
1576 * In case we are hitting the timeout we need to defer all further
1577 * EOIs in order to ensure to leave the event handling loop rather
1578 * sooner than later.
1580 if (!ctrl->defer_eoi && !(++ctrl->count & 0xff)) {
1581 ktime_t kt = ktime_get();
1583 if (!ctrl->timeout.tv64) {
1584 kt = ktime_add_ms(kt,
1585 jiffies_to_msecs(event_loop_timeout));
1587 } else if (kt.tv64 > ctrl->timeout.tv64) {
1588 ctrl->defer_eoi = true;
1592 info = info_for_irq(irq);
1593 if (xchg_acquire(&info->is_active, 1))
1596 if (ctrl->defer_eoi) {
1597 info->eoi_cpu = smp_processor_id();
1598 info->irq_epoch = __this_cpu_read(irq_epoch);
1599 info->eoi_time = get_jiffies_64() + event_eoi_delay;
1602 generic_handle_irq(irq);
1605 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1607 static void __xen_evtchn_do_upcall(void)
1609 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1610 int cpu = get_cpu();
1612 struct evtchn_loop_ctrl ctrl = { 0 };
1614 read_lock(&evtchn_rwlock);
1617 vcpu_info->evtchn_upcall_pending = 0;
1619 if (__this_cpu_inc_return(xed_nesting_count) - 1)
1622 xen_evtchn_handle_events(cpu, &ctrl);
1624 BUG_ON(!irqs_disabled());
1626 count = __this_cpu_read(xed_nesting_count);
1627 __this_cpu_write(xed_nesting_count, 0);
1628 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1631 read_unlock(&evtchn_rwlock);
1634 * Increment irq_epoch only now to defer EOIs only for
1635 * xen_irq_lateeoi() invocations occurring from inside the loop
1638 __this_cpu_inc(irq_epoch);
1643 void xen_evtchn_do_upcall(struct pt_regs *regs)
1645 struct pt_regs *old_regs = set_irq_regs(regs);
1650 inc_irq_stat(irq_hv_callback_count);
1653 __xen_evtchn_do_upcall();
1656 set_irq_regs(old_regs);
1659 void xen_hvm_evtchn_do_upcall(void)
1661 __xen_evtchn_do_upcall();
1663 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1665 /* Rebind a new event channel to an existing irq. */
1666 void rebind_evtchn_irq(int evtchn, int irq)
1668 struct irq_info *info = info_for_irq(irq);
1673 /* Make sure the irq is masked, since the new event channel
1674 will also be masked. */
1677 mutex_lock(&irq_mapping_update_lock);
1679 /* After resume the irq<->evtchn mappings are all cleared out */
1680 BUG_ON(get_evtchn_to_irq(evtchn) != -1);
1681 /* Expect irq to have been bound before,
1682 so there should be a proper type */
1683 BUG_ON(info->type == IRQT_UNBOUND);
1685 (void)xen_irq_info_evtchn_setup(irq, evtchn);
1687 mutex_unlock(&irq_mapping_update_lock);
1689 bind_evtchn_to_cpu(evtchn, info->cpu);
1690 /* This will be deferred until interrupt is processed */
1691 irq_set_affinity(irq, cpumask_of(info->cpu));
1693 /* Unmask the event channel. */
1697 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1698 static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
1700 struct evtchn_bind_vcpu bind_vcpu;
1701 struct irq_info *info = info_for_irq(irq);
1702 int evtchn = info ? info->evtchn : 0;
1704 if (!VALID_EVTCHN(evtchn))
1707 if (!xen_support_evtchn_rebind())
1710 /* Send future instances of this interrupt to other vcpu. */
1711 bind_vcpu.port = evtchn;
1712 bind_vcpu.vcpu = tcpu;
1715 * Mask the event while changing the VCPU binding to prevent
1716 * it being delivered on an unexpected VCPU.
1718 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1721 * If this fails, it usually just indicates that we're dealing with a
1722 * virq or IPI channel, which don't actually need to be rebound. Ignore
1723 * it, but don't do the xenlinux-level rebind in that case.
1725 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1726 bind_evtchn_to_cpu(evtchn, tcpu);
1728 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1733 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1736 unsigned tcpu = cpumask_first_and(dest, cpu_online_mask);
1738 return rebind_irq_to_cpu(data->irq, tcpu);
1741 static void enable_dynirq(struct irq_data *data)
1743 struct irq_info *info = info_for_irq(data->irq);
1744 evtchn_port_t evtchn = info ? info->evtchn : 0;
1746 if (VALID_EVTCHN(evtchn))
1747 do_unmask(info, EVT_MASK_REASON_EXPLICIT);
1750 static void disable_dynirq(struct irq_data *data)
1752 struct irq_info *info = info_for_irq(data->irq);
1753 evtchn_port_t evtchn = info ? info->evtchn : 0;
1755 if (VALID_EVTCHN(evtchn))
1756 do_mask(info, EVT_MASK_REASON_EXPLICIT);
1759 static void ack_dynirq(struct irq_data *data)
1761 struct irq_info *info = info_for_irq(data->irq);
1762 evtchn_port_t evtchn = info ? info->evtchn : 0;
1764 if (!VALID_EVTCHN(evtchn))
1767 if (unlikely(irqd_is_setaffinity_pending(data)) &&
1768 likely(!irqd_irq_disabled(data))) {
1769 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1771 event_handler_exit(info);
1773 irq_move_masked_irq(data);
1775 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1777 event_handler_exit(info);
1780 static void mask_ack_dynirq(struct irq_data *data)
1782 disable_dynirq(data);
1786 static void lateeoi_ack_dynirq(struct irq_data *data)
1788 struct irq_info *info = info_for_irq(data->irq);
1789 evtchn_port_t evtchn = info ? info->evtchn : 0;
1791 if (!VALID_EVTCHN(evtchn))
1794 do_mask(info, EVT_MASK_REASON_EOI_PENDING);
1796 if (unlikely(irqd_is_setaffinity_pending(data)) &&
1797 likely(!irqd_irq_disabled(data))) {
1798 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1800 clear_evtchn(evtchn);
1802 irq_move_masked_irq(data);
1804 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1806 clear_evtchn(evtchn);
1809 static void lateeoi_mask_ack_dynirq(struct irq_data *data)
1811 struct irq_info *info = info_for_irq(data->irq);
1812 evtchn_port_t evtchn = info ? info->evtchn : 0;
1814 if (VALID_EVTCHN(evtchn)) {
1815 do_mask(info, EVT_MASK_REASON_EXPLICIT);
1820 static int retrigger_dynirq(struct irq_data *data)
1822 struct irq_info *info = info_for_irq(data->irq);
1823 evtchn_port_t evtchn = info ? info->evtchn : 0;
1825 if (!VALID_EVTCHN(evtchn))
1828 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1830 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1835 static void restore_pirqs(void)
1837 int pirq, rc, irq, gsi;
1838 struct physdev_map_pirq map_irq;
1839 struct irq_info *info;
1841 list_for_each_entry(info, &xen_irq_list_head, list) {
1842 if (info->type != IRQT_PIRQ)
1845 pirq = info->u.pirq.pirq;
1846 gsi = info->u.pirq.gsi;
1849 /* save/restore of PT devices doesn't work, so at this point the
1850 * only devices present are GSI based emulated devices */
1854 map_irq.domid = DOMID_SELF;
1855 map_irq.type = MAP_PIRQ_TYPE_GSI;
1856 map_irq.index = gsi;
1857 map_irq.pirq = pirq;
1859 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1861 pr_warn("xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1862 gsi, irq, pirq, rc);
1867 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1869 __startup_pirq(irq);
1873 static void restore_cpu_virqs(unsigned int cpu)
1875 struct evtchn_bind_virq bind_virq;
1876 int virq, irq, evtchn;
1878 for (virq = 0; virq < NR_VIRQS; virq++) {
1879 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1882 BUG_ON(virq_from_irq(irq) != virq);
1884 /* Get a new binding from Xen. */
1885 bind_virq.virq = virq;
1886 bind_virq.vcpu = cpu;
1887 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1890 evtchn = bind_virq.port;
1892 /* Record the new mapping. */
1893 (void)xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1894 bind_evtchn_to_cpu(evtchn, cpu);
1898 static void restore_cpu_ipis(unsigned int cpu)
1900 struct evtchn_bind_ipi bind_ipi;
1901 int ipi, irq, evtchn;
1903 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1904 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1907 BUG_ON(ipi_from_irq(irq) != ipi);
1909 /* Get a new binding from Xen. */
1910 bind_ipi.vcpu = cpu;
1911 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1914 evtchn = bind_ipi.port;
1916 /* Record the new mapping. */
1917 (void)xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
1918 bind_evtchn_to_cpu(evtchn, cpu);
1922 /* Clear an irq's pending state, in preparation for polling on it */
1923 void xen_clear_irq_pending(int irq)
1925 struct irq_info *info = info_for_irq(irq);
1926 evtchn_port_t evtchn = info ? info->evtchn : 0;
1928 if (VALID_EVTCHN(evtchn))
1929 event_handler_exit(info);
1931 EXPORT_SYMBOL(xen_clear_irq_pending);
1932 void xen_set_irq_pending(int irq)
1934 int evtchn = evtchn_from_irq(irq);
1936 if (VALID_EVTCHN(evtchn))
1940 bool xen_test_irq_pending(int irq)
1942 int evtchn = evtchn_from_irq(irq);
1945 if (VALID_EVTCHN(evtchn))
1946 ret = test_evtchn(evtchn);
1951 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1952 * the irq will be disabled so it won't deliver an interrupt. */
1953 void xen_poll_irq_timeout(int irq, u64 timeout)
1955 evtchn_port_t evtchn = evtchn_from_irq(irq);
1957 if (VALID_EVTCHN(evtchn)) {
1958 struct sched_poll poll;
1961 poll.timeout = timeout;
1962 set_xen_guest_handle(poll.ports, &evtchn);
1964 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1968 EXPORT_SYMBOL(xen_poll_irq_timeout);
1969 /* Poll waiting for an irq to become pending. In the usual case, the
1970 * irq will be disabled so it won't deliver an interrupt. */
1971 void xen_poll_irq(int irq)
1973 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1976 /* Check whether the IRQ line is shared with other guests. */
1977 int xen_test_irq_shared(int irq)
1979 struct irq_info *info = info_for_irq(irq);
1980 struct physdev_irq_status_query irq_status;
1985 irq_status.irq = info->u.pirq.pirq;
1987 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1989 return !(irq_status.flags & XENIRQSTAT_shared);
1991 EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1993 void xen_irq_resume(void)
1996 struct irq_info *info;
1998 /* New event-channel space is not 'live' yet. */
1999 xen_evtchn_mask_all();
2000 xen_evtchn_resume();
2002 /* No IRQ <-> event-channel mappings. */
2003 list_for_each_entry(info, &xen_irq_list_head, list)
2004 info->evtchn = 0; /* zap event-channel binding */
2006 clear_evtchn_to_irq_all();
2008 for_each_possible_cpu(cpu) {
2009 restore_cpu_virqs(cpu);
2010 restore_cpu_ipis(cpu);
2016 static struct irq_chip xen_dynamic_chip __read_mostly = {
2019 .irq_disable = disable_dynirq,
2020 .irq_mask = disable_dynirq,
2021 .irq_unmask = enable_dynirq,
2023 .irq_ack = ack_dynirq,
2024 .irq_mask_ack = mask_ack_dynirq,
2026 .irq_set_affinity = set_affinity_irq,
2027 .irq_retrigger = retrigger_dynirq,
2030 static struct irq_chip xen_lateeoi_chip __read_mostly = {
2031 /* The chip name needs to contain "xen-dyn" for irqbalance to work. */
2032 .name = "xen-dyn-lateeoi",
2034 .irq_disable = disable_dynirq,
2035 .irq_mask = disable_dynirq,
2036 .irq_unmask = enable_dynirq,
2038 .irq_ack = lateeoi_ack_dynirq,
2039 .irq_mask_ack = lateeoi_mask_ack_dynirq,
2041 .irq_set_affinity = set_affinity_irq,
2042 .irq_retrigger = retrigger_dynirq,
2045 static struct irq_chip xen_pirq_chip __read_mostly = {
2048 .irq_startup = startup_pirq,
2049 .irq_shutdown = shutdown_pirq,
2050 .irq_enable = enable_pirq,
2051 .irq_disable = disable_pirq,
2053 .irq_mask = disable_dynirq,
2054 .irq_unmask = enable_dynirq,
2056 .irq_ack = eoi_pirq,
2057 .irq_eoi = eoi_pirq,
2058 .irq_mask_ack = mask_ack_pirq,
2060 .irq_set_affinity = set_affinity_irq,
2062 .irq_retrigger = retrigger_dynirq,
2065 static struct irq_chip xen_percpu_chip __read_mostly = {
2066 .name = "xen-percpu",
2068 .irq_disable = disable_dynirq,
2069 .irq_mask = disable_dynirq,
2070 .irq_unmask = enable_dynirq,
2072 .irq_ack = ack_dynirq,
2075 int xen_set_callback_via(uint64_t via)
2077 struct xen_hvm_param a;
2078 a.domid = DOMID_SELF;
2079 a.index = HVM_PARAM_CALLBACK_IRQ;
2081 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
2083 EXPORT_SYMBOL_GPL(xen_set_callback_via);
2085 #ifdef CONFIG_XEN_PVHVM
2086 /* Vector callbacks are better than PCI interrupts to receive event
2087 * channel notifications because we can receive vector callbacks on any
2088 * vcpu and we don't need PCI support or APIC interactions. */
2089 void xen_callback_vector(void)
2092 uint64_t callback_via;
2093 if (xen_have_vector_callback) {
2094 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
2095 rc = xen_set_callback_via(callback_via);
2097 pr_err("Request for Xen HVM callback vector failed\n");
2098 xen_have_vector_callback = 0;
2101 pr_info("Xen HVM callback vector for event delivery is enabled\n");
2102 /* in the restore case the vector has already been allocated */
2103 if (!test_bit(HYPERVISOR_CALLBACK_VECTOR, used_vectors))
2104 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
2105 xen_hvm_callback_vector);
2109 void xen_callback_vector(void) {}
2112 static bool fifo_events = true;
2113 module_param(fifo_events, bool, 0);
2115 static int xen_evtchn_cpu_prepare(unsigned int cpu)
2119 xen_cpu_init_eoi(cpu);
2121 if (evtchn_ops->percpu_init)
2122 ret = evtchn_ops->percpu_init(cpu);
2127 static int xen_evtchn_cpu_dead(unsigned int cpu)
2131 if (evtchn_ops->percpu_deinit)
2132 ret = evtchn_ops->percpu_deinit(cpu);
2137 static int evtchn_cpu_notification(struct notifier_block *self,
2138 unsigned long action, void *hcpu)
2140 int cpu = (long)hcpu;
2144 case CPU_UP_PREPARE:
2145 ret = xen_evtchn_cpu_prepare(cpu);
2148 ret = xen_evtchn_cpu_dead(cpu);
2154 return ret < 0 ? NOTIFY_BAD : NOTIFY_OK;
2157 static struct notifier_block evtchn_cpu_notifier = {
2158 .notifier_call = evtchn_cpu_notification,
2161 void __init xen_init_IRQ(void)
2166 ret = xen_evtchn_fifo_init();
2168 xen_evtchn_2l_init();
2170 xen_cpu_init_eoi(smp_processor_id());
2172 register_cpu_notifier(&evtchn_cpu_notifier);
2174 evtchn_to_irq = kcalloc(EVTCHN_ROW(xen_evtchn_max_channels()),
2175 sizeof(*evtchn_to_irq), GFP_KERNEL);
2176 BUG_ON(!evtchn_to_irq);
2178 /* No event channels are 'live' right now. */
2179 xen_evtchn_mask_all();
2181 pirq_needs_eoi = pirq_needs_eoi_flag;
2184 if (xen_pv_domain()) {
2185 irq_ctx_init(smp_processor_id());
2186 if (xen_initial_domain())
2187 pci_xen_initial_domain();
2189 if (xen_feature(XENFEAT_hvm_callback_vector))
2190 xen_callback_vector();
2192 if (xen_hvm_domain()) {
2194 /* pci_xen_hvm_init must be called after native_init_IRQ so that
2195 * __acpi_register_gsi can point at the right function */
2199 struct physdev_pirq_eoi_gmfn eoi_gmfn;
2201 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
2202 eoi_gmfn.gmfn = virt_to_gfn(pirq_eoi_map);
2203 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
2204 /* TODO: No PVH support for PIRQ EOI */
2206 free_page((unsigned long) pirq_eoi_map);
2207 pirq_eoi_map = NULL;
2209 pirq_needs_eoi = pirq_check_eoi_map;