Merge tag 'staging-3.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6.git] / drivers / xen / evtchn.c
blob8feecf01d55c95c9241080cf36823f197b3b2d5c
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 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/errno.h>
42 #include <linux/fs.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/mutex.h>
51 #include <linux/cpu.h>
53 #include <xen/xen.h>
54 #include <xen/events.h>
55 #include <xen/evtchn.h>
56 #include <asm/xen/hypervisor.h>
58 struct per_user_data {
59 struct mutex bind_mutex; /* serialize bind/unbind operations */
61 /* Notification ring, accessed via /dev/xen/evtchn. */
62 #define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t))
63 #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
64 evtchn_port_t *ring;
65 unsigned int ring_cons, ring_prod, ring_overflow;
66 struct mutex ring_cons_mutex; /* protect against concurrent readers */
68 /* Processes wait on this queue when ring is empty. */
69 wait_queue_head_t evtchn_wait;
70 struct fasync_struct *evtchn_async_queue;
71 const char *name;
75 * Who's bound to each port? This is logically an array of struct
76 * per_user_data *, but we encode the current enabled-state in bit 0.
78 static unsigned long *port_user;
79 static DEFINE_SPINLOCK(port_user_lock); /* protects port_user[] and ring_prod */
81 static inline struct per_user_data *get_port_user(unsigned port)
83 return (struct per_user_data *)(port_user[port] & ~1);
86 static inline void set_port_user(unsigned port, struct per_user_data *u)
88 port_user[port] = (unsigned long)u;
91 static inline bool get_port_enabled(unsigned port)
93 return port_user[port] & 1;
96 static inline void set_port_enabled(unsigned port, bool enabled)
98 if (enabled)
99 port_user[port] |= 1;
100 else
101 port_user[port] &= ~1;
104 static irqreturn_t evtchn_interrupt(int irq, void *data)
106 unsigned int port = (unsigned long)data;
107 struct per_user_data *u;
109 spin_lock(&port_user_lock);
111 u = get_port_user(port);
113 WARN(!get_port_enabled(port),
114 "Interrupt for port %d, but apparently not enabled; per-user %p\n",
115 port, u);
117 disable_irq_nosync(irq);
118 set_port_enabled(port, false);
120 if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
121 u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port;
122 wmb(); /* Ensure ring contents visible */
123 if (u->ring_cons == u->ring_prod++) {
124 wake_up_interruptible(&u->evtchn_wait);
125 kill_fasync(&u->evtchn_async_queue,
126 SIGIO, POLL_IN);
128 } else
129 u->ring_overflow = 1;
131 spin_unlock(&port_user_lock);
133 return IRQ_HANDLED;
136 static ssize_t evtchn_read(struct file *file, char __user *buf,
137 size_t count, loff_t *ppos)
139 int rc;
140 unsigned int c, p, bytes1 = 0, bytes2 = 0;
141 struct per_user_data *u = file->private_data;
143 /* Whole number of ports. */
144 count &= ~(sizeof(evtchn_port_t)-1);
146 if (count == 0)
147 return 0;
149 if (count > PAGE_SIZE)
150 count = PAGE_SIZE;
152 for (;;) {
153 mutex_lock(&u->ring_cons_mutex);
155 rc = -EFBIG;
156 if (u->ring_overflow)
157 goto unlock_out;
159 c = u->ring_cons;
160 p = u->ring_prod;
161 if (c != p)
162 break;
164 mutex_unlock(&u->ring_cons_mutex);
166 if (file->f_flags & O_NONBLOCK)
167 return -EAGAIN;
169 rc = wait_event_interruptible(u->evtchn_wait,
170 u->ring_cons != u->ring_prod);
171 if (rc)
172 return rc;
175 /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
176 if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
177 bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) *
178 sizeof(evtchn_port_t);
179 bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t);
180 } else {
181 bytes1 = (p - c) * sizeof(evtchn_port_t);
182 bytes2 = 0;
185 /* Truncate chunks according to caller's maximum byte count. */
186 if (bytes1 > count) {
187 bytes1 = count;
188 bytes2 = 0;
189 } else if ((bytes1 + bytes2) > count) {
190 bytes2 = count - bytes1;
193 rc = -EFAULT;
194 rmb(); /* Ensure that we see the port before we copy it. */
195 if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
196 ((bytes2 != 0) &&
197 copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
198 goto unlock_out;
200 u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
201 rc = bytes1 + bytes2;
203 unlock_out:
204 mutex_unlock(&u->ring_cons_mutex);
205 return rc;
208 static ssize_t evtchn_write(struct file *file, const char __user *buf,
209 size_t count, loff_t *ppos)
211 int rc, i;
212 evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
213 struct per_user_data *u = file->private_data;
215 if (kbuf == NULL)
216 return -ENOMEM;
218 /* Whole number of ports. */
219 count &= ~(sizeof(evtchn_port_t)-1);
221 rc = 0;
222 if (count == 0)
223 goto out;
225 if (count > PAGE_SIZE)
226 count = PAGE_SIZE;
228 rc = -EFAULT;
229 if (copy_from_user(kbuf, buf, count) != 0)
230 goto out;
232 spin_lock_irq(&port_user_lock);
234 for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
235 unsigned port = kbuf[i];
237 if (port < NR_EVENT_CHANNELS &&
238 get_port_user(port) == u &&
239 !get_port_enabled(port)) {
240 set_port_enabled(port, true);
241 enable_irq(irq_from_evtchn(port));
245 spin_unlock_irq(&port_user_lock);
247 rc = count;
249 out:
250 free_page((unsigned long)kbuf);
251 return rc;
254 static int evtchn_bind_to_user(struct per_user_data *u, int port)
256 int rc = 0;
259 * Ports are never reused, so every caller should pass in a
260 * unique port.
262 * (Locking not necessary because we haven't registered the
263 * interrupt handler yet, and our caller has already
264 * serialized bind operations.)
266 BUG_ON(get_port_user(port) != NULL);
267 set_port_user(port, u);
268 set_port_enabled(port, true); /* start enabled */
270 rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
271 u->name, (void *)(unsigned long)port);
272 if (rc >= 0)
273 rc = evtchn_make_refcounted(port);
274 else {
275 /* bind failed, should close the port now */
276 struct evtchn_close close;
277 close.port = port;
278 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
279 BUG();
280 set_port_user(port, NULL);
283 return rc;
286 static void evtchn_unbind_from_user(struct per_user_data *u, int port)
288 int irq = irq_from_evtchn(port);
290 BUG_ON(irq < 0);
292 unbind_from_irqhandler(irq, (void *)(unsigned long)port);
294 set_port_user(port, NULL);
297 static long evtchn_ioctl(struct file *file,
298 unsigned int cmd, unsigned long arg)
300 int rc;
301 struct per_user_data *u = file->private_data;
302 void __user *uarg = (void __user *) arg;
304 /* Prevent bind from racing with unbind */
305 mutex_lock(&u->bind_mutex);
307 switch (cmd) {
308 case IOCTL_EVTCHN_BIND_VIRQ: {
309 struct ioctl_evtchn_bind_virq bind;
310 struct evtchn_bind_virq bind_virq;
312 rc = -EFAULT;
313 if (copy_from_user(&bind, uarg, sizeof(bind)))
314 break;
316 bind_virq.virq = bind.virq;
317 bind_virq.vcpu = 0;
318 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
319 &bind_virq);
320 if (rc != 0)
321 break;
323 rc = evtchn_bind_to_user(u, bind_virq.port);
324 if (rc == 0)
325 rc = bind_virq.port;
326 break;
329 case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
330 struct ioctl_evtchn_bind_interdomain bind;
331 struct evtchn_bind_interdomain bind_interdomain;
333 rc = -EFAULT;
334 if (copy_from_user(&bind, uarg, sizeof(bind)))
335 break;
337 bind_interdomain.remote_dom = bind.remote_domain;
338 bind_interdomain.remote_port = bind.remote_port;
339 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
340 &bind_interdomain);
341 if (rc != 0)
342 break;
344 rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
345 if (rc == 0)
346 rc = bind_interdomain.local_port;
347 break;
350 case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
351 struct ioctl_evtchn_bind_unbound_port bind;
352 struct evtchn_alloc_unbound alloc_unbound;
354 rc = -EFAULT;
355 if (copy_from_user(&bind, uarg, sizeof(bind)))
356 break;
358 alloc_unbound.dom = DOMID_SELF;
359 alloc_unbound.remote_dom = bind.remote_domain;
360 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
361 &alloc_unbound);
362 if (rc != 0)
363 break;
365 rc = evtchn_bind_to_user(u, alloc_unbound.port);
366 if (rc == 0)
367 rc = alloc_unbound.port;
368 break;
371 case IOCTL_EVTCHN_UNBIND: {
372 struct ioctl_evtchn_unbind unbind;
374 rc = -EFAULT;
375 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
376 break;
378 rc = -EINVAL;
379 if (unbind.port >= NR_EVENT_CHANNELS)
380 break;
382 spin_lock_irq(&port_user_lock);
384 rc = -ENOTCONN;
385 if (get_port_user(unbind.port) != u) {
386 spin_unlock_irq(&port_user_lock);
387 break;
390 disable_irq(irq_from_evtchn(unbind.port));
392 spin_unlock_irq(&port_user_lock);
394 evtchn_unbind_from_user(u, unbind.port);
396 rc = 0;
397 break;
400 case IOCTL_EVTCHN_NOTIFY: {
401 struct ioctl_evtchn_notify notify;
403 rc = -EFAULT;
404 if (copy_from_user(&notify, uarg, sizeof(notify)))
405 break;
407 if (notify.port >= NR_EVENT_CHANNELS) {
408 rc = -EINVAL;
409 } else if (get_port_user(notify.port) != u) {
410 rc = -ENOTCONN;
411 } else {
412 notify_remote_via_evtchn(notify.port);
413 rc = 0;
415 break;
418 case IOCTL_EVTCHN_RESET: {
419 /* Initialise the ring to empty. Clear errors. */
420 mutex_lock(&u->ring_cons_mutex);
421 spin_lock_irq(&port_user_lock);
422 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
423 spin_unlock_irq(&port_user_lock);
424 mutex_unlock(&u->ring_cons_mutex);
425 rc = 0;
426 break;
429 default:
430 rc = -ENOSYS;
431 break;
433 mutex_unlock(&u->bind_mutex);
435 return rc;
438 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
440 unsigned int mask = POLLOUT | POLLWRNORM;
441 struct per_user_data *u = file->private_data;
443 poll_wait(file, &u->evtchn_wait, wait);
444 if (u->ring_cons != u->ring_prod)
445 mask |= POLLIN | POLLRDNORM;
446 if (u->ring_overflow)
447 mask = POLLERR;
448 return mask;
451 static int evtchn_fasync(int fd, struct file *filp, int on)
453 struct per_user_data *u = filp->private_data;
454 return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
457 static int evtchn_open(struct inode *inode, struct file *filp)
459 struct per_user_data *u;
461 u = kzalloc(sizeof(*u), GFP_KERNEL);
462 if (u == NULL)
463 return -ENOMEM;
465 u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
466 if (u->name == NULL) {
467 kfree(u);
468 return -ENOMEM;
471 init_waitqueue_head(&u->evtchn_wait);
473 u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
474 if (u->ring == NULL) {
475 kfree(u->name);
476 kfree(u);
477 return -ENOMEM;
480 mutex_init(&u->bind_mutex);
481 mutex_init(&u->ring_cons_mutex);
483 filp->private_data = u;
485 return nonseekable_open(inode, filp);
488 static int evtchn_release(struct inode *inode, struct file *filp)
490 int i;
491 struct per_user_data *u = filp->private_data;
493 spin_lock_irq(&port_user_lock);
495 free_page((unsigned long)u->ring);
497 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
498 if (get_port_user(i) != u)
499 continue;
501 disable_irq(irq_from_evtchn(i));
504 spin_unlock_irq(&port_user_lock);
506 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
507 if (get_port_user(i) != u)
508 continue;
510 evtchn_unbind_from_user(get_port_user(i), i);
513 kfree(u->name);
514 kfree(u);
516 return 0;
519 static const struct file_operations evtchn_fops = {
520 .owner = THIS_MODULE,
521 .read = evtchn_read,
522 .write = evtchn_write,
523 .unlocked_ioctl = evtchn_ioctl,
524 .poll = evtchn_poll,
525 .fasync = evtchn_fasync,
526 .open = evtchn_open,
527 .release = evtchn_release,
528 .llseek = no_llseek,
531 static struct miscdevice evtchn_miscdev = {
532 .minor = MISC_DYNAMIC_MINOR,
533 .name = "xen/evtchn",
534 .fops = &evtchn_fops,
536 static int __init evtchn_init(void)
538 int err;
540 if (!xen_domain())
541 return -ENODEV;
543 port_user = kcalloc(NR_EVENT_CHANNELS, sizeof(*port_user), GFP_KERNEL);
544 if (port_user == NULL)
545 return -ENOMEM;
547 spin_lock_init(&port_user_lock);
549 /* Create '/dev/xen/evtchn'. */
550 err = misc_register(&evtchn_miscdev);
551 if (err != 0) {
552 pr_err("Could not register /dev/xen/evtchn\n");
553 return err;
556 pr_info("Event-channel device installed\n");
558 return 0;
561 static void __exit evtchn_cleanup(void)
563 kfree(port_user);
564 port_user = NULL;
566 misc_deregister(&evtchn_miscdev);
569 module_init(evtchn_init);
570 module_exit(evtchn_cleanup);
572 MODULE_LICENSE("GPL");