2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2019-12-30 21:40:24 +08:00
|
|
|
* Copyright (C) 2017-2020 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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/command_stream/preemption_mode.h"
|
|
|
|
#include "shared/source/helpers/aligned_memory.h"
|
|
|
|
#include "shared/source/helpers/bit_helpers.h"
|
|
|
|
#include "shared/source/helpers/common_types.h"
|
|
|
|
#include "shared/source/helpers/engine_control.h"
|
|
|
|
#include "shared/source/memory_manager/allocation_properties.h"
|
|
|
|
#include "shared/source/memory_manager/gfx_partition.h"
|
|
|
|
#include "shared/source/memory_manager/graphics_allocation.h"
|
|
|
|
#include "shared/source/memory_manager/host_ptr_defines.h"
|
|
|
|
#include "shared/source/memory_manager/local_memory_usage.h"
|
|
|
|
#include "shared/source/page_fault_manager/cpu_page_fault_manager.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
2018-11-26 21:04:52 +08:00
|
|
|
#include "engine_node.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2019-03-13 22:00:07 +08:00
|
|
|
#include <bitset>
|
2017-12-21 07:45:38 +08:00
|
|
|
#include <cstdint>
|
|
|
|
#include <mutex>
|
2018-07-23 01:27:33 +08:00
|
|
|
#include <vector>
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2017-12-21 07:45:38 +08:00
|
|
|
class DeferredDeleter;
|
2018-10-01 22:10:54 +08:00
|
|
|
class ExecutionEnvironment;
|
2018-12-20 23:58:15 +08:00
|
|
|
class Gmm;
|
2018-10-24 14:46:54 +08:00
|
|
|
class HostPtrManager;
|
2018-10-22 22:43:20 +08:00
|
|
|
class OsContext;
|
2018-12-20 23:58:15 +08:00
|
|
|
|
2018-07-05 22:31:57 +08:00
|
|
|
enum AllocationUsage {
|
2017-12-21 07:45:38 +08:00
|
|
|
TEMPORARY_ALLOCATION,
|
|
|
|
REUSABLE_ALLOCATION
|
|
|
|
};
|
|
|
|
|
2018-01-22 23:43:26 +08:00
|
|
|
struct AlignedMallocRestrictions {
|
|
|
|
uintptr_t minAddress;
|
|
|
|
};
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
constexpr size_t paddingBufferSize = 2 * MemoryConstants::megaByte;
|
|
|
|
|
|
|
|
class MemoryManager {
|
|
|
|
public:
|
2018-02-28 19:09:48 +08:00
|
|
|
enum AllocationStatus {
|
|
|
|
Success = 0,
|
|
|
|
Error,
|
|
|
|
InvalidHostPointer,
|
2018-07-18 15:48:21 +08:00
|
|
|
RetryInNonDevicePool
|
2018-02-28 19:09:48 +08:00
|
|
|
};
|
|
|
|
|
2019-03-15 17:22:35 +08:00
|
|
|
MemoryManager(ExecutionEnvironment &executionEnvironment);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
virtual ~MemoryManager();
|
2018-01-26 23:53:18 +08:00
|
|
|
MOCKABLE_VIRTUAL void *allocateSystemMemory(size_t size, size_t alignment);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-05-08 16:00:23 +08:00
|
|
|
virtual void addAllocationToHostPtrManager(GraphicsAllocation *memory) = 0;
|
|
|
|
virtual void removeAllocationFromHostPtrManager(GraphicsAllocation *memory) = 0;
|
|
|
|
|
2018-12-14 18:24:45 +08:00
|
|
|
MOCKABLE_VIRTUAL GraphicsAllocation *allocateGraphicsMemoryWithProperties(const AllocationProperties &properties) {
|
2019-04-04 13:49:33 +08:00
|
|
|
return allocateGraphicsMemoryInPreferredPool(properties, nullptr);
|
2018-11-30 18:01:33 +08:00
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2019-04-03 15:08:10 +08:00
|
|
|
MOCKABLE_VIRTUAL GraphicsAllocation *allocateGraphicsMemoryWithProperties(const AllocationProperties &properties, const void *ptr) {
|
2019-04-04 13:49:33 +08:00
|
|
|
return allocateGraphicsMemoryInPreferredPool(properties, ptr);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
2019-12-12 18:51:23 +08:00
|
|
|
MOCKABLE_VIRTUAL GraphicsAllocation *allocateGraphicsMemoryInPreferredPool(const AllocationProperties &properties, const void *hostPtr);
|
2018-07-09 20:12:32 +08:00
|
|
|
|
2019-04-01 20:04:50 +08:00
|
|
|
virtual GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness) = 0;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2019-10-22 16:26:23 +08:00
|
|
|
virtual GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) = 0;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2019-11-07 01:14:30 +08:00
|
|
|
virtual bool mapAuxGpuVA(GraphicsAllocation *graphicsAllocation);
|
2018-01-25 22:10:07 +08:00
|
|
|
|
2019-01-24 18:51:33 +08:00
|
|
|
void *lockResource(GraphicsAllocation *graphicsAllocation);
|
|
|
|
void unlockResource(GraphicsAllocation *graphicsAllocation);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
void cleanGraphicsMemoryCreatedFromHostPtr(GraphicsAllocation *);
|
|
|
|
GraphicsAllocation *createGraphicsAllocationWithPadding(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding);
|
|
|
|
virtual GraphicsAllocation *createPaddedAllocation(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding);
|
|
|
|
|
2020-01-07 14:42:40 +08:00
|
|
|
virtual AllocationStatus populateOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) = 0;
|
2019-11-15 16:59:48 +08:00
|
|
|
virtual void cleanOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) = 0;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
void freeSystemMemory(void *ptr);
|
|
|
|
|
|
|
|
virtual void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) = 0;
|
2019-12-12 18:51:23 +08:00
|
|
|
MOCKABLE_VIRTUAL void freeGraphicsMemory(GraphicsAllocation *gfxAllocation);
|
2019-03-04 21:50:26 +08:00
|
|
|
virtual void handleFenceCompletion(GraphicsAllocation *allocation){};
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-01-19 17:55:36 +08:00
|
|
|
void checkGpuUsageAndDestroyGraphicsAllocations(GraphicsAllocation *gfxAllocation);
|
|
|
|
|
2020-01-07 14:42:40 +08:00
|
|
|
virtual uint64_t getSystemSharedMemory(uint32_t rootDeviceIndex) = 0;
|
|
|
|
virtual uint64_t getLocalMemorySize(uint32_t rootDeviceIndex) = 0;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2019-07-04 18:40:58 +08:00
|
|
|
uint64_t getMaxApplicationAddress() { return is64bit ? MemoryConstants::max64BitAppAddress : MemoryConstants::max32BitAppAddress; };
|
2020-04-05 12:05:43 +08:00
|
|
|
MOCKABLE_VIRTUAL uint64_t getInternalHeapBaseAddress(uint32_t rootDeviceIndex) { return getGfxPartition(rootDeviceIndex)->getHeapBase(HeapIndex::HEAP_INTERNAL_DEVICE_MEMORY); }
|
2019-10-22 16:26:23 +08:00
|
|
|
uint64_t getExternalHeapBaseAddress(uint32_t rootDeviceIndex) { return getGfxPartition(rootDeviceIndex)->getHeapBase(HeapIndex::HEAP_EXTERNAL); }
|
2019-04-16 17:47:47 +08:00
|
|
|
|
2019-10-22 16:26:23 +08:00
|
|
|
bool isLimitedRange(uint32_t rootDeviceIndex) { return getGfxPartition(rootDeviceIndex)->isLimitedRange(); }
|
2019-04-18 21:30:47 +08:00
|
|
|
|
2020-02-12 15:29:26 +08:00
|
|
|
bool peek64kbPagesEnabled(uint32_t rootDeviceIndex) const;
|
2019-02-25 21:11:34 +08:00
|
|
|
bool peekForce32BitAllocations() const { return force32bitAllocations; }
|
2019-07-04 18:40:58 +08:00
|
|
|
void setForce32BitAllocations(bool newValue) { force32bitAllocations = newValue; }
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2019-02-25 21:11:34 +08:00
|
|
|
bool peekVirtualPaddingSupport() const { return virtualPaddingAvailable; }
|
2017-12-21 07:45:38 +08:00
|
|
|
void setVirtualPaddingSupport(bool virtualPaddingSupport) { virtualPaddingAvailable = virtualPaddingSupport; }
|
|
|
|
|
|
|
|
DeferredDeleter *getDeferredDeleter() const {
|
|
|
|
return deferredDeleter.get();
|
|
|
|
}
|
|
|
|
|
2019-07-04 18:17:42 +08:00
|
|
|
PageFaultManager *getPageFaultManager() const {
|
|
|
|
return pageFaultManager.get();
|
|
|
|
}
|
|
|
|
|
2018-05-10 16:16:22 +08:00
|
|
|
void waitForDeletions();
|
2019-03-22 21:04:25 +08:00
|
|
|
void waitForEnginesCompletion(GraphicsAllocation &graphicsAllocation);
|
2019-03-23 19:52:57 +08:00
|
|
|
void cleanTemporaryAllocationListOnAllEngines(bool waitForCompletion);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
bool isAsyncDeleterEnabled() const;
|
2020-02-12 15:29:26 +08:00
|
|
|
bool isLocalMemorySupported(uint32_t rootDeviceIndex) const;
|
2017-12-21 07:45:38 +08:00
|
|
|
virtual bool isMemoryBudgetExhausted() const;
|
|
|
|
|
2018-01-22 23:43:26 +08:00
|
|
|
virtual AlignedMallocRestrictions *getAlignedMallocRestrictions() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOCKABLE_VIRTUAL void *alignedMallocWrapper(size_t bytes, size_t alignment) {
|
|
|
|
return ::alignedMalloc(bytes, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOCKABLE_VIRTUAL void alignedFreeWrapper(void *ptr) {
|
|
|
|
::alignedFree(ptr);
|
|
|
|
}
|
|
|
|
|
2020-02-18 20:29:30 +08:00
|
|
|
MOCKABLE_VIRTUAL bool isHostPointerTrackingEnabled(uint32_t rootDeviceIndex);
|
2019-12-04 20:42:41 +08:00
|
|
|
|
2020-01-24 18:30:07 +08:00
|
|
|
void setForceNonSvmForExternalHostPtr(bool mode) {
|
|
|
|
forceNonSvmForExternalHostPtr = mode;
|
|
|
|
}
|
|
|
|
|
2019-09-02 15:13:00 +08:00
|
|
|
const ExecutionEnvironment &peekExecutionEnvironment() const { return executionEnvironment; }
|
|
|
|
|
2019-03-27 17:06:29 +08:00
|
|
|
OsContext *createAndRegisterOsContext(CommandStreamReceiver *commandStreamReceiver, aub_stream::EngineType engineType,
|
2020-03-04 06:33:31 +08:00
|
|
|
DeviceBitfield deviceBitfield, PreemptionMode preemptionMode,
|
|
|
|
bool lowPriority, bool internalEngine, bool rootDevice);
|
2019-02-25 21:11:34 +08:00
|
|
|
uint32_t getRegisteredEnginesCount() const { return static_cast<uint32_t>(registeredEngines.size()); }
|
2019-02-18 20:59:16 +08:00
|
|
|
EngineControlContainer &getRegisteredEngines();
|
2019-02-27 00:52:31 +08:00
|
|
|
EngineControl *getRegisteredEngineForCsr(CommandStreamReceiver *commandStreamReceiver);
|
2019-09-02 19:23:25 +08:00
|
|
|
void unregisterEngineForCsr(CommandStreamReceiver *commandStreamReceiver);
|
2018-10-24 14:46:54 +08:00
|
|
|
HostPtrManager *getHostPtrManager() const { return hostPtrManager.get(); }
|
2018-11-29 18:39:10 +08:00
|
|
|
void setDefaultEngineIndex(uint32_t index) { defaultEngineIndex = index; }
|
2019-07-08 02:33:17 +08:00
|
|
|
virtual bool copyMemoryToAllocation(GraphicsAllocation *graphicsAllocation, const void *memoryToCopy, size_t sizeToCopy);
|
2019-03-18 17:06:01 +08:00
|
|
|
static HeapIndex selectHeap(const GraphicsAllocation *allocation, bool hasPointer, bool isFullRangeSVM);
|
2019-03-15 17:22:35 +08:00
|
|
|
static std::unique_ptr<MemoryManager> createMemoryManager(ExecutionEnvironment &executionEnvironment);
|
2020-01-07 14:42:40 +08:00
|
|
|
virtual void *reserveCpuAddressRange(size_t size, uint32_t rootDeviceIndex) { return nullptr; };
|
|
|
|
virtual void releaseReservedCpuAddressRange(void *reserved, size_t size, uint32_t rootDeviceIndex){};
|
2019-08-27 21:26:30 +08:00
|
|
|
void *getReservedMemory(size_t size, size_t alignment);
|
2019-10-22 16:26:23 +08:00
|
|
|
GfxPartition *getGfxPartition(uint32_t rootDeviceIndex) { return gfxPartitions.at(rootDeviceIndex).get(); }
|
2019-02-20 18:18:24 +08:00
|
|
|
|
2019-12-17 15:11:16 +08:00
|
|
|
static uint32_t maxOsContextCount;
|
2020-02-12 00:48:40 +08:00
|
|
|
virtual void commonCleanup(){};
|
2020-02-28 16:59:18 +08:00
|
|
|
virtual bool isCpuCopyRequired(const void *ptr) { return false; }
|
2019-12-17 15:11:16 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
protected:
|
2018-12-06 22:03:06 +08:00
|
|
|
struct AllocationData {
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
uint32_t allocateMemory : 1;
|
|
|
|
uint32_t allow64kbPages : 1;
|
|
|
|
uint32_t allow32Bit : 1;
|
|
|
|
uint32_t useSystemMemory : 1;
|
|
|
|
uint32_t forcePin : 1;
|
|
|
|
uint32_t uncacheable : 1;
|
|
|
|
uint32_t flushL3 : 1;
|
|
|
|
uint32_t preferRenderCompressed : 1;
|
2019-01-07 16:29:49 +08:00
|
|
|
uint32_t multiOsContextCapable : 1;
|
2019-02-04 21:13:51 +08:00
|
|
|
uint32_t requiresCpuAccess : 1;
|
2019-11-14 17:08:59 +08:00
|
|
|
uint32_t shareable : 1;
|
2020-02-11 00:58:02 +08:00
|
|
|
uint32_t resource48Bit : 1;
|
|
|
|
uint32_t reserved : 20;
|
2018-12-06 22:03:06 +08:00
|
|
|
} flags;
|
|
|
|
uint32_t allFlags = 0;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(AllocationData::flags) == sizeof(AllocationData::allFlags), "");
|
|
|
|
GraphicsAllocation::AllocationType type = GraphicsAllocation::AllocationType::UNKNOWN;
|
|
|
|
const void *hostPtr = nullptr;
|
|
|
|
size_t size = 0;
|
|
|
|
size_t alignment = 0;
|
2019-02-28 23:12:23 +08:00
|
|
|
StorageInfo storageInfo = {};
|
2018-12-20 23:58:15 +08:00
|
|
|
ImageInfo *imgInfo = nullptr;
|
2019-10-22 16:26:23 +08:00
|
|
|
uint32_t rootDeviceIndex = 0;
|
2018-12-06 22:03:06 +08:00
|
|
|
};
|
|
|
|
|
2019-05-10 17:30:07 +08:00
|
|
|
static bool getAllocationData(AllocationData &allocationData, const AllocationProperties &properties, const void *hostPtr, const StorageInfo &storageInfo);
|
2020-01-16 00:02:47 +08:00
|
|
|
static void overrideAllocationData(AllocationData &allocationData, const AllocationProperties &properties);
|
2019-02-21 23:29:05 +08:00
|
|
|
static bool useInternal32BitAllocator(GraphicsAllocation::AllocationType allocationType) {
|
|
|
|
return allocationType == GraphicsAllocation::AllocationType::KERNEL_ISA ||
|
|
|
|
allocationType == GraphicsAllocation::AllocationType::INTERNAL_HEAP;
|
|
|
|
}
|
2020-01-13 16:40:03 +08:00
|
|
|
static bool isCopyRequired(ImageInfo &imgInfo, const void *hostPtr);
|
|
|
|
|
2020-03-04 15:51:02 +08:00
|
|
|
bool useNonSvmHostPtrAlloc(GraphicsAllocation::AllocationType allocationType, uint32_t rootDeviceIndex);
|
2019-05-10 17:30:07 +08:00
|
|
|
StorageInfo createStorageInfoFromProperties(const AllocationProperties &properties);
|
2018-07-09 20:12:32 +08:00
|
|
|
|
2019-02-25 21:11:34 +08:00
|
|
|
virtual GraphicsAllocation *createGraphicsAllocation(OsHandleStorage &handleStorage, const AllocationData &allocationData) = 0;
|
2019-03-12 19:00:41 +08:00
|
|
|
virtual GraphicsAllocation *allocateGraphicsMemoryForNonSvmHostPtr(const AllocationData &allocationData) = 0;
|
2018-07-09 20:12:32 +08:00
|
|
|
GraphicsAllocation *allocateGraphicsMemory(const AllocationData &allocationData);
|
2018-11-30 18:01:33 +08:00
|
|
|
virtual GraphicsAllocation *allocateGraphicsMemoryWithHostPtr(const AllocationData &allocationData);
|
|
|
|
virtual GraphicsAllocation *allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) = 0;
|
2019-02-28 21:12:13 +08:00
|
|
|
virtual GraphicsAllocation *allocateGraphicsMemory64kb(const AllocationData &allocationData) = 0;
|
2018-12-21 17:16:27 +08:00
|
|
|
virtual GraphicsAllocation *allocate32BitGraphicsMemoryImpl(const AllocationData &allocationData) = 0;
|
2019-05-25 07:55:40 +08:00
|
|
|
virtual GraphicsAllocation *allocateGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) = 0;
|
2019-01-22 19:40:17 +08:00
|
|
|
GraphicsAllocation *allocateGraphicsMemoryForImageFromHostPtr(const AllocationData &allocationData);
|
|
|
|
MOCKABLE_VIRTUAL GraphicsAllocation *allocateGraphicsMemoryForImage(const AllocationData &allocationData);
|
|
|
|
virtual GraphicsAllocation *allocateGraphicsMemoryForImageImpl(const AllocationData &allocationData, std::unique_ptr<Gmm> gmm) = 0;
|
2019-11-14 17:08:59 +08:00
|
|
|
virtual GraphicsAllocation *allocateShareableMemory(const AllocationData &allocationData) = 0;
|
2019-01-24 18:51:33 +08:00
|
|
|
virtual void *lockResourceImpl(GraphicsAllocation &graphicsAllocation) = 0;
|
|
|
|
virtual void unlockResourceImpl(GraphicsAllocation &graphicsAllocation) = 0;
|
2019-03-11 17:19:02 +08:00
|
|
|
virtual void freeAssociatedResourceImpl(GraphicsAllocation &graphicsAllocation) { return unlockResourceImpl(graphicsAllocation); };
|
2019-01-24 18:51:33 +08:00
|
|
|
|
2020-01-24 18:30:07 +08:00
|
|
|
bool forceNonSvmForExternalHostPtr = false;
|
2017-12-21 07:45:38 +08:00
|
|
|
bool force32bitAllocations = false;
|
|
|
|
bool virtualPaddingAvailable = false;
|
|
|
|
std::unique_ptr<DeferredDeleter> deferredDeleter;
|
|
|
|
bool asyncDeleterEnabled = false;
|
2020-02-12 15:29:26 +08:00
|
|
|
std::vector<bool> enable64kbpages;
|
|
|
|
std::vector<bool> localMemorySupported;
|
2019-06-06 14:45:14 +08:00
|
|
|
bool supportsMultiStorageResources = true;
|
2018-10-01 22:10:54 +08:00
|
|
|
ExecutionEnvironment &executionEnvironment;
|
2019-02-18 20:59:16 +08:00
|
|
|
EngineControlContainer registeredEngines;
|
2018-10-24 14:46:54 +08:00
|
|
|
std::unique_ptr<HostPtrManager> hostPtrManager;
|
2018-11-21 16:57:51 +08:00
|
|
|
uint32_t latestContextId = std::numeric_limits<uint32_t>::max();
|
2018-11-29 18:39:10 +08:00
|
|
|
uint32_t defaultEngineIndex = 0;
|
2019-01-03 21:48:24 +08:00
|
|
|
std::unique_ptr<DeferredDeleter> multiContextResourceDestructor;
|
2019-10-22 16:26:23 +08:00
|
|
|
std::vector<std::unique_ptr<GfxPartition>> gfxPartitions;
|
2020-02-25 19:54:11 +08:00
|
|
|
std::vector<std::unique_ptr<LocalMemoryUsageBankSelector>> localMemoryUsageBankSelector;
|
2019-08-27 21:26:30 +08:00
|
|
|
void *reservedMemory = nullptr;
|
2019-07-04 18:17:42 +08:00
|
|
|
std::unique_ptr<PageFaultManager> pageFaultManager;
|
2017-12-21 07:45:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<DeferredDeleter> createDeferredDeleter();
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|