aboutsummaryrefslogtreecommitdiffstats
path: root/hw/csu_f3c.py
blob: 907174326e9ce56f8c392bea9375b63d31b33370 (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
#!/usr/bin/env python3
"""CSU cmd 6: is the stall driven by the value at shared-buffer offset 0x3c?

Per-field fuzzing found CSU cmd 6 stalls when buf[0x3c] = 0xFFFFFFFF, while CSU was clean
under uniform profiles. If smaller values return normally and only large ones stall, the host
controls the stall -- which is a different and more interesting class than the AUC/FP wedge
(which stalls with a zeroed buffer).

Safe values first, since a stall costs an ATX cycle.
"""
import glob, struct, sys, time
from tinygrad import Device
import tinygrad.runtime.autogen.am.am as am
adev=Device["AMD"].iface.dev_impl; psp=adev.psp
def alloc(n):
    pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa)
def load(blob):
    ap_,amc=alloc(0x80000); cp,cmc=alloc(0x8000)
    n=(len(blob)+3)&~3
    adev.vram.view(ap_,n)[:]=blob+b'\x00'*(n-len(blob)); 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(blob)
    t.cmd_buf_phy_addr_lo,t.cmd_buf_phy_addr_hi=cmc&0xFFFFFFFF,cmc>>32; t.cmd_buf_len=0x4000
    try:
        r=psp._ring_submit(c); return r.resp.status&0xFFFFFFFF,r.resp.session_id,cp
    except Exception as e:
        try: return int(str(e).strip().split()[-1])&0xFFFFFFFF,None,cp
        except Exception: return -1,None,cp
def inv(sid,shm,cid,val):
    buf=bytearray(0x4000); struct.pack_into('<I',buf,0,cid); struct.pack_into('<I',buf,0x3c,val)
    adev.vram.view(shm,0x4000)[:]=bytes(buf); adev.gmc.flush_hdp()
    c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD)
    iv=c.cmd.cmd_invoke_cmd; iv.session_id,iv.ta_cmd_id=sid,cid
    t0=time.time()
    try:
        r=psp._ring_submit(c); st=r.resp.status&0xFFFFFFFF
    except Exception as e:
        try: st=int(str(e).strip().split()[-1])&0xFFFFFFFF
        except Exception: st=-1
    return st,time.time()-t0
blob=open(glob.glob('/root/psp/contall/ta_42-0-0-6_1c874c3c*')[0],'rb').read()
st,sid,shm=load(blob); print('CSU load %#x sid=%s'%(st,sid))
for v in (0x0, 0x1, 0x10, 0x1000, 0x7FFFFFFF, 0x80000000, 0xFFFFFFFF):
    s,dt=inv(sid,shm,6,v)
    print('  buf[0x3c]=%#-12x -> status %#-12x %5.1fs%s'%(v,s,dt,'   <<< STALL' if dt>5 else ''))
    if dt>5: break