xref: /openbmc/linux/arch/powerpc/platforms/pseries/ras.c (revision 90128997815009686ec9d72a4b1490455d838e66)
1d9953105SMichael Ellerman /*
2d9953105SMichael Ellerman  * Copyright (C) 2001 Dave Engebretsen IBM Corporation
3d9953105SMichael Ellerman  *
4d9953105SMichael Ellerman  * This program is free software; you can redistribute it and/or modify
5d9953105SMichael Ellerman  * it under the terms of the GNU General Public License as published by
6d9953105SMichael Ellerman  * the Free Software Foundation; either version 2 of the License, or
7d9953105SMichael Ellerman  * (at your option) any later version.
8d9953105SMichael Ellerman  *
9d9953105SMichael Ellerman  * This program is distributed in the hope that it will be useful,
10d9953105SMichael Ellerman  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11d9953105SMichael Ellerman  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12d9953105SMichael Ellerman  * GNU General Public License for more details.
13d9953105SMichael Ellerman  *
14d9953105SMichael Ellerman  * You should have received a copy of the GNU General Public License
15d9953105SMichael Ellerman  * along with this program; if not, write to the Free Software
16d9953105SMichael Ellerman  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17d9953105SMichael Ellerman  */
18d9953105SMichael Ellerman 
19d9953105SMichael Ellerman #include <linux/sched.h>
20d9953105SMichael Ellerman #include <linux/interrupt.h>
21d9953105SMichael Ellerman #include <linux/irq.h>
22*90128997SAnton Blanchard #include <linux/of.h>
2355fc0c56SAnton Blanchard #include <linux/fs.h>
2455fc0c56SAnton Blanchard #include <linux/reboot.h>
25d9953105SMichael Ellerman 
26d9953105SMichael Ellerman #include <asm/machdep.h>
27d9953105SMichael Ellerman #include <asm/rtas.h>
288c4f1f29SMichael Ellerman #include <asm/firmware.h>
29d9953105SMichael Ellerman 
30577830b0SMichael Ellerman #include "pseries.h"
31c902be71SArnd Bergmann 
32d9953105SMichael Ellerman static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
33d9953105SMichael Ellerman static DEFINE_SPINLOCK(ras_log_buf_lock);
34d9953105SMichael Ellerman 
35d368514cSAnton Blanchard static char global_mce_data_buf[RTAS_ERROR_LOG_MAX];
36d368514cSAnton Blanchard static DEFINE_PER_CPU(__u64, mce_data_buf);
37d9953105SMichael Ellerman 
38d9953105SMichael Ellerman static int ras_check_exception_token;
39d9953105SMichael Ellerman 
40d9953105SMichael Ellerman #define EPOW_SENSOR_TOKEN	9
41d9953105SMichael Ellerman #define EPOW_SENSOR_INDEX	0
42d9953105SMichael Ellerman 
437d12e780SDavid Howells static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
447d12e780SDavid Howells static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
45d9953105SMichael Ellerman 
460ebfff14SBenjamin Herrenschmidt 
47d9953105SMichael Ellerman /*
48d9953105SMichael Ellerman  * Initialize handlers for the set of interrupts caused by hardware errors
49d9953105SMichael Ellerman  * and power system events.
50d9953105SMichael Ellerman  */
51d9953105SMichael Ellerman static int __init init_ras_IRQ(void)
52d9953105SMichael Ellerman {
53d9953105SMichael Ellerman 	struct device_node *np;
54d9953105SMichael Ellerman 
55d9953105SMichael Ellerman 	ras_check_exception_token = rtas_token("check-exception");
56d9953105SMichael Ellerman 
57d9953105SMichael Ellerman 	/* Internal Errors */
58d9953105SMichael Ellerman 	np = of_find_node_by_path("/event-sources/internal-errors");
59d9953105SMichael Ellerman 	if (np != NULL) {
6032c96f77SMark Nelson 		request_event_sources_irqs(np, ras_error_interrupt,
6132c96f77SMark Nelson 					   "RAS_ERROR");
62d9953105SMichael Ellerman 		of_node_put(np);
63d9953105SMichael Ellerman 	}
64d9953105SMichael Ellerman 
65d9953105SMichael Ellerman 	/* EPOW Events */
66d9953105SMichael Ellerman 	np = of_find_node_by_path("/event-sources/epow-events");
67d9953105SMichael Ellerman 	if (np != NULL) {
6832c96f77SMark Nelson 		request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
69d9953105SMichael Ellerman 		of_node_put(np);
70d9953105SMichael Ellerman 	}
71d9953105SMichael Ellerman 
7269ed3324SAnton Blanchard 	return 0;
73d9953105SMichael Ellerman }
7455fc0c56SAnton Blanchard subsys_initcall(init_ras_IRQ);
75d9953105SMichael Ellerman 
7655fc0c56SAnton Blanchard #define EPOW_SHUTDOWN_NORMAL				1
7755fc0c56SAnton Blanchard #define EPOW_SHUTDOWN_ON_UPS				2
7855fc0c56SAnton Blanchard #define EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS	3
7955fc0c56SAnton Blanchard #define EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH	4
8055fc0c56SAnton Blanchard 
8155fc0c56SAnton Blanchard static void handle_system_shutdown(char event_modifier)
8255fc0c56SAnton Blanchard {
8355fc0c56SAnton Blanchard 	switch (event_modifier) {
8455fc0c56SAnton Blanchard 	case EPOW_SHUTDOWN_NORMAL:
8555fc0c56SAnton Blanchard 		pr_emerg("Firmware initiated power off");
8655fc0c56SAnton Blanchard 		orderly_poweroff(1);
8755fc0c56SAnton Blanchard 		break;
8855fc0c56SAnton Blanchard 
8955fc0c56SAnton Blanchard 	case EPOW_SHUTDOWN_ON_UPS:
9055fc0c56SAnton Blanchard 		pr_emerg("Loss of power reported by firmware, system is "
9155fc0c56SAnton Blanchard 			"running on UPS/battery");
9255fc0c56SAnton Blanchard 		break;
9355fc0c56SAnton Blanchard 
9455fc0c56SAnton Blanchard 	case EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS:
9555fc0c56SAnton Blanchard 		pr_emerg("Loss of system critical functions reported by "
9655fc0c56SAnton Blanchard 			"firmware");
9755fc0c56SAnton Blanchard 		pr_emerg("Check RTAS error log for details");
9855fc0c56SAnton Blanchard 		orderly_poweroff(1);
9955fc0c56SAnton Blanchard 		break;
10055fc0c56SAnton Blanchard 
10155fc0c56SAnton Blanchard 	case EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH:
10255fc0c56SAnton Blanchard 		pr_emerg("Ambient temperature too high reported by firmware");
10355fc0c56SAnton Blanchard 		pr_emerg("Check RTAS error log for details");
10455fc0c56SAnton Blanchard 		orderly_poweroff(1);
10555fc0c56SAnton Blanchard 		break;
10655fc0c56SAnton Blanchard 
10755fc0c56SAnton Blanchard 	default:
10855fc0c56SAnton Blanchard 		pr_err("Unknown power/cooling shutdown event (modifier %d)",
10955fc0c56SAnton Blanchard 			event_modifier);
11055fc0c56SAnton Blanchard 	}
11155fc0c56SAnton Blanchard }
11255fc0c56SAnton Blanchard 
11355fc0c56SAnton Blanchard struct epow_errorlog {
11455fc0c56SAnton Blanchard 	unsigned char sensor_value;
11555fc0c56SAnton Blanchard 	unsigned char event_modifier;
11655fc0c56SAnton Blanchard 	unsigned char extended_modifier;
11755fc0c56SAnton Blanchard 	unsigned char reserved;
11855fc0c56SAnton Blanchard 	unsigned char platform_reason;
11955fc0c56SAnton Blanchard };
12055fc0c56SAnton Blanchard 
12155fc0c56SAnton Blanchard #define EPOW_RESET			0
12255fc0c56SAnton Blanchard #define EPOW_WARN_COOLING		1
12355fc0c56SAnton Blanchard #define EPOW_WARN_POWER			2
12455fc0c56SAnton Blanchard #define EPOW_SYSTEM_SHUTDOWN		3
12555fc0c56SAnton Blanchard #define EPOW_SYSTEM_HALT		4
12655fc0c56SAnton Blanchard #define EPOW_MAIN_ENCLOSURE		5
12755fc0c56SAnton Blanchard #define EPOW_POWER_OFF			7
12855fc0c56SAnton Blanchard 
12955fc0c56SAnton Blanchard void rtas_parse_epow_errlog(struct rtas_error_log *log)
13055fc0c56SAnton Blanchard {
13155fc0c56SAnton Blanchard 	struct pseries_errorlog *pseries_log;
13255fc0c56SAnton Blanchard 	struct epow_errorlog *epow_log;
13355fc0c56SAnton Blanchard 	char action_code;
13455fc0c56SAnton Blanchard 	char modifier;
13555fc0c56SAnton Blanchard 
13655fc0c56SAnton Blanchard 	pseries_log = get_pseries_errorlog(log, PSERIES_ELOG_SECT_ID_EPOW);
13755fc0c56SAnton Blanchard 	if (pseries_log == NULL)
13855fc0c56SAnton Blanchard 		return;
13955fc0c56SAnton Blanchard 
14055fc0c56SAnton Blanchard 	epow_log = (struct epow_errorlog *)pseries_log->data;
14155fc0c56SAnton Blanchard 	action_code = epow_log->sensor_value & 0xF;	/* bottom 4 bits */
14255fc0c56SAnton Blanchard 	modifier = epow_log->event_modifier & 0xF;	/* bottom 4 bits */
14355fc0c56SAnton Blanchard 
14455fc0c56SAnton Blanchard 	switch (action_code) {
14555fc0c56SAnton Blanchard 	case EPOW_RESET:
14655fc0c56SAnton Blanchard 		pr_err("Non critical power or cooling issue cleared");
14755fc0c56SAnton Blanchard 		break;
14855fc0c56SAnton Blanchard 
14955fc0c56SAnton Blanchard 	case EPOW_WARN_COOLING:
15055fc0c56SAnton Blanchard 		pr_err("Non critical cooling issue reported by firmware");
15155fc0c56SAnton Blanchard 		pr_err("Check RTAS error log for details");
15255fc0c56SAnton Blanchard 		break;
15355fc0c56SAnton Blanchard 
15455fc0c56SAnton Blanchard 	case EPOW_WARN_POWER:
15555fc0c56SAnton Blanchard 		pr_err("Non critical power issue reported by firmware");
15655fc0c56SAnton Blanchard 		pr_err("Check RTAS error log for details");
15755fc0c56SAnton Blanchard 		break;
15855fc0c56SAnton Blanchard 
15955fc0c56SAnton Blanchard 	case EPOW_SYSTEM_SHUTDOWN:
16055fc0c56SAnton Blanchard 		handle_system_shutdown(epow_log->event_modifier);
16155fc0c56SAnton Blanchard 		break;
16255fc0c56SAnton Blanchard 
16355fc0c56SAnton Blanchard 	case EPOW_SYSTEM_HALT:
16455fc0c56SAnton Blanchard 		pr_emerg("Firmware initiated power off");
16555fc0c56SAnton Blanchard 		orderly_poweroff(1);
16655fc0c56SAnton Blanchard 		break;
16755fc0c56SAnton Blanchard 
16855fc0c56SAnton Blanchard 	case EPOW_MAIN_ENCLOSURE:
16955fc0c56SAnton Blanchard 	case EPOW_POWER_OFF:
17055fc0c56SAnton Blanchard 		pr_emerg("Critical power/cooling issue reported by firmware");
17155fc0c56SAnton Blanchard 		pr_emerg("Check RTAS error log for details");
17255fc0c56SAnton Blanchard 		pr_emerg("Immediate power off");
17355fc0c56SAnton Blanchard 		emergency_sync();
17455fc0c56SAnton Blanchard 		kernel_power_off();
17555fc0c56SAnton Blanchard 		break;
17655fc0c56SAnton Blanchard 
17755fc0c56SAnton Blanchard 	default:
17855fc0c56SAnton Blanchard 		pr_err("Unknown power/cooling event (action code %d)",
17955fc0c56SAnton Blanchard 			action_code);
18055fc0c56SAnton Blanchard 	}
18155fc0c56SAnton Blanchard }
18255fc0c56SAnton Blanchard 
18355fc0c56SAnton Blanchard /* Handle environmental and power warning (EPOW) interrupts. */
1847d12e780SDavid Howells static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
185d9953105SMichael Ellerman {
18655fc0c56SAnton Blanchard 	int status;
18755fc0c56SAnton Blanchard 	int state;
188d9953105SMichael Ellerman 	int critical;
189d9953105SMichael Ellerman 
190587f83e8SAnton Blanchard 	status = rtas_get_sensor(EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX, &state);
191d9953105SMichael Ellerman 
192d9953105SMichael Ellerman 	if (state > 3)
193d9953105SMichael Ellerman 		critical = 1;		/* Time Critical */
194d9953105SMichael Ellerman 	else
195d9953105SMichael Ellerman 		critical = 0;
196d9953105SMichael Ellerman 
197d9953105SMichael Ellerman 	spin_lock(&ras_log_buf_lock);
198d9953105SMichael Ellerman 
199d9953105SMichael Ellerman 	status = rtas_call(ras_check_exception_token, 6, 1, NULL,
200b08e281bSMark Nelson 			   RTAS_VECTOR_EXTERNAL_INTERRUPT,
201476eb491SGrant Likely 			   virq_to_hw(irq),
2026f43747fSAnton Blanchard 			   RTAS_EPOW_WARNING,
203d9953105SMichael Ellerman 			   critical, __pa(&ras_log_buf),
204d9953105SMichael Ellerman 				rtas_get_error_log_max());
205d9953105SMichael Ellerman 
206d9953105SMichael Ellerman 	log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
207d9953105SMichael Ellerman 
20855fc0c56SAnton Blanchard 	rtas_parse_epow_errlog((struct rtas_error_log *)ras_log_buf);
20955fc0c56SAnton Blanchard 
210d9953105SMichael Ellerman 	spin_unlock(&ras_log_buf_lock);
211d9953105SMichael Ellerman 	return IRQ_HANDLED;
212d9953105SMichael Ellerman }
213d9953105SMichael Ellerman 
214d9953105SMichael Ellerman /*
215d9953105SMichael Ellerman  * Handle hardware error interrupts.
216d9953105SMichael Ellerman  *
217d9953105SMichael Ellerman  * RTAS check-exception is called to collect data on the exception.  If
218d9953105SMichael Ellerman  * the error is deemed recoverable, we log a warning and return.
219d9953105SMichael Ellerman  * For nonrecoverable errors, an error is logged and we stop all processing
220d9953105SMichael Ellerman  * as quickly as possible in order to prevent propagation of the failure.
221d9953105SMichael Ellerman  */
2227d12e780SDavid Howells static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
223d9953105SMichael Ellerman {
224d9953105SMichael Ellerman 	struct rtas_error_log *rtas_elog;
225cc8b5263SAnton Blanchard 	int status;
226d9953105SMichael Ellerman 	int fatal;
227d9953105SMichael Ellerman 
228d9953105SMichael Ellerman 	spin_lock(&ras_log_buf_lock);
229d9953105SMichael Ellerman 
230d9953105SMichael Ellerman 	status = rtas_call(ras_check_exception_token, 6, 1, NULL,
231b08e281bSMark Nelson 			   RTAS_VECTOR_EXTERNAL_INTERRUPT,
232476eb491SGrant Likely 			   virq_to_hw(irq),
233d9953105SMichael Ellerman 			   RTAS_INTERNAL_ERROR, 1 /* Time Critical */,
234d9953105SMichael Ellerman 			   __pa(&ras_log_buf),
235d9953105SMichael Ellerman 				rtas_get_error_log_max());
236d9953105SMichael Ellerman 
237d9953105SMichael Ellerman 	rtas_elog = (struct rtas_error_log *)ras_log_buf;
238d9953105SMichael Ellerman 
239d9953105SMichael Ellerman 	if ((status == 0) && (rtas_elog->severity >= RTAS_SEVERITY_ERROR_SYNC))
240d9953105SMichael Ellerman 		fatal = 1;
241d9953105SMichael Ellerman 	else
242d9953105SMichael Ellerman 		fatal = 0;
243d9953105SMichael Ellerman 
244d9953105SMichael Ellerman 	/* format and print the extended information */
245d9953105SMichael Ellerman 	log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
246d9953105SMichael Ellerman 
247d9953105SMichael Ellerman 	if (fatal) {
248cc8b5263SAnton Blanchard 		pr_emerg("Fatal hardware error reported by firmware");
249cc8b5263SAnton Blanchard 		pr_emerg("Check RTAS error log for details");
250cc8b5263SAnton Blanchard 		pr_emerg("Immediate power off");
251cc8b5263SAnton Blanchard 		emergency_sync();
252cc8b5263SAnton Blanchard 		kernel_power_off();
253d9953105SMichael Ellerman 	} else {
254cc8b5263SAnton Blanchard 		pr_err("Recoverable hardware error reported by firmware");
255d9953105SMichael Ellerman 	}
256d9953105SMichael Ellerman 
257d9953105SMichael Ellerman 	spin_unlock(&ras_log_buf_lock);
258d9953105SMichael Ellerman 	return IRQ_HANDLED;
259d9953105SMichael Ellerman }
260d9953105SMichael Ellerman 
261d368514cSAnton Blanchard /*
262d368514cSAnton Blanchard  * Some versions of FWNMI place the buffer inside the 4kB page starting at
263d368514cSAnton Blanchard  * 0x7000. Other versions place it inside the rtas buffer. We check both.
264d368514cSAnton Blanchard  */
265d368514cSAnton Blanchard #define VALID_FWNMI_BUFFER(A) \
266d368514cSAnton Blanchard 	((((A) >= 0x7000) && ((A) < 0x7ff0)) || \
267d368514cSAnton Blanchard 	(((A) >= rtas.base) && ((A) < (rtas.base + rtas.size - 16))))
268d368514cSAnton Blanchard 
269d368514cSAnton Blanchard /*
270d368514cSAnton Blanchard  * Get the error information for errors coming through the
271d9953105SMichael Ellerman  * FWNMI vectors.  The pt_regs' r3 will be updated to reflect
272d9953105SMichael Ellerman  * the actual r3 if possible, and a ptr to the error log entry
273d9953105SMichael Ellerman  * will be returned if found.
274d9953105SMichael Ellerman  *
275d368514cSAnton Blanchard  * If the RTAS error is not of the extended type, then we put it in a per
276d368514cSAnton Blanchard  * cpu 64bit buffer. If it is the extended type we use global_mce_data_buf.
277d368514cSAnton Blanchard  *
278d368514cSAnton Blanchard  * The global_mce_data_buf does not have any locks or protection around it,
279d9953105SMichael Ellerman  * if a second machine check comes in, or a system reset is done
280d9953105SMichael Ellerman  * before we have logged the error, then we will get corruption in the
281d9953105SMichael Ellerman  * error log.  This is preferable over holding off on calling
282d9953105SMichael Ellerman  * ibm,nmi-interlock which would result in us checkstopping if a
283d9953105SMichael Ellerman  * second machine check did come in.
284d9953105SMichael Ellerman  */
285d9953105SMichael Ellerman static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
286d9953105SMichael Ellerman {
287d9953105SMichael Ellerman 	unsigned long *savep;
288d368514cSAnton Blanchard 	struct rtas_error_log *h, *errhdr = NULL;
289d9953105SMichael Ellerman 
290d368514cSAnton Blanchard 	if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
291f0e939aeSAnton Blanchard 		printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
292d368514cSAnton Blanchard 		return NULL;
293d9953105SMichael Ellerman 	}
294d368514cSAnton Blanchard 
295d368514cSAnton Blanchard 	savep = __va(regs->gpr[3]);
296d368514cSAnton Blanchard 	regs->gpr[3] = savep[0];	/* restore original r3 */
297d368514cSAnton Blanchard 
298d368514cSAnton Blanchard 	/* If it isn't an extended log we can use the per cpu 64bit buffer */
299d368514cSAnton Blanchard 	h = (struct rtas_error_log *)&savep[1];
300d368514cSAnton Blanchard 	if (!h->extended) {
301d368514cSAnton Blanchard 		memcpy(&__get_cpu_var(mce_data_buf), h, sizeof(__u64));
302d368514cSAnton Blanchard 		errhdr = (struct rtas_error_log *)&__get_cpu_var(mce_data_buf);
303d368514cSAnton Blanchard 	} else {
304d368514cSAnton Blanchard 		int len;
305d368514cSAnton Blanchard 
306d368514cSAnton Blanchard 		len = max_t(int, 8+h->extended_log_length, RTAS_ERROR_LOG_MAX);
307d368514cSAnton Blanchard 		memset(global_mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
308d368514cSAnton Blanchard 		memcpy(global_mce_data_buf, h, len);
309d368514cSAnton Blanchard 		errhdr = (struct rtas_error_log *)global_mce_data_buf;
310d368514cSAnton Blanchard 	}
311d368514cSAnton Blanchard 
312d9953105SMichael Ellerman 	return errhdr;
313d9953105SMichael Ellerman }
314d9953105SMichael Ellerman 
315d9953105SMichael Ellerman /* Call this when done with the data returned by FWNMI_get_errinfo.
316d9953105SMichael Ellerman  * It will release the saved data area for other CPUs in the
317d9953105SMichael Ellerman  * partition to receive FWNMI errors.
318d9953105SMichael Ellerman  */
319d9953105SMichael Ellerman static void fwnmi_release_errinfo(void)
320d9953105SMichael Ellerman {
321d9953105SMichael Ellerman 	int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
322d9953105SMichael Ellerman 	if (ret != 0)
323d368514cSAnton Blanchard 		printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
324d9953105SMichael Ellerman }
325d9953105SMichael Ellerman 
326c902be71SArnd Bergmann int pSeries_system_reset_exception(struct pt_regs *regs)
327d9953105SMichael Ellerman {
328d9953105SMichael Ellerman 	if (fwnmi_active) {
329d9953105SMichael Ellerman 		struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
330d9953105SMichael Ellerman 		if (errhdr) {
331d9953105SMichael Ellerman 			/* XXX Should look at FWNMI information */
332d9953105SMichael Ellerman 		}
333d9953105SMichael Ellerman 		fwnmi_release_errinfo();
334d9953105SMichael Ellerman 	}
335c902be71SArnd Bergmann 	return 0; /* need to perform reset */
336d9953105SMichael Ellerman }
337d9953105SMichael Ellerman 
338d9953105SMichael Ellerman /*
339d9953105SMichael Ellerman  * See if we can recover from a machine check exception.
340d9953105SMichael Ellerman  * This is only called on power4 (or above) and only via
341d9953105SMichael Ellerman  * the Firmware Non-Maskable Interrupts (fwnmi) handler
342d9953105SMichael Ellerman  * which provides the error analysis for us.
343d9953105SMichael Ellerman  *
344d9953105SMichael Ellerman  * Return 1 if corrected (or delivered a signal).
345d9953105SMichael Ellerman  * Return 0 if there is nothing we can do.
346d9953105SMichael Ellerman  */
347d9953105SMichael Ellerman static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
348d9953105SMichael Ellerman {
349d47d1d8aSAnton Blanchard 	int recovered = 0;
350d9953105SMichael Ellerman 
351d47d1d8aSAnton Blanchard 	if (!(regs->msr & MSR_RI)) {
352d47d1d8aSAnton Blanchard 		/* If MSR_RI isn't set, we cannot recover */
353d47d1d8aSAnton Blanchard 		recovered = 0;
354d47d1d8aSAnton Blanchard 
355d47d1d8aSAnton Blanchard 	} else if (err->disposition == RTAS_DISP_FULLY_RECOVERED) {
356d9953105SMichael Ellerman 		/* Platform corrected itself */
357d47d1d8aSAnton Blanchard 		recovered = 1;
358d47d1d8aSAnton Blanchard 
359d47d1d8aSAnton Blanchard 	} else if (err->disposition == RTAS_DISP_LIMITED_RECOVERY) {
360d47d1d8aSAnton Blanchard 		/* Platform corrected itself but could be degraded */
361d47d1d8aSAnton Blanchard 		printk(KERN_ERR "MCE: limited recovery, system may "
362d47d1d8aSAnton Blanchard 		       "be degraded\n");
363d47d1d8aSAnton Blanchard 		recovered = 1;
364d47d1d8aSAnton Blanchard 
365d47d1d8aSAnton Blanchard 	} else if (user_mode(regs) && !is_global_init(current) &&
366d47d1d8aSAnton Blanchard 		   err->severity == RTAS_SEVERITY_ERROR_SYNC) {
367d47d1d8aSAnton Blanchard 
368d47d1d8aSAnton Blanchard 		/*
369d47d1d8aSAnton Blanchard 		 * If we received a synchronous error when in userspace
370d47d1d8aSAnton Blanchard 		 * kill the task. Firmware may report details of the fail
371d47d1d8aSAnton Blanchard 		 * asynchronously, so we can't rely on the target and type
372d47d1d8aSAnton Blanchard 		 * fields being valid here.
373d47d1d8aSAnton Blanchard 		 */
374d47d1d8aSAnton Blanchard 		printk(KERN_ERR "MCE: uncorrectable error, killing task "
375d47d1d8aSAnton Blanchard 		       "%s:%d\n", current->comm, current->pid);
376d47d1d8aSAnton Blanchard 
377d47d1d8aSAnton Blanchard 		_exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
378d47d1d8aSAnton Blanchard 		recovered = 1;
379d9953105SMichael Ellerman 	}
380d9953105SMichael Ellerman 
3813f9793e6SAnton Blanchard 	log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
382d9953105SMichael Ellerman 
383d47d1d8aSAnton Blanchard 	return recovered;
384d9953105SMichael Ellerman }
385d9953105SMichael Ellerman 
386d9953105SMichael Ellerman /*
387d9953105SMichael Ellerman  * Handle a machine check.
388d9953105SMichael Ellerman  *
389d9953105SMichael Ellerman  * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
390d9953105SMichael Ellerman  * should be present.  If so the handler which called us tells us if the
391d9953105SMichael Ellerman  * error was recovered (never true if RI=0).
392d9953105SMichael Ellerman  *
393d9953105SMichael Ellerman  * On hardware prior to Power 4 these exceptions were asynchronous which
394d9953105SMichael Ellerman  * means we can't tell exactly where it occurred and so we can't recover.
395d9953105SMichael Ellerman  */
396d9953105SMichael Ellerman int pSeries_machine_check_exception(struct pt_regs *regs)
397d9953105SMichael Ellerman {
398d9953105SMichael Ellerman 	struct rtas_error_log *errp;
399d9953105SMichael Ellerman 
400d9953105SMichael Ellerman 	if (fwnmi_active) {
401d9953105SMichael Ellerman 		errp = fwnmi_get_errinfo(regs);
402d9953105SMichael Ellerman 		fwnmi_release_errinfo();
403d9953105SMichael Ellerman 		if (errp && recover_mce(regs, errp))
404d9953105SMichael Ellerman 			return 1;
405d9953105SMichael Ellerman 	}
406d9953105SMichael Ellerman 
407d9953105SMichael Ellerman 	return 0;
408d9953105SMichael Ellerman }
409