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