diff --git a/opencl/test/unit_test/linux/main_linux_dll.cpp b/opencl/test/unit_test/linux/main_linux_dll.cpp index f012082cee..ee31999349 100644 --- a/opencl/test/unit_test/linux/main_linux_dll.cpp +++ b/opencl/test/unit_test/linux/main_linux_dll.cpp @@ -114,27 +114,51 @@ TEST_F(DrmSimpleTests, GivenTwoOpenableDevicesWhenDiscoverDevicesThenCreateTwoHw EXPECT_EQ(2u, hwDeviceIds.size()); } -TEST_F(DrmSimpleTests, GivenSelectedNotExistingDeviceWhenGetDeviceFdThenFail) { +TEST_F(DrmSimpleTests, GivenSelectedNotExistingDeviceUsingForceDeviceIdFlagWhenGetDeviceFdThenFail) { DebugManagerStateRestore stateRestore; - DebugManager.flags.ForceDeviceId.set("1234"); - VariableBackup backupOpenFull(&openFull); - openFull = testOpen; - openRetVal = -1; + DebugManager.flags.ForceDeviceId.set("invalid"); + openFull = nullptr; // open shouldn't be called ExecutionEnvironment executionEnvironment; auto hwDeviceIds = OSInterface::discoverDevices(executionEnvironment); EXPECT_TRUE(hwDeviceIds.empty()); } -TEST_F(DrmSimpleTests, GivenSelectedExistingDeviceWhenGetDeviceFdThenReturnFd) { +TEST_F(DrmSimpleTests, GivenSelectedExistingDeviceUsingForceDeviceIdFlagWhenGetDeviceFdThenReturnFd) { DebugManagerStateRestore stateRestore; DebugManager.flags.ForceDeviceId.set("0000:00:02.0"); VariableBackup backupOpenFull(&openFull); - openRetVal = 1023; // fakeFd - openFull = testOpen; + openFull = openWithCounter; + openCounter = 10; ExecutionEnvironment executionEnvironment; auto hwDeviceIds = OSInterface::discoverDevices(executionEnvironment); EXPECT_EQ(1u, hwDeviceIds.size()); EXPECT_NE(nullptr, hwDeviceIds[0].get()); + EXPECT_STREQ("/dev/dri/by-path/platform-4010000000.pcie-pci-0000:00:02.0-render", lastOpenedPath.c_str()); + EXPECT_EQ(9, openCounter); // only one opened file +} + +TEST_F(DrmSimpleTests, GivenSelectedNotExistingDeviceUsingFilterBdfWhenGetDeviceFdThenFail) { + DebugManagerStateRestore stateRestore; + DebugManager.flags.FilterBdfPath.set("invalid"); + VariableBackup backupOpenFull(&openFull); + openFull = nullptr; // open shouldn't be called + ExecutionEnvironment executionEnvironment; + auto hwDeviceIds = OSInterface::discoverDevices(executionEnvironment); + EXPECT_TRUE(hwDeviceIds.empty()); +} + +TEST_F(DrmSimpleTests, GivenSelectedExistingDeviceUsingFilterBdfWhenGetDeviceFdThenReturnFd) { + DebugManagerStateRestore stateRestore; + DebugManager.flags.FilterBdfPath.set("0000:00:02.0"); + VariableBackup backupOpenFull(&openFull); + openFull = openWithCounter; + openCounter = 10; + ExecutionEnvironment executionEnvironment; + auto hwDeviceIds = OSInterface::discoverDevices(executionEnvironment); + EXPECT_EQ(1u, hwDeviceIds.size()); + EXPECT_NE(nullptr, hwDeviceIds[0].get()); + EXPECT_STREQ("/dev/dri/by-path/platform-4010000000.pcie-pci-0000:00:02.0-render", lastOpenedPath.c_str()); + EXPECT_EQ(9, openCounter); // only one opened file } TEST_F(DrmSimpleTests, GivenSelectedExistingDeviceWhenOpenDirSuccedsThenHwDeviceIdsHaveProperPciPaths) { @@ -405,16 +429,21 @@ TEST_F(DrmSimpleTests, GivenMultipleAvailableDevicesWhenCreateMultipleRootDevice EXPECT_STREQ("0000:00:02.0", hwDeviceIds[1]->as()->getPciPath()); } -TEST_F(DrmSimpleTests, GivenSelectedIncorectDeviceWhenGetDeviceFdThenFail) { +TEST_F(DrmTests, GivenSelectedIncorectDeviceByDeviceIdWhenGetDeviceFdThenFail) { DebugManagerStateRestore stateRestore; - DebugManager.flags.ForceDeviceId.set("1234"); - VariableBackup backupOpenFull(&openFull); - openFull = testOpen; - openRetVal = 1024; - ExecutionEnvironment executionEnvironment; + DebugManager.flags.FilterDeviceId.set("invalid"); + auto drm1 = DrmWrap::createDrm(*rootDeviceEnvironment); + EXPECT_EQ(drm1, nullptr); +} - auto hwDeviceIds = OSInterface::discoverDevices(executionEnvironment); - EXPECT_TRUE(hwDeviceIds.empty()); +TEST_F(DrmTests, GivenSelectedCorrectDeviceByDeviceIdWhenGetDeviceFdThenSucceed) { + DebugManagerStateRestore stateRestore; + std::stringstream deviceIdStr; + deviceIdStr << std::hex << deviceId; + + DebugManager.flags.FilterDeviceId.set(deviceIdStr.str()); + auto drm1 = DrmWrap::createDrm(*rootDeviceEnvironment); + EXPECT_NE(drm1, nullptr); } TEST_F(DrmSimpleTests, givenUseVmBindFlagWhenOverrideBindSupportThenReturnProperValue) { diff --git a/opencl/test/unit_test/os_interface/device_factory_tests.cpp b/opencl/test/unit_test/os_interface/device_factory_tests.cpp index b6515f1b1a..da16770560 100644 --- a/opencl/test/unit_test/os_interface/device_factory_tests.cpp +++ b/opencl/test/unit_test/os_interface/device_factory_tests.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2021 Intel Corporation + * Copyright (C) 2018-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -377,6 +377,25 @@ TEST(DiscoverDevices, whenDiscoverDevicesAndForceDeviceIdIsDifferentFromTheExist EXPECT_FALSE(result); } +TEST(DiscoverDevices, whenDiscoverDevicesAndFilterDifferentFromTheExistingDeviceThenReturnNullptr) { + DebugManagerStateRestore stateRestore; + DebugManager.flags.FilterDeviceId.set("invalid"); + DebugManager.flags.FilterBdfPath.set("invalid"); + ExecutionEnvironment executionEnviornment; + auto hwDeviceIds = OSInterface::discoverDevices(executionEnviornment); + EXPECT_TRUE(hwDeviceIds.empty()); +} + +TEST(DiscoverDevices, whenDiscoverDevicesAndFilterDifferentFromTheExistingDeviceThenPrepareDeviceEnvironmentsReturnsFalse) { + DebugManagerStateRestore stateRestore; + DebugManager.flags.FilterDeviceId.set("invalid"); + DebugManager.flags.FilterBdfPath.set("invalid"); + ExecutionEnvironment executionEnviornment; + + auto result = DeviceFactory::prepareDeviceEnvironments(executionEnviornment); + EXPECT_FALSE(result); +} + using UltDeviceFactoryTest = DeviceFactoryTest; TEST_F(UltDeviceFactoryTest, givenExecutionEnvironmentWhenCreatingUltDeviceFactoryThenMockMemoryManagerIsAllocated) { diff --git a/opencl/test/unit_test/os_interface/windows/wddm20_tests.cpp b/opencl/test/unit_test/os_interface/windows/wddm20_tests.cpp index 861d742e60..5039b06ccc 100644 --- a/opencl/test/unit_test/os_interface/windows/wddm20_tests.cpp +++ b/opencl/test/unit_test/os_interface/windows/wddm20_tests.cpp @@ -1232,6 +1232,15 @@ TEST(WddmGfxPartitionTests, givenInternalFrontWindowHeapWhenAllocatingSmallOrBig } } +TEST_F(Wddm20Tests, givenWddmWhenDiscoverDevicesAndFilterDeviceIdIsTheSameAsTheExistingDeviceThenReturnTheAdapter) { + DebugManagerStateRestore stateRestore; + DebugManager.flags.FilterDeviceId.set("1234"); // Existing device Id + ExecutionEnvironment executionEnvironment; + auto hwDeviceIds = OSInterface::discoverDevices(executionEnvironment); + EXPECT_EQ(1u, hwDeviceIds.size()); + EXPECT_NE(nullptr, hwDeviceIds[0].get()); +} + TEST_F(Wddm20Tests, givenWddmWhenDiscoverDevicesAndForceDeviceIdIsTheSameAsTheExistingDeviceThenReturnTheAdapter) { DebugManagerStateRestore stateRestore; DebugManager.flags.ForceDeviceId.set("1234"); // Existing device Id diff --git a/opencl/test/unit_test/test_files/igdrcl.config b/opencl/test/unit_test/test_files/igdrcl.config index d12712f688..9ccb2161b2 100644 --- a/opencl/test/unit_test/test_files/igdrcl.config +++ b/opencl/test/unit_test/test_files/igdrcl.config @@ -32,6 +32,8 @@ GenerateAubFilePerProcessId = 0 EnableSWTags = 0 DumpSWTagsBXML = 0 ForceDeviceId = unk +FilterDeviceId = unk +FilterBdfPath = unk LoadBinarySipFromFile = unk InjectInternalBuildOptions = unk OverrideCsrAllocationSize = -1 diff --git a/shared/source/debug_settings/debug_variables_base.inl b/shared/source/debug_settings/debug_variables_base.inl index 763272a675..0bf9e87115 100644 --- a/shared/source/debug_settings/debug_variables_base.inl +++ b/shared/source/debug_settings/debug_variables_base.inl @@ -75,6 +75,8 @@ DECLARE_DEBUG_VARIABLE(bool, AllowPatchingVfeStateInCommandLists, false, "true: DECLARE_DEBUG_VARIABLE(bool, PrintMemoryRegionSizes, false, "print memory bank type, instance and it's size") DECLARE_DEBUG_VARIABLE(bool, UpdateCrossThreadDataSize, false, "Turn on cross thread data size calculation for PATCH TOKEN binary") DECLARE_DEBUG_VARIABLE(std::string, ForceDeviceId, std::string("unk"), "DeviceId selected for testing") +DECLARE_DEBUG_VARIABLE(std::string, FilterDeviceId, std::string("unk"), "Device id filter, adapter matching device id will be opened. Ignored when unk.") +DECLARE_DEBUG_VARIABLE(std::string, FilterBdfPath, std::string("unk"), "Linux-only, BDF path filter, only matching paths will be opened. Ignored when unk.") DECLARE_DEBUG_VARIABLE(std::string, LoadBinarySipFromFile, std::string("unk"), "Select binary file to load SIP kernel raw binary. When file named *_header.* exists, it is used as header") DECLARE_DEBUG_VARIABLE(std::string, InjectInternalBuildOptions, std::string("unk"), "Appends internal build options string to user modules") DECLARE_DEBUG_VARIABLE(int64_t, OverrideMultiStoragePlacement, -1, "-1: disable, 0+: tile mask, each bit corresponds to tile") diff --git a/shared/source/dll/linux/drm_neo_create.cpp b/shared/source/dll/linux/drm_neo_create.cpp index c7ac1c3d8c..89b7f06c56 100644 --- a/shared/source/dll/linux/drm_neo_create.cpp +++ b/shared/source/dll/linux/drm_neo_create.cpp @@ -11,6 +11,7 @@ #include "shared/source/gmm_helper/gmm_helper.h" #include "shared/source/helpers/hw_helper.h" #include "shared/source/helpers/hw_info.h" +#include "shared/source/os_interface/device_factory.h" #include "shared/source/os_interface/linux/drm_neo.h" #include "shared/source/os_interface/linux/drm_null_device.h" @@ -45,6 +46,9 @@ Drm *Drm::create(std::unique_ptr &&hwDeviceId, RootDeviceEnvironm printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr, "%s", "FATAL: Cannot query device ID parameter!\n"); return nullptr; } + if (!DeviceFactory::isAllowedDeviceId(drmObject->deviceId, DebugManager.flags.FilterDeviceId.get())) { + return nullptr; + } // Get HW Revision (I915_drm.h) ret = drmObject->getDeviceRevID(drmObject->revisionId); diff --git a/shared/source/os_interface/device_factory.cpp b/shared/source/os_interface/device_factory.cpp index 55d0085c40..6a140ce546 100644 --- a/shared/source/os_interface/device_factory.cpp +++ b/shared/source/os_interface/device_factory.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2021 Intel Corporation + * Copyright (C) 2018-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -206,4 +206,12 @@ bool (*DeviceFactory::createMemoryManagerFunc)(ExecutionEnvironment &) = [](Exec return executionEnvironment.initializeMemoryManager(); }; +bool DeviceFactory::isAllowedDeviceId(uint32_t deviceId, const std::string &deviceIdString) { + if (deviceIdString != "unk") { + char *endptr = nullptr; + auto reqDeviceId = strtoul(deviceIdString.c_str(), &endptr, 16); + return (static_cast(reqDeviceId) == deviceId); + } + return true; +} } // namespace NEO diff --git a/shared/source/os_interface/device_factory.h b/shared/source/os_interface/device_factory.h index 633175796f..6f310c5347 100644 --- a/shared/source/os_interface/device_factory.h +++ b/shared/source/os_interface/device_factory.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2021 Intel Corporation + * Copyright (C) 2018-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -28,5 +28,6 @@ class DeviceFactory { static std::unique_ptr (*createRootDeviceFunc)(ExecutionEnvironment &executionEnvironment, uint32_t rootDeviceIndex); static bool (*createMemoryManagerFunc)(ExecutionEnvironment &executionEnvironment); + static bool isAllowedDeviceId(uint32_t deviceId, const std::string &deviceIdString); }; } // namespace NEO diff --git a/shared/source/os_interface/linux/drm_neo.cpp b/shared/source/os_interface/linux/drm_neo.cpp index 7524b61cc7..848ec267f3 100644 --- a/shared/source/os_interface/linux/drm_neo.cpp +++ b/shared/source/os_interface/linux/drm_neo.cpp @@ -597,6 +597,11 @@ std::vector> Drm::discoverDevices(ExecutionEnvironme } } + if (DebugManager.flags.FilterBdfPath.get() != "unk") { + if (devicePathView.find(DebugManager.flags.FilterBdfPath.get().c_str()) == std::string::npos) { + continue; + } + } if (DebugManager.flags.ForceDeviceId.get() != "unk") { if (devicePathView.find(DebugManager.flags.ForceDeviceId.get().c_str()) == std::string::npos) { continue; diff --git a/shared/source/os_interface/windows/wddm/adapter_factory.cpp b/shared/source/os_interface/windows/wddm/adapter_factory.cpp index d9d37cc156..25fa29c2f3 100644 --- a/shared/source/os_interface/windows/wddm/adapter_factory.cpp +++ b/shared/source/os_interface/windows/wddm/adapter_factory.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Intel Corporation + * Copyright (C) 2021-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -9,6 +9,7 @@ #include "shared/source/debug_settings/debug_settings_manager.h" #include "shared/source/helpers/debug_helpers.h" +#include "shared/source/os_interface/device_factory.h" #include "shared/source/utilities/stackvec.h" #include @@ -24,13 +25,8 @@ bool canUseAdapterBasedOnDriverDesc(const char *driverDescription) { } bool isAllowedDeviceId(uint32_t deviceId) { - if (DebugManager.flags.ForceDeviceId.get() == "unk") { - return true; - } - - char *endptr = nullptr; - auto reqDeviceId = strtoul(DebugManager.flags.ForceDeviceId.get().c_str(), &endptr, 16); - return (static_cast(reqDeviceId) == deviceId); + return DeviceFactory::isAllowedDeviceId(deviceId, DebugManager.flags.FilterDeviceId.get()) && + DeviceFactory::isAllowedDeviceId(deviceId, DebugManager.flags.ForceDeviceId.get()); } } // namespace NEO diff --git a/shared/test/unit_test/os_interface/windows/adapter_info_tests.cpp b/shared/test/unit_test/os_interface/windows/adapter_info_tests.cpp index bfb36d962f..8d1c04765f 100644 --- a/shared/test/unit_test/os_interface/windows/adapter_info_tests.cpp +++ b/shared/test/unit_test/os_interface/windows/adapter_info_tests.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Intel Corporation + * Copyright (C) 2021-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -130,7 +130,7 @@ TEST(IsAllowedDeviceId, whenDebugKeyNotSetThenReturnTrue) { EXPECT_TRUE(NEO::isAllowedDeviceId(0xdeadbeef)); } -TEST(IsAllowedDeviceId, whenDebugKeySetThenExpectSpecificValue) { +TEST(IsAllowedDeviceId, whenForceDeviceIdDebugKeySetThenExpectSpecificValue) { DebugManagerStateRestore rest; DebugManager.flags.ForceDeviceId.set("167"); EXPECT_FALSE(NEO::isAllowedDeviceId(0xdeadbeef)); @@ -142,7 +142,7 @@ TEST(IsAllowedDeviceId, whenDebugKeySetThenExpectSpecificValue) { EXPECT_TRUE(NEO::isAllowedDeviceId(0x167)); } -TEST(IsAllowedDeviceId, whenDebugKeySetThenTreatAsHex) { +TEST(IsAllowedDeviceId, whenForceDeviceIdDebugKeySetThenTreatAsHex) { DebugManagerStateRestore rest; DebugManager.flags.ForceDeviceId.set("167"); @@ -151,3 +151,25 @@ TEST(IsAllowedDeviceId, whenDebugKeySetThenTreatAsHex) { DebugManager.flags.ForceDeviceId.set("167"); EXPECT_TRUE(NEO::isAllowedDeviceId(0x167)); } + +TEST(IsAllowedDeviceId, whenFilterDeviceIdDebugKeySetThenExpectSpecificValue) { + DebugManagerStateRestore rest; + DebugManager.flags.FilterDeviceId.set("167"); + EXPECT_FALSE(NEO::isAllowedDeviceId(0xdeadbeef)); + + DebugManager.flags.FilterDeviceId.set("1678"); + EXPECT_FALSE(NEO::isAllowedDeviceId(0x167)); + + DebugManager.flags.FilterDeviceId.set("167"); + EXPECT_TRUE(NEO::isAllowedDeviceId(0x167)); +} + +TEST(IsAllowedDeviceId, whenFilterDeviceIdDebugKeySetThenTreatAsHex) { + DebugManagerStateRestore rest; + + DebugManager.flags.FilterDeviceId.set("167"); + EXPECT_FALSE(NEO::isAllowedDeviceId(167)); + + DebugManager.flags.FilterDeviceId.set("167"); + EXPECT_TRUE(NEO::isAllowedDeviceId(0x167)); +}