diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp index a4ab518d73..31196835c8 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp @@ -1238,6 +1238,11 @@ hsa_status_t HSA_API hsa_amd_vmem_address_reserve(void** ptr, size_t size, uint6 return amdExtTable->hsa_amd_vmem_address_reserve_fn(ptr, size, address, flags); } +hsa_status_t HSA_API hsa_amd_vmem_address_reserve_align(void** ptr, size_t size, uint64_t address, + uint64_t alignment, uint64_t flags) { + return amdExtTable->hsa_amd_vmem_address_reserve_align_fn(ptr, size, address, alignment, flags); +} + hsa_status_t HSA_API hsa_amd_vmem_address_free(void* ptr, size_t size) { return amdExtTable->hsa_amd_vmem_address_free_fn(ptr, size); } diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/hsa_ext_amd_impl.h b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/hsa_ext_amd_impl.h index f3c0ca851c..e5717b3fae 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/hsa_ext_amd_impl.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/hsa_ext_amd_impl.h @@ -302,6 +302,10 @@ hsa_status_t HSA_API hsa_amd_portable_close_dmabuf(int dmabuf); hsa_status_t hsa_amd_vmem_address_reserve(void** ptr, size_t size, uint64_t address, uint64_t flags); +// Mirrors Amd Extension Apis +hsa_status_t hsa_amd_vmem_address_reserve_align(void** ptr, size_t size, uint64_t address, + uint64_t alignment, uint64_t flags); + // Mirrors Amd Extension Apis hsa_status_t hsa_amd_vmem_address_free(void* ptr, size_t size); diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/runtime.h b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/runtime.h index 6f5997f7e4..3d4d889d8c 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/runtime.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/runtime.h @@ -359,7 +359,7 @@ class Runtime { hsa_status_t DmaBufClose(int dmabuf); - hsa_status_t VMemoryAddressReserve(void** ptr, size_t size, uint64_t address, uint64_t flags); + hsa_status_t VMemoryAddressReserve(void** ptr, size_t size, uint64_t address, uint64_t alignment, uint64_t flags); hsa_status_t VMemoryAddressFree(void* ptr, size_t size); diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_api_trace.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_api_trace.cpp index 031a18d8c0..48dee48585 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_api_trace.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_api_trace.cpp @@ -80,7 +80,7 @@ void HsaApiTable::Init() { // they can add preprocessor macros on the new functions constexpr size_t expected_core_api_table_size = 1016; - constexpr size_t expected_amd_ext_table_size = 568; + constexpr size_t expected_amd_ext_table_size = 576; constexpr size_t expected_image_ext_table_size = 120; constexpr size_t expected_finalizer_ext_table_size = 64; constexpr size_t expected_tools_table_size = 64; @@ -451,6 +451,7 @@ void HsaApiTable::UpdateAmdExts() { amd_ext_api.hsa_amd_portable_export_dmabuf_fn = AMD::hsa_amd_portable_export_dmabuf; amd_ext_api.hsa_amd_portable_close_dmabuf_fn = AMD::hsa_amd_portable_close_dmabuf; amd_ext_api.hsa_amd_vmem_address_reserve_fn = AMD::hsa_amd_vmem_address_reserve; + amd_ext_api.hsa_amd_vmem_address_reserve_align_fn = AMD::hsa_amd_vmem_address_reserve_align; amd_ext_api.hsa_amd_vmem_address_free_fn = AMD::hsa_amd_vmem_address_free; amd_ext_api.hsa_amd_vmem_handle_create_fn = AMD::hsa_amd_vmem_handle_create; amd_ext_api.hsa_amd_vmem_handle_release_fn = AMD::hsa_amd_vmem_handle_release; diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_ext_amd.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_ext_amd.cpp index dbd1286735..ce8d9256c6 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_ext_amd.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_ext_amd.cpp @@ -1231,10 +1231,21 @@ hsa_status_t hsa_amd_vmem_address_reserve(void** va, size_t size, uint64_t addre IS_OPEN(); IS_ZERO(size); IS_TRUE(core::Runtime::runtime_singleton_->VirtualMemApiSupported()); - return core::Runtime::runtime_singleton_->VMemoryAddressReserve(va, size, address, flags); + return core::Runtime::runtime_singleton_->VMemoryAddressReserve(va, size, address, 0, flags); CATCH; } +hsa_status_t hsa_amd_vmem_address_reserve_align(void** va, size_t size, uint64_t address, + uint64_t alignment, uint64_t flags) { + TRY; + IS_OPEN(); + IS_ZERO(size); + IS_TRUE(core::Runtime::runtime_singleton_->VirtualMemApiSupported()); + return core::Runtime::runtime_singleton_->VMemoryAddressReserve(va, size, address, alignment, flags); + CATCH; +} + + hsa_status_t hsa_amd_vmem_address_free(void* va, size_t size) { TRY; IS_OPEN(); diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp index 740fd1c941..9ccef65cc6 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp @@ -2962,18 +2962,23 @@ hsa_status_t Runtime::DmaBufClose(int dmabuf) { } hsa_status_t Runtime::VMemoryAddressReserve(void** va, size_t size, uint64_t address, - uint64_t flags) { + uint64_t alignment, uint64_t flags) { void* addr = (void*)address; HsaMemFlags memFlags = {}; + + if (!alignment) + alignment = sysconf(_SC_PAGE_SIZE); + ScopedAcquire lock(&memory_lock_); memFlags.ui32.OnlyAddress = 1; memFlags.ui32.FixedAddress = 1; + /* Try to reserving the VA requested by user */ - if (hsaKmtAllocMemory(0, size, memFlags, &addr) != HSAKMT_STATUS_SUCCESS) { + if (hsaKmtAllocMemoryAlign(0, size, alignment, memFlags, &addr) != HSAKMT_STATUS_SUCCESS) { memFlags.ui32.FixedAddress = 0; /* Could not reserved VA requested, allocate alternate VA */ - if (hsaKmtAllocMemory(0, size, memFlags, &addr) != HSAKMT_STATUS_SUCCESS) + if (hsaKmtAllocMemoryAlign(0, size, alignment, memFlags, &addr) != HSAKMT_STATUS_SUCCESS) return HSA_STATUS_ERROR_OUT_OF_RESOURCES; } diff --git a/projects/rocr-runtime/runtime/hsa-runtime/hsacore.so.def b/projects/rocr-runtime/runtime/hsa-runtime/hsacore.so.def index 347322a2f7..dd9b554a18 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/hsacore.so.def +++ b/projects/rocr-runtime/runtime/hsa-runtime/hsacore.so.def @@ -234,6 +234,7 @@ global: hsa_amd_portable_export_dmabuf; hsa_amd_portable_close_dmabuf; hsa_amd_vmem_address_reserve; + hsa_amd_vmem_address_reserve_align; hsa_amd_vmem_address_free; hsa_amd_vmem_handle_create; hsa_amd_vmem_handle_release; diff --git a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace.h b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace.h index 070f88b2b1..c7c2884c51 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace.h @@ -250,6 +250,7 @@ struct AmdExtTable { decltype(hsa_amd_portable_export_dmabuf)* hsa_amd_portable_export_dmabuf_fn; decltype(hsa_amd_portable_close_dmabuf)* hsa_amd_portable_close_dmabuf_fn; decltype(hsa_amd_vmem_address_reserve)* hsa_amd_vmem_address_reserve_fn; + decltype(hsa_amd_vmem_address_reserve_align)* hsa_amd_vmem_address_reserve_align_fn; decltype(hsa_amd_vmem_address_free)* hsa_amd_vmem_address_free_fn; decltype(hsa_amd_vmem_handle_create)* hsa_amd_vmem_handle_create_fn; decltype(hsa_amd_vmem_handle_release)* hsa_amd_vmem_handle_release_fn; diff --git a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace_version.h b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace_version.h index 52e4557db3..3393a77620 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace_version.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace_version.h @@ -58,7 +58,7 @@ // Step Ids of the Api tables exported by Hsa Core Runtime #define HSA_API_TABLE_STEP_VERSION 0x01 #define HSA_CORE_API_TABLE_STEP_VERSION 0x00 -#define HSA_AMD_EXT_API_TABLE_STEP_VERSION 0x02 +#define HSA_AMD_EXT_API_TABLE_STEP_VERSION 0x03 #define HSA_FINALIZER_API_TABLE_STEP_VERSION 0x00 #define HSA_IMAGE_API_TABLE_STEP_VERSION 0x00 #define HSA_AQLPROFILE_API_TABLE_STEP_VERSION 0x00 diff --git a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h index 44a75172f5..f9f60edeb9 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h @@ -56,9 +56,10 @@ * - 1.3 - HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_EXTENDED_SCOPE_FINE_GRAINED pool * - 1.4 - Virtual Memory API * - 1.5 - hsa_amd_agent_info: HSA_AMD_AGENT_INFO_MEMORY_PROPERTIES + * - 1.6 - Virtual Memory API: hsa_amd_vmem_address_reserve_align */ #define HSA_AMD_INTERFACE_VERSION_MAJOR 1 -#define HSA_AMD_INTERFACE_VERSION_MINOR 5 +#define HSA_AMD_INTERFACE_VERSION_MINOR 6 #ifdef __cplusplus extern "C" { @@ -2814,10 +2815,38 @@ hsa_status_t hsa_amd_portable_close_dmabuf(int dmabuf); * * @retval ::HSA_STATUS_ERROR_OUT_OF_RESOURCES Insufficient resources to allocate an address * range of this size. + * + * Note that this API will be deprecated in a future release and replaced by + * hsa_amd_vmem_address_reserve_align */ hsa_status_t hsa_amd_vmem_address_reserve(void** va, size_t size, uint64_t address, uint64_t flags); +/** + * @brief Allocate a reserved address range + * + * Reserve a virtual address range. The size must be a multiple of the system page size. + * If it is not possible to allocate the address specified by @p address, then @p va will be + * a different address range. + * Address range should be released by calling hsa_amd_vmem_address_free. + * + * @param[out] va virtual address allocated + * @param[in] size of address range requested + * @param[in] address requested + * @param[in] alignment requested. 0 for default. Must be >= page-size and a power of 2 + * @param[in] flags currently unsupported + * + * @retval ::HSA_STATUS_SUCCESS Address range allocated successfully + * + * @retval ::HSA_STATUS_ERROR_NOT_INITIALIZED The HSA runtime has not been + * initialized. + * + * @retval ::HSA_STATUS_ERROR_OUT_OF_RESOURCES Insufficient resources to allocate an address + * range of this size. + */ +hsa_status_t hsa_amd_vmem_address_reserve_align(void** va, size_t size, uint64_t address, + uint64_t alignment, uint64_t flags); + /** * @brief Free a reserved address range *