machdep.c revision 1.85 1 1.85 aymeric /* $NetBSD: machdep.c,v 1.85 2003/06/23 20:14:33 aymeric Exp $ */
2 1.1 ws
3 1.1 ws /*
4 1.1 ws * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 1.1 ws * Copyright (C) 1995, 1996 TooLs GmbH.
6 1.1 ws * All rights reserved.
7 1.1 ws *
8 1.1 ws * Redistribution and use in source and binary forms, with or without
9 1.1 ws * modification, are permitted provided that the following conditions
10 1.1 ws * are met:
11 1.1 ws * 1. Redistributions of source code must retain the above copyright
12 1.1 ws * notice, this list of conditions and the following disclaimer.
13 1.1 ws * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 ws * notice, this list of conditions and the following disclaimer in the
15 1.1 ws * documentation and/or other materials provided with the distribution.
16 1.1 ws * 3. All advertising materials mentioning features or use of this software
17 1.1 ws * must display the following acknowledgement:
18 1.1 ws * This product includes software developed by TooLs GmbH.
19 1.1 ws * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 1.1 ws * derived from this software without specific prior written permission.
21 1.1 ws *
22 1.1 ws * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 1.1 ws * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 ws * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 ws * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.1 ws * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 1.1 ws * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 1.1 ws * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 ws * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 1.1 ws * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 1.1 ws * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 ws */
33 1.21 jonathan
34 1.27 thorpej #include "opt_compat_netbsd.h"
35 1.21 jonathan #include "opt_ddb.h"
36 1.1 ws
37 1.1 ws #include <sys/param.h>
38 1.1 ws #include <sys/buf.h>
39 1.1 ws #include <sys/exec.h>
40 1.1 ws #include <sys/malloc.h>
41 1.1 ws #include <sys/mbuf.h>
42 1.1 ws #include <sys/mount.h>
43 1.1 ws #include <sys/msgbuf.h>
44 1.1 ws #include <sys/proc.h>
45 1.1 ws #include <sys/reboot.h>
46 1.80 thorpej #include <sys/sa.h>
47 1.1 ws #include <sys/syscallargs.h>
48 1.1 ws #include <sys/syslog.h>
49 1.1 ws #include <sys/systm.h>
50 1.43 thorpej #include <sys/kernel.h>
51 1.1 ws #include <sys/user.h>
52 1.59 jdolecek #include <sys/boot_flag.h>
53 1.84 ragge #include <sys/ksyms.h>
54 1.1 ws
55 1.19 sakamoto #include <uvm/uvm_extern.h>
56 1.19 sakamoto
57 1.1 ws #include <net/netisr.h>
58 1.1 ws
59 1.72 thorpej #include <machine/db_machdep.h>
60 1.72 thorpej #include <ddb/db_extern.h>
61 1.72 thorpej
62 1.70 thorpej #include <dev/ofw/openfirm.h>
63 1.70 thorpej
64 1.68 matt #include <machine/autoconf.h>
65 1.1 ws #include <machine/bat.h>
66 1.1 ws #include <machine/pmap.h>
67 1.1 ws #include <machine/powerpc.h>
68 1.1 ws #include <machine/trap.h>
69 1.54 thorpej
70 1.71 thorpej #include <machine/platform.h>
71 1.71 thorpej
72 1.68 matt #include <dev/cons.h>
73 1.1 ws
74 1.84 ragge #include "ksyms.h"
75 1.1 ws /*
76 1.1 ws * Global variables used here and there
77 1.1 ws */
78 1.78 chs char bootpath[256];
79 1.1 ws
80 1.76 matt int lcsplx(int); /* called from locore.S */
81 1.60 thorpej
82 1.73 thorpej static int fake_spl __P((int));
83 1.73 thorpej static void fake_splx __P((int));
84 1.73 thorpej static void fake_setsoft __P((int));
85 1.7 thorpej static void fake_clock_return __P((struct clockframe *, int));
86 1.73 thorpej static void *fake_intr_establish __P((int, int, int, int (*)(void *), void *));
87 1.73 thorpej static void fake_intr_disestablish __P((void *));
88 1.1 ws
89 1.1 ws struct machvec machine_interface = {
90 1.44 wrstuden fake_spl,
91 1.7 thorpej fake_spl,
92 1.1 ws fake_splx,
93 1.7 thorpej fake_setsoft,
94 1.7 thorpej fake_clock_return,
95 1.73 thorpej fake_intr_establish,
96 1.73 thorpej fake_intr_disestablish,
97 1.1 ws };
98 1.1 ws
99 1.71 thorpej void ofppc_bootstrap_console(void);
100 1.77 matt
101 1.77 matt struct pmap ofw_pmap;
102 1.70 thorpej
103 1.1 ws void
104 1.1 ws initppc(startkernel, endkernel, args)
105 1.1 ws u_int startkernel, endkernel;
106 1.1 ws char *args;
107 1.1 ws {
108 1.84 ragge #if NKSYMS || defined(DDB) || defined(LKM)
109 1.72 thorpej extern void *startsym, *endsym;
110 1.14 sakamoto #endif
111 1.1 ws
112 1.71 thorpej /* Initialize the bootstrap console. */
113 1.71 thorpej ofppc_bootstrap_console();
114 1.71 thorpej
115 1.1 ws /*
116 1.76 matt * Initialize the bat registers
117 1.1 ws */
118 1.82 matt oea_batinit(0);
119 1.29 sakamoto
120 1.1 ws /*
121 1.73 thorpej * Initialize the platform structure. This may add entries
122 1.73 thorpej * to the BAT table.
123 1.73 thorpej */
124 1.73 thorpej platform_init();
125 1.73 thorpej
126 1.73 thorpej #ifdef __notyet__ /* Needs some rethinking regarding real/virtual OFW */
127 1.73 thorpej OF_set_callback(callback);
128 1.73 thorpej #endif
129 1.73 thorpej
130 1.82 matt oea_init(NULL);
131 1.1 ws
132 1.1 ws /*
133 1.73 thorpej * Now that translation is enabled (and we can access bus space),
134 1.73 thorpej * initialize the console.
135 1.73 thorpej */
136 1.73 thorpej (*platform.cons_init)();
137 1.73 thorpej
138 1.73 thorpej /*
139 1.1 ws * Parse arg string.
140 1.1 ws */
141 1.78 chs strcpy(bootpath, args);
142 1.7 thorpej while (*++args && *args != ' ');
143 1.1 ws if (*args) {
144 1.59 jdolecek for(*args++ = 0; *args; args++)
145 1.59 jdolecek BOOT_FLAG(*args, boothowto);
146 1.29 sakamoto }
147 1.1 ws
148 1.72 thorpej /*
149 1.72 thorpej * Set the page size.
150 1.72 thorpej */
151 1.72 thorpej uvm_setpagesize();
152 1.72 thorpej
153 1.72 thorpej /*
154 1.72 thorpej * Initialize pmap module.
155 1.72 thorpej */
156 1.81 kleink pmap_bootstrap(startkernel, endkernel);
157 1.72 thorpej
158 1.84 ragge #if NKSYMS || defined(DDB) || defined(LKM)
159 1.84 ragge ksyms_init((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
160 1.84 ragge #endif
161 1.14 sakamoto #ifdef DDB
162 1.72 thorpej if (boothowto & RB_KDB)
163 1.72 thorpej Debugger();
164 1.14 sakamoto #endif
165 1.53 ws #ifdef IPKDB
166 1.1 ws /*
167 1.4 ws * Now trap to IPKDB
168 1.1 ws */
169 1.4 ws ipkdb_init();
170 1.1 ws if (boothowto & RB_KDB)
171 1.4 ws ipkdb_connect(0);
172 1.1 ws #endif
173 1.1 ws }
174 1.1 ws
175 1.1 ws /*
176 1.1 ws * Machine dependent startup code.
177 1.1 ws */
178 1.1 ws void
179 1.1 ws cpu_startup()
180 1.1 ws {
181 1.78 chs
182 1.82 matt oea_startup(NULL);
183 1.1 ws
184 1.1 ws /*
185 1.1 ws * Now allow hardware interrupts.
186 1.1 ws */
187 1.76 matt splhigh();
188 1.78 chs mtmsr(mfmsr() | PSL_EE | PSL_RI);
189 1.85 aymeric if (platform.softintr_init != NULL)
190 1.85 aymeric platform.softintr_init();
191 1.1 ws }
192 1.1 ws
193 1.1 ws void
194 1.1 ws consinit()
195 1.1 ws {
196 1.29 sakamoto
197 1.78 chs (*cn_tab->cn_probe)(cn_tab);
198 1.71 thorpej }
199 1.71 thorpej
200 1.78 chs void ofcons_cnprobe(struct consdev *);
201 1.71 thorpej int ofppc_cngetc(dev_t);
202 1.71 thorpej void ofppc_cnputc(dev_t, int);
203 1.71 thorpej
204 1.71 thorpej struct consdev ofppc_bootcons = {
205 1.78 chs ofcons_cnprobe, NULL, ofppc_cngetc, ofppc_cnputc, nullcnpollc, NULL,
206 1.83 bjh21 NULL, NULL, makedev(0,0), 1,
207 1.71 thorpej };
208 1.71 thorpej
209 1.71 thorpej int ofppc_stdin_ihandle, ofppc_stdout_ihandle;
210 1.71 thorpej int ofppc_stdin_phandle, ofppc_stdout_phandle;
211 1.71 thorpej
212 1.71 thorpej void
213 1.71 thorpej ofppc_bootstrap_console(void)
214 1.71 thorpej {
215 1.71 thorpej int chosen;
216 1.71 thorpej char data[4];
217 1.71 thorpej
218 1.71 thorpej chosen = OF_finddevice("/chosen");
219 1.71 thorpej
220 1.71 thorpej if (OF_getprop(chosen, "stdin", data, sizeof(data)) != sizeof(int))
221 1.71 thorpej goto nocons;
222 1.71 thorpej ofppc_stdin_ihandle = of_decode_int(data);
223 1.71 thorpej ofppc_stdin_phandle = OF_instance_to_package(ofppc_stdin_ihandle);
224 1.71 thorpej
225 1.71 thorpej if (OF_getprop(chosen, "stdout", data, sizeof(data)) != sizeof(int))
226 1.71 thorpej goto nocons;
227 1.71 thorpej ofppc_stdout_ihandle = of_decode_int(data);
228 1.71 thorpej ofppc_stdout_phandle = OF_instance_to_package(ofppc_stdout_ihandle);
229 1.71 thorpej
230 1.71 thorpej cn_tab = &ofppc_bootcons;
231 1.71 thorpej
232 1.71 thorpej nocons:
233 1.71 thorpej return;
234 1.71 thorpej }
235 1.71 thorpej
236 1.71 thorpej int
237 1.71 thorpej ofppc_cngetc(dev_t dev)
238 1.71 thorpej {
239 1.71 thorpej u_char ch = '\0';
240 1.71 thorpej int l;
241 1.71 thorpej
242 1.71 thorpej while ((l = OF_read(ofppc_stdin_ihandle, &ch, 1)) != 1)
243 1.71 thorpej if (l != -2 && l != 0)
244 1.71 thorpej return (-1);
245 1.71 thorpej
246 1.71 thorpej return (ch);
247 1.71 thorpej }
248 1.71 thorpej
249 1.71 thorpej void
250 1.71 thorpej ofppc_cnputc(dev_t dev, int c)
251 1.71 thorpej {
252 1.71 thorpej char ch = c;
253 1.71 thorpej
254 1.71 thorpej OF_write(ofppc_stdout_ihandle, &ch, 1);
255 1.1 ws }
256 1.1 ws
257 1.1 ws /*
258 1.1 ws * Crash dump handling.
259 1.1 ws */
260 1.1 ws
261 1.1 ws /*
262 1.1 ws * Stray interrupts.
263 1.1 ws */
264 1.1 ws void
265 1.1 ws strayintr(irq)
266 1.1 ws int irq;
267 1.1 ws {
268 1.1 ws log(LOG_ERR, "stray interrupt %d\n", irq);
269 1.1 ws }
270 1.1 ws
271 1.1 ws /*
272 1.1 ws * Halt or reboot the machine after syncing/dumping according to howto.
273 1.1 ws */
274 1.1 ws void
275 1.5 gwr cpu_reboot(howto, what)
276 1.1 ws int howto;
277 1.1 ws char *what;
278 1.1 ws {
279 1.1 ws static int syncing;
280 1.1 ws static char str[256];
281 1.1 ws char *ap = str, *ap1 = ap;
282 1.1 ws
283 1.1 ws boothowto = howto;
284 1.1 ws if (!cold && !(howto & RB_NOSYNC) && !syncing) {
285 1.1 ws syncing = 1;
286 1.1 ws vfs_shutdown(); /* sync */
287 1.1 ws resettodr(); /* set wall clock */
288 1.1 ws }
289 1.1 ws splhigh();
290 1.1 ws if (howto & RB_HALT) {
291 1.1 ws doshutdownhooks();
292 1.3 christos printf("halted\n\n");
293 1.1 ws ppc_exit();
294 1.1 ws }
295 1.1 ws if (!cold && (howto & RB_DUMP))
296 1.82 matt oea_dumpsys();
297 1.1 ws doshutdownhooks();
298 1.3 christos printf("rebooting\n\n");
299 1.1 ws if (what && *what) {
300 1.1 ws if (strlen(what) > sizeof str - 5)
301 1.3 christos printf("boot string too large, ignored\n");
302 1.1 ws else {
303 1.1 ws strcpy(str, what);
304 1.1 ws ap1 = ap = str + strlen(str);
305 1.1 ws *ap++ = ' ';
306 1.1 ws }
307 1.1 ws }
308 1.1 ws *ap++ = '-';
309 1.1 ws if (howto & RB_SINGLE)
310 1.1 ws *ap++ = 's';
311 1.1 ws if (howto & RB_KDB)
312 1.1 ws *ap++ = 'd';
313 1.1 ws *ap++ = 0;
314 1.1 ws if (ap[-2] == '-')
315 1.1 ws *ap1 = 0;
316 1.1 ws ppc_boot(str);
317 1.1 ws }
318 1.1 ws
319 1.68 matt #ifdef notyet
320 1.1 ws /*
321 1.1 ws * OpenFirmware callback routine
322 1.1 ws */
323 1.1 ws void
324 1.1 ws callback(p)
325 1.1 ws void *p;
326 1.1 ws {
327 1.1 ws panic("callback"); /* for now XXX */
328 1.60 thorpej }
329 1.68 matt #endif
330 1.60 thorpej
331 1.60 thorpej /*
332 1.60 thorpej * Perform an `splx()' for locore.
333 1.60 thorpej */
334 1.60 thorpej int
335 1.60 thorpej lcsplx(int ipl)
336 1.60 thorpej {
337 1.60 thorpej
338 1.73 thorpej return (_spllower(ipl));
339 1.1 ws }
340 1.1 ws
341 1.1 ws /*
342 1.7 thorpej * Initial Machine Interface.
343 1.1 ws */
344 1.7 thorpej static int
345 1.73 thorpej fake_spl(int new)
346 1.7 thorpej {
347 1.7 thorpej int scratch;
348 1.7 thorpej
349 1.7 thorpej asm volatile ("mfmsr %0; andi. %0,%0,%1; mtmsr %0; isync"
350 1.7 thorpej : "=r"(scratch) : "K"((u_short)~(PSL_EE|PSL_ME)));
351 1.29 sakamoto return (-1);
352 1.7 thorpej }
353 1.7 thorpej
354 1.1 ws static void
355 1.73 thorpej fake_setsoft(int ipl)
356 1.7 thorpej {
357 1.7 thorpej /* Do nothing */
358 1.7 thorpej }
359 1.7 thorpej
360 1.73 thorpej static void
361 1.1 ws fake_splx(new)
362 1.1 ws int new;
363 1.1 ws {
364 1.73 thorpej
365 1.73 thorpej (void) fake_spl(0);
366 1.7 thorpej }
367 1.7 thorpej
368 1.7 thorpej static void
369 1.7 thorpej fake_clock_return(frame, nticks)
370 1.7 thorpej struct clockframe *frame;
371 1.7 thorpej int nticks;
372 1.7 thorpej {
373 1.7 thorpej /* Do nothing */
374 1.1 ws }
375 1.1 ws
376 1.73 thorpej static void *
377 1.73 thorpej fake_intr_establish(irq, level, ist, handler, arg)
378 1.73 thorpej int irq, level, ist;
379 1.73 thorpej int (*handler) __P((void *));
380 1.73 thorpej void *arg;
381 1.73 thorpej {
382 1.73 thorpej
383 1.73 thorpej panic("fake_intr_establish");
384 1.73 thorpej }
385 1.73 thorpej
386 1.1 ws static void
387 1.73 thorpej fake_intr_disestablish(cookie)
388 1.73 thorpej void *cookie;
389 1.1 ws {
390 1.73 thorpej
391 1.73 thorpej panic("fake_intr_disestablish");
392 1.1 ws }
393