adl.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. #include "config.h"
  2. #if defined(HAVE_ADL) && (defined(__linux) || defined (WIN32))
  3. #include <stdio.h>
  4. #include <curses.h>
  5. #include "miner.h"
  6. #include "ADL_SDK/adl_sdk.h"
  7. #if defined (__linux)
  8. #include <dlfcn.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #else /* WIN32 */
  12. #include <windows.h>
  13. #include <tchar.h>
  14. #define sleep(x) Sleep(x)
  15. #endif
  16. #include "adl_functions.h"
  17. bool adl_active;
  18. int opt_hysteresis = 3;
  19. const int opt_targettemp = 75;
  20. const int opt_overheattemp = 85;
  21. const int opt_cutofftemp = 95;
  22. static pthread_mutex_t adl_lock;
  23. // Memory allocation function
  24. static void * __stdcall ADL_Main_Memory_Alloc(int iSize)
  25. {
  26. void *lpBuffer = malloc(iSize);
  27. return lpBuffer;
  28. }
  29. // Optional Memory de-allocation function
  30. static void __stdcall ADL_Main_Memory_Free (void **lpBuffer)
  31. {
  32. if (*lpBuffer) {
  33. free (*lpBuffer);
  34. *lpBuffer = NULL;
  35. }
  36. }
  37. #if defined (LINUX)
  38. // equivalent functions in linux
  39. static void *GetProcAddress(void *pLibrary, const char *name)
  40. {
  41. return dlsym( pLibrary, name);
  42. }
  43. #endif
  44. static ADL_MAIN_CONTROL_CREATE ADL_Main_Control_Create;
  45. static ADL_MAIN_CONTROL_DESTROY ADL_Main_Control_Destroy;
  46. static ADL_ADAPTER_NUMBEROFADAPTERS_GET ADL_Adapter_NumberOfAdapters_Get;
  47. static ADL_ADAPTER_ADAPTERINFO_GET ADL_Adapter_AdapterInfo_Get;
  48. static ADL_ADAPTER_ID_GET ADL_Adapter_ID_Get;
  49. static ADL_OVERDRIVE5_TEMPERATURE_GET ADL_Overdrive5_Temperature_Get;
  50. static ADL_OVERDRIVE5_THERMALDEVICES_ENUM ADL_Overdrive5_ThermalDevices_Enum;
  51. static ADL_OVERDRIVE5_CURRENTACTIVITY_GET ADL_Overdrive5_CurrentActivity_Get;
  52. static ADL_OVERDRIVE5_ODPARAMETERS_GET ADL_Overdrive5_ODParameters_Get;
  53. static ADL_OVERDRIVE5_FANSPEEDINFO_GET ADL_Overdrive5_FanSpeedInfo_Get;
  54. static ADL_OVERDRIVE5_FANSPEED_GET ADL_Overdrive5_FanSpeed_Get;
  55. static ADL_OVERDRIVE5_FANSPEED_SET ADL_Overdrive5_FanSpeed_Set;
  56. static ADL_OVERDRIVE5_ODPERFORMANCELEVELS_GET ADL_Overdrive5_ODPerformanceLevels_Get;
  57. static ADL_OVERDRIVE5_ODPERFORMANCELEVELS_SET ADL_Overdrive5_ODPerformanceLevels_Set;
  58. static ADL_MAIN_CONTROL_REFRESH ADL_Main_Control_Refresh;
  59. static ADL_OVERDRIVE5_POWERCONTROL_GET ADL_Overdrive5_PowerControl_Get;
  60. static ADL_OVERDRIVE5_POWERCONTROL_SET ADL_Overdrive5_PowerControl_Set;
  61. static ADL_OVERDRIVE5_FANSPEEDTODEFAULT_SET ADL_Overdrive5_FanSpeedToDefault_Set;
  62. #if defined (LINUX)
  63. static void *hDLL; // Handle to .so library
  64. #else
  65. HINSTANCE hDLL; // Handle to DLL
  66. #endif
  67. static int iNumberAdapters;
  68. static LPAdapterInfo lpInfo = NULL;
  69. int set_fanspeed(int gpu, int iFanSpeed);
  70. static float __gpu_temp(struct gpu_adl *ga);
  71. static inline void lock_adl(void)
  72. {
  73. mutex_lock(&adl_lock);
  74. }
  75. static inline void unlock_adl(void)
  76. {
  77. mutex_unlock(&adl_lock);
  78. }
  79. void init_adl(int nDevs)
  80. {
  81. int i, j, devices = 0, last_adapter = -1, gpu = 0, dummy = 0;
  82. #if defined (LINUX)
  83. hDLL = dlopen( "libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
  84. #else
  85. hDLL = LoadLibrary("atiadlxx.dll");
  86. if (hDLL == NULL)
  87. // A 32 bit calling application on 64 bit OS will fail to LoadLIbrary.
  88. // Try to load the 32 bit library (atiadlxy.dll) instead
  89. hDLL = LoadLibrary("atiadlxy.dll");
  90. #endif
  91. if (hDLL == NULL) {
  92. applog(LOG_INFO, "Unable to load ati adl library");
  93. return;
  94. }
  95. if (unlikely(pthread_mutex_init(&adl_lock, NULL))) {
  96. applog(LOG_ERR, "Failed to init adl_lock in init_adl");
  97. return;
  98. }
  99. ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE) GetProcAddress(hDLL,"ADL_Main_Control_Create");
  100. ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY) GetProcAddress(hDLL,"ADL_Main_Control_Destroy");
  101. ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET) GetProcAddress(hDLL,"ADL_Adapter_NumberOfAdapters_Get");
  102. ADL_Adapter_AdapterInfo_Get = (ADL_ADAPTER_ADAPTERINFO_GET) GetProcAddress(hDLL,"ADL_Adapter_AdapterInfo_Get");
  103. ADL_Adapter_ID_Get = (ADL_ADAPTER_ID_GET) GetProcAddress(hDLL,"ADL_Adapter_ID_Get");
  104. ADL_Overdrive5_Temperature_Get = (ADL_OVERDRIVE5_TEMPERATURE_GET) GetProcAddress(hDLL,"ADL_Overdrive5_Temperature_Get");
  105. ADL_Overdrive5_ThermalDevices_Enum = (ADL_OVERDRIVE5_THERMALDEVICES_ENUM) GetProcAddress(hDLL,"ADL_Overdrive5_ThermalDevices_Enum");
  106. ADL_Overdrive5_CurrentActivity_Get = (ADL_OVERDRIVE5_CURRENTACTIVITY_GET) GetProcAddress(hDLL, "ADL_Overdrive5_CurrentActivity_Get");
  107. ADL_Overdrive5_ODParameters_Get = (ADL_OVERDRIVE5_ODPARAMETERS_GET) GetProcAddress(hDLL, "ADL_Overdrive5_ODParameters_Get");
  108. ADL_Overdrive5_FanSpeedInfo_Get = (ADL_OVERDRIVE5_FANSPEEDINFO_GET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeedInfo_Get");
  109. ADL_Overdrive5_FanSpeed_Get = (ADL_OVERDRIVE5_FANSPEED_GET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeed_Get");
  110. ADL_Overdrive5_FanSpeed_Set = (ADL_OVERDRIVE5_FANSPEED_SET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeed_Set");
  111. ADL_Overdrive5_ODPerformanceLevels_Get = (ADL_OVERDRIVE5_ODPERFORMANCELEVELS_GET) GetProcAddress(hDLL, "ADL_Overdrive5_ODPerformanceLevels_Get");
  112. ADL_Overdrive5_ODPerformanceLevels_Set = (ADL_OVERDRIVE5_ODPERFORMANCELEVELS_SET) GetProcAddress(hDLL, "ADL_Overdrive5_ODPerformanceLevels_Set");
  113. ADL_Main_Control_Refresh = (ADL_MAIN_CONTROL_REFRESH) GetProcAddress(hDLL, "ADL_Main_Control_Refresh");
  114. ADL_Overdrive5_PowerControl_Get = (ADL_OVERDRIVE5_POWERCONTROL_GET) GetProcAddress(hDLL, "ADL_Overdrive5_PowerControl_Get");
  115. ADL_Overdrive5_PowerControl_Set = (ADL_OVERDRIVE5_POWERCONTROL_SET) GetProcAddress(hDLL, "ADL_Overdrive5_PowerControl_Set");
  116. ADL_Overdrive5_FanSpeedToDefault_Set = (ADL_OVERDRIVE5_FANSPEEDTODEFAULT_SET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeedToDefault_Set");
  117. if (!ADL_Main_Control_Create || !ADL_Main_Control_Destroy ||
  118. !ADL_Adapter_NumberOfAdapters_Get || !ADL_Adapter_AdapterInfo_Get ||
  119. !ADL_Adapter_ID_Get || !ADL_Overdrive5_Temperature_Get ||
  120. !ADL_Overdrive5_ThermalDevices_Enum || !ADL_Overdrive5_CurrentActivity_Get ||
  121. !ADL_Overdrive5_ODParameters_Get || !ADL_Overdrive5_FanSpeedInfo_Get ||
  122. !ADL_Overdrive5_FanSpeed_Get || !ADL_Overdrive5_FanSpeed_Set ||
  123. !ADL_Overdrive5_ODPerformanceLevels_Get || !ADL_Overdrive5_ODPerformanceLevels_Set ||
  124. !ADL_Main_Control_Refresh || !ADL_Overdrive5_PowerControl_Get ||
  125. !ADL_Overdrive5_PowerControl_Set || !ADL_Overdrive5_FanSpeedToDefault_Set) {
  126. applog(LOG_WARNING, "ATI ADL's API is missing");
  127. return;
  128. }
  129. // Initialise ADL. The second parameter is 1, which means:
  130. // retrieve adapter information only for adapters that are physically present and enabled in the system
  131. if (ADL_Main_Control_Create (ADL_Main_Memory_Alloc, 1) != ADL_OK) {
  132. applog(LOG_INFO, "ADL Initialisation Error!");
  133. return ;
  134. }
  135. if (ADL_Main_Control_Refresh() != ADL_OK) {
  136. applog(LOG_INFO, "ADL Refresh Error!");
  137. return ;
  138. }
  139. // Obtain the number of adapters for the system
  140. if (ADL_Adapter_NumberOfAdapters_Get ( &iNumberAdapters ) != ADL_OK) {
  141. applog(LOG_INFO, "Cannot get the number of adapters!\n");
  142. return ;
  143. }
  144. if (iNumberAdapters > 0) {
  145. lpInfo = malloc ( sizeof (AdapterInfo) * iNumberAdapters );
  146. memset ( lpInfo,'\0', sizeof (AdapterInfo) * iNumberAdapters );
  147. lpInfo->iSize = sizeof(lpInfo);
  148. // Get the AdapterInfo structure for all adapters in the system
  149. if (ADL_Adapter_AdapterInfo_Get (lpInfo, sizeof (AdapterInfo) * iNumberAdapters) != ADL_OK) {
  150. applog(LOG_INFO, "ADL_Adapter_AdapterInfo_Get Error!");
  151. return ;
  152. }
  153. } else {
  154. applog(LOG_INFO, "No adapters found");
  155. return;
  156. }
  157. for (i = 0; i < iNumberAdapters; i++) {
  158. struct gpu_adl *ga;
  159. int iAdapterIndex;
  160. int lpAdapterID;
  161. ADLODPerformanceLevels *lpOdPerformanceLevels;
  162. int lev;
  163. iAdapterIndex = lpInfo[i].iAdapterIndex;
  164. /* Get unique identifier of the adapter, 0 means not AMD */
  165. if (ADL_Adapter_ID_Get(iAdapterIndex, &lpAdapterID) != ADL_OK) {
  166. applog(LOG_INFO, "Failed to ADL_Adapter_ID_Get");
  167. continue;
  168. }
  169. /* Each adapter may have multiple entries */
  170. if (lpAdapterID == last_adapter)
  171. continue;
  172. /* We found a truly new adapter instead of a logical
  173. * one. Now since there's no way of correlating the
  174. * opencl enumerated devices and the ADL enumerated
  175. * ones, we have to assume they're in the same order.*/
  176. if (++devices > nDevs) {
  177. applog(LOG_ERR, "ADL found more devices than opencl");
  178. return;
  179. }
  180. gpu = devices - 1;
  181. last_adapter = lpAdapterID;
  182. if (!lpAdapterID) {
  183. applog(LOG_INFO, "Adapter returns ID 0 meaning not AMD. Card order might be confused");
  184. continue;
  185. }
  186. if (!gpus[gpu].enabled) {
  187. gpus[i].gpu_engine =
  188. gpus[i].gpu_memclock =
  189. gpus[i].gpu_vddc =
  190. gpus[i].gpu_fan =
  191. gpus[i].gpu_powertune = 0;
  192. continue;
  193. }
  194. gpus[gpu].has_adl = true;
  195. /* Flag adl as active if any card is successfully activated */
  196. adl_active = true;
  197. /* From here on we know this device is a discrete device and
  198. * should support ADL */
  199. ga = &gpus[gpu].adl;
  200. ga->iAdapterIndex = iAdapterIndex;
  201. ga->lpAdapterID = lpAdapterID;
  202. ga->DefPerfLev = NULL;
  203. ga->lpThermalControllerInfo.iSize=sizeof(ADLThermalControllerInfo);
  204. if (ADL_Overdrive5_ThermalDevices_Enum(iAdapterIndex, 0, &ga->lpThermalControllerInfo) != ADL_OK)
  205. applog(LOG_INFO, "Failed to ADL_Overdrive5_ThermalDevices_Enum");
  206. ga->lpOdParameters.iSize = sizeof(ADLODParameters);
  207. if (ADL_Overdrive5_ODParameters_Get(iAdapterIndex, &ga->lpOdParameters) != ADL_OK)
  208. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODParameters_Get");
  209. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  210. /* We're only interested in the top performance level */
  211. lpOdPerformanceLevels = malloc(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  212. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  213. /* Get default performance levels first */
  214. if (ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 1, lpOdPerformanceLevels) != ADL_OK)
  215. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODPerformanceLevels_Get");
  216. /* Set the limits we'd use based on default gpu speeds */
  217. ga->maxspeed = ga->minspeed = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  218. ga->lpTemperature.iSize = sizeof(ADLTemperature);
  219. ga->lpFanSpeedInfo.iSize = sizeof(ADLFanSpeedInfo);
  220. ga->lpFanSpeedValue.iSize = ga->DefFanSpeedValue.iSize = sizeof(ADLFanSpeedValue);
  221. /* Now get the current performance levels for any existing overclock */
  222. ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
  223. /* Save these values as the defaults in case we wish to reset to defaults */
  224. ga->DefPerfLev = lpOdPerformanceLevels;
  225. if (gpus[gpu].gpu_engine) {
  226. int setengine = gpus[gpu].gpu_engine * 100;
  227. /* Lower profiles can't have a higher setting */
  228. for (j = 0; j < lev; j++) {
  229. if (lpOdPerformanceLevels->aLevels[j].iEngineClock > setengine)
  230. lpOdPerformanceLevels->aLevels[j].iEngineClock = setengine;
  231. }
  232. lpOdPerformanceLevels->aLevels[lev].iEngineClock = setengine;
  233. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, gpus[gpu].gpu_engine);
  234. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  235. ga->maxspeed = setengine;
  236. if (gpus[gpu].min_engine)
  237. ga->minspeed = gpus[gpu].min_engine * 100;
  238. ga->managed = true;
  239. }
  240. if (gpus[gpu].gpu_memclock) {
  241. int setmem = gpus[gpu].gpu_memclock * 100;
  242. for (j = 0; j < lev; j++) {
  243. if (lpOdPerformanceLevels->aLevels[j].iMemoryClock > setmem)
  244. lpOdPerformanceLevels->aLevels[j].iMemoryClock = setmem;
  245. }
  246. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = setmem;
  247. applog(LOG_INFO, "Setting GPU %d memory clock to %d", gpu, gpus[gpu].gpu_memclock);
  248. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  249. ga->managed = true;
  250. }
  251. if (gpus[gpu].gpu_vddc) {
  252. int setv = gpus[gpu].gpu_vddc * 1000;
  253. for (j = 0; j < lev; j++) {
  254. if (lpOdPerformanceLevels->aLevels[j].iVddc > setv)
  255. lpOdPerformanceLevels->aLevels[j].iVddc = setv;
  256. }
  257. lpOdPerformanceLevels->aLevels[lev].iVddc = setv;
  258. applog(LOG_INFO, "Setting GPU %d voltage to %.3f", gpu, gpus[gpu].gpu_vddc);
  259. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  260. ga->managed = true;
  261. }
  262. ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
  263. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  264. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  265. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  266. if (ADL_Overdrive5_FanSpeedInfo_Get(iAdapterIndex, 0, &ga->lpFanSpeedInfo) != ADL_OK)
  267. applog(LOG_INFO, "Failed to ADL_Overdrive5_FanSpeedInfo_Get");
  268. /* Save the fanspeed values as defaults in case we reset later */
  269. ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  270. if (gpus[gpu].gpu_fan)
  271. set_fanspeed(gpu, gpus[gpu].gpu_fan);
  272. else
  273. gpus[gpu].gpu_fan = 85; /* Set a nominal upper limit of 85% */
  274. /* Not fatal if powercontrol get fails */
  275. if (ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy) != ADL_OK)
  276. applog(LOG_INFO, "Failed to ADL_Overdrive5_PowerControl_get");
  277. if (gpus[gpu].gpu_powertune) {
  278. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, gpus[gpu].gpu_powertune);
  279. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  280. ga->managed = true;
  281. }
  282. /* Set some default temperatures for autotune when enabled */
  283. if (!ga->targettemp)
  284. ga->targettemp = opt_targettemp;
  285. if (!ga->overtemp)
  286. ga->overtemp = opt_overheattemp;
  287. if (!ga->cutofftemp)
  288. ga->cutofftemp = opt_cutofftemp;
  289. if (opt_autofan) {
  290. ga->autofan = true;
  291. /* Set a safe starting default if we're automanaging fan speeds */
  292. set_fanspeed(gpu, gpus[gpu].gpu_fan);
  293. }
  294. if (opt_autoengine) {
  295. ga->autoengine = true;
  296. ga->managed = true;
  297. }
  298. ga->lasttemp = __gpu_temp(ga);
  299. }
  300. }
  301. static float __gpu_temp(struct gpu_adl *ga)
  302. {
  303. if (ADL_Overdrive5_Temperature_Get(ga->iAdapterIndex, 0, &ga->lpTemperature) != ADL_OK)
  304. return -1;
  305. return (float)ga->lpTemperature.iTemperature / 1000;
  306. }
  307. float gpu_temp(int gpu)
  308. {
  309. struct gpu_adl *ga;
  310. float ret = -1;
  311. if (!gpus[gpu].has_adl || !adl_active)
  312. return ret;
  313. ga = &gpus[gpu].adl;
  314. lock_adl();
  315. ret = __gpu_temp(ga);
  316. unlock_adl();
  317. return ret;
  318. }
  319. static inline int __gpu_engineclock(struct gpu_adl *ga)
  320. {
  321. return ga->lpActivity.iEngineClock / 100;
  322. }
  323. int gpu_engineclock(int gpu)
  324. {
  325. struct gpu_adl *ga;
  326. int ret = -1;
  327. if (!gpus[gpu].has_adl || !adl_active)
  328. return ret;
  329. ga = &gpus[gpu].adl;
  330. lock_adl();
  331. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  332. goto out;
  333. ret = __gpu_engineclock(ga);
  334. out:
  335. unlock_adl();
  336. return ret;
  337. }
  338. static inline int __gpu_memclock(struct gpu_adl *ga)
  339. {
  340. return ga->lpActivity.iMemoryClock / 100;
  341. }
  342. int gpu_memclock(int gpu)
  343. {
  344. struct gpu_adl *ga;
  345. int ret = -1;
  346. if (!gpus[gpu].has_adl || !adl_active)
  347. return ret;
  348. ga = &gpus[gpu].adl;
  349. lock_adl();
  350. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  351. goto out;
  352. ret = __gpu_memclock(ga);
  353. out:
  354. unlock_adl();
  355. return ret;
  356. }
  357. static inline float __gpu_vddc(struct gpu_adl *ga)
  358. {
  359. return (float)ga->lpActivity.iVddc / 1000;
  360. }
  361. float gpu_vddc(int gpu)
  362. {
  363. struct gpu_adl *ga;
  364. float ret = -1;
  365. if (!gpus[gpu].has_adl || !adl_active)
  366. return ret;
  367. ga = &gpus[gpu].adl;
  368. lock_adl();
  369. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  370. goto out;
  371. ret = __gpu_vddc(ga);
  372. out:
  373. unlock_adl();
  374. return ret;
  375. }
  376. static inline int __gpu_activity(struct gpu_adl *ga)
  377. {
  378. if (!ga->lpOdParameters.iActivityReportingSupported)
  379. return -1;
  380. return ga->lpActivity.iActivityPercent;
  381. }
  382. int gpu_activity(int gpu)
  383. {
  384. struct gpu_adl *ga;
  385. int ret = -1;
  386. if (!gpus[gpu].has_adl || !adl_active)
  387. return ret;
  388. ga = &gpus[gpu].adl;
  389. lock_adl();
  390. ret = ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity);
  391. unlock_adl();
  392. if (ret != ADL_OK)
  393. return ret;
  394. if (!ga->lpOdParameters.iActivityReportingSupported)
  395. return ret;
  396. return ga->lpActivity.iActivityPercent;
  397. }
  398. static inline int __gpu_fanspeed(struct gpu_adl *ga)
  399. {
  400. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_READ))
  401. return -1;
  402. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  403. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  404. return -1;
  405. return ga->lpFanSpeedValue.iFanSpeed;
  406. }
  407. int gpu_fanspeed(int gpu)
  408. {
  409. struct gpu_adl *ga;
  410. int ret = -1;
  411. if (!gpus[gpu].has_adl || !adl_active)
  412. return ret;
  413. ga = &gpus[gpu].adl;
  414. lock_adl();
  415. ret = __gpu_fanspeed(ga);
  416. unlock_adl();
  417. return ret;
  418. }
  419. static inline int __gpu_fanpercent(struct gpu_adl *ga)
  420. {
  421. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ ))
  422. return -1;
  423. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  424. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  425. return -1;
  426. return ga->lpFanSpeedValue.iFanSpeed;
  427. }
  428. int gpu_fanpercent(int gpu)
  429. {
  430. struct gpu_adl *ga;
  431. int ret = -1;
  432. if (!gpus[gpu].has_adl || !adl_active)
  433. return ret;
  434. ga = &gpus[gpu].adl;
  435. lock_adl();
  436. ret = __gpu_fanpercent(ga);
  437. unlock_adl();
  438. return ret;
  439. }
  440. static inline int __gpu_powertune(struct gpu_adl *ga)
  441. {
  442. int dummy = 0;
  443. if (ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy) != ADL_OK)
  444. return -1;
  445. return ga->iPercentage;
  446. }
  447. int gpu_powertune(int gpu)
  448. {
  449. struct gpu_adl *ga;
  450. int ret = -1;
  451. if (!gpus[gpu].has_adl || !adl_active)
  452. return ret;
  453. ga = &gpus[gpu].adl;
  454. lock_adl();
  455. ret = __gpu_powertune(ga);
  456. unlock_adl();
  457. return ret;
  458. }
  459. bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc,
  460. int *activity, int *fanspeed, int *fanpercent, int *powertune)
  461. {
  462. struct gpu_adl *ga;
  463. if (!gpus[gpu].has_adl || !adl_active)
  464. return false;
  465. ga = &gpus[gpu].adl;
  466. lock_adl();
  467. *temp = __gpu_temp(ga);
  468. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK) {
  469. *engineclock = 0;
  470. *memclock = 0;
  471. *vddc = 0;
  472. *activity = 0;
  473. } else {
  474. *engineclock = __gpu_engineclock(ga);
  475. *memclock = __gpu_memclock(ga);
  476. *vddc = __gpu_vddc(ga);
  477. *activity = __gpu_activity(ga);
  478. }
  479. *fanspeed = __gpu_fanspeed(ga);
  480. *fanpercent = __gpu_fanpercent(ga);
  481. *powertune = __gpu_powertune(ga);
  482. unlock_adl();
  483. return true;
  484. }
  485. static void get_enginerange(int gpu, int *imin, int *imax)
  486. {
  487. struct gpu_adl *ga;
  488. if (!gpus[gpu].has_adl || !adl_active) {
  489. wlogprint("Get enginerange not supported\n");
  490. return;
  491. }
  492. ga = &gpus[gpu].adl;
  493. *imin = ga->lpOdParameters.sEngineClock.iMin / 100;
  494. *imax = ga->lpOdParameters.sEngineClock.iMax / 100;
  495. }
  496. int set_engineclock(int gpu, int iEngineClock)
  497. {
  498. ADLODPerformanceLevels *lpOdPerformanceLevels;
  499. int i, lev, ret = 1;
  500. struct gpu_adl *ga;
  501. if (!gpus[gpu].has_adl || !adl_active) {
  502. wlogprint("Set engineclock not supported\n");
  503. return ret;
  504. }
  505. iEngineClock *= 100;
  506. ga = &gpus[gpu].adl;
  507. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  508. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  509. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  510. lock_adl();
  511. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  512. goto out;
  513. for (i = 0; i < lev; i++) {
  514. if (lpOdPerformanceLevels->aLevels[i].iEngineClock > iEngineClock)
  515. lpOdPerformanceLevels->aLevels[i].iEngineClock = iEngineClock;
  516. }
  517. lpOdPerformanceLevels->aLevels[lev].iEngineClock = iEngineClock;
  518. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  519. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  520. if (lpOdPerformanceLevels->aLevels[lev].iEngineClock == iEngineClock)
  521. ret = 0;
  522. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  523. if (ga->iEngineClock > ga->maxspeed)
  524. ga->maxspeed = ga->iEngineClock;
  525. if (ga->iEngineClock < ga->minspeed)
  526. ga->minspeed = ga->iEngineClock;
  527. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  528. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  529. ga->managed = true;
  530. out:
  531. unlock_adl();
  532. return ret;
  533. }
  534. static void get_memoryrange(int gpu, int *imin, int *imax)
  535. {
  536. struct gpu_adl *ga;
  537. if (!gpus[gpu].has_adl || !adl_active) {
  538. wlogprint("Get memoryrange not supported\n");
  539. return;
  540. }
  541. ga = &gpus[gpu].adl;
  542. *imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
  543. *imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
  544. }
  545. int set_memoryclock(int gpu, int iMemoryClock)
  546. {
  547. ADLODPerformanceLevels *lpOdPerformanceLevels;
  548. int i, lev, ret = 1;
  549. struct gpu_adl *ga;
  550. if (!gpus[gpu].has_adl || !adl_active) {
  551. wlogprint("Set memoryclock not supported\n");
  552. return ret;
  553. }
  554. iMemoryClock *= 100;
  555. ga = &gpus[gpu].adl;
  556. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  557. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  558. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  559. lock_adl();
  560. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  561. goto out;
  562. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
  563. for (i = 0; i < lev; i++) {
  564. if (lpOdPerformanceLevels->aLevels[i].iMemoryClock > iMemoryClock)
  565. lpOdPerformanceLevels->aLevels[i].iMemoryClock = iMemoryClock;
  566. }
  567. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  568. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  569. if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock == iMemoryClock)
  570. ret = 0;
  571. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  572. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  573. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  574. ga->managed = true;
  575. out:
  576. unlock_adl();
  577. return ret;
  578. }
  579. static void get_vddcrange(int gpu, float *imin, float *imax)
  580. {
  581. struct gpu_adl *ga;
  582. if (!gpus[gpu].has_adl || !adl_active) {
  583. wlogprint("Get vddcrange not supported\n");
  584. return;
  585. }
  586. ga = &gpus[gpu].adl;
  587. *imin = (float)ga->lpOdParameters.sVddc.iMin / 1000;
  588. *imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
  589. }
  590. static float curses_float(const char *query)
  591. {
  592. float ret;
  593. char *cvar;
  594. cvar = curses_input(query);
  595. ret = atof(cvar);
  596. free(cvar);
  597. return ret;
  598. }
  599. int set_vddc(int gpu, float fVddc)
  600. {
  601. ADLODPerformanceLevels *lpOdPerformanceLevels;
  602. int i, iVddc, lev, ret = 1;
  603. struct gpu_adl *ga;
  604. if (!gpus[gpu].has_adl || !adl_active) {
  605. wlogprint("Set vddc not supported\n");
  606. return ret;
  607. }
  608. iVddc = 1000 * fVddc;
  609. ga = &gpus[gpu].adl;
  610. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  611. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  612. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  613. lock_adl();
  614. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  615. goto out;
  616. for (i = 0; i < lev; i++) {
  617. if (lpOdPerformanceLevels->aLevels[i].iVddc > iVddc)
  618. lpOdPerformanceLevels->aLevels[i].iVddc = iVddc;
  619. }
  620. lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
  621. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  622. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  623. if (lpOdPerformanceLevels->aLevels[lev].iVddc == iVddc)
  624. ret = 0;
  625. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  626. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  627. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  628. ga->managed = true;
  629. out:
  630. unlock_adl();
  631. return ret;
  632. }
  633. static void get_fanrange(int gpu, int *imin, int *imax)
  634. {
  635. struct gpu_adl *ga;
  636. if (!gpus[gpu].has_adl || !adl_active) {
  637. wlogprint("Get fanrange not supported\n");
  638. return;
  639. }
  640. ga = &gpus[gpu].adl;
  641. *imin = ga->lpFanSpeedInfo.iMinPercent;
  642. *imax = ga->lpFanSpeedInfo.iMaxPercent;
  643. }
  644. int set_fanspeed(int gpu, int iFanSpeed)
  645. {
  646. struct gpu_adl *ga;
  647. int ret = 1;
  648. if (!gpus[gpu].has_adl || !adl_active) {
  649. wlogprint("Set fanspeed not supported\n");
  650. return ret;
  651. }
  652. ga = &gpus[gpu].adl;
  653. if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE ))) {
  654. if (opt_debug)
  655. applog(LOG_DEBUG, "GPU %d doesn't support rpm or percent write", gpu);
  656. return ret;
  657. }
  658. /* Store what fanspeed we're actually aiming for for re-entrant changes
  659. * in case this device does not support fine setting changes */
  660. ga->targetfan = iFanSpeed;
  661. lock_adl();
  662. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK) {
  663. if (opt_debug)
  664. applog(LOG_DEBUG, "GPU %d call to fanspeed get failed", gpu);
  665. }
  666. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE)) {
  667. /* Must convert speed to an RPM */
  668. iFanSpeed = ga->lpFanSpeedInfo.iMaxRPM * iFanSpeed / 100;
  669. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  670. } else
  671. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  672. if (!(ga->lpFanSpeedValue.iFlags & ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED)) {
  673. /* If user defined is not already specified, set it first */
  674. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  675. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  676. }
  677. ga->lpFanSpeedValue.iFanSpeed = iFanSpeed;
  678. ret = ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  679. ga->managed = true;
  680. unlock_adl();
  681. return ret;
  682. }
  683. static int set_powertune(int gpu, int iPercentage)
  684. {
  685. struct gpu_adl *ga;
  686. int dummy, ret = 1;
  687. if (!gpus[gpu].has_adl || !adl_active) {
  688. wlogprint("Set powertune not supported\n");
  689. return ret;
  690. }
  691. ga = &gpus[gpu].adl;
  692. lock_adl();
  693. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, iPercentage);
  694. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  695. if (ga->iPercentage == iPercentage)
  696. ret = 0;
  697. ga->managed = true;
  698. unlock_adl();
  699. return ret;
  700. }
  701. void gpu_autotune(int gpu, bool *enable)
  702. {
  703. int temp, fanpercent, engine, newpercent, newengine;
  704. bool fan_optimal = true;
  705. struct cgpu_info *cgpu;
  706. struct gpu_adl *ga;
  707. cgpu = &gpus[gpu];
  708. ga = &cgpu->adl;
  709. lock_adl();
  710. ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity);
  711. temp = __gpu_temp(ga);
  712. newpercent = fanpercent = __gpu_fanpercent(ga);
  713. unlock_adl();
  714. newengine = engine = gpu_engineclock(gpu) * 100;
  715. if (temp && fanpercent >= 0 && ga->autofan) {
  716. int top = gpus[gpu].gpu_fan;
  717. int bot = gpus[gpu].min_fan;
  718. int iMin = 0, iMax = 100;
  719. get_fanrange(gpu, &iMin, &iMax);
  720. if (temp > ga->overtemp && fanpercent < iMax) {
  721. applog(LOG_WARNING, "Overheat detected on GPU %d, increasing fan to 100%", gpu);
  722. newpercent = iMax;
  723. } else if (temp > ga->targettemp && fanpercent < top && temp >= ga->lasttemp) {
  724. if (opt_debug)
  725. applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
  726. if (temp > ga->targettemp + opt_hysteresis)
  727. newpercent = ga->targetfan + 10;
  728. else
  729. newpercent = ga->targetfan + 5;
  730. if (newpercent > top)
  731. newpercent = top;
  732. } else if (fanpercent > bot && temp < ga->targettemp - opt_hysteresis && temp <= ga->lasttemp) {
  733. if (opt_debug)
  734. applog(LOG_DEBUG, "Temperature %d degrees below target, decreasing fanspeed", opt_hysteresis);
  735. newpercent = ga->targetfan - 1;
  736. }
  737. if (newpercent > iMax)
  738. newpercent = iMax;
  739. else if (newpercent < iMin)
  740. newpercent = iMin;
  741. if (newpercent != fanpercent) {
  742. fan_optimal = false;
  743. applog(LOG_INFO, "Setting GPU %d fan percentage to %d", gpu, newpercent);
  744. set_fanspeed(gpu, newpercent);
  745. }
  746. }
  747. if (engine && ga->autoengine) {
  748. if (temp > ga->cutofftemp) {
  749. applog(LOG_WARNING, "Hit thermal cutoff limit on GPU %d, disabling!", gpu);
  750. *enable = false;
  751. newengine = ga->minspeed;
  752. } else if (temp > ga->overtemp && engine > ga->minspeed) {
  753. applog(LOG_WARNING, "Overheat detected, decreasing GPU %d clock speed", gpu);
  754. newengine = ga->minspeed;
  755. /* Only try to tune engine speed if the current performance level is at max */
  756. } else if ((ga->lpActivity.iCurrentPerformanceLevel == ga->lpOdParameters.iNumberOfPerformanceLevels - 1) &&
  757. (temp > ga->targettemp + opt_hysteresis && engine > ga->minspeed && fan_optimal)) {
  758. if (opt_debug)
  759. applog(LOG_DEBUG, "Temperature %d degrees over target, decreasing clock speed", opt_hysteresis);
  760. newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
  761. } else if ((ga->lpActivity.iCurrentPerformanceLevel == ga->lpOdParameters.iNumberOfPerformanceLevels - 1) &&
  762. (temp < ga->targettemp && engine < ga->maxspeed)) {
  763. if (opt_debug)
  764. applog(LOG_DEBUG, "Temperature below target, increasing clock speed");
  765. newengine = engine + ga->lpOdParameters.sEngineClock.iStep;
  766. }
  767. if (newengine > ga->maxspeed)
  768. newengine = ga->maxspeed;
  769. else if (newengine < ga->minspeed)
  770. newengine = ga->minspeed;
  771. if (newengine != engine) {
  772. newengine /= 100;
  773. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, newengine);
  774. set_engineclock(gpu, newengine);
  775. if (cgpu->gpu_memdiff)
  776. set_memoryclock(gpu, newengine + cgpu->gpu_memdiff);
  777. }
  778. }
  779. ga->lasttemp = temp;
  780. }
  781. void set_defaultfan(int gpu)
  782. {
  783. struct gpu_adl *ga;
  784. if (!gpus[gpu].has_adl || !adl_active)
  785. return;
  786. ga = &gpus[gpu].adl;
  787. lock_adl();
  788. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  789. unlock_adl();
  790. }
  791. void set_defaultengine(int gpu)
  792. {
  793. struct gpu_adl *ga;
  794. if (!gpus[gpu].has_adl || !adl_active)
  795. return;
  796. ga = &gpus[gpu].adl;
  797. lock_adl();
  798. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  799. unlock_adl();
  800. }
  801. void change_autosettings(int gpu)
  802. {
  803. struct gpu_adl *ga = &gpus[gpu].adl;
  804. char input;
  805. int val;
  806. wlogprint("Target temperature: %d\n", ga->targettemp);
  807. wlogprint("Overheat temperature: %d\n", ga->overtemp);
  808. wlogprint("Cutoff temperature: %d\n", ga->cutofftemp);
  809. wlogprint("Toggle [F]an auto [G]PU auto\nChange [T]arget [O]verheat [C]utoff\n");
  810. wlogprint("Or press any other key to continue\n");
  811. input = getch();
  812. if (!strncasecmp(&input, "f", 1)) {
  813. ga->autofan ^= true;
  814. wlogprint("Fan autotune is now %s\n", ga->autofan ? "enabled" : "disabled");
  815. if (!ga->autofan) {
  816. wlogprint("Resetting fan to startup settings\n");
  817. set_defaultfan(gpu);
  818. }
  819. } else if (!strncasecmp(&input, "g", 1)) {
  820. ga->autoengine ^= true;
  821. wlogprint("GPU engine clock autotune is now %s\n", ga->autoengine ? "enabled" : "disabled");
  822. if (!ga->autoengine) {
  823. wlogprint("Resetting GPU engine clock to startup settings\n");
  824. set_defaultengine(gpu);
  825. }
  826. } else if (!strncasecmp(&input, "t", 1)) {
  827. val = curses_int("Enter target temperature for this GPU in C (0-200)");
  828. if (val < 0 || val > 200)
  829. wlogprint("Invalid temperature");
  830. else
  831. ga->targettemp = val;
  832. } else if (!strncasecmp(&input, "o", 1)) {
  833. wlogprint("Enter overheat temperature for this GPU in C (%d+)", ga->targettemp);
  834. val = curses_int("");
  835. if (val <= ga->targettemp || val > 200)
  836. wlogprint("Invalid temperature");
  837. else
  838. ga->overtemp = val;
  839. } else if (!strncasecmp(&input, "c", 1)) {
  840. wlogprint("Enter cutoff temperature for this GPU in C (%d+)", ga->overtemp);
  841. val = curses_int("");
  842. if (val <= ga->overtemp || val > 200)
  843. wlogprint("Invalid temperature");
  844. else
  845. ga->cutofftemp = val;
  846. }
  847. }
  848. void change_gpusettings(int gpu)
  849. {
  850. struct gpu_adl *ga = &gpus[gpu].adl;
  851. float fval, fmin = 0, fmax = 0;
  852. int val, imin = 0, imax = 0;
  853. char input;
  854. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  855. float temp = 0, vddc = 0;
  856. updated:
  857. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune))
  858. wlogprint("Temp: %.1f C\n", temp);
  859. if (fanpercent >= 0 || fanspeed >= 0) {
  860. wlogprint("Fan Speed: ");
  861. if (fanpercent >= 0)
  862. wlogprint("%d%% ", fanpercent);
  863. if (fanspeed >= 0)
  864. wlogprint("(%d RPM)", fanspeed);
  865. wlogprint("\n");
  866. }
  867. wlogprint("Engine Clock: %d MHz\nMemory Clock: %d Mhz\nVddc: %.3f V\nActivity: %d%%\nPowertune: %d%%\n",
  868. engineclock, memclock, vddc, activity, powertune);
  869. wlogprint("Fan autotune is %s (%d-%d)\n", ga->autofan ? "enabled" : "disabled",
  870. gpus[gpu].min_fan, gpus[gpu].gpu_fan);
  871. wlogprint("GPU engine clock autotune is %s (%d-%d)\n", ga->autoengine ? "enabled" : "disabled",
  872. ga->minspeed / 100, ga->maxspeed / 100);
  873. wlogprint("Change [A]utomatic [E]ngine [F]an [M]emory [V]oltage [P]owertune\n");
  874. wlogprint("Or press any other key to continue\n");
  875. input = getch();
  876. if (!strncasecmp(&input, "a", 1)) {
  877. change_autosettings(gpu);
  878. } else if (!strncasecmp(&input, "e", 1)) {
  879. get_enginerange(gpu, &imin, &imax);
  880. wlogprint("Enter GPU engine clock speed (%d - %d Mhz)", imin, imax);
  881. val = curses_int("");
  882. if (val < imin || val > imax) {
  883. wlogprint("Value is outside safe range, are you sure?\n");
  884. input = getch();
  885. if (strncasecmp(&input, "y", 1))
  886. return;
  887. }
  888. if (!set_engineclock(gpu, val))
  889. wlogprint("Driver reports success but check values below\n");
  890. else
  891. wlogprint("Failed to modify engine clock speed\n");
  892. } else if (!strncasecmp(&input, "f", 1)) {
  893. get_fanrange(gpu, &imin, &imax);
  894. wlogprint("Enter fan percentage (%d - %d %)", imin, imax);
  895. val = curses_int("");
  896. if (val < imin || val > imax) {
  897. wlogprint("Value is outside safe range, are you sure?\n");
  898. input = getch();
  899. if (strncasecmp(&input, "y", 1))
  900. return;
  901. }
  902. if (!set_fanspeed(gpu, val))
  903. wlogprint("Driver reports success but check values below\n");
  904. else
  905. wlogprint("Failed to modify fan speed\n");
  906. } else if (!strncasecmp(&input, "m", 1)) {
  907. get_memoryrange(gpu, &imin, &imax);
  908. wlogprint("Enter GPU memory clock speed (%d - %d Mhz)", imin, imax);
  909. val = curses_int("");
  910. if (val < imin || val > imax) {
  911. wlogprint("Value is outside safe range, are you sure?\n");
  912. input = getch();
  913. if (strncasecmp(&input, "y", 1))
  914. return;
  915. }
  916. if (!set_memoryclock(gpu, val))
  917. wlogprint("Driver reports success but check values below\n");
  918. else
  919. wlogprint("Failed to modify memory clock speed\n");
  920. } else if (!strncasecmp(&input, "v", 1)) {
  921. get_vddcrange(gpu, &fmin, &fmax);
  922. wlogprint("Enter GPU voltage (%.3f - %.3f V)", fmin, fmax);
  923. fval = curses_float("");
  924. if (fval < fmin || fval > fmax) {
  925. wlogprint("Value is outside safe range, are you sure?\n");
  926. input = getch();
  927. if (strncasecmp(&input, "y", 1))
  928. return;
  929. }
  930. if (!set_vddc(gpu, fval))
  931. wlogprint("Driver reports success but check values below\n");
  932. else
  933. wlogprint("Failed to modify voltage\n");
  934. } else if (!strncasecmp(&input, "p", 1)) {
  935. val = curses_int("Enter powertune value (-20 - 20)");
  936. if (val < -20 || val > 20) {
  937. wlogprint("Value is outside safe range, are you sure?\n");
  938. input = getch();
  939. if (strncasecmp(&input, "y", 1))
  940. return;
  941. }
  942. if (!set_powertune(gpu, val))
  943. wlogprint("Driver reports success but check values below\n");
  944. else
  945. wlogprint("Failed to modify powertune value\n");
  946. } else {
  947. clear_logwin();
  948. return;
  949. }
  950. sleep(1);
  951. goto updated;
  952. }
  953. void clear_adl(nDevs)
  954. {
  955. struct gpu_adl *ga;
  956. int i;
  957. if (!adl_active)
  958. return;
  959. lock_adl();
  960. /* Try to reset values to their defaults */
  961. for (i = 0; i < nDevs; i++) {
  962. ga = &gpus[i].adl;
  963. /* Only reset the values if we've changed them at any time */
  964. if (!gpus[i].has_adl || !ga->managed)
  965. continue;
  966. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  967. free(ga->DefPerfLev);
  968. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  969. ADL_Overdrive5_FanSpeedToDefault_Set(ga->iAdapterIndex, 0);
  970. }
  971. ADL_Main_Memory_Free ( (void **)&lpInfo );
  972. ADL_Main_Control_Destroy ();
  973. unlock_adl();
  974. #if defined (LINUX)
  975. dlclose(hDLL);
  976. #else
  977. FreeLibrary(hDLL);
  978. #endif
  979. }
  980. #endif /* HAVE_ADL */