From bf5953c091515a724c2ad8c53ba74a3ce0e40905 Mon Sep 17 00:00:00 2001 From: Slawomir Milczarek Date: Tue, 3 Sep 2024 17:20:54 +0000 Subject: [PATCH] feature: Add ioctl helper function to set external context Related-To: NEO-11817 Signed-off-by: Slawomir Milczarek --- .../os_interface/linux/ioctl_helper.cpp | 7 ++++++ .../source/os_interface/linux/ioctl_helper.h | 10 ++++++++ .../linux/xe/ioctl_helper_xe_tests.cpp | 25 +++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/shared/source/os_interface/linux/ioctl_helper.cpp b/shared/source/os_interface/linux/ioctl_helper.cpp index bde81ae741..267e387a64 100644 --- a/shared/source/os_interface/linux/ioctl_helper.cpp +++ b/shared/source/os_interface/linux/ioctl_helper.cpp @@ -22,7 +22,14 @@ namespace NEO { +void IoctlHelper::setExternalContext(ExternalCtx *ctx) { + externalCtx = ctx; +} + int IoctlHelper::ioctl(DrmIoctl request, void *arg) { + if (externalCtx) { + return externalCtx->ioctl(externalCtx->handle, drm.getFileDescriptor(), getIoctlRequestValue(request), arg, false); + } return drm.ioctl(request, arg); } diff --git a/shared/source/os_interface/linux/ioctl_helper.h b/shared/source/os_interface/linux/ioctl_helper.h index 47333dd120..08f284540d 100644 --- a/shared/source/os_interface/linux/ioctl_helper.h +++ b/shared/source/os_interface/linux/ioctl_helper.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -87,6 +88,13 @@ struct ResetStatsFault { uint16_t flags; }; +using IoctlFunc = std::function; + +struct ExternalCtx { + void *handle = nullptr; + IoctlFunc ioctl = nullptr; +}; + using MemRegionsVec = StackVec; using VmBindExtSetPatT = uint8_t[40]; using VmBindExtUserFenceT = uint8_t[56]; @@ -98,6 +106,7 @@ class IoctlHelper { static std::unique_ptr getI915Helper(const PRODUCT_FAMILY productFamily, const std::string &prelimVersion, Drm &drm); virtual int ioctl(DrmIoctl request, void *arg); virtual int ioctl(int fd, DrmIoctl request, void *arg); + virtual void setExternalContext(ExternalCtx *ctx); virtual bool initialize() = 0; virtual bool isSetPairAvailable() = 0; @@ -214,6 +223,7 @@ class IoctlHelper { protected: Drm &drm; + ExternalCtx *externalCtx = nullptr; }; class IoctlHelperI915 : public IoctlHelper { diff --git a/shared/test/unit_test/os_interface/linux/xe/ioctl_helper_xe_tests.cpp b/shared/test/unit_test/os_interface/linux/xe/ioctl_helper_xe_tests.cpp index a9175d5b54..976491a2d1 100644 --- a/shared/test/unit_test/os_interface/linux/xe/ioctl_helper_xe_tests.cpp +++ b/shared/test/unit_test/os_interface/linux/xe/ioctl_helper_xe_tests.cpp @@ -2402,3 +2402,28 @@ TEST(IoctlHelperXeTest, givenCorrectEuPerDssTypeWhenCheckingIfTopologyIsEuPerDss EXPECT_FALSE(MockIoctlHelperXe::isEuPerDssTopologyType(DRM_XE_TOPO_DSS_GEOMETRY)); EXPECT_FALSE(MockIoctlHelperXe::isEuPerDssTopologyType(DRM_XE_TOPO_DSS_COMPUTE)); } + +TEST(IoctlHelperXeTest, givenIoctlHelperWhenSettingExtContextThenCallExternalIoctlFunction) { + MockExecutionEnvironment executionEnvironment{}; + std::unique_ptr drm{Drm::create(std::make_unique(0, ""), *executionEnvironment.rootDeviceEnvironments[0])}; + IoctlHelperXe ioctlHelper{*drm}; + + bool ioctlCalled = false; + ResetStats resetStats{}; + EXPECT_TRUE(ioctlHelper.ioctl(DrmIoctl::getResetStats, &resetStats)); + EXPECT_FALSE(ioctlCalled); + + int handle = 0; + IoctlFunc ioctl = [&](void *, int, unsigned long int, void *, bool) { ioctlCalled=true; return 0; }; + ExternalCtx ctx{&handle, ioctl}; + + ioctlHelper.setExternalContext(&ctx); + ioctlCalled = false; + EXPECT_EQ(0, ioctlHelper.ioctl(DrmIoctl::getResetStats, &resetStats)); + EXPECT_TRUE(ioctlCalled); + + ioctlHelper.setExternalContext(nullptr); + ioctlCalled = false; + EXPECT_TRUE(ioctlHelper.ioctl(DrmIoctl::getResetStats, &resetStats)); + EXPECT_FALSE(ioctlCalled); +}