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/range.h"
25 #include "sysemu/sysemu.h"
26 #include "exec/memory.h"
27 #include "exec/address-spaces.h"
28 #include "qemu/queue.h"
29 #include "hw/sysbus.h"
31 #include "hw/platform-bus.h"
32 #include "sysemu/kvm.h"
35 * Functions used whatever the injection method
38 static inline bool vfio_irq_is_automasked(VFIOINTp
*intp
)
40 return intp
->flags
& VFIO_IRQ_INFO_AUTOMASKED
;
44 * vfio_init_intp - allocate, initialize the IRQ struct pointer
45 * and add it into the list of IRQs
46 * @vbasedev: the VFIO device handle
47 * @info: irq info struct retrieved from VFIO driver
50 static VFIOINTp
*vfio_init_intp(VFIODevice
*vbasedev
,
51 struct vfio_irq_info info
, Error
**errp
)
54 VFIOPlatformDevice
*vdev
=
55 container_of(vbasedev
, VFIOPlatformDevice
, vbasedev
);
56 SysBusDevice
*sbdev
= SYS_BUS_DEVICE(vdev
);
59 intp
= g_malloc0(sizeof(*intp
));
61 intp
->pin
= info
.index
;
62 intp
->flags
= info
.flags
;
63 intp
->state
= VFIO_IRQ_INACTIVE
;
64 intp
->kvm_accel
= false;
66 sysbus_init_irq(sbdev
, &intp
->qemuirq
);
68 /* Get an eventfd for trigger */
69 intp
->interrupt
= g_malloc0(sizeof(EventNotifier
));
70 ret
= event_notifier_init(intp
->interrupt
, 0);
72 g_free(intp
->interrupt
);
74 error_setg_errno(errp
, -ret
,
75 "failed to initialize trigger eventd notifier");
78 if (vfio_irq_is_automasked(intp
)) {
79 /* Get an eventfd for resample/unmask */
80 intp
->unmask
= g_malloc0(sizeof(EventNotifier
));
81 ret
= event_notifier_init(intp
->unmask
, 0);
83 g_free(intp
->interrupt
);
86 error_setg_errno(errp
, -ret
,
87 "failed to initialize resample eventd notifier");
92 QLIST_INSERT_HEAD(&vdev
->intp_list
, intp
, next
);
97 * vfio_set_trigger_eventfd - set VFIO eventfd handling
99 * @intp: IRQ struct handle
100 * @handler: handler to be called on eventfd signaling
102 * Setup VFIO signaling and attach an optional user-side handler
105 static int vfio_set_trigger_eventfd(VFIOINTp
*intp
,
106 eventfd_user_side_handler_t handler
)
108 VFIODevice
*vbasedev
= &intp
->vdev
->vbasedev
;
109 struct vfio_irq_set
*irq_set
;
113 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
114 irq_set
= g_malloc0(argsz
);
115 irq_set
->argsz
= argsz
;
116 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
| VFIO_IRQ_SET_ACTION_TRIGGER
;
117 irq_set
->index
= intp
->pin
;
120 pfd
= (int32_t *)&irq_set
->data
;
121 *pfd
= event_notifier_get_fd(intp
->interrupt
);
122 qemu_set_fd_handler(*pfd
, (IOHandler
*)handler
, NULL
, intp
);
123 ret
= ioctl(vbasedev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
125 error_report("vfio: Failed to set trigger eventfd: %m");
126 qemu_set_fd_handler(*pfd
, NULL
, NULL
, NULL
);
133 * Functions only used when eventfds are handled on user-side
138 * vfio_mmap_set_enabled - enable/disable the fast path mode
139 * @vdev: the VFIO platform device
140 * @enabled: the target mmap state
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
147 static void vfio_mmap_set_enabled(VFIOPlatformDevice
*vdev
, bool enabled
)
151 for (i
= 0; i
< vdev
->vbasedev
.num_regions
; i
++) {
152 vfio_region_mmaps_set_enabled(vdev
->regions
[i
], enabled
);
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
161 * Called on mmap timer timout, 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.
166 static void vfio_intp_mmap_enable(void *opaque
)
169 VFIOPlatformDevice
*vdev
= (VFIOPlatformDevice
*)opaque
;
171 qemu_mutex_lock(&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
) +
179 qemu_mutex_unlock(&vdev
->intp_mutex
);
183 vfio_mmap_set_enabled(vdev
, true);
184 qemu_mutex_unlock(&vdev
->intp_mutex
);
188 * vfio_intp_inject_pending_lockheld - Injects a pending IRQ
189 * @opaque: opaque pointer, in practice the VFIOINTp handle
191 * The function is called on a previous IRQ completion, from
192 * vfio_platform_eoi, while the intp_mutex is locked.
193 * Also in such situation, the slow path already is set and
194 * the mmap timer was already programmed.
196 static void vfio_intp_inject_pending_lockheld(VFIOINTp
*intp
)
198 trace_vfio_platform_intp_inject_pending_lockheld(intp
->pin
,
199 event_notifier_get_fd(intp
->interrupt
));
201 intp
->state
= VFIO_IRQ_ACTIVE
;
203 /* trigger the virtual IRQ */
204 qemu_set_irq(intp
->qemuirq
, 1);
208 * vfio_intp_interrupt - The user-side eventfd handler
209 * @opaque: opaque pointer which in practice is the VFIOINTp handle
211 * the function is entered in event handler context:
212 * the vIRQ is injected into the guest if there is no other active
215 static void vfio_intp_interrupt(VFIOINTp
*intp
)
219 VFIOPlatformDevice
*vdev
= intp
->vdev
;
220 bool delay_handling
= false;
222 qemu_mutex_lock(&vdev
->intp_mutex
);
223 if (intp
->state
== VFIO_IRQ_INACTIVE
) {
224 QLIST_FOREACH(tmp
, &vdev
->intp_list
, next
) {
225 if (tmp
->state
== VFIO_IRQ_ACTIVE
||
226 tmp
->state
== VFIO_IRQ_PENDING
) {
227 delay_handling
= true;
232 if (delay_handling
) {
234 * the new IRQ gets a pending status and is pushed in
237 intp
->state
= VFIO_IRQ_PENDING
;
238 trace_vfio_intp_interrupt_set_pending(intp
->pin
);
239 QSIMPLEQ_INSERT_TAIL(&vdev
->pending_intp_queue
,
241 ret
= event_notifier_test_and_clear(intp
->interrupt
);
242 qemu_mutex_unlock(&vdev
->intp_mutex
);
246 trace_vfio_platform_intp_interrupt(intp
->pin
,
247 event_notifier_get_fd(intp
->interrupt
));
249 ret
= event_notifier_test_and_clear(intp
->interrupt
);
251 error_report("Error when clearing fd=%d (ret = %d)",
252 event_notifier_get_fd(intp
->interrupt
), ret
);
255 intp
->state
= VFIO_IRQ_ACTIVE
;
258 vfio_mmap_set_enabled(vdev
, false);
260 /* trigger the virtual IRQ */
261 qemu_set_irq(intp
->qemuirq
, 1);
264 * Schedule the mmap timer which will restore fastpath when no IRQ
267 if (vdev
->mmap_timeout
) {
268 timer_mod(vdev
->mmap_timer
,
269 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
272 qemu_mutex_unlock(&vdev
->intp_mutex
);
276 * vfio_platform_eoi - IRQ completion routine
277 * @vbasedev: the VFIO device handle
279 * De-asserts the active virtual IRQ and unmasks the physical IRQ
280 * (effective for level sensitive IRQ auto-masked by the VFIO driver).
281 * Then it handles next pending IRQ if any.
282 * eoi function is called on the first access to any MMIO region
283 * after an IRQ was triggered, trapped since slow path was set.
284 * It is assumed this access corresponds to the IRQ status
285 * register reset. With such a mechanism, a single IRQ can be
286 * handled at a time since there is no way to know which IRQ
287 * was completed by the guest (we would need additional details
288 * about the IRQ status register mask).
290 static void vfio_platform_eoi(VFIODevice
*vbasedev
)
293 VFIOPlatformDevice
*vdev
=
294 container_of(vbasedev
, VFIOPlatformDevice
, vbasedev
);
296 qemu_mutex_lock(&vdev
->intp_mutex
);
297 QLIST_FOREACH(intp
, &vdev
->intp_list
, next
) {
298 if (intp
->state
== VFIO_IRQ_ACTIVE
) {
299 trace_vfio_platform_eoi(intp
->pin
,
300 event_notifier_get_fd(intp
->interrupt
));
301 intp
->state
= VFIO_IRQ_INACTIVE
;
303 /* deassert the virtual IRQ */
304 qemu_set_irq(intp
->qemuirq
, 0);
306 if (vfio_irq_is_automasked(intp
)) {
307 /* unmasks the physical level-sensitive IRQ */
308 vfio_unmask_single_irqindex(vbasedev
, intp
->pin
);
311 /* a single IRQ can be active at a time */
315 /* in case there are pending IRQs, handle the first one */
316 if (!QSIMPLEQ_EMPTY(&vdev
->pending_intp_queue
)) {
317 intp
= QSIMPLEQ_FIRST(&vdev
->pending_intp_queue
);
318 vfio_intp_inject_pending_lockheld(intp
);
319 QSIMPLEQ_REMOVE_HEAD(&vdev
->pending_intp_queue
, pqnext
);
321 qemu_mutex_unlock(&vdev
->intp_mutex
);
325 * vfio_start_eventfd_injection - starts the virtual IRQ injection using
326 * user-side handled eventfds
327 * @sbdev: the sysbus device handle
328 * @irq: the qemu irq handle
331 static void vfio_start_eventfd_injection(SysBusDevice
*sbdev
, qemu_irq irq
)
334 VFIOPlatformDevice
*vdev
= VFIO_PLATFORM_DEVICE(sbdev
);
337 QLIST_FOREACH(intp
, &vdev
->intp_list
, next
) {
338 if (intp
->qemuirq
== irq
) {
344 ret
= vfio_set_trigger_eventfd(intp
, vfio_intp_interrupt
);
346 error_report("vfio: failed to start eventfd signaling for IRQ %d: %m",
353 * Functions used for irqfd
357 * vfio_set_resample_eventfd - sets the resamplefd for an IRQ
358 * @intp: the IRQ struct handle
359 * programs the VFIO driver to unmask this IRQ when the
360 * intp->unmask eventfd is triggered
362 static int vfio_set_resample_eventfd(VFIOINTp
*intp
)
364 VFIODevice
*vbasedev
= &intp
->vdev
->vbasedev
;
365 struct vfio_irq_set
*irq_set
;
369 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
370 irq_set
= g_malloc0(argsz
);
371 irq_set
->argsz
= argsz
;
372 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
| VFIO_IRQ_SET_ACTION_UNMASK
;
373 irq_set
->index
= intp
->pin
;
376 pfd
= (int32_t *)&irq_set
->data
;
377 *pfd
= event_notifier_get_fd(intp
->unmask
);
378 qemu_set_fd_handler(*pfd
, NULL
, NULL
, NULL
);
379 ret
= ioctl(vbasedev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
382 error_report("vfio: Failed to set resample eventfd: %m");
388 * vfio_start_irqfd_injection - starts the virtual IRQ injection using
391 * @sbdev: the sysbus device handle
392 * @irq: the qemu irq handle
394 * In case the irqfd setup fails, we fallback to userspace handled eventfd
396 static void vfio_start_irqfd_injection(SysBusDevice
*sbdev
, qemu_irq irq
)
398 VFIOPlatformDevice
*vdev
= VFIO_PLATFORM_DEVICE(sbdev
);
401 if (!kvm_irqfds_enabled() || !kvm_resamplefds_enabled() ||
402 !vdev
->irqfd_allowed
) {
406 QLIST_FOREACH(intp
, &vdev
->intp_list
, next
) {
407 if (intp
->qemuirq
== irq
) {
413 if (kvm_irqchip_add_irqfd_notifier(kvm_state
, intp
->interrupt
,
414 intp
->unmask
, irq
) < 0) {
418 if (vfio_set_trigger_eventfd(intp
, NULL
) < 0) {
421 if (vfio_irq_is_automasked(intp
)) {
422 if (vfio_set_resample_eventfd(intp
) < 0) {
425 trace_vfio_platform_start_level_irqfd_injection(intp
->pin
,
426 event_notifier_get_fd(intp
->interrupt
),
427 event_notifier_get_fd(intp
->unmask
));
429 trace_vfio_platform_start_edge_irqfd_injection(intp
->pin
,
430 event_notifier_get_fd(intp
->interrupt
));
433 intp
->kvm_accel
= true;
437 kvm_irqchip_remove_irqfd_notifier(kvm_state
, intp
->interrupt
, irq
);
438 error_report("vfio: failed to start eventfd signaling for IRQ %d: %m",
442 vfio_start_eventfd_injection(sbdev
, irq
);
448 static void vfio_platform_compute_needs_reset(VFIODevice
*vbasedev
)
450 vbasedev
->needs_reset
= true;
453 /* not implemented yet */
454 static int vfio_platform_hot_reset_multi(VFIODevice
*vbasedev
)
460 * vfio_populate_device - Allocate and populate MMIO region
461 * and IRQ structs according to driver returned information
462 * @vbasedev: the VFIO device handle
463 * @errp: error object
466 static int vfio_populate_device(VFIODevice
*vbasedev
, Error
**errp
)
468 VFIOINTp
*intp
, *tmp
;
470 VFIOPlatformDevice
*vdev
=
471 container_of(vbasedev
, VFIOPlatformDevice
, vbasedev
);
473 if (!(vbasedev
->flags
& VFIO_DEVICE_FLAGS_PLATFORM
)) {
474 error_setg(errp
, "this isn't a platform device");
478 vdev
->regions
= g_new0(VFIORegion
*, vbasedev
->num_regions
);
480 for (i
= 0; i
< vbasedev
->num_regions
; i
++) {
481 char *name
= g_strdup_printf("VFIO %s region %d\n", vbasedev
->name
, i
);
483 vdev
->regions
[i
] = g_new0(VFIORegion
, 1);
484 ret
= vfio_region_setup(OBJECT(vdev
), vbasedev
,
485 vdev
->regions
[i
], i
, name
);
488 error_setg_errno(errp
, -ret
, "failed to get region %d info", i
);
493 vdev
->mmap_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
494 vfio_intp_mmap_enable
, vdev
);
496 QSIMPLEQ_INIT(&vdev
->pending_intp_queue
);
498 for (i
= 0; i
< vbasedev
->num_irqs
; i
++) {
499 struct vfio_irq_info irq
= { .argsz
= sizeof(irq
) };
502 ret
= ioctl(vbasedev
->fd
, VFIO_DEVICE_GET_IRQ_INFO
, &irq
);
504 error_setg_errno(errp
, -ret
, "failed to get device irq info");
507 trace_vfio_platform_populate_interrupts(irq
.index
,
510 intp
= vfio_init_intp(vbasedev
, irq
, errp
);
519 timer_del(vdev
->mmap_timer
);
520 QLIST_FOREACH_SAFE(intp
, &vdev
->intp_list
, next
, tmp
) {
521 QLIST_REMOVE(intp
, next
);
525 for (i
= 0; i
< vbasedev
->num_regions
; i
++) {
526 if (vdev
->regions
[i
]) {
527 vfio_region_finalize(vdev
->regions
[i
]);
529 g_free(vdev
->regions
[i
]);
531 g_free(vdev
->regions
);
535 /* specialized functions for VFIO Platform devices */
536 static VFIODeviceOps vfio_platform_ops
= {
537 .vfio_compute_needs_reset
= vfio_platform_compute_needs_reset
,
538 .vfio_hot_reset_multi
= vfio_platform_hot_reset_multi
,
539 .vfio_eoi
= vfio_platform_eoi
,
543 * vfio_base_device_init - perform preliminary VFIO setup
544 * @vbasedev: the VFIO device handle
545 * @errp: error object
547 * Implement the VFIO command sequence that allows to discover
548 * assigned device resources: group extraction, device
549 * fd retrieval, resource query.
550 * Precondition: the device name must be initialized
552 static int vfio_base_device_init(VFIODevice
*vbasedev
, Error
**errp
)
555 VFIODevice
*vbasedev_iter
;
556 char *tmp
, group_path
[PATH_MAX
], *group_name
;
562 /* @sysfsdev takes precedence over @host */
563 if (vbasedev
->sysfsdev
) {
564 g_free(vbasedev
->name
);
565 vbasedev
->name
= g_path_get_basename(vbasedev
->sysfsdev
);
567 if (!vbasedev
->name
|| strchr(vbasedev
->name
, '/')) {
568 error_setg(errp
, "wrong host device name");
572 vbasedev
->sysfsdev
= g_strdup_printf("/sys/bus/platform/devices/%s",
576 if (stat(vbasedev
->sysfsdev
, &st
) < 0) {
577 error_setg_errno(errp
, errno
,
578 "failed to get the sysfs host device file status");
582 tmp
= g_strdup_printf("%s/iommu_group", vbasedev
->sysfsdev
);
583 len
= readlink(tmp
, group_path
, sizeof(group_path
));
586 if (len
< 0 || len
>= sizeof(group_path
)) {
587 ret
= len
< 0 ? -errno
: -ENAMETOOLONG
;
588 error_setg_errno(errp
, -ret
, "no iommu_group found");
594 group_name
= basename(group_path
);
595 if (sscanf(group_name
, "%d", &groupid
) != 1) {
596 error_setg_errno(errp
, errno
, "failed to read %s", group_path
);
600 trace_vfio_platform_base_device_init(vbasedev
->name
, groupid
);
602 group
= vfio_get_group(groupid
, &address_space_memory
, errp
);
607 QLIST_FOREACH(vbasedev_iter
, &group
->device_list
, next
) {
608 if (strcmp(vbasedev_iter
->name
, vbasedev
->name
) == 0) {
609 error_setg(errp
, "device is already attached");
610 vfio_put_group(group
);
614 ret
= vfio_get_device(group
, vbasedev
->name
, vbasedev
, errp
);
616 vfio_put_group(group
);
620 ret
= vfio_populate_device(vbasedev
, errp
);
622 vfio_put_group(group
);
629 * vfio_platform_realize - the device realize function
630 * @dev: device state pointer
633 * initialize the device, its memory regions and IRQ structures
634 * IRQ are started separately
636 static void vfio_platform_realize(DeviceState
*dev
, Error
**errp
)
638 VFIOPlatformDevice
*vdev
= VFIO_PLATFORM_DEVICE(dev
);
639 SysBusDevice
*sbdev
= SYS_BUS_DEVICE(dev
);
640 VFIODevice
*vbasedev
= &vdev
->vbasedev
;
643 vbasedev
->type
= VFIO_DEVICE_TYPE_PLATFORM
;
645 vbasedev
->ops
= &vfio_platform_ops
;
647 qemu_mutex_init(&vdev
->intp_mutex
);
649 trace_vfio_platform_realize(vbasedev
->sysfsdev
?
650 vbasedev
->sysfsdev
: vbasedev
->name
,
653 ret
= vfio_base_device_init(vbasedev
, errp
);
658 for (i
= 0; i
< vbasedev
->num_regions
; i
++) {
659 if (vfio_region_mmap(vdev
->regions
[i
])) {
660 error_report("%s mmap unsupported. Performance may be slow",
661 memory_region_name(vdev
->regions
[i
]->mem
));
663 sysbus_init_mmio(sbdev
, vdev
->regions
[i
]->mem
);
670 if (vdev
->vbasedev
.name
) {
671 error_prepend(errp
, ERR_PREFIX
, vdev
->vbasedev
.name
);
673 error_prepend(errp
, "vfio error: ");
677 static const VMStateDescription vfio_platform_vmstate
= {
678 .name
= TYPE_VFIO_PLATFORM
,
682 static 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
,
688 DEFINE_PROP_BOOL("x-irqfd", VFIOPlatformDevice
, irqfd_allowed
, true),
689 DEFINE_PROP_END_OF_LIST(),
692 static void vfio_platform_class_init(ObjectClass
*klass
, void *data
)
694 DeviceClass
*dc
= DEVICE_CLASS(klass
);
695 SysBusDeviceClass
*sbc
= SYS_BUS_DEVICE_CLASS(klass
);
697 dc
->realize
= vfio_platform_realize
;
698 dc
->props
= 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
);
705 static const TypeInfo vfio_platform_dev_info
= {
706 .name
= TYPE_VFIO_PLATFORM
,
707 .parent
= TYPE_SYS_BUS_DEVICE
,
708 .instance_size
= sizeof(VFIOPlatformDevice
),
709 .class_init
= vfio_platform_class_init
,
710 .class_size
= sizeof(VFIOPlatformDeviceClass
),
714 static void register_vfio_platform_dev_type(void)
716 type_register_static(&vfio_platform_dev_info
);
719 type_init(register_vfio_platform_dev_type
)