xref: /netbsd-src/sys/arch/playstation2/playstation2/machdep.c (revision 6db267571823ee3b0a1d61478df085a087f2e990)
1 /*	$NetBSD: machdep.c,v 1.34 2021/11/16 05:16:47 msaitoh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.34 2021/11/16 05:16:47 msaitoh Exp $");
31 
32 #include "opt_ddb.h"
33 #include "opt_kloader.h"
34 #include "opt_kloader_kernel_path.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/buf.h>
40 #include <sys/reboot.h>
41 #include <sys/mount.h>
42 #include <sys/kcore.h>
43 #include <sys/boot_flag.h>
44 #include <sys/device.h>
45 
46 #include <uvm/uvm_extern.h>
47 
48 #ifdef DDB
49 #include <machine/db_machdep.h>
50 #include <ddb/db_sym.h>
51 #include <ddb/db_extern.h>
52 #include <sys/exec_elf.h>
53 #endif
54 
55 #include <dev/cons.h>	/* cntab access (cpu_reboot) */
56 #include <machine/bootinfo.h>
57 #include <machine/psl.h>
58 #include <machine/intr.h>/* hardintr_init */
59 #include <playstation2/playstation2/sifbios.h>
60 #include <playstation2/playstation2/interrupt.h>
61 
62 #if defined KLOADER_KERNEL_PATH && !defined KLOADER
63 #error "define KLOADER"
64 #endif
65 #ifdef KLOADER
66 #include <machine/kloader.h>
67 #endif
68 
69 struct cpu_info cpu_info_store;
70 
71 struct vm_map *phys_map;
72 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
73 int mem_cluster_cnt;
74 
75 #ifdef DEBUG
76 static void bootinfo_dump(void);
77 #endif
78 
79 void mach_init(void);
80 /*
81  * Do all the stuff that locore normally does before calling main().
82  */
83 void
84 mach_init(void)
85 {
86 	extern char kernel_text[], edata[], end[];
87 	void *kernend;
88 	struct pcb *pcb0;
89 	vaddr_t v;
90 	paddr_t start;
91 	size_t size;
92 
93 	/*
94 	 * Clear the BSS segment.
95 	 */
96 	kernend = (void *)mips_round_page(end);
97 	memset(edata, 0, kernend - edata);
98 
99 	/* disable all interrupt */
100 	interrupt_init_bootstrap();
101 
102 	/* enable SIF BIOS */
103 	sifbios_init();
104 
105 	consinit();
106 
107 	printf("kernel_text=%p edata=%p end=%p\n", kernel_text, edata, end);
108 
109 #ifdef DEBUG
110 	bootinfo_dump();
111 #endif
112 	uvm_md_init();
113 
114 	physmem = atop(PS2_MEMORY_SIZE);
115 
116 	/*
117 	 * Copy exception-dispatch code down to exception vector.
118 	 * Initialize locore-function vector.
119 	 * Clear out the I and D caches.
120 	 */
121 	mips_vector_init();
122 
123 	/*
124 	 * Load the rest of the available pages into the VM system.
125 	 */
126 	start = (paddr_t)round_page(MIPS_KSEG0_TO_PHYS(kernend));
127 	size = PS2_MEMORY_SIZE - start - BOOTINFO_BLOCK_SIZE;
128 	memset((void *)MIPS_PHYS_TO_KSEG1(start), 0, size);
129 
130 	/* kernel itself */
131 	mem_clusters[0].start = trunc_page(MIPS_KSEG0_TO_PHYS(kernel_text));
132 	mem_clusters[0].size = start - mem_clusters[0].start;
133 	/* heap */
134 	mem_clusters[1].start = start;
135 	mem_clusters[1].size = size;
136 	/* load */
137 	printf("load memory %#lx, %#x\n", start, size);
138 	uvm_page_physload(atop(start), atop(start + size),
139 	    atop(start), atop(start + size), VM_FREELIST_DEFAULT);
140 
141 	strcpy(cpu_model, "SONY PlayStation 2");
142 
143 	/*
144 	 * Initialize error message buffer (at end of core).
145 	 */
146 	mips_init_msgbuf();
147 
148 	pmap_bootstrap();
149 
150 	/*
151 	 * Allocate uarea page for lwp0 and set it.
152 	 */
153 	v = uvm_pageboot_alloc(USPACE);
154 
155 	pcb0 = lwp_getpcb(&lwp0);
156 	pcb0->pcb_context[11] = PSL_LOWIPL;	/* SR */
157 #ifdef IPL_ICU_MASK
158 	pcb0->pcb_ppl = 0;
159 #endif
160 
161 	lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1
162 }
163 
164 /*
165  * Allocate memory for variable-sized tables,
166  */
167 void
168 cpu_startup(void)
169 {
170 	cpu_startup_common();
171 }
172 
173 void
174 cpu_reboot(int howto, char *bootstr)
175 {
176 #ifdef KLOADER
177 	struct kloader_bootinfo kbi;
178 #endif
179 	static int waittime = -1;
180 
181 	/* Take a snapshot before clobbering any registers. */
182 	if (curlwp)
183 		savectx(curpcb);
184 
185 	if (cold) {
186 		howto |= RB_HALT;
187 		goto haltsys;
188 	}
189 
190 	/* If "always halt" was specified as a boot flag, obey. */
191 	if (boothowto & RB_HALT) {
192 		howto |= RB_HALT;
193 	}
194 
195 #ifdef KLOADER
196 	/* No bootinfo is required. */
197 	kloader_bootinfo_set(&kbi, 0, NULL, NULL, true);
198 #ifndef KLOADER_KERNEL_PATH
199 #define	KLOADER_KERNEL_PATH	"/netbsd"
200 #endif
201 	if ((howto & RB_HALT) == 0)
202 		kloader_reboot_setup(KLOADER_KERNEL_PATH);
203 #endif
204 
205 	boothowto = howto;
206 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
207 		waittime = 0;
208 		vfs_shutdown();
209 
210 		/*
211 		 * If we've been adjusting the clock, the todr
212 		 * will be out of synch; adjust it now.
213 		 */
214 		resettodr();
215 	}
216 
217 	splhigh();
218 
219 	if (howto & RB_DUMP)
220 		dumpsys();
221 
222  haltsys:
223 	doshutdownhooks();
224 
225 	pmf_system_shutdown(boothowto);
226 
227 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
228 		sifbios_halt(0); /* power down */
229 	else if (howto & RB_HALT)
230 		sifbios_halt(1); /* halt */
231 	else {
232 #ifdef KLOADER
233 		kloader_reboot();
234 		/* NOTREACHED */
235 #endif
236 		sifbios_halt(2); /* reset */
237 	}
238 
239 	while (1)
240 		;
241 	/* NOTREACHED */
242 }
243 
244 #ifdef DEBUG
245 void
246 bootinfo_dump(void)
247 {
248 	printf("devconf=%#x, option=%#x, rtc=%#x, pcmcia_type=%#x,"
249 	    "sysconf=%#x\n",
250 	    BOOTINFO_REF(BOOTINFO_DEVCONF),
251 	    BOOTINFO_REF(BOOTINFO_OPTION_PTR),
252 	    BOOTINFO_REF(BOOTINFO_RTC),
253 	    BOOTINFO_REF(BOOTINFO_PCMCIA_TYPE),
254 	    BOOTINFO_REF(BOOTINFO_SYSCONF));
255 }
256 #endif /* DEBUG */
257