1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Extended Error Log driver
4 *
5 * Copyright (C) 2013 Intel Corp.
6 * Author: Chen, Gong <gong.chen@intel.com>
7 */
8
9 #include <linux/module.h>
10 #include <linux/acpi.h>
11 #include <linux/cper.h>
12 #include <linux/ratelimit.h>
13 #include <linux/edac.h>
14 #include <linux/ras.h>
15 #include <acpi/ghes.h>
16 #include <asm/cpu.h>
17 #include <asm/mce.h>
18
19 #include "apei/apei-internal.h"
20 #include <ras/ras_event.h>
21
22 #define EXT_ELOG_ENTRY_MASK GENMASK_ULL(51, 0) /* elog entry address mask */
23
24 #define EXTLOG_DSM_REV 0x0
25 #define EXTLOG_FN_ADDR 0x1
26
27 #define FLAG_OS_OPTIN BIT(0)
28 #define ELOG_ENTRY_VALID (1ULL<<63)
29 #define ELOG_ENTRY_LEN 0x1000
30
31 #define EMCA_BUG \
32 "Can not request iomem region <0x%016llx-0x%016llx> - eMCA disabled\n"
33
34 struct extlog_l1_head {
35 u32 ver; /* Header Version */
36 u32 hdr_len; /* Header Length */
37 u64 total_len; /* entire L1 Directory length including this header */
38 u64 elog_base; /* MCA Error Log Directory base address */
39 u64 elog_len; /* MCA Error Log Directory length */
40 u32 flags; /* bit 0 - OS/VMM Opt-in */
41 u8 rev0[12];
42 u32 entries; /* Valid L1 Directory entries per logical processor */
43 u8 rev1[12];
44 };
45
46 static int old_edac_report_status;
47
48 static u8 extlog_dsm_uuid[] __initdata = "663E35AF-CC10-41A4-88EA-5470AF055295";
49
50 /* L1 table related physical address */
51 static u64 elog_base;
52 static size_t elog_size;
53 static u64 l1_dirbase;
54 static size_t l1_size;
55
56 /* L1 table related virtual address */
57 static void __iomem *extlog_l1_addr;
58 static void __iomem *elog_addr;
59
60 static void *elog_buf;
61
62 static u64 *l1_entry_base;
63 static u32 l1_percpu_entry;
64
65 #define ELOG_IDX(cpu, bank) \
66 (cpu_physical_id(cpu) * l1_percpu_entry + (bank))
67
68 #define ELOG_ENTRY_DATA(idx) \
69 (*(l1_entry_base + (idx)))
70
71 #define ELOG_ENTRY_ADDR(phyaddr) \
72 (phyaddr - elog_base + (u8 *)elog_addr)
73
extlog_elog_entry_check(int cpu,int bank)74 static struct acpi_hest_generic_status *extlog_elog_entry_check(int cpu, int bank)
75 {
76 int idx;
77 u64 data;
78 struct acpi_hest_generic_status *estatus;
79
80 WARN_ON(cpu < 0);
81 idx = ELOG_IDX(cpu, bank);
82 data = ELOG_ENTRY_DATA(idx);
83 if ((data & ELOG_ENTRY_VALID) == 0)
84 return NULL;
85
86 data &= EXT_ELOG_ENTRY_MASK;
87 estatus = (struct acpi_hest_generic_status *)ELOG_ENTRY_ADDR(data);
88
89 /* if no valid data in elog entry, just return */
90 if (estatus->block_status == 0)
91 return NULL;
92
93 return estatus;
94 }
95
__print_extlog_rcd(const char * pfx,struct acpi_hest_generic_status * estatus,int cpu)96 static void __print_extlog_rcd(const char *pfx,
97 struct acpi_hest_generic_status *estatus, int cpu)
98 {
99 static atomic_t seqno;
100 unsigned int curr_seqno;
101 char pfx_seq[64];
102
103 if (!pfx) {
104 if (estatus->error_severity <= CPER_SEV_CORRECTED)
105 pfx = KERN_INFO;
106 else
107 pfx = KERN_ERR;
108 }
109 curr_seqno = atomic_inc_return(&seqno);
110 snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}", pfx, curr_seqno);
111 printk("%s""Hardware error detected on CPU%d\n", pfx_seq, cpu);
112 cper_estatus_print(pfx_seq, estatus);
113 }
114
print_extlog_rcd(const char * pfx,struct acpi_hest_generic_status * estatus,int cpu)115 static int print_extlog_rcd(const char *pfx,
116 struct acpi_hest_generic_status *estatus, int cpu)
117 {
118 /* Not more than 2 messages every 5 seconds */
119 static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
120 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
121 struct ratelimit_state *ratelimit;
122
123 if (estatus->error_severity == CPER_SEV_CORRECTED ||
124 (estatus->error_severity == CPER_SEV_INFORMATIONAL))
125 ratelimit = &ratelimit_corrected;
126 else
127 ratelimit = &ratelimit_uncorrected;
128 if (__ratelimit(ratelimit)) {
129 __print_extlog_rcd(pfx, estatus, cpu);
130 return 0;
131 }
132
133 return 1;
134 }
135
extlog_print(struct notifier_block * nb,unsigned long val,void * data)136 static int extlog_print(struct notifier_block *nb, unsigned long val,
137 void *data)
138 {
139 struct mce *mce = (struct mce *)data;
140 int bank = mce->bank;
141 int cpu = mce->extcpu;
142 struct acpi_hest_generic_status *estatus, *tmp;
143 struct acpi_hest_generic_data *gdata;
144 const guid_t *fru_id;
145 char *fru_text;
146 guid_t *sec_type;
147 static u32 err_seq;
148
149 estatus = extlog_elog_entry_check(cpu, bank);
150 if (estatus == NULL)
151 return NOTIFY_DONE;
152
153 memcpy(elog_buf, (void *)estatus, ELOG_ENTRY_LEN);
154 /* clear record status to enable BIOS to update it again */
155 estatus->block_status = 0;
156
157 tmp = (struct acpi_hest_generic_status *)elog_buf;
158
159 if (!ras_userspace_consumers()) {
160 print_extlog_rcd(NULL, tmp, cpu);
161 goto out;
162 }
163
164 /* log event via trace */
165 err_seq++;
166 apei_estatus_for_each_section(tmp, gdata) {
167 if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
168 fru_id = (guid_t *)gdata->fru_id;
169 else
170 fru_id = &guid_null;
171 if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
172 fru_text = gdata->fru_text;
173 else
174 fru_text = "";
175 sec_type = (guid_t *)gdata->section_type;
176 if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
177 struct cper_sec_mem_err *mem = (void *)(gdata + 1);
178
179 if (gdata->error_data_length >= sizeof(*mem))
180 trace_extlog_mem_event(mem, err_seq, fru_id, fru_text,
181 (u8)gdata->error_severity);
182 }
183 }
184
185 out:
186 return NOTIFY_STOP;
187 }
188
extlog_get_l1addr(void)189 static bool __init extlog_get_l1addr(void)
190 {
191 guid_t guid;
192 acpi_handle handle;
193 union acpi_object *obj;
194
195 if (guid_parse(extlog_dsm_uuid, &guid))
196 return false;
197 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
198 return false;
199 if (!acpi_check_dsm(handle, &guid, EXTLOG_DSM_REV, 1 << EXTLOG_FN_ADDR))
200 return false;
201 obj = acpi_evaluate_dsm_typed(handle, &guid, EXTLOG_DSM_REV,
202 EXTLOG_FN_ADDR, NULL, ACPI_TYPE_INTEGER);
203 if (!obj) {
204 return false;
205 } else {
206 l1_dirbase = obj->integer.value;
207 ACPI_FREE(obj);
208 }
209
210 /* Spec says L1 directory must be 4K aligned, bail out if it isn't */
211 if (l1_dirbase & ((1 << 12) - 1)) {
212 pr_warn(FW_BUG "L1 Directory is invalid at physical %llx\n",
213 l1_dirbase);
214 return false;
215 }
216
217 return true;
218 }
219 static struct notifier_block extlog_mce_dec = {
220 .notifier_call = extlog_print,
221 .priority = MCE_PRIO_EXTLOG,
222 };
223
extlog_init(void)224 static int __init extlog_init(void)
225 {
226 struct extlog_l1_head *l1_head;
227 void __iomem *extlog_l1_hdr;
228 size_t l1_hdr_size;
229 struct resource *r;
230 u64 cap;
231 int rc;
232
233 if (rdmsrl_safe(MSR_IA32_MCG_CAP, &cap) ||
234 !(cap & MCG_ELOG_P) ||
235 !extlog_get_l1addr())
236 return -ENODEV;
237
238 if (edac_get_report_status() == EDAC_REPORTING_FORCE) {
239 pr_warn("Not loading eMCA, error reporting force-enabled through EDAC.\n");
240 return -EPERM;
241 }
242
243 rc = -EINVAL;
244 /* get L1 header to fetch necessary information */
245 l1_hdr_size = sizeof(struct extlog_l1_head);
246 r = request_mem_region(l1_dirbase, l1_hdr_size, "L1 DIR HDR");
247 if (!r) {
248 pr_warn(FW_BUG EMCA_BUG,
249 (unsigned long long)l1_dirbase,
250 (unsigned long long)l1_dirbase + l1_hdr_size);
251 goto err;
252 }
253
254 extlog_l1_hdr = acpi_os_map_iomem(l1_dirbase, l1_hdr_size);
255 l1_head = (struct extlog_l1_head *)extlog_l1_hdr;
256 l1_size = l1_head->total_len;
257 l1_percpu_entry = l1_head->entries;
258 elog_base = l1_head->elog_base;
259 elog_size = l1_head->elog_len;
260 acpi_os_unmap_iomem(extlog_l1_hdr, l1_hdr_size);
261 release_mem_region(l1_dirbase, l1_hdr_size);
262
263 /* remap L1 header again based on completed information */
264 r = request_mem_region(l1_dirbase, l1_size, "L1 Table");
265 if (!r) {
266 pr_warn(FW_BUG EMCA_BUG,
267 (unsigned long long)l1_dirbase,
268 (unsigned long long)l1_dirbase + l1_size);
269 goto err;
270 }
271 extlog_l1_addr = acpi_os_map_iomem(l1_dirbase, l1_size);
272 l1_entry_base = (u64 *)((u8 *)extlog_l1_addr + l1_hdr_size);
273
274 /* remap elog table */
275 r = request_mem_region(elog_base, elog_size, "Elog Table");
276 if (!r) {
277 pr_warn(FW_BUG EMCA_BUG,
278 (unsigned long long)elog_base,
279 (unsigned long long)elog_base + elog_size);
280 goto err_release_l1_dir;
281 }
282 elog_addr = acpi_os_map_iomem(elog_base, elog_size);
283
284 rc = -ENOMEM;
285 /* allocate buffer to save elog record */
286 elog_buf = kmalloc(ELOG_ENTRY_LEN, GFP_KERNEL);
287 if (elog_buf == NULL)
288 goto err_release_elog;
289
290 /*
291 * eMCA event report method has higher priority than EDAC method,
292 * unless EDAC event report method is mandatory.
293 */
294 old_edac_report_status = edac_get_report_status();
295 edac_set_report_status(EDAC_REPORTING_DISABLED);
296 mce_register_decode_chain(&extlog_mce_dec);
297 /* enable OS to be involved to take over management from BIOS */
298 ((struct extlog_l1_head *)extlog_l1_addr)->flags |= FLAG_OS_OPTIN;
299
300 return 0;
301
302 err_release_elog:
303 if (elog_addr)
304 acpi_os_unmap_iomem(elog_addr, elog_size);
305 release_mem_region(elog_base, elog_size);
306 err_release_l1_dir:
307 if (extlog_l1_addr)
308 acpi_os_unmap_iomem(extlog_l1_addr, l1_size);
309 release_mem_region(l1_dirbase, l1_size);
310 err:
311 pr_warn(FW_BUG "Extended error log disabled because of problems parsing f/w tables\n");
312 return rc;
313 }
314
extlog_exit(void)315 static void __exit extlog_exit(void)
316 {
317 edac_set_report_status(old_edac_report_status);
318 mce_unregister_decode_chain(&extlog_mce_dec);
319 if (extlog_l1_addr) {
320 ((struct extlog_l1_head *)extlog_l1_addr)->flags &= ~FLAG_OS_OPTIN;
321 acpi_os_unmap_iomem(extlog_l1_addr, l1_size);
322 }
323 if (elog_addr)
324 acpi_os_unmap_iomem(elog_addr, elog_size);
325 release_mem_region(elog_base, elog_size);
326 release_mem_region(l1_dirbase, l1_size);
327 kfree(elog_buf);
328 }
329
330 module_init(extlog_init);
331 module_exit(extlog_exit);
332
333 MODULE_AUTHOR("Chen, Gong <gong.chen@intel.com>");
334 MODULE_DESCRIPTION("Extended MCA Error Log Driver");
335 MODULE_LICENSE("GPL");
336