4 * handles SCLP event types
5 * - Signal Quiesce - system power down
6 * - ASCII Console Data - VT220 read and write
8 * Copyright IBM, Corp. 2012
11 * Heinz Graalfs <graalfs@de.ibm.com>
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.
18 #include "qemu/osdep.h"
19 #include "qapi/error.h"
20 #include "sysemu/sysemu.h"
22 #include "hw/s390x/sclp.h"
23 #include "hw/s390x/event-facility.h"
25 typedef struct SCLPEventsBus
{
29 struct SCLPEventFacility
{
30 SysBusDevice parent_obj
;
32 /* guest' receive mask */
33 unsigned int receive_mask
;
36 /* return true if any child has event pending set */
37 static bool event_pending(SCLPEventFacility
*ef
)
41 SCLPEventClass
*event_class
;
43 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
44 DeviceState
*qdev
= kid
->child
;
45 event
= DO_UPCAST(SCLPEvent
, qdev
, qdev
);
46 event_class
= SCLP_EVENT_GET_CLASS(event
);
47 if (event
->event_pending
&&
48 event_class
->get_send_mask() & ef
->receive_mask
) {
55 static unsigned int get_host_send_mask(SCLPEventFacility
*ef
)
59 SCLPEventClass
*child
;
63 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
64 DeviceState
*qdev
= kid
->child
;
65 child
= SCLP_EVENT_GET_CLASS((SCLPEvent
*) qdev
);
66 mask
|= child
->get_send_mask();
71 static unsigned int get_host_receive_mask(SCLPEventFacility
*ef
)
75 SCLPEventClass
*child
;
79 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
80 DeviceState
*qdev
= kid
->child
;
81 child
= SCLP_EVENT_GET_CLASS((SCLPEvent
*) qdev
);
82 mask
|= child
->get_receive_mask();
87 static uint16_t write_event_length_check(SCCB
*sccb
)
91 EventBufferHeader
*event
;
92 WriteEventData
*wed
= (WriteEventData
*) sccb
;
94 event
= (EventBufferHeader
*) &wed
->ebh
;
95 for (slen
= sccb_data_len(sccb
); slen
> 0; slen
-= elen
) {
96 elen
= be16_to_cpu(event
->length
);
97 if (elen
< sizeof(*event
) || elen
> slen
) {
98 return SCLP_RC_EVENT_BUFFER_SYNTAX_ERROR
;
100 event
= (void *) event
+ elen
;
103 return SCLP_RC_INCONSISTENT_LENGTHS
;
105 return SCLP_RC_NORMAL_COMPLETION
;
108 static uint16_t handle_write_event_buf(SCLPEventFacility
*ef
,
109 EventBufferHeader
*event_buf
, SCCB
*sccb
)
116 rc
= SCLP_RC_INVALID_FUNCTION
;
118 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
119 DeviceState
*qdev
= kid
->child
;
120 event
= (SCLPEvent
*) qdev
;
121 ec
= SCLP_EVENT_GET_CLASS(event
);
123 if (ec
->write_event_data
&&
124 ec
->can_handle_event(event_buf
->type
)) {
125 rc
= ec
->write_event_data(event
, event_buf
);
132 static uint16_t handle_sccb_write_events(SCLPEventFacility
*ef
, SCCB
*sccb
)
137 EventBufferHeader
*event_buf
;
138 WriteEventData
*wed
= (WriteEventData
*) sccb
;
140 event_buf
= &wed
->ebh
;
141 rc
= SCLP_RC_NORMAL_COMPLETION
;
143 /* loop over all contained event buffers */
144 for (slen
= sccb_data_len(sccb
); slen
> 0; slen
-= elen
) {
145 elen
= be16_to_cpu(event_buf
->length
);
147 /* in case of a previous error mark all trailing buffers
149 if (rc
!= SCLP_RC_NORMAL_COMPLETION
) {
150 event_buf
->flags
&= ~(SCLP_EVENT_BUFFER_ACCEPTED
);
152 rc
= handle_write_event_buf(ef
, event_buf
, sccb
);
154 event_buf
= (void *) event_buf
+ elen
;
159 static void write_event_data(SCLPEventFacility
*ef
, SCCB
*sccb
)
161 if (sccb
->h
.function_code
!= SCLP_FC_NORMAL_WRITE
) {
162 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_FUNCTION
);
165 if (be16_to_cpu(sccb
->h
.length
) < 8) {
166 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH
);
169 /* first do a sanity check of the write events */
170 sccb
->h
.response_code
= cpu_to_be16(write_event_length_check(sccb
));
172 /* if no early error, then execute */
173 if (sccb
->h
.response_code
== be16_to_cpu(SCLP_RC_NORMAL_COMPLETION
)) {
174 sccb
->h
.response_code
=
175 cpu_to_be16(handle_sccb_write_events(ef
, sccb
));
182 static uint16_t handle_sccb_read_events(SCLPEventFacility
*ef
, SCCB
*sccb
,
191 EventBufferHeader
*event_buf
;
192 ReadEventData
*red
= (ReadEventData
*) sccb
;
194 event_buf
= &red
->ebh
;
195 event_buf
->length
= 0;
196 slen
= sizeof(sccb
->data
);
198 rc
= SCLP_RC_NO_EVENT_BUFFERS_STORED
;
200 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
201 DeviceState
*qdev
= kid
->child
;
202 event
= (SCLPEvent
*) qdev
;
203 ec
= SCLP_EVENT_GET_CLASS(event
);
205 if (mask
& ec
->get_send_mask()) {
206 if (ec
->read_event_data(event
, event_buf
, &slen
)) {
207 elen
= be16_to_cpu(event_buf
->length
);
208 event_buf
= (EventBufferHeader
*) ((char *)event_buf
+ elen
);
209 rc
= SCLP_RC_NORMAL_COMPLETION
;
214 if (sccb
->h
.control_mask
[2] & SCLP_VARIABLE_LENGTH_RESPONSE
) {
215 /* architecture suggests to reset variable-length-response bit */
216 sccb
->h
.control_mask
[2] &= ~SCLP_VARIABLE_LENGTH_RESPONSE
;
217 /* with a new length value */
218 sccb
->h
.length
= cpu_to_be16(SCCB_SIZE
- slen
);
223 static void read_event_data(SCLPEventFacility
*ef
, SCCB
*sccb
)
225 unsigned int sclp_active_selection_mask
;
226 unsigned int sclp_cp_receive_mask
;
228 ReadEventData
*red
= (ReadEventData
*) sccb
;
230 if (be16_to_cpu(sccb
->h
.length
) != SCCB_SIZE
) {
231 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH
);
235 sclp_cp_receive_mask
= ef
->receive_mask
;
237 /* get active selection mask */
238 switch (sccb
->h
.function_code
) {
239 case SCLP_UNCONDITIONAL_READ
:
240 sclp_active_selection_mask
= sclp_cp_receive_mask
;
242 case SCLP_SELECTIVE_READ
:
243 sclp_active_selection_mask
= be32_to_cpu(red
->mask
);
244 if (!sclp_cp_receive_mask
||
245 (sclp_active_selection_mask
& ~sclp_cp_receive_mask
)) {
246 sccb
->h
.response_code
=
247 cpu_to_be16(SCLP_RC_INVALID_SELECTION_MASK
);
252 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_FUNCTION
);
255 sccb
->h
.response_code
= cpu_to_be16(
256 handle_sccb_read_events(ef
, sccb
, sclp_active_selection_mask
));
262 /* copy up to dst_len bytes and fill the rest of dst with zeroes */
263 static void copy_mask(uint8_t *dst
, uint8_t *src
, uint16_t dst_len
,
268 for (i
= 0; i
< dst_len
; i
++) {
269 dst
[i
] = i
< src_len
? src
[i
] : 0;
273 static void write_event_mask(SCLPEventFacility
*ef
, SCCB
*sccb
)
275 WriteEventMask
*we_mask
= (WriteEventMask
*) sccb
;
276 uint16_t mask_length
= be16_to_cpu(we_mask
->mask_length
);
279 if (!mask_length
|| (mask_length
> SCLP_EVENT_MASK_LEN_MAX
)) {
280 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_MASK_LENGTH
);
285 * Note: We currently only support masks up to 4 byte length;
286 * the remainder is filled up with zeroes. Linux uses
287 * a 4 byte mask length.
290 /* keep track of the guest's capability masks */
291 copy_mask((uint8_t *)&tmp_mask
, WEM_CP_RECEIVE_MASK(we_mask
, mask_length
),
292 sizeof(tmp_mask
), mask_length
);
293 ef
->receive_mask
= be32_to_cpu(tmp_mask
);
295 /* return the SCLP's capability masks to the guest */
296 tmp_mask
= cpu_to_be32(get_host_send_mask(ef
));
297 copy_mask(WEM_RECEIVE_MASK(we_mask
, mask_length
), (uint8_t *)&tmp_mask
,
298 mask_length
, sizeof(tmp_mask
));
299 tmp_mask
= cpu_to_be32(get_host_receive_mask(ef
));
300 copy_mask(WEM_SEND_MASK(we_mask
, mask_length
), (uint8_t *)&tmp_mask
,
301 mask_length
, sizeof(tmp_mask
));
303 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_NORMAL_COMPLETION
);
309 /* qemu object creation and initialization functions */
311 #define TYPE_SCLP_EVENTS_BUS "s390-sclp-events-bus"
313 static void sclp_events_bus_realize(BusState
*bus
, Error
**errp
)
317 /* TODO: recursive realization has to be done in common code */
318 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
319 DeviceState
*dev
= kid
->child
;
321 object_property_set_bool(OBJECT(dev
), true, "realized", errp
);
328 static void sclp_events_bus_class_init(ObjectClass
*klass
, void *data
)
330 BusClass
*bc
= BUS_CLASS(klass
);
332 bc
->realize
= sclp_events_bus_realize
;
335 static const TypeInfo sclp_events_bus_info
= {
336 .name
= TYPE_SCLP_EVENTS_BUS
,
338 .class_init
= sclp_events_bus_class_init
,
341 static void command_handler(SCLPEventFacility
*ef
, SCCB
*sccb
, uint64_t code
)
343 switch (code
& SCLP_CMD_CODE_MASK
) {
344 case SCLP_CMD_READ_EVENT_DATA
:
345 read_event_data(ef
, sccb
);
347 case SCLP_CMD_WRITE_EVENT_DATA
:
348 write_event_data(ef
, sccb
);
350 case SCLP_CMD_WRITE_EVENT_MASK
:
351 write_event_mask(ef
, sccb
);
354 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND
);
359 static const VMStateDescription vmstate_event_facility
= {
360 .name
= "vmstate-event-facility",
362 .minimum_version_id
= 0,
363 .fields
= (VMStateField
[]) {
364 VMSTATE_UINT32(receive_mask
, SCLPEventFacility
),
365 VMSTATE_END_OF_LIST()
369 static void init_event_facility(Object
*obj
)
371 SCLPEventFacility
*event_facility
= EVENT_FACILITY(obj
);
372 DeviceState
*sdev
= DEVICE(obj
);
375 /* Spawn a new bus for SCLP events */
376 qbus_create_inplace(&event_facility
->sbus
, sizeof(event_facility
->sbus
),
377 TYPE_SCLP_EVENTS_BUS
, sdev
, NULL
);
379 new = object_new(TYPE_SCLP_QUIESCE
);
380 object_property_add_child(obj
, TYPE_SCLP_QUIESCE
, new, NULL
);
382 qdev_set_parent_bus(DEVICE(new), &event_facility
->sbus
.qbus
);
384 new = object_new(TYPE_SCLP_CPU_HOTPLUG
);
385 object_property_add_child(obj
, TYPE_SCLP_CPU_HOTPLUG
, new, NULL
);
387 qdev_set_parent_bus(DEVICE(new), &event_facility
->sbus
.qbus
);
388 /* the facility will automatically realize the devices via the bus */
391 static void reset_event_facility(DeviceState
*dev
)
393 SCLPEventFacility
*sdev
= EVENT_FACILITY(dev
);
395 sdev
->receive_mask
= 0;
398 static void init_event_facility_class(ObjectClass
*klass
, void *data
)
400 SysBusDeviceClass
*sbdc
= SYS_BUS_DEVICE_CLASS(klass
);
401 DeviceClass
*dc
= DEVICE_CLASS(sbdc
);
402 SCLPEventFacilityClass
*k
= EVENT_FACILITY_CLASS(dc
);
404 dc
->reset
= reset_event_facility
;
405 dc
->vmsd
= &vmstate_event_facility
;
406 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
407 k
->command_handler
= command_handler
;
408 k
->event_pending
= event_pending
;
411 static const TypeInfo sclp_event_facility_info
= {
412 .name
= TYPE_SCLP_EVENT_FACILITY
,
413 .parent
= TYPE_SYS_BUS_DEVICE
,
414 .instance_init
= init_event_facility
,
415 .instance_size
= sizeof(SCLPEventFacility
),
416 .class_init
= init_event_facility_class
,
417 .class_size
= sizeof(SCLPEventFacilityClass
),
420 static void event_realize(DeviceState
*qdev
, Error
**errp
)
422 SCLPEvent
*event
= SCLP_EVENT(qdev
);
423 SCLPEventClass
*child
= SCLP_EVENT_GET_CLASS(event
);
426 int rc
= child
->init(event
);
428 error_setg(errp
, "SCLP event initialization failed.");
434 static void event_unrealize(DeviceState
*qdev
, Error
**errp
)
436 SCLPEvent
*event
= SCLP_EVENT(qdev
);
437 SCLPEventClass
*child
= SCLP_EVENT_GET_CLASS(event
);
439 int rc
= child
->exit(event
);
441 error_setg(errp
, "SCLP event exit failed.");
447 static void event_class_init(ObjectClass
*klass
, void *data
)
449 DeviceClass
*dc
= DEVICE_CLASS(klass
);
451 dc
->bus_type
= TYPE_SCLP_EVENTS_BUS
;
452 dc
->realize
= event_realize
;
453 dc
->unrealize
= event_unrealize
;
456 static const TypeInfo sclp_event_type_info
= {
457 .name
= TYPE_SCLP_EVENT
,
458 .parent
= TYPE_DEVICE
,
459 .instance_size
= sizeof(SCLPEvent
),
460 .class_init
= event_class_init
,
461 .class_size
= sizeof(SCLPEventClass
),
465 static void register_types(void)
467 type_register_static(&sclp_events_bus_info
);
468 type_register_static(&sclp_event_facility_info
);
469 type_register_static(&sclp_event_type_info
);
472 type_init(register_types
)