Skip to content

Commit

Permalink
Reintroduce prelim DRM debug support
Browse files Browse the repository at this point in the history
It was verified that the issue existed before the failing commit.

Signed-off-by: Daniel Chabrowski [email protected]
Related-To: NEO-6591
  • Loading branch information
DanielChabrowski authored and Compute-Runtime-Automation committed Feb 28, 2022
1 parent 8aa2e76 commit ca7d475
Show file tree
Hide file tree
Showing 16 changed files with 665 additions and 31 deletions.
3 changes: 1 addition & 2 deletions opencl/test/unit_test/os_interface/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ set(IGDRCL_SRCS_tests_os_interface_linux
${CMAKE_CURRENT_SOURCE_DIR}/drm_command_stream_mm_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/drm_command_stream_tests_1.cpp
${CMAKE_CURRENT_SOURCE_DIR}/drm_command_stream_tests_2.cpp
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}drm_debug_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/drm_gem_close_worker_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/drm_mapper_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/drm_memory_manager_tests.cpp
Expand All @@ -24,7 +23,7 @@ set(IGDRCL_SRCS_tests_os_interface_linux
${CMAKE_CURRENT_SOURCE_DIR}/drm_residency_handler_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/drm_system_info_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/drm_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}drm_uuid_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/drm_uuid_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/file_logger_linux_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/hw_info_config_linux_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/hw_info_config_linux_tests.h
Expand Down
44 changes: 43 additions & 1 deletion opencl/test/unit_test/os_interface/linux/drm_uuid_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
*/

#include "shared/source/os_interface/linux/drm_debug.h"
#include "shared/test/common/libult/linux/drm_mock.h"

#include "gtest/gtest.h"
Expand Down Expand Up @@ -32,8 +33,49 @@ TEST(DrmUuidTest, GivenDrmWhenGeneratingElfUUIDThenCorrectStringsAreReturned) {
auto executionEnvironment = std::make_unique<ExecutionEnvironment>();
executionEnvironment->prepareRootDeviceEnvironments(1);
DrmMock drm{*executionEnvironment->rootDeviceEnvironments[0]};

std::string elfClassUuid = classNamesToUuid[static_cast<uint32_t>(Drm::ResourceClass::Elf)].second;
std::string Uuid1stElfClass = elfClassUuid.substr(0, 18);

char data[] = "abc";
auto uuid1 = drm.generateElfUUID(static_cast<const void *>(data));
std::string Uuid1stElfBin1 = uuid1.substr(0, 18);
EXPECT_STREQ(Uuid1stElfClass.c_str(), Uuid1stElfBin1.c_str());

EXPECT_STREQ("00000000-0000-0000-0000-000000000001", uuid1.c_str());
char data2[] = "123";
auto uuid2 = drm.generateElfUUID(static_cast<const void *>(data2));
std::string Uuid1stElfBin2 = uuid2.substr(0, 18);
EXPECT_STREQ(Uuid1stElfClass.c_str(), Uuid1stElfBin2.c_str());

auto uuid3 = drm.generateElfUUID(reinterpret_cast<const void *>(0xFFFFFFFFFFFFFFFF));
std::string UuidElf = Uuid1stElfClass + "-ffff-ffffffffffff";
EXPECT_STREQ(UuidElf.c_str(), uuid3.c_str());
}

TEST(DrmUuidTest, whenResourceClassIsUsedToIndexClassNamesThenCorrectNamesAreReturned) {
EXPECT_STREQ(classNamesToUuid[static_cast<uint32_t>(Drm::ResourceClass::Elf)].first, "I915_UUID_CLASS_ELF_BINARY");
EXPECT_STREQ(classNamesToUuid[static_cast<uint32_t>(Drm::ResourceClass::Isa)].first, "I915_UUID_CLASS_ISA_BYTECODE");
EXPECT_STREQ(classNamesToUuid[static_cast<uint32_t>(Drm::ResourceClass::ContextSaveArea)].first, "I915_UUID_L0_SIP_AREA");
EXPECT_STREQ(classNamesToUuid[static_cast<uint32_t>(Drm::ResourceClass::ModuleHeapDebugArea)].first, "I915_UUID_L0_MODULE_AREA");
EXPECT_STREQ(classNamesToUuid[static_cast<uint32_t>(Drm::ResourceClass::SbaTrackingBuffer)].first, "I915_UUID_L0_SBA_AREA");
EXPECT_STREQ(classNamesToUuid[static_cast<uint32_t>(Drm::ResourceClass::L0ZebinModule)].first, "L0_ZEBIN_MODULE");
}

TEST(DrmUuidTest, givenUuidStringWhenGettingClassIndexThenCorrectIndexForValidStringsIsReturned) {
uint32_t index = 100;
auto validUuid = DrmUuid::getClassUuidIndex(classNamesToUuid[static_cast<uint32_t>(Drm::ResourceClass::ContextSaveArea)].second, index);

EXPECT_TRUE(validUuid);
EXPECT_EQ(static_cast<uint32_t>(Drm::ResourceClass::ContextSaveArea), index);

validUuid = DrmUuid::getClassUuidIndex(classNamesToUuid[static_cast<uint32_t>(Drm::ResourceClass::ModuleHeapDebugArea)].second, index);

EXPECT_TRUE(validUuid);
EXPECT_EQ(static_cast<uint32_t>(Drm::ResourceClass::ModuleHeapDebugArea), index);

index = 100;
validUuid = DrmUuid::getClassUuidIndex("invalid", index);

EXPECT_FALSE(validUuid);
EXPECT_EQ(100u, index);
}
3 changes: 2 additions & 1 deletion shared/source/os_interface/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ set(NEO_CORE_OS_INTERFACE_LINUX
${CMAKE_CURRENT_SOURCE_DIR}/drm_command_stream.h
${CMAKE_CURRENT_SOURCE_DIR}/drm_command_stream.inl
${CMAKE_CURRENT_SOURCE_DIR}/drm_command_stream_bdw_and_later.inl
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}drm_debug.cpp
${CMAKE_CURRENT_SOURCE_DIR}/drm_debug.cpp
${CMAKE_CURRENT_SOURCE_DIR}/drm_debug.h
${CMAKE_CURRENT_SOURCE_DIR}/drm_gem_close_worker.cpp
${CMAKE_CURRENT_SOURCE_DIR}/drm_gem_close_worker.h
${CMAKE_CURRENT_SOURCE_DIR}/drm_memory_manager.cpp
Expand Down
88 changes: 79 additions & 9 deletions shared/source/os_interface/linux/drm_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,68 @@
*
*/

#include "drm_neo.h"
#include "shared/source/os_interface/linux/drm_debug.h"

#include "shared/source/helpers/string.h"
#include "shared/source/os_interface/linux/drm_neo.h"

#include <string>
#include <utility>

namespace NEO {

bool Drm::registerResourceClasses() {
return false;
for (auto classNameUUID : classNamesToUuid) {
auto className = classNameUUID.first;
auto uuid = classNameUUID.second;

const auto result = ioctlHelper->registerStringClassUuid(this, uuid, (uintptr_t)className, strnlen_s(className, 100));
if (result.retVal != 0) {
return false;
}

classHandles.push_back(result.handle);
}
return true;
}

uint32_t Drm::registerResource(ResourceClass classType, const void *data, size_t size) {
return 0;
if (classHandles.size() < static_cast<uint32_t>(classType)) {
return 0;
}

std::string uuid;
if (classType == NEO::Drm::ResourceClass::Elf) {
uuid = generateElfUUID(data);
} else {
uuid = generateUUID();
}

const auto uuidClass = classHandles[static_cast<uint32_t>(classType)];
const auto ptr = size > 0 ? (uintptr_t)data : 0;
const auto result = ioctlHelper->registerUuid(this, uuid, uuidClass, ptr, size);

PRINT_DEBUGGER_INFO_LOG("PRELIM_DRM_IOCTL_I915_UUID_REGISTER: classType = %d, uuid = %s, data = %p, handle = %lu, ret = %d\n", (int)classType, std::string(uuid, 36).c_str(), ptr, result.handle, result.retVal);
DEBUG_BREAK_IF(result.retVal != 0);

return result.handle;
}

uint32_t Drm::registerIsaCookie(uint32_t isaHandle) {
return 0;
auto uuid = generateUUID();

const auto result = ioctlHelper->registerUuid(this, uuid, isaHandle, 0, 0);

PRINT_DEBUGGER_INFO_LOG("PRELIM_DRM_IOCTL_I915_UUID_REGISTER: isa handle = %lu, uuid = %s, data = %p, handle = %lu, ret = %d\n", isaHandle, std::string(uuid, 36).c_str(), 0, result.handle, result.retVal);
DEBUG_BREAK_IF(result.retVal != 0);

return result.handle;
}

void Drm::unregisterResource(uint32_t handle) {
PRINT_DEBUGGER_INFO_LOG("PRELIM_DRM_IOCTL_I915_UUID_UNREGISTER: handle = %lu\n", handle);
[[maybe_unused]] const auto ret = ioctlHelper->unregisterUuid(this, handle);
DEBUG_BREAK_IF(ret != 0);
}

std::string Drm::generateUUID() {
Expand All @@ -40,12 +85,37 @@ std::string Drm::generateUUID() {
}

std::string Drm::generateElfUUID(const void *data) {
return generateUUID();
std::string elf_class_uuid = classNamesToUuid[static_cast<uint32_t>(Drm::ResourceClass::Elf)].second;
std::string UUID1st = elf_class_uuid.substr(0, 18);

const char uuidString[] = "%s-%04" SCNx64 "-%012" SCNx64;
char buffer[36 + 1] = "00000000-0000-0000-0000-000000000000";

uint64_t parts[2] = {0, 0};
parts[0] = reinterpret_cast<uintptr_t>(data) & 0xFFFFFFFFFFFF;
parts[1] = (reinterpret_cast<uintptr_t>(data) & 0xFFFF000000000000) >> 48;
snprintf(buffer, sizeof(buffer), uuidString, UUID1st.c_str(), parts[1], parts[0]);

return std::string(buffer, 36);
}

void Drm::checkContextDebugSupport() {
contextDebugSupported = ioctlHelper->isContextDebugSupported(this);
}

void Drm::setContextDebugFlag(uint32_t drmContextId) {
[[maybe_unused]] const auto retVal = ioctlHelper->setContextDebugFlag(this, drmContextId);
DEBUG_BREAK_IF(retVal != 0 && contextDebugSupported);
}

void Drm::checkContextDebugSupport() {}
void Drm::setContextDebugFlag(uint32_t drmContextId) {}
uint32_t Drm::notifyFirstCommandQueueCreated() { return 0; }
void Drm::notifyLastCommandQueueDestroyed(uint32_t handle) {}
uint32_t Drm::notifyFirstCommandQueueCreated() {
const auto result = ioctlHelper->registerStringClassUuid(this, uuidL0CommandQueueHash, (uintptr_t)uuidL0CommandQueueName, strnlen_s(uuidL0CommandQueueName, 100));
DEBUG_BREAK_IF(result.retVal);
return result.handle;
}

void Drm::notifyLastCommandQueueDestroyed(uint32_t handle) {
unregisterResource(handle);
}

} // namespace NEO
69 changes: 69 additions & 0 deletions shared/source/os_interface/linux/drm_debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2022 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/

#include "shared/source/os_interface/linux/drm_neo.h"

#include <string>
#include <utility>

namespace NEO {

/*
UUIDs: Deterministic generation
$ python -q # Version 3.x
>>> import uuid
>>>
>>> I915_UUID_NAMESPACE = uuid.UUID(bytes = b'i915' * 4);
>>> I915_UUID_NAMESPACE
UUID('69393135-6939-3135-6939-313569393135')
>>>
>>>
>>> I915_UUID = lambda x: uuid.uuid5(I915_UUID_NAMESPACE, x)
>>> I915_UUID('I915_UUID_CLASS_CUSTOM')
UUID('74644f12-6a2c-59e6-ac92-ea7f2ef530eb')
>>>
>>>
>>> L0_UUID_NAMESPACE = uuid.UUID(bytes = b'L0' * 8);
>>> L0_UUID_NAMESPACE
UUID('4c304c30-4c30-4c30-4c30-4c304c304c30')
>>>
>>>
>>> L0_UUID = lambda x: uuid.uuid5(L0_UUID_NAMESPACE, x)
>>> L0_UUID('L0_ZEBIN_MODULE')
UUID('88d347c1-c79b-530a-b68f-e0db7d575e04')
>>>
>>>
>>> L0_UUID('L0_COMMAND_QUEUE')
UUID('285208b2-c5e0-5fcb-90bb-7576ed7a9697')
*/

using ClassNamesArray = std::array<std::pair<const char *, const std::string>, size_t(Drm::ResourceClass::MaxSize)>;
const ClassNamesArray classNamesToUuid = {std::make_pair("I915_UUID_CLASS_ELF_BINARY", "31203221-8069-5a0a-9d43-94a4d3395ee1"),
std::make_pair("I915_UUID_CLASS_ISA_BYTECODE", "53baed0a-12c3-5d19-aa69-ab9c51aa1039"),
std::make_pair("I915_UUID_L0_MODULE_AREA", "a411e82e-16c9-58b7-bfb5-b209b8601d5f"),
std::make_pair("I915_UUID_L0_SIP_AREA", "21fd6baf-f918-53cc-ba74-f09aaaea2dc0"),
std::make_pair("I915_UUID_L0_SBA_AREA", "ec45189d-97d3-58e2-80d1-ab52c72fdcc1"),
std::make_pair("L0_ZEBIN_MODULE", "88d347c1-c79b-530a-b68f-e0db7d575e04")};

constexpr auto uuidL0CommandQueueName = "L0_COMMAND_QUEUE";
constexpr auto uuidL0CommandQueueHash = "285208b2-c5e0-5fcb-90bb-7576ed7a9697"; // L0_UUID('L0_COMMAND_QUEUE')

struct DrmUuid {
static bool getClassUuidIndex(std::string uuid, uint32_t &index) {
for (uint32_t i = 0; i < uint32_t(Drm::ResourceClass::MaxSize); i++) {
if (uuid == classNamesToUuid[i].second) {
index = i;
return true;
}
}
return false;
}
};
} // namespace NEO
20 changes: 20 additions & 0 deletions shared/source/os_interface/linux/ioctl_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ struct VmBindParams {
uint64_t extensions;
};

struct UuidRegisterResult {
uint32_t retVal;
uint32_t handle;
};

using MemRegionsVec = StackVec<MemoryClassInstance, 5>;

class IoctlHelper {
Expand Down Expand Up @@ -114,6 +119,11 @@ class IoctlHelper {
virtual int vmUnbind(Drm *drm, const VmBindParams &vmBindParams) = 0;
virtual bool getEuStallProperties(std::array<uint64_t, 10u> &properties, uint64_t dssBufferSize, uint64_t samplingRate, uint64_t pollPeriod, uint64_t engineInstance) = 0;
virtual uint32_t getEuStallFdParameter() = 0;
virtual UuidRegisterResult registerUuid(Drm *drm, const std::string &uuid, uint32_t uuidClass, uint64_t ptr, uint64_t size) = 0;
virtual UuidRegisterResult registerStringClassUuid(Drm *drm, const std::string &uuid, uint64_t ptr, uint64_t size) = 0;
virtual int unregisterUuid(Drm *drm, uint32_t handle) = 0;
virtual bool isContextDebugSupported(Drm *drm) = 0;
virtual int setContextDebugFlag(Drm *drm, uint32_t drmContextId) = 0;
};

class IoctlHelperUpstream : public IoctlHelper {
Expand Down Expand Up @@ -159,6 +169,11 @@ class IoctlHelperUpstream : public IoctlHelper {
int vmUnbind(Drm *drm, const VmBindParams &vmBindParams) override;
bool getEuStallProperties(std::array<uint64_t, 10u> &properties, uint64_t dssBufferSize, uint64_t samplingRate, uint64_t pollPeriod, uint64_t engineInstance) override;
uint32_t getEuStallFdParameter() override;
UuidRegisterResult registerUuid(Drm *drm, const std::string &uuid, uint32_t uuidClass, uint64_t ptr, uint64_t size) override;
UuidRegisterResult registerStringClassUuid(Drm *drm, const std::string &uuid, uint64_t ptr, uint64_t size) override;
int unregisterUuid(Drm *drm, uint32_t handle) override;
bool isContextDebugSupported(Drm *drm) override;
int setContextDebugFlag(Drm *drm, uint32_t drmContextId) override;
};

template <PRODUCT_FAMILY gfxProduct>
Expand Down Expand Up @@ -217,6 +232,11 @@ class IoctlHelperPrelim20 : public IoctlHelper {
int vmUnbind(Drm *drm, const VmBindParams &vmBindParams) override;
bool getEuStallProperties(std::array<uint64_t, 10u> &properties, uint64_t dssBufferSize, uint64_t samplingRate, uint64_t pollPeriod, uint64_t engineInstance) override;
uint32_t getEuStallFdParameter() override;
UuidRegisterResult registerUuid(Drm *drm, const std::string &uuid, uint32_t uuidClass, uint64_t ptr, uint64_t size) override;
UuidRegisterResult registerStringClassUuid(Drm *drm, const std::string &uuid, uint64_t ptr, uint64_t size) override;
int unregisterUuid(Drm *drm, uint32_t handle) override;
bool isContextDebugSupported(Drm *drm) override;
int setContextDebugFlag(Drm *drm, uint32_t drmContextId) override;
};

} // namespace NEO
50 changes: 50 additions & 0 deletions shared/source/os_interface/linux/ioctl_helper_prelim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "shared/source/helpers/hw_helper.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/helpers/ptr_math.h"
#include "shared/source/helpers/string.h"
#include "shared/source/os_interface/linux/cache_info.h"
#include "shared/source/os_interface/linux/ioctl_helper.h"

Expand Down Expand Up @@ -319,6 +320,7 @@ int32_t IoctlHelperPrelim20::getComputeEngineClass() {
std::optional<int> IoctlHelperPrelim20::getHasPageFaultParamId() {
return PRELIM_I915_PARAM_HAS_PAGE_FAULT;
};

bool IoctlHelperPrelim20::getEuStallProperties(std::array<uint64_t, 10u> &properties, uint64_t dssBufferSize, uint64_t samplingRate, uint64_t pollPeriod, uint64_t engineInstance) {
properties[0] = PRELIM_DRM_I915_EU_STALL_PROP_BUF_SZ;
properties[1] = dssBufferSize;
Expand Down Expand Up @@ -472,4 +474,52 @@ int IoctlHelperPrelim20::vmUnbind(Drm *drm, const VmBindParams &vmBindParams) {
auto prelimVmBind = translateVmBindParamsToPrelimStruct(vmBindParams);
return IoctlHelper::ioctl(drm, PRELIM_DRM_IOCTL_I915_GEM_VM_UNBIND, &prelimVmBind);
}

UuidRegisterResult IoctlHelperPrelim20::registerUuid(Drm *drm, const std::string &uuid, uint32_t uuidClass, uint64_t ptr, uint64_t size) {
prelim_drm_i915_uuid_control uuidControl = {};
memcpy_s(uuidControl.uuid, sizeof(uuidControl.uuid), uuid.c_str(), uuid.size());
uuidControl.uuid_class = uuidClass;
uuidControl.ptr = ptr;
uuidControl.size = size;

const auto retVal = IoctlHelper::ioctl(drm, PRELIM_DRM_IOCTL_I915_UUID_REGISTER, &uuidControl);

return {
retVal,
uuidControl.handle,
};
}

UuidRegisterResult IoctlHelperPrelim20::registerStringClassUuid(Drm *drm, const std::string &uuid, uint64_t ptr, uint64_t size) {
return registerUuid(drm, uuid, PRELIM_I915_UUID_CLASS_STRING, ptr, size);
}

int IoctlHelperPrelim20::unregisterUuid(Drm *drm, uint32_t handle) {
prelim_drm_i915_uuid_control uuidControl = {};
uuidControl.handle = handle;

return IoctlHelper::ioctl(drm, PRELIM_DRM_IOCTL_I915_UUID_UNREGISTER, &uuidControl);
}

bool IoctlHelperPrelim20::isContextDebugSupported(Drm *drm) {
drm_i915_gem_context_param ctxParam = {};
ctxParam.size = 0;
ctxParam.param = PRELIM_I915_CONTEXT_PARAM_DEBUG_FLAGS;
ctxParam.ctx_id = 0;
ctxParam.value = 0;

const auto retVal = IoctlHelper::ioctl(drm, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM, &ctxParam);
return retVal == 0 && ctxParam.value == (PRELIM_I915_CONTEXT_PARAM_DEBUG_FLAG_SIP << 32);
}

int IoctlHelperPrelim20::setContextDebugFlag(Drm *drm, uint32_t drmContextId) {
drm_i915_gem_context_param ctxParam = {};
ctxParam.size = 0;
ctxParam.param = PRELIM_I915_CONTEXT_PARAM_DEBUG_FLAGS;
ctxParam.ctx_id = drmContextId;
ctxParam.value = PRELIM_I915_CONTEXT_PARAM_DEBUG_FLAG_SIP << 32 | PRELIM_I915_CONTEXT_PARAM_DEBUG_FLAG_SIP;

return IoctlHelper::ioctl(drm, DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM, &ctxParam);
}

} // namespace NEO
Loading

0 comments on commit ca7d475

Please sign in to comment.