Merge with /home/stefan/git/u-boot/denx-440-exceptions
[platform/kernel/u-boot.git] / cpu / ppc4xx / traps.c
1 /*
2  * linux/arch/ppc/kernel/traps.c
3  *
4  * Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
5  *
6  * Modified by Cort Dougan (cort@cs.nmt.edu)
7  * and Paul Mackerras (paulus@cs.anu.edu.au)
8  *
9  * (C) Copyright 2000
10  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11  *
12  * See file CREDITS for list of people who contributed to this
13  * project.
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License as
17  * published by the Free Software Foundation; either version 2 of
18  * the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28  * MA 02111-1307 USA
29  */
30
31 /*
32  * This file handles the architecture-dependent parts of hardware exceptions
33  */
34
35 #include <common.h>
36 #include <command.h>
37 #include <asm/processor.h>
38
39 DECLARE_GLOBAL_DATA_PTR;
40
41 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
42 int (*debugger_exception_handler)(struct pt_regs *) = 0;
43 #endif
44
45 /* Returns 0 if exception not found and fixup otherwise.  */
46 extern unsigned long search_exception_table(unsigned long);
47
48 /* THIS NEEDS CHANGING to use the board info structure.
49  */
50 #define END_OF_MEM      (gd->bd->bi_memstart + gd->bd->bi_memsize)
51
52 static __inline__ void set_tsr(unsigned long val)
53 {
54 #if defined(CONFIG_440)
55         asm volatile("mtspr 0x150, %0" : : "r" (val));
56 #else
57         asm volatile("mttsr %0" : : "r" (val));
58 #endif
59 }
60
61 static __inline__ unsigned long get_esr(void)
62 {
63         unsigned long val;
64
65 #if defined(CONFIG_440)
66         asm volatile("mfspr %0, 0x03e" : "=r" (val) :);
67 #else
68         asm volatile("mfesr %0" : "=r" (val) :);
69 #endif
70         return val;
71 }
72
73 #define ESR_MCI 0x80000000
74 #define ESR_PIL 0x08000000
75 #define ESR_PPR 0x04000000
76 #define ESR_PTR 0x02000000
77 #define ESR_DST 0x00800000
78 #define ESR_DIZ 0x00400000
79 #define ESR_U0F 0x00008000
80
81 #if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
82 extern void do_bedbug_breakpoint(struct pt_regs *);
83 #endif
84
85 /*
86  * Trap & Exception support
87  */
88
89 void
90 print_backtrace(unsigned long *sp)
91 {
92         int cnt = 0;
93         unsigned long i;
94
95         printf("Call backtrace: ");
96         while (sp) {
97                 if ((uint)sp > END_OF_MEM)
98                         break;
99
100                 i = sp[1];
101                 if (cnt++ % 7 == 0)
102                         printf("\n");
103                 printf("%08lX ", i);
104                 if (cnt > 32) break;
105                 sp = (unsigned long *)*sp;
106         }
107         printf("\n");
108 }
109
110 void show_regs(struct pt_regs * regs)
111 {
112         int i;
113
114         printf("NIP: %08lX XER: %08lX LR: %08lX REGS: %p TRAP: %04lx DEAR: %08lX\n",
115                regs->nip, regs->xer, regs->link, regs, regs->trap, regs->dar);
116         printf("MSR: %08lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
117                regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
118                regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
119                regs->msr&MSR_IR ? 1 : 0,
120                regs->msr&MSR_DR ? 1 : 0);
121
122         printf("\n");
123         for (i = 0;  i < 32;  i++) {
124                 if ((i % 8) == 0)
125                 {
126                         printf("GPR%02d: ", i);
127                 }
128
129                 printf("%08lX ", regs->gpr[i]);
130                 if ((i % 8) == 7)
131                 {
132                         printf("\n");
133                 }
134         }
135 }
136
137
138 void
139 _exception(int signr, struct pt_regs *regs)
140 {
141         show_regs(regs);
142         print_backtrace((unsigned long *)regs->gpr[1]);
143         panic("Exception");
144 }
145
146 void
147 MachineCheckException(struct pt_regs *regs)
148 {
149         unsigned long fixup, val;
150         
151         /* Probing PCI using config cycles cause this exception
152          * when a device is not present.  Catch it and return to
153          * the PCI exception handler.
154          */
155         if ((fixup = search_exception_table(regs->nip)) != 0) {
156                 regs->nip = fixup;
157                 return;
158         }
159
160 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
161         if (debugger_exception_handler && (*debugger_exception_handler)(regs))
162                 return;
163 #endif
164
165         printf("Machine Check Exception.\n");
166         printf("Caused by (from msr): ");
167         printf("regs %p ", regs);
168
169         val = get_esr();
170
171 #if !defined(CONFIG_440)
172         if (val& ESR_IMCP) {
173                 printf("Instruction");
174                 mtspr(ESR, val & ~ESR_IMCP);
175         } else
176                 printf("Data");
177         printf(" machine check.\n");
178
179 #elif defined(CONFIG_440)
180         if (val& ESR_IMCP){
181                 printf("Instruction Synchronous Machine Check exception\n");
182                 mtspr(SPRN_ESR, val & ~ESR_IMCP);
183         }
184         else { 
185                 val = mfspr(MCSR);
186                 if (val & MCSR_IB)
187                         printf("Instruction Read PLB Error\n");
188                 if (val & MCSR_DRB)
189                         printf("Data Read PLB Error\n");
190                 if (val & MCSR_DWB)
191                         printf("Data Write PLB Error\n");
192                 if (val & MCSR_TLBP)
193                         printf("TLB Parity Error\n");
194                 if (val & MCSR_ICP){
195                         /*flush_instruction_cache(); */
196                         printf("I-Cache Parity Error\n");
197                 }
198                 if (val & MCSR_DCSP)
199                         printf("D-Cache Search Parity Error\n");
200                 if (val & MCSR_DCFP)
201                         printf("D-Cache Flush Parity Error\n");
202                 if (val & MCSR_IMPE)
203                         printf("Machine Check exception is imprecise\n");
204
205                 /* Clear MCSR */
206                 mtspr(SPRN_MCSR, val);
207         }
208 #endif
209         show_regs(regs);
210         print_backtrace((unsigned long *)regs->gpr[1]);
211         panic("machine check");
212 }
213
214 void
215 AlignmentException(struct pt_regs *regs)
216 {
217 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
218         if (debugger_exception_handler && (*debugger_exception_handler)(regs))
219                 return;
220 #endif
221
222         show_regs(regs);
223         print_backtrace((unsigned long *)regs->gpr[1]);
224         panic("Alignment Exception");
225 }
226
227 void
228 ProgramCheckException(struct pt_regs *regs)
229 {
230         long esr_val;
231
232 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
233         if (debugger_exception_handler && (*debugger_exception_handler)(regs))
234                 return;
235 #endif
236
237         show_regs(regs);
238
239         esr_val = get_esr();
240         if( esr_val & ESR_PIL )
241                 printf( "** Illegal Instruction **\n" );
242         else if( esr_val & ESR_PPR )
243                 printf( "** Privileged Instruction **\n" );
244         else if( esr_val & ESR_PTR )
245                 printf( "** Trap Instruction **\n" );
246
247         print_backtrace((unsigned long *)regs->gpr[1]);
248         panic("Program Check Exception");
249 }
250
251 void
252 DecrementerPITException(struct pt_regs *regs)
253 {
254         /*
255          * Reset PIT interrupt
256          */
257         set_tsr(0x08000000);
258
259         /*
260          * Call timer_interrupt routine in interrupts.c
261          */
262         timer_interrupt(NULL);
263 }
264
265
266 void
267 UnknownException(struct pt_regs *regs)
268 {
269 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
270         if (debugger_exception_handler && (*debugger_exception_handler)(regs))
271                 return;
272 #endif
273
274         printf("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
275                regs->nip, regs->msr, regs->trap);
276         _exception(0, regs);
277 }
278
279 void
280 DebugException(struct pt_regs *regs)
281 {
282         printf("Debugger trap at @ %lx\n", regs->nip );
283         show_regs(regs);
284 #if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
285         do_bedbug_breakpoint( regs );
286 #endif
287 }
288
289 /* Probe an address by reading.  If not present, return -1, otherwise
290  * return 0.
291  */
292 int
293 addr_probe(uint *addr)
294 {
295 #if 0
296         int     retval;
297
298         __asm__ __volatile__(                   \
299                 "1:     lwz %0,0(%1)\n"         \
300                                                 "       eieio\n"                \
301                                                 "       li %0,0\n"              \
302                                                 "2:\n"                          \
303                                                 ".section .fixup,\"ax\"\n"      \
304                                                 "3:     li %0,-1\n"             \
305                                                 "       b 2b\n"                 \
306                                                 ".section __ex_table,\"a\"\n"   \
307                                                 "       .align 2\n"             \
308                                                 "       .long 1b,3b\n"          \
309                                                 ".text"                         \
310                                                 : "=r" (retval) : "r"(addr));
311
312         return (retval);
313 #endif
314         return 0;
315 }