]> git.proxmox.com Git - mirror_qemu.git/blame - hw/vfio/platform.c
Merge remote-tracking branch 'remotes/riscv/tags/riscv-for-master-3.1-sf0' into staging
[mirror_qemu.git] / hw / vfio / platform.c
CommitLineData
0ea2730b
EA
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
c6eacb1a 17#include "qemu/osdep.h"
da34e65c 18#include "qapi/error.h"
0ea2730b 19#include <sys/ioctl.h>
e2075277 20#include <linux/vfio.h>
0ea2730b
EA
21
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"
d791937f 27#include "exec/address-spaces.h"
38559979 28#include "qemu/queue.h"
0ea2730b
EA
29#include "hw/sysbus.h"
30#include "trace.h"
31#include "hw/platform-bus.h"
fb5f8164 32#include "sysemu/kvm.h"
0ea2730b 33
38559979
EA
34/*
35 * Functions used whatever the injection method
36 */
37
a5b39cd3
EA
38static inline bool vfio_irq_is_automasked(VFIOINTp *intp)
39{
40 return intp->flags & VFIO_IRQ_INFO_AUTOMASKED;
41}
42
38559979
EA
43/**
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
5ff7419d 48 * @errp: error object
38559979
EA
49 */
50static VFIOINTp *vfio_init_intp(VFIODevice *vbasedev,
5ff7419d 51 struct vfio_irq_info info, Error **errp)
38559979
EA
52{
53 int ret;
54 VFIOPlatformDevice *vdev =
55 container_of(vbasedev, VFIOPlatformDevice, vbasedev);
56 SysBusDevice *sbdev = SYS_BUS_DEVICE(vdev);
57 VFIOINTp *intp;
58
59 intp = g_malloc0(sizeof(*intp));
60 intp->vdev = vdev;
61 intp->pin = info.index;
62 intp->flags = info.flags;
63 intp->state = VFIO_IRQ_INACTIVE;
fb5f8164 64 intp->kvm_accel = false;
38559979
EA
65
66 sysbus_init_irq(sbdev, &intp->qemuirq);
67
68 /* Get an eventfd for trigger */
a22313de
EA
69 intp->interrupt = g_malloc0(sizeof(EventNotifier));
70 ret = event_notifier_init(intp->interrupt, 0);
38559979 71 if (ret) {
a22313de 72 g_free(intp->interrupt);
38559979 73 g_free(intp);
5ff7419d
EA
74 error_setg_errno(errp, -ret,
75 "failed to initialize trigger eventd notifier");
38559979
EA
76 return NULL;
77 }
a5b39cd3
EA
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);
82 if (ret) {
83 g_free(intp->interrupt);
84 g_free(intp->unmask);
85 g_free(intp);
5ff7419d
EA
86 error_setg_errno(errp, -ret,
87 "failed to initialize resample eventd notifier");
a5b39cd3
EA
88 return NULL;
89 }
fb5f8164 90 }
38559979
EA
91
92 QLIST_INSERT_HEAD(&vdev->intp_list, intp, next);
93 return intp;
94}
95
96/**
97 * vfio_set_trigger_eventfd - set VFIO eventfd handling
98 *
99 * @intp: IRQ struct handle
100 * @handler: handler to be called on eventfd signaling
101 *
102 * Setup VFIO signaling and attach an optional user-side handler
103 * to the eventfd
104 */
105static int vfio_set_trigger_eventfd(VFIOINTp *intp,
106 eventfd_user_side_handler_t handler)
107{
108 VFIODevice *vbasedev = &intp->vdev->vbasedev;
109 struct vfio_irq_set *irq_set;
110 int argsz, ret;
111 int32_t *pfd;
112
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;
118 irq_set->start = 0;
119 irq_set->count = 1;
120 pfd = (int32_t *)&irq_set->data;
a22313de 121 *pfd = event_notifier_get_fd(intp->interrupt);
38559979
EA
122 qemu_set_fd_handler(*pfd, (IOHandler *)handler, NULL, intp);
123 ret = ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set);
38559979
EA
124 if (ret < 0) {
125 error_report("vfio: Failed to set trigger eventfd: %m");
126 qemu_set_fd_handler(*pfd, NULL, NULL, NULL);
127 }
418c6981 128 g_free(irq_set);
38559979
EA
129 return ret;
130}
131
132/*
133 * Functions only used when eventfds are handled on user-side
134 * ie. without irqfd
135 */
136
137/**
138 * vfio_mmap_set_enabled - enable/disable the fast path mode
139 * @vdev: the VFIO platform device
140 * @enabled: the target mmap state
141 *
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
145*/
146
147static void vfio_mmap_set_enabled(VFIOPlatformDevice *vdev, bool enabled)
148{
149 int i;
150
38559979 151 for (i = 0; i < vdev->vbasedev.num_regions; i++) {
db0da029 152 vfio_region_mmaps_set_enabled(vdev->regions[i], enabled);
38559979
EA
153 }
154}
155
156/**
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
160 *
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.
165 */
166static void vfio_intp_mmap_enable(void *opaque)
167{
168 VFIOINTp *tmp;
169 VFIOPlatformDevice *vdev = (VFIOPlatformDevice *)opaque;
170
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) +
178 vdev->mmap_timeout);
179 qemu_mutex_unlock(&vdev->intp_mutex);
180 return;
181 }
182 }
183 vfio_mmap_set_enabled(vdev, true);
184 qemu_mutex_unlock(&vdev->intp_mutex);
185}
186
187/**
188 * vfio_intp_inject_pending_lockheld - Injects a pending IRQ
189 * @opaque: opaque pointer, in practice the VFIOINTp handle
190 *
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.
195 */
196static void vfio_intp_inject_pending_lockheld(VFIOINTp *intp)
197{
198 trace_vfio_platform_intp_inject_pending_lockheld(intp->pin,
a22313de 199 event_notifier_get_fd(intp->interrupt));
38559979
EA
200
201 intp->state = VFIO_IRQ_ACTIVE;
202
203 /* trigger the virtual IRQ */
204 qemu_set_irq(intp->qemuirq, 1);
205}
206
207/**
208 * vfio_intp_interrupt - The user-side eventfd handler
209 * @opaque: opaque pointer which in practice is the VFIOINTp handle
210 *
211 * the function is entered in event handler context:
212 * the vIRQ is injected into the guest if there is no other active
213 * or pending IRQ.
214 */
215static void vfio_intp_interrupt(VFIOINTp *intp)
216{
217 int ret;
218 VFIOINTp *tmp;
219 VFIOPlatformDevice *vdev = intp->vdev;
220 bool delay_handling = false;
221
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;
228 break;
229 }
230 }
231 }
232 if (delay_handling) {
233 /*
234 * the new IRQ gets a pending status and is pushed in
235 * the pending queue
236 */
237 intp->state = VFIO_IRQ_PENDING;
238 trace_vfio_intp_interrupt_set_pending(intp->pin);
239 QSIMPLEQ_INSERT_TAIL(&vdev->pending_intp_queue,
240 intp, pqnext);
a22313de 241 ret = event_notifier_test_and_clear(intp->interrupt);
38559979
EA
242 qemu_mutex_unlock(&vdev->intp_mutex);
243 return;
244 }
245
246 trace_vfio_platform_intp_interrupt(intp->pin,
a22313de 247 event_notifier_get_fd(intp->interrupt));
38559979 248
a22313de 249 ret = event_notifier_test_and_clear(intp->interrupt);
38559979 250 if (!ret) {
594fd211 251 error_report("Error when clearing fd=%d (ret = %d)",
a22313de 252 event_notifier_get_fd(intp->interrupt), ret);
38559979
EA
253 }
254
255 intp->state = VFIO_IRQ_ACTIVE;
256
257 /* sets slow path */
258 vfio_mmap_set_enabled(vdev, false);
259
260 /* trigger the virtual IRQ */
261 qemu_set_irq(intp->qemuirq, 1);
262
263 /*
264 * Schedule the mmap timer which will restore fastpath when no IRQ
265 * is active anymore
266 */
267 if (vdev->mmap_timeout) {
268 timer_mod(vdev->mmap_timer,
269 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
270 vdev->mmap_timeout);
271 }
272 qemu_mutex_unlock(&vdev->intp_mutex);
273}
274
275/**
276 * vfio_platform_eoi - IRQ completion routine
277 * @vbasedev: the VFIO device handle
278 *
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).
289 */
290static void vfio_platform_eoi(VFIODevice *vbasedev)
291{
292 VFIOINTp *intp;
293 VFIOPlatformDevice *vdev =
294 container_of(vbasedev, VFIOPlatformDevice, vbasedev);
295
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,
a22313de 300 event_notifier_get_fd(intp->interrupt));
38559979
EA
301 intp->state = VFIO_IRQ_INACTIVE;
302
303 /* deassert the virtual IRQ */
304 qemu_set_irq(intp->qemuirq, 0);
305
a5b39cd3 306 if (vfio_irq_is_automasked(intp)) {
38559979
EA
307 /* unmasks the physical level-sensitive IRQ */
308 vfio_unmask_single_irqindex(vbasedev, intp->pin);
309 }
310
311 /* a single IRQ can be active at a time */
312 break;
313 }
314 }
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);
320 }
321 qemu_mutex_unlock(&vdev->intp_mutex);
322}
323
324/**
325 * vfio_start_eventfd_injection - starts the virtual IRQ injection using
326 * user-side handled eventfds
58892b44
EA
327 * @sbdev: the sysbus device handle
328 * @irq: the qemu irq handle
38559979
EA
329 */
330
58892b44 331static void vfio_start_eventfd_injection(SysBusDevice *sbdev, qemu_irq irq)
38559979
EA
332{
333 int ret;
58892b44
EA
334 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
335 VFIOINTp *intp;
336
337 QLIST_FOREACH(intp, &vdev->intp_list, next) {
338 if (intp->qemuirq == irq) {
339 break;
340 }
341 }
342 assert(intp);
38559979
EA
343
344 ret = vfio_set_trigger_eventfd(intp, vfio_intp_interrupt);
345 if (ret) {
58892b44
EA
346 error_report("vfio: failed to start eventfd signaling for IRQ %d: %m",
347 intp->pin);
348 abort();
38559979 349 }
38559979
EA
350}
351
fb5f8164
EA
352/*
353 * Functions used for irqfd
354 */
355
356/**
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
361 */
362static int vfio_set_resample_eventfd(VFIOINTp *intp)
363{
364 VFIODevice *vbasedev = &intp->vdev->vbasedev;
365 struct vfio_irq_set *irq_set;
366 int argsz, ret;
367 int32_t *pfd;
368
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;
374 irq_set->start = 0;
375 irq_set->count = 1;
376 pfd = (int32_t *)&irq_set->data;
a22313de 377 *pfd = event_notifier_get_fd(intp->unmask);
fb5f8164
EA
378 qemu_set_fd_handler(*pfd, NULL, NULL, NULL);
379 ret = ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set);
380 g_free(irq_set);
381 if (ret < 0) {
382 error_report("vfio: Failed to set resample eventfd: %m");
383 }
384 return ret;
385}
386
58892b44
EA
387/**
388 * vfio_start_irqfd_injection - starts the virtual IRQ injection using
389 * irqfd
390 *
391 * @sbdev: the sysbus device handle
392 * @irq: the qemu irq handle
393 *
394 * In case the irqfd setup fails, we fallback to userspace handled eventfd
395 */
fb5f8164
EA
396static void vfio_start_irqfd_injection(SysBusDevice *sbdev, qemu_irq irq)
397{
398 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
399 VFIOINTp *intp;
400
401 if (!kvm_irqfds_enabled() || !kvm_resamplefds_enabled() ||
402 !vdev->irqfd_allowed) {
58892b44 403 goto fail_irqfd;
fb5f8164
EA
404 }
405
406 QLIST_FOREACH(intp, &vdev->intp_list, next) {
407 if (intp->qemuirq == irq) {
408 break;
409 }
410 }
411 assert(intp);
412
a22313de
EA
413 if (kvm_irqchip_add_irqfd_notifier(kvm_state, intp->interrupt,
414 intp->unmask, irq) < 0) {
fb5f8164
EA
415 goto fail_irqfd;
416 }
417
418 if (vfio_set_trigger_eventfd(intp, NULL) < 0) {
419 goto fail_vfio;
420 }
a5b39cd3
EA
421 if (vfio_irq_is_automasked(intp)) {
422 if (vfio_set_resample_eventfd(intp) < 0) {
423 goto fail_vfio;
424 }
425 trace_vfio_platform_start_level_irqfd_injection(intp->pin,
426 event_notifier_get_fd(intp->interrupt),
427 event_notifier_get_fd(intp->unmask));
428 } else {
429 trace_vfio_platform_start_edge_irqfd_injection(intp->pin,
430 event_notifier_get_fd(intp->interrupt));
fb5f8164
EA
431 }
432
fb5f8164
EA
433 intp->kvm_accel = true;
434
fb5f8164
EA
435 return;
436fail_vfio:
a22313de 437 kvm_irqchip_remove_irqfd_notifier(kvm_state, intp->interrupt, irq);
58892b44
EA
438 error_report("vfio: failed to start eventfd signaling for IRQ %d: %m",
439 intp->pin);
440 abort();
fb5f8164 441fail_irqfd:
58892b44 442 vfio_start_eventfd_injection(sbdev, irq);
fb5f8164
EA
443 return;
444}
445
0ea2730b
EA
446/* VFIO skeleton */
447
448static void vfio_platform_compute_needs_reset(VFIODevice *vbasedev)
449{
450 vbasedev->needs_reset = true;
451}
452
453/* not implemented yet */
454static int vfio_platform_hot_reset_multi(VFIODevice *vbasedev)
455{
456 return -1;
457}
458
459/**
460 * vfio_populate_device - Allocate and populate MMIO region
38559979 461 * and IRQ structs according to driver returned information
0ea2730b 462 * @vbasedev: the VFIO device handle
5ff7419d 463 * @errp: error object
0ea2730b
EA
464 *
465 */
5ff7419d 466static int vfio_populate_device(VFIODevice *vbasedev, Error **errp)
0ea2730b 467{
38559979 468 VFIOINTp *intp, *tmp;
0ea2730b
EA
469 int i, ret = -1;
470 VFIOPlatformDevice *vdev =
471 container_of(vbasedev, VFIOPlatformDevice, vbasedev);
472
473 if (!(vbasedev->flags & VFIO_DEVICE_FLAGS_PLATFORM)) {
5ff7419d 474 error_setg(errp, "this isn't a platform device");
0ea2730b
EA
475 return ret;
476 }
477
0b70743d 478 vdev->regions = g_new0(VFIORegion *, vbasedev->num_regions);
0ea2730b
EA
479
480 for (i = 0; i < vbasedev->num_regions; i++) {
db0da029 481 char *name = g_strdup_printf("VFIO %s region %d\n", vbasedev->name, i);
0ea2730b 482
bdd81add 483 vdev->regions[i] = g_new0(VFIORegion, 1);
db0da029
AW
484 ret = vfio_region_setup(OBJECT(vdev), vbasedev,
485 vdev->regions[i], i, name);
486 g_free(name);
0ea2730b 487 if (ret) {
5ff7419d 488 error_setg_errno(errp, -ret, "failed to get region %d info", i);
0ea2730b
EA
489 goto reg_error;
490 }
0ea2730b
EA
491 }
492
38559979
EA
493 vdev->mmap_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
494 vfio_intp_mmap_enable, vdev);
495
496 QSIMPLEQ_INIT(&vdev->pending_intp_queue);
497
498 for (i = 0; i < vbasedev->num_irqs; i++) {
499 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
500
501 irq.index = i;
502 ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
503 if (ret) {
5ff7419d 504 error_setg_errno(errp, -ret, "failed to get device irq info");
38559979
EA
505 goto irq_err;
506 } else {
507 trace_vfio_platform_populate_interrupts(irq.index,
508 irq.count,
509 irq.flags);
5ff7419d 510 intp = vfio_init_intp(vbasedev, irq, errp);
38559979 511 if (!intp) {
0d84f47b 512 ret = -1;
38559979
EA
513 goto irq_err;
514 }
515 }
516 }
0ea2730b 517 return 0;
38559979
EA
518irq_err:
519 timer_del(vdev->mmap_timer);
520 QLIST_FOREACH_SAFE(intp, &vdev->intp_list, next, tmp) {
521 QLIST_REMOVE(intp, next);
522 g_free(intp);
523 }
0ea2730b
EA
524reg_error:
525 for (i = 0; i < vbasedev->num_regions; i++) {
db0da029
AW
526 if (vdev->regions[i]) {
527 vfio_region_finalize(vdev->regions[i]);
528 }
0ea2730b
EA
529 g_free(vdev->regions[i]);
530 }
531 g_free(vdev->regions);
532 return ret;
533}
534
535/* specialized functions for VFIO Platform devices */
536static VFIODeviceOps vfio_platform_ops = {
537 .vfio_compute_needs_reset = vfio_platform_compute_needs_reset,
538 .vfio_hot_reset_multi = vfio_platform_hot_reset_multi,
38559979 539 .vfio_eoi = vfio_platform_eoi,
0ea2730b
EA
540};
541
542/**
543 * vfio_base_device_init - perform preliminary VFIO setup
544 * @vbasedev: the VFIO device handle
9bdbfbd5 545 * @errp: error object
0ea2730b
EA
546 *
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
551 */
9bdbfbd5 552static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp)
0ea2730b
EA
553{
554 VFIOGroup *group;
555 VFIODevice *vbasedev_iter;
7df9381b 556 char *tmp, group_path[PATH_MAX], *group_name;
0ea2730b
EA
557 ssize_t len;
558 struct stat st;
559 int groupid;
560 int ret;
561
7df9381b
AW
562 /* @sysfsdev takes precedence over @host */
563 if (vbasedev->sysfsdev) {
564 g_free(vbasedev->name);
3e015d81 565 vbasedev->name = g_path_get_basename(vbasedev->sysfsdev);
7df9381b
AW
566 } else {
567 if (!vbasedev->name || strchr(vbasedev->name, '/')) {
9bdbfbd5 568 error_setg(errp, "wrong host device name");
7df9381b
AW
569 return -EINVAL;
570 }
0ea2730b 571
7df9381b
AW
572 vbasedev->sysfsdev = g_strdup_printf("/sys/bus/platform/devices/%s",
573 vbasedev->name);
574 }
0ea2730b 575
7df9381b 576 if (stat(vbasedev->sysfsdev, &st) < 0) {
9bdbfbd5
EA
577 error_setg_errno(errp, errno,
578 "failed to get the sysfs host device file status");
0ea2730b
EA
579 return -errno;
580 }
581
7df9381b
AW
582 tmp = g_strdup_printf("%s/iommu_group", vbasedev->sysfsdev);
583 len = readlink(tmp, group_path, sizeof(group_path));
584 g_free(tmp);
585
586 if (len < 0 || len >= sizeof(group_path)) {
9bdbfbd5
EA
587 ret = len < 0 ? -errno : -ENAMETOOLONG;
588 error_setg_errno(errp, -ret, "no iommu_group found");
589 return ret;
0ea2730b
EA
590 }
591
7df9381b 592 group_path[len] = 0;
0ea2730b 593
7df9381b 594 group_name = basename(group_path);
0ea2730b 595 if (sscanf(group_name, "%d", &groupid) != 1) {
9bdbfbd5 596 error_setg_errno(errp, errno, "failed to read %s", group_path);
0ea2730b
EA
597 return -errno;
598 }
599
600 trace_vfio_platform_base_device_init(vbasedev->name, groupid);
601
9bdbfbd5 602 group = vfio_get_group(groupid, &address_space_memory, errp);
0ea2730b 603 if (!group) {
9bdbfbd5 604 return -ENOENT;
0ea2730b
EA
605 }
606
0ea2730b
EA
607 QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
608 if (strcmp(vbasedev_iter->name, vbasedev->name) == 0) {
9bdbfbd5 609 error_setg(errp, "device is already attached");
0ea2730b
EA
610 vfio_put_group(group);
611 return -EBUSY;
612 }
613 }
9bdbfbd5 614 ret = vfio_get_device(group, vbasedev->name, vbasedev, errp);
0ea2730b 615 if (ret) {
0ea2730b 616 vfio_put_group(group);
9bdbfbd5 617 return ret;
0ea2730b
EA
618 }
619
9bdbfbd5 620 ret = vfio_populate_device(vbasedev, errp);
0ea2730b 621 if (ret) {
0ea2730b
EA
622 vfio_put_group(group);
623 }
624
625 return ret;
626}
627
0ea2730b
EA
628/**
629 * vfio_platform_realize - the device realize function
630 * @dev: device state pointer
631 * @errp: error
632 *
633 * initialize the device, its memory regions and IRQ structures
634 * IRQ are started separately
635 */
636static void vfio_platform_realize(DeviceState *dev, Error **errp)
637{
638 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(dev);
639 SysBusDevice *sbdev = SYS_BUS_DEVICE(dev);
640 VFIODevice *vbasedev = &vdev->vbasedev;
641 int i, ret;
642
643 vbasedev->type = VFIO_DEVICE_TYPE_PLATFORM;
7da624e2 644 vbasedev->dev = dev;
0ea2730b
EA
645 vbasedev->ops = &vfio_platform_ops;
646
89202c6f
EA
647 qemu_mutex_init(&vdev->intp_mutex);
648
7df9381b
AW
649 trace_vfio_platform_realize(vbasedev->sysfsdev ?
650 vbasedev->sysfsdev : vbasedev->name,
651 vdev->compat);
0ea2730b 652
9bdbfbd5 653 ret = vfio_base_device_init(vbasedev, errp);
0ea2730b 654 if (ret) {
9bdbfbd5 655 goto out;
0ea2730b
EA
656 }
657
a49531eb
EA
658 if (!vdev->compat) {
659 GError *gerr = NULL;
660 gchar *contents;
661 gsize length;
662 char *path;
663
664 path = g_strdup_printf("%s/of_node/compatible", vbasedev->sysfsdev);
665 if (!g_file_get_contents(path, &contents, &length, &gerr)) {
666 error_setg(errp, "%s", gerr->message);
667 g_error_free(gerr);
668 g_free(path);
669 return;
670 }
671 g_free(path);
672 vdev->compat = contents;
673 for (vdev->num_compat = 0; length; vdev->num_compat++) {
674 size_t skip = strlen(contents) + 1;
675 contents += skip;
676 length -= skip;
677 }
678 }
679
0ea2730b 680 for (i = 0; i < vbasedev->num_regions; i++) {
db0da029 681 if (vfio_region_mmap(vdev->regions[i])) {
e1eb292a
MA
682 warn_report("%s mmap unsupported, performance may be slow",
683 memory_region_name(vdev->regions[i]->mem));
db0da029
AW
684 }
685 sysbus_init_mmio(sbdev, vdev->regions[i]->mem);
0ea2730b 686 }
9bdbfbd5
EA
687out:
688 if (!ret) {
689 return;
690 }
691
692 if (vdev->vbasedev.name) {
c3b8e3e0 693 error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.name);
9bdbfbd5
EA
694 } else {
695 error_prepend(errp, "vfio error: ");
696 }
0ea2730b
EA
697}
698
699static const VMStateDescription vfio_platform_vmstate = {
700 .name = TYPE_VFIO_PLATFORM,
701 .unmigratable = 1,
702};
703
704static Property vfio_platform_dev_properties[] = {
705 DEFINE_PROP_STRING("host", VFIOPlatformDevice, vbasedev.name),
7df9381b 706 DEFINE_PROP_STRING("sysfsdev", VFIOPlatformDevice, vbasedev.sysfsdev),
5e15d79b 707 DEFINE_PROP_BOOL("x-no-mmap", VFIOPlatformDevice, vbasedev.no_mmap, false),
38559979
EA
708 DEFINE_PROP_UINT32("mmap-timeout-ms", VFIOPlatformDevice,
709 mmap_timeout, 1100),
fb5f8164 710 DEFINE_PROP_BOOL("x-irqfd", VFIOPlatformDevice, irqfd_allowed, true),
0ea2730b
EA
711 DEFINE_PROP_END_OF_LIST(),
712};
713
714static void vfio_platform_class_init(ObjectClass *klass, void *data)
715{
716 DeviceClass *dc = DEVICE_CLASS(klass);
fb5f8164 717 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
0ea2730b
EA
718
719 dc->realize = vfio_platform_realize;
720 dc->props = vfio_platform_dev_properties;
721 dc->vmsd = &vfio_platform_vmstate;
722 dc->desc = "VFIO-based platform device assignment";
fb5f8164 723 sbc->connect_irq_notifier = vfio_start_irqfd_injection;
0ea2730b 724 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
a49531eb
EA
725 /* Supported by TYPE_VIRT_MACHINE */
726 dc->user_creatable = true;
0ea2730b
EA
727}
728
729static const TypeInfo vfio_platform_dev_info = {
730 .name = TYPE_VFIO_PLATFORM,
731 .parent = TYPE_SYS_BUS_DEVICE,
732 .instance_size = sizeof(VFIOPlatformDevice),
733 .class_init = vfio_platform_class_init,
734 .class_size = sizeof(VFIOPlatformDeviceClass),
0ea2730b
EA
735};
736
737static void register_vfio_platform_dev_type(void)
738{
739 type_register_static(&vfio_platform_dev_info);
740}
741
742type_init(register_vfio_platform_dev_type)