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