]> git.proxmox.com Git - mirror_qemu.git/blob - hw/s390x/event-facility.c
Include migration/vmstate.h less
[mirror_qemu.git] / hw / s390x / event-facility.c
1 /*
2 * SCLP
3 * Event Facility
4 * handles SCLP event types
5 * - Signal Quiesce - system power down
6 * - ASCII Console Data - VT220 read and write
7 *
8 * Copyright IBM, Corp. 2012
9 *
10 * Authors:
11 * Heinz Graalfs <graalfs@de.ibm.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2 or (at your
14 * option) any later version. See the COPYING file in the top-level directory.
15 *
16 */
17
18 #include "qemu/osdep.h"
19 #include "qapi/error.h"
20 #include "qemu/module.h"
21 #include "sysemu/sysemu.h"
22
23 #include "hw/s390x/sclp.h"
24 #include "migration/vmstate.h"
25 #include "hw/s390x/event-facility.h"
26
27 typedef struct SCLPEventsBus {
28 BusState qbus;
29 } SCLPEventsBus;
30
31 /* we need to save 32 bit chunks for compatibility */
32 #ifdef HOST_WORDS_BIGENDIAN
33 #define RECV_MASK_LOWER 1
34 #define RECV_MASK_UPPER 0
35 #else /* little endian host */
36 #define RECV_MASK_LOWER 0
37 #define RECV_MASK_UPPER 1
38 #endif
39
40 struct SCLPEventFacility {
41 SysBusDevice parent_obj;
42 SCLPEventsBus sbus;
43 /* guest's receive mask */
44 union {
45 uint32_t receive_mask_pieces[2];
46 sccb_mask_t receive_mask;
47 };
48 /*
49 * when false, we keep the same broken, backwards compatible behaviour as
50 * before, allowing only masks of size exactly 4; when true, we implement
51 * the architecture correctly, allowing all valid mask sizes. Needed for
52 * migration toward older versions.
53 */
54 bool allow_all_mask_sizes;
55 /* length of the receive mask */
56 uint16_t mask_length;
57 };
58
59 /* return true if any child has event pending set */
60 static bool event_pending(SCLPEventFacility *ef)
61 {
62 BusChild *kid;
63 SCLPEvent *event;
64 SCLPEventClass *event_class;
65
66 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
67 DeviceState *qdev = kid->child;
68 event = DO_UPCAST(SCLPEvent, qdev, qdev);
69 event_class = SCLP_EVENT_GET_CLASS(event);
70 if (event->event_pending &&
71 event_class->get_send_mask() & ef->receive_mask) {
72 return true;
73 }
74 }
75 return false;
76 }
77
78 static sccb_mask_t get_host_send_mask(SCLPEventFacility *ef)
79 {
80 sccb_mask_t mask;
81 BusChild *kid;
82 SCLPEventClass *child;
83
84 mask = 0;
85
86 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
87 DeviceState *qdev = kid->child;
88 child = SCLP_EVENT_GET_CLASS((SCLPEvent *) qdev);
89 mask |= child->get_send_mask();
90 }
91 return mask;
92 }
93
94 static sccb_mask_t get_host_receive_mask(SCLPEventFacility *ef)
95 {
96 sccb_mask_t mask;
97 BusChild *kid;
98 SCLPEventClass *child;
99
100 mask = 0;
101
102 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
103 DeviceState *qdev = kid->child;
104 child = SCLP_EVENT_GET_CLASS((SCLPEvent *) qdev);
105 mask |= child->get_receive_mask();
106 }
107 return mask;
108 }
109
110 static uint16_t write_event_length_check(SCCB *sccb)
111 {
112 int slen;
113 unsigned elen = 0;
114 EventBufferHeader *event;
115 WriteEventData *wed = (WriteEventData *) sccb;
116
117 event = (EventBufferHeader *) &wed->ebh;
118 for (slen = sccb_data_len(sccb); slen > 0; slen -= elen) {
119 elen = be16_to_cpu(event->length);
120 if (elen < sizeof(*event) || elen > slen) {
121 return SCLP_RC_EVENT_BUFFER_SYNTAX_ERROR;
122 }
123 event = (void *) event + elen;
124 }
125 if (slen) {
126 return SCLP_RC_INCONSISTENT_LENGTHS;
127 }
128 return SCLP_RC_NORMAL_COMPLETION;
129 }
130
131 static uint16_t handle_write_event_buf(SCLPEventFacility *ef,
132 EventBufferHeader *event_buf, SCCB *sccb)
133 {
134 uint16_t rc;
135 BusChild *kid;
136 SCLPEvent *event;
137 SCLPEventClass *ec;
138
139 rc = SCLP_RC_INVALID_FUNCTION;
140
141 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
142 DeviceState *qdev = kid->child;
143 event = (SCLPEvent *) qdev;
144 ec = SCLP_EVENT_GET_CLASS(event);
145
146 if (ec->write_event_data &&
147 ec->can_handle_event(event_buf->type)) {
148 rc = ec->write_event_data(event, event_buf);
149 break;
150 }
151 }
152 return rc;
153 }
154
155 static uint16_t handle_sccb_write_events(SCLPEventFacility *ef, SCCB *sccb)
156 {
157 uint16_t rc;
158 int slen;
159 unsigned elen = 0;
160 EventBufferHeader *event_buf;
161 WriteEventData *wed = (WriteEventData *) sccb;
162
163 event_buf = &wed->ebh;
164 rc = SCLP_RC_NORMAL_COMPLETION;
165
166 /* loop over all contained event buffers */
167 for (slen = sccb_data_len(sccb); slen > 0; slen -= elen) {
168 elen = be16_to_cpu(event_buf->length);
169
170 /* in case of a previous error mark all trailing buffers
171 * as not accepted */
172 if (rc != SCLP_RC_NORMAL_COMPLETION) {
173 event_buf->flags &= ~(SCLP_EVENT_BUFFER_ACCEPTED);
174 } else {
175 rc = handle_write_event_buf(ef, event_buf, sccb);
176 }
177 event_buf = (void *) event_buf + elen;
178 }
179 return rc;
180 }
181
182 static void write_event_data(SCLPEventFacility *ef, SCCB *sccb)
183 {
184 if (sccb->h.function_code != SCLP_FC_NORMAL_WRITE) {
185 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_FUNCTION);
186 goto out;
187 }
188 if (be16_to_cpu(sccb->h.length) < 8) {
189 sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH);
190 goto out;
191 }
192 /* first do a sanity check of the write events */
193 sccb->h.response_code = cpu_to_be16(write_event_length_check(sccb));
194
195 /* if no early error, then execute */
196 if (sccb->h.response_code == be16_to_cpu(SCLP_RC_NORMAL_COMPLETION)) {
197 sccb->h.response_code =
198 cpu_to_be16(handle_sccb_write_events(ef, sccb));
199 }
200
201 out:
202 return;
203 }
204
205 static uint16_t handle_sccb_read_events(SCLPEventFacility *ef, SCCB *sccb,
206 sccb_mask_t mask)
207 {
208 uint16_t rc;
209 int slen;
210 unsigned elen;
211 BusChild *kid;
212 SCLPEvent *event;
213 SCLPEventClass *ec;
214 EventBufferHeader *event_buf;
215 ReadEventData *red = (ReadEventData *) sccb;
216
217 event_buf = &red->ebh;
218 event_buf->length = 0;
219 slen = sizeof(sccb->data);
220
221 rc = SCLP_RC_NO_EVENT_BUFFERS_STORED;
222
223 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
224 DeviceState *qdev = kid->child;
225 event = (SCLPEvent *) qdev;
226 ec = SCLP_EVENT_GET_CLASS(event);
227
228 if (mask & ec->get_send_mask()) {
229 if (ec->read_event_data(event, event_buf, &slen)) {
230 elen = be16_to_cpu(event_buf->length);
231 event_buf = (EventBufferHeader *) ((char *)event_buf + elen);
232 rc = SCLP_RC_NORMAL_COMPLETION;
233 }
234 }
235 }
236
237 if (sccb->h.control_mask[2] & SCLP_VARIABLE_LENGTH_RESPONSE) {
238 /* architecture suggests to reset variable-length-response bit */
239 sccb->h.control_mask[2] &= ~SCLP_VARIABLE_LENGTH_RESPONSE;
240 /* with a new length value */
241 sccb->h.length = cpu_to_be16(SCCB_SIZE - slen);
242 }
243 return rc;
244 }
245
246 /* copy up to src_len bytes and fill the rest of dst with zeroes */
247 static void copy_mask(uint8_t *dst, uint8_t *src, uint16_t dst_len,
248 uint16_t src_len)
249 {
250 int i;
251
252 for (i = 0; i < dst_len; i++) {
253 dst[i] = i < src_len ? src[i] : 0;
254 }
255 }
256
257 static void read_event_data(SCLPEventFacility *ef, SCCB *sccb)
258 {
259 sccb_mask_t sclp_active_selection_mask;
260 sccb_mask_t sclp_cp_receive_mask;
261
262 ReadEventData *red = (ReadEventData *) sccb;
263
264 if (be16_to_cpu(sccb->h.length) != SCCB_SIZE) {
265 sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH);
266 goto out;
267 }
268
269 sclp_cp_receive_mask = ef->receive_mask;
270
271 /* get active selection mask */
272 switch (sccb->h.function_code) {
273 case SCLP_UNCONDITIONAL_READ:
274 sclp_active_selection_mask = sclp_cp_receive_mask;
275 break;
276 case SCLP_SELECTIVE_READ:
277 copy_mask((uint8_t *)&sclp_active_selection_mask, (uint8_t *)&red->mask,
278 sizeof(sclp_active_selection_mask), ef->mask_length);
279 sclp_active_selection_mask = be64_to_cpu(sclp_active_selection_mask);
280 if (!sclp_cp_receive_mask ||
281 (sclp_active_selection_mask & ~sclp_cp_receive_mask)) {
282 sccb->h.response_code =
283 cpu_to_be16(SCLP_RC_INVALID_SELECTION_MASK);
284 goto out;
285 }
286 break;
287 default:
288 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_FUNCTION);
289 goto out;
290 }
291 sccb->h.response_code = cpu_to_be16(
292 handle_sccb_read_events(ef, sccb, sclp_active_selection_mask));
293
294 out:
295 return;
296 }
297
298 static void write_event_mask(SCLPEventFacility *ef, SCCB *sccb)
299 {
300 WriteEventMask *we_mask = (WriteEventMask *) sccb;
301 uint16_t mask_length = be16_to_cpu(we_mask->mask_length);
302 sccb_mask_t tmp_mask;
303
304 if (!mask_length || (mask_length > SCLP_EVENT_MASK_LEN_MAX) ||
305 ((mask_length != 4) && !ef->allow_all_mask_sizes)) {
306 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_MASK_LENGTH);
307 goto out;
308 }
309
310 /*
311 * Note: We currently only support masks up to 8 byte length;
312 * the remainder is filled up with zeroes. Older Linux
313 * kernels use a 4 byte mask length, newer ones can use both
314 * 8 or 4 depending on what is available on the host.
315 */
316
317 /* keep track of the guest's capability masks */
318 copy_mask((uint8_t *)&tmp_mask, WEM_CP_RECEIVE_MASK(we_mask, mask_length),
319 sizeof(tmp_mask), mask_length);
320 ef->receive_mask = be64_to_cpu(tmp_mask);
321
322 /* return the SCLP's capability masks to the guest */
323 tmp_mask = cpu_to_be64(get_host_receive_mask(ef));
324 copy_mask(WEM_RECEIVE_MASK(we_mask, mask_length), (uint8_t *)&tmp_mask,
325 mask_length, sizeof(tmp_mask));
326 tmp_mask = cpu_to_be64(get_host_send_mask(ef));
327 copy_mask(WEM_SEND_MASK(we_mask, mask_length), (uint8_t *)&tmp_mask,
328 mask_length, sizeof(tmp_mask));
329
330 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION);
331 ef->mask_length = mask_length;
332
333 out:
334 return;
335 }
336
337 /* qemu object creation and initialization functions */
338
339 #define TYPE_SCLP_EVENTS_BUS "s390-sclp-events-bus"
340
341 static void sclp_events_bus_realize(BusState *bus, Error **errp)
342 {
343 BusChild *kid;
344
345 /* TODO: recursive realization has to be done in common code */
346 QTAILQ_FOREACH(kid, &bus->children, sibling) {
347 DeviceState *dev = kid->child;
348
349 object_property_set_bool(OBJECT(dev), true, "realized", errp);
350 if (*errp) {
351 return;
352 }
353 }
354 }
355
356 static void sclp_events_bus_class_init(ObjectClass *klass, void *data)
357 {
358 BusClass *bc = BUS_CLASS(klass);
359
360 bc->realize = sclp_events_bus_realize;
361 }
362
363 static const TypeInfo sclp_events_bus_info = {
364 .name = TYPE_SCLP_EVENTS_BUS,
365 .parent = TYPE_BUS,
366 .class_init = sclp_events_bus_class_init,
367 };
368
369 static void command_handler(SCLPEventFacility *ef, SCCB *sccb, uint64_t code)
370 {
371 switch (code & SCLP_CMD_CODE_MASK) {
372 case SCLP_CMD_READ_EVENT_DATA:
373 read_event_data(ef, sccb);
374 break;
375 case SCLP_CMD_WRITE_EVENT_DATA:
376 write_event_data(ef, sccb);
377 break;
378 case SCLP_CMD_WRITE_EVENT_MASK:
379 write_event_mask(ef, sccb);
380 break;
381 default:
382 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
383 break;
384 }
385 }
386
387 static bool vmstate_event_facility_mask64_needed(void *opaque)
388 {
389 SCLPEventFacility *ef = opaque;
390
391 return (ef->receive_mask & 0xFFFFFFFF) != 0;
392 }
393
394 static bool vmstate_event_facility_mask_length_needed(void *opaque)
395 {
396 SCLPEventFacility *ef = opaque;
397
398 return ef->allow_all_mask_sizes;
399 }
400
401 static const VMStateDescription vmstate_event_facility_mask64 = {
402 .name = "vmstate-event-facility/mask64",
403 .version_id = 0,
404 .minimum_version_id = 0,
405 .needed = vmstate_event_facility_mask64_needed,
406 .fields = (VMStateField[]) {
407 VMSTATE_UINT32(receive_mask_pieces[RECV_MASK_LOWER], SCLPEventFacility),
408 VMSTATE_END_OF_LIST()
409 }
410 };
411
412 static const VMStateDescription vmstate_event_facility_mask_length = {
413 .name = "vmstate-event-facility/mask_length",
414 .version_id = 0,
415 .minimum_version_id = 0,
416 .needed = vmstate_event_facility_mask_length_needed,
417 .fields = (VMStateField[]) {
418 VMSTATE_UINT16(mask_length, SCLPEventFacility),
419 VMSTATE_END_OF_LIST()
420 }
421 };
422
423 static const VMStateDescription vmstate_event_facility = {
424 .name = "vmstate-event-facility",
425 .version_id = 0,
426 .minimum_version_id = 0,
427 .fields = (VMStateField[]) {
428 VMSTATE_UINT32(receive_mask_pieces[RECV_MASK_UPPER], SCLPEventFacility),
429 VMSTATE_END_OF_LIST()
430 },
431 .subsections = (const VMStateDescription * []) {
432 &vmstate_event_facility_mask64,
433 &vmstate_event_facility_mask_length,
434 NULL
435 }
436 };
437
438 static void sclp_event_set_allow_all_mask_sizes(Object *obj, bool value,
439 Error **errp)
440 {
441 SCLPEventFacility *ef = (SCLPEventFacility *)obj;
442
443 ef->allow_all_mask_sizes = value;
444 }
445
446 static bool sclp_event_get_allow_all_mask_sizes(Object *obj, Error **e)
447 {
448 SCLPEventFacility *ef = (SCLPEventFacility *)obj;
449
450 return ef->allow_all_mask_sizes;
451 }
452
453 static void init_event_facility(Object *obj)
454 {
455 SCLPEventFacility *event_facility = EVENT_FACILITY(obj);
456 DeviceState *sdev = DEVICE(obj);
457 Object *new;
458
459 event_facility->mask_length = 4;
460 event_facility->allow_all_mask_sizes = true;
461 object_property_add_bool(obj, "allow_all_mask_sizes",
462 sclp_event_get_allow_all_mask_sizes,
463 sclp_event_set_allow_all_mask_sizes, NULL);
464 /* Spawn a new bus for SCLP events */
465 qbus_create_inplace(&event_facility->sbus, sizeof(event_facility->sbus),
466 TYPE_SCLP_EVENTS_BUS, sdev, NULL);
467
468 new = object_new(TYPE_SCLP_QUIESCE);
469 object_property_add_child(obj, TYPE_SCLP_QUIESCE, new, NULL);
470 object_unref(new);
471 qdev_set_parent_bus(DEVICE(new), BUS(&event_facility->sbus));
472
473 new = object_new(TYPE_SCLP_CPU_HOTPLUG);
474 object_property_add_child(obj, TYPE_SCLP_CPU_HOTPLUG, new, NULL);
475 object_unref(new);
476 qdev_set_parent_bus(DEVICE(new), BUS(&event_facility->sbus));
477 /* the facility will automatically realize the devices via the bus */
478 }
479
480 static void reset_event_facility(DeviceState *dev)
481 {
482 SCLPEventFacility *sdev = EVENT_FACILITY(dev);
483
484 sdev->receive_mask = 0;
485 }
486
487 static void init_event_facility_class(ObjectClass *klass, void *data)
488 {
489 SysBusDeviceClass *sbdc = SYS_BUS_DEVICE_CLASS(klass);
490 DeviceClass *dc = DEVICE_CLASS(sbdc);
491 SCLPEventFacilityClass *k = EVENT_FACILITY_CLASS(dc);
492
493 dc->reset = reset_event_facility;
494 dc->vmsd = &vmstate_event_facility;
495 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
496 k->command_handler = command_handler;
497 k->event_pending = event_pending;
498 }
499
500 static const TypeInfo sclp_event_facility_info = {
501 .name = TYPE_SCLP_EVENT_FACILITY,
502 .parent = TYPE_SYS_BUS_DEVICE,
503 .instance_init = init_event_facility,
504 .instance_size = sizeof(SCLPEventFacility),
505 .class_init = init_event_facility_class,
506 .class_size = sizeof(SCLPEventFacilityClass),
507 };
508
509 static void event_realize(DeviceState *qdev, Error **errp)
510 {
511 SCLPEvent *event = SCLP_EVENT(qdev);
512 SCLPEventClass *child = SCLP_EVENT_GET_CLASS(event);
513
514 if (child->init) {
515 int rc = child->init(event);
516 if (rc < 0) {
517 error_setg(errp, "SCLP event initialization failed.");
518 return;
519 }
520 }
521 }
522
523 static void event_class_init(ObjectClass *klass, void *data)
524 {
525 DeviceClass *dc = DEVICE_CLASS(klass);
526
527 dc->bus_type = TYPE_SCLP_EVENTS_BUS;
528 dc->realize = event_realize;
529 }
530
531 static const TypeInfo sclp_event_type_info = {
532 .name = TYPE_SCLP_EVENT,
533 .parent = TYPE_DEVICE,
534 .instance_size = sizeof(SCLPEvent),
535 .class_init = event_class_init,
536 .class_size = sizeof(SCLPEventClass),
537 .abstract = true,
538 };
539
540 static void register_types(void)
541 {
542 type_register_static(&sclp_events_bus_info);
543 type_register_static(&sclp_event_facility_info);
544 type_register_static(&sclp_event_type_info);
545 }
546
547 type_init(register_types)
548
549 BusState *sclp_get_event_facility_bus(void)
550 {
551 Object *busobj;
552 SCLPEventsBus *sbus;
553
554 busobj = object_resolve_path_type("", TYPE_SCLP_EVENTS_BUS, NULL);
555 sbus = OBJECT_CHECK(SCLPEventsBus, busobj, TYPE_SCLP_EVENTS_BUS);
556 if (!sbus) {
557 return NULL;
558 }
559
560 return &sbus->qbus;
561 }