1
+ /*
2
+ * Copyright (C) 2022 Intel Corporation
3
+ *
4
+ * SPDX-License-Identifier: MIT
5
+ *
6
+ */
7
+
8
+ #include " shared/source/os_interface/windows/gdi_interface.h"
9
+ #include " shared/source/os_interface/windows/hw_device_id.h"
10
+ #include " shared/source/os_interface/windows/os_context_win.h"
11
+ #include " shared/source/os_interface/windows/os_environment_win.h"
12
+ #include " shared/source/os_interface/windows/wddm/wddm.h"
13
+ #include " shared/test/common/helpers/engine_descriptor_helper.h"
14
+ #include " shared/test/common/mocks/mock_device.h"
15
+ #include " shared/test/common/mocks/mock_driver_info.h"
16
+ #include " shared/test/common/mocks/mock_driver_model.h"
17
+ #include " shared/test/common/test_macros/hw_test.h"
18
+
19
+ #include " level_zero/core/source/driver/driver_handle_imp.h"
20
+ #include " level_zero/core/test/unit_tests/fixtures/device_fixture.h"
21
+ #include " level_zero/core/test/unit_tests/mocks/mock_built_ins.h"
22
+ #include " level_zero/core/test/unit_tests/mocks/mock_driver_handle.h"
23
+
24
+ #include " gtest/gtest.h"
25
+
26
+ namespace L0 {
27
+ namespace ult {
28
+
29
+ #define ADAPTER_HANDLE_WDDM_FAKE (static_cast <D3DKMT_HANDLE>(0x40001234 ))
30
+
31
+ struct CloseAdapterMock {
32
+ static NTSTATUS (APIENTRY closeAdapter)(
33
+ const D3DKMT_CLOSEADAPTER *closeAdapter) {
34
+ return STATUS_SUCCESS;
35
+ }
36
+ };
37
+
38
+ struct MockHwDeviceIdWddm : public HwDeviceIdWddm {
39
+ using HwDeviceIdWddm::osEnvironment;
40
+ MockHwDeviceIdWddm (D3DKMT_HANDLE adapterIn, LUID adapterLuidIn, OsEnvironment *osEnvironmentIn, std::unique_ptr<UmKmDataTranslator> umKmDataTranslator) : HwDeviceIdWddm(adapterIn, adapterLuidIn, osEnvironmentIn, std::move(umKmDataTranslator)) {}
41
+ };
42
+
43
+ class MockDriverModelWDDMLUID : public NEO ::Wddm {
44
+ public:
45
+ MockDriverModelWDDMLUID (std::unique_ptr<HwDeviceIdWddm> &&hwDeviceId, RootDeviceEnvironment &rootDeviceEnvironment) : Wddm(std::move(hwDeviceId), rootDeviceEnvironment) {
46
+ }
47
+ bool init () {
48
+ return true ;
49
+ }
50
+
51
+ bool isDriverAvaliable () override {
52
+ return false ;
53
+ }
54
+
55
+ bool skipResourceCleanup () {
56
+ return true ;
57
+ }
58
+
59
+ MockDriverModelWDDMLUID (RootDeviceEnvironment &rootDeviceEnvironment) : Wddm(std::make_unique<MockHwDeviceIdWddm>(ADAPTER_HANDLE_WDDM_FAKE, LUID{0x12 , 0x1234 }, rootDeviceEnvironment.executionEnvironment.osEnvironment.get(), std::make_unique<UmKmDataTranslator>()), rootDeviceEnvironment) {
60
+ if (!rootDeviceEnvironment.executionEnvironment .osEnvironment .get ()) {
61
+ rootDeviceEnvironment.executionEnvironment .osEnvironment = std::make_unique<OsEnvironmentWin>();
62
+ }
63
+ static_cast <MockHwDeviceIdWddm *>(this ->hwDeviceId .get ())->osEnvironment = rootDeviceEnvironment.executionEnvironment .osEnvironment .get ();
64
+ OsEnvironmentWin *osEnvWin = reinterpret_cast <OsEnvironmentWin *>(static_cast <MockHwDeviceIdWddm *>(this ->hwDeviceId .get ())->osEnvironment );
65
+ osEnvWin->gdi .get ()->closeAdapter .mFunc = CloseAdapterMock::closeAdapter;
66
+ }
67
+ };
68
+
69
+ class MockOsContextWin : public OsContextWin {
70
+ public:
71
+ MockOsContextWin (MockDriverModelWDDMLUID &wddm, uint32_t contextId, const EngineDescriptor &engineDescriptor)
72
+ : OsContextWin(wddm, contextId, engineDescriptor) {}
73
+ };
74
+
75
+ using LuidDeviceTest = Test<DeviceFixture>;
76
+
77
+ TEST_F (LuidDeviceTest, givenOsContextWinAndGetLUIDArrayThenLUIDisValid) {
78
+ auto luidMock = new MockDriverModelWDDMLUID (*neoDevice->executionEnvironment ->rootDeviceEnvironments [0 ]);
79
+ auto defaultEngine = defaultHwInfo->capabilityTable .defaultEngineType ;
80
+ OsContextWin osContext (*luidMock, 0u , EngineDescriptorHelper::getDefaultDescriptor ({defaultEngine, EngineUsage::Regular}));
81
+ std::vector<uint8_t > luidData;
82
+ size_t arraySize = 8 ;
83
+ osContext.getDeviceLuidArray (luidData, arraySize);
84
+ uint64_t luid = 0 ;
85
+ memcpy_s (&luid, sizeof (uint64_t ), luidData.data (), sizeof (uint8_t ) * luidData.size ());
86
+ EXPECT_NE (luid, (uint64_t )0 );
87
+ delete luidMock;
88
+ }
89
+
90
+ TEST_F (LuidDeviceTest, givenLuidDevicePropertiesStructureAndWDDMDriverTypeThenSuccessReturned) {
91
+ auto defaultEngine = defaultHwInfo->capabilityTable .defaultEngineType ;
92
+ auto luidMock = new MockDriverModelWDDMLUID (*neoDevice->executionEnvironment ->rootDeviceEnvironments [0 ]);
93
+ neoDevice->executionEnvironment ->rootDeviceEnvironments [0 ]->osInterface .reset (new NEO::OSInterface ());
94
+ neoDevice->executionEnvironment ->rootDeviceEnvironments [0 ]->osInterface ->setDriverModel (std::unique_ptr<DriverModel>(luidMock));
95
+ MockOsContextWin mockContext (*luidMock, 0u , EngineDescriptorHelper::getDefaultDescriptor ({defaultEngine, EngineUsage::Regular}));
96
+ auto &deviceRegularEngines = neoDevice->getRegularEngineGroups ();
97
+ auto &deviceEngine = deviceRegularEngines[0 ].engines [0 ];
98
+ auto csr = deviceEngine.commandStreamReceiver ;
99
+ csr->setupContext (mockContext);
100
+ ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES};
101
+ ze_device_luid_ext_properties_t deviceLuidProperties = {ZE_STRUCTURE_TYPE_DEVICE_LUID_EXT_PROPERTIES};
102
+ deviceProperties.pNext = &deviceLuidProperties;
103
+ ze_result_t result = device->getProperties (&deviceProperties);
104
+ EXPECT_EQ (result, ZE_RESULT_SUCCESS);
105
+ uint64_t luid = 0 ;
106
+ LUID adapterLuid{0x12 , 0x1234 };
107
+ memcpy_s (&luid, sizeof (uint64_t ), &deviceLuidProperties.luid , sizeof (deviceLuidProperties.luid ));
108
+ uint32_t lowLUID = luid & 0xFFFFFFFF ;
109
+ uint32_t highLUID = ((luid & 0xFFFFFFFF00000000 ) >> 32 );
110
+ EXPECT_EQ (lowLUID, (uint32_t )adapterLuid.LowPart );
111
+ EXPECT_EQ (highLUID, (uint32_t )adapterLuid.HighPart );
112
+ EXPECT_NE (deviceLuidProperties.nodeMask , (uint32_t )0 );
113
+ }
114
+
115
+ TEST_F (LuidDeviceTest, givenLuidDevicePropertiesStructureAndDRMDriverTypeThenUnsupportedReturned) {
116
+ neoDevice->executionEnvironment ->rootDeviceEnvironments [0 ]->osInterface .reset (new NEO::OSInterface ());
117
+ neoDevice->executionEnvironment ->rootDeviceEnvironments [0 ]->osInterface ->setDriverModel (std::make_unique<NEO::MockDriverModelDRM>());
118
+ ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES};
119
+ ze_device_luid_ext_properties_t deviceLuidProperties = {ZE_STRUCTURE_TYPE_DEVICE_LUID_EXT_PROPERTIES};
120
+ deviceProperties.pNext = &deviceLuidProperties;
121
+ ze_result_t result = device->getProperties (&deviceProperties);
122
+ EXPECT_EQ (result, ZE_RESULT_ERROR_UNSUPPORTED_FEATURE);
123
+ }
124
+
125
+ TEST_F (LuidDeviceTest, givenLuidDevicePropertiesStructureAndAndNoOsInterfaceThenUninitReturned) {
126
+ neoDevice->executionEnvironment ->rootDeviceEnvironments [0 ]->osInterface .reset (nullptr );
127
+ ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES};
128
+ ze_device_luid_ext_properties_t deviceLuidProperties = {ZE_STRUCTURE_TYPE_DEVICE_LUID_EXT_PROPERTIES};
129
+ deviceProperties.pNext = &deviceLuidProperties;
130
+ ze_result_t result = device->getProperties (&deviceProperties);
131
+ EXPECT_EQ (result, ZE_RESULT_ERROR_UNINITIALIZED);
132
+ }
133
+
134
+ } // namespace ult
135
+ } // namespace L0
0 commit comments