blob: f8f08a0f362df21a122606921bbe63f4d6675fdc [file] [log] [blame]
Eric Auger0ea27302015-06-08 09:25:25 -06001/*
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 Maydellc6eacb12016-01-26 18:17:14 +000017#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010018#include "qapi/error.h"
Eric Auger0ea27302015-06-08 09:25:25 -060019#include <sys/ioctl.h>
Leon Alraee2075272015-06-17 13:35:00 +010020#include <linux/vfio.h>
Eric Auger0ea27302015-06-08 09:25:25 -060021
22#include "hw/vfio/vfio-platform.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020023#include "migration/vmstate.h"
Eric Auger0ea27302015-06-08 09:25:25 -060024#include "qemu/error-report.h"
Daniel Brodsky6e8a3552020-04-03 21:21:08 -070025#include "qemu/lockable.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020026#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020027#include "qemu/module.h"
Eric Auger0ea27302015-06-08 09:25:25 -060028#include "qemu/range.h"
Eric Auger0ea27302015-06-08 09:25:25 -060029#include "exec/memory.h"
Philippe Mathieu-Daudéd7919372018-05-28 20:26:59 -030030#include "exec/address-spaces.h"
Eric Auger38559972015-06-08 09:25:26 -060031#include "qemu/queue.h"
Eric Auger0ea27302015-06-08 09:25:25 -060032#include "hw/sysbus.h"
33#include "trace.h"
Markus Armbruster64552b62019-08-12 07:23:42 +020034#include "hw/irq.h"
Eric Auger0ea27302015-06-08 09:25:25 -060035#include "hw/platform-bus.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020036#include "hw/qdev-properties.h"
Eric Augerfb5f8162015-07-06 12:15:14 -060037#include "sysemu/kvm.h"
Eric Auger0ea27302015-06-08 09:25:25 -060038
Eric Auger38559972015-06-08 09:25:26 -060039/*
40 * Functions used whatever the injection method
41 */
42
Eric Augera5b39cd2015-10-05 12:30:12 -060043static inline bool vfio_irq_is_automasked(VFIOINTp *intp)
44{
45 return intp->flags & VFIO_IRQ_INFO_AUTOMASKED;
46}
47
Eric Auger38559972015-06-08 09:25:26 -060048/**
49 * vfio_init_intp - allocate, initialize the IRQ struct pointer
50 * and add it into the list of IRQs
51 * @vbasedev: the VFIO device handle
52 * @info: irq info struct retrieved from VFIO driver
Eric Auger5ff74192016-10-17 10:58:00 -060053 * @errp: error object
Eric Auger38559972015-06-08 09:25:26 -060054 */
55static VFIOINTp *vfio_init_intp(VFIODevice *vbasedev,
Eric Auger5ff74192016-10-17 10:58:00 -060056 struct vfio_irq_info info, Error **errp)
Eric Auger38559972015-06-08 09:25:26 -060057{
58 int ret;
59 VFIOPlatformDevice *vdev =
60 container_of(vbasedev, VFIOPlatformDevice, vbasedev);
61 SysBusDevice *sbdev = SYS_BUS_DEVICE(vdev);
62 VFIOINTp *intp;
63
64 intp = g_malloc0(sizeof(*intp));
65 intp->vdev = vdev;
66 intp->pin = info.index;
67 intp->flags = info.flags;
68 intp->state = VFIO_IRQ_INACTIVE;
Eric Augerfb5f8162015-07-06 12:15:14 -060069 intp->kvm_accel = false;
Eric Auger38559972015-06-08 09:25:26 -060070
71 sysbus_init_irq(sbdev, &intp->qemuirq);
72
73 /* Get an eventfd for trigger */
Eric Augera22313d2015-10-05 12:30:12 -060074 intp->interrupt = g_malloc0(sizeof(EventNotifier));
75 ret = event_notifier_init(intp->interrupt, 0);
Eric Auger38559972015-06-08 09:25:26 -060076 if (ret) {
Eric Augera22313d2015-10-05 12:30:12 -060077 g_free(intp->interrupt);
Eric Auger38559972015-06-08 09:25:26 -060078 g_free(intp);
Eric Auger5ff74192016-10-17 10:58:00 -060079 error_setg_errno(errp, -ret,
Li Qiangbf04ef32019-05-21 08:15:42 -070080 "failed to initialize trigger eventfd notifier");
Eric Auger38559972015-06-08 09:25:26 -060081 return NULL;
82 }
Eric Augera5b39cd2015-10-05 12:30:12 -060083 if (vfio_irq_is_automasked(intp)) {
84 /* Get an eventfd for resample/unmask */
85 intp->unmask = g_malloc0(sizeof(EventNotifier));
86 ret = event_notifier_init(intp->unmask, 0);
87 if (ret) {
88 g_free(intp->interrupt);
89 g_free(intp->unmask);
90 g_free(intp);
Eric Auger5ff74192016-10-17 10:58:00 -060091 error_setg_errno(errp, -ret,
Li Qiangbf04ef32019-05-21 08:15:42 -070092 "failed to initialize resample eventfd notifier");
Eric Augera5b39cd2015-10-05 12:30:12 -060093 return NULL;
94 }
Eric Augerfb5f8162015-07-06 12:15:14 -060095 }
Eric Auger38559972015-06-08 09:25:26 -060096
97 QLIST_INSERT_HEAD(&vdev->intp_list, intp, next);
98 return intp;
99}
100
101/**
102 * vfio_set_trigger_eventfd - set VFIO eventfd handling
103 *
104 * @intp: IRQ struct handle
105 * @handler: handler to be called on eventfd signaling
106 *
107 * Setup VFIO signaling and attach an optional user-side handler
108 * to the eventfd
109 */
110static int vfio_set_trigger_eventfd(VFIOINTp *intp,
111 eventfd_user_side_handler_t handler)
112{
113 VFIODevice *vbasedev = &intp->vdev->vbasedev;
Eric Auger201a7332019-06-13 09:57:37 -0600114 int32_t fd = event_notifier_get_fd(intp->interrupt);
115 Error *err = NULL;
116 int ret;
Eric Auger38559972015-06-08 09:25:26 -0600117
Eric Auger201a7332019-06-13 09:57:37 -0600118 qemu_set_fd_handler(fd, (IOHandler *)handler, NULL, intp);
119
120 ret = vfio_set_irq_signaling(vbasedev, intp->pin, 0,
121 VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err);
122 if (ret) {
123 error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name);
124 qemu_set_fd_handler(fd, NULL, NULL, NULL);
Eric Auger38559972015-06-08 09:25:26 -0600125 }
Eric Auger201a7332019-06-13 09:57:37 -0600126
Eric Auger38559972015-06-08 09:25:26 -0600127 return ret;
128}
129
130/*
131 * Functions only used when eventfds are handled on user-side
132 * ie. without irqfd
133 */
134
135/**
136 * vfio_mmap_set_enabled - enable/disable the fast path mode
137 * @vdev: the VFIO platform device
138 * @enabled: the target mmap state
139 *
140 * enabled = true ~ fast path = MMIO region is mmaped (no KVM TRAP);
141 * enabled = false ~ slow path = MMIO region is trapped and region callbacks
142 * are called; slow path enables to trap the device IRQ status register reset
143*/
144
145static void vfio_mmap_set_enabled(VFIOPlatformDevice *vdev, bool enabled)
146{
147 int i;
148
Eric Auger38559972015-06-08 09:25:26 -0600149 for (i = 0; i < vdev->vbasedev.num_regions; i++) {
Alex Williamsondb0da022016-03-10 09:39:07 -0700150 vfio_region_mmaps_set_enabled(vdev->regions[i], enabled);
Eric Auger38559972015-06-08 09:25:26 -0600151 }
152}
153
154/**
155 * vfio_intp_mmap_enable - timer function, restores the fast path
156 * if there is no more active IRQ
157 * @opaque: actually points to the VFIO platform device
158 *
Cai Huoqing631ba5a2021-07-30 09:26:13 +0800159 * Called on mmap timer timeout, this function checks whether the
Eric Auger38559972015-06-08 09:25:26 -0600160 * IRQ is still active and if not, restores the fast path.
161 * by construction a single eventfd is handled at a time.
162 * if the IRQ is still active, the timer is re-programmed.
163 */
164static void vfio_intp_mmap_enable(void *opaque)
165{
166 VFIOINTp *tmp;
167 VFIOPlatformDevice *vdev = (VFIOPlatformDevice *)opaque;
168
Amey Narkhede88eef592020-10-23 18:13:42 +0530169 QEMU_LOCK_GUARD(&vdev->intp_mutex);
Eric Auger38559972015-06-08 09:25:26 -0600170 QLIST_FOREACH(tmp, &vdev->intp_list, next) {
171 if (tmp->state == VFIO_IRQ_ACTIVE) {
172 trace_vfio_platform_intp_mmap_enable(tmp->pin);
173 /* re-program the timer to check active status later */
174 timer_mod(vdev->mmap_timer,
175 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
176 vdev->mmap_timeout);
Eric Auger38559972015-06-08 09:25:26 -0600177 return;
178 }
179 }
180 vfio_mmap_set_enabled(vdev, true);
Eric Auger38559972015-06-08 09:25:26 -0600181}
182
183/**
184 * vfio_intp_inject_pending_lockheld - Injects a pending IRQ
185 * @opaque: opaque pointer, in practice the VFIOINTp handle
186 *
187 * The function is called on a previous IRQ completion, from
188 * vfio_platform_eoi, while the intp_mutex is locked.
189 * Also in such situation, the slow path already is set and
190 * the mmap timer was already programmed.
191 */
192static void vfio_intp_inject_pending_lockheld(VFIOINTp *intp)
193{
194 trace_vfio_platform_intp_inject_pending_lockheld(intp->pin,
Eric Augera22313d2015-10-05 12:30:12 -0600195 event_notifier_get_fd(intp->interrupt));
Eric Auger38559972015-06-08 09:25:26 -0600196
197 intp->state = VFIO_IRQ_ACTIVE;
198
199 /* trigger the virtual IRQ */
200 qemu_set_irq(intp->qemuirq, 1);
201}
202
203/**
204 * vfio_intp_interrupt - The user-side eventfd handler
205 * @opaque: opaque pointer which in practice is the VFIOINTp handle
206 *
207 * the function is entered in event handler context:
208 * the vIRQ is injected into the guest if there is no other active
209 * or pending IRQ.
210 */
211static void vfio_intp_interrupt(VFIOINTp *intp)
212{
213 int ret;
214 VFIOINTp *tmp;
215 VFIOPlatformDevice *vdev = intp->vdev;
216 bool delay_handling = false;
217
Daniel Brodsky6e8a3552020-04-03 21:21:08 -0700218 QEMU_LOCK_GUARD(&vdev->intp_mutex);
Eric Auger38559972015-06-08 09:25:26 -0600219 if (intp->state == VFIO_IRQ_INACTIVE) {
220 QLIST_FOREACH(tmp, &vdev->intp_list, next) {
221 if (tmp->state == VFIO_IRQ_ACTIVE ||
222 tmp->state == VFIO_IRQ_PENDING) {
223 delay_handling = true;
224 break;
225 }
226 }
227 }
228 if (delay_handling) {
229 /*
230 * the new IRQ gets a pending status and is pushed in
231 * the pending queue
232 */
233 intp->state = VFIO_IRQ_PENDING;
234 trace_vfio_intp_interrupt_set_pending(intp->pin);
235 QSIMPLEQ_INSERT_TAIL(&vdev->pending_intp_queue,
236 intp, pqnext);
Chen Qun9b83b002020-08-27 19:03:08 +0800237 event_notifier_test_and_clear(intp->interrupt);
Eric Auger38559972015-06-08 09:25:26 -0600238 return;
239 }
240
241 trace_vfio_platform_intp_interrupt(intp->pin,
Eric Augera22313d2015-10-05 12:30:12 -0600242 event_notifier_get_fd(intp->interrupt));
Eric Auger38559972015-06-08 09:25:26 -0600243
Eric Augera22313d2015-10-05 12:30:12 -0600244 ret = event_notifier_test_and_clear(intp->interrupt);
Eric Auger38559972015-06-08 09:25:26 -0600245 if (!ret) {
John Snow594fd212015-06-29 16:56:26 -0400246 error_report("Error when clearing fd=%d (ret = %d)",
Eric Augera22313d2015-10-05 12:30:12 -0600247 event_notifier_get_fd(intp->interrupt), ret);
Eric Auger38559972015-06-08 09:25:26 -0600248 }
249
250 intp->state = VFIO_IRQ_ACTIVE;
251
252 /* sets slow path */
253 vfio_mmap_set_enabled(vdev, false);
254
255 /* trigger the virtual IRQ */
256 qemu_set_irq(intp->qemuirq, 1);
257
258 /*
259 * Schedule the mmap timer which will restore fastpath when no IRQ
260 * is active anymore
261 */
262 if (vdev->mmap_timeout) {
263 timer_mod(vdev->mmap_timer,
264 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
265 vdev->mmap_timeout);
266 }
Eric Auger38559972015-06-08 09:25:26 -0600267}
268
269/**
270 * vfio_platform_eoi - IRQ completion routine
271 * @vbasedev: the VFIO device handle
272 *
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).
283 */
284static void vfio_platform_eoi(VFIODevice *vbasedev)
285{
286 VFIOINTp *intp;
287 VFIOPlatformDevice *vdev =
288 container_of(vbasedev, VFIOPlatformDevice, vbasedev);
289
Amey Narkhede88eef592020-10-23 18:13:42 +0530290 QEMU_LOCK_GUARD(&vdev->intp_mutex);
Eric Auger38559972015-06-08 09:25:26 -0600291 QLIST_FOREACH(intp, &vdev->intp_list, next) {
292 if (intp->state == VFIO_IRQ_ACTIVE) {
293 trace_vfio_platform_eoi(intp->pin,
Eric Augera22313d2015-10-05 12:30:12 -0600294 event_notifier_get_fd(intp->interrupt));
Eric Auger38559972015-06-08 09:25:26 -0600295 intp->state = VFIO_IRQ_INACTIVE;
296
297 /* deassert the virtual IRQ */
298 qemu_set_irq(intp->qemuirq, 0);
299
Eric Augera5b39cd2015-10-05 12:30:12 -0600300 if (vfio_irq_is_automasked(intp)) {
Eric Auger38559972015-06-08 09:25:26 -0600301 /* unmasks the physical level-sensitive IRQ */
302 vfio_unmask_single_irqindex(vbasedev, intp->pin);
303 }
304
305 /* a single IRQ can be active at a time */
306 break;
307 }
308 }
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);
314 }
Eric Auger38559972015-06-08 09:25:26 -0600315}
316
317/**
318 * vfio_start_eventfd_injection - starts the virtual IRQ injection using
319 * user-side handled eventfds
Eric Auger58892b42015-10-05 12:30:12 -0600320 * @sbdev: the sysbus device handle
321 * @irq: the qemu irq handle
Eric Auger38559972015-06-08 09:25:26 -0600322 */
323
Eric Auger58892b42015-10-05 12:30:12 -0600324static void vfio_start_eventfd_injection(SysBusDevice *sbdev, qemu_irq irq)
Eric Auger38559972015-06-08 09:25:26 -0600325{
Eric Auger58892b42015-10-05 12:30:12 -0600326 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
327 VFIOINTp *intp;
328
329 QLIST_FOREACH(intp, &vdev->intp_list, next) {
330 if (intp->qemuirq == irq) {
331 break;
332 }
333 }
334 assert(intp);
Eric Auger38559972015-06-08 09:25:26 -0600335
Eric Auger201a7332019-06-13 09:57:37 -0600336 if (vfio_set_trigger_eventfd(intp, vfio_intp_interrupt)) {
Eric Auger58892b42015-10-05 12:30:12 -0600337 abort();
Eric Auger38559972015-06-08 09:25:26 -0600338 }
Eric Auger38559972015-06-08 09:25:26 -0600339}
340
Eric Augerfb5f8162015-07-06 12:15:14 -0600341/*
342 * Functions used for irqfd
343 */
344
345/**
346 * vfio_set_resample_eventfd - sets the resamplefd for an IRQ
347 * @intp: the IRQ struct handle
348 * programs the VFIO driver to unmask this IRQ when the
349 * intp->unmask eventfd is triggered
350 */
351static int vfio_set_resample_eventfd(VFIOINTp *intp)
352{
Eric Auger201a7332019-06-13 09:57:37 -0600353 int32_t fd = event_notifier_get_fd(intp->unmask);
Eric Augerfb5f8162015-07-06 12:15:14 -0600354 VFIODevice *vbasedev = &intp->vdev->vbasedev;
Eric Auger201a7332019-06-13 09:57:37 -0600355 Error *err = NULL;
356 int ret;
Eric Augerfb5f8162015-07-06 12:15:14 -0600357
Eric Auger201a7332019-06-13 09:57:37 -0600358 qemu_set_fd_handler(fd, NULL, NULL, NULL);
359 ret = vfio_set_irq_signaling(vbasedev, intp->pin, 0,
360 VFIO_IRQ_SET_ACTION_UNMASK, fd, &err);
361 if (ret) {
362 error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name);
Eric Augerfb5f8162015-07-06 12:15:14 -0600363 }
364 return ret;
365}
366
Eric Auger58892b42015-10-05 12:30:12 -0600367/**
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 Augerfb5f8162015-07-06 12:15:14 -0600376static 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 Auger58892b42015-10-05 12:30:12 -0600383 goto fail_irqfd;
Eric Augerfb5f8162015-07-06 12:15:14 -0600384 }
385
386 QLIST_FOREACH(intp, &vdev->intp_list, next) {
387 if (intp->qemuirq == irq) {
388 break;
389 }
390 }
391 assert(intp);
392
Eric Augera22313d2015-10-05 12:30:12 -0600393 if (kvm_irqchip_add_irqfd_notifier(kvm_state, intp->interrupt,
394 intp->unmask, irq) < 0) {
Eric Augerfb5f8162015-07-06 12:15:14 -0600395 goto fail_irqfd;
396 }
397
398 if (vfio_set_trigger_eventfd(intp, NULL) < 0) {
399 goto fail_vfio;
400 }
Eric Augera5b39cd2015-10-05 12:30:12 -0600401 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 Augerfb5f8162015-07-06 12:15:14 -0600411 }
412
Eric Augerfb5f8162015-07-06 12:15:14 -0600413 intp->kvm_accel = true;
414
Eric Augerfb5f8162015-07-06 12:15:14 -0600415 return;
416fail_vfio:
Eric Augera22313d2015-10-05 12:30:12 -0600417 kvm_irqchip_remove_irqfd_notifier(kvm_state, intp->interrupt, irq);
Eric Auger58892b42015-10-05 12:30:12 -0600418 abort();
Eric Augerfb5f8162015-07-06 12:15:14 -0600419fail_irqfd:
Eric Auger58892b42015-10-05 12:30:12 -0600420 vfio_start_eventfd_injection(sbdev, irq);
Eric Augerfb5f8162015-07-06 12:15:14 -0600421 return;
422}
423
Eric Auger0ea27302015-06-08 09:25:25 -0600424/* VFIO skeleton */
425
426static void vfio_platform_compute_needs_reset(VFIODevice *vbasedev)
427{
428 vbasedev->needs_reset = true;
429}
430
431/* not implemented yet */
432static 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 Auger38559972015-06-08 09:25:26 -0600439 * and IRQ structs according to driver returned information
Eric Auger0ea27302015-06-08 09:25:25 -0600440 * @vbasedev: the VFIO device handle
Eric Auger5ff74192016-10-17 10:58:00 -0600441 * @errp: error object
Eric Auger0ea27302015-06-08 09:25:25 -0600442 *
443 */
Eric Auger5ff74192016-10-17 10:58:00 -0600444static int vfio_populate_device(VFIODevice *vbasedev, Error **errp)
Eric Auger0ea27302015-06-08 09:25:25 -0600445{
Eric Auger38559972015-06-08 09:25:26 -0600446 VFIOINTp *intp, *tmp;
Eric Auger0ea27302015-06-08 09:25:25 -0600447 int i, ret = -1;
448 VFIOPlatformDevice *vdev =
449 container_of(vbasedev, VFIOPlatformDevice, vbasedev);
450
451 if (!(vbasedev->flags & VFIO_DEVICE_FLAGS_PLATFORM)) {
Eric Auger5ff74192016-10-17 10:58:00 -0600452 error_setg(errp, "this isn't a platform device");
Eric Auger0ea27302015-06-08 09:25:25 -0600453 return ret;
454 }
455
Eric Auger0b707432015-06-11 09:44:40 +0100456 vdev->regions = g_new0(VFIORegion *, vbasedev->num_regions);
Eric Auger0ea27302015-06-08 09:25:25 -0600457
458 for (i = 0; i < vbasedev->num_regions; i++) {
Alex Williamsondb0da022016-03-10 09:39:07 -0700459 char *name = g_strdup_printf("VFIO %s region %d\n", vbasedev->name, i);
Eric Auger0ea27302015-06-08 09:25:25 -0600460
Markus Armbrusterbdd81ad2015-11-10 12:11:08 -0700461 vdev->regions[i] = g_new0(VFIORegion, 1);
Alex Williamsondb0da022016-03-10 09:39:07 -0700462 ret = vfio_region_setup(OBJECT(vdev), vbasedev,
463 vdev->regions[i], i, name);
464 g_free(name);
Eric Auger0ea27302015-06-08 09:25:25 -0600465 if (ret) {
Eric Auger5ff74192016-10-17 10:58:00 -0600466 error_setg_errno(errp, -ret, "failed to get region %d info", i);
Eric Auger0ea27302015-06-08 09:25:25 -0600467 goto reg_error;
468 }
Eric Auger0ea27302015-06-08 09:25:25 -0600469 }
470
Eric Auger38559972015-06-08 09:25:26 -0600471 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 Auger5ff74192016-10-17 10:58:00 -0600482 error_setg_errno(errp, -ret, "failed to get device irq info");
Eric Auger38559972015-06-08 09:25:26 -0600483 goto irq_err;
484 } else {
485 trace_vfio_platform_populate_interrupts(irq.index,
486 irq.count,
487 irq.flags);
Eric Auger5ff74192016-10-17 10:58:00 -0600488 intp = vfio_init_intp(vbasedev, irq, errp);
Eric Auger38559972015-06-08 09:25:26 -0600489 if (!intp) {
Eric Auger0d84f472016-10-17 10:58:00 -0600490 ret = -1;
Eric Auger38559972015-06-08 09:25:26 -0600491 goto irq_err;
492 }
493 }
494 }
Eric Auger0ea27302015-06-08 09:25:25 -0600495 return 0;
Eric Auger38559972015-06-08 09:25:26 -0600496irq_err:
497 timer_del(vdev->mmap_timer);
498 QLIST_FOREACH_SAFE(intp, &vdev->intp_list, next, tmp) {
499 QLIST_REMOVE(intp, next);
500 g_free(intp);
501 }
Eric Auger0ea27302015-06-08 09:25:25 -0600502reg_error:
503 for (i = 0; i < vbasedev->num_regions; i++) {
Alex Williamsondb0da022016-03-10 09:39:07 -0700504 if (vdev->regions[i]) {
505 vfio_region_finalize(vdev->regions[i]);
506 }
Eric Auger0ea27302015-06-08 09:25:25 -0600507 g_free(vdev->regions[i]);
508 }
509 g_free(vdev->regions);
510 return ret;
511}
512
513/* specialized functions for VFIO Platform devices */
514static VFIODeviceOps vfio_platform_ops = {
515 .vfio_compute_needs_reset = vfio_platform_compute_needs_reset,
516 .vfio_hot_reset_multi = vfio_platform_hot_reset_multi,
Eric Auger38559972015-06-08 09:25:26 -0600517 .vfio_eoi = vfio_platform_eoi,
Eric Auger0ea27302015-06-08 09:25:25 -0600518};
519
520/**
521 * vfio_base_device_init - perform preliminary VFIO setup
522 * @vbasedev: the VFIO device handle
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600523 * @errp: error object
Eric Auger0ea27302015-06-08 09:25:25 -0600524 *
525 * Implement the VFIO command sequence that allows to discover
526 * assigned device resources: group extraction, device
527 * fd retrieval, resource query.
528 * Precondition: the device name must be initialized
529 */
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600530static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp)
Eric Auger0ea27302015-06-08 09:25:25 -0600531{
532 VFIOGroup *group;
533 VFIODevice *vbasedev_iter;
Alex Williamson7df93812016-03-10 09:39:07 -0700534 char *tmp, group_path[PATH_MAX], *group_name;
Eric Auger0ea27302015-06-08 09:25:25 -0600535 ssize_t len;
536 struct stat st;
537 int groupid;
538 int ret;
539
Alex Williamson7df93812016-03-10 09:39:07 -0700540 /* @sysfsdev takes precedence over @host */
541 if (vbasedev->sysfsdev) {
542 g_free(vbasedev->name);
Julia Suvorova3e015d82018-03-01 10:08:06 +0300543 vbasedev->name = g_path_get_basename(vbasedev->sysfsdev);
Alex Williamson7df93812016-03-10 09:39:07 -0700544 } else {
545 if (!vbasedev->name || strchr(vbasedev->name, '/')) {
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600546 error_setg(errp, "wrong host device name");
Alex Williamson7df93812016-03-10 09:39:07 -0700547 return -EINVAL;
548 }
549
550 vbasedev->sysfsdev = g_strdup_printf("/sys/bus/platform/devices/%s",
551 vbasedev->name);
Eric Auger0ea27302015-06-08 09:25:25 -0600552 }
553
Alex Williamson7df93812016-03-10 09:39:07 -0700554 if (stat(vbasedev->sysfsdev, &st) < 0) {
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600555 error_setg_errno(errp, errno,
556 "failed to get the sysfs host device file status");
Eric Auger0ea27302015-06-08 09:25:25 -0600557 return -errno;
558 }
559
Alex Williamson7df93812016-03-10 09:39:07 -0700560 tmp = g_strdup_printf("%s/iommu_group", vbasedev->sysfsdev);
561 len = readlink(tmp, group_path, sizeof(group_path));
562 g_free(tmp);
563
564 if (len < 0 || len >= sizeof(group_path)) {
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600565 ret = len < 0 ? -errno : -ENAMETOOLONG;
566 error_setg_errno(errp, -ret, "no iommu_group found");
567 return ret;
Eric Auger0ea27302015-06-08 09:25:25 -0600568 }
569
Alex Williamson7df93812016-03-10 09:39:07 -0700570 group_path[len] = 0;
Eric Auger0ea27302015-06-08 09:25:25 -0600571
Alex Williamson7df93812016-03-10 09:39:07 -0700572 group_name = basename(group_path);
Eric Auger0ea27302015-06-08 09:25:25 -0600573 if (sscanf(group_name, "%d", &groupid) != 1) {
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600574 error_setg_errno(errp, errno, "failed to read %s", group_path);
Eric Auger0ea27302015-06-08 09:25:25 -0600575 return -errno;
576 }
577
578 trace_vfio_platform_base_device_init(vbasedev->name, groupid);
579
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600580 group = vfio_get_group(groupid, &address_space_memory, errp);
Eric Auger0ea27302015-06-08 09:25:25 -0600581 if (!group) {
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600582 return -ENOENT;
Eric Auger0ea27302015-06-08 09:25:25 -0600583 }
584
Eric Auger0ea27302015-06-08 09:25:25 -0600585 QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
586 if (strcmp(vbasedev_iter->name, vbasedev->name) == 0) {
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600587 error_setg(errp, "device is already attached");
Eric Auger0ea27302015-06-08 09:25:25 -0600588 vfio_put_group(group);
589 return -EBUSY;
590 }
591 }
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600592 ret = vfio_get_device(group, vbasedev->name, vbasedev, errp);
Eric Auger0ea27302015-06-08 09:25:25 -0600593 if (ret) {
Eric Auger0ea27302015-06-08 09:25:25 -0600594 vfio_put_group(group);
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600595 return ret;
Eric Auger0ea27302015-06-08 09:25:25 -0600596 }
597
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600598 ret = vfio_populate_device(vbasedev, errp);
Eric Auger0ea27302015-06-08 09:25:25 -0600599 if (ret) {
Eric Auger0ea27302015-06-08 09:25:25 -0600600 vfio_put_group(group);
601 }
602
Eric Auger0ea27302015-06-08 09:25:25 -0600603 return ret;
604}
605
606/**
Eric Auger0ea27302015-06-08 09:25:25 -0600607 * vfio_platform_realize - the device realize function
608 * @dev: device state pointer
609 * @errp: error
610 *
611 * initialize the device, its memory regions and IRQ structures
612 * IRQ are started separately
613 */
614static void vfio_platform_realize(DeviceState *dev, Error **errp)
615{
616 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(dev);
617 SysBusDevice *sbdev = SYS_BUS_DEVICE(dev);
618 VFIODevice *vbasedev = &vdev->vbasedev;
619 int i, ret;
620
621 vbasedev->type = VFIO_DEVICE_TYPE_PLATFORM;
Alex Williamson7da624e2017-07-10 10:39:43 -0600622 vbasedev->dev = dev;
Eric Auger0ea27302015-06-08 09:25:25 -0600623 vbasedev->ops = &vfio_platform_ops;
624
Eric Auger89202c62018-02-06 11:08:26 -0700625 qemu_mutex_init(&vdev->intp_mutex);
626
Alex Williamson7df93812016-03-10 09:39:07 -0700627 trace_vfio_platform_realize(vbasedev->sysfsdev ?
628 vbasedev->sysfsdev : vbasedev->name,
629 vdev->compat);
Eric Auger0ea27302015-06-08 09:25:25 -0600630
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600631 ret = vfio_base_device_init(vbasedev, errp);
Eric Auger0ea27302015-06-08 09:25:25 -0600632 if (ret) {
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600633 goto out;
Eric Auger0ea27302015-06-08 09:25:25 -0600634 }
635
Eric Augera49531e2018-10-15 10:52:09 -0600636 if (!vdev->compat) {
637 GError *gerr = NULL;
638 gchar *contents;
639 gsize length;
640 char *path;
641
642 path = g_strdup_printf("%s/of_node/compatible", vbasedev->sysfsdev);
643 if (!g_file_get_contents(path, &contents, &length, &gerr)) {
644 error_setg(errp, "%s", gerr->message);
645 g_error_free(gerr);
646 g_free(path);
647 return;
648 }
649 g_free(path);
650 vdev->compat = contents;
651 for (vdev->num_compat = 0; length; vdev->num_compat++) {
652 size_t skip = strlen(contents) + 1;
653 contents += skip;
654 length -= skip;
655 }
656 }
657
Eric Auger0ea27302015-06-08 09:25:25 -0600658 for (i = 0; i < vbasedev->num_regions; i++) {
Alex Williamsondb0da022016-03-10 09:39:07 -0700659 if (vfio_region_mmap(vdev->regions[i])) {
Markus Armbrustere1eb2922018-10-17 10:26:29 +0200660 warn_report("%s mmap unsupported, performance may be slow",
661 memory_region_name(vdev->regions[i]->mem));
Alex Williamsondb0da022016-03-10 09:39:07 -0700662 }
663 sysbus_init_mmio(sbdev, vdev->regions[i]->mem);
Eric Auger0ea27302015-06-08 09:25:25 -0600664 }
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600665out:
666 if (!ret) {
667 return;
668 }
669
670 if (vdev->vbasedev.name) {
Markus Armbrusterc3b8e3e2018-10-17 10:26:30 +0200671 error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.name);
Eric Auger9bdbfbd2016-10-17 10:58:01 -0600672 } else {
673 error_prepend(errp, "vfio error: ");
674 }
Eric Auger0ea27302015-06-08 09:25:25 -0600675}
676
677static const VMStateDescription vfio_platform_vmstate = {
Li Qiangda56e332019-05-21 08:15:41 -0700678 .name = "vfio-platform",
Eric Auger0ea27302015-06-08 09:25:25 -0600679 .unmigratable = 1,
680};
681
682static Property vfio_platform_dev_properties[] = {
683 DEFINE_PROP_STRING("host", VFIOPlatformDevice, vbasedev.name),
Alex Williamson7df93812016-03-10 09:39:07 -0700684 DEFINE_PROP_STRING("sysfsdev", VFIOPlatformDevice, vbasedev.sysfsdev),
Alex Williamson5e15d792015-09-23 13:04:44 -0600685 DEFINE_PROP_BOOL("x-no-mmap", VFIOPlatformDevice, vbasedev.no_mmap, false),
Eric Auger38559972015-06-08 09:25:26 -0600686 DEFINE_PROP_UINT32("mmap-timeout-ms", VFIOPlatformDevice,
687 mmap_timeout, 1100),
Eric Augerfb5f8162015-07-06 12:15:14 -0600688 DEFINE_PROP_BOOL("x-irqfd", VFIOPlatformDevice, irqfd_allowed, true),
Eric Auger0ea27302015-06-08 09:25:25 -0600689 DEFINE_PROP_END_OF_LIST(),
690};
691
692static void vfio_platform_class_init(ObjectClass *klass, void *data)
693{
694 DeviceClass *dc = DEVICE_CLASS(klass);
Eric Augerfb5f8162015-07-06 12:15:14 -0600695 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
Eric Auger0ea27302015-06-08 09:25:25 -0600696
697 dc->realize = vfio_platform_realize;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400698 device_class_set_props(dc, vfio_platform_dev_properties);
Eric Auger0ea27302015-06-08 09:25:25 -0600699 dc->vmsd = &vfio_platform_vmstate;
700 dc->desc = "VFIO-based platform device assignment";
Eric Augerfb5f8162015-07-06 12:15:14 -0600701 sbc->connect_irq_notifier = vfio_start_irqfd_injection;
Eric Auger0ea27302015-06-08 09:25:25 -0600702 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
Eric Augera49531e2018-10-15 10:52:09 -0600703 /* Supported by TYPE_VIRT_MACHINE */
704 dc->user_creatable = true;
Eric Auger0ea27302015-06-08 09:25:25 -0600705}
706
707static const TypeInfo vfio_platform_dev_info = {
708 .name = TYPE_VFIO_PLATFORM,
709 .parent = TYPE_SYS_BUS_DEVICE,
710 .instance_size = sizeof(VFIOPlatformDevice),
711 .class_init = vfio_platform_class_init,
712 .class_size = sizeof(VFIOPlatformDeviceClass),
Eric Auger0ea27302015-06-08 09:25:25 -0600713};
714
715static void register_vfio_platform_dev_type(void)
716{
717 type_register_static(&vfio_platform_dev_info);
718}
719
720type_init(register_vfio_platform_dev_type)