Home | History | Annotate | Line # | Download | only in m68k
      1 /*	$NetBSD: fpu.c,v 1.6 2026/04/26 10:52:15 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.6 2026/04/26 10:52:15 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 		return;
     91 	}
     92 
     93 	/*
     94 	 * Synchronize FPU or cause a fault.
     95 	 * This should leave the 881/882 in the IDLE state,
     96 	 * state, so we can determine which we have by
     97 	 * examining the size of the FP state frame
     98 	 */
     99 	__asm volatile("fnop");
    100 
    101 	nofault = NULL;
    102 
    103 	/*
    104 	 * Presumably, if we're an 040/060 and did not take exception
    105 	 * above, we have an FPU.  Don't bother probing.
    106 	 */
    107 	if (cputype == CPU_68060) {
    108 		fputype = FPU_68060;
    109 	} else if (cputype == CPU_68040) {
    110 		fputype = FPU_68040;
    111 	}
    112 
    113 	if (fputype == FPU_UNKNOWN) {
    114 		/*
    115 		 * fpu_doprobe() will have saved an FP state frame for us,
    116 		 * and can consult it to intuit the FP type.
    117 		 *
    118 		 * The size of a 68881 IDLE frame is 0x18
    119 		 *         and a 68882 frame is 0x38
    120 		 */
    121 		__asm volatile("fsave %0@" : : "a" (&fpframe) : "memory");
    122 		if (fpframe.fpf_fsize == 0x18) {
    123 			fputype = FPU_68881;
    124 		} else if (fpframe.fpf_fsize == 0x38) {
    125 			fputype = FPU_68882;
    126 		}
    127 		/*
    128 		 * If it's not one of the above, we have no clue
    129 		 * what it is.
    130 		 */
    131 	}
    132 
    133  done:
    134 	/* Avoid crazy values. */
    135 	if (fputype < 0 || fputype > FPU_UNKNOWN) {
    136 		fputype = FPU_UNKNOWN;
    137 	}
    138 	if (fputype != FPU_UNKNOWN) {
    139 		m68k_make_fpu_idle_frame();
    140 		vec_init_fp();
    141 	}
    142 #else
    143 	fputype = FPU_NONE;
    144 #endif
    145 }
    146