blob: 4246d7b9dce95ce8902eaf5d0595ef43e7a9cf55 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2001 Dave Engebretsen IBM Corporation
Michael Ellermand9953102005-10-24 15:07:30 +10003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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.
Michael Ellermand9953102005-10-24 15:07:30 +10008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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.
Michael Ellermand9953102005-10-24 15:07:30 +100013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 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.
Michael Ellermand9953102005-10-24 15:07:30 +100021 * End Change Activity
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/irq.h>
35#include <linux/random.h>
36#include <linux/sysrq.h>
37#include <linux/bitops.h>
Anton Blanchard55fc0c52012-03-21 15:49:59 +000038#include <linux/fs.h>
39#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/machdep.h>
50#include <asm/rtas.h>
David Gibsondcad47f2005-11-07 09:49:43 +110051#include <asm/udbg.h>
Michael Ellerman8c4f1f22005-12-04 18:39:33 +110052#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Michael Ellerman577830b2007-02-08 18:33:51 +110054#include "pseries.h"
Arnd Bergmannc902be72006-01-04 19:55:53 +000055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
57static DEFINE_SPINLOCK(ras_log_buf_lock);
58
Anton Blanchardd3685142011-01-11 19:50:51 +000059static char global_mce_data_buf[RTAS_ERROR_LOG_MAX];
60static DEFINE_PER_CPU(__u64, mce_data_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int ras_get_sensor_state_token;
63static int ras_check_exception_token;
64
65#define EPOW_SENSOR_TOKEN 9
66#define EPOW_SENSOR_INDEX 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
David Howells7d12e782006-10-05 14:55:46 +010068static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
69static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
73 * Initialize handlers for the set of interrupts caused by hardware errors
74 * and power system events.
75 */
76static int __init init_ras_IRQ(void)
77{
78 struct device_node *np;
79
80 ras_get_sensor_state_token = rtas_token("get-sensor-state");
81 ras_check_exception_token = rtas_token("check-exception");
82
83 /* Internal Errors */
84 np = of_find_node_by_path("/event-sources/internal-errors");
85 if (np != NULL) {
Mark Nelson32c96f72010-05-18 22:51:00 +000086 request_event_sources_irqs(np, ras_error_interrupt,
87 "RAS_ERROR");
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 of_node_put(np);
89 }
90
91 /* EPOW Events */
92 np = of_find_node_by_path("/event-sources/epow-events");
93 if (np != NULL) {
Mark Nelson32c96f72010-05-18 22:51:00 +000094 request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 of_node_put(np);
96 }
97
Anton Blanchard69ed3322006-03-28 14:08:39 +110098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
Anton Blanchard55fc0c52012-03-21 15:49:59 +0000100subsys_initcall(init_ras_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Anton Blanchard55fc0c52012-03-21 15:49:59 +0000102#define EPOW_SHUTDOWN_NORMAL 1
103#define EPOW_SHUTDOWN_ON_UPS 2
104#define EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS 3
105#define EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH 4
106
107static void handle_system_shutdown(char event_modifier)
108{
109 switch (event_modifier) {
110 case EPOW_SHUTDOWN_NORMAL:
111 pr_emerg("Firmware initiated power off");
112 orderly_poweroff(1);
113 break;
114
115 case EPOW_SHUTDOWN_ON_UPS:
116 pr_emerg("Loss of power reported by firmware, system is "
117 "running on UPS/battery");
118 break;
119
120 case EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS:
121 pr_emerg("Loss of system critical functions reported by "
122 "firmware");
123 pr_emerg("Check RTAS error log for details");
124 orderly_poweroff(1);
125 break;
126
127 case EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH:
128 pr_emerg("Ambient temperature too high reported by firmware");
129 pr_emerg("Check RTAS error log for details");
130 orderly_poweroff(1);
131 break;
132
133 default:
134 pr_err("Unknown power/cooling shutdown event (modifier %d)",
135 event_modifier);
136 }
137}
138
139struct epow_errorlog {
140 unsigned char sensor_value;
141 unsigned char event_modifier;
142 unsigned char extended_modifier;
143 unsigned char reserved;
144 unsigned char platform_reason;
145};
146
147#define EPOW_RESET 0
148#define EPOW_WARN_COOLING 1
149#define EPOW_WARN_POWER 2
150#define EPOW_SYSTEM_SHUTDOWN 3
151#define EPOW_SYSTEM_HALT 4
152#define EPOW_MAIN_ENCLOSURE 5
153#define EPOW_POWER_OFF 7
154
155void rtas_parse_epow_errlog(struct rtas_error_log *log)
156{
157 struct pseries_errorlog *pseries_log;
158 struct epow_errorlog *epow_log;
159 char action_code;
160 char modifier;
161
162 pseries_log = get_pseries_errorlog(log, PSERIES_ELOG_SECT_ID_EPOW);
163 if (pseries_log == NULL)
164 return;
165
166 epow_log = (struct epow_errorlog *)pseries_log->data;
167 action_code = epow_log->sensor_value & 0xF; /* bottom 4 bits */
168 modifier = epow_log->event_modifier & 0xF; /* bottom 4 bits */
169
170 switch (action_code) {
171 case EPOW_RESET:
172 pr_err("Non critical power or cooling issue cleared");
173 break;
174
175 case EPOW_WARN_COOLING:
176 pr_err("Non critical cooling issue reported by firmware");
177 pr_err("Check RTAS error log for details");
178 break;
179
180 case EPOW_WARN_POWER:
181 pr_err("Non critical power issue reported by firmware");
182 pr_err("Check RTAS error log for details");
183 break;
184
185 case EPOW_SYSTEM_SHUTDOWN:
186 handle_system_shutdown(epow_log->event_modifier);
187 break;
188
189 case EPOW_SYSTEM_HALT:
190 pr_emerg("Firmware initiated power off");
191 orderly_poweroff(1);
192 break;
193
194 case EPOW_MAIN_ENCLOSURE:
195 case EPOW_POWER_OFF:
196 pr_emerg("Critical power/cooling issue reported by firmware");
197 pr_emerg("Check RTAS error log for details");
198 pr_emerg("Immediate power off");
199 emergency_sync();
200 kernel_power_off();
201 break;
202
203 default:
204 pr_err("Unknown power/cooling event (action code %d)",
205 action_code);
206 }
207}
208
209/* Handle environmental and power warning (EPOW) interrupts. */
David Howells7d12e782006-10-05 14:55:46 +0100210static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Anton Blanchard55fc0c52012-03-21 15:49:59 +0000212 int status;
213 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int critical;
215
216 status = rtas_call(ras_get_sensor_state_token, 2, 2, &state,
217 EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX);
218
219 if (state > 3)
Anton Blanchard55fc0c52012-03-21 15:49:59 +0000220 critical = 1; /* Time Critical */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 else
222 critical = 0;
223
224 spin_lock(&ras_log_buf_lock);
225
226 status = rtas_call(ras_check_exception_token, 6, 1, NULL,
Mark Nelsonb08e2812010-05-26 21:40:39 +0000227 RTAS_VECTOR_EXTERNAL_INTERRUPT,
Grant Likely476eb492011-05-04 15:02:15 +1000228 virq_to_hw(irq),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 RTAS_EPOW_WARNING | RTAS_POWERMGM_EVENTS,
230 critical, __pa(&ras_log_buf),
231 rtas_get_error_log_max());
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
234
Anton Blanchard55fc0c52012-03-21 15:49:59 +0000235 rtas_parse_epow_errlog((struct rtas_error_log *)ras_log_buf);
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 spin_unlock(&ras_log_buf_lock);
238 return IRQ_HANDLED;
239}
240
241/*
242 * Handle hardware error interrupts.
243 *
244 * RTAS check-exception is called to collect data on the exception. If
245 * the error is deemed recoverable, we log a warning and return.
246 * For nonrecoverable errors, an error is logged and we stop all processing
247 * as quickly as possible in order to prevent propagation of the failure.
248 */
David Howells7d12e782006-10-05 14:55:46 +0100249static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct rtas_error_log *rtas_elog;
252 int status = 0xdeadbeef;
253 int fatal;
254
255 spin_lock(&ras_log_buf_lock);
256
257 status = rtas_call(ras_check_exception_token, 6, 1, NULL,
Mark Nelsonb08e2812010-05-26 21:40:39 +0000258 RTAS_VECTOR_EXTERNAL_INTERRUPT,
Grant Likely476eb492011-05-04 15:02:15 +1000259 virq_to_hw(irq),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 RTAS_INTERNAL_ERROR, 1 /*Time Critical */,
261 __pa(&ras_log_buf),
262 rtas_get_error_log_max());
263
264 rtas_elog = (struct rtas_error_log *)ras_log_buf;
265
266 if ((status == 0) && (rtas_elog->severity >= RTAS_SEVERITY_ERROR_SYNC))
267 fatal = 1;
268 else
269 fatal = 0;
270
271 /* format and print the extended information */
272 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
273
274 if (fatal) {
275 udbg_printf("Fatal HW Error <0x%lx 0x%x>\n",
276 *((unsigned long *)&ras_log_buf), status);
277 printk(KERN_EMERG "Error: Fatal hardware error <0x%lx 0x%x>\n",
278 *((unsigned long *)&ras_log_buf), status);
279
Michael Ellerman36f8a2c2008-04-24 15:13:21 +1000280#ifndef DEBUG_RTAS_POWER_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /* Don't actually power off when debugging so we can test
282 * without actually failing while injecting errors.
283 * Error data will not be logged to syslog.
284 */
285 ppc_md.power_off();
286#endif
287 } else {
288 udbg_printf("Recoverable HW Error <0x%lx 0x%x>\n",
289 *((unsigned long *)&ras_log_buf), status);
290 printk(KERN_WARNING
291 "Warning: Recoverable hardware error <0x%lx 0x%x>\n",
292 *((unsigned long *)&ras_log_buf), status);
293 }
294
295 spin_unlock(&ras_log_buf_lock);
296 return IRQ_HANDLED;
297}
298
Anton Blanchardd3685142011-01-11 19:50:51 +0000299/*
300 * Some versions of FWNMI place the buffer inside the 4kB page starting at
301 * 0x7000. Other versions place it inside the rtas buffer. We check both.
302 */
303#define VALID_FWNMI_BUFFER(A) \
304 ((((A) >= 0x7000) && ((A) < 0x7ff0)) || \
305 (((A) >= rtas.base) && ((A) < (rtas.base + rtas.size - 16))))
306
307/*
308 * Get the error information for errors coming through the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * FWNMI vectors. The pt_regs' r3 will be updated to reflect
310 * the actual r3 if possible, and a ptr to the error log entry
311 * will be returned if found.
312 *
Anton Blanchardd3685142011-01-11 19:50:51 +0000313 * If the RTAS error is not of the extended type, then we put it in a per
314 * cpu 64bit buffer. If it is the extended type we use global_mce_data_buf.
315 *
316 * The global_mce_data_buf does not have any locks or protection around it,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 * if a second machine check comes in, or a system reset is done
318 * before we have logged the error, then we will get corruption in the
319 * error log. This is preferable over holding off on calling
320 * ibm,nmi-interlock which would result in us checkstopping if a
321 * second machine check did come in.
322 */
323static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
324{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 unsigned long *savep;
Anton Blanchardd3685142011-01-11 19:50:51 +0000326 struct rtas_error_log *h, *errhdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Anton Blanchardd3685142011-01-11 19:50:51 +0000328 if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
Anton Blanchardf0e939a2011-05-10 13:34:03 +0000329 printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
Anton Blanchardd3685142011-01-11 19:50:51 +0000330 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Anton Blanchardd3685142011-01-11 19:50:51 +0000332
333 savep = __va(regs->gpr[3]);
334 regs->gpr[3] = savep[0]; /* restore original r3 */
335
336 /* If it isn't an extended log we can use the per cpu 64bit buffer */
337 h = (struct rtas_error_log *)&savep[1];
338 if (!h->extended) {
339 memcpy(&__get_cpu_var(mce_data_buf), h, sizeof(__u64));
340 errhdr = (struct rtas_error_log *)&__get_cpu_var(mce_data_buf);
341 } else {
342 int len;
343
344 len = max_t(int, 8+h->extended_log_length, RTAS_ERROR_LOG_MAX);
345 memset(global_mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
346 memcpy(global_mce_data_buf, h, len);
347 errhdr = (struct rtas_error_log *)global_mce_data_buf;
348 }
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return errhdr;
351}
352
353/* Call this when done with the data returned by FWNMI_get_errinfo.
354 * It will release the saved data area for other CPUs in the
355 * partition to receive FWNMI errors.
356 */
357static void fwnmi_release_errinfo(void)
358{
359 int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
360 if (ret != 0)
Anton Blanchardd3685142011-01-11 19:50:51 +0000361 printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Arnd Bergmannc902be72006-01-04 19:55:53 +0000364int pSeries_system_reset_exception(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 if (fwnmi_active) {
367 struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
368 if (errhdr) {
369 /* XXX Should look at FWNMI information */
370 }
371 fwnmi_release_errinfo();
372 }
Arnd Bergmannc902be72006-01-04 19:55:53 +0000373 return 0; /* need to perform reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376/*
377 * See if we can recover from a machine check exception.
378 * This is only called on power4 (or above) and only via
379 * the Firmware Non-Maskable Interrupts (fwnmi) handler
380 * which provides the error analysis for us.
381 *
382 * Return 1 if corrected (or delivered a signal).
383 * Return 0 if there is nothing we can do.
384 */
Anton Blanchardd47d1d82011-01-11 19:49:19 +0000385static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Anton Blanchardd47d1d82011-01-11 19:49:19 +0000387 int recovered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Anton Blanchardd47d1d82011-01-11 19:49:19 +0000389 if (!(regs->msr & MSR_RI)) {
390 /* If MSR_RI isn't set, we cannot recover */
391 recovered = 0;
392
393 } else if (err->disposition == RTAS_DISP_FULLY_RECOVERED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* Platform corrected itself */
Anton Blanchardd47d1d82011-01-11 19:49:19 +0000395 recovered = 1;
396
397 } else if (err->disposition == RTAS_DISP_LIMITED_RECOVERY) {
398 /* Platform corrected itself but could be degraded */
399 printk(KERN_ERR "MCE: limited recovery, system may "
400 "be degraded\n");
401 recovered = 1;
402
403 } else if (user_mode(regs) && !is_global_init(current) &&
404 err->severity == RTAS_SEVERITY_ERROR_SYNC) {
405
406 /*
407 * If we received a synchronous error when in userspace
408 * kill the task. Firmware may report details of the fail
409 * asynchronously, so we can't rely on the target and type
410 * fields being valid here.
411 */
412 printk(KERN_ERR "MCE: uncorrectable error, killing task "
413 "%s:%d\n", current->comm, current->pid);
414
415 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
416 recovered = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Anton Blanchard3f9793e2011-01-11 19:46:29 +0000419 log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Anton Blanchardd47d1d82011-01-11 19:49:19 +0000421 return recovered;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424/*
425 * Handle a machine check.
426 *
427 * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
428 * should be present. If so the handler which called us tells us if the
429 * error was recovered (never true if RI=0).
430 *
431 * On hardware prior to Power 4 these exceptions were asynchronous which
432 * means we can't tell exactly where it occurred and so we can't recover.
433 */
434int pSeries_machine_check_exception(struct pt_regs *regs)
435{
436 struct rtas_error_log *errp;
437
438 if (fwnmi_active) {
439 errp = fwnmi_get_errinfo(regs);
440 fwnmi_release_errinfo();
441 if (errp && recover_mce(regs, errp))
442 return 1;
443 }
444
445 return 0;
446}