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 CONFIG_DEVICES /* CONFIG_IOMMUFD */
15 #include <linux/vfio.h>
16 #include <sys/ioctl.h>
17 #include "qapi/error.h"
18 #include "hw/vfio/vfio-common.h"
19 #include "sysemu/iommufd.h"
20 #include "hw/s390x/ap-device.h"
21 #include "qemu/error-report.h"
22 #include "qemu/event_notifier.h"
23 #include "qemu/main-loop.h"
24 #include "qemu/module.h"
25 #include "qemu/option.h"
26 #include "qemu/config-file.h"
27 #include "kvm/kvm_s390x.h"
28 #include "migration/vmstate.h"
29 #include "hw/qdev-properties.h"
30 #include "hw/s390x/ap-bridge.h"
31 #include "exec/address-spaces.h"
32 #include "qom/object.h"
34 #define TYPE_VFIO_AP_DEVICE "vfio-ap"
39 EventNotifier req_notifier
;
42 OBJECT_DECLARE_SIMPLE_TYPE(VFIOAPDevice
, 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
53 struct VFIODeviceOps vfio_ap_ops
= {
54 .vfio_compute_needs_reset
= vfio_ap_compute_needs_reset
,
57 static void vfio_ap_req_notifier_handler(void *opaque
)
59 VFIOAPDevice
*vapdev
= opaque
;
62 if (!event_notifier_test_and_clear(&vapdev
->req_notifier
)) {
66 qdev_unplug(DEVICE(vapdev
), &err
);
69 warn_reportf_err(err
, VFIO_MSG_PREFIX
, vapdev
->vdev
.name
);
73 static void vfio_ap_register_irq_notifier(VFIOAPDevice
*vapdev
,
74 unsigned int irq
, Error
**errp
)
79 EventNotifier
*notifier
;
80 struct vfio_irq_info
*irq_info
;
81 VFIODevice
*vdev
= &vapdev
->vdev
;
84 case VFIO_AP_REQ_IRQ_INDEX
:
85 notifier
= &vapdev
->req_notifier
;
86 fd_read
= vfio_ap_req_notifier_handler
;
89 error_setg(errp
, "vfio: Unsupported device irq(%d)", irq
);
93 if (vdev
->num_irqs
< irq
+ 1) {
94 error_setg(errp
, "vfio: IRQ %u not available (number of irqs %u)",
99 argsz
= sizeof(*irq_info
);
100 irq_info
= g_malloc0(argsz
);
101 irq_info
->index
= irq
;
102 irq_info
->argsz
= argsz
;
104 if (ioctl(vdev
->fd
, VFIO_DEVICE_GET_IRQ_INFO
,
105 irq_info
) < 0 || irq_info
->count
< 1) {
106 error_setg_errno(errp
, errno
, "vfio: Error getting irq info");
110 if (event_notifier_init(notifier
, 0)) {
111 error_setg_errno(errp
, errno
,
112 "vfio: Unable to init event notifier for irq (%d)",
117 fd
= event_notifier_get_fd(notifier
);
118 qemu_set_fd_handler(fd
, fd_read
, NULL
, vapdev
);
120 if (vfio_set_irq_signaling(vdev
, irq
, 0, VFIO_IRQ_SET_ACTION_TRIGGER
, fd
,
122 qemu_set_fd_handler(fd
, NULL
, NULL
, vapdev
);
123 event_notifier_cleanup(notifier
);
131 static void vfio_ap_unregister_irq_notifier(VFIOAPDevice
*vapdev
,
135 EventNotifier
*notifier
;
138 case VFIO_AP_REQ_IRQ_INDEX
:
139 notifier
= &vapdev
->req_notifier
;
142 error_report("vfio: Unsupported device irq(%d)", irq
);
146 if (vfio_set_irq_signaling(&vapdev
->vdev
, irq
, 0,
147 VFIO_IRQ_SET_ACTION_TRIGGER
, -1, &err
)) {
148 warn_reportf_err(err
, VFIO_MSG_PREFIX
, vapdev
->vdev
.name
);
151 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
153 event_notifier_cleanup(notifier
);
156 static void vfio_ap_realize(DeviceState
*dev
, Error
**errp
)
161 VFIOAPDevice
*vapdev
= VFIO_AP_DEVICE(dev
);
162 VFIODevice
*vbasedev
= &vapdev
->vdev
;
164 if (vfio_device_get_name(vbasedev
, errp
) < 0) {
168 ret
= vfio_attach_device(vbasedev
->name
, vbasedev
,
169 &address_space_memory
, errp
);
174 vfio_ap_register_irq_notifier(vapdev
, VFIO_AP_REQ_IRQ_INDEX
, &err
);
177 * Report this error, but do not make it a failing condition.
178 * Lack of this IRQ in the host does not prevent normal operation.
180 error_report_err(err
);
186 error_prepend(errp
, VFIO_MSG_PREFIX
, vbasedev
->name
);
187 g_free(vbasedev
->name
);
190 static void vfio_ap_unrealize(DeviceState
*dev
)
192 VFIOAPDevice
*vapdev
= VFIO_AP_DEVICE(dev
);
194 vfio_ap_unregister_irq_notifier(vapdev
, VFIO_AP_REQ_IRQ_INDEX
);
195 vfio_detach_device(&vapdev
->vdev
);
196 g_free(vapdev
->vdev
.name
);
199 static Property vfio_ap_properties
[] = {
200 DEFINE_PROP_STRING("sysfsdev", VFIOAPDevice
, vdev
.sysfsdev
),
201 #ifdef CONFIG_IOMMUFD
202 DEFINE_PROP_LINK("iommufd", VFIOAPDevice
, vdev
.iommufd
,
203 TYPE_IOMMUFD_BACKEND
, IOMMUFDBackend
*),
205 DEFINE_PROP_END_OF_LIST(),
208 static void vfio_ap_reset(DeviceState
*dev
)
211 VFIOAPDevice
*vapdev
= VFIO_AP_DEVICE(dev
);
213 ret
= ioctl(vapdev
->vdev
.fd
, VFIO_DEVICE_RESET
);
215 error_report("%s: failed to reset %s device: %s", __func__
,
216 vapdev
->vdev
.name
, strerror(errno
));
220 static const VMStateDescription vfio_ap_vmstate
= {
225 static void vfio_ap_instance_init(Object
*obj
)
227 VFIOAPDevice
*vapdev
= VFIO_AP_DEVICE(obj
);
228 VFIODevice
*vbasedev
= &vapdev
->vdev
;
231 * vfio-ap devices operate in a way compatible with discarding of
232 * memory in RAM blocks, as no pages are pinned in the host.
233 * This needs to be set before vfio_get_device() for vfio common to
234 * handle ram_block_discard_disable().
236 vfio_device_init(vbasedev
, VFIO_DEVICE_TYPE_AP
, &vfio_ap_ops
,
237 DEVICE(vapdev
), true);
240 #ifdef CONFIG_IOMMUFD
241 static void vfio_ap_set_fd(Object
*obj
, const char *str
, Error
**errp
)
243 vfio_device_set_fd(&VFIO_AP_DEVICE(obj
)->vdev
, str
, errp
);
247 static void vfio_ap_class_init(ObjectClass
*klass
, void *data
)
249 DeviceClass
*dc
= DEVICE_CLASS(klass
);
251 device_class_set_props(dc
, vfio_ap_properties
);
252 #ifdef CONFIG_IOMMUFD
253 object_class_property_add_str(klass
, "fd", NULL
, vfio_ap_set_fd
);
255 dc
->vmsd
= &vfio_ap_vmstate
;
256 dc
->desc
= "VFIO-based AP device assignment";
257 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
258 dc
->realize
= vfio_ap_realize
;
259 dc
->unrealize
= vfio_ap_unrealize
;
260 dc
->hotpluggable
= true;
261 dc
->reset
= vfio_ap_reset
;
262 dc
->bus_type
= TYPE_AP_BUS
;
265 static const TypeInfo vfio_ap_info
= {
266 .name
= TYPE_VFIO_AP_DEVICE
,
267 .parent
= TYPE_AP_DEVICE
,
268 .instance_size
= sizeof(VFIOAPDevice
),
269 .instance_init
= vfio_ap_instance_init
,
270 .class_init
= vfio_ap_class_init
,
273 static void vfio_ap_type_init(void)
275 type_register_static(&vfio_ap_info
);
278 type_init(vfio_ap_type_init
)