blob: 84b4bfb843443ef934d2d80abc12131bb8616a5a [file] [log] [blame]
David Vrabel1fe56552013-03-15 13:02:35 +00001/*
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/module.h>
40#include <linux/smp.h>
41#include <linux/percpu.h>
42#include <linux/cpu.h>
43
44#include <asm/sync_bitops.h>
45#include <asm/xen/hypercall.h>
46#include <asm/xen/hypervisor.h>
47#include <asm/xen/page.h>
48
49#include <xen/xen.h>
50#include <xen/xen-ops.h>
51#include <xen/events.h>
52#include <xen/interface/xen.h>
53#include <xen/interface/event_channel.h>
54
55#include "events_internal.h"
56
57#define EVENT_WORDS_PER_PAGE (PAGE_SIZE / sizeof(event_word_t))
58#define MAX_EVENT_ARRAY_PAGES (EVTCHN_FIFO_NR_CHANNELS / EVENT_WORDS_PER_PAGE)
59
60struct evtchn_fifo_queue {
61 uint32_t head[EVTCHN_FIFO_MAX_QUEUES];
62};
63
64static DEFINE_PER_CPU(struct evtchn_fifo_control_block *, cpu_control_block);
65static DEFINE_PER_CPU(struct evtchn_fifo_queue, cpu_queue);
66static event_word_t *event_array[MAX_EVENT_ARRAY_PAGES] __read_mostly;
67static unsigned event_array_pages __read_mostly;
68
Vladimir Murzin05a812a2014-04-27 10:09:12 +010069/*
70 * sync_set_bit() and friends must be unsigned long aligned on non-x86
71 * platforms.
72 */
73#if !defined(CONFIG_X86) && BITS_PER_LONG > 32
74
75#define BM(w) (unsigned long *)((unsigned long)w & ~0x7UL)
76#define EVTCHN_FIFO_BIT(b, w) \
77 (((unsigned long)w & 0x4UL) ? (EVTCHN_FIFO_ ##b + 32) : EVTCHN_FIFO_ ##b)
78
79#else
80
David Vrabel1fe56552013-03-15 13:02:35 +000081#define BM(w) ((unsigned long *)(w))
Vladimir Murzin05a812a2014-04-27 10:09:12 +010082#define EVTCHN_FIFO_BIT(b, w) EVTCHN_FIFO_ ##b
83
84#endif
David Vrabel1fe56552013-03-15 13:02:35 +000085
86static inline event_word_t *event_word_from_port(unsigned port)
87{
88 unsigned i = port / EVENT_WORDS_PER_PAGE;
89
90 return event_array[i] + port % EVENT_WORDS_PER_PAGE;
91}
92
93static unsigned evtchn_fifo_max_channels(void)
94{
95 return EVTCHN_FIFO_NR_CHANNELS;
96}
97
98static unsigned evtchn_fifo_nr_channels(void)
99{
100 return event_array_pages * EVENT_WORDS_PER_PAGE;
101}
102
103static void free_unused_array_pages(void)
104{
105 unsigned i;
106
107 for (i = event_array_pages; i < MAX_EVENT_ARRAY_PAGES; i++) {
108 if (!event_array[i])
109 break;
110 free_page((unsigned long)event_array[i]);
111 event_array[i] = NULL;
112 }
113}
114
115static void init_array_page(event_word_t *array_page)
116{
117 unsigned i;
118
119 for (i = 0; i < EVENT_WORDS_PER_PAGE; i++)
120 array_page[i] = 1 << EVTCHN_FIFO_MASKED;
121}
122
123static int evtchn_fifo_setup(struct irq_info *info)
124{
125 unsigned port = info->evtchn;
126 unsigned new_array_pages;
Wei Yongjunbe1403b2014-01-07 21:11:25 +0800127 int ret;
David Vrabel1fe56552013-03-15 13:02:35 +0000128
129 new_array_pages = port / EVENT_WORDS_PER_PAGE + 1;
130
131 if (new_array_pages > MAX_EVENT_ARRAY_PAGES)
132 return -EINVAL;
133
134 while (event_array_pages < new_array_pages) {
135 void *array_page;
136 struct evtchn_expand_array expand_array;
137
138 /* Might already have a page if we've resumed. */
139 array_page = event_array[event_array_pages];
140 if (!array_page) {
141 array_page = (void *)__get_free_page(GFP_KERNEL);
Wei Yongjunbe1403b2014-01-07 21:11:25 +0800142 if (array_page == NULL) {
143 ret = -ENOMEM;
David Vrabel1fe56552013-03-15 13:02:35 +0000144 goto error;
Wei Yongjunbe1403b2014-01-07 21:11:25 +0800145 }
David Vrabel1fe56552013-03-15 13:02:35 +0000146 event_array[event_array_pages] = array_page;
147 }
148
149 /* Mask all events in this page before adding it. */
150 init_array_page(array_page);
151
152 expand_array.array_gfn = virt_to_mfn(array_page);
153
154 ret = HYPERVISOR_event_channel_op(EVTCHNOP_expand_array, &expand_array);
155 if (ret < 0)
156 goto error;
157
158 event_array_pages++;
159 }
160 return 0;
161
162 error:
163 if (event_array_pages == 0)
164 panic("xen: unable to expand event array with initial page (%d)\n", ret);
165 else
166 pr_err("unable to expand event array (%d)\n", ret);
167 free_unused_array_pages();
168 return ret;
169}
170
171static void evtchn_fifo_bind_to_cpu(struct irq_info *info, unsigned cpu)
172{
173 /* no-op */
174}
175
176static void evtchn_fifo_clear_pending(unsigned port)
177{
178 event_word_t *word = event_word_from_port(port);
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100179 sync_clear_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
David Vrabel1fe56552013-03-15 13:02:35 +0000180}
181
182static void evtchn_fifo_set_pending(unsigned port)
183{
184 event_word_t *word = event_word_from_port(port);
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100185 sync_set_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
David Vrabel1fe56552013-03-15 13:02:35 +0000186}
187
188static bool evtchn_fifo_is_pending(unsigned port)
189{
190 event_word_t *word = event_word_from_port(port);
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100191 return sync_test_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
David Vrabel1fe56552013-03-15 13:02:35 +0000192}
193
194static bool evtchn_fifo_test_and_set_mask(unsigned port)
195{
196 event_word_t *word = event_word_from_port(port);
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100197 return sync_test_and_set_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
David Vrabel1fe56552013-03-15 13:02:35 +0000198}
199
200static void evtchn_fifo_mask(unsigned port)
201{
202 event_word_t *word = event_word_from_port(port);
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100203 sync_set_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
David Vrabel1fe56552013-03-15 13:02:35 +0000204}
205
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100206static bool evtchn_fifo_is_masked(unsigned port)
207{
208 event_word_t *word = event_word_from_port(port);
209 return sync_test_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
210}
David Vrabel1fe56552013-03-15 13:02:35 +0000211/*
212 * Clear MASKED, spinning if BUSY is set.
213 */
214static void clear_masked(volatile event_word_t *word)
215{
216 event_word_t new, old, w;
217
218 w = *word;
219
220 do {
221 old = w & ~(1 << EVTCHN_FIFO_BUSY);
222 new = old & ~(1 << EVTCHN_FIFO_MASKED);
223 w = sync_cmpxchg(word, old, new);
224 } while (w != old);
225}
226
227static void evtchn_fifo_unmask(unsigned port)
228{
229 event_word_t *word = event_word_from_port(port);
230
231 BUG_ON(!irqs_disabled());
232
233 clear_masked(word);
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100234 if (evtchn_fifo_is_pending(port)) {
David Vrabel1fe56552013-03-15 13:02:35 +0000235 struct evtchn_unmask unmask = { .port = port };
236 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
237 }
238}
239
240static uint32_t clear_linked(volatile event_word_t *word)
241{
242 event_word_t new, old, w;
243
244 w = *word;
245
246 do {
247 old = w;
248 new = (w & ~((1 << EVTCHN_FIFO_LINKED)
249 | EVTCHN_FIFO_LINK_MASK));
250 } while ((w = sync_cmpxchg(word, old, new)) != old);
251
252 return w & EVTCHN_FIFO_LINK_MASK;
253}
254
255static void handle_irq_for_port(unsigned port)
256{
257 int irq;
David Vrabel1fe56552013-03-15 13:02:35 +0000258
259 irq = get_evtchn_to_irq(port);
Thomas Gleixner589d03e2014-02-23 21:40:18 +0000260 if (irq != -1)
261 generic_handle_irq(irq);
David Vrabel1fe56552013-03-15 13:02:35 +0000262}
263
264static void consume_one_event(unsigned cpu,
265 struct evtchn_fifo_control_block *control_block,
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100266 unsigned priority, unsigned long *ready)
David Vrabel1fe56552013-03-15 13:02:35 +0000267{
268 struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
269 uint32_t head;
270 unsigned port;
271 event_word_t *word;
272
273 head = q->head[priority];
274
275 /*
276 * Reached the tail last time? Read the new HEAD from the
277 * control block.
278 */
279 if (head == 0) {
280 rmb(); /* Ensure word is up-to-date before reading head. */
281 head = control_block->head[priority];
282 }
283
284 port = head;
285 word = event_word_from_port(port);
286 head = clear_linked(word);
287
288 /*
289 * If the link is non-zero, there are more events in the
290 * queue, otherwise the queue is empty.
291 *
292 * If the queue is empty, clear this priority from our local
293 * copy of the ready word.
294 */
295 if (head == 0)
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100296 clear_bit(priority, ready);
David Vrabel1fe56552013-03-15 13:02:35 +0000297
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100298 if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port))
David Vrabel1fe56552013-03-15 13:02:35 +0000299 handle_irq_for_port(port);
300
301 q->head[priority] = head;
302}
303
304static void evtchn_fifo_handle_events(unsigned cpu)
305{
306 struct evtchn_fifo_control_block *control_block;
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100307 unsigned long ready;
David Vrabel1fe56552013-03-15 13:02:35 +0000308 unsigned q;
309
310 control_block = per_cpu(cpu_control_block, cpu);
311
312 ready = xchg(&control_block->ready, 0);
313
314 while (ready) {
315 q = find_first_bit(BM(&ready), EVTCHN_FIFO_MAX_QUEUES);
316 consume_one_event(cpu, control_block, q, &ready);
317 ready |= xchg(&control_block->ready, 0);
318 }
319}
320
321static void evtchn_fifo_resume(void)
322{
323 unsigned cpu;
324
325 for_each_possible_cpu(cpu) {
326 void *control_block = per_cpu(cpu_control_block, cpu);
327 struct evtchn_init_control init_control;
328 int ret;
329
330 if (!control_block)
331 continue;
332
333 /*
334 * If this CPU is offline, take the opportunity to
335 * free the control block while it is not being
336 * used.
337 */
338 if (!cpu_online(cpu)) {
339 free_page((unsigned long)control_block);
340 per_cpu(cpu_control_block, cpu) = NULL;
341 continue;
342 }
343
344 init_control.control_gfn = virt_to_mfn(control_block);
345 init_control.offset = 0;
346 init_control.vcpu = cpu;
347
348 ret = HYPERVISOR_event_channel_op(EVTCHNOP_init_control,
349 &init_control);
350 if (ret < 0)
351 BUG();
352 }
353
354 /*
355 * The event array starts out as empty again and is extended
356 * as normal when events are bound. The existing pages will
357 * be reused.
358 */
359 event_array_pages = 0;
360}
361
362static const struct evtchn_ops evtchn_ops_fifo = {
363 .max_channels = evtchn_fifo_max_channels,
364 .nr_channels = evtchn_fifo_nr_channels,
365 .setup = evtchn_fifo_setup,
366 .bind_to_cpu = evtchn_fifo_bind_to_cpu,
367 .clear_pending = evtchn_fifo_clear_pending,
368 .set_pending = evtchn_fifo_set_pending,
369 .is_pending = evtchn_fifo_is_pending,
370 .test_and_set_mask = evtchn_fifo_test_and_set_mask,
371 .mask = evtchn_fifo_mask,
372 .unmask = evtchn_fifo_unmask,
373 .handle_events = evtchn_fifo_handle_events,
374 .resume = evtchn_fifo_resume,
375};
376
Paul Gortmaker0db69912014-01-10 09:50:08 -0500377static int evtchn_fifo_init_control_block(unsigned cpu)
David Vrabel1fe56552013-03-15 13:02:35 +0000378{
379 struct page *control_block = NULL;
380 struct evtchn_init_control init_control;
381 int ret = -ENOMEM;
382
383 control_block = alloc_page(GFP_KERNEL|__GFP_ZERO);
384 if (control_block == NULL)
385 goto error;
386
387 init_control.control_gfn = virt_to_mfn(page_address(control_block));
388 init_control.offset = 0;
389 init_control.vcpu = cpu;
390
391 ret = HYPERVISOR_event_channel_op(EVTCHNOP_init_control, &init_control);
392 if (ret < 0)
393 goto error;
394
395 per_cpu(cpu_control_block, cpu) = page_address(control_block);
396
397 return 0;
398
399 error:
400 __free_page(control_block);
401 return ret;
402}
403
Paul Gortmaker0db69912014-01-10 09:50:08 -0500404static int evtchn_fifo_cpu_notification(struct notifier_block *self,
David Vrabel1fe56552013-03-15 13:02:35 +0000405 unsigned long action,
406 void *hcpu)
407{
408 int cpu = (long)hcpu;
409 int ret = 0;
410
411 switch (action) {
412 case CPU_UP_PREPARE:
413 if (!per_cpu(cpu_control_block, cpu))
414 ret = evtchn_fifo_init_control_block(cpu);
415 break;
416 default:
417 break;
418 }
419 return ret < 0 ? NOTIFY_BAD : NOTIFY_OK;
420}
421
Paul Gortmaker0db69912014-01-10 09:50:08 -0500422static struct notifier_block evtchn_fifo_cpu_notifier = {
David Vrabel1fe56552013-03-15 13:02:35 +0000423 .notifier_call = evtchn_fifo_cpu_notification,
424};
425
426int __init xen_evtchn_fifo_init(void)
427{
428 int cpu = get_cpu();
429 int ret;
430
431 ret = evtchn_fifo_init_control_block(cpu);
432 if (ret < 0)
433 goto out;
434
435 pr_info("Using FIFO-based ABI\n");
436
437 evtchn_ops = &evtchn_ops_fifo;
438
439 register_cpu_notifier(&evtchn_fifo_cpu_notifier);
440out:
441 put_cpu();
442 return ret;
443}