blob: 340d09518f8e986a56bffd4dc61291fbcb6e0b87 [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"
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
Chen, Gong42139eb2013-12-06 01:17:10 -050044static int old_edac_report_status;
45
Jiang Liu7ede9f82013-12-19 20:47:46 +080046static u8 extlog_dsm_uuid[] __initdata = "663E35AF-CC10-41A4-88EA-5470AF055295";
Chen, Gong4b3db702013-10-21 14:29:25 -070047
48/* L1 table related physical address */
49static u64 elog_base;
50static size_t elog_size;
51static u64 l1_dirbase;
52static size_t l1_size;
53
54/* L1 table related virtual address */
55static void __iomem *extlog_l1_addr;
56static void __iomem *elog_addr;
57
58static void *elog_buf;
59
60static u64 *l1_entry_base;
61static 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
Lv Zheng0a00fd52014-06-03 16:32:53 +080072static struct acpi_hest_generic_status *extlog_elog_entry_check(int cpu, int bank)
Chen, Gong4b3db702013-10-21 14:29:25 -070073{
74 int idx;
75 u64 data;
Lv Zheng0a00fd52014-06-03 16:32:53 +080076 struct acpi_hest_generic_status *estatus;
Chen, Gong4b3db702013-10-21 14:29:25 -070077
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;
Lv Zheng0a00fd52014-06-03 16:32:53 +080085 estatus = (struct acpi_hest_generic_status *)ELOG_ENTRY_ADDR(data);
Chen, Gong4b3db702013-10-21 14:29:25 -070086
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
94static void __print_extlog_rcd(const char *pfx,
Lv Zheng0a00fd52014-06-03 16:32:53 +080095 struct acpi_hest_generic_status *estatus, int cpu)
Chen, Gong4b3db702013-10-21 14:29:25 -070096{
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
113static int print_extlog_rcd(const char *pfx,
Lv Zheng0a00fd52014-06-03 16:32:53 +0800114 struct acpi_hest_generic_status *estatus, int cpu)
Chen, Gong4b3db702013-10-21 14:29:25 -0700115{
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
134static 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;
Lv Zheng0a00fd52014-06-03 16:32:53 +0800140 struct acpi_hest_generic_status *estatus;
Chen, Gong4b3db702013-10-21 14:29:25 -0700141 int rc;
142
143 estatus = extlog_elog_entry_check(cpu, bank);
144 if (estatus == NULL)
145 return NOTIFY_DONE;
146
147 memcpy(elog_buf, (void *)estatus, ELOG_ENTRY_LEN);
148 /* clear record status to enable BIOS to update it again */
149 estatus->block_status = 0;
150
Lv Zheng0a00fd52014-06-03 16:32:53 +0800151 rc = print_extlog_rcd(NULL, (struct acpi_hest_generic_status *)elog_buf, cpu);
Chen, Gong4b3db702013-10-21 14:29:25 -0700152
Chen, Gong42139eb2013-12-06 01:17:10 -0500153 return NOTIFY_STOP;
Chen, Gong4b3db702013-10-21 14:29:25 -0700154}
155
Jiang Liu7ede9f82013-12-19 20:47:46 +0800156static bool __init extlog_get_l1addr(void)
Chen, Gong4b3db702013-10-21 14:29:25 -0700157{
Chen, Gong4b3db702013-10-21 14:29:25 -0700158 u8 uuid[16];
Jiang Liu7ede9f82013-12-19 20:47:46 +0800159 acpi_handle handle;
160 union acpi_object *obj;
Chen, Gong4b3db702013-10-21 14:29:25 -0700161
162 acpi_str_to_uuid(extlog_dsm_uuid, uuid);
Chen, Gong4b3db702013-10-21 14:29:25 -0700163
164 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
165 return false;
Jiang Liu7ede9f82013-12-19 20:47:46 +0800166 if (!acpi_check_dsm(handle, uuid, EXTLOG_DSM_REV, 1 << EXTLOG_FN_ADDR))
Chen, Gong4b3db702013-10-21 14:29:25 -0700167 return false;
Jiang Liu7ede9f82013-12-19 20:47:46 +0800168 obj = acpi_evaluate_dsm_typed(handle, uuid, EXTLOG_DSM_REV,
169 EXTLOG_FN_ADDR, NULL, ACPI_TYPE_INTEGER);
170 if (!obj) {
Chen, Gong4b3db702013-10-21 14:29:25 -0700171 return false;
Jiang Liu7ede9f82013-12-19 20:47:46 +0800172 } else {
173 l1_dirbase = obj->integer.value;
174 ACPI_FREE(obj);
175 }
Chen, Gong4b3db702013-10-21 14:29:25 -0700176
Chen, Gong4b3db702013-10-21 14:29:25 -0700177 /* Spec says L1 directory must be 4K aligned, bail out if it isn't */
178 if (l1_dirbase & ((1 << 12) - 1)) {
179 pr_warn(FW_BUG "L1 Directory is invalid at physical %llx\n",
180 l1_dirbase);
181 return false;
182 }
183
184 return true;
185}
186static struct notifier_block extlog_mce_dec = {
187 .notifier_call = extlog_print,
188};
189
190static int __init extlog_init(void)
191{
192 struct extlog_l1_head *l1_head;
193 void __iomem *extlog_l1_hdr;
194 size_t l1_hdr_size;
195 struct resource *r;
196 u64 cap;
197 int rc;
198
Chen, Gong42139eb2013-12-06 01:17:10 -0500199 if (get_edac_report_status() == EDAC_REPORTING_FORCE) {
200 pr_warn("Not loading eMCA, error reporting force-enabled through EDAC.\n");
201 return -EPERM;
202 }
Chen, Gong4b3db702013-10-21 14:29:25 -0700203
Chen, Gong42139eb2013-12-06 01:17:10 -0500204 rc = -ENODEV;
Chen, Gong4b3db702013-10-21 14:29:25 -0700205 rdmsrl(MSR_IA32_MCG_CAP, cap);
206 if (!(cap & MCG_ELOG_P))
207 return rc;
208
209 if (!extlog_get_l1addr())
210 return rc;
211
212 rc = -EINVAL;
213 /* get L1 header to fetch necessary information */
214 l1_hdr_size = sizeof(struct extlog_l1_head);
215 r = request_mem_region(l1_dirbase, l1_hdr_size, "L1 DIR HDR");
216 if (!r) {
217 pr_warn(FW_BUG EMCA_BUG,
218 (unsigned long long)l1_dirbase,
219 (unsigned long long)l1_dirbase + l1_hdr_size);
220 goto err;
221 }
222
Lv Zhenga2383172014-05-20 15:39:41 +0800223 extlog_l1_hdr = acpi_os_map_iomem(l1_dirbase, l1_hdr_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700224 l1_head = (struct extlog_l1_head *)extlog_l1_hdr;
225 l1_size = l1_head->total_len;
226 l1_percpu_entry = l1_head->entries;
227 elog_base = l1_head->elog_base;
228 elog_size = l1_head->elog_len;
Lv Zhenga2383172014-05-20 15:39:41 +0800229 acpi_os_unmap_iomem(extlog_l1_hdr, l1_hdr_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700230 release_mem_region(l1_dirbase, l1_hdr_size);
231
232 /* remap L1 header again based on completed information */
233 r = request_mem_region(l1_dirbase, l1_size, "L1 Table");
234 if (!r) {
235 pr_warn(FW_BUG EMCA_BUG,
236 (unsigned long long)l1_dirbase,
237 (unsigned long long)l1_dirbase + l1_size);
238 goto err;
239 }
Lv Zhenga2383172014-05-20 15:39:41 +0800240 extlog_l1_addr = acpi_os_map_iomem(l1_dirbase, l1_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700241 l1_entry_base = (u64 *)((u8 *)extlog_l1_addr + l1_hdr_size);
242
243 /* remap elog table */
244 r = request_mem_region(elog_base, elog_size, "Elog Table");
245 if (!r) {
246 pr_warn(FW_BUG EMCA_BUG,
247 (unsigned long long)elog_base,
248 (unsigned long long)elog_base + elog_size);
249 goto err_release_l1_dir;
250 }
Lv Zhenga2383172014-05-20 15:39:41 +0800251 elog_addr = acpi_os_map_iomem(elog_base, elog_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700252
253 rc = -ENOMEM;
254 /* allocate buffer to save elog record */
255 elog_buf = kmalloc(ELOG_ENTRY_LEN, GFP_KERNEL);
256 if (elog_buf == NULL)
257 goto err_release_elog;
258
Chen, Gong42139eb2013-12-06 01:17:10 -0500259 /*
260 * eMCA event report method has higher priority than EDAC method,
261 * unless EDAC event report method is mandatory.
262 */
263 old_edac_report_status = get_edac_report_status();
264 set_edac_report_status(EDAC_REPORTING_DISABLED);
Chen, Gong4b3db702013-10-21 14:29:25 -0700265 mce_register_decode_chain(&extlog_mce_dec);
266 /* enable OS to be involved to take over management from BIOS */
267 ((struct extlog_l1_head *)extlog_l1_addr)->flags |= FLAG_OS_OPTIN;
268
269 return 0;
270
271err_release_elog:
272 if (elog_addr)
Lv Zhenga2383172014-05-20 15:39:41 +0800273 acpi_os_unmap_iomem(elog_addr, elog_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700274 release_mem_region(elog_base, elog_size);
275err_release_l1_dir:
276 if (extlog_l1_addr)
Lv Zhenga2383172014-05-20 15:39:41 +0800277 acpi_os_unmap_iomem(extlog_l1_addr, l1_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700278 release_mem_region(l1_dirbase, l1_size);
279err:
280 pr_warn(FW_BUG "Extended error log disabled because of problems parsing f/w tables\n");
281 return rc;
282}
283
284static void __exit extlog_exit(void)
285{
Chen, Gong42139eb2013-12-06 01:17:10 -0500286 set_edac_report_status(old_edac_report_status);
Chen, Gong4b3db702013-10-21 14:29:25 -0700287 mce_unregister_decode_chain(&extlog_mce_dec);
288 ((struct extlog_l1_head *)extlog_l1_addr)->flags &= ~FLAG_OS_OPTIN;
289 if (extlog_l1_addr)
Lv Zhenga2383172014-05-20 15:39:41 +0800290 acpi_os_unmap_iomem(extlog_l1_addr, l1_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700291 if (elog_addr)
Lv Zhenga2383172014-05-20 15:39:41 +0800292 acpi_os_unmap_iomem(elog_addr, elog_size);
Chen, Gong4b3db702013-10-21 14:29:25 -0700293 release_mem_region(elog_base, elog_size);
294 release_mem_region(l1_dirbase, l1_size);
295 kfree(elog_buf);
296}
297
298module_init(extlog_init);
299module_exit(extlog_exit);
300
301MODULE_AUTHOR("Chen, Gong <gong.chen@intel.com>");
302MODULE_DESCRIPTION("Extended MCA Error Log Driver");
303MODULE_LICENSE("GPL");