]>
Commit | Line | Data |
---|---|---|
559a17a1 HG |
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 <[email protected]> | |
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 | ||
9615495a | 18 | #include "qemu/osdep.h" |
da34e65c | 19 | #include "qapi/error.h" |
9c17d615 | 20 | #include "sysemu/sysemu.h" |
559a17a1 | 21 | |
83c9f4ca PB |
22 | #include "hw/s390x/sclp.h" |
23 | #include "hw/s390x/event-facility.h" | |
559a17a1 | 24 | |
65e526c2 | 25 | typedef struct SCLPEventsBus { |
559a17a1 | 26 | BusState qbus; |
65e526c2 | 27 | } SCLPEventsBus; |
559a17a1 HG |
28 | |
29 | struct SCLPEventFacility { | |
477a72a1 | 30 | SysBusDevice parent_obj; |
65e526c2 | 31 | SCLPEventsBus sbus; |
559a17a1 HG |
32 | /* guest' receive mask */ |
33 | unsigned int receive_mask; | |
34 | }; | |
35 | ||
36 | /* return true if any child has event pending set */ | |
37 | static bool event_pending(SCLPEventFacility *ef) | |
38 | { | |
39 | BusChild *kid; | |
40 | SCLPEvent *event; | |
41 | SCLPEventClass *event_class; | |
42 | ||
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) { | |
49 | return true; | |
50 | } | |
51 | } | |
52 | return false; | |
53 | } | |
54 | ||
55 | static unsigned int get_host_send_mask(SCLPEventFacility *ef) | |
56 | { | |
57 | unsigned int mask; | |
58 | BusChild *kid; | |
59 | SCLPEventClass *child; | |
60 | ||
61 | mask = 0; | |
62 | ||
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(); | |
67 | } | |
68 | return mask; | |
69 | } | |
70 | ||
71 | static unsigned int get_host_receive_mask(SCLPEventFacility *ef) | |
72 | { | |
73 | unsigned int mask; | |
74 | BusChild *kid; | |
75 | SCLPEventClass *child; | |
76 | ||
77 | mask = 0; | |
78 | ||
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(); | |
83 | } | |
84 | return mask; | |
85 | } | |
86 | ||
87 | static uint16_t write_event_length_check(SCCB *sccb) | |
88 | { | |
89 | int slen; | |
90 | unsigned elen = 0; | |
91 | EventBufferHeader *event; | |
92 | WriteEventData *wed = (WriteEventData *) sccb; | |
93 | ||
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; | |
99 | } | |
100 | event = (void *) event + elen; | |
101 | } | |
102 | if (slen) { | |
103 | return SCLP_RC_INCONSISTENT_LENGTHS; | |
104 | } | |
105 | return SCLP_RC_NORMAL_COMPLETION; | |
106 | } | |
107 | ||
108 | static uint16_t handle_write_event_buf(SCLPEventFacility *ef, | |
109 | EventBufferHeader *event_buf, SCCB *sccb) | |
110 | { | |
111 | uint16_t rc; | |
112 | BusChild *kid; | |
113 | SCLPEvent *event; | |
114 | SCLPEventClass *ec; | |
115 | ||
773de5c7 CH |
116 | rc = SCLP_RC_INVALID_FUNCTION; |
117 | ||
559a17a1 HG |
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); | |
122 | ||
559a17a1 | 123 | if (ec->write_event_data && |
c3d9f24a | 124 | ec->can_handle_event(event_buf->type)) { |
559a17a1 HG |
125 | rc = ec->write_event_data(event, event_buf); |
126 | break; | |
127 | } | |
128 | } | |
129 | return rc; | |
130 | } | |
131 | ||
132 | static uint16_t handle_sccb_write_events(SCLPEventFacility *ef, SCCB *sccb) | |
133 | { | |
134 | uint16_t rc; | |
135 | int slen; | |
136 | unsigned elen = 0; | |
137 | EventBufferHeader *event_buf; | |
138 | WriteEventData *wed = (WriteEventData *) sccb; | |
139 | ||
140 | event_buf = &wed->ebh; | |
141 | rc = SCLP_RC_NORMAL_COMPLETION; | |
142 | ||
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); | |
146 | ||
147 | /* in case of a previous error mark all trailing buffers | |
148 | * as not accepted */ | |
149 | if (rc != SCLP_RC_NORMAL_COMPLETION) { | |
150 | event_buf->flags &= ~(SCLP_EVENT_BUFFER_ACCEPTED); | |
151 | } else { | |
152 | rc = handle_write_event_buf(ef, event_buf, sccb); | |
153 | } | |
154 | event_buf = (void *) event_buf + elen; | |
155 | } | |
156 | return rc; | |
157 | } | |
158 | ||
159 | static void write_event_data(SCLPEventFacility *ef, SCCB *sccb) | |
160 | { | |
161 | if (sccb->h.function_code != SCLP_FC_NORMAL_WRITE) { | |
162 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_FUNCTION); | |
163 | goto out; | |
164 | } | |
165 | if (be16_to_cpu(sccb->h.length) < 8) { | |
166 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH); | |
167 | goto out; | |
168 | } | |
169 | /* first do a sanity check of the write events */ | |
170 | sccb->h.response_code = cpu_to_be16(write_event_length_check(sccb)); | |
171 | ||
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)); | |
176 | } | |
177 | ||
178 | out: | |
179 | return; | |
180 | } | |
181 | ||
182 | static uint16_t handle_sccb_read_events(SCLPEventFacility *ef, SCCB *sccb, | |
183 | unsigned int mask) | |
184 | { | |
185 | uint16_t rc; | |
186 | int slen; | |
a0c8699b | 187 | unsigned elen; |
559a17a1 HG |
188 | BusChild *kid; |
189 | SCLPEvent *event; | |
190 | SCLPEventClass *ec; | |
191 | EventBufferHeader *event_buf; | |
192 | ReadEventData *red = (ReadEventData *) sccb; | |
193 | ||
194 | event_buf = &red->ebh; | |
195 | event_buf->length = 0; | |
196 | slen = sizeof(sccb->data); | |
197 | ||
198 | rc = SCLP_RC_NO_EVENT_BUFFERS_STORED; | |
199 | ||
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); | |
204 | ||
205 | if (mask & ec->get_send_mask()) { | |
206 | if (ec->read_event_data(event, event_buf, &slen)) { | |
a0c8699b RH |
207 | elen = be16_to_cpu(event_buf->length); |
208 | event_buf = (EventBufferHeader *) ((char *)event_buf + elen); | |
559a17a1 HG |
209 | rc = SCLP_RC_NORMAL_COMPLETION; |
210 | } | |
211 | } | |
559a17a1 HG |
212 | } |
213 | ||
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); | |
219 | } | |
220 | return rc; | |
221 | } | |
222 | ||
223 | static void read_event_data(SCLPEventFacility *ef, SCCB *sccb) | |
224 | { | |
225 | unsigned int sclp_active_selection_mask; | |
226 | unsigned int sclp_cp_receive_mask; | |
227 | ||
228 | ReadEventData *red = (ReadEventData *) sccb; | |
229 | ||
230 | if (be16_to_cpu(sccb->h.length) != SCCB_SIZE) { | |
231 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH); | |
232 | goto out; | |
233 | } | |
234 | ||
235 | sclp_cp_receive_mask = ef->receive_mask; | |
236 | ||
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; | |
241 | break; | |
242 | case SCLP_SELECTIVE_READ: | |
3335dddd CH |
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)) { | |
559a17a1 HG |
246 | sccb->h.response_code = |
247 | cpu_to_be16(SCLP_RC_INVALID_SELECTION_MASK); | |
248 | goto out; | |
249 | } | |
559a17a1 HG |
250 | break; |
251 | default: | |
252 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_FUNCTION); | |
253 | goto out; | |
254 | } | |
255 | sccb->h.response_code = cpu_to_be16( | |
256 | handle_sccb_read_events(ef, sccb, sclp_active_selection_mask)); | |
257 | ||
258 | out: | |
259 | return; | |
260 | } | |
261 | ||
262 | static void write_event_mask(SCLPEventFacility *ef, SCCB *sccb) | |
263 | { | |
264 | WriteEventMask *we_mask = (WriteEventMask *) sccb; | |
265 | ||
266 | /* Attention: We assume that Linux uses 4-byte masks, what it actually | |
267 | does. Architecture allows for masks of variable size, though */ | |
268 | if (be16_to_cpu(we_mask->mask_length) != 4) { | |
269 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_MASK_LENGTH); | |
270 | goto out; | |
271 | } | |
272 | ||
273 | /* keep track of the guest's capability masks */ | |
274 | ef->receive_mask = be32_to_cpu(we_mask->cp_receive_mask); | |
275 | ||
276 | /* return the SCLP's capability masks to the guest */ | |
277 | we_mask->send_mask = cpu_to_be32(get_host_send_mask(ef)); | |
278 | we_mask->receive_mask = cpu_to_be32(get_host_receive_mask(ef)); | |
279 | ||
280 | sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION); | |
281 | ||
282 | out: | |
283 | return; | |
284 | } | |
285 | ||
286 | /* qemu object creation and initialization functions */ | |
287 | ||
288 | #define TYPE_SCLP_EVENTS_BUS "s390-sclp-events-bus" | |
289 | ||
f6102c32 DH |
290 | static void sclp_events_bus_realize(BusState *bus, Error **errp) |
291 | { | |
292 | BusChild *kid; | |
293 | ||
294 | /* TODO: recursive realization has to be done in common code */ | |
295 | QTAILQ_FOREACH(kid, &bus->children, sibling) { | |
296 | DeviceState *dev = kid->child; | |
297 | ||
298 | object_property_set_bool(OBJECT(dev), true, "realized", errp); | |
299 | if (*errp) { | |
300 | return; | |
301 | } | |
302 | } | |
303 | } | |
304 | ||
559a17a1 HG |
305 | static void sclp_events_bus_class_init(ObjectClass *klass, void *data) |
306 | { | |
f6102c32 DH |
307 | BusClass *bc = BUS_CLASS(klass); |
308 | ||
309 | bc->realize = sclp_events_bus_realize; | |
559a17a1 HG |
310 | } |
311 | ||
65e526c2 | 312 | static const TypeInfo sclp_events_bus_info = { |
559a17a1 HG |
313 | .name = TYPE_SCLP_EVENTS_BUS, |
314 | .parent = TYPE_BUS, | |
315 | .class_init = sclp_events_bus_class_init, | |
316 | }; | |
317 | ||
318 | static void command_handler(SCLPEventFacility *ef, SCCB *sccb, uint64_t code) | |
319 | { | |
9da45bb2 | 320 | switch (code & SCLP_CMD_CODE_MASK) { |
559a17a1 HG |
321 | case SCLP_CMD_READ_EVENT_DATA: |
322 | read_event_data(ef, sccb); | |
323 | break; | |
324 | case SCLP_CMD_WRITE_EVENT_DATA: | |
325 | write_event_data(ef, sccb); | |
326 | break; | |
327 | case SCLP_CMD_WRITE_EVENT_MASK: | |
328 | write_event_mask(ef, sccb); | |
329 | break; | |
330 | default: | |
331 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND); | |
332 | break; | |
333 | } | |
334 | } | |
335 | ||
6fbef18a HG |
336 | static const VMStateDescription vmstate_event_facility = { |
337 | .name = "vmstate-event-facility", | |
338 | .version_id = 0, | |
339 | .minimum_version_id = 0, | |
35d08458 | 340 | .fields = (VMStateField[]) { |
6fbef18a HG |
341 | VMSTATE_UINT32(receive_mask, SCLPEventFacility), |
342 | VMSTATE_END_OF_LIST() | |
343 | } | |
344 | }; | |
345 | ||
f6102c32 | 346 | static void init_event_facility(Object *obj) |
559a17a1 | 347 | { |
f6102c32 DH |
348 | SCLPEventFacility *event_facility = EVENT_FACILITY(obj); |
349 | DeviceState *sdev = DEVICE(obj); | |
7059384c | 350 | Object *new; |
559a17a1 | 351 | |
477a72a1 | 352 | /* Spawn a new bus for SCLP events */ |
fb17dfe0 | 353 | qbus_create_inplace(&event_facility->sbus, sizeof(event_facility->sbus), |
477a72a1 | 354 | TYPE_SCLP_EVENTS_BUS, sdev, NULL); |
559a17a1 | 355 | |
7059384c DH |
356 | new = object_new(TYPE_SCLP_QUIESCE); |
357 | object_property_add_child(obj, TYPE_SCLP_QUIESCE, new, NULL); | |
358 | object_unref(new); | |
359 | qdev_set_parent_bus(DEVICE(new), &event_facility->sbus.qbus); | |
360 | ||
361 | new = object_new(TYPE_SCLP_CPU_HOTPLUG); | |
362 | object_property_add_child(obj, TYPE_SCLP_CPU_HOTPLUG, new, NULL); | |
363 | object_unref(new); | |
364 | qdev_set_parent_bus(DEVICE(new), &event_facility->sbus.qbus); | |
f6102c32 | 365 | /* the facility will automatically realize the devices via the bus */ |
559a17a1 HG |
366 | } |
367 | ||
3af6de32 HG |
368 | static void reset_event_facility(DeviceState *dev) |
369 | { | |
477a72a1 | 370 | SCLPEventFacility *sdev = EVENT_FACILITY(dev); |
3af6de32 | 371 | |
477a72a1 | 372 | sdev->receive_mask = 0; |
3af6de32 HG |
373 | } |
374 | ||
559a17a1 HG |
375 | static void init_event_facility_class(ObjectClass *klass, void *data) |
376 | { | |
477a72a1 HG |
377 | SysBusDeviceClass *sbdc = SYS_BUS_DEVICE_CLASS(klass); |
378 | DeviceClass *dc = DEVICE_CLASS(sbdc); | |
379 | SCLPEventFacilityClass *k = EVENT_FACILITY_CLASS(dc); | |
559a17a1 | 380 | |
3af6de32 | 381 | dc->reset = reset_event_facility; |
6fbef18a | 382 | dc->vmsd = &vmstate_event_facility; |
183f6b8d | 383 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
477a72a1 HG |
384 | k->command_handler = command_handler; |
385 | k->event_pending = event_pending; | |
559a17a1 HG |
386 | } |
387 | ||
65e526c2 | 388 | static const TypeInfo sclp_event_facility_info = { |
477a72a1 HG |
389 | .name = TYPE_SCLP_EVENT_FACILITY, |
390 | .parent = TYPE_SYS_BUS_DEVICE, | |
f6102c32 | 391 | .instance_init = init_event_facility, |
477a72a1 | 392 | .instance_size = sizeof(SCLPEventFacility), |
559a17a1 | 393 | .class_init = init_event_facility_class, |
477a72a1 | 394 | .class_size = sizeof(SCLPEventFacilityClass), |
559a17a1 HG |
395 | }; |
396 | ||
c804c2a7 | 397 | static void event_realize(DeviceState *qdev, Error **errp) |
559a17a1 | 398 | { |
c804c2a7 | 399 | SCLPEvent *event = SCLP_EVENT(qdev); |
559a17a1 HG |
400 | SCLPEventClass *child = SCLP_EVENT_GET_CLASS(event); |
401 | ||
c804c2a7 HG |
402 | if (child->init) { |
403 | int rc = child->init(event); | |
404 | if (rc < 0) { | |
405 | error_setg(errp, "SCLP event initialization failed."); | |
406 | return; | |
407 | } | |
408 | } | |
559a17a1 HG |
409 | } |
410 | ||
c804c2a7 | 411 | static void event_unrealize(DeviceState *qdev, Error **errp) |
559a17a1 | 412 | { |
c804c2a7 | 413 | SCLPEvent *event = SCLP_EVENT(qdev); |
559a17a1 HG |
414 | SCLPEventClass *child = SCLP_EVENT_GET_CLASS(event); |
415 | if (child->exit) { | |
c804c2a7 HG |
416 | int rc = child->exit(event); |
417 | if (rc < 0) { | |
418 | error_setg(errp, "SCLP event exit failed."); | |
419 | return; | |
420 | } | |
559a17a1 | 421 | } |
559a17a1 HG |
422 | } |
423 | ||
424 | static void event_class_init(ObjectClass *klass, void *data) | |
425 | { | |
426 | DeviceClass *dc = DEVICE_CLASS(klass); | |
427 | ||
428 | dc->bus_type = TYPE_SCLP_EVENTS_BUS; | |
c804c2a7 HG |
429 | dc->realize = event_realize; |
430 | dc->unrealize = event_unrealize; | |
559a17a1 HG |
431 | } |
432 | ||
65e526c2 | 433 | static const TypeInfo sclp_event_type_info = { |
559a17a1 HG |
434 | .name = TYPE_SCLP_EVENT, |
435 | .parent = TYPE_DEVICE, | |
436 | .instance_size = sizeof(SCLPEvent), | |
437 | .class_init = event_class_init, | |
438 | .class_size = sizeof(SCLPEventClass), | |
439 | .abstract = true, | |
440 | }; | |
441 | ||
442 | static void register_types(void) | |
443 | { | |
65e526c2 HG |
444 | type_register_static(&sclp_events_bus_info); |
445 | type_register_static(&sclp_event_facility_info); | |
446 | type_register_static(&sclp_event_type_info); | |
559a17a1 HG |
447 | } |
448 | ||
449 | type_init(register_types) |