From 77fa5af618cc327d696d703ed341e8a7fb85054b Mon Sep 17 00:00:00 2001 From: Tony Gutierrez Date: Fri, 8 Nov 2024 16:40:21 -0800 Subject: [PATCH] rocr: Make Open() and Close() virtual in Driver Change-Id: Iac054c08383b080ca2b2ec6d65019bf2f083b763 --- runtime/hsa-runtime/core/driver/driver.cpp | 25 ------------------- .../core/driver/kfd/amd_kfd_driver.cpp | 15 +++++++++-- .../core/driver/xdna/amd_xdna_driver.cpp | 21 ++++++++++++++++ runtime/hsa-runtime/core/inc/amd_kfd_driver.h | 2 ++ .../hsa-runtime/core/inc/amd_xdna_driver.h | 2 ++ runtime/hsa-runtime/core/inc/driver.h | 4 +-- .../hsa-runtime/core/runtime/amd_topology.cpp | 3 --- 7 files changed, 40 insertions(+), 32 deletions(-) diff --git a/runtime/hsa-runtime/core/driver/driver.cpp b/runtime/hsa-runtime/core/driver/driver.cpp index 3a2f7862f5..96518942b9 100644 --- a/runtime/hsa-runtime/core/driver/driver.cpp +++ b/runtime/hsa-runtime/core/driver/driver.cpp @@ -42,9 +42,6 @@ #include "core/inc/driver.h" -#include -#include - #include "inc/hsa.h" namespace rocr { @@ -54,27 +51,5 @@ Driver::Driver(DriverType kernel_driver_type, std::string devnode_name) : kernel_driver_type_(std::move(kernel_driver_type)), devnode_name_(std::move(devnode_name)) {} -hsa_status_t Driver::Open() -{ - fd_ = open(devnode_name_.c_str(), O_RDWR | O_CLOEXEC); - if (fd_ < 0) { - return HSA_STATUS_ERROR_OUT_OF_RESOURCES; - } - return HSA_STATUS_SUCCESS; -} - -hsa_status_t Driver::Close() -{ - int ret(0); - if (fd_ > 0) { - ret = close(fd_); - fd_ = -1; - } - if (ret) { - return HSA_STATUS_ERROR; - } - return HSA_STATUS_SUCCESS; -} - } // namespace core } // namespace rocr diff --git a/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp b/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp index 5acbe8aad1..ceda2d6aae 100644 --- a/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp +++ b/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp @@ -64,8 +64,9 @@ KfdDriver::KfdDriver(std::string devnode_name) hsa_status_t KfdDriver::Init() { return HSA_STATUS_SUCCESS; } hsa_status_t KfdDriver::DiscoverDriver() { - if (hsaKmtOpenKFD() == HSAKMT_STATUS_SUCCESS) { - std::unique_ptr kfd_drv(new KfdDriver("/dev/kfd")); + std::unique_ptr kfd_drv(new KfdDriver("/dev/kfd")); + + if (kfd_drv->Open() == HSA_STATUS_SUCCESS) { core::Runtime::runtime_singleton_->RegisterDriver(kfd_drv); return HSA_STATUS_SUCCESS; } @@ -76,6 +77,16 @@ hsa_status_t KfdDriver::QueryKernelModeDriver(core::DriverQuery query) { return HSA_STATUS_SUCCESS; } +hsa_status_t KfdDriver::Open() { + return hsaKmtOpenKFD() == HSAKMT_STATUS_SUCCESS ? HSA_STATUS_SUCCESS + : HSA_STATUS_ERROR; +} + +hsa_status_t KfdDriver::Close() { + return hsaKmtCloseKFD() == HSAKMT_STATUS_SUCCESS ? HSA_STATUS_SUCCESS + : HSA_STATUS_ERROR; +} + hsa_status_t KfdDriver::GetAgentProperties(core::Agent &agent) const { return HSA_STATUS_SUCCESS; } diff --git a/runtime/hsa-runtime/core/driver/xdna/amd_xdna_driver.cpp b/runtime/hsa-runtime/core/driver/xdna/amd_xdna_driver.cpp index edee88b730..b684b69f98 100644 --- a/runtime/hsa-runtime/core/driver/xdna/amd_xdna_driver.cpp +++ b/runtime/hsa-runtime/core/driver/xdna/amd_xdna_driver.cpp @@ -42,6 +42,7 @@ #include "core/inc/amd_xdna_driver.h" +#include #include #include @@ -100,6 +101,26 @@ hsa_status_t XdnaDriver::QueryKernelModeDriver(core::DriverQuery query) { return HSA_STATUS_SUCCESS; } +hsa_status_t XdnaDriver::Open() { + fd_ = open(devnode_name_.c_str(), O_RDWR | O_CLOEXEC); + if (fd_ < 0) { + return HSA_STATUS_ERROR_OUT_OF_RESOURCES; + } + return HSA_STATUS_SUCCESS; +} + +hsa_status_t XdnaDriver::Close() { + int ret(0); + if (fd_ > 0) { + ret = close(fd_); + fd_ = -1; + } + if (ret) { + return HSA_STATUS_ERROR; + } + return HSA_STATUS_SUCCESS; +} + hsa_status_t XdnaDriver::GetAgentProperties(core::Agent &agent) const { if (agent.device_type() != core::Agent::DeviceType::kAmdAieDevice) { return HSA_STATUS_ERROR_INVALID_AGENT; diff --git a/runtime/hsa-runtime/core/inc/amd_kfd_driver.h b/runtime/hsa-runtime/core/inc/amd_kfd_driver.h index d8d2af8b7a..ee53995a62 100644 --- a/runtime/hsa-runtime/core/inc/amd_kfd_driver.h +++ b/runtime/hsa-runtime/core/inc/amd_kfd_driver.h @@ -68,6 +68,8 @@ public: hsa_status_t Init() override; hsa_status_t QueryKernelModeDriver(core::DriverQuery query) override; + hsa_status_t Open() override; + hsa_status_t Close() override; hsa_status_t GetAgentProperties(core::Agent &agent) const override; hsa_status_t GetMemoryProperties(uint32_t node_id, diff --git a/runtime/hsa-runtime/core/inc/amd_xdna_driver.h b/runtime/hsa-runtime/core/inc/amd_xdna_driver.h index 4c0cad47fb..feaa0588a0 100644 --- a/runtime/hsa-runtime/core/inc/amd_xdna_driver.h +++ b/runtime/hsa-runtime/core/inc/amd_xdna_driver.h @@ -142,6 +142,8 @@ public: std::unordered_map& GetHandleMappings(); std::unordered_map& GetAddrMappings(); + hsa_status_t Open() override; + hsa_status_t Close() override; hsa_status_t GetAgentProperties(core::Agent &agent) const override; hsa_status_t GetMemoryProperties(uint32_t node_id, diff --git a/runtime/hsa-runtime/core/inc/driver.h b/runtime/hsa-runtime/core/inc/driver.h index bbb70ea694..b621b8d6f7 100644 --- a/runtime/hsa-runtime/core/inc/driver.h +++ b/runtime/hsa-runtime/core/inc/driver.h @@ -84,11 +84,11 @@ class Driver { /// @brief Open a connection to the driver using name_. /// @retval HSA_STATUS_SUCCESS if the driver was opened successfully. - hsa_status_t Open(); + virtual hsa_status_t Open() = 0; /// @brief Close a connection to the open driver using fd_. /// @retval HSA_STATUS_SUCCESS if the driver was opened successfully. - hsa_status_t Close(); + virtual hsa_status_t Close() = 0; /// @brief Get driver version information. /// @retval DriverVersionInfo containing the driver's version information. diff --git a/runtime/hsa-runtime/core/runtime/amd_topology.cpp b/runtime/hsa-runtime/core/runtime/amd_topology.cpp index c1ae81bed6..28856bf9fe 100644 --- a/runtime/hsa-runtime/core/runtime/amd_topology.cpp +++ b/runtime/hsa-runtime/core/runtime/amd_topology.cpp @@ -458,9 +458,6 @@ bool Unload() { hsaKmtReleaseSystemProperties(); - // Close connection to kernel driver. - hsaKmtCloseKFD(); - return true; } } // namespace amd