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
13 #include "qemu/osdep.h"
14 #include <linux/vfio.h>
15 #include <sys/ioctl.h>
16 #include "qapi/error.h"
17 #include "hw/vfio/vfio.h"
18 #include "hw/vfio/vfio-common.h"
19 #include "hw/s390x/ap-device.h"
20 #include "qemu/error-report.h"
21 #include "qemu/module.h"
22 #include "qemu/option.h"
23 #include "qemu/config-file.h"
25 #include "kvm_s390x.h"
26 #include "migration/vmstate.h"
27 #include "hw/qdev-properties.h"
28 #include "hw/s390x/ap-bridge.h"
29 #include "exec/address-spaces.h"
30 #include "qom/object.h"
32 #define TYPE_VFIO_AP_DEVICE "vfio-ap"
39 OBJECT_DECLARE_SIMPLE_TYPE(VFIOAPDevice
, VFIO_AP_DEVICE
)
41 static void vfio_ap_compute_needs_reset(VFIODevice
*vdev
)
43 vdev
->needs_reset
= false;
47 * We don't need vfio_hot_reset_multi and vfio_eoi operations for
50 struct VFIODeviceOps vfio_ap_ops
= {
51 .vfio_compute_needs_reset
= vfio_ap_compute_needs_reset
,
54 static void vfio_ap_put_device(VFIOAPDevice
*vapdev
)
56 g_free(vapdev
->vdev
.name
);
57 vfio_put_base_device(&vapdev
->vdev
);
60 static VFIOGroup
*vfio_ap_get_group(VFIOAPDevice
*vapdev
, Error
**errp
)
62 GError
*gerror
= NULL
;
63 char *symlink
, *group_path
;
66 symlink
= g_strdup_printf("%s/iommu_group", vapdev
->vdev
.sysfsdev
);
67 group_path
= g_file_read_link(symlink
, &gerror
);
71 error_setg(errp
, "%s: no iommu_group found for %s: %s",
72 TYPE_VFIO_AP_DEVICE
, vapdev
->vdev
.sysfsdev
, gerror
->message
);
77 if (sscanf(basename(group_path
), "%d", &groupid
) != 1) {
78 error_setg(errp
, "vfio: failed to read %s", group_path
);
85 return vfio_get_group(groupid
, &address_space_memory
, errp
);
88 static void vfio_ap_realize(DeviceState
*dev
, Error
**errp
)
92 VFIOGroup
*vfio_group
;
93 APDevice
*apdev
= AP_DEVICE(dev
);
94 VFIOAPDevice
*vapdev
= VFIO_AP_DEVICE(apdev
);
96 vfio_group
= vfio_ap_get_group(vapdev
, errp
);
101 vapdev
->vdev
.ops
= &vfio_ap_ops
;
102 vapdev
->vdev
.type
= VFIO_DEVICE_TYPE_AP
;
103 mdevid
= basename(vapdev
->vdev
.sysfsdev
);
104 vapdev
->vdev
.name
= g_strdup_printf("%s", mdevid
);
105 vapdev
->vdev
.dev
= dev
;
108 * vfio-ap devices operate in a way compatible with discarding of
109 * memory in RAM blocks, as no pages are pinned in the host.
110 * This needs to be set before vfio_get_device() for vfio common to
111 * handle ram_block_discard_disable().
113 vapdev
->vdev
.ram_block_discard_allowed
= true;
115 ret
= vfio_get_device(vfio_group
, mdevid
, &vapdev
->vdev
, errp
);
117 goto out_get_dev_err
;
123 vfio_ap_put_device(vapdev
);
124 vfio_put_group(vfio_group
);
127 static void vfio_ap_unrealize(DeviceState
*dev
)
129 APDevice
*apdev
= AP_DEVICE(dev
);
130 VFIOAPDevice
*vapdev
= VFIO_AP_DEVICE(apdev
);
131 VFIOGroup
*group
= vapdev
->vdev
.group
;
133 vfio_ap_put_device(vapdev
);
134 vfio_put_group(group
);
137 static Property vfio_ap_properties
[] = {
138 DEFINE_PROP_STRING("sysfsdev", VFIOAPDevice
, vdev
.sysfsdev
),
139 DEFINE_PROP_END_OF_LIST(),
142 static void vfio_ap_reset(DeviceState
*dev
)
145 APDevice
*apdev
= AP_DEVICE(dev
);
146 VFIOAPDevice
*vapdev
= VFIO_AP_DEVICE(apdev
);
148 ret
= ioctl(vapdev
->vdev
.fd
, VFIO_DEVICE_RESET
);
150 error_report("%s: failed to reset %s device: %s", __func__
,
151 vapdev
->vdev
.name
, strerror(errno
));
155 static const VMStateDescription vfio_ap_vmstate
= {
160 static void vfio_ap_class_init(ObjectClass
*klass
, void *data
)
162 DeviceClass
*dc
= DEVICE_CLASS(klass
);
164 device_class_set_props(dc
, vfio_ap_properties
);
165 dc
->vmsd
= &vfio_ap_vmstate
;
166 dc
->desc
= "VFIO-based AP device assignment";
167 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
168 dc
->realize
= vfio_ap_realize
;
169 dc
->unrealize
= vfio_ap_unrealize
;
170 dc
->hotpluggable
= true;
171 dc
->reset
= vfio_ap_reset
;
172 dc
->bus_type
= TYPE_AP_BUS
;
175 static const TypeInfo vfio_ap_info
= {
176 .name
= TYPE_VFIO_AP_DEVICE
,
177 .parent
= TYPE_AP_DEVICE
,
178 .instance_size
= sizeof(VFIOAPDevice
),
179 .class_init
= vfio_ap_class_init
,
182 static void vfio_ap_type_init(void)
184 type_register_static(&vfio_ap_info
);
187 type_init(vfio_ap_type_init
)