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 "qemu/error-report.h"
24 #include "qemu/module.h"
25 #include "qemu/range.h"
26 #include "sysemu/sysemu.h"
27 #include "exec/memory.h"
28 #include "exec/address-spaces.h"
29 #include "qemu/queue.h"
30 #include "hw/sysbus.h"
32 #include "hw/platform-bus.h"
33 #include "sysemu/kvm.h"
36 * Functions used whatever the injection method
39 static inline bool vfio_irq_is_automasked(VFIOINTp
*intp
)
41 return intp
->flags
& VFIO_IRQ_INFO_AUTOMASKED
;
45 * vfio_init_intp - allocate, initialize the IRQ struct pointer
46 * and add it into the list of IRQs
47 * @vbasedev: the VFIO device handle
48 * @info: irq info struct retrieved from VFIO driver
51 static VFIOINTp
*vfio_init_intp(VFIODevice
*vbasedev
,
52 struct vfio_irq_info info
, Error
**errp
)
55 VFIOPlatformDevice
*vdev
=
56 container_of(vbasedev
, VFIOPlatformDevice
, vbasedev
);
57 SysBusDevice
*sbdev
= SYS_BUS_DEVICE(vdev
);
60 intp
= g_malloc0(sizeof(*intp
));
62 intp
->pin
= info
.index
;
63 intp
->flags
= info
.flags
;
64 intp
->state
= VFIO_IRQ_INACTIVE
;
65 intp
->kvm_accel
= false;
67 sysbus_init_irq(sbdev
, &intp
->qemuirq
);
69 /* Get an eventfd for trigger */
70 intp
->interrupt
= g_malloc0(sizeof(EventNotifier
));
71 ret
= event_notifier_init(intp
->interrupt
, 0);
73 g_free(intp
->interrupt
);
75 error_setg_errno(errp
, -ret
,
76 "failed to initialize trigger eventfd notifier");
79 if (vfio_irq_is_automasked(intp
)) {
80 /* Get an eventfd for resample/unmask */
81 intp
->unmask
= g_malloc0(sizeof(EventNotifier
));
82 ret
= event_notifier_init(intp
->unmask
, 0);
84 g_free(intp
->interrupt
);
87 error_setg_errno(errp
, -ret
,
88 "failed to initialize resample eventfd notifier");
93 QLIST_INSERT_HEAD(&vdev
->intp_list
, intp
, next
);
98 * vfio_set_trigger_eventfd - set VFIO eventfd handling
100 * @intp: IRQ struct handle
101 * @handler: handler to be called on eventfd signaling
103 * Setup VFIO signaling and attach an optional user-side handler
106 static int vfio_set_trigger_eventfd(VFIOINTp
*intp
,
107 eventfd_user_side_handler_t handler
)
109 VFIODevice
*vbasedev
= &intp
->vdev
->vbasedev
;
110 int32_t fd
= event_notifier_get_fd(intp
->interrupt
);
114 qemu_set_fd_handler(fd
, (IOHandler
*)handler
, NULL
, intp
);
116 ret
= vfio_set_irq_signaling(vbasedev
, intp
->pin
, 0,
117 VFIO_IRQ_SET_ACTION_TRIGGER
, fd
, &err
);
119 error_reportf_err(err
, VFIO_MSG_PREFIX
, vbasedev
->name
);
120 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
127 * Functions only used when eventfds are handled on user-side
132 * vfio_mmap_set_enabled - enable/disable the fast path mode
133 * @vdev: the VFIO platform device
134 * @enabled: the target mmap state
136 * enabled = true ~ fast path = MMIO region is mmaped (no KVM TRAP);
137 * enabled = false ~ slow path = MMIO region is trapped and region callbacks
138 * are called; slow path enables to trap the device IRQ status register reset
141 static void vfio_mmap_set_enabled(VFIOPlatformDevice
*vdev
, bool enabled
)
145 for (i
= 0; i
< vdev
->vbasedev
.num_regions
; i
++) {
146 vfio_region_mmaps_set_enabled(vdev
->regions
[i
], enabled
);
151 * vfio_intp_mmap_enable - timer function, restores the fast path
152 * if there is no more active IRQ
153 * @opaque: actually points to the VFIO platform device
155 * Called on mmap timer timout, this function checks whether the
156 * IRQ is still active and if not, restores the fast path.
157 * by construction a single eventfd is handled at a time.
158 * if the IRQ is still active, the timer is re-programmed.
160 static void vfio_intp_mmap_enable(void *opaque
)
163 VFIOPlatformDevice
*vdev
= (VFIOPlatformDevice
*)opaque
;
165 qemu_mutex_lock(&vdev
->intp_mutex
);
166 QLIST_FOREACH(tmp
, &vdev
->intp_list
, next
) {
167 if (tmp
->state
== VFIO_IRQ_ACTIVE
) {
168 trace_vfio_platform_intp_mmap_enable(tmp
->pin
);
169 /* re-program the timer to check active status later */
170 timer_mod(vdev
->mmap_timer
,
171 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
173 qemu_mutex_unlock(&vdev
->intp_mutex
);
177 vfio_mmap_set_enabled(vdev
, true);
178 qemu_mutex_unlock(&vdev
->intp_mutex
);
182 * vfio_intp_inject_pending_lockheld - Injects a pending IRQ
183 * @opaque: opaque pointer, in practice the VFIOINTp handle
185 * The function is called on a previous IRQ completion, from
186 * vfio_platform_eoi, while the intp_mutex is locked.
187 * Also in such situation, the slow path already is set and
188 * the mmap timer was already programmed.
190 static void vfio_intp_inject_pending_lockheld(VFIOINTp
*intp
)
192 trace_vfio_platform_intp_inject_pending_lockheld(intp
->pin
,
193 event_notifier_get_fd(intp
->interrupt
));
195 intp
->state
= VFIO_IRQ_ACTIVE
;
197 /* trigger the virtual IRQ */
198 qemu_set_irq(intp
->qemuirq
, 1);
202 * vfio_intp_interrupt - The user-side eventfd handler
203 * @opaque: opaque pointer which in practice is the VFIOINTp handle
205 * the function is entered in event handler context:
206 * the vIRQ is injected into the guest if there is no other active
209 static void vfio_intp_interrupt(VFIOINTp
*intp
)
213 VFIOPlatformDevice
*vdev
= intp
->vdev
;
214 bool delay_handling
= false;
216 qemu_mutex_lock(&vdev
->intp_mutex
);
217 if (intp
->state
== VFIO_IRQ_INACTIVE
) {
218 QLIST_FOREACH(tmp
, &vdev
->intp_list
, next
) {
219 if (tmp
->state
== VFIO_IRQ_ACTIVE
||
220 tmp
->state
== VFIO_IRQ_PENDING
) {
221 delay_handling
= true;
226 if (delay_handling
) {
228 * the new IRQ gets a pending status and is pushed in
231 intp
->state
= VFIO_IRQ_PENDING
;
232 trace_vfio_intp_interrupt_set_pending(intp
->pin
);
233 QSIMPLEQ_INSERT_TAIL(&vdev
->pending_intp_queue
,
235 ret
= event_notifier_test_and_clear(intp
->interrupt
);
236 qemu_mutex_unlock(&vdev
->intp_mutex
);
240 trace_vfio_platform_intp_interrupt(intp
->pin
,
241 event_notifier_get_fd(intp
->interrupt
));
243 ret
= event_notifier_test_and_clear(intp
->interrupt
);
245 error_report("Error when clearing fd=%d (ret = %d)",
246 event_notifier_get_fd(intp
->interrupt
), ret
);
249 intp
->state
= VFIO_IRQ_ACTIVE
;
252 vfio_mmap_set_enabled(vdev
, false);
254 /* trigger the virtual IRQ */
255 qemu_set_irq(intp
->qemuirq
, 1);
258 * Schedule the mmap timer which will restore fastpath when no IRQ
261 if (vdev
->mmap_timeout
) {
262 timer_mod(vdev
->mmap_timer
,
263 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
266 qemu_mutex_unlock(&vdev
->intp_mutex
);
270 * vfio_platform_eoi - IRQ completion routine
271 * @vbasedev: the VFIO device handle
273 * De-asserts the active virtual IRQ and unmasks the physical IRQ
274 * (effective for level sensitive IRQ auto-masked by the VFIO driver).
275 * Then it handles next pending IRQ if any.
276 * eoi function is called on the first access to any MMIO region
277 * after an IRQ was triggered, trapped since slow path was set.
278 * It is assumed this access corresponds to the IRQ status
279 * register reset. With such a mechanism, a single IRQ can be
280 * handled at a time since there is no way to know which IRQ
281 * was completed by the guest (we would need additional details
282 * about the IRQ status register mask).
284 static void vfio_platform_eoi(VFIODevice
*vbasedev
)
287 VFIOPlatformDevice
*vdev
=
288 container_of(vbasedev
, VFIOPlatformDevice
, vbasedev
);
290 qemu_mutex_lock(&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
;
297 /* deassert the virtual IRQ */
298 qemu_set_irq(intp
->qemuirq
, 0);
300 if (vfio_irq_is_automasked(intp
)) {
301 /* unmasks the physical level-sensitive IRQ */
302 vfio_unmask_single_irqindex(vbasedev
, intp
->pin
);
305 /* a single IRQ can be active at a time */
309 /* in case there are pending IRQs, handle the first one */
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
);
315 qemu_mutex_unlock(&vdev
->intp_mutex
);
319 * vfio_start_eventfd_injection - starts the virtual IRQ injection using
320 * user-side handled eventfds
321 * @sbdev: the sysbus device handle
322 * @irq: the qemu irq handle
325 static void vfio_start_eventfd_injection(SysBusDevice
*sbdev
, qemu_irq irq
)
327 VFIOPlatformDevice
*vdev
= VFIO_PLATFORM_DEVICE(sbdev
);
330 QLIST_FOREACH(intp
, &vdev
->intp_list
, next
) {
331 if (intp
->qemuirq
== irq
) {
337 if (vfio_set_trigger_eventfd(intp
, vfio_intp_interrupt
)) {
343 * Functions used for irqfd
347 * vfio_set_resample_eventfd - sets the resamplefd for an IRQ
348 * @intp: the IRQ struct handle
349 * programs the VFIO driver to unmask this IRQ when the
350 * intp->unmask eventfd is triggered
352 static int vfio_set_resample_eventfd(VFIOINTp
*intp
)
354 int32_t fd
= event_notifier_get_fd(intp
->unmask
);
355 VFIODevice
*vbasedev
= &intp
->vdev
->vbasedev
;
359 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
360 ret
= vfio_set_irq_signaling(vbasedev
, intp
->pin
, 0,
361 VFIO_IRQ_SET_ACTION_UNMASK
, fd
, &err
);
363 error_reportf_err(err
, VFIO_MSG_PREFIX
, vbasedev
->name
);
369 * vfio_start_irqfd_injection - starts the virtual IRQ injection using
372 * @sbdev: the sysbus device handle
373 * @irq: the qemu irq handle
375 * In case the irqfd setup fails, we fallback to userspace handled eventfd
377 static void vfio_start_irqfd_injection(SysBusDevice
*sbdev
, qemu_irq irq
)
379 VFIOPlatformDevice
*vdev
= VFIO_PLATFORM_DEVICE(sbdev
);
382 if (!kvm_irqfds_enabled() || !kvm_resamplefds_enabled() ||
383 !vdev
->irqfd_allowed
) {
387 QLIST_FOREACH(intp
, &vdev
->intp_list
, next
) {
388 if (intp
->qemuirq
== irq
) {
394 if (kvm_irqchip_add_irqfd_notifier(kvm_state
, intp
->interrupt
,
395 intp
->unmask
, irq
) < 0) {
399 if (vfio_set_trigger_eventfd(intp
, NULL
) < 0) {
402 if (vfio_irq_is_automasked(intp
)) {
403 if (vfio_set_resample_eventfd(intp
) < 0) {
406 trace_vfio_platform_start_level_irqfd_injection(intp
->pin
,
407 event_notifier_get_fd(intp
->interrupt
),
408 event_notifier_get_fd(intp
->unmask
));
410 trace_vfio_platform_start_edge_irqfd_injection(intp
->pin
,
411 event_notifier_get_fd(intp
->interrupt
));
414 intp
->kvm_accel
= true;
418 kvm_irqchip_remove_irqfd_notifier(kvm_state
, intp
->interrupt
, irq
);
421 vfio_start_eventfd_injection(sbdev
, irq
);
427 static void vfio_platform_compute_needs_reset(VFIODevice
*vbasedev
)
429 vbasedev
->needs_reset
= true;
432 /* not implemented yet */
433 static int vfio_platform_hot_reset_multi(VFIODevice
*vbasedev
)
439 * vfio_populate_device - Allocate and populate MMIO region
440 * and IRQ structs according to driver returned information
441 * @vbasedev: the VFIO device handle
442 * @errp: error object
445 static int vfio_populate_device(VFIODevice
*vbasedev
, Error
**errp
)
447 VFIOINTp
*intp
, *tmp
;
449 VFIOPlatformDevice
*vdev
=
450 container_of(vbasedev
, VFIOPlatformDevice
, vbasedev
);
452 if (!(vbasedev
->flags
& VFIO_DEVICE_FLAGS_PLATFORM
)) {
453 error_setg(errp
, "this isn't a platform device");
457 vdev
->regions
= g_new0(VFIORegion
*, vbasedev
->num_regions
);
459 for (i
= 0; i
< vbasedev
->num_regions
; i
++) {
460 char *name
= g_strdup_printf("VFIO %s region %d\n", vbasedev
->name
, i
);
462 vdev
->regions
[i
] = g_new0(VFIORegion
, 1);
463 ret
= vfio_region_setup(OBJECT(vdev
), vbasedev
,
464 vdev
->regions
[i
], i
, name
);
467 error_setg_errno(errp
, -ret
, "failed to get region %d info", i
);
472 vdev
->mmap_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
473 vfio_intp_mmap_enable
, vdev
);
475 QSIMPLEQ_INIT(&vdev
->pending_intp_queue
);
477 for (i
= 0; i
< vbasedev
->num_irqs
; i
++) {
478 struct vfio_irq_info irq
= { .argsz
= sizeof(irq
) };
481 ret
= ioctl(vbasedev
->fd
, VFIO_DEVICE_GET_IRQ_INFO
, &irq
);
483 error_setg_errno(errp
, -ret
, "failed to get device irq info");
486 trace_vfio_platform_populate_interrupts(irq
.index
,
489 intp
= vfio_init_intp(vbasedev
, irq
, errp
);
498 timer_del(vdev
->mmap_timer
);
499 QLIST_FOREACH_SAFE(intp
, &vdev
->intp_list
, next
, tmp
) {
500 QLIST_REMOVE(intp
, next
);
504 for (i
= 0; i
< vbasedev
->num_regions
; i
++) {
505 if (vdev
->regions
[i
]) {
506 vfio_region_finalize(vdev
->regions
[i
]);
508 g_free(vdev
->regions
[i
]);
510 g_free(vdev
->regions
);
514 /* specialized functions for VFIO Platform devices */
515 static VFIODeviceOps vfio_platform_ops
= {
516 .vfio_compute_needs_reset
= vfio_platform_compute_needs_reset
,
517 .vfio_hot_reset_multi
= vfio_platform_hot_reset_multi
,
518 .vfio_eoi
= vfio_platform_eoi
,
522 * vfio_base_device_init - perform preliminary VFIO setup
523 * @vbasedev: the VFIO device handle
524 * @errp: error object
526 * Implement the VFIO command sequence that allows to discover
527 * assigned device resources: group extraction, device
528 * fd retrieval, resource query.
529 * Precondition: the device name must be initialized
531 static int vfio_base_device_init(VFIODevice
*vbasedev
, Error
**errp
)
534 VFIODevice
*vbasedev_iter
;
535 char *tmp
, group_path
[PATH_MAX
], *group_name
;
541 /* @sysfsdev takes precedence over @host */
542 if (vbasedev
->sysfsdev
) {
543 g_free(vbasedev
->name
);
544 vbasedev
->name
= g_path_get_basename(vbasedev
->sysfsdev
);
546 if (!vbasedev
->name
|| strchr(vbasedev
->name
, '/')) {
547 error_setg(errp
, "wrong host device name");
551 vbasedev
->sysfsdev
= g_strdup_printf("/sys/bus/platform/devices/%s",
555 if (stat(vbasedev
->sysfsdev
, &st
) < 0) {
556 error_setg_errno(errp
, errno
,
557 "failed to get the sysfs host device file status");
561 tmp
= g_strdup_printf("%s/iommu_group", vbasedev
->sysfsdev
);
562 len
= readlink(tmp
, group_path
, sizeof(group_path
));
565 if (len
< 0 || len
>= sizeof(group_path
)) {
566 ret
= len
< 0 ? -errno
: -ENAMETOOLONG
;
567 error_setg_errno(errp
, -ret
, "no iommu_group found");
573 group_name
= basename(group_path
);
574 if (sscanf(group_name
, "%d", &groupid
) != 1) {
575 error_setg_errno(errp
, errno
, "failed to read %s", group_path
);
579 trace_vfio_platform_base_device_init(vbasedev
->name
, groupid
);
581 group
= vfio_get_group(groupid
, &address_space_memory
, errp
);
586 QLIST_FOREACH(vbasedev_iter
, &group
->device_list
, next
) {
587 if (strcmp(vbasedev_iter
->name
, vbasedev
->name
) == 0) {
588 error_setg(errp
, "device is already attached");
589 vfio_put_group(group
);
593 ret
= vfio_get_device(group
, vbasedev
->name
, vbasedev
, errp
);
595 vfio_put_group(group
);
599 ret
= vfio_populate_device(vbasedev
, errp
);
601 vfio_put_group(group
);
608 * vfio_platform_realize - the device realize function
609 * @dev: device state pointer
612 * initialize the device, its memory regions and IRQ structures
613 * IRQ are started separately
615 static void vfio_platform_realize(DeviceState
*dev
, Error
**errp
)
617 VFIOPlatformDevice
*vdev
= VFIO_PLATFORM_DEVICE(dev
);
618 SysBusDevice
*sbdev
= SYS_BUS_DEVICE(dev
);
619 VFIODevice
*vbasedev
= &vdev
->vbasedev
;
622 vbasedev
->type
= VFIO_DEVICE_TYPE_PLATFORM
;
624 vbasedev
->ops
= &vfio_platform_ops
;
626 qemu_mutex_init(&vdev
->intp_mutex
);
628 trace_vfio_platform_realize(vbasedev
->sysfsdev
?
629 vbasedev
->sysfsdev
: vbasedev
->name
,
632 ret
= vfio_base_device_init(vbasedev
, errp
);
643 path
= g_strdup_printf("%s/of_node/compatible", vbasedev
->sysfsdev
);
644 if (!g_file_get_contents(path
, &contents
, &length
, &gerr
)) {
645 error_setg(errp
, "%s", gerr
->message
);
651 vdev
->compat
= contents
;
652 for (vdev
->num_compat
= 0; length
; vdev
->num_compat
++) {
653 size_t skip
= strlen(contents
) + 1;
659 for (i
= 0; i
< vbasedev
->num_regions
; i
++) {
660 if (vfio_region_mmap(vdev
->regions
[i
])) {
661 warn_report("%s mmap unsupported, performance may be slow",
662 memory_region_name(vdev
->regions
[i
]->mem
));
664 sysbus_init_mmio(sbdev
, vdev
->regions
[i
]->mem
);
671 if (vdev
->vbasedev
.name
) {
672 error_prepend(errp
, VFIO_MSG_PREFIX
, vdev
->vbasedev
.name
);
674 error_prepend(errp
, "vfio error: ");
678 static const VMStateDescription vfio_platform_vmstate
= {
679 .name
= "vfio-platform",
683 static Property vfio_platform_dev_properties
[] = {
684 DEFINE_PROP_STRING("host", VFIOPlatformDevice
, vbasedev
.name
),
685 DEFINE_PROP_STRING("sysfsdev", VFIOPlatformDevice
, vbasedev
.sysfsdev
),
686 DEFINE_PROP_BOOL("x-no-mmap", VFIOPlatformDevice
, vbasedev
.no_mmap
, false),
687 DEFINE_PROP_UINT32("mmap-timeout-ms", VFIOPlatformDevice
,
689 DEFINE_PROP_BOOL("x-irqfd", VFIOPlatformDevice
, irqfd_allowed
, true),
690 DEFINE_PROP_END_OF_LIST(),
693 static void vfio_platform_class_init(ObjectClass
*klass
, void *data
)
695 DeviceClass
*dc
= DEVICE_CLASS(klass
);
696 SysBusDeviceClass
*sbc
= SYS_BUS_DEVICE_CLASS(klass
);
698 dc
->realize
= vfio_platform_realize
;
699 dc
->props
= vfio_platform_dev_properties
;
700 dc
->vmsd
= &vfio_platform_vmstate
;
701 dc
->desc
= "VFIO-based platform device assignment";
702 sbc
->connect_irq_notifier
= vfio_start_irqfd_injection
;
703 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
704 /* Supported by TYPE_VIRT_MACHINE */
705 dc
->user_creatable
= true;
708 static const TypeInfo vfio_platform_dev_info
= {
709 .name
= TYPE_VFIO_PLATFORM
,
710 .parent
= TYPE_SYS_BUS_DEVICE
,
711 .instance_size
= sizeof(VFIOPlatformDevice
),
712 .class_init
= vfio_platform_class_init
,
713 .class_size
= sizeof(VFIOPlatformDeviceClass
),
716 static void register_vfio_platform_dev_type(void)
718 type_register_static(&vfio_platform_dev_info
);
721 type_init(register_vfio_platform_dev_type
)