1 /* $NetBSD: fpu.c,v 1.10 2026/11/13 04:23:27 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1996, 2026 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Gordon W. Ross. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Floating Point Unit (MC68881/882/040/060) 34 * Probe for the FPU at early bootstrap. 35 */ 36 37 #include "opt_m68k_arch.h" 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.10 2026/11/13 04:23:27 thorpej Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/time.h> 45 #include <sys/kernel.h> 46 #include <sys/lwp.h> 47 48 #include <uvm/uvm_extern.h> 49 50 #include <m68k/cacheops.h> 51 52 #include <machine/pcb.h> 53 #include <machine/psl.h> 54 #include <machine/cpu.h> 55 #include <machine/frame.h> 56 #include <machine/vectors.h> 57 58 /* 59 * This is the default, but may be overridden as we determine 60 * the machine / cpu type before we ever get to fpu_init(). 61 * 62 * For systems that lack a coprocessor interface, we just default 63 * to NONE. 64 */ 65 #ifdef M68K_FPCOPROC 66 int fputype = FPU_UNKNOWN; 67 68 extern uint16_t * const fpu_patch_locs[]; 69 70 #if defined(M68020) || defined(M68030) || defined(M68040) 71 extern const uint16_t fpu_patch_disps[]; 72 #endif 73 74 #if defined(M68060) 75 extern const uint16_t fpu_patch_disps60[]; 76 #endif 77 78 /* 79 * Patch up the various FPU-related conditional code points. The default 80 * paths branch around FPU code. The patches update the 16-bit displacements 81 * in BRA.W insns to branch to the appropriate code path for the detected 82 * FPU. 83 */ 84 static void 85 fpu_patch(void) 86 { 87 const uint16_t *disps; 88 89 switch (fputype) { 90 #if defined(M68060) 91 case FPU_68060: 92 disps = fpu_patch_disps60; 93 break; 94 #endif 95 #if defined(M68020) || defined(M68030) || defined(M68040) 96 case FPU_68881: 97 case FPU_68882: 98 case FPU_68040: 99 disps = fpu_patch_disps; 100 break; 101 #endif 102 default: 103 return; 104 } 105 106 struct pmap_write_text_context ctx; 107 vaddr_t pgva, last_pgva = (vaddr_t)-1; 108 uint16_t *insn, *loc; 109 110 for (int i = 0; fpu_patch_locs[i] != NULL; i++) { 111 insn = fpu_patch_locs[i]; 112 loc = &insn[1]; 113 pgva = trunc_page((vaddr_t)loc); 114 if (pgva != last_pgva) { 115 if (last_pgva != (vaddr_t)-1) { 116 pmap_write_text_exit(&ctx); 117 } 118 if (! pmap_write_text_enter(pgva, &ctx)) { 119 panic("%s: cannot make loc=%p writable", 120 __func__, loc); 121 } 122 last_pgva = pgva; 123 } 124 *loc = disps[i]; 125 } 126 127 pmap_write_text_exit(&ctx); 128 ICIA(); 129 } 130 #else 131 int fputype = FPU_NONE; 132 #endif 133 134 void 135 fpu_init(void) 136 { 137 #ifdef M68K_FPCOPROC 138 /* 139 * A 68881 idle frame is 28 bytes and a 68882's is 60 bytes. 140 * We, of course, need to have enough room for either. 141 */ 142 struct pcb *pcb = lwp_getpcb(&lwp0); 143 struct fpframe fpframe; 144 label_t faultbuf; 145 146 /* Make sure lwp0 has a NULL state frame. */ 147 memset(&pcb->pcb_fpregs, 0, sizeof(pcb->pcb_fpregs)); 148 149 if (fputype != FPU_UNKNOWN) { 150 goto done; 151 } 152 153 nofault = &faultbuf; 154 if (setjmp(&faultbuf)) { 155 nofault = NULL; 156 fputype = FPU_NONE; 157 /* 158 * Set up the cached idle frame to mimic the 68881 159 * for the benefit of cpu_setmcontext(). 160 */ 161 fpu_idle_frame.fpf_version = 0x1f; 162 fpu_idle_frame.fpf_fsize = 0x18; 163 return; 164 } 165 166 /* 167 * Synchronize FPU or cause a fault. 168 * This should leave the 881/882 in the IDLE state, 169 * state, so we can determine which we have by 170 * examining the size of the FP state frame 171 */ 172 __asm volatile("fnop"); 173 174 nofault = NULL; 175 176 /* 177 * Presumably, if we're an 040/060 and did not take exception 178 * above, we have an FPU. Don't bother probing. 179 */ 180 if (cputype == CPU_68060) { 181 fputype = FPU_68060; 182 } else if (cputype == CPU_68040) { 183 fputype = FPU_68040; 184 } 185 186 if (fputype == FPU_UNKNOWN) { 187 /* 188 * fpu_doprobe() will have saved an FP state frame for us, 189 * and can consult it to intuit the FP type. 190 * 191 * The size of a 68881 IDLE frame is 0x18 192 * and a 68882 frame is 0x38 193 */ 194 __asm volatile("fsave %0@" : : "a" (&fpframe) : "memory"); 195 if (fpframe.fpf_fsize == 0x18) { 196 fputype = FPU_68881; 197 } else if (fpframe.fpf_fsize == 0x38) { 198 fputype = FPU_68882; 199 } 200 /* 201 * If it's not one of the above, we have no clue 202 * what it is. 203 */ 204 } 205 206 done: 207 /* Avoid crazy values. */ 208 if (fputype < 0 || fputype > FPU_UNKNOWN) { 209 fputype = FPU_UNKNOWN; 210 } 211 if (fputype != FPU_UNKNOWN) { 212 fpu_make_idle_frame(); 213 fpu_patch(); 214 vec_init_fp(); 215 } 216 #else 217 fputype = FPU_NONE; 218 #endif 219 } 220