From 727159b4dbfd839cfcda1ea81c31bf3fc96bae09 Mon Sep 17 00:00:00 2001 From: Tony Gutierrez Date: Tue, 4 Feb 2025 10:24:10 -0800 Subject: [PATCH] rocr: Remove KMT usage from CPU agent Use the core Driver object in the CPU agent to make it OS/driver agnostic. Implement the GetMemoryProperties() and GetCacheProperties methods for the KFD driver. [ROCm/ROCR-Runtime commit: a9f6bc8d0e8a8774c7bca42b154b9d1b49551a05] --- .../core/driver/kfd/amd_kfd_driver.cpp | 22 +++++++++++++--- .../core/driver/xdna/amd_xdna_driver.cpp | 11 +++++--- .../hsa-runtime/core/inc/amd_cpu_agent.h | 20 +++++++-------- .../hsa-runtime/core/inc/amd_kfd_driver.h | 18 ++++++++++--- .../hsa-runtime/core/inc/amd_xdna_driver.h | 7 +++--- .../runtime/hsa-runtime/core/inc/driver.h | 25 +++++++++++-------- .../core/runtime/amd_cpu_agent.cpp | 10 +++----- 7 files changed, 73 insertions(+), 40 deletions(-) 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 index 248a79174d..83b83c977d 100644 --- 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 @@ -178,9 +178,25 @@ hsa_status_t KfdDriver::GetAgentProperties(core::Agent &agent) const { return HSA_STATUS_SUCCESS; } -hsa_status_t -KfdDriver::GetMemoryProperties(uint32_t node_id, - core::MemoryRegion &mem_region) const { +hsa_status_t KfdDriver::GetMemoryProperties(uint32_t node_id, + std::vector& mem_props) const { + if (!mem_props.data()) return HSA_STATUS_ERROR_INVALID_ARGUMENT; + + if (hsaKmtGetNodeMemoryProperties(node_id, mem_props.size(), mem_props.data()) != + HSAKMT_STATUS_SUCCESS) + return HSA_STATUS_ERROR; + + return HSA_STATUS_SUCCESS; +} + +hsa_status_t KfdDriver::GetCacheProperties(uint32_t node_id, uint32_t processor_id, + std::vector& cache_props) const { + if (!cache_props.data()) return HSA_STATUS_ERROR_INVALID_ARGUMENT; + + if (hsaKmtGetNodeCacheProperties(node_id, processor_id, cache_props.size(), cache_props.data()) != + HSAKMT_STATUS_SUCCESS) + return HSA_STATUS_ERROR; + return HSA_STATUS_SUCCESS; } diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/driver/xdna/amd_xdna_driver.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/driver/xdna/amd_xdna_driver.cpp index 60325b7f53..f802193516 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/driver/xdna/amd_xdna_driver.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/driver/xdna/amd_xdna_driver.cpp @@ -175,12 +175,17 @@ hsa_status_t XdnaDriver::GetAgentProperties(core::Agent &agent) const { return HSA_STATUS_SUCCESS; } -hsa_status_t -XdnaDriver::GetMemoryProperties(uint32_t node_id, - core::MemoryRegion &mem_region) const { +hsa_status_t XdnaDriver::GetMemoryProperties(uint32_t node_id, + std::vector& mem_props) const { return HSA_STATUS_SUCCESS; } +hsa_status_t XdnaDriver::GetCacheProperties(uint32_t node_id, uint32_t processor_id, + std::vector& cache_props) const { + // AIE currently has no caches. + return HSA_STATUS_ERROR_INVALID_CACHE; +} + hsa_status_t XdnaDriver::AllocateMemory(const core::MemoryRegion &mem_region, core::MemoryRegion::AllocateFlags alloc_flags, diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_cpu_agent.h b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_cpu_agent.h index 4ee5fa3aee..21fb66b2d7 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_cpu_agent.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_cpu_agent.h @@ -2,24 +2,24 @@ // // The University of Illinois/NCSA // Open Source License (NCSA) -// -// Copyright (c) 2014-2020, Advanced Micro Devices, Inc. All rights reserved. -// +// +// Copyright (c) 2014-2025, 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 @@ -29,7 +29,7 @@ // 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 @@ -47,8 +47,6 @@ #include -#include "hsakmt/hsakmt.h" - #include "core/inc/runtime.h" #include "core/inc/agent.h" #include "core/inc/queue.h" 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 index 35d2b23a17..256805fa8e 100644 --- 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 @@ -61,10 +61,21 @@ class Queue; namespace AMD { +/// @brief AMD Kernel Fusion Driver (KFD) for AMD GPU and CPU agents. +/// +/// @details The user-mode driver into the Linux KFD for AMD GPU and CPU HSA +/// agents. Provides APIs for the ROCr core to discover the topology produced +/// by the KFD, allocate memory out of the KFD, manage DMA bufs, allocate queues, +/// and more. class KfdDriver final : public core::Driver { public: KfdDriver(std::string devnode_name); + /// @brief Determine of the KFD is present on the system and attemp to open it if found. + /// + /// @param[out] Driver object for the KFD. + /// @return HSA_STATUS_SUCCESS if driver found and opened. + /// @return HSA_STATUS_ERROR if unable to find or open the KFD. static hsa_status_t DiscoverDriver(std::unique_ptr& driver); hsa_status_t Init() override; @@ -77,9 +88,10 @@ public: hsa_status_t GetEdgeProperties(std::vector& io_link_props, uint32_t node_id) const override; hsa_status_t GetAgentProperties(core::Agent &agent) const override; - hsa_status_t - GetMemoryProperties(uint32_t node_id, - core::MemoryRegion &mem_region) const override; + hsa_status_t GetMemoryProperties(uint32_t node_id, + std::vector& mem_props) const override; + hsa_status_t GetCacheProperties(uint32_t node_id, uint32_t processor_id, + std::vector& cache_props) const override; hsa_status_t AllocateMemory(const core::MemoryRegion &mem_region, core::MemoryRegion::AllocateFlags alloc_flags, void **mem, size_t size, diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_xdna_driver.h b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_xdna_driver.h index 080bfc5202..6726c5aba1 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_xdna_driver.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_xdna_driver.h @@ -153,9 +153,10 @@ public: hsa_status_t GetEdgeProperties(std::vector& io_link_props, uint32_t node_id) const override; hsa_status_t GetAgentProperties(core::Agent &agent) const override; - hsa_status_t - GetMemoryProperties(uint32_t node_id, - core::MemoryRegion &mem_region) const override; + hsa_status_t GetMemoryProperties(uint32_t node_id, + std::vector& mem_props) const override; + hsa_status_t GetCacheProperties(uint32_t node_id, uint32_t processor_id, + std::vector& cache_props) const override; hsa_status_t AllocateMemory(const core::MemoryRegion &mem_region, core::MemoryRegion::AllocateFlags alloc_flags, void **mem, size_t size, 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 8a56060f7e..024b33a5c0 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/driver.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/driver.h @@ -108,8 +108,7 @@ public: /// @brief Get the edge (IO link) properties of a specific node (that is /// managed by this driver) in the topology graph. - /// @param[out] io_link_props IO link properties of the node specified by \p - /// node_id. + /// @param[out] io_link_props IO link properties of the node specified by @p node_id. /// @param[in] node_id ID of the node whose link properties are being queried. virtual hsa_status_t GetEdgeProperties(std::vector& io_link_props, uint32_t node_id) const = 0; @@ -118,22 +117,26 @@ public: /// object. /// @param agent Agent whose properties we're getting. /// @retval HSA_STATUS_SUCCESS if the driver successfully returns the agent's - /// properties. + /// properties. virtual hsa_status_t GetAgentProperties(Agent &agent) const = 0; /// @brief Get the memory properties of a specific node. - /// @param node_id Node ID of the agent - /// @param[in, out] mem_region MemoryRegion object whose properties will be - /// retrieved. + /// @param[in] node_id Node ID of the agent. + /// @param[out] mem_props Memory properties of the node specified by @p node_id. /// @retval HSA_STATUS_SUCCESS if the driver sucessfully returns the node's - /// memory properties. + /// memory properties. virtual hsa_status_t GetMemoryProperties(uint32_t node_id, - MemoryRegion &mem_region) const = 0; + std::vector& mem_props) const = 0; + + /// @brief Get the cache properties of a specific node. + /// @param[in] node_ide Node ID of the agent. + /// @param[out] cache_props Cache properties of the node specified by @p node_id. + /// @retval HSA_STATUS_SUCCESS if the driver successfully returns the node's cache properties. + virtual hsa_status_t GetCacheProperties(uint32_t node_id, uint32_t processor_id, + std::vector& cache_props) const = 0; /// @brief Allocate agent-accessible memory (system or agent-local memory). - /// /// @param[out] mem pointer to newly allocated memory. - /// /// @retval HSA_STATUS_SUCCESS if memory was successfully allocated or /// hsa_status_t error code if the memory allocation failed. virtual hsa_status_t AllocateMemory(const MemoryRegion &mem_region, @@ -170,7 +173,7 @@ public: /// @param[in] mem virtual address associated with the handle /// @param[in] offset memory offset in bytes /// @param[in] size memory size in bytes - /// @param[perms] perms new permissions + /// @param[out] perms new permissions virtual hsa_status_t Map(core::ShareableHandle handle, void *mem, size_t offset, size_t size, hsa_access_permission_t perms) = 0; diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_cpu_agent.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_cpu_agent.cpp index 0faba4377e..8486abe24e 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_cpu_agent.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_cpu_agent.cpp @@ -3,7 +3,7 @@ // The University of Illinois/NCSA // Open Source License (NCSA) // -// Copyright (c) 2014-2020, Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2014-2025, Advanced Micro Devices, Inc. All rights reserved. // // Developed by: // @@ -73,8 +73,7 @@ void CpuAgent::InitRegionList() { const bool is_apu_node = (properties_.NumFComputeCores > 0); std::vector mem_props(properties_.NumMemoryBanks); - if (HSAKMT_STATUS_SUCCESS == - hsaKmtGetNodeMemoryProperties(node_id(), properties_.NumMemoryBanks, &mem_props[0])) { + if (HSA_STATUS_SUCCESS == driver().GetMemoryProperties(node_id(), mem_props)) { std::vector::iterator system_prop = std::find_if(mem_props.begin(), mem_props.end(), [](HsaMemoryProperties prop) -> bool { return (prop.SizeInBytes > 0 && prop.HeapType == HSA_HEAPTYPE_SYSTEM); @@ -107,9 +106,8 @@ void CpuAgent::InitRegionList() { void CpuAgent::InitCacheList() { // Get CPU cache information. cache_props_.resize(properties_.NumCaches); - if (HSAKMT_STATUS_SUCCESS != - hsaKmtGetNodeCacheProperties(node_id(), properties_.CComputeIdLo, - properties_.NumCaches, &cache_props_[0])) { + if (HSA_STATUS_SUCCESS != + driver().GetCacheProperties(node_id(), properties_.CComputeIdLo, cache_props_)) { cache_props_.clear(); } else { // Only store CPU D-cache.