xref: /netbsd-src/sys/dev/acpi/apei_bert.c (revision 4b159fe528de68c639f4f9cd4d8b3ef59df8a58d)
1 /*	$NetBSD: apei_bert.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 BERT -- Boot Error Record Table
31  *
32  * https://uefi.org/specs/ACPI/6.5/18_Platform_Error_Interfaces.html#boot-error-source
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: apei_bert.c,v 1.1 2024/03/20 17:11:43 riastradh Exp $");
37 
38 #include <sys/types.h>
39 
40 #include <sys/systm.h>
41 
42 #include <dev/acpi/acpivar.h>
43 #include <dev/acpi/apeivar.h>
44 #include <dev/acpi/apei_bertvar.h>
45 
46 #define	_COMPONENT	ACPI_RESOURCE_COMPONENT
47 ACPI_MODULE_NAME	("apei")
48 
49 /*
50  * apei_bert_attach(sc)
51  *
52  *	Scan the Boot Error Record Table for hardware errors that
53  *	happened early at boot or on the previous boot.
54  */
55 void
apei_bert_attach(struct apei_softc * sc)56 apei_bert_attach(struct apei_softc *sc)
57 {
58 	const ACPI_TABLE_BERT *bert = sc->sc_tab.bert;
59 	struct apei_bert_softc *bsc = &sc->sc_bert;
60 	bool fatal = false;
61 
62 	/*
63 	 * Verify the table is large enough.
64 	 */
65 	if (bert->Header.Length < sizeof(*bert)) {
66 		aprint_error_dev(sc->sc_dev, "BERT: truncated table:"
67 		    " %"PRIu32" < %zu bytes\n",
68 		    bert->Header.Length, sizeof(*bert));
69 		return;
70 	}
71 
72 	/*
73 	 * In verbose boots, print the BERT physical address and
74 	 * length.  The operator might find this handy for dd'ing it
75 	 * from /dev/mem, if allowed.
76 	 */
77 	aprint_verbose_dev(sc->sc_dev, "BERT: 0x%x bytes at 0x%"PRIx64"\n",
78 	    bert->RegionLength, bert->Address);
79 
80 	/*
81 	 * Verify the length is enough for a Generic Error Status Block
82 	 * header, at least.
83 	 */
84 	if (bert->RegionLength < sizeof(*bsc->bsc_gesb)) {
85 		aprint_error_dev(sc->sc_dev,
86 		    "BERT: truncated boot error region, %"PRIu32" < %zu bytes",
87 		    bert->RegionLength, sizeof(*bsc->bsc_gesb));
88 		return;
89 	}
90 
91 	/*
92 	 * Map the GESB and process it, but don't acknowledge it --
93 	 * this is a one-time polled source; it won't (or at least,
94 	 * shouldn't) change after boot.
95 	 */
96 	bsc->bsc_gesb = AcpiOsMapMemory(bert->Address, bert->RegionLength);
97 	const uint32_t status = apei_gesb_report(sc, bsc->bsc_gesb,
98 	    bert->RegionLength, "boot error record", &fatal);
99 	if (status == 0) {
100 		/*
101 		 * If there were no boot errors, leave a note in dmesg
102 		 * to this effect without cluttering up the console
103 		 * unless you asked for it by `boot -v'.
104 		 */
105 		aprint_verbose_dev(sc->sc_dev,
106 		    "BERT: no boot errors recorded\n");
107 	}
108 
109 	/*
110 	 * If the error was fatal, print a warning to the console.
111 	 * Probably not actually fatal now since it is usually related
112 	 * to early or previous boot.
113 	 */
114 	if (fatal) {
115 		aprint_error_dev(sc->sc_dev, "BERT:"
116 		    " fatal pre-boot error recorded\n");
117 	}
118 
119 	/* XXX expose content via sysctl? */
120 }
121 
122 /*
123  * apei_bert_detach(sc)
124  *
125  *	Free any software resources associated with the Boot Error
126  *	Record Table.
127  */
128 void
apei_bert_detach(struct apei_softc * sc)129 apei_bert_detach(struct apei_softc *sc)
130 {
131 	const ACPI_TABLE_BERT *bert = sc->sc_tab.bert;
132 	struct apei_bert_softc *bsc = &sc->sc_bert;
133 
134 	if (bsc->bsc_gesb) {
135 		AcpiOsUnmapMemory(bsc->bsc_gesb, bert->RegionLength);
136 		bsc->bsc_gesb = NULL;
137 	}
138 }
139