1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * APEI Boot Error Record Table (BERT) support
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright 2011 Intel Corp.
6*4882a593Smuzhiyun * Author: Huang Ying <ying.huang@intel.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Under normal circumstances, when a hardware error occurs, the error
9*4882a593Smuzhiyun * handler receives control and processes the error. This gives OSPM a
10*4882a593Smuzhiyun * chance to process the error condition, report it, and optionally attempt
11*4882a593Smuzhiyun * recovery. In some cases, the system is unable to process an error.
12*4882a593Smuzhiyun * For example, system firmware or a management controller may choose to
13*4882a593Smuzhiyun * reset the system or the system might experience an uncontrolled crash
14*4882a593Smuzhiyun * or reset.The boot error source is used to report unhandled errors that
15*4882a593Smuzhiyun * occurred in a previous boot. This mechanism is described in the BERT
16*4882a593Smuzhiyun * table.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * For more information about BERT, please refer to ACPI Specification
19*4882a593Smuzhiyun * version 4.0, section 17.3.1
20*4882a593Smuzhiyun */
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include <linux/kernel.h>
23*4882a593Smuzhiyun #include <linux/module.h>
24*4882a593Smuzhiyun #include <linux/init.h>
25*4882a593Smuzhiyun #include <linux/acpi.h>
26*4882a593Smuzhiyun #include <linux/io.h>
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #include "apei-internal.h"
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #undef pr_fmt
31*4882a593Smuzhiyun #define pr_fmt(fmt) "BERT: " fmt
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #define ACPI_BERT_PRINT_MAX_RECORDS 5
34*4882a593Smuzhiyun #define ACPI_BERT_PRINT_MAX_LEN 1024
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun static int bert_disable;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun * Print "all" the error records in the BERT table, but avoid huge spam to
40*4882a593Smuzhiyun * the console if the BIOS included oversize records, or too many records.
41*4882a593Smuzhiyun * Skipping some records here does not lose anything because the full
42*4882a593Smuzhiyun * data is available to user tools in:
43*4882a593Smuzhiyun * /sys/firmware/acpi/tables/data/BERT
44*4882a593Smuzhiyun */
bert_print_all(struct acpi_bert_region * region,unsigned int region_len)45*4882a593Smuzhiyun static void __init bert_print_all(struct acpi_bert_region *region,
46*4882a593Smuzhiyun unsigned int region_len)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun struct acpi_hest_generic_status *estatus =
49*4882a593Smuzhiyun (struct acpi_hest_generic_status *)region;
50*4882a593Smuzhiyun int remain = region_len;
51*4882a593Smuzhiyun int printed = 0, skipped = 0;
52*4882a593Smuzhiyun u32 estatus_len;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun while (remain >= sizeof(struct acpi_bert_region)) {
55*4882a593Smuzhiyun estatus_len = cper_estatus_len(estatus);
56*4882a593Smuzhiyun if (remain < estatus_len) {
57*4882a593Smuzhiyun pr_err(FW_BUG "Truncated status block (length: %u).\n",
58*4882a593Smuzhiyun estatus_len);
59*4882a593Smuzhiyun break;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /* No more error records. */
63*4882a593Smuzhiyun if (!estatus->block_status)
64*4882a593Smuzhiyun break;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun if (cper_estatus_check(estatus)) {
67*4882a593Smuzhiyun pr_err(FW_BUG "Invalid error record.\n");
68*4882a593Smuzhiyun break;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun if (estatus_len < ACPI_BERT_PRINT_MAX_LEN &&
72*4882a593Smuzhiyun printed < ACPI_BERT_PRINT_MAX_RECORDS) {
73*4882a593Smuzhiyun pr_info_once("Error records from previous boot:\n");
74*4882a593Smuzhiyun cper_estatus_print(KERN_INFO HW_ERR, estatus);
75*4882a593Smuzhiyun printed++;
76*4882a593Smuzhiyun } else {
77*4882a593Smuzhiyun skipped++;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /*
81*4882a593Smuzhiyun * Because the boot error source is "one-time polled" type,
82*4882a593Smuzhiyun * clear Block Status of current Generic Error Status Block,
83*4882a593Smuzhiyun * once it's printed.
84*4882a593Smuzhiyun */
85*4882a593Smuzhiyun estatus->block_status = 0;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun estatus = (void *)estatus + estatus_len;
88*4882a593Smuzhiyun remain -= estatus_len;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun if (skipped)
92*4882a593Smuzhiyun pr_info(HW_ERR "Skipped %d error records\n", skipped);
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
setup_bert_disable(char * str)95*4882a593Smuzhiyun static int __init setup_bert_disable(char *str)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun bert_disable = 1;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun return 1;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun __setup("bert_disable", setup_bert_disable);
102*4882a593Smuzhiyun
bert_check_table(struct acpi_table_bert * bert_tab)103*4882a593Smuzhiyun static int __init bert_check_table(struct acpi_table_bert *bert_tab)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun if (bert_tab->header.length < sizeof(struct acpi_table_bert) ||
106*4882a593Smuzhiyun bert_tab->region_length < sizeof(struct acpi_bert_region))
107*4882a593Smuzhiyun return -EINVAL;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun return 0;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
bert_init(void)112*4882a593Smuzhiyun static int __init bert_init(void)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun struct apei_resources bert_resources;
115*4882a593Smuzhiyun struct acpi_bert_region *boot_error_region;
116*4882a593Smuzhiyun struct acpi_table_bert *bert_tab;
117*4882a593Smuzhiyun unsigned int region_len;
118*4882a593Smuzhiyun acpi_status status;
119*4882a593Smuzhiyun int rc = 0;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun if (acpi_disabled)
122*4882a593Smuzhiyun return 0;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun if (bert_disable) {
125*4882a593Smuzhiyun pr_info("Boot Error Record Table support is disabled.\n");
126*4882a593Smuzhiyun return 0;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun status = acpi_get_table(ACPI_SIG_BERT, 0, (struct acpi_table_header **)&bert_tab);
130*4882a593Smuzhiyun if (status == AE_NOT_FOUND)
131*4882a593Smuzhiyun return 0;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if (ACPI_FAILURE(status)) {
134*4882a593Smuzhiyun pr_err("get table failed, %s.\n", acpi_format_exception(status));
135*4882a593Smuzhiyun return -EINVAL;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun rc = bert_check_table(bert_tab);
139*4882a593Smuzhiyun if (rc) {
140*4882a593Smuzhiyun pr_err(FW_BUG "table invalid.\n");
141*4882a593Smuzhiyun goto out_put_bert_tab;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun region_len = bert_tab->region_length;
145*4882a593Smuzhiyun apei_resources_init(&bert_resources);
146*4882a593Smuzhiyun rc = apei_resources_add(&bert_resources, bert_tab->address,
147*4882a593Smuzhiyun region_len, true);
148*4882a593Smuzhiyun if (rc)
149*4882a593Smuzhiyun goto out_put_bert_tab;
150*4882a593Smuzhiyun rc = apei_resources_request(&bert_resources, "APEI BERT");
151*4882a593Smuzhiyun if (rc)
152*4882a593Smuzhiyun goto out_fini;
153*4882a593Smuzhiyun boot_error_region = ioremap_cache(bert_tab->address, region_len);
154*4882a593Smuzhiyun if (boot_error_region) {
155*4882a593Smuzhiyun bert_print_all(boot_error_region, region_len);
156*4882a593Smuzhiyun iounmap(boot_error_region);
157*4882a593Smuzhiyun } else {
158*4882a593Smuzhiyun rc = -ENOMEM;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun apei_resources_release(&bert_resources);
162*4882a593Smuzhiyun out_fini:
163*4882a593Smuzhiyun apei_resources_fini(&bert_resources);
164*4882a593Smuzhiyun out_put_bert_tab:
165*4882a593Smuzhiyun acpi_put_table((struct acpi_table_header *)bert_tab);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun return rc;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun late_initcall(bert_init);
171