Skip to content

Commit deaaa90

Browse files
Extract Os Context from Wddm and WddmInterface
Change-Id: I13a52fc466a14f4bd28876d3c47884dc596f2b58 Signed-off-by: Mateusz Jablonski <[email protected]>
1 parent f6f9c0f commit deaaa90

File tree

13 files changed

+228
-147
lines changed

13 files changed

+228
-147
lines changed

runtime/os_interface/windows/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ set(RUNTIME_SRCS_OS_INTERFACE_WINDOWS
4141
${CMAKE_CURRENT_SOURCE_DIR}/gdi_interface.h
4242
${CMAKE_CURRENT_SOURCE_DIR}/kmdaf_listener${KMDAF_FILE_SUFFIX}.cpp
4343
${CMAKE_CURRENT_SOURCE_DIR}/kmdaf_listener.h
44+
${CMAKE_CURRENT_SOURCE_DIR}/os_context_win.cpp
45+
${CMAKE_CURRENT_SOURCE_DIR}/os_context_win.h
4446
${CMAKE_CURRENT_SOURCE_DIR}/os_inc.h
4547
${CMAKE_CURRENT_SOURCE_DIR}/os_interface.cpp
4648
${CMAKE_CURRENT_SOURCE_DIR}/os_interface.h
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2018, Intel Corporation
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included
12+
* in all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18+
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20+
* OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#include "runtime/os_interface/windows/os_context_win.h"
24+
#include "runtime/os_interface/windows/wddm/wddm.h"
25+
#include "runtime/os_interface/windows/wddm/wddm_interface.h"
26+
27+
namespace OCLRT {
28+
OsContextWin::OsContextWin(Wddm &wddm) : wddm(wddm) {
29+
auto wddmInterface = wddm.getWddmInterface();
30+
if (!wddm.createContext(context))
31+
return;
32+
if (wddmInterface->hwQueuesSupported()) {
33+
if (!wddmInterface->createHwQueue(wddm.getPreemptionMode(), *this))
34+
return;
35+
}
36+
initialized = wddmInterface->createMonitoredFence(*this);
37+
};
38+
OsContextWin::~OsContextWin() {
39+
wddm.getWddmInterface()->destroyHwQueue(hwQueueHandle);
40+
wddm.destroyContext(context);
41+
}
42+
43+
void OsContextWin::resetMonitoredFenceParams(D3DKMT_HANDLE &handle, uint64_t *cpuAddress, D3DGPU_VIRTUAL_ADDRESS &gpuAddress) {
44+
monitoredFence.lastSubmittedFence = 0;
45+
monitoredFence.currentFenceValue = 1;
46+
monitoredFence.fenceHandle = handle;
47+
monitoredFence.cpuAddress = cpuAddress;
48+
monitoredFence.gpuAddress = gpuAddress;
49+
}
50+
} // namespace OCLRT
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2018, Intel Corporation
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included
12+
* in all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18+
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20+
* OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#pragma once
24+
#include "runtime/os_interface/windows/windows_wrapper.h"
25+
#include "runtime/os_interface/windows/windows_defs.h"
26+
#include <d3dkmthk.h>
27+
28+
namespace OCLRT {
29+
class Wddm;
30+
31+
class OsContextWin {
32+
public:
33+
OsContextWin() = delete;
34+
OsContextWin(Wddm &wddm);
35+
~OsContextWin();
36+
D3DKMT_HANDLE getContext() const {
37+
return context;
38+
}
39+
D3DKMT_HANDLE getHwQueue() const {
40+
return hwQueueHandle;
41+
}
42+
void setHwQueue(D3DKMT_HANDLE hwQueue) {
43+
hwQueueHandle = hwQueue;
44+
}
45+
bool isInitialized() const {
46+
return initialized;
47+
}
48+
MonitoredFence &getMonitoredFence() { return monitoredFence; }
49+
void resetMonitoredFenceParams(D3DKMT_HANDLE &handle, uint64_t *cpuAddress, D3DGPU_VIRTUAL_ADDRESS &gpuAddress);
50+
51+
protected:
52+
bool initialized = false;
53+
D3DKMT_HANDLE context = 0;
54+
D3DKMT_HANDLE hwQueueHandle = 0;
55+
Wddm &wddm;
56+
MonitoredFence monitoredFence = {};
57+
};
58+
} // namespace OCLRT

runtime/os_interface/windows/wddm/wddm.cpp

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "runtime/gmm_helper/page_table_mngr.h"
3131
#include "runtime/os_interface/windows/wddm/wddm.h"
3232
#include "runtime/os_interface/hw_info_config.h"
33+
#include "runtime/os_interface/windows/os_context_win.h"
3334
#include "runtime/os_interface/windows/wddm_allocation.h"
3435
#include "runtime/os_interface/windows/registry_reader.h"
3536
#include "runtime/helpers/debug_helpers.h"
@@ -54,16 +55,7 @@ Wddm::GetSystemInfoFcn Wddm::getSystemInfo = getGetSystemInfo();
5455
Wddm::VirtualAllocFcn Wddm::virtualAllocFnc = getVirtualAlloc();
5556
Wddm::VirtualFreeFcn Wddm::virtualFreeFnc = getVirtualFree();
5657

57-
Wddm::Wddm() : initialized(false),
58-
adapter(0),
59-
context(0),
60-
device(0),
61-
pagingQueue(0),
62-
pagingQueueSyncObject(0),
63-
pagingFenceAddress(nullptr),
64-
currentPagingFenceValue(0),
65-
hwContextId(0),
66-
trimCallbackHandle(nullptr) {
58+
Wddm::Wddm() {
6759
featureTable.reset(new FeatureTable());
6860
waTable.reset(new WorkaroundTable());
6961
gtSystemInfo.reset(new GT_SYSTEM_INFO);
@@ -74,17 +66,12 @@ Wddm::Wddm() : initialized(false),
7466
registryReader.reset(new RegistryReader("System\\CurrentControlSet\\Control\\GraphicsDrivers\\Scheduler"));
7567
adapterLuid.HighPart = 0;
7668
adapterLuid.LowPart = 0;
77-
maximumApplicationAddress = 0;
78-
node = GPUNODE_3D;
79-
preemptionMode = PreemptionMode::Disabled;
80-
minAddress = 0;
8169
kmDafListener = std::unique_ptr<KmDafListener>(new KmDafListener);
8270
gdi = std::unique_ptr<Gdi>(new Gdi());
8371
}
8472

8573
Wddm::~Wddm() {
8674
resetPageTableManager(nullptr);
87-
destroyContext(context);
8875
destroyPagingQueue();
8976
destroyDevice();
9077
closeAdapter();
@@ -684,7 +671,7 @@ void Wddm::kmDafLock(WddmAllocation *wddmAllocation) {
684671
kmDafListener->notifyLock(featureTable->ftrKmdDaf, adapter, device, wddmAllocation->handle, 0, gdi->escape);
685672
}
686673

687-
bool Wddm::createContext() {
674+
bool Wddm::createContext(D3DKMT_HANDLE &context) {
688675
NTSTATUS status = STATUS_UNSUCCESSFUL;
689676
D3DKMT_CREATECONTEXTVIRTUAL CreateContext = {0};
690677
CREATECONTEXT_PVTDATA PrivateData = {{0}};
@@ -730,15 +717,15 @@ bool Wddm::destroyContext(D3DKMT_HANDLE context) {
730717

731718
bool Wddm::submit(uint64_t commandBuffer, size_t size, void *commandHeader) {
732719
bool status = false;
733-
if (currentPagingFenceValue > *pagingFenceAddress && !waitOnGPU()) {
720+
if (currentPagingFenceValue > *pagingFenceAddress && !waitOnGPU(osContext->getContext())) {
734721
return false;
735722
}
736-
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "currentFenceValue =", monitoredFence.currentFenceValue);
723+
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "currentFenceValue =", osContext->getMonitoredFence().currentFenceValue);
737724

738-
status = wddmInterface->submit(commandBuffer, size, commandHeader);
725+
status = wddmInterface->submit(commandBuffer, size, commandHeader, *osContext);
739726
if (status) {
740-
monitoredFence.lastSubmittedFence = monitoredFence.currentFenceValue;
741-
monitoredFence.currentFenceValue++;
727+
osContext->getMonitoredFence().lastSubmittedFence = osContext->getMonitoredFence().currentFenceValue;
728+
osContext->getMonitoredFence().currentFenceValue++;
742729
}
743730
getDeviceState();
744731
UNRECOVERABLE_IF(!status);
@@ -764,9 +751,9 @@ void Wddm::getDeviceState() {
764751
}
765752

766753
void Wddm::handleCompletion() {
767-
if (monitoredFence.cpuAddress) {
768-
auto *currentTag = monitoredFence.cpuAddress;
769-
while (*currentTag < monitoredFence.currentFenceValue - 1)
754+
if (osContext->getMonitoredFence().cpuAddress) {
755+
auto *currentTag = osContext->getMonitoredFence().cpuAddress;
756+
while (*currentTag < osContext->getMonitoredFence().currentFenceValue - 1)
770757
;
771758
}
772759
}
@@ -775,7 +762,7 @@ unsigned int Wddm::readEnablePreemptionRegKey() {
775762
return static_cast<unsigned int>(registryReader->getSetting("EnablePreemption", 1));
776763
}
777764

778-
bool Wddm::waitOnGPU() {
765+
bool Wddm::waitOnGPU(D3DKMT_HANDLE context) {
779766
D3DKMT_WAITFORSYNCHRONIZATIONOBJECTFROMGPU WaitOnGPU = {0};
780767

781768
WaitOnGPU.hContext = context;
@@ -792,10 +779,10 @@ bool Wddm::waitOnGPU() {
792779
bool Wddm::waitFromCpu(uint64_t lastFenceValue) {
793780
NTSTATUS status = STATUS_SUCCESS;
794781

795-
if (lastFenceValue > *monitoredFence.cpuAddress) {
782+
if (lastFenceValue > *osContext->getMonitoredFence().cpuAddress) {
796783
D3DKMT_WAITFORSYNCHRONIZATIONOBJECTFROMCPU waitFromCpu = {0};
797784
waitFromCpu.ObjectCount = 1;
798-
waitFromCpu.ObjectHandleArray = &monitoredFence.fenceHandle;
785+
waitFromCpu.ObjectHandleArray = &osContext->getMonitoredFence().fenceHandle;
799786
waitFromCpu.FenceValueArray = &lastFenceValue;
800787
waitFromCpu.hDevice = device;
801788
waitFromCpu.hAsyncEvent = NULL;
@@ -901,12 +888,9 @@ void *Wddm::virtualAlloc(void *inPtr, size_t size, unsigned long flags, unsigned
901888
int Wddm::virtualFree(void *ptr, size_t size, unsigned long flags) {
902889
return virtualFreeFnc(ptr, size, flags);
903890
}
891+
MonitoredFence &Wddm::getMonitoredFence() { return osContext->getMonitoredFence(); }
904892

905-
void Wddm::resetMonitoredFenceParams(D3DKMT_HANDLE &handle, uint64_t *cpuAddress, D3DGPU_VIRTUAL_ADDRESS &gpuAddress) {
906-
monitoredFence.lastSubmittedFence = 0;
907-
monitoredFence.currentFenceValue = 1;
908-
monitoredFence.fenceHandle = handle;
909-
monitoredFence.cpuAddress = cpuAddress;
910-
monitoredFence.gpuAddress = gpuAddress;
893+
D3DKMT_HANDLE Wddm::getOsDeviceContext() const {
894+
return osContext->getContext();
911895
}
912896
} // namespace OCLRT

runtime/os_interface/windows/wddm/wddm.h

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Gdi;
4545
class Gmm;
4646
class LinearStream;
4747
class GmmPageTableMngr;
48+
class OsContextWin;
4849
struct FeatureTable;
4950
struct WorkaroundTable;
5051
struct KmDafListener;
@@ -70,7 +71,7 @@ class Wddm {
7071
MOCKABLE_VIRTUAL bool makeResident(D3DKMT_HANDLE *handles, uint32_t count, bool cantTrimFurther, uint64_t *numberOfBytesToTrim);
7172
bool mapGpuVirtualAddress(WddmAllocation *allocation, void *cpuPtr, bool allocation32bit, bool use64kbPages, bool useHeap1);
7273
bool mapGpuVirtualAddress(AllocationStorageData *allocationStorageData, bool allocation32bit, bool use64kbPages);
73-
MOCKABLE_VIRTUAL bool createContext();
74+
MOCKABLE_VIRTUAL bool createContext(D3DKMT_HANDLE &context);
7475
MOCKABLE_VIRTUAL bool freeGpuVirtualAddres(D3DGPU_VIRTUAL_ADDRESS &gpuPtr, uint64_t size);
7576
MOCKABLE_VIRTUAL NTSTATUS createAllocation(WddmAllocation *alloc);
7677
MOCKABLE_VIRTUAL bool createAllocation64k(WddmAllocation *alloc);
@@ -86,8 +87,7 @@ class Wddm {
8687
MOCKABLE_VIRTUAL bool destroyContext(D3DKMT_HANDLE context);
8788
MOCKABLE_VIRTUAL bool queryAdapterInfo();
8889

89-
virtual bool submit(uint64_t commandBuffer, size_t size, void *commandHeader);
90-
MOCKABLE_VIRTUAL bool waitOnGPU();
90+
MOCKABLE_VIRTUAL bool submit(uint64_t commandBuffer, size_t size, void *commandHeader);
9191
MOCKABLE_VIRTUAL bool waitFromCpu(uint64_t lastFenceValue);
9292

9393
NTSTATUS escape(D3DKMT_ESCAPE &escapeCommand);
@@ -121,7 +121,7 @@ class Wddm {
121121
return deviceRegistryPath;
122122
}
123123

124-
MonitoredFence &getMonitoredFence() { return monitoredFence; }
124+
MonitoredFence &getMonitoredFence();
125125

126126
uint64_t getSystemSharedMemory() const;
127127

@@ -159,27 +159,26 @@ class Wddm {
159159
uintptr_t getWddmMinAddress() const {
160160
return this->minAddress;
161161
}
162-
163-
D3DKMT_HANDLE getOsDeviceContext() {
164-
return context;
162+
WddmInterface *getWddmInterface() const {
163+
return wddmInterface.get();
164+
}
165+
PreemptionMode getPreemptionMode() const {
166+
return preemptionMode;
165167
}
168+
D3DKMT_HANDLE getOsDeviceContext() const;
166169

167170
unsigned int readEnablePreemptionRegKey();
168-
void resetMonitoredFenceParams(D3DKMT_HANDLE &handle, uint64_t *cpuAddress, D3DGPU_VIRTUAL_ADDRESS &gpuAddress);
169171

170172
protected:
171-
bool initialized;
173+
bool initialized = false;
172174
std::unique_ptr<Gdi> gdi;
173-
D3DKMT_HANDLE adapter;
174-
D3DKMT_HANDLE context;
175-
D3DKMT_HANDLE device;
176-
D3DKMT_HANDLE pagingQueue;
177-
D3DKMT_HANDLE pagingQueueSyncObject;
178-
179-
uint64_t *pagingFenceAddress;
180-
std::atomic<std::uint64_t> currentPagingFenceValue;
175+
D3DKMT_HANDLE adapter = 0;
176+
D3DKMT_HANDLE device = 0;
177+
D3DKMT_HANDLE pagingQueue = 0;
178+
D3DKMT_HANDLE pagingQueueSyncObject = 0;
181179

182-
MonitoredFence monitoredFence;
180+
uint64_t *pagingFenceAddress = nullptr;
181+
std::atomic<std::uint64_t> currentPagingFenceValue{0};
183182

184183
// Adapter information
185184
std::unique_ptr<PLATFORM> gfxPlatform;
@@ -192,18 +191,19 @@ class Wddm {
192191
bool instrumentationEnabled = false;
193192
std::string deviceRegistryPath;
194193

195-
unsigned long hwContextId;
194+
unsigned long hwContextId = 0;
196195
LUID adapterLuid;
197-
void *trimCallbackHandle;
198-
uintptr_t maximumApplicationAddress;
199-
GPUNODE_ORDINAL node;
200-
PreemptionMode preemptionMode;
196+
void *trimCallbackHandle = nullptr;
197+
uintptr_t maximumApplicationAddress = 0;
198+
GPUNODE_ORDINAL node = GPUNODE_3D;
199+
PreemptionMode preemptionMode = PreemptionMode::Disabled;
201200
std::unique_ptr<GmmMemory> gmmMemory;
202-
uintptr_t minAddress;
201+
uintptr_t minAddress = 0;
203202

204203
Wddm();
205204
MOCKABLE_VIRTUAL bool mapGpuVirtualAddressImpl(Gmm *gmm, D3DKMT_HANDLE handle, void *cpuPtr, D3DGPU_VIRTUAL_ADDRESS &gpuPtr, bool allocation32bit, bool use64kbPages, bool useHeap1);
206205
MOCKABLE_VIRTUAL bool openAdapter();
206+
MOCKABLE_VIRTUAL bool waitOnGPU(D3DKMT_HANDLE context);
207207
bool createDevice();
208208
bool createPagingQueue();
209209
bool destroyPagingQueue();
@@ -221,5 +221,6 @@ class Wddm {
221221

222222
std::unique_ptr<KmDafListener> kmDafListener;
223223
std::unique_ptr<WddmInterface> wddmInterface;
224+
std::unique_ptr<OsContextWin> osContext;
224225
};
225226
} // namespace OCLRT

runtime/os_interface/windows/wddm/wddm.inl

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "runtime/os_interface/windows/gdi_interface.h"
2424
#include "runtime/os_interface/windows/wddm/wddm.h"
25+
#include "runtime/os_interface/windows/os_context_win.h"
2526

2627
namespace OCLRT {
2728

@@ -67,16 +68,8 @@ bool Wddm::init() {
6768
if (!configureDeviceAddressSpace<GfxFamily>()) {
6869
return false;
6970
}
70-
if (!createContext()) {
71-
return false;
72-
}
73-
if (wddmInterface->hwQueuesSupported() && !wddmInterface->createHwQueue(preemptionMode)) {
74-
return false;
75-
}
76-
if (!wddmInterface->createMonitoredFence()) {
77-
return false;
78-
}
79-
initialized = true;
71+
osContext = std::make_unique<OsContextWin>(*this);
72+
initialized = osContext->isInitialized();
8073
}
8174
return initialized;
8275
}

0 commit comments

Comments
 (0)