xref: /netbsd-src/sys/arch/powerpc/ibm4xx/ibm4xx_machdep.c (revision b5c47949a45ac972130c38cf13dfd8afb1f09285)
1 /*	$NetBSD: ibm4xx_machdep.c,v 1.35 2021/03/06 08:08:19 rin Exp $	*/
2 /*	Original: ibm40x_machdep.c,v 1.3 2005/01/17 17:19:36 shige Exp $ */
3 
4 /*
5  * Copyright 2001, 2002 Wasabi Systems, Inc.
6  * All rights reserved.
7  *
8  * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed for the NetBSD Project by
21  *      Wasabi Systems, Inc.
22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23  *    or promote products derived from this software without specific prior
24  *    written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
41  * Copyright (C) 1995, 1996 TooLs GmbH.
42  * All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *	This product includes software developed by TooLs GmbH.
55  * 4. The name of TooLs GmbH may not be used to endorse or promote products
56  *    derived from this software without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
62  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
63  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
64  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
65  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
66  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
67  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68  */
69 
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: ibm4xx_machdep.c,v 1.35 2021/03/06 08:08:19 rin Exp $");
72 
73 #include "ksyms.h"
74 
75 #ifdef _KERNEL_OPT
76 #include "opt_ddb.h"
77 #include "opt_kgdb.h"
78 #include "opt_modular.h"
79 #endif
80 
81 #include <sys/param.h>
82 #include <sys/cpu.h>
83 #include <sys/ksyms.h>
84 #include <sys/msgbuf.h>
85 #include <sys/proc.h>
86 
87 #include <uvm/uvm_extern.h>
88 
89 #if defined(DDB)
90 #include <machine/db_machdep.h>
91 #include <ddb/db_extern.h>
92 #endif
93 
94 #if defined(KGDB)
95 #include <sys/kgdb.h>
96 #endif
97 
98 #include <machine/powerpc.h>
99 #include <machine/trap.h>
100 
101 #include <powerpc/pcb.h>
102 #include <powerpc/spr.h>
103 
104 #include <powerpc/ibm4xx/cpu.h>
105 #include <powerpc/ibm4xx/spr.h>
106 
107 /*
108  * Global variables used here and there
109  */
110 paddr_t msgbuf_paddr;
111 vaddr_t msgbuf_vaddr;
112 char msgbuf[MSGBUFSIZE];
113 
114 #if NKSYMS || defined(DDB) || defined(MODULAR)
115 void *startsym, *endsym;
116 #endif
117 
118 #ifdef MODULAR
119 register_t cpu_psluserset = PSL_USERSET;
120 register_t cpu_pslusermod = PSL_USERMOD;
121 register_t cpu_pslusermask = PSL_USERMASK;
122 #endif
123 
124 /*
125  * Trap vectors
126  */
127 extern const uint32_t defaulttrap[], defaultsize;
128 extern const uint32_t sctrap[], scsize;
129 extern const uint32_t accesstrap[], accesssize;
130 extern const uint32_t criticaltrap[], criticalsize;
131 extern const uint32_t tlbimiss4xx[], tlbim4size;
132 extern const uint32_t tlbdmiss4xx[], tlbdm4size;
133 extern const uint32_t pitfitwdog[], pitfitwdogsize;
134 extern const uint32_t errata51handler[], errata51size;
135 #if defined(DDB)
136 extern const uint32_t ddblow[], ddbsize;
137 #endif
138 static const struct exc_info trap_table[] = {
139 	{ EXC_SC,	sctrap,		(uintptr_t)&scsize },
140 	{ EXC_ALI,	accesstrap,	(uintptr_t)&accesssize },
141 	{ EXC_DSI,	accesstrap,	(uintptr_t)&accesssize },
142 	{ EXC_MCHK,	criticaltrap,	(uintptr_t)&criticalsize },
143 	{ EXC_ITMISS,	tlbimiss4xx,	(uintptr_t)&tlbim4size },
144 	{ EXC_DTMISS,	tlbdmiss4xx,	(uintptr_t)&tlbdm4size },
145 	{ EXC_PIT,	pitfitwdog,	(uintptr_t)&pitfitwdogsize },
146 	{ EXC_DEBUG,	criticaltrap,	(uintptr_t)&criticalsize },
147 	{ (EXC_DTMISS|EXC_ALI),
148 			errata51handler, (uintptr_t)&errata51size },
149 #if defined(DDB)
150 	{ EXC_PGM,	ddblow,		(uintptr_t)&ddbsize },
151 #else
152 	{ EXC_PGM,	accesstrap,	(uintptr_t)&accesssize },
153 #endif
154 };
155 
156 /*
157  * Install a trap vector. We cannot use memcpy because the
158  * destination may be zero.
159  */
160 static void
161 trap_copy(const uint32_t *src, vaddr_t dest, size_t len)
162 {
163 	uint32_t *dest_p = (void *)dest;
164 
165 	while (len > 0) {
166 		*dest_p++ = *src++;
167 		len -= sizeof(uint32_t);
168 	}
169 }
170 
171 /*
172  * ibm4xx_init:
173  */
174 void
175 ibm4xx_init(vaddr_t startkernel, vaddr_t endkernel, void (*handler)(void))
176 {
177 	/* Initialize cache info for memcpy, etc. */
178 	cpu_probe_cache();
179 
180 	/*
181 	 * Initialize current pcb and pmap pointers.
182 	 */
183 	KASSERT(curcpu() == &cpu_info[0]);
184 	KASSERT(lwp0.l_cpu == curcpu());
185 	KASSERT(curlwp == &lwp0);
186 
187 	curpcb = lwp_getpcb(curlwp);
188 	memset(curpcb, 0, sizeof(struct pcb));
189 
190 	curpcb->pcb_pm = pmap_kernel();
191 
192 	for (uintptr_t exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100) {
193 		trap_copy(defaulttrap, exc, (uintptr_t)&defaultsize);
194 	}
195 
196 	for (size_t i = 0; i < __arraycount(trap_table); i++) {
197 		KASSERT(trap_table[i].exc_size <= 0x100);
198 		trap_copy(trap_table[i].exc_addr, trap_table[i].exc_vector,
199 		    trap_table[i].exc_size);
200 	}
201 
202 	__syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
203 
204 	mtspr(SPR_EVPR, 0);		/* Set Exception vector base */
205 
206 	/* Handle trap instruction as PGM exception */
207 	mtspr(SPR_DBCR0, mfspr(SPR_DBCR0) & ~DBCR0_TDE);
208 
209 	/*
210 	 * external interrupt handler install
211 	 */
212 	if (handler)
213 		ibm4xx_install_extint(handler);
214 
215 	/*
216 	 * Now enable translation (and machine checks/recoverable interrupts).
217 	 */
218 	__asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
219 		      : : "r"(0), "K"(PSL_IR|PSL_DR));
220 	/* XXXX PSL_ME - With ME set kernel gets stuck... */
221 
222 	/*
223 	 * turn on console after enable translation
224 	 */
225 	consinit();
226 
227 	uvm_md_init();
228 
229 	/*
230 	 * Initialize pmap module.
231 	 */
232 	pmap_bootstrap(startkernel, endkernel);
233 
234 	/*
235 	 * Let's take all the indirect calls via our stubs and patch
236 	 * them to be direct calls.
237 	 */
238 	cpu_fixup_stubs();
239 
240 #if NKSYMS || defined(DDB) || defined(MODULAR)
241 	ksyms_addsyms_elf((uintptr_t)endsym - (uintptr_t)startsym,
242 	    startsym, endsym);
243 #endif
244 }
245 
246 void
247 ibm4xx_install_extint(void (*handler)(void))
248 {
249 	extern int extint, extsize;
250 	extern u_long extint_call;
251 	u_long offset = (u_long)handler - (u_long)&extint_call;
252 	int msr;
253 
254 #ifdef	DIAGNOSTIC
255 	if (offset > 0x1ffffff)
256 		panic("install_extint: too far away");
257 #endif
258 	__asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr));
259 	extint_call = (extint_call & 0xfc000003) | offset;
260 	memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
261 	__syncicache((void *)&extint_call, sizeof extint_call);
262 	__syncicache((void *)EXC_EXI, (int)&extsize);
263 	__asm volatile ("mtmsr %0" :: "r"(msr));
264 }
265 
266 /*
267  * ibm4xx_cpu_startup:
268  * Machine dependent startup code.
269  */
270 void
271 ibm4xx_cpu_startup(const char *model)
272 {
273 	vaddr_t minaddr, maxaddr;
274 	char pbuf[9];
275 
276 	KASSERT(curcpu() != NULL);
277 	KASSERT(lwp0.l_cpu != NULL);
278 	KASSERT(curcpu()->ci_intstk != 0);
279 	KASSERT(curcpu()->ci_idepth == -1);
280 
281 	/*
282 	 * Initialize error message buffer (at end of core).
283 	 */
284 #if 0	/* For some reason this fails... --Artem
285 	 * Besides, do we really have to put it at the end of core?
286 	 * Let's use static buffer for now
287 	 */
288 	if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE),
289 	    0, UVM_KMF_VAONLY)))
290 		panic("startup: no room for message buffer");
291 	for (i = 0; i < btoc(MSGBUFSIZE); i++)
292 		pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
293 		    msgbuf_paddr + i * PAGE_SIZE,
294 		    VM_PROT_READ|VM_PROT_WRITE, 0);
295 	initmsgbuf((void *)msgbuf_vaddr, round_page(MSGBUFSIZE));
296 #else
297 	initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
298 #endif
299 
300 	printf("%s%s", copyright, version);
301 	if (model != NULL)
302 		printf("Model: %s\n", model);
303 
304 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
305 	printf("total memory = %s\n", pbuf);
306 
307 	minaddr = 0;
308 	/*
309 	 * Allocate a submap for physio
310 	 */
311 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
312 				 VM_PHYS_SIZE, 0, false, NULL);
313 
314 	/*
315 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
316 	 * are allocated via the pool allocator, and we use direct-mapped
317 	 * pool pages.
318 	 */
319 
320 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false)));
321 	printf("avail memory = %s\n", pbuf);
322 }
323 
324 /*
325  * ibm4xx_dumpsys:
326  * Crash dump handling.
327  */
328 void
329 ibm4xx_dumpsys(void)
330 {
331 
332 	printf("dumpsys: TBD\n");
333 }
334