xref: /openbmc/linux/arch/powerpc/platforms/pseries/ras.c (revision b7d9eb397b8764c1f1c53d504aa70f85ce0e212f)
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>
2290128997SAnton 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 
43b4af279aSVipin K Parashar /* EPOW events counter variable */
44b4af279aSVipin K Parashar static int num_epow_events;
45b4af279aSVipin K Parashar 
46*b7d9eb39SJohn Allen static irqreturn_t ras_hotplug_interrupt(int irq, void *dev_id);
477d12e780SDavid Howells static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
487d12e780SDavid Howells static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
49d9953105SMichael Ellerman 
500ebfff14SBenjamin Herrenschmidt 
51d9953105SMichael Ellerman /*
52d9953105SMichael Ellerman  * Initialize handlers for the set of interrupts caused by hardware errors
53d9953105SMichael Ellerman  * and power system events.
54d9953105SMichael Ellerman  */
55d9953105SMichael Ellerman static int __init init_ras_IRQ(void)
56d9953105SMichael Ellerman {
57d9953105SMichael Ellerman 	struct device_node *np;
58d9953105SMichael Ellerman 
59d9953105SMichael Ellerman 	ras_check_exception_token = rtas_token("check-exception");
60d9953105SMichael Ellerman 
61d9953105SMichael Ellerman 	/* Internal Errors */
62d9953105SMichael Ellerman 	np = of_find_node_by_path("/event-sources/internal-errors");
63d9953105SMichael Ellerman 	if (np != NULL) {
6432c96f77SMark Nelson 		request_event_sources_irqs(np, ras_error_interrupt,
6532c96f77SMark Nelson 					   "RAS_ERROR");
66d9953105SMichael Ellerman 		of_node_put(np);
67d9953105SMichael Ellerman 	}
68d9953105SMichael Ellerman 
69*b7d9eb39SJohn Allen 	/* Hotplug Events */
70*b7d9eb39SJohn Allen 	np = of_find_node_by_path("/event-sources/hot-plug-events");
71*b7d9eb39SJohn Allen 	if (np != NULL) {
72*b7d9eb39SJohn Allen 		request_event_sources_irqs(np, ras_hotplug_interrupt,
73*b7d9eb39SJohn Allen 					   "RAS_HOTPLUG");
74*b7d9eb39SJohn Allen 		of_node_put(np);
75*b7d9eb39SJohn Allen 	}
76*b7d9eb39SJohn Allen 
77d9953105SMichael Ellerman 	/* EPOW Events */
78d9953105SMichael Ellerman 	np = of_find_node_by_path("/event-sources/epow-events");
79d9953105SMichael Ellerman 	if (np != NULL) {
8032c96f77SMark Nelson 		request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
81d9953105SMichael Ellerman 		of_node_put(np);
82d9953105SMichael Ellerman 	}
83d9953105SMichael Ellerman 
8469ed3324SAnton Blanchard 	return 0;
85d9953105SMichael Ellerman }
868e83e905SMichael Ellerman machine_subsys_initcall(pseries, init_ras_IRQ);
87d9953105SMichael Ellerman 
8855fc0c56SAnton Blanchard #define EPOW_SHUTDOWN_NORMAL				1
8955fc0c56SAnton Blanchard #define EPOW_SHUTDOWN_ON_UPS				2
9055fc0c56SAnton Blanchard #define EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS	3
9155fc0c56SAnton Blanchard #define EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH	4
9255fc0c56SAnton Blanchard 
9355fc0c56SAnton Blanchard static void handle_system_shutdown(char event_modifier)
9455fc0c56SAnton Blanchard {
9555fc0c56SAnton Blanchard 	switch (event_modifier) {
9655fc0c56SAnton Blanchard 	case EPOW_SHUTDOWN_NORMAL:
97b4af279aSVipin K Parashar 		pr_emerg("Power off requested\n");
981b7e0cbeSliguang 		orderly_poweroff(true);
9955fc0c56SAnton Blanchard 		break;
10055fc0c56SAnton Blanchard 
10155fc0c56SAnton Blanchard 	case EPOW_SHUTDOWN_ON_UPS:
102b4af279aSVipin K Parashar 		pr_emerg("Loss of system power detected. System is running on"
103b4af279aSVipin K Parashar 			 " UPS/battery. Check RTAS error log for details\n");
10479872e35SAnshuman Khandual 		orderly_poweroff(true);
10555fc0c56SAnton Blanchard 		break;
10655fc0c56SAnton Blanchard 
10755fc0c56SAnton Blanchard 	case EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS:
108b4af279aSVipin K Parashar 		pr_emerg("Loss of system critical functions detected. Check"
109b4af279aSVipin K Parashar 			 " RTAS error log for details\n");
1101b7e0cbeSliguang 		orderly_poweroff(true);
11155fc0c56SAnton Blanchard 		break;
11255fc0c56SAnton Blanchard 
11355fc0c56SAnton Blanchard 	case EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH:
114b4af279aSVipin K Parashar 		pr_emerg("High ambient temperature detected. Check RTAS"
115b4af279aSVipin K Parashar 			 " error log for details\n");
1161b7e0cbeSliguang 		orderly_poweroff(true);
11755fc0c56SAnton Blanchard 		break;
11855fc0c56SAnton Blanchard 
11955fc0c56SAnton Blanchard 	default:
120b4af279aSVipin K Parashar 		pr_err("Unknown power/cooling shutdown event (modifier = %d)\n",
12155fc0c56SAnton Blanchard 			event_modifier);
12255fc0c56SAnton Blanchard 	}
12355fc0c56SAnton Blanchard }
12455fc0c56SAnton Blanchard 
12555fc0c56SAnton Blanchard struct epow_errorlog {
12655fc0c56SAnton Blanchard 	unsigned char sensor_value;
12755fc0c56SAnton Blanchard 	unsigned char event_modifier;
12855fc0c56SAnton Blanchard 	unsigned char extended_modifier;
12955fc0c56SAnton Blanchard 	unsigned char reserved;
13055fc0c56SAnton Blanchard 	unsigned char platform_reason;
13155fc0c56SAnton Blanchard };
13255fc0c56SAnton Blanchard 
13355fc0c56SAnton Blanchard #define EPOW_RESET			0
13455fc0c56SAnton Blanchard #define EPOW_WARN_COOLING		1
13555fc0c56SAnton Blanchard #define EPOW_WARN_POWER			2
13655fc0c56SAnton Blanchard #define EPOW_SYSTEM_SHUTDOWN		3
13755fc0c56SAnton Blanchard #define EPOW_SYSTEM_HALT		4
13855fc0c56SAnton Blanchard #define EPOW_MAIN_ENCLOSURE		5
13955fc0c56SAnton Blanchard #define EPOW_POWER_OFF			7
14055fc0c56SAnton Blanchard 
141e51df2c1SAnton Blanchard static void rtas_parse_epow_errlog(struct rtas_error_log *log)
14255fc0c56SAnton Blanchard {
14355fc0c56SAnton Blanchard 	struct pseries_errorlog *pseries_log;
14455fc0c56SAnton Blanchard 	struct epow_errorlog *epow_log;
14555fc0c56SAnton Blanchard 	char action_code;
14655fc0c56SAnton Blanchard 	char modifier;
14755fc0c56SAnton Blanchard 
14855fc0c56SAnton Blanchard 	pseries_log = get_pseries_errorlog(log, PSERIES_ELOG_SECT_ID_EPOW);
14955fc0c56SAnton Blanchard 	if (pseries_log == NULL)
15055fc0c56SAnton Blanchard 		return;
15155fc0c56SAnton Blanchard 
15255fc0c56SAnton Blanchard 	epow_log = (struct epow_errorlog *)pseries_log->data;
15355fc0c56SAnton Blanchard 	action_code = epow_log->sensor_value & 0xF;	/* bottom 4 bits */
15455fc0c56SAnton Blanchard 	modifier = epow_log->event_modifier & 0xF;	/* bottom 4 bits */
15555fc0c56SAnton Blanchard 
15655fc0c56SAnton Blanchard 	switch (action_code) {
15755fc0c56SAnton Blanchard 	case EPOW_RESET:
158b4af279aSVipin K Parashar 		if (num_epow_events) {
159b4af279aSVipin K Parashar 			pr_info("Non critical power/cooling issue cleared\n");
160b4af279aSVipin K Parashar 			num_epow_events--;
161b4af279aSVipin K Parashar 		}
16255fc0c56SAnton Blanchard 		break;
16355fc0c56SAnton Blanchard 
16455fc0c56SAnton Blanchard 	case EPOW_WARN_COOLING:
165b4af279aSVipin K Parashar 		pr_info("Non-critical cooling issue detected. Check RTAS error"
166b4af279aSVipin K Parashar 			" log for details\n");
16755fc0c56SAnton Blanchard 		break;
16855fc0c56SAnton Blanchard 
16955fc0c56SAnton Blanchard 	case EPOW_WARN_POWER:
170b4af279aSVipin K Parashar 		pr_info("Non-critical power issue detected. Check RTAS error"
171b4af279aSVipin K Parashar 			" log for details\n");
17255fc0c56SAnton Blanchard 		break;
17355fc0c56SAnton Blanchard 
17455fc0c56SAnton Blanchard 	case EPOW_SYSTEM_SHUTDOWN:
17555fc0c56SAnton Blanchard 		handle_system_shutdown(epow_log->event_modifier);
17655fc0c56SAnton Blanchard 		break;
17755fc0c56SAnton Blanchard 
17855fc0c56SAnton Blanchard 	case EPOW_SYSTEM_HALT:
179b4af279aSVipin K Parashar 		pr_emerg("Critical power/cooling issue detected. Check RTAS"
180b4af279aSVipin K Parashar 			 " error log for details. Powering off.\n");
1811b7e0cbeSliguang 		orderly_poweroff(true);
18255fc0c56SAnton Blanchard 		break;
18355fc0c56SAnton Blanchard 
18455fc0c56SAnton Blanchard 	case EPOW_MAIN_ENCLOSURE:
18555fc0c56SAnton Blanchard 	case EPOW_POWER_OFF:
186b4af279aSVipin K Parashar 		pr_emerg("System about to lose power. Check RTAS error log "
187b4af279aSVipin K Parashar 			 " for details. Powering off immediately.\n");
18855fc0c56SAnton Blanchard 		emergency_sync();
18955fc0c56SAnton Blanchard 		kernel_power_off();
19055fc0c56SAnton Blanchard 		break;
19155fc0c56SAnton Blanchard 
19255fc0c56SAnton Blanchard 	default:
193b4af279aSVipin K Parashar 		pr_err("Unknown power/cooling event (action code  = %d)\n",
19455fc0c56SAnton Blanchard 			action_code);
19555fc0c56SAnton Blanchard 	}
196b4af279aSVipin K Parashar 
197b4af279aSVipin K Parashar 	/* Increment epow events counter variable */
198b4af279aSVipin K Parashar 	if (action_code != EPOW_RESET)
199b4af279aSVipin K Parashar 		num_epow_events++;
20055fc0c56SAnton Blanchard }
20155fc0c56SAnton Blanchard 
202*b7d9eb39SJohn Allen static irqreturn_t ras_hotplug_interrupt(int irq, void *dev_id)
203*b7d9eb39SJohn Allen {
204*b7d9eb39SJohn Allen 	struct pseries_errorlog *pseries_log;
205*b7d9eb39SJohn Allen 	struct pseries_hp_errorlog *hp_elog;
206*b7d9eb39SJohn Allen 
207*b7d9eb39SJohn Allen 	spin_lock(&ras_log_buf_lock);
208*b7d9eb39SJohn Allen 
209*b7d9eb39SJohn Allen 	rtas_call(ras_check_exception_token, 6, 1, NULL,
210*b7d9eb39SJohn Allen 		  RTAS_VECTOR_EXTERNAL_INTERRUPT, virq_to_hw(irq),
211*b7d9eb39SJohn Allen 		  RTAS_HOTPLUG_EVENTS, 0, __pa(&ras_log_buf),
212*b7d9eb39SJohn Allen 		  rtas_get_error_log_max());
213*b7d9eb39SJohn Allen 
214*b7d9eb39SJohn Allen 	pseries_log = get_pseries_errorlog((struct rtas_error_log *)ras_log_buf,
215*b7d9eb39SJohn Allen 					   PSERIES_ELOG_SECT_ID_HOTPLUG);
216*b7d9eb39SJohn Allen 	hp_elog = (struct pseries_hp_errorlog *)pseries_log->data;
217*b7d9eb39SJohn Allen 
218*b7d9eb39SJohn Allen 	/*
219*b7d9eb39SJohn Allen 	 * Since PCI hotplug is not currently supported on pseries, put PCI
220*b7d9eb39SJohn Allen 	 * hotplug events on the ras_log_buf to be handled by rtas_errd.
221*b7d9eb39SJohn Allen 	 */
222*b7d9eb39SJohn Allen 	if (hp_elog->resource == PSERIES_HP_ELOG_RESOURCE_MEM ||
223*b7d9eb39SJohn Allen 	    hp_elog->resource == PSERIES_HP_ELOG_RESOURCE_CPU)
224*b7d9eb39SJohn Allen 		queue_hotplug_event(hp_elog, NULL, NULL);
225*b7d9eb39SJohn Allen 	else
226*b7d9eb39SJohn Allen 		log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
227*b7d9eb39SJohn Allen 
228*b7d9eb39SJohn Allen 	spin_unlock(&ras_log_buf_lock);
229*b7d9eb39SJohn Allen 	return IRQ_HANDLED;
230*b7d9eb39SJohn Allen }
231*b7d9eb39SJohn Allen 
23255fc0c56SAnton Blanchard /* Handle environmental and power warning (EPOW) interrupts. */
2337d12e780SDavid Howells static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
234d9953105SMichael Ellerman {
23555fc0c56SAnton Blanchard 	int status;
23655fc0c56SAnton Blanchard 	int state;
237d9953105SMichael Ellerman 	int critical;
238d9953105SMichael Ellerman 
2391c2cb594SThomas Huth 	status = rtas_get_sensor_fast(EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX,
2401c2cb594SThomas Huth 				      &state);
241d9953105SMichael Ellerman 
242d9953105SMichael Ellerman 	if (state > 3)
243d9953105SMichael Ellerman 		critical = 1;		/* Time Critical */
244d9953105SMichael Ellerman 	else
245d9953105SMichael Ellerman 		critical = 0;
246d9953105SMichael Ellerman 
247d9953105SMichael Ellerman 	spin_lock(&ras_log_buf_lock);
248d9953105SMichael Ellerman 
249d9953105SMichael Ellerman 	status = rtas_call(ras_check_exception_token, 6, 1, NULL,
250b08e281bSMark Nelson 			   RTAS_VECTOR_EXTERNAL_INTERRUPT,
251476eb491SGrant Likely 			   virq_to_hw(irq),
2526f43747fSAnton Blanchard 			   RTAS_EPOW_WARNING,
253d9953105SMichael Ellerman 			   critical, __pa(&ras_log_buf),
254d9953105SMichael Ellerman 				rtas_get_error_log_max());
255d9953105SMichael Ellerman 
256d9953105SMichael Ellerman 	log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
257d9953105SMichael Ellerman 
25855fc0c56SAnton Blanchard 	rtas_parse_epow_errlog((struct rtas_error_log *)ras_log_buf);
25955fc0c56SAnton Blanchard 
260d9953105SMichael Ellerman 	spin_unlock(&ras_log_buf_lock);
261d9953105SMichael Ellerman 	return IRQ_HANDLED;
262d9953105SMichael Ellerman }
263d9953105SMichael Ellerman 
264d9953105SMichael Ellerman /*
265d9953105SMichael Ellerman  * Handle hardware error interrupts.
266d9953105SMichael Ellerman  *
267d9953105SMichael Ellerman  * RTAS check-exception is called to collect data on the exception.  If
268d9953105SMichael Ellerman  * the error is deemed recoverable, we log a warning and return.
269d9953105SMichael Ellerman  * For nonrecoverable errors, an error is logged and we stop all processing
270d9953105SMichael Ellerman  * as quickly as possible in order to prevent propagation of the failure.
271d9953105SMichael Ellerman  */
2727d12e780SDavid Howells static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
273d9953105SMichael Ellerman {
274d9953105SMichael Ellerman 	struct rtas_error_log *rtas_elog;
275cc8b5263SAnton Blanchard 	int status;
276d9953105SMichael Ellerman 	int fatal;
277d9953105SMichael Ellerman 
278d9953105SMichael Ellerman 	spin_lock(&ras_log_buf_lock);
279d9953105SMichael Ellerman 
280d9953105SMichael Ellerman 	status = rtas_call(ras_check_exception_token, 6, 1, NULL,
281b08e281bSMark Nelson 			   RTAS_VECTOR_EXTERNAL_INTERRUPT,
282476eb491SGrant Likely 			   virq_to_hw(irq),
283d9953105SMichael Ellerman 			   RTAS_INTERNAL_ERROR, 1 /* Time Critical */,
284d9953105SMichael Ellerman 			   __pa(&ras_log_buf),
285d9953105SMichael Ellerman 				rtas_get_error_log_max());
286d9953105SMichael Ellerman 
287d9953105SMichael Ellerman 	rtas_elog = (struct rtas_error_log *)ras_log_buf;
288d9953105SMichael Ellerman 
289a08a53eaSGreg Kurz 	if (status == 0 &&
290a08a53eaSGreg Kurz 	    rtas_error_severity(rtas_elog) >= RTAS_SEVERITY_ERROR_SYNC)
291d9953105SMichael Ellerman 		fatal = 1;
292d9953105SMichael Ellerman 	else
293d9953105SMichael Ellerman 		fatal = 0;
294d9953105SMichael Ellerman 
295d9953105SMichael Ellerman 	/* format and print the extended information */
296d9953105SMichael Ellerman 	log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
297d9953105SMichael Ellerman 
298d9953105SMichael Ellerman 	if (fatal) {
299b4af279aSVipin K Parashar 		pr_emerg("Fatal hardware error detected. Check RTAS error"
300b4af279aSVipin K Parashar 			 " log for details. Powering off immediately\n");
301cc8b5263SAnton Blanchard 		emergency_sync();
302cc8b5263SAnton Blanchard 		kernel_power_off();
303d9953105SMichael Ellerman 	} else {
304b4af279aSVipin K Parashar 		pr_err("Recoverable hardware error detected\n");
305d9953105SMichael Ellerman 	}
306d9953105SMichael Ellerman 
307d9953105SMichael Ellerman 	spin_unlock(&ras_log_buf_lock);
308d9953105SMichael Ellerman 	return IRQ_HANDLED;
309d9953105SMichael Ellerman }
310d9953105SMichael Ellerman 
311d368514cSAnton Blanchard /*
312d368514cSAnton Blanchard  * Some versions of FWNMI place the buffer inside the 4kB page starting at
313d368514cSAnton Blanchard  * 0x7000. Other versions place it inside the rtas buffer. We check both.
314d368514cSAnton Blanchard  */
315d368514cSAnton Blanchard #define VALID_FWNMI_BUFFER(A) \
316d368514cSAnton Blanchard 	((((A) >= 0x7000) && ((A) < 0x7ff0)) || \
317d368514cSAnton Blanchard 	(((A) >= rtas.base) && ((A) < (rtas.base + rtas.size - 16))))
318d368514cSAnton Blanchard 
319d368514cSAnton Blanchard /*
320d368514cSAnton Blanchard  * Get the error information for errors coming through the
321d9953105SMichael Ellerman  * FWNMI vectors.  The pt_regs' r3 will be updated to reflect
322d9953105SMichael Ellerman  * the actual r3 if possible, and a ptr to the error log entry
323d9953105SMichael Ellerman  * will be returned if found.
324d9953105SMichael Ellerman  *
325d368514cSAnton Blanchard  * If the RTAS error is not of the extended type, then we put it in a per
326d368514cSAnton Blanchard  * cpu 64bit buffer. If it is the extended type we use global_mce_data_buf.
327d368514cSAnton Blanchard  *
328d368514cSAnton Blanchard  * The global_mce_data_buf does not have any locks or protection around it,
329d9953105SMichael Ellerman  * if a second machine check comes in, or a system reset is done
330d9953105SMichael Ellerman  * before we have logged the error, then we will get corruption in the
331d9953105SMichael Ellerman  * error log.  This is preferable over holding off on calling
332d9953105SMichael Ellerman  * ibm,nmi-interlock which would result in us checkstopping if a
333d9953105SMichael Ellerman  * second machine check did come in.
334d9953105SMichael Ellerman  */
335d9953105SMichael Ellerman static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
336d9953105SMichael Ellerman {
337d9953105SMichael Ellerman 	unsigned long *savep;
338d368514cSAnton Blanchard 	struct rtas_error_log *h, *errhdr = NULL;
339d9953105SMichael Ellerman 
340ee1dd1e3SMahesh Salgaonkar 	/* Mask top two bits */
341ee1dd1e3SMahesh Salgaonkar 	regs->gpr[3] &= ~(0x3UL << 62);
342ee1dd1e3SMahesh Salgaonkar 
343d368514cSAnton Blanchard 	if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
344f0e939aeSAnton Blanchard 		printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
345d368514cSAnton Blanchard 		return NULL;
346d9953105SMichael Ellerman 	}
347d368514cSAnton Blanchard 
348d368514cSAnton Blanchard 	savep = __va(regs->gpr[3]);
349d368514cSAnton Blanchard 	regs->gpr[3] = savep[0];	/* restore original r3 */
350d368514cSAnton Blanchard 
351d368514cSAnton Blanchard 	/* If it isn't an extended log we can use the per cpu 64bit buffer */
352d368514cSAnton Blanchard 	h = (struct rtas_error_log *)&savep[1];
353a08a53eaSGreg Kurz 	if (!rtas_error_extended(h)) {
35469111bacSChristoph Lameter 		memcpy(this_cpu_ptr(&mce_data_buf), h, sizeof(__u64));
35569111bacSChristoph Lameter 		errhdr = (struct rtas_error_log *)this_cpu_ptr(&mce_data_buf);
356d368514cSAnton Blanchard 	} else {
357a08a53eaSGreg Kurz 		int len, error_log_length;
358d368514cSAnton Blanchard 
359a08a53eaSGreg Kurz 		error_log_length = 8 + rtas_error_extended_log_length(h);
360a08a53eaSGreg Kurz 		len = max_t(int, error_log_length, RTAS_ERROR_LOG_MAX);
361d368514cSAnton Blanchard 		memset(global_mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
362d368514cSAnton Blanchard 		memcpy(global_mce_data_buf, h, len);
363d368514cSAnton Blanchard 		errhdr = (struct rtas_error_log *)global_mce_data_buf;
364d368514cSAnton Blanchard 	}
365d368514cSAnton Blanchard 
366d9953105SMichael Ellerman 	return errhdr;
367d9953105SMichael Ellerman }
368d9953105SMichael Ellerman 
369d9953105SMichael Ellerman /* Call this when done with the data returned by FWNMI_get_errinfo.
370d9953105SMichael Ellerman  * It will release the saved data area for other CPUs in the
371d9953105SMichael Ellerman  * partition to receive FWNMI errors.
372d9953105SMichael Ellerman  */
373d9953105SMichael Ellerman static void fwnmi_release_errinfo(void)
374d9953105SMichael Ellerman {
375d9953105SMichael Ellerman 	int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
376d9953105SMichael Ellerman 	if (ret != 0)
377d368514cSAnton Blanchard 		printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
378d9953105SMichael Ellerman }
379d9953105SMichael Ellerman 
380c902be71SArnd Bergmann int pSeries_system_reset_exception(struct pt_regs *regs)
381d9953105SMichael Ellerman {
382d9953105SMichael Ellerman 	if (fwnmi_active) {
383d9953105SMichael Ellerman 		struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
384d9953105SMichael Ellerman 		if (errhdr) {
385d9953105SMichael Ellerman 			/* XXX Should look at FWNMI information */
386d9953105SMichael Ellerman 		}
387d9953105SMichael Ellerman 		fwnmi_release_errinfo();
388d9953105SMichael Ellerman 	}
389c902be71SArnd Bergmann 	return 0; /* need to perform reset */
390d9953105SMichael Ellerman }
391d9953105SMichael Ellerman 
392d9953105SMichael Ellerman /*
393d9953105SMichael Ellerman  * See if we can recover from a machine check exception.
394d9953105SMichael Ellerman  * This is only called on power4 (or above) and only via
395d9953105SMichael Ellerman  * the Firmware Non-Maskable Interrupts (fwnmi) handler
396d9953105SMichael Ellerman  * which provides the error analysis for us.
397d9953105SMichael Ellerman  *
398d9953105SMichael Ellerman  * Return 1 if corrected (or delivered a signal).
399d9953105SMichael Ellerman  * Return 0 if there is nothing we can do.
400d9953105SMichael Ellerman  */
401d9953105SMichael Ellerman static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
402d9953105SMichael Ellerman {
403d47d1d8aSAnton Blanchard 	int recovered = 0;
404a08a53eaSGreg Kurz 	int disposition = rtas_error_disposition(err);
405d9953105SMichael Ellerman 
406d47d1d8aSAnton Blanchard 	if (!(regs->msr & MSR_RI)) {
407d47d1d8aSAnton Blanchard 		/* If MSR_RI isn't set, we cannot recover */
408d47d1d8aSAnton Blanchard 		recovered = 0;
409d47d1d8aSAnton Blanchard 
410a08a53eaSGreg Kurz 	} else if (disposition == RTAS_DISP_FULLY_RECOVERED) {
411d9953105SMichael Ellerman 		/* Platform corrected itself */
412d47d1d8aSAnton Blanchard 		recovered = 1;
413d47d1d8aSAnton Blanchard 
414a08a53eaSGreg Kurz 	} else if (disposition == RTAS_DISP_LIMITED_RECOVERY) {
415d47d1d8aSAnton Blanchard 		/* Platform corrected itself but could be degraded */
416d47d1d8aSAnton Blanchard 		printk(KERN_ERR "MCE: limited recovery, system may "
417d47d1d8aSAnton Blanchard 		       "be degraded\n");
418d47d1d8aSAnton Blanchard 		recovered = 1;
419d47d1d8aSAnton Blanchard 
420d47d1d8aSAnton Blanchard 	} else if (user_mode(regs) && !is_global_init(current) &&
421a08a53eaSGreg Kurz 		   rtas_error_severity(err) == RTAS_SEVERITY_ERROR_SYNC) {
422d47d1d8aSAnton Blanchard 
423d47d1d8aSAnton Blanchard 		/*
424d47d1d8aSAnton Blanchard 		 * If we received a synchronous error when in userspace
425d47d1d8aSAnton Blanchard 		 * kill the task. Firmware may report details of the fail
426d47d1d8aSAnton Blanchard 		 * asynchronously, so we can't rely on the target and type
427d47d1d8aSAnton Blanchard 		 * fields being valid here.
428d47d1d8aSAnton Blanchard 		 */
429d47d1d8aSAnton Blanchard 		printk(KERN_ERR "MCE: uncorrectable error, killing task "
430d47d1d8aSAnton Blanchard 		       "%s:%d\n", current->comm, current->pid);
431d47d1d8aSAnton Blanchard 
432d47d1d8aSAnton Blanchard 		_exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
433d47d1d8aSAnton Blanchard 		recovered = 1;
434d9953105SMichael Ellerman 	}
435d9953105SMichael Ellerman 
4363f9793e6SAnton Blanchard 	log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
437d9953105SMichael Ellerman 
438d47d1d8aSAnton Blanchard 	return recovered;
439d9953105SMichael Ellerman }
440d9953105SMichael Ellerman 
441d9953105SMichael Ellerman /*
442d9953105SMichael Ellerman  * Handle a machine check.
443d9953105SMichael Ellerman  *
444d9953105SMichael Ellerman  * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
445d9953105SMichael Ellerman  * should be present.  If so the handler which called us tells us if the
446d9953105SMichael Ellerman  * error was recovered (never true if RI=0).
447d9953105SMichael Ellerman  *
448d9953105SMichael Ellerman  * On hardware prior to Power 4 these exceptions were asynchronous which
449d9953105SMichael Ellerman  * means we can't tell exactly where it occurred and so we can't recover.
450d9953105SMichael Ellerman  */
451d9953105SMichael Ellerman int pSeries_machine_check_exception(struct pt_regs *regs)
452d9953105SMichael Ellerman {
453d9953105SMichael Ellerman 	struct rtas_error_log *errp;
454d9953105SMichael Ellerman 
455d9953105SMichael Ellerman 	if (fwnmi_active) {
456d9953105SMichael Ellerman 		errp = fwnmi_get_errinfo(regs);
457d9953105SMichael Ellerman 		fwnmi_release_errinfo();
458d9953105SMichael Ellerman 		if (errp && recover_mce(regs, errp))
459d9953105SMichael Ellerman 			return 1;
460d9953105SMichael Ellerman 	}
461d9953105SMichael Ellerman 
462d9953105SMichael Ellerman 	return 0;
463d9953105SMichael Ellerman }
464