1 /* $NetBSD: machdep.c,v 1.47 2026/11/14 23:08:26 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1988 University of Utah. 5 * Copyright (c) 1982, 1986, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: Utah $Hdr: machdep.c 1.74 92/12/20$ 37 * 38 * @(#)machdep.c 8.10 (Berkeley) 4/20/94 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.47 2026/11/14 23:08:26 thorpej Exp $"); 43 44 #include "opt_m68k_arch.h" 45 46 #define _M68K_BUS_SPACE_PRIVATE 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/bus.h> 51 #include <sys/cpu.h> 52 #include <sys/boot_flag.h> 53 54 #include <uvm/uvm_extern.h> 55 56 #include <sys/sysctl.h> 57 58 #include <machine/psl.h> 59 #include <m68k/linux_bootinfo.h> 60 #include <dev/cons.h> 61 #include <dev/mm.h> 62 63 #ifdef DDB 64 #include <ddb/ddb.h> 65 #endif 66 67 #include "gftty.h" 68 #if NGFTTY > 0 69 #include <dev/goldfish/gfttyvar.h> 70 #endif 71 72 /* Machine-dependent initialization routines. */ 73 void machine_init(paddr_t); 74 75 struct pmap_bootmap machine_bootmap[] = { 76 /* 77 * We map the CPU info at a fixed VA range. On the boot 78 * CPU, we re-use the tmpstk page for this, and start() 79 * will fill in that PA before calling pmap_bootstrap1(). 80 */ 81 { .pmbm_vaddr = CPUINFO_VA, 82 .pmbm_paddr = 0, /* filled in at boot time */ 83 .pmbm_size = PAGE_SIZE, 84 .pmbm_flags = PMBM_F_FIXEDVA }, 85 86 /* 87 * Keep out of the TT range we use to map the I/O devices. 88 */ 89 { .pmbm_vaddr = VIRT68K_IO_BASE, 90 .pmbm_size = VIRT68K_IO_SIZE, 91 .pmbm_flags = PMBM_F_KEEPOUT }, 92 93 { .pmbm_vaddr = -1 }, 94 }; 95 96 static void 97 machine_cnattach(void (*func)(bus_space_tag_t, bus_space_handle_t), 98 paddr_t addr, paddr_t size) 99 { 100 extern paddr_t consdev_addr; 101 bus_space_tag_t bst = &m68k_simple_bus_space; 102 bus_space_handle_t bsh; 103 104 if (bus_space_map(bst, addr, size, 0, &bsh) == 0) { 105 func(bst, bsh); 106 } 107 consdev_addr = addr; 108 } 109 110 /* 111 * Early initialization, right before main is called. 112 */ 113 void 114 machine_init(paddr_t nextpa) 115 { 116 struct bi_record *bi __unused; 117 118 /* 119 * Find the console in the bootinfo and attach it. 120 */ 121 #if NGFTTY > 0 122 bi = bootinfo_find(BI_VIRT_GF_TTY_BASE); 123 if (bi != NULL) { 124 struct bi_virt_dev *vd = bootinfo_dataptr(bi); 125 machine_cnattach(gftty_cnattach, vd->vd_mmio_base, 0x1000); 126 printf("Initialized Goldfish TTY console @ 0x%08x\n", 127 vd->vd_mmio_base); 128 } 129 #endif /* NGFTTY > 0 */ 130 131 /* 132 * Pass 2 at parsing bootinfo now that the MMU is enabled. 133 */ 134 bootinfo_startup2(nextpa); 135 136 /* 137 * Just use the default pager_map_size for now. We may decide 138 * to make it larger for large memory configs. 139 */ 140 141 machine_init_common(nextpa); 142 143 /* Check for RND seed from the loader. */ 144 bootinfo_setup_rndseed(); 145 146 char flags[32]; 147 if (bootinfo_getarg("flags", flags, sizeof(flags))) { 148 for (const char *cp = flags; *cp != '\0'; cp++) { 149 /* Consume 'm' in favor of BI_RAMDISK. */ 150 if (*cp == 'm') { 151 continue; 152 } 153 BOOT_FLAG(*cp, boothowto); 154 } 155 } 156 } 157 158 /* 159 * Console initialization: called early on from main, 160 * before vm init or startup. Do enough configuration 161 * to choose and initialize a console. 162 */ 163 void 164 consinit(void) 165 { 166 #ifdef DDB 167 if (boothowto & RB_KDB) 168 Debugger(); 169 #endif 170 } 171 172 /* 173 * cpu_startup: allocate memory for variable-sized tables, 174 * initialize CPU, and do autoconfiguration. 175 */ 176 void 177 cpu_startup(void) 178 { 179 struct bootinfo_data *bid = bootinfo_data(); 180 181 cpu_startup_common(); 182 183 if (bid->bootinfo_mem_segments_ignored) { 184 printf("WARNING: ignored %zd bytes of memory in %d segments.\n", 185 bid->bootinfo_mem_segments_ignored_bytes, 186 bid->bootinfo_mem_segments_ignored); 187 } 188 } 189 190 void 191 machine_set_model(void) 192 { 193 struct bi_record *bi = bootinfo_find(BI_VIRT_QEMU_VERSION); 194 if (bi != NULL) { 195 uint32_t qvers = bootinfo_get_u32(bi); 196 cpu_setmodel("Qemu %d.%d.%d Virt platform", 197 (qvers >> 24) & 0xff, 198 (qvers >> 16) & 0xff, 199 (qvers >> 8) & 0xff); 200 } else { 201 /* XXX Assume Nono. */ 202 cpu_setmodel("Nono Virt platform"); 203 } 204 } 205 206 /* 207 * machine dependent system variables. 208 */ 209 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup") 210 { 211 /* 212 * virt68k obviously does not have a non-working /RMC, but we 213 * provide this as a r/w node in order to faciliate testing. 214 */ 215 static bool broken_rmc; 216 217 sysctl_createv(clog, 0, NULL, NULL, 218 CTLFLAG_PERMANENT, 219 CTLTYPE_NODE, "machdep", NULL, 220 NULL, 0, NULL, 0, 221 CTL_MACHDEP, CTL_EOL); 222 223 sysctl_createv(clog, 0, NULL, NULL, 224 CTLFLAG_PERMANENT, 225 CTLTYPE_STRUCT, "console_device", NULL, 226 sysctl_consdev, 0, NULL, sizeof(dev_t), 227 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL); 228 229 sysctl_createv(clog, 0, NULL, NULL, 230 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 231 CTLTYPE_BOOL, "broken_rmc", NULL, 232 NULL, 0, &broken_rmc, 0, 233 CTL_MACHDEP, CPU_BROKEN_RMC, CTL_EOL); 234 } 235 236 static void 237 default_reset_func(void *arg, int howto) 238 { 239 printf("WARNING: No reset handler, holding here.\n\n"); 240 for (;;) { 241 /* spin forever. */ 242 } 243 } 244 245 static void (*cpu_reset_func)(void *, int) = default_reset_func; 246 static void *cpu_reset_func_arg; 247 248 void 249 cpu_set_reset_func(void (*func)(void *, int), void *arg) 250 { 251 if (cpu_reset_func == default_reset_func && func != NULL) { 252 cpu_reset_func = func; 253 cpu_reset_func_arg = arg; 254 } 255 } 256 257 void 258 machine_halt(void) 259 { 260 (*cpu_reset_func)(cpu_reset_func_arg, RB_HALT); 261 } 262 263 void 264 machine_reboot(int howto, char *bootstr) 265 { 266 (*cpu_reset_func)(cpu_reset_func_arg, RB_AUTOBOOT); 267 } 268 269 /* 270 * Level 7 interrupts are caused by e.g. the ABORT switch. 271 * 272 * If we have DDB, then break into DDB on ABORT. In a production 273 * environment, bumping the ABORT switch would be bad, so we enable 274 * panic'ing on ABORT with the kernel option "PANICBUTTON". 275 */ 276 int 277 nmihand(void *arg) 278 { 279 280 printf("NMI ignored.\n"); 281 282 return 1; 283 } 284 285 const uint16_t ipl2psl_table[NIPL] = { 286 [IPL_NONE] = PSL_S | PSL_IPL0, 287 [IPL_SOFTCLOCK] = PSL_S | PSL_IPL1, 288 [IPL_SOFTBIO] = PSL_S | PSL_IPL1, 289 [IPL_SOFTNET] = PSL_S | PSL_IPL1, 290 [IPL_SOFTSERIAL] = PSL_S | PSL_IPL1, 291 [IPL_VM] = PSL_S | PSL_IPL5, 292 [IPL_SCHED] = PSL_S | PSL_IPL6, 293 [IPL_HIGH] = PSL_S | PSL_IPL7, 294 }; 295