xref: /linux/arch/powerpc/kernel/mce.c (revision b5ff4211a8294be2ddbaf963fa3666fa042292a8)
1 /*
2  * Machine check exception handling.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Copyright 2013 IBM Corporation
19  * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
20  */
21 
22 #undef DEBUG
23 #define pr_fmt(fmt) "mce: " fmt
24 
25 #include <linux/types.h>
26 #include <linux/ptrace.h>
27 #include <linux/percpu.h>
28 #include <linux/export.h>
29 #include <asm/mce.h>
30 
31 static DEFINE_PER_CPU(int, mce_nest_count);
32 static DEFINE_PER_CPU(struct machine_check_event[MAX_MC_EVT], mce_event);
33 
34 /* Queue for delayed MCE events. */
35 static DEFINE_PER_CPU(int, mce_queue_count);
36 static DEFINE_PER_CPU(struct machine_check_event[MAX_MC_EVT], mce_event_queue);
37 
38 static void mce_set_error_info(struct machine_check_event *mce,
39 			       struct mce_error_info *mce_err)
40 {
41 	mce->error_type = mce_err->error_type;
42 	switch (mce_err->error_type) {
43 	case MCE_ERROR_TYPE_UE:
44 		mce->u.ue_error.ue_error_type = mce_err->u.ue_error_type;
45 		break;
46 	case MCE_ERROR_TYPE_SLB:
47 		mce->u.slb_error.slb_error_type = mce_err->u.slb_error_type;
48 		break;
49 	case MCE_ERROR_TYPE_ERAT:
50 		mce->u.erat_error.erat_error_type = mce_err->u.erat_error_type;
51 		break;
52 	case MCE_ERROR_TYPE_TLB:
53 		mce->u.tlb_error.tlb_error_type = mce_err->u.tlb_error_type;
54 		break;
55 	case MCE_ERROR_TYPE_UNKNOWN:
56 	default:
57 		break;
58 	}
59 }
60 
61 /*
62  * Decode and save high level MCE information into per cpu buffer which
63  * is an array of machine_check_event structure.
64  */
65 void save_mce_event(struct pt_regs *regs, long handled,
66 		    struct mce_error_info *mce_err,
67 		    uint64_t addr)
68 {
69 	uint64_t srr1;
70 	int index = __get_cpu_var(mce_nest_count)++;
71 	struct machine_check_event *mce = &__get_cpu_var(mce_event[index]);
72 
73 	/*
74 	 * Return if we don't have enough space to log mce event.
75 	 * mce_nest_count may go beyond MAX_MC_EVT but that's ok,
76 	 * the check below will stop buffer overrun.
77 	 */
78 	if (index >= MAX_MC_EVT)
79 		return;
80 
81 	/* Populate generic machine check info */
82 	mce->version = MCE_V1;
83 	mce->srr0 = regs->nip;
84 	mce->srr1 = regs->msr;
85 	mce->gpr3 = regs->gpr[3];
86 	mce->in_use = 1;
87 
88 	mce->initiator = MCE_INITIATOR_CPU;
89 	if (handled)
90 		mce->disposition = MCE_DISPOSITION_RECOVERED;
91 	else
92 		mce->disposition = MCE_DISPOSITION_NOT_RECOVERED;
93 	mce->severity = MCE_SEV_ERROR_SYNC;
94 
95 	srr1 = regs->msr;
96 
97 	/*
98 	 * Populate the mce error_type and type-specific error_type.
99 	 */
100 	mce_set_error_info(mce, mce_err);
101 
102 	if (!addr)
103 		return;
104 
105 	if (mce->error_type == MCE_ERROR_TYPE_TLB) {
106 		mce->u.tlb_error.effective_address_provided = true;
107 		mce->u.tlb_error.effective_address = addr;
108 	} else if (mce->error_type == MCE_ERROR_TYPE_SLB) {
109 		mce->u.slb_error.effective_address_provided = true;
110 		mce->u.slb_error.effective_address = addr;
111 	} else if (mce->error_type == MCE_ERROR_TYPE_ERAT) {
112 		mce->u.erat_error.effective_address_provided = true;
113 		mce->u.erat_error.effective_address = addr;
114 	} else if (mce->error_type == MCE_ERROR_TYPE_UE) {
115 		mce->u.ue_error.effective_address_provided = true;
116 		mce->u.ue_error.effective_address = addr;
117 	}
118 	return;
119 }
120 
121 /*
122  * get_mce_event:
123  *	mce	Pointer to machine_check_event structure to be filled.
124  *	release Flag to indicate whether to free the event slot or not.
125  *		0 <= do not release the mce event. Caller will invoke
126  *		     release_mce_event() once event has been consumed.
127  *		1 <= release the slot.
128  *
129  *	return	1 = success
130  *		0 = failure
131  *
132  * get_mce_event() will be called by platform specific machine check
133  * handle routine and in KVM.
134  * When we call get_mce_event(), we are still in interrupt context and
135  * preemption will not be scheduled until ret_from_expect() routine
136  * is called.
137  */
138 int get_mce_event(struct machine_check_event *mce, bool release)
139 {
140 	int index = __get_cpu_var(mce_nest_count) - 1;
141 	struct machine_check_event *mc_evt;
142 	int ret = 0;
143 
144 	/* Sanity check */
145 	if (index < 0)
146 		return ret;
147 
148 	/* Check if we have MCE info to process. */
149 	if (index < MAX_MC_EVT) {
150 		mc_evt = &__get_cpu_var(mce_event[index]);
151 		/* Copy the event structure and release the original */
152 		if (mce)
153 			*mce = *mc_evt;
154 		if (release)
155 			mc_evt->in_use = 0;
156 		ret = 1;
157 	}
158 	/* Decrement the count to free the slot. */
159 	if (release)
160 		__get_cpu_var(mce_nest_count)--;
161 
162 	return ret;
163 }
164 
165 void release_mce_event(void)
166 {
167 	get_mce_event(NULL, true);
168 }
169 
170 /*
171  * Queue up the MCE event which then can be handled later.
172  */
173 void machine_check_queue_event(void)
174 {
175 	int index;
176 	struct machine_check_event evt;
177 
178 	if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
179 		return;
180 
181 	index = __get_cpu_var(mce_queue_count)++;
182 	/* If queue is full, just return for now. */
183 	if (index >= MAX_MC_EVT) {
184 		__get_cpu_var(mce_queue_count)--;
185 		return;
186 	}
187 	__get_cpu_var(mce_event_queue[index]) = evt;
188 }
189 
190 /*
191  * process pending MCE event from the mce event queue. This function will be
192  * called during syscall exit.
193  */
194 void machine_check_process_queued_event(void)
195 {
196 	int index;
197 
198 	preempt_disable();
199 	/*
200 	 * For now just print it to console.
201 	 * TODO: log this error event to FSP or nvram.
202 	 */
203 	while (__get_cpu_var(mce_queue_count) > 0) {
204 		index = __get_cpu_var(mce_queue_count) - 1;
205 		machine_check_print_event_info(
206 				&__get_cpu_var(mce_event_queue[index]));
207 		__get_cpu_var(mce_queue_count)--;
208 	}
209 	preempt_enable();
210 }
211 
212 void machine_check_print_event_info(struct machine_check_event *evt)
213 {
214 	const char *level, *sevstr, *subtype;
215 	static const char *mc_ue_types[] = {
216 		"Indeterminate",
217 		"Instruction fetch",
218 		"Page table walk ifetch",
219 		"Load/Store",
220 		"Page table walk Load/Store",
221 	};
222 	static const char *mc_slb_types[] = {
223 		"Indeterminate",
224 		"Parity",
225 		"Multihit",
226 	};
227 	static const char *mc_erat_types[] = {
228 		"Indeterminate",
229 		"Parity",
230 		"Multihit",
231 	};
232 	static const char *mc_tlb_types[] = {
233 		"Indeterminate",
234 		"Parity",
235 		"Multihit",
236 	};
237 
238 	/* Print things out */
239 	if (evt->version != MCE_V1) {
240 		pr_err("Machine Check Exception, Unknown event version %d !\n",
241 		       evt->version);
242 		return;
243 	}
244 	switch (evt->severity) {
245 	case MCE_SEV_NO_ERROR:
246 		level = KERN_INFO;
247 		sevstr = "Harmless";
248 		break;
249 	case MCE_SEV_WARNING:
250 		level = KERN_WARNING;
251 		sevstr = "";
252 		break;
253 	case MCE_SEV_ERROR_SYNC:
254 		level = KERN_ERR;
255 		sevstr = "Severe";
256 		break;
257 	case MCE_SEV_FATAL:
258 	default:
259 		level = KERN_ERR;
260 		sevstr = "Fatal";
261 		break;
262 	}
263 
264 	printk("%s%s Machine check interrupt [%s]\n", level, sevstr,
265 	       evt->disposition == MCE_DISPOSITION_RECOVERED ?
266 	       "Recovered" : "[Not recovered");
267 	printk("%s  Initiator: %s\n", level,
268 	       evt->initiator == MCE_INITIATOR_CPU ? "CPU" : "Unknown");
269 	switch (evt->error_type) {
270 	case MCE_ERROR_TYPE_UE:
271 		subtype = evt->u.ue_error.ue_error_type <
272 			ARRAY_SIZE(mc_ue_types) ?
273 			mc_ue_types[evt->u.ue_error.ue_error_type]
274 			: "Unknown";
275 		printk("%s  Error type: UE [%s]\n", level, subtype);
276 		if (evt->u.ue_error.effective_address_provided)
277 			printk("%s    Effective address: %016llx\n",
278 			       level, evt->u.ue_error.effective_address);
279 		if (evt->u.ue_error.physical_address_provided)
280 			printk("%s      Physial address: %016llx\n",
281 			       level, evt->u.ue_error.physical_address);
282 		break;
283 	case MCE_ERROR_TYPE_SLB:
284 		subtype = evt->u.slb_error.slb_error_type <
285 			ARRAY_SIZE(mc_slb_types) ?
286 			mc_slb_types[evt->u.slb_error.slb_error_type]
287 			: "Unknown";
288 		printk("%s  Error type: SLB [%s]\n", level, subtype);
289 		if (evt->u.slb_error.effective_address_provided)
290 			printk("%s    Effective address: %016llx\n",
291 			       level, evt->u.slb_error.effective_address);
292 		break;
293 	case MCE_ERROR_TYPE_ERAT:
294 		subtype = evt->u.erat_error.erat_error_type <
295 			ARRAY_SIZE(mc_erat_types) ?
296 			mc_erat_types[evt->u.erat_error.erat_error_type]
297 			: "Unknown";
298 		printk("%s  Error type: ERAT [%s]\n", level, subtype);
299 		if (evt->u.erat_error.effective_address_provided)
300 			printk("%s    Effective address: %016llx\n",
301 			       level, evt->u.erat_error.effective_address);
302 		break;
303 	case MCE_ERROR_TYPE_TLB:
304 		subtype = evt->u.tlb_error.tlb_error_type <
305 			ARRAY_SIZE(mc_tlb_types) ?
306 			mc_tlb_types[evt->u.tlb_error.tlb_error_type]
307 			: "Unknown";
308 		printk("%s  Error type: TLB [%s]\n", level, subtype);
309 		if (evt->u.tlb_error.effective_address_provided)
310 			printk("%s    Effective address: %016llx\n",
311 			       level, evt->u.tlb_error.effective_address);
312 		break;
313 	default:
314 	case MCE_ERROR_TYPE_UNKNOWN:
315 		printk("%s  Error type: Unknown\n", level);
316 		break;
317 	}
318 }
319