- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.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 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/string.h>
39 #include <linux/errno.h>
40 #include <linux/fs.h>
41 #include <linux/miscdevice.h>
42 #include <linux/major.h>
43 #include <linux/proc_fs.h>
44 #include <linux/stat.h>
45 #include <linux/poll.h>
46 #include <linux/irq.h>
47 #include <linux/init.h>
48 #include <linux/mutex.h>
49 #include <linux/cpu.h>
50
51 #include <xen/xen.h>
52 #ifdef CONFIG_PARAVIRT_XEN
53 #include <xen/events.h>
54 #include <xen/evtchn.h>
55 #include <asm/xen/hypervisor.h>
56 #else
57 #include <xen/evtchn.h>
58 #include <xen/public/evtchn.h>
59 #define bind_evtchn_to_irqhandler bind_caller_port_to_irqhandler
60 #endif
61
62 struct per_user_data {
63         struct mutex bind_mutex; /* serialize bind/unbind operations */
64
65         /* Notification ring, accessed via /dev/xen/evtchn. */
66 #define EVTCHN_RING_SIZE     (PAGE_SIZE / sizeof(evtchn_port_t))
67 #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
68         evtchn_port_t *ring;
69         unsigned int ring_cons, ring_prod, ring_overflow;
70         struct mutex ring_cons_mutex; /* protect against concurrent readers */
71
72         /* Processes wait on this queue when ring is empty. */
73         wait_queue_head_t evtchn_wait;
74         struct fasync_struct *evtchn_async_queue;
75         const char *name;
76 };
77
78 /*
79  * Who's bound to each port?  This is logically an array of struct
80  * per_user_data *, but we encode the current enabled-state in bit 0.
81  */
82 static unsigned long *port_user;
83 static DEFINE_SPINLOCK(port_user_lock); /* protects port_user[] and ring_prod */
84
85 static inline struct per_user_data *get_port_user(unsigned port)
86 {
87         return (struct per_user_data *)(port_user[port] & ~1);
88 }
89
90 static inline void set_port_user(unsigned port, struct per_user_data *u)
91 {
92         port_user[port] = (unsigned long)u;
93 }
94
95 static inline bool get_port_enabled(unsigned port)
96 {
97         return port_user[port] & 1;
98 }
99
100 static inline void set_port_enabled(unsigned port, bool enabled)
101 {
102         if (enabled)
103                 port_user[port] |= 1;
104         else
105                 port_user[port] &= ~1;
106 }
107
108 static irqreturn_t evtchn_interrupt(int irq, void *data)
109 {
110         unsigned int port = (unsigned long)data;
111         struct per_user_data *u;
112
113         spin_lock(&port_user_lock);
114
115         u = get_port_user(port);
116
117         WARN(!get_port_enabled(port),
118              "Interrupt for port %d, but apparently not enabled; per-user %p\n",
119              port, u);
120
121         disable_irq_nosync(irq);
122         set_port_enabled(port, false);
123
124         if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
125                 u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port;
126                 wmb(); /* Ensure ring contents visible */
127                 if (u->ring_cons == u->ring_prod++) {
128                         wake_up_interruptible(&u->evtchn_wait);
129                         kill_fasync(&u->evtchn_async_queue,
130                                     SIGIO, POLL_IN);
131                 }
132         } else
133                 u->ring_overflow = 1;
134
135         spin_unlock(&port_user_lock);
136
137         return IRQ_HANDLED;
138 }
139
140 static ssize_t evtchn_read(struct file *file, char __user *buf,
141                            size_t count, loff_t *ppos)
142 {
143         int rc;
144         unsigned int c, p, bytes1 = 0, bytes2 = 0;
145         struct per_user_data *u = file->private_data;
146
147         /* Whole number of ports. */
148         count &= ~(sizeof(evtchn_port_t)-1);
149
150         if (count == 0)
151                 return 0;
152
153         if (count > PAGE_SIZE)
154                 count = PAGE_SIZE;
155
156         for (;;) {
157                 mutex_lock(&u->ring_cons_mutex);
158
159                 rc = -EFBIG;
160                 if (u->ring_overflow)
161                         goto unlock_out;
162
163                 c = u->ring_cons;
164                 p = u->ring_prod;
165                 if (c != p)
166                         break;
167
168                 mutex_unlock(&u->ring_cons_mutex);
169
170                 if (file->f_flags & O_NONBLOCK)
171                         return -EAGAIN;
172
173                 rc = wait_event_interruptible(u->evtchn_wait,
174                                               u->ring_cons != u->ring_prod);
175                 if (rc)
176                         return rc;
177         }
178
179         /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
180         if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
181                 bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) *
182                         sizeof(evtchn_port_t);
183                 bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t);
184         } else {
185                 bytes1 = (p - c) * sizeof(evtchn_port_t);
186                 bytes2 = 0;
187         }
188
189         /* Truncate chunks according to caller's maximum byte count. */
190         if (bytes1 > count) {
191                 bytes1 = count;
192                 bytes2 = 0;
193         } else if ((bytes1 + bytes2) > count) {
194                 bytes2 = count - bytes1;
195         }
196
197         rc = -EFAULT;
198         rmb(); /* Ensure that we see the port before we copy it. */
199         if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
200             ((bytes2 != 0) &&
201              copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
202                 goto unlock_out;
203
204         u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
205         rc = bytes1 + bytes2;
206
207  unlock_out:
208         mutex_unlock(&u->ring_cons_mutex);
209         return rc;
210 }
211
212 static ssize_t evtchn_write(struct file *file, const char __user *buf,
213                             size_t count, loff_t *ppos)
214 {
215         int rc, i;
216         evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
217         struct per_user_data *u = file->private_data;
218
219         if (kbuf == NULL)
220                 return -ENOMEM;
221
222         /* Whole number of ports. */
223         count &= ~(sizeof(evtchn_port_t)-1);
224
225         rc = 0;
226         if (count == 0)
227                 goto out;
228
229         if (count > PAGE_SIZE)
230                 count = PAGE_SIZE;
231
232         rc = -EFAULT;
233         if (copy_from_user(kbuf, buf, count) != 0)
234                 goto out;
235
236         spin_lock_irq(&port_user_lock);
237
238         for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
239                 unsigned port = kbuf[i];
240
241                 if (port < NR_EVENT_CHANNELS &&
242                     get_port_user(port) == u &&
243                     !get_port_enabled(port)) {
244                         set_port_enabled(port, true);
245                         enable_irq(irq_from_evtchn(port));
246                 }
247         }
248
249         spin_unlock_irq(&port_user_lock);
250
251         rc = count;
252
253  out:
254         free_page((unsigned long)kbuf);
255         return rc;
256 }
257
258 static int evtchn_bind_to_user(struct per_user_data *u, int port)
259 {
260         int rc = 0;
261
262         /*
263          * Ports are never reused, so every caller should pass in a
264          * unique port.
265          *
266          * (Locking not necessary because we haven't registered the
267          * interrupt handler yet, and our caller has already
268          * serialized bind operations.)
269          */
270         BUG_ON(get_port_user(port) != NULL);
271         set_port_user(port, u);
272         set_port_enabled(port, true); /* start enabled */
273
274         rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
275                                        u->name, (void *)(unsigned long)port);
276         if (rc >= 0)
277                 rc = evtchn_make_refcounted(port);
278
279         return rc;
280 }
281
282 static void evtchn_unbind_from_user(struct per_user_data *u, int port)
283 {
284         int irq = irq_from_evtchn(port);
285
286         unbind_from_irqhandler(irq, (void *)(unsigned long)port);
287 #ifdef CONFIG_XEN
288         WARN_ON(close_evtchn(port));
289 #endif
290
291         set_port_user(port, NULL);
292 }
293
294 static long evtchn_ioctl(struct file *file,
295                          unsigned int cmd, unsigned long arg)
296 {
297         int rc;
298         struct per_user_data *u = file->private_data;
299         void __user *uarg = (void __user *) arg;
300
301         /* Prevent bind from racing with unbind */
302         mutex_lock(&u->bind_mutex);
303
304         switch (cmd) {
305         case IOCTL_EVTCHN_BIND_VIRQ: {
306                 struct ioctl_evtchn_bind_virq bind;
307                 struct evtchn_bind_virq bind_virq;
308
309                 rc = -EFAULT;
310                 if (copy_from_user(&bind, uarg, sizeof(bind)))
311                         break;
312
313                 bind_virq.virq = bind.virq;
314                 bind_virq.vcpu = 0;
315                 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
316                                                  &bind_virq);
317                 if (rc != 0)
318                         break;
319
320                 rc = evtchn_bind_to_user(u, bind_virq.port);
321                 if (rc == 0)
322                         rc = bind_virq.port;
323                 break;
324         }
325
326         case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
327                 struct ioctl_evtchn_bind_interdomain bind;
328                 struct evtchn_bind_interdomain bind_interdomain;
329
330                 rc = -EFAULT;
331                 if (copy_from_user(&bind, uarg, sizeof(bind)))
332                         break;
333
334                 bind_interdomain.remote_dom  = bind.remote_domain;
335                 bind_interdomain.remote_port = bind.remote_port;
336                 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
337                                                  &bind_interdomain);
338                 if (rc != 0)
339                         break;
340
341                 rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
342                 if (rc == 0)
343                         rc = bind_interdomain.local_port;
344                 break;
345         }
346
347         case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
348                 struct ioctl_evtchn_bind_unbound_port bind;
349                 struct evtchn_alloc_unbound alloc_unbound;
350
351                 rc = -EFAULT;
352                 if (copy_from_user(&bind, uarg, sizeof(bind)))
353                         break;
354
355                 alloc_unbound.dom        = DOMID_SELF;
356                 alloc_unbound.remote_dom = bind.remote_domain;
357                 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
358                                                  &alloc_unbound);
359                 if (rc != 0)
360                         break;
361
362                 rc = evtchn_bind_to_user(u, alloc_unbound.port);
363                 if (rc == 0)
364                         rc = alloc_unbound.port;
365                 break;
366         }
367
368         case IOCTL_EVTCHN_UNBIND: {
369                 struct ioctl_evtchn_unbind unbind;
370
371                 rc = -EFAULT;
372                 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
373                         break;
374
375                 rc = -EINVAL;
376                 if (unbind.port >= NR_EVENT_CHANNELS)
377                         break;
378
379                 spin_lock_irq(&port_user_lock);
380
381                 rc = -ENOTCONN;
382                 if (get_port_user(unbind.port) != u) {
383                         spin_unlock_irq(&port_user_lock);
384                         break;
385                 }
386
387                 disable_irq(irq_from_evtchn(unbind.port));
388
389                 spin_unlock_irq(&port_user_lock);
390
391                 evtchn_unbind_from_user(u, unbind.port);
392
393                 rc = 0;
394                 break;
395         }
396
397         case IOCTL_EVTCHN_NOTIFY: {
398                 struct ioctl_evtchn_notify notify;
399
400                 rc = -EFAULT;
401                 if (copy_from_user(&notify, uarg, sizeof(notify)))
402                         break;
403
404                 if (notify.port >= NR_EVENT_CHANNELS) {
405                         rc = -EINVAL;
406                 } else if (get_port_user(notify.port) != u) {
407                         rc = -ENOTCONN;
408                 } else {
409                         notify_remote_via_evtchn(notify.port);
410                         rc = 0;
411                 }
412                 break;
413         }
414
415         case IOCTL_EVTCHN_RESET: {
416                 /* Initialise the ring to empty. Clear errors. */
417                 mutex_lock(&u->ring_cons_mutex);
418                 spin_lock_irq(&port_user_lock);
419                 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
420                 spin_unlock_irq(&port_user_lock);
421                 mutex_unlock(&u->ring_cons_mutex);
422                 rc = 0;
423                 break;
424         }
425
426         default:
427                 rc = -ENOSYS;
428                 break;
429         }
430         mutex_unlock(&u->bind_mutex);
431
432         return rc;
433 }
434
435 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
436 {
437         unsigned int mask = POLLOUT | POLLWRNORM;
438         struct per_user_data *u = file->private_data;
439
440         poll_wait(file, &u->evtchn_wait, wait);
441         if (u->ring_cons != u->ring_prod)
442                 mask |= POLLIN | POLLRDNORM;
443         if (u->ring_overflow)
444                 mask = POLLERR;
445         return mask;
446 }
447
448 static int evtchn_fasync(int fd, struct file *filp, int on)
449 {
450         struct per_user_data *u = filp->private_data;
451         return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
452 }
453
454 static int evtchn_open(struct inode *inode, struct file *filp)
455 {
456         struct per_user_data *u;
457
458         u = kzalloc(sizeof(*u), GFP_KERNEL);
459         if (u == NULL)
460                 return -ENOMEM;
461
462         u->name = kasprintf(GFP_KERNEL, "evtchn:%s[%d]",
463                             current->comm, current->pid);
464         if (u->name == NULL) {
465                 kfree(u);
466                 return -ENOMEM;
467         }
468
469         init_waitqueue_head(&u->evtchn_wait);
470
471         u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
472         if (u->ring == NULL) {
473                 kfree(u->name);
474                 kfree(u);
475                 return -ENOMEM;
476         }
477
478         mutex_init(&u->bind_mutex);
479         mutex_init(&u->ring_cons_mutex);
480
481         filp->private_data = u;
482
483         return nonseekable_open(inode, filp);
484 }
485
486 static int evtchn_release(struct inode *inode, struct file *filp)
487 {
488         int i;
489         struct per_user_data *u = filp->private_data;
490
491         spin_lock_irq(&port_user_lock);
492
493         free_page((unsigned long)u->ring);
494
495         for (i = 0; i < NR_EVENT_CHANNELS; i++) {
496                 if (get_port_user(i) != u)
497                         continue;
498
499                 disable_irq(irq_from_evtchn(i));
500         }
501
502         spin_unlock_irq(&port_user_lock);
503
504         for (i = 0; i < NR_EVENT_CHANNELS; i++) {
505                 if (get_port_user(i) != u)
506                         continue;
507
508                 evtchn_unbind_from_user(get_port_user(i), i);
509         }
510
511         kfree(u->name);
512         kfree(u);
513
514         return 0;
515 }
516
517 static const struct file_operations evtchn_fops = {
518         .owner   = THIS_MODULE,
519         .read    = evtchn_read,
520         .write   = evtchn_write,
521         .unlocked_ioctl = evtchn_ioctl,
522         .poll    = evtchn_poll,
523         .fasync  = evtchn_fasync,
524         .open    = evtchn_open,
525         .release = evtchn_release,
526         .llseek  = no_llseek,
527 };
528
529 static struct miscdevice evtchn_miscdev = {
530         .minor        = MISC_DYNAMIC_MINOR,
531 #ifdef CONFIG_PARAVIRT_XEN
532         .name         = "xen/evtchn",
533 #else
534         .name         = "evtchn",
535 #endif
536         .nodename     = "xen/evtchn",
537         .fops         = &evtchn_fops,
538 };
539 static int __init evtchn_init(void)
540 {
541         int err;
542
543         if (!xen_domain())
544                 return -ENODEV;
545
546         port_user = kcalloc(NR_EVENT_CHANNELS, sizeof(*port_user), GFP_KERNEL);
547         if (port_user == NULL)
548                 return -ENOMEM;
549
550         spin_lock_init(&port_user_lock);
551
552         /* Create '/dev/xen/evtchn'. */
553         err = misc_register(&evtchn_miscdev);
554         if (err != 0) {
555                 pr_alert("Could not register /dev/xen/evtchn\n");
556                 return err;
557         }
558
559         printk(KERN_INFO "Event-channel device installed.\n");
560
561         return 0;
562 }
563
564 static void __exit evtchn_cleanup(void)
565 {
566         kfree(port_user);
567         port_user = NULL;
568
569         misc_deregister(&evtchn_miscdev);
570 }
571
572 module_init(evtchn_init);
573 module_exit(evtchn_cleanup);
574
575 MODULE_LICENSE("GPL");
576 MODULE_ALIAS("devname:xen/evtchn");