|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include "shared/source/execution_environment/root_device_environment.h" |
| 9 | +#include "shared/source/os_interface/linux/drm_command_stream.h" |
| 10 | +#include "shared/source/os_interface/linux/drm_memory_manager.h" |
| 11 | +#include "shared/source/os_interface/linux/drm_memory_operations_handler.h" |
| 12 | +#include "shared/source/os_interface/linux/os_context_linux.h" |
| 13 | +#include "shared/source/os_interface/os_interface.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/helpers/variable_backup.h" |
| 17 | +#include "shared/test/common/libult/linux/drm_mock.h" |
| 18 | +#include "shared/test/common/mocks/linux/mock_drm_allocation.h" |
| 19 | +#include "shared/test/common/mocks/linux/mock_drm_memory_manager.h" |
| 20 | +#include "shared/test/common/mocks/mock_allocation_properties.h" |
| 21 | +#include "shared/test/common/mocks/mock_execution_environment.h" |
| 22 | +#include "shared/test/common/os_interface/linux/device_command_stream_fixture.h" |
| 23 | +#include "shared/test/common/os_interface/linux/drm_buffer_object_fixture.h" |
| 24 | +#include "shared/test/common/test_macros/test.h" |
| 25 | + |
| 26 | +#include "opencl/test/unit_test/os_interface/linux/drm_command_stream_fixture.h" |
| 27 | + |
| 28 | +using namespace NEO; |
| 29 | + |
| 30 | +struct DrmCommandStreamMultiTileMemExecFixture { |
| 31 | + void SetUp() { |
| 32 | + DebugManager.flags.CreateMultipleSubDevices.set(2u); |
| 33 | + DebugManager.flags.EnableImplicitScaling.set(1); |
| 34 | + DebugManager.flags.EnableForcePin.set(false); |
| 35 | + osLocalMemoryBackup = std::make_unique<VariableBackup<bool>>(&OSInterface::osEnableLocalMemory, true); |
| 36 | + |
| 37 | + executionEnvironment = new MockExecutionEnvironment(); |
| 38 | + executionEnvironment->incRefInternal(); |
| 39 | + executionEnvironment->initGmm(); |
| 40 | + |
| 41 | + mock = new DrmMockCustom(*executionEnvironment->rootDeviceEnvironments[0]); |
| 42 | + executionEnvironment->rootDeviceEnvironments[0]->osInterface = std::make_unique<OSInterface>(); |
| 43 | + executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::unique_ptr<DriverModel>(mock)); |
| 44 | + executionEnvironment->rootDeviceEnvironments[0]->memoryOperationsInterface = DrmMemoryOperationsHandler::create(*mock, 0); |
| 45 | + |
| 46 | + memoryManager = new DrmMemoryManager(gemCloseWorkerMode::gemCloseWorkerInactive, |
| 47 | + DebugManager.flags.EnableForcePin.get(), |
| 48 | + true, |
| 49 | + *executionEnvironment); |
| 50 | + executionEnvironment->memoryManager.reset(memoryManager); |
| 51 | + executionEnvironment->prepareRootDeviceEnvironments(1u); |
| 52 | + executionEnvironment->rootDeviceEnvironments[0]->setHwInfo(NEO::defaultHwInfo.get()); |
| 53 | + executionEnvironment->initializeMemoryManager(); |
| 54 | + device.reset(MockDevice::create<MockDevice>(executionEnvironment, 0)); |
| 55 | + |
| 56 | + osContext = std::make_unique<OsContextLinux>(*mock, 0u, EngineDescriptorHelper::getDefaultDescriptor(device->getDeviceBitfield())); |
| 57 | + osContext->ensureContextInitialized(); |
| 58 | + } |
| 59 | + |
| 60 | + void TearDown() { |
| 61 | + executionEnvironment->decRefInternal(); |
| 62 | + } |
| 63 | + |
| 64 | + DebugManagerStateRestore dbgRestore; |
| 65 | + std::unique_ptr<VariableBackup<bool>> osLocalMemoryBackup; |
| 66 | + std::unique_ptr<MockDevice> device; |
| 67 | + std::unique_ptr<OsContext> osContext; |
| 68 | + MockExecutionEnvironment *executionEnvironment = nullptr; |
| 69 | + DrmMockCustom *mock = nullptr; |
| 70 | + DrmMemoryManager *memoryManager = nullptr; |
| 71 | +}; |
| 72 | + |
| 73 | +using DrmCommandStreamMultiTileMemExecTest = Test<DrmCommandStreamMultiTileMemExecFixture>; |
| 74 | + |
| 75 | +HWCMDTEST_F(IGFX_XE_HP_CORE, DrmCommandStreamMultiTileMemExecTest, GivenDrmSupportsCompletionFenceAndVmBindWhenCallingCsrExecThenMultipleTagAllocationIsPassed) { |
| 76 | + auto *testCsr = new TestedDrmCommandStreamReceiver<FamilyType>(*executionEnvironment, 0, device->getDeviceBitfield()); |
| 77 | + device->resetCommandStreamReceiver(testCsr); |
| 78 | + EXPECT_EQ(2u, testCsr->activePartitions); |
| 79 | + testCsr->setupContext(*osContext.get()); |
| 80 | + |
| 81 | + mock->completionFenceSupported = true; |
| 82 | + mock->isVmBindAvailableCall.callParent = false; |
| 83 | + mock->isVmBindAvailableCall.returnValue = true; |
| 84 | + |
| 85 | + TestedBufferObject bo(mock, 128); |
| 86 | + MockDrmAllocation cmdBuffer(GraphicsAllocation::AllocationType::COMMAND_BUFFER, MemoryPool::System4KBPages); |
| 87 | + cmdBuffer.bufferObjects[0] = &bo; |
| 88 | + uint8_t buff[128]; |
| 89 | + |
| 90 | + LinearStream cs(&cmdBuffer, buff, 128); |
| 91 | + CommandStreamReceiverHw<FamilyType>::addBatchBufferEnd(cs, nullptr); |
| 92 | + EncodeNoop<FamilyType>::alignToCacheLine(cs); |
| 93 | + |
| 94 | + BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 0, nullptr, false, false, QueueThrottle::MEDIUM, QueueSliceCount::defaultSliceCount, cs.getUsed(), &cs, nullptr, false}; |
| 95 | + |
| 96 | + auto allocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{testCsr->getRootDeviceIndex(), MemoryConstants::pageSize}); |
| 97 | + testCsr->makeResident(cmdBuffer); |
| 98 | + testCsr->makeResident(*allocation); |
| 99 | + testCsr->makeResident(*testCsr->getTagAllocation()); |
| 100 | + |
| 101 | + testCsr->latestSentTaskCount = 2; |
| 102 | + testCsr->postSyncWriteOffset = 16; |
| 103 | + |
| 104 | + uint64_t expectedCompletionGpuAddress = testCsr->getTagAllocation()->getGpuAddress() + Drm::completionFenceOffset + testCsr->postSyncWriteOffset; |
| 105 | + |
| 106 | + int ret = testCsr->flushInternal(batchBuffer, testCsr->getResidencyAllocations()); |
| 107 | + EXPECT_EQ(0, ret); |
| 108 | + |
| 109 | + EXPECT_EQ(expectedCompletionGpuAddress, bo.receivedCompletionGpuAddress); |
| 110 | + EXPECT_EQ(testCsr->latestSentTaskCount, bo.receivedCompletionValue); |
| 111 | + EXPECT_EQ(2u, bo.execCalled); |
| 112 | + |
| 113 | + memoryManager->freeGraphicsMemory(allocation); |
| 114 | +} |
| 115 | + |
| 116 | +HWCMDTEST_F(IGFX_XE_HP_CORE, DrmCommandStreamMultiTileMemExecTest, GivenDrmSupportsCompletionFenceAndVmBindWhenHandlingCompletionThenExpectMultipleWaitCalls) { |
| 117 | + EngineControl &defaultEngine = device->getDefaultEngine(); |
| 118 | + EXPECT_EQ(2u, defaultEngine.commandStreamReceiver->getActivePartitions()); |
| 119 | + |
| 120 | + uint32_t postSyncOffset = defaultEngine.commandStreamReceiver->getPostSyncWriteOffset(); |
| 121 | + EXPECT_NE(0u, postSyncOffset); |
| 122 | + |
| 123 | + mock->completionFenceSupported = true; |
| 124 | + mock->isVmBindAvailableCall.callParent = false; |
| 125 | + mock->isVmBindAvailableCall.returnValue = true; |
| 126 | + |
| 127 | + auto allocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{0, 1024, GraphicsAllocation::AllocationType::COMMAND_BUFFER}); |
| 128 | + allocation->updateTaskCount(2, defaultEngine.osContext->getContextId()); |
| 129 | + |
| 130 | + volatile uint32_t *completionAddress = defaultEngine.commandStreamReceiver->getTagAddress(); |
| 131 | + completionAddress += (Drm::completionFenceOffset / sizeof(uint32_t)); |
| 132 | + *completionAddress = 1; |
| 133 | + completionAddress += (postSyncOffset / sizeof(uint32_t)); |
| 134 | + *completionAddress = 1; |
| 135 | + |
| 136 | + memoryManager->handleFenceCompletion(allocation); |
| 137 | + |
| 138 | + uint64_t expectedAddress = castToUint64(const_cast<uint32_t *>(defaultEngine.commandStreamReceiver->getTagAddress())) + |
| 139 | + Drm::completionFenceOffset + |
| 140 | + postSyncOffset; |
| 141 | + constexpr uint64_t expectedValue = 2; |
| 142 | + |
| 143 | + EXPECT_EQ(2u, mock->waitUserFenceCall.called); |
| 144 | + EXPECT_EQ(expectedAddress, mock->waitUserFenceCall.address); |
| 145 | + EXPECT_EQ(expectedValue, mock->waitUserFenceCall.value); |
| 146 | + |
| 147 | + memoryManager->freeGraphicsMemory(allocation); |
| 148 | +} |
| 149 | + |
| 150 | +HWCMDTEST_F(IGFX_XE_HP_CORE, DrmCommandStreamMultiTileMemExecTest, GivenDrmSupportsCompletionFenceAndVmBindWhenHandlingCompletionAndOneContextIsReadyThenExpectOneWaitCall) { |
| 151 | + EngineControl &defaultEngine = device->getDefaultEngine(); |
| 152 | + EXPECT_EQ(2u, defaultEngine.commandStreamReceiver->getActivePartitions()); |
| 153 | + |
| 154 | + uint32_t postSyncOffset = defaultEngine.commandStreamReceiver->getPostSyncWriteOffset(); |
| 155 | + EXPECT_NE(0u, postSyncOffset); |
| 156 | + |
| 157 | + mock->completionFenceSupported = true; |
| 158 | + mock->isVmBindAvailableCall.callParent = false; |
| 159 | + mock->isVmBindAvailableCall.returnValue = true; |
| 160 | + |
| 161 | + auto allocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{0, 1024, GraphicsAllocation::AllocationType::COMMAND_BUFFER}); |
| 162 | + allocation->updateTaskCount(2, defaultEngine.osContext->getContextId()); |
| 163 | + |
| 164 | + volatile uint32_t *completionAddress = defaultEngine.commandStreamReceiver->getTagAddress(); |
| 165 | + completionAddress += (Drm::completionFenceOffset / sizeof(uint32_t)); |
| 166 | + *completionAddress = 2; //1st context is ready |
| 167 | + completionAddress += (postSyncOffset / sizeof(uint32_t)); |
| 168 | + *completionAddress = 1; |
| 169 | + |
| 170 | + memoryManager->handleFenceCompletion(allocation); |
| 171 | + |
| 172 | + uint64_t expectedAddress = castToUint64(const_cast<uint32_t *>(defaultEngine.commandStreamReceiver->getTagAddress())) + |
| 173 | + Drm::completionFenceOffset + |
| 174 | + postSyncOffset; |
| 175 | + constexpr uint64_t expectedValue = 2; |
| 176 | + |
| 177 | + EXPECT_EQ(1u, mock->waitUserFenceCall.called); |
| 178 | + EXPECT_EQ(expectedAddress, mock->waitUserFenceCall.address); |
| 179 | + EXPECT_EQ(expectedValue, mock->waitUserFenceCall.value); |
| 180 | + |
| 181 | + memoryManager->freeGraphicsMemory(allocation); |
| 182 | +} |
0 commit comments