From e65edb35fc0cef2cd1312977e6310ef9c83715f8 Mon Sep 17 00:00:00 2001 From: David Yat Sin Date: Mon, 19 Dec 2022 00:29:17 +0000 Subject: [PATCH] Add check/query for virtual memory API support Checks whether version of libdrm library installed on current system supports the amdgpu_device_get_fd API. This API is required to support the virtual memory API functions. The amdgpu_device_get_fd function was introduced in libdrm-2.4.109. Using a runtime check test instead of static dependency to be able to support previous APIs on older versions of libdrm. Add query for virtual memory API support. This is part of patch series for Virtual Memory API. Change-Id: Iec831eb24b5d1689c392e50ae86f4d52d4870ac4 --- runtime/hsa-runtime/core/inc/runtime.h | 7 ++++ runtime/hsa-runtime/core/runtime/runtime.cpp | 37 ++++++++++++++++++++ runtime/hsa-runtime/inc/hsa.h | 7 +++- 3 files changed, 50 insertions(+), 1 deletion(-) 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; /**