xref: /openbmc/qemu/hw/intc/s390_flic.c (revision 6e0d8175d63afba248f7da09047d0fcedd1e953d)
1 /*
2  * QEMU S390x floating interrupt controller (flic)
3  *
4  * Copyright 2014 IBM Corp.
5  * Author(s): Jens Freimann <jfrei@linux.vnet.ibm.com>
6  *            Cornelia Huck <cornelia.huck@de.ibm.com>
7  *
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.
11  */
12 
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"
19 #include "trace.h"
20 #include "cpu.h"
21 #include "hw/qdev.h"
22 #include "qapi/error.h"
23 #include "hw/s390x/s390-virtio-ccw.h"
24 
25 S390FLICState *s390_get_flic(void)
26 {
27     static S390FLICState *fs;
28 
29     if (!fs) {
30         fs = S390_FLIC_COMMON(object_resolve_path_type("",
31                                                        TYPE_S390_FLIC_COMMON,
32                                                        NULL));
33     }
34     return fs;
35 }
36 
37 void s390_flic_init(void)
38 {
39     DeviceState *dev;
40 
41     if (kvm_enabled()) {
42         dev = qdev_create(NULL, TYPE_KVM_S390_FLIC);
43         object_property_add_child(qdev_get_machine(), TYPE_KVM_S390_FLIC,
44                                   OBJECT(dev), NULL);
45     } else {
46         dev = qdev_create(NULL, TYPE_QEMU_S390_FLIC);
47         object_property_add_child(qdev_get_machine(), TYPE_QEMU_S390_FLIC,
48                                   OBJECT(dev), NULL);
49     }
50     qdev_init_nofail(dev);
51 }
52 
53 static int qemu_s390_register_io_adapter(S390FLICState *fs, uint32_t id,
54                                          uint8_t isc, bool swap,
55                                          bool is_maskable, uint8_t flags)
56 {
57     /* nothing to do */
58     return 0;
59 }
60 
61 static int qemu_s390_io_adapter_map(S390FLICState *fs, uint32_t id,
62                                     uint64_t map_addr, bool do_map)
63 {
64     /* nothing to do */
65     return 0;
66 }
67 
68 static int qemu_s390_add_adapter_routes(S390FLICState *fs,
69                                         AdapterRoutes *routes)
70 {
71     return -ENOSYS;
72 }
73 
74 static void qemu_s390_release_adapter_routes(S390FLICState *fs,
75                                              AdapterRoutes *routes)
76 {
77 }
78 
79 static int qemu_s390_clear_io_flic(S390FLICState *fs, uint16_t subchannel_id,
80                            uint16_t subchannel_nr)
81 {
82     QEMUS390FLICState *flic  = QEMU_S390_FLIC(fs);
83     QEMUS390FlicIO *cur, *next;
84     uint8_t isc;
85 
86     g_assert(qemu_mutex_iothread_locked());
87     if (!(flic->pending & FLIC_PENDING_IO)) {
88         return 0;
89     }
90 
91     /* check all iscs */
92     for (isc = 0; isc < 8; isc++) {
93         if (QLIST_EMPTY(&flic->io[isc])) {
94             continue;
95         }
96 
97         /* search and delete any matching one */
98         QLIST_FOREACH_SAFE(cur, &flic->io[isc], next, next) {
99             if (cur->id == subchannel_id && cur->nr == subchannel_nr) {
100                 QLIST_REMOVE(cur, next);
101                 g_free(cur);
102             }
103         }
104 
105         /* update our indicator bit */
106         if (QLIST_EMPTY(&flic->io[isc])) {
107             flic->pending &= ~ISC_TO_PENDING_IO(isc);
108         }
109     }
110     return 0;
111 }
112 
113 static int qemu_s390_modify_ais_mode(S390FLICState *fs, uint8_t isc,
114                                      uint16_t mode)
115 {
116     QEMUS390FLICState *flic  = QEMU_S390_FLIC(fs);
117 
118     switch (mode) {
119     case SIC_IRQ_MODE_ALL:
120         flic->simm &= ~AIS_MODE_MASK(isc);
121         flic->nimm &= ~AIS_MODE_MASK(isc);
122         break;
123     case SIC_IRQ_MODE_SINGLE:
124         flic->simm |= AIS_MODE_MASK(isc);
125         flic->nimm &= ~AIS_MODE_MASK(isc);
126         break;
127     default:
128         return -EINVAL;
129     }
130 
131     return 0;
132 }
133 
134 static int qemu_s390_inject_airq(S390FLICState *fs, uint8_t type,
135                                  uint8_t isc, uint8_t flags)
136 {
137     QEMUS390FLICState *flic = QEMU_S390_FLIC(fs);
138     S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
139     bool flag = flags & S390_ADAPTER_SUPPRESSIBLE;
140     uint32_t io_int_word = (isc << 27) | IO_INT_WORD_AI;
141 
142     if (flag && (flic->nimm & AIS_MODE_MASK(isc))) {
143         trace_qemu_s390_airq_suppressed(type, isc);
144         return 0;
145     }
146 
147     fsc->inject_io(fs, 0, 0, 0, io_int_word);
148 
149     if (flag && (flic->simm & AIS_MODE_MASK(isc))) {
150         flic->nimm |= AIS_MODE_MASK(isc);
151         trace_qemu_s390_suppress_airq(isc, "Single-Interruption Mode",
152                                       "NO-Interruptions Mode");
153     }
154 
155     return 0;
156 }
157 
158 static void qemu_s390_flic_notify(uint32_t type)
159 {
160     CPUState *cs;
161 
162     /*
163      * We have to make all CPUs see CPU_INTERRUPT_HARD, so they might
164      * consider it. TODO: don't kick/wakeup all VCPUs but try to be
165      * smarter (using the interrupt type).
166      */
167     CPU_FOREACH(cs) {
168         cpu_interrupt(cs, CPU_INTERRUPT_HARD);
169     }
170 }
171 
172 uint32_t qemu_s390_flic_dequeue_service(QEMUS390FLICState *flic)
173 {
174     uint32_t tmp;
175 
176     g_assert(qemu_mutex_iothread_locked());
177     g_assert(flic->pending & FLIC_PENDING_SERVICE);
178     tmp = flic->service_param;
179     flic->service_param = 0;
180     flic->pending &= ~FLIC_PENDING_SERVICE;
181 
182     return tmp;
183 }
184 
185 /* caller has to free the returned object */
186 QEMUS390FlicIO *qemu_s390_flic_dequeue_io(QEMUS390FLICState *flic, uint64_t cr6)
187 {
188     QEMUS390FlicIO *io;
189     uint8_t isc;
190 
191     g_assert(qemu_mutex_iothread_locked());
192     if (!(flic->pending & CR6_TO_PENDING_IO(cr6))) {
193         return NULL;
194     }
195 
196     for (isc = 0; isc < 8; isc++) {
197         if (QLIST_EMPTY(&flic->io[isc]) || !(cr6 & ISC_TO_ISC_BITS(isc))) {
198             continue;
199         }
200         io = QLIST_FIRST(&flic->io[isc]);
201         QLIST_REMOVE(io, next);
202 
203         /* update our indicator bit */
204         if (QLIST_EMPTY(&flic->io[isc])) {
205             flic->pending &= ~ISC_TO_PENDING_IO(isc);
206         }
207         return io;
208     }
209 
210     return NULL;
211 }
212 
213 void qemu_s390_flic_dequeue_crw_mchk(QEMUS390FLICState *flic)
214 {
215     g_assert(qemu_mutex_iothread_locked());
216     g_assert(flic->pending & FLIC_PENDING_MCHK_CR);
217     flic->pending &= ~FLIC_PENDING_MCHK_CR;
218 }
219 
220 static void qemu_s390_inject_service(S390FLICState *fs, uint32_t parm)
221 {
222     QEMUS390FLICState *flic = QEMU_S390_FLIC(fs);
223 
224     g_assert(qemu_mutex_iothread_locked());
225     /* multiplexing is good enough for sclp - kvm does it internally as well */
226     flic->service_param |= parm;
227     flic->pending |= FLIC_PENDING_SERVICE;
228 
229     qemu_s390_flic_notify(FLIC_PENDING_SERVICE);
230 }
231 
232 static void qemu_s390_inject_io(S390FLICState *fs, uint16_t subchannel_id,
233                                 uint16_t subchannel_nr, uint32_t io_int_parm,
234                                 uint32_t io_int_word)
235 {
236     const uint8_t isc = IO_INT_WORD_ISC(io_int_word);
237     QEMUS390FLICState *flic = QEMU_S390_FLIC(fs);
238     QEMUS390FlicIO *io;
239 
240     g_assert(qemu_mutex_iothread_locked());
241     io = g_new0(QEMUS390FlicIO, 1);
242     io->id = subchannel_id;
243     io->nr = subchannel_nr;
244     io->parm = io_int_parm;
245     io->word = io_int_word;
246 
247     QLIST_INSERT_HEAD(&flic->io[isc], io, next);
248     flic->pending |= ISC_TO_PENDING_IO(isc);
249 
250     qemu_s390_flic_notify(ISC_TO_PENDING_IO(isc));
251 }
252 
253 static void qemu_s390_inject_crw_mchk(S390FLICState *fs)
254 {
255     QEMUS390FLICState *flic = QEMU_S390_FLIC(fs);
256 
257     g_assert(qemu_mutex_iothread_locked());
258     flic->pending |= FLIC_PENDING_MCHK_CR;
259 
260     qemu_s390_flic_notify(FLIC_PENDING_MCHK_CR);
261 }
262 
263 bool qemu_s390_flic_has_service(QEMUS390FLICState *flic)
264 {
265     /* called without lock via cc->has_work, will be validated under lock */
266     return !!(flic->pending & FLIC_PENDING_SERVICE);
267 }
268 
269 bool qemu_s390_flic_has_io(QEMUS390FLICState *flic, uint64_t cr6)
270 {
271     /* called without lock via cc->has_work, will be validated under lock */
272     return !!(flic->pending & CR6_TO_PENDING_IO(cr6));
273 }
274 
275 bool qemu_s390_flic_has_crw_mchk(QEMUS390FLICState *flic)
276 {
277     /* called without lock via cc->has_work, will be validated under lock */
278     return !!(flic->pending & FLIC_PENDING_MCHK_CR);
279 }
280 
281 bool qemu_s390_flic_has_any(QEMUS390FLICState *flic)
282 {
283     g_assert(qemu_mutex_iothread_locked());
284     return !!flic->pending;
285 }
286 
287 static void qemu_s390_flic_reset(DeviceState *dev)
288 {
289     QEMUS390FLICState *flic = QEMU_S390_FLIC(dev);
290     QEMUS390FlicIO *cur, *next;
291     int isc;
292 
293     g_assert(qemu_mutex_iothread_locked());
294     flic->simm = 0;
295     flic->nimm = 0;
296     flic->pending = 0;
297 
298     /* remove all pending io interrupts */
299     for (isc = 0; isc < 8; isc++) {
300         QLIST_FOREACH_SAFE(cur, &flic->io[isc], next, next) {
301             QLIST_REMOVE(cur, next);
302             g_free(cur);
303         }
304     }
305 }
306 
307 bool ais_needed(void *opaque)
308 {
309     S390FLICState *s = opaque;
310 
311     return s->ais_supported;
312 }
313 
314 static const VMStateDescription qemu_s390_flic_vmstate = {
315     .name = "qemu-s390-flic",
316     .version_id = 1,
317     .minimum_version_id = 1,
318     .needed = ais_needed,
319     .fields = (VMStateField[]) {
320         VMSTATE_UINT8(simm, QEMUS390FLICState),
321         VMSTATE_UINT8(nimm, QEMUS390FLICState),
322         VMSTATE_END_OF_LIST()
323     }
324 };
325 
326 static void qemu_s390_flic_instance_init(Object *obj)
327 {
328     QEMUS390FLICState *flic = QEMU_S390_FLIC(obj);
329     int isc;
330 
331     for (isc = 0; isc < 8; isc++) {
332         QLIST_INIT(&flic->io[isc]);
333     }
334 }
335 
336 static void qemu_s390_flic_class_init(ObjectClass *oc, void *data)
337 {
338     DeviceClass *dc = DEVICE_CLASS(oc);
339     S390FLICStateClass *fsc = S390_FLIC_COMMON_CLASS(oc);
340 
341     dc->reset = qemu_s390_flic_reset;
342     dc->vmsd = &qemu_s390_flic_vmstate;
343     fsc->register_io_adapter = qemu_s390_register_io_adapter;
344     fsc->io_adapter_map = qemu_s390_io_adapter_map;
345     fsc->add_adapter_routes = qemu_s390_add_adapter_routes;
346     fsc->release_adapter_routes = qemu_s390_release_adapter_routes;
347     fsc->clear_io_irq = qemu_s390_clear_io_flic;
348     fsc->modify_ais_mode = qemu_s390_modify_ais_mode;
349     fsc->inject_airq = qemu_s390_inject_airq;
350     fsc->inject_service = qemu_s390_inject_service;
351     fsc->inject_io = qemu_s390_inject_io;
352     fsc->inject_crw_mchk = qemu_s390_inject_crw_mchk;
353 }
354 
355 static Property s390_flic_common_properties[] = {
356     DEFINE_PROP_UINT32("adapter_routes_max_batch", S390FLICState,
357                        adapter_routes_max_batch, ADAPTER_ROUTES_MAX_GSI),
358     DEFINE_PROP_END_OF_LIST(),
359 };
360 
361 static void s390_flic_common_realize(DeviceState *dev, Error **errp)
362 {
363     S390FLICState *fs = S390_FLIC_COMMON(dev);
364     uint32_t max_batch = fs->adapter_routes_max_batch;
365 
366     if (max_batch > ADAPTER_ROUTES_MAX_GSI) {
367         error_setg(errp, "flic property adapter_routes_max_batch too big"
368                    " (%d > %d)", max_batch, ADAPTER_ROUTES_MAX_GSI);
369         return;
370     }
371 
372     fs->ais_supported = s390_has_feat(S390_FEAT_ADAPTER_INT_SUPPRESSION);
373 }
374 
375 static void s390_flic_class_init(ObjectClass *oc, void *data)
376 {
377     DeviceClass *dc = DEVICE_CLASS(oc);
378 
379     dc->props = s390_flic_common_properties;
380     dc->realize = s390_flic_common_realize;
381 }
382 
383 static const TypeInfo qemu_s390_flic_info = {
384     .name          = TYPE_QEMU_S390_FLIC,
385     .parent        = TYPE_S390_FLIC_COMMON,
386     .instance_size = sizeof(QEMUS390FLICState),
387     .instance_init = qemu_s390_flic_instance_init,
388     .class_init    = qemu_s390_flic_class_init,
389 };
390 
391 
392 static const TypeInfo s390_flic_common_info = {
393     .name          = TYPE_S390_FLIC_COMMON,
394     .parent        = TYPE_SYS_BUS_DEVICE,
395     .instance_size = sizeof(S390FLICState),
396     .class_init    = s390_flic_class_init,
397     .class_size    = sizeof(S390FLICStateClass),
398 };
399 
400 static void qemu_s390_flic_register_types(void)
401 {
402     type_register_static(&s390_flic_common_info);
403     type_register_static(&qemu_s390_flic_info);
404 }
405 
406 type_init(qemu_s390_flic_register_types)
407 
408 static bool adapter_info_so_needed(void *opaque)
409 {
410     return css_migration_enabled();
411 }
412 
413 const VMStateDescription vmstate_adapter_info_so = {
414     .name = "s390_adapter_info/summary_offset",
415     .version_id = 1,
416     .minimum_version_id = 1,
417     .needed = adapter_info_so_needed,
418     .fields = (VMStateField[]) {
419         VMSTATE_UINT32(summary_offset, AdapterInfo),
420         VMSTATE_END_OF_LIST()
421     }
422 };
423 
424 const VMStateDescription vmstate_adapter_info = {
425     .name = "s390_adapter_info",
426     .version_id = 1,
427     .minimum_version_id = 1,
428     .fields = (VMStateField[]) {
429         VMSTATE_UINT64(ind_offset, AdapterInfo),
430         /*
431          * We do not have to migrate neither the id nor the addresses.
432          * The id is set by css_register_io_adapter and the addresses
433          * are set based on the IndAddr objects after those get mapped.
434          */
435         VMSTATE_END_OF_LIST()
436     },
437     .subsections = (const VMStateDescription * []) {
438         &vmstate_adapter_info_so,
439         NULL
440     }
441 };
442 
443 const VMStateDescription vmstate_adapter_routes = {
444 
445     .name = "s390_adapter_routes",
446     .version_id = 1,
447     .minimum_version_id = 1,
448     .fields = (VMStateField[]) {
449         VMSTATE_STRUCT(adapter, AdapterRoutes, 1, vmstate_adapter_info,
450                        AdapterInfo),
451         VMSTATE_END_OF_LIST()
452     }
453 };
454