GNU Linux-libre 4.14.332-gnu1
[releases.git] / arch / sparc / kernel / traps_64.c
1 /* arch/sparc64/kernel/traps.c
2  *
3  * Copyright (C) 1995,1997,2008,2009,2012 David S. Miller (davem@davemloft.net)
4  * Copyright (C) 1997,1999,2000 Jakub Jelinek (jakub@redhat.com)
5  */
6
7 /*
8  * I like traps on v9, :))))
9  */
10
11 #include <linux/extable.h>
12 #include <linux/sched/mm.h>
13 #include <linux/sched/debug.h>
14 #include <linux/linkage.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/smp.h>
18 #include <linux/mm.h>
19 #include <linux/init.h>
20 #include <linux/kdebug.h>
21 #include <linux/ftrace.h>
22 #include <linux/reboot.h>
23 #include <linux/gfp.h>
24 #include <linux/context_tracking.h>
25
26 #include <asm/smp.h>
27 #include <asm/delay.h>
28 #include <asm/ptrace.h>
29 #include <asm/oplib.h>
30 #include <asm/page.h>
31 #include <asm/pgtable.h>
32 #include <asm/unistd.h>
33 #include <linux/uaccess.h>
34 #include <asm/fpumacro.h>
35 #include <asm/lsu.h>
36 #include <asm/dcu.h>
37 #include <asm/estate.h>
38 #include <asm/chafsr.h>
39 #include <asm/sfafsr.h>
40 #include <asm/psrcompat.h>
41 #include <asm/processor.h>
42 #include <asm/timer.h>
43 #include <asm/head.h>
44 #include <asm/prom.h>
45 #include <asm/memctrl.h>
46 #include <asm/cacheflush.h>
47 #include <asm/setup.h>
48
49 #include "entry.h"
50 #include "kernel.h"
51 #include "kstack.h"
52
53 /* When an irrecoverable trap occurs at tl > 0, the trap entry
54  * code logs the trap state registers at every level in the trap
55  * stack.  It is found at (pt_regs + sizeof(pt_regs)) and the layout
56  * is as follows:
57  */
58 struct tl1_traplog {
59         struct {
60                 unsigned long tstate;
61                 unsigned long tpc;
62                 unsigned long tnpc;
63                 unsigned long tt;
64         } trapstack[4];
65         unsigned long tl;
66 };
67
68 static void dump_tl1_traplog(struct tl1_traplog *p)
69 {
70         int i, limit;
71
72         printk(KERN_EMERG "TRAPLOG: Error at trap level 0x%lx, "
73                "dumping track stack.\n", p->tl);
74
75         limit = (tlb_type == hypervisor) ? 2 : 4;
76         for (i = 0; i < limit; i++) {
77                 printk(KERN_EMERG
78                        "TRAPLOG: Trap level %d TSTATE[%016lx] TPC[%016lx] "
79                        "TNPC[%016lx] TT[%lx]\n",
80                        i + 1,
81                        p->trapstack[i].tstate, p->trapstack[i].tpc,
82                        p->trapstack[i].tnpc, p->trapstack[i].tt);
83                 printk("TRAPLOG: TPC<%pS>\n", (void *) p->trapstack[i].tpc);
84         }
85 }
86
87 void bad_trap(struct pt_regs *regs, long lvl)
88 {
89         char buffer[36];
90         siginfo_t info;
91
92         if (notify_die(DIE_TRAP, "bad trap", regs,
93                        0, lvl, SIGTRAP) == NOTIFY_STOP)
94                 return;
95
96         if (lvl < 0x100) {
97                 sprintf(buffer, "Bad hw trap %lx at tl0\n", lvl);
98                 die_if_kernel(buffer, regs);
99         }
100
101         lvl -= 0x100;
102         if (regs->tstate & TSTATE_PRIV) {
103                 sprintf(buffer, "Kernel bad sw trap %lx", lvl);
104                 die_if_kernel(buffer, regs);
105         }
106         if (test_thread_flag(TIF_32BIT)) {
107                 regs->tpc &= 0xffffffff;
108                 regs->tnpc &= 0xffffffff;
109         }
110         info.si_signo = SIGILL;
111         info.si_errno = 0;
112         info.si_code = ILL_ILLTRP;
113         info.si_addr = (void __user *)regs->tpc;
114         info.si_trapno = lvl;
115         force_sig_info(SIGILL, &info, current);
116 }
117
118 void bad_trap_tl1(struct pt_regs *regs, long lvl)
119 {
120         char buffer[36];
121         
122         if (notify_die(DIE_TRAP_TL1, "bad trap tl1", regs,
123                        0, lvl, SIGTRAP) == NOTIFY_STOP)
124                 return;
125
126         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
127
128         sprintf (buffer, "Bad trap %lx at tl>0", lvl);
129         die_if_kernel (buffer, regs);
130 }
131
132 #ifdef CONFIG_DEBUG_BUGVERBOSE
133 void do_BUG(const char *file, int line)
134 {
135         bust_spinlocks(1);
136         printk("kernel BUG at %s:%d!\n", file, line);
137 }
138 EXPORT_SYMBOL(do_BUG);
139 #endif
140
141 static DEFINE_SPINLOCK(dimm_handler_lock);
142 static dimm_printer_t dimm_handler;
143
144 static int sprintf_dimm(int synd_code, unsigned long paddr, char *buf, int buflen)
145 {
146         unsigned long flags;
147         int ret = -ENODEV;
148
149         spin_lock_irqsave(&dimm_handler_lock, flags);
150         if (dimm_handler) {
151                 ret = dimm_handler(synd_code, paddr, buf, buflen);
152         } else if (tlb_type == spitfire) {
153                 if (prom_getunumber(synd_code, paddr, buf, buflen) == -1)
154                         ret = -EINVAL;
155                 else
156                         ret = 0;
157         } else
158                 ret = -ENODEV;
159         spin_unlock_irqrestore(&dimm_handler_lock, flags);
160
161         return ret;
162 }
163
164 int register_dimm_printer(dimm_printer_t func)
165 {
166         unsigned long flags;
167         int ret = 0;
168
169         spin_lock_irqsave(&dimm_handler_lock, flags);
170         if (!dimm_handler)
171                 dimm_handler = func;
172         else
173                 ret = -EEXIST;
174         spin_unlock_irqrestore(&dimm_handler_lock, flags);
175
176         return ret;
177 }
178 EXPORT_SYMBOL_GPL(register_dimm_printer);
179
180 void unregister_dimm_printer(dimm_printer_t func)
181 {
182         unsigned long flags;
183
184         spin_lock_irqsave(&dimm_handler_lock, flags);
185         if (dimm_handler == func)
186                 dimm_handler = NULL;
187         spin_unlock_irqrestore(&dimm_handler_lock, flags);
188 }
189 EXPORT_SYMBOL_GPL(unregister_dimm_printer);
190
191 void spitfire_insn_access_exception(struct pt_regs *regs, unsigned long sfsr, unsigned long sfar)
192 {
193         enum ctx_state prev_state = exception_enter();
194         siginfo_t info;
195
196         if (notify_die(DIE_TRAP, "instruction access exception", regs,
197                        0, 0x8, SIGTRAP) == NOTIFY_STOP)
198                 goto out;
199
200         if (regs->tstate & TSTATE_PRIV) {
201                 printk("spitfire_insn_access_exception: SFSR[%016lx] "
202                        "SFAR[%016lx], going.\n", sfsr, sfar);
203                 die_if_kernel("Iax", regs);
204         }
205         if (test_thread_flag(TIF_32BIT)) {
206                 regs->tpc &= 0xffffffff;
207                 regs->tnpc &= 0xffffffff;
208         }
209         info.si_signo = SIGSEGV;
210         info.si_errno = 0;
211         info.si_code = SEGV_MAPERR;
212         info.si_addr = (void __user *)regs->tpc;
213         info.si_trapno = 0;
214         force_sig_info(SIGSEGV, &info, current);
215 out:
216         exception_exit(prev_state);
217 }
218
219 void spitfire_insn_access_exception_tl1(struct pt_regs *regs, unsigned long sfsr, unsigned long sfar)
220 {
221         if (notify_die(DIE_TRAP_TL1, "instruction access exception tl1", regs,
222                        0, 0x8, SIGTRAP) == NOTIFY_STOP)
223                 return;
224
225         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
226         spitfire_insn_access_exception(regs, sfsr, sfar);
227 }
228
229 void sun4v_insn_access_exception(struct pt_regs *regs, unsigned long addr, unsigned long type_ctx)
230 {
231         unsigned short type = (type_ctx >> 16);
232         unsigned short ctx  = (type_ctx & 0xffff);
233         siginfo_t info;
234
235         if (notify_die(DIE_TRAP, "instruction access exception", regs,
236                        0, 0x8, SIGTRAP) == NOTIFY_STOP)
237                 return;
238
239         if (regs->tstate & TSTATE_PRIV) {
240                 printk("sun4v_insn_access_exception: ADDR[%016lx] "
241                        "CTX[%04x] TYPE[%04x], going.\n",
242                        addr, ctx, type);
243                 die_if_kernel("Iax", regs);
244         }
245
246         if (test_thread_flag(TIF_32BIT)) {
247                 regs->tpc &= 0xffffffff;
248                 regs->tnpc &= 0xffffffff;
249         }
250         info.si_signo = SIGSEGV;
251         info.si_errno = 0;
252         info.si_code = SEGV_MAPERR;
253         info.si_addr = (void __user *) addr;
254         info.si_trapno = 0;
255         force_sig_info(SIGSEGV, &info, current);
256 }
257
258 void sun4v_insn_access_exception_tl1(struct pt_regs *regs, unsigned long addr, unsigned long type_ctx)
259 {
260         if (notify_die(DIE_TRAP_TL1, "instruction access exception tl1", regs,
261                        0, 0x8, SIGTRAP) == NOTIFY_STOP)
262                 return;
263
264         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
265         sun4v_insn_access_exception(regs, addr, type_ctx);
266 }
267
268 bool is_no_fault_exception(struct pt_regs *regs)
269 {
270         unsigned char asi;
271         u32 insn;
272
273         if (get_user(insn, (u32 __user *)regs->tpc) == -EFAULT)
274                 return false;
275
276         /*
277          * Must do a little instruction decoding here in order to
278          * decide on a course of action. The bits of interest are:
279          *  insn[31:30] = op, where 3 indicates the load/store group
280          *  insn[24:19] = op3, which identifies individual opcodes
281          *  insn[13] indicates an immediate offset
282          *  op3[4]=1 identifies alternate space instructions
283          *  op3[5:4]=3 identifies floating point instructions
284          *  op3[2]=1 identifies stores
285          * See "Opcode Maps" in the appendix of any Sparc V9
286          * architecture spec for full details.
287          */
288         if ((insn & 0xc0800000) == 0xc0800000) {    /* op=3, op3[4]=1   */
289                 if (insn & 0x2000)                  /* immediate offset */
290                         asi = (regs->tstate >> 24); /* saved %asi       */
291                 else
292                         asi = (insn >> 5);          /* immediate asi    */
293                 if ((asi & 0xf6) == ASI_PNF) {
294                         if (insn & 0x200000)        /* op3[2], stores   */
295                                 return false;
296                         if (insn & 0x1000000)       /* op3[5:4]=3 (fp)  */
297                                 handle_ldf_stq(insn, regs);
298                         else
299                                 handle_ld_nf(insn, regs);
300                         return true;
301                 }
302         }
303         return false;
304 }
305
306 void spitfire_data_access_exception(struct pt_regs *regs, unsigned long sfsr, unsigned long sfar)
307 {
308         enum ctx_state prev_state = exception_enter();
309         siginfo_t info;
310
311         if (notify_die(DIE_TRAP, "data access exception", regs,
312                        0, 0x30, SIGTRAP) == NOTIFY_STOP)
313                 goto out;
314
315         if (regs->tstate & TSTATE_PRIV) {
316                 /* Test if this comes from uaccess places. */
317                 const struct exception_table_entry *entry;
318
319                 entry = search_exception_tables(regs->tpc);
320                 if (entry) {
321                         /* Ouch, somebody is trying VM hole tricks on us... */
322 #ifdef DEBUG_EXCEPTIONS
323                         printk("Exception: PC<%016lx> faddr<UNKNOWN>\n", regs->tpc);
324                         printk("EX_TABLE: insn<%016lx> fixup<%016lx>\n",
325                                regs->tpc, entry->fixup);
326 #endif
327                         regs->tpc = entry->fixup;
328                         regs->tnpc = regs->tpc + 4;
329                         goto out;
330                 }
331                 /* Shit... */
332                 printk("spitfire_data_access_exception: SFSR[%016lx] "
333                        "SFAR[%016lx], going.\n", sfsr, sfar);
334                 die_if_kernel("Dax", regs);
335         }
336
337         if (is_no_fault_exception(regs))
338                 return;
339
340         info.si_signo = SIGSEGV;
341         info.si_errno = 0;
342         info.si_code = SEGV_MAPERR;
343         info.si_addr = (void __user *)sfar;
344         info.si_trapno = 0;
345         force_sig_info(SIGSEGV, &info, current);
346 out:
347         exception_exit(prev_state);
348 }
349
350 void spitfire_data_access_exception_tl1(struct pt_regs *regs, unsigned long sfsr, unsigned long sfar)
351 {
352         if (notify_die(DIE_TRAP_TL1, "data access exception tl1", regs,
353                        0, 0x30, SIGTRAP) == NOTIFY_STOP)
354                 return;
355
356         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
357         spitfire_data_access_exception(regs, sfsr, sfar);
358 }
359
360 void sun4v_data_access_exception(struct pt_regs *regs, unsigned long addr, unsigned long type_ctx)
361 {
362         unsigned short type = (type_ctx >> 16);
363         unsigned short ctx  = (type_ctx & 0xffff);
364         siginfo_t info;
365
366         if (notify_die(DIE_TRAP, "data access exception", regs,
367                        0, 0x8, SIGTRAP) == NOTIFY_STOP)
368                 return;
369
370         if (regs->tstate & TSTATE_PRIV) {
371                 /* Test if this comes from uaccess places. */
372                 const struct exception_table_entry *entry;
373
374                 entry = search_exception_tables(regs->tpc);
375                 if (entry) {
376                         /* Ouch, somebody is trying VM hole tricks on us... */
377 #ifdef DEBUG_EXCEPTIONS
378                         printk("Exception: PC<%016lx> faddr<UNKNOWN>\n", regs->tpc);
379                         printk("EX_TABLE: insn<%016lx> fixup<%016lx>\n",
380                                regs->tpc, entry->fixup);
381 #endif
382                         regs->tpc = entry->fixup;
383                         regs->tnpc = regs->tpc + 4;
384                         return;
385                 }
386                 printk("sun4v_data_access_exception: ADDR[%016lx] "
387                        "CTX[%04x] TYPE[%04x], going.\n",
388                        addr, ctx, type);
389                 die_if_kernel("Dax", regs);
390         }
391
392         if (test_thread_flag(TIF_32BIT)) {
393                 regs->tpc &= 0xffffffff;
394                 regs->tnpc &= 0xffffffff;
395         }
396         if (is_no_fault_exception(regs))
397                 return;
398
399         info.si_signo = SIGSEGV;
400         info.si_errno = 0;
401         info.si_code = SEGV_MAPERR;
402         info.si_addr = (void __user *) addr;
403         info.si_trapno = 0;
404         force_sig_info(SIGSEGV, &info, current);
405 }
406
407 void sun4v_data_access_exception_tl1(struct pt_regs *regs, unsigned long addr, unsigned long type_ctx)
408 {
409         if (notify_die(DIE_TRAP_TL1, "data access exception tl1", regs,
410                        0, 0x8, SIGTRAP) == NOTIFY_STOP)
411                 return;
412
413         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
414         sun4v_data_access_exception(regs, addr, type_ctx);
415 }
416
417 #ifdef CONFIG_PCI
418 #include "pci_impl.h"
419 #endif
420
421 /* When access exceptions happen, we must do this. */
422 static void spitfire_clean_and_reenable_l1_caches(void)
423 {
424         unsigned long va;
425
426         if (tlb_type != spitfire)
427                 BUG();
428
429         /* Clean 'em. */
430         for (va =  0; va < (PAGE_SIZE << 1); va += 32) {
431                 spitfire_put_icache_tag(va, 0x0);
432                 spitfire_put_dcache_tag(va, 0x0);
433         }
434
435         /* Re-enable in LSU. */
436         __asm__ __volatile__("flush %%g6\n\t"
437                              "membar #Sync\n\t"
438                              "stxa %0, [%%g0] %1\n\t"
439                              "membar #Sync"
440                              : /* no outputs */
441                              : "r" (LSU_CONTROL_IC | LSU_CONTROL_DC |
442                                     LSU_CONTROL_IM | LSU_CONTROL_DM),
443                              "i" (ASI_LSU_CONTROL)
444                              : "memory");
445 }
446
447 static void spitfire_enable_estate_errors(void)
448 {
449         __asm__ __volatile__("stxa      %0, [%%g0] %1\n\t"
450                              "membar    #Sync"
451                              : /* no outputs */
452                              : "r" (ESTATE_ERR_ALL),
453                                "i" (ASI_ESTATE_ERROR_EN));
454 }
455
456 static char ecc_syndrome_table[] = {
457         0x4c, 0x40, 0x41, 0x48, 0x42, 0x48, 0x48, 0x49,
458         0x43, 0x48, 0x48, 0x49, 0x48, 0x49, 0x49, 0x4a,
459         0x44, 0x48, 0x48, 0x20, 0x48, 0x39, 0x4b, 0x48,
460         0x48, 0x25, 0x31, 0x48, 0x28, 0x48, 0x48, 0x2c,
461         0x45, 0x48, 0x48, 0x21, 0x48, 0x3d, 0x04, 0x48,
462         0x48, 0x4b, 0x35, 0x48, 0x2d, 0x48, 0x48, 0x29,
463         0x48, 0x00, 0x01, 0x48, 0x0a, 0x48, 0x48, 0x4b,
464         0x0f, 0x48, 0x48, 0x4b, 0x48, 0x49, 0x49, 0x48,
465         0x46, 0x48, 0x48, 0x2a, 0x48, 0x3b, 0x27, 0x48,
466         0x48, 0x4b, 0x33, 0x48, 0x22, 0x48, 0x48, 0x2e,
467         0x48, 0x19, 0x1d, 0x48, 0x1b, 0x4a, 0x48, 0x4b,
468         0x1f, 0x48, 0x4a, 0x4b, 0x48, 0x4b, 0x4b, 0x48,
469         0x48, 0x4b, 0x24, 0x48, 0x07, 0x48, 0x48, 0x36,
470         0x4b, 0x48, 0x48, 0x3e, 0x48, 0x30, 0x38, 0x48,
471         0x49, 0x48, 0x48, 0x4b, 0x48, 0x4b, 0x16, 0x48,
472         0x48, 0x12, 0x4b, 0x48, 0x49, 0x48, 0x48, 0x4b,
473         0x47, 0x48, 0x48, 0x2f, 0x48, 0x3f, 0x4b, 0x48,
474         0x48, 0x06, 0x37, 0x48, 0x23, 0x48, 0x48, 0x2b,
475         0x48, 0x05, 0x4b, 0x48, 0x4b, 0x48, 0x48, 0x32,
476         0x26, 0x48, 0x48, 0x3a, 0x48, 0x34, 0x3c, 0x48,
477         0x48, 0x11, 0x15, 0x48, 0x13, 0x4a, 0x48, 0x4b,
478         0x17, 0x48, 0x4a, 0x4b, 0x48, 0x4b, 0x4b, 0x48,
479         0x49, 0x48, 0x48, 0x4b, 0x48, 0x4b, 0x1e, 0x48,
480         0x48, 0x1a, 0x4b, 0x48, 0x49, 0x48, 0x48, 0x4b,
481         0x48, 0x08, 0x0d, 0x48, 0x02, 0x48, 0x48, 0x49,
482         0x03, 0x48, 0x48, 0x49, 0x48, 0x4b, 0x4b, 0x48,
483         0x49, 0x48, 0x48, 0x49, 0x48, 0x4b, 0x10, 0x48,
484         0x48, 0x14, 0x4b, 0x48, 0x4b, 0x48, 0x48, 0x4b,
485         0x49, 0x48, 0x48, 0x49, 0x48, 0x4b, 0x18, 0x48,
486         0x48, 0x1c, 0x4b, 0x48, 0x4b, 0x48, 0x48, 0x4b,
487         0x4a, 0x0c, 0x09, 0x48, 0x0e, 0x48, 0x48, 0x4b,
488         0x0b, 0x48, 0x48, 0x4b, 0x48, 0x4b, 0x4b, 0x4a
489 };
490
491 static char *syndrome_unknown = "<Unknown>";
492
493 static void spitfire_log_udb_syndrome(unsigned long afar, unsigned long udbh, unsigned long udbl, unsigned long bit)
494 {
495         unsigned short scode;
496         char memmod_str[64], *p;
497
498         if (udbl & bit) {
499                 scode = ecc_syndrome_table[udbl & 0xff];
500                 if (sprintf_dimm(scode, afar, memmod_str, sizeof(memmod_str)) < 0)
501                         p = syndrome_unknown;
502                 else
503                         p = memmod_str;
504                 printk(KERN_WARNING "CPU[%d]: UDBL Syndrome[%x] "
505                        "Memory Module \"%s\"\n",
506                        smp_processor_id(), scode, p);
507         }
508
509         if (udbh & bit) {
510                 scode = ecc_syndrome_table[udbh & 0xff];
511                 if (sprintf_dimm(scode, afar, memmod_str, sizeof(memmod_str)) < 0)
512                         p = syndrome_unknown;
513                 else
514                         p = memmod_str;
515                 printk(KERN_WARNING "CPU[%d]: UDBH Syndrome[%x] "
516                        "Memory Module \"%s\"\n",
517                        smp_processor_id(), scode, p);
518         }
519
520 }
521
522 static void spitfire_cee_log(unsigned long afsr, unsigned long afar, unsigned long udbh, unsigned long udbl, int tl1, struct pt_regs *regs)
523 {
524
525         printk(KERN_WARNING "CPU[%d]: Correctable ECC Error "
526                "AFSR[%lx] AFAR[%016lx] UDBL[%lx] UDBH[%lx] TL>1[%d]\n",
527                smp_processor_id(), afsr, afar, udbl, udbh, tl1);
528
529         spitfire_log_udb_syndrome(afar, udbh, udbl, UDBE_CE);
530
531         /* We always log it, even if someone is listening for this
532          * trap.
533          */
534         notify_die(DIE_TRAP, "Correctable ECC Error", regs,
535                    0, TRAP_TYPE_CEE, SIGTRAP);
536
537         /* The Correctable ECC Error trap does not disable I/D caches.  So
538          * we only have to restore the ESTATE Error Enable register.
539          */
540         spitfire_enable_estate_errors();
541 }
542
543 static void spitfire_ue_log(unsigned long afsr, unsigned long afar, unsigned long udbh, unsigned long udbl, unsigned long tt, int tl1, struct pt_regs *regs)
544 {
545         siginfo_t info;
546
547         printk(KERN_WARNING "CPU[%d]: Uncorrectable Error AFSR[%lx] "
548                "AFAR[%lx] UDBL[%lx] UDBH[%ld] TT[%lx] TL>1[%d]\n",
549                smp_processor_id(), afsr, afar, udbl, udbh, tt, tl1);
550
551         /* XXX add more human friendly logging of the error status
552          * XXX as is implemented for cheetah
553          */
554
555         spitfire_log_udb_syndrome(afar, udbh, udbl, UDBE_UE);
556
557         /* We always log it, even if someone is listening for this
558          * trap.
559          */
560         notify_die(DIE_TRAP, "Uncorrectable Error", regs,
561                    0, tt, SIGTRAP);
562
563         if (regs->tstate & TSTATE_PRIV) {
564                 if (tl1)
565                         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
566                 die_if_kernel("UE", regs);
567         }
568
569         /* XXX need more intelligent processing here, such as is implemented
570          * XXX for cheetah errors, in fact if the E-cache still holds the
571          * XXX line with bad parity this will loop
572          */
573
574         spitfire_clean_and_reenable_l1_caches();
575         spitfire_enable_estate_errors();
576
577         if (test_thread_flag(TIF_32BIT)) {
578                 regs->tpc &= 0xffffffff;
579                 regs->tnpc &= 0xffffffff;
580         }
581         info.si_signo = SIGBUS;
582         info.si_errno = 0;
583         info.si_code = BUS_OBJERR;
584         info.si_addr = (void *)0;
585         info.si_trapno = 0;
586         force_sig_info(SIGBUS, &info, current);
587 }
588
589 void spitfire_access_error(struct pt_regs *regs, unsigned long status_encoded, unsigned long afar)
590 {
591         unsigned long afsr, tt, udbh, udbl;
592         int tl1;
593
594         afsr = (status_encoded & SFSTAT_AFSR_MASK) >> SFSTAT_AFSR_SHIFT;
595         tt = (status_encoded & SFSTAT_TRAP_TYPE) >> SFSTAT_TRAP_TYPE_SHIFT;
596         tl1 = (status_encoded & SFSTAT_TL_GT_ONE) ? 1 : 0;
597         udbl = (status_encoded & SFSTAT_UDBL_MASK) >> SFSTAT_UDBL_SHIFT;
598         udbh = (status_encoded & SFSTAT_UDBH_MASK) >> SFSTAT_UDBH_SHIFT;
599
600 #ifdef CONFIG_PCI
601         if (tt == TRAP_TYPE_DAE &&
602             pci_poke_in_progress && pci_poke_cpu == smp_processor_id()) {
603                 spitfire_clean_and_reenable_l1_caches();
604                 spitfire_enable_estate_errors();
605
606                 pci_poke_faulted = 1;
607                 regs->tnpc = regs->tpc + 4;
608                 return;
609         }
610 #endif
611
612         if (afsr & SFAFSR_UE)
613                 spitfire_ue_log(afsr, afar, udbh, udbl, tt, tl1, regs);
614
615         if (tt == TRAP_TYPE_CEE) {
616                 /* Handle the case where we took a CEE trap, but ACK'd
617                  * only the UE state in the UDB error registers.
618                  */
619                 if (afsr & SFAFSR_UE) {
620                         if (udbh & UDBE_CE) {
621                                 __asm__ __volatile__(
622                                         "stxa   %0, [%1] %2\n\t"
623                                         "membar #Sync"
624                                         : /* no outputs */
625                                         : "r" (udbh & UDBE_CE),
626                                           "r" (0x0), "i" (ASI_UDB_ERROR_W));
627                         }
628                         if (udbl & UDBE_CE) {
629                                 __asm__ __volatile__(
630                                         "stxa   %0, [%1] %2\n\t"
631                                         "membar #Sync"
632                                         : /* no outputs */
633                                         : "r" (udbl & UDBE_CE),
634                                           "r" (0x18), "i" (ASI_UDB_ERROR_W));
635                         }
636                 }
637
638                 spitfire_cee_log(afsr, afar, udbh, udbl, tl1, regs);
639         }
640 }
641
642 int cheetah_pcache_forced_on;
643
644 void cheetah_enable_pcache(void)
645 {
646         unsigned long dcr;
647
648         printk("CHEETAH: Enabling P-Cache on cpu %d.\n",
649                smp_processor_id());
650
651         __asm__ __volatile__("ldxa [%%g0] %1, %0"
652                              : "=r" (dcr)
653                              : "i" (ASI_DCU_CONTROL_REG));
654         dcr |= (DCU_PE | DCU_HPE | DCU_SPE | DCU_SL);
655         __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
656                              "membar #Sync"
657                              : /* no outputs */
658                              : "r" (dcr), "i" (ASI_DCU_CONTROL_REG));
659 }
660
661 /* Cheetah error trap handling. */
662 static unsigned long ecache_flush_physbase;
663 static unsigned long ecache_flush_linesize;
664 static unsigned long ecache_flush_size;
665
666 /* This table is ordered in priority of errors and matches the
667  * AFAR overwrite policy as well.
668  */
669
670 struct afsr_error_table {
671         unsigned long mask;
672         const char *name;
673 };
674
675 static const char CHAFSR_PERR_msg[] =
676         "System interface protocol error";
677 static const char CHAFSR_IERR_msg[] =
678         "Internal processor error";
679 static const char CHAFSR_ISAP_msg[] =
680         "System request parity error on incoming address";
681 static const char CHAFSR_UCU_msg[] =
682         "Uncorrectable E-cache ECC error for ifetch/data";
683 static const char CHAFSR_UCC_msg[] =
684         "SW Correctable E-cache ECC error for ifetch/data";
685 static const char CHAFSR_UE_msg[] =
686         "Uncorrectable system bus data ECC error for read";
687 static const char CHAFSR_EDU_msg[] =
688         "Uncorrectable E-cache ECC error for stmerge/blkld";
689 static const char CHAFSR_EMU_msg[] =
690         "Uncorrectable system bus MTAG error";
691 static const char CHAFSR_WDU_msg[] =
692         "Uncorrectable E-cache ECC error for writeback";
693 static const char CHAFSR_CPU_msg[] =
694         "Uncorrectable ECC error for copyout";
695 static const char CHAFSR_CE_msg[] =
696         "HW corrected system bus data ECC error for read";
697 static const char CHAFSR_EDC_msg[] =
698         "HW corrected E-cache ECC error for stmerge/blkld";
699 static const char CHAFSR_EMC_msg[] =
700         "HW corrected system bus MTAG ECC error";
701 static const char CHAFSR_WDC_msg[] =
702         "HW corrected E-cache ECC error for writeback";
703 static const char CHAFSR_CPC_msg[] =
704         "HW corrected ECC error for copyout";
705 static const char CHAFSR_TO_msg[] =
706         "Unmapped error from system bus";
707 static const char CHAFSR_BERR_msg[] =
708         "Bus error response from system bus";
709 static const char CHAFSR_IVC_msg[] =
710         "HW corrected system bus data ECC error for ivec read";
711 static const char CHAFSR_IVU_msg[] =
712         "Uncorrectable system bus data ECC error for ivec read";
713 static struct afsr_error_table __cheetah_error_table[] = {
714         {       CHAFSR_PERR,    CHAFSR_PERR_msg         },
715         {       CHAFSR_IERR,    CHAFSR_IERR_msg         },
716         {       CHAFSR_ISAP,    CHAFSR_ISAP_msg         },
717         {       CHAFSR_UCU,     CHAFSR_UCU_msg          },
718         {       CHAFSR_UCC,     CHAFSR_UCC_msg          },
719         {       CHAFSR_UE,      CHAFSR_UE_msg           },
720         {       CHAFSR_EDU,     CHAFSR_EDU_msg          },
721         {       CHAFSR_EMU,     CHAFSR_EMU_msg          },
722         {       CHAFSR_WDU,     CHAFSR_WDU_msg          },
723         {       CHAFSR_CPU,     CHAFSR_CPU_msg          },
724         {       CHAFSR_CE,      CHAFSR_CE_msg           },
725         {       CHAFSR_EDC,     CHAFSR_EDC_msg          },
726         {       CHAFSR_EMC,     CHAFSR_EMC_msg          },
727         {       CHAFSR_WDC,     CHAFSR_WDC_msg          },
728         {       CHAFSR_CPC,     CHAFSR_CPC_msg          },
729         {       CHAFSR_TO,      CHAFSR_TO_msg           },
730         {       CHAFSR_BERR,    CHAFSR_BERR_msg         },
731         /* These two do not update the AFAR. */
732         {       CHAFSR_IVC,     CHAFSR_IVC_msg          },
733         {       CHAFSR_IVU,     CHAFSR_IVU_msg          },
734         {       0,              NULL                    },
735 };
736 static const char CHPAFSR_DTO_msg[] =
737         "System bus unmapped error for prefetch/storequeue-read";
738 static const char CHPAFSR_DBERR_msg[] =
739         "System bus error for prefetch/storequeue-read";
740 static const char CHPAFSR_THCE_msg[] =
741         "Hardware corrected E-cache Tag ECC error";
742 static const char CHPAFSR_TSCE_msg[] =
743         "SW handled correctable E-cache Tag ECC error";
744 static const char CHPAFSR_TUE_msg[] =
745         "Uncorrectable E-cache Tag ECC error";
746 static const char CHPAFSR_DUE_msg[] =
747         "System bus uncorrectable data ECC error due to prefetch/store-fill";
748 static struct afsr_error_table __cheetah_plus_error_table[] = {
749         {       CHAFSR_PERR,    CHAFSR_PERR_msg         },
750         {       CHAFSR_IERR,    CHAFSR_IERR_msg         },
751         {       CHAFSR_ISAP,    CHAFSR_ISAP_msg         },
752         {       CHAFSR_UCU,     CHAFSR_UCU_msg          },
753         {       CHAFSR_UCC,     CHAFSR_UCC_msg          },
754         {       CHAFSR_UE,      CHAFSR_UE_msg           },
755         {       CHAFSR_EDU,     CHAFSR_EDU_msg          },
756         {       CHAFSR_EMU,     CHAFSR_EMU_msg          },
757         {       CHAFSR_WDU,     CHAFSR_WDU_msg          },
758         {       CHAFSR_CPU,     CHAFSR_CPU_msg          },
759         {       CHAFSR_CE,      CHAFSR_CE_msg           },
760         {       CHAFSR_EDC,     CHAFSR_EDC_msg          },
761         {       CHAFSR_EMC,     CHAFSR_EMC_msg          },
762         {       CHAFSR_WDC,     CHAFSR_WDC_msg          },
763         {       CHAFSR_CPC,     CHAFSR_CPC_msg          },
764         {       CHAFSR_TO,      CHAFSR_TO_msg           },
765         {       CHAFSR_BERR,    CHAFSR_BERR_msg         },
766         {       CHPAFSR_DTO,    CHPAFSR_DTO_msg         },
767         {       CHPAFSR_DBERR,  CHPAFSR_DBERR_msg       },
768         {       CHPAFSR_THCE,   CHPAFSR_THCE_msg        },
769         {       CHPAFSR_TSCE,   CHPAFSR_TSCE_msg        },
770         {       CHPAFSR_TUE,    CHPAFSR_TUE_msg         },
771         {       CHPAFSR_DUE,    CHPAFSR_DUE_msg         },
772         /* These two do not update the AFAR. */
773         {       CHAFSR_IVC,     CHAFSR_IVC_msg          },
774         {       CHAFSR_IVU,     CHAFSR_IVU_msg          },
775         {       0,              NULL                    },
776 };
777 static const char JPAFSR_JETO_msg[] =
778         "System interface protocol error, hw timeout caused";
779 static const char JPAFSR_SCE_msg[] =
780         "Parity error on system snoop results";
781 static const char JPAFSR_JEIC_msg[] =
782         "System interface protocol error, illegal command detected";
783 static const char JPAFSR_JEIT_msg[] =
784         "System interface protocol error, illegal ADTYPE detected";
785 static const char JPAFSR_OM_msg[] =
786         "Out of range memory error has occurred";
787 static const char JPAFSR_ETP_msg[] =
788         "Parity error on L2 cache tag SRAM";
789 static const char JPAFSR_UMS_msg[] =
790         "Error due to unsupported store";
791 static const char JPAFSR_RUE_msg[] =
792         "Uncorrectable ECC error from remote cache/memory";
793 static const char JPAFSR_RCE_msg[] =
794         "Correctable ECC error from remote cache/memory";
795 static const char JPAFSR_BP_msg[] =
796         "JBUS parity error on returned read data";
797 static const char JPAFSR_WBP_msg[] =
798         "JBUS parity error on data for writeback or block store";
799 static const char JPAFSR_FRC_msg[] =
800         "Foreign read to DRAM incurring correctable ECC error";
801 static const char JPAFSR_FRU_msg[] =
802         "Foreign read to DRAM incurring uncorrectable ECC error";
803 static struct afsr_error_table __jalapeno_error_table[] = {
804         {       JPAFSR_JETO,    JPAFSR_JETO_msg         },
805         {       JPAFSR_SCE,     JPAFSR_SCE_msg          },
806         {       JPAFSR_JEIC,    JPAFSR_JEIC_msg         },
807         {       JPAFSR_JEIT,    JPAFSR_JEIT_msg         },
808         {       CHAFSR_PERR,    CHAFSR_PERR_msg         },
809         {       CHAFSR_IERR,    CHAFSR_IERR_msg         },
810         {       CHAFSR_ISAP,    CHAFSR_ISAP_msg         },
811         {       CHAFSR_UCU,     CHAFSR_UCU_msg          },
812         {       CHAFSR_UCC,     CHAFSR_UCC_msg          },
813         {       CHAFSR_UE,      CHAFSR_UE_msg           },
814         {       CHAFSR_EDU,     CHAFSR_EDU_msg          },
815         {       JPAFSR_OM,      JPAFSR_OM_msg           },
816         {       CHAFSR_WDU,     CHAFSR_WDU_msg          },
817         {       CHAFSR_CPU,     CHAFSR_CPU_msg          },
818         {       CHAFSR_CE,      CHAFSR_CE_msg           },
819         {       CHAFSR_EDC,     CHAFSR_EDC_msg          },
820         {       JPAFSR_ETP,     JPAFSR_ETP_msg          },
821         {       CHAFSR_WDC,     CHAFSR_WDC_msg          },
822         {       CHAFSR_CPC,     CHAFSR_CPC_msg          },
823         {       CHAFSR_TO,      CHAFSR_TO_msg           },
824         {       CHAFSR_BERR,    CHAFSR_BERR_msg         },
825         {       JPAFSR_UMS,     JPAFSR_UMS_msg          },
826         {       JPAFSR_RUE,     JPAFSR_RUE_msg          },
827         {       JPAFSR_RCE,     JPAFSR_RCE_msg          },
828         {       JPAFSR_BP,      JPAFSR_BP_msg           },
829         {       JPAFSR_WBP,     JPAFSR_WBP_msg          },
830         {       JPAFSR_FRC,     JPAFSR_FRC_msg          },
831         {       JPAFSR_FRU,     JPAFSR_FRU_msg          },
832         /* These two do not update the AFAR. */
833         {       CHAFSR_IVU,     CHAFSR_IVU_msg          },
834         {       0,              NULL                    },
835 };
836 static struct afsr_error_table *cheetah_error_table;
837 static unsigned long cheetah_afsr_errors;
838
839 struct cheetah_err_info *cheetah_error_log;
840
841 static inline struct cheetah_err_info *cheetah_get_error_log(unsigned long afsr)
842 {
843         struct cheetah_err_info *p;
844         int cpu = smp_processor_id();
845
846         if (!cheetah_error_log)
847                 return NULL;
848
849         p = cheetah_error_log + (cpu * 2);
850         if ((afsr & CHAFSR_TL1) != 0UL)
851                 p++;
852
853         return p;
854 }
855
856 extern unsigned int tl0_icpe[], tl1_icpe[];
857 extern unsigned int tl0_dcpe[], tl1_dcpe[];
858 extern unsigned int tl0_fecc[], tl1_fecc[];
859 extern unsigned int tl0_cee[], tl1_cee[];
860 extern unsigned int tl0_iae[], tl1_iae[];
861 extern unsigned int tl0_dae[], tl1_dae[];
862 extern unsigned int cheetah_plus_icpe_trap_vector[], cheetah_plus_icpe_trap_vector_tl1[];
863 extern unsigned int cheetah_plus_dcpe_trap_vector[], cheetah_plus_dcpe_trap_vector_tl1[];
864 extern unsigned int cheetah_fecc_trap_vector[], cheetah_fecc_trap_vector_tl1[];
865 extern unsigned int cheetah_cee_trap_vector[], cheetah_cee_trap_vector_tl1[];
866 extern unsigned int cheetah_deferred_trap_vector[], cheetah_deferred_trap_vector_tl1[];
867
868 void __init cheetah_ecache_flush_init(void)
869 {
870         unsigned long largest_size, smallest_linesize, order, ver;
871         int i, sz;
872
873         /* Scan all cpu device tree nodes, note two values:
874          * 1) largest E-cache size
875          * 2) smallest E-cache line size
876          */
877         largest_size = 0UL;
878         smallest_linesize = ~0UL;
879
880         for (i = 0; i < NR_CPUS; i++) {
881                 unsigned long val;
882
883                 val = cpu_data(i).ecache_size;
884                 if (!val)
885                         continue;
886
887                 if (val > largest_size)
888                         largest_size = val;
889
890                 val = cpu_data(i).ecache_line_size;
891                 if (val < smallest_linesize)
892                         smallest_linesize = val;
893
894         }
895
896         if (largest_size == 0UL || smallest_linesize == ~0UL) {
897                 prom_printf("cheetah_ecache_flush_init: Cannot probe cpu E-cache "
898                             "parameters.\n");
899                 prom_halt();
900         }
901
902         ecache_flush_size = (2 * largest_size);
903         ecache_flush_linesize = smallest_linesize;
904
905         ecache_flush_physbase = find_ecache_flush_span(ecache_flush_size);
906
907         if (ecache_flush_physbase == ~0UL) {
908                 prom_printf("cheetah_ecache_flush_init: Cannot find %ld byte "
909                             "contiguous physical memory.\n",
910                             ecache_flush_size);
911                 prom_halt();
912         }
913
914         /* Now allocate error trap reporting scoreboard. */
915         sz = NR_CPUS * (2 * sizeof(struct cheetah_err_info));
916         for (order = 0; order < MAX_ORDER; order++) {
917                 if ((PAGE_SIZE << order) >= sz)
918                         break;
919         }
920         cheetah_error_log = (struct cheetah_err_info *)
921                 __get_free_pages(GFP_KERNEL, order);
922         if (!cheetah_error_log) {
923                 prom_printf("cheetah_ecache_flush_init: Failed to allocate "
924                             "error logging scoreboard (%d bytes).\n", sz);
925                 prom_halt();
926         }
927         memset(cheetah_error_log, 0, PAGE_SIZE << order);
928
929         /* Mark all AFSRs as invalid so that the trap handler will
930          * log new new information there.
931          */
932         for (i = 0; i < 2 * NR_CPUS; i++)
933                 cheetah_error_log[i].afsr = CHAFSR_INVALID;
934
935         __asm__ ("rdpr %%ver, %0" : "=r" (ver));
936         if ((ver >> 32) == __JALAPENO_ID ||
937             (ver >> 32) == __SERRANO_ID) {
938                 cheetah_error_table = &__jalapeno_error_table[0];
939                 cheetah_afsr_errors = JPAFSR_ERRORS;
940         } else if ((ver >> 32) == 0x003e0015) {
941                 cheetah_error_table = &__cheetah_plus_error_table[0];
942                 cheetah_afsr_errors = CHPAFSR_ERRORS;
943         } else {
944                 cheetah_error_table = &__cheetah_error_table[0];
945                 cheetah_afsr_errors = CHAFSR_ERRORS;
946         }
947
948         /* Now patch trap tables. */
949         memcpy(tl0_fecc, cheetah_fecc_trap_vector, (8 * 4));
950         memcpy(tl1_fecc, cheetah_fecc_trap_vector_tl1, (8 * 4));
951         memcpy(tl0_cee, cheetah_cee_trap_vector, (8 * 4));
952         memcpy(tl1_cee, cheetah_cee_trap_vector_tl1, (8 * 4));
953         memcpy(tl0_iae, cheetah_deferred_trap_vector, (8 * 4));
954         memcpy(tl1_iae, cheetah_deferred_trap_vector_tl1, (8 * 4));
955         memcpy(tl0_dae, cheetah_deferred_trap_vector, (8 * 4));
956         memcpy(tl1_dae, cheetah_deferred_trap_vector_tl1, (8 * 4));
957         if (tlb_type == cheetah_plus) {
958                 memcpy(tl0_dcpe, cheetah_plus_dcpe_trap_vector, (8 * 4));
959                 memcpy(tl1_dcpe, cheetah_plus_dcpe_trap_vector_tl1, (8 * 4));
960                 memcpy(tl0_icpe, cheetah_plus_icpe_trap_vector, (8 * 4));
961                 memcpy(tl1_icpe, cheetah_plus_icpe_trap_vector_tl1, (8 * 4));
962         }
963         flushi(PAGE_OFFSET);
964 }
965
966 static void cheetah_flush_ecache(void)
967 {
968         unsigned long flush_base = ecache_flush_physbase;
969         unsigned long flush_linesize = ecache_flush_linesize;
970         unsigned long flush_size = ecache_flush_size;
971
972         __asm__ __volatile__("1: subcc  %0, %4, %0\n\t"
973                              "   bne,pt %%xcc, 1b\n\t"
974                              "    ldxa  [%2 + %0] %3, %%g0\n\t"
975                              : "=&r" (flush_size)
976                              : "0" (flush_size), "r" (flush_base),
977                                "i" (ASI_PHYS_USE_EC), "r" (flush_linesize));
978 }
979
980 static void cheetah_flush_ecache_line(unsigned long physaddr)
981 {
982         unsigned long alias;
983
984         physaddr &= ~(8UL - 1UL);
985         physaddr = (ecache_flush_physbase +
986                     (physaddr & ((ecache_flush_size>>1UL) - 1UL)));
987         alias = physaddr + (ecache_flush_size >> 1UL);
988         __asm__ __volatile__("ldxa [%0] %2, %%g0\n\t"
989                              "ldxa [%1] %2, %%g0\n\t"
990                              "membar #Sync"
991                              : /* no outputs */
992                              : "r" (physaddr), "r" (alias),
993                                "i" (ASI_PHYS_USE_EC));
994 }
995
996 /* Unfortunately, the diagnostic access to the I-cache tags we need to
997  * use to clear the thing interferes with I-cache coherency transactions.
998  *
999  * So we must only flush the I-cache when it is disabled.
1000  */
1001 static void __cheetah_flush_icache(void)
1002 {
1003         unsigned int icache_size, icache_line_size;
1004         unsigned long addr;
1005
1006         icache_size = local_cpu_data().icache_size;
1007         icache_line_size = local_cpu_data().icache_line_size;
1008
1009         /* Clear the valid bits in all the tags. */
1010         for (addr = 0; addr < icache_size; addr += icache_line_size) {
1011                 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1012                                      "membar #Sync"
1013                                      : /* no outputs */
1014                                      : "r" (addr | (2 << 3)),
1015                                        "i" (ASI_IC_TAG));
1016         }
1017 }
1018
1019 static void cheetah_flush_icache(void)
1020 {
1021         unsigned long dcu_save;
1022
1023         /* Save current DCU, disable I-cache. */
1024         __asm__ __volatile__("ldxa [%%g0] %1, %0\n\t"
1025                              "or %0, %2, %%g1\n\t"
1026                              "stxa %%g1, [%%g0] %1\n\t"
1027                              "membar #Sync"
1028                              : "=r" (dcu_save)
1029                              : "i" (ASI_DCU_CONTROL_REG), "i" (DCU_IC)
1030                              : "g1");
1031
1032         __cheetah_flush_icache();
1033
1034         /* Restore DCU register */
1035         __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
1036                              "membar #Sync"
1037                              : /* no outputs */
1038                              : "r" (dcu_save), "i" (ASI_DCU_CONTROL_REG));
1039 }
1040
1041 static void cheetah_flush_dcache(void)
1042 {
1043         unsigned int dcache_size, dcache_line_size;
1044         unsigned long addr;
1045
1046         dcache_size = local_cpu_data().dcache_size;
1047         dcache_line_size = local_cpu_data().dcache_line_size;
1048
1049         for (addr = 0; addr < dcache_size; addr += dcache_line_size) {
1050                 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1051                                      "membar #Sync"
1052                                      : /* no outputs */
1053                                      : "r" (addr), "i" (ASI_DCACHE_TAG));
1054         }
1055 }
1056
1057 /* In order to make the even parity correct we must do two things.
1058  * First, we clear DC_data_parity and set DC_utag to an appropriate value.
1059  * Next, we clear out all 32-bytes of data for that line.  Data of
1060  * all-zero + tag parity value of zero == correct parity.
1061  */
1062 static void cheetah_plus_zap_dcache_parity(void)
1063 {
1064         unsigned int dcache_size, dcache_line_size;
1065         unsigned long addr;
1066
1067         dcache_size = local_cpu_data().dcache_size;
1068         dcache_line_size = local_cpu_data().dcache_line_size;
1069
1070         for (addr = 0; addr < dcache_size; addr += dcache_line_size) {
1071                 unsigned long tag = (addr >> 14);
1072                 unsigned long line;
1073
1074                 __asm__ __volatile__("membar    #Sync\n\t"
1075                                      "stxa      %0, [%1] %2\n\t"
1076                                      "membar    #Sync"
1077                                      : /* no outputs */
1078                                      : "r" (tag), "r" (addr),
1079                                        "i" (ASI_DCACHE_UTAG));
1080                 for (line = addr; line < addr + dcache_line_size; line += 8)
1081                         __asm__ __volatile__("membar    #Sync\n\t"
1082                                              "stxa      %%g0, [%0] %1\n\t"
1083                                              "membar    #Sync"
1084                                              : /* no outputs */
1085                                              : "r" (line),
1086                                                "i" (ASI_DCACHE_DATA));
1087         }
1088 }
1089
1090 /* Conversion tables used to frob Cheetah AFSR syndrome values into
1091  * something palatable to the memory controller driver get_unumber
1092  * routine.
1093  */
1094 #define MT0     137
1095 #define MT1     138
1096 #define MT2     139
1097 #define NONE    254
1098 #define MTC0    140
1099 #define MTC1    141
1100 #define MTC2    142
1101 #define MTC3    143
1102 #define C0      128
1103 #define C1      129
1104 #define C2      130
1105 #define C3      131
1106 #define C4      132
1107 #define C5      133
1108 #define C6      134
1109 #define C7      135
1110 #define C8      136
1111 #define M2      144
1112 #define M3      145
1113 #define M4      146
1114 #define M       147
1115 static unsigned char cheetah_ecc_syntab[] = {
1116 /*00*/NONE, C0, C1, M2, C2, M2, M3, 47, C3, M2, M2, 53, M2, 41, 29, M,
1117 /*01*/C4, M, M, 50, M2, 38, 25, M2, M2, 33, 24, M2, 11, M, M2, 16,
1118 /*02*/C5, M, M, 46, M2, 37, 19, M2, M, 31, 32, M, 7, M2, M2, 10,
1119 /*03*/M2, 40, 13, M2, 59, M, M2, 66, M, M2, M2, 0, M2, 67, 71, M,
1120 /*04*/C6, M, M, 43, M, 36, 18, M, M2, 49, 15, M, 63, M2, M2, 6,
1121 /*05*/M2, 44, 28, M2, M, M2, M2, 52, 68, M2, M2, 62, M2, M3, M3, M4,
1122 /*06*/M2, 26, 106, M2, 64, M, M2, 2, 120, M, M2, M3, M, M3, M3, M4,
1123 /*07*/116, M2, M2, M3, M2, M3, M, M4, M2, 58, 54, M2, M, M4, M4, M3,
1124 /*08*/C7, M2, M, 42, M, 35, 17, M2, M, 45, 14, M2, 21, M2, M2, 5,
1125 /*09*/M, 27, M, M, 99, M, M, 3, 114, M2, M2, 20, M2, M3, M3, M,
1126 /*0a*/M2, 23, 113, M2, 112, M2, M, 51, 95, M, M2, M3, M2, M3, M3, M2,
1127 /*0b*/103, M, M2, M3, M2, M3, M3, M4, M2, 48, M, M, 73, M2, M, M3,
1128 /*0c*/M2, 22, 110, M2, 109, M2, M, 9, 108, M2, M, M3, M2, M3, M3, M,
1129 /*0d*/102, M2, M, M, M2, M3, M3, M, M2, M3, M3, M2, M, M4, M, M3,
1130 /*0e*/98, M, M2, M3, M2, M, M3, M4, M2, M3, M3, M4, M3, M, M, M,
1131 /*0f*/M2, M3, M3, M, M3, M, M, M, 56, M4, M, M3, M4, M, M, M,
1132 /*10*/C8, M, M2, 39, M, 34, 105, M2, M, 30, 104, M, 101, M, M, 4,
1133 /*11*/M, M, 100, M, 83, M, M2, 12, 87, M, M, 57, M2, M, M3, M,
1134 /*12*/M2, 97, 82, M2, 78, M2, M2, 1, 96, M, M, M, M, M, M3, M2,
1135 /*13*/94, M, M2, M3, M2, M, M3, M, M2, M, 79, M, 69, M, M4, M,
1136 /*14*/M2, 93, 92, M, 91, M, M2, 8, 90, M2, M2, M, M, M, M, M4,
1137 /*15*/89, M, M, M3, M2, M3, M3, M, M, M, M3, M2, M3, M2, M, M3,
1138 /*16*/86, M, M2, M3, M2, M, M3, M, M2, M, M3, M, M3, M, M, M3,
1139 /*17*/M, M, M3, M2, M3, M2, M4, M, 60, M, M2, M3, M4, M, M, M2,
1140 /*18*/M2, 88, 85, M2, 84, M, M2, 55, 81, M2, M2, M3, M2, M3, M3, M4,
1141 /*19*/77, M, M, M, M2, M3, M, M, M2, M3, M3, M4, M3, M2, M, M,
1142 /*1a*/74, M, M2, M3, M, M, M3, M, M, M, M3, M, M3, M, M4, M3,
1143 /*1b*/M2, 70, 107, M4, 65, M2, M2, M, 127, M, M, M, M2, M3, M3, M,
1144 /*1c*/80, M2, M2, 72, M, 119, 118, M, M2, 126, 76, M, 125, M, M4, M3,
1145 /*1d*/M2, 115, 124, M, 75, M, M, M3, 61, M, M4, M, M4, M, M, M,
1146 /*1e*/M, 123, 122, M4, 121, M4, M, M3, 117, M2, M2, M3, M4, M3, M, M,
1147 /*1f*/111, M, M, M, M4, M3, M3, M, M, M, M3, M, M3, M2, M, M
1148 };
1149 static unsigned char cheetah_mtag_syntab[] = {
1150        NONE, MTC0,
1151        MTC1, NONE,
1152        MTC2, NONE,
1153        NONE, MT0,
1154        MTC3, NONE,
1155        NONE, MT1,
1156        NONE, MT2,
1157        NONE, NONE
1158 };
1159
1160 /* Return the highest priority error conditon mentioned. */
1161 static inline unsigned long cheetah_get_hipri(unsigned long afsr)
1162 {
1163         unsigned long tmp = 0;
1164         int i;
1165
1166         for (i = 0; cheetah_error_table[i].mask; i++) {
1167                 if ((tmp = (afsr & cheetah_error_table[i].mask)) != 0UL)
1168                         return tmp;
1169         }
1170         return tmp;
1171 }
1172
1173 static const char *cheetah_get_string(unsigned long bit)
1174 {
1175         int i;
1176
1177         for (i = 0; cheetah_error_table[i].mask; i++) {
1178                 if ((bit & cheetah_error_table[i].mask) != 0UL)
1179                         return cheetah_error_table[i].name;
1180         }
1181         return "???";
1182 }
1183
1184 static void cheetah_log_errors(struct pt_regs *regs, struct cheetah_err_info *info,
1185                                unsigned long afsr, unsigned long afar, int recoverable)
1186 {
1187         unsigned long hipri;
1188         char unum[256];
1189
1190         printk("%s" "ERROR(%d): Cheetah error trap taken afsr[%016lx] afar[%016lx] TL1(%d)\n",
1191                (recoverable ? KERN_WARNING : KERN_CRIT), smp_processor_id(),
1192                afsr, afar,
1193                (afsr & CHAFSR_TL1) ? 1 : 0);
1194         printk("%s" "ERROR(%d): TPC[%lx] TNPC[%lx] O7[%lx] TSTATE[%lx]\n",
1195                (recoverable ? KERN_WARNING : KERN_CRIT), smp_processor_id(),
1196                regs->tpc, regs->tnpc, regs->u_regs[UREG_I7], regs->tstate);
1197         printk("%s" "ERROR(%d): ",
1198                (recoverable ? KERN_WARNING : KERN_CRIT), smp_processor_id());
1199         printk("TPC<%pS>\n", (void *) regs->tpc);
1200         printk("%s" "ERROR(%d): M_SYND(%lx),  E_SYND(%lx)%s%s\n",
1201                (recoverable ? KERN_WARNING : KERN_CRIT), smp_processor_id(),
1202                (afsr & CHAFSR_M_SYNDROME) >> CHAFSR_M_SYNDROME_SHIFT,
1203                (afsr & CHAFSR_E_SYNDROME) >> CHAFSR_E_SYNDROME_SHIFT,
1204                (afsr & CHAFSR_ME) ? ", Multiple Errors" : "",
1205                (afsr & CHAFSR_PRIV) ? ", Privileged" : "");
1206         hipri = cheetah_get_hipri(afsr);
1207         printk("%s" "ERROR(%d): Highest priority error (%016lx) \"%s\"\n",
1208                (recoverable ? KERN_WARNING : KERN_CRIT), smp_processor_id(),
1209                hipri, cheetah_get_string(hipri));
1210
1211         /* Try to get unumber if relevant. */
1212 #define ESYND_ERRORS    (CHAFSR_IVC | CHAFSR_IVU | \
1213                          CHAFSR_CPC | CHAFSR_CPU | \
1214                          CHAFSR_UE  | CHAFSR_CE  | \
1215                          CHAFSR_EDC | CHAFSR_EDU  | \
1216                          CHAFSR_UCC | CHAFSR_UCU  | \
1217                          CHAFSR_WDU | CHAFSR_WDC)
1218 #define MSYND_ERRORS    (CHAFSR_EMC | CHAFSR_EMU)
1219         if (afsr & ESYND_ERRORS) {
1220                 int syndrome;
1221                 int ret;
1222
1223                 syndrome = (afsr & CHAFSR_E_SYNDROME) >> CHAFSR_E_SYNDROME_SHIFT;
1224                 syndrome = cheetah_ecc_syntab[syndrome];
1225                 ret = sprintf_dimm(syndrome, afar, unum, sizeof(unum));
1226                 if (ret != -1)
1227                         printk("%s" "ERROR(%d): AFAR E-syndrome [%s]\n",
1228                                (recoverable ? KERN_WARNING : KERN_CRIT),
1229                                smp_processor_id(), unum);
1230         } else if (afsr & MSYND_ERRORS) {
1231                 int syndrome;
1232                 int ret;
1233
1234                 syndrome = (afsr & CHAFSR_M_SYNDROME) >> CHAFSR_M_SYNDROME_SHIFT;
1235                 syndrome = cheetah_mtag_syntab[syndrome];
1236                 ret = sprintf_dimm(syndrome, afar, unum, sizeof(unum));
1237                 if (ret != -1)
1238                         printk("%s" "ERROR(%d): AFAR M-syndrome [%s]\n",
1239                                (recoverable ? KERN_WARNING : KERN_CRIT),
1240                                smp_processor_id(), unum);
1241         }
1242
1243         /* Now dump the cache snapshots. */
1244         printk("%s" "ERROR(%d): D-cache idx[%x] tag[%016llx] utag[%016llx] stag[%016llx]\n",
1245                (recoverable ? KERN_WARNING : KERN_CRIT), smp_processor_id(),
1246                (int) info->dcache_index,
1247                info->dcache_tag,
1248                info->dcache_utag,
1249                info->dcache_stag);
1250         printk("%s" "ERROR(%d): D-cache data0[%016llx] data1[%016llx] data2[%016llx] data3[%016llx]\n",
1251                (recoverable ? KERN_WARNING : KERN_CRIT), smp_processor_id(),
1252                info->dcache_data[0],
1253                info->dcache_data[1],
1254                info->dcache_data[2],
1255                info->dcache_data[3]);
1256         printk("%s" "ERROR(%d): I-cache idx[%x] tag[%016llx] utag[%016llx] stag[%016llx] "
1257                "u[%016llx] l[%016llx]\n",
1258                (recoverable ? KERN_WARNING : KERN_CRIT), smp_processor_id(),
1259                (int) info->icache_index,
1260                info->icache_tag,
1261                info->icache_utag,
1262                info->icache_stag,
1263                info->icache_upper,
1264                info->icache_lower);
1265         printk("%s" "ERROR(%d): I-cache INSN0[%016llx] INSN1[%016llx] INSN2[%016llx] INSN3[%016llx]\n",
1266                (recoverable ? KERN_WARNING : KERN_CRIT), smp_processor_id(),
1267                info->icache_data[0],
1268                info->icache_data[1],
1269                info->icache_data[2],
1270                info->icache_data[3]);
1271         printk("%s" "ERROR(%d): I-cache INSN4[%016llx] INSN5[%016llx] INSN6[%016llx] INSN7[%016llx]\n",
1272                (recoverable ? KERN_WARNING : KERN_CRIT), smp_processor_id(),
1273                info->icache_data[4],
1274                info->icache_data[5],
1275                info->icache_data[6],
1276                info->icache_data[7]);
1277         printk("%s" "ERROR(%d): E-cache idx[%x] tag[%016llx]\n",
1278                (recoverable ? KERN_WARNING : KERN_CRIT), smp_processor_id(),
1279                (int) info->ecache_index, info->ecache_tag);
1280         printk("%s" "ERROR(%d): E-cache data0[%016llx] data1[%016llx] data2[%016llx] data3[%016llx]\n",
1281                (recoverable ? KERN_WARNING : KERN_CRIT), smp_processor_id(),
1282                info->ecache_data[0],
1283                info->ecache_data[1],
1284                info->ecache_data[2],
1285                info->ecache_data[3]);
1286
1287         afsr = (afsr & ~hipri) & cheetah_afsr_errors;
1288         while (afsr != 0UL) {
1289                 unsigned long bit = cheetah_get_hipri(afsr);
1290
1291                 printk("%s" "ERROR: Multiple-error (%016lx) \"%s\"\n",
1292                        (recoverable ? KERN_WARNING : KERN_CRIT),
1293                        bit, cheetah_get_string(bit));
1294
1295                 afsr &= ~bit;
1296         }
1297
1298         if (!recoverable)
1299                 printk(KERN_CRIT "ERROR: This condition is not recoverable.\n");
1300 }
1301
1302 static int cheetah_recheck_errors(struct cheetah_err_info *logp)
1303 {
1304         unsigned long afsr, afar;
1305         int ret = 0;
1306
1307         __asm__ __volatile__("ldxa [%%g0] %1, %0\n\t"
1308                              : "=r" (afsr)
1309                              : "i" (ASI_AFSR));
1310         if ((afsr & cheetah_afsr_errors) != 0) {
1311                 if (logp != NULL) {
1312                         __asm__ __volatile__("ldxa [%%g0] %1, %0\n\t"
1313                                              : "=r" (afar)
1314                                              : "i" (ASI_AFAR));
1315                         logp->afsr = afsr;
1316                         logp->afar = afar;
1317                 }
1318                 ret = 1;
1319         }
1320         __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
1321                              "membar #Sync\n\t"
1322                              : : "r" (afsr), "i" (ASI_AFSR));
1323
1324         return ret;
1325 }
1326
1327 void cheetah_fecc_handler(struct pt_regs *regs, unsigned long afsr, unsigned long afar)
1328 {
1329         struct cheetah_err_info local_snapshot, *p;
1330         int recoverable;
1331
1332         /* Flush E-cache */
1333         cheetah_flush_ecache();
1334
1335         p = cheetah_get_error_log(afsr);
1336         if (!p) {
1337                 prom_printf("ERROR: Early Fast-ECC error afsr[%016lx] afar[%016lx]\n",
1338                             afsr, afar);
1339                 prom_printf("ERROR: CPU(%d) TPC[%016lx] TNPC[%016lx] TSTATE[%016lx]\n",
1340                             smp_processor_id(), regs->tpc, regs->tnpc, regs->tstate);
1341                 prom_halt();
1342         }
1343
1344         /* Grab snapshot of logged error. */
1345         memcpy(&local_snapshot, p, sizeof(local_snapshot));
1346
1347         /* If the current trap snapshot does not match what the
1348          * trap handler passed along into our args, big trouble.
1349          * In such a case, mark the local copy as invalid.
1350          *
1351          * Else, it matches and we mark the afsr in the non-local
1352          * copy as invalid so we may log new error traps there.
1353          */
1354         if (p->afsr != afsr || p->afar != afar)
1355                 local_snapshot.afsr = CHAFSR_INVALID;
1356         else
1357                 p->afsr = CHAFSR_INVALID;
1358
1359         cheetah_flush_icache();
1360         cheetah_flush_dcache();
1361
1362         /* Re-enable I-cache/D-cache */
1363         __asm__ __volatile__("ldxa [%%g0] %0, %%g1\n\t"
1364                              "or %%g1, %1, %%g1\n\t"
1365                              "stxa %%g1, [%%g0] %0\n\t"
1366                              "membar #Sync"
1367                              : /* no outputs */
1368                              : "i" (ASI_DCU_CONTROL_REG),
1369                                "i" (DCU_DC | DCU_IC)
1370                              : "g1");
1371
1372         /* Re-enable error reporting */
1373         __asm__ __volatile__("ldxa [%%g0] %0, %%g1\n\t"
1374                              "or %%g1, %1, %%g1\n\t"
1375                              "stxa %%g1, [%%g0] %0\n\t"
1376                              "membar #Sync"
1377                              : /* no outputs */
1378                              : "i" (ASI_ESTATE_ERROR_EN),
1379                                "i" (ESTATE_ERROR_NCEEN | ESTATE_ERROR_CEEN)
1380                              : "g1");
1381
1382         /* Decide if we can continue after handling this trap and
1383          * logging the error.
1384          */
1385         recoverable = 1;
1386         if (afsr & (CHAFSR_PERR | CHAFSR_IERR | CHAFSR_ISAP))
1387                 recoverable = 0;
1388
1389         /* Re-check AFSR/AFAR.  What we are looking for here is whether a new
1390          * error was logged while we had error reporting traps disabled.
1391          */
1392         if (cheetah_recheck_errors(&local_snapshot)) {
1393                 unsigned long new_afsr = local_snapshot.afsr;
1394
1395                 /* If we got a new asynchronous error, die... */
1396                 if (new_afsr & (CHAFSR_EMU | CHAFSR_EDU |
1397                                 CHAFSR_WDU | CHAFSR_CPU |
1398                                 CHAFSR_IVU | CHAFSR_UE |
1399                                 CHAFSR_BERR | CHAFSR_TO))
1400                         recoverable = 0;
1401         }
1402
1403         /* Log errors. */
1404         cheetah_log_errors(regs, &local_snapshot, afsr, afar, recoverable);
1405
1406         if (!recoverable)
1407                 panic("Irrecoverable Fast-ECC error trap.\n");
1408
1409         /* Flush E-cache to kick the error trap handlers out. */
1410         cheetah_flush_ecache();
1411 }
1412
1413 /* Try to fix a correctable error by pushing the line out from
1414  * the E-cache.  Recheck error reporting registers to see if the
1415  * problem is intermittent.
1416  */
1417 static int cheetah_fix_ce(unsigned long physaddr)
1418 {
1419         unsigned long orig_estate;
1420         unsigned long alias1, alias2;
1421         int ret;
1422
1423         /* Make sure correctable error traps are disabled. */
1424         __asm__ __volatile__("ldxa      [%%g0] %2, %0\n\t"
1425                              "andn      %0, %1, %%g1\n\t"
1426                              "stxa      %%g1, [%%g0] %2\n\t"
1427                              "membar    #Sync"
1428                              : "=&r" (orig_estate)
1429                              : "i" (ESTATE_ERROR_CEEN),
1430                                "i" (ASI_ESTATE_ERROR_EN)
1431                              : "g1");
1432
1433         /* We calculate alias addresses that will force the
1434          * cache line in question out of the E-cache.  Then
1435          * we bring it back in with an atomic instruction so
1436          * that we get it in some modified/exclusive state,
1437          * then we displace it again to try and get proper ECC
1438          * pushed back into the system.
1439          */
1440         physaddr &= ~(8UL - 1UL);
1441         alias1 = (ecache_flush_physbase +
1442                   (physaddr & ((ecache_flush_size >> 1) - 1)));
1443         alias2 = alias1 + (ecache_flush_size >> 1);
1444         __asm__ __volatile__("ldxa      [%0] %3, %%g0\n\t"
1445                              "ldxa      [%1] %3, %%g0\n\t"
1446                              "casxa     [%2] %3, %%g0, %%g0\n\t"
1447                              "ldxa      [%0] %3, %%g0\n\t"
1448                              "ldxa      [%1] %3, %%g0\n\t"
1449                              "membar    #Sync"
1450                              : /* no outputs */
1451                              : "r" (alias1), "r" (alias2),
1452                                "r" (physaddr), "i" (ASI_PHYS_USE_EC));
1453
1454         /* Did that trigger another error? */
1455         if (cheetah_recheck_errors(NULL)) {
1456                 /* Try one more time. */
1457                 __asm__ __volatile__("ldxa [%0] %1, %%g0\n\t"
1458                                      "membar #Sync"
1459                                      : : "r" (physaddr), "i" (ASI_PHYS_USE_EC));
1460                 if (cheetah_recheck_errors(NULL))
1461                         ret = 2;
1462                 else
1463                         ret = 1;
1464         } else {
1465                 /* No new error, intermittent problem. */
1466                 ret = 0;
1467         }
1468
1469         /* Restore error enables. */
1470         __asm__ __volatile__("stxa      %0, [%%g0] %1\n\t"
1471                              "membar    #Sync"
1472                              : : "r" (orig_estate), "i" (ASI_ESTATE_ERROR_EN));
1473
1474         return ret;
1475 }
1476
1477 /* Return non-zero if PADDR is a valid physical memory address. */
1478 static int cheetah_check_main_memory(unsigned long paddr)
1479 {
1480         unsigned long vaddr = PAGE_OFFSET + paddr;
1481
1482         if (vaddr > (unsigned long) high_memory)
1483                 return 0;
1484
1485         return kern_addr_valid(vaddr);
1486 }
1487
1488 void cheetah_cee_handler(struct pt_regs *regs, unsigned long afsr, unsigned long afar)
1489 {
1490         struct cheetah_err_info local_snapshot, *p;
1491         int recoverable, is_memory;
1492
1493         p = cheetah_get_error_log(afsr);
1494         if (!p) {
1495                 prom_printf("ERROR: Early CEE error afsr[%016lx] afar[%016lx]\n",
1496                             afsr, afar);
1497                 prom_printf("ERROR: CPU(%d) TPC[%016lx] TNPC[%016lx] TSTATE[%016lx]\n",
1498                             smp_processor_id(), regs->tpc, regs->tnpc, regs->tstate);
1499                 prom_halt();
1500         }
1501
1502         /* Grab snapshot of logged error. */
1503         memcpy(&local_snapshot, p, sizeof(local_snapshot));
1504
1505         /* If the current trap snapshot does not match what the
1506          * trap handler passed along into our args, big trouble.
1507          * In such a case, mark the local copy as invalid.
1508          *
1509          * Else, it matches and we mark the afsr in the non-local
1510          * copy as invalid so we may log new error traps there.
1511          */
1512         if (p->afsr != afsr || p->afar != afar)
1513                 local_snapshot.afsr = CHAFSR_INVALID;
1514         else
1515                 p->afsr = CHAFSR_INVALID;
1516
1517         is_memory = cheetah_check_main_memory(afar);
1518
1519         if (is_memory && (afsr & CHAFSR_CE) != 0UL) {
1520                 /* XXX Might want to log the results of this operation
1521                  * XXX somewhere... -DaveM
1522                  */
1523                 cheetah_fix_ce(afar);
1524         }
1525
1526         {
1527                 int flush_all, flush_line;
1528
1529                 flush_all = flush_line = 0;
1530                 if ((afsr & CHAFSR_EDC) != 0UL) {
1531                         if ((afsr & cheetah_afsr_errors) == CHAFSR_EDC)
1532                                 flush_line = 1;
1533                         else
1534                                 flush_all = 1;
1535                 } else if ((afsr & CHAFSR_CPC) != 0UL) {
1536                         if ((afsr & cheetah_afsr_errors) == CHAFSR_CPC)
1537                                 flush_line = 1;
1538                         else
1539                                 flush_all = 1;
1540                 }
1541
1542                 /* Trap handler only disabled I-cache, flush it. */
1543                 cheetah_flush_icache();
1544
1545                 /* Re-enable I-cache */
1546                 __asm__ __volatile__("ldxa [%%g0] %0, %%g1\n\t"
1547                                      "or %%g1, %1, %%g1\n\t"
1548                                      "stxa %%g1, [%%g0] %0\n\t"
1549                                      "membar #Sync"
1550                                      : /* no outputs */
1551                                      : "i" (ASI_DCU_CONTROL_REG),
1552                                      "i" (DCU_IC)
1553                                      : "g1");
1554
1555                 if (flush_all)
1556                         cheetah_flush_ecache();
1557                 else if (flush_line)
1558                         cheetah_flush_ecache_line(afar);
1559         }
1560
1561         /* Re-enable error reporting */
1562         __asm__ __volatile__("ldxa [%%g0] %0, %%g1\n\t"
1563                              "or %%g1, %1, %%g1\n\t"
1564                              "stxa %%g1, [%%g0] %0\n\t"
1565                              "membar #Sync"
1566                              : /* no outputs */
1567                              : "i" (ASI_ESTATE_ERROR_EN),
1568                                "i" (ESTATE_ERROR_CEEN)
1569                              : "g1");
1570
1571         /* Decide if we can continue after handling this trap and
1572          * logging the error.
1573          */
1574         recoverable = 1;
1575         if (afsr & (CHAFSR_PERR | CHAFSR_IERR | CHAFSR_ISAP))
1576                 recoverable = 0;
1577
1578         /* Re-check AFSR/AFAR */
1579         (void) cheetah_recheck_errors(&local_snapshot);
1580
1581         /* Log errors. */
1582         cheetah_log_errors(regs, &local_snapshot, afsr, afar, recoverable);
1583
1584         if (!recoverable)
1585                 panic("Irrecoverable Correctable-ECC error trap.\n");
1586 }
1587
1588 void cheetah_deferred_handler(struct pt_regs *regs, unsigned long afsr, unsigned long afar)
1589 {
1590         struct cheetah_err_info local_snapshot, *p;
1591         int recoverable, is_memory;
1592
1593 #ifdef CONFIG_PCI
1594         /* Check for the special PCI poke sequence. */
1595         if (pci_poke_in_progress && pci_poke_cpu == smp_processor_id()) {
1596                 cheetah_flush_icache();
1597                 cheetah_flush_dcache();
1598
1599                 /* Re-enable I-cache/D-cache */
1600                 __asm__ __volatile__("ldxa [%%g0] %0, %%g1\n\t"
1601                                      "or %%g1, %1, %%g1\n\t"
1602                                      "stxa %%g1, [%%g0] %0\n\t"
1603                                      "membar #Sync"
1604                                      : /* no outputs */
1605                                      : "i" (ASI_DCU_CONTROL_REG),
1606                                        "i" (DCU_DC | DCU_IC)
1607                                      : "g1");
1608
1609                 /* Re-enable error reporting */
1610                 __asm__ __volatile__("ldxa [%%g0] %0, %%g1\n\t"
1611                                      "or %%g1, %1, %%g1\n\t"
1612                                      "stxa %%g1, [%%g0] %0\n\t"
1613                                      "membar #Sync"
1614                                      : /* no outputs */
1615                                      : "i" (ASI_ESTATE_ERROR_EN),
1616                                        "i" (ESTATE_ERROR_NCEEN | ESTATE_ERROR_CEEN)
1617                                      : "g1");
1618
1619                 (void) cheetah_recheck_errors(NULL);
1620
1621                 pci_poke_faulted = 1;
1622                 regs->tpc += 4;
1623                 regs->tnpc = regs->tpc + 4;
1624                 return;
1625         }
1626 #endif
1627
1628         p = cheetah_get_error_log(afsr);
1629         if (!p) {
1630                 prom_printf("ERROR: Early deferred error afsr[%016lx] afar[%016lx]\n",
1631                             afsr, afar);
1632                 prom_printf("ERROR: CPU(%d) TPC[%016lx] TNPC[%016lx] TSTATE[%016lx]\n",
1633                             smp_processor_id(), regs->tpc, regs->tnpc, regs->tstate);
1634                 prom_halt();
1635         }
1636
1637         /* Grab snapshot of logged error. */
1638         memcpy(&local_snapshot, p, sizeof(local_snapshot));
1639
1640         /* If the current trap snapshot does not match what the
1641          * trap handler passed along into our args, big trouble.
1642          * In such a case, mark the local copy as invalid.
1643          *
1644          * Else, it matches and we mark the afsr in the non-local
1645          * copy as invalid so we may log new error traps there.
1646          */
1647         if (p->afsr != afsr || p->afar != afar)
1648                 local_snapshot.afsr = CHAFSR_INVALID;
1649         else
1650                 p->afsr = CHAFSR_INVALID;
1651
1652         is_memory = cheetah_check_main_memory(afar);
1653
1654         {
1655                 int flush_all, flush_line;
1656
1657                 flush_all = flush_line = 0;
1658                 if ((afsr & CHAFSR_EDU) != 0UL) {
1659                         if ((afsr & cheetah_afsr_errors) == CHAFSR_EDU)
1660                                 flush_line = 1;
1661                         else
1662                                 flush_all = 1;
1663                 } else if ((afsr & CHAFSR_BERR) != 0UL) {
1664                         if ((afsr & cheetah_afsr_errors) == CHAFSR_BERR)
1665                                 flush_line = 1;
1666                         else
1667                                 flush_all = 1;
1668                 }
1669
1670                 cheetah_flush_icache();
1671                 cheetah_flush_dcache();
1672
1673                 /* Re-enable I/D caches */
1674                 __asm__ __volatile__("ldxa [%%g0] %0, %%g1\n\t"
1675                                      "or %%g1, %1, %%g1\n\t"
1676                                      "stxa %%g1, [%%g0] %0\n\t"
1677                                      "membar #Sync"
1678                                      : /* no outputs */
1679                                      : "i" (ASI_DCU_CONTROL_REG),
1680                                      "i" (DCU_IC | DCU_DC)
1681                                      : "g1");
1682
1683                 if (flush_all)
1684                         cheetah_flush_ecache();
1685                 else if (flush_line)
1686                         cheetah_flush_ecache_line(afar);
1687         }
1688
1689         /* Re-enable error reporting */
1690         __asm__ __volatile__("ldxa [%%g0] %0, %%g1\n\t"
1691                              "or %%g1, %1, %%g1\n\t"
1692                              "stxa %%g1, [%%g0] %0\n\t"
1693                              "membar #Sync"
1694                              : /* no outputs */
1695                              : "i" (ASI_ESTATE_ERROR_EN),
1696                              "i" (ESTATE_ERROR_NCEEN | ESTATE_ERROR_CEEN)
1697                              : "g1");
1698
1699         /* Decide if we can continue after handling this trap and
1700          * logging the error.
1701          */
1702         recoverable = 1;
1703         if (afsr & (CHAFSR_PERR | CHAFSR_IERR | CHAFSR_ISAP))
1704                 recoverable = 0;
1705
1706         /* Re-check AFSR/AFAR.  What we are looking for here is whether a new
1707          * error was logged while we had error reporting traps disabled.
1708          */
1709         if (cheetah_recheck_errors(&local_snapshot)) {
1710                 unsigned long new_afsr = local_snapshot.afsr;
1711
1712                 /* If we got a new asynchronous error, die... */
1713                 if (new_afsr & (CHAFSR_EMU | CHAFSR_EDU |
1714                                 CHAFSR_WDU | CHAFSR_CPU |
1715                                 CHAFSR_IVU | CHAFSR_UE |
1716                                 CHAFSR_BERR | CHAFSR_TO))
1717                         recoverable = 0;
1718         }
1719
1720         /* Log errors. */
1721         cheetah_log_errors(regs, &local_snapshot, afsr, afar, recoverable);
1722
1723         /* "Recoverable" here means we try to yank the page from ever
1724          * being newly used again.  This depends upon a few things:
1725          * 1) Must be main memory, and AFAR must be valid.
1726          * 2) If we trapped from user, OK.
1727          * 3) Else, if we trapped from kernel we must find exception
1728          *    table entry (ie. we have to have been accessing user
1729          *    space).
1730          *
1731          * If AFAR is not in main memory, or we trapped from kernel
1732          * and cannot find an exception table entry, it is unacceptable
1733          * to try and continue.
1734          */
1735         if (recoverable && is_memory) {
1736                 if ((regs->tstate & TSTATE_PRIV) == 0UL) {
1737                         /* OK, usermode access. */
1738                         recoverable = 1;
1739                 } else {
1740                         const struct exception_table_entry *entry;
1741
1742                         entry = search_exception_tables(regs->tpc);
1743                         if (entry) {
1744                                 /* OK, kernel access to userspace. */
1745                                 recoverable = 1;
1746
1747                         } else {
1748                                 /* BAD, privileged state is corrupted. */
1749                                 recoverable = 0;
1750                         }
1751
1752                         if (recoverable) {
1753                                 if (pfn_valid(afar >> PAGE_SHIFT))
1754                                         get_page(pfn_to_page(afar >> PAGE_SHIFT));
1755                                 else
1756                                         recoverable = 0;
1757
1758                                 /* Only perform fixup if we still have a
1759                                  * recoverable condition.
1760                                  */
1761                                 if (recoverable) {
1762                                         regs->tpc = entry->fixup;
1763                                         regs->tnpc = regs->tpc + 4;
1764                                 }
1765                         }
1766                 }
1767         } else {
1768                 recoverable = 0;
1769         }
1770
1771         if (!recoverable)
1772                 panic("Irrecoverable deferred error trap.\n");
1773 }
1774
1775 /* Handle a D/I cache parity error trap.  TYPE is encoded as:
1776  *
1777  * Bit0:        0=dcache,1=icache
1778  * Bit1:        0=recoverable,1=unrecoverable
1779  *
1780  * The hardware has disabled both the I-cache and D-cache in
1781  * the %dcr register.  
1782  */
1783 void cheetah_plus_parity_error(int type, struct pt_regs *regs)
1784 {
1785         if (type & 0x1)
1786                 __cheetah_flush_icache();
1787         else
1788                 cheetah_plus_zap_dcache_parity();
1789         cheetah_flush_dcache();
1790
1791         /* Re-enable I-cache/D-cache */
1792         __asm__ __volatile__("ldxa [%%g0] %0, %%g1\n\t"
1793                              "or %%g1, %1, %%g1\n\t"
1794                              "stxa %%g1, [%%g0] %0\n\t"
1795                              "membar #Sync"
1796                              : /* no outputs */
1797                              : "i" (ASI_DCU_CONTROL_REG),
1798                                "i" (DCU_DC | DCU_IC)
1799                              : "g1");
1800
1801         if (type & 0x2) {
1802                 printk(KERN_EMERG "CPU[%d]: Cheetah+ %c-cache parity error at TPC[%016lx]\n",
1803                        smp_processor_id(),
1804                        (type & 0x1) ? 'I' : 'D',
1805                        regs->tpc);
1806                 printk(KERN_EMERG "TPC<%pS>\n", (void *) regs->tpc);
1807                 panic("Irrecoverable Cheetah+ parity error.");
1808         }
1809
1810         printk(KERN_WARNING "CPU[%d]: Cheetah+ %c-cache parity error at TPC[%016lx]\n",
1811                smp_processor_id(),
1812                (type & 0x1) ? 'I' : 'D',
1813                regs->tpc);
1814         printk(KERN_WARNING "TPC<%pS>\n", (void *) regs->tpc);
1815 }
1816
1817 struct sun4v_error_entry {
1818         /* Unique error handle */
1819 /*0x00*/u64             err_handle;
1820
1821         /* %stick value at the time of the error */
1822 /*0x08*/u64             err_stick;
1823
1824 /*0x10*/u8              reserved_1[3];
1825
1826         /* Error type */
1827 /*0x13*/u8              err_type;
1828 #define SUN4V_ERR_TYPE_UNDEFINED        0
1829 #define SUN4V_ERR_TYPE_UNCORRECTED_RES  1
1830 #define SUN4V_ERR_TYPE_PRECISE_NONRES   2
1831 #define SUN4V_ERR_TYPE_DEFERRED_NONRES  3
1832 #define SUN4V_ERR_TYPE_SHUTDOWN_RQST    4
1833 #define SUN4V_ERR_TYPE_DUMP_CORE        5
1834 #define SUN4V_ERR_TYPE_SP_STATE_CHANGE  6
1835 #define SUN4V_ERR_TYPE_NUM              7
1836
1837         /* Error attributes */
1838 /*0x14*/u32             err_attrs;
1839 #define SUN4V_ERR_ATTRS_PROCESSOR       0x00000001
1840 #define SUN4V_ERR_ATTRS_MEMORY          0x00000002
1841 #define SUN4V_ERR_ATTRS_PIO             0x00000004
1842 #define SUN4V_ERR_ATTRS_INT_REGISTERS   0x00000008
1843 #define SUN4V_ERR_ATTRS_FPU_REGISTERS   0x00000010
1844 #define SUN4V_ERR_ATTRS_SHUTDOWN_RQST   0x00000020
1845 #define SUN4V_ERR_ATTRS_ASR             0x00000040
1846 #define SUN4V_ERR_ATTRS_ASI             0x00000080
1847 #define SUN4V_ERR_ATTRS_PRIV_REG        0x00000100
1848 #define SUN4V_ERR_ATTRS_SPSTATE_MSK     0x00000600
1849 #define SUN4V_ERR_ATTRS_SPSTATE_SHFT    9
1850 #define SUN4V_ERR_ATTRS_MODE_MSK        0x03000000
1851 #define SUN4V_ERR_ATTRS_MODE_SHFT       24
1852 #define SUN4V_ERR_ATTRS_RES_QUEUE_FULL  0x80000000
1853
1854 #define SUN4V_ERR_SPSTATE_FAULTED       0
1855 #define SUN4V_ERR_SPSTATE_AVAILABLE     1
1856 #define SUN4V_ERR_SPSTATE_NOT_PRESENT   2
1857
1858 #define SUN4V_ERR_MODE_USER             1
1859 #define SUN4V_ERR_MODE_PRIV             2
1860
1861         /* Real address of the memory region or PIO transaction */
1862 /*0x18*/u64             err_raddr;
1863
1864         /* Size of the operation triggering the error, in bytes */
1865 /*0x20*/u32             err_size;
1866
1867         /* ID of the CPU */
1868 /*0x24*/u16             err_cpu;
1869
1870         /* Grace periof for shutdown, in seconds */
1871 /*0x26*/u16             err_secs;
1872
1873         /* Value of the %asi register */
1874 /*0x28*/u8              err_asi;
1875
1876 /*0x29*/u8              reserved_2;
1877
1878         /* Value of the ASR register number */
1879 /*0x2a*/u16             err_asr;
1880 #define SUN4V_ERR_ASR_VALID             0x8000
1881
1882 /*0x2c*/u32             reserved_3;
1883 /*0x30*/u64             reserved_4;
1884 /*0x38*/u64             reserved_5;
1885 };
1886
1887 static atomic_t sun4v_resum_oflow_cnt = ATOMIC_INIT(0);
1888 static atomic_t sun4v_nonresum_oflow_cnt = ATOMIC_INIT(0);
1889
1890 static const char *sun4v_err_type_to_str(u8 type)
1891 {
1892         static const char *types[SUN4V_ERR_TYPE_NUM] = {
1893                 "undefined",
1894                 "uncorrected resumable",
1895                 "precise nonresumable",
1896                 "deferred nonresumable",
1897                 "shutdown request",
1898                 "dump core",
1899                 "SP state change",
1900         };
1901
1902         if (type < SUN4V_ERR_TYPE_NUM)
1903                 return types[type];
1904
1905         return "unknown";
1906 }
1907
1908 static void sun4v_emit_err_attr_strings(u32 attrs)
1909 {
1910         static const char *attr_names[] = {
1911                 "processor",
1912                 "memory",
1913                 "PIO",
1914                 "int-registers",
1915                 "fpu-registers",
1916                 "shutdown-request",
1917                 "ASR",
1918                 "ASI",
1919                 "priv-reg",
1920         };
1921         static const char *sp_states[] = {
1922                 "sp-faulted",
1923                 "sp-available",
1924                 "sp-not-present",
1925                 "sp-state-reserved",
1926         };
1927         static const char *modes[] = {
1928                 "mode-reserved0",
1929                 "user",
1930                 "priv",
1931                 "mode-reserved1",
1932         };
1933         u32 sp_state, mode;
1934         int i;
1935
1936         for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
1937                 if (attrs & (1U << i)) {
1938                         const char *s = attr_names[i];
1939
1940                         pr_cont("%s ", s);
1941                 }
1942         }
1943
1944         sp_state = ((attrs & SUN4V_ERR_ATTRS_SPSTATE_MSK) >>
1945                     SUN4V_ERR_ATTRS_SPSTATE_SHFT);
1946         pr_cont("%s ", sp_states[sp_state]);
1947
1948         mode = ((attrs & SUN4V_ERR_ATTRS_MODE_MSK) >>
1949                 SUN4V_ERR_ATTRS_MODE_SHFT);
1950         pr_cont("%s ", modes[mode]);
1951
1952         if (attrs & SUN4V_ERR_ATTRS_RES_QUEUE_FULL)
1953                 pr_cont("res-queue-full ");
1954 }
1955
1956 /* When the report contains a real-address of "-1" it means that the
1957  * hardware did not provide the address.  So we compute the effective
1958  * address of the load or store instruction at regs->tpc and report
1959  * that.  Usually when this happens it's a PIO and in such a case we
1960  * are using physical addresses with bypass ASIs anyways, so what we
1961  * report here is exactly what we want.
1962  */
1963 static void sun4v_report_real_raddr(const char *pfx, struct pt_regs *regs)
1964 {
1965         unsigned int insn;
1966         u64 addr;
1967
1968         if (!(regs->tstate & TSTATE_PRIV))
1969                 return;
1970
1971         insn = *(unsigned int *) regs->tpc;
1972
1973         addr = compute_effective_address(regs, insn, 0);
1974
1975         printk("%s: insn effective address [0x%016llx]\n",
1976                pfx, addr);
1977 }
1978
1979 static void sun4v_log_error(struct pt_regs *regs, struct sun4v_error_entry *ent,
1980                             int cpu, const char *pfx, atomic_t *ocnt)
1981 {
1982         u64 *raw_ptr = (u64 *) ent;
1983         u32 attrs;
1984         int cnt;
1985
1986         printk("%s: Reporting on cpu %d\n", pfx, cpu);
1987         printk("%s: TPC [0x%016lx] <%pS>\n",
1988                pfx, regs->tpc, (void *) regs->tpc);
1989
1990         printk("%s: RAW [%016llx:%016llx:%016llx:%016llx\n",
1991                pfx, raw_ptr[0], raw_ptr[1], raw_ptr[2], raw_ptr[3]);
1992         printk("%s:      %016llx:%016llx:%016llx:%016llx]\n",
1993                pfx, raw_ptr[4], raw_ptr[5], raw_ptr[6], raw_ptr[7]);
1994
1995         printk("%s: handle [0x%016llx] stick [0x%016llx]\n",
1996                pfx, ent->err_handle, ent->err_stick);
1997
1998         printk("%s: type [%s]\n", pfx, sun4v_err_type_to_str(ent->err_type));
1999
2000         attrs = ent->err_attrs;
2001         printk("%s: attrs [0x%08x] < ", pfx, attrs);
2002         sun4v_emit_err_attr_strings(attrs);
2003         pr_cont(">\n");
2004
2005         /* Various fields in the error report are only valid if
2006          * certain attribute bits are set.
2007          */
2008         if (attrs & (SUN4V_ERR_ATTRS_MEMORY |
2009                      SUN4V_ERR_ATTRS_PIO |
2010                      SUN4V_ERR_ATTRS_ASI)) {
2011                 printk("%s: raddr [0x%016llx]\n", pfx, ent->err_raddr);
2012
2013                 if (ent->err_raddr == ~(u64)0)
2014                         sun4v_report_real_raddr(pfx, regs);
2015         }
2016
2017         if (attrs & (SUN4V_ERR_ATTRS_MEMORY | SUN4V_ERR_ATTRS_ASI))
2018                 printk("%s: size [0x%x]\n", pfx, ent->err_size);
2019
2020         if (attrs & (SUN4V_ERR_ATTRS_PROCESSOR |
2021                      SUN4V_ERR_ATTRS_INT_REGISTERS |
2022                      SUN4V_ERR_ATTRS_FPU_REGISTERS |
2023                      SUN4V_ERR_ATTRS_PRIV_REG))
2024                 printk("%s: cpu[%u]\n", pfx, ent->err_cpu);
2025
2026         if (attrs & SUN4V_ERR_ATTRS_ASI)
2027                 printk("%s: asi [0x%02x]\n", pfx, ent->err_asi);
2028
2029         if ((attrs & (SUN4V_ERR_ATTRS_INT_REGISTERS |
2030                       SUN4V_ERR_ATTRS_FPU_REGISTERS |
2031                       SUN4V_ERR_ATTRS_PRIV_REG)) &&
2032             (ent->err_asr & SUN4V_ERR_ASR_VALID) != 0)
2033                 printk("%s: reg [0x%04x]\n",
2034                        pfx, ent->err_asr & ~SUN4V_ERR_ASR_VALID);
2035
2036         show_regs(regs);
2037
2038         if ((cnt = atomic_read(ocnt)) != 0) {
2039                 atomic_set(ocnt, 0);
2040                 wmb();
2041                 printk("%s: Queue overflowed %d times.\n",
2042                        pfx, cnt);
2043         }
2044 }
2045
2046 /* We run with %pil set to PIL_NORMAL_MAX and PSTATE_IE enabled in %pstate.
2047  * Log the event and clear the first word of the entry.
2048  */
2049 void sun4v_resum_error(struct pt_regs *regs, unsigned long offset)
2050 {
2051         enum ctx_state prev_state = exception_enter();
2052         struct sun4v_error_entry *ent, local_copy;
2053         struct trap_per_cpu *tb;
2054         unsigned long paddr;
2055         int cpu;
2056
2057         cpu = get_cpu();
2058
2059         tb = &trap_block[cpu];
2060         paddr = tb->resum_kernel_buf_pa + offset;
2061         ent = __va(paddr);
2062
2063         memcpy(&local_copy, ent, sizeof(struct sun4v_error_entry));
2064
2065         /* We have a local copy now, so release the entry.  */
2066         ent->err_handle = 0;
2067         wmb();
2068
2069         put_cpu();
2070
2071         if (local_copy.err_type == SUN4V_ERR_TYPE_SHUTDOWN_RQST) {
2072                 /* We should really take the seconds field of
2073                  * the error report and use it for the shutdown
2074                  * invocation, but for now do the same thing we
2075                  * do for a DS shutdown request.
2076                  */
2077                 pr_info("Shutdown request, %u seconds...\n",
2078                         local_copy.err_secs);
2079                 orderly_poweroff(true);
2080                 goto out;
2081         }
2082
2083         sun4v_log_error(regs, &local_copy, cpu,
2084                         KERN_ERR "RESUMABLE ERROR",
2085                         &sun4v_resum_oflow_cnt);
2086 out:
2087         exception_exit(prev_state);
2088 }
2089
2090 /* If we try to printk() we'll probably make matters worse, by trying
2091  * to retake locks this cpu already holds or causing more errors. So
2092  * just bump a counter, and we'll report these counter bumps above.
2093  */
2094 void sun4v_resum_overflow(struct pt_regs *regs)
2095 {
2096         atomic_inc(&sun4v_resum_oflow_cnt);
2097 }
2098
2099 /* Given a set of registers, get the virtual addressi that was being accessed
2100  * by the faulting instructions at tpc.
2101  */
2102 static unsigned long sun4v_get_vaddr(struct pt_regs *regs)
2103 {
2104         unsigned int insn;
2105
2106         if (!copy_from_user(&insn, (void __user *)regs->tpc, 4)) {
2107                 return compute_effective_address(regs, insn,
2108                                                  (insn >> 25) & 0x1f);
2109         }
2110         return 0;
2111 }
2112
2113 /* Attempt to handle non-resumable errors generated from userspace.
2114  * Returns true if the signal was handled, false otherwise.
2115  */
2116 bool sun4v_nonresum_error_user_handled(struct pt_regs *regs,
2117                                   struct sun4v_error_entry *ent) {
2118
2119         unsigned int attrs = ent->err_attrs;
2120
2121         if (attrs & SUN4V_ERR_ATTRS_MEMORY) {
2122                 unsigned long addr = ent->err_raddr;
2123                 siginfo_t info;
2124
2125                 if (addr == ~(u64)0) {
2126                         /* This seems highly unlikely to ever occur */
2127                         pr_emerg("SUN4V NON-RECOVERABLE ERROR: Memory error detected in unknown location!\n");
2128                 } else {
2129                         unsigned long page_cnt = DIV_ROUND_UP(ent->err_size,
2130                                                               PAGE_SIZE);
2131
2132                         /* Break the unfortunate news. */
2133                         pr_emerg("SUN4V NON-RECOVERABLE ERROR: Memory failed at %016lX\n",
2134                                  addr);
2135                         pr_emerg("SUN4V NON-RECOVERABLE ERROR:   Claiming %lu ages.\n",
2136                                  page_cnt);
2137
2138                         while (page_cnt-- > 0) {
2139                                 if (pfn_valid(addr >> PAGE_SHIFT))
2140                                         get_page(pfn_to_page(addr >> PAGE_SHIFT));
2141                                 addr += PAGE_SIZE;
2142                         }
2143                 }
2144                 info.si_signo = SIGKILL;
2145                 info.si_errno = 0;
2146                 info.si_trapno = 0;
2147                 force_sig_info(info.si_signo, &info, current);
2148
2149                 return true;
2150         }
2151         if (attrs & SUN4V_ERR_ATTRS_PIO) {
2152                 siginfo_t info;
2153
2154                 info.si_signo = SIGBUS;
2155                 info.si_code = BUS_ADRERR;
2156                 info.si_addr = (void __user *)sun4v_get_vaddr(regs);
2157                 force_sig_info(info.si_signo, &info, current);
2158
2159                 return true;
2160         }
2161
2162         /* Default to doing nothing */
2163         return false;
2164 }
2165
2166 /* We run with %pil set to PIL_NORMAL_MAX and PSTATE_IE enabled in %pstate.
2167  * Log the event, clear the first word of the entry, and die.
2168  */
2169 void sun4v_nonresum_error(struct pt_regs *regs, unsigned long offset)
2170 {
2171         struct sun4v_error_entry *ent, local_copy;
2172         struct trap_per_cpu *tb;
2173         unsigned long paddr;
2174         int cpu;
2175
2176         cpu = get_cpu();
2177
2178         tb = &trap_block[cpu];
2179         paddr = tb->nonresum_kernel_buf_pa + offset;
2180         ent = __va(paddr);
2181
2182         memcpy(&local_copy, ent, sizeof(struct sun4v_error_entry));
2183
2184         /* We have a local copy now, so release the entry.  */
2185         ent->err_handle = 0;
2186         wmb();
2187
2188         put_cpu();
2189
2190         if (!(regs->tstate & TSTATE_PRIV) &&
2191             sun4v_nonresum_error_user_handled(regs, &local_copy)) {
2192                 /* DON'T PANIC: This userspace error was handled. */
2193                 return;
2194         }
2195
2196 #ifdef CONFIG_PCI
2197         /* Check for the special PCI poke sequence. */
2198         if (pci_poke_in_progress && pci_poke_cpu == cpu) {
2199                 pci_poke_faulted = 1;
2200                 regs->tpc += 4;
2201                 regs->tnpc = regs->tpc + 4;
2202                 return;
2203         }
2204 #endif
2205
2206         sun4v_log_error(regs, &local_copy, cpu,
2207                         KERN_EMERG "NON-RESUMABLE ERROR",
2208                         &sun4v_nonresum_oflow_cnt);
2209
2210         panic("Non-resumable error.");
2211 }
2212
2213 /* If we try to printk() we'll probably make matters worse, by trying
2214  * to retake locks this cpu already holds or causing more errors. So
2215  * just bump a counter, and we'll report these counter bumps above.
2216  */
2217 void sun4v_nonresum_overflow(struct pt_regs *regs)
2218 {
2219         /* XXX Actually even this can make not that much sense.  Perhaps
2220          * XXX we should just pull the plug and panic directly from here?
2221          */
2222         atomic_inc(&sun4v_nonresum_oflow_cnt);
2223 }
2224
2225 static void sun4v_tlb_error(struct pt_regs *regs)
2226 {
2227         die_if_kernel("TLB/TSB error", regs);
2228 }
2229
2230 unsigned long sun4v_err_itlb_vaddr;
2231 unsigned long sun4v_err_itlb_ctx;
2232 unsigned long sun4v_err_itlb_pte;
2233 unsigned long sun4v_err_itlb_error;
2234
2235 void sun4v_itlb_error_report(struct pt_regs *regs, int tl)
2236 {
2237         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
2238
2239         printk(KERN_EMERG "SUN4V-ITLB: Error at TPC[%lx], tl %d\n",
2240                regs->tpc, tl);
2241         printk(KERN_EMERG "SUN4V-ITLB: TPC<%pS>\n", (void *) regs->tpc);
2242         printk(KERN_EMERG "SUN4V-ITLB: O7[%lx]\n", regs->u_regs[UREG_I7]);
2243         printk(KERN_EMERG "SUN4V-ITLB: O7<%pS>\n",
2244                (void *) regs->u_regs[UREG_I7]);
2245         printk(KERN_EMERG "SUN4V-ITLB: vaddr[%lx] ctx[%lx] "
2246                "pte[%lx] error[%lx]\n",
2247                sun4v_err_itlb_vaddr, sun4v_err_itlb_ctx,
2248                sun4v_err_itlb_pte, sun4v_err_itlb_error);
2249
2250         sun4v_tlb_error(regs);
2251 }
2252
2253 unsigned long sun4v_err_dtlb_vaddr;
2254 unsigned long sun4v_err_dtlb_ctx;
2255 unsigned long sun4v_err_dtlb_pte;
2256 unsigned long sun4v_err_dtlb_error;
2257
2258 void sun4v_dtlb_error_report(struct pt_regs *regs, int tl)
2259 {
2260         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
2261
2262         printk(KERN_EMERG "SUN4V-DTLB: Error at TPC[%lx], tl %d\n",
2263                regs->tpc, tl);
2264         printk(KERN_EMERG "SUN4V-DTLB: TPC<%pS>\n", (void *) regs->tpc);
2265         printk(KERN_EMERG "SUN4V-DTLB: O7[%lx]\n", regs->u_regs[UREG_I7]);
2266         printk(KERN_EMERG "SUN4V-DTLB: O7<%pS>\n",
2267                (void *) regs->u_regs[UREG_I7]);
2268         printk(KERN_EMERG "SUN4V-DTLB: vaddr[%lx] ctx[%lx] "
2269                "pte[%lx] error[%lx]\n",
2270                sun4v_err_dtlb_vaddr, sun4v_err_dtlb_ctx,
2271                sun4v_err_dtlb_pte, sun4v_err_dtlb_error);
2272
2273         sun4v_tlb_error(regs);
2274 }
2275
2276 void hypervisor_tlbop_error(unsigned long err, unsigned long op)
2277 {
2278         printk(KERN_CRIT "SUN4V: TLB hv call error %lu for op %lu\n",
2279                err, op);
2280 }
2281
2282 void hypervisor_tlbop_error_xcall(unsigned long err, unsigned long op)
2283 {
2284         printk(KERN_CRIT "SUN4V: XCALL TLB hv call error %lu for op %lu\n",
2285                err, op);
2286 }
2287
2288 static void do_fpe_common(struct pt_regs *regs)
2289 {
2290         if (regs->tstate & TSTATE_PRIV) {
2291                 regs->tpc = regs->tnpc;
2292                 regs->tnpc += 4;
2293         } else {
2294                 unsigned long fsr = current_thread_info()->xfsr[0];
2295                 siginfo_t info;
2296
2297                 if (test_thread_flag(TIF_32BIT)) {
2298                         regs->tpc &= 0xffffffff;
2299                         regs->tnpc &= 0xffffffff;
2300                 }
2301                 info.si_signo = SIGFPE;
2302                 info.si_errno = 0;
2303                 info.si_addr = (void __user *)regs->tpc;
2304                 info.si_trapno = 0;
2305                 info.si_code = FPE_FIXME;
2306                 if ((fsr & 0x1c000) == (1 << 14)) {
2307                         if (fsr & 0x10)
2308                                 info.si_code = FPE_FLTINV;
2309                         else if (fsr & 0x08)
2310                                 info.si_code = FPE_FLTOVF;
2311                         else if (fsr & 0x04)
2312                                 info.si_code = FPE_FLTUND;
2313                         else if (fsr & 0x02)
2314                                 info.si_code = FPE_FLTDIV;
2315                         else if (fsr & 0x01)
2316                                 info.si_code = FPE_FLTRES;
2317                 }
2318                 force_sig_info(SIGFPE, &info, current);
2319         }
2320 }
2321
2322 void do_fpieee(struct pt_regs *regs)
2323 {
2324         enum ctx_state prev_state = exception_enter();
2325
2326         if (notify_die(DIE_TRAP, "fpu exception ieee", regs,
2327                        0, 0x24, SIGFPE) == NOTIFY_STOP)
2328                 goto out;
2329
2330         do_fpe_common(regs);
2331 out:
2332         exception_exit(prev_state);
2333 }
2334
2335 void do_fpother(struct pt_regs *regs)
2336 {
2337         enum ctx_state prev_state = exception_enter();
2338         struct fpustate *f = FPUSTATE;
2339         int ret = 0;
2340
2341         if (notify_die(DIE_TRAP, "fpu exception other", regs,
2342                        0, 0x25, SIGFPE) == NOTIFY_STOP)
2343                 goto out;
2344
2345         switch ((current_thread_info()->xfsr[0] & 0x1c000)) {
2346         case (2 << 14): /* unfinished_FPop */
2347         case (3 << 14): /* unimplemented_FPop */
2348                 ret = do_mathemu(regs, f, false);
2349                 break;
2350         }
2351         if (ret)
2352                 goto out;
2353         do_fpe_common(regs);
2354 out:
2355         exception_exit(prev_state);
2356 }
2357
2358 void do_tof(struct pt_regs *regs)
2359 {
2360         enum ctx_state prev_state = exception_enter();
2361         siginfo_t info;
2362
2363         if (notify_die(DIE_TRAP, "tagged arithmetic overflow", regs,
2364                        0, 0x26, SIGEMT) == NOTIFY_STOP)
2365                 goto out;
2366
2367         if (regs->tstate & TSTATE_PRIV)
2368                 die_if_kernel("Penguin overflow trap from kernel mode", regs);
2369         if (test_thread_flag(TIF_32BIT)) {
2370                 regs->tpc &= 0xffffffff;
2371                 regs->tnpc &= 0xffffffff;
2372         }
2373         info.si_signo = SIGEMT;
2374         info.si_errno = 0;
2375         info.si_code = EMT_TAGOVF;
2376         info.si_addr = (void __user *)regs->tpc;
2377         info.si_trapno = 0;
2378         force_sig_info(SIGEMT, &info, current);
2379 out:
2380         exception_exit(prev_state);
2381 }
2382
2383 void do_div0(struct pt_regs *regs)
2384 {
2385         enum ctx_state prev_state = exception_enter();
2386         siginfo_t info;
2387
2388         if (notify_die(DIE_TRAP, "integer division by zero", regs,
2389                        0, 0x28, SIGFPE) == NOTIFY_STOP)
2390                 goto out;
2391
2392         if (regs->tstate & TSTATE_PRIV)
2393                 die_if_kernel("TL0: Kernel divide by zero.", regs);
2394         if (test_thread_flag(TIF_32BIT)) {
2395                 regs->tpc &= 0xffffffff;
2396                 regs->tnpc &= 0xffffffff;
2397         }
2398         info.si_signo = SIGFPE;
2399         info.si_errno = 0;
2400         info.si_code = FPE_INTDIV;
2401         info.si_addr = (void __user *)regs->tpc;
2402         info.si_trapno = 0;
2403         force_sig_info(SIGFPE, &info, current);
2404 out:
2405         exception_exit(prev_state);
2406 }
2407
2408 static void instruction_dump(unsigned int *pc)
2409 {
2410         int i;
2411
2412         if ((((unsigned long) pc) & 3))
2413                 return;
2414
2415         printk("Instruction DUMP:");
2416         for (i = -3; i < 6; i++)
2417                 printk("%c%08x%c",i?' ':'<',pc[i],i?' ':'>');
2418         printk("\n");
2419 }
2420
2421 static void user_instruction_dump(unsigned int __user *pc)
2422 {
2423         int i;
2424         unsigned int buf[9];
2425         
2426         if ((((unsigned long) pc) & 3))
2427                 return;
2428                 
2429         if (copy_from_user(buf, pc - 3, sizeof(buf)))
2430                 return;
2431
2432         printk("Instruction DUMP:");
2433         for (i = 0; i < 9; i++)
2434                 printk("%c%08x%c",i==3?' ':'<',buf[i],i==3?' ':'>');
2435         printk("\n");
2436 }
2437
2438 void show_stack(struct task_struct *tsk, unsigned long *_ksp)
2439 {
2440         unsigned long fp, ksp;
2441         struct thread_info *tp;
2442         int count = 0;
2443 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2444         int graph = 0;
2445 #endif
2446
2447         ksp = (unsigned long) _ksp;
2448         if (!tsk)
2449                 tsk = current;
2450         tp = task_thread_info(tsk);
2451         if (ksp == 0UL) {
2452                 if (tsk == current)
2453                         asm("mov %%fp, %0" : "=r" (ksp));
2454                 else
2455                         ksp = tp->ksp;
2456         }
2457         if (tp == current_thread_info())
2458                 flushw_all();
2459
2460         fp = ksp + STACK_BIAS;
2461
2462         printk("Call Trace:\n");
2463         do {
2464                 struct sparc_stackf *sf;
2465                 struct pt_regs *regs;
2466                 unsigned long pc;
2467
2468                 if (!kstack_valid(tp, fp))
2469                         break;
2470                 sf = (struct sparc_stackf *) fp;
2471                 regs = (struct pt_regs *) (sf + 1);
2472
2473                 if (kstack_is_trap_frame(tp, regs)) {
2474                         if (!(regs->tstate & TSTATE_PRIV))
2475                                 break;
2476                         pc = regs->tpc;
2477                         fp = regs->u_regs[UREG_I6] + STACK_BIAS;
2478                 } else {
2479                         pc = sf->callers_pc;
2480                         fp = (unsigned long)sf->fp + STACK_BIAS;
2481                 }
2482
2483                 printk(" [%016lx] %pS\n", pc, (void *) pc);
2484 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2485                 if ((pc + 8UL) == (unsigned long) &return_to_handler) {
2486                         int index = tsk->curr_ret_stack;
2487                         if (tsk->ret_stack && index >= graph) {
2488                                 pc = tsk->ret_stack[index - graph].ret;
2489                                 printk(" [%016lx] %pS\n", pc, (void *) pc);
2490                                 graph++;
2491                         }
2492                 }
2493 #endif
2494         } while (++count < 16);
2495 }
2496
2497 static inline struct reg_window *kernel_stack_up(struct reg_window *rw)
2498 {
2499         unsigned long fp = rw->ins[6];
2500
2501         if (!fp)
2502                 return NULL;
2503
2504         return (struct reg_window *) (fp + STACK_BIAS);
2505 }
2506
2507 void __noreturn die_if_kernel(char *str, struct pt_regs *regs)
2508 {
2509         static int die_counter;
2510         int count = 0;
2511         
2512         /* Amuse the user. */
2513         printk(
2514 "              \\|/ ____ \\|/\n"
2515 "              \"@'/ .. \\`@\"\n"
2516 "              /_| \\__/ |_\\\n"
2517 "                 \\__U_/\n");
2518
2519         printk("%s(%d): %s [#%d]\n", current->comm, task_pid_nr(current), str, ++die_counter);
2520         notify_die(DIE_OOPS, str, regs, 0, 255, SIGSEGV);
2521         __asm__ __volatile__("flushw");
2522         show_regs(regs);
2523         add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
2524         if (regs->tstate & TSTATE_PRIV) {
2525                 struct thread_info *tp = current_thread_info();
2526                 struct reg_window *rw = (struct reg_window *)
2527                         (regs->u_regs[UREG_FP] + STACK_BIAS);
2528
2529                 /* Stop the back trace when we hit userland or we
2530                  * find some badly aligned kernel stack.
2531                  */
2532                 while (rw &&
2533                        count++ < 30 &&
2534                        kstack_valid(tp, (unsigned long) rw)) {
2535                         printk("Caller[%016lx]: %pS\n", rw->ins[7],
2536                                (void *) rw->ins[7]);
2537
2538                         rw = kernel_stack_up(rw);
2539                 }
2540                 instruction_dump ((unsigned int *) regs->tpc);
2541         } else {
2542                 if (test_thread_flag(TIF_32BIT)) {
2543                         regs->tpc &= 0xffffffff;
2544                         regs->tnpc &= 0xffffffff;
2545                 }
2546                 user_instruction_dump ((unsigned int __user *) regs->tpc);
2547         }
2548         if (panic_on_oops)
2549                 panic("Fatal exception");
2550         make_task_dead((regs->tstate & TSTATE_PRIV)? SIGKILL : SIGSEGV);
2551 }
2552 EXPORT_SYMBOL(die_if_kernel);
2553
2554 #define VIS_OPCODE_MASK ((0x3 << 30) | (0x3f << 19))
2555 #define VIS_OPCODE_VAL  ((0x2 << 30) | (0x36 << 19))
2556
2557 void do_illegal_instruction(struct pt_regs *regs)
2558 {
2559         enum ctx_state prev_state = exception_enter();
2560         unsigned long pc = regs->tpc;
2561         unsigned long tstate = regs->tstate;
2562         u32 insn;
2563         siginfo_t info;
2564
2565         if (notify_die(DIE_TRAP, "illegal instruction", regs,
2566                        0, 0x10, SIGILL) == NOTIFY_STOP)
2567                 goto out;
2568
2569         if (tstate & TSTATE_PRIV)
2570                 die_if_kernel("Kernel illegal instruction", regs);
2571         if (test_thread_flag(TIF_32BIT))
2572                 pc = (u32)pc;
2573         if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
2574                 if ((insn & 0xc1ffc000) == 0x81700000) /* POPC */ {
2575                         if (handle_popc(insn, regs))
2576                                 goto out;
2577                 } else if ((insn & 0xc1580000) == 0xc1100000) /* LDQ/STQ */ {
2578                         if (handle_ldf_stq(insn, regs))
2579                                 goto out;
2580                 } else if (tlb_type == hypervisor) {
2581                         if ((insn & VIS_OPCODE_MASK) == VIS_OPCODE_VAL) {
2582                                 if (!vis_emul(regs, insn))
2583                                         goto out;
2584                         } else {
2585                                 struct fpustate *f = FPUSTATE;
2586
2587                                 /* On UltraSPARC T2 and later, FPU insns which
2588                                  * are not implemented in HW signal an illegal
2589                                  * instruction trap and do not set the FP Trap
2590                                  * Trap in the %fsr to unimplemented_FPop.
2591                                  */
2592                                 if (do_mathemu(regs, f, true))
2593                                         goto out;
2594                         }
2595                 }
2596         }
2597         info.si_signo = SIGILL;
2598         info.si_errno = 0;
2599         info.si_code = ILL_ILLOPC;
2600         info.si_addr = (void __user *)pc;
2601         info.si_trapno = 0;
2602         force_sig_info(SIGILL, &info, current);
2603 out:
2604         exception_exit(prev_state);
2605 }
2606
2607 void mem_address_unaligned(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr)
2608 {
2609         enum ctx_state prev_state = exception_enter();
2610         siginfo_t info;
2611
2612         if (notify_die(DIE_TRAP, "memory address unaligned", regs,
2613                        0, 0x34, SIGSEGV) == NOTIFY_STOP)
2614                 goto out;
2615
2616         if (regs->tstate & TSTATE_PRIV) {
2617                 kernel_unaligned_trap(regs, *((unsigned int *)regs->tpc));
2618                 goto out;
2619         }
2620         if (is_no_fault_exception(regs))
2621                 return;
2622
2623         info.si_signo = SIGBUS;
2624         info.si_errno = 0;
2625         info.si_code = BUS_ADRALN;
2626         info.si_addr = (void __user *)sfar;
2627         info.si_trapno = 0;
2628         force_sig_info(SIGBUS, &info, current);
2629 out:
2630         exception_exit(prev_state);
2631 }
2632
2633 void sun4v_do_mna(struct pt_regs *regs, unsigned long addr, unsigned long type_ctx)
2634 {
2635         siginfo_t info;
2636
2637         if (notify_die(DIE_TRAP, "memory address unaligned", regs,
2638                        0, 0x34, SIGSEGV) == NOTIFY_STOP)
2639                 return;
2640
2641         if (regs->tstate & TSTATE_PRIV) {
2642                 kernel_unaligned_trap(regs, *((unsigned int *)regs->tpc));
2643                 return;
2644         }
2645         if (is_no_fault_exception(regs))
2646                 return;
2647
2648         info.si_signo = SIGBUS;
2649         info.si_errno = 0;
2650         info.si_code = BUS_ADRALN;
2651         info.si_addr = (void __user *) addr;
2652         info.si_trapno = 0;
2653         force_sig_info(SIGBUS, &info, current);
2654 }
2655
2656 void do_privop(struct pt_regs *regs)
2657 {
2658         enum ctx_state prev_state = exception_enter();
2659         siginfo_t info;
2660
2661         if (notify_die(DIE_TRAP, "privileged operation", regs,
2662                        0, 0x11, SIGILL) == NOTIFY_STOP)
2663                 goto out;
2664
2665         if (test_thread_flag(TIF_32BIT)) {
2666                 regs->tpc &= 0xffffffff;
2667                 regs->tnpc &= 0xffffffff;
2668         }
2669         info.si_signo = SIGILL;
2670         info.si_errno = 0;
2671         info.si_code = ILL_PRVOPC;
2672         info.si_addr = (void __user *)regs->tpc;
2673         info.si_trapno = 0;
2674         force_sig_info(SIGILL, &info, current);
2675 out:
2676         exception_exit(prev_state);
2677 }
2678
2679 void do_privact(struct pt_regs *regs)
2680 {
2681         do_privop(regs);
2682 }
2683
2684 /* Trap level 1 stuff or other traps we should never see... */
2685 void do_cee(struct pt_regs *regs)
2686 {
2687         exception_enter();
2688         die_if_kernel("TL0: Cache Error Exception", regs);
2689 }
2690
2691 void do_div0_tl1(struct pt_regs *regs)
2692 {
2693         exception_enter();
2694         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
2695         die_if_kernel("TL1: DIV0 Exception", regs);
2696 }
2697
2698 void do_fpieee_tl1(struct pt_regs *regs)
2699 {
2700         exception_enter();
2701         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
2702         die_if_kernel("TL1: FPU IEEE Exception", regs);
2703 }
2704
2705 void do_fpother_tl1(struct pt_regs *regs)
2706 {
2707         exception_enter();
2708         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
2709         die_if_kernel("TL1: FPU Other Exception", regs);
2710 }
2711
2712 void do_ill_tl1(struct pt_regs *regs)
2713 {
2714         exception_enter();
2715         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
2716         die_if_kernel("TL1: Illegal Instruction Exception", regs);
2717 }
2718
2719 void do_irq_tl1(struct pt_regs *regs)
2720 {
2721         exception_enter();
2722         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
2723         die_if_kernel("TL1: IRQ Exception", regs);
2724 }
2725
2726 void do_lddfmna_tl1(struct pt_regs *regs)
2727 {
2728         exception_enter();
2729         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
2730         die_if_kernel("TL1: LDDF Exception", regs);
2731 }
2732
2733 void do_stdfmna_tl1(struct pt_regs *regs)
2734 {
2735         exception_enter();
2736         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
2737         die_if_kernel("TL1: STDF Exception", regs);
2738 }
2739
2740 void do_paw(struct pt_regs *regs)
2741 {
2742         exception_enter();
2743         die_if_kernel("TL0: Phys Watchpoint Exception", regs);
2744 }
2745
2746 void do_paw_tl1(struct pt_regs *regs)
2747 {
2748         exception_enter();
2749         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
2750         die_if_kernel("TL1: Phys Watchpoint Exception", regs);
2751 }
2752
2753 void do_vaw(struct pt_regs *regs)
2754 {
2755         exception_enter();
2756         die_if_kernel("TL0: Virt Watchpoint Exception", regs);
2757 }
2758
2759 void do_vaw_tl1(struct pt_regs *regs)
2760 {
2761         exception_enter();
2762         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
2763         die_if_kernel("TL1: Virt Watchpoint Exception", regs);
2764 }
2765
2766 void do_tof_tl1(struct pt_regs *regs)
2767 {
2768         exception_enter();
2769         dump_tl1_traplog((struct tl1_traplog *)(regs + 1));
2770         die_if_kernel("TL1: Tag Overflow Exception", regs);
2771 }
2772
2773 void do_getpsr(struct pt_regs *regs)
2774 {
2775         regs->u_regs[UREG_I0] = tstate_to_psr(regs->tstate);
2776         regs->tpc   = regs->tnpc;
2777         regs->tnpc += 4;
2778         if (test_thread_flag(TIF_32BIT)) {
2779                 regs->tpc &= 0xffffffff;
2780                 regs->tnpc &= 0xffffffff;
2781         }
2782 }
2783
2784 u64 cpu_mondo_counter[NR_CPUS] = {0};
2785 struct trap_per_cpu trap_block[NR_CPUS];
2786 EXPORT_SYMBOL(trap_block);
2787
2788 /* This can get invoked before sched_init() so play it super safe
2789  * and use hard_smp_processor_id().
2790  */
2791 void notrace init_cur_cpu_trap(struct thread_info *t)
2792 {
2793         int cpu = hard_smp_processor_id();
2794         struct trap_per_cpu *p = &trap_block[cpu];
2795
2796         p->thread = t;
2797         p->pgd_paddr = 0;
2798 }
2799
2800 extern void thread_info_offsets_are_bolixed_dave(void);
2801 extern void trap_per_cpu_offsets_are_bolixed_dave(void);
2802 extern void tsb_config_offsets_are_bolixed_dave(void);
2803
2804 /* Only invoked on boot processor. */
2805 void __init trap_init(void)
2806 {
2807         /* Compile time sanity check. */
2808         BUILD_BUG_ON(TI_TASK != offsetof(struct thread_info, task) ||
2809                      TI_FLAGS != offsetof(struct thread_info, flags) ||
2810                      TI_CPU != offsetof(struct thread_info, cpu) ||
2811                      TI_FPSAVED != offsetof(struct thread_info, fpsaved) ||
2812                      TI_KSP != offsetof(struct thread_info, ksp) ||
2813                      TI_FAULT_ADDR != offsetof(struct thread_info,
2814                                                fault_address) ||
2815                      TI_KREGS != offsetof(struct thread_info, kregs) ||
2816                      TI_UTRAPS != offsetof(struct thread_info, utraps) ||
2817                      TI_REG_WINDOW != offsetof(struct thread_info,
2818                                                reg_window) ||
2819                      TI_RWIN_SPTRS != offsetof(struct thread_info,
2820                                                rwbuf_stkptrs) ||
2821                      TI_GSR != offsetof(struct thread_info, gsr) ||
2822                      TI_XFSR != offsetof(struct thread_info, xfsr) ||
2823                      TI_PRE_COUNT != offsetof(struct thread_info,
2824                                               preempt_count) ||
2825                      TI_NEW_CHILD != offsetof(struct thread_info, new_child) ||
2826                      TI_CURRENT_DS != offsetof(struct thread_info,
2827                                                 current_ds) ||
2828                      TI_KUNA_REGS != offsetof(struct thread_info,
2829                                               kern_una_regs) ||
2830                      TI_KUNA_INSN != offsetof(struct thread_info,
2831                                               kern_una_insn) ||
2832                      TI_FPREGS != offsetof(struct thread_info, fpregs) ||
2833                      (TI_FPREGS & (64 - 1)));
2834
2835         BUILD_BUG_ON(TRAP_PER_CPU_THREAD != offsetof(struct trap_per_cpu,
2836                                                      thread) ||
2837                      (TRAP_PER_CPU_PGD_PADDR !=
2838                       offsetof(struct trap_per_cpu, pgd_paddr)) ||
2839                      (TRAP_PER_CPU_CPU_MONDO_PA !=
2840                       offsetof(struct trap_per_cpu, cpu_mondo_pa)) ||
2841                      (TRAP_PER_CPU_DEV_MONDO_PA !=
2842                       offsetof(struct trap_per_cpu, dev_mondo_pa)) ||
2843                      (TRAP_PER_CPU_RESUM_MONDO_PA !=
2844                       offsetof(struct trap_per_cpu, resum_mondo_pa)) ||
2845                      (TRAP_PER_CPU_RESUM_KBUF_PA !=
2846                       offsetof(struct trap_per_cpu, resum_kernel_buf_pa)) ||
2847                      (TRAP_PER_CPU_NONRESUM_MONDO_PA !=
2848                       offsetof(struct trap_per_cpu, nonresum_mondo_pa)) ||
2849                      (TRAP_PER_CPU_NONRESUM_KBUF_PA !=
2850                       offsetof(struct trap_per_cpu, nonresum_kernel_buf_pa)) ||
2851                      (TRAP_PER_CPU_FAULT_INFO !=
2852                       offsetof(struct trap_per_cpu, fault_info)) ||
2853                      (TRAP_PER_CPU_CPU_MONDO_BLOCK_PA !=
2854                       offsetof(struct trap_per_cpu, cpu_mondo_block_pa)) ||
2855                      (TRAP_PER_CPU_CPU_LIST_PA !=
2856                       offsetof(struct trap_per_cpu, cpu_list_pa)) ||
2857                      (TRAP_PER_CPU_TSB_HUGE !=
2858                       offsetof(struct trap_per_cpu, tsb_huge)) ||
2859                      (TRAP_PER_CPU_TSB_HUGE_TEMP !=
2860                       offsetof(struct trap_per_cpu, tsb_huge_temp)) ||
2861                      (TRAP_PER_CPU_IRQ_WORKLIST_PA !=
2862                       offsetof(struct trap_per_cpu, irq_worklist_pa)) ||
2863                      (TRAP_PER_CPU_CPU_MONDO_QMASK !=
2864                       offsetof(struct trap_per_cpu, cpu_mondo_qmask)) ||
2865                      (TRAP_PER_CPU_DEV_MONDO_QMASK !=
2866                       offsetof(struct trap_per_cpu, dev_mondo_qmask)) ||
2867                      (TRAP_PER_CPU_RESUM_QMASK !=
2868                       offsetof(struct trap_per_cpu, resum_qmask)) ||
2869                      (TRAP_PER_CPU_NONRESUM_QMASK !=
2870                       offsetof(struct trap_per_cpu, nonresum_qmask)) ||
2871                      (TRAP_PER_CPU_PER_CPU_BASE !=
2872                       offsetof(struct trap_per_cpu, __per_cpu_base)));
2873
2874         BUILD_BUG_ON((TSB_CONFIG_TSB !=
2875                       offsetof(struct tsb_config, tsb)) ||
2876                      (TSB_CONFIG_RSS_LIMIT !=
2877                       offsetof(struct tsb_config, tsb_rss_limit)) ||
2878                      (TSB_CONFIG_NENTRIES !=
2879                       offsetof(struct tsb_config, tsb_nentries)) ||
2880                      (TSB_CONFIG_REG_VAL !=
2881                       offsetof(struct tsb_config, tsb_reg_val)) ||
2882                      (TSB_CONFIG_MAP_VADDR !=
2883                       offsetof(struct tsb_config, tsb_map_vaddr)) ||
2884                      (TSB_CONFIG_MAP_PTE !=
2885                       offsetof(struct tsb_config, tsb_map_pte)));
2886
2887         /* Attach to the address space of init_task.  On SMP we
2888          * do this in smp.c:smp_callin for other cpus.
2889          */
2890         mmgrab(&init_mm);
2891         current->active_mm = &init_mm;
2892 }