1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/kernel.h>
4 #include <linux/kgdb.h>
5 #include <linux/printk.h>
6 #include <linux/sched/debug.h>
7 #include <linux/delay.h>
8 #include <linux/reboot.h>
9 
10 #include <asm/pdc.h>
11 #include <asm/pdc_chassis.h>
12 
13 unsigned int __aligned(16) toc_lock = 1;
14 
toc20_to_pt_regs(struct pt_regs * regs,struct pdc_toc_pim_20 * toc)15 static void toc20_to_pt_regs(struct pt_regs *regs, struct pdc_toc_pim_20 *toc)
16 {
17 	int i;
18 
19 	regs->gr[0] = (unsigned long)toc->cr[22];
20 
21 	for (i = 1; i < 32; i++)
22 		regs->gr[i] = (unsigned long)toc->gr[i];
23 
24 	for (i = 0; i < 8; i++)
25 		regs->sr[i] = (unsigned long)toc->sr[i];
26 
27 	regs->iasq[0] = (unsigned long)toc->cr[17];
28 	regs->iasq[1] = (unsigned long)toc->iasq_back;
29 	regs->iaoq[0] = (unsigned long)toc->cr[18];
30 	regs->iaoq[1] = (unsigned long)toc->iaoq_back;
31 
32 	regs->sar = (unsigned long)toc->cr[11];
33 	regs->iir = (unsigned long)toc->cr[19];
34 	regs->isr = (unsigned long)toc->cr[20];
35 	regs->ior = (unsigned long)toc->cr[21];
36 }
37 
toc11_to_pt_regs(struct pt_regs * regs,struct pdc_toc_pim_11 * toc)38 static void toc11_to_pt_regs(struct pt_regs *regs, struct pdc_toc_pim_11 *toc)
39 {
40 	int i;
41 
42 	regs->gr[0] = toc->cr[22];
43 
44 	for (i = 1; i < 32; i++)
45 		regs->gr[i] = toc->gr[i];
46 
47 	for (i = 0; i < 8; i++)
48 		regs->sr[i] = toc->sr[i];
49 
50 	regs->iasq[0] = toc->cr[17];
51 	regs->iasq[1] = toc->iasq_back;
52 	regs->iaoq[0] = toc->cr[18];
53 	regs->iaoq[1] = toc->iaoq_back;
54 
55 	regs->sar  = toc->cr[11];
56 	regs->iir  = toc->cr[19];
57 	regs->isr  = toc->cr[20];
58 	regs->ior  = toc->cr[21];
59 }
60 
toc_intr(struct pt_regs * regs)61 void notrace __noreturn __cold toc_intr(struct pt_regs *regs)
62 {
63 	struct pdc_toc_pim_20 pim_data20;
64 	struct pdc_toc_pim_11 pim_data11;
65 
66 	nmi_enter();
67 
68 	if (boot_cpu_data.cpu_type >= pcxu) {
69 		if (pdc_pim_toc20(&pim_data20))
70 			panic("Failed to get PIM data");
71 		toc20_to_pt_regs(regs, &pim_data20);
72 	} else {
73 		if (pdc_pim_toc11(&pim_data11))
74 			panic("Failed to get PIM data");
75 		toc11_to_pt_regs(regs, &pim_data11);
76 	}
77 
78 #ifdef CONFIG_KGDB
79 	if (atomic_read(&kgdb_active) != -1)
80 		kgdb_nmicallback(raw_smp_processor_id(), regs);
81 	kgdb_handle_exception(9, SIGTRAP, 0, regs);
82 #endif
83 	show_regs(regs);
84 
85 	/* give other CPUs time to show their backtrace */
86 	mdelay(2000);
87 	machine_restart("TOC");
88 
89 	/* should never reach this */
90 	panic("TOC");
91 }
92 
setup_toc(void)93 static __init int setup_toc(void)
94 {
95 	unsigned int csum = 0;
96 	unsigned long toc_code = (unsigned long)dereference_function_descriptor(toc_handler);
97 	int i;
98 
99 	PAGE0->vec_toc = __pa(toc_code) & 0xffffffff;
100 #ifdef CONFIG_64BIT
101 	PAGE0->vec_toc_hi = __pa(toc_code) >> 32;
102 #endif
103 	PAGE0->vec_toclen = toc_handler_size;
104 
105 	for (i = 0; i < toc_handler_size/4; i++)
106 		csum += ((u32 *)toc_code)[i];
107 	toc_handler_csum = -csum;
108 	pr_info("TOC handler registered\n");
109 	return 0;
110 }
111 early_initcall(setup_toc);
112