]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/xen/xenpci/evtchn.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / xen / xenpci / evtchn.c
1 /******************************************************************************
2  * evtchn.c
3  *
4  * A simplified event channel for para-drivers in unmodified linux
5  *
6  * Copyright (c) 2002-2005, K A Fraser
7  * Copyright (c) 2005, Intel Corporation <xiaofeng.ling@intel.com>
8  *
9  * This file may be distributed separately from the Linux kernel, or
10  * incorporated into other software packages, subject to the following license:
11  *
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:
18  *
19  * The above copyright notice and this permission notice shall be included in
20  * all copies or substantial portions of the Software.
21  *
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
28  * IN THE SOFTWARE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/limits.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/interrupt.h>
43 #include <sys/pcpu.h>
44
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>
50 #include <sys/smp.h>
51
52 #include <dev/xen/xenpci/xenpcivar.h>
53
54 static inline unsigned long __ffs(unsigned long word)
55 {
56         __asm__("bsfq %1,%0"
57                 :"=r" (word)
58                 :"rm" (word));
59         return word;
60 }
61
62 #define is_valid_evtchn(x)      ((x) != 0)
63 #define evtchn_from_irq(x)      (irq_evtchn[irq].evtchn)
64
65 static struct {
66         struct mtx lock;
67         driver_intr_t *handler;
68         void *arg;
69         int evtchn;
70         int close:1; /* close on unbind_from_irqhandler()? */
71         int inuse:1;
72         int in_handler:1;
73         int mpsafe:1;
74 } irq_evtchn[256];
75 static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
76         [0 ...  NR_EVENT_CHANNELS-1] = -1 };
77
78 static struct mtx irq_alloc_lock;
79 static device_t xenpci_device;
80
81 #define ARRAY_SIZE(a)   (sizeof(a) / sizeof(a[0]))
82
83 static unsigned int
84 alloc_xen_irq(void)
85 {
86         static int warned;
87         unsigned int irq;
88
89         mtx_lock(&irq_alloc_lock);
90
91         for (irq = 1; irq < ARRAY_SIZE(irq_evtchn); irq++) {
92                 if (irq_evtchn[irq].inuse) 
93                         continue;
94                 irq_evtchn[irq].inuse = 1;
95                 mtx_unlock(&irq_alloc_lock);
96                 return irq;
97         }
98
99         if (!warned) {
100                 warned = 1;
101                 printf("alloc_xen_irq: No available IRQ to bind to: "
102                        "increase irq_evtchn[] size in evtchn.c.\n");
103         }
104
105         mtx_unlock(&irq_alloc_lock);
106
107         return -ENOSPC;
108 }
109
110 static void
111 free_xen_irq(int irq)
112 {
113
114         mtx_lock(&irq_alloc_lock);
115         irq_evtchn[irq].inuse = 0;
116         mtx_unlock(&irq_alloc_lock);
117 }
118
119 int
120 irq_to_evtchn_port(int irq)
121 {
122
123         return irq_evtchn[irq].evtchn;
124 }
125
126 void
127 mask_evtchn(int port)
128 {
129         shared_info_t *s = HYPERVISOR_shared_info;
130
131         synch_set_bit(port, &s->evtchn_mask[0]);
132 }
133
134 void
135 unmask_evtchn(int port)
136 {
137         evtchn_unmask_t op = { .port = port };
138
139         HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &op);
140 }
141
142 int
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)
146 {
147         struct evtchn_alloc_unbound alloc_unbound;
148         unsigned int irq;
149         int error;
150
151         irq = alloc_xen_irq();
152         if (irq < 0)
153                 return irq;
154
155         mtx_lock(&irq_evtchn[irq].lock);
156
157         alloc_unbound.dom        = DOMID_SELF;
158         alloc_unbound.remote_dom = remote_domain;
159         error = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
160                                           &alloc_unbound);
161         if (error) {
162                 mtx_unlock(&irq_evtchn[irq].lock);
163                 free_xen_irq(irq);
164                 return (-error);
165         }
166
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;
172
173         evtchn_to_irq[alloc_unbound.port] = irq;
174
175         unmask_evtchn(alloc_unbound.port);
176
177         mtx_unlock(&irq_evtchn[irq].lock);
178
179         if (irqp)
180                 *irqp = irq;
181         return (0);
182 }
183
184 int
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)
188 {
189         unsigned int irq;
190
191         irq = alloc_xen_irq();
192         if (irq < 0)
193                 return irq;
194
195         mtx_lock(&irq_evtchn[irq].lock);
196
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;
202
203         evtchn_to_irq[caller_port] = irq;
204
205         unmask_evtchn(caller_port);
206
207         mtx_unlock(&irq_evtchn[irq].lock);
208
209         if (irqp)
210                 *irqp = irq;
211         return (0);
212 }
213
214 void
215 unbind_from_irqhandler(unsigned int irq)
216 {
217         int evtchn;
218
219         mtx_lock(&irq_evtchn[irq].lock);
220
221         evtchn = evtchn_from_irq(irq);
222
223         if (is_valid_evtchn(evtchn)) {
224                 evtchn_to_irq[evtchn] = -1;
225                 mask_evtchn(evtchn);
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");
230                 }
231         }
232
233         irq_evtchn[irq].handler = NULL;
234         irq_evtchn[irq].evtchn  = 0;
235
236         mtx_unlock(&irq_evtchn[irq].lock);
237
238         while (irq_evtchn[irq].in_handler)
239                 cpu_relax();
240
241         free_xen_irq(irq);
242 }
243
244 void notify_remote_via_irq(int irq)
245 {
246         int evtchn;
247
248         evtchn = evtchn_from_irq(irq);
249         if (is_valid_evtchn(evtchn))
250                 notify_remote_via_evtchn(evtchn);
251 }
252
253 static inline unsigned long active_evtchns(unsigned int cpu, shared_info_t *sh,
254                                                 unsigned int idx)
255 {
256         return (sh->evtchn_pending[idx] & ~sh->evtchn_mask[idx]);
257 }
258
259 static void
260 evtchn_interrupt(void *arg)
261 {
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;
267         void *handler_arg;
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;
273
274         v->evtchn_upcall_pending = 0;
275
276 #if 0
277 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
278         /* Clear master flag /before/ clearing selector flag. */
279         wmb();
280 #endif
281 #endif
282
283         l1 = atomic_readandclear_long(&v->evtchn_pending_sel);
284
285         l1i = pc->pc_last_processed_l1i;
286         l2i = pc->pc_last_processed_l2i;
287
288         while (l1 != 0) {
289
290                 l1i = (l1i + 1) % LONG_BIT;
291                 masked_l1 = l1 & ((~0UL) << l1i);
292
293                 if (masked_l1 == 0) { /* if we masked out all events, wrap around to the beginning */
294                         l1i = LONG_BIT - 1;
295                         l2i = LONG_BIT - 1;
296                         continue;
297                 }
298                 l1i = __ffs(masked_l1);
299
300                 do {
301                         l2 = active_evtchns(cpu, s, l1i);
302
303                         l2i = (l2i + 1) % LONG_BIT;
304                         masked_l2 = l2 & ((~0UL) << l2i);
305
306                         if (masked_l2 == 0) { /* if we masked out all events, move on */
307                                 l2i = LONG_BIT - 1;
308                                 break;
309                         }
310                         l2i = __ffs(masked_l2);
311
312                         /* process port */
313                         port = (l1i * LONG_BIT) + l2i;
314                         synch_clear_bit(port, &s->evtchn_pending[0]);
315
316                         irq = evtchn_to_irq[port];
317                         if (irq < 0)
318                                 continue;
319
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",
326                                        irq, port);
327                                 mtx_unlock(&irq_evtchn[irq].lock);
328                                 continue;
329                         }
330                         irq_evtchn[irq].in_handler = 1;
331                         mtx_unlock(&irq_evtchn[irq].lock);
332
333                         //local_irq_enable();
334                         if (!handler_mpsafe)
335                                 mtx_lock(&Giant);
336                         handler(handler_arg);
337                         if (!handler_mpsafe)
338                                 mtx_unlock(&Giant);
339                         //local_irq_disable();
340
341                         mtx_lock(&irq_evtchn[irq].lock);
342                         irq_evtchn[irq].in_handler = 0;
343                         mtx_unlock(&irq_evtchn[irq].lock);
344
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;
348
349                 } while (l2i != LONG_BIT - 1);
350
351                 l2 = active_evtchns(cpu, s, l1i);
352                 if (l2 == 0) /* we handled all ports, so we can clear the selector bit */
353                         l1 &= ~(1UL << l1i);
354         }
355 }
356
357 void
358 irq_suspend(void)
359 {
360         struct xenpci_softc *scp = device_get_softc(xenpci_device);
361
362         /*
363          * Take our interrupt handler out of the list of handlers
364          * that can handle this irq.
365          */
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;
371         }
372 }
373
374 void
375 irq_resume(void)
376 {
377         struct xenpci_softc *scp = device_get_softc(xenpci_device);
378         int evtchn, irq;
379
380         for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++) {
381                 mask_evtchn(evtchn);
382                 evtchn_to_irq[evtchn] = -1;
383         }
384
385         for (irq = 0; irq < ARRAY_SIZE(irq_evtchn); irq++)
386                 irq_evtchn[irq].evtchn = 0;
387
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);
391 }
392
393 int
394 xenpci_irq_init(device_t device, struct xenpci_softc *scp)
395 {
396         int irq, cpu;
397         int error;
398
399         mtx_init(&irq_alloc_lock, "xen-irq-lock", NULL, MTX_DEF);
400
401         for (irq = 0; irq < ARRAY_SIZE(irq_evtchn); irq++)
402                 mtx_init(&irq_evtchn[irq].lock, "irq-evtchn", NULL, MTX_DEF);
403
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;
407         }
408
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);
412         if (error)
413                 return (error);
414
415         xenpci_device = device;
416
417         return (0);
418 }