blob: e61da957f30f14c1e4917b450179e78afa994a1d [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>
Chen, Gong4b3db702013-10-21 14:29:25 -070012#include <linux/cper.h>
13#include <linux/ratelimit.h>
Chen, Gong42139eb2013-12-06 01:17:10 -050014#include <linux/edac.h>
Chen, Gong4b3db702013-10-21 14:29:25 -070015#include <asm/cpu.h>
16#include <asm/mce.h>
17
18#include "apei/apei-internal.h"
Chen, Gong2dfb7d52014-06-17 22:33:07 -040019#include <ras/ras_event.h>
Chen, Gong4b3db702013-10-21 14:29:25 -070020
21#define EXT_ELOG_ENTRY_MASK GENMASK_ULL(51, 0) /* elog entry address mask */
22
23#define EXTLOG_DSM_REV 0x0
Chen, Gong4b3db702013-10-21 14:29:25 -070024#define EXTLOG_FN_ADDR 0x1
25
26#define FLAG_OS_OPTIN BIT(0)
Chen, Gong4b3db702013-10-21 14:29:25 -070027#define ELOG_ENTRY_VALID (1ULL<<63)
28#define ELOG_ENTRY_LEN 0x1000
29
30#define EMCA_BUG \
31 "Can not request iomem region <0x%016llx-0x%016llx> - eMCA disabled\n"
32
33struct extlog_l1_head {
34 u32 ver; /* Header Version */
35 u32 hdr_len; /* Header Length */
36 u64 total_len; /* entire L1 Directory length including this header */
37 u64 elog_base; /* MCA Error Log Directory base address */
38 u64 elog_len; /* MCA Error Log Directory length */
39 u32 flags; /* bit 0 - OS/VMM Opt-in */
40 u8 rev0[12];
41 u32 entries; /* Valid L1 Directory entries per logical processor */
42 u8 rev1[12];
43};
44
Chen, Gong42139eb2013-12-06 01:17:10 -050045static int old_edac_report_status;
46
Jiang Liu7ede9f82013-12-19 20:47:46 +080047static u8 extlog_dsm_uuid[] __initdata = "663E35AF-CC10-41A4-88EA-5470AF055295";
Chen, Gong4b3db702013-10-21 14:29:25 -070048
49/* L1 table related physical address */
50static u64 elog_base;
51static size_t elog_size;
52static u64 l1_dirbase;
53static size_t l1_size;
54
55/* L1 table related virtual address */
56static void __iomem *extlog_l1_addr;
57static void __iomem *elog_addr;
58
59static void *elog_buf;
60
61static u64 *l1_entry_base;
62static 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
73static struct acpi_generic_status *extlog_elog_entry_check(int cpu, int bank)
74{
75 int idx;
76 u64 data;
77 struct acpi_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_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
95static void __print_extlog_rcd(const char *pfx,
96 struct acpi_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
114static int print_extlog_rcd(const char *pfx,
115 struct acpi_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
135static 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;
Chen, Gong2dfb7d52014-06-17 22:33:07 -0400141 struct acpi_generic_status *estatus, *tmp;
142 struct acpi_generic_data *gdata;
143 const uuid_le *fru_id = &NULL_UUID_LE;
144 char *fru_text = "";
145 uuid_le *sec_type;
146 static u32 err_seq;
Chen, Gong4b3db702013-10-21 14:29:25 -0700147
148 estatus = extlog_elog_entry_check(cpu, bank);
149 if (estatus == NULL)
150 return NOTIFY_DONE;
151
152 memcpy(elog_buf, (void *)estatus, ELOG_ENTRY_LEN);
153 /* clear record status to enable BIOS to update it again */
154 estatus->block_status = 0;
155
Chen, Gong2dfb7d52014-06-17 22:33:07 -0400156 tmp = (struct acpi_generic_status *)elog_buf;
157 print_extlog_rcd(NULL, tmp, cpu);
158
159 /* log event via trace */
160 err_seq++;
161 gdata = (struct acpi_generic_data *)(tmp + 1);
162 if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
163 fru_id = (uuid_le *)gdata->fru_id;
164 if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
165 fru_text = gdata->fru_text;
166 sec_type = (uuid_le *)gdata->section_type;
167 if (!uuid_le_cmp(*sec_type, CPER_SEC_PLATFORM_MEM)) {
168 struct cper_sec_mem_err *mem = (void *)(gdata + 1);
169 if (gdata->error_data_length >= sizeof(*mem))
170 trace_extlog_mem_event(mem, err_seq, fru_id, fru_text,
171 (u8)gdata->error_severity);
172 }
Chen, Gong4b3db702013-10-21 14:29:25 -0700173
Chen, Gong42139eb2013-12-06 01:17:10 -0500174 return NOTIFY_STOP;
Chen, Gong4b3db702013-10-21 14:29:25 -0700175}
176
Jiang Liu7ede9f82013-12-19 20:47:46 +0800177static bool __init extlog_get_l1addr(void)
Chen, Gong4b3db702013-10-21 14:29:25 -0700178{
Chen, Gong4b3db702013-10-21 14:29:25 -0700179 u8 uuid[16];
Jiang Liu7ede9f82013-12-19 20:47:46 +0800180 acpi_handle handle;
181 union acpi_object *obj;
Chen, Gong4b3db702013-10-21 14:29:25 -0700182
183 acpi_str_to_uuid(extlog_dsm_uuid, uuid);
Chen, Gong4b3db702013-10-21 14:29:25 -0700184
185 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
186 return false;
Jiang Liu7ede9f82013-12-19 20:47:46 +0800187 if (!acpi_check_dsm(handle, uuid, EXTLOG_DSM_REV, 1 << EXTLOG_FN_ADDR))
Chen, Gong4b3db702013-10-21 14:29:25 -0700188 return false;
Jiang Liu7ede9f82013-12-19 20:47:46 +0800189 obj = acpi_evaluate_dsm_typed(handle, uuid, EXTLOG_DSM_REV,
190 EXTLOG_FN_ADDR, NULL, ACPI_TYPE_INTEGER);
191 if (!obj) {
Chen, Gong4b3db702013-10-21 14:29:25 -0700192 return false;
Jiang Liu7ede9f82013-12-19 20:47:46 +0800193 } else {
194 l1_dirbase = obj->integer.value;
195 ACPI_FREE(obj);
196 }
Chen, Gong4b3db702013-10-21 14:29:25 -0700197
Chen, Gong4b3db702013-10-21 14:29:25 -0700198 /* Spec says L1 directory must be 4K aligned, bail out if it isn't */
199 if (l1_dirbase & ((1 << 12) - 1)) {
200 pr_warn(FW_BUG "L1 Directory is invalid at physical %llx\n",
201 l1_dirbase);
202 return false;
203 }
204
205 return true;
206}
207static struct notifier_block extlog_mce_dec = {
208 .notifier_call = extlog_print,
209};
210
211static int __init extlog_init(void)
212{
213 struct extlog_l1_head *l1_head;
214 void __iomem *extlog_l1_hdr;
215 size_t l1_hdr_size;
216 struct resource *r;
217 u64 cap;
218 int rc;
219
Chen, Gong42139eb2013-12-06 01:17:10 -0500220 if (get_edac_report_status() == EDAC_REPORTING_FORCE) {
221 pr_warn("Not loading eMCA, error reporting force-enabled through EDAC.\n");
222 return -EPERM;
223 }
Chen, Gong4b3db702013-10-21 14:29:25 -0700224
Chen, Gong42139eb2013-12-06 01:17:10 -0500225 rc = -ENODEV;
Chen, Gong4b3db702013-10-21 14:29:25 -0700226 rdmsrl(MSR_IA32_MCG_CAP, cap);
227 if (!(cap & MCG_ELOG_P))
228 return rc;
229
230 if (!extlog_get_l1addr())
231 return rc;
232
233 rc = -EINVAL;
234 /* get L1 header to fetch necessary information */
235 l1_hdr_size = sizeof(struct extlog_l1_head);
236 r = request_mem_region(l1_dirbase, l1_hdr_size, "L1 DIR HDR");
237 if (!r) {
238 pr_warn(FW_BUG EMCA_BUG,
239 (unsigned long long)l1_dirbase,
240 (unsigned long long)l1_dirbase + l1_hdr_size);
241 goto err;
242 }
243
Lv Zhenga2383172014-05-20 15:39:41 +0800244 extlog_l1_hdr = acpi_os_map_iomem(l1_dirbase, l1_hdr_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700245 l1_head = (struct extlog_l1_head *)extlog_l1_hdr;
246 l1_size = l1_head->total_len;
247 l1_percpu_entry = l1_head->entries;
248 elog_base = l1_head->elog_base;
249 elog_size = l1_head->elog_len;
Lv Zhenga2383172014-05-20 15:39:41 +0800250 acpi_os_unmap_iomem(extlog_l1_hdr, l1_hdr_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700251 release_mem_region(l1_dirbase, l1_hdr_size);
252
253 /* remap L1 header again based on completed information */
254 r = request_mem_region(l1_dirbase, l1_size, "L1 Table");
255 if (!r) {
256 pr_warn(FW_BUG EMCA_BUG,
257 (unsigned long long)l1_dirbase,
258 (unsigned long long)l1_dirbase + l1_size);
259 goto err;
260 }
Lv Zhenga2383172014-05-20 15:39:41 +0800261 extlog_l1_addr = acpi_os_map_iomem(l1_dirbase, l1_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700262 l1_entry_base = (u64 *)((u8 *)extlog_l1_addr + l1_hdr_size);
263
264 /* remap elog table */
265 r = request_mem_region(elog_base, elog_size, "Elog Table");
266 if (!r) {
267 pr_warn(FW_BUG EMCA_BUG,
268 (unsigned long long)elog_base,
269 (unsigned long long)elog_base + elog_size);
270 goto err_release_l1_dir;
271 }
Lv Zhenga2383172014-05-20 15:39:41 +0800272 elog_addr = acpi_os_map_iomem(elog_base, elog_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700273
274 rc = -ENOMEM;
275 /* allocate buffer to save elog record */
276 elog_buf = kmalloc(ELOG_ENTRY_LEN, GFP_KERNEL);
277 if (elog_buf == NULL)
278 goto err_release_elog;
279
Chen, Gong42139eb2013-12-06 01:17:10 -0500280 /*
281 * eMCA event report method has higher priority than EDAC method,
282 * unless EDAC event report method is mandatory.
283 */
284 old_edac_report_status = get_edac_report_status();
285 set_edac_report_status(EDAC_REPORTING_DISABLED);
Chen, Gong4b3db702013-10-21 14:29:25 -0700286 mce_register_decode_chain(&extlog_mce_dec);
287 /* enable OS to be involved to take over management from BIOS */
288 ((struct extlog_l1_head *)extlog_l1_addr)->flags |= FLAG_OS_OPTIN;
289
290 return 0;
291
292err_release_elog:
293 if (elog_addr)
Lv Zhenga2383172014-05-20 15:39:41 +0800294 acpi_os_unmap_iomem(elog_addr, elog_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700295 release_mem_region(elog_base, elog_size);
296err_release_l1_dir:
297 if (extlog_l1_addr)
Lv Zhenga2383172014-05-20 15:39:41 +0800298 acpi_os_unmap_iomem(extlog_l1_addr, l1_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700299 release_mem_region(l1_dirbase, l1_size);
300err:
301 pr_warn(FW_BUG "Extended error log disabled because of problems parsing f/w tables\n");
302 return rc;
303}
304
305static void __exit extlog_exit(void)
306{
Chen, Gong42139eb2013-12-06 01:17:10 -0500307 set_edac_report_status(old_edac_report_status);
Chen, Gong4b3db702013-10-21 14:29:25 -0700308 mce_unregister_decode_chain(&extlog_mce_dec);
309 ((struct extlog_l1_head *)extlog_l1_addr)->flags &= ~FLAG_OS_OPTIN;
310 if (extlog_l1_addr)
Lv Zhenga2383172014-05-20 15:39:41 +0800311 acpi_os_unmap_iomem(extlog_l1_addr, l1_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700312 if (elog_addr)
Lv Zhenga2383172014-05-20 15:39:41 +0800313 acpi_os_unmap_iomem(elog_addr, elog_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700314 release_mem_region(elog_base, elog_size);
315 release_mem_region(l1_dirbase, l1_size);
316 kfree(elog_buf);
317}
318
319module_init(extlog_init);
320module_exit(extlog_exit);
321
322MODULE_AUTHOR("Chen, Gong <gong.chen@intel.com>");
323MODULE_DESCRIPTION("Extended MCA Error Log Driver");
324MODULE_LICENSE("GPL");