#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: altivec.c,v 1.35 2024/06/15 19:48:13 mrg Exp $");
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/atomic.h>
#include <uvm/uvm_extern.h>
#include <powerpc/pcb.h>
#include <powerpc/altivec.h>
#include <powerpc/spr.h>
#include <powerpc/oea/spr.h>
#include <powerpc/psl.h>
static void vec_state_load(lwp_t *, u_int);
static void vec_state_save(lwp_t *);
static void vec_state_release(lwp_t *);
const pcu_ops_t vec_ops = {
.pcu_id = PCU_VEC,
.pcu_state_load = vec_state_load,
.pcu_state_save = vec_state_save,
.pcu_state_release = vec_state_release,
};
bool
vec_used_p(lwp_t *l)
{
return pcu_valid_p(&vec_ops, l);
}
void
vec_mark_used(lwp_t *l)
{
return pcu_discard(&vec_ops, l, true);
}
void
vec_state_load(lwp_t *l, u_int flags)
{
struct pcb * const pcb = lwp_getpcb(l);
if ((flags & PCU_VALID) == 0) {
memset(&pcb->pcb_vr, 0, sizeof(pcb->pcb_vr));
vec_mark_used(l);
}
if ((flags & PCU_REENABLE) == 0) {
const register_t msr = mfmsr();
mtmsr((msr & ~PSL_EE) | PSL_VEC);
__asm volatile ("isync");
vec_load_from_vreg(&pcb->pcb_vr);
mtmsr(msr);
__asm volatile ("isync");
}
l->l_md.md_utf->tf_vrsave = pcb->pcb_vr.vrsave;
l->l_md.md_flags |= PSL_VEC;
l->l_md.md_utf->tf_srr1 |= PSL_VEC;
}
void
vec_state_save(lwp_t *l)
{
struct pcb * const pcb = lwp_getpcb(l);
const register_t msr = mfmsr();
mtmsr((msr & ~PSL_EE) | PSL_VEC);
__asm volatile ("isync");
vec_unload_to_vreg(&pcb->pcb_vr);
pcb->pcb_vr.vrsave = l->l_md.md_utf->tf_vrsave;
__asm volatile ("dssall; sync");
mtmsr(msr);
__asm volatile ("isync");
}
void
vec_state_release(lwp_t *l)
{
__asm volatile("dssall;sync");
l->l_md.md_utf->tf_srr1 &= ~PSL_VEC;
l->l_md.md_flags &= ~PSL_VEC;
}
void
vec_restore_from_mcontext(struct lwp *l, const mcontext_t *mcp)
{
struct pcb * const pcb = lwp_getpcb(l);
KASSERT(l == curlwp);
if (!vec_used_p(l))
return;
pcu_discard(&vec_ops, l, true);
if (mcp != NULL) {
memcpy(pcb->pcb_vr.vreg, &mcp->__vrf.__vrs,
sizeof (pcb->pcb_vr.vreg));
pcb->pcb_vr.vscr = mcp->__vrf.__vscr;
pcb->pcb_vr.vrsave = mcp->__vrf.__vrsave;
l->l_md.md_utf->tf_vrsave = pcb->pcb_vr.vrsave;
}
}
bool
vec_save_to_mcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flagp)
{
struct pcb * const pcb = lwp_getpcb(l);
KASSERT(l == curlwp);
if (!vec_used_p(l))
return false;
pcu_save(&vec_ops, l);
if (mcp != NULL) {
mcp->__gregs[_REG_MSR] |= PSL_VEC;
mcp->__vrf.__vscr = pcb->pcb_vr.vscr;
mcp->__vrf.__vrsave = l->l_md.md_utf->tf_vrsave;
memcpy(mcp->__vrf.__vrs, pcb->pcb_vr.vreg,
sizeof (mcp->__vrf.__vrs));
*flagp |= _UC_POWERPC_VEC;
}
return true;
}
#define ZERO_VEC 19
void
vzeropage(paddr_t pa)
{
const paddr_t ea = pa + PAGE_SIZE;
uint32_t vec[7], *vp = (void *) roundup((uintptr_t) vec, 16);
register_t omsr, msr;
__asm volatile("mfmsr %0" : "=r"(omsr) :);
msr = (omsr & ~PSL_EE) | PSL_VEC;
__asm volatile("sync; mtmsr %0; isync" :: "r"(msr));
__asm("stvx %1,0,%0" :: "r"(vp), "n"(ZERO_VEC));
__asm("vxor %0,%0,%0" :: "n"(ZERO_VEC));
__asm volatile(
" sync ;"
" mfmsr %[msr];"
" rlwinm %[msr],%[msr],0,28,26;"
" mtmsr %[msr];"
" isync;"
"1: stvx %[zv], %[pa], %[off0];"
" stvxl %[zv], %[pa], %[off16];"
" stvx %[zv], %[pa], %[off32];"
" stvxl %[zv], %[pa], %[off48];"
" addi %[pa], %[pa], 64;"
" cmplw %[pa], %[ea];"
" blt+ 1b;"
" ori %[msr], %[msr], 0x10;"
" sync;"
" mtmsr %[msr];"
" isync;"
:: [msr] "r"(msr), [pa] "b"(pa), [ea] "b"(ea),
[off0] "r"(0), [off16] "r"(16), [off32] "r"(32), [off48] "r"(48),
[zv] "n"(ZERO_VEC));
__asm("lvx %1,0,%0" :: "r"(vp), "n"(ZERO_VEC));
__asm volatile("sync; mtmsr %0; isync" :: "r"(omsr));
}
#define LO_VEC 16
#define HI_VEC 17
void
vcopypage(paddr_t dst, paddr_t src)
{
const paddr_t edst = dst + PAGE_SIZE;
uint32_t vec[11], *vp = (void *) roundup((uintptr_t) vec, 16);
register_t omsr, msr;
__asm volatile("mfmsr %0" : "=r"(omsr) :);
msr = (omsr & ~PSL_EE) | PSL_VEC;
__asm volatile("sync; mtmsr %0; isync" :: "r"(msr));
__asm("stvx %2,%1,%0" :: "b"(vp), "r"( 0), "n"(LO_VEC));
__asm("stvx %2,%1,%0" :: "b"(vp), "r"(16), "n"(HI_VEC));
__asm volatile(
" sync ;"
" mfmsr %[msr];"
" rlwinm %[msr],%[msr],0,28,26;"
" mtmsr %[msr];"
" isync;"
"1: lvx %[lv], %[src], %[off0];"
" stvx %[lv], %[dst], %[off0];"
" lvxl %[hv], %[src], %[off16];"
" stvxl %[hv], %[dst], %[off16];"
" addi %[src], %[src], 32;"
" addi %[dst], %[dst], 32;"
" cmplw %[dst], %[edst];"
" blt+ 1b;"
" ori %[msr], %[msr], 0x10;"
" sync;"
" mtmsr %[msr];"
" isync;"
:: [msr] "r"(msr), [src] "b"(src), [dst] "b"(dst),
[edst] "b"(edst), [off0] "r"(0), [off16] "r"(16),
[lv] "n"(LO_VEC), [hv] "n"(HI_VEC));
__asm("lvx %2,%1,%0" :: "b"(vp), "r"( 0), "n"(LO_VEC));
__asm("lvx %2,%1,%0" :: "b"(vp), "r"(16), "n"(HI_VEC));
__asm volatile("sync; mtmsr %0; isync" :: "r"(omsr));
}