apei_bert.c revision 1.1.4.2 1 1.1.4.2 martin /* $NetBSD: apei_bert.c,v 1.1.4.2 2024/10/09 13:00:11 martin Exp $ */
2 1.1.4.2 martin
3 1.1.4.2 martin /*-
4 1.1.4.2 martin * Copyright (c) 2024 The NetBSD Foundation, Inc.
5 1.1.4.2 martin * All rights reserved.
6 1.1.4.2 martin *
7 1.1.4.2 martin * Redistribution and use in source and binary forms, with or without
8 1.1.4.2 martin * modification, are permitted provided that the following conditions
9 1.1.4.2 martin * are met:
10 1.1.4.2 martin * 1. Redistributions of source code must retain the above copyright
11 1.1.4.2 martin * notice, this list of conditions and the following disclaimer.
12 1.1.4.2 martin * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.4.2 martin * notice, this list of conditions and the following disclaimer in the
14 1.1.4.2 martin * documentation and/or other materials provided with the distribution.
15 1.1.4.2 martin *
16 1.1.4.2 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.4.2 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.4.2 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.4.2 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.4.2 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.4.2 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.4.2 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.4.2 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.4.2 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.4.2 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.4.2 martin * POSSIBILITY OF SUCH DAMAGE.
27 1.1.4.2 martin */
28 1.1.4.2 martin
29 1.1.4.2 martin /*
30 1.1.4.2 martin * APEI BERT -- Boot Error Record Table
31 1.1.4.2 martin *
32 1.1.4.2 martin * https://uefi.org/specs/ACPI/6.5/18_Platform_Error_Interfaces.html#boot-error-source
33 1.1.4.2 martin */
34 1.1.4.2 martin
35 1.1.4.2 martin #include <sys/cdefs.h>
36 1.1.4.2 martin __KERNEL_RCSID(0, "$NetBSD: apei_bert.c,v 1.1.4.2 2024/10/09 13:00:11 martin Exp $");
37 1.1.4.2 martin
38 1.1.4.2 martin #include <sys/types.h>
39 1.1.4.2 martin
40 1.1.4.2 martin #include <sys/systm.h>
41 1.1.4.2 martin
42 1.1.4.2 martin #include <dev/acpi/acpivar.h>
43 1.1.4.2 martin #include <dev/acpi/apeivar.h>
44 1.1.4.2 martin #include <dev/acpi/apei_bertvar.h>
45 1.1.4.2 martin
46 1.1.4.2 martin #define _COMPONENT ACPI_RESOURCE_COMPONENT
47 1.1.4.2 martin ACPI_MODULE_NAME ("apei")
48 1.1.4.2 martin
49 1.1.4.2 martin /*
50 1.1.4.2 martin * apei_bert_attach(sc)
51 1.1.4.2 martin *
52 1.1.4.2 martin * Scan the Boot Error Record Table for hardware errors that
53 1.1.4.2 martin * happened early at boot or on the previous boot.
54 1.1.4.2 martin */
55 1.1.4.2 martin void
56 1.1.4.2 martin apei_bert_attach(struct apei_softc *sc)
57 1.1.4.2 martin {
58 1.1.4.2 martin const ACPI_TABLE_BERT *bert = sc->sc_tab.bert;
59 1.1.4.2 martin struct apei_bert_softc *bsc = &sc->sc_bert;
60 1.1.4.2 martin bool fatal = false;
61 1.1.4.2 martin
62 1.1.4.2 martin /*
63 1.1.4.2 martin * Verify the table is large enough.
64 1.1.4.2 martin */
65 1.1.4.2 martin if (bert->Header.Length < sizeof(*bert)) {
66 1.1.4.2 martin aprint_error_dev(sc->sc_dev, "BERT: truncated table:"
67 1.1.4.2 martin " %"PRIu32" < %zu bytes\n",
68 1.1.4.2 martin bert->Header.Length, sizeof(*bert));
69 1.1.4.2 martin return;
70 1.1.4.2 martin }
71 1.1.4.2 martin
72 1.1.4.2 martin /*
73 1.1.4.2 martin * In verbose boots, print the BERT physical address and
74 1.1.4.2 martin * length. The operator might find this handy for dd'ing it
75 1.1.4.2 martin * from /dev/mem, if allowed.
76 1.1.4.2 martin */
77 1.1.4.2 martin aprint_verbose_dev(sc->sc_dev, "BERT: 0x%x bytes at 0x%"PRIx64"\n",
78 1.1.4.2 martin bert->RegionLength, bert->Address);
79 1.1.4.2 martin
80 1.1.4.2 martin /*
81 1.1.4.2 martin * Verify the length is enough for a Generic Error Status Block
82 1.1.4.2 martin * header, at least.
83 1.1.4.2 martin */
84 1.1.4.2 martin if (bert->RegionLength < sizeof(*bsc->bsc_gesb)) {
85 1.1.4.2 martin aprint_error_dev(sc->sc_dev,
86 1.1.4.2 martin "BERT: truncated boot error region, %"PRIu32" < %zu bytes",
87 1.1.4.2 martin bert->RegionLength, sizeof(*bsc->bsc_gesb));
88 1.1.4.2 martin return;
89 1.1.4.2 martin }
90 1.1.4.2 martin
91 1.1.4.2 martin /*
92 1.1.4.2 martin * Map the GESB and process it, but don't acknowledge it --
93 1.1.4.2 martin * this is a one-time polled source; it won't (or at least,
94 1.1.4.2 martin * shouldn't) change after boot.
95 1.1.4.2 martin */
96 1.1.4.2 martin bsc->bsc_gesb = AcpiOsMapMemory(bert->Address, bert->RegionLength);
97 1.1.4.2 martin const uint32_t status = apei_gesb_report(sc, bsc->bsc_gesb,
98 1.1.4.2 martin bert->RegionLength, "boot error record", &fatal);
99 1.1.4.2 martin if (status == 0) {
100 1.1.4.2 martin /*
101 1.1.4.2 martin * If there were no boot errors, leave a note in dmesg
102 1.1.4.2 martin * to this effect without cluttering up the console
103 1.1.4.2 martin * unless you asked for it by `boot -v'.
104 1.1.4.2 martin */
105 1.1.4.2 martin aprint_verbose_dev(sc->sc_dev,
106 1.1.4.2 martin "BERT: no boot errors recorded\n");
107 1.1.4.2 martin }
108 1.1.4.2 martin
109 1.1.4.2 martin /*
110 1.1.4.2 martin * If the error was fatal, print a warning to the console.
111 1.1.4.2 martin * Probably not actually fatal now since it is usually related
112 1.1.4.2 martin * to early or previous boot.
113 1.1.4.2 martin */
114 1.1.4.2 martin if (fatal) {
115 1.1.4.2 martin aprint_error_dev(sc->sc_dev, "BERT:"
116 1.1.4.2 martin " fatal pre-boot error recorded\n");
117 1.1.4.2 martin }
118 1.1.4.2 martin
119 1.1.4.2 martin /* XXX expose content via sysctl? */
120 1.1.4.2 martin }
121 1.1.4.2 martin
122 1.1.4.2 martin /*
123 1.1.4.2 martin * apei_bert_detach(sc)
124 1.1.4.2 martin *
125 1.1.4.2 martin * Free any software resources associated with the Boot Error
126 1.1.4.2 martin * Record Table.
127 1.1.4.2 martin */
128 1.1.4.2 martin void
129 1.1.4.2 martin apei_bert_detach(struct apei_softc *sc)
130 1.1.4.2 martin {
131 1.1.4.2 martin const ACPI_TABLE_BERT *bert = sc->sc_tab.bert;
132 1.1.4.2 martin struct apei_bert_softc *bsc = &sc->sc_bert;
133 1.1.4.2 martin
134 1.1.4.2 martin if (bsc->bsc_gesb) {
135 1.1.4.2 martin AcpiOsUnmapMemory(bsc->bsc_gesb, bert->RegionLength);
136 1.1.4.2 martin bsc->bsc_gesb = NULL;
137 1.1.4.2 martin }
138 1.1.4.2 martin }
139