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