2 * Extended Error Log driver
4 * Copyright (C) 2013 Intel Corp.
5 * Author: Chen, Gong <gong.chen@intel.com>
7 * This file is licensed under GPLv2.
10 #include <linux/module.h>
11 #include <linux/acpi.h>
12 #include <linux/cper.h>
13 #include <linux/ratelimit.h>
14 #include <linux/edac.h>
15 #include <linux/ras.h>
16 #include <acpi/ghes.h>
20 #include "apei/apei-internal.h"
21 #include <ras/ras_event.h>
23 #define EXT_ELOG_ENTRY_MASK GENMASK_ULL(51, 0) /* elog entry address mask */
25 #define EXTLOG_DSM_REV 0x0
26 #define EXTLOG_FN_ADDR 0x1
28 #define FLAG_OS_OPTIN BIT(0)
29 #define ELOG_ENTRY_VALID (1ULL<<63)
30 #define ELOG_ENTRY_LEN 0x1000
33 "Can not request iomem region <0x%016llx-0x%016llx> - eMCA disabled\n"
35 struct extlog_l1_head {
36 u32 ver; /* Header Version */
37 u32 hdr_len; /* Header Length */
38 u64 total_len; /* entire L1 Directory length including this header */
39 u64 elog_base; /* MCA Error Log Directory base address */
40 u64 elog_len; /* MCA Error Log Directory length */
41 u32 flags; /* bit 0 - OS/VMM Opt-in */
43 u32 entries; /* Valid L1 Directory entries per logical processor */
47 static int old_edac_report_status;
49 static u8 extlog_dsm_uuid[] __initdata = "663E35AF-CC10-41A4-88EA-5470AF055295";
51 /* L1 table related physical address */
53 static size_t elog_size;
54 static u64 l1_dirbase;
55 static size_t l1_size;
57 /* L1 table related virtual address */
58 static void __iomem *extlog_l1_addr;
59 static void __iomem *elog_addr;
61 static void *elog_buf;
63 static u64 *l1_entry_base;
64 static u32 l1_percpu_entry;
66 #define ELOG_IDX(cpu, bank) \
67 (cpu_physical_id(cpu) * l1_percpu_entry + (bank))
69 #define ELOG_ENTRY_DATA(idx) \
70 (*(l1_entry_base + (idx)))
72 #define ELOG_ENTRY_ADDR(phyaddr) \
73 (phyaddr - elog_base + (u8 *)elog_addr)
75 static struct acpi_hest_generic_status *extlog_elog_entry_check(int cpu, int bank)
79 struct acpi_hest_generic_status *estatus;
82 idx = ELOG_IDX(cpu, bank);
83 data = ELOG_ENTRY_DATA(idx);
84 if ((data & ELOG_ENTRY_VALID) == 0)
87 data &= EXT_ELOG_ENTRY_MASK;
88 estatus = (struct acpi_hest_generic_status *)ELOG_ENTRY_ADDR(data);
90 /* if no valid data in elog entry, just return */
91 if (estatus->block_status == 0)
97 static void __print_extlog_rcd(const char *pfx,
98 struct acpi_hest_generic_status *estatus, int cpu)
100 static atomic_t seqno;
101 unsigned int curr_seqno;
105 if (estatus->error_severity <= CPER_SEV_CORRECTED)
110 curr_seqno = atomic_inc_return(&seqno);
111 snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}", pfx, curr_seqno);
112 printk("%s""Hardware error detected on CPU%d\n", pfx_seq, cpu);
113 cper_estatus_print(pfx_seq, estatus);
116 static int print_extlog_rcd(const char *pfx,
117 struct acpi_hest_generic_status *estatus, int cpu)
119 /* Not more than 2 messages every 5 seconds */
120 static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
121 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
122 struct ratelimit_state *ratelimit;
124 if (estatus->error_severity == CPER_SEV_CORRECTED ||
125 (estatus->error_severity == CPER_SEV_INFORMATIONAL))
126 ratelimit = &ratelimit_corrected;
128 ratelimit = &ratelimit_uncorrected;
129 if (__ratelimit(ratelimit)) {
130 __print_extlog_rcd(pfx, estatus, cpu);
137 static int extlog_print(struct notifier_block *nb, unsigned long val,
140 struct mce *mce = (struct mce *)data;
141 int bank = mce->bank;
142 int cpu = mce->extcpu;
143 struct acpi_hest_generic_status *estatus, *tmp;
144 struct acpi_hest_generic_data *gdata;
145 const guid_t *fru_id;
150 estatus = extlog_elog_entry_check(cpu, bank);
154 memcpy(elog_buf, (void *)estatus, ELOG_ENTRY_LEN);
155 /* clear record status to enable BIOS to update it again */
156 estatus->block_status = 0;
158 tmp = (struct acpi_hest_generic_status *)elog_buf;
160 if (!ras_userspace_consumers()) {
161 print_extlog_rcd(NULL, tmp, cpu);
165 /* log event via trace */
167 apei_estatus_for_each_section(tmp, gdata) {
168 if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
169 fru_id = (guid_t *)gdata->fru_id;
172 if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
173 fru_text = gdata->fru_text;
176 sec_type = (guid_t *)gdata->section_type;
177 if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
178 struct cper_sec_mem_err *mem = (void *)(gdata + 1);
180 if (gdata->error_data_length >= sizeof(*mem))
181 trace_extlog_mem_event(mem, err_seq, fru_id, fru_text,
182 (u8)gdata->error_severity);
190 static bool __init extlog_get_l1addr(void)
194 union acpi_object *obj;
196 if (guid_parse(extlog_dsm_uuid, &guid))
198 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
200 if (!acpi_check_dsm(handle, &guid, EXTLOG_DSM_REV, 1 << EXTLOG_FN_ADDR))
202 obj = acpi_evaluate_dsm_typed(handle, &guid, EXTLOG_DSM_REV,
203 EXTLOG_FN_ADDR, NULL, ACPI_TYPE_INTEGER);
207 l1_dirbase = obj->integer.value;
211 /* Spec says L1 directory must be 4K aligned, bail out if it isn't */
212 if (l1_dirbase & ((1 << 12) - 1)) {
213 pr_warn(FW_BUG "L1 Directory is invalid at physical %llx\n",
220 static struct notifier_block extlog_mce_dec = {
221 .notifier_call = extlog_print,
222 .priority = MCE_PRIO_EXTLOG,
225 static int __init extlog_init(void)
227 struct extlog_l1_head *l1_head;
228 void __iomem *extlog_l1_hdr;
234 if (rdmsrl_safe(MSR_IA32_MCG_CAP, &cap) ||
235 !(cap & MCG_ELOG_P) ||
236 !extlog_get_l1addr())
239 if (edac_get_report_status() == EDAC_REPORTING_FORCE) {
240 pr_warn("Not loading eMCA, error reporting force-enabled through EDAC.\n");
245 /* get L1 header to fetch necessary information */
246 l1_hdr_size = sizeof(struct extlog_l1_head);
247 r = request_mem_region(l1_dirbase, l1_hdr_size, "L1 DIR HDR");
249 pr_warn(FW_BUG EMCA_BUG,
250 (unsigned long long)l1_dirbase,
251 (unsigned long long)l1_dirbase + l1_hdr_size);
255 extlog_l1_hdr = acpi_os_map_iomem(l1_dirbase, l1_hdr_size);
256 l1_head = (struct extlog_l1_head *)extlog_l1_hdr;
257 l1_size = l1_head->total_len;
258 l1_percpu_entry = l1_head->entries;
259 elog_base = l1_head->elog_base;
260 elog_size = l1_head->elog_len;
261 acpi_os_unmap_iomem(extlog_l1_hdr, l1_hdr_size);
262 release_mem_region(l1_dirbase, l1_hdr_size);
264 /* remap L1 header again based on completed information */
265 r = request_mem_region(l1_dirbase, l1_size, "L1 Table");
267 pr_warn(FW_BUG EMCA_BUG,
268 (unsigned long long)l1_dirbase,
269 (unsigned long long)l1_dirbase + l1_size);
272 extlog_l1_addr = acpi_os_map_iomem(l1_dirbase, l1_size);
273 l1_entry_base = (u64 *)((u8 *)extlog_l1_addr + l1_hdr_size);
275 /* remap elog table */
276 r = request_mem_region(elog_base, elog_size, "Elog Table");
278 pr_warn(FW_BUG EMCA_BUG,
279 (unsigned long long)elog_base,
280 (unsigned long long)elog_base + elog_size);
281 goto err_release_l1_dir;
283 elog_addr = acpi_os_map_iomem(elog_base, elog_size);
286 /* allocate buffer to save elog record */
287 elog_buf = kmalloc(ELOG_ENTRY_LEN, GFP_KERNEL);
288 if (elog_buf == NULL)
289 goto err_release_elog;
292 * eMCA event report method has higher priority than EDAC method,
293 * unless EDAC event report method is mandatory.
295 old_edac_report_status = edac_get_report_status();
296 edac_set_report_status(EDAC_REPORTING_DISABLED);
297 mce_register_decode_chain(&extlog_mce_dec);
298 /* enable OS to be involved to take over management from BIOS */
299 ((struct extlog_l1_head *)extlog_l1_addr)->flags |= FLAG_OS_OPTIN;
305 acpi_os_unmap_iomem(elog_addr, elog_size);
306 release_mem_region(elog_base, elog_size);
309 acpi_os_unmap_iomem(extlog_l1_addr, l1_size);
310 release_mem_region(l1_dirbase, l1_size);
312 pr_warn(FW_BUG "Extended error log disabled because of problems parsing f/w tables\n");
316 static void __exit extlog_exit(void)
318 edac_set_report_status(old_edac_report_status);
319 mce_unregister_decode_chain(&extlog_mce_dec);
320 ((struct extlog_l1_head *)extlog_l1_addr)->flags &= ~FLAG_OS_OPTIN;
322 acpi_os_unmap_iomem(extlog_l1_addr, l1_size);
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);
330 module_init(extlog_init);
331 module_exit(extlog_exit);
333 MODULE_AUTHOR("Chen, Gong <gong.chen@intel.com>");
334 MODULE_DESCRIPTION("Extended MCA Error Log Driver");
335 MODULE_LICENSE("GPL");