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/event_notifier.h"
22 #include "qemu/main-loop.h"
23 #include "qemu/module.h"
24 #include "qemu/option.h"
25 #include "qemu/config-file.h"
26 #include "kvm/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"
38 EventNotifier req_notifier
;
41 OBJECT_DECLARE_SIMPLE_TYPE(VFIOAPDevice
, VFIO_AP_DEVICE
)
43 static void vfio_ap_compute_needs_reset(VFIODevice
*vdev
)
45 vdev
->needs_reset
= false;
49 * We don't need vfio_hot_reset_multi and vfio_eoi operations for
52 struct VFIODeviceOps vfio_ap_ops
= {
53 .vfio_compute_needs_reset
= vfio_ap_compute_needs_reset
,
56 static void vfio_ap_put_device(VFIOAPDevice
*vapdev
)
58 g_free(vapdev
->vdev
.name
);
59 vfio_put_base_device(&vapdev
->vdev
);
62 static VFIOGroup
*vfio_ap_get_group(VFIOAPDevice
*vapdev
, Error
**errp
)
64 GError
*gerror
= NULL
;
65 char *symlink
, *group_path
;
68 symlink
= g_strdup_printf("%s/iommu_group", vapdev
->vdev
.sysfsdev
);
69 group_path
= g_file_read_link(symlink
, &gerror
);
73 error_setg(errp
, "%s: no iommu_group found for %s: %s",
74 TYPE_VFIO_AP_DEVICE
, vapdev
->vdev
.sysfsdev
, gerror
->message
);
79 if (sscanf(basename(group_path
), "%d", &groupid
) != 1) {
80 error_setg(errp
, "vfio: failed to read %s", group_path
);
87 return vfio_get_group(groupid
, &address_space_memory
, errp
);
90 static void vfio_ap_req_notifier_handler(void *opaque
)
92 VFIOAPDevice
*vapdev
= opaque
;
95 if (!event_notifier_test_and_clear(&vapdev
->req_notifier
)) {
99 qdev_unplug(DEVICE(vapdev
), &err
);
102 warn_reportf_err(err
, VFIO_MSG_PREFIX
, vapdev
->vdev
.name
);
106 static void vfio_ap_register_irq_notifier(VFIOAPDevice
*vapdev
,
107 unsigned int irq
, Error
**errp
)
112 EventNotifier
*notifier
;
113 struct vfio_irq_info
*irq_info
;
114 VFIODevice
*vdev
= &vapdev
->vdev
;
117 case VFIO_AP_REQ_IRQ_INDEX
:
118 notifier
= &vapdev
->req_notifier
;
119 fd_read
= vfio_ap_req_notifier_handler
;
122 error_setg(errp
, "vfio: Unsupported device irq(%d)", irq
);
126 if (vdev
->num_irqs
< irq
+ 1) {
127 error_setg(errp
, "vfio: IRQ %u not available (number of irqs %u)",
128 irq
, vdev
->num_irqs
);
132 argsz
= sizeof(*irq_info
);
133 irq_info
= g_malloc0(argsz
);
134 irq_info
->index
= irq
;
135 irq_info
->argsz
= argsz
;
137 if (ioctl(vdev
->fd
, VFIO_DEVICE_GET_IRQ_INFO
,
138 irq_info
) < 0 || irq_info
->count
< 1) {
139 error_setg_errno(errp
, errno
, "vfio: Error getting irq info");
143 if (event_notifier_init(notifier
, 0)) {
144 error_setg_errno(errp
, errno
,
145 "vfio: Unable to init event notifier for irq (%d)",
150 fd
= event_notifier_get_fd(notifier
);
151 qemu_set_fd_handler(fd
, fd_read
, NULL
, vapdev
);
153 if (vfio_set_irq_signaling(vdev
, irq
, 0, VFIO_IRQ_SET_ACTION_TRIGGER
, fd
,
155 qemu_set_fd_handler(fd
, NULL
, NULL
, vapdev
);
156 event_notifier_cleanup(notifier
);
164 static void vfio_ap_unregister_irq_notifier(VFIOAPDevice
*vapdev
,
168 EventNotifier
*notifier
;
171 case VFIO_AP_REQ_IRQ_INDEX
:
172 notifier
= &vapdev
->req_notifier
;
175 error_report("vfio: Unsupported device irq(%d)", irq
);
179 if (vfio_set_irq_signaling(&vapdev
->vdev
, irq
, 0,
180 VFIO_IRQ_SET_ACTION_TRIGGER
, -1, &err
)) {
181 warn_reportf_err(err
, VFIO_MSG_PREFIX
, vapdev
->vdev
.name
);
184 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
186 event_notifier_cleanup(notifier
);
189 static void vfio_ap_realize(DeviceState
*dev
, Error
**errp
)
194 VFIOGroup
*vfio_group
;
195 APDevice
*apdev
= AP_DEVICE(dev
);
196 VFIOAPDevice
*vapdev
= VFIO_AP_DEVICE(apdev
);
198 vfio_group
= vfio_ap_get_group(vapdev
, errp
);
203 vapdev
->vdev
.ops
= &vfio_ap_ops
;
204 vapdev
->vdev
.type
= VFIO_DEVICE_TYPE_AP
;
205 mdevid
= basename(vapdev
->vdev
.sysfsdev
);
206 vapdev
->vdev
.name
= g_strdup_printf("%s", mdevid
);
207 vapdev
->vdev
.dev
= dev
;
210 * vfio-ap devices operate in a way compatible with discarding of
211 * memory in RAM blocks, as no pages are pinned in the host.
212 * This needs to be set before vfio_get_device() for vfio common to
213 * handle ram_block_discard_disable().
215 vapdev
->vdev
.ram_block_discard_allowed
= true;
217 ret
= vfio_get_device(vfio_group
, mdevid
, &vapdev
->vdev
, errp
);
219 goto out_get_dev_err
;
222 vfio_ap_register_irq_notifier(vapdev
, VFIO_AP_REQ_IRQ_INDEX
, &err
);
225 * Report this error, but do not make it a failing condition.
226 * Lack of this IRQ in the host does not prevent normal operation.
228 error_report_err(err
);
234 vfio_ap_put_device(vapdev
);
235 vfio_put_group(vfio_group
);
238 static void vfio_ap_unrealize(DeviceState
*dev
)
240 APDevice
*apdev
= AP_DEVICE(dev
);
241 VFIOAPDevice
*vapdev
= VFIO_AP_DEVICE(apdev
);
242 VFIOGroup
*group
= vapdev
->vdev
.group
;
244 vfio_ap_unregister_irq_notifier(vapdev
, VFIO_AP_REQ_IRQ_INDEX
);
245 vfio_ap_put_device(vapdev
);
246 vfio_put_group(group
);
249 static Property vfio_ap_properties
[] = {
250 DEFINE_PROP_STRING("sysfsdev", VFIOAPDevice
, vdev
.sysfsdev
),
251 DEFINE_PROP_END_OF_LIST(),
254 static void vfio_ap_reset(DeviceState
*dev
)
257 APDevice
*apdev
= AP_DEVICE(dev
);
258 VFIOAPDevice
*vapdev
= VFIO_AP_DEVICE(apdev
);
260 ret
= ioctl(vapdev
->vdev
.fd
, VFIO_DEVICE_RESET
);
262 error_report("%s: failed to reset %s device: %s", __func__
,
263 vapdev
->vdev
.name
, strerror(errno
));
267 static const VMStateDescription vfio_ap_vmstate
= {
272 static void vfio_ap_class_init(ObjectClass
*klass
, void *data
)
274 DeviceClass
*dc
= DEVICE_CLASS(klass
);
276 device_class_set_props(dc
, vfio_ap_properties
);
277 dc
->vmsd
= &vfio_ap_vmstate
;
278 dc
->desc
= "VFIO-based AP device assignment";
279 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
280 dc
->realize
= vfio_ap_realize
;
281 dc
->unrealize
= vfio_ap_unrealize
;
282 dc
->hotpluggable
= true;
283 dc
->reset
= vfio_ap_reset
;
284 dc
->bus_type
= TYPE_AP_BUS
;
287 static const TypeInfo vfio_ap_info
= {
288 .name
= TYPE_VFIO_AP_DEVICE
,
289 .parent
= TYPE_AP_DEVICE
,
290 .instance_size
= sizeof(VFIOAPDevice
),
291 .class_init
= vfio_ap_class_init
,
294 static void vfio_ap_type_init(void)
296 type_register_static(&vfio_ap_info
);
299 type_init(vfio_ap_type_init
)