-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathplatform.cpp
1164 lines (992 loc) · 41.5 KB
/
platform.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
***********************************************************************************************************************
*
* Copyright (c) 2014-2023 Advanced Micro Devices, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
**********************************************************************************************************************/
#include "core/device.h"
#include "core/platform.h"
#include "core/settingsLoader.h"
#include "core/os/nullDevice/ndDevice.h"
#include "core/os/nullDevice/ndPlatform.h"
#include "palAssert.h"
#if PAL_ENABLE_LOGGING
#include "palDbgLogger.h"
#include "palDbgLogMgr.h"
#endif
#include "palDbgPrint.h"
#include "palSysUtil.h"
#include "palSysMemory.h"
#include "core/layers/decorators.h"
#if PAL_AMDGPU_BUILD
#include "core/os/amdgpu/amdgpuHeaders.h"
#else
#include "core/os/wddm/wddmHeaders.h"
#endif
#if PAL_BUILD_RDF
#include "palTraceSession.h"
#include "gpuUtil/asicInfoTraceSource.h"
#include "gpuUtil/apiInfoTraceSource.h"
#include "gpuUtil/clockCalibTraceSource.h"
#include "gpuUtil/uberTraceService.h"
#include "gpuUtil/frameTraceController.h"
#endif
// Dev Driver includes
#include "devDriverUtil.h"
#include "devDriverServer.h"
#include "protocols/driverControlServer.h"
#include "protocols/rgpServer.h"
#include "protocols/ddInfoService.h"
#include "settingsService.h"
#include "driverUtilsService.h"
using namespace Util;
namespace Pal
{
static_assert(static_cast<uint32>(LogLevel::Debug) == static_cast<uint32>(DevDriver::LogLevel::Debug),
"DevDriver::LogLevel enum mismatch!");
static_assert(static_cast<uint32>(LogLevel::Verbose) == static_cast<uint32>(DevDriver::LogLevel::Verbose),
"DevDriver::LogLevel enum mismatch!");
static_assert(static_cast<uint32>(LogLevel::Info) == static_cast<uint32>(DevDriver::LogLevel::Info),
"DevDriver::LogLevel enum mismatch!");
static_assert(static_cast<uint32>(LogLevel::Alert) == static_cast<uint32>(DevDriver::LogLevel::Alert),
"DevDriver::LogLevel enum mismatch!");
static_assert(static_cast<uint32>(LogLevel::Error) == static_cast<uint32>(DevDriver::LogLevel::Error),
"DevDriver::LogLevel enum mismatch!");
static_assert(static_cast<uint32>(LogLevel::Always) == static_cast<uint32>(DevDriver::LogLevel::Always),
"DevDriver::LogLevel enum mismatch!");
#if PAL_ENABLE_PRINTS_ASSERTS
// =====================================================================================================================
// Callback function used to route debug prints into the logging protocol.
void PAL_STDCALL DbgPrintCb(
void* pUserdata,
Util::DbgPrintCategory category,
const char* pText)
{
IPlatform* pPlatform = static_cast<IPlatform*>(pUserdata);
// Convert the debug print category into a log level.
constexpr LogLevel LogLevelLookup[DbgPrintCatCount] =
{
LogLevel::Info,
LogLevel::Alert,
LogLevel::Error,
LogLevel::Info
};
pPlatform->LogMessage(LogLevelLookup[category], LogCategoryMaskInternal, "%s", pText);
}
#endif
// =====================================================================================================================
Platform::Platform(
const PlatformCreateInfo& createInfo,
const AllocCallbacks& allocCb)
:
Pal::IPlatform(allocCb),
m_deviceCount(0),
m_clientApiId(createInfo.clientApiId),
m_clientApiMajorVer(createInfo.apiMajorVer),
m_clientApiMinorVer(createInfo.apiMinorVer),
m_pDevDriverServer(nullptr),
m_pSettingsService(nullptr),
m_pDriverUtilsService(nullptr),
m_pEventServer(nullptr),
m_settingsLoader(this),
m_pRgpServer(nullptr),
#if PAL_BUILD_RDF
m_pTraceSession(nullptr),
m_pFrameTraceController(nullptr),
m_pAsicInfoTraceSource(nullptr),
m_pApiInfoTraceSource(nullptr),
m_pClockCalibTraceSource(nullptr),
m_pUberTraceService(nullptr),
#endif
m_rpcServer(DD_API_INVALID_HANDLE),
m_pfnDeveloperCb(DefaultDeveloperCb),
m_pClientPrivateData(nullptr),
m_svmRangeStart(0),
m_maxSvmSize(createInfo.maxSvmSize),
m_logCb(),
m_gpuMemoryEventProvider(this),
m_crashAnalysisEventProvider(this),
m_enabledCallbackTypesMask(Developer::DefaultEnabledCallbackTypes),
m_subAllocTrackingEnabled(false)
{
memset(&m_pDevice[0], 0, sizeof(m_pDevice));
memset(&m_properties, 0, sizeof(m_properties));
m_flags.u32All = 0;
m_flags.disableGpuTimeout = createInfo.flags.disableGpuTimeout;
m_flags.force32BitVaSpace = createInfo.flags.force32BitVaSpace;
m_flags.createNullDevice = createInfo.flags.createNullDevice;
m_flags.enableSvmMode = createInfo.flags.enableSvmMode;
m_flags.requestShadowDescVaRange = createInfo.flags.requestShadowDescriptorVaRange;
m_flags.disableInternalResidencyOpts = createInfo.flags.disableInternalResidencyOpts;
m_flags.supportRgpTraces = createInfo.flags.supportRgpTraces;
m_flags.dontOpenPrimaryNode = createInfo.flags.dontOpenPrimaryNode;
#if PAL_CLIENT_INTERFACE_MAJOR_VERSION >= 765
m_flags.disableDevDriver = createInfo.flags.disableDevDriver;
#endif
if (createInfo.pLogInfo != nullptr)
{
m_logCb = *createInfo.pLogInfo;
}
Util::Strncpy(&m_settingsPath[0], createInfo.pSettingsPath, MaxSettingsPathLength);
}
// =====================================================================================================================
Platform::~Platform()
{
DestroyDevDriver();
#if PAL_BUILD_RDF
DestroyDefaultTraceSources();
DestroyTraceControllers();
DestroyTraceSession();
#endif
#if PAL_ENABLE_PRINTS_ASSERTS
// Unhook the debug print callback to keep assert/alert function (majorly for client driver) after platform get
// destroyed. Otherwise random crash can be triggered when calling g_dbgPrintCallback with a dangling pointer.
Util::DbgPrintCallback dbgPrintCallback = {};
Util::SetDbgPrintCallback(dbgPrintCallback);
#endif
}
// =====================================================================================================================
// Creates and initializes the Platform singleton instance. This may result in additional DLL's being loaded (for
// obtaining pointers to OS thunks on Windows, etc.) so it is very unsafe to call this from within a client driver's
// DllMain() function on Windows.
//
// This function is not re-entrant!
Result Platform::Create(
const PlatformCreateInfo& createInfo,
const AllocCallbacks& allocCb,
void* pPlacementAddr,
Platform** ppPlatform)
{
Result result = Result::ErrorInitializationFailed;
Platform* pPlatform = nullptr;
// Create either a "null" device (good for off-line shader compilation and not much else) or a real device.
if (createInfo.flags.createNullDevice)
{
#if PAL_BUILD_NULL_DEVICE
pPlatform = Pal::NullDevice::Platform::CreateInstance(createInfo, allocCb, pPlacementAddr);
#else
// Why are you trying to do this?
PAL_ASSERT_ALWAYS();
#endif
}
else
{
pPlatform = CreateInstance(createInfo, allocCb, pPlacementAddr);
}
if (pPlatform != nullptr)
{
result = pPlatform->Init();
}
if (result == Result::Success)
{
(*ppPlatform) = pPlatform;
}
else if (pPlatform != nullptr)
{
pPlatform->Destroy();
}
return result;
}
// =====================================================================================================================
// Returns a count and list of devices attached to the system. If this function is called more than once, then it will
// also cleanup any device objects enumerated on the previous call, a sequence expected when the client is returned an
// ErrorDeviceLost error from any PAL function. See the public interface documentation for more detail.
Result Platform::EnumerateDevices(
uint32* pDeviceCount,
IDevice* pDevices[MaxDevices])
{
PAL_ASSERT((pDeviceCount != nullptr) && (pDevices != nullptr));
Result result = ReEnumerateDevices();
if (result == Result::Success)
{
*pDeviceCount = m_deviceCount;
for (uint32 gpu = 0; gpu < *pDeviceCount; ++gpu)
{
pDevices[gpu] = m_pDevice[gpu];
}
// We need to internally query the screen topology so that each Device will know whether or not screen(s) are
// available. This affects which presentation techniques are selected by any Queues created for the Device.
uint32 dummyScreenCount = 0;
result = ReQueryScreens(&dummyScreenCount, nullptr, nullptr);
}
return result;
}
// =====================================================================================================================
// Retrieves the list of available screens. This function queries a set of IScreen objects corresponding to the screens
// attached to the system. The caller owns any returned IScreens.
Result Platform::GetScreens(
uint32* pScreenCount,
void* pStorage[MaxScreens],
IScreen* pScreens[MaxScreens])
{
PAL_ASSERT(pScreenCount != nullptr);
Result result = Result::ErrorUnavailable;
if (m_deviceCount >= 1)
{
result = ReQueryScreens(pScreenCount, pStorage, pScreens);
}
return result;
}
// =====================================================================================================================
// Queries the kernel-mode driver to determine if there is a platform-wide profile for a specific application that the
// client would like to honor. Returned in raw format.
Result Platform::QueryRawApplicationProfile(
const wchar_t* pFilename,
const wchar_t* pPathname,
ApplicationProfileClient client,
const char** pOut)
{
PAL_ASSERT((pFilename != nullptr) && (pOut != nullptr));
Result result = Result::ErrorUnavailable;
if (m_deviceCount >= 1)
{
// NOTE: These application profiles are meant to be interpreted at system-wide scope. We'll only query the
// first discovered physical GPU under the assumption that all GPU's would return the same profile (or none
// at all, as the case may be).
result = m_pDevice[0]->QueryRawApplicationProfile(pFilename, pPathname, client, pOut);
}
return result;
}
// =====================================================================================================================
// Queries the kernel-mode driver to determine if there is a platform-wide spp profile for a specific application that
// the client would like to honor.
Result Platform::EnableSppProfile(
const wchar_t* pFilename,
const wchar_t* pPathname)
{
PAL_ASSERT(pFilename != nullptr);
Result result = Result::ErrorUnavailable;
if (m_deviceCount >= 1)
{
// NOTE: The spp profiles are meant to be interpreted at system-wide scope. We'll only query the
// first discovered physical GPU under the assumption that all GPU's would return the same profile (or none
// at all, as the case may be).
result = m_pDevice[0]->EnableSppProfile(pFilename, pPathname);
}
return result;
}
// =====================================================================================================================
Result Platform::GetProperties(
PlatformProperties* pProperties)
{
Result result = Result::ErrorInvalidPointer;
if (pProperties != nullptr)
{
// Copy our pre-baked properties struct.
memcpy(pProperties, &m_properties, sizeof(m_properties));
result = Result::Success;
}
return result;
}
// =====================================================================================================================
// Helper method which destroys all previously enumerated devices.
void Platform::TearDownDevices()
{
for (uint32 gpu = 0; gpu < m_deviceCount; ++gpu)
{
const Result result = m_pDevice[gpu]->Cleanup();
PAL_ASSERT(result == Result::Success);
m_pDevice[gpu]->~Device();
PAL_SAFE_FREE(m_pDevice[gpu], this);
}
m_deviceCount = 0;
}
// =====================================================================================================================
// Initializes the platform singleton's connection to the host operating system and kernel-mode driver.
//
// This function is not re-entrant!
Result Platform::Init()
{
Result result = IPlatform::Init();
#if PAL_BUILD_RDF
// Safer to initialize the TraceSession before EarlyInitDevDriver(), since the session's JSON-based config may be
// updated during that method
if (result == Result::Success)
{
result = InitTraceSession();
}
#endif
// Perform early initialization of the developer driver after the platform is available.
if (result == Result::Success)
{
result = EarlyInitDevDriver();
}
#if PAL_ENABLE_PRINTS_ASSERTS
// Set the debug print callback to make debug prints visible over the logging protocol.
Util::DbgPrintCallback dbgPrintCallback = {};
dbgPrintCallback.pCallbackFunc = &DbgPrintCb;
dbgPrintCallback.pUserdata = this;
Util::SetDbgPrintCallback(dbgPrintCallback);
#endif
if (result == Result::Success)
{
result = ConnectToOsInterface();
}
if (result == Result::Success)
{
result = ReEnumerateDevices();
}
// Perform late initialization of the developer driver after devices have been enumerated.
if (result == Result::Success)
{
LateInitDevDriver();
}
if (result == Result::Success)
{
result = InitProperties();
}
#if PAL_BUILD_RDF
if (result == Result::Success)
{
result = InitTraceControllers();
}
if (result == Result::Success)
{
result = RegisterTraceControllers();
}
if (result == Result::Success)
{
result = InitDefaultTraceSources();
}
if (result == Result::Success)
{
result = RegisterDefaultTraceSources();
}
#endif
return result;
}
// =====================================================================================================================
// Optionally overrides the GPU ID for a single device. This can be initiated through the panel settings for some build
// configurations. This MUST BE called after EarlyInitDevDriver() !!
bool Platform::OverrideGpuId(
GpuId* pGpuId) // in,out: GPU ID information to potentially override.
{
bool overridden = false;
const PalPlatformSettings& settings = PlatformSettings();
#if PAL_BUILD_NULL_DEVICE
if (strcmp(settings.spoofNullGpuIfh, "") != 0)
{
NullDevice::NullIdLookup foundGpu = NullDevice::Device::GetDeviceByName(settings.spoofNullGpuIfh);
if (foundGpu.nullId != NullGpuId::Max)
{
pGpuId->gfxEngineId = foundGpu.gfxEngineId;
pGpuId->familyId = foundGpu.familyId;
pGpuId->revisionId = foundGpu.revisionId;
pGpuId->eRevId = foundGpu.eRevId;
pGpuId->deviceId = foundGpu.deviceId;
overridden = true;
}
else
{
PAL_ALERT_ALWAYS_MSG("NullGpu name '%s' not found!", settings.spoofNullGpuIfh);
}
}
#endif
if (overridden)
{
m_flags.gpuIsSpoofed = 1;
}
return overridden;
}
// =====================================================================================================================
// Initializes a connection with the developer driver message bus if it's currently enabled on the system.
// This function should be called before device enumeration.
Result Platform::EarlyInitDevDriver()
{
DevDriver::HostInfo hostInfo = DevDriver::kDefaultNamedPipe;
bool isConnectionAvailable = (m_flags.disableDevDriver == false);
if (isConnectionAvailable)
{
isConnectionAvailable = DevDriver::DevDriverServer::IsConnectionAvailable(hostInfo);
}
DevDriver::Result devDriverResult = DevDriver::Result::Success;
if (isConnectionAvailable)
{
const char*const pClientStr = GetClientApiStr();
// Configure the developer driver server for driver usage
DevDriver::ServerCreateInfo createInfo = {};
createInfo.connectionInfo = hostInfo;
createInfo.componentType = DevDriver::Component::Driver;
createInfo.createUpdateThread = true;
// Set up developer driver memory allocation callbacks
DevDriver::AllocCb allocCb = {};
allocCb.pUserdata = this;
allocCb.pfnAlloc = &DevDriverAlloc;
allocCb.pfnFree = &DevDriverFree;
// Copy the client string into the description field
Util::Strncpy(createInfo.clientDescription, pClientStr, sizeof(createInfo.clientDescription));
// Enable all supported protocols
createInfo.servers.settings = true;
createInfo.servers.driverControl = true;
createInfo.servers.rgp = true;
createInfo.servers.event = true;
m_pDevDriverServer = PAL_NEW(DevDriver::DevDriverServer, this, AllocInternal) (allocCb, createInfo);
if (m_pDevDriverServer != nullptr)
{
devDriverResult = m_pDevDriverServer->Initialize();
if (devDriverResult == DevDriver::Result::Success)
{
// We successfully initialized the message bus. Check if developer mode is enabled by attempting
// to locate a tool on the bus that has the developer mode enabled status flag set.
DevDriver::IMsgChannel* pMsgChannel = m_pDevDriverServer->GetMessageChannel();
DevDriver::ClientId clientId = DevDriver::kBroadcastClientId;
DevDriver::ClientMetadata filter = {};
filter.clientType = DevDriver::Component::Tool;
filter.status =
static_cast<DevDriver::StatusFlags>(DevDriver::ClientStatusFlags::DeveloperModeEnabled);
devDriverResult = pMsgChannel->FindFirstClient(filter,
&clientId,
DevDriver::kFindClientTimeout,
&filter);
}
else
{
// Trigger an assert if we fail to initialize the developer driver server.
PAL_ASSERT_ALWAYS();
}
// Free the memory for the developer driver server object if we fail to initialize it completely.
if (devDriverResult != DevDriver::Result::Success)
{
PAL_SAFE_DELETE(m_pDevDriverServer, this);
}
else // Initialize the event server and our services if we have a valid DevDriver server.
{
m_pEventServer = m_pDevDriverServer->GetEventServer();
#if PAL_ENABLE_RPC_SETTINGS
DevDriver::AllocCb allocCb = {};
allocCb.pUserdata = this;
allocCb.pfnAlloc = &DevDriverAlloc;
allocCb.pfnFree = &DevDriverFree;
m_pSettingsService = PAL_NEW(SettingsRpcService::SettingsService, this, AllocInternal)(allocCb);
PAL_ASSERT(m_pSettingsService != nullptr);
#endif
m_pDriverUtilsService = PAL_NEW(DriverUtilsService::DriverUtilsService, this, AllocInternal)(this);
}
}
else
{
// Trigger an assert if we're unable to create the developer driver server due to memory allocation failure.
PAL_ASSERT_ALWAYS();
}
}
// Initialize Platform settings
Result result = m_settingsLoader.Init();
if ((result == Result::Success) && (m_pDevDriverServer != nullptr))
{
// Initialize the event providers if we have a valid connection
result = m_gpuMemoryEventProvider.Init();
if (result == Result::Success)
{
result = m_crashAnalysisEventProvider.Init();
}
if (result == Result::Success)
{
#if GPUOPEN_CLIENT_INTERFACE_MAJOR_VERSION < GPUOPEN_DRIVER_CONTROL_CLEANUP_VERSION
m_pDevDriverServer->StartDeviceInit();
#else
DevDriver::DriverControlProtocol::DriverControlServer* pDriverControlServer =
m_pDevDriverServer->GetDriverControlServer();
PAL_ASSERT(pDriverControlServer != nullptr);
#if PAL_BUILD_RDF
CreateUberTraceService();
#endif
RegisterRpcServices();
pDriverControlServer->StartEarlyDeviceInit();
#endif
if (pDriverControlServer->IsDriverIgnored() == false)
{
// Cache the pointer for the RGP server after successful initialization.
m_pRgpServer = m_pDevDriverServer->GetRGPServer();
}
else
{
// The tools have indicated that they do not wish to communicate with this driver
// so we can safely destroy all of the previously initialized DevDriver infrastructure.
m_gpuMemoryEventProvider.Destroy();
m_crashAnalysisEventProvider.Destroy();
#if PAL_BUILD_RDF
DestroyRpcServices();
#endif
PAL_SAFE_DELETE(m_pDevDriverServer, this);
}
}
}
return result;
}
// =====================================================================================================================
// Finishes any initialization of the developer driver that requires the devices to be initialized first.
// This function should be called after device enumeration.
void Platform::LateInitDevDriver()
{
// Late init only needs to be performed if we actually set up the developer driver object earlier.
if (m_pDevDriverServer != nullptr)
{
DevDriver::DriverControlProtocol::DriverControlServer* pDriverControlServer =
m_pDevDriverServer->GetDriverControlServer();
// If the developer driver server is initialized successfully and we requested a specific protocol
// during initialization, the associated object should always be valid.
PAL_ASSERT(pDriverControlServer != nullptr);
// Set up the callbacks for changing the device clock.
#if GPUOPEN_CLIENT_INTERFACE_MAJOR_VERSION < GPUOPEN_DRIVER_CONTROL_QUERY_CLOCKS_BY_MODE_VERSION
DevDriver::DriverControlProtocol::DeviceClockCallbackInfo deviceClockCallbackInfo = {};
deviceClockCallbackInfo.queryClockCallback = QueryClockCallback;
deviceClockCallbackInfo.queryMaxClockCallback = QueryMaxClockCallback;
deviceClockCallbackInfo.setCallback = SetClockModeCallback;
deviceClockCallbackInfo.pUserdata = this;
#else
DevDriver::DriverControlProtocol::DeviceClockCallbackInfo deviceClockCallbackInfo = {};
deviceClockCallbackInfo.queryClockCallback = QueryClockCallback;
deviceClockCallbackInfo.setCallback = SetClockModeCallback;
deviceClockCallbackInfo.pUserdata = this;
#endif
// This is a callback that can be used for any pal information as we add more support for more
// information. Currently it is only used for perf experiment information.
DevDriver::InfoURIService::InfoService::InfoSource palInfoSource = {};
palInfoSource.name = "pal";
palInfoSource.version = 1;
palInfoSource.pUserdata = this;
palInfoSource.pfnWriteCallback = &PalCallback;
m_pDevDriverServer->GetInfoService()->RegisterInfoSource(palInfoSource);
pDriverControlServer->SetNumGpus(m_deviceCount);
// Set up the device clock callbacks.
pDriverControlServer->SetDeviceClockCallback(deviceClockCallbackInfo);
}
// Now that we have some valid devices we can look for settings overrides in the registry/settings file.
// Note, we don't really care if this is the device that will actually be used for rendering, we just
// need a device object for the OS specific ReadSetting function.
if (m_deviceCount >= 1)
{
m_settingsLoader.ReadSettings(m_pDevice[0]);
}
// And then before finishing init we have an opportunity to override the settings default values based on
// runtime info
m_settingsLoader.OverrideDefaults();
m_settingsLoader.ValidateSettings(); // Also, validate them.
#if PAL_ENABLE_LOGGING
// Configure debug log manager as soon as settings are overridden.
g_dbgLogMgr.SetLoggingEnabled(PlatformSettings().dbgLogEnabled);
#endif
// Late init only needs to be performed if we actually set up the developer driver object earlier.
#if GPUOPEN_CLIENT_INTERFACE_MAJOR_VERSION >= GPUOPEN_DRIVER_CONTROL_CLEANUP_VERSION
if (m_pDevDriverServer != nullptr)
{
if (m_deviceCount >= 1)
{
Pal::DeviceProperties deviceProperties = {};
m_pDevice[0]->GetProperties(&deviceProperties);
// If both the client and the hardware support rgp traces, attempt to enable them on the rgp server.
if ((m_flags.supportRgpTraces) && (deviceProperties.gfxipProperties.flags.supportRgpTraces))
{
// Enable the trace functionality on the RGP Server.
m_pDevDriverServer->GetRGPServer()->EnableTraces();
}
}
DevDriver::DriverControlProtocol::DriverControlServer* pDriverControlServer =
m_pDevDriverServer->GetDriverControlServer();
PAL_ASSERT(pDriverControlServer != nullptr);
}
#endif
}
// =====================================================================================================================
// Destroys the connection to the developer driver message bus if it was previously initialized.
void Platform::DestroyDevDriver()
{
if (m_pDevDriverServer != nullptr)
{
DestroyRpcServices();
m_gpuMemoryEventProvider.Destroy();
m_crashAnalysisEventProvider.Destroy();
// Null out cached pointers
m_pRgpServer = nullptr;
m_pEventServer = nullptr;
m_pDevDriverServer->Destroy();
PAL_SAFE_DELETE(m_pDevDriverServer, this);
}
}
#if PAL_BUILD_RDF
// =====================================================================================================================
// Creates and initializes a TraceSession that's centrally owned and managed by PAL
Result Platform::InitTraceSession()
{
Result result = Result::ErrorOutOfMemory;
m_pTraceSession = PAL_NEW(GpuUtil::TraceSession, this, AllocInternal) (this);
if (m_pTraceSession != nullptr)
{
result = m_pTraceSession->Init();
}
return result;
}
// =====================================================================================================================
// Destroys the PAL-owned TraceSession
void Platform::DestroyTraceSession()
{
if (m_pTraceSession != nullptr)
{
PAL_SAFE_DELETE(m_pTraceSession, this);
}
}
// =====================================================================================================================
Result Platform::InitTraceControllers()
{
Result result = Result::Success;
m_pFrameTraceController = PAL_NEW(GpuUtil::FrameTraceController, this, AllocInternal) (this);
if (m_pFrameTraceController == nullptr)
{
result = Result::ErrorOutOfMemory;
}
return result;
}
// =====================================================================================================================
Result Platform::RegisterTraceControllers()
{
Result result = m_pTraceSession->RegisterController(m_pFrameTraceController);
return result;
}
// =====================================================================================================================
void Platform::UpdateFrameTraceController(
CmdBuffer* pCmdBuffer)
{
m_pFrameTraceController->UpdateFrame(pCmdBuffer);
}
// =====================================================================================================================
void Platform::DestroyTraceControllers()
{
if (m_pFrameTraceController != nullptr)
{
PAL_SAFE_DELETE(m_pFrameTraceController, this);
}
}
// =====================================================================================================================
Result Platform::InitDefaultTraceSources()
{
Result result = Result::Success;
m_pAsicInfoTraceSource = PAL_NEW(GpuUtil::AsicInfoTraceSource, this, AllocInternal) (this);
m_pApiInfoTraceSource = PAL_NEW(GpuUtil::ApiInfoTraceSource, this, AllocInternal) (this);
m_pClockCalibTraceSource = PAL_NEW(GpuUtil::ClockCalibrationTraceSource, this, AllocInternal) (this);
if ((m_pAsicInfoTraceSource == nullptr) ||
(m_pApiInfoTraceSource == nullptr) ||
(m_pClockCalibTraceSource == nullptr))
{
result = Result::ErrorOutOfMemory;
}
return result;
}
// =====================================================================================================================
// Register the default trace sources(eg. GpuInfo) on start-up
Result Platform::RegisterDefaultTraceSources()
{
Result result = m_pTraceSession->RegisterSource(m_pAsicInfoTraceSource);
if (Util::IsErrorResult(result) == false)
{
result = m_pTraceSession->RegisterSource(m_pApiInfoTraceSource);
}
if (Util::IsErrorResult(result) == false)
{
result = m_pTraceSession->RegisterSource(m_pClockCalibTraceSource);
}
return result;
}
// =====================================================================================================================
// Destroy the default trace sources
void Platform::DestroyDefaultTraceSources()
{
if (m_pAsicInfoTraceSource != nullptr)
{
PAL_SAFE_DELETE(m_pAsicInfoTraceSource, this);
}
if (m_pApiInfoTraceSource != nullptr)
{
PAL_SAFE_DELETE(m_pApiInfoTraceSource, this);
}
if (m_pClockCalibTraceSource != nullptr)
{
PAL_SAFE_DELETE(m_pClockCalibTraceSource, this);
}
}
// =====================================================================================================================
// Creates a UberTraceService that forwards network requests into TraceSession
Result Platform::CreateUberTraceService()
{
Result result = Result::Success;
m_pUberTraceService = PAL_NEW(GpuUtil::UberTraceService, this, AllocInternal) (this);
if (m_pUberTraceService == nullptr)
{
result = Result::ErrorOutOfMemory;
}
return result;
}
#endif
// =====================================================================================================================
void Platform::RegisterRpcServices()
{
DD_RESULT devDriverResult = DD_RESULT_SUCCESS;
DevDriver::IMsgChannel* pMsgChannel = m_pDevDriverServer->GetMessageChannel();
DDRpcServerCreateInfo rpcServerInfo = {};
rpcServerInfo.hConnection = reinterpret_cast<DDNetConnection>(pMsgChannel);
// Create the rpc server with above parameters
devDriverResult = ddRpcServerCreate(&rpcServerInfo, &m_rpcServer);
if (devDriverResult == DD_RESULT_SUCCESS)
{
#if PAL_BUILD_RDF
if (m_pUberTraceService != nullptr)
{
devDriverResult = UberTrace::RegisterService(m_rpcServer, m_pUberTraceService);
PAL_ASSERT(devDriverResult == DD_RESULT_SUCCESS);
}
#endif
if (m_pSettingsService != nullptr)
{
devDriverResult = SettingsRpc::RegisterService(m_rpcServer, m_pSettingsService);
PAL_ASSERT(devDriverResult == DD_RESULT_SUCCESS);
}
if (m_pDriverUtilsService != nullptr)
{
devDriverResult = DriverUtils::RegisterService(m_rpcServer, m_pDriverUtilsService);
PAL_ASSERT(devDriverResult == DD_RESULT_SUCCESS);
}
pMsgChannel->RegisterProtocolServer(m_pDevDriverServer->GetDriverControlServer());
}
}
// =====================================================================================================================
void Platform::DestroyRpcServices()
{
#if PAL_BUILD_RDF
if (m_pUberTraceService != nullptr)
{
PAL_SAFE_DELETE(m_pUberTraceService, this);
}
#endif
if (m_pSettingsService != nullptr)
{
PAL_SAFE_DELETE(m_pSettingsService, this);
}
if (m_pDriverUtilsService != nullptr)
{
PAL_SAFE_DELETE(m_pDriverUtilsService, this);
}
if (m_rpcServer != DD_API_INVALID_HANDLE)
{
ddRpcServerDestroy(m_rpcServer);
m_rpcServer = DD_API_INVALID_HANDLE;
}
}
// =====================================================================================================================
// Forwards event logging calls to the event provider.
void Platform::LogEvent(
PalEvent eventId,
const void* pEventData,
uint32 eventDataSize)
{
static_assert(static_cast<uint32>(PalEvent::Count) == 17, "Write support for new event!");
PAL_ASSERT(pEventData != nullptr);
switch(eventId)
{
case PalEvent::CreateGpuMemory:
case PalEvent::DestroyGpuMemory:
case PalEvent::GpuMemoryCpuMap:
case PalEvent::GpuMemoryCpuUnmap:
case PalEvent::GpuMemoryAddReference:
case PalEvent::GpuMemoryRemoveReference:
// These functions are not currently supported/expected through the PAL interface
PAL_ASSERT_ALWAYS();
break;
case PalEvent::GpuMemoryResourceBind:
PAL_ASSERT(eventDataSize == sizeof(GpuMemoryResourceBindEventData));
m_gpuMemoryEventProvider.LogGpuMemoryResourceBindEvent(*(static_cast<const GpuMemoryResourceBindEventData*>(pEventData)));
break;
case PalEvent::GpuMemoryResourceCreate:
PAL_ASSERT(eventDataSize == sizeof(ResourceCreateEventData));
m_gpuMemoryEventProvider.LogGpuMemoryResourceCreateEvent(*(static_cast<const ResourceCreateEventData*>(pEventData)));
break;
case PalEvent::GpuMemoryResourceDestroy:
PAL_ASSERT(eventDataSize == sizeof(ResourceDestroyEventData));
m_gpuMemoryEventProvider.LogGpuMemoryResourceDestroyEvent(*(static_cast<const ResourceDestroyEventData*>(pEventData)));
break;
case PalEvent::GpuMemoryMisc:
PAL_ASSERT(eventDataSize == sizeof(MiscEventData));
m_gpuMemoryEventProvider.LogGpuMemoryMiscEvent(*(static_cast<const MiscEventData*>(pEventData)));
break;
case PalEvent::GpuMemorySnapshot:
PAL_ASSERT(eventDataSize == sizeof(GpuMemorySnapshotEventData));
m_gpuMemoryEventProvider.LogGpuMemorySnapshotEvent(*(static_cast<const GpuMemorySnapshotEventData*>(pEventData)));
break;
case PalEvent::DebugName:
PAL_ASSERT(eventDataSize == sizeof(DebugNameEventData));
m_gpuMemoryEventProvider.LogDebugNameEvent(*(static_cast<const DebugNameEventData*>(pEventData)));
break;
case PalEvent::ResourceCorrelation:
PAL_ASSERT(eventDataSize == sizeof(ResourceCorrelationEventData));
m_gpuMemoryEventProvider.LogResourceCorrelationEvent(*(static_cast<const ResourceCorrelationEventData*>(pEventData)));
break;
case PalEvent::ResourceInfoUpdate:
PAL_ASSERT(eventDataSize == sizeof(ResourceUpdateEventData));
m_gpuMemoryEventProvider.LogResourceUpdateEvent(*(static_cast<const ResourceUpdateEventData*>(pEventData)));
break;
default:
PAL_ASSERT_ALWAYS_MSG("Unhandled PalEvent type");
}
}
#if PAL_ENABLE_LOGGING
// =====================================================================================================================
// Copy PalPlatformSettings.dbgLoggerFileConfig fields into 'settings'
void Platform::GetDbgLoggerFileSettings(
Util::DbgLoggerFileSettings* pSettings)
{