#!/usr/bin/env python3 """Is the CSU stall driven by invoke COUNT (a resource leak) or by content? Replay stalls deterministically at invoke #1008, yet the same command+field+value in isolation returns instantly. Test the simplest alternative hypothesis: plain, identical invokes with a zeroed buffer. If a stall appears at a similar count, the cause is invoke-count/resource exhaustion, not that field. """ 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): buf=bytearray(0x4000); struct.pack_into('5: print('STALL at plain invoke #%d (status %#x)'%(i,s), flush=True); sys.exit(0) if i%200==0: print(' %d invokes ok (last status %#x)'%(i,s), flush=True) print('1400 plain invokes with no stall', flush=True)