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 "sysemu/sysemu.h"
21 #include "hw/s390x/sclp.h"
22 #include "hw/s390x/event-facility.h"
24 typedef struct SCLPEventsBus
{
28 struct SCLPEventFacility
{
29 SysBusDevice parent_obj
;
31 /* guest' receive mask */
32 unsigned int receive_mask
;
35 /* return true if any child has event pending set */
36 static bool event_pending(SCLPEventFacility
*ef
)
40 SCLPEventClass
*event_class
;
42 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
43 DeviceState
*qdev
= kid
->child
;
44 event
= DO_UPCAST(SCLPEvent
, qdev
, qdev
);
45 event_class
= SCLP_EVENT_GET_CLASS(event
);
46 if (event
->event_pending
&&
47 event_class
->get_send_mask() & ef
->receive_mask
) {
54 static unsigned int get_host_send_mask(SCLPEventFacility
*ef
)
58 SCLPEventClass
*child
;
62 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
63 DeviceState
*qdev
= kid
->child
;
64 child
= SCLP_EVENT_GET_CLASS((SCLPEvent
*) qdev
);
65 mask
|= child
->get_send_mask();
70 static unsigned int get_host_receive_mask(SCLPEventFacility
*ef
)
74 SCLPEventClass
*child
;
78 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
79 DeviceState
*qdev
= kid
->child
;
80 child
= SCLP_EVENT_GET_CLASS((SCLPEvent
*) qdev
);
81 mask
|= child
->get_receive_mask();
86 static uint16_t write_event_length_check(SCCB
*sccb
)
90 EventBufferHeader
*event
;
91 WriteEventData
*wed
= (WriteEventData
*) sccb
;
93 event
= (EventBufferHeader
*) &wed
->ebh
;
94 for (slen
= sccb_data_len(sccb
); slen
> 0; slen
-= elen
) {
95 elen
= be16_to_cpu(event
->length
);
96 if (elen
< sizeof(*event
) || elen
> slen
) {
97 return SCLP_RC_EVENT_BUFFER_SYNTAX_ERROR
;
99 event
= (void *) event
+ elen
;
102 return SCLP_RC_INCONSISTENT_LENGTHS
;
104 return SCLP_RC_NORMAL_COMPLETION
;
107 static uint16_t handle_write_event_buf(SCLPEventFacility
*ef
,
108 EventBufferHeader
*event_buf
, SCCB
*sccb
)
115 rc
= SCLP_RC_INVALID_FUNCTION
;
117 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
118 DeviceState
*qdev
= kid
->child
;
119 event
= (SCLPEvent
*) qdev
;
120 ec
= SCLP_EVENT_GET_CLASS(event
);
122 if (ec
->write_event_data
&&
123 ec
->can_handle_event(event_buf
->type
)) {
124 rc
= ec
->write_event_data(event
, event_buf
);
131 static uint16_t handle_sccb_write_events(SCLPEventFacility
*ef
, SCCB
*sccb
)
136 EventBufferHeader
*event_buf
;
137 WriteEventData
*wed
= (WriteEventData
*) sccb
;
139 event_buf
= &wed
->ebh
;
140 rc
= SCLP_RC_NORMAL_COMPLETION
;
142 /* loop over all contained event buffers */
143 for (slen
= sccb_data_len(sccb
); slen
> 0; slen
-= elen
) {
144 elen
= be16_to_cpu(event_buf
->length
);
146 /* in case of a previous error mark all trailing buffers
148 if (rc
!= SCLP_RC_NORMAL_COMPLETION
) {
149 event_buf
->flags
&= ~(SCLP_EVENT_BUFFER_ACCEPTED
);
151 rc
= handle_write_event_buf(ef
, event_buf
, sccb
);
153 event_buf
= (void *) event_buf
+ elen
;
158 static void write_event_data(SCLPEventFacility
*ef
, SCCB
*sccb
)
160 if (sccb
->h
.function_code
!= SCLP_FC_NORMAL_WRITE
) {
161 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_FUNCTION
);
164 if (be16_to_cpu(sccb
->h
.length
) < 8) {
165 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH
);
168 /* first do a sanity check of the write events */
169 sccb
->h
.response_code
= cpu_to_be16(write_event_length_check(sccb
));
171 /* if no early error, then execute */
172 if (sccb
->h
.response_code
== be16_to_cpu(SCLP_RC_NORMAL_COMPLETION
)) {
173 sccb
->h
.response_code
=
174 cpu_to_be16(handle_sccb_write_events(ef
, sccb
));
181 static uint16_t handle_sccb_read_events(SCLPEventFacility
*ef
, SCCB
*sccb
,
190 EventBufferHeader
*event_buf
;
191 ReadEventData
*red
= (ReadEventData
*) sccb
;
193 event_buf
= &red
->ebh
;
194 event_buf
->length
= 0;
195 slen
= sizeof(sccb
->data
);
197 rc
= SCLP_RC_NO_EVENT_BUFFERS_STORED
;
199 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
200 DeviceState
*qdev
= kid
->child
;
201 event
= (SCLPEvent
*) qdev
;
202 ec
= SCLP_EVENT_GET_CLASS(event
);
204 if (mask
& ec
->get_send_mask()) {
205 if (ec
->read_event_data(event
, event_buf
, &slen
)) {
206 elen
= be16_to_cpu(event_buf
->length
);
207 event_buf
= (EventBufferHeader
*) ((char *)event_buf
+ elen
);
208 rc
= SCLP_RC_NORMAL_COMPLETION
;
213 if (sccb
->h
.control_mask
[2] & SCLP_VARIABLE_LENGTH_RESPONSE
) {
214 /* architecture suggests to reset variable-length-response bit */
215 sccb
->h
.control_mask
[2] &= ~SCLP_VARIABLE_LENGTH_RESPONSE
;
216 /* with a new length value */
217 sccb
->h
.length
= cpu_to_be16(SCCB_SIZE
- slen
);
222 static void read_event_data(SCLPEventFacility
*ef
, SCCB
*sccb
)
224 unsigned int sclp_active_selection_mask
;
225 unsigned int sclp_cp_receive_mask
;
227 ReadEventData
*red
= (ReadEventData
*) sccb
;
229 if (be16_to_cpu(sccb
->h
.length
) != SCCB_SIZE
) {
230 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH
);
234 sclp_cp_receive_mask
= ef
->receive_mask
;
236 /* get active selection mask */
237 switch (sccb
->h
.function_code
) {
238 case SCLP_UNCONDITIONAL_READ
:
239 sclp_active_selection_mask
= sclp_cp_receive_mask
;
241 case SCLP_SELECTIVE_READ
:
242 sclp_active_selection_mask
= be32_to_cpu(red
->mask
);
243 if (!sclp_cp_receive_mask
||
244 (sclp_active_selection_mask
& ~sclp_cp_receive_mask
)) {
245 sccb
->h
.response_code
=
246 cpu_to_be16(SCLP_RC_INVALID_SELECTION_MASK
);
251 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_FUNCTION
);
254 sccb
->h
.response_code
= cpu_to_be16(
255 handle_sccb_read_events(ef
, sccb
, sclp_active_selection_mask
));
261 static void write_event_mask(SCLPEventFacility
*ef
, SCCB
*sccb
)
263 WriteEventMask
*we_mask
= (WriteEventMask
*) sccb
;
265 /* Attention: We assume that Linux uses 4-byte masks, what it actually
266 does. Architecture allows for masks of variable size, though */
267 if (be16_to_cpu(we_mask
->mask_length
) != 4) {
268 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_MASK_LENGTH
);
272 /* keep track of the guest's capability masks */
273 ef
->receive_mask
= be32_to_cpu(we_mask
->cp_receive_mask
);
275 /* return the SCLP's capability masks to the guest */
276 we_mask
->send_mask
= cpu_to_be32(get_host_send_mask(ef
));
277 we_mask
->receive_mask
= cpu_to_be32(get_host_receive_mask(ef
));
279 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_NORMAL_COMPLETION
);
285 /* qemu object creation and initialization functions */
287 #define TYPE_SCLP_EVENTS_BUS "s390-sclp-events-bus"
289 static void sclp_events_bus_realize(BusState
*bus
, Error
**errp
)
293 /* TODO: recursive realization has to be done in common code */
294 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
295 DeviceState
*dev
= kid
->child
;
297 object_property_set_bool(OBJECT(dev
), true, "realized", errp
);
304 static void sclp_events_bus_class_init(ObjectClass
*klass
, void *data
)
306 BusClass
*bc
= BUS_CLASS(klass
);
308 bc
->realize
= sclp_events_bus_realize
;
311 static const TypeInfo sclp_events_bus_info
= {
312 .name
= TYPE_SCLP_EVENTS_BUS
,
314 .class_init
= sclp_events_bus_class_init
,
317 static void command_handler(SCLPEventFacility
*ef
, SCCB
*sccb
, uint64_t code
)
319 switch (code
& SCLP_CMD_CODE_MASK
) {
320 case SCLP_CMD_READ_EVENT_DATA
:
321 read_event_data(ef
, sccb
);
323 case SCLP_CMD_WRITE_EVENT_DATA
:
324 write_event_data(ef
, sccb
);
326 case SCLP_CMD_WRITE_EVENT_MASK
:
327 write_event_mask(ef
, sccb
);
330 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND
);
335 static const VMStateDescription vmstate_event_facility
= {
336 .name
= "vmstate-event-facility",
338 .minimum_version_id
= 0,
339 .fields
= (VMStateField
[]) {
340 VMSTATE_UINT32(receive_mask
, SCLPEventFacility
),
341 VMSTATE_END_OF_LIST()
345 static void init_event_facility(Object
*obj
)
347 SCLPEventFacility
*event_facility
= EVENT_FACILITY(obj
);
348 DeviceState
*sdev
= DEVICE(obj
);
351 /* Spawn a new bus for SCLP events */
352 qbus_create_inplace(&event_facility
->sbus
, sizeof(event_facility
->sbus
),
353 TYPE_SCLP_EVENTS_BUS
, sdev
, NULL
);
355 new = object_new(TYPE_SCLP_QUIESCE
);
356 object_property_add_child(obj
, TYPE_SCLP_QUIESCE
, new, NULL
);
358 qdev_set_parent_bus(DEVICE(new), &event_facility
->sbus
.qbus
);
360 new = object_new(TYPE_SCLP_CPU_HOTPLUG
);
361 object_property_add_child(obj
, TYPE_SCLP_CPU_HOTPLUG
, new, NULL
);
363 qdev_set_parent_bus(DEVICE(new), &event_facility
->sbus
.qbus
);
364 /* the facility will automatically realize the devices via the bus */
367 static void reset_event_facility(DeviceState
*dev
)
369 SCLPEventFacility
*sdev
= EVENT_FACILITY(dev
);
371 sdev
->receive_mask
= 0;
374 static void init_event_facility_class(ObjectClass
*klass
, void *data
)
376 SysBusDeviceClass
*sbdc
= SYS_BUS_DEVICE_CLASS(klass
);
377 DeviceClass
*dc
= DEVICE_CLASS(sbdc
);
378 SCLPEventFacilityClass
*k
= EVENT_FACILITY_CLASS(dc
);
380 dc
->reset
= reset_event_facility
;
381 dc
->vmsd
= &vmstate_event_facility
;
382 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
383 k
->command_handler
= command_handler
;
384 k
->event_pending
= event_pending
;
387 static const TypeInfo sclp_event_facility_info
= {
388 .name
= TYPE_SCLP_EVENT_FACILITY
,
389 .parent
= TYPE_SYS_BUS_DEVICE
,
390 .instance_init
= init_event_facility
,
391 .instance_size
= sizeof(SCLPEventFacility
),
392 .class_init
= init_event_facility_class
,
393 .class_size
= sizeof(SCLPEventFacilityClass
),
396 static void event_realize(DeviceState
*qdev
, Error
**errp
)
398 SCLPEvent
*event
= SCLP_EVENT(qdev
);
399 SCLPEventClass
*child
= SCLP_EVENT_GET_CLASS(event
);
402 int rc
= child
->init(event
);
404 error_setg(errp
, "SCLP event initialization failed.");
410 static void event_unrealize(DeviceState
*qdev
, Error
**errp
)
412 SCLPEvent
*event
= SCLP_EVENT(qdev
);
413 SCLPEventClass
*child
= SCLP_EVENT_GET_CLASS(event
);
415 int rc
= child
->exit(event
);
417 error_setg(errp
, "SCLP event exit failed.");
423 static void event_class_init(ObjectClass
*klass
, void *data
)
425 DeviceClass
*dc
= DEVICE_CLASS(klass
);
427 dc
->bus_type
= TYPE_SCLP_EVENTS_BUS
;
428 dc
->realize
= event_realize
;
429 dc
->unrealize
= event_unrealize
;
432 static const TypeInfo sclp_event_type_info
= {
433 .name
= TYPE_SCLP_EVENT
,
434 .parent
= TYPE_DEVICE
,
435 .instance_size
= sizeof(SCLPEvent
),
436 .class_init
= event_class_init
,
437 .class_size
= sizeof(SCLPEventClass
),
441 static void register_types(void)
443 type_register_static(&sclp_events_bus_info
);
444 type_register_static(&sclp_event_facility_info
);
445 type_register_static(&sclp_event_type_info
);
448 type_init(register_types
)