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/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>
52 #ifdef CONFIG_PARAVIRT_XEN
53 #include <xen/events.h>
54 #include <xen/evtchn.h>
55 #include <asm/xen/hypervisor.h>
57 #include <xen/evtchn.h>
58 #include <xen/public/evtchn.h>
59 #define bind_evtchn_to_irqhandler bind_caller_port_to_irqhandler
62 struct per_user_data {
63 struct mutex bind_mutex; /* serialize bind/unbind operations */
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))
69 unsigned int ring_cons, ring_prod, ring_overflow;
70 struct mutex ring_cons_mutex; /* protect against concurrent readers */
72 /* Processes wait on this queue when ring is empty. */
73 wait_queue_head_t evtchn_wait;
74 struct fasync_struct *evtchn_async_queue;
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.
82 static unsigned long *port_user;
83 static DEFINE_SPINLOCK(port_user_lock); /* protects port_user[] and ring_prod */
85 static inline struct per_user_data *get_port_user(unsigned port)
87 return (struct per_user_data *)(port_user[port] & ~1);
90 static inline void set_port_user(unsigned port, struct per_user_data *u)
92 port_user[port] = (unsigned long)u;
95 static inline bool get_port_enabled(unsigned port)
97 return port_user[port] & 1;
100 static inline void set_port_enabled(unsigned port, bool enabled)
103 port_user[port] |= 1;
105 port_user[port] &= ~1;
108 static irqreturn_t evtchn_interrupt(int irq, void *data)
110 unsigned int port = (unsigned long)data;
111 struct per_user_data *u;
113 spin_lock(&port_user_lock);
115 u = get_port_user(port);
117 WARN(!get_port_enabled(port),
118 "Interrupt for port %d, but apparently not enabled; per-user %p\n",
121 disable_irq_nosync(irq);
122 set_port_enabled(port, false);
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,
133 u->ring_overflow = 1;
135 spin_unlock(&port_user_lock);
140 static ssize_t evtchn_read(struct file *file, char __user *buf,
141 size_t count, loff_t *ppos)
144 unsigned int c, p, bytes1 = 0, bytes2 = 0;
145 struct per_user_data *u = file->private_data;
147 /* Whole number of ports. */
148 count &= ~(sizeof(evtchn_port_t)-1);
153 if (count > PAGE_SIZE)
157 mutex_lock(&u->ring_cons_mutex);
160 if (u->ring_overflow)
168 mutex_unlock(&u->ring_cons_mutex);
170 if (file->f_flags & O_NONBLOCK)
173 rc = wait_event_interruptible(u->evtchn_wait,
174 u->ring_cons != u->ring_prod);
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);
185 bytes1 = (p - c) * sizeof(evtchn_port_t);
189 /* Truncate chunks according to caller's maximum byte count. */
190 if (bytes1 > count) {
193 } else if ((bytes1 + bytes2) > count) {
194 bytes2 = count - bytes1;
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) ||
201 copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
204 u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
205 rc = bytes1 + bytes2;
208 mutex_unlock(&u->ring_cons_mutex);
212 static ssize_t evtchn_write(struct file *file, const char __user *buf,
213 size_t count, loff_t *ppos)
216 evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
217 struct per_user_data *u = file->private_data;
222 /* Whole number of ports. */
223 count &= ~(sizeof(evtchn_port_t)-1);
229 if (count > PAGE_SIZE)
233 if (copy_from_user(kbuf, buf, count) != 0)
236 spin_lock_irq(&port_user_lock);
238 for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
239 unsigned port = kbuf[i];
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));
249 spin_unlock_irq(&port_user_lock);
254 free_page((unsigned long)kbuf);
258 static int evtchn_bind_to_user(struct per_user_data *u, int port)
263 * Ports are never reused, so every caller should pass in a
266 * (Locking not necessary because we haven't registered the
267 * interrupt handler yet, and our caller has already
268 * serialized bind operations.)
270 BUG_ON(get_port_user(port) != NULL);
271 set_port_user(port, u);
272 set_port_enabled(port, true); /* start enabled */
274 rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
275 u->name, (void *)(unsigned long)port);
277 rc = evtchn_make_refcounted(port);
282 static void evtchn_unbind_from_user(struct per_user_data *u, int port)
284 int irq = irq_from_evtchn(port);
286 unbind_from_irqhandler(irq, (void *)(unsigned long)port);
288 WARN_ON(close_evtchn(port));
291 set_port_user(port, NULL);
294 static long evtchn_ioctl(struct file *file,
295 unsigned int cmd, unsigned long arg)
298 struct per_user_data *u = file->private_data;
299 void __user *uarg = (void __user *) arg;
301 /* Prevent bind from racing with unbind */
302 mutex_lock(&u->bind_mutex);
305 case IOCTL_EVTCHN_BIND_VIRQ: {
306 struct ioctl_evtchn_bind_virq bind;
307 struct evtchn_bind_virq bind_virq;
310 if (copy_from_user(&bind, uarg, sizeof(bind)))
313 bind_virq.virq = bind.virq;
315 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
320 rc = evtchn_bind_to_user(u, bind_virq.port);
326 case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
327 struct ioctl_evtchn_bind_interdomain bind;
328 struct evtchn_bind_interdomain bind_interdomain;
331 if (copy_from_user(&bind, uarg, sizeof(bind)))
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,
341 rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
343 rc = bind_interdomain.local_port;
347 case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
348 struct ioctl_evtchn_bind_unbound_port bind;
349 struct evtchn_alloc_unbound alloc_unbound;
352 if (copy_from_user(&bind, uarg, sizeof(bind)))
355 alloc_unbound.dom = DOMID_SELF;
356 alloc_unbound.remote_dom = bind.remote_domain;
357 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
362 rc = evtchn_bind_to_user(u, alloc_unbound.port);
364 rc = alloc_unbound.port;
368 case IOCTL_EVTCHN_UNBIND: {
369 struct ioctl_evtchn_unbind unbind;
372 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
376 if (unbind.port >= NR_EVENT_CHANNELS)
379 spin_lock_irq(&port_user_lock);
382 if (get_port_user(unbind.port) != u) {
383 spin_unlock_irq(&port_user_lock);
387 disable_irq(irq_from_evtchn(unbind.port));
389 spin_unlock_irq(&port_user_lock);
391 evtchn_unbind_from_user(u, unbind.port);
397 case IOCTL_EVTCHN_NOTIFY: {
398 struct ioctl_evtchn_notify notify;
401 if (copy_from_user(¬ify, uarg, sizeof(notify)))
404 if (notify.port >= NR_EVENT_CHANNELS) {
406 } else if (get_port_user(notify.port) != u) {
409 notify_remote_via_evtchn(notify.port);
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);
430 mutex_unlock(&u->bind_mutex);
435 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
437 unsigned int mask = POLLOUT | POLLWRNORM;
438 struct per_user_data *u = file->private_data;
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)
448 static int evtchn_fasync(int fd, struct file *filp, int on)
450 struct per_user_data *u = filp->private_data;
451 return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
454 static int evtchn_open(struct inode *inode, struct file *filp)
456 struct per_user_data *u;
458 u = kzalloc(sizeof(*u), GFP_KERNEL);
462 u->name = kasprintf(GFP_KERNEL, "evtchn:%s[%d]",
463 current->comm, current->pid);
464 if (u->name == NULL) {
469 init_waitqueue_head(&u->evtchn_wait);
471 u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
472 if (u->ring == NULL) {
478 mutex_init(&u->bind_mutex);
479 mutex_init(&u->ring_cons_mutex);
481 filp->private_data = u;
483 return nonseekable_open(inode, filp);
486 static int evtchn_release(struct inode *inode, struct file *filp)
489 struct per_user_data *u = filp->private_data;
491 spin_lock_irq(&port_user_lock);
493 free_page((unsigned long)u->ring);
495 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
496 if (get_port_user(i) != u)
499 disable_irq(irq_from_evtchn(i));
502 spin_unlock_irq(&port_user_lock);
504 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
505 if (get_port_user(i) != u)
508 evtchn_unbind_from_user(get_port_user(i), i);
517 static const struct file_operations evtchn_fops = {
518 .owner = THIS_MODULE,
520 .write = evtchn_write,
521 .unlocked_ioctl = evtchn_ioctl,
523 .fasync = evtchn_fasync,
525 .release = evtchn_release,
529 static struct miscdevice evtchn_miscdev = {
530 .minor = MISC_DYNAMIC_MINOR,
531 #ifdef CONFIG_PARAVIRT_XEN
532 .name = "xen/evtchn",
536 .nodename = "xen/evtchn",
537 .fops = &evtchn_fops,
539 static int __init evtchn_init(void)
546 port_user = kcalloc(NR_EVENT_CHANNELS, sizeof(*port_user), GFP_KERNEL);
547 if (port_user == NULL)
550 spin_lock_init(&port_user_lock);
552 /* Create '/dev/xen/evtchn'. */
553 err = misc_register(&evtchn_miscdev);
555 pr_alert("Could not register /dev/xen/evtchn\n");
559 printk(KERN_INFO "Event-channel device installed.\n");
564 static void __exit evtchn_cleanup(void)
569 misc_deregister(&evtchn_miscdev);
572 module_init(evtchn_init);
573 module_exit(evtchn_cleanup);
575 MODULE_LICENSE("GPL");
576 MODULE_ALIAS("devname:xen/evtchn");