Merge branch 'master' of /pub/scm/linux/kernel/git/torvalds/linux-2.6
[sfrench/cifs-2.6.git] / arch / powerpc / platforms / cell / ras.c
1 /*
2  * Copyright 2006-2008, IBM Corporation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  */
9
10 #undef DEBUG
11
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/smp.h>
15 #include <linux/reboot.h>
16
17 #include <asm/reg.h>
18 #include <asm/io.h>
19 #include <asm/prom.h>
20 #include <asm/machdep.h>
21 #include <asm/rtas.h>
22 #include <asm/cell-regs.h>
23
24 #include "ras.h"
25
26
27 static void dump_fir(int cpu)
28 {
29         struct cbe_pmd_regs __iomem *pregs = cbe_get_cpu_pmd_regs(cpu);
30         struct cbe_iic_regs __iomem *iregs = cbe_get_cpu_iic_regs(cpu);
31
32         if (pregs == NULL)
33                 return;
34
35         /* Todo: do some nicer parsing of bits and based on them go down
36          * to other sub-units FIRs and not only IIC
37          */
38         printk(KERN_ERR "Global Checkstop FIR    : 0x%016lx\n",
39                in_be64(&pregs->checkstop_fir));
40         printk(KERN_ERR "Global Recoverable FIR  : 0x%016lx\n",
41                in_be64(&pregs->checkstop_fir));
42         printk(KERN_ERR "Global MachineCheck FIR : 0x%016lx\n",
43                in_be64(&pregs->spec_att_mchk_fir));
44
45         if (iregs == NULL)
46                 return;
47         printk(KERN_ERR "IOC FIR                 : 0x%016lx\n",
48                in_be64(&iregs->ioc_fir));
49
50 }
51
52 void cbe_system_error_exception(struct pt_regs *regs)
53 {
54         int cpu = smp_processor_id();
55
56         printk(KERN_ERR "System Error Interrupt on CPU %d !\n", cpu);
57         dump_fir(cpu);
58         dump_stack();
59 }
60
61 void cbe_maintenance_exception(struct pt_regs *regs)
62 {
63         int cpu = smp_processor_id();
64
65         /*
66          * Nothing implemented for the maintenance interrupt at this point
67          */
68
69         printk(KERN_ERR "Unhandled Maintenance interrupt on CPU %d !\n", cpu);
70         dump_stack();
71 }
72
73 void cbe_thermal_exception(struct pt_regs *regs)
74 {
75         int cpu = smp_processor_id();
76
77         /*
78          * Nothing implemented for the thermal interrupt at this point
79          */
80
81         printk(KERN_ERR "Unhandled Thermal interrupt on CPU %d !\n", cpu);
82         dump_stack();
83 }
84
85 static int cbe_machine_check_handler(struct pt_regs *regs)
86 {
87         int cpu = smp_processor_id();
88
89         printk(KERN_ERR "Machine Check Interrupt on CPU %d !\n", cpu);
90         dump_fir(cpu);
91
92         /* No recovery from this code now, lets continue */
93         return 0;
94 }
95
96 struct ptcal_area {
97         struct list_head list;
98         int nid;
99         int order;
100         struct page *pages;
101 };
102
103 static LIST_HEAD(ptcal_list);
104
105 static int ptcal_start_tok, ptcal_stop_tok;
106
107 static int __init cbe_ptcal_enable_on_node(int nid, int order)
108 {
109         struct ptcal_area *area;
110         int ret = -ENOMEM;
111         unsigned long addr;
112
113 #ifdef CONFIG_CRASH_DUMP
114         rtas_call(ptcal_stop_tok, 1, 1, NULL, nid);
115 #endif
116
117         area = kmalloc(sizeof(*area), GFP_KERNEL);
118         if (!area)
119                 goto out_err;
120
121         area->nid = nid;
122         area->order = order;
123         area->pages = alloc_pages_node(area->nid, GFP_KERNEL, area->order);
124
125         if (!area->pages)
126                 goto out_free_area;
127
128         addr = __pa(page_address(area->pages));
129
130         ret = -EIO;
131         if (rtas_call(ptcal_start_tok, 3, 1, NULL, area->nid,
132                                 (unsigned int)(addr >> 32),
133                                 (unsigned int)(addr & 0xffffffff))) {
134                 printk(KERN_ERR "%s: error enabling PTCAL on node %d!\n",
135                                 __func__, nid);
136                 goto out_free_pages;
137         }
138
139         list_add(&area->list, &ptcal_list);
140
141         return 0;
142
143 out_free_pages:
144         __free_pages(area->pages, area->order);
145 out_free_area:
146         kfree(area);
147 out_err:
148         return ret;
149 }
150
151 static int __init cbe_ptcal_enable(void)
152 {
153         const u32 *size;
154         struct device_node *np;
155         int order, found_mic = 0;
156
157         np = of_find_node_by_path("/rtas");
158         if (!np)
159                 return -ENODEV;
160
161         size = of_get_property(np, "ibm,cbe-ptcal-size", NULL);
162         if (!size)
163                 return -ENODEV;
164
165         pr_debug("%s: enabling PTCAL, size = 0x%x\n", __func__, *size);
166         order = get_order(*size);
167         of_node_put(np);
168
169         /* support for malta device trees, with be@/mic@ nodes */
170         for_each_node_by_type(np, "mic-tm") {
171                 cbe_ptcal_enable_on_node(of_node_to_nid(np), order);
172                 found_mic = 1;
173         }
174
175         if (found_mic)
176                 return 0;
177
178         /* support for older device tree - use cpu nodes */
179         for_each_node_by_type(np, "cpu") {
180                 const u32 *nid = of_get_property(np, "node-id", NULL);
181                 if (!nid) {
182                         printk(KERN_ERR "%s: node %s is missing node-id?\n",
183                                         __func__, np->full_name);
184                         continue;
185                 }
186                 cbe_ptcal_enable_on_node(*nid, order);
187                 found_mic = 1;
188         }
189
190         return found_mic ? 0 : -ENODEV;
191 }
192
193 static int cbe_ptcal_disable(void)
194 {
195         struct ptcal_area *area, *tmp;
196         int ret = 0;
197
198         pr_debug("%s: disabling PTCAL\n", __func__);
199
200         list_for_each_entry_safe(area, tmp, &ptcal_list, list) {
201                 /* disable ptcal on this node */
202                 if (rtas_call(ptcal_stop_tok, 1, 1, NULL, area->nid)) {
203                         printk(KERN_ERR "%s: error disabling PTCAL "
204                                         "on node %d!\n", __func__,
205                                         area->nid);
206                         ret = -EIO;
207                         continue;
208                 }
209
210                 /* ensure we can access the PTCAL area */
211                 memset(page_address(area->pages), 0,
212                                 1 << (area->order + PAGE_SHIFT));
213
214                 /* clean up */
215                 list_del(&area->list);
216                 __free_pages(area->pages, area->order);
217                 kfree(area);
218         }
219
220         return ret;
221 }
222
223 static int cbe_ptcal_notify_reboot(struct notifier_block *nb,
224                 unsigned long code, void *data)
225 {
226         return cbe_ptcal_disable();
227 }
228
229 static struct notifier_block cbe_ptcal_reboot_notifier = {
230         .notifier_call = cbe_ptcal_notify_reboot
231 };
232
233 int __init cbe_ptcal_init(void)
234 {
235         int ret;
236         ptcal_start_tok = rtas_token("ibm,cbe-start-ptcal");
237         ptcal_stop_tok = rtas_token("ibm,cbe-stop-ptcal");
238
239         if (ptcal_start_tok == RTAS_UNKNOWN_SERVICE
240                         || ptcal_stop_tok == RTAS_UNKNOWN_SERVICE)
241                 return -ENODEV;
242
243         ret = register_reboot_notifier(&cbe_ptcal_reboot_notifier);
244         if (ret) {
245                 printk(KERN_ERR "Can't disable PTCAL, so not enabling\n");
246                 return ret;
247         }
248
249         return cbe_ptcal_enable();
250 }
251
252 arch_initcall(cbe_ptcal_init);
253
254 void __init cbe_ras_init(void)
255 {
256         unsigned long hid0;
257
258         /*
259          * Enable System Error & thermal interrupts and wakeup conditions
260          */
261
262         hid0 = mfspr(SPRN_HID0);
263         hid0 |= HID0_CBE_THERM_INT_EN | HID0_CBE_THERM_WAKEUP |
264                 HID0_CBE_SYSERR_INT_EN | HID0_CBE_SYSERR_WAKEUP;
265         mtspr(SPRN_HID0, hid0);
266         mb();
267
268         /*
269          * Install machine check handler. Leave setting of precise mode to
270          * what the firmware did for now
271          */
272         ppc_md.machine_check_exception = cbe_machine_check_handler;
273         mb();
274
275         /*
276          * For now, we assume that IOC_FIR is already set to forward some
277          * error conditions to the System Error handler. If that is not true
278          * then it will have to be fixed up here.
279          */
280 }