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 "kvm_s390x.h"
17 #include <sys/ioctl.h>
18 #include "qemu/error-report.h"
19 #include "qapi/error.h"
20 #include "hw/sysbus.h"
21 #include "sysemu/kvm.h"
22 #include "hw/s390x/s390_flic.h"
23 #include "hw/s390x/adapter.h"
24 #include "hw/s390x/css.h"
27 #define FLIC_SAVE_INITIAL_SIZE getpagesize()
28 #define FLIC_FAILED (-1UL)
29 #define FLIC_SAVEVM_VERSION 1
31 typedef struct KVMS390FLICState
{
32 S390FLICState parent_obj
;
35 bool clear_io_supported
;
38 DeviceState
*s390_flic_kvm_create(void)
40 DeviceState
*dev
= NULL
;
43 dev
= qdev_create(NULL
, TYPE_KVM_S390_FLIC
);
44 object_property_add_child(qdev_get_machine(), TYPE_KVM_S390_FLIC
,
51 * flic_get_all_irqs - store all pending irqs in buffer
52 * @buf: pointer to buffer which is passed to kernel
53 * @len: length of buffer
54 * @flic: pointer to flic device state
56 * Returns: -ENOMEM if buffer is too small,
57 * -EINVAL if attr.group is invalid,
58 * -EFAULT if copying to userspace failed,
59 * on success return number of stored interrupts
61 static int flic_get_all_irqs(KVMS390FLICState
*flic
,
64 struct kvm_device_attr attr
= {
65 .group
= KVM_DEV_FLIC_GET_ALL_IRQS
,
66 .addr
= (uint64_t) buf
,
71 rc
= ioctl(flic
->fd
, KVM_GET_DEVICE_ATTR
, &attr
);
73 return rc
== -1 ? -errno
: rc
;
76 static void flic_enable_pfault(KVMS390FLICState
*flic
)
78 struct kvm_device_attr attr
= {
79 .group
= KVM_DEV_FLIC_APF_ENABLE
,
83 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
86 fprintf(stderr
, "flic: couldn't enable pfault\n");
90 static void flic_disable_wait_pfault(KVMS390FLICState
*flic
)
92 struct kvm_device_attr attr
= {
93 .group
= KVM_DEV_FLIC_APF_DISABLE_WAIT
,
97 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
100 fprintf(stderr
, "flic: couldn't disable pfault\n");
104 /** flic_enqueue_irqs - returns 0 on success
105 * @buf: pointer to buffer which is passed to kernel
106 * @len: length of buffer
107 * @flic: pointer to flic device state
109 * Returns: -EINVAL if attr.group is unknown
111 static int flic_enqueue_irqs(void *buf
, uint64_t len
,
112 KVMS390FLICState
*flic
)
115 struct kvm_device_attr attr
= {
116 .group
= KVM_DEV_FLIC_ENQUEUE
,
117 .addr
= (uint64_t) buf
,
121 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
123 return rc
? -errno
: 0;
126 int kvm_s390_inject_flic(struct kvm_s390_irq
*irq
)
128 static KVMS390FLICState
*flic
;
130 if (unlikely(!flic
)) {
131 flic
= KVM_S390_FLIC(s390_get_flic());
133 return flic_enqueue_irqs(irq
, sizeof(*irq
), flic
);
136 static int kvm_s390_clear_io_flic(S390FLICState
*fs
, uint16_t subchannel_id
,
137 uint16_t subchannel_nr
)
139 KVMS390FLICState
*flic
= KVM_S390_FLIC(fs
);
141 uint32_t sid
= subchannel_id
<< 16 | subchannel_nr
;
142 struct kvm_device_attr attr
= {
143 .group
= KVM_DEV_FLIC_CLEAR_IO_IRQ
,
144 .addr
= (uint64_t) &sid
,
147 if (unlikely(!flic
->clear_io_supported
)) {
150 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
151 return rc
? -errno
: 0;
154 static int kvm_s390_modify_ais_mode(S390FLICState
*fs
, uint8_t isc
,
157 KVMS390FLICState
*flic
= KVM_S390_FLIC(fs
);
158 struct kvm_s390_ais_req req
= {
162 struct kvm_device_attr attr
= {
163 .group
= KVM_DEV_FLIC_AISM
,
164 .addr
= (uint64_t)&req
,
167 if (!fs
->ais_supported
) {
171 return ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
) ? -errno
: 0;
174 static int kvm_s390_inject_airq(S390FLICState
*fs
, uint8_t type
,
175 uint8_t isc
, uint8_t flags
)
177 KVMS390FLICState
*flic
= KVM_S390_FLIC(fs
);
178 uint32_t id
= css_get_adapter_id(type
, isc
);
179 struct kvm_device_attr attr
= {
180 .group
= KVM_DEV_FLIC_AIRQ_INJECT
,
184 if (!fs
->ais_supported
) {
188 return ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
) ? -errno
: 0;
192 * __get_all_irqs - store all pending irqs in buffer
193 * @flic: pointer to flic device state
194 * @buf: pointer to pointer to a buffer
195 * @len: length of buffer
197 * Returns: return value of flic_get_all_irqs
198 * Note: Retry and increase buffer size until flic_get_all_irqs
199 * either returns a value >= 0 or a negative error code.
200 * -ENOMEM is an exception, which means the buffer is too small
201 * and we should try again. Other negative error codes can be
202 * -EFAULT and -EINVAL which we ignore at this point
204 static int __get_all_irqs(KVMS390FLICState
*flic
,
210 /* returns -ENOMEM if buffer is too small and number
211 * of queued interrupts on success */
212 r
= flic_get_all_irqs(flic
, *buf
, len
);
217 *buf
= g_try_realloc(*buf
, len
);
221 } while (r
== -ENOMEM
&& len
<= KVM_S390_FLIC_MAX_BUFFER
);
226 static int kvm_s390_register_io_adapter(S390FLICState
*fs
, uint32_t id
,
227 uint8_t isc
, bool swap
,
228 bool is_maskable
, uint8_t flags
)
230 struct kvm_s390_io_adapter adapter
= {
233 .maskable
= is_maskable
,
237 KVMS390FLICState
*flic
= KVM_S390_FLIC(fs
);
239 struct kvm_device_attr attr
= {
240 .group
= KVM_DEV_FLIC_ADAPTER_REGISTER
,
241 .addr
= (uint64_t)&adapter
,
244 if (!kvm_gsi_routing_enabled()) {
249 r
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
251 return r
? -errno
: 0;
254 static int kvm_s390_io_adapter_map(S390FLICState
*fs
, uint32_t id
,
255 uint64_t map_addr
, bool do_map
)
257 struct kvm_s390_io_adapter_req req
= {
259 .type
= do_map
? KVM_S390_IO_ADAPTER_MAP
: KVM_S390_IO_ADAPTER_UNMAP
,
262 struct kvm_device_attr attr
= {
263 .group
= KVM_DEV_FLIC_ADAPTER_MODIFY
,
264 .addr
= (uint64_t)&req
,
266 KVMS390FLICState
*flic
= KVM_S390_FLIC(fs
);
269 if (!kvm_gsi_routing_enabled()) {
274 r
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
275 return r
? -errno
: 0;
278 static int kvm_s390_add_adapter_routes(S390FLICState
*fs
,
279 AdapterRoutes
*routes
)
282 uint64_t ind_offset
= routes
->adapter
.ind_offset
;
284 for (i
= 0; i
< routes
->num_routes
; i
++) {
285 ret
= kvm_irqchip_add_adapter_route(kvm_state
, &routes
->adapter
);
289 routes
->gsi
[i
] = ret
;
290 routes
->adapter
.ind_offset
++;
292 kvm_irqchip_commit_routes(kvm_state
);
294 /* Restore passed-in structure to original state. */
295 routes
->adapter
.ind_offset
= ind_offset
;
299 kvm_irqchip_release_virq(kvm_state
, routes
->gsi
[i
]);
302 routes
->adapter
.ind_offset
= ind_offset
;
306 static void kvm_s390_release_adapter_routes(S390FLICState
*fs
,
307 AdapterRoutes
*routes
)
311 for (i
= 0; i
< routes
->num_routes
; i
++) {
312 if (routes
->gsi
[i
] >= 0) {
313 kvm_irqchip_release_virq(kvm_state
, routes
->gsi
[i
]);
320 * kvm_flic_save - Save pending floating interrupts
321 * @f: QEMUFile containing migration state
322 * @opaque: pointer to flic device state
325 * Note: Pass buf and len to kernel. Start with one page and
326 * increase until buffer is sufficient or maxium size is
329 static int kvm_flic_save(QEMUFile
*f
, void *opaque
, size_t size
,
330 VMStateField
*field
, QJSON
*vmdesc
)
332 KVMS390FLICState
*flic
= opaque
;
333 int len
= FLIC_SAVE_INITIAL_SIZE
;
338 flic_disable_wait_pfault((struct KVMS390FLICState
*) opaque
);
340 buf
= g_try_malloc0(len
);
342 /* Storing FLIC_FAILED into the count field here will cause the
343 * target system to fail when attempting to load irqs from the
345 error_report("flic: couldn't allocate memory");
346 qemu_put_be64(f
, FLIC_FAILED
);
350 count
= __get_all_irqs(flic
, &buf
, len
);
352 error_report("flic: couldn't retrieve irqs from kernel, rc %d",
354 /* Storing FLIC_FAILED into the count field here will cause the
355 * target system to fail when attempting to load irqs from the
357 qemu_put_be64(f
, FLIC_FAILED
);
360 qemu_put_be64(f
, count
);
361 qemu_put_buffer(f
, (uint8_t *) buf
,
362 count
* sizeof(struct kvm_s390_irq
));
370 * kvm_flic_load - Load pending floating interrupts
371 * @f: QEMUFile containing migration state
372 * @opaque: pointer to flic device state
375 * Returns: value of flic_enqueue_irqs, -EINVAL on error
376 * Note: Do nothing when no interrupts where stored
379 static int kvm_flic_load(QEMUFile
*f
, void *opaque
, size_t size
,
387 flic_enable_pfault((struct KVMS390FLICState
*) opaque
);
389 count
= qemu_get_be64(f
);
390 len
= count
* sizeof(struct kvm_s390_irq
);
391 if (count
== FLIC_FAILED
) {
399 buf
= g_try_malloc0(len
);
405 if (qemu_get_buffer(f
, (uint8_t *) buf
, len
) != len
) {
409 r
= flic_enqueue_irqs(buf
, len
, (struct KVMS390FLICState
*) opaque
);
417 typedef struct KVMS390FLICStateMigTmp
{
418 KVMS390FLICState
*parent
;
421 } KVMS390FLICStateMigTmp
;
423 static int kvm_flic_ais_pre_save(void *opaque
)
425 KVMS390FLICStateMigTmp
*tmp
= opaque
;
426 KVMS390FLICState
*flic
= tmp
->parent
;
427 struct kvm_s390_ais_all ais
;
428 struct kvm_device_attr attr
= {
429 .group
= KVM_DEV_FLIC_AISM_ALL
,
430 .addr
= (uint64_t)&ais
,
434 if (ioctl(flic
->fd
, KVM_GET_DEVICE_ATTR
, &attr
)) {
435 error_report("Failed to retrieve kvm flic ais states");
439 tmp
->simm
= ais
.simm
;
440 tmp
->nimm
= ais
.nimm
;
445 static int kvm_flic_ais_post_load(void *opaque
, int version_id
)
447 KVMS390FLICStateMigTmp
*tmp
= opaque
;
448 KVMS390FLICState
*flic
= tmp
->parent
;
449 struct kvm_s390_ais_all ais
= {
453 struct kvm_device_attr attr
= {
454 .group
= KVM_DEV_FLIC_AISM_ALL
,
455 .addr
= (uint64_t)&ais
,
458 /* This can happen when the user mis-configures its guests in an
459 * incompatible fashion or without a CPU model. For example using
460 * qemu with -cpu host (which is not migration safe) and do a
461 * migration from a host that has AIS to a host that has no AIS.
462 * In that case the target system will reject the migration here.
464 if (!ais_needed(flic
)) {
468 return ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
) ? -errno
: 0;
471 static const VMStateDescription kvm_s390_flic_ais_tmp
= {
472 .name
= "s390-flic-ais-tmp",
473 .pre_save
= kvm_flic_ais_pre_save
,
474 .post_load
= kvm_flic_ais_post_load
,
475 .fields
= (VMStateField
[]) {
476 VMSTATE_UINT8(simm
, KVMS390FLICStateMigTmp
),
477 VMSTATE_UINT8(nimm
, KVMS390FLICStateMigTmp
),
478 VMSTATE_END_OF_LIST()
482 static const VMStateDescription kvm_s390_flic_vmstate_ais
= {
483 .name
= "s390-flic/ais",
485 .minimum_version_id
= 1,
486 .needed
= ais_needed
,
487 .fields
= (VMStateField
[]) {
488 VMSTATE_WITH_TMP(KVMS390FLICState
, KVMS390FLICStateMigTmp
,
489 kvm_s390_flic_ais_tmp
),
490 VMSTATE_END_OF_LIST()
494 static const VMStateDescription kvm_s390_flic_vmstate
= {
495 /* should have been like kvm-s390-flic,
496 * can't change without breaking compat */
498 .version_id
= FLIC_SAVEVM_VERSION
,
499 .minimum_version_id
= FLIC_SAVEVM_VERSION
,
500 .fields
= (VMStateField
[]) {
503 .info
= &(const VMStateInfo
) {
505 .get
= kvm_flic_load
,
506 .put
= kvm_flic_save
,
510 VMSTATE_END_OF_LIST()
512 .subsections
= (const VMStateDescription
* []) {
513 &kvm_s390_flic_vmstate_ais
,
518 typedef struct KVMS390FLICStateClass
{
519 S390FLICStateClass parent_class
;
520 DeviceRealize parent_realize
;
521 } KVMS390FLICStateClass
;
523 #define KVM_S390_FLIC_GET_CLASS(obj) \
524 OBJECT_GET_CLASS(KVMS390FLICStateClass, (obj), TYPE_KVM_S390_FLIC)
526 #define KVM_S390_FLIC_CLASS(klass) \
527 OBJECT_CLASS_CHECK(KVMS390FLICStateClass, (klass), TYPE_KVM_S390_FLIC)
529 static void kvm_s390_flic_realize(DeviceState
*dev
, Error
**errp
)
531 KVMS390FLICState
*flic_state
= KVM_S390_FLIC(dev
);
532 struct kvm_create_device cd
= {0};
533 struct kvm_device_attr test_attr
= {0};
535 Error
*errp_local
= NULL
;
537 KVM_S390_FLIC_GET_CLASS(dev
)->parent_realize(dev
, &errp_local
);
542 if (!kvm_check_extension(kvm_state
, KVM_CAP_DEVICE_CTRL
)) {
543 error_setg_errno(&errp_local
, errno
, "KVM is missing capability"
544 " KVM_CAP_DEVICE_CTRL");
545 trace_flic_no_device_api(errno
);
549 cd
.type
= KVM_DEV_TYPE_FLIC
;
550 ret
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_DEVICE
, &cd
);
552 error_setg_errno(&errp_local
, errno
, "Creating the KVM device failed");
553 trace_flic_create_device(errno
);
556 flic_state
->fd
= cd
.fd
;
558 /* Check clear_io_irq support */
559 test_attr
.group
= KVM_DEV_FLIC_CLEAR_IO_IRQ
;
560 flic_state
->clear_io_supported
= !ioctl(flic_state
->fd
,
561 KVM_HAS_DEVICE_ATTR
, test_attr
);
564 error_propagate(errp
, errp_local
);
567 static void kvm_s390_flic_reset(DeviceState
*dev
)
569 KVMS390FLICState
*flic
= KVM_S390_FLIC(dev
);
570 S390FLICState
*fs
= S390_FLIC_COMMON(dev
);
571 struct kvm_device_attr attr
= {
572 .group
= KVM_DEV_FLIC_CLEAR_IRQS
,
577 if (flic
->fd
== -1) {
581 flic_disable_wait_pfault(flic
);
583 if (fs
->ais_supported
) {
584 for (isc
= 0; isc
<= MAX_ISC
; isc
++) {
585 rc
= kvm_s390_modify_ais_mode(fs
, isc
, SIC_IRQ_MODE_ALL
);
587 error_report("Failed to reset ais mode for isc %d: %s",
593 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
595 trace_flic_reset_failed(errno
);
598 flic_enable_pfault(flic
);
601 static void kvm_s390_flic_class_init(ObjectClass
*oc
, void *data
)
603 DeviceClass
*dc
= DEVICE_CLASS(oc
);
604 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_CLASS(oc
);
606 KVM_S390_FLIC_CLASS(oc
)->parent_realize
= dc
->realize
;
607 dc
->realize
= kvm_s390_flic_realize
;
608 dc
->vmsd
= &kvm_s390_flic_vmstate
;
609 dc
->reset
= kvm_s390_flic_reset
;
610 fsc
->register_io_adapter
= kvm_s390_register_io_adapter
;
611 fsc
->io_adapter_map
= kvm_s390_io_adapter_map
;
612 fsc
->add_adapter_routes
= kvm_s390_add_adapter_routes
;
613 fsc
->release_adapter_routes
= kvm_s390_release_adapter_routes
;
614 fsc
->clear_io_irq
= kvm_s390_clear_io_flic
;
615 fsc
->modify_ais_mode
= kvm_s390_modify_ais_mode
;
616 fsc
->inject_airq
= kvm_s390_inject_airq
;
619 static const TypeInfo kvm_s390_flic_info
= {
620 .name
= TYPE_KVM_S390_FLIC
,
621 .parent
= TYPE_S390_FLIC_COMMON
,
622 .instance_size
= sizeof(KVMS390FLICState
),
623 .class_size
= sizeof(KVMS390FLICStateClass
),
624 .class_init
= kvm_s390_flic_class_init
,
627 static void kvm_s390_flic_register_types(void)
629 type_register_static(&kvm_s390_flic_info
);
632 type_init(kvm_s390_flic_register_types
)