xref: /netbsd-src/sys/arch/evbppc/virtex/machdep.c (revision 4d342c046e3288fb5a1edcd33cfec48c41c80664)
1 /*	$NetBSD: machdep.c,v 1.25 2020/06/11 19:20:43 ad Exp $ */
2 
3 /*
4  * Copyright (c) 2006 Jachym Holecek
5  * All rights reserved.
6  *
7  * Written for DFC Design, s.r.o.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Based on Walnut and Explora.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.25 2020/06/11 19:20:43 ad Exp $");
38 
39 #include "opt_compat_netbsd.h"
40 #include "opt_ddb.h"
41 #include "opt_virtex.h"
42 #include "opt_kgdb.h"
43 
44 #include <sys/param.h>
45 #include <sys/boot_flag.h>
46 #include <sys/buf.h>
47 #include <sys/bus.h>
48 #include <sys/device.h>
49 #include <sys/exec.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/module.h>
53 #include <sys/mount.h>
54 #include <sys/msgbuf.h>
55 #include <sys/kernel.h>
56 #include <sys/ksyms.h>
57 #include <sys/proc.h>
58 #include <sys/reboot.h>
59 #include <sys/syscallargs.h>
60 #include <sys/syslog.h>
61 #include <sys/systm.h>
62 
63 #include <uvm/uvm_extern.h>
64 
65 #include <dev/cons.h>
66 
67 #include <machine/powerpc.h>
68 
69 #include <powerpc/trap.h>
70 #include <powerpc/pcb.h>
71 
72 #include <powerpc/spr.h>
73 #include <powerpc/ibm4xx/spr.h>
74 
75 #include <powerpc/ibm4xx/cpu.h>
76 
77 #include <evbppc/virtex/dcr.h>
78 #include <evbppc/virtex/virtex.h>
79 
80 #include "ksyms.h"
81 
82 #if defined(DDB)
83 #include <powerpc/db_machdep.h>
84 #include <ddb/db_extern.h>
85 #endif
86 
87 #if defined(KGDB)
88 #include <sys/kgdb.h>
89 #endif
90 
91 /*
92  * Global variables used here and there
93  */
94 struct vm_map *phys_map = NULL;
95 
96 /*
97  * This should probably be in autoconf!				XXX
98  */
99 char machine[] = MACHINE;		/* from <machine/param.h> */
100 char machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
101 
102 char bootpath[256];
103 vaddr_t msgbuf_vaddr;
104 
105 void initppc(vaddr_t, vaddr_t);
106 
107 static void dumpsys(void);
108 
109 /* BSS segment start & end. */
110 extern char 		edata[], end[];
111 
112 /* One region holds all memory, the other is terminator expected by 405 pmap. */
113 #define MEMREGIONS 	2
114 struct mem_region 	physmemr[MEMREGIONS];
115 struct mem_region 	availmemr[MEMREGIONS];
116 
117 /* Maximum TLB page size. */
118 #define TLB_PG_SIZE 	(16*1024*1024)
119 
120 void
121 initppc(vaddr_t startkernel, vaddr_t endkernel)
122 {
123 	paddr_t			addr, memsize;
124 
125         /* Initialize cache info for memcpy, memset, etc. */
126         cpu_probe_cache();
127 
128 	memset(physmemr, 0, sizeof(physmemr)); 		/* [1].size = 0 */
129 	memset(availmemr, 0, sizeof(availmemr)); 	/* [1].size = 0 */
130 
131 	memsize = (PHYSMEM * 1024 * 1024) & ~PGOFSET;
132 
133 	physmemr[0].start 	= 0;
134 	physmemr[0].size 	= memsize;
135 
136 	availmemr[0].start 	= startkernel;
137 	availmemr[0].size 	= memsize - availmemr[0].start;
138 
139 	/* Map kernel memory linearly. */
140 	for (addr = 0; addr < endkernel; addr += TLB_PG_SIZE)
141 		ppc4xx_tlb_reserve(addr, addr, TLB_PG_SIZE, TLB_EX);
142 
143 	/* Give design-specific code a hint for reserved mappings. */
144 	virtex_machdep_init(roundup(memsize, TLB_PG_SIZE), TLB_PG_SIZE,
145 	    physmemr, availmemr);
146 
147 	ibm4xx_init(startkernel, endkernel, pic_ext_intr);
148 
149 #ifdef DDB
150 	if (boothowto & RB_KDB)
151 		Debugger();
152 #endif
153 
154 #ifdef KGDB
155 	/*
156 	 * Now trap to KGDB
157 	 */
158 	kgdb_connect(1);
159 #endif /* KGDB */
160 
161 	/*
162 	 * Look for the ibm4xx modules in the right place.
163 	 */
164 	module_machine = module_machine_ibm4xx;
165 }
166 
167 /*
168  * Machine dependent startup code.
169  */
170 
171 void
172 cpu_startup(void)
173 {
174 	/* For use by propdb. */
175 	static u_int 	memsize = PHYSMEM * 1024 * 1024;
176 	static u_int 	cpuspeed = CPUFREQ * 1000 * 1000;
177 	prop_number_t 	pn;
178 
179 	vaddr_t 	minaddr, maxaddr;
180 	char 		pbuf[9];
181 
182 	curcpu()->ci_khz = cpuspeed / 1000;
183 
184 	/* Initialize error message buffer. */
185 	initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
186 
187 	printf("%s%s", copyright, version);
188 
189 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
190 	printf("total memory = %s\n", pbuf);
191 
192 	minaddr = 0;
193 	/*
194 	 * Allocate a submap for physio
195 	 */
196 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
197 				 VM_PHYS_SIZE, 0, false, NULL);
198 
199 	/*
200 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
201 	 * are allocated via the pool allocator, and we use direct-mapped
202 	 * pool pages.
203 	 */
204 
205 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false)));
206 	printf("avail memory = %s\n", pbuf);
207 
208 	/*
209 	 * Set up the board properties database.
210 	 */
211 	board_info_init();
212 
213 	pn = prop_number_create_integer(memsize);
214 	KASSERT(pn != NULL);
215 	if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
216 		panic("setting mem-size");
217 	prop_object_release(pn);
218 
219 	pn = prop_number_create_integer(cpuspeed);
220 	KASSERT(pn != NULL);
221 	if (prop_dictionary_set(board_properties, "processor-frequency",
222 	    pn) == false)
223 		panic("setting processor-frequency");
224 	prop_object_release(pn);
225 
226 	/*
227 	 * Now that we have VM, malloc()s are OK in bus_space.
228 	 */
229 	bus_space_mallocok();
230 	fake_mapiodev = 0;
231 }
232 
233 
234 static void
235 dumpsys(void)
236 {
237 	printf("dumpsys: TBD\n");
238 }
239 
240 /* Hook used by 405 pmap module. */
241 void
242 mem_regions(struct mem_region **mem, struct mem_region **avail)
243 {
244 	*mem 	= physmemr;
245 	*avail 	= availmemr;
246 }
247 
248 /*
249  * Halt or reboot the machine after syncing/dumping according to howto.
250  */
251 void
252 cpu_reboot(int howto, char *what)
253 {
254 	static int syncing;
255 	static char str[256];
256 	char *ap = str, *ap1 = ap;
257 
258 	boothowto = howto;
259 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
260 		syncing = 1;
261 		vfs_shutdown();		/* sync */
262 		resettodr();		/* set wall clock */
263 	}
264 
265 	splhigh();
266 
267 	if (!cold && (howto & RB_DUMP))
268 		dumpsys();
269 
270 	doshutdownhooks();
271 
272 	pmf_system_shutdown(boothowto);
273 
274 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
275 		/* Power off here if we know how...*/
276 	}
277 
278 	if (howto & RB_HALT) {
279 		printf("halted\n\n");
280 
281 		goto reboot;	/* XXX for now... */
282 
283 #ifdef DDB
284 		printf("dropping to debugger\n");
285 		while(1)
286 			Debugger();
287 #endif
288 #ifdef KGDB
289 		printf("dropping to kgdb\n");
290 		while(1)
291 			kgdb_connect(1);
292 #endif
293 	}
294 
295 	printf("rebooting\n\n");
296 	if (what && *what) {
297 		if (strlen(what) > sizeof str - 5)
298 			printf("boot string too large, ignored\n");
299 		else {
300 			strcpy(str, what);
301 			ap1 = ap = str + strlen(str);
302 			*ap++ = ' ';
303 		}
304 	}
305 	*ap++ = '-';
306 	if (howto & RB_SINGLE)
307 		*ap++ = 's';
308 	if (howto & RB_KDB)
309 		*ap++ = 'd';
310 	*ap++ = 0;
311 	if (ap[-2] == '-')
312 		*ap1 = 0;
313 
314 	/* flush cache for msgbuf */
315 	__syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
316 
317  reboot:
318 	ppc4xx_reset();
319 
320 	printf("ppc4xx_reset() failed!\n");
321 #ifdef DDB
322 	while(1)
323 		Debugger();
324 #endif
325 #ifdef KGDB
326 	while(1)
327 		kgdb_connect(1);
328 #else
329 	while (1)
330 		/* nothing */;
331 #endif
332 }
333