qemu-char: append opt to stop truncation of serial file
[qemu/ar7.git] / hw / ppc / spapr_events.c
blob744ea62ac04fab8996527bab23025f4d086bf2d3
1 /*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
4 * RTAS events handling
6 * Copyright (c) 2012 David Gibson, IBM Corporation.
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
27 #include "cpu.h"
28 #include "sysemu/sysemu.h"
29 #include "sysemu/char.h"
30 #include "hw/qdev.h"
31 #include "sysemu/device_tree.h"
33 #include "hw/ppc/spapr.h"
34 #include "hw/ppc/spapr_vio.h"
35 #include "hw/pci/pci.h"
36 #include "hw/pci-host/spapr.h"
37 #include "hw/ppc/spapr_drc.h"
39 #include <libfdt.h>
41 struct rtas_error_log {
42 uint32_t summary;
43 #define RTAS_LOG_VERSION_MASK 0xff000000
44 #define RTAS_LOG_VERSION_6 0x06000000
45 #define RTAS_LOG_SEVERITY_MASK 0x00e00000
46 #define RTAS_LOG_SEVERITY_ALREADY_REPORTED 0x00c00000
47 #define RTAS_LOG_SEVERITY_FATAL 0x00a00000
48 #define RTAS_LOG_SEVERITY_ERROR 0x00800000
49 #define RTAS_LOG_SEVERITY_ERROR_SYNC 0x00600000
50 #define RTAS_LOG_SEVERITY_WARNING 0x00400000
51 #define RTAS_LOG_SEVERITY_EVENT 0x00200000
52 #define RTAS_LOG_SEVERITY_NO_ERROR 0x00000000
53 #define RTAS_LOG_DISPOSITION_MASK 0x00180000
54 #define RTAS_LOG_DISPOSITION_FULLY_RECOVERED 0x00000000
55 #define RTAS_LOG_DISPOSITION_LIMITED_RECOVERY 0x00080000
56 #define RTAS_LOG_DISPOSITION_NOT_RECOVERED 0x00100000
57 #define RTAS_LOG_OPTIONAL_PART_PRESENT 0x00040000
58 #define RTAS_LOG_INITIATOR_MASK 0x0000f000
59 #define RTAS_LOG_INITIATOR_UNKNOWN 0x00000000
60 #define RTAS_LOG_INITIATOR_CPU 0x00001000
61 #define RTAS_LOG_INITIATOR_PCI 0x00002000
62 #define RTAS_LOG_INITIATOR_MEMORY 0x00004000
63 #define RTAS_LOG_INITIATOR_HOTPLUG 0x00006000
64 #define RTAS_LOG_TARGET_MASK 0x00000f00
65 #define RTAS_LOG_TARGET_UNKNOWN 0x00000000
66 #define RTAS_LOG_TARGET_CPU 0x00000100
67 #define RTAS_LOG_TARGET_PCI 0x00000200
68 #define RTAS_LOG_TARGET_MEMORY 0x00000400
69 #define RTAS_LOG_TARGET_HOTPLUG 0x00000600
70 #define RTAS_LOG_TYPE_MASK 0x000000ff
71 #define RTAS_LOG_TYPE_OTHER 0x00000000
72 #define RTAS_LOG_TYPE_RETRY 0x00000001
73 #define RTAS_LOG_TYPE_TCE_ERR 0x00000002
74 #define RTAS_LOG_TYPE_INTERN_DEV_FAIL 0x00000003
75 #define RTAS_LOG_TYPE_TIMEOUT 0x00000004
76 #define RTAS_LOG_TYPE_DATA_PARITY 0x00000005
77 #define RTAS_LOG_TYPE_ADDR_PARITY 0x00000006
78 #define RTAS_LOG_TYPE_CACHE_PARITY 0x00000007
79 #define RTAS_LOG_TYPE_ADDR_INVALID 0x00000008
80 #define RTAS_LOG_TYPE_ECC_UNCORR 0x00000009
81 #define RTAS_LOG_TYPE_ECC_CORR 0x0000000a
82 #define RTAS_LOG_TYPE_EPOW 0x00000040
83 #define RTAS_LOG_TYPE_HOTPLUG 0x000000e5
84 uint32_t extended_length;
85 } QEMU_PACKED;
87 struct rtas_event_log_v6 {
88 uint8_t b0;
89 #define RTAS_LOG_V6_B0_VALID 0x80
90 #define RTAS_LOG_V6_B0_UNRECOVERABLE_ERROR 0x40
91 #define RTAS_LOG_V6_B0_RECOVERABLE_ERROR 0x20
92 #define RTAS_LOG_V6_B0_DEGRADED_OPERATION 0x10
93 #define RTAS_LOG_V6_B0_PREDICTIVE_ERROR 0x08
94 #define RTAS_LOG_V6_B0_NEW_LOG 0x04
95 #define RTAS_LOG_V6_B0_BIGENDIAN 0x02
96 uint8_t _resv1;
97 uint8_t b2;
98 #define RTAS_LOG_V6_B2_POWERPC_FORMAT 0x80
99 #define RTAS_LOG_V6_B2_LOG_FORMAT_MASK 0x0f
100 #define RTAS_LOG_V6_B2_LOG_FORMAT_PLATFORM_EVENT 0x0e
101 uint8_t _resv2[9];
102 uint32_t company;
103 #define RTAS_LOG_V6_COMPANY_IBM 0x49424d00 /* IBM<null> */
104 } QEMU_PACKED;
106 struct rtas_event_log_v6_section_header {
107 uint16_t section_id;
108 uint16_t section_length;
109 uint8_t section_version;
110 uint8_t section_subtype;
111 uint16_t creator_component_id;
112 } QEMU_PACKED;
114 struct rtas_event_log_v6_maina {
115 #define RTAS_LOG_V6_SECTION_ID_MAINA 0x5048 /* PH */
116 struct rtas_event_log_v6_section_header hdr;
117 uint32_t creation_date; /* BCD: YYYYMMDD */
118 uint32_t creation_time; /* BCD: HHMMSS00 */
119 uint8_t _platform1[8];
120 char creator_id;
121 uint8_t _resv1[2];
122 uint8_t section_count;
123 uint8_t _resv2[4];
124 uint8_t _platform2[8];
125 uint32_t plid;
126 uint8_t _platform3[4];
127 } QEMU_PACKED;
129 struct rtas_event_log_v6_mainb {
130 #define RTAS_LOG_V6_SECTION_ID_MAINB 0x5548 /* UH */
131 struct rtas_event_log_v6_section_header hdr;
132 uint8_t subsystem_id;
133 uint8_t _platform1;
134 uint8_t event_severity;
135 uint8_t event_subtype;
136 uint8_t _platform2[4];
137 uint8_t _resv1[2];
138 uint16_t action_flags;
139 uint8_t _resv2[4];
140 } QEMU_PACKED;
142 struct rtas_event_log_v6_epow {
143 #define RTAS_LOG_V6_SECTION_ID_EPOW 0x4550 /* EP */
144 struct rtas_event_log_v6_section_header hdr;
145 uint8_t sensor_value;
146 #define RTAS_LOG_V6_EPOW_ACTION_RESET 0
147 #define RTAS_LOG_V6_EPOW_ACTION_WARN_COOLING 1
148 #define RTAS_LOG_V6_EPOW_ACTION_WARN_POWER 2
149 #define RTAS_LOG_V6_EPOW_ACTION_SYSTEM_SHUTDOWN 3
150 #define RTAS_LOG_V6_EPOW_ACTION_SYSTEM_HALT 4
151 #define RTAS_LOG_V6_EPOW_ACTION_MAIN_ENCLOSURE 5
152 #define RTAS_LOG_V6_EPOW_ACTION_POWER_OFF 7
153 uint8_t event_modifier;
154 #define RTAS_LOG_V6_EPOW_MODIFIER_NORMAL 1
155 #define RTAS_LOG_V6_EPOW_MODIFIER_ON_UPS 2
156 #define RTAS_LOG_V6_EPOW_MODIFIER_CRITICAL 3
157 #define RTAS_LOG_V6_EPOW_MODIFIER_TEMPERATURE 4
158 uint8_t extended_modifier;
159 #define RTAS_LOG_V6_EPOW_XMODIFIER_SYSTEM_WIDE 0
160 #define RTAS_LOG_V6_EPOW_XMODIFIER_PARTITION_SPECIFIC 1
161 uint8_t _resv;
162 uint64_t reason_code;
163 } QEMU_PACKED;
165 struct epow_log_full {
166 struct rtas_error_log hdr;
167 struct rtas_event_log_v6 v6hdr;
168 struct rtas_event_log_v6_maina maina;
169 struct rtas_event_log_v6_mainb mainb;
170 struct rtas_event_log_v6_epow epow;
171 } QEMU_PACKED;
173 struct rtas_event_log_v6_hp {
174 #define RTAS_LOG_V6_SECTION_ID_HOTPLUG 0x4850 /* HP */
175 struct rtas_event_log_v6_section_header hdr;
176 uint8_t hotplug_type;
177 #define RTAS_LOG_V6_HP_TYPE_CPU 1
178 #define RTAS_LOG_V6_HP_TYPE_MEMORY 2
179 #define RTAS_LOG_V6_HP_TYPE_SLOT 3
180 #define RTAS_LOG_V6_HP_TYPE_PHB 4
181 #define RTAS_LOG_V6_HP_TYPE_PCI 5
182 uint8_t hotplug_action;
183 #define RTAS_LOG_V6_HP_ACTION_ADD 1
184 #define RTAS_LOG_V6_HP_ACTION_REMOVE 2
185 uint8_t hotplug_identifier;
186 #define RTAS_LOG_V6_HP_ID_DRC_NAME 1
187 #define RTAS_LOG_V6_HP_ID_DRC_INDEX 2
188 #define RTAS_LOG_V6_HP_ID_DRC_COUNT 3
189 uint8_t reserved;
190 union {
191 uint32_t index;
192 uint32_t count;
193 char name[1];
194 } drc;
195 } QEMU_PACKED;
197 struct hp_log_full {
198 struct rtas_error_log hdr;
199 struct rtas_event_log_v6 v6hdr;
200 struct rtas_event_log_v6_maina maina;
201 struct rtas_event_log_v6_mainb mainb;
202 struct rtas_event_log_v6_hp hp;
203 } QEMU_PACKED;
205 #define EVENT_MASK_INTERNAL_ERRORS 0x80000000
206 #define EVENT_MASK_EPOW 0x40000000
207 #define EVENT_MASK_HOTPLUG 0x10000000
208 #define EVENT_MASK_IO 0x08000000
210 #define _FDT(exp) \
211 do { \
212 int ret = (exp); \
213 if (ret < 0) { \
214 fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
215 #exp, fdt_strerror(ret)); \
216 exit(1); \
218 } while (0)
220 void spapr_events_fdt_skel(void *fdt, uint32_t check_exception_irq)
222 uint32_t irq_ranges[] = {cpu_to_be32(check_exception_irq), cpu_to_be32(1)};
223 uint32_t interrupts[] = {cpu_to_be32(check_exception_irq), 0};
225 _FDT((fdt_begin_node(fdt, "event-sources")));
227 _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
228 _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2)));
229 _FDT((fdt_property(fdt, "interrupt-ranges",
230 irq_ranges, sizeof(irq_ranges))));
232 _FDT((fdt_begin_node(fdt, "epow-events")));
233 _FDT((fdt_property(fdt, "interrupts", interrupts, sizeof(interrupts))));
234 _FDT((fdt_end_node(fdt)));
236 _FDT((fdt_end_node(fdt)));
239 static void rtas_event_log_queue(int log_type, void *data, bool exception)
241 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
242 sPAPREventLogEntry *entry = g_new(sPAPREventLogEntry, 1);
244 g_assert(data);
245 entry->log_type = log_type;
246 entry->exception = exception;
247 entry->data = data;
248 QTAILQ_INSERT_TAIL(&spapr->pending_events, entry, next);
251 static sPAPREventLogEntry *rtas_event_log_dequeue(uint32_t event_mask,
252 bool exception)
254 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
255 sPAPREventLogEntry *entry = NULL;
257 /* we only queue EPOW events atm. */
258 if ((event_mask & EVENT_MASK_EPOW) == 0) {
259 return NULL;
262 QTAILQ_FOREACH(entry, &spapr->pending_events, next) {
263 if (entry->exception != exception) {
264 continue;
267 /* EPOW and hotplug events are surfaced in the same manner */
268 if (entry->log_type == RTAS_LOG_TYPE_EPOW ||
269 entry->log_type == RTAS_LOG_TYPE_HOTPLUG) {
270 break;
274 if (entry) {
275 QTAILQ_REMOVE(&spapr->pending_events, entry, next);
278 return entry;
281 static bool rtas_event_log_contains(uint32_t event_mask, bool exception)
283 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
284 sPAPREventLogEntry *entry = NULL;
286 /* we only queue EPOW events atm. */
287 if ((event_mask & EVENT_MASK_EPOW) == 0) {
288 return false;
291 QTAILQ_FOREACH(entry, &spapr->pending_events, next) {
292 if (entry->exception != exception) {
293 continue;
296 /* EPOW and hotplug events are surfaced in the same manner */
297 if (entry->log_type == RTAS_LOG_TYPE_EPOW ||
298 entry->log_type == RTAS_LOG_TYPE_HOTPLUG) {
299 return true;
303 return false;
306 static uint32_t next_plid;
308 static void spapr_init_v6hdr(struct rtas_event_log_v6 *v6hdr)
310 v6hdr->b0 = RTAS_LOG_V6_B0_VALID | RTAS_LOG_V6_B0_NEW_LOG
311 | RTAS_LOG_V6_B0_BIGENDIAN;
312 v6hdr->b2 = RTAS_LOG_V6_B2_POWERPC_FORMAT
313 | RTAS_LOG_V6_B2_LOG_FORMAT_PLATFORM_EVENT;
314 v6hdr->company = cpu_to_be32(RTAS_LOG_V6_COMPANY_IBM);
317 static void spapr_init_maina(struct rtas_event_log_v6_maina *maina,
318 int section_count)
320 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
321 struct tm tm;
322 int year;
324 maina->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_MAINA);
325 maina->hdr.section_length = cpu_to_be16(sizeof(*maina));
326 /* FIXME: section version, subtype and creator id? */
327 spapr_rtc_read(spapr->rtc, &tm, NULL);
328 year = tm.tm_year + 1900;
329 maina->creation_date = cpu_to_be32((to_bcd(year / 100) << 24)
330 | (to_bcd(year % 100) << 16)
331 | (to_bcd(tm.tm_mon + 1) << 8)
332 | to_bcd(tm.tm_mday));
333 maina->creation_time = cpu_to_be32((to_bcd(tm.tm_hour) << 24)
334 | (to_bcd(tm.tm_min) << 16)
335 | (to_bcd(tm.tm_sec) << 8));
336 maina->creator_id = 'H'; /* Hypervisor */
337 maina->section_count = section_count;
338 maina->plid = next_plid++;
341 static void spapr_powerdown_req(Notifier *n, void *opaque)
343 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
344 struct rtas_error_log *hdr;
345 struct rtas_event_log_v6 *v6hdr;
346 struct rtas_event_log_v6_maina *maina;
347 struct rtas_event_log_v6_mainb *mainb;
348 struct rtas_event_log_v6_epow *epow;
349 struct epow_log_full *new_epow;
351 new_epow = g_malloc0(sizeof(*new_epow));
352 hdr = &new_epow->hdr;
353 v6hdr = &new_epow->v6hdr;
354 maina = &new_epow->maina;
355 mainb = &new_epow->mainb;
356 epow = &new_epow->epow;
358 hdr->summary = cpu_to_be32(RTAS_LOG_VERSION_6
359 | RTAS_LOG_SEVERITY_EVENT
360 | RTAS_LOG_DISPOSITION_NOT_RECOVERED
361 | RTAS_LOG_OPTIONAL_PART_PRESENT
362 | RTAS_LOG_TYPE_EPOW);
363 hdr->extended_length = cpu_to_be32(sizeof(*new_epow)
364 - sizeof(new_epow->hdr));
366 spapr_init_v6hdr(v6hdr);
367 spapr_init_maina(maina, 3 /* Main-A, Main-B and EPOW */);
369 mainb->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_MAINB);
370 mainb->hdr.section_length = cpu_to_be16(sizeof(*mainb));
371 /* FIXME: section version, subtype and creator id? */
372 mainb->subsystem_id = 0xa0; /* External environment */
373 mainb->event_severity = 0x00; /* Informational / non-error */
374 mainb->event_subtype = 0xd0; /* Normal shutdown */
376 epow->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_EPOW);
377 epow->hdr.section_length = cpu_to_be16(sizeof(*epow));
378 epow->hdr.section_version = 2; /* includes extended modifier */
379 /* FIXME: section subtype and creator id? */
380 epow->sensor_value = RTAS_LOG_V6_EPOW_ACTION_SYSTEM_SHUTDOWN;
381 epow->event_modifier = RTAS_LOG_V6_EPOW_MODIFIER_NORMAL;
382 epow->extended_modifier = RTAS_LOG_V6_EPOW_XMODIFIER_PARTITION_SPECIFIC;
384 rtas_event_log_queue(RTAS_LOG_TYPE_EPOW, new_epow, true);
386 qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
389 static void spapr_hotplug_req_event(uint8_t hp_id, uint8_t hp_action,
390 sPAPRDRConnectorType drc_type,
391 uint32_t drc)
393 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
394 struct hp_log_full *new_hp;
395 struct rtas_error_log *hdr;
396 struct rtas_event_log_v6 *v6hdr;
397 struct rtas_event_log_v6_maina *maina;
398 struct rtas_event_log_v6_mainb *mainb;
399 struct rtas_event_log_v6_hp *hp;
401 new_hp = g_malloc0(sizeof(struct hp_log_full));
402 hdr = &new_hp->hdr;
403 v6hdr = &new_hp->v6hdr;
404 maina = &new_hp->maina;
405 mainb = &new_hp->mainb;
406 hp = &new_hp->hp;
408 hdr->summary = cpu_to_be32(RTAS_LOG_VERSION_6
409 | RTAS_LOG_SEVERITY_EVENT
410 | RTAS_LOG_DISPOSITION_NOT_RECOVERED
411 | RTAS_LOG_OPTIONAL_PART_PRESENT
412 | RTAS_LOG_INITIATOR_HOTPLUG
413 | RTAS_LOG_TYPE_HOTPLUG);
414 hdr->extended_length = cpu_to_be32(sizeof(*new_hp)
415 - sizeof(new_hp->hdr));
417 spapr_init_v6hdr(v6hdr);
418 spapr_init_maina(maina, 3 /* Main-A, Main-B, HP */);
420 mainb->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_MAINB);
421 mainb->hdr.section_length = cpu_to_be16(sizeof(*mainb));
422 mainb->subsystem_id = 0x80; /* External environment */
423 mainb->event_severity = 0x00; /* Informational / non-error */
424 mainb->event_subtype = 0x00; /* Normal shutdown */
426 hp->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_HOTPLUG);
427 hp->hdr.section_length = cpu_to_be16(sizeof(*hp));
428 hp->hdr.section_version = 1; /* includes extended modifier */
429 hp->hotplug_action = hp_action;
430 hp->hotplug_identifier = hp_id;
432 switch (drc_type) {
433 case SPAPR_DR_CONNECTOR_TYPE_PCI:
434 hp->hotplug_type = RTAS_LOG_V6_HP_TYPE_PCI;
435 break;
436 case SPAPR_DR_CONNECTOR_TYPE_LMB:
437 hp->hotplug_type = RTAS_LOG_V6_HP_TYPE_MEMORY;
438 break;
439 default:
440 /* we shouldn't be signaling hotplug events for resources
441 * that don't support them
443 g_assert(false);
444 return;
447 if (hp_id == RTAS_LOG_V6_HP_ID_DRC_COUNT) {
448 hp->drc.count = cpu_to_be32(drc);
449 } else if (hp_id == RTAS_LOG_V6_HP_ID_DRC_INDEX) {
450 hp->drc.index = cpu_to_be32(drc);
453 rtas_event_log_queue(RTAS_LOG_TYPE_HOTPLUG, new_hp, true);
455 qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
458 void spapr_hotplug_req_add_by_index(sPAPRDRConnector *drc)
460 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
461 sPAPRDRConnectorType drc_type = drck->get_type(drc);
462 uint32 index = drck->get_index(drc);
464 spapr_hotplug_req_event(RTAS_LOG_V6_HP_ID_DRC_INDEX,
465 RTAS_LOG_V6_HP_ACTION_ADD, drc_type, index);
468 void spapr_hotplug_req_remove_by_index(sPAPRDRConnector *drc)
470 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
471 sPAPRDRConnectorType drc_type = drck->get_type(drc);
472 uint32 index = drck->get_index(drc);
474 spapr_hotplug_req_event(RTAS_LOG_V6_HP_ID_DRC_INDEX,
475 RTAS_LOG_V6_HP_ACTION_REMOVE, drc_type, index);
478 void spapr_hotplug_req_add_by_count(sPAPRDRConnectorType drc_type,
479 uint32_t count)
481 spapr_hotplug_req_event(RTAS_LOG_V6_HP_ID_DRC_COUNT,
482 RTAS_LOG_V6_HP_ACTION_ADD, drc_type, count);
485 void spapr_hotplug_req_remove_by_count(sPAPRDRConnectorType drc_type,
486 uint32_t count)
488 spapr_hotplug_req_event(RTAS_LOG_V6_HP_ID_DRC_COUNT,
489 RTAS_LOG_V6_HP_ACTION_REMOVE, drc_type, count);
492 static void check_exception(PowerPCCPU *cpu, sPAPRMachineState *spapr,
493 uint32_t token, uint32_t nargs,
494 target_ulong args,
495 uint32_t nret, target_ulong rets)
497 uint32_t mask, buf, len, event_len;
498 uint64_t xinfo;
499 sPAPREventLogEntry *event;
500 struct rtas_error_log *hdr;
502 if ((nargs < 6) || (nargs > 7) || nret != 1) {
503 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
504 return;
507 xinfo = rtas_ld(args, 1);
508 mask = rtas_ld(args, 2);
509 buf = rtas_ld(args, 4);
510 len = rtas_ld(args, 5);
511 if (nargs == 7) {
512 xinfo |= (uint64_t)rtas_ld(args, 6) << 32;
515 event = rtas_event_log_dequeue(mask, true);
516 if (!event) {
517 goto out_no_events;
520 hdr = event->data;
521 event_len = be32_to_cpu(hdr->extended_length) + sizeof(*hdr);
523 if (event_len < len) {
524 len = event_len;
527 cpu_physical_memory_write(buf, event->data, len);
528 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
529 g_free(event->data);
530 g_free(event);
532 /* according to PAPR+, the IRQ must be left asserted, or re-asserted, if
533 * there are still pending events to be fetched via check-exception. We
534 * do the latter here, since our code relies on edge-triggered
535 * interrupts.
537 if (rtas_event_log_contains(mask, true)) {
538 qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
541 return;
543 out_no_events:
544 rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND);
547 static void event_scan(PowerPCCPU *cpu, sPAPRMachineState *spapr,
548 uint32_t token, uint32_t nargs,
549 target_ulong args,
550 uint32_t nret, target_ulong rets)
552 uint32_t mask, buf, len, event_len;
553 sPAPREventLogEntry *event;
554 struct rtas_error_log *hdr;
556 if (nargs != 4 || nret != 1) {
557 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
558 return;
561 mask = rtas_ld(args, 0);
562 buf = rtas_ld(args, 2);
563 len = rtas_ld(args, 3);
565 event = rtas_event_log_dequeue(mask, false);
566 if (!event) {
567 goto out_no_events;
570 hdr = event->data;
571 event_len = be32_to_cpu(hdr->extended_length) + sizeof(*hdr);
573 if (event_len < len) {
574 len = event_len;
577 cpu_physical_memory_write(buf, event->data, len);
578 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
579 g_free(event->data);
580 g_free(event);
581 return;
583 out_no_events:
584 rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND);
587 void spapr_events_init(sPAPRMachineState *spapr)
589 QTAILQ_INIT(&spapr->pending_events);
590 spapr->check_exception_irq = xics_alloc(spapr->icp, 0, 0, false);
591 spapr->epow_notifier.notify = spapr_powerdown_req;
592 qemu_register_powerdown_notifier(&spapr->epow_notifier);
593 spapr_rtas_register(RTAS_CHECK_EXCEPTION, "check-exception",
594 check_exception);
595 spapr_rtas_register(RTAS_EVENT_SCAN, "event-scan", event_scan);