From 188c0797b66c55de83f347b07e5082848487090b Mon Sep 17 00:00:00 2001 From: "Venevtsev, Igor" Date: Thu, 25 Apr 2019 10:32:56 +0200 Subject: [PATCH] Revert "[2/n] Use GfxPartition for 32-bit allocations - DrmMemoryManager" This reverts commit 1ce2f9564ab3be8add6d362a4884dd149d471619. Related-To: NEO-2877 Change-Id: Id17e0bce560ed1d934412067f9e41d39c529018f Signed-off-by: Venevtsev, Igor --- runtime/memory_manager/gfx_partition.h | 2 - runtime/os_interface/linux/CMakeLists.txt | 2 + .../os_interface/linux/drm_limited_range.cpp | 31 +++ .../os_interface/linux/drm_limited_range.h | 34 +++ .../os_interface/linux/drm_memory_manager.cpp | 112 ++++++---- .../os_interface/linux/drm_memory_manager.h | 6 +- .../mocks/linux/mock_drm_memory_manager.h | 7 +- .../linux/drm_buffer_object_tests.cpp | 1 + .../linux/drm_memory_manager_tests.cpp | 194 ++++++++++++------ 9 files changed, 280 insertions(+), 109 deletions(-) create mode 100644 runtime/os_interface/linux/drm_limited_range.cpp create mode 100644 runtime/os_interface/linux/drm_limited_range.h diff --git a/runtime/memory_manager/gfx_partition.h b/runtime/memory_manager/gfx_partition.h index b752e8302b..3695836e50 100644 --- a/runtime/memory_manager/gfx_partition.h +++ b/runtime/memory_manager/gfx_partition.h @@ -58,8 +58,6 @@ class GfxPartition { return getHeapBase(heapIndex) + heapGranularity; } - bool isLimitedRange() { return getHeap(HeapIndex::HEAP_SVM).getSize() == 0ull; } - static const uint64_t heapGranularity = MemoryConstants::pageSize64k; static const std::array heap32Names; diff --git a/runtime/os_interface/linux/CMakeLists.txt b/runtime/os_interface/linux/CMakeLists.txt index e4f4e428a0..7591179c43 100644 --- a/runtime/os_interface/linux/CMakeLists.txt +++ b/runtime/os_interface/linux/CMakeLists.txt @@ -24,6 +24,8 @@ set(RUNTIME_SRCS_OS_INTERFACE_LINUX ${CMAKE_CURRENT_SOURCE_DIR}/drm_engine_mapper.h ${CMAKE_CURRENT_SOURCE_DIR}/drm_gem_close_worker.cpp ${CMAKE_CURRENT_SOURCE_DIR}/drm_gem_close_worker.h + ${CMAKE_CURRENT_SOURCE_DIR}/drm_limited_range.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/drm_limited_range.h ${CMAKE_CURRENT_SOURCE_DIR}/drm_memory_manager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/drm_memory_manager.h ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/drm_memory_manager_allocate_in_device_pool.cpp diff --git a/runtime/os_interface/linux/drm_limited_range.cpp b/runtime/os_interface/linux/drm_limited_range.cpp new file mode 100644 index 0000000000..f17818a580 --- /dev/null +++ b/runtime/os_interface/linux/drm_limited_range.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2017-2019 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "runtime/os_interface/linux/drm_limited_range.h" + +#include "runtime/os_interface/debug_settings_manager.h" +#include "runtime/os_interface/linux/allocator_helper.h" + +using namespace NEO; + +NEO::AllocatorLimitedRange::AllocatorLimitedRange(uint64_t base, uint64_t size) : base(base), size(size), heapAllocator(std::make_unique(base, size)) { +} + +uint64_t NEO::AllocatorLimitedRange::allocate(size_t &size) { + if (size >= this->size) { + return 0llu; + } + return this->heapAllocator->allocate(size); +} + +void NEO::AllocatorLimitedRange::free(uint64_t ptr, size_t size) { + this->heapAllocator->free(ptr, size); +} + +uint64_t NEO::AllocatorLimitedRange::getBase() const { + return base; +} diff --git a/runtime/os_interface/linux/drm_limited_range.h b/runtime/os_interface/linux/drm_limited_range.h new file mode 100644 index 0000000000..c48ff02eea --- /dev/null +++ b/runtime/os_interface/linux/drm_limited_range.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2017-2019 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "runtime/helpers/aligned_memory.h" +#include "runtime/helpers/basic_math.h" +#include "runtime/helpers/ptr_math.h" +#include "runtime/utilities/heap_allocator.h" + +#include +#include +#include + +namespace NEO { +class AllocatorLimitedRange { + public: + AllocatorLimitedRange(uint64_t base, uint64_t size); + AllocatorLimitedRange() = delete; + ~AllocatorLimitedRange() = default; + + uint64_t allocate(size_t &size); + void free(uint64_t ptr, size_t size); + uint64_t getBase() const; + + protected: + uint64_t base = 0; + uint64_t size = 0; + std::unique_ptr heapAllocator; +}; +} // namespace NEO diff --git a/runtime/os_interface/linux/drm_memory_manager.cpp b/runtime/os_interface/linux/drm_memory_manager.cpp index f70fe7c06f..288a059679 100644 --- a/runtime/os_interface/linux/drm_memory_manager.cpp +++ b/runtime/os_interface/linux/drm_memory_manager.cpp @@ -18,6 +18,7 @@ #include "runtime/helpers/ptr_math.h" #include "runtime/helpers/surface_formats.h" #include "runtime/memory_manager/host_ptr_manager.h" +#include "runtime/os_interface/32bit_memory.h" #include "runtime/os_interface/linux/os_context_linux.h" #include "runtime/os_interface/linux/os_interface.h" #include "runtime/os_interface/linux/tiling_mode_helper.h" @@ -38,7 +39,6 @@ DrmMemoryManager::DrmMemoryManager(gemCloseWorkerMode mode, forcePinEnabled(forcePinAllowed), validateHostPtrMemory(validateHostPtrMemory) { gfxPartition.init(platformDevices[0]->capabilityTable.gpuAddressSpace); - MemoryManager::virtualPaddingAvailable = true; if (mode != gemCloseWorkerMode::gemCloseWorkerInactive) { gemCloseWorker.reset(new DrmGemCloseWorker(*this)); @@ -58,9 +58,20 @@ DrmMemoryManager::DrmMemoryManager(gemCloseWorkerMode mode, } else { pinBB->isAllocated = true; } + + initInternalRangeAllocator(platformDevices[0]->capabilityTable.gpuAddressSpace); } DrmMemoryManager::~DrmMemoryManager() { + if (this->limitedGpuAddressRangeAllocator) { + // freeing space for internal 32bit allocator + uint64_t size = 4 * MemoryConstants::gigaByte - MemoryConstants::pageSize; + this->limitedGpuAddressRangeAllocator->free(this->internal32bitAllocator->getBase(), size); + + // freeing space for external 32bit allocator + size += MemoryConstants::pageSize; + this->limitedGpuAddressRangeAllocator->free(this->allocator32Bit->getBase(), size); + } applyCommonCleanup(); if (gemCloseWorker) { gemCloseWorker->close(false); @@ -71,6 +82,28 @@ DrmMemoryManager::~DrmMemoryManager() { } } +void DrmMemoryManager::initInternalRangeAllocator(size_t gpuRange) { + if (gpuRange < MemoryConstants::max48BitAddress || !DebugManager.flags.EnableHostPtrTracking.get()) { + // set the allocator with the whole reduced address space range - pageSize (base address) to + // avoid starting address of the heap to be 0, which could be interpreted as invalid address + // nullPtr. + this->limitedGpuAddressRangeAllocator.reset(new AllocatorLimitedRange(MemoryConstants::pageSize, gpuRange + 1 - MemoryConstants::pageSize)); + + // 0x1000 ~ 0xFFFFFFFF address space for external 32bit allocator // + uint64_t size = 4 * MemoryConstants::gigaByte - MemoryConstants::pageSize; + uint64_t allocatorBase = this->limitedGpuAddressRangeAllocator->allocate(size); + allocator32Bit.reset(new Allocator32bit(allocatorBase, size)); + + // 0x100000000 ~ 0x1FFFFFFFF address space for internal 32bit allocator // + size += MemoryConstants::pageSize; + allocatorBase = this->limitedGpuAddressRangeAllocator->allocate(size); + internal32bitAllocator.reset(new Allocator32bit(allocatorBase, size)); + } else { + // when in full range space, set the internal32bitAllocator using 32bit addressing allocator. + internal32bitAllocator.reset(new Allocator32bit); + } +} + void DrmMemoryManager::eraseSharedBufferObject(NEO::BufferObject *bo) { auto it = std::find(sharingBufferObjects.begin(), sharingBufferObjects.end(), bo); //If an object isReused = true, it must be in the vector @@ -129,12 +162,12 @@ uint32_t DrmMemoryManager::unreference(NEO::BufferObject *bo, bool synchronousDe uint64_t DrmMemoryManager::acquireGpuRange(size_t &size, StorageAllocatorType &storageType, bool specificBitness) { if (specificBitness && this->force32bitAllocations) { storageType = BIT32_ALLOCATOR_EXTERNAL; - return gfxPartition.heapAllocate(HeapIndex::HEAP_EXTERNAL, size); + return this->allocator32Bit->allocate(size); } - if (isLimitedRange()) { + if (limitedGpuAddressRangeAllocator.get()) { storageType = INTERNAL_ALLOCATOR_WITH_DYNAMIC_BITRANGE; - return gfxPartition.heapAllocate(HeapIndex::HEAP_STANDARD, size); + return limitedGpuAddressRangeAllocator->allocate(size); } storageType = MMAP_ALLOCATOR; @@ -150,17 +183,17 @@ void DrmMemoryManager::releaseGpuRange(void *address, size_t unmapSize, StorageA uint64_t graphicsAddress = static_cast(reinterpret_cast(address)); if (allocatorType == BIT32_ALLOCATOR_EXTERNAL) { - gfxPartition.heapFree(HeapIndex::HEAP_EXTERNAL, graphicsAddress, unmapSize); + allocator32Bit->free(graphicsAddress, unmapSize); return; } if (allocatorType == BIT32_ALLOCATOR_INTERNAL) { - gfxPartition.heapFree(internalHeapIndex, graphicsAddress, unmapSize); + internal32bitAllocator->free(graphicsAddress, unmapSize); return; } UNRECOVERABLE_IF(allocatorType != INTERNAL_ALLOCATOR_WITH_DYNAMIC_BITRANGE); - gfxPartition.heapFree(HeapIndex::HEAP_STANDARD, graphicsAddress, unmapSize); + limitedGpuAddressRangeAllocator->free(graphicsAddress, unmapSize); } NEO::BufferObject *DrmMemoryManager::allocUserptr(uintptr_t address, size_t size, uint64_t flags, bool softpin) { @@ -219,10 +252,10 @@ DrmAllocation *DrmMemoryManager::allocateGraphicsMemoryWithAlignment(const Alloc bo->isAllocated = true; - // if Limited Range Alloction is enabled, memory allocation for bo in the limited Range heap is required - if (isLimitedRange()) { + // if limitedRangeAlloction is enabled, memory allocation for bo in the limited Range heap is required + if (limitedGpuAddressRangeAllocator) { StorageAllocatorType allocType; - bo->gpuAddress = GmmHelper::canonize(acquireGpuRange(cSize, allocType, false)); + bo->gpuAddress = acquireGpuRange(cSize, allocType, false); if (!bo->gpuAddress) { bo->close(); delete bo; @@ -237,7 +270,7 @@ DrmAllocation *DrmMemoryManager::allocateGraphicsMemoryWithAlignment(const Alloc emitPinningRequest(bo, allocationData); auto allocation = new DrmAllocation(allocationData.type, bo, res, bo->gpuAddress, cSize, MemoryPool::System4KBPages, allocationData.flags.multiOsContextCapable); - allocation->setDriverAllocatedCpuPtr(isLimitedRange() ? res : nullptr); + allocation->setDriverAllocatedCpuPtr(limitedGpuAddressRangeAllocator ? res : nullptr); return allocation; } @@ -273,7 +306,7 @@ DrmAllocation *DrmMemoryManager::allocateGraphicsMemoryForNonSvmHostPtr(const Al bo->isAllocated = false; bo->setUnmapSize(alignedSize); - bo->gpuAddress = GmmHelper::canonize(gpuVirtualAddress); + bo->gpuAddress = gpuVirtualAddress; bo->setAllocationType(allocType); auto allocation = new DrmAllocation(allocationData.type, bo, const_cast(alignedPtr), gpuVirtualAddress, @@ -312,7 +345,8 @@ GraphicsAllocation *DrmMemoryManager::allocateGraphicsMemoryForImageImpl(const A return nullptr; } bo->size = allocationData.imgInfo->size; - bo->gpuAddress = GmmHelper::canonize(gpuRange); + bo->gpuAddress = gpuRange; + auto ret2 = bo->setTiling(I915_TILING_Y, static_cast(allocationData.imgInfo->rowPitch)); DEBUG_BREAK_IF(ret2 != true); ((void)(ret2)); @@ -327,14 +361,14 @@ GraphicsAllocation *DrmMemoryManager::allocateGraphicsMemoryForImageImpl(const A DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemoryImpl(const AllocationData &allocationData) { auto internal = useInternal32BitAllocator(allocationData.type); - auto allocatorToUse = internal ? internalHeapIndex : HeapIndex::HEAP_EXTERNAL; + auto allocatorToUse = internal ? internal32bitAllocator.get() : allocator32Bit.get(); auto allocatorType = internal ? BIT32_ALLOCATOR_INTERNAL : BIT32_ALLOCATOR_EXTERNAL; if (allocationData.hostPtr) { uintptr_t inputPtr = reinterpret_cast(allocationData.hostPtr); auto allocationSize = alignSizeWholePage(allocationData.hostPtr, allocationData.size); auto realAllocationSize = allocationSize; - auto gpuVirtualAddress = gfxPartition.heapAllocate(allocatorToUse, realAllocationSize); + auto gpuVirtualAddress = allocatorToUse->allocate(realAllocationSize); if (!gpuVirtualAddress) { return nullptr; } @@ -343,41 +377,47 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemoryImpl(const Allocatio BufferObject *bo = allocUserptr(alignedUserPointer, allocationSize, 0, true); if (!bo) { - gfxPartition.heapFree(allocatorToUse, gpuVirtualAddress, realAllocationSize); + allocatorToUse->free(gpuVirtualAddress, realAllocationSize); return nullptr; } bo->isAllocated = false; bo->setUnmapSize(realAllocationSize); - bo->gpuAddress = GmmHelper::canonize(gpuVirtualAddress); + bo->gpuAddress = gpuVirtualAddress; bo->setAllocationType(allocatorType); auto allocation = new DrmAllocation(allocationData.type, bo, const_cast(allocationData.hostPtr), ptrOffset(gpuVirtualAddress, inputPointerOffset), allocationSize, MemoryPool::System4KBPagesWith32BitGpuAddressing, false); allocation->set32BitAllocation(true); - allocation->setGpuBaseAddress(gfxPartition.getHeapBase(allocatorToUse)); + allocation->setGpuBaseAddress(allocatorToUse->getBase()); return allocation; } size_t alignedAllocationSize = alignUp(allocationData.size, MemoryConstants::pageSize); auto allocationSize = alignedAllocationSize; - auto res = gfxPartition.heapAllocate(allocatorToUse, allocationSize); + auto res = allocatorToUse->allocate(allocationSize); if (!res) { return nullptr; } - auto ptrAlloc = alignedMallocWrapper(alignedAllocationSize, MemoryConstants::allocationAlignment); + void *ptrAlloc = reinterpret_cast(res); - if (!ptrAlloc) { - gfxPartition.heapFree(allocatorToUse, res, allocationSize); - return nullptr; + if (limitedGpuAddressRangeAllocator) { + ptrAlloc = alignedMallocWrapper(alignedAllocationSize, MemoryConstants::allocationAlignment); + + if (!ptrAlloc) { + allocatorToUse->free(res, allocationSize); + return nullptr; + } } BufferObject *bo = allocUserptr(reinterpret_cast(ptrAlloc), alignedAllocationSize, 0, true); if (!bo) { - alignedFreeWrapper(ptrAlloc); - gfxPartition.heapFree(allocatorToUse, res, allocationSize); + if (limitedGpuAddressRangeAllocator) { + alignedFreeWrapper(ptrAlloc); + } + allocatorToUse->free(res, allocationSize); return nullptr; } @@ -385,15 +425,15 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemoryImpl(const Allocatio bo->setUnmapSize(allocationSize); bo->setAllocationType(allocatorType); - bo->gpuAddress = GmmHelper::canonize(res); + bo->gpuAddress = res; - // softpin to the GPU address, res if it uses Limited Range Allocation + // softpin to the GPU address, res if it uses limitedRange Allocation auto allocation = new DrmAllocation(allocationData.type, bo, ptrAlloc, res, alignedAllocationSize, MemoryPool::System4KBPagesWith32BitGpuAddressing, false); allocation->set32BitAllocation(true); - allocation->setGpuBaseAddress(gfxPartition.getHeapBase(allocatorToUse)); - allocation->setDriverAllocatedCpuPtr(ptrAlloc); + allocation->setGpuBaseAddress(allocatorToUse->getBase()); + allocation->setDriverAllocatedCpuPtr(limitedGpuAddressRangeAllocator ? ptrAlloc : nullptr); return allocation; } @@ -423,7 +463,7 @@ BufferObject *DrmMemoryManager::createSharedBufferObject(int boHandle, size_t si } bo->size = size; - bo->gpuAddress = GmmHelper::canonize(gpuRange); + bo->gpuAddress = gpuRange; bo->setUnmapSize(size); bo->setAllocationType(storageType); return bo; @@ -466,9 +506,9 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromSharedHandle(o if (requireSpecificBitness && this->force32bitAllocations) { drmAllocation->set32BitAllocation(true); - drmAllocation->setGpuBaseAddress(gfxPartition.getHeapBase(HeapIndex::HEAP_EXTERNAL)); - } else if (isLimitedRange()) { - drmAllocation->setGpuBaseAddress(gfxPartition.getHeapBase(HeapIndex::HEAP_STANDARD)); + drmAllocation->setGpuBaseAddress(getExternalHeapBaseAddress()); + } else if (this->limitedGpuAddressRangeAllocator.get()) { + drmAllocation->setGpuBaseAddress(this->limitedGpuAddressRangeAllocator->getBase()); } if (properties.imgInfo) { @@ -502,7 +542,7 @@ GraphicsAllocation *DrmMemoryManager::createPaddedAllocation(GraphicsAllocation if (!bo) { return nullptr; } - bo->gpuAddress = GmmHelper::canonize(gpuRange); + bo->gpuAddress = gpuRange; bo->setUnmapSize(sizeWithPadding); bo->setAllocationType(storageType); return new DrmAllocation(inputGraphicsAllocation->getAllocationType(), bo, srcPtr, ptrOffset(gpuRange, offset), sizeWithPadding, @@ -587,11 +627,11 @@ uint64_t DrmMemoryManager::getMaxApplicationAddress() { } uint64_t DrmMemoryManager::getInternalHeapBaseAddress() { - return gfxPartition.getHeapBase(internalHeapIndex); + return this->internal32bitAllocator->getBase(); } uint64_t DrmMemoryManager::getExternalHeapBaseAddress() { - return gfxPartition.getHeapBase(HeapIndex::HEAP_EXTERNAL); + return this->allocator32Bit->getBase(); } MemoryManager::AllocationStatus DrmMemoryManager::populateOsHandles(OsHandleStorage &handleStorage) { diff --git a/runtime/os_interface/linux/drm_memory_manager.h b/runtime/os_interface/linux/drm_memory_manager.h index 1480fbcaa9..fe91eec65e 100644 --- a/runtime/os_interface/linux/drm_memory_manager.h +++ b/runtime/os_interface/linux/drm_memory_manager.h @@ -9,6 +9,7 @@ #include "runtime/memory_manager/memory_manager.h" #include "runtime/os_interface/linux/drm_allocation.h" #include "runtime/os_interface/linux/drm_buffer_object.h" +#include "runtime/os_interface/linux/drm_limited_range.h" #include "runtime/os_interface/linux/drm_neo.h" #include "drm_gem_close_worker.h" @@ -17,7 +18,6 @@ #include namespace NEO { - class BufferObject; class Drm; @@ -56,7 +56,6 @@ class DrmMemoryManager : public MemoryManager { DrmGemCloseWorker *peekGemCloseWorker() const { return this->gemCloseWorker.get(); } void *reserveCpuAddressRange(size_t size) override; void releaseReservedCpuAddressRange(void *reserved, size_t size) override; - bool isLimitedRange() { return gfxPartition.isLimitedRange(); } protected: BufferObject *findAndReferenceSharedBufferObject(int boHandle); @@ -67,6 +66,7 @@ class DrmMemoryManager : public MemoryManager { bool setDomainCpu(GraphicsAllocation &graphicsAllocation, bool writeEnable); uint64_t acquireGpuRange(size_t &size, StorageAllocatorType &allocType, bool requireSpecificBitness); void releaseGpuRange(void *address, size_t unmapSize, StorageAllocatorType allocatorType); + void initInternalRangeAllocator(size_t range); void emitPinningRequest(BufferObject *bo, const AllocationData &allocationData) const; DrmAllocation *createGraphicsAllocation(OsHandleStorage &handleStorage, const AllocationData &allocationData) override; @@ -93,5 +93,7 @@ class DrmMemoryManager : public MemoryManager { decltype(&close) closeFunction = close; std::vector sharingBufferObjects; std::mutex mtx; + std::unique_ptr internal32bitAllocator; + std::unique_ptr limitedGpuAddressRangeAllocator; }; } // namespace NEO diff --git a/unit_tests/mocks/linux/mock_drm_memory_manager.h b/unit_tests/mocks/linux/mock_drm_memory_manager.h index 2db2e12365..93b64e2bdb 100644 --- a/unit_tests/mocks/linux/mock_drm_memory_manager.h +++ b/unit_tests/mocks/linux/mock_drm_memory_manager.h @@ -50,7 +50,8 @@ class TestedDrmMemoryManager : public MemoryManagerCreate { using DrmMemoryManager::allocator32Bit; using DrmMemoryManager::allocUserptr; using DrmMemoryManager::createGraphicsAllocation; - using DrmMemoryManager::gfxPartition; + using DrmMemoryManager::internal32bitAllocator; + using DrmMemoryManager::limitedGpuAddressRangeAllocator; using DrmMemoryManager::pinThreshold; using DrmMemoryManager::setDomainCpu; using DrmMemoryManager::sharingBufferObjects; @@ -100,8 +101,10 @@ class TestedDrmMemoryManager : public MemoryManagerCreate { } DrmGemCloseWorker *getgemCloseWorker() { return this->gemCloseWorker.get(); } - void forceLimitedRangeAllocator(uint64_t range) { gfxPartition.init(range); } + void forceLimitedRangeAllocator(uint64_t range) { initInternalRangeAllocator(range); } + Allocator32bit *getDrmInternal32BitAllocator() const { return internal32bitAllocator.get(); } + AllocatorLimitedRange *getDrmLimitedRangeAllocator() const { return limitedGpuAddressRangeAllocator.get(); } DrmAllocation *allocate32BitGraphicsMemory(size_t size, const void *ptr, GraphicsAllocation::AllocationType allocationType) { bool allocateMemory = ptr == nullptr; AllocationData allocationData; diff --git a/unit_tests/os_interface/linux/drm_buffer_object_tests.cpp b/unit_tests/os_interface/linux/drm_buffer_object_tests.cpp index 8256e8b2d5..5144427cc4 100644 --- a/unit_tests/os_interface/linux/drm_buffer_object_tests.cpp +++ b/unit_tests/os_interface/linux/drm_buffer_object_tests.cpp @@ -5,6 +5,7 @@ * */ +#include "runtime/os_interface/32bit_memory.h" #include "runtime/os_interface/linux/drm_buffer_object.h" #include "test.h" #include "unit_tests/os_interface/linux/device_command_stream_fixture.h" diff --git a/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp b/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp index fb3cb89619..82d4744559 100644 --- a/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp +++ b/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp @@ -56,6 +56,10 @@ AllocationProperties createAllocationProperties(size_t size, bool forcePin) { typedef Test DrmMemoryManagerTest; typedef Test DrmMemoryManagerWithExplicitExpectationsTest; +TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDefaultDrmMemoryMangerWhenItIsCreatedThenItContainsInternal32BitAllocator) { + EXPECT_NE(nullptr, memoryManager->getDrmInternal32BitAllocator()); +} + TEST_F(DrmMemoryManagerTest, GivenGraphicsAllocationWhenAddAndRemoveAllocationToHostPtrManagerThenfragmentHasCorrectValues) { void *cpuPtr = (void *)0x30000; size_t size = 0x1000; @@ -213,7 +217,7 @@ TEST_F(DrmMemoryManagerTest, pinAfterAllocateWhenAskedAndAllowedAndBigAllocation auto memoryManager = std::make_unique(false, true, false, *executionEnvironment); ASSERT_NE(nullptr, memoryManager->getPinBB()); - allocationData.size = 10 * MB; + allocationData.size = 10 * 1024 * 1024; allocationData.hostPtr = ::alignedMalloc(allocationData.size, 4096); allocationData.flags.forcePin = true; auto alloc = memoryManager->allocateGraphicsMemoryWithHostPtr(allocationData); @@ -707,8 +711,10 @@ TEST_F(DrmMemoryManagerTest, givenMemoryManagerWhenAskedFor32BitAllocationThen32 EXPECT_NE(nullptr, allocation->getUnderlyingBuffer()); EXPECT_GE(allocation->getUnderlyingBufferSize(), size); - auto address64bit = allocation->getGpuAddressToPatch(); - EXPECT_LT(address64bit, MemoryConstants::max32BitAddress); + uintptr_t address64bit = (uintptr_t)allocation->getGpuAddressToPatch(); + if (is32BitOsAllocatorAvailable) { + EXPECT_LT(address64bit, max32BitAddress); + } auto bo = allocation->getBO(); EXPECT_GE(bo->peekUnmapSize(), 0u); EXPECT_TRUE(allocation->is32BitAllocation()); @@ -738,6 +744,26 @@ TEST_F(DrmMemoryManagerTest, givenMemoryManagerWhenAskedFor32BitAllocationWhenLi memoryManager->freeGraphicsMemory(allocation); } +TEST_F(DrmMemoryManagerTest, givenMemoryManagerWhensetForce32BitAllocationsIsCalledWithTrueMutlipleTimesThenAllocatorIsReused) { + // allocator32Bit is created unconditionally when limitedRangeAllocation is enabled. + if (!memoryManager->limitedGpuAddressRangeAllocator.get()) { + EXPECT_EQ(nullptr, memoryManager->allocator32Bit.get()); + } + + memoryManager->setForce32BitAllocations(true); + EXPECT_NE(nullptr, memoryManager->allocator32Bit.get()); + auto currentAllocator = memoryManager->allocator32Bit.get(); + memoryManager->setForce32BitAllocations(true); + EXPECT_EQ(memoryManager->allocator32Bit.get(), currentAllocator); +} + +TEST_F(DrmMemoryManagerTest, givenMemoryManagerWhensetForce32BitAllocationsIsCalledWithFalseThenAllocatorIsNotDeleted) { + memoryManager->setForce32BitAllocations(true); + EXPECT_NE(nullptr, memoryManager->allocator32Bit.get()); + memoryManager->setForce32BitAllocations(false); + EXPECT_NE(nullptr, memoryManager->allocator32Bit.get()); +} + TEST_F(DrmMemoryManagerTest, Given32bitAllocatorWhenAskedForBufferAllocationThen32BitBufferIsReturned) { DebugManagerStateRestore dbgRestorer; mock->ioctl_expected.gemUserptr = 1; @@ -765,7 +791,11 @@ TEST_F(DrmMemoryManagerTest, Given32bitAllocatorWhenAskedForBufferAllocationThen auto bufferAddress = buffer->getGraphicsAllocation()->getGpuAddress(); auto baseAddress = buffer->getGraphicsAllocation()->getGpuBaseAddress(); - EXPECT_LT(ptrDiff(bufferAddress, baseAddress), MemoryConstants::max32BitAddress); + uintptr_t address64bit = (uintptr_t)bufferAddress; + + if (is32BitOsAllocatorAvailable) { + EXPECT_LT(address64bit - baseAddress, max32BitAddress); + } delete buffer; } @@ -799,16 +829,20 @@ TEST_F(DrmMemoryManagerTest, Given32bitAllocatorWhenAskedForBufferCreatedFromHos auto bufferAddress = buffer->getGraphicsAllocation()->getGpuAddress(); auto drmAllocation = static_cast(buffer->getGraphicsAllocation()); - auto baseAddress = buffer->getGraphicsAllocation()->getGpuBaseAddress(); - EXPECT_LT(ptrDiff(bufferAddress, baseAddress), MemoryConstants::max32BitAddress); + uintptr_t address64bitOnGpu = (uintptr_t)bufferAddress; + + if (is32BitOsAllocatorAvailable) { + auto baseAddress = buffer->getGraphicsAllocation()->getGpuBaseAddress(); + EXPECT_LT(address64bitOnGpu - baseAddress, max32BitAddress); + } EXPECT_TRUE(drmAllocation->is32BitAllocation()); - auto allocationCpuPtr = drmAllocation->getUnderlyingBuffer(); - auto allocationPageOffset = ptrDiff(allocationCpuPtr, alignDown(allocationCpuPtr, MemoryConstants::pageSize)); + auto allocationCpuPtr = (uintptr_t)drmAllocation->getUnderlyingBuffer(); + auto allocationPageOffset = allocationCpuPtr - alignDown(allocationCpuPtr, MemoryConstants::pageSize); - auto allocationGpuPtr = drmAllocation->getGpuAddress(); - auto allocationGpuOffset = ptrDiff(allocationGpuPtr, alignDown(allocationGpuPtr, MemoryConstants::pageSize)); + auto allocationGpuPtr = (uintptr_t)drmAllocation->getGpuAddress(); + auto allocationGpuOffset = allocationGpuPtr - alignDown(allocationGpuPtr, MemoryConstants::pageSize); auto bufferObject = drmAllocation->getBO(); @@ -861,15 +895,19 @@ TEST_F(DrmMemoryManagerTest, Given32bitAllocatorWhenAskedForBufferCreatedFrom64B EXPECT_TRUE(buffer->isMemObjZeroCopy()); auto bufferAddress = buffer->getGraphicsAllocation()->getGpuAddress(); - auto baseAddress = buffer->getGraphicsAllocation()->getGpuBaseAddress(); - EXPECT_LT(ptrDiff(bufferAddress, baseAddress), MemoryConstants::max32BitAddress); + uintptr_t address64bit = (uintptr_t)bufferAddress; + + if (is32BitOsAllocatorAvailable) { + auto baseAddress = buffer->getGraphicsAllocation()->getGpuBaseAddress(); + EXPECT_LT(address64bit - baseAddress, max32BitAddress); + } auto drmAllocation = static_cast(buffer->getGraphicsAllocation()); EXPECT_TRUE(drmAllocation->is32BitAllocation()); - auto allocationCpuPtr = drmAllocation->getUnderlyingBuffer(); - auto allocationPageOffset = ptrDiff(allocationCpuPtr, alignDown(allocationCpuPtr, MemoryConstants::pageSize)); + auto allocationCpuPtr = (uintptr_t)drmAllocation->getUnderlyingBuffer(); + auto allocationPageOffset = allocationCpuPtr - alignDown(allocationCpuPtr, MemoryConstants::pageSize); auto bufferObject = drmAllocation->getBO(); EXPECT_NE(0u, bufferObject->peekUnmapSize()); @@ -892,22 +930,21 @@ TEST_F(DrmMemoryManagerTest, Given32bitAllocatorWhenAskedForBufferCreatedFrom64B TEST_F(DrmMemoryManagerTest, givenMemoryManagerWhenLimitedRangeAllocatorSetThenHeapSizeAndEndAddrCorrectlySetForGivenGpuRange) { memoryManager->forceLimitedRangeAllocator(0xFFFFFFFFF); - uint64_t sizeBig = 4 * MemoryConstants::megaByte + MemoryConstants::pageSize; - auto gpuAddressLimitedRange = memoryManager->gfxPartition.heapAllocate(HeapIndex::HEAP_STANDARD, sizeBig); - EXPECT_LT(memoryManager->gfxPartition.getHeapBase(HeapIndex::HEAP_STANDARD), gpuAddressLimitedRange); - EXPECT_GT(memoryManager->gfxPartition.getHeapLimit(HeapIndex::HEAP_STANDARD), gpuAddressLimitedRange + sizeBig); - EXPECT_EQ(memoryManager->gfxPartition.getHeapMinimalAddress(HeapIndex::HEAP_STANDARD), gpuAddressLimitedRange); + // check if limitedGpuAddressRangeAllocator is initialized + EXPECT_NE(memoryManager->limitedGpuAddressRangeAllocator.get(), nullptr); - auto gpuInternal32BitAlloc = memoryManager->gfxPartition.heapAllocate(internalHeapIndex, sizeBig); - EXPECT_LT(memoryManager->gfxPartition.getHeapBase(internalHeapIndex), gpuInternal32BitAlloc); - EXPECT_GT(memoryManager->gfxPartition.getHeapLimit(internalHeapIndex), gpuInternal32BitAlloc + sizeBig); - EXPECT_EQ(memoryManager->gfxPartition.getHeapMinimalAddress(internalHeapIndex), gpuInternal32BitAlloc); + uint64_t size = MemoryConstants::pageSize; + auto baseAddressLimitedRange = memoryManager->limitedGpuAddressRangeAllocator->allocate(size); + EXPECT_EQ(0x1000000000u, baseAddressLimitedRange + size); + + auto baseInternal32BitAlloc = memoryManager->internal32bitAllocator->allocate(size); + EXPECT_EQ(memoryManager->internal32bitAllocator->getBase() + 4 * MemoryConstants::gigaByte, baseInternal32BitAlloc + size); } TEST_F(DrmMemoryManagerTest, givenMemoryManagerWhenAskedForAllocationWithAlignmentThenCorrectAllocatorTypeSelected) { // if limitedRangeAllocator is enabled by default on the platform, only limitedRangeAllocator case will be tested. - auto limitedRange = memoryManager->isLimitedRange(); - if (limitedRange) { + auto limitedRangeAllocator = memoryManager->getDrmLimitedRangeAllocator(); + if (limitedRangeAllocator) { mock->ioctl_expected.gemUserptr = 1; mock->ioctl_expected.gemWait = 1; mock->ioctl_expected.gemClose = 1; @@ -924,7 +961,7 @@ TEST_F(DrmMemoryManagerTest, givenMemoryManagerWhenAskedForAllocationWithAlignme // if limitedRangeAllocator is enabled by default on the platform, expect the allocator type // is always INTERNAL_ALLOCATOR_WITH_DYNAMIC_BITRANGE - if (limitedRange) { + if (limitedRangeAllocator) { EXPECT_EQ(INTERNAL_ALLOCATOR_WITH_DYNAMIC_BITRANGE, bo->peekAllocationType()); memoryManager->freeGraphicsMemory(allocation); } else { @@ -949,12 +986,9 @@ TEST_F(DrmMemoryManagerTest, givenMemoryManagerWhenAskedForAllocationWithAlignme TestedDrmMemoryManager::AllocationData allocationData; - // emulate GPU address space exhaust - memoryManager->forceLimitedRangeAllocator(0xFFFFFFFFF); - memoryManager->gfxPartition.heapInit(HeapIndex::HEAP_STANDARD, 0x0, 0x10000); - + memoryManager->forceLimitedRangeAllocator(0xFFFF); // set size to something bigger than allowed space - allocationData.size = 0x20000; + allocationData.size = 0x10000; EXPECT_EQ(nullptr, memoryManager->allocateGraphicsMemoryWithAlignment(allocationData)); } @@ -1009,7 +1043,7 @@ TEST_F(DrmMemoryManagerTest, GivenSizeAbove2GBWhenUseHostPtrAndAllocHostPtrAreCr memoryManager->setForce32BitAllocations(true); context.setMemoryManager(memoryManager); - size_t size = 2 * GB; + size_t size = 2 * 1024 * 1024 * 1024u; void *ptr = reinterpret_cast(0x100000000000); auto retVal = CL_SUCCESS; @@ -1020,7 +1054,7 @@ TEST_F(DrmMemoryManagerTest, GivenSizeAbove2GBWhenUseHostPtrAndAllocHostPtrAreCr ptr, retVal); - size_t size2 = 4 * GB - MemoryConstants::pageSize; // Keep size aligned + size_t size2 = 4 * 1024 * 1024 * 1024u - 1u; auto buffer2 = Buffer::create( &context, @@ -1032,12 +1066,12 @@ TEST_F(DrmMemoryManagerTest, GivenSizeAbove2GBWhenUseHostPtrAndAllocHostPtrAreCr EXPECT_NE(retVal, CL_SUCCESS); EXPECT_EQ(nullptr, buffer2); - if (buffer) { + if (is32BitOsAllocatorAvailable && buffer) { auto bufferPtr = buffer->getGraphicsAllocation()->getGpuAddress(); EXPECT_TRUE(buffer->getGraphicsAllocation()->is32BitAllocation()); auto baseAddress = buffer->getGraphicsAllocation()->getGpuBaseAddress(); - EXPECT_LT(ptrDiff(bufferPtr, baseAddress), MemoryConstants::max32BitAddress); + EXPECT_LT((uintptr_t)(bufferPtr - baseAddress), max32BitAddress); } delete buffer; @@ -1051,7 +1085,7 @@ TEST_F(DrmMemoryManagerTest, GivenSizeAbove2GBWhenAllocHostPtrAndUseHostPtrAreCr memoryManager->setForce32BitAllocations(true); context.setMemoryManager(memoryManager); - size_t size = 2 * GB; + size_t size = 2 * 1024 * 1024 * 1024u; void *ptr = reinterpret_cast(0x100000000000); auto retVal = CL_SUCCESS; @@ -1062,7 +1096,7 @@ TEST_F(DrmMemoryManagerTest, GivenSizeAbove2GBWhenAllocHostPtrAndUseHostPtrAreCr nullptr, retVal); - size_t size2 = 4 * GB - MemoryConstants::pageSize; // Keep size aligned + size_t size2 = 4 * 1024 * 1024 * 1024u - 1u; auto buffer2 = Buffer::create( &context, @@ -1074,12 +1108,12 @@ TEST_F(DrmMemoryManagerTest, GivenSizeAbove2GBWhenAllocHostPtrAndUseHostPtrAreCr EXPECT_NE(retVal, CL_SUCCESS); EXPECT_EQ(nullptr, buffer2); - if (buffer) { + if (is32BitOsAllocatorAvailable && buffer) { auto bufferPtr = buffer->getGraphicsAllocation()->getGpuAddress(); EXPECT_TRUE(buffer->getGraphicsAllocation()->is32BitAllocation()); auto baseAddress = buffer->getGraphicsAllocation()->getGpuBaseAddress(); - EXPECT_LT(ptrDiff(bufferPtr, baseAddress), MemoryConstants::max32BitAddress); + EXPECT_LT((uintptr_t)(bufferPtr - baseAddress), max32BitAddress); } delete buffer; @@ -1091,11 +1125,12 @@ TEST_F(DrmMemoryManagerTest, Given32BitDeviceWithMemoryManagerWhenInternalHeapIs memoryManager->setForce32BitAllocations(true); std::unique_ptr pDevice(MockDevice::createWithNewExecutionEnvironment(nullptr)); + auto allocator = memoryManager->getDrmInternal32BitAllocator(); size_t size = getSizeToMap(); - auto alloc = memoryManager->gfxPartition.heapAllocate(internalHeapIndex, size); + auto alloc = allocator->allocate(size); EXPECT_NE(0llu, alloc); - size_t allocationSize = 4 * GB; + size_t allocationSize = 4 * 1024 * 1024 * 1024llu; auto graphicsAllocation = memoryManager->allocate32BitGraphicsMemory(allocationSize, nullptr, GraphicsAllocation::AllocationType::INTERNAL_HEAP); EXPECT_EQ(nullptr, graphicsAllocation); EXPECT_TRUE(pDevice->getDeviceInfo().force32BitAddressess); @@ -1131,7 +1166,7 @@ TEST_F(DrmMemoryManagerTest, GivenMemoryManagerWhenAllocateGraphicsMemoryForImag DrmAllocation *drmAllocation = static_cast(imageGraphicsAllocation); EXPECT_EQ(imgInfo.size, drmAllocation->getBO()->peekUnmapSize()); - if (memoryManager->isLimitedRange()) { + if (memoryManager->getDrmLimitedRangeAllocator() != nullptr) { EXPECT_EQ(INTERNAL_ALLOCATOR_WITH_DYNAMIC_BITRANGE, drmAllocation->getBO()->peekAllocationType()); } else { EXPECT_EQ(MMAP_ALLOCATOR, drmAllocation->getBO()->peekAllocationType()); @@ -1146,7 +1181,7 @@ TEST_F(DrmMemoryManagerTest, GivenMemoryManagerWhenAllocateGraphicsMemoryForImag memoryManager->freeGraphicsMemory(imageGraphicsAllocation); - if (!memoryManager->isLimitedRange()) { + if (memoryManager->getDrmLimitedRangeAllocator() == nullptr) { EXPECT_EQ(1, mmapMockCallCount); EXPECT_EQ(1, munmapMockCallCount); } @@ -1356,8 +1391,8 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenMemoryAllocatedForImageThe // if limitedRangeAllocator is enabled, gpuRange is acquired and it should be // set as unmapsize for freeing in the furture. - auto limitedRange = memoryManager->isLimitedRange(); - if (!limitedRange) { + auto limitedRangeAllocator = memoryManager->getDrmLimitedRangeAllocator(); + if (!limitedRangeAllocator) { EXPECT_EQ(0u, drmAllocation->getBO()->peekUnmapSize()); } else { EXPECT_NE(0u, drmAllocation->getBO()->peekUnmapSize()); @@ -1749,7 +1784,7 @@ TEST_F(DrmMemoryManagerTest, given32BitAddressingWhenBufferFromSharedHandleIsCre EXPECT_FALSE(graphicsAllocation->is32BitAllocation()); EXPECT_EQ(1, lseekCalledCount); - if (memoryManager->isLimitedRange()) { + if (memoryManager->getDrmLimitedRangeAllocator() != nullptr) { EXPECT_EQ(INTERNAL_ALLOCATOR_WITH_DYNAMIC_BITRANGE, drmAllocation->getBO()->peekAllocationType()); } else { EXPECT_EQ(MMAP_ALLOCATOR, drmAllocation->getBO()->peekAllocationType()); @@ -1757,7 +1792,7 @@ TEST_F(DrmMemoryManagerTest, given32BitAddressingWhenBufferFromSharedHandleIsCre memoryManager->freeGraphicsMemory(graphicsAllocation); - if (!memoryManager->isLimitedRange()) { + if (memoryManager->getDrmLimitedRangeAllocator() == nullptr) { EXPECT_EQ(1, mmapMockCallCount); EXPECT_EQ(1, munmapMockCallCount); } @@ -1794,7 +1829,7 @@ TEST_F(DrmMemoryManagerTest, givenNon32BitAddressingWhenBufferFromSharedHandleIs EXPECT_FALSE(graphicsAllocation->is32BitAllocation()); EXPECT_EQ(1, lseekCalledCount); - if (memoryManager->isLimitedRange()) { + if (memoryManager->getDrmLimitedRangeAllocator() != nullptr) { EXPECT_EQ(INTERNAL_ALLOCATOR_WITH_DYNAMIC_BITRANGE, drmAllocation->getBO()->peekAllocationType()); } else { EXPECT_EQ(MMAP_ALLOCATOR, drmAllocation->getBO()->peekAllocationType()); @@ -1802,7 +1837,7 @@ TEST_F(DrmMemoryManagerTest, givenNon32BitAddressingWhenBufferFromSharedHandleIs memoryManager->freeGraphicsMemory(graphicsAllocation); - if (!memoryManager->isLimitedRange()) { + if (memoryManager->getDrmLimitedRangeAllocator() == nullptr) { EXPECT_EQ(1, mmapMockCallCount); EXPECT_EQ(1, munmapMockCallCount); } @@ -2080,12 +2115,12 @@ TEST_F(DrmMemoryManagerTest, given32BitAllocatorWithHeapAllocatorWhenLargerFragm memoryManager->setForce32BitAllocations(true); size_t allocationSize = 4 * MemoryConstants::pageSize; - auto ptr = memoryManager->gfxPartition.heapAllocate(HeapIndex::HEAP_EXTERNAL, allocationSize); + auto ptr = memoryManager->allocator32Bit->allocate(allocationSize); size_t smallAllocationSize = MemoryConstants::pageSize; - memoryManager->gfxPartition.heapAllocate(HeapIndex::HEAP_EXTERNAL, smallAllocationSize); + memoryManager->allocator32Bit->allocate(smallAllocationSize); //now free first allocation , this will move it to chunks - memoryManager->gfxPartition.heapFree(HeapIndex::HEAP_EXTERNAL, ptr, allocationSize); + memoryManager->allocator32Bit->free(ptr, allocationSize); //now ask for 3 pages, this will give ptr from chunks size_t pages3size = 3 * MemoryConstants::pageSize; @@ -2178,6 +2213,8 @@ TEST_F(DrmMemoryManagerTest, givenMemoryManagerWhenAskedForInternalAllocationWit auto drmAllocation = static_cast(memoryManager->allocate32BitGraphicsMemory(bufferSize, ptr, GraphicsAllocation::AllocationType::INTERNAL_HEAP)); ASSERT_NE(nullptr, drmAllocation); + auto internalAllocator = memoryManager->getDrmInternal32BitAllocator(); + EXPECT_NE(nullptr, drmAllocation->getUnderlyingBuffer()); EXPECT_EQ(bufferSize, drmAllocation->getUnderlyingBufferSize()); @@ -2185,7 +2222,7 @@ TEST_F(DrmMemoryManagerTest, givenMemoryManagerWhenAskedForInternalAllocationWit auto gpuPtr = drmAllocation->getGpuAddress(); - auto heapBase = memoryManager->getInternalHeapBaseAddress(); + auto heapBase = internalAllocator->getBase(); auto heapSize = 4 * GB; EXPECT_GE(gpuPtr, heapBase); @@ -2213,6 +2250,8 @@ TEST_F(DrmMemoryManagerTest, givenLimitedRangeAllocatorWhenAskedForInternalAlloc auto drmAllocation = static_cast(memoryManager->allocate32BitGraphicsMemory(bufferSize, ptr, GraphicsAllocation::AllocationType::INTERNAL_HEAP)); ASSERT_NE(nullptr, drmAllocation); + auto internalAllocator = memoryManager->getDrmInternal32BitAllocator(); + EXPECT_NE(nullptr, drmAllocation->getUnderlyingBuffer()); EXPECT_EQ(bufferSize, drmAllocation->getUnderlyingBufferSize()); @@ -2223,7 +2262,7 @@ TEST_F(DrmMemoryManagerTest, givenLimitedRangeAllocatorWhenAskedForInternalAlloc auto gpuPtr = drmAllocation->getGpuAddress(); - auto heapBase = memoryManager->getInternalHeapBaseAddress(); + auto heapBase = internalAllocator->getBase(); auto heapSize = 4 * GB; EXPECT_GE(gpuPtr, heapBase); @@ -2277,6 +2316,8 @@ TEST_F(DrmMemoryManagerTest, givenMemoryManagerWhenAskedForInternalAllocationWit auto drmAllocation = static_cast(memoryManager->allocate32BitGraphicsMemory(bufferSize, ptr, GraphicsAllocation::AllocationType::INTERNAL_HEAP)); ASSERT_NE(nullptr, drmAllocation); + auto internalAllocator = memoryManager->getDrmInternal32BitAllocator(); + EXPECT_NE(nullptr, drmAllocation->getUnderlyingBuffer()); EXPECT_EQ(ptr, drmAllocation->getUnderlyingBuffer()); EXPECT_EQ(bufferSize, drmAllocation->getUnderlyingBufferSize()); @@ -2285,7 +2326,7 @@ TEST_F(DrmMemoryManagerTest, givenMemoryManagerWhenAskedForInternalAllocationWit auto gpuPtr = drmAllocation->getGpuAddress(); - auto heapBase = memoryManager->getInternalHeapBaseAddress(); + auto heapBase = internalAllocator->getBase(); auto heapSize = 4 * GB; EXPECT_GE(gpuPtr, heapBase); @@ -2357,6 +2398,7 @@ TEST(Allocator32BitUsingHeapAllocator, given32BitAllocatorWhenFirstMMapFailsThen auto ptr = mock32BitAllocator.allocate(size); EXPECT_NE(0llu, ptr); EXPECT_EQ(2u, mmapCallCount); + EXPECT_NE(nullptr, osInternals->heapBasePtr); EXPECT_NE(0u, osInternals->heapSize); } @@ -2379,7 +2421,7 @@ TEST(MmapFlags, givenVariousMmapParametersGetTimeDeltaForTheOperation) { std::vector pointersForFree; //allocate 4GB. - auto size = 4 * GB; + auto size = 4 * 1024 * 1024 * 1023u; unsigned int maxTime = 0; unsigned int minTime = -1; unsigned int totalTime = 0; @@ -2439,7 +2481,8 @@ TEST_F(DrmMemoryManagerBasic, givenDefaultDrmMemoryManagerWhenItIsQueriedForInte true, true, executionEnvironment)); - auto heapBase = memoryManager->gfxPartition.getHeapBase(internalHeapIndex); + auto internalAllocator = memoryManager->getDrmInternal32BitAllocator(); + auto heapBase = internalAllocator->getBase(); EXPECT_EQ(heapBase, memoryManager->getInternalHeapBaseAddress()); } @@ -2663,7 +2706,7 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenValidateHostPtrMemoryE std::unique_ptr memoryManager(new (std::nothrow) TestedDrmMemoryManager(false, true, true, *executionEnvironment)); ASSERT_NE(nullptr, memoryManager->getPinBB()); - size_t size = 10 * MB; + size_t size = 10 * 1024 * 1024; void *ptr = ::alignedMalloc(size, 4096); auto alloc = static_cast(memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{false, size}, ptr)); ASSERT_NE(nullptr, alloc); @@ -2762,7 +2805,7 @@ TEST_F(DrmMemoryManagerBasic, givenDrmMemoryManagerWhenAllocateGraphicsMemoryFor memoryManager->forceLimitedRangeAllocator(0xFFFFFFFFF); - allocationData.size = 4 * MB + 16 * 1024; + allocationData.size = 4llu * 1024 * 1024 + 16 * 1024; allocationData.hostPtr = reinterpret_cast(0x10000000); auto allocation0 = memoryManager->allocateGraphicsMemoryForNonSvmHostPtr(allocationData); @@ -2771,12 +2814,12 @@ TEST_F(DrmMemoryManagerBasic, givenDrmMemoryManagerWhenAllocateGraphicsMemoryFor memoryManager->freeGraphicsMemory(allocation0); - allocationData.size = 4 * MB + 12 * 1024; + allocationData.size = 4llu * 1024 * 1024 + 12 * 1024; allocationData.hostPtr = reinterpret_cast(0x30000000); allocation0 = memoryManager->allocateGraphicsMemoryForNonSvmHostPtr(allocationData); - EXPECT_EQ((uint64_t)(allocation0->getBO()->peekUnmapSize()), 4 * MB + 16 * 1024); - EXPECT_EQ((uint64_t)(allocation0->getBO()->peekSize()), 4 * MB + 12 * 1024); + EXPECT_EQ((uint64_t)(allocation0->getBO()->peekUnmapSize()), 4llu * 1024 * 1024 + 16 * 1024); + EXPECT_EQ((uint64_t)(allocation0->getBO()->peekSize()), 4llu * 1024 * 1024 + 12 * 1024); memoryManager->freeGraphicsMemory(allocation0); memoryManager->freeGraphicsMemory(allocation1); @@ -2788,7 +2831,7 @@ TEST_F(DrmMemoryManagerBasic, givenDrmMemoryManagerWhenAllocateGraphicsMemoryFor memoryManager->forceLimitedRangeAllocator(0xFFFFFFFFF); - allocationData.size = 64 * GB; + allocationData.size = 64llu * 1024 * 1024 * 1024; allocationData.hostPtr = reinterpret_cast(0x100000000000); EXPECT_FALSE(memoryManager->allocateGraphicsMemoryForNonSvmHostPtr(allocationData)); } @@ -2838,7 +2881,7 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenForcePinNotAllowedAndH mock->ioctl_expected.gemWait = 1; AllocationData allocationData; - allocationData.size = 10 * MB; // bigger than threshold + allocationData.size = 10 * 1024 * 1024; // bigger than threshold allocationData.hostPtr = ::alignedMalloc(allocationData.size, 4096); allocationData.flags.forcePin = true; auto alloc = memoryManager->allocateGraphicsMemoryWithHostPtr(allocationData); @@ -3000,15 +3043,32 @@ TEST_F(DrmMemoryManagerBasic, ifLimitedRangeAllocatorAvailableWhenAskedForAlloca memoryManager->forceLimitedRangeAllocator(0xFFFFFFFFF); + auto limitedRangeAllocator = memoryManager->getDrmLimitedRangeAllocator(); size_t size = 100u; - auto ptr = memoryManager->gfxPartition.heapAllocate(HeapIndex::HEAP_STANDARD, size); - auto address64bit = ptrDiff(ptr, memoryManager->gfxPartition.getHeapBase(HeapIndex::HEAP_STANDARD)); + auto ptr = limitedRangeAllocator->allocate(size); + uintptr_t address64bit = (uintptr_t)ptr - (uintptr_t)limitedRangeAllocator->getBase(); EXPECT_LT(address64bit, platformDevices[0]->capabilityTable.gpuAddressSpace); EXPECT_LT(0u, address64bit); - memoryManager->gfxPartition.heapFree(HeapIndex::HEAP_STANDARD, ptr, size); + limitedRangeAllocator->free(ptr, size); +} + +TEST_F(DrmMemoryManagerTest, givenLimitedRangeAllocatorWithNonZeroBaseAndSizeWhenAskedForBaseThenCorrectBaseIsReturned) { + uint64_t size = 100u; + uint64_t base = 0x23000; + AllocatorLimitedRange allocator(base, size); + + EXPECT_EQ(base, allocator.getBase()); +} + +TEST_F(DrmMemoryManagerTest, givenLimitedRangeAllocatorWithZeroBaseAndSizeWhenAskedForBaseThenCorrectBaseIsReturned) { + uint64_t size = 100u; + uint64_t base = 0x1000; + AllocatorLimitedRange allocator(base, size); + + EXPECT_EQ(base, allocator.getBase()); } TEST_F(DrmMemoryManagerBasic, givenDisabledHostPtrTrackingWhenAllocateGraphicsMemoryForNonSvmHostPtrIsCalledWithNotAlignedPtrIsPassedThenAllocationIsCreated) {