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