Home | History | Annotate | Line # | Download | only in m68k
      1 /*	$NetBSD: fpu.c,v 1.3 2025/04/10 19:22:20 nat Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 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 <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.3 2025/04/10 19:22:20 nat Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/time.h>
     43 #include <sys/kernel.h>
     44 #include <sys/device.h>
     45 
     46 #include <machine/psl.h>
     47 #include <machine/cpu.h>
     48 #include <machine/frame.h>
     49 
     50 extern int *nofault;
     51 
     52 int
     53 fpu_probe(void)
     54 {
     55 	/*
     56 	 * A 68881 idle frame is 28 bytes and a 68882's is 60 bytes.
     57 	 * We, of course, need to have enough room for either.
     58 	 */
     59 	struct fpframe fpframe;
     60 	label_t faultbuf;
     61 	uint8_t b;
     62 
     63 	nofault = (int *)&faultbuf;
     64 	if (setjmp(&faultbuf)) {
     65 		nofault = (int *)0;
     66 		return FPU_NONE;
     67 	}
     68 
     69 	/*
     70 	 * Synchronize FPU or cause a fault.
     71 	 * This should leave the 881/882 in the IDLE state,
     72 	 * state, so we can determine which we have by
     73 	 * examining the size of the FP state frame
     74 	 */
     75 	__asm("fnop");
     76 
     77 	nofault = NULL;
     78 
     79 	/*
     80 	 * Presumably, if we're an 040/060 and did not take exception
     81 	 * above, we have an FPU.  Don't bother probing.
     82 	 */
     83 	if (cputype == CPU_68060)
     84 		return FPU_68060;
     85 	if (cputype == CPU_68040)
     86 		return FPU_68040;
     87 
     88 	/*
     89 	 * Presumably, this will not cause a fault--the fnop should
     90 	 * have if this will.  We save the state in order to get the
     91 	 * size of the frame.
     92 	 */
     93 	__asm("fsave %0@" : : "a" (&fpframe) : "memory");
     94 
     95 	b = fpframe.fpf_fsize;
     96 
     97 	/*
     98 	 * Now, restore a NULL state to reset the FPU.
     99 	 */
    100 	fpframe.fpf_null = 0;
    101 	fpframe.fpf_idle.fpf_ccr = 0;
    102 	m68881_restore(&fpframe);
    103 
    104 	/*
    105 	 * The size of a 68881 IDLE frame is 0x18
    106 	 *         and a 68882 frame is 0x38
    107 	 */
    108 	if (b == 0x18)
    109 		return FPU_68881;
    110 	if (b == 0x38)
    111 		return FPU_68882;
    112 
    113 	/*
    114 	 * If it's not one of the above, we have no clue what it is.
    115 	 */
    116 	return FPU_UNKNOWN;
    117 }
    118