#include #include #include #include #include "amdgpu_aql_packets.h" #include "amdgpu_vram.h" #include "amdgpu_software_stats.h" #include "amdgpu_ip.h" #include "../upstream/linux/drivers/gpu/drm/amd/include/v12_structs.h" #include "../upstream/linux/drivers/gpu/drm/amd/include/asic_reg/gc/gc_12_0_0_sh_mask.h" #include "../upstream/rocm-systems/projects/rocr-runtime/runtime/hsa-runtime/core/inc/registers.h" using namespace amdgpu; #include "aql_mqd_offsets.inc" #include "aql_topology.inc" static_assert(sizeof(AQLComputeMQD)==sizeof(v12_compute_mqd)); using kern_return_t=int; constexpr int kIOReturnSuccess=0,kIOReturnBusy=1,kIOReturnBadArgument=2, kIOReturnUnsupported=3,kIOReturnNotReady=4,kIOReturnNoMemory=5, kIOReturnIOError=6,kIOReturnTimeout=7,kIOReturnNotAttached=8; static unsigned mode, maps, unmaps, bells, writes; static uint64_t now; static bool persistent=false; static unsigned persistentSlot=1; struct PCI { alignas(64) uint8_t memory[16384]{}; void MemoryWrite32(unsigned bar,uint64_t offset,uint32_t value) { assert(bar==0 && offset+4<=sizeof(memory)); ++writes; if (mode!=1 && !(mode==3 && maps)) memcpy(memory+offset,&value,4); } void MemoryRead32(unsigned bar,uint64_t offset,uint32_t *value) { assert(bar==0 && offset+4<=sizeof(memory)); memcpy(value,memory+offset,4); } void MemoryRead64(unsigned bar,uint64_t offset,uint64_t *value) { assert(bar==0 && offset+8<=sizeof(memory)); memcpy(value,memory+offset,8); if (mode==8 && bells) *value=UINT64_MAX; } void MemoryWrite64(unsigned bar,uint64_t offset,uint64_t value) { if (persistent) {assert(bar==1 && offset==uint64_t(0x80+persistentSlot*2)*4);++bells;return;} assert(bar==1 && offset==0x200 && value==0); ++bells; uint64_t wptr; memcpy(&wptr,memory+kAQLMetadataOffset+offsetof(amd_queue_t,write_dispatch_id),8); assert(wptr==1); // doorbell 0 publishes first packet, shadow counts 1 auto *completion=reinterpret_cast(memory+kAQLCompletionOffset); auto *inactive=reinterpret_cast(memory+kAQLInactiveOffset); auto *queue=reinterpret_cast(memory+kAQLMetadataOffset); completion->value=mode==4 ? 1 : mode==9 ? 2 : 0; if (mode==5) inactive->value=2; queue->read_dispatch_id=mode==7 ? 0 : 1; } }; namespace amdgpu { struct DeviceContext { PCI *pci; uint64_t bar0Size=16384,bar2Size=0x200000; unsigned bar0MemIndex=0,bar2MemIndex=1; IPBaseTable ip{}; software_stats::Counters *softwareStats=nullptr; }; struct GMCContext { uint64_t vram_start=0x8000000000; VRAMBumpAllocator vram_alloc,device_vram_alloc; }; struct MESInstance { bool enabled=true,inited=true,submission_pending=false; }; struct MESContext { bool uni_mes_active=true; MESInstance pipe[2]; }; struct GFXConfig { unsigned max_shader_engines=4,max_sh_per_se=2,num_active_cus=64,max_scratch_waves_per_cu=32; uint32_t active_cu_bitmap[4][2]={{255,255},{255,255},{255,255},{255,255}}; }; #include "aql_context.inc" static int mes_map_legacy_queue(DeviceContext &dev,MESContext &,unsigned type,unsigned pipe,unsigned queue, unsigned doorbell,uint64_t base,uint64_t wptr) { const auto &mqd=*reinterpret_cast(dev.pci->memory); assert(mqd.compute_static_thread_mgmt_se0==0x00ff00ff && mqd.compute_static_thread_mgmt_se1==0x00ff00ff); assert(mqd.compute_static_thread_mgmt_se2==0x00ff00ff && mqd.compute_static_thread_mgmt_se3==0x00ff00ff); if (persistent) { assert(type==1 && pipe==persistentSlot/4 && queue==persistentSlot%4 && doorbell==0x80+persistentSlot*2); assert(wptr==0x110000000ull+offsetof(amd_queue_t,write_dispatch_id)); ++maps;return mode==2 ? kIOReturnTimeout : 0; } assert(type==1 && pipe==0 && queue==0 && doorbell==0x80 && base==0x8000000000); assert(wptr==base+kAQLMetadataOffset+offsetof(amd_queue_t,write_dispatch_id)); const auto *q=reinterpret_cast(dev.pci->memory+kAQLMetadataOffset); assert(!q->write_dispatch_id && !q->read_dispatch_id); ++maps; return mode==2 ? kIOReturnTimeout : 0; } static int mes_unmap_legacy_queue(DeviceContext &,MESContext &,unsigned type,unsigned pipe,unsigned queue,unsigned doorbell) { assert(type==1 && pipe==(persistent ? persistentSlot/4 : 0) && queue==(persistent ? persistentSlot%4 : 0) && doorbell==0x80+(persistent ? persistentSlot : 0)*2); ++unmaps; return mode==6 ? kIOReturnTimeout : 0; } static void amdgpu_hdp_flush(DeviceContext &) {} static uint64_t test_clock(int) { return now; } static void IOSleep(unsigned ms) { now+=uint64_t(ms)*1000000; } } #include "amdgpu_vram_io.h" #define clock_gettime_nsec_np test_clock #define CLOCK_UPTIME_RAW 0 namespace amdgpu { #include "aql_launch.inc" } #undef clock_gettime_nsec_np int main() { { uint32_t topology[4][2]={{255,255},{255,255},{255,255},{255,255}},masks[4]{}; assert(gfx12_compute_masks(4,2,64,topology,masks)); for (const auto mask:masks) assert(mask==0x00ff00ff); // Physical harvest holes are compacted independently in each array. topology[0][0]=0xcc;topology[0][1]=0xf3; assert(gfx12_compute_masks(4,2,58,topology,masks) && masks[0]==0x003f000f); assert(!gfx12_compute_masks(4,2,64,topology,masks)); assert(!gfx12_compute_masks(4,3,58,topology,masks)); topology[0][0]=1; assert(!gfx12_compute_masks(4,2,55,topology,masks)); for (auto &se:topology) {se[0]=255;se[1]=0;} assert(gfx12_compute_masks(4,1,32,topology,masks)); for (const auto mask:masks) assert(mask==255); } AQLDispatchRequest r{}; r.version=1;r.codeHandle=1;r.kernargHandle=2;r.kernargBytes=12;r.timeoutUS=100000; r.groups[0]=4;r.groups[1]=r.groups[2]=1;r.threads[0]=32;r.threads[1]=r.threads[2]=1; alignas(64) uint8_t staging[16384]; const uint32_t masks[]={255,63,15,3}; const uint64_t base=0x8000000000,descriptor=base+0x10000000,kernarg=base+0x10004000; assert(aql_build_storage(staging,base,descriptor,kernarg,r,masks,22,32)); const auto &m=*reinterpret_cast(staging); assert(m.header==0xc0310800 && m.compute_static_thread_mgmt_se2==15); assert(m.cp_hqd_persistent_state==(CP_HQD_PERSISTENT_STATE__PRELOAD_REQ_MASK | (0x55<(staging+kAQLMetadataOffset); assert(q.queue_properties==2 && q.caps==0 && q.read_dispatch_id_field_base_byte_offset==offsetof(amd_queue_t,read_dispatch_id)); assert(q.hsa_queue.size==64 && q.max_cu_id==21); const auto &p=*reinterpret_cast(staging+kAQLRingOffset); assert(p.kernel_object==descriptor && reinterpret_cast(p.kernarg_address)==kernarg); assert(p.grid_size_x==128 && p.workgroup_size_x==32 && p.setup==3 && p.header==0x1402); assert(p.completion_signal.handle==base+kAQLCompletionOffset); for (unsigned i=1;i<64;++i) { const auto *slot=reinterpret_cast(staging+kAQLRingOffset+64*i); assert(slot[0]==HSA_PACKET_TYPE_INVALID); for (unsigned j=1;j<16;++j) assert(!slot[j]); } for (unsigned bad=0;bad<10;++bad) { auto invalid=r; switch (bad) { case 0: invalid.version=2;break;case 1: invalid.flags=1;break; case 2: invalid.descriptorOffset=32;break;case 3: invalid.kernargOffset=8;break; case 4: invalid.timeoutUS=1000001;break;case 5: invalid.reserved=1;break; case 6: invalid.threads[2]=1024;break;case 7: invalid.groups[0]=UINT32_MAX;break; case 8: invalid.kernargBytes=4*1024*1024+1;break;case 9: invalid.codeHandle=0;break; } assert(!aql_dispatch_shape(invalid)); } assert(!aql_build_storage(staging,base+4,descriptor,kernarg,r,masks,22,32)); assert(!aql_build_storage(staging,base,descriptor+4,kernarg,r,masks,22,32)); assert(!aql_build_storage(staging,base,descriptor,kernarg+4,r,masks,22,32)); for (mode=0;mode<=9;++mode) { PCI pci; DeviceContext dev{&pci}; dev.ip.version[0]={12,0,1}; GMCContext gmc; gmc.vram_alloc.init(base,16384); MESContext mes; GFXConfig gfx; maps=unmaps=bells=writes=0; now=0; AQLLaunch launch{}; AQLLaunchResult result{}; const auto status=aql_launch(dev,gmc,mes,gfx,launch,descriptor,kernarg,r,result); if (!mode) assert(status==0 && result.stage==5 && !result.completion && result.readIndex==1 && maps==1 && unmaps==1 && bells==1); else assert(status!=0); if (mode>=2) { assert(launch.retained && gmc.vram_alloc.bytes_used()==16384); const auto before=writes; assert(aql_launch(dev,gmc,mes,gfx,launch,descriptor,kernarg,r,result)==kIOReturnBusy && writes==before); } else assert(!launch.retained && gmc.vram_alloc.bytes_used()==0); if (mode==4) assert(now==100000000); if (mode>=2 && mode<=5) assert(unmaps==0); } persistent=true; for (persistentSlot=1;persistentSlot<=7;++persistentSlot) { for (mode=0;mode<=6;++mode) { if (mode==3 || mode==4 || mode==5) continue; PCI pci;DeviceContext dev{&pci};dev.ip.version[0]={12,0,1}; GMCContext gmc;gmc.vram_alloc.init(base,16384);gmc.device_vram_alloc.init(base+0x10000000,256ull<<20);MESContext mes;GFXConfig gfx; alignas(64) amd_queue_t metadata{}; metadata.hsa_queue.base_address=reinterpret_cast(0x110004000ull);metadata.hsa_queue.size=64; metadata.queue_properties=2;metadata.read_dispatch_id_field_base_byte_offset=offsetof(amd_queue_t,read_dispatch_id); metadata.scratch_wave64_lane_byte_size=64; PersistentAQLQueue q{};maps=unmaps=bells=writes=0; auto status=aql_queue_open(dev,gmc,mes,gfx,q,0x110004000,0x110000000,&metadata,64,persistentSlot); if (mode==1) {assert(status!=0 && !q.retained && !gmc.vram_alloc.bytes_used() && !gmc.device_vram_alloc.bytes_used() && !maps);continue;} if (mode==2) {assert(status!=0 && q.retained && gmc.vram_alloc.bytes_used()==16384 && gmc.device_vram_alloc.bytes_used());continue;} assert(status==0 && q.mapped && !q.retained && maps==1); assert(aql_queue_kick(dev,q,0)==kIOReturnBadArgument && !bells); metadata.write_dispatch_id=70;metadata.read_dispatch_id=64; assert(aql_queue_kick(dev,q,69)==0 && bells==1); assert(aql_queue_kick(dev,q,63)==0 && bells==1); // stale multi-producer doorbell assert(aql_queue_kick(dev,q,70)==kIOReturnBadArgument && bells==1); assert(aql_queue_kick(dev,q,UINT64_MAX)==kIOReturnBadArgument && bells==1); status=aql_queue_close(dev,gmc,mes,q); if (mode==6) { assert(status==kIOReturnTimeout && q.retained && q.mapped && gmc.vram_alloc.bytes_used()==16384); assert(aql_queue_kick(dev,q,69)==kIOReturnNotReady); } else assert(status==0 && !q.mapped && !gmc.vram_alloc.bytes_used() && !gmc.device_vram_alloc.bytes_used() && unmaps==1); } } // CP's non-async scratch request allocates device VRAM, publishes the gfx12 // SRD/TMPRING ABI, and clears only recoverable inactive-signal bits. mode=0;persistentSlot=1; { PCI pci;DeviceContext dev{&pci};dev.ip.version[0]={12,0,1}; GMCContext gmc;gmc.vram_alloc.init(base,16384); gmc.device_vram_alloc.init(base+0x10000000,256ull<<20); MESContext mes;GFXConfig gfx; alignas(64) amd_queue_t metadata{}; alignas(64) hsa_kernel_dispatch_packet_t packets[64]{}; metadata.hsa_queue.base_address=reinterpret_cast(0x110004000ull);metadata.hsa_queue.size=64; metadata.queue_properties=2;metadata.read_dispatch_id_field_base_byte_offset=offsetof(amd_queue_t,read_dispatch_id); PersistentAQLQueue queue{}; assert(aql_queue_open(dev,gmc,mes,gfx,queue,0x110004000,0x110000000,&metadata,64,1)==0); assert(metadata.group_segment_aperture_base_hi==0x10000 && metadata.private_segment_aperture_base_hi==0x20000); auto &signal=*reinterpret_cast(pci.memory+kAQLInactiveOffset); metadata.write_dispatch_id=1; packets[0].header=HSA_PACKET_TYPE_KERNEL_DISPATCH;packets[0].private_segment_size=257; signal.value=0x400;uint64_t inactive=UINT64_MAX; assert(aql_queue_service(dev,gmc,gfx,queue,packets,inactive)==0 && !inactive && !signal.value); assert(queue.scratch.size==uint64_t(272)*64*64*32); assert(metadata.max_cu_id==63 && metadata.max_wave_id==31); assert(metadata.scratch_wave64_lane_byte_size==272 && metadata.scratch_backing_memory_location==queue.scratch.gpu_va); assert(metadata.scratch_resource_descriptor[1]==((uint32_t(queue.scratch.gpu_va>>32))|(1u<<30))); SQ_BUF_RSRC_WORD3_GFX12 rsrc{}; rsrc.bits.DST_SEL_X=SQ_SEL_X;rsrc.bits.DST_SEL_Y=SQ_SEL_Y; rsrc.bits.DST_SEL_Z=SQ_SEL_Z;rsrc.bits.DST_SEL_W=SQ_SEL_W; rsrc.bits.FORMAT=BUF_FORMAT_32_UINT;rsrc.bits.ADD_TID_ENABLE=1;rsrc.bits.OOB_SELECT=2; assert(metadata.scratch_resource_descriptor[3]==rsrc.u32All); COMPUTE_TMPRING_SIZE_GFX12 tmpring{};tmpring.bits.WAVES=512;tmpring.bits.WAVESIZE=68; assert(metadata.compute_tmpring_size==tmpring.u32All); assert(metadata.compute_tmpring_size==0x44200); const auto original=queue.scratch; signal.value=4; assert(aql_queue_service(dev,gmc,gfx,queue,packets,inactive)==kIOReturnIOError && inactive==4 && signal.value==4); assert(queue.scratch.gpu_va==original.gpu_va); signal.value=0x405; assert(aql_queue_service(dev,gmc,gfx,queue,packets,inactive)==kIOReturnIOError); signal.value=1;packets[0].private_segment_size=1024; assert(aql_queue_service(dev,gmc,gfx,queue,packets,inactive)==0); assert(metadata.scratch_wave64_lane_byte_size==1024 && gmc.device_vram_alloc.alloc_count()==1); const auto validBacking=queue.scratch.gpu_va; const auto validBytes=metadata.scratch_wave64_lane_byte_size; signal.value=1;packets[0].private_segment_size=262128; assert(aql_queue_service(dev,gmc,gfx,queue,packets,inactive)==kIOReturnNoMemory); assert(inactive==1 && signal.value==1 && queue.scratch.gpu_va==validBacking); assert(metadata.scratch_wave64_lane_byte_size==validBytes && gmc.device_vram_alloc.alloc_count()==1); signal.value=1;packets[0].private_segment_size=262129; assert(aql_queue_service(dev,gmc,gfx,queue,packets,inactive)==kIOReturnBadArgument && signal.value==1); // A later producer may reserve beyond ring capacity; inspect only the // current resident window, without treating that legal MPSC state as fatal. metadata.write_dispatch_id=128;packets[0].private_segment_size=272;signal.value=0x401; assert(aql_queue_service(dev,gmc,gfx,queue,packets,inactive)==0 && !inactive); // Queue teardown must retire scratch only after MES removal. mode=6; assert(aql_queue_close(dev,gmc,mes,queue)==kIOReturnTimeout && gmc.device_vram_alloc.alloc_count()==1); queue.retained=false;mode=0; assert(aql_queue_close(dev,gmc,mes,queue)==0 && !gmc.device_vram_alloc.bytes_used()); } { amd_queue_t metadata{};uint32_t aligned,waves;uint64_t bytes; assert(!aql_scratch_geometry({12,0,1},262129,32,4,32,aligned,waves,bytes)); assert(aql_scratch_geometry({12,0,1},1,33,4,32,aligned,waves,bytes) && aligned==16 && waves==288); assert(!aql_scratch_metadata({12,0,1},metadata,base,UINT64_MAX,16,4,32)); assert(!aql_scratch_metadata({12,0,1},metadata,base,16384,16,4,32)); COMPUTE_TMPRING_SIZE legacy{};legacy.u32All=UINT32_MAX; COMPUTE_TMPRING_SIZE_GFX11 gfx11{};gfx11.u32All=UINT32_MAX; COMPUTE_TMPRING_SIZE_GFX12 gfx12{};gfx12.u32All=UINT32_MAX; assert(legacy.bits.WAVESIZE==((1u<waveSizeBits)-1)); assert(legacy.bits.WAVESIZE==((1u<waveSizeBits)-1)); assert(gfx11.bits.WAVESIZE==((1u<waveSizeBits)-1)); assert(gfx12.bits.WAVESIZE==((1u<waveSizeBits)-1)); assert(gfx12.bits.WAVES==((1u<wavesBits)-1)); assert(!scratch_architecture({0,0,0}) && !scratch_architecture({13,0,0})); assert(!scratch_architecture({8,0,0})); // No capability entry, no guessed fallback. const IPVersion unsupportedVersions[]={{9,0,0},{10,3,0},{11,0,0},{12,0,0},{12,1,0},{13,0,0}}; for (const auto unsupported:unsupportedVersions) { assert(!aql_scratch_geometry(unsupported,272,64,4,32,aligned,waves,bytes)); std::memset(&metadata,0xa5,sizeof(metadata)); amd_queue_t before=metadata; assert(!aql_scratch_metadata(unsupported,metadata,base,34ull<<20,272,4,512)); assert(!std::memcmp(&metadata,&before,sizeof(metadata))); } assert(aql_scratch_geometry({12,0,1},262128,64,4,32,aligned,waves,bytes)); assert(aligned==262128 && waves==512); assert(aql_scratch_metadata({12,0,1},metadata,base,uint64_t(262128)*64*4*32,262128,4,32)); assert((metadata.compute_tmpring_size>>12)==65532); assert(!aql_scratch_metadata({12,0,1},metadata,base,1ull<<32,16,4,32)); assert(!aql_scratch_metadata({12,0,1},metadata,1ull<<48,1ull<<20,16,4,32)); assert(aql_scratch_metadata({12,0,1},metadata,base,1ull<<24,16,4,4095)); assert(!aql_scratch_metadata({12,0,1},metadata,base,1ull<<24,16,4,4096)); } puts("AQL: Linux MQD/register layout, ROCr packet/metadata, publication, completion, unmap and failure retention passed"); }