blob: 048e25711c6bbd3d2fe43d2067545d99dc2999ce [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>
38
39#include <asm/uaccess.h>
40#include <asm/system.h>
41#include <asm/io.h>
42#include <asm/pgtable.h>
43#include <asm/irq.h>
44#include <asm/cache.h>
45#include <asm/prom.h>
46#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/machdep.h>
48#include <asm/rtas.h>
David Gibsondcad47f2005-11-07 09:49:43 +110049#include <asm/udbg.h>
Michael Ellerman8c4f1f22005-12-04 18:39:33 +110050#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Michael Ellerman577830b2007-02-08 18:33:51 +110052#include "pseries.h"
Arnd Bergmannc902be72006-01-04 19:55:53 +000053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
55static DEFINE_SPINLOCK(ras_log_buf_lock);
56
Michael Ellerman541b2752008-05-08 14:27:23 +100057static char mce_data_buf[RTAS_ERROR_LOG_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static int ras_get_sensor_state_token;
60static int ras_check_exception_token;
61
62#define EPOW_SENSOR_TOKEN 9
63#define EPOW_SENSOR_INDEX 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
David Howells7d12e782006-10-05 14:55:46 +010065static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
66static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
70 * Initialize handlers for the set of interrupts caused by hardware errors
71 * and power system events.
72 */
73static int __init init_ras_IRQ(void)
74{
75 struct device_node *np;
76
77 ras_get_sensor_state_token = rtas_token("get-sensor-state");
78 ras_check_exception_token = rtas_token("check-exception");
79
80 /* Internal Errors */
81 np = of_find_node_by_path("/event-sources/internal-errors");
82 if (np != NULL) {
Mark Nelson32c96f72010-05-18 22:51:00 +000083 request_event_sources_irqs(np, ras_error_interrupt,
84 "RAS_ERROR");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 of_node_put(np);
86 }
87
88 /* EPOW Events */
89 np = of_find_node_by_path("/event-sources/epow-events");
90 if (np != NULL) {
Mark Nelson32c96f72010-05-18 22:51:00 +000091 request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 of_node_put(np);
93 }
94
Anton Blanchard69ed3322006-03-28 14:08:39 +110095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97__initcall(init_ras_IRQ);
98
99/*
100 * Handle power subsystem events (EPOW).
101 *
102 * Presently we just log the event has occurred. This should be fixed
103 * to examine the type of power failure and take appropriate action where
104 * the time horizon permits something useful to be done.
105 */
David Howells7d12e782006-10-05 14:55:46 +0100106static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 int status = 0xdeadbeef;
109 int state = 0;
110 int critical;
111
112 status = rtas_call(ras_get_sensor_state_token, 2, 2, &state,
113 EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX);
114
115 if (state > 3)
116 critical = 1; /* Time Critical */
117 else
118 critical = 0;
119
120 spin_lock(&ras_log_buf_lock);
121
122 status = rtas_call(ras_check_exception_token, 6, 1, NULL,
Mark Nelsonb08e2812010-05-26 21:40:39 +0000123 RTAS_VECTOR_EXTERNAL_INTERRUPT,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000124 irq_map[irq].hwirq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 RTAS_EPOW_WARNING | RTAS_POWERMGM_EVENTS,
126 critical, __pa(&ras_log_buf),
127 rtas_get_error_log_max());
128
129 udbg_printf("EPOW <0x%lx 0x%x 0x%x>\n",
130 *((unsigned long *)&ras_log_buf), status, state);
131 printk(KERN_WARNING "EPOW <0x%lx 0x%x 0x%x>\n",
132 *((unsigned long *)&ras_log_buf), status, state);
133
134 /* format and print the extended information */
135 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
136
137 spin_unlock(&ras_log_buf_lock);
138 return IRQ_HANDLED;
139}
140
141/*
142 * Handle hardware error interrupts.
143 *
144 * RTAS check-exception is called to collect data on the exception. If
145 * the error is deemed recoverable, we log a warning and return.
146 * For nonrecoverable errors, an error is logged and we stop all processing
147 * as quickly as possible in order to prevent propagation of the failure.
148 */
David Howells7d12e782006-10-05 14:55:46 +0100149static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 struct rtas_error_log *rtas_elog;
152 int status = 0xdeadbeef;
153 int fatal;
154
155 spin_lock(&ras_log_buf_lock);
156
157 status = rtas_call(ras_check_exception_token, 6, 1, NULL,
Mark Nelsonb08e2812010-05-26 21:40:39 +0000158 RTAS_VECTOR_EXTERNAL_INTERRUPT,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000159 irq_map[irq].hwirq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 RTAS_INTERNAL_ERROR, 1 /*Time Critical */,
161 __pa(&ras_log_buf),
162 rtas_get_error_log_max());
163
164 rtas_elog = (struct rtas_error_log *)ras_log_buf;
165
166 if ((status == 0) && (rtas_elog->severity >= RTAS_SEVERITY_ERROR_SYNC))
167 fatal = 1;
168 else
169 fatal = 0;
170
171 /* format and print the extended information */
172 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
173
174 if (fatal) {
175 udbg_printf("Fatal HW Error <0x%lx 0x%x>\n",
176 *((unsigned long *)&ras_log_buf), status);
177 printk(KERN_EMERG "Error: Fatal hardware error <0x%lx 0x%x>\n",
178 *((unsigned long *)&ras_log_buf), status);
179
Michael Ellerman36f8a2c2008-04-24 15:13:21 +1000180#ifndef DEBUG_RTAS_POWER_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* Don't actually power off when debugging so we can test
182 * without actually failing while injecting errors.
183 * Error data will not be logged to syslog.
184 */
185 ppc_md.power_off();
186#endif
187 } else {
188 udbg_printf("Recoverable HW Error <0x%lx 0x%x>\n",
189 *((unsigned long *)&ras_log_buf), status);
190 printk(KERN_WARNING
191 "Warning: Recoverable hardware error <0x%lx 0x%x>\n",
192 *((unsigned long *)&ras_log_buf), status);
193 }
194
195 spin_unlock(&ras_log_buf_lock);
196 return IRQ_HANDLED;
197}
198
199/* Get the error information for errors coming through the
200 * FWNMI vectors. The pt_regs' r3 will be updated to reflect
201 * the actual r3 if possible, and a ptr to the error log entry
202 * will be returned if found.
203 *
204 * The mce_data_buf does not have any locks or protection around it,
205 * if a second machine check comes in, or a system reset is done
206 * before we have logged the error, then we will get corruption in the
207 * error log. This is preferable over holding off on calling
208 * ibm,nmi-interlock which would result in us checkstopping if a
209 * second machine check did come in.
210 */
211static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
212{
213 unsigned long errdata = regs->gpr[3];
214 struct rtas_error_log *errhdr = NULL;
215 unsigned long *savep;
216
217 if ((errdata >= 0x7000 && errdata < 0x7fff0) ||
218 (errdata >= rtas.base && errdata < rtas.base + rtas.size - 16)) {
219 savep = __va(errdata);
220 regs->gpr[3] = savep[0]; /* restore original r3 */
221 memset(mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
222 memcpy(mce_data_buf, (char *)(savep + 1), RTAS_ERROR_LOG_MAX);
223 errhdr = (struct rtas_error_log *)mce_data_buf;
224 } else {
225 printk("FWNMI: corrupt r3\n");
226 }
227 return errhdr;
228}
229
230/* Call this when done with the data returned by FWNMI_get_errinfo.
231 * It will release the saved data area for other CPUs in the
232 * partition to receive FWNMI errors.
233 */
234static void fwnmi_release_errinfo(void)
235{
236 int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
237 if (ret != 0)
238 printk("FWNMI: nmi-interlock failed: %d\n", ret);
239}
240
Arnd Bergmannc902be72006-01-04 19:55:53 +0000241int pSeries_system_reset_exception(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 if (fwnmi_active) {
244 struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
245 if (errhdr) {
246 /* XXX Should look at FWNMI information */
247 }
248 fwnmi_release_errinfo();
249 }
Arnd Bergmannc902be72006-01-04 19:55:53 +0000250 return 0; /* need to perform reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/*
254 * See if we can recover from a machine check exception.
255 * This is only called on power4 (or above) and only via
256 * the Firmware Non-Maskable Interrupts (fwnmi) handler
257 * which provides the error analysis for us.
258 *
259 * Return 1 if corrected (or delivered a signal).
260 * Return 0 if there is nothing we can do.
261 */
262static int recover_mce(struct pt_regs *regs, struct rtas_error_log * err)
263{
264 int nonfatal = 0;
265
266 if (err->disposition == RTAS_DISP_FULLY_RECOVERED) {
267 /* Platform corrected itself */
268 nonfatal = 1;
269 } else if ((regs->msr & MSR_RI) &&
270 user_mode(regs) &&
271 err->severity == RTAS_SEVERITY_ERROR_SYNC &&
272 err->disposition == RTAS_DISP_NOT_RECOVERED &&
273 err->target == RTAS_TARGET_MEMORY &&
274 err->type == RTAS_TYPE_ECC_UNCORR &&
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700275 !(current->pid == 0 || is_global_init(current))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* Kill off a user process with an ECC error */
277 printk(KERN_ERR "MCE: uncorrectable ecc error for pid %d\n",
278 current->pid);
279 /* XXX something better for ECC error? */
280 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
281 nonfatal = 1;
282 }
283
Anton Blanchard3f9793e2011-01-11 19:46:29 +0000284 log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 return nonfatal;
287}
288
289/*
290 * Handle a machine check.
291 *
292 * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
293 * should be present. If so the handler which called us tells us if the
294 * error was recovered (never true if RI=0).
295 *
296 * On hardware prior to Power 4 these exceptions were asynchronous which
297 * means we can't tell exactly where it occurred and so we can't recover.
298 */
299int pSeries_machine_check_exception(struct pt_regs *regs)
300{
301 struct rtas_error_log *errp;
302
303 if (fwnmi_active) {
304 errp = fwnmi_get_errinfo(regs);
305 fwnmi_release_errinfo();
306 if (errp && recover_mce(regs, errp))
307 return 1;
308 }
309
310 return 0;
311}