From a8ce3ca00a3e7a925bfda24fdc44363c665c108c Mon Sep 17 00:00:00 2001 From: "Dunajski, Bartosz" Date: Thu, 12 Jul 2018 15:42:46 +0200 Subject: [PATCH] Wddm: Use GMM allocation size during map GPU VA Change-Id: Ie10898db7c539ce5025ab4a6d658d6e593e94c50 --- runtime/os_interface/windows/wddm/wddm.cpp | 9 +++--- runtime/os_interface/windows/wddm/wddm.h | 4 +-- .../windows/wddm_memory_manager.cpp | 8 ++--- unit_tests/mocks/mock_gmm_resource_info.h | 3 +- unit_tests/mocks/mock_wddm20.cpp | 4 +-- unit_tests/mocks/mock_wddm20.h | 2 +- .../os_interface/windows/wddm20_tests.cpp | 31 +++++++++++++++---- .../windows/wddm_kmdaf_listener_tests.cpp | 6 ++-- .../windows/wddm_memory_manager_tests.cpp | 8 ++--- 9 files changed, 48 insertions(+), 27 deletions(-) diff --git a/runtime/os_interface/windows/wddm/wddm.cpp b/runtime/os_interface/windows/wddm/wddm.cpp index 87a36c64bd..5f963500ad 100644 --- a/runtime/os_interface/windows/wddm/wddm.cpp +++ b/runtime/os_interface/windows/wddm/wddm.cpp @@ -344,26 +344,27 @@ bool Wddm::makeResident(D3DKMT_HANDLE *handles, uint32_t count, bool cantTrimFur return success; } -bool Wddm::mapGpuVirtualAddress(WddmAllocation *allocation, void *cpuPtr, uint64_t size, bool allocation32bit, bool use64kbPages, bool useHeap1) { +bool Wddm::mapGpuVirtualAddress(WddmAllocation *allocation, void *cpuPtr, bool allocation32bit, bool use64kbPages, bool useHeap1) { void *mapPtr = allocation->getReservedAddress() != nullptr ? allocation->getReservedAddress() : cpuPtr; - return mapGpuVirtualAddressImpl(allocation->gmm, allocation->handle, mapPtr, size, allocation->gpuPtr, allocation32bit, use64kbPages, useHeap1); + return mapGpuVirtualAddressImpl(allocation->gmm, allocation->handle, mapPtr, allocation->gpuPtr, allocation32bit, use64kbPages, useHeap1); } bool Wddm::mapGpuVirtualAddress(AllocationStorageData *allocationStorageData, bool allocation32bit, bool use64kbPages) { return mapGpuVirtualAddressImpl(allocationStorageData->osHandleStorage->gmm, allocationStorageData->osHandleStorage->handle, const_cast(allocationStorageData->cpuPtr), - allocationStorageData->fragmentSize, allocationStorageData->osHandleStorage->gpuPtr, allocation32bit, use64kbPages, false); } -bool Wddm::mapGpuVirtualAddressImpl(Gmm *gmm, D3DKMT_HANDLE handle, void *cpuPtr, uint64_t size, D3DGPU_VIRTUAL_ADDRESS &gpuPtr, bool allocation32bit, bool use64kbPages, bool useHeap1) { +bool Wddm::mapGpuVirtualAddressImpl(Gmm *gmm, D3DKMT_HANDLE handle, void *cpuPtr, D3DGPU_VIRTUAL_ADDRESS &gpuPtr, bool allocation32bit, bool use64kbPages, bool useHeap1) { NTSTATUS status = STATUS_SUCCESS; D3DDDI_MAPGPUVIRTUALADDRESS MapGPUVA = {0}; D3DDDIGPUVIRTUALADDRESS_PROTECTION_TYPE protectionType = {{{0}}}; protectionType.Write = TRUE; + uint64_t size = static_cast(gmm->gmmResourceInfo->getSizeAllocation()); + MapGPUVA.hPagingQueue = pagingQueue; MapGPUVA.hAllocation = handle; MapGPUVA.Protection = protectionType; diff --git a/runtime/os_interface/windows/wddm/wddm.h b/runtime/os_interface/windows/wddm/wddm.h index 637b160ad8..50154b4b92 100644 --- a/runtime/os_interface/windows/wddm/wddm.h +++ b/runtime/os_interface/windows/wddm/wddm.h @@ -68,7 +68,7 @@ class Wddm { MOCKABLE_VIRTUAL bool evict(D3DKMT_HANDLE *handleList, uint32_t numOfHandles, uint64_t &sizeToTrim); MOCKABLE_VIRTUAL bool makeResident(D3DKMT_HANDLE *handles, uint32_t count, bool cantTrimFurther, uint64_t *numberOfBytesToTrim); - bool mapGpuVirtualAddress(WddmAllocation *allocation, void *cpuPtr, uint64_t size, bool allocation32bit, bool use64kbPages, bool useHeap1); + bool mapGpuVirtualAddress(WddmAllocation *allocation, void *cpuPtr, bool allocation32bit, bool use64kbPages, bool useHeap1); bool mapGpuVirtualAddress(AllocationStorageData *allocationStorageData, bool allocation32bit, bool use64kbPages); MOCKABLE_VIRTUAL bool createContext(); virtual bool createHwQueue() { return false; } @@ -200,7 +200,7 @@ class Wddm { uintptr_t minAddress; Wddm(); - MOCKABLE_VIRTUAL bool mapGpuVirtualAddressImpl(Gmm *gmm, D3DKMT_HANDLE handle, void *cpuPtr, uint64_t size, D3DGPU_VIRTUAL_ADDRESS &gpuPtr, bool allocation32bit, bool use64kbPages, bool useHeap1); + MOCKABLE_VIRTUAL bool mapGpuVirtualAddressImpl(Gmm *gmm, D3DKMT_HANDLE handle, void *cpuPtr, D3DGPU_VIRTUAL_ADDRESS &gpuPtr, bool allocation32bit, bool use64kbPages, bool useHeap1); MOCKABLE_VIRTUAL bool openAdapter(); bool createDevice(); bool createPagingQueue(); diff --git a/runtime/os_interface/windows/wddm_memory_manager.cpp b/runtime/os_interface/windows/wddm_memory_manager.cpp index 08288c6959..8e11ad47a5 100644 --- a/runtime/os_interface/windows/wddm_memory_manager.cpp +++ b/runtime/os_interface/windows/wddm_memory_manager.cpp @@ -95,7 +95,7 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory64kb(size_t size, s wddmAllocation->setAlignedCpuPtr(cpuPtr); // 64kb map is not needed - auto status = wddm->mapGpuVirtualAddress(wddmAllocation, cpuPtr, sizeAligned, false, false, false); + auto status = wddm->mapGpuVirtualAddress(wddmAllocation, cpuPtr, false, false, false); DEBUG_BREAK_IF(!status); wddmAllocation->setCpuPtrAndGpuAddress(cpuPtr, (uint64_t)wddmAllocation->gpuPtr); @@ -234,7 +234,7 @@ GraphicsAllocation *WddmMemoryManager::createAllocationFromHandle(osHandle handl allocation->is32BitAllocation = true; allocation->gpuBaseAddress = GmmHelper::canonize(allocator32Bit->getBase()); } - status = wddm->mapGpuVirtualAddress(allocation, ptr, size, is32BitAllocation, false, false); + status = wddm->mapGpuVirtualAddress(allocation, ptr, is32BitAllocation, false, false); DEBUG_BREAK_IF(!status); allocation->setGpuAddress(allocation->gpuPtr); @@ -812,10 +812,10 @@ bool WddmMemoryManager::createWddmAllocation(WddmAllocation *allocation, Allocat wddmSuccess = wddm->createAllocation(allocation); } if (wddmSuccess == STATUS_SUCCESS) { - bool mapSuccess = wddm->mapGpuVirtualAddress(allocation, allocation->getAlignedCpuPtr(), allocation->getAlignedSize(), allocation->is32BitAllocation, false, useHeap1); + bool mapSuccess = wddm->mapGpuVirtualAddress(allocation, allocation->getAlignedCpuPtr(), allocation->is32BitAllocation, false, useHeap1); if (!mapSuccess && deferredDeleter) { deferredDeleter->drain(true); - mapSuccess = wddm->mapGpuVirtualAddress(allocation, allocation->getAlignedCpuPtr(), allocation->getAlignedSize(), allocation->is32BitAllocation, false, useHeap1); + mapSuccess = wddm->mapGpuVirtualAddress(allocation, allocation->getAlignedCpuPtr(), allocation->is32BitAllocation, false, useHeap1); } if (!mapSuccess) { wddm->destroyAllocations(&allocation->handle, 1, 0, allocation->resourceHandle); diff --git a/unit_tests/mocks/mock_gmm_resource_info.h b/unit_tests/mocks/mock_gmm_resource_info.h index 071191c1c3..d08d2f523c 100644 --- a/unit_tests/mocks/mock_gmm_resource_info.h +++ b/unit_tests/mocks/mock_gmm_resource_info.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2017, Intel Corporation +* Copyright (c) 2017 - 2018, Intel Corporation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -90,6 +90,7 @@ class MockGmmResourceInfo : public GmmResourceInfo { GMM_RESCREATE_PARAMS mockResourceCreateParams = {}; void overrideReturnedRenderPitch(size_t newPitch) { rowPitch = newPitch; } + void overrideReturnedSize(size_t newSize) { size = newSize; } void setUnifiedAuxTranslationCapable(); diff --git a/unit_tests/mocks/mock_wddm20.cpp b/unit_tests/mocks/mock_wddm20.cpp index d5f9100d37..73f5ff1e60 100644 --- a/unit_tests/mocks/mock_wddm20.cpp +++ b/unit_tests/mocks/mock_wddm20.cpp @@ -48,11 +48,11 @@ bool WddmMock::evict(D3DKMT_HANDLE *handles, uint32_t num, uint64_t &sizeToTrim) return makeNonResidentResult.success = Wddm::evict(handles, num, sizeToTrim); } -bool WddmMock::mapGpuVirtualAddressImpl(Gmm *gmm, D3DKMT_HANDLE handle, void *cpuPtr, uint64_t size, D3DGPU_VIRTUAL_ADDRESS &gpuPtr, bool allocation32Bit, bool use64kbPages, bool useHeap1) { +bool WddmMock::mapGpuVirtualAddressImpl(Gmm *gmm, D3DKMT_HANDLE handle, void *cpuPtr, D3DGPU_VIRTUAL_ADDRESS &gpuPtr, bool allocation32Bit, bool use64kbPages, bool useHeap1) { mapGpuVirtualAddressResult.called++; mapGpuVirtualAddressResult.cpuPtrPassed = cpuPtr; if (callBaseMapGpuVa) { - return mapGpuVirtualAddressResult.success = Wddm::mapGpuVirtualAddressImpl(gmm, handle, cpuPtr, size, gpuPtr, allocation32Bit, use64kbPages, useHeap1); + return mapGpuVirtualAddressResult.success = Wddm::mapGpuVirtualAddressImpl(gmm, handle, cpuPtr, gpuPtr, allocation32Bit, use64kbPages, useHeap1); } else { gpuPtr = reinterpret_cast(cpuPtr); return mapGpuVaStatus; diff --git a/unit_tests/mocks/mock_wddm20.h b/unit_tests/mocks/mock_wddm20.h index c1f668c2a0..ed840ad710 100644 --- a/unit_tests/mocks/mock_wddm20.h +++ b/unit_tests/mocks/mock_wddm20.h @@ -65,7 +65,7 @@ class WddmMock : public Wddm20 { bool makeResident(D3DKMT_HANDLE *handles, uint32_t count, bool cantTrimFurther, uint64_t *numberOfBytesToTrim) override; bool evict(D3DKMT_HANDLE *handles, uint32_t num, uint64_t &sizeToTrim) override; - bool mapGpuVirtualAddressImpl(Gmm *gmm, D3DKMT_HANDLE handle, void *cpuPtr, uint64_t size, D3DGPU_VIRTUAL_ADDRESS &gpuPtr, bool allocation32Bit, bool use64kbPages, bool useHeap1) override; + bool mapGpuVirtualAddressImpl(Gmm *gmm, D3DKMT_HANDLE handle, void *cpuPtr, D3DGPU_VIRTUAL_ADDRESS &gpuPtr, bool allocation32Bit, bool use64kbPages, bool useHeap1) override; bool freeGpuVirtualAddres(D3DGPU_VIRTUAL_ADDRESS &gpuPtr, uint64_t size) override; NTSTATUS createAllocation(WddmAllocation *alloc) override; bool createAllocation64k(WddmAllocation *alloc) override; diff --git a/unit_tests/os_interface/windows/wddm20_tests.cpp b/unit_tests/os_interface/windows/wddm20_tests.cpp index d7259262a3..f09ef20abe 100644 --- a/unit_tests/os_interface/windows/wddm20_tests.cpp +++ b/unit_tests/os_interface/windows/wddm20_tests.cpp @@ -225,7 +225,7 @@ HWTEST_F(Wddm20WithMockGdiDllTests, givenAllocationSmallerUnderlyingThanAlignedS EXPECT_EQ(STATUS_SUCCESS, status); EXPECT_NE(0, allocation.handle); - bool ret = wddm->mapGpuVirtualAddress(&allocation, allocation.getAlignedCpuPtr(), allocation.getAlignedSize(), allocation.is32BitAllocation, false, false); + bool ret = wddm->mapGpuVirtualAddress(&allocation, allocation.getAlignedCpuPtr(), allocation.is32BitAllocation, false, false); EXPECT_TRUE(ret); EXPECT_EQ(alignedPages, getLastCallMapGpuVaArgFcn()->SizeInPages); @@ -237,6 +237,25 @@ HWTEST_F(Wddm20WithMockGdiDllTests, givenAllocationSmallerUnderlyingThanAlignedS delete gmm; } +HWTEST_F(Wddm20WithMockGdiDllTests, givenWddmAllocationWhenMappingGpuVaThenUseGmmSize) { + wddm->init(); + + void *fakePtr = reinterpret_cast(0x123); + WddmAllocation allocation(fakePtr, 100, fakePtr, 200, nullptr); + std::unique_ptr gmm(GmmHelperFunctions::getGmm(allocation.getAlignedCpuPtr(), allocation.getAlignedSize())); + + allocation.gmm = gmm.get(); + auto status = wddm->createAllocation(&allocation); + + auto mockResourceInfo = static_cast(gmm->gmmResourceInfo.get()); + mockResourceInfo->overrideReturnedSize(allocation.getAlignedSize() + (2 * MemoryConstants::pageSize)); + + wddm->mapGpuVirtualAddress(&allocation, allocation.getAlignedCpuPtr(), allocation.is32BitAllocation, false, false); + + uint64_t expectedSizeInPages = static_cast(mockResourceInfo->getSizeAllocation() / MemoryConstants::pageSize); + EXPECT_EQ(expectedSizeInPages, getLastCallMapGpuVaArgFcn()->SizeInPages); +} + HWTEST_F(Wddm20Tests, createAllocation32bit) { wddm->init(); ASSERT_TRUE(wddm->isInitialized()); @@ -259,7 +278,7 @@ HWTEST_F(Wddm20Tests, createAllocation32bit) { EXPECT_EQ(STATUS_SUCCESS, status); EXPECT_TRUE(allocation.handle != 0); - bool ret = wddm->mapGpuVirtualAddress(&allocation, allocation.getAlignedCpuPtr(), allocation.getAlignedSize(), allocation.is32BitAllocation, false, false); + bool ret = wddm->mapGpuVirtualAddress(&allocation, allocation.getAlignedCpuPtr(), allocation.is32BitAllocation, false, false); EXPECT_TRUE(ret); EXPECT_EQ(1u, wddm->mapGpuVirtualAddressResult.called); @@ -282,7 +301,7 @@ HWTEST_F(Wddm20Tests, givenGraphicsAllocationWhenItIsMappedInHeap1ThenItHasGpuAd allocation.handle = ALLOCATION_HANDLE; allocation.gmm = GmmHelperFunctions::getGmm(allocation.getUnderlyingBuffer(), allocation.getUnderlyingBufferSize()); - bool ret = wddm->mapGpuVirtualAddress(&allocation, allocation.getAlignedCpuPtr(), allocation.getAlignedSize(), false, false, true); + bool ret = wddm->mapGpuVirtualAddress(&allocation, allocation.getAlignedCpuPtr(), false, false, true); EXPECT_TRUE(ret); auto cannonizedHeapBase = GmmHelper::canonize(this->wddm->getGfxPartition().Heap32[1].Base); @@ -340,7 +359,7 @@ HWTEST_F(Wddm20Tests, mapAndFreeGpuVa) { EXPECT_EQ(STATUS_SUCCESS, status); EXPECT_TRUE(allocation.handle != 0); - auto error = wddm->mapGpuVirtualAddress(&allocation, allocation.getAlignedCpuPtr(), allocation.getUnderlyingBufferSize(), false, false, false); + auto error = wddm->mapGpuVirtualAddress(&allocation, allocation.getAlignedCpuPtr(), false, false, false); EXPECT_TRUE(error); EXPECT_TRUE(allocation.gpuPtr != 0); @@ -366,7 +385,7 @@ HWTEST_F(Wddm20Tests, givenNullAllocationWhenCreateThenAllocateAndMap) { auto status = wddm->createAllocation(&allocation); EXPECT_EQ(STATUS_SUCCESS, status); - bool ret = wddm->mapGpuVirtualAddress(&allocation, allocation.getAlignedCpuPtr(), allocation.getAlignedSize(), allocation.is32BitAllocation, false, false); + bool ret = wddm->mapGpuVirtualAddress(&allocation, allocation.getAlignedCpuPtr(), allocation.is32BitAllocation, false, false); EXPECT_TRUE(ret); EXPECT_NE(0u, allocation.gpuPtr); @@ -390,7 +409,7 @@ HWTEST_F(Wddm20Tests, makeResidentNonResident) { EXPECT_EQ(STATUS_SUCCESS, status); EXPECT_TRUE(allocation.handle != 0); - auto error = wddm->mapGpuVirtualAddress(&allocation, allocation.getAlignedCpuPtr(), allocation.getUnderlyingBufferSize(), false, false, false); + auto error = wddm->mapGpuVirtualAddress(&allocation, allocation.getAlignedCpuPtr(), false, false, false); EXPECT_TRUE(error); EXPECT_TRUE(allocation.gpuPtr != 0); diff --git a/unit_tests/os_interface/windows/wddm_kmdaf_listener_tests.cpp b/unit_tests/os_interface/windows/wddm_kmdaf_listener_tests.cpp index 8274d409f1..5f4a43c77b 100644 --- a/unit_tests/os_interface/windows/wddm_kmdaf_listener_tests.cpp +++ b/unit_tests/os_interface/windows/wddm_kmdaf_listener_tests.cpp @@ -47,8 +47,8 @@ class WddmWithKmDafMock : public Wddm { return featureTable.get(); } - bool mapGpuVirtualAddressImpl(Gmm *gmm, D3DKMT_HANDLE handle, void *cpuPtr, uint64_t size, D3DGPU_VIRTUAL_ADDRESS &gpuPtr, bool allocation32bit, bool use64kbPages, bool useHeap1) override { - return Wddm::mapGpuVirtualAddressImpl(gmm, handle, cpuPtr, size, gpuPtr, allocation32bit, use64kbPages, useHeap1); + bool mapGpuVirtualAddressImpl(Gmm *gmm, D3DKMT_HANDLE handle, void *cpuPtr, D3DGPU_VIRTUAL_ADDRESS &gpuPtr, bool allocation32bit, bool use64kbPages, bool useHeap1) override { + return Wddm::mapGpuVirtualAddressImpl(gmm, handle, cpuPtr, gpuPtr, allocation32bit, use64kbPages, useHeap1); }; }; @@ -98,7 +98,7 @@ HWTEST_F(WddmKmDafListenerTest, givenWddmWhenMapGpuVirtualAddressIsCalledThenKmD auto gmm = std::unique_ptr(new Gmm(nullptr, 1, false)); allocation.gmm = gmm.get(); - wddmWithKmDafMock->mapGpuVirtualAddressImpl(allocation.gmm, allocation.handle, allocation.getUnderlyingBuffer(), allocation.getUnderlyingBufferSize(), allocation.gpuPtr, false, false, false); + wddmWithKmDafMock->mapGpuVirtualAddressImpl(allocation.gmm, allocation.handle, allocation.getUnderlyingBuffer(), allocation.gpuPtr, false, false, false); EXPECT_EQ(wddmWithKmDafMock->getFeatureTable()->ftrKmdDaf, wddmWithKmDafMock->getKmDafListenerMock().notifyMapGpuVAParametrization.ftrKmdDaf); EXPECT_EQ(wddmWithKmDafMock->getAdapter(), wddmWithKmDafMock->getKmDafListenerMock().notifyMapGpuVAParametrization.hAdapter); diff --git a/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp b/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp index 9bec4c6dd3..254b0ae38f 100644 --- a/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp +++ b/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp @@ -2027,7 +2027,7 @@ HWTEST_F(MockWddmMemoryManagerTest, givenRenderCompressedAllocationWhenMappedGpu EXPECT_CALL(*mockMngr, updateAuxTable(_)).Times(1).WillOnce(Invoke([&](const GMM_DDI_UPDATEAUXTABLE *arg) {givenDdiUpdateAuxTable = *arg; return GMM_SUCCESS; })); - auto result = wddm.mapGpuVirtualAddressImpl(gmm.get(), ALLOCATION_HANDLE, nullptr, 3, gpuVa, false, false, false); + auto result = wddm.mapGpuVirtualAddressImpl(gmm.get(), ALLOCATION_HANDLE, nullptr, gpuVa, false, false, false); ASSERT_TRUE(result); EXPECT_EQ(GmmHelper::canonize(wddm.getGfxPartition().Standard.Base), gpuVa); @@ -2088,7 +2088,7 @@ HWTEST_F(MockWddmMemoryManagerTest, givenNonRenderCompressedAllocationWhenMapped EXPECT_CALL(*mockMngr, updateAuxTable(_)).Times(0); - auto result = wddm.mapGpuVirtualAddressImpl(gmm.get(), ALLOCATION_HANDLE, nullptr, 3, gpuVa, false, false, false); + auto result = wddm.mapGpuVirtualAddressImpl(gmm.get(), ALLOCATION_HANDLE, nullptr, gpuVa, false, false, false); ASSERT_TRUE(result); } @@ -2099,7 +2099,7 @@ HWTEST_F(MockWddmMemoryManagerTest, givenFailingAllocationWhenMappedGpuVaThenRet WddmMock wddm; EXPECT_TRUE(wddm.init()); - auto result = wddm.mapGpuVirtualAddressImpl(gmm.get(), 0, nullptr, 3, gpuVa, false, false, false); + auto result = wddm.mapGpuVirtualAddressImpl(gmm.get(), 0, nullptr, gpuVa, false, false, false); ASSERT_FALSE(result); } @@ -2122,7 +2122,7 @@ HWTEST_F(MockWddmMemoryManagerTest, givenRenderCompressedFlagSetWhenInternalIsUn EXPECT_CALL(*mockMngr, updateAuxTable(_)).Times(0); - auto result = wddm->mapGpuVirtualAddressImpl(myGmm, ALLOCATION_HANDLE, nullptr, 3, gpuVa, false, false, false); + auto result = wddm->mapGpuVirtualAddressImpl(myGmm, ALLOCATION_HANDLE, nullptr, gpuVa, false, false, false); EXPECT_TRUE(result); memoryManager.freeGraphicsMemory(wddmAlloc); }