xref: /openbmc/qemu/hw/ppc/spapr_events.c (revision ddbb0d09)
1 /*
2  * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
3  *
4  * RTAS events handling
5  *
6  * Copyright (c) 2012 David Gibson, IBM Corporation.
7  *
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:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
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.
25  *
26  */
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"
32 
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"
38 
39 #include <libfdt.h>
40 
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;
86 
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;
105 
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;
113 
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;
128 
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;
141 
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;
164 
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;
172 
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;
196 
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;
204 
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
209 
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);                                               \
217         }                                                          \
218     } while (0)
219 
220 void spapr_events_fdt_skel(void *fdt, uint32_t check_exception_irq)
221 {
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};
224 
225     _FDT((fdt_begin_node(fdt, "event-sources")));
226 
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))));
231 
232     _FDT((fdt_begin_node(fdt, "epow-events")));
233     _FDT((fdt_property(fdt, "interrupts", interrupts, sizeof(interrupts))));
234     _FDT((fdt_end_node(fdt)));
235 
236     _FDT((fdt_end_node(fdt)));
237 }
238 
239 static void rtas_event_log_queue(int log_type, void *data, bool exception)
240 {
241     sPAPREventLogEntry *entry = g_new(sPAPREventLogEntry, 1);
242 
243     g_assert(data);
244     entry->log_type = log_type;
245     entry->exception = exception;
246     entry->data = data;
247     QTAILQ_INSERT_TAIL(&spapr->pending_events, entry, next);
248 }
249 
250 static sPAPREventLogEntry *rtas_event_log_dequeue(uint32_t event_mask,
251                                                   bool exception)
252 {
253     sPAPREventLogEntry *entry = NULL;
254 
255     /* we only queue EPOW events atm. */
256     if ((event_mask & EVENT_MASK_EPOW) == 0) {
257         return NULL;
258     }
259 
260     QTAILQ_FOREACH(entry, &spapr->pending_events, next) {
261         if (entry->exception != exception) {
262             continue;
263         }
264 
265         /* EPOW and hotplug events are surfaced in the same manner */
266         if (entry->log_type == RTAS_LOG_TYPE_EPOW ||
267             entry->log_type == RTAS_LOG_TYPE_HOTPLUG) {
268             break;
269         }
270     }
271 
272     if (entry) {
273         QTAILQ_REMOVE(&spapr->pending_events, entry, next);
274     }
275 
276     return entry;
277 }
278 
279 static bool rtas_event_log_contains(uint32_t event_mask, bool exception)
280 {
281     sPAPREventLogEntry *entry = NULL;
282 
283     /* we only queue EPOW events atm. */
284     if ((event_mask & EVENT_MASK_EPOW) == 0) {
285         return false;
286     }
287 
288     QTAILQ_FOREACH(entry, &spapr->pending_events, next) {
289         if (entry->exception != exception) {
290             continue;
291         }
292 
293         /* EPOW and hotplug events are surfaced in the same manner */
294         if (entry->log_type == RTAS_LOG_TYPE_EPOW ||
295             entry->log_type == RTAS_LOG_TYPE_HOTPLUG) {
296             return true;
297         }
298     }
299 
300     return false;
301 }
302 
303 static uint32_t next_plid;
304 
305 static void spapr_init_v6hdr(struct rtas_event_log_v6 *v6hdr)
306 {
307     v6hdr->b0 = RTAS_LOG_V6_B0_VALID | RTAS_LOG_V6_B0_NEW_LOG
308         | RTAS_LOG_V6_B0_BIGENDIAN;
309     v6hdr->b2 = RTAS_LOG_V6_B2_POWERPC_FORMAT
310         | RTAS_LOG_V6_B2_LOG_FORMAT_PLATFORM_EVENT;
311     v6hdr->company = cpu_to_be32(RTAS_LOG_V6_COMPANY_IBM);
312 }
313 
314 static void spapr_init_maina(struct rtas_event_log_v6_maina *maina,
315                              int section_count)
316 {
317     struct tm tm;
318     int year;
319 
320     maina->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_MAINA);
321     maina->hdr.section_length = cpu_to_be16(sizeof(*maina));
322     /* FIXME: section version, subtype and creator id? */
323     spapr_rtc_read(spapr->rtc, &tm, NULL);
324     year = tm.tm_year + 1900;
325     maina->creation_date = cpu_to_be32((to_bcd(year / 100) << 24)
326                                        | (to_bcd(year % 100) << 16)
327                                        | (to_bcd(tm.tm_mon + 1) << 8)
328                                        | to_bcd(tm.tm_mday));
329     maina->creation_time = cpu_to_be32((to_bcd(tm.tm_hour) << 24)
330                                        | (to_bcd(tm.tm_min) << 16)
331                                        | (to_bcd(tm.tm_sec) << 8));
332     maina->creator_id = 'H'; /* Hypervisor */
333     maina->section_count = section_count;
334     maina->plid = next_plid++;
335 }
336 
337 static void spapr_powerdown_req(Notifier *n, void *opaque)
338 {
339     sPAPREnvironment *spapr = container_of(n, sPAPREnvironment, epow_notifier);
340     struct rtas_error_log *hdr;
341     struct rtas_event_log_v6 *v6hdr;
342     struct rtas_event_log_v6_maina *maina;
343     struct rtas_event_log_v6_mainb *mainb;
344     struct rtas_event_log_v6_epow *epow;
345     struct epow_log_full *new_epow;
346 
347     new_epow = g_malloc0(sizeof(*new_epow));
348     hdr = &new_epow->hdr;
349     v6hdr = &new_epow->v6hdr;
350     maina = &new_epow->maina;
351     mainb = &new_epow->mainb;
352     epow = &new_epow->epow;
353 
354     hdr->summary = cpu_to_be32(RTAS_LOG_VERSION_6
355                                | RTAS_LOG_SEVERITY_EVENT
356                                | RTAS_LOG_DISPOSITION_NOT_RECOVERED
357                                | RTAS_LOG_OPTIONAL_PART_PRESENT
358                                | RTAS_LOG_TYPE_EPOW);
359     hdr->extended_length = cpu_to_be32(sizeof(*new_epow)
360                                        - sizeof(new_epow->hdr));
361 
362     spapr_init_v6hdr(v6hdr);
363     spapr_init_maina(maina, 3 /* Main-A, Main-B and EPOW */);
364 
365     mainb->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_MAINB);
366     mainb->hdr.section_length = cpu_to_be16(sizeof(*mainb));
367     /* FIXME: section version, subtype and creator id? */
368     mainb->subsystem_id = 0xa0; /* External environment */
369     mainb->event_severity = 0x00; /* Informational / non-error */
370     mainb->event_subtype = 0xd0; /* Normal shutdown */
371 
372     epow->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_EPOW);
373     epow->hdr.section_length = cpu_to_be16(sizeof(*epow));
374     epow->hdr.section_version = 2; /* includes extended modifier */
375     /* FIXME: section subtype and creator id? */
376     epow->sensor_value = RTAS_LOG_V6_EPOW_ACTION_SYSTEM_SHUTDOWN;
377     epow->event_modifier = RTAS_LOG_V6_EPOW_MODIFIER_NORMAL;
378     epow->extended_modifier = RTAS_LOG_V6_EPOW_XMODIFIER_PARTITION_SPECIFIC;
379 
380     rtas_event_log_queue(RTAS_LOG_TYPE_EPOW, new_epow, true);
381 
382     qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
383 }
384 
385 static void spapr_hotplug_req_event(sPAPRDRConnector *drc, uint8_t hp_action)
386 {
387     struct hp_log_full *new_hp;
388     struct rtas_error_log *hdr;
389     struct rtas_event_log_v6 *v6hdr;
390     struct rtas_event_log_v6_maina *maina;
391     struct rtas_event_log_v6_mainb *mainb;
392     struct rtas_event_log_v6_hp *hp;
393     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
394     sPAPRDRConnectorType drc_type = drck->get_type(drc);
395 
396     new_hp = g_malloc0(sizeof(struct hp_log_full));
397     hdr = &new_hp->hdr;
398     v6hdr = &new_hp->v6hdr;
399     maina = &new_hp->maina;
400     mainb = &new_hp->mainb;
401     hp = &new_hp->hp;
402 
403     hdr->summary = cpu_to_be32(RTAS_LOG_VERSION_6
404                                | RTAS_LOG_SEVERITY_EVENT
405                                | RTAS_LOG_DISPOSITION_NOT_RECOVERED
406                                | RTAS_LOG_OPTIONAL_PART_PRESENT
407                                | RTAS_LOG_INITIATOR_HOTPLUG
408                                | RTAS_LOG_TYPE_HOTPLUG);
409     hdr->extended_length = cpu_to_be32(sizeof(*new_hp)
410                                        - sizeof(new_hp->hdr));
411 
412     spapr_init_v6hdr(v6hdr);
413     spapr_init_maina(maina, 3 /* Main-A, Main-B, HP */);
414 
415     mainb->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_MAINB);
416     mainb->hdr.section_length = cpu_to_be16(sizeof(*mainb));
417     mainb->subsystem_id = 0x80; /* External environment */
418     mainb->event_severity = 0x00; /* Informational / non-error */
419     mainb->event_subtype = 0x00; /* Normal shutdown */
420 
421     hp->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_HOTPLUG);
422     hp->hdr.section_length = cpu_to_be16(sizeof(*hp));
423     hp->hdr.section_version = 1; /* includes extended modifier */
424     hp->hotplug_action = hp_action;
425 
426 
427     switch (drc_type) {
428     case SPAPR_DR_CONNECTOR_TYPE_PCI:
429         hp->drc.index = cpu_to_be32(drck->get_index(drc));
430         hp->hotplug_identifier = RTAS_LOG_V6_HP_ID_DRC_INDEX;
431         hp->hotplug_type = RTAS_LOG_V6_HP_TYPE_PCI;
432         break;
433     default:
434         /* we shouldn't be signaling hotplug events for resources
435          * that don't support them
436          */
437         g_assert(false);
438         return;
439     }
440 
441     rtas_event_log_queue(RTAS_LOG_TYPE_HOTPLUG, new_hp, true);
442 
443     qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
444 }
445 
446 void spapr_hotplug_req_add_event(sPAPRDRConnector *drc)
447 {
448     spapr_hotplug_req_event(drc, RTAS_LOG_V6_HP_ACTION_ADD);
449 }
450 
451 void spapr_hotplug_req_remove_event(sPAPRDRConnector *drc)
452 {
453     spapr_hotplug_req_event(drc, RTAS_LOG_V6_HP_ACTION_REMOVE);
454 }
455 
456 static void check_exception(PowerPCCPU *cpu, sPAPREnvironment *spapr,
457                             uint32_t token, uint32_t nargs,
458                             target_ulong args,
459                             uint32_t nret, target_ulong rets)
460 {
461     uint32_t mask, buf, len, event_len;
462     uint64_t xinfo;
463     sPAPREventLogEntry *event;
464     struct rtas_error_log *hdr;
465 
466     if ((nargs < 6) || (nargs > 7) || nret != 1) {
467         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
468         return;
469     }
470 
471     xinfo = rtas_ld(args, 1);
472     mask = rtas_ld(args, 2);
473     buf = rtas_ld(args, 4);
474     len = rtas_ld(args, 5);
475     if (nargs == 7) {
476         xinfo |= (uint64_t)rtas_ld(args, 6) << 32;
477     }
478 
479     event = rtas_event_log_dequeue(mask, true);
480     if (!event) {
481         goto out_no_events;
482     }
483 
484     hdr = event->data;
485     event_len = be32_to_cpu(hdr->extended_length) + sizeof(*hdr);
486 
487     if (event_len < len) {
488         len = event_len;
489     }
490 
491     cpu_physical_memory_write(buf, event->data, len);
492     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
493     g_free(event->data);
494     g_free(event);
495 
496     /* according to PAPR+, the IRQ must be left asserted, or re-asserted, if
497      * there are still pending events to be fetched via check-exception. We
498      * do the latter here, since our code relies on edge-triggered
499      * interrupts.
500      */
501     if (rtas_event_log_contains(mask, true)) {
502         qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
503     }
504 
505     return;
506 
507 out_no_events:
508     rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND);
509 }
510 
511 static void event_scan(PowerPCCPU *cpu, sPAPREnvironment *spapr,
512                        uint32_t token, uint32_t nargs,
513                        target_ulong args,
514                        uint32_t nret, target_ulong rets)
515 {
516     uint32_t mask, buf, len, event_len;
517     sPAPREventLogEntry *event;
518     struct rtas_error_log *hdr;
519 
520     if (nargs != 4 || nret != 1) {
521         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
522         return;
523     }
524 
525     mask = rtas_ld(args, 0);
526     buf = rtas_ld(args, 2);
527     len = rtas_ld(args, 3);
528 
529     event = rtas_event_log_dequeue(mask, false);
530     if (!event) {
531         goto out_no_events;
532     }
533 
534     hdr = event->data;
535     event_len = be32_to_cpu(hdr->extended_length) + sizeof(*hdr);
536 
537     if (event_len < len) {
538         len = event_len;
539     }
540 
541     cpu_physical_memory_write(buf, event->data, len);
542     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
543     g_free(event->data);
544     g_free(event);
545     return;
546 
547 out_no_events:
548     rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND);
549 }
550 
551 void spapr_events_init(sPAPREnvironment *spapr)
552 {
553     QTAILQ_INIT(&spapr->pending_events);
554     spapr->check_exception_irq = xics_alloc(spapr->icp, 0, 0, false);
555     spapr->epow_notifier.notify = spapr_powerdown_req;
556     qemu_register_powerdown_notifier(&spapr->epow_notifier);
557     spapr_rtas_register(RTAS_CHECK_EXCEPTION, "check-exception",
558                         check_exception);
559     spapr_rtas_register(RTAS_EVENT_SCAN, "event-scan", event_scan);
560 }
561