2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2018-09-18 15:11:08 +08:00
|
|
|
* Copyright (C) 2017-2018 Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
2018-09-18 15:11:08 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-07-16 23:30:56 +08:00
|
|
|
#include "runtime/os_interface/windows/wddm_memory_manager.h"
|
2018-01-17 23:23:51 +08:00
|
|
|
#include "runtime/command_stream/command_stream_receiver_hw.h"
|
|
|
|
#include "runtime/device/device.h"
|
2018-06-21 17:36:47 +08:00
|
|
|
#include "runtime/gmm_helper/gmm.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
#include "runtime/gmm_helper/gmm_helper.h"
|
|
|
|
#include "runtime/gmm_helper/resource_info.h"
|
2018-07-16 23:30:56 +08:00
|
|
|
#include "runtime/helpers/aligned_memory.h"
|
|
|
|
#include "runtime/helpers/ptr_math.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
#include "runtime/helpers/surface_formats.h"
|
|
|
|
#include "runtime/memory_manager/deferrable_deletion.h"
|
2018-07-16 23:30:56 +08:00
|
|
|
#include "runtime/memory_manager/deferred_deleter.h"
|
2018-05-10 17:42:41 +08:00
|
|
|
#include "runtime/os_interface/windows/wddm/wddm.h"
|
2018-07-16 23:30:56 +08:00
|
|
|
#include "runtime/os_interface/windows/wddm_allocation.h"
|
2018-10-04 06:52:51 +08:00
|
|
|
#include "runtime/os_interface/windows/wddm_residency_controller.h"
|
2018-08-27 21:48:29 +08:00
|
|
|
#include "runtime/os_interface/windows/os_context_win.h"
|
|
|
|
#include "runtime/platform/platform.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace OCLRT {
|
|
|
|
|
|
|
|
WddmMemoryManager::~WddmMemoryManager() {
|
|
|
|
applyCommonCleanup();
|
|
|
|
}
|
|
|
|
|
2018-10-04 06:52:51 +08:00
|
|
|
WddmMemoryManager::WddmMemoryManager(bool enable64kbPages, bool enableLocalMemory, Wddm *wddm, ExecutionEnvironment &executionEnvironment) : MemoryManager(enable64kbPages, enableLocalMemory, executionEnvironment) {
|
2017-12-21 07:45:38 +08:00
|
|
|
DEBUG_BREAK_IF(wddm == nullptr);
|
|
|
|
this->wddm = wddm;
|
|
|
|
allocator32Bit = std::unique_ptr<Allocator32bit>(new Allocator32bit(wddm->getHeap32Base(), wddm->getHeap32Size()));
|
|
|
|
wddm->registerTrimCallback(trimCallback, this);
|
|
|
|
asyncDeleterEnabled = DebugManager.flags.EnableDeferredDeleter.get();
|
|
|
|
if (asyncDeleterEnabled)
|
|
|
|
deferredDeleter = createDeferredDeleter();
|
2018-01-22 23:43:26 +08:00
|
|
|
mallocRestrictions.minAddress = wddm->getWddmMinAddress();
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY WddmMemoryManager::trimCallback(_Inout_ D3DKMT_TRIMNOTIFICATION *trimNotification) {
|
|
|
|
WddmMemoryManager *wddmMemMngr = (WddmMemoryManager *)trimNotification->Context;
|
|
|
|
DEBUG_BREAK_IF(wddmMemMngr == nullptr);
|
|
|
|
|
|
|
|
wddmMemMngr->trimResidency(trimNotification->Flags, trimNotification->NumBytesToTrim);
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemoryForImage(ImageInfo &imgInfo, Gmm *gmm) {
|
2018-06-21 17:36:47 +08:00
|
|
|
if (!GmmHelper::allowTiling(*imgInfo.imgDesc) && imgInfo.mipCount == 0) {
|
2017-12-21 07:45:38 +08:00
|
|
|
delete gmm;
|
2018-07-09 20:12:32 +08:00
|
|
|
return allocateGraphicsMemory(imgInfo.size);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
2018-07-14 00:50:55 +08:00
|
|
|
auto allocation = new WddmAllocation(nullptr, imgInfo.size, nullptr, MemoryPool::SystemCpuInaccessible);
|
2018-03-01 17:08:20 +08:00
|
|
|
allocation->gmm = gmm;
|
|
|
|
|
2018-05-11 23:24:00 +08:00
|
|
|
if (!WddmMemoryManager::createWddmAllocation(allocation, AllocationOrigin::EXTERNAL_ALLOCATION)) {
|
2018-03-01 17:08:20 +08:00
|
|
|
delete allocation;
|
2018-02-07 05:43:38 +08:00
|
|
|
return nullptr;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
2018-03-01 17:08:20 +08:00
|
|
|
return allocation;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
2018-07-23 01:27:33 +08:00
|
|
|
GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory64kb(size_t size, size_t alignment, bool forcePin, bool preferRenderCompressed) {
|
2017-12-21 07:45:38 +08:00
|
|
|
size_t sizeAligned = alignUp(size, MemoryConstants::pageSize64k);
|
|
|
|
Gmm *gmm = nullptr;
|
|
|
|
|
2018-07-14 00:50:55 +08:00
|
|
|
auto wddmAllocation = new WddmAllocation(nullptr, sizeAligned, nullptr, sizeAligned, nullptr, MemoryPool::System64KBPages);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-08-03 00:24:59 +08:00
|
|
|
gmm = new Gmm(nullptr, sizeAligned, false, preferRenderCompressed, true);
|
2018-03-01 17:08:20 +08:00
|
|
|
wddmAllocation->gmm = gmm;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-03-01 17:08:20 +08:00
|
|
|
if (!wddm->createAllocation64k(wddmAllocation)) {
|
|
|
|
delete gmm;
|
|
|
|
delete wddmAllocation;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-03-01 17:08:20 +08:00
|
|
|
auto cpuPtr = lockResource(wddmAllocation);
|
|
|
|
wddmAllocation->setLocked(true);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-03-01 17:08:20 +08:00
|
|
|
wddmAllocation->setAlignedCpuPtr(cpuPtr);
|
|
|
|
// 64kb map is not needed
|
2018-07-12 21:42:46 +08:00
|
|
|
auto status = wddm->mapGpuVirtualAddress(wddmAllocation, cpuPtr, false, false, false);
|
2018-03-01 17:08:20 +08:00
|
|
|
DEBUG_BREAK_IF(!status);
|
|
|
|
wddmAllocation->setCpuPtrAndGpuAddress(cpuPtr, (uint64_t)wddmAllocation->gpuPtr);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-03-01 17:08:20 +08:00
|
|
|
return wddmAllocation;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
2018-01-05 00:48:46 +08:00
|
|
|
GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(size_t size, size_t alignment, bool forcePin, bool uncacheable) {
|
2017-12-21 07:45:38 +08:00
|
|
|
size_t newAlignment = alignment ? alignUp(alignment, MemoryConstants::pageSize) : MemoryConstants::pageSize;
|
|
|
|
size_t sizeAligned = size ? alignUp(size, MemoryConstants::pageSize) : MemoryConstants::pageSize;
|
|
|
|
void *pSysMem = allocateSystemMemory(sizeAligned, newAlignment);
|
|
|
|
Gmm *gmm = nullptr;
|
|
|
|
|
|
|
|
if (pSysMem == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-14 00:50:55 +08:00
|
|
|
auto wddmAllocation = new WddmAllocation(pSysMem, sizeAligned, pSysMem, sizeAligned, nullptr, MemoryPool::System4KBPages);
|
2018-03-01 17:08:20 +08:00
|
|
|
wddmAllocation->cpuPtrAllocated = true;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-06-29 17:48:19 +08:00
|
|
|
gmm = new Gmm(pSysMem, sizeAligned, uncacheable);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-03-01 17:08:20 +08:00
|
|
|
wddmAllocation->gmm = gmm;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-05-11 23:24:00 +08:00
|
|
|
if (!createWddmAllocation(wddmAllocation, AllocationOrigin::EXTERNAL_ALLOCATION)) {
|
2018-03-01 17:08:20 +08:00
|
|
|
delete gmm;
|
|
|
|
delete wddmAllocation;
|
|
|
|
freeSystemMemory(pSysMem);
|
|
|
|
return nullptr;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
2018-03-01 17:08:20 +08:00
|
|
|
return wddmAllocation;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
2018-08-24 21:23:45 +08:00
|
|
|
GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemoryForNonSvmHostPtr(size_t size, void *cpuPtr) {
|
2018-09-10 18:50:45 +08:00
|
|
|
auto alignedPtr = alignDown(cpuPtr, MemoryConstants::pageSize);
|
|
|
|
auto offsetInPage = ptrDiff(cpuPtr, alignedPtr);
|
2018-08-24 21:23:45 +08:00
|
|
|
auto alignedSize = alignSizeWholePage(cpuPtr, size);
|
|
|
|
|
|
|
|
auto wddmAllocation = new WddmAllocation(cpuPtr, size, alignedPtr, alignedSize, nullptr, MemoryPool::System4KBPages);
|
|
|
|
wddmAllocation->allocationOffset = offsetInPage;
|
|
|
|
|
|
|
|
auto gmm = new Gmm(alignedPtr, alignedSize, false);
|
|
|
|
|
|
|
|
wddmAllocation->gmm = gmm;
|
|
|
|
|
|
|
|
if (!createWddmAllocation(wddmAllocation, AllocationOrigin::EXTERNAL_ALLOCATION)) {
|
|
|
|
delete gmm;
|
|
|
|
delete wddmAllocation;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return wddmAllocation;
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(size_t size, const void *ptrArg) {
|
|
|
|
void *ptr = const_cast<void *>(ptrArg);
|
|
|
|
|
|
|
|
if (ptr == nullptr) {
|
|
|
|
DEBUG_BREAK_IF(true);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-01-30 22:07:58 +08:00
|
|
|
|
|
|
|
if (mallocRestrictions.minAddress > reinterpret_cast<uintptr_t>(ptrArg)) {
|
|
|
|
void *reserve = nullptr;
|
|
|
|
void *ptrAligned = alignDown(ptr, MemoryConstants::allocationAlignment);
|
|
|
|
size_t sizeAligned = alignSizeWholePage(ptr, size);
|
|
|
|
size_t offset = ptrDiff(ptr, ptrAligned);
|
|
|
|
|
|
|
|
if (!wddm->reserveValidAddressRange(sizeAligned, reserve)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-14 00:50:55 +08:00
|
|
|
auto allocation = new WddmAllocation(ptr, size, ptrAligned, sizeAligned, reserve, MemoryPool::System4KBPages);
|
2018-01-30 22:07:58 +08:00
|
|
|
allocation->allocationOffset = offset;
|
|
|
|
|
2018-06-29 17:48:19 +08:00
|
|
|
Gmm *gmm = new Gmm(ptrAligned, sizeAligned, false);
|
2018-01-30 22:07:58 +08:00
|
|
|
allocation->gmm = gmm;
|
2018-05-11 23:24:00 +08:00
|
|
|
if (createWddmAllocation(allocation, AllocationOrigin::EXTERNAL_ALLOCATION)) {
|
2018-01-30 22:07:58 +08:00
|
|
|
return allocation;
|
|
|
|
}
|
|
|
|
freeGraphicsMemory(allocation);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
return MemoryManager::allocateGraphicsMemory(size, ptr);
|
|
|
|
}
|
|
|
|
|
2018-07-09 20:12:32 +08:00
|
|
|
GraphicsAllocation *WddmMemoryManager::allocate32BitGraphicsMemory(size_t size, const void *ptr, AllocationOrigin allocationOrigin) {
|
2017-12-21 07:45:38 +08:00
|
|
|
Gmm *gmm = nullptr;
|
|
|
|
const void *ptrAligned = nullptr;
|
|
|
|
size_t sizeAligned = size;
|
|
|
|
void *pSysMem = nullptr;
|
|
|
|
size_t offset = 0;
|
|
|
|
bool cpuPtrAllocated = false;
|
|
|
|
|
|
|
|
if (ptr) {
|
|
|
|
ptrAligned = alignDown(ptr, MemoryConstants::allocationAlignment);
|
|
|
|
sizeAligned = alignSizeWholePage(ptr, size);
|
|
|
|
offset = ptrDiff(ptr, ptrAligned);
|
|
|
|
} else {
|
|
|
|
sizeAligned = alignUp(size, MemoryConstants::allocationAlignment);
|
|
|
|
pSysMem = allocateSystemMemory(sizeAligned, MemoryConstants::allocationAlignment);
|
|
|
|
if (pSysMem == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
ptrAligned = pSysMem;
|
|
|
|
cpuPtrAllocated = true;
|
|
|
|
}
|
|
|
|
|
2018-07-14 00:50:55 +08:00
|
|
|
auto wddmAllocation = new WddmAllocation(const_cast<void *>(ptrAligned), sizeAligned, const_cast<void *>(ptrAligned), sizeAligned, nullptr, MemoryPool::System4KBPagesWith32BitGpuAddressing);
|
2018-03-01 17:08:20 +08:00
|
|
|
wddmAllocation->cpuPtrAllocated = cpuPtrAllocated;
|
|
|
|
wddmAllocation->is32BitAllocation = true;
|
|
|
|
wddmAllocation->allocationOffset = offset;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-06-29 17:48:19 +08:00
|
|
|
gmm = new Gmm(ptrAligned, sizeAligned, false);
|
2018-03-01 17:08:20 +08:00
|
|
|
wddmAllocation->gmm = gmm;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-05-11 23:24:00 +08:00
|
|
|
if (!createWddmAllocation(wddmAllocation, allocationOrigin)) {
|
2018-03-01 17:08:20 +08:00
|
|
|
delete gmm;
|
|
|
|
delete wddmAllocation;
|
|
|
|
freeSystemMemory(pSysMem);
|
|
|
|
return nullptr;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
2018-03-01 17:08:20 +08:00
|
|
|
wddmAllocation->is32BitAllocation = true;
|
2018-05-11 23:24:00 +08:00
|
|
|
auto baseAddress = allocationOrigin == AllocationOrigin::EXTERNAL_ALLOCATION ? allocator32Bit->getBase() : this->wddm->getGfxPartition().Heap32[1].Base;
|
2018-06-21 17:36:47 +08:00
|
|
|
wddmAllocation->gpuBaseAddress = GmmHelper::canonize(baseAddress);
|
2018-03-01 17:08:20 +08:00
|
|
|
|
|
|
|
return wddmAllocation;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsAllocation *WddmMemoryManager::createAllocationFromHandle(osHandle handle, bool requireSpecificBitness, bool ntHandle) {
|
2018-07-14 00:50:55 +08:00
|
|
|
auto allocation = new WddmAllocation(nullptr, 0, handle, MemoryPool::SystemCpuInaccessible);
|
2017-12-21 07:45:38 +08:00
|
|
|
bool is32BitAllocation = false;
|
|
|
|
|
2018-03-01 17:08:20 +08:00
|
|
|
bool status = ntHandle ? wddm->openNTHandle((HANDLE)((UINT_PTR)handle), allocation)
|
|
|
|
: wddm->openSharedHandle(handle, allocation);
|
|
|
|
|
|
|
|
if (!status) {
|
|
|
|
delete allocation;
|
|
|
|
return nullptr;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shared objects are passed without size
|
2018-03-01 17:08:20 +08:00
|
|
|
size_t size = allocation->gmm->gmmResourceInfo->getSizeAllocation();
|
|
|
|
allocation->setSize(size);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
void *ptr = nullptr;
|
|
|
|
if (is32bit) {
|
2018-01-30 22:07:58 +08:00
|
|
|
if (!wddm->reserveValidAddressRange(size, ptr)) {
|
2018-03-01 17:08:20 +08:00
|
|
|
delete allocation;
|
2018-01-30 22:07:58 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-03-01 17:08:20 +08:00
|
|
|
allocation->setReservedAddress(ptr);
|
2017-12-21 07:45:38 +08:00
|
|
|
} else if (requireSpecificBitness && this->force32bitAllocations) {
|
|
|
|
is32BitAllocation = true;
|
2018-03-01 17:08:20 +08:00
|
|
|
allocation->is32BitAllocation = true;
|
2018-06-21 17:36:47 +08:00
|
|
|
allocation->gpuBaseAddress = GmmHelper::canonize(allocator32Bit->getBase());
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
2018-07-12 21:42:46 +08:00
|
|
|
status = wddm->mapGpuVirtualAddress(allocation, ptr, is32BitAllocation, false, false);
|
2018-02-07 05:43:38 +08:00
|
|
|
DEBUG_BREAK_IF(!status);
|
2018-03-01 17:08:20 +08:00
|
|
|
allocation->setGpuAddress(allocation->gpuPtr);
|
|
|
|
return allocation;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
2018-08-29 20:50:36 +08:00
|
|
|
GraphicsAllocation *WddmMemoryManager::createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness) {
|
2017-12-21 07:45:38 +08:00
|
|
|
return createAllocationFromHandle(handle, requireSpecificBitness, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsAllocation *WddmMemoryManager::createGraphicsAllocationFromNTHandle(void *handle) {
|
|
|
|
return createAllocationFromHandle((osHandle)((UINT_PTR)handle), false, true);
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:00:23 +08:00
|
|
|
void WddmMemoryManager::addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) {
|
|
|
|
WddmAllocation *wddmMemory = static_cast<WddmAllocation *>(gfxAllocation);
|
|
|
|
FragmentStorage fragment = {};
|
|
|
|
fragment.driverAllocation = true;
|
|
|
|
fragment.fragmentCpuPointer = gfxAllocation->getUnderlyingBuffer();
|
|
|
|
fragment.fragmentSize = alignUp(gfxAllocation->getUnderlyingBufferSize(), MemoryConstants::pageSize);
|
|
|
|
|
|
|
|
fragment.osInternalStorage = new OsHandle();
|
|
|
|
fragment.osInternalStorage->gpuPtr = gfxAllocation->getGpuAddress();
|
2018-06-15 21:44:28 +08:00
|
|
|
fragment.osInternalStorage->handle = wddmMemory->handle;
|
2018-05-08 16:00:23 +08:00
|
|
|
fragment.osInternalStorage->gmm = gfxAllocation->gmm;
|
|
|
|
fragment.residency = &wddmMemory->getResidencyData();
|
|
|
|
hostPtrManager.storeFragment(fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WddmMemoryManager::removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) {
|
|
|
|
auto buffer = gfxAllocation->getUnderlyingBuffer();
|
|
|
|
auto fragment = hostPtrManager.getFragment(buffer);
|
|
|
|
if (fragment && fragment->driverAllocation) {
|
|
|
|
OsHandle *osStorageToRelease = fragment->osInternalStorage;
|
|
|
|
if (hostPtrManager.releaseHostPtr(buffer)) {
|
|
|
|
delete osStorageToRelease;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
void *WddmMemoryManager::lockResource(GraphicsAllocation *graphicsAllocation) {
|
|
|
|
return wddm->lockResource(static_cast<WddmAllocation *>(graphicsAllocation));
|
|
|
|
};
|
|
|
|
void WddmMemoryManager::unlockResource(GraphicsAllocation *graphicsAllocation) {
|
|
|
|
wddm->unlockResource(static_cast<WddmAllocation *>(graphicsAllocation));
|
|
|
|
};
|
|
|
|
|
|
|
|
void WddmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) {
|
|
|
|
WddmAllocation *input = static_cast<WddmAllocation *>(gfxAllocation);
|
2018-01-18 04:28:55 +08:00
|
|
|
DEBUG_BREAK_IF(!validateAllocation(input));
|
|
|
|
if (gfxAllocation == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-04 06:52:51 +08:00
|
|
|
|
|
|
|
for (const auto &residencyController : this->residencyControllers) {
|
|
|
|
if (residencyController) {
|
|
|
|
residencyController->acquireLock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
if (input->getTrimCandidateListPosition() != trimListUnusedPosition) {
|
|
|
|
removeFromTrimCandidateList(gfxAllocation, true);
|
|
|
|
}
|
|
|
|
|
2018-10-04 06:52:51 +08:00
|
|
|
for (const auto &residencyController : this->residencyControllers) {
|
|
|
|
if (residencyController) {
|
|
|
|
residencyController->releaseLock();
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-09-25 03:41:59 +08:00
|
|
|
UNRECOVERABLE_IF(DebugManager.flags.CreateMultipleDevices.get() == 0 &&
|
2018-10-04 14:50:20 +08:00
|
|
|
gfxAllocation->taskCount != ObjectNotUsed && this->executionEnvironment.commandStreamReceivers.size() > 0 &&
|
2018-10-01 22:10:54 +08:00
|
|
|
this->getCommandStreamReceiver(0) && this->getCommandStreamReceiver(0)->getTagAddress() &&
|
|
|
|
gfxAllocation->taskCount > *this->getCommandStreamReceiver(0)->getTagAddress());
|
2018-03-09 21:48:42 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
if (input->gmm) {
|
2018-10-03 01:10:29 +08:00
|
|
|
if (input->gmm->isRenderCompressed && wddm->getPageTableManager()) {
|
2018-01-25 22:10:07 +08:00
|
|
|
auto status = wddm->updateAuxTable(input->gpuPtr, input->gmm, false);
|
2017-12-21 07:45:38 +08:00
|
|
|
DEBUG_BREAK_IF(!status);
|
|
|
|
}
|
|
|
|
delete input->gmm;
|
|
|
|
}
|
2018-05-08 16:00:23 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
if (input->peekSharedHandle() == false &&
|
|
|
|
input->cpuPtrAllocated == false &&
|
|
|
|
input->fragmentsStorage.fragmentCount > 0) {
|
|
|
|
cleanGraphicsMemoryCreatedFromHostPtr(gfxAllocation);
|
|
|
|
} else {
|
|
|
|
D3DKMT_HANDLE *allocationHandles = nullptr;
|
|
|
|
uint32_t allocationCount = 0;
|
|
|
|
D3DKMT_HANDLE resourceHandle = 0;
|
|
|
|
void *cpuPtr = nullptr;
|
|
|
|
if (input->peekSharedHandle()) {
|
|
|
|
resourceHandle = input->resourceHandle;
|
|
|
|
} else {
|
|
|
|
allocationHandles = &input->handle;
|
|
|
|
allocationCount = 1;
|
|
|
|
if (input->cpuPtrAllocated) {
|
|
|
|
cpuPtr = input->getAlignedCpuPtr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (input->isLocked()) {
|
|
|
|
unlockResource(input);
|
|
|
|
input->setLocked(false);
|
|
|
|
}
|
2018-09-06 23:58:32 +08:00
|
|
|
auto status = tryDeferDeletions(allocationHandles, allocationCount, resourceHandle);
|
2017-12-21 07:45:38 +08:00
|
|
|
DEBUG_BREAK_IF(!status);
|
2018-01-22 23:43:26 +08:00
|
|
|
alignedFreeWrapper(cpuPtr);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
2018-01-30 22:07:58 +08:00
|
|
|
wddm->releaseReservedAddress(input->getReservedAddress());
|
2017-12-21 07:45:38 +08:00
|
|
|
delete gfxAllocation;
|
|
|
|
}
|
|
|
|
|
2018-09-06 23:58:32 +08:00
|
|
|
bool WddmMemoryManager::tryDeferDeletions(D3DKMT_HANDLE *handles, uint32_t allocationCount, D3DKMT_HANDLE resourceHandle) {
|
2017-12-21 07:45:38 +08:00
|
|
|
bool status = true;
|
|
|
|
if (deferredDeleter) {
|
2018-09-06 23:58:32 +08:00
|
|
|
deferredDeleter->deferDeletion(DeferrableDeletion::create(wddm, handles, allocationCount, resourceHandle));
|
2017-12-21 07:45:38 +08:00
|
|
|
} else {
|
2018-09-06 23:58:32 +08:00
|
|
|
status = wddm->destroyAllocations(handles, allocationCount, resourceHandle);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WddmMemoryManager::validateAllocation(WddmAllocation *alloc) {
|
|
|
|
if (alloc == nullptr)
|
|
|
|
return false;
|
|
|
|
auto size = alloc->getUnderlyingBufferSize();
|
|
|
|
if (alloc->getGpuAddress() == 0u || size == 0 || (alloc->handle == 0 && alloc->fragmentsStorage.fragmentCount == 0))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-28 19:09:48 +08:00
|
|
|
MemoryManager::AllocationStatus WddmMemoryManager::populateOsHandles(OsHandleStorage &handleStorage) {
|
2018-03-27 20:01:04 +08:00
|
|
|
uint32_t allocatedFragmentIndexes[max_fragments_count];
|
|
|
|
uint32_t allocatedFragmentsCounter = 0;
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
for (unsigned int i = 0; i < max_fragments_count; i++) {
|
|
|
|
// If no fragment is present it means it already exists.
|
|
|
|
if (!handleStorage.fragmentStorageData[i].osHandleStorage && handleStorage.fragmentStorageData[i].cpuPtr) {
|
|
|
|
handleStorage.fragmentStorageData[i].osHandleStorage = new OsHandle();
|
|
|
|
handleStorage.fragmentStorageData[i].residency = new ResidencyData();
|
|
|
|
|
2018-06-29 17:48:19 +08:00
|
|
|
handleStorage.fragmentStorageData[i].osHandleStorage->gmm = new Gmm(handleStorage.fragmentStorageData[i].cpuPtr, handleStorage.fragmentStorageData[i].fragmentSize, false);
|
2018-03-27 20:01:04 +08:00
|
|
|
allocatedFragmentIndexes[allocatedFragmentsCounter] = i;
|
|
|
|
allocatedFragmentsCounter++;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
}
|
2018-03-23 17:07:31 +08:00
|
|
|
NTSTATUS result = wddm->createAllocationsAndMapGpuVa(handleStorage);
|
|
|
|
|
|
|
|
if (result == STATUS_GRAPHICS_NO_VIDEO_MEMORY) {
|
|
|
|
return AllocationStatus::InvalidHostPointer;
|
|
|
|
}
|
2018-03-27 20:01:04 +08:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < allocatedFragmentsCounter; i++) {
|
|
|
|
hostPtrManager.storeFragment(handleStorage.fragmentStorageData[allocatedFragmentIndexes[i]]);
|
|
|
|
}
|
|
|
|
|
2018-02-28 19:09:48 +08:00
|
|
|
return AllocationStatus::Success;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WddmMemoryManager::cleanOsHandles(OsHandleStorage &handleStorage) {
|
|
|
|
|
|
|
|
D3DKMT_HANDLE handles[max_fragments_count] = {0};
|
|
|
|
auto allocationCount = 0;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < max_fragments_count; i++) {
|
|
|
|
if (handleStorage.fragmentStorageData[i].freeTheFragment) {
|
|
|
|
handles[allocationCount] = handleStorage.fragmentStorageData[i].osHandleStorage->handle;
|
|
|
|
handleStorage.fragmentStorageData[i].residency->resident = false;
|
|
|
|
allocationCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 23:58:32 +08:00
|
|
|
bool success = tryDeferDeletions(handles, allocationCount, 0);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < max_fragments_count; i++) {
|
|
|
|
if (handleStorage.fragmentStorageData[i].freeTheFragment) {
|
|
|
|
if (success) {
|
|
|
|
handleStorage.fragmentStorageData[i].osHandleStorage->handle = 0;
|
|
|
|
}
|
|
|
|
delete handleStorage.fragmentStorageData[i].osHandleStorage->gmm;
|
|
|
|
delete handleStorage.fragmentStorageData[i].osHandleStorage;
|
|
|
|
delete handleStorage.fragmentStorageData[i].residency;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 19:19:20 +08:00
|
|
|
void WddmMemoryManager::registerOsContext(OsContext *contextToRegister) {
|
|
|
|
MemoryManager::registerOsContext(contextToRegister);
|
2018-10-04 06:52:51 +08:00
|
|
|
this->residencyControllers.resize(this->getOsContextCount());
|
|
|
|
this->residencyControllers[contextToRegister->getContextId()] = std::make_unique<WddmResidencyController>();
|
2018-09-26 19:19:20 +08:00
|
|
|
}
|
|
|
|
|
2018-06-21 17:42:03 +08:00
|
|
|
void WddmMemoryManager::obtainGpuAddresFromFragments(WddmAllocation *allocation, OsHandleStorage &handleStorage) {
|
|
|
|
if (this->force32bitAllocations && (handleStorage.fragmentCount > 0)) {
|
|
|
|
auto hostPtr = allocation->getUnderlyingBuffer();
|
|
|
|
auto fragment = hostPtrManager.getFragment(hostPtr);
|
|
|
|
if (fragment && fragment->driverAllocation) {
|
|
|
|
auto gpuPtr = handleStorage.fragmentStorageData[0].osHandleStorage->gpuPtr;
|
|
|
|
for (uint32_t i = 1; i < handleStorage.fragmentCount; i++) {
|
|
|
|
if (handleStorage.fragmentStorageData[i].osHandleStorage->gpuPtr < gpuPtr) {
|
|
|
|
gpuPtr = handleStorage.fragmentStorageData[i].osHandleStorage->gpuPtr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allocation->allocationOffset = reinterpret_cast<uint64_t>(hostPtr) & MemoryConstants::pageMask;
|
|
|
|
allocation->setGpuAddress(gpuPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
GraphicsAllocation *WddmMemoryManager::createGraphicsAllocation(OsHandleStorage &handleStorage, size_t hostPtrSize, const void *hostPtr) {
|
2018-07-14 00:50:55 +08:00
|
|
|
auto allocation = new WddmAllocation(const_cast<void *>(hostPtr), hostPtrSize, const_cast<void *>(hostPtr), hostPtrSize, nullptr, MemoryPool::System4KBPages);
|
2017-12-21 07:45:38 +08:00
|
|
|
allocation->fragmentsStorage = handleStorage;
|
2018-06-21 17:42:03 +08:00
|
|
|
obtainGpuAddresFromFragments(allocation, handleStorage);
|
2017-12-21 07:45:38 +08:00
|
|
|
return allocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t WddmMemoryManager::getSystemSharedMemory() {
|
|
|
|
return wddm->getSystemSharedMemory();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t WddmMemoryManager::getMaxApplicationAddress() {
|
|
|
|
return wddm->getMaxApplicationAddress();
|
|
|
|
}
|
|
|
|
|
2018-03-12 22:24:46 +08:00
|
|
|
uint64_t WddmMemoryManager::getInternalHeapBaseAddress() {
|
|
|
|
return this->wddm->getGfxPartition().Heap32[1].Base;
|
|
|
|
}
|
|
|
|
|
2018-09-25 17:34:16 +08:00
|
|
|
bool WddmMemoryManager::makeResidentResidencyAllocations(ResidencyContainer &allocationsForResidency, OsContext &osContext) {
|
|
|
|
size_t residencyCount = allocationsForResidency.size();
|
2017-12-21 07:45:38 +08:00
|
|
|
std::unique_ptr<D3DKMT_HANDLE[]> handlesForResidency(new D3DKMT_HANDLE[residencyCount * max_fragments_count]);
|
|
|
|
|
|
|
|
uint32_t totalHandlesCount = 0;
|
|
|
|
|
2018-10-04 06:52:51 +08:00
|
|
|
this->residencyControllers[osContext.getContextId()]->acquireLock();
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-08-27 21:48:29 +08:00
|
|
|
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "currentFenceValue =", osContext.get()->getMonitoredFence().currentFenceValue);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < residencyCount; i++) {
|
2018-09-25 17:34:16 +08:00
|
|
|
WddmAllocation *allocation = reinterpret_cast<WddmAllocation *>(allocationsForResidency[i]);
|
2017-12-21 07:45:38 +08:00
|
|
|
bool mainResidency = false;
|
|
|
|
bool fragmentResidency[3] = {false, false, false};
|
|
|
|
|
|
|
|
mainResidency = allocation->getResidencyData().resident;
|
|
|
|
|
|
|
|
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "allocation =", allocation, mainResidency ? "resident" : "not resident");
|
|
|
|
|
|
|
|
if (allocation->getTrimCandidateListPosition() != trimListUnusedPosition) {
|
|
|
|
|
|
|
|
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "allocation =", allocation, "on trimCandidateList");
|
|
|
|
removeFromTrimCandidateList(allocation);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
for (uint32_t allocationId = 0; allocationId < allocation->fragmentsStorage.fragmentCount; allocationId++) {
|
|
|
|
fragmentResidency[allocationId] = allocation->fragmentsStorage.fragmentStorageData[allocationId].residency->resident;
|
|
|
|
|
|
|
|
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "fragment handle =",
|
|
|
|
allocation->fragmentsStorage.fragmentStorageData[allocationId].osHandleStorage->handle,
|
|
|
|
fragmentResidency[allocationId] ? "resident" : "not resident");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allocation->fragmentsStorage.fragmentCount == 0) {
|
|
|
|
if (!mainResidency)
|
|
|
|
handlesForResidency[totalHandlesCount++] = allocation->handle;
|
|
|
|
} else {
|
|
|
|
for (uint32_t allocationId = 0; allocationId < allocation->fragmentsStorage.fragmentCount; allocationId++) {
|
|
|
|
if (!fragmentResidency[allocationId])
|
|
|
|
handlesForResidency[totalHandlesCount++] = allocation->fragmentsStorage.fragmentStorageData[allocationId].osHandleStorage->handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool result = true;
|
|
|
|
if (totalHandlesCount) {
|
|
|
|
uint64_t bytesToTrim = 0;
|
|
|
|
while ((result = wddm->makeResident(handlesForResidency.get(), totalHandlesCount, false, &bytesToTrim)) == false) {
|
|
|
|
this->memoryBudgetExhausted = true;
|
|
|
|
bool trimmingDone = trimResidencyToBudget(bytesToTrim);
|
|
|
|
bool cantTrimFurther = !trimmingDone;
|
|
|
|
if (cantTrimFurther) {
|
|
|
|
result = wddm->makeResident(handlesForResidency.get(), totalHandlesCount, true, &bytesToTrim);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == true) {
|
|
|
|
for (uint32_t i = 0; i < residencyCount; i++) {
|
2018-09-25 17:34:16 +08:00
|
|
|
WddmAllocation *allocation = reinterpret_cast<WddmAllocation *>(allocationsForResidency[i]);
|
2017-12-21 07:45:38 +08:00
|
|
|
// Update fence value not to early destroy / evict allocation
|
2018-09-07 13:18:38 +08:00
|
|
|
auto currentFence = osContext.get()->getMonitoredFence().currentFenceValue;
|
|
|
|
allocation->getResidencyData().updateCompletionData(currentFence, &osContext);
|
2017-12-21 07:45:38 +08:00
|
|
|
allocation->getResidencyData().resident = true;
|
|
|
|
|
|
|
|
for (uint32_t allocationId = 0; allocationId < allocation->fragmentsStorage.fragmentCount; allocationId++) {
|
2018-08-27 21:48:29 +08:00
|
|
|
auto residencyData = allocation->fragmentsStorage.fragmentStorageData[allocationId].residency;
|
2017-12-21 07:45:38 +08:00
|
|
|
// Update fence value not to remove the fragment referenced by different GA in trimming callback
|
2018-09-07 13:18:38 +08:00
|
|
|
residencyData->updateCompletionData(currentFence, &osContext);
|
|
|
|
residencyData->resident = true;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 06:52:51 +08:00
|
|
|
this->residencyControllers[osContext.getContextId()]->releaseLock();
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-10-03 23:48:59 +08:00
|
|
|
void WddmMemoryManager::makeNonResidentEvictionAllocations(ResidencyContainer &evictionAllocations, OsContext &osContext) {
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-10-04 06:52:51 +08:00
|
|
|
this->residencyControllers[osContext.getContextId()]->acquireLock();
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
size_t residencyCount = evictionAllocations.size();
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < residencyCount; i++) {
|
|
|
|
WddmAllocation *allocation = reinterpret_cast<WddmAllocation *>(evictionAllocations[i]);
|
|
|
|
|
|
|
|
addToTrimCandidateList(allocation);
|
|
|
|
}
|
|
|
|
|
2018-10-04 06:52:51 +08:00
|
|
|
this->residencyControllers[osContext.getContextId()]->releaseLock();
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WddmMemoryManager::removeFromTrimCandidateList(GraphicsAllocation *allocation, bool compactList) {
|
|
|
|
WddmAllocation *wddmAllocation = (WddmAllocation *)allocation;
|
|
|
|
size_t position = wddmAllocation->getTrimCandidateListPosition();
|
|
|
|
|
|
|
|
DEBUG_BREAK_IF(!(trimCandidatesCount > (trimCandidatesCount - 1)));
|
|
|
|
DEBUG_BREAK_IF(trimCandidatesCount > trimCandidateList.size());
|
|
|
|
|
|
|
|
trimCandidatesCount--;
|
|
|
|
|
|
|
|
trimCandidateList[position] = nullptr;
|
|
|
|
|
|
|
|
checkTrimCandidateCount();
|
|
|
|
|
|
|
|
if (position == trimCandidateList.size() - 1) {
|
|
|
|
size_t erasePosition = position;
|
|
|
|
|
|
|
|
if (position == 0) {
|
|
|
|
trimCandidateList.resize(0);
|
|
|
|
} else {
|
|
|
|
while (trimCandidateList[erasePosition] == nullptr && erasePosition > 0) {
|
|
|
|
erasePosition--;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t sizeRemaining = erasePosition + 1;
|
|
|
|
if (erasePosition == 0 && trimCandidateList[erasePosition] == nullptr) {
|
|
|
|
sizeRemaining = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
trimCandidateList.resize(sizeRemaining);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wddmAllocation->setTrimCandidateListPosition(trimListUnusedPosition);
|
|
|
|
|
|
|
|
if (compactList && checkTrimCandidateListCompaction()) {
|
|
|
|
compactTrimCandidateList();
|
|
|
|
}
|
|
|
|
|
|
|
|
checkTrimCandidateCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WddmMemoryManager::addToTrimCandidateList(GraphicsAllocation *allocation) {
|
|
|
|
WddmAllocation *wddmAllocation = (WddmAllocation *)allocation;
|
|
|
|
size_t position = trimCandidateList.size();
|
|
|
|
|
|
|
|
DEBUG_BREAK_IF(trimCandidatesCount > trimCandidateList.size());
|
|
|
|
|
|
|
|
if (wddmAllocation->getTrimCandidateListPosition() == trimListUnusedPosition) {
|
|
|
|
trimCandidatesCount++;
|
|
|
|
trimCandidateList.push_back(allocation);
|
|
|
|
wddmAllocation->setTrimCandidateListPosition(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
checkTrimCandidateCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WddmMemoryManager::compactTrimCandidateList() {
|
|
|
|
|
|
|
|
size_t size = trimCandidateList.size();
|
|
|
|
size_t freePosition = 0;
|
|
|
|
|
|
|
|
if (size == 0 || size == trimCandidatesCount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_BREAK_IF(!(trimCandidateList[size - 1] != nullptr));
|
|
|
|
|
|
|
|
uint32_t previousCount = trimCandidatesCount;
|
|
|
|
DEBUG_BREAK_IF(trimCandidatesCount > trimCandidateList.size());
|
|
|
|
|
|
|
|
while (freePosition < trimCandidatesCount && trimCandidateList[freePosition] != nullptr)
|
|
|
|
freePosition++;
|
|
|
|
|
|
|
|
for (uint32_t i = 1; i < size; i++) {
|
|
|
|
|
|
|
|
if (trimCandidateList[i] != nullptr && freePosition < i) {
|
|
|
|
trimCandidateList[freePosition] = trimCandidateList[i];
|
|
|
|
trimCandidateList[i] = nullptr;
|
|
|
|
((WddmAllocation *)trimCandidateList[freePosition])->setTrimCandidateListPosition(freePosition);
|
|
|
|
freePosition++;
|
|
|
|
|
|
|
|
// Last element was moved, erase elements from freePosition
|
|
|
|
if (i == size - 1) {
|
|
|
|
trimCandidateList.resize(freePosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG_BREAK_IF(trimCandidatesCount > trimCandidateList.size());
|
|
|
|
DEBUG_BREAK_IF(trimCandidatesCount != previousCount);
|
|
|
|
|
|
|
|
checkTrimCandidateCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WddmMemoryManager::trimResidency(D3DDDI_TRIMRESIDENCYSET_FLAGS flags, uint64_t bytes) {
|
2018-10-04 06:52:51 +08:00
|
|
|
OsContext &osContext = *getRegisteredOsContext(0);
|
2017-12-21 07:45:38 +08:00
|
|
|
if (flags.PeriodicTrim) {
|
|
|
|
bool periodicTrimDone = false;
|
|
|
|
D3DKMT_HANDLE fragmentEvictHandles[3] = {0};
|
|
|
|
uint64_t sizeToTrim = 0;
|
|
|
|
|
2018-10-04 06:52:51 +08:00
|
|
|
this->residencyControllers[osContext.getContextId()]->acquireLock();
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
WddmAllocation *wddmAllocation = nullptr;
|
|
|
|
while ((wddmAllocation = getTrimCandidateHead()) != nullptr) {
|
|
|
|
|
2018-10-04 06:52:51 +08:00
|
|
|
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "lastPeriodicTrimFenceValue = ", this->residencyControllers[osContext.getContextId()]->getLastTrimFenceValue());
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
// allocation was not used from last periodic trim
|
2018-10-04 06:52:51 +08:00
|
|
|
if (wddmAllocation->getResidencyData().getFenceValueForContextId(0) <= this->residencyControllers[osContext.getContextId()]->getLastTrimFenceValue()) {
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-09-07 13:18:38 +08:00
|
|
|
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "allocation: handle =", wddmAllocation->handle, "lastFence =", (wddmAllocation)->getResidencyData().getFenceValueForContextId(0));
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
uint32_t fragmentsToEvict = 0;
|
|
|
|
|
|
|
|
if (wddmAllocation->fragmentsStorage.fragmentCount == 0) {
|
2018-09-07 13:18:38 +08:00
|
|
|
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "Evict allocation: handle =", wddmAllocation->handle, "lastFence =", (wddmAllocation)->getResidencyData().getFenceValueForContextId(0));
|
2017-12-21 07:45:38 +08:00
|
|
|
wddm->evict(&wddmAllocation->handle, 1, sizeToTrim);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t allocationId = 0; allocationId < wddmAllocation->fragmentsStorage.fragmentCount; allocationId++) {
|
2018-10-04 06:52:51 +08:00
|
|
|
if (wddmAllocation->fragmentsStorage.fragmentStorageData[allocationId].residency->getFenceValueForContextId(0) <= this->residencyControllers[osContext.getContextId()]->getLastTrimFenceValue()) {
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-09-07 13:18:38 +08:00
|
|
|
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "Evict fragment: handle =", wddmAllocation->fragmentsStorage.fragmentStorageData[allocationId].osHandleStorage->handle, "lastFence =", wddmAllocation->fragmentsStorage.fragmentStorageData[allocationId].residency->getFenceValueForContextId(0));
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
fragmentEvictHandles[fragmentsToEvict++] = wddmAllocation->fragmentsStorage.fragmentStorageData[allocationId].osHandleStorage->handle;
|
|
|
|
wddmAllocation->fragmentsStorage.fragmentStorageData[allocationId].residency->resident = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fragmentsToEvict != 0) {
|
|
|
|
wddm->evict((D3DKMT_HANDLE *)fragmentEvictHandles, fragmentsToEvict, sizeToTrim);
|
|
|
|
}
|
|
|
|
|
|
|
|
wddmAllocation->getResidencyData().resident = false;
|
|
|
|
removeFromTrimCandidateList(wddmAllocation);
|
|
|
|
} else {
|
|
|
|
periodicTrimDone = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (checkTrimCandidateListCompaction()) {
|
|
|
|
compactTrimCandidateList();
|
|
|
|
}
|
|
|
|
|
2018-10-04 06:52:51 +08:00
|
|
|
this->residencyControllers[osContext.getContextId()]->releaseLock();
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags.TrimToBudget) {
|
|
|
|
|
2018-10-04 06:52:51 +08:00
|
|
|
this->residencyControllers[osContext.getContextId()]->acquireLock();
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
trimResidencyToBudget(bytes);
|
|
|
|
|
2018-10-04 06:52:51 +08:00
|
|
|
this->residencyControllers[osContext.getContextId()]->releaseLock();
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags.PeriodicTrim || flags.RestartPeriodicTrim) {
|
2018-10-04 06:52:51 +08:00
|
|
|
const auto newPeriodicTrimFenceValue = *osContext.get()->getMonitoredFence().cpuAddress;
|
|
|
|
this->residencyControllers[osContext.getContextId()]->setLastTrimFenceValue(newPeriodicTrimFenceValue);
|
|
|
|
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "updated lastPeriodicTrimFenceValue =", newPeriodicTrimFenceValue);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WddmMemoryManager::checkTrimCandidateCount() {
|
|
|
|
if (DebugManager.flags.ResidencyDebugEnable.get()) {
|
|
|
|
uint32_t sum = 0;
|
|
|
|
for (size_t i = 0; i < trimCandidateList.size(); i++) {
|
|
|
|
if (trimCandidateList[i] != nullptr) {
|
|
|
|
sum++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG_BREAK_IF(sum != trimCandidatesCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WddmMemoryManager::checkTrimCandidateListCompaction() {
|
|
|
|
if (2 * trimCandidatesCount <= trimCandidateList.size()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WddmMemoryManager::trimResidencyToBudget(uint64_t bytes) {
|
|
|
|
bool trimToBudgetDone = false;
|
|
|
|
D3DKMT_HANDLE fragmentEvictHandles[3] = {0};
|
|
|
|
uint64_t numberOfBytesToTrim = bytes;
|
|
|
|
WddmAllocation *wddmAllocation = nullptr;
|
|
|
|
|
|
|
|
trimToBudgetDone = (numberOfBytesToTrim == 0);
|
|
|
|
|
|
|
|
while (!trimToBudgetDone) {
|
|
|
|
uint64_t lastFence = 0;
|
|
|
|
wddmAllocation = getTrimCandidateHead();
|
|
|
|
|
|
|
|
if (wddmAllocation == nullptr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-09-07 13:18:38 +08:00
|
|
|
lastFence = wddmAllocation->getResidencyData().getFenceValueForContextId(0);
|
|
|
|
auto osContext = wddmAllocation->getResidencyData().getOsContextFromId(0);
|
2018-08-27 21:48:29 +08:00
|
|
|
if (!osContext) {
|
|
|
|
removeFromTrimCandidateList(wddmAllocation);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto &monitoredFence = osContext->get()->getMonitoredFence();
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-08-27 21:48:29 +08:00
|
|
|
if (lastFence <= monitoredFence.lastSubmittedFence) {
|
2017-12-21 07:45:38 +08:00
|
|
|
uint32_t fragmentsToEvict = 0;
|
|
|
|
uint64_t sizeEvicted = 0;
|
|
|
|
uint64_t sizeToTrim = 0;
|
|
|
|
|
2018-08-27 21:48:29 +08:00
|
|
|
if (lastFence > *monitoredFence.cpuAddress) {
|
|
|
|
wddm->waitFromCpu(lastFence, *osContext->get());
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (wddmAllocation->fragmentsStorage.fragmentCount == 0) {
|
|
|
|
wddm->evict(&wddmAllocation->handle, 1, sizeToTrim);
|
|
|
|
|
2018-02-02 00:16:36 +08:00
|
|
|
sizeEvicted = wddmAllocation->getAlignedSize();
|
2017-12-21 07:45:38 +08:00
|
|
|
} else {
|
2018-08-27 21:48:29 +08:00
|
|
|
auto &fragmentStorageData = wddmAllocation->fragmentsStorage.fragmentStorageData;
|
2017-12-21 07:45:38 +08:00
|
|
|
for (uint32_t allocationId = 0; allocationId < wddmAllocation->fragmentsStorage.fragmentCount; allocationId++) {
|
2018-09-07 13:18:38 +08:00
|
|
|
if (fragmentStorageData[allocationId].residency->getFenceValueForContextId(0) <= monitoredFence.lastSubmittedFence) {
|
2018-08-27 21:48:29 +08:00
|
|
|
fragmentEvictHandles[fragmentsToEvict++] = fragmentStorageData[allocationId].osHandleStorage->handle;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fragmentsToEvict != 0) {
|
|
|
|
wddm->evict((D3DKMT_HANDLE *)fragmentEvictHandles, fragmentsToEvict, sizeToTrim);
|
|
|
|
|
|
|
|
for (uint32_t allocationId = 0; allocationId < wddmAllocation->fragmentsStorage.fragmentCount; allocationId++) {
|
2018-09-07 13:18:38 +08:00
|
|
|
if (fragmentStorageData[allocationId].residency->getFenceValueForContextId(0) <= monitoredFence.lastSubmittedFence) {
|
2018-08-27 21:48:29 +08:00
|
|
|
fragmentStorageData[allocationId].residency->resident = false;
|
|
|
|
sizeEvicted += fragmentStorageData[allocationId].fragmentSize;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sizeEvicted >= numberOfBytesToTrim) {
|
|
|
|
numberOfBytesToTrim = 0;
|
|
|
|
} else {
|
|
|
|
numberOfBytesToTrim -= sizeEvicted;
|
|
|
|
}
|
|
|
|
|
|
|
|
wddmAllocation->getResidencyData().resident = false;
|
|
|
|
removeFromTrimCandidateList(wddmAllocation);
|
|
|
|
trimToBudgetDone = (numberOfBytesToTrim == 0);
|
|
|
|
} else {
|
|
|
|
trimToBudgetDone = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes > numberOfBytesToTrim && checkTrimCandidateListCompaction()) {
|
|
|
|
compactTrimCandidateList();
|
|
|
|
}
|
|
|
|
|
|
|
|
return numberOfBytesToTrim == 0;
|
|
|
|
}
|
|
|
|
|
2018-01-25 22:10:07 +08:00
|
|
|
bool WddmMemoryManager::mapAuxGpuVA(GraphicsAllocation *graphicsAllocation) {
|
|
|
|
return wddm->updateAuxTable(graphicsAllocation->getGpuAddress(), graphicsAllocation->gmm, true);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
2018-01-22 23:43:26 +08:00
|
|
|
|
|
|
|
AlignedMallocRestrictions *WddmMemoryManager::getAlignedMallocRestrictions() {
|
|
|
|
return &mallocRestrictions;
|
|
|
|
}
|
|
|
|
|
2018-05-11 23:24:00 +08:00
|
|
|
bool WddmMemoryManager::createWddmAllocation(WddmAllocation *allocation, AllocationOrigin allocationOrigin) {
|
|
|
|
bool useHeap1 = (allocationOrigin == AllocationOrigin::INTERNAL_ALLOCATION);
|
2018-02-07 05:43:38 +08:00
|
|
|
auto wddmSuccess = wddm->createAllocation(allocation);
|
|
|
|
if (wddmSuccess == STATUS_GRAPHICS_NO_VIDEO_MEMORY && deferredDeleter) {
|
|
|
|
deferredDeleter->drain(true);
|
|
|
|
wddmSuccess = wddm->createAllocation(allocation);
|
|
|
|
}
|
|
|
|
if (wddmSuccess == STATUS_SUCCESS) {
|
2018-07-12 21:42:46 +08:00
|
|
|
bool mapSuccess = wddm->mapGpuVirtualAddress(allocation, allocation->getAlignedCpuPtr(), allocation->is32BitAllocation, false, useHeap1);
|
2018-02-07 05:43:38 +08:00
|
|
|
if (!mapSuccess && deferredDeleter) {
|
|
|
|
deferredDeleter->drain(true);
|
2018-07-12 21:42:46 +08:00
|
|
|
mapSuccess = wddm->mapGpuVirtualAddress(allocation, allocation->getAlignedCpuPtr(), allocation->is32BitAllocation, false, useHeap1);
|
2018-02-07 05:43:38 +08:00
|
|
|
}
|
|
|
|
if (!mapSuccess) {
|
2018-09-06 23:58:32 +08:00
|
|
|
wddm->destroyAllocations(&allocation->handle, 1, allocation->resourceHandle);
|
2018-02-07 05:43:38 +08:00
|
|
|
wddmSuccess = STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
allocation->setGpuAddress(allocation->gpuPtr);
|
|
|
|
}
|
|
|
|
return (wddmSuccess == STATUS_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
} // namespace OCLRT
|