machdep.c revision 1.26 1 /* $NetBSD: machdep.c,v 1.26 2021/03/29 13:14:13 rin 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.26 2021/03/29 13:14:13 rin 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
104 void initppc(vaddr_t, vaddr_t);
105
106 static void dumpsys(void);
107
108 /* BSS segment start & end. */
109 extern char edata[], end[];
110
111 /* One region holds all memory, the other is terminator expected by 405 pmap. */
112 #define MEMREGIONS 2
113 struct mem_region physmemr[MEMREGIONS];
114 struct mem_region availmemr[MEMREGIONS];
115
116 /* Maximum TLB page size. */
117 #define TLB_PG_SIZE (16*1024*1024)
118
119 void
120 initppc(vaddr_t startkernel, vaddr_t endkernel)
121 {
122 paddr_t addr, memsize;
123
124 /* Initialize cache info for memcpy, memset, etc. */
125 cpu_probe_cache();
126
127 memset(physmemr, 0, sizeof(physmemr)); /* [1].size = 0 */
128 memset(availmemr, 0, sizeof(availmemr)); /* [1].size = 0 */
129
130 memsize = (PHYSMEM * 1024 * 1024) & ~PGOFSET;
131
132 physmemr[0].start = 0;
133 physmemr[0].size = memsize;
134
135 availmemr[0].start = startkernel;
136 availmemr[0].size = memsize - availmemr[0].start;
137
138 /* Map kernel memory linearly. */
139 for (addr = 0; addr < endkernel; addr += TLB_PG_SIZE)
140 ppc4xx_tlb_reserve(addr, addr, TLB_PG_SIZE, TLB_EX);
141
142 /* Give design-specific code a hint for reserved mappings. */
143 virtex_machdep_init(roundup(memsize, TLB_PG_SIZE), TLB_PG_SIZE,
144 physmemr, availmemr);
145
146 ibm4xx_init(startkernel, endkernel, pic_ext_intr);
147
148 #ifdef DDB
149 if (boothowto & RB_KDB)
150 Debugger();
151 #endif
152
153 #ifdef KGDB
154 /*
155 * Now trap to KGDB
156 */
157 kgdb_connect(1);
158 #endif /* KGDB */
159
160 /*
161 * Look for the ibm4xx modules in the right place.
162 */
163 module_machine = module_machine_ibm4xx;
164 }
165
166 /*
167 * Machine dependent startup code.
168 */
169
170 void
171 cpu_startup(void)
172 {
173 /* For use by propdb. */
174 static u_int memsize = PHYSMEM * 1024 * 1024;
175 static u_int cpuspeed = CPUFREQ * 1000 * 1000;
176 prop_number_t pn;
177
178 vaddr_t minaddr, maxaddr;
179 char pbuf[9];
180
181 curcpu()->ci_khz = cpuspeed / 1000;
182
183 /* Initialize error message buffer. */
184 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
185
186 printf("%s%s", copyright, version);
187
188 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
189 printf("total memory = %s\n", pbuf);
190
191 minaddr = 0;
192 /*
193 * Allocate a submap for physio
194 */
195 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
196 VM_PHYS_SIZE, 0, false, NULL);
197
198 /*
199 * No need to allocate an mbuf cluster submap. Mbuf clusters
200 * are allocated via the pool allocator, and we use direct-mapped
201 * pool pages.
202 */
203
204 format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false)));
205 printf("avail memory = %s\n", pbuf);
206
207 /*
208 * Set up the board properties database.
209 */
210 board_info_init();
211
212 pn = prop_number_create_integer(memsize);
213 KASSERT(pn != NULL);
214 if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
215 panic("setting mem-size");
216 prop_object_release(pn);
217
218 pn = prop_number_create_integer(cpuspeed);
219 KASSERT(pn != NULL);
220 if (prop_dictionary_set(board_properties, "processor-frequency",
221 pn) == false)
222 panic("setting processor-frequency");
223 prop_object_release(pn);
224
225 /*
226 * Now that we have VM, malloc()s are OK in bus_space.
227 */
228 bus_space_mallocok();
229 fake_mapiodev = 0;
230 }
231
232
233 static void
234 dumpsys(void)
235 {
236 printf("dumpsys: TBD\n");
237 }
238
239 /* Hook used by 405 pmap module. */
240 void
241 mem_regions(struct mem_region **mem, struct mem_region **avail)
242 {
243 *mem = physmemr;
244 *avail = availmemr;
245 }
246
247 /*
248 * Halt or reboot the machine after syncing/dumping according to howto.
249 */
250 void
251 cpu_reboot(int howto, char *what)
252 {
253 static int syncing;
254 static char str[256];
255 char *ap = str, *ap1 = ap;
256
257 boothowto = howto;
258 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
259 syncing = 1;
260 vfs_shutdown(); /* sync */
261 resettodr(); /* set wall clock */
262 }
263
264 splhigh();
265
266 if (!cold && (howto & RB_DUMP))
267 dumpsys();
268
269 doshutdownhooks();
270
271 pmf_system_shutdown(boothowto);
272
273 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
274 /* Power off here if we know how...*/
275 }
276
277 if (howto & RB_HALT) {
278 printf("halted\n\n");
279
280 goto reboot; /* XXX for now... */
281
282 #ifdef DDB
283 printf("dropping to debugger\n");
284 while(1)
285 Debugger();
286 #endif
287 #ifdef KGDB
288 printf("dropping to kgdb\n");
289 while(1)
290 kgdb_connect(1);
291 #endif
292 }
293
294 printf("rebooting\n\n");
295 if (what && *what) {
296 if (strlen(what) > sizeof str - 5)
297 printf("boot string too large, ignored\n");
298 else {
299 strcpy(str, what);
300 ap1 = ap = str + strlen(str);
301 *ap++ = ' ';
302 }
303 }
304 *ap++ = '-';
305 if (howto & RB_SINGLE)
306 *ap++ = 's';
307 if (howto & RB_KDB)
308 *ap++ = 'd';
309 *ap++ = 0;
310 if (ap[-2] == '-')
311 *ap1 = 0;
312
313 /* flush cache for msgbuf */
314 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
315
316 reboot:
317 ppc4xx_reset();
318
319 printf("ppc4xx_reset() failed!\n");
320 #ifdef DDB
321 while(1)
322 Debugger();
323 #endif
324 #ifdef KGDB
325 while(1)
326 kgdb_connect(1);
327 #else
328 while (1)
329 /* nothing */;
330 #endif
331 }
332