machdep.c revision 1.20 1 /* $NetBSD: machdep.c,v 1.20 2011/06/20 17:44:33 matt 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.20 2011/06/20 17:44:33 matt Exp $");
38
39 #include "opt_compat_netbsd.h"
40 #include "opt_ddb.h"
41 #include "opt_ipkdb.h"
42 #include "opt_virtex.h"
43 #include "opt_kgdb.h"
44
45 #include <sys/param.h>
46 #include <sys/boot_flag.h>
47 #include <sys/buf.h>
48 #include <sys/bus.h>
49 #include <sys/device.h>
50 #include <sys/exec.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/module.h>
54 #include <sys/mount.h>
55 #include <sys/msgbuf.h>
56 #include <sys/kernel.h>
57 #include <sys/ksyms.h>
58 #include <sys/proc.h>
59 #include <sys/reboot.h>
60 #include <sys/syscallargs.h>
61 #include <sys/syslog.h>
62 #include <sys/systm.h>
63
64 #include <uvm/uvm_extern.h>
65
66 #include <dev/cons.h>
67
68 #include <machine/powerpc.h>
69
70 #include <powerpc/trap.h>
71 #include <powerpc/pcb.h>
72
73 #include <powerpc/spr.h>
74 #include <powerpc/ibm4xx/spr.h>
75
76 #include <powerpc/ibm4xx/cpu.h>
77
78 #include <evbppc/virtex/dcr.h>
79 #include <evbppc/virtex/virtex.h>
80
81 #include "ksyms.h"
82
83 #if defined(DDB)
84 #include <powerpc/db_machdep.h>
85 #include <ddb/db_extern.h>
86 #endif
87
88 #if defined(KGDB)
89 #include <sys/kgdb.h>
90 #endif
91
92 /*
93 * Global variables used here and there
94 */
95 struct vm_map *phys_map = NULL;
96
97 /*
98 * This should probably be in autoconf! XXX
99 */
100 char cpu_model[80];
101 char machine[] = MACHINE; /* from <machine/param.h> */
102 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
103
104 char bootpath[256];
105 paddr_t msgbuf_paddr;
106 vaddr_t msgbuf_vaddr;
107
108 void initppc(vaddr_t, vaddr_t);
109
110 static void dumpsys(void);
111
112 /* BSS segment start & end. */
113 extern char edata[], end[];
114
115 /* One region holds all memory, the other is terminator expected by 405 pmap. */
116 #define MEMREGIONS 2
117 struct mem_region physmemr[MEMREGIONS];
118 struct mem_region availmemr[MEMREGIONS];
119
120 /* Maximum TLB page size. */
121 #define TLB_PG_SIZE (16*1024*1024)
122
123 void
124 initppc(vaddr_t startkernel, vaddr_t endkernel)
125 {
126 paddr_t addr, memsize;
127
128 /* Initialize cache info for memcpy, memset, etc. */
129 cpu_probe_cache();
130
131 memset(physmemr, 0, sizeof(physmemr)); /* [1].size = 0 */
132 memset(availmemr, 0, sizeof(availmemr)); /* [1].size = 0 */
133
134 memsize = (PHYSMEM * 1024 * 1024) & ~PGOFSET;
135
136 physmemr[0].start = 0;
137 physmemr[0].size = memsize;
138
139 availmemr[0].start = startkernel;
140 availmemr[0].size = memsize - availmemr[0].start;
141
142 /* Map kernel memory linearly. */
143 for (addr = 0; addr < endkernel; addr += TLB_PG_SIZE)
144 ppc4xx_tlb_reserve(addr, addr, TLB_PG_SIZE, TLB_EX);
145
146 /* Give design-specific code a hint for reserved mappings. */
147 virtex_machdep_init(roundup(memsize, TLB_PG_SIZE), TLB_PG_SIZE,
148 physmemr, availmemr);
149
150 ibm4xx_init(startkernel, endkernel, pic_ext_intr);
151
152 #ifdef DDB
153 if (boothowto & RB_KDB)
154 Debugger();
155 #endif
156 #ifdef IPKDB
157 /*
158 * Now trap to IPKDB
159 */
160 ipkdb_init();
161 if (boothowto & RB_KDB)
162 ipkdb_connect(0);
163 #endif
164 #ifdef KGDB
165 /*
166 * Now trap to KGDB
167 */
168 kgdb_connect(1);
169 #endif /* KGDB */
170
171 /*
172 * Look for the ibm4xx modules in the right place.
173 */
174 module_machine = module_machine_ibm4xx;
175 }
176
177 /*
178 * Machine dependent startup code.
179 */
180
181 char msgbuf[MSGBUFSIZE];
182
183 void
184 cpu_startup(void)
185 {
186 /* For use by propdb. */
187 static u_int memsize = PHYSMEM * 1024 * 1024;
188 static u_int cpuspeed = CPUFREQ * 1000 * 1000;
189 prop_number_t pn;
190
191 vaddr_t minaddr, maxaddr;
192 char pbuf[9];
193
194 curcpu()->ci_khz = cpuspeed / 1000;
195
196 /* Initialize error message buffer. */
197 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
198
199 printf("%s%s", copyright, version);
200
201 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
202 printf("total memory = %s\n", pbuf);
203
204 minaddr = 0;
205 /*
206 * Allocate a submap for physio
207 */
208 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
209 VM_PHYS_SIZE, 0, false, NULL);
210
211 /*
212 * No need to allocate an mbuf cluster submap. Mbuf clusters
213 * are allocated via the pool allocator, and we use direct-mapped
214 * pool pages.
215 */
216
217 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
218 printf("avail memory = %s\n", pbuf);
219
220 /*
221 * Set up the board properties database.
222 */
223 board_info_init();
224
225 pn = prop_number_create_integer(memsize);
226 KASSERT(pn != NULL);
227 if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
228 panic("setting mem-size");
229 prop_object_release(pn);
230
231 pn = prop_number_create_integer(cpuspeed);
232 KASSERT(pn != NULL);
233 if (prop_dictionary_set(board_properties, "processor-frequency",
234 pn) == false)
235 panic("setting processor-frequency");
236 prop_object_release(pn);
237
238 /*
239 * Now that we have VM, malloc()s are OK in bus_space.
240 */
241 bus_space_mallocok();
242 fake_mapiodev = 0;
243 }
244
245
246 static void
247 dumpsys(void)
248 {
249 printf("dumpsys: TBD\n");
250 }
251
252 /* Hook used by 405 pmap module. */
253 void
254 mem_regions(struct mem_region **mem, struct mem_region **avail)
255 {
256 *mem = physmemr;
257 *avail = availmemr;
258 }
259
260 /*
261 * Halt or reboot the machine after syncing/dumping according to howto.
262 */
263 void
264 cpu_reboot(int howto, char *what)
265 {
266 static int syncing;
267 static char str[256];
268 char *ap = str, *ap1 = ap;
269
270 boothowto = howto;
271 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
272 syncing = 1;
273 vfs_shutdown(); /* sync */
274 resettodr(); /* set wall clock */
275 }
276
277 splhigh();
278
279 if (!cold && (howto & RB_DUMP))
280 dumpsys();
281
282 doshutdownhooks();
283
284 pmf_system_shutdown(boothowto);
285
286 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
287 /* Power off here if we know how...*/
288 }
289
290 if (howto & RB_HALT) {
291 printf("halted\n\n");
292
293 goto reboot; /* XXX for now... */
294
295 #ifdef DDB
296 printf("dropping to debugger\n");
297 while(1)
298 Debugger();
299 #endif
300 #ifdef KGDB
301 printf("dropping to kgdb\n");
302 while(1)
303 kgdb_connect(1);
304 #endif
305 }
306
307 printf("rebooting\n\n");
308 if (what && *what) {
309 if (strlen(what) > sizeof str - 5)
310 printf("boot string too large, ignored\n");
311 else {
312 strcpy(str, what);
313 ap1 = ap = str + strlen(str);
314 *ap++ = ' ';
315 }
316 }
317 *ap++ = '-';
318 if (howto & RB_SINGLE)
319 *ap++ = 's';
320 if (howto & RB_KDB)
321 *ap++ = 'd';
322 *ap++ = 0;
323 if (ap[-2] == '-')
324 *ap1 = 0;
325
326 /* flush cache for msgbuf */
327 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
328
329 reboot:
330 ppc4xx_reset();
331
332 printf("ppc4xx_reset() failed!\n");
333 #ifdef DDB
334 while(1)
335 Debugger();
336 #endif
337 #ifdef KGDB
338 while(1)
339 kgdb_connect(1);
340 #else
341 while (1)
342 /* nothing */;
343 #endif
344 }
345