xref: /openbmc/linux/arch/parisc/kernel/toc.c (revision 34fa67e7)
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 #include <asm/ldcw.h>
13 
14 static unsigned int __aligned(16) toc_lock = 1;
15 DEFINE_PER_CPU_PAGE_ALIGNED(char [16384], toc_stack);
16 
17 static void toc20_to_pt_regs(struct pt_regs *regs, struct pdc_toc_pim_20 *toc)
18 {
19 	int i;
20 
21 	regs->gr[0] = (unsigned long)toc->cr[22];
22 
23 	for (i = 1; i < 32; i++)
24 		regs->gr[i] = (unsigned long)toc->gr[i];
25 
26 	for (i = 0; i < 8; i++)
27 		regs->sr[i] = (unsigned long)toc->sr[i];
28 
29 	regs->iasq[0] = (unsigned long)toc->cr[17];
30 	regs->iasq[1] = (unsigned long)toc->iasq_back;
31 	regs->iaoq[0] = (unsigned long)toc->cr[18];
32 	regs->iaoq[1] = (unsigned long)toc->iaoq_back;
33 
34 	regs->sar = (unsigned long)toc->cr[11];
35 	regs->iir = (unsigned long)toc->cr[19];
36 	regs->isr = (unsigned long)toc->cr[20];
37 	regs->ior = (unsigned long)toc->cr[21];
38 }
39 
40 static void toc11_to_pt_regs(struct pt_regs *regs, struct pdc_toc_pim_11 *toc)
41 {
42 	int i;
43 
44 	regs->gr[0] = toc->cr[22];
45 
46 	for (i = 1; i < 32; i++)
47 		regs->gr[i] = toc->gr[i];
48 
49 	for (i = 0; i < 8; i++)
50 		regs->sr[i] = toc->sr[i];
51 
52 	regs->iasq[0] = toc->cr[17];
53 	regs->iasq[1] = toc->iasq_back;
54 	regs->iaoq[0] = toc->cr[18];
55 	regs->iaoq[1] = toc->iaoq_back;
56 
57 	regs->sar  = toc->cr[11];
58 	regs->iir  = toc->cr[19];
59 	regs->isr  = toc->cr[20];
60 	regs->ior  = toc->cr[21];
61 }
62 
63 void notrace __noreturn __cold toc_intr(struct pt_regs *regs)
64 {
65 	struct pdc_toc_pim_20 pim_data20;
66 	struct pdc_toc_pim_11 pim_data11;
67 
68 	/* verify we wrote regs to the correct stack */
69 	BUG_ON(regs != (struct pt_regs *)&per_cpu(toc_stack, raw_smp_processor_id()));
70 
71 	if (boot_cpu_data.cpu_type >= pcxu) {
72 		if (pdc_pim_toc20(&pim_data20))
73 			panic("Failed to get PIM data");
74 		toc20_to_pt_regs(regs, &pim_data20);
75 	} else {
76 		if (pdc_pim_toc11(&pim_data11))
77 			panic("Failed to get PIM data");
78 		toc11_to_pt_regs(regs, &pim_data11);
79 	}
80 
81 #ifdef CONFIG_KGDB
82 	nmi_enter();
83 
84 	if (atomic_read(&kgdb_active) != -1)
85 		kgdb_nmicallback(raw_smp_processor_id(), regs);
86 	kgdb_handle_exception(9, SIGTRAP, 0, regs);
87 #endif
88 
89 	/* serialize output, otherwise all CPUs write backtrace at once */
90 	while (__ldcw(&toc_lock) == 0)
91 		; /* wait */
92 	show_regs(regs);
93 	toc_lock = 1;	 /* release lock for next CPU */
94 
95 	if (raw_smp_processor_id() != 0)
96 		while (1) ; /* all but monarch CPU will wait endless. */
97 
98 	/* give other CPUs time to show their backtrace */
99 	mdelay(2000);
100 
101 	machine_restart("TOC");
102 
103 	/* should never reach this */
104 	panic("TOC");
105 }
106 
107 static __init int setup_toc(void)
108 {
109 	unsigned int csum = 0;
110 	unsigned long toc_code = (unsigned long)dereference_function_descriptor(toc_handler);
111 	int i;
112 
113 	PAGE0->vec_toc = __pa(toc_code) & 0xffffffff;
114 #ifdef CONFIG_64BIT
115 	PAGE0->vec_toc_hi = __pa(toc_code) >> 32;
116 #endif
117 	PAGE0->vec_toclen = toc_handler_size;
118 
119 	for (i = 0; i < toc_handler_size/4; i++)
120 		csum += ((u32 *)toc_code)[i];
121 	toc_handler_csum = -csum;
122 	pr_info("TOC handler registered\n");
123 	return 0;
124 }
125 early_initcall(setup_toc);
126