Staging: hv: rename Channel.c and .h to channel.c and .h
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / xen / evtchn.c
blob66e185cfe92fa8bb70c29d27de013fed1685c6d2
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/mutex.h>
49 #include <linux/cpu.h>
51 #include <xen/xen.h>
52 #include <xen/events.h>
53 #include <xen/evtchn.h>
54 #include <asm/xen/hypervisor.h>
56 struct per_user_data {
57 struct mutex bind_mutex; /* serialize bind/unbind operations */
59 /* Notification ring, accessed via /dev/xen/evtchn. */
60 #define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t))
61 #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
62 evtchn_port_t *ring;
63 unsigned int ring_cons, ring_prod, ring_overflow;
64 struct mutex ring_cons_mutex; /* protect against concurrent readers */
66 /* Processes wait on this queue when ring is empty. */
67 wait_queue_head_t evtchn_wait;
68 struct fasync_struct *evtchn_async_queue;
69 const char *name;
72 /* Who's bound to each port? */
73 static struct per_user_data *port_user[NR_EVENT_CHANNELS];
74 static DEFINE_SPINLOCK(port_user_lock); /* protects port_user[] and ring_prod */
76 irqreturn_t evtchn_interrupt(int irq, void *data)
78 unsigned int port = (unsigned long)data;
79 struct per_user_data *u;
81 spin_lock(&port_user_lock);
83 u = port_user[port];
85 disable_irq_nosync(irq);
87 if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
88 u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port;
89 wmb(); /* Ensure ring contents visible */
90 if (u->ring_cons == u->ring_prod++) {
91 wake_up_interruptible(&u->evtchn_wait);
92 kill_fasync(&u->evtchn_async_queue,
93 SIGIO, POLL_IN);
95 } else {
96 u->ring_overflow = 1;
99 spin_unlock(&port_user_lock);
101 return IRQ_HANDLED;
104 static ssize_t evtchn_read(struct file *file, char __user *buf,
105 size_t count, loff_t *ppos)
107 int rc;
108 unsigned int c, p, bytes1 = 0, bytes2 = 0;
109 struct per_user_data *u = file->private_data;
111 /* Whole number of ports. */
112 count &= ~(sizeof(evtchn_port_t)-1);
114 if (count == 0)
115 return 0;
117 if (count > PAGE_SIZE)
118 count = PAGE_SIZE;
120 for (;;) {
121 mutex_lock(&u->ring_cons_mutex);
123 rc = -EFBIG;
124 if (u->ring_overflow)
125 goto unlock_out;
127 c = u->ring_cons;
128 p = u->ring_prod;
129 if (c != p)
130 break;
132 mutex_unlock(&u->ring_cons_mutex);
134 if (file->f_flags & O_NONBLOCK)
135 return -EAGAIN;
137 rc = wait_event_interruptible(u->evtchn_wait,
138 u->ring_cons != u->ring_prod);
139 if (rc)
140 return rc;
143 /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
144 if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
145 bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) *
146 sizeof(evtchn_port_t);
147 bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t);
148 } else {
149 bytes1 = (p - c) * sizeof(evtchn_port_t);
150 bytes2 = 0;
153 /* Truncate chunks according to caller's maximum byte count. */
154 if (bytes1 > count) {
155 bytes1 = count;
156 bytes2 = 0;
157 } else if ((bytes1 + bytes2) > count) {
158 bytes2 = count - bytes1;
161 rc = -EFAULT;
162 rmb(); /* Ensure that we see the port before we copy it. */
163 if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
164 ((bytes2 != 0) &&
165 copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
166 goto unlock_out;
168 u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
169 rc = bytes1 + bytes2;
171 unlock_out:
172 mutex_unlock(&u->ring_cons_mutex);
173 return rc;
176 static ssize_t evtchn_write(struct file *file, const char __user *buf,
177 size_t count, loff_t *ppos)
179 int rc, i;
180 evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
181 struct per_user_data *u = file->private_data;
183 if (kbuf == NULL)
184 return -ENOMEM;
186 /* Whole number of ports. */
187 count &= ~(sizeof(evtchn_port_t)-1);
189 rc = 0;
190 if (count == 0)
191 goto out;
193 if (count > PAGE_SIZE)
194 count = PAGE_SIZE;
196 rc = -EFAULT;
197 if (copy_from_user(kbuf, buf, count) != 0)
198 goto out;
200 spin_lock_irq(&port_user_lock);
201 for (i = 0; i < (count/sizeof(evtchn_port_t)); i++)
202 if ((kbuf[i] < NR_EVENT_CHANNELS) && (port_user[kbuf[i]] == u))
203 enable_irq(irq_from_evtchn(kbuf[i]));
204 spin_unlock_irq(&port_user_lock);
206 rc = count;
208 out:
209 free_page((unsigned long)kbuf);
210 return rc;
213 static int evtchn_bind_to_user(struct per_user_data *u, int port)
215 int rc = 0;
218 * Ports are never reused, so every caller should pass in a
219 * unique port.
221 * (Locking not necessary because we haven't registered the
222 * interrupt handler yet, and our caller has already
223 * serialized bind operations.)
225 BUG_ON(port_user[port] != NULL);
226 port_user[port] = u;
228 rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
229 u->name, (void *)(unsigned long)port);
230 if (rc >= 0)
231 rc = 0;
233 return rc;
236 static void evtchn_unbind_from_user(struct per_user_data *u, int port)
238 int irq = irq_from_evtchn(port);
240 unbind_from_irqhandler(irq, (void *)(unsigned long)port);
242 /* make sure we unbind the irq handler before clearing the port */
243 barrier();
245 port_user[port] = NULL;
248 static long evtchn_ioctl(struct file *file,
249 unsigned int cmd, unsigned long arg)
251 int rc;
252 struct per_user_data *u = file->private_data;
253 void __user *uarg = (void __user *) arg;
255 /* Prevent bind from racing with unbind */
256 mutex_lock(&u->bind_mutex);
258 switch (cmd) {
259 case IOCTL_EVTCHN_BIND_VIRQ: {
260 struct ioctl_evtchn_bind_virq bind;
261 struct evtchn_bind_virq bind_virq;
263 rc = -EFAULT;
264 if (copy_from_user(&bind, uarg, sizeof(bind)))
265 break;
267 bind_virq.virq = bind.virq;
268 bind_virq.vcpu = 0;
269 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
270 &bind_virq);
271 if (rc != 0)
272 break;
274 rc = evtchn_bind_to_user(u, bind_virq.port);
275 if (rc == 0)
276 rc = bind_virq.port;
277 break;
280 case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
281 struct ioctl_evtchn_bind_interdomain bind;
282 struct evtchn_bind_interdomain bind_interdomain;
284 rc = -EFAULT;
285 if (copy_from_user(&bind, uarg, sizeof(bind)))
286 break;
288 bind_interdomain.remote_dom = bind.remote_domain;
289 bind_interdomain.remote_port = bind.remote_port;
290 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
291 &bind_interdomain);
292 if (rc != 0)
293 break;
295 rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
296 if (rc == 0)
297 rc = bind_interdomain.local_port;
298 break;
301 case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
302 struct ioctl_evtchn_bind_unbound_port bind;
303 struct evtchn_alloc_unbound alloc_unbound;
305 rc = -EFAULT;
306 if (copy_from_user(&bind, uarg, sizeof(bind)))
307 break;
309 alloc_unbound.dom = DOMID_SELF;
310 alloc_unbound.remote_dom = bind.remote_domain;
311 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
312 &alloc_unbound);
313 if (rc != 0)
314 break;
316 rc = evtchn_bind_to_user(u, alloc_unbound.port);
317 if (rc == 0)
318 rc = alloc_unbound.port;
319 break;
322 case IOCTL_EVTCHN_UNBIND: {
323 struct ioctl_evtchn_unbind unbind;
325 rc = -EFAULT;
326 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
327 break;
329 rc = -EINVAL;
330 if (unbind.port >= NR_EVENT_CHANNELS)
331 break;
333 spin_lock_irq(&port_user_lock);
335 rc = -ENOTCONN;
336 if (port_user[unbind.port] != u) {
337 spin_unlock_irq(&port_user_lock);
338 break;
341 evtchn_unbind_from_user(u, unbind.port);
343 spin_unlock_irq(&port_user_lock);
345 rc = 0;
346 break;
349 case IOCTL_EVTCHN_NOTIFY: {
350 struct ioctl_evtchn_notify notify;
352 rc = -EFAULT;
353 if (copy_from_user(&notify, uarg, sizeof(notify)))
354 break;
356 if (notify.port >= NR_EVENT_CHANNELS) {
357 rc = -EINVAL;
358 } else if (port_user[notify.port] != u) {
359 rc = -ENOTCONN;
360 } else {
361 notify_remote_via_evtchn(notify.port);
362 rc = 0;
364 break;
367 case IOCTL_EVTCHN_RESET: {
368 /* Initialise the ring to empty. Clear errors. */
369 mutex_lock(&u->ring_cons_mutex);
370 spin_lock_irq(&port_user_lock);
371 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
372 spin_unlock_irq(&port_user_lock);
373 mutex_unlock(&u->ring_cons_mutex);
374 rc = 0;
375 break;
378 default:
379 rc = -ENOSYS;
380 break;
382 mutex_unlock(&u->bind_mutex);
384 return rc;
387 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
389 unsigned int mask = POLLOUT | POLLWRNORM;
390 struct per_user_data *u = file->private_data;
392 poll_wait(file, &u->evtchn_wait, wait);
393 if (u->ring_cons != u->ring_prod)
394 mask |= POLLIN | POLLRDNORM;
395 if (u->ring_overflow)
396 mask = POLLERR;
397 return mask;
400 static int evtchn_fasync(int fd, struct file *filp, int on)
402 struct per_user_data *u = filp->private_data;
403 return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
406 static int evtchn_open(struct inode *inode, struct file *filp)
408 struct per_user_data *u;
410 u = kzalloc(sizeof(*u), GFP_KERNEL);
411 if (u == NULL)
412 return -ENOMEM;
414 u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
415 if (u->name == NULL) {
416 kfree(u);
417 return -ENOMEM;
420 init_waitqueue_head(&u->evtchn_wait);
422 u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
423 if (u->ring == NULL) {
424 kfree(u->name);
425 kfree(u);
426 return -ENOMEM;
429 mutex_init(&u->bind_mutex);
430 mutex_init(&u->ring_cons_mutex);
432 filp->private_data = u;
434 return 0;
437 static int evtchn_release(struct inode *inode, struct file *filp)
439 int i;
440 struct per_user_data *u = filp->private_data;
442 spin_lock_irq(&port_user_lock);
444 free_page((unsigned long)u->ring);
446 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
447 if (port_user[i] != u)
448 continue;
450 evtchn_unbind_from_user(port_user[i], i);
453 spin_unlock_irq(&port_user_lock);
455 kfree(u->name);
456 kfree(u);
458 return 0;
461 static const struct file_operations evtchn_fops = {
462 .owner = THIS_MODULE,
463 .read = evtchn_read,
464 .write = evtchn_write,
465 .unlocked_ioctl = evtchn_ioctl,
466 .poll = evtchn_poll,
467 .fasync = evtchn_fasync,
468 .open = evtchn_open,
469 .release = evtchn_release,
472 static struct miscdevice evtchn_miscdev = {
473 .minor = MISC_DYNAMIC_MINOR,
474 .name = "evtchn",
475 .fops = &evtchn_fops,
477 static int __init evtchn_init(void)
479 int err;
481 if (!xen_domain())
482 return -ENODEV;
484 spin_lock_init(&port_user_lock);
485 memset(port_user, 0, sizeof(port_user));
487 /* Create '/dev/misc/evtchn'. */
488 err = misc_register(&evtchn_miscdev);
489 if (err != 0) {
490 printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
491 return err;
494 printk(KERN_INFO "Event-channel device installed.\n");
496 return 0;
499 static void __exit evtchn_cleanup(void)
501 misc_deregister(&evtchn_miscdev);
504 module_init(evtchn_init);
505 module_exit(evtchn_cleanup);
507 MODULE_LICENSE("GPL");