diff --git a/runtime/hsa-runtime/core/inc/runtime.h b/runtime/hsa-runtime/core/inc/runtime.h index 8f4db07832..cc61166959 100644 --- a/runtime/hsa-runtime/core/inc/runtime.h +++ b/runtime/hsa-runtime/core/inc/runtime.h @@ -415,6 +415,8 @@ class Runtime { KfdVersion_t KfdVersion() const { return kfd_version; } + bool VirtualMemApiSupported() const { return virtual_mem_api_supported_; } + protected: static void AsyncEventsLoop(void*); @@ -662,6 +664,11 @@ class Runtime { std::unique_ptr svm_profile_; + private: + void CheckVirtualMemApiSupport(); + + bool virtual_mem_api_supported_; + // Frees runtime memory when the runtime library is unloaded if safe to do so. // Failure to release the runtime indicates an incorrect application but is // common (example: calls library routines at process exit). diff --git a/runtime/hsa-runtime/core/runtime/runtime.cpp b/runtime/hsa-runtime/core/runtime/runtime.cpp index 9647c3690d..83239b7bbd 100644 --- a/runtime/hsa-runtime/core/runtime/runtime.cpp +++ b/runtime/hsa-runtime/core/runtime/runtime.cpp @@ -48,6 +48,7 @@ #include #include #include +#include #include "core/common/shared.h" #include "core/inc/hsa_ext_interface.h" @@ -702,6 +703,10 @@ hsa_status_t Runtime::GetSystemInfo(hsa_system_info_t attribute, void* value) { *(reinterpret_cast(value)) = false; break; } + case HSA_AMD_SYSTEM_INFO_VIRTUAL_MEM_API_SUPPORTED: { + *((bool*)value) = core::Runtime::runtime_singleton_->VirtualMemApiSupported(); + break; + } default: return HSA_STATUS_ERROR_INVALID_ARGUMENT; } @@ -1462,6 +1467,9 @@ hsa_status_t Runtime::Load() { // Load tools libraries LoadTools(); + // Initialize libdrm helper function + CheckVirtualMemApiSupport(); + // Load svm profiler svm_profile_.reset(new AMD::SvmProfileControl); @@ -1578,6 +1586,35 @@ static std::vector parse_tool_names(std::string tool_names) { return names; } + +static int (*fn_amdgpu_device_get_fd)(HsaAMDGPUDeviceHandle device_handle) = NULL; + +int fn_amdgpu_device_get_fd_nosupport(HsaAMDGPUDeviceHandle device_handle) { + fprintf(stderr, "amdgpu_device_get_fd not available. Please update version of libdrm"); + return -1; +} + +void Runtime::CheckVirtualMemApiSupport() { + virtual_mem_api_supported_ = false; + // TODO: May have to change the minor version required once Thunk merges changes into amd-staging + auto kfd_version = core::Runtime::runtime_singleton_->KfdVersion().version; + + if (kfd_version.KernelInterfaceMajorVersion > 1 || + kfd_version.KernelInterfaceMajorVersion == 1 && + kfd_version.KernelInterfaceMinorVersion >= 12) { + char* error; + + fn_amdgpu_device_get_fd = + (int (*)(HsaAMDGPUDeviceHandle device_handle))dlsym(RTLD_DEFAULT, "amdgpu_device_get_fd"); + if ((error = dlerror()) != NULL) { + debug_warning("amdgpu_device_get_fd not available. Please update version of libdrm"); + fn_amdgpu_device_get_fd = &fn_amdgpu_device_get_fd_nosupport; + } else { + virtual_mem_api_supported_ = true; + } + } +} + void Runtime::LoadTools() { typedef bool (*tool_init_t)(::HsaApiTable*, uint64_t, uint64_t, const char* const*); diff --git a/runtime/hsa-runtime/inc/hsa.h b/runtime/hsa-runtime/inc/hsa.h index a70fd0f061..1dbf2993e7 100644 --- a/runtime/hsa-runtime/inc/hsa.h +++ b/runtime/hsa-runtime/inc/hsa.h @@ -504,7 +504,12 @@ typedef enum { * Returns true if DMABUF APIs are supported by the driver. The type of * this attribute is bool. */ - HSA_AMD_SYSTEM_INFO_DMABUF_SUPPORTED = 0x204 + HSA_AMD_SYSTEM_INFO_DMABUF_SUPPORTED = 0x204, + /** + * Returns true if Virtual Memory APIs are supported by the driver. The type of + * this attribute is bool. + */ + HSA_AMD_SYSTEM_INFO_VIRTUAL_MEM_API_SUPPORTED = 0x205, } hsa_system_info_t; /**