1 // SPDX-License-Identifier: GPL-2.0-only
3 * Extended Error Log driver
5 * Copyright (C) 2013 Intel Corp.
6 * Author: Chen, Gong <gong.chen@intel.com>
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>
19 #include "apei/apei-internal.h"
20 #include <ras/ras_event.h>
22 #define EXT_ELOG_ENTRY_MASK GENMASK_ULL(51, 0) /* elog entry address mask */
24 #define EXTLOG_DSM_REV 0x0
25 #define EXTLOG_FN_ADDR 0x1
27 #define FLAG_OS_OPTIN BIT(0)
28 #define ELOG_ENTRY_VALID (1ULL<<63)
29 #define ELOG_ENTRY_LEN 0x1000
32 "Can not request iomem region <0x%016llx-0x%016llx> - eMCA disabled\n"
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 */
42 u32 entries
; /* Valid L1 Directory entries per logical processor */
46 static u8 extlog_dsm_uuid
[] __initdata
= "663E35AF-CC10-41A4-88EA-5470AF055295";
48 /* L1 table related physical address */
50 static size_t elog_size
;
51 static u64 l1_dirbase
;
52 static size_t l1_size
;
54 /* L1 table related virtual address */
55 static void __iomem
*extlog_l1_addr
;
56 static void __iomem
*elog_addr
;
58 static void *elog_buf
;
60 static u64
*l1_entry_base
;
61 static u32 l1_percpu_entry
;
63 #define ELOG_IDX(cpu, bank) \
64 (cpu_physical_id(cpu) * l1_percpu_entry + (bank))
66 #define ELOG_ENTRY_DATA(idx) \
67 (*(l1_entry_base + (idx)))
69 #define ELOG_ENTRY_ADDR(phyaddr) \
70 (phyaddr - elog_base + (u8 *)elog_addr)
72 static struct acpi_hest_generic_status
*extlog_elog_entry_check(int cpu
, int bank
)
76 struct acpi_hest_generic_status
*estatus
;
79 idx
= ELOG_IDX(cpu
, bank
);
80 data
= ELOG_ENTRY_DATA(idx
);
81 if ((data
& ELOG_ENTRY_VALID
) == 0)
84 data
&= EXT_ELOG_ENTRY_MASK
;
85 estatus
= (struct acpi_hest_generic_status
*)ELOG_ENTRY_ADDR(data
);
87 /* if no valid data in elog entry, just return */
88 if (estatus
->block_status
== 0)
94 static void __print_extlog_rcd(const char *pfx
,
95 struct acpi_hest_generic_status
*estatus
, int cpu
)
97 static atomic_t seqno
;
98 unsigned int curr_seqno
;
102 if (estatus
->error_severity
<= CPER_SEV_CORRECTED
)
107 curr_seqno
= atomic_inc_return(&seqno
);
108 snprintf(pfx_seq
, sizeof(pfx_seq
), "%s{%u}", pfx
, curr_seqno
);
109 printk("%s""Hardware error detected on CPU%d\n", pfx_seq
, cpu
);
110 cper_estatus_print(pfx_seq
, estatus
);
113 static int print_extlog_rcd(const char *pfx
,
114 struct acpi_hest_generic_status
*estatus
, int cpu
)
116 /* Not more than 2 messages every 5 seconds */
117 static DEFINE_RATELIMIT_STATE(ratelimit_corrected
, 5*HZ
, 2);
118 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected
, 5*HZ
, 2);
119 struct ratelimit_state
*ratelimit
;
121 if (estatus
->error_severity
== CPER_SEV_CORRECTED
||
122 (estatus
->error_severity
== CPER_SEV_INFORMATIONAL
))
123 ratelimit
= &ratelimit_corrected
;
125 ratelimit
= &ratelimit_uncorrected
;
126 if (__ratelimit(ratelimit
)) {
127 __print_extlog_rcd(pfx
, estatus
, cpu
);
134 static int extlog_print(struct notifier_block
*nb
, unsigned long val
,
137 struct mce
*mce
= (struct mce
*)data
;
138 int bank
= mce
->bank
;
139 int cpu
= mce
->extcpu
;
140 struct acpi_hest_generic_status
*estatus
, *tmp
;
141 struct acpi_hest_generic_data
*gdata
;
142 const guid_t
*fru_id
;
147 estatus
= extlog_elog_entry_check(cpu
, bank
);
151 if (mce
->kflags
& MCE_HANDLED_CEC
) {
152 estatus
->block_status
= 0;
156 memcpy(elog_buf
, (void *)estatus
, ELOG_ENTRY_LEN
);
157 /* clear record status to enable BIOS to update it again */
158 estatus
->block_status
= 0;
160 tmp
= (struct acpi_hest_generic_status
*)elog_buf
;
162 if (!ras_userspace_consumers()) {
163 print_extlog_rcd(NULL
, tmp
, cpu
);
167 /* log event via trace */
169 apei_estatus_for_each_section(tmp
, gdata
) {
170 if (gdata
->validation_bits
& CPER_SEC_VALID_FRU_ID
)
171 fru_id
= (guid_t
*)gdata
->fru_id
;
174 if (gdata
->validation_bits
& CPER_SEC_VALID_FRU_TEXT
)
175 fru_text
= gdata
->fru_text
;
178 sec_type
= (guid_t
*)gdata
->section_type
;
179 if (guid_equal(sec_type
, &CPER_SEC_PLATFORM_MEM
)) {
180 struct cper_sec_mem_err
*mem
= acpi_hest_get_payload(gdata
);
182 if (gdata
->error_data_length
>= sizeof(*mem
))
183 trace_extlog_mem_event(mem
, err_seq
, fru_id
, fru_text
,
184 (u8
)gdata
->error_severity
);
189 mce
->kflags
|= MCE_HANDLED_EXTLOG
;
193 static bool __init
extlog_get_l1addr(void)
197 union acpi_object
*obj
;
199 if (guid_parse(extlog_dsm_uuid
, &guid
))
201 if (ACPI_FAILURE(acpi_get_handle(NULL
, "\\_SB", &handle
)))
203 if (!acpi_check_dsm(handle
, &guid
, EXTLOG_DSM_REV
, 1 << EXTLOG_FN_ADDR
))
205 obj
= acpi_evaluate_dsm_typed(handle
, &guid
, EXTLOG_DSM_REV
,
206 EXTLOG_FN_ADDR
, NULL
, ACPI_TYPE_INTEGER
);
210 l1_dirbase
= obj
->integer
.value
;
214 /* Spec says L1 directory must be 4K aligned, bail out if it isn't */
215 if (l1_dirbase
& ((1 << 12) - 1)) {
216 pr_warn(FW_BUG
"L1 Directory is invalid at physical %llx\n",
223 static struct notifier_block extlog_mce_dec
= {
224 .notifier_call
= extlog_print
,
225 .priority
= MCE_PRIO_EXTLOG
,
228 static int __init
extlog_init(void)
230 struct extlog_l1_head
*l1_head
;
231 void __iomem
*extlog_l1_hdr
;
237 if (rdmsrl_safe(MSR_IA32_MCG_CAP
, &cap
) ||
238 !(cap
& MCG_ELOG_P
) ||
239 !extlog_get_l1addr())
243 /* get L1 header to fetch necessary information */
244 l1_hdr_size
= sizeof(struct extlog_l1_head
);
245 r
= request_mem_region(l1_dirbase
, l1_hdr_size
, "L1 DIR HDR");
247 pr_warn(FW_BUG EMCA_BUG
,
248 (unsigned long long)l1_dirbase
,
249 (unsigned long long)l1_dirbase
+ l1_hdr_size
);
253 extlog_l1_hdr
= acpi_os_map_iomem(l1_dirbase
, l1_hdr_size
);
254 l1_head
= (struct extlog_l1_head
*)extlog_l1_hdr
;
255 l1_size
= l1_head
->total_len
;
256 l1_percpu_entry
= l1_head
->entries
;
257 elog_base
= l1_head
->elog_base
;
258 elog_size
= l1_head
->elog_len
;
259 acpi_os_unmap_iomem(extlog_l1_hdr
, l1_hdr_size
);
260 release_mem_region(l1_dirbase
, l1_hdr_size
);
262 /* remap L1 header again based on completed information */
263 r
= request_mem_region(l1_dirbase
, l1_size
, "L1 Table");
265 pr_warn(FW_BUG EMCA_BUG
,
266 (unsigned long long)l1_dirbase
,
267 (unsigned long long)l1_dirbase
+ l1_size
);
270 extlog_l1_addr
= acpi_os_map_iomem(l1_dirbase
, l1_size
);
271 l1_entry_base
= (u64
*)((u8
*)extlog_l1_addr
+ l1_hdr_size
);
273 /* remap elog table */
274 r
= request_mem_region(elog_base
, elog_size
, "Elog Table");
276 pr_warn(FW_BUG EMCA_BUG
,
277 (unsigned long long)elog_base
,
278 (unsigned long long)elog_base
+ elog_size
);
279 goto err_release_l1_dir
;
281 elog_addr
= acpi_os_map_iomem(elog_base
, elog_size
);
284 /* allocate buffer to save elog record */
285 elog_buf
= kmalloc(ELOG_ENTRY_LEN
, GFP_KERNEL
);
286 if (elog_buf
== NULL
)
287 goto err_release_elog
;
289 mce_register_decode_chain(&extlog_mce_dec
);
290 /* enable OS to be involved to take over management from BIOS */
291 ((struct extlog_l1_head
*)extlog_l1_addr
)->flags
|= FLAG_OS_OPTIN
;
297 acpi_os_unmap_iomem(elog_addr
, elog_size
);
298 release_mem_region(elog_base
, elog_size
);
301 acpi_os_unmap_iomem(extlog_l1_addr
, l1_size
);
302 release_mem_region(l1_dirbase
, l1_size
);
304 pr_warn(FW_BUG
"Extended error log disabled because of problems parsing f/w tables\n");
308 static void __exit
extlog_exit(void)
310 mce_unregister_decode_chain(&extlog_mce_dec
);
311 if (extlog_l1_addr
) {
312 ((struct extlog_l1_head
*)extlog_l1_addr
)->flags
&= ~FLAG_OS_OPTIN
;
313 acpi_os_unmap_iomem(extlog_l1_addr
, l1_size
);
316 acpi_os_unmap_iomem(elog_addr
, elog_size
);
317 release_mem_region(elog_base
, elog_size
);
318 release_mem_region(l1_dirbase
, l1_size
);
322 module_init(extlog_init
);
323 module_exit(extlog_exit
);
325 MODULE_AUTHOR("Chen, Gong <gong.chen@intel.com>");
326 MODULE_DESCRIPTION("Extended MCA Error Log Driver");
327 MODULE_LICENSE("GPL");