]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/xen/evtchn.c
f328f121c92d261aeca13708f6763148298fb7c5
[karo-tx-linux.git] / drivers / xen / evtchn.c
1 /******************************************************************************
2  * evtchn.c
3  *
4  * Driver for receiving and demuxing event-channel signals.
5  *
6  * Copyright (c) 2004-2005, K A Fraser
7  * Multi-process extensions Copyright (c) 2004, Steven Smith
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License version 2
11  * as published by the Free Software Foundation; or, when distributed
12  * separately from the Linux kernel or incorporated into other
13  * software packages, subject to the following license:
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a copy
16  * of this source file (the "Software"), to deal in the Software without
17  * restriction, including without limitation the rights to use, copy, modify,
18  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19  * and to permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice shall be included in
23  * all copies or substantial portions of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31  * IN THE SOFTWARE.
32  */
33
34 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
35
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/errno.h>
42 #include <linux/fs.h>
43 #include <linux/miscdevice.h>
44 #include <linux/major.h>
45 #include <linux/proc_fs.h>
46 #include <linux/stat.h>
47 #include <linux/poll.h>
48 #include <linux/irq.h>
49 #include <linux/init.h>
50 #include <linux/mutex.h>
51 #include <linux/cpu.h>
52
53 #include <xen/xen.h>
54 #include <xen/events.h>
55 #include <xen/evtchn.h>
56 #include <asm/xen/hypervisor.h>
57
58 struct per_user_data {
59         struct mutex bind_mutex; /* serialize bind/unbind operations */
60         struct rb_root evtchns;
61
62         /* Notification ring, accessed via /dev/xen/evtchn. */
63 #define EVTCHN_RING_SIZE     (PAGE_SIZE / sizeof(evtchn_port_t))
64 #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
65         evtchn_port_t *ring;
66         unsigned int ring_cons, ring_prod, ring_overflow;
67         struct mutex ring_cons_mutex; /* protect against concurrent readers */
68         spinlock_t ring_prod_lock; /* product against concurrent interrupts */
69
70         /* Processes wait on this queue when ring is empty. */
71         wait_queue_head_t evtchn_wait;
72         struct fasync_struct *evtchn_async_queue;
73         const char *name;
74 };
75
76 struct user_evtchn {
77         struct rb_node node;
78         struct per_user_data *user;
79         unsigned port;
80         bool enabled;
81 };
82
83 static int add_evtchn(struct per_user_data *u, struct user_evtchn *evtchn)
84 {
85         struct rb_node **new = &(u->evtchns.rb_node), *parent = NULL;
86
87         while (*new) {
88                 struct user_evtchn *this;
89
90                 this = container_of(*new, struct user_evtchn, node);
91
92                 parent = *new;
93                 if (this->port < evtchn->port)
94                         new = &((*new)->rb_left);
95                 else if (this->port > evtchn->port)
96                         new = &((*new)->rb_right);
97                 else
98                         return -EEXIST;
99         }
100
101         /* Add new node and rebalance tree. */
102         rb_link_node(&evtchn->node, parent, new);
103         rb_insert_color(&evtchn->node, &u->evtchns);
104
105         return 0;
106 }
107
108 static void del_evtchn(struct per_user_data *u, struct user_evtchn *evtchn)
109 {
110         rb_erase(&evtchn->node, &u->evtchns);
111         kfree(evtchn);
112 }
113
114 static struct user_evtchn *find_evtchn(struct per_user_data *u, unsigned port)
115 {
116         struct rb_node *node = u->evtchns.rb_node;
117
118         while (node) {
119                 struct user_evtchn *evtchn;
120
121                 evtchn = container_of(node, struct user_evtchn, node);
122
123                 if (evtchn->port < port)
124                         node = node->rb_left;
125                 else if (evtchn->port > port)
126                         node = node->rb_right;
127                 else
128                         return evtchn;
129         }
130         return NULL;
131 }
132
133 static irqreturn_t evtchn_interrupt(int irq, void *data)
134 {
135         struct user_evtchn *evtchn = data;
136         struct per_user_data *u = evtchn->user;
137
138         WARN(!evtchn->enabled,
139              "Interrupt for port %d, but apparently not enabled; per-user %p\n",
140              evtchn->port, u);
141
142         disable_irq_nosync(irq);
143         evtchn->enabled = false;
144
145         spin_lock(&u->ring_prod_lock);
146
147         if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
148                 u->ring[EVTCHN_RING_MASK(u->ring_prod)] = evtchn->port;
149                 wmb(); /* Ensure ring contents visible */
150                 if (u->ring_cons == u->ring_prod++) {
151                         wake_up_interruptible(&u->evtchn_wait);
152                         kill_fasync(&u->evtchn_async_queue,
153                                     SIGIO, POLL_IN);
154                 }
155         } else
156                 u->ring_overflow = 1;
157
158         spin_unlock(&u->ring_prod_lock);
159
160         return IRQ_HANDLED;
161 }
162
163 static ssize_t evtchn_read(struct file *file, char __user *buf,
164                            size_t count, loff_t *ppos)
165 {
166         int rc;
167         unsigned int c, p, bytes1 = 0, bytes2 = 0;
168         struct per_user_data *u = file->private_data;
169
170         /* Whole number of ports. */
171         count &= ~(sizeof(evtchn_port_t)-1);
172
173         if (count == 0)
174                 return 0;
175
176         if (count > PAGE_SIZE)
177                 count = PAGE_SIZE;
178
179         for (;;) {
180                 mutex_lock(&u->ring_cons_mutex);
181
182                 rc = -EFBIG;
183                 if (u->ring_overflow)
184                         goto unlock_out;
185
186                 c = u->ring_cons;
187                 p = u->ring_prod;
188                 if (c != p)
189                         break;
190
191                 mutex_unlock(&u->ring_cons_mutex);
192
193                 if (file->f_flags & O_NONBLOCK)
194                         return -EAGAIN;
195
196                 rc = wait_event_interruptible(u->evtchn_wait,
197                                               u->ring_cons != u->ring_prod);
198                 if (rc)
199                         return rc;
200         }
201
202         /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
203         if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
204                 bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) *
205                         sizeof(evtchn_port_t);
206                 bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t);
207         } else {
208                 bytes1 = (p - c) * sizeof(evtchn_port_t);
209                 bytes2 = 0;
210         }
211
212         /* Truncate chunks according to caller's maximum byte count. */
213         if (bytes1 > count) {
214                 bytes1 = count;
215                 bytes2 = 0;
216         } else if ((bytes1 + bytes2) > count) {
217                 bytes2 = count - bytes1;
218         }
219
220         rc = -EFAULT;
221         rmb(); /* Ensure that we see the port before we copy it. */
222         if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
223             ((bytes2 != 0) &&
224              copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
225                 goto unlock_out;
226
227         u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
228         rc = bytes1 + bytes2;
229
230  unlock_out:
231         mutex_unlock(&u->ring_cons_mutex);
232         return rc;
233 }
234
235 static ssize_t evtchn_write(struct file *file, const char __user *buf,
236                             size_t count, loff_t *ppos)
237 {
238         int rc, i;
239         evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
240         struct per_user_data *u = file->private_data;
241
242         if (kbuf == NULL)
243                 return -ENOMEM;
244
245         /* Whole number of ports. */
246         count &= ~(sizeof(evtchn_port_t)-1);
247
248         rc = 0;
249         if (count == 0)
250                 goto out;
251
252         if (count > PAGE_SIZE)
253                 count = PAGE_SIZE;
254
255         rc = -EFAULT;
256         if (copy_from_user(kbuf, buf, count) != 0)
257                 goto out;
258
259         mutex_lock(&u->bind_mutex);
260
261         for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
262                 unsigned port = kbuf[i];
263                 struct user_evtchn *evtchn;
264
265                 evtchn = find_evtchn(u, port);
266                 if (evtchn && !evtchn->enabled) {
267                         evtchn->enabled = true;
268                         enable_irq(irq_from_evtchn(port));
269                 }
270         }
271
272         mutex_unlock(&u->bind_mutex);
273
274         rc = count;
275
276  out:
277         free_page((unsigned long)kbuf);
278         return rc;
279 }
280
281 static int evtchn_bind_to_user(struct per_user_data *u, int port)
282 {
283         struct user_evtchn *evtchn;
284         struct evtchn_close close;
285         int rc = 0;
286
287         /*
288          * Ports are never reused, so every caller should pass in a
289          * unique port.
290          *
291          * (Locking not necessary because we haven't registered the
292          * interrupt handler yet, and our caller has already
293          * serialized bind operations.)
294          */
295
296         evtchn = kzalloc(sizeof(*evtchn), GFP_KERNEL);
297         if (!evtchn)
298                 return -ENOMEM;
299
300         evtchn->user = u;
301         evtchn->port = port;
302         evtchn->enabled = true; /* start enabled */
303
304         rc = add_evtchn(u, evtchn);
305         if (rc < 0)
306                 goto err;
307
308         rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
309                                        u->name, evtchn);
310         if (rc < 0)
311                 goto err;
312
313         rc = evtchn_make_refcounted(port);
314         return rc;
315
316 err:
317         /* bind failed, should close the port now */
318         close.port = port;
319         if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
320                 BUG();
321         del_evtchn(u, evtchn);
322         kfree(evtchn);
323         return rc;
324 }
325
326 static void evtchn_unbind_from_user(struct per_user_data *u,
327                                     struct user_evtchn *evtchn)
328 {
329         int irq = irq_from_evtchn(evtchn->port);
330
331         BUG_ON(irq < 0);
332
333         unbind_from_irqhandler(irq, evtchn);
334
335         del_evtchn(u, evtchn);
336 }
337
338 static long evtchn_ioctl(struct file *file,
339                          unsigned int cmd, unsigned long arg)
340 {
341         int rc;
342         struct per_user_data *u = file->private_data;
343         void __user *uarg = (void __user *) arg;
344
345         /* Prevent bind from racing with unbind */
346         mutex_lock(&u->bind_mutex);
347
348         switch (cmd) {
349         case IOCTL_EVTCHN_BIND_VIRQ: {
350                 struct ioctl_evtchn_bind_virq bind;
351                 struct evtchn_bind_virq bind_virq;
352
353                 rc = -EFAULT;
354                 if (copy_from_user(&bind, uarg, sizeof(bind)))
355                         break;
356
357                 bind_virq.virq = bind.virq;
358                 bind_virq.vcpu = 0;
359                 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
360                                                  &bind_virq);
361                 if (rc != 0)
362                         break;
363
364                 rc = evtchn_bind_to_user(u, bind_virq.port);
365                 if (rc == 0)
366                         rc = bind_virq.port;
367                 break;
368         }
369
370         case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
371                 struct ioctl_evtchn_bind_interdomain bind;
372                 struct evtchn_bind_interdomain bind_interdomain;
373
374                 rc = -EFAULT;
375                 if (copy_from_user(&bind, uarg, sizeof(bind)))
376                         break;
377
378                 bind_interdomain.remote_dom  = bind.remote_domain;
379                 bind_interdomain.remote_port = bind.remote_port;
380                 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
381                                                  &bind_interdomain);
382                 if (rc != 0)
383                         break;
384
385                 rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
386                 if (rc == 0)
387                         rc = bind_interdomain.local_port;
388                 break;
389         }
390
391         case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
392                 struct ioctl_evtchn_bind_unbound_port bind;
393                 struct evtchn_alloc_unbound alloc_unbound;
394
395                 rc = -EFAULT;
396                 if (copy_from_user(&bind, uarg, sizeof(bind)))
397                         break;
398
399                 alloc_unbound.dom        = DOMID_SELF;
400                 alloc_unbound.remote_dom = bind.remote_domain;
401                 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
402                                                  &alloc_unbound);
403                 if (rc != 0)
404                         break;
405
406                 rc = evtchn_bind_to_user(u, alloc_unbound.port);
407                 if (rc == 0)
408                         rc = alloc_unbound.port;
409                 break;
410         }
411
412         case IOCTL_EVTCHN_UNBIND: {
413                 struct ioctl_evtchn_unbind unbind;
414                 struct user_evtchn *evtchn;
415
416                 rc = -EFAULT;
417                 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
418                         break;
419
420                 rc = -EINVAL;
421                 if (unbind.port >= NR_EVENT_CHANNELS)
422                         break;
423
424                 rc = -ENOTCONN;
425                 evtchn = find_evtchn(u, unbind.port);
426                 if (!evtchn)
427                         break;
428
429                 disable_irq(irq_from_evtchn(unbind.port));
430                 evtchn_unbind_from_user(u, evtchn);
431                 rc = 0;
432                 break;
433         }
434
435         case IOCTL_EVTCHN_NOTIFY: {
436                 struct ioctl_evtchn_notify notify;
437                 struct user_evtchn *evtchn;
438
439                 rc = -EFAULT;
440                 if (copy_from_user(&notify, uarg, sizeof(notify)))
441                         break;
442
443                 rc = -ENOTCONN;
444                 evtchn = find_evtchn(u, notify.port);
445                 if (evtchn) {
446                         notify_remote_via_evtchn(notify.port);
447                         rc = 0;
448                 }
449                 break;
450         }
451
452         case IOCTL_EVTCHN_RESET: {
453                 /* Initialise the ring to empty. Clear errors. */
454                 mutex_lock(&u->ring_cons_mutex);
455                 spin_lock_irq(&u->ring_prod_lock);
456                 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
457                 spin_unlock_irq(&u->ring_prod_lock);
458                 mutex_unlock(&u->ring_cons_mutex);
459                 rc = 0;
460                 break;
461         }
462
463         default:
464                 rc = -ENOSYS;
465                 break;
466         }
467         mutex_unlock(&u->bind_mutex);
468
469         return rc;
470 }
471
472 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
473 {
474         unsigned int mask = POLLOUT | POLLWRNORM;
475         struct per_user_data *u = file->private_data;
476
477         poll_wait(file, &u->evtchn_wait, wait);
478         if (u->ring_cons != u->ring_prod)
479                 mask |= POLLIN | POLLRDNORM;
480         if (u->ring_overflow)
481                 mask = POLLERR;
482         return mask;
483 }
484
485 static int evtchn_fasync(int fd, struct file *filp, int on)
486 {
487         struct per_user_data *u = filp->private_data;
488         return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
489 }
490
491 static int evtchn_open(struct inode *inode, struct file *filp)
492 {
493         struct per_user_data *u;
494
495         u = kzalloc(sizeof(*u), GFP_KERNEL);
496         if (u == NULL)
497                 return -ENOMEM;
498
499         u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
500         if (u->name == NULL) {
501                 kfree(u);
502                 return -ENOMEM;
503         }
504
505         init_waitqueue_head(&u->evtchn_wait);
506
507         u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
508         if (u->ring == NULL) {
509                 kfree(u->name);
510                 kfree(u);
511                 return -ENOMEM;
512         }
513
514         mutex_init(&u->bind_mutex);
515         mutex_init(&u->ring_cons_mutex);
516         spin_lock_init(&u->ring_prod_lock);
517
518         filp->private_data = u;
519
520         return nonseekable_open(inode, filp);
521 }
522
523 static int evtchn_release(struct inode *inode, struct file *filp)
524 {
525         struct per_user_data *u = filp->private_data;
526         struct rb_node *node;
527
528         while ((node = u->evtchns.rb_node)) {
529                 struct user_evtchn *evtchn;
530
531                 evtchn = rb_entry(node, struct user_evtchn, node);
532                 disable_irq(irq_from_evtchn(evtchn->port));
533                 evtchn_unbind_from_user(u, evtchn);
534         }
535
536         free_page((unsigned long)u->ring);
537         kfree(u->name);
538         kfree(u);
539
540         return 0;
541 }
542
543 static const struct file_operations evtchn_fops = {
544         .owner   = THIS_MODULE,
545         .read    = evtchn_read,
546         .write   = evtchn_write,
547         .unlocked_ioctl = evtchn_ioctl,
548         .poll    = evtchn_poll,
549         .fasync  = evtchn_fasync,
550         .open    = evtchn_open,
551         .release = evtchn_release,
552         .llseek  = no_llseek,
553 };
554
555 static struct miscdevice evtchn_miscdev = {
556         .minor        = MISC_DYNAMIC_MINOR,
557         .name         = "xen/evtchn",
558         .fops         = &evtchn_fops,
559 };
560 static int __init evtchn_init(void)
561 {
562         int err;
563
564         if (!xen_domain())
565                 return -ENODEV;
566
567         /* Create '/dev/xen/evtchn'. */
568         err = misc_register(&evtchn_miscdev);
569         if (err != 0) {
570                 pr_err("Could not register /dev/xen/evtchn\n");
571                 return err;
572         }
573
574         pr_info("Event-channel device installed\n");
575
576         return 0;
577 }
578
579 static void __exit evtchn_cleanup(void)
580 {
581         misc_deregister(&evtchn_miscdev);
582 }
583
584 module_init(evtchn_init);
585 module_exit(evtchn_cleanup);
586
587 MODULE_LICENSE("GPL");