xref: /linux/arch/powerpc/platforms/pseries/ras.c (revision 587f83e8dd50d22bc0c62e32ec49fd319b0912fe)
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 #include <linux/fs.h>
39 #include <linux/reboot.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 #include "pseries.h"
55 
56 static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
57 static DEFINE_SPINLOCK(ras_log_buf_lock);
58 
59 static char global_mce_data_buf[RTAS_ERROR_LOG_MAX];
60 static DEFINE_PER_CPU(__u64, mce_data_buf);
61 
62 static int ras_check_exception_token;
63 
64 #define EPOW_SENSOR_TOKEN	9
65 #define EPOW_SENSOR_INDEX	0
66 
67 static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
68 static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
69 
70 
71 /*
72  * Initialize handlers for the set of interrupts caused by hardware errors
73  * and power system events.
74  */
75 static int __init init_ras_IRQ(void)
76 {
77 	struct device_node *np;
78 
79 	ras_check_exception_token = rtas_token("check-exception");
80 
81 	/* Internal Errors */
82 	np = of_find_node_by_path("/event-sources/internal-errors");
83 	if (np != NULL) {
84 		request_event_sources_irqs(np, ras_error_interrupt,
85 					   "RAS_ERROR");
86 		of_node_put(np);
87 	}
88 
89 	/* EPOW Events */
90 	np = of_find_node_by_path("/event-sources/epow-events");
91 	if (np != NULL) {
92 		request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
93 		of_node_put(np);
94 	}
95 
96 	return 0;
97 }
98 subsys_initcall(init_ras_IRQ);
99 
100 #define EPOW_SHUTDOWN_NORMAL				1
101 #define EPOW_SHUTDOWN_ON_UPS				2
102 #define EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS	3
103 #define EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH	4
104 
105 static void handle_system_shutdown(char event_modifier)
106 {
107 	switch (event_modifier) {
108 	case EPOW_SHUTDOWN_NORMAL:
109 		pr_emerg("Firmware initiated power off");
110 		orderly_poweroff(1);
111 		break;
112 
113 	case EPOW_SHUTDOWN_ON_UPS:
114 		pr_emerg("Loss of power reported by firmware, system is "
115 			"running on UPS/battery");
116 		break;
117 
118 	case EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS:
119 		pr_emerg("Loss of system critical functions reported by "
120 			"firmware");
121 		pr_emerg("Check RTAS error log for details");
122 		orderly_poweroff(1);
123 		break;
124 
125 	case EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH:
126 		pr_emerg("Ambient temperature too high reported by firmware");
127 		pr_emerg("Check RTAS error log for details");
128 		orderly_poweroff(1);
129 		break;
130 
131 	default:
132 		pr_err("Unknown power/cooling shutdown event (modifier %d)",
133 			event_modifier);
134 	}
135 }
136 
137 struct epow_errorlog {
138 	unsigned char sensor_value;
139 	unsigned char event_modifier;
140 	unsigned char extended_modifier;
141 	unsigned char reserved;
142 	unsigned char platform_reason;
143 };
144 
145 #define EPOW_RESET			0
146 #define EPOW_WARN_COOLING		1
147 #define EPOW_WARN_POWER			2
148 #define EPOW_SYSTEM_SHUTDOWN		3
149 #define EPOW_SYSTEM_HALT		4
150 #define EPOW_MAIN_ENCLOSURE		5
151 #define EPOW_POWER_OFF			7
152 
153 void rtas_parse_epow_errlog(struct rtas_error_log *log)
154 {
155 	struct pseries_errorlog *pseries_log;
156 	struct epow_errorlog *epow_log;
157 	char action_code;
158 	char modifier;
159 
160 	pseries_log = get_pseries_errorlog(log, PSERIES_ELOG_SECT_ID_EPOW);
161 	if (pseries_log == NULL)
162 		return;
163 
164 	epow_log = (struct epow_errorlog *)pseries_log->data;
165 	action_code = epow_log->sensor_value & 0xF;	/* bottom 4 bits */
166 	modifier = epow_log->event_modifier & 0xF;	/* bottom 4 bits */
167 
168 	switch (action_code) {
169 	case EPOW_RESET:
170 		pr_err("Non critical power or cooling issue cleared");
171 		break;
172 
173 	case EPOW_WARN_COOLING:
174 		pr_err("Non critical cooling issue reported by firmware");
175 		pr_err("Check RTAS error log for details");
176 		break;
177 
178 	case EPOW_WARN_POWER:
179 		pr_err("Non critical power issue reported by firmware");
180 		pr_err("Check RTAS error log for details");
181 		break;
182 
183 	case EPOW_SYSTEM_SHUTDOWN:
184 		handle_system_shutdown(epow_log->event_modifier);
185 		break;
186 
187 	case EPOW_SYSTEM_HALT:
188 		pr_emerg("Firmware initiated power off");
189 		orderly_poweroff(1);
190 		break;
191 
192 	case EPOW_MAIN_ENCLOSURE:
193 	case EPOW_POWER_OFF:
194 		pr_emerg("Critical power/cooling issue reported by firmware");
195 		pr_emerg("Check RTAS error log for details");
196 		pr_emerg("Immediate power off");
197 		emergency_sync();
198 		kernel_power_off();
199 		break;
200 
201 	default:
202 		pr_err("Unknown power/cooling event (action code %d)",
203 			action_code);
204 	}
205 }
206 
207 /* Handle environmental and power warning (EPOW) interrupts. */
208 static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
209 {
210 	int status;
211 	int state;
212 	int critical;
213 
214 	status = rtas_get_sensor(EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX, &state);
215 
216 	if (state > 3)
217 		critical = 1;		/* Time Critical */
218 	else
219 		critical = 0;
220 
221 	spin_lock(&ras_log_buf_lock);
222 
223 	status = rtas_call(ras_check_exception_token, 6, 1, NULL,
224 			   RTAS_VECTOR_EXTERNAL_INTERRUPT,
225 			   virq_to_hw(irq),
226 			   RTAS_EPOW_WARNING | RTAS_POWERMGM_EVENTS,
227 			   critical, __pa(&ras_log_buf),
228 				rtas_get_error_log_max());
229 
230 	log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
231 
232 	rtas_parse_epow_errlog((struct rtas_error_log *)ras_log_buf);
233 
234 	spin_unlock(&ras_log_buf_lock);
235 	return IRQ_HANDLED;
236 }
237 
238 /*
239  * Handle hardware error interrupts.
240  *
241  * RTAS check-exception is called to collect data on the exception.  If
242  * the error is deemed recoverable, we log a warning and return.
243  * For nonrecoverable errors, an error is logged and we stop all processing
244  * as quickly as possible in order to prevent propagation of the failure.
245  */
246 static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
247 {
248 	struct rtas_error_log *rtas_elog;
249 	int status = 0xdeadbeef;
250 	int fatal;
251 
252 	spin_lock(&ras_log_buf_lock);
253 
254 	status = rtas_call(ras_check_exception_token, 6, 1, NULL,
255 			   RTAS_VECTOR_EXTERNAL_INTERRUPT,
256 			   virq_to_hw(irq),
257 			   RTAS_INTERNAL_ERROR, 1 /*Time Critical */,
258 			   __pa(&ras_log_buf),
259 				rtas_get_error_log_max());
260 
261 	rtas_elog = (struct rtas_error_log *)ras_log_buf;
262 
263 	if ((status == 0) && (rtas_elog->severity >= RTAS_SEVERITY_ERROR_SYNC))
264 		fatal = 1;
265 	else
266 		fatal = 0;
267 
268 	/* format and print the extended information */
269 	log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
270 
271 	if (fatal) {
272 		udbg_printf("Fatal HW Error <0x%lx 0x%x>\n",
273 			    *((unsigned long *)&ras_log_buf), status);
274 		printk(KERN_EMERG "Error: Fatal hardware error <0x%lx 0x%x>\n",
275 		       *((unsigned long *)&ras_log_buf), status);
276 
277 #ifndef DEBUG_RTAS_POWER_OFF
278 		/* Don't actually power off when debugging so we can test
279 		 * without actually failing while injecting errors.
280 		 * Error data will not be logged to syslog.
281 		 */
282 		ppc_md.power_off();
283 #endif
284 	} else {
285 		udbg_printf("Recoverable HW Error <0x%lx 0x%x>\n",
286 			    *((unsigned long *)&ras_log_buf), status);
287 		printk(KERN_WARNING
288 		       "Warning: Recoverable hardware error <0x%lx 0x%x>\n",
289 		       *((unsigned long *)&ras_log_buf), status);
290 	}
291 
292 	spin_unlock(&ras_log_buf_lock);
293 	return IRQ_HANDLED;
294 }
295 
296 /*
297  * Some versions of FWNMI place the buffer inside the 4kB page starting at
298  * 0x7000. Other versions place it inside the rtas buffer. We check both.
299  */
300 #define VALID_FWNMI_BUFFER(A) \
301 	((((A) >= 0x7000) && ((A) < 0x7ff0)) || \
302 	(((A) >= rtas.base) && ((A) < (rtas.base + rtas.size - 16))))
303 
304 /*
305  * Get the error information for errors coming through the
306  * FWNMI vectors.  The pt_regs' r3 will be updated to reflect
307  * the actual r3 if possible, and a ptr to the error log entry
308  * will be returned if found.
309  *
310  * If the RTAS error is not of the extended type, then we put it in a per
311  * cpu 64bit buffer. If it is the extended type we use global_mce_data_buf.
312  *
313  * The global_mce_data_buf does not have any locks or protection around it,
314  * if a second machine check comes in, or a system reset is done
315  * before we have logged the error, then we will get corruption in the
316  * error log.  This is preferable over holding off on calling
317  * ibm,nmi-interlock which would result in us checkstopping if a
318  * second machine check did come in.
319  */
320 static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
321 {
322 	unsigned long *savep;
323 	struct rtas_error_log *h, *errhdr = NULL;
324 
325 	if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
326 		printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
327 		return NULL;
328 	}
329 
330 	savep = __va(regs->gpr[3]);
331 	regs->gpr[3] = savep[0];	/* restore original r3 */
332 
333 	/* If it isn't an extended log we can use the per cpu 64bit buffer */
334 	h = (struct rtas_error_log *)&savep[1];
335 	if (!h->extended) {
336 		memcpy(&__get_cpu_var(mce_data_buf), h, sizeof(__u64));
337 		errhdr = (struct rtas_error_log *)&__get_cpu_var(mce_data_buf);
338 	} else {
339 		int len;
340 
341 		len = max_t(int, 8+h->extended_log_length, RTAS_ERROR_LOG_MAX);
342 		memset(global_mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
343 		memcpy(global_mce_data_buf, h, len);
344 		errhdr = (struct rtas_error_log *)global_mce_data_buf;
345 	}
346 
347 	return errhdr;
348 }
349 
350 /* Call this when done with the data returned by FWNMI_get_errinfo.
351  * It will release the saved data area for other CPUs in the
352  * partition to receive FWNMI errors.
353  */
354 static void fwnmi_release_errinfo(void)
355 {
356 	int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
357 	if (ret != 0)
358 		printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
359 }
360 
361 int pSeries_system_reset_exception(struct pt_regs *regs)
362 {
363 	if (fwnmi_active) {
364 		struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
365 		if (errhdr) {
366 			/* XXX Should look at FWNMI information */
367 		}
368 		fwnmi_release_errinfo();
369 	}
370 	return 0; /* need to perform reset */
371 }
372 
373 /*
374  * See if we can recover from a machine check exception.
375  * This is only called on power4 (or above) and only via
376  * the Firmware Non-Maskable Interrupts (fwnmi) handler
377  * which provides the error analysis for us.
378  *
379  * Return 1 if corrected (or delivered a signal).
380  * Return 0 if there is nothing we can do.
381  */
382 static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
383 {
384 	int recovered = 0;
385 
386 	if (!(regs->msr & MSR_RI)) {
387 		/* If MSR_RI isn't set, we cannot recover */
388 		recovered = 0;
389 
390 	} else if (err->disposition == RTAS_DISP_FULLY_RECOVERED) {
391 		/* Platform corrected itself */
392 		recovered = 1;
393 
394 	} else if (err->disposition == RTAS_DISP_LIMITED_RECOVERY) {
395 		/* Platform corrected itself but could be degraded */
396 		printk(KERN_ERR "MCE: limited recovery, system may "
397 		       "be degraded\n");
398 		recovered = 1;
399 
400 	} else if (user_mode(regs) && !is_global_init(current) &&
401 		   err->severity == RTAS_SEVERITY_ERROR_SYNC) {
402 
403 		/*
404 		 * If we received a synchronous error when in userspace
405 		 * kill the task. Firmware may report details of the fail
406 		 * asynchronously, so we can't rely on the target and type
407 		 * fields being valid here.
408 		 */
409 		printk(KERN_ERR "MCE: uncorrectable error, killing task "
410 		       "%s:%d\n", current->comm, current->pid);
411 
412 		_exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
413 		recovered = 1;
414 	}
415 
416 	log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
417 
418 	return recovered;
419 }
420 
421 /*
422  * Handle a machine check.
423  *
424  * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
425  * should be present.  If so the handler which called us tells us if the
426  * error was recovered (never true if RI=0).
427  *
428  * On hardware prior to Power 4 these exceptions were asynchronous which
429  * means we can't tell exactly where it occurred and so we can't recover.
430  */
431 int pSeries_machine_check_exception(struct pt_regs *regs)
432 {
433 	struct rtas_error_log *errp;
434 
435 	if (fwnmi_active) {
436 		errp = fwnmi_get_errinfo(regs);
437 		fwnmi_release_errinfo();
438 		if (errp && recover_mce(regs, errp))
439 			return 1;
440 	}
441 
442 	return 0;
443 }
444