xref: /openbmc/qemu/hw/vfio/platform.c (revision 86e372ad)
1 /*
2  * vfio based device assignment support - platform devices
3  *
4  * Copyright Linaro Limited, 2014
5  *
6  * Authors:
7  *  Kim Phillips <kim.phillips@linaro.org>
8  *  Eric Auger <eric.auger@linaro.org>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2.  See
11  * the COPYING file in the top-level directory.
12  *
13  * Based on vfio based PCI device assignment support:
14  *  Copyright Red Hat, Inc. 2012
15  */
16 
17 #include "qemu/osdep.h"
18 #include CONFIG_DEVICES /* CONFIG_IOMMUFD */
19 #include "qapi/error.h"
20 #include <sys/ioctl.h>
21 #include <linux/vfio.h>
22 
23 #include "hw/vfio/vfio-platform.h"
24 #include "sysemu/iommufd.h"
25 #include "migration/vmstate.h"
26 #include "qemu/error-report.h"
27 #include "qemu/lockable.h"
28 #include "qemu/main-loop.h"
29 #include "qemu/module.h"
30 #include "qemu/range.h"
31 #include "exec/memory.h"
32 #include "exec/address-spaces.h"
33 #include "qemu/queue.h"
34 #include "hw/sysbus.h"
35 #include "trace.h"
36 #include "hw/irq.h"
37 #include "hw/platform-bus.h"
38 #include "hw/qdev-properties.h"
39 #include "sysemu/kvm.h"
40 
41 /*
42  * Functions used whatever the injection method
43  */
44 
45 static inline bool vfio_irq_is_automasked(VFIOINTp *intp)
46 {
47     return intp->flags & VFIO_IRQ_INFO_AUTOMASKED;
48 }
49 
50 /**
51  * vfio_init_intp - allocate, initialize the IRQ struct pointer
52  * and add it into the list of IRQs
53  * @vbasedev: the VFIO device handle
54  * @info: irq info struct retrieved from VFIO driver
55  * @errp: error object
56  */
57 static VFIOINTp *vfio_init_intp(VFIODevice *vbasedev,
58                                 struct vfio_irq_info info, Error **errp)
59 {
60     int ret;
61     VFIOPlatformDevice *vdev =
62         container_of(vbasedev, VFIOPlatformDevice, vbasedev);
63     SysBusDevice *sbdev = SYS_BUS_DEVICE(vdev);
64     VFIOINTp *intp;
65 
66     intp = g_malloc0(sizeof(*intp));
67     intp->vdev = vdev;
68     intp->pin = info.index;
69     intp->flags = info.flags;
70     intp->state = VFIO_IRQ_INACTIVE;
71     intp->kvm_accel = false;
72 
73     sysbus_init_irq(sbdev, &intp->qemuirq);
74 
75     /* Get an eventfd for trigger */
76     intp->interrupt = g_new0(EventNotifier, 1);
77     ret = event_notifier_init(intp->interrupt, 0);
78     if (ret) {
79         g_free(intp->interrupt);
80         g_free(intp);
81         error_setg_errno(errp, -ret,
82                          "failed to initialize trigger eventfd notifier");
83         return NULL;
84     }
85     if (vfio_irq_is_automasked(intp)) {
86         /* Get an eventfd for resample/unmask */
87         intp->unmask = g_new0(EventNotifier, 1);
88         ret = event_notifier_init(intp->unmask, 0);
89         if (ret) {
90             g_free(intp->interrupt);
91             g_free(intp->unmask);
92             g_free(intp);
93             error_setg_errno(errp, -ret,
94                              "failed to initialize resample eventfd notifier");
95             return NULL;
96         }
97     }
98 
99     QLIST_INSERT_HEAD(&vdev->intp_list, intp, next);
100     return intp;
101 }
102 
103 /**
104  * vfio_set_trigger_eventfd - set VFIO eventfd handling
105  *
106  * @intp: IRQ struct handle
107  * @handler: handler to be called on eventfd signaling
108  *
109  * Setup VFIO signaling and attach an optional user-side handler
110  * to the eventfd
111  */
112 static int vfio_set_trigger_eventfd(VFIOINTp *intp,
113                                     eventfd_user_side_handler_t handler)
114 {
115     VFIODevice *vbasedev = &intp->vdev->vbasedev;
116     int32_t fd = event_notifier_get_fd(intp->interrupt);
117     Error *err = NULL;
118     int ret;
119 
120     qemu_set_fd_handler(fd, (IOHandler *)handler, NULL, intp);
121 
122     ret = vfio_set_irq_signaling(vbasedev, intp->pin, 0,
123                                  VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err);
124     if (ret) {
125         error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name);
126         qemu_set_fd_handler(fd, NULL, NULL, NULL);
127     }
128 
129     return ret;
130 }
131 
132 /*
133  * Functions only used when eventfds are handled on user-side
134  * ie. without irqfd
135  */
136 
137 /**
138  * vfio_mmap_set_enabled - enable/disable the fast path mode
139  * @vdev: the VFIO platform device
140  * @enabled: the target mmap state
141  *
142  * enabled = true ~ fast path = MMIO region is mmaped (no KVM TRAP);
143  * enabled = false ~ slow path = MMIO region is trapped and region callbacks
144  * are called; slow path enables to trap the device IRQ status register reset
145 */
146 
147 static void vfio_mmap_set_enabled(VFIOPlatformDevice *vdev, bool enabled)
148 {
149     int i;
150 
151     for (i = 0; i < vdev->vbasedev.num_regions; i++) {
152         vfio_region_mmaps_set_enabled(vdev->regions[i], enabled);
153     }
154 }
155 
156 /**
157  * vfio_intp_mmap_enable - timer function, restores the fast path
158  * if there is no more active IRQ
159  * @opaque: actually points to the VFIO platform device
160  *
161  * Called on mmap timer timeout, this function checks whether the
162  * IRQ is still active and if not, restores the fast path.
163  * by construction a single eventfd is handled at a time.
164  * if the IRQ is still active, the timer is re-programmed.
165  */
166 static void vfio_intp_mmap_enable(void *opaque)
167 {
168     VFIOINTp *tmp;
169     VFIOPlatformDevice *vdev = (VFIOPlatformDevice *)opaque;
170 
171     QEMU_LOCK_GUARD(&vdev->intp_mutex);
172     QLIST_FOREACH(tmp, &vdev->intp_list, next) {
173         if (tmp->state == VFIO_IRQ_ACTIVE) {
174             trace_vfio_platform_intp_mmap_enable(tmp->pin);
175             /* re-program the timer to check active status later */
176             timer_mod(vdev->mmap_timer,
177                       qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
178                           vdev->mmap_timeout);
179             return;
180         }
181     }
182     vfio_mmap_set_enabled(vdev, true);
183 }
184 
185 /**
186  * vfio_intp_inject_pending_lockheld - Injects a pending IRQ
187  * @opaque: opaque pointer, in practice the VFIOINTp handle
188  *
189  * The function is called on a previous IRQ completion, from
190  * vfio_platform_eoi, while the intp_mutex is locked.
191  * Also in such situation, the slow path already is set and
192  * the mmap timer was already programmed.
193  */
194 static void vfio_intp_inject_pending_lockheld(VFIOINTp *intp)
195 {
196     trace_vfio_platform_intp_inject_pending_lockheld(intp->pin,
197                               event_notifier_get_fd(intp->interrupt));
198 
199     intp->state = VFIO_IRQ_ACTIVE;
200 
201     /* trigger the virtual IRQ */
202     qemu_set_irq(intp->qemuirq, 1);
203 }
204 
205 /**
206  * vfio_intp_interrupt - The user-side eventfd handler
207  * @opaque: opaque pointer which in practice is the VFIOINTp handle
208  *
209  * the function is entered in event handler context:
210  * the vIRQ is injected into the guest if there is no other active
211  * or pending IRQ.
212  */
213 static void vfio_intp_interrupt(VFIOINTp *intp)
214 {
215     int ret;
216     VFIOINTp *tmp;
217     VFIOPlatformDevice *vdev = intp->vdev;
218     bool delay_handling = false;
219 
220     QEMU_LOCK_GUARD(&vdev->intp_mutex);
221     if (intp->state == VFIO_IRQ_INACTIVE) {
222         QLIST_FOREACH(tmp, &vdev->intp_list, next) {
223             if (tmp->state == VFIO_IRQ_ACTIVE ||
224                 tmp->state == VFIO_IRQ_PENDING) {
225                 delay_handling = true;
226                 break;
227             }
228         }
229     }
230     if (delay_handling) {
231         /*
232          * the new IRQ gets a pending status and is pushed in
233          * the pending queue
234          */
235         intp->state = VFIO_IRQ_PENDING;
236         trace_vfio_intp_interrupt_set_pending(intp->pin);
237         QSIMPLEQ_INSERT_TAIL(&vdev->pending_intp_queue,
238                              intp, pqnext);
239         event_notifier_test_and_clear(intp->interrupt);
240         return;
241     }
242 
243     trace_vfio_platform_intp_interrupt(intp->pin,
244                               event_notifier_get_fd(intp->interrupt));
245 
246     ret = event_notifier_test_and_clear(intp->interrupt);
247     if (!ret) {
248         error_report("Error when clearing fd=%d (ret = %d)",
249                      event_notifier_get_fd(intp->interrupt), ret);
250     }
251 
252     intp->state = VFIO_IRQ_ACTIVE;
253 
254     /* sets slow path */
255     vfio_mmap_set_enabled(vdev, false);
256 
257     /* trigger the virtual IRQ */
258     qemu_set_irq(intp->qemuirq, 1);
259 
260     /*
261      * Schedule the mmap timer which will restore fastpath when no IRQ
262      * is active anymore
263      */
264     if (vdev->mmap_timeout) {
265         timer_mod(vdev->mmap_timer,
266                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
267                       vdev->mmap_timeout);
268     }
269 }
270 
271 /**
272  * vfio_platform_eoi - IRQ completion routine
273  * @vbasedev: the VFIO device handle
274  *
275  * De-asserts the active virtual IRQ and unmasks the physical IRQ
276  * (effective for level sensitive IRQ auto-masked by the  VFIO driver).
277  * Then it handles next pending IRQ if any.
278  * eoi function is called on the first access to any MMIO region
279  * after an IRQ was triggered, trapped since slow path was set.
280  * It is assumed this access corresponds to the IRQ status
281  * register reset. With such a mechanism, a single IRQ can be
282  * handled at a time since there is no way to know which IRQ
283  * was completed by the guest (we would need additional details
284  * about the IRQ status register mask).
285  */
286 static void vfio_platform_eoi(VFIODevice *vbasedev)
287 {
288     VFIOINTp *intp;
289     VFIOPlatformDevice *vdev =
290         container_of(vbasedev, VFIOPlatformDevice, vbasedev);
291 
292     QEMU_LOCK_GUARD(&vdev->intp_mutex);
293     QLIST_FOREACH(intp, &vdev->intp_list, next) {
294         if (intp->state == VFIO_IRQ_ACTIVE) {
295             trace_vfio_platform_eoi(intp->pin,
296                                 event_notifier_get_fd(intp->interrupt));
297             intp->state = VFIO_IRQ_INACTIVE;
298 
299             /* deassert the virtual IRQ */
300             qemu_set_irq(intp->qemuirq, 0);
301 
302             if (vfio_irq_is_automasked(intp)) {
303                 /* unmasks the physical level-sensitive IRQ */
304                 vfio_unmask_single_irqindex(vbasedev, intp->pin);
305             }
306 
307             /* a single IRQ can be active at a time */
308             break;
309         }
310     }
311     /* in case there are pending IRQs, handle the first one */
312     if (!QSIMPLEQ_EMPTY(&vdev->pending_intp_queue)) {
313         intp = QSIMPLEQ_FIRST(&vdev->pending_intp_queue);
314         vfio_intp_inject_pending_lockheld(intp);
315         QSIMPLEQ_REMOVE_HEAD(&vdev->pending_intp_queue, pqnext);
316     }
317 }
318 
319 /**
320  * vfio_start_eventfd_injection - starts the virtual IRQ injection using
321  * user-side handled eventfds
322  * @sbdev: the sysbus device handle
323  * @irq: the qemu irq handle
324  */
325 
326 static void vfio_start_eventfd_injection(SysBusDevice *sbdev, qemu_irq irq)
327 {
328     VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
329     VFIOINTp *intp;
330 
331     QLIST_FOREACH(intp, &vdev->intp_list, next) {
332         if (intp->qemuirq == irq) {
333             break;
334         }
335     }
336     assert(intp);
337 
338     if (vfio_set_trigger_eventfd(intp, vfio_intp_interrupt)) {
339         abort();
340     }
341 }
342 
343 /*
344  * Functions used for irqfd
345  */
346 
347 /**
348  * vfio_set_resample_eventfd - sets the resamplefd for an IRQ
349  * @intp: the IRQ struct handle
350  * programs the VFIO driver to unmask this IRQ when the
351  * intp->unmask eventfd is triggered
352  */
353 static int vfio_set_resample_eventfd(VFIOINTp *intp)
354 {
355     int32_t fd = event_notifier_get_fd(intp->unmask);
356     VFIODevice *vbasedev = &intp->vdev->vbasedev;
357     Error *err = NULL;
358     int ret;
359 
360     qemu_set_fd_handler(fd, NULL, NULL, NULL);
361     ret = vfio_set_irq_signaling(vbasedev, intp->pin, 0,
362                                  VFIO_IRQ_SET_ACTION_UNMASK, fd, &err);
363     if (ret) {
364         error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name);
365     }
366     return ret;
367 }
368 
369 /**
370  * vfio_start_irqfd_injection - starts the virtual IRQ injection using
371  * irqfd
372  *
373  * @sbdev: the sysbus device handle
374  * @irq: the qemu irq handle
375  *
376  * In case the irqfd setup fails, we fallback to userspace handled eventfd
377  */
378 static void vfio_start_irqfd_injection(SysBusDevice *sbdev, qemu_irq irq)
379 {
380     VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
381     VFIOINTp *intp;
382 
383     if (!kvm_irqfds_enabled() || !kvm_resamplefds_enabled() ||
384         !vdev->irqfd_allowed) {
385         goto fail_irqfd;
386     }
387 
388     QLIST_FOREACH(intp, &vdev->intp_list, next) {
389         if (intp->qemuirq == irq) {
390             break;
391         }
392     }
393     assert(intp);
394 
395     if (kvm_irqchip_add_irqfd_notifier(kvm_state, intp->interrupt,
396                                    intp->unmask, irq) < 0) {
397         goto fail_irqfd;
398     }
399 
400     if (vfio_set_trigger_eventfd(intp, NULL) < 0) {
401         goto fail_vfio;
402     }
403     if (vfio_irq_is_automasked(intp)) {
404         if (vfio_set_resample_eventfd(intp) < 0) {
405             goto fail_vfio;
406         }
407         trace_vfio_platform_start_level_irqfd_injection(intp->pin,
408                                     event_notifier_get_fd(intp->interrupt),
409                                     event_notifier_get_fd(intp->unmask));
410     } else {
411         trace_vfio_platform_start_edge_irqfd_injection(intp->pin,
412                                     event_notifier_get_fd(intp->interrupt));
413     }
414 
415     intp->kvm_accel = true;
416 
417     return;
418 fail_vfio:
419     kvm_irqchip_remove_irqfd_notifier(kvm_state, intp->interrupt, irq);
420     abort();
421 fail_irqfd:
422     vfio_start_eventfd_injection(sbdev, irq);
423     return;
424 }
425 
426 /* VFIO skeleton */
427 
428 static void vfio_platform_compute_needs_reset(VFIODevice *vbasedev)
429 {
430     vbasedev->needs_reset = true;
431 }
432 
433 /* not implemented yet */
434 static int vfio_platform_hot_reset_multi(VFIODevice *vbasedev)
435 {
436     return -1;
437 }
438 
439 /**
440  * vfio_populate_device - Allocate and populate MMIO region
441  * and IRQ structs according to driver returned information
442  * @vbasedev: the VFIO device handle
443  * @errp: error object
444  *
445  */
446 static int vfio_populate_device(VFIODevice *vbasedev, Error **errp)
447 {
448     VFIOINTp *intp, *tmp;
449     int i, ret = -1;
450     VFIOPlatformDevice *vdev =
451         container_of(vbasedev, VFIOPlatformDevice, vbasedev);
452 
453     if (!(vbasedev->flags & VFIO_DEVICE_FLAGS_PLATFORM)) {
454         error_setg(errp, "this isn't a platform device");
455         return ret;
456     }
457 
458     vdev->regions = g_new0(VFIORegion *, vbasedev->num_regions);
459 
460     for (i = 0; i < vbasedev->num_regions; i++) {
461         char *name = g_strdup_printf("VFIO %s region %d\n", vbasedev->name, i);
462 
463         vdev->regions[i] = g_new0(VFIORegion, 1);
464         ret = vfio_region_setup(OBJECT(vdev), vbasedev,
465                                 vdev->regions[i], i, name);
466         g_free(name);
467         if (ret) {
468             error_setg_errno(errp, -ret, "failed to get region %d info", i);
469             goto reg_error;
470         }
471     }
472 
473     vdev->mmap_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
474                                     vfio_intp_mmap_enable, vdev);
475 
476     QSIMPLEQ_INIT(&vdev->pending_intp_queue);
477 
478     for (i = 0; i < vbasedev->num_irqs; i++) {
479         struct vfio_irq_info irq = { .argsz = sizeof(irq) };
480 
481         irq.index = i;
482         ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
483         if (ret) {
484             error_setg_errno(errp, -ret, "failed to get device irq info");
485             goto irq_err;
486         } else {
487             trace_vfio_platform_populate_interrupts(irq.index,
488                                                     irq.count,
489                                                     irq.flags);
490             intp = vfio_init_intp(vbasedev, irq, errp);
491             if (!intp) {
492                 ret = -1;
493                 goto irq_err;
494             }
495         }
496     }
497     return 0;
498 irq_err:
499     timer_del(vdev->mmap_timer);
500     QLIST_FOREACH_SAFE(intp, &vdev->intp_list, next, tmp) {
501         QLIST_REMOVE(intp, next);
502         g_free(intp);
503     }
504 reg_error:
505     for (i = 0; i < vbasedev->num_regions; i++) {
506         if (vdev->regions[i]) {
507             vfio_region_finalize(vdev->regions[i]);
508         }
509         g_free(vdev->regions[i]);
510     }
511     g_free(vdev->regions);
512     return ret;
513 }
514 
515 /* specialized functions for VFIO Platform devices */
516 static VFIODeviceOps vfio_platform_ops = {
517     .vfio_compute_needs_reset = vfio_platform_compute_needs_reset,
518     .vfio_hot_reset_multi = vfio_platform_hot_reset_multi,
519     .vfio_eoi = vfio_platform_eoi,
520 };
521 
522 /**
523  * vfio_base_device_init - perform preliminary VFIO setup
524  * @vbasedev: the VFIO device handle
525  * @errp: error object
526  *
527  * Implement the VFIO command sequence that allows to discover
528  * assigned device resources: group extraction, device
529  * fd retrieval, resource query.
530  * Precondition: the device name must be initialized
531  */
532 static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp)
533 {
534     int ret;
535 
536     /* @fd takes precedence over @sysfsdev which takes precedence over @host */
537     if (vbasedev->fd < 0 && vbasedev->sysfsdev) {
538         g_free(vbasedev->name);
539         vbasedev->name = g_path_get_basename(vbasedev->sysfsdev);
540     } else if (vbasedev->fd < 0) {
541         if (!vbasedev->name || strchr(vbasedev->name, '/')) {
542             error_setg(errp, "wrong host device name");
543             return -EINVAL;
544         }
545 
546         vbasedev->sysfsdev = g_strdup_printf("/sys/bus/platform/devices/%s",
547                                              vbasedev->name);
548     }
549 
550     ret = vfio_device_get_name(vbasedev, errp);
551     if (ret) {
552         return ret;
553     }
554 
555     ret = vfio_attach_device(vbasedev->name, vbasedev,
556                              &address_space_memory, errp);
557     if (ret) {
558         return ret;
559     }
560 
561     ret = vfio_populate_device(vbasedev, errp);
562     if (ret) {
563         vfio_detach_device(vbasedev);
564     }
565 
566     return ret;
567 }
568 
569 /**
570  * vfio_platform_realize  - the device realize function
571  * @dev: device state pointer
572  * @errp: error
573  *
574  * initialize the device, its memory regions and IRQ structures
575  * IRQ are started separately
576  */
577 static void vfio_platform_realize(DeviceState *dev, Error **errp)
578 {
579     ERRP_GUARD();
580     VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(dev);
581     SysBusDevice *sbdev = SYS_BUS_DEVICE(dev);
582     VFIODevice *vbasedev = &vdev->vbasedev;
583     int i, ret;
584 
585     qemu_mutex_init(&vdev->intp_mutex);
586 
587     trace_vfio_platform_realize(vbasedev->sysfsdev ?
588                                 vbasedev->sysfsdev : vbasedev->name,
589                                 vdev->compat);
590 
591     ret = vfio_base_device_init(vbasedev, errp);
592     if (ret) {
593         goto out;
594     }
595 
596     if (!vdev->compat) {
597         GError *gerr = NULL;
598         gchar *contents;
599         gsize length;
600         char *path;
601 
602         path = g_strdup_printf("%s/of_node/compatible", vbasedev->sysfsdev);
603         if (!g_file_get_contents(path, &contents, &length, &gerr)) {
604             error_setg(errp, "%s", gerr->message);
605             g_error_free(gerr);
606             g_free(path);
607             return;
608         }
609         g_free(path);
610         vdev->compat = contents;
611         for (vdev->num_compat = 0; length; vdev->num_compat++) {
612             size_t skip = strlen(contents) + 1;
613             contents += skip;
614             length -= skip;
615         }
616     }
617 
618     for (i = 0; i < vbasedev->num_regions; i++) {
619         if (vfio_region_mmap(vdev->regions[i])) {
620             warn_report("%s mmap unsupported, performance may be slow",
621                         memory_region_name(vdev->regions[i]->mem));
622         }
623         sysbus_init_mmio(sbdev, vdev->regions[i]->mem);
624     }
625 out:
626     if (!ret) {
627         return;
628     }
629 
630     if (vdev->vbasedev.name) {
631         error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.name);
632     } else {
633         error_prepend(errp, "vfio error: ");
634     }
635 }
636 
637 static const VMStateDescription vfio_platform_vmstate = {
638     .name = "vfio-platform",
639     .unmigratable = 1,
640 };
641 
642 static Property vfio_platform_dev_properties[] = {
643     DEFINE_PROP_STRING("host", VFIOPlatformDevice, vbasedev.name),
644     DEFINE_PROP_STRING("sysfsdev", VFIOPlatformDevice, vbasedev.sysfsdev),
645     DEFINE_PROP_BOOL("x-no-mmap", VFIOPlatformDevice, vbasedev.no_mmap, false),
646     DEFINE_PROP_UINT32("mmap-timeout-ms", VFIOPlatformDevice,
647                        mmap_timeout, 1100),
648     DEFINE_PROP_BOOL("x-irqfd", VFIOPlatformDevice, irqfd_allowed, true),
649 #ifdef CONFIG_IOMMUFD
650     DEFINE_PROP_LINK("iommufd", VFIOPlatformDevice, vbasedev.iommufd,
651                      TYPE_IOMMUFD_BACKEND, IOMMUFDBackend *),
652 #endif
653     DEFINE_PROP_END_OF_LIST(),
654 };
655 
656 static void vfio_platform_instance_init(Object *obj)
657 {
658     VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(obj);
659     VFIODevice *vbasedev = &vdev->vbasedev;
660 
661     vfio_device_init(vbasedev, VFIO_DEVICE_TYPE_PLATFORM, &vfio_platform_ops,
662                      DEVICE(vdev), false);
663 }
664 
665 #ifdef CONFIG_IOMMUFD
666 static void vfio_platform_set_fd(Object *obj, const char *str, Error **errp)
667 {
668     vfio_device_set_fd(&VFIO_PLATFORM_DEVICE(obj)->vbasedev, str, errp);
669 }
670 #endif
671 
672 static void vfio_platform_class_init(ObjectClass *klass, void *data)
673 {
674     DeviceClass *dc = DEVICE_CLASS(klass);
675     SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
676 
677     dc->realize = vfio_platform_realize;
678     device_class_set_props(dc, vfio_platform_dev_properties);
679 #ifdef CONFIG_IOMMUFD
680     object_class_property_add_str(klass, "fd", NULL, vfio_platform_set_fd);
681 #endif
682     dc->vmsd = &vfio_platform_vmstate;
683     dc->desc = "VFIO-based platform device assignment";
684     sbc->connect_irq_notifier = vfio_start_irqfd_injection;
685     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
686     /* Supported by TYPE_VIRT_MACHINE */
687     dc->user_creatable = true;
688 }
689 
690 static const TypeInfo vfio_platform_dev_info = {
691     .name = TYPE_VFIO_PLATFORM,
692     .parent = TYPE_SYS_BUS_DEVICE,
693     .instance_size = sizeof(VFIOPlatformDevice),
694     .instance_init = vfio_platform_instance_init,
695     .class_init = vfio_platform_class_init,
696     .class_size = sizeof(VFIOPlatformDeviceClass),
697 };
698 
699 static void register_vfio_platform_dev_type(void)
700 {
701     type_register_static(&vfio_platform_dev_info);
702 }
703 
704 type_init(register_vfio_platform_dev_type)
705