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