1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*
* Copyright (C) 2019-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "shared/source/command_stream/preemption.h"
#include "shared/source/helpers/aligned_memory.h"
#include "shared/source/os_interface/linux/drm_allocation.h"
#include "shared/source/os_interface/linux/drm_buffer_object.h"
#include "shared/source/os_interface/linux/drm_memory_operations_handler.h"
#include "shared/source/os_interface/linux/os_context_linux.h"
#include "shared/test/common/helpers/debug_manager_state_restore.h"
#include "shared/test/common/helpers/engine_descriptor_helper.h"
#include "shared/test/common/libult/linux/drm_mock.h"
#include "shared/test/common/mocks/linux/mock_drm_command_stream_receiver.h"
#include "shared/test/common/mocks/mock_device.h"
#include "shared/test/common/mocks/mock_execution_environment.h"
#include "shared/test/common/mocks/mock_sip.h"
#include "shared/test/common/os_interface/linux/drm_mock_memory_info.h"
#include "gtest/gtest.h"
#include <algorithm>
template <typename GfxFamily>
struct MockDrmCsr : public DrmCommandStreamReceiver<GfxFamily> {
using DrmCommandStreamReceiver<GfxFamily>::DrmCommandStreamReceiver;
using DrmCommandStreamReceiver<GfxFamily>::dispatchMode;
using DrmCommandStreamReceiver<GfxFamily>::completionFenceValuePointer;
using DrmCommandStreamReceiver<GfxFamily>::flushInternal;
using DrmCommandStreamReceiver<GfxFamily>::CommandStreamReceiver::taskCount;
};
class DrmCommandStreamTest : public ::testing::Test {
public:
template <typename GfxFamily>
void setUpT() {
// make sure this is disabled, we don't want to test this now
debugManager.flags.EnableL3FlushAfterPostSync.set(0);
debugManager.flags.EnableForcePin.set(false);
mock = new DrmMock(mockFd, *executionEnvironment.rootDeviceEnvironments[0]);
auto hwInfo = executionEnvironment.rootDeviceEnvironments[0]->getHardwareInfo();
mock->setupIoctlHelper(hwInfo->platform.eProductFamily);
executionEnvironment.rootDeviceEnvironments[0]->osInterface = std::make_unique<OSInterface>();
executionEnvironment.rootDeviceEnvironments[0]->osInterface->setDriverModel(std::unique_ptr<DriverModel>(mock));
executionEnvironment.rootDeviceEnvironments[0]->memoryOperationsInterface = DrmMemoryOperationsHandler::create(*mock, 0u, false);
executionEnvironment.rootDeviceEnvironments[0]->initGmm();
gmmHelper = executionEnvironment.rootDeviceEnvironments[0]->getGmmHelper();
auto &gfxCoreHelper = executionEnvironment.rootDeviceEnvironments[0]->getHelper<GfxCoreHelper>();
mock->createVirtualMemoryAddressSpace(GfxCoreHelper::getSubDevicesCount(hwInfo));
osContext = std::make_unique<OsContextLinux>(*mock, 0, 0u,
EngineDescriptorHelper::getDefaultDescriptor(gfxCoreHelper.getGpgpuEngineInstances(*executionEnvironment.rootDeviceEnvironments[0])[0],
PreemptionHelper::getDefaultPreemptionMode(*hwInfo)));
osContext->ensureContextInitialized(false);
csr = new MockDrmCsr<GfxFamily>(executionEnvironment, 0, 1);
ASSERT_NE(nullptr, csr);
csr->setupContext(*osContext);
mock->ioctlCallsCount = 0u;
mock->memoryInfo.reset(new MockMemoryInfo{*mock});
memoryManager = new DrmMemoryManager(GemCloseWorkerMode::gemCloseWorkerActive,
debugManager.flags.EnableForcePin.get(),
true,
executionEnvironment);
executionEnvironment.memoryManager.reset(memoryManager);
mock->memoryInfo.reset();
// Memory manager creates pinBB with ioctl, expect one call
EXPECT_EQ(1u, mock->ioctlCallsCount);
// assert we have memory manager
ASSERT_NE(nullptr, memoryManager);
mock->ioctlCount.reset();
mock->ioctlTearDownExpected.reset();
}
template <typename GfxFamily>
void tearDownT() {
memoryManager->waitForDeletions();
if (memoryManager->peekGemCloseWorker()) {
memoryManager->peekGemCloseWorker()->close(true);
}
delete csr;
if (mock->ioctlTearDownExpects) {
EXPECT_EQ(mock->ioctlCount.gemWait, mock->ioctlTearDownExpected.gemWait);
EXPECT_EQ(mock->ioctlCount.gemClose, mock->ioctlTearDownExpected.gemClose);
}
// Expect 1 call with DRM_IOCTL_I915_GEM_CONTEXT_DESTROY request on destroyDrmContext
// Expect 1 call with DRM_IOCTL_GEM_CLOSE request on BufferObject close
mock->expectedIoctlCallsOnDestruction = mock->ioctlCallsCount + 2 + static_cast<uint32_t>(mock->virtualMemoryIds.size());
mock->expectIoctlCallsOnDestruction = true;
}
CommandStreamReceiver *csr = nullptr;
DrmMemoryManager *memoryManager = nullptr;
DrmMock *mock = nullptr;
const int mockFd = 33;
static const uint64_t alignment = MemoryConstants::allocationAlignment;
DebugManagerStateRestore dbgState;
MockExecutionEnvironment executionEnvironment;
std::unique_ptr<OsContextLinux> osContext;
GmmHelper *gmmHelper = nullptr;
};
template <typename DrmType>
class DrmCommandStreamEnhancedTemplate : public ::testing::Test {
public:
VariableBackup<bool> mockSipBackup{&MockSipData::useMockSip, false};
std::unique_ptr<DebugManagerStateRestore> dbgState;
MockExecutionEnvironment *executionEnvironment;
DrmType *mock;
CommandStreamReceiver *csr = nullptr;
const uint32_t rootDeviceIndex = 0u;
DrmMemoryManager *mm = nullptr;
std::unique_ptr<MockDevice> device;
template <typename GfxFamily>
void setUpT() {
executionEnvironment = new MockExecutionEnvironment();
executionEnvironment->incRefInternal();
executionEnvironment->initGmm();
this->dbgState = std::make_unique<DebugManagerStateRestore>();
// make sure this is disabled, we don't want to test this now
debugManager.flags.EnableForcePin.set(false);
debugManager.flags.EnableL3FlushAfterPostSync.set(0);
mock = DrmType::create(*executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]).release();
executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]->osInterface = std::make_unique<OSInterface>();
executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]->osInterface->setDriverModel(std::unique_ptr<DriverModel>(mock));
executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]->memoryOperationsInterface = DrmMemoryOperationsHandler::create(*mock, rootDeviceIndex, false);
csr = new TestedDrmCommandStreamReceiver<GfxFamily>(*executionEnvironment, rootDeviceIndex, 1);
ASSERT_NE(nullptr, csr);
mm = new DrmMemoryManager(GemCloseWorkerMode::gemCloseWorkerInactive,
debugManager.flags.EnableForcePin.get(),
true,
*executionEnvironment);
ASSERT_NE(nullptr, mm);
executionEnvironment->memoryManager.reset(mm);
executionEnvironment->prepareRootDeviceEnvironments(1u);
executionEnvironment->rootDeviceEnvironments[0]->setHwInfoAndInitHelpers(NEO::defaultHwInfo.get());
executionEnvironment->initializeMemoryManager();
device.reset(MockDevice::create<MockDevice>(executionEnvironment, rootDeviceIndex));
device->resetCommandStreamReceiver(csr);
ASSERT_NE(nullptr, device);
}
template <typename GfxFamily>
void tearDownT() {
executionEnvironment->decRefInternal();
device.reset();
}
template <typename GfxFamily>
void makeResidentBufferObjects(OsContext *osContext, DrmAllocation *drmAllocation) {
drmAllocation->bindBOs(osContext, 0u, &static_cast<TestedDrmCommandStreamReceiver<GfxFamily> *>(csr)->residency, false, false);
}
template <typename GfxFamily>
bool isResident(BufferObject *bo) const {
auto &residency = this->getResidencyVector<GfxFamily>();
return std::find(residency.begin(), residency.end(), bo) != residency.end();
}
template <typename GfxFamily>
const std::vector<BufferObject *> &getResidencyVector() const {
return static_cast<const TestedDrmCommandStreamReceiver<GfxFamily> *>(csr)->residency;
}
protected:
class MockBufferObject : public BufferObject {
friend DrmCommandStreamEnhancedTemplate<DrmType>;
protected:
MockBufferObject(uint32_t rootDeviceIndex, Drm *drm, size_t size) : BufferObject(rootDeviceIndex, drm, CommonConstants::unsupportedPatIndex, 1, 0, 16u) {
this->size = alignUp(size, 4096);
}
};
MockBufferObject *createBO(size_t size) {
return new MockBufferObject(0, this->mock, size);
}
};
using DrmCommandStreamEnhancedTest = DrmCommandStreamEnhancedTemplate<DrmMockCustom>;
template <typename T>
class DrmCommandStreamEnhancedWithFailingExecTemplate : public ::testing::Test {
public:
std::unique_ptr<DebugManagerStateRestore> dbgState;
MockExecutionEnvironment *executionEnvironment;
T *mock;
CommandStreamReceiver *csr = nullptr;
const uint32_t rootDeviceIndex = 0u;
DrmMemoryManager *mm = nullptr;
std::unique_ptr<MockDevice> device;
template <typename GfxFamily>
void setUpT() {
executionEnvironment = new MockExecutionEnvironment();
executionEnvironment->incRefInternal();
executionEnvironment->initGmm();
this->dbgState = std::make_unique<DebugManagerStateRestore>();
// make sure this is disabled, we don't want to test this now
debugManager.flags.EnableForcePin.set(false);
mock = T::create(*executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]).release();
executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]->osInterface = std::make_unique<OSInterface>();
executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]->osInterface->setDriverModel(std::unique_ptr<DriverModel>(mock));
executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]->memoryOperationsInterface = DrmMemoryOperationsHandler::create(*mock, rootDeviceIndex, false);
csr = new TestedDrmCommandStreamReceiverWithFailingExec<GfxFamily>(*executionEnvironment, rootDeviceIndex, 1);
ASSERT_NE(nullptr, csr);
mm = new DrmMemoryManager(GemCloseWorkerMode::gemCloseWorkerInactive,
debugManager.flags.EnableForcePin.get(),
true,
*executionEnvironment);
ASSERT_NE(nullptr, mm);
executionEnvironment->memoryManager.reset(mm);
executionEnvironment->prepareRootDeviceEnvironments(1u);
executionEnvironment->rootDeviceEnvironments[0]->setHwInfoAndInitHelpers(NEO::defaultHwInfo.get());
executionEnvironment->initializeMemoryManager();
device.reset(MockDevice::create<MockDevice>(executionEnvironment, rootDeviceIndex));
device->resetCommandStreamReceiver(csr);
ASSERT_NE(nullptr, device);
}
template <typename GfxFamily>
void tearDownT() {
executionEnvironment->decRefInternal();
}
template <typename GfxFamily>
void makeResidentBufferObjects(OsContext *osContext, DrmAllocation *drmAllocation) {
drmAllocation->bindBOs(osContext, 0u, &static_cast<TestedDrmCommandStreamReceiver<GfxFamily> *>(csr)->residency, false, false);
}
template <typename GfxFamily>
bool isResident(BufferObject *bo) const {
auto &residency = this->getResidencyVector<GfxFamily>();
return std::find(residency.begin(), residency.end(), bo) != residency.end();
}
template <typename GfxFamily>
const std::vector<BufferObject *> &getResidencyVector() const {
return static_cast<const TestedDrmCommandStreamReceiver<GfxFamily> *>(csr)->residency;
}
};
using DrmCommandStreamEnhancedWithFailingExec = DrmCommandStreamEnhancedWithFailingExecTemplate<DrmMockCustom>;
struct DrmCommandStreamDirectSubmissionTest : public DrmCommandStreamEnhancedTest {
template <typename GfxFamily>
void setUpT() {
debugManager.flags.EnableDirectSubmission.set(1u);
debugManager.flags.DirectSubmissionFlatRingBuffer.set(0);
DrmCommandStreamEnhancedTest::setUpT<GfxFamily>();
auto hwInfo = device->getRootDeviceEnvironment().getMutableHardwareInfo();
auto engineType = device->getDefaultEngine().osContext->getEngineType();
hwInfo->capabilityTable.directSubmissionEngines.data[engineType].engineSupported = true;
device->finalizeRayTracing();
csr->initDirectSubmission();
}
template <typename GfxFamily>
void tearDownT() {
csr->stopDirectSubmission(false, false);
this->dbgState.reset();
DrmCommandStreamEnhancedTest::tearDownT<GfxFamily>();
}
DebugManagerStateRestore restorer;
};
|