xref: /netbsd-src/sys/arch/evbppc/explora/machdep.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: machdep.c,v 1.10 2005/12/31 14:09:02 hannken Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Juergen Hannken-Illjes.
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 by the NetBSD
21  *      Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND 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 THE FOUNDATION OR CONTRIBUTORS
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 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.10 2005/12/31 14:09:02 hannken Exp $");
41 
42 #include "opt_explora.h"
43 #include "ksyms.h"
44 
45 #include <sys/param.h>
46 #include <sys/buf.h>
47 #include <sys/msgbuf.h>
48 #include <sys/kernel.h>
49 #include <sys/mount.h>
50 #include <sys/proc.h>
51 #include <sys/user.h>
52 #include <sys/reboot.h>
53 #include <sys/properties.h>
54 #include <sys/ksyms.h>
55 
56 #include <uvm/uvm_extern.h>
57 
58 #include <net/netisr.h>
59 
60 #include <machine/explora.h>
61 #include <machine/bus.h>
62 #include <machine/powerpc.h>
63 #include <machine/tlb.h>
64 #include <machine/trap.h>
65 
66 #include <powerpc/spr.h>
67 #include <powerpc/ibm4xx/dcr403cgx.h>
68 
69 #if NKSYMS || defined(DDB) || defined(LKM)
70 #include <machine/db_machdep.h>
71 #include <ddb/db_extern.h>
72 #endif
73 
74 #define MEMREGIONS	2
75 #define TLB_PG_SIZE	(16*1024*1024)
76 
77 char cpu_model[80];
78 char machine[] = MACHINE;		/* from <machine/param.h> */
79 char machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
80 
81 extern struct user *proc0paddr;
82 
83 struct propdb *board_info = NULL;
84 struct vm_map *phys_map = NULL;
85 struct vm_map *mb_map = NULL;
86 struct vm_map *exec_map = NULL;
87 char msgbuf[MSGBUFSIZE];
88 paddr_t msgbuf_paddr;
89 static unsigned cpuspeed = 66000000;
90 static unsigned memsize;
91 
92 static struct mem_region phys_mem[MEMREGIONS];
93 static struct mem_region avail_mem[MEMREGIONS];
94 
95 void		bootstrap(u_int, u_int);
96 static void	install_extint(void (*)(void));
97 int		lcsplx(int);
98 
99 /*
100  * Trap vectors
101  */
102 extern int defaulttrap, defaultsize;
103 extern int sctrap, scsize;
104 extern int alitrap, alisize;
105 extern int dsitrap, dsisize;
106 extern int isitrap, isisize;
107 extern int mchktrap, mchksize;
108 extern int tlbimiss4xx, tlbim4size;
109 extern int tlbdmiss4xx, tlbdm4size;
110 extern int pitfitwdog, pitfitwdogsize;
111 extern int debugtrap, debugsize;
112 extern int errata51handler, errata51size;
113 #ifdef DDB
114 extern int ddblow, ddbsize;
115 #endif
116 static struct {
117 	int vector;
118 	void *addr;
119 	void *size;
120 } trap_table[] = {
121 	{ EXC_SC,	&sctrap,	&scsize },
122 	{ EXC_ALI,	&alitrap,	&alisize },
123 	{ EXC_DSI,	&dsitrap,	&dsisize },
124 	{ EXC_ISI,	&isitrap,	&isisize },
125 	{ EXC_MCHK,	&mchktrap,	&mchksize },
126 	{ EXC_ITMISS,	&tlbimiss4xx,	&tlbim4size },
127 	{ EXC_DTMISS,	&tlbdmiss4xx,	&tlbdm4size },
128 	{ EXC_PIT,	&pitfitwdog,	&pitfitwdogsize },
129 	{ EXC_DEBUG,	&debugtrap,	&debugsize },
130 	{ (EXC_DTMISS|EXC_ALI), &errata51handler, &errata51size },
131 #if defined(DDB)
132 	{ EXC_PGM,	&ddblow,	&ddbsize },
133 #endif /* DDB */
134 };
135 
136 static void
137 set_tlb(int idx, u_int addr, u_int flags)
138 {
139 	u_int lo, hi;
140 
141 	addr &= ~(TLB_PG_SIZE-1);
142 
143 	lo = addr | TLB_EX | TLB_WR | flags;
144 #ifdef PPC_4XX_NOCACHE
145 	lo |= TLB_I;
146 #endif
147 	hi = addr | TLB_VALID | TLB_PG_16M;
148 
149 	__asm volatile(
150 	    "	tlbwe %1,%0,1	\n"
151 	    "	tlbwe %2,%0,0	\n"
152 	    "	sync		\n"
153 	    : : "r" (idx), "r" (lo), "r" (hi) );
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(void *src, int dest, size_t len)
162 {
163 	uint32_t *src_p = src;
164 	uint32_t *dest_p = (void *)dest;
165 
166 	while (len > 0) {
167 		*dest_p++ = *src_p++;
168 		len -= sizeof(uint32_t);
169 	}
170 }
171 
172 void
173 bootstrap(u_int startkernel, u_int endkernel)
174 {
175 	u_int i, j, t, br[4];
176 	u_int ntlb, maddr, msize, size;
177 	struct cpu_info * const ci = &cpu_info[0];
178 
179 	consinit();
180 
181 	br[0] = mfdcr(DCR_BR4);
182 	br[1] = mfdcr(DCR_BR5);
183 	br[2] = mfdcr(DCR_BR6);
184 	br[3] = mfdcr(DCR_BR7);
185 
186 	for (i = 0; i < 4; i++)
187 		for (j = i+1; j < 4; j++)
188 			if (br[j] < br[i])
189 				t = br[j], br[j] = br[i], br[i] = t;
190 
191 	for (i = 0, size = 0; i < 4; i++) {
192 		if (((br[i] >> 19) & 3) != 3)
193 			continue;
194 		maddr = ((br[i] >> 24) & 0xff) << 20;
195 		msize = 1 << (20 + ((br[i] >> 21) & 7));
196 		if (maddr+msize > size)
197 			size = maddr+msize;
198 	}
199 
200 #ifdef COM_IS_CONSOLE
201 	ntlb = TLB_NRESERVED-1;
202 #else
203 	ntlb = TLB_NRESERVED-2;
204 #endif
205 	if (size > ntlb*TLB_PG_SIZE)
206 		size = ntlb*TLB_PG_SIZE;
207 
208 	phys_mem[0].start = 0;
209 	phys_mem[0].size = size & ~PGOFSET;
210 	avail_mem[0].start = startkernel;
211 	avail_mem[0].size = size-startkernel;
212 
213 	__asm volatile(
214 	    "	mtpid %0	\n"
215 	    "	sync		\n"
216 	    : : "r" (1) );
217 
218 	/*
219 	 * Setup initial tlbs.
220 	 * Physical memory and  console device are
221 	 * mapped into the first (reserved) tlbs.
222 	 */
223 
224 	t = 0;
225 	for (maddr = 0; maddr < phys_mem[0].size; maddr += TLB_PG_SIZE)
226 		set_tlb(t++, maddr, 0);
227 
228 #ifdef COM_IS_CONSOLE
229 	set_tlb(t++, BASE_COM, TLB_I | TLB_G);
230 #else
231 	set_tlb(t++, BASE_FB, TLB_I | TLB_G);
232 	set_tlb(t++, BASE_FB2, TLB_I | TLB_G);
233 #endif
234 
235 	/* Disable all external interrupts */
236 	mtdcr(DCR_EXIER, 0);
237 
238 	/* Disable all timer interrupts */
239 	mtspr(SPR_TCR, 0);
240 
241 	/* Initialize cache info for memcpy, etc. */
242 	cpu_probe_cache();
243 
244 	/*
245 	 * Initialize lwp0 and current pcb and pmap pointers.
246 	 */
247 	lwp0.l_cpu = ci;
248 	lwp0.l_addr = proc0paddr;
249 	memset(lwp0.l_addr, 0, sizeof *lwp0.l_addr);
250 
251 	curpcb = &proc0paddr->u_pcb;
252 	curpcb->pcb_pm = pmap_kernel();
253 
254 	/*
255 	 * Install trap vectors.
256 	 */
257 
258 	for (i = EXC_RSVD; i <= EXC_LAST; i += 0x100)
259 		trap_copy(&defaulttrap, i, (size_t)&defaultsize);
260 
261 	for (i = 0; i < sizeof(trap_table)/sizeof(trap_table[0]); i++) {
262 		trap_copy(trap_table[i].addr, trap_table[i].vector,
263 		    (size_t)trap_table[i].size);
264 	}
265 
266 	__syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
267 
268 	/*
269 	 * Set Exception vector base.
270 	 * Handle trap instruction as PGM exception.
271 	 */
272 
273 	mtspr(SPR_EVPR, 0);
274 
275 	t = mfspr(SPR_DBCR0);
276 	t &= ~DBCR0_TDE;
277 	mtspr(SPR_DBCR0, t);
278 
279 	/*
280 	 * External interrupt handler install.
281 	 */
282 
283 	install_extint(ext_intr);
284 
285 	/*
286 	 * Now enable translation (and machine checks/recoverable interrupts).
287 	 */
288 	__asm volatile (
289 	    "	mfmsr %0	\n"
290 	    "	ori %0,%0,%1	\n"
291 	    "	mtmsr %0	\n"
292 	    "	sync		\n"
293 	    : : "r" (0), "K" (PSL_IR|PSL_DR|PSL_ME) );
294 
295 	uvm_setpagesize();
296 
297 	/*
298 	 * Initialize pmap module.
299 	 */
300 	pmap_bootstrap(startkernel, endkernel);
301 
302 #if NKSYMS || defined(DDB) || defined(LKM)
303 	ksyms_init(0, NULL, NULL);
304 #endif
305 
306 	fake_mapiodev = 0;
307 }
308 
309 static void
310 install_extint(void (*handler)(void))
311 {
312 	extern int extint, extsize;
313 	extern u_long extint_call;
314 	u_long offset = (u_long)handler - (u_long)&extint_call;
315 	int omsr, msr;
316 
317 #ifdef	DIAGNOSTIC
318 	if (offset > 0x1ffffff)
319 		panic("install_extint: too far away");
320 #endif
321 	__asm volatile (
322 	    "	mfmsr %0	\n"
323 	    "	andi. %1,%0,%2	\n"
324 	    "	mtmsr %1	\n"
325 	    : "=r" (omsr), "=r" (msr) : "K" ((u_short)~PSL_EE) );
326 	extint_call = (extint_call & 0xfc000003) | offset;
327 	memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
328 	__syncicache((void *)&extint_call, sizeof extint_call);
329 	__syncicache((void *)EXC_EXI, (int)&extsize);
330 	__asm volatile (
331 	    "	mtmsr %0	\n"
332 	    : : "r" (omsr) );
333 }
334 
335 void
336 cpu_startup(void)
337 {
338 	vaddr_t minaddr, maxaddr;
339 	char pbuf[9];
340 
341 	/*
342 	 * Initialize error message buffer (before start of kernel)
343 	 */
344 	initmsgbuf((caddr_t)msgbuf, round_page(MSGBUFSIZE));
345 
346 	printf("%s%s", copyright, version);
347 	printf("NCD Explora451\n");
348 
349 	memsize = ctob(physmem);
350 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
351 	printf("total memory = %s\n", pbuf);
352 
353 	minaddr = 0;
354 	/*
355 	 * Allocate a submap for exec arguments.  This map effectively
356 	 * limits the number of processes exec'ing at any time.
357 	 */
358 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
359 				 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
360 
361 	/*
362 	 * Allocate a submap for physio
363 	 */
364 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
365 				 VM_PHYS_SIZE, 0, FALSE, NULL);
366 
367 	/*
368 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
369 	 * are allocated via the pool allocator, and we use direct-mapped
370 	 * pool pages.
371 	 */
372 
373 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
374 	printf("avail memory = %s\n", pbuf);
375 
376 	/*
377 	 * Set up the board properties database.
378 	 */
379 	if (!(board_info = propdb_create("board info")))
380 		panic("Cannot create board info database");
381 
382 	if (board_info_set("processor-frequency", &cpuspeed,
383 	    sizeof(&cpuspeed), PROP_CONST, 0))
384 		panic("setting processor-frequency");
385 	if (board_info_set("mem-size", &memsize,
386 	    sizeof(&memsize), PROP_CONST, 0))
387 		panic("setting mem-size");
388 }
389 
390 int
391 lcsplx(int ipl)
392 {
393 	return spllower(ipl);	/*XXX*/
394 }
395 
396 void
397 softnet(void)
398 {
399 	int isr;
400 
401 	isr = netisr;
402 	netisr = 0;
403 
404 #define DONETISR(bit, fn)		\
405 	do {				\
406 		if (isr & (1 << bit))	\
407 		fn();			\
408 	} while (0)
409 
410 #include <net/netisr_dispatch.h>
411 
412 #undef DONETISR
413 }
414 
415 #include "com.h"
416 void
417 softserial(void)
418 {
419 #if NCOM > 0
420 	void comsoft(void);	/* XXX from dev/ic/com.c */
421 
422 	comsoft();
423 #endif
424 }
425 
426 void
427 cpu_reboot(int howto, char *what)
428 {
429 	static int syncing = 0;
430 
431 	boothowto = howto;
432 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
433 		syncing = 1;
434 		vfs_shutdown();
435 		resettodr();
436 	}
437 
438 	splhigh();
439 
440 	if (!cold && (howto & RB_DUMP))
441 		/*XXX dumpsys()*/;
442 
443 	doshutdownhooks();
444 
445 	if (howto & RB_HALT) {
446 		printf("halted\n\n");
447 
448 		while (1)
449 			;
450 	}
451 
452 	printf("rebooting\n\n");
453 
454 	/* flush cache for msgbuf */
455 	__syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
456 
457 	ppc4xx_reset();
458 
459 #ifdef DDB
460 	while (1)
461 		Debugger();
462 #else
463 	while (1)
464 		;
465 #endif
466 }
467 
468 void
469 inittodr(time_t base)
470 {
471 	if (base > 365*24*60*60 && time.tv_sec < 365*24*60*60)
472 		time.tv_sec = base;
473 }
474 
475 void
476 resettodr(void)
477 {
478 }
479 
480 void
481 mem_regions(struct mem_region **mem, struct mem_region **avail)
482 {
483 	*mem = phys_mem;
484 	*avail = avail_mem;
485 }
486