1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/device.h>
17#include <linux/interrupt.h>
18#include <linux/eventfd.h>
19#include <linux/msi.h>
20#include <linux/pci.h>
21#include <linux/file.h>
22#include <linux/vfio.h>
23#include <linux/wait.h>
24#include <linux/slab.h>
25
26#include "vfio_pci_private.h"
27
28
29
30
31static void vfio_send_intx_eventfd(void *opaque, void *unused)
32{
33 struct vfio_pci_device *vdev = opaque;
34
35 if (likely(is_intx(vdev) && !vdev->virq_disabled))
36 eventfd_signal(vdev->ctx[0].trigger, 1);
37}
38
39void vfio_pci_intx_mask(struct vfio_pci_device *vdev)
40{
41 struct pci_dev *pdev = vdev->pdev;
42 unsigned long flags;
43
44 spin_lock_irqsave(&vdev->irqlock, flags);
45
46
47
48
49
50
51
52 if (unlikely(!is_intx(vdev))) {
53 if (vdev->pci_2_3)
54 pci_intx(pdev, 0);
55 } else if (!vdev->ctx[0].masked) {
56
57
58
59
60 if (vdev->pci_2_3)
61 pci_intx(pdev, 0);
62 else
63 disable_irq_nosync(pdev->irq);
64
65 vdev->ctx[0].masked = true;
66 }
67
68 spin_unlock_irqrestore(&vdev->irqlock, flags);
69}
70
71
72
73
74
75
76
77static int vfio_pci_intx_unmask_handler(void *opaque, void *unused)
78{
79 struct vfio_pci_device *vdev = opaque;
80 struct pci_dev *pdev = vdev->pdev;
81 unsigned long flags;
82 int ret = 0;
83
84 spin_lock_irqsave(&vdev->irqlock, flags);
85
86
87
88
89
90 if (unlikely(!is_intx(vdev))) {
91 if (vdev->pci_2_3)
92 pci_intx(pdev, 1);
93 } else if (vdev->ctx[0].masked && !vdev->virq_disabled) {
94
95
96
97
98
99 if (vdev->pci_2_3) {
100 if (!pci_check_and_unmask_intx(pdev))
101 ret = 1;
102 } else
103 enable_irq(pdev->irq);
104
105 vdev->ctx[0].masked = (ret > 0);
106 }
107
108 spin_unlock_irqrestore(&vdev->irqlock, flags);
109
110 return ret;
111}
112
113void vfio_pci_intx_unmask(struct vfio_pci_device *vdev)
114{
115 if (vfio_pci_intx_unmask_handler(vdev, NULL) > 0)
116 vfio_send_intx_eventfd(vdev, NULL);
117}
118
119static irqreturn_t vfio_intx_handler(int irq, void *dev_id)
120{
121 struct vfio_pci_device *vdev = dev_id;
122 unsigned long flags;
123 int ret = IRQ_NONE;
124
125 spin_lock_irqsave(&vdev->irqlock, flags);
126
127 if (!vdev->pci_2_3) {
128 disable_irq_nosync(vdev->pdev->irq);
129 vdev->ctx[0].masked = true;
130 ret = IRQ_HANDLED;
131 } else if (!vdev->ctx[0].masked &&
132 pci_check_and_mask_intx(vdev->pdev)) {
133 vdev->ctx[0].masked = true;
134 ret = IRQ_HANDLED;
135 }
136
137 spin_unlock_irqrestore(&vdev->irqlock, flags);
138
139 if (ret == IRQ_HANDLED)
140 vfio_send_intx_eventfd(vdev, NULL);
141
142 return ret;
143}
144
145static int vfio_intx_enable(struct vfio_pci_device *vdev)
146{
147 if (!is_irq_none(vdev))
148 return -EINVAL;
149
150 if (!vdev->pdev->irq)
151 return -ENODEV;
152
153 vdev->ctx = kzalloc(sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
154 if (!vdev->ctx)
155 return -ENOMEM;
156
157 vdev->num_ctx = 1;
158
159
160
161
162
163
164
165 vdev->ctx[0].masked = vdev->virq_disabled;
166 if (vdev->pci_2_3)
167 pci_intx(vdev->pdev, !vdev->ctx[0].masked);
168
169 vdev->irq_type = VFIO_PCI_INTX_IRQ_INDEX;
170
171 return 0;
172}
173
174static int vfio_intx_set_signal(struct vfio_pci_device *vdev, int fd)
175{
176 struct pci_dev *pdev = vdev->pdev;
177 unsigned long irqflags = IRQF_SHARED;
178 struct eventfd_ctx *trigger;
179 unsigned long flags;
180 int ret;
181
182 if (vdev->ctx[0].trigger) {
183 free_irq(pdev->irq, vdev);
184 kfree(vdev->ctx[0].name);
185 eventfd_ctx_put(vdev->ctx[0].trigger);
186 vdev->ctx[0].trigger = NULL;
187 }
188
189 if (fd < 0)
190 return 0;
191
192 vdev->ctx[0].name = kasprintf(GFP_KERNEL, "vfio-intx(%s)",
193 pci_name(pdev));
194 if (!vdev->ctx[0].name)
195 return -ENOMEM;
196
197 trigger = eventfd_ctx_fdget(fd);
198 if (IS_ERR(trigger)) {
199 kfree(vdev->ctx[0].name);
200 return PTR_ERR(trigger);
201 }
202
203 vdev->ctx[0].trigger = trigger;
204
205 if (!vdev->pci_2_3)
206 irqflags = 0;
207
208 ret = request_irq(pdev->irq, vfio_intx_handler,
209 irqflags, vdev->ctx[0].name, vdev);
210 if (ret) {
211 vdev->ctx[0].trigger = NULL;
212 kfree(vdev->ctx[0].name);
213 eventfd_ctx_put(trigger);
214 return ret;
215 }
216
217
218
219
220
221 spin_lock_irqsave(&vdev->irqlock, flags);
222 if (!vdev->pci_2_3 && vdev->ctx[0].masked)
223 disable_irq_nosync(pdev->irq);
224 spin_unlock_irqrestore(&vdev->irqlock, flags);
225
226 return 0;
227}
228
229static void vfio_intx_disable(struct vfio_pci_device *vdev)
230{
231 vfio_virqfd_disable(&vdev->ctx[0].unmask);
232 vfio_virqfd_disable(&vdev->ctx[0].mask);
233 vfio_intx_set_signal(vdev, -1);
234 vdev->irq_type = VFIO_PCI_NUM_IRQS;
235 vdev->num_ctx = 0;
236 kfree(vdev->ctx);
237}
238
239
240
241
242static irqreturn_t vfio_msihandler(int irq, void *arg)
243{
244 struct eventfd_ctx *trigger = arg;
245
246 eventfd_signal(trigger, 1);
247 return IRQ_HANDLED;
248}
249
250static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)
251{
252 struct pci_dev *pdev = vdev->pdev;
253 unsigned int flag = msix ? PCI_IRQ_MSIX : PCI_IRQ_MSI;
254 int ret;
255 u16 cmd;
256
257 if (!is_irq_none(vdev))
258 return -EINVAL;
259
260 vdev->ctx = kcalloc(nvec, sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
261 if (!vdev->ctx)
262 return -ENOMEM;
263
264
265 cmd = vfio_pci_memory_lock_and_enable(vdev);
266 ret = pci_alloc_irq_vectors(pdev, 1, nvec, flag);
267 if (ret < nvec) {
268 if (ret > 0)
269 pci_free_irq_vectors(pdev);
270 vfio_pci_memory_unlock_and_restore(vdev, cmd);
271 kfree(vdev->ctx);
272 return ret;
273 }
274 vfio_pci_memory_unlock_and_restore(vdev, cmd);
275
276 vdev->num_ctx = nvec;
277 vdev->irq_type = msix ? VFIO_PCI_MSIX_IRQ_INDEX :
278 VFIO_PCI_MSI_IRQ_INDEX;
279
280 if (!msix) {
281
282
283
284
285 vdev->msi_qmax = fls(nvec * 2 - 1) - 1;
286 }
287
288 return 0;
289}
290
291static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
292 int vector, int fd, bool msix)
293{
294 struct pci_dev *pdev = vdev->pdev;
295 struct eventfd_ctx *trigger;
296 int irq, ret;
297 u16 cmd;
298
299 if (vector < 0 || vector >= vdev->num_ctx)
300 return -EINVAL;
301
302 irq = pci_irq_vector(pdev, vector);
303
304 if (vdev->ctx[vector].trigger) {
305 irq_bypass_unregister_producer(&vdev->ctx[vector].producer);
306
307 cmd = vfio_pci_memory_lock_and_enable(vdev);
308 free_irq(irq, vdev->ctx[vector].trigger);
309 vfio_pci_memory_unlock_and_restore(vdev, cmd);
310
311 kfree(vdev->ctx[vector].name);
312 eventfd_ctx_put(vdev->ctx[vector].trigger);
313 vdev->ctx[vector].trigger = NULL;
314 }
315
316 if (fd < 0)
317 return 0;
318
319 vdev->ctx[vector].name = kasprintf(GFP_KERNEL, "vfio-msi%s[%d](%s)",
320 msix ? "x" : "", vector,
321 pci_name(pdev));
322 if (!vdev->ctx[vector].name)
323 return -ENOMEM;
324
325 trigger = eventfd_ctx_fdget(fd);
326 if (IS_ERR(trigger)) {
327 kfree(vdev->ctx[vector].name);
328 return PTR_ERR(trigger);
329 }
330
331
332
333
334
335
336
337
338 cmd = vfio_pci_memory_lock_and_enable(vdev);
339 if (msix) {
340 struct msi_msg msg;
341
342 get_cached_msi_msg(irq, &msg);
343 pci_write_msi_msg(irq, &msg);
344 }
345
346 ret = request_irq(irq, vfio_msihandler, 0,
347 vdev->ctx[vector].name, trigger);
348 vfio_pci_memory_unlock_and_restore(vdev, cmd);
349 if (ret) {
350 kfree(vdev->ctx[vector].name);
351 eventfd_ctx_put(trigger);
352 return ret;
353 }
354
355 vdev->ctx[vector].producer.token = trigger;
356 vdev->ctx[vector].producer.irq = irq;
357 ret = irq_bypass_register_producer(&vdev->ctx[vector].producer);
358 if (unlikely(ret)) {
359 dev_info(&pdev->dev,
360 "irq bypass producer (token %p) registration fails: %d\n",
361 vdev->ctx[vector].producer.token, ret);
362
363 vdev->ctx[vector].producer.token = NULL;
364 }
365 vdev->ctx[vector].trigger = trigger;
366
367 return 0;
368}
369
370static int vfio_msi_set_block(struct vfio_pci_device *vdev, unsigned start,
371 unsigned count, int32_t *fds, bool msix)
372{
373 int i, j, ret = 0;
374
375 if (start >= vdev->num_ctx || start + count > vdev->num_ctx)
376 return -EINVAL;
377
378 for (i = 0, j = start; i < count && !ret; i++, j++) {
379 int fd = fds ? fds[i] : -1;
380 ret = vfio_msi_set_vector_signal(vdev, j, fd, msix);
381 }
382
383 if (ret) {
384 for (--j; j >= (int)start; j--)
385 vfio_msi_set_vector_signal(vdev, j, -1, msix);
386 }
387
388 return ret;
389}
390
391static void vfio_msi_disable(struct vfio_pci_device *vdev, bool msix)
392{
393 struct pci_dev *pdev = vdev->pdev;
394 int i;
395 u16 cmd;
396
397 for (i = 0; i < vdev->num_ctx; i++) {
398 vfio_virqfd_disable(&vdev->ctx[i].unmask);
399 vfio_virqfd_disable(&vdev->ctx[i].mask);
400 }
401
402 vfio_msi_set_block(vdev, 0, vdev->num_ctx, NULL, msix);
403
404 cmd = vfio_pci_memory_lock_and_enable(vdev);
405 pci_free_irq_vectors(pdev);
406 vfio_pci_memory_unlock_and_restore(vdev, cmd);
407
408
409
410
411
412 if (vdev->nointx)
413 pci_intx(pdev, 0);
414
415 vdev->irq_type = VFIO_PCI_NUM_IRQS;
416 vdev->num_ctx = 0;
417 kfree(vdev->ctx);
418}
419
420
421
422
423static int vfio_pci_set_intx_unmask(struct vfio_pci_device *vdev,
424 unsigned index, unsigned start,
425 unsigned count, uint32_t flags, void *data)
426{
427 if (!is_intx(vdev) || start != 0 || count != 1)
428 return -EINVAL;
429
430 if (flags & VFIO_IRQ_SET_DATA_NONE) {
431 vfio_pci_intx_unmask(vdev);
432 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
433 uint8_t unmask = *(uint8_t *)data;
434 if (unmask)
435 vfio_pci_intx_unmask(vdev);
436 } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
437 int32_t fd = *(int32_t *)data;
438 if (fd >= 0)
439 return vfio_virqfd_enable((void *) vdev,
440 vfio_pci_intx_unmask_handler,
441 vfio_send_intx_eventfd, NULL,
442 &vdev->ctx[0].unmask, fd);
443
444 vfio_virqfd_disable(&vdev->ctx[0].unmask);
445 }
446
447 return 0;
448}
449
450static int vfio_pci_set_intx_mask(struct vfio_pci_device *vdev,
451 unsigned index, unsigned start,
452 unsigned count, uint32_t flags, void *data)
453{
454 if (!is_intx(vdev) || start != 0 || count != 1)
455 return -EINVAL;
456
457 if (flags & VFIO_IRQ_SET_DATA_NONE) {
458 vfio_pci_intx_mask(vdev);
459 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
460 uint8_t mask = *(uint8_t *)data;
461 if (mask)
462 vfio_pci_intx_mask(vdev);
463 } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
464 return -ENOTTY;
465 }
466
467 return 0;
468}
469
470static int vfio_pci_set_intx_trigger(struct vfio_pci_device *vdev,
471 unsigned index, unsigned start,
472 unsigned count, uint32_t flags, void *data)
473{
474 if (is_intx(vdev) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
475 vfio_intx_disable(vdev);
476 return 0;
477 }
478
479 if (!(is_intx(vdev) || is_irq_none(vdev)) || start != 0 || count != 1)
480 return -EINVAL;
481
482 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
483 int32_t fd = *(int32_t *)data;
484 int ret;
485
486 if (is_intx(vdev))
487 return vfio_intx_set_signal(vdev, fd);
488
489 ret = vfio_intx_enable(vdev);
490 if (ret)
491 return ret;
492
493 ret = vfio_intx_set_signal(vdev, fd);
494 if (ret)
495 vfio_intx_disable(vdev);
496
497 return ret;
498 }
499
500 if (!is_intx(vdev))
501 return -EINVAL;
502
503 if (flags & VFIO_IRQ_SET_DATA_NONE) {
504 vfio_send_intx_eventfd(vdev, NULL);
505 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
506 uint8_t trigger = *(uint8_t *)data;
507 if (trigger)
508 vfio_send_intx_eventfd(vdev, NULL);
509 }
510 return 0;
511}
512
513static int vfio_pci_set_msi_trigger(struct vfio_pci_device *vdev,
514 unsigned index, unsigned start,
515 unsigned count, uint32_t flags, void *data)
516{
517 int i;
518 bool msix = (index == VFIO_PCI_MSIX_IRQ_INDEX) ? true : false;
519
520 if (irq_is(vdev, index) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
521 vfio_msi_disable(vdev, msix);
522 return 0;
523 }
524
525 if (!(irq_is(vdev, index) || is_irq_none(vdev)))
526 return -EINVAL;
527
528 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
529 int32_t *fds = data;
530 int ret;
531
532 if (vdev->irq_type == index)
533 return vfio_msi_set_block(vdev, start, count,
534 fds, msix);
535
536 ret = vfio_msi_enable(vdev, start + count, msix);
537 if (ret)
538 return ret;
539
540 ret = vfio_msi_set_block(vdev, start, count, fds, msix);
541 if (ret)
542 vfio_msi_disable(vdev, msix);
543
544 return ret;
545 }
546
547 if (!irq_is(vdev, index) || start + count > vdev->num_ctx)
548 return -EINVAL;
549
550 for (i = start; i < start + count; i++) {
551 if (!vdev->ctx[i].trigger)
552 continue;
553 if (flags & VFIO_IRQ_SET_DATA_NONE) {
554 eventfd_signal(vdev->ctx[i].trigger, 1);
555 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
556 uint8_t *bools = data;
557 if (bools[i - start])
558 eventfd_signal(vdev->ctx[i].trigger, 1);
559 }
560 }
561 return 0;
562}
563
564static int vfio_pci_set_ctx_trigger_single(struct eventfd_ctx **ctx,
565 unsigned int count, uint32_t flags,
566 void *data)
567{
568
569 if (flags & VFIO_IRQ_SET_DATA_NONE) {
570 if (*ctx) {
571 if (count) {
572 eventfd_signal(*ctx, 1);
573 } else {
574 eventfd_ctx_put(*ctx);
575 *ctx = NULL;
576 }
577 return 0;
578 }
579 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
580 uint8_t trigger;
581
582 if (!count)
583 return -EINVAL;
584
585 trigger = *(uint8_t *)data;
586 if (trigger && *ctx)
587 eventfd_signal(*ctx, 1);
588
589 return 0;
590 } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
591 int32_t fd;
592
593 if (!count)
594 return -EINVAL;
595
596 fd = *(int32_t *)data;
597 if (fd == -1) {
598 if (*ctx)
599 eventfd_ctx_put(*ctx);
600 *ctx = NULL;
601 } else if (fd >= 0) {
602 struct eventfd_ctx *efdctx;
603
604 efdctx = eventfd_ctx_fdget(fd);
605 if (IS_ERR(efdctx))
606 return PTR_ERR(efdctx);
607
608 if (*ctx)
609 eventfd_ctx_put(*ctx);
610
611 *ctx = efdctx;
612 }
613 return 0;
614 }
615
616 return -EINVAL;
617}
618
619static int vfio_pci_set_err_trigger(struct vfio_pci_device *vdev,
620 unsigned index, unsigned start,
621 unsigned count, uint32_t flags, void *data)
622{
623 if (index != VFIO_PCI_ERR_IRQ_INDEX || start != 0 || count > 1)
624 return -EINVAL;
625
626 return vfio_pci_set_ctx_trigger_single(&vdev->err_trigger,
627 count, flags, data);
628}
629
630static int vfio_pci_set_req_trigger(struct vfio_pci_device *vdev,
631 unsigned index, unsigned start,
632 unsigned count, uint32_t flags, void *data)
633{
634 if (index != VFIO_PCI_REQ_IRQ_INDEX || start != 0 || count > 1)
635 return -EINVAL;
636
637 return vfio_pci_set_ctx_trigger_single(&vdev->req_trigger,
638 count, flags, data);
639}
640
641int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev, uint32_t flags,
642 unsigned index, unsigned start, unsigned count,
643 void *data)
644{
645 int (*func)(struct vfio_pci_device *vdev, unsigned index,
646 unsigned start, unsigned count, uint32_t flags,
647 void *data) = NULL;
648
649 switch (index) {
650 case VFIO_PCI_INTX_IRQ_INDEX:
651 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
652 case VFIO_IRQ_SET_ACTION_MASK:
653 func = vfio_pci_set_intx_mask;
654 break;
655 case VFIO_IRQ_SET_ACTION_UNMASK:
656 func = vfio_pci_set_intx_unmask;
657 break;
658 case VFIO_IRQ_SET_ACTION_TRIGGER:
659 func = vfio_pci_set_intx_trigger;
660 break;
661 }
662 break;
663 case VFIO_PCI_MSI_IRQ_INDEX:
664 case VFIO_PCI_MSIX_IRQ_INDEX:
665 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
666 case VFIO_IRQ_SET_ACTION_MASK:
667 case VFIO_IRQ_SET_ACTION_UNMASK:
668
669 break;
670 case VFIO_IRQ_SET_ACTION_TRIGGER:
671 func = vfio_pci_set_msi_trigger;
672 break;
673 }
674 break;
675 case VFIO_PCI_ERR_IRQ_INDEX:
676 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
677 case VFIO_IRQ_SET_ACTION_TRIGGER:
678 if (pci_is_pcie(vdev->pdev))
679 func = vfio_pci_set_err_trigger;
680 break;
681 }
682 break;
683 case VFIO_PCI_REQ_IRQ_INDEX:
684 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
685 case VFIO_IRQ_SET_ACTION_TRIGGER:
686 func = vfio_pci_set_req_trigger;
687 break;
688 }
689 break;
690 }
691
692 if (!func)
693 return -ENOTTY;
694
695 return func(vdev, index, start, count, flags, data);
696}
697