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: c42ff44a6a]
This commit is contained in:
Tony Gutierrez
2024-08-19 15:44:50 +00:00
parent c75f2d749d
commit 6e02f63333
6 changed files with 172 additions and 2 deletions
@@ -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
@@ -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 <sys/ioctl.h>
#include <memory>
#include <string>
#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<Driver> 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
@@ -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 <string>
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
@@ -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.
///
@@ -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"
@@ -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);
}