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