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