fpu.c revision 82555
1270817Spluknet/*-
2253009Sedwin * Copyright (c) 1990 William Jolitz.
3192886Sedwin * Copyright (c) 1991 The Regents of the University of California.
4192886Sedwin * All rights reserved.
558787Sru *
6270817Spluknet * Redistribution and use in source and binary forms, with or without
7270817Spluknet * modification, are permitted provided that the following conditions
8270817Spluknet * are met:
9270817Spluknet * 1. Redistributions of source code must retain the above copyright
1019874Swollman *    notice, this list of conditions and the following disclaimer.
11270817Spluknet * 2. Redistributions in binary form must reproduce the above copyright
12270817Spluknet *    notice, this list of conditions and the following disclaimer in the
13257697Sedwin *    documentation and/or other materials provided with the distribution.
14270817Spluknet * 3. All advertising materials mentioning features or use of this software
15270817Spluknet *    must display the following acknowledgement:
16270817Spluknet *	This product includes software developed by the University of
17270817Spluknet *	California, Berkeley and its contributors.
1886222Swollman * 4. Neither the name of the University nor the names of its contributors
19253009Sedwin *    may be used to endorse or promote products derived from this software
20273719Sedwin *    without specific prior written permission.
21273719Sedwin *
22253009Sedwin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2319874Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2419874Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2519874Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2619874Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2719874Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2819874Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2919874Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3019874Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3119874Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3219874Swollman * SUCH DAMAGE.
33257697Sedwin *
34114173Swollman *	from: @(#)npx.c	7.2 (Berkeley) 5/12/91
3537998Sdes * $FreeBSD: head/sys/amd64/amd64/fpu.c 82555 2001-08-30 09:17:03Z msmith $
3637998Sdes */
3743014Swollman
3819874Swollman#include "opt_cpu.h"
39213312Sedwin#include "opt_debug_npx.h"
40273719Sedwin#include "opt_math_emulate.h"
4158787Sru
42263906Sedwin#include <sys/param.h>
43136638Swollman#include <sys/systm.h>
44184406Sedwin#include <sys/bus.h>
45184406Sedwin#include <sys/kernel.h>
46136638Swollman#include <sys/lock.h>
47163302Sru#include <sys/malloc.h>
48149514Swollman#include <sys/module.h>
49136638Swollman#include <sys/mutex.h>
50136638Swollman#include <sys/mutex.h>
51136638Swollman#include <sys/proc.h>
52184406Sedwin#include <sys/sysctl.h>
53136638Swollman#include <machine/bus.h>
54136638Swollman#include <sys/rman.h>
5519874Swollman#ifdef NPX_DEBUG
5619874Swollman#include <sys/syslog.h>
5719874Swollman#endif
58249692Sedwin#include <sys/signalvar.h>
59149514Swollman#include <sys/user.h>
60149514Swollman
6119874Swollman#ifndef SMP
6219874Swollman#include <machine/asmacros.h>
6375267Swollman#endif
6437998Sdes#include <machine/cputypes.h>
6537998Sdes#include <machine/frame.h>
6619874Swollman#include <machine/md_var.h>
6719874Swollman#include <machine/pcb.h>
68169811Swollman#include <machine/psl.h>
69169811Swollman#ifndef SMP
70171948Sedwin#include <machine/clock.h>
71136638Swollman#endif
7219874Swollman#include <machine/resource.h>
7319874Swollman#include <machine/specialreg.h>
7419874Swollman#include <machine/segments.h>
7575267Swollman
7619874Swollman#ifndef SMP
7719874Swollman#include <i386/isa/icu.h>
7819874Swollman#include <i386/isa/intr_machdep.h>
7919874Swollman#include <i386/isa/isa.h>
8019874Swollman#endif
8119874Swollman#include <isa/isavar.h>
82174242Sedwin
8319874Swollman/*
8419874Swollman * 387 and 287 Numeric Coprocessor Extension (NPX) Driver.
8519874Swollman */
86223629Sedwin
8775267Swollman/* Configuration flags. */
8843543Swollman#define	NPX_DISABLE_I586_OPTIMIZED_BCOPY	(1 << 0)
89121098Swollman#define	NPX_DISABLE_I586_OPTIMIZED_BZERO	(1 << 1)
9075267Swollman#define	NPX_DISABLE_I586_OPTIMIZED_COPYIO	(1 << 2)
9143543Swollman#define	NPX_PREFER_EMULATOR			(1 << 3)
9243543Swollman
93121098Swollman#ifdef	__GNUC__
94121098Swollman
95121098Swollman#define	fldcw(addr)		__asm("fldcw %0" : : "m" (*(addr)))
96121098Swollman#define	fnclex()		__asm("fnclex")
97181421Sedwin#define	fninit()		__asm("fninit")
98181421Sedwin#define	fnsave(addr)		__asm __volatile("fnsave %0" : "=m" (*(addr)))
9958787Sru#define	fnstcw(addr)		__asm __volatile("fnstcw %0" : "=m" (*(addr)))
10075267Swollman#define	fnstsw(addr)		__asm __volatile("fnstsw %0" : "=m" (*(addr)))
10175267Swollman#define	fp_divide_by_0()	__asm("fldz; fld1; fdiv %st,%st(1); fnop")
10275267Swollman#define	frstor(addr)		__asm("frstor %0" : : "m" (*(addr)))
10319874Swollman#ifdef CPU_ENABLE_SSE
10467578Swollman#define	fxrstor(addr)		__asm("fxrstor %0" : : "m" (*(addr)))
105192886Sedwin#define	fxsave(addr)		__asm __volatile("fxsave %0" : "=m" (*(addr)))
10619874Swollman#endif
10719874Swollman#define	start_emulating()	__asm("smsw %%ax; orb %0,%%al; lmsw %%ax" \
108163302Sru				      : : "n" (CR0_TS) : "ax")
109163302Sru#define	stop_emulating()	__asm("clts")
11019874Swollman
111158421Swollman#else	/* not __GNUC__ */
112163302Sru
113163302Sruvoid	fldcw		__P((caddr_t addr));
114257697Sedwinvoid	fnclex		__P((void));
11519874Swollmanvoid	fninit		__P((void));
11619874Swollmanvoid	fnsave		__P((caddr_t addr));
117169811Swollmanvoid	fnstcw		__P((caddr_t addr));
118149514Swollmanvoid	fnstsw		__P((caddr_t addr));
119270817Spluknetvoid	fp_divide_by_0	__P((void));
120163302Sruvoid	frstor		__P((caddr_t addr));
121163302Sru#ifdef CPU_ENABLE_SSE
12219874Swollmanvoid	fxsave		__P((caddr_t addr));
123163302Sruvoid	fxrstor		__P((caddr_t addr));
12419874Swollman#endif
12519874Swollmanvoid	start_emulating	__P((void));
12619874Swollmanvoid	stop_emulating	__P((void));
127171948Sedwin
12819874Swollman#endif	/* __GNUC__ */
129163302Sru
130240457Sedwin#ifdef CPU_ENABLE_SSE
13119874Swollman#define GET_FPU_CW(proc) \
13219874Swollman	(cpu_fxsr ? \
13319874Swollman		(proc)->p_addr->u_pcb.pcb_save.sv_xmm.sv_env.en_cw : \
13419874Swollman		(proc)->p_addr->u_pcb.pcb_save.sv_87.sv_env.en_cw)
13519874Swollman#define GET_FPU_SW(proc) \
13637998Sdes	(cpu_fxsr ? \
13737998Sdes		(proc)->p_addr->u_pcb.pcb_save.sv_xmm.sv_env.en_sw : \
13819874Swollman		(proc)->p_addr->u_pcb.pcb_save.sv_87.sv_env.en_sw)
13919874Swollman#define GET_FPU_EXSW_PTR(pcb) \
14019874Swollman	(cpu_fxsr ? \
14119874Swollman		&(pcb)->pcb_save.sv_xmm.sv_ex_sw : \
14219874Swollman		&(pcb)->pcb_save.sv_87.sv_ex_sw)
14386222Swollman#else /* CPU_ENABLE_SSE */
144270817Spluknet#define GET_FPU_CW(proc) \
14519874Swollman	(proc->p_addr->u_pcb.pcb_save.sv_87.sv_env.en_cw)
146270817Spluknet#define GET_FPU_SW(proc) \
147270817Spluknet	(proc->p_addr->u_pcb.pcb_save.sv_87.sv_env.en_sw)
14819874Swollman#define GET_FPU_EXSW_PTR(pcb) \
14919874Swollman	(&(pcb)->pcb_save.sv_87.sv_ex_sw)
15019874Swollman#endif /* CPU_ENABLE_SSE */
15119874Swollman
152223629Sedwintypedef u_char bool_t;
15319874Swollman
15419874Swollmanstatic	int	npx_attach	__P((device_t dev));
15519874Swollmanstatic	void	npx_identify	__P((driver_t *driver, device_t parent));
156248307Sedwin#ifndef SMP
157248307Sedwinstatic	void	npx_intr	__P((void *));
15819874Swollman#endif
15919874Swollmanstatic	int	npx_probe	__P((device_t dev));
16019874Swollmanstatic	int	npx_probe1	__P((device_t dev));
16119874Swollmanstatic	void	fpusave		__P((union savefpu *));
16219874Swollmanstatic	void	fpurstor	__P((union savefpu *));
16319874Swollman#ifdef I586_CPU_XXX
16419874Swollmanstatic	long	timezero	__P((const char *funcname,
16519874Swollman				     void (*func)(void *buf, size_t len)));
16619874Swollman#endif /* I586_CPU */
16719874Swollman
168169811Swollmanint	hw_float;		/* XXX currently just alias for npx_exists */
16919874Swollman
17019874SwollmanSYSCTL_INT(_hw,HW_FLOATINGPT, floatingpoint,
17119874Swollman	CTLFLAG_RD, &hw_float, 0,
17219874Swollman	"Floatingpoint instructions executed in hardware");
17319874Swollman
17419874Swollman#ifndef SMP
17519874Swollmanstatic	volatile u_int		npx_intrs_while_probing;
176210718Sedwinstatic	volatile u_int		npx_traps_while_probing;
177210718Sedwin#endif
17819874Swollman
179169811Swollmanstatic	bool_t			npx_ex16;
18019874Swollmanstatic	bool_t			npx_exists;
18119874Swollmanstatic	bool_t			npx_irq13;
182171948Sedwinstatic	int			npx_irq;	/* irq number */
18319874Swollman
18419874Swollman#ifndef SMP
18519874Swollman/*
186163302Sru * Special interrupt handlers.  Someday intr0-intr15 will be used to count
18719874Swollman * interrupts.  We'll still need a special exception 16 handler.  The busy
18819874Swollman * latch stuff in probeintr() can be moved to npxprobe().
18986222Swollman */
19093799Swollmaninthand_t probeintr;
191163302Sru__asm("								\n\
19293799Swollman	.text							\n\
19319874Swollman	.p2align 2,0x90						\n\
19419874Swollman	.type	" __XSTRING(CNAME(probeintr)) ",@function	\n\
19519874Swollman" __XSTRING(CNAME(probeintr)) ":				\n\
19619874Swollman	ss							\n\
19719874Swollman	incl	" __XSTRING(CNAME(npx_intrs_while_probing)) "	\n\
19819874Swollman	pushl	%eax						\n\
19919874Swollman	movb	$0x20,%al	# EOI (asm in strings loses cpp features) \n\
20019874Swollman	outb	%al,$0xa0	# IO_ICU2			\n\
20119874Swollman	outb	%al,$0x20	# IO_ICU1			\n\
20219874Swollman	movb	$0,%al						\n\
20358787Sru	outb	%al,$0xf0	# clear BUSY# latch		\n\
20419874Swollman	popl	%eax						\n\
20519874Swollman	iret							\n\
20619874Swollman");
20719874Swollman
20819874Swollmaninthand_t probetrap;
20986222Swollman__asm("								\n\
210219149Sedwin	.text							\n\
211219149Sedwin	.p2align 2,0x90						\n\
21219874Swollman	.type	" __XSTRING(CNAME(probetrap)) ",@function	\n\
213253009Sedwin" __XSTRING(CNAME(probetrap)) ":				\n\
214163302Sru	ss							\n\
215177591Sedwin	incl	" __XSTRING(CNAME(npx_traps_while_probing)) "	\n\
21619874Swollman	fnclex							\n\
21719874Swollman	iret							\n\
21819874Swollman");
21919874Swollman#endif /* SMP */
22019874Swollman
221169811Swollman/*
222257697Sedwin * Identify routine.  Create a connection point on our parent for probing.
22319874Swollman */
22443543Swollmanstatic void
22519874Swollmannpx_identify(driver, parent)
22619874Swollman	driver_t *driver;
22719874Swollman	device_t parent;
22819874Swollman{
22919874Swollman	device_t child;
23019874Swollman
23119874Swollman	child = BUS_ADD_CHILD(parent, 0, "npx", 0);
23219874Swollman	if (child == NULL)
23319874Swollman		panic("npx_identify");
23419874Swollman}
23519874Swollman
23619874Swollman#ifndef SMP
23793799Swollman/*
23893799Swollman * Do minimal handling of npx interrupts to convert them to traps.
23993799Swollman */
24093799Swollmanstatic void
24193799Swollmannpx_intr(dummy)
24219874Swollman	void *dummy;
24319874Swollman{
24419874Swollman	struct proc *p;
24519874Swollman
24619874Swollman	/*
24719874Swollman	 * The BUSY# latch must be cleared in all cases so that the next
24819874Swollman	 * unmasked npx exception causes an interrupt.
24919874Swollman	 */
25019874Swollman	outb(0xf0, 0);
25119874Swollman
25219874Swollman	/*
25319874Swollman	 * npxproc is normally non-null here.  In that case, schedule an
25419874Swollman	 * AST to finish the exception handling in the correct context
255226976Sedwin	 * (this interrupt may occur after the process has entered the
256169811Swollman	 * kernel via a syscall or an interrupt).  Otherwise, the npx
257174242Sedwin	 * state of the process that caused this interrupt must have been
25819874Swollman	 * pushed to the process' pcb, and clearing of the busy latch
25919874Swollman	 * above has finished the (essentially null) handling of this
26019874Swollman	 * interrupt.  Control will eventually return to the instruction
26119874Swollman	 * that caused it and it will repeat.  We will eventually (usually
262149514Swollman	 * soon) win the race to handle the interrupt properly.
26319874Swollman	 */
26458787Sru	p = PCPU_GET(npxproc);
26586222Swollman	if (p != NULL) {
26686222Swollman		p->p_addr->u_pcb.pcb_flags |= PCB_NPXTRAP;
26793799Swollman		mtx_lock_spin(&sched_lock);
26819874Swollman		p->p_sflag |= PS_ASTPENDING;
26919874Swollman		mtx_unlock_spin(&sched_lock);
27019874Swollman	}
271158421Swollman}
27219874Swollman
27319874Swollman/*
27419874Swollman * XXX these "local" variables of npx_probe() are non-local so that
27519874Swollman * npxprobe1() can abuse them.
27658787Sru */
277279707Sedwinstatic	int	npx_intrno;
27864499Swollmanstatic	struct	gate_descriptor save_idt_npxintr;
279203019Sedwin#endif /* !SMP */
280203019Sedwin
28158787Sru/*
282203019Sedwin * Probe routine.  Initialize cr0 to give correct behaviour for [f]wait
283203019Sedwin * whether the device exists or not (XXX should be elsewhere).  Set flags
28458787Sru * to tell npxattach() what to do.  Modify device struct if npx doesn't
285203019Sedwin * need to use interrupts.  Return 1 if device exists.
286203019Sedwin */
287207898Sedwinstatic int
28819874Swollmannpx_probe(dev)
28919874Swollman	device_t dev;
29019874Swollman{
29119874Swollman#ifdef SMP
292192886Sedwin
29319874Swollman	if (resource_int_value("npx", 0, "irq", &npx_irq) != 0)
29419874Swollman		npx_irq = 13;
29519874Swollman	return npx_probe1(dev);
29619874Swollman
29719874Swollman#else /* SMP */
29819874Swollman
299187588Sedwin	int	result;
30019874Swollman	critical_t	savecrit;
301183864Sedwin	u_char	save_icu1_mask;
30219874Swollman	u_char	save_icu2_mask;
303121098Swollman	struct	gate_descriptor save_idt_npxtrap;
30419874Swollman	/*
30519874Swollman	 * This routine is now just a wrapper for npxprobe1(), to install
30619874Swollman	 * special npx interrupt and trap handlers, to enable npx interrupts
30719874Swollman	 * and to disable other interrupts.  Someday isa_configure() will
30819874Swollman	 * install suitable handlers and run with interrupts enabled so we
30919874Swollman	 * won't need to do so much here.
310273719Sedwin	 */
311273719Sedwin	if (resource_int_value("npx", 0, "irq", &npx_irq) != 0)
31219874Swollman		npx_irq = 13;
31319874Swollman	npx_intrno = NRSVIDT + npx_irq;
31419874Swollman	savecrit = critical_enter();
31519874Swollman	save_icu1_mask = inb(IO_ICU1 + 1);
31619874Swollman	save_icu2_mask = inb(IO_ICU2 + 1);
31719874Swollman	save_idt_npxintr = idt[npx_intrno];
318226289Sedwin	save_idt_npxtrap = idt[16];
319226289Sedwin	outb(IO_ICU1 + 1, ~IRQ_SLAVE);
32019874Swollman	outb(IO_ICU2 + 1, ~(1 << (npx_irq - 8)));
32119874Swollman	setidt(16, probetrap, SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
32219874Swollman	setidt(npx_intrno, probeintr, SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
32319874Swollman
32419874Swollman	/*
32519874Swollman	 * XXX This looks highly bogus, but it appears that npc_probe1
32619874Swollman	 * needs interrupts enabled.  Does this make any difference
32719874Swollman	 * here?
328163302Sru	 */
32937998Sdes	critical_exit(savecrit);
330270817Spluknet	result = npx_probe1(dev);
331270817Spluknet	savecrit = critical_enter();
332163302Sru	outb(IO_ICU1 + 1, save_icu1_mask);
333270817Spluknet	outb(IO_ICU2 + 1, save_icu2_mask);
33419874Swollman	idt[npx_intrno] = save_idt_npxintr;
33519874Swollman	idt[16] = save_idt_npxtrap;
33619874Swollman	critical_exit(savecrit);
337270817Spluknet	return (result);
33819874Swollman
33937998Sdes#endif /* SMP */
340270817Spluknet}
34119874Swollman
342248307Sedwinstatic int
34337998Sdesnpx_probe1(dev)
34486222Swollman	device_t dev;
345248307Sedwin{
346270817Spluknet#ifndef SMP
347270817Spluknet	u_short control;
348270817Spluknet	u_short status;
349270817Spluknet#endif
35019874Swollman
35119874Swollman	/*
35219874Swollman	 * Partially reset the coprocessor, if any.  Some BIOS's don't reset
35319874Swollman	 * it after a warm boot.
35419874Swollman	 */
35519874Swollman	outb(0xf1, 0);		/* full reset on some systems, NOP on others */
35619874Swollman	outb(0xf0, 0);		/* clear BUSY# latch */
35719874Swollman	/*
35819874Swollman	 * Prepare to trap all ESC (i.e., NPX) instructions and all WAIT
359175034Sedwin	 * instructions.  We must set the CR0_MP bit and use the CR0_TS
36019874Swollman	 * bit to control the trap, because setting the CR0_EM bit does
36119874Swollman	 * not cause WAIT instructions to trap.  It's important to trap
36219874Swollman	 * WAIT instructions - otherwise the "wait" variants of no-wait
36319874Swollman	 * control instructions would degenerate to the "no-wait" variants
36419874Swollman	 * after FP context switches but work correctly otherwise.  It's
36519874Swollman	 * particularly important to trap WAITs when there is no NPX -
366226289Sedwin	 * otherwise the "wait" variants would always degenerate.
36719874Swollman	 *
36819874Swollman	 * Try setting CR0_NE to get correct error reporting on 486DX's.
369223629Sedwin	 * Setting it should fail or do nothing on lesser processors.
37019874Swollman	 */
37119874Swollman	load_cr0(rcr0() | CR0_MP | CR0_NE);
37219874Swollman	/*
37319874Swollman	 * But don't trap while we're probing.
37437998Sdes	 */
37519874Swollman	stop_emulating();
37619874Swollman	/*
37719874Swollman	 * Finish resetting the coprocessor, if any.  If there is an error
37819874Swollman	 * pending, then we may get a bogus IRQ13, but probeintr() will handle
379105195Swollman	 * it OK.  Bogus halts have never been observed, but we enabled
38067578Swollman	 * IRQ13 and cleared the BUSY# latch early to handle them anyway.
38119874Swollman	 */
382192886Sedwin	fninit();
38319874Swollman
38419874Swollman#ifdef SMP
38519874Swollman	/*
38619874Swollman	 * Exception 16 MUST work for SMP.
38719874Swollman	 */
38819874Swollman	npx_irq13 = 0;
38958787Sru	npx_ex16 = hw_float = npx_exists = 1;
390171948Sedwin	device_set_desc(dev, "math processor");
39119874Swollman	return (0);
392171948Sedwin
39319874Swollman#else /* !SMP */
39419874Swollman	device_set_desc(dev, "math processor");
39519874Swollman
39619874Swollman	/*
397149514Swollman	 * Don't use fwait here because it might hang.
39867578Swollman	 * Don't use fnop here because it usually hangs if there is no FPU.
399158421Swollman	 */
400171948Sedwin	DELAY(1000);		/* wait for any IRQ13 */
401169811Swollman#ifdef DIAGNOSTIC
402158421Swollman	if (npx_intrs_while_probing != 0)
403184406Sedwin		printf("fninit caused %u bogus npx interrupt(s)\n",
404158421Swollman		       npx_intrs_while_probing);
40519874Swollman	if (npx_traps_while_probing != 0)
406171948Sedwin		printf("fninit caused %u bogus npx trap(s)\n",
407184406Sedwin		       npx_traps_while_probing);
408158421Swollman#endif
40986222Swollman	/*
410158421Swollman	 * Check for a status of mostly zero.
411219149Sedwin	 */
41219874Swollman	status = 0x5a5a;
41319874Swollman	fnstsw(&status);
414257697Sedwin	if ((status & 0xb8ff) == 0) {
41519874Swollman		/*
416270817Spluknet		 * Good, now check for a proper control word.
41719874Swollman		 */
41819874Swollman		control = 0x5a5a;
419219411Sedwin		fnstcw(&control);
42019874Swollman		if ((control & 0x1f3f) == 0x033f) {
42119874Swollman			hw_float = npx_exists = 1;
42219874Swollman			/*
42319874Swollman			 * We have an npx, now divide by 0 to see if exception
42419874Swollman			 * 16 works.
42543014Swollman			 */
42643014Swollman			control &= ~(1 << 2);	/* enable divide by 0 trap */
427192886Sedwin			fldcw(&control);
42819874Swollman			npx_traps_while_probing = npx_intrs_while_probing = 0;
42919874Swollman			fp_divide_by_0();
43019874Swollman			if (npx_traps_while_probing != 0) {
43119874Swollman				/*
432177591Sedwin				 * Good, exception 16 works.
43319874Swollman				 */
43419874Swollman				npx_ex16 = 1;
43519874Swollman				return (0);
43619874Swollman			}
43719874Swollman			if (npx_intrs_while_probing != 0) {
43819874Swollman				int	rid;
43919874Swollman				struct	resource *r;
44019874Swollman				void	*intr;
441				/*
442				 * Bad, we are stuck with IRQ13.
443				 */
444				npx_irq13 = 1;
445
446				/*
447				 * We allocate these resources permanently,
448				 * so there is no need to keep track of them.
449				 */
450				rid = 0;
451				r = bus_alloc_resource(dev, SYS_RES_IOPORT,
452						       &rid, IO_NPX, IO_NPX,
453						       IO_NPXSIZE, RF_ACTIVE);
454				if (r == 0)
455					panic("npx: can't get ports");
456				rid = 0;
457				r = bus_alloc_resource(dev, SYS_RES_IRQ,
458						       &rid, npx_irq, npx_irq,
459						       1, RF_ACTIVE);
460				if (r == 0)
461					panic("npx: can't get IRQ");
462				BUS_SETUP_INTR(device_get_parent(dev),
463					       dev, r,
464					       INTR_TYPE_MISC | INTR_FAST,
465					       npx_intr, 0, &intr);
466				if (intr == 0)
467					panic("npx: can't create intr");
468
469				/*
470				 * XXX BUS_SETUP_INTR() has changed
471				 * idt[npx_intrno] to point to Xfastintr0
472				 * instead of Xfastintr0.  Adjust
473				 * save_idt_npxintr so that npxprobe()
474				 * doesn't undo this.
475				 */
476				save_idt_npxintr = idt[npx_intrno];
477
478				return (0);
479			}
480			/*
481			 * Worse, even IRQ13 is broken.  Use emulator.
482			 */
483		}
484	}
485	/*
486	 * Probe failed, but we want to get to npxattach to initialize the
487	 * emulator and say that it has been installed.  XXX handle devices
488	 * that aren't really devices better.
489	 */
490	return (0);
491#endif /* SMP */
492}
493
494/*
495 * Attach routine - announce which it is, and wire into system
496 */
497int
498npx_attach(dev)
499	device_t dev;
500{
501	int flags;
502
503	if (resource_int_value("npx", 0, "flags", &flags) != 0)
504		flags = 0;
505
506	if (flags)
507		device_printf(dev, "flags 0x%x ", flags);
508	if (npx_irq13) {
509		device_printf(dev, "using IRQ 13 interface\n");
510	} else {
511#if defined(MATH_EMULATE) || defined(GPL_MATH_EMULATE)
512		if (npx_ex16) {
513			if (!(flags & NPX_PREFER_EMULATOR))
514				device_printf(dev, "INT 16 interface\n");
515			else {
516				device_printf(dev, "FPU exists, but flags request "
517				    "emulator\n");
518				hw_float = npx_exists = 0;
519			}
520		} else if (npx_exists) {
521			device_printf(dev, "error reporting broken; using 387 emulator\n");
522			hw_float = npx_exists = 0;
523		} else
524			device_printf(dev, "387 emulator\n");
525#else
526		if (npx_ex16) {
527			device_printf(dev, "INT 16 interface\n");
528			if (flags & NPX_PREFER_EMULATOR) {
529				device_printf(dev, "emulator requested, but none compiled "
530				    "into kernel, using FPU\n");
531			}
532		} else
533			device_printf(dev, "no 387 emulator in kernel and no FPU!\n");
534#endif
535	}
536	npxinit(__INITIAL_NPXCW__);
537
538#ifdef I586_CPU_XXX
539	if (cpu_class == CPUCLASS_586 && npx_ex16 && npx_exists &&
540	    timezero("i586_bzero()", i586_bzero) <
541	    timezero("bzero()", bzero) * 4 / 5) {
542		if (!(flags & NPX_DISABLE_I586_OPTIMIZED_BCOPY)) {
543			bcopy_vector = i586_bcopy;
544			ovbcopy_vector = i586_bcopy;
545		}
546		if (!(flags & NPX_DISABLE_I586_OPTIMIZED_BZERO))
547			bzero = i586_bzero;
548		if (!(flags & NPX_DISABLE_I586_OPTIMIZED_COPYIO)) {
549			copyin_vector = i586_copyin;
550			copyout_vector = i586_copyout;
551		}
552	}
553#endif
554
555	return (0);		/* XXX unused */
556}
557
558/*
559 * Initialize floating point unit.
560 */
561void
562npxinit(control)
563	u_short control;
564{
565	static union savefpu dummy;
566	critical_t savecrit;
567
568	if (!npx_exists)
569		return;
570	/*
571	 * fninit has the same h/w bugs as fnsave.  Use the detoxified
572	 * fnsave to throw away any junk in the fpu.  npxsave() initializes
573	 * the fpu and sets npxproc = NULL as important side effects.
574	 */
575	savecrit = critical_enter();
576	npxsave(&dummy);
577	stop_emulating();
578	fldcw(&control);
579	if (PCPU_GET(curpcb) != NULL)
580		fpusave(&PCPU_GET(curpcb)->pcb_save);
581	start_emulating();
582	critical_exit(savecrit);
583}
584
585/*
586 * Free coprocessor (if we have it).
587 */
588void
589npxexit(p)
590	struct proc *p;
591{
592	critical_t savecrit;
593
594	savecrit = critical_enter();
595	if (p == PCPU_GET(npxproc))
596		npxsave(&PCPU_GET(curpcb)->pcb_save);
597	critical_exit(savecrit);
598#ifdef NPX_DEBUG
599	if (npx_exists) {
600		u_int	masked_exceptions;
601
602		masked_exceptions = PCPU_GET(curpcb)->pcb_save.sv_87.sv_env.en_cw
603		    & PCPU_GET(curpcb)->pcb_save.sv_87.sv_env.en_sw & 0x7f;
604		/*
605		 * Log exceptions that would have trapped with the old
606		 * control word (overflow, divide by 0, and invalid operand).
607		 */
608		if (masked_exceptions & 0x0d)
609			log(LOG_ERR,
610	"pid %d (%s) exited with masked floating point exceptions 0x%02x\n",
611			    p->p_pid, p->p_comm, masked_exceptions);
612	}
613#endif
614}
615
616/*
617 * The following mechanism is used to ensure that the FPE_... value
618 * that is passed as a trapcode to the signal handler of the user
619 * process does not have more than one bit set.
620 *
621 * Multiple bits may be set if the user process modifies the control
622 * word while a status word bit is already set.  While this is a sign
623 * of bad coding, we have no choise than to narrow them down to one
624 * bit, since we must not send a trapcode that is not exactly one of
625 * the FPE_ macros.
626 *
627 * The mechanism has a static table with 127 entries.  Each combination
628 * of the 7 FPU status word exception bits directly translates to a
629 * position in this table, where a single FPE_... value is stored.
630 * This FPE_... value stored there is considered the "most important"
631 * of the exception bits and will be sent as the signal code.  The
632 * precedence of the bits is based upon Intel Document "Numerical
633 * Applications", Chapter "Special Computational Situations".
634 *
635 * The macro to choose one of these values does these steps: 1) Throw
636 * away status word bits that cannot be masked.  2) Throw away the bits
637 * currently masked in the control word, assuming the user isn't
638 * interested in them anymore.  3) Reinsert status word bit 7 (stack
639 * fault) if it is set, which cannot be masked but must be presered.
640 * 4) Use the remaining bits to point into the trapcode table.
641 *
642 * The 6 maskable bits in order of their preference, as stated in the
643 * above referenced Intel manual:
644 * 1  Invalid operation (FP_X_INV)
645 * 1a   Stack underflow
646 * 1b   Stack overflow
647 * 1c   Operand of unsupported format
648 * 1d   SNaN operand.
649 * 2  QNaN operand (not an exception, irrelavant here)
650 * 3  Any other invalid-operation not mentioned above or zero divide
651 *      (FP_X_INV, FP_X_DZ)
652 * 4  Denormal operand (FP_X_DNML)
653 * 5  Numeric over/underflow (FP_X_OFL, FP_X_UFL)
654 * 6  Inexact result (FP_X_IMP)
655 */
656static char fpetable[128] = {
657	0,
658	FPE_FLTINV,	/*  1 - INV */
659	FPE_FLTUND,	/*  2 - DNML */
660	FPE_FLTINV,	/*  3 - INV | DNML */
661	FPE_FLTDIV,	/*  4 - DZ */
662	FPE_FLTINV,	/*  5 - INV | DZ */
663	FPE_FLTDIV,	/*  6 - DNML | DZ */
664	FPE_FLTINV,	/*  7 - INV | DNML | DZ */
665	FPE_FLTOVF,	/*  8 - OFL */
666	FPE_FLTINV,	/*  9 - INV | OFL */
667	FPE_FLTUND,	/*  A - DNML | OFL */
668	FPE_FLTINV,	/*  B - INV | DNML | OFL */
669	FPE_FLTDIV,	/*  C - DZ | OFL */
670	FPE_FLTINV,	/*  D - INV | DZ | OFL */
671	FPE_FLTDIV,	/*  E - DNML | DZ | OFL */
672	FPE_FLTINV,	/*  F - INV | DNML | DZ | OFL */
673	FPE_FLTUND,	/* 10 - UFL */
674	FPE_FLTINV,	/* 11 - INV | UFL */
675	FPE_FLTUND,	/* 12 - DNML | UFL */
676	FPE_FLTINV,	/* 13 - INV | DNML | UFL */
677	FPE_FLTDIV,	/* 14 - DZ | UFL */
678	FPE_FLTINV,	/* 15 - INV | DZ | UFL */
679	FPE_FLTDIV,	/* 16 - DNML | DZ | UFL */
680	FPE_FLTINV,	/* 17 - INV | DNML | DZ | UFL */
681	FPE_FLTOVF,	/* 18 - OFL | UFL */
682	FPE_FLTINV,	/* 19 - INV | OFL | UFL */
683	FPE_FLTUND,	/* 1A - DNML | OFL | UFL */
684	FPE_FLTINV,	/* 1B - INV | DNML | OFL | UFL */
685	FPE_FLTDIV,	/* 1C - DZ | OFL | UFL */
686	FPE_FLTINV,	/* 1D - INV | DZ | OFL | UFL */
687	FPE_FLTDIV,	/* 1E - DNML | DZ | OFL | UFL */
688	FPE_FLTINV,	/* 1F - INV | DNML | DZ | OFL | UFL */
689	FPE_FLTRES,	/* 20 - IMP */
690	FPE_FLTINV,	/* 21 - INV | IMP */
691	FPE_FLTUND,	/* 22 - DNML | IMP */
692	FPE_FLTINV,	/* 23 - INV | DNML | IMP */
693	FPE_FLTDIV,	/* 24 - DZ | IMP */
694	FPE_FLTINV,	/* 25 - INV | DZ | IMP */
695	FPE_FLTDIV,	/* 26 - DNML | DZ | IMP */
696	FPE_FLTINV,	/* 27 - INV | DNML | DZ | IMP */
697	FPE_FLTOVF,	/* 28 - OFL | IMP */
698	FPE_FLTINV,	/* 29 - INV | OFL | IMP */
699	FPE_FLTUND,	/* 2A - DNML | OFL | IMP */
700	FPE_FLTINV,	/* 2B - INV | DNML | OFL | IMP */
701	FPE_FLTDIV,	/* 2C - DZ | OFL | IMP */
702	FPE_FLTINV,	/* 2D - INV | DZ | OFL | IMP */
703	FPE_FLTDIV,	/* 2E - DNML | DZ | OFL | IMP */
704	FPE_FLTINV,	/* 2F - INV | DNML | DZ | OFL | IMP */
705	FPE_FLTUND,	/* 30 - UFL | IMP */
706	FPE_FLTINV,	/* 31 - INV | UFL | IMP */
707	FPE_FLTUND,	/* 32 - DNML | UFL | IMP */
708	FPE_FLTINV,	/* 33 - INV | DNML | UFL | IMP */
709	FPE_FLTDIV,	/* 34 - DZ | UFL | IMP */
710	FPE_FLTINV,	/* 35 - INV | DZ | UFL | IMP */
711	FPE_FLTDIV,	/* 36 - DNML | DZ | UFL | IMP */
712	FPE_FLTINV,	/* 37 - INV | DNML | DZ | UFL | IMP */
713	FPE_FLTOVF,	/* 38 - OFL | UFL | IMP */
714	FPE_FLTINV,	/* 39 - INV | OFL | UFL | IMP */
715	FPE_FLTUND,	/* 3A - DNML | OFL | UFL | IMP */
716	FPE_FLTINV,	/* 3B - INV | DNML | OFL | UFL | IMP */
717	FPE_FLTDIV,	/* 3C - DZ | OFL | UFL | IMP */
718	FPE_FLTINV,	/* 3D - INV | DZ | OFL | UFL | IMP */
719	FPE_FLTDIV,	/* 3E - DNML | DZ | OFL | UFL | IMP */
720	FPE_FLTINV,	/* 3F - INV | DNML | DZ | OFL | UFL | IMP */
721	FPE_FLTSUB,	/* 40 - STK */
722	FPE_FLTSUB,	/* 41 - INV | STK */
723	FPE_FLTUND,	/* 42 - DNML | STK */
724	FPE_FLTSUB,	/* 43 - INV | DNML | STK */
725	FPE_FLTDIV,	/* 44 - DZ | STK */
726	FPE_FLTSUB,	/* 45 - INV | DZ | STK */
727	FPE_FLTDIV,	/* 46 - DNML | DZ | STK */
728	FPE_FLTSUB,	/* 47 - INV | DNML | DZ | STK */
729	FPE_FLTOVF,	/* 48 - OFL | STK */
730	FPE_FLTSUB,	/* 49 - INV | OFL | STK */
731	FPE_FLTUND,	/* 4A - DNML | OFL | STK */
732	FPE_FLTSUB,	/* 4B - INV | DNML | OFL | STK */
733	FPE_FLTDIV,	/* 4C - DZ | OFL | STK */
734	FPE_FLTSUB,	/* 4D - INV | DZ | OFL | STK */
735	FPE_FLTDIV,	/* 4E - DNML | DZ | OFL | STK */
736	FPE_FLTSUB,	/* 4F - INV | DNML | DZ | OFL | STK */
737	FPE_FLTUND,	/* 50 - UFL | STK */
738	FPE_FLTSUB,	/* 51 - INV | UFL | STK */
739	FPE_FLTUND,	/* 52 - DNML | UFL | STK */
740	FPE_FLTSUB,	/* 53 - INV | DNML | UFL | STK */
741	FPE_FLTDIV,	/* 54 - DZ | UFL | STK */
742	FPE_FLTSUB,	/* 55 - INV | DZ | UFL | STK */
743	FPE_FLTDIV,	/* 56 - DNML | DZ | UFL | STK */
744	FPE_FLTSUB,	/* 57 - INV | DNML | DZ | UFL | STK */
745	FPE_FLTOVF,	/* 58 - OFL | UFL | STK */
746	FPE_FLTSUB,	/* 59 - INV | OFL | UFL | STK */
747	FPE_FLTUND,	/* 5A - DNML | OFL | UFL | STK */
748	FPE_FLTSUB,	/* 5B - INV | DNML | OFL | UFL | STK */
749	FPE_FLTDIV,	/* 5C - DZ | OFL | UFL | STK */
750	FPE_FLTSUB,	/* 5D - INV | DZ | OFL | UFL | STK */
751	FPE_FLTDIV,	/* 5E - DNML | DZ | OFL | UFL | STK */
752	FPE_FLTSUB,	/* 5F - INV | DNML | DZ | OFL | UFL | STK */
753	FPE_FLTRES,	/* 60 - IMP | STK */
754	FPE_FLTSUB,	/* 61 - INV | IMP | STK */
755	FPE_FLTUND,	/* 62 - DNML | IMP | STK */
756	FPE_FLTSUB,	/* 63 - INV | DNML | IMP | STK */
757	FPE_FLTDIV,	/* 64 - DZ | IMP | STK */
758	FPE_FLTSUB,	/* 65 - INV | DZ | IMP | STK */
759	FPE_FLTDIV,	/* 66 - DNML | DZ | IMP | STK */
760	FPE_FLTSUB,	/* 67 - INV | DNML | DZ | IMP | STK */
761	FPE_FLTOVF,	/* 68 - OFL | IMP | STK */
762	FPE_FLTSUB,	/* 69 - INV | OFL | IMP | STK */
763	FPE_FLTUND,	/* 6A - DNML | OFL | IMP | STK */
764	FPE_FLTSUB,	/* 6B - INV | DNML | OFL | IMP | STK */
765	FPE_FLTDIV,	/* 6C - DZ | OFL | IMP | STK */
766	FPE_FLTSUB,	/* 6D - INV | DZ | OFL | IMP | STK */
767	FPE_FLTDIV,	/* 6E - DNML | DZ | OFL | IMP | STK */
768	FPE_FLTSUB,	/* 6F - INV | DNML | DZ | OFL | IMP | STK */
769	FPE_FLTUND,	/* 70 - UFL | IMP | STK */
770	FPE_FLTSUB,	/* 71 - INV | UFL | IMP | STK */
771	FPE_FLTUND,	/* 72 - DNML | UFL | IMP | STK */
772	FPE_FLTSUB,	/* 73 - INV | DNML | UFL | IMP | STK */
773	FPE_FLTDIV,	/* 74 - DZ | UFL | IMP | STK */
774	FPE_FLTSUB,	/* 75 - INV | DZ | UFL | IMP | STK */
775	FPE_FLTDIV,	/* 76 - DNML | DZ | UFL | IMP | STK */
776	FPE_FLTSUB,	/* 77 - INV | DNML | DZ | UFL | IMP | STK */
777	FPE_FLTOVF,	/* 78 - OFL | UFL | IMP | STK */
778	FPE_FLTSUB,	/* 79 - INV | OFL | UFL | IMP | STK */
779	FPE_FLTUND,	/* 7A - DNML | OFL | UFL | IMP | STK */
780	FPE_FLTSUB,	/* 7B - INV | DNML | OFL | UFL | IMP | STK */
781	FPE_FLTDIV,	/* 7C - DZ | OFL | UFL | IMP | STK */
782	FPE_FLTSUB,	/* 7D - INV | DZ | OFL | UFL | IMP | STK */
783	FPE_FLTDIV,	/* 7E - DNML | DZ | OFL | UFL | IMP | STK */
784	FPE_FLTSUB,	/* 7F - INV | DNML | DZ | OFL | UFL | IMP | STK */
785};
786
787/*
788 * Preserve the FP status word, clear FP exceptions, then generate a SIGFPE.
789 *
790 * Clearing exceptions is necessary mainly to avoid IRQ13 bugs.  We now
791 * depend on longjmp() restoring a usable state.  Restoring the state
792 * or examining it might fail if we didn't clear exceptions.
793 *
794 * The error code chosen will be one of the FPE_... macros. It will be
795 * sent as the second argument to old BSD-style signal handlers and as
796 * "siginfo_t->si_code" (second argument) to SA_SIGINFO signal handlers.
797 *
798 * XXX the FP state is not preserved across signal handlers.  So signal
799 * handlers cannot afford to do FP unless they preserve the state or
800 * longjmp() out.  Both preserving the state and longjmp()ing may be
801 * destroyed by IRQ13 bugs.  Clearing FP exceptions is not an acceptable
802 * solution for signals other than SIGFPE.
803 */
804int
805npxtrap()
806{
807	critical_t savecrit;
808	u_short control, status;
809	u_long *exstat;
810
811	if (!npx_exists) {
812		printf("npxtrap: npxproc = %p, curproc = %p, npx_exists = %d\n",
813		       PCPU_GET(npxproc), curproc, npx_exists);
814		panic("npxtrap from nowhere");
815	}
816	savecrit = critical_enter();
817
818	/*
819	 * Interrupt handling (for another interrupt) may have pushed the
820	 * state to memory.  Fetch the relevant parts of the state from
821	 * wherever they are.
822	 */
823	if (PCPU_GET(npxproc) != curproc) {
824		control = GET_FPU_CW(curproc);
825		status = GET_FPU_SW(curproc);
826	} else {
827		fnstcw(&control);
828		fnstsw(&status);
829	}
830
831	exstat = GET_FPU_EXSW_PTR(&curproc->p_addr->u_pcb);
832	*exstat = status;
833	if (PCPU_GET(npxproc) != curproc)
834		GET_FPU_SW(curproc) &= ~0x80bf;
835	else
836		fnclex();
837	critical_exit(savecrit);
838	return (fpetable[status & ((~control & 0x3f) | 0x40)]);
839}
840
841/*
842 * Implement device not available (DNA) exception
843 *
844 * It would be better to switch FP context here (if curproc != npxproc)
845 * and not necessarily for every context switch, but it is too hard to
846 * access foreign pcb's.
847 */
848int
849npxdna()
850{
851	u_long *exstat;
852	critical_t s;
853
854	if (!npx_exists)
855		return (0);
856	if (PCPU_GET(npxproc) != NULL) {
857		printf("npxdna: npxproc = %p, curproc = %p\n",
858		       PCPU_GET(npxproc), curproc);
859		panic("npxdna");
860	}
861	s = critical_enter();
862	stop_emulating();
863	/*
864	 * Record new context early in case frstor causes an IRQ13.
865	 */
866	PCPU_SET(npxproc, CURPROC);
867
868	exstat = GET_FPU_EXSW_PTR(PCPU_GET(curpcb));
869	*exstat = 0;
870	/*
871	 * The following frstor may cause an IRQ13 when the state being
872	 * restored has a pending error.  The error will appear to have been
873	 * triggered by the current (npx) user instruction even when that
874	 * instruction is a no-wait instruction that should not trigger an
875	 * error (e.g., fnclex).  On at least one 486 system all of the
876	 * no-wait instructions are broken the same as frstor, so our
877	 * treatment does not amplify the breakage.  On at least one
878	 * 386/Cyrix 387 system, fnclex works correctly while frstor and
879	 * fnsave are broken, so our treatment breaks fnclex if it is the
880	 * first FPU instruction after a context switch.
881	 */
882	fpurstor(&PCPU_GET(curpcb)->pcb_save);
883	critical_exit(s);
884
885	return (1);
886}
887
888/*
889 * Wrapper for fnsave instruction, partly to handle hardware bugs.  When npx
890 * exceptions are reported via IRQ13, spurious IRQ13's may be triggered by
891 * no-wait npx instructions.  See the Intel application note AP-578 for
892 * details.  This doesn't cause any additional complications here.  IRQ13's
893 * are inherently asynchronous unless the CPU is frozen to deliver them --
894 * one that started in userland may be delivered many instructions later,
895 * after the process has entered the kernel.  It may even be delivered after
896 * the fnsave here completes.  A spurious IRQ13 for the fnsave is handled in
897 * the same way as a very-late-arriving non-spurious IRQ13 from user mode:
898 * it is normally ignored at first because we set npxproc to NULL; it is
899 * normally retriggered in npxdna() after return to user mode.
900 *
901 * npxsave() must be called with interrupts disabled, so that it clears
902 * npxproc atomically with saving the state.  We require callers to do the
903 * disabling, since most callers need to disable interrupts anyway to call
904 * npxsave() atomically with checking npxproc.
905 *
906 * A previous version of npxsave() went to great lengths to excecute fnsave
907 * with interrupts enabled in case executing it froze the CPU.  This case
908 * can't happen, at least for Intel CPU/NPX's.  Spurious IRQ13's don't imply
909 * spurious freezes.
910 */
911void
912npxsave(addr)
913	union savefpu *addr;
914{
915
916	stop_emulating();
917	fpusave(addr);
918
919	start_emulating();
920	PCPU_SET(npxproc, NULL);
921}
922
923static void
924fpusave(addr)
925	union savefpu *addr;
926{
927
928#ifdef CPU_ENABLE_SSE
929	if (cpu_fxsr)
930		fxsave(addr);
931	else
932#endif
933		fnsave(addr);
934}
935
936static void
937fpurstor(addr)
938	union savefpu *addr;
939{
940
941#ifdef CPU_ENABLE_SSE
942	if (cpu_fxsr)
943		fxrstor(addr);
944	else
945#endif
946		frstor(addr);
947}
948
949#ifdef I586_CPU_XXX
950static long
951timezero(funcname, func)
952	const char *funcname;
953	void (*func) __P((void *buf, size_t len));
954
955{
956	void *buf;
957#define	BUFSIZE		1048576
958	long usec;
959	struct timeval finish, start;
960
961	buf = malloc(BUFSIZE, M_TEMP, M_NOWAIT);
962	if (buf == NULL)
963		return (BUFSIZE);
964	microtime(&start);
965	(*func)(buf, BUFSIZE);
966	microtime(&finish);
967	usec = 1000000 * (finish.tv_sec - start.tv_sec) +
968	    finish.tv_usec - start.tv_usec;
969	if (usec <= 0)
970		usec = 1;
971	if (bootverbose)
972		printf("%s bandwidth = %u kBps\n", funcname,
973		    (u_int32_t)(((BUFSIZE >> 10) * 1000000) / usec));
974	free(buf, M_TEMP);
975	return (usec);
976}
977#endif /* I586_CPU */
978
979static device_method_t npx_methods[] = {
980	/* Device interface */
981	DEVMETHOD(device_identify,	npx_identify),
982	DEVMETHOD(device_probe,		npx_probe),
983	DEVMETHOD(device_attach,	npx_attach),
984	DEVMETHOD(device_detach,	bus_generic_detach),
985	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
986	DEVMETHOD(device_suspend,	bus_generic_suspend),
987	DEVMETHOD(device_resume,	bus_generic_resume),
988
989	{ 0, 0 }
990};
991
992static driver_t npx_driver = {
993	"npx",
994	npx_methods,
995	1,			/* no softc */
996};
997
998static devclass_t npx_devclass;
999
1000/*
1001 * We prefer to attach to the root nexus so that the usual case (exception 16)
1002 * doesn't describe the processor as being `on isa'.
1003 */
1004DRIVER_MODULE(npx, nexus, npx_driver, npx_devclass, 0, 0);
1005
1006/*
1007 * This sucks up the legacy ISA support assignments from PNPBIOS/ACPI.
1008 */
1009static struct isa_pnp_id npxisa_ids[] = {
1010	{ 0x040cd041, "Legacy ISA coprocessor support" }, /* PNP0C04 */
1011	{ 0 }
1012};
1013
1014static int
1015npxisa_probe(device_t dev)
1016{
1017	int result;
1018	if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, npxisa_ids)) <= 0) {
1019		device_quiet(dev);
1020	}
1021	return(result);
1022}
1023
1024static int
1025npxisa_attach(device_t dev)
1026{
1027	return (0);
1028}
1029
1030static device_method_t npxisa_methods[] = {
1031	/* Device interface */
1032	DEVMETHOD(device_probe,		npxisa_probe),
1033	DEVMETHOD(device_attach,	npxisa_attach),
1034	DEVMETHOD(device_detach,	bus_generic_detach),
1035	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1036	DEVMETHOD(device_suspend,	bus_generic_suspend),
1037	DEVMETHOD(device_resume,	bus_generic_resume),
1038
1039	{ 0, 0 }
1040};
1041
1042static driver_t npxisa_driver = {
1043	"npxisa",
1044	npxisa_methods,
1045	1,			/* no softc */
1046};
1047
1048static devclass_t npxisa_devclass;
1049
1050DRIVER_MODULE(npxisa, isa, npxisa_driver, npxisa_devclass, 0, 0);
1051DRIVER_MODULE(npxisa, acpi, npxisa_driver, npxisa_devclass, 0, 0);
1052
1053