1 /*
2  * Copyright (C) 2018-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #include "shared/source/aub_mem_dump/page_table_entry_bits.h"
9 #include "shared/source/helpers/engine_node_helper.h"
10 #include "shared/source/helpers/hardware_context_controller.h"
11 #include "shared/source/helpers/hw_helper.h"
12 #include "shared/source/os_interface/os_context.h"
13 #include "shared/test/common/fixtures/aub_command_stream_receiver_fixture.h"
14 #include "shared/test/common/helpers/debug_manager_state_restore.h"
15 #include "shared/test/common/helpers/engine_descriptor_helper.h"
16 #include "shared/test/common/mocks/mock_aub_center.h"
17 #include "shared/test/common/mocks/mock_aub_csr.h"
18 #include "shared/test/common/mocks/mock_aub_file_stream.h"
19 #include "shared/test/common/mocks/mock_aub_manager.h"
20 #include "shared/test/common/mocks/mock_aub_subcapture_manager.h"
21 #include "shared/test/common/mocks/mock_csr.h"
22 #include "shared/test/common/mocks/mock_execution_environment.h"
23 #include "shared/test/common/mocks/mock_gmm.h"
24 #include "shared/test/common/mocks/mock_memory_manager.h"
25 #include "shared/test/common/mocks/mock_os_context.h"
26 #include "shared/test/common/mocks/ult_device_factory.h"
27 #include "shared/test/common/test_macros/test.h"
28 #include "shared/test/unit_test/fixtures/mock_aub_center_fixture.h"
29 
30 #include "third_party/aub_stream/headers/aubstream.h"
31 
32 using namespace NEO;
33 
34 using AubCommandStreamReceiverTests = Test<AubCommandStreamReceiverFixture>;
35 
36 struct FlatBatchBufferHelperAubTests : AubCommandStreamReceiverTests {
SetUpFlatBatchBufferHelperAubTests37     void SetUp() override {
38         DebugManager.flags.FlattenBatchBufferForAUBDump.set(true);
39         AubCommandStreamReceiverTests::SetUp();
40     }
41 
42     DebugManagerStateRestore restore;
43 };
44 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenForcedBatchBufferFlatteningInImmediateDispatchModeThenNewCombinedBatchBufferIsCreated)45 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenForcedBatchBufferFlatteningInImmediateDispatchModeThenNewCombinedBatchBufferIsCreated) {
46     std::unique_ptr<AUBCommandStreamReceiverHw<FamilyType>> aubCsr(new AUBCommandStreamReceiverHw<FamilyType>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()));
47     std::unique_ptr<MemoryManager> memoryManager(new OsAgnosticMemoryManager(*pDevice->executionEnvironment));
48     auto flatBatchBufferHelper = new FlatBatchBufferHelperHw<FamilyType>(*pDevice->executionEnvironment);
49     aubCsr->overwriteFlatBatchBufferHelper(flatBatchBufferHelper);
50 
51     auto chainedBatchBuffer = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{pDevice->getRootDeviceIndex(), 128u});
52     auto otherAllocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{pDevice->getRootDeviceIndex(), 128u});
53     ASSERT_NE(nullptr, chainedBatchBuffer);
54 
55     GraphicsAllocation *commandBuffer = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{pDevice->getRootDeviceIndex(), 128u});
56     ASSERT_NE(nullptr, commandBuffer);
57     LinearStream cs(commandBuffer);
58 
59     BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 128u, chainedBatchBuffer, false, false, QueueThrottle::MEDIUM, QueueSliceCount::defaultSliceCount, cs.getUsed(), &cs, nullptr, false};
60 
61     size_t sizeBatchBuffer = 0xffffu;
62 
63     std::unique_ptr<GraphicsAllocation, std::function<void(GraphicsAllocation *)>> flatBatchBuffer(
64         aubCsr->getFlatBatchBufferHelper().flattenBatchBuffer(aubCsr->getRootDeviceIndex(), batchBuffer, sizeBatchBuffer, DispatchMode::ImmediateDispatch, pDevice->getDeviceBitfield()),
65         [&](GraphicsAllocation *ptr) { memoryManager->freeGraphicsMemory(ptr); });
66     EXPECT_NE(nullptr, flatBatchBuffer->getUnderlyingBuffer());
67     EXPECT_EQ(alignUp(128u + 128u, MemoryConstants::pageSize), sizeBatchBuffer);
68 
69     memoryManager->freeGraphicsMemory(commandBuffer);
70     memoryManager->freeGraphicsMemory(chainedBatchBuffer);
71     memoryManager->freeGraphicsMemory(otherAllocation);
72 }
73 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenForcedBatchBufferInImmediateDispatchModeAndNoChainedBatchBufferThenCombinedBatchBufferIsNotCreated)74 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenForcedBatchBufferInImmediateDispatchModeAndNoChainedBatchBufferThenCombinedBatchBufferIsNotCreated) {
75     std::unique_ptr<AUBCommandStreamReceiverHw<FamilyType>> aubCsr(new AUBCommandStreamReceiverHw<FamilyType>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()));
76     std::unique_ptr<MemoryManager> memoryManager(new OsAgnosticMemoryManager(*pDevice->executionEnvironment));
77     auto flatBatchBufferHelper = new FlatBatchBufferHelperHw<FamilyType>(*pDevice->executionEnvironment);
78     aubCsr->overwriteFlatBatchBufferHelper(flatBatchBufferHelper);
79 
80     GraphicsAllocation *commandBuffer = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{pDevice->getRootDeviceIndex(), MemoryConstants::pageSize});
81     ASSERT_NE(nullptr, commandBuffer);
82     LinearStream cs(commandBuffer);
83 
84     BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 128u, nullptr, false, false, QueueThrottle::MEDIUM, QueueSliceCount::defaultSliceCount, cs.getUsed(), &cs, nullptr, false};
85 
86     size_t sizeBatchBuffer = 0xffffu;
87 
88     std::unique_ptr<GraphicsAllocation, std::function<void(GraphicsAllocation *)>> flatBatchBuffer(
89         aubCsr->getFlatBatchBufferHelper().flattenBatchBuffer(aubCsr->getRootDeviceIndex(), batchBuffer, sizeBatchBuffer, DispatchMode::ImmediateDispatch, pDevice->getDeviceBitfield()),
90         [&](GraphicsAllocation *ptr) { memoryManager->freeGraphicsMemory(ptr); });
91     EXPECT_EQ(nullptr, flatBatchBuffer.get());
92     EXPECT_EQ(0xffffu, sizeBatchBuffer);
93 
94     memoryManager->freeGraphicsMemory(commandBuffer);
95 }
96 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenForcedBatchBufferAndNotImmediateOrBatchedDispatchModeThenCombinedBatchBufferIsNotCreated)97 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenForcedBatchBufferAndNotImmediateOrBatchedDispatchModeThenCombinedBatchBufferIsNotCreated) {
98     std::unique_ptr<AUBCommandStreamReceiverHw<FamilyType>> aubCsr(new AUBCommandStreamReceiverHw<FamilyType>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()));
99     std::unique_ptr<MemoryManager> memoryManager(new OsAgnosticMemoryManager(*pDevice->executionEnvironment));
100     auto flatBatchBufferHelper = new FlatBatchBufferHelperHw<FamilyType>(*pDevice->executionEnvironment);
101     aubCsr->overwriteFlatBatchBufferHelper(flatBatchBufferHelper);
102 
103     auto chainedBatchBuffer = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{pDevice->getRootDeviceIndex(), MemoryConstants::pageSize});
104     auto otherAllocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{pDevice->getRootDeviceIndex(), MemoryConstants::pageSize});
105     ASSERT_NE(nullptr, chainedBatchBuffer);
106 
107     GraphicsAllocation *commandBuffer = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{pDevice->getRootDeviceIndex(), MemoryConstants::pageSize});
108     ASSERT_NE(nullptr, commandBuffer);
109     LinearStream cs(commandBuffer);
110 
111     BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 128u, chainedBatchBuffer, false, false, QueueThrottle::MEDIUM, QueueSliceCount::defaultSliceCount, cs.getUsed(), &cs, nullptr, false};
112 
113     size_t sizeBatchBuffer = 0xffffu;
114 
115     std::unique_ptr<GraphicsAllocation, std::function<void(GraphicsAllocation *)>> flatBatchBuffer(
116         aubCsr->getFlatBatchBufferHelper().flattenBatchBuffer(aubCsr->getRootDeviceIndex(), batchBuffer, sizeBatchBuffer, DispatchMode::AdaptiveDispatch, pDevice->getDeviceBitfield()),
117         [&](GraphicsAllocation *ptr) { memoryManager->freeGraphicsMemory(ptr); });
118     EXPECT_EQ(nullptr, flatBatchBuffer.get());
119     EXPECT_EQ(0xffffu, sizeBatchBuffer);
120 
121     memoryManager->freeGraphicsMemory(commandBuffer);
122     memoryManager->freeGraphicsMemory(chainedBatchBuffer);
123     memoryManager->freeGraphicsMemory(otherAllocation);
124 }
125 
HWTEST_F(FlatBatchBufferHelperAubTests,givenAubCommandStreamReceiverWhenRegisterCommandChunkIsCalledThenNewChunkIsAddedToTheList)126 HWTEST_F(FlatBatchBufferHelperAubTests, givenAubCommandStreamReceiverWhenRegisterCommandChunkIsCalledThenNewChunkIsAddedToTheList) {
127     typedef typename FamilyType::MI_BATCH_BUFFER_START MI_BATCH_BUFFER_START;
128 
129     auto aubExecutionEnvironment = getEnvironment<AUBCommandStreamReceiverHw<FamilyType>>(false, true, true);
130     auto aubCsr = aubExecutionEnvironment->template getCsr<AUBCommandStreamReceiverHw<FamilyType>>();
131     LinearStream cs(aubExecutionEnvironment->commandBuffer);
132 
133     BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 128u, nullptr, false, false, QueueThrottle::MEDIUM, QueueSliceCount::defaultSliceCount, cs.getUsed(), &cs, nullptr, false};
134 
135     aubCsr->getFlatBatchBufferHelper().registerCommandChunk(batchBuffer, sizeof(MI_BATCH_BUFFER_START));
136     ASSERT_EQ(1u, aubCsr->getFlatBatchBufferHelper().getCommandChunkList().size());
137     EXPECT_EQ(128u + sizeof(MI_BATCH_BUFFER_START), aubCsr->getFlatBatchBufferHelper().getCommandChunkList()[0].endOffset);
138 
139     CommandChunk chunk;
140     chunk.endOffset = 0x123;
141     aubCsr->getFlatBatchBufferHelper().registerCommandChunk(chunk);
142 
143     ASSERT_EQ(2u, aubCsr->getFlatBatchBufferHelper().getCommandChunkList().size());
144     EXPECT_EQ(0x123u, aubCsr->getFlatBatchBufferHelper().getCommandChunkList()[1].endOffset);
145 }
146 
HWTEST_F(FlatBatchBufferHelperAubTests,givenAubCommandStreamReceiverWhenRemovePatchInfoDataIsCalledThenElementIsRemovedFromPatchInfoList)147 HWTEST_F(FlatBatchBufferHelperAubTests, givenAubCommandStreamReceiverWhenRemovePatchInfoDataIsCalledThenElementIsRemovedFromPatchInfoList) {
148     auto aubExecutionEnvironment = getEnvironment<AUBCommandStreamReceiverHw<FamilyType>>(false, true, true);
149     auto aubCsr = aubExecutionEnvironment->template getCsr<AUBCommandStreamReceiverHw<FamilyType>>();
150 
151     PatchInfoData patchInfoData(0xA000, 0x0, PatchInfoAllocationType::KernelArg, 0xB000, 0x0, PatchInfoAllocationType::Default);
152     aubCsr->getFlatBatchBufferHelper().setPatchInfoData(patchInfoData);
153     EXPECT_EQ(1u, aubCsr->getFlatBatchBufferHelper().getPatchInfoCollection().size());
154 
155     EXPECT_TRUE(aubCsr->getFlatBatchBufferHelper().removePatchInfoData(0xC000));
156     EXPECT_EQ(1u, aubCsr->getFlatBatchBufferHelper().getPatchInfoCollection().size());
157 
158     EXPECT_TRUE(aubCsr->getFlatBatchBufferHelper().removePatchInfoData(0xB000));
159     EXPECT_EQ(0u, aubCsr->getFlatBatchBufferHelper().getPatchInfoCollection().size());
160 }
161 
HWTEST_F(FlatBatchBufferHelperAubTests,givenAubCommandStreamReceiverWhenAddGucStartMessageIsCalledThenBatchBufferAddressIsStoredInPatchInfoCollection)162 HWTEST_F(FlatBatchBufferHelperAubTests, givenAubCommandStreamReceiverWhenAddGucStartMessageIsCalledThenBatchBufferAddressIsStoredInPatchInfoCollection) {
163     DebugManager.flags.AddPatchInfoCommentsForAUBDump.set(true);
164 
165     auto aubExecutionEnvironment = getEnvironment<AUBCommandStreamReceiverHw<FamilyType>>(false, false, true);
166     auto aubCsr = aubExecutionEnvironment->template getCsr<AUBCommandStreamReceiverHw<FamilyType>>();
167     LinearStream cs(aubExecutionEnvironment->commandBuffer);
168 
169     std::unique_ptr<char> batchBuffer(new char[1024]);
170     aubCsr->addGUCStartMessage(static_cast<uint64_t>(reinterpret_cast<std::uintptr_t>(batchBuffer.get())));
171 
172     auto &patchInfoCollection = aubCsr->getFlatBatchBufferHelper().getPatchInfoCollection();
173     ASSERT_EQ(1u, patchInfoCollection.size());
174     EXPECT_EQ(patchInfoCollection[0].sourceAllocation, reinterpret_cast<uint64_t>(batchBuffer.get()));
175     EXPECT_EQ(patchInfoCollection[0].targetType, PatchInfoAllocationType::GUCStartMessage);
176 }
177 
HWTEST_F(FlatBatchBufferHelperAubTests,givenAubCommandStreamReceiverWhenForcedBatchBufferFlatteningInBatchedDispatchModeThenNewCombinedBatchBufferIsCreated)178 HWTEST_F(FlatBatchBufferHelperAubTests, givenAubCommandStreamReceiverWhenForcedBatchBufferFlatteningInBatchedDispatchModeThenNewCombinedBatchBufferIsCreated) {
179     DebugManager.flags.AddPatchInfoCommentsForAUBDump.set(true);
180     DebugManager.flags.CsrDispatchMode.set(static_cast<uint32_t>(DispatchMode::BatchedDispatch));
181 
182     auto aubExecutionEnvironment = getEnvironment<AUBCommandStreamReceiverHw<FamilyType>>(false, true, true);
183     auto aubCsr = aubExecutionEnvironment->template getCsr<AUBCommandStreamReceiverHw<FamilyType>>();
184     auto memoryManager = aubExecutionEnvironment->executionEnvironment->memoryManager.get();
185     LinearStream cs(aubExecutionEnvironment->commandBuffer);
186 
187     CommandChunk chunk1;
188     CommandChunk chunk2;
189     CommandChunk chunk3;
190 
191     std::unique_ptr<char> commands1(new char[0x100u]);
192     commands1.get()[0] = 0x1;
193     chunk1.baseAddressCpu = chunk1.baseAddressGpu = reinterpret_cast<uint64_t>(commands1.get());
194     chunk1.startOffset = 0u;
195     chunk1.endOffset = 0x50u;
196 
197     std::unique_ptr<char> commands2(new char[0x100u]);
198     commands2.get()[0] = 0x2;
199     chunk2.baseAddressCpu = chunk2.baseAddressGpu = reinterpret_cast<uint64_t>(commands2.get());
200     chunk2.startOffset = 0u;
201     chunk2.endOffset = 0x50u;
202     aubCsr->getFlatBatchBufferHelper().registerBatchBufferStartAddress(reinterpret_cast<uint64_t>(commands2.get() + 0x40), reinterpret_cast<uint64_t>(commands1.get()));
203 
204     std::unique_ptr<char> commands3(new char[0x100u]);
205     commands3.get()[0] = 0x3;
206     chunk3.baseAddressCpu = chunk3.baseAddressGpu = reinterpret_cast<uint64_t>(commands3.get());
207     chunk3.startOffset = 0u;
208     chunk3.endOffset = 0x50u;
209     aubCsr->getFlatBatchBufferHelper().registerBatchBufferStartAddress(reinterpret_cast<uint64_t>(commands3.get() + 0x40), reinterpret_cast<uint64_t>(commands2.get()));
210 
211     aubCsr->getFlatBatchBufferHelper().registerCommandChunk(chunk1);
212     aubCsr->getFlatBatchBufferHelper().registerCommandChunk(chunk2);
213     aubCsr->getFlatBatchBufferHelper().registerCommandChunk(chunk3);
214 
215     ASSERT_EQ(3u, aubCsr->getFlatBatchBufferHelper().getCommandChunkList().size());
216 
217     PatchInfoData patchInfoData1(0xAAAu, 0xAu, PatchInfoAllocationType::IndirectObjectHeap, chunk1.baseAddressGpu, 0x10, PatchInfoAllocationType::Default);
218     PatchInfoData patchInfoData2(0xBBBu, 0xAu, PatchInfoAllocationType::IndirectObjectHeap, chunk1.baseAddressGpu, 0x60, PatchInfoAllocationType::Default);
219     PatchInfoData patchInfoData3(0xCCCu, 0xAu, PatchInfoAllocationType::IndirectObjectHeap, 0x0, 0x10, PatchInfoAllocationType::Default);
220 
221     aubCsr->getFlatBatchBufferHelper().setPatchInfoData(patchInfoData1);
222     aubCsr->getFlatBatchBufferHelper().setPatchInfoData(patchInfoData2);
223     aubCsr->getFlatBatchBufferHelper().setPatchInfoData(patchInfoData3);
224 
225     ASSERT_EQ(3u, aubCsr->getFlatBatchBufferHelper().getPatchInfoCollection().size());
226 
227     BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 128u, nullptr, false, false, QueueThrottle::MEDIUM, QueueSliceCount::defaultSliceCount, cs.getUsed(), &cs, nullptr, false};
228 
229     size_t sizeBatchBuffer = 0u;
230 
231     std::unique_ptr<GraphicsAllocation, std::function<void(GraphicsAllocation *)>> flatBatchBuffer(
232         aubCsr->getFlatBatchBufferHelper().flattenBatchBuffer(aubCsr->getRootDeviceIndex(), batchBuffer, sizeBatchBuffer, DispatchMode::BatchedDispatch, pDevice->getDeviceBitfield()),
233         [&](GraphicsAllocation *ptr) { memoryManager->freeGraphicsMemory(ptr); });
234 
235     EXPECT_NE(nullptr, flatBatchBuffer.get());
236     EXPECT_EQ(alignUp(0x50u + 0x40u + 0x40u + CSRequirements::csOverfetchSize, 0x1000u), sizeBatchBuffer);
237 
238     ASSERT_EQ(1u, aubCsr->getFlatBatchBufferHelper().getPatchInfoCollection().size());
239     EXPECT_EQ(0xAAAu, aubCsr->getFlatBatchBufferHelper().getPatchInfoCollection()[0].sourceAllocation);
240 
241     EXPECT_EQ(0u, aubCsr->getFlatBatchBufferHelper().getCommandChunkList().size());
242 
243     EXPECT_EQ(0x3, static_cast<char *>(flatBatchBuffer->getUnderlyingBuffer())[0]);
244     EXPECT_EQ(0x2, static_cast<char *>(flatBatchBuffer->getUnderlyingBuffer())[0x40]);
245     EXPECT_EQ(0x1, static_cast<char *>(flatBatchBuffer->getUnderlyingBuffer())[0x40 + 0x40]);
246 }
247 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenDefaultDebugConfigThenExpectFlattenBatchBufferIsNotCalled)248 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenDefaultDebugConfigThenExpectFlattenBatchBufferIsNotCalled) {
249     auto aubExecutionEnvironment = getEnvironment<MockAubCsr<FamilyType>>(true, true, true);
250     auto aubCsr = aubExecutionEnvironment->template getCsr<MockAubCsr<FamilyType>>();
251     LinearStream cs(aubExecutionEnvironment->commandBuffer);
252 
253     auto mockHelper = new MockFlatBatchBufferHelper<FamilyType>(*aubExecutionEnvironment->executionEnvironment);
254     aubCsr->overwriteFlatBatchBufferHelper(mockHelper);
255 
256     BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 0, nullptr, false, false, QueueThrottle::MEDIUM, QueueSliceCount::defaultSliceCount, cs.getUsed(), &cs, nullptr, false};
257     ResidencyContainer allocationsForResidency = {};
258 
259     aubCsr->flush(batchBuffer, allocationsForResidency);
260 
261     EXPECT_EQ(0u, mockHelper->flattenBatchBufferCalled);
262 }
263 
HWTEST_F(AubCommandStreamReceiverTests,givenNoCpuPtrAndNotLockableAllocationWhenGettingParametersForWriteThenLockResourceIsNotCalled)264 HWTEST_F(AubCommandStreamReceiverTests, givenNoCpuPtrAndNotLockableAllocationWhenGettingParametersForWriteThenLockResourceIsNotCalled) {
265     auto aubExecutionEnvironment = getEnvironment<MockAubCsr<FamilyType>>(true, true, true);
266     auto aubCsr = aubExecutionEnvironment->template getCsr<MockAubCsr<FamilyType>>();
267     auto mockMemoryManager = new MockMemoryManager();
268 
269     auto memoryManagerBackup = aubExecutionEnvironment->executionEnvironment->memoryManager.release();
270     aubExecutionEnvironment->executionEnvironment->memoryManager.reset(mockMemoryManager);
271 
272     EXPECT_EQ(0u, mockMemoryManager->lockResourceCalled);
273 
274     constexpr uint64_t initGpuAddress = 1234;
275     constexpr size_t initSize = 10;
276     MockGraphicsAllocation allocation(nullptr, initGpuAddress, initSize);
277     allocation.setAllocationType(GraphicsAllocation::AllocationType::BUFFER);
278     allocation.overrideMemoryPool(MemoryPool::LocalMemory);
279 
280     aubExecutionEnvironment->executionEnvironment->rootDeviceEnvironments[0]->initGmm();
281     MockGmm mockGmm(aubExecutionEnvironment->executionEnvironment->rootDeviceEnvironments[0]->getGmmClientContext(), nullptr, initSize, initSize, false, false, false, {});
282     mockGmm.resourceParams.Flags.Info.NotLockable = true;
283     allocation.setDefaultGmm(&mockGmm);
284 
285     uint64_t gpuAddress{};
286     void *cpuAddress{};
287     size_t size{};
288 
289     aubCsr->getParametersForWriteMemory(allocation, gpuAddress, cpuAddress, size);
290 
291     EXPECT_EQ(nullptr, cpuAddress);
292     EXPECT_EQ(initGpuAddress, gpuAddress);
293     EXPECT_EQ(initSize, size);
294 
295     EXPECT_EQ(0u, mockMemoryManager->lockResourceCalled);
296     aubExecutionEnvironment->executionEnvironment->memoryManager.reset(memoryManagerBackup);
297 }
298 
HWTEST_F(AubCommandStreamReceiverTests,givenNoCpuPtrAndLockableAllocationWhenGettingParametersForWriteThenLockResourceIsCalled)299 HWTEST_F(AubCommandStreamReceiverTests, givenNoCpuPtrAndLockableAllocationWhenGettingParametersForWriteThenLockResourceIsCalled) {
300     auto aubExecutionEnvironment = getEnvironment<MockAubCsr<FamilyType>>(true, true, true);
301     auto aubCsr = aubExecutionEnvironment->template getCsr<MockAubCsr<FamilyType>>();
302     auto mockMemoryManager = new MockMemoryManager();
303 
304     auto memoryManagerBackup = aubExecutionEnvironment->executionEnvironment->memoryManager.release();
305     aubExecutionEnvironment->executionEnvironment->memoryManager.reset(mockMemoryManager);
306 
307     EXPECT_EQ(0u, mockMemoryManager->lockResourceCalled);
308 
309     constexpr uint64_t initGpuAddress = 1234;
310     constexpr size_t initSize = 10;
311     MockGraphicsAllocation allocation(nullptr, initGpuAddress, initSize);
312     allocation.setAllocationType(GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY);
313     allocation.overrideMemoryPool(MemoryPool::LocalMemory);
314 
315     aubExecutionEnvironment->executionEnvironment->rootDeviceEnvironments[0]->initGmm();
316     MockGmm mockGmm(aubExecutionEnvironment->executionEnvironment->rootDeviceEnvironments[0]->getGmmClientContext(), nullptr, initSize, initSize, false, false, false, {});
317     mockGmm.resourceParams.Flags.Info.NotLockable = false;
318     allocation.setDefaultGmm(&mockGmm);
319 
320     uint64_t gpuAddress{};
321     void *cpuAddress{};
322     size_t size{};
323 
324     aubCsr->getParametersForWriteMemory(allocation, gpuAddress, cpuAddress, size);
325 
326     EXPECT_EQ(nullptr, cpuAddress);
327     EXPECT_EQ(initGpuAddress, gpuAddress);
328     EXPECT_EQ(initSize, size);
329 
330     EXPECT_EQ(1u, mockMemoryManager->lockResourceCalled);
331     aubExecutionEnvironment->executionEnvironment->memoryManager.reset(memoryManagerBackup);
332 }
333 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenForcedFlattenBatchBufferAndImmediateDispatchModeThenExpectFlattenBatchBufferIsCalled)334 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenForcedFlattenBatchBufferAndImmediateDispatchModeThenExpectFlattenBatchBufferIsCalled) {
335     DebugManagerStateRestore dbgRestore;
336     DebugManager.flags.FlattenBatchBufferForAUBDump.set(true);
337     DebugManager.flags.CsrDispatchMode.set(static_cast<uint32_t>(DispatchMode::ImmediateDispatch));
338 
339     auto aubExecutionEnvironment = getEnvironment<MockAubCsr<FamilyType>>(true, true, true);
340     auto aubCsr = aubExecutionEnvironment->template getCsr<MockAubCsr<FamilyType>>();
341     auto allocationsForResidency = aubCsr->getResidencyAllocations();
342     LinearStream cs(aubExecutionEnvironment->commandBuffer);
343 
344     auto mockHelper = new MockFlatBatchBufferHelper<FamilyType>(*aubExecutionEnvironment->executionEnvironment);
345     aubCsr->overwriteFlatBatchBufferHelper(mockHelper);
346 
347     auto chainedBatchBuffer = aubExecutionEnvironment->executionEnvironment->memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{pDevice->getRootDeviceIndex(), MemoryConstants::pageSize});
348     ASSERT_NE(nullptr, chainedBatchBuffer);
349 
350     BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 128u, chainedBatchBuffer, false, false, QueueThrottle::MEDIUM, QueueSliceCount::defaultSliceCount, cs.getUsed(), &cs, nullptr, false};
351 
352     aubCsr->makeResident(*chainedBatchBuffer);
353 
354     std::unique_ptr<GraphicsAllocation, std::function<void(GraphicsAllocation *)>> ptr(
355         aubExecutionEnvironment->executionEnvironment->memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{pDevice->getRootDeviceIndex(), MemoryConstants::pageSize}),
356         [&](GraphicsAllocation *ptr) { aubExecutionEnvironment->executionEnvironment->memoryManager->freeGraphicsMemory(ptr); });
357 
358     auto expectedAllocation = ptr.get();
359     mockHelper->flattenBatchBufferResult = ptr.release();
360 
361     aubCsr->flush(batchBuffer, allocationsForResidency);
362 
363     EXPECT_EQ(batchBuffer.commandBufferAllocation, expectedAllocation);
364 
365     aubExecutionEnvironment->executionEnvironment->memoryManager->freeGraphicsMemory(chainedBatchBuffer);
366 
367     EXPECT_EQ(1u, mockHelper->flattenBatchBufferCalled);
368 }
369 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenForcedFlattenBatchBufferAndImmediateDispatchModeAndThereIsNoChainedBatchBufferThenExpectFlattenBatchBufferIsCalledAnyway)370 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenForcedFlattenBatchBufferAndImmediateDispatchModeAndThereIsNoChainedBatchBufferThenExpectFlattenBatchBufferIsCalledAnyway) {
371     DebugManagerStateRestore dbgRestore;
372     DebugManager.flags.FlattenBatchBufferForAUBDump.set(true);
373     DebugManager.flags.CsrDispatchMode.set(static_cast<uint32_t>(DispatchMode::ImmediateDispatch));
374 
375     auto aubExecutionEnvironment = getEnvironment<MockAubCsr<FamilyType>>(true, true, true);
376     auto aubCsr = aubExecutionEnvironment->template getCsr<MockAubCsr<FamilyType>>();
377     auto allocationsForResidency = aubCsr->getResidencyAllocations();
378     LinearStream cs(aubExecutionEnvironment->commandBuffer);
379 
380     auto mockHelper = new MockFlatBatchBufferHelper<FamilyType>(*aubExecutionEnvironment->executionEnvironment);
381     mockHelper->flattenBatchBufferParamsPassed.clear();
382 
383     aubCsr->overwriteFlatBatchBufferHelper(mockHelper);
384 
385     BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 128u, nullptr, false, false, QueueThrottle::MEDIUM, QueueSliceCount::defaultSliceCount, cs.getUsed(), &cs, nullptr, false};
386 
387     aubCsr->flush(batchBuffer, allocationsForResidency);
388 
389     EXPECT_EQ(1u, mockHelper->flattenBatchBufferCalled);
390     EXPECT_EQ(aubCsr->getRootDeviceIndex(), mockHelper->flattenBatchBufferParamsPassed[0].rootDeviceIndex);
391     EXPECT_EQ(aubCsr->getOsContext().getDeviceBitfield().to_ulong(), mockHelper->flattenBatchBufferParamsPassed[0].deviceBitfield.to_ulong());
392 }
393 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenForcedFlattenBatchBufferAndBatchedDispatchModeThenExpectFlattenBatchBufferIsCalledAnyway)394 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenForcedFlattenBatchBufferAndBatchedDispatchModeThenExpectFlattenBatchBufferIsCalledAnyway) {
395     DebugManagerStateRestore dbgRestore;
396     DebugManager.flags.FlattenBatchBufferForAUBDump.set(true);
397     DebugManager.flags.CsrDispatchMode.set(static_cast<uint32_t>(DispatchMode::BatchedDispatch));
398 
399     auto aubExecutionEnvironment = getEnvironment<MockAubCsr<FamilyType>>(true, true, true);
400     auto aubCsr = aubExecutionEnvironment->template getCsr<MockAubCsr<FamilyType>>();
401     LinearStream cs(aubExecutionEnvironment->commandBuffer);
402 
403     auto mockHelper = new MockFlatBatchBufferHelper<FamilyType>(*aubExecutionEnvironment->executionEnvironment);
404     mockHelper->flattenBatchBufferParamsPassed.clear();
405     aubCsr->overwriteFlatBatchBufferHelper(mockHelper);
406     ResidencyContainer allocationsForResidency;
407 
408     BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 128u, nullptr, false, false, QueueThrottle::MEDIUM, QueueSliceCount::defaultSliceCount, cs.getUsed(), &cs, nullptr, false};
409 
410     aubCsr->flush(batchBuffer, allocationsForResidency);
411 
412     EXPECT_EQ(1u, mockHelper->flattenBatchBufferCalled);
413     EXPECT_EQ(aubCsr->getRootDeviceIndex(), mockHelper->flattenBatchBufferParamsPassed[0].rootDeviceIndex);
414     EXPECT_EQ(aubCsr->getOsContext().getDeviceBitfield().to_ulong(), mockHelper->flattenBatchBufferParamsPassed[0].deviceBitfield.to_ulong());
415 }
416 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenAddPatchInfoCommentsForAUBDumpIsSetThenAddPatchInfoCommentsIsCalled)417 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenAddPatchInfoCommentsForAUBDumpIsSetThenAddPatchInfoCommentsIsCalled) {
418     DebugManagerStateRestore dbgRestore;
419     DebugManager.flags.AddPatchInfoCommentsForAUBDump.set(true);
420 
421     auto aubExecutionEnvironment = getEnvironment<MockAubCsr<FamilyType>>(true, true, true);
422     auto aubCsr = aubExecutionEnvironment->template getCsr<MockAubCsr<FamilyType>>();
423     LinearStream cs(aubExecutionEnvironment->commandBuffer);
424 
425     BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 0, nullptr, false, false, QueueThrottle::MEDIUM, QueueSliceCount::defaultSliceCount, cs.getUsed(), &cs, nullptr, false};
426 
427     ResidencyContainer allocationsForResidency;
428 
429     EXPECT_CALL(*aubCsr, addPatchInfoComments()).Times(1);
430     aubCsr->flush(batchBuffer, allocationsForResidency);
431 }
432 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenAddPatchInfoCommentsForAUBDumpIsNotSetThenAddPatchInfoCommentsIsNotCalled)433 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenAddPatchInfoCommentsForAUBDumpIsNotSetThenAddPatchInfoCommentsIsNotCalled) {
434     auto aubExecutionEnvironment = getEnvironment<MockAubCsr<FamilyType>>(true, true, true);
435     auto aubCsr = aubExecutionEnvironment->template getCsr<MockAubCsr<FamilyType>>();
436     LinearStream cs(aubExecutionEnvironment->commandBuffer);
437 
438     BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 0, nullptr, false, false, QueueThrottle::MEDIUM, QueueSliceCount::defaultSliceCount, cs.getUsed(), &cs, nullptr, false};
439 
440     ResidencyContainer allocationsForResidency;
441 
442     EXPECT_CALL(*aubCsr, addPatchInfoComments()).Times(0);
443 
444     aubCsr->flush(batchBuffer, allocationsForResidency);
445 }
446 
HWTEST_F(FlatBatchBufferHelperAubTests,givenAubCommandStreamReceiverWhenGetIndirectPatchCommandsIsCalledForEmptyPatchInfoListThenIndirectPatchCommandBufferIsNotCreated)447 HWTEST_F(FlatBatchBufferHelperAubTests, givenAubCommandStreamReceiverWhenGetIndirectPatchCommandsIsCalledForEmptyPatchInfoListThenIndirectPatchCommandBufferIsNotCreated) {
448     auto aubExecutionEnvironment = getEnvironment<AUBCommandStreamReceiverHw<FamilyType>>(false, false, true);
449     auto aubCsr = aubExecutionEnvironment->template getCsr<AUBCommandStreamReceiverHw<FamilyType>>();
450 
451     size_t indirectPatchCommandsSize = 0u;
452     std::vector<PatchInfoData> indirectPatchInfo;
453 
454     std::unique_ptr<char> commandBuffer(aubCsr->getFlatBatchBufferHelper().getIndirectPatchCommands(indirectPatchCommandsSize, indirectPatchInfo));
455     EXPECT_EQ(0u, indirectPatchCommandsSize);
456     EXPECT_EQ(0u, indirectPatchInfo.size());
457 }
458 
HWTEST_F(FlatBatchBufferHelperAubTests,givenAubCommandStreamReceiverWhenGetIndirectPatchCommandsIsCalledForNonEmptyPatchInfoListThenIndirectPatchCommandBufferIsCreated)459 HWTEST_F(FlatBatchBufferHelperAubTests, givenAubCommandStreamReceiverWhenGetIndirectPatchCommandsIsCalledForNonEmptyPatchInfoListThenIndirectPatchCommandBufferIsCreated) {
460     typedef typename FamilyType::MI_STORE_DATA_IMM MI_STORE_DATA_IMM;
461     std::unique_ptr<AUBCommandStreamReceiverHw<FamilyType>> aubCsr(new AUBCommandStreamReceiverHw<FamilyType>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()));
462 
463     PatchInfoData patchInfo1(0xA000, 0u, PatchInfoAllocationType::KernelArg, 0x6000, 0x100, PatchInfoAllocationType::IndirectObjectHeap);
464     PatchInfoData patchInfo2(0xB000, 0u, PatchInfoAllocationType::KernelArg, 0x6000, 0x200, PatchInfoAllocationType::IndirectObjectHeap);
465     PatchInfoData patchInfo3(0xC000, 0u, PatchInfoAllocationType::IndirectObjectHeap, 0x1000, 0x100, PatchInfoAllocationType::Default);
466     PatchInfoData patchInfo4(0xC000, 0u, PatchInfoAllocationType::Default, 0x2000, 0x100, PatchInfoAllocationType::GUCStartMessage);
467 
468     aubCsr->getFlatBatchBufferHelper().setPatchInfoData(patchInfo1);
469     aubCsr->getFlatBatchBufferHelper().setPatchInfoData(patchInfo2);
470     aubCsr->getFlatBatchBufferHelper().setPatchInfoData(patchInfo3);
471     aubCsr->getFlatBatchBufferHelper().setPatchInfoData(patchInfo4);
472 
473     size_t indirectPatchCommandsSize = 0u;
474     std::vector<PatchInfoData> indirectPatchInfo;
475 
476     std::unique_ptr<char> commandBuffer(aubCsr->getFlatBatchBufferHelper().getIndirectPatchCommands(indirectPatchCommandsSize, indirectPatchInfo));
477     EXPECT_EQ(4u, indirectPatchInfo.size());
478     EXPECT_EQ(2u * sizeof(MI_STORE_DATA_IMM), indirectPatchCommandsSize);
479 }
480 
HWTEST_F(FlatBatchBufferHelperAubTests,GivenAubCommandStreamReceiverWhenGetIndirectPatchCommandsIsCalledFor64BitAddressingModeThenDwordLengthAndStoreQwordAreSetCorrectly)481 HWTEST_F(FlatBatchBufferHelperAubTests, GivenAubCommandStreamReceiverWhenGetIndirectPatchCommandsIsCalledFor64BitAddressingModeThenDwordLengthAndStoreQwordAreSetCorrectly) {
482     using MI_STORE_DATA_IMM = typename FamilyType::MI_STORE_DATA_IMM;
483     std::unique_ptr<AUBCommandStreamReceiverHw<FamilyType>> aubCsr(new AUBCommandStreamReceiverHw<FamilyType>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()));
484 
485     PatchInfoData patchInfo(0xA000, 0u, PatchInfoAllocationType::KernelArg, 0x6000, 0x100, PatchInfoAllocationType::IndirectObjectHeap, sizeof(uint64_t));
486     aubCsr->getFlatBatchBufferHelper().setPatchInfoData(patchInfo);
487 
488     size_t indirectPatchCommandsSize = 0u;
489     std::vector<PatchInfoData> indirectPatchInfo;
490 
491     std::unique_ptr<char> commandBuffer(aubCsr->getFlatBatchBufferHelper().getIndirectPatchCommands(indirectPatchCommandsSize, indirectPatchInfo));
492     ASSERT_EQ(sizeof(MI_STORE_DATA_IMM), indirectPatchCommandsSize);
493     ASSERT_EQ(2u, indirectPatchInfo.size());
494 
495     auto cmd = reinterpret_cast<MI_STORE_DATA_IMM *>(commandBuffer.get());
496     EXPECT_TRUE(cmd->getStoreQword());
497     EXPECT_EQ(MI_STORE_DATA_IMM::DWORD_LENGTH::DWORD_LENGTH_STORE_QWORD, cmd->getDwordLength());
498 }
499 
HWTEST_F(FlatBatchBufferHelperAubTests,GivenAubCommandStreamReceiverWhenGetIndirectPatchCommandsIsCalledFor32BitAddressingModeThenDwordLengthAndSetStoreDwordAreSetCorrectly)500 HWTEST_F(FlatBatchBufferHelperAubTests, GivenAubCommandStreamReceiverWhenGetIndirectPatchCommandsIsCalledFor32BitAddressingModeThenDwordLengthAndSetStoreDwordAreSetCorrectly) {
501     using MI_STORE_DATA_IMM = typename FamilyType::MI_STORE_DATA_IMM;
502     std::unique_ptr<AUBCommandStreamReceiverHw<FamilyType>> aubCsr(new AUBCommandStreamReceiverHw<FamilyType>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()));
503 
504     PatchInfoData patchInfo(0xA000, 0u, PatchInfoAllocationType::KernelArg, 0x6000, 0x100, PatchInfoAllocationType::IndirectObjectHeap, sizeof(uint32_t));
505     aubCsr->getFlatBatchBufferHelper().setPatchInfoData(patchInfo);
506 
507     size_t indirectPatchCommandsSize = 0u;
508     std::vector<PatchInfoData> indirectPatchInfo;
509 
510     std::unique_ptr<char> commandBuffer(aubCsr->getFlatBatchBufferHelper().getIndirectPatchCommands(indirectPatchCommandsSize, indirectPatchInfo));
511     ASSERT_EQ(sizeof(MI_STORE_DATA_IMM), indirectPatchCommandsSize);
512     ASSERT_EQ(2u, indirectPatchInfo.size());
513 
514     auto cmd = reinterpret_cast<MI_STORE_DATA_IMM *>(commandBuffer.get());
515     EXPECT_FALSE(cmd->getStoreQword());
516     EXPECT_EQ(MI_STORE_DATA_IMM::DWORD_LENGTH::DWORD_LENGTH_STORE_DWORD, cmd->getDwordLength());
517 }
518 
HWTEST_F(FlatBatchBufferHelperAubTests,givenAubCommandStreamReceiverWhenAddBatchBufferStartCalledAndBatchBUfferFlatteningEnabledThenBatchBufferStartAddressIsRegistered)519 HWTEST_F(FlatBatchBufferHelperAubTests, givenAubCommandStreamReceiverWhenAddBatchBufferStartCalledAndBatchBUfferFlatteningEnabledThenBatchBufferStartAddressIsRegistered) {
520     typedef typename FamilyType::MI_BATCH_BUFFER_START MI_BATCH_BUFFER_START;
521 
522     std::unique_ptr<AUBCommandStreamReceiverHw<FamilyType>> aubCsr(new AUBCommandStreamReceiverHw<FamilyType>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()));
523 
524     MI_BATCH_BUFFER_START bbStart;
525 
526     aubCsr->addBatchBufferStart(&bbStart, 0xA000u, false);
527     std::map<uint64_t, uint64_t> &batchBufferStartAddressSequence = aubCsr->getFlatBatchBufferHelper().getBatchBufferStartAddressSequence();
528 
529     ASSERT_EQ(1u, batchBufferStartAddressSequence.size());
530     std::pair<uint64_t, uint64_t> addr = *batchBufferStartAddressSequence.begin();
531     EXPECT_EQ(reinterpret_cast<uint64_t>(&bbStart), addr.first);
532     EXPECT_EQ(0xA000u, addr.second);
533 }
534 
535 class OsAgnosticMemoryManagerForImagesWithNoHostPtr : public OsAgnosticMemoryManager {
536   public:
OsAgnosticMemoryManagerForImagesWithNoHostPtr(ExecutionEnvironment & executionEnvironment)537     OsAgnosticMemoryManagerForImagesWithNoHostPtr(ExecutionEnvironment &executionEnvironment) : OsAgnosticMemoryManager(executionEnvironment) {}
538 
allocateGraphicsMemoryForImage(const AllocationData & allocationData)539     GraphicsAllocation *allocateGraphicsMemoryForImage(const AllocationData &allocationData) override {
540         auto imageAllocation = OsAgnosticMemoryManager::allocateGraphicsMemoryForImage(allocationData);
541         cpuPtr = imageAllocation->getUnderlyingBuffer();
542         imageAllocation->setCpuPtrAndGpuAddress(nullptr, imageAllocation->getGpuAddress());
543         return imageAllocation;
544     };
freeGraphicsMemoryImpl(GraphicsAllocation * imageAllocation)545     void freeGraphicsMemoryImpl(GraphicsAllocation *imageAllocation) override {
546         imageAllocation->setCpuPtrAndGpuAddress(cpuPtr, imageAllocation->getGpuAddress());
547         OsAgnosticMemoryManager::freeGraphicsMemoryImpl(imageAllocation);
548     };
lockResourceImpl(GraphicsAllocation & imageAllocation)549     void *lockResourceImpl(GraphicsAllocation &imageAllocation) override {
550         lockResourceParam.wasCalled = true;
551         lockResourceParam.inImageAllocation = &imageAllocation;
552         lockCpuPtr = alignedMalloc(imageAllocation.getUnderlyingBufferSize(), MemoryConstants::pageSize);
553         lockResourceParam.retCpuPtr = lockCpuPtr;
554         return lockResourceParam.retCpuPtr;
555     };
unlockResourceImpl(GraphicsAllocation & imageAllocation)556     void unlockResourceImpl(GraphicsAllocation &imageAllocation) override {
557         unlockResourceParam.wasCalled = true;
558         unlockResourceParam.inImageAllocation = &imageAllocation;
559         alignedFree(lockCpuPtr);
560     };
561 
562     struct LockResourceParam {
563         bool wasCalled = false;
564         GraphicsAllocation *inImageAllocation = nullptr;
565         void *retCpuPtr = nullptr;
566     } lockResourceParam;
567     struct UnlockResourceParam {
568         bool wasCalled = false;
569         GraphicsAllocation *inImageAllocation = nullptr;
570     } unlockResourceParam;
571 
572   protected:
573     void *cpuPtr = nullptr;
574     void *lockCpuPtr = nullptr;
575 };
576 
577 using AubCommandStreamReceiverNoHostPtrTests = ::testing::Test;
HWTEST_F(AubCommandStreamReceiverNoHostPtrTests,givenAubCommandStreamReceiverWhenWriteMemoryIsCalledOnImageWithNoHostPtrThenResourceShouldBeLockedToGetCpuAddress)578 HWTEST_F(AubCommandStreamReceiverNoHostPtrTests, givenAubCommandStreamReceiverWhenWriteMemoryIsCalledOnImageWithNoHostPtrThenResourceShouldBeLockedToGetCpuAddress) {
579     ExecutionEnvironment *executionEnvironment = new MockExecutionEnvironment();
580     auto memoryManager = new OsAgnosticMemoryManagerForImagesWithNoHostPtr(*executionEnvironment);
581     executionEnvironment->memoryManager.reset(memoryManager);
582     auto hwInfo = executionEnvironment->rootDeviceEnvironments[0]->getHardwareInfo();
583     auto engineInstance = HwHelper::get(hwInfo->platform.eRenderCoreFamily).getGpgpuEngineInstances(*hwInfo)[0];
584     UltDeviceFactory deviceFactory{1, 0, *executionEnvironment};
585     DeviceBitfield deviceBitfield(1);
586     MockOsContext osContext(0, EngineDescriptorHelper::getDefaultDescriptor(engineInstance, deviceBitfield));
587     std::unique_ptr<AUBCommandStreamReceiverHw<FamilyType>> aubCsr(new AUBCommandStreamReceiverHw<FamilyType>("", true, *executionEnvironment, 0, deviceBitfield));
588     aubCsr->setupContext(osContext);
589     aubCsr->initializeEngine();
590 
591     ImageDescriptor imgDesc = {};
592     imgDesc.imageWidth = 512;
593     imgDesc.imageHeight = 1;
594     imgDesc.imageType = ImageType::Image2D;
595 
596     auto imgInfo = MockGmm::initImgInfo(imgDesc, 0, nullptr);
597 
598     AllocationProperties allocProperties{0u /* rootDeviceIndex */, true /* allocateMemory */,
599                                          imgInfo, GraphicsAllocation::AllocationType::IMAGE, deviceBitfield};
600 
601     auto imageAllocation = memoryManager->allocateGraphicsMemoryInPreferredPool(allocProperties, nullptr);
602     ASSERT_NE(nullptr, imageAllocation);
603 
604     EXPECT_TRUE(aubCsr->writeMemory(*imageAllocation));
605 
606     EXPECT_TRUE(memoryManager->lockResourceParam.wasCalled);
607     EXPECT_EQ(imageAllocation, memoryManager->lockResourceParam.inImageAllocation);
608     EXPECT_NE(nullptr, memoryManager->lockResourceParam.retCpuPtr);
609 
610     EXPECT_TRUE(memoryManager->unlockResourceParam.wasCalled);
611     EXPECT_EQ(imageAllocation, memoryManager->unlockResourceParam.inImageAllocation);
612     memoryManager->freeGraphicsMemory(imageAllocation);
613 }
614 
HWTEST_F(AubCommandStreamReceiverNoHostPtrTests,givenAubCommandStreamReceiverWhenWriteMemoryIsCalledOnLockedResourceThenResourceShouldNotBeUnlocked)615 HWTEST_F(AubCommandStreamReceiverNoHostPtrTests, givenAubCommandStreamReceiverWhenWriteMemoryIsCalledOnLockedResourceThenResourceShouldNotBeUnlocked) {
616     auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
617     auto memoryManager = new OsAgnosticMemoryManagerForImagesWithNoHostPtr(*executionEnvironment);
618     executionEnvironment->memoryManager.reset(memoryManager);
619     DeviceBitfield deviceBitfield(1);
620     std::unique_ptr<AUBCommandStreamReceiverHw<FamilyType>> aubCsr(new AUBCommandStreamReceiverHw<FamilyType>("", true, *executionEnvironment, 0, deviceBitfield));
621     auto osContext = memoryManager->createAndRegisterOsContext(aubCsr.get(), EngineDescriptorHelper::getDefaultDescriptor({getChosenEngineType(*defaultHwInfo), EngineUsage::Regular}));
622     aubCsr->setupContext(*osContext);
623     aubCsr->initializeEngine();
624     auto gfxAllocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{aubCsr->getRootDeviceIndex(), MemoryConstants::pageSize});
625 
626     memoryManager->lockResource(gfxAllocation);
627     EXPECT_TRUE(aubCsr->writeMemory(*gfxAllocation));
628 
629     EXPECT_FALSE(memoryManager->unlockResourceParam.wasCalled);
630     memoryManager->freeGraphicsMemory(gfxAllocation);
631 }
632 
HWTEST_F(AubCommandStreamReceiverTests,givenNoDbgDeviceIdFlagWhenAubCsrIsCreatedThenUseDefaultDeviceId)633 HWTEST_F(AubCommandStreamReceiverTests, givenNoDbgDeviceIdFlagWhenAubCsrIsCreatedThenUseDefaultDeviceId) {
634     std::unique_ptr<MockAubCsr<FamilyType>> aubCsr(new MockAubCsr<FamilyType>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()));
635     EXPECT_EQ(pDevice->getHardwareInfo().capabilityTable.aubDeviceId, aubCsr->aubDeviceId);
636 }
637 
HWTEST_F(AubCommandStreamReceiverTests,givenDbgDeviceIdFlagIsSetWhenAubCsrIsCreatedThenUseDebugDeviceId)638 HWTEST_F(AubCommandStreamReceiverTests, givenDbgDeviceIdFlagIsSetWhenAubCsrIsCreatedThenUseDebugDeviceId) {
639     DebugManagerStateRestore stateRestore;
640     DebugManager.flags.OverrideAubDeviceId.set(9); //this is Hsw, not used
641     std::unique_ptr<MockAubCsr<FamilyType>> aubCsr(new MockAubCsr<FamilyType>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()));
642     EXPECT_EQ(9u, aubCsr->aubDeviceId);
643 }
644 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenGetGTTDataIsCalledThenLocalMemoryIsSetAccordingToCsrFeature)645 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenGetGTTDataIsCalledThenLocalMemoryIsSetAccordingToCsrFeature) {
646     std::unique_ptr<MockAubCsr<FamilyType>> aubCsr(new MockAubCsr<FamilyType>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()));
647     AubGTTData data = {};
648     aubCsr->getGTTData(nullptr, data);
649     EXPECT_TRUE(data.present);
650 
651     if (aubCsr->localMemoryEnabled) {
652         EXPECT_TRUE(data.localMemory);
653     } else {
654         EXPECT_FALSE(data.localMemory);
655     }
656 }
657 
HWTEST_F(AubCommandStreamReceiverTests,givenPhysicalAddressWhenSetGttEntryIsCalledThenGttEntrysBitFieldsShouldBePopulated)658 HWTEST_F(AubCommandStreamReceiverTests, givenPhysicalAddressWhenSetGttEntryIsCalledThenGttEntrysBitFieldsShouldBePopulated) {
659     typedef typename AUBFamilyMapper<FamilyType>::AUB AUB;
660 
661     AubMemDump::MiGttEntry entry = {};
662     uint64_t address = 0x0123456789;
663     AubGTTData data = {true, false};
664     AUB::setGttEntry(entry, address, data);
665 
666     EXPECT_EQ(entry.pageConfig.PhysicalAddress, address / 4096);
667     EXPECT_TRUE(entry.pageConfig.Present);
668     EXPECT_FALSE(entry.pageConfig.LocalMemory);
669 }
670 
HWTEST_F(AubCommandStreamReceiverTests,whenGetMemoryBankForGttIsCalledThenCorrectBankIsReturned)671 HWTEST_F(AubCommandStreamReceiverTests, whenGetMemoryBankForGttIsCalledThenCorrectBankIsReturned) {
672     std::unique_ptr<MockAubCsr<FamilyType>> aubCsr(new MockAubCsr<FamilyType>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()));
673     aubCsr->localMemoryEnabled = false;
674 
675     aubCsr->setupContext(*pDevice->getDefaultEngine().osContext);
676     auto bank = aubCsr->getMemoryBankForGtt();
677     EXPECT_EQ(MemoryBanks::MainBank, bank);
678 }
679 
HWTEST_F(AubCommandStreamReceiverTests,givenEntryBitsPresentAndWritableWhenGetAddressSpaceFromPTEBitsIsCalledThenTraceNonLocalIsReturned)680 HWTEST_F(AubCommandStreamReceiverTests, givenEntryBitsPresentAndWritableWhenGetAddressSpaceFromPTEBitsIsCalledThenTraceNonLocalIsReturned) {
681     std::unique_ptr<MockAubCsr<FamilyType>> aubCsr(new MockAubCsr<FamilyType>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()));
682 
683     auto space = aubCsr->getAddressSpaceFromPTEBits(PageTableEntry::presentBit | PageTableEntry::writableBit);
684     EXPECT_EQ(AubMemDump::AddressSpaceValues::TraceNonlocal, space);
685 }
686 
687 template <typename GfxFamily>
688 struct MockAubCsrToTestExternalAllocations : public AUBCommandStreamReceiverHw<GfxFamily> {
689     using AUBCommandStreamReceiverHw<GfxFamily>::AUBCommandStreamReceiverHw;
690     using AUBCommandStreamReceiverHw<GfxFamily>::externalAllocations;
691 
writeMemoryMockAubCsrToTestExternalAllocations692     bool writeMemory(AllocationView &allocationView) override {
693         writeMemoryParametrization.wasCalled = true;
694         writeMemoryParametrization.receivedAllocationView = allocationView;
695         writeMemoryParametrization.statusToReturn = (0 != allocationView.second) ? true : false;
696         return writeMemoryParametrization.statusToReturn;
697     }
698     struct WriteMemoryParametrization {
699         bool wasCalled = false;
700         AllocationView receivedAllocationView = {};
701         bool statusToReturn = false;
702     } writeMemoryParametrization;
703 };
704 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenMakeResidentExternalIsCalledThenGivenAllocationViewShouldBeAddedToExternalAllocations)705 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenMakeResidentExternalIsCalledThenGivenAllocationViewShouldBeAddedToExternalAllocations) {
706     auto aubCsr = std::make_unique<MockAubCsrToTestExternalAllocations<FamilyType>>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
707     size_t size = 100;
708     auto ptr = std::make_unique<char[]>(size);
709     auto addr = reinterpret_cast<uint64_t>(ptr.get());
710     AllocationView externalAllocation(addr, size);
711 
712     ASSERT_EQ(0u, aubCsr->externalAllocations.size());
713     aubCsr->makeResidentExternal(externalAllocation);
714     EXPECT_EQ(1u, aubCsr->externalAllocations.size());
715     EXPECT_EQ(addr, aubCsr->externalAllocations[0].first);
716     EXPECT_EQ(size, aubCsr->externalAllocations[0].second);
717 }
718 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenMakeNonResidentExternalIsCalledThenMatchingAllocationViewShouldBeRemovedFromExternalAllocations)719 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenMakeNonResidentExternalIsCalledThenMatchingAllocationViewShouldBeRemovedFromExternalAllocations) {
720     auto aubCsr = std::make_unique<MockAubCsrToTestExternalAllocations<FamilyType>>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
721     size_t size = 100;
722     auto ptr = std::make_unique<char[]>(size);
723     auto addr = reinterpret_cast<uint64_t>(ptr.get());
724     AllocationView externalAllocation(addr, size);
725     aubCsr->makeResidentExternal(externalAllocation);
726 
727     ASSERT_EQ(1u, aubCsr->externalAllocations.size());
728     aubCsr->makeNonResidentExternal(addr);
729     EXPECT_EQ(0u, aubCsr->externalAllocations.size());
730 }
731 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenMakeNonResidentExternalIsCalledThenNonMatchingAllocationViewShouldNotBeRemovedFromExternalAllocations)732 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenMakeNonResidentExternalIsCalledThenNonMatchingAllocationViewShouldNotBeRemovedFromExternalAllocations) {
733     auto aubCsr = std::make_unique<MockAubCsrToTestExternalAllocations<FamilyType>>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
734     size_t size = 100;
735     auto ptr = std::make_unique<char[]>(size);
736     auto addr = reinterpret_cast<uint64_t>(ptr.get());
737     AllocationView externalAllocation(addr, size);
738     aubCsr->makeResidentExternal(externalAllocation);
739 
740     ASSERT_EQ(1u, aubCsr->externalAllocations.size());
741     aubCsr->makeNonResidentExternal(0);
742     EXPECT_EQ(1u, aubCsr->externalAllocations.size());
743 }
744 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenProcessResidencyIsCalledThenExternalAllocationsShouldBeMadeResident)745 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenProcessResidencyIsCalledThenExternalAllocationsShouldBeMadeResident) {
746     auto aubCsr = std::make_unique<MockAubCsrToTestExternalAllocations<FamilyType>>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
747     size_t size = 100;
748     auto ptr = std::make_unique<char[]>(size);
749     auto addr = reinterpret_cast<uint64_t>(ptr.get());
750     AllocationView externalAllocation(addr, size);
751     aubCsr->makeResidentExternal(externalAllocation);
752 
753     ASSERT_EQ(1u, aubCsr->externalAllocations.size());
754     ResidencyContainer allocationsForResidency;
755     aubCsr->processResidency(allocationsForResidency, 0u);
756 
757     EXPECT_TRUE(aubCsr->writeMemoryParametrization.wasCalled);
758     EXPECT_EQ(addr, aubCsr->writeMemoryParametrization.receivedAllocationView.first);
759     EXPECT_EQ(size, aubCsr->writeMemoryParametrization.receivedAllocationView.second);
760     EXPECT_TRUE(aubCsr->writeMemoryParametrization.statusToReturn);
761 }
762 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenProcessResidencyIsCalledThenExternalAllocationWithZeroSizeShouldNotBeMadeResident)763 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenProcessResidencyIsCalledThenExternalAllocationWithZeroSizeShouldNotBeMadeResident) {
764     auto aubCsr = std::make_unique<MockAubCsrToTestExternalAllocations<FamilyType>>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
765     AllocationView externalAllocation(0, 0);
766     aubCsr->makeResidentExternal(externalAllocation);
767 
768     ASSERT_EQ(1u, aubCsr->externalAllocations.size());
769     ResidencyContainer allocationsForResidency;
770     aubCsr->processResidency(allocationsForResidency, 0u);
771 
772     EXPECT_TRUE(aubCsr->writeMemoryParametrization.wasCalled);
773     EXPECT_EQ(0u, aubCsr->writeMemoryParametrization.receivedAllocationView.first);
774     EXPECT_EQ(0u, aubCsr->writeMemoryParametrization.receivedAllocationView.second);
775     EXPECT_FALSE(aubCsr->writeMemoryParametrization.statusToReturn);
776 }
777 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenWriteMemoryIsCalledThenGraphicsAllocationSizeIsReadCorrectly)778 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenWriteMemoryIsCalledThenGraphicsAllocationSizeIsReadCorrectly) {
779     pDevice->executionEnvironment->rootDeviceEnvironments[0]->aubCenter.reset(new AubCenter());
780 
781     auto aubCsr = std::make_unique<AUBCommandStreamReceiverHw<FamilyType>>("", false, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
782     aubCsr->setupContext(*pDevice->getDefaultEngine().osContext);
783     aubCsr->initializeEngine();
784     std::unique_ptr<MemoryManager> memoryManager(new OsAgnosticMemoryManager(*pDevice->executionEnvironment));
785 
786     PhysicalAddressAllocator allocator;
787     struct PpgttMock : std::conditional<is64bit, PML4, PDPE>::type {
788         PpgttMock(PhysicalAddressAllocator *allocator) : std::conditional<is64bit, PML4, PDPE>::type(allocator) {}
789 
790         void pageWalk(uintptr_t vm, size_t size, size_t offset, uint64_t entryBits, PageWalker &pageWalker, uint32_t memoryBank) override {
791             receivedSize = size;
792         }
793         size_t receivedSize = 0;
794     };
795     auto ppgttMock = new PpgttMock(&allocator);
796 
797     aubCsr->ppgtt.reset(ppgttMock);
798 
799     auto gfxAllocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{pDevice->getRootDeviceIndex(), MemoryConstants::pageSize});
800     aubCsr->setAubWritable(true, *gfxAllocation);
801 
802     auto gmm = new Gmm(pDevice->getGmmClientContext(), nullptr, 1, 0, false);
803     gfxAllocation->setDefaultGmm(gmm);
804 
805     for (bool compressed : {false, true}) {
806         gmm->isCompressionEnabled = compressed;
807 
808         aubCsr->writeMemory(*gfxAllocation);
809 
810         if (compressed) {
811             EXPECT_EQ(gfxAllocation->getDefaultGmm()->gmmResourceInfo->getSizeAllocation(), ppgttMock->receivedSize);
812         } else {
813             EXPECT_EQ(gfxAllocation->getUnderlyingBufferSize(), ppgttMock->receivedSize);
814         }
815     }
816 
817     memoryManager->freeGraphicsMemory(gfxAllocation);
818 }
819 
HWTEST_F(AubCommandStreamReceiverTests,whenAubCommandStreamReceiverIsCreatedThenPPGTTAndGGTTCreatedHavePhysicalAddressAllocatorSet)820 HWTEST_F(AubCommandStreamReceiverTests, whenAubCommandStreamReceiverIsCreatedThenPPGTTAndGGTTCreatedHavePhysicalAddressAllocatorSet) {
821     auto aubCsr = std::make_unique<AUBCommandStreamReceiverHw<FamilyType>>("", false, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
822     ASSERT_NE(nullptr, aubCsr->ppgtt.get());
823     ASSERT_NE(nullptr, aubCsr->ggtt.get());
824 
825     uintptr_t address = 0x20000;
826     auto physicalAddress = aubCsr->ppgtt->map(address, MemoryConstants::pageSize, 0, MemoryBanks::MainBank);
827     EXPECT_NE(0u, physicalAddress);
828 
829     physicalAddress = aubCsr->ggtt->map(address, MemoryConstants::pageSize, 0, MemoryBanks::MainBank);
830     EXPECT_NE(0u, physicalAddress);
831 }
832 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenEngineIsInitializedThenDumpHandleIsGenerated)833 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenEngineIsInitializedThenDumpHandleIsGenerated) {
834     MockExecutionEnvironment executionEnvironment(defaultHwInfo.get());
835     auto hwInfo = executionEnvironment.rootDeviceEnvironments[0]->getHardwareInfo();
836     auto engineInstance = HwHelper::get(hwInfo->platform.eRenderCoreFamily).getGpgpuEngineInstances(*hwInfo)[0];
837     DeviceBitfield deviceBitfield(1);
838     MockOsContext osContext(0, EngineDescriptorHelper::getDefaultDescriptor(engineInstance, deviceBitfield));
839     executionEnvironment.initializeMemoryManager();
840 
841     auto aubCsr = std::make_unique<MockAubCsrToTestDumpContext<FamilyType>>("", true, executionEnvironment, 0, deviceBitfield);
842     EXPECT_NE(nullptr, aubCsr);
843     EXPECT_EQ(nullptr, aubCsr->hardwareContextController.get());
844     aubCsr->aubManager = nullptr;
845 
846     aubCsr->setupContext(osContext);
847     aubCsr->initializeEngine();
848     EXPECT_NE(0u, aubCsr->handle);
849 }
850 
851 using InjectMmmioTest = Test<DeviceFixture>;
852 
HWTEST_F(InjectMmmioTest,givenAddMmioKeySetToZeroWhenInitAdditionalMmioCalledThenDoNotWriteMmio)853 HWTEST_F(InjectMmmioTest, givenAddMmioKeySetToZeroWhenInitAdditionalMmioCalledThenDoNotWriteMmio) {
854     DebugManagerStateRestore stateRestore;
855     DebugManager.flags.AubDumpAddMmioRegistersList.set("");
856 
857     auto aubCsr = std::make_unique<MockAubCsrToTestDumpContext<FamilyType>>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
858     EXPECT_NE(nullptr, aubCsr);
859 
860     auto stream = std::make_unique<MockAubFileStreamMockMmioWrite>();
861     aubCsr->stream = stream.get();
862     EXPECT_EQ(0u, stream->mmioList.size());
863     aubCsr->initAdditionalMMIO();
864     EXPECT_EQ(0u, stream->mmioList.size());
865 }
866 
HWTEST_F(InjectMmmioTest,givenAddMmioRegistersListSetWhenInitAdditionalMmioCalledThenWriteGivenMmio)867 HWTEST_F(InjectMmmioTest, givenAddMmioRegistersListSetWhenInitAdditionalMmioCalledThenWriteGivenMmio) {
868     std::string registers("0xdead;0xbeef;and another very long string");
869     MMIOPair mmioPair(0xdead, 0xbeef);
870 
871     DebugManagerStateRestore stateRestore;
872     DebugManager.flags.AubDumpAddMmioRegistersList.set(registers);
873 
874     auto aubCsr = std::make_unique<MockAubCsrToTestDumpContext<FamilyType>>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
875     EXPECT_NE(nullptr, aubCsr);
876 
877     auto stream = std::make_unique<MockAubFileStreamMockMmioWrite>();
878     aubCsr->stream = stream.get();
879     EXPECT_EQ(0u, stream->mmioList.size());
880     aubCsr->initAdditionalMMIO();
881     EXPECT_EQ(1u, stream->mmioList.size());
882     EXPECT_TRUE(stream->isOnMmioList(mmioPair));
883 };
884 
HWTEST_F(InjectMmmioTest,givenLongSequenceOfAddMmioRegistersListSetWhenInitAdditionalMmioCalledThenWriteGivenMmio)885 HWTEST_F(InjectMmmioTest, givenLongSequenceOfAddMmioRegistersListSetWhenInitAdditionalMmioCalledThenWriteGivenMmio) {
886     std::string registers("1;1;2;2;3;3");
887 
888     DebugManagerStateRestore stateRestore;
889     DebugManager.flags.AubDumpAddMmioRegistersList.set(registers);
890 
891     auto aubCsr = std::make_unique<MockAubCsrToTestDumpContext<FamilyType>>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
892     EXPECT_NE(nullptr, aubCsr);
893 
894     auto stream = std::make_unique<MockAubFileStreamMockMmioWrite>();
895     aubCsr->stream = stream.get();
896     EXPECT_EQ(0u, stream->mmioList.size());
897     aubCsr->initAdditionalMMIO();
898     EXPECT_EQ(3u, stream->mmioList.size());
899 }
900 
HWTEST_F(InjectMmmioTest,givenSequenceWithIncompletePairOfAddMmioRegistersListSetWhenInitAdditionalMmioCalledThenWriteGivenMmio)901 HWTEST_F(InjectMmmioTest, givenSequenceWithIncompletePairOfAddMmioRegistersListSetWhenInitAdditionalMmioCalledThenWriteGivenMmio) {
902     std::string registers("0x1;0x1;0x2");
903     MMIOPair mmioPair0(0x1, 0x1);
904     MMIOPair mmioPair1(0x2, 0x2);
905 
906     DebugManagerStateRestore stateRestore;
907     DebugManager.flags.AubDumpAddMmioRegistersList.set(registers);
908 
909     auto aubCsr = std::make_unique<MockAubCsrToTestDumpContext<FamilyType>>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
910     EXPECT_NE(nullptr, aubCsr);
911 
912     auto stream = std::make_unique<MockAubFileStreamMockMmioWrite>();
913     aubCsr->stream = stream.get();
914     EXPECT_EQ(0u, stream->mmioList.size());
915     aubCsr->initAdditionalMMIO();
916     EXPECT_EQ(1u, stream->mmioList.size());
917     EXPECT_TRUE(stream->isOnMmioList(mmioPair0));
918     EXPECT_FALSE(stream->isOnMmioList(mmioPair1));
919 }
920 
HWTEST_F(InjectMmmioTest,givenAddMmioRegistersListSetWithSemicolonAtTheEndWhenInitAdditionalMmioCalledThenWriteGivenMmio)921 HWTEST_F(InjectMmmioTest, givenAddMmioRegistersListSetWithSemicolonAtTheEndWhenInitAdditionalMmioCalledThenWriteGivenMmio) {
922     std::string registers("0xdead;0xbeef;");
923     MMIOPair mmioPair(0xdead, 0xbeef);
924 
925     DebugManagerStateRestore stateRestore;
926     DebugManager.flags.AubDumpAddMmioRegistersList.set(registers);
927 
928     auto aubCsr = std::make_unique<MockAubCsrToTestDumpContext<FamilyType>>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
929     EXPECT_NE(nullptr, aubCsr);
930 
931     auto stream = std::make_unique<MockAubFileStreamMockMmioWrite>();
932     aubCsr->stream = stream.get();
933     EXPECT_EQ(0u, stream->mmioList.size());
934     aubCsr->initAdditionalMMIO();
935     EXPECT_EQ(1u, stream->mmioList.size());
936     EXPECT_TRUE(stream->isOnMmioList(mmioPair));
937 }
938 
HWTEST_F(InjectMmmioTest,givenAddMmioRegistersListSetWithInvalidValueWhenInitAdditionalMmioCalledThenMmioIsNotWritten)939 HWTEST_F(InjectMmmioTest, givenAddMmioRegistersListSetWithInvalidValueWhenInitAdditionalMmioCalledThenMmioIsNotWritten) {
940     std::string registers("0xdead;invalid");
941 
942     DebugManagerStateRestore stateRestore;
943     DebugManager.flags.AubDumpAddMmioRegistersList.set(registers);
944 
945     auto aubCsr = std::make_unique<MockAubCsrToTestDumpContext<FamilyType>>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
946     EXPECT_NE(nullptr, aubCsr);
947 
948     auto stream = std::make_unique<MockAubFileStreamMockMmioWrite>();
949     aubCsr->stream = stream.get();
950     EXPECT_EQ(0u, stream->mmioList.size());
951     aubCsr->initAdditionalMMIO();
952     EXPECT_EQ(0u, stream->mmioList.size());
953 }
954 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCsrWhenAskedForMemoryExpectationThenPassValidCompareOperationType)955 HWTEST_F(AubCommandStreamReceiverTests, givenAubCsrWhenAskedForMemoryExpectationThenPassValidCompareOperationType) {
956     class MyMockAubCsr : public AUBCommandStreamReceiverHw<FamilyType> {
957       public:
958         using AUBCommandStreamReceiverHw<FamilyType>::AUBCommandStreamReceiverHw;
959 
960         bool expectMemory(const void *gfxAddress, const void *srcAddress, size_t length, uint32_t compareOperation) override {
961             inputCompareOperation = compareOperation;
962             return AUBCommandStreamReceiverHw<FamilyType>::expectMemory(gfxAddress, srcAddress, length, compareOperation);
963         }
964         uint32_t inputCompareOperation = 0;
965     };
966     void *mockAddress = reinterpret_cast<void *>(1);
967     uint32_t compareNotEqual = AubMemDump::CmdServicesMemTraceMemoryCompare::CompareOperationValues::CompareNotEqual;
968     uint32_t compareEqual = AubMemDump::CmdServicesMemTraceMemoryCompare::CompareOperationValues::CompareEqual;
969 
970     auto mockStream = std::make_unique<MockAubFileStream>();
971     MyMockAubCsr myMockCsr(std::string(), true, *pDevice->getExecutionEnvironment(), pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
972     myMockCsr.setupContext(pDevice->commandStreamReceivers[0]->getOsContext());
973     myMockCsr.stream = mockStream.get();
974 
975     myMockCsr.expectMemoryNotEqual(mockAddress, mockAddress, 1);
976     EXPECT_EQ(compareNotEqual, myMockCsr.inputCompareOperation);
977     EXPECT_EQ(compareNotEqual, mockStream->compareOperationFromExpectMemory);
978 
979     myMockCsr.expectMemoryEqual(mockAddress, mockAddress, 1);
980     EXPECT_EQ(compareEqual, myMockCsr.inputCompareOperation);
981     EXPECT_EQ(compareEqual, mockStream->compareOperationFromExpectMemory);
982 }
983 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenObtainingPreferredTagPoolSizeThenReturnOne)984 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenObtainingPreferredTagPoolSizeThenReturnOne) {
985     auto aubCsr = std::make_unique<AUBCommandStreamReceiverHw<FamilyType>>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
986     EXPECT_EQ(1u, aubCsr->getPreferredTagPoolSize());
987 }
988 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenSshSizeIsObtainedThenReturn64KB)989 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenSshSizeIsObtainedThenReturn64KB) {
990     auto aubCsr = std::make_unique<MockAubCsr<FamilyType>>("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
991     EXPECT_EQ(64 * KB, aubCsr->defaultSshSize);
992 }
993 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWhenPhysicalAddressAllocatorIsCreatedThenItIsNotNull)994 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenPhysicalAddressAllocatorIsCreatedThenItIsNotNull) {
995     MockAubCsr<FamilyType> aubCsr("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
996     std::unique_ptr<PhysicalAddressAllocator> allocator(aubCsr.createPhysicalAddressAllocator(&hardwareInfo));
997     ASSERT_NE(nullptr, allocator);
998 }
999 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWithoutHardwareContextControllerWhenCallingWriteMMIOThenDontRedirectToHardwareContextController)1000 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWithoutHardwareContextControllerWhenCallingWriteMMIOThenDontRedirectToHardwareContextController) {
1001     MockAubCsr<FamilyType> aubCsr("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
1002     MockOsContext osContext(0, EngineDescriptorHelper::getDefaultDescriptor());
1003     EXPECT_EQ(nullptr, aubCsr.hardwareContextController);
1004 
1005     aubCsr.writeMMIO(0x11111111, 0x22222222);
1006 
1007     EXPECT_TRUE(aubCsr.writeMMIOCalled);
1008 }
1009 
HWTEST_F(AubCommandStreamReceiverTests,givenAubCommandStreamReceiverWithHardwareContextControllerWhenCallingWriteMMIOThenRedirectToHardwareContextController)1010 HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWithHardwareContextControllerWhenCallingWriteMMIOThenRedirectToHardwareContextController) {
1011     MockAubCsr<FamilyType> aubCsr("", true, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
1012     MockOsContext osContext(0, EngineDescriptorHelper::getDefaultDescriptor());
1013     aubCsr.setupContext(osContext);
1014     EXPECT_NE(nullptr, aubCsr.hardwareContextController);
1015 
1016     auto mockHardwareContext = static_cast<MockHardwareContext *>(aubCsr.hardwareContextController->hardwareContexts[0].get());
1017 
1018     aubCsr.writeMMIO(0x11111111, 0x22222222);
1019 
1020     EXPECT_TRUE(mockHardwareContext->writeMMIOCalled);
1021 }
1022