From b32b5784c229be35c57a91d474ee93431a4c1031 Mon Sep 17 00:00:00 2001 From: Zbigniew Zdanowicz Date: Tue, 8 Dec 2020 14:03:23 +0000 Subject: [PATCH] Fix calculations for offseted addresses Signed-off-by: Zbigniew Zdanowicz --- level_zero/core/source/cmdlist/cmdlist.cpp | 4 +- level_zero/core/source/cmdlist/cmdlist.h | 2 +- level_zero/core/source/cmdlist/cmdlist_hw.h | 2 +- level_zero/core/source/cmdlist/cmdlist_hw.inl | 11 +- .../core/source/context/context_imp.cpp | 12 +- level_zero/core/source/driver/driver_handle.h | 5 +- .../core/source/driver/driver_handle_imp.cpp | 18 +- .../core/source/driver/driver_handle_imp.h | 5 +- level_zero/core/source/kernel/kernel_imp.cpp | 4 +- .../unit_tests/mocks/mock_driver_handle.h | 8 +- .../sources/cmdlist/test_cmdlist_1.cpp | 112 --------- .../sources/cmdlist/test_cmdlist_2.cpp | 36 --- .../sources/cmdlist/test_cmdlist_3.cpp | 223 ++++++++++++++++++ .../driver/host_pointer_manager_tests.cpp | 73 +++++- 14 files changed, 346 insertions(+), 169 deletions(-) diff --git a/level_zero/core/source/cmdlist/cmdlist.cpp b/level_zero/core/source/cmdlist/cmdlist.cpp index 25c2faa31d..0abf7e7781 100644 --- a/level_zero/core/source/cmdlist/cmdlist.cpp +++ b/level_zero/core/source/cmdlist/cmdlist.cpp @@ -55,13 +55,13 @@ NEO::GraphicsAllocation *CommandList::getAllocationFromHostPtrMap(const void *bu return nullptr; } -NEO::GraphicsAllocation *CommandList::getHostPtrAlloc(const void *buffer, uint64_t bufferSize, size_t *offset) { +NEO::GraphicsAllocation *CommandList::getHostPtrAlloc(const void *buffer, uint64_t bufferSize) { NEO::GraphicsAllocation *alloc = getAllocationFromHostPtrMap(buffer, bufferSize); if (alloc) { - *offset += ptrDiff(buffer, alloc->getUnderlyingBuffer()); return alloc; } alloc = device->allocateMemoryFromHostPtr(buffer, bufferSize); + UNRECOVERABLE_IF(alloc == nullptr); hostPtrMap.insert(std::make_pair(buffer, alloc)); return alloc; } diff --git a/level_zero/core/source/cmdlist/cmdlist.h b/level_zero/core/source/cmdlist/cmdlist.h index c357566acb..aa933748f7 100644 --- a/level_zero/core/source/cmdlist/cmdlist.h +++ b/level_zero/core/source/cmdlist/cmdlist.h @@ -197,7 +197,7 @@ struct CommandList : _ze_command_list_handle_t { bool indirectAllocationsAllowed = false; bool internalUsage = false; NEO::GraphicsAllocation *getAllocationFromHostPtrMap(const void *buffer, uint64_t bufferSize); - NEO::GraphicsAllocation *getHostPtrAlloc(const void *buffer, uint64_t bufferSize, size_t *offset); + NEO::GraphicsAllocation *getHostPtrAlloc(const void *buffer, uint64_t bufferSize); bool containsStatelessUncachedResource = false; }; diff --git a/level_zero/core/source/cmdlist/cmdlist_hw.h b/level_zero/core/source/cmdlist/cmdlist_hw.h index c076c176fe..b771bcbb18 100644 --- a/level_zero/core/source/cmdlist/cmdlist_hw.h +++ b/level_zero/core/source/cmdlist/cmdlist_hw.h @@ -205,7 +205,7 @@ struct CommandListCoreFamily : CommandListImp { void programThreadArbitrationPolicy(Device *device); uint64_t getInputBufferSize(NEO::ImageType imageType, uint64_t bytesPerPixel, const ze_image_region_t *region); - virtual AlignedAllocationData getAlignedAllocation(Device *device, const void *buffer, uint64_t bufferSize); + MOCKABLE_VIRTUAL AlignedAllocationData getAlignedAllocation(Device *device, const void *buffer, uint64_t bufferSize); ze_result_t addEventsToCmdList(ze_event_handle_t hEvent, uint32_t numWaitEvents, ze_event_handle_t *phWaitEvents); }; diff --git a/level_zero/core/source/cmdlist/cmdlist_hw.inl b/level_zero/core/source/cmdlist/cmdlist_hw.inl index 718fa49cfe..b887de57e6 100644 --- a/level_zero/core/source/cmdlist/cmdlist_hw.inl +++ b/level_zero/core/source/cmdlist/cmdlist_hw.inl @@ -1216,7 +1216,8 @@ ze_result_t CommandListCoreFamily::appendBlitFill(void *ptr, appendEventForProfiling(hEvent, true); NEO::GraphicsAllocation *gpuAllocation = device->getDriverHandle()->getDriverSystemMemoryAllocation(ptr, size, - neoDevice->getRootDeviceIndex()); + neoDevice->getRootDeviceIndex(), + nullptr); if (gpuAllocation == nullptr) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; } @@ -1301,17 +1302,15 @@ inline AlignedAllocationData CommandListCoreFamily::getAlignedAll if (srcAllocFound == false) { alloc = device->getDriverHandle()->findHostPointerAllocation(ptr, static_cast(bufferSize), device->getRootDeviceIndex()); - if (alloc != nullptr) { - offset += ptrDiff(buffer, alloc->getUnderlyingBuffer()); - } else { - alloc = getHostPtrAlloc(buffer, bufferSize, &offset); + if (alloc == nullptr) { + alloc = getHostPtrAlloc(buffer, bufferSize); } alignedPtr = static_cast(alignDown(alloc->getGpuAddress(), NEO::EncodeSurfaceState::getSurfaceBaseAddressAlignment())); hostPointerNeedsFlush = true; } else { alloc = allocData->gpuAllocations.getGraphicsAllocation(device->getRootDeviceIndex()); - alignedPtr = reinterpret_cast(ptr) - offset; + alignedPtr = sourcePtr; if (allocData->memoryType == InternalMemoryType::HOST_UNIFIED_MEMORY || allocData->memoryType == InternalMemoryType::SHARED_UNIFIED_MEMORY) { diff --git a/level_zero/core/source/context/context_imp.cpp b/level_zero/core/source/context/context_imp.cpp index 2d0ee45be2..8c5f5cffb7 100644 --- a/level_zero/core/source/context/context_imp.cpp +++ b/level_zero/core/source/context/context_imp.cpp @@ -88,7 +88,11 @@ ze_result_t ContextImp::freeMem(const void *ptr) { ze_result_t ContextImp::makeMemoryResident(ze_device_handle_t hDevice, void *ptr, size_t size) { Device *device = L0::Device::fromHandle(hDevice); NEO::Device *neoDevice = device->getNEODevice(); - auto allocation = device->getDriverHandle()->getDriverSystemMemoryAllocation(ptr, size, neoDevice->getRootDeviceIndex()); + auto allocation = device->getDriverHandle()->getDriverSystemMemoryAllocation( + ptr, + size, + neoDevice->getRootDeviceIndex(), + nullptr); if (allocation == nullptr) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; } @@ -112,7 +116,11 @@ ze_result_t ContextImp::makeMemoryResident(ze_device_handle_t hDevice, void *ptr ze_result_t ContextImp::evictMemory(ze_device_handle_t hDevice, void *ptr, size_t size) { Device *device = L0::Device::fromHandle(hDevice); NEO::Device *neoDevice = device->getNEODevice(); - auto allocation = device->getDriverHandle()->getDriverSystemMemoryAllocation(ptr, size, neoDevice->getRootDeviceIndex()); + auto allocation = device->getDriverHandle()->getDriverSystemMemoryAllocation( + ptr, + size, + neoDevice->getRootDeviceIndex(), + nullptr); if (allocation == nullptr) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; } diff --git a/level_zero/core/source/driver/driver_handle.h b/level_zero/core/source/driver/driver_handle.h index 457e5cd578..5fc1be7e89 100644 --- a/level_zero/core/source/driver/driver_handle.h +++ b/level_zero/core/source/driver/driver_handle.h @@ -75,7 +75,10 @@ struct DriverHandle : _ze_driver_handle_t { virtual ze_result_t getHostPointerBaseAddress(void *ptr, void **baseAddress) = 0; virtual NEO::GraphicsAllocation *findHostPointerAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) = 0; - virtual NEO::GraphicsAllocation *getDriverSystemMemoryAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) = 0; + virtual NEO::GraphicsAllocation *getDriverSystemMemoryAllocation(void *ptr, + size_t size, + uint32_t rootDeviceIndex, + uintptr_t *gpuAddress) = 0; static DriverHandle *fromHandle(ze_driver_handle_t handle) { return static_cast(handle); } inline ze_driver_handle_t toHandle() { return this; } diff --git a/level_zero/core/source/driver/driver_handle_imp.cpp b/level_zero/core/source/driver/driver_handle_imp.cpp index 3300538360..a561766323 100644 --- a/level_zero/core/source/driver/driver_handle_imp.cpp +++ b/level_zero/core/source/driver/driver_handle_imp.cpp @@ -453,13 +453,27 @@ NEO::GraphicsAllocation *DriverHandleImp::findHostPointerAllocation(void *ptr, s return nullptr; } -NEO::GraphicsAllocation *DriverHandleImp::getDriverSystemMemoryAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) { +NEO::GraphicsAllocation *DriverHandleImp::getDriverSystemMemoryAllocation(void *ptr, + size_t size, + uint32_t rootDeviceIndex, + uintptr_t *gpuAddress) { NEO::SvmAllocationData *allocData = nullptr; bool allocFound = findAllocationDataForRange(ptr, size, &allocData); if (allocFound) { + if (gpuAddress != nullptr) { + *gpuAddress = reinterpret_cast(ptr); + } return allocData->gpuAllocations.getGraphicsAllocation(rootDeviceIndex); } - return findHostPointerAllocation(ptr, size, rootDeviceIndex); + auto allocation = findHostPointerAllocation(ptr, size, rootDeviceIndex); + if (allocation != nullptr) { + if (gpuAddress != nullptr) { + uintptr_t offset = reinterpret_cast(ptr) - + reinterpret_cast(allocation->getUnderlyingBuffer()); + *gpuAddress = static_cast(allocation->getGpuAddress()) + offset; + } + } + return allocation; } } // namespace L0 diff --git a/level_zero/core/source/driver/driver_handle_imp.h b/level_zero/core/source/driver/driver_handle_imp.h index 5488791d5c..0df1451e36 100644 --- a/level_zero/core/source/driver/driver_handle_imp.h +++ b/level_zero/core/source/driver/driver_handle_imp.h @@ -76,7 +76,10 @@ struct DriverHandleImp : public DriverHandle { ze_result_t getHostPointerBaseAddress(void *ptr, void **baseAddress) override; virtual NEO::GraphicsAllocation *findHostPointerAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) override; - virtual NEO::GraphicsAllocation *getDriverSystemMemoryAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) override; + virtual NEO::GraphicsAllocation *getDriverSystemMemoryAllocation(void *ptr, + size_t size, + uint32_t rootDeviceIndex, + uintptr_t *gpuAddress) override; uint32_t parseAffinityMask(std::vector> &neoDevices); void createHostPointerManager(); diff --git a/level_zero/core/source/kernel/kernel_imp.cpp b/level_zero/core/source/kernel/kernel_imp.cpp index 4821dbbcea..29e9048902 100644 --- a/level_zero/core/source/kernel/kernel_imp.cpp +++ b/level_zero/core/source/kernel/kernel_imp.cpp @@ -524,11 +524,11 @@ ze_result_t KernelImp::setArgBuffer(uint32_t argIndex, size_t argSize, const voi uintptr_t gpuAddress = 0u; NEO::GraphicsAllocation *alloc = module->getDevice()->getDriverHandle()->getDriverSystemMemoryAllocation(requestedAddress, 1u, - module->getDevice()->getRootDeviceIndex()); + module->getDevice()->getRootDeviceIndex(), + &gpuAddress); if (nullptr == alloc) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; } - gpuAddress = static_cast(alloc->getGpuAddress()); return setArgBufferWithAlloc(argIndex, gpuAddress, alloc); } diff --git a/level_zero/core/test/unit_tests/mocks/mock_driver_handle.h b/level_zero/core/test/unit_tests/mocks/mock_driver_handle.h index e72a6ab400..f61fd0af20 100644 --- a/level_zero/core/test/unit_tests/mocks/mock_driver_handle.h +++ b/level_zero/core/test/unit_tests/mocks/mock_driver_handle.h @@ -73,9 +73,15 @@ struct Mock : public DriverHandleImp { NEO::GraphicsAllocation *findHostPointerAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) override { return nullptr; } - NEO::GraphicsAllocation *getDriverSystemMemoryAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) override { + NEO::GraphicsAllocation *getDriverSystemMemoryAllocation(void *ptr, + size_t size, + uint32_t rootDeviceIndex, + uintptr_t *gpuAddress) override { auto svmData = svmAllocsManager->getSVMAlloc(ptr); if (svmData != nullptr) { + if (gpuAddress != nullptr) { + *gpuAddress = reinterpret_cast(ptr); + } return svmData->gpuAllocations.getGraphicsAllocation(rootDeviceIndex); } return nullptr; diff --git a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_1.cpp b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_1.cpp index b68a9bb451..4e9bbbf3e6 100644 --- a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_1.cpp +++ b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_1.cpp @@ -1042,117 +1042,5 @@ HWTEST2_F(CommandListCreate, givenNullEventWhenAppendEventAfterWalkerThenNothing EXPECT_EQ(commandList->commandContainer.getCommandStream()->getUsed(), usedBefore); } -HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenGettingAllocInRangeThenAllocFromMapReturned, Platforms) { - auto commandList = std::make_unique>>(); - commandList->initialize(device, NEO::EngineGroupType::Copy); - uint64_t gpuAddress = 0x1200; - const void *cpuPtr = reinterpret_cast(gpuAddress); - size_t allocSize = 0x1000; - NEO::MockGraphicsAllocation alloc(const_cast(cpuPtr), gpuAddress, allocSize); - commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc)); - - auto newBufferPtr = ptrOffset(cpuPtr, 0x10); - auto newBufferSize = allocSize - 0x20; - auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize); - EXPECT_NE(newAlloc, nullptr); - commandList->hostPtrMap.clear(); -} - -HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenSizeIsOutOfRangeThenNullPtrReturned, Platforms) { - auto commandList = std::make_unique>>(); - commandList->initialize(device, NEO::EngineGroupType::Copy); - uint64_t gpuAddress = 0x1200; - const void *cpuPtr = reinterpret_cast(gpuAddress); - size_t allocSize = 0x1000; - NEO::MockGraphicsAllocation alloc(const_cast(cpuPtr), gpuAddress, allocSize); - commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc)); - - auto newBufferPtr = ptrOffset(cpuPtr, 0x10); - auto newBufferSize = allocSize + 0x20; - auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize); - EXPECT_EQ(newAlloc, nullptr); - commandList->hostPtrMap.clear(); -} - -HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenPtrIsOutOfRangeThenNullPtrReturned, Platforms) { - auto commandList = std::make_unique>>(); - commandList->initialize(device, NEO::EngineGroupType::Copy); - uint64_t gpuAddress = 0x1200; - const void *cpuPtr = reinterpret_cast(gpuAddress); - size_t allocSize = 0x1000; - NEO::MockGraphicsAllocation alloc(const_cast(cpuPtr), gpuAddress, allocSize); - commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc)); - - auto newBufferPtr = reinterpret_cast(gpuAddress - 0x100); - auto newBufferSize = allocSize - 0x200; - auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize); - EXPECT_EQ(newAlloc, nullptr); - commandList->hostPtrMap.clear(); -} - -HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenGetHostPtrAllocCalledThenCorrectOffsetIsSet, Platforms) { - auto commandList = std::make_unique>>(); - commandList->initialize(device, NEO::EngineGroupType::Copy); - uint64_t gpuAddress = 0x1200; - const void *cpuPtr = reinterpret_cast(gpuAddress); - size_t allocSize = 0x1000; - NEO::MockGraphicsAllocation alloc(const_cast(cpuPtr), gpuAddress, allocSize); - commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc)); - size_t expectedOffset = 0x10; - auto newBufferPtr = ptrOffset(cpuPtr, expectedOffset); - auto newBufferSize = allocSize - 0x20; - size_t offset = 0; - auto newAlloc = commandList->getHostPtrAlloc(newBufferPtr, newBufferSize, &offset); - EXPECT_NE(newAlloc, nullptr); - EXPECT_EQ(offset, expectedOffset); - commandList->hostPtrMap.clear(); -} - -HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenPtrIsInMapThenAllocationReturned, Platforms) { - auto commandList = std::make_unique>>(); - commandList->initialize(device, NEO::EngineGroupType::Copy); - uint64_t gpuAddress = 0x1200; - const void *cpuPtr = reinterpret_cast(gpuAddress); - size_t allocSize = 0x1000; - NEO::MockGraphicsAllocation alloc(const_cast(cpuPtr), gpuAddress, allocSize); - commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc)); - - auto newBufferPtr = cpuPtr; - auto newBufferSize = allocSize - 0x20; - auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize); - EXPECT_EQ(newAlloc, &alloc); - commandList->hostPtrMap.clear(); -} -HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenPtrIsInMapButWithBiggerSizeThenNullPtrReturned, Platforms) { - auto commandList = std::make_unique>>(); - commandList->initialize(device, NEO::EngineGroupType::Copy); - uint64_t gpuAddress = 0x1200; - const void *cpuPtr = reinterpret_cast(gpuAddress); - size_t allocSize = 0x1000; - NEO::MockGraphicsAllocation alloc(const_cast(cpuPtr), gpuAddress, allocSize); - commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc)); - - auto newBufferPtr = cpuPtr; - auto newBufferSize = allocSize + 0x20; - auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize); - EXPECT_EQ(newAlloc, nullptr); - commandList->hostPtrMap.clear(); -} -HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenPtrLowerThanAnyInMapThenNullPtrReturned, Platforms) { - auto commandList = std::make_unique>>(); - commandList->initialize(device, NEO::EngineGroupType::Copy); - uint64_t gpuAddress = 0x1200; - const void *cpuPtr = reinterpret_cast(gpuAddress); - size_t allocSize = 0x1000; - NEO::MockGraphicsAllocation alloc(const_cast(cpuPtr), gpuAddress, allocSize); - commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc)); - - auto newBufferPtr = reinterpret_cast(gpuAddress - 0x10); - auto newBufferSize = allocSize - 0x20; - auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize); - EXPECT_EQ(newAlloc, nullptr); - commandList->hostPtrMap.clear(); -} - } // namespace ult } // namespace L0 diff --git a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_2.cpp b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_2.cpp index 876c3dfaa6..02521630e1 100644 --- a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_2.cpp +++ b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_2.cpp @@ -17,7 +17,6 @@ #include "level_zero/core/source/driver/driver_handle_imp.h" #include "level_zero/core/source/image/image_hw.h" #include "level_zero/core/test/unit_tests/fixtures/device_fixture.h" -#include "level_zero/core/test/unit_tests/fixtures/host_pointer_manager_fixture.h" #include "level_zero/core/test/unit_tests/mocks/mock_cmdlist.h" #include "level_zero/core/test/unit_tests/mocks/mock_cmdqueue.h" #include "level_zero/core/test/unit_tests/mocks/mock_event.h" @@ -1104,40 +1103,5 @@ HWTEST_F(CommandListCreate, GivenCommandListWhenUnalignedPtrThenLeftMiddleAndRig EXPECT_NE(cmdList.end(), itor); } -using HostPointerManagerCommandListTest = Test; -HWTEST2_F(HostPointerManagerCommandListTest, - givenImportedHostPointerWhenAppendMemoryFillUsingHostPointerThenAppendFillUsingHostPointerAllocation, - Platforms) { - auto commandList = std::make_unique>>(); - commandList->initialize(device, NEO::EngineGroupType::RenderCompute); - - auto ret = hostDriverHandle->importExternalPointer(heapPointer, MemoryConstants::pageSize); - EXPECT_EQ(ZE_RESULT_SUCCESS, ret); - - int pattern = 1; - ret = commandList->appendMemoryFill(heapPointer, reinterpret_cast(&pattern), sizeof(pattern), 64u, nullptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, ret); - - ret = hostDriverHandle->releaseImportedPointer(heapPointer); - EXPECT_EQ(ZE_RESULT_SUCCESS, ret); -} - -HWTEST2_F(HostPointerManagerCommandListTest, - givenImportedHostPointerAndCopyEngineWhenAppendMemoryFillUsingHostPointerThenAppendFillUsingHostPointerAllocation, - Platforms) { - auto commandList = std::make_unique>>(); - commandList->initialize(device, NEO::EngineGroupType::Copy); - - auto ret = hostDriverHandle->importExternalPointer(heapPointer, MemoryConstants::pageSize); - EXPECT_EQ(ZE_RESULT_SUCCESS, ret); - - int pattern = 1; - ret = commandList->appendMemoryFill(heapPointer, reinterpret_cast(&pattern), sizeof(pattern), 64u, nullptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, ret); - - ret = hostDriverHandle->releaseImportedPointer(heapPointer); - EXPECT_EQ(ZE_RESULT_SUCCESS, ret); -} - } // namespace ult } // namespace L0 diff --git a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_3.cpp b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_3.cpp index 4d656a8b0c..bf053152f5 100644 --- a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_3.cpp +++ b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_3.cpp @@ -9,11 +9,14 @@ #include "test.h" #include "level_zero/core/test/unit_tests/fixtures/device_fixture.h" +#include "level_zero/core/test/unit_tests/fixtures/host_pointer_manager_fixture.h" #include "level_zero/core/test/unit_tests/mocks/mock_cmdlist.h" namespace L0 { namespace ult { +using Platforms = IsAtLeastProduct; + struct MemoryManagerCommandListCreateNegativeTest : public NEO::MockMemoryManager { MemoryManagerCommandListCreateNegativeTest(NEO::ExecutionEnvironment &executionEnvironment) : NEO::MockMemoryManager(const_cast(executionEnvironment)) {} NEO::GraphicsAllocation *allocateGraphicsMemoryWithProperties(const NEO::AllocationProperties &properties) override { @@ -80,5 +83,225 @@ TEST_F(CommandListCreateNegativeTest, whenDeviceAllocationFailsDuringCommandList EXPECT_EQ(ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY, returnValue); ASSERT_EQ(nullptr, commandList); } + +using CommandListCreate = Test; + +HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenGettingAllocInRangeThenAllocFromMapReturned, Platforms) { + auto commandList = std::make_unique>>(); + commandList->initialize(device, NEO::EngineGroupType::Copy); + uint64_t gpuAddress = 0x1200; + const void *cpuPtr = reinterpret_cast(gpuAddress); + size_t allocSize = 0x1000; + NEO::MockGraphicsAllocation alloc(const_cast(cpuPtr), gpuAddress, allocSize); + commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc)); + + auto newBufferPtr = ptrOffset(cpuPtr, 0x10); + auto newBufferSize = allocSize - 0x20; + auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize); + EXPECT_NE(newAlloc, nullptr); + commandList->hostPtrMap.clear(); +} + +HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenSizeIsOutOfRangeThenNullPtrReturned, Platforms) { + auto commandList = std::make_unique>>(); + commandList->initialize(device, NEO::EngineGroupType::Copy); + uint64_t gpuAddress = 0x1200; + const void *cpuPtr = reinterpret_cast(gpuAddress); + size_t allocSize = 0x1000; + NEO::MockGraphicsAllocation alloc(const_cast(cpuPtr), gpuAddress, allocSize); + commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc)); + + auto newBufferPtr = ptrOffset(cpuPtr, 0x10); + auto newBufferSize = allocSize + 0x20; + auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize); + EXPECT_EQ(newAlloc, nullptr); + commandList->hostPtrMap.clear(); +} + +HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenPtrIsOutOfRangeThenNullPtrReturned, Platforms) { + auto commandList = std::make_unique>>(); + commandList->initialize(device, NEO::EngineGroupType::Copy); + uint64_t gpuAddress = 0x1200; + const void *cpuPtr = reinterpret_cast(gpuAddress); + size_t allocSize = 0x1000; + NEO::MockGraphicsAllocation alloc(const_cast(cpuPtr), gpuAddress, allocSize); + commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc)); + + auto newBufferPtr = reinterpret_cast(gpuAddress - 0x100); + auto newBufferSize = allocSize - 0x200; + auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize); + EXPECT_EQ(newAlloc, nullptr); + commandList->hostPtrMap.clear(); +} + +HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenGetHostPtrAllocCalledThenCorrectOffsetIsSet, Platforms) { + auto commandList = std::make_unique>>(); + commandList->initialize(device, NEO::EngineGroupType::Copy); + uint64_t gpuAddress = 0x1200; + const void *cpuPtr = reinterpret_cast(gpuAddress); + size_t allocSize = 0x1000; + NEO::MockGraphicsAllocation alloc(const_cast(cpuPtr), gpuAddress, allocSize); + commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc)); + size_t expectedOffset = 0x10; + auto newBufferPtr = ptrOffset(cpuPtr, expectedOffset); + auto newBufferSize = allocSize - 0x20; + auto newAlloc = commandList->getHostPtrAlloc(newBufferPtr, newBufferSize); + EXPECT_NE(nullptr, newAlloc); + commandList->hostPtrMap.clear(); +} + +HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenPtrIsInMapThenAllocationReturned, Platforms) { + auto commandList = std::make_unique>>(); + commandList->initialize(device, NEO::EngineGroupType::Copy); + uint64_t gpuAddress = 0x1200; + const void *cpuPtr = reinterpret_cast(gpuAddress); + size_t allocSize = 0x1000; + NEO::MockGraphicsAllocation alloc(const_cast(cpuPtr), gpuAddress, allocSize); + commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc)); + + auto newBufferPtr = cpuPtr; + auto newBufferSize = allocSize - 0x20; + auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize); + EXPECT_EQ(newAlloc, &alloc); + commandList->hostPtrMap.clear(); +} + +HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenPtrIsInMapButWithBiggerSizeThenNullPtrReturned, Platforms) { + auto commandList = std::make_unique>>(); + commandList->initialize(device, NEO::EngineGroupType::Copy); + uint64_t gpuAddress = 0x1200; + const void *cpuPtr = reinterpret_cast(gpuAddress); + size_t allocSize = 0x1000; + NEO::MockGraphicsAllocation alloc(const_cast(cpuPtr), gpuAddress, allocSize); + commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc)); + + auto newBufferPtr = cpuPtr; + auto newBufferSize = allocSize + 0x20; + auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize); + EXPECT_EQ(newAlloc, nullptr); + commandList->hostPtrMap.clear(); +} + +HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenPtrLowerThanAnyInMapThenNullPtrReturned, Platforms) { + auto commandList = std::make_unique>>(); + commandList->initialize(device, NEO::EngineGroupType::Copy); + uint64_t gpuAddress = 0x1200; + const void *cpuPtr = reinterpret_cast(gpuAddress); + size_t allocSize = 0x1000; + NEO::MockGraphicsAllocation alloc(const_cast(cpuPtr), gpuAddress, allocSize); + commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc)); + + auto newBufferPtr = reinterpret_cast(gpuAddress - 0x10); + auto newBufferSize = allocSize - 0x20; + auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize); + EXPECT_EQ(newAlloc, nullptr); + commandList->hostPtrMap.clear(); +} + +HWTEST2_F(CommandListCreate, + givenCmdListHostPointerUsedWhenGettingAlignedAllocationThenRetrieveProperOffsetAndAddress, + Platforms) { + auto commandList = std::make_unique<::L0::ult::CommandListCoreFamily>(); + commandList->initialize(device, NEO::EngineGroupType::RenderCompute); + + size_t cmdListHostPtrSize = MemoryConstants::pageSize; + void *cmdListHostBuffer = device->getNEODevice()->getMemoryManager()->allocateSystemMemory(cmdListHostPtrSize, cmdListHostPtrSize); + void *startMemory = cmdListHostBuffer; + void *baseAddress = alignDown(startMemory, MemoryConstants::pageSize); + size_t expectedOffset = ptrDiff(startMemory, baseAddress); + + AlignedAllocationData outData = commandList->getAlignedAllocation(device, startMemory, cmdListHostPtrSize); + ASSERT_NE(nullptr, outData.alloc); + auto firstAlloc = outData.alloc; + auto expectedGpuAddress = static_cast(alignDown(outData.alloc->getGpuAddress(), MemoryConstants::pageSize)); + EXPECT_EQ(startMemory, outData.alloc->getUnderlyingBuffer()); + EXPECT_EQ(expectedGpuAddress, outData.alignedAllocationPtr); + EXPECT_EQ(expectedOffset, outData.offset); + + size_t offset = 0x20u; + void *offsetMemory = ptrOffset(startMemory, offset); + expectedOffset = ptrDiff(offsetMemory, baseAddress); + EXPECT_EQ(outData.offset + offset, expectedOffset); + + outData = commandList->getAlignedAllocation(device, offsetMemory, 4u); + ASSERT_NE(nullptr, outData.alloc); + EXPECT_EQ(firstAlloc, outData.alloc); + EXPECT_EQ(startMemory, outData.alloc->getUnderlyingBuffer()); + EXPECT_EQ(expectedGpuAddress, outData.alignedAllocationPtr); + EXPECT_EQ(expectedOffset, outData.offset); + + commandList->removeHostPtrAllocations(); + device->getNEODevice()->getMemoryManager()->freeSystemMemory(cmdListHostBuffer); +} + +using HostPointerManagerCommandListTest = Test; +HWTEST2_F(HostPointerManagerCommandListTest, + givenImportedHostPointerWhenAppendMemoryFillUsingHostPointerThenAppendFillUsingHostPointerAllocation, + Platforms) { + auto commandList = std::make_unique>>(); + commandList->initialize(device, NEO::EngineGroupType::RenderCompute); + + auto ret = hostDriverHandle->importExternalPointer(heapPointer, MemoryConstants::pageSize); + EXPECT_EQ(ZE_RESULT_SUCCESS, ret); + + int pattern = 1; + ret = commandList->appendMemoryFill(heapPointer, reinterpret_cast(&pattern), sizeof(pattern), 64u, nullptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, ret); + + ret = hostDriverHandle->releaseImportedPointer(heapPointer); + EXPECT_EQ(ZE_RESULT_SUCCESS, ret); +} + +HWTEST2_F(HostPointerManagerCommandListTest, + givenImportedHostPointerAndCopyEngineWhenAppendMemoryFillUsingHostPointerThenAppendFillUsingHostPointerAllocation, + Platforms) { + auto commandList = std::make_unique>>(); + commandList->initialize(device, NEO::EngineGroupType::Copy); + + auto ret = hostDriverHandle->importExternalPointer(heapPointer, MemoryConstants::pageSize); + EXPECT_EQ(ZE_RESULT_SUCCESS, ret); + + int pattern = 1; + ret = commandList->appendMemoryFill(heapPointer, reinterpret_cast(&pattern), sizeof(pattern), 64u, nullptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, ret); + + ret = hostDriverHandle->releaseImportedPointer(heapPointer); + EXPECT_EQ(ZE_RESULT_SUCCESS, ret); +} + +HWTEST2_F(HostPointerManagerCommandListTest, + givenHostPointerImportedWhenGettingAlignedAllocationThenRetrieveProperOffsetAndAddress, + Platforms) { + auto commandList = std::make_unique<::L0::ult::CommandListCoreFamily>(); + commandList->initialize(device, NEO::EngineGroupType::RenderCompute); + + size_t pointerSize = MemoryConstants::pageSize; + size_t offset = 100; + void *offsetPointer = ptrOffset(heapPointer, offset); + + auto ret = hostDriverHandle->importExternalPointer(offsetPointer, offset); + EXPECT_EQ(ZE_RESULT_SUCCESS, ret); + + auto hostAllocation = hostDriverHandle->findHostPointerAllocation(heapPointer, pointerSize, device->getRootDeviceIndex()); + ASSERT_NE(nullptr, hostAllocation); + + AlignedAllocationData outData = commandList->getAlignedAllocation(device, heapPointer, pointerSize); + auto expectedAlignedAddress = static_cast(hostAllocation->getGpuAddress()); + EXPECT_EQ(heapPointer, hostAllocation->getUnderlyingBuffer()); + EXPECT_EQ(expectedAlignedAddress, outData.alignedAllocationPtr); + EXPECT_EQ(hostAllocation, outData.alloc); + EXPECT_EQ(0u, outData.offset); + + outData = commandList->getAlignedAllocation(device, offsetPointer, 2u); + expectedAlignedAddress = static_cast(hostAllocation->getGpuAddress()); + EXPECT_EQ(heapPointer, hostAllocation->getUnderlyingBuffer()); + EXPECT_EQ(expectedAlignedAddress, outData.alignedAllocationPtr); + EXPECT_EQ(hostAllocation, outData.alloc); + EXPECT_EQ(offset, outData.offset); + + ret = hostDriverHandle->releaseImportedPointer(heapPointer); + EXPECT_EQ(ZE_RESULT_SUCCESS, ret); +} + } // namespace ult } // namespace L0 diff --git a/level_zero/core/test/unit_tests/sources/driver/host_pointer_manager_tests.cpp b/level_zero/core/test/unit_tests/sources/driver/host_pointer_manager_tests.cpp index 2c259b9323..4ca5136508 100644 --- a/level_zero/core/test/unit_tests/sources/driver/host_pointer_manager_tests.cpp +++ b/level_zero/core/test/unit_tests/sources/driver/host_pointer_manager_tests.cpp @@ -52,10 +52,40 @@ TEST_F(HostPointerManagerTest, givenNoHeapManagerThenReturnFeatureUnsupported) { result = hostDriverHandle->releaseImportedPointer(testPtr); EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, result); - auto gfxAllocation = hostDriverHandle->getDriverSystemMemoryAllocation(testPtr, 1u, device->getRootDeviceIndex()); + auto gfxAllocation = hostDriverHandle->getDriverSystemMemoryAllocation(testPtr, 1u, device->getRootDeviceIndex(), nullptr); EXPECT_EQ(nullptr, gfxAllocation); } +TEST_F(HostPointerManagerTest, givenHostPointerImportedWhenGettingExistingAllocationThenRetrieveProperGpuAddress) { + void *testPtr = heapPointer; + + auto result = hostDriverHandle->importExternalPointer(testPtr, MemoryConstants::pageSize); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + uintptr_t gpuAddress = 0u; + auto gfxAllocation = hostDriverHandle->getDriverSystemMemoryAllocation(testPtr, + 1u, + device->getRootDeviceIndex(), + &gpuAddress); + ASSERT_NE(nullptr, gfxAllocation); + EXPECT_EQ(testPtr, gfxAllocation->getUnderlyingBuffer()); + EXPECT_EQ(static_cast(gfxAllocation->getGpuAddress()), gpuAddress); + + size_t offset = 10u; + testPtr = ptrOffset(testPtr, offset); + gfxAllocation = hostDriverHandle->getDriverSystemMemoryAllocation(testPtr, + 1u, + device->getRootDeviceIndex(), + &gpuAddress); + ASSERT_NE(nullptr, gfxAllocation); + EXPECT_EQ(heapPointer, gfxAllocation->getUnderlyingBuffer()); + auto expectedGpuAddress = static_cast(gfxAllocation->getGpuAddress()) + offset; + EXPECT_EQ(expectedGpuAddress, gpuAddress); + + result = hostDriverHandle->releaseImportedPointer(heapPointer); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); +} + TEST_F(HostPointerManagerTest, givenPointerRegisteredWhenSvmAllocationExistsThenRetrieveSvmFirst) { void *testPtr = heapPointer; @@ -77,7 +107,7 @@ TEST_F(HostPointerManagerTest, givenPointerRegisteredWhenSvmAllocationExistsThen auto result = hostDriverHandle->importExternalPointer(testPtr, MemoryConstants::pageSize); EXPECT_EQ(ZE_RESULT_SUCCESS, result); - auto gfxAllocation = hostDriverHandle->getDriverSystemMemoryAllocation(usmBuffer, 1u, device->getRootDeviceIndex()); + auto gfxAllocation = hostDriverHandle->getDriverSystemMemoryAllocation(usmBuffer, 1u, device->getRootDeviceIndex(), nullptr); ASSERT_NE(nullptr, gfxAllocation); EXPECT_EQ(usmBuffer, gfxAllocation->getUnderlyingBuffer()); @@ -88,6 +118,45 @@ TEST_F(HostPointerManagerTest, givenPointerRegisteredWhenSvmAllocationExistsThen hostDriverHandle->getMemoryManager()->freeSystemMemory(usmBuffer); } +TEST_F(HostPointerManagerTest, givenSvmAllocationExistsWhenGettingExistingAllocationThenRetrieveProperGpuAddress) { + size_t usmSize = MemoryConstants::pageSize; + void *usmBuffer = hostDriverHandle->getMemoryManager()->allocateSystemMemory(usmSize, usmSize); + NEO::GraphicsAllocation *usmAllocation = hostDriverHandle->getMemoryManager()->allocateGraphicsMemoryWithProperties( + {device->getRootDeviceIndex(), false, usmSize, NEO::GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, false, neoDevice->getDeviceBitfield()}, + usmBuffer); + ASSERT_NE(nullptr, usmAllocation); + + NEO::SvmAllocationData allocData(device->getRootDeviceIndex()); + allocData.gpuAllocations.addAllocation(usmAllocation); + allocData.cpuAllocation = nullptr; + allocData.size = usmSize; + allocData.memoryType = InternalMemoryType::NOT_SPECIFIED; + allocData.device = nullptr; + hostDriverHandle->getSvmAllocsManager()->insertSVMAlloc(allocData); + + uintptr_t gpuAddress = 0u; + auto gfxAllocation = hostDriverHandle->getDriverSystemMemoryAllocation(usmBuffer, + 1u, + device->getRootDeviceIndex(), + &gpuAddress); + ASSERT_NE(nullptr, gfxAllocation); + EXPECT_EQ(usmBuffer, gfxAllocation->getUnderlyingBuffer()); + EXPECT_EQ(static_cast(gfxAllocation->getGpuAddress()), gpuAddress); + + size_t offset = 10u; + void *offsetUsmBuffer = ptrOffset(usmBuffer, offset); + gfxAllocation = hostDriverHandle->getDriverSystemMemoryAllocation(offsetUsmBuffer, + 1u, + device->getRootDeviceIndex(), + &gpuAddress); + ASSERT_NE(nullptr, gfxAllocation); + EXPECT_EQ(usmBuffer, gfxAllocation->getUnderlyingBuffer()); + EXPECT_EQ(reinterpret_cast(offsetUsmBuffer), gpuAddress); + + hostDriverHandle->getMemoryManager()->freeGraphicsMemory(usmAllocation); + hostDriverHandle->getMemoryManager()->freeSystemMemory(usmBuffer); +} + TEST_F(HostPointerManagerTest, WhenSizeIsZeroThenExpectInvalidArgument) { void *testPtr = heapPointer;