2 * QEMU S390x KVM floating interrupt controller (flic)
4 * Copyright 2014 IBM Corp.
5 * Author(s): Jens Freimann <jfrei@linux.vnet.ibm.com>
6 * Cornelia Huck <cornelia.huck@de.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 "qemu-common.h"
16 #include <sys/ioctl.h>
17 #include "qemu/error-report.h"
18 #include "qapi/error.h"
19 #include "hw/sysbus.h"
20 #include "sysemu/kvm.h"
21 #include "hw/s390x/s390_flic.h"
22 #include "hw/s390x/adapter.h"
23 #include "hw/s390x/css.h"
26 #define FLIC_SAVE_INITIAL_SIZE getpagesize()
27 #define FLIC_FAILED (-1UL)
28 #define FLIC_SAVEVM_VERSION 1
30 typedef struct KVMS390FLICState
{
31 S390FLICState parent_obj
;
34 bool clear_io_supported
;
37 DeviceState
*s390_flic_kvm_create(void)
39 DeviceState
*dev
= NULL
;
42 dev
= qdev_create(NULL
, TYPE_KVM_S390_FLIC
);
43 object_property_add_child(qdev_get_machine(), TYPE_KVM_S390_FLIC
,
50 * flic_get_all_irqs - store all pending irqs in buffer
51 * @buf: pointer to buffer which is passed to kernel
52 * @len: length of buffer
53 * @flic: pointer to flic device state
55 * Returns: -ENOMEM if buffer is too small,
56 * -EINVAL if attr.group is invalid,
57 * -EFAULT if copying to userspace failed,
58 * on success return number of stored interrupts
60 static int flic_get_all_irqs(KVMS390FLICState
*flic
,
63 struct kvm_device_attr attr
= {
64 .group
= KVM_DEV_FLIC_GET_ALL_IRQS
,
65 .addr
= (uint64_t) buf
,
70 rc
= ioctl(flic
->fd
, KVM_GET_DEVICE_ATTR
, &attr
);
72 return rc
== -1 ? -errno
: rc
;
75 static void flic_enable_pfault(KVMS390FLICState
*flic
)
77 struct kvm_device_attr attr
= {
78 .group
= KVM_DEV_FLIC_APF_ENABLE
,
82 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
85 fprintf(stderr
, "flic: couldn't enable pfault\n");
89 static void flic_disable_wait_pfault(KVMS390FLICState
*flic
)
91 struct kvm_device_attr attr
= {
92 .group
= KVM_DEV_FLIC_APF_DISABLE_WAIT
,
96 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
99 fprintf(stderr
, "flic: couldn't disable pfault\n");
103 /** flic_enqueue_irqs - returns 0 on success
104 * @buf: pointer to buffer which is passed to kernel
105 * @len: length of buffer
106 * @flic: pointer to flic device state
108 * Returns: -EINVAL if attr.group is unknown
110 static int flic_enqueue_irqs(void *buf
, uint64_t len
,
111 KVMS390FLICState
*flic
)
114 struct kvm_device_attr attr
= {
115 .group
= KVM_DEV_FLIC_ENQUEUE
,
116 .addr
= (uint64_t) buf
,
120 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
122 return rc
? -errno
: 0;
125 int kvm_s390_inject_flic(struct kvm_s390_irq
*irq
)
127 static KVMS390FLICState
*flic
;
129 if (unlikely(!flic
)) {
130 flic
= KVM_S390_FLIC(s390_get_flic());
132 return flic_enqueue_irqs(irq
, sizeof(*irq
), flic
);
135 static int kvm_s390_clear_io_flic(S390FLICState
*fs
, uint16_t subchannel_id
,
136 uint16_t subchannel_nr
)
138 KVMS390FLICState
*flic
= KVM_S390_FLIC(fs
);
140 uint32_t sid
= subchannel_id
<< 16 | subchannel_nr
;
141 struct kvm_device_attr attr
= {
142 .group
= KVM_DEV_FLIC_CLEAR_IO_IRQ
,
143 .addr
= (uint64_t) &sid
,
146 if (unlikely(!flic
->clear_io_supported
)) {
149 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
150 return rc
? -errno
: 0;
153 static int kvm_s390_modify_ais_mode(S390FLICState
*fs
, uint8_t isc
,
156 KVMS390FLICState
*flic
= KVM_S390_FLIC(fs
);
157 struct kvm_s390_ais_req req
= {
161 struct kvm_device_attr attr
= {
162 .group
= KVM_DEV_FLIC_AISM
,
163 .addr
= (uint64_t)&req
,
166 if (!fs
->ais_supported
) {
170 return ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
) ? -errno
: 0;
173 static int kvm_s390_inject_airq(S390FLICState
*fs
, uint8_t type
,
174 uint8_t isc
, uint8_t flags
)
176 KVMS390FLICState
*flic
= KVM_S390_FLIC(fs
);
177 uint32_t id
= css_get_adapter_id(type
, isc
);
178 struct kvm_device_attr attr
= {
179 .group
= KVM_DEV_FLIC_AIRQ_INJECT
,
183 if (!fs
->ais_supported
) {
187 return ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
) ? -errno
: 0;
191 * __get_all_irqs - store all pending irqs in buffer
192 * @flic: pointer to flic device state
193 * @buf: pointer to pointer to a buffer
194 * @len: length of buffer
196 * Returns: return value of flic_get_all_irqs
197 * Note: Retry and increase buffer size until flic_get_all_irqs
198 * either returns a value >= 0 or a negative error code.
199 * -ENOMEM is an exception, which means the buffer is too small
200 * and we should try again. Other negative error codes can be
201 * -EFAULT and -EINVAL which we ignore at this point
203 static int __get_all_irqs(KVMS390FLICState
*flic
,
209 /* returns -ENOMEM if buffer is too small and number
210 * of queued interrupts on success */
211 r
= flic_get_all_irqs(flic
, *buf
, len
);
216 *buf
= g_try_realloc(*buf
, len
);
220 } while (r
== -ENOMEM
&& len
<= KVM_S390_FLIC_MAX_BUFFER
);
225 static int kvm_s390_register_io_adapter(S390FLICState
*fs
, uint32_t id
,
226 uint8_t isc
, bool swap
,
227 bool is_maskable
, uint8_t flags
)
229 struct kvm_s390_io_adapter adapter
= {
232 .maskable
= is_maskable
,
236 KVMS390FLICState
*flic
= KVM_S390_FLIC(fs
);
238 struct kvm_device_attr attr
= {
239 .group
= KVM_DEV_FLIC_ADAPTER_REGISTER
,
240 .addr
= (uint64_t)&adapter
,
243 if (!kvm_gsi_routing_enabled()) {
248 r
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
250 return r
? -errno
: 0;
253 static int kvm_s390_io_adapter_map(S390FLICState
*fs
, uint32_t id
,
254 uint64_t map_addr
, bool do_map
)
256 struct kvm_s390_io_adapter_req req
= {
258 .type
= do_map
? KVM_S390_IO_ADAPTER_MAP
: KVM_S390_IO_ADAPTER_UNMAP
,
261 struct kvm_device_attr attr
= {
262 .group
= KVM_DEV_FLIC_ADAPTER_MODIFY
,
263 .addr
= (uint64_t)&req
,
265 KVMS390FLICState
*flic
= KVM_S390_FLIC(fs
);
268 if (!kvm_gsi_routing_enabled()) {
273 r
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
274 return r
? -errno
: 0;
277 static int kvm_s390_add_adapter_routes(S390FLICState
*fs
,
278 AdapterRoutes
*routes
)
281 uint64_t ind_offset
= routes
->adapter
.ind_offset
;
283 for (i
= 0; i
< routes
->num_routes
; i
++) {
284 ret
= kvm_irqchip_add_adapter_route(kvm_state
, &routes
->adapter
);
288 routes
->gsi
[i
] = ret
;
289 routes
->adapter
.ind_offset
++;
291 kvm_irqchip_commit_routes(kvm_state
);
293 /* Restore passed-in structure to original state. */
294 routes
->adapter
.ind_offset
= ind_offset
;
298 kvm_irqchip_release_virq(kvm_state
, routes
->gsi
[i
]);
301 routes
->adapter
.ind_offset
= ind_offset
;
305 static void kvm_s390_release_adapter_routes(S390FLICState
*fs
,
306 AdapterRoutes
*routes
)
310 for (i
= 0; i
< routes
->num_routes
; i
++) {
311 if (routes
->gsi
[i
] >= 0) {
312 kvm_irqchip_release_virq(kvm_state
, routes
->gsi
[i
]);
319 * kvm_flic_save - Save pending floating interrupts
320 * @f: QEMUFile containing migration state
321 * @opaque: pointer to flic device state
324 * Note: Pass buf and len to kernel. Start with one page and
325 * increase until buffer is sufficient or maxium size is
328 static int kvm_flic_save(QEMUFile
*f
, void *opaque
, size_t size
,
329 VMStateField
*field
, QJSON
*vmdesc
)
331 KVMS390FLICState
*flic
= opaque
;
332 int len
= FLIC_SAVE_INITIAL_SIZE
;
337 flic_disable_wait_pfault((struct KVMS390FLICState
*) opaque
);
339 buf
= g_try_malloc0(len
);
341 /* Storing FLIC_FAILED into the count field here will cause the
342 * target system to fail when attempting to load irqs from the
344 error_report("flic: couldn't allocate memory");
345 qemu_put_be64(f
, FLIC_FAILED
);
349 count
= __get_all_irqs(flic
, &buf
, len
);
351 error_report("flic: couldn't retrieve irqs from kernel, rc %d",
353 /* Storing FLIC_FAILED into the count field here will cause the
354 * target system to fail when attempting to load irqs from the
356 qemu_put_be64(f
, FLIC_FAILED
);
359 qemu_put_be64(f
, count
);
360 qemu_put_buffer(f
, (uint8_t *) buf
,
361 count
* sizeof(struct kvm_s390_irq
));
369 * kvm_flic_load - Load pending floating interrupts
370 * @f: QEMUFile containing migration state
371 * @opaque: pointer to flic device state
374 * Returns: value of flic_enqueue_irqs, -EINVAL on error
375 * Note: Do nothing when no interrupts where stored
378 static int kvm_flic_load(QEMUFile
*f
, void *opaque
, size_t size
,
386 flic_enable_pfault((struct KVMS390FLICState
*) opaque
);
388 count
= qemu_get_be64(f
);
389 len
= count
* sizeof(struct kvm_s390_irq
);
390 if (count
== FLIC_FAILED
) {
398 buf
= g_try_malloc0(len
);
404 if (qemu_get_buffer(f
, (uint8_t *) buf
, len
) != len
) {
408 r
= flic_enqueue_irqs(buf
, len
, (struct KVMS390FLICState
*) opaque
);
416 static const VMStateDescription kvm_s390_flic_vmstate
= {
418 .version_id
= FLIC_SAVEVM_VERSION
,
419 .minimum_version_id
= FLIC_SAVEVM_VERSION
,
420 .fields
= (VMStateField
[]) {
423 .info
= &(const VMStateInfo
) {
425 .get
= kvm_flic_load
,
426 .put
= kvm_flic_save
,
430 VMSTATE_END_OF_LIST()
434 typedef struct KVMS390FLICStateClass
{
435 S390FLICStateClass parent_class
;
436 DeviceRealize parent_realize
;
437 } KVMS390FLICStateClass
;
439 #define KVM_S390_FLIC_GET_CLASS(obj) \
440 OBJECT_GET_CLASS(KVMS390FLICStateClass, (obj), TYPE_KVM_S390_FLIC)
442 #define KVM_S390_FLIC_CLASS(klass) \
443 OBJECT_CLASS_CHECK(KVMS390FLICStateClass, (klass), TYPE_KVM_S390_FLIC)
445 static void kvm_s390_flic_realize(DeviceState
*dev
, Error
**errp
)
447 S390FLICState
*fs
= S390_FLIC_COMMON(dev
);
448 KVMS390FLICState
*flic_state
= KVM_S390_FLIC(dev
);
449 struct kvm_create_device cd
= {0};
450 struct kvm_device_attr test_attr
= {0};
452 Error
*errp_local
= NULL
;
454 KVM_S390_FLIC_GET_CLASS(dev
)->parent_realize(dev
, &errp_local
);
459 if (!kvm_check_extension(kvm_state
, KVM_CAP_DEVICE_CTRL
)) {
460 error_setg_errno(&errp_local
, errno
, "KVM is missing capability"
461 " KVM_CAP_DEVICE_CTRL");
462 trace_flic_no_device_api(errno
);
466 cd
.type
= KVM_DEV_TYPE_FLIC
;
467 ret
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_DEVICE
, &cd
);
469 error_setg_errno(&errp_local
, errno
, "Creating the KVM device failed");
470 trace_flic_create_device(errno
);
473 flic_state
->fd
= cd
.fd
;
475 /* Check clear_io_irq support */
476 test_attr
.group
= KVM_DEV_FLIC_CLEAR_IO_IRQ
;
477 flic_state
->clear_io_supported
= !ioctl(flic_state
->fd
,
478 KVM_HAS_DEVICE_ATTR
, test_attr
);
480 fs
->ais_supported
= false;
483 error_propagate(errp
, errp_local
);
486 static void kvm_s390_flic_reset(DeviceState
*dev
)
488 KVMS390FLICState
*flic
= KVM_S390_FLIC(dev
);
489 S390FLICState
*fs
= S390_FLIC_COMMON(dev
);
490 struct kvm_device_attr attr
= {
491 .group
= KVM_DEV_FLIC_CLEAR_IRQS
,
496 if (flic
->fd
== -1) {
500 flic_disable_wait_pfault(flic
);
502 if (fs
->ais_supported
) {
503 for (isc
= 0; isc
<= MAX_ISC
; isc
++) {
504 rc
= kvm_s390_modify_ais_mode(fs
, isc
, SIC_IRQ_MODE_ALL
);
506 error_report("Failed to reset ais mode for isc %d: %s",
512 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
514 trace_flic_reset_failed(errno
);
517 flic_enable_pfault(flic
);
520 static void kvm_s390_flic_class_init(ObjectClass
*oc
, void *data
)
522 DeviceClass
*dc
= DEVICE_CLASS(oc
);
523 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_CLASS(oc
);
525 KVM_S390_FLIC_CLASS(oc
)->parent_realize
= dc
->realize
;
526 dc
->realize
= kvm_s390_flic_realize
;
527 dc
->vmsd
= &kvm_s390_flic_vmstate
;
528 dc
->reset
= kvm_s390_flic_reset
;
529 fsc
->register_io_adapter
= kvm_s390_register_io_adapter
;
530 fsc
->io_adapter_map
= kvm_s390_io_adapter_map
;
531 fsc
->add_adapter_routes
= kvm_s390_add_adapter_routes
;
532 fsc
->release_adapter_routes
= kvm_s390_release_adapter_routes
;
533 fsc
->clear_io_irq
= kvm_s390_clear_io_flic
;
534 fsc
->modify_ais_mode
= kvm_s390_modify_ais_mode
;
535 fsc
->inject_airq
= kvm_s390_inject_airq
;
538 static const TypeInfo kvm_s390_flic_info
= {
539 .name
= TYPE_KVM_S390_FLIC
,
540 .parent
= TYPE_S390_FLIC_COMMON
,
541 .instance_size
= sizeof(KVMS390FLICState
),
542 .class_size
= sizeof(KVMS390FLICStateClass
),
543 .class_init
= kvm_s390_flic_class_init
,
546 static void kvm_s390_flic_register_types(void)
548 type_register_static(&kvm_s390_flic_info
);
551 type_init(kvm_s390_flic_register_types
)