- 2.6.17 port work build breaks, but the patch set is relativly stable
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / evtchn / 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/config.h>
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/slab.h>
39 #include <linux/string.h>
40 #include <linux/errno.h>
41 #include <linux/fs.h>
42 #include <linux/errno.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/gfp.h>
51 #include <xen/evtchn.h>
52 #include <xen/public/evtchn.h>
53
54 struct per_user_data {
55         /* Notification ring, accessed via /dev/xen/evtchn. */
56 #define EVTCHN_RING_SIZE     (PAGE_SIZE / sizeof(evtchn_port_t))
57 #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
58         evtchn_port_t *ring;
59         unsigned int ring_cons, ring_prod, ring_overflow;
60
61         /* Processes wait on this queue when ring is empty. */
62         wait_queue_head_t evtchn_wait;
63         struct fasync_struct *evtchn_async_queue;
64 };
65
66 /* Who's bound to each port? */
67 static struct per_user_data *port_user[NR_EVENT_CHANNELS];
68 static spinlock_t port_user_lock;
69
70 void evtchn_device_upcall(int port)
71 {
72         struct per_user_data *u;
73
74         spin_lock(&port_user_lock);
75
76         mask_evtchn(port);
77         clear_evtchn(port);
78
79         if ((u = port_user[port]) != NULL) {
80                 if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
81                         u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port;
82                         if (u->ring_cons == u->ring_prod++) {
83                                 wake_up_interruptible(&u->evtchn_wait);
84                                 kill_fasync(&u->evtchn_async_queue,
85                                             SIGIO, POLL_IN);
86                         }
87                 } else {
88                         u->ring_overflow = 1;
89                 }
90         }
91
92         spin_unlock(&port_user_lock);
93 }
94
95 static ssize_t evtchn_read(struct file *file, char __user *buf,
96                            size_t count, loff_t *ppos)
97 {
98         int rc;
99         unsigned int c, p, bytes1 = 0, bytes2 = 0;
100         struct per_user_data *u = file->private_data;
101
102         /* Whole number of ports. */
103         count &= ~(sizeof(evtchn_port_t)-1);
104
105         if (count == 0)
106                 return 0;
107
108         if (count > PAGE_SIZE)
109                 count = PAGE_SIZE;
110
111         for (;;) {
112                 if (u->ring_overflow)
113                         return -EFBIG;
114
115                 if ((c = u->ring_cons) != (p = u->ring_prod))
116                         break;
117
118                 if (file->f_flags & O_NONBLOCK)
119                         return -EAGAIN;
120
121                 rc = wait_event_interruptible(
122                         u->evtchn_wait, u->ring_cons != u->ring_prod);
123                 if (rc)
124                         return rc;
125         }
126
127         /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
128         if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
129                 bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) *
130                         sizeof(evtchn_port_t);
131                 bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t);
132         } else {
133                 bytes1 = (p - c) * sizeof(evtchn_port_t);
134                 bytes2 = 0;
135         }
136
137         /* Truncate chunks according to caller's maximum byte count. */
138         if (bytes1 > count) {
139                 bytes1 = count;
140                 bytes2 = 0;
141         } else if ((bytes1 + bytes2) > count) {
142                 bytes2 = count - bytes1;
143         }
144
145         if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
146             ((bytes2 != 0) &&
147              copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
148                 return -EFAULT;
149
150         u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
151
152         return bytes1 + bytes2;
153 }
154
155 static ssize_t evtchn_write(struct file *file, const char __user *buf,
156                             size_t count, loff_t *ppos)
157 {
158         int  rc, i;
159         evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
160         struct per_user_data *u = file->private_data;
161
162         if (kbuf == NULL)
163                 return -ENOMEM;
164
165         /* Whole number of ports. */
166         count &= ~(sizeof(evtchn_port_t)-1);
167
168         if (count == 0) {
169                 rc = 0;
170                 goto out;
171         }
172
173         if (count > PAGE_SIZE)
174                 count = PAGE_SIZE;
175
176         if (copy_from_user(kbuf, buf, count) != 0) {
177                 rc = -EFAULT;
178                 goto out;
179         }
180
181         spin_lock_irq(&port_user_lock);
182         for (i = 0; i < (count/sizeof(evtchn_port_t)); i++)
183                 if ((kbuf[i] < NR_EVENT_CHANNELS) && (port_user[kbuf[i]] == u))
184                         unmask_evtchn(kbuf[i]);
185         spin_unlock_irq(&port_user_lock);
186
187         rc = count;
188
189  out:
190         free_page((unsigned long)kbuf);
191         return rc;
192 }
193
194 static void evtchn_bind_to_user(struct per_user_data *u, int port)
195 {
196         spin_lock_irq(&port_user_lock);
197         BUG_ON(port_user[port] != NULL);
198         port_user[port] = u;
199         unmask_evtchn(port);
200         spin_unlock_irq(&port_user_lock);
201 }
202
203 static int evtchn_ioctl(struct inode *inode, struct file *file,
204                         unsigned int cmd, unsigned long arg)
205 {
206         int rc;
207         struct per_user_data *u = file->private_data;
208         void __user *uarg = (void __user *) arg;
209         evtchn_op_t op = { 0 };
210
211         switch (cmd) {
212         case IOCTL_EVTCHN_BIND_VIRQ: {
213                 struct ioctl_evtchn_bind_virq bind;
214
215                 rc = -EFAULT;
216                 if (copy_from_user(&bind, uarg, sizeof(bind)))
217                         break;
218
219                 op.cmd = EVTCHNOP_bind_virq;
220                 op.u.bind_virq.virq = bind.virq;
221                 op.u.bind_virq.vcpu = 0;
222                 rc = HYPERVISOR_event_channel_op(&op);
223                 if (rc != 0)
224                         break;
225
226                 rc = op.u.bind_virq.port;
227                 evtchn_bind_to_user(u, rc);
228                 break;
229         }
230
231         case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
232                 struct ioctl_evtchn_bind_interdomain bind;
233
234                 rc = -EFAULT;
235                 if (copy_from_user(&bind, uarg, sizeof(bind)))
236                         break;
237
238                 op.cmd = EVTCHNOP_bind_interdomain;
239                 op.u.bind_interdomain.remote_dom  = bind.remote_domain;
240                 op.u.bind_interdomain.remote_port = bind.remote_port;
241                 rc = HYPERVISOR_event_channel_op(&op);
242                 if (rc != 0)
243                         break;
244
245                 rc = op.u.bind_interdomain.local_port;
246                 evtchn_bind_to_user(u, rc);
247                 break;
248         }
249
250         case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
251                 struct ioctl_evtchn_bind_unbound_port bind;
252
253                 rc = -EFAULT;
254                 if (copy_from_user(&bind, uarg, sizeof(bind)))
255                         break;
256
257                 op.cmd = EVTCHNOP_alloc_unbound;
258                 op.u.alloc_unbound.dom        = DOMID_SELF;
259                 op.u.alloc_unbound.remote_dom = bind.remote_domain;
260                 rc = HYPERVISOR_event_channel_op(&op);
261                 if (rc != 0)
262                         break;
263
264                 rc = op.u.alloc_unbound.port;
265                 evtchn_bind_to_user(u, rc);
266                 break;
267         }
268
269         case IOCTL_EVTCHN_UNBIND: {
270                 struct ioctl_evtchn_unbind unbind;
271                 int ret;
272
273                 rc = -EFAULT;
274                 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
275                         break;
276
277                 rc = -EINVAL;
278                 if (unbind.port >= NR_EVENT_CHANNELS)
279                         break;
280
281                 spin_lock_irq(&port_user_lock);
282     
283                 rc = -ENOTCONN;
284                 if (port_user[unbind.port] != u) {
285                         spin_unlock_irq(&port_user_lock);
286                         break;
287                 }
288
289                 port_user[unbind.port] = NULL;
290                 mask_evtchn(unbind.port);
291
292                 spin_unlock_irq(&port_user_lock);
293
294                 op.cmd = EVTCHNOP_close;
295                 op.u.close.port = unbind.port;
296                 ret = HYPERVISOR_event_channel_op(&op);
297                 BUG_ON(ret);
298
299                 rc = 0;
300                 break;
301         }
302
303         case IOCTL_EVTCHN_NOTIFY: {
304                 struct ioctl_evtchn_notify notify;
305
306                 rc = -EFAULT;
307                 if (copy_from_user(&notify, uarg, sizeof(notify)))
308                         break;
309
310                 if (notify.port >= NR_EVENT_CHANNELS) {
311                         rc = -EINVAL;
312                 } else if (port_user[notify.port] != u) {
313                         rc = -ENOTCONN;
314                 } else {
315                         notify_remote_via_evtchn(notify.port);
316                         rc = 0;
317                 }
318                 break;
319         }
320
321         case IOCTL_EVTCHN_RESET: {
322                 /* Initialise the ring to empty. Clear errors. */
323                 spin_lock_irq(&port_user_lock);
324                 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
325                 spin_unlock_irq(&port_user_lock);
326                 rc = 0;
327                 break;
328         }
329
330         default:
331                 rc = -ENOSYS;
332                 break;
333         }
334
335         return rc;
336 }
337
338 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
339 {
340         unsigned int mask = POLLOUT | POLLWRNORM;
341         struct per_user_data *u = file->private_data;
342
343         poll_wait(file, &u->evtchn_wait, wait);
344         if (u->ring_cons != u->ring_prod)
345                 mask |= POLLIN | POLLRDNORM;
346         if (u->ring_overflow)
347                 mask = POLLERR;
348         return mask;
349 }
350
351 static int evtchn_fasync(int fd, struct file *filp, int on)
352 {
353         struct per_user_data *u = filp->private_data;
354         return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
355 }
356
357 static int evtchn_open(struct inode *inode, struct file *filp)
358 {
359         struct per_user_data *u;
360
361         if ((u = kmalloc(sizeof(*u), GFP_KERNEL)) == NULL)
362                 return -ENOMEM;
363
364         memset(u, 0, sizeof(*u));
365         init_waitqueue_head(&u->evtchn_wait);
366
367         u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
368         if (u->ring == NULL) {
369                 kfree(u);
370                 return -ENOMEM;
371         }
372
373         filp->private_data = u;
374
375         return 0;
376 }
377
378 static int evtchn_release(struct inode *inode, struct file *filp)
379 {
380         int i;
381         struct per_user_data *u = filp->private_data;
382         evtchn_op_t op = { 0 };
383
384         spin_lock_irq(&port_user_lock);
385
386         free_page((unsigned long)u->ring);
387
388         for (i = 0; i < NR_EVENT_CHANNELS; i++) {
389                 int ret;
390                 if (port_user[i] != u)
391                         continue;
392
393                 port_user[i] = NULL;
394                 mask_evtchn(i);
395
396                 op.cmd = EVTCHNOP_close;
397                 op.u.close.port = i;
398                 ret = HYPERVISOR_event_channel_op(&op);
399                 BUG_ON(ret);
400         }
401
402         spin_unlock_irq(&port_user_lock);
403
404         kfree(u);
405
406         return 0;
407 }
408
409 static struct file_operations evtchn_fops = {
410         .owner   = THIS_MODULE,
411         .read    = evtchn_read,
412         .write   = evtchn_write,
413         .ioctl   = evtchn_ioctl,
414         .poll    = evtchn_poll,
415         .fasync  = evtchn_fasync,
416         .open    = evtchn_open,
417         .release = evtchn_release,
418 };
419
420 static struct miscdevice evtchn_miscdev = {
421         .minor        = EVTCHN_MINOR,
422         .name         = "evtchn",
423         .fops         = &evtchn_fops,
424         .devfs_name   = "misc/evtchn",
425 };
426
427 static int __init evtchn_init(void)
428 {
429         int err;
430
431         spin_lock_init(&port_user_lock);
432         memset(port_user, 0, sizeof(port_user));
433
434         /* Create '/dev/misc/evtchn'. */
435         err = misc_register(&evtchn_miscdev);
436         if (err != 0) {
437                 printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
438                 return err;
439         }
440
441         printk("Event-channel device installed.\n");
442
443         return 0;
444 }
445
446 static void evtchn_cleanup(void)
447 {
448         misc_deregister(&evtchn_miscdev);
449 }
450
451 module_init(evtchn_init);
452 module_exit(evtchn_cleanup);
453
454 MODULE_LICENSE("Dual BSD/GPL");
455
456 /*
457  * Local variables:
458  *  c-file-style: "linux"
459  *  indent-tabs-mode: t
460  *  c-indent-level: 8
461  *  c-basic-offset: 8
462  *  tab-width: 8
463  * End:
464  */