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 "kvm/kvm_s390x.h"
15 #include <sys/ioctl.h>
16 #include "qemu/error-report.h"
17 #include "qemu/module.h"
18 #include "qapi/error.h"
19 #include "sysemu/kvm.h"
20 #include "hw/s390x/s390_flic.h"
21 #include "hw/s390x/adapter.h"
22 #include "hw/s390x/css.h"
23 #include "migration/qemu-file-types.h"
25 #include "qom/object.h"
27 #define FLIC_SAVE_INITIAL_SIZE qemu_real_host_page_size()
28 #define FLIC_FAILED (-1UL)
29 #define FLIC_SAVEVM_VERSION 1
31 struct KVMS390FLICState
{
32 S390FLICState parent_obj
;
35 bool clear_io_supported
;
38 static KVMS390FLICState
*s390_get_kvm_flic(S390FLICState
*fs
)
40 static KVMS390FLICState
*flic
;
43 /* we only have one flic device, so this is fine to cache */
44 flic
= KVM_S390_FLIC(fs
);
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 static void kvm_s390_inject_flic(S390FLICState
*fs
, struct kvm_s390_irq
*irq
)
127 static bool use_flic
= true;
131 r
= flic_enqueue_irqs(irq
, sizeof(*irq
), s390_get_kvm_flic(fs
));
139 /* fallback to legacy KVM IOCTL in case FLIC fails */
140 kvm_s390_floating_interrupt_legacy(irq
);
143 static void kvm_s390_inject_service(S390FLICState
*fs
, uint32_t parm
)
145 struct kvm_s390_irq irq
= {
146 .type
= KVM_S390_INT_SERVICE
,
147 .u
.ext
.ext_params
= parm
,
150 kvm_s390_inject_flic(fs
, &irq
);
153 static void kvm_s390_inject_io(S390FLICState
*fs
, uint16_t subchannel_id
,
154 uint16_t subchannel_nr
, uint32_t io_int_parm
,
155 uint32_t io_int_word
)
157 struct kvm_s390_irq irq
= {
158 .u
.io
.subchannel_id
= subchannel_id
,
159 .u
.io
.subchannel_nr
= subchannel_nr
,
160 .u
.io
.io_int_parm
= io_int_parm
,
161 .u
.io
.io_int_word
= io_int_word
,
164 if (io_int_word
& IO_INT_WORD_AI
) {
165 irq
.type
= KVM_S390_INT_IO(1, 0, 0, 0);
167 irq
.type
= KVM_S390_INT_IO(0, (subchannel_id
& 0xff00) >> 8,
168 (subchannel_id
& 0x0006),
171 kvm_s390_inject_flic(fs
, &irq
);
174 static void kvm_s390_inject_crw_mchk(S390FLICState
*fs
)
176 struct kvm_s390_irq irq
= {
177 .type
= KVM_S390_MCHK
,
178 .u
.mchk
.cr14
= CR14_CHANNEL_REPORT_SC
,
179 .u
.mchk
.mcic
= s390_build_validity_mcic() | MCIC_SC_CP
,
182 kvm_s390_inject_flic(fs
, &irq
);
185 static int kvm_s390_clear_io_flic(S390FLICState
*fs
, uint16_t subchannel_id
,
186 uint16_t subchannel_nr
)
188 KVMS390FLICState
*flic
= s390_get_kvm_flic(fs
);
190 uint32_t sid
= subchannel_id
<< 16 | subchannel_nr
;
191 struct kvm_device_attr attr
= {
192 .group
= KVM_DEV_FLIC_CLEAR_IO_IRQ
,
193 .addr
= (uint64_t) &sid
,
196 if (unlikely(!flic
->clear_io_supported
)) {
199 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
200 return rc
? -errno
: 0;
203 static int kvm_s390_modify_ais_mode(S390FLICState
*fs
, uint8_t isc
,
206 KVMS390FLICState
*flic
= s390_get_kvm_flic(fs
);
207 struct kvm_s390_ais_req req
= {
211 struct kvm_device_attr attr
= {
212 .group
= KVM_DEV_FLIC_AISM
,
213 .addr
= (uint64_t)&req
,
216 if (!fs
->ais_supported
) {
220 return ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
) ? -errno
: 0;
223 static int kvm_s390_inject_airq(S390FLICState
*fs
, uint8_t type
,
224 uint8_t isc
, uint8_t flags
)
226 KVMS390FLICState
*flic
= s390_get_kvm_flic(fs
);
227 uint32_t id
= css_get_adapter_id(type
, isc
);
228 struct kvm_device_attr attr
= {
229 .group
= KVM_DEV_FLIC_AIRQ_INJECT
,
233 if (!fs
->ais_supported
) {
237 return ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
) ? -errno
: 0;
241 * __get_all_irqs - store all pending irqs in buffer
242 * @flic: pointer to flic device state
243 * @buf: pointer to pointer to a buffer
244 * @len: length of buffer
246 * Returns: return value of flic_get_all_irqs
247 * Note: Retry and increase buffer size until flic_get_all_irqs
248 * either returns a value >= 0 or a negative error code.
249 * -ENOMEM is an exception, which means the buffer is too small
250 * and we should try again. Other negative error codes can be
251 * -EFAULT and -EINVAL which we ignore at this point
253 static int __get_all_irqs(KVMS390FLICState
*flic
,
259 /* returns -ENOMEM if buffer is too small and number
260 * of queued interrupts on success */
261 r
= flic_get_all_irqs(flic
, *buf
, len
);
266 *buf
= g_try_realloc(*buf
, len
);
270 } while (r
== -ENOMEM
&& len
<= KVM_S390_FLIC_MAX_BUFFER
);
275 static int kvm_s390_register_io_adapter(S390FLICState
*fs
, uint32_t id
,
276 uint8_t isc
, bool swap
,
277 bool is_maskable
, uint8_t flags
)
279 struct kvm_s390_io_adapter adapter
= {
282 .maskable
= is_maskable
,
286 KVMS390FLICState
*flic
= KVM_S390_FLIC(fs
);
288 struct kvm_device_attr attr
= {
289 .group
= KVM_DEV_FLIC_ADAPTER_REGISTER
,
290 .addr
= (uint64_t)&adapter
,
293 if (!kvm_gsi_routing_enabled()) {
298 r
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
300 return r
? -errno
: 0;
303 static int kvm_s390_io_adapter_map(S390FLICState
*fs
, uint32_t id
,
304 uint64_t map_addr
, bool do_map
)
306 struct kvm_s390_io_adapter_req req
= {
308 .type
= do_map
? KVM_S390_IO_ADAPTER_MAP
: KVM_S390_IO_ADAPTER_UNMAP
,
311 struct kvm_device_attr attr
= {
312 .group
= KVM_DEV_FLIC_ADAPTER_MODIFY
,
313 .addr
= (uint64_t)&req
,
315 KVMS390FLICState
*flic
= s390_get_kvm_flic(fs
);
318 if (!kvm_gsi_routing_enabled()) {
323 r
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
324 return r
? -errno
: 0;
327 static int kvm_s390_add_adapter_routes(S390FLICState
*fs
,
328 AdapterRoutes
*routes
)
331 uint64_t ind_offset
= routes
->adapter
.ind_offset
;
333 if (!kvm_gsi_routing_enabled()) {
337 for (i
= 0; i
< routes
->num_routes
; i
++) {
338 ret
= kvm_irqchip_add_adapter_route(kvm_state
, &routes
->adapter
);
342 routes
->gsi
[i
] = ret
;
343 routes
->adapter
.ind_offset
++;
345 kvm_irqchip_commit_routes(kvm_state
);
347 /* Restore passed-in structure to original state. */
348 routes
->adapter
.ind_offset
= ind_offset
;
352 kvm_irqchip_release_virq(kvm_state
, routes
->gsi
[i
]);
355 routes
->adapter
.ind_offset
= ind_offset
;
359 static void kvm_s390_release_adapter_routes(S390FLICState
*fs
,
360 AdapterRoutes
*routes
)
364 if (!kvm_gsi_routing_enabled()) {
368 for (i
= 0; i
< routes
->num_routes
; i
++) {
369 if (routes
->gsi
[i
] >= 0) {
370 kvm_irqchip_release_virq(kvm_state
, routes
->gsi
[i
]);
377 * kvm_flic_save - Save pending floating interrupts
378 * @f: QEMUFile containing migration state
379 * @opaque: pointer to flic device state
382 * Note: Pass buf and len to kernel. Start with one page and
383 * increase until buffer is sufficient or maxium size is
386 static int kvm_flic_save(QEMUFile
*f
, void *opaque
, size_t size
,
387 const VMStateField
*field
, JSONWriter
*vmdesc
)
389 KVMS390FLICState
*flic
= opaque
;
390 int len
= FLIC_SAVE_INITIAL_SIZE
;
395 flic_disable_wait_pfault((struct KVMS390FLICState
*) opaque
);
397 buf
= g_try_malloc0(len
);
399 /* Storing FLIC_FAILED into the count field here will cause the
400 * target system to fail when attempting to load irqs from the
402 error_report("flic: couldn't allocate memory");
403 qemu_put_be64(f
, FLIC_FAILED
);
407 count
= __get_all_irqs(flic
, &buf
, len
);
409 error_report("flic: couldn't retrieve irqs from kernel, rc %d",
411 /* Storing FLIC_FAILED into the count field here will cause the
412 * target system to fail when attempting to load irqs from the
414 qemu_put_be64(f
, FLIC_FAILED
);
417 qemu_put_be64(f
, count
);
418 qemu_put_buffer(f
, (uint8_t *) buf
,
419 count
* sizeof(struct kvm_s390_irq
));
427 * kvm_flic_load - Load pending floating interrupts
428 * @f: QEMUFile containing migration state
429 * @opaque: pointer to flic device state
432 * Returns: value of flic_enqueue_irqs, -EINVAL on error
433 * Note: Do nothing when no interrupts where stored
436 static int kvm_flic_load(QEMUFile
*f
, void *opaque
, size_t size
,
437 const VMStateField
*field
)
444 flic_enable_pfault((struct KVMS390FLICState
*) opaque
);
446 count
= qemu_get_be64(f
);
447 len
= count
* sizeof(struct kvm_s390_irq
);
448 if (count
== FLIC_FAILED
) {
454 buf
= g_try_malloc0(len
);
459 if (qemu_get_buffer(f
, (uint8_t *) buf
, len
) != len
) {
463 r
= flic_enqueue_irqs(buf
, len
, (struct KVMS390FLICState
*) opaque
);
470 typedef struct KVMS390FLICStateMigTmp
{
471 KVMS390FLICState
*parent
;
474 } KVMS390FLICStateMigTmp
;
476 static int kvm_flic_ais_pre_save(void *opaque
)
478 KVMS390FLICStateMigTmp
*tmp
= opaque
;
479 KVMS390FLICState
*flic
= tmp
->parent
;
480 struct kvm_s390_ais_all ais
;
481 struct kvm_device_attr attr
= {
482 .group
= KVM_DEV_FLIC_AISM_ALL
,
483 .addr
= (uint64_t)&ais
,
487 if (ioctl(flic
->fd
, KVM_GET_DEVICE_ATTR
, &attr
)) {
488 error_report("Failed to retrieve kvm flic ais states");
492 tmp
->simm
= ais
.simm
;
493 tmp
->nimm
= ais
.nimm
;
498 static int kvm_flic_ais_post_load(void *opaque
, int version_id
)
500 KVMS390FLICStateMigTmp
*tmp
= opaque
;
501 KVMS390FLICState
*flic
= tmp
->parent
;
502 struct kvm_s390_ais_all ais
= {
506 struct kvm_device_attr attr
= {
507 .group
= KVM_DEV_FLIC_AISM_ALL
,
508 .addr
= (uint64_t)&ais
,
511 /* This can happen when the user mis-configures its guests in an
512 * incompatible fashion or without a CPU model. For example using
513 * qemu with -cpu host (which is not migration safe) and do a
514 * migration from a host that has AIS to a host that has no AIS.
515 * In that case the target system will reject the migration here.
517 if (!ais_needed(flic
)) {
521 return ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
) ? -errno
: 0;
524 static const VMStateDescription kvm_s390_flic_ais_tmp
= {
525 .name
= "s390-flic-ais-tmp",
526 .pre_save
= kvm_flic_ais_pre_save
,
527 .post_load
= kvm_flic_ais_post_load
,
528 .fields
= (VMStateField
[]) {
529 VMSTATE_UINT8(simm
, KVMS390FLICStateMigTmp
),
530 VMSTATE_UINT8(nimm
, KVMS390FLICStateMigTmp
),
531 VMSTATE_END_OF_LIST()
535 static const VMStateDescription kvm_s390_flic_vmstate_ais
= {
536 .name
= "s390-flic/ais",
538 .minimum_version_id
= 1,
539 .needed
= ais_needed
,
540 .fields
= (VMStateField
[]) {
541 VMSTATE_WITH_TMP(KVMS390FLICState
, KVMS390FLICStateMigTmp
,
542 kvm_s390_flic_ais_tmp
),
543 VMSTATE_END_OF_LIST()
547 static const VMStateDescription kvm_s390_flic_vmstate
= {
548 /* should have been like kvm-s390-flic,
549 * can't change without breaking compat */
551 .version_id
= FLIC_SAVEVM_VERSION
,
552 .minimum_version_id
= FLIC_SAVEVM_VERSION
,
553 .fields
= (VMStateField
[]) {
556 .info
= &(const VMStateInfo
) {
558 .get
= kvm_flic_load
,
559 .put
= kvm_flic_save
,
563 VMSTATE_END_OF_LIST()
565 .subsections
= (const VMStateDescription
* []) {
566 &kvm_s390_flic_vmstate_ais
,
571 struct KVMS390FLICStateClass
{
572 S390FLICStateClass parent_class
;
573 DeviceRealize parent_realize
;
575 typedef struct KVMS390FLICStateClass KVMS390FLICStateClass
;
577 DECLARE_CLASS_CHECKERS(KVMS390FLICStateClass
, KVM_S390_FLIC
,
581 static void kvm_s390_flic_realize(DeviceState
*dev
, Error
**errp
)
583 KVMS390FLICState
*flic_state
= KVM_S390_FLIC(dev
);
584 struct kvm_create_device cd
= {0};
585 struct kvm_device_attr test_attr
= {0};
589 KVM_S390_FLIC_GET_CLASS(dev
)->parent_realize(dev
, &err
);
591 error_propagate(errp
, err
);
596 cd
.type
= KVM_DEV_TYPE_FLIC
;
597 ret
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_DEVICE
, &cd
);
599 error_setg_errno(errp
, errno
, "Creating the KVM device failed");
600 trace_flic_create_device(errno
);
603 flic_state
->fd
= cd
.fd
;
605 /* Check clear_io_irq support */
606 test_attr
.group
= KVM_DEV_FLIC_CLEAR_IO_IRQ
;
607 flic_state
->clear_io_supported
= !ioctl(flic_state
->fd
,
608 KVM_HAS_DEVICE_ATTR
, test_attr
);
611 static void kvm_s390_flic_reset(DeviceState
*dev
)
613 KVMS390FLICState
*flic
= KVM_S390_FLIC(dev
);
614 S390FLICState
*fs
= S390_FLIC_COMMON(dev
);
615 struct kvm_device_attr attr
= {
616 .group
= KVM_DEV_FLIC_CLEAR_IRQS
,
621 if (flic
->fd
== -1) {
625 flic_disable_wait_pfault(flic
);
627 if (fs
->ais_supported
) {
628 for (isc
= 0; isc
<= MAX_ISC
; isc
++) {
629 rc
= kvm_s390_modify_ais_mode(fs
, isc
, SIC_IRQ_MODE_ALL
);
631 error_report("Failed to reset ais mode for isc %d: %s",
637 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
639 trace_flic_reset_failed(errno
);
642 flic_enable_pfault(flic
);
645 static void kvm_s390_flic_class_init(ObjectClass
*oc
, void *data
)
647 DeviceClass
*dc
= DEVICE_CLASS(oc
);
648 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_CLASS(oc
);
650 KVM_S390_FLIC_CLASS(oc
)->parent_realize
= dc
->realize
;
651 dc
->realize
= kvm_s390_flic_realize
;
652 dc
->vmsd
= &kvm_s390_flic_vmstate
;
653 dc
->reset
= kvm_s390_flic_reset
;
654 fsc
->register_io_adapter
= kvm_s390_register_io_adapter
;
655 fsc
->io_adapter_map
= kvm_s390_io_adapter_map
;
656 fsc
->add_adapter_routes
= kvm_s390_add_adapter_routes
;
657 fsc
->release_adapter_routes
= kvm_s390_release_adapter_routes
;
658 fsc
->clear_io_irq
= kvm_s390_clear_io_flic
;
659 fsc
->modify_ais_mode
= kvm_s390_modify_ais_mode
;
660 fsc
->inject_airq
= kvm_s390_inject_airq
;
661 fsc
->inject_service
= kvm_s390_inject_service
;
662 fsc
->inject_io
= kvm_s390_inject_io
;
663 fsc
->inject_crw_mchk
= kvm_s390_inject_crw_mchk
;
666 static const TypeInfo kvm_s390_flic_info
= {
667 .name
= TYPE_KVM_S390_FLIC
,
668 .parent
= TYPE_S390_FLIC_COMMON
,
669 .instance_size
= sizeof(KVMS390FLICState
),
670 .class_size
= sizeof(KVMS390FLICStateClass
),
671 .class_init
= kvm_s390_flic_class_init
,
674 static void kvm_s390_flic_register_types(void)
676 type_register_static(&kvm_s390_flic_info
);
679 type_init(kvm_s390_flic_register_types
)