]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/xen/xen_intr.c
xen/intr: stop passing shared_info_t to xen_intr_active_ports()
[FreeBSD/FreeBSD.git] / sys / x86 / xen / xen_intr.c
1 /******************************************************************************
2  * xen_intr.c
3  *
4  * Xen event and interrupt services for x86 HVM guests.
5  *
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
9  * Copyright © 2021-2023, Elliott Mitchell
10  *
11  * This file may be distributed separately from the Linux kernel, or
12  * incorporated into other software packages, subject to the following license:
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this source file (the "Software"), to deal in the Software without
16  * restriction, including without limitation the rights to use, copy, modify,
17  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18  * and to permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * IN THE SOFTWARE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include "opt_ddb.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/limits.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/interrupt.h>
47 #include <sys/pcpu.h>
48 #include <sys/smp.h>
49 #include <sys/refcount.h>
50
51 #include <vm/vm.h>
52 #include <vm/pmap.h>
53
54 #include <machine/intr_machdep.h>
55 #include <x86/apicvar.h>
56 #include <x86/apicreg.h>
57 #include <machine/smp.h>
58 #include <machine/stdarg.h>
59
60 #include <xen/xen-os.h>
61 #include <xen/hvm.h>
62 #include <xen/hypervisor.h>
63 #include <xen/xen_intr.h>
64 #include <xen/evtchn/evtchnvar.h>
65
66 #include <dev/xen/xenpci/xenpcivar.h>
67 #include <dev/pci/pcivar.h>
68
69 #ifdef DDB
70 #include <ddb/ddb.h>
71 #endif
72
73 /* The code below is the implementation of 2L event channels. */
74 #define NR_EVENT_CHANNELS EVTCHN_2L_NR_CHANNELS
75
76 static MALLOC_DEFINE(M_XENINTR, "xen_intr", "Xen Interrupt Services");
77
78 static u_int first_evtchn_irq;
79
80 /**
81  * Per-cpu event channel processing state.
82  */
83 struct xen_intr_pcpu_data {
84         /**
85          * The last event channel bitmap section (level one bit) processed.
86          * This is used to ensure we scan all ports before
87          * servicing an already servied port again.
88          */
89         u_int   last_processed_l1i;
90
91         /**
92          * The last event channel processed within the event channel
93          * bitmap being scanned.
94          */
95         u_int   last_processed_l2i;
96
97         /** Pointer to this CPU's interrupt statistic counter. */
98         u_long *evtchn_intrcnt;
99
100         /**
101          * A bitmap of ports that can be serviced from this CPU.
102          * A set bit means interrupt handling is enabled.
103          */
104         u_long  evtchn_enabled[sizeof(u_long) * 8];
105 };
106
107 /*
108  * Start the scan at port 0 by initializing the last scanned
109  * location as the highest numbered event channel port.
110  */
111 DPCPU_DEFINE_STATIC(struct xen_intr_pcpu_data, xen_intr_pcpu) = {
112         .last_processed_l1i = LONG_BIT - 1,
113         .last_processed_l2i = LONG_BIT - 1
114 };
115
116 DPCPU_DECLARE(struct vcpu_info *, vcpu_info);
117
118 #define INVALID_EVTCHN          (~(evtchn_port_t)0) /* Invalid event channel */
119 #define is_valid_evtchn(x)      ((uintmax_t)(x) < NR_EVENT_CHANNELS)
120
121 struct xenisrc {
122         struct intsrc   xi_intsrc;
123         enum evtchn_type xi_type;
124         int             xi_cpu;         /* VCPU for delivery. */
125         int             xi_vector;      /* Global isrc vector number. */
126         evtchn_port_t   xi_port;
127         int             xi_virq;
128         void            *xi_cookie;
129         u_int           xi_close:1;     /* close on unbind? */
130         u_int           xi_masked:1;
131         volatile u_int  xi_refcount;
132 };
133
134 static void     xen_intr_suspend(struct pic *);
135 static void     xen_intr_resume(struct pic *, bool suspend_cancelled);
136 static void     xen_intr_enable_source(struct intsrc *isrc);
137 static void     xen_intr_disable_source(struct intsrc *isrc, int eoi);
138 static void     xen_intr_eoi_source(struct intsrc *isrc);
139 static void     xen_intr_enable_intr(struct intsrc *isrc);
140 static void     xen_intr_disable_intr(struct intsrc *isrc);
141 static int      xen_intr_vector(struct intsrc *isrc);
142 static int      xen_intr_source_pending(struct intsrc *isrc);
143 static int      xen_intr_config_intr(struct intsrc *isrc,
144                      enum intr_trigger trig, enum intr_polarity pol);
145 static int      xen_intr_assign_cpu(struct intsrc *isrc, u_int apic_id);
146
147 /**
148  * PIC interface for all event channel port types except physical IRQs.
149  */
150 struct pic xen_intr_pic = {
151         .pic_enable_source  = xen_intr_enable_source,
152         .pic_disable_source = xen_intr_disable_source,
153         .pic_eoi_source     = xen_intr_eoi_source,
154         .pic_enable_intr    = xen_intr_enable_intr,
155         .pic_disable_intr   = xen_intr_disable_intr,
156         .pic_vector         = xen_intr_vector,
157         .pic_source_pending = xen_intr_source_pending,
158         .pic_suspend        = xen_intr_suspend,
159         .pic_resume         = xen_intr_resume,
160         .pic_config_intr    = xen_intr_config_intr,
161         .pic_assign_cpu     = xen_intr_assign_cpu
162 };
163
164 static struct mtx        xen_intr_isrc_lock;
165 static u_int             xen_intr_auto_vector_count;
166 static struct xenisrc   *xen_intr_port_to_isrc[NR_EVENT_CHANNELS];
167
168 /*------------------------- Private Functions --------------------------------*/
169
170 /**
171  * Retrieve a handle for a Xen interrupt source.
172  *
173  * \param isrc  A valid Xen interrupt source structure.
174  *
175  * \returns  A handle suitable for use with xen_intr_isrc_from_handle()
176  *           to retrieve the original Xen interrupt source structure.
177  */
178
179 static inline xen_intr_handle_t
180 xen_intr_handle_from_isrc(struct xenisrc *isrc)
181 {
182         return (isrc);
183 }
184
185 /**
186  * Lookup a Xen interrupt source object given an interrupt binding handle.
187  *
188  * \param handle  A handle initialized by a previous call to
189  *                xen_intr_bind_isrc().
190  *
191  * \returns  A pointer to the Xen interrupt source object associated
192  *           with the given interrupt handle.  NULL if no association
193  *           currently exists.
194  */
195 static inline struct xenisrc *
196 xen_intr_isrc_from_handle(xen_intr_handle_t handle)
197 {
198         return ((struct xenisrc *)handle);
199 }
200
201 /**
202  * Disable signal delivery for an event channel port on the
203  * specified CPU.
204  *
205  * \param port  The event channel port to mask.
206  *
207  * This API is used to manage the port<=>CPU binding of event
208  * channel handlers.
209  *
210  * \note  This operation does not preclude reception of an event
211  *        for this event channel on another CPU.  To mask the
212  *        event channel globally, use evtchn_mask().
213  */
214 static inline void
215 evtchn_cpu_mask_port(u_int cpu, evtchn_port_t port)
216 {
217         struct xen_intr_pcpu_data *pcpu;
218
219         pcpu = DPCPU_ID_PTR(cpu, xen_intr_pcpu);
220         xen_clear_bit(port, pcpu->evtchn_enabled);
221 }
222
223 /**
224  * Enable signal delivery for an event channel port on the
225  * specified CPU.
226  *
227  * \param port  The event channel port to unmask.
228  *
229  * This API is used to manage the port<=>CPU binding of event
230  * channel handlers.
231  *
232  * \note  This operation does not guarantee that event delivery
233  *        is enabled for this event channel port.  The port must
234  *        also be globally enabled.  See evtchn_unmask().
235  */
236 static inline void
237 evtchn_cpu_unmask_port(u_int cpu, evtchn_port_t port)
238 {
239         struct xen_intr_pcpu_data *pcpu;
240
241         pcpu = DPCPU_ID_PTR(cpu, xen_intr_pcpu);
242         xen_set_bit(port, pcpu->evtchn_enabled);
243 }
244
245 /**
246  * Allocate and register a per-cpu Xen upcall interrupt counter.
247  *
248  * \param cpu  The cpu for which to register this interrupt count.
249  */
250 static void
251 xen_intr_intrcnt_add(u_int cpu)
252 {
253         char buf[MAXCOMLEN + 1];
254         struct xen_intr_pcpu_data *pcpu;
255
256         pcpu = DPCPU_ID_PTR(cpu, xen_intr_pcpu);
257         if (pcpu->evtchn_intrcnt != NULL)
258                 return;
259
260         snprintf(buf, sizeof(buf), "cpu%d:xen", cpu);
261         intrcnt_add(buf, &pcpu->evtchn_intrcnt);
262 }
263
264 /**
265  * Search for an already allocated but currently unused Xen interrupt
266  * source object.
267  *
268  * \param type  Restrict the search to interrupt sources of the given
269  *              type.
270  *
271  * \return  A pointer to a free Xen interrupt source object or NULL.
272  */
273 static struct xenisrc *
274 xen_intr_find_unused_isrc(enum evtchn_type type)
275 {
276         int isrc_idx;
277
278         KASSERT(mtx_owned(&xen_intr_isrc_lock), ("Evtchn isrc lock not held"));
279
280         for (isrc_idx = 0; isrc_idx < xen_intr_auto_vector_count; isrc_idx ++) {
281                 struct xenisrc *isrc;
282                 u_int vector;
283
284                 vector = first_evtchn_irq + isrc_idx;
285                 isrc = (struct xenisrc *)intr_lookup_source(vector);
286                 /*
287                  * Since intr_register_source() must be called while unlocked,
288                  * isrc == NULL *will* occur, though very infrequently.
289                  *
290                  * This also allows a very small gap where a foreign intrusion
291                  * into Xen's interrupt range could be examined by this test.
292                  */
293                 if (__predict_true(isrc != NULL) &&
294                     __predict_true(isrc->xi_intsrc.is_pic == &xen_intr_pic) &&
295                     isrc->xi_type == EVTCHN_TYPE_UNBOUND) {
296                         KASSERT(isrc->xi_intsrc.is_handlers == 0,
297                             ("Free evtchn still has handlers"));
298                         isrc->xi_type = type;
299                         return (isrc);
300                 }
301         }
302         return (NULL);
303 }
304
305 /**
306  * Allocate a Xen interrupt source object.
307  *
308  * \param type  The type of interrupt source to create.
309  *
310  * \return  A pointer to a newly allocated Xen interrupt source
311  *          object or NULL.
312  */
313 static struct xenisrc *
314 xen_intr_alloc_isrc(enum evtchn_type type)
315 {
316         static int warned;
317         struct xenisrc *isrc;
318         unsigned int vector;
319         int error;
320
321         KASSERT(mtx_owned(&xen_intr_isrc_lock), ("Evtchn alloc lock not held"));
322
323         if (xen_intr_auto_vector_count >= NR_EVENT_CHANNELS) {
324                 if (!warned) {
325                         warned = 1;
326                         printf("%s: Xen interrupts exhausted.\n", __func__);
327                 }
328                 return (NULL);
329         }
330
331         vector = first_evtchn_irq + xen_intr_auto_vector_count;
332         xen_intr_auto_vector_count++;
333
334         KASSERT((intr_lookup_source(vector) == NULL),
335             ("Trying to use an already allocated vector"));
336
337         mtx_unlock(&xen_intr_isrc_lock);
338         isrc = malloc(sizeof(*isrc), M_XENINTR, M_WAITOK | M_ZERO);
339         isrc->xi_intsrc.is_pic = &xen_intr_pic;
340         isrc->xi_vector = vector;
341         isrc->xi_type = type;
342         error = intr_register_source(&isrc->xi_intsrc);
343         if (error != 0)
344                 panic("%s(): failed registering interrupt %u, error=%d\n",
345                     __func__, vector, error);
346         mtx_lock(&xen_intr_isrc_lock);
347
348         return (isrc);
349 }
350
351 /**
352  * Attempt to free an active Xen interrupt source object.
353  *
354  * \param isrc  The interrupt source object to release.
355  *
356  * \returns  EBUSY if the source is still in use, otherwise 0.
357  */
358 static int
359 xen_intr_release_isrc(struct xenisrc *isrc)
360 {
361
362         KASSERT(isrc->xi_intsrc.is_handlers == 0,
363             ("Release called, but xenisrc still in use"));
364         mtx_lock(&xen_intr_isrc_lock);
365         if (is_valid_evtchn(isrc->xi_port)) {
366                 evtchn_mask_port(isrc->xi_port);
367                 evtchn_clear_port(isrc->xi_port);
368
369                 /* Rebind port to CPU 0. */
370                 evtchn_cpu_mask_port(isrc->xi_cpu, isrc->xi_port);
371                 evtchn_cpu_unmask_port(0, isrc->xi_port);
372
373                 if (isrc->xi_close != 0) {
374                         struct evtchn_close close = { .port = isrc->xi_port };
375
376                         if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
377                                 panic("EVTCHNOP_close failed");
378                 }
379
380                 xen_intr_port_to_isrc[isrc->xi_port] = NULL;
381         }
382         isrc->xi_cpu = 0;
383         isrc->xi_type = EVTCHN_TYPE_UNBOUND;
384         isrc->xi_port = INVALID_EVTCHN;
385         isrc->xi_cookie = NULL;
386         mtx_unlock(&xen_intr_isrc_lock);
387         return (0);
388 }
389
390 /**
391  * Associate an interrupt handler with an already allocated local Xen
392  * event channel port.
393  *
394  * \param isrcp       The returned Xen interrupt object associated with
395  *                    the specified local port.
396  * \param local_port  The event channel to bind.
397  * \param type        The event channel type of local_port.
398  * \param intr_owner  The device making this bind request.
399  * \param filter      An interrupt filter handler.  Specify NULL
400  *                    to always dispatch to the ithread handler.
401  * \param handler     An interrupt ithread handler.  Optional (can
402  *                    specify NULL) if all necessary event actions
403  *                    are performed by filter.
404  * \param arg         Argument to present to both filter and handler.
405  * \param irqflags    Interrupt handler flags.  See sys/bus.h.
406  * \param handlep     Pointer to an opaque handle used to manage this
407  *                    registration.
408  *
409  * \returns  0 on success, otherwise an errno.
410  */
411 static int
412 xen_intr_bind_isrc(struct xenisrc **isrcp, evtchn_port_t local_port,
413     enum evtchn_type type, const char *intr_owner, driver_filter_t filter,
414     driver_intr_t handler, void *arg, enum intr_type flags,
415     xen_intr_handle_t *port_handlep)
416 {
417         struct xenisrc *isrc;
418         int error;
419
420         *isrcp = NULL;
421         if (port_handlep == NULL) {
422                 printf("%s: %s: Bad event handle\n", intr_owner, __func__);
423                 return (EINVAL);
424         }
425
426         mtx_lock(&xen_intr_isrc_lock);
427         isrc = xen_intr_find_unused_isrc(type);
428         if (isrc == NULL) {
429                 isrc = xen_intr_alloc_isrc(type);
430                 if (isrc == NULL) {
431                         mtx_unlock(&xen_intr_isrc_lock);
432                         return (ENOSPC);
433                 }
434         }
435         isrc->xi_port = local_port;
436         isrc->xi_close = 0;
437         xen_intr_port_to_isrc[local_port] = isrc;
438         refcount_init(&isrc->xi_refcount, 1);
439         mtx_unlock(&xen_intr_isrc_lock);
440
441         /* Assign the opaque handler */
442         *port_handlep = xen_intr_handle_from_isrc(isrc);
443
444 #ifdef SMP
445         if (type == EVTCHN_TYPE_PORT) {
446                 /*
447                  * By default all interrupts are assigned to vCPU#0
448                  * unless specified otherwise, so shuffle them to balance
449                  * the interrupt load.
450                  */
451                 xen_intr_assign_cpu(&isrc->xi_intsrc, intr_next_cpu(0));
452         }
453 #endif
454
455         if (filter == NULL && handler == NULL) {
456                 /*
457                  * No filter/handler provided, leave the event channel
458                  * masked and without a valid handler, the caller is
459                  * in charge of setting that up.
460                  */
461                 *isrcp = isrc;
462                 return (0);
463         }
464
465         error = xen_intr_add_handler(intr_owner, filter, handler, arg, flags,
466             *port_handlep);
467         if (error != 0) {
468                 xen_intr_release_isrc(isrc);
469                 return (error);
470         }
471         *isrcp = isrc;
472         return (0);
473 }
474
475 /**
476  * Determine the event channel ports at the given section of the
477  * event port bitmap which have pending events for the given cpu.
478  * 
479  * \param pcpu  The Xen interrupt pcpu data for the cpu being queried.
480  * \param sh    The Xen shared info area.
481  * \param idx   The index of the section of the event channel bitmap to
482  *              inspect.
483  *
484  * \returns  A u_long with bits set for every event channel with pending
485  *           events.
486  */
487 static inline u_long
488 xen_intr_active_ports(const struct xen_intr_pcpu_data *const pcpu,
489     const u_int idx)
490 {
491         volatile const shared_info_t *const sh = HYPERVISOR_shared_info;
492
493         CTASSERT(sizeof(sh->evtchn_mask[0]) == sizeof(sh->evtchn_pending[0]));
494         CTASSERT(sizeof(sh->evtchn_mask[0]) == sizeof(pcpu->evtchn_enabled[0]));
495         CTASSERT(sizeof(sh->evtchn_mask) == sizeof(sh->evtchn_pending));
496         CTASSERT(sizeof(sh->evtchn_mask) == sizeof(pcpu->evtchn_enabled));
497         return (sh->evtchn_pending[idx]
498               & ~sh->evtchn_mask[idx]
499               & pcpu->evtchn_enabled[idx]);
500 }
501
502 /**
503  * Interrupt handler for processing all Xen event channel events.
504  * 
505  * \param trap_frame  The trap frame context for the current interrupt.
506  */
507 void
508 xen_intr_handle_upcall(struct trapframe *trap_frame)
509 {
510         u_int l1i, l2i, port, cpu __diagused;
511         u_long masked_l1, masked_l2;
512         struct xenisrc *isrc;
513         vcpu_info_t *v;
514         struct xen_intr_pcpu_data *pc;
515         u_long l1, l2;
516
517         /*
518          * Disable preemption in order to always check and fire events
519          * on the right vCPU
520          */
521         critical_enter();
522
523         cpu = PCPU_GET(cpuid);
524         pc  = DPCPU_PTR(xen_intr_pcpu);
525         v   = DPCPU_GET(vcpu_info);
526
527         if (!xen_has_percpu_evtchn()) {
528                 KASSERT((cpu == 0), ("Fired PCI event callback on wrong CPU"));
529         }
530
531         v->evtchn_upcall_pending = 0;
532
533 #if 0
534 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
535         /* Clear master flag /before/ clearing selector flag. */
536         wmb();
537 #endif
538 #endif
539
540         l1 = atomic_readandclear_long(&v->evtchn_pending_sel);
541
542         l1i = pc->last_processed_l1i;
543         l2i = pc->last_processed_l2i;
544         (*pc->evtchn_intrcnt)++;
545
546         while (l1 != 0) {
547                 l1i = (l1i + 1) % LONG_BIT;
548                 masked_l1 = l1 & ((~0UL) << l1i);
549
550                 if (masked_l1 == 0) {
551                         /*
552                          * if we masked out all events, wrap around
553                          * to the beginning.
554                          */
555                         l1i = LONG_BIT - 1;
556                         l2i = LONG_BIT - 1;
557                         continue;
558                 }
559                 l1i = ffsl(masked_l1) - 1;
560
561                 do {
562                         l2 = xen_intr_active_ports(pc, l1i);
563
564                         l2i = (l2i + 1) % LONG_BIT;
565                         masked_l2 = l2 & ((~0UL) << l2i);
566
567                         if (masked_l2 == 0) {
568                                 /* if we masked out all events, move on */
569                                 l2i = LONG_BIT - 1;
570                                 break;
571                         }
572                         l2i = ffsl(masked_l2) - 1;
573
574                         /* process port */
575                         port = (l1i * LONG_BIT) + l2i;
576                         evtchn_clear_port(port);
577
578                         isrc = xen_intr_port_to_isrc[port];
579                         if (__predict_false(isrc == NULL))
580                                 continue;
581
582                         /* Make sure we are firing on the right vCPU */
583                         KASSERT((isrc->xi_cpu == PCPU_GET(cpuid)),
584                                 ("Received unexpected event on vCPU#%d, event bound to vCPU#%d",
585                                 PCPU_GET(cpuid), isrc->xi_cpu));
586
587                         intr_execute_handlers(&isrc->xi_intsrc, trap_frame);
588
589                         /*
590                          * If this is the final port processed,
591                          * we'll pick up here+1 next time.
592                          */
593                         pc->last_processed_l1i = l1i;
594                         pc->last_processed_l2i = l2i;
595
596                 } while (l2i != LONG_BIT - 1);
597
598                 l2 = xen_intr_active_ports(pc, l1i);
599                 if (l2 == 0) {
600                         /*
601                          * We handled all ports, so we can clear the
602                          * selector bit.
603                          */
604                         l1 &= ~(1UL << l1i);
605                 }
606         }
607
608         if (xen_evtchn_needs_ack)
609                 lapic_eoi();
610
611         critical_exit();
612 }
613
614 static int
615 xen_intr_init(void *dummy __unused)
616 {
617         shared_info_t *s = HYPERVISOR_shared_info;
618         struct xen_intr_pcpu_data *pcpu;
619         int i;
620
621         if (!xen_domain())
622                 return (0);
623
624         _Static_assert(is_valid_evtchn(0),
625             "is_valid_evtchn(0) fails (unused by Xen, but valid by interface");
626         _Static_assert(is_valid_evtchn(NR_EVENT_CHANNELS - 1),
627             "is_valid_evtchn(max) fails (is a valid channel)");
628         _Static_assert(!is_valid_evtchn(NR_EVENT_CHANNELS),
629             "is_valid_evtchn(>max) fails (NOT a valid channel)");
630         _Static_assert(!is_valid_evtchn(~(evtchn_port_t)0),
631             "is_valid_evtchn(maxint) fails (overflow?)");
632         _Static_assert(!is_valid_evtchn(INVALID_EVTCHN),
633             "is_valid_evtchn(INVALID_EVTCHN) fails (must be invalid!)");
634         _Static_assert(!is_valid_evtchn(-1),
635             "is_valid_evtchn(-1) fails (negative are invalid)");
636
637         mtx_init(&xen_intr_isrc_lock, "xen-irq-lock", NULL, MTX_DEF);
638
639         /*
640          * Set the per-cpu mask of CPU#0 to enable all, since by default all
641          * event channels are bound to CPU#0.
642          */
643         CPU_FOREACH(i) {
644                 pcpu = DPCPU_ID_PTR(i, xen_intr_pcpu);
645                 memset(pcpu->evtchn_enabled, i == 0 ? ~0 : 0,
646                     sizeof(pcpu->evtchn_enabled));
647         }
648
649         for (i = 0; i < nitems(s->evtchn_mask); i++)
650                 atomic_store_rel_long(&s->evtchn_mask[i], ~0);
651
652         intr_register_pic(&xen_intr_pic);
653
654         if (bootverbose)
655                 printf("Xen interrupt system initialized\n");
656
657         return (0);
658 }
659 SYSINIT(xen_intr_init, SI_SUB_INTR, SI_ORDER_SECOND, xen_intr_init, NULL);
660
661 static void
662 xen_intrcnt_init(void *dummy __unused)
663 {
664         unsigned int i;
665
666         if (!xen_domain())
667                 return;
668
669         /*
670          * Register interrupt count manually as we aren't guaranteed to see a
671          * call to xen_intr_assign_cpu() before our first interrupt.
672          */
673         CPU_FOREACH(i)
674                 xen_intr_intrcnt_add(i);
675 }
676 SYSINIT(xen_intrcnt_init, SI_SUB_INTR, SI_ORDER_MIDDLE, xen_intrcnt_init, NULL);
677
678 void
679 xen_intr_alloc_irqs(void)
680 {
681
682         if (num_io_irqs > UINT_MAX - NR_EVENT_CHANNELS)
683                 panic("IRQ allocation overflow (num_msi_irqs too high?)");
684         first_evtchn_irq = num_io_irqs;
685         num_io_irqs += NR_EVENT_CHANNELS;
686 }
687
688 /*--------------------------- Common PIC Functions ---------------------------*/
689 /**
690  * Prepare this PIC for system suspension.
691  */
692 static void
693 xen_intr_suspend(struct pic *unused)
694 {
695 }
696
697 static void
698 xen_rebind_ipi(struct xenisrc *isrc)
699 {
700 #ifdef SMP
701         int cpu = isrc->xi_cpu;
702         int vcpu_id = pcpu_find(cpu)->pc_vcpu_id;
703         int error;
704         struct evtchn_bind_ipi bind_ipi = { .vcpu = vcpu_id };
705
706         error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
707                                             &bind_ipi);
708         if (error != 0)
709                 panic("unable to rebind xen IPI: %d", error);
710
711         isrc->xi_port = bind_ipi.port;
712         isrc->xi_cpu = 0;
713         xen_intr_port_to_isrc[bind_ipi.port] = isrc;
714
715         error = xen_intr_assign_cpu(&isrc->xi_intsrc,
716                                     cpu_apic_ids[cpu]);
717         if (error)
718                 panic("unable to bind xen IPI to CPU#%d: %d",
719                       cpu, error);
720
721         evtchn_unmask_port(bind_ipi.port);
722 #else
723         panic("Resume IPI event channel on UP");
724 #endif
725 }
726
727 static void
728 xen_rebind_virq(struct xenisrc *isrc)
729 {
730         int cpu = isrc->xi_cpu;
731         int vcpu_id = pcpu_find(cpu)->pc_vcpu_id;
732         int error;
733         struct evtchn_bind_virq bind_virq = { .virq = isrc->xi_virq,
734                                               .vcpu = vcpu_id };
735
736         error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
737                                             &bind_virq);
738         if (error != 0)
739                 panic("unable to rebind xen VIRQ#%d: %d", isrc->xi_virq, error);
740
741         isrc->xi_port = bind_virq.port;
742         isrc->xi_cpu = 0;
743         xen_intr_port_to_isrc[bind_virq.port] = isrc;
744
745 #ifdef SMP
746         error = xen_intr_assign_cpu(&isrc->xi_intsrc,
747                                     cpu_apic_ids[cpu]);
748         if (error)
749                 panic("unable to bind xen VIRQ#%d to CPU#%d: %d",
750                       isrc->xi_virq, cpu, error);
751 #endif
752
753         evtchn_unmask_port(bind_virq.port);
754 }
755
756 /**
757  * Return this PIC to service after being suspended.
758  */
759 static void
760 xen_intr_resume(struct pic *unused, bool suspend_cancelled)
761 {
762         shared_info_t *s = HYPERVISOR_shared_info;
763         struct xenisrc *isrc;
764         u_int isrc_idx;
765         int i;
766
767         if (suspend_cancelled)
768                 return;
769
770         /* Reset the per-CPU masks */
771         CPU_FOREACH(i) {
772                 struct xen_intr_pcpu_data *pcpu;
773
774                 pcpu = DPCPU_ID_PTR(i, xen_intr_pcpu);
775                 memset(pcpu->evtchn_enabled, i == 0 ? ~0 : 0,
776                     sizeof(pcpu->evtchn_enabled));
777         }
778
779         /* Mask all event channels. */
780         for (i = 0; i < nitems(s->evtchn_mask); i++)
781                 atomic_store_rel_long(&s->evtchn_mask[i], ~0);
782
783         /* Remove port -> isrc mappings */
784         memset(xen_intr_port_to_isrc, 0, sizeof(xen_intr_port_to_isrc));
785
786         /* Free unused isrcs and rebind VIRQs and IPIs */
787         for (isrc_idx = 0; isrc_idx < xen_intr_auto_vector_count; isrc_idx++) {
788                 u_int vector;
789
790                 vector = first_evtchn_irq + isrc_idx;
791                 isrc = (struct xenisrc *)intr_lookup_source(vector);
792                 if (isrc != NULL) {
793                         isrc->xi_port = INVALID_EVTCHN;
794                         switch (isrc->xi_type) {
795                         case EVTCHN_TYPE_IPI:
796                                 xen_rebind_ipi(isrc);
797                                 break;
798                         case EVTCHN_TYPE_VIRQ:
799                                 xen_rebind_virq(isrc);
800                                 break;
801                         default:
802                                 break;
803                         }
804                 }
805         }
806 }
807
808 /**
809  * Disable a Xen interrupt source.
810  *
811  * \param isrc  The interrupt source to disable.
812  */
813 static void
814 xen_intr_disable_intr(struct intsrc *base_isrc)
815 {
816         struct xenisrc *isrc = (struct xenisrc *)base_isrc;
817
818         evtchn_mask_port(isrc->xi_port);
819 }
820
821 /**
822  * Determine the global interrupt vector number for
823  * a Xen interrupt source.
824  *
825  * \param isrc  The interrupt source to query.
826  *
827  * \return  The vector number corresponding to the given interrupt source.
828  */
829 static int
830 xen_intr_vector(struct intsrc *base_isrc)
831 {
832         struct xenisrc *isrc = (struct xenisrc *)base_isrc;
833
834         return (isrc->xi_vector);
835 }
836
837 /**
838  * Determine whether or not interrupt events are pending on the
839  * the given interrupt source.
840  *
841  * \param isrc  The interrupt source to query.
842  *
843  * \returns  0 if no events are pending, otherwise non-zero.
844  */
845 static int
846 xen_intr_source_pending(struct intsrc *isrc)
847 {
848         /*
849          * EventChannels are edge triggered and never masked.
850          * There can be no pending events.
851          */
852         return (0);
853 }
854
855 /**
856  * Perform configuration of an interrupt source.
857  *
858  * \param isrc  The interrupt source to configure.
859  * \param trig  Edge or level.
860  * \param pol   Active high or low.
861  *
862  * \returns  0 if no events are pending, otherwise non-zero.
863  */
864 static int
865 xen_intr_config_intr(struct intsrc *isrc, enum intr_trigger trig,
866     enum intr_polarity pol)
867 {
868         /* Configuration is only possible via the evtchn apis. */
869         return (ENODEV);
870 }
871
872 /**
873  * Configure CPU affinity for interrupt source event delivery.
874  *
875  * \param isrc     The interrupt source to configure.
876  * \param apic_id  The apic id of the CPU for handling future events.
877  *
878  * \returns  0 if successful, otherwise an errno.
879  */
880 static int
881 xen_intr_assign_cpu(struct intsrc *base_isrc, u_int apic_id)
882 {
883 #ifdef SMP
884         struct evtchn_bind_vcpu bind_vcpu;
885         struct xenisrc *isrc;
886         u_int to_cpu, vcpu_id;
887         int error, masked;
888
889         if (!xen_has_percpu_evtchn())
890                 return (EOPNOTSUPP);
891
892         to_cpu = apic_cpuid(apic_id);
893         vcpu_id = pcpu_find(to_cpu)->pc_vcpu_id;
894
895         mtx_lock(&xen_intr_isrc_lock);
896         isrc = (struct xenisrc *)base_isrc;
897         if (!is_valid_evtchn(isrc->xi_port)) {
898                 mtx_unlock(&xen_intr_isrc_lock);
899                 return (EINVAL);
900         }
901
902         /*
903          * Mask the event channel while binding it to prevent interrupt
904          * delivery with an inconsistent state in isrc->xi_cpu.
905          */
906         masked = evtchn_test_and_set_mask(isrc->xi_port);
907         if ((isrc->xi_type == EVTCHN_TYPE_VIRQ) ||
908                 (isrc->xi_type == EVTCHN_TYPE_IPI)) {
909                 /*
910                  * Virtual IRQs are associated with a cpu by
911                  * the Hypervisor at evtchn_bind_virq time, so
912                  * all we need to do is update the per-CPU masks.
913                  */
914                 evtchn_cpu_mask_port(isrc->xi_cpu, isrc->xi_port);
915                 isrc->xi_cpu = to_cpu;
916                 evtchn_cpu_unmask_port(isrc->xi_cpu, isrc->xi_port);
917                 goto out;
918         }
919
920         bind_vcpu.port = isrc->xi_port;
921         bind_vcpu.vcpu = vcpu_id;
922
923         error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu);
924         if (isrc->xi_cpu != to_cpu) {
925                 if (error == 0) {
926                         /* Commit to new binding by removing the old one. */
927                         evtchn_cpu_mask_port(isrc->xi_cpu, isrc->xi_port);
928                         isrc->xi_cpu = to_cpu;
929                         evtchn_cpu_unmask_port(isrc->xi_cpu, isrc->xi_port);
930                 }
931         }
932
933 out:
934         if (masked == 0)
935                 evtchn_unmask_port(isrc->xi_port);
936         mtx_unlock(&xen_intr_isrc_lock);
937         return (0);
938 #else
939         return (EOPNOTSUPP);
940 #endif
941 }
942
943 /*------------------- Virtual Interrupt Source PIC Functions -----------------*/
944 /*
945  * Mask a level triggered interrupt source.
946  *
947  * \param isrc  The interrupt source to mask (if necessary).
948  * \param eoi   If non-zero, perform any necessary end-of-interrupt
949  *              acknowledgements.
950  */
951 static void
952 xen_intr_disable_source(struct intsrc *base_isrc, int eoi)
953 {
954         struct xenisrc *isrc;
955
956         isrc = (struct xenisrc *)base_isrc;
957
958         /*
959          * NB: checking if the event channel is already masked is
960          * needed because the event channel user-space device
961          * masks event channels on its filter as part of its
962          * normal operation, and those shouldn't be automatically
963          * unmasked by the generic interrupt code. The event channel
964          * device will unmask them when needed.
965          */
966         isrc->xi_masked = !!evtchn_test_and_set_mask(isrc->xi_port);
967 }
968
969 /*
970  * Unmask a level triggered interrupt source.
971  *
972  * \param isrc  The interrupt source to unmask (if necessary).
973  */
974 static void
975 xen_intr_enable_source(struct intsrc *base_isrc)
976 {
977         struct xenisrc *isrc;
978
979         isrc = (struct xenisrc *)base_isrc;
980
981         if (isrc->xi_masked == 0)
982                 evtchn_unmask_port(isrc->xi_port);
983 }
984
985 /*
986  * Perform any necessary end-of-interrupt acknowledgements.
987  *
988  * \param isrc  The interrupt source to EOI.
989  */
990 static void
991 xen_intr_eoi_source(struct intsrc *base_isrc)
992 {
993 }
994
995 /*
996  * Enable and unmask the interrupt source.
997  *
998  * \param isrc  The interrupt source to enable.
999  */
1000 static void
1001 xen_intr_enable_intr(struct intsrc *base_isrc)
1002 {
1003         struct xenisrc *isrc = (struct xenisrc *)base_isrc;
1004
1005         evtchn_unmask_port(isrc->xi_port);
1006 }
1007
1008 /*--------------------------- Public Functions -------------------------------*/
1009 /*------- API comments for these methods can be found in xen/xenintr.h -------*/
1010 int
1011 xen_intr_bind_local_port(device_t dev, evtchn_port_t local_port,
1012     driver_filter_t filter, driver_intr_t handler, void *arg,
1013     enum intr_type flags, xen_intr_handle_t *port_handlep)
1014 {
1015         struct xenisrc *isrc;
1016         int error;
1017
1018         error = xen_intr_bind_isrc(&isrc, local_port, EVTCHN_TYPE_PORT,
1019             device_get_nameunit(dev), filter, handler, arg, flags,
1020             port_handlep);
1021         if (error != 0)
1022                 return (error);
1023
1024         /*
1025          * The Event Channel API didn't open this port, so it is not
1026          * responsible for closing it automatically on unbind.
1027          */
1028         isrc->xi_close = 0;
1029         return (0);
1030 }
1031
1032 int
1033 xen_intr_alloc_and_bind_local_port(device_t dev, u_int remote_domain,
1034     driver_filter_t filter, driver_intr_t handler, void *arg,
1035     enum intr_type flags, xen_intr_handle_t *port_handlep)
1036 {
1037         struct xenisrc *isrc;
1038         struct evtchn_alloc_unbound alloc_unbound;
1039         int error;
1040
1041         alloc_unbound.dom        = DOMID_SELF;
1042         alloc_unbound.remote_dom = remote_domain;
1043         error = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
1044                     &alloc_unbound);
1045         if (error != 0) {
1046                 /*
1047                  * XXX Trap Hypercall error code Linuxisms in
1048                  *     the HYPERCALL layer.
1049                  */
1050                 return (-error);
1051         }
1052
1053         error = xen_intr_bind_isrc(&isrc, alloc_unbound.port, EVTCHN_TYPE_PORT,
1054             device_get_nameunit(dev), filter, handler, arg, flags,
1055             port_handlep);
1056         if (error != 0) {
1057                 evtchn_close_t close = { .port = alloc_unbound.port };
1058                 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
1059                         panic("EVTCHNOP_close failed");
1060                 return (error);
1061         }
1062
1063         isrc->xi_close = 1;
1064         return (0);
1065 }
1066
1067 int 
1068 xen_intr_bind_remote_port(device_t dev, u_int remote_domain,
1069     u_int remote_port, driver_filter_t filter, driver_intr_t handler,
1070     void *arg, enum intr_type flags, xen_intr_handle_t *port_handlep)
1071 {
1072         struct xenisrc *isrc;
1073         struct evtchn_bind_interdomain bind_interdomain;
1074         int error;
1075
1076         bind_interdomain.remote_dom  = remote_domain;
1077         bind_interdomain.remote_port = remote_port;
1078         error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
1079                                             &bind_interdomain);
1080         if (error != 0) {
1081                 /*
1082                  * XXX Trap Hypercall error code Linuxisms in
1083                  *     the HYPERCALL layer.
1084                  */
1085                 return (-error);
1086         }
1087
1088         error = xen_intr_bind_isrc(&isrc, bind_interdomain.local_port,
1089             EVTCHN_TYPE_PORT, device_get_nameunit(dev), filter, handler, arg,
1090             flags, port_handlep);
1091         if (error) {
1092                 evtchn_close_t close = { .port = bind_interdomain.local_port };
1093                 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
1094                         panic("EVTCHNOP_close failed");
1095                 return (error);
1096         }
1097
1098         /*
1099          * The Event Channel API opened this port, so it is
1100          * responsible for closing it automatically on unbind.
1101          */
1102         isrc->xi_close = 1;
1103         return (0);
1104 }
1105
1106 int 
1107 xen_intr_bind_virq(device_t dev, u_int virq, u_int cpu,
1108     driver_filter_t filter, driver_intr_t handler, void *arg,
1109     enum intr_type flags, xen_intr_handle_t *port_handlep)
1110 {
1111         int vcpu_id = pcpu_find(cpu)->pc_vcpu_id;
1112         struct xenisrc *isrc;
1113         struct evtchn_bind_virq bind_virq = { .virq = virq, .vcpu = vcpu_id };
1114         int error;
1115
1116         isrc = NULL;
1117         error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq, &bind_virq);
1118         if (error != 0) {
1119                 /*
1120                  * XXX Trap Hypercall error code Linuxisms in
1121                  *     the HYPERCALL layer.
1122                  */
1123                 return (-error);
1124         }
1125
1126         error = xen_intr_bind_isrc(&isrc, bind_virq.port, EVTCHN_TYPE_VIRQ,
1127             device_get_nameunit(dev), filter, handler, arg, flags,
1128             port_handlep);
1129
1130 #ifdef SMP
1131         if (error == 0)
1132                 error = intr_event_bind(isrc->xi_intsrc.is_event, cpu);
1133 #endif
1134
1135         if (error != 0) {
1136                 evtchn_close_t close = { .port = bind_virq.port };
1137
1138                 xen_intr_unbind(*port_handlep);
1139                 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
1140                         panic("EVTCHNOP_close failed");
1141                 return (error);
1142         }
1143
1144 #ifdef SMP
1145         if (isrc->xi_cpu != cpu) {
1146                 /*
1147                  * Too early in the boot process for the generic interrupt
1148                  * code to perform the binding.  Update our event channel
1149                  * masks manually so events can't fire on the wrong cpu
1150                  * during AP startup.
1151                  */
1152                 xen_intr_assign_cpu(&isrc->xi_intsrc, cpu_apic_ids[cpu]);
1153         }
1154 #endif
1155
1156         /*
1157          * The Event Channel API opened this port, so it is
1158          * responsible for closing it automatically on unbind.
1159          */
1160         isrc->xi_close = 1;
1161         isrc->xi_virq = virq;
1162
1163         return (0);
1164 }
1165
1166 int
1167 xen_intr_alloc_and_bind_ipi(u_int cpu, driver_filter_t filter,
1168     enum intr_type flags, xen_intr_handle_t *port_handlep)
1169 {
1170 #ifdef SMP
1171         int vcpu_id = pcpu_find(cpu)->pc_vcpu_id;
1172         struct xenisrc *isrc;
1173         struct evtchn_bind_ipi bind_ipi = { .vcpu = vcpu_id };
1174         /* Same size as the one used by intr_handler->ih_name. */
1175         char name[MAXCOMLEN + 1];
1176         int error;
1177
1178         isrc = NULL;
1179         error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi, &bind_ipi);
1180         if (error != 0) {
1181                 /*
1182                  * XXX Trap Hypercall error code Linuxisms in
1183                  *     the HYPERCALL layer.
1184                  */
1185                 return (-error);
1186         }
1187
1188         snprintf(name, sizeof(name), "cpu%u", cpu);
1189
1190         error = xen_intr_bind_isrc(&isrc, bind_ipi.port, EVTCHN_TYPE_IPI,
1191             name, filter, NULL, NULL, flags, port_handlep);
1192         if (error != 0) {
1193                 evtchn_close_t close = { .port = bind_ipi.port };
1194
1195                 xen_intr_unbind(*port_handlep);
1196                 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
1197                         panic("EVTCHNOP_close failed");
1198                 return (error);
1199         }
1200
1201         if (isrc->xi_cpu != cpu) {
1202                 /*
1203                  * Too early in the boot process for the generic interrupt
1204                  * code to perform the binding.  Update our event channel
1205                  * masks manually so events can't fire on the wrong cpu
1206                  * during AP startup.
1207                  */
1208                 xen_intr_assign_cpu(&isrc->xi_intsrc, cpu_apic_ids[cpu]);
1209         }
1210
1211         /*
1212          * The Event Channel API opened this port, so it is
1213          * responsible for closing it automatically on unbind.
1214          */
1215         isrc->xi_close = 1;
1216         return (0);
1217 #else
1218         return (EOPNOTSUPP);
1219 #endif
1220 }
1221
1222 int
1223 xen_intr_describe(xen_intr_handle_t port_handle, const char *fmt, ...)
1224 {
1225         char descr[MAXCOMLEN + 1];
1226         struct xenisrc *isrc;
1227         va_list ap;
1228
1229         isrc = xen_intr_isrc_from_handle(port_handle);
1230         if (isrc == NULL)
1231                 return (EINVAL);
1232
1233         va_start(ap, fmt);
1234         vsnprintf(descr, sizeof(descr), fmt, ap);
1235         va_end(ap);
1236         return (intr_describe(isrc->xi_vector, isrc->xi_cookie, descr));
1237 }
1238
1239 void
1240 xen_intr_unbind(xen_intr_handle_t *port_handlep)
1241 {
1242         struct xenisrc *isrc;
1243
1244         KASSERT(port_handlep != NULL,
1245             ("NULL xen_intr_handle_t passed to %s", __func__));
1246
1247         isrc = xen_intr_isrc_from_handle(*port_handlep);
1248         *port_handlep = NULL;
1249         if (isrc == NULL)
1250                 return;
1251
1252         mtx_lock(&xen_intr_isrc_lock);
1253         if (refcount_release(&isrc->xi_refcount) == 0) {
1254                 mtx_unlock(&xen_intr_isrc_lock);
1255                 return;
1256         }
1257         mtx_unlock(&xen_intr_isrc_lock);
1258
1259         if (isrc->xi_cookie != NULL)
1260                 intr_remove_handler(isrc->xi_cookie);
1261         xen_intr_release_isrc(isrc);
1262 }
1263
1264 void
1265 xen_intr_signal(xen_intr_handle_t handle)
1266 {
1267         struct xenisrc *isrc;
1268
1269         isrc = xen_intr_isrc_from_handle(handle);
1270         if (isrc != NULL) {
1271                 KASSERT(isrc->xi_type == EVTCHN_TYPE_PORT ||
1272                         isrc->xi_type == EVTCHN_TYPE_IPI,
1273                         ("evtchn_signal on something other than a local port"));
1274                 struct evtchn_send send = { .port = isrc->xi_port };
1275                 (void)HYPERVISOR_event_channel_op(EVTCHNOP_send, &send);
1276         }
1277 }
1278
1279 evtchn_port_t
1280 xen_intr_port(xen_intr_handle_t handle)
1281 {
1282         struct xenisrc *isrc;
1283
1284         isrc = xen_intr_isrc_from_handle(handle);
1285         if (isrc == NULL)
1286                 return (0);
1287
1288         return (isrc->xi_port);
1289 }
1290
1291 int
1292 xen_intr_add_handler(const char *name, driver_filter_t filter,
1293     driver_intr_t handler, void *arg, enum intr_type flags,
1294     xen_intr_handle_t handle)
1295 {
1296         struct xenisrc *isrc;
1297         int error;
1298
1299         isrc = xen_intr_isrc_from_handle(handle);
1300         if (isrc == NULL || isrc->xi_cookie != NULL)
1301                 return (EINVAL);
1302
1303         error = intr_add_handler(name, isrc->xi_vector,filter, handler, arg,
1304             flags|INTR_EXCL, &isrc->xi_cookie, 0);
1305         if (error != 0)
1306                 printf("%s: %s: add handler failed: %d\n", name, __func__,
1307                     error);
1308
1309         return (error);
1310 }
1311
1312 int
1313 xen_intr_get_evtchn_from_port(evtchn_port_t port, xen_intr_handle_t *handlep)
1314 {
1315
1316         if (!is_valid_evtchn(port))
1317                 return (EINVAL);
1318
1319         if (handlep == NULL) {
1320                 return (EINVAL);
1321         }
1322
1323         mtx_lock(&xen_intr_isrc_lock);
1324         if (xen_intr_port_to_isrc[port] == NULL) {
1325                 mtx_unlock(&xen_intr_isrc_lock);
1326                 return (EINVAL);
1327         }
1328         refcount_acquire(&xen_intr_port_to_isrc[port]->xi_refcount);
1329         mtx_unlock(&xen_intr_isrc_lock);
1330
1331         /* Assign the opaque handler */
1332         *handlep = xen_intr_handle_from_isrc(xen_intr_port_to_isrc[port]);
1333
1334         return (0);
1335 }
1336
1337 #ifdef DDB
1338 static const char *
1339 xen_intr_print_type(enum evtchn_type type)
1340 {
1341         static const char *evtchn_type_to_string[EVTCHN_TYPE_COUNT] = {
1342                 [EVTCHN_TYPE_UNBOUND]   = "UNBOUND",
1343                 [EVTCHN_TYPE_VIRQ]      = "VIRQ",
1344                 [EVTCHN_TYPE_IPI]       = "IPI",
1345                 [EVTCHN_TYPE_PORT]      = "PORT",
1346         };
1347
1348         if (type >= EVTCHN_TYPE_COUNT)
1349                 return ("UNKNOWN");
1350
1351         return (evtchn_type_to_string[type]);
1352 }
1353
1354 static void
1355 xen_intr_dump_port(struct xenisrc *isrc)
1356 {
1357         struct xen_intr_pcpu_data *pcpu;
1358         shared_info_t *s = HYPERVISOR_shared_info;
1359         int i;
1360
1361         db_printf("Port %d Type: %s\n",
1362             isrc->xi_port, xen_intr_print_type(isrc->xi_type));
1363         if (isrc->xi_type == EVTCHN_TYPE_VIRQ)
1364                 db_printf("\tVirq: %d\n", isrc->xi_virq);
1365
1366         db_printf("\tMasked: %d Pending: %d\n",
1367             !!xen_test_bit(isrc->xi_port, &s->evtchn_mask[0]),
1368             !!xen_test_bit(isrc->xi_port, &s->evtchn_pending[0]));
1369
1370         db_printf("\tPer-CPU Masks: ");
1371         CPU_FOREACH(i) {
1372                 pcpu = DPCPU_ID_PTR(i, xen_intr_pcpu);
1373                 db_printf("cpu#%d: %d ", i,
1374                     !!xen_test_bit(isrc->xi_port, pcpu->evtchn_enabled));
1375         }
1376         db_printf("\n");
1377 }
1378
1379 DB_SHOW_COMMAND(xen_evtchn, db_show_xen_evtchn)
1380 {
1381         int i;
1382
1383         if (!xen_domain()) {
1384                 db_printf("Only available on Xen guests\n");
1385                 return;
1386         }
1387
1388         for (i = 0; i < NR_EVENT_CHANNELS; i++) {
1389                 struct xenisrc *isrc;
1390
1391                 isrc = xen_intr_port_to_isrc[i];
1392                 if (isrc == NULL)
1393                         continue;
1394
1395                 xen_intr_dump_port(isrc);
1396         }
1397 }
1398 #endif /* DDB */