xref: /openbmc/linux/drivers/xen/evtchn.c (revision 93afe0b7)
1f7116284SIan Campbell /******************************************************************************
2f7116284SIan Campbell  * evtchn.c
3f7116284SIan Campbell  *
4f7116284SIan Campbell  * Driver for receiving and demuxing event-channel signals.
5f7116284SIan Campbell  *
6f7116284SIan Campbell  * Copyright (c) 2004-2005, K A Fraser
7f7116284SIan Campbell  * Multi-process extensions Copyright (c) 2004, Steven Smith
8f7116284SIan Campbell  *
9f7116284SIan Campbell  * This program is free software; you can redistribute it and/or
10f7116284SIan Campbell  * modify it under the terms of the GNU General Public License version 2
11f7116284SIan Campbell  * as published by the Free Software Foundation; or, when distributed
12f7116284SIan Campbell  * separately from the Linux kernel or incorporated into other
13f7116284SIan Campbell  * software packages, subject to the following license:
14f7116284SIan Campbell  *
15f7116284SIan Campbell  * Permission is hereby granted, free of charge, to any person obtaining a copy
16f7116284SIan Campbell  * of this source file (the "Software"), to deal in the Software without
17f7116284SIan Campbell  * restriction, including without limitation the rights to use, copy, modify,
18f7116284SIan Campbell  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19f7116284SIan Campbell  * and to permit persons to whom the Software is furnished to do so, subject to
20f7116284SIan Campbell  * the following conditions:
21f7116284SIan Campbell  *
22f7116284SIan Campbell  * The above copyright notice and this permission notice shall be included in
23f7116284SIan Campbell  * all copies or substantial portions of the Software.
24f7116284SIan Campbell  *
25f7116284SIan Campbell  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26f7116284SIan Campbell  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27f7116284SIan Campbell  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28f7116284SIan Campbell  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29f7116284SIan Campbell  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30f7116284SIan Campbell  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31f7116284SIan Campbell  * IN THE SOFTWARE.
32f7116284SIan Campbell  */
33f7116284SIan Campbell 
34f7116284SIan Campbell #include <linux/module.h>
35f7116284SIan Campbell #include <linux/kernel.h>
36f7116284SIan Campbell #include <linux/sched.h>
37f7116284SIan Campbell #include <linux/slab.h>
38f7116284SIan Campbell #include <linux/string.h>
39f7116284SIan Campbell #include <linux/errno.h>
40f7116284SIan Campbell #include <linux/fs.h>
41f7116284SIan Campbell #include <linux/errno.h>
42f7116284SIan Campbell #include <linux/miscdevice.h>
43f7116284SIan Campbell #include <linux/major.h>
44f7116284SIan Campbell #include <linux/proc_fs.h>
45f7116284SIan Campbell #include <linux/stat.h>
46f7116284SIan Campbell #include <linux/poll.h>
47f7116284SIan Campbell #include <linux/irq.h>
48f7116284SIan Campbell #include <linux/init.h>
49f7116284SIan Campbell #include <linux/gfp.h>
50f7116284SIan Campbell #include <linux/mutex.h>
51f7116284SIan Campbell #include <linux/cpu.h>
52f7116284SIan Campbell #include <xen/events.h>
53f7116284SIan Campbell #include <xen/evtchn.h>
54f7116284SIan Campbell #include <asm/xen/hypervisor.h>
55f7116284SIan Campbell 
56f7116284SIan Campbell struct per_user_data {
570a4666b5SJeremy Fitzhardinge 	struct mutex bind_mutex; /* serialize bind/unbind operations */
580a4666b5SJeremy Fitzhardinge 
59f7116284SIan Campbell 	/* Notification ring, accessed via /dev/xen/evtchn. */
60f7116284SIan Campbell #define EVTCHN_RING_SIZE     (PAGE_SIZE / sizeof(evtchn_port_t))
61f7116284SIan Campbell #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
62f7116284SIan Campbell 	evtchn_port_t *ring;
63f7116284SIan Campbell 	unsigned int ring_cons, ring_prod, ring_overflow;
64f7116284SIan Campbell 	struct mutex ring_cons_mutex; /* protect against concurrent readers */
65f7116284SIan Campbell 
66f7116284SIan Campbell 	/* Processes wait on this queue when ring is empty. */
67f7116284SIan Campbell 	wait_queue_head_t evtchn_wait;
68f7116284SIan Campbell 	struct fasync_struct *evtchn_async_queue;
69f7116284SIan Campbell 	const char *name;
70f7116284SIan Campbell };
71f7116284SIan Campbell 
72e3cc067bSJeremy Fitzhardinge /*
73e3cc067bSJeremy Fitzhardinge  * Who's bound to each port?  This is logically an array of struct
74e3cc067bSJeremy Fitzhardinge  * per_user_data *, but we encode the current enabled-state in bit 0.
75e3cc067bSJeremy Fitzhardinge  */
7693afe0b7SJeremy Fitzhardinge static unsigned long *port_user;
770a4666b5SJeremy Fitzhardinge static DEFINE_SPINLOCK(port_user_lock); /* protects port_user[] and ring_prod */
78f7116284SIan Campbell 
79e3cc067bSJeremy Fitzhardinge static inline struct per_user_data *get_port_user(unsigned port)
80e3cc067bSJeremy Fitzhardinge {
81e3cc067bSJeremy Fitzhardinge 	return (struct per_user_data *)(port_user[port] & ~1);
82e3cc067bSJeremy Fitzhardinge }
83e3cc067bSJeremy Fitzhardinge 
84e3cc067bSJeremy Fitzhardinge static inline void set_port_user(unsigned port, struct per_user_data *u)
85e3cc067bSJeremy Fitzhardinge {
86e3cc067bSJeremy Fitzhardinge 	port_user[port] = (unsigned long)u;
87e3cc067bSJeremy Fitzhardinge }
88e3cc067bSJeremy Fitzhardinge 
89e3cc067bSJeremy Fitzhardinge static inline bool get_port_enabled(unsigned port)
90e3cc067bSJeremy Fitzhardinge {
91e3cc067bSJeremy Fitzhardinge 	return port_user[port] & 1;
92e3cc067bSJeremy Fitzhardinge }
93e3cc067bSJeremy Fitzhardinge 
94e3cc067bSJeremy Fitzhardinge static inline void set_port_enabled(unsigned port, bool enabled)
95e3cc067bSJeremy Fitzhardinge {
96e3cc067bSJeremy Fitzhardinge 	if (enabled)
97e3cc067bSJeremy Fitzhardinge 		port_user[port] |= 1;
98e3cc067bSJeremy Fitzhardinge 	else
99e3cc067bSJeremy Fitzhardinge 		port_user[port] &= ~1;
100e3cc067bSJeremy Fitzhardinge }
101e3cc067bSJeremy Fitzhardinge 
102f7116284SIan Campbell irqreturn_t evtchn_interrupt(int irq, void *data)
103f7116284SIan Campbell {
104f7116284SIan Campbell 	unsigned int port = (unsigned long)data;
105f7116284SIan Campbell 	struct per_user_data *u;
106f7116284SIan Campbell 
107f7116284SIan Campbell 	spin_lock(&port_user_lock);
108f7116284SIan Campbell 
109e3cc067bSJeremy Fitzhardinge 	u = get_port_user(port);
110e3cc067bSJeremy Fitzhardinge 
111e3cc067bSJeremy Fitzhardinge 	if (WARN(!get_port_enabled(port),
112e3cc067bSJeremy Fitzhardinge 		 "Interrupt for port %d, but apparently not enabled; per-user %p\n",
113e3cc067bSJeremy Fitzhardinge 		 port, u))
114e3cc067bSJeremy Fitzhardinge 		goto out;
115f7116284SIan Campbell 
116f7116284SIan Campbell 	disable_irq_nosync(irq);
117e3cc067bSJeremy Fitzhardinge 	set_port_enabled(port, false);
118f7116284SIan Campbell 
119f7116284SIan Campbell 	if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
120f7116284SIan Campbell 		u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port;
121f7116284SIan Campbell 		wmb(); /* Ensure ring contents visible */
122f7116284SIan Campbell 		if (u->ring_cons == u->ring_prod++) {
123f7116284SIan Campbell 			wake_up_interruptible(&u->evtchn_wait);
124f7116284SIan Campbell 			kill_fasync(&u->evtchn_async_queue,
125f7116284SIan Campbell 				    SIGIO, POLL_IN);
126f7116284SIan Campbell 		}
127e3cc067bSJeremy Fitzhardinge 	} else
128f7116284SIan Campbell 		u->ring_overflow = 1;
129f7116284SIan Campbell 
130e3cc067bSJeremy Fitzhardinge out:
131f7116284SIan Campbell 	spin_unlock(&port_user_lock);
132f7116284SIan Campbell 
133f7116284SIan Campbell 	return IRQ_HANDLED;
134f7116284SIan Campbell }
135f7116284SIan Campbell 
136f7116284SIan Campbell static ssize_t evtchn_read(struct file *file, char __user *buf,
137f7116284SIan Campbell 			   size_t count, loff_t *ppos)
138f7116284SIan Campbell {
139f7116284SIan Campbell 	int rc;
140f7116284SIan Campbell 	unsigned int c, p, bytes1 = 0, bytes2 = 0;
141f7116284SIan Campbell 	struct per_user_data *u = file->private_data;
142f7116284SIan Campbell 
143f7116284SIan Campbell 	/* Whole number of ports. */
144f7116284SIan Campbell 	count &= ~(sizeof(evtchn_port_t)-1);
145f7116284SIan Campbell 
146f7116284SIan Campbell 	if (count == 0)
147f7116284SIan Campbell 		return 0;
148f7116284SIan Campbell 
149f7116284SIan Campbell 	if (count > PAGE_SIZE)
150f7116284SIan Campbell 		count = PAGE_SIZE;
151f7116284SIan Campbell 
152f7116284SIan Campbell 	for (;;) {
153f7116284SIan Campbell 		mutex_lock(&u->ring_cons_mutex);
154f7116284SIan Campbell 
155f7116284SIan Campbell 		rc = -EFBIG;
156f7116284SIan Campbell 		if (u->ring_overflow)
157f7116284SIan Campbell 			goto unlock_out;
158f7116284SIan Campbell 
159f7116284SIan Campbell 		c = u->ring_cons;
160f7116284SIan Campbell 		p = u->ring_prod;
161f7116284SIan Campbell 		if (c != p)
162f7116284SIan Campbell 			break;
163f7116284SIan Campbell 
164f7116284SIan Campbell 		mutex_unlock(&u->ring_cons_mutex);
165f7116284SIan Campbell 
166f7116284SIan Campbell 		if (file->f_flags & O_NONBLOCK)
167f7116284SIan Campbell 			return -EAGAIN;
168f7116284SIan Campbell 
169f7116284SIan Campbell 		rc = wait_event_interruptible(u->evtchn_wait,
170f7116284SIan Campbell 					      u->ring_cons != u->ring_prod);
171f7116284SIan Campbell 		if (rc)
172f7116284SIan Campbell 			return rc;
173f7116284SIan Campbell 	}
174f7116284SIan Campbell 
175f7116284SIan Campbell 	/* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
176f7116284SIan Campbell 	if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
177f7116284SIan Campbell 		bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) *
178f7116284SIan Campbell 			sizeof(evtchn_port_t);
179f7116284SIan Campbell 		bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t);
180f7116284SIan Campbell 	} else {
181f7116284SIan Campbell 		bytes1 = (p - c) * sizeof(evtchn_port_t);
182f7116284SIan Campbell 		bytes2 = 0;
183f7116284SIan Campbell 	}
184f7116284SIan Campbell 
185f7116284SIan Campbell 	/* Truncate chunks according to caller's maximum byte count. */
186f7116284SIan Campbell 	if (bytes1 > count) {
187f7116284SIan Campbell 		bytes1 = count;
188f7116284SIan Campbell 		bytes2 = 0;
189f7116284SIan Campbell 	} else if ((bytes1 + bytes2) > count) {
190f7116284SIan Campbell 		bytes2 = count - bytes1;
191f7116284SIan Campbell 	}
192f7116284SIan Campbell 
193f7116284SIan Campbell 	rc = -EFAULT;
194f7116284SIan Campbell 	rmb(); /* Ensure that we see the port before we copy it. */
195f7116284SIan Campbell 	if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
196f7116284SIan Campbell 	    ((bytes2 != 0) &&
197f7116284SIan Campbell 	     copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
198f7116284SIan Campbell 		goto unlock_out;
199f7116284SIan Campbell 
200f7116284SIan Campbell 	u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
201f7116284SIan Campbell 	rc = bytes1 + bytes2;
202f7116284SIan Campbell 
203f7116284SIan Campbell  unlock_out:
204f7116284SIan Campbell 	mutex_unlock(&u->ring_cons_mutex);
205f7116284SIan Campbell 	return rc;
206f7116284SIan Campbell }
207f7116284SIan Campbell 
208f7116284SIan Campbell static ssize_t evtchn_write(struct file *file, const char __user *buf,
209f7116284SIan Campbell 			    size_t count, loff_t *ppos)
210f7116284SIan Campbell {
211f7116284SIan Campbell 	int rc, i;
212f7116284SIan Campbell 	evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
213f7116284SIan Campbell 	struct per_user_data *u = file->private_data;
214f7116284SIan Campbell 
215f7116284SIan Campbell 	if (kbuf == NULL)
216f7116284SIan Campbell 		return -ENOMEM;
217f7116284SIan Campbell 
218f7116284SIan Campbell 	/* Whole number of ports. */
219f7116284SIan Campbell 	count &= ~(sizeof(evtchn_port_t)-1);
220f7116284SIan Campbell 
221f7116284SIan Campbell 	rc = 0;
222f7116284SIan Campbell 	if (count == 0)
223f7116284SIan Campbell 		goto out;
224f7116284SIan Campbell 
225f7116284SIan Campbell 	if (count > PAGE_SIZE)
226f7116284SIan Campbell 		count = PAGE_SIZE;
227f7116284SIan Campbell 
228f7116284SIan Campbell 	rc = -EFAULT;
229f7116284SIan Campbell 	if (copy_from_user(kbuf, buf, count) != 0)
230f7116284SIan Campbell 		goto out;
231f7116284SIan Campbell 
232f7116284SIan Campbell 	spin_lock_irq(&port_user_lock);
233e3cc067bSJeremy Fitzhardinge 
234e3cc067bSJeremy Fitzhardinge 	for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
235e3cc067bSJeremy Fitzhardinge 		unsigned port = kbuf[i];
236e3cc067bSJeremy Fitzhardinge 
237e3cc067bSJeremy Fitzhardinge 		if (port < NR_EVENT_CHANNELS &&
238e3cc067bSJeremy Fitzhardinge 		    get_port_user(port) == u &&
239e3cc067bSJeremy Fitzhardinge 		    !get_port_enabled(port)) {
240e3cc067bSJeremy Fitzhardinge 			set_port_enabled(port, true);
241e3cc067bSJeremy Fitzhardinge 			enable_irq(irq_from_evtchn(port));
242e3cc067bSJeremy Fitzhardinge 		}
243e3cc067bSJeremy Fitzhardinge 	}
244e3cc067bSJeremy Fitzhardinge 
245f7116284SIan Campbell 	spin_unlock_irq(&port_user_lock);
246f7116284SIan Campbell 
247f7116284SIan Campbell 	rc = count;
248f7116284SIan Campbell 
249f7116284SIan Campbell  out:
250f7116284SIan Campbell 	free_page((unsigned long)kbuf);
251f7116284SIan Campbell 	return rc;
252f7116284SIan Campbell }
253f7116284SIan Campbell 
254f7116284SIan Campbell static int evtchn_bind_to_user(struct per_user_data *u, int port)
255f7116284SIan Campbell {
256f7116284SIan Campbell 	int rc = 0;
257f7116284SIan Campbell 
2580a4666b5SJeremy Fitzhardinge 	/*
2590a4666b5SJeremy Fitzhardinge 	 * Ports are never reused, so every caller should pass in a
2600a4666b5SJeremy Fitzhardinge 	 * unique port.
2610a4666b5SJeremy Fitzhardinge 	 *
2620a4666b5SJeremy Fitzhardinge 	 * (Locking not necessary because we haven't registered the
2630a4666b5SJeremy Fitzhardinge 	 * interrupt handler yet, and our caller has already
2640a4666b5SJeremy Fitzhardinge 	 * serialized bind operations.)
2650a4666b5SJeremy Fitzhardinge 	 */
266e3cc067bSJeremy Fitzhardinge 	BUG_ON(get_port_user(port) != NULL);
267e3cc067bSJeremy Fitzhardinge 	set_port_user(port, u);
268f7116284SIan Campbell 
2690a4666b5SJeremy Fitzhardinge 	rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
2700a4666b5SJeremy Fitzhardinge 				       u->name, (void *)(unsigned long)port);
2710a4666b5SJeremy Fitzhardinge 	if (rc >= 0)
2720a4666b5SJeremy Fitzhardinge 		rc = 0;
2730a4666b5SJeremy Fitzhardinge 
274f7116284SIan Campbell 	return rc;
275f7116284SIan Campbell }
276f7116284SIan Campbell 
277f7116284SIan Campbell static void evtchn_unbind_from_user(struct per_user_data *u, int port)
278f7116284SIan Campbell {
279f7116284SIan Campbell 	int irq = irq_from_evtchn(port);
280f7116284SIan Campbell 
281f7116284SIan Campbell 	unbind_from_irqhandler(irq, (void *)(unsigned long)port);
2820a4666b5SJeremy Fitzhardinge 
2830a4666b5SJeremy Fitzhardinge 	/* make sure we unbind the irq handler before clearing the port */
2840a4666b5SJeremy Fitzhardinge 	barrier();
2850a4666b5SJeremy Fitzhardinge 
286e3cc067bSJeremy Fitzhardinge 	set_port_user(port, NULL);
287f7116284SIan Campbell }
288f7116284SIan Campbell 
289f7116284SIan Campbell static long evtchn_ioctl(struct file *file,
290f7116284SIan Campbell 			 unsigned int cmd, unsigned long arg)
291f7116284SIan Campbell {
292f7116284SIan Campbell 	int rc;
293f7116284SIan Campbell 	struct per_user_data *u = file->private_data;
294f7116284SIan Campbell 	void __user *uarg = (void __user *) arg;
295f7116284SIan Campbell 
2960a4666b5SJeremy Fitzhardinge 	/* Prevent bind from racing with unbind */
2970a4666b5SJeremy Fitzhardinge 	mutex_lock(&u->bind_mutex);
2980a4666b5SJeremy Fitzhardinge 
299f7116284SIan Campbell 	switch (cmd) {
300f7116284SIan Campbell 	case IOCTL_EVTCHN_BIND_VIRQ: {
301f7116284SIan Campbell 		struct ioctl_evtchn_bind_virq bind;
302f7116284SIan Campbell 		struct evtchn_bind_virq bind_virq;
303f7116284SIan Campbell 
304f7116284SIan Campbell 		rc = -EFAULT;
305f7116284SIan Campbell 		if (copy_from_user(&bind, uarg, sizeof(bind)))
306f7116284SIan Campbell 			break;
307f7116284SIan Campbell 
308f7116284SIan Campbell 		bind_virq.virq = bind.virq;
309f7116284SIan Campbell 		bind_virq.vcpu = 0;
310f7116284SIan Campbell 		rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
311f7116284SIan Campbell 						 &bind_virq);
312f7116284SIan Campbell 		if (rc != 0)
313f7116284SIan Campbell 			break;
314f7116284SIan Campbell 
315f7116284SIan Campbell 		rc = evtchn_bind_to_user(u, bind_virq.port);
316f7116284SIan Campbell 		if (rc == 0)
317f7116284SIan Campbell 			rc = bind_virq.port;
318f7116284SIan Campbell 		break;
319f7116284SIan Campbell 	}
320f7116284SIan Campbell 
321f7116284SIan Campbell 	case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
322f7116284SIan Campbell 		struct ioctl_evtchn_bind_interdomain bind;
323f7116284SIan Campbell 		struct evtchn_bind_interdomain bind_interdomain;
324f7116284SIan Campbell 
325f7116284SIan Campbell 		rc = -EFAULT;
326f7116284SIan Campbell 		if (copy_from_user(&bind, uarg, sizeof(bind)))
327f7116284SIan Campbell 			break;
328f7116284SIan Campbell 
329f7116284SIan Campbell 		bind_interdomain.remote_dom  = bind.remote_domain;
330f7116284SIan Campbell 		bind_interdomain.remote_port = bind.remote_port;
331f7116284SIan Campbell 		rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
332f7116284SIan Campbell 						 &bind_interdomain);
333f7116284SIan Campbell 		if (rc != 0)
334f7116284SIan Campbell 			break;
335f7116284SIan Campbell 
336f7116284SIan Campbell 		rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
337f7116284SIan Campbell 		if (rc == 0)
338f7116284SIan Campbell 			rc = bind_interdomain.local_port;
339f7116284SIan Campbell 		break;
340f7116284SIan Campbell 	}
341f7116284SIan Campbell 
342f7116284SIan Campbell 	case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
343f7116284SIan Campbell 		struct ioctl_evtchn_bind_unbound_port bind;
344f7116284SIan Campbell 		struct evtchn_alloc_unbound alloc_unbound;
345f7116284SIan Campbell 
346f7116284SIan Campbell 		rc = -EFAULT;
347f7116284SIan Campbell 		if (copy_from_user(&bind, uarg, sizeof(bind)))
348f7116284SIan Campbell 			break;
349f7116284SIan Campbell 
350f7116284SIan Campbell 		alloc_unbound.dom        = DOMID_SELF;
351f7116284SIan Campbell 		alloc_unbound.remote_dom = bind.remote_domain;
352f7116284SIan Campbell 		rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
353f7116284SIan Campbell 						 &alloc_unbound);
354f7116284SIan Campbell 		if (rc != 0)
355f7116284SIan Campbell 			break;
356f7116284SIan Campbell 
357f7116284SIan Campbell 		rc = evtchn_bind_to_user(u, alloc_unbound.port);
358f7116284SIan Campbell 		if (rc == 0)
359f7116284SIan Campbell 			rc = alloc_unbound.port;
360f7116284SIan Campbell 		break;
361f7116284SIan Campbell 	}
362f7116284SIan Campbell 
363f7116284SIan Campbell 	case IOCTL_EVTCHN_UNBIND: {
364f7116284SIan Campbell 		struct ioctl_evtchn_unbind unbind;
365f7116284SIan Campbell 
366f7116284SIan Campbell 		rc = -EFAULT;
367f7116284SIan Campbell 		if (copy_from_user(&unbind, uarg, sizeof(unbind)))
368f7116284SIan Campbell 			break;
369f7116284SIan Campbell 
370f7116284SIan Campbell 		rc = -EINVAL;
371f7116284SIan Campbell 		if (unbind.port >= NR_EVENT_CHANNELS)
372f7116284SIan Campbell 			break;
373f7116284SIan Campbell 
374f7116284SIan Campbell 		spin_lock_irq(&port_user_lock);
375f7116284SIan Campbell 
376f7116284SIan Campbell 		rc = -ENOTCONN;
377e3cc067bSJeremy Fitzhardinge 		if (get_port_user(unbind.port) != u) {
378f7116284SIan Campbell 			spin_unlock_irq(&port_user_lock);
379f7116284SIan Campbell 			break;
380f7116284SIan Campbell 		}
381f7116284SIan Campbell 
382f7116284SIan Campbell 		evtchn_unbind_from_user(u, unbind.port);
383f7116284SIan Campbell 
384f7116284SIan Campbell 		spin_unlock_irq(&port_user_lock);
385f7116284SIan Campbell 
386f7116284SIan Campbell 		rc = 0;
387f7116284SIan Campbell 		break;
388f7116284SIan Campbell 	}
389f7116284SIan Campbell 
390f7116284SIan Campbell 	case IOCTL_EVTCHN_NOTIFY: {
391f7116284SIan Campbell 		struct ioctl_evtchn_notify notify;
392f7116284SIan Campbell 
393f7116284SIan Campbell 		rc = -EFAULT;
394f7116284SIan Campbell 		if (copy_from_user(&notify, uarg, sizeof(notify)))
395f7116284SIan Campbell 			break;
396f7116284SIan Campbell 
397f7116284SIan Campbell 		if (notify.port >= NR_EVENT_CHANNELS) {
398f7116284SIan Campbell 			rc = -EINVAL;
399e3cc067bSJeremy Fitzhardinge 		} else if (get_port_user(notify.port) != u) {
400f7116284SIan Campbell 			rc = -ENOTCONN;
401f7116284SIan Campbell 		} else {
402f7116284SIan Campbell 			notify_remote_via_evtchn(notify.port);
403f7116284SIan Campbell 			rc = 0;
404f7116284SIan Campbell 		}
405f7116284SIan Campbell 		break;
406f7116284SIan Campbell 	}
407f7116284SIan Campbell 
408f7116284SIan Campbell 	case IOCTL_EVTCHN_RESET: {
409f7116284SIan Campbell 		/* Initialise the ring to empty. Clear errors. */
410f7116284SIan Campbell 		mutex_lock(&u->ring_cons_mutex);
411f7116284SIan Campbell 		spin_lock_irq(&port_user_lock);
412f7116284SIan Campbell 		u->ring_cons = u->ring_prod = u->ring_overflow = 0;
413f7116284SIan Campbell 		spin_unlock_irq(&port_user_lock);
414f7116284SIan Campbell 		mutex_unlock(&u->ring_cons_mutex);
415f7116284SIan Campbell 		rc = 0;
416f7116284SIan Campbell 		break;
417f7116284SIan Campbell 	}
418f7116284SIan Campbell 
419f7116284SIan Campbell 	default:
420f7116284SIan Campbell 		rc = -ENOSYS;
421f7116284SIan Campbell 		break;
422f7116284SIan Campbell 	}
4230a4666b5SJeremy Fitzhardinge 	mutex_unlock(&u->bind_mutex);
424f7116284SIan Campbell 
425f7116284SIan Campbell 	return rc;
426f7116284SIan Campbell }
427f7116284SIan Campbell 
428f7116284SIan Campbell static unsigned int evtchn_poll(struct file *file, poll_table *wait)
429f7116284SIan Campbell {
430f7116284SIan Campbell 	unsigned int mask = POLLOUT | POLLWRNORM;
431f7116284SIan Campbell 	struct per_user_data *u = file->private_data;
432f7116284SIan Campbell 
433f7116284SIan Campbell 	poll_wait(file, &u->evtchn_wait, wait);
434f7116284SIan Campbell 	if (u->ring_cons != u->ring_prod)
435f7116284SIan Campbell 		mask |= POLLIN | POLLRDNORM;
436f7116284SIan Campbell 	if (u->ring_overflow)
437f7116284SIan Campbell 		mask = POLLERR;
438f7116284SIan Campbell 	return mask;
439f7116284SIan Campbell }
440f7116284SIan Campbell 
441f7116284SIan Campbell static int evtchn_fasync(int fd, struct file *filp, int on)
442f7116284SIan Campbell {
443f7116284SIan Campbell 	struct per_user_data *u = filp->private_data;
444f7116284SIan Campbell 	return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
445f7116284SIan Campbell }
446f7116284SIan Campbell 
447f7116284SIan Campbell static int evtchn_open(struct inode *inode, struct file *filp)
448f7116284SIan Campbell {
449f7116284SIan Campbell 	struct per_user_data *u;
450f7116284SIan Campbell 
451f7116284SIan Campbell 	u = kzalloc(sizeof(*u), GFP_KERNEL);
452f7116284SIan Campbell 	if (u == NULL)
453f7116284SIan Campbell 		return -ENOMEM;
454f7116284SIan Campbell 
455f7116284SIan Campbell 	u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
456f7116284SIan Campbell 	if (u->name == NULL) {
457f7116284SIan Campbell 		kfree(u);
458f7116284SIan Campbell 		return -ENOMEM;
459f7116284SIan Campbell 	}
460f7116284SIan Campbell 
461f7116284SIan Campbell 	init_waitqueue_head(&u->evtchn_wait);
462f7116284SIan Campbell 
463f7116284SIan Campbell 	u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
464f7116284SIan Campbell 	if (u->ring == NULL) {
465f7116284SIan Campbell 		kfree(u->name);
466f7116284SIan Campbell 		kfree(u);
467f7116284SIan Campbell 		return -ENOMEM;
468f7116284SIan Campbell 	}
469f7116284SIan Campbell 
4700a4666b5SJeremy Fitzhardinge 	mutex_init(&u->bind_mutex);
471f7116284SIan Campbell 	mutex_init(&u->ring_cons_mutex);
472f7116284SIan Campbell 
473f7116284SIan Campbell 	filp->private_data = u;
474f7116284SIan Campbell 
475f7116284SIan Campbell 	return 0;
476f7116284SIan Campbell }
477f7116284SIan Campbell 
478f7116284SIan Campbell static int evtchn_release(struct inode *inode, struct file *filp)
479f7116284SIan Campbell {
480f7116284SIan Campbell 	int i;
481f7116284SIan Campbell 	struct per_user_data *u = filp->private_data;
482f7116284SIan Campbell 
483f7116284SIan Campbell 	spin_lock_irq(&port_user_lock);
484f7116284SIan Campbell 
485f7116284SIan Campbell 	free_page((unsigned long)u->ring);
486f7116284SIan Campbell 
487f7116284SIan Campbell 	for (i = 0; i < NR_EVENT_CHANNELS; i++) {
488e3cc067bSJeremy Fitzhardinge 		if (get_port_user(i) != u)
489f7116284SIan Campbell 			continue;
490f7116284SIan Campbell 
491e3cc067bSJeremy Fitzhardinge 		evtchn_unbind_from_user(get_port_user(i), i);
492f7116284SIan Campbell 	}
493f7116284SIan Campbell 
494f7116284SIan Campbell 	spin_unlock_irq(&port_user_lock);
495f7116284SIan Campbell 
496f7116284SIan Campbell 	kfree(u->name);
497f7116284SIan Campbell 	kfree(u);
498f7116284SIan Campbell 
499f7116284SIan Campbell 	return 0;
500f7116284SIan Campbell }
501f7116284SIan Campbell 
502f7116284SIan Campbell static const struct file_operations evtchn_fops = {
503f7116284SIan Campbell 	.owner   = THIS_MODULE,
504f7116284SIan Campbell 	.read    = evtchn_read,
505f7116284SIan Campbell 	.write   = evtchn_write,
506f7116284SIan Campbell 	.unlocked_ioctl = evtchn_ioctl,
507f7116284SIan Campbell 	.poll    = evtchn_poll,
508f7116284SIan Campbell 	.fasync  = evtchn_fasync,
509f7116284SIan Campbell 	.open    = evtchn_open,
510f7116284SIan Campbell 	.release = evtchn_release,
511f7116284SIan Campbell };
512f7116284SIan Campbell 
513f7116284SIan Campbell static struct miscdevice evtchn_miscdev = {
514f7116284SIan Campbell 	.minor        = MISC_DYNAMIC_MINOR,
515f7116284SIan Campbell 	.name         = "evtchn",
516f7116284SIan Campbell 	.fops         = &evtchn_fops,
517f7116284SIan Campbell };
518f7116284SIan Campbell static int __init evtchn_init(void)
519f7116284SIan Campbell {
520f7116284SIan Campbell 	int err;
521f7116284SIan Campbell 
522f7116284SIan Campbell 	if (!xen_domain())
523f7116284SIan Campbell 		return -ENODEV;
524f7116284SIan Campbell 
52593afe0b7SJeremy Fitzhardinge 	port_user = kcalloc(NR_EVENT_CHANNELS, sizeof(*port_user), GFP_KERNEL);
52693afe0b7SJeremy Fitzhardinge 	if (port_user == NULL)
52793afe0b7SJeremy Fitzhardinge 		return -ENOMEM;
52893afe0b7SJeremy Fitzhardinge 
529f7116284SIan Campbell 	spin_lock_init(&port_user_lock);
530f7116284SIan Campbell 
531f7116284SIan Campbell 	/* Create '/dev/misc/evtchn'. */
532f7116284SIan Campbell 	err = misc_register(&evtchn_miscdev);
533f7116284SIan Campbell 	if (err != 0) {
534f7116284SIan Campbell 		printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
535f7116284SIan Campbell 		return err;
536f7116284SIan Campbell 	}
537f7116284SIan Campbell 
538f7116284SIan Campbell 	printk(KERN_INFO "Event-channel device installed.\n");
539f7116284SIan Campbell 
540f7116284SIan Campbell 	return 0;
541f7116284SIan Campbell }
542f7116284SIan Campbell 
543f7116284SIan Campbell static void __exit evtchn_cleanup(void)
544f7116284SIan Campbell {
54593afe0b7SJeremy Fitzhardinge 	kfree(port_user);
54693afe0b7SJeremy Fitzhardinge 	port_user = NULL;
54793afe0b7SJeremy Fitzhardinge 
548f7116284SIan Campbell 	misc_deregister(&evtchn_miscdev);
549f7116284SIan Campbell }
550f7116284SIan Campbell 
551f7116284SIan Campbell module_init(evtchn_init);
552f7116284SIan Campbell module_exit(evtchn_cleanup);
553f7116284SIan Campbell 
554f7116284SIan Campbell MODULE_LICENSE("GPL");
555