aboutsummaryrefslogtreecommitdiffstats
path: root/hw/field24.py
blob: 57ff525367dbd874f7459baa78dbbc185dbccc52 (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
#!/usr/bin/env python3
"""Ring-command dword +0x24 is the only field in the invoke structure that is validated.

A full-resolution sweep of all 196 dwords showed only +0x24 changes the status (0x2a when
all-ones). A validated field is a used field, so: which values are accepted, and does an
accepted value change what gets written?
"""
import glob, struct, sys, time
sys.path.insert(0,'/root/psp/hw')
from tinygrad import Device
import tinygrad.runtime.autogen.am.am as am
from ring_wrap import install
adev=Device["AMD"].iface.dev_impl; psp=adev.psp
submit=install(psp,adev)
PAT=0x77777777
base=open(glob.glob('/root/psp/contall/ta_16-0-0-9_9e9044cf*')[0],'rb').read()  # AUC: writes output
def alloc(n):
    pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa)
ap_,amc=alloc(0x80000); cp,cmc=alloc(0x20000)
n=(len(base)+3)&~3
adev.vram.view(ap_,n)[:]=base+b'\x00'*(n-len(base)); adev.gmc.flush_hdp()
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(base)
t.cmd_buf_phy_addr_lo,t.cmd_buf_phy_addr_hi=cmc&0xFFFFFFFF,cmc>>32; t.cmd_buf_len=0x4000
r=submit(c); sid=r.resp.session_id
print('AUC load %#x sid=%s'%(r.resp.status&0xFFFFFFFF,sid), flush=True)
def go(val):
    adev.vram.view(cp,0x20000)[:]=bytes(struct.pack('<I',PAT)*(0x20000//4))
    b=bytearray(0x4000); struct.pack_into('<I',b,0,5)
    adev.vram.view(cp,0x4000)[:]=bytes(b); adev.gmc.flush_hdp()
    c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD)
    raw=memoryview(c).cast('B'); struct.pack_into('<I',raw,0x24,val)
    iv=c.cmd.cmd_invoke_cmd; iv.session_id,iv.ta_cmd_id=sid,5
    t0=time.time()
    try: st=submit(c).resp.status&0xFFFFFFFF
    except Exception as e:
        try: st=int(str(e).strip().split()[-1])&0xFFFFFFFF
        except Exception: st=-1
    dt=time.time()-t0
    adev.gmc.flush_hdp()
    out=bytes(adev.vram.view(cp,0x20000)[:])
    ins=sum(1 for o in range(0x10,0x4000,4) if struct.unpack_from('<I',out,o)[0]!=PAT)
    bey=sum(1 for o in range(0x4000,0x20000,4) if struct.unpack_from('<I',out,o)[0]!=PAT)
    print('  +0x24=%#-12x -> %#-10x %.1fs  in-window=%-4d beyond=%d%s'%(val,st,dt,ins,bey,
          '  <<< STALL' if dt>5 else ''), flush=True)
    return dt>5
for v in (0,1,2,4,0x10,0x100,0x1000,0x4000,0x8000,0x10000,0x100000,0x7FFFFFFF,0x80000000,0xFFFFFFFF):
    if go(v): break