1 /******************************************************************************
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
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>
41 #include <linux/errno.h>
42 #include <linux/miscdevice.h>
43 #include <linux/major.h>
44 #include <linux/proc_fs.h>
45 #include <linux/stat.h>
46 #include <linux/poll.h>
47 #include <linux/irq.h>
48 #include <linux/init.h>
49 #include <linux/gfp.h>
50 #include <linux/mutex.h>
51 #include <linux/cpu.h>
52 #include <xen/events.h>
53 #include <xen/evtchn.h>
54 #include <asm/xen/hypervisor.h>
56 struct per_user_data {
57 /* Notification ring, accessed via /dev/xen/evtchn. */
58 #define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t))
59 #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
61 unsigned int ring_cons, ring_prod, ring_overflow;
62 struct mutex ring_cons_mutex; /* protect against concurrent readers */
64 /* Processes wait on this queue when ring is empty. */
65 wait_queue_head_t evtchn_wait;
66 struct fasync_struct *evtchn_async_queue;
70 /* Who's bound to each port? */
71 static struct per_user_data *port_user[NR_EVENT_CHANNELS];
72 static DEFINE_SPINLOCK(port_user_lock);
74 irqreturn_t evtchn_interrupt(int irq, void *data)
76 unsigned int port = (unsigned long)data;
77 struct per_user_data *u;
79 spin_lock(&port_user_lock);
83 disable_irq_nosync(irq);
85 if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
86 u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port;
87 wmb(); /* Ensure ring contents visible */
88 if (u->ring_cons == u->ring_prod++) {
89 wake_up_interruptible(&u->evtchn_wait);
90 kill_fasync(&u->evtchn_async_queue,
97 spin_unlock(&port_user_lock);
102 static ssize_t evtchn_read(struct file *file, char __user *buf,
103 size_t count, loff_t *ppos)
106 unsigned int c, p, bytes1 = 0, bytes2 = 0;
107 struct per_user_data *u = file->private_data;
109 /* Whole number of ports. */
110 count &= ~(sizeof(evtchn_port_t)-1);
115 if (count > PAGE_SIZE)
119 mutex_lock(&u->ring_cons_mutex);
122 if (u->ring_overflow)
130 mutex_unlock(&u->ring_cons_mutex);
132 if (file->f_flags & O_NONBLOCK)
135 rc = wait_event_interruptible(u->evtchn_wait,
136 u->ring_cons != u->ring_prod);
141 /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
142 if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
143 bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) *
144 sizeof(evtchn_port_t);
145 bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t);
147 bytes1 = (p - c) * sizeof(evtchn_port_t);
151 /* Truncate chunks according to caller's maximum byte count. */
152 if (bytes1 > count) {
155 } else if ((bytes1 + bytes2) > count) {
156 bytes2 = count - bytes1;
160 rmb(); /* Ensure that we see the port before we copy it. */
161 if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
163 copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
166 u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
167 rc = bytes1 + bytes2;
170 mutex_unlock(&u->ring_cons_mutex);
174 static ssize_t evtchn_write(struct file *file, const char __user *buf,
175 size_t count, loff_t *ppos)
178 evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
179 struct per_user_data *u = file->private_data;
184 /* Whole number of ports. */
185 count &= ~(sizeof(evtchn_port_t)-1);
191 if (count > PAGE_SIZE)
195 if (copy_from_user(kbuf, buf, count) != 0)
198 spin_lock_irq(&port_user_lock);
199 for (i = 0; i < (count/sizeof(evtchn_port_t)); i++)
200 if ((kbuf[i] < NR_EVENT_CHANNELS) && (port_user[kbuf[i]] == u))
201 enable_irq(irq_from_evtchn(kbuf[i]));
202 spin_unlock_irq(&port_user_lock);
207 free_page((unsigned long)kbuf);
211 static int evtchn_bind_to_user(struct per_user_data *u, int port)
216 spin_lock_irq(&port_user_lock);
218 BUG_ON(port_user[port] != NULL);
220 irq = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
221 u->name, (void *)(unsigned long)port);
228 spin_unlock_irq(&port_user_lock);
232 static void evtchn_unbind_from_user(struct per_user_data *u, int port)
234 int irq = irq_from_evtchn(port);
236 unbind_from_irqhandler(irq, (void *)(unsigned long)port);
237 port_user[port] = NULL;
240 static long evtchn_ioctl(struct file *file,
241 unsigned int cmd, unsigned long arg)
244 struct per_user_data *u = file->private_data;
245 void __user *uarg = (void __user *) arg;
248 case IOCTL_EVTCHN_BIND_VIRQ: {
249 struct ioctl_evtchn_bind_virq bind;
250 struct evtchn_bind_virq bind_virq;
253 if (copy_from_user(&bind, uarg, sizeof(bind)))
256 bind_virq.virq = bind.virq;
258 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
263 rc = evtchn_bind_to_user(u, bind_virq.port);
269 case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
270 struct ioctl_evtchn_bind_interdomain bind;
271 struct evtchn_bind_interdomain bind_interdomain;
274 if (copy_from_user(&bind, uarg, sizeof(bind)))
277 bind_interdomain.remote_dom = bind.remote_domain;
278 bind_interdomain.remote_port = bind.remote_port;
279 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
284 rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
286 rc = bind_interdomain.local_port;
290 case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
291 struct ioctl_evtchn_bind_unbound_port bind;
292 struct evtchn_alloc_unbound alloc_unbound;
295 if (copy_from_user(&bind, uarg, sizeof(bind)))
298 alloc_unbound.dom = DOMID_SELF;
299 alloc_unbound.remote_dom = bind.remote_domain;
300 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
305 rc = evtchn_bind_to_user(u, alloc_unbound.port);
307 rc = alloc_unbound.port;
311 case IOCTL_EVTCHN_UNBIND: {
312 struct ioctl_evtchn_unbind unbind;
315 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
319 if (unbind.port >= NR_EVENT_CHANNELS)
322 spin_lock_irq(&port_user_lock);
325 if (port_user[unbind.port] != u) {
326 spin_unlock_irq(&port_user_lock);
330 evtchn_unbind_from_user(u, unbind.port);
332 spin_unlock_irq(&port_user_lock);
338 case IOCTL_EVTCHN_NOTIFY: {
339 struct ioctl_evtchn_notify notify;
342 if (copy_from_user(¬ify, uarg, sizeof(notify)))
345 if (notify.port >= NR_EVENT_CHANNELS) {
347 } else if (port_user[notify.port] != u) {
350 notify_remote_via_evtchn(notify.port);
356 case IOCTL_EVTCHN_RESET: {
357 /* Initialise the ring to empty. Clear errors. */
358 mutex_lock(&u->ring_cons_mutex);
359 spin_lock_irq(&port_user_lock);
360 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
361 spin_unlock_irq(&port_user_lock);
362 mutex_unlock(&u->ring_cons_mutex);
375 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
377 unsigned int mask = POLLOUT | POLLWRNORM;
378 struct per_user_data *u = file->private_data;
380 poll_wait(file, &u->evtchn_wait, wait);
381 if (u->ring_cons != u->ring_prod)
382 mask |= POLLIN | POLLRDNORM;
383 if (u->ring_overflow)
388 static int evtchn_fasync(int fd, struct file *filp, int on)
390 struct per_user_data *u = filp->private_data;
391 return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
394 static int evtchn_open(struct inode *inode, struct file *filp)
396 struct per_user_data *u;
398 u = kzalloc(sizeof(*u), GFP_KERNEL);
402 u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
403 if (u->name == NULL) {
408 init_waitqueue_head(&u->evtchn_wait);
410 u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
411 if (u->ring == NULL) {
417 mutex_init(&u->ring_cons_mutex);
419 filp->private_data = u;
424 static int evtchn_release(struct inode *inode, struct file *filp)
427 struct per_user_data *u = filp->private_data;
429 spin_lock_irq(&port_user_lock);
431 free_page((unsigned long)u->ring);
433 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
434 if (port_user[i] != u)
437 evtchn_unbind_from_user(port_user[i], i);
440 spin_unlock_irq(&port_user_lock);
448 static const struct file_operations evtchn_fops = {
449 .owner = THIS_MODULE,
451 .write = evtchn_write,
452 .unlocked_ioctl = evtchn_ioctl,
454 .fasync = evtchn_fasync,
456 .release = evtchn_release,
459 static struct miscdevice evtchn_miscdev = {
460 .minor = MISC_DYNAMIC_MINOR,
462 .fops = &evtchn_fops,
464 static int __init evtchn_init(void)
471 spin_lock_init(&port_user_lock);
472 memset(port_user, 0, sizeof(port_user));
474 /* Create '/dev/misc/evtchn'. */
475 err = misc_register(&evtchn_miscdev);
477 printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
481 printk(KERN_INFO "Event-channel device installed.\n");
486 static void __exit evtchn_cleanup(void)
488 misc_deregister(&evtchn_miscdev);
491 module_init(evtchn_init);
492 module_exit(evtchn_cleanup);
494 MODULE_LICENSE("GPL");