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