From 6e02f6333321a8db877382ad3eae5cb3e9e2756f Mon Sep 17 00:00:00 2001 From: Tony Gutierrez Date: Mon, 19 Aug 2024 15:44:50 +0000 Subject: [PATCH] rocr/kfd-driver: Add initial KFD driver interface Adds the initial KFD driver interface and use it to open the KFD from amd_topology.cpp. This change is to show the direction of the Driver interface for initially supporting the KFD and to get feedback on the approach. For now we wrap relevant ROCt calls behind this generic driver interface so that we can generalize core ROCr components like MemoryRegion, Runtime, etc. Now that ROCt is incorporated into ROCr, we can more fully integrate ROCt into the Driver interface. Ideally, we get to a point where the generic Driver interface can support KFD, XDNA, and potential future drivers. Change-Id: I4573fd6af1f8398233ee9d3814d9f3139dd0279c [ROCm/ROCR-Runtime commit: c42ff44a6a8f47606d7ae1e7b10353f00ee537c3] --- .../runtime/hsa-runtime/CMakeLists.txt | 1 + .../core/driver/kfd/amd_kfd_driver.cpp | 96 +++++++++++++++++++ .../hsa-runtime/core/inc/amd_kfd_driver.h | 72 ++++++++++++++ .../runtime/hsa-runtime/core/inc/driver.h | 2 +- .../runtime/hsa-runtime/core/inc/runtime.h | 1 + .../hsa-runtime/core/runtime/amd_topology.cpp | 2 +- 6 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 projects/rocr-runtime/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp create mode 100644 projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_kfd_driver.h diff --git a/projects/rocr-runtime/runtime/hsa-runtime/CMakeLists.txt b/projects/rocr-runtime/runtime/hsa-runtime/CMakeLists.txt index c127e7b1cc..8baa8f54c6 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/CMakeLists.txt +++ b/projects/rocr-runtime/runtime/hsa-runtime/CMakeLists.txt @@ -150,6 +150,7 @@ set_property(TARGET ${CORE_RUNTIME_TARGET} PROPERTY LINK_FLAGS ${HSA_SHARED_LINK ## Source files. set ( SRCS core/driver/driver.cpp + core/driver/kfd/amd_kfd_driver.cpp core/driver/xdna/amd_xdna_driver.cpp core/util/lnx/os_linux.cpp core/util/small_heap.cpp diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp new file mode 100644 index 0000000000..f3e6f05419 --- /dev/null +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp @@ -0,0 +1,96 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// The University of Illinois/NCSA +// Open Source License (NCSA) +// +// Copyright (c) 2024, Advanced Micro Devices, Inc. All rights reserved. +// +// Developed by: +// +// AMD Research and AMD HSA Software Development +// +// Advanced Micro Devices, Inc. +// +// www.amd.com +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal with the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// - Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimers. +// - Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimers in +// the documentation and/or other materials provided with the distribution. +// - Neither the names of Advanced Micro Devices, Inc, +// nor the names of its contributors may be used to endorse or promote +// products derived from this Software without specific prior written +// permission. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS WITH THE SOFTWARE. +// +//////////////////////////////////////////////////////////////////////////////// + +#include "core/inc/amd_kfd_driver.h" + +#include + +#include +#include + +#include "hsakmt/hsakmt.h" + +#include "core/inc/runtime.h" + +namespace rocr { +namespace AMD { + +KfdDriver::KfdDriver(std::string devnode_name) + : core::Driver(core::DriverType::KFD, devnode_name) {} + +hsa_status_t KfdDriver::DiscoverDriver() { + if (hsaKmtOpenKFD() == HSAKMT_STATUS_SUCCESS) { + std::unique_ptr kfd_drv(new KfdDriver("/dev/kfd")); + core::Runtime::runtime_singleton_->RegisterDriver(kfd_drv); + return HSA_STATUS_SUCCESS; + } + return HSA_STATUS_ERROR; +} + +hsa_status_t KfdDriver::QueryKernelModeDriver(core::DriverQuery query) { + return HSA_STATUS_SUCCESS; +} + +hsa_status_t KfdDriver::GetMemoryProperties(uint32_t node_id, + core::MemProperties &mprops) const { + return HSA_STATUS_SUCCESS; +} + +hsa_status_t KfdDriver::AllocateMemory(void **mem, size_t size, + uint32_t node_id, core::MemFlags flags) { + return HSA_STATUS_SUCCESS; +} + +hsa_status_t KfdDriver::FreeMemory(void *mem, uint32_t node_id) { + return HSA_STATUS_SUCCESS; +} + +hsa_status_t KfdDriver::CreateQueue(core::Queue &queue) { + return HSA_STATUS_SUCCESS; +} + +hsa_status_t KfdDriver::DestroyQueue(core::Queue &queue) const { + return HSA_STATUS_SUCCESS; +} + +} // namespace AMD +} // namespace rocr diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_kfd_driver.h b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_kfd_driver.h new file mode 100644 index 0000000000..40fdf91779 --- /dev/null +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_kfd_driver.h @@ -0,0 +1,72 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// The University of Illinois/NCSA +// Open Source License (NCSA) +// +// Copyright (c) 2024, Advanced Micro Devices, Inc. All rights reserved. +// +// Developed by: +// +// AMD Research and AMD HSA Software Development +// +// Advanced Micro Devices, Inc. +// +// www.amd.com +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal with the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// - Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimers. +// - Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimers in +// the documentation and/or other materials provided with the distribution. +// - Neither the names of Advanced Micro Devices, Inc, +// nor the names of its contributors may be used to endorse or promote +// products derived from this Software without specific prior written +// permission. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS WITH THE SOFTWARE. +// +//////////////////////////////////////////////////////////////////////////////// + +#ifndef HSA_RUNTIME_CORE_INC_AMD_KFD_DRIVER_H_ +#define HSA_RUNTIME_CORE_INC_AMD_KFD_DRIVER_H_ + +#include "core/inc/driver.h" + +#include + +namespace rocr { +namespace AMD { + +class KfdDriver : public core::Driver { +public: + KfdDriver(std::string devnode_name); + + static hsa_status_t DiscoverDriver(); + + hsa_status_t QueryKernelModeDriver(core::DriverQuery query) override; + hsa_status_t GetMemoryProperties(uint32_t node_id, + core::MemProperties &mprops) const override; + hsa_status_t AllocateMemory(void **mem, size_t size, uint32_t node_id, + core::MemFlags flags) override; + hsa_status_t FreeMemory(void *mem, uint32_t node_id) override; + hsa_status_t CreateQueue(core::Queue &queue) override; + hsa_status_t DestroyQueue(core::Queue &queue) const override; +}; + +} // namespace AMD +} // namespace rocr + +#endif // header guard diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/driver.h b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/driver.h index 9d6e623c29..22f5f3d0a1 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/driver.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/driver.h @@ -68,7 +68,7 @@ struct DriverVersionInfo { enum class DriverQuery { GET_DRIVER_VERSION }; -enum class DriverType { XDNA = 0, NUM_DRIVER_TYPES }; +enum class DriverType { XDNA = 0, KFD, NUM_DRIVER_TYPES }; /// @brief Kernel driver interface. /// 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 96de57653f..981bd48525 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/runtime.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/runtime.h @@ -63,6 +63,7 @@ #include "core/inc/hsa_ext_amd_impl.h" #include "core/inc/agent.h" +#include "core/inc/amd_kfd_driver.h" #include "core/inc/amd_xdna_driver.h" #include "core/inc/exceptions.h" #include "core/inc/interrupt_signal.h" diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_topology.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_topology.cpp index 4932de9051..bffcc03c9c 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_topology.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_topology.cpp @@ -74,7 +74,7 @@ static const uint kKfdVersionMinor = 99; void DiscoverDrivers(bool &gpu_found, bool &aie_found) { // Open connection to GPU and AIE kernel drivers. - gpu_found = (hsaKmtOpenKFD() == HSAKMT_STATUS_SUCCESS); + gpu_found = (KfdDriver::DiscoverDriver() == HSA_STATUS_SUCCESS); aie_found = (XdnaDriver::DiscoverDriver() == HSA_STATUS_SUCCESS); }