2017-12-21 00:45:38 +01:00
|
|
|
/*
|
2024-01-12 10:41:27 +00:00
|
|
|
* Copyright (C) 2018-2024 Intel Corporation
|
2017-12-21 00:45:38 +01:00
|
|
|
*
|
2018-09-17 14:03:37 +02:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 00:45:38 +01:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2020-02-23 22:44:01 +01: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"
|
2022-12-29 12:27:52 +00:00
|
|
|
#include "shared/source/device/device.h"
|
2022-12-15 17:32:03 +00:00
|
|
|
#include "shared/source/execution_environment/execution_environment.h"
|
2020-02-23 22:44:01 +01:00
|
|
|
#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 14:31:29 +01:00
|
|
|
#include "shared/source/helpers/api_specific_config.h"
|
2022-12-01 19:42:57 +00:00
|
|
|
#include "shared/source/helpers/bindless_heaps_helper.h"
|
2024-01-15 14:47:26 +00:00
|
|
|
#include "shared/source/helpers/bit_helpers.h"
|
2023-01-18 15:52:24 +00:00
|
|
|
#include "shared/source/helpers/blit_helper.h"
|
2023-02-01 16:23:01 +00:00
|
|
|
#include "shared/source/helpers/gfx_core_helper.h"
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/helpers/hw_info.h"
|
2022-10-24 23:25:04 +00:00
|
|
|
#include "shared/source/helpers/memory_properties_helpers.h"
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/helpers/string.h"
|
2024-01-15 14:47:26 +00:00
|
|
|
#include "shared/source/helpers/string_helpers.h"
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/helpers/surface_format_info.h"
|
2023-01-11 17:04:14 +00:00
|
|
|
#include "shared/source/memory_manager/allocation_properties.h"
|
2020-09-11 15:44:09 +02:00
|
|
|
#include "shared/source/memory_manager/compression_selector.h"
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/memory_manager/deferrable_allocation_deletion.h"
|
|
|
|
|
#include "shared/source/memory_manager/deferred_deleter.h"
|
2022-12-15 16:01:37 +00:00
|
|
|
#include "shared/source/memory_manager/gfx_partition.h"
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/memory_manager/host_ptr_manager.h"
|
|
|
|
|
#include "shared/source/memory_manager/internal_allocation_storage.h"
|
2022-12-07 11:51:44 +00:00
|
|
|
#include "shared/source/memory_manager/local_memory_usage.h"
|
2023-01-13 15:18:40 +00:00
|
|
|
#include "shared/source/memory_manager/multi_graphics_allocation.h"
|
2022-08-02 16:08:54 +00:00
|
|
|
#include "shared/source/memory_manager/prefetch_manager.h"
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/os_interface/os_context.h"
|
|
|
|
|
#include "shared/source/os_interface/os_interface.h"
|
2023-03-10 12:28:11 +00:00
|
|
|
#include "shared/source/os_interface/product_helper.h"
|
2023-01-02 16:19:30 +00:00
|
|
|
#include "shared/source/page_fault_manager/cpu_page_fault_manager.h"
|
2017-12-21 00:45:38 +01:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
2024-03-29 10:17:04 +00:00
|
|
|
#include <iostream>
|
2017-12-21 00:45:38 +01:00
|
|
|
|
2019-03-26 11:59:46 +01:00
|
|
|
namespace NEO {
|
2019-12-17 08:11:16 +01:00
|
|
|
uint32_t MemoryManager::maxOsContextCount = 0u;
|
|
|
|
|
|
2019-04-15 15:20:51 +02:00
|
|
|
MemoryManager::MemoryManager(ExecutionEnvironment &executionEnvironment) : executionEnvironment(executionEnvironment), hostPtrManager(std::make_unique<HostPtrManager>()),
|
2019-07-04 12:40:58 +02:00
|
|
|
multiContextResourceDestructor(std::make_unique<DeferredDeleter>()) {
|
2020-02-18 13:29:30 +01:00
|
|
|
|
2020-02-12 08:29:26 +01:00
|
|
|
bool anyLocalMemorySupported = false;
|
2021-06-21 10:45:19 +00:00
|
|
|
const auto rootEnvCount = executionEnvironment.rootDeviceEnvironments.size();
|
2020-02-12 08:29:26 +01:00
|
|
|
|
2021-06-21 10:45:19 +00:00
|
|
|
defaultEngineIndex.resize(rootEnvCount);
|
|
|
|
|
checkIsaPlacementOnceFlags = std::make_unique<std::once_flag[]>(rootEnvCount);
|
|
|
|
|
isaInLocalMemory.resize(rootEnvCount);
|
2023-04-27 15:42:17 +00:00
|
|
|
allRegisteredEngines.resize(rootEnvCount + 1);
|
2023-11-15 09:01:43 +00:00
|
|
|
secondaryEngines.resize(rootEnvCount + 1);
|
2021-06-21 10:45:19 +00:00
|
|
|
|
|
|
|
|
for (uint32_t rootDeviceIndex = 0; rootDeviceIndex < rootEnvCount; ++rootDeviceIndex) {
|
2022-12-12 12:39:04 +00:00
|
|
|
auto &rootDeviceEnvironment = *executionEnvironment.rootDeviceEnvironments[rootDeviceIndex];
|
|
|
|
|
auto hwInfo = rootDeviceEnvironment.getHardwareInfo();
|
|
|
|
|
auto &gfxCoreHelper = rootDeviceEnvironment.getHelper<GfxCoreHelper>();
|
2023-10-26 15:04:39 +02:00
|
|
|
internalLocalMemoryUsageBankSelector.emplace_back(new LocalMemoryUsageBankSelector(GfxCoreHelper::getSubDevicesCount(hwInfo)));
|
|
|
|
|
externalLocalMemoryUsageBankSelector.emplace_back(new LocalMemoryUsageBankSelector(GfxCoreHelper::getSubDevicesCount(hwInfo)));
|
2022-12-12 12:39:04 +00:00
|
|
|
this->localMemorySupported.push_back(gfxCoreHelper.getEnableLocalMemory(*hwInfo));
|
2023-11-30 08:32:25 +00:00
|
|
|
this->enable64kbpages.push_back(OSInterface::osEnabled64kbPages && hwInfo->capabilityTable.ftr64KBpages && !!debugManager.flags.Enable64kbpages.get());
|
2020-02-12 08:29:26 +01:00
|
|
|
|
2020-08-12 12:18:34 +02:00
|
|
|
gfxPartitions.push_back(std::make_unique<GfxPartition>(reservedCpuAddressRange));
|
2020-02-12 08:29:26 +01:00
|
|
|
|
|
|
|
|
anyLocalMemorySupported |= this->localMemorySupported[rootDeviceIndex];
|
2023-10-19 14:24:35 +00:00
|
|
|
|
2024-03-20 16:58:17 +00:00
|
|
|
auto globalHeap = ApiSpecificConfig::getGlobalBindlessHeapConfiguration(rootDeviceEnvironment.getReleaseHelper());
|
2023-10-19 14:24:35 +00:00
|
|
|
heapAssigners.push_back(std::make_unique<HeapAssigner>(globalHeap));
|
2019-10-22 10:26:23 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-12 08:29:26 +01:00
|
|
|
if (anyLocalMemorySupported) {
|
2019-07-04 12:17:42 +02:00
|
|
|
pageFaultManager = PageFaultManager::create();
|
2022-08-02 16:08:54 +00:00
|
|
|
prefetchManager = PrefetchManager::create();
|
2019-07-04 12:17:42 +02:00
|
|
|
}
|
2020-06-06 13:13:22 +02:00
|
|
|
|
2023-11-30 08:32:25 +00:00
|
|
|
if (debugManager.flags.EnableMultiStorageResources.get() != -1) {
|
|
|
|
|
supportsMultiStorageResources = !!debugManager.flags.EnableMultiStorageResources.get();
|
2020-06-06 13:13:22 +02:00
|
|
|
}
|
2019-03-15 10:22:35 +01:00
|
|
|
}
|
2018-09-06 10:53:35 +02:00
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
MemoryManager::~MemoryManager() {
|
2023-11-15 09:01:43 +00:00
|
|
|
for (auto &engineContainer : secondaryEngines) {
|
|
|
|
|
for (auto &engine : engineContainer) {
|
2024-04-05 15:51:02 +00:00
|
|
|
DEBUG_BREAK_IF(true);
|
2023-11-15 09:01:43 +00:00
|
|
|
engine.osContext->decRefInternal();
|
|
|
|
|
}
|
|
|
|
|
engineContainer.clear();
|
|
|
|
|
}
|
|
|
|
|
secondaryEngines.clear();
|
|
|
|
|
|
2023-04-26 10:36:25 +00:00
|
|
|
for (auto &engineContainer : allRegisteredEngines) {
|
2023-04-27 15:42:17 +00:00
|
|
|
for (auto &engine : engineContainer) {
|
2023-04-26 10:36:25 +00:00
|
|
|
engine.osContext->decRefInternal();
|
|
|
|
|
}
|
2023-04-27 15:42:17 +00:00
|
|
|
engineContainer.clear();
|
2018-09-06 09:01:13 +02:00
|
|
|
}
|
2023-04-26 10:36:25 +00:00
|
|
|
allRegisteredEngines.clear();
|
2019-08-27 15:26:30 +02:00
|
|
|
if (reservedMemory) {
|
2019-08-29 09:52:06 +02:00
|
|
|
MemoryManager::alignedFreeWrapper(reservedMemory);
|
2019-08-27 15:26:30 +02:00
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-07 11:51:44 +00:00
|
|
|
bool MemoryManager::isLimitedGPU(uint32_t rootDeviceIndex) {
|
|
|
|
|
return peek32bit() && !peekExecutionEnvironment().rootDeviceEnvironments[rootDeviceIndex]->isFullRangeSvm();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MemoryManager::isLimitedGPUOnType(uint32_t rootDeviceIndex, AllocationType type) {
|
|
|
|
|
return isLimitedGPU(rootDeviceIndex) &&
|
2023-12-11 14:24:36 +00:00
|
|
|
(type != AllocationType::mapAllocation) &&
|
|
|
|
|
(type != AllocationType::image);
|
2022-12-07 11:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-15 16:01:37 +00:00
|
|
|
void *MemoryManager::alignedMallocWrapper(size_t bytes, size_t alignment) {
|
|
|
|
|
return ::alignedMalloc(bytes, alignment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MemoryManager::alignedFreeWrapper(void *ptr) {
|
|
|
|
|
::alignedFree(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 11:51:44 +00:00
|
|
|
GmmHelper *MemoryManager::getGmmHelper(uint32_t rootDeviceIndex) {
|
|
|
|
|
return executionEnvironment.rootDeviceEnvironments[rootDeviceIndex]->getGmmHelper();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 16:01:37 +00:00
|
|
|
HeapIndex MemoryManager::selectInternalHeap(bool useLocalMemory) {
|
2023-12-12 08:58:42 +00:00
|
|
|
return useLocalMemory ? HeapIndex::heapInternalDeviceMemory : HeapIndex::heapInternal;
|
2022-12-15 16:01:37 +00:00
|
|
|
}
|
|
|
|
|
HeapIndex MemoryManager::selectExternalHeap(bool useLocalMemory) {
|
2023-12-12 08:58:42 +00:00
|
|
|
return useLocalMemory ? HeapIndex::heapExternalDeviceMemory : HeapIndex::heapExternal;
|
2022-12-15 16:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-07 11:51:44 +00:00
|
|
|
void MemoryManager::zeroCpuMemoryIfRequested(const AllocationData &allocationData, void *cpuPtr, size_t size) {
|
|
|
|
|
if (allocationData.flags.zeroMemory) {
|
|
|
|
|
memset(cpuPtr, 0, size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
void *MemoryManager::allocateSystemMemory(size_t size, size_t alignment) {
|
|
|
|
|
// Establish a minimum alignment of 16bytes.
|
2018-01-22 16:43:26 +01:00
|
|
|
constexpr size_t minAlignment = 16;
|
|
|
|
|
alignment = std::max(alignment, minAlignment);
|
|
|
|
|
auto restrictions = getAlignedMallocRestrictions();
|
2019-05-18 13:44:46 -07:00
|
|
|
void *ptr = alignedMallocWrapper(size, alignment);
|
2018-01-22 16:43:26 +01:00
|
|
|
|
2019-05-18 13:44:46 -07:00
|
|
|
if (restrictions == nullptr || restrictions->minAddress == 0) {
|
2018-01-22 16:43:26 +01:00
|
|
|
return ptr;
|
2019-05-18 13:44:46 -07: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 16:43:26 +01:00
|
|
|
}
|
2019-05-18 13:44:46 -07:00
|
|
|
} while (1);
|
|
|
|
|
for (auto &it : invalidMemVector) {
|
|
|
|
|
alignedFreeWrapper(it);
|
2018-01-22 16:43:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ptr;
|
2017-12-21 00:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-30 11:01:33 +01:00
|
|
|
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryWithHostPtr(const AllocationData &allocationData) {
|
2017-12-21 00:45:38 +01:00
|
|
|
if (deferredDeleter) {
|
|
|
|
|
deferredDeleter->drain(true);
|
|
|
|
|
}
|
2018-10-23 11:42:40 +02:00
|
|
|
GraphicsAllocation *graphicsAllocation = nullptr;
|
2019-11-15 09:59:48 +01:00
|
|
|
auto osStorage = hostPtrManager->prepareOsStorageForAllocation(*this, allocationData.size, allocationData.hostPtr, allocationData.rootDeviceIndex);
|
2018-10-23 11:42:40 +02:00
|
|
|
if (osStorage.fragmentCount > 0) {
|
2019-02-25 14:11:34 +01:00
|
|
|
graphicsAllocation = createGraphicsAllocation(osStorage, allocationData);
|
2021-02-16 22:58:32 +00:00
|
|
|
if (graphicsAllocation == nullptr) {
|
|
|
|
|
hostPtrManager->releaseHandleStorage(allocationData.rootDeviceIndex, osStorage);
|
|
|
|
|
cleanOsHandles(osStorage, allocationData.rootDeviceIndex);
|
|
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
}
|
|
|
|
|
return graphicsAllocation;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-22 12:40:17 +01:00
|
|
|
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryForImageFromHostPtr(const AllocationData &allocationData) {
|
2020-01-13 09:40:03 +01:00
|
|
|
bool copyRequired = isCopyRequired(*allocationData.imgInfo, allocationData.hostPtr);
|
2018-12-14 11:24:45 +01:00
|
|
|
|
2021-07-01 16:00:22 +00:00
|
|
|
if (allocationData.hostPtr && !copyRequired) {
|
2019-01-22 12:40:17 +01:00
|
|
|
return allocateGraphicsMemoryWithHostPtr(allocationData);
|
2018-12-14 11:24:45 +01:00
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
void MemoryManager::cleanGraphicsMemoryCreatedFromHostPtr(GraphicsAllocation *graphicsAllocation) {
|
2020-07-07 08:41:26 +02:00
|
|
|
hostPtrManager->releaseHandleStorage(graphicsAllocation->getRootDeviceIndex(), graphicsAllocation->fragmentsStorage);
|
2019-11-15 09:59:48 +01:00
|
|
|
cleanOsHandles(graphicsAllocation->fragmentsStorage, graphicsAllocation->getRootDeviceIndex());
|
2017-12-21 00:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-07 13:09:40 +00:00
|
|
|
void *MemoryManager::createMultiGraphicsAllocationInSystemMemoryPool(RootDeviceIndicesContainer &rootDeviceIndices, AllocationProperties &properties, MultiGraphicsAllocation &multiGraphicsAllocation, void *ptr) {
|
2021-05-13 10:28:30 +00:00
|
|
|
properties.flags.forceSystemMemory = true;
|
2020-08-21 13:10:59 +02:00
|
|
|
for (auto &rootDeviceIndex : rootDeviceIndices) {
|
2021-05-13 10:28:30 +00:00
|
|
|
if (multiGraphicsAllocation.getGraphicsAllocation(rootDeviceIndex)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-08-21 13:10:59 +02:00
|
|
|
properties.rootDeviceIndex = rootDeviceIndex;
|
2021-03-16 11:38:53 +01:00
|
|
|
properties.flags.isUSMHostAllocation = true;
|
|
|
|
|
|
|
|
|
|
if (isLimitedRange(properties.rootDeviceIndex)) {
|
|
|
|
|
properties.flags.isUSMHostAllocation = false;
|
|
|
|
|
DEBUG_BREAK_IF(rootDeviceIndices.size() > 1);
|
|
|
|
|
}
|
2020-08-21 13:10:59 +02:00
|
|
|
|
|
|
|
|
if (!ptr) {
|
|
|
|
|
auto graphicsAllocation = allocateGraphicsMemoryWithProperties(properties);
|
|
|
|
|
if (!graphicsAllocation) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
multiGraphicsAllocation.addAllocation(graphicsAllocation);
|
2021-02-25 09:38:48 +01:00
|
|
|
ptr = reinterpret_cast<void *>(graphicsAllocation->getUnderlyingBuffer());
|
2020-08-21 13:10:59 +02:00
|
|
|
} else {
|
|
|
|
|
properties.flags.allocateMemory = false;
|
|
|
|
|
|
2020-10-21 10:50:53 +02:00
|
|
|
auto graphicsAllocation = createGraphicsAllocationFromExistingStorage(properties, ptr, multiGraphicsAllocation);
|
|
|
|
|
|
2020-08-21 13:10:59 +02:00
|
|
|
if (!graphicsAllocation) {
|
2023-10-03 15:02:52 +00:00
|
|
|
for (auto &gpuAllocation : multiGraphicsAllocation.getGraphicsAllocations()) {
|
2020-08-21 13:10:59 +02:00
|
|
|
freeGraphicsMemory(gpuAllocation);
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
multiGraphicsAllocation.addAllocation(graphicsAllocation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 10:50:53 +02:00
|
|
|
GraphicsAllocation *MemoryManager::createGraphicsAllocationFromExistingStorage(AllocationProperties &properties, void *ptr, MultiGraphicsAllocation &multiGraphicsAllocation) {
|
|
|
|
|
return allocateGraphicsMemoryWithProperties(properties, ptr);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
void MemoryManager::freeSystemMemory(void *ptr) {
|
|
|
|
|
::alignedFree(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MemoryManager::freeGraphicsMemory(GraphicsAllocation *gfxAllocation) {
|
2022-03-17 15:54:48 +00:00
|
|
|
freeGraphicsMemory(gfxAllocation, false);
|
2022-03-02 03:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MemoryManager::freeGraphicsMemory(GraphicsAllocation *gfxAllocation, bool isImportedAllocation) {
|
2019-01-24 11:51:33 +01:00
|
|
|
if (!gfxAllocation) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-09-25 10:10:46 +00:00
|
|
|
|
2024-03-19 17:00:21 +00:00
|
|
|
if (executionEnvironment.rootDeviceEnvironments.size() > 0 && executionEnvironment.rootDeviceEnvironments[gfxAllocation->getRootDeviceIndex()]->getBindlessHeapsHelper() != nullptr) {
|
2023-08-25 14:28:27 +00:00
|
|
|
executionEnvironment.rootDeviceEnvironments[gfxAllocation->getRootDeviceIndex()]->getBindlessHeapsHelper()->releaseSSToReusePool(gfxAllocation->getBindlessInfo());
|
2021-01-27 14:31:29 +01:00
|
|
|
}
|
2019-03-04 14:50:26 +01: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 10:19:02 +01:00
|
|
|
freeAssociatedResourceImpl(*gfxAllocation);
|
2019-01-24 15:16:12 +01:00
|
|
|
}
|
2019-05-10 11:30:07 +02:00
|
|
|
|
2021-07-22 06:36:45 +00:00
|
|
|
getLocalMemoryUsageBankSelector(gfxAllocation->getAllocationType(), gfxAllocation->getRootDeviceIndex())->freeOnBanks(gfxAllocation->storageInfo.getMemoryBanks(), gfxAllocation->getUnderlyingBufferSize());
|
2022-03-02 03:43:59 +00:00
|
|
|
freeGraphicsMemoryImpl(gfxAllocation, isImportedAllocation);
|
2017-12-21 00:45:38 +01:00
|
|
|
}
|
2022-03-17 15:54:48 +00:00
|
|
|
|
2022-11-10 12:41:52 +00:00
|
|
|
// if not in use destroy in place
|
|
|
|
|
// if in use pass to temporary allocation list that is cleaned on blocking calls
|
2018-01-19 10:55:36 +01:00
|
|
|
void MemoryManager::checkGpuUsageAndDestroyGraphicsAllocations(GraphicsAllocation *gfxAllocation) {
|
2019-01-03 14:48:24 +01:00
|
|
|
if (gfxAllocation->isUsed()) {
|
|
|
|
|
if (gfxAllocation->isUsedByManyOsContexts()) {
|
|
|
|
|
multiContextResourceDestructor->deferDeletion(new DeferrableAllocationDeletion{*this, *gfxAllocation});
|
2023-03-20 14:34:46 +00:00
|
|
|
multiContextResourceDestructor->drain(false);
|
2019-01-03 14:48:24 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2023-04-26 10:36:25 +00:00
|
|
|
for (auto &engine : getRegisteredEngines(gfxAllocation->getRootDeviceIndex())) {
|
2019-02-18 13:59:16 +01:00
|
|
|
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),
|
2022-07-13 15:05:42 +00:00
|
|
|
DEFERRED_DEALLOCATION);
|
2019-02-18 13:59:16 +01:00
|
|
|
return;
|
2018-12-10 13:17:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-01-19 10:55:36 +01:00
|
|
|
}
|
2018-12-10 13:17:52 +01:00
|
|
|
freeGraphicsMemory(gfxAllocation);
|
2018-01-19 10:55:36 +01:00
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
|
2022-12-15 16:01:37 +00:00
|
|
|
uint64_t MemoryManager::getInternalHeapBaseAddress(uint32_t rootDeviceIndex, bool useLocalMemory) {
|
|
|
|
|
return getGfxPartition(rootDeviceIndex)->getHeapBase(selectInternalHeap(useLocalMemory));
|
|
|
|
|
}
|
|
|
|
|
uint64_t MemoryManager::getExternalHeapBaseAddress(uint32_t rootDeviceIndex, bool useLocalMemory) {
|
|
|
|
|
return getGfxPartition(rootDeviceIndex)->getHeapBase(selectExternalHeap(useLocalMemory));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MemoryManager::isLimitedRange(uint32_t rootDeviceIndex) {
|
|
|
|
|
return getGfxPartition(rootDeviceIndex)->isLimitedRange();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
void MemoryManager::waitForDeletions() {
|
|
|
|
|
if (deferredDeleter) {
|
|
|
|
|
deferredDeleter->drain(false);
|
|
|
|
|
}
|
|
|
|
|
deferredDeleter.reset(nullptr);
|
|
|
|
|
}
|
|
|
|
|
bool MemoryManager::isAsyncDeleterEnabled() const {
|
|
|
|
|
return asyncDeleterEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 08:29:26 +01:00
|
|
|
bool MemoryManager::isLocalMemorySupported(uint32_t rootDeviceIndex) const {
|
|
|
|
|
return localMemorySupported[rootDeviceIndex];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MemoryManager::peek64kbPagesEnabled(uint32_t rootDeviceIndex) const {
|
|
|
|
|
return enable64kbpages[rootDeviceIndex];
|
2018-10-12 15:20:02 +02:00
|
|
|
}
|
|
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
bool MemoryManager::isMemoryBudgetExhausted() const {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-07 07:26:03 +00:00
|
|
|
void MemoryManager::updateLatestContextIdForRootDevice(uint32_t rootDeviceIndex) {
|
|
|
|
|
// rootDeviceIndexToContextId map would contain the first entry for context for each rootDevice
|
|
|
|
|
auto entry = rootDeviceIndexToContextId.insert(std::pair<uint32_t, uint32_t>(rootDeviceIndex, latestContextId));
|
|
|
|
|
if (entry.second == false) {
|
|
|
|
|
if (latestContextId == std::numeric_limits<uint32_t>::max()) {
|
|
|
|
|
// If we are here, it means we are reinitializing the contextId.
|
|
|
|
|
latestContextId = entry.first->second;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 18:50:32 +00:00
|
|
|
OsContext *MemoryManager::createAndRegisterOsContext(CommandStreamReceiver *commandStreamReceiver,
|
2021-08-11 17:36:00 +00:00
|
|
|
const EngineDescriptor &engineDescriptor) {
|
2021-10-07 07:26:03 +00:00
|
|
|
auto rootDeviceIndex = commandStreamReceiver->getRootDeviceIndex();
|
|
|
|
|
updateLatestContextIdForRootDevice(rootDeviceIndex);
|
|
|
|
|
|
2018-11-21 09:57:51 +01:00
|
|
|
auto contextId = ++latestContextId;
|
2022-11-16 19:31:25 +00:00
|
|
|
auto osContext = OsContext::create(peekExecutionEnvironment().rootDeviceEnvironments[rootDeviceIndex]->osInterface.get(), rootDeviceIndex, contextId, engineDescriptor);
|
2018-11-21 09:57:51 +01:00
|
|
|
osContext->incRefInternal();
|
2019-02-18 13:59:16 +01:00
|
|
|
|
2022-11-16 19:31:25 +00:00
|
|
|
UNRECOVERABLE_IF(rootDeviceIndex != osContext->getRootDeviceIndex());
|
|
|
|
|
|
2023-04-26 10:36:25 +00:00
|
|
|
allRegisteredEngines[rootDeviceIndex].emplace_back(commandStreamReceiver, osContext);
|
2018-11-21 09:57:51 +01:00
|
|
|
|
|
|
|
|
return osContext;
|
2018-09-06 09:01:13 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-15 09:01:43 +00:00
|
|
|
OsContext *MemoryManager::createAndRegisterSecondaryOsContext(const OsContext *primaryContext, CommandStreamReceiver *commandStreamReceiver,
|
|
|
|
|
const EngineDescriptor &engineDescriptor) {
|
|
|
|
|
auto rootDeviceIndex = commandStreamReceiver->getRootDeviceIndex();
|
|
|
|
|
|
|
|
|
|
updateLatestContextIdForRootDevice(rootDeviceIndex);
|
|
|
|
|
|
2024-05-20 11:34:33 +00:00
|
|
|
auto contextId = ++latestContextId;
|
2023-11-15 09:01:43 +00:00
|
|
|
auto osContext = OsContext::create(peekExecutionEnvironment().rootDeviceEnvironments[rootDeviceIndex]->osInterface.get(), rootDeviceIndex, contextId, engineDescriptor);
|
|
|
|
|
osContext->incRefInternal();
|
|
|
|
|
|
|
|
|
|
osContext->setPrimaryContext(primaryContext);
|
|
|
|
|
|
|
|
|
|
UNRECOVERABLE_IF(rootDeviceIndex != osContext->getRootDeviceIndex());
|
|
|
|
|
|
|
|
|
|
secondaryEngines[rootDeviceIndex].emplace_back(commandStreamReceiver, osContext);
|
2024-05-20 11:34:33 +00:00
|
|
|
allRegisteredEngines[rootDeviceIndex].emplace_back(commandStreamReceiver, osContext);
|
2023-11-15 09:01:43 +00:00
|
|
|
|
|
|
|
|
return osContext;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-05 15:51:02 +00:00
|
|
|
void MemoryManager::releaseSecondaryOsContexts(uint32_t rootDeviceIndex) {
|
|
|
|
|
|
|
|
|
|
auto &engineContainer = secondaryEngines[rootDeviceIndex];
|
|
|
|
|
|
|
|
|
|
for (auto &engine : engineContainer) {
|
|
|
|
|
engine.osContext->decRefInternal();
|
|
|
|
|
}
|
|
|
|
|
engineContainer.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 11:30:07 +02:00
|
|
|
bool MemoryManager::getAllocationData(AllocationData &allocationData, const AllocationProperties &properties, const void *hostPtr, const StorageInfo &storageInfo) {
|
2018-12-06 15:03:06 +01:00
|
|
|
UNRECOVERABLE_IF(hostPtr == nullptr && !properties.flags.allocateMemory);
|
2023-12-11 14:24:36 +00:00
|
|
|
UNRECOVERABLE_IF(properties.allocationType == AllocationType::unknown);
|
2018-07-09 14:12:32 +02:00
|
|
|
|
2023-01-04 09:45:07 +00:00
|
|
|
auto &rootDeviceEnvironment = *executionEnvironment.rootDeviceEnvironments[properties.rootDeviceIndex];
|
|
|
|
|
auto &hwInfo = *rootDeviceEnvironment.getHardwareInfo();
|
|
|
|
|
auto &helper = rootDeviceEnvironment.getHelper<GfxCoreHelper>();
|
2024-03-07 15:53:20 +00:00
|
|
|
auto &productHelper = rootDeviceEnvironment.getProductHelper();
|
2020-09-21 18:08:07 +02:00
|
|
|
|
2024-04-11 12:23:06 +00:00
|
|
|
if (storageInfo.getMemoryBanks() == 0) {
|
|
|
|
|
allocationData.flags.useSystemMemory = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-09 14:12:32 +02:00
|
|
|
bool allow64KbPages = false;
|
|
|
|
|
bool allow32Bit = false;
|
2018-12-06 15:03:06 +01:00
|
|
|
bool forcePin = properties.flags.forcePin;
|
2019-04-03 11:22:04 +02:00
|
|
|
bool mayRequireL3Flush = false;
|
2018-07-09 14:12:32 +02:00
|
|
|
|
2018-12-11 18:56:37 +01:00
|
|
|
switch (properties.allocationType) {
|
2023-12-11 14:24:36 +00:00
|
|
|
case AllocationType::buffer:
|
|
|
|
|
case AllocationType::bufferHostMemory:
|
|
|
|
|
case AllocationType::constantSurface:
|
|
|
|
|
case AllocationType::globalSurface:
|
|
|
|
|
case AllocationType::pipe:
|
|
|
|
|
case AllocationType::printfSurface:
|
|
|
|
|
case AllocationType::privateSurface:
|
|
|
|
|
case AllocationType::scratchSurface:
|
|
|
|
|
case AllocationType::workPartitionSurface:
|
|
|
|
|
case AllocationType::writeCombined:
|
|
|
|
|
case AllocationType::assertBuffer:
|
2018-07-09 14:12:32 +02:00
|
|
|
allow64KbPages = true;
|
|
|
|
|
allow32Bit = true;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 10:40:11 +01:00
|
|
|
switch (properties.allocationType) {
|
2023-12-11 14:24:36 +00:00
|
|
|
case AllocationType::svmGpu:
|
|
|
|
|
case AllocationType::svmZeroCopy:
|
|
|
|
|
case AllocationType::gpuTimestampDeviceBuffer:
|
|
|
|
|
case AllocationType::preemption:
|
2024-03-26 10:02:52 +00:00
|
|
|
case AllocationType::syncDispatchToken:
|
2018-12-31 10:40:11 +01:00
|
|
|
allow64KbPages = true;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 18:56:37 +01:00
|
|
|
switch (properties.allocationType) {
|
2023-12-11 14:24:36 +00:00
|
|
|
case AllocationType::buffer:
|
|
|
|
|
case AllocationType::bufferHostMemory:
|
|
|
|
|
case AllocationType::writeCombined:
|
2018-07-24 18:36:26 +02:00
|
|
|
forcePin = true;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-03 11:22:04 +02:00
|
|
|
switch (properties.allocationType) {
|
2023-12-11 14:24:36 +00:00
|
|
|
case AllocationType::buffer:
|
|
|
|
|
case AllocationType::bufferHostMemory:
|
|
|
|
|
case AllocationType::externalHostPtr:
|
|
|
|
|
case AllocationType::globalSurface:
|
|
|
|
|
case AllocationType::image:
|
|
|
|
|
case AllocationType::mapAllocation:
|
|
|
|
|
case AllocationType::pipe:
|
|
|
|
|
case AllocationType::sharedBuffer:
|
|
|
|
|
case AllocationType::sharedImage:
|
|
|
|
|
case AllocationType::sharedResourceCopy:
|
|
|
|
|
case AllocationType::svmCpu:
|
|
|
|
|
case AllocationType::svmGpu:
|
|
|
|
|
case AllocationType::svmZeroCopy:
|
|
|
|
|
case AllocationType::writeCombined:
|
2019-04-03 11:22:04 +02:00
|
|
|
mayRequireL3Flush = true;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 18:56:37 +01:00
|
|
|
switch (properties.allocationType) {
|
2023-12-11 14:24:36 +00:00
|
|
|
case AllocationType::commandBuffer:
|
|
|
|
|
case AllocationType::ringBuffer:
|
|
|
|
|
case AllocationType::semaphoreBuffer:
|
|
|
|
|
case AllocationType::bufferHostMemory:
|
|
|
|
|
case AllocationType::externalHostPtr:
|
|
|
|
|
case AllocationType::fillPattern:
|
|
|
|
|
case AllocationType::mapAllocation:
|
|
|
|
|
case AllocationType::mcs:
|
|
|
|
|
case AllocationType::profilingTagBuffer:
|
|
|
|
|
case AllocationType::svmCpu:
|
|
|
|
|
case AllocationType::svmZeroCopy:
|
|
|
|
|
case AllocationType::tagBuffer:
|
|
|
|
|
case AllocationType::globalFence:
|
|
|
|
|
case AllocationType::internalHostMemory:
|
|
|
|
|
case AllocationType::debugContextSaveArea:
|
|
|
|
|
case AllocationType::debugSbaTrackingBuffer:
|
|
|
|
|
case AllocationType::swTagBuffer:
|
2018-12-11 18:56:37 +01:00
|
|
|
allocationData.flags.useSystemMemory = true;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 19:53:30 +01:00
|
|
|
if (GraphicsAllocation::isIsaAllocationType(properties.allocationType)) {
|
2022-11-10 12:41:52 +00:00
|
|
|
allocationData.flags.useSystemMemory = helper.useSystemMemoryPlacementForISA(hwInfo);
|
2020-09-21 18:08:07 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-06 16:58:43 +01:00
|
|
|
switch (properties.allocationType) {
|
2023-12-11 14:24:36 +00:00
|
|
|
case AllocationType::commandBuffer:
|
2023-12-28 14:18:34 +00:00
|
|
|
case AllocationType::ringBuffer:
|
|
|
|
|
allocationData.flags.resource48Bit = helper.is48ResourceNeededForCmdBuffer();
|
|
|
|
|
break;
|
|
|
|
|
case AllocationType::deferredTasksList:
|
2023-12-11 14:24:36 +00:00
|
|
|
case AllocationType::image:
|
|
|
|
|
case AllocationType::indirectObjectHeap:
|
|
|
|
|
case AllocationType::instructionHeap:
|
|
|
|
|
case AllocationType::internalHeap:
|
|
|
|
|
case AllocationType::kernelIsa:
|
|
|
|
|
case AllocationType::kernelIsaInternal:
|
|
|
|
|
case AllocationType::linearStream:
|
|
|
|
|
case AllocationType::mcs:
|
|
|
|
|
case AllocationType::preemption:
|
|
|
|
|
case AllocationType::scratchSurface:
|
|
|
|
|
case AllocationType::workPartitionSurface:
|
|
|
|
|
case AllocationType::sharedImage:
|
|
|
|
|
case AllocationType::sharedResourceCopy:
|
|
|
|
|
case AllocationType::surfaceStateHeap:
|
|
|
|
|
case AllocationType::timestampPacketTagBuffer:
|
|
|
|
|
case AllocationType::debugModuleArea:
|
|
|
|
|
case AllocationType::gpuTimestampDeviceBuffer:
|
|
|
|
|
case AllocationType::semaphoreBuffer:
|
2024-03-26 10:02:52 +00:00
|
|
|
case AllocationType::syncDispatchToken:
|
2020-03-06 16:58:43 +01:00
|
|
|
allocationData.flags.resource48Bit = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
allocationData.flags.resource48Bit = properties.flags.resource48Bit;
|
|
|
|
|
}
|
2022-12-14 15:14:49 +01:00
|
|
|
allocationData.forceKMDAllocation = properties.forceKMDAllocation;
|
|
|
|
|
allocationData.makeGPUVaDifferentThanCPUPtr = properties.makeGPUVaDifferentThanCPUPtr;
|
2019-11-14 04:08:59 -05:00
|
|
|
allocationData.flags.shareable = properties.flags.shareable;
|
2021-08-11 10:36:04 +00:00
|
|
|
allocationData.flags.isUSMDeviceMemory = properties.flags.isUSMDeviceAllocation;
|
2019-02-18 10:49:21 +01:00
|
|
|
allocationData.flags.requiresCpuAccess = GraphicsAllocation::isCpuAccessRequired(properties.allocationType);
|
2018-12-06 15:03:06 +01:00
|
|
|
allocationData.flags.allocateMemory = properties.flags.allocateMemory;
|
2018-07-09 14:12:32 +02:00
|
|
|
allocationData.flags.allow32Bit = allow32Bit;
|
|
|
|
|
allocationData.flags.allow64kbPages = allow64KbPages;
|
|
|
|
|
allocationData.flags.forcePin = forcePin;
|
2019-04-03 19:16:55 +02:00
|
|
|
allocationData.flags.uncacheable = properties.flags.uncacheable;
|
2019-04-03 11:22:04 +02:00
|
|
|
allocationData.flags.flushL3 =
|
|
|
|
|
(mayRequireL3Flush ? properties.flags.flushL3RequiredForRead | properties.flags.flushL3RequiredForWrite : 0u);
|
2021-12-03 13:52:16 +00:00
|
|
|
allocationData.flags.preferCompressed = properties.flags.preferCompressed;
|
2023-01-04 09:45:07 +00:00
|
|
|
allocationData.flags.preferCompressed |= CompressionSelector::preferCompressedAllocation(properties);
|
2019-04-03 19:16:55 +02:00
|
|
|
allocationData.flags.multiOsContextCapable = properties.flags.multiOsContextCapable;
|
2022-01-31 10:12:51 +00:00
|
|
|
allocationData.usmInitialPlacement = properties.usmInitialPlacement;
|
2020-10-21 16:29:25 +02:00
|
|
|
|
2023-02-08 17:05:30 +00:00
|
|
|
if (GraphicsAllocation::isDebugSurfaceAllocationType(properties.allocationType) ||
|
|
|
|
|
GraphicsAllocation::isConstantOrGlobalSurfaceAllocationType(properties.allocationType)) {
|
2021-09-21 18:17:41 +00:00
|
|
|
allocationData.flags.zeroMemory = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 14:24:36 +00:00
|
|
|
if (properties.allocationType == AllocationType::debugModuleArea) {
|
2020-10-21 16:29:25 +02:00
|
|
|
allocationData.flags.use32BitFrontWindow = true;
|
|
|
|
|
} else {
|
|
|
|
|
allocationData.flags.use32BitFrontWindow = properties.flags.use32BitFrontWindow;
|
|
|
|
|
}
|
2018-07-09 14:12:32 +02:00
|
|
|
|
|
|
|
|
allocationData.hostPtr = hostPtr;
|
2023-04-24 11:58:49 +00:00
|
|
|
if (GraphicsAllocation::isKernelIsaAllocationType(properties.allocationType)) {
|
2022-11-10 12:41:52 +00:00
|
|
|
allocationData.size = properties.size + helper.getPaddingForISAAllocation();
|
2021-04-23 14:09:03 +00:00
|
|
|
} else {
|
|
|
|
|
allocationData.size = properties.size;
|
|
|
|
|
}
|
2018-12-11 18:56:37 +01:00
|
|
|
allocationData.type = properties.allocationType;
|
2019-05-10 11:30:07 +02:00
|
|
|
allocationData.storageInfo = storageInfo;
|
2018-12-06 15:03:06 +01:00
|
|
|
allocationData.alignment = properties.alignment ? properties.alignment : MemoryConstants::preferredAlignment;
|
2018-12-20 16:58:15 +01:00
|
|
|
allocationData.imgInfo = properties.imgInfo;
|
2018-07-09 14:12:32 +02:00
|
|
|
|
|
|
|
|
if (allocationData.flags.allocateMemory) {
|
|
|
|
|
allocationData.hostPtr = nullptr;
|
|
|
|
|
}
|
2019-10-22 10:26:23 +02:00
|
|
|
|
2020-07-01 10:38:19 +02:00
|
|
|
allocationData.gpuAddress = properties.gpuAddress;
|
|
|
|
|
allocationData.osContext = properties.osContext;
|
2019-11-07 14:15:04 +01:00
|
|
|
allocationData.rootDeviceIndex = properties.rootDeviceIndex;
|
2020-10-15 08:53:36 +02:00
|
|
|
allocationData.useMmapObject = properties.useMmapObject;
|
2019-11-07 14:15:04 +01:00
|
|
|
|
2023-01-23 17:58:31 +00:00
|
|
|
helper.setExtraAllocationData(allocationData, properties, rootDeviceEnvironment);
|
2021-05-13 17:42:08 +00:00
|
|
|
allocationData.flags.useSystemMemory |= properties.flags.forceSystemMemory;
|
2020-06-02 13:38:02 +02:00
|
|
|
|
2021-05-24 18:57:32 +00:00
|
|
|
overrideAllocationData(allocationData, properties);
|
|
|
|
|
allocationData.flags.isUSMHostAllocation = properties.flags.isUSMHostAllocation;
|
2022-02-09 13:14:31 +00:00
|
|
|
|
|
|
|
|
allocationData.storageInfo.systemMemoryPlacement = allocationData.flags.useSystemMemory;
|
2023-02-22 07:29:42 +00:00
|
|
|
allocationData.storageInfo.systemMemoryForced = properties.flags.forceSystemMemory;
|
2023-08-30 09:38:37 +00:00
|
|
|
allocationData.allocationMethod = getPreferredAllocationMethod(properties);
|
2022-02-09 13:14:31 +00:00
|
|
|
|
2024-03-07 15:53:20 +00:00
|
|
|
bool useLocalPreferredForCacheableBuffers = productHelper.useLocalPreferredForCacheableBuffers();
|
|
|
|
|
if (debugManager.flags.UseLocalPreferredForCacheableBuffers.get() != -1) {
|
|
|
|
|
useLocalPreferredForCacheableBuffers = debugManager.flags.UseLocalPreferredForCacheableBuffers.get() == 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (properties.allocationType) {
|
|
|
|
|
case AllocationType::buffer:
|
|
|
|
|
case AllocationType::svmGpu:
|
|
|
|
|
case AllocationType::image:
|
|
|
|
|
if (false == allocationData.flags.uncacheable && useLocalPreferredForCacheableBuffers) {
|
|
|
|
|
allocationData.storageInfo.localOnlyRequired = false;
|
|
|
|
|
allocationData.storageInfo.systemMemoryPlacement = false;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-09 14:12:32 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 09:38:37 +00:00
|
|
|
GfxMemoryAllocationMethod MemoryManager::getPreferredAllocationMethod(const AllocationProperties &allocationProperties) const {
|
2023-12-12 11:37:31 +00:00
|
|
|
return GfxMemoryAllocationMethod::notDefined;
|
2023-08-30 09:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-06 22:36:12 +00:00
|
|
|
GraphicsAllocation *MemoryManager::allocatePhysicalGraphicsMemory(const AllocationProperties &properties) {
|
|
|
|
|
AllocationData allocationData;
|
|
|
|
|
GraphicsAllocation *allocation = nullptr;
|
|
|
|
|
getAllocationData(allocationData, properties, nullptr, createStorageInfoFromProperties(properties));
|
|
|
|
|
|
|
|
|
|
AllocationStatus status = AllocationStatus::Error;
|
|
|
|
|
if (this->localMemorySupported[allocationData.rootDeviceIndex]) {
|
|
|
|
|
allocation = allocatePhysicalLocalDeviceMemory(allocationData, status);
|
|
|
|
|
if (allocation) {
|
|
|
|
|
getLocalMemoryUsageBankSelector(properties.allocationType, properties.rootDeviceIndex)->reserveOnBanks(allocationData.storageInfo.getMemoryBanks(), allocation->getUnderlyingBufferSize());
|
|
|
|
|
status = this->registerLocalMemAlloc(allocation, properties.rootDeviceIndex);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
allocation = allocatePhysicalDeviceMemory(allocationData, status);
|
|
|
|
|
}
|
|
|
|
|
if (allocation && status != AllocationStatus::Success) {
|
|
|
|
|
freeGraphicsMemory(allocation);
|
|
|
|
|
allocation = nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (!allocation) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileLoggerInstance().logAllocation(allocation);
|
|
|
|
|
registerAllocationInOs(allocation);
|
|
|
|
|
return allocation;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-04 07:49:33 +02:00
|
|
|
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryInPreferredPool(const AllocationProperties &properties, const void *hostPtr) {
|
2018-07-09 14:12:32 +02:00
|
|
|
AllocationData allocationData;
|
2019-05-10 11:30:07 +02:00
|
|
|
getAllocationData(allocationData, properties, hostPtr, createStorageInfoFromProperties(properties));
|
2018-07-09 14:12:32 +02:00
|
|
|
|
2019-01-10 11:10:58 +01:00
|
|
|
AllocationStatus status = AllocationStatus::Error;
|
|
|
|
|
GraphicsAllocation *allocation = allocateGraphicsMemoryInDevicePool(allocationData, status);
|
2019-05-10 11:30:07 +02:00
|
|
|
if (allocation) {
|
2021-07-22 06:36:45 +00:00
|
|
|
getLocalMemoryUsageBankSelector(properties.allocationType, properties.rootDeviceIndex)->reserveOnBanks(allocationData.storageInfo.getMemoryBanks(), allocation->getUnderlyingBufferSize());
|
2022-11-15 14:34:44 +00:00
|
|
|
status = this->registerLocalMemAlloc(allocation, properties.rootDeviceIndex);
|
2019-05-10 11:30:07 +02:00
|
|
|
}
|
2018-07-18 09:48:21 +02:00
|
|
|
if (!allocation && status == AllocationStatus::RetryInNonDevicePool) {
|
|
|
|
|
allocation = allocateGraphicsMemory(allocationData);
|
2021-07-05 13:21:23 +00:00
|
|
|
if (allocation) {
|
2022-11-15 14:34:44 +00:00
|
|
|
status = this->registerSysMemAlloc(allocation);
|
2021-07-05 13:21:23 +00:00
|
|
|
}
|
2018-07-18 09:48:21 +02:00
|
|
|
}
|
2022-11-15 14:34:44 +00:00
|
|
|
if (allocation && status != AllocationStatus::Success) {
|
|
|
|
|
freeGraphicsMemory(allocation);
|
|
|
|
|
allocation = nullptr;
|
|
|
|
|
}
|
2022-04-11 03:40:18 +00:00
|
|
|
if (!allocation) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2024-03-29 10:17:04 +00:00
|
|
|
allocation->checkAllocationTypeReadOnlyRestrictions(properties);
|
2022-04-11 03:40:18 +00:00
|
|
|
|
2024-03-27 12:52:30 +00:00
|
|
|
auto &rootDeviceEnvironment = *executionEnvironment.rootDeviceEnvironments[properties.rootDeviceIndex];
|
|
|
|
|
auto &productHelper = rootDeviceEnvironment.getProductHelper();
|
|
|
|
|
if (productHelper.supportReadOnlyAllocations() &&
|
2024-03-29 10:17:04 +00:00
|
|
|
!productHelper.isBlitCopyRequiredForLocalMemory(rootDeviceEnvironment, *allocation) &&
|
|
|
|
|
allocation->canBeReadOnly()) {
|
2024-03-27 12:52:30 +00:00
|
|
|
allocation->setAsReadOnly();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-16 14:06:56 +00:00
|
|
|
fileLoggerInstance().logAllocation(allocation);
|
2020-10-09 10:48:37 +02:00
|
|
|
registerAllocationInOs(allocation);
|
2018-07-18 09:48:21 +02:00
|
|
|
return allocation;
|
2018-07-09 14:12:32 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-30 22:21:42 -07:00
|
|
|
GraphicsAllocation *MemoryManager::allocateInternalGraphicsMemoryWithHostCopy(uint32_t rootDeviceIndex,
|
|
|
|
|
DeviceBitfield bitField,
|
|
|
|
|
const void *ptr,
|
|
|
|
|
size_t size) {
|
|
|
|
|
NEO::AllocationProperties copyProperties{rootDeviceIndex,
|
|
|
|
|
size,
|
2023-12-11 14:24:36 +00:00
|
|
|
NEO::AllocationType::internalHostMemory,
|
2020-09-30 22:21:42 -07:00
|
|
|
bitField};
|
|
|
|
|
copyProperties.alignment = MemoryConstants::pageSize;
|
|
|
|
|
auto allocation = this->allocateGraphicsMemoryWithProperties(copyProperties);
|
|
|
|
|
if (allocation) {
|
|
|
|
|
memcpy_s(allocation->getUnderlyingBuffer(), allocation->getUnderlyingBufferSize(), ptr, size);
|
|
|
|
|
}
|
|
|
|
|
return allocation;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 18:14:30 +01:00
|
|
|
bool MemoryManager::mapAuxGpuVA(GraphicsAllocation *graphicsAllocation) {
|
2021-09-29 15:59:41 +00:00
|
|
|
bool ret = false;
|
2023-10-04 14:39:18 +00:00
|
|
|
for (auto &engine : getRegisteredEngines(graphicsAllocation->getRootDeviceIndex())) {
|
2021-09-29 15:59:41 +00:00
|
|
|
if (engine.commandStreamReceiver->pageTableManager.get()) {
|
|
|
|
|
ret = engine.commandStreamReceiver->pageTableManager->updateAuxTable(graphicsAllocation->getGpuAddress(), graphicsAllocation->getDefaultGmm(), true);
|
|
|
|
|
if (!ret) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-06 18:14:30 +01:00
|
|
|
}
|
2021-09-29 15:59:41 +00:00
|
|
|
return ret;
|
2019-11-06 18:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
2018-07-09 14:12:32 +02:00
|
|
|
GraphicsAllocation *MemoryManager::allocateGraphicsMemory(const AllocationData &allocationData) {
|
2023-12-11 14:24:36 +00:00
|
|
|
if (allocationData.type == AllocationType::image || allocationData.type == AllocationType::sharedResourceCopy) {
|
2018-12-20 16:58:15 +01:00
|
|
|
UNRECOVERABLE_IF(allocationData.imgInfo == nullptr);
|
2019-01-22 12:40:17 +01:00
|
|
|
return allocateGraphicsMemoryForImage(allocationData);
|
2018-12-20 16:58:15 +01:00
|
|
|
}
|
2021-08-11 10:36:04 +00:00
|
|
|
if (allocationData.flags.shareable || allocationData.flags.isUSMDeviceMemory) {
|
|
|
|
|
return allocateMemoryByKMD(allocationData);
|
2019-11-14 04:08:59 -05:00
|
|
|
}
|
2022-02-23 14:06:22 +00:00
|
|
|
if (((false == allocationData.flags.isUSMHostAllocation) || (nullptr == allocationData.hostPtr)) &&
|
|
|
|
|
(useNonSvmHostPtrAlloc(allocationData.type, allocationData.rootDeviceIndex) || isNonSvmBuffer(allocationData.hostPtr, allocationData.type, allocationData.rootDeviceIndex))) {
|
2019-03-12 12:00:41 +01:00
|
|
|
auto allocation = allocateGraphicsMemoryForNonSvmHostPtr(allocationData);
|
|
|
|
|
if (allocation) {
|
|
|
|
|
allocation->setFlushL3Required(allocationData.flags.flushL3);
|
|
|
|
|
}
|
|
|
|
|
return allocation;
|
|
|
|
|
}
|
2023-10-19 14:24:35 +00:00
|
|
|
bool use32Allocator = heapAssigners[allocationData.rootDeviceIndex]->use32BitHeap(allocationData.type);
|
2020-11-04 14:55:09 +00:00
|
|
|
|
2021-05-06 14:15:19 +00:00
|
|
|
bool isAllocationOnLimitedGPU = isLimitedGPUOnType(allocationData.rootDeviceIndex, allocationData.type);
|
|
|
|
|
if (use32Allocator || isAllocationOnLimitedGPU ||
|
2018-12-21 10:16:27 +01:00
|
|
|
(force32bitAllocations && allocationData.flags.allow32Bit && is64bit)) {
|
2023-09-15 17:20:28 +00:00
|
|
|
return allocate32BitGraphicsMemoryImpl(allocationData);
|
2018-07-09 14:12:32 +02:00
|
|
|
}
|
2020-10-05 09:57:50 +02:00
|
|
|
if (allocationData.flags.isUSMHostAllocation && allocationData.hostPtr) {
|
2020-09-03 00:57:05 -07:00
|
|
|
return allocateUSMHostGraphicsMemory(allocationData);
|
|
|
|
|
}
|
2018-07-09 14:12:32 +02:00
|
|
|
if (allocationData.hostPtr) {
|
2018-11-30 11:01:33 +01:00
|
|
|
return allocateGraphicsMemoryWithHostPtr(allocationData);
|
2018-07-09 14:12:32 +02:00
|
|
|
}
|
2020-07-01 10:38:19 +02:00
|
|
|
if (allocationData.gpuAddress) {
|
|
|
|
|
return allocateGraphicsMemoryWithGpuVa(allocationData);
|
|
|
|
|
}
|
2020-02-12 08:29:26 +01:00
|
|
|
if (peek64kbPagesEnabled(allocationData.rootDeviceIndex) && allocationData.flags.allow64kbPages) {
|
2018-12-06 15:03:06 +01:00
|
|
|
return allocateGraphicsMemory64kb(allocationData);
|
2018-07-09 14:12:32 +02:00
|
|
|
}
|
2018-11-30 11:01:33 +01:00
|
|
|
return allocateGraphicsMemoryWithAlignment(allocationData);
|
2018-07-09 14:12:32 +02:00
|
|
|
}
|
2018-11-29 11:39:10 +01:00
|
|
|
|
2019-01-22 12:40:17 +01:00
|
|
|
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryForImage(const AllocationData &allocationData) {
|
2022-04-27 10:20:10 +00:00
|
|
|
auto gmm = std::make_unique<Gmm>(executionEnvironment.rootDeviceEnvironments[allocationData.rootDeviceIndex]->getGmmHelper(), *allocationData.imgInfo,
|
2021-12-03 13:52:16 +00:00
|
|
|
allocationData.storageInfo, allocationData.flags.preferCompressed);
|
2019-01-22 12:40:17 +01: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 13:24:58 +01:00
|
|
|
hostPtrAllocation->setDefaultGmm(gmm.release());
|
2019-01-22 12:40:17 +01:00
|
|
|
return hostPtrAllocation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return allocateGraphicsMemoryForImageImpl(allocationDataWithSize, std::move(gmm));
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 13:59:01 +00:00
|
|
|
bool MemoryManager::isExternalAllocation(AllocationType allocationType) {
|
2023-12-11 14:24:36 +00:00
|
|
|
if (allocationType == AllocationType::buffer ||
|
|
|
|
|
allocationType == AllocationType::bufferHostMemory ||
|
|
|
|
|
allocationType == AllocationType::externalHostPtr ||
|
|
|
|
|
allocationType == AllocationType::fillPattern ||
|
|
|
|
|
allocationType == AllocationType::image ||
|
|
|
|
|
allocationType == AllocationType::mapAllocation ||
|
|
|
|
|
allocationType == AllocationType::pipe ||
|
|
|
|
|
allocationType == AllocationType::sharedBuffer ||
|
|
|
|
|
allocationType == AllocationType::sharedImage ||
|
|
|
|
|
allocationType == AllocationType::sharedResourceCopy ||
|
|
|
|
|
allocationType == AllocationType::svmCpu ||
|
|
|
|
|
allocationType == AllocationType::svmGpu ||
|
|
|
|
|
allocationType == AllocationType::svmZeroCopy ||
|
|
|
|
|
allocationType == AllocationType::unifiedSharedMemory ||
|
|
|
|
|
allocationType == AllocationType::writeCombined) {
|
2021-07-22 06:36:45 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 13:59:01 +00:00
|
|
|
LocalMemoryUsageBankSelector *MemoryManager::getLocalMemoryUsageBankSelector(AllocationType allocationType, uint32_t rootDeviceIndex) {
|
2021-07-22 08:43:58 +00:00
|
|
|
if (isExternalAllocation(allocationType)) {
|
|
|
|
|
return externalLocalMemoryUsageBankSelector[rootDeviceIndex].get();
|
2021-07-22 06:36:45 +00:00
|
|
|
}
|
2021-07-22 08:43:58 +00:00
|
|
|
return internalLocalMemoryUsageBankSelector[rootDeviceIndex].get();
|
2021-07-22 06:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
2023-04-26 10:36:25 +00:00
|
|
|
const EngineControl *MemoryManager::getRegisteredEngineForCsr(CommandStreamReceiver *commandStreamReceiver) {
|
|
|
|
|
const EngineControl *engineCtrl = nullptr;
|
|
|
|
|
for (auto &engine : getRegisteredEngines(commandStreamReceiver->getRootDeviceIndex())) {
|
2019-02-26 17:52:31 +01:00
|
|
|
if (engine.commandStreamReceiver == commandStreamReceiver) {
|
|
|
|
|
engineCtrl = &engine;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return engineCtrl;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-02 13:23:25 +02:00
|
|
|
void MemoryManager::unregisterEngineForCsr(CommandStreamReceiver *commandStreamReceiver) {
|
2023-04-26 10:36:25 +00:00
|
|
|
auto ®isteredEngines = allRegisteredEngines[commandStreamReceiver->getRootDeviceIndex()];
|
2019-09-02 13:23:25 +02:00
|
|
|
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 11:51:33 +01:00
|
|
|
void *MemoryManager::lockResource(GraphicsAllocation *graphicsAllocation) {
|
|
|
|
|
if (!graphicsAllocation) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2019-01-24 15:16:12 +01:00
|
|
|
if (graphicsAllocation->isLocked()) {
|
|
|
|
|
return graphicsAllocation->getLockedPtr();
|
|
|
|
|
}
|
2019-01-24 11:51:33 +01:00
|
|
|
auto retVal = lockResourceImpl(*graphicsAllocation);
|
2022-04-11 03:40:18 +00:00
|
|
|
|
|
|
|
|
if (!retVal) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-24 15:16:12 +01:00
|
|
|
graphicsAllocation->lock(retVal);
|
2019-01-24 11:51:33 +01:00
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MemoryManager::unlockResource(GraphicsAllocation *graphicsAllocation) {
|
|
|
|
|
if (!graphicsAllocation) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
DEBUG_BREAK_IF(!graphicsAllocation->isLocked());
|
|
|
|
|
unlockResourceImpl(*graphicsAllocation);
|
2019-01-24 15:16:12 +01:00
|
|
|
graphicsAllocation->unlock();
|
2019-01-24 11:51:33 +01:00
|
|
|
}
|
2019-02-20 11:18:24 +01:00
|
|
|
|
2020-10-14 09:50:07 +02:00
|
|
|
HeapIndex MemoryManager::selectHeap(const GraphicsAllocation *allocation, bool hasPointer, bool isFullRangeSVM, bool useFrontWindow) {
|
2019-02-20 11:18:24 +01:00
|
|
|
if (allocation) {
|
2023-10-19 14:24:35 +00:00
|
|
|
if (heapAssigners[allocation->getRootDeviceIndex()]->useInternal32BitHeap(allocation->getAllocationType())) {
|
2020-10-21 16:29:25 +02:00
|
|
|
return useFrontWindow ? HeapAssigner::mapInternalWindowIndex(selectInternalHeap(allocation->isAllocatedInLocalMemoryPool())) : selectInternalHeap(allocation->isAllocatedInLocalMemoryPool());
|
2019-05-18 13:44:46 -07:00
|
|
|
}
|
2023-10-19 14:24:35 +00:00
|
|
|
if (allocation->is32BitAllocation() || heapAssigners[allocation->getRootDeviceIndex()]->useExternal32BitHeap(allocation->getAllocationType())) {
|
2020-10-14 09:50:07 +02:00
|
|
|
return useFrontWindow ? HeapAssigner::mapExternalWindowIndex(selectExternalHeap(allocation->isAllocatedInLocalMemoryPool()))
|
|
|
|
|
: selectExternalHeap(allocation->isAllocatedInLocalMemoryPool());
|
2019-02-20 11:18:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-03-18 10:06:01 +01:00
|
|
|
if (isFullRangeSVM) {
|
|
|
|
|
if (hasPointer) {
|
2023-12-12 08:58:42 +00:00
|
|
|
return HeapIndex::heapSvm;
|
2019-02-20 11:18:24 +01:00
|
|
|
}
|
2019-03-12 13:24:58 +01:00
|
|
|
if (allocation && allocation->getDefaultGmm()->gmmResourceInfo->is64KBPageSuitable()) {
|
2023-12-12 08:58:42 +00:00
|
|
|
return HeapIndex::heapStandard64KB;
|
2019-02-20 11:18:24 +01:00
|
|
|
}
|
2023-12-12 08:58:42 +00:00
|
|
|
return HeapIndex::heapStandard;
|
2019-02-20 11:18:24 +01:00
|
|
|
}
|
2019-03-15 11:09:06 +01:00
|
|
|
// Limited range allocation goes to STANDARD heap
|
2023-12-12 08:58:42 +00:00
|
|
|
return HeapIndex::heapStandard;
|
2019-02-20 11:18:24 +01:00
|
|
|
}
|
2019-07-07 20:33:17 +02:00
|
|
|
|
2020-10-07 15:32:03 +02:00
|
|
|
bool MemoryManager::copyMemoryToAllocation(GraphicsAllocation *graphicsAllocation, size_t destinationOffset, const void *memoryToCopy, size_t sizeToCopy) {
|
2019-03-11 11:13:17 +01:00
|
|
|
if (!graphicsAllocation->getUnderlyingBuffer()) {
|
2019-03-01 14:25:33 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2022-11-14 12:06:54 +00:00
|
|
|
|
2021-05-21 13:16:52 +00: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);
|
2022-11-14 12:06:54 +00:00
|
|
|
if (!GraphicsAllocation::isDebugSurfaceAllocationType(graphicsAllocation->getAllocationType())) {
|
2021-10-21 11:16:19 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2021-05-21 13:16:52 +00:00
|
|
|
}
|
2019-03-01 14:25:33 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2019-03-22 14:04:25 +01:00
|
|
|
|
2021-10-21 11:16:19 +00:00
|
|
|
bool MemoryManager::copyMemoryToAllocationBanks(GraphicsAllocation *graphicsAllocation, size_t destinationOffset, const void *memoryToCopy, size_t sizeToCopy, DeviceBitfield handleMask) {
|
|
|
|
|
memcpy_s(ptrOffset(static_cast<uint8_t *>(graphicsAllocation->getUnderlyingBuffer()), destinationOffset),
|
|
|
|
|
(graphicsAllocation->getUnderlyingBufferSize() - destinationOffset), memoryToCopy, sizeToCopy);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-03-22 14:04:25 +01:00
|
|
|
void MemoryManager::waitForEnginesCompletion(GraphicsAllocation &graphicsAllocation) {
|
2023-04-26 10:36:25 +00:00
|
|
|
for (auto &engine : getRegisteredEngines(graphicsAllocation.getRootDeviceIndex())) {
|
|
|
|
|
auto osContextId = engine.osContext->getContextId();
|
|
|
|
|
auto allocationTaskCount = graphicsAllocation.getTaskCount(osContextId);
|
|
|
|
|
if (graphicsAllocation.isUsedByOsContext(osContextId) &&
|
|
|
|
|
engine.commandStreamReceiver->getTagAllocation() != nullptr &&
|
|
|
|
|
allocationTaskCount > *engine.commandStreamReceiver->getTagAddress()) {
|
|
|
|
|
engine.commandStreamReceiver->waitForCompletionWithTimeout(WaitParams{false, false, TimeoutControls::maxTimeout}, allocationTaskCount);
|
2019-03-22 14:04:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-03 18:39:11 +00:00
|
|
|
bool MemoryManager::allocInUse(GraphicsAllocation &graphicsAllocation) {
|
2023-04-26 10:36:25 +00:00
|
|
|
for (auto &engine : getRegisteredEngines(graphicsAllocation.getRootDeviceIndex())) {
|
|
|
|
|
auto osContextId = engine.osContext->getContextId();
|
|
|
|
|
auto allocationTaskCount = graphicsAllocation.getTaskCount(osContextId);
|
|
|
|
|
if (graphicsAllocation.isUsedByOsContext(osContextId) &&
|
|
|
|
|
engine.commandStreamReceiver->getTagAllocation() != nullptr &&
|
|
|
|
|
allocationTaskCount > *engine.commandStreamReceiver->getTagAddress()) {
|
|
|
|
|
return true;
|
2023-01-03 18:39:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-23 12:52:57 +01:00
|
|
|
void MemoryManager::cleanTemporaryAllocationListOnAllEngines(bool waitForCompletion) {
|
2023-04-26 10:36:25 +00:00
|
|
|
for (auto &engineContainer : allRegisteredEngines) {
|
2023-04-27 15:42:17 +00:00
|
|
|
for (auto &engine : engineContainer) {
|
2023-04-26 10:36:25 +00:00
|
|
|
auto csr = engine.commandStreamReceiver;
|
|
|
|
|
if (waitForCompletion) {
|
|
|
|
|
csr->waitForCompletionWithTimeout(WaitParams{false, false, 0}, csr->peekLatestSentTaskCount());
|
|
|
|
|
}
|
|
|
|
|
csr->getInternalAllocationStorage()->cleanAllocationList(*csr->getTagAddress(), AllocationUsage::TEMPORARY_ALLOCATION);
|
2019-03-23 12:52:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 15:26:30 +02: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 13:29:30 +01:00
|
|
|
bool MemoryManager::isHostPointerTrackingEnabled(uint32_t rootDeviceIndex) {
|
2023-11-30 08:32:25 +00:00
|
|
|
if (debugManager.flags.EnableHostPtrTracking.get() != -1) {
|
|
|
|
|
return !!debugManager.flags.EnableHostPtrTracking.get();
|
2019-12-04 13:42:41 +01:00
|
|
|
}
|
2020-02-18 13:29:30 +01:00
|
|
|
return (peekExecutionEnvironment().rootDeviceEnvironments[rootDeviceIndex]->getHardwareInfo()->capabilityTable.hostPtrTrackingEnabled | is32bit);
|
2019-12-04 13:42:41 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-04 13:59:01 +00:00
|
|
|
bool MemoryManager::useNonSvmHostPtrAlloc(AllocationType allocationType, uint32_t rootDeviceIndex) {
|
2023-12-11 14:24:36 +00:00
|
|
|
bool isExternalHostPtrAlloc = (allocationType == AllocationType::externalHostPtr);
|
|
|
|
|
bool isMapAlloc = (allocationType == AllocationType::mapAllocation);
|
2020-03-04 08:51:02 +01:00
|
|
|
|
|
|
|
|
if (forceNonSvmForExternalHostPtr && isExternalHostPtrAlloc) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isNonSvmPtrCapable = ((!peekExecutionEnvironment().rootDeviceEnvironments[rootDeviceIndex]->isFullRangeSvm() || !isHostPointerTrackingEnabled(rootDeviceIndex)) & !is32bit);
|
|
|
|
|
|
|
|
|
|
return isNonSvmPtrCapable && (isExternalHostPtrAlloc || isMapAlloc);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-13 09:40:03 +01: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) {
|
2023-12-13 16:09:52 +00:00
|
|
|
case ImageType::image3D:
|
2020-01-13 09:40:03 +01:00
|
|
|
imageDepth = imgInfo.imgDesc.imageDepth;
|
2021-10-22 16:12:30 +00:00
|
|
|
[[fallthrough]];
|
2023-12-13 16:09:52 +00:00
|
|
|
case ImageType::image2D:
|
|
|
|
|
case ImageType::image2DArray:
|
2020-01-13 09:40:03 +01:00
|
|
|
imageHeight = imgInfo.imgDesc.imageHeight;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-27 09:50:55 +00:00
|
|
|
auto hostPtrRowPitch = imgInfo.imgDesc.imageRowPitch ? imgInfo.imgDesc.imageRowPitch : imageWidth * imgInfo.surfaceFormat->imageElementSizeInBytes;
|
2020-01-13 09:40:03 +01:00
|
|
|
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-15 17:02:47 +01:00
|
|
|
|
|
|
|
|
void MemoryManager::overrideAllocationData(AllocationData &allocationData, const AllocationProperties &properties) {
|
2023-11-30 08:32:25 +00:00
|
|
|
if (debugManager.flags.ForceSystemMemoryPlacement.get()) {
|
2023-12-11 14:24:36 +00:00
|
|
|
UNRECOVERABLE_IF(properties.allocationType == AllocationType::unknown);
|
2023-11-30 08:32:25 +00:00
|
|
|
if ((1llu << (static_cast<int64_t>(properties.allocationType) - 1)) & debugManager.flags.ForceSystemMemoryPlacement.get()) {
|
2020-04-30 17:43:37 +02:00
|
|
|
allocationData.flags.useSystemMemory = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-30 08:32:25 +00:00
|
|
|
if (debugManager.flags.ForceNonSystemMemoryPlacement.get()) {
|
2023-12-11 14:24:36 +00:00
|
|
|
UNRECOVERABLE_IF(properties.allocationType == AllocationType::unknown);
|
2023-11-30 08:32:25 +00:00
|
|
|
if ((1llu << (static_cast<int64_t>(properties.allocationType) - 1)) & debugManager.flags.ForceNonSystemMemoryPlacement.get()) {
|
2020-04-30 17:43:37 +02:00
|
|
|
allocationData.flags.useSystemMemory = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-30 08:32:25 +00:00
|
|
|
int32_t directRingPlacement = debugManager.flags.DirectSubmissionBufferPlacement.get();
|
|
|
|
|
int32_t directRingAddressing = debugManager.flags.DirectSubmissionBufferAddressing.get();
|
2023-12-11 14:24:36 +00:00
|
|
|
if (properties.allocationType == AllocationType::ringBuffer) {
|
2020-03-25 14:55:23 +01:00
|
|
|
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-15 17:02:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-11-30 08:32:25 +00:00
|
|
|
int32_t directSemaphorePlacement = debugManager.flags.DirectSubmissionSemaphorePlacement.get();
|
|
|
|
|
int32_t directSemaphoreAddressing = debugManager.flags.DirectSubmissionSemaphoreAddressing.get();
|
2023-12-11 14:24:36 +00:00
|
|
|
if (properties.allocationType == AllocationType::semaphoreBuffer) {
|
2020-03-25 14:55:23 +01:00
|
|
|
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-15 17:02:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-10-09 11:52:00 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-04 13:59:01 +00:00
|
|
|
bool MemoryManager::isAllocationTypeToCapture(AllocationType type) const {
|
2021-03-09 23:02:59 +00:00
|
|
|
switch (type) {
|
2023-12-11 14:24:36 +00:00
|
|
|
case AllocationType::scratchSurface:
|
|
|
|
|
case AllocationType::privateSurface:
|
|
|
|
|
case AllocationType::linearStream:
|
|
|
|
|
case AllocationType::internalHeap:
|
2021-03-09 23:02:59 +00:00
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 18:57:32 +00:00
|
|
|
bool MemoryManager::isLocalMemoryUsedForIsa(uint32_t rootDeviceIndex) {
|
2021-06-21 10:45:19 +00:00
|
|
|
std::call_once(checkIsaPlacementOnceFlags[rootDeviceIndex], [&] {
|
2023-12-11 14:24:36 +00:00
|
|
|
AllocationProperties properties = {rootDeviceIndex, 0x1000, AllocationType::kernelIsa, 1};
|
2021-05-24 18:57:32 +00:00
|
|
|
AllocationData data;
|
2024-04-11 12:23:06 +00:00
|
|
|
getAllocationData(data, properties, nullptr, createStorageInfoFromProperties(properties));
|
2021-06-21 10:45:19 +00:00
|
|
|
isaInLocalMemory[rootDeviceIndex] = !data.flags.useSystemMemory;
|
2021-05-24 18:57:32 +00:00
|
|
|
});
|
|
|
|
|
|
2021-06-21 10:45:19 +00:00
|
|
|
return isaInLocalMemory[rootDeviceIndex];
|
2021-05-24 18:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-15 16:01:37 +00:00
|
|
|
bool MemoryManager::isKernelBinaryReuseEnabled() {
|
|
|
|
|
auto reuseBinaries = false;
|
|
|
|
|
|
2023-11-30 08:32:25 +00:00
|
|
|
if (debugManager.flags.ReuseKernelBinaries.get() != -1) {
|
|
|
|
|
reuseBinaries = debugManager.flags.ReuseKernelBinaries.get();
|
2022-12-15 16:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return reuseBinaries;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 20:26:22 +00:00
|
|
|
OsContext *MemoryManager::getDefaultEngineContext(uint32_t rootDeviceIndex, DeviceBitfield subdevicesBitfield) {
|
|
|
|
|
OsContext *defaultContext = nullptr;
|
2023-04-26 10:36:25 +00:00
|
|
|
for (auto &engine : getRegisteredEngines(rootDeviceIndex)) {
|
|
|
|
|
auto osContext = engine.osContext;
|
|
|
|
|
if (osContext->isDefaultContext() && osContext->getDeviceBitfield() == subdevicesBitfield) {
|
|
|
|
|
defaultContext = osContext;
|
2022-10-17 20:26:22 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!defaultContext) {
|
2023-04-26 10:36:25 +00:00
|
|
|
defaultContext = getRegisteredEngines(rootDeviceIndex)[defaultEngineIndex[rootDeviceIndex]].osContext;
|
2022-10-17 20:26:22 +00:00
|
|
|
}
|
|
|
|
|
return defaultContext;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 11:55:22 +00:00
|
|
|
bool MemoryManager::allocateBindlessSlot(GraphicsAllocation *allocation) {
|
|
|
|
|
auto bindlessHelper = peekExecutionEnvironment().rootDeviceEnvironments[allocation->getRootDeviceIndex()]->getBindlessHeapsHelper();
|
|
|
|
|
|
|
|
|
|
if (bindlessHelper && allocation->getBindlessOffset() == std::numeric_limits<uint64_t>::max()) {
|
|
|
|
|
auto &gfxCoreHelper = peekExecutionEnvironment().rootDeviceEnvironments[allocation->getRootDeviceIndex()]->getHelper<GfxCoreHelper>();
|
2023-12-11 14:24:36 +00:00
|
|
|
const auto isImage = allocation->getAllocationType() == AllocationType::image || allocation->getAllocationType() == AllocationType::sharedImage;
|
2024-05-06 09:48:21 +00:00
|
|
|
auto surfStateCount = isImage ? NEO::BindlessImageSlot::max : 1;
|
2023-08-17 08:17:57 +00:00
|
|
|
auto surfaceStateSize = surfStateCount * gfxCoreHelper.getRenderSurfaceStateSize();
|
2023-07-21 11:55:22 +00:00
|
|
|
|
2023-12-13 16:09:52 +00:00
|
|
|
auto surfaceStateInfo = bindlessHelper->allocateSSInHeap(surfaceStateSize, allocation, NEO::BindlessHeapsHelper::globalSsh);
|
2023-07-21 11:55:22 +00:00
|
|
|
if (surfaceStateInfo.heapAllocation == nullptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
allocation->setBindlessInfo(surfaceStateInfo);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-09 11:52:00 +02:00
|
|
|
bool MemoryTransferHelper::transferMemoryToAllocation(bool useBlitter, const Device &device, GraphicsAllocation *dstAllocation, size_t dstOffset, const void *srcMemory, size_t srcSize) {
|
|
|
|
|
if (useBlitter) {
|
2023-12-19 07:40:17 +00:00
|
|
|
if (BlitHelperFunctions::blitMemoryToAllocation(device, dstAllocation, dstOffset, srcMemory, {srcSize, 1, 1}) == BlitOperationResult::success) {
|
2021-05-12 13:58:40 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2020-10-09 11:52:00 +02:00
|
|
|
}
|
2021-05-12 13:58:40 +00:00
|
|
|
return device.getMemoryManager()->copyMemoryToAllocation(dstAllocation, dstOffset, srcMemory, srcSize);
|
2020-10-09 11:52:00 +02:00
|
|
|
}
|
2021-10-21 11:16:19 +00:00
|
|
|
bool MemoryTransferHelper::transferMemoryToAllocationBanks(const Device &device, GraphicsAllocation *dstAllocation, size_t dstOffset, const void *srcMemory,
|
|
|
|
|
size_t srcSize, DeviceBitfield dstMemoryBanks) {
|
2023-12-19 07:40:17 +00:00
|
|
|
auto blitSuccess = BlitHelper::blitMemoryToAllocationBanks(device, dstAllocation, dstOffset, srcMemory, {srcSize, 1, 1}, dstMemoryBanks) == BlitOperationResult::success;
|
2021-10-21 11:16:19 +00:00
|
|
|
|
|
|
|
|
if (!blitSuccess) {
|
|
|
|
|
return device.getMemoryManager()->copyMemoryToAllocationBanks(dstAllocation, dstOffset, srcMemory, srcSize, dstMemoryBanks);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-01-15 14:47:26 +00:00
|
|
|
|
|
|
|
|
uint64_t MemoryManager::adjustToggleBitFlagForGpuVa(AllocationType inputAllocationType, uint64_t gpuAddress) {
|
|
|
|
|
if (debugManager.flags.ToggleBitIn57GpuVa.get() != "unk") {
|
|
|
|
|
auto toggleBitIn57GpuVaEntries = StringHelpers::split(debugManager.flags.ToggleBitIn57GpuVa.get(), ",");
|
|
|
|
|
|
|
|
|
|
for (const auto &entry : toggleBitIn57GpuVaEntries) {
|
|
|
|
|
auto subEntries = StringHelpers::split(entry, ":");
|
|
|
|
|
UNRECOVERABLE_IF(subEntries.size() < 2u);
|
|
|
|
|
uint32_t allocationType = StringHelpers::toUint32t(subEntries[0]);
|
|
|
|
|
uint32_t bitNumber = StringHelpers::toUint32t(subEntries[1]);
|
|
|
|
|
|
|
|
|
|
UNRECOVERABLE_IF(allocationType >= static_cast<uint32_t>(AllocationType::count));
|
|
|
|
|
UNRECOVERABLE_IF(bitNumber >= 56);
|
|
|
|
|
if (allocationType == static_cast<uint32_t>(inputAllocationType)) {
|
|
|
|
|
if (isBitSet(gpuAddress, bitNumber)) {
|
|
|
|
|
gpuAddress &= ~(1ull << bitNumber);
|
|
|
|
|
} else {
|
|
|
|
|
gpuAddress |= 1ull << bitNumber;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return gpuAddress;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 12:39:04 +00:00
|
|
|
} // namespace NEO
|