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
|
"""Get RAS actually invocable. Commands live in frame[0], values 0-4:
0 ENABLE FEATURES 1 DISABLE FEATURES 2 TRIGGER ERROR
3 QUERY_BLOCK_INFO 4 QUERY_SUB_BLOCK_INFO
Gate: byte_4060E0 must be non-zero (set at OpenSession when asic==49), else 40974.
cmd 2 (TRIGGER ERROR) is deliberately skipped here -- injecting real RAS errors
could destabilise the card; probe the read-only queries first.
"""
import ctypes,struct,sys
from tinygrad import Device
from tinygrad.runtime.autogen.am import am
from tinygrad.helpers import lo32,hi32
d=Device["AMD"]; adev=d.iface.dev_impl; psp=adev.psp
SZ=ctypes.sizeof(am.struct_psp_gfx_cmd_resp); P='/Users/user/dev/amd-psp-re/psp_out/'; S=0x8000; MARK=0x5A
def stat(): return int.from_bytes(bytes(adev.vram.view(psp.cmd_paddr,SZ)[:])[864:868],"little")
def alloc(n):
pa=adev.mm.palloc(n,align=0x1000,zero=True); return pa,adev.paddr2mc(pa)
def load(cmd,blob):
ap,amc=alloc(0x40000); cp,cmc=alloc(S)
adev.vram.view(ap,len(blob))[:]=blob; adev.gmc.flush_hdp()
adev.vram.view(psp.cmd_paddr,SZ)[:]=b"\x00"*SZ
c=am.struct_psp_gfx_cmd_resp(cmd_id=cmd); t=c.cmd.cmd_load_ta
t.app_phy_addr_hi,t.app_phy_addr_lo=hi32(amc),lo32(amc); t.app_len=len(blob)
t.cmd_buf_phy_addr_hi,t.cmd_buf_phy_addr_lo=hi32(cmc),lo32(cmc); t.cmd_buf_len=S
try: return psp._ring_submit(c).resp.session_id, cp, 0
except RuntimeError: return None, cp, stat()
def inv(sid,shm,buf,tc):
adev.vram.view(shm,S)[:]=bytes(buf); adev.gmc.flush_hdp()
adev.vram.view(psp.cmd_paddr,SZ)[:]=b"\x00"*SZ
ic=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD); iv=ic.cmd.cmd_invoke_cmd
iv.session_id,iv.ta_cmd_id=sid,tc
try: psp._ring_submit(ic); ps=0
except RuntimeError: ps=stat()
except TimeoutError: return "TIMEOUT",None
return ps, bytes(adev.vram.view(shm,S)[:])
ras,rsh,err=load(am.GFX_CMD_ID_LOAD_TA, open(P+'ras_full.bin','rb').read())
print("RAS loaded sid=%s err=%s"%(ras,err))
if ras is None: sys.exit(1)
def go(c, tc=None, extra=None):
b=bytearray(S)
for i in range(0x800,S): b[i]=MARK
struct.pack_into('<I',b,0,c)
if extra: extra(b)
sent=bytes(b)
ps,o=inv(ras,rsh,b,c if tc is None else tc)
if o is None: return ps,None,None,None
diff=[i for i in range(S) if sent[i]!=o[i]]
return ps,o,diff,struct.unpack_from('<I',o,4)[0]
print("\n cmd ta_cmd_id psp_status out[4] bytes_written span")
for c in (0,1,3,4):
for tc in (c,5): # RAS is "GP command 5" per the header comment
ps,o,diff,r4=go(c,tc)
if o is None: print(" %d tc=%-3s TIMEOUT"%(c,tc)); continue
print(" %d tc=%-3s %-10s %-11s %6d bytes %s"%(c,tc,hex(ps) if isinstance(ps,int) else ps,
hex(r4), len(diff), ("%#06x..%#06x"%(diff[0],diff[-1])) if diff else ""))
print("\n(40974 = 0xA00E means byte_4060E0 not initialised; 0xFFFF000B = shared-buffer map failed)")
print("sos_alive:",psp.is_sos_alive())
|