xref: /linux/arch/powerpc/platforms/pseries/ras.c (revision ca7e0c612005937a4a5a75d3fed90459993de65c)
1 /*
2  * Copyright (C) 2001 Dave Engebretsen IBM Corporation
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 
19 /* Change Activity:
20  * 2001/09/21 : engebret : Created with minimal EPOW and HW exception support.
21  * End Change Activity
22  */
23 
24 #include <linux/errno.h>
25 #include <linux/threads.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/signal.h>
28 #include <linux/sched.h>
29 #include <linux/ioport.h>
30 #include <linux/interrupt.h>
31 #include <linux/timex.h>
32 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/irq.h>
35 #include <linux/random.h>
36 #include <linux/sysrq.h>
37 #include <linux/bitops.h>
38 
39 #include <asm/uaccess.h>
40 #include <asm/system.h>
41 #include <asm/io.h>
42 #include <asm/pgtable.h>
43 #include <asm/irq.h>
44 #include <asm/cache.h>
45 #include <asm/prom.h>
46 #include <asm/ptrace.h>
47 #include <asm/machdep.h>
48 #include <asm/rtas.h>
49 #include <asm/udbg.h>
50 #include <asm/firmware.h>
51 
52 #include "pseries.h"
53 
54 static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
55 static DEFINE_SPINLOCK(ras_log_buf_lock);
56 
57 static char mce_data_buf[RTAS_ERROR_LOG_MAX];
58 
59 static int ras_get_sensor_state_token;
60 static int ras_check_exception_token;
61 
62 #define EPOW_SENSOR_TOKEN	9
63 #define EPOW_SENSOR_INDEX	0
64 #define RAS_VECTOR_OFFSET	0x500
65 
66 static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
67 static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
68 
69 
70 static void request_ras_irqs(struct device_node *np,
71 			irq_handler_t handler,
72 			const char *name)
73 {
74 	int i, index, count = 0;
75 	struct of_irq oirq;
76 	const u32 *opicprop;
77 	unsigned int opicplen;
78 	unsigned int virqs[16];
79 
80 	/* Check for obsolete "open-pic-interrupt" property. If present, then
81 	 * map those interrupts using the default interrupt host and default
82 	 * trigger
83 	 */
84 	opicprop = of_get_property(np, "open-pic-interrupt", &opicplen);
85 	if (opicprop) {
86 		opicplen /= sizeof(u32);
87 		for (i = 0; i < opicplen; i++) {
88 			if (count > 15)
89 				break;
90 			virqs[count] = irq_create_mapping(NULL, *(opicprop++));
91 			if (virqs[count] == NO_IRQ)
92 				printk(KERN_ERR "Unable to allocate interrupt "
93 				       "number for %s\n", np->full_name);
94 			else
95 				count++;
96 
97 		}
98 	}
99 	/* Else use normal interrupt tree parsing */
100 	else {
101 		/* First try to do a proper OF tree parsing */
102 		for (index = 0; of_irq_map_one(np, index, &oirq) == 0;
103 		     index++) {
104 			if (count > 15)
105 				break;
106 			virqs[count] = irq_create_of_mapping(oirq.controller,
107 							    oirq.specifier,
108 							    oirq.size);
109 			if (virqs[count] == NO_IRQ)
110 				printk(KERN_ERR "Unable to allocate interrupt "
111 				       "number for %s\n", np->full_name);
112 			else
113 				count++;
114 		}
115 	}
116 
117 	/* Now request them */
118 	for (i = 0; i < count; i++) {
119 		if (request_irq(virqs[i], handler, 0, name, NULL)) {
120 			printk(KERN_ERR "Unable to request interrupt %d for "
121 			       "%s\n", virqs[i], np->full_name);
122 			return;
123 		}
124 	}
125 }
126 
127 /*
128  * Initialize handlers for the set of interrupts caused by hardware errors
129  * and power system events.
130  */
131 static int __init init_ras_IRQ(void)
132 {
133 	struct device_node *np;
134 
135 	ras_get_sensor_state_token = rtas_token("get-sensor-state");
136 	ras_check_exception_token = rtas_token("check-exception");
137 
138 	/* Internal Errors */
139 	np = of_find_node_by_path("/event-sources/internal-errors");
140 	if (np != NULL) {
141 		request_ras_irqs(np, ras_error_interrupt, "RAS_ERROR");
142 		of_node_put(np);
143 	}
144 
145 	/* EPOW Events */
146 	np = of_find_node_by_path("/event-sources/epow-events");
147 	if (np != NULL) {
148 		request_ras_irqs(np, ras_epow_interrupt, "RAS_EPOW");
149 		of_node_put(np);
150 	}
151 
152 	return 0;
153 }
154 __initcall(init_ras_IRQ);
155 
156 /*
157  * Handle power subsystem events (EPOW).
158  *
159  * Presently we just log the event has occurred.  This should be fixed
160  * to examine the type of power failure and take appropriate action where
161  * the time horizon permits something useful to be done.
162  */
163 static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
164 {
165 	int status = 0xdeadbeef;
166 	int state = 0;
167 	int critical;
168 
169 	status = rtas_call(ras_get_sensor_state_token, 2, 2, &state,
170 			   EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX);
171 
172 	if (state > 3)
173 		critical = 1;  /* Time Critical */
174 	else
175 		critical = 0;
176 
177 	spin_lock(&ras_log_buf_lock);
178 
179 	status = rtas_call(ras_check_exception_token, 6, 1, NULL,
180 			   RAS_VECTOR_OFFSET,
181 			   irq_map[irq].hwirq,
182 			   RTAS_EPOW_WARNING | RTAS_POWERMGM_EVENTS,
183 			   critical, __pa(&ras_log_buf),
184 				rtas_get_error_log_max());
185 
186 	udbg_printf("EPOW <0x%lx 0x%x 0x%x>\n",
187 		    *((unsigned long *)&ras_log_buf), status, state);
188 	printk(KERN_WARNING "EPOW <0x%lx 0x%x 0x%x>\n",
189 	       *((unsigned long *)&ras_log_buf), status, state);
190 
191 	/* format and print the extended information */
192 	log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
193 
194 	spin_unlock(&ras_log_buf_lock);
195 	return IRQ_HANDLED;
196 }
197 
198 /*
199  * Handle hardware error interrupts.
200  *
201  * RTAS check-exception is called to collect data on the exception.  If
202  * the error is deemed recoverable, we log a warning and return.
203  * For nonrecoverable errors, an error is logged and we stop all processing
204  * as quickly as possible in order to prevent propagation of the failure.
205  */
206 static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
207 {
208 	struct rtas_error_log *rtas_elog;
209 	int status = 0xdeadbeef;
210 	int fatal;
211 
212 	spin_lock(&ras_log_buf_lock);
213 
214 	status = rtas_call(ras_check_exception_token, 6, 1, NULL,
215 			   RAS_VECTOR_OFFSET,
216 			   irq_map[irq].hwirq,
217 			   RTAS_INTERNAL_ERROR, 1 /*Time Critical */,
218 			   __pa(&ras_log_buf),
219 				rtas_get_error_log_max());
220 
221 	rtas_elog = (struct rtas_error_log *)ras_log_buf;
222 
223 	if ((status == 0) && (rtas_elog->severity >= RTAS_SEVERITY_ERROR_SYNC))
224 		fatal = 1;
225 	else
226 		fatal = 0;
227 
228 	/* format and print the extended information */
229 	log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
230 
231 	if (fatal) {
232 		udbg_printf("Fatal HW Error <0x%lx 0x%x>\n",
233 			    *((unsigned long *)&ras_log_buf), status);
234 		printk(KERN_EMERG "Error: Fatal hardware error <0x%lx 0x%x>\n",
235 		       *((unsigned long *)&ras_log_buf), status);
236 
237 #ifndef DEBUG_RTAS_POWER_OFF
238 		/* Don't actually power off when debugging so we can test
239 		 * without actually failing while injecting errors.
240 		 * Error data will not be logged to syslog.
241 		 */
242 		ppc_md.power_off();
243 #endif
244 	} else {
245 		udbg_printf("Recoverable HW Error <0x%lx 0x%x>\n",
246 			    *((unsigned long *)&ras_log_buf), status);
247 		printk(KERN_WARNING
248 		       "Warning: Recoverable hardware error <0x%lx 0x%x>\n",
249 		       *((unsigned long *)&ras_log_buf), status);
250 	}
251 
252 	spin_unlock(&ras_log_buf_lock);
253 	return IRQ_HANDLED;
254 }
255 
256 /* Get the error information for errors coming through the
257  * FWNMI vectors.  The pt_regs' r3 will be updated to reflect
258  * the actual r3 if possible, and a ptr to the error log entry
259  * will be returned if found.
260  *
261  * The mce_data_buf does not have any locks or protection around it,
262  * if a second machine check comes in, or a system reset is done
263  * before we have logged the error, then we will get corruption in the
264  * error log.  This is preferable over holding off on calling
265  * ibm,nmi-interlock which would result in us checkstopping if a
266  * second machine check did come in.
267  */
268 static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
269 {
270 	unsigned long errdata = regs->gpr[3];
271 	struct rtas_error_log *errhdr = NULL;
272 	unsigned long *savep;
273 
274 	if ((errdata >= 0x7000 && errdata < 0x7fff0) ||
275 	    (errdata >= rtas.base && errdata < rtas.base + rtas.size - 16)) {
276 		savep = __va(errdata);
277 		regs->gpr[3] = savep[0];	/* restore original r3 */
278 		memset(mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
279 		memcpy(mce_data_buf, (char *)(savep + 1), RTAS_ERROR_LOG_MAX);
280 		errhdr = (struct rtas_error_log *)mce_data_buf;
281 	} else {
282 		printk("FWNMI: corrupt r3\n");
283 	}
284 	return errhdr;
285 }
286 
287 /* Call this when done with the data returned by FWNMI_get_errinfo.
288  * It will release the saved data area for other CPUs in the
289  * partition to receive FWNMI errors.
290  */
291 static void fwnmi_release_errinfo(void)
292 {
293 	int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
294 	if (ret != 0)
295 		printk("FWNMI: nmi-interlock failed: %d\n", ret);
296 }
297 
298 int pSeries_system_reset_exception(struct pt_regs *regs)
299 {
300 	if (fwnmi_active) {
301 		struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
302 		if (errhdr) {
303 			/* XXX Should look at FWNMI information */
304 		}
305 		fwnmi_release_errinfo();
306 	}
307 	return 0; /* need to perform reset */
308 }
309 
310 /*
311  * See if we can recover from a machine check exception.
312  * This is only called on power4 (or above) and only via
313  * the Firmware Non-Maskable Interrupts (fwnmi) handler
314  * which provides the error analysis for us.
315  *
316  * Return 1 if corrected (or delivered a signal).
317  * Return 0 if there is nothing we can do.
318  */
319 static int recover_mce(struct pt_regs *regs, struct rtas_error_log * err)
320 {
321 	int nonfatal = 0;
322 
323 	if (err->disposition == RTAS_DISP_FULLY_RECOVERED) {
324 		/* Platform corrected itself */
325 		nonfatal = 1;
326 	} else if ((regs->msr & MSR_RI) &&
327 		   user_mode(regs) &&
328 		   err->severity == RTAS_SEVERITY_ERROR_SYNC &&
329 		   err->disposition == RTAS_DISP_NOT_RECOVERED &&
330 		   err->target == RTAS_TARGET_MEMORY &&
331 		   err->type == RTAS_TYPE_ECC_UNCORR &&
332 		   !(current->pid == 0 || is_global_init(current))) {
333 		/* Kill off a user process with an ECC error */
334 		printk(KERN_ERR "MCE: uncorrectable ecc error for pid %d\n",
335 		       current->pid);
336 		/* XXX something better for ECC error? */
337 		_exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
338 		nonfatal = 1;
339 	}
340 
341 	log_error((char *)err, ERR_TYPE_RTAS_LOG, !nonfatal);
342 
343 	return nonfatal;
344 }
345 
346 /*
347  * Handle a machine check.
348  *
349  * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
350  * should be present.  If so the handler which called us tells us if the
351  * error was recovered (never true if RI=0).
352  *
353  * On hardware prior to Power 4 these exceptions were asynchronous which
354  * means we can't tell exactly where it occurred and so we can't recover.
355  */
356 int pSeries_machine_check_exception(struct pt_regs *regs)
357 {
358 	struct rtas_error_log *errp;
359 
360 	if (fwnmi_active) {
361 		errp = fwnmi_get_errinfo(regs);
362 		fwnmi_release_errinfo();
363 		if (errp && recover_mce(regs, errp))
364 			return 1;
365 	}
366 
367 	return 0;
368 }
369