]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/xen/xen_intr.c
Update libdialog to 1.3-20180621
[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  *
10  * This file may be distributed separately from the Linux kernel, or
11  * incorporated into other software packages, subject to the following license:
12  *
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:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies or substantial portions of the Software.
22  *
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
29  * IN THE SOFTWARE.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_ddb.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/limits.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/interrupt.h>
46 #include <sys/pcpu.h>
47 #include <sys/smp.h>
48 #include <sys/refcount.h>
49
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52
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>
58
59 #include <machine/xen/synch_bitops.h>
60 #include <machine/xen/xen-os.h>
61
62 #include <xen/xen-os.h>
63 #include <xen/hypervisor.h>
64 #include <xen/xen_intr.h>
65 #include <xen/evtchn/evtchnvar.h>
66
67 #include <dev/xen/xenpci/xenpcivar.h>
68 #include <dev/pci/pcivar.h>
69
70 #ifdef DDB
71 #include <ddb/ddb.h>
72 #endif
73
74 static MALLOC_DEFINE(M_XENINTR, "xen_intr", "Xen Interrupt Services");
75
76 static u_int first_evtchn_irq;
77
78 /**
79  * Per-cpu event channel processing state.
80  */
81 struct xen_intr_pcpu_data {
82         /**
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.
86          */
87         u_int   last_processed_l1i;
88
89         /**
90          * The last event channel processed within the event channel
91          * bitmap being scanned.
92          */
93         u_int   last_processed_l2i;
94
95         /** Pointer to this CPU's interrupt statistic counter. */
96         u_long *evtchn_intrcnt;
97
98         /**
99          * A bitmap of ports that can be serviced from this CPU.
100          * A set bit means interrupt handling is enabled.
101          */
102         u_long  evtchn_enabled[sizeof(u_long) * 8];
103 };
104
105 /*
106  * Start the scan at port 0 by initializing the last scanned
107  * location as the highest numbered event channel port.
108  */
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
112 };
113
114 DPCPU_DECLARE(struct vcpu_info *, vcpu_info);
115
116 #define XEN_EEXIST              17 /* Xen "already exists" error */
117 #define XEN_ALLOCATE_VECTOR     0 /* Allocate a vector for this event channel */
118 #define XEN_INVALID_EVTCHN      0 /* Invalid event channel */
119
120 #define is_valid_evtchn(x)      ((x) != XEN_INVALID_EVTCHN)
121
122 struct xenisrc {
123         struct intsrc   xi_intsrc;
124         enum evtchn_type xi_type;
125         int             xi_cpu;         /* VCPU for delivery. */
126         int             xi_vector;      /* Global isrc vector number. */
127         evtchn_port_t   xi_port;
128         int             xi_pirq;
129         int             xi_virq;
130         void            *xi_cookie;
131         u_int           xi_close:1;     /* close on unbind? */
132         u_int           xi_activehi:1;
133         u_int           xi_edgetrigger:1;
134         u_int           xi_masked:1;
135         volatile u_int  xi_refcount;
136 };
137
138 static void     xen_intr_suspend(struct pic *);
139 static void     xen_intr_resume(struct pic *, bool suspend_cancelled);
140 static void     xen_intr_enable_source(struct intsrc *isrc);
141 static void     xen_intr_disable_source(struct intsrc *isrc, int eoi);
142 static void     xen_intr_eoi_source(struct intsrc *isrc);
143 static void     xen_intr_enable_intr(struct intsrc *isrc);
144 static void     xen_intr_disable_intr(struct intsrc *isrc);
145 static int      xen_intr_vector(struct intsrc *isrc);
146 static int      xen_intr_source_pending(struct intsrc *isrc);
147 static int      xen_intr_config_intr(struct intsrc *isrc,
148                      enum intr_trigger trig, enum intr_polarity pol);
149 static int      xen_intr_assign_cpu(struct intsrc *isrc, u_int apic_id);
150
151 static void     xen_intr_pirq_enable_source(struct intsrc *isrc);
152 static void     xen_intr_pirq_disable_source(struct intsrc *isrc, int eoi);
153 static void     xen_intr_pirq_eoi_source(struct intsrc *isrc);
154 static void     xen_intr_pirq_enable_intr(struct intsrc *isrc);
155 static void     xen_intr_pirq_disable_intr(struct intsrc *isrc);
156 static int      xen_intr_pirq_config_intr(struct intsrc *isrc,
157                      enum intr_trigger trig, enum intr_polarity pol);
158
159 /**
160  * PIC interface for all event channel port types except physical IRQs.
161  */
162 struct pic xen_intr_pic = {
163         .pic_enable_source  = xen_intr_enable_source,
164         .pic_disable_source = xen_intr_disable_source,
165         .pic_eoi_source     = xen_intr_eoi_source,
166         .pic_enable_intr    = xen_intr_enable_intr,
167         .pic_disable_intr   = xen_intr_disable_intr,
168         .pic_vector         = xen_intr_vector,
169         .pic_source_pending = xen_intr_source_pending,
170         .pic_suspend        = xen_intr_suspend,
171         .pic_resume         = xen_intr_resume,
172         .pic_config_intr    = xen_intr_config_intr,
173         .pic_assign_cpu     = xen_intr_assign_cpu
174 };
175
176 /**
177  * PIC interface for all event channel representing
178  * physical interrupt sources.
179  */
180 struct pic xen_intr_pirq_pic = {
181 #ifdef __amd64__
182         .pic_register_sources = xenpv_register_pirqs,
183 #endif
184         .pic_enable_source  = xen_intr_pirq_enable_source,
185         .pic_disable_source = xen_intr_pirq_disable_source,
186         .pic_eoi_source     = xen_intr_pirq_eoi_source,
187         .pic_enable_intr    = xen_intr_pirq_enable_intr,
188         .pic_disable_intr   = xen_intr_pirq_disable_intr,
189         .pic_vector         = xen_intr_vector,
190         .pic_source_pending = xen_intr_source_pending,
191         .pic_config_intr    = xen_intr_pirq_config_intr,
192         .pic_assign_cpu     = xen_intr_assign_cpu
193 };
194
195 static struct mtx        xen_intr_isrc_lock;
196 static u_int             xen_intr_auto_vector_count;
197 static struct xenisrc   *xen_intr_port_to_isrc[NR_EVENT_CHANNELS];
198 static u_long           *xen_intr_pirq_eoi_map;
199 static boolean_t         xen_intr_pirq_eoi_map_enabled;
200
201 /*------------------------- Private Functions --------------------------------*/
202 /**
203  * Disable signal delivery for an event channel port on the
204  * specified CPU.
205  *
206  * \param port  The event channel port to mask.
207  *
208  * This API is used to manage the port<=>CPU binding of event
209  * channel handlers.
210  *
211  * \note  This operation does not preclude reception of an event
212  *        for this event channel on another CPU.  To mask the
213  *        event channel globally, use evtchn_mask().
214  */
215 static inline void
216 evtchn_cpu_mask_port(u_int cpu, evtchn_port_t port)
217 {
218         struct xen_intr_pcpu_data *pcpu;
219
220         pcpu = DPCPU_ID_PTR(cpu, xen_intr_pcpu);
221         xen_clear_bit(port, pcpu->evtchn_enabled);
222 }
223
224 /**
225  * Enable signal delivery for an event channel port on the
226  * specified CPU.
227  *
228  * \param port  The event channel port to unmask.
229  *
230  * This API is used to manage the port<=>CPU binding of event
231  * channel handlers.
232  *
233  * \note  This operation does not guarantee that event delivery
234  *        is enabled for this event channel port.  The port must
235  *        also be globally enabled.  See evtchn_unmask().
236  */
237 static inline void
238 evtchn_cpu_unmask_port(u_int cpu, evtchn_port_t port)
239 {
240         struct xen_intr_pcpu_data *pcpu;
241
242         pcpu = DPCPU_ID_PTR(cpu, xen_intr_pcpu);
243         xen_set_bit(port, pcpu->evtchn_enabled);
244 }
245
246 /**
247  * Allocate and register a per-cpu Xen upcall interrupt counter.
248  *
249  * \param cpu  The cpu for which to register this interrupt count.
250  */
251 static void
252 xen_intr_intrcnt_add(u_int cpu)
253 {
254         char buf[MAXCOMLEN + 1];
255         struct xen_intr_pcpu_data *pcpu;
256
257         pcpu = DPCPU_ID_PTR(cpu, xen_intr_pcpu);
258         if (pcpu->evtchn_intrcnt != NULL)
259                 return;
260
261         snprintf(buf, sizeof(buf), "cpu%d:xen", cpu);
262         intrcnt_add(buf, &pcpu->evtchn_intrcnt);
263 }
264
265 /**
266  * Search for an already allocated but currently unused Xen interrupt
267  * source object.
268  *
269  * \param type  Restrict the search to interrupt sources of the given
270  *              type.
271  *
272  * \return  A pointer to a free Xen interrupt source object or NULL.
273  */
274 static struct xenisrc *
275 xen_intr_find_unused_isrc(enum evtchn_type type)
276 {
277         int isrc_idx;
278
279         KASSERT(mtx_owned(&xen_intr_isrc_lock), ("Evtchn isrc lock not held"));
280
281         for (isrc_idx = 0; isrc_idx < xen_intr_auto_vector_count; isrc_idx ++) {
282                 struct xenisrc *isrc;
283                 u_int vector;
284
285                 vector = first_evtchn_irq + isrc_idx;
286                 isrc = (struct xenisrc *)intr_lookup_source(vector);
287                 if (isrc != NULL
288                  && isrc->xi_type == EVTCHN_TYPE_UNBOUND) {
289                         KASSERT(isrc->xi_intsrc.is_handlers == 0,
290                             ("Free evtchn still has handlers"));
291                         isrc->xi_type = type;
292                         return (isrc);
293                 }
294         }
295         return (NULL);
296 }
297
298 /**
299  * Allocate a Xen interrupt source object.
300  *
301  * \param type  The type of interrupt source to create.
302  *
303  * \return  A pointer to a newly allocated Xen interrupt source
304  *          object or NULL.
305  */
306 static struct xenisrc *
307 xen_intr_alloc_isrc(enum evtchn_type type, int vector)
308 {
309         static int warned;
310         struct xenisrc *isrc;
311
312         KASSERT(mtx_owned(&xen_intr_isrc_lock), ("Evtchn alloc lock not held"));
313
314         if (xen_intr_auto_vector_count > NR_EVENT_CHANNELS) {
315                 if (!warned) {
316                         warned = 1;
317                         printf("xen_intr_alloc: Event channels exhausted.\n");
318                 }
319                 return (NULL);
320         }
321
322         if (type != EVTCHN_TYPE_PIRQ) {
323                 vector = first_evtchn_irq + xen_intr_auto_vector_count;
324                 xen_intr_auto_vector_count++;
325         }
326
327         KASSERT((intr_lookup_source(vector) == NULL),
328             ("Trying to use an already allocated vector"));
329
330         mtx_unlock(&xen_intr_isrc_lock);
331         isrc = malloc(sizeof(*isrc), M_XENINTR, M_WAITOK | M_ZERO);
332         isrc->xi_intsrc.is_pic =
333             (type == EVTCHN_TYPE_PIRQ) ? &xen_intr_pirq_pic : &xen_intr_pic;
334         isrc->xi_vector = vector;
335         isrc->xi_type = type;
336         intr_register_source(&isrc->xi_intsrc);
337         mtx_lock(&xen_intr_isrc_lock);
338
339         return (isrc);
340 }
341
342 /**
343  * Attempt to free an active Xen interrupt source object.
344  *
345  * \param isrc  The interrupt source object to release.
346  *
347  * \returns  EBUSY if the source is still in use, otherwise 0.
348  */
349 static int
350 xen_intr_release_isrc(struct xenisrc *isrc)
351 {
352
353         mtx_lock(&xen_intr_isrc_lock);
354         KASSERT(isrc->xi_intsrc.is_handlers == 0,
355             ("Release called, but xenisrc still in use"));
356         evtchn_mask_port(isrc->xi_port);
357         evtchn_clear_port(isrc->xi_port);
358
359         /* Rebind port to CPU 0. */
360         evtchn_cpu_mask_port(isrc->xi_cpu, isrc->xi_port);
361         evtchn_cpu_unmask_port(0, isrc->xi_port);
362
363         if (isrc->xi_close != 0 && is_valid_evtchn(isrc->xi_port)) {
364                 struct evtchn_close close = { .port = isrc->xi_port };
365                 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
366                         panic("EVTCHNOP_close failed");
367         }
368
369         xen_intr_port_to_isrc[isrc->xi_port] = NULL;
370         isrc->xi_cpu = 0;
371         isrc->xi_type = EVTCHN_TYPE_UNBOUND;
372         isrc->xi_port = 0;
373         isrc->xi_cookie = NULL;
374         mtx_unlock(&xen_intr_isrc_lock);
375         return (0);
376 }
377
378 /**
379  * Associate an interrupt handler with an already allocated local Xen
380  * event channel port.
381  *
382  * \param isrcp       The returned Xen interrupt object associated with
383  *                    the specified local port.
384  * \param local_port  The event channel to bind.
385  * \param type        The event channel type of local_port.
386  * \param intr_owner  The device making this bind request.
387  * \param filter      An interrupt filter handler.  Specify NULL
388  *                    to always dispatch to the ithread handler.
389  * \param handler     An interrupt ithread handler.  Optional (can
390  *                    specify NULL) if all necessary event actions
391  *                    are performed by filter.
392  * \param arg         Argument to present to both filter and handler.
393  * \param irqflags    Interrupt handler flags.  See sys/bus.h.
394  * \param handlep     Pointer to an opaque handle used to manage this
395  *                    registration.
396  *
397  * \returns  0 on success, otherwise an errno.
398  */
399 static int
400 xen_intr_bind_isrc(struct xenisrc **isrcp, evtchn_port_t local_port,
401     enum evtchn_type type, const char *intr_owner, driver_filter_t filter,
402     driver_intr_t handler, void *arg, enum intr_type flags,
403     xen_intr_handle_t *port_handlep)
404 {
405         struct xenisrc *isrc;
406         int error;
407
408         *isrcp = NULL;
409         if (port_handlep == NULL) {
410                 printf("%s: xen_intr_bind_isrc: Bad event handle\n",
411                     intr_owner);
412                 return (EINVAL);
413         }
414
415         mtx_lock(&xen_intr_isrc_lock);
416         isrc = xen_intr_find_unused_isrc(type);
417         if (isrc == NULL) {
418                 isrc = xen_intr_alloc_isrc(type, XEN_ALLOCATE_VECTOR);
419                 if (isrc == NULL) {
420                         mtx_unlock(&xen_intr_isrc_lock);
421                         return (ENOSPC);
422                 }
423         }
424         isrc->xi_port = local_port;
425         xen_intr_port_to_isrc[local_port] = isrc;
426         refcount_init(&isrc->xi_refcount, 1);
427         mtx_unlock(&xen_intr_isrc_lock);
428
429         /* Assign the opaque handler (the event channel port) */
430         *port_handlep = &isrc->xi_vector;
431
432 #ifdef SMP
433         if (type == EVTCHN_TYPE_PORT) {
434                 /*
435                  * By default all interrupts are assigned to vCPU#0
436                  * unless specified otherwise, so shuffle them to balance
437                  * the interrupt load.
438                  */
439                 xen_intr_assign_cpu(&isrc->xi_intsrc, intr_next_cpu(0));
440         }
441 #endif
442
443         if (filter == NULL && handler == NULL) {
444                 /*
445                  * No filter/handler provided, leave the event channel
446                  * masked and without a valid handler, the caller is
447                  * in charge of setting that up.
448                  */
449                 *isrcp = isrc;
450                 return (0);
451         }
452
453         error = xen_intr_add_handler(intr_owner, filter, handler, arg, flags,
454             *port_handlep);
455         if (error != 0) {
456                 xen_intr_release_isrc(isrc);
457                 return (error);
458         }
459         *isrcp = isrc;
460         return (0);
461 }
462
463 /**
464  * Lookup a Xen interrupt source object given an interrupt binding handle.
465  * 
466  * \param handle  A handle initialized by a previous call to
467  *                xen_intr_bind_isrc().
468  *
469  * \returns  A pointer to the Xen interrupt source object associated
470  *           with the given interrupt handle.  NULL if no association
471  *           currently exists.
472  */
473 static struct xenisrc *
474 xen_intr_isrc(xen_intr_handle_t handle)
475 {
476         int vector;
477
478         if (handle == NULL)
479                 return (NULL);
480
481         vector = *(int *)handle;
482         KASSERT(vector >= first_evtchn_irq &&
483             vector < (first_evtchn_irq + xen_intr_auto_vector_count),
484             ("Xen interrupt vector is out of range"));
485
486         return ((struct xenisrc *)intr_lookup_source(vector));
487 }
488
489 /**
490  * Determine the event channel ports at the given section of the
491  * event port bitmap which have pending events for the given cpu.
492  * 
493  * \param pcpu  The Xen interrupt pcpu data for the cpu being querried.
494  * \param sh    The Xen shared info area.
495  * \param idx   The index of the section of the event channel bitmap to
496  *              inspect.
497  *
498  * \returns  A u_long with bits set for every event channel with pending
499  *           events.
500  */
501 static inline u_long
502 xen_intr_active_ports(struct xen_intr_pcpu_data *pcpu, shared_info_t *sh,
503     u_int idx)
504 {
505
506         CTASSERT(sizeof(sh->evtchn_mask[0]) == sizeof(sh->evtchn_pending[0]));
507         CTASSERT(sizeof(sh->evtchn_mask[0]) == sizeof(pcpu->evtchn_enabled[0]));
508         CTASSERT(sizeof(sh->evtchn_mask) == sizeof(sh->evtchn_pending));
509         CTASSERT(sizeof(sh->evtchn_mask) == sizeof(pcpu->evtchn_enabled));
510         return (sh->evtchn_pending[idx]
511               & ~sh->evtchn_mask[idx]
512               & pcpu->evtchn_enabled[idx]);
513 }
514
515 /**
516  * Interrupt handler for processing all Xen event channel events.
517  * 
518  * \param trap_frame  The trap frame context for the current interrupt.
519  */
520 void
521 xen_intr_handle_upcall(struct trapframe *trap_frame)
522 {
523         u_int l1i, l2i, port, cpu;
524         u_long masked_l1, masked_l2;
525         struct xenisrc *isrc;
526         shared_info_t *s;
527         vcpu_info_t *v;
528         struct xen_intr_pcpu_data *pc;
529         u_long l1, l2;
530
531         /*
532          * Disable preemption in order to always check and fire events
533          * on the right vCPU
534          */
535         critical_enter();
536
537         cpu = PCPU_GET(cpuid);
538         pc  = DPCPU_PTR(xen_intr_pcpu);
539         s   = HYPERVISOR_shared_info;
540         v   = DPCPU_GET(vcpu_info);
541
542         if (xen_hvm_domain() && !xen_vector_callback_enabled) {
543                 KASSERT((cpu == 0), ("Fired PCI event callback on wrong CPU"));
544         }
545
546         v->evtchn_upcall_pending = 0;
547
548 #if 0
549 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
550         /* Clear master flag /before/ clearing selector flag. */
551         wmb();
552 #endif
553 #endif
554
555         l1 = atomic_readandclear_long(&v->evtchn_pending_sel);
556
557         l1i = pc->last_processed_l1i;
558         l2i = pc->last_processed_l2i;
559         (*pc->evtchn_intrcnt)++;
560
561         while (l1 != 0) {
562
563                 l1i = (l1i + 1) % LONG_BIT;
564                 masked_l1 = l1 & ((~0UL) << l1i);
565
566                 if (masked_l1 == 0) {
567                         /*
568                          * if we masked out all events, wrap around
569                          * to the beginning.
570                          */
571                         l1i = LONG_BIT - 1;
572                         l2i = LONG_BIT - 1;
573                         continue;
574                 }
575                 l1i = ffsl(masked_l1) - 1;
576
577                 do {
578                         l2 = xen_intr_active_ports(pc, s, l1i);
579
580                         l2i = (l2i + 1) % LONG_BIT;
581                         masked_l2 = l2 & ((~0UL) << l2i);
582
583                         if (masked_l2 == 0) {
584                                 /* if we masked out all events, move on */
585                                 l2i = LONG_BIT - 1;
586                                 break;
587                         }
588                         l2i = ffsl(masked_l2) - 1;
589
590                         /* process port */
591                         port = (l1i * LONG_BIT) + l2i;
592                         synch_clear_bit(port, &s->evtchn_pending[0]);
593
594                         isrc = xen_intr_port_to_isrc[port];
595                         if (__predict_false(isrc == NULL))
596                                 continue;
597
598                         /* Make sure we are firing on the right vCPU */
599                         KASSERT((isrc->xi_cpu == PCPU_GET(cpuid)),
600                                 ("Received unexpected event on vCPU#%d, event bound to vCPU#%d",
601                                 PCPU_GET(cpuid), isrc->xi_cpu));
602
603                         intr_execute_handlers(&isrc->xi_intsrc, trap_frame);
604
605                         /*
606                          * If this is the final port processed,
607                          * we'll pick up here+1 next time.
608                          */
609                         pc->last_processed_l1i = l1i;
610                         pc->last_processed_l2i = l2i;
611
612                 } while (l2i != LONG_BIT - 1);
613
614                 l2 = xen_intr_active_ports(pc, s, l1i);
615                 if (l2 == 0) {
616                         /*
617                          * We handled all ports, so we can clear the
618                          * selector bit.
619                          */
620                         l1 &= ~(1UL << l1i);
621                 }
622         }
623         critical_exit();
624 }
625
626 static int
627 xen_intr_init(void *dummy __unused)
628 {
629         shared_info_t *s = HYPERVISOR_shared_info;
630         struct xen_intr_pcpu_data *pcpu;
631         struct physdev_pirq_eoi_gmfn eoi_gmfn;
632         int i, rc;
633
634         if (!xen_domain())
635                 return (0);
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         /* Try to register PIRQ EOI map */
653         xen_intr_pirq_eoi_map = malloc(PAGE_SIZE, M_XENINTR, M_WAITOK | M_ZERO);
654         eoi_gmfn.gmfn = atop(vtophys(xen_intr_pirq_eoi_map));
655         rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
656         if (rc != 0 && bootverbose)
657                 printf("Xen interrupts: unable to register PIRQ EOI map\n");
658         else
659                 xen_intr_pirq_eoi_map_enabled = true;
660
661         intr_register_pic(&xen_intr_pic);
662         if (xen_pv_domain() && xen_initial_domain())
663                 intr_register_pic(&xen_intr_pirq_pic);
664
665         if (bootverbose)
666                 printf("Xen interrupt system initialized\n");
667
668         return (0);
669 }
670 SYSINIT(xen_intr_init, SI_SUB_INTR, SI_ORDER_SECOND, xen_intr_init, NULL);
671
672 static void
673 xen_intrcnt_init(void *dummy __unused)
674 {
675         unsigned int i;
676
677         if (!xen_domain())
678                 return;
679
680         /*
681          * Register interrupt count manually as we aren't guaranteed to see a
682          * call to xen_intr_assign_cpu() before our first interrupt.
683          */
684         CPU_FOREACH(i)
685                 xen_intr_intrcnt_add(i);
686 }
687 SYSINIT(xen_intrcnt_init, SI_SUB_INTR, SI_ORDER_MIDDLE, xen_intrcnt_init, NULL);
688
689 void
690 xen_intr_alloc_irqs(void)
691 {
692
693         first_evtchn_irq = num_io_irqs;
694         num_io_irqs += NR_EVENT_CHANNELS;
695 }
696
697 /*--------------------------- Common PIC Functions ---------------------------*/
698 /**
699  * Prepare this PIC for system suspension.
700  */
701 static void
702 xen_intr_suspend(struct pic *unused)
703 {
704 }
705
706 static void
707 xen_rebind_ipi(struct xenisrc *isrc)
708 {
709 #ifdef SMP
710         int cpu = isrc->xi_cpu;
711         int vcpu_id = pcpu_find(cpu)->pc_vcpu_id;
712         int error;
713         struct evtchn_bind_ipi bind_ipi = { .vcpu = vcpu_id };
714
715         error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
716                                             &bind_ipi);
717         if (error != 0)
718                 panic("unable to rebind xen IPI: %d", error);
719
720         isrc->xi_port = bind_ipi.port;
721         isrc->xi_cpu = 0;
722         xen_intr_port_to_isrc[bind_ipi.port] = isrc;
723
724         error = xen_intr_assign_cpu(&isrc->xi_intsrc,
725                                     cpu_apic_ids[cpu]);
726         if (error)
727                 panic("unable to bind xen IPI to CPU#%d: %d",
728                       cpu, error);
729
730         evtchn_unmask_port(bind_ipi.port);
731 #else
732         panic("Resume IPI event channel on UP");
733 #endif
734 }
735
736 static void
737 xen_rebind_virq(struct xenisrc *isrc)
738 {
739         int cpu = isrc->xi_cpu;
740         int vcpu_id = pcpu_find(cpu)->pc_vcpu_id;
741         int error;
742         struct evtchn_bind_virq bind_virq = { .virq = isrc->xi_virq,
743                                               .vcpu = vcpu_id };
744
745         error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
746                                             &bind_virq);
747         if (error != 0)
748                 panic("unable to rebind xen VIRQ#%d: %d", isrc->xi_virq, error);
749
750         isrc->xi_port = bind_virq.port;
751         isrc->xi_cpu = 0;
752         xen_intr_port_to_isrc[bind_virq.port] = isrc;
753
754 #ifdef SMP
755         error = xen_intr_assign_cpu(&isrc->xi_intsrc,
756                                     cpu_apic_ids[cpu]);
757         if (error)
758                 panic("unable to bind xen VIRQ#%d to CPU#%d: %d",
759                       isrc->xi_virq, cpu, error);
760 #endif
761
762         evtchn_unmask_port(bind_virq.port);
763 }
764
765 /**
766  * Return this PIC to service after being suspended.
767  */
768 static void
769 xen_intr_resume(struct pic *unused, bool suspend_cancelled)
770 {
771         shared_info_t *s = HYPERVISOR_shared_info;
772         struct xenisrc *isrc;
773         u_int isrc_idx;
774         int i;
775
776         if (suspend_cancelled)
777                 return;
778
779         /* Reset the per-CPU masks */
780         CPU_FOREACH(i) {
781                 struct xen_intr_pcpu_data *pcpu;
782
783                 pcpu = DPCPU_ID_PTR(i, xen_intr_pcpu);
784                 memset(pcpu->evtchn_enabled, i == 0 ? ~0 : 0,
785                     sizeof(pcpu->evtchn_enabled));
786         }
787
788         /* Mask all event channels. */
789         for (i = 0; i < nitems(s->evtchn_mask); i++)
790                 atomic_store_rel_long(&s->evtchn_mask[i], ~0);
791
792         /* Remove port -> isrc mappings */
793         memset(xen_intr_port_to_isrc, 0, sizeof(xen_intr_port_to_isrc));
794
795         /* Free unused isrcs and rebind VIRQs and IPIs */
796         for (isrc_idx = 0; isrc_idx < xen_intr_auto_vector_count; isrc_idx++) {
797                 u_int vector;
798
799                 vector = first_evtchn_irq + isrc_idx;
800                 isrc = (struct xenisrc *)intr_lookup_source(vector);
801                 if (isrc != NULL) {
802                         isrc->xi_port = 0;
803                         switch (isrc->xi_type) {
804                         case EVTCHN_TYPE_IPI:
805                                 xen_rebind_ipi(isrc);
806                                 break;
807                         case EVTCHN_TYPE_VIRQ:
808                                 xen_rebind_virq(isrc);
809                                 break;
810                         default:
811                                 break;
812                         }
813                 }
814         }
815 }
816
817 /**
818  * Disable a Xen interrupt source.
819  *
820  * \param isrc  The interrupt source to disable.
821  */
822 static void
823 xen_intr_disable_intr(struct intsrc *base_isrc)
824 {
825         struct xenisrc *isrc = (struct xenisrc *)base_isrc;
826
827         evtchn_mask_port(isrc->xi_port);
828 }
829
830 /**
831  * Determine the global interrupt vector number for
832  * a Xen interrupt source.
833  *
834  * \param isrc  The interrupt source to query.
835  *
836  * \return  The vector number corresponding to the given interrupt source.
837  */
838 static int
839 xen_intr_vector(struct intsrc *base_isrc)
840 {
841         struct xenisrc *isrc = (struct xenisrc *)base_isrc;
842
843         return (isrc->xi_vector);
844 }
845
846 /**
847  * Determine whether or not interrupt events are pending on the
848  * the given interrupt source.
849  *
850  * \param isrc  The interrupt source to query.
851  *
852  * \returns  0 if no events are pending, otherwise non-zero.
853  */
854 static int
855 xen_intr_source_pending(struct intsrc *isrc)
856 {
857         /*
858          * EventChannels are edge triggered and never masked.
859          * There can be no pending events.
860          */
861         return (0);
862 }
863
864 /**
865  * Perform configuration of an interrupt source.
866  *
867  * \param isrc  The interrupt source to configure.
868  * \param trig  Edge or level.
869  * \param pol   Active high or low.
870  *
871  * \returns  0 if no events are pending, otherwise non-zero.
872  */
873 static int
874 xen_intr_config_intr(struct intsrc *isrc, enum intr_trigger trig,
875     enum intr_polarity pol)
876 {
877         /* Configuration is only possible via the evtchn apis. */
878         return (ENODEV);
879 }
880
881 /**
882  * Configure CPU affinity for interrupt source event delivery.
883  *
884  * \param isrc     The interrupt source to configure.
885  * \param apic_id  The apic id of the CPU for handling future events.
886  *
887  * \returns  0 if successful, otherwise an errno.
888  */
889 static int
890 xen_intr_assign_cpu(struct intsrc *base_isrc, u_int apic_id)
891 {
892 #ifdef SMP
893         struct evtchn_bind_vcpu bind_vcpu;
894         struct xenisrc *isrc;
895         u_int to_cpu, vcpu_id;
896         int error, masked;
897
898         if (xen_vector_callback_enabled == 0)
899                 return (EOPNOTSUPP);
900
901         to_cpu = apic_cpuid(apic_id);
902         vcpu_id = pcpu_find(to_cpu)->pc_vcpu_id;
903
904         mtx_lock(&xen_intr_isrc_lock);
905         isrc = (struct xenisrc *)base_isrc;
906         if (!is_valid_evtchn(isrc->xi_port)) {
907                 mtx_unlock(&xen_intr_isrc_lock);
908                 return (EINVAL);
909         }
910
911         /*
912          * Mask the event channel while binding it to prevent interrupt
913          * delivery with an inconsistent state in isrc->xi_cpu.
914          */
915         masked = evtchn_test_and_set_mask(isrc->xi_port);
916         if ((isrc->xi_type == EVTCHN_TYPE_VIRQ) ||
917                 (isrc->xi_type == EVTCHN_TYPE_IPI)) {
918                 /*
919                  * Virtual IRQs are associated with a cpu by
920                  * the Hypervisor at evtchn_bind_virq time, so
921                  * all we need to do is update the per-CPU masks.
922                  */
923                 evtchn_cpu_mask_port(isrc->xi_cpu, isrc->xi_port);
924                 isrc->xi_cpu = to_cpu;
925                 evtchn_cpu_unmask_port(isrc->xi_cpu, isrc->xi_port);
926                 goto out;
927         }
928
929         bind_vcpu.port = isrc->xi_port;
930         bind_vcpu.vcpu = vcpu_id;
931
932         error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu);
933         if (isrc->xi_cpu != to_cpu) {
934                 if (error == 0) {
935                         /* Commit to new binding by removing the old one. */
936                         evtchn_cpu_mask_port(isrc->xi_cpu, isrc->xi_port);
937                         isrc->xi_cpu = to_cpu;
938                         evtchn_cpu_unmask_port(isrc->xi_cpu, isrc->xi_port);
939                 }
940         }
941
942 out:
943         if (masked == 0)
944                 evtchn_unmask_port(isrc->xi_port);
945         mtx_unlock(&xen_intr_isrc_lock);
946         return (0);
947 #else
948         return (EOPNOTSUPP);
949 #endif
950 }
951
952 /*------------------- Virtual Interrupt Source PIC Functions -----------------*/
953 /*
954  * Mask a level triggered interrupt source.
955  *
956  * \param isrc  The interrupt source to mask (if necessary).
957  * \param eoi   If non-zero, perform any necessary end-of-interrupt
958  *              acknowledgements.
959  */
960 static void
961 xen_intr_disable_source(struct intsrc *base_isrc, int eoi)
962 {
963         struct xenisrc *isrc;
964
965         isrc = (struct xenisrc *)base_isrc;
966
967         /*
968          * NB: checking if the event channel is already masked is
969          * needed because the event channel user-space device
970          * masks event channels on its filter as part of its
971          * normal operation, and those shouldn't be automatically
972          * unmasked by the generic interrupt code. The event channel
973          * device will unmask them when needed.
974          */
975         isrc->xi_masked = !!evtchn_test_and_set_mask(isrc->xi_port);
976 }
977
978 /*
979  * Unmask a level triggered interrupt source.
980  *
981  * \param isrc  The interrupt source to unmask (if necessary).
982  */
983 static void
984 xen_intr_enable_source(struct intsrc *base_isrc)
985 {
986         struct xenisrc *isrc;
987
988         isrc = (struct xenisrc *)base_isrc;
989
990         if (isrc->xi_masked == 0)
991                 evtchn_unmask_port(isrc->xi_port);
992 }
993
994 /*
995  * Perform any necessary end-of-interrupt acknowledgements.
996  *
997  * \param isrc  The interrupt source to EOI.
998  */
999 static void
1000 xen_intr_eoi_source(struct intsrc *base_isrc)
1001 {
1002 }
1003
1004 /*
1005  * Enable and unmask the interrupt source.
1006  *
1007  * \param isrc  The interrupt source to enable.
1008  */
1009 static void
1010 xen_intr_enable_intr(struct intsrc *base_isrc)
1011 {
1012         struct xenisrc *isrc = (struct xenisrc *)base_isrc;
1013
1014         evtchn_unmask_port(isrc->xi_port);
1015 }
1016
1017 /*------------------ Physical Interrupt Source PIC Functions -----------------*/
1018 /*
1019  * Mask a level triggered interrupt source.
1020  *
1021  * \param isrc  The interrupt source to mask (if necessary).
1022  * \param eoi   If non-zero, perform any necessary end-of-interrupt
1023  *              acknowledgements.
1024  */
1025 static void
1026 xen_intr_pirq_disable_source(struct intsrc *base_isrc, int eoi)
1027 {
1028         struct xenisrc *isrc;
1029
1030         isrc = (struct xenisrc *)base_isrc;
1031
1032         if (isrc->xi_edgetrigger == 0)
1033                 evtchn_mask_port(isrc->xi_port);
1034         if (eoi == PIC_EOI)
1035                 xen_intr_pirq_eoi_source(base_isrc);
1036 }
1037
1038 /*
1039  * Unmask a level triggered interrupt source.
1040  *
1041  * \param isrc  The interrupt source to unmask (if necessary).
1042  */
1043 static void
1044 xen_intr_pirq_enable_source(struct intsrc *base_isrc)
1045 {
1046         struct xenisrc *isrc;
1047
1048         isrc = (struct xenisrc *)base_isrc;
1049
1050         if (isrc->xi_edgetrigger == 0)
1051                 evtchn_unmask_port(isrc->xi_port);
1052 }
1053
1054 /*
1055  * Perform any necessary end-of-interrupt acknowledgements.
1056  *
1057  * \param isrc  The interrupt source to EOI.
1058  */
1059 static void
1060 xen_intr_pirq_eoi_source(struct intsrc *base_isrc)
1061 {
1062         struct xenisrc *isrc;
1063         int error;
1064
1065         isrc = (struct xenisrc *)base_isrc;
1066
1067         if (xen_test_bit(isrc->xi_pirq, xen_intr_pirq_eoi_map)) {
1068                 struct physdev_eoi eoi = { .irq = isrc->xi_pirq };
1069
1070                 error = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
1071                 if (error != 0)
1072                         panic("Unable to EOI PIRQ#%d: %d\n",
1073                             isrc->xi_pirq, error);
1074         }
1075 }
1076
1077 /*
1078  * Enable and unmask the interrupt source.
1079  *
1080  * \param isrc  The interrupt source to enable.
1081  */
1082 static void
1083 xen_intr_pirq_enable_intr(struct intsrc *base_isrc)
1084 {
1085         struct xenisrc *isrc;
1086         struct evtchn_bind_pirq bind_pirq;
1087         struct physdev_irq_status_query irq_status;
1088         int error;
1089
1090         isrc = (struct xenisrc *)base_isrc;
1091
1092         if (!xen_intr_pirq_eoi_map_enabled) {
1093                 irq_status.irq = isrc->xi_pirq;
1094                 error = HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query,
1095                     &irq_status);
1096                 if (error)
1097                         panic("unable to get status of IRQ#%d", isrc->xi_pirq);
1098
1099                 if (irq_status.flags & XENIRQSTAT_needs_eoi) {
1100                         /*
1101                          * Since the dynamic PIRQ EOI map is not available
1102                          * mark the PIRQ as needing EOI unconditionally.
1103                          */
1104                         xen_set_bit(isrc->xi_pirq, xen_intr_pirq_eoi_map);
1105                 }
1106         }
1107
1108         bind_pirq.pirq = isrc->xi_pirq;
1109         bind_pirq.flags = isrc->xi_edgetrigger ? 0 : BIND_PIRQ__WILL_SHARE;
1110         error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
1111         if (error)
1112                 panic("unable to bind IRQ#%d", isrc->xi_pirq);
1113
1114         isrc->xi_port = bind_pirq.port;
1115
1116         mtx_lock(&xen_intr_isrc_lock);
1117         KASSERT((xen_intr_port_to_isrc[bind_pirq.port] == NULL),
1118             ("trying to override an already setup event channel port"));
1119         xen_intr_port_to_isrc[bind_pirq.port] = isrc;
1120         mtx_unlock(&xen_intr_isrc_lock);
1121
1122         evtchn_unmask_port(isrc->xi_port);
1123 }
1124
1125 /*
1126  * Disable an interrupt source.
1127  *
1128  * \param isrc  The interrupt source to disable.
1129  */
1130 static void
1131 xen_intr_pirq_disable_intr(struct intsrc *base_isrc)
1132 {
1133         struct xenisrc *isrc;
1134         struct evtchn_close close;
1135         int error;
1136
1137         isrc = (struct xenisrc *)base_isrc;
1138
1139         evtchn_mask_port(isrc->xi_port);
1140
1141         close.port = isrc->xi_port;
1142         error = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
1143         if (error)
1144                 panic("unable to close event channel %d IRQ#%d",
1145                     isrc->xi_port, isrc->xi_pirq);
1146
1147         mtx_lock(&xen_intr_isrc_lock);
1148         xen_intr_port_to_isrc[isrc->xi_port] = NULL;
1149         mtx_unlock(&xen_intr_isrc_lock);
1150
1151         isrc->xi_port = 0;
1152 }
1153
1154 /**
1155  * Perform configuration of an interrupt source.
1156  *
1157  * \param isrc  The interrupt source to configure.
1158  * \param trig  Edge or level.
1159  * \param pol   Active high or low.
1160  *
1161  * \returns  0 if no events are pending, otherwise non-zero.
1162  */
1163 static int
1164 xen_intr_pirq_config_intr(struct intsrc *base_isrc, enum intr_trigger trig,
1165     enum intr_polarity pol)
1166 {
1167         struct xenisrc *isrc = (struct xenisrc *)base_isrc;
1168         struct physdev_setup_gsi setup_gsi;
1169         int error;
1170
1171         KASSERT(!(trig == INTR_TRIGGER_CONFORM || pol == INTR_POLARITY_CONFORM),
1172             ("%s: Conforming trigger or polarity\n", __func__));
1173
1174         setup_gsi.gsi = isrc->xi_pirq;
1175         setup_gsi.triggering = trig == INTR_TRIGGER_EDGE ? 0 : 1;
1176         setup_gsi.polarity = pol == INTR_POLARITY_HIGH ? 0 : 1;
1177
1178         error = HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi, &setup_gsi);
1179         if (error == -XEN_EEXIST) {
1180                 if ((isrc->xi_edgetrigger && (trig != INTR_TRIGGER_EDGE)) ||
1181                     (isrc->xi_activehi && (pol != INTR_POLARITY_HIGH)))
1182                         panic("unable to reconfigure interrupt IRQ#%d",
1183                             isrc->xi_pirq);
1184                 error = 0;
1185         }
1186         if (error)
1187                 panic("unable to configure IRQ#%d\n", isrc->xi_pirq);
1188
1189         isrc->xi_activehi = pol == INTR_POLARITY_HIGH ? 1 : 0;
1190         isrc->xi_edgetrigger = trig == INTR_TRIGGER_EDGE ? 1 : 0;
1191
1192         return (0);
1193 }
1194
1195 /*--------------------------- Public Functions -------------------------------*/
1196 /*------- API comments for these methods can be found in xen/xenintr.h -------*/
1197 int
1198 xen_intr_bind_local_port(device_t dev, evtchn_port_t local_port,
1199     driver_filter_t filter, driver_intr_t handler, void *arg,
1200     enum intr_type flags, xen_intr_handle_t *port_handlep)
1201 {
1202         struct xenisrc *isrc;
1203         int error;
1204
1205         error = xen_intr_bind_isrc(&isrc, local_port, EVTCHN_TYPE_PORT,
1206             device_get_nameunit(dev), filter, handler, arg, flags,
1207             port_handlep);
1208         if (error != 0)
1209                 return (error);
1210
1211         /*
1212          * The Event Channel API didn't open this port, so it is not
1213          * responsible for closing it automatically on unbind.
1214          */
1215         isrc->xi_close = 0;
1216         return (0);
1217 }
1218
1219 int
1220 xen_intr_alloc_and_bind_local_port(device_t dev, u_int remote_domain,
1221     driver_filter_t filter, driver_intr_t handler, void *arg,
1222     enum intr_type flags, xen_intr_handle_t *port_handlep)
1223 {
1224         struct xenisrc *isrc;
1225         struct evtchn_alloc_unbound alloc_unbound;
1226         int error;
1227
1228         alloc_unbound.dom        = DOMID_SELF;
1229         alloc_unbound.remote_dom = remote_domain;
1230         error = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
1231                     &alloc_unbound);
1232         if (error != 0) {
1233                 /*
1234                  * XXX Trap Hypercall error code Linuxisms in
1235                  *     the HYPERCALL layer.
1236                  */
1237                 return (-error);
1238         }
1239
1240         error = xen_intr_bind_isrc(&isrc, alloc_unbound.port, EVTCHN_TYPE_PORT,
1241             device_get_nameunit(dev), filter, handler, arg, flags,
1242             port_handlep);
1243         if (error != 0) {
1244                 evtchn_close_t close = { .port = alloc_unbound.port };
1245                 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
1246                         panic("EVTCHNOP_close failed");
1247                 return (error);
1248         }
1249
1250         isrc->xi_close = 1;
1251         return (0);
1252 }
1253
1254 int 
1255 xen_intr_bind_remote_port(device_t dev, u_int remote_domain,
1256     u_int remote_port, driver_filter_t filter, driver_intr_t handler,
1257     void *arg, enum intr_type flags, xen_intr_handle_t *port_handlep)
1258 {
1259         struct xenisrc *isrc;
1260         struct evtchn_bind_interdomain bind_interdomain;
1261         int error;
1262
1263         bind_interdomain.remote_dom  = remote_domain;
1264         bind_interdomain.remote_port = remote_port;
1265         error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
1266                                             &bind_interdomain);
1267         if (error != 0) {
1268                 /*
1269                  * XXX Trap Hypercall error code Linuxisms in
1270                  *     the HYPERCALL layer.
1271                  */
1272                 return (-error);
1273         }
1274
1275         error = xen_intr_bind_isrc(&isrc, bind_interdomain.local_port,
1276             EVTCHN_TYPE_PORT, device_get_nameunit(dev), filter, handler, arg,
1277             flags, port_handlep);
1278         if (error) {
1279                 evtchn_close_t close = { .port = bind_interdomain.local_port };
1280                 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
1281                         panic("EVTCHNOP_close failed");
1282                 return (error);
1283         }
1284
1285         /*
1286          * The Event Channel API opened this port, so it is
1287          * responsible for closing it automatically on unbind.
1288          */
1289         isrc->xi_close = 1;
1290         return (0);
1291 }
1292
1293 int 
1294 xen_intr_bind_virq(device_t dev, u_int virq, u_int cpu,
1295     driver_filter_t filter, driver_intr_t handler, void *arg,
1296     enum intr_type flags, xen_intr_handle_t *port_handlep)
1297 {
1298         int vcpu_id = pcpu_find(cpu)->pc_vcpu_id;
1299         struct xenisrc *isrc;
1300         struct evtchn_bind_virq bind_virq = { .virq = virq, .vcpu = vcpu_id };
1301         int error;
1302
1303         isrc = NULL;
1304         error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq, &bind_virq);
1305         if (error != 0) {
1306                 /*
1307                  * XXX Trap Hypercall error code Linuxisms in
1308                  *     the HYPERCALL layer.
1309                  */
1310                 return (-error);
1311         }
1312
1313         error = xen_intr_bind_isrc(&isrc, bind_virq.port, EVTCHN_TYPE_VIRQ,
1314             device_get_nameunit(dev), filter, handler, arg, flags,
1315             port_handlep);
1316
1317 #ifdef SMP
1318         if (error == 0)
1319                 error = intr_event_bind(isrc->xi_intsrc.is_event, cpu);
1320 #endif
1321
1322         if (error != 0) {
1323                 evtchn_close_t close = { .port = bind_virq.port };
1324
1325                 xen_intr_unbind(*port_handlep);
1326                 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
1327                         panic("EVTCHNOP_close failed");
1328                 return (error);
1329         }
1330
1331 #ifdef SMP
1332         if (isrc->xi_cpu != cpu) {
1333                 /*
1334                  * Too early in the boot process for the generic interrupt
1335                  * code to perform the binding.  Update our event channel
1336                  * masks manually so events can't fire on the wrong cpu
1337                  * during AP startup.
1338                  */
1339                 xen_intr_assign_cpu(&isrc->xi_intsrc, cpu_apic_ids[cpu]);
1340         }
1341 #endif
1342
1343         /*
1344          * The Event Channel API opened this port, so it is
1345          * responsible for closing it automatically on unbind.
1346          */
1347         isrc->xi_close = 1;
1348         isrc->xi_virq = virq;
1349
1350         return (0);
1351 }
1352
1353 int
1354 xen_intr_alloc_and_bind_ipi(u_int cpu, driver_filter_t filter,
1355     enum intr_type flags, xen_intr_handle_t *port_handlep)
1356 {
1357 #ifdef SMP
1358         int vcpu_id = pcpu_find(cpu)->pc_vcpu_id;
1359         struct xenisrc *isrc;
1360         struct evtchn_bind_ipi bind_ipi = { .vcpu = vcpu_id };
1361         /* Same size as the one used by intr_handler->ih_name. */
1362         char name[MAXCOMLEN + 1];
1363         int error;
1364
1365         isrc = NULL;
1366         error = HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi, &bind_ipi);
1367         if (error != 0) {
1368                 /*
1369                  * XXX Trap Hypercall error code Linuxisms in
1370                  *     the HYPERCALL layer.
1371                  */
1372                 return (-error);
1373         }
1374
1375         snprintf(name, sizeof(name), "cpu%u", cpu);
1376
1377         error = xen_intr_bind_isrc(&isrc, bind_ipi.port, EVTCHN_TYPE_IPI,
1378             name, filter, NULL, NULL, flags, port_handlep);
1379         if (error != 0) {
1380                 evtchn_close_t close = { .port = bind_ipi.port };
1381
1382                 xen_intr_unbind(*port_handlep);
1383                 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
1384                         panic("EVTCHNOP_close failed");
1385                 return (error);
1386         }
1387
1388         if (isrc->xi_cpu != cpu) {
1389                 /*
1390                  * Too early in the boot process for the generic interrupt
1391                  * code to perform the binding.  Update our event channel
1392                  * masks manually so events can't fire on the wrong cpu
1393                  * during AP startup.
1394                  */
1395                 xen_intr_assign_cpu(&isrc->xi_intsrc, cpu_apic_ids[cpu]);
1396         }
1397
1398         /*
1399          * The Event Channel API opened this port, so it is
1400          * responsible for closing it automatically on unbind.
1401          */
1402         isrc->xi_close = 1;
1403         return (0);
1404 #else
1405         return (EOPNOTSUPP);
1406 #endif
1407 }
1408
1409 int
1410 xen_register_pirq(int vector, enum intr_trigger trig, enum intr_polarity pol)
1411 {
1412         struct physdev_map_pirq map_pirq;
1413         struct xenisrc *isrc;
1414         int error;
1415
1416         if (vector == 0)
1417                 return (EINVAL);
1418
1419         if (bootverbose)
1420                 printf("xen: register IRQ#%d\n", vector);
1421
1422         map_pirq.domid = DOMID_SELF;
1423         map_pirq.type = MAP_PIRQ_TYPE_GSI;
1424         map_pirq.index = vector;
1425         map_pirq.pirq = vector;
1426
1427         error = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_pirq);
1428         if (error) {
1429                 printf("xen: unable to map IRQ#%d\n", vector);
1430                 return (error);
1431         }
1432
1433         mtx_lock(&xen_intr_isrc_lock);
1434         isrc = xen_intr_alloc_isrc(EVTCHN_TYPE_PIRQ, vector);
1435         mtx_unlock(&xen_intr_isrc_lock);
1436         KASSERT((isrc != NULL), ("xen: unable to allocate isrc for interrupt"));
1437         isrc->xi_pirq = vector;
1438         isrc->xi_activehi = pol == INTR_POLARITY_HIGH ? 1 : 0;
1439         isrc->xi_edgetrigger = trig == INTR_TRIGGER_EDGE ? 1 : 0;
1440
1441         return (0);
1442 }
1443
1444 int
1445 xen_register_msi(device_t dev, int vector, int count)
1446 {
1447         struct physdev_map_pirq msi_irq;
1448         struct xenisrc *isrc;
1449         int ret;
1450
1451         memset(&msi_irq, 0, sizeof(msi_irq));
1452         msi_irq.domid = DOMID_SELF;
1453         msi_irq.type = count == 1 ?
1454             MAP_PIRQ_TYPE_MSI_SEG : MAP_PIRQ_TYPE_MULTI_MSI;
1455         msi_irq.index = -1;
1456         msi_irq.pirq = -1;
1457         msi_irq.bus = pci_get_bus(dev) | (pci_get_domain(dev) << 16);
1458         msi_irq.devfn = (pci_get_slot(dev) << 3) | pci_get_function(dev);
1459         msi_irq.entry_nr = count;
1460
1461         ret = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &msi_irq);
1462         if (ret != 0)
1463                 return (ret);
1464         if (count != msi_irq.entry_nr) {
1465                 panic("unable to setup all requested MSI vectors "
1466                     "(expected %d got %d)", count, msi_irq.entry_nr);
1467         }
1468
1469         mtx_lock(&xen_intr_isrc_lock);
1470         for (int i = 0; i < count; i++) {
1471                 isrc = xen_intr_alloc_isrc(EVTCHN_TYPE_PIRQ, vector + i);
1472                 KASSERT(isrc != NULL,
1473                     ("xen: unable to allocate isrc for interrupt"));
1474                 isrc->xi_pirq = msi_irq.pirq + i;
1475                 /* MSI interrupts are always edge triggered */
1476                 isrc->xi_edgetrigger = 1;
1477         }
1478         mtx_unlock(&xen_intr_isrc_lock);
1479
1480         return (0);
1481 }
1482
1483 int
1484 xen_release_msi(int vector)
1485 {
1486         struct physdev_unmap_pirq unmap;
1487         struct xenisrc *isrc;
1488         int ret;
1489
1490         isrc = (struct xenisrc *)intr_lookup_source(vector);
1491         if (isrc == NULL)
1492                 return (ENXIO);
1493
1494         unmap.pirq = isrc->xi_pirq;
1495         ret = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap);
1496         if (ret != 0)
1497                 return (ret);
1498
1499         xen_intr_release_isrc(isrc);
1500
1501         return (0);
1502 }
1503
1504 int
1505 xen_intr_describe(xen_intr_handle_t port_handle, const char *fmt, ...)
1506 {
1507         char descr[MAXCOMLEN + 1];
1508         struct xenisrc *isrc;
1509         va_list ap;
1510
1511         isrc = xen_intr_isrc(port_handle);
1512         if (isrc == NULL)
1513                 return (EINVAL);
1514
1515         va_start(ap, fmt);
1516         vsnprintf(descr, sizeof(descr), fmt, ap);
1517         va_end(ap);
1518         return (intr_describe(isrc->xi_vector, isrc->xi_cookie, descr));
1519 }
1520
1521 void
1522 xen_intr_unbind(xen_intr_handle_t *port_handlep)
1523 {
1524         struct xenisrc *isrc;
1525
1526         KASSERT(port_handlep != NULL,
1527             ("NULL xen_intr_handle_t passed to xen_intr_unbind"));
1528
1529         isrc = xen_intr_isrc(*port_handlep);
1530         *port_handlep = NULL;
1531         if (isrc == NULL)
1532                 return;
1533
1534         mtx_lock(&xen_intr_isrc_lock);
1535         if (refcount_release(&isrc->xi_refcount) == 0) {
1536                 mtx_unlock(&xen_intr_isrc_lock);
1537                 return;
1538         }
1539         mtx_unlock(&xen_intr_isrc_lock);
1540
1541         if (isrc->xi_cookie != NULL)
1542                 intr_remove_handler(isrc->xi_cookie);
1543         xen_intr_release_isrc(isrc);
1544 }
1545
1546 void
1547 xen_intr_signal(xen_intr_handle_t handle)
1548 {
1549         struct xenisrc *isrc;
1550
1551         isrc = xen_intr_isrc(handle);
1552         if (isrc != NULL) {
1553                 KASSERT(isrc->xi_type == EVTCHN_TYPE_PORT ||
1554                         isrc->xi_type == EVTCHN_TYPE_IPI,
1555                         ("evtchn_signal on something other than a local port"));
1556                 struct evtchn_send send = { .port = isrc->xi_port };
1557                 (void)HYPERVISOR_event_channel_op(EVTCHNOP_send, &send);
1558         }
1559 }
1560
1561 evtchn_port_t
1562 xen_intr_port(xen_intr_handle_t handle)
1563 {
1564         struct xenisrc *isrc;
1565
1566         isrc = xen_intr_isrc(handle);
1567         if (isrc == NULL)
1568                 return (0);
1569         
1570         return (isrc->xi_port);
1571 }
1572
1573 int
1574 xen_intr_add_handler(const char *name, driver_filter_t filter,
1575     driver_intr_t handler, void *arg, enum intr_type flags,
1576     xen_intr_handle_t handle)
1577 {
1578         struct xenisrc *isrc;
1579         int error;
1580
1581         isrc = xen_intr_isrc(handle);
1582         if (isrc == NULL || isrc->xi_cookie != NULL)
1583                 return (EINVAL);
1584
1585         error = intr_add_handler(name, isrc->xi_vector,filter, handler, arg,
1586             flags|INTR_EXCL, &isrc->xi_cookie, 0);
1587         if (error != 0) {
1588                 printf(
1589                     "%s: xen_intr_add_handler: intr_add_handler failed: %d\n",
1590                     name, error);
1591         }
1592
1593         return (error);
1594 }
1595
1596 int
1597 xen_intr_get_evtchn_from_port(evtchn_port_t port, xen_intr_handle_t *handlep)
1598 {
1599
1600         if (!is_valid_evtchn(port) || port >= NR_EVENT_CHANNELS)
1601                 return (EINVAL);
1602
1603         if (handlep == NULL) {
1604                 return (EINVAL);
1605         }
1606
1607         mtx_lock(&xen_intr_isrc_lock);
1608         if (xen_intr_port_to_isrc[port] == NULL) {
1609                 mtx_unlock(&xen_intr_isrc_lock);
1610                 return (EINVAL);
1611         }
1612         refcount_acquire(&xen_intr_port_to_isrc[port]->xi_refcount);
1613         mtx_unlock(&xen_intr_isrc_lock);
1614
1615         /* Assign the opaque handler (the event channel port) */
1616         *handlep = &xen_intr_port_to_isrc[port]->xi_vector;
1617
1618         return (0);
1619 }
1620
1621 #ifdef DDB
1622 static const char *
1623 xen_intr_print_type(enum evtchn_type type)
1624 {
1625         static const char *evtchn_type_to_string[EVTCHN_TYPE_COUNT] = {
1626                 [EVTCHN_TYPE_UNBOUND]   = "UNBOUND",
1627                 [EVTCHN_TYPE_PIRQ]      = "PIRQ",
1628                 [EVTCHN_TYPE_VIRQ]      = "VIRQ",
1629                 [EVTCHN_TYPE_IPI]       = "IPI",
1630                 [EVTCHN_TYPE_PORT]      = "PORT",
1631         };
1632
1633         if (type >= EVTCHN_TYPE_COUNT)
1634                 return ("UNKNOWN");
1635
1636         return (evtchn_type_to_string[type]);
1637 }
1638
1639 static void
1640 xen_intr_dump_port(struct xenisrc *isrc)
1641 {
1642         struct xen_intr_pcpu_data *pcpu;
1643         shared_info_t *s = HYPERVISOR_shared_info;
1644         int i;
1645
1646         db_printf("Port %d Type: %s\n",
1647             isrc->xi_port, xen_intr_print_type(isrc->xi_type));
1648         if (isrc->xi_type == EVTCHN_TYPE_PIRQ) {
1649                 db_printf("\tPirq: %d ActiveHi: %d EdgeTrigger: %d "
1650                     "NeedsEOI: %d\n",
1651                     isrc->xi_pirq, isrc->xi_activehi, isrc->xi_edgetrigger,
1652                     !!xen_test_bit(isrc->xi_pirq, xen_intr_pirq_eoi_map));
1653         }
1654         if (isrc->xi_type == EVTCHN_TYPE_VIRQ)
1655                 db_printf("\tVirq: %d\n", isrc->xi_virq);
1656
1657         db_printf("\tMasked: %d Pending: %d\n",
1658             !!xen_test_bit(isrc->xi_port, &s->evtchn_mask[0]),
1659             !!xen_test_bit(isrc->xi_port, &s->evtchn_pending[0]));
1660
1661         db_printf("\tPer-CPU Masks: ");
1662         CPU_FOREACH(i) {
1663                 pcpu = DPCPU_ID_PTR(i, xen_intr_pcpu);
1664                 db_printf("cpu#%d: %d ", i,
1665                     !!xen_test_bit(isrc->xi_port, pcpu->evtchn_enabled));
1666         }
1667         db_printf("\n");
1668 }
1669
1670 DB_SHOW_COMMAND(xen_evtchn, db_show_xen_evtchn)
1671 {
1672         int i;
1673
1674         if (!xen_domain()) {
1675                 db_printf("Only available on Xen guests\n");
1676                 return;
1677         }
1678
1679         for (i = 0; i < NR_EVENT_CHANNELS; i++) {
1680                 struct xenisrc *isrc;
1681
1682                 isrc = xen_intr_port_to_isrc[i];
1683                 if (isrc == NULL)
1684                         continue;
1685
1686                 xen_intr_dump_port(isrc);
1687         }
1688 }
1689 #endif /* DDB */