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