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