1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include "qemu/osdep.h"
18#include "qapi/error.h"
19#include <sys/ioctl.h>
20#include <linux/vfio.h>
21
22#include "hw/vfio/vfio-platform.h"
23#include "migration/vmstate.h"
24#include "qemu/error-report.h"
25#include "qemu/lockable.h"
26#include "qemu/main-loop.h"
27#include "qemu/module.h"
28#include "qemu/range.h"
29#include "exec/memory.h"
30#include "exec/address-spaces.h"
31#include "qemu/queue.h"
32#include "hw/sysbus.h"
33#include "trace.h"
34#include "hw/irq.h"
35#include "hw/platform-bus.h"
36#include "hw/qdev-properties.h"
37#include "sysemu/kvm.h"
38
39
40
41
42
43static inline bool vfio_irq_is_automasked(VFIOINTp *intp)
44{
45 return intp->flags & VFIO_IRQ_INFO_AUTOMASKED;
46}
47
48
49
50
51
52
53
54
55static VFIOINTp *vfio_init_intp(VFIODevice *vbasedev,
56 struct vfio_irq_info info, Error **errp)
57{
58 int ret;
59 VFIOPlatformDevice *vdev =
60 container_of(vbasedev, VFIOPlatformDevice, vbasedev);
61 SysBusDevice *sbdev = SYS_BUS_DEVICE(vdev);
62 VFIOINTp *intp;
63
64 intp = g_malloc0(sizeof(*intp));
65 intp->vdev = vdev;
66 intp->pin = info.index;
67 intp->flags = info.flags;
68 intp->state = VFIO_IRQ_INACTIVE;
69 intp->kvm_accel = false;
70
71 sysbus_init_irq(sbdev, &intp->qemuirq);
72
73
74 intp->interrupt = g_malloc0(sizeof(EventNotifier));
75 ret = event_notifier_init(intp->interrupt, 0);
76 if (ret) {
77 g_free(intp->interrupt);
78 g_free(intp);
79 error_setg_errno(errp, -ret,
80 "failed to initialize trigger eventfd notifier");
81 return NULL;
82 }
83 if (vfio_irq_is_automasked(intp)) {
84
85 intp->unmask = g_malloc0(sizeof(EventNotifier));
86 ret = event_notifier_init(intp->unmask, 0);
87 if (ret) {
88 g_free(intp->interrupt);
89 g_free(intp->unmask);
90 g_free(intp);
91 error_setg_errno(errp, -ret,
92 "failed to initialize resample eventfd notifier");
93 return NULL;
94 }
95 }
96
97 QLIST_INSERT_HEAD(&vdev->intp_list, intp, next);
98 return intp;
99}
100
101
102
103
104
105
106
107
108
109
110static int vfio_set_trigger_eventfd(VFIOINTp *intp,
111 eventfd_user_side_handler_t handler)
112{
113 VFIODevice *vbasedev = &intp->vdev->vbasedev;
114 int32_t fd = event_notifier_get_fd(intp->interrupt);
115 Error *err = NULL;
116 int ret;
117
118 qemu_set_fd_handler(fd, (IOHandler *)handler, NULL, intp);
119
120 ret = vfio_set_irq_signaling(vbasedev, intp->pin, 0,
121 VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err);
122 if (ret) {
123 error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name);
124 qemu_set_fd_handler(fd, NULL, NULL, NULL);
125 }
126
127 return ret;
128}
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145static void vfio_mmap_set_enabled(VFIOPlatformDevice *vdev, bool enabled)
146{
147 int i;
148
149 for (i = 0; i < vdev->vbasedev.num_regions; i++) {
150 vfio_region_mmaps_set_enabled(vdev->regions[i], enabled);
151 }
152}
153
154
155
156
157
158
159
160
161
162
163
164static void vfio_intp_mmap_enable(void *opaque)
165{
166 VFIOINTp *tmp;
167 VFIOPlatformDevice *vdev = (VFIOPlatformDevice *)opaque;
168
169 QEMU_LOCK_GUARD(&vdev->intp_mutex);
170 QLIST_FOREACH(tmp, &vdev->intp_list, next) {
171 if (tmp->state == VFIO_IRQ_ACTIVE) {
172 trace_vfio_platform_intp_mmap_enable(tmp->pin);
173
174 timer_mod(vdev->mmap_timer,
175 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
176 vdev->mmap_timeout);
177 return;
178 }
179 }
180 vfio_mmap_set_enabled(vdev, true);
181}
182
183
184
185
186
187
188
189
190
191
192static void vfio_intp_inject_pending_lockheld(VFIOINTp *intp)
193{
194 trace_vfio_platform_intp_inject_pending_lockheld(intp->pin,
195 event_notifier_get_fd(intp->interrupt));
196
197 intp->state = VFIO_IRQ_ACTIVE;
198
199
200 qemu_set_irq(intp->qemuirq, 1);
201}
202
203
204
205
206
207
208
209
210
211static void vfio_intp_interrupt(VFIOINTp *intp)
212{
213 int ret;
214 VFIOINTp *tmp;
215 VFIOPlatformDevice *vdev = intp->vdev;
216 bool delay_handling = false;
217
218 QEMU_LOCK_GUARD(&vdev->intp_mutex);
219 if (intp->state == VFIO_IRQ_INACTIVE) {
220 QLIST_FOREACH(tmp, &vdev->intp_list, next) {
221 if (tmp->state == VFIO_IRQ_ACTIVE ||
222 tmp->state == VFIO_IRQ_PENDING) {
223 delay_handling = true;
224 break;
225 }
226 }
227 }
228 if (delay_handling) {
229
230
231
232
233 intp->state = VFIO_IRQ_PENDING;
234 trace_vfio_intp_interrupt_set_pending(intp->pin);
235 QSIMPLEQ_INSERT_TAIL(&vdev->pending_intp_queue,
236 intp, pqnext);
237 event_notifier_test_and_clear(intp->interrupt);
238 return;
239 }
240
241 trace_vfio_platform_intp_interrupt(intp->pin,
242 event_notifier_get_fd(intp->interrupt));
243
244 ret = event_notifier_test_and_clear(intp->interrupt);
245 if (!ret) {
246 error_report("Error when clearing fd=%d (ret = %d)",
247 event_notifier_get_fd(intp->interrupt), ret);
248 }
249
250 intp->state = VFIO_IRQ_ACTIVE;
251
252
253 vfio_mmap_set_enabled(vdev, false);
254
255
256 qemu_set_irq(intp->qemuirq, 1);
257
258
259
260
261
262 if (vdev->mmap_timeout) {
263 timer_mod(vdev->mmap_timer,
264 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
265 vdev->mmap_timeout);
266 }
267}
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284static void vfio_platform_eoi(VFIODevice *vbasedev)
285{
286 VFIOINTp *intp;
287 VFIOPlatformDevice *vdev =
288 container_of(vbasedev, VFIOPlatformDevice, vbasedev);
289
290 QEMU_LOCK_GUARD(&vdev->intp_mutex);
291 QLIST_FOREACH(intp, &vdev->intp_list, next) {
292 if (intp->state == VFIO_IRQ_ACTIVE) {
293 trace_vfio_platform_eoi(intp->pin,
294 event_notifier_get_fd(intp->interrupt));
295 intp->state = VFIO_IRQ_INACTIVE;
296
297
298 qemu_set_irq(intp->qemuirq, 0);
299
300 if (vfio_irq_is_automasked(intp)) {
301
302 vfio_unmask_single_irqindex(vbasedev, intp->pin);
303 }
304
305
306 break;
307 }
308 }
309
310 if (!QSIMPLEQ_EMPTY(&vdev->pending_intp_queue)) {
311 intp = QSIMPLEQ_FIRST(&vdev->pending_intp_queue);
312 vfio_intp_inject_pending_lockheld(intp);
313 QSIMPLEQ_REMOVE_HEAD(&vdev->pending_intp_queue, pqnext);
314 }
315}
316
317
318
319
320
321
322
323
324static void vfio_start_eventfd_injection(SysBusDevice *sbdev, qemu_irq irq)
325{
326 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
327 VFIOINTp *intp;
328
329 QLIST_FOREACH(intp, &vdev->intp_list, next) {
330 if (intp->qemuirq == irq) {
331 break;
332 }
333 }
334 assert(intp);
335
336 if (vfio_set_trigger_eventfd(intp, vfio_intp_interrupt)) {
337 abort();
338 }
339}
340
341
342
343
344
345
346
347
348
349
350
351static int vfio_set_resample_eventfd(VFIOINTp *intp)
352{
353 int32_t fd = event_notifier_get_fd(intp->unmask);
354 VFIODevice *vbasedev = &intp->vdev->vbasedev;
355 Error *err = NULL;
356 int ret;
357
358 qemu_set_fd_handler(fd, NULL, NULL, NULL);
359 ret = vfio_set_irq_signaling(vbasedev, intp->pin, 0,
360 VFIO_IRQ_SET_ACTION_UNMASK, fd, &err);
361 if (ret) {
362 error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name);
363 }
364 return ret;
365}
366
367
368
369
370
371
372
373
374
375
376static void vfio_start_irqfd_injection(SysBusDevice *sbdev, qemu_irq irq)
377{
378 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
379 VFIOINTp *intp;
380
381 if (!kvm_irqfds_enabled() || !kvm_resamplefds_enabled() ||
382 !vdev->irqfd_allowed) {
383 goto fail_irqfd;
384 }
385
386 QLIST_FOREACH(intp, &vdev->intp_list, next) {
387 if (intp->qemuirq == irq) {
388 break;
389 }
390 }
391 assert(intp);
392
393 if (kvm_irqchip_add_irqfd_notifier(kvm_state, intp->interrupt,
394 intp->unmask, irq) < 0) {
395 goto fail_irqfd;
396 }
397
398 if (vfio_set_trigger_eventfd(intp, NULL) < 0) {
399 goto fail_vfio;
400 }
401 if (vfio_irq_is_automasked(intp)) {
402 if (vfio_set_resample_eventfd(intp) < 0) {
403 goto fail_vfio;
404 }
405 trace_vfio_platform_start_level_irqfd_injection(intp->pin,
406 event_notifier_get_fd(intp->interrupt),
407 event_notifier_get_fd(intp->unmask));
408 } else {
409 trace_vfio_platform_start_edge_irqfd_injection(intp->pin,
410 event_notifier_get_fd(intp->interrupt));
411 }
412
413 intp->kvm_accel = true;
414
415 return;
416fail_vfio:
417 kvm_irqchip_remove_irqfd_notifier(kvm_state, intp->interrupt, irq);
418 abort();
419fail_irqfd:
420 vfio_start_eventfd_injection(sbdev, irq);
421 return;
422}
423
424
425
426static void vfio_platform_compute_needs_reset(VFIODevice *vbasedev)
427{
428 vbasedev->needs_reset = true;
429}
430
431
432static int vfio_platform_hot_reset_multi(VFIODevice *vbasedev)
433{
434 return -1;
435}
436
437
438
439
440
441
442
443
444static int vfio_populate_device(VFIODevice *vbasedev, Error **errp)
445{
446 VFIOINTp *intp, *tmp;
447 int i, ret = -1;
448 VFIOPlatformDevice *vdev =
449 container_of(vbasedev, VFIOPlatformDevice, vbasedev);
450
451 if (!(vbasedev->flags & VFIO_DEVICE_FLAGS_PLATFORM)) {
452 error_setg(errp, "this isn't a platform device");
453 return ret;
454 }
455
456 vdev->regions = g_new0(VFIORegion *, vbasedev->num_regions);
457
458 for (i = 0; i < vbasedev->num_regions; i++) {
459 char *name = g_strdup_printf("VFIO %s region %d\n", vbasedev->name, i);
460
461 vdev->regions[i] = g_new0(VFIORegion, 1);
462 ret = vfio_region_setup(OBJECT(vdev), vbasedev,
463 vdev->regions[i], i, name);
464 g_free(name);
465 if (ret) {
466 error_setg_errno(errp, -ret, "failed to get region %d info", i);
467 goto reg_error;
468 }
469 }
470
471 vdev->mmap_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
472 vfio_intp_mmap_enable, vdev);
473
474 QSIMPLEQ_INIT(&vdev->pending_intp_queue);
475
476 for (i = 0; i < vbasedev->num_irqs; i++) {
477 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
478
479 irq.index = i;
480 ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
481 if (ret) {
482 error_setg_errno(errp, -ret, "failed to get device irq info");
483 goto irq_err;
484 } else {
485 trace_vfio_platform_populate_interrupts(irq.index,
486 irq.count,
487 irq.flags);
488 intp = vfio_init_intp(vbasedev, irq, errp);
489 if (!intp) {
490 ret = -1;
491 goto irq_err;
492 }
493 }
494 }
495 return 0;
496irq_err:
497 timer_del(vdev->mmap_timer);
498 QLIST_FOREACH_SAFE(intp, &vdev->intp_list, next, tmp) {
499 QLIST_REMOVE(intp, next);
500 g_free(intp);
501 }
502reg_error:
503 for (i = 0; i < vbasedev->num_regions; i++) {
504 if (vdev->regions[i]) {
505 vfio_region_finalize(vdev->regions[i]);
506 }
507 g_free(vdev->regions[i]);
508 }
509 g_free(vdev->regions);
510 return ret;
511}
512
513
514static VFIODeviceOps vfio_platform_ops = {
515 .vfio_compute_needs_reset = vfio_platform_compute_needs_reset,
516 .vfio_hot_reset_multi = vfio_platform_hot_reset_multi,
517 .vfio_eoi = vfio_platform_eoi,
518};
519
520
521
522
523
524
525
526
527
528
529
530static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp)
531{
532 VFIOGroup *group;
533 VFIODevice *vbasedev_iter;
534 char *tmp, group_path[PATH_MAX], *group_name;
535 ssize_t len;
536 struct stat st;
537 int groupid;
538 int ret;
539
540
541 if (vbasedev->sysfsdev) {
542 g_free(vbasedev->name);
543 vbasedev->name = g_path_get_basename(vbasedev->sysfsdev);
544 } else {
545 if (!vbasedev->name || strchr(vbasedev->name, '/')) {
546 error_setg(errp, "wrong host device name");
547 return -EINVAL;
548 }
549
550 vbasedev->sysfsdev = g_strdup_printf("/sys/bus/platform/devices/%s",
551 vbasedev->name);
552 }
553
554 if (stat(vbasedev->sysfsdev, &st) < 0) {
555 error_setg_errno(errp, errno,
556 "failed to get the sysfs host device file status");
557 return -errno;
558 }
559
560 tmp = g_strdup_printf("%s/iommu_group", vbasedev->sysfsdev);
561 len = readlink(tmp, group_path, sizeof(group_path));
562 g_free(tmp);
563
564 if (len < 0 || len >= sizeof(group_path)) {
565 ret = len < 0 ? -errno : -ENAMETOOLONG;
566 error_setg_errno(errp, -ret, "no iommu_group found");
567 return ret;
568 }
569
570 group_path[len] = 0;
571
572 group_name = basename(group_path);
573 if (sscanf(group_name, "%d", &groupid) != 1) {
574 error_setg_errno(errp, errno, "failed to read %s", group_path);
575 return -errno;
576 }
577
578 trace_vfio_platform_base_device_init(vbasedev->name, groupid);
579
580 group = vfio_get_group(groupid, &address_space_memory, errp);
581 if (!group) {
582 return -ENOENT;
583 }
584
585 QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
586 if (strcmp(vbasedev_iter->name, vbasedev->name) == 0) {
587 error_setg(errp, "device is already attached");
588 vfio_put_group(group);
589 return -EBUSY;
590 }
591 }
592 ret = vfio_get_device(group, vbasedev->name, vbasedev, errp);
593 if (ret) {
594 vfio_put_group(group);
595 return ret;
596 }
597
598 ret = vfio_populate_device(vbasedev, errp);
599 if (ret) {
600 vfio_put_group(group);
601 }
602
603 return ret;
604}
605
606
607
608
609
610
611
612
613
614static void vfio_platform_realize(DeviceState *dev, Error **errp)
615{
616 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(dev);
617 SysBusDevice *sbdev = SYS_BUS_DEVICE(dev);
618 VFIODevice *vbasedev = &vdev->vbasedev;
619 int i, ret;
620
621 vbasedev->type = VFIO_DEVICE_TYPE_PLATFORM;
622 vbasedev->dev = dev;
623 vbasedev->ops = &vfio_platform_ops;
624
625 qemu_mutex_init(&vdev->intp_mutex);
626
627 trace_vfio_platform_realize(vbasedev->sysfsdev ?
628 vbasedev->sysfsdev : vbasedev->name,
629 vdev->compat);
630
631 ret = vfio_base_device_init(vbasedev, errp);
632 if (ret) {
633 goto out;
634 }
635
636 if (!vdev->compat) {
637 GError *gerr = NULL;
638 gchar *contents;
639 gsize length;
640 char *path;
641
642 path = g_strdup_printf("%s/of_node/compatible", vbasedev->sysfsdev);
643 if (!g_file_get_contents(path, &contents, &length, &gerr)) {
644 error_setg(errp, "%s", gerr->message);
645 g_error_free(gerr);
646 g_free(path);
647 return;
648 }
649 g_free(path);
650 vdev->compat = contents;
651 for (vdev->num_compat = 0; length; vdev->num_compat++) {
652 size_t skip = strlen(contents) + 1;
653 contents += skip;
654 length -= skip;
655 }
656 }
657
658 for (i = 0; i < vbasedev->num_regions; i++) {
659 if (vfio_region_mmap(vdev->regions[i])) {
660 warn_report("%s mmap unsupported, performance may be slow",
661 memory_region_name(vdev->regions[i]->mem));
662 }
663 sysbus_init_mmio(sbdev, vdev->regions[i]->mem);
664 }
665out:
666 if (!ret) {
667 return;
668 }
669
670 if (vdev->vbasedev.name) {
671 error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.name);
672 } else {
673 error_prepend(errp, "vfio error: ");
674 }
675}
676
677static const VMStateDescription vfio_platform_vmstate = {
678 .name = "vfio-platform",
679 .unmigratable = 1,
680};
681
682static Property vfio_platform_dev_properties[] = {
683 DEFINE_PROP_STRING("host", VFIOPlatformDevice, vbasedev.name),
684 DEFINE_PROP_STRING("sysfsdev", VFIOPlatformDevice, vbasedev.sysfsdev),
685 DEFINE_PROP_BOOL("x-no-mmap", VFIOPlatformDevice, vbasedev.no_mmap, false),
686 DEFINE_PROP_UINT32("mmap-timeout-ms", VFIOPlatformDevice,
687 mmap_timeout, 1100),
688 DEFINE_PROP_BOOL("x-irqfd", VFIOPlatformDevice, irqfd_allowed, true),
689 DEFINE_PROP_END_OF_LIST(),
690};
691
692static void vfio_platform_class_init(ObjectClass *klass, void *data)
693{
694 DeviceClass *dc = DEVICE_CLASS(klass);
695 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
696
697 dc->realize = vfio_platform_realize;
698 device_class_set_props(dc, vfio_platform_dev_properties);
699 dc->vmsd = &vfio_platform_vmstate;
700 dc->desc = "VFIO-based platform device assignment";
701 sbc->connect_irq_notifier = vfio_start_irqfd_injection;
702 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
703
704 dc->user_creatable = true;
705}
706
707static const TypeInfo vfio_platform_dev_info = {
708 .name = TYPE_VFIO_PLATFORM,
709 .parent = TYPE_SYS_BUS_DEVICE,
710 .instance_size = sizeof(VFIOPlatformDevice),
711 .class_init = vfio_platform_class_init,
712 .class_size = sizeof(VFIOPlatformDeviceClass),
713};
714
715static void register_vfio_platform_dev_type(void)
716{
717 type_register_static(&vfio_platform_dev_info);
718}
719
720type_init(register_vfio_platform_dev_type)
721