xref: /linux/arch/powerpc/platforms/pseries/ras.c (revision cc8b526366e8bed9a950288316e0ab03bef4420a)
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,
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;
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 		pr_emerg("Fatal hardware error reported by firmware");
273 		pr_emerg("Check RTAS error log for details");
274 		pr_emerg("Immediate power off");
275 		emergency_sync();
276 		kernel_power_off();
277 	} else {
278 		pr_err("Recoverable hardware error reported by firmware");
279 	}
280 
281 	spin_unlock(&ras_log_buf_lock);
282 	return IRQ_HANDLED;
283 }
284 
285 /*
286  * Some versions of FWNMI place the buffer inside the 4kB page starting at
287  * 0x7000. Other versions place it inside the rtas buffer. We check both.
288  */
289 #define VALID_FWNMI_BUFFER(A) \
290 	((((A) >= 0x7000) && ((A) < 0x7ff0)) || \
291 	(((A) >= rtas.base) && ((A) < (rtas.base + rtas.size - 16))))
292 
293 /*
294  * Get the error information for errors coming through the
295  * FWNMI vectors.  The pt_regs' r3 will be updated to reflect
296  * the actual r3 if possible, and a ptr to the error log entry
297  * will be returned if found.
298  *
299  * If the RTAS error is not of the extended type, then we put it in a per
300  * cpu 64bit buffer. If it is the extended type we use global_mce_data_buf.
301  *
302  * The global_mce_data_buf does not have any locks or protection around it,
303  * if a second machine check comes in, or a system reset is done
304  * before we have logged the error, then we will get corruption in the
305  * error log.  This is preferable over holding off on calling
306  * ibm,nmi-interlock which would result in us checkstopping if a
307  * second machine check did come in.
308  */
309 static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
310 {
311 	unsigned long *savep;
312 	struct rtas_error_log *h, *errhdr = NULL;
313 
314 	if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
315 		printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
316 		return NULL;
317 	}
318 
319 	savep = __va(regs->gpr[3]);
320 	regs->gpr[3] = savep[0];	/* restore original r3 */
321 
322 	/* If it isn't an extended log we can use the per cpu 64bit buffer */
323 	h = (struct rtas_error_log *)&savep[1];
324 	if (!h->extended) {
325 		memcpy(&__get_cpu_var(mce_data_buf), h, sizeof(__u64));
326 		errhdr = (struct rtas_error_log *)&__get_cpu_var(mce_data_buf);
327 	} else {
328 		int len;
329 
330 		len = max_t(int, 8+h->extended_log_length, RTAS_ERROR_LOG_MAX);
331 		memset(global_mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
332 		memcpy(global_mce_data_buf, h, len);
333 		errhdr = (struct rtas_error_log *)global_mce_data_buf;
334 	}
335 
336 	return errhdr;
337 }
338 
339 /* Call this when done with the data returned by FWNMI_get_errinfo.
340  * It will release the saved data area for other CPUs in the
341  * partition to receive FWNMI errors.
342  */
343 static void fwnmi_release_errinfo(void)
344 {
345 	int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
346 	if (ret != 0)
347 		printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
348 }
349 
350 int pSeries_system_reset_exception(struct pt_regs *regs)
351 {
352 	if (fwnmi_active) {
353 		struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
354 		if (errhdr) {
355 			/* XXX Should look at FWNMI information */
356 		}
357 		fwnmi_release_errinfo();
358 	}
359 	return 0; /* need to perform reset */
360 }
361 
362 /*
363  * See if we can recover from a machine check exception.
364  * This is only called on power4 (or above) and only via
365  * the Firmware Non-Maskable Interrupts (fwnmi) handler
366  * which provides the error analysis for us.
367  *
368  * Return 1 if corrected (or delivered a signal).
369  * Return 0 if there is nothing we can do.
370  */
371 static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
372 {
373 	int recovered = 0;
374 
375 	if (!(regs->msr & MSR_RI)) {
376 		/* If MSR_RI isn't set, we cannot recover */
377 		recovered = 0;
378 
379 	} else if (err->disposition == RTAS_DISP_FULLY_RECOVERED) {
380 		/* Platform corrected itself */
381 		recovered = 1;
382 
383 	} else if (err->disposition == RTAS_DISP_LIMITED_RECOVERY) {
384 		/* Platform corrected itself but could be degraded */
385 		printk(KERN_ERR "MCE: limited recovery, system may "
386 		       "be degraded\n");
387 		recovered = 1;
388 
389 	} else if (user_mode(regs) && !is_global_init(current) &&
390 		   err->severity == RTAS_SEVERITY_ERROR_SYNC) {
391 
392 		/*
393 		 * If we received a synchronous error when in userspace
394 		 * kill the task. Firmware may report details of the fail
395 		 * asynchronously, so we can't rely on the target and type
396 		 * fields being valid here.
397 		 */
398 		printk(KERN_ERR "MCE: uncorrectable error, killing task "
399 		       "%s:%d\n", current->comm, current->pid);
400 
401 		_exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
402 		recovered = 1;
403 	}
404 
405 	log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
406 
407 	return recovered;
408 }
409 
410 /*
411  * Handle a machine check.
412  *
413  * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
414  * should be present.  If so the handler which called us tells us if the
415  * error was recovered (never true if RI=0).
416  *
417  * On hardware prior to Power 4 these exceptions were asynchronous which
418  * means we can't tell exactly where it occurred and so we can't recover.
419  */
420 int pSeries_machine_check_exception(struct pt_regs *regs)
421 {
422 	struct rtas_error_log *errp;
423 
424 	if (fwnmi_active) {
425 		errp = fwnmi_get_errinfo(regs);
426 		fwnmi_release_errinfo();
427 		if (errp && recover_mce(regs, errp))
428 			return 1;
429 	}
430 
431 	return 0;
432 }
433