adl.c 34 KB

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