Add hsa_amd_vmem_address_reserve_align API

New API to support alignment parameter when reserving virtual addresses.
If the alignment is 0, then the default size is used. Otherwise the
alignment needs to be a power of 2 and greater than or equal to page
size.

Existing hsa_amd_vmem_address_reserve marked for future deprecation.

Change-Id: I17cee75420183dea5842fc1ecc2514cdcd760bac
Signed-off-by: Chris Freehill <cfreehil@amd.com>


[ROCm/ROCR-Runtime commit: 08c44fbda6]
This commit is contained in:
David Yat Sin
2024-05-13 14:50:38 +00:00
committed by Chris Freehill
vanhempi 2d6be55401
commit 140b5fbd40
10 muutettua tiedostoa jossa 65 lisäystä ja 8 poistoa
@@ -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);
}
@@ -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);
@@ -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);
@@ -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;
@@ -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();
@@ -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<KernelSharedMutex> 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;
}
@@ -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;
@@ -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;
@@ -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
@@ -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
*