Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / hw / intc / s390_flic.c
blob74e02858d43e3596d669e0d1a26b2bc8e4c90f3c
1 /*
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
10 * directory.
13 #include "qemu/osdep.h"
14 #include "qemu/error-report.h"
15 #include "qemu/main-loop.h"
16 #include "qemu/module.h"
17 #include "hw/sysbus.h"
18 #include "hw/s390x/ioinst.h"
19 #include "hw/s390x/s390_flic.h"
20 #include "hw/qdev-properties.h"
21 #include "hw/s390x/css.h"
22 #include "trace.h"
23 #include "qapi/error.h"
24 #include "hw/s390x/s390-virtio-ccw.h"
26 S390FLICStateClass *s390_get_flic_class(S390FLICState *fs)
28 static S390FLICStateClass *class;
30 if (!class) {
31 /* we only have one flic device, so this is fine to cache */
32 class = S390_FLIC_COMMON_GET_CLASS(fs);
34 return class;
37 QEMUS390FLICState *s390_get_qemu_flic(S390FLICState *fs)
39 static QEMUS390FLICState *flic;
41 if (!flic) {
42 /* we only have one flic device, so this is fine to cache */
43 flic = QEMU_S390_FLIC(fs);
45 return flic;
48 S390FLICState *s390_get_flic(void)
50 static S390FLICState *fs;
52 if (!fs) {
53 fs = S390_FLIC_COMMON(object_resolve_path_type("",
54 TYPE_S390_FLIC_COMMON,
55 NULL));
57 return fs;
60 void s390_flic_init(void)
62 DeviceState *dev;
64 if (kvm_enabled()) {
65 dev = qdev_new(TYPE_KVM_S390_FLIC);
66 object_property_add_child(qdev_get_machine(), TYPE_KVM_S390_FLIC,
67 OBJECT(dev));
68 } else {
69 dev = qdev_new(TYPE_QEMU_S390_FLIC);
70 object_property_add_child(qdev_get_machine(), TYPE_QEMU_S390_FLIC,
71 OBJECT(dev));
73 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
76 static int qemu_s390_register_io_adapter(S390FLICState *fs, uint32_t id,
77 uint8_t isc, bool swap,
78 bool is_maskable, uint8_t flags)
80 /* nothing to do */
81 return 0;
84 static int qemu_s390_io_adapter_map(S390FLICState *fs, uint32_t id,
85 uint64_t map_addr, bool do_map)
87 /* nothing to do */
88 return 0;
91 static int qemu_s390_add_adapter_routes(S390FLICState *fs,
92 AdapterRoutes *routes)
94 return -ENOSYS;
97 static void qemu_s390_release_adapter_routes(S390FLICState *fs,
98 AdapterRoutes *routes)
102 static int qemu_s390_clear_io_flic(S390FLICState *fs, uint16_t subchannel_id,
103 uint16_t subchannel_nr)
105 QEMUS390FLICState *flic = s390_get_qemu_flic(fs);
106 QEMUS390FlicIO *cur, *next;
107 uint8_t isc;
109 g_assert(qemu_mutex_iothread_locked());
110 if (!(flic->pending & FLIC_PENDING_IO)) {
111 return 0;
114 /* check all iscs */
115 for (isc = 0; isc < 8; isc++) {
116 if (QLIST_EMPTY(&flic->io[isc])) {
117 continue;
120 /* search and delete any matching one */
121 QLIST_FOREACH_SAFE(cur, &flic->io[isc], next, next) {
122 if (cur->id == subchannel_id && cur->nr == subchannel_nr) {
123 QLIST_REMOVE(cur, next);
124 g_free(cur);
128 /* update our indicator bit */
129 if (QLIST_EMPTY(&flic->io[isc])) {
130 flic->pending &= ~ISC_TO_PENDING_IO(isc);
133 return 0;
136 static int qemu_s390_modify_ais_mode(S390FLICState *fs, uint8_t isc,
137 uint16_t mode)
139 QEMUS390FLICState *flic = s390_get_qemu_flic(fs);
141 switch (mode) {
142 case SIC_IRQ_MODE_ALL:
143 flic->simm &= ~AIS_MODE_MASK(isc);
144 flic->nimm &= ~AIS_MODE_MASK(isc);
145 break;
146 case SIC_IRQ_MODE_SINGLE:
147 flic->simm |= AIS_MODE_MASK(isc);
148 flic->nimm &= ~AIS_MODE_MASK(isc);
149 break;
150 default:
151 return -EINVAL;
154 return 0;
157 static int qemu_s390_inject_airq(S390FLICState *fs, uint8_t type,
158 uint8_t isc, uint8_t flags)
160 QEMUS390FLICState *flic = s390_get_qemu_flic(fs);
161 S390FLICStateClass *fsc = s390_get_flic_class(fs);
162 bool flag = flags & S390_ADAPTER_SUPPRESSIBLE;
163 uint32_t io_int_word = (isc << 27) | IO_INT_WORD_AI;
165 if (flag && (flic->nimm & AIS_MODE_MASK(isc))) {
166 trace_qemu_s390_airq_suppressed(type, isc);
167 return 0;
170 fsc->inject_io(fs, 0, 0, 0, io_int_word);
172 if (flag && (flic->simm & AIS_MODE_MASK(isc))) {
173 flic->nimm |= AIS_MODE_MASK(isc);
174 trace_qemu_s390_suppress_airq(isc, "Single-Interruption Mode",
175 "NO-Interruptions Mode");
178 return 0;
181 static void qemu_s390_flic_notify(uint32_t type)
183 CPUState *cs;
186 * We have to make all CPUs see CPU_INTERRUPT_HARD, so they might
187 * consider it. We will kick all running CPUs and only relevant
188 * sleeping ones.
190 CPU_FOREACH(cs) {
191 S390CPU *cpu = S390_CPU(cs);
193 cs->interrupt_request |= CPU_INTERRUPT_HARD;
195 /* ignore CPUs that are not sleeping */
196 if (s390_cpu_get_state(cpu) != S390_CPU_STATE_OPERATING &&
197 s390_cpu_get_state(cpu) != S390_CPU_STATE_LOAD) {
198 continue;
201 /* we always kick running CPUs for now, this is tricky */
202 if (cs->halted) {
203 /* don't check for subclasses, CPUs double check when waking up */
204 if (type & FLIC_PENDING_SERVICE) {
205 if (!(cpu->env.psw.mask & PSW_MASK_EXT)) {
206 continue;
208 } else if (type & FLIC_PENDING_IO) {
209 if (!(cpu->env.psw.mask & PSW_MASK_IO)) {
210 continue;
212 } else if (type & FLIC_PENDING_MCHK_CR) {
213 if (!(cpu->env.psw.mask & PSW_MASK_MCHECK)) {
214 continue;
218 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
222 uint32_t qemu_s390_flic_dequeue_service(QEMUS390FLICState *flic)
224 uint32_t tmp;
226 g_assert(qemu_mutex_iothread_locked());
227 g_assert(flic->pending & FLIC_PENDING_SERVICE);
228 tmp = flic->service_param;
229 flic->service_param = 0;
230 flic->pending &= ~FLIC_PENDING_SERVICE;
232 return tmp;
235 /* caller has to free the returned object */
236 QEMUS390FlicIO *qemu_s390_flic_dequeue_io(QEMUS390FLICState *flic, uint64_t cr6)
238 QEMUS390FlicIO *io;
239 uint8_t isc;
241 g_assert(qemu_mutex_iothread_locked());
242 if (!(flic->pending & CR6_TO_PENDING_IO(cr6))) {
243 return NULL;
246 for (isc = 0; isc < 8; isc++) {
247 if (QLIST_EMPTY(&flic->io[isc]) || !(cr6 & ISC_TO_ISC_BITS(isc))) {
248 continue;
250 io = QLIST_FIRST(&flic->io[isc]);
251 QLIST_REMOVE(io, next);
253 /* update our indicator bit */
254 if (QLIST_EMPTY(&flic->io[isc])) {
255 flic->pending &= ~ISC_TO_PENDING_IO(isc);
257 return io;
260 return NULL;
263 void qemu_s390_flic_dequeue_crw_mchk(QEMUS390FLICState *flic)
265 g_assert(qemu_mutex_iothread_locked());
266 g_assert(flic->pending & FLIC_PENDING_MCHK_CR);
267 flic->pending &= ~FLIC_PENDING_MCHK_CR;
270 static void qemu_s390_inject_service(S390FLICState *fs, uint32_t parm)
272 QEMUS390FLICState *flic = s390_get_qemu_flic(fs);
274 g_assert(qemu_mutex_iothread_locked());
275 /* multiplexing is good enough for sclp - kvm does it internally as well */
276 flic->service_param |= parm;
277 flic->pending |= FLIC_PENDING_SERVICE;
279 qemu_s390_flic_notify(FLIC_PENDING_SERVICE);
282 static void qemu_s390_inject_io(S390FLICState *fs, uint16_t subchannel_id,
283 uint16_t subchannel_nr, uint32_t io_int_parm,
284 uint32_t io_int_word)
286 const uint8_t isc = IO_INT_WORD_ISC(io_int_word);
287 QEMUS390FLICState *flic = s390_get_qemu_flic(fs);
288 QEMUS390FlicIO *io;
290 g_assert(qemu_mutex_iothread_locked());
291 io = g_new0(QEMUS390FlicIO, 1);
292 io->id = subchannel_id;
293 io->nr = subchannel_nr;
294 io->parm = io_int_parm;
295 io->word = io_int_word;
297 QLIST_INSERT_HEAD(&flic->io[isc], io, next);
298 flic->pending |= ISC_TO_PENDING_IO(isc);
300 qemu_s390_flic_notify(ISC_TO_PENDING_IO(isc));
303 static void qemu_s390_inject_crw_mchk(S390FLICState *fs)
305 QEMUS390FLICState *flic = s390_get_qemu_flic(fs);
307 g_assert(qemu_mutex_iothread_locked());
308 flic->pending |= FLIC_PENDING_MCHK_CR;
310 qemu_s390_flic_notify(FLIC_PENDING_MCHK_CR);
313 bool qemu_s390_flic_has_service(QEMUS390FLICState *flic)
315 /* called without lock via cc->has_work, will be validated under lock */
316 return !!(flic->pending & FLIC_PENDING_SERVICE);
319 bool qemu_s390_flic_has_io(QEMUS390FLICState *flic, uint64_t cr6)
321 /* called without lock via cc->has_work, will be validated under lock */
322 return !!(flic->pending & CR6_TO_PENDING_IO(cr6));
325 bool qemu_s390_flic_has_crw_mchk(QEMUS390FLICState *flic)
327 /* called without lock via cc->has_work, will be validated under lock */
328 return !!(flic->pending & FLIC_PENDING_MCHK_CR);
331 bool qemu_s390_flic_has_any(QEMUS390FLICState *flic)
333 g_assert(qemu_mutex_iothread_locked());
334 return !!flic->pending;
337 static void qemu_s390_flic_reset(DeviceState *dev)
339 QEMUS390FLICState *flic = QEMU_S390_FLIC(dev);
340 QEMUS390FlicIO *cur, *next;
341 int isc;
343 g_assert(qemu_mutex_iothread_locked());
344 flic->simm = 0;
345 flic->nimm = 0;
346 flic->pending = 0;
348 /* remove all pending io interrupts */
349 for (isc = 0; isc < 8; isc++) {
350 QLIST_FOREACH_SAFE(cur, &flic->io[isc], next, next) {
351 QLIST_REMOVE(cur, next);
352 g_free(cur);
357 bool ais_needed(void *opaque)
359 S390FLICState *s = opaque;
361 return s->ais_supported;
364 static const VMStateDescription qemu_s390_flic_vmstate = {
365 .name = "qemu-s390-flic",
366 .version_id = 1,
367 .minimum_version_id = 1,
368 .needed = ais_needed,
369 .fields = (VMStateField[]) {
370 VMSTATE_UINT8(simm, QEMUS390FLICState),
371 VMSTATE_UINT8(nimm, QEMUS390FLICState),
372 VMSTATE_END_OF_LIST()
376 static void qemu_s390_flic_instance_init(Object *obj)
378 QEMUS390FLICState *flic = QEMU_S390_FLIC(obj);
379 int isc;
381 for (isc = 0; isc < 8; isc++) {
382 QLIST_INIT(&flic->io[isc]);
386 static void qemu_s390_flic_class_init(ObjectClass *oc, void *data)
388 DeviceClass *dc = DEVICE_CLASS(oc);
389 S390FLICStateClass *fsc = S390_FLIC_COMMON_CLASS(oc);
391 dc->reset = qemu_s390_flic_reset;
392 dc->vmsd = &qemu_s390_flic_vmstate;
393 fsc->register_io_adapter = qemu_s390_register_io_adapter;
394 fsc->io_adapter_map = qemu_s390_io_adapter_map;
395 fsc->add_adapter_routes = qemu_s390_add_adapter_routes;
396 fsc->release_adapter_routes = qemu_s390_release_adapter_routes;
397 fsc->clear_io_irq = qemu_s390_clear_io_flic;
398 fsc->modify_ais_mode = qemu_s390_modify_ais_mode;
399 fsc->inject_airq = qemu_s390_inject_airq;
400 fsc->inject_service = qemu_s390_inject_service;
401 fsc->inject_io = qemu_s390_inject_io;
402 fsc->inject_crw_mchk = qemu_s390_inject_crw_mchk;
405 static Property s390_flic_common_properties[] = {
406 DEFINE_PROP_UINT32("adapter_routes_max_batch", S390FLICState,
407 adapter_routes_max_batch, ADAPTER_ROUTES_MAX_GSI),
408 DEFINE_PROP_END_OF_LIST(),
411 static void s390_flic_common_realize(DeviceState *dev, Error **errp)
413 S390FLICState *fs = S390_FLIC_COMMON(dev);
414 uint32_t max_batch = fs->adapter_routes_max_batch;
416 if (max_batch > ADAPTER_ROUTES_MAX_GSI) {
417 error_setg(errp, "flic property adapter_routes_max_batch too big"
418 " (%d > %d)", max_batch, ADAPTER_ROUTES_MAX_GSI);
419 return;
422 fs->ais_supported = s390_has_feat(S390_FEAT_ADAPTER_INT_SUPPRESSION);
425 static void s390_flic_class_init(ObjectClass *oc, void *data)
427 DeviceClass *dc = DEVICE_CLASS(oc);
429 device_class_set_props(dc, s390_flic_common_properties);
430 dc->realize = s390_flic_common_realize;
433 static const TypeInfo qemu_s390_flic_info = {
434 .name = TYPE_QEMU_S390_FLIC,
435 .parent = TYPE_S390_FLIC_COMMON,
436 .instance_size = sizeof(QEMUS390FLICState),
437 .instance_init = qemu_s390_flic_instance_init,
438 .class_init = qemu_s390_flic_class_init,
442 static const TypeInfo s390_flic_common_info = {
443 .name = TYPE_S390_FLIC_COMMON,
444 .parent = TYPE_SYS_BUS_DEVICE,
445 .instance_size = sizeof(S390FLICState),
446 .class_init = s390_flic_class_init,
447 .class_size = sizeof(S390FLICStateClass),
450 static void qemu_s390_flic_register_types(void)
452 type_register_static(&s390_flic_common_info);
453 type_register_static(&qemu_s390_flic_info);
456 type_init(qemu_s390_flic_register_types)
458 static bool adapter_info_so_needed(void *opaque)
460 return css_migration_enabled();
463 const VMStateDescription vmstate_adapter_info_so = {
464 .name = "s390_adapter_info/summary_offset",
465 .version_id = 1,
466 .minimum_version_id = 1,
467 .needed = adapter_info_so_needed,
468 .fields = (VMStateField[]) {
469 VMSTATE_UINT32(summary_offset, AdapterInfo),
470 VMSTATE_END_OF_LIST()
474 const VMStateDescription vmstate_adapter_info = {
475 .name = "s390_adapter_info",
476 .version_id = 1,
477 .minimum_version_id = 1,
478 .fields = (VMStateField[]) {
479 VMSTATE_UINT64(ind_offset, AdapterInfo),
481 * We do not have to migrate neither the id nor the addresses.
482 * The id is set by css_register_io_adapter and the addresses
483 * are set based on the IndAddr objects after those get mapped.
485 VMSTATE_END_OF_LIST()
487 .subsections = (const VMStateDescription * []) {
488 &vmstate_adapter_info_so,
489 NULL
493 const VMStateDescription vmstate_adapter_routes = {
495 .name = "s390_adapter_routes",
496 .version_id = 1,
497 .minimum_version_id = 1,
498 .fields = (VMStateField[]) {
499 VMSTATE_STRUCT(adapter, AdapterRoutes, 1, vmstate_adapter_info,
500 AdapterInfo),
501 VMSTATE_END_OF_LIST()