Add qemu 2.4.0
[kvmfornfv.git] / qemu / hw / ppc / spapr_events.c
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     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
242     sPAPREventLogEntry *entry = g_new(sPAPREventLogEntry, 1);
243
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);
249 }
250
251 static sPAPREventLogEntry *rtas_event_log_dequeue(uint32_t event_mask,
252                                                   bool exception)
253 {
254     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
255     sPAPREventLogEntry *entry = NULL;
256
257     /* we only queue EPOW events atm. */
258     if ((event_mask & EVENT_MASK_EPOW) == 0) {
259         return NULL;
260     }
261
262     QTAILQ_FOREACH(entry, &spapr->pending_events, next) {
263         if (entry->exception != exception) {
264             continue;
265         }
266
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;
271         }
272     }
273
274     if (entry) {
275         QTAILQ_REMOVE(&spapr->pending_events, entry, next);
276     }
277
278     return entry;
279 }
280
281 static bool rtas_event_log_contains(uint32_t event_mask, bool exception)
282 {
283     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
284     sPAPREventLogEntry *entry = NULL;
285
286     /* we only queue EPOW events atm. */
287     if ((event_mask & EVENT_MASK_EPOW) == 0) {
288         return false;
289     }
290
291     QTAILQ_FOREACH(entry, &spapr->pending_events, next) {
292         if (entry->exception != exception) {
293             continue;
294         }
295
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;
300         }
301     }
302
303     return false;
304 }
305
306 static uint32_t next_plid;
307
308 static void spapr_init_v6hdr(struct rtas_event_log_v6 *v6hdr)
309 {
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);
315 }
316
317 static void spapr_init_maina(struct rtas_event_log_v6_maina *maina,
318                              int section_count)
319 {
320     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
321     struct tm tm;
322     int year;
323
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++;
339 }
340
341 static void spapr_powerdown_req(Notifier *n, void *opaque)
342 {
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;
350
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;
357
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));
365
366     spapr_init_v6hdr(v6hdr);
367     spapr_init_maina(maina, 3 /* Main-A, Main-B and EPOW */);
368
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 */
375
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;
383
384     rtas_event_log_queue(RTAS_LOG_TYPE_EPOW, new_epow, true);
385
386     qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
387 }
388
389 static void spapr_hotplug_req_event(sPAPRDRConnector *drc, uint8_t hp_action)
390 {
391     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
392     struct hp_log_full *new_hp;
393     struct rtas_error_log *hdr;
394     struct rtas_event_log_v6 *v6hdr;
395     struct rtas_event_log_v6_maina *maina;
396     struct rtas_event_log_v6_mainb *mainb;
397     struct rtas_event_log_v6_hp *hp;
398     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
399     sPAPRDRConnectorType drc_type = drck->get_type(drc);
400
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;
407
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));
416
417     spapr_init_v6hdr(v6hdr);
418     spapr_init_maina(maina, 3 /* Main-A, Main-B, HP */);
419
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 */
425
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
431
432     switch (drc_type) {
433     case SPAPR_DR_CONNECTOR_TYPE_PCI:
434         hp->drc.index = cpu_to_be32(drck->get_index(drc));
435         hp->hotplug_identifier = RTAS_LOG_V6_HP_ID_DRC_INDEX;
436         hp->hotplug_type = RTAS_LOG_V6_HP_TYPE_PCI;
437         break;
438     default:
439         /* we shouldn't be signaling hotplug events for resources
440          * that don't support them
441          */
442         g_assert(false);
443         return;
444     }
445
446     rtas_event_log_queue(RTAS_LOG_TYPE_HOTPLUG, new_hp, true);
447
448     qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
449 }
450
451 void spapr_hotplug_req_add_event(sPAPRDRConnector *drc)
452 {
453     spapr_hotplug_req_event(drc, RTAS_LOG_V6_HP_ACTION_ADD);
454 }
455
456 void spapr_hotplug_req_remove_event(sPAPRDRConnector *drc)
457 {
458     spapr_hotplug_req_event(drc, RTAS_LOG_V6_HP_ACTION_REMOVE);
459 }
460
461 static void check_exception(PowerPCCPU *cpu, sPAPRMachineState *spapr,
462                             uint32_t token, uint32_t nargs,
463                             target_ulong args,
464                             uint32_t nret, target_ulong rets)
465 {
466     uint32_t mask, buf, len, event_len;
467     uint64_t xinfo;
468     sPAPREventLogEntry *event;
469     struct rtas_error_log *hdr;
470
471     if ((nargs < 6) || (nargs > 7) || nret != 1) {
472         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
473         return;
474     }
475
476     xinfo = rtas_ld(args, 1);
477     mask = rtas_ld(args, 2);
478     buf = rtas_ld(args, 4);
479     len = rtas_ld(args, 5);
480     if (nargs == 7) {
481         xinfo |= (uint64_t)rtas_ld(args, 6) << 32;
482     }
483
484     event = rtas_event_log_dequeue(mask, true);
485     if (!event) {
486         goto out_no_events;
487     }
488
489     hdr = event->data;
490     event_len = be32_to_cpu(hdr->extended_length) + sizeof(*hdr);
491
492     if (event_len < len) {
493         len = event_len;
494     }
495
496     cpu_physical_memory_write(buf, event->data, len);
497     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
498     g_free(event->data);
499     g_free(event);
500
501     /* according to PAPR+, the IRQ must be left asserted, or re-asserted, if
502      * there are still pending events to be fetched via check-exception. We
503      * do the latter here, since our code relies on edge-triggered
504      * interrupts.
505      */
506     if (rtas_event_log_contains(mask, true)) {
507         qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
508     }
509
510     return;
511
512 out_no_events:
513     rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND);
514 }
515
516 static void event_scan(PowerPCCPU *cpu, sPAPRMachineState *spapr,
517                        uint32_t token, uint32_t nargs,
518                        target_ulong args,
519                        uint32_t nret, target_ulong rets)
520 {
521     uint32_t mask, buf, len, event_len;
522     sPAPREventLogEntry *event;
523     struct rtas_error_log *hdr;
524
525     if (nargs != 4 || nret != 1) {
526         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
527         return;
528     }
529
530     mask = rtas_ld(args, 0);
531     buf = rtas_ld(args, 2);
532     len = rtas_ld(args, 3);
533
534     event = rtas_event_log_dequeue(mask, false);
535     if (!event) {
536         goto out_no_events;
537     }
538
539     hdr = event->data;
540     event_len = be32_to_cpu(hdr->extended_length) + sizeof(*hdr);
541
542     if (event_len < len) {
543         len = event_len;
544     }
545
546     cpu_physical_memory_write(buf, event->data, len);
547     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
548     g_free(event->data);
549     g_free(event);
550     return;
551
552 out_no_events:
553     rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND);
554 }
555
556 void spapr_events_init(sPAPRMachineState *spapr)
557 {
558     QTAILQ_INIT(&spapr->pending_events);
559     spapr->check_exception_irq = xics_alloc(spapr->icp, 0, 0, false);
560     spapr->epow_notifier.notify = spapr_powerdown_req;
561     qemu_register_powerdown_notifier(&spapr->epow_notifier);
562     spapr_rtas_register(RTAS_CHECK_EXCEPTION, "check-exception",
563                         check_exception);
564     spapr_rtas_register(RTAS_EVENT_SCAN, "event-scan", event_scan);
565 }