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