lib/amdgpu: Add flexible command context API with external BO support
PATCH v3 1/6 lib/amdgpu: Add flexible command context API with external BO support
Implement a new command context management API that supports for command submission.
Key changes:
1. Add cmd_context_create(): Supports external BO injection with GPU/CPU address tracking, and maintains backward compatibility for internal BO allocation.
2. Add cmd_context_destroy(): Conditional BO cleanup (preserve external BOs unless explicitly requested to destroy).
3. Add cmd_submit_packet(): Use separate IB buffer for command submission to avoid overwriting original BO data, and properly manage resource lists including both user-provided and internal resources.
4. Add convenience functions: cmd_submit_write_linear(), cmd_submit_copy_linear(), cmd_submit_atomic() for common operations.
5. Add utility functions: cmd_ring_available() and cmd_get_available_rings() for ring validation.
6. Add cmd_wait_completion(): Unified waiting for both user queues (timeline syncobj) and kernel queues (fence status query).
--- lib/amdgpu/amd_command_submission.c | 592 ++++++++++++++++++++++++++++ lib/amdgpu/amd_command_submission.h | 72 ++++ 2 files changed, 664 insertions(+)
lib/amdgpu/amd_command_submission.c +592 -0
@@ -852,3 +852,595 @@ amdgpu_command_ce_write_fence(amdgpu_device_handle dev,free_cmd_base (base); } +/** +* command context creation with optional external BO +* +* @param device AMDGPU device handle +* @param ip_type IP block type (GFX, DMA, etc.) +* @param ring_id Ring index to use +* @param user_queue Whether to use user queue mode +* @param write_length Size of write operations in DWORDs +* @param external_bo Optional external buffer object (NULL for internal allocation) +* @param external_bo_mc GPU MC address of external BO (required if external_bo provided) +* @param external_bo_cpu CPU mapping of external BO (required if external_bo provided) +* @return New command context, or NULL on failure +*/ +cmd_context_t *cmd_context_create (amdgpu_device_handle device, +enum amd_ip_block_type ip_type, +uint32_t ring_id, +bool user_queue, +uint32_t write_length, +amdgpu_bo_handle external_bo, +uint64_t external_bo_mc, +volatile uint32_t *external_bo_cpu) +{ +cmd_context_t *ctx; +void *cpu_ptr =NULL ; +uint64_t bo_mc =0 ; +amdgpu_va_handle va_handle =NULL ; +int r; + +if (!device) +return NULL ; + +/* Check if the requested ring is available */ +if (!cmd_ring_available (device, ip_type, ring_id, user_queue)) +return NULL ; + +/* Allocate context structure */ + ctx =calloc (1 ,sizeof (cmd_context_t)); +if (!ctx) +return NULL ; + + ctx->device = device; + ctx->ip_type = ip_type; + ctx->user_queue = user_queue; + ctx->last_submit_seq =0 ; + ctx->uses_external_bo = (external_bo !=NULL );// Now this member exists + ctx->initialized = false; + +/* Get IP block for the specified IP type */ + ctx->ip_block =get_ip_block (device, (unsigned int )ip_type); +if (!ctx->ip_block) { +free (ctx); +return NULL ; + } + +/* Allocate ring context structure */ + ctx->ring_ctx =calloc (1 ,sizeof (struct amdgpu_ring_context )); +if (!ctx->ring_ctx) { +free (ctx); +return NULL ; + } + +/* Setup ring context parameters */ + ctx->ring_ctx->write_length = write_length ? write_length :128 ; + ctx->ring_ctx->pm4_size =256 ; + ctx->ring_ctx->pm4 =calloc (ctx->ring_ctx->pm4_size,sizeof (uint32_t )); +if (!ctx->ring_ctx->pm4) { +free (ctx->ring_ctx); +free (ctx); +return NULL ; + } + + ctx->ring_ctx->res_cnt =1 ; + ctx->ring_ctx->ring_id = ring_id; + ctx->ring_ctx->secure = false; + ctx->ring_ctx->user_queue = user_queue; + ctx->ring_ctx->time_out =0 ; + +if (user_queue) { +/* Initialize user queue if requested */ +if (!ctx->ip_block->funcs->userq_create) { +free (ctx->ring_ctx->pm4); +free (ctx->ring_ctx); +free (ctx); +return NULL ; + } + ctx->ip_block->funcs->userq_create (device, ctx->ring_ctx, (unsigned int )ip_type); + }else { +/* Create regular command submission context */ + r =amdgpu_cs_ctx_create (device, &ctx->ring_ctx->context_handle); +if (r) { +free (ctx->ring_ctx->pm4); +free (ctx->ring_ctx); +free (ctx); +return NULL ; + } + } + +/* BO allocation strategy: use external BO if provided, otherwise allocate internally */ +if (external_bo) { +/* Validate external BO arguments */ +if (!external_bo_cpu || !external_bo_mc) { +igt_debug ("Invalid external BO arguments (mc=0x%llx, cpu=%p)\n" , + (unsigned long long )external_bo_mc, external_bo_cpu); +free (ctx->ring_ctx->pm4); +free (ctx->ring_ctx); +free (ctx); +return NULL ; + } + +/* Use caller-provided external BO */ + ctx->ring_ctx->bo = external_bo; + ctx->ring_ctx->bo_mc = external_bo_mc; + ctx->ring_ctx->bo_cpu = external_bo_cpu; + ctx->ring_ctx->va_handle =NULL ;// VA management is caller's responsibility + +igt_info ("Using external BO: GPU=0x%llx, CPU=%p\n" , + (unsigned long long )external_bo_mc, external_bo_cpu); + }else { +/* Allocate internal BO for command operations */ + r =amdgpu_bo_alloc_and_map (device, + ctx->ring_ctx->write_length *sizeof (uint32_t ), +4096 , +AMDGPU_GEM_DOMAIN_GTT , +0 , + &ctx->ring_ctx->bo, + &cpu_ptr, + &bo_mc, + &va_handle); +if (r) { + goto cleanup_error; + } + + ctx->ring_ctx->bo_mc = bo_mc; + ctx->ring_ctx->bo_cpu = (volatile uint32_t *)cpu_ptr; + ctx->ring_ctx->va_handle = va_handle; + +igt_info ("Allocated internal BO: GPU=0x%llx, CPU=%p\n" , + (unsigned long long )bo_mc, cpu_ptr); + } + +/* Initialize resources array */ + ctx->ring_ctx->resources[0 ] = ctx->ring_ctx->bo; +for (int i =1 ; i <4 ; i++) { + ctx->ring_ctx->resources[i] =NULL ; + } + + ctx->initialized = true; +return ctx; + +cleanup_error: +/* Cleanup resources in case of allocation failure */ +if (user_queue) { + ctx->ip_block->funcs->userq_destroy (device, ctx->ring_ctx, (unsigned int )ip_type); + }else { +if (ctx->ring_ctx->context_handle) +amdgpu_cs_ctx_free (ctx->ring_ctx->context_handle); + } +free (ctx->ring_ctx->pm4); +free (ctx->ring_ctx); +free (ctx); +return NULL ; +} + +/** +* context destruction function with external BO control +* +* @param ctx Command context to destroy +* @param destroy_external_bo Whether to destroy external BO (if used) +*/ +void cmd_context_destroy (cmd_context_t *ctx,bool destroy_external_bo) +{ +if (!ctx || !ctx->initialized) +return ; + +if (ctx->ring_ctx) { +/* Handle BO cleanup based on allocation type */ +if (ctx->ring_ctx->bo) { +if (!ctx->uses_external_bo || destroy_external_bo) { +/* Clean up internal BO or external BO if explicitly requested */ +if (ctx->ring_ctx->va_handle) { +/* Internal BO with VA handle - full cleanup */ +amdgpu_bo_unmap_and_free (ctx->ring_ctx->bo, ctx->ring_ctx->va_handle, + ctx->ring_ctx->bo_mc, + ctx->ring_ctx->write_length *sizeof (uint32_t )); + }else if (destroy_external_bo) { +/* External BO without VA handle - just free the BO */ +amdgpu_bo_free (ctx->ring_ctx->bo); + } + }else { +/* External BO, preserve it as requested by caller */ +igt_info ("Preserving external BO (GPU=0x%llx)\n" , + (unsigned long long )ctx->ring_ctx->bo_mc); + } + } + +/* Clean up command submission context */ +if (ctx->user_queue) { +if (ctx->ip_block && ctx->ip_block->funcs->userq_destroy) { + ctx->ip_block->funcs->userq_destroy (ctx->device, ctx->ring_ctx, + (unsigned int )ctx->ip_type); + } + }else { +if (ctx->ring_ctx->context_handle) { +amdgpu_cs_ctx_free (ctx->ring_ctx->context_handle); + } + } + +/* Free PM4 command buffer */ +if (ctx->ring_ctx->pm4) { +free (ctx->ring_ctx->pm4); + } + +free (ctx->ring_ctx); + } + +free (ctx); +} + +/** +* Submit command packet without waiting - Fixed submission logic +*/ +int cmd_submit_packet (cmd_context_t *ctx) +{ +amdgpu_bo_list_handle bo_list; +int r; +struct amdgpu_cs_request ibs_request; +struct amdgpu_cs_ib_info ib_info; +amdgpu_bo_handle ib_bo; +void *ib_cpu; +uint64_t ib_mc_address; +amdgpu_va_handle ib_va_handle; +amdgpu_bo_handle all_res[5 ] = {0 }; +uint32_t res_count =0 ; +uint32_t i; + +if (!ctx || !ctx->initialized || !ctx->ring_ctx) { +igt_debug ("Invalid context parameters\n" ); +return -EINVAL ; + } + +/* Check required parameters */ +if (ctx->ring_ctx->pm4_dw ==0 || ctx->ring_ctx->pm4_dw >1024 ) { +igt_debug ("Invalid PM4 size: %u (must be 1-1024)\n" , ctx->ring_ctx->pm4_dw); +return -EINVAL ; + } + +/* For user queues, use userq_submit */ +if (ctx->user_queue) { +if (!ctx->ip_block || !ctx->ip_block->funcs->userq_submit) { +igt_debug ("User queue submission not supported\n" ); +return -ENOTSUP ; + } + +if (!ctx->ring_ctx->bo_mc) { +igt_debug ("Invalid BO MC address for user queue\n" ); +return -EINVAL ; + } + + ctx->ip_block->funcs->userq_submit (ctx->device, ctx->ring_ctx, + (unsigned int )ctx->ip_type, + ctx->ring_ctx->bo_mc); + ctx->last_submit_seq = ctx->ring_ctx->point; +igt_info ("User queue submission successful, point=%lu\n" , ctx->last_submit_seq); +return 0 ; + } + +/* For regular queues, setup and submit CS */ +memset (&ibs_request,0 ,sizeof (ibs_request)); +memset (&ib_info,0 ,sizeof (ib_info)); + +/* Allocate separate IB buffer instead of reusing command buffer */ + r =amdgpu_bo_alloc_and_map (ctx->device, + ctx->ring_ctx->pm4_dw *sizeof (uint32_t ), +4096 , +AMDGPU_GEM_DOMAIN_GTT , +0 , + &ib_bo, + &ib_cpu, + &ib_mc_address, + &ib_va_handle); +if (r) { +igt_debug ("Failed to allocate IB: %d\n" , r); +return r; + } + +/* Copy PM4 commands to IB */ +memcpy (ib_cpu, ctx->ring_ctx->pm4, ctx->ring_ctx->pm4_dw *sizeof (uint32_t )); + +/* Setup IB information */ + ib_info.ib_mc_address = ib_mc_address; + ib_info.size = ctx->ring_ctx->pm4_dw; +if (ctx->ring_ctx->secure) + ib_info.flags |=AMDGPU_IB_FLAGS_SECURE ; + +/* Setup submission request */ + ibs_request.ip_type = (unsigned int )ctx->ip_type; + ibs_request.ring = ctx->ring_ctx->ring_id; + ibs_request.number_of_ibs =1 ; + ibs_request.ibs = &ib_info; + ibs_request.fence_info.handle =NULL ; + +/* Create resource list containing both original resources and IB */ +/* Add original resources */ +for (i =0 ; i < ctx->ring_ctx->res_cnt && i <4 ; i++) { +if (ctx->ring_ctx->resources[i]) { + all_res[res_count++] = ctx->ring_ctx->resources[i]; + } + } + +/* Add IB as the last resource */ + all_res[res_count++] = ib_bo; + +/* Create BO list */ + r =amdgpu_bo_list_create (ctx->device, res_count, all_res,NULL , &bo_list); +if (r) { +igt_debug ("amdgpu_bo_list_create failed: %d\n" , r); +amdgpu_bo_unmap_and_free (ib_bo, ib_va_handle, ib_mc_address, + ctx->ring_ctx->pm4_dw *sizeof (uint32_t )); +return r; + } + + ibs_request.resources = bo_list; + +/* Submit command - strict error checking */ + r =amdgpu_cs_submit (ctx->ring_ctx->context_handle,0 , &ibs_request,1 ); + +if (r !=0 ) { +igt_debug ("amdgpu_cs_submit failed: %d\n" , r); +amdgpu_bo_list_destroy (bo_list); +amdgpu_bo_unmap_and_free (ib_bo, ib_va_handle, ib_mc_address, + ctx->ring_ctx->pm4_dw *sizeof (uint32_t )); +return r; + } + +/* Record submission sequence number for waiting */ + ctx->last_submit_seq = ibs_request.seq_no; +igt_debug ("Command submitted successfully, seq_no=%lu\n" , ctx->last_submit_seq); + +/* Cleanup resources */ +amdgpu_bo_list_destroy (bo_list); +amdgpu_bo_unmap_and_free (ib_bo, ib_va_handle, ib_mc_address, + ctx->ring_ctx->pm4_dw *sizeof (uint32_t )); + +return 0 ; +} + +int cmd_place_packet (cmd_context_t *ctx,const cmd_packet_params_t *params) +{ +int result = -EINVAL ; + +if (!ctx || !ctx->initialized || !ctx->ip_block || !params) +return -EINVAL ; + +if (!ctx->ring_ctx || !ctx->ring_ctx->bo_cpu) +return -EINVAL ; + +/* Clear the buffer before operation */ +memset ((void *)ctx->ring_ctx->bo_cpu,0 , + ctx->ring_ctx->write_length *sizeof (uint32_t )); + + +/* Build the command packet based on type */ +switch (params->type) { +case CMD_PACKET_WRITE_LINEAR : +if (!ctx->ip_block->funcs->write_linear) +return -ENOTSUP ; + +/* TODO: allow user set the dst and data */ +//ctx->ring_ctx->bo_mc = params->dst_addr; + ctx->ring_ctx->write_length = params->size /sizeof (uint32_t ); + +/* Build PM4 packet */ + result = ctx->ip_block->funcs->write_linear (ctx->ip_block->funcs, + ctx->ring_ctx, + &ctx->ring_ctx->pm4_dw); + +/* Copy PM4 packet to ring buffer */ +if (result ==0 && ctx->ring_ctx->pm4_dw >0 ) { +memcpy ((void *)ctx->ring_ctx->bo_cpu, + ctx->ring_ctx->pm4, + ctx->ring_ctx->pm4_dw *sizeof (uint32_t )); + +//igt_info("cmd_place_packet: Copied %u DWORDs to ring buffer\n", +// ctx->ring_ctx->pm4_dw); + } +return result; + +case CMD_PACKET_WRITE_ATOMIC : +if (!ctx->ip_block->funcs->write_linear_atomic) +return -ENOTSUP ; + +/* TODO: allow user set the dst and data */ +//ctx->ring_ctx->bo_mc = params->dst_addr; + ctx->ring_ctx->write_length =1 ;/* Atomic operations typically work on single DWORD */ + + result = ctx->ip_block->funcs->write_linear_atomic (ctx->ip_block->funcs, + ctx->ring_ctx, + &ctx->ring_ctx->pm4_dw); + +/* Copy PM4 packet to ring buffer */ +if (result ==0 && ctx->ring_ctx->pm4_dw >0 ) { +memcpy ((void *)ctx->ring_ctx->bo_cpu, + ctx->ring_ctx->pm4, + ctx->ring_ctx->pm4_dw *sizeof (uint32_t )); + +//igt_info("cmd_place_packet: Copied %u DWORDs to ring buffer (atomic)\n", +// ctx->ring_ctx->pm4_dw); + } +return result; + +case CMD_PACKET_COPY_LINEAR : +if (!ctx->ip_block->funcs->copy_linear) +return -ENOTSUP ; + +/* For copy operations, we need both source and destination addresses */ +/* This would require extending the API to pass both addresses */ +/* For now, use the internal buffer as source */ + ctx->ring_ctx->write_length = params->size /sizeof (uint32_t ); + + result = ctx->ip_block->funcs->copy_linear (ctx->ip_block->funcs, + ctx->ring_ctx, + &ctx->ring_ctx->pm4_dw); + +/* Copy PM4 packet to ring buffer */ +if (result ==0 && ctx->ring_ctx->pm4_dw >0 ) { +memcpy ((void *)ctx->ring_ctx->bo_cpu, + ctx->ring_ctx->pm4, + ctx->ring_ctx->pm4_dw *sizeof (uint32_t )); + +//igt_info("cmd_place_packet: Copied %u DWORDs to ring buffer (copy)\n", +// ctx->ring_ctx->pm4_dw); + } +return result; + +case CMD_PACKET_COPY_ATOMIC : +/* TODO: Implement atomic copy if supported */ +igt_info ("cmd_place_packet: ATOMIC_COPY not implemented\n" ); +return -ENOTSUP ; + +case CMD_PACKET_FENCE : +/* TODO: Implement fence operation */ +igt_info ("cmd_place_packet: FENCE not implemented\n" ); +return -ENOTSUP ; + +case CMD_PACKET_TIMESTAMP : +/* TODO: Implement timestamp operation */ +igt_info ("cmd_place_packet: TIMESTAMP not implemented\n" ); +return -ENOTSUP ; + +default : +igt_info ("cmd_place_packet: Unknown packet type: %d\n" , params->type); +return -EINVAL ; + } +} + +/** +* Build and submit packet in one operation +*/ +int cmd_place_and_submit_packet (cmd_context_t *ctx,const cmd_packet_params_t *params) +{ +int r; + + r =cmd_place_packet (ctx, params); +if (r) +return r; + +return cmd_submit_packet (ctx); +} + +/** +* Convenience function for write linear +*/ +int cmd_submit_write_linear (cmd_context_t *ctx,uint64_t dst_addr,uint32_t size,uint32_t data) +{ +cmd_packet_params_t params = { + .type =CMD_PACKET_WRITE_LINEAR , + .dst_addr = dst_addr, + .size = size, + .data = data, + }; + +return cmd_place_and_submit_packet (ctx, ¶ms); +} + +/** +* Convenience function for copy linear +*/ +int cmd_submit_copy_linear (cmd_context_t *ctx,uint64_t src_addr,uint64_t dst_addr,uint32_t size) +{ +cmd_packet_params_t params = { + .type =CMD_PACKET_COPY_LINEAR , + .src_addr = src_addr, + .dst_addr = dst_addr, + .size = size, + }; + +return cmd_place_and_submit_packet (ctx, ¶ms); +} + +/** +* Convenience function for atomic operation +*/ +int cmd_submit_atomic (cmd_context_t *ctx,uint64_t dst_addr,uint32_t data) +{ +cmd_packet_params_t params = { + .type =CMD_PACKET_WRITE_ATOMIC , + .dst_addr = dst_addr, + .data = data, + }; + +return cmd_place_and_submit_packet (ctx, ¶ms); +} + +/** +* Wait for all submitted packets to complete (equivalent to Wait4PacketConsumption) +*/ +int cmd_wait_completion (cmd_context_t *ctx) +{ +struct amdgpu_cs_fence fence_status = {0 }; +uint32_t expired; + +if (!ctx || !ctx->initialized) +return -EINVAL ; + +/* For user queues, wait on timeline syncobj */ +if (ctx->user_queue) { +if (!ctx->ring_ctx->timeline_syncobj_handle) +return -EINVAL ; + +return amdgpu_timeline_syncobj_wait (ctx->device, + ctx->ring_ctx->timeline_syncobj_handle, + ctx->ring_ctx->point); + } + +/* For regular queues, wait on fence */ + fence_status.ip_type = (unsigned int )ctx->ip_type; + fence_status.ip_instance =0 ; + fence_status.ring = ctx->ring_ctx->ring_id; + fence_status.context = ctx->ring_ctx->context_handle; + fence_status.fence = ctx->last_submit_seq; + +return amdgpu_cs_query_fence_status (&fence_status, +AMDGPU_TIMEOUT_INFINITE , +0 , &expired); +} + +/** +* Check if ring is available +*/ +bool cmd_ring_available (amdgpu_device_handle device, +enum amd_ip_block_type ip_type, +uint32_t ring_id, +bool user_queue) +{ +struct drm_amdgpu_info_hw_ip hw_ip_info; +int r; + + r =amdgpu_query_hw_ip_info (device, (unsigned int )ip_type,0 , &hw_ip_info); +if (r) +return false; + +if (user_queue) { +/* For user queues, check user queue slots */ +uint32_t userq_slots = hw_ip_info.num_userq_slots; + +return (ring_id < userq_slots); + }else { +/* For regular queues, check available rings */ +return !!(hw_ip_info.available_rings & (1ULL << ring_id)); + } +} + +/** +* Get number of available rings +*/ +uint32_t cmd_get_available_rings (amdgpu_device_handle device, +enum amd_ip_block_type ip_type, +bool user_queue) +{ +struct drm_amdgpu_info_hw_ip hw_ip_info; +int r; + + r =amdgpu_query_hw_ip_info (device, (unsigned int )ip_type,0 , &hw_ip_info); +if (r) +return 0 ; + +if (user_queue) { +return hw_ip_info.num_userq_slots; + }else { +return __builtin_popcountll (hw_ip_info.available_rings); + } +}
lib/amdgpu/amd_command_submission.h +72 -0
82 lines not on this page · raw message
Signature · 1 line
2.49.0
PATCH v3 2/6 lib/amdgpu: Add compute dispatch helper for GPU memory verification
Implement a reusable compute dispatch helper function that executes a pre-compiled shader to copy a DWORD from a source to destination address and verifies the result. This function is designed for GPU-side validation of memory operations (e.g., ptrace modifications to VRAM) by ensuring the GPU can read/write the modified memory.
Key features:
1. Uses independent resources (context, command buffer, shader BO, destination BO) for each dispatch to avoid state pollution between tests.
2. Programs user data registers to pass source/destination addresses to the shader.
3. Dispatches a 1x1x1 workgroup to execute the shader once.
4. Waits for fence completion and verifies the destination buffer contains the expected value.
5. Cleans up all allocated resources even if an error occurs.
---
lib/amdgpu/{ => compute_utils}/amd_dispatch.c | 8 +-
lib/amdgpu/{ => compute_utils}/amd_dispatch.h | 0
.../amd_dispatch_helpers.c | 173 +++++++++++++++-
.../amd_dispatch_helpers.h | 8 +-
lib/amdgpu/compute_utils/amd_gca.c | 190 ++++++++++++++++++
lib/amdgpu/compute_utils/amd_gca.h | 51 +++++
.../{ => compute_utils}/amd_shared_dispatch.h | 0
7 files changed, 422 insertions(+), 8 deletions(-)
rename lib/amdgpu/{ => compute_utils}/amd_dispatch.c (99%)
rename lib/amdgpu/{ => compute_utils}/amd_dispatch.h (100%)
rename lib/amdgpu/{ => compute_utils}/amd_dispatch_helpers.c (53%)
rename lib/amdgpu/{ => compute_utils}/amd_dispatch_helpers.h (86%)
create mode 100644 lib/amdgpu/compute_utils/amd_gca.c
create mode 100644 lib/amdgpu/compute_utils/amd_gca.h
rename lib/amdgpu/{ => compute_utils}/amd_shared_dispatch.h (100%)lib/amdgpu/compute_utils/amd_dispatch.c renamed from lib/amdgpu/amd_dispatch.c +4 -4
@@ -4,13 +4,13 @@// Copyright 2023 Advanced Micro Devices, Inc. #include <amdgpu.h> -#include "amd_memory.h" +#include "amdgpu/amd_memory.h" #include "amd_dispatch.h" #include "amd_shared_dispatch.h" #include "amd_dispatch_helpers.h" -#include "amd_PM4.h" -#include "amd_ip_blocks.h" -#include "amd_shaders.h" +#include "amdgpu/amd_PM4.h" +#include "amdgpu/amd_ip_blocks.h" +#include "amdgpu/shaders/amd_shaders.h" static void amdgpu_memset_dispatch_test(amdgpu_device_handle device_handle,
lib/amdgpu/compute_utils/amd_dispatch.h renamed from lib/amdgpu/amd_dispatch.h +0 -0
lib/amdgpu/compute_utils/amd_dispatch_helpers.c renamed from lib/amdgpu/amd_dispatch_helpers.c +170 -3
183 lines not on this page · raw message
lib/amdgpu/compute_utils/amd_dispatch_helpers.h renamed from lib/amdgpu/amd_dispatch_helpers.h +7 -1
@@ -25,7 +25,7 @@#define AMD_DISPATCH_HELPERS_H #include <amdgpu.h> -#include "amd_ip_blocks.h" +#include "lib/amdgpu/amd_ip_blocks.h" struct amdgpu_cmd_base ;
@@ -35,4 +35,10 @@ int amdgpu_dispatch_write_cumask(struct amdgpu_cmd_base *base_cmd, uint32_t versint amdgpu_dispatch_write2hw (struct amdgpu_cmd_base *base_cmd,uint64_t shader_addr,uint32_t version,enum cmd_error_type err); +int execute_compute_dispatch (amdgpu_device_handle device, +uint32_t version, +uint64_t shader_mc_addr, +uint64_t src_mc_addr, +uint64_t dst_mc_addr, +uint32_t expected_value);#endif
lib/amdgpu/compute_utils/amd_gca.c new file +190 -0
190 lines not on this page · raw message
lib/amdgpu/compute_utils/amd_gca.h new file +51 -0
51 lines not on this page · raw message
lib/amdgpu/compute_utils/amd_shared_dispatch.h renamed from lib/amdgpu/amd_shared_dispatch.h +0 -0
Signature · 1 line
2.49.0
PATCH v3 3/6 lib/amdgpu: add GPU memory utilities
In reply to Jesse.ZhangExtend amd_memory.c with a suite of reusable memory management utilities.
Key additions:
1. Memory info queries:
- get_available_system_memory(): Fetch max allocatable GTT size via heap info
- get_available_vram(): Fetch max allocatable VRAM size via heap info
- get_gpu_memory_info(): Comprehensive memory info (VRAM/GTT/CPU-accessible VRAM) using amdgpu_query_info(AMDGPU_INFO_MEMORY)
2. DMA-BUF and GPU BO utilities:
- create_dmabuf(): Convert user-space memory to GPU-accessible DMA-BUF via amdgpu_bo
- virtual_mem_to_gpu_bo(): Map user-space memory to GPU BO (DMA-BUF import + VA mapping)
- virtual_alloc_memory(): Wrapper for mmap with protection flag mapping
- virtual_free_memory(): Wrapper for munmap
- wait_on_value(): Poll memory for expected value with timeout (test utility)
--- lib/amdgpu/amd_memory.c | 300 ++++++++++++++++++++++++++++++++++++++++ lib/amdgpu/amd_memory.h | 29 +++- 2 files changed, 328 insertions(+), 1 deletion(-)
lib/amdgpu/amd_memory.c +300 -0
@@ -26,6 +26,14 @@#include "amd_memory.h" #include "amd_PM4.h" #include "amd_ip_blocks.h" +#include <amdgpu.h> +#include <amdgpu_drm.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <sys/mman.h> +#include <inttypes.h> +/** * * @param device_handle
@@ -398,4 +406,296 @@ void amdgpu_command_submission_multi_fence_wait_all(amdgpu_device_handle device,igt_assert_eq (r,0 ); } +/* +* Get available GTT size of the GPU node +* Equivalent to Thunk's hsaKmtAvailableMemory function, using amdgpu_query_heap_info +* to fetch max allocatable size from GTT domain heap. +*/ +uint64_t get_available_system_memory (amdgpu_device_handle dev) +{ +struct amdgpu_heap_info gtt_heap; +int ret; + + ret =amdgpu_query_heap_info (dev,AMDGPU_GEM_DOMAIN_GTT ,0 , >t_heap); +if (ret !=0 ) { +fprintf (stderr,"Failed to query VRAM heap: %d\n" , ret); +return 0 ; + } + +return gtt_heap.max_allocation; +} + +/* +* Get available VRAM size of the GPU node +* Equivalent to Thunk's hsaKmtAvailableMemory function, using amdgpu_query_heap_info +* to fetch max allocatable size from VRAM domain heap. +*/ +uint64_t get_available_vram (amdgpu_device_handle dev) +{ +struct amdgpu_heap_info vram_heap; +int ret; + + ret =amdgpu_query_heap_info (dev,AMDGPU_GEM_DOMAIN_VRAM ,0 , &vram_heap); +if (ret !=0 ) { +fprintf (stderr,"Failed to query VRAM heap: %d\n" , ret); +return 0 ; + } + +return vram_heap.max_allocation; +} + +/** +* get_gpu_memory_info - Get GPU memory information +* @device: AMDGPU device handle +* @vram_size: Output for VRAM usable heap size +* @cpu_accessible_vram_size: Output for CPU accessible VRAM usable heap size +* @gtt_size: Output for GTT usable heap size +* +* Returns 0 on success, error code on failure. +*/ +int get_gpu_memory_info (amdgpu_device_handle device, +uint64_t *vram_size, +uint64_t *cpu_accessible_vram_size, +uint64_t *gtt_size) +{ +struct drm_amdgpu_memory_info mem_info; +int r; + + r =amdgpu_query_info (device,AMDGPU_INFO_MEMORY ,sizeof (mem_info), &mem_info); +if (r) { +igt_info ("Failed to query GPU memory info, error: %d\n" , r); +return r; + } + +if (vram_size) + *vram_size = mem_info.vram.usable_heap_size; + +if (cpu_accessible_vram_size) + *cpu_accessible_vram_size = mem_info.cpu_accessible_vram.usable_heap_size; + +if (gtt_size) + *gtt_size = mem_info.gtt.usable_heap_size; + +igt_debug ("GPU Memory Info: VRAM=%" PRIu64" MB, CPU_ACCESSIBLE_VRAM=%" PRIu64" MB, GTT=%" PRIu64" MB\n" , + (uint64_t )(mem_info.vram.usable_heap_size / (1024 *1024 )), + (uint64_t )(mem_info.cpu_accessible_vram.usable_heap_size / (1024 *1024 )), + (uint64_t )(mem_info.gtt.usable_heap_size / (1024 *1024 ))); +return 0 ; +} + +/** +* Create a DMA-BUF to encapsulate system memory (using amdgpu_bo_alloc instead of DRM_IOCTL_GEM_CREATE) +* Workflow: Allocate GPU-accessible BO -> Map to user space -> Copy system memory data -> Export as DMA-BUF +*/ +int create_dmabuf (amdgpu_device_handle dev,void *sys_addr,size_t size) +{ +struct amdgpu_bo *bo =NULL ; +struct amdgpu_bo_alloc_request alloc_req = {0 }; +void *bo_map =NULL ; +int dmabuf_fd =-1 ; +int ret; + +/* 1. Allocate GPU buffer object (replaces DRM_IOCTL_GEM_CREATE) */ + alloc_req.alloc_size = size; + alloc_req.flags =AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED ;// Allow CPU access + alloc_req.preferred_heap =AMDGPU_GEM_DOMAIN_GTT ;// Use GTT domain (system memory accessible) + ret =amdgpu_bo_alloc (dev, &alloc_req, &bo); +if (ret !=0 ) { +igt_warn ("amdgpu_bo_alloc failed: %d\n" , ret); +return -1 ; + } + +/* 2. Map BO to user space (facilitates copying system memory data) */ + ret =amdgpu_bo_cpu_map (bo, &bo_map); +if (ret !=0 ) { +igt_warn ("amdgpu_bo_cpu_map failed: %d\n" , ret); + goto free_bo; + } + +/* 3. Copy system memory data to GPU BO */ +memcpy (bo_map, sys_addr, size); + +/* 4. Unmap from user space (data copy completed) */ +amdgpu_bo_cpu_unmap (bo); + bo_map =NULL ; +/* 5. Export BO as DMA-BUF (replaces DRM_IOCTL_PRIME_HANDLE_TO_FD) */ + ret =amdgpu_bo_export (bo, amdgpu_bo_handle_type_dma_buf_fd, (uint32_t *)&dmabuf_fd); +if (ret !=0 ) { +igt_warn ("amdgpu_bo_export failed: %d\n" , ret); + goto free_bo; + } + +/* 6. Free BO handle (DMA-BUF exists independently) */ +amdgpu_bo_free (bo); +return dmabuf_fd; + +free_bo: +if (bo) +amdgpu_bo_free (bo); +return -1 ; +} + +/** +* virtual_mem_to_gpu_bo - Map user-space virtual memory to GPU buffer object +* @device: AMDGPU device handle +* @cpu_ptr: Pointer to user-space memory to map +* @size: Size of the memory region to map +* @out_bo: Output pointer for the created GPU buffer handle +* @out_gpu_va: Output pointer for the GPU virtual address of the mapped buffer +* @out_va_handle: Output pointer for the VA range handle +* +* This function converts a user-space memory region to a DMA-BUF, imports it +* as a GPU buffer object (BO), and maps it to the GPU's virtual address space. +* Returns 0 on success, negative error code on failure. +*/ +int virtual_mem_to_gpu_bo (amdgpu_device_handle device, +void *cpu_ptr, +size_t size, +amdgpu_bo_handle *out_bo, +uint64_t *out_gpu_va, +amdgpu_va_handle *out_va_handle) +{ +int r; +int dmabuf_fd; +struct amdgpu_bo_import_result import_result = {0 }; +uint64_t aligned_size =ALIGN (size,getpagesize ()); +uint64_t va_base; +amdgpu_va_handle va_handle; + +/* Convert user memory to DMA-BUF */ + dmabuf_fd =create_dmabuf (device, cpu_ptr, size); +if (dmabuf_fd <0 ) { +igt_warn ("Failed to create DMA-BUF fd\n" ); +return -1 ; + } + +/* Import DMA-BUF into AMDGPU as a buffer object */ + r =amdgpu_bo_import (device, + amdgpu_bo_handle_type_dma_buf_fd, + dmabuf_fd, + &import_result); +close (dmabuf_fd);/* FD no longer needed after import */ +if (r !=0 ) { +igt_warn ("amdgpu_bo_import failed: %d\n" , r); +return r; + } + +/* Allocate GPU virtual address range for the buffer */ + r =amdgpu_va_range_alloc (device, + amdgpu_gpu_va_range_general, + aligned_size, +getpagesize (), +0 , + &va_base, + &va_handle, +0 ); +if (r !=0 ) { +igt_warn ("amdgpu_va_range_alloc failed: %d\n" , r); +amdgpu_bo_free (import_result.buf_handle); +return r; + } + +/* Map buffer object to the allocated GPU virtual address */ + r =amdgpu_bo_va_op (import_result.buf_handle, +0 , + aligned_size, + va_base, +AMDGPU_VM_PAGE_READABLE |AMDGPU_VM_PAGE_WRITEABLE , +AMDGPU_VA_OP_MAP ); +if (r !=0 ) { +igt_warn ("amdgpu_bo_va_op failed: %d\n" , r); +amdgpu_va_range_free (va_handle); +amdgpu_bo_free (import_result.buf_handle); +return r; + } + +/* Output results */ + *out_bo = import_result.buf_handle; + *out_gpu_va = va_base; + *out_va_handle = va_handle; + +return 0 ; +} + +/** +* Virtual memory allocation wrapper +*/ +void *virtual_alloc_memory (void *address,unsigned int size,int memProtection) +{ +void *ptr; +/** +* Memory protection flags mapping table +* +* This array maps logical memory protection combinations to the corresponding +* POSIX mmap protection flags. The index corresponds to the memory protection +* type defined in the test framework. +* +* Index mapping: +* 0: MEM_NONE - No access permissions +* 1: MEM_READ - Read-only access +* 2: MEM_WRITE - Write-only access +* 3: MEM_READ_WRITE - Read and write access +* 4: MEM_EXEC - Execute-only access +* 5: MEM_EXEC_READ - Execute and read access +* 6: MEM_EXEC_WRITE - Execute and write access +* 7: MEM_EXEC_READ_WRITE - Execute, read and write access +* +* Note: Some combinations like write-only (PROT_WRITE) may not be supported +* on all systems and typically require read permission as well. +*/ +static int protection_flags[8 ] = { +PROT_NONE ,// 0: No access +PROT_READ ,// 1: Read-only +PROT_WRITE ,// 2: Write-only (may not work on some systems) +PROT_READ |PROT_WRITE ,// 3: Read and write +PROT_EXEC ,// 4: Execute-only +PROT_EXEC |PROT_READ ,// 5: Execute and read +PROT_EXEC |PROT_WRITE ,// 6: Execute and write +PROT_EXEC |PROT_READ |PROT_WRITE // 7: Execute, read and write + }; + + ptr =mmap (address, size, protection_flags[memProtection], +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); + +if (ptr ==MAP_FAILED ) { +igt_warn ("mmap failed: %s\n" ,strerror (errno)); + ptr =NULL ; + } + +return ptr; +} + +/** +* Virtual memory free wrapper +*/ +bool virtual_free_memory (void *address,unsigned int size) +{ +if (munmap (address, size) ==0 ) +return true; +else { +igt_warn ("munmap failed: %s\n" ,strerror (errno)); +return false; + } +} + +/** +* Wait for specific value in memory with timeout +*/ +bool wait_on_value (unsigned int *ptr,unsigned int expected) +{ +const int timeout_ms =5000 ;// 5 second timeout +int elapsed_ms =0 ; + +while (*ptr != expected) { +usleep (1000 );// 1ms + elapsed_ms++; + +if (elapsed_ms > timeout_ms) { +igt_info ("Timeout waiting for value 0x%08X, got 0x%08X\n" , + expected, *ptr); +return false; + } + } +return true; +}
lib/amdgpu/amd_memory.h +28 -1
@@ -29,7 +29,6 @@#include <amdgpu.h> #include <amdgpu_drm.h> - amdgpu_bo_handle gpu_mem_alloc(amdgpu_device_handle device_handle, uint64_t size,
@@ -80,4 +79,32 @@ amdgpu_get_bo_list(amdgpu_device_handle dev, amdgpu_bo_handle bo1,void amdgpu_command_submission_multi_fence_wait_all (amdgpu_device_handle device,bool wait_all); +uint64_t +get_available_system_memory (amdgpu_device_handle dev); + +uint64_t +get_available_vram (amdgpu_device_handle dev); + +int +get_gpu_memory_info (amdgpu_device_handle device, +uint64_t *vram_size, +uint64_t *cpu_accessible_vram_size, +uint64_t *gtt_size); +int +create_dmabuf (amdgpu_device_handle dev,void *sys_addr,size_t size); + +int virtual_mem_to_gpu_bo (amdgpu_device_handle device, +void *cpu_ptr, +size_t size, +amdgpu_bo_handle *out_bo, +uint64_t *out_gpu_va, +amdgpu_va_handle *out_va_handle); +void +*virtual_alloc_memory (void *address,unsigned int size,int memProtection); + +bool +virtual_free_memory (void *address,unsigned int size); + +bool +wait_on_value (unsigned int *ptr,unsigned int expected);#endif
Signature · 1 line
2.49.0
The series of 6 patches looks good to me. Reviewed-by: Vitaly Prosyak <[email protected]>
Quoted 712 lines · Jesse.Zhang
Implement a new command context management API that supports for command submission.
Key changes:
1. Addcmd_context_create(): Supports external BO injection with GPU/CPU address tracking, and maintains backward compatibility for internal BO allocation.
2. Addcmd_context_destroy(): Conditional BO cleanup (preserve external BOs unless explicitly requested to destroy).
3. Addcmd_submit_packet(): Use separate IB buffer for command submission to avoid overwriting original BO data, and properly manage resource lists including both user-provided and internal resources.
4. Add convenience functions:cmd_submit_write_linear(),cmd_submit_copy_linear(),cmd_submit_atomic()for common operations.
5. Add utility functions:cmd_ring_available()andcmd_get_available_rings()for ring validation.
6. Addcmd_wait_completion(): Unified waiting for both user queues (timeline syncobj) and kernel queues (fence status query).Signed-off-by Jesse Zhang <[email protected]>--- lib/amdgpu/amd_command_submission.c | 592 ++++++++++++++++++++++++++++ lib/amdgpu/amd_command_submission.h | 72 ++++ 2 files changed, 664 insertions(+)lib/amdgpu/amd_command_submission.c +592
-0@@ -852,3 +852,595 @@ amdgpu_command_ce_write_fence(amdgpu_device_handle dev,free_cmd_base (base); } +/** +* command context creation with optional external BO +* +* @param device AMDGPU device handle +* @param ip_type IP block type (GFX, DMA, etc.) +* @param ring_id Ring index to use +* @param user_queue Whether to use user queue mode +* @param write_length Size of write operations in DWORDs +* @param external_bo Optional external buffer object (NULL for internal allocation) +* @param external_bo_mc GPU MC address of external BO (required if external_bo provided) +* @param external_bo_cpu CPU mapping of external BO (required if external_bo provided) +* @return New command context, or NULL on failure +*/ +cmd_context_t *cmd_context_create (amdgpu_device_handle device, +enum amd_ip_block_type ip_type, +uint32_t ring_id, +bool user_queue, +uint32_t write_length, +amdgpu_bo_handle external_bo, +uint64_t external_bo_mc, +volatile uint32_t *external_bo_cpu) +{ +cmd_context_t *ctx; +void *cpu_ptr =NULL ; +uint64_t bo_mc =0 ; +amdgpu_va_handle va_handle =NULL ; +int r; + +if (!device) +return NULL ; + +/* Check if the requested ring is available */ +if (!cmd_ring_available (device, ip_type, ring_id, user_queue)) +return NULL ; + +/* Allocate context structure */ + ctx =calloc (1 ,sizeof (cmd_context_t)); +if (!ctx) +return NULL ; + + ctx->device = device; + ctx->ip_type = ip_type; + ctx->user_queue = user_queue; + ctx->last_submit_seq =0 ; + ctx->uses_external_bo = (external_bo !=NULL );// Now this member exists + ctx->initialized = false; + +/* Get IP block for the specified IP type */ + ctx->ip_block =get_ip_block (device, (unsigned int )ip_type); +if (!ctx->ip_block) { +free (ctx); +return NULL ; + } + +/* Allocate ring context structure */ + ctx->ring_ctx =calloc (1 ,sizeof (struct amdgpu_ring_context )); +if (!ctx->ring_ctx) { +free (ctx); +return NULL ; + } + +/* Setup ring context parameters */ + ctx->ring_ctx->write_length = write_length ? write_length :128 ; + ctx->ring_ctx->pm4_size =256 ; + ctx->ring_ctx->pm4 =calloc (ctx->ring_ctx->pm4_size,sizeof (uint32_t )); +if (!ctx->ring_ctx->pm4) { +free (ctx->ring_ctx); +free (ctx); +return NULL ; + } + + ctx->ring_ctx->res_cnt =1 ; + ctx->ring_ctx->ring_id = ring_id; + ctx->ring_ctx->secure = false; + ctx->ring_ctx->user_queue = user_queue; + ctx->ring_ctx->time_out =0 ; + +if (user_queue) { +/* Initialize user queue if requested */ +if (!ctx->ip_block->funcs->userq_create) { +free (ctx->ring_ctx->pm4); +free (ctx->ring_ctx); +free (ctx); +return NULL ; + } + ctx->ip_block->funcs->userq_create (device, ctx->ring_ctx, (unsigned int )ip_type); + }else { +/* Create regular command submission context */ + r =amdgpu_cs_ctx_create (device, &ctx->ring_ctx->context_handle); +if (r) { +free (ctx->ring_ctx->pm4); +free (ctx->ring_ctx); +free (ctx); +return NULL ; + } + } + +/* BO allocation strategy: use external BO if provided, otherwise allocate internally */ +if (external_bo) { +/* Validate external BO arguments */ +if (!external_bo_cpu || !external_bo_mc) { +igt_debug ("Invalid external BO arguments (mc=0x%llx, cpu=%p)\n" , + (unsigned long long )external_bo_mc, external_bo_cpu); +free (ctx->ring_ctx->pm4); +free (ctx->ring_ctx); +free (ctx); +return NULL ; + } + +/* Use caller-provided external BO */ + ctx->ring_ctx->bo = external_bo; + ctx->ring_ctx->bo_mc = external_bo_mc; + ctx->ring_ctx->bo_cpu = external_bo_cpu; + ctx->ring_ctx->va_handle =NULL ;// VA management is caller's responsibility + +igt_info ("Using external BO: GPU=0x%llx, CPU=%p\n" , + (unsigned long long )external_bo_mc, external_bo_cpu); + }else { +/* Allocate internal BO for command operations */ + r =amdgpu_bo_alloc_and_map (device, + ctx->ring_ctx->write_length *sizeof (uint32_t ), +4096 , +AMDGPU_GEM_DOMAIN_GTT , +0 , + &ctx->ring_ctx->bo, + &cpu_ptr, + &bo_mc, + &va_handle); +if (r) { + goto cleanup_error; + } + + ctx->ring_ctx->bo_mc = bo_mc; + ctx->ring_ctx->bo_cpu = (volatile uint32_t *)cpu_ptr; + ctx->ring_ctx->va_handle = va_handle; + +igt_info ("Allocated internal BO: GPU=0x%llx, CPU=%p\n" , + (unsigned long long )bo_mc, cpu_ptr); + } + +/* Initialize resources array */ + ctx->ring_ctx->resources[0 ] = ctx->ring_ctx->bo; +for (int i =1 ; i <4 ; i++) { + ctx->ring_ctx->resources[i] =NULL ; + } + + ctx->initialized = true; +return ctx; + +cleanup_error: +/* Cleanup resources in case of allocation failure */ +if (user_queue) { + ctx->ip_block->funcs->userq_destroy (device, ctx->ring_ctx, (unsigned int )ip_type); + }else { +if (ctx->ring_ctx->context_handle) +amdgpu_cs_ctx_free (ctx->ring_ctx->context_handle); + } +free (ctx->ring_ctx->pm4); +free (ctx->ring_ctx); +free (ctx); +return NULL ; +} + +/** +* context destruction function with external BO control +* +* @param ctx Command context to destroy +* @param destroy_external_bo Whether to destroy external BO (if used) +*/ +void cmd_context_destroy (cmd_context_t *ctx,bool destroy_external_bo) +{ +if (!ctx || !ctx->initialized) +return ; + +if (ctx->ring_ctx) { +/* Handle BO cleanup based on allocation type */ +if (ctx->ring_ctx->bo) { +if (!ctx->uses_external_bo || destroy_external_bo) { +/* Clean up internal BO or external BO if explicitly requested */ +if (ctx->ring_ctx->va_handle) { +/* Internal BO with VA handle - full cleanup */ +amdgpu_bo_unmap_and_free (ctx->ring_ctx->bo, ctx->ring_ctx->va_handle, + ctx->ring_ctx->bo_mc, + ctx->ring_ctx->write_length *sizeof (uint32_t )); + }else if (destroy_external_bo) { +/* External BO without VA handle - just free the BO */ +amdgpu_bo_free (ctx->ring_ctx->bo); + } + }else { +/* External BO, preserve it as requested by caller */ +igt_info ("Preserving external BO (GPU=0x%llx)\n" , + (unsigned long long )ctx->ring_ctx->bo_mc); + } + } + +/* Clean up command submission context */ +if (ctx->user_queue) { +if (ctx->ip_block && ctx->ip_block->funcs->userq_destroy) { + ctx->ip_block->funcs->userq_destroy (ctx->device, ctx->ring_ctx, + (unsigned int )ctx->ip_type); + } + }else { +if (ctx->ring_ctx->context_handle) { +amdgpu_cs_ctx_free (ctx->ring_ctx->context_handle); + } + } + +/* Free PM4 command buffer */ +if (ctx->ring_ctx->pm4) { +free (ctx->ring_ctx->pm4); + } + +free (ctx->ring_ctx); + } + +free (ctx); +} + +/** +* Submit command packet without waiting - Fixed submission logic +*/ +int cmd_submit_packet (cmd_context_t *ctx) +{ +amdgpu_bo_list_handle bo_list; +int r; +struct amdgpu_cs_request ibs_request; +struct amdgpu_cs_ib_info ib_info; +amdgpu_bo_handle ib_bo; +void *ib_cpu; +uint64_t ib_mc_address; +amdgpu_va_handle ib_va_handle; +amdgpu_bo_handle all_res[5 ] = {0 }; +uint32_t res_count =0 ; +uint32_t i; + +if (!ctx || !ctx->initialized || !ctx->ring_ctx) { +igt_debug ("Invalid context parameters\n" ); +return -EINVAL ; + } + +/* Check required parameters */ +if (ctx->ring_ctx->pm4_dw ==0 || ctx->ring_ctx->pm4_dw >1024 ) { +igt_debug ("Invalid PM4 size: %u (must be 1-1024)\n" , ctx->ring_ctx->pm4_dw); +return -EINVAL ; + } + +/* For user queues, use userq_submit */ +if (ctx->user_queue) { +if (!ctx->ip_block || !ctx->ip_block->funcs->userq_submit) { +igt_debug ("User queue submission not supported\n" ); +return -ENOTSUP ; + } + +if (!ctx->ring_ctx->bo_mc) { +igt_debug ("Invalid BO MC address for user queue\n" ); +return -EINVAL ; + } + + ctx->ip_block->funcs->userq_submit (ctx->device, ctx->ring_ctx, + (unsigned int )ctx->ip_type, + ctx->ring_ctx->bo_mc); + ctx->last_submit_seq = ctx->ring_ctx->point; +igt_info ("User queue submission successful, point=%lu\n" , ctx->last_submit_seq); +return 0 ; + } + +/* For regular queues, setup and submit CS */ +memset (&ibs_request,0 ,sizeof (ibs_request)); +memset (&ib_info,0 ,sizeof (ib_info)); + +/* Allocate separate IB buffer instead of reusing command buffer */ + r =amdgpu_bo_alloc_and_map (ctx->device, + ctx->ring_ctx->pm4_dw *sizeof (uint32_t ), +4096 , +AMDGPU_GEM_DOMAIN_GTT , +0 , + &ib_bo, + &ib_cpu, + &ib_mc_address, + &ib_va_handle); +if (r) { +igt_debug ("Failed to allocate IB: %d\n" , r); +return r; + } + +/* Copy PM4 commands to IB */ +memcpy (ib_cpu, ctx->ring_ctx->pm4, ctx->ring_ctx->pm4_dw *sizeof (uint32_t )); + +/* Setup IB information */ + ib_info.ib_mc_address = ib_mc_address; + ib_info.size = ctx->ring_ctx->pm4_dw; +if (ctx->ring_ctx->secure) + ib_info.flags |=AMDGPU_IB_FLAGS_SECURE ; + +/* Setup submission request */ + ibs_request.ip_type = (unsigned int )ctx->ip_type; + ibs_request.ring = ctx->ring_ctx->ring_id; + ibs_request.number_of_ibs =1 ; + ibs_request.ibs = &ib_info; + ibs_request.fence_info.handle =NULL ; + +/* Create resource list containing both original resources and IB */ +/* Add original resources */ +for (i =0 ; i < ctx->ring_ctx->res_cnt && i <4 ; i++) { +if (ctx->ring_ctx->resources[i]) { + all_res[res_count++] = ctx->ring_ctx->resources[i]; + } + } + +/* Add IB as the last resource */ + all_res[res_count++] = ib_bo; + +/* Create BO list */ + r =amdgpu_bo_list_create (ctx->device, res_count, all_res,NULL , &bo_list); +if (r) { +igt_debug ("amdgpu_bo_list_create failed: %d\n" , r); +amdgpu_bo_unmap_and_free (ib_bo, ib_va_handle, ib_mc_address, + ctx->ring_ctx->pm4_dw *sizeof (uint32_t )); +return r; + } + + ibs_request.resources = bo_list; + +/* Submit command - strict error checking */ + r =amdgpu_cs_submit (ctx->ring_ctx->context_handle,0 , &ibs_request,1 ); + +if (r !=0 ) { +igt_debug ("amdgpu_cs_submit failed: %d\n" , r); +amdgpu_bo_list_destroy (bo_list); +amdgpu_bo_unmap_and_free (ib_bo, ib_va_handle, ib_mc_address, + ctx->ring_ctx->pm4_dw *sizeof (uint32_t )); +return r; + } + +/* Record submission sequence number for waiting */ + ctx->last_submit_seq = ibs_request.seq_no; +igt_debug ("Command submitted successfully, seq_no=%lu\n" , ctx->last_submit_seq); + +/* Cleanup resources */ +amdgpu_bo_list_destroy (bo_list); +amdgpu_bo_unmap_and_free (ib_bo, ib_va_handle, ib_mc_address, + ctx->ring_ctx->pm4_dw *sizeof (uint32_t )); + +return 0 ; +} + +int cmd_place_packet (cmd_context_t *ctx,const cmd_packet_params_t *params) +{ +int result = -EINVAL ; + +if (!ctx || !ctx->initialized || !ctx->ip_block || !params) +return -EINVAL ; + +if (!ctx->ring_ctx || !ctx->ring_ctx->bo_cpu) +return -EINVAL ; + +/* Clear the buffer before operation */ +memset ((void *)ctx->ring_ctx->bo_cpu,0 , + ctx->ring_ctx->write_length *sizeof (uint32_t )); + + +/* Build the command packet based on type */ +switch (params->type) { +case CMD_PACKET_WRITE_LINEAR : +if (!ctx->ip_block->funcs->write_linear) +return -ENOTSUP ; + +/* TODO: allow user set the dst and data */ +//ctx->ring_ctx->bo_mc = params->dst_addr; + ctx->ring_ctx->write_length = params->size /sizeof (uint32_t ); + +/* Build PM4 packet */ + result = ctx->ip_block->funcs->write_linear (ctx->ip_block->funcs, + ctx->ring_ctx, + &ctx->ring_ctx->pm4_dw); + +/* Copy PM4 packet to ring buffer */ +if (result ==0 && ctx->ring_ctx->pm4_dw >0 ) { +memcpy ((void *)ctx->ring_ctx->bo_cpu, + ctx->ring_ctx->pm4, + ctx->ring_ctx->pm4_dw *sizeof (uint32_t )); + +//igt_info("cmd_place_packet: Copied %u DWORDs to ring buffer\n", +// ctx->ring_ctx->pm4_dw); + } +return result; + +case CMD_PACKET_WRITE_ATOMIC : +if (!ctx->ip_block->funcs->write_linear_atomic) +return -ENOTSUP ; + +/* TODO: allow user set the dst and data */ +//ctx->ring_ctx->bo_mc = params->dst_addr; + ctx->ring_ctx->write_length =1 ;/* Atomic operations typically work on single DWORD */ + + result = ctx->ip_block->funcs->write_linear_atomic (ctx->ip_block->funcs, + ctx->ring_ctx, + &ctx->ring_ctx->pm4_dw); + +/* Copy PM4 packet to ring buffer */ +if (result ==0 && ctx->ring_ctx->pm4_dw >0 ) { +memcpy ((void *)ctx->ring_ctx->bo_cpu, + ctx->ring_ctx->pm4, + ctx->ring_ctx->pm4_dw *sizeof (uint32_t )); + +//igt_info("cmd_place_packet: Copied %u DWORDs to ring buffer (atomic)\n", +// ctx->ring_ctx->pm4_dw); + } +return result; + +case CMD_PACKET_COPY_LINEAR : +if (!ctx->ip_block->funcs->copy_linear) +return -ENOTSUP ; + +/* For copy operations, we need both source and destination addresses */ +/* This would require extending the API to pass both addresses */ +/* For now, use the internal buffer as source */ + ctx->ring_ctx->write_length = params->size /sizeof (uint32_t ); + + result = ctx->ip_block->funcs->copy_linear (ctx->ip_block->funcs, + ctx->ring_ctx, + &ctx->ring_ctx->pm4_dw); + +/* Copy PM4 packet to ring buffer */ +if (result ==0 && ctx->ring_ctx->pm4_dw >0 ) { +memcpy ((void *)ctx->ring_ctx->bo_cpu, + ctx->ring_ctx->pm4, + ctx->ring_ctx->pm4_dw *sizeof (uint32_t )); + +//igt_info("cmd_place_packet: Copied %u DWORDs to ring buffer (copy)\n", +// ctx->ring_ctx->pm4_dw); + } +return result; + +case CMD_PACKET_COPY_ATOMIC : +/* TODO: Implement atomic copy if supported */ +igt_info ("cmd_place_packet: ATOMIC_COPY not implemented\n" ); +return -ENOTSUP ; + +case CMD_PACKET_FENCE : +/* TODO: Implement fence operation */ +igt_info ("cmd_place_packet: FENCE not implemented\n" ); +return -ENOTSUP ; + +case CMD_PACKET_TIMESTAMP : +/* TODO: Implement timestamp operation */ +igt_info ("cmd_place_packet: TIMESTAMP not implemented\n" ); +return -ENOTSUP ; + +default : +igt_info ("cmd_place_packet: Unknown packet type: %d\n" , params->type); +return -EINVAL ; + } +} + +/** +* Build and submit packet in one operation +*/ +int cmd_place_and_submit_packet (cmd_context_t *ctx,const cmd_packet_params_t *params) +{ +int r; + + r =cmd_place_packet (ctx, params); +if (r) +return r; + +return cmd_submit_packet (ctx); +} + +/** +* Convenience function for write linear +*/ +int cmd_submit_write_linear (cmd_context_t *ctx,uint64_t dst_addr,uint32_t size,uint32_t data) +{ +cmd_packet_params_t params = { + .type =CMD_PACKET_WRITE_LINEAR , + .dst_addr = dst_addr, + .size = size, + .data = data, + }; + +return cmd_place_and_submit_packet (ctx, ¶ms); +} + +/** +* Convenience function for copy linear +*/ +int cmd_submit_copy_linear (cmd_context_t *ctx,uint64_t src_addr,uint64_t dst_addr,uint32_t size) +{ +cmd_packet_params_t params = { + .type =CMD_PACKET_COPY_LINEAR , + .src_addr = src_addr, + .dst_addr = dst_addr, + .size = size, + }; + +return cmd_place_and_submit_packet (ctx, ¶ms); +} + +/** +* Convenience function for atomic operation +*/ +int cmd_submit_atomic (cmd_context_t *ctx,uint64_t dst_addr,uint32_t data) +{ +cmd_packet_params_t params = { + .type =CMD_PACKET_WRITE_ATOMIC , + .dst_addr = dst_addr, + .data = data, + }; + +return cmd_place_and_submit_packet (ctx, ¶ms); +} + +/** +* Wait for all submitted packets to complete (equivalent to Wait4PacketConsumption) +*/ +int cmd_wait_completion (cmd_context_t *ctx) +{ +struct amdgpu_cs_fence fence_status = {0 }; +uint32_t expired; + +if (!ctx || !ctx->initialized) +return -EINVAL ; + +/* For user queues, wait on timeline syncobj */ +if (ctx->user_queue) { +if (!ctx->ring_ctx->timeline_syncobj_handle) +return -EINVAL ; + +return amdgpu_timeline_syncobj_wait (ctx->device, + ctx->ring_ctx->timeline_syncobj_handle, + ctx->ring_ctx->point); + } + +/* For regular queues, wait on fence */ + fence_status.ip_type = (unsigned int )ctx->ip_type; + fence_status.ip_instance =0 ; + fence_status.ring = ctx->ring_ctx->ring_id; + fence_status.context = ctx->ring_ctx->context_handle; + fence_status.fence = ctx->last_submit_seq; + +return amdgpu_cs_query_fence_status (&fence_status, +AMDGPU_TIMEOUT_INFINITE , +0 , &expired); +} + +/** +* Check if ring is available +*/ +bool cmd_ring_available (amdgpu_device_handle device, +enum amd_ip_block_type ip_type, +uint32_t ring_id, +bool user_queue) +{ +struct drm_amdgpu_info_hw_ip hw_ip_info; +int r; + + r =amdgpu_query_hw_ip_info (device, (unsigned int )ip_type,0 , &hw_ip_info); +if (r) +return false; + +if (user_queue) { +/* For user queues, check user queue slots */ +uint32_t userq_slots = hw_ip_info.num_userq_slots; + +return (ring_id < userq_slots); + }else { +/* For regular queues, check available rings */ +return !!(hw_ip_info.available_rings & (1ULL << ring_id)); + } +} + +/** +* Get number of available rings +*/ +uint32_t cmd_get_available_rings (amdgpu_device_handle device, +enum amd_ip_block_type ip_type, +bool user_queue) +{ +struct drm_amdgpu_info_hw_ip hw_ip_info; +int r; + + r =amdgpu_query_hw_ip_info (device, (unsigned int )ip_type,0 , &hw_ip_info); +if (r) +return 0 ; + +if (user_queue) { +return hw_ip_info.num_userq_slots; + }else { +return __builtin_popcountll (hw_ip_info.available_rings); + } +}lib/amdgpu/amd_command_submission.h +72
-0@@ -28,6 +28,45 @@#include "amd_ip_blocks.h" +/** +* Packet types supported by the command submission library +*/ +typedef enum { +CMD_PACKET_WRITE_LINEAR , +CMD_PACKET_WRITE_ATOMIC , +CMD_PACKET_COPY_LINEAR , +CMD_PACKET_COPY_ATOMIC , +CMD_PACKET_FENCE , +CMD_PACKET_TIMESTAMP , +}cmd_packet_type_t ; + +/** +* Command submission context structure +*/ +typedef struct { +amdgpu_device_handle device; +struct amdgpu_ring_context *ring_ctx; +const struct amdgpu_ip_block_version *ip_block; +enum amd_ip_block_type ip_type; +bool initialized; +bool user_queue; +bool uses_external_bo; +uint64_t last_submit_seq;/* Last submission sequence number */ +}cmd_context_t ; + +/** +* Packet building parameters +*/ +typedef struct { +cmd_packet_type_t type; +uint64_t src_addr; +uint64_t dst_addr; +uint32_t size; +uint32_t data; +uint32_t *custom_data; +uint32_t custom_data_size; +}cmd_packet_params_t ; +int amdgpu_test_exec_cs_helper (amdgpu_device_handle device,unsigned int ip_type,struct amdgpu_ring_context *ring_context,int expect);@@ -49,4 +88,37 @@ void amdgpu_command_submission_copy_linear_helper(amdgpu_device_handle device,void amdgpu_command_ce_write_fence (amdgpu_device_handle dev,amdgpu_context_handle ctx); + +cmd_context_t *cmd_context_create (amdgpu_device_handle device, +enum amd_ip_block_type ip_type, +uint32_t ring_id, +bool user_queue, +uint32_t write_length, +amdgpu_bo_handle external_bo, +uint64_t external_bo_mc, +volatile uint32_t *external_bo_cpu); +void cmd_context_destroy (cmd_context_t *ctx,bool destroy_external_bo); + +int cmd_place_packet (cmd_context_t *ctx,const cmd_packet_params_t *params); + +int cmd_submit_packet (cmd_context_t *ctx); + +int cmd_place_and_submit_packet (cmd_context_t *ctx,const cmd_packet_params_t *params); + +int cmd_wait_completion (cmd_context_t *ctx); + +int cmd_submit_write_linear (cmd_context_t *ctx,uint64_t dst_addr,uint32_t size,uint32_t data); + +int cmd_submit_copy_linear (cmd_context_t *ctx,uint64_t src_addr,uint64_t dst_addr,uint32_t size); + +int cmd_submit_atomic (cmd_context_t *ctx,uint64_t dst_addr,uint32_t data); + +bool cmd_ring_available (amdgpu_device_handle device, +enum amd_ip_block_type ip_type, +uint32_t ring_id, +bool user_queue); + +uint32_t cmd_get_available_rings (amdgpu_device_handle device, +enum amd_ip_block_type ip_type, +bool user_queue);#endif