1 /******************************************************************************
4 * A simplified event channel for para-drivers in unmodified linux
6 * Copyright (c) 2002-2005, K A Fraser
7 * Copyright (c) 2005, Intel Corporation <xiaofeng.ling@intel.com>
9 * This file may be distributed separately from the Linux kernel, or
10 * incorporated into other software packages, subject to the following license:
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/limits.h>
41 #include <sys/mutex.h>
42 #include <sys/interrupt.h>
45 #include <machine/xen/xen-os.h>
46 #include <machine/xen/xenvar.h>
47 #include <xen/hypervisor.h>
48 #include <xen/xen_intr.h>
49 #include <xen/evtchn.h>
52 #include <dev/xen/xenpci/xenpcivar.h>
54 static inline unsigned long __ffs(unsigned long word)
62 #define is_valid_evtchn(x) ((x) != 0)
63 #define evtchn_from_irq(x) (irq_evtchn[irq].evtchn)
67 driver_intr_t *handler;
70 int close:1; /* close on unbind_from_irqhandler()? */
75 static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
76 [0 ... NR_EVENT_CHANNELS-1] = -1 };
78 static struct mtx irq_alloc_lock;
79 static device_t xenpci_device;
81 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
89 mtx_lock(&irq_alloc_lock);
91 for (irq = 1; irq < ARRAY_SIZE(irq_evtchn); irq++) {
92 if (irq_evtchn[irq].inuse)
94 irq_evtchn[irq].inuse = 1;
95 mtx_unlock(&irq_alloc_lock);
101 printf("alloc_xen_irq: No available IRQ to bind to: "
102 "increase irq_evtchn[] size in evtchn.c.\n");
105 mtx_unlock(&irq_alloc_lock);
111 free_xen_irq(int irq)
114 mtx_lock(&irq_alloc_lock);
115 irq_evtchn[irq].inuse = 0;
116 mtx_unlock(&irq_alloc_lock);
120 irq_to_evtchn_port(int irq)
123 return irq_evtchn[irq].evtchn;
127 mask_evtchn(int port)
129 shared_info_t *s = HYPERVISOR_shared_info;
131 synch_set_bit(port, &s->evtchn_mask[0]);
135 unmask_evtchn(int port)
137 evtchn_unmask_t op = { .port = port };
139 HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &op);
143 bind_listening_port_to_irqhandler(unsigned int remote_domain,
144 const char *devname, driver_intr_t handler, void *arg,
145 unsigned long irqflags, unsigned int *irqp)
147 struct evtchn_alloc_unbound alloc_unbound;
151 irq = alloc_xen_irq();
155 mtx_lock(&irq_evtchn[irq].lock);
157 alloc_unbound.dom = DOMID_SELF;
158 alloc_unbound.remote_dom = remote_domain;
159 error = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
162 mtx_unlock(&irq_evtchn[irq].lock);
167 irq_evtchn[irq].handler = handler;
168 irq_evtchn[irq].arg = arg;
169 irq_evtchn[irq].evtchn = alloc_unbound.port;
170 irq_evtchn[irq].close = 1;
171 irq_evtchn[irq].mpsafe = (irqflags & INTR_MPSAFE) != 0;
173 evtchn_to_irq[alloc_unbound.port] = irq;
175 unmask_evtchn(alloc_unbound.port);
177 mtx_unlock(&irq_evtchn[irq].lock);
185 bind_caller_port_to_irqhandler(unsigned int caller_port,
186 const char *devname, driver_intr_t handler, void *arg,
187 unsigned long irqflags, unsigned int *irqp)
191 irq = alloc_xen_irq();
195 mtx_lock(&irq_evtchn[irq].lock);
197 irq_evtchn[irq].handler = handler;
198 irq_evtchn[irq].arg = arg;
199 irq_evtchn[irq].evtchn = caller_port;
200 irq_evtchn[irq].close = 0;
201 irq_evtchn[irq].mpsafe = (irqflags & INTR_MPSAFE) != 0;
203 evtchn_to_irq[caller_port] = irq;
205 unmask_evtchn(caller_port);
207 mtx_unlock(&irq_evtchn[irq].lock);
215 unbind_from_irqhandler(unsigned int irq)
219 mtx_lock(&irq_evtchn[irq].lock);
221 evtchn = evtchn_from_irq(irq);
223 if (is_valid_evtchn(evtchn)) {
224 evtchn_to_irq[evtchn] = -1;
226 if (irq_evtchn[irq].close) {
227 struct evtchn_close close = { .port = evtchn };
228 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close))
229 panic("EVTCHNOP_close failed");
233 irq_evtchn[irq].handler = NULL;
234 irq_evtchn[irq].evtchn = 0;
236 mtx_unlock(&irq_evtchn[irq].lock);
238 while (irq_evtchn[irq].in_handler)
244 void notify_remote_via_irq(int irq)
248 evtchn = evtchn_from_irq(irq);
249 if (is_valid_evtchn(evtchn))
250 notify_remote_via_evtchn(evtchn);
253 static inline unsigned long active_evtchns(unsigned int cpu, shared_info_t *sh,
256 return (sh->evtchn_pending[idx] & ~sh->evtchn_mask[idx]);
260 evtchn_interrupt(void *arg)
262 unsigned int l1i, l2i, port;
263 unsigned long masked_l1, masked_l2;
264 /* XXX: All events are bound to vcpu0 but irq may be redirected. */
265 int cpu = 0; /*smp_processor_id();*/
266 driver_intr_t *handler;
268 int irq, handler_mpsafe;
269 shared_info_t *s = HYPERVISOR_shared_info;
270 vcpu_info_t *v = &s->vcpu_info[cpu];
271 struct pcpu *pc = pcpu_find(cpu);
272 unsigned long l1, l2;
274 v->evtchn_upcall_pending = 0;
277 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
278 /* Clear master flag /before/ clearing selector flag. */
283 l1 = atomic_readandclear_long(&v->evtchn_pending_sel);
285 l1i = pc->pc_last_processed_l1i;
286 l2i = pc->pc_last_processed_l2i;
290 l1i = (l1i + 1) % LONG_BIT;
291 masked_l1 = l1 & ((~0UL) << l1i);
293 if (masked_l1 == 0) { /* if we masked out all events, wrap around to the beginning */
298 l1i = __ffs(masked_l1);
301 l2 = active_evtchns(cpu, s, l1i);
303 l2i = (l2i + 1) % LONG_BIT;
304 masked_l2 = l2 & ((~0UL) << l2i);
306 if (masked_l2 == 0) { /* if we masked out all events, move on */
310 l2i = __ffs(masked_l2);
313 port = (l1i * LONG_BIT) + l2i;
314 synch_clear_bit(port, &s->evtchn_pending[0]);
316 irq = evtchn_to_irq[port];
320 mtx_lock(&irq_evtchn[irq].lock);
321 handler = irq_evtchn[irq].handler;
322 handler_arg = irq_evtchn[irq].arg;
323 handler_mpsafe = irq_evtchn[irq].mpsafe;
324 if (unlikely(handler == NULL)) {
325 printf("Xen IRQ%d (port %d) has no handler!\n",
327 mtx_unlock(&irq_evtchn[irq].lock);
330 irq_evtchn[irq].in_handler = 1;
331 mtx_unlock(&irq_evtchn[irq].lock);
333 //local_irq_enable();
336 handler(handler_arg);
339 //local_irq_disable();
341 mtx_lock(&irq_evtchn[irq].lock);
342 irq_evtchn[irq].in_handler = 0;
343 mtx_unlock(&irq_evtchn[irq].lock);
345 /* if this is the final port processed, we'll pick up here+1 next time */
346 pc->pc_last_processed_l1i = l1i;
347 pc->pc_last_processed_l2i = l2i;
349 } while (l2i != LONG_BIT - 1);
351 l2 = active_evtchns(cpu, s, l1i);
352 if (l2 == 0) /* we handled all ports, so we can clear the selector bit */
360 struct xenpci_softc *scp = device_get_softc(xenpci_device);
363 * Take our interrupt handler out of the list of handlers
364 * that can handle this irq.
366 if (scp->intr_cookie != NULL) {
367 if (BUS_TEARDOWN_INTR(device_get_parent(xenpci_device),
368 xenpci_device, scp->res_irq, scp->intr_cookie) != 0)
369 printf("intr teardown failed.. continuing\n");
370 scp->intr_cookie = NULL;
377 struct xenpci_softc *scp = device_get_softc(xenpci_device);
380 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++) {
382 evtchn_to_irq[evtchn] = -1;
385 for (irq = 0; irq < ARRAY_SIZE(irq_evtchn); irq++)
386 irq_evtchn[irq].evtchn = 0;
388 BUS_SETUP_INTR(device_get_parent(xenpci_device),
389 xenpci_device, scp->res_irq, INTR_TYPE_MISC,
390 NULL, evtchn_interrupt, NULL, &scp->intr_cookie);
394 xenpci_irq_init(device_t device, struct xenpci_softc *scp)
399 mtx_init(&irq_alloc_lock, "xen-irq-lock", NULL, MTX_DEF);
401 for (irq = 0; irq < ARRAY_SIZE(irq_evtchn); irq++)
402 mtx_init(&irq_evtchn[irq].lock, "irq-evtchn", NULL, MTX_DEF);
404 for (cpu = 0; cpu < mp_ncpus; cpu++) {
405 pcpu_find(cpu)->pc_last_processed_l1i = LONG_BIT - 1;
406 pcpu_find(cpu)->pc_last_processed_l2i = LONG_BIT - 1;
409 error = BUS_SETUP_INTR(device_get_parent(device), device,
410 scp->res_irq, INTR_MPSAFE|INTR_TYPE_MISC, NULL, evtchn_interrupt,
411 NULL, &scp->intr_cookie);
415 xenpci_device = device;