adl.c 34 KB

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