|
| 1 | +/* |
| 2 | +* Copyright (c) 2018, Intel Corporation |
| 3 | +* |
| 4 | +* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | +* copy of this software and associated documentation files (the "Software"), |
| 6 | +* to deal in the Software without restriction, including without limitation |
| 7 | +* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | +* and/or sell copies of the Software, and to permit persons to whom the |
| 9 | +* Software is furnished to do so, subject to the following conditions: |
| 10 | +* |
| 11 | +* The above copyright notice and this permission notice shall be included |
| 12 | +* in all copies or substantial portions of the Software. |
| 13 | +* |
| 14 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | +* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | +* OTHER DEALINGS IN THE SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +#include "runtime/helpers/aligned_memory.h" |
| 24 | +#include "runtime/os_interface/windows/wddm_allocation.h" |
| 25 | +#include "unit_tests/mocks/mock_wddm.h" |
| 26 | +#include "unit_tests/mock_gdi/mock_gdi.h" |
| 27 | + |
| 28 | +#include "gtest/gtest.h" |
| 29 | + |
| 30 | +using namespace OCLRT; |
| 31 | + |
| 32 | +WddmMock::~WddmMock() { |
| 33 | + EXPECT_EQ(0, reservedAddresses.size()); |
| 34 | +} |
| 35 | + |
| 36 | +bool WddmMock::makeResident(D3DKMT_HANDLE *handles, uint32_t count, bool cantTrimFurther, uint64_t *numberOfBytesToTrim) { |
| 37 | + makeResidentResult.called++; |
| 38 | + makeResidentResult.handleCount = count; |
| 39 | + for (auto i = 0u; i < count; i++) { |
| 40 | + makeResidentResult.handlePack.push_back(handles[i]); |
| 41 | + } |
| 42 | + |
| 43 | + return makeResidentResult.success = Wddm::makeResident(handles, count, cantTrimFurther, numberOfBytesToTrim); |
| 44 | +} |
| 45 | + |
| 46 | +bool WddmMock::evict(D3DKMT_HANDLE *handles, uint32_t num, uint64_t &sizeToTrim) { |
| 47 | + makeNonResidentResult.called++; |
| 48 | + return makeNonResidentResult.success = Wddm::evict(handles, num, sizeToTrim); |
| 49 | +} |
| 50 | + |
| 51 | +bool WddmMock::mapGpuVirtualAddressImpl(Gmm *gmm, D3DKMT_HANDLE handle, void *cpuPtr, uint64_t size, D3DGPU_VIRTUAL_ADDRESS &gpuPtr, bool allocation32Bit, bool use64kbPages, bool useHeap1) { |
| 52 | + mapGpuVirtualAddressResult.called++; |
| 53 | + mapGpuVirtualAddressResult.cpuPtrPassed = cpuPtr; |
| 54 | + if (callBaseMapGpuVa) { |
| 55 | + return mapGpuVirtualAddressResult.success = Wddm::mapGpuVirtualAddressImpl(gmm, handle, cpuPtr, size, gpuPtr, allocation32Bit, use64kbPages, useHeap1); |
| 56 | + } else { |
| 57 | + gpuPtr = reinterpret_cast<D3DGPU_VIRTUAL_ADDRESS>(cpuPtr); |
| 58 | + return mapGpuVaStatus; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +bool WddmMock::freeGpuVirtualAddres(D3DGPU_VIRTUAL_ADDRESS &gpuPtr, uint64_t size) { |
| 63 | + freeGpuVirtualAddresResult.called++; |
| 64 | + return freeGpuVirtualAddresResult.success = Wddm::freeGpuVirtualAddres(gpuPtr, size); |
| 65 | +} |
| 66 | + |
| 67 | +NTSTATUS WddmMock::createAllocation(WddmAllocation *alloc) { |
| 68 | + createAllocationResult.called++; |
| 69 | + if (callBaseDestroyAllocations) { |
| 70 | + createAllocationStatus = Wddm::createAllocation(alloc); |
| 71 | + createAllocationResult.success = createAllocationStatus == STATUS_SUCCESS; |
| 72 | + } else { |
| 73 | + createAllocationResult.success = true; |
| 74 | + alloc->handle = ALLOCATION_HANDLE; |
| 75 | + return createAllocationStatus; |
| 76 | + } |
| 77 | + return createAllocationStatus; |
| 78 | +} |
| 79 | + |
| 80 | +bool WddmMock::createAllocation64k(WddmAllocation *alloc) { |
| 81 | + createAllocationResult.called++; |
| 82 | + return createAllocationResult.success = Wddm::createAllocation64k(alloc); |
| 83 | +} |
| 84 | + |
| 85 | +bool WddmMock::destroyAllocations(D3DKMT_HANDLE *handles, uint32_t allocationCount, uint64_t lastFenceValue, D3DKMT_HANDLE resourceHandle) { |
| 86 | + destroyAllocationResult.called++; |
| 87 | + if (callBaseDestroyAllocations) { |
| 88 | + return destroyAllocationResult.success = Wddm::destroyAllocations(handles, allocationCount, lastFenceValue, resourceHandle); |
| 89 | + } else { |
| 90 | + return true; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +bool WddmMock::destroyAllocation(WddmAllocation *alloc) { |
| 95 | + D3DKMT_HANDLE *allocationHandles = nullptr; |
| 96 | + uint32_t allocationCount = 0; |
| 97 | + D3DKMT_HANDLE resourceHandle = 0; |
| 98 | + void *cpuPtr = nullptr; |
| 99 | + void *reserveAddress = alloc->getReservedAddress(); |
| 100 | + if (alloc->peekSharedHandle()) { |
| 101 | + resourceHandle = alloc->resourceHandle; |
| 102 | + } else { |
| 103 | + allocationHandles = &alloc->handle; |
| 104 | + allocationCount = 1; |
| 105 | + if (alloc->cpuPtrAllocated) { |
| 106 | + cpuPtr = alloc->getAlignedCpuPtr(); |
| 107 | + } |
| 108 | + } |
| 109 | + auto success = destroyAllocations(allocationHandles, allocationCount, alloc->getResidencyData().lastFence, resourceHandle); |
| 110 | + ::alignedFree(cpuPtr); |
| 111 | + releaseReservedAddress(reserveAddress); |
| 112 | + return success; |
| 113 | +} |
| 114 | + |
| 115 | +bool WddmMock::openSharedHandle(D3DKMT_HANDLE handle, WddmAllocation *alloc) { |
| 116 | + if (failOpenSharedHandle) { |
| 117 | + return false; |
| 118 | + } else { |
| 119 | + return Wddm::openSharedHandle(handle, alloc); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +bool WddmMock::createContext() { |
| 124 | + createContextResult.called++; |
| 125 | + return createContextResult.success = Wddm::createContext(); |
| 126 | +} |
| 127 | + |
| 128 | +bool WddmMock::destroyContext(D3DKMT_HANDLE context) { |
| 129 | + destroyContextResult.called++; |
| 130 | + return destroyContextResult.success = Wddm::destroyContext(context); |
| 131 | +} |
| 132 | + |
| 133 | +bool WddmMock::queryAdapterInfo() { |
| 134 | + queryAdapterInfoResult.called++; |
| 135 | + return queryAdapterInfoResult.success = Wddm::queryAdapterInfo(); |
| 136 | +} |
| 137 | + |
| 138 | +bool WddmMock::submit(uint64_t commandBuffer, size_t size, void *commandHeader) { |
| 139 | + submitResult.called++; |
| 140 | + submitResult.commandBufferSubmitted = commandBuffer; |
| 141 | + submitResult.commandHeaderSubmitted = commandHeader; |
| 142 | + return submitResult.success = Wddm::submit(commandBuffer, size, commandHeader); |
| 143 | +} |
| 144 | + |
| 145 | +bool WddmMock::waitOnGPU() { |
| 146 | + waitOnGPUResult.called++; |
| 147 | + return waitOnGPUResult.success = Wddm::waitOnGPU(); |
| 148 | +} |
| 149 | + |
| 150 | +void *WddmMock::lockResource(WddmAllocation *allocation) { |
| 151 | + lockResult.called++; |
| 152 | + auto ptr = Wddm::lockResource(allocation); |
| 153 | + lockResult.success = ptr != nullptr; |
| 154 | + return ptr; |
| 155 | +} |
| 156 | + |
| 157 | +void WddmMock::unlockResource(WddmAllocation *allocation) { |
| 158 | + unlockResult.called++; |
| 159 | + unlockResult.success = true; |
| 160 | + Wddm::unlockResource(allocation); |
| 161 | +} |
| 162 | + |
| 163 | +void WddmMock::kmDafLock(WddmAllocation *allocation) { |
| 164 | + kmDafLockResult.called++; |
| 165 | + kmDafLockResult.success = true; |
| 166 | + kmDafLockResult.lockedAllocations.push_back(allocation); |
| 167 | + Wddm::kmDafLock(allocation); |
| 168 | +} |
| 169 | + |
| 170 | +bool WddmMock::isKmDafEnabled() { |
| 171 | + return kmDafEnabled; |
| 172 | +} |
| 173 | + |
| 174 | +void WddmMock::setKmDafEnabled(bool state) { |
| 175 | + kmDafEnabled = state; |
| 176 | +} |
| 177 | + |
| 178 | +void WddmMock::setHwContextId(unsigned long hwContextId) { |
| 179 | + this->hwContextId = hwContextId; |
| 180 | +} |
| 181 | + |
| 182 | +bool WddmMock::openAdapter() { |
| 183 | + this->adapter = ADAPTER_HANDLE; |
| 184 | + return true; |
| 185 | +} |
| 186 | + |
| 187 | +void WddmMock::setHeap32(uint64_t base, uint64_t size) { |
| 188 | + gfxPartition.Heap32[0].Base = base; |
| 189 | + gfxPartition.Heap32[0].Limit = size; |
| 190 | +} |
| 191 | + |
| 192 | +GMM_GFX_PARTITIONING *WddmMock::getGfxPartitionPtr() { |
| 193 | + return &gfxPartition; |
| 194 | +} |
| 195 | + |
| 196 | +bool WddmMock::waitFromCpu(uint64_t lastFenceValue) { |
| 197 | + waitFromCpuResult.called++; |
| 198 | + waitFromCpuResult.uint64ParamPassed = lastFenceValue; |
| 199 | + return waitFromCpuResult.success = Wddm::waitFromCpu(lastFenceValue); |
| 200 | +} |
| 201 | + |
| 202 | +void *WddmMock::virtualAlloc(void *inPtr, size_t size, unsigned long flags, unsigned long type) { |
| 203 | + void *address = Wddm::virtualAlloc(inPtr, size, flags, type); |
| 204 | + virtualAllocAddress = reinterpret_cast<uintptr_t>(address); |
| 205 | + return address; |
| 206 | +} |
| 207 | + |
| 208 | +int WddmMock::virtualFree(void *ptr, size_t size, unsigned long flags) { |
| 209 | + int success = Wddm::virtualFree(ptr, size, flags); |
| 210 | + return success; |
| 211 | +} |
| 212 | + |
| 213 | +void WddmMock::releaseReservedAddress(void *reservedAddress) { |
| 214 | + releaseReservedAddressResult.called++; |
| 215 | + if (reservedAddress != nullptr) { |
| 216 | + std::set<void *>::iterator it; |
| 217 | + it = reservedAddresses.find(reservedAddress); |
| 218 | + EXPECT_NE(reservedAddresses.end(), it); |
| 219 | + reservedAddresses.erase(it); |
| 220 | + } |
| 221 | + Wddm::releaseReservedAddress(reservedAddress); |
| 222 | +} |
| 223 | + |
| 224 | +bool WddmMock::reserveValidAddressRange(size_t size, void *&reservedMem) { |
| 225 | + reserveValidAddressRangeResult.called++; |
| 226 | + bool ret = Wddm::reserveValidAddressRange(size, reservedMem); |
| 227 | + if (reservedMem != nullptr) { |
| 228 | + std::set<void *>::iterator it; |
| 229 | + it = reservedAddresses.find(reservedMem); |
| 230 | + EXPECT_EQ(reservedAddresses.end(), it); |
| 231 | + reservedAddresses.insert(reservedMem); |
| 232 | + } |
| 233 | + return ret; |
| 234 | +} |
| 235 | + |
| 236 | +GmmMemory *WddmMock::getGmmMemory() const { |
| 237 | + return gmmMemory.get(); |
| 238 | +} |
0 commit comments