1 /******************************************************************************
4 * Xen event and interrupt services for x86 HVM guests.
6 * Copyright (c) 2002-2005, K A Fraser
7 * Copyright (c) 2005, Intel Corporation <xiaofeng.ling@intel.com>
8 * Copyright (c) 2012, Spectra Logic Corporation
10 * This file may be distributed separately from the Linux kernel, or
11 * incorporated into other software packages, subject to the following license:
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this source file (the "Software"), to deal in the Software without
15 * restriction, including without limitation the rights to use, copy, modify,
16 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/limits.h>
44 #include <sys/mutex.h>
45 #include <sys/interrupt.h>
48 #include <sys/refcount.h>
53 #include <machine/intr_machdep.h>
54 #include <x86/apicvar.h>
55 #include <x86/apicreg.h>
56 #include <machine/smp.h>
57 #include <machine/stdarg.h>
59 #include <machine/xen/synch_bitops.h>
61 #include <xen/xen-os.h>
63 #include <xen/hypervisor.h>
64 #include <xen/xen_intr.h>
65 #include <xen/evtchn/evtchnvar.h>
67 #include <dev/xen/xenpci/xenpcivar.h>
68 #include <dev/pci/pcivar.h>
74 static MALLOC_DEFINE(M_XENINTR, "xen_intr", "Xen Interrupt Services");
76 static u_int first_evtchn_irq;
79 * Per-cpu event channel processing state.
81 struct xen_intr_pcpu_data {
83 * The last event channel bitmap section (level one bit) processed.
84 * This is used to ensure we scan all ports before
85 * servicing an already servied port again.
87 u_int last_processed_l1i;
90 * The last event channel processed within the event channel
91 * bitmap being scanned.
93 u_int last_processed_l2i;
95 /** Pointer to this CPU's interrupt statistic counter. */
96 u_long *evtchn_intrcnt;
99 * A bitmap of ports that can be serviced from this CPU.
100 * A set bit means interrupt handling is enabled.
102 u_long evtchn_enabled[sizeof(u_long) * 8];
106 * Start the scan at port 0 by initializing the last scanned
107 * location as the highest numbered event channel port.
109 DPCPU_DEFINE_STATIC(struct xen_intr_pcpu_data, xen_intr_pcpu) = {
110 .last_processed_l1i = LONG_BIT - 1,
111 .last_processed_l2i = LONG_BIT - 1
114 DPCPU_DECLARE(struct vcpu_info *, vcpu_info);
116 #define XEN_INVALID_EVTCHN 0 /* Invalid event channel */
118 #define is_valid_evtchn(x) ((x) != XEN_INVALID_EVTCHN)
121 struct intsrc xi_intsrc;
122 enum evtchn_type xi_type;
123 int xi_cpu; /* VCPU for delivery. */
124 int xi_vector; /* Global isrc vector number. */
125 evtchn_port_t xi_port;
128 u_int xi_close:1; /* close on unbind? */
130 volatile u_int xi_refcount;
133 static void xen_intr_suspend(struct pic *);
134 static void xen_intr_resume(struct pic *, bool suspend_cancelled);
135 static void xen_intr_enable_source(struct intsrc *isrc);
136 static void xen_intr_disable_source(struct intsrc *isrc, int eoi);
137 static void xen_intr_eoi_source(struct intsrc *isrc);
138 static void xen_intr_enable_intr(struct intsrc *isrc);
139 static void xen_intr_disable_intr(struct intsrc *isrc);
140 static int xen_intr_vector(struct intsrc *isrc);
141 static int xen_intr_source_pending(struct intsrc *isrc);
142 static int xen_intr_config_intr(struct intsrc *isrc,
143 enum intr_trigger trig, enum intr_polarity pol);
144 static int xen_intr_assign_cpu(struct intsrc *isrc, u_int apic_id);
147 * PIC interface for all event channel port types except physical IRQs.
149 struct pic xen_intr_pic = {
150 .pic_enable_source = xen_intr_enable_source,
151 .pic_disable_source = xen_intr_disable_source,
152 .pic_eoi_source = xen_intr_eoi_source,
153 .pic_enable_intr = xen_intr_enable_intr,
154 .pic_disable_intr = xen_intr_disable_intr,
155 .pic_vector = xen_intr_vector,
156 .pic_source_pending = xen_intr_source_pending,
157 .pic_suspend = xen_intr_suspend,
158 .pic_resume = xen_intr_resume,
159 .pic_config_intr = xen_intr_config_intr,
160 .pic_assign_cpu = xen_intr_assign_cpu
163 static struct mtx xen_intr_isrc_lock;
164 static u_int xen_intr_auto_vector_count;
165 static struct xenisrc *xen_intr_port_to_isrc[NR_EVENT_CHANNELS];
167 /*------------------------- Private Functions --------------------------------*/
170 * Retrieve a handle for a Xen interrupt source.
172 * \param isrc A valid Xen interrupt source structure.
174 * \returns A handle suitable for use with xen_intr_isrc_from_handle()
175 * to retrieve the original Xen interrupt source structure.
178 static inline xen_intr_handle_t
179 xen_intr_handle_from_isrc(struct xenisrc *isrc)
185 * Lookup a Xen interrupt source object given an interrupt binding handle.
187 * \param handle A handle initialized by a previous call to
188 * xen_intr_bind_isrc().
190 * \returns A pointer to the Xen interrupt source object associated
191 * with the given interrupt handle. NULL if no association
194 static inline struct xenisrc *
195 xen_intr_isrc_from_handle(xen_intr_handle_t handle)
197 return ((struct xenisrc *)handle);
201 * Disable signal delivery for an event channel port on the
204 * \param port The event channel port to mask.
206 * This API is used to manage the port<=>CPU binding of event
209 * \note This operation does not preclude reception of an event
210 * for this event channel on another CPU. To mask the
211 * event channel globally, use evtchn_mask().
214 evtchn_cpu_mask_port(u_int cpu, evtchn_port_t port)
216 struct xen_intr_pcpu_data *pcpu;
218 pcpu = DPCPU_ID_PTR(cpu, xen_intr_pcpu);
219 xen_clear_bit(port, pcpu->evtchn_enabled);
223 * Enable signal delivery for an event channel port on the
226 * \param port The event channel port to unmask.
228 * This API is used to manage the port<=>CPU binding of event
231 * \note This operation does not guarantee that event delivery
232 * is enabled for this event channel port. The port must
233 * also be globally enabled. See evtchn_unmask().
236 evtchn_cpu_unmask_port(u_int cpu, evtchn_port_t port)
238 struct xen_intr_pcpu_data *pcpu;
240 pcpu = DPCPU_ID_PTR(cpu, xen_intr_pcpu);
241 xen_set_bit(port, pcpu->evtchn_enabled);
245 * Allocate and register a per-cpu Xen upcall interrupt counter.
247 * \param cpu The cpu for which to register this interrupt count.
250 xen_intr_intrcnt_add(u_int cpu)
252 char buf[MAXCOMLEN + 1];
253 struct xen_intr_pcpu_data *pcpu;
255 pcpu = DPCPU_ID_PTR(cpu, xen_intr_pcpu);
256 if (pcpu->evtchn_intrcnt != NULL)
259 snprintf(buf, sizeof(buf), "cpu%d:xen", cpu);
260 intrcnt_add(buf, &pcpu->evtchn_intrcnt);
264 * Search for an already allocated but currently unused Xen interrupt
267 * \param type Restrict the search to interrupt sources of the given
270 * \return A pointer to a free Xen interrupt source object or NULL.
272 static struct xenisrc *
273 xen_intr_find_unused_isrc(enum evtchn_type type)
277 KASSERT(mtx_owned(&xen_intr_isrc_lock), ("Evtchn isrc lock not held"));
279 for (isrc_idx = 0; isrc_idx < xen_intr_auto_vector_count; isrc_idx ++) {
280 struct xenisrc *isrc;
283 vector = first_evtchn_irq + isrc_idx;
284 isrc = (struct xenisrc *)intr_lookup_source(vector);
286 && isrc->xi_type == EVTCHN_TYPE_UNBOUND) {
287 KASSERT(isrc->xi_intsrc.is_handlers == 0,
288 ("Free evtchn still has handlers"));
289 isrc->xi_type = type;
297 * Allocate a Xen interrupt source object.
299 * \param type The type of interrupt source to create.
301 * \return A pointer to a newly allocated Xen interrupt source
304 static struct xenisrc *
305 xen_intr_alloc_isrc(enum evtchn_type type)
308 struct xenisrc *isrc;
311 KASSERT(mtx_owned(&xen_intr_isrc_lock), ("Evtchn alloc lock not held"));
313 if (xen_intr_auto_vector_count > NR_EVENT_CHANNELS) {
316 printf("%s: Event channels exhausted.\n", __func__);
321 vector = first_evtchn_irq + xen_intr_auto_vector_count;
322 xen_intr_auto_vector_count++;
324 KASSERT((intr_lookup_source(vector) == NULL),
325 ("Trying to use an already allocated vector"));
327 mtx_unlock(&xen_intr_isrc_lock);
328 isrc = malloc(sizeof(*isrc), M_XENINTR, M_WAITOK | M_ZERO);
329 isrc->xi_intsrc.is_pic = &xen_intr_pic;
330 isrc->xi_vector = vector;
331 isrc->xi_type = type;
332 intr_register_source(&isrc->xi_intsrc);
333 mtx_lock(&xen_intr_isrc_lock);
339 * Attempt to free an active Xen interrupt source object.
341 * \param isrc The interrupt source object to release.
343 * \returns EBUSY if the source is still in use, otherwise 0.
346 xen_intr_release_isrc(struct xenisrc *isrc)
349 mtx_lock(&xen_intr_isrc_lock);
350 KASSERT(isrc->xi_intsrc.is_handlers == 0,
351 ("Release called, but xenisrc still in use"));
352 evtchn_mask_port(isrc->xi_port);
353 evtchn_clear_port(isrc->xi_port);
355 /* Rebind port to CPU 0. */
356 evtchn_cpu_mask_port(isrc->xi_cpu, isrc->xi_port);
357 evtchn_cpu_unmask_port(0, isrc->xi_port);
359 if (isrc->xi_close != 0 && is_valid_evtchn(isrc->xi_port)) {
360 struct evtchn_close close = { .port = isrc->xi_port };
361 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
362 panic("EVTCHNOP_close failed");
365 xen_intr_port_to_isrc[isrc->xi_port] = NULL;
367 isrc->xi_type = EVTCHN_TYPE_UNBOUND;
369 isrc->xi_cookie = NULL;
370 mtx_unlock(&xen_intr_isrc_lock);
375 * Associate an interrupt handler with an already allocated local Xen
376 * event channel port.
378 * \param isrcp The returned Xen interrupt object associated with
379 * the specified local port.
380 * \param local_port The event channel to bind.
381 * \param type The event channel type of local_port.
382 * \param intr_owner The device making this bind request.
383 * \param filter An interrupt filter handler. Specify NULL
384 * to always dispatch to the ithread handler.
385 * \param handler An interrupt ithread handler. Optional (can
386 * specify NULL) if all necessary event actions
387 * are performed by filter.
388 * \param arg Argument to present to both filter and handler.
389 * \param irqflags Interrupt handler flags. See sys/bus.h.
390 * \param handlep Pointer to an opaque handle used to manage this
393 * \returns 0 on success, otherwise an errno.
396 xen_intr_bind_isrc(struct xenisrc **isrcp, evtchn_port_t local_port,
397 enum evtchn_type type, const char *intr_owner, driver_filter_t filter,
398 driver_intr_t handler, void *arg, enum intr_type flags,
399 xen_intr_handle_t *port_handlep)
401 struct xenisrc *isrc;
405 if (port_handlep == NULL) {
406 printf("%s: %s: Bad event handle\n", intr_owner, __func__);
410 mtx_lock(&xen_intr_isrc_lock);
411 isrc = xen_intr_find_unused_isrc(type);
413 isrc = xen_intr_alloc_isrc(type);
415 mtx_unlock(&xen_intr_isrc_lock);
419 isrc->xi_port = local_port;
420 xen_intr_port_to_isrc[local_port] = isrc;
421 refcount_init(&isrc->xi_refcount, 1);
422 mtx_unlock(&xen_intr_isrc_lock);
424 /* Assign the opaque handler */
425 *port_handlep = xen_intr_handle_from_isrc(isrc);
428 if (type == EVTCHN_TYPE_PORT) {
430 * By default all interrupts are assigned to vCPU#0
431 * unless specified otherwise, so shuffle them to balance
432 * the interrupt load.
434 xen_intr_assign_cpu(&isrc->xi_intsrc, intr_next_cpu(0));
438 if (filter == NULL && handler == NULL) {
440 * No filter/handler provided, leave the event channel
441 * masked and without a valid handler, the caller is
442 * in charge of setting that up.
448 error = xen_intr_add_handler(intr_owner, filter, handler, arg, flags,
451 xen_intr_release_isrc(isrc);
459 * Determine the event channel ports at the given section of the
460 * event port bitmap which have pending events for the given cpu.
462 * \param pcpu The Xen interrupt pcpu data for the cpu being queried.
463 * \param sh The Xen shared info area.
464 * \param idx The index of the section of the event channel bitmap to
467 * \returns A u_long with bits set for every event channel with pending
471 xen_intr_active_ports(struct xen_intr_pcpu_data *pcpu, shared_info_t *sh,
475 CTASSERT(sizeof(sh->evtchn_mask[0]) == sizeof(sh->evtchn_pending[0]));
476 CTASSERT(sizeof(sh->evtchn_mask[0]) == sizeof(pcpu->evtchn_enabled[0]));
477 CTASSERT(sizeof(sh->evtchn_mask) == sizeof(sh->evtchn_pending));
478 CTASSERT(sizeof(sh->evtchn_mask) == sizeof(pcpu->evtchn_enabled));
479 return (sh->evtchn_pending[idx]
480 & ~sh->evtchn_mask[idx]
481 & pcpu->evtchn_enabled[idx]);
485 * Interrupt handler for processing all Xen event channel events.
487 * \param trap_frame The trap frame context for the current interrupt.
490 xen_intr_handle_upcall(struct trapframe *trap_frame)
492 u_int l1i, l2i, port, cpu __diagused;
493 u_long masked_l1, masked_l2;
494 struct xenisrc *isrc;
497 struct xen_intr_pcpu_data *pc;
501 * Disable preemption in order to always check and fire events
506 cpu = PCPU_GET(cpuid);
507 pc = DPCPU_PTR(xen_intr_pcpu);
508 s = HYPERVISOR_shared_info;
509 v = DPCPU_GET(vcpu_info);
511 if (!xen_has_percpu_evtchn()) {
512 KASSERT((cpu == 0), ("Fired PCI event callback on wrong CPU"));
515 v->evtchn_upcall_pending = 0;
518 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
519 /* Clear master flag /before/ clearing selector flag. */
524 l1 = atomic_readandclear_long(&v->evtchn_pending_sel);
526 l1i = pc->last_processed_l1i;
527 l2i = pc->last_processed_l2i;
528 (*pc->evtchn_intrcnt)++;
531 l1i = (l1i + 1) % LONG_BIT;
532 masked_l1 = l1 & ((~0UL) << l1i);
534 if (masked_l1 == 0) {
536 * if we masked out all events, wrap around
543 l1i = ffsl(masked_l1) - 1;
546 l2 = xen_intr_active_ports(pc, s, l1i);
548 l2i = (l2i + 1) % LONG_BIT;
549 masked_l2 = l2 & ((~0UL) << l2i);
551 if (masked_l2 == 0) {
552 /* if we masked out all events, move on */
556 l2i = ffsl(masked_l2) - 1;
559 port = (l1i * LONG_BIT) + l2i;
560 synch_clear_bit(port, &s->evtchn_pending[0]);
562 isrc = xen_intr_port_to_isrc[port];
563 if (__predict_false(isrc == NULL))
566 /* Make sure we are firing on the right vCPU */
567 KASSERT((isrc->xi_cpu == PCPU_GET(cpuid)),
568 ("Received unexpected event on vCPU#%d, event bound to vCPU#%d",
569 PCPU_GET(cpuid), isrc->xi_cpu));
571 intr_execute_handlers(&isrc->xi_intsrc, trap_frame);
574 * If this is the final port processed,
575 * we'll pick up here+1 next time.
577 pc->last_processed_l1i = l1i;
578 pc->last_processed_l2i = l2i;
580 } while (l2i != LONG_BIT - 1);
582 l2 = xen_intr_active_ports(pc, s, l1i);
585 * We handled all ports, so we can clear the
592 if (xen_evtchn_needs_ack)
599 xen_intr_init(void *dummy __unused)
601 shared_info_t *s = HYPERVISOR_shared_info;
602 struct xen_intr_pcpu_data *pcpu;
608 mtx_init(&xen_intr_isrc_lock, "xen-irq-lock", NULL, MTX_DEF);
611 * Set the per-cpu mask of CPU#0 to enable all, since by default all
612 * event channels are bound to CPU#0.
615 pcpu = DPCPU_ID_PTR(i, xen_intr_pcpu);
616 memset(pcpu->evtchn_enabled, i == 0 ? ~0 : 0,
617 sizeof(pcpu->evtchn_enabled));
620 for (i = 0; i < nitems(s->evtchn_mask); i++)
621 atomic_store_rel_long(&s->evtchn_mask[i], ~0);
623 intr_register_pic(&xen_intr_pic);
626 printf("Xen interrupt system initialized\n");
630 SYSINIT(xen_intr_init, SI_SUB_INTR, SI_ORDER_SECOND, xen_intr_init, NULL);
633 xen_intrcnt_init(void *dummy __unused)
641 * Register interrupt count manually as we aren't guaranteed to see a
642 * call to xen_intr_assign_cpu() before our first interrupt.
645 xen_intr_intrcnt_add(i);
647 SYSINIT(xen_intrcnt_init, SI_SUB_INTR, SI_ORDER_MIDDLE, xen_intrcnt_init, NULL);
650 xen_intr_alloc_irqs(void)
653 if (num_io_irqs > UINT_MAX - NR_EVENT_CHANNELS)
654 panic("IRQ allocation overflow (num_msi_irqs too high?)");
655 first_evtchn_irq = num_io_irqs;
656 num_io_irqs += NR_EVENT_CHANNELS;
659 /*--------------------------- Common PIC Functions ---------------------------*/
661 * Prepare this PIC for system suspension.
664 xen_intr_suspend(struct pic *unused)
669 xen_rebind_ipi(struct xenisrc *isrc)
672 int cpu = isrc->xi_cpu;
673 int vcpu_id = pcpu_find(cpu)->pc_vcpu_id;
675 struct evtchn_bind_ipi bind_ipi = { .vcpu = vcpu_id };
677 error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
680 panic("unable to rebind xen IPI: %d", error);
682 isrc->xi_port = bind_ipi.port;
684 xen_intr_port_to_isrc[bind_ipi.port] = isrc;
686 error = xen_intr_assign_cpu(&isrc->xi_intsrc,
689 panic("unable to bind xen IPI to CPU#%d: %d",
692 evtchn_unmask_port(bind_ipi.port);
694 panic("Resume IPI event channel on UP");
699 xen_rebind_virq(struct xenisrc *isrc)
701 int cpu = isrc->xi_cpu;
702 int vcpu_id = pcpu_find(cpu)->pc_vcpu_id;
704 struct evtchn_bind_virq bind_virq = { .virq = isrc->xi_virq,
707 error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
710 panic("unable to rebind xen VIRQ#%d: %d", isrc->xi_virq, error);
712 isrc->xi_port = bind_virq.port;
714 xen_intr_port_to_isrc[bind_virq.port] = isrc;
717 error = xen_intr_assign_cpu(&isrc->xi_intsrc,
720 panic("unable to bind xen VIRQ#%d to CPU#%d: %d",
721 isrc->xi_virq, cpu, error);
724 evtchn_unmask_port(bind_virq.port);
728 * Return this PIC to service after being suspended.
731 xen_intr_resume(struct pic *unused, bool suspend_cancelled)
733 shared_info_t *s = HYPERVISOR_shared_info;
734 struct xenisrc *isrc;
738 if (suspend_cancelled)
741 /* Reset the per-CPU masks */
743 struct xen_intr_pcpu_data *pcpu;
745 pcpu = DPCPU_ID_PTR(i, xen_intr_pcpu);
746 memset(pcpu->evtchn_enabled, i == 0 ? ~0 : 0,
747 sizeof(pcpu->evtchn_enabled));
750 /* Mask all event channels. */
751 for (i = 0; i < nitems(s->evtchn_mask); i++)
752 atomic_store_rel_long(&s->evtchn_mask[i], ~0);
754 /* Remove port -> isrc mappings */
755 memset(xen_intr_port_to_isrc, 0, sizeof(xen_intr_port_to_isrc));
757 /* Free unused isrcs and rebind VIRQs and IPIs */
758 for (isrc_idx = 0; isrc_idx < xen_intr_auto_vector_count; isrc_idx++) {
761 vector = first_evtchn_irq + isrc_idx;
762 isrc = (struct xenisrc *)intr_lookup_source(vector);
765 switch (isrc->xi_type) {
766 case EVTCHN_TYPE_IPI:
767 xen_rebind_ipi(isrc);
769 case EVTCHN_TYPE_VIRQ:
770 xen_rebind_virq(isrc);
780 * Disable a Xen interrupt source.
782 * \param isrc The interrupt source to disable.
785 xen_intr_disable_intr(struct intsrc *base_isrc)
787 struct xenisrc *isrc = (struct xenisrc *)base_isrc;
789 evtchn_mask_port(isrc->xi_port);
793 * Determine the global interrupt vector number for
794 * a Xen interrupt source.
796 * \param isrc The interrupt source to query.
798 * \return The vector number corresponding to the given interrupt source.
801 xen_intr_vector(struct intsrc *base_isrc)
803 struct xenisrc *isrc = (struct xenisrc *)base_isrc;
805 return (isrc->xi_vector);
809 * Determine whether or not interrupt events are pending on the
810 * the given interrupt source.
812 * \param isrc The interrupt source to query.
814 * \returns 0 if no events are pending, otherwise non-zero.
817 xen_intr_source_pending(struct intsrc *isrc)
820 * EventChannels are edge triggered and never masked.
821 * There can be no pending events.
827 * Perform configuration of an interrupt source.
829 * \param isrc The interrupt source to configure.
830 * \param trig Edge or level.
831 * \param pol Active high or low.
833 * \returns 0 if no events are pending, otherwise non-zero.
836 xen_intr_config_intr(struct intsrc *isrc, enum intr_trigger trig,
837 enum intr_polarity pol)
839 /* Configuration is only possible via the evtchn apis. */
844 * Configure CPU affinity for interrupt source event delivery.
846 * \param isrc The interrupt source to configure.
847 * \param apic_id The apic id of the CPU for handling future events.
849 * \returns 0 if successful, otherwise an errno.
852 xen_intr_assign_cpu(struct intsrc *base_isrc, u_int apic_id)
855 struct evtchn_bind_vcpu bind_vcpu;
856 struct xenisrc *isrc;
857 u_int to_cpu, vcpu_id;
860 if (!xen_has_percpu_evtchn())
863 to_cpu = apic_cpuid(apic_id);
864 vcpu_id = pcpu_find(to_cpu)->pc_vcpu_id;
866 mtx_lock(&xen_intr_isrc_lock);
867 isrc = (struct xenisrc *)base_isrc;
868 if (!is_valid_evtchn(isrc->xi_port)) {
869 mtx_unlock(&xen_intr_isrc_lock);
874 * Mask the event channel while binding it to prevent interrupt
875 * delivery with an inconsistent state in isrc->xi_cpu.
877 masked = evtchn_test_and_set_mask(isrc->xi_port);
878 if ((isrc->xi_type == EVTCHN_TYPE_VIRQ) ||
879 (isrc->xi_type == EVTCHN_TYPE_IPI)) {
881 * Virtual IRQs are associated with a cpu by
882 * the Hypervisor at evtchn_bind_virq time, so
883 * all we need to do is update the per-CPU masks.
885 evtchn_cpu_mask_port(isrc->xi_cpu, isrc->xi_port);
886 isrc->xi_cpu = to_cpu;
887 evtchn_cpu_unmask_port(isrc->xi_cpu, isrc->xi_port);
891 bind_vcpu.port = isrc->xi_port;
892 bind_vcpu.vcpu = vcpu_id;
894 error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu);
895 if (isrc->xi_cpu != to_cpu) {
897 /* Commit to new binding by removing the old one. */
898 evtchn_cpu_mask_port(isrc->xi_cpu, isrc->xi_port);
899 isrc->xi_cpu = to_cpu;
900 evtchn_cpu_unmask_port(isrc->xi_cpu, isrc->xi_port);
906 evtchn_unmask_port(isrc->xi_port);
907 mtx_unlock(&xen_intr_isrc_lock);
914 /*------------------- Virtual Interrupt Source PIC Functions -----------------*/
916 * Mask a level triggered interrupt source.
918 * \param isrc The interrupt source to mask (if necessary).
919 * \param eoi If non-zero, perform any necessary end-of-interrupt
923 xen_intr_disable_source(struct intsrc *base_isrc, int eoi)
925 struct xenisrc *isrc;
927 isrc = (struct xenisrc *)base_isrc;
930 * NB: checking if the event channel is already masked is
931 * needed because the event channel user-space device
932 * masks event channels on its filter as part of its
933 * normal operation, and those shouldn't be automatically
934 * unmasked by the generic interrupt code. The event channel
935 * device will unmask them when needed.
937 isrc->xi_masked = !!evtchn_test_and_set_mask(isrc->xi_port);
941 * Unmask a level triggered interrupt source.
943 * \param isrc The interrupt source to unmask (if necessary).
946 xen_intr_enable_source(struct intsrc *base_isrc)
948 struct xenisrc *isrc;
950 isrc = (struct xenisrc *)base_isrc;
952 if (isrc->xi_masked == 0)
953 evtchn_unmask_port(isrc->xi_port);
957 * Perform any necessary end-of-interrupt acknowledgements.
959 * \param isrc The interrupt source to EOI.
962 xen_intr_eoi_source(struct intsrc *base_isrc)
967 * Enable and unmask the interrupt source.
969 * \param isrc The interrupt source to enable.
972 xen_intr_enable_intr(struct intsrc *base_isrc)
974 struct xenisrc *isrc = (struct xenisrc *)base_isrc;
976 evtchn_unmask_port(isrc->xi_port);
979 /*--------------------------- Public Functions -------------------------------*/
980 /*------- API comments for these methods can be found in xen/xenintr.h -------*/
982 xen_intr_bind_local_port(device_t dev, evtchn_port_t local_port,
983 driver_filter_t filter, driver_intr_t handler, void *arg,
984 enum intr_type flags, xen_intr_handle_t *port_handlep)
986 struct xenisrc *isrc;
989 error = xen_intr_bind_isrc(&isrc, local_port, EVTCHN_TYPE_PORT,
990 device_get_nameunit(dev), filter, handler, arg, flags,
996 * The Event Channel API didn't open this port, so it is not
997 * responsible for closing it automatically on unbind.
1004 xen_intr_alloc_and_bind_local_port(device_t dev, u_int remote_domain,
1005 driver_filter_t filter, driver_intr_t handler, void *arg,
1006 enum intr_type flags, xen_intr_handle_t *port_handlep)
1008 struct xenisrc *isrc;
1009 struct evtchn_alloc_unbound alloc_unbound;
1012 alloc_unbound.dom = DOMID_SELF;
1013 alloc_unbound.remote_dom = remote_domain;
1014 error = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
1018 * XXX Trap Hypercall error code Linuxisms in
1019 * the HYPERCALL layer.
1024 error = xen_intr_bind_isrc(&isrc, alloc_unbound.port, EVTCHN_TYPE_PORT,
1025 device_get_nameunit(dev), filter, handler, arg, flags,
1028 evtchn_close_t close = { .port = alloc_unbound.port };
1029 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
1030 panic("EVTCHNOP_close failed");
1039 xen_intr_bind_remote_port(device_t dev, u_int remote_domain,
1040 u_int remote_port, driver_filter_t filter, driver_intr_t handler,
1041 void *arg, enum intr_type flags, xen_intr_handle_t *port_handlep)
1043 struct xenisrc *isrc;
1044 struct evtchn_bind_interdomain bind_interdomain;
1047 bind_interdomain.remote_dom = remote_domain;
1048 bind_interdomain.remote_port = remote_port;
1049 error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
1053 * XXX Trap Hypercall error code Linuxisms in
1054 * the HYPERCALL layer.
1059 error = xen_intr_bind_isrc(&isrc, bind_interdomain.local_port,
1060 EVTCHN_TYPE_PORT, device_get_nameunit(dev), filter, handler, arg,
1061 flags, port_handlep);
1063 evtchn_close_t close = { .port = bind_interdomain.local_port };
1064 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
1065 panic("EVTCHNOP_close failed");
1070 * The Event Channel API opened this port, so it is
1071 * responsible for closing it automatically on unbind.
1078 xen_intr_bind_virq(device_t dev, u_int virq, u_int cpu,
1079 driver_filter_t filter, driver_intr_t handler, void *arg,
1080 enum intr_type flags, xen_intr_handle_t *port_handlep)
1082 int vcpu_id = pcpu_find(cpu)->pc_vcpu_id;
1083 struct xenisrc *isrc;
1084 struct evtchn_bind_virq bind_virq = { .virq = virq, .vcpu = vcpu_id };
1088 error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq, &bind_virq);
1091 * XXX Trap Hypercall error code Linuxisms in
1092 * the HYPERCALL layer.
1097 error = xen_intr_bind_isrc(&isrc, bind_virq.port, EVTCHN_TYPE_VIRQ,
1098 device_get_nameunit(dev), filter, handler, arg, flags,
1103 error = intr_event_bind(isrc->xi_intsrc.is_event, cpu);
1107 evtchn_close_t close = { .port = bind_virq.port };
1109 xen_intr_unbind(*port_handlep);
1110 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
1111 panic("EVTCHNOP_close failed");
1116 if (isrc->xi_cpu != cpu) {
1118 * Too early in the boot process for the generic interrupt
1119 * code to perform the binding. Update our event channel
1120 * masks manually so events can't fire on the wrong cpu
1121 * during AP startup.
1123 xen_intr_assign_cpu(&isrc->xi_intsrc, cpu_apic_ids[cpu]);
1128 * The Event Channel API opened this port, so it is
1129 * responsible for closing it automatically on unbind.
1132 isrc->xi_virq = virq;
1138 xen_intr_alloc_and_bind_ipi(u_int cpu, driver_filter_t filter,
1139 enum intr_type flags, xen_intr_handle_t *port_handlep)
1142 int vcpu_id = pcpu_find(cpu)->pc_vcpu_id;
1143 struct xenisrc *isrc;
1144 struct evtchn_bind_ipi bind_ipi = { .vcpu = vcpu_id };
1145 /* Same size as the one used by intr_handler->ih_name. */
1146 char name[MAXCOMLEN + 1];
1150 error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi, &bind_ipi);
1153 * XXX Trap Hypercall error code Linuxisms in
1154 * the HYPERCALL layer.
1159 snprintf(name, sizeof(name), "cpu%u", cpu);
1161 error = xen_intr_bind_isrc(&isrc, bind_ipi.port, EVTCHN_TYPE_IPI,
1162 name, filter, NULL, NULL, flags, port_handlep);
1164 evtchn_close_t close = { .port = bind_ipi.port };
1166 xen_intr_unbind(*port_handlep);
1167 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
1168 panic("EVTCHNOP_close failed");
1172 if (isrc->xi_cpu != cpu) {
1174 * Too early in the boot process for the generic interrupt
1175 * code to perform the binding. Update our event channel
1176 * masks manually so events can't fire on the wrong cpu
1177 * during AP startup.
1179 xen_intr_assign_cpu(&isrc->xi_intsrc, cpu_apic_ids[cpu]);
1183 * The Event Channel API opened this port, so it is
1184 * responsible for closing it automatically on unbind.
1189 return (EOPNOTSUPP);
1194 xen_intr_describe(xen_intr_handle_t port_handle, const char *fmt, ...)
1196 char descr[MAXCOMLEN + 1];
1197 struct xenisrc *isrc;
1200 isrc = xen_intr_isrc_from_handle(port_handle);
1205 vsnprintf(descr, sizeof(descr), fmt, ap);
1207 return (intr_describe(isrc->xi_vector, isrc->xi_cookie, descr));
1211 xen_intr_unbind(xen_intr_handle_t *port_handlep)
1213 struct xenisrc *isrc;
1215 KASSERT(port_handlep != NULL,
1216 ("NULL xen_intr_handle_t passed to %s", __func__));
1218 isrc = xen_intr_isrc_from_handle(*port_handlep);
1219 *port_handlep = NULL;
1223 mtx_lock(&xen_intr_isrc_lock);
1224 if (refcount_release(&isrc->xi_refcount) == 0) {
1225 mtx_unlock(&xen_intr_isrc_lock);
1228 mtx_unlock(&xen_intr_isrc_lock);
1230 if (isrc->xi_cookie != NULL)
1231 intr_remove_handler(isrc->xi_cookie);
1232 xen_intr_release_isrc(isrc);
1236 xen_intr_signal(xen_intr_handle_t handle)
1238 struct xenisrc *isrc;
1240 isrc = xen_intr_isrc_from_handle(handle);
1242 KASSERT(isrc->xi_type == EVTCHN_TYPE_PORT ||
1243 isrc->xi_type == EVTCHN_TYPE_IPI,
1244 ("evtchn_signal on something other than a local port"));
1245 struct evtchn_send send = { .port = isrc->xi_port };
1246 (void)HYPERVISOR_event_channel_op(EVTCHNOP_send, &send);
1251 xen_intr_port(xen_intr_handle_t handle)
1253 struct xenisrc *isrc;
1255 isrc = xen_intr_isrc_from_handle(handle);
1259 return (isrc->xi_port);
1263 xen_intr_add_handler(const char *name, driver_filter_t filter,
1264 driver_intr_t handler, void *arg, enum intr_type flags,
1265 xen_intr_handle_t handle)
1267 struct xenisrc *isrc;
1270 isrc = xen_intr_isrc_from_handle(handle);
1271 if (isrc == NULL || isrc->xi_cookie != NULL)
1274 error = intr_add_handler(name, isrc->xi_vector,filter, handler, arg,
1275 flags|INTR_EXCL, &isrc->xi_cookie, 0);
1277 printf("%s: %s: add handler failed: %d\n", name, __func__,
1284 xen_intr_get_evtchn_from_port(evtchn_port_t port, xen_intr_handle_t *handlep)
1287 if (!is_valid_evtchn(port) || port >= NR_EVENT_CHANNELS)
1290 if (handlep == NULL) {
1294 mtx_lock(&xen_intr_isrc_lock);
1295 if (xen_intr_port_to_isrc[port] == NULL) {
1296 mtx_unlock(&xen_intr_isrc_lock);
1299 refcount_acquire(&xen_intr_port_to_isrc[port]->xi_refcount);
1300 mtx_unlock(&xen_intr_isrc_lock);
1302 /* Assign the opaque handler */
1303 *handlep = xen_intr_handle_from_isrc(xen_intr_port_to_isrc[port]);
1310 xen_intr_print_type(enum evtchn_type type)
1312 static const char *evtchn_type_to_string[EVTCHN_TYPE_COUNT] = {
1313 [EVTCHN_TYPE_UNBOUND] = "UNBOUND",
1314 [EVTCHN_TYPE_VIRQ] = "VIRQ",
1315 [EVTCHN_TYPE_IPI] = "IPI",
1316 [EVTCHN_TYPE_PORT] = "PORT",
1319 if (type >= EVTCHN_TYPE_COUNT)
1322 return (evtchn_type_to_string[type]);
1326 xen_intr_dump_port(struct xenisrc *isrc)
1328 struct xen_intr_pcpu_data *pcpu;
1329 shared_info_t *s = HYPERVISOR_shared_info;
1332 db_printf("Port %d Type: %s\n",
1333 isrc->xi_port, xen_intr_print_type(isrc->xi_type));
1334 if (isrc->xi_type == EVTCHN_TYPE_VIRQ)
1335 db_printf("\tVirq: %d\n", isrc->xi_virq);
1337 db_printf("\tMasked: %d Pending: %d\n",
1338 !!xen_test_bit(isrc->xi_port, &s->evtchn_mask[0]),
1339 !!xen_test_bit(isrc->xi_port, &s->evtchn_pending[0]));
1341 db_printf("\tPer-CPU Masks: ");
1343 pcpu = DPCPU_ID_PTR(i, xen_intr_pcpu);
1344 db_printf("cpu#%d: %d ", i,
1345 !!xen_test_bit(isrc->xi_port, pcpu->evtchn_enabled));
1350 DB_SHOW_COMMAND(xen_evtchn, db_show_xen_evtchn)
1354 if (!xen_domain()) {
1355 db_printf("Only available on Xen guests\n");
1359 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
1360 struct xenisrc *isrc;
1362 isrc = xen_intr_port_to_isrc[i];
1366 xen_intr_dump_port(isrc);