xref: /openbmc/linux/drivers/xen/events/events_fifo.c (revision 2208f39c)
1 /*
2  * Xen event channels (FIFO-based ABI)
3  *
4  * Copyright (C) 2013 Citrix Systems R&D ltd.
5  *
6  * This source code is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * Or, when distributed separately from the Linux kernel or
12  * incorporated into other software packages, subject to the following
13  * 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/linkage.h>
37 #include <linux/interrupt.h>
38 #include <linux/irq.h>
39 #include <linux/smp.h>
40 #include <linux/percpu.h>
41 #include <linux/cpu.h>
42 
43 #include <asm/barrier.h>
44 #include <asm/sync_bitops.h>
45 #include <asm/xen/hypercall.h>
46 #include <asm/xen/hypervisor.h>
47 
48 #include <xen/xen.h>
49 #include <xen/xen-ops.h>
50 #include <xen/events.h>
51 #include <xen/interface/xen.h>
52 #include <xen/interface/event_channel.h>
53 #include <xen/page.h>
54 
55 #include "events_internal.h"
56 
57 #define EVENT_WORDS_PER_PAGE (XEN_PAGE_SIZE / sizeof(event_word_t))
58 #define MAX_EVENT_ARRAY_PAGES (EVTCHN_FIFO_NR_CHANNELS / EVENT_WORDS_PER_PAGE)
59 
60 struct evtchn_fifo_queue {
61 	uint32_t head[EVTCHN_FIFO_MAX_QUEUES];
62 };
63 
64 static DEFINE_PER_CPU(struct evtchn_fifo_control_block *, cpu_control_block);
65 static DEFINE_PER_CPU(struct evtchn_fifo_queue, cpu_queue);
66 static event_word_t *event_array[MAX_EVENT_ARRAY_PAGES] __read_mostly;
67 static unsigned event_array_pages __read_mostly;
68 
69 /*
70  * sync_set_bit() and friends must be unsigned long aligned.
71  */
72 #if BITS_PER_LONG > 32
73 
74 #define BM(w) (unsigned long *)((unsigned long)w & ~0x7UL)
75 #define EVTCHN_FIFO_BIT(b, w) \
76     (((unsigned long)w & 0x4UL) ? (EVTCHN_FIFO_ ##b + 32) : EVTCHN_FIFO_ ##b)
77 
78 #else
79 
80 #define BM(w) ((unsigned long *)(w))
81 #define EVTCHN_FIFO_BIT(b, w) EVTCHN_FIFO_ ##b
82 
83 #endif
84 
85 static inline event_word_t *event_word_from_port(evtchn_port_t port)
86 {
87 	unsigned i = port / EVENT_WORDS_PER_PAGE;
88 
89 	return event_array[i] + port % EVENT_WORDS_PER_PAGE;
90 }
91 
92 static unsigned evtchn_fifo_max_channels(void)
93 {
94 	return EVTCHN_FIFO_NR_CHANNELS;
95 }
96 
97 static unsigned evtchn_fifo_nr_channels(void)
98 {
99 	return event_array_pages * EVENT_WORDS_PER_PAGE;
100 }
101 
102 static int init_control_block(int cpu,
103                               struct evtchn_fifo_control_block *control_block)
104 {
105 	struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
106 	struct evtchn_init_control init_control;
107 	unsigned int i;
108 
109 	/* Reset the control block and the local HEADs. */
110 	clear_page(control_block);
111 	for (i = 0; i < EVTCHN_FIFO_MAX_QUEUES; i++)
112 		q->head[i] = 0;
113 
114 	init_control.control_gfn = virt_to_gfn(control_block);
115 	init_control.offset      = 0;
116 	init_control.vcpu        = xen_vcpu_nr(cpu);
117 
118 	return HYPERVISOR_event_channel_op(EVTCHNOP_init_control, &init_control);
119 }
120 
121 static void free_unused_array_pages(void)
122 {
123 	unsigned i;
124 
125 	for (i = event_array_pages; i < MAX_EVENT_ARRAY_PAGES; i++) {
126 		if (!event_array[i])
127 			break;
128 		free_page((unsigned long)event_array[i]);
129 		event_array[i] = NULL;
130 	}
131 }
132 
133 static void init_array_page(event_word_t *array_page)
134 {
135 	unsigned i;
136 
137 	for (i = 0; i < EVENT_WORDS_PER_PAGE; i++)
138 		array_page[i] = 1 << EVTCHN_FIFO_MASKED;
139 }
140 
141 static int evtchn_fifo_setup(evtchn_port_t port)
142 {
143 	unsigned new_array_pages;
144 	int ret;
145 
146 	new_array_pages = port / EVENT_WORDS_PER_PAGE + 1;
147 
148 	if (new_array_pages > MAX_EVENT_ARRAY_PAGES)
149 		return -EINVAL;
150 
151 	while (event_array_pages < new_array_pages) {
152 		void *array_page;
153 		struct evtchn_expand_array expand_array;
154 
155 		/* Might already have a page if we've resumed. */
156 		array_page = event_array[event_array_pages];
157 		if (!array_page) {
158 			array_page = (void *)__get_free_page(GFP_KERNEL);
159 			if (array_page == NULL) {
160 				ret = -ENOMEM;
161 				goto error;
162 			}
163 			event_array[event_array_pages] = array_page;
164 		}
165 
166 		/* Mask all events in this page before adding it. */
167 		init_array_page(array_page);
168 
169 		expand_array.array_gfn = virt_to_gfn(array_page);
170 
171 		ret = HYPERVISOR_event_channel_op(EVTCHNOP_expand_array, &expand_array);
172 		if (ret < 0)
173 			goto error;
174 
175 		event_array_pages++;
176 	}
177 	return 0;
178 
179   error:
180 	if (event_array_pages == 0)
181 		panic("xen: unable to expand event array with initial page (%d)\n", ret);
182 	else
183 		pr_err("unable to expand event array (%d)\n", ret);
184 	free_unused_array_pages();
185 	return ret;
186 }
187 
188 static void evtchn_fifo_bind_to_cpu(evtchn_port_t evtchn, unsigned int cpu,
189 				    unsigned int old_cpu)
190 {
191 	/* no-op */
192 }
193 
194 static void evtchn_fifo_clear_pending(evtchn_port_t port)
195 {
196 	event_word_t *word = event_word_from_port(port);
197 	sync_clear_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
198 }
199 
200 static void evtchn_fifo_set_pending(evtchn_port_t port)
201 {
202 	event_word_t *word = event_word_from_port(port);
203 	sync_set_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
204 }
205 
206 static bool evtchn_fifo_is_pending(evtchn_port_t port)
207 {
208 	event_word_t *word = event_word_from_port(port);
209 	return sync_test_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
210 }
211 
212 static bool evtchn_fifo_test_and_set_mask(evtchn_port_t port)
213 {
214 	event_word_t *word = event_word_from_port(port);
215 	return sync_test_and_set_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
216 }
217 
218 static void evtchn_fifo_mask(evtchn_port_t port)
219 {
220 	event_word_t *word = event_word_from_port(port);
221 	sync_set_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
222 }
223 
224 static bool evtchn_fifo_is_masked(evtchn_port_t port)
225 {
226 	event_word_t *word = event_word_from_port(port);
227 	return sync_test_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
228 }
229 /*
230  * Clear MASKED if not PENDING, spinning if BUSY is set.
231  * Return true if mask was cleared.
232  */
233 static bool clear_masked_cond(volatile event_word_t *word)
234 {
235 	event_word_t new, old, w;
236 
237 	w = *word;
238 
239 	do {
240 		if (!(w & (1 << EVTCHN_FIFO_MASKED)))
241 			return true;
242 
243 		if (w & (1 << EVTCHN_FIFO_PENDING))
244 			return false;
245 
246 		old = w & ~(1 << EVTCHN_FIFO_BUSY);
247 		new = old & ~(1 << EVTCHN_FIFO_MASKED);
248 		w = sync_cmpxchg(word, old, new);
249 	} while (w != old);
250 
251 	return true;
252 }
253 
254 static void evtchn_fifo_unmask(evtchn_port_t port)
255 {
256 	event_word_t *word = event_word_from_port(port);
257 
258 	BUG_ON(!irqs_disabled());
259 
260 	if (!clear_masked_cond(word)) {
261 		struct evtchn_unmask unmask = { .port = port };
262 		(void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
263 	}
264 }
265 
266 static uint32_t clear_linked(volatile event_word_t *word)
267 {
268 	event_word_t new, old, w;
269 
270 	w = *word;
271 
272 	do {
273 		old = w;
274 		new = (w & ~((1 << EVTCHN_FIFO_LINKED)
275 			     | EVTCHN_FIFO_LINK_MASK));
276 	} while ((w = sync_cmpxchg(word, old, new)) != old);
277 
278 	return w & EVTCHN_FIFO_LINK_MASK;
279 }
280 
281 static void consume_one_event(unsigned cpu, struct evtchn_loop_ctrl *ctrl,
282 			      struct evtchn_fifo_control_block *control_block,
283 			      unsigned priority, unsigned long *ready)
284 {
285 	struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
286 	uint32_t head;
287 	evtchn_port_t port;
288 	event_word_t *word;
289 
290 	head = q->head[priority];
291 
292 	/*
293 	 * Reached the tail last time?  Read the new HEAD from the
294 	 * control block.
295 	 */
296 	if (head == 0) {
297 		virt_rmb(); /* Ensure word is up-to-date before reading head. */
298 		head = control_block->head[priority];
299 	}
300 
301 	port = head;
302 	word = event_word_from_port(port);
303 	head = clear_linked(word);
304 
305 	/*
306 	 * If the link is non-zero, there are more events in the
307 	 * queue, otherwise the queue is empty.
308 	 *
309 	 * If the queue is empty, clear this priority from our local
310 	 * copy of the ready word.
311 	 */
312 	if (head == 0)
313 		clear_bit(priority, ready);
314 
315 	if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port)) {
316 		if (unlikely(!ctrl))
317 			pr_warn("Dropping pending event for port %u\n", port);
318 		else
319 			handle_irq_for_port(port, ctrl);
320 	}
321 
322 	q->head[priority] = head;
323 }
324 
325 static void __evtchn_fifo_handle_events(unsigned cpu,
326 					struct evtchn_loop_ctrl *ctrl)
327 {
328 	struct evtchn_fifo_control_block *control_block;
329 	unsigned long ready;
330 	unsigned q;
331 
332 	control_block = per_cpu(cpu_control_block, cpu);
333 
334 	ready = xchg(&control_block->ready, 0);
335 
336 	while (ready) {
337 		q = find_first_bit(&ready, EVTCHN_FIFO_MAX_QUEUES);
338 		consume_one_event(cpu, ctrl, control_block, q, &ready);
339 		ready |= xchg(&control_block->ready, 0);
340 	}
341 }
342 
343 static void evtchn_fifo_handle_events(unsigned cpu,
344 				      struct evtchn_loop_ctrl *ctrl)
345 {
346 	__evtchn_fifo_handle_events(cpu, ctrl);
347 }
348 
349 static void evtchn_fifo_resume(void)
350 {
351 	unsigned cpu;
352 
353 	for_each_possible_cpu(cpu) {
354 		void *control_block = per_cpu(cpu_control_block, cpu);
355 		int ret;
356 
357 		if (!control_block)
358 			continue;
359 
360 		/*
361 		 * If this CPU is offline, take the opportunity to
362 		 * free the control block while it is not being
363 		 * used.
364 		 */
365 		if (!cpu_online(cpu)) {
366 			free_page((unsigned long)control_block);
367 			per_cpu(cpu_control_block, cpu) = NULL;
368 			continue;
369 		}
370 
371 		ret = init_control_block(cpu, control_block);
372 		BUG_ON(ret < 0);
373 	}
374 
375 	/*
376 	 * The event array starts out as empty again and is extended
377 	 * as normal when events are bound.  The existing pages will
378 	 * be reused.
379 	 */
380 	event_array_pages = 0;
381 }
382 
383 static int evtchn_fifo_alloc_control_block(unsigned cpu)
384 {
385 	void *control_block = NULL;
386 	int ret = -ENOMEM;
387 
388 	control_block = (void *)__get_free_page(GFP_KERNEL);
389 	if (control_block == NULL)
390 		goto error;
391 
392 	ret = init_control_block(cpu, control_block);
393 	if (ret < 0)
394 		goto error;
395 
396 	per_cpu(cpu_control_block, cpu) = control_block;
397 
398 	return 0;
399 
400   error:
401 	free_page((unsigned long)control_block);
402 	return ret;
403 }
404 
405 static int evtchn_fifo_percpu_init(unsigned int cpu)
406 {
407 	if (!per_cpu(cpu_control_block, cpu))
408 		return evtchn_fifo_alloc_control_block(cpu);
409 	return 0;
410 }
411 
412 static int evtchn_fifo_percpu_deinit(unsigned int cpu)
413 {
414 	__evtchn_fifo_handle_events(cpu, NULL);
415 	return 0;
416 }
417 
418 static const struct evtchn_ops evtchn_ops_fifo = {
419 	.max_channels      = evtchn_fifo_max_channels,
420 	.nr_channels       = evtchn_fifo_nr_channels,
421 	.setup             = evtchn_fifo_setup,
422 	.bind_to_cpu       = evtchn_fifo_bind_to_cpu,
423 	.clear_pending     = evtchn_fifo_clear_pending,
424 	.set_pending       = evtchn_fifo_set_pending,
425 	.is_pending        = evtchn_fifo_is_pending,
426 	.test_and_set_mask = evtchn_fifo_test_and_set_mask,
427 	.mask              = evtchn_fifo_mask,
428 	.unmask            = evtchn_fifo_unmask,
429 	.handle_events     = evtchn_fifo_handle_events,
430 	.resume            = evtchn_fifo_resume,
431 	.percpu_init       = evtchn_fifo_percpu_init,
432 	.percpu_deinit     = evtchn_fifo_percpu_deinit,
433 };
434 
435 int __init xen_evtchn_fifo_init(void)
436 {
437 	int cpu = smp_processor_id();
438 	int ret;
439 
440 	ret = evtchn_fifo_alloc_control_block(cpu);
441 	if (ret < 0)
442 		return ret;
443 
444 	pr_info("Using FIFO-based ABI\n");
445 
446 	evtchn_ops = &evtchn_ops_fifo;
447 
448 	return ret;
449 }
450