xref: /openbmc/qemu/hw/ppc/spapr_events.c (revision b23197f9)
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 "qemu/osdep.h"
28 #include "cpu.h"
29 #include "sysemu/sysemu.h"
30 #include "sysemu/char.h"
31 #include "hw/qdev.h"
32 #include "sysemu/device_tree.h"
33 
34 #include "hw/ppc/spapr.h"
35 #include "hw/ppc/spapr_vio.h"
36 #include "hw/pci/pci.h"
37 #include "hw/pci-host/spapr.h"
38 #include "hw/ppc/spapr_drc.h"
39 
40 #include <libfdt.h>
41 
42 struct rtas_error_log {
43     uint32_t summary;
44 #define RTAS_LOG_VERSION_MASK                   0xff000000
45 #define   RTAS_LOG_VERSION_6                    0x06000000
46 #define RTAS_LOG_SEVERITY_MASK                  0x00e00000
47 #define   RTAS_LOG_SEVERITY_ALREADY_REPORTED    0x00c00000
48 #define   RTAS_LOG_SEVERITY_FATAL               0x00a00000
49 #define   RTAS_LOG_SEVERITY_ERROR               0x00800000
50 #define   RTAS_LOG_SEVERITY_ERROR_SYNC          0x00600000
51 #define   RTAS_LOG_SEVERITY_WARNING             0x00400000
52 #define   RTAS_LOG_SEVERITY_EVENT               0x00200000
53 #define   RTAS_LOG_SEVERITY_NO_ERROR            0x00000000
54 #define RTAS_LOG_DISPOSITION_MASK               0x00180000
55 #define   RTAS_LOG_DISPOSITION_FULLY_RECOVERED  0x00000000
56 #define   RTAS_LOG_DISPOSITION_LIMITED_RECOVERY 0x00080000
57 #define   RTAS_LOG_DISPOSITION_NOT_RECOVERED    0x00100000
58 #define RTAS_LOG_OPTIONAL_PART_PRESENT          0x00040000
59 #define RTAS_LOG_INITIATOR_MASK                 0x0000f000
60 #define   RTAS_LOG_INITIATOR_UNKNOWN            0x00000000
61 #define   RTAS_LOG_INITIATOR_CPU                0x00001000
62 #define   RTAS_LOG_INITIATOR_PCI                0x00002000
63 #define   RTAS_LOG_INITIATOR_MEMORY             0x00004000
64 #define   RTAS_LOG_INITIATOR_HOTPLUG            0x00006000
65 #define RTAS_LOG_TARGET_MASK                    0x00000f00
66 #define   RTAS_LOG_TARGET_UNKNOWN               0x00000000
67 #define   RTAS_LOG_TARGET_CPU                   0x00000100
68 #define   RTAS_LOG_TARGET_PCI                   0x00000200
69 #define   RTAS_LOG_TARGET_MEMORY                0x00000400
70 #define   RTAS_LOG_TARGET_HOTPLUG               0x00000600
71 #define RTAS_LOG_TYPE_MASK                      0x000000ff
72 #define   RTAS_LOG_TYPE_OTHER                   0x00000000
73 #define   RTAS_LOG_TYPE_RETRY                   0x00000001
74 #define   RTAS_LOG_TYPE_TCE_ERR                 0x00000002
75 #define   RTAS_LOG_TYPE_INTERN_DEV_FAIL         0x00000003
76 #define   RTAS_LOG_TYPE_TIMEOUT                 0x00000004
77 #define   RTAS_LOG_TYPE_DATA_PARITY             0x00000005
78 #define   RTAS_LOG_TYPE_ADDR_PARITY             0x00000006
79 #define   RTAS_LOG_TYPE_CACHE_PARITY            0x00000007
80 #define   RTAS_LOG_TYPE_ADDR_INVALID            0x00000008
81 #define   RTAS_LOG_TYPE_ECC_UNCORR              0x00000009
82 #define   RTAS_LOG_TYPE_ECC_CORR                0x0000000a
83 #define   RTAS_LOG_TYPE_EPOW                    0x00000040
84 #define   RTAS_LOG_TYPE_HOTPLUG                 0x000000e5
85     uint32_t extended_length;
86 } QEMU_PACKED;
87 
88 struct rtas_event_log_v6 {
89     uint8_t b0;
90 #define RTAS_LOG_V6_B0_VALID                          0x80
91 #define RTAS_LOG_V6_B0_UNRECOVERABLE_ERROR            0x40
92 #define RTAS_LOG_V6_B0_RECOVERABLE_ERROR              0x20
93 #define RTAS_LOG_V6_B0_DEGRADED_OPERATION             0x10
94 #define RTAS_LOG_V6_B0_PREDICTIVE_ERROR               0x08
95 #define RTAS_LOG_V6_B0_NEW_LOG                        0x04
96 #define RTAS_LOG_V6_B0_BIGENDIAN                      0x02
97     uint8_t _resv1;
98     uint8_t b2;
99 #define RTAS_LOG_V6_B2_POWERPC_FORMAT                 0x80
100 #define RTAS_LOG_V6_B2_LOG_FORMAT_MASK                0x0f
101 #define   RTAS_LOG_V6_B2_LOG_FORMAT_PLATFORM_EVENT    0x0e
102     uint8_t _resv2[9];
103     uint32_t company;
104 #define RTAS_LOG_V6_COMPANY_IBM                 0x49424d00 /* IBM<null> */
105 } QEMU_PACKED;
106 
107 struct rtas_event_log_v6_section_header {
108     uint16_t section_id;
109     uint16_t section_length;
110     uint8_t section_version;
111     uint8_t section_subtype;
112     uint16_t creator_component_id;
113 } QEMU_PACKED;
114 
115 struct rtas_event_log_v6_maina {
116 #define RTAS_LOG_V6_SECTION_ID_MAINA                0x5048 /* PH */
117     struct rtas_event_log_v6_section_header hdr;
118     uint32_t creation_date; /* BCD: YYYYMMDD */
119     uint32_t creation_time; /* BCD: HHMMSS00 */
120     uint8_t _platform1[8];
121     char creator_id;
122     uint8_t _resv1[2];
123     uint8_t section_count;
124     uint8_t _resv2[4];
125     uint8_t _platform2[8];
126     uint32_t plid;
127     uint8_t _platform3[4];
128 } QEMU_PACKED;
129 
130 struct rtas_event_log_v6_mainb {
131 #define RTAS_LOG_V6_SECTION_ID_MAINB                0x5548 /* UH */
132     struct rtas_event_log_v6_section_header hdr;
133     uint8_t subsystem_id;
134     uint8_t _platform1;
135     uint8_t event_severity;
136     uint8_t event_subtype;
137     uint8_t _platform2[4];
138     uint8_t _resv1[2];
139     uint16_t action_flags;
140     uint8_t _resv2[4];
141 } QEMU_PACKED;
142 
143 struct rtas_event_log_v6_epow {
144 #define RTAS_LOG_V6_SECTION_ID_EPOW                 0x4550 /* EP */
145     struct rtas_event_log_v6_section_header hdr;
146     uint8_t sensor_value;
147 #define RTAS_LOG_V6_EPOW_ACTION_RESET                    0
148 #define RTAS_LOG_V6_EPOW_ACTION_WARN_COOLING             1
149 #define RTAS_LOG_V6_EPOW_ACTION_WARN_POWER               2
150 #define RTAS_LOG_V6_EPOW_ACTION_SYSTEM_SHUTDOWN          3
151 #define RTAS_LOG_V6_EPOW_ACTION_SYSTEM_HALT              4
152 #define RTAS_LOG_V6_EPOW_ACTION_MAIN_ENCLOSURE           5
153 #define RTAS_LOG_V6_EPOW_ACTION_POWER_OFF                7
154     uint8_t event_modifier;
155 #define RTAS_LOG_V6_EPOW_MODIFIER_NORMAL                 1
156 #define RTAS_LOG_V6_EPOW_MODIFIER_ON_UPS                 2
157 #define RTAS_LOG_V6_EPOW_MODIFIER_CRITICAL               3
158 #define RTAS_LOG_V6_EPOW_MODIFIER_TEMPERATURE            4
159     uint8_t extended_modifier;
160 #define RTAS_LOG_V6_EPOW_XMODIFIER_SYSTEM_WIDE           0
161 #define RTAS_LOG_V6_EPOW_XMODIFIER_PARTITION_SPECIFIC    1
162     uint8_t _resv;
163     uint64_t reason_code;
164 } QEMU_PACKED;
165 
166 struct epow_log_full {
167     struct rtas_error_log hdr;
168     struct rtas_event_log_v6 v6hdr;
169     struct rtas_event_log_v6_maina maina;
170     struct rtas_event_log_v6_mainb mainb;
171     struct rtas_event_log_v6_epow epow;
172 } QEMU_PACKED;
173 
174 struct rtas_event_log_v6_hp {
175 #define RTAS_LOG_V6_SECTION_ID_HOTPLUG              0x4850 /* HP */
176     struct rtas_event_log_v6_section_header hdr;
177     uint8_t hotplug_type;
178 #define RTAS_LOG_V6_HP_TYPE_CPU                          1
179 #define RTAS_LOG_V6_HP_TYPE_MEMORY                       2
180 #define RTAS_LOG_V6_HP_TYPE_SLOT                         3
181 #define RTAS_LOG_V6_HP_TYPE_PHB                          4
182 #define RTAS_LOG_V6_HP_TYPE_PCI                          5
183     uint8_t hotplug_action;
184 #define RTAS_LOG_V6_HP_ACTION_ADD                        1
185 #define RTAS_LOG_V6_HP_ACTION_REMOVE                     2
186     uint8_t hotplug_identifier;
187 #define RTAS_LOG_V6_HP_ID_DRC_NAME                       1
188 #define RTAS_LOG_V6_HP_ID_DRC_INDEX                      2
189 #define RTAS_LOG_V6_HP_ID_DRC_COUNT                      3
190     uint8_t reserved;
191     union {
192         uint32_t index;
193         uint32_t count;
194         char name[1];
195     } drc;
196 } QEMU_PACKED;
197 
198 struct hp_log_full {
199     struct rtas_error_log hdr;
200     struct rtas_event_log_v6 v6hdr;
201     struct rtas_event_log_v6_maina maina;
202     struct rtas_event_log_v6_mainb mainb;
203     struct rtas_event_log_v6_hp hp;
204 } QEMU_PACKED;
205 
206 #define EVENT_MASK_INTERNAL_ERRORS           0x80000000
207 #define EVENT_MASK_EPOW                      0x40000000
208 #define EVENT_MASK_HOTPLUG                   0x10000000
209 #define EVENT_MASK_IO                        0x08000000
210 
211 #define _FDT(exp) \
212     do { \
213         int ret = (exp);                                           \
214         if (ret < 0) {                                             \
215             fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
216                     #exp, fdt_strerror(ret));                      \
217             exit(1);                                               \
218         }                                                          \
219     } while (0)
220 
221 void spapr_events_fdt_skel(void *fdt, uint32_t check_exception_irq)
222 {
223     uint32_t irq_ranges[] = {cpu_to_be32(check_exception_irq), cpu_to_be32(1)};
224     uint32_t interrupts[] = {cpu_to_be32(check_exception_irq), 0};
225 
226     _FDT((fdt_begin_node(fdt, "event-sources")));
227 
228     _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
229     _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2)));
230     _FDT((fdt_property(fdt, "interrupt-ranges",
231                        irq_ranges, sizeof(irq_ranges))));
232 
233     _FDT((fdt_begin_node(fdt, "epow-events")));
234     _FDT((fdt_property(fdt, "interrupts", interrupts, sizeof(interrupts))));
235     _FDT((fdt_end_node(fdt)));
236 
237     _FDT((fdt_end_node(fdt)));
238 }
239 
240 static void rtas_event_log_queue(int log_type, void *data, bool exception)
241 {
242     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
243     sPAPREventLogEntry *entry = g_new(sPAPREventLogEntry, 1);
244 
245     g_assert(data);
246     entry->log_type = log_type;
247     entry->exception = exception;
248     entry->data = data;
249     QTAILQ_INSERT_TAIL(&spapr->pending_events, entry, next);
250 }
251 
252 static sPAPREventLogEntry *rtas_event_log_dequeue(uint32_t event_mask,
253                                                   bool exception)
254 {
255     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
256     sPAPREventLogEntry *entry = NULL;
257 
258     /* we only queue EPOW events atm. */
259     if ((event_mask & EVENT_MASK_EPOW) == 0) {
260         return NULL;
261     }
262 
263     QTAILQ_FOREACH(entry, &spapr->pending_events, next) {
264         if (entry->exception != exception) {
265             continue;
266         }
267 
268         /* EPOW and hotplug events are surfaced in the same manner */
269         if (entry->log_type == RTAS_LOG_TYPE_EPOW ||
270             entry->log_type == RTAS_LOG_TYPE_HOTPLUG) {
271             break;
272         }
273     }
274 
275     if (entry) {
276         QTAILQ_REMOVE(&spapr->pending_events, entry, next);
277     }
278 
279     return entry;
280 }
281 
282 static bool rtas_event_log_contains(uint32_t event_mask, bool exception)
283 {
284     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
285     sPAPREventLogEntry *entry = NULL;
286 
287     /* we only queue EPOW events atm. */
288     if ((event_mask & EVENT_MASK_EPOW) == 0) {
289         return false;
290     }
291 
292     QTAILQ_FOREACH(entry, &spapr->pending_events, next) {
293         if (entry->exception != exception) {
294             continue;
295         }
296 
297         /* EPOW and hotplug events are surfaced in the same manner */
298         if (entry->log_type == RTAS_LOG_TYPE_EPOW ||
299             entry->log_type == RTAS_LOG_TYPE_HOTPLUG) {
300             return true;
301         }
302     }
303 
304     return false;
305 }
306 
307 static uint32_t next_plid;
308 
309 static void spapr_init_v6hdr(struct rtas_event_log_v6 *v6hdr)
310 {
311     v6hdr->b0 = RTAS_LOG_V6_B0_VALID | RTAS_LOG_V6_B0_NEW_LOG
312         | RTAS_LOG_V6_B0_BIGENDIAN;
313     v6hdr->b2 = RTAS_LOG_V6_B2_POWERPC_FORMAT
314         | RTAS_LOG_V6_B2_LOG_FORMAT_PLATFORM_EVENT;
315     v6hdr->company = cpu_to_be32(RTAS_LOG_V6_COMPANY_IBM);
316 }
317 
318 static void spapr_init_maina(struct rtas_event_log_v6_maina *maina,
319                              int section_count)
320 {
321     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
322     struct tm tm;
323     int year;
324 
325     maina->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_MAINA);
326     maina->hdr.section_length = cpu_to_be16(sizeof(*maina));
327     /* FIXME: section version, subtype and creator id? */
328     spapr_rtc_read(spapr->rtc, &tm, NULL);
329     year = tm.tm_year + 1900;
330     maina->creation_date = cpu_to_be32((to_bcd(year / 100) << 24)
331                                        | (to_bcd(year % 100) << 16)
332                                        | (to_bcd(tm.tm_mon + 1) << 8)
333                                        | to_bcd(tm.tm_mday));
334     maina->creation_time = cpu_to_be32((to_bcd(tm.tm_hour) << 24)
335                                        | (to_bcd(tm.tm_min) << 16)
336                                        | (to_bcd(tm.tm_sec) << 8));
337     maina->creator_id = 'H'; /* Hypervisor */
338     maina->section_count = section_count;
339     maina->plid = next_plid++;
340 }
341 
342 static void spapr_powerdown_req(Notifier *n, void *opaque)
343 {
344     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
345     struct rtas_error_log *hdr;
346     struct rtas_event_log_v6 *v6hdr;
347     struct rtas_event_log_v6_maina *maina;
348     struct rtas_event_log_v6_mainb *mainb;
349     struct rtas_event_log_v6_epow *epow;
350     struct epow_log_full *new_epow;
351 
352     new_epow = g_malloc0(sizeof(*new_epow));
353     hdr = &new_epow->hdr;
354     v6hdr = &new_epow->v6hdr;
355     maina = &new_epow->maina;
356     mainb = &new_epow->mainb;
357     epow = &new_epow->epow;
358 
359     hdr->summary = cpu_to_be32(RTAS_LOG_VERSION_6
360                                | RTAS_LOG_SEVERITY_EVENT
361                                | RTAS_LOG_DISPOSITION_NOT_RECOVERED
362                                | RTAS_LOG_OPTIONAL_PART_PRESENT
363                                | RTAS_LOG_TYPE_EPOW);
364     hdr->extended_length = cpu_to_be32(sizeof(*new_epow)
365                                        - sizeof(new_epow->hdr));
366 
367     spapr_init_v6hdr(v6hdr);
368     spapr_init_maina(maina, 3 /* Main-A, Main-B and EPOW */);
369 
370     mainb->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_MAINB);
371     mainb->hdr.section_length = cpu_to_be16(sizeof(*mainb));
372     /* FIXME: section version, subtype and creator id? */
373     mainb->subsystem_id = 0xa0; /* External environment */
374     mainb->event_severity = 0x00; /* Informational / non-error */
375     mainb->event_subtype = 0xd0; /* Normal shutdown */
376 
377     epow->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_EPOW);
378     epow->hdr.section_length = cpu_to_be16(sizeof(*epow));
379     epow->hdr.section_version = 2; /* includes extended modifier */
380     /* FIXME: section subtype and creator id? */
381     epow->sensor_value = RTAS_LOG_V6_EPOW_ACTION_SYSTEM_SHUTDOWN;
382     epow->event_modifier = RTAS_LOG_V6_EPOW_MODIFIER_NORMAL;
383     epow->extended_modifier = RTAS_LOG_V6_EPOW_XMODIFIER_PARTITION_SPECIFIC;
384 
385     rtas_event_log_queue(RTAS_LOG_TYPE_EPOW, new_epow, true);
386 
387     qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
388 }
389 
390 static void spapr_hotplug_req_event(uint8_t hp_id, uint8_t hp_action,
391                                     sPAPRDRConnectorType drc_type,
392                                     uint32_t drc)
393 {
394     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
395     struct hp_log_full *new_hp;
396     struct rtas_error_log *hdr;
397     struct rtas_event_log_v6 *v6hdr;
398     struct rtas_event_log_v6_maina *maina;
399     struct rtas_event_log_v6_mainb *mainb;
400     struct rtas_event_log_v6_hp *hp;
401 
402     new_hp = g_malloc0(sizeof(struct hp_log_full));
403     hdr = &new_hp->hdr;
404     v6hdr = &new_hp->v6hdr;
405     maina = &new_hp->maina;
406     mainb = &new_hp->mainb;
407     hp = &new_hp->hp;
408 
409     hdr->summary = cpu_to_be32(RTAS_LOG_VERSION_6
410                                | RTAS_LOG_SEVERITY_EVENT
411                                | RTAS_LOG_DISPOSITION_NOT_RECOVERED
412                                | RTAS_LOG_OPTIONAL_PART_PRESENT
413                                | RTAS_LOG_INITIATOR_HOTPLUG
414                                | RTAS_LOG_TYPE_HOTPLUG);
415     hdr->extended_length = cpu_to_be32(sizeof(*new_hp)
416                                        - sizeof(new_hp->hdr));
417 
418     spapr_init_v6hdr(v6hdr);
419     spapr_init_maina(maina, 3 /* Main-A, Main-B, HP */);
420 
421     mainb->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_MAINB);
422     mainb->hdr.section_length = cpu_to_be16(sizeof(*mainb));
423     mainb->subsystem_id = 0x80; /* External environment */
424     mainb->event_severity = 0x00; /* Informational / non-error */
425     mainb->event_subtype = 0x00; /* Normal shutdown */
426 
427     hp->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_HOTPLUG);
428     hp->hdr.section_length = cpu_to_be16(sizeof(*hp));
429     hp->hdr.section_version = 1; /* includes extended modifier */
430     hp->hotplug_action = hp_action;
431     hp->hotplug_identifier = hp_id;
432 
433     switch (drc_type) {
434     case SPAPR_DR_CONNECTOR_TYPE_PCI:
435         hp->hotplug_type = RTAS_LOG_V6_HP_TYPE_PCI;
436         break;
437     case SPAPR_DR_CONNECTOR_TYPE_LMB:
438         hp->hotplug_type = RTAS_LOG_V6_HP_TYPE_MEMORY;
439         break;
440     default:
441         /* we shouldn't be signaling hotplug events for resources
442          * that don't support them
443          */
444         g_assert(false);
445         return;
446     }
447 
448     if (hp_id == RTAS_LOG_V6_HP_ID_DRC_COUNT) {
449         hp->drc.count = cpu_to_be32(drc);
450     } else if (hp_id == RTAS_LOG_V6_HP_ID_DRC_INDEX) {
451         hp->drc.index = cpu_to_be32(drc);
452     }
453 
454     rtas_event_log_queue(RTAS_LOG_TYPE_HOTPLUG, new_hp, true);
455 
456     qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
457 }
458 
459 void spapr_hotplug_req_add_by_index(sPAPRDRConnector *drc)
460 {
461     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
462     sPAPRDRConnectorType drc_type = drck->get_type(drc);
463     uint32_t index = drck->get_index(drc);
464 
465     spapr_hotplug_req_event(RTAS_LOG_V6_HP_ID_DRC_INDEX,
466                             RTAS_LOG_V6_HP_ACTION_ADD, drc_type, index);
467 }
468 
469 void spapr_hotplug_req_remove_by_index(sPAPRDRConnector *drc)
470 {
471     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
472     sPAPRDRConnectorType drc_type = drck->get_type(drc);
473     uint32_t index = drck->get_index(drc);
474 
475     spapr_hotplug_req_event(RTAS_LOG_V6_HP_ID_DRC_INDEX,
476                             RTAS_LOG_V6_HP_ACTION_REMOVE, drc_type, index);
477 }
478 
479 void spapr_hotplug_req_add_by_count(sPAPRDRConnectorType drc_type,
480                                        uint32_t count)
481 {
482     spapr_hotplug_req_event(RTAS_LOG_V6_HP_ID_DRC_COUNT,
483                             RTAS_LOG_V6_HP_ACTION_ADD, drc_type, count);
484 }
485 
486 void spapr_hotplug_req_remove_by_count(sPAPRDRConnectorType drc_type,
487                                           uint32_t count)
488 {
489     spapr_hotplug_req_event(RTAS_LOG_V6_HP_ID_DRC_COUNT,
490                             RTAS_LOG_V6_HP_ACTION_REMOVE, drc_type, count);
491 }
492 
493 static void check_exception(PowerPCCPU *cpu, sPAPRMachineState *spapr,
494                             uint32_t token, uint32_t nargs,
495                             target_ulong args,
496                             uint32_t nret, target_ulong rets)
497 {
498     uint32_t mask, buf, len, event_len;
499     uint64_t xinfo;
500     sPAPREventLogEntry *event;
501     struct rtas_error_log *hdr;
502 
503     if ((nargs < 6) || (nargs > 7) || nret != 1) {
504         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
505         return;
506     }
507 
508     xinfo = rtas_ld(args, 1);
509     mask = rtas_ld(args, 2);
510     buf = rtas_ld(args, 4);
511     len = rtas_ld(args, 5);
512     if (nargs == 7) {
513         xinfo |= (uint64_t)rtas_ld(args, 6) << 32;
514     }
515 
516     event = rtas_event_log_dequeue(mask, true);
517     if (!event) {
518         goto out_no_events;
519     }
520 
521     hdr = event->data;
522     event_len = be32_to_cpu(hdr->extended_length) + sizeof(*hdr);
523 
524     if (event_len < len) {
525         len = event_len;
526     }
527 
528     cpu_physical_memory_write(buf, event->data, len);
529     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
530     g_free(event->data);
531     g_free(event);
532 
533     /* according to PAPR+, the IRQ must be left asserted, or re-asserted, if
534      * there are still pending events to be fetched via check-exception. We
535      * do the latter here, since our code relies on edge-triggered
536      * interrupts.
537      */
538     if (rtas_event_log_contains(mask, true)) {
539         qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
540     }
541 
542     return;
543 
544 out_no_events:
545     rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND);
546 }
547 
548 static void event_scan(PowerPCCPU *cpu, sPAPRMachineState *spapr,
549                        uint32_t token, uint32_t nargs,
550                        target_ulong args,
551                        uint32_t nret, target_ulong rets)
552 {
553     uint32_t mask, buf, len, event_len;
554     sPAPREventLogEntry *event;
555     struct rtas_error_log *hdr;
556 
557     if (nargs != 4 || nret != 1) {
558         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
559         return;
560     }
561 
562     mask = rtas_ld(args, 0);
563     buf = rtas_ld(args, 2);
564     len = rtas_ld(args, 3);
565 
566     event = rtas_event_log_dequeue(mask, false);
567     if (!event) {
568         goto out_no_events;
569     }
570 
571     hdr = event->data;
572     event_len = be32_to_cpu(hdr->extended_length) + sizeof(*hdr);
573 
574     if (event_len < len) {
575         len = event_len;
576     }
577 
578     cpu_physical_memory_write(buf, event->data, len);
579     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
580     g_free(event->data);
581     g_free(event);
582     return;
583 
584 out_no_events:
585     rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND);
586 }
587 
588 void spapr_events_init(sPAPRMachineState *spapr)
589 {
590     QTAILQ_INIT(&spapr->pending_events);
591     spapr->check_exception_irq = xics_alloc(spapr->icp, 0, 0, false,
592                                             &error_fatal);
593     spapr->epow_notifier.notify = spapr_powerdown_req;
594     qemu_register_powerdown_notifier(&spapr->epow_notifier);
595     spapr_rtas_register(RTAS_CHECK_EXCEPTION, "check-exception",
596                         check_exception);
597     spapr_rtas_register(RTAS_EVENT_SCAN, "event-scan", event_scan);
598 }
599