forked from kR105-zz/cgminer-alt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adl.c
1435 lines (1254 loc) · 43.8 KB
/
adl.c
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 2011-2012 Con Kolivas
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#include "config.h"
#if defined(HAVE_ADL) && (defined(__linux) || defined (WIN32))
#include <stdio.h>
#include <string.h>
#include <math.h>
#ifdef HAVE_CURSES
#include <curses.h>
#endif
#include "miner.h"
#include "ADL_SDK/adl_sdk.h"
#include "compat.h"
#if defined (__linux)
#include <dlfcn.h>
#include <stdlib.h>
#include <unistd.h>
#else /* WIN32 */
#include <windows.h>
#include <tchar.h>
#endif
#include "adl_functions.h"
#ifndef HAVE_CURSES
#define wlogprint(...) applog(LOG_WARNING, __VA_ARGS__)
#endif
bool adl_active;
bool opt_reorder = false;
int opt_hysteresis = 3;
const int opt_targettemp = 75;
const int opt_overheattemp = 85;
static pthread_mutex_t adl_lock;
struct gpu_adapters {
int iAdapterIndex;
int iBusNumber;
int virtual_gpu;
int id;
};
// Memory allocation function
static void * __stdcall ADL_Main_Memory_Alloc(int iSize)
{
void *lpBuffer = malloc(iSize);
return lpBuffer;
}
// Optional Memory de-allocation function
static void __stdcall ADL_Main_Memory_Free (void **lpBuffer)
{
if (*lpBuffer) {
free (*lpBuffer);
*lpBuffer = NULL;
}
}
#if defined (LINUX)
// equivalent functions in linux
static void *GetProcAddress(void *pLibrary, const char *name)
{
return dlsym( pLibrary, name);
}
#endif
static ADL_MAIN_CONTROL_CREATE ADL_Main_Control_Create;
static ADL_MAIN_CONTROL_DESTROY ADL_Main_Control_Destroy;
static ADL_ADAPTER_NUMBEROFADAPTERS_GET ADL_Adapter_NumberOfAdapters_Get;
static ADL_ADAPTER_ADAPTERINFO_GET ADL_Adapter_AdapterInfo_Get;
static ADL_ADAPTER_ID_GET ADL_Adapter_ID_Get;
static ADL_OVERDRIVE5_TEMPERATURE_GET ADL_Overdrive5_Temperature_Get;
static ADL_OVERDRIVE5_CURRENTACTIVITY_GET ADL_Overdrive5_CurrentActivity_Get;
static ADL_OVERDRIVE5_ODPARAMETERS_GET ADL_Overdrive5_ODParameters_Get;
static ADL_OVERDRIVE5_FANSPEEDINFO_GET ADL_Overdrive5_FanSpeedInfo_Get;
static ADL_OVERDRIVE5_FANSPEED_GET ADL_Overdrive5_FanSpeed_Get;
static ADL_OVERDRIVE5_FANSPEED_SET ADL_Overdrive5_FanSpeed_Set;
static ADL_OVERDRIVE5_ODPERFORMANCELEVELS_GET ADL_Overdrive5_ODPerformanceLevels_Get;
static ADL_OVERDRIVE5_ODPERFORMANCELEVELS_SET ADL_Overdrive5_ODPerformanceLevels_Set;
static ADL_MAIN_CONTROL_REFRESH ADL_Main_Control_Refresh;
static ADL_OVERDRIVE5_POWERCONTROL_GET ADL_Overdrive5_PowerControl_Get;
static ADL_OVERDRIVE5_POWERCONTROL_SET ADL_Overdrive5_PowerControl_Set;
static ADL_OVERDRIVE5_FANSPEEDTODEFAULT_SET ADL_Overdrive5_FanSpeedToDefault_Set;
#if defined (LINUX)
static void *hDLL; // Handle to .so library
#else
HINSTANCE hDLL; // Handle to DLL
#endif
static int iNumberAdapters;
static LPAdapterInfo lpInfo = NULL;
int set_fanspeed(int gpu, int iFanSpeed);
static float __gpu_temp(struct gpu_adl *ga);
static inline void lock_adl(void)
{
mutex_lock(&adl_lock);
}
static inline void unlock_adl(void)
{
mutex_unlock(&adl_lock);
}
/* This looks for the twin GPU that has the fanspeed control of a non fanspeed
* control GPU on dual GPU cards */
static bool fanspeed_twin(struct gpu_adl *ga, struct gpu_adl *other_ga)
{
if (!other_ga->has_fanspeed)
return false;
if (abs(ga->iBusNumber - other_ga->iBusNumber) != 1)
return false;
if (strcmp(ga->strAdapterName, other_ga->strAdapterName))
return false;
return true;
}
static bool prepare_adl(void)
{
int result;
#if defined (LINUX)
hDLL = dlopen( "libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
#else
hDLL = LoadLibrary("atiadlxx.dll");
if (hDLL == NULL)
// A 32 bit calling application on 64 bit OS will fail to LoadLIbrary.
// Try to load the 32 bit library (atiadlxy.dll) instead
hDLL = LoadLibrary("atiadlxy.dll");
#endif
if (hDLL == NULL) {
applog(LOG_INFO, "Unable to load ati adl library");
return false;
}
ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE) GetProcAddress(hDLL,"ADL_Main_Control_Create");
ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY) GetProcAddress(hDLL,"ADL_Main_Control_Destroy");
ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET) GetProcAddress(hDLL,"ADL_Adapter_NumberOfAdapters_Get");
ADL_Adapter_AdapterInfo_Get = (ADL_ADAPTER_ADAPTERINFO_GET) GetProcAddress(hDLL,"ADL_Adapter_AdapterInfo_Get");
ADL_Adapter_ID_Get = (ADL_ADAPTER_ID_GET) GetProcAddress(hDLL,"ADL_Adapter_ID_Get");
ADL_Overdrive5_Temperature_Get = (ADL_OVERDRIVE5_TEMPERATURE_GET) GetProcAddress(hDLL,"ADL_Overdrive5_Temperature_Get");
ADL_Overdrive5_CurrentActivity_Get = (ADL_OVERDRIVE5_CURRENTACTIVITY_GET) GetProcAddress(hDLL, "ADL_Overdrive5_CurrentActivity_Get");
ADL_Overdrive5_ODParameters_Get = (ADL_OVERDRIVE5_ODPARAMETERS_GET) GetProcAddress(hDLL, "ADL_Overdrive5_ODParameters_Get");
ADL_Overdrive5_FanSpeedInfo_Get = (ADL_OVERDRIVE5_FANSPEEDINFO_GET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeedInfo_Get");
ADL_Overdrive5_FanSpeed_Get = (ADL_OVERDRIVE5_FANSPEED_GET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeed_Get");
ADL_Overdrive5_FanSpeed_Set = (ADL_OVERDRIVE5_FANSPEED_SET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeed_Set");
ADL_Overdrive5_ODPerformanceLevels_Get = (ADL_OVERDRIVE5_ODPERFORMANCELEVELS_GET) GetProcAddress(hDLL, "ADL_Overdrive5_ODPerformanceLevels_Get");
ADL_Overdrive5_ODPerformanceLevels_Set = (ADL_OVERDRIVE5_ODPERFORMANCELEVELS_SET) GetProcAddress(hDLL, "ADL_Overdrive5_ODPerformanceLevels_Set");
ADL_Main_Control_Refresh = (ADL_MAIN_CONTROL_REFRESH) GetProcAddress(hDLL, "ADL_Main_Control_Refresh");
ADL_Overdrive5_PowerControl_Get = (ADL_OVERDRIVE5_POWERCONTROL_GET) GetProcAddress(hDLL, "ADL_Overdrive5_PowerControl_Get");
ADL_Overdrive5_PowerControl_Set = (ADL_OVERDRIVE5_POWERCONTROL_SET) GetProcAddress(hDLL, "ADL_Overdrive5_PowerControl_Set");
ADL_Overdrive5_FanSpeedToDefault_Set = (ADL_OVERDRIVE5_FANSPEEDTODEFAULT_SET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeedToDefault_Set");
if (!ADL_Main_Control_Create || !ADL_Main_Control_Destroy ||
!ADL_Adapter_NumberOfAdapters_Get || !ADL_Adapter_AdapterInfo_Get ||
!ADL_Adapter_ID_Get || !ADL_Overdrive5_Temperature_Get ||
!ADL_Overdrive5_CurrentActivity_Get ||
!ADL_Overdrive5_ODParameters_Get || !ADL_Overdrive5_FanSpeedInfo_Get ||
!ADL_Overdrive5_FanSpeed_Get || !ADL_Overdrive5_FanSpeed_Set ||
!ADL_Overdrive5_ODPerformanceLevels_Get || !ADL_Overdrive5_ODPerformanceLevels_Set ||
!ADL_Main_Control_Refresh || !ADL_Overdrive5_PowerControl_Get ||
!ADL_Overdrive5_PowerControl_Set || !ADL_Overdrive5_FanSpeedToDefault_Set) {
applog(LOG_WARNING, "ATI ADL's API is missing");
return false;
}
// Initialise ADL. The second parameter is 1, which means:
// retrieve adapter information only for adapters that are physically present and enabled in the system
result = ADL_Main_Control_Create (ADL_Main_Memory_Alloc, 1);
if (result != ADL_OK) {
applog(LOG_INFO, "ADL Initialisation Error! Error %d!", result);
return false;
}
result = ADL_Main_Control_Refresh();
if (result != ADL_OK) {
applog(LOG_INFO, "ADL Refresh Error! Error %d!", result);
return false;
}
return true;
}
void init_adl(int nDevs)
{
int result, i, j, devices = 0, last_adapter = -1, gpu = 0, dummy = 0;
struct gpu_adapters adapters[MAX_GPUDEVICES], vadapters[MAX_GPUDEVICES];
bool devs_match = true;
if (unlikely(pthread_mutex_init(&adl_lock, NULL))) {
applog(LOG_ERR, "Failed to init adl_lock in init_adl");
return;
}
if (!prepare_adl())
return;
// Obtain the number of adapters for the system
result = ADL_Adapter_NumberOfAdapters_Get (&iNumberAdapters);
if (result != ADL_OK) {
applog(LOG_INFO, "Cannot get the number of adapters! Error %d!", result);
return ;
}
if (iNumberAdapters > 0) {
lpInfo = malloc ( sizeof (AdapterInfo) * iNumberAdapters );
memset ( lpInfo,'\0', sizeof (AdapterInfo) * iNumberAdapters );
lpInfo->iSize = sizeof(lpInfo);
// Get the AdapterInfo structure for all adapters in the system
result = ADL_Adapter_AdapterInfo_Get (lpInfo, sizeof (AdapterInfo) * iNumberAdapters);
if (result != ADL_OK) {
applog(LOG_INFO, "ADL_Adapter_AdapterInfo_Get Error! Error %d", result);
return ;
}
} else {
applog(LOG_INFO, "No adapters found");
return;
}
/* Iterate over iNumberAdapters and find the lpAdapterID of real devices */
for (i = 0; i < iNumberAdapters; i++) {
int iAdapterIndex;
int lpAdapterID;
iAdapterIndex = lpInfo[i].iAdapterIndex;
/* Get unique identifier of the adapter, 0 means not AMD */
result = ADL_Adapter_ID_Get(iAdapterIndex, &lpAdapterID);
if (result != ADL_OK) {
applog(LOG_INFO, "Failed to ADL_Adapter_ID_Get. Error %d", result);
if (result == -10)
applog(LOG_INFO, "This error says the device is not enabled");
continue;
}
/* Each adapter may have multiple entries */
if (lpAdapterID == last_adapter)
continue;
applog(LOG_DEBUG, "GPU %d "
"iAdapterIndex %d "
"strUDID %s "
"iBusNumber %d "
"iDeviceNumber %d "
"iFunctionNumber %d "
"iVendorID %d "
"strAdapterName %s ",
devices,
iAdapterIndex,
lpInfo[i].strUDID,
lpInfo[i].iBusNumber,
lpInfo[i].iDeviceNumber,
lpInfo[i].iFunctionNumber,
lpInfo[i].iVendorID,
lpInfo[i].strAdapterName);
adapters[devices].iAdapterIndex = iAdapterIndex;
adapters[devices].iBusNumber = lpInfo[i].iBusNumber;
adapters[devices].id = i;
/* We found a truly new adapter instead of a logical
* one. Now since there's no way of correlating the
* opencl enumerated devices and the ADL enumerated
* ones, we have to assume they're in the same order.*/
if (++devices > nDevs && devs_match) {
applog(LOG_ERR, "ADL found more devices than opencl!");
applog(LOG_ERR, "There is possibly at least one GPU that doesn't support OpenCL");
applog(LOG_ERR, "Use the gpu map feature to reliably map OpenCL to ADL");
devs_match = false;
}
last_adapter = lpAdapterID;
if (!lpAdapterID) {
applog(LOG_INFO, "Adapter returns ID 0 meaning not AMD. Card order might be confused");
continue;
}
}
if (devices < nDevs) {
applog(LOG_ERR, "ADL found less devices than opencl!");
applog(LOG_ERR, "There is possibly more than one display attached to a GPU");
applog(LOG_ERR, "Use the gpu map feature to reliably map OpenCL to ADL");
devs_match = false;
}
for (i = 0; i < devices; i++) {
vadapters[i].virtual_gpu = i;
vadapters[i].id = adapters[i].id;
}
/* Apply manually provided OpenCL to ADL mapping, if any */
for (i = 0; i < nDevs; i++) {
if (gpus[i].mapped) {
vadapters[gpus[i].virtual_adl].virtual_gpu = i;
applog(LOG_INFO, "Mapping OpenCL device %d to ADL device %d", i, gpus[i].virtual_adl);
} else
gpus[i].virtual_adl = i;
}
if (!devs_match) {
applog(LOG_ERR, "WARNING: Number of OpenCL and ADL devices did not match!");
applog(LOG_ERR, "Hardware monitoring may NOT match up with devices!");
} else if (opt_reorder) {
/* Windows has some kind of random ordering for bus number IDs and
* ordering the GPUs according to ascending order fixes it. Linux
* has usually sequential but decreasing order instead! */
for (i = 0; i < devices; i++) {
int j, virtual_gpu;
virtual_gpu = 0;
for (j = 0; j < devices; j++) {
if (i == j)
continue;
#ifdef WIN32
if (adapters[j].iBusNumber < adapters[i].iBusNumber)
#else
if (adapters[j].iBusNumber > adapters[i].iBusNumber)
#endif
virtual_gpu++;
}
if (virtual_gpu != i) {
applog(LOG_INFO, "Mapping device %d to GPU %d according to Bus Number order",
i, virtual_gpu);
vadapters[virtual_gpu].virtual_gpu = i;
vadapters[virtual_gpu].id = adapters[i].id;
}
}
}
if (devices > nDevs)
devices = nDevs;
for (gpu = 0; gpu < devices; gpu++) {
struct gpu_adl *ga;
int iAdapterIndex;
int lpAdapterID;
ADLODPerformanceLevels *lpOdPerformanceLevels;
int lev, adlGpu;
adlGpu = gpus[gpu].virtual_adl;
i = vadapters[adlGpu].id;
iAdapterIndex = lpInfo[i].iAdapterIndex;
gpus[gpu].virtual_gpu = vadapters[adlGpu].virtual_gpu;
/* Get unique identifier of the adapter, 0 means not AMD */
result = ADL_Adapter_ID_Get(iAdapterIndex, &lpAdapterID);
if (result != ADL_OK) {
applog(LOG_INFO, "Failed to ADL_Adapter_ID_Get. Error %d", result);
continue;
}
if (gpus[gpu].deven == DEV_DISABLED) {
gpus[gpu].gpu_engine =
gpus[gpu].gpu_memclock =
gpus[gpu].gpu_vddc =
gpus[gpu].gpu_fan =
gpus[gpu].gpu_powertune = 0;
continue;
}
applog(LOG_INFO, "GPU %d %s hardware monitoring enabled", gpu, lpInfo[i].strAdapterName);
if (gpus[gpu].name)
free(gpus[gpu].name);
gpus[gpu].name = lpInfo[i].strAdapterName;
gpus[gpu].has_adl = true;
/* Flag adl as active if any card is successfully activated */
adl_active = true;
/* From here on we know this device is a discrete device and
* should support ADL */
ga = &gpus[gpu].adl;
ga->gpu = gpu;
ga->iAdapterIndex = iAdapterIndex;
ga->lpAdapterID = lpAdapterID;
strcpy(ga->strAdapterName, lpInfo[i].strAdapterName);
ga->DefPerfLev = NULL;
ga->twin = NULL;
ga->lpOdParameters.iSize = sizeof(ADLODParameters);
if (ADL_Overdrive5_ODParameters_Get(iAdapterIndex, &ga->lpOdParameters) != ADL_OK)
applog(LOG_INFO, "Failed to ADL_Overdrive5_ODParameters_Get");
lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
/* We're only interested in the top performance level */
lpOdPerformanceLevels = malloc(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
/* Get default performance levels first */
if (ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 1, lpOdPerformanceLevels) != ADL_OK)
applog(LOG_INFO, "Failed to ADL_Overdrive5_ODPerformanceLevels_Get");
/* Set the limits we'd use based on default gpu speeds */
ga->maxspeed = ga->minspeed = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
ga->lpTemperature.iSize = sizeof(ADLTemperature);
ga->lpFanSpeedInfo.iSize = sizeof(ADLFanSpeedInfo);
ga->lpFanSpeedValue.iSize = ga->DefFanSpeedValue.iSize = sizeof(ADLFanSpeedValue);
/* Now get the current performance levels for any existing overclock */
ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
/* Save these values as the defaults in case we wish to reset to defaults */
ga->DefPerfLev = lpOdPerformanceLevels;
if (gpus[gpu].gpu_engine) {
int setengine = gpus[gpu].gpu_engine * 100;
/* Lower profiles can't have a higher setting */
for (j = 0; j < lev; j++) {
if (lpOdPerformanceLevels->aLevels[j].iEngineClock > setengine)
lpOdPerformanceLevels->aLevels[j].iEngineClock = setengine;
}
lpOdPerformanceLevels->aLevels[lev].iEngineClock = setengine;
applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, gpus[gpu].gpu_engine);
ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
ga->maxspeed = setengine;
if (gpus[gpu].min_engine)
ga->minspeed = gpus[gpu].min_engine * 100;
ga->managed = true;
if (gpus[gpu].gpu_memdiff)
set_memoryclock(gpu, gpus[gpu].gpu_engine + gpus[gpu].gpu_memdiff);
}
if (gpus[gpu].gpu_memclock) {
int setmem = gpus[gpu].gpu_memclock * 100;
for (j = 0; j < lev; j++) {
if (lpOdPerformanceLevels->aLevels[j].iMemoryClock > setmem)
lpOdPerformanceLevels->aLevels[j].iMemoryClock = setmem;
}
lpOdPerformanceLevels->aLevels[lev].iMemoryClock = setmem;
applog(LOG_INFO, "Setting GPU %d memory clock to %d", gpu, gpus[gpu].gpu_memclock);
ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
ga->managed = true;
}
if (gpus[gpu].gpu_vddc) {
int setv = gpus[gpu].gpu_vddc * 1000;
for (j = 0; j < lev; j++) {
if (lpOdPerformanceLevels->aLevels[j].iVddc > setv)
lpOdPerformanceLevels->aLevels[j].iVddc = setv;
}
lpOdPerformanceLevels->aLevels[lev].iVddc = setv;
applog(LOG_INFO, "Setting GPU %d voltage to %.3f", gpu, gpus[gpu].gpu_vddc);
ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
ga->managed = true;
}
ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
ga->iBusNumber = lpInfo[i].iBusNumber;
if (ADL_Overdrive5_FanSpeedInfo_Get(iAdapterIndex, 0, &ga->lpFanSpeedInfo) != ADL_OK)
applog(LOG_INFO, "Failed to ADL_Overdrive5_FanSpeedInfo_Get");
else
ga->has_fanspeed = true;
/* Save the fanspeed values as defaults in case we reset later */
ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
if (gpus[gpu].gpu_fan)
set_fanspeed(gpu, gpus[gpu].gpu_fan);
else
gpus[gpu].gpu_fan = 85; /* Set a nominal upper limit of 85% */
/* Not fatal if powercontrol get fails */
if (ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy) != ADL_OK)
applog(LOG_INFO, "Failed to ADL_Overdrive5_PowerControl_get");
if (gpus[gpu].gpu_powertune) {
ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, gpus[gpu].gpu_powertune);
ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
ga->managed = true;
}
/* Set some default temperatures for autotune when enabled */
if (!ga->targettemp)
ga->targettemp = opt_targettemp;
if (!ga->overtemp)
ga->overtemp = opt_overheattemp;
if (!gpus[gpu].cutofftemp)
gpus[gpu].cutofftemp = opt_cutofftemp;
if (opt_autofan) {
ga->autofan = true;
/* Set a safe starting default if we're automanaging fan speeds */
set_fanspeed(gpu, 50);
}
if (opt_autoengine) {
ga->autoengine = true;
ga->managed = true;
}
ga->lasttemp = __gpu_temp(ga);
}
for (gpu = 0; gpu < devices; gpu++) {
struct gpu_adl *ga = &gpus[gpu].adl;
int j;
for (j = 0; j < devices; j++) {
struct gpu_adl *other_ga;
if (j == gpu)
continue;
other_ga = &gpus[j].adl;
/* Search for twin GPUs on a single card. They will be
* separated by one bus id and one will have fanspeed
* while the other won't. */
if (!ga->has_fanspeed) {
if (fanspeed_twin(ga, other_ga)) {
applog(LOG_INFO, "Dual GPUs detected: %d and %d",
ga->gpu, other_ga->gpu);
ga->twin = other_ga;
other_ga->twin = ga;
}
}
}
}
}
static float __gpu_temp(struct gpu_adl *ga)
{
if (ADL_Overdrive5_Temperature_Get(ga->iAdapterIndex, 0, &ga->lpTemperature) != ADL_OK)
return -1;
return (float)ga->lpTemperature.iTemperature / 1000;
}
float gpu_temp(int gpu)
{
struct gpu_adl *ga;
float ret = -1;
if (!gpus[gpu].has_adl || !adl_active)
return ret;
ga = &gpus[gpu].adl;
lock_adl();
ret = __gpu_temp(ga);
unlock_adl();
gpus[gpu].temp = ret;
return ret;
}
static inline int __gpu_engineclock(struct gpu_adl *ga)
{
return ga->lpActivity.iEngineClock / 100;
}
int gpu_engineclock(int gpu)
{
struct gpu_adl *ga;
int ret = -1;
if (!gpus[gpu].has_adl || !adl_active)
return ret;
ga = &gpus[gpu].adl;
lock_adl();
if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
goto out;
ret = __gpu_engineclock(ga);
out:
unlock_adl();
return ret;
}
static inline int __gpu_memclock(struct gpu_adl *ga)
{
return ga->lpActivity.iMemoryClock / 100;
}
int gpu_memclock(int gpu)
{
struct gpu_adl *ga;
int ret = -1;
if (!gpus[gpu].has_adl || !adl_active)
return ret;
ga = &gpus[gpu].adl;
lock_adl();
if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
goto out;
ret = __gpu_memclock(ga);
out:
unlock_adl();
return ret;
}
static inline float __gpu_vddc(struct gpu_adl *ga)
{
return (float)ga->lpActivity.iVddc / 1000;
}
float gpu_vddc(int gpu)
{
struct gpu_adl *ga;
float ret = -1;
if (!gpus[gpu].has_adl || !adl_active)
return ret;
ga = &gpus[gpu].adl;
lock_adl();
if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
goto out;
ret = __gpu_vddc(ga);
out:
unlock_adl();
return ret;
}
static inline int __gpu_activity(struct gpu_adl *ga)
{
if (!ga->lpOdParameters.iActivityReportingSupported)
return -1;
return ga->lpActivity.iActivityPercent;
}
int gpu_activity(int gpu)
{
struct gpu_adl *ga;
int ret = -1;
if (!gpus[gpu].has_adl || !adl_active)
return ret;
ga = &gpus[gpu].adl;
lock_adl();
ret = ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity);
unlock_adl();
if (ret != ADL_OK)
return ret;
if (!ga->lpOdParameters.iActivityReportingSupported)
return ret;
return ga->lpActivity.iActivityPercent;
}
static inline int __gpu_fanspeed(struct gpu_adl *ga)
{
if (!ga->has_fanspeed && ga->twin)
return __gpu_fanspeed(ga->twin);
if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_READ))
return -1;
ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
return -1;
return ga->lpFanSpeedValue.iFanSpeed;
}
int gpu_fanspeed(int gpu)
{
struct gpu_adl *ga;
int ret = -1;
if (!gpus[gpu].has_adl || !adl_active)
return ret;
ga = &gpus[gpu].adl;
lock_adl();
ret = __gpu_fanspeed(ga);
unlock_adl();
return ret;
}
static int __gpu_fanpercent(struct gpu_adl *ga)
{
if (!ga->has_fanspeed && ga->twin)
return __gpu_fanpercent(ga->twin);
if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ ))
return -1;
ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
return -1;
return ga->lpFanSpeedValue.iFanSpeed;
}
int gpu_fanpercent(int gpu)
{
struct gpu_adl *ga;
int ret = -1;
if (!gpus[gpu].has_adl || !adl_active)
return ret;
ga = &gpus[gpu].adl;
lock_adl();
ret = __gpu_fanpercent(ga);
unlock_adl();
if (unlikely(ga->has_fanspeed && ret == -1)) {
#if 0
/* Recursive calling applog causes a hang, so disable messages */
applog(LOG_WARNING, "GPU %d stopped reporting fanspeed due to driver corruption", gpu);
if (opt_restart) {
applog(LOG_WARNING, "Restart enabled, will attempt to restart cgminer");
applog(LOG_WARNING, "You can disable this with the --no-restart option");
app_restart();
}
applog(LOG_WARNING, "Disabling fanspeed monitoring on this device");
ga->has_fanspeed = false;
if (ga->twin) {
applog(LOG_WARNING, "Disabling fanspeed linking on GPU twins");
ga->twin->twin = NULL;;
ga->twin = NULL;
}
#endif
if (opt_restart)
app_restart();
ga->has_fanspeed = false;
if (ga->twin) {
ga->twin->twin = NULL;;
ga->twin = NULL;
}
}
return ret;
}
static inline int __gpu_powertune(struct gpu_adl *ga)
{
int dummy = 0;
if (ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy) != ADL_OK)
return -1;
return ga->iPercentage;
}
int gpu_powertune(int gpu)
{
struct gpu_adl *ga;
int ret = -1;
if (!gpus[gpu].has_adl || !adl_active)
return ret;
ga = &gpus[gpu].adl;
lock_adl();
ret = __gpu_powertune(ga);
unlock_adl();
return ret;
}
bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc,
int *activity, int *fanspeed, int *fanpercent, int *powertune)
{
struct gpu_adl *ga;
if (!gpus[gpu].has_adl || !adl_active)
return false;
ga = &gpus[gpu].adl;
lock_adl();
*temp = __gpu_temp(ga);
if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK) {
*engineclock = 0;
*memclock = 0;
*vddc = 0;
*activity = 0;
} else {
*engineclock = __gpu_engineclock(ga);
*memclock = __gpu_memclock(ga);
*vddc = __gpu_vddc(ga);
*activity = __gpu_activity(ga);
}
*fanspeed = __gpu_fanspeed(ga);
*fanpercent = __gpu_fanpercent(ga);
*powertune = __gpu_powertune(ga);
unlock_adl();
return true;
}
#ifdef HAVE_CURSES
static void get_enginerange(int gpu, int *imin, int *imax)
{
struct gpu_adl *ga;
if (!gpus[gpu].has_adl || !adl_active) {
wlogprint("Get enginerange not supported\n");
return;
}
ga = &gpus[gpu].adl;
*imin = ga->lpOdParameters.sEngineClock.iMin / 100;
*imax = ga->lpOdParameters.sEngineClock.iMax / 100;
}
#endif
int set_engineclock(int gpu, int iEngineClock)
{
ADLODPerformanceLevels *lpOdPerformanceLevels;
struct cgpu_info *cgpu;
int i, lev, ret = 1;
struct gpu_adl *ga;
if (!gpus[gpu].has_adl || !adl_active) {
wlogprint("Set engineclock not supported\n");
return ret;
}
iEngineClock *= 100;
ga = &gpus[gpu].adl;
/* Keep track of intended engine clock in case the device changes
* profile and drops while idle, not taking the new engine clock */
ga->lastengine = iEngineClock;
lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
lock_adl();
if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
goto out;
for (i = 0; i < lev; i++) {
if (lpOdPerformanceLevels->aLevels[i].iEngineClock > iEngineClock)
lpOdPerformanceLevels->aLevels[i].iEngineClock = iEngineClock;
}
lpOdPerformanceLevels->aLevels[lev].iEngineClock = iEngineClock;
ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
if (lpOdPerformanceLevels->aLevels[lev].iEngineClock == iEngineClock)
ret = 0;
ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
if (ga->iEngineClock > ga->maxspeed)
ga->maxspeed = ga->iEngineClock;
if (ga->iEngineClock < ga->minspeed)
ga->minspeed = ga->iEngineClock;
ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
ga->managed = true;
out:
unlock_adl();
cgpu = &gpus[gpu];
if (cgpu->gpu_memdiff)
set_memoryclock(gpu, iEngineClock / 100 + cgpu->gpu_memdiff);
return ret;
}
#ifdef HAVE_CURSES
static void get_memoryrange(int gpu, int *imin, int *imax)
{
struct gpu_adl *ga;
if (!gpus[gpu].has_adl || !adl_active) {
wlogprint("Get memoryrange not supported\n");
return;
}
ga = &gpus[gpu].adl;
*imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
*imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
}
#endif
int set_memoryclock(int gpu, int iMemoryClock)
{
ADLODPerformanceLevels *lpOdPerformanceLevels;
int i, lev, ret = 1;
struct gpu_adl *ga;
if (!gpus[gpu].has_adl || !adl_active) {
wlogprint("Set memoryclock not supported\n");
return ret;
}
iMemoryClock *= 100;
ga = &gpus[gpu].adl;
lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
lock_adl();
if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
goto out;
lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
for (i = 0; i < lev; i++) {
if (lpOdPerformanceLevels->aLevels[i].iMemoryClock > iMemoryClock)
lpOdPerformanceLevels->aLevels[i].iMemoryClock = iMemoryClock;
}
ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock == iMemoryClock)
ret = 0;
ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
ga->managed = true;
out:
unlock_adl();
return ret;
}
#ifdef HAVE_CURSES
static void get_vddcrange(int gpu, float *imin, float *imax)
{
struct gpu_adl *ga;
if (!gpus[gpu].has_adl || !adl_active) {
wlogprint("Get vddcrange not supported\n");
return;
}
ga = &gpus[gpu].adl;
*imin = (float)ga->lpOdParameters.sVddc.iMin / 1000;
*imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
}
static float curses_float(const char *query)
{
float ret;
char *cvar;
cvar = curses_input(query);
ret = atof(cvar);
free(cvar);
return ret;
}
#endif
int set_vddc(int gpu, float fVddc)
{
ADLODPerformanceLevels *lpOdPerformanceLevels;
int i, iVddc, lev, ret = 1;
struct gpu_adl *ga;
if (!gpus[gpu].has_adl || !adl_active) {
wlogprint("Set vddc not supported\n");
return ret;
}
iVddc = 1000 * fVddc;
ga = &gpus[gpu].adl;
lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
lock_adl();
if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
goto out;
for (i = 0; i < lev; i++) {
if (lpOdPerformanceLevels->aLevels[i].iVddc > iVddc)
lpOdPerformanceLevels->aLevels[i].iVddc = iVddc;
}
lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
if (lpOdPerformanceLevels->aLevels[lev].iVddc == iVddc)
ret = 0;
ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
ga->managed = true;
out:
unlock_adl();
return ret;
}
static void get_fanrange(int gpu, int *imin, int *imax)
{
struct gpu_adl *ga;
if (!gpus[gpu].has_adl || !adl_active) {
wlogprint("Get fanrange not supported\n");
return;
}
ga = &gpus[gpu].adl;
*imin = ga->lpFanSpeedInfo.iMinPercent;
*imax = ga->lpFanSpeedInfo.iMaxPercent;
}
int set_fanspeed(int gpu, int iFanSpeed)
{
struct gpu_adl *ga;
int ret = 1;
if (!gpus[gpu].has_adl || !adl_active) {
wlogprint("Set fanspeed not supported\n");
return ret;
}
ga = &gpus[gpu].adl;
if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE ))) {
applog(LOG_DEBUG, "GPU %d doesn't support rpm or percent write", gpu);