2 * QEMU S390x 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/error-report.h"
15 #include "hw/sysbus.h"
16 #include "hw/s390x/ioinst.h"
17 #include "hw/s390x/s390_flic.h"
18 #include "hw/s390x/css.h"
22 #include "qapi/error.h"
23 #include "hw/s390x/s390-virtio-ccw.h"
25 QEMUS390FLICState
*s390_get_qemu_flic(S390FLICState
*fs
)
27 static QEMUS390FLICState
*flic
;
30 /* we only have one flic device, so this is fine to cache */
31 flic
= QEMU_S390_FLIC(fs
);
36 S390FLICState
*s390_get_flic(void)
38 static S390FLICState
*fs
;
41 fs
= S390_FLIC_COMMON(object_resolve_path_type("",
42 TYPE_S390_FLIC_COMMON
,
48 void s390_flic_init(void)
53 dev
= qdev_create(NULL
, TYPE_KVM_S390_FLIC
);
54 object_property_add_child(qdev_get_machine(), TYPE_KVM_S390_FLIC
,
57 dev
= qdev_create(NULL
, TYPE_QEMU_S390_FLIC
);
58 object_property_add_child(qdev_get_machine(), TYPE_QEMU_S390_FLIC
,
61 qdev_init_nofail(dev
);
64 static int qemu_s390_register_io_adapter(S390FLICState
*fs
, uint32_t id
,
65 uint8_t isc
, bool swap
,
66 bool is_maskable
, uint8_t flags
)
72 static int qemu_s390_io_adapter_map(S390FLICState
*fs
, uint32_t id
,
73 uint64_t map_addr
, bool do_map
)
79 static int qemu_s390_add_adapter_routes(S390FLICState
*fs
,
80 AdapterRoutes
*routes
)
85 static void qemu_s390_release_adapter_routes(S390FLICState
*fs
,
86 AdapterRoutes
*routes
)
90 static int qemu_s390_clear_io_flic(S390FLICState
*fs
, uint16_t subchannel_id
,
91 uint16_t subchannel_nr
)
93 QEMUS390FLICState
*flic
= s390_get_qemu_flic(fs
);
94 QEMUS390FlicIO
*cur
, *next
;
97 g_assert(qemu_mutex_iothread_locked());
98 if (!(flic
->pending
& FLIC_PENDING_IO
)) {
103 for (isc
= 0; isc
< 8; isc
++) {
104 if (QLIST_EMPTY(&flic
->io
[isc
])) {
108 /* search and delete any matching one */
109 QLIST_FOREACH_SAFE(cur
, &flic
->io
[isc
], next
, next
) {
110 if (cur
->id
== subchannel_id
&& cur
->nr
== subchannel_nr
) {
111 QLIST_REMOVE(cur
, next
);
116 /* update our indicator bit */
117 if (QLIST_EMPTY(&flic
->io
[isc
])) {
118 flic
->pending
&= ~ISC_TO_PENDING_IO(isc
);
124 static int qemu_s390_modify_ais_mode(S390FLICState
*fs
, uint8_t isc
,
127 QEMUS390FLICState
*flic
= s390_get_qemu_flic(fs
);
130 case SIC_IRQ_MODE_ALL
:
131 flic
->simm
&= ~AIS_MODE_MASK(isc
);
132 flic
->nimm
&= ~AIS_MODE_MASK(isc
);
134 case SIC_IRQ_MODE_SINGLE
:
135 flic
->simm
|= AIS_MODE_MASK(isc
);
136 flic
->nimm
&= ~AIS_MODE_MASK(isc
);
145 static int qemu_s390_inject_airq(S390FLICState
*fs
, uint8_t type
,
146 uint8_t isc
, uint8_t flags
)
148 QEMUS390FLICState
*flic
= s390_get_qemu_flic(fs
);
149 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_GET_CLASS(fs
);
150 bool flag
= flags
& S390_ADAPTER_SUPPRESSIBLE
;
151 uint32_t io_int_word
= (isc
<< 27) | IO_INT_WORD_AI
;
153 if (flag
&& (flic
->nimm
& AIS_MODE_MASK(isc
))) {
154 trace_qemu_s390_airq_suppressed(type
, isc
);
158 fsc
->inject_io(fs
, 0, 0, 0, io_int_word
);
160 if (flag
&& (flic
->simm
& AIS_MODE_MASK(isc
))) {
161 flic
->nimm
|= AIS_MODE_MASK(isc
);
162 trace_qemu_s390_suppress_airq(isc
, "Single-Interruption Mode",
163 "NO-Interruptions Mode");
169 static void qemu_s390_flic_notify(uint32_t type
)
174 * We have to make all CPUs see CPU_INTERRUPT_HARD, so they might
175 * consider it. We will kick all running CPUs and only relevant
179 S390CPU
*cpu
= S390_CPU(cs
);
181 cs
->interrupt_request
|= CPU_INTERRUPT_HARD
;
183 /* ignore CPUs that are not sleeping */
184 if (s390_cpu_get_state(cpu
) != CPU_STATE_OPERATING
&&
185 s390_cpu_get_state(cpu
) != CPU_STATE_LOAD
) {
189 /* we always kick running CPUs for now, this is tricky */
191 /* don't check for subclasses, CPUs double check when waking up */
192 if (type
& FLIC_PENDING_SERVICE
) {
193 if (!(cpu
->env
.psw
.mask
& PSW_MASK_EXT
)) {
196 } else if (type
& FLIC_PENDING_IO
) {
197 if (!(cpu
->env
.psw
.mask
& PSW_MASK_IO
)) {
200 } else if (type
& FLIC_PENDING_MCHK_CR
) {
201 if (!(cpu
->env
.psw
.mask
& PSW_MASK_MCHECK
)) {
206 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
210 uint32_t qemu_s390_flic_dequeue_service(QEMUS390FLICState
*flic
)
214 g_assert(qemu_mutex_iothread_locked());
215 g_assert(flic
->pending
& FLIC_PENDING_SERVICE
);
216 tmp
= flic
->service_param
;
217 flic
->service_param
= 0;
218 flic
->pending
&= ~FLIC_PENDING_SERVICE
;
223 /* caller has to free the returned object */
224 QEMUS390FlicIO
*qemu_s390_flic_dequeue_io(QEMUS390FLICState
*flic
, uint64_t cr6
)
229 g_assert(qemu_mutex_iothread_locked());
230 if (!(flic
->pending
& CR6_TO_PENDING_IO(cr6
))) {
234 for (isc
= 0; isc
< 8; isc
++) {
235 if (QLIST_EMPTY(&flic
->io
[isc
]) || !(cr6
& ISC_TO_ISC_BITS(isc
))) {
238 io
= QLIST_FIRST(&flic
->io
[isc
]);
239 QLIST_REMOVE(io
, next
);
241 /* update our indicator bit */
242 if (QLIST_EMPTY(&flic
->io
[isc
])) {
243 flic
->pending
&= ~ISC_TO_PENDING_IO(isc
);
251 void qemu_s390_flic_dequeue_crw_mchk(QEMUS390FLICState
*flic
)
253 g_assert(qemu_mutex_iothread_locked());
254 g_assert(flic
->pending
& FLIC_PENDING_MCHK_CR
);
255 flic
->pending
&= ~FLIC_PENDING_MCHK_CR
;
258 static void qemu_s390_inject_service(S390FLICState
*fs
, uint32_t parm
)
260 QEMUS390FLICState
*flic
= s390_get_qemu_flic(fs
);
262 g_assert(qemu_mutex_iothread_locked());
263 /* multiplexing is good enough for sclp - kvm does it internally as well */
264 flic
->service_param
|= parm
;
265 flic
->pending
|= FLIC_PENDING_SERVICE
;
267 qemu_s390_flic_notify(FLIC_PENDING_SERVICE
);
270 static void qemu_s390_inject_io(S390FLICState
*fs
, uint16_t subchannel_id
,
271 uint16_t subchannel_nr
, uint32_t io_int_parm
,
272 uint32_t io_int_word
)
274 const uint8_t isc
= IO_INT_WORD_ISC(io_int_word
);
275 QEMUS390FLICState
*flic
= s390_get_qemu_flic(fs
);
278 g_assert(qemu_mutex_iothread_locked());
279 io
= g_new0(QEMUS390FlicIO
, 1);
280 io
->id
= subchannel_id
;
281 io
->nr
= subchannel_nr
;
282 io
->parm
= io_int_parm
;
283 io
->word
= io_int_word
;
285 QLIST_INSERT_HEAD(&flic
->io
[isc
], io
, next
);
286 flic
->pending
|= ISC_TO_PENDING_IO(isc
);
288 qemu_s390_flic_notify(ISC_TO_PENDING_IO(isc
));
291 static void qemu_s390_inject_crw_mchk(S390FLICState
*fs
)
293 QEMUS390FLICState
*flic
= s390_get_qemu_flic(fs
);
295 g_assert(qemu_mutex_iothread_locked());
296 flic
->pending
|= FLIC_PENDING_MCHK_CR
;
298 qemu_s390_flic_notify(FLIC_PENDING_MCHK_CR
);
301 bool qemu_s390_flic_has_service(QEMUS390FLICState
*flic
)
303 /* called without lock via cc->has_work, will be validated under lock */
304 return !!(flic
->pending
& FLIC_PENDING_SERVICE
);
307 bool qemu_s390_flic_has_io(QEMUS390FLICState
*flic
, uint64_t cr6
)
309 /* called without lock via cc->has_work, will be validated under lock */
310 return !!(flic
->pending
& CR6_TO_PENDING_IO(cr6
));
313 bool qemu_s390_flic_has_crw_mchk(QEMUS390FLICState
*flic
)
315 /* called without lock via cc->has_work, will be validated under lock */
316 return !!(flic
->pending
& FLIC_PENDING_MCHK_CR
);
319 bool qemu_s390_flic_has_any(QEMUS390FLICState
*flic
)
321 g_assert(qemu_mutex_iothread_locked());
322 return !!flic
->pending
;
325 static void qemu_s390_flic_reset(DeviceState
*dev
)
327 QEMUS390FLICState
*flic
= QEMU_S390_FLIC(dev
);
328 QEMUS390FlicIO
*cur
, *next
;
331 g_assert(qemu_mutex_iothread_locked());
336 /* remove all pending io interrupts */
337 for (isc
= 0; isc
< 8; isc
++) {
338 QLIST_FOREACH_SAFE(cur
, &flic
->io
[isc
], next
, next
) {
339 QLIST_REMOVE(cur
, next
);
345 bool ais_needed(void *opaque
)
347 S390FLICState
*s
= opaque
;
349 return s
->ais_supported
;
352 static const VMStateDescription qemu_s390_flic_vmstate
= {
353 .name
= "qemu-s390-flic",
355 .minimum_version_id
= 1,
356 .needed
= ais_needed
,
357 .fields
= (VMStateField
[]) {
358 VMSTATE_UINT8(simm
, QEMUS390FLICState
),
359 VMSTATE_UINT8(nimm
, QEMUS390FLICState
),
360 VMSTATE_END_OF_LIST()
364 static void qemu_s390_flic_instance_init(Object
*obj
)
366 QEMUS390FLICState
*flic
= QEMU_S390_FLIC(obj
);
369 for (isc
= 0; isc
< 8; isc
++) {
370 QLIST_INIT(&flic
->io
[isc
]);
374 static void qemu_s390_flic_class_init(ObjectClass
*oc
, void *data
)
376 DeviceClass
*dc
= DEVICE_CLASS(oc
);
377 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_CLASS(oc
);
379 dc
->reset
= qemu_s390_flic_reset
;
380 dc
->vmsd
= &qemu_s390_flic_vmstate
;
381 fsc
->register_io_adapter
= qemu_s390_register_io_adapter
;
382 fsc
->io_adapter_map
= qemu_s390_io_adapter_map
;
383 fsc
->add_adapter_routes
= qemu_s390_add_adapter_routes
;
384 fsc
->release_adapter_routes
= qemu_s390_release_adapter_routes
;
385 fsc
->clear_io_irq
= qemu_s390_clear_io_flic
;
386 fsc
->modify_ais_mode
= qemu_s390_modify_ais_mode
;
387 fsc
->inject_airq
= qemu_s390_inject_airq
;
388 fsc
->inject_service
= qemu_s390_inject_service
;
389 fsc
->inject_io
= qemu_s390_inject_io
;
390 fsc
->inject_crw_mchk
= qemu_s390_inject_crw_mchk
;
393 static Property s390_flic_common_properties
[] = {
394 DEFINE_PROP_UINT32("adapter_routes_max_batch", S390FLICState
,
395 adapter_routes_max_batch
, ADAPTER_ROUTES_MAX_GSI
),
396 DEFINE_PROP_END_OF_LIST(),
399 static void s390_flic_common_realize(DeviceState
*dev
, Error
**errp
)
401 S390FLICState
*fs
= S390_FLIC_COMMON(dev
);
402 uint32_t max_batch
= fs
->adapter_routes_max_batch
;
404 if (max_batch
> ADAPTER_ROUTES_MAX_GSI
) {
405 error_setg(errp
, "flic property adapter_routes_max_batch too big"
406 " (%d > %d)", max_batch
, ADAPTER_ROUTES_MAX_GSI
);
410 fs
->ais_supported
= s390_has_feat(S390_FEAT_ADAPTER_INT_SUPPRESSION
);
413 static void s390_flic_class_init(ObjectClass
*oc
, void *data
)
415 DeviceClass
*dc
= DEVICE_CLASS(oc
);
417 dc
->props
= s390_flic_common_properties
;
418 dc
->realize
= s390_flic_common_realize
;
421 static const TypeInfo qemu_s390_flic_info
= {
422 .name
= TYPE_QEMU_S390_FLIC
,
423 .parent
= TYPE_S390_FLIC_COMMON
,
424 .instance_size
= sizeof(QEMUS390FLICState
),
425 .instance_init
= qemu_s390_flic_instance_init
,
426 .class_init
= qemu_s390_flic_class_init
,
430 static const TypeInfo s390_flic_common_info
= {
431 .name
= TYPE_S390_FLIC_COMMON
,
432 .parent
= TYPE_SYS_BUS_DEVICE
,
433 .instance_size
= sizeof(S390FLICState
),
434 .class_init
= s390_flic_class_init
,
435 .class_size
= sizeof(S390FLICStateClass
),
438 static void qemu_s390_flic_register_types(void)
440 type_register_static(&s390_flic_common_info
);
441 type_register_static(&qemu_s390_flic_info
);
444 type_init(qemu_s390_flic_register_types
)
446 static bool adapter_info_so_needed(void *opaque
)
448 return css_migration_enabled();
451 const VMStateDescription vmstate_adapter_info_so
= {
452 .name
= "s390_adapter_info/summary_offset",
454 .minimum_version_id
= 1,
455 .needed
= adapter_info_so_needed
,
456 .fields
= (VMStateField
[]) {
457 VMSTATE_UINT32(summary_offset
, AdapterInfo
),
458 VMSTATE_END_OF_LIST()
462 const VMStateDescription vmstate_adapter_info
= {
463 .name
= "s390_adapter_info",
465 .minimum_version_id
= 1,
466 .fields
= (VMStateField
[]) {
467 VMSTATE_UINT64(ind_offset
, AdapterInfo
),
469 * We do not have to migrate neither the id nor the addresses.
470 * The id is set by css_register_io_adapter and the addresses
471 * are set based on the IndAddr objects after those get mapped.
473 VMSTATE_END_OF_LIST()
475 .subsections
= (const VMStateDescription
* []) {
476 &vmstate_adapter_info_so
,
481 const VMStateDescription vmstate_adapter_routes
= {
483 .name
= "s390_adapter_routes",
485 .minimum_version_id
= 1,
486 .fields
= (VMStateField
[]) {
487 VMSTATE_STRUCT(adapter
, AdapterRoutes
, 1, vmstate_adapter_info
,
489 VMSTATE_END_OF_LIST()