VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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 unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
56 static spinlock_t ras_log_buf_lock = SPIN_LOCK_UNLOCKED;
57
58 static int ras_get_sensor_state_token;
59 static int ras_check_exception_token;
60
61 #define EPOW_SENSOR_TOKEN       9
62 #define EPOW_SENSOR_INDEX       0
63 #define RAS_VECTOR_OFFSET       0x500
64
65 static irqreturn_t ras_epow_interrupt(int irq, void *dev_id,
66                                         struct pt_regs * regs);
67 static irqreturn_t ras_error_interrupt(int irq, void *dev_id,
68                                         struct pt_regs * regs);
69
70 /* #define DEBUG */
71
72 static void request_ras_irqs(struct device_node *np, char *propname,
73                         irqreturn_t (*handler)(int, void *, struct pt_regs *),
74                         const char *name)
75 {
76         unsigned int *ireg, len, i;
77         int virq, n_intr;
78
79         ireg = (unsigned int *)get_property(np, propname, &len);
80         if (ireg == NULL)
81                 return;
82         n_intr = prom_n_intr_cells(np);
83         len /= n_intr * sizeof(*ireg);
84
85         for (i = 0; i < len; i++) {
86                 virq = virt_irq_create_mapping(*ireg);
87                 if (virq == NO_IRQ) {
88                         printk(KERN_ERR "Unable to allocate interrupt "
89                                "number for %s\n", np->full_name);
90                         return;
91                 }
92                 if (request_irq(irq_offset_up(virq), handler, 0, name, NULL)) {
93                         printk(KERN_ERR "Unable to request interrupt %d for "
94                                "%s\n", irq_offset_up(virq), np->full_name);
95                         return;
96                 }
97                 ireg += n_intr;
98         }
99 }
100
101 /*
102  * Initialize handlers for the set of interrupts caused by hardware errors
103  * and power system events.
104  */
105 static int __init init_ras_IRQ(void)
106 {
107         struct device_node *np;
108
109         ras_get_sensor_state_token = rtas_token("get-sensor-state");
110         ras_check_exception_token = rtas_token("check-exception");
111
112         /* Internal Errors */
113         np = of_find_node_by_path("/event-sources/internal-errors");
114         if (np != NULL) {
115                 request_ras_irqs(np, "open-pic-interrupt", ras_error_interrupt,
116                                  "RAS_ERROR");
117                 request_ras_irqs(np, "interrupts", ras_error_interrupt,
118                                  "RAS_ERROR");
119                 of_node_put(np);
120         }
121
122         /* EPOW Events */
123         np = of_find_node_by_path("/event-sources/epow-events");
124         if (np != NULL) {
125                 request_ras_irqs(np, "open-pic-interrupt", ras_epow_interrupt,
126                                  "RAS_EPOW");
127                 request_ras_irqs(np, "interrupts", ras_epow_interrupt,
128                                  "RAS_EPOW");
129                 of_node_put(np);
130         }
131
132         return 1;
133 }
134 __initcall(init_ras_IRQ);
135
136 /*
137  * Handle power subsystem events (EPOW).
138  *
139  * Presently we just log the event has occurred.  This should be fixed
140  * to examine the type of power failure and take appropriate action where
141  * the time horizon permits something useful to be done.
142  */
143 static irqreturn_t
144 ras_epow_interrupt(int irq, void *dev_id, struct pt_regs * regs)
145 {
146         int status = 0xdeadbeef;
147         int state = 0;
148         int critical;
149
150         status = rtas_call(ras_get_sensor_state_token, 2, 2, &state,
151                            EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX);
152
153         if (state > 3)
154                 critical = 1;  /* Time Critical */
155         else
156                 critical = 0;
157
158         spin_lock(&ras_log_buf_lock);
159
160         status = rtas_call(ras_check_exception_token, 6, 1, NULL,
161                            RAS_VECTOR_OFFSET,
162                            virt_irq_to_real(irq_offset_down(irq)),
163                            RTAS_EPOW_WARNING | RTAS_POWERMGM_EVENTS,
164                            critical, __pa(&ras_log_buf), RTAS_ERROR_LOG_MAX);
165
166         udbg_printf("EPOW <0x%lx 0x%x 0x%x>\n",
167                     *((unsigned long *)&ras_log_buf), status, state);
168         printk(KERN_WARNING "EPOW <0x%lx 0x%x 0x%x>\n",
169                *((unsigned long *)&ras_log_buf), status, state);
170
171         /* format and print the extended information */
172         log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
173
174         spin_unlock(&ras_log_buf_lock);
175         return IRQ_HANDLED;
176 }
177
178 /*
179  * Handle hardware error interrupts.
180  *
181  * RTAS check-exception is called to collect data on the exception.  If
182  * the error is deemed recoverable, we log a warning and return.
183  * For nonrecoverable errors, an error is logged and we stop all processing
184  * as quickly as possible in order to prevent propagation of the failure.
185  */
186 static irqreturn_t
187 ras_error_interrupt(int irq, void *dev_id, struct pt_regs * regs)
188 {
189         struct rtas_error_log *rtas_elog;
190         int status = 0xdeadbeef;
191         int fatal;
192
193         spin_lock(&ras_log_buf_lock);
194
195         status = rtas_call(ras_check_exception_token, 6, 1, NULL,
196                            RAS_VECTOR_OFFSET,
197                            virt_irq_to_real(irq_offset_down(irq)),
198                            RTAS_INTERNAL_ERROR, 1 /*Time Critical */,
199                            __pa(&ras_log_buf), RTAS_ERROR_LOG_MAX);
200
201         rtas_elog = (struct rtas_error_log *)ras_log_buf;
202
203         if ((status == 0) && (rtas_elog->severity >= SEVERITY_ERROR_SYNC))
204                 fatal = 1;
205         else
206                 fatal = 0;
207
208         /* format and print the extended information */
209         log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
210
211         if (fatal) {
212                 udbg_printf("Fatal HW Error <0x%lx 0x%x>\n",
213                             *((unsigned long *)&ras_log_buf), status);
214                 printk(KERN_EMERG "Error: Fatal hardware error <0x%lx 0x%x>\n",
215                        *((unsigned long *)&ras_log_buf), status);
216
217 #ifndef DEBUG
218                 /* Don't actually power off when debugging so we can test
219                  * without actually failing while injecting errors.
220                  * Error data will not be logged to syslog.
221                  */
222                 ppc_md.power_off();
223 #endif
224         } else {
225                 udbg_printf("Recoverable HW Error <0x%lx 0x%x>\n",
226                             *((unsigned long *)&ras_log_buf), status);
227                 printk(KERN_WARNING
228                        "Warning: Recoverable hardware error <0x%lx 0x%x>\n",
229                        *((unsigned long *)&ras_log_buf), status);
230         }
231
232         spin_unlock(&ras_log_buf_lock);
233         return IRQ_HANDLED;
234 }