diff --git a/tests/kfdtest/src/KFDBaseComponentTest.cpp b/tests/kfdtest/src/KFDBaseComponentTest.cpp index 0a7c1659bd..ff7ce063c2 100644 --- a/tests/kfdtest/src/KFDBaseComponentTest.cpp +++ b/tests/kfdtest/src/KFDBaseComponentTest.cpp @@ -36,6 +36,7 @@ void KFDBaseComponentTest::SetUp() { ASSERT_SUCCESS(hsaKmtOpenKFD()); EXPECT_SUCCESS(hsaKmtGetVersion(&m_VersionInfo)); memset( &m_SystemProperties, 0, sizeof(m_SystemProperties) ); + memset(m_RenderNodes, 0, sizeof(m_RenderNodes)); /** in order to be correctly testing the KFD interfaces and ensure * that the KFD acknowledges relevant node parameters @@ -70,6 +71,14 @@ void KFDBaseComponentTest::SetUp() { void KFDBaseComponentTest::TearDown() { ROUTINE_START + for (int i = 0; i < MAX_RENDER_NODES; i++) { + if (m_RenderNodes[i].fd <= 0) + continue; + + amdgpu_device_deinitialize(m_RenderNodes[i].device_handle); + drmClose(m_RenderNodes[i].fd); + } + ASSERT_SUCCESS(hsaKmtReleaseSystemProperties()); ASSERT_SUCCESS(hsaKmtCloseKFD()); @@ -115,3 +124,52 @@ HSAuint64 KFDBaseComponentTest::GetVramSize(int defaultGPUNode) { return 0; } + +int KFDBaseComponentTest::FindDRMRenderNode(int gpuNode) { + char path[PATH_MAX], buf[PAGE_SIZE]; + + snprintf(path, PATH_MAX, "/sys/class/kfd/kfd/topology/nodes/%d/properties", gpuNode); + + int fd = open(path, O_RDONLY); + + if (fd < 0) { + LOG() << "Failed to open " << path << std::endl; + return -EINVAL; + } + + read(fd, buf, PAGE_SIZE); + + close(fd); + + char *s = strstr(buf, "drm_render_minor"); + + int minor = atoi(s + 17); + + if (minor < 128) { + LOG() << "Failed to get minor number " << minor << std::endl; + return -EINVAL; + } + + int index = minor - 128; + + if (m_RenderNodes[index].fd == 0) { + m_RenderNodes[index].fd = drmOpenRender(minor); + + if (m_RenderNodes[index].fd < 0) { + LOG() << "Failed to open render node" << std::endl; + return -EINVAL; + } + + if (amdgpu_device_initialize(m_RenderNodes[index].fd, + &m_RenderNodes[index].major_version, + &m_RenderNodes[index].minor_version, + &m_RenderNodes[index].device_handle) != 0) { + drmClose(m_RenderNodes[index].fd); + m_RenderNodes[index].fd = 0; + LOG() << "Failed to initialize amdgpu device" << std::endl; + return -EINVAL; + } + } + + return index; +} diff --git a/tests/kfdtest/src/KFDBaseComponentTest.hpp b/tests/kfdtest/src/KFDBaseComponentTest.hpp index 18ab7b2a4a..9a12e545ce 100644 --- a/tests/kfdtest/src/KFDBaseComponentTest.hpp +++ b/tests/kfdtest/src/KFDBaseComponentTest.hpp @@ -20,14 +20,20 @@ * OTHER DEALINGS IN THE SOFTWARE. * */ +#ifndef __KFD_BASE_COMPONENT_TEST__H__ +#define __KFD_BASE_COMPONENT_TEST__H__ #include #include "hsakmt.h" #include "OSWrapper.hpp" #include "KFDTestUtil.hpp" - -#ifndef __KFD_BASE_COMPONENT_TEST__H__ -#define __KFD_BASE_COMPONENT_TEST__H__ +#include +#include +#include +#include +#include +#include +#include // @class KFDBaseComponentTest class KFDBaseComponentTest : public testing::Test { @@ -37,6 +43,18 @@ class KFDBaseComponentTest : public testing::Test { HSAuint64 GetSysMemSize(); HSAuint64 GetVramSize(int defaultGPUNode); +#define MAX_RENDER_NODES 64 + struct { + int fd; + uint32_t major_version; + uint32_t minor_version; + amdgpu_device_handle device_handle; + uint32_t bdf; + } m_RenderNodes[MAX_RENDER_NODES]; + +// @brief Finds DRM Render node corresponding to gpuNode +// @return DRM Render Node if successful or -1 on failure + int FindDRMRenderNode(int gpuNode); protected: HsaVersionInfo m_VersionInfo; diff --git a/tests/kfdtest/src/KFDGraphicsInterop.cpp b/tests/kfdtest/src/KFDGraphicsInterop.cpp index b99c1f3b9c..f44130aea8 100644 --- a/tests/kfdtest/src/KFDGraphicsInterop.cpp +++ b/tests/kfdtest/src/KFDGraphicsInterop.cpp @@ -23,103 +23,9 @@ #include "KFDGraphicsInterop.hpp" -extern "C" { -#include -#include -#include -} -#include -#include -#include -#include -#include #include "Dispatch.hpp" #include "PM4Queue.hpp" -void KFDGraphicsInterop::SetUp() { - ROUTINE_START - - KFDMemoryTest::SetUp(); - - // Try to open and initialize a render node for each device - for (int i = 0; i < MAX_RENDER_NODES; i++) { - m_RenderNodes[i].fd = drmOpenRender(i + 128); - - if (m_RenderNodes[i].fd <= 0) - continue; - - if (amdgpu_device_initialize(m_RenderNodes[i].fd, - &m_RenderNodes[i].major_version, - &m_RenderNodes[i].minor_version, - &m_RenderNodes[i].device_handle) != 0) { - drmClose(m_RenderNodes[i].fd); - m_RenderNodes[i].fd = 0; - } - - // Try to determine the bus-ID from sysfs - char path[PATH_MAX], link[PATH_MAX]; - snprintf(path, PATH_MAX, "/sys/class/drm/renderD%d", i+128); - if (readlink(path, link, PATH_MAX) < 0) { - LOG() << "Failed to read sysfs link " << path - << ", can't determine bus ID." << std::endl; - continue; - } - - char *state = NULL; - char *prev = NULL; - char *tok = strtok_r(link, "/", &state); - while (tok && strcmp(tok, "drm")) { - prev = tok; - tok = strtok_r(NULL, "/", &state); - } - unsigned domain, bus, device, func; - if (!prev || - sscanf(prev, "%04x:%02x:%02x.%1x", &domain, &bus, &device, &func) - != 4) { - LOG() << "Failed to parse sysfs link " << path - << ", can't determine bus ID." << std::endl; - continue; - } - - m_RenderNodes[i].bdf = (bus << 8) | (device << 3) | func; - } - - ROUTINE_END -} - -void KFDGraphicsInterop::TearDown() { - ROUTINE_START - - for (int i = 0; i < MAX_RENDER_NODES; i++) { - if (m_RenderNodes[i].fd <= 0) - continue; - - EXPECT_EQ(0, amdgpu_device_deinitialize(m_RenderNodes[i].device_handle)); - EXPECT_EQ(0, drmClose(m_RenderNodes[i].fd)); - } - - KFDMemoryTest::TearDown(); - - ROUTINE_END -} - -int KFDGraphicsInterop::FindDRMRenderNode(int gpuNode) { - int rn; - const HsaNodeProperties *pNodeProps = - m_NodeInfo.GetNodeProperties(gpuNode); - - for (rn = 0; rn < MAX_RENDER_NODES; rn++) { - if (m_RenderNodes[rn].fd <= 0) - continue; - if (m_RenderNodes[rn].bdf == pNodeProps->LocationId) - return rn; - } - LOG() << "Found no render node corresponding to GPU node " - << gpuNode << std::endl; - LOG() << "Check your device permissions" << std::endl; - return -1; -} - TEST_F(KFDGraphicsInterop, RegisterGraphicsHandle) { TEST_START(TESTPROFILE_RUNALL) diff --git a/tests/kfdtest/src/KFDGraphicsInterop.hpp b/tests/kfdtest/src/KFDGraphicsInterop.hpp index 9b94062520..260044cebf 100644 --- a/tests/kfdtest/src/KFDGraphicsInterop.hpp +++ b/tests/kfdtest/src/KFDGraphicsInterop.hpp @@ -22,9 +22,6 @@ */ #include "KFDMemoryTest.hpp" -extern "C" { -#include -} #ifndef __KFD_GRAPHICS_INTEROP_TEST__H__ #define __KFD_GRAPHICS_INTEROP_TEST__H__ @@ -36,23 +33,6 @@ class KFDGraphicsInterop : public KFDMemoryTest public: KFDGraphicsInterop(void) {}; ~KFDGraphicsInterop(void) {}; -protected: - virtual void SetUp(); - virtual void TearDown(); - -protected: -#define MAX_RENDER_NODES 64 - struct { - int fd; - uint32_t major_version; - uint32_t minor_version; - amdgpu_device_handle device_handle; - uint32_t bdf; - } m_RenderNodes[MAX_RENDER_NODES]; - -// @brief Finds DRM Render node corresponding to gpuNode -// @return DRM Render Node if successful or -1 on failure -int FindDRMRenderNode(int gpuNode); }; #endif