ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc64 / kernel / ras.c
1 /*
2  * ras.c
3  * Copyright (C) 2001 Dave Engebretsen IBM Corporation
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  */
19
20 /* Change Activity:
21  * 2001/09/21 : engebret : Created with minimal EPOW and HW exception support.
22  * End Change Activity 
23  */
24
25 #include <linux/errno.h>
26 #include <linux/threads.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/signal.h>
29 #include <linux/sched.h>
30 #include <linux/ioport.h>
31 #include <linux/interrupt.h>
32 #include <linux/timex.h>
33 #include <linux/init.h>
34 #include <linux/slab.h>
35 #include <linux/pci.h>
36 #include <linux/delay.h>
37 #include <linux/irq.h>
38 #include <linux/random.h>
39 #include <linux/sysrq.h>
40
41 #include <asm/uaccess.h>
42 #include <asm/bitops.h>
43 #include <asm/system.h>
44 #include <asm/io.h>
45 #include <asm/pgtable.h>
46 #include <asm/irq.h>
47 #include <asm/cache.h>
48 #include <asm/prom.h>
49 #include <asm/ptrace.h>
50 #include <asm/iSeries/LparData.h>
51 #include <asm/machdep.h>
52 #include <asm/rtas.h>
53 #include <asm/ppcdebug.h>
54
55 static irqreturn_t ras_epow_interrupt(int irq, void *dev_id,
56                                         struct pt_regs * regs);
57 static irqreturn_t ras_error_interrupt(int irq, void *dev_id,
58                                         struct pt_regs * regs);
59
60 /* #define DEBUG */
61
62 /*
63  * Initialize handlers for the set of interrupts caused by hardware errors
64  * and power system events.
65  */
66 static int __init init_ras_IRQ(void)
67 {
68         struct device_node *np;
69         unsigned int *ireg, len, i;
70         int virq;
71
72         if ((np = of_find_node_by_path("/event-sources/internal-errors")) &&
73             (ireg = (unsigned int *)get_property(np, "open-pic-interrupt",
74                                                  &len))) {
75                 for (i=0; i<(len / sizeof(*ireg)); i++) {
76                         virq = virt_irq_create_mapping(*(ireg));
77                         if (virq == NO_IRQ) {
78                                 printk(KERN_ERR "Unable to allocate interrupt "
79                                        "number for %s\n", np->full_name);
80                                 break;
81                         }
82                         request_irq(irq_offset_up(virq),
83                                     ras_error_interrupt, 0, 
84                                     "RAS_ERROR", NULL);
85                         ireg++;
86                 }
87         }
88         of_node_put(np);
89
90         if ((np = of_find_node_by_path("/event-sources/epow-events")) &&
91             (ireg = (unsigned int *)get_property(np, "open-pic-interrupt",
92                                                  &len))) {
93                 for (i=0; i<(len / sizeof(*ireg)); i++) {
94                         virq = virt_irq_create_mapping(*(ireg));
95                         if (virq == NO_IRQ) {
96                                 printk(KERN_ERR "Unable to allocate interrupt "
97                                        " number for %s\n", np->full_name);
98                                 break;
99                         }
100                         request_irq(irq_offset_up(virq),
101                                     ras_epow_interrupt, 0, 
102                                     "RAS_EPOW", NULL);
103                         ireg++;
104                 }
105         }
106         of_node_put(np);
107
108         return 1;
109 }
110 __initcall(init_ras_IRQ);
111
112 static struct rtas_error_log log_buf;
113 static spinlock_t log_lock = SPIN_LOCK_UNLOCKED;
114
115 /*
116  * Handle power subsystem events (EPOW).
117  *
118  * Presently we just log the event has occurred.  This should be fixed
119  * to examine the type of power failure and take appropriate action where
120  * the time horizon permits something useful to be done.
121  */
122 static irqreturn_t
123 ras_epow_interrupt(int irq, void *dev_id, struct pt_regs * regs)
124 {
125         struct rtas_error_log log_entry;
126         unsigned int size = sizeof(log_entry);
127         long status = 0xdeadbeef;
128
129         spin_lock(&log_lock);
130
131         status = rtas_call(rtas_token("check-exception"), 6, 1, NULL, 
132                            0x500, irq, 
133                            RTAS_EPOW_WARNING | RTAS_POWERMGM_EVENTS, 
134                            1,  /* Time Critical */
135                            __pa(&log_buf), size);
136
137         log_entry = log_buf;
138
139         spin_unlock(&log_lock);
140
141         udbg_printf("EPOW <0x%lx 0x%lx>\n", 
142                     *((unsigned long *)&log_entry), status); 
143         printk(KERN_WARNING 
144                 "EPOW <0x%lx 0x%lx>\n",*((unsigned long *)&log_entry), status);
145
146         /* format and print the extended information */
147         log_error((char *)&log_entry, ERR_TYPE_RTAS_LOG, 0);
148         
149         return IRQ_HANDLED;
150 }
151
152 /*
153  * Handle hardware error interrupts.
154  *
155  * RTAS check-exception is called to collect data on the exception.  If
156  * the error is deemed recoverable, we log a warning and return.
157  * For nonrecoverable errors, an error is logged and we stop all processing
158  * as quickly as possible in order to prevent propagation of the failure.
159  */
160 static irqreturn_t
161 ras_error_interrupt(int irq, void *dev_id, struct pt_regs * regs)
162 {
163         struct rtas_error_log log_entry;
164         unsigned int size = sizeof(log_entry);
165         long status = 0xdeadbeef;
166         int fatal;
167
168         spin_lock(&log_lock);
169
170         status = rtas_call(rtas_token("check-exception"), 6, 1, NULL, 
171                            0x500, irq, 
172                            RTAS_INTERNAL_ERROR, 
173                            1, /* Time Critical */
174                            __pa(&log_buf), size);
175
176         log_entry = log_buf;
177
178         spin_unlock(&log_lock);
179
180         if ((status == 0) && (log_entry.severity >= SEVERITY_ERROR_SYNC)) 
181                 fatal = 1;
182         else
183                 fatal = 0;
184
185         /* format and print the extended information */
186         log_error((char *)&log_entry, ERR_TYPE_RTAS_LOG, fatal); 
187
188         if (fatal) {
189                 udbg_printf("HW Error <0x%lx 0x%lx>\n",
190                             *((unsigned long *)&log_entry), status);
191                 printk(KERN_EMERG 
192                        "Error: Fatal hardware error <0x%lx 0x%lx>\n",
193                        *((unsigned long *)&log_entry), status);
194
195 #ifndef DEBUG
196                 /* Don't actually power off when debugging so we can test
197                  * without actually failing while injecting errors.
198                  * Error data will not be logged to syslog.
199                  */
200                 ppc_md.power_off();
201 #endif
202         } else {
203                 udbg_printf("Recoverable HW Error <0x%lx 0x%lx>\n",
204                             *((unsigned long *)&log_entry), status); 
205                 printk(KERN_WARNING 
206                        "Warning: Recoverable hardware error <0x%lx 0x%lx>\n",
207                        *((unsigned long *)&log_entry), status);
208         }
209         return IRQ_HANDLED;
210 }