blob: 09e38b92ae503036535102f8f7353b4c83af6f53 [file] [log] [blame]
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001/*
2 * VFIO PCI interrupt handling
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Derived from original vfio:
12 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
13 * Author: Tom Lyon, pugs@cisco.com
14 */
15
16#include <linux/device.h>
17#include <linux/interrupt.h>
18#include <linux/eventfd.h>
Gavin Shanb8f02af2014-09-29 10:16:24 -060019#include <linux/msi.h>
Alex Williamson89e1f7d2012-07-31 08:16:24 -060020#include <linux/pci.h>
21#include <linux/file.h>
22#include <linux/poll.h>
23#include <linux/vfio.h>
24#include <linux/wait.h>
25#include <linux/workqueue.h>
Arnd Bergmann25e97892013-03-15 12:58:20 -060026#include <linux/slab.h>
Alex Williamson89e1f7d2012-07-31 08:16:24 -060027
28#include "vfio_pci_private.h"
29
30/*
31 * IRQfd - generic
32 */
33struct virqfd {
34 struct vfio_pci_device *vdev;
35 struct eventfd_ctx *eventfd;
36 int (*handler)(struct vfio_pci_device *, void *);
37 void (*thread)(struct vfio_pci_device *, void *);
38 void *data;
39 struct work_struct inject;
40 wait_queue_t wait;
41 poll_table pt;
42 struct work_struct shutdown;
43 struct virqfd **pvirqfd;
44};
45
46static struct workqueue_struct *vfio_irqfd_cleanup_wq;
47
48int __init vfio_pci_virqfd_init(void)
49{
50 vfio_irqfd_cleanup_wq =
51 create_singlethread_workqueue("vfio-irqfd-cleanup");
52 if (!vfio_irqfd_cleanup_wq)
53 return -ENOMEM;
54
55 return 0;
56}
57
58void vfio_pci_virqfd_exit(void)
59{
60 destroy_workqueue(vfio_irqfd_cleanup_wq);
61}
62
63static void virqfd_deactivate(struct virqfd *virqfd)
64{
65 queue_work(vfio_irqfd_cleanup_wq, &virqfd->shutdown);
66}
67
68static int virqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
69{
70 struct virqfd *virqfd = container_of(wait, struct virqfd, wait);
71 unsigned long flags = (unsigned long)key;
72
73 if (flags & POLLIN) {
74 /* An event has been signaled, call function */
75 if ((!virqfd->handler ||
76 virqfd->handler(virqfd->vdev, virqfd->data)) &&
77 virqfd->thread)
78 schedule_work(&virqfd->inject);
79 }
80
Alex Williamsonb68e7fa2012-09-21 10:48:28 -060081 if (flags & POLLHUP) {
82 unsigned long flags;
83 spin_lock_irqsave(&virqfd->vdev->irqlock, flags);
84
85 /*
86 * The eventfd is closing, if the virqfd has not yet been
87 * queued for release, as determined by testing whether the
88 * vdev pointer to it is still valid, queue it now. As
89 * with kvm irqfds, we know we won't race against the virqfd
90 * going away because we hold wqh->lock to get here.
91 */
92 if (*(virqfd->pvirqfd) == virqfd) {
93 *(virqfd->pvirqfd) = NULL;
94 virqfd_deactivate(virqfd);
95 }
96
97 spin_unlock_irqrestore(&virqfd->vdev->irqlock, flags);
98 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -060099
100 return 0;
101}
102
103static void virqfd_ptable_queue_proc(struct file *file,
104 wait_queue_head_t *wqh, poll_table *pt)
105{
106 struct virqfd *virqfd = container_of(pt, struct virqfd, pt);
107 add_wait_queue(wqh, &virqfd->wait);
108}
109
110static void virqfd_shutdown(struct work_struct *work)
111{
112 struct virqfd *virqfd = container_of(work, struct virqfd, shutdown);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600113 u64 cnt;
114
115 eventfd_ctx_remove_wait_queue(virqfd->eventfd, &virqfd->wait, &cnt);
116 flush_work(&virqfd->inject);
117 eventfd_ctx_put(virqfd->eventfd);
118
119 kfree(virqfd);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600120}
121
122static void virqfd_inject(struct work_struct *work)
123{
124 struct virqfd *virqfd = container_of(work, struct virqfd, inject);
125 if (virqfd->thread)
126 virqfd->thread(virqfd->vdev, virqfd->data);
127}
128
Antonios Motakisbdc5e102015-03-16 14:08:51 -0600129int vfio_virqfd_enable(struct vfio_pci_device *vdev,
130 int (*handler)(struct vfio_pci_device *, void *),
131 void (*thread)(struct vfio_pci_device *, void *),
132 void *data, struct virqfd **pvirqfd, int fd)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600133{
Alex Williamson20e77452013-08-28 09:49:55 -0600134 struct fd irqfd;
135 struct eventfd_ctx *ctx;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600136 struct virqfd *virqfd;
137 int ret = 0;
138 unsigned int events;
139
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600140 virqfd = kzalloc(sizeof(*virqfd), GFP_KERNEL);
141 if (!virqfd)
142 return -ENOMEM;
143
144 virqfd->pvirqfd = pvirqfd;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600145 virqfd->vdev = vdev;
146 virqfd->handler = handler;
147 virqfd->thread = thread;
148 virqfd->data = data;
149
150 INIT_WORK(&virqfd->shutdown, virqfd_shutdown);
151 INIT_WORK(&virqfd->inject, virqfd_inject);
152
Alex Williamson20e77452013-08-28 09:49:55 -0600153 irqfd = fdget(fd);
154 if (!irqfd.file) {
155 ret = -EBADF;
156 goto err_fd;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600157 }
158
Alex Williamson20e77452013-08-28 09:49:55 -0600159 ctx = eventfd_ctx_fileget(irqfd.file);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600160 if (IS_ERR(ctx)) {
161 ret = PTR_ERR(ctx);
Alex Williamson20e77452013-08-28 09:49:55 -0600162 goto err_ctx;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600163 }
164
165 virqfd->eventfd = ctx;
166
167 /*
Alex Williamsonb68e7fa2012-09-21 10:48:28 -0600168 * virqfds can be released by closing the eventfd or directly
169 * through ioctl. These are both done through a workqueue, so
170 * we update the pointer to the virqfd under lock to avoid
171 * pushing multiple jobs to release the same virqfd.
172 */
173 spin_lock_irq(&vdev->irqlock);
174
175 if (*pvirqfd) {
176 spin_unlock_irq(&vdev->irqlock);
177 ret = -EBUSY;
Alex Williamson20e77452013-08-28 09:49:55 -0600178 goto err_busy;
Alex Williamsonb68e7fa2012-09-21 10:48:28 -0600179 }
180 *pvirqfd = virqfd;
181
182 spin_unlock_irq(&vdev->irqlock);
183
184 /*
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600185 * Install our own custom wake-up handling so we are notified via
186 * a callback whenever someone signals the underlying eventfd.
187 */
188 init_waitqueue_func_entry(&virqfd->wait, virqfd_wakeup);
189 init_poll_funcptr(&virqfd->pt, virqfd_ptable_queue_proc);
190
Alex Williamson20e77452013-08-28 09:49:55 -0600191 events = irqfd.file->f_op->poll(irqfd.file, &virqfd->pt);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600192
193 /*
194 * Check if there was an event already pending on the eventfd
195 * before we registered and trigger it as if we didn't miss it.
196 */
197 if (events & POLLIN) {
198 if ((!handler || handler(vdev, data)) && thread)
199 schedule_work(&virqfd->inject);
200 }
201
202 /*
203 * Do not drop the file until the irqfd is fully initialized,
204 * otherwise we might race against the POLLHUP.
205 */
Alex Williamson20e77452013-08-28 09:49:55 -0600206 fdput(irqfd);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600207
208 return 0;
Alex Williamson20e77452013-08-28 09:49:55 -0600209err_busy:
210 eventfd_ctx_put(ctx);
211err_ctx:
212 fdput(irqfd);
213err_fd:
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600214 kfree(virqfd);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600215
216 return ret;
217}
Antonios Motakisbdc5e102015-03-16 14:08:51 -0600218EXPORT_SYMBOL_GPL(vfio_virqfd_enable);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600219
Antonios Motakisbdc5e102015-03-16 14:08:51 -0600220void vfio_virqfd_disable(struct vfio_pci_device *vdev, struct virqfd **pvirqfd)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600221{
Alex Williamsonb68e7fa2012-09-21 10:48:28 -0600222 unsigned long flags;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600223
Alex Williamsonb68e7fa2012-09-21 10:48:28 -0600224 spin_lock_irqsave(&vdev->irqlock, flags);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600225
Alex Williamsonb68e7fa2012-09-21 10:48:28 -0600226 if (*pvirqfd) {
227 virqfd_deactivate(*pvirqfd);
228 *pvirqfd = NULL;
229 }
230
231 spin_unlock_irqrestore(&vdev->irqlock, flags);
232
233 /*
234 * Block until we know all outstanding shutdown jobs have completed.
235 * Even if we don't queue the job, flush the wq to be sure it's
236 * been released.
237 */
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600238 flush_workqueue(vfio_irqfd_cleanup_wq);
239}
Antonios Motakisbdc5e102015-03-16 14:08:51 -0600240EXPORT_SYMBOL_GPL(vfio_virqfd_disable);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600241
242/*
243 * INTx
244 */
245static void vfio_send_intx_eventfd(struct vfio_pci_device *vdev, void *unused)
246{
247 if (likely(is_intx(vdev) && !vdev->virq_disabled))
248 eventfd_signal(vdev->ctx[0].trigger, 1);
249}
250
251void vfio_pci_intx_mask(struct vfio_pci_device *vdev)
252{
253 struct pci_dev *pdev = vdev->pdev;
254 unsigned long flags;
255
256 spin_lock_irqsave(&vdev->irqlock, flags);
257
258 /*
259 * Masking can come from interrupt, ioctl, or config space
260 * via INTx disable. The latter means this can get called
261 * even when not using intx delivery. In this case, just
262 * try to have the physical bit follow the virtual bit.
263 */
264 if (unlikely(!is_intx(vdev))) {
265 if (vdev->pci_2_3)
266 pci_intx(pdev, 0);
267 } else if (!vdev->ctx[0].masked) {
268 /*
269 * Can't use check_and_mask here because we always want to
270 * mask, not just when something is pending.
271 */
272 if (vdev->pci_2_3)
273 pci_intx(pdev, 0);
274 else
275 disable_irq_nosync(pdev->irq);
276
277 vdev->ctx[0].masked = true;
278 }
279
280 spin_unlock_irqrestore(&vdev->irqlock, flags);
281}
282
283/*
284 * If this is triggered by an eventfd, we can't call eventfd_signal
285 * or else we'll deadlock on the eventfd wait queue. Return >0 when
286 * a signal is necessary, which can then be handled via a work queue
287 * or directly depending on the caller.
288 */
Wei Yongjun0bced2f2013-03-25 11:15:44 -0600289static int vfio_pci_intx_unmask_handler(struct vfio_pci_device *vdev,
290 void *unused)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600291{
292 struct pci_dev *pdev = vdev->pdev;
293 unsigned long flags;
294 int ret = 0;
295
296 spin_lock_irqsave(&vdev->irqlock, flags);
297
298 /*
299 * Unmasking comes from ioctl or config, so again, have the
300 * physical bit follow the virtual even when not using INTx.
301 */
302 if (unlikely(!is_intx(vdev))) {
303 if (vdev->pci_2_3)
304 pci_intx(pdev, 1);
305 } else if (vdev->ctx[0].masked && !vdev->virq_disabled) {
306 /*
307 * A pending interrupt here would immediately trigger,
308 * but we can avoid that overhead by just re-sending
309 * the interrupt to the user.
310 */
311 if (vdev->pci_2_3) {
312 if (!pci_check_and_unmask_intx(pdev))
313 ret = 1;
314 } else
315 enable_irq(pdev->irq);
316
317 vdev->ctx[0].masked = (ret > 0);
318 }
319
320 spin_unlock_irqrestore(&vdev->irqlock, flags);
321
322 return ret;
323}
324
325void vfio_pci_intx_unmask(struct vfio_pci_device *vdev)
326{
327 if (vfio_pci_intx_unmask_handler(vdev, NULL) > 0)
328 vfio_send_intx_eventfd(vdev, NULL);
329}
330
331static irqreturn_t vfio_intx_handler(int irq, void *dev_id)
332{
333 struct vfio_pci_device *vdev = dev_id;
334 unsigned long flags;
335 int ret = IRQ_NONE;
336
337 spin_lock_irqsave(&vdev->irqlock, flags);
338
339 if (!vdev->pci_2_3) {
340 disable_irq_nosync(vdev->pdev->irq);
341 vdev->ctx[0].masked = true;
342 ret = IRQ_HANDLED;
343 } else if (!vdev->ctx[0].masked && /* may be shared */
344 pci_check_and_mask_intx(vdev->pdev)) {
345 vdev->ctx[0].masked = true;
346 ret = IRQ_HANDLED;
347 }
348
349 spin_unlock_irqrestore(&vdev->irqlock, flags);
350
351 if (ret == IRQ_HANDLED)
352 vfio_send_intx_eventfd(vdev, NULL);
353
354 return ret;
355}
356
357static int vfio_intx_enable(struct vfio_pci_device *vdev)
358{
359 if (!is_irq_none(vdev))
360 return -EINVAL;
361
362 if (!vdev->pdev->irq)
363 return -ENODEV;
364
365 vdev->ctx = kzalloc(sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
366 if (!vdev->ctx)
367 return -ENOMEM;
368
369 vdev->num_ctx = 1;
Alex Williamson899649b2012-10-10 09:10:32 -0600370
371 /*
372 * If the virtual interrupt is masked, restore it. Devices
373 * supporting DisINTx can be masked at the hardware level
374 * here, non-PCI-2.3 devices will have to wait until the
375 * interrupt is enabled.
376 */
377 vdev->ctx[0].masked = vdev->virq_disabled;
378 if (vdev->pci_2_3)
379 pci_intx(vdev->pdev, !vdev->ctx[0].masked);
380
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600381 vdev->irq_type = VFIO_PCI_INTX_IRQ_INDEX;
382
383 return 0;
384}
385
386static int vfio_intx_set_signal(struct vfio_pci_device *vdev, int fd)
387{
388 struct pci_dev *pdev = vdev->pdev;
389 unsigned long irqflags = IRQF_SHARED;
390 struct eventfd_ctx *trigger;
391 unsigned long flags;
392 int ret;
393
394 if (vdev->ctx[0].trigger) {
395 free_irq(pdev->irq, vdev);
396 kfree(vdev->ctx[0].name);
397 eventfd_ctx_put(vdev->ctx[0].trigger);
398 vdev->ctx[0].trigger = NULL;
399 }
400
401 if (fd < 0) /* Disable only */
402 return 0;
403
404 vdev->ctx[0].name = kasprintf(GFP_KERNEL, "vfio-intx(%s)",
405 pci_name(pdev));
406 if (!vdev->ctx[0].name)
407 return -ENOMEM;
408
409 trigger = eventfd_ctx_fdget(fd);
410 if (IS_ERR(trigger)) {
411 kfree(vdev->ctx[0].name);
412 return PTR_ERR(trigger);
413 }
414
Alex Williamson9dbdfd22012-10-10 09:10:32 -0600415 vdev->ctx[0].trigger = trigger;
416
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600417 if (!vdev->pci_2_3)
418 irqflags = 0;
419
420 ret = request_irq(pdev->irq, vfio_intx_handler,
421 irqflags, vdev->ctx[0].name, vdev);
422 if (ret) {
Alex Williamson9dbdfd22012-10-10 09:10:32 -0600423 vdev->ctx[0].trigger = NULL;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600424 kfree(vdev->ctx[0].name);
425 eventfd_ctx_put(trigger);
426 return ret;
427 }
428
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600429 /*
430 * INTx disable will stick across the new irq setup,
431 * disable_irq won't.
432 */
433 spin_lock_irqsave(&vdev->irqlock, flags);
Alex Williamson899649b2012-10-10 09:10:32 -0600434 if (!vdev->pci_2_3 && vdev->ctx[0].masked)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600435 disable_irq_nosync(pdev->irq);
436 spin_unlock_irqrestore(&vdev->irqlock, flags);
437
438 return 0;
439}
440
441static void vfio_intx_disable(struct vfio_pci_device *vdev)
442{
443 vfio_intx_set_signal(vdev, -1);
Antonios Motakisbdc5e102015-03-16 14:08:51 -0600444 vfio_virqfd_disable(vdev, &vdev->ctx[0].unmask);
445 vfio_virqfd_disable(vdev, &vdev->ctx[0].mask);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600446 vdev->irq_type = VFIO_PCI_NUM_IRQS;
447 vdev->num_ctx = 0;
448 kfree(vdev->ctx);
449}
450
451/*
452 * MSI/MSI-X
453 */
454static irqreturn_t vfio_msihandler(int irq, void *arg)
455{
456 struct eventfd_ctx *trigger = arg;
457
458 eventfd_signal(trigger, 1);
459 return IRQ_HANDLED;
460}
461
462static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)
463{
464 struct pci_dev *pdev = vdev->pdev;
465 int ret;
466
467 if (!is_irq_none(vdev))
468 return -EINVAL;
469
470 vdev->ctx = kzalloc(nvec * sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
471 if (!vdev->ctx)
472 return -ENOMEM;
473
474 if (msix) {
475 int i;
476
477 vdev->msix = kzalloc(nvec * sizeof(struct msix_entry),
478 GFP_KERNEL);
479 if (!vdev->msix) {
480 kfree(vdev->ctx);
481 return -ENOMEM;
482 }
483
484 for (i = 0; i < nvec; i++)
485 vdev->msix[i].entry = i;
486
Alexander Gordeev94cccde2014-01-17 17:02:21 +0100487 ret = pci_enable_msix_range(pdev, vdev->msix, 1, nvec);
488 if (ret < nvec) {
489 if (ret > 0)
490 pci_disable_msix(pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600491 kfree(vdev->msix);
492 kfree(vdev->ctx);
493 return ret;
494 }
495 } else {
Alexander Gordeev94cccde2014-01-17 17:02:21 +0100496 ret = pci_enable_msi_range(pdev, 1, nvec);
497 if (ret < nvec) {
498 if (ret > 0)
499 pci_disable_msi(pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600500 kfree(vdev->ctx);
501 return ret;
502 }
503 }
504
505 vdev->num_ctx = nvec;
506 vdev->irq_type = msix ? VFIO_PCI_MSIX_IRQ_INDEX :
507 VFIO_PCI_MSI_IRQ_INDEX;
508
509 if (!msix) {
510 /*
511 * Compute the virtual hardware field for max msi vectors -
512 * it is the log base 2 of the number of vectors.
513 */
514 vdev->msi_qmax = fls(nvec * 2 - 1) - 1;
515 }
516
517 return 0;
518}
519
520static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
521 int vector, int fd, bool msix)
522{
523 struct pci_dev *pdev = vdev->pdev;
524 int irq = msix ? vdev->msix[vector].vector : pdev->irq + vector;
525 char *name = msix ? "vfio-msix" : "vfio-msi";
526 struct eventfd_ctx *trigger;
527 int ret;
528
529 if (vector >= vdev->num_ctx)
530 return -EINVAL;
531
532 if (vdev->ctx[vector].trigger) {
533 free_irq(irq, vdev->ctx[vector].trigger);
534 kfree(vdev->ctx[vector].name);
535 eventfd_ctx_put(vdev->ctx[vector].trigger);
536 vdev->ctx[vector].trigger = NULL;
537 }
538
539 if (fd < 0)
540 return 0;
541
542 vdev->ctx[vector].name = kasprintf(GFP_KERNEL, "%s[%d](%s)",
543 name, vector, pci_name(pdev));
544 if (!vdev->ctx[vector].name)
545 return -ENOMEM;
546
547 trigger = eventfd_ctx_fdget(fd);
548 if (IS_ERR(trigger)) {
549 kfree(vdev->ctx[vector].name);
550 return PTR_ERR(trigger);
551 }
552
Gavin Shanb8f02af2014-09-29 10:16:24 -0600553 /*
554 * The MSIx vector table resides in device memory which may be cleared
555 * via backdoor resets. We don't allow direct access to the vector
556 * table so even if a userspace driver attempts to save/restore around
557 * such a reset it would be unsuccessful. To avoid this, restore the
558 * cached value of the message prior to enabling.
559 */
560 if (msix) {
561 struct msi_msg msg;
562
563 get_cached_msi_msg(irq, &msg);
Jiang Liu83a18912014-11-09 23:10:34 +0800564 pci_write_msi_msg(irq, &msg);
Gavin Shanb8f02af2014-09-29 10:16:24 -0600565 }
566
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600567 ret = request_irq(irq, vfio_msihandler, 0,
568 vdev->ctx[vector].name, trigger);
569 if (ret) {
570 kfree(vdev->ctx[vector].name);
571 eventfd_ctx_put(trigger);
572 return ret;
573 }
574
575 vdev->ctx[vector].trigger = trigger;
576
577 return 0;
578}
579
580static int vfio_msi_set_block(struct vfio_pci_device *vdev, unsigned start,
581 unsigned count, int32_t *fds, bool msix)
582{
583 int i, j, ret = 0;
584
585 if (start + count > vdev->num_ctx)
586 return -EINVAL;
587
588 for (i = 0, j = start; i < count && !ret; i++, j++) {
589 int fd = fds ? fds[i] : -1;
590 ret = vfio_msi_set_vector_signal(vdev, j, fd, msix);
591 }
592
593 if (ret) {
594 for (--j; j >= start; j--)
595 vfio_msi_set_vector_signal(vdev, j, -1, msix);
596 }
597
598 return ret;
599}
600
601static void vfio_msi_disable(struct vfio_pci_device *vdev, bool msix)
602{
603 struct pci_dev *pdev = vdev->pdev;
604 int i;
605
606 vfio_msi_set_block(vdev, 0, vdev->num_ctx, NULL, msix);
607
608 for (i = 0; i < vdev->num_ctx; i++) {
Antonios Motakisbdc5e102015-03-16 14:08:51 -0600609 vfio_virqfd_disable(vdev, &vdev->ctx[i].unmask);
610 vfio_virqfd_disable(vdev, &vdev->ctx[i].mask);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600611 }
612
613 if (msix) {
614 pci_disable_msix(vdev->pdev);
615 kfree(vdev->msix);
616 } else
617 pci_disable_msi(pdev);
618
619 vdev->irq_type = VFIO_PCI_NUM_IRQS;
620 vdev->num_ctx = 0;
621 kfree(vdev->ctx);
622}
623
624/*
625 * IOCTL support
626 */
627static int vfio_pci_set_intx_unmask(struct vfio_pci_device *vdev,
628 unsigned index, unsigned start,
629 unsigned count, uint32_t flags, void *data)
630{
631 if (!is_intx(vdev) || start != 0 || count != 1)
632 return -EINVAL;
633
634 if (flags & VFIO_IRQ_SET_DATA_NONE) {
635 vfio_pci_intx_unmask(vdev);
636 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
637 uint8_t unmask = *(uint8_t *)data;
638 if (unmask)
639 vfio_pci_intx_unmask(vdev);
640 } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
641 int32_t fd = *(int32_t *)data;
642 if (fd >= 0)
Antonios Motakisbdc5e102015-03-16 14:08:51 -0600643 return vfio_virqfd_enable(vdev,
644 vfio_pci_intx_unmask_handler,
645 vfio_send_intx_eventfd, NULL,
646 &vdev->ctx[0].unmask, fd);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600647
Antonios Motakisbdc5e102015-03-16 14:08:51 -0600648 vfio_virqfd_disable(vdev, &vdev->ctx[0].unmask);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600649 }
650
651 return 0;
652}
653
654static int vfio_pci_set_intx_mask(struct vfio_pci_device *vdev,
655 unsigned index, unsigned start,
656 unsigned count, uint32_t flags, void *data)
657{
658 if (!is_intx(vdev) || start != 0 || count != 1)
659 return -EINVAL;
660
661 if (flags & VFIO_IRQ_SET_DATA_NONE) {
662 vfio_pci_intx_mask(vdev);
663 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
664 uint8_t mask = *(uint8_t *)data;
665 if (mask)
666 vfio_pci_intx_mask(vdev);
667 } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
668 return -ENOTTY; /* XXX implement me */
669 }
670
671 return 0;
672}
673
674static int vfio_pci_set_intx_trigger(struct vfio_pci_device *vdev,
675 unsigned index, unsigned start,
676 unsigned count, uint32_t flags, void *data)
677{
678 if (is_intx(vdev) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
679 vfio_intx_disable(vdev);
680 return 0;
681 }
682
683 if (!(is_intx(vdev) || is_irq_none(vdev)) || start != 0 || count != 1)
684 return -EINVAL;
685
686 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
687 int32_t fd = *(int32_t *)data;
688 int ret;
689
690 if (is_intx(vdev))
691 return vfio_intx_set_signal(vdev, fd);
692
693 ret = vfio_intx_enable(vdev);
694 if (ret)
695 return ret;
696
697 ret = vfio_intx_set_signal(vdev, fd);
698 if (ret)
699 vfio_intx_disable(vdev);
700
701 return ret;
702 }
703
704 if (!is_intx(vdev))
705 return -EINVAL;
706
707 if (flags & VFIO_IRQ_SET_DATA_NONE) {
708 vfio_send_intx_eventfd(vdev, NULL);
709 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
710 uint8_t trigger = *(uint8_t *)data;
711 if (trigger)
712 vfio_send_intx_eventfd(vdev, NULL);
713 }
714 return 0;
715}
716
717static int vfio_pci_set_msi_trigger(struct vfio_pci_device *vdev,
718 unsigned index, unsigned start,
719 unsigned count, uint32_t flags, void *data)
720{
721 int i;
722 bool msix = (index == VFIO_PCI_MSIX_IRQ_INDEX) ? true : false;
723
724 if (irq_is(vdev, index) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
725 vfio_msi_disable(vdev, msix);
726 return 0;
727 }
728
729 if (!(irq_is(vdev, index) || is_irq_none(vdev)))
730 return -EINVAL;
731
732 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
733 int32_t *fds = data;
734 int ret;
735
736 if (vdev->irq_type == index)
737 return vfio_msi_set_block(vdev, start, count,
738 fds, msix);
739
740 ret = vfio_msi_enable(vdev, start + count, msix);
741 if (ret)
742 return ret;
743
744 ret = vfio_msi_set_block(vdev, start, count, fds, msix);
745 if (ret)
746 vfio_msi_disable(vdev, msix);
747
748 return ret;
749 }
750
751 if (!irq_is(vdev, index) || start + count > vdev->num_ctx)
752 return -EINVAL;
753
754 for (i = start; i < start + count; i++) {
755 if (!vdev->ctx[i].trigger)
756 continue;
757 if (flags & VFIO_IRQ_SET_DATA_NONE) {
758 eventfd_signal(vdev->ctx[i].trigger, 1);
759 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
760 uint8_t *bools = data;
761 if (bools[i - start])
762 eventfd_signal(vdev->ctx[i].trigger, 1);
763 }
764 }
765 return 0;
766}
767
Alex Williamsoncac80d62015-02-06 15:05:07 -0700768static int vfio_pci_set_ctx_trigger_single(struct eventfd_ctx **ctx,
769 uint32_t flags, void *data)
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600770{
771 int32_t fd = *(int32_t *)data;
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600772
Alex Williamsoncac80d62015-02-06 15:05:07 -0700773 if (!(flags & VFIO_IRQ_SET_DATA_TYPE_MASK))
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600774 return -EINVAL;
775
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600776 /* DATA_NONE/DATA_BOOL enables loopback testing */
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600777 if (flags & VFIO_IRQ_SET_DATA_NONE) {
Alex Williamsoncac80d62015-02-06 15:05:07 -0700778 if (*ctx)
779 eventfd_signal(*ctx, 1);
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600780 return 0;
781 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
782 uint8_t trigger = *(uint8_t *)data;
Alex Williamsoncac80d62015-02-06 15:05:07 -0700783 if (trigger && *ctx)
784 eventfd_signal(*ctx, 1);
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600785 return 0;
786 }
787
788 /* Handle SET_DATA_EVENTFD */
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600789 if (fd == -1) {
Alex Williamsoncac80d62015-02-06 15:05:07 -0700790 if (*ctx)
791 eventfd_ctx_put(*ctx);
792 *ctx = NULL;
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600793 return 0;
794 } else if (fd >= 0) {
795 struct eventfd_ctx *efdctx;
796 efdctx = eventfd_ctx_fdget(fd);
797 if (IS_ERR(efdctx))
798 return PTR_ERR(efdctx);
Alex Williamsoncac80d62015-02-06 15:05:07 -0700799 if (*ctx)
800 eventfd_ctx_put(*ctx);
801 *ctx = efdctx;
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600802 return 0;
803 } else
804 return -EINVAL;
805}
Alex Williamsoncac80d62015-02-06 15:05:07 -0700806
807static int vfio_pci_set_err_trigger(struct vfio_pci_device *vdev,
808 unsigned index, unsigned start,
809 unsigned count, uint32_t flags, void *data)
810{
811 if (index != VFIO_PCI_ERR_IRQ_INDEX)
812 return -EINVAL;
813
814 /*
815 * We should sanitize start & count, but that wasn't caught
816 * originally, so this IRQ index must forever ignore them :-(
817 */
818
819 return vfio_pci_set_ctx_trigger_single(&vdev->err_trigger, flags, data);
820}
821
Alex Williamson6140a8f2015-02-06 15:05:08 -0700822static int vfio_pci_set_req_trigger(struct vfio_pci_device *vdev,
823 unsigned index, unsigned start,
824 unsigned count, uint32_t flags, void *data)
825{
826 if (index != VFIO_PCI_REQ_IRQ_INDEX || start != 0 || count != 1)
827 return -EINVAL;
828
829 return vfio_pci_set_ctx_trigger_single(&vdev->req_trigger, flags, data);
830}
831
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600832int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev, uint32_t flags,
833 unsigned index, unsigned start, unsigned count,
834 void *data)
835{
836 int (*func)(struct vfio_pci_device *vdev, unsigned index,
837 unsigned start, unsigned count, uint32_t flags,
838 void *data) = NULL;
839
840 switch (index) {
841 case VFIO_PCI_INTX_IRQ_INDEX:
842 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
843 case VFIO_IRQ_SET_ACTION_MASK:
844 func = vfio_pci_set_intx_mask;
845 break;
846 case VFIO_IRQ_SET_ACTION_UNMASK:
847 func = vfio_pci_set_intx_unmask;
848 break;
849 case VFIO_IRQ_SET_ACTION_TRIGGER:
850 func = vfio_pci_set_intx_trigger;
851 break;
852 }
853 break;
854 case VFIO_PCI_MSI_IRQ_INDEX:
855 case VFIO_PCI_MSIX_IRQ_INDEX:
856 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
857 case VFIO_IRQ_SET_ACTION_MASK:
858 case VFIO_IRQ_SET_ACTION_UNMASK:
859 /* XXX Need masking support exported */
860 break;
861 case VFIO_IRQ_SET_ACTION_TRIGGER:
862 func = vfio_pci_set_msi_trigger;
863 break;
864 }
865 break;
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600866 case VFIO_PCI_ERR_IRQ_INDEX:
867 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
868 case VFIO_IRQ_SET_ACTION_TRIGGER:
869 if (pci_is_pcie(vdev->pdev))
870 func = vfio_pci_set_err_trigger;
871 break;
872 }
Alexey Kardashevskiyec76f402015-03-12 14:43:12 +1100873 break;
Alex Williamson6140a8f2015-02-06 15:05:08 -0700874 case VFIO_PCI_REQ_IRQ_INDEX:
875 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
876 case VFIO_IRQ_SET_ACTION_TRIGGER:
877 func = vfio_pci_set_req_trigger;
878 break;
879 }
Alexey Kardashevskiyec76f402015-03-12 14:43:12 +1100880 break;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600881 }
882
883 if (!func)
884 return -ENOTTY;
885
886 return func(vdev, index, start, count, flags, data);
887}