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