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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
/*
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/test/common/mocks/mock_wddm.h"
#include "shared/source/execution_environment/execution_environment.h"
#include "shared/source/execution_environment/root_device_environment.h"
#include "shared/source/gmm_helper/gmm.h"
#include "shared/source/helpers/aligned_memory.h"
#include "shared/source/os_interface/windows/gdi_interface.h"
#include "shared/source/os_interface/windows/os_environment_win.h"
#include "shared/source/os_interface/windows/wddm/um_km_data_translator.h"
#include "shared/source/os_interface/windows/wddm_allocation.h"
#include "shared/test/common/mock_gdi/mock_gdi.h"
#include "shared/test/common/mocks/mock_wddm_residency_allocations_container.h"
#include "shared/test/common/mocks/mock_wddm_residency_logger.h"
#include "gtest/gtest.h"
using namespace NEO;
namespace NEO {
NTSTATUS(*pCallEscape)
(D3DKMT_ESCAPE &escapeCommand) = nullptr;
uint32_t (*pGetTimestampFrequency)() = nullptr;
bool (*pPerfOpenEuStallStream)(uint32_t sampleRate, uint32_t minBufferSize) = nullptr;
bool (*pPerfDisableEuStallStream)() = nullptr;
bool (*pPerfReadEuStallStream)(uint8_t *pRawData, size_t *pRawDataSize) = nullptr;
} // namespace NEO
struct MockHwDeviceId : public HwDeviceIdWddm {
using HwDeviceIdWddm::osEnvironment;
};
WddmMock::WddmMock(RootDeviceEnvironment &rootDeviceEnvironment) : Wddm(std::make_unique<HwDeviceIdWddm>(ADAPTER_HANDLE, LUID{}, 1u, rootDeviceEnvironment.executionEnvironment.osEnvironment.get(), std::make_unique<UmKmDataTranslator>()), rootDeviceEnvironment) {
if (!rootDeviceEnvironment.executionEnvironment.osEnvironment.get()) {
rootDeviceEnvironment.executionEnvironment.osEnvironment = std::make_unique<OsEnvironmentWin>();
}
static_cast<MockHwDeviceId *>(this->hwDeviceId.get())->osEnvironment = rootDeviceEnvironment.executionEnvironment.osEnvironment.get();
this->temporaryResources = std::make_unique<MockWddmResidentAllocationsContainer>(this);
};
WddmMock::~WddmMock() {
EXPECT_EQ(0u, reservedAddresses.size());
}
bool WddmMock::makeResident(const D3DKMT_HANDLE *handles, uint32_t count, bool cantTrimFurther, uint64_t *numberOfBytesToTrim, size_t totalSize) {
makeResidentResult.called++;
makeResidentResult.handleCount = count;
makeResidentResult.cantTrimFurther = cantTrimFurther;
makeResidentResult.totalSize = totalSize;
if (handles) {
for (size_t i = 0; i < count; i++) {
makeResidentResult.handlePack.push_back(handles[i]);
}
}
makeResidentParamsPassed.push_back(makeResidentResult);
if (callBaseMakeResident) {
return Wddm::makeResident(handles, count, cantTrimFurther, numberOfBytesToTrim, totalSize);
}
if (numberOfBytesToTrim != nullptr) {
*numberOfBytesToTrim = makeResidentNumberOfBytesToTrim;
}
if (makeResidentResult.called <= makeResidentResults.size()) {
return makeResidentResults[makeResidentResult.called - 1];
}
return makeResidentStatus;
}
bool WddmMock::evict(const D3DKMT_HANDLE *handles, uint32_t num, uint64_t &sizeToTrim, bool evictNeeded) {
evictResult.called++;
if (callBaseEvict) {
evictStatus = Wddm::evict(handles, num, sizeToTrim, evictNeeded);
}
return evictStatus;
}
NTSTATUS WddmMock::createAllocationsAndMapGpuVa(OsHandleStorage &osHandles) {
createAllocationsAndMapGpuVaResult.called++;
if (callBaseCreateAllocationsAndMapGpuVa) {
createAllocationsAndMapGpuVaStatus = Wddm::createAllocationsAndMapGpuVa(osHandles);
}
return createAllocationsAndMapGpuVaStatus;
}
NTSTATUS WddmMock::escape(D3DKMT_ESCAPE &escapeCommand) {
if (pCallEscape != nullptr) {
return pCallEscape(escapeCommand);
}
return Wddm::escape(escapeCommand);
}
bool WddmMock::mapGpuVirtualAddress(WddmAllocation *allocation) {
D3DGPU_VIRTUAL_ADDRESS minimumAddress = gfxPartition.Standard.Base;
D3DGPU_VIRTUAL_ADDRESS maximumAddress = gfxPartition.Standard.Limit;
if (allocation->getAlignedCpuPtr()) {
minimumAddress = 0u;
maximumAddress = MemoryConstants::maxSvmAddress;
}
return mapGpuVirtualAddress(allocation->getDefaultGmm(), allocation->getDefaultHandle(), minimumAddress, maximumAddress,
reinterpret_cast<D3DGPU_VIRTUAL_ADDRESS>(allocation->getAlignedCpuPtr()), allocation->getGpuAddressToModify(), allocation->getAllocationType());
}
bool WddmMock::mapGpuVirtualAddress(Gmm *gmm, D3DKMT_HANDLE handle, D3DGPU_VIRTUAL_ADDRESS minimumAddress, D3DGPU_VIRTUAL_ADDRESS maximumAddress, D3DGPU_VIRTUAL_ADDRESS preferredAddress, D3DGPU_VIRTUAL_ADDRESS &gpuPtr, AllocationType type) {
mapGpuVirtualAddressResult.called++;
mapGpuVirtualAddressResult.cpuPtrPassed = reinterpret_cast<void *>(preferredAddress);
mapGpuVirtualAddressResult.uint64ParamPassed = preferredAddress;
mapGpuVirtualAddressResult.alignment = gmm->resourceParams.BaseAlignment;
if (callBaseMapGpuVa) {
return mapGpuVirtualAddressResult.success = Wddm::mapGpuVirtualAddress(gmm, handle, minimumAddress, maximumAddress, preferredAddress, gpuPtr, type);
} else {
gpuPtr = preferredAddress;
return mapGpuVaStatus;
}
}
bool WddmMock::freeGpuVirtualAddress(D3DGPU_VIRTUAL_ADDRESS &gpuPtr, uint64_t size) {
freeGpuVirtualAddressResult.called++;
freeGpuVirtualAddressResult.uint64ParamPassed = gpuPtr;
freeGpuVirtualAddressResult.sizePassed = size;
return freeGpuVirtualAddressResult.success = Wddm::freeGpuVirtualAddress(gpuPtr, size);
}
NTSTATUS WddmMock::createAllocation(WddmAllocation *wddmAllocation) {
if (wddmAllocation) {
return createAllocation(wddmAllocation->getAlignedCpuPtr(), wddmAllocation->getDefaultGmm(), wddmAllocation->getHandleToModify(0u), wddmAllocation->getResourceHandleToModify(), wddmAllocation->getSharedHandleToModify());
}
return false;
}
NTSTATUS WddmMock::createAllocation(const void *alignedCpuPtr, const Gmm *gmm, D3DKMT_HANDLE &outHandle, D3DKMT_HANDLE &outResourceHandle, uint64_t *outSharedHandle) {
createAllocationResult.called++;
if (failCreateAllocation) {
return STATUS_NO_MEMORY;
}
if (callBaseDestroyAllocations) {
createAllocationStatus = Wddm::createAllocation(alignedCpuPtr, gmm, outHandle, outResourceHandle, outSharedHandle);
createAllocationResult.success = createAllocationStatus == STATUS_SUCCESS;
} else {
createAllocationResult.success = true;
outHandle = ALLOCATION_HANDLE;
return createAllocationStatus;
}
return createAllocationStatus;
}
NTSTATUS WddmMock::createAllocation(const void *alignedCpuPtr, const Gmm *gmm, D3DKMT_HANDLE &outHandle, D3DKMT_HANDLE &outResourceHandle, uint64_t *outSharedHandle, bool createNTHandle) {
createAllocationResult.called++;
if (failCreateAllocation) {
return STATUS_NO_MEMORY;
}
if (callBaseDestroyAllocations) {
createAllocationStatus = Wddm::createAllocation(alignedCpuPtr, gmm, outHandle, outResourceHandle, outSharedHandle, createNTHandle);
createAllocationResult.success = createAllocationStatus == STATUS_SUCCESS;
if (createAllocationStatus != STATUS_SUCCESS) {
destroyAllocationResult.called++;
}
} else {
createAllocationResult.success = true;
outHandle = ALLOCATION_HANDLE;
outResourceHandle = ALLOCATION_HANDLE;
if (outSharedHandle && !createNTHandle) {
// For shared allocations without NT handle, set a special value
*outSharedHandle = 1u;
}
return createAllocationStatus;
}
return createAllocationStatus;
}
bool WddmMock::createAllocation64k(WddmAllocation *wddmAllocation) {
if (wddmAllocation) {
return createAllocation(wddmAllocation->getDefaultGmm(), wddmAllocation->getHandleToModify(0u));
}
return false;
}
bool WddmMock::createAllocation(const Gmm *gmm, D3DKMT_HANDLE &outHandle) {
createAllocationResult.called++;
return createAllocationResult.success = Wddm::createAllocation(gmm, outHandle);
}
bool WddmMock::destroyAllocations(const D3DKMT_HANDLE *handles, uint32_t allocationCount, D3DKMT_HANDLE resourceHandle) {
destroyAllocationResult.called++;
if (callBaseDestroyAllocations) {
return destroyAllocationResult.success = Wddm::destroyAllocations(handles, allocationCount, resourceHandle);
} else {
return true;
}
}
bool WddmMock::destroyAllocation(WddmAllocation *alloc, OsContextWin *osContext) {
const D3DKMT_HANDLE *allocationHandles = nullptr;
uint32_t allocationCount = 0;
D3DKMT_HANDLE resourceHandle = 0;
void *reserveAddress = alloc->getReservedAddressPtr();
if (alloc->peekSharedHandle()) {
resourceHandle = alloc->getResourceHandle();
} else {
allocationHandles = &alloc->getHandles()[0];
allocationCount = static_cast<uint32_t>(alloc->getHandles().size());
}
auto success = destroyAllocations(allocationHandles, allocationCount, resourceHandle);
::alignedFree(alloc->getDriverAllocatedCpuPtr());
releaseReservedAddress(reserveAddress);
return success;
}
bool WddmMock::openSharedHandle(const MemoryManager::OsHandleData &osHandleData, WddmAllocation *alloc) {
if (failOpenSharedHandle) {
return false;
} else {
return Wddm::openSharedHandle(osHandleData, alloc);
}
}
bool WddmMock::createContext(OsContextWin &osContext) {
createContextResult.called++;
return createContextResult.success = Wddm::createContext(osContext);
}
void WddmMock::applyAdditionalContextFlags(CREATECONTEXT_PVTDATA &privateData, OsContextWin &osContext) {
applyAdditionalContextFlagsResult.called++;
Wddm::applyAdditionalContextFlags(privateData, osContext);
}
bool WddmMock::destroyContext(D3DKMT_HANDLE context) {
destroyContextResult.called++;
return destroyContextResult.success = Wddm::destroyContext(context);
}
bool WddmMock::queryAdapterInfo() {
queryAdapterInfoResult.called++;
return queryAdapterInfoResult.success = Wddm::queryAdapterInfo();
}
bool WddmMock::submit(uint64_t commandBuffer, size_t size, void *commandHeader, WddmSubmitArguments &submitArguments) {
submitResult.called++;
submitResult.commandBufferSubmitted = commandBuffer;
submitResult.size = size;
submitResult.commandHeaderSubmitted = commandHeader;
submitResult.submitArgs = submitArguments;
return submitResult.success = Wddm::submit(commandBuffer, size, commandHeader, submitArguments);
}
bool WddmMock::waitOnGPU(D3DKMT_HANDLE context) {
waitOnGPUResult.called++;
return waitOnGPUResult.success = Wddm::waitOnGPU(context);
}
void *WddmMock::lockResource(const D3DKMT_HANDLE &handle, bool applyMakeResidentPriorToLock, size_t size) {
lockResult.called++;
auto ptr = Wddm::lockResource(handle, applyMakeResidentPriorToLock, size);
lockResult.success = ptr != nullptr;
lockResult.uint64ParamPassed = applyMakeResidentPriorToLock;
return ptr;
}
void WddmMock::unlockResource(const D3DKMT_HANDLE &handle, bool applyMakeResidentPriorToLock) {
unlockResult.called++;
unlockResult.success = true;
Wddm::unlockResource(handle, applyMakeResidentPriorToLock);
}
void WddmMock::setHwContextId(unsigned long hwContextId) {
this->hwContextId = hwContextId;
}
void WddmMock::resetGdi(Gdi *gdi) {
static_cast<OsEnvironmentWin *>(this->rootDeviceEnvironment.executionEnvironment.osEnvironment.get())->gdi.reset(gdi);
}
void WddmMock::setHeap32(uint64_t base, uint64_t size) {
gfxPartition.Heap32[3].Base = base;
gfxPartition.Heap32[3].Limit = base + size;
}
GMM_GFX_PARTITIONING *WddmMock::getGfxPartitionPtr() {
return &gfxPartition;
}
bool WddmMock::waitFromCpu(uint64_t lastFenceValue, const MonitoredFence &monitoredFence, bool busyWait) {
waitFromCpuResult.called++;
waitFromCpuResult.uint64ParamPassed = lastFenceValue;
waitFromCpuResult.monitoredFence = &monitoredFence;
if (callBaseWaitFromCpu) {
return waitFromCpuResult.success = Wddm::waitFromCpu(lastFenceValue, monitoredFence, busyWait);
}
return waitFromCpuResult.success = true;
}
void *WddmMock::virtualAlloc(void *inPtr, size_t size, bool topDownHint) {
void *address = Wddm::virtualAlloc(inPtr, size, topDownHint);
virtualAllocAddress = reinterpret_cast<uintptr_t>(address);
return address;
}
void WddmMock::virtualFree(void *ptr, size_t size) {
Wddm::virtualFree(ptr, size);
}
void WddmMock::releaseReservedAddress(void *reservedAddress) {
releaseReservedAddressResult.called++;
if (reservedAddress != nullptr) {
std::unordered_multiset<void *>::iterator it;
it = reservedAddresses.find(reservedAddress);
EXPECT_NE(reservedAddresses.end(), it);
reservedAddresses.erase(it);
}
Wddm::releaseReservedAddress(reservedAddress);
}
bool WddmMock::reserveValidAddressRange(size_t size, void *&reservedMem) {
reserveValidAddressRangeResult.called++;
bool ret = Wddm::reserveValidAddressRange(size, reservedMem);
if (reservedMem != nullptr) {
std::unordered_multiset<void *>::iterator it;
it = reservedAddresses.find(reservedMem);
reservedAddresses.insert(reservedMem);
}
return ret;
}
VOID *WddmMock::registerTrimCallback(PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback, WddmResidencyController &residencyController) {
registerTrimCallbackResult.called++;
return Wddm::registerTrimCallback(callback, residencyController);
}
NTSTATUS WddmMock::reserveGpuVirtualAddress(D3DGPU_VIRTUAL_ADDRESS baseAddress, D3DGPU_VIRTUAL_ADDRESS minimumAddress, D3DGPU_VIRTUAL_ADDRESS maximumAddress, D3DGPU_SIZE_T size, D3DGPU_VIRTUAL_ADDRESS *reservedAddress) {
reserveGpuVirtualAddressResult.called++;
if (failReserveGpuVirtualAddress) {
D3DGPU_VIRTUAL_ADDRESS nullAddress = {};
*reservedAddress = nullAddress;
return STATUS_INVALID_PARAMETER;
}
return Wddm::reserveGpuVirtualAddress(baseAddress, minimumAddress, maximumAddress, size, reservedAddress);
}
volatile uint64_t *WddmMock::getPagingFenceAddress() {
if (NEO::wddmResidencyLoggingAvailable) {
getPagingFenceAddressResult.called++;
}
mockPagingFence++;
return &mockPagingFence;
}
void WddmMock::waitOnPagingFenceFromCpu(bool isKmdWaitNeeded) {
waitOnPagingFenceFromCpuResult.called++;
waitOnPagingFenceFromCpuResult.isKmdWaitNeededPassed = isKmdWaitNeeded;
Wddm::waitOnPagingFenceFromCpu(isKmdWaitNeeded);
}
void WddmMock::delayPagingFenceFromCpu(int64_t delayTime) {
delayPagingFenceFromCpuResult.called++;
Wddm::delayPagingFenceFromCpu(delayTime);
}
void WddmMock::createPagingFenceLogger() {
if (callBaseCreatePagingLogger) {
Wddm::createPagingFenceLogger();
} else {
if (debugManager.flags.WddmResidencyLogger.get()) {
residencyLogger = std::make_unique<MockWddmResidencyLogger>(device, pagingFenceAddress, debugManager.flags.WddmResidencyLoggerOutputDirectory.get());
}
}
}
bool WddmMock::setAllocationPriority(const D3DKMT_HANDLE *handles, uint32_t allocationCount, uint32_t priority) {
if (callBaseSetAllocationPriority) {
auto status = Wddm::setAllocationPriority(handles, allocationCount, priority);
setAllocationPriorityResult.success = status;
setAllocationPriorityResult.called++;
setAllocationPriorityResult.uint64ParamPassed = priority;
return status;
}
return setAllocationPriorityResult.success;
}
bool WddmMock::perfOpenEuStallStream(uint32_t sampleRate, uint32_t minBufferSize) {
if (pPerfOpenEuStallStream != nullptr) {
return pPerfOpenEuStallStream(sampleRate, minBufferSize);
}
return Wddm::perfOpenEuStallStream(sampleRate, minBufferSize);
}
bool WddmMock::perfDisableEuStallStream() {
if (pPerfDisableEuStallStream != nullptr) {
return pPerfDisableEuStallStream();
}
return Wddm::perfDisableEuStallStream();
}
bool WddmMock::perfReadEuStallStream(uint8_t *pRawData, size_t *pRawDataSize) {
if (pPerfReadEuStallStream != nullptr) {
return pPerfReadEuStallStream(pRawData, pRawDataSize);
}
return Wddm::perfReadEuStallStream(pRawData, pRawDataSize);
}
uint32_t WddmMock::getTimestampFrequency() const {
if (pGetTimestampFrequency != nullptr) {
return pGetTimestampFrequency();
}
return Wddm::getTimestampFrequency();
}
|