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 "hw/sysbus.h"
19 #include "sysemu/kvm.h"
20 #include "migration/qemu-file.h"
21 #include "hw/s390x/s390_flic.h"
22 #include "hw/s390x/adapter.h"
25 #define FLIC_SAVE_INITIAL_SIZE getpagesize()
26 #define FLIC_FAILED (-1UL)
27 #define FLIC_SAVEVM_VERSION 1
29 typedef struct KVMS390FLICState
{
30 S390FLICState parent_obj
;
33 bool clear_io_supported
;
36 DeviceState
*s390_flic_kvm_create(void)
38 DeviceState
*dev
= NULL
;
41 dev
= qdev_create(NULL
, TYPE_KVM_S390_FLIC
);
42 object_property_add_child(qdev_get_machine(), TYPE_KVM_S390_FLIC
,
49 * flic_get_all_irqs - store all pending irqs in buffer
50 * @buf: pointer to buffer which is passed to kernel
51 * @len: length of buffer
52 * @flic: pointer to flic device state
54 * Returns: -ENOMEM if buffer is too small,
55 * -EINVAL if attr.group is invalid,
56 * -EFAULT if copying to userspace failed,
57 * on success return number of stored interrupts
59 static int flic_get_all_irqs(KVMS390FLICState
*flic
,
62 struct kvm_device_attr attr
= {
63 .group
= KVM_DEV_FLIC_GET_ALL_IRQS
,
64 .addr
= (uint64_t) buf
,
69 rc
= ioctl(flic
->fd
, KVM_GET_DEVICE_ATTR
, &attr
);
71 return rc
== -1 ? -errno
: rc
;
74 static void flic_enable_pfault(KVMS390FLICState
*flic
)
76 struct kvm_device_attr attr
= {
77 .group
= KVM_DEV_FLIC_APF_ENABLE
,
81 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
84 fprintf(stderr
, "flic: couldn't enable pfault\n");
88 static void flic_disable_wait_pfault(KVMS390FLICState
*flic
)
90 struct kvm_device_attr attr
= {
91 .group
= KVM_DEV_FLIC_APF_DISABLE_WAIT
,
95 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
98 fprintf(stderr
, "flic: couldn't disable pfault\n");
102 /** flic_enqueue_irqs - returns 0 on success
103 * @buf: pointer to buffer which is passed to kernel
104 * @len: length of buffer
105 * @flic: pointer to flic device state
107 * Returns: -EINVAL if attr.group is unknown
109 static int flic_enqueue_irqs(void *buf
, uint64_t len
,
110 KVMS390FLICState
*flic
)
113 struct kvm_device_attr attr
= {
114 .group
= KVM_DEV_FLIC_ENQUEUE
,
115 .addr
= (uint64_t) buf
,
119 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
121 return rc
? -errno
: 0;
124 int kvm_s390_inject_flic(struct kvm_s390_irq
*irq
)
126 static KVMS390FLICState
*flic
;
128 if (unlikely(!flic
)) {
129 flic
= KVM_S390_FLIC(s390_get_flic());
131 return flic_enqueue_irqs(irq
, sizeof(*irq
), flic
);
134 static int kvm_s390_clear_io_flic(S390FLICState
*fs
, uint16_t subchannel_id
,
135 uint16_t subchannel_nr
)
137 KVMS390FLICState
*flic
= KVM_S390_FLIC(fs
);
139 uint32_t sid
= subchannel_id
<< 16 | subchannel_nr
;
140 struct kvm_device_attr attr
= {
141 .group
= KVM_DEV_FLIC_CLEAR_IO_IRQ
,
142 .addr
= (uint64_t) &sid
,
145 if (unlikely(!flic
->clear_io_supported
)) {
148 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
149 return rc
? -errno
: 0;
153 * __get_all_irqs - store all pending irqs in buffer
154 * @flic: pointer to flic device state
155 * @buf: pointer to pointer to a buffer
156 * @len: length of buffer
158 * Returns: return value of flic_get_all_irqs
159 * Note: Retry and increase buffer size until flic_get_all_irqs
160 * either returns a value >= 0 or a negative error code.
161 * -ENOMEM is an exception, which means the buffer is too small
162 * and we should try again. Other negative error codes can be
163 * -EFAULT and -EINVAL which we ignore at this point
165 static int __get_all_irqs(KVMS390FLICState
*flic
,
171 /* returns -ENOMEM if buffer is too small and number
172 * of queued interrupts on success */
173 r
= flic_get_all_irqs(flic
, *buf
, len
);
178 *buf
= g_try_realloc(*buf
, len
);
182 } while (r
== -ENOMEM
&& len
<= KVM_S390_FLIC_MAX_BUFFER
);
187 static int kvm_s390_register_io_adapter(S390FLICState
*fs
, uint32_t id
,
188 uint8_t isc
, bool swap
,
191 struct kvm_s390_io_adapter adapter
= {
194 .maskable
= is_maskable
,
197 KVMS390FLICState
*flic
= KVM_S390_FLIC(fs
);
199 struct kvm_device_attr attr
= {
200 .group
= KVM_DEV_FLIC_ADAPTER_REGISTER
,
201 .addr
= (uint64_t)&adapter
,
204 if (!kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
)) {
209 r
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
211 ret
= r
? -errno
: 0;
215 static int kvm_s390_io_adapter_map(S390FLICState
*fs
, uint32_t id
,
216 uint64_t map_addr
, bool do_map
)
218 struct kvm_s390_io_adapter_req req
= {
220 .type
= do_map
? KVM_S390_IO_ADAPTER_MAP
: KVM_S390_IO_ADAPTER_UNMAP
,
223 struct kvm_device_attr attr
= {
224 .group
= KVM_DEV_FLIC_ADAPTER_MODIFY
,
225 .addr
= (uint64_t)&req
,
227 KVMS390FLICState
*flic
= KVM_S390_FLIC(fs
);
230 if (!kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
)) {
235 r
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
236 return r
? -errno
: 0;
239 static int kvm_s390_add_adapter_routes(S390FLICState
*fs
,
240 AdapterRoutes
*routes
)
243 uint64_t ind_offset
= routes
->adapter
.ind_offset
;
245 for (i
= 0; i
< routes
->num_routes
; i
++) {
246 ret
= kvm_irqchip_add_adapter_route(kvm_state
, &routes
->adapter
);
250 routes
->gsi
[i
] = ret
;
251 routes
->adapter
.ind_offset
++;
253 kvm_irqchip_commit_routes(kvm_state
);
255 /* Restore passed-in structure to original state. */
256 routes
->adapter
.ind_offset
= ind_offset
;
260 kvm_irqchip_release_virq(kvm_state
, routes
->gsi
[i
]);
263 routes
->adapter
.ind_offset
= ind_offset
;
267 static void kvm_s390_release_adapter_routes(S390FLICState
*fs
,
268 AdapterRoutes
*routes
)
272 for (i
= 0; i
< routes
->num_routes
; i
++) {
273 if (routes
->gsi
[i
] >= 0) {
274 kvm_irqchip_release_virq(kvm_state
, routes
->gsi
[i
]);
281 * kvm_flic_save - Save pending floating interrupts
282 * @f: QEMUFile containing migration state
283 * @opaque: pointer to flic device state
285 * Note: Pass buf and len to kernel. Start with one page and
286 * increase until buffer is sufficient or maxium size is
289 static void kvm_flic_save(QEMUFile
*f
, void *opaque
)
291 KVMS390FLICState
*flic
= opaque
;
292 int len
= FLIC_SAVE_INITIAL_SIZE
;
296 flic_disable_wait_pfault((struct KVMS390FLICState
*) opaque
);
298 buf
= g_try_malloc0(len
);
300 /* Storing FLIC_FAILED into the count field here will cause the
301 * target system to fail when attempting to load irqs from the
303 error_report("flic: couldn't allocate memory");
304 qemu_put_be64(f
, FLIC_FAILED
);
308 count
= __get_all_irqs(flic
, &buf
, len
);
310 error_report("flic: couldn't retrieve irqs from kernel, rc %d",
312 /* Storing FLIC_FAILED into the count field here will cause the
313 * target system to fail when attempting to load irqs from the
315 qemu_put_be64(f
, FLIC_FAILED
);
317 qemu_put_be64(f
, count
);
318 qemu_put_buffer(f
, (uint8_t *) buf
,
319 count
* sizeof(struct kvm_s390_irq
));
325 * kvm_flic_load - Load pending floating interrupts
326 * @f: QEMUFile containing migration state
327 * @opaque: pointer to flic device state
328 * @version_id: version id for migration
330 * Returns: value of flic_enqueue_irqs, -EINVAL on error
331 * Note: Do nothing when no interrupts where stored
334 static int kvm_flic_load(QEMUFile
*f
, void *opaque
, int version_id
)
341 if (version_id
!= FLIC_SAVEVM_VERSION
) {
346 flic_enable_pfault((struct KVMS390FLICState
*) opaque
);
348 count
= qemu_get_be64(f
);
349 len
= count
* sizeof(struct kvm_s390_irq
);
350 if (count
== FLIC_FAILED
) {
358 buf
= g_try_malloc0(len
);
364 if (qemu_get_buffer(f
, (uint8_t *) buf
, len
) != len
) {
368 r
= flic_enqueue_irqs(buf
, len
, (struct KVMS390FLICState
*) opaque
);
376 static void kvm_s390_flic_realize(DeviceState
*dev
, Error
**errp
)
378 KVMS390FLICState
*flic_state
= KVM_S390_FLIC(dev
);
379 struct kvm_create_device cd
= {0};
380 struct kvm_device_attr test_attr
= {0};
384 if (!kvm_check_extension(kvm_state
, KVM_CAP_DEVICE_CTRL
)) {
385 trace_flic_no_device_api(errno
);
389 cd
.type
= KVM_DEV_TYPE_FLIC
;
390 ret
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_DEVICE
, &cd
);
392 trace_flic_create_device(errno
);
395 flic_state
->fd
= cd
.fd
;
397 /* Check clear_io_irq support */
398 test_attr
.group
= KVM_DEV_FLIC_CLEAR_IO_IRQ
;
399 flic_state
->clear_io_supported
= !ioctl(flic_state
->fd
,
400 KVM_HAS_DEVICE_ATTR
, test_attr
);
402 /* Register savevm handler for floating interrupts */
403 register_savevm(NULL
, "s390-flic", 0, 1, kvm_flic_save
,
404 kvm_flic_load
, (void *) flic_state
);
407 static void kvm_s390_flic_unrealize(DeviceState
*dev
, Error
**errp
)
409 KVMS390FLICState
*flic_state
= KVM_S390_FLIC(dev
);
411 unregister_savevm(DEVICE(flic_state
), "s390-flic", flic_state
);
414 static void kvm_s390_flic_reset(DeviceState
*dev
)
416 KVMS390FLICState
*flic
= KVM_S390_FLIC(dev
);
417 struct kvm_device_attr attr
= {
418 .group
= KVM_DEV_FLIC_CLEAR_IRQS
,
422 if (flic
->fd
== -1) {
426 flic_disable_wait_pfault(flic
);
428 rc
= ioctl(flic
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
430 trace_flic_reset_failed(errno
);
433 flic_enable_pfault(flic
);
436 static void kvm_s390_flic_class_init(ObjectClass
*oc
, void *data
)
438 DeviceClass
*dc
= DEVICE_CLASS(oc
);
439 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_CLASS(oc
);
441 dc
->realize
= kvm_s390_flic_realize
;
442 dc
->unrealize
= kvm_s390_flic_unrealize
;
443 dc
->reset
= kvm_s390_flic_reset
;
444 fsc
->register_io_adapter
= kvm_s390_register_io_adapter
;
445 fsc
->io_adapter_map
= kvm_s390_io_adapter_map
;
446 fsc
->add_adapter_routes
= kvm_s390_add_adapter_routes
;
447 fsc
->release_adapter_routes
= kvm_s390_release_adapter_routes
;
448 fsc
->clear_io_irq
= kvm_s390_clear_io_flic
;
451 static const TypeInfo kvm_s390_flic_info
= {
452 .name
= TYPE_KVM_S390_FLIC
,
453 .parent
= TYPE_S390_FLIC_COMMON
,
454 .instance_size
= sizeof(KVMS390FLICState
),
455 .class_init
= kvm_s390_flic_class_init
,
458 static void kvm_s390_flic_register_types(void)
460 type_register_static(&kvm_s390_flic_info
);
463 type_init(kvm_s390_flic_register_types
)