#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/limits.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <machine/armreg.h>
#include <machine/elf.h>
#include <machine/frame.h>
#include <machine/md_var.h>
#include <machine/pcb.h>
#include <machine/undefined.h>
#include <machine/vfp.h>
static int vfp_bounce(u_int, u_int, struct trapframe *, int);
static void vfp_restore(struct vfp_state *);
extern int vfp_exists;
static struct undefined_handler vfp10_uh, vfp11_uh;
static int is_d32;
static MALLOC_DEFINE(M_FPUKERN_CTX, "fpukern_ctx",
"Kernel contexts for VFP state");
struct fpu_kern_ctx {
struct vfp_state *prev;
#define FPU_KERN_CTX_DUMMY 0x01
#define FPU_KERN_CTX_INUSE 0x02
uint32_t flags;
struct vfp_state state;
};
#define fmxr(reg, val) \
__asm __volatile(" .fpu vfpv2\n .fpu vfpv3\n" \
" vmsr " __STRING(reg) ", %0" :: "r"(val));
#define fmrx(reg) \
({ u_int val = 0;\
__asm __volatile(" .fpu vfpv2\n .fpu vfpv3\n" \
" vmrs %0, " __STRING(reg) : "=r"(val)); \
val; \
})
static u_int
get_coprocessorACR(void)
{
u_int val;
__asm __volatile("mrc p15, 0, %0, c1, c0, 2" : "=r" (val) : : "cc");
return val;
}
static void
set_coprocessorACR(u_int val)
{
__asm __volatile("mcr p15, 0, %0, c1, c0, 2\n\t"
: : "r" (val) : "cc");
isb();
}
static void
vfp_enable(void)
{
uint32_t fpexc;
fpexc = fmrx(fpexc);
fmxr(fpexc, fpexc | VFPEXC_EN);
isb();
}
static void
vfp_disable(void)
{
uint32_t fpexc;
fpexc = fmrx(fpexc);
fmxr(fpexc, fpexc & ~VFPEXC_EN);
isb();
}
void
vfp_init(void)
{
u_int fpsid, tmp;
u_int coproc, vfp_arch;
coproc = get_coprocessorACR();
coproc |= COPROC10 | COPROC11;
set_coprocessorACR(coproc);
fpsid = fmrx(fpsid);
if (!(fpsid & VFPSID_HARDSOFT_IMP)) {
vfp_exists = 1;
is_d32 = 0;
PCPU_SET(vfpsid, fpsid);
elf_hwcap |= HWCAP_VFP;
vfp_arch =
(fpsid & VFPSID_SUBVERSION2_MASK) >> VFPSID_SUBVERSION_OFF;
if (vfp_arch >= VFP_ARCH3) {
tmp = fmrx(mvfr0);
PCPU_SET(vfpmvfr0, tmp);
elf_hwcap |= HWCAP_VFPv3;
if ((tmp & VMVFR0_RB_MASK) == 2) {
elf_hwcap |= HWCAP_VFPD32;
is_d32 = 1;
} else
elf_hwcap |= HWCAP_VFPv3D16;
tmp = fmrx(mvfr1);
PCPU_SET(vfpmvfr1, tmp);
if (PCPU_GET(cpuid) == 0) {
if ((tmp & VMVFR1_FZ_MASK) == 0x1) {
initial_fpscr &= ~VFPSCR_FZ;
thread0.td_pcb->pcb_vfpstate.fpscr =
initial_fpscr;
}
}
if ((tmp & VMVFR1_LS_MASK) >> VMVFR1_LS_OFF == 1 &&
(tmp & VMVFR1_I_MASK) >> VMVFR1_I_OFF == 1 &&
(tmp & VMVFR1_SP_MASK) >> VMVFR1_SP_OFF == 1)
elf_hwcap |= HWCAP_NEON;
if ((tmp & VMVFR1_FMAC_MASK) >> VMVFR1_FMAC_OFF == 1)
elf_hwcap |= HWCAP_VFPv4;
}
vfp_disable();
if (vfp10_uh.uh_handler == NULL) {
vfp10_uh.uh_handler = vfp_bounce;
vfp11_uh.uh_handler = vfp_bounce;
install_coproc_handler_static(10, &vfp10_uh);
install_coproc_handler_static(11, &vfp11_uh);
}
}
}
SYSINIT(vfp, SI_SUB_CPU, SI_ORDER_ANY, vfp_init, NULL);
static int
vfp_bounce(u_int addr, u_int insn, struct trapframe *frame, int code)
{
u_int cpu, fpexc;
struct pcb *curpcb;
ksiginfo_t ksi;
critical_enter();
fpexc = fmrx(fpexc);
if (fpexc & VFPEXC_EN) {
fmxr(fpexc, fpexc & ~(VFPEXC_EX | VFPEXC_FP2V));
critical_exit();
if (fpexc & VFPEXC_EX) {
ksiginfo_init_trap(&ksi);
ksi.ksi_signo = SIGFPE;
if (fpexc & VFPEXC_UFC)
ksi.ksi_code = FPE_FLTUND;
else if (fpexc & VFPEXC_OFC)
ksi.ksi_code = FPE_FLTOVF;
else if (fpexc & VFPEXC_IOC)
ksi.ksi_code = FPE_FLTINV;
ksi.ksi_addr = (void *)addr;
trapsignal(curthread, &ksi);
return 0;
}
return 1;
}
curpcb = curthread->td_pcb;
if ((code & FAULT_USER) == 0 &&
(curpcb->pcb_fpflags & PCB_FP_KERN) == 0) {
critical_exit();
return (1);
}
fmxr(fpexc, fpexc | VFPEXC_EN);
cpu = PCPU_GET(cpuid);
if (curpcb->pcb_vfpcpu != cpu || curthread != PCPU_GET(fpcurthread)) {
vfp_restore(curpcb->pcb_vfpsaved);
curpcb->pcb_vfpcpu = cpu;
PCPU_SET(fpcurthread, curthread);
}
critical_exit();
KASSERT((code & FAULT_USER) == 0 ||
curpcb->pcb_vfpsaved == &curpcb->pcb_vfpstate,
("Kernel VFP state in use when entering userspace"));
return (0);
}
void
vfp_new_thread(struct thread *newtd, struct thread *oldtd, bool fork)
{
struct pcb *newpcb;
newpcb = newtd->td_pcb;
if ((oldtd->td_pflags & TDP_KTHREAD) != 0) {
newpcb->pcb_fpflags &=
~(PCB_FP_STARTED | PCB_FP_KERN | PCB_FP_NOSAVE);
} else {
MPASS((newpcb->pcb_fpflags & (PCB_FP_KERN|PCB_FP_NOSAVE)) == 0);
if (!fork) {
newpcb->pcb_fpflags &= ~PCB_FP_STARTED;
}
}
newpcb->pcb_vfpsaved = &newpcb->pcb_vfpstate;
newpcb->pcb_vfpcpu = UINT_MAX;
}
static void
vfp_restore(struct vfp_state *vfpsave)
{
uint32_t fpexc;
fpexc = vfpsave->fpexec;
if (fpexc & VFPEXC_EX) {
fmxr(fpinst, vfpsave->fpinst);
if (fpexc & VFPEXC_FP2V)
fmxr(fpinst2, vfpsave->fpinst2);
}
fmxr(fpscr, vfpsave->fpscr);
__asm __volatile(
" .fpu vfpv2\n"
" .fpu vfpv3\n"
" vldmia %0!, {d0-d15}\n"
" cmp %1, #0\n"
" vldmiane %0!, {d16-d31}\n"
" addeq %0, %0, #128\n"
: "+&r" (vfpsave) : "r" (is_d32) : "cc"
);
fmxr(fpexc, fpexc);
}
void
vfp_store(struct vfp_state *vfpsave, boolean_t disable_vfp)
{
uint32_t fpexc;
fpexc = fmrx(fpexc);
if (fpexc & VFPEXC_EN) {
vfpsave->fpexec = fpexc;
vfpsave->fpscr = fmrx(fpscr);
if (fpexc & VFPEXC_EX) {
vfpsave->fpinst = fmrx(fpinst);
if (fpexc & VFPEXC_FP2V)
vfpsave->fpinst2 = fmrx(fpinst2);
fpexc &= ~VFPEXC_EX;
}
__asm __volatile(
" .fpu vfpv2\n"
" .fpu vfpv3\n"
" vstmia %0!, {d0-d15}\n"
" cmp %1, #0\n"
" vstmiane %0!, {d16-d31}\n"
" addeq %0, %0, #128\n"
: "+&r" (vfpsave) : "r" (is_d32) : "cc"
);
if (disable_vfp)
fmxr(fpexc , fpexc & ~VFPEXC_EN);
}
}
void
vfp_discard(struct thread *td)
{
u_int tmp;
if (PCPU_GET(fpcurthread) == td)
PCPU_SET(fpcurthread, NULL);
tmp = fmrx(fpexc);
if (tmp & VFPEXC_EN)
fmxr(fpexc, tmp & ~VFPEXC_EN);
}
void
vfp_save_state(struct thread *td, struct pcb *pcb)
{
int32_t fpexc;
KASSERT(pcb != NULL, ("NULL vfp pcb"));
KASSERT(td == NULL || td->td_pcb == pcb, ("Invalid vfp pcb"));
if (pcb->pcb_vfpsaved == NULL)
pcb->pcb_vfpsaved = &pcb->pcb_vfpstate;
if (td == NULL)
td = curthread;
critical_enter();
fpexc = fmrx(fpexc);
if (fpexc & VFPEXC_EN) {
KASSERT(PCPU_GET(fpcurthread) == td,
("Storing an invalid VFP state"));
vfp_store(pcb->pcb_vfpsaved, true);
}
critical_exit();
}
struct fpu_kern_ctx *
fpu_kern_alloc_ctx(u_int flags)
{
return (malloc(sizeof(struct fpu_kern_ctx), M_FPUKERN_CTX,
((flags & FPU_KERN_NOWAIT) ? M_NOWAIT : M_WAITOK) | M_ZERO));
}
void
fpu_kern_free_ctx(struct fpu_kern_ctx *ctx)
{
KASSERT((ctx->flags & FPU_KERN_CTX_INUSE) == 0, ("freeing in-use ctx"));
free(ctx, M_FPUKERN_CTX);
}
void
fpu_kern_enter(struct thread *td, struct fpu_kern_ctx *ctx, u_int flags)
{
struct pcb *pcb;
pcb = td->td_pcb;
KASSERT((flags & FPU_KERN_NOCTX) != 0 || ctx != NULL,
("ctx is required when !FPU_KERN_NOCTX"));
KASSERT(ctx == NULL || (ctx->flags & FPU_KERN_CTX_INUSE) == 0,
("using inuse ctx"));
KASSERT((pcb->pcb_fpflags & PCB_FP_NOSAVE) == 0,
("recursive fpu_kern_enter while in PCB_FP_NOSAVE state"));
if ((flags & FPU_KERN_NOCTX) != 0) {
critical_enter();
if (curthread == PCPU_GET(fpcurthread)) {
vfp_save_state(curthread, pcb);
}
PCPU_SET(fpcurthread, NULL);
vfp_enable();
pcb->pcb_fpflags |= PCB_FP_KERN | PCB_FP_NOSAVE |
PCB_FP_STARTED;
return;
}
if ((flags & FPU_KERN_KTHR) != 0 && is_fpu_kern_thread(0)) {
ctx->flags = FPU_KERN_CTX_DUMMY | FPU_KERN_CTX_INUSE;
return;
}
KASSERT((pcb->pcb_fpflags & PCB_FP_KERN) != 0 ||
pcb->pcb_vfpsaved == &pcb->pcb_vfpstate,
("Mangled pcb_vfpsaved %x %p %p", pcb->pcb_fpflags, pcb->pcb_vfpsaved,
&pcb->pcb_vfpstate));
ctx->flags = FPU_KERN_CTX_INUSE;
vfp_save_state(curthread, pcb);
ctx->prev = pcb->pcb_vfpsaved;
pcb->pcb_vfpsaved = &ctx->state;
pcb->pcb_fpflags |= PCB_FP_KERN;
pcb->pcb_fpflags &= ~PCB_FP_STARTED;
return;
}
int
fpu_kern_leave(struct thread *td, struct fpu_kern_ctx *ctx)
{
struct pcb *pcb;
pcb = td->td_pcb;
if ((pcb->pcb_fpflags & PCB_FP_NOSAVE) != 0) {
KASSERT(ctx == NULL, ("non-null ctx after FPU_KERN_NOCTX"));
KASSERT(PCPU_GET(fpcurthread) == NULL,
("non-NULL fpcurthread for PCB_FP_NOSAVE"));
CRITICAL_ASSERT(td);
vfp_disable();
pcb->pcb_fpflags &= ~(PCB_FP_NOSAVE | PCB_FP_STARTED);
critical_exit();
} else {
KASSERT((ctx->flags & FPU_KERN_CTX_INUSE) != 0,
("FPU context not inuse"));
ctx->flags &= ~FPU_KERN_CTX_INUSE;
if (is_fpu_kern_thread(0) &&
(ctx->flags & FPU_KERN_CTX_DUMMY) != 0)
return (0);
KASSERT((ctx->flags & FPU_KERN_CTX_DUMMY) == 0, ("dummy ctx"));
critical_enter();
vfp_discard(td);
critical_exit();
pcb->pcb_fpflags &= ~PCB_FP_STARTED;
pcb->pcb_vfpsaved = ctx->prev;
}
if (pcb->pcb_vfpsaved == &pcb->pcb_vfpstate) {
pcb->pcb_fpflags &= ~PCB_FP_KERN;
} else {
KASSERT((pcb->pcb_fpflags & PCB_FP_KERN) != 0,
("unpaired fpu_kern_leave"));
}
return (0);
}
int
fpu_kern_thread(u_int flags __unused)
{
struct pcb *pcb = curthread->td_pcb;
KASSERT((curthread->td_pflags & TDP_KTHREAD) != 0,
("Only kthread may use fpu_kern_thread"));
KASSERT(pcb->pcb_vfpsaved == &pcb->pcb_vfpstate,
("Mangled pcb_vfpsaved"));
KASSERT((pcb->pcb_fpflags & PCB_FP_KERN) == 0,
("Thread already setup for the VFP"));
pcb->pcb_fpflags |= PCB_FP_KERN;
return (0);
}
int
is_fpu_kern_thread(u_int flags __unused)
{
struct pcb *curpcb;
if ((curthread->td_pflags & TDP_KTHREAD) == 0)
return (0);
curpcb = curthread->td_pcb;
return ((curpcb->pcb_fpflags & PCB_FP_KERN) != 0);
}