blob: 02e4a17455164d6faf5f983fde85b44bcc04008a [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/irq.h>
Anton Blanchard90128992012-03-21 15:59:04 +000022#include <linux/of.h>
Anton Blanchard55fc0c52012-03-21 15:49:59 +000023#include <linux/fs.h>
24#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/machdep.h>
27#include <asm/rtas.h>
Michael Ellerman8c4f1f22005-12-04 18:39:33 +110028#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Michael Ellerman577830b2007-02-08 18:33:51 +110030#include "pseries.h"
Arnd Bergmannc902be72006-01-04 19:55:53 +000031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
33static DEFINE_SPINLOCK(ras_log_buf_lock);
34
Anton Blanchardd3685142011-01-11 19:50:51 +000035static char global_mce_data_buf[RTAS_ERROR_LOG_MAX];
36static DEFINE_PER_CPU(__u64, mce_data_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static int ras_check_exception_token;
39
40#define EPOW_SENSOR_TOKEN 9
41#define EPOW_SENSOR_INDEX 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
David Howells7d12e782006-10-05 14:55:46 +010043static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
44static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * Initialize handlers for the set of interrupts caused by hardware errors
49 * and power system events.
50 */
51static int __init init_ras_IRQ(void)
52{
53 struct device_node *np;
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 ras_check_exception_token = rtas_token("check-exception");
56
57 /* Internal Errors */
58 np = of_find_node_by_path("/event-sources/internal-errors");
59 if (np != NULL) {
Mark Nelson32c96f72010-05-18 22:51:00 +000060 request_event_sources_irqs(np, ras_error_interrupt,
61 "RAS_ERROR");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 of_node_put(np);
63 }
64
65 /* EPOW Events */
66 np = of_find_node_by_path("/event-sources/epow-events");
67 if (np != NULL) {
Mark Nelson32c96f72010-05-18 22:51:00 +000068 request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 of_node_put(np);
70 }
71
Anton Blanchard69ed3322006-03-28 14:08:39 +110072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
Michael Ellerman8e83e902014-07-16 12:02:43 +100074machine_subsys_initcall(pseries, init_ras_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Anton Blanchard55fc0c52012-03-21 15:49:59 +000076#define EPOW_SHUTDOWN_NORMAL 1
77#define EPOW_SHUTDOWN_ON_UPS 2
78#define EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS 3
79#define EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH 4
80
81static void handle_system_shutdown(char event_modifier)
82{
83 switch (event_modifier) {
84 case EPOW_SHUTDOWN_NORMAL:
85 pr_emerg("Firmware initiated power off");
liguang1b7e0cb2013-05-30 15:20:33 +080086 orderly_poweroff(true);
Anton Blanchard55fc0c52012-03-21 15:49:59 +000087 break;
88
89 case EPOW_SHUTDOWN_ON_UPS:
90 pr_emerg("Loss of power reported by firmware, system is "
91 "running on UPS/battery");
Anshuman Khandual79872e32014-10-21 13:41:29 +053092 pr_emerg("Check RTAS error log for details");
93 orderly_poweroff(true);
Anton Blanchard55fc0c52012-03-21 15:49:59 +000094 break;
95
96 case EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS:
97 pr_emerg("Loss of system critical functions reported by "
98 "firmware");
99 pr_emerg("Check RTAS error log for details");
liguang1b7e0cb2013-05-30 15:20:33 +0800100 orderly_poweroff(true);
Anton Blanchard55fc0c52012-03-21 15:49:59 +0000101 break;
102
103 case EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH:
104 pr_emerg("Ambient temperature too high reported by firmware");
105 pr_emerg("Check RTAS error log for details");
liguang1b7e0cb2013-05-30 15:20:33 +0800106 orderly_poweroff(true);
Anton Blanchard55fc0c52012-03-21 15:49:59 +0000107 break;
108
109 default:
110 pr_err("Unknown power/cooling shutdown event (modifier %d)",
111 event_modifier);
112 }
113}
114
115struct epow_errorlog {
116 unsigned char sensor_value;
117 unsigned char event_modifier;
118 unsigned char extended_modifier;
119 unsigned char reserved;
120 unsigned char platform_reason;
121};
122
123#define EPOW_RESET 0
124#define EPOW_WARN_COOLING 1
125#define EPOW_WARN_POWER 2
126#define EPOW_SYSTEM_SHUTDOWN 3
127#define EPOW_SYSTEM_HALT 4
128#define EPOW_MAIN_ENCLOSURE 5
129#define EPOW_POWER_OFF 7
130
Anton Blancharde51df2c2014-08-20 08:55:18 +1000131static void rtas_parse_epow_errlog(struct rtas_error_log *log)
Anton Blanchard55fc0c52012-03-21 15:49:59 +0000132{
133 struct pseries_errorlog *pseries_log;
134 struct epow_errorlog *epow_log;
135 char action_code;
136 char modifier;
137
138 pseries_log = get_pseries_errorlog(log, PSERIES_ELOG_SECT_ID_EPOW);
139 if (pseries_log == NULL)
140 return;
141
142 epow_log = (struct epow_errorlog *)pseries_log->data;
143 action_code = epow_log->sensor_value & 0xF; /* bottom 4 bits */
144 modifier = epow_log->event_modifier & 0xF; /* bottom 4 bits */
145
146 switch (action_code) {
147 case EPOW_RESET:
148 pr_err("Non critical power or cooling issue cleared");
149 break;
150
151 case EPOW_WARN_COOLING:
152 pr_err("Non critical cooling issue reported by firmware");
153 pr_err("Check RTAS error log for details");
154 break;
155
156 case EPOW_WARN_POWER:
157 pr_err("Non critical power issue reported by firmware");
158 pr_err("Check RTAS error log for details");
159 break;
160
161 case EPOW_SYSTEM_SHUTDOWN:
162 handle_system_shutdown(epow_log->event_modifier);
163 break;
164
165 case EPOW_SYSTEM_HALT:
166 pr_emerg("Firmware initiated power off");
liguang1b7e0cb2013-05-30 15:20:33 +0800167 orderly_poweroff(true);
Anton Blanchard55fc0c52012-03-21 15:49:59 +0000168 break;
169
170 case EPOW_MAIN_ENCLOSURE:
171 case EPOW_POWER_OFF:
172 pr_emerg("Critical power/cooling issue reported by firmware");
173 pr_emerg("Check RTAS error log for details");
174 pr_emerg("Immediate power off");
175 emergency_sync();
176 kernel_power_off();
177 break;
178
179 default:
180 pr_err("Unknown power/cooling event (action code %d)",
181 action_code);
182 }
183}
184
185/* Handle environmental and power warning (EPOW) interrupts. */
David Howells7d12e782006-10-05 14:55:46 +0100186static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Anton Blanchard55fc0c52012-03-21 15:49:59 +0000188 int status;
189 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int critical;
191
Anton Blanchard587f83e2012-03-21 15:53:43 +0000192 status = rtas_get_sensor(EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 if (state > 3)
Anton Blanchard55fc0c52012-03-21 15:49:59 +0000195 critical = 1; /* Time Critical */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 else
197 critical = 0;
198
199 spin_lock(&ras_log_buf_lock);
200
201 status = rtas_call(ras_check_exception_token, 6, 1, NULL,
Mark Nelsonb08e2812010-05-26 21:40:39 +0000202 RTAS_VECTOR_EXTERNAL_INTERRUPT,
Grant Likely476eb492011-05-04 15:02:15 +1000203 virq_to_hw(irq),
Anton Blanchard6f437472012-03-21 15:56:49 +0000204 RTAS_EPOW_WARNING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 critical, __pa(&ras_log_buf),
206 rtas_get_error_log_max());
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
209
Anton Blanchard55fc0c52012-03-21 15:49:59 +0000210 rtas_parse_epow_errlog((struct rtas_error_log *)ras_log_buf);
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 spin_unlock(&ras_log_buf_lock);
213 return IRQ_HANDLED;
214}
215
216/*
217 * Handle hardware error interrupts.
218 *
219 * RTAS check-exception is called to collect data on the exception. If
220 * the error is deemed recoverable, we log a warning and return.
221 * For nonrecoverable errors, an error is logged and we stop all processing
222 * as quickly as possible in order to prevent propagation of the failure.
223 */
David Howells7d12e782006-10-05 14:55:46 +0100224static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct rtas_error_log *rtas_elog;
Anton Blanchardcc8b5262012-03-21 15:58:03 +0000227 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int fatal;
229
230 spin_lock(&ras_log_buf_lock);
231
232 status = rtas_call(ras_check_exception_token, 6, 1, NULL,
Mark Nelsonb08e2812010-05-26 21:40:39 +0000233 RTAS_VECTOR_EXTERNAL_INTERRUPT,
Grant Likely476eb492011-05-04 15:02:15 +1000234 virq_to_hw(irq),
Anton Blanchardcc8b5262012-03-21 15:58:03 +0000235 RTAS_INTERNAL_ERROR, 1 /* Time Critical */,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 __pa(&ras_log_buf),
237 rtas_get_error_log_max());
238
239 rtas_elog = (struct rtas_error_log *)ras_log_buf;
240
Greg Kurza08a53ea2014-04-04 09:35:13 +0200241 if (status == 0 &&
242 rtas_error_severity(rtas_elog) >= RTAS_SEVERITY_ERROR_SYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 fatal = 1;
244 else
245 fatal = 0;
246
247 /* format and print the extended information */
248 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
249
250 if (fatal) {
Anton Blanchardcc8b5262012-03-21 15:58:03 +0000251 pr_emerg("Fatal hardware error reported by firmware");
252 pr_emerg("Check RTAS error log for details");
253 pr_emerg("Immediate power off");
254 emergency_sync();
255 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 } else {
Anton Blanchardcc8b5262012-03-21 15:58:03 +0000257 pr_err("Recoverable hardware error reported by firmware");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
260 spin_unlock(&ras_log_buf_lock);
261 return IRQ_HANDLED;
262}
263
Anton Blanchardd3685142011-01-11 19:50:51 +0000264/*
265 * Some versions of FWNMI place the buffer inside the 4kB page starting at
266 * 0x7000. Other versions place it inside the rtas buffer. We check both.
267 */
268#define VALID_FWNMI_BUFFER(A) \
269 ((((A) >= 0x7000) && ((A) < 0x7ff0)) || \
270 (((A) >= rtas.base) && ((A) < (rtas.base + rtas.size - 16))))
271
272/*
273 * Get the error information for errors coming through the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * FWNMI vectors. The pt_regs' r3 will be updated to reflect
275 * the actual r3 if possible, and a ptr to the error log entry
276 * will be returned if found.
277 *
Anton Blanchardd3685142011-01-11 19:50:51 +0000278 * If the RTAS error is not of the extended type, then we put it in a per
279 * cpu 64bit buffer. If it is the extended type we use global_mce_data_buf.
280 *
281 * The global_mce_data_buf does not have any locks or protection around it,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * if a second machine check comes in, or a system reset is done
283 * before we have logged the error, then we will get corruption in the
284 * error log. This is preferable over holding off on calling
285 * ibm,nmi-interlock which would result in us checkstopping if a
286 * second machine check did come in.
287 */
288static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
289{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 unsigned long *savep;
Anton Blanchardd3685142011-01-11 19:50:51 +0000291 struct rtas_error_log *h, *errhdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Mahesh Salgaonkaree1dd1e2013-07-10 18:32:56 +0530293 /* Mask top two bits */
294 regs->gpr[3] &= ~(0x3UL << 62);
295
Anton Blanchardd3685142011-01-11 19:50:51 +0000296 if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
Anton Blanchardf0e939a2011-05-10 13:34:03 +0000297 printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
Anton Blanchardd3685142011-01-11 19:50:51 +0000298 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
Anton Blanchardd3685142011-01-11 19:50:51 +0000300
301 savep = __va(regs->gpr[3]);
302 regs->gpr[3] = savep[0]; /* restore original r3 */
303
304 /* If it isn't an extended log we can use the per cpu 64bit buffer */
305 h = (struct rtas_error_log *)&savep[1];
Greg Kurza08a53ea2014-04-04 09:35:13 +0200306 if (!rtas_error_extended(h)) {
Christoph Lameter69111ba2014-10-21 15:23:25 -0500307 memcpy(this_cpu_ptr(&mce_data_buf), h, sizeof(__u64));
308 errhdr = (struct rtas_error_log *)this_cpu_ptr(&mce_data_buf);
Anton Blanchardd3685142011-01-11 19:50:51 +0000309 } else {
Greg Kurza08a53ea2014-04-04 09:35:13 +0200310 int len, error_log_length;
Anton Blanchardd3685142011-01-11 19:50:51 +0000311
Greg Kurza08a53ea2014-04-04 09:35:13 +0200312 error_log_length = 8 + rtas_error_extended_log_length(h);
313 len = max_t(int, error_log_length, RTAS_ERROR_LOG_MAX);
Anton Blanchardd3685142011-01-11 19:50:51 +0000314 memset(global_mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
315 memcpy(global_mce_data_buf, h, len);
316 errhdr = (struct rtas_error_log *)global_mce_data_buf;
317 }
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return errhdr;
320}
321
322/* Call this when done with the data returned by FWNMI_get_errinfo.
323 * It will release the saved data area for other CPUs in the
324 * partition to receive FWNMI errors.
325 */
326static void fwnmi_release_errinfo(void)
327{
328 int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
329 if (ret != 0)
Anton Blanchardd3685142011-01-11 19:50:51 +0000330 printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Arnd Bergmannc902be72006-01-04 19:55:53 +0000333int pSeries_system_reset_exception(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 if (fwnmi_active) {
336 struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
337 if (errhdr) {
338 /* XXX Should look at FWNMI information */
339 }
340 fwnmi_release_errinfo();
341 }
Arnd Bergmannc902be72006-01-04 19:55:53 +0000342 return 0; /* need to perform reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345/*
346 * See if we can recover from a machine check exception.
347 * This is only called on power4 (or above) and only via
348 * the Firmware Non-Maskable Interrupts (fwnmi) handler
349 * which provides the error analysis for us.
350 *
351 * Return 1 if corrected (or delivered a signal).
352 * Return 0 if there is nothing we can do.
353 */
Anton Blanchardd47d1d82011-01-11 19:49:19 +0000354static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Anton Blanchardd47d1d82011-01-11 19:49:19 +0000356 int recovered = 0;
Greg Kurza08a53ea2014-04-04 09:35:13 +0200357 int disposition = rtas_error_disposition(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Anton Blanchardd47d1d82011-01-11 19:49:19 +0000359 if (!(regs->msr & MSR_RI)) {
360 /* If MSR_RI isn't set, we cannot recover */
361 recovered = 0;
362
Greg Kurza08a53ea2014-04-04 09:35:13 +0200363 } else if (disposition == RTAS_DISP_FULLY_RECOVERED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /* Platform corrected itself */
Anton Blanchardd47d1d82011-01-11 19:49:19 +0000365 recovered = 1;
366
Greg Kurza08a53ea2014-04-04 09:35:13 +0200367 } else if (disposition == RTAS_DISP_LIMITED_RECOVERY) {
Anton Blanchardd47d1d82011-01-11 19:49:19 +0000368 /* Platform corrected itself but could be degraded */
369 printk(KERN_ERR "MCE: limited recovery, system may "
370 "be degraded\n");
371 recovered = 1;
372
373 } else if (user_mode(regs) && !is_global_init(current) &&
Greg Kurza08a53ea2014-04-04 09:35:13 +0200374 rtas_error_severity(err) == RTAS_SEVERITY_ERROR_SYNC) {
Anton Blanchardd47d1d82011-01-11 19:49:19 +0000375
376 /*
377 * If we received a synchronous error when in userspace
378 * kill the task. Firmware may report details of the fail
379 * asynchronously, so we can't rely on the target and type
380 * fields being valid here.
381 */
382 printk(KERN_ERR "MCE: uncorrectable error, killing task "
383 "%s:%d\n", current->comm, current->pid);
384
385 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
386 recovered = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
Anton Blanchard3f9793e2011-01-11 19:46:29 +0000389 log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Anton Blanchardd47d1d82011-01-11 19:49:19 +0000391 return recovered;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394/*
395 * Handle a machine check.
396 *
397 * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
398 * should be present. If so the handler which called us tells us if the
399 * error was recovered (never true if RI=0).
400 *
401 * On hardware prior to Power 4 these exceptions were asynchronous which
402 * means we can't tell exactly where it occurred and so we can't recover.
403 */
404int pSeries_machine_check_exception(struct pt_regs *regs)
405{
406 struct rtas_error_log *errp;
407
408 if (fwnmi_active) {
409 errp = fwnmi_get_errinfo(regs);
410 fwnmi_release_errinfo();
411 if (errp && recover_mce(regs, errp))
412 return 1;
413 }
414
415 return 0;
416}