blob: 311ed1993fc036de995f444bac92d1a327236c89 [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>
33#include <linux/slab.h>
34#include <linux/pci.h>
35#include <linux/delay.h>
36#include <linux/irq.h>
37#include <linux/random.h>
38#include <linux/sysrq.h>
39#include <linux/bitops.h>
40
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
Arnd Bergmannc902be72006-01-04 19:55:53 +000054#include "ras.h"
55
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
Michael Ellerman8c4f1f22005-12-04 18:39:33 +110059char mce_data_buf[RTAS_ERROR_LOG_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int ras_get_sensor_state_token;
62static int ras_check_exception_token;
63
64#define EPOW_SENSOR_TOKEN 9
65#define EPOW_SENSOR_INDEX 0
66#define RAS_VECTOR_OFFSET 0x500
67
68static irqreturn_t ras_epow_interrupt(int irq, void *dev_id,
69 struct pt_regs * regs);
70static irqreturn_t ras_error_interrupt(int irq, void *dev_id,
71 struct pt_regs * regs);
72
73/* #define DEBUG */
74
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100075
76static void request_ras_irqs(struct device_node *np,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 irqreturn_t (*handler)(int, void *, struct pt_regs *),
78 const char *name)
79{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100080 int i, index, count = 0;
81 struct of_irq oirq;
Jeremy Kerr954a46e2006-07-12 15:39:43 +100082 const u32 *opicprop;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100083 unsigned int opicplen;
84 unsigned int virqs[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100086 /* Check for obsolete "open-pic-interrupt" property. If present, then
87 * map those interrupts using the default interrupt host and default
88 * trigger
89 */
Jeremy Kerr954a46e2006-07-12 15:39:43 +100090 opicprop = get_property(np, "open-pic-interrupt", &opicplen);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100091 if (opicprop) {
92 opicplen /= sizeof(u32);
93 for (i = 0; i < opicplen; i++) {
94 if (count > 15)
95 break;
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -070096 virqs[count] = irq_create_mapping(NULL, *(opicprop++));
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100097 if (virqs[count] == NO_IRQ)
98 printk(KERN_ERR "Unable to allocate interrupt "
99 "number for %s\n", np->full_name);
100 else
101 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000104 }
105 /* Else use normal interrupt tree parsing */
106 else {
107 /* First try to do a proper OF tree parsing */
108 for (index = 0; of_irq_map_one(np, index, &oirq) == 0;
109 index++) {
110 if (count > 15)
111 break;
112 virqs[count] = irq_create_of_mapping(oirq.controller,
113 oirq.specifier,
114 oirq.size);
115 if (virqs[count] == NO_IRQ)
116 printk(KERN_ERR "Unable to allocate interrupt "
117 "number for %s\n", np->full_name);
118 else
119 count++;
120 }
121 }
122
123 /* Now request them */
124 for (i = 0; i < count; i++) {
125 if (request_irq(virqs[i], handler, 0, name, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 printk(KERN_ERR "Unable to request interrupt %d for "
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000127 "%s\n", virqs[i], np->full_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return;
129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131}
132
133/*
134 * Initialize handlers for the set of interrupts caused by hardware errors
135 * and power system events.
136 */
137static int __init init_ras_IRQ(void)
138{
139 struct device_node *np;
140
141 ras_get_sensor_state_token = rtas_token("get-sensor-state");
142 ras_check_exception_token = rtas_token("check-exception");
143
144 /* Internal Errors */
145 np = of_find_node_by_path("/event-sources/internal-errors");
146 if (np != NULL) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000147 request_ras_irqs(np, ras_error_interrupt, "RAS_ERROR");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 of_node_put(np);
149 }
150
151 /* EPOW Events */
152 np = of_find_node_by_path("/event-sources/epow-events");
153 if (np != NULL) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000154 request_ras_irqs(np, ras_epow_interrupt, "RAS_EPOW");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 of_node_put(np);
156 }
157
Anton Blanchard69ed3322006-03-28 14:08:39 +1100158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160__initcall(init_ras_IRQ);
161
162/*
163 * Handle power subsystem events (EPOW).
164 *
165 * Presently we just log the event has occurred. This should be fixed
166 * to examine the type of power failure and take appropriate action where
167 * the time horizon permits something useful to be done.
168 */
169static irqreturn_t
170ras_epow_interrupt(int irq, void *dev_id, struct pt_regs * regs)
171{
172 int status = 0xdeadbeef;
173 int state = 0;
174 int critical;
175
176 status = rtas_call(ras_get_sensor_state_token, 2, 2, &state,
177 EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX);
178
179 if (state > 3)
180 critical = 1; /* Time Critical */
181 else
182 critical = 0;
183
184 spin_lock(&ras_log_buf_lock);
185
186 status = rtas_call(ras_check_exception_token, 6, 1, NULL,
187 RAS_VECTOR_OFFSET,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000188 irq_map[irq].hwirq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 RTAS_EPOW_WARNING | RTAS_POWERMGM_EVENTS,
190 critical, __pa(&ras_log_buf),
191 rtas_get_error_log_max());
192
193 udbg_printf("EPOW <0x%lx 0x%x 0x%x>\n",
194 *((unsigned long *)&ras_log_buf), status, state);
195 printk(KERN_WARNING "EPOW <0x%lx 0x%x 0x%x>\n",
196 *((unsigned long *)&ras_log_buf), status, state);
197
198 /* format and print the extended information */
199 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
200
201 spin_unlock(&ras_log_buf_lock);
202 return IRQ_HANDLED;
203}
204
205/*
206 * Handle hardware error interrupts.
207 *
208 * RTAS check-exception is called to collect data on the exception. If
209 * the error is deemed recoverable, we log a warning and return.
210 * For nonrecoverable errors, an error is logged and we stop all processing
211 * as quickly as possible in order to prevent propagation of the failure.
212 */
213static irqreturn_t
214ras_error_interrupt(int irq, void *dev_id, struct pt_regs * regs)
215{
216 struct rtas_error_log *rtas_elog;
217 int status = 0xdeadbeef;
218 int fatal;
219
220 spin_lock(&ras_log_buf_lock);
221
222 status = rtas_call(ras_check_exception_token, 6, 1, NULL,
223 RAS_VECTOR_OFFSET,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000224 irq_map[irq].hwirq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 RTAS_INTERNAL_ERROR, 1 /*Time Critical */,
226 __pa(&ras_log_buf),
227 rtas_get_error_log_max());
228
229 rtas_elog = (struct rtas_error_log *)ras_log_buf;
230
231 if ((status == 0) && (rtas_elog->severity >= RTAS_SEVERITY_ERROR_SYNC))
232 fatal = 1;
233 else
234 fatal = 0;
235
236 /* format and print the extended information */
237 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
238
239 if (fatal) {
240 udbg_printf("Fatal HW Error <0x%lx 0x%x>\n",
241 *((unsigned long *)&ras_log_buf), status);
242 printk(KERN_EMERG "Error: Fatal hardware error <0x%lx 0x%x>\n",
243 *((unsigned long *)&ras_log_buf), status);
244
245#ifndef DEBUG
246 /* Don't actually power off when debugging so we can test
247 * without actually failing while injecting errors.
248 * Error data will not be logged to syslog.
249 */
250 ppc_md.power_off();
251#endif
252 } else {
253 udbg_printf("Recoverable HW Error <0x%lx 0x%x>\n",
254 *((unsigned long *)&ras_log_buf), status);
255 printk(KERN_WARNING
256 "Warning: Recoverable hardware error <0x%lx 0x%x>\n",
257 *((unsigned long *)&ras_log_buf), status);
258 }
259
260 spin_unlock(&ras_log_buf_lock);
261 return IRQ_HANDLED;
262}
263
264/* Get the error information for errors coming through the
265 * FWNMI vectors. The pt_regs' r3 will be updated to reflect
266 * the actual r3 if possible, and a ptr to the error log entry
267 * will be returned if found.
268 *
269 * The mce_data_buf does not have any locks or protection around it,
270 * if a second machine check comes in, or a system reset is done
271 * before we have logged the error, then we will get corruption in the
272 * error log. This is preferable over holding off on calling
273 * ibm,nmi-interlock which would result in us checkstopping if a
274 * second machine check did come in.
275 */
276static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
277{
278 unsigned long errdata = regs->gpr[3];
279 struct rtas_error_log *errhdr = NULL;
280 unsigned long *savep;
281
282 if ((errdata >= 0x7000 && errdata < 0x7fff0) ||
283 (errdata >= rtas.base && errdata < rtas.base + rtas.size - 16)) {
284 savep = __va(errdata);
285 regs->gpr[3] = savep[0]; /* restore original r3 */
286 memset(mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
287 memcpy(mce_data_buf, (char *)(savep + 1), RTAS_ERROR_LOG_MAX);
288 errhdr = (struct rtas_error_log *)mce_data_buf;
289 } else {
290 printk("FWNMI: corrupt r3\n");
291 }
292 return errhdr;
293}
294
295/* Call this when done with the data returned by FWNMI_get_errinfo.
296 * It will release the saved data area for other CPUs in the
297 * partition to receive FWNMI errors.
298 */
299static void fwnmi_release_errinfo(void)
300{
301 int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
302 if (ret != 0)
303 printk("FWNMI: nmi-interlock failed: %d\n", ret);
304}
305
Arnd Bergmannc902be72006-01-04 19:55:53 +0000306int pSeries_system_reset_exception(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 if (fwnmi_active) {
309 struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
310 if (errhdr) {
311 /* XXX Should look at FWNMI information */
312 }
313 fwnmi_release_errinfo();
314 }
Arnd Bergmannc902be72006-01-04 19:55:53 +0000315 return 0; /* need to perform reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
318/*
319 * See if we can recover from a machine check exception.
320 * This is only called on power4 (or above) and only via
321 * the Firmware Non-Maskable Interrupts (fwnmi) handler
322 * which provides the error analysis for us.
323 *
324 * Return 1 if corrected (or delivered a signal).
325 * Return 0 if there is nothing we can do.
326 */
327static int recover_mce(struct pt_regs *regs, struct rtas_error_log * err)
328{
329 int nonfatal = 0;
330
331 if (err->disposition == RTAS_DISP_FULLY_RECOVERED) {
332 /* Platform corrected itself */
333 nonfatal = 1;
334 } else if ((regs->msr & MSR_RI) &&
335 user_mode(regs) &&
336 err->severity == RTAS_SEVERITY_ERROR_SYNC &&
337 err->disposition == RTAS_DISP_NOT_RECOVERED &&
338 err->target == RTAS_TARGET_MEMORY &&
339 err->type == RTAS_TYPE_ECC_UNCORR &&
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -0700340 !(current->pid == 0 || is_init(current))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /* Kill off a user process with an ECC error */
342 printk(KERN_ERR "MCE: uncorrectable ecc error for pid %d\n",
343 current->pid);
344 /* XXX something better for ECC error? */
345 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
346 nonfatal = 1;
347 }
348
Michael Ellermand9953102005-10-24 15:07:30 +1000349 log_error((char *)err, ERR_TYPE_RTAS_LOG, !nonfatal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 return nonfatal;
352}
353
354/*
355 * Handle a machine check.
356 *
357 * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
358 * should be present. If so the handler which called us tells us if the
359 * error was recovered (never true if RI=0).
360 *
361 * On hardware prior to Power 4 these exceptions were asynchronous which
362 * means we can't tell exactly where it occurred and so we can't recover.
363 */
364int pSeries_machine_check_exception(struct pt_regs *regs)
365{
366 struct rtas_error_log *errp;
367
368 if (fwnmi_active) {
369 errp = fwnmi_get_errinfo(regs);
370 fwnmi_release_errinfo();
371 if (errp && recover_mce(regs, errp))
372 return 1;
373 }
374
375 return 0;
376}