Linux 2.6.34-rc3
[pohmelfs.git] / drivers / xen / evtchn.c
blobf70a4f4698c5994348a14dd216bb91ad72ec5b6a
1 /******************************************************************************
2 * evtchn.c
4 * Driver for receiving and demuxing event-channel signals.
6 * Copyright (c) 2004-2005, K A Fraser
7 * Multi-process extensions Copyright (c) 2004, Steven Smith
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:
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:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
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.
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/gfp.h>
49 #include <linux/mutex.h>
50 #include <linux/cpu.h>
52 #include <xen/xen.h>
53 #include <xen/events.h>
54 #include <xen/evtchn.h>
55 #include <asm/xen/hypervisor.h>
57 struct per_user_data {
58 struct mutex bind_mutex; /* serialize bind/unbind operations */
60 /* Notification ring, accessed via /dev/xen/evtchn. */
61 #define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t))
62 #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
63 evtchn_port_t *ring;
64 unsigned int ring_cons, ring_prod, ring_overflow;
65 struct mutex ring_cons_mutex; /* protect against concurrent readers */
67 /* Processes wait on this queue when ring is empty. */
68 wait_queue_head_t evtchn_wait;
69 struct fasync_struct *evtchn_async_queue;
70 const char *name;
73 /* Who's bound to each port? */
74 static struct per_user_data *port_user[NR_EVENT_CHANNELS];
75 static DEFINE_SPINLOCK(port_user_lock); /* protects port_user[] and ring_prod */
77 irqreturn_t evtchn_interrupt(int irq, void *data)
79 unsigned int port = (unsigned long)data;
80 struct per_user_data *u;
82 spin_lock(&port_user_lock);
84 u = port_user[port];
86 disable_irq_nosync(irq);
88 if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
89 u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port;
90 wmb(); /* Ensure ring contents visible */
91 if (u->ring_cons == u->ring_prod++) {
92 wake_up_interruptible(&u->evtchn_wait);
93 kill_fasync(&u->evtchn_async_queue,
94 SIGIO, POLL_IN);
96 } else {
97 u->ring_overflow = 1;
100 spin_unlock(&port_user_lock);
102 return IRQ_HANDLED;
105 static ssize_t evtchn_read(struct file *file, char __user *buf,
106 size_t count, loff_t *ppos)
108 int rc;
109 unsigned int c, p, bytes1 = 0, bytes2 = 0;
110 struct per_user_data *u = file->private_data;
112 /* Whole number of ports. */
113 count &= ~(sizeof(evtchn_port_t)-1);
115 if (count == 0)
116 return 0;
118 if (count > PAGE_SIZE)
119 count = PAGE_SIZE;
121 for (;;) {
122 mutex_lock(&u->ring_cons_mutex);
124 rc = -EFBIG;
125 if (u->ring_overflow)
126 goto unlock_out;
128 c = u->ring_cons;
129 p = u->ring_prod;
130 if (c != p)
131 break;
133 mutex_unlock(&u->ring_cons_mutex);
135 if (file->f_flags & O_NONBLOCK)
136 return -EAGAIN;
138 rc = wait_event_interruptible(u->evtchn_wait,
139 u->ring_cons != u->ring_prod);
140 if (rc)
141 return rc;
144 /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
145 if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
146 bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) *
147 sizeof(evtchn_port_t);
148 bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t);
149 } else {
150 bytes1 = (p - c) * sizeof(evtchn_port_t);
151 bytes2 = 0;
154 /* Truncate chunks according to caller's maximum byte count. */
155 if (bytes1 > count) {
156 bytes1 = count;
157 bytes2 = 0;
158 } else if ((bytes1 + bytes2) > count) {
159 bytes2 = count - bytes1;
162 rc = -EFAULT;
163 rmb(); /* Ensure that we see the port before we copy it. */
164 if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
165 ((bytes2 != 0) &&
166 copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
167 goto unlock_out;
169 u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
170 rc = bytes1 + bytes2;
172 unlock_out:
173 mutex_unlock(&u->ring_cons_mutex);
174 return rc;
177 static ssize_t evtchn_write(struct file *file, const char __user *buf,
178 size_t count, loff_t *ppos)
180 int rc, i;
181 evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
182 struct per_user_data *u = file->private_data;
184 if (kbuf == NULL)
185 return -ENOMEM;
187 /* Whole number of ports. */
188 count &= ~(sizeof(evtchn_port_t)-1);
190 rc = 0;
191 if (count == 0)
192 goto out;
194 if (count > PAGE_SIZE)
195 count = PAGE_SIZE;
197 rc = -EFAULT;
198 if (copy_from_user(kbuf, buf, count) != 0)
199 goto out;
201 spin_lock_irq(&port_user_lock);
202 for (i = 0; i < (count/sizeof(evtchn_port_t)); i++)
203 if ((kbuf[i] < NR_EVENT_CHANNELS) && (port_user[kbuf[i]] == u))
204 enable_irq(irq_from_evtchn(kbuf[i]));
205 spin_unlock_irq(&port_user_lock);
207 rc = count;
209 out:
210 free_page((unsigned long)kbuf);
211 return rc;
214 static int evtchn_bind_to_user(struct per_user_data *u, int port)
216 int rc = 0;
219 * Ports are never reused, so every caller should pass in a
220 * unique port.
222 * (Locking not necessary because we haven't registered the
223 * interrupt handler yet, and our caller has already
224 * serialized bind operations.)
226 BUG_ON(port_user[port] != NULL);
227 port_user[port] = u;
229 rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
230 u->name, (void *)(unsigned long)port);
231 if (rc >= 0)
232 rc = 0;
234 return rc;
237 static void evtchn_unbind_from_user(struct per_user_data *u, int port)
239 int irq = irq_from_evtchn(port);
241 unbind_from_irqhandler(irq, (void *)(unsigned long)port);
243 /* make sure we unbind the irq handler before clearing the port */
244 barrier();
246 port_user[port] = NULL;
249 static long evtchn_ioctl(struct file *file,
250 unsigned int cmd, unsigned long arg)
252 int rc;
253 struct per_user_data *u = file->private_data;
254 void __user *uarg = (void __user *) arg;
256 /* Prevent bind from racing with unbind */
257 mutex_lock(&u->bind_mutex);
259 switch (cmd) {
260 case IOCTL_EVTCHN_BIND_VIRQ: {
261 struct ioctl_evtchn_bind_virq bind;
262 struct evtchn_bind_virq bind_virq;
264 rc = -EFAULT;
265 if (copy_from_user(&bind, uarg, sizeof(bind)))
266 break;
268 bind_virq.virq = bind.virq;
269 bind_virq.vcpu = 0;
270 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
271 &bind_virq);
272 if (rc != 0)
273 break;
275 rc = evtchn_bind_to_user(u, bind_virq.port);
276 if (rc == 0)
277 rc = bind_virq.port;
278 break;
281 case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
282 struct ioctl_evtchn_bind_interdomain bind;
283 struct evtchn_bind_interdomain bind_interdomain;
285 rc = -EFAULT;
286 if (copy_from_user(&bind, uarg, sizeof(bind)))
287 break;
289 bind_interdomain.remote_dom = bind.remote_domain;
290 bind_interdomain.remote_port = bind.remote_port;
291 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
292 &bind_interdomain);
293 if (rc != 0)
294 break;
296 rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
297 if (rc == 0)
298 rc = bind_interdomain.local_port;
299 break;
302 case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
303 struct ioctl_evtchn_bind_unbound_port bind;
304 struct evtchn_alloc_unbound alloc_unbound;
306 rc = -EFAULT;
307 if (copy_from_user(&bind, uarg, sizeof(bind)))
308 break;
310 alloc_unbound.dom = DOMID_SELF;
311 alloc_unbound.remote_dom = bind.remote_domain;
312 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
313 &alloc_unbound);
314 if (rc != 0)
315 break;
317 rc = evtchn_bind_to_user(u, alloc_unbound.port);
318 if (rc == 0)
319 rc = alloc_unbound.port;
320 break;
323 case IOCTL_EVTCHN_UNBIND: {
324 struct ioctl_evtchn_unbind unbind;
326 rc = -EFAULT;
327 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
328 break;
330 rc = -EINVAL;
331 if (unbind.port >= NR_EVENT_CHANNELS)
332 break;
334 spin_lock_irq(&port_user_lock);
336 rc = -ENOTCONN;
337 if (port_user[unbind.port] != u) {
338 spin_unlock_irq(&port_user_lock);
339 break;
342 evtchn_unbind_from_user(u, unbind.port);
344 spin_unlock_irq(&port_user_lock);
346 rc = 0;
347 break;
350 case IOCTL_EVTCHN_NOTIFY: {
351 struct ioctl_evtchn_notify notify;
353 rc = -EFAULT;
354 if (copy_from_user(&notify, uarg, sizeof(notify)))
355 break;
357 if (notify.port >= NR_EVENT_CHANNELS) {
358 rc = -EINVAL;
359 } else if (port_user[notify.port] != u) {
360 rc = -ENOTCONN;
361 } else {
362 notify_remote_via_evtchn(notify.port);
363 rc = 0;
365 break;
368 case IOCTL_EVTCHN_RESET: {
369 /* Initialise the ring to empty. Clear errors. */
370 mutex_lock(&u->ring_cons_mutex);
371 spin_lock_irq(&port_user_lock);
372 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
373 spin_unlock_irq(&port_user_lock);
374 mutex_unlock(&u->ring_cons_mutex);
375 rc = 0;
376 break;
379 default:
380 rc = -ENOSYS;
381 break;
383 mutex_unlock(&u->bind_mutex);
385 return rc;
388 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
390 unsigned int mask = POLLOUT | POLLWRNORM;
391 struct per_user_data *u = file->private_data;
393 poll_wait(file, &u->evtchn_wait, wait);
394 if (u->ring_cons != u->ring_prod)
395 mask |= POLLIN | POLLRDNORM;
396 if (u->ring_overflow)
397 mask = POLLERR;
398 return mask;
401 static int evtchn_fasync(int fd, struct file *filp, int on)
403 struct per_user_data *u = filp->private_data;
404 return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
407 static int evtchn_open(struct inode *inode, struct file *filp)
409 struct per_user_data *u;
411 u = kzalloc(sizeof(*u), GFP_KERNEL);
412 if (u == NULL)
413 return -ENOMEM;
415 u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
416 if (u->name == NULL) {
417 kfree(u);
418 return -ENOMEM;
421 init_waitqueue_head(&u->evtchn_wait);
423 u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
424 if (u->ring == NULL) {
425 kfree(u->name);
426 kfree(u);
427 return -ENOMEM;
430 mutex_init(&u->bind_mutex);
431 mutex_init(&u->ring_cons_mutex);
433 filp->private_data = u;
435 return 0;
438 static int evtchn_release(struct inode *inode, struct file *filp)
440 int i;
441 struct per_user_data *u = filp->private_data;
443 spin_lock_irq(&port_user_lock);
445 free_page((unsigned long)u->ring);
447 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
448 if (port_user[i] != u)
449 continue;
451 evtchn_unbind_from_user(port_user[i], i);
454 spin_unlock_irq(&port_user_lock);
456 kfree(u->name);
457 kfree(u);
459 return 0;
462 static const struct file_operations evtchn_fops = {
463 .owner = THIS_MODULE,
464 .read = evtchn_read,
465 .write = evtchn_write,
466 .unlocked_ioctl = evtchn_ioctl,
467 .poll = evtchn_poll,
468 .fasync = evtchn_fasync,
469 .open = evtchn_open,
470 .release = evtchn_release,
473 static struct miscdevice evtchn_miscdev = {
474 .minor = MISC_DYNAMIC_MINOR,
475 .name = "evtchn",
476 .fops = &evtchn_fops,
478 static int __init evtchn_init(void)
480 int err;
482 if (!xen_domain())
483 return -ENODEV;
485 spin_lock_init(&port_user_lock);
486 memset(port_user, 0, sizeof(port_user));
488 /* Create '/dev/misc/evtchn'. */
489 err = misc_register(&evtchn_miscdev);
490 if (err != 0) {
491 printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
492 return err;
495 printk(KERN_INFO "Event-channel device installed.\n");
497 return 0;
500 static void __exit evtchn_cleanup(void)
502 misc_deregister(&evtchn_miscdev);
505 module_init(evtchn_init);
506 module_exit(evtchn_cleanup);
508 MODULE_LICENSE("GPL");