1/*	$NetBSD: machdep.c,v 1.36 2024/03/05 14:15:34 thorpej 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.36 2024/03/05 14:15:34 thorpej 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.h>
47#include <uvm/uvm_extern.h>
48
49#ifdef DDB
50#include <machine/db_machdep.h>
51#include <ddb/db_sym.h>
52#include <ddb/db_extern.h>
53#include <sys/exec_elf.h>
54#endif
55
56#include <dev/cons.h>	/* cntab access (cpu_reboot) */
57#include <machine/bootinfo.h>
58#include <machine/locore.h>
59#include <machine/psl.h>
60#include <machine/intr.h>/* hardintr_init */
61#include <playstation2/playstation2/sifbios.h>
62#include <playstation2/playstation2/interrupt.h>
63
64#if defined KLOADER_KERNEL_PATH && !defined KLOADER
65#error "define KLOADER"
66#endif
67#ifdef KLOADER
68#include <machine/kloader.h>
69#endif
70
71struct cpu_info cpu_info_store;
72
73struct vm_map *phys_map;
74phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
75int mem_cluster_cnt;
76
77#ifdef DEBUG
78static void bootinfo_dump(void);
79#endif
80
81void mach_init(void);
82/*
83 * Do all the stuff that locore normally does before calling main().
84 */
85void
86mach_init(void)
87{
88	extern char kernel_text[], edata[], end[];
89	char *kernend;
90	struct pcb *pcb0;
91	vaddr_t v;
92	paddr_t start;
93	size_t size;
94
95	/*
96	 * Clear the BSS segment.
97	 */
98	kernend = (void *)mips_round_page(end);
99	memset(edata, 0, kernend - edata);
100
101	/* disable all interrupt */
102	interrupt_init_bootstrap();
103
104	/* enable SIF BIOS */
105	sifbios_init();
106
107	consinit();
108
109	printf("kernel_text=%p edata=%p end=%p\n", kernel_text, edata, end);
110
111#ifdef DEBUG
112	bootinfo_dump();
113#endif
114	uvm_md_init();
115
116	physmem = atop(PS2_MEMORY_SIZE);
117
118	/*
119	 * Copy exception-dispatch code down to exception vector.
120	 * Initialize locore-function vector.
121	 * Clear out the I and D caches.
122	 */
123	mips_vector_init(NULL, false);
124
125	/*
126	 * Load the rest of the available pages into the VM system.
127	 */
128	start = (paddr_t)round_page(MIPS_KSEG0_TO_PHYS(kernend));
129	size = PS2_MEMORY_SIZE - start - BOOTINFO_BLOCK_SIZE;
130	memset((void *)MIPS_PHYS_TO_KSEG1(start), 0, size);
131
132	/* kernel itself */
133	mem_clusters[0].start = trunc_page(MIPS_KSEG0_TO_PHYS(kernel_text));
134	mem_clusters[0].size = start - mem_clusters[0].start;
135	/* heap */
136	mem_clusters[1].start = start;
137	mem_clusters[1].size = size;
138	/* load */
139	printf("load memory %#x, %#lx\n", start, size);
140	uvm_page_physload(atop(start), atop(start + size),
141	    atop(start), atop(start + size), VM_FREELIST_DEFAULT);
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.val[_L_SR] = PSL_LOWIPL;	/* SR */
157#ifdef IPL_ICU_MASK
158	pcb0->pcb_ppl = 0;
159#endif
160
161	lwp0.l_md.md_utf = (struct trapframe *)(v + USPACE) - 1;
162}
163
164/*
165 * Allocate memory for variable-sized tables,
166 */
167void
168cpu_startup(void)
169{
170	cpu_startup_common();
171}
172
173void
174cpu_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	splhigh();
212
213	if (howto & RB_DUMP)
214		dumpsys();
215
216 haltsys:
217	doshutdownhooks();
218
219	pmf_system_shutdown(boothowto);
220
221	if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
222		sifbios_halt(0); /* power down */
223	else if (howto & RB_HALT)
224		sifbios_halt(1); /* halt */
225	else {
226#ifdef KLOADER
227		kloader_reboot();
228		/* NOTREACHED */
229#endif
230		sifbios_halt(2); /* reset */
231	}
232
233	while (1)
234		;
235	/* NOTREACHED */
236}
237
238#ifdef DEBUG
239void
240bootinfo_dump(void)
241{
242	printf("devconf=%#x, option=%#x, rtc=%#x, pcmcia_type=%#x,"
243	    "sysconf=%#x\n",
244	    BOOTINFO_REF(BOOTINFO_DEVCONF),
245	    BOOTINFO_REF(BOOTINFO_OPTION_PTR),
246	    BOOTINFO_REF(BOOTINFO_RTC),
247	    BOOTINFO_REF(BOOTINFO_PCMCIA_TYPE),
248	    BOOTINFO_REF(BOOTINFO_SYSCONF));
249}
250#endif /* DEBUG */
251