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