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