2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2021-05-17 02:51:16 +08:00
|
|
|
* Copyright (C) 2018-2021 Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
2018-09-17 20:03:37 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/memory_manager/memory_manager.h"
|
|
|
|
|
|
|
|
#include "shared/source/command_stream/command_stream_receiver.h"
|
|
|
|
#include "shared/source/debug_settings/debug_settings_manager.h"
|
|
|
|
#include "shared/source/execution_environment/root_device_environment.h"
|
|
|
|
#include "shared/source/gmm_helper/gmm.h"
|
|
|
|
#include "shared/source/gmm_helper/gmm_helper.h"
|
|
|
|
#include "shared/source/gmm_helper/page_table_mngr.h"
|
|
|
|
#include "shared/source/gmm_helper/resource_info.h"
|
|
|
|
#include "shared/source/helpers/aligned_memory.h"
|
2021-01-27 21:31:29 +08:00
|
|
|
#include "shared/source/helpers/api_specific_config.h"
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/helpers/basic_math.h"
|
2020-08-27 14:55:09 +08:00
|
|
|
#include "shared/source/helpers/heap_assigner.h"
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/helpers/hw_helper.h"
|
|
|
|
#include "shared/source/helpers/hw_info.h"
|
|
|
|
#include "shared/source/helpers/string.h"
|
|
|
|
#include "shared/source/helpers/surface_format_info.h"
|
2020-09-11 21:44:09 +08:00
|
|
|
#include "shared/source/memory_manager/compression_selector.h"
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/memory_manager/deferrable_allocation_deletion.h"
|
|
|
|
#include "shared/source/memory_manager/deferred_deleter.h"
|
|
|
|
#include "shared/source/memory_manager/host_ptr_manager.h"
|
|
|
|
#include "shared/source/memory_manager/internal_allocation_storage.h"
|
2021-09-21 21:34:25 +08:00
|
|
|
#include "shared/source/os_interface/hw_info_config.h"
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/os_interface/os_context.h"
|
|
|
|
#include "shared/source/os_interface/os_interface.h"
|
|
|
|
#include "shared/source/utilities/compiler_support.h"
|
|
|
|
#include "shared/source/utilities/stackvec.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2019-12-17 15:11:16 +08:00
|
|
|
uint32_t MemoryManager::maxOsContextCount = 0u;
|
|
|
|
|
2019-04-15 21:20:51 +08:00
|
|
|
MemoryManager::MemoryManager(ExecutionEnvironment &executionEnvironment) : executionEnvironment(executionEnvironment), hostPtrManager(std::make_unique<HostPtrManager>()),
|
2019-07-04 18:40:58 +08:00
|
|
|
multiContextResourceDestructor(std::make_unique<DeferredDeleter>()) {
|
2020-02-18 20:29:30 +08:00
|
|
|
|
2020-02-12 15:29:26 +08:00
|
|
|
bool anyLocalMemorySupported = false;
|
2021-06-21 18:45:19 +08:00
|
|
|
const auto rootEnvCount = executionEnvironment.rootDeviceEnvironments.size();
|
2020-02-12 15:29:26 +08:00
|
|
|
|
2021-06-21 18:45:19 +08:00
|
|
|
defaultEngineIndex.resize(rootEnvCount);
|
|
|
|
checkIsaPlacementOnceFlags = std::make_unique<std::once_flag[]>(rootEnvCount);
|
|
|
|
isaInLocalMemory.resize(rootEnvCount);
|
|
|
|
|
|
|
|
for (uint32_t rootDeviceIndex = 0; rootDeviceIndex < rootEnvCount; ++rootDeviceIndex) {
|
2020-02-18 20:29:30 +08:00
|
|
|
auto hwInfo = executionEnvironment.rootDeviceEnvironments[rootDeviceIndex]->getHardwareInfo();
|
2021-07-22 14:36:45 +08:00
|
|
|
internalLocalMemoryUsageBankSelector.emplace_back(new LocalMemoryUsageBankSelector(HwHelper::getSubDevicesCount(hwInfo)));
|
|
|
|
externalLocalMemoryUsageBankSelector.emplace_back(new LocalMemoryUsageBankSelector(HwHelper::getSubDevicesCount(hwInfo)));
|
2020-02-12 15:29:26 +08:00
|
|
|
this->localMemorySupported.push_back(HwHelper::get(hwInfo->platform.eRenderCoreFamily).getEnableLocalMemory(*hwInfo));
|
2021-02-19 19:45:53 +08:00
|
|
|
this->enable64kbpages.push_back(OSInterface::osEnabled64kbPages && hwInfo->capabilityTable.ftr64KBpages && !!DebugManager.flags.Enable64kbpages.get());
|
2020-02-12 15:29:26 +08:00
|
|
|
|
2020-08-12 18:18:34 +08:00
|
|
|
gfxPartitions.push_back(std::make_unique<GfxPartition>(reservedCpuAddressRange));
|
2020-02-12 15:29:26 +08:00
|
|
|
|
|
|
|
anyLocalMemorySupported |= this->localMemorySupported[rootDeviceIndex];
|
2021-05-25 02:57:32 +08:00
|
|
|
isLocalMemoryUsedForIsa(rootDeviceIndex);
|
2019-10-22 16:26:23 +08:00
|
|
|
}
|
|
|
|
|
2020-02-12 15:29:26 +08:00
|
|
|
if (anyLocalMemorySupported) {
|
2019-07-04 18:17:42 +08:00
|
|
|
pageFaultManager = PageFaultManager::create();
|
|
|
|
}
|
2020-06-06 19:13:22 +08:00
|
|
|
|
|
|
|
if (DebugManager.flags.EnableMultiStorageResources.get() != -1) {
|
|
|
|
supportsMultiStorageResources = !!DebugManager.flags.EnableMultiStorageResources.get();
|
|
|
|
}
|
2019-03-15 17:22:35 +08:00
|
|
|
}
|
2018-09-06 16:53:35 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
MemoryManager::~MemoryManager() {
|
2019-02-18 20:59:16 +08:00
|
|
|
for (auto &engine : registeredEngines) {
|
|
|
|
engine.osContext->decRefInternal();
|
2018-09-06 15:01:13 +08:00
|
|
|
}
|
2019-08-27 21:26:30 +08:00
|
|
|
if (reservedMemory) {
|
2019-08-29 15:52:06 +08:00
|
|
|
MemoryManager::alignedFreeWrapper(reservedMemory);
|
2019-08-27 21:26:30 +08:00
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *MemoryManager::allocateSystemMemory(size_t size, size_t alignment) {
|
|
|
|
// Establish a minimum alignment of 16bytes.
|
2018-01-22 23:43:26 +08:00
|
|
|
constexpr size_t minAlignment = 16;
|
|
|
|
alignment = std::max(alignment, minAlignment);
|
|
|
|
auto restrictions = getAlignedMallocRestrictions();
|
2019-05-19 04:44:46 +08:00
|
|
|
void *ptr = alignedMallocWrapper(size, alignment);
|
2018-01-22 23:43:26 +08:00
|
|
|
|
2019-05-19 04:44:46 +08:00
|
|
|
if (restrictions == nullptr || restrictions->minAddress == 0) {
|
2018-01-22 23:43:26 +08:00
|
|
|
return ptr;
|
2019-05-19 04:44:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (restrictions->minAddress > reinterpret_cast<uintptr_t>(ptr) && ptr != nullptr) {
|
|
|
|
StackVec<void *, 100> invalidMemVector;
|
|
|
|
invalidMemVector.push_back(ptr);
|
|
|
|
do {
|
|
|
|
ptr = alignedMallocWrapper(size, alignment);
|
|
|
|
if (restrictions->minAddress > reinterpret_cast<uintptr_t>(ptr) && ptr != nullptr) {
|
|
|
|
invalidMemVector.push_back(ptr);
|
|
|
|
} else {
|
|
|
|
break;
|
2018-01-22 23:43:26 +08:00
|
|
|
}
|
2019-05-19 04:44:46 +08:00
|
|
|
} while (1);
|
|
|
|
for (auto &it : invalidMemVector) {
|
|
|
|
alignedFreeWrapper(it);
|
2018-01-22 23:43:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
2018-11-30 18:01:33 +08:00
|
|
|
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryWithHostPtr(const AllocationData &allocationData) {
|
2017-12-21 07:45:38 +08:00
|
|
|
if (deferredDeleter) {
|
|
|
|
deferredDeleter->drain(true);
|
|
|
|
}
|
2018-10-23 17:42:40 +08:00
|
|
|
GraphicsAllocation *graphicsAllocation = nullptr;
|
2019-11-15 16:59:48 +08:00
|
|
|
auto osStorage = hostPtrManager->prepareOsStorageForAllocation(*this, allocationData.size, allocationData.hostPtr, allocationData.rootDeviceIndex);
|
2018-10-23 17:42:40 +08:00
|
|
|
if (osStorage.fragmentCount > 0) {
|
2019-02-25 21:11:34 +08:00
|
|
|
graphicsAllocation = createGraphicsAllocation(osStorage, allocationData);
|
2021-02-17 06:58:32 +08:00
|
|
|
if (graphicsAllocation == nullptr) {
|
|
|
|
hostPtrManager->releaseHandleStorage(allocationData.rootDeviceIndex, osStorage);
|
|
|
|
cleanOsHandles(osStorage, allocationData.rootDeviceIndex);
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
return graphicsAllocation;
|
|
|
|
}
|
|
|
|
|
2019-01-22 19:40:17 +08:00
|
|
|
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryForImageFromHostPtr(const AllocationData &allocationData) {
|
2020-01-13 16:40:03 +08:00
|
|
|
bool copyRequired = isCopyRequired(*allocationData.imgInfo, allocationData.hostPtr);
|
2018-12-14 18:24:45 +08:00
|
|
|
|
2021-07-02 00:00:22 +08:00
|
|
|
if (allocationData.hostPtr && !copyRequired) {
|
2019-01-22 19:40:17 +08:00
|
|
|
return allocateGraphicsMemoryWithHostPtr(allocationData);
|
2018-12-14 18:24:45 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
void MemoryManager::cleanGraphicsMemoryCreatedFromHostPtr(GraphicsAllocation *graphicsAllocation) {
|
2020-07-07 14:41:26 +08:00
|
|
|
hostPtrManager->releaseHandleStorage(graphicsAllocation->getRootDeviceIndex(), graphicsAllocation->fragmentsStorage);
|
2019-11-15 16:59:48 +08:00
|
|
|
cleanOsHandles(graphicsAllocation->fragmentsStorage, graphicsAllocation->getRootDeviceIndex());
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsAllocation *MemoryManager::createGraphicsAllocationWithPadding(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding) {
|
|
|
|
return createPaddedAllocation(inputGraphicsAllocation, sizeWithPadding);
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsAllocation *MemoryManager::createPaddedAllocation(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding) {
|
2020-07-08 18:09:05 +08:00
|
|
|
return allocateGraphicsMemoryWithProperties({inputGraphicsAllocation->getRootDeviceIndex(), sizeWithPadding, GraphicsAllocation::AllocationType::INTERNAL_HOST_MEMORY, systemMemoryBitfield});
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
2021-02-25 16:38:48 +08:00
|
|
|
void *MemoryManager::createMultiGraphicsAllocationInSystemMemoryPool(std::vector<uint32_t> &rootDeviceIndices, AllocationProperties &properties, MultiGraphicsAllocation &multiGraphicsAllocation) {
|
2020-08-21 19:10:59 +08:00
|
|
|
void *ptr = nullptr;
|
|
|
|
|
2021-05-13 18:28:30 +08:00
|
|
|
properties.flags.forceSystemMemory = true;
|
2020-08-21 19:10:59 +08:00
|
|
|
for (auto &rootDeviceIndex : rootDeviceIndices) {
|
2021-05-13 18:28:30 +08:00
|
|
|
if (multiGraphicsAllocation.getGraphicsAllocation(rootDeviceIndex)) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-21 19:10:59 +08:00
|
|
|
properties.rootDeviceIndex = rootDeviceIndex;
|
2021-03-16 18:38:53 +08:00
|
|
|
properties.flags.isUSMHostAllocation = true;
|
|
|
|
|
|
|
|
if (isLimitedRange(properties.rootDeviceIndex)) {
|
|
|
|
properties.flags.isUSMHostAllocation = false;
|
|
|
|
DEBUG_BREAK_IF(rootDeviceIndices.size() > 1);
|
|
|
|
}
|
2020-08-21 19:10:59 +08:00
|
|
|
|
|
|
|
if (!ptr) {
|
|
|
|
auto graphicsAllocation = allocateGraphicsMemoryWithProperties(properties);
|
|
|
|
if (!graphicsAllocation) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
multiGraphicsAllocation.addAllocation(graphicsAllocation);
|
2021-02-25 16:38:48 +08:00
|
|
|
ptr = reinterpret_cast<void *>(graphicsAllocation->getUnderlyingBuffer());
|
2020-08-21 19:10:59 +08:00
|
|
|
} else {
|
|
|
|
properties.flags.allocateMemory = false;
|
|
|
|
|
2020-10-21 16:50:53 +08:00
|
|
|
auto graphicsAllocation = createGraphicsAllocationFromExistingStorage(properties, ptr, multiGraphicsAllocation);
|
|
|
|
|
2020-08-21 19:10:59 +08:00
|
|
|
if (!graphicsAllocation) {
|
|
|
|
for (auto gpuAllocation : multiGraphicsAllocation.getGraphicsAllocations()) {
|
|
|
|
freeGraphicsMemory(gpuAllocation);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
multiGraphicsAllocation.addAllocation(graphicsAllocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2020-10-21 16:50:53 +08:00
|
|
|
GraphicsAllocation *MemoryManager::createGraphicsAllocationFromExistingStorage(AllocationProperties &properties, void *ptr, MultiGraphicsAllocation &multiGraphicsAllocation) {
|
|
|
|
return allocateGraphicsMemoryWithProperties(properties, ptr);
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
void MemoryManager::freeSystemMemory(void *ptr) {
|
|
|
|
::alignedFree(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::freeGraphicsMemory(GraphicsAllocation *gfxAllocation) {
|
2019-01-24 18:51:33 +08:00
|
|
|
if (!gfxAllocation) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-27 21:31:29 +08:00
|
|
|
if (ApiSpecificConfig::getBindlessConfiguration() && executionEnvironment.rootDeviceEnvironments[gfxAllocation->getRootDeviceIndex()]->getBindlessHeapsHelper() != nullptr) {
|
|
|
|
executionEnvironment.rootDeviceEnvironments[gfxAllocation->getRootDeviceIndex()]->getBindlessHeapsHelper()->placeSSAllocationInReuseVectorOnFreeMemory(gfxAllocation);
|
|
|
|
}
|
2019-03-04 21:50:26 +08:00
|
|
|
const bool hasFragments = gfxAllocation->fragmentsStorage.fragmentCount != 0;
|
|
|
|
const bool isLocked = gfxAllocation->isLocked();
|
|
|
|
DEBUG_BREAK_IF(hasFragments && isLocked);
|
|
|
|
|
|
|
|
if (!hasFragments) {
|
|
|
|
handleFenceCompletion(gfxAllocation);
|
|
|
|
}
|
|
|
|
if (isLocked) {
|
2019-03-11 17:19:02 +08:00
|
|
|
freeAssociatedResourceImpl(*gfxAllocation);
|
2019-01-24 22:16:12 +08:00
|
|
|
}
|
2019-05-10 17:30:07 +08:00
|
|
|
|
2021-07-22 14:36:45 +08:00
|
|
|
getLocalMemoryUsageBankSelector(gfxAllocation->getAllocationType(), gfxAllocation->getRootDeviceIndex())->freeOnBanks(gfxAllocation->storageInfo.getMemoryBanks(), gfxAllocation->getUnderlyingBufferSize());
|
2017-12-21 07:45:38 +08:00
|
|
|
freeGraphicsMemoryImpl(gfxAllocation);
|
|
|
|
}
|
2018-01-19 17:55:36 +08:00
|
|
|
//if not in use destroy in place
|
|
|
|
//if in use pass to temporary allocation list that is cleaned on blocking calls
|
|
|
|
void MemoryManager::checkGpuUsageAndDestroyGraphicsAllocations(GraphicsAllocation *gfxAllocation) {
|
2019-01-03 21:48:24 +08:00
|
|
|
if (gfxAllocation->isUsed()) {
|
|
|
|
if (gfxAllocation->isUsedByManyOsContexts()) {
|
|
|
|
multiContextResourceDestructor->deferDeletion(new DeferrableAllocationDeletion{*this, *gfxAllocation});
|
|
|
|
multiContextResourceDestructor->drain(false);
|
|
|
|
return;
|
|
|
|
}
|
2019-02-18 20:59:16 +08:00
|
|
|
for (auto &engine : getRegisteredEngines()) {
|
|
|
|
auto osContextId = engine.osContext->getContextId();
|
|
|
|
auto allocationTaskCount = gfxAllocation->getTaskCount(osContextId);
|
|
|
|
if (gfxAllocation->isUsedByOsContext(osContextId) &&
|
|
|
|
allocationTaskCount > *engine.commandStreamReceiver->getTagAddress()) {
|
|
|
|
engine.commandStreamReceiver->getInternalAllocationStorage()->storeAllocation(std::unique_ptr<GraphicsAllocation>(gfxAllocation),
|
|
|
|
TEMPORARY_ALLOCATION);
|
|
|
|
return;
|
2018-12-10 20:17:52 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-19 17:55:36 +08:00
|
|
|
}
|
2018-12-10 20:17:52 +08:00
|
|
|
freeGraphicsMemory(gfxAllocation);
|
2018-01-19 17:55:36 +08:00
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
void MemoryManager::waitForDeletions() {
|
|
|
|
if (deferredDeleter) {
|
|
|
|
deferredDeleter->drain(false);
|
|
|
|
}
|
|
|
|
deferredDeleter.reset(nullptr);
|
|
|
|
}
|
|
|
|
bool MemoryManager::isAsyncDeleterEnabled() const {
|
|
|
|
return asyncDeleterEnabled;
|
|
|
|
}
|
|
|
|
|
2020-02-12 15:29:26 +08:00
|
|
|
bool MemoryManager::isLocalMemorySupported(uint32_t rootDeviceIndex) const {
|
|
|
|
return localMemorySupported[rootDeviceIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemoryManager::peek64kbPagesEnabled(uint32_t rootDeviceIndex) const {
|
|
|
|
return enable64kbpages[rootDeviceIndex];
|
2018-10-12 21:20:02 +08:00
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
bool MemoryManager::isMemoryBudgetExhausted() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-09 02:50:32 +08:00
|
|
|
OsContext *MemoryManager::createAndRegisterOsContext(CommandStreamReceiver *commandStreamReceiver,
|
2021-08-12 01:36:00 +08:00
|
|
|
const EngineDescriptor &engineDescriptor) {
|
2018-11-21 16:57:51 +08:00
|
|
|
auto contextId = ++latestContextId;
|
2020-03-04 06:33:31 +08:00
|
|
|
auto osContext = OsContext::create(peekExecutionEnvironment().rootDeviceEnvironments[commandStreamReceiver->getRootDeviceIndex()]->osInterface.get(),
|
2021-08-12 01:36:00 +08:00
|
|
|
contextId, engineDescriptor);
|
2018-11-21 16:57:51 +08:00
|
|
|
osContext->incRefInternal();
|
2019-02-18 20:59:16 +08:00
|
|
|
|
|
|
|
registeredEngines.emplace_back(commandStreamReceiver, osContext);
|
2018-11-21 16:57:51 +08:00
|
|
|
|
|
|
|
return osContext;
|
2018-09-06 15:01:13 +08:00
|
|
|
}
|
|
|
|
|
2019-05-10 17:30:07 +08:00
|
|
|
bool MemoryManager::getAllocationData(AllocationData &allocationData, const AllocationProperties &properties, const void *hostPtr, const StorageInfo &storageInfo) {
|
2018-12-06 22:03:06 +08:00
|
|
|
UNRECOVERABLE_IF(hostPtr == nullptr && !properties.flags.allocateMemory);
|
2019-04-17 19:59:24 +08:00
|
|
|
UNRECOVERABLE_IF(properties.allocationType == GraphicsAllocation::AllocationType::UNKNOWN);
|
2018-07-09 20:12:32 +08:00
|
|
|
|
2020-09-22 00:08:07 +08:00
|
|
|
auto hwInfo = executionEnvironment.rootDeviceEnvironments[properties.rootDeviceIndex]->getHardwareInfo();
|
|
|
|
auto &hwHelper = HwHelper::get(hwInfo->platform.eRenderCoreFamily);
|
|
|
|
|
2018-07-09 20:12:32 +08:00
|
|
|
bool allow64KbPages = false;
|
|
|
|
bool allow32Bit = false;
|
2018-12-06 22:03:06 +08:00
|
|
|
bool forcePin = properties.flags.forcePin;
|
2019-04-03 17:22:04 +08:00
|
|
|
bool mayRequireL3Flush = false;
|
2018-07-09 20:12:32 +08:00
|
|
|
|
2018-12-12 01:56:37 +08:00
|
|
|
switch (properties.allocationType) {
|
2018-07-09 20:12:32 +08:00
|
|
|
case GraphicsAllocation::AllocationType::BUFFER:
|
2018-07-23 01:27:33 +08:00
|
|
|
case GraphicsAllocation::AllocationType::BUFFER_COMPRESSED:
|
2019-04-15 17:00:21 +08:00
|
|
|
case GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY:
|
2018-07-09 20:12:32 +08:00
|
|
|
case GraphicsAllocation::AllocationType::CONSTANT_SURFACE:
|
|
|
|
case GraphicsAllocation::AllocationType::GLOBAL_SURFACE:
|
2019-04-15 17:00:21 +08:00
|
|
|
case GraphicsAllocation::AllocationType::PIPE:
|
|
|
|
case GraphicsAllocation::AllocationType::PRINTF_SURFACE:
|
|
|
|
case GraphicsAllocation::AllocationType::PRIVATE_SURFACE:
|
|
|
|
case GraphicsAllocation::AllocationType::SCRATCH_SURFACE:
|
2021-02-17 01:04:00 +08:00
|
|
|
case GraphicsAllocation::AllocationType::WORK_PARTITION_SURFACE:
|
2019-07-02 22:39:32 +08:00
|
|
|
case GraphicsAllocation::AllocationType::WRITE_COMBINED:
|
2018-07-09 20:12:32 +08:00
|
|
|
allow64KbPages = true;
|
|
|
|
allow32Bit = true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-31 17:40:11 +08:00
|
|
|
switch (properties.allocationType) {
|
2019-03-06 23:35:21 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SVM_GPU:
|
2019-04-15 17:00:21 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SVM_ZERO_COPY:
|
2021-02-19 23:11:13 +08:00
|
|
|
case GraphicsAllocation::AllocationType::GPU_TIMESTAMP_DEVICE_BUFFER:
|
2021-04-30 23:39:01 +08:00
|
|
|
case GraphicsAllocation::AllocationType::PREEMPTION:
|
2018-12-31 17:40:11 +08:00
|
|
|
allow64KbPages = true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-12 01:56:37 +08:00
|
|
|
switch (properties.allocationType) {
|
2018-07-25 00:36:26 +08:00
|
|
|
case GraphicsAllocation::AllocationType::BUFFER:
|
|
|
|
case GraphicsAllocation::AllocationType::BUFFER_COMPRESSED:
|
2019-04-15 17:00:21 +08:00
|
|
|
case GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY:
|
2019-07-02 22:39:32 +08:00
|
|
|
case GraphicsAllocation::AllocationType::WRITE_COMBINED:
|
2018-07-25 00:36:26 +08:00
|
|
|
forcePin = true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-04-03 17:22:04 +08:00
|
|
|
switch (properties.allocationType) {
|
|
|
|
case GraphicsAllocation::AllocationType::BUFFER:
|
|
|
|
case GraphicsAllocation::AllocationType::BUFFER_COMPRESSED:
|
|
|
|
case GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY:
|
|
|
|
case GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR:
|
|
|
|
case GraphicsAllocation::AllocationType::GLOBAL_SURFACE:
|
|
|
|
case GraphicsAllocation::AllocationType::IMAGE:
|
2019-12-04 20:42:41 +08:00
|
|
|
case GraphicsAllocation::AllocationType::MAP_ALLOCATION:
|
2019-04-03 17:22:04 +08:00
|
|
|
case GraphicsAllocation::AllocationType::PIPE:
|
|
|
|
case GraphicsAllocation::AllocationType::SHARED_BUFFER:
|
2019-04-15 17:00:21 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SHARED_IMAGE:
|
2019-04-03 17:22:04 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SHARED_RESOURCE_COPY:
|
2019-03-06 23:35:21 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SVM_CPU:
|
2019-04-15 17:00:21 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SVM_GPU:
|
|
|
|
case GraphicsAllocation::AllocationType::SVM_ZERO_COPY:
|
2019-07-02 22:39:32 +08:00
|
|
|
case GraphicsAllocation::AllocationType::WRITE_COMBINED:
|
2019-04-03 17:22:04 +08:00
|
|
|
mayRequireL3Flush = true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-12 01:56:37 +08:00
|
|
|
switch (properties.allocationType) {
|
2020-04-07 18:22:47 +08:00
|
|
|
case GraphicsAllocation::AllocationType::COMMAND_BUFFER:
|
2021-06-29 18:26:04 +08:00
|
|
|
case GraphicsAllocation::AllocationType::RING_BUFFER:
|
2021-08-02 20:35:33 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER:
|
2019-04-15 19:10:26 +08:00
|
|
|
case GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY:
|
|
|
|
case GraphicsAllocation::AllocationType::DEVICE_QUEUE_BUFFER:
|
2019-04-15 17:00:21 +08:00
|
|
|
case GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR:
|
2018-12-12 01:56:37 +08:00
|
|
|
case GraphicsAllocation::AllocationType::FILL_PATTERN:
|
2019-12-04 20:42:41 +08:00
|
|
|
case GraphicsAllocation::AllocationType::MAP_ALLOCATION:
|
2019-04-16 21:59:23 +08:00
|
|
|
case GraphicsAllocation::AllocationType::MCS:
|
2019-02-08 17:27:48 +08:00
|
|
|
case GraphicsAllocation::AllocationType::PROFILING_TAG_BUFFER:
|
2019-04-16 21:59:23 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SHARED_CONTEXT_IMAGE:
|
2019-03-06 23:35:21 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SVM_CPU:
|
2019-04-15 17:00:21 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SVM_ZERO_COPY:
|
|
|
|
case GraphicsAllocation::AllocationType::TAG_BUFFER:
|
2020-02-06 03:00:08 +08:00
|
|
|
case GraphicsAllocation::AllocationType::GLOBAL_FENCE:
|
2019-04-16 17:29:19 +08:00
|
|
|
case GraphicsAllocation::AllocationType::INTERNAL_HOST_MEMORY:
|
2020-05-27 23:39:43 +08:00
|
|
|
case GraphicsAllocation::AllocationType::TIMESTAMP_PACKET_TAG_BUFFER:
|
2020-06-24 18:54:30 +08:00
|
|
|
case GraphicsAllocation::AllocationType::DEBUG_CONTEXT_SAVE_AREA:
|
2020-10-29 14:23:59 +08:00
|
|
|
case GraphicsAllocation::AllocationType::DEBUG_SBA_TRACKING_BUFFER:
|
2021-04-03 01:53:59 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SW_TAG_BUFFER:
|
2018-12-12 01:56:37 +08:00
|
|
|
allocationData.flags.useSystemMemory = true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:53:30 +08:00
|
|
|
if (GraphicsAllocation::isIsaAllocationType(properties.allocationType)) {
|
2020-09-22 00:08:07 +08:00
|
|
|
allocationData.flags.useSystemMemory = hwHelper.useSystemMemoryPlacementForISA(*hwInfo);
|
|
|
|
}
|
|
|
|
|
2020-03-06 23:58:43 +08:00
|
|
|
switch (properties.allocationType) {
|
2020-03-24 16:26:43 +08:00
|
|
|
case GraphicsAllocation::AllocationType::COMMAND_BUFFER:
|
2020-03-06 23:58:43 +08:00
|
|
|
case GraphicsAllocation::AllocationType::DEVICE_QUEUE_BUFFER:
|
|
|
|
case GraphicsAllocation::AllocationType::IMAGE:
|
|
|
|
case GraphicsAllocation::AllocationType::INDIRECT_OBJECT_HEAP:
|
|
|
|
case GraphicsAllocation::AllocationType::INSTRUCTION_HEAP:
|
|
|
|
case GraphicsAllocation::AllocationType::INTERNAL_HEAP:
|
|
|
|
case GraphicsAllocation::AllocationType::KERNEL_ISA:
|
2020-11-13 02:53:30 +08:00
|
|
|
case GraphicsAllocation::AllocationType::KERNEL_ISA_INTERNAL:
|
2020-03-06 23:58:43 +08:00
|
|
|
case GraphicsAllocation::AllocationType::LINEAR_STREAM:
|
|
|
|
case GraphicsAllocation::AllocationType::MCS:
|
2021-04-30 23:39:01 +08:00
|
|
|
case GraphicsAllocation::AllocationType::PREEMPTION:
|
2020-03-06 23:58:43 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SCRATCH_SURFACE:
|
2021-02-17 01:04:00 +08:00
|
|
|
case GraphicsAllocation::AllocationType::WORK_PARTITION_SURFACE:
|
2020-03-06 23:58:43 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SHARED_CONTEXT_IMAGE:
|
|
|
|
case GraphicsAllocation::AllocationType::SHARED_IMAGE:
|
|
|
|
case GraphicsAllocation::AllocationType::SHARED_RESOURCE_COPY:
|
|
|
|
case GraphicsAllocation::AllocationType::SURFACE_STATE_HEAP:
|
|
|
|
case GraphicsAllocation::AllocationType::TIMESTAMP_PACKET_TAG_BUFFER:
|
2020-10-14 22:46:11 +08:00
|
|
|
case GraphicsAllocation::AllocationType::DEBUG_MODULE_AREA:
|
2021-02-19 23:11:13 +08:00
|
|
|
case GraphicsAllocation::AllocationType::GPU_TIMESTAMP_DEVICE_BUFFER:
|
2020-03-06 23:58:43 +08:00
|
|
|
allocationData.flags.resource48Bit = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
allocationData.flags.resource48Bit = properties.flags.resource48Bit;
|
|
|
|
}
|
|
|
|
|
2019-11-14 17:08:59 +08:00
|
|
|
allocationData.flags.shareable = properties.flags.shareable;
|
2021-08-11 18:36:04 +08:00
|
|
|
allocationData.flags.isUSMDeviceMemory = properties.flags.isUSMDeviceAllocation;
|
2019-02-18 17:49:21 +08:00
|
|
|
allocationData.flags.requiresCpuAccess = GraphicsAllocation::isCpuAccessRequired(properties.allocationType);
|
2018-12-06 22:03:06 +08:00
|
|
|
allocationData.flags.allocateMemory = properties.flags.allocateMemory;
|
2018-07-09 20:12:32 +08:00
|
|
|
allocationData.flags.allow32Bit = allow32Bit;
|
|
|
|
allocationData.flags.allow64kbPages = allow64KbPages;
|
|
|
|
allocationData.flags.forcePin = forcePin;
|
2019-04-04 01:16:55 +08:00
|
|
|
allocationData.flags.uncacheable = properties.flags.uncacheable;
|
2019-04-03 17:22:04 +08:00
|
|
|
allocationData.flags.flushL3 =
|
|
|
|
(mayRequireL3Flush ? properties.flags.flushL3RequiredForRead | properties.flags.flushL3RequiredForWrite : 0u);
|
2021-08-02 18:44:48 +08:00
|
|
|
allocationData.flags.preferRenderCompressed = CompressionSelector::preferRenderCompressedBuffer(properties, *hwInfo);
|
2019-04-04 01:16:55 +08:00
|
|
|
allocationData.flags.multiOsContextCapable = properties.flags.multiOsContextCapable;
|
2020-10-21 22:29:25 +08:00
|
|
|
|
2021-09-22 02:17:41 +08:00
|
|
|
if (properties.allocationType == GraphicsAllocation::AllocationType::DEBUG_CONTEXT_SAVE_AREA) {
|
|
|
|
allocationData.flags.zeroMemory = 1;
|
|
|
|
}
|
|
|
|
|
2020-10-21 22:29:25 +08:00
|
|
|
if (properties.allocationType == GraphicsAllocation::AllocationType::DEBUG_MODULE_AREA) {
|
|
|
|
allocationData.flags.use32BitFrontWindow = true;
|
|
|
|
} else {
|
|
|
|
allocationData.flags.use32BitFrontWindow = properties.flags.use32BitFrontWindow;
|
|
|
|
}
|
2018-07-09 20:12:32 +08:00
|
|
|
|
|
|
|
allocationData.hostPtr = hostPtr;
|
2021-04-23 22:09:03 +08:00
|
|
|
if (properties.allocationType == GraphicsAllocation::AllocationType::KERNEL_ISA ||
|
|
|
|
properties.allocationType == GraphicsAllocation::AllocationType::KERNEL_ISA_INTERNAL) {
|
|
|
|
allocationData.size = properties.size + hwHelper.getPaddingForISAAllocation();
|
|
|
|
} else {
|
|
|
|
allocationData.size = properties.size;
|
|
|
|
}
|
2018-12-12 01:56:37 +08:00
|
|
|
allocationData.type = properties.allocationType;
|
2019-05-10 17:30:07 +08:00
|
|
|
allocationData.storageInfo = storageInfo;
|
2018-12-06 22:03:06 +08:00
|
|
|
allocationData.alignment = properties.alignment ? properties.alignment : MemoryConstants::preferredAlignment;
|
2018-12-20 23:58:15 +08:00
|
|
|
allocationData.imgInfo = properties.imgInfo;
|
2018-07-09 20:12:32 +08:00
|
|
|
|
|
|
|
if (allocationData.flags.allocateMemory) {
|
|
|
|
allocationData.hostPtr = nullptr;
|
|
|
|
}
|
2019-10-22 16:26:23 +08:00
|
|
|
|
2020-07-01 16:38:19 +08:00
|
|
|
allocationData.gpuAddress = properties.gpuAddress;
|
|
|
|
allocationData.osContext = properties.osContext;
|
2019-11-07 21:15:04 +08:00
|
|
|
allocationData.rootDeviceIndex = properties.rootDeviceIndex;
|
2020-10-15 14:53:36 +08:00
|
|
|
allocationData.useMmapObject = properties.useMmapObject;
|
2019-11-07 21:15:04 +08:00
|
|
|
|
2021-03-24 21:19:30 +08:00
|
|
|
allocationData.flags.crossRootDeviceAccess = properties.flags.crossRootDeviceAccess;
|
2021-03-23 20:10:21 +08:00
|
|
|
allocationData.flags.useSystemMemory |= properties.flags.crossRootDeviceAccess;
|
|
|
|
|
2020-09-22 00:08:07 +08:00
|
|
|
hwHelper.setExtraAllocationData(allocationData, properties, *hwInfo);
|
2021-05-14 01:42:08 +08:00
|
|
|
allocationData.flags.useSystemMemory |= properties.flags.forceSystemMemory;
|
2020-06-02 19:38:02 +08:00
|
|
|
|
2021-05-25 02:57:32 +08:00
|
|
|
overrideAllocationData(allocationData, properties);
|
|
|
|
allocationData.flags.isUSMHostAllocation = properties.flags.isUSMHostAllocation;
|
2018-07-09 20:12:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-04 13:49:33 +08:00
|
|
|
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryInPreferredPool(const AllocationProperties &properties, const void *hostPtr) {
|
2018-07-09 20:12:32 +08:00
|
|
|
AllocationData allocationData;
|
2019-05-10 17:30:07 +08:00
|
|
|
getAllocationData(allocationData, properties, hostPtr, createStorageInfoFromProperties(properties));
|
2018-07-09 20:12:32 +08:00
|
|
|
|
2019-01-10 18:10:58 +08:00
|
|
|
AllocationStatus status = AllocationStatus::Error;
|
|
|
|
GraphicsAllocation *allocation = allocateGraphicsMemoryInDevicePool(allocationData, status);
|
2019-05-10 17:30:07 +08:00
|
|
|
if (allocation) {
|
2021-07-22 14:36:45 +08:00
|
|
|
getLocalMemoryUsageBankSelector(properties.allocationType, properties.rootDeviceIndex)->reserveOnBanks(allocationData.storageInfo.getMemoryBanks(), allocation->getUnderlyingBufferSize());
|
2020-09-10 18:36:44 +08:00
|
|
|
this->registerLocalMemAlloc(allocation, properties.rootDeviceIndex);
|
2019-05-10 17:30:07 +08:00
|
|
|
}
|
2018-07-18 15:48:21 +08:00
|
|
|
if (!allocation && status == AllocationStatus::RetryInNonDevicePool) {
|
|
|
|
allocation = allocateGraphicsMemory(allocationData);
|
2021-07-05 21:21:23 +08:00
|
|
|
if (allocation) {
|
|
|
|
this->registerSysMemAlloc(allocation);
|
|
|
|
}
|
2018-07-18 15:48:21 +08:00
|
|
|
}
|
2019-12-10 23:26:35 +08:00
|
|
|
FileLoggerInstance().logAllocation(allocation);
|
2020-10-09 16:48:37 +08:00
|
|
|
registerAllocationInOs(allocation);
|
2018-07-18 15:48:21 +08:00
|
|
|
return allocation;
|
2018-07-09 20:12:32 +08:00
|
|
|
}
|
|
|
|
|
2020-10-01 13:21:42 +08:00
|
|
|
GraphicsAllocation *MemoryManager::allocateInternalGraphicsMemoryWithHostCopy(uint32_t rootDeviceIndex,
|
|
|
|
DeviceBitfield bitField,
|
|
|
|
const void *ptr,
|
|
|
|
size_t size) {
|
|
|
|
NEO::AllocationProperties copyProperties{rootDeviceIndex,
|
|
|
|
size,
|
|
|
|
NEO::GraphicsAllocation::AllocationType::INTERNAL_HOST_MEMORY,
|
|
|
|
bitField};
|
|
|
|
copyProperties.alignment = MemoryConstants::pageSize;
|
|
|
|
auto allocation = this->allocateGraphicsMemoryWithProperties(copyProperties);
|
|
|
|
if (allocation) {
|
|
|
|
memcpy_s(allocation->getUnderlyingBuffer(), allocation->getUnderlyingBufferSize(), ptr, size);
|
|
|
|
}
|
|
|
|
return allocation;
|
|
|
|
}
|
|
|
|
|
2019-11-07 01:14:30 +08:00
|
|
|
bool MemoryManager::mapAuxGpuVA(GraphicsAllocation *graphicsAllocation) {
|
2021-09-29 23:59:41 +08:00
|
|
|
bool ret = false;
|
|
|
|
for (auto engine : registeredEngines) {
|
|
|
|
if (engine.commandStreamReceiver->pageTableManager.get()) {
|
|
|
|
ret = engine.commandStreamReceiver->pageTableManager->updateAuxTable(graphicsAllocation->getGpuAddress(), graphicsAllocation->getDefaultGmm(), true);
|
|
|
|
if (!ret) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 01:14:30 +08:00
|
|
|
}
|
2021-09-29 23:59:41 +08:00
|
|
|
return ret;
|
2019-11-07 01:14:30 +08:00
|
|
|
}
|
|
|
|
|
2018-07-09 20:12:32 +08:00
|
|
|
GraphicsAllocation *MemoryManager::allocateGraphicsMemory(const AllocationData &allocationData) {
|
2019-01-28 20:59:37 +08:00
|
|
|
if (allocationData.type == GraphicsAllocation::AllocationType::IMAGE || allocationData.type == GraphicsAllocation::AllocationType::SHARED_RESOURCE_COPY) {
|
2018-12-20 23:58:15 +08:00
|
|
|
UNRECOVERABLE_IF(allocationData.imgInfo == nullptr);
|
2019-01-22 19:40:17 +08:00
|
|
|
return allocateGraphicsMemoryForImage(allocationData);
|
2018-12-20 23:58:15 +08:00
|
|
|
}
|
2021-08-11 18:36:04 +08:00
|
|
|
if (allocationData.flags.shareable || allocationData.flags.isUSMDeviceMemory) {
|
|
|
|
return allocateMemoryByKMD(allocationData);
|
2019-11-14 17:08:59 +08:00
|
|
|
}
|
2021-06-02 21:57:28 +08:00
|
|
|
if (useNonSvmHostPtrAlloc(allocationData.type, allocationData.rootDeviceIndex) || isNonSvmBuffer(allocationData.hostPtr, allocationData.type, allocationData.rootDeviceIndex)) {
|
2019-03-12 19:00:41 +08:00
|
|
|
auto allocation = allocateGraphicsMemoryForNonSvmHostPtr(allocationData);
|
|
|
|
if (allocation) {
|
|
|
|
allocation->setFlushL3Required(allocationData.flags.flushL3);
|
|
|
|
}
|
|
|
|
return allocation;
|
|
|
|
}
|
2020-08-27 14:55:09 +08:00
|
|
|
bool use32Allocator = heapAssigner.use32BitHeap(allocationData.type);
|
2020-11-04 22:55:09 +08:00
|
|
|
|
2021-05-06 22:15:19 +08:00
|
|
|
bool isAllocationOnLimitedGPU = isLimitedGPUOnType(allocationData.rootDeviceIndex, allocationData.type);
|
|
|
|
if (use32Allocator || isAllocationOnLimitedGPU ||
|
2018-12-21 17:16:27 +08:00
|
|
|
(force32bitAllocations && allocationData.flags.allow32Bit && is64bit)) {
|
2020-08-27 14:55:09 +08:00
|
|
|
auto hwInfo = executionEnvironment.rootDeviceEnvironments[allocationData.rootDeviceIndex]->getHardwareInfo();
|
2021-09-21 21:34:25 +08:00
|
|
|
bool useLocalMem = heapAssigner.useExternal32BitHeap(allocationData.type) ? HwInfoConfig::get(hwInfo->platform.eProductFamily)->heapInLocalMem(*hwInfo) : false;
|
2020-08-27 14:55:09 +08:00
|
|
|
return allocate32BitGraphicsMemoryImpl(allocationData, useLocalMem);
|
2018-07-09 20:12:32 +08:00
|
|
|
}
|
2020-10-05 15:57:50 +08:00
|
|
|
if (allocationData.flags.isUSMHostAllocation && allocationData.hostPtr) {
|
2020-09-03 15:57:05 +08:00
|
|
|
return allocateUSMHostGraphicsMemory(allocationData);
|
|
|
|
}
|
2018-07-09 20:12:32 +08:00
|
|
|
if (allocationData.hostPtr) {
|
2018-11-30 18:01:33 +08:00
|
|
|
return allocateGraphicsMemoryWithHostPtr(allocationData);
|
2018-07-09 20:12:32 +08:00
|
|
|
}
|
2020-07-01 16:38:19 +08:00
|
|
|
if (allocationData.gpuAddress) {
|
|
|
|
return allocateGraphicsMemoryWithGpuVa(allocationData);
|
|
|
|
}
|
2020-02-12 15:29:26 +08:00
|
|
|
if (peek64kbPagesEnabled(allocationData.rootDeviceIndex) && allocationData.flags.allow64kbPages) {
|
2018-12-06 22:03:06 +08:00
|
|
|
return allocateGraphicsMemory64kb(allocationData);
|
2018-07-09 20:12:32 +08:00
|
|
|
}
|
2018-11-30 18:01:33 +08:00
|
|
|
return allocateGraphicsMemoryWithAlignment(allocationData);
|
2018-07-09 20:12:32 +08:00
|
|
|
}
|
2018-11-29 18:39:10 +08:00
|
|
|
|
2019-01-22 19:40:17 +08:00
|
|
|
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryForImage(const AllocationData &allocationData) {
|
2020-02-25 01:04:30 +08:00
|
|
|
auto gmm = std::make_unique<Gmm>(executionEnvironment.rootDeviceEnvironments[allocationData.rootDeviceIndex]->getGmmClientContext(), *allocationData.imgInfo, allocationData.storageInfo);
|
2019-01-22 19:40:17 +08:00
|
|
|
|
|
|
|
// AllocationData needs to be reconfigured for System Memory paths
|
|
|
|
AllocationData allocationDataWithSize = allocationData;
|
|
|
|
allocationDataWithSize.size = allocationData.imgInfo->size;
|
|
|
|
|
|
|
|
auto hostPtrAllocation = allocateGraphicsMemoryForImageFromHostPtr(allocationDataWithSize);
|
|
|
|
|
|
|
|
if (hostPtrAllocation) {
|
2019-03-12 20:24:58 +08:00
|
|
|
hostPtrAllocation->setDefaultGmm(gmm.release());
|
2019-01-22 19:40:17 +08:00
|
|
|
return hostPtrAllocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
return allocateGraphicsMemoryForImageImpl(allocationDataWithSize, std::move(gmm));
|
|
|
|
}
|
|
|
|
|
2019-02-18 20:59:16 +08:00
|
|
|
EngineControlContainer &MemoryManager::getRegisteredEngines() {
|
|
|
|
return registeredEngines;
|
2018-11-29 18:39:10 +08:00
|
|
|
}
|
|
|
|
|
2021-07-22 16:43:58 +08:00
|
|
|
bool MemoryManager::isExternalAllocation(GraphicsAllocation::AllocationType allocationType) {
|
|
|
|
if (allocationType == GraphicsAllocation::AllocationType::BUFFER ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::BUFFER_COMPRESSED ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::FILL_PATTERN ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::IMAGE ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::MAP_ALLOCATION ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::PIPE ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::SHARED_BUFFER ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::SHARED_CONTEXT_IMAGE ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::SHARED_IMAGE ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::SHARED_RESOURCE_COPY ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::SVM_CPU ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::SVM_GPU ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::SVM_ZERO_COPY ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::UNIFIED_SHARED_MEMORY ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::WRITE_COMBINED) {
|
2021-07-22 14:36:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalMemoryUsageBankSelector *MemoryManager::getLocalMemoryUsageBankSelector(GraphicsAllocation::AllocationType allocationType, uint32_t rootDeviceIndex) {
|
2021-07-22 16:43:58 +08:00
|
|
|
if (isExternalAllocation(allocationType)) {
|
|
|
|
return externalLocalMemoryUsageBankSelector[rootDeviceIndex].get();
|
2021-07-22 14:36:45 +08:00
|
|
|
}
|
2021-07-22 16:43:58 +08:00
|
|
|
return internalLocalMemoryUsageBankSelector[rootDeviceIndex].get();
|
2021-07-22 14:36:45 +08:00
|
|
|
}
|
|
|
|
|
2019-02-27 00:52:31 +08:00
|
|
|
EngineControl *MemoryManager::getRegisteredEngineForCsr(CommandStreamReceiver *commandStreamReceiver) {
|
|
|
|
EngineControl *engineCtrl = nullptr;
|
|
|
|
for (auto &engine : registeredEngines) {
|
|
|
|
if (engine.commandStreamReceiver == commandStreamReceiver) {
|
|
|
|
engineCtrl = &engine;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return engineCtrl;
|
|
|
|
}
|
|
|
|
|
2019-09-02 19:23:25 +08:00
|
|
|
void MemoryManager::unregisterEngineForCsr(CommandStreamReceiver *commandStreamReceiver) {
|
|
|
|
auto numRegisteredEngines = registeredEngines.size();
|
|
|
|
for (auto i = 0u; i < numRegisteredEngines; i++) {
|
|
|
|
if (registeredEngines[i].commandStreamReceiver == commandStreamReceiver) {
|
|
|
|
registeredEngines[i].osContext->decRefInternal();
|
|
|
|
std::swap(registeredEngines[i], registeredEngines[numRegisteredEngines - 1]);
|
|
|
|
registeredEngines.pop_back();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 18:51:33 +08:00
|
|
|
void *MemoryManager::lockResource(GraphicsAllocation *graphicsAllocation) {
|
|
|
|
if (!graphicsAllocation) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-01-24 22:16:12 +08:00
|
|
|
if (graphicsAllocation->isLocked()) {
|
|
|
|
return graphicsAllocation->getLockedPtr();
|
|
|
|
}
|
2019-01-24 18:51:33 +08:00
|
|
|
auto retVal = lockResourceImpl(*graphicsAllocation);
|
2019-01-24 22:16:12 +08:00
|
|
|
graphicsAllocation->lock(retVal);
|
2019-01-24 18:51:33 +08:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::unlockResource(GraphicsAllocation *graphicsAllocation) {
|
|
|
|
if (!graphicsAllocation) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DEBUG_BREAK_IF(!graphicsAllocation->isLocked());
|
|
|
|
unlockResourceImpl(*graphicsAllocation);
|
2019-01-24 22:16:12 +08:00
|
|
|
graphicsAllocation->unlock();
|
2019-01-24 18:51:33 +08:00
|
|
|
}
|
2019-02-20 18:18:24 +08:00
|
|
|
|
2020-10-14 15:50:07 +08:00
|
|
|
HeapIndex MemoryManager::selectHeap(const GraphicsAllocation *allocation, bool hasPointer, bool isFullRangeSVM, bool useFrontWindow) {
|
2019-02-20 18:18:24 +08:00
|
|
|
if (allocation) {
|
2020-08-27 14:55:09 +08:00
|
|
|
if (heapAssigner.useInternal32BitHeap(allocation->getAllocationType())) {
|
2020-10-21 22:29:25 +08:00
|
|
|
return useFrontWindow ? HeapAssigner::mapInternalWindowIndex(selectInternalHeap(allocation->isAllocatedInLocalMemoryPool())) : selectInternalHeap(allocation->isAllocatedInLocalMemoryPool());
|
2019-05-19 04:44:46 +08:00
|
|
|
}
|
2020-08-27 14:55:09 +08:00
|
|
|
if (allocation->is32BitAllocation() || heapAssigner.useExternal32BitHeap(allocation->getAllocationType())) {
|
2020-10-14 15:50:07 +08:00
|
|
|
return useFrontWindow ? HeapAssigner::mapExternalWindowIndex(selectExternalHeap(allocation->isAllocatedInLocalMemoryPool()))
|
|
|
|
: selectExternalHeap(allocation->isAllocatedInLocalMemoryPool());
|
2019-02-20 18:18:24 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-18 17:06:01 +08:00
|
|
|
if (isFullRangeSVM) {
|
|
|
|
if (hasPointer) {
|
2019-02-20 18:18:24 +08:00
|
|
|
return HeapIndex::HEAP_SVM;
|
|
|
|
}
|
2019-03-12 20:24:58 +08:00
|
|
|
if (allocation && allocation->getDefaultGmm()->gmmResourceInfo->is64KBPageSuitable()) {
|
2019-02-22 17:53:37 +08:00
|
|
|
return HeapIndex::HEAP_STANDARD64KB;
|
2019-02-20 18:18:24 +08:00
|
|
|
}
|
|
|
|
return HeapIndex::HEAP_STANDARD;
|
|
|
|
}
|
2019-03-15 18:09:06 +08:00
|
|
|
// Limited range allocation goes to STANDARD heap
|
|
|
|
return HeapIndex::HEAP_STANDARD;
|
2019-02-20 18:18:24 +08:00
|
|
|
}
|
2019-07-08 02:33:17 +08:00
|
|
|
|
2020-10-07 21:32:03 +08:00
|
|
|
bool MemoryManager::copyMemoryToAllocation(GraphicsAllocation *graphicsAllocation, size_t destinationOffset, const void *memoryToCopy, size_t sizeToCopy) {
|
2019-03-11 18:13:17 +08:00
|
|
|
if (!graphicsAllocation->getUnderlyingBuffer()) {
|
2019-03-01 21:25:33 +08:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-21 21:16:52 +08:00
|
|
|
for (auto i = 0u; i < graphicsAllocation->storageInfo.getNumBanks(); ++i) {
|
|
|
|
memcpy_s(ptrOffset(static_cast<uint8_t *>(graphicsAllocation->getUnderlyingBuffer()) + i * graphicsAllocation->getUnderlyingBufferSize(), destinationOffset),
|
|
|
|
(graphicsAllocation->getUnderlyingBufferSize() - destinationOffset), memoryToCopy, sizeToCopy);
|
|
|
|
}
|
2019-03-01 21:25:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
2019-03-22 21:04:25 +08:00
|
|
|
|
|
|
|
void MemoryManager::waitForEnginesCompletion(GraphicsAllocation &graphicsAllocation) {
|
|
|
|
for (auto &engine : getRegisteredEngines()) {
|
|
|
|
auto osContextId = engine.osContext->getContextId();
|
|
|
|
auto allocationTaskCount = graphicsAllocation.getTaskCount(osContextId);
|
|
|
|
if (graphicsAllocation.isUsedByOsContext(osContextId) &&
|
2021-04-29 16:58:16 +08:00
|
|
|
engine.commandStreamReceiver->getTagAllocation() != nullptr &&
|
2019-03-22 21:04:25 +08:00
|
|
|
allocationTaskCount > *engine.commandStreamReceiver->getTagAddress()) {
|
|
|
|
engine.commandStreamReceiver->waitForCompletionWithTimeout(false, TimeoutControls::maxTimeout, allocationTaskCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-23 19:52:57 +08:00
|
|
|
void MemoryManager::cleanTemporaryAllocationListOnAllEngines(bool waitForCompletion) {
|
|
|
|
for (auto &engine : getRegisteredEngines()) {
|
|
|
|
auto csr = engine.commandStreamReceiver;
|
|
|
|
if (waitForCompletion) {
|
|
|
|
csr->waitForCompletionWithTimeout(false, 0, csr->peekLatestSentTaskCount());
|
|
|
|
}
|
|
|
|
csr->getInternalAllocationStorage()->cleanAllocationList(*csr->getTagAddress(), AllocationUsage::TEMPORARY_ALLOCATION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:26:30 +08:00
|
|
|
void *MemoryManager::getReservedMemory(size_t size, size_t alignment) {
|
|
|
|
static std::mutex mutex;
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
if (!reservedMemory) {
|
|
|
|
reservedMemory = allocateSystemMemory(size, alignment);
|
|
|
|
}
|
|
|
|
return reservedMemory;
|
|
|
|
}
|
|
|
|
|
2020-02-18 20:29:30 +08:00
|
|
|
bool MemoryManager::isHostPointerTrackingEnabled(uint32_t rootDeviceIndex) {
|
2019-12-04 20:42:41 +08:00
|
|
|
if (DebugManager.flags.EnableHostPtrTracking.get() != -1) {
|
|
|
|
return !!DebugManager.flags.EnableHostPtrTracking.get();
|
|
|
|
}
|
2020-02-18 20:29:30 +08:00
|
|
|
return (peekExecutionEnvironment().rootDeviceEnvironments[rootDeviceIndex]->getHardwareInfo()->capabilityTable.hostPtrTrackingEnabled | is32bit);
|
2019-12-04 20:42:41 +08:00
|
|
|
}
|
|
|
|
|
2020-03-04 15:51:02 +08:00
|
|
|
bool MemoryManager::useNonSvmHostPtrAlloc(GraphicsAllocation::AllocationType allocationType, uint32_t rootDeviceIndex) {
|
|
|
|
bool isExternalHostPtrAlloc = (allocationType == GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR);
|
|
|
|
bool isMapAlloc = (allocationType == GraphicsAllocation::AllocationType::MAP_ALLOCATION);
|
|
|
|
|
|
|
|
if (forceNonSvmForExternalHostPtr && isExternalHostPtrAlloc) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNonSvmPtrCapable = ((!peekExecutionEnvironment().rootDeviceEnvironments[rootDeviceIndex]->isFullRangeSvm() || !isHostPointerTrackingEnabled(rootDeviceIndex)) & !is32bit);
|
|
|
|
|
|
|
|
return isNonSvmPtrCapable && (isExternalHostPtrAlloc || isMapAlloc);
|
|
|
|
}
|
|
|
|
|
2020-01-13 16:40:03 +08:00
|
|
|
bool MemoryManager::isCopyRequired(ImageInfo &imgInfo, const void *hostPtr) {
|
|
|
|
if (!hostPtr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t imageWidth = imgInfo.imgDesc.imageWidth;
|
|
|
|
size_t imageHeight = 1;
|
|
|
|
size_t imageDepth = 1;
|
|
|
|
size_t imageCount = 1;
|
|
|
|
|
|
|
|
switch (imgInfo.imgDesc.imageType) {
|
|
|
|
case ImageType::Image3D:
|
|
|
|
imageDepth = imgInfo.imgDesc.imageDepth;
|
|
|
|
CPP_ATTRIBUTE_FALLTHROUGH;
|
|
|
|
case ImageType::Image2D:
|
|
|
|
case ImageType::Image2DArray:
|
|
|
|
imageHeight = imgInfo.imgDesc.imageHeight;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto hostPtrRowPitch = imgInfo.imgDesc.imageRowPitch ? imgInfo.imgDesc.imageRowPitch : imageWidth * imgInfo.surfaceFormat->ImageElementSizeInBytes;
|
|
|
|
auto hostPtrSlicePitch = imgInfo.imgDesc.imageSlicePitch ? imgInfo.imgDesc.imageSlicePitch : hostPtrRowPitch * imgInfo.imgDesc.imageHeight;
|
|
|
|
|
|
|
|
size_t pointerPassedSize = hostPtrRowPitch * imageHeight * imageDepth * imageCount;
|
|
|
|
auto alignedSizePassedPointer = alignSizeWholePage(const_cast<void *>(hostPtr), pointerPassedSize);
|
|
|
|
auto alignedSizeRequiredForAllocation = alignSizeWholePage(const_cast<void *>(hostPtr), imgInfo.size);
|
|
|
|
|
|
|
|
// Passed pointer doesn't have enough memory, copy is needed
|
|
|
|
bool copyRequired = (alignedSizeRequiredForAllocation > alignedSizePassedPointer) |
|
|
|
|
(imgInfo.rowPitch != hostPtrRowPitch) |
|
|
|
|
(imgInfo.slicePitch != hostPtrSlicePitch) |
|
|
|
|
((reinterpret_cast<uintptr_t>(hostPtr) & (MemoryConstants::cacheLineSize - 1)) != 0) |
|
|
|
|
!imgInfo.linearStorage;
|
|
|
|
|
|
|
|
return copyRequired;
|
|
|
|
}
|
2020-01-16 00:02:47 +08:00
|
|
|
|
|
|
|
void MemoryManager::overrideAllocationData(AllocationData &allocationData, const AllocationProperties &properties) {
|
2020-04-30 23:43:37 +08:00
|
|
|
if (DebugManager.flags.ForceSystemMemoryPlacement.get()) {
|
|
|
|
if ((1llu << (static_cast<int64_t>(properties.allocationType) - 1)) & DebugManager.flags.ForceSystemMemoryPlacement.get()) {
|
|
|
|
allocationData.flags.useSystemMemory = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DebugManager.flags.ForceNonSystemMemoryPlacement.get()) {
|
|
|
|
if ((1llu << (static_cast<int64_t>(properties.allocationType) - 1)) & DebugManager.flags.ForceNonSystemMemoryPlacement.get()) {
|
|
|
|
allocationData.flags.useSystemMemory = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 00:02:47 +08:00
|
|
|
int32_t directRingPlacement = DebugManager.flags.DirectSubmissionBufferPlacement.get();
|
2020-03-25 21:55:23 +08:00
|
|
|
int32_t directRingAddressing = DebugManager.flags.DirectSubmissionBufferAddressing.get();
|
|
|
|
if (properties.allocationType == GraphicsAllocation::AllocationType::RING_BUFFER) {
|
|
|
|
if (directRingPlacement != -1) {
|
|
|
|
if (directRingPlacement == 0) {
|
|
|
|
allocationData.flags.requiresCpuAccess = true;
|
|
|
|
allocationData.flags.useSystemMemory = false;
|
|
|
|
} else {
|
|
|
|
allocationData.flags.requiresCpuAccess = false;
|
|
|
|
allocationData.flags.useSystemMemory = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (directRingAddressing != -1) {
|
|
|
|
if (directRingAddressing == 0) {
|
|
|
|
allocationData.flags.resource48Bit = false;
|
|
|
|
} else {
|
|
|
|
allocationData.flags.resource48Bit = true;
|
|
|
|
}
|
2020-01-16 00:02:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
int32_t directSemaphorePlacement = DebugManager.flags.DirectSubmissionSemaphorePlacement.get();
|
2020-03-25 21:55:23 +08:00
|
|
|
int32_t directSemaphoreAddressing = DebugManager.flags.DirectSubmissionSemaphoreAddressing.get();
|
|
|
|
if (properties.allocationType == GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER) {
|
|
|
|
if (directSemaphorePlacement != -1) {
|
|
|
|
if (directSemaphorePlacement == 0) {
|
|
|
|
allocationData.flags.requiresCpuAccess = true;
|
|
|
|
allocationData.flags.useSystemMemory = false;
|
|
|
|
} else {
|
|
|
|
allocationData.flags.requiresCpuAccess = false;
|
|
|
|
allocationData.flags.useSystemMemory = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (directSemaphoreAddressing != -1) {
|
|
|
|
if (directSemaphoreAddressing == 0) {
|
|
|
|
allocationData.flags.resource48Bit = false;
|
|
|
|
} else {
|
|
|
|
allocationData.flags.resource48Bit = true;
|
|
|
|
}
|
2020-01-16 00:02:47 +08:00
|
|
|
}
|
|
|
|
}
|
2020-10-09 17:52:00 +08:00
|
|
|
}
|
|
|
|
|
2021-03-10 07:02:59 +08:00
|
|
|
bool MemoryManager::isAllocationTypeToCapture(GraphicsAllocation::AllocationType type) const {
|
|
|
|
switch (type) {
|
|
|
|
case GraphicsAllocation::AllocationType::SCRATCH_SURFACE:
|
|
|
|
case GraphicsAllocation::AllocationType::PRIVATE_SURFACE:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-25 02:57:32 +08:00
|
|
|
bool MemoryManager::isLocalMemoryUsedForIsa(uint32_t rootDeviceIndex) {
|
2021-06-21 18:45:19 +08:00
|
|
|
std::call_once(checkIsaPlacementOnceFlags[rootDeviceIndex], [&] {
|
2021-05-25 02:57:32 +08:00
|
|
|
AllocationProperties properties = {rootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::KERNEL_ISA, 1};
|
|
|
|
AllocationData data;
|
|
|
|
getAllocationData(data, properties, nullptr, StorageInfo());
|
2021-06-21 18:45:19 +08:00
|
|
|
isaInLocalMemory[rootDeviceIndex] = !data.flags.useSystemMemory;
|
2021-05-25 02:57:32 +08:00
|
|
|
});
|
|
|
|
|
2021-06-21 18:45:19 +08:00
|
|
|
return isaInLocalMemory[rootDeviceIndex];
|
2021-05-25 02:57:32 +08:00
|
|
|
}
|
|
|
|
|
2020-10-09 17:52:00 +08:00
|
|
|
bool MemoryTransferHelper::transferMemoryToAllocation(bool useBlitter, const Device &device, GraphicsAllocation *dstAllocation, size_t dstOffset, const void *srcMemory, size_t srcSize) {
|
|
|
|
if (useBlitter) {
|
2021-05-12 21:58:40 +08:00
|
|
|
if (BlitHelperFunctions::blitMemoryToAllocation(device, dstAllocation, dstOffset, srcMemory, {srcSize, 1, 1}) == BlitOperationResult::Success) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-09 17:52:00 +08:00
|
|
|
}
|
2021-05-12 21:58:40 +08:00
|
|
|
return device.getMemoryManager()->copyMemoryToAllocation(dstAllocation, dstOffset, srcMemory, srcSize);
|
2020-10-09 17:52:00 +08:00
|
|
|
}
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|