]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr_events.c
spapr: Revert to memory@XXXX representation for non-hotplugged memory
[mirror_qemu.git] / hw / ppc / spapr_events.c
CommitLineData
74d042e5
DG
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"
9c17d615 28#include "sysemu/sysemu.h"
dccfcd0e 29#include "sysemu/char.h"
74d042e5 30#include "hw/qdev.h"
9c17d615 31#include "sysemu/device_tree.h"
74d042e5 32
0d09e41a
PB
33#include "hw/ppc/spapr.h"
34#include "hw/ppc/spapr_vio.h"
31fe14d1
NF
35#include "hw/pci/pci.h"
36#include "hw/pci-host/spapr.h"
37#include "hw/ppc/spapr_drc.h"
74d042e5
DG
38
39#include <libfdt.h>
40
41struct 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
31fe14d1 83#define RTAS_LOG_TYPE_HOTPLUG 0x000000e5
74d042e5
DG
84 uint32_t extended_length;
85} QEMU_PACKED;
86
87struct 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
106struct 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
114struct 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
129struct 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
142struct 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
165struct 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
31fe14d1
NF
173struct 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
197struct 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
74d042e5
DG
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
31fe14d1 220void spapr_events_fdt_skel(void *fdt, uint32_t check_exception_irq)
74d042e5 221{
31fe14d1
NF
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};
74d042e5
DG
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",
31fe14d1 230 irq_ranges, sizeof(irq_ranges))));
74d042e5
DG
231
232 _FDT((fdt_begin_node(fdt, "epow-events")));
31fe14d1 233 _FDT((fdt_property(fdt, "interrupts", interrupts, sizeof(interrupts))));
74d042e5
DG
234 _FDT((fdt_end_node(fdt)));
235
236 _FDT((fdt_end_node(fdt)));
237}
238
79853e18 239static void rtas_event_log_queue(int log_type, void *data, bool exception)
31fe14d1 240{
28e02042 241 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
31fe14d1 242 sPAPREventLogEntry *entry = g_new(sPAPREventLogEntry, 1);
74d042e5 243
31fe14d1
NF
244 g_assert(data);
245 entry->log_type = log_type;
79853e18 246 entry->exception = exception;
31fe14d1
NF
247 entry->data = data;
248 QTAILQ_INSERT_TAIL(&spapr->pending_events, entry, next);
249}
250
79853e18
TD
251static sPAPREventLogEntry *rtas_event_log_dequeue(uint32_t event_mask,
252 bool exception)
74d042e5 253{
28e02042 254 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
31fe14d1 255 sPAPREventLogEntry *entry = NULL;
74d042e5 256
31fe14d1
NF
257 /* we only queue EPOW events atm. */
258 if ((event_mask & EVENT_MASK_EPOW) == 0) {
259 return NULL;
74d042e5 260 }
74d042e5 261
31fe14d1 262 QTAILQ_FOREACH(entry, &spapr->pending_events, next) {
79853e18
TD
263 if (entry->exception != exception) {
264 continue;
265 }
266
31fe14d1
NF
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
79853e18 281static bool rtas_event_log_contains(uint32_t event_mask, bool exception)
31fe14d1 282{
28e02042 283 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
31fe14d1
NF
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) {
79853e18
TD
292 if (entry->exception != exception) {
293 continue;
294 }
295
31fe14d1
NF
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}
74d042e5 305
31fe14d1
NF
306static uint32_t next_plid;
307
308static void spapr_init_v6hdr(struct rtas_event_log_v6 *v6hdr)
309{
74d042e5
DG
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);
31fe14d1
NF
315}
316
317static void spapr_init_maina(struct rtas_event_log_v6_maina *maina,
318 int section_count)
319{
28e02042 320 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
31fe14d1
NF
321 struct tm tm;
322 int year;
74d042e5
DG
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? */
28df36a1 327 spapr_rtc_read(spapr->rtc, &tm, NULL);
74d042e5
DG
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 */
31fe14d1 337 maina->section_count = section_count;
74d042e5 338 maina->plid = next_plid++;
31fe14d1
NF
339}
340
341static void spapr_powerdown_req(Notifier *n, void *opaque)
342{
28e02042 343 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
31fe14d1
NF
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 */);
74d042e5
DG
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
79853e18 384 rtas_event_log_queue(RTAS_LOG_TYPE_EPOW, new_epow, true);
31fe14d1
NF
385
386 qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
387}
388
389static void spapr_hotplug_req_event(sPAPRDRConnector *drc, uint8_t hp_action)
390{
28e02042 391 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
31fe14d1
NF
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;
c20d332a
BR
430 hp->drc.index = cpu_to_be32(drck->get_index(drc));
431 hp->hotplug_identifier = RTAS_LOG_V6_HP_ID_DRC_INDEX;
31fe14d1
NF
432
433 switch (drc_type) {
434 case SPAPR_DR_CONNECTOR_TYPE_PCI:
31fe14d1
NF
435 hp->hotplug_type = RTAS_LOG_V6_HP_TYPE_PCI;
436 break;
c20d332a
BR
437 case SPAPR_DR_CONNECTOR_TYPE_LMB:
438 hp->hotplug_type = RTAS_LOG_V6_HP_TYPE_MEMORY;
439 break;
31fe14d1
NF
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
79853e18 448 rtas_event_log_queue(RTAS_LOG_TYPE_HOTPLUG, new_hp, true);
31fe14d1
NF
449
450 qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
451}
452
453void spapr_hotplug_req_add_event(sPAPRDRConnector *drc)
454{
455 spapr_hotplug_req_event(drc, RTAS_LOG_V6_HP_ACTION_ADD);
456}
457
458void spapr_hotplug_req_remove_event(sPAPRDRConnector *drc)
459{
460 spapr_hotplug_req_event(drc, RTAS_LOG_V6_HP_ACTION_REMOVE);
74d042e5
DG
461}
462
28e02042 463static void check_exception(PowerPCCPU *cpu, sPAPRMachineState *spapr,
74d042e5
DG
464 uint32_t token, uint32_t nargs,
465 target_ulong args,
466 uint32_t nret, target_ulong rets)
467{
31fe14d1 468 uint32_t mask, buf, len, event_len;
74d042e5 469 uint64_t xinfo;
31fe14d1
NF
470 sPAPREventLogEntry *event;
471 struct rtas_error_log *hdr;
74d042e5
DG
472
473 if ((nargs < 6) || (nargs > 7) || nret != 1) {
a64d325d 474 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
74d042e5
DG
475 return;
476 }
477
478 xinfo = rtas_ld(args, 1);
479 mask = rtas_ld(args, 2);
480 buf = rtas_ld(args, 4);
481 len = rtas_ld(args, 5);
482 if (nargs == 7) {
483 xinfo |= (uint64_t)rtas_ld(args, 6) << 32;
484 }
485
79853e18 486 event = rtas_event_log_dequeue(mask, true);
31fe14d1
NF
487 if (!event) {
488 goto out_no_events;
489 }
74d042e5 490
31fe14d1
NF
491 hdr = event->data;
492 event_len = be32_to_cpu(hdr->extended_length) + sizeof(*hdr);
493
494 if (event_len < len) {
495 len = event_len;
496 }
497
498 cpu_physical_memory_write(buf, event->data, len);
499 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
500 g_free(event->data);
501 g_free(event);
502
503 /* according to PAPR+, the IRQ must be left asserted, or re-asserted, if
504 * there are still pending events to be fetched via check-exception. We
505 * do the latter here, since our code relies on edge-triggered
506 * interrupts.
507 */
79853e18 508 if (rtas_event_log_contains(mask, true)) {
31fe14d1 509 qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->check_exception_irq));
74d042e5 510 }
31fe14d1
NF
511
512 return;
513
514out_no_events:
515 rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND);
74d042e5
DG
516}
517
28e02042 518static void event_scan(PowerPCCPU *cpu, sPAPRMachineState *spapr,
79853e18
TD
519 uint32_t token, uint32_t nargs,
520 target_ulong args,
521 uint32_t nret, target_ulong rets)
522{
523 uint32_t mask, buf, len, event_len;
524 sPAPREventLogEntry *event;
525 struct rtas_error_log *hdr;
526
527 if (nargs != 4 || nret != 1) {
528 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
529 return;
530 }
531
532 mask = rtas_ld(args, 0);
533 buf = rtas_ld(args, 2);
534 len = rtas_ld(args, 3);
535
536 event = rtas_event_log_dequeue(mask, false);
537 if (!event) {
538 goto out_no_events;
539 }
540
541 hdr = event->data;
542 event_len = be32_to_cpu(hdr->extended_length) + sizeof(*hdr);
543
544 if (event_len < len) {
545 len = event_len;
546 }
547
548 cpu_physical_memory_write(buf, event->data, len);
549 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
550 g_free(event->data);
551 g_free(event);
552 return;
553
554out_no_events:
555 rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND);
556}
557
28e02042 558void spapr_events_init(sPAPRMachineState *spapr)
74d042e5 559{
31fe14d1
NF
560 QTAILQ_INIT(&spapr->pending_events);
561 spapr->check_exception_irq = xics_alloc(spapr->icp, 0, 0, false);
74d042e5
DG
562 spapr->epow_notifier.notify = spapr_powerdown_req;
563 qemu_register_powerdown_notifier(&spapr->epow_notifier);
3a3b8502
AK
564 spapr_rtas_register(RTAS_CHECK_EXCEPTION, "check-exception",
565 check_exception);
79853e18 566 spapr_rtas_register(RTAS_EVENT_SCAN, "event-scan", event_scan);
74d042e5 567}