From 1044a8aeee6e203bf4bf525943a5cee549807124 Mon Sep 17 00:00:00 2001 From: Kamil Diedrich Date: Thu, 20 Oct 2022 08:59:02 +0200 Subject: [PATCH] Add support for USM shared in WSL for dGPU This patch force KMD allocation path for USM host allocation and also for host part of USM shared allocation Related-To: NEO-6913 Signed-off-by: Kamil Diedrich --- .../windows/wddm_memory_manager.cpp | 4 +- .../windows/wddm_memory_manager.h | 2 +- .../os_interface/windows/CMakeLists.txt | 1 + .../windows/device_command_stream_tests.cpp | 3 +- .../windows/wddm_memory_manager_test.cpp | 140 ++++++++++++++++++ 5 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 shared/test/unit_test/os_interface/windows/wddm_memory_manager_test.cpp diff --git a/shared/source/os_interface/windows/wddm_memory_manager.cpp b/shared/source/os_interface/windows/wddm_memory_manager.cpp index 28e2e3e7f9..ff395202ea 100644 --- a/shared/source/os_interface/windows/wddm_memory_manager.cpp +++ b/shared/source/os_interface/windows/wddm_memory_manager.cpp @@ -146,7 +146,7 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemoryUsingKmdAndMapItToC [[maybe_unused]] auto status = true; - if (executionEnvironment.rootDeviceEnvironments[allocationData.rootDeviceIndex]->getHardwareInfo()->capabilityTable.gpuAddressSpace >= MemoryConstants::max64BitAppAddress || is32bit) { + if (((preferredAllocationMethod == GfxMemoryAllocationMethod::UseUmdSystemPtr || allocationData.type != NEO::AllocationType::SVM_CPU) && executionEnvironment.rootDeviceEnvironments[allocationData.rootDeviceIndex]->getHardwareInfo()->capabilityTable.gpuAddressSpace >= MemoryConstants::max64BitAppAddress) || is32bit) { status = mapGpuVirtualAddress(wddmAllocation.get(), cpuPtr); } else { status = mapGpuVirtualAddress(wddmAllocation.get(), nullptr); @@ -224,7 +224,7 @@ GraphicsAllocation *WddmMemoryManager::allocateUSMHostGraphicsMemory(const Alloc GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) { auto pageSize = NEO::OSInterface::osEnabled64kbPages ? MemoryConstants::pageSize64k : MemoryConstants::pageSize; bool requiresNonStandardAlignment = allocationData.alignment > pageSize; - if ((preferredAllocationMethod == GfxMemoryAllocationMethod::UseUmdSystemPtr) || requiresNonStandardAlignment) { + if ((preferredAllocationMethod == GfxMemoryAllocationMethod::UseUmdSystemPtr) || (requiresNonStandardAlignment && allocationData.type != NEO::AllocationType::SVM_CPU)) { return allocateSystemMemoryAndCreateGraphicsAllocationFromIt(allocationData); } else { return allocateGraphicsMemoryUsingKmdAndMapItToCpuVA(allocationData, NEO::OSInterface::osEnabled64kbPages); diff --git a/shared/source/os_interface/windows/wddm_memory_manager.h b/shared/source/os_interface/windows/wddm_memory_manager.h index 17c73b526e..417c12fec5 100644 --- a/shared/source/os_interface/windows/wddm_memory_manager.h +++ b/shared/source/os_interface/windows/wddm_memory_manager.h @@ -107,7 +107,7 @@ class WddmMemoryManager : public MemoryManager { GraphicsAllocation *createAllocationFromHandle(osHandle handle, bool requireSpecificBitness, bool ntHandle, AllocationType allocationType, uint32_t rootDeviceIndex); static bool validateAllocation(WddmAllocation *alloc); MOCKABLE_VIRTUAL bool createWddmAllocation(WddmAllocation *allocation, void *requiredGpuPtr); - bool mapGpuVirtualAddress(WddmAllocation *graphicsAllocation, const void *requiredGpuPtr); + MOCKABLE_VIRTUAL bool mapGpuVirtualAddress(WddmAllocation *graphicsAllocation, const void *requiredGpuPtr); bool mapGpuVaForOneHandleAllocation(WddmAllocation *graphicsAllocation, const void *requiredGpuPtr); bool mapMultiHandleAllocationWithRetry(WddmAllocation *allocation, const void *requiredGpuPtr); bool createGpuAllocationsWithRetry(WddmAllocation *graphicsAllocation); diff --git a/shared/test/unit_test/os_interface/windows/CMakeLists.txt b/shared/test/unit_test/os_interface/windows/CMakeLists.txt index c0a2fa8133..2f52361285 100644 --- a/shared/test/unit_test/os_interface/windows/CMakeLists.txt +++ b/shared/test/unit_test/os_interface/windows/CMakeLists.txt @@ -24,6 +24,7 @@ if(WIN32) ${CMAKE_CURRENT_SOURCE_DIR}/wddm_address_space_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/wddm_command_stream_l0_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/wddm_mapper_tests.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/wddm_memory_manager_test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/wddm_preemption_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/wddm_shared_allocations_test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/wddm_special_heap_test.cpp diff --git a/shared/test/unit_test/os_interface/windows/device_command_stream_tests.cpp b/shared/test/unit_test/os_interface/windows/device_command_stream_tests.cpp index 7361d4d824..38040e683e 100644 --- a/shared/test/unit_test/os_interface/windows/device_command_stream_tests.cpp +++ b/shared/test/unit_test/os_interface/windows/device_command_stream_tests.cpp @@ -874,7 +874,7 @@ HWTEST_TEMPLATED_F(WddmCommandStreamMockGdiTest, WhenMakingResidentThenResidency HWTEST_TEMPLATED_F(WddmCommandStreamMockGdiTest, givenRecordedCommandBufferWhenItIsSubmittedThenFlushTaskIsProperlyCalled) { auto mockCsr = static_cast *>(csr); - //preemption allocation + sip allocation + // preemption allocation + sip allocation size_t csrSurfaceCount = 0; if (device->getPreemptionMode() == PreemptionMode::MidThread) { csrSurfaceCount = 2; @@ -961,6 +961,7 @@ HWTEST_TEMPLATED_F(WddmCommandStreamMockGdiTest, givenRecordedCommandBufferWhenI using WddmSimpleTest = ::testing::Test; HWTEST_F(WddmSimpleTest, givenDefaultWddmCsrWhenItIsCreatedThenBatchingIsTurnedOn) { + DebugManagerStateRestore stateRestore; DebugManager.flags.CsrDispatchMode.set(0); HardwareInfo *hwInfo = nullptr; ExecutionEnvironment *executionEnvironment = getExecutionEnvironmentImpl(hwInfo, 1); diff --git a/shared/test/unit_test/os_interface/windows/wddm_memory_manager_test.cpp b/shared/test/unit_test/os_interface/windows/wddm_memory_manager_test.cpp new file mode 100644 index 0000000000..ab10192ade --- /dev/null +++ b/shared/test/unit_test/os_interface/windows/wddm_memory_manager_test.cpp @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2022 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "shared/test/common/helpers/execution_environment_helper.h" +#include "shared/test/common/mocks/mock_device.h" +#include "shared/test/common/mocks/mock_memory_manager.h" +#include "shared/test/common/os_interface/windows/mock_wddm_memory_manager.h" +#include "shared/test/common/os_interface/windows/wddm_fixture.h" + +using namespace NEO; +using namespace ::testing; + +class MockAllocateGraphicsMemoryWithAlignmentWddm : public MemoryManagerCreate { + public: + using WddmMemoryManager::allocateGraphicsMemoryWithAlignment; + MockAllocateGraphicsMemoryWithAlignmentWddm(ExecutionEnvironment &executionEnvironment) : MemoryManagerCreate(false, false, executionEnvironment) {} + bool allocateSystemMemoryAndCreateGraphicsAllocationFromItCalled = false; + bool allocateGraphicsMemoryUsingKmdAndMapItToCpuVACalled = false; + bool mapGpuVirtualAddressWithCpuPtr = false; + + GraphicsAllocation *allocateSystemMemoryAndCreateGraphicsAllocationFromIt(const AllocationData &allocationData) override { + allocateSystemMemoryAndCreateGraphicsAllocationFromItCalled = true; + + return nullptr; + } + GraphicsAllocation *allocateGraphicsMemoryUsingKmdAndMapItToCpuVA(const AllocationData &allocationData, bool allowLargePages) override { + allocateGraphicsMemoryUsingKmdAndMapItToCpuVACalled = true; + + return nullptr; + } + bool mapGpuVirtualAddress(WddmAllocation *graphicsAllocation, const void *requiredGpuPtr) override { + if (requiredGpuPtr != nullptr) { + mapGpuVirtualAddressWithCpuPtr = true; + } else { + mapGpuVirtualAddressWithCpuPtr = false; + } + + return true; + } +}; + +class WddmMemoryManagerTests : public ::testing::Test { + public: + MockAllocateGraphicsMemoryWithAlignmentWddm *memoryManager = nullptr; + WddmMock *wddm = nullptr; + ExecutionEnvironment *executionEnvironment = nullptr; + + void SetUp() override { + HardwareInfo *hwInfo = nullptr; + executionEnvironment = getExecutionEnvironmentImpl(hwInfo, 1); + + memoryManager = new MockAllocateGraphicsMemoryWithAlignmentWddm(*executionEnvironment); + executionEnvironment->memoryManager.reset(memoryManager); + wddm = static_cast(executionEnvironment->rootDeviceEnvironments[0]->osInterface->getDriverModel()->as()); + } + + void TearDown() override { + delete executionEnvironment; + } +}; + +TEST_F(WddmMemoryManagerTests, GivenAllocDataWithSVMCPUSetWhenAllocateGraphicsMemoryWithAlignmentThenProperFunctionIsUsed) { + NEO::AllocationData allocData = {}; + allocData.type = NEO::AllocationType::SVM_CPU; + memoryManager->allocateGraphicsMemoryWithAlignment(allocData); + + if (preferredAllocationMethod == GfxMemoryAllocationMethod::AllocateByKmd) { + EXPECT_TRUE(memoryManager->allocateGraphicsMemoryUsingKmdAndMapItToCpuVACalled); + } else { + EXPECT_TRUE(memoryManager->allocateSystemMemoryAndCreateGraphicsAllocationFromItCalled); + } +} + +class MockAllocateGraphicsMemoryUsingKmdAndMapItToCpuVAWddm : public MemoryManagerCreate { + public: + using WddmMemoryManager::allocateGraphicsMemoryUsingKmdAndMapItToCpuVA; + using WddmMemoryManager::mapGpuVirtualAddress; + MockAllocateGraphicsMemoryUsingKmdAndMapItToCpuVAWddm(ExecutionEnvironment &executionEnvironment) : MemoryManagerCreate(false, false, executionEnvironment) {} + + bool mapGpuVirtualAddressWithCpuPtr = false; + + bool mapGpuVirtualAddress(WddmAllocation *graphicsAllocation, const void *requiredGpuPtr) override { + if (requiredGpuPtr != nullptr) { + mapGpuVirtualAddressWithCpuPtr = true; + } else { + mapGpuVirtualAddressWithCpuPtr = false; + } + + return true; + } +}; + +class WddmMemoryManagerAllocPathTests : public ::testing::Test { + public: + MockAllocateGraphicsMemoryUsingKmdAndMapItToCpuVAWddm *memoryManager = nullptr; + WddmMock *wddm = nullptr; + ExecutionEnvironment *executionEnvironment = nullptr; + + void SetUp() override { + HardwareInfo *hwInfo = nullptr; + executionEnvironment = getExecutionEnvironmentImpl(hwInfo, 1); + + memoryManager = new MockAllocateGraphicsMemoryUsingKmdAndMapItToCpuVAWddm(*executionEnvironment); + executionEnvironment->memoryManager.reset(memoryManager); + wddm = static_cast(executionEnvironment->rootDeviceEnvironments[0]->osInterface->getDriverModel()->as()); + } + + void TearDown() override { + delete executionEnvironment; + } +}; + +TEST_F(WddmMemoryManagerAllocPathTests, givenAllocateGraphicsMemoryUsingKmdAndMapItToCpuVAWhenPreferedAllocationMethodThenProperArgumentsAreSet) { + { + NEO::AllocationData allocData = {}; + allocData.type = NEO::AllocationType::SVM_CPU; + auto graphicsAllocation = memoryManager->allocateGraphicsMemoryUsingKmdAndMapItToCpuVA(allocData, false); + + if (preferredAllocationMethod == GfxMemoryAllocationMethod::AllocateByKmd) { + EXPECT_FALSE(memoryManager->mapGpuVirtualAddressWithCpuPtr); + } else { + EXPECT_TRUE(memoryManager->mapGpuVirtualAddressWithCpuPtr); + } + + memoryManager->freeGraphicsMemory(graphicsAllocation); + } + { + NEO::AllocationData allocData = {}; + allocData.type = NEO::AllocationType::EXTERNAL_HOST_PTR; + auto graphicsAllocation = memoryManager->allocateGraphicsMemoryUsingKmdAndMapItToCpuVA(allocData, false); + + EXPECT_TRUE(memoryManager->mapGpuVirtualAddressWithCpuPtr); + + memoryManager->freeGraphicsMemory(graphicsAllocation); + } +} \ No newline at end of file