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