xref: /netbsd-src/sys/arch/powerpc/ibm4xx/ibm4xx_machdep.c (revision 404ee5b9334f618040b6cdef96a0ff35a6fc4636)
1 /*	$NetBSD: ibm4xx_machdep.c,v 1.26 2018/07/15 05:16:43 maxv 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.26 2018/07/15 05:16:43 maxv Exp $");
72 
73 #include "opt_compat_netbsd.h"
74 #include "opt_ddb.h"
75 #include "opt_kgdb.h"
76 #include "opt_modular.h"
77 #include "ksyms.h" /* for NKSYMS */
78 
79 #include <sys/param.h>
80 #include <sys/msgbuf.h>
81 #include <sys/proc.h>
82 #include <sys/cpu.h>
83 #include <sys/ksyms.h>
84 
85 #include <uvm/uvm_extern.h>
86 
87 #if defined(DDB)
88 #include <machine/db_machdep.h>
89 #include <ddb/db_extern.h>
90 #endif
91 
92 #if defined(KGDB)
93 #include <sys/kgdb.h>
94 #endif
95 
96 #include <machine/powerpc.h>
97 #include <powerpc/pcb.h>
98 #include <machine/trap.h>
99 
100 #include <powerpc/spr.h>
101 #include <powerpc/ibm4xx/spr.h>
102 
103 #include <powerpc/ibm4xx/cpu.h>
104 
105 /*
106  * Global variables used here and there
107  */
108 paddr_t msgbuf_paddr;
109 vaddr_t msgbuf_vaddr;
110 char msgbuf[MSGBUFSIZE];
111 
112 #if NKSYMS || defined(DDB) || defined(MODULAR)
113 void *startsym, *endsym;
114 #endif
115 
116 /*
117  * Trap vectors
118  */
119 extern const uint32_t defaulttrap[], defaultsize;
120 extern const uint32_t sctrap[], scsize;
121 extern const uint32_t accesstrap[], accesssize;
122 extern const uint32_t criticaltrap[], criticalsize;
123 extern const uint32_t tlbimiss4xx[], tlbim4size;
124 extern const uint32_t tlbdmiss4xx[], tlbdm4size;
125 extern const uint32_t pitfitwdog[], pitfitwdogsize;
126 extern const uint32_t errata51handler[], errata51size;
127 #if defined(DDB)
128 extern const uint32_t ddblow[], ddbsize;
129 #endif
130 static const struct exc_info trap_table[] = {
131 	{ EXC_SC,	sctrap,		(uintptr_t)&scsize },
132 	{ EXC_ALI,	accesstrap,	(uintptr_t)&accesssize },
133 	{ EXC_DSI,	accesstrap,	(uintptr_t)&accesssize },
134 	{ EXC_MCHK,	criticaltrap,	(uintptr_t)&criticalsize },
135 	{ EXC_ITMISS,	tlbimiss4xx,	(uintptr_t)&tlbim4size },
136 	{ EXC_DTMISS,	tlbdmiss4xx,	(uintptr_t)&tlbdm4size },
137 	{ EXC_PIT,	pitfitwdog,	(uintptr_t)&pitfitwdogsize },
138 	{ EXC_DEBUG,	criticaltrap,	(uintptr_t)&criticalsize },
139 	{ (EXC_DTMISS|EXC_ALI),
140 			errata51handler, (uintptr_t)&errata51size },
141 #if defined(DDB)
142 	{ EXC_PGM,	ddblow,		(uintptr_t)&ddbsize },
143 #endif
144 };
145 
146 /*
147  * Install a trap vector. We cannot use memcpy because the
148  * destination may be zero.
149  */
150 static void
151 trap_copy(const uint32_t *src, vaddr_t dest, size_t len)
152 {
153 	uint32_t *dest_p = (void *)dest;
154 
155 	while (len > 0) {
156 		*dest_p++ = *src++;
157 		len -= sizeof(uint32_t);
158 	}
159 }
160 
161 /*
162  * ibm4xx_init:
163  */
164 void
165 ibm4xx_init(vaddr_t startkernel, vaddr_t endkernel, void (*handler)(void))
166 {
167 	/* Initialize cache info for memcpy, etc. */
168 	cpu_probe_cache();
169 
170 	/*
171 	 * Initialize current pcb and pmap pointers.
172 	 */
173 	KASSERT(curcpu() == &cpu_info[0]);
174 	KASSERT(lwp0.l_cpu == curcpu());
175 	KASSERT(curlwp == &lwp0);
176 
177 	curpcb = lwp_getpcb(curlwp);
178 	memset(curpcb, 0, sizeof(struct pcb));
179 
180 	curpcb->pcb_pm = pmap_kernel();
181 
182 	for (uintptr_t exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100) {
183 		trap_copy(defaulttrap, exc, (uintptr_t)&defaultsize);
184 	}
185 
186 	for (size_t i = 0; i < __arraycount(trap_table); i++) {
187 		KASSERT(trap_table[i].exc_size <= 0x100);
188 		trap_copy(trap_table[i].exc_addr, trap_table[i].exc_vector,
189 		    trap_table[i].exc_size);
190 	}
191 
192 	__syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
193 
194 	mtspr(SPR_EVPR, 0);		/* Set Exception vector base */
195 
196 	/* Handle trap instruction as PGM exception */
197 	mtspr(SPR_DBCR0, mfspr(SPR_DBCR0) & ~DBCR0_TDE);
198 
199 	/*
200 	 * external interrupt handler install
201 	 */
202 	if (handler)
203 	    ibm4xx_install_extint(handler);
204 
205 	/*
206 	 * Now enable translation (and machine checks/recoverable interrupts).
207 	 */
208 	__asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
209 		      : : "r"(0), "K"(PSL_IR|PSL_DR));
210 	/* XXXX PSL_ME - With ME set kernel gets stuck... */
211 
212 	/*
213 	 * turn on console after enable translation
214 	 */
215 	consinit();
216 
217 	uvm_md_init();
218 
219 	/*
220 	 * Initialize pmap module.
221 	 */
222 	pmap_bootstrap(startkernel, endkernel);
223 
224 	/*
225 	 * Let's take all the indirect calls via our stubs and patch
226 	 * them to be direct calls.
227 	 */
228 	cpu_fixup_stubs();
229 
230 #if NKSYMS || defined(DDB) || defined(MODULAR)
231 	ksyms_addsyms_elf((uintptr_t)endsym - (uintptr_t)startsym,
232 	    startsym, endsym);
233 #endif
234 }
235 
236 void
237 ibm4xx_install_extint(void (*handler)(void))
238 {
239 	extern int extint, extsize;
240 	extern u_long extint_call;
241 	u_long offset = (u_long)handler - (u_long)&extint_call;
242 	int msr;
243 
244 #ifdef	DIAGNOSTIC
245 	if (offset > 0x1ffffff)
246 		panic("install_extint: too far away");
247 #endif
248 	__asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr));
249 	extint_call = (extint_call & 0xfc000003) | offset;
250 	memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
251 	__syncicache((void *)&extint_call, sizeof extint_call);
252 	__syncicache((void *)EXC_EXI, (int)&extsize);
253 	__asm volatile ("mtmsr %0" :: "r"(msr));
254 }
255 
256 /*
257  * ibm4xx_cpu_startup:
258  * Machine dependent startup code.
259  */
260 void
261 ibm4xx_cpu_startup(const char *model)
262 {
263 	vaddr_t minaddr, maxaddr;
264 	char pbuf[9];
265 
266 	KASSERT(curcpu() != NULL);
267 	KASSERT(lwp0.l_cpu != NULL);
268 	KASSERT(curcpu()->ci_intstk != 0);
269 	KASSERT(curcpu()->ci_idepth == -1);
270 
271 	/*
272 	 * Initialize error message buffer (at end of core).
273 	 */
274 #if 0	/* For some reason this fails... --Artem
275 	 * Besides, do we really have to put it at the end of core?
276 	 * Let's use static buffer for now
277 	 */
278 	if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE),
279 	    0, UVM_KMF_VAONLY)))
280 		panic("startup: no room for message buffer");
281 	for (i = 0; i < btoc(MSGBUFSIZE); i++)
282 		pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
283 		    msgbuf_paddr + i * PAGE_SIZE,
284 		    VM_PROT_READ|VM_PROT_WRITE, 0);
285 	initmsgbuf((void *)msgbuf_vaddr, round_page(MSGBUFSIZE));
286 #else
287 	initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
288 #endif
289 
290 	printf("%s%s", copyright, version);
291 	if (model != NULL)
292 		printf("Model: %s\n", model);
293 
294 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
295 	printf("total memory = %s\n", pbuf);
296 
297 	minaddr = 0;
298 	/*
299 	 * Allocate a submap for physio
300 	 */
301 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
302 				 VM_PHYS_SIZE, 0, false, NULL);
303 
304 	/*
305 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
306 	 * are allocated via the pool allocator, and we use direct-mapped
307 	 * pool pages.
308 	 */
309 
310 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
311 	printf("avail memory = %s\n", pbuf);
312 }
313 
314 /*
315  * ibm4xx_dumpsys:
316  * Crash dump handling.
317  */
318 void
319 ibm4xx_dumpsys(void)
320 {
321 
322 	printf("dumpsys: TBD\n");
323 }
324