eaf5a43ff0821baede5806ac7cf49fe39137f80a
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / x86 / kernel / cpu / mcheck / mce-severity.c
1 /*
2  * MCE grading rules.
3  * Copyright 2008, 2009 Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; version 2
8  * of the License.
9  *
10  * Author: Andi Kleen
11  */
12 #include <linux/kernel.h>
13 #include <linux/seq_file.h>
14 #include <linux/init.h>
15 #include <linux/debugfs.h>
16 #include <asm/mce.h>
17
18 #include "mce-internal.h"
19
20 /*
21  * Grade an mce by severity. In general the most severe ones are processed
22  * first. Since there are quite a lot of combinations test the bits in a
23  * table-driven way. The rules are simply processed in order, first
24  * match wins.
25  *
26  * Note this is only used for machine check exceptions, the corrected
27  * errors use much simpler rules. The exceptions still check for the corrected
28  * errors, but only to leave them alone for the CMCI handler (except for
29  * panic situations)
30  */
31
32 enum context { IN_KERNEL = 1, IN_USER = 2 };
33 enum ser { SER_REQUIRED = 1, NO_SER = 2 };
34
35 static struct severity {
36         u64 mask;
37         u64 result;
38         unsigned char sev;
39         unsigned char mcgmask;
40         unsigned char mcgres;
41         unsigned char ser;
42         unsigned char context;
43         unsigned char covered;
44         char *msg;
45 } severities[] = {
46 #define KERNEL .context = IN_KERNEL
47 #define USER .context = IN_USER
48 #define SER .ser = SER_REQUIRED
49 #define NOSER .ser = NO_SER
50 #define SEV(s) .sev = MCE_ ## s ## _SEVERITY
51 #define BITCLR(x, s, m, r...) { .mask = x, .result = 0, SEV(s), .msg = m, ## r }
52 #define BITSET(x, s, m, r...) { .mask = x, .result = x, SEV(s), .msg = m, ## r }
53 #define MCGMASK(x, res, s, m, r...) \
54         { .mcgmask = x, .mcgres = res, SEV(s), .msg = m, ## r }
55 #define MASK(x, y, s, m, r...) \
56         { .mask = x, .result = y, SEV(s), .msg = m, ## r }
57 #define MCI_UC_S (MCI_STATUS_UC|MCI_STATUS_S)
58 #define MCI_UC_SAR (MCI_STATUS_UC|MCI_STATUS_S|MCI_STATUS_AR)
59 #define MCACOD 0xffff
60
61         BITCLR(
62                 MCI_STATUS_VAL,
63                 NO, "Invalid"
64                 ),
65         BITCLR(
66                 MCI_STATUS_EN,
67                 NO, "Not enabled"
68                 ),
69         BITSET(
70                 MCI_STATUS_PCC,
71                 PANIC, "Processor context corrupt"
72                 ),
73         /* When MCIP is not set something is very confused */
74         MCGMASK(
75                 MCG_STATUS_MCIP, 0,
76                 PANIC, "MCIP not set in MCA handler"
77                 ),
78         /* Neither return not error IP -- no chance to recover -> PANIC */
79         MCGMASK(
80                 MCG_STATUS_RIPV|MCG_STATUS_EIPV, 0,
81                 PANIC, "Neither restart nor error IP"
82                 ),
83         MCGMASK(
84                 MCG_STATUS_RIPV, 0,
85                 PANIC, "In kernel and no restart IP",
86                 KERNEL
87                 ),
88         BITCLR(
89                 MCI_STATUS_UC,
90                 KEEP, "Corrected error",
91                 NOSER
92                 ),
93
94         /* ignore OVER for UCNA */
95         MASK(
96                 MCI_UC_SAR, MCI_STATUS_UC,
97                 KEEP, "Uncorrected no action required",
98                 SER
99                 ),
100         MASK(
101                 MCI_STATUS_OVER|MCI_UC_SAR, MCI_STATUS_UC|MCI_STATUS_AR,
102                 PANIC, "Illegal combination (UCNA with AR=1)",
103                 SER
104                 ),
105         MASK(
106                 MCI_STATUS_S, 0,
107                 KEEP, "Non signalled machine check",
108                 SER
109                 ),
110
111         /* AR add known MCACODs here */
112         MASK(
113                 MCI_STATUS_OVER|MCI_UC_SAR, MCI_STATUS_OVER|MCI_UC_SAR,
114                 PANIC, "Action required with lost events",
115                 SER
116                 ),
117         MASK(
118                 MCI_STATUS_OVER|MCI_UC_SAR, MCI_UC_SAR,
119                 PANIC, "Action required; unknown MCACOD",
120                 SER
121                 ),
122
123         /* known AO MCACODs: */
124         MASK(
125                 MCI_UC_SAR|MCI_STATUS_OVER|0xfff0, MCI_UC_S|0xc0,
126                 AO, "Action optional: memory scrubbing error",
127                 SER
128                 ),
129         MASK(
130                 MCI_UC_SAR|MCI_STATUS_OVER|MCACOD, MCI_UC_S|0x17a,
131                 AO, "Action optional: last level cache writeback error",
132                 SER
133                 ),
134
135         MASK(
136                 MCI_STATUS_OVER|MCI_UC_SAR, MCI_UC_S,
137                 SOME, "Action optional unknown MCACOD",
138                 SER
139                 ),
140         MASK(
141                 MCI_STATUS_OVER|MCI_UC_SAR, MCI_UC_S|MCI_STATUS_OVER,
142                 SOME, "Action optional with lost events",
143                 SER
144                 ),
145         BITSET(
146                 MCI_STATUS_UC|MCI_STATUS_OVER,
147                 PANIC, "Overflowed uncorrected"
148                 ),
149         BITSET(
150                 MCI_STATUS_UC,
151                 UC, "Uncorrected"
152                 ),
153         BITSET(
154                 0,
155                 SOME, "No match"
156                 )       /* always matches. keep at end */
157 };
158
159 /*
160  * If the EIPV bit is set, it means the saved IP is the
161  * instruction which caused the MCE.
162  */
163 static int error_context(struct mce *m)
164 {
165         if (m->mcgstatus & MCG_STATUS_EIPV)
166                 return (m->ip && (m->cs & 3) == 3) ? IN_USER : IN_KERNEL;
167         /* Unknown, assume kernel */
168         return IN_KERNEL;
169 }
170
171 int mce_severity(struct mce *a, int tolerant, char **msg)
172 {
173         enum context ctx = error_context(a);
174         struct severity *s;
175
176         for (s = severities;; s++) {
177                 if ((a->status & s->mask) != s->result)
178                         continue;
179                 if ((a->mcgstatus & s->mcgmask) != s->mcgres)
180                         continue;
181                 if (s->ser == SER_REQUIRED && !mce_ser)
182                         continue;
183                 if (s->ser == NO_SER && mce_ser)
184                         continue;
185                 if (s->context && ctx != s->context)
186                         continue;
187                 if (msg)
188                         *msg = s->msg;
189                 s->covered = 1;
190                 if (s->sev >= MCE_UC_SEVERITY && ctx == IN_KERNEL) {
191                         if (panic_on_oops || tolerant < 1)
192                                 return MCE_PANIC_SEVERITY;
193                 }
194                 return s->sev;
195         }
196 }
197
198 #ifdef CONFIG_DEBUG_FS
199 static void *s_start(struct seq_file *f, loff_t *pos)
200 {
201         if (*pos >= ARRAY_SIZE(severities))
202                 return NULL;
203         return &severities[*pos];
204 }
205
206 static void *s_next(struct seq_file *f, void *data, loff_t *pos)
207 {
208         if (++(*pos) >= ARRAY_SIZE(severities))
209                 return NULL;
210         return &severities[*pos];
211 }
212
213 static void s_stop(struct seq_file *f, void *data)
214 {
215 }
216
217 static int s_show(struct seq_file *f, void *data)
218 {
219         struct severity *ser = data;
220         seq_printf(f, "%d\t%s\n", ser->covered, ser->msg);
221         return 0;
222 }
223
224 static const struct seq_operations severities_seq_ops = {
225         .start  = s_start,
226         .next   = s_next,
227         .stop   = s_stop,
228         .show   = s_show,
229 };
230
231 static int severities_coverage_open(struct inode *inode, struct file *file)
232 {
233         return seq_open(file, &severities_seq_ops);
234 }
235
236 static ssize_t severities_coverage_write(struct file *file,
237                                          const char __user *ubuf,
238                                          size_t count, loff_t *ppos)
239 {
240         int i;
241         for (i = 0; i < ARRAY_SIZE(severities); i++)
242                 severities[i].covered = 0;
243         return count;
244 }
245
246 static const struct file_operations severities_coverage_fops = {
247         .open           = severities_coverage_open,
248         .release        = seq_release,
249         .read           = seq_read,
250         .write          = severities_coverage_write,
251         .llseek         = seq_lseek,
252 };
253
254 static int __init severities_debugfs_init(void)
255 {
256         struct dentry *dmce = NULL, *fseverities_coverage = NULL;
257
258         dmce = mce_get_debugfs_dir();
259         if (dmce == NULL)
260                 goto err_out;
261         fseverities_coverage = debugfs_create_file("severities-coverage",
262                                                    0444, dmce, NULL,
263                                                    &severities_coverage_fops);
264         if (fseverities_coverage == NULL)
265                 goto err_out;
266
267         return 0;
268
269 err_out:
270         return -ENOMEM;
271 }
272 late_initcall(severities_debugfs_init);
273 #endif