Simplify memory manager API [1/n]

pass struct with properties to allocate graphics memory methods:
for protected methods use AllocationData
for public methods use AllocationProperties

Change-Id: Ie1c3cb6b5e330bc4adac2ca8b0bf02d30ec76065
Signed-off-by: Mateusz Jablonski <mateusz.jablonski@intel.com>
This commit is contained in:
Mateusz Jablonski
2018-11-30 11:01:33 +01:00
committed by sys_ocldev
parent ace20aba5b
commit c8748b77a0
30 changed files with 235 additions and 163 deletions

View File

@@ -355,7 +355,10 @@ bool CommandStreamReceiver::createAllocationForHostSurface(HostPtrSurface &surfa
allocation = memoryManager->allocateGraphicsMemoryForHostPtr(surface.getSurfaceSize(), surface.getMemoryPointer(), device.isFullRangeSvm(), requiresL3Flush);
if (allocation == nullptr && surface.peekIsPtrCopyAllowed()) {
// Try with no host pointer allocation and copy
allocation = memoryManager->allocateGraphicsMemory(surface.getSurfaceSize(), MemoryConstants::pageSize, false, false);
AllocationProperties properties;
properties.alignment = MemoryConstants::pageSize;
properties.size = surface.getSurfaceSize();
allocation = memoryManager->allocateGraphicsMemoryWithProperties(properties);
if (allocation) {
memcpy_s(allocation->getUnderlyingBuffer(), allocation->getUnderlyingBufferSize(), surface.getMemoryPointer(), surface.getSurfaceSize());

View File

@@ -143,10 +143,11 @@ bool Device::createDeviceImpl(const HardwareInfo *pHwInfo, Device &outDevice) {
outDevice.executionEnvironment->memoryManager->setForce32BitAllocations(outDevice.getDeviceInfo().force32BitAddressess);
if (outDevice.preemptionMode == PreemptionMode::MidThread || outDevice.isSourceLevelDebuggerActive()) {
size_t requiredSize = pHwInfo->capabilityTable.requiredPreemptionSurfaceSize;
size_t alignment = 256 * MemoryConstants::kiloByte;
bool uncacheable = outDevice.getWaTable()->waCSRUncachable;
outDevice.preemptionAllocation = outDevice.executionEnvironment->memoryManager->allocateGraphicsMemory(requiredSize, alignment, false, uncacheable);
AllocationProperties properties;
properties.size = pHwInfo->capabilityTable.requiredPreemptionSurfaceSize;
properties.flags.uncacheable = outDevice.getWaTable()->waCSRUncachable;
properties.alignment = 256 * MemoryConstants::kiloByte;
outDevice.preemptionAllocation = outDevice.executionEnvironment->memoryManager->allocateGraphicsMemoryWithProperties(properties);
if (!outDevice.preemptionAllocation) {
return false;
}

View File

@@ -31,9 +31,11 @@ GraphicsAllocation *FlatBatchBufferHelperHw<GfxFamily>::flattenBatchBuffer(Batch
batchBuffer.chainedBatchBuffer->setAubWritable(false);
auto sizeMainBatchBuffer = batchBuffer.chainedBatchBufferStartOffset - batchBuffer.startOffset;
auto flatBatchBufferSize = alignUp(sizeMainBatchBuffer + indirectPatchCommandsSize + batchBuffer.chainedBatchBuffer->getUnderlyingBufferSize(), MemoryConstants::pageSize);
AllocationProperties flatBatchBufferProperties;
flatBatchBufferProperties.alignment = MemoryConstants::pageSize;
flatBatchBufferProperties.size = alignUp(sizeMainBatchBuffer + indirectPatchCommandsSize + batchBuffer.chainedBatchBuffer->getUnderlyingBufferSize(), MemoryConstants::pageSize);
flatBatchBuffer =
getMemoryManager()->allocateGraphicsMemory(flatBatchBufferSize, MemoryConstants::pageSize, false, false);
getMemoryManager()->allocateGraphicsMemoryWithProperties(flatBatchBufferProperties);
UNRECOVERABLE_IF(flatBatchBuffer == nullptr);
// Copy main batchbuffer
memcpy_s(flatBatchBuffer->getUnderlyingBuffer(), sizeMainBatchBuffer,
@@ -46,7 +48,7 @@ GraphicsAllocation *FlatBatchBufferHelperHw<GfxFamily>::flattenBatchBuffer(Batch
memcpy_s(ptrOffset(flatBatchBuffer->getUnderlyingBuffer(), sizeMainBatchBuffer + indirectPatchCommandsSize),
batchBuffer.chainedBatchBuffer->getUnderlyingBufferSize(), batchBuffer.chainedBatchBuffer->getUnderlyingBuffer(),
batchBuffer.chainedBatchBuffer->getUnderlyingBufferSize());
sizeBatchBuffer = flatBatchBufferSize;
sizeBatchBuffer = flatBatchBufferProperties.size;
patchInfoCollection.insert(std::end(patchInfoCollection), std::begin(indirectPatchInfo), std::end(indirectPatchInfo));
}
} else if (dispatchMode == DispatchMode::BatchedDispatch) {
@@ -108,8 +110,10 @@ GraphicsAllocation *FlatBatchBufferHelperHw<GfxFamily>::flattenBatchBuffer(Batch
flatBatchBufferSize = alignUp(flatBatchBufferSize, MemoryConstants::pageSize);
flatBatchBufferSize += CSRequirements::csOverfetchSize;
flatBatchBuffer = getMemoryManager()->allocateGraphicsMemory(static_cast<size_t>(flatBatchBufferSize),
MemoryConstants::pageSize, false, false);
AllocationProperties flatBatchBufferProperties;
flatBatchBufferProperties.size = static_cast<size_t>(flatBatchBufferSize);
flatBatchBufferProperties.alignment = MemoryConstants::pageSize;
flatBatchBuffer = getMemoryManager()->allocateGraphicsMemoryWithProperties(flatBatchBufferProperties);
UNRECOVERABLE_IF(flatBatchBuffer == nullptr);
char *ptr = reinterpret_cast<char *>(flatBatchBuffer->getUnderlyingBuffer());

View File

@@ -86,14 +86,14 @@ GraphicsAllocation *MemoryManager::allocateGraphicsMemoryForSVM(size_t size, boo
return graphicsAllocation;
}
GraphicsAllocation *MemoryManager::allocateGraphicsMemory(size_t size, const void *ptr, bool forcePin) {
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryWithHostPtr(const AllocationData &allocationData) {
if (deferredDeleter) {
deferredDeleter->drain(true);
}
GraphicsAllocation *graphicsAllocation = nullptr;
auto osStorage = hostPtrManager->prepareOsStorageForAllocation(*this, size, ptr);
auto osStorage = hostPtrManager->prepareOsStorageForAllocation(*this, allocationData.size, allocationData.hostPtr);
if (osStorage.fragmentCount > 0) {
graphicsAllocation = createGraphicsAllocation(osStorage, size, ptr);
graphicsAllocation = createGraphicsAllocation(osStorage, allocationData.size, allocationData.hostPtr);
}
return graphicsAllocation;
}
@@ -239,6 +239,7 @@ bool MemoryManager::getAllocationData(AllocationData &allocationData, const Allo
allocationData.size = size;
allocationData.type = type;
allocationData.devicesBitfield = devicesBitfield;
allocationData.alignment = MemoryConstants::pageSize;
if (allocationData.flags.allocateMemory) {
allocationData.hostPtr = nullptr;
@@ -266,13 +267,13 @@ GraphicsAllocation *MemoryManager::allocateGraphicsMemory(const AllocationData &
return allocate32BitGraphicsMemory(allocationData.size, allocationData.hostPtr, AllocationOrigin::EXTERNAL_ALLOCATION);
}
if (allocationData.hostPtr) {
return allocateGraphicsMemory(allocationData.size, allocationData.hostPtr, allocationData.flags.forcePin);
return allocateGraphicsMemoryWithHostPtr(allocationData);
}
if (peek64kbPagesEnabled() && allocationData.flags.allow64kbPages) {
bool preferRenderCompressed = (allocationData.type == GraphicsAllocation::AllocationType::BUFFER_COMPRESSED);
return allocateGraphicsMemory64kb(allocationData.size, MemoryConstants::pageSize64k, allocationData.flags.forcePin, preferRenderCompressed);
}
return allocateGraphicsMemory(allocationData.size, MemoryConstants::pageSize, allocationData.flags.forcePin, allocationData.flags.uncacheable);
return allocateGraphicsMemoryWithAlignment(allocationData);
}
const CsrContainer &MemoryManager::getCommandStreamReceivers() const {

View File

@@ -42,26 +42,29 @@ enum AllocationOrigin {
INTERNAL_ALLOCATION
};
struct AllocationFlags {
struct AllocationProperties {
union {
struct {
uint32_t allocateMemory : 1;
uint32_t flushL3RequiredForRead : 1;
uint32_t flushL3RequiredForWrite : 1;
uint32_t reserved : 29;
uint32_t forcePin : 1;
uint32_t uncacheable : 1;
uint32_t reserved : 27;
} flags;
uint32_t allFlags;
uint32_t allFlags = 0;
};
static_assert(sizeof(AllocationProperties::flags) == sizeof(AllocationProperties::allFlags), "");
size_t size = 0;
size_t alignment = 0;
static_assert(sizeof(AllocationFlags::flags) == sizeof(AllocationFlags::allFlags), "");
AllocationFlags() {
AllocationProperties() {
allFlags = 0;
flags.flushL3RequiredForRead = 1;
flags.flushL3RequiredForWrite = 1;
}
AllocationFlags(bool allocateMemory) {
AllocationProperties(bool allocateMemory) {
allFlags = 0;
flags.flushL3RequiredForRead = 1;
flags.flushL3RequiredForWrite = 1;
@@ -69,6 +72,8 @@ struct AllocationFlags {
}
};
using AllocationFlags = AllocationProperties;
struct AllocationData {
union {
struct {
@@ -85,10 +90,10 @@ struct AllocationData {
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;
DevicesBitfield devicesBitfield = 0;
};
@@ -119,18 +124,30 @@ class MemoryManager {
virtual void removeAllocationFromHostPtrManager(GraphicsAllocation *memory) = 0;
GraphicsAllocation *allocateGraphicsMemory(size_t size) {
return allocateGraphicsMemory(size, MemoryConstants::preferredAlignment, false, false);
AllocationData allocationData;
allocationData.size = size;
allocationData.alignment = MemoryConstants::preferredAlignment;
return allocateGraphicsMemoryWithAlignment(allocationData);
}
virtual GraphicsAllocation *allocateGraphicsMemory(size_t size, size_t alignment, bool forcePin, bool uncacheable) = 0;
GraphicsAllocation *allocateGraphicsMemoryWithProperties(const AllocationProperties &properties) {
AllocationData allocationData;
allocationData.alignment = properties.alignment;
allocationData.size = properties.size;
allocationData.flags.uncacheable = properties.flags.uncacheable;
allocationData.flags.forcePin = properties.flags.forcePin;
return allocateGraphicsMemoryWithAlignment(allocationData);
}
virtual GraphicsAllocation *allocateGraphicsMemory64kb(size_t size, size_t alignment, bool forcePin, bool preferRenderCompressed) = 0;
virtual GraphicsAllocation *allocateGraphicsMemoryForNonSvmHostPtr(size_t size, void *cpuPtr) = 0;
virtual GraphicsAllocation *allocateGraphicsMemory(size_t size, const void *ptr) {
return MemoryManager::allocateGraphicsMemory(size, ptr, false);
AllocationData allocationData;
allocationData.hostPtr = ptr;
allocationData.size = size;
return MemoryManager::allocateGraphicsMemoryWithHostPtr(allocationData);
}
virtual GraphicsAllocation *allocateGraphicsMemory(size_t size, const void *ptr, bool forcePin);
GraphicsAllocation *allocateGraphicsMemoryForHostPtr(size_t size, void *ptr, bool fullRangeSvm, bool requiresL3Flush) {
if (fullRangeSvm) {
@@ -243,6 +260,9 @@ class MemoryManager {
const void *hostPtr, size_t size, GraphicsAllocation::AllocationType type);
GraphicsAllocation *allocateGraphicsMemory(const AllocationData &allocationData);
virtual GraphicsAllocation *allocateGraphicsMemoryWithHostPtr(const AllocationData &allocationData);
virtual GraphicsAllocation *allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) = 0;
bool force32bitAllocations = false;
bool virtualPaddingAvailable = false;
GraphicsAllocation *paddingAllocation = nullptr;

View File

@@ -30,27 +30,27 @@ OsAgnosticMemoryManager::~OsAgnosticMemoryManager() {
struct OsHandle {
};
GraphicsAllocation *OsAgnosticMemoryManager::allocateGraphicsMemory(size_t size, size_t alignment, bool forcePin, bool uncacheable) {
GraphicsAllocation *OsAgnosticMemoryManager::allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) {
auto sizeAligned = alignUp(size, MemoryConstants::pageSize);
auto sizeAligned = alignUp(allocationData.size, MemoryConstants::pageSize);
MemoryAllocation *memoryAllocation = nullptr;
if (fakeBigAllocations && size > bigAllocation) {
memoryAllocation = new MemoryAllocation(nullptr, (void *)dummyAddress, static_cast<uint64_t>(dummyAddress), size, counter,
if (fakeBigAllocations && allocationData.size > bigAllocation) {
memoryAllocation = new MemoryAllocation(nullptr, (void *)dummyAddress, static_cast<uint64_t>(dummyAddress), allocationData.size, counter,
MemoryPool::System4KBPages, this->getOsContextCount(), false);
counter++;
memoryAllocation->uncacheable = uncacheable;
memoryAllocation->uncacheable = allocationData.flags.uncacheable;
return memoryAllocation;
}
auto ptr = allocateSystemMemory(sizeAligned, alignment ? alignUp(alignment, MemoryConstants::pageSize) : MemoryConstants::pageSize);
auto ptr = allocateSystemMemory(sizeAligned, allocationData.alignment ? alignUp(allocationData.alignment, MemoryConstants::pageSize) : MemoryConstants::pageSize);
if (ptr != nullptr) {
memoryAllocation = new MemoryAllocation(ptr, ptr, reinterpret_cast<uint64_t>(ptr), size, counter, MemoryPool::System4KBPages,
memoryAllocation = new MemoryAllocation(ptr, ptr, reinterpret_cast<uint64_t>(ptr), allocationData.size, counter, MemoryPool::System4KBPages,
this->getOsContextCount(), false);
if (!memoryAllocation) {
alignedFreeWrapper(ptr);
return nullptr;
}
memoryAllocation->uncacheable = uncacheable;
memoryAllocation->uncacheable = allocationData.flags.uncacheable;
}
counter++;
return memoryAllocation;
@@ -72,7 +72,11 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocateGraphicsMemoryForNonSvmHost
}
GraphicsAllocation *OsAgnosticMemoryManager::allocateGraphicsMemory64kb(size_t size, size_t alignment, bool forcePin, bool preferRenderCompressed) {
auto memoryAllocation = allocateGraphicsMemory(alignUp(size, MemoryConstants::pageSize64k), MemoryConstants::pageSize64k, forcePin, false);
AllocationProperties properties;
properties.size = alignUp(size, MemoryConstants::pageSize64k);
properties.flags.forcePin = forcePin;
properties.alignment = MemoryConstants::pageSize64k;
auto memoryAllocation = allocateGraphicsMemoryWithProperties(properties);
if (memoryAllocation) {
reinterpret_cast<MemoryAllocation *>(memoryAllocation)->overrideMemoryPool(MemoryPool::System64KBPages);
}

View File

@@ -44,7 +44,6 @@ class OsAgnosticMemoryManager : public MemoryManager {
};
~OsAgnosticMemoryManager() override;
GraphicsAllocation *allocateGraphicsMemory(size_t size, size_t alignment, bool forcePin, bool uncacheable) override;
GraphicsAllocation *allocateGraphicsMemory64kb(size_t size, size_t alignment, bool forcePin, bool preferRenderCompressed) override;
GraphicsAllocation *allocateGraphicsMemoryForNonSvmHostPtr(size_t size, void *cpuPtr) override;
GraphicsAllocation *allocate32BitGraphicsMemory(size_t size, const void *ptr, AllocationOrigin allocationOrigin) override;
@@ -72,6 +71,9 @@ class OsAgnosticMemoryManager : public MemoryManager {
Allocator32bit *create32BitAllocator(bool enableLocalMemory);
protected:
GraphicsAllocation *allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) override;
private:
unsigned long long counter = 0;
bool fakeBigAllocations = false;

View File

@@ -203,12 +203,12 @@ DrmAllocation *DrmMemoryManager::createGraphicsAllocation(OsHandleStorage &handl
return allocation;
}
DrmAllocation *DrmMemoryManager::allocateGraphicsMemory(size_t size, size_t alignment, bool forcePin, bool uncacheable) {
DrmAllocation *DrmMemoryManager::allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) {
const size_t minAlignment = MemoryConstants::allocationAlignment;
size_t cAlignment = alignUp(std::max(alignment, minAlignment), minAlignment);
size_t cAlignment = alignUp(std::max(allocationData.alignment, minAlignment), minAlignment);
// When size == 0 allocate allocationAlignment
// It's needed to prevent overlapping pages with user pointers
size_t cSize = std::max(alignUp(size, minAlignment), minAlignment);
size_t cSize = std::max(alignUp(allocationData.size, minAlignment), minAlignment);
auto res = alignedMallocWrapper(cSize, cAlignment);
@@ -223,16 +223,16 @@ DrmAllocation *DrmMemoryManager::allocateGraphicsMemory(size_t size, size_t alig
}
bo->isAllocated = true;
if (forcePinEnabled && pinBB != nullptr && forcePin && size >= this->pinThreshold) {
if (forcePinEnabled && pinBB != nullptr && allocationData.flags.forcePin && allocationData.size >= this->pinThreshold) {
pinBB->pin(&bo, 1);
}
return new DrmAllocation(bo, res, cSize, MemoryPool::System4KBPages, getOsContextCount(), false);
}
DrmAllocation *DrmMemoryManager::allocateGraphicsMemory(size_t size, const void *ptr, bool forcePin) {
auto res = static_cast<DrmAllocation *>(MemoryManager::allocateGraphicsMemory(size, const_cast<void *>(ptr), forcePin));
DrmAllocation *DrmMemoryManager::allocateGraphicsMemoryWithHostPtr(const AllocationData &allocationData) {
auto res = static_cast<DrmAllocation *>(MemoryManager::allocateGraphicsMemoryWithHostPtr(allocationData));
bool forcePinAllowed = res != nullptr && pinBB != nullptr && forcePinEnabled && forcePin && size >= this->pinThreshold;
bool forcePinAllowed = res != nullptr && pinBB != nullptr && forcePinEnabled && allocationData.flags.forcePin && allocationData.size >= this->pinThreshold;
if (!validateHostPtrMemory && forcePinAllowed) {
BufferObject *boArray[] = {res->getBO()};
pinBB->pin(boArray, 1);

View File

@@ -31,13 +31,14 @@ class DrmMemoryManager : public MemoryManager {
void addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) override;
void removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) override;
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override;
DrmAllocation *allocateGraphicsMemory(size_t size, size_t alignment, bool forcePin, bool uncacheable) override;
DrmAllocation *allocateGraphicsMemory64kb(size_t size, size_t alignment, bool forcePin, bool preferRenderCompressed) override;
DrmAllocation *allocateGraphicsMemoryForNonSvmHostPtr(size_t size, void *cpuPtr) override;
DrmAllocation *allocateGraphicsMemory(size_t size, const void *ptr) override {
return allocateGraphicsMemory(size, ptr, false);
AllocationData allocationData;
allocationData.hostPtr = ptr;
allocationData.size = size;
return allocateGraphicsMemoryWithHostPtr(allocationData);
}
DrmAllocation *allocateGraphicsMemory(size_t size, const void *ptr, bool forcePin) override;
GraphicsAllocation *allocateGraphicsMemoryForImage(ImageInfo &imgInfo, Gmm *gmm) override;
DrmAllocation *allocate32BitGraphicsMemory(size_t size, const void *ptr, AllocationOrigin allocationOrigin) override;
GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness) override;
@@ -74,6 +75,9 @@ class DrmMemoryManager : public MemoryManager {
void releaseGpuRange(void *address, size_t unmapSize, StorageAllocatorType allocatorType);
void initInternalRangeAllocator(size_t range);
DrmAllocation *allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) override;
DrmAllocation *allocateGraphicsMemoryWithHostPtr(const AllocationData &allocationData) override;
Drm *drm;
BufferObject *pinBB;
size_t pinThreshold = 8 * 1024 * 1024;

View File

@@ -79,9 +79,9 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory64kb(size_t size, s
return wddmAllocation.release();
}
GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(size_t size, size_t alignment, bool forcePin, bool uncacheable) {
size_t newAlignment = alignment ? alignUp(alignment, MemoryConstants::pageSize) : MemoryConstants::pageSize;
size_t sizeAligned = size ? alignUp(size, MemoryConstants::pageSize) : MemoryConstants::pageSize;
GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) {
size_t newAlignment = allocationData.alignment ? alignUp(allocationData.alignment, MemoryConstants::pageSize) : MemoryConstants::pageSize;
size_t sizeAligned = allocationData.size ? alignUp(allocationData.size, MemoryConstants::pageSize) : MemoryConstants::pageSize;
void *pSysMem = allocateSystemMemory(sizeAligned, newAlignment);
Gmm *gmm = nullptr;
@@ -92,7 +92,7 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(size_t size, size_
auto wddmAllocation = std::make_unique<WddmAllocation>(pSysMem, sizeAligned, nullptr, MemoryPool::System4KBPages, getOsContextCount(), false);
wddmAllocation->driverAllocatedCpuPointer = pSysMem;
gmm = new Gmm(pSysMem, sizeAligned, uncacheable);
gmm = new Gmm(pSysMem, sizeAligned, allocationData.flags.uncacheable);
wddmAllocation->gmm = gmm;

View File

@@ -35,7 +35,6 @@ class WddmMemoryManager : public MemoryManager {
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override;
GraphicsAllocation *allocateGraphicsMemory64kb(size_t size, size_t alignment, bool forcePin, bool preferRenderCompressed) override;
GraphicsAllocation *allocateGraphicsMemory(size_t size, size_t alignment, bool forcePin, bool uncacheable) override;
GraphicsAllocation *allocateGraphicsMemory(size_t size, const void *ptr) override;
GraphicsAllocation *allocateGraphicsMemoryForNonSvmHostPtr(size_t size, void *cpuPtr) override;
GraphicsAllocation *allocate32BitGraphicsMemory(size_t size, const void *ptr, AllocationOrigin allocationOrigin) override;
@@ -74,6 +73,8 @@ class WddmMemoryManager : public MemoryManager {
AlignedMallocRestrictions *getAlignedMallocRestrictions() override;
protected:
GraphicsAllocation *allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) override;
GraphicsAllocation *createAllocationFromHandle(osHandle handle, bool requireSpecificBitness, bool ntHandle);
static bool validateAllocation(WddmAllocation *alloc);
bool createWddmAllocation(WddmAllocation *allocation, AllocationOrigin origin);