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