aboutsummaryrefslogtreecommitdiffstats
path: root/hw/vmid_fault.py
blob: e6dee0f9f6c9eb4c2af8981dba5cf48369a20d0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
"""Characterise the vmid!=0 failure: read the L2 protection-fault status registers.

Context 1 is now known to be programmed identically to context 0 (verified by read-back), yet
vmid=1 still fails. So "the context is unconfigured" is not the explanation. The fault status
register encodes WHY: range vs PTE-valid vs permission, plus client and read/write.

  1  configure MM VM context 1 identically to context 0
  2  clear the fault status
  3  one LOAD_TA with vmid=1
  4  read fault status + address
Control: the same with vmid=0, which must leave no fault.
"""
import ctypes, glob, struct, sys, time
sys.path.insert(0,'/root/psp/hw')
from tinygrad import Device
import tinygrad.runtime.autogen.am.am as am
adev=Device["AMD"].iface.dev_impl; psp=adev.psp; g=adev.gmc
RESP=864; FRAME=ctypes.sizeof(am.struct_psp_gfx_rb_frame); step=FRAME//4
def alloc(n):
    pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa)
blob=open(glob.glob('/root/psp/contall/ta_42-0-0-6_1c874c3c*')[0],'rb').read()
ap_,amc=alloc(0x80000); cp,cmc=alloc(0x8000); scratch,smc=alloc(0x1000)
n=(len(blob)+3)&~3
adev.vram.view(ap_,n)[:]=blob+b'\x00'*(n-len(blob)); adev.gmc.flush_hdp()
for inst in g.mm_insts: g.enable_vm_addressing(adev.mm.root_page_table,"MM",vmid=1,inst=inst)
g.flush_tlb(ip="MM", vmid=1)
print('context 1 programmed identically to context 0', flush=True)
def fault(tag):
    for nm in ('STATUS','ADDR_LO32','ADDR_HI32','DEFAULT_ADDR_LO32'):
        try:
            v=adev.reg(f"regMMVM_L2_PROTECTION_FAULT_{nm}").read()
            print('    %-18s = %#x'%(nm,v), flush=True)
        except Exception as e: print('    %-18s = n/a'%nm, flush=True)
def go(vmid, tag):
    try: adev.reg("regMMVM_L2_PROTECTION_FAULT_STATUS").write(0)
    except Exception: pass
    c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_LOAD_TA); t=c.cmd.cmd_load_ta
    t.app_phy_addr_lo,t.app_phy_addr_hi=amc&0xFFFFFFFF,amc>>32; t.app_len=len(blob)
    t.cmd_buf_phy_addr_lo,t.cmd_buf_phy_addr_hi=cmc&0xFFFFFFFF,cmc>>32; t.cmd_buf_len=0x4000
    adev.vram.view(psp.cmd_paddr, ctypes.sizeof(c))[:]=memoryview(c).cast('B')
    adev.vram.view(psp.cmd_paddr+RESP,4,'I')[0]=0xA5A5A5A5; adev.gmc.flush_hdp()
    f=am.struct_psp_gfx_rb_frame(fence_value=0x1234,
        cmd_buf_addr_lo=adev.paddr2mc(psp.cmd_paddr)&0xFFFFFFFF,
        cmd_buf_addr_hi=adev.paddr2mc(psp.cmd_paddr)>>32,
        fence_addr_lo=smc&0xFFFFFFFF, fence_addr_hi=smc>>32)
    raw=bytearray(memoryview(f).cast('B')); raw[32]=vmid
    wmax=psp.ring_size//4; prev=adev.reg(f"{psp.reg_pref}_67").read()%wmax
    adev.vram.view(psp.ring_paddr+prev*4, FRAME)[:]=bytes(raw)
    adev.reg(f"{psp.reg_pref}_67").write((prev+step)%wmax)
    t0=time.time(); st=None
    while time.time()-t0<1.5:
        adev.gmc.flush_hdp()
        v=struct.unpack_from('<I',bytes(adev.vram.view(psp.cmd_paddr+RESP,4)[:]),0)[0]
        if v!=0xA5A5A5A5: st=v; break
    print('  %-14s vmid=%-3d resp=%s'%(tag,vmid,hex(st) if st is not None else 'TIMEOUT'), flush=True)
    fault(tag)
go(0,'control')
go(1,'vmid=1')