1 /* $NetBSD: fpu.c,v 1.8 2026/10/31 20:53:49 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.8 2026/10/31 20:53:49 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 <machine/pcb.h> 49 #include <machine/psl.h> 50 #include <machine/cpu.h> 51 #include <machine/frame.h> 52 #include <machine/vectors.h> 53 54 /* 55 * This is the default, but may be overridden as we determine 56 * the machine / cpu type before we ever get to fpu_init(). 57 * 58 * For systems that lack a coprocessor interface, we just default 59 * to NONE. 60 */ 61 #ifdef M68K_FPCOPROC 62 int fputype = FPU_UNKNOWN; 63 #else 64 int fputype = FPU_NONE; 65 #endif 66 67 void 68 fpu_init(void) 69 { 70 #ifdef M68K_FPCOPROC 71 /* 72 * A 68881 idle frame is 28 bytes and a 68882's is 60 bytes. 73 * We, of course, need to have enough room for either. 74 */ 75 struct pcb *pcb = lwp_getpcb(&lwp0); 76 struct fpframe fpframe; 77 label_t faultbuf; 78 79 /* Make sure lwp0 has a NULL state frame. */ 80 memset(&pcb->pcb_fpregs, 0, sizeof(pcb->pcb_fpregs)); 81 82 if (fputype != FPU_UNKNOWN) { 83 goto done; 84 } 85 86 nofault = &faultbuf; 87 if (setjmp(&faultbuf)) { 88 nofault = NULL; 89 fputype = FPU_NONE; 90 /* 91 * Set up the cached idle frame to mimic the 68881 92 * for the benefit of cpu_setmcontext(). 93 */ 94 fpu_idle_frame.fpf_version = 0x1f; 95 fpu_idle_frame.fpf_fsize = 0x18; 96 return; 97 } 98 99 /* 100 * Synchronize FPU or cause a fault. 101 * This should leave the 881/882 in the IDLE state, 102 * state, so we can determine which we have by 103 * examining the size of the FP state frame 104 */ 105 __asm volatile("fnop"); 106 107 nofault = NULL; 108 109 /* 110 * Presumably, if we're an 040/060 and did not take exception 111 * above, we have an FPU. Don't bother probing. 112 */ 113 if (cputype == CPU_68060) { 114 fputype = FPU_68060; 115 } else if (cputype == CPU_68040) { 116 fputype = FPU_68040; 117 } 118 119 if (fputype == FPU_UNKNOWN) { 120 /* 121 * fpu_doprobe() will have saved an FP state frame for us, 122 * and can consult it to intuit the FP type. 123 * 124 * The size of a 68881 IDLE frame is 0x18 125 * and a 68882 frame is 0x38 126 */ 127 __asm volatile("fsave %0@" : : "a" (&fpframe) : "memory"); 128 if (fpframe.fpf_fsize == 0x18) { 129 fputype = FPU_68881; 130 } else if (fpframe.fpf_fsize == 0x38) { 131 fputype = FPU_68882; 132 } 133 /* 134 * If it's not one of the above, we have no clue 135 * what it is. 136 */ 137 } 138 139 done: 140 /* Avoid crazy values. */ 141 if (fputype < 0 || fputype > FPU_UNKNOWN) { 142 fputype = FPU_UNKNOWN; 143 } 144 if (fputype != FPU_UNKNOWN) { 145 fpu_make_idle_frame(); 146 vec_init_fp(); 147 } 148 #else 149 fputype = FPU_NONE; 150 #endif 151 } 152