xref: /openbmc/qemu/hw/ppc/spapr_events.c (revision 1fd6bb44)
1 /*
2  * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
3  *
4  * RTAS events handling
5  *
6  * Copyright (c) 2012 David Gibson, IBM Corporation.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  *
26  */
27 #include "cpu.h"
28 #include "sysemu/sysemu.h"
29 #include "char/char.h"
30 #include "hw/qdev.h"
31 #include "sysemu/device_tree.h"
32 
33 #include "hw/ppc/spapr.h"
34 #include "hw/ppc/spapr_vio.h"
35 
36 #include <libfdt.h>
37 
38 struct rtas_error_log {
39     uint32_t summary;
40 #define RTAS_LOG_VERSION_MASK                   0xff000000
41 #define   RTAS_LOG_VERSION_6                    0x06000000
42 #define RTAS_LOG_SEVERITY_MASK                  0x00e00000
43 #define   RTAS_LOG_SEVERITY_ALREADY_REPORTED    0x00c00000
44 #define   RTAS_LOG_SEVERITY_FATAL               0x00a00000
45 #define   RTAS_LOG_SEVERITY_ERROR               0x00800000
46 #define   RTAS_LOG_SEVERITY_ERROR_SYNC          0x00600000
47 #define   RTAS_LOG_SEVERITY_WARNING             0x00400000
48 #define   RTAS_LOG_SEVERITY_EVENT               0x00200000
49 #define   RTAS_LOG_SEVERITY_NO_ERROR            0x00000000
50 #define RTAS_LOG_DISPOSITION_MASK               0x00180000
51 #define   RTAS_LOG_DISPOSITION_FULLY_RECOVERED  0x00000000
52 #define   RTAS_LOG_DISPOSITION_LIMITED_RECOVERY 0x00080000
53 #define   RTAS_LOG_DISPOSITION_NOT_RECOVERED    0x00100000
54 #define RTAS_LOG_OPTIONAL_PART_PRESENT          0x00040000
55 #define RTAS_LOG_INITIATOR_MASK                 0x0000f000
56 #define   RTAS_LOG_INITIATOR_UNKNOWN            0x00000000
57 #define   RTAS_LOG_INITIATOR_CPU                0x00001000
58 #define   RTAS_LOG_INITIATOR_PCI                0x00002000
59 #define   RTAS_LOG_INITIATOR_MEMORY             0x00004000
60 #define   RTAS_LOG_INITIATOR_HOTPLUG            0x00006000
61 #define RTAS_LOG_TARGET_MASK                    0x00000f00
62 #define   RTAS_LOG_TARGET_UNKNOWN               0x00000000
63 #define   RTAS_LOG_TARGET_CPU                   0x00000100
64 #define   RTAS_LOG_TARGET_PCI                   0x00000200
65 #define   RTAS_LOG_TARGET_MEMORY                0x00000400
66 #define   RTAS_LOG_TARGET_HOTPLUG               0x00000600
67 #define RTAS_LOG_TYPE_MASK                      0x000000ff
68 #define   RTAS_LOG_TYPE_OTHER                   0x00000000
69 #define   RTAS_LOG_TYPE_RETRY                   0x00000001
70 #define   RTAS_LOG_TYPE_TCE_ERR                 0x00000002
71 #define   RTAS_LOG_TYPE_INTERN_DEV_FAIL         0x00000003
72 #define   RTAS_LOG_TYPE_TIMEOUT                 0x00000004
73 #define   RTAS_LOG_TYPE_DATA_PARITY             0x00000005
74 #define   RTAS_LOG_TYPE_ADDR_PARITY             0x00000006
75 #define   RTAS_LOG_TYPE_CACHE_PARITY            0x00000007
76 #define   RTAS_LOG_TYPE_ADDR_INVALID            0x00000008
77 #define   RTAS_LOG_TYPE_ECC_UNCORR              0x00000009
78 #define   RTAS_LOG_TYPE_ECC_CORR                0x0000000a
79 #define   RTAS_LOG_TYPE_EPOW                    0x00000040
80     uint32_t extended_length;
81 } QEMU_PACKED;
82 
83 struct rtas_event_log_v6 {
84     uint8_t b0;
85 #define RTAS_LOG_V6_B0_VALID                          0x80
86 #define RTAS_LOG_V6_B0_UNRECOVERABLE_ERROR            0x40
87 #define RTAS_LOG_V6_B0_RECOVERABLE_ERROR              0x20
88 #define RTAS_LOG_V6_B0_DEGRADED_OPERATION             0x10
89 #define RTAS_LOG_V6_B0_PREDICTIVE_ERROR               0x08
90 #define RTAS_LOG_V6_B0_NEW_LOG                        0x04
91 #define RTAS_LOG_V6_B0_BIGENDIAN                      0x02
92     uint8_t _resv1;
93     uint8_t b2;
94 #define RTAS_LOG_V6_B2_POWERPC_FORMAT                 0x80
95 #define RTAS_LOG_V6_B2_LOG_FORMAT_MASK                0x0f
96 #define   RTAS_LOG_V6_B2_LOG_FORMAT_PLATFORM_EVENT    0x0e
97     uint8_t _resv2[9];
98     uint32_t company;
99 #define RTAS_LOG_V6_COMPANY_IBM                 0x49424d00 /* IBM<null> */
100 } QEMU_PACKED;
101 
102 struct rtas_event_log_v6_section_header {
103     uint16_t section_id;
104     uint16_t section_length;
105     uint8_t section_version;
106     uint8_t section_subtype;
107     uint16_t creator_component_id;
108 } QEMU_PACKED;
109 
110 struct rtas_event_log_v6_maina {
111 #define RTAS_LOG_V6_SECTION_ID_MAINA                0x5048 /* PH */
112     struct rtas_event_log_v6_section_header hdr;
113     uint32_t creation_date; /* BCD: YYYYMMDD */
114     uint32_t creation_time; /* BCD: HHMMSS00 */
115     uint8_t _platform1[8];
116     char creator_id;
117     uint8_t _resv1[2];
118     uint8_t section_count;
119     uint8_t _resv2[4];
120     uint8_t _platform2[8];
121     uint32_t plid;
122     uint8_t _platform3[4];
123 } QEMU_PACKED;
124 
125 struct rtas_event_log_v6_mainb {
126 #define RTAS_LOG_V6_SECTION_ID_MAINB                0x5548 /* UH */
127     struct rtas_event_log_v6_section_header hdr;
128     uint8_t subsystem_id;
129     uint8_t _platform1;
130     uint8_t event_severity;
131     uint8_t event_subtype;
132     uint8_t _platform2[4];
133     uint8_t _resv1[2];
134     uint16_t action_flags;
135     uint8_t _resv2[4];
136 } QEMU_PACKED;
137 
138 struct rtas_event_log_v6_epow {
139 #define RTAS_LOG_V6_SECTION_ID_EPOW                 0x4550 /* EP */
140     struct rtas_event_log_v6_section_header hdr;
141     uint8_t sensor_value;
142 #define RTAS_LOG_V6_EPOW_ACTION_RESET                    0
143 #define RTAS_LOG_V6_EPOW_ACTION_WARN_COOLING             1
144 #define RTAS_LOG_V6_EPOW_ACTION_WARN_POWER               2
145 #define RTAS_LOG_V6_EPOW_ACTION_SYSTEM_SHUTDOWN          3
146 #define RTAS_LOG_V6_EPOW_ACTION_SYSTEM_HALT              4
147 #define RTAS_LOG_V6_EPOW_ACTION_MAIN_ENCLOSURE           5
148 #define RTAS_LOG_V6_EPOW_ACTION_POWER_OFF                7
149     uint8_t event_modifier;
150 #define RTAS_LOG_V6_EPOW_MODIFIER_NORMAL                 1
151 #define RTAS_LOG_V6_EPOW_MODIFIER_ON_UPS                 2
152 #define RTAS_LOG_V6_EPOW_MODIFIER_CRITICAL               3
153 #define RTAS_LOG_V6_EPOW_MODIFIER_TEMPERATURE            4
154     uint8_t extended_modifier;
155 #define RTAS_LOG_V6_EPOW_XMODIFIER_SYSTEM_WIDE           0
156 #define RTAS_LOG_V6_EPOW_XMODIFIER_PARTITION_SPECIFIC    1
157     uint8_t _resv;
158     uint64_t reason_code;
159 } QEMU_PACKED;
160 
161 struct epow_log_full {
162     struct rtas_error_log hdr;
163     struct rtas_event_log_v6 v6hdr;
164     struct rtas_event_log_v6_maina maina;
165     struct rtas_event_log_v6_mainb mainb;
166     struct rtas_event_log_v6_epow epow;
167 } QEMU_PACKED;
168 
169 #define EVENT_MASK_INTERNAL_ERRORS           0x80000000
170 #define EVENT_MASK_EPOW                      0x40000000
171 #define EVENT_MASK_HOTPLUG                   0x10000000
172 #define EVENT_MASK_IO                        0x08000000
173 
174 #define _FDT(exp) \
175     do { \
176         int ret = (exp);                                           \
177         if (ret < 0) {                                             \
178             fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
179                     #exp, fdt_strerror(ret));                      \
180             exit(1);                                               \
181         }                                                          \
182     } while (0)
183 
184 void spapr_events_fdt_skel(void *fdt, uint32_t epow_irq)
185 {
186     uint32_t epow_irq_ranges[] = {cpu_to_be32(epow_irq), cpu_to_be32(1)};
187     uint32_t epow_interrupts[] = {cpu_to_be32(epow_irq), 0};
188 
189     _FDT((fdt_begin_node(fdt, "event-sources")));
190 
191     _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
192     _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2)));
193     _FDT((fdt_property(fdt, "interrupt-ranges",
194                        epow_irq_ranges, sizeof(epow_irq_ranges))));
195 
196     _FDT((fdt_begin_node(fdt, "epow-events")));
197     _FDT((fdt_property(fdt, "interrupts",
198                        epow_interrupts, sizeof(epow_interrupts))));
199     _FDT((fdt_end_node(fdt)));
200 
201     _FDT((fdt_end_node(fdt)));
202 }
203 
204 static struct epow_log_full *pending_epow;
205 static uint32_t next_plid;
206 
207 static void spapr_powerdown_req(Notifier *n, void *opaque)
208 {
209     sPAPREnvironment *spapr = container_of(n, sPAPREnvironment, epow_notifier);
210     struct rtas_error_log *hdr;
211     struct rtas_event_log_v6 *v6hdr;
212     struct rtas_event_log_v6_maina *maina;
213     struct rtas_event_log_v6_mainb *mainb;
214     struct rtas_event_log_v6_epow *epow;
215     struct tm tm;
216     int year;
217 
218     if (pending_epow) {
219         /* For now, we just throw away earlier events if two come
220          * along before any are consumed.  This is sufficient for our
221          * powerdown messages, but we'll need more if we do more
222          * general error/event logging */
223         g_free(pending_epow);
224     }
225     pending_epow = g_malloc0(sizeof(*pending_epow));
226     hdr = &pending_epow->hdr;
227     v6hdr = &pending_epow->v6hdr;
228     maina = &pending_epow->maina;
229     mainb = &pending_epow->mainb;
230     epow = &pending_epow->epow;
231 
232     hdr->summary = cpu_to_be32(RTAS_LOG_VERSION_6
233                                | RTAS_LOG_SEVERITY_EVENT
234                                | RTAS_LOG_DISPOSITION_NOT_RECOVERED
235                                | RTAS_LOG_OPTIONAL_PART_PRESENT
236                                | RTAS_LOG_TYPE_EPOW);
237     hdr->extended_length = cpu_to_be32(sizeof(*pending_epow)
238                                        - sizeof(pending_epow->hdr));
239 
240     v6hdr->b0 = RTAS_LOG_V6_B0_VALID | RTAS_LOG_V6_B0_NEW_LOG
241         | RTAS_LOG_V6_B0_BIGENDIAN;
242     v6hdr->b2 = RTAS_LOG_V6_B2_POWERPC_FORMAT
243         | RTAS_LOG_V6_B2_LOG_FORMAT_PLATFORM_EVENT;
244     v6hdr->company = cpu_to_be32(RTAS_LOG_V6_COMPANY_IBM);
245 
246     maina->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_MAINA);
247     maina->hdr.section_length = cpu_to_be16(sizeof(*maina));
248     /* FIXME: section version, subtype and creator id? */
249     qemu_get_timedate(&tm, spapr->rtc_offset);
250     year = tm.tm_year + 1900;
251     maina->creation_date = cpu_to_be32((to_bcd(year / 100) << 24)
252                                        | (to_bcd(year % 100) << 16)
253                                        | (to_bcd(tm.tm_mon + 1) << 8)
254                                        | to_bcd(tm.tm_mday));
255     maina->creation_time = cpu_to_be32((to_bcd(tm.tm_hour) << 24)
256                                        | (to_bcd(tm.tm_min) << 16)
257                                        | (to_bcd(tm.tm_sec) << 8));
258     maina->creator_id = 'H'; /* Hypervisor */
259     maina->section_count = 3; /* Main-A, Main-B and EPOW */
260     maina->plid = next_plid++;
261 
262     mainb->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_MAINB);
263     mainb->hdr.section_length = cpu_to_be16(sizeof(*mainb));
264     /* FIXME: section version, subtype and creator id? */
265     mainb->subsystem_id = 0xa0; /* External environment */
266     mainb->event_severity = 0x00; /* Informational / non-error */
267     mainb->event_subtype = 0xd0; /* Normal shutdown */
268 
269     epow->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_EPOW);
270     epow->hdr.section_length = cpu_to_be16(sizeof(*epow));
271     epow->hdr.section_version = 2; /* includes extended modifier */
272     /* FIXME: section subtype and creator id? */
273     epow->sensor_value = RTAS_LOG_V6_EPOW_ACTION_SYSTEM_SHUTDOWN;
274     epow->event_modifier = RTAS_LOG_V6_EPOW_MODIFIER_NORMAL;
275     epow->extended_modifier = RTAS_LOG_V6_EPOW_XMODIFIER_PARTITION_SPECIFIC;
276 
277     qemu_irq_pulse(xics_get_qirq(spapr->icp, spapr->epow_irq));
278 }
279 
280 static void check_exception(sPAPREnvironment *spapr,
281                             uint32_t token, uint32_t nargs,
282                             target_ulong args,
283                             uint32_t nret, target_ulong rets)
284 {
285     uint32_t mask, buf, len;
286     uint64_t xinfo;
287 
288     if ((nargs < 6) || (nargs > 7) || nret != 1) {
289         rtas_st(rets, 0, -3);
290         return;
291     }
292 
293     xinfo = rtas_ld(args, 1);
294     mask = rtas_ld(args, 2);
295     buf = rtas_ld(args, 4);
296     len = rtas_ld(args, 5);
297     if (nargs == 7) {
298         xinfo |= (uint64_t)rtas_ld(args, 6) << 32;
299     }
300 
301     if ((mask & EVENT_MASK_EPOW) && pending_epow) {
302         if (sizeof(*pending_epow) < len) {
303             len = sizeof(*pending_epow);
304         }
305 
306         cpu_physical_memory_write(buf, pending_epow, len);
307         g_free(pending_epow);
308         pending_epow = NULL;
309         rtas_st(rets, 0, 0);
310     } else {
311         rtas_st(rets, 0, 1);
312     }
313 }
314 
315 void spapr_events_init(sPAPREnvironment *spapr)
316 {
317     spapr->epow_irq = spapr_allocate_msi(0);
318     spapr->epow_notifier.notify = spapr_powerdown_req;
319     qemu_register_powerdown_notifier(&spapr->epow_notifier);
320     spapr_rtas_register("check-exception", check_exception);
321 }
322