]> rtime.felk.cvut.cz Git - linux-imx.git/blob - drivers/xen/evtchn.c
virtio-scsi: Fix virtqueue affinity setup
[linux-imx.git] / drivers / xen / evtchn.c
1 /******************************************************************************
2  * evtchn.c
3  *
4  * Driver for receiving and demuxing event-channel signals.
5  *
6  * Copyright (c) 2004-2005, K A Fraser
7  * Multi-process extensions Copyright (c) 2004, Steven Smith
8  *
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:
14  *
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:
21  *
22  * The above copyright notice and this permission notice shall be included in
23  * all copies or substantial portions of the Software.
24  *
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.
32  */
33
34 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
35
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>
52
53 #include <xen/xen.h>
54 #include <xen/events.h>
55 #include <xen/evtchn.h>
56 #include <asm/xen/hypervisor.h>
57
58 struct per_user_data {
59         struct mutex bind_mutex; /* serialize bind/unbind operations */
60
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 */
67
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;
72 };
73
74 /*
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.
77  */
78 static unsigned long *port_user;
79 static DEFINE_SPINLOCK(port_user_lock); /* protects port_user[] and ring_prod */
80
81 static inline struct per_user_data *get_port_user(unsigned port)
82 {
83         return (struct per_user_data *)(port_user[port] & ~1);
84 }
85
86 static inline void set_port_user(unsigned port, struct per_user_data *u)
87 {
88         port_user[port] = (unsigned long)u;
89 }
90
91 static inline bool get_port_enabled(unsigned port)
92 {
93         return port_user[port] & 1;
94 }
95
96 static inline void set_port_enabled(unsigned port, bool enabled)
97 {
98         if (enabled)
99                 port_user[port] |= 1;
100         else
101                 port_user[port] &= ~1;
102 }
103
104 static irqreturn_t evtchn_interrupt(int irq, void *data)
105 {
106         unsigned int port = (unsigned long)data;
107         struct per_user_data *u;
108
109         spin_lock(&port_user_lock);
110
111         u = get_port_user(port);
112
113         WARN(!get_port_enabled(port),
114              "Interrupt for port %d, but apparently not enabled; per-user %p\n",
115              port, u);
116
117         disable_irq_nosync(irq);
118         set_port_enabled(port, false);
119
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);
127                 }
128         } else
129                 u->ring_overflow = 1;
130
131         spin_unlock(&port_user_lock);
132
133         return IRQ_HANDLED;
134 }
135
136 static ssize_t evtchn_read(struct file *file, char __user *buf,
137                            size_t count, loff_t *ppos)
138 {
139         int rc;
140         unsigned int c, p, bytes1 = 0, bytes2 = 0;
141         struct per_user_data *u = file->private_data;
142
143         /* Whole number of ports. */
144         count &= ~(sizeof(evtchn_port_t)-1);
145
146         if (count == 0)
147                 return 0;
148
149         if (count > PAGE_SIZE)
150                 count = PAGE_SIZE;
151
152         for (;;) {
153                 mutex_lock(&u->ring_cons_mutex);
154
155                 rc = -EFBIG;
156                 if (u->ring_overflow)
157                         goto unlock_out;
158
159                 c = u->ring_cons;
160                 p = u->ring_prod;
161                 if (c != p)
162                         break;
163
164                 mutex_unlock(&u->ring_cons_mutex);
165
166                 if (file->f_flags & O_NONBLOCK)
167                         return -EAGAIN;
168
169                 rc = wait_event_interruptible(u->evtchn_wait,
170                                               u->ring_cons != u->ring_prod);
171                 if (rc)
172                         return rc;
173         }
174
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;
183         }
184
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;
191         }
192
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;
199
200         u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
201         rc = bytes1 + bytes2;
202
203  unlock_out:
204         mutex_unlock(&u->ring_cons_mutex);
205         return rc;
206 }
207
208 static ssize_t evtchn_write(struct file *file, const char __user *buf,
209                             size_t count, loff_t *ppos)
210 {
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;
214
215         if (kbuf == NULL)
216                 return -ENOMEM;
217
218         /* Whole number of ports. */
219         count &= ~(sizeof(evtchn_port_t)-1);
220
221         rc = 0;
222         if (count == 0)
223                 goto out;
224
225         if (count > PAGE_SIZE)
226                 count = PAGE_SIZE;
227
228         rc = -EFAULT;
229         if (copy_from_user(kbuf, buf, count) != 0)
230                 goto out;
231
232         spin_lock_irq(&port_user_lock);
233
234         for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
235                 unsigned port = kbuf[i];
236
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));
242                 }
243         }
244
245         spin_unlock_irq(&port_user_lock);
246
247         rc = count;
248
249  out:
250         free_page((unsigned long)kbuf);
251         return rc;
252 }
253
254 static int evtchn_bind_to_user(struct per_user_data *u, int port)
255 {
256         int rc = 0;
257
258         /*
259          * Ports are never reused, so every caller should pass in a
260          * unique port.
261          *
262          * (Locking not necessary because we haven't registered the
263          * interrupt handler yet, and our caller has already
264          * serialized bind operations.)
265          */
266         BUG_ON(get_port_user(port) != NULL);
267         set_port_user(port, u);
268         set_port_enabled(port, true); /* start enabled */
269
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);
281         }
282
283         return rc;
284 }
285
286 static void evtchn_unbind_from_user(struct per_user_data *u, int port)
287 {
288         int irq = irq_from_evtchn(port);
289
290         BUG_ON(irq < 0);
291
292         unbind_from_irqhandler(irq, (void *)(unsigned long)port);
293
294         set_port_user(port, NULL);
295 }
296
297 static long evtchn_ioctl(struct file *file,
298                          unsigned int cmd, unsigned long arg)
299 {
300         int rc;
301         struct per_user_data *u = file->private_data;
302         void __user *uarg = (void __user *) arg;
303
304         /* Prevent bind from racing with unbind */
305         mutex_lock(&u->bind_mutex);
306
307         switch (cmd) {
308         case IOCTL_EVTCHN_BIND_VIRQ: {
309                 struct ioctl_evtchn_bind_virq bind;
310                 struct evtchn_bind_virq bind_virq;
311
312                 rc = -EFAULT;
313                 if (copy_from_user(&bind, uarg, sizeof(bind)))
314                         break;
315
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;
322
323                 rc = evtchn_bind_to_user(u, bind_virq.port);
324                 if (rc == 0)
325                         rc = bind_virq.port;
326                 break;
327         }
328
329         case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
330                 struct ioctl_evtchn_bind_interdomain bind;
331                 struct evtchn_bind_interdomain bind_interdomain;
332
333                 rc = -EFAULT;
334                 if (copy_from_user(&bind, uarg, sizeof(bind)))
335                         break;
336
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;
343
344                 rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
345                 if (rc == 0)
346                         rc = bind_interdomain.local_port;
347                 break;
348         }
349
350         case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
351                 struct ioctl_evtchn_bind_unbound_port bind;
352                 struct evtchn_alloc_unbound alloc_unbound;
353
354                 rc = -EFAULT;
355                 if (copy_from_user(&bind, uarg, sizeof(bind)))
356                         break;
357
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;
364
365                 rc = evtchn_bind_to_user(u, alloc_unbound.port);
366                 if (rc == 0)
367                         rc = alloc_unbound.port;
368                 break;
369         }
370
371         case IOCTL_EVTCHN_UNBIND: {
372                 struct ioctl_evtchn_unbind unbind;
373
374                 rc = -EFAULT;
375                 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
376                         break;
377
378                 rc = -EINVAL;
379                 if (unbind.port >= NR_EVENT_CHANNELS)
380                         break;
381
382                 spin_lock_irq(&port_user_lock);
383
384                 rc = -ENOTCONN;
385                 if (get_port_user(unbind.port) != u) {
386                         spin_unlock_irq(&port_user_lock);
387                         break;
388                 }
389
390                 disable_irq(irq_from_evtchn(unbind.port));
391
392                 spin_unlock_irq(&port_user_lock);
393
394                 evtchn_unbind_from_user(u, unbind.port);
395
396                 rc = 0;
397                 break;
398         }
399
400         case IOCTL_EVTCHN_NOTIFY: {
401                 struct ioctl_evtchn_notify notify;
402
403                 rc = -EFAULT;
404                 if (copy_from_user(&notify, uarg, sizeof(notify)))
405                         break;
406
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;
414                 }
415                 break;
416         }
417
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;
427         }
428
429         default:
430                 rc = -ENOSYS;
431                 break;
432         }
433         mutex_unlock(&u->bind_mutex);
434
435         return rc;
436 }
437
438 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
439 {
440         unsigned int mask = POLLOUT | POLLWRNORM;
441         struct per_user_data *u = file->private_data;
442
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;
449 }
450
451 static int evtchn_fasync(int fd, struct file *filp, int on)
452 {
453         struct per_user_data *u = filp->private_data;
454         return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
455 }
456
457 static int evtchn_open(struct inode *inode, struct file *filp)
458 {
459         struct per_user_data *u;
460
461         u = kzalloc(sizeof(*u), GFP_KERNEL);
462         if (u == NULL)
463                 return -ENOMEM;
464
465         u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
466         if (u->name == NULL) {
467                 kfree(u);
468                 return -ENOMEM;
469         }
470
471         init_waitqueue_head(&u->evtchn_wait);
472
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;
478         }
479
480         mutex_init(&u->bind_mutex);
481         mutex_init(&u->ring_cons_mutex);
482
483         filp->private_data = u;
484
485         return nonseekable_open(inode, filp);
486 }
487
488 static int evtchn_release(struct inode *inode, struct file *filp)
489 {
490         int i;
491         struct per_user_data *u = filp->private_data;
492
493         spin_lock_irq(&port_user_lock);
494
495         free_page((unsigned long)u->ring);
496
497         for (i = 0; i < NR_EVENT_CHANNELS; i++) {
498                 if (get_port_user(i) != u)
499                         continue;
500
501                 disable_irq(irq_from_evtchn(i));
502         }
503
504         spin_unlock_irq(&port_user_lock);
505
506         for (i = 0; i < NR_EVENT_CHANNELS; i++) {
507                 if (get_port_user(i) != u)
508                         continue;
509
510                 evtchn_unbind_from_user(get_port_user(i), i);
511         }
512
513         kfree(u->name);
514         kfree(u);
515
516         return 0;
517 }
518
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,
529 };
530
531 static struct miscdevice evtchn_miscdev = {
532         .minor        = MISC_DYNAMIC_MINOR,
533         .name         = "xen/evtchn",
534         .fops         = &evtchn_fops,
535 };
536 static int __init evtchn_init(void)
537 {
538         int err;
539
540         if (!xen_domain())
541                 return -ENODEV;
542
543         port_user = kcalloc(NR_EVENT_CHANNELS, sizeof(*port_user), GFP_KERNEL);
544         if (port_user == NULL)
545                 return -ENOMEM;
546
547         spin_lock_init(&port_user_lock);
548
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;
554         }
555
556         pr_info("Event-channel device installed\n");
557
558         return 0;
559 }
560
561 static void __exit evtchn_cleanup(void)
562 {
563         kfree(port_user);
564         port_user = NULL;
565
566         misc_deregister(&evtchn_miscdev);
567 }
568
569 module_init(evtchn_init);
570 module_exit(evtchn_cleanup);
571
572 MODULE_LICENSE("GPL");