From 962d98a2d89aca6ad6b073245c03f44fed610905 Mon Sep 17 00:00:00 2001 From: Szymon Morek Date: Tue, 21 Dec 2021 14:17:26 +0000 Subject: [PATCH] Enable MemoryInfo for platforms without local mem Query for memory regions on all platforms. Fix createPaddedAllocation when input allocation was made by KMD Resolves: NEO-6472 Signed-off-by: Szymon Morek --- .../linux/drm_memory_info_tests.cpp | 17 ------- .../drm_memory_manager_localmem_tests.cpp | 11 ----- .../linux/drm_memory_manager_tests.cpp | 45 +++++++++++++++++++ .../os_interface/linux/drm_memory_manager.cpp | 20 +++++++-- .../source/os_interface/linux/drm_query.cpp | 7 --- 5 files changed, 62 insertions(+), 38 deletions(-) diff --git a/opencl/test/unit_test/os_interface/linux/drm_memory_info_tests.cpp b/opencl/test/unit_test/os_interface/linux/drm_memory_info_tests.cpp index 87d0cc2fc4..193f1b1af9 100644 --- a/opencl/test/unit_test/os_interface/linux/drm_memory_info_tests.cpp +++ b/opencl/test/unit_test/os_interface/linux/drm_memory_info_tests.cpp @@ -18,23 +18,6 @@ using namespace NEO; -TEST(MemoryInfo, givenNotSupportedLocalMemoryQueryingMemoryInfoThenMemoryInfoIsNotCreated) { - DebugManagerStateRestore restorer; - DebugManager.flags.EnableLocalMemory.set(0); - auto executionEnvironment = std::make_unique(); - executionEnvironment->prepareRootDeviceEnvironments(1); - - auto drm = std::make_unique(*executionEnvironment->rootDeviceEnvironments[0]); - ASSERT_NE(nullptr, drm); - - auto ret = drm->queryMemoryInfo(); - auto memoryInfo = drm->getMemoryInfo(); - - EXPECT_EQ(0u, drm->ioctlCallsCount); - EXPECT_TRUE(ret); - EXPECT_EQ(nullptr, memoryInfo); -} - TEST(MemoryInfo, givenMemoryRegionQuerySupportedWhenQueryingMemoryInfoThenMemoryInfoIsCreatedWithRegions) { DebugManagerStateRestore restorer; DebugManager.flags.EnableLocalMemory.set(1); diff --git a/opencl/test/unit_test/os_interface/linux/drm_memory_manager_localmem_tests.cpp b/opencl/test/unit_test/os_interface/linux/drm_memory_manager_localmem_tests.cpp index f9a701b064..cf0d933075 100644 --- a/opencl/test/unit_test/os_interface/linux/drm_memory_manager_localmem_tests.cpp +++ b/opencl/test/unit_test/os_interface/linux/drm_memory_manager_localmem_tests.cpp @@ -865,17 +865,6 @@ TEST_F(DrmMemoryManagerTestImpl, givenDrmMemoryManagerWhenGetLocalMemorySizeIsCa EXPECT_EQ(memoryInfo->getMemoryRegionSize(MemoryBanks::getBankForLocalMemory(0)), memoryManager.getLocalMemorySize(0u, 0xF)); } -TEST_F(DrmMemoryManagerTestImpl, givenLocalMemoryDisabledWhenQueryMemoryInfoThenReturnTrueAndDontCreateMemoryInfo) { - DebugManagerStateRestore restorer; - DebugManager.flags.EnableLocalMemory.set(0); - MockExecutionEnvironment executionEnvironment; - executionEnvironment.rootDeviceEnvironments[0]->osInterface = std::make_unique(); - auto drm = std::make_unique(*executionEnvironment.rootDeviceEnvironments[0]); - auto ret = drm->queryMemoryInfo(); - EXPECT_TRUE(ret); - EXPECT_EQ(nullptr, drm->memoryInfo); -} - TEST_F(DrmMemoryManagerTestImpl, givenDrmMemoryManagerWhenGetLocalMemorySizeIsCalledForMemoryInfoAndInvalidDeviceBitfieldThenReturnZero) { MockExecutionEnvironment executionEnvironment; executionEnvironment.rootDeviceEnvironments[0]->osInterface = std::make_unique(); diff --git a/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp b/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp index a400cef35c..80df1666cc 100644 --- a/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp +++ b/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp @@ -5710,4 +5710,49 @@ TEST_F(DrmMemoryManagerTest, givenDrmWhenRetrieveMmapOffsetForBufferObjectIsCall } } +TEST_F(DrmMemoryManagerTest, whenCallPaddedAllocationWithoutMmapPtrThenOnlyUserptrCalled) { + mock->ioctl_expected.gemUserptr = 1; + mock->ioctl_expected.gemClose = 1; + + void *cpuPtr = (void *)0x30000; + size_t size = 0x1000; + DrmAllocation gfxAllocation(rootDeviceIndex, GraphicsAllocation::AllocationType::UNKNOWN, nullptr, cpuPtr, size, (osHandle)1u, MemoryPool::MemoryNull); + auto gfxPaddedAllocation = memoryManager->createPaddedAllocation(&gfxAllocation, size); + ASSERT_NE(nullptr, gfxPaddedAllocation); + memoryManager->freeGraphicsMemoryImpl(gfxPaddedAllocation); +} + +TEST_F(DrmMemoryManagerTest, whenCallPaddedAllocationWithMmapPtrThenMmapCalled) { + mock->ioctl_expected.gemMmap = 1; + mock->ioctl_expected.gemUserptr = 1; + mock->ioctl_expected.gemClose = 1; + BufferObject bo(mock, 1, 1024, 0); + + void *cpuPtr = (void *)0x30000; + size_t size = 0x1000; + DrmAllocation gfxAllocation(rootDeviceIndex, GraphicsAllocation::AllocationType::UNKNOWN, &bo, cpuPtr, size, (osHandle)1u, MemoryPool::MemoryNull); + gfxAllocation.setMmapPtr(cpuPtr); + gfxAllocation.setMmapSize(size); + auto gfxPaddedAllocation = memoryManager->createPaddedAllocation(&gfxAllocation, size); + ASSERT_NE(nullptr, gfxPaddedAllocation); + EXPECT_TRUE(gfxAllocation.isLocked()); + memoryManager->freeGraphicsMemoryImpl(gfxPaddedAllocation); +} + +TEST_F(DrmMemoryManagerTest, whenCallPaddedAllocationWithMmapPtrAndFailedMmapCalledThenReturnNullptr) { + mock->ioctl_expected.gemMmap = 1; + mock->ioctl_res = -1; + + BufferObject bo(mock, 1, 1024, 0); + + void *cpuPtr = (void *)0x30000; + size_t size = 0x1000; + DrmAllocation gfxAllocation(rootDeviceIndex, GraphicsAllocation::AllocationType::UNKNOWN, &bo, cpuPtr, size, (osHandle)1u, MemoryPool::MemoryNull); + gfxAllocation.setMmapPtr(cpuPtr); + gfxAllocation.setMmapSize(size); + auto gfxPaddedAllocation = memoryManager->createPaddedAllocation(&gfxAllocation, size); + ASSERT_EQ(nullptr, gfxPaddedAllocation); + mock->ioctl_res = 0; +} + } // namespace NEO diff --git a/shared/source/os_interface/linux/drm_memory_manager.cpp b/shared/source/os_interface/linux/drm_memory_manager.cpp index 493955381a..2cccc200d2 100644 --- a/shared/source/os_interface/linux/drm_memory_manager.cpp +++ b/shared/source/os_interface/linux/drm_memory_manager.cpp @@ -705,11 +705,25 @@ GraphicsAllocation *DrmMemoryManager::createPaddedAllocation(GraphicsAllocation auto rootDeviceIndex = inputGraphicsAllocation->getRootDeviceIndex(); gpuRange = acquireGpuRange(sizeWithPadding, rootDeviceIndex, HeapIndex::HEAP_STANDARD); - auto srcPtr = inputGraphicsAllocation->getUnderlyingBuffer(); + void *srcPtr = nullptr; + auto drmInputAllocation = static_cast(inputGraphicsAllocation); + if (drmInputAllocation->getMmapPtr()) { + auto bo = drmInputAllocation->getBO(); + drm_i915_gem_mmap mmap_arg = {}; + mmap_arg.handle = bo->peekHandle(); + mmap_arg.size = bo->peekSize(); + if (getDrm(rootDeviceIndex).ioctl(DRM_IOCTL_I915_GEM_MMAP, &mmap_arg) != 0) { + return nullptr; + } + srcPtr = addrToPtr(mmap_arg.addr_ptr); + inputGraphicsAllocation->lock(srcPtr); + } else { + srcPtr = inputGraphicsAllocation->getUnderlyingBuffer(); + } auto srcSize = inputGraphicsAllocation->getUnderlyingBufferSize(); auto alignedSrcSize = alignUp(srcSize, MemoryConstants::pageSize); - auto alignedPtr = (uintptr_t)alignDown(srcPtr, MemoryConstants::pageSize); - auto offset = (uintptr_t)srcPtr - alignedPtr; + auto alignedPtr = reinterpret_cast(alignDown(srcPtr, MemoryConstants::pageSize)); + auto offset = ptrDiff(srcPtr, alignedPtr); std::unique_ptr bo(allocUserptr(alignedPtr, alignedSrcSize, 0, rootDeviceIndex)); if (!bo) { diff --git a/shared/source/os_interface/linux/drm_query.cpp b/shared/source/os_interface/linux/drm_query.cpp index 804c73a7fe..b1028a03ce 100644 --- a/shared/source/os_interface/linux/drm_query.cpp +++ b/shared/source/os_interface/linux/drm_query.cpp @@ -6,7 +6,6 @@ */ #include "shared/source/execution_environment/root_device_environment.h" -#include "shared/source/helpers/hw_helper.h" #include "shared/source/helpers/string.h" #include "shared/source/os_interface/linux/cache_info_impl.h" #include "shared/source/os_interface/linux/drm_engine_mapper.h" @@ -49,12 +48,6 @@ std::unique_ptr Drm::getMemoryRegions() { } bool Drm::queryMemoryInfo() { - auto pHwInfo = getRootDeviceEnvironment().getHardwareInfo(); - auto isLocalMemSupported = HwHelper::get(pHwInfo->platform.eRenderCoreFamily).getEnableLocalMemory(*pHwInfo); - if (!isLocalMemSupported) { - return true; - } - auto length = 0; auto dataQuery = this->query(DRM_I915_QUERY_MEMORY_REGIONS, DrmQueryItemFlags::empty, length); if (dataQuery) {