Skip to content

Commit

Permalink
rocr: Remove KMT usage from CPU agent
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
atgutier authored and dayatsin-amd committed Feb 21, 2025
1 parent 20e6c87 commit a9f6bc8
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 40 deletions.
22 changes: 19 additions & 3 deletions runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<HsaMemoryProperties>& 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<HsaCacheProperties>& 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;
}

Expand Down
11 changes: 8 additions & 3 deletions runtime/hsa-runtime/core/driver/xdna/amd_xdna_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<HsaMemoryProperties>& mem_props) const {
return HSA_STATUS_SUCCESS;
}

hsa_status_t XdnaDriver::GetCacheProperties(uint32_t node_id, uint32_t processor_id,
std::vector<HsaCacheProperties>& 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,
Expand Down
20 changes: 9 additions & 11 deletions runtime/hsa-runtime/core/inc/amd_cpu_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -47,8 +47,6 @@

#include <vector>

#include "hsakmt/hsakmt.h"

#include "core/inc/runtime.h"
#include "core/inc/agent.h"
#include "core/inc/queue.h"
Expand Down
18 changes: 15 additions & 3 deletions runtime/hsa-runtime/core/inc/amd_kfd_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<core::Driver>& driver);

hsa_status_t Init() override;
Expand All @@ -77,9 +88,10 @@ class KfdDriver final : public core::Driver {
hsa_status_t GetEdgeProperties(std::vector<HsaIoLinkProperties>& 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<HsaMemoryProperties>& mem_props) const override;
hsa_status_t GetCacheProperties(uint32_t node_id, uint32_t processor_id,
std::vector<HsaCacheProperties>& cache_props) const override;
hsa_status_t AllocateMemory(const core::MemoryRegion &mem_region,
core::MemoryRegion::AllocateFlags alloc_flags,
void **mem, size_t size,
Expand Down
7 changes: 4 additions & 3 deletions runtime/hsa-runtime/core/inc/amd_xdna_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ class XdnaDriver final : public core::Driver {
hsa_status_t GetEdgeProperties(std::vector<HsaIoLinkProperties>& 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<HsaMemoryProperties>& mem_props) const override;
hsa_status_t GetCacheProperties(uint32_t node_id, uint32_t processor_id,
std::vector<HsaCacheProperties>& cache_props) const override;
hsa_status_t AllocateMemory(const core::MemoryRegion &mem_region,
core::MemoryRegion::AllocateFlags alloc_flags,
void **mem, size_t size,
Expand Down
25 changes: 14 additions & 11 deletions runtime/hsa-runtime/core/inc/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ class Driver {

/// @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<HsaIoLinkProperties>& io_link_props,
uint32_t node_id) const = 0;
Expand All @@ -118,22 +117,26 @@ class Driver {
/// 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<HsaMemoryProperties>& 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<HsaCacheProperties>& 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,
Expand Down Expand Up @@ -170,7 +173,7 @@ class Driver {
/// @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;
Expand Down
10 changes: 4 additions & 6 deletions runtime/hsa-runtime/core/runtime/amd_cpu_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//
Expand Down Expand Up @@ -73,8 +73,7 @@ void CpuAgent::InitRegionList() {
const bool is_apu_node = (properties_.NumFComputeCores > 0);

std::vector<HsaMemoryProperties> 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<HsaMemoryProperties>::iterator system_prop =
std::find_if(mem_props.begin(), mem_props.end(), [](HsaMemoryProperties prop) -> bool {
return (prop.SizeInBytes > 0 && prop.HeapType == HSA_HEAPTYPE_SYSTEM);
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit a9f6bc8

Please sign in to comment.