2 * Xen event channels (FIFO-based ABI)
4 * Copyright (C) 2013 Citrix Systems R&D ltd.
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.
11 * Or, when distributed separately from the Linux kernel or
12 * incorporated into other software packages, subject to the following
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:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
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
34 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
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>
44 #include <asm/barrier.h>
45 #include <asm/sync_bitops.h>
46 #include <asm/xen/hypercall.h>
47 #include <asm/xen/hypervisor.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>
56 #include "events_internal.h"
58 #define EVENT_WORDS_PER_PAGE (XEN_PAGE_SIZE / sizeof(event_word_t))
59 #define MAX_EVENT_ARRAY_PAGES (EVTCHN_FIFO_NR_CHANNELS / EVENT_WORDS_PER_PAGE)
61 struct evtchn_fifo_queue {
62 uint32_t head[EVTCHN_FIFO_MAX_QUEUES];
65 static DEFINE_PER_CPU(struct evtchn_fifo_control_block *, cpu_control_block);
66 static DEFINE_PER_CPU(struct evtchn_fifo_queue, cpu_queue);
67 static event_word_t *event_array[MAX_EVENT_ARRAY_PAGES] __read_mostly;
68 static unsigned event_array_pages __read_mostly;
71 * sync_set_bit() and friends must be unsigned long aligned.
73 #if BITS_PER_LONG > 32
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)
81 #define BM(w) ((unsigned long *)(w))
82 #define EVTCHN_FIFO_BIT(b, w) EVTCHN_FIFO_ ##b
86 static inline event_word_t *event_word_from_port(unsigned port)
88 unsigned i = port / EVENT_WORDS_PER_PAGE;
90 return event_array[i] + port % EVENT_WORDS_PER_PAGE;
93 static unsigned evtchn_fifo_max_channels(void)
95 return EVTCHN_FIFO_NR_CHANNELS;
98 static unsigned evtchn_fifo_nr_channels(void)
100 return event_array_pages * EVENT_WORDS_PER_PAGE;
103 static int init_control_block(int cpu,
104 struct evtchn_fifo_control_block *control_block)
106 struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
107 struct evtchn_init_control init_control;
110 /* Reset the control block and the local HEADs. */
111 clear_page(control_block);
112 for (i = 0; i < EVTCHN_FIFO_MAX_QUEUES; i++)
115 init_control.control_gfn = virt_to_gfn(control_block);
116 init_control.offset = 0;
117 init_control.vcpu = cpu;
119 return HYPERVISOR_event_channel_op(EVTCHNOP_init_control, &init_control);
122 static void free_unused_array_pages(void)
126 for (i = event_array_pages; i < MAX_EVENT_ARRAY_PAGES; i++) {
129 free_page((unsigned long)event_array[i]);
130 event_array[i] = NULL;
134 static void init_array_page(event_word_t *array_page)
138 for (i = 0; i < EVENT_WORDS_PER_PAGE; i++)
139 array_page[i] = 1 << EVTCHN_FIFO_MASKED;
142 static int evtchn_fifo_setup(struct irq_info *info)
144 unsigned port = info->evtchn;
145 unsigned new_array_pages;
148 new_array_pages = port / EVENT_WORDS_PER_PAGE + 1;
150 if (new_array_pages > MAX_EVENT_ARRAY_PAGES)
153 while (event_array_pages < new_array_pages) {
155 struct evtchn_expand_array expand_array;
157 /* Might already have a page if we've resumed. */
158 array_page = event_array[event_array_pages];
160 array_page = (void *)__get_free_page(GFP_KERNEL);
161 if (array_page == NULL) {
165 event_array[event_array_pages] = array_page;
168 /* Mask all events in this page before adding it. */
169 init_array_page(array_page);
171 expand_array.array_gfn = virt_to_gfn(array_page);
173 ret = HYPERVISOR_event_channel_op(EVTCHNOP_expand_array, &expand_array);
182 if (event_array_pages == 0)
183 panic("xen: unable to expand event array with initial page (%d)\n", ret);
185 pr_err("unable to expand event array (%d)\n", ret);
186 free_unused_array_pages();
190 static void evtchn_fifo_bind_to_cpu(struct irq_info *info, unsigned cpu)
195 static void evtchn_fifo_clear_pending(unsigned port)
197 event_word_t *word = event_word_from_port(port);
198 sync_clear_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
201 static void evtchn_fifo_set_pending(unsigned port)
203 event_word_t *word = event_word_from_port(port);
204 sync_set_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
207 static bool evtchn_fifo_is_pending(unsigned port)
209 event_word_t *word = event_word_from_port(port);
210 return sync_test_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
213 static bool evtchn_fifo_test_and_set_mask(unsigned port)
215 event_word_t *word = event_word_from_port(port);
216 return sync_test_and_set_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
219 static void evtchn_fifo_mask(unsigned port)
221 event_word_t *word = event_word_from_port(port);
222 sync_set_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
225 static bool evtchn_fifo_is_masked(unsigned port)
227 event_word_t *word = event_word_from_port(port);
228 return sync_test_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
231 * Clear MASKED, spinning if BUSY is set.
233 static void clear_masked(volatile event_word_t *word)
235 event_word_t new, old, w;
240 old = w & ~(1 << EVTCHN_FIFO_BUSY);
241 new = old & ~(1 << EVTCHN_FIFO_MASKED);
242 w = sync_cmpxchg(word, old, new);
246 static void evtchn_fifo_unmask(unsigned port)
248 event_word_t *word = event_word_from_port(port);
250 BUG_ON(!irqs_disabled());
253 if (evtchn_fifo_is_pending(port)) {
254 struct evtchn_unmask unmask = { .port = port };
255 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
259 static uint32_t clear_linked(volatile event_word_t *word)
261 event_word_t new, old, w;
267 new = (w & ~((1 << EVTCHN_FIFO_LINKED)
268 | EVTCHN_FIFO_LINK_MASK));
269 } while ((w = sync_cmpxchg(word, old, new)) != old);
271 return w & EVTCHN_FIFO_LINK_MASK;
274 static void handle_irq_for_port(unsigned port)
278 irq = get_evtchn_to_irq(port);
280 generic_handle_irq(irq);
283 static void consume_one_event(unsigned cpu,
284 struct evtchn_fifo_control_block *control_block,
285 unsigned priority, unsigned long *ready,
288 struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
293 head = q->head[priority];
296 * Reached the tail last time? Read the new HEAD from the
300 virt_rmb(); /* Ensure word is up-to-date before reading head. */
301 head = control_block->head[priority];
305 word = event_word_from_port(port);
306 head = clear_linked(word);
309 * If the link is non-zero, there are more events in the
310 * queue, otherwise the queue is empty.
312 * If the queue is empty, clear this priority from our local
313 * copy of the ready word.
316 clear_bit(priority, ready);
318 if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port)) {
320 pr_warn("Dropping pending event for port %u\n", port);
322 handle_irq_for_port(port);
325 q->head[priority] = head;
328 static void __evtchn_fifo_handle_events(unsigned cpu, bool drop)
330 struct evtchn_fifo_control_block *control_block;
334 control_block = per_cpu(cpu_control_block, cpu);
336 ready = xchg(&control_block->ready, 0);
339 q = find_first_bit(&ready, EVTCHN_FIFO_MAX_QUEUES);
340 consume_one_event(cpu, control_block, q, &ready, drop);
341 ready |= xchg(&control_block->ready, 0);
345 static void evtchn_fifo_handle_events(unsigned cpu)
347 __evtchn_fifo_handle_events(cpu, false);
350 static void evtchn_fifo_resume(void)
354 for_each_possible_cpu(cpu) {
355 void *control_block = per_cpu(cpu_control_block, cpu);
362 * If this CPU is offline, take the opportunity to
363 * free the control block while it is not being
366 if (!cpu_online(cpu)) {
367 free_page((unsigned long)control_block);
368 per_cpu(cpu_control_block, cpu) = NULL;
372 ret = init_control_block(cpu, control_block);
378 * The event array starts out as empty again and is extended
379 * as normal when events are bound. The existing pages will
382 event_array_pages = 0;
385 static 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,
400 static int evtchn_fifo_alloc_control_block(unsigned cpu)
402 void *control_block = NULL;
405 control_block = (void *)__get_free_page(GFP_KERNEL);
406 if (control_block == NULL)
409 ret = init_control_block(cpu, control_block);
413 per_cpu(cpu_control_block, cpu) = control_block;
418 free_page((unsigned long)control_block);
422 static int evtchn_fifo_cpu_notification(struct notifier_block *self,
423 unsigned long action,
426 int cpu = (long)hcpu;
431 if (!per_cpu(cpu_control_block, cpu))
432 ret = evtchn_fifo_alloc_control_block(cpu);
435 __evtchn_fifo_handle_events(cpu, true);
440 return ret < 0 ? NOTIFY_BAD : NOTIFY_OK;
443 static struct notifier_block evtchn_fifo_cpu_notifier = {
444 .notifier_call = evtchn_fifo_cpu_notification,
447 int __init xen_evtchn_fifo_init(void)
452 ret = evtchn_fifo_alloc_control_block(cpu);
456 pr_info("Using FIFO-based ABI\n");
458 evtchn_ops = &evtchn_ops_fifo;
460 register_cpu_notifier(&evtchn_fifo_cpu_notifier);