David Vrabel | 1fe5655 | 2013-03-15 13:02:35 +0000 | [diff] [blame] | 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> |
| 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 | |
| 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 | #define BM(w) ((unsigned long *)(w)) |
| 70 | |
| 71 | static inline event_word_t *event_word_from_port(unsigned port) |
| 72 | { |
| 73 | unsigned i = port / EVENT_WORDS_PER_PAGE; |
| 74 | |
| 75 | return event_array[i] + port % EVENT_WORDS_PER_PAGE; |
| 76 | } |
| 77 | |
| 78 | static unsigned evtchn_fifo_max_channels(void) |
| 79 | { |
| 80 | return EVTCHN_FIFO_NR_CHANNELS; |
| 81 | } |
| 82 | |
| 83 | static unsigned evtchn_fifo_nr_channels(void) |
| 84 | { |
| 85 | return event_array_pages * EVENT_WORDS_PER_PAGE; |
| 86 | } |
| 87 | |
| 88 | static void free_unused_array_pages(void) |
| 89 | { |
| 90 | unsigned i; |
| 91 | |
| 92 | for (i = event_array_pages; i < MAX_EVENT_ARRAY_PAGES; i++) { |
| 93 | if (!event_array[i]) |
| 94 | break; |
| 95 | free_page((unsigned long)event_array[i]); |
| 96 | event_array[i] = NULL; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static void init_array_page(event_word_t *array_page) |
| 101 | { |
| 102 | unsigned i; |
| 103 | |
| 104 | for (i = 0; i < EVENT_WORDS_PER_PAGE; i++) |
| 105 | array_page[i] = 1 << EVTCHN_FIFO_MASKED; |
| 106 | } |
| 107 | |
| 108 | static int evtchn_fifo_setup(struct irq_info *info) |
| 109 | { |
| 110 | unsigned port = info->evtchn; |
| 111 | unsigned new_array_pages; |
Wei Yongjun | be1403b | 2014-01-07 21:11:25 +0800 | [diff] [blame] | 112 | int ret; |
David Vrabel | 1fe5655 | 2013-03-15 13:02:35 +0000 | [diff] [blame] | 113 | |
| 114 | new_array_pages = port / EVENT_WORDS_PER_PAGE + 1; |
| 115 | |
| 116 | if (new_array_pages > MAX_EVENT_ARRAY_PAGES) |
| 117 | return -EINVAL; |
| 118 | |
| 119 | while (event_array_pages < new_array_pages) { |
| 120 | void *array_page; |
| 121 | struct evtchn_expand_array expand_array; |
| 122 | |
| 123 | /* Might already have a page if we've resumed. */ |
| 124 | array_page = event_array[event_array_pages]; |
| 125 | if (!array_page) { |
| 126 | array_page = (void *)__get_free_page(GFP_KERNEL); |
Wei Yongjun | be1403b | 2014-01-07 21:11:25 +0800 | [diff] [blame] | 127 | if (array_page == NULL) { |
| 128 | ret = -ENOMEM; |
David Vrabel | 1fe5655 | 2013-03-15 13:02:35 +0000 | [diff] [blame] | 129 | goto error; |
Wei Yongjun | be1403b | 2014-01-07 21:11:25 +0800 | [diff] [blame] | 130 | } |
David Vrabel | 1fe5655 | 2013-03-15 13:02:35 +0000 | [diff] [blame] | 131 | event_array[event_array_pages] = array_page; |
| 132 | } |
| 133 | |
| 134 | /* Mask all events in this page before adding it. */ |
| 135 | init_array_page(array_page); |
| 136 | |
| 137 | expand_array.array_gfn = virt_to_mfn(array_page); |
| 138 | |
| 139 | ret = HYPERVISOR_event_channel_op(EVTCHNOP_expand_array, &expand_array); |
| 140 | if (ret < 0) |
| 141 | goto error; |
| 142 | |
| 143 | event_array_pages++; |
| 144 | } |
| 145 | return 0; |
| 146 | |
| 147 | error: |
| 148 | if (event_array_pages == 0) |
| 149 | panic("xen: unable to expand event array with initial page (%d)\n", ret); |
| 150 | else |
| 151 | pr_err("unable to expand event array (%d)\n", ret); |
| 152 | free_unused_array_pages(); |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | static void evtchn_fifo_bind_to_cpu(struct irq_info *info, unsigned cpu) |
| 157 | { |
| 158 | /* no-op */ |
| 159 | } |
| 160 | |
| 161 | static void evtchn_fifo_clear_pending(unsigned port) |
| 162 | { |
| 163 | event_word_t *word = event_word_from_port(port); |
| 164 | sync_clear_bit(EVTCHN_FIFO_PENDING, BM(word)); |
| 165 | } |
| 166 | |
| 167 | static void evtchn_fifo_set_pending(unsigned port) |
| 168 | { |
| 169 | event_word_t *word = event_word_from_port(port); |
| 170 | sync_set_bit(EVTCHN_FIFO_PENDING, BM(word)); |
| 171 | } |
| 172 | |
| 173 | static bool evtchn_fifo_is_pending(unsigned port) |
| 174 | { |
| 175 | event_word_t *word = event_word_from_port(port); |
| 176 | return sync_test_bit(EVTCHN_FIFO_PENDING, BM(word)); |
| 177 | } |
| 178 | |
| 179 | static bool evtchn_fifo_test_and_set_mask(unsigned port) |
| 180 | { |
| 181 | event_word_t *word = event_word_from_port(port); |
| 182 | return sync_test_and_set_bit(EVTCHN_FIFO_MASKED, BM(word)); |
| 183 | } |
| 184 | |
| 185 | static void evtchn_fifo_mask(unsigned port) |
| 186 | { |
| 187 | event_word_t *word = event_word_from_port(port); |
| 188 | sync_set_bit(EVTCHN_FIFO_MASKED, BM(word)); |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Clear MASKED, spinning if BUSY is set. |
| 193 | */ |
| 194 | static void clear_masked(volatile event_word_t *word) |
| 195 | { |
| 196 | event_word_t new, old, w; |
| 197 | |
| 198 | w = *word; |
| 199 | |
| 200 | do { |
| 201 | old = w & ~(1 << EVTCHN_FIFO_BUSY); |
| 202 | new = old & ~(1 << EVTCHN_FIFO_MASKED); |
| 203 | w = sync_cmpxchg(word, old, new); |
| 204 | } while (w != old); |
| 205 | } |
| 206 | |
| 207 | static void evtchn_fifo_unmask(unsigned port) |
| 208 | { |
| 209 | event_word_t *word = event_word_from_port(port); |
| 210 | |
| 211 | BUG_ON(!irqs_disabled()); |
| 212 | |
| 213 | clear_masked(word); |
| 214 | if (sync_test_bit(EVTCHN_FIFO_PENDING, BM(word))) { |
| 215 | struct evtchn_unmask unmask = { .port = port }; |
| 216 | (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | static uint32_t clear_linked(volatile event_word_t *word) |
| 221 | { |
| 222 | event_word_t new, old, w; |
| 223 | |
| 224 | w = *word; |
| 225 | |
| 226 | do { |
| 227 | old = w; |
| 228 | new = (w & ~((1 << EVTCHN_FIFO_LINKED) |
| 229 | | EVTCHN_FIFO_LINK_MASK)); |
| 230 | } while ((w = sync_cmpxchg(word, old, new)) != old); |
| 231 | |
| 232 | return w & EVTCHN_FIFO_LINK_MASK; |
| 233 | } |
| 234 | |
| 235 | static void handle_irq_for_port(unsigned port) |
| 236 | { |
| 237 | int irq; |
David Vrabel | 1fe5655 | 2013-03-15 13:02:35 +0000 | [diff] [blame] | 238 | |
| 239 | irq = get_evtchn_to_irq(port); |
Thomas Gleixner | 589d03e | 2014-02-23 21:40:18 +0000 | [diff] [blame] | 240 | if (irq != -1) |
| 241 | generic_handle_irq(irq); |
David Vrabel | 1fe5655 | 2013-03-15 13:02:35 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static void consume_one_event(unsigned cpu, |
| 245 | struct evtchn_fifo_control_block *control_block, |
| 246 | unsigned priority, uint32_t *ready) |
| 247 | { |
| 248 | struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu); |
| 249 | uint32_t head; |
| 250 | unsigned port; |
| 251 | event_word_t *word; |
| 252 | |
| 253 | head = q->head[priority]; |
| 254 | |
| 255 | /* |
| 256 | * Reached the tail last time? Read the new HEAD from the |
| 257 | * control block. |
| 258 | */ |
| 259 | if (head == 0) { |
| 260 | rmb(); /* Ensure word is up-to-date before reading head. */ |
| 261 | head = control_block->head[priority]; |
| 262 | } |
| 263 | |
| 264 | port = head; |
| 265 | word = event_word_from_port(port); |
| 266 | head = clear_linked(word); |
| 267 | |
| 268 | /* |
| 269 | * If the link is non-zero, there are more events in the |
| 270 | * queue, otherwise the queue is empty. |
| 271 | * |
| 272 | * If the queue is empty, clear this priority from our local |
| 273 | * copy of the ready word. |
| 274 | */ |
| 275 | if (head == 0) |
| 276 | clear_bit(priority, BM(ready)); |
| 277 | |
| 278 | if (sync_test_bit(EVTCHN_FIFO_PENDING, BM(word)) |
| 279 | && !sync_test_bit(EVTCHN_FIFO_MASKED, BM(word))) |
| 280 | handle_irq_for_port(port); |
| 281 | |
| 282 | q->head[priority] = head; |
| 283 | } |
| 284 | |
| 285 | static void evtchn_fifo_handle_events(unsigned cpu) |
| 286 | { |
| 287 | struct evtchn_fifo_control_block *control_block; |
| 288 | uint32_t ready; |
| 289 | unsigned q; |
| 290 | |
| 291 | control_block = per_cpu(cpu_control_block, cpu); |
| 292 | |
| 293 | ready = xchg(&control_block->ready, 0); |
| 294 | |
| 295 | while (ready) { |
| 296 | q = find_first_bit(BM(&ready), EVTCHN_FIFO_MAX_QUEUES); |
| 297 | consume_one_event(cpu, control_block, q, &ready); |
| 298 | ready |= xchg(&control_block->ready, 0); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static void evtchn_fifo_resume(void) |
| 303 | { |
| 304 | unsigned cpu; |
| 305 | |
| 306 | for_each_possible_cpu(cpu) { |
| 307 | void *control_block = per_cpu(cpu_control_block, cpu); |
| 308 | struct evtchn_init_control init_control; |
| 309 | int ret; |
| 310 | |
| 311 | if (!control_block) |
| 312 | continue; |
| 313 | |
| 314 | /* |
| 315 | * If this CPU is offline, take the opportunity to |
| 316 | * free the control block while it is not being |
| 317 | * used. |
| 318 | */ |
| 319 | if (!cpu_online(cpu)) { |
| 320 | free_page((unsigned long)control_block); |
| 321 | per_cpu(cpu_control_block, cpu) = NULL; |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | init_control.control_gfn = virt_to_mfn(control_block); |
| 326 | init_control.offset = 0; |
| 327 | init_control.vcpu = cpu; |
| 328 | |
| 329 | ret = HYPERVISOR_event_channel_op(EVTCHNOP_init_control, |
| 330 | &init_control); |
| 331 | if (ret < 0) |
| 332 | BUG(); |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * The event array starts out as empty again and is extended |
| 337 | * as normal when events are bound. The existing pages will |
| 338 | * be reused. |
| 339 | */ |
| 340 | event_array_pages = 0; |
| 341 | } |
| 342 | |
| 343 | static const struct evtchn_ops evtchn_ops_fifo = { |
| 344 | .max_channels = evtchn_fifo_max_channels, |
| 345 | .nr_channels = evtchn_fifo_nr_channels, |
| 346 | .setup = evtchn_fifo_setup, |
| 347 | .bind_to_cpu = evtchn_fifo_bind_to_cpu, |
| 348 | .clear_pending = evtchn_fifo_clear_pending, |
| 349 | .set_pending = evtchn_fifo_set_pending, |
| 350 | .is_pending = evtchn_fifo_is_pending, |
| 351 | .test_and_set_mask = evtchn_fifo_test_and_set_mask, |
| 352 | .mask = evtchn_fifo_mask, |
| 353 | .unmask = evtchn_fifo_unmask, |
| 354 | .handle_events = evtchn_fifo_handle_events, |
| 355 | .resume = evtchn_fifo_resume, |
| 356 | }; |
| 357 | |
Paul Gortmaker | 0db6991 | 2014-01-10 09:50:08 -0500 | [diff] [blame] | 358 | static int evtchn_fifo_init_control_block(unsigned cpu) |
David Vrabel | 1fe5655 | 2013-03-15 13:02:35 +0000 | [diff] [blame] | 359 | { |
| 360 | struct page *control_block = NULL; |
| 361 | struct evtchn_init_control init_control; |
| 362 | int ret = -ENOMEM; |
| 363 | |
| 364 | control_block = alloc_page(GFP_KERNEL|__GFP_ZERO); |
| 365 | if (control_block == NULL) |
| 366 | goto error; |
| 367 | |
| 368 | init_control.control_gfn = virt_to_mfn(page_address(control_block)); |
| 369 | init_control.offset = 0; |
| 370 | init_control.vcpu = cpu; |
| 371 | |
| 372 | ret = HYPERVISOR_event_channel_op(EVTCHNOP_init_control, &init_control); |
| 373 | if (ret < 0) |
| 374 | goto error; |
| 375 | |
| 376 | per_cpu(cpu_control_block, cpu) = page_address(control_block); |
| 377 | |
| 378 | return 0; |
| 379 | |
| 380 | error: |
| 381 | __free_page(control_block); |
| 382 | return ret; |
| 383 | } |
| 384 | |
Paul Gortmaker | 0db6991 | 2014-01-10 09:50:08 -0500 | [diff] [blame] | 385 | static int evtchn_fifo_cpu_notification(struct notifier_block *self, |
David Vrabel | 1fe5655 | 2013-03-15 13:02:35 +0000 | [diff] [blame] | 386 | unsigned long action, |
| 387 | void *hcpu) |
| 388 | { |
| 389 | int cpu = (long)hcpu; |
| 390 | int ret = 0; |
| 391 | |
| 392 | switch (action) { |
| 393 | case CPU_UP_PREPARE: |
| 394 | if (!per_cpu(cpu_control_block, cpu)) |
| 395 | ret = evtchn_fifo_init_control_block(cpu); |
| 396 | break; |
| 397 | default: |
| 398 | break; |
| 399 | } |
| 400 | return ret < 0 ? NOTIFY_BAD : NOTIFY_OK; |
| 401 | } |
| 402 | |
Paul Gortmaker | 0db6991 | 2014-01-10 09:50:08 -0500 | [diff] [blame] | 403 | static struct notifier_block evtchn_fifo_cpu_notifier = { |
David Vrabel | 1fe5655 | 2013-03-15 13:02:35 +0000 | [diff] [blame] | 404 | .notifier_call = evtchn_fifo_cpu_notification, |
| 405 | }; |
| 406 | |
| 407 | int __init xen_evtchn_fifo_init(void) |
| 408 | { |
| 409 | int cpu = get_cpu(); |
| 410 | int ret; |
| 411 | |
| 412 | ret = evtchn_fifo_init_control_block(cpu); |
| 413 | if (ret < 0) |
| 414 | goto out; |
| 415 | |
| 416 | pr_info("Using FIFO-based ABI\n"); |
| 417 | |
| 418 | evtchn_ops = &evtchn_ops_fifo; |
| 419 | |
| 420 | register_cpu_notifier(&evtchn_fifo_cpu_notifier); |
| 421 | out: |
| 422 | put_cpu(); |
| 423 | return ret; |
| 424 | } |