xref: /openbmc/linux/arch/powerpc/platforms/pseries/ras.c (revision f0e939ae373836400b38e090d78fba6a183976f0)
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 /* Change Activity:
20d9953105SMichael Ellerman  * 2001/09/21 : engebret : Created with minimal EPOW and HW exception support.
21d9953105SMichael Ellerman  * End Change Activity
22d9953105SMichael Ellerman  */
23d9953105SMichael Ellerman 
24d9953105SMichael Ellerman #include <linux/errno.h>
25d9953105SMichael Ellerman #include <linux/threads.h>
26d9953105SMichael Ellerman #include <linux/kernel_stat.h>
27d9953105SMichael Ellerman #include <linux/signal.h>
28d9953105SMichael Ellerman #include <linux/sched.h>
29d9953105SMichael Ellerman #include <linux/ioport.h>
30d9953105SMichael Ellerman #include <linux/interrupt.h>
31d9953105SMichael Ellerman #include <linux/timex.h>
32d9953105SMichael Ellerman #include <linux/init.h>
33d9953105SMichael Ellerman #include <linux/delay.h>
34d9953105SMichael Ellerman #include <linux/irq.h>
35d9953105SMichael Ellerman #include <linux/random.h>
36d9953105SMichael Ellerman #include <linux/sysrq.h>
37d9953105SMichael Ellerman #include <linux/bitops.h>
38d9953105SMichael Ellerman 
39d9953105SMichael Ellerman #include <asm/uaccess.h>
40d9953105SMichael Ellerman #include <asm/system.h>
41d9953105SMichael Ellerman #include <asm/io.h>
42d9953105SMichael Ellerman #include <asm/pgtable.h>
43d9953105SMichael Ellerman #include <asm/irq.h>
44d9953105SMichael Ellerman #include <asm/cache.h>
45d9953105SMichael Ellerman #include <asm/prom.h>
46d9953105SMichael Ellerman #include <asm/ptrace.h>
47d9953105SMichael Ellerman #include <asm/machdep.h>
48d9953105SMichael Ellerman #include <asm/rtas.h>
49dcad47fcSDavid Gibson #include <asm/udbg.h>
508c4f1f29SMichael Ellerman #include <asm/firmware.h>
51d9953105SMichael Ellerman 
52577830b0SMichael Ellerman #include "pseries.h"
53c902be71SArnd Bergmann 
54d9953105SMichael Ellerman static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
55d9953105SMichael Ellerman static DEFINE_SPINLOCK(ras_log_buf_lock);
56d9953105SMichael Ellerman 
57d368514cSAnton Blanchard static char global_mce_data_buf[RTAS_ERROR_LOG_MAX];
58d368514cSAnton Blanchard static DEFINE_PER_CPU(__u64, mce_data_buf);
59d9953105SMichael Ellerman 
60d9953105SMichael Ellerman static int ras_get_sensor_state_token;
61d9953105SMichael Ellerman static int ras_check_exception_token;
62d9953105SMichael Ellerman 
63d9953105SMichael Ellerman #define EPOW_SENSOR_TOKEN	9
64d9953105SMichael Ellerman #define EPOW_SENSOR_INDEX	0
65d9953105SMichael Ellerman 
667d12e780SDavid Howells static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
677d12e780SDavid Howells static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
68d9953105SMichael Ellerman 
690ebfff14SBenjamin Herrenschmidt 
70d9953105SMichael Ellerman /*
71d9953105SMichael Ellerman  * Initialize handlers for the set of interrupts caused by hardware errors
72d9953105SMichael Ellerman  * and power system events.
73d9953105SMichael Ellerman  */
74d9953105SMichael Ellerman static int __init init_ras_IRQ(void)
75d9953105SMichael Ellerman {
76d9953105SMichael Ellerman 	struct device_node *np;
77d9953105SMichael Ellerman 
78d9953105SMichael Ellerman 	ras_get_sensor_state_token = rtas_token("get-sensor-state");
79d9953105SMichael Ellerman 	ras_check_exception_token = rtas_token("check-exception");
80d9953105SMichael Ellerman 
81d9953105SMichael Ellerman 	/* Internal Errors */
82d9953105SMichael Ellerman 	np = of_find_node_by_path("/event-sources/internal-errors");
83d9953105SMichael Ellerman 	if (np != NULL) {
8432c96f77SMark Nelson 		request_event_sources_irqs(np, ras_error_interrupt,
8532c96f77SMark Nelson 					   "RAS_ERROR");
86d9953105SMichael Ellerman 		of_node_put(np);
87d9953105SMichael Ellerman 	}
88d9953105SMichael Ellerman 
89d9953105SMichael Ellerman 	/* EPOW Events */
90d9953105SMichael Ellerman 	np = of_find_node_by_path("/event-sources/epow-events");
91d9953105SMichael Ellerman 	if (np != NULL) {
9232c96f77SMark Nelson 		request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
93d9953105SMichael Ellerman 		of_node_put(np);
94d9953105SMichael Ellerman 	}
95d9953105SMichael Ellerman 
9669ed3324SAnton Blanchard 	return 0;
97d9953105SMichael Ellerman }
98d9953105SMichael Ellerman __initcall(init_ras_IRQ);
99d9953105SMichael Ellerman 
100d9953105SMichael Ellerman /*
101d9953105SMichael Ellerman  * Handle power subsystem events (EPOW).
102d9953105SMichael Ellerman  *
103d9953105SMichael Ellerman  * Presently we just log the event has occurred.  This should be fixed
104d9953105SMichael Ellerman  * to examine the type of power failure and take appropriate action where
105d9953105SMichael Ellerman  * the time horizon permits something useful to be done.
106d9953105SMichael Ellerman  */
1077d12e780SDavid Howells static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
108d9953105SMichael Ellerman {
109d9953105SMichael Ellerman 	int status = 0xdeadbeef;
110d9953105SMichael Ellerman 	int state = 0;
111d9953105SMichael Ellerman 	int critical;
112d9953105SMichael Ellerman 
113d9953105SMichael Ellerman 	status = rtas_call(ras_get_sensor_state_token, 2, 2, &state,
114d9953105SMichael Ellerman 			   EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX);
115d9953105SMichael Ellerman 
116d9953105SMichael Ellerman 	if (state > 3)
117d9953105SMichael Ellerman 		critical = 1;  /* Time Critical */
118d9953105SMichael Ellerman 	else
119d9953105SMichael Ellerman 		critical = 0;
120d9953105SMichael Ellerman 
121d9953105SMichael Ellerman 	spin_lock(&ras_log_buf_lock);
122d9953105SMichael Ellerman 
123d9953105SMichael Ellerman 	status = rtas_call(ras_check_exception_token, 6, 1, NULL,
124b08e281bSMark Nelson 			   RTAS_VECTOR_EXTERNAL_INTERRUPT,
125476eb491SGrant Likely 			   virq_to_hw(irq),
126d9953105SMichael Ellerman 			   RTAS_EPOW_WARNING | RTAS_POWERMGM_EVENTS,
127d9953105SMichael Ellerman 			   critical, __pa(&ras_log_buf),
128d9953105SMichael Ellerman 				rtas_get_error_log_max());
129d9953105SMichael Ellerman 
130d9953105SMichael Ellerman 	udbg_printf("EPOW <0x%lx 0x%x 0x%x>\n",
131d9953105SMichael Ellerman 		    *((unsigned long *)&ras_log_buf), status, state);
132d9953105SMichael Ellerman 	printk(KERN_WARNING "EPOW <0x%lx 0x%x 0x%x>\n",
133d9953105SMichael Ellerman 	       *((unsigned long *)&ras_log_buf), status, state);
134d9953105SMichael Ellerman 
135d9953105SMichael Ellerman 	/* format and print the extended information */
136d9953105SMichael Ellerman 	log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
137d9953105SMichael Ellerman 
138d9953105SMichael Ellerman 	spin_unlock(&ras_log_buf_lock);
139d9953105SMichael Ellerman 	return IRQ_HANDLED;
140d9953105SMichael Ellerman }
141d9953105SMichael Ellerman 
142d9953105SMichael Ellerman /*
143d9953105SMichael Ellerman  * Handle hardware error interrupts.
144d9953105SMichael Ellerman  *
145d9953105SMichael Ellerman  * RTAS check-exception is called to collect data on the exception.  If
146d9953105SMichael Ellerman  * the error is deemed recoverable, we log a warning and return.
147d9953105SMichael Ellerman  * For nonrecoverable errors, an error is logged and we stop all processing
148d9953105SMichael Ellerman  * as quickly as possible in order to prevent propagation of the failure.
149d9953105SMichael Ellerman  */
1507d12e780SDavid Howells static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
151d9953105SMichael Ellerman {
152d9953105SMichael Ellerman 	struct rtas_error_log *rtas_elog;
153d9953105SMichael Ellerman 	int status = 0xdeadbeef;
154d9953105SMichael Ellerman 	int fatal;
155d9953105SMichael Ellerman 
156d9953105SMichael Ellerman 	spin_lock(&ras_log_buf_lock);
157d9953105SMichael Ellerman 
158d9953105SMichael Ellerman 	status = rtas_call(ras_check_exception_token, 6, 1, NULL,
159b08e281bSMark Nelson 			   RTAS_VECTOR_EXTERNAL_INTERRUPT,
160476eb491SGrant Likely 			   virq_to_hw(irq),
161d9953105SMichael Ellerman 			   RTAS_INTERNAL_ERROR, 1 /*Time Critical */,
162d9953105SMichael Ellerman 			   __pa(&ras_log_buf),
163d9953105SMichael Ellerman 				rtas_get_error_log_max());
164d9953105SMichael Ellerman 
165d9953105SMichael Ellerman 	rtas_elog = (struct rtas_error_log *)ras_log_buf;
166d9953105SMichael Ellerman 
167d9953105SMichael Ellerman 	if ((status == 0) && (rtas_elog->severity >= RTAS_SEVERITY_ERROR_SYNC))
168d9953105SMichael Ellerman 		fatal = 1;
169d9953105SMichael Ellerman 	else
170d9953105SMichael Ellerman 		fatal = 0;
171d9953105SMichael Ellerman 
172d9953105SMichael Ellerman 	/* format and print the extended information */
173d9953105SMichael Ellerman 	log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
174d9953105SMichael Ellerman 
175d9953105SMichael Ellerman 	if (fatal) {
176d9953105SMichael Ellerman 		udbg_printf("Fatal HW Error <0x%lx 0x%x>\n",
177d9953105SMichael Ellerman 			    *((unsigned long *)&ras_log_buf), status);
178d9953105SMichael Ellerman 		printk(KERN_EMERG "Error: Fatal hardware error <0x%lx 0x%x>\n",
179d9953105SMichael Ellerman 		       *((unsigned long *)&ras_log_buf), status);
180d9953105SMichael Ellerman 
18136f8a2c4SMichael Ellerman #ifndef DEBUG_RTAS_POWER_OFF
182d9953105SMichael Ellerman 		/* Don't actually power off when debugging so we can test
183d9953105SMichael Ellerman 		 * without actually failing while injecting errors.
184d9953105SMichael Ellerman 		 * Error data will not be logged to syslog.
185d9953105SMichael Ellerman 		 */
186d9953105SMichael Ellerman 		ppc_md.power_off();
187d9953105SMichael Ellerman #endif
188d9953105SMichael Ellerman 	} else {
189d9953105SMichael Ellerman 		udbg_printf("Recoverable HW Error <0x%lx 0x%x>\n",
190d9953105SMichael Ellerman 			    *((unsigned long *)&ras_log_buf), status);
191d9953105SMichael Ellerman 		printk(KERN_WARNING
192d9953105SMichael Ellerman 		       "Warning: Recoverable hardware error <0x%lx 0x%x>\n",
193d9953105SMichael Ellerman 		       *((unsigned long *)&ras_log_buf), status);
194d9953105SMichael Ellerman 	}
195d9953105SMichael Ellerman 
196d9953105SMichael Ellerman 	spin_unlock(&ras_log_buf_lock);
197d9953105SMichael Ellerman 	return IRQ_HANDLED;
198d9953105SMichael Ellerman }
199d9953105SMichael Ellerman 
200d368514cSAnton Blanchard /*
201d368514cSAnton Blanchard  * Some versions of FWNMI place the buffer inside the 4kB page starting at
202d368514cSAnton Blanchard  * 0x7000. Other versions place it inside the rtas buffer. We check both.
203d368514cSAnton Blanchard  */
204d368514cSAnton Blanchard #define VALID_FWNMI_BUFFER(A) \
205d368514cSAnton Blanchard 	((((A) >= 0x7000) && ((A) < 0x7ff0)) || \
206d368514cSAnton Blanchard 	(((A) >= rtas.base) && ((A) < (rtas.base + rtas.size - 16))))
207d368514cSAnton Blanchard 
208d368514cSAnton Blanchard /*
209d368514cSAnton Blanchard  * Get the error information for errors coming through the
210d9953105SMichael Ellerman  * FWNMI vectors.  The pt_regs' r3 will be updated to reflect
211d9953105SMichael Ellerman  * the actual r3 if possible, and a ptr to the error log entry
212d9953105SMichael Ellerman  * will be returned if found.
213d9953105SMichael Ellerman  *
214d368514cSAnton Blanchard  * If the RTAS error is not of the extended type, then we put it in a per
215d368514cSAnton Blanchard  * cpu 64bit buffer. If it is the extended type we use global_mce_data_buf.
216d368514cSAnton Blanchard  *
217d368514cSAnton Blanchard  * The global_mce_data_buf does not have any locks or protection around it,
218d9953105SMichael Ellerman  * if a second machine check comes in, or a system reset is done
219d9953105SMichael Ellerman  * before we have logged the error, then we will get corruption in the
220d9953105SMichael Ellerman  * error log.  This is preferable over holding off on calling
221d9953105SMichael Ellerman  * ibm,nmi-interlock which would result in us checkstopping if a
222d9953105SMichael Ellerman  * second machine check did come in.
223d9953105SMichael Ellerman  */
224d9953105SMichael Ellerman static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
225d9953105SMichael Ellerman {
226d9953105SMichael Ellerman 	unsigned long *savep;
227d368514cSAnton Blanchard 	struct rtas_error_log *h, *errhdr = NULL;
228d9953105SMichael Ellerman 
229d368514cSAnton Blanchard 	if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
230*f0e939aeSAnton Blanchard 		printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
231d368514cSAnton Blanchard 		return NULL;
232d9953105SMichael Ellerman 	}
233d368514cSAnton Blanchard 
234d368514cSAnton Blanchard 	savep = __va(regs->gpr[3]);
235d368514cSAnton Blanchard 	regs->gpr[3] = savep[0];	/* restore original r3 */
236d368514cSAnton Blanchard 
237d368514cSAnton Blanchard 	/* If it isn't an extended log we can use the per cpu 64bit buffer */
238d368514cSAnton Blanchard 	h = (struct rtas_error_log *)&savep[1];
239d368514cSAnton Blanchard 	if (!h->extended) {
240d368514cSAnton Blanchard 		memcpy(&__get_cpu_var(mce_data_buf), h, sizeof(__u64));
241d368514cSAnton Blanchard 		errhdr = (struct rtas_error_log *)&__get_cpu_var(mce_data_buf);
242d368514cSAnton Blanchard 	} else {
243d368514cSAnton Blanchard 		int len;
244d368514cSAnton Blanchard 
245d368514cSAnton Blanchard 		len = max_t(int, 8+h->extended_log_length, RTAS_ERROR_LOG_MAX);
246d368514cSAnton Blanchard 		memset(global_mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
247d368514cSAnton Blanchard 		memcpy(global_mce_data_buf, h, len);
248d368514cSAnton Blanchard 		errhdr = (struct rtas_error_log *)global_mce_data_buf;
249d368514cSAnton Blanchard 	}
250d368514cSAnton Blanchard 
251d9953105SMichael Ellerman 	return errhdr;
252d9953105SMichael Ellerman }
253d9953105SMichael Ellerman 
254d9953105SMichael Ellerman /* Call this when done with the data returned by FWNMI_get_errinfo.
255d9953105SMichael Ellerman  * It will release the saved data area for other CPUs in the
256d9953105SMichael Ellerman  * partition to receive FWNMI errors.
257d9953105SMichael Ellerman  */
258d9953105SMichael Ellerman static void fwnmi_release_errinfo(void)
259d9953105SMichael Ellerman {
260d9953105SMichael Ellerman 	int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
261d9953105SMichael Ellerman 	if (ret != 0)
262d368514cSAnton Blanchard 		printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
263d9953105SMichael Ellerman }
264d9953105SMichael Ellerman 
265c902be71SArnd Bergmann int pSeries_system_reset_exception(struct pt_regs *regs)
266d9953105SMichael Ellerman {
267d9953105SMichael Ellerman 	if (fwnmi_active) {
268d9953105SMichael Ellerman 		struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
269d9953105SMichael Ellerman 		if (errhdr) {
270d9953105SMichael Ellerman 			/* XXX Should look at FWNMI information */
271d9953105SMichael Ellerman 		}
272d9953105SMichael Ellerman 		fwnmi_release_errinfo();
273d9953105SMichael Ellerman 	}
274c902be71SArnd Bergmann 	return 0; /* need to perform reset */
275d9953105SMichael Ellerman }
276d9953105SMichael Ellerman 
277d9953105SMichael Ellerman /*
278d9953105SMichael Ellerman  * See if we can recover from a machine check exception.
279d9953105SMichael Ellerman  * This is only called on power4 (or above) and only via
280d9953105SMichael Ellerman  * the Firmware Non-Maskable Interrupts (fwnmi) handler
281d9953105SMichael Ellerman  * which provides the error analysis for us.
282d9953105SMichael Ellerman  *
283d9953105SMichael Ellerman  * Return 1 if corrected (or delivered a signal).
284d9953105SMichael Ellerman  * Return 0 if there is nothing we can do.
285d9953105SMichael Ellerman  */
286d9953105SMichael Ellerman static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
287d9953105SMichael Ellerman {
288d47d1d8aSAnton Blanchard 	int recovered = 0;
289d9953105SMichael Ellerman 
290d47d1d8aSAnton Blanchard 	if (!(regs->msr & MSR_RI)) {
291d47d1d8aSAnton Blanchard 		/* If MSR_RI isn't set, we cannot recover */
292d47d1d8aSAnton Blanchard 		recovered = 0;
293d47d1d8aSAnton Blanchard 
294d47d1d8aSAnton Blanchard 	} else if (err->disposition == RTAS_DISP_FULLY_RECOVERED) {
295d9953105SMichael Ellerman 		/* Platform corrected itself */
296d47d1d8aSAnton Blanchard 		recovered = 1;
297d47d1d8aSAnton Blanchard 
298d47d1d8aSAnton Blanchard 	} else if (err->disposition == RTAS_DISP_LIMITED_RECOVERY) {
299d47d1d8aSAnton Blanchard 		/* Platform corrected itself but could be degraded */
300d47d1d8aSAnton Blanchard 		printk(KERN_ERR "MCE: limited recovery, system may "
301d47d1d8aSAnton Blanchard 		       "be degraded\n");
302d47d1d8aSAnton Blanchard 		recovered = 1;
303d47d1d8aSAnton Blanchard 
304d47d1d8aSAnton Blanchard 	} else if (user_mode(regs) && !is_global_init(current) &&
305d47d1d8aSAnton Blanchard 		   err->severity == RTAS_SEVERITY_ERROR_SYNC) {
306d47d1d8aSAnton Blanchard 
307d47d1d8aSAnton Blanchard 		/*
308d47d1d8aSAnton Blanchard 		 * If we received a synchronous error when in userspace
309d47d1d8aSAnton Blanchard 		 * kill the task. Firmware may report details of the fail
310d47d1d8aSAnton Blanchard 		 * asynchronously, so we can't rely on the target and type
311d47d1d8aSAnton Blanchard 		 * fields being valid here.
312d47d1d8aSAnton Blanchard 		 */
313d47d1d8aSAnton Blanchard 		printk(KERN_ERR "MCE: uncorrectable error, killing task "
314d47d1d8aSAnton Blanchard 		       "%s:%d\n", current->comm, current->pid);
315d47d1d8aSAnton Blanchard 
316d47d1d8aSAnton Blanchard 		_exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
317d47d1d8aSAnton Blanchard 		recovered = 1;
318d9953105SMichael Ellerman 	}
319d9953105SMichael Ellerman 
3203f9793e6SAnton Blanchard 	log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
321d9953105SMichael Ellerman 
322d47d1d8aSAnton Blanchard 	return recovered;
323d9953105SMichael Ellerman }
324d9953105SMichael Ellerman 
325d9953105SMichael Ellerman /*
326d9953105SMichael Ellerman  * Handle a machine check.
327d9953105SMichael Ellerman  *
328d9953105SMichael Ellerman  * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
329d9953105SMichael Ellerman  * should be present.  If so the handler which called us tells us if the
330d9953105SMichael Ellerman  * error was recovered (never true if RI=0).
331d9953105SMichael Ellerman  *
332d9953105SMichael Ellerman  * On hardware prior to Power 4 these exceptions were asynchronous which
333d9953105SMichael Ellerman  * means we can't tell exactly where it occurred and so we can't recover.
334d9953105SMichael Ellerman  */
335d9953105SMichael Ellerman int pSeries_machine_check_exception(struct pt_regs *regs)
336d9953105SMichael Ellerman {
337d9953105SMichael Ellerman 	struct rtas_error_log *errp;
338d9953105SMichael Ellerman 
339d9953105SMichael Ellerman 	if (fwnmi_active) {
340d9953105SMichael Ellerman 		errp = fwnmi_get_errinfo(regs);
341d9953105SMichael Ellerman 		fwnmi_release_errinfo();
342d9953105SMichael Ellerman 		if (errp && recover_mce(regs, errp))
343d9953105SMichael Ellerman 			return 1;
344d9953105SMichael Ellerman 	}
345d9953105SMichael Ellerman 
346d9953105SMichael Ellerman 	return 0;
347d9953105SMichael Ellerman }
348