From d24f403cc43742a3983cc4307e1885bd5e140a12 Mon Sep 17 00:00:00 2001 From: Lukasz Jobczyk Date: Wed, 11 Jan 2023 11:03:48 +0000 Subject: [PATCH] Align external host ptr allocation gpu va to 2MB Related-To: NEO-7116 Signed-off-by: Lukasz Jobczyk --- .../os_interface/linux/drm_memory_manager.cpp | 8 +++++++- .../os_interface/linux/drm_memory_manager.h | 3 ++- .../linux/drm_memory_manager_tests.cpp | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/shared/source/os_interface/linux/drm_memory_manager.cpp b/shared/source/os_interface/linux/drm_memory_manager.cpp index da401f5dd8..6835eb4700 100644 --- a/shared/source/os_interface/linux/drm_memory_manager.cpp +++ b/shared/source/os_interface/linux/drm_memory_manager.cpp @@ -214,6 +214,12 @@ uint64_t DrmMemoryManager::acquireGpuRange(size_t &size, uint32_t rootDeviceInde return gmmHelper->canonize(gfxPartition->heapAllocate(heapIndex, size)); } +uint64_t DrmMemoryManager::acquireGpuRangeWithCustomAlignment(size_t &size, uint32_t rootDeviceIndex, HeapIndex heapIndex, size_t alignment) { + auto gfxPartition = getGfxPartition(rootDeviceIndex); + auto gmmHelper = getGmmHelper(rootDeviceIndex); + return gmmHelper->canonize(gfxPartition->heapAllocateWithCustomAlignment(heapIndex, size, alignment)); +} + void DrmMemoryManager::releaseGpuRange(void *address, size_t unmapSize, uint32_t rootDeviceIndex) { uint64_t graphicsAddress = static_cast(reinterpret_cast(address)); auto gmmHelper = getGmmHelper(rootDeviceIndex); @@ -493,7 +499,7 @@ GraphicsAllocation *DrmMemoryManager::allocateGraphicsMemoryForNonSvmHostPtr(con auto offsetInPage = ptrDiff(allocationData.hostPtr, alignedPtr); auto rootDeviceIndex = allocationData.rootDeviceIndex; - auto gpuVirtualAddress = acquireGpuRange(alignedSize, rootDeviceIndex, HeapIndex::HEAP_STANDARD); + auto gpuVirtualAddress = acquireGpuRangeWithCustomAlignment(alignedSize, rootDeviceIndex, HeapIndex::HEAP_STANDARD, MemoryConstants::pageSize2Mb); if (!gpuVirtualAddress) { return nullptr; } diff --git a/shared/source/os_interface/linux/drm_memory_manager.h b/shared/source/os_interface/linux/drm_memory_manager.h index a9c0e2d4a5..eb27e5497c 100644 --- a/shared/source/os_interface/linux/drm_memory_manager.h +++ b/shared/source/os_interface/linux/drm_memory_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 Intel Corporation + * Copyright (C) 2018-2023 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -93,6 +93,7 @@ class DrmMemoryManager : public MemoryManager { BufferObject *allocUserptr(uintptr_t address, size_t size, uint32_t rootDeviceIndex); bool setDomainCpu(GraphicsAllocation &graphicsAllocation, bool writeEnable); uint64_t acquireGpuRange(size_t &size, uint32_t rootDeviceIndex, HeapIndex heapIndex); + uint64_t acquireGpuRangeWithCustomAlignment(size_t &size, uint32_t rootDeviceIndex, HeapIndex heapIndex, size_t alignment); MOCKABLE_VIRTUAL void releaseGpuRange(void *address, size_t size, uint32_t rootDeviceIndex); void emitPinningRequest(BufferObject *bo, const AllocationData &allocationData) const; uint32_t getDefaultDrmContextId(uint32_t rootDeviceIndex) const; diff --git a/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp b/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp index 5731e97f3d..cbc00d3b4c 100644 --- a/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp +++ b/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp @@ -2807,6 +2807,23 @@ TEST_F(DrmMemoryManagerBasic, givenDrmMemoryManagerWhenAllocateGraphicsMemoryFor memoryManager->freeGraphicsMemory(allocation1); } +TEST_F(DrmMemoryManagerBasic, givenDrmMemoryManagerWhenAllocateGraphicsMemoryForNonSvmHostPtrThenGpuVaIsAlignedTo2Mb) { + AllocationData allocationData; + allocationData.rootDeviceIndex = rootDeviceIndex; + std::unique_ptr memoryManager(new (std::nothrow) TestedDrmMemoryManager(false, false, false, executionEnvironment)); + + memoryManager->forceLimitedRangeAllocator(0xFFFFFFFFF); + + allocationData.size = 2 * MemoryConstants::kiloByte; + allocationData.hostPtr = reinterpret_cast(0x1234); + auto allocation = static_cast(memoryManager->allocateGraphicsMemoryForNonSvmHostPtr(allocationData)); + + EXPECT_EQ(static_cast(allocation)->getBO()->peekAddress(), castToUint64(allocation->getReservedAddressPtr())); + EXPECT_TRUE(isAligned(static_cast(allocation)->getBO()->peekAddress())); + + memoryManager->freeGraphicsMemory(allocation); +} + TEST_F(DrmMemoryManagerBasic, givenDrmMemoryManagerWhenAllocateGraphicsMemoryForNonSvmHostPtrIsCalledButAllocationFailedThenNullPtrReturned) { AllocationData allocationData; allocationData.rootDeviceIndex = rootDeviceIndex;