tests/acpi: update expected data files for microvm
[qemu/ar7.git] / hw / vfio / ap.c
blob582c091a245c4a28a62716f3b77bf7bf0cb7bf51
1 /*
2 * VFIO based AP matrix device assignment
4 * Copyright 2018 IBM Corp.
5 * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
6 * Halil Pasic <pasic@linux.ibm.com>
8 * This work is licensed under the terms of the GNU GPL, version 2 or (at
9 * your option) any later version. See the COPYING file in the top-level
10 * directory.
13 #include "qemu/osdep.h"
14 #include <linux/vfio.h>
15 #include <sys/ioctl.h>
16 #include "qapi/error.h"
17 #include "hw/sysbus.h"
18 #include "hw/vfio/vfio.h"
19 #include "hw/vfio/vfio-common.h"
20 #include "hw/s390x/ap-device.h"
21 #include "qemu/error-report.h"
22 #include "qemu/module.h"
23 #include "qemu/option.h"
24 #include "qemu/config-file.h"
25 #include "cpu.h"
26 #include "kvm_s390x.h"
27 #include "migration/vmstate.h"
28 #include "hw/qdev-properties.h"
29 #include "hw/s390x/ap-bridge.h"
30 #include "exec/address-spaces.h"
31 #include "qom/object.h"
33 #define TYPE_VFIO_AP_DEVICE "vfio-ap"
35 struct VFIOAPDevice {
36 APDevice apdev;
37 VFIODevice vdev;
39 typedef struct VFIOAPDevice VFIOAPDevice;
41 DECLARE_INSTANCE_CHECKER(VFIOAPDevice, VFIO_AP_DEVICE,
42 TYPE_VFIO_AP_DEVICE)
44 static void vfio_ap_compute_needs_reset(VFIODevice *vdev)
46 vdev->needs_reset = false;
50 * We don't need vfio_hot_reset_multi and vfio_eoi operations for
51 * vfio-ap device now.
53 struct VFIODeviceOps vfio_ap_ops = {
54 .vfio_compute_needs_reset = vfio_ap_compute_needs_reset,
57 static void vfio_ap_put_device(VFIOAPDevice *vapdev)
59 g_free(vapdev->vdev.name);
60 vfio_put_base_device(&vapdev->vdev);
63 static VFIOGroup *vfio_ap_get_group(VFIOAPDevice *vapdev, Error **errp)
65 GError *gerror = NULL;
66 char *symlink, *group_path;
67 int groupid;
69 symlink = g_strdup_printf("%s/iommu_group", vapdev->vdev.sysfsdev);
70 group_path = g_file_read_link(symlink, &gerror);
71 g_free(symlink);
73 if (!group_path) {
74 error_setg(errp, "%s: no iommu_group found for %s: %s",
75 TYPE_VFIO_AP_DEVICE, vapdev->vdev.sysfsdev, gerror->message);
76 g_error_free(gerror);
77 return NULL;
80 if (sscanf(basename(group_path), "%d", &groupid) != 1) {
81 error_setg(errp, "vfio: failed to read %s", group_path);
82 g_free(group_path);
83 return NULL;
86 g_free(group_path);
88 return vfio_get_group(groupid, &address_space_memory, errp);
91 static void vfio_ap_realize(DeviceState *dev, Error **errp)
93 int ret;
94 char *mdevid;
95 VFIOGroup *vfio_group;
96 APDevice *apdev = AP_DEVICE(dev);
97 VFIOAPDevice *vapdev = VFIO_AP_DEVICE(apdev);
99 vfio_group = vfio_ap_get_group(vapdev, errp);
100 if (!vfio_group) {
101 return;
104 vapdev->vdev.ops = &vfio_ap_ops;
105 vapdev->vdev.type = VFIO_DEVICE_TYPE_AP;
106 mdevid = basename(vapdev->vdev.sysfsdev);
107 vapdev->vdev.name = g_strdup_printf("%s", mdevid);
108 vapdev->vdev.dev = dev;
111 * vfio-ap devices operate in a way compatible with discarding of
112 * memory in RAM blocks, as no pages are pinned in the host.
113 * This needs to be set before vfio_get_device() for vfio common to
114 * handle ram_block_discard_disable().
116 vapdev->vdev.ram_block_discard_allowed = true;
118 ret = vfio_get_device(vfio_group, mdevid, &vapdev->vdev, errp);
119 if (ret) {
120 goto out_get_dev_err;
123 return;
125 out_get_dev_err:
126 vfio_ap_put_device(vapdev);
127 vfio_put_group(vfio_group);
130 static void vfio_ap_unrealize(DeviceState *dev)
132 APDevice *apdev = AP_DEVICE(dev);
133 VFIOAPDevice *vapdev = VFIO_AP_DEVICE(apdev);
134 VFIOGroup *group = vapdev->vdev.group;
136 vfio_ap_put_device(vapdev);
137 vfio_put_group(group);
140 static Property vfio_ap_properties[] = {
141 DEFINE_PROP_STRING("sysfsdev", VFIOAPDevice, vdev.sysfsdev),
142 DEFINE_PROP_END_OF_LIST(),
145 static void vfio_ap_reset(DeviceState *dev)
147 int ret;
148 APDevice *apdev = AP_DEVICE(dev);
149 VFIOAPDevice *vapdev = VFIO_AP_DEVICE(apdev);
151 ret = ioctl(vapdev->vdev.fd, VFIO_DEVICE_RESET);
152 if (ret) {
153 error_report("%s: failed to reset %s device: %s", __func__,
154 vapdev->vdev.name, strerror(errno));
158 static const VMStateDescription vfio_ap_vmstate = {
159 .name = "vfio-ap",
160 .unmigratable = 1,
163 static void vfio_ap_class_init(ObjectClass *klass, void *data)
165 DeviceClass *dc = DEVICE_CLASS(klass);
167 device_class_set_props(dc, vfio_ap_properties);
168 dc->vmsd = &vfio_ap_vmstate;
169 dc->desc = "VFIO-based AP device assignment";
170 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
171 dc->realize = vfio_ap_realize;
172 dc->unrealize = vfio_ap_unrealize;
173 dc->hotpluggable = true;
174 dc->reset = vfio_ap_reset;
175 dc->bus_type = TYPE_AP_BUS;
178 static const TypeInfo vfio_ap_info = {
179 .name = TYPE_VFIO_AP_DEVICE,
180 .parent = TYPE_AP_DEVICE,
181 .instance_size = sizeof(VFIOAPDevice),
182 .class_init = vfio_ap_class_init,
185 static void vfio_ap_type_init(void)
187 type_register_static(&vfio_ap_info);
190 type_init(vfio_ap_type_init)