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