2 * vfio based device assignment support - platform devices
4 * Copyright Linaro Limited, 2014
7 * Kim Phillips <kim.phillips@linaro.org>
8 * Eric Auger <eric.auger@linaro.org>
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
13 * Based on vfio based PCI device assignment support:
14 * Copyright Red Hat, Inc. 2012
17 #include "qemu/osdep.h"
18 #include "qapi/error.h"
19 #include <sys/ioctl.h>
20 #include <linux/vfio.h>
22 #include "hw/vfio/vfio-platform.h"
23 #include "migration/vmstate.h"
24 #include "qemu/error-report.h"
25 #include "qemu/main-loop.h"
26 #include "qemu/module.h"
27 #include "qemu/range.h"
28 #include "exec/memory.h"
29 #include "exec/address-spaces.h"
30 #include "qemu/queue.h"
31 #include "hw/sysbus.h"
34 #include "hw/platform-bus.h"
35 #include "hw/qdev-properties.h"
36 #include "sysemu/kvm.h"
39 * Functions used whatever the injection method
42 static inline bool vfio_irq_is_automasked(VFIOINTp
*intp
)
44 return intp
->flags
& VFIO_IRQ_INFO_AUTOMASKED
;
48 * vfio_init_intp - allocate, initialize the IRQ struct pointer
49 * and add it into the list of IRQs
50 * @vbasedev: the VFIO device handle
51 * @info: irq info struct retrieved from VFIO driver
54 static VFIOINTp
*vfio_init_intp(VFIODevice
*vbasedev
,
55 struct vfio_irq_info info
, Error
**errp
)
58 VFIOPlatformDevice
*vdev
=
59 container_of(vbasedev
, VFIOPlatformDevice
, vbasedev
);
60 SysBusDevice
*sbdev
= SYS_BUS_DEVICE(vdev
);
63 intp
= g_malloc0(sizeof(*intp
));
65 intp
->pin
= info
.index
;
66 intp
->flags
= info
.flags
;
67 intp
->state
= VFIO_IRQ_INACTIVE
;
68 intp
->kvm_accel
= false;
70 sysbus_init_irq(sbdev
, &intp
->qemuirq
);
72 /* Get an eventfd for trigger */
73 intp
->interrupt
= g_malloc0(sizeof(EventNotifier
));
74 ret
= event_notifier_init(intp
->interrupt
, 0);
76 g_free(intp
->interrupt
);
78 error_setg_errno(errp
, -ret
,
79 "failed to initialize trigger eventfd notifier");
82 if (vfio_irq_is_automasked(intp
)) {
83 /* Get an eventfd for resample/unmask */
84 intp
->unmask
= g_malloc0(sizeof(EventNotifier
));
85 ret
= event_notifier_init(intp
->unmask
, 0);
87 g_free(intp
->interrupt
);
90 error_setg_errno(errp
, -ret
,
91 "failed to initialize resample eventfd notifier");
96 QLIST_INSERT_HEAD(&vdev
->intp_list
, intp
, next
);
101 * vfio_set_trigger_eventfd - set VFIO eventfd handling
103 * @intp: IRQ struct handle
104 * @handler: handler to be called on eventfd signaling
106 * Setup VFIO signaling and attach an optional user-side handler
109 static int vfio_set_trigger_eventfd(VFIOINTp
*intp
,
110 eventfd_user_side_handler_t handler
)
112 VFIODevice
*vbasedev
= &intp
->vdev
->vbasedev
;
113 int32_t fd
= event_notifier_get_fd(intp
->interrupt
);
117 qemu_set_fd_handler(fd
, (IOHandler
*)handler
, NULL
, intp
);
119 ret
= vfio_set_irq_signaling(vbasedev
, intp
->pin
, 0,
120 VFIO_IRQ_SET_ACTION_TRIGGER
, fd
, &err
);
122 error_reportf_err(err
, VFIO_MSG_PREFIX
, vbasedev
->name
);
123 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
130 * Functions only used when eventfds are handled on user-side
135 * vfio_mmap_set_enabled - enable/disable the fast path mode
136 * @vdev: the VFIO platform device
137 * @enabled: the target mmap state
139 * enabled = true ~ fast path = MMIO region is mmaped (no KVM TRAP);
140 * enabled = false ~ slow path = MMIO region is trapped and region callbacks
141 * are called; slow path enables to trap the device IRQ status register reset
144 static void vfio_mmap_set_enabled(VFIOPlatformDevice
*vdev
, bool enabled
)
148 for (i
= 0; i
< vdev
->vbasedev
.num_regions
; i
++) {
149 vfio_region_mmaps_set_enabled(vdev
->regions
[i
], enabled
);
154 * vfio_intp_mmap_enable - timer function, restores the fast path
155 * if there is no more active IRQ
156 * @opaque: actually points to the VFIO platform device
158 * Called on mmap timer timout, this function checks whether the
159 * IRQ is still active and if not, restores the fast path.
160 * by construction a single eventfd is handled at a time.
161 * if the IRQ is still active, the timer is re-programmed.
163 static void vfio_intp_mmap_enable(void *opaque
)
166 VFIOPlatformDevice
*vdev
= (VFIOPlatformDevice
*)opaque
;
168 qemu_mutex_lock(&vdev
->intp_mutex
);
169 QLIST_FOREACH(tmp
, &vdev
->intp_list
, next
) {
170 if (tmp
->state
== VFIO_IRQ_ACTIVE
) {
171 trace_vfio_platform_intp_mmap_enable(tmp
->pin
);
172 /* re-program the timer to check active status later */
173 timer_mod(vdev
->mmap_timer
,
174 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
176 qemu_mutex_unlock(&vdev
->intp_mutex
);
180 vfio_mmap_set_enabled(vdev
, true);
181 qemu_mutex_unlock(&vdev
->intp_mutex
);
185 * vfio_intp_inject_pending_lockheld - Injects a pending IRQ
186 * @opaque: opaque pointer, in practice the VFIOINTp handle
188 * The function is called on a previous IRQ completion, from
189 * vfio_platform_eoi, while the intp_mutex is locked.
190 * Also in such situation, the slow path already is set and
191 * the mmap timer was already programmed.
193 static void vfio_intp_inject_pending_lockheld(VFIOINTp
*intp
)
195 trace_vfio_platform_intp_inject_pending_lockheld(intp
->pin
,
196 event_notifier_get_fd(intp
->interrupt
));
198 intp
->state
= VFIO_IRQ_ACTIVE
;
200 /* trigger the virtual IRQ */
201 qemu_set_irq(intp
->qemuirq
, 1);
205 * vfio_intp_interrupt - The user-side eventfd handler
206 * @opaque: opaque pointer which in practice is the VFIOINTp handle
208 * the function is entered in event handler context:
209 * the vIRQ is injected into the guest if there is no other active
212 static void vfio_intp_interrupt(VFIOINTp
*intp
)
216 VFIOPlatformDevice
*vdev
= intp
->vdev
;
217 bool delay_handling
= false;
219 qemu_mutex_lock(&vdev
->intp_mutex
);
220 if (intp
->state
== VFIO_IRQ_INACTIVE
) {
221 QLIST_FOREACH(tmp
, &vdev
->intp_list
, next
) {
222 if (tmp
->state
== VFIO_IRQ_ACTIVE
||
223 tmp
->state
== VFIO_IRQ_PENDING
) {
224 delay_handling
= true;
229 if (delay_handling
) {
231 * the new IRQ gets a pending status and is pushed in
234 intp
->state
= VFIO_IRQ_PENDING
;
235 trace_vfio_intp_interrupt_set_pending(intp
->pin
);
236 QSIMPLEQ_INSERT_TAIL(&vdev
->pending_intp_queue
,
238 ret
= event_notifier_test_and_clear(intp
->interrupt
);
239 qemu_mutex_unlock(&vdev
->intp_mutex
);
243 trace_vfio_platform_intp_interrupt(intp
->pin
,
244 event_notifier_get_fd(intp
->interrupt
));
246 ret
= event_notifier_test_and_clear(intp
->interrupt
);
248 error_report("Error when clearing fd=%d (ret = %d)",
249 event_notifier_get_fd(intp
->interrupt
), ret
);
252 intp
->state
= VFIO_IRQ_ACTIVE
;
255 vfio_mmap_set_enabled(vdev
, false);
257 /* trigger the virtual IRQ */
258 qemu_set_irq(intp
->qemuirq
, 1);
261 * Schedule the mmap timer which will restore fastpath when no IRQ
264 if (vdev
->mmap_timeout
) {
265 timer_mod(vdev
->mmap_timer
,
266 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
269 qemu_mutex_unlock(&vdev
->intp_mutex
);
273 * vfio_platform_eoi - IRQ completion routine
274 * @vbasedev: the VFIO device handle
276 * De-asserts the active virtual IRQ and unmasks the physical IRQ
277 * (effective for level sensitive IRQ auto-masked by the VFIO driver).
278 * Then it handles next pending IRQ if any.
279 * eoi function is called on the first access to any MMIO region
280 * after an IRQ was triggered, trapped since slow path was set.
281 * It is assumed this access corresponds to the IRQ status
282 * register reset. With such a mechanism, a single IRQ can be
283 * handled at a time since there is no way to know which IRQ
284 * was completed by the guest (we would need additional details
285 * about the IRQ status register mask).
287 static void vfio_platform_eoi(VFIODevice
*vbasedev
)
290 VFIOPlatformDevice
*vdev
=
291 container_of(vbasedev
, VFIOPlatformDevice
, vbasedev
);
293 qemu_mutex_lock(&vdev
->intp_mutex
);
294 QLIST_FOREACH(intp
, &vdev
->intp_list
, next
) {
295 if (intp
->state
== VFIO_IRQ_ACTIVE
) {
296 trace_vfio_platform_eoi(intp
->pin
,
297 event_notifier_get_fd(intp
->interrupt
));
298 intp
->state
= VFIO_IRQ_INACTIVE
;
300 /* deassert the virtual IRQ */
301 qemu_set_irq(intp
->qemuirq
, 0);
303 if (vfio_irq_is_automasked(intp
)) {
304 /* unmasks the physical level-sensitive IRQ */
305 vfio_unmask_single_irqindex(vbasedev
, intp
->pin
);
308 /* a single IRQ can be active at a time */
312 /* in case there are pending IRQs, handle the first one */
313 if (!QSIMPLEQ_EMPTY(&vdev
->pending_intp_queue
)) {
314 intp
= QSIMPLEQ_FIRST(&vdev
->pending_intp_queue
);
315 vfio_intp_inject_pending_lockheld(intp
);
316 QSIMPLEQ_REMOVE_HEAD(&vdev
->pending_intp_queue
, pqnext
);
318 qemu_mutex_unlock(&vdev
->intp_mutex
);
322 * vfio_start_eventfd_injection - starts the virtual IRQ injection using
323 * user-side handled eventfds
324 * @sbdev: the sysbus device handle
325 * @irq: the qemu irq handle
328 static void vfio_start_eventfd_injection(SysBusDevice
*sbdev
, qemu_irq irq
)
330 VFIOPlatformDevice
*vdev
= VFIO_PLATFORM_DEVICE(sbdev
);
333 QLIST_FOREACH(intp
, &vdev
->intp_list
, next
) {
334 if (intp
->qemuirq
== irq
) {
340 if (vfio_set_trigger_eventfd(intp
, vfio_intp_interrupt
)) {
346 * Functions used for irqfd
350 * vfio_set_resample_eventfd - sets the resamplefd for an IRQ
351 * @intp: the IRQ struct handle
352 * programs the VFIO driver to unmask this IRQ when the
353 * intp->unmask eventfd is triggered
355 static int vfio_set_resample_eventfd(VFIOINTp
*intp
)
357 int32_t fd
= event_notifier_get_fd(intp
->unmask
);
358 VFIODevice
*vbasedev
= &intp
->vdev
->vbasedev
;
362 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
363 ret
= vfio_set_irq_signaling(vbasedev
, intp
->pin
, 0,
364 VFIO_IRQ_SET_ACTION_UNMASK
, fd
, &err
);
366 error_reportf_err(err
, VFIO_MSG_PREFIX
, vbasedev
->name
);
372 * vfio_start_irqfd_injection - starts the virtual IRQ injection using
375 * @sbdev: the sysbus device handle
376 * @irq: the qemu irq handle
378 * In case the irqfd setup fails, we fallback to userspace handled eventfd
380 static void vfio_start_irqfd_injection(SysBusDevice
*sbdev
, qemu_irq irq
)
382 VFIOPlatformDevice
*vdev
= VFIO_PLATFORM_DEVICE(sbdev
);
385 if (!kvm_irqfds_enabled() || !kvm_resamplefds_enabled() ||
386 !vdev
->irqfd_allowed
) {
390 QLIST_FOREACH(intp
, &vdev
->intp_list
, next
) {
391 if (intp
->qemuirq
== irq
) {
397 if (kvm_irqchip_add_irqfd_notifier(kvm_state
, intp
->interrupt
,
398 intp
->unmask
, irq
) < 0) {
402 if (vfio_set_trigger_eventfd(intp
, NULL
) < 0) {
405 if (vfio_irq_is_automasked(intp
)) {
406 if (vfio_set_resample_eventfd(intp
) < 0) {
409 trace_vfio_platform_start_level_irqfd_injection(intp
->pin
,
410 event_notifier_get_fd(intp
->interrupt
),
411 event_notifier_get_fd(intp
->unmask
));
413 trace_vfio_platform_start_edge_irqfd_injection(intp
->pin
,
414 event_notifier_get_fd(intp
->interrupt
));
417 intp
->kvm_accel
= true;
421 kvm_irqchip_remove_irqfd_notifier(kvm_state
, intp
->interrupt
, irq
);
424 vfio_start_eventfd_injection(sbdev
, irq
);
430 static void vfio_platform_compute_needs_reset(VFIODevice
*vbasedev
)
432 vbasedev
->needs_reset
= true;
435 /* not implemented yet */
436 static int vfio_platform_hot_reset_multi(VFIODevice
*vbasedev
)
442 * vfio_populate_device - Allocate and populate MMIO region
443 * and IRQ structs according to driver returned information
444 * @vbasedev: the VFIO device handle
445 * @errp: error object
448 static int vfio_populate_device(VFIODevice
*vbasedev
, Error
**errp
)
450 VFIOINTp
*intp
, *tmp
;
452 VFIOPlatformDevice
*vdev
=
453 container_of(vbasedev
, VFIOPlatformDevice
, vbasedev
);
455 if (!(vbasedev
->flags
& VFIO_DEVICE_FLAGS_PLATFORM
)) {
456 error_setg(errp
, "this isn't a platform device");
460 vdev
->regions
= g_new0(VFIORegion
*, vbasedev
->num_regions
);
462 for (i
= 0; i
< vbasedev
->num_regions
; i
++) {
463 char *name
= g_strdup_printf("VFIO %s region %d\n", vbasedev
->name
, i
);
465 vdev
->regions
[i
] = g_new0(VFIORegion
, 1);
466 ret
= vfio_region_setup(OBJECT(vdev
), vbasedev
,
467 vdev
->regions
[i
], i
, name
);
470 error_setg_errno(errp
, -ret
, "failed to get region %d info", i
);
475 vdev
->mmap_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
476 vfio_intp_mmap_enable
, vdev
);
478 QSIMPLEQ_INIT(&vdev
->pending_intp_queue
);
480 for (i
= 0; i
< vbasedev
->num_irqs
; i
++) {
481 struct vfio_irq_info irq
= { .argsz
= sizeof(irq
) };
484 ret
= ioctl(vbasedev
->fd
, VFIO_DEVICE_GET_IRQ_INFO
, &irq
);
486 error_setg_errno(errp
, -ret
, "failed to get device irq info");
489 trace_vfio_platform_populate_interrupts(irq
.index
,
492 intp
= vfio_init_intp(vbasedev
, irq
, errp
);
501 timer_del(vdev
->mmap_timer
);
502 QLIST_FOREACH_SAFE(intp
, &vdev
->intp_list
, next
, tmp
) {
503 QLIST_REMOVE(intp
, next
);
507 for (i
= 0; i
< vbasedev
->num_regions
; i
++) {
508 if (vdev
->regions
[i
]) {
509 vfio_region_finalize(vdev
->regions
[i
]);
511 g_free(vdev
->regions
[i
]);
513 g_free(vdev
->regions
);
517 /* specialized functions for VFIO Platform devices */
518 static VFIODeviceOps vfio_platform_ops
= {
519 .vfio_compute_needs_reset
= vfio_platform_compute_needs_reset
,
520 .vfio_hot_reset_multi
= vfio_platform_hot_reset_multi
,
521 .vfio_eoi
= vfio_platform_eoi
,
525 * vfio_base_device_init - perform preliminary VFIO setup
526 * @vbasedev: the VFIO device handle
527 * @errp: error object
529 * Implement the VFIO command sequence that allows to discover
530 * assigned device resources: group extraction, device
531 * fd retrieval, resource query.
532 * Precondition: the device name must be initialized
534 static int vfio_base_device_init(VFIODevice
*vbasedev
, Error
**errp
)
537 VFIODevice
*vbasedev_iter
;
538 char *tmp
, group_path
[PATH_MAX
], *group_name
;
544 /* @sysfsdev takes precedence over @host */
545 if (vbasedev
->sysfsdev
) {
546 g_free(vbasedev
->name
);
547 vbasedev
->name
= g_path_get_basename(vbasedev
->sysfsdev
);
549 if (!vbasedev
->name
|| strchr(vbasedev
->name
, '/')) {
550 error_setg(errp
, "wrong host device name");
554 vbasedev
->sysfsdev
= g_strdup_printf("/sys/bus/platform/devices/%s",
558 if (stat(vbasedev
->sysfsdev
, &st
) < 0) {
559 error_setg_errno(errp
, errno
,
560 "failed to get the sysfs host device file status");
564 tmp
= g_strdup_printf("%s/iommu_group", vbasedev
->sysfsdev
);
565 len
= readlink(tmp
, group_path
, sizeof(group_path
));
568 if (len
< 0 || len
>= sizeof(group_path
)) {
569 ret
= len
< 0 ? -errno
: -ENAMETOOLONG
;
570 error_setg_errno(errp
, -ret
, "no iommu_group found");
576 group_name
= basename(group_path
);
577 if (sscanf(group_name
, "%d", &groupid
) != 1) {
578 error_setg_errno(errp
, errno
, "failed to read %s", group_path
);
582 trace_vfio_platform_base_device_init(vbasedev
->name
, groupid
);
584 group
= vfio_get_group(groupid
, &address_space_memory
, errp
);
589 QLIST_FOREACH(vbasedev_iter
, &group
->device_list
, next
) {
590 if (strcmp(vbasedev_iter
->name
, vbasedev
->name
) == 0) {
591 error_setg(errp
, "device is already attached");
592 vfio_put_group(group
);
596 ret
= vfio_get_device(group
, vbasedev
->name
, vbasedev
, errp
);
598 vfio_put_group(group
);
602 ret
= vfio_populate_device(vbasedev
, errp
);
604 vfio_put_group(group
);
611 * vfio_platform_realize - the device realize function
612 * @dev: device state pointer
615 * initialize the device, its memory regions and IRQ structures
616 * IRQ are started separately
618 static void vfio_platform_realize(DeviceState
*dev
, Error
**errp
)
620 VFIOPlatformDevice
*vdev
= VFIO_PLATFORM_DEVICE(dev
);
621 SysBusDevice
*sbdev
= SYS_BUS_DEVICE(dev
);
622 VFIODevice
*vbasedev
= &vdev
->vbasedev
;
625 vbasedev
->type
= VFIO_DEVICE_TYPE_PLATFORM
;
627 vbasedev
->ops
= &vfio_platform_ops
;
629 qemu_mutex_init(&vdev
->intp_mutex
);
631 trace_vfio_platform_realize(vbasedev
->sysfsdev
?
632 vbasedev
->sysfsdev
: vbasedev
->name
,
635 ret
= vfio_base_device_init(vbasedev
, errp
);
646 path
= g_strdup_printf("%s/of_node/compatible", vbasedev
->sysfsdev
);
647 if (!g_file_get_contents(path
, &contents
, &length
, &gerr
)) {
648 error_setg(errp
, "%s", gerr
->message
);
654 vdev
->compat
= contents
;
655 for (vdev
->num_compat
= 0; length
; vdev
->num_compat
++) {
656 size_t skip
= strlen(contents
) + 1;
662 for (i
= 0; i
< vbasedev
->num_regions
; i
++) {
663 if (vfio_region_mmap(vdev
->regions
[i
])) {
664 warn_report("%s mmap unsupported, performance may be slow",
665 memory_region_name(vdev
->regions
[i
]->mem
));
667 sysbus_init_mmio(sbdev
, vdev
->regions
[i
]->mem
);
674 if (vdev
->vbasedev
.name
) {
675 error_prepend(errp
, VFIO_MSG_PREFIX
, vdev
->vbasedev
.name
);
677 error_prepend(errp
, "vfio error: ");
681 static const VMStateDescription vfio_platform_vmstate
= {
682 .name
= "vfio-platform",
686 static Property vfio_platform_dev_properties
[] = {
687 DEFINE_PROP_STRING("host", VFIOPlatformDevice
, vbasedev
.name
),
688 DEFINE_PROP_STRING("sysfsdev", VFIOPlatformDevice
, vbasedev
.sysfsdev
),
689 DEFINE_PROP_BOOL("x-no-mmap", VFIOPlatformDevice
, vbasedev
.no_mmap
, false),
690 DEFINE_PROP_UINT32("mmap-timeout-ms", VFIOPlatformDevice
,
692 DEFINE_PROP_BOOL("x-irqfd", VFIOPlatformDevice
, irqfd_allowed
, true),
693 DEFINE_PROP_END_OF_LIST(),
696 static void vfio_platform_class_init(ObjectClass
*klass
, void *data
)
698 DeviceClass
*dc
= DEVICE_CLASS(klass
);
699 SysBusDeviceClass
*sbc
= SYS_BUS_DEVICE_CLASS(klass
);
701 dc
->realize
= vfio_platform_realize
;
702 dc
->props
= vfio_platform_dev_properties
;
703 dc
->vmsd
= &vfio_platform_vmstate
;
704 dc
->desc
= "VFIO-based platform device assignment";
705 sbc
->connect_irq_notifier
= vfio_start_irqfd_injection
;
706 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
707 /* Supported by TYPE_VIRT_MACHINE */
708 dc
->user_creatable
= true;
711 static const TypeInfo vfio_platform_dev_info
= {
712 .name
= TYPE_VFIO_PLATFORM
,
713 .parent
= TYPE_SYS_BUS_DEVICE
,
714 .instance_size
= sizeof(VFIOPlatformDevice
),
715 .class_init
= vfio_platform_class_init
,
716 .class_size
= sizeof(VFIOPlatformDeviceClass
),
719 static void register_vfio_platform_dev_type(void)
721 type_register_static(&vfio_platform_dev_info
);
724 type_init(register_vfio_platform_dev_type
)