xen/events/fifo: Handle linked events when closing a port
[deliverable/linux.git] / drivers / xen / events / events_fifo.c
CommitLineData
1fe56551
DV
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/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>
1fe56551
DV
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>
a9fd60e2 53#include <xen/page.h>
1fe56551
DV
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
05a812ac 69/*
dcecb8fd 70 * sync_set_bit() and friends must be unsigned long aligned.
05a812ac 71 */
dcecb8fd 72#if BITS_PER_LONG > 32
05a812ac
VM
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
1fe56551 80#define BM(w) ((unsigned long *)(w))
05a812ac
VM
81#define EVTCHN_FIFO_BIT(b, w) EVTCHN_FIFO_ ##b
82
83#endif
1fe56551
DV
84
85static inline event_word_t *event_word_from_port(unsigned port)
86{
87 unsigned i = port / EVENT_WORDS_PER_PAGE;
88
89 return event_array[i] + port % EVENT_WORDS_PER_PAGE;
90}
91
92static unsigned evtchn_fifo_max_channels(void)
93{
94 return EVTCHN_FIFO_NR_CHANNELS;
95}
96
97static unsigned evtchn_fifo_nr_channels(void)
98{
99 return event_array_pages * EVENT_WORDS_PER_PAGE;
100}
101
c12784c3
DV
102static 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_mfn(control_block);
115 init_control.offset = 0;
116 init_control.vcpu = cpu;
117
118 return HYPERVISOR_event_channel_op(EVTCHNOP_init_control, &init_control);
119}
120
1fe56551
DV
121static 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
133static 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
141static int evtchn_fifo_setup(struct irq_info *info)
142{
143 unsigned port = info->evtchn;
144 unsigned new_array_pages;
be1403b9 145 int ret;
1fe56551
DV
146
147 new_array_pages = port / EVENT_WORDS_PER_PAGE + 1;
148
149 if (new_array_pages > MAX_EVENT_ARRAY_PAGES)
150 return -EINVAL;
151
152 while (event_array_pages < new_array_pages) {
153 void *array_page;
154 struct evtchn_expand_array expand_array;
155
156 /* Might already have a page if we've resumed. */
157 array_page = event_array[event_array_pages];
158 if (!array_page) {
159 array_page = (void *)__get_free_page(GFP_KERNEL);
be1403b9
WY
160 if (array_page == NULL) {
161 ret = -ENOMEM;
1fe56551 162 goto error;
be1403b9 163 }
1fe56551
DV
164 event_array[event_array_pages] = array_page;
165 }
166
167 /* Mask all events in this page before adding it. */
168 init_array_page(array_page);
169
170 expand_array.array_gfn = virt_to_mfn(array_page);
171
172 ret = HYPERVISOR_event_channel_op(EVTCHNOP_expand_array, &expand_array);
173 if (ret < 0)
174 goto error;
175
176 event_array_pages++;
177 }
178 return 0;
179
180 error:
181 if (event_array_pages == 0)
182 panic("xen: unable to expand event array with initial page (%d)\n", ret);
183 else
184 pr_err("unable to expand event array (%d)\n", ret);
185 free_unused_array_pages();
186 return ret;
187}
188
189static void evtchn_fifo_bind_to_cpu(struct irq_info *info, unsigned cpu)
190{
191 /* no-op */
192}
193
194static void evtchn_fifo_clear_pending(unsigned port)
195{
196 event_word_t *word = event_word_from_port(port);
05a812ac 197 sync_clear_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
1fe56551
DV
198}
199
200static void evtchn_fifo_set_pending(unsigned port)
201{
202 event_word_t *word = event_word_from_port(port);
05a812ac 203 sync_set_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
1fe56551
DV
204}
205
206static bool evtchn_fifo_is_pending(unsigned port)
207{
208 event_word_t *word = event_word_from_port(port);
05a812ac 209 return sync_test_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
1fe56551
DV
210}
211
212static bool evtchn_fifo_test_and_set_mask(unsigned port)
213{
214 event_word_t *word = event_word_from_port(port);
05a812ac 215 return sync_test_and_set_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
1fe56551
DV
216}
217
218static void evtchn_fifo_mask(unsigned port)
219{
220 event_word_t *word = event_word_from_port(port);
05a812ac 221 sync_set_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
1fe56551
DV
222}
223
05a812ac
VM
224static bool evtchn_fifo_is_masked(unsigned 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}
1fe56551
DV
229/*
230 * Clear MASKED, spinning if BUSY is set.
231 */
232static void clear_masked(volatile event_word_t *word)
233{
234 event_word_t new, old, w;
235
236 w = *word;
237
238 do {
239 old = w & ~(1 << EVTCHN_FIFO_BUSY);
240 new = old & ~(1 << EVTCHN_FIFO_MASKED);
241 w = sync_cmpxchg(word, old, new);
242 } while (w != old);
243}
244
245static void evtchn_fifo_unmask(unsigned port)
246{
247 event_word_t *word = event_word_from_port(port);
248
249 BUG_ON(!irqs_disabled());
250
251 clear_masked(word);
05a812ac 252 if (evtchn_fifo_is_pending(port)) {
1fe56551
DV
253 struct evtchn_unmask unmask = { .port = port };
254 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
255 }
256}
257
fcdf31a7
RL
258static bool evtchn_fifo_is_linked(unsigned port)
259{
260 event_word_t *word = event_word_from_port(port);
261 return sync_test_bit(EVTCHN_FIFO_BIT(LINKED, word), BM(word));
262}
263
1fe56551
DV
264static uint32_t clear_linked(volatile event_word_t *word)
265{
266 event_word_t new, old, w;
267
268 w = *word;
269
270 do {
271 old = w;
272 new = (w & ~((1 << EVTCHN_FIFO_LINKED)
273 | EVTCHN_FIFO_LINK_MASK));
274 } while ((w = sync_cmpxchg(word, old, new)) != old);
275
276 return w & EVTCHN_FIFO_LINK_MASK;
277}
278
279static void handle_irq_for_port(unsigned port)
280{
281 int irq;
1fe56551
DV
282
283 irq = get_evtchn_to_irq(port);
589d03e9
TG
284 if (irq != -1)
285 generic_handle_irq(irq);
1fe56551
DV
286}
287
288static void consume_one_event(unsigned cpu,
289 struct evtchn_fifo_control_block *control_block,
fcdf31a7
RL
290 unsigned priority, unsigned long *ready,
291 bool drop)
1fe56551
DV
292{
293 struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
294 uint32_t head;
295 unsigned port;
296 event_word_t *word;
297
298 head = q->head[priority];
299
300 /*
301 * Reached the tail last time? Read the new HEAD from the
302 * control block.
303 */
304 if (head == 0) {
305 rmb(); /* Ensure word is up-to-date before reading head. */
306 head = control_block->head[priority];
307 }
308
309 port = head;
310 word = event_word_from_port(port);
311 head = clear_linked(word);
312
313 /*
314 * If the link is non-zero, there are more events in the
315 * queue, otherwise the queue is empty.
316 *
317 * If the queue is empty, clear this priority from our local
318 * copy of the ready word.
319 */
320 if (head == 0)
05a812ac 321 clear_bit(priority, ready);
1fe56551 322
fcdf31a7
RL
323 if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port)) {
324 if (likely(!drop))
325 handle_irq_for_port(port);
326 }
1fe56551
DV
327
328 q->head[priority] = head;
329}
330
fcdf31a7 331static void __evtchn_fifo_handle_events(unsigned cpu, bool drop)
1fe56551
DV
332{
333 struct evtchn_fifo_control_block *control_block;
05a812ac 334 unsigned long ready;
1fe56551
DV
335 unsigned q;
336
337 control_block = per_cpu(cpu_control_block, cpu);
338
339 ready = xchg(&control_block->ready, 0);
340
341 while (ready) {
e4a74312 342 q = find_first_bit(&ready, EVTCHN_FIFO_MAX_QUEUES);
fcdf31a7 343 consume_one_event(cpu, control_block, q, &ready, drop);
1fe56551
DV
344 ready |= xchg(&control_block->ready, 0);
345 }
346}
347
fcdf31a7
RL
348static void evtchn_fifo_handle_events(unsigned cpu)
349{
350 __evtchn_fifo_handle_events(cpu, false);
351}
352
1fe56551
DV
353static void evtchn_fifo_resume(void)
354{
355 unsigned cpu;
356
357 for_each_possible_cpu(cpu) {
358 void *control_block = per_cpu(cpu_control_block, cpu);
1fe56551
DV
359 int ret;
360
361 if (!control_block)
362 continue;
363
364 /*
365 * If this CPU is offline, take the opportunity to
366 * free the control block while it is not being
367 * used.
368 */
369 if (!cpu_online(cpu)) {
370 free_page((unsigned long)control_block);
371 per_cpu(cpu_control_block, cpu) = NULL;
372 continue;
373 }
374
c12784c3 375 ret = init_control_block(cpu, control_block);
1fe56551
DV
376 if (ret < 0)
377 BUG();
378 }
379
380 /*
381 * The event array starts out as empty again and is extended
382 * as normal when events are bound. The existing pages will
383 * be reused.
384 */
385 event_array_pages = 0;
386}
387
fcdf31a7
RL
388static void evtchn_fifo_close(unsigned port, unsigned int cpu)
389{
390 if (cpu == NR_CPUS)
391 return;
392
393 get_online_cpus();
394 if (cpu_online(cpu)) {
395 if (WARN_ON(irqs_disabled()))
396 goto out;
397
398 while (evtchn_fifo_is_linked(port))
399 cpu_relax();
400 } else {
401 __evtchn_fifo_handle_events(cpu, true);
402 }
403
404out:
405 put_online_cpus();
406}
407
1fe56551
DV
408static const struct evtchn_ops evtchn_ops_fifo = {
409 .max_channels = evtchn_fifo_max_channels,
410 .nr_channels = evtchn_fifo_nr_channels,
411 .setup = evtchn_fifo_setup,
412 .bind_to_cpu = evtchn_fifo_bind_to_cpu,
413 .clear_pending = evtchn_fifo_clear_pending,
414 .set_pending = evtchn_fifo_set_pending,
415 .is_pending = evtchn_fifo_is_pending,
416 .test_and_set_mask = evtchn_fifo_test_and_set_mask,
417 .mask = evtchn_fifo_mask,
418 .unmask = evtchn_fifo_unmask,
419 .handle_events = evtchn_fifo_handle_events,
420 .resume = evtchn_fifo_resume,
fcdf31a7 421 .close = evtchn_fifo_close,
1fe56551
DV
422};
423
c12784c3 424static int evtchn_fifo_alloc_control_block(unsigned cpu)
1fe56551 425{
c12784c3 426 void *control_block = NULL;
1fe56551
DV
427 int ret = -ENOMEM;
428
c12784c3 429 control_block = (void *)__get_free_page(GFP_KERNEL);
1fe56551
DV
430 if (control_block == NULL)
431 goto error;
432
c12784c3 433 ret = init_control_block(cpu, control_block);
1fe56551
DV
434 if (ret < 0)
435 goto error;
436
c12784c3 437 per_cpu(cpu_control_block, cpu) = control_block;
1fe56551
DV
438
439 return 0;
440
441 error:
c12784c3 442 free_page((unsigned long)control_block);
1fe56551
DV
443 return ret;
444}
445
0db6991d 446static int evtchn_fifo_cpu_notification(struct notifier_block *self,
1fe56551
DV
447 unsigned long action,
448 void *hcpu)
449{
450 int cpu = (long)hcpu;
451 int ret = 0;
452
453 switch (action) {
454 case CPU_UP_PREPARE:
455 if (!per_cpu(cpu_control_block, cpu))
c12784c3 456 ret = evtchn_fifo_alloc_control_block(cpu);
1fe56551
DV
457 break;
458 default:
459 break;
460 }
461 return ret < 0 ? NOTIFY_BAD : NOTIFY_OK;
462}
463
0db6991d 464static struct notifier_block evtchn_fifo_cpu_notifier = {
1fe56551
DV
465 .notifier_call = evtchn_fifo_cpu_notification,
466};
467
468int __init xen_evtchn_fifo_init(void)
469{
470 int cpu = get_cpu();
471 int ret;
472
c12784c3 473 ret = evtchn_fifo_alloc_control_block(cpu);
1fe56551
DV
474 if (ret < 0)
475 goto out;
476
477 pr_info("Using FIFO-based ABI\n");
478
479 evtchn_ops = &evtchn_ops_fifo;
480
481 register_cpu_notifier(&evtchn_fifo_cpu_notifier);
482out:
483 put_cpu();
484 return ret;
485}
This page took 0.108146 seconds and 5 git commands to generate.