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