xref: /openbmc/linux/drivers/xen/evtchn.c (revision 27e0e6385377c4dc68a4ddaf1a35a2dfa951f3c5)
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 
34283c0972SJoe Perches #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
35283c0972SJoe Perches 
36f7116284SIan Campbell #include <linux/module.h>
37f7116284SIan Campbell #include <linux/kernel.h>
38f7116284SIan Campbell #include <linux/sched.h>
39f7116284SIan Campbell #include <linux/slab.h>
40f7116284SIan Campbell #include <linux/string.h>
41f7116284SIan Campbell #include <linux/errno.h>
42f7116284SIan Campbell #include <linux/fs.h>
43f7116284SIan Campbell #include <linux/miscdevice.h>
44f7116284SIan Campbell #include <linux/major.h>
45f7116284SIan Campbell #include <linux/proc_fs.h>
46f7116284SIan Campbell #include <linux/stat.h>
47f7116284SIan Campbell #include <linux/poll.h>
48f7116284SIan Campbell #include <linux/irq.h>
49f7116284SIan Campbell #include <linux/init.h>
50f7116284SIan Campbell #include <linux/mutex.h>
51f7116284SIan Campbell #include <linux/cpu.h>
5286200154SDavid Vrabel #include <linux/mm.h>
5386200154SDavid Vrabel #include <linux/vmalloc.h>
541ccbf534SJeremy Fitzhardinge 
551ccbf534SJeremy Fitzhardinge #include <xen/xen.h>
56f7116284SIan Campbell #include <xen/events.h>
57f7116284SIan Campbell #include <xen/evtchn.h>
58f7116284SIan Campbell #include <asm/xen/hypervisor.h>
59f7116284SIan Campbell 
60f7116284SIan Campbell struct per_user_data {
610a4666b5SJeremy Fitzhardinge 	struct mutex bind_mutex; /* serialize bind/unbind operations */
6273cc4bb0SDavid Vrabel 	struct rb_root evtchns;
6386200154SDavid Vrabel 	unsigned int nr_evtchns;
640a4666b5SJeremy Fitzhardinge 
65f7116284SIan Campbell 	/* Notification ring, accessed via /dev/xen/evtchn. */
6686200154SDavid Vrabel 	unsigned int ring_size;
67f7116284SIan Campbell 	evtchn_port_t *ring;
68f7116284SIan Campbell 	unsigned int ring_cons, ring_prod, ring_overflow;
69f7116284SIan Campbell 	struct mutex ring_cons_mutex; /* protect against concurrent readers */
7073cc4bb0SDavid Vrabel 	spinlock_t ring_prod_lock; /* product against concurrent interrupts */
71f7116284SIan Campbell 
72f7116284SIan Campbell 	/* Processes wait on this queue when ring is empty. */
73f7116284SIan Campbell 	wait_queue_head_t evtchn_wait;
74f7116284SIan Campbell 	struct fasync_struct *evtchn_async_queue;
75f7116284SIan Campbell 	const char *name;
76f7116284SIan Campbell };
77f7116284SIan Campbell 
7873cc4bb0SDavid Vrabel struct user_evtchn {
7973cc4bb0SDavid Vrabel 	struct rb_node node;
8073cc4bb0SDavid Vrabel 	struct per_user_data *user;
8173cc4bb0SDavid Vrabel 	unsigned port;
8273cc4bb0SDavid Vrabel 	bool enabled;
8373cc4bb0SDavid Vrabel };
84f7116284SIan Campbell 
8586200154SDavid Vrabel static evtchn_port_t *evtchn_alloc_ring(unsigned int size)
8686200154SDavid Vrabel {
8786200154SDavid Vrabel 	evtchn_port_t *ring;
8886200154SDavid Vrabel 	size_t s = size * sizeof(*ring);
8986200154SDavid Vrabel 
9086200154SDavid Vrabel 	ring = kmalloc(s, GFP_KERNEL);
9186200154SDavid Vrabel 	if (!ring)
9286200154SDavid Vrabel 		ring = vmalloc(s);
9386200154SDavid Vrabel 
9486200154SDavid Vrabel 	return ring;
9586200154SDavid Vrabel }
9686200154SDavid Vrabel 
9786200154SDavid Vrabel static void evtchn_free_ring(evtchn_port_t *ring)
9886200154SDavid Vrabel {
9986200154SDavid Vrabel 	kvfree(ring);
10086200154SDavid Vrabel }
10186200154SDavid Vrabel 
10286200154SDavid Vrabel static unsigned int evtchn_ring_offset(struct per_user_data *u,
10386200154SDavid Vrabel 				       unsigned int idx)
10486200154SDavid Vrabel {
10586200154SDavid Vrabel 	return idx & (u->ring_size - 1);
10686200154SDavid Vrabel }
10786200154SDavid Vrabel 
10886200154SDavid Vrabel static evtchn_port_t *evtchn_ring_entry(struct per_user_data *u,
10986200154SDavid Vrabel 					unsigned int idx)
11086200154SDavid Vrabel {
11186200154SDavid Vrabel 	return u->ring + evtchn_ring_offset(u, idx);
11286200154SDavid Vrabel }
11386200154SDavid Vrabel 
11473cc4bb0SDavid Vrabel static int add_evtchn(struct per_user_data *u, struct user_evtchn *evtchn)
115e3cc067bSJeremy Fitzhardinge {
11673cc4bb0SDavid Vrabel 	struct rb_node **new = &(u->evtchns.rb_node), *parent = NULL;
117e3cc067bSJeremy Fitzhardinge 
11886200154SDavid Vrabel 	u->nr_evtchns++;
11986200154SDavid Vrabel 
12073cc4bb0SDavid Vrabel 	while (*new) {
12173cc4bb0SDavid Vrabel 		struct user_evtchn *this;
122e3cc067bSJeremy Fitzhardinge 
12373cc4bb0SDavid Vrabel 		this = container_of(*new, struct user_evtchn, node);
124e3cc067bSJeremy Fitzhardinge 
12573cc4bb0SDavid Vrabel 		parent = *new;
12673cc4bb0SDavid Vrabel 		if (this->port < evtchn->port)
12773cc4bb0SDavid Vrabel 			new = &((*new)->rb_left);
12873cc4bb0SDavid Vrabel 		else if (this->port > evtchn->port)
12973cc4bb0SDavid Vrabel 			new = &((*new)->rb_right);
130e3cc067bSJeremy Fitzhardinge 		else
13173cc4bb0SDavid Vrabel 			return -EEXIST;
13273cc4bb0SDavid Vrabel 	}
13373cc4bb0SDavid Vrabel 
13473cc4bb0SDavid Vrabel 	/* Add new node and rebalance tree. */
13573cc4bb0SDavid Vrabel 	rb_link_node(&evtchn->node, parent, new);
13673cc4bb0SDavid Vrabel 	rb_insert_color(&evtchn->node, &u->evtchns);
13773cc4bb0SDavid Vrabel 
13873cc4bb0SDavid Vrabel 	return 0;
13973cc4bb0SDavid Vrabel }
14073cc4bb0SDavid Vrabel 
14173cc4bb0SDavid Vrabel static void del_evtchn(struct per_user_data *u, struct user_evtchn *evtchn)
14273cc4bb0SDavid Vrabel {
14386200154SDavid Vrabel 	u->nr_evtchns--;
14473cc4bb0SDavid Vrabel 	rb_erase(&evtchn->node, &u->evtchns);
14573cc4bb0SDavid Vrabel 	kfree(evtchn);
14673cc4bb0SDavid Vrabel }
14773cc4bb0SDavid Vrabel 
14873cc4bb0SDavid Vrabel static struct user_evtchn *find_evtchn(struct per_user_data *u, unsigned port)
14973cc4bb0SDavid Vrabel {
15073cc4bb0SDavid Vrabel 	struct rb_node *node = u->evtchns.rb_node;
15173cc4bb0SDavid Vrabel 
15273cc4bb0SDavid Vrabel 	while (node) {
15373cc4bb0SDavid Vrabel 		struct user_evtchn *evtchn;
15473cc4bb0SDavid Vrabel 
15573cc4bb0SDavid Vrabel 		evtchn = container_of(node, struct user_evtchn, node);
15673cc4bb0SDavid Vrabel 
15773cc4bb0SDavid Vrabel 		if (evtchn->port < port)
15873cc4bb0SDavid Vrabel 			node = node->rb_left;
15973cc4bb0SDavid Vrabel 		else if (evtchn->port > port)
16073cc4bb0SDavid Vrabel 			node = node->rb_right;
16173cc4bb0SDavid Vrabel 		else
16273cc4bb0SDavid Vrabel 			return evtchn;
16373cc4bb0SDavid Vrabel 	}
16473cc4bb0SDavid Vrabel 	return NULL;
165e3cc067bSJeremy Fitzhardinge }
166e3cc067bSJeremy Fitzhardinge 
16770697d54SJeremy Fitzhardinge static irqreturn_t evtchn_interrupt(int irq, void *data)
168f7116284SIan Campbell {
16973cc4bb0SDavid Vrabel 	struct user_evtchn *evtchn = data;
17073cc4bb0SDavid Vrabel 	struct per_user_data *u = evtchn->user;
171f7116284SIan Campbell 
17273cc4bb0SDavid Vrabel 	WARN(!evtchn->enabled,
173e3cc067bSJeremy Fitzhardinge 	     "Interrupt for port %d, but apparently not enabled; per-user %p\n",
17473cc4bb0SDavid Vrabel 	     evtchn->port, u);
175f7116284SIan Campbell 
176f7116284SIan Campbell 	disable_irq_nosync(irq);
17773cc4bb0SDavid Vrabel 	evtchn->enabled = false;
17873cc4bb0SDavid Vrabel 
17973cc4bb0SDavid Vrabel 	spin_lock(&u->ring_prod_lock);
180f7116284SIan Campbell 
18186200154SDavid Vrabel 	if ((u->ring_prod - u->ring_cons) < u->ring_size) {
18286200154SDavid Vrabel 		*evtchn_ring_entry(u, u->ring_prod) = evtchn->port;
183f7116284SIan Campbell 		wmb(); /* Ensure ring contents visible */
184f7116284SIan Campbell 		if (u->ring_cons == u->ring_prod++) {
185f7116284SIan Campbell 			wake_up_interruptible(&u->evtchn_wait);
186f7116284SIan Campbell 			kill_fasync(&u->evtchn_async_queue,
187f7116284SIan Campbell 				    SIGIO, POLL_IN);
188f7116284SIan Campbell 		}
189e3cc067bSJeremy Fitzhardinge 	} else
190f7116284SIan Campbell 		u->ring_overflow = 1;
191f7116284SIan Campbell 
19273cc4bb0SDavid Vrabel 	spin_unlock(&u->ring_prod_lock);
193f7116284SIan Campbell 
194f7116284SIan Campbell 	return IRQ_HANDLED;
195f7116284SIan Campbell }
196f7116284SIan Campbell 
197f7116284SIan Campbell static ssize_t evtchn_read(struct file *file, char __user *buf,
198f7116284SIan Campbell 			   size_t count, loff_t *ppos)
199f7116284SIan Campbell {
200f7116284SIan Campbell 	int rc;
201f7116284SIan Campbell 	unsigned int c, p, bytes1 = 0, bytes2 = 0;
202f7116284SIan Campbell 	struct per_user_data *u = file->private_data;
203f7116284SIan Campbell 
204f7116284SIan Campbell 	/* Whole number of ports. */
205f7116284SIan Campbell 	count &= ~(sizeof(evtchn_port_t)-1);
206f7116284SIan Campbell 
207f7116284SIan Campbell 	if (count == 0)
208f7116284SIan Campbell 		return 0;
209f7116284SIan Campbell 
210f7116284SIan Campbell 	if (count > PAGE_SIZE)
211f7116284SIan Campbell 		count = PAGE_SIZE;
212f7116284SIan Campbell 
213f7116284SIan Campbell 	for (;;) {
214f7116284SIan Campbell 		mutex_lock(&u->ring_cons_mutex);
215f7116284SIan Campbell 
216f7116284SIan Campbell 		rc = -EFBIG;
217f7116284SIan Campbell 		if (u->ring_overflow)
218f7116284SIan Campbell 			goto unlock_out;
219f7116284SIan Campbell 
220f7116284SIan Campbell 		c = u->ring_cons;
221f7116284SIan Campbell 		p = u->ring_prod;
222f7116284SIan Campbell 		if (c != p)
223f7116284SIan Campbell 			break;
224f7116284SIan Campbell 
225f7116284SIan Campbell 		mutex_unlock(&u->ring_cons_mutex);
226f7116284SIan Campbell 
227f7116284SIan Campbell 		if (file->f_flags & O_NONBLOCK)
228f7116284SIan Campbell 			return -EAGAIN;
229f7116284SIan Campbell 
230f7116284SIan Campbell 		rc = wait_event_interruptible(u->evtchn_wait,
231f7116284SIan Campbell 					      u->ring_cons != u->ring_prod);
232f7116284SIan Campbell 		if (rc)
233f7116284SIan Campbell 			return rc;
234f7116284SIan Campbell 	}
235f7116284SIan Campbell 
236f7116284SIan Campbell 	/* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
23786200154SDavid Vrabel 	if (((c ^ p) & u->ring_size) != 0) {
23886200154SDavid Vrabel 		bytes1 = (u->ring_size - evtchn_ring_offset(u, c)) *
239f7116284SIan Campbell 			sizeof(evtchn_port_t);
24086200154SDavid Vrabel 		bytes2 = evtchn_ring_offset(u, p) * sizeof(evtchn_port_t);
241f7116284SIan Campbell 	} else {
242f7116284SIan Campbell 		bytes1 = (p - c) * sizeof(evtchn_port_t);
243f7116284SIan Campbell 		bytes2 = 0;
244f7116284SIan Campbell 	}
245f7116284SIan Campbell 
246f7116284SIan Campbell 	/* Truncate chunks according to caller's maximum byte count. */
247f7116284SIan Campbell 	if (bytes1 > count) {
248f7116284SIan Campbell 		bytes1 = count;
249f7116284SIan Campbell 		bytes2 = 0;
250f7116284SIan Campbell 	} else if ((bytes1 + bytes2) > count) {
251f7116284SIan Campbell 		bytes2 = count - bytes1;
252f7116284SIan Campbell 	}
253f7116284SIan Campbell 
254f7116284SIan Campbell 	rc = -EFAULT;
255f7116284SIan Campbell 	rmb(); /* Ensure that we see the port before we copy it. */
25686200154SDavid Vrabel 	if (copy_to_user(buf, evtchn_ring_entry(u, c), bytes1) ||
257f7116284SIan Campbell 	    ((bytes2 != 0) &&
258f7116284SIan Campbell 	     copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
259f7116284SIan Campbell 		goto unlock_out;
260f7116284SIan Campbell 
261f7116284SIan Campbell 	u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
262f7116284SIan Campbell 	rc = bytes1 + bytes2;
263f7116284SIan Campbell 
264f7116284SIan Campbell  unlock_out:
265f7116284SIan Campbell 	mutex_unlock(&u->ring_cons_mutex);
266f7116284SIan Campbell 	return rc;
267f7116284SIan Campbell }
268f7116284SIan Campbell 
269f7116284SIan Campbell static ssize_t evtchn_write(struct file *file, const char __user *buf,
270f7116284SIan Campbell 			    size_t count, loff_t *ppos)
271f7116284SIan Campbell {
272f7116284SIan Campbell 	int rc, i;
273f7116284SIan Campbell 	evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
274f7116284SIan Campbell 	struct per_user_data *u = file->private_data;
275f7116284SIan Campbell 
276f7116284SIan Campbell 	if (kbuf == NULL)
277f7116284SIan Campbell 		return -ENOMEM;
278f7116284SIan Campbell 
279f7116284SIan Campbell 	/* Whole number of ports. */
280f7116284SIan Campbell 	count &= ~(sizeof(evtchn_port_t)-1);
281f7116284SIan Campbell 
282f7116284SIan Campbell 	rc = 0;
283f7116284SIan Campbell 	if (count == 0)
284f7116284SIan Campbell 		goto out;
285f7116284SIan Campbell 
286f7116284SIan Campbell 	if (count > PAGE_SIZE)
287f7116284SIan Campbell 		count = PAGE_SIZE;
288f7116284SIan Campbell 
289f7116284SIan Campbell 	rc = -EFAULT;
290f7116284SIan Campbell 	if (copy_from_user(kbuf, buf, count) != 0)
291f7116284SIan Campbell 		goto out;
292f7116284SIan Campbell 
29373cc4bb0SDavid Vrabel 	mutex_lock(&u->bind_mutex);
294e3cc067bSJeremy Fitzhardinge 
295e3cc067bSJeremy Fitzhardinge 	for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
296e3cc067bSJeremy Fitzhardinge 		unsigned port = kbuf[i];
29773cc4bb0SDavid Vrabel 		struct user_evtchn *evtchn;
298e3cc067bSJeremy Fitzhardinge 
29973cc4bb0SDavid Vrabel 		evtchn = find_evtchn(u, port);
30073cc4bb0SDavid Vrabel 		if (evtchn && !evtchn->enabled) {
30173cc4bb0SDavid Vrabel 			evtchn->enabled = true;
302e3cc067bSJeremy Fitzhardinge 			enable_irq(irq_from_evtchn(port));
303e3cc067bSJeremy Fitzhardinge 		}
304e3cc067bSJeremy Fitzhardinge 	}
305e3cc067bSJeremy Fitzhardinge 
30673cc4bb0SDavid Vrabel 	mutex_unlock(&u->bind_mutex);
307f7116284SIan Campbell 
308f7116284SIan Campbell 	rc = count;
309f7116284SIan Campbell 
310f7116284SIan Campbell  out:
311f7116284SIan Campbell 	free_page((unsigned long)kbuf);
312f7116284SIan Campbell 	return rc;
313f7116284SIan Campbell }
314f7116284SIan Campbell 
31586200154SDavid Vrabel static int evtchn_resize_ring(struct per_user_data *u)
31686200154SDavid Vrabel {
31786200154SDavid Vrabel 	unsigned int new_size;
31886200154SDavid Vrabel 	evtchn_port_t *new_ring, *old_ring;
31986200154SDavid Vrabel 
32086200154SDavid Vrabel 	/*
32186200154SDavid Vrabel 	 * Ensure the ring is large enough to capture all possible
32286200154SDavid Vrabel 	 * events. i.e., one free slot for each bound event.
32386200154SDavid Vrabel 	 */
32486200154SDavid Vrabel 	if (u->nr_evtchns <= u->ring_size)
32586200154SDavid Vrabel 		return 0;
32686200154SDavid Vrabel 
32786200154SDavid Vrabel 	if (u->ring_size == 0)
32886200154SDavid Vrabel 		new_size = 64;
32986200154SDavid Vrabel 	else
33086200154SDavid Vrabel 		new_size = 2 * u->ring_size;
33186200154SDavid Vrabel 
33286200154SDavid Vrabel 	new_ring = evtchn_alloc_ring(new_size);
33386200154SDavid Vrabel 	if (!new_ring)
33486200154SDavid Vrabel 		return -ENOMEM;
33586200154SDavid Vrabel 
33686200154SDavid Vrabel 	old_ring = u->ring;
33786200154SDavid Vrabel 
33886200154SDavid Vrabel 	/*
33986200154SDavid Vrabel 	 * Access to the ring contents is serialized by either the
34086200154SDavid Vrabel 	 * prod /or/ cons lock so take both when resizing.
34186200154SDavid Vrabel 	 */
34286200154SDavid Vrabel 	mutex_lock(&u->ring_cons_mutex);
34386200154SDavid Vrabel 	spin_lock_irq(&u->ring_prod_lock);
34486200154SDavid Vrabel 
34586200154SDavid Vrabel 	/*
34686200154SDavid Vrabel 	 * Copy the old ring contents to the new ring.
34786200154SDavid Vrabel 	 *
348*27e0e638SJan Beulich 	 * To take care of wrapping, a full ring, and the new index
349*27e0e638SJan Beulich 	 * pointing into the second half, simply copy the old contents
350*27e0e638SJan Beulich 	 * twice.
35186200154SDavid Vrabel 	 *
35286200154SDavid Vrabel 	 * +---------+    +------------------+
353*27e0e638SJan Beulich 	 * |34567  12| -> |34567  1234567  12|
354*27e0e638SJan Beulich 	 * +-----p-c-+    +-------c------p---+
35586200154SDavid Vrabel 	 */
356*27e0e638SJan Beulich 	memcpy(new_ring, old_ring, u->ring_size * sizeof(*u->ring));
357*27e0e638SJan Beulich 	memcpy(new_ring + u->ring_size, old_ring,
358*27e0e638SJan Beulich 	       u->ring_size * sizeof(*u->ring));
35986200154SDavid Vrabel 
36086200154SDavid Vrabel 	u->ring = new_ring;
36186200154SDavid Vrabel 	u->ring_size = new_size;
36286200154SDavid Vrabel 
36386200154SDavid Vrabel 	spin_unlock_irq(&u->ring_prod_lock);
36486200154SDavid Vrabel 	mutex_unlock(&u->ring_cons_mutex);
36586200154SDavid Vrabel 
36686200154SDavid Vrabel 	evtchn_free_ring(old_ring);
36786200154SDavid Vrabel 
36886200154SDavid Vrabel 	return 0;
36986200154SDavid Vrabel }
37086200154SDavid Vrabel 
371f7116284SIan Campbell static int evtchn_bind_to_user(struct per_user_data *u, int port)
372f7116284SIan Campbell {
37373cc4bb0SDavid Vrabel 	struct user_evtchn *evtchn;
37473cc4bb0SDavid Vrabel 	struct evtchn_close close;
375f7116284SIan Campbell 	int rc = 0;
376f7116284SIan Campbell 
3770a4666b5SJeremy Fitzhardinge 	/*
3780a4666b5SJeremy Fitzhardinge 	 * Ports are never reused, so every caller should pass in a
3790a4666b5SJeremy Fitzhardinge 	 * unique port.
3800a4666b5SJeremy Fitzhardinge 	 *
3810a4666b5SJeremy Fitzhardinge 	 * (Locking not necessary because we haven't registered the
3820a4666b5SJeremy Fitzhardinge 	 * interrupt handler yet, and our caller has already
3830a4666b5SJeremy Fitzhardinge 	 * serialized bind operations.)
3840a4666b5SJeremy Fitzhardinge 	 */
38573cc4bb0SDavid Vrabel 
38673cc4bb0SDavid Vrabel 	evtchn = kzalloc(sizeof(*evtchn), GFP_KERNEL);
38773cc4bb0SDavid Vrabel 	if (!evtchn)
38873cc4bb0SDavid Vrabel 		return -ENOMEM;
38973cc4bb0SDavid Vrabel 
39073cc4bb0SDavid Vrabel 	evtchn->user = u;
39173cc4bb0SDavid Vrabel 	evtchn->port = port;
39273cc4bb0SDavid Vrabel 	evtchn->enabled = true; /* start enabled */
39373cc4bb0SDavid Vrabel 
39473cc4bb0SDavid Vrabel 	rc = add_evtchn(u, evtchn);
39573cc4bb0SDavid Vrabel 	if (rc < 0)
39673cc4bb0SDavid Vrabel 		goto err;
397f7116284SIan Campbell 
39886200154SDavid Vrabel 	rc = evtchn_resize_ring(u);
39986200154SDavid Vrabel 	if (rc < 0)
40086200154SDavid Vrabel 		goto err;
40186200154SDavid Vrabel 
402af09d1a7SMichael Opdenacker 	rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, 0,
40373cc4bb0SDavid Vrabel 				       u->name, evtchn);
40473cc4bb0SDavid Vrabel 	if (rc < 0)
40573cc4bb0SDavid Vrabel 		goto err;
40673cc4bb0SDavid Vrabel 
407420eb554SDaniel De Graaf 	rc = evtchn_make_refcounted(port);
40873cc4bb0SDavid Vrabel 	return rc;
40973cc4bb0SDavid Vrabel 
41073cc4bb0SDavid Vrabel err:
411e7e44e44SWei Liu 	/* bind failed, should close the port now */
412e7e44e44SWei Liu 	close.port = port;
413e7e44e44SWei Liu 	if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
414e7e44e44SWei Liu 		BUG();
41573cc4bb0SDavid Vrabel 	del_evtchn(u, evtchn);
416f7116284SIan Campbell 	return rc;
417f7116284SIan Campbell }
418f7116284SIan Campbell 
41973cc4bb0SDavid Vrabel static void evtchn_unbind_from_user(struct per_user_data *u,
42073cc4bb0SDavid Vrabel 				    struct user_evtchn *evtchn)
421f7116284SIan Campbell {
42273cc4bb0SDavid Vrabel 	int irq = irq_from_evtchn(evtchn->port);
423f7116284SIan Campbell 
424e7e44e44SWei Liu 	BUG_ON(irq < 0);
425e7e44e44SWei Liu 
42673cc4bb0SDavid Vrabel 	unbind_from_irqhandler(irq, evtchn);
4270a4666b5SJeremy Fitzhardinge 
42873cc4bb0SDavid Vrabel 	del_evtchn(u, evtchn);
429f7116284SIan Campbell }
430f7116284SIan Campbell 
431f7116284SIan Campbell static long evtchn_ioctl(struct file *file,
432f7116284SIan Campbell 			 unsigned int cmd, unsigned long arg)
433f7116284SIan Campbell {
434f7116284SIan Campbell 	int rc;
435f7116284SIan Campbell 	struct per_user_data *u = file->private_data;
436f7116284SIan Campbell 	void __user *uarg = (void __user *) arg;
437f7116284SIan Campbell 
4380a4666b5SJeremy Fitzhardinge 	/* Prevent bind from racing with unbind */
4390a4666b5SJeremy Fitzhardinge 	mutex_lock(&u->bind_mutex);
4400a4666b5SJeremy Fitzhardinge 
441f7116284SIan Campbell 	switch (cmd) {
442f7116284SIan Campbell 	case IOCTL_EVTCHN_BIND_VIRQ: {
443f7116284SIan Campbell 		struct ioctl_evtchn_bind_virq bind;
444f7116284SIan Campbell 		struct evtchn_bind_virq bind_virq;
445f7116284SIan Campbell 
446f7116284SIan Campbell 		rc = -EFAULT;
447f7116284SIan Campbell 		if (copy_from_user(&bind, uarg, sizeof(bind)))
448f7116284SIan Campbell 			break;
449f7116284SIan Campbell 
450f7116284SIan Campbell 		bind_virq.virq = bind.virq;
451f7116284SIan Campbell 		bind_virq.vcpu = 0;
452f7116284SIan Campbell 		rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
453f7116284SIan Campbell 						 &bind_virq);
454f7116284SIan Campbell 		if (rc != 0)
455f7116284SIan Campbell 			break;
456f7116284SIan Campbell 
457f7116284SIan Campbell 		rc = evtchn_bind_to_user(u, bind_virq.port);
458f7116284SIan Campbell 		if (rc == 0)
459f7116284SIan Campbell 			rc = bind_virq.port;
460f7116284SIan Campbell 		break;
461f7116284SIan Campbell 	}
462f7116284SIan Campbell 
463f7116284SIan Campbell 	case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
464f7116284SIan Campbell 		struct ioctl_evtchn_bind_interdomain bind;
465f7116284SIan Campbell 		struct evtchn_bind_interdomain bind_interdomain;
466f7116284SIan Campbell 
467f7116284SIan Campbell 		rc = -EFAULT;
468f7116284SIan Campbell 		if (copy_from_user(&bind, uarg, sizeof(bind)))
469f7116284SIan Campbell 			break;
470f7116284SIan Campbell 
471f7116284SIan Campbell 		bind_interdomain.remote_dom  = bind.remote_domain;
472f7116284SIan Campbell 		bind_interdomain.remote_port = bind.remote_port;
473f7116284SIan Campbell 		rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
474f7116284SIan Campbell 						 &bind_interdomain);
475f7116284SIan Campbell 		if (rc != 0)
476f7116284SIan Campbell 			break;
477f7116284SIan Campbell 
478f7116284SIan Campbell 		rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
479f7116284SIan Campbell 		if (rc == 0)
480f7116284SIan Campbell 			rc = bind_interdomain.local_port;
481f7116284SIan Campbell 		break;
482f7116284SIan Campbell 	}
483f7116284SIan Campbell 
484f7116284SIan Campbell 	case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
485f7116284SIan Campbell 		struct ioctl_evtchn_bind_unbound_port bind;
486f7116284SIan Campbell 		struct evtchn_alloc_unbound alloc_unbound;
487f7116284SIan Campbell 
488f7116284SIan Campbell 		rc = -EFAULT;
489f7116284SIan Campbell 		if (copy_from_user(&bind, uarg, sizeof(bind)))
490f7116284SIan Campbell 			break;
491f7116284SIan Campbell 
492f7116284SIan Campbell 		alloc_unbound.dom        = DOMID_SELF;
493f7116284SIan Campbell 		alloc_unbound.remote_dom = bind.remote_domain;
494f7116284SIan Campbell 		rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
495f7116284SIan Campbell 						 &alloc_unbound);
496f7116284SIan Campbell 		if (rc != 0)
497f7116284SIan Campbell 			break;
498f7116284SIan Campbell 
499f7116284SIan Campbell 		rc = evtchn_bind_to_user(u, alloc_unbound.port);
500f7116284SIan Campbell 		if (rc == 0)
501f7116284SIan Campbell 			rc = alloc_unbound.port;
502f7116284SIan Campbell 		break;
503f7116284SIan Campbell 	}
504f7116284SIan Campbell 
505f7116284SIan Campbell 	case IOCTL_EVTCHN_UNBIND: {
506f7116284SIan Campbell 		struct ioctl_evtchn_unbind unbind;
50773cc4bb0SDavid Vrabel 		struct user_evtchn *evtchn;
508f7116284SIan Campbell 
509f7116284SIan Campbell 		rc = -EFAULT;
510f7116284SIan Campbell 		if (copy_from_user(&unbind, uarg, sizeof(unbind)))
511f7116284SIan Campbell 			break;
512f7116284SIan Campbell 
513f7116284SIan Campbell 		rc = -EINVAL;
5140dc0064aSDavid Vrabel 		if (unbind.port >= xen_evtchn_nr_channels())
515f7116284SIan Campbell 			break;
516f7116284SIan Campbell 
517f7116284SIan Campbell 		rc = -ENOTCONN;
51873cc4bb0SDavid Vrabel 		evtchn = find_evtchn(u, unbind.port);
51973cc4bb0SDavid Vrabel 		if (!evtchn)
520f7116284SIan Campbell 			break;
521f7116284SIan Campbell 
5223f5e554fSJeremy Fitzhardinge 		disable_irq(irq_from_evtchn(unbind.port));
52373cc4bb0SDavid Vrabel 		evtchn_unbind_from_user(u, evtchn);
524f7116284SIan Campbell 		rc = 0;
525f7116284SIan Campbell 		break;
526f7116284SIan Campbell 	}
527f7116284SIan Campbell 
528f7116284SIan Campbell 	case IOCTL_EVTCHN_NOTIFY: {
529f7116284SIan Campbell 		struct ioctl_evtchn_notify notify;
53073cc4bb0SDavid Vrabel 		struct user_evtchn *evtchn;
531f7116284SIan Campbell 
532f7116284SIan Campbell 		rc = -EFAULT;
533f7116284SIan Campbell 		if (copy_from_user(&notify, uarg, sizeof(notify)))
534f7116284SIan Campbell 			break;
535f7116284SIan Campbell 
536f7116284SIan Campbell 		rc = -ENOTCONN;
53773cc4bb0SDavid Vrabel 		evtchn = find_evtchn(u, notify.port);
53873cc4bb0SDavid Vrabel 		if (evtchn) {
539f7116284SIan Campbell 			notify_remote_via_evtchn(notify.port);
540f7116284SIan Campbell 			rc = 0;
541f7116284SIan Campbell 		}
542f7116284SIan Campbell 		break;
543f7116284SIan Campbell 	}
544f7116284SIan Campbell 
545f7116284SIan Campbell 	case IOCTL_EVTCHN_RESET: {
546f7116284SIan Campbell 		/* Initialise the ring to empty. Clear errors. */
547f7116284SIan Campbell 		mutex_lock(&u->ring_cons_mutex);
54873cc4bb0SDavid Vrabel 		spin_lock_irq(&u->ring_prod_lock);
549f7116284SIan Campbell 		u->ring_cons = u->ring_prod = u->ring_overflow = 0;
55073cc4bb0SDavid Vrabel 		spin_unlock_irq(&u->ring_prod_lock);
551f7116284SIan Campbell 		mutex_unlock(&u->ring_cons_mutex);
552f7116284SIan Campbell 		rc = 0;
553f7116284SIan Campbell 		break;
554f7116284SIan Campbell 	}
555f7116284SIan Campbell 
556f7116284SIan Campbell 	default:
557f7116284SIan Campbell 		rc = -ENOSYS;
558f7116284SIan Campbell 		break;
559f7116284SIan Campbell 	}
5600a4666b5SJeremy Fitzhardinge 	mutex_unlock(&u->bind_mutex);
561f7116284SIan Campbell 
562f7116284SIan Campbell 	return rc;
563f7116284SIan Campbell }
564f7116284SIan Campbell 
565f7116284SIan Campbell static unsigned int evtchn_poll(struct file *file, poll_table *wait)
566f7116284SIan Campbell {
567f7116284SIan Campbell 	unsigned int mask = POLLOUT | POLLWRNORM;
568f7116284SIan Campbell 	struct per_user_data *u = file->private_data;
569f7116284SIan Campbell 
570f7116284SIan Campbell 	poll_wait(file, &u->evtchn_wait, wait);
571f7116284SIan Campbell 	if (u->ring_cons != u->ring_prod)
572f7116284SIan Campbell 		mask |= POLLIN | POLLRDNORM;
573f7116284SIan Campbell 	if (u->ring_overflow)
574f7116284SIan Campbell 		mask = POLLERR;
575f7116284SIan Campbell 	return mask;
576f7116284SIan Campbell }
577f7116284SIan Campbell 
578f7116284SIan Campbell static int evtchn_fasync(int fd, struct file *filp, int on)
579f7116284SIan Campbell {
580f7116284SIan Campbell 	struct per_user_data *u = filp->private_data;
581f7116284SIan Campbell 	return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
582f7116284SIan Campbell }
583f7116284SIan Campbell 
584f7116284SIan Campbell static int evtchn_open(struct inode *inode, struct file *filp)
585f7116284SIan Campbell {
586f7116284SIan Campbell 	struct per_user_data *u;
587f7116284SIan Campbell 
588f7116284SIan Campbell 	u = kzalloc(sizeof(*u), GFP_KERNEL);
589f7116284SIan Campbell 	if (u == NULL)
590f7116284SIan Campbell 		return -ENOMEM;
591f7116284SIan Campbell 
592f7116284SIan Campbell 	u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
593f7116284SIan Campbell 	if (u->name == NULL) {
594f7116284SIan Campbell 		kfree(u);
595f7116284SIan Campbell 		return -ENOMEM;
596f7116284SIan Campbell 	}
597f7116284SIan Campbell 
598f7116284SIan Campbell 	init_waitqueue_head(&u->evtchn_wait);
599f7116284SIan Campbell 
6000a4666b5SJeremy Fitzhardinge 	mutex_init(&u->bind_mutex);
601f7116284SIan Campbell 	mutex_init(&u->ring_cons_mutex);
60273cc4bb0SDavid Vrabel 	spin_lock_init(&u->ring_prod_lock);
603f7116284SIan Campbell 
604f7116284SIan Campbell 	filp->private_data = u;
605f7116284SIan Campbell 
6066eab04a8SJustin P. Mattock 	return nonseekable_open(inode, filp);
607f7116284SIan Campbell }
608f7116284SIan Campbell 
609f7116284SIan Campbell static int evtchn_release(struct inode *inode, struct file *filp)
610f7116284SIan Campbell {
611f7116284SIan Campbell 	struct per_user_data *u = filp->private_data;
61273cc4bb0SDavid Vrabel 	struct rb_node *node;
613f7116284SIan Campbell 
61473cc4bb0SDavid Vrabel 	while ((node = u->evtchns.rb_node)) {
61573cc4bb0SDavid Vrabel 		struct user_evtchn *evtchn;
616f7116284SIan Campbell 
61773cc4bb0SDavid Vrabel 		evtchn = rb_entry(node, struct user_evtchn, node);
61873cc4bb0SDavid Vrabel 		disable_irq(irq_from_evtchn(evtchn->port));
61973cc4bb0SDavid Vrabel 		evtchn_unbind_from_user(u, evtchn);
6203f5e554fSJeremy Fitzhardinge 	}
6213f5e554fSJeremy Fitzhardinge 
62286200154SDavid Vrabel 	evtchn_free_ring(u->ring);
623f7116284SIan Campbell 	kfree(u->name);
624f7116284SIan Campbell 	kfree(u);
625f7116284SIan Campbell 
626f7116284SIan Campbell 	return 0;
627f7116284SIan Campbell }
628f7116284SIan Campbell 
629f7116284SIan Campbell static const struct file_operations evtchn_fops = {
630f7116284SIan Campbell 	.owner   = THIS_MODULE,
631f7116284SIan Campbell 	.read    = evtchn_read,
632f7116284SIan Campbell 	.write   = evtchn_write,
633f7116284SIan Campbell 	.unlocked_ioctl = evtchn_ioctl,
634f7116284SIan Campbell 	.poll    = evtchn_poll,
635f7116284SIan Campbell 	.fasync  = evtchn_fasync,
636f7116284SIan Campbell 	.open    = evtchn_open,
637f7116284SIan Campbell 	.release = evtchn_release,
638bc7fc5e3SJeremy Fitzhardinge 	.llseek	 = no_llseek,
639f7116284SIan Campbell };
640f7116284SIan Campbell 
641f7116284SIan Campbell static struct miscdevice evtchn_miscdev = {
642f7116284SIan Campbell 	.minor        = MISC_DYNAMIC_MINOR,
643376d908fSBastian Blank 	.name         = "xen/evtchn",
644f7116284SIan Campbell 	.fops         = &evtchn_fops,
645f7116284SIan Campbell };
646f7116284SIan Campbell static int __init evtchn_init(void)
647f7116284SIan Campbell {
648f7116284SIan Campbell 	int err;
649f7116284SIan Campbell 
650f7116284SIan Campbell 	if (!xen_domain())
651f7116284SIan Campbell 		return -ENODEV;
652f7116284SIan Campbell 
65318283ea7SWei Liu 	/* Create '/dev/xen/evtchn'. */
654f7116284SIan Campbell 	err = misc_register(&evtchn_miscdev);
655f7116284SIan Campbell 	if (err != 0) {
656283c0972SJoe Perches 		pr_err("Could not register /dev/xen/evtchn\n");
657f7116284SIan Campbell 		return err;
658f7116284SIan Campbell 	}
659f7116284SIan Campbell 
660283c0972SJoe Perches 	pr_info("Event-channel device installed\n");
661f7116284SIan Campbell 
662f7116284SIan Campbell 	return 0;
663f7116284SIan Campbell }
664f7116284SIan Campbell 
665f7116284SIan Campbell static void __exit evtchn_cleanup(void)
666f7116284SIan Campbell {
667f7116284SIan Campbell 	misc_deregister(&evtchn_miscdev);
668f7116284SIan Campbell }
669f7116284SIan Campbell 
670f7116284SIan Campbell module_init(evtchn_init);
671f7116284SIan Campbell module_exit(evtchn_cleanup);
672f7116284SIan Campbell 
673f7116284SIan Campbell MODULE_LICENSE("GPL");
674