blob: eff2b88003d930c2085b33820e970d55c1008b9c [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
Michael S. Tsirkin23492752015-12-27 18:02:16 +020044#include <asm/barrier.h>
David Vrabel1fe56552013-03-15 13:02:35 +000045#include <asm/sync_bitops.h>
46#include <asm/xen/hypercall.h>
47#include <asm/xen/hypervisor.h>
David Vrabel1fe56552013-03-15 13:02:35 +000048
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>
Julien Gralla9fd60e2015-06-17 15:28:02 +010054#include <xen/page.h>
David Vrabel1fe56552013-03-15 13:02:35 +000055
56#include "events_internal.h"
57
Julien Gralla001c9d2015-05-05 16:37:30 +010058#define EVENT_WORDS_PER_PAGE (XEN_PAGE_SIZE / sizeof(event_word_t))
David Vrabel1fe56552013-03-15 13:02:35 +000059#define MAX_EVENT_ARRAY_PAGES (EVTCHN_FIFO_NR_CHANNELS / EVENT_WORDS_PER_PAGE)
60
61struct evtchn_fifo_queue {
62 uint32_t head[EVTCHN_FIFO_MAX_QUEUES];
63};
64
65static DEFINE_PER_CPU(struct evtchn_fifo_control_block *, cpu_control_block);
66static DEFINE_PER_CPU(struct evtchn_fifo_queue, cpu_queue);
67static event_word_t *event_array[MAX_EVENT_ARRAY_PAGES] __read_mostly;
68static unsigned event_array_pages __read_mostly;
69
Vladimir Murzin05a812a2014-04-27 10:09:12 +010070/*
David Vrabeldcecb8f2014-07-31 16:22:25 +010071 * sync_set_bit() and friends must be unsigned long aligned.
Vladimir Murzin05a812a2014-04-27 10:09:12 +010072 */
David Vrabeldcecb8f2014-07-31 16:22:25 +010073#if BITS_PER_LONG > 32
Vladimir Murzin05a812a2014-04-27 10:09:12 +010074
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
David Vrabelc12784c2014-07-31 16:22:24 +0100103static int init_control_block(int cpu,
104 struct evtchn_fifo_control_block *control_block)
105{
106 struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
107 struct evtchn_init_control init_control;
108 unsigned int i;
109
110 /* Reset the control block and the local HEADs. */
111 clear_page(control_block);
112 for (i = 0; i < EVTCHN_FIFO_MAX_QUEUES; i++)
113 q->head[i] = 0;
114
Julien Grall0df4f262015-08-07 17:34:37 +0100115 init_control.control_gfn = virt_to_gfn(control_block);
David Vrabelc12784c2014-07-31 16:22:24 +0100116 init_control.offset = 0;
117 init_control.vcpu = cpu;
118
119 return HYPERVISOR_event_channel_op(EVTCHNOP_init_control, &init_control);
120}
121
David Vrabel1fe56552013-03-15 13:02:35 +0000122static void free_unused_array_pages(void)
123{
124 unsigned i;
125
126 for (i = event_array_pages; i < MAX_EVENT_ARRAY_PAGES; i++) {
127 if (!event_array[i])
128 break;
129 free_page((unsigned long)event_array[i]);
130 event_array[i] = NULL;
131 }
132}
133
134static void init_array_page(event_word_t *array_page)
135{
136 unsigned i;
137
138 for (i = 0; i < EVENT_WORDS_PER_PAGE; i++)
139 array_page[i] = 1 << EVTCHN_FIFO_MASKED;
140}
141
142static int evtchn_fifo_setup(struct irq_info *info)
143{
144 unsigned port = info->evtchn;
145 unsigned new_array_pages;
Wei Yongjunbe1403b2014-01-07 21:11:25 +0800146 int ret;
David Vrabel1fe56552013-03-15 13:02:35 +0000147
148 new_array_pages = port / EVENT_WORDS_PER_PAGE + 1;
149
150 if (new_array_pages > MAX_EVENT_ARRAY_PAGES)
151 return -EINVAL;
152
153 while (event_array_pages < new_array_pages) {
154 void *array_page;
155 struct evtchn_expand_array expand_array;
156
157 /* Might already have a page if we've resumed. */
158 array_page = event_array[event_array_pages];
159 if (!array_page) {
160 array_page = (void *)__get_free_page(GFP_KERNEL);
Wei Yongjunbe1403b2014-01-07 21:11:25 +0800161 if (array_page == NULL) {
162 ret = -ENOMEM;
David Vrabel1fe56552013-03-15 13:02:35 +0000163 goto error;
Wei Yongjunbe1403b2014-01-07 21:11:25 +0800164 }
David Vrabel1fe56552013-03-15 13:02:35 +0000165 event_array[event_array_pages] = array_page;
166 }
167
168 /* Mask all events in this page before adding it. */
169 init_array_page(array_page);
170
Julien Grall0df4f262015-08-07 17:34:37 +0100171 expand_array.array_gfn = virt_to_gfn(array_page);
David Vrabel1fe56552013-03-15 13:02:35 +0000172
173 ret = HYPERVISOR_event_channel_op(EVTCHNOP_expand_array, &expand_array);
174 if (ret < 0)
175 goto error;
176
177 event_array_pages++;
178 }
179 return 0;
180
181 error:
182 if (event_array_pages == 0)
183 panic("xen: unable to expand event array with initial page (%d)\n", ret);
184 else
185 pr_err("unable to expand event array (%d)\n", ret);
186 free_unused_array_pages();
187 return ret;
188}
189
190static void evtchn_fifo_bind_to_cpu(struct irq_info *info, unsigned cpu)
191{
192 /* no-op */
193}
194
195static void evtchn_fifo_clear_pending(unsigned port)
196{
197 event_word_t *word = event_word_from_port(port);
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100198 sync_clear_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
David Vrabel1fe56552013-03-15 13:02:35 +0000199}
200
201static void evtchn_fifo_set_pending(unsigned port)
202{
203 event_word_t *word = event_word_from_port(port);
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100204 sync_set_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
David Vrabel1fe56552013-03-15 13:02:35 +0000205}
206
207static bool evtchn_fifo_is_pending(unsigned port)
208{
209 event_word_t *word = event_word_from_port(port);
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100210 return sync_test_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
David Vrabel1fe56552013-03-15 13:02:35 +0000211}
212
213static bool evtchn_fifo_test_and_set_mask(unsigned port)
214{
215 event_word_t *word = event_word_from_port(port);
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100216 return sync_test_and_set_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
David Vrabel1fe56552013-03-15 13:02:35 +0000217}
218
219static void evtchn_fifo_mask(unsigned port)
220{
221 event_word_t *word = event_word_from_port(port);
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100222 sync_set_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
David Vrabel1fe56552013-03-15 13:02:35 +0000223}
224
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100225static bool evtchn_fifo_is_masked(unsigned port)
226{
227 event_word_t *word = event_word_from_port(port);
228 return sync_test_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
229}
David Vrabel1fe56552013-03-15 13:02:35 +0000230/*
231 * Clear MASKED, spinning if BUSY is set.
232 */
233static void clear_masked(volatile event_word_t *word)
234{
235 event_word_t new, old, w;
236
237 w = *word;
238
239 do {
240 old = w & ~(1 << EVTCHN_FIFO_BUSY);
241 new = old & ~(1 << EVTCHN_FIFO_MASKED);
242 w = sync_cmpxchg(word, old, new);
243 } while (w != old);
244}
245
246static void evtchn_fifo_unmask(unsigned port)
247{
248 event_word_t *word = event_word_from_port(port);
249
250 BUG_ON(!irqs_disabled());
251
252 clear_masked(word);
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100253 if (evtchn_fifo_is_pending(port)) {
David Vrabel1fe56552013-03-15 13:02:35 +0000254 struct evtchn_unmask unmask = { .port = port };
255 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
256 }
257}
258
259static uint32_t clear_linked(volatile event_word_t *word)
260{
261 event_word_t new, old, w;
262
263 w = *word;
264
265 do {
266 old = w;
267 new = (w & ~((1 << EVTCHN_FIFO_LINKED)
268 | EVTCHN_FIFO_LINK_MASK));
269 } while ((w = sync_cmpxchg(word, old, new)) != old);
270
271 return w & EVTCHN_FIFO_LINK_MASK;
272}
273
274static void handle_irq_for_port(unsigned port)
275{
276 int irq;
David Vrabel1fe56552013-03-15 13:02:35 +0000277
278 irq = get_evtchn_to_irq(port);
Thomas Gleixner589d03e2014-02-23 21:40:18 +0000279 if (irq != -1)
280 generic_handle_irq(irq);
David Vrabel1fe56552013-03-15 13:02:35 +0000281}
282
283static void consume_one_event(unsigned cpu,
284 struct evtchn_fifo_control_block *control_block,
Ross Lagerwall3de88d62015-06-19 16:15:57 +0100285 unsigned priority, unsigned long *ready,
286 bool drop)
David Vrabel1fe56552013-03-15 13:02:35 +0000287{
288 struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
289 uint32_t head;
290 unsigned port;
291 event_word_t *word;
292
293 head = q->head[priority];
294
295 /*
296 * Reached the tail last time? Read the new HEAD from the
297 * control block.
298 */
299 if (head == 0) {
Michael S. Tsirkin23492752015-12-27 18:02:16 +0200300 virt_rmb(); /* Ensure word is up-to-date before reading head. */
David Vrabel1fe56552013-03-15 13:02:35 +0000301 head = control_block->head[priority];
302 }
303
304 port = head;
305 word = event_word_from_port(port);
306 head = clear_linked(word);
307
308 /*
309 * If the link is non-zero, there are more events in the
310 * queue, otherwise the queue is empty.
311 *
312 * If the queue is empty, clear this priority from our local
313 * copy of the ready word.
314 */
315 if (head == 0)
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100316 clear_bit(priority, ready);
David Vrabel1fe56552013-03-15 13:02:35 +0000317
Ross Lagerwall3de88d62015-06-19 16:15:57 +0100318 if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port)) {
319 if (unlikely(drop))
320 pr_warn("Dropping pending event for port %u\n", port);
321 else
322 handle_irq_for_port(port);
323 }
David Vrabel1fe56552013-03-15 13:02:35 +0000324
325 q->head[priority] = head;
326}
327
Ross Lagerwall3de88d62015-06-19 16:15:57 +0100328static void __evtchn_fifo_handle_events(unsigned cpu, bool drop)
David Vrabel1fe56552013-03-15 13:02:35 +0000329{
330 struct evtchn_fifo_control_block *control_block;
Vladimir Murzin05a812a2014-04-27 10:09:12 +0100331 unsigned long ready;
David Vrabel1fe56552013-03-15 13:02:35 +0000332 unsigned q;
333
334 control_block = per_cpu(cpu_control_block, cpu);
335
336 ready = xchg(&control_block->ready, 0);
337
338 while (ready) {
Frediano Ziglioe4a74312014-07-31 16:22:26 +0100339 q = find_first_bit(&ready, EVTCHN_FIFO_MAX_QUEUES);
Ross Lagerwall3de88d62015-06-19 16:15:57 +0100340 consume_one_event(cpu, control_block, q, &ready, drop);
David Vrabel1fe56552013-03-15 13:02:35 +0000341 ready |= xchg(&control_block->ready, 0);
342 }
343}
344
Ross Lagerwall3de88d62015-06-19 16:15:57 +0100345static void evtchn_fifo_handle_events(unsigned cpu)
346{
347 __evtchn_fifo_handle_events(cpu, false);
348}
349
David Vrabel1fe56552013-03-15 13:02:35 +0000350static void evtchn_fifo_resume(void)
351{
352 unsigned cpu;
353
354 for_each_possible_cpu(cpu) {
355 void *control_block = per_cpu(cpu_control_block, cpu);
David Vrabel1fe56552013-03-15 13:02:35 +0000356 int ret;
357
358 if (!control_block)
359 continue;
360
361 /*
362 * If this CPU is offline, take the opportunity to
363 * free the control block while it is not being
364 * used.
365 */
366 if (!cpu_online(cpu)) {
367 free_page((unsigned long)control_block);
368 per_cpu(cpu_control_block, cpu) = NULL;
369 continue;
370 }
371
David Vrabelc12784c2014-07-31 16:22:24 +0100372 ret = init_control_block(cpu, control_block);
David Vrabel1fe56552013-03-15 13:02:35 +0000373 if (ret < 0)
374 BUG();
375 }
376
377 /*
378 * The event array starts out as empty again and is extended
379 * as normal when events are bound. The existing pages will
380 * be reused.
381 */
382 event_array_pages = 0;
383}
384
385static const struct evtchn_ops evtchn_ops_fifo = {
386 .max_channels = evtchn_fifo_max_channels,
387 .nr_channels = evtchn_fifo_nr_channels,
388 .setup = evtchn_fifo_setup,
389 .bind_to_cpu = evtchn_fifo_bind_to_cpu,
390 .clear_pending = evtchn_fifo_clear_pending,
391 .set_pending = evtchn_fifo_set_pending,
392 .is_pending = evtchn_fifo_is_pending,
393 .test_and_set_mask = evtchn_fifo_test_and_set_mask,
394 .mask = evtchn_fifo_mask,
395 .unmask = evtchn_fifo_unmask,
396 .handle_events = evtchn_fifo_handle_events,
397 .resume = evtchn_fifo_resume,
398};
399
David Vrabelc12784c2014-07-31 16:22:24 +0100400static int evtchn_fifo_alloc_control_block(unsigned cpu)
David Vrabel1fe56552013-03-15 13:02:35 +0000401{
David Vrabelc12784c2014-07-31 16:22:24 +0100402 void *control_block = NULL;
David Vrabel1fe56552013-03-15 13:02:35 +0000403 int ret = -ENOMEM;
404
David Vrabelc12784c2014-07-31 16:22:24 +0100405 control_block = (void *)__get_free_page(GFP_KERNEL);
David Vrabel1fe56552013-03-15 13:02:35 +0000406 if (control_block == NULL)
407 goto error;
408
David Vrabelc12784c2014-07-31 16:22:24 +0100409 ret = init_control_block(cpu, control_block);
David Vrabel1fe56552013-03-15 13:02:35 +0000410 if (ret < 0)
411 goto error;
412
David Vrabelc12784c2014-07-31 16:22:24 +0100413 per_cpu(cpu_control_block, cpu) = control_block;
David Vrabel1fe56552013-03-15 13:02:35 +0000414
415 return 0;
416
417 error:
David Vrabelc12784c2014-07-31 16:22:24 +0100418 free_page((unsigned long)control_block);
David Vrabel1fe56552013-03-15 13:02:35 +0000419 return ret;
420}
421
Paul Gortmaker0db69912014-01-10 09:50:08 -0500422static int evtchn_fifo_cpu_notification(struct notifier_block *self,
David Vrabel1fe56552013-03-15 13:02:35 +0000423 unsigned long action,
424 void *hcpu)
425{
426 int cpu = (long)hcpu;
427 int ret = 0;
428
429 switch (action) {
430 case CPU_UP_PREPARE:
431 if (!per_cpu(cpu_control_block, cpu))
David Vrabelc12784c2014-07-31 16:22:24 +0100432 ret = evtchn_fifo_alloc_control_block(cpu);
David Vrabel1fe56552013-03-15 13:02:35 +0000433 break;
Ross Lagerwall3de88d62015-06-19 16:15:57 +0100434 case CPU_DEAD:
435 __evtchn_fifo_handle_events(cpu, true);
436 break;
David Vrabel1fe56552013-03-15 13:02:35 +0000437 default:
438 break;
439 }
440 return ret < 0 ? NOTIFY_BAD : NOTIFY_OK;
441}
442
Paul Gortmaker0db69912014-01-10 09:50:08 -0500443static struct notifier_block evtchn_fifo_cpu_notifier = {
David Vrabel1fe56552013-03-15 13:02:35 +0000444 .notifier_call = evtchn_fifo_cpu_notification,
445};
446
447int __init xen_evtchn_fifo_init(void)
448{
449 int cpu = get_cpu();
450 int ret;
451
David Vrabelc12784c2014-07-31 16:22:24 +0100452 ret = evtchn_fifo_alloc_control_block(cpu);
David Vrabel1fe56552013-03-15 13:02:35 +0000453 if (ret < 0)
454 goto out;
455
456 pr_info("Using FIFO-based ABI\n");
457
458 evtchn_ops = &evtchn_ops_fifo;
459
460 register_cpu_notifier(&evtchn_fifo_cpu_notifier);
461out:
462 put_cpu();
463 return ret;
464}