2017-12-21 00:45:38 +01:00
|
|
|
/*
|
2024-01-03 13:15:24 +00:00
|
|
|
* Copyright (C) 2019-2024 Intel Corporation
|
2017-12-21 00:45:38 +01:00
|
|
|
*
|
2018-12-06 15:33:02 +01:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 00:45:38 +01:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
2022-11-22 13:53:59 +00:00
|
|
|
#include "shared/source/command_stream/task_count_helper.h"
|
2023-01-24 15:33:52 +00:00
|
|
|
#include "shared/source/helpers/device_bitfield.h"
|
2020-06-29 12:47:13 +02:00
|
|
|
#include "shared/source/memory_manager/multi_graphics_allocation.h"
|
2020-03-12 16:33:22 -07:00
|
|
|
#include "shared/source/memory_manager/residency_container.h"
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/unified_memory/unified_memory.h"
|
2023-12-13 11:34:50 +00:00
|
|
|
#include "shared/source/utilities/sorted_vector.h"
|
2019-06-03 10:50:21 +02:00
|
|
|
|
2019-12-05 10:32:42 +01:00
|
|
|
#include "memory_properties_flags.h"
|
|
|
|
|
|
2022-07-27 15:41:20 +00:00
|
|
|
#include <atomic>
|
2017-12-21 00:45:38 +01:00
|
|
|
#include <cstdint>
|
|
|
|
|
#include <map>
|
2023-11-23 09:01:31 +00:00
|
|
|
#include <memory>
|
2017-12-21 00:45:38 +01:00
|
|
|
#include <mutex>
|
2022-02-23 16:27:17 +00:00
|
|
|
#include <shared_mutex>
|
2023-11-08 14:04:01 +00:00
|
|
|
#include <type_traits>
|
2017-12-21 00:45:38 +01:00
|
|
|
|
2019-03-26 11:59:46 +01:00
|
|
|
namespace NEO {
|
2019-04-25 10:11:25 +02:00
|
|
|
class CommandStreamReceiver;
|
2017-12-21 00:45:38 +01:00
|
|
|
class GraphicsAllocation;
|
|
|
|
|
class MemoryManager;
|
2020-12-23 13:47:18 +00:00
|
|
|
class Device;
|
2023-05-24 02:07:44 +00:00
|
|
|
struct VirtualMemoryReservation;
|
2017-12-21 00:45:38 +01:00
|
|
|
|
2019-03-06 16:35:21 +01:00
|
|
|
struct SvmAllocationData {
|
2020-06-29 12:47:13 +02:00
|
|
|
SvmAllocationData(uint32_t maxRootDeviceIndex) : gpuAllocations(maxRootDeviceIndex), maxRootDeviceIndex(maxRootDeviceIndex){};
|
|
|
|
|
SvmAllocationData(const SvmAllocationData &svmAllocData) : SvmAllocationData(svmAllocData.maxRootDeviceIndex) {
|
|
|
|
|
this->allocationFlagsProperty = svmAllocData.allocationFlagsProperty;
|
|
|
|
|
this->cpuAllocation = svmAllocData.cpuAllocation;
|
|
|
|
|
this->device = svmAllocData.device;
|
|
|
|
|
this->size = svmAllocData.size;
|
|
|
|
|
this->memoryType = svmAllocData.memoryType;
|
2021-09-15 10:52:31 +00:00
|
|
|
this->allocId = svmAllocData.allocId;
|
2022-01-25 05:42:00 +00:00
|
|
|
this->pageSizeForAlignment = svmAllocData.pageSizeForAlignment;
|
2022-03-02 03:43:59 +00:00
|
|
|
this->isImportedAllocation = svmAllocData.isImportedAllocation;
|
2020-06-29 12:47:13 +02:00
|
|
|
for (auto allocation : svmAllocData.gpuAllocations.getGraphicsAllocations()) {
|
|
|
|
|
if (allocation) {
|
|
|
|
|
this->gpuAllocations.addAllocation(allocation);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-04 01:40:52 +00:00
|
|
|
this->mappedAllocData = svmAllocData.mappedAllocData;
|
2023-05-24 02:07:44 +00:00
|
|
|
this->virtualReservationData = svmAllocData.virtualReservationData;
|
2020-06-29 12:47:13 +02:00
|
|
|
}
|
2020-07-06 13:56:33 +02:00
|
|
|
SvmAllocationData &operator=(const SvmAllocationData &) = delete;
|
2019-03-06 16:35:21 +01:00
|
|
|
GraphicsAllocation *cpuAllocation = nullptr;
|
2020-06-29 12:47:13 +02:00
|
|
|
MultiGraphicsAllocation gpuAllocations;
|
2023-05-24 02:07:44 +00:00
|
|
|
VirtualMemoryReservation *virtualReservationData = nullptr;
|
2019-03-06 16:35:21 +01:00
|
|
|
size_t size = 0;
|
2022-01-25 05:42:00 +00:00
|
|
|
size_t pageSizeForAlignment = 0;
|
2023-12-13 10:09:37 +00:00
|
|
|
InternalMemoryType memoryType = InternalMemoryType::svm;
|
2020-04-22 14:37:30 +02:00
|
|
|
MemoryProperties allocationFlagsProperty;
|
2020-12-23 13:47:18 +00:00
|
|
|
Device *device = nullptr;
|
2022-03-02 03:43:59 +00:00
|
|
|
bool isImportedAllocation = false;
|
2021-09-15 10:52:31 +00:00
|
|
|
void setAllocId(uint32_t id) {
|
|
|
|
|
allocId = id;
|
|
|
|
|
}
|
2023-05-04 01:40:52 +00:00
|
|
|
bool mappedAllocData = false;
|
2021-09-15 10:52:31 +00:00
|
|
|
|
|
|
|
|
uint32_t getAllocId() {
|
|
|
|
|
return allocId;
|
|
|
|
|
}
|
2020-06-29 12:47:13 +02:00
|
|
|
|
2022-10-27 10:40:20 +00:00
|
|
|
static constexpr uint32_t uninitializedAllocId = std::numeric_limits<uint32_t>::max();
|
|
|
|
|
|
2020-06-29 12:47:13 +02:00
|
|
|
protected:
|
|
|
|
|
const uint32_t maxRootDeviceIndex;
|
2022-10-27 10:40:20 +00:00
|
|
|
uint32_t allocId = uninitializedAllocId;
|
2019-03-06 16:35:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct SvmMapOperation {
|
|
|
|
|
void *regionSvmPtr = nullptr;
|
|
|
|
|
size_t regionSize = 0;
|
|
|
|
|
void *baseSvmPtr = nullptr;
|
|
|
|
|
size_t offset = 0;
|
|
|
|
|
bool readOnlyMap = false;
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
class SVMAllocsManager {
|
|
|
|
|
public:
|
2024-01-03 13:15:24 +00:00
|
|
|
using SortedVectorBasedAllocationTracker = BaseSortedPointerWithValueVector<SvmAllocationData>;
|
2023-11-23 09:01:31 +00:00
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
class MapBasedAllocationTracker {
|
2019-06-11 11:17:01 +02:00
|
|
|
friend class SVMAllocsManager;
|
|
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
public:
|
2019-03-06 16:35:21 +01:00
|
|
|
using SvmAllocationContainer = std::map<const void *, SvmAllocationData>;
|
2023-10-04 08:58:05 +00:00
|
|
|
void insert(const SvmAllocationData &);
|
|
|
|
|
void remove(const SvmAllocationData &);
|
2019-03-06 16:35:21 +01:00
|
|
|
SvmAllocationData *get(const void *);
|
|
|
|
|
size_t getNumAllocs() const { return allocations.size(); };
|
|
|
|
|
|
|
|
|
|
SvmAllocationContainer allocations;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct MapOperationsTracker {
|
|
|
|
|
using SvmMapOperationsContainer = std::map<const void *, SvmMapOperation>;
|
|
|
|
|
void insert(SvmMapOperation);
|
|
|
|
|
void remove(const void *);
|
|
|
|
|
SvmMapOperation *get(const void *);
|
|
|
|
|
size_t getNumMapOperations() const { return operations.size(); };
|
2017-12-21 00:45:38 +01:00
|
|
|
|
|
|
|
|
protected:
|
2019-03-06 16:35:21 +01:00
|
|
|
SvmMapOperationsContainer operations;
|
2017-12-21 00:45:38 +01:00
|
|
|
};
|
|
|
|
|
|
2019-04-17 10:02:31 +02:00
|
|
|
struct SvmAllocationProperties {
|
|
|
|
|
bool coherent = false;
|
|
|
|
|
bool hostPtrReadOnly = false;
|
|
|
|
|
bool readOnly = false;
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-24 15:13:30 +00:00
|
|
|
struct InternalAllocationsTracker {
|
2022-11-22 13:53:59 +00:00
|
|
|
TaskCountType latestSentTaskCount = 0lu;
|
|
|
|
|
TaskCountType latestResidentObjectId = 0lu;
|
2022-01-24 15:13:30 +00:00
|
|
|
};
|
|
|
|
|
|
2019-06-11 11:17:01 +02:00
|
|
|
struct UnifiedMemoryProperties {
|
2020-12-23 13:47:18 +00:00
|
|
|
UnifiedMemoryProperties(InternalMemoryType memoryType,
|
2023-03-31 14:23:21 +00:00
|
|
|
size_t alignment,
|
2022-04-07 13:09:40 +00:00
|
|
|
const RootDeviceIndicesContainer &rootDeviceIndices,
|
2020-12-23 13:47:18 +00:00
|
|
|
const std::map<uint32_t, DeviceBitfield> &subdeviceBitfields) : memoryType(memoryType),
|
2023-03-31 14:23:21 +00:00
|
|
|
alignment(alignment),
|
2020-12-23 13:47:18 +00:00
|
|
|
rootDeviceIndices(rootDeviceIndices),
|
|
|
|
|
subdeviceBitfields(subdeviceBitfields){};
|
2023-06-01 15:55:58 +00:00
|
|
|
uint32_t getRootDeviceIndex() const;
|
2023-12-13 10:09:37 +00:00
|
|
|
InternalMemoryType memoryType = InternalMemoryType::notSpecified;
|
2020-04-22 14:37:30 +02:00
|
|
|
MemoryProperties allocationFlags;
|
2020-12-23 13:47:18 +00:00
|
|
|
Device *device = nullptr;
|
2023-03-31 14:23:21 +00:00
|
|
|
size_t alignment;
|
2022-04-07 13:09:40 +00:00
|
|
|
const RootDeviceIndicesContainer &rootDeviceIndices;
|
2020-12-23 13:47:18 +00:00
|
|
|
const std::map<uint32_t, DeviceBitfield> &subdeviceBitfields;
|
2023-12-11 14:24:36 +00:00
|
|
|
AllocationType requestedAllocationType = AllocationType::unknown;
|
2019-06-11 11:17:01 +02:00
|
|
|
};
|
|
|
|
|
|
2022-04-14 15:23:52 +00:00
|
|
|
struct SvmCacheAllocationInfo {
|
|
|
|
|
size_t allocationSize;
|
|
|
|
|
void *allocation;
|
|
|
|
|
SvmCacheAllocationInfo(size_t allocationSize, void *allocation) : allocationSize(allocationSize), allocation(allocation) {}
|
2023-04-18 20:01:15 -05:00
|
|
|
bool operator<(SvmCacheAllocationInfo const &other) const {
|
2022-04-14 15:23:52 +00:00
|
|
|
return allocationSize < other.allocationSize;
|
|
|
|
|
}
|
2023-04-18 20:01:15 -05:00
|
|
|
bool operator<(size_t const &size) const {
|
2022-04-14 15:23:52 +00:00
|
|
|
return allocationSize < size;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct SvmAllocationCache {
|
2024-02-05 15:53:23 +00:00
|
|
|
bool insert(size_t size, void *);
|
2022-04-14 15:23:52 +00:00
|
|
|
void *get(size_t size, const UnifiedMemoryProperties &unifiedMemoryProperties, SVMAllocsManager *svmAllocsManager);
|
|
|
|
|
void trim(SVMAllocsManager *svmAllocsManager);
|
|
|
|
|
std::vector<SvmCacheAllocationInfo> allocations;
|
|
|
|
|
std::mutex mtx;
|
2024-02-05 15:53:23 +00:00
|
|
|
size_t maxSize = 0;
|
|
|
|
|
size_t totalSize = 0;
|
2022-04-14 15:23:52 +00:00
|
|
|
};
|
|
|
|
|
|
2023-01-03 18:39:11 +00:00
|
|
|
enum class FreePolicyType : uint32_t {
|
2023-12-13 16:09:52 +00:00
|
|
|
none = 0,
|
|
|
|
|
blocking = 1,
|
|
|
|
|
defer = 2
|
2023-01-03 18:39:11 +00:00
|
|
|
};
|
|
|
|
|
|
2021-03-08 12:27:14 +00:00
|
|
|
SVMAllocsManager(MemoryManager *memoryManager, bool multiOsContextSupport);
|
2022-04-14 15:23:52 +00:00
|
|
|
MOCKABLE_VIRTUAL ~SVMAllocsManager();
|
2020-12-23 13:47:18 +00:00
|
|
|
void *createSVMAlloc(size_t size,
|
2020-07-08 18:31:52 -07:00
|
|
|
const SvmAllocationProperties svmProperties,
|
2022-04-07 13:09:40 +00:00
|
|
|
const RootDeviceIndicesContainer &rootDeviceIndices,
|
2020-12-23 13:47:18 +00:00
|
|
|
const std::map<uint32_t, DeviceBitfield> &subdeviceBitfields);
|
2021-03-15 23:46:09 +00:00
|
|
|
MOCKABLE_VIRTUAL void *createHostUnifiedMemoryAllocation(size_t size,
|
2022-12-21 04:39:44 +01:00
|
|
|
const UnifiedMemoryProperties &svmProperties);
|
2021-03-13 01:07:50 +00:00
|
|
|
MOCKABLE_VIRTUAL void *createUnifiedMemoryAllocation(size_t size,
|
2022-12-21 04:39:44 +01:00
|
|
|
const UnifiedMemoryProperties &svmProperties);
|
2021-03-13 01:07:50 +00:00
|
|
|
MOCKABLE_VIRTUAL void *createSharedUnifiedMemoryAllocation(size_t size,
|
|
|
|
|
const UnifiedMemoryProperties &svmProperties,
|
2022-12-21 04:39:44 +01:00
|
|
|
void *cmdQ);
|
2020-12-23 13:47:18 +00:00
|
|
|
void *createUnifiedKmdMigratedAllocation(size_t size,
|
2020-10-22 14:13:44 +02:00
|
|
|
const SvmAllocationProperties &svmProperties,
|
2022-12-21 04:39:44 +01:00
|
|
|
const UnifiedMemoryProperties &unifiedMemoryProperties);
|
2023-11-08 14:04:01 +00:00
|
|
|
|
2020-10-22 14:13:44 +02:00
|
|
|
void setUnifiedAllocationProperties(GraphicsAllocation *allocation, const SvmAllocationProperties &svmProperties);
|
2023-11-08 14:04:01 +00:00
|
|
|
|
|
|
|
|
template <typename T,
|
|
|
|
|
std::enable_if_t<std::is_same_v<T, void> || std::is_same_v<T, const void>, int> = 0>
|
|
|
|
|
SvmAllocationData *getSVMAlloc(T *ptr) {
|
|
|
|
|
std::shared_lock<std::shared_mutex> lock(mtx);
|
|
|
|
|
return svmAllocs.get(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T,
|
|
|
|
|
std::enable_if_t<std::is_same_v<T, void *>, int> = 0>
|
|
|
|
|
SvmAllocationData *getSVMDeferFreeAlloc(T ptr) {
|
|
|
|
|
std::shared_lock<std::shared_mutex> lock(mtx);
|
|
|
|
|
return svmDeferFreeAllocs.get(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 23:41:51 +00:00
|
|
|
MOCKABLE_VIRTUAL bool freeSVMAlloc(void *ptr, bool blocking);
|
2023-01-03 18:39:11 +00:00
|
|
|
MOCKABLE_VIRTUAL bool freeSVMAllocDefer(void *ptr);
|
|
|
|
|
MOCKABLE_VIRTUAL void freeSVMAllocDeferImpl();
|
|
|
|
|
MOCKABLE_VIRTUAL void freeSVMAllocImpl(void *ptr, FreePolicyType policy, SvmAllocationData *svmData);
|
2020-02-11 13:54:40 +01:00
|
|
|
bool freeSVMAlloc(void *ptr) { return freeSVMAlloc(ptr, false); }
|
2022-04-14 15:23:52 +00:00
|
|
|
void trimUSMDeviceAllocCache();
|
2024-01-31 13:07:07 +00:00
|
|
|
void trimUSMHostAllocCache();
|
2020-05-07 18:08:36 +05:30
|
|
|
void insertSVMAlloc(const SvmAllocationData &svmData);
|
|
|
|
|
void removeSVMAlloc(const SvmAllocationData &svmData);
|
2023-04-28 09:38:31 +00:00
|
|
|
size_t getNumAllocs() const { return svmAllocs.getNumAllocs(); }
|
|
|
|
|
MOCKABLE_VIRTUAL size_t getNumDeferFreeAllocs() const { return svmDeferFreeAllocs.getNumAllocs(); }
|
2023-11-23 09:01:31 +00:00
|
|
|
SortedVectorBasedAllocationTracker *getSVMAllocs() { return &svmAllocs; }
|
2019-03-06 16:35:21 +01:00
|
|
|
|
2020-09-14 10:57:27 +02:00
|
|
|
MOCKABLE_VIRTUAL void insertSvmMapOperation(void *regionSvmPtr, size_t regionSize, void *baseSvmPtr, size_t offset, bool readOnlyMap);
|
2019-03-06 16:35:21 +01:00
|
|
|
void removeSvmMapOperation(const void *regionSvmPtr);
|
|
|
|
|
SvmMapOperation *getSvmMapOperation(const void *regionPtr);
|
2022-09-22 10:46:22 +00:00
|
|
|
MOCKABLE_VIRTUAL void addInternalAllocationsToResidencyContainer(uint32_t rootDeviceIndex,
|
|
|
|
|
ResidencyContainer &residencyContainer,
|
|
|
|
|
uint32_t requestedTypesMask);
|
2019-06-17 13:31:23 +02:00
|
|
|
void makeInternalAllocationsResident(CommandStreamReceiver &commandStreamReceiver, uint32_t requestedTypesMask);
|
2022-12-21 04:39:44 +01:00
|
|
|
void *createUnifiedAllocationWithDeviceStorage(size_t size, const SvmAllocationProperties &svmProperties, const UnifiedMemoryProperties &unifiedMemoryProperties);
|
2019-12-03 19:04:55 -08:00
|
|
|
void freeSvmAllocationWithDeviceStorage(SvmAllocationData *svmData);
|
2021-01-11 17:42:33 +00:00
|
|
|
bool hasHostAllocations();
|
2021-09-15 10:52:31 +00:00
|
|
|
std::atomic<uint32_t> allocationsCounter = 0;
|
2022-11-22 13:53:59 +00:00
|
|
|
MOCKABLE_VIRTUAL void makeIndirectAllocationsResident(CommandStreamReceiver &commandStreamReceiver, TaskCountType taskCount);
|
2022-01-24 15:13:30 +00:00
|
|
|
void prepareIndirectAllocationForDestruction(SvmAllocationData *);
|
2023-08-08 08:25:58 +00:00
|
|
|
MOCKABLE_VIRTUAL void prefetchMemory(Device &device, CommandStreamReceiver &commandStreamReceiver, SvmAllocationData &svmData);
|
2023-04-10 22:40:54 +00:00
|
|
|
void prefetchSVMAllocs(Device &device, CommandStreamReceiver &commandStreamReceiver);
|
2022-09-22 10:46:22 +00:00
|
|
|
std::unique_lock<std::mutex> obtainOwnership();
|
2022-01-24 15:13:30 +00:00
|
|
|
|
|
|
|
|
std::map<CommandStreamReceiver *, InternalAllocationsTracker> indirectAllocationsResidency;
|
2017-12-21 00:45:38 +01:00
|
|
|
|
2022-03-09 14:55:32 +00:00
|
|
|
using NonGpuDomainAllocsContainer = std::vector<void *>;
|
|
|
|
|
NonGpuDomainAllocsContainer nonGpuDomainAllocs;
|
|
|
|
|
|
2024-02-05 15:53:23 +00:00
|
|
|
void initUsmAllocationsCaches(Device &device);
|
|
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
protected:
|
2020-12-23 13:47:18 +00:00
|
|
|
void *createZeroCopySvmAllocation(size_t size, const SvmAllocationProperties &svmProperties,
|
2022-04-07 13:09:40 +00:00
|
|
|
const RootDeviceIndicesContainer &rootDeviceIndices,
|
2020-12-23 13:47:18 +00:00
|
|
|
const std::map<uint32_t, DeviceBitfield> &subdeviceBitfields);
|
2022-02-04 13:59:01 +00:00
|
|
|
AllocationType getGraphicsAllocationTypeAndCompressionPreference(const UnifiedMemoryProperties &unifiedMemoryProperties, bool &compressionEnabled) const;
|
2019-06-11 11:17:01 +02:00
|
|
|
|
2019-03-06 16:35:21 +01:00
|
|
|
void freeZeroCopySvmAllocation(SvmAllocationData *svmData);
|
|
|
|
|
|
2024-02-05 15:53:23 +00:00
|
|
|
void initUsmDeviceAllocationsCache(Device &device);
|
2024-01-31 13:07:07 +00:00
|
|
|
void initUsmHostAllocationsCache();
|
2022-12-02 07:25:45 +00:00
|
|
|
void freeSVMData(SvmAllocationData *svmData);
|
2022-04-14 15:23:52 +00:00
|
|
|
|
2023-11-23 09:01:31 +00:00
|
|
|
SortedVectorBasedAllocationTracker svmAllocs;
|
2019-03-06 16:35:21 +01:00
|
|
|
MapOperationsTracker svmMapOperations;
|
2023-04-28 09:38:31 +00:00
|
|
|
MapBasedAllocationTracker svmDeferFreeAllocs;
|
2017-12-21 00:45:38 +01:00
|
|
|
MemoryManager *memoryManager;
|
2022-09-20 15:32:40 +02:00
|
|
|
std::shared_mutex mtx;
|
2022-09-22 10:46:22 +00:00
|
|
|
std::mutex mtxForIndirectAccess;
|
2021-03-08 12:27:14 +00:00
|
|
|
bool multiOsContextSupport;
|
2022-04-14 15:23:52 +00:00
|
|
|
SvmAllocationCache usmDeviceAllocationsCache;
|
2024-01-31 13:07:07 +00:00
|
|
|
SvmAllocationCache usmHostAllocationsCache;
|
2022-04-14 15:23:52 +00:00
|
|
|
bool usmDeviceAllocationsCacheEnabled = false;
|
2024-01-31 13:07:07 +00:00
|
|
|
bool usmHostAllocationsCacheEnabled = false;
|
2017-12-21 00:45:38 +01:00
|
|
|
};
|
2019-03-26 11:59:46 +01:00
|
|
|
} // namespace NEO
|