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