apei_erst.c revision 1.1 1 /* $NetBSD: apei_erst.c,v 1.1 2024/03/20 17:11:43 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2024 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * APEI ERST -- Error Record Serialization Table
31 *
32 * https://uefi.org/specs/ACPI/6.5/18_Platform_Error_Interfaces.html#error-serialization
33 *
34 * XXX Expose this through a /dev node with ioctls and/or through a
35 * file system.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: apei_erst.c,v 1.1 2024/03/20 17:11:43 riastradh Exp $");
40
41 #include <sys/param.h>
42 #include <sys/types.h>
43
44 #include <sys/systm.h>
45
46 #include <dev/acpi/acpivar.h>
47 #include <dev/acpi/apei_erstvar.h>
48 #include <dev/acpi/apei_interp.h>
49 #include <dev/acpi/apei_reg.h>
50 #include <dev/acpi/apeivar.h>
51
52 #define _COMPONENT ACPI_RESOURCE_COMPONENT
53 ACPI_MODULE_NAME ("apei")
54
55 static bool apei_erst_instvalid(ACPI_WHEA_HEADER *, uint32_t, uint32_t);
56 static void apei_erst_instfunc(ACPI_WHEA_HEADER *, void *, uint32_t *,
57 uint32_t);
58 static uint64_t apei_erst_act(struct apei_softc *, enum AcpiErstActions,
59 uint64_t);
60
61 /*
62 * apei_erst_action
63 *
64 * Symbolic names of the APEI ERST (Error Record Serialization
65 * Table) logical actions are taken (and downcased) from:
66 *
67 * https://uefi.org/specs/ACPI/6.5/18_Platform_Error_Interfaces.html#error-record-serialization-actions-table
68 */
69 static const char *const apei_erst_action[] = {
70 [ACPI_ERST_BEGIN_WRITE] = "begin_write_operation",
71 [ACPI_ERST_BEGIN_READ] = "begin_read_operation",
72 [ACPI_ERST_BEGIN_CLEAR] = "begin_clear_operation",
73 [ACPI_ERST_END] = "end_operation",
74 [ACPI_ERST_SET_RECORD_OFFSET] = "set_record_offset",
75 [ACPI_ERST_EXECUTE_OPERATION] = "execute_operation",
76 [ACPI_ERST_CHECK_BUSY_STATUS] = "check_busy_status",
77 [ACPI_ERST_GET_COMMAND_STATUS] = "get_command_status",
78 [ACPI_ERST_GET_RECORD_ID] = "get_record_identifier",
79 [ACPI_ERST_SET_RECORD_ID] = "set_record_identifier",
80 [ACPI_ERST_GET_RECORD_COUNT] = "get_record_count",
81 [ACPI_ERST_BEGIN_DUMMY_WRIITE] = "begin_dummy_write_operation",
82 [ACPI_ERST_NOT_USED] = "reserved",
83 [ACPI_ERST_GET_ERROR_RANGE] = "get_error_log_address_range",
84 [ACPI_ERST_GET_ERROR_LENGTH] = "get_error_log_address_range_length",
85 [ACPI_ERST_GET_ERROR_ATTRIBUTES] =
86 "get_error_log_address_range_attributes",
87 [ACPI_ERST_EXECUTE_TIMINGS] = "get_execute_operations_timings",
88 };
89
90 /*
91 * apei_erst_instruction
92 *
93 * Symbolic names of the APEI ERST (Error Record Serialization
94 * Table) instructions to implement logical actions are taken (and
95 * downcased) from:
96 *
97 * https://uefi.org/specs/ACPI/6.5/18_Platform_Error_Interfaces.html#serialization-instructions
98 */
99 static const char *apei_erst_instruction[] = {
100 [ACPI_ERST_READ_REGISTER] = "read_register",
101 [ACPI_ERST_READ_REGISTER_VALUE] = "read_register_value",
102 [ACPI_ERST_WRITE_REGISTER] = "write_register",
103 [ACPI_ERST_WRITE_REGISTER_VALUE] = "write_register_value",
104 [ACPI_ERST_NOOP] = "noop",
105 [ACPI_ERST_LOAD_VAR1] = "load_var1",
106 [ACPI_ERST_LOAD_VAR2] = "load_var2",
107 [ACPI_ERST_STORE_VAR1] = "store_var1",
108 [ACPI_ERST_ADD] = "add",
109 [ACPI_ERST_SUBTRACT] = "subtract",
110 [ACPI_ERST_ADD_VALUE] = "add_value",
111 [ACPI_ERST_SUBTRACT_VALUE] = "subtract_value",
112 [ACPI_ERST_STALL] = "stall",
113 [ACPI_ERST_STALL_WHILE_TRUE] = "stall_while_true",
114 [ACPI_ERST_SKIP_NEXT_IF_TRUE] = "skip_next_instruction_if_true",
115 [ACPI_ERST_GOTO] = "goto",
116 [ACPI_ERST_SET_SRC_ADDRESS_BASE] = "set_src_address_base",
117 [ACPI_ERST_SET_DST_ADDRESS_BASE] = "set_dst_address_base",
118 [ACPI_ERST_MOVE_DATA] = "move_data",
119 };
120
121 /*
122 * XXX dtrace and kernhist
123 */
124 static void
125 apei_pmemmove(uint64_t pdst, uint64_t psrc, uint64_t nbytes)
126 {
127 char *vdst, *vsrc;
128
129 aprint_debug("ERST: move"
130 " %"PRIu64" bytes from 0x%"PRIx64" to 0x%"PRIx64"\n",
131 nbytes, psrc, pdst);
132
133 /*
134 * Carefully check for overlap.
135 */
136 if (pdst == psrc) {
137 /*
138 * Technically this could happen, I guess!
139 */
140 return;
141 } else if (pdst < psrc && psrc < pdst + nbytes) {
142 /*
143 * psrc ______ psrc + nbytes
144 * / \
145 * <--------------------->
146 * \______/
147 * pdst pdst + nbytes
148 */
149 vdst = AcpiOsMapMemory(pdst, nbytes + (psrc - pdst));
150 vsrc = vdst + (psrc - pdst);
151 memmove(vdst, vsrc, nbytes);
152 AcpiOsUnmapMemory(vdst, nbytes + (psrc - pdst));
153 } else if (psrc < pdst && pdst < psrc + nbytes) {
154 /*
155 * psrc ______ psrc + nbytes
156 * / \
157 * <--------------------->
158 * \______/
159 * pdst pdst + nbytes
160 */
161 vsrc = AcpiOsMapMemory(psrc, nbytes + (pdst - psrc));
162 vdst = vsrc + (pdst - psrc);
163 memmove(vdst, vsrc, nbytes);
164 AcpiOsUnmapMemory(vsrc, nbytes + (pdst - psrc));
165 } else {
166 /*
167 * No overlap.
168 */
169 vdst = AcpiOsMapMemory(pdst, nbytes);
170 vsrc = AcpiOsMapMemory(psrc, nbytes);
171 memcpy(vdst, vsrc, nbytes);
172 AcpiOsUnmapMemory(vsrc, nbytes);
173 AcpiOsUnmapMemory(vdst, nbytes);
174 }
175 }
176
177 /*
178 * apei_erst_attach(sc)
179 *
180 * Scan the Error Record Serialization Table to collate the
181 * instructions for each ERST action.
182 */
183 void
184 apei_erst_attach(struct apei_softc *sc)
185 {
186 ACPI_TABLE_ERST *erst = sc->sc_tab.erst;
187 struct apei_erst_softc *ssc = &sc->sc_erst;
188 ACPI_ERST_ENTRY *entry;
189 uint32_t i, nentries, maxnentries;
190
191 /*
192 * Verify the table length, table header length, and
193 * instruction entry count are all sensible. If the header is
194 * truncated, stop here; if the entries are truncated, stop at
195 * the largest integral number of full entries that fits.
196 */
197 if (erst->Header.Length < sizeof(*erst)) {
198 aprint_error_dev(sc->sc_dev, "ERST: truncated table:"
199 " %"PRIu32" < %zu minimum bytes\n",
200 erst->Header.Length, sizeof(*erst));
201 return;
202 }
203 if (erst->HeaderLength <
204 sizeof(*erst) - offsetof(ACPI_TABLE_ERST, HeaderLength)) {
205 aprint_error_dev(sc->sc_dev, "ERST: truncated header:"
206 " %"PRIu32" < %zu bytes\n",
207 erst->HeaderLength,
208 sizeof(*erst) - offsetof(ACPI_TABLE_ERST, HeaderLength));
209 return;
210 }
211 nentries = erst->Entries;
212 maxnentries = (erst->Header.Length - sizeof(*erst))/sizeof(*entry);
213 if (nentries > maxnentries) {
214 aprint_error_dev(sc->sc_dev, "ERST: excessive entries:"
215 " %"PRIu32", truncating to %"PRIu32"\n",
216 nentries, maxnentries);
217 nentries = maxnentries;
218 }
219 if (nentries*sizeof(*entry) < erst->Header.Length - sizeof(*erst)) {
220 aprint_error_dev(sc->sc_dev, "ERST:"
221 " %zu bytes of trailing garbage after last entry\n",
222 erst->Header.Length - nentries*sizeof(*entry));
223 }
224
225 /*
226 * Create an interpreter for ERST actions.
227 */
228 ssc->ssc_interp = apei_interp_create("ERST",
229 apei_erst_action, __arraycount(apei_erst_action),
230 apei_erst_instruction, __arraycount(apei_erst_instruction),
231 apei_erst_instvalid, apei_erst_instfunc);
232
233 /*
234 * Compile the interpreter from the ERST action instruction
235 * table.
236 */
237 entry = (ACPI_ERST_ENTRY *)(erst + 1);
238 for (i = 0; i < nentries; i++, entry++)
239 apei_interp_pass1_load(ssc->ssc_interp, i, &entry->WheaHeader);
240 entry = (ACPI_ERST_ENTRY *)(erst + 1);
241 for (i = 0; i < nentries; i++, entry++) {
242 apei_interp_pass2_verify(ssc->ssc_interp, i,
243 &entry->WheaHeader);
244 }
245 apei_interp_pass3_alloc(ssc->ssc_interp);
246 entry = (ACPI_ERST_ENTRY *)(erst + 1);
247 for (i = 0; i < nentries; i++, entry++) {
248 apei_interp_pass4_assemble(ssc->ssc_interp, i,
249 &entry->WheaHeader);
250 }
251 apei_interp_pass5_verify(ssc->ssc_interp);
252
253 /*
254 * Print some basic information about the stored records.
255 */
256 uint64_t logaddr = apei_erst_act(sc, ACPI_ERST_GET_ERROR_RANGE, 0);
257 uint64_t logbytes = apei_erst_act(sc, ACPI_ERST_GET_ERROR_LENGTH, 0);
258 uint64_t attr = apei_erst_act(sc, ACPI_ERST_GET_ERROR_ATTRIBUTES, 0);
259 uint64_t nrecords = apei_erst_act(sc, ACPI_ERST_GET_RECORD_COUNT, 0);
260 char attrbuf[128];
261
262 /* XXX define this format somewhere */
263 snprintb(attrbuf, sizeof(attrbuf), "\177\020"
264 "\001" "NVRAM\0"
265 "\002" "SLOW\0"
266 "\0", attr);
267
268 aprint_normal_dev(sc->sc_dev, "ERST: %"PRIu64" records in error log"
269 " %"PRIu64" bytes @ 0x%"PRIx64" attr=%s\n",
270 nrecords, logbytes, logaddr, attrbuf);
271
272 /*
273 * XXX wire up to sysctl or a file system or something, and/or
274 * dmesg or crash dumps
275 */
276 }
277
278 /*
279 * apei_erst_detach(sc)
280 *
281 * Free software resource allocated for ERST handling.
282 */
283 void
284 apei_erst_detach(struct apei_softc *sc)
285 {
286 struct apei_erst_softc *ssc = &sc->sc_erst;
287
288 if (ssc->ssc_interp) {
289 apei_interp_destroy(ssc->ssc_interp);
290 ssc->ssc_interp = NULL;
291 }
292 }
293
294 /*
295 * apei_erst_instvalid(header, ninst, i)
296 *
297 * Routine to validate the ith entry, for an action with ninst
298 * instructions.
299 */
300 static bool
301 apei_erst_instvalid(ACPI_WHEA_HEADER *header, uint32_t ninst, uint32_t i)
302 {
303
304 switch (header->Instruction) {
305 case ACPI_ERST_GOTO:
306 if (header->Value > ninst) {
307 aprint_error("ERST[%"PRIu32"]:"
308 " GOTO(%"PRIu64") out of bounds,"
309 " disabling action %"PRIu32" (%s)\n", i,
310 header->Value,
311 header->Action,
312 apei_erst_action[header->Action]);
313 return false;
314 }
315 }
316 return true;
317 }
318
319 /*
320 * struct apei_erst_machine
321 *
322 * Machine state for executing ERST instructions.
323 */
324 struct apei_erst_machine {
325 struct apei_softc *sc;
326 uint64_t x; /* in */
327 uint64_t y; /* out */
328 uint64_t var1;
329 uint64_t var2;
330 uint64_t src_base;
331 uint64_t dst_base;
332 };
333
334 /*
335 * apei_erst_instfunc(header, cookie, &ip, maxip)
336 *
337 * Run a single instruction in the service of performing an ERST
338 * action. Updates the ERST machine at cookie, and the ip if
339 * necessary, in place.
340 *
341 * On entry, ip points to the next instruction after this one
342 * sequentially; on exit, ip points to the next instruction to
343 * execute.
344 */
345 static void
346 apei_erst_instfunc(ACPI_WHEA_HEADER *header, void *cookie, uint32_t *ipp,
347 uint32_t maxip)
348 {
349 struct apei_erst_machine *const M = cookie;
350 ACPI_STATUS rv = AE_OK;
351
352 /*
353 * Abbreviate some of the intermediate quantities to make the
354 * instruction logic conciser and more legible.
355 */
356 const uint8_t BitOffset = header->RegisterRegion.BitOffset;
357 const uint64_t Mask = header->Mask;
358 const uint64_t Value = header->Value;
359 ACPI_GENERIC_ADDRESS *const reg = &header->RegisterRegion;
360 const bool preserve_register = header->Flags & ACPI_ERST_PRESERVE;
361
362 aprint_debug_dev(M->sc->sc_dev, "%s: instr=0x%02"PRIx8
363 " (%s)"
364 " Address=0x%"PRIx64
365 " BitOffset=%"PRIu8" Mask=0x%"PRIx64" Value=0x%"PRIx64
366 " Flags=0x%"PRIx8"\n",
367 __func__, header->Instruction,
368 (header->Instruction < __arraycount(apei_erst_instruction)
369 ? apei_erst_instruction[header->Instruction]
370 : "unknown"),
371 reg->Address,
372 BitOffset, Mask, Value,
373 header->Flags);
374
375 /*
376 * Zero-initialize the output by default.
377 */
378 M->y = 0;
379
380 /*
381 * Dispatch the instruction.
382 */
383 switch (header->Instruction) {
384 case ACPI_ERST_READ_REGISTER:
385 rv = apei_read_register(reg, Mask, &M->y);
386 break;
387 case ACPI_ERST_READ_REGISTER_VALUE: {
388 uint64_t v;
389
390 rv = apei_read_register(reg, Mask, &v);
391 if (ACPI_FAILURE(rv))
392 break;
393 M->y = (v == Value ? 1 : 0);
394 break;
395 }
396 case ACPI_ERST_WRITE_REGISTER:
397 rv = apei_write_register(reg, Mask, preserve_register, M->x);
398 break;
399 case ACPI_ERST_WRITE_REGISTER_VALUE:
400 rv = apei_write_register(reg, Mask, preserve_register, Value);
401 break;
402 case ACPI_ERST_NOOP:
403 break;
404 case ACPI_ERST_LOAD_VAR1:
405 rv = apei_read_register(reg, Mask, &M->var1);
406 break;
407 case ACPI_ERST_LOAD_VAR2:
408 rv = apei_read_register(reg, Mask, &M->var2);
409 break;
410 case ACPI_ERST_STORE_VAR1:
411 rv = apei_write_register(reg, Mask, preserve_register,
412 M->var1);
413 break;
414 case ACPI_ERST_ADD:
415 M->var1 += M->var2;
416 break;
417 case ACPI_ERST_SUBTRACT:
418 /*
419 * The specification at
420 * https://uefi.org/specs/ACPI/6.5/18_Platform_Error_Interfaces.html#serialization-instructions
421 * says:
422 *
423 * 0x09 SUBTRACT Subtracts VAR1 from VAR2
424 * and stores the result in
425 * VAR1.
426 *
427 * So, according to the spec, this is _not_ simply
428 *
429 * M->var1 -= M->var2;
430 */
431 M->var1 = M->var2 - M->var1;
432 break;
433 case ACPI_ERST_ADD_VALUE: {
434 uint64_t v;
435
436 rv = apei_read_register(reg, Mask, &v);
437 if (ACPI_FAILURE(rv))
438 break;
439
440 v += Value;
441
442 rv = apei_write_register(reg, Mask, preserve_register, v);
443 break;
444 }
445 case ACPI_ERST_SUBTRACT_VALUE: {
446 uint64_t v;
447
448 rv = apei_read_register(reg, Mask, &v);
449 if (ACPI_FAILURE(rv))
450 break;
451
452 v -= Value;
453
454 rv = apei_write_register(reg, Mask, preserve_register, v);
455 break;
456 }
457 case ACPI_ERST_STALL:
458 DELAY(Value); /* XXX avoid excessive delays */
459 break;
460 case ACPI_ERST_STALL_WHILE_TRUE:
461 for (;;) {
462 uint64_t v;
463
464 rv = apei_read_register(reg, Mask, &v);
465 if (ACPI_FAILURE(rv))
466 break;
467 if (v != Value)
468 break;
469 DELAY(M->var1);
470 }
471 break;
472 case ACPI_ERST_SKIP_NEXT_IF_TRUE: {
473 uint64_t v;
474
475 rv = apei_read_register(reg, Mask, &v);
476 if (ACPI_FAILURE(rv))
477 break;
478
479 /*
480 * If reading the register yields Value, skip the next
481 * instruction -- unless that would run past the end of
482 * the instruction buffer.
483 */
484 if (v == Value) {
485 if (*ipp < maxip)
486 (*ipp)++;
487 }
488 break;
489 }
490 case ACPI_ERST_GOTO:
491 if (Value >= maxip) /* paranoia */
492 *ipp = maxip;
493 else
494 *ipp = Value;
495 break;
496 case ACPI_ERST_SET_SRC_ADDRESS_BASE: {
497 uint64_t v;
498
499 rv = apei_read_register(reg, Mask, &v);
500 if (ACPI_FAILURE(rv))
501 break;
502 M->src_base = v;
503 break;
504 }
505 case ACPI_ERST_SET_DST_ADDRESS_BASE: {
506 uint64_t v;
507
508 rv = apei_read_register(reg, Mask, &v);
509 if (ACPI_FAILURE(rv))
510 break;
511 M->src_base = v;
512 break;
513 }
514 case ACPI_ERST_MOVE_DATA: {
515 uint64_t v;
516
517 rv = apei_read_register(reg, Mask, &v);
518 if (ACPI_FAILURE(rv))
519 break;
520 apei_pmemmove(M->dst_base + v, M->src_base + v, M->var2);
521 break;
522 }
523 default:
524 break;
525 }
526
527 /*
528 * If any register I/O failed, print the failure message. This
529 * could be more specific about exactly what failed, but that
530 * takes a little more effort to write.
531 */
532 if (ACPI_FAILURE(rv)) {
533 aprint_debug_dev(M->sc->sc_dev, "%s: failed: %s\n", __func__,
534 AcpiFormatException(rv));
535 }
536 }
537
538 /*
539 * apei_erst_act(sc, action, x)
540 *
541 * Perform the named ERST action with input x, by stepping through
542 * all the instructions defined for the action by the ERST, and
543 * return the output.
544 */
545 static uint64_t
546 apei_erst_act(struct apei_softc *sc, enum AcpiErstActions action, uint64_t x)
547 {
548 struct apei_erst_softc *const ssc = &sc->sc_erst;
549 struct apei_erst_machine erst_machine, *const M = &erst_machine;
550
551 aprint_debug_dev(sc->sc_dev, "%s: action=%d (%s) input=0x%"PRIx64"\n",
552 __func__,
553 action,
554 (action < __arraycount(apei_erst_action)
555 ? apei_erst_action[action]
556 : "unknown"),
557 x);
558
559 /*
560 * Initialize the machine to execute the action's instructions.
561 */
562 memset(M, 0, sizeof(*M));
563 M->sc = sc;
564 M->x = x; /* input */
565 M->y = 0; /* output */
566 M->var1 = 0;
567 M->var2 = 0;
568 M->src_base = 0;
569 M->dst_base = 0;
570
571 /*
572 * Run the interpreter.
573 */
574 apei_interpret(ssc->ssc_interp, action, M);
575
576 /*
577 * Return the result.
578 */
579 aprint_debug_dev(sc->sc_dev, "%s: output=0x%"PRIx64"\n", __func__,
580 M->y);
581 return M->y;
582 }
583