hw/arm/raspi: Make machines children of abstract RaspiMachineClass
[qemu/ar7.git] / hw / intc / s390_flic.c
blob5f290f54102a3c403fc6b039938e3ff98bea5787
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 "cpu.h"
24 #include "qapi/error.h"
25 #include "hw/s390x/s390-virtio-ccw.h"
27 S390FLICStateClass *s390_get_flic_class(S390FLICState *fs)
29 static S390FLICStateClass *class;
31 if (!class) {
32 /* we only have one flic device, so this is fine to cache */
33 class = S390_FLIC_COMMON_GET_CLASS(fs);
35 return class;
38 QEMUS390FLICState *s390_get_qemu_flic(S390FLICState *fs)
40 static QEMUS390FLICState *flic;
42 if (!flic) {
43 /* we only have one flic device, so this is fine to cache */
44 flic = QEMU_S390_FLIC(fs);
46 return flic;
49 S390FLICState *s390_get_flic(void)
51 static S390FLICState *fs;
53 if (!fs) {
54 fs = S390_FLIC_COMMON(object_resolve_path_type("",
55 TYPE_S390_FLIC_COMMON,
56 NULL));
58 return fs;
61 void s390_flic_init(void)
63 DeviceState *dev;
65 if (kvm_enabled()) {
66 dev = qdev_create(NULL, TYPE_KVM_S390_FLIC);
67 object_property_add_child(qdev_get_machine(), TYPE_KVM_S390_FLIC,
68 OBJECT(dev), NULL);
69 } else {
70 dev = qdev_create(NULL, TYPE_QEMU_S390_FLIC);
71 object_property_add_child(qdev_get_machine(), TYPE_QEMU_S390_FLIC,
72 OBJECT(dev), NULL);
74 qdev_init_nofail(dev);
77 static int qemu_s390_register_io_adapter(S390FLICState *fs, uint32_t id,
78 uint8_t isc, bool swap,
79 bool is_maskable, uint8_t flags)
81 /* nothing to do */
82 return 0;
85 static int qemu_s390_io_adapter_map(S390FLICState *fs, uint32_t id,
86 uint64_t map_addr, bool do_map)
88 /* nothing to do */
89 return 0;
92 static int qemu_s390_add_adapter_routes(S390FLICState *fs,
93 AdapterRoutes *routes)
95 return -ENOSYS;
98 static void qemu_s390_release_adapter_routes(S390FLICState *fs,
99 AdapterRoutes *routes)
103 static int qemu_s390_clear_io_flic(S390FLICState *fs, uint16_t subchannel_id,
104 uint16_t subchannel_nr)
106 QEMUS390FLICState *flic = s390_get_qemu_flic(fs);
107 QEMUS390FlicIO *cur, *next;
108 uint8_t isc;
110 g_assert(qemu_mutex_iothread_locked());
111 if (!(flic->pending & FLIC_PENDING_IO)) {
112 return 0;
115 /* check all iscs */
116 for (isc = 0; isc < 8; isc++) {
117 if (QLIST_EMPTY(&flic->io[isc])) {
118 continue;
121 /* search and delete any matching one */
122 QLIST_FOREACH_SAFE(cur, &flic->io[isc], next, next) {
123 if (cur->id == subchannel_id && cur->nr == subchannel_nr) {
124 QLIST_REMOVE(cur, next);
125 g_free(cur);
129 /* update our indicator bit */
130 if (QLIST_EMPTY(&flic->io[isc])) {
131 flic->pending &= ~ISC_TO_PENDING_IO(isc);
134 return 0;
137 static int qemu_s390_modify_ais_mode(S390FLICState *fs, uint8_t isc,
138 uint16_t mode)
140 QEMUS390FLICState *flic = s390_get_qemu_flic(fs);
142 switch (mode) {
143 case SIC_IRQ_MODE_ALL:
144 flic->simm &= ~AIS_MODE_MASK(isc);
145 flic->nimm &= ~AIS_MODE_MASK(isc);
146 break;
147 case SIC_IRQ_MODE_SINGLE:
148 flic->simm |= AIS_MODE_MASK(isc);
149 flic->nimm &= ~AIS_MODE_MASK(isc);
150 break;
151 default:
152 return -EINVAL;
155 return 0;
158 static int qemu_s390_inject_airq(S390FLICState *fs, uint8_t type,
159 uint8_t isc, uint8_t flags)
161 QEMUS390FLICState *flic = s390_get_qemu_flic(fs);
162 S390FLICStateClass *fsc = s390_get_flic_class(fs);
163 bool flag = flags & S390_ADAPTER_SUPPRESSIBLE;
164 uint32_t io_int_word = (isc << 27) | IO_INT_WORD_AI;
166 if (flag && (flic->nimm & AIS_MODE_MASK(isc))) {
167 trace_qemu_s390_airq_suppressed(type, isc);
168 return 0;
171 fsc->inject_io(fs, 0, 0, 0, io_int_word);
173 if (flag && (flic->simm & AIS_MODE_MASK(isc))) {
174 flic->nimm |= AIS_MODE_MASK(isc);
175 trace_qemu_s390_suppress_airq(isc, "Single-Interruption Mode",
176 "NO-Interruptions Mode");
179 return 0;
182 static void qemu_s390_flic_notify(uint32_t type)
184 CPUState *cs;
187 * We have to make all CPUs see CPU_INTERRUPT_HARD, so they might
188 * consider it. We will kick all running CPUs and only relevant
189 * sleeping ones.
191 CPU_FOREACH(cs) {
192 S390CPU *cpu = S390_CPU(cs);
194 cs->interrupt_request |= CPU_INTERRUPT_HARD;
196 /* ignore CPUs that are not sleeping */
197 if (s390_cpu_get_state(cpu) != S390_CPU_STATE_OPERATING &&
198 s390_cpu_get_state(cpu) != S390_CPU_STATE_LOAD) {
199 continue;
202 /* we always kick running CPUs for now, this is tricky */
203 if (cs->halted) {
204 /* don't check for subclasses, CPUs double check when waking up */
205 if (type & FLIC_PENDING_SERVICE) {
206 if (!(cpu->env.psw.mask & PSW_MASK_EXT)) {
207 continue;
209 } else if (type & FLIC_PENDING_IO) {
210 if (!(cpu->env.psw.mask & PSW_MASK_IO)) {
211 continue;
213 } else if (type & FLIC_PENDING_MCHK_CR) {
214 if (!(cpu->env.psw.mask & PSW_MASK_MCHECK)) {
215 continue;
219 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
223 uint32_t qemu_s390_flic_dequeue_service(QEMUS390FLICState *flic)
225 uint32_t tmp;
227 g_assert(qemu_mutex_iothread_locked());
228 g_assert(flic->pending & FLIC_PENDING_SERVICE);
229 tmp = flic->service_param;
230 flic->service_param = 0;
231 flic->pending &= ~FLIC_PENDING_SERVICE;
233 return tmp;
236 /* caller has to free the returned object */
237 QEMUS390FlicIO *qemu_s390_flic_dequeue_io(QEMUS390FLICState *flic, uint64_t cr6)
239 QEMUS390FlicIO *io;
240 uint8_t isc;
242 g_assert(qemu_mutex_iothread_locked());
243 if (!(flic->pending & CR6_TO_PENDING_IO(cr6))) {
244 return NULL;
247 for (isc = 0; isc < 8; isc++) {
248 if (QLIST_EMPTY(&flic->io[isc]) || !(cr6 & ISC_TO_ISC_BITS(isc))) {
249 continue;
251 io = QLIST_FIRST(&flic->io[isc]);
252 QLIST_REMOVE(io, next);
254 /* update our indicator bit */
255 if (QLIST_EMPTY(&flic->io[isc])) {
256 flic->pending &= ~ISC_TO_PENDING_IO(isc);
258 return io;
261 return NULL;
264 void qemu_s390_flic_dequeue_crw_mchk(QEMUS390FLICState *flic)
266 g_assert(qemu_mutex_iothread_locked());
267 g_assert(flic->pending & FLIC_PENDING_MCHK_CR);
268 flic->pending &= ~FLIC_PENDING_MCHK_CR;
271 static void qemu_s390_inject_service(S390FLICState *fs, uint32_t parm)
273 QEMUS390FLICState *flic = s390_get_qemu_flic(fs);
275 g_assert(qemu_mutex_iothread_locked());
276 /* multiplexing is good enough for sclp - kvm does it internally as well */
277 flic->service_param |= parm;
278 flic->pending |= FLIC_PENDING_SERVICE;
280 qemu_s390_flic_notify(FLIC_PENDING_SERVICE);
283 static void qemu_s390_inject_io(S390FLICState *fs, uint16_t subchannel_id,
284 uint16_t subchannel_nr, uint32_t io_int_parm,
285 uint32_t io_int_word)
287 const uint8_t isc = IO_INT_WORD_ISC(io_int_word);
288 QEMUS390FLICState *flic = s390_get_qemu_flic(fs);
289 QEMUS390FlicIO *io;
291 g_assert(qemu_mutex_iothread_locked());
292 io = g_new0(QEMUS390FlicIO, 1);
293 io->id = subchannel_id;
294 io->nr = subchannel_nr;
295 io->parm = io_int_parm;
296 io->word = io_int_word;
298 QLIST_INSERT_HEAD(&flic->io[isc], io, next);
299 flic->pending |= ISC_TO_PENDING_IO(isc);
301 qemu_s390_flic_notify(ISC_TO_PENDING_IO(isc));
304 static void qemu_s390_inject_crw_mchk(S390FLICState *fs)
306 QEMUS390FLICState *flic = s390_get_qemu_flic(fs);
308 g_assert(qemu_mutex_iothread_locked());
309 flic->pending |= FLIC_PENDING_MCHK_CR;
311 qemu_s390_flic_notify(FLIC_PENDING_MCHK_CR);
314 bool qemu_s390_flic_has_service(QEMUS390FLICState *flic)
316 /* called without lock via cc->has_work, will be validated under lock */
317 return !!(flic->pending & FLIC_PENDING_SERVICE);
320 bool qemu_s390_flic_has_io(QEMUS390FLICState *flic, uint64_t cr6)
322 /* called without lock via cc->has_work, will be validated under lock */
323 return !!(flic->pending & CR6_TO_PENDING_IO(cr6));
326 bool qemu_s390_flic_has_crw_mchk(QEMUS390FLICState *flic)
328 /* called without lock via cc->has_work, will be validated under lock */
329 return !!(flic->pending & FLIC_PENDING_MCHK_CR);
332 bool qemu_s390_flic_has_any(QEMUS390FLICState *flic)
334 g_assert(qemu_mutex_iothread_locked());
335 return !!flic->pending;
338 static void qemu_s390_flic_reset(DeviceState *dev)
340 QEMUS390FLICState *flic = QEMU_S390_FLIC(dev);
341 QEMUS390FlicIO *cur, *next;
342 int isc;
344 g_assert(qemu_mutex_iothread_locked());
345 flic->simm = 0;
346 flic->nimm = 0;
347 flic->pending = 0;
349 /* remove all pending io interrupts */
350 for (isc = 0; isc < 8; isc++) {
351 QLIST_FOREACH_SAFE(cur, &flic->io[isc], next, next) {
352 QLIST_REMOVE(cur, next);
353 g_free(cur);
358 bool ais_needed(void *opaque)
360 S390FLICState *s = opaque;
362 return s->ais_supported;
365 static const VMStateDescription qemu_s390_flic_vmstate = {
366 .name = "qemu-s390-flic",
367 .version_id = 1,
368 .minimum_version_id = 1,
369 .needed = ais_needed,
370 .fields = (VMStateField[]) {
371 VMSTATE_UINT8(simm, QEMUS390FLICState),
372 VMSTATE_UINT8(nimm, QEMUS390FLICState),
373 VMSTATE_END_OF_LIST()
377 static void qemu_s390_flic_instance_init(Object *obj)
379 QEMUS390FLICState *flic = QEMU_S390_FLIC(obj);
380 int isc;
382 for (isc = 0; isc < 8; isc++) {
383 QLIST_INIT(&flic->io[isc]);
387 static void qemu_s390_flic_class_init(ObjectClass *oc, void *data)
389 DeviceClass *dc = DEVICE_CLASS(oc);
390 S390FLICStateClass *fsc = S390_FLIC_COMMON_CLASS(oc);
392 dc->reset = qemu_s390_flic_reset;
393 dc->vmsd = &qemu_s390_flic_vmstate;
394 fsc->register_io_adapter = qemu_s390_register_io_adapter;
395 fsc->io_adapter_map = qemu_s390_io_adapter_map;
396 fsc->add_adapter_routes = qemu_s390_add_adapter_routes;
397 fsc->release_adapter_routes = qemu_s390_release_adapter_routes;
398 fsc->clear_io_irq = qemu_s390_clear_io_flic;
399 fsc->modify_ais_mode = qemu_s390_modify_ais_mode;
400 fsc->inject_airq = qemu_s390_inject_airq;
401 fsc->inject_service = qemu_s390_inject_service;
402 fsc->inject_io = qemu_s390_inject_io;
403 fsc->inject_crw_mchk = qemu_s390_inject_crw_mchk;
406 static Property s390_flic_common_properties[] = {
407 DEFINE_PROP_UINT32("adapter_routes_max_batch", S390FLICState,
408 adapter_routes_max_batch, ADAPTER_ROUTES_MAX_GSI),
409 DEFINE_PROP_END_OF_LIST(),
412 static void s390_flic_common_realize(DeviceState *dev, Error **errp)
414 S390FLICState *fs = S390_FLIC_COMMON(dev);
415 uint32_t max_batch = fs->adapter_routes_max_batch;
417 if (max_batch > ADAPTER_ROUTES_MAX_GSI) {
418 error_setg(errp, "flic property adapter_routes_max_batch too big"
419 " (%d > %d)", max_batch, ADAPTER_ROUTES_MAX_GSI);
420 return;
423 fs->ais_supported = s390_has_feat(S390_FEAT_ADAPTER_INT_SUPPRESSION);
426 static void s390_flic_class_init(ObjectClass *oc, void *data)
428 DeviceClass *dc = DEVICE_CLASS(oc);
430 device_class_set_props(dc, s390_flic_common_properties);
431 dc->realize = s390_flic_common_realize;
434 static const TypeInfo qemu_s390_flic_info = {
435 .name = TYPE_QEMU_S390_FLIC,
436 .parent = TYPE_S390_FLIC_COMMON,
437 .instance_size = sizeof(QEMUS390FLICState),
438 .instance_init = qemu_s390_flic_instance_init,
439 .class_init = qemu_s390_flic_class_init,
443 static const TypeInfo s390_flic_common_info = {
444 .name = TYPE_S390_FLIC_COMMON,
445 .parent = TYPE_SYS_BUS_DEVICE,
446 .instance_size = sizeof(S390FLICState),
447 .class_init = s390_flic_class_init,
448 .class_size = sizeof(S390FLICStateClass),
451 static void qemu_s390_flic_register_types(void)
453 type_register_static(&s390_flic_common_info);
454 type_register_static(&qemu_s390_flic_info);
457 type_init(qemu_s390_flic_register_types)
459 static bool adapter_info_so_needed(void *opaque)
461 return css_migration_enabled();
464 const VMStateDescription vmstate_adapter_info_so = {
465 .name = "s390_adapter_info/summary_offset",
466 .version_id = 1,
467 .minimum_version_id = 1,
468 .needed = adapter_info_so_needed,
469 .fields = (VMStateField[]) {
470 VMSTATE_UINT32(summary_offset, AdapterInfo),
471 VMSTATE_END_OF_LIST()
475 const VMStateDescription vmstate_adapter_info = {
476 .name = "s390_adapter_info",
477 .version_id = 1,
478 .minimum_version_id = 1,
479 .fields = (VMStateField[]) {
480 VMSTATE_UINT64(ind_offset, AdapterInfo),
482 * We do not have to migrate neither the id nor the addresses.
483 * The id is set by css_register_io_adapter and the addresses
484 * are set based on the IndAddr objects after those get mapped.
486 VMSTATE_END_OF_LIST()
488 .subsections = (const VMStateDescription * []) {
489 &vmstate_adapter_info_so,
490 NULL
494 const VMStateDescription vmstate_adapter_routes = {
496 .name = "s390_adapter_routes",
497 .version_id = 1,
498 .minimum_version_id = 1,
499 .fields = (VMStateField[]) {
500 VMSTATE_STRUCT(adapter, AdapterRoutes, 1, vmstate_adapter_info,
501 AdapterInfo),
502 VMSTATE_END_OF_LIST()