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