1/*
2 * Copyright (C) 2019-2021 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8#include "shared/source/command_stream/command_stream_receiver.h"
9#include "shared/source/command_stream/linear_stream.h"
10#include "shared/test/common/cmd_parse/hw_parse.h"
11#include "shared/test/common/libult/gen12lp/special_ult_helper_gen12lp.h"
12#include "shared/test/common/mocks/mock_csr.h"
13#include "shared/test/common/test_macros/test.h"
14
15#include "opencl/source/command_queue/command_queue_hw.h"
16#include "opencl/test/unit_test/fixtures/cl_device_fixture.h"
17#include "opencl/test/unit_test/fixtures/ult_command_stream_receiver_fixture.h"
18#include "opencl/test/unit_test/mocks/mock_command_queue.h"
19#include "opencl/test/unit_test/mocks/mock_context.h"
20#include "opencl/test/unit_test/mocks/mock_event.h"
21#include "opencl/test/unit_test/mocks/mock_kernel.h"
22
23#include "gtest/gtest.h"
24#include "reg_configs_common.h"
25
26using namespace NEO;
27
28#include "opencl/test/unit_test/command_stream/command_stream_receiver_hw_tests.inl"
29
30using CommandStreamReceiverHwTestGen12lp = CommandStreamReceiverHwTest<TGLLPFamily>;
31
32GEN12LPTEST_F(CommandStreamReceiverHwTestGen12lp, givenPreambleSentWhenL3ConfigRequestChangedThenDontProgramL3Register) {
33    size_t GWS = 1;
34    MockContext ctx(pClDevice);
35    MockKernelWithInternals kernel(*pClDevice);
36    CommandQueueHw<FamilyType> commandQueue(&ctx, pClDevice, 0, false);
37    auto commandStreamReceiver = new MockCsrHw<FamilyType>(*pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
38    pDevice->resetCommandStreamReceiver(commandStreamReceiver);
39    auto &commandStreamCSR = commandStreamReceiver->getCS();
40
41    commandStreamReceiver->isPreambleSent = true;
42    commandStreamReceiver->lastSentL3Config = 0;
43
44    commandQueue.enqueueKernel(kernel, 1, nullptr, &GWS, nullptr, 0, nullptr, nullptr);
45
46    parseCommands<FamilyType>(commandStreamCSR, 0);
47    auto itorCmd = findMmio<FamilyType>(cmdList.begin(), cmdList.end(), L3CNTLRegisterOffset<FamilyType>::registerOffset);
48    ASSERT_EQ(cmdList.end(), itorCmd);
49}
50
51GEN12LPTEST_F(CommandStreamReceiverHwTestGen12lp, whenProgrammingMiSemaphoreWaitThenSetRegisterPollModeMemoryPoll) {
52    using MI_SEMAPHORE_WAIT = typename FamilyType::MI_SEMAPHORE_WAIT;
53    MI_SEMAPHORE_WAIT miSemaphoreWait = FamilyType::cmdInitMiSemaphoreWait;
54    EXPECT_EQ(MI_SEMAPHORE_WAIT::REGISTER_POLL_MODE::REGISTER_POLL_MODE_MEMORY_POLL, miSemaphoreWait.getRegisterPollMode());
55}
56
57using CommandStreamReceiverFlushTaskTests = UltCommandStreamReceiverTest;
58GEN12LPTEST_F(UltCommandStreamReceiverTest, givenStateBaseAddressWhenItIsRequiredThenThereIsPipeControlPriorToItWithTextureCacheFlushAndHdc) {
59    using STATE_BASE_ADDRESS = typename FamilyType::STATE_BASE_ADDRESS;
60    auto &commandStreamReceiver = pDevice->getUltCommandStreamReceiver<FamilyType>();
61    configureCSRtoNonDirtyState<FamilyType>(false);
62    ioh.replaceBuffer(ptrOffset(ioh.getCpuBase(), +1u), ioh.getMaxAvailableSpace() + MemoryConstants::pageSize * 3);
63
64    flushTask(commandStreamReceiver);
65
66    parseCommands<FamilyType>(commandStreamReceiver.commandStream, 0);
67    auto stateBaseAddressItor = find<STATE_BASE_ADDRESS *>(cmdList.begin(), cmdList.end());
68    auto pipeControlItor = find<typename FamilyType::PIPE_CONTROL *>(cmdList.begin(), stateBaseAddressItor);
69    EXPECT_NE(stateBaseAddressItor, pipeControlItor);
70    auto pipeControlCmd = reinterpret_cast<typename FamilyType::PIPE_CONTROL *>(*pipeControlItor);
71    EXPECT_TRUE(pipeControlCmd->getTextureCacheInvalidationEnable());
72    EXPECT_TRUE(pipeControlCmd->getDcFlushEnable());
73    EXPECT_TRUE(pipeControlCmd->getHdcPipelineFlush());
74}
75
76using UltCommandStreamReceiverTestGen12Lp = UltCommandStreamReceiverTest;
77
78GEN12LPTEST_F(UltCommandStreamReceiverTestGen12Lp, givenDebugEnablingCacheFlushWhenAddingPipeControlWithoutCacheFlushThenOverrideRequestAndEnableCacheFlushFlags) {
79    using PIPE_CONTROL = typename FamilyType::PIPE_CONTROL;
80    DebugManagerStateRestore dbgRestorer;
81    DebugManager.flags.FlushAllCaches.set(true);
82
83    char buff[sizeof(PIPE_CONTROL) * 3];
84    LinearStream stream(buff, sizeof(PIPE_CONTROL) * 3);
85
86    PipeControlArgs args;
87    MemorySynchronizationCommands<FamilyType>::addPipeControl(stream, args);
88
89    parseCommands<FamilyType>(stream, 0);
90
91    PIPE_CONTROL *pipeControl = getCommand<PIPE_CONTROL>();
92
93    ASSERT_NE(nullptr, pipeControl);
94
95    // WA pipeControl added
96    if (cmdList.size() == 2) {
97        pipeControl++;
98    }
99
100    EXPECT_TRUE(pipeControl->getHdcPipelineFlush());
101}
102
103GEN12LPTEST_F(UltCommandStreamReceiverTestGen12Lp, givenDebugDisablingCacheFlushWhenAddingPipeControlWithCacheFlushThenOverrideRequestAndDisableCacheFlushFlags) {
104    using PIPE_CONTROL = typename FamilyType::PIPE_CONTROL;
105    DebugManagerStateRestore dbgRestorer;
106    DebugManager.flags.DoNotFlushCaches.set(true);
107
108    char buff[sizeof(PIPE_CONTROL) * 3];
109    LinearStream stream(buff, sizeof(PIPE_CONTROL) * 3);
110
111    PipeControlArgs args;
112    args.dcFlushEnable = true;
113    args.hdcPipelineFlush = true;
114    MemorySynchronizationCommands<FamilyType>::addPipeControl(stream, args);
115
116    parseCommands<FamilyType>(stream, 0);
117
118    PIPE_CONTROL *pipeControl = getCommand<PIPE_CONTROL>();
119
120    ASSERT_NE(nullptr, pipeControl);
121
122    // WA pipeControl added
123    if (cmdList.size() == 2) {
124        pipeControl++;
125    }
126
127    EXPECT_FALSE(pipeControl->getHdcPipelineFlush());
128}
129