2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2023-01-11 19:03:48 +08:00
|
|
|
* Copyright (C) 2018-2023 Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
2018-09-18 15:11:08 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/memory_manager/memory_manager.h"
|
2023-02-01 23:58:22 +08:00
|
|
|
#include "shared/source/os_interface/linux/drm_buffer_object.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
2020-03-19 17:41:35 +08:00
|
|
|
#include <limits>
|
2017-12-21 07:45:38 +08:00
|
|
|
#include <map>
|
|
|
|
#include <sys/mman.h>
|
2022-07-27 00:39:17 +08:00
|
|
|
#include <unistd.h>
|
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 BufferObject;
|
|
|
|
class Drm;
|
2022-07-27 00:39:17 +08:00
|
|
|
class DrmGemCloseWorker;
|
|
|
|
class DrmAllocation;
|
2023-04-26 18:36:25 +08:00
|
|
|
class OsContextLinux;
|
2023-09-21 06:34:19 +08:00
|
|
|
enum class AtomicAccessMode : uint32_t;
|
2022-07-27 00:39:17 +08:00
|
|
|
|
|
|
|
enum class gemCloseWorkerMode;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
class DrmMemoryManager : public MemoryManager {
|
|
|
|
public:
|
2019-03-25 20:12:55 +08:00
|
|
|
DrmMemoryManager(gemCloseWorkerMode mode,
|
2019-03-12 06:06:30 +08:00
|
|
|
bool forcePinAllowed,
|
|
|
|
bool validateHostPtrMemory,
|
|
|
|
ExecutionEnvironment &executionEnvironment);
|
2017-12-21 07:45:38 +08:00
|
|
|
~DrmMemoryManager() override;
|
|
|
|
|
2020-09-18 22:19:41 +08:00
|
|
|
void initialize(gemCloseWorkerMode mode);
|
2018-05-08 16:00:23 +08:00
|
|
|
void addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) override;
|
|
|
|
void removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) override;
|
2017-12-21 07:45:38 +08:00
|
|
|
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override;
|
2022-03-02 11:43:59 +08:00
|
|
|
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation, bool isImportedAllocation) override;
|
2019-03-04 21:50:26 +08:00
|
|
|
void handleFenceCompletion(GraphicsAllocation *allocation) override;
|
2020-10-21 16:50:53 +08:00
|
|
|
GraphicsAllocation *createGraphicsAllocationFromExistingStorage(AllocationProperties &properties, void *ptr, MultiGraphicsAllocation &multiGraphicsAllocation) override;
|
2023-05-04 09:40:52 +08:00
|
|
|
GraphicsAllocation *createGraphicsAllocationFromMultipleSharedHandles(const std::vector<osHandle> &handles, AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation, bool reuseSharedAllocation, void *mapPointer) override;
|
|
|
|
GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation, bool reuseSharedAllocation, void *mapPointer) override;
|
2021-03-18 23:16:58 +08:00
|
|
|
void closeSharedHandle(GraphicsAllocation *gfxAllocation) override;
|
2023-04-07 10:57:37 +08:00
|
|
|
void closeInternalHandle(uint64_t &handle, uint32_t handleId, GraphicsAllocation *graphicsAllocation) override;
|
2022-06-08 00:30:59 +08:00
|
|
|
|
2022-02-04 21:59:01 +08:00
|
|
|
GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, AllocationType allocType) override { return nullptr; }
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2020-01-07 14:42:40 +08:00
|
|
|
uint64_t getSystemSharedMemory(uint32_t rootDeviceIndex) override;
|
2020-10-30 17:27:48 +08:00
|
|
|
uint64_t getLocalMemorySize(uint32_t rootDeviceIndex, uint32_t deviceBitfield) override;
|
2021-07-27 22:24:20 +08:00
|
|
|
double getPercentOfGlobalMemoryAvailable(uint32_t rootDeviceIndex) override;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2020-01-07 14:42:40 +08:00
|
|
|
AllocationStatus populateOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override;
|
2019-11-15 16:59:48 +08:00
|
|
|
void cleanOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override;
|
2020-02-12 00:48:40 +08:00
|
|
|
void commonCleanup() override;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
// drm/i915 ioctl wrappers
|
2019-08-27 21:33:58 +08:00
|
|
|
MOCKABLE_VIRTUAL uint32_t unreference(BufferObject *bo, bool synchronousDestroy);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2023-02-01 23:58:22 +08:00
|
|
|
void registerIpcExportedAllocation(GraphicsAllocation *graphicsAllocation) override;
|
|
|
|
|
2018-02-28 19:09:48 +08:00
|
|
|
bool isValidateHostMemoryEnabled() const {
|
|
|
|
return validateHostPtrMemory;
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2019-02-25 21:11:34 +08:00
|
|
|
DrmGemCloseWorker *peekGemCloseWorker() const { return this->gemCloseWorker.get(); }
|
2020-10-07 21:32:03 +08:00
|
|
|
bool copyMemoryToAllocation(GraphicsAllocation *graphicsAllocation, size_t destinationOffset, const void *memoryToCopy, size_t sizeToCopy) override;
|
2021-10-21 19:16:19 +08:00
|
|
|
bool copyMemoryToAllocationBanks(GraphicsAllocation *graphicsAllocation, size_t destinationOffset, const void *memoryToCopy, size_t sizeToCopy, DeviceBitfield handleMask) override;
|
2018-05-11 15:57:59 +08:00
|
|
|
|
2021-05-24 14:14:30 +08:00
|
|
|
MOCKABLE_VIRTUAL int obtainFdFromHandle(int boHandle, uint32_t rootDeviceindex);
|
2023-03-08 07:04:46 +08:00
|
|
|
AddressRange reserveGpuAddress(const uint64_t requiredStartAddress, size_t size, RootDeviceIndicesContainer rootDeviceIndices, uint32_t *reservedOnRootDeviceIndex) override;
|
2023-10-12 08:51:45 +08:00
|
|
|
AddressRange reserveGpuAddressOnHeap(const uint64_t requiredStartAddress, size_t size, RootDeviceIndicesContainer rootDeviceIndices, uint32_t *reservedOnRootDeviceIndex, HeapIndex heap, size_t alignment) override;
|
|
|
|
size_t selectAlignmentAndHeap(size_t size, HeapIndex *heap) override;
|
2020-07-01 16:38:19 +08:00
|
|
|
void freeGpuAddress(AddressRange addressRange, uint32_t rootDeviceIndex) override;
|
2023-04-26 18:36:25 +08:00
|
|
|
MOCKABLE_VIRTUAL BufferObject *createBufferObjectInMemoryRegion(uint32_t rootDeviceIndex, Gmm *gmm, AllocationType allocationType, uint64_t gpuAddress, size_t size,
|
2023-10-12 22:06:59 +08:00
|
|
|
uint32_t memoryBanks, size_t maxOsContextCount, int32_t pairHandle, bool isSystemMemoryPool);
|
2019-06-06 22:26:47 +08:00
|
|
|
|
2023-02-28 08:20:17 +08:00
|
|
|
bool hasPageFaultsEnabled(const Device &neoDevice) override;
|
2021-02-03 22:53:13 +08:00
|
|
|
bool isKmdMigrationAvailable(uint32_t rootDeviceIndex) override;
|
|
|
|
|
2021-10-11 17:27:26 +08:00
|
|
|
bool setMemAdvise(GraphicsAllocation *gfxAllocation, MemAdviseFlags flags, uint32_t rootDeviceIndex) override;
|
2022-11-30 08:48:56 +08:00
|
|
|
bool setMemPrefetch(GraphicsAllocation *gfxAllocation, SubDeviceIdsVec &subDeviceIds, uint32_t rootDeviceIndex) override;
|
2023-09-21 06:34:19 +08:00
|
|
|
bool setAtomicAccess(GraphicsAllocation *gfxAllocation, size_t size, AtomicAccessMode mode, uint32_t rootDeviceIndex) override;
|
2022-09-01 23:54:57 +08:00
|
|
|
[[nodiscard]] std::unique_lock<std::mutex> acquireAllocLock();
|
2020-09-10 18:36:44 +08:00
|
|
|
std::vector<GraphicsAllocation *> &getSysMemAllocs();
|
|
|
|
std::vector<GraphicsAllocation *> &getLocalMemAllocs(uint32_t rootDeviceIndex);
|
2022-11-15 22:34:44 +08:00
|
|
|
AllocationStatus registerSysMemAlloc(GraphicsAllocation *allocation) override;
|
|
|
|
AllocationStatus registerLocalMemAlloc(GraphicsAllocation *allocation, uint32_t rootDeviceIndex) override;
|
2020-09-10 18:36:44 +08:00
|
|
|
void unregisterAllocation(GraphicsAllocation *allocation);
|
|
|
|
|
2021-05-28 01:44:47 +08:00
|
|
|
static std::unique_ptr<MemoryManager> create(ExecutionEnvironment &executionEnvironment);
|
|
|
|
|
2022-12-08 08:09:22 +08:00
|
|
|
DrmAllocation *createUSMHostAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool hasMappedPtr, bool reuseSharedAllocation);
|
2021-10-07 15:26:03 +08:00
|
|
|
void releaseDeviceSpecificMemResources(uint32_t rootDeviceIndex) override;
|
|
|
|
void createDeviceSpecificMemResources(uint32_t rootDeviceIndex) override;
|
2022-07-27 00:39:17 +08:00
|
|
|
bool allowIndirectAllocationsAsPack(uint32_t rootDeviceIndex) override;
|
2023-07-26 08:59:11 +08:00
|
|
|
Drm &getDrm(uint32_t rootDeviceIndex) const;
|
2023-12-04 04:04:30 +08:00
|
|
|
size_t getSizeOfChunk(size_t allocSize);
|
|
|
|
bool checkAllocationForChunking(size_t allocSize, size_t minSize, bool subDeviceEnabled, bool debugDisabled, bool modeEnabled, bool bufferEnabled);
|
2021-05-24 14:14:30 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
protected:
|
2023-02-01 23:58:22 +08:00
|
|
|
void registerSharedBoHandleAllocation(DrmAllocation *drmAllocation);
|
|
|
|
BufferObjectHandleWrapper tryToGetBoHandleWrapperWithSharedOwnership(int boHandle);
|
|
|
|
void eraseSharedBoHandleWrapper(int boHandle);
|
|
|
|
|
2022-02-01 07:29:01 +08:00
|
|
|
MOCKABLE_VIRTUAL BufferObject *findAndReferenceSharedBufferObject(int boHandle, uint32_t rootDeviceIndex);
|
2017-12-21 07:45:38 +08:00
|
|
|
void eraseSharedBufferObject(BufferObject *bo);
|
|
|
|
void pushSharedBufferObject(BufferObject *bo);
|
2022-06-07 01:09:35 +08:00
|
|
|
BufferObject *allocUserptr(uintptr_t address, size_t size, uint32_t rootDeviceIndex);
|
2018-02-27 06:23:43 +08:00
|
|
|
bool setDomainCpu(GraphicsAllocation &graphicsAllocation, bool writeEnable);
|
2021-03-10 06:41:46 +08:00
|
|
|
uint64_t acquireGpuRange(size_t &size, uint32_t rootDeviceIndex, HeapIndex heapIndex);
|
2023-01-13 18:05:49 +08:00
|
|
|
uint64_t acquireGpuRangeWithCustomAlignment(size_t &size, uint32_t rootDeviceIndex, HeapIndex heapIndex, size_t alignment);
|
2019-10-22 16:26:23 +08:00
|
|
|
MOCKABLE_VIRTUAL void releaseGpuRange(void *address, size_t size, uint32_t rootDeviceIndex);
|
2019-02-25 21:11:34 +08:00
|
|
|
void emitPinningRequest(BufferObject *bo, const AllocationData &allocationData) const;
|
2021-04-20 20:24:04 +08:00
|
|
|
uint32_t getDefaultDrmContextId(uint32_t rootDeviceIndex) const;
|
2023-04-26 18:36:25 +08:00
|
|
|
OsContextLinux *getDefaultOsContext(uint32_t rootDeviceIndex) const;
|
2020-08-28 14:37:26 +08:00
|
|
|
size_t getUserptrAlignment();
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2022-07-27 00:39:17 +08:00
|
|
|
GraphicsAllocation *createGraphicsAllocation(OsHandleStorage &handleStorage, const AllocationData &allocationData) override;
|
|
|
|
GraphicsAllocation *allocateGraphicsMemoryForNonSvmHostPtr(const AllocationData &allocationData) override;
|
|
|
|
GraphicsAllocation *allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) override;
|
2021-05-05 02:17:30 +08:00
|
|
|
DrmAllocation *allocateGraphicsMemoryWithAlignmentImpl(const AllocationData &allocationData);
|
2020-10-05 15:57:50 +08:00
|
|
|
DrmAllocation *createAllocWithAlignmentFromUserptr(const AllocationData &allocationData, size_t size, size_t alignment, size_t alignedSVMSize, uint64_t gpuAddress);
|
2020-10-13 20:24:49 +08:00
|
|
|
DrmAllocation *createAllocWithAlignment(const AllocationData &allocationData, size_t size, size_t alignment, size_t alignedSize, uint64_t gpuAddress);
|
2021-05-05 02:17:30 +08:00
|
|
|
DrmAllocation *createMultiHostAllocation(const AllocationData &allocationData);
|
2020-10-05 15:57:50 +08:00
|
|
|
void obtainGpuAddress(const AllocationData &allocationData, BufferObject *bo, uint64_t gpuAddress);
|
2022-07-27 00:39:17 +08:00
|
|
|
GraphicsAllocation *allocateUSMHostGraphicsMemory(const AllocationData &allocationData) override;
|
|
|
|
GraphicsAllocation *allocateGraphicsMemoryWithHostPtr(const AllocationData &allocationData) override;
|
|
|
|
GraphicsAllocation *allocateGraphicsMemory64kb(const AllocationData &allocationData) override;
|
2021-08-11 18:36:04 +08:00
|
|
|
GraphicsAllocation *allocateMemoryByKMD(const AllocationData &allocationData) override;
|
2022-12-07 06:36:12 +08:00
|
|
|
GraphicsAllocation *allocatePhysicalDeviceMemory(const AllocationData &allocationData, AllocationStatus &status) override;
|
|
|
|
GraphicsAllocation *allocatePhysicalLocalDeviceMemory(const AllocationData &allocationData, AllocationStatus &status) override;
|
|
|
|
bool mapPhysicalToVirtualMemory(GraphicsAllocation *physicalAllocation, uint64_t gpuRange, size_t bufferSize) override;
|
|
|
|
void unMapPhysicalToVirtualMemory(GraphicsAllocation *physicalAllocation, uint64_t gpuRange, size_t bufferSize, OsContext *osContext, uint32_t rootDeviceIndex) override;
|
2019-01-22 19:40:17 +08:00
|
|
|
GraphicsAllocation *allocateGraphicsMemoryForImageImpl(const AllocationData &allocationData, std::unique_ptr<Gmm> gmm) override;
|
2020-07-01 16:38:19 +08:00
|
|
|
GraphicsAllocation *allocateGraphicsMemoryWithGpuVa(const AllocationData &allocationData) override;
|
2020-10-22 20:13:44 +08:00
|
|
|
GraphicsAllocation *createSharedUnifiedMemoryAllocation(const AllocationData &allocationData);
|
2018-11-30 18:01:33 +08:00
|
|
|
|
2019-01-24 18:51:33 +08:00
|
|
|
void *lockResourceImpl(GraphicsAllocation &graphicsAllocation) override;
|
2022-06-07 01:43:28 +08:00
|
|
|
MOCKABLE_VIRTUAL void *lockBufferObject(BufferObject *bo);
|
|
|
|
MOCKABLE_VIRTUAL void unlockBufferObject(BufferObject *bo);
|
2019-01-24 18:51:33 +08:00
|
|
|
void unlockResourceImpl(GraphicsAllocation &graphicsAllocation) override;
|
2023-09-16 01:20:28 +08:00
|
|
|
GraphicsAllocation *allocate32BitGraphicsMemoryImpl(const AllocationData &allocationData) override;
|
2022-04-11 11:40:18 +08:00
|
|
|
void cleanupBeforeReturn(const AllocationData &allocationData, GfxPartition *gfxPartition, DrmAllocation *drmAllocation, GraphicsAllocation *graphicsAllocation, uint64_t &gpuAddress, size_t &sizeAllocated);
|
2019-02-27 04:12:26 +08:00
|
|
|
GraphicsAllocation *allocateGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) override;
|
2023-03-08 12:06:00 +08:00
|
|
|
bool createDrmChunkedAllocation(Drm *drm, DrmAllocation *allocation, uint64_t boAddress, size_t boSize, size_t maxOsContextCount);
|
2020-09-02 14:12:46 +08:00
|
|
|
bool createDrmAllocation(Drm *drm, DrmAllocation *allocation, uint64_t gpuAddress, size_t maxOsContextCount);
|
2020-10-09 16:48:37 +08:00
|
|
|
void registerAllocationInOs(GraphicsAllocation *allocation) override;
|
2022-01-20 02:14:10 +08:00
|
|
|
void waitOnCompletionFence(GraphicsAllocation *allocation);
|
2022-02-04 21:59:01 +08:00
|
|
|
bool allocationTypeForCompletionFence(AllocationType allocationType);
|
2022-11-15 22:34:44 +08:00
|
|
|
bool makeAllocationResident(GraphicsAllocation *allocation);
|
2019-01-24 18:51:33 +08:00
|
|
|
|
2023-03-28 17:13:07 +08:00
|
|
|
inline std::unique_ptr<Gmm> makeGmmIfSingleHandle(const AllocationData &allocationData, size_t sizeAligned);
|
|
|
|
inline std::unique_ptr<DrmAllocation> makeDrmAllocation(const AllocationData &allocationData, std::unique_ptr<Gmm> gmm, uint64_t gpuAddress, size_t sizeAligned);
|
2020-03-19 17:41:35 +08:00
|
|
|
uint32_t getRootDeviceIndex(const Drm *drm);
|
2021-10-07 15:26:03 +08:00
|
|
|
BufferObject *createRootDeviceBufferObject(uint32_t rootDeviceIndex);
|
|
|
|
void releaseBufferObject(uint32_t rootDeviceIndex);
|
2021-11-30 01:48:55 +08:00
|
|
|
bool retrieveMmapOffsetForBufferObject(uint32_t rootDeviceIndex, BufferObject &bo, uint64_t flags, uint64_t &offset);
|
2020-01-07 14:42:40 +08:00
|
|
|
|
2020-02-21 20:41:02 +08:00
|
|
|
std::vector<BufferObject *> pinBBs;
|
|
|
|
std::vector<void *> memoryForPinBBs;
|
2017-12-21 07:45:38 +08:00
|
|
|
size_t pinThreshold = 8 * 1024 * 1024;
|
2018-02-28 19:09:48 +08:00
|
|
|
bool forcePinEnabled = false;
|
2020-10-01 14:13:09 +08:00
|
|
|
const bool validateHostPtrMemory;
|
2017-12-21 07:45:38 +08:00
|
|
|
std::unique_ptr<DrmGemCloseWorker> gemCloseWorker;
|
2020-06-02 01:00:59 +08:00
|
|
|
decltype(&mmap) mmapFunction = mmap;
|
|
|
|
decltype(&munmap) munmapFunction = munmap;
|
2017-12-21 07:45:38 +08:00
|
|
|
decltype(&close) closeFunction = close;
|
|
|
|
std::vector<BufferObject *> sharingBufferObjects;
|
2018-08-01 20:47:14 +08:00
|
|
|
std::mutex mtx;
|
2020-09-10 18:36:44 +08:00
|
|
|
|
2023-02-01 23:58:22 +08:00
|
|
|
std::map<int, BufferObjectHandleWrapper> sharedBoHandles;
|
2020-09-10 18:36:44 +08:00
|
|
|
std::vector<std::vector<GraphicsAllocation *>> localMemAllocs;
|
|
|
|
std::vector<GraphicsAllocation *> sysMemAllocs;
|
|
|
|
std::mutex allocMutex;
|
2017-12-21 07:45:38 +08:00
|
|
|
};
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|