adl.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. #include "miner.h"
  2. #include "adl.h"
  3. bool adl_active;
  4. #ifdef HAVE_ADL
  5. #if defined (__linux)
  6. #include "ADL_SDK/adl_sdk.h"
  7. #include <dlfcn.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #else
  11. #include <windows.h>
  12. #include <tchar.h>
  13. #include "ADL_SDK/adl_sdk.h"
  14. #endif
  15. #include <stdio.h>
  16. #include <curses.h>
  17. #include "adl_functions.h"
  18. int opt_hysteresis = 3;
  19. int opt_targettemp = 75;
  20. int opt_overheattemp = 85;
  21. static pthread_mutex_t adl_lock;
  22. // Memory allocation function
  23. static void * __stdcall ADL_Main_Memory_Alloc(int iSize)
  24. {
  25. void *lpBuffer = malloc(iSize);
  26. return lpBuffer;
  27. }
  28. // Optional Memory de-allocation function
  29. static void __stdcall ADL_Main_Memory_Free (void **lpBuffer)
  30. {
  31. if (*lpBuffer) {
  32. free (*lpBuffer);
  33. *lpBuffer = NULL;
  34. }
  35. }
  36. #if defined (LINUX)
  37. // equivalent functions in linux
  38. static void *GetProcAddress(void *pLibrary, const char *name)
  39. {
  40. return dlsym( pLibrary, name);
  41. }
  42. #endif
  43. static ADL_MAIN_CONTROL_CREATE ADL_Main_Control_Create;
  44. static ADL_MAIN_CONTROL_DESTROY ADL_Main_Control_Destroy;
  45. static ADL_ADAPTER_NUMBEROFADAPTERS_GET ADL_Adapter_NumberOfAdapters_Get;
  46. static ADL_ADAPTER_ADAPTERINFO_GET ADL_Adapter_AdapterInfo_Get;
  47. static ADL_ADAPTER_ID_GET ADL_Adapter_ID_Get;
  48. static ADL_OVERDRIVE5_TEMPERATURE_GET ADL_Overdrive5_Temperature_Get;
  49. static ADL_ADAPTER_ACTIVE_GET ADL_Adapter_Active_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_Adapter_Active_Get = (ADL_ADAPTER_ACTIVE_GET) GetProcAddress(hDLL, "ADL_Adapter_Active_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. if (!ADL_Main_Control_Create || !ADL_Main_Control_Destroy ||
  118. !ADL_Adapter_NumberOfAdapters_Get || !ADL_Adapter_AdapterInfo_Get ||
  119. !ADL_Adapter_ID_Get || !ADL_Overdrive5_Temperature_Get ||
  120. !ADL_Adapter_Active_Get || !ADL_Overdrive5_CurrentActivity_Get ||
  121. !ADL_Overdrive5_ODParameters_Get || !ADL_Overdrive5_FanSpeedInfo_Get ||
  122. !ADL_Overdrive5_FanSpeed_Get || !ADL_Overdrive5_FanSpeed_Set ||
  123. !ADL_Overdrive5_ODPerformanceLevels_Get || !ADL_Overdrive5_ODPerformanceLevels_Set ||
  124. !ADL_Main_Control_Refresh || !ADL_Overdrive5_PowerControl_Get ||
  125. !ADL_Overdrive5_PowerControl_Set || !ADL_Adapter_Speed_Get ||
  126. !ADL_Adapter_Speed_Set) {
  127. applog(LOG_WARNING, "ATI ADL's API is missing");
  128. return;
  129. }
  130. // Initialise ADL. The second parameter is 1, which means:
  131. // retrieve adapter information only for adapters that are physically present and enabled in the system
  132. if (ADL_Main_Control_Create (ADL_Main_Memory_Alloc, 1) != ADL_OK) {
  133. applog(LOG_INFO, "ADL Initialisation Error!");
  134. return ;
  135. }
  136. if (ADL_Main_Control_Refresh() != ADL_OK) {
  137. applog(LOG_INFO, "ADL Refresh Error!");
  138. return ;
  139. }
  140. // Obtain the number of adapters for the system
  141. if (ADL_Adapter_NumberOfAdapters_Get ( &iNumberAdapters ) != ADL_OK) {
  142. applog(LOG_INFO, "Cannot get the number of adapters!\n");
  143. return ;
  144. }
  145. if (iNumberAdapters > 0) {
  146. lpInfo = malloc ( sizeof (AdapterInfo) * iNumberAdapters );
  147. memset ( lpInfo,'\0', sizeof (AdapterInfo) * iNumberAdapters );
  148. // Get the AdapterInfo structure for all adapters in the system
  149. if (ADL_Adapter_AdapterInfo_Get (lpInfo, sizeof (AdapterInfo) * iNumberAdapters) != ADL_OK) {
  150. applog(LOG_INFO, "ADL_Adapter_AdapterInfo_Get Error!");
  151. return ;
  152. }
  153. } else {
  154. applog(LOG_INFO, "No adapters found");
  155. return;
  156. }
  157. for (i = 0; i < iNumberAdapters; i++) {
  158. struct gpu_adl *ga;
  159. int iAdapterIndex;
  160. int lpAdapterID;
  161. int lpStatus;
  162. ADLODPerformanceLevels *lpOdPerformanceLevels;
  163. int lev;
  164. iAdapterIndex = lpInfo[i].iAdapterIndex;
  165. /* Get unique identifier of the adapter */
  166. if (ADL_Adapter_ID_Get(iAdapterIndex, &lpAdapterID) != ADL_OK) {
  167. applog(LOG_INFO, "Failed to ADL_Adapter_ID_Get");
  168. continue;
  169. }
  170. if (!lpAdapterID)
  171. continue;
  172. if (lpAdapterID != last_adapter) {
  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. }
  184. /* See if the adapter is an AMD device with ADL active */
  185. if (ADL_Adapter_Active_Get(iAdapterIndex, &lpStatus) != ADL_OK) {
  186. applog(LOG_INFO, "Failed to ADL_Adapter_Active_Get");
  187. continue;
  188. }
  189. if (!lpStatus)
  190. continue;
  191. /* From here on we know this device is a discrete device and
  192. * should support ADL */
  193. ga = &gpus[gpu].adl;
  194. ga->iAdapterIndex = iAdapterIndex;
  195. ga->lpAdapterID = lpAdapterID;
  196. ga->lpStatus = lpStatus;
  197. ga->DefPerfLev = NULL;
  198. /* Save whatever the current speed setting is to restore on exit */
  199. if (ADL_Adapter_Speed_Get(iAdapterIndex, &ga->lpCurrent, &dummy) != ADL_OK) {
  200. applog(LOG_INFO, "Failed to ADL_Adapter_Speed_Get");
  201. continue;
  202. }
  203. /* Force the speed to high, whether it's ignored or not */
  204. ADL_Adapter_Speed_Set(iAdapterIndex, ADL_CONTEXT_SPEED_FORCEHIGH);
  205. if (ADL_Overdrive5_ODParameters_Get(iAdapterIndex, &ga->lpOdParameters) != ADL_OK) {
  206. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODParameters_Get");
  207. continue;
  208. }
  209. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  210. /* We're only interested in the top performance level */
  211. lpOdPerformanceLevels = malloc(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  212. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  213. /* Get default performance levels first */
  214. if (ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 1, lpOdPerformanceLevels) != ADL_OK) {
  215. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODPerformanceLevels_Get");
  216. continue;
  217. }
  218. /* Set the limits we'd use based on default gpu speeds */
  219. ga->maxspeed = ga->minspeed = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  220. /* Now get the current performance levels for any existing overclock */
  221. ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
  222. /* Save these values as the defaults in case we wish to reset to defaults */
  223. ga->DefPerfLev = lpOdPerformanceLevels;
  224. if (gpus[gpu].gpu_engine) {
  225. int setengine = gpus[gpu].gpu_engine * 100;
  226. /* Lower profiles can't have a higher setting */
  227. for (j = 0; j < lev; j++) {
  228. if (lpOdPerformanceLevels->aLevels[j].iEngineClock > setengine)
  229. lpOdPerformanceLevels->aLevels[j].iEngineClock = setengine;
  230. }
  231. lpOdPerformanceLevels->aLevels[lev].iEngineClock = setengine;
  232. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, gpus[gpu].gpu_engine);
  233. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  234. }
  235. if (gpus[gpu].gpu_memclock) {
  236. int setmem = gpus[gpu].gpu_memclock * 100;
  237. for (j = 0; j < lev; j++) {
  238. if (lpOdPerformanceLevels->aLevels[j].iMemoryClock > setmem)
  239. lpOdPerformanceLevels->aLevels[j].iMemoryClock = setmem;
  240. }
  241. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = setmem;
  242. applog(LOG_INFO, "Setting GPU %d memory clock to %d", gpu, gpus[gpu].gpu_memclock);
  243. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  244. }
  245. if (gpus[gpu].gpu_vddc) {
  246. int setv = gpus[gpu].gpu_vddc * 1000;
  247. for (j = 0; j < lev; j++) {
  248. if (lpOdPerformanceLevels->aLevels[j].iVddc > setv)
  249. lpOdPerformanceLevels->aLevels[j].iVddc = setv;
  250. }
  251. lpOdPerformanceLevels->aLevels[lev].iVddc = setv;
  252. applog(LOG_INFO, "Setting GPU %d voltage to %.3f", gpu, gpus[gpu].gpu_vddc);
  253. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  254. }
  255. ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
  256. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  257. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  258. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  259. if (ga->iEngineClock < ga->minspeed)
  260. ga->minspeed = ga->iEngineClock;
  261. if (ga->iEngineClock > ga->maxspeed)
  262. ga->maxspeed = ga->iEngineClock;
  263. if (ADL_Overdrive5_FanSpeedInfo_Get(iAdapterIndex, 0, &ga->lpFanSpeedInfo) != ADL_OK) {
  264. applog(LOG_INFO, "Failed to ADL_Overdrive5_FanSpeedInfo_Get");
  265. continue;
  266. }
  267. /* Save the fanspeed values as defaults in case we reset later */
  268. ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  269. if (gpus[gpu].gpu_fan) {
  270. ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  271. ga->lpFanSpeedValue.iFanSpeed = gpus[gpu].gpu_fan;
  272. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  273. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  274. applog(LOG_INFO, "Setting GPU %d fan speed to %d%%", gpu, gpus[gpu].gpu_fan);
  275. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  276. }
  277. /* Not fatal if powercontrol get fails */
  278. if (ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy) != ADL_OK)
  279. applog(LOG_INFO, "Failed to ADL_Overdrive5_PowerControl_get");
  280. if (gpus[gpu].gpu_powertune) {
  281. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, gpus[gpu].gpu_powertune);
  282. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  283. }
  284. /* Set some default temperatures for autotune when enabled */
  285. ga->targettemp = opt_targettemp;
  286. ga->overtemp = opt_overheattemp;
  287. if (opt_autofan) {
  288. ga->autofan = true;
  289. /* Set a safe starting default if we're automanaging fan speeds */
  290. set_fanspeed(gpu, 85);
  291. }
  292. if (opt_autoengine)
  293. ga->autoengine = true;
  294. gpus[gpu].has_adl = true;
  295. }
  296. adl_active = true;
  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 0;
  302. return (float)ga->lpTemperature.iTemperature / 1000;
  303. }
  304. float gpu_temp(int gpu)
  305. {
  306. struct gpu_adl *ga;
  307. float ret = 0;
  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 = 0;
  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 = 0;
  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 = 0;
  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 0;
  377. return ga->lpActivity.iActivityPercent;
  378. }
  379. int gpu_activity(int gpu)
  380. {
  381. struct gpu_adl *ga;
  382. int ret;
  383. if (!gpus[gpu].has_adl || !adl_active)
  384. return 0;
  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 0;
  391. if (!ga->lpOdParameters.iActivityReportingSupported)
  392. return 0;
  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 0;
  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 0;
  402. return ga->lpFanSpeedValue.iFanSpeed;
  403. }
  404. int gpu_fanspeed(int gpu)
  405. {
  406. struct gpu_adl *ga;
  407. int ret = 0;
  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 = 0;
  429. if (!gpus[gpu].has_adl || !adl_active)
  430. return -1;
  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 0;
  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. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  516. goto out;
  517. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  518. /* Reset to old value if it fails! */
  519. if (lpOdPerformanceLevels->aLevels[lev].iEngineClock != iEngineClock) {
  520. /* Set all the parameters in case they're linked somehow */
  521. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  522. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  523. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  524. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  525. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  526. goto out;
  527. }
  528. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  529. if (ga->iEngineClock > ga->maxspeed)
  530. ga->maxspeed = ga->iEngineClock;
  531. if (ga->iEngineClock < ga->minspeed)
  532. ga->minspeed = ga->iEngineClock;
  533. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  534. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  535. ret = 0;
  536. out:
  537. unlock_adl();
  538. return ret;
  539. }
  540. static void get_memoryrange(int gpu, int *imin, int *imax)
  541. {
  542. struct gpu_adl *ga;
  543. if (!gpus[gpu].has_adl || !adl_active) {
  544. wlogprint("Get memoryrange not supported\n");
  545. return;
  546. }
  547. ga = &gpus[gpu].adl;
  548. *imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
  549. *imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
  550. }
  551. static int set_memoryclock(int gpu, int iMemoryClock)
  552. {
  553. ADLODPerformanceLevels *lpOdPerformanceLevels;
  554. int i, lev, ret = 1;
  555. struct gpu_adl *ga;
  556. if (!gpus[gpu].has_adl || !adl_active) {
  557. wlogprint("Set memoryclock not supported\n");
  558. return ret;
  559. }
  560. iMemoryClock *= 100;
  561. ga = &gpus[gpu].adl;
  562. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  563. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  564. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  565. lock_adl();
  566. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  567. goto out;
  568. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
  569. for (i = 0; i < lev; i++) {
  570. if (lpOdPerformanceLevels->aLevels[i].iMemoryClock > iMemoryClock)
  571. lpOdPerformanceLevels->aLevels[i].iMemoryClock = iMemoryClock;
  572. }
  573. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  574. goto out;
  575. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  576. /* Reset to old value if it fails! */
  577. if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock != iMemoryClock) {
  578. /* Set all the parameters in case they're linked somehow */
  579. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iEngineClock;
  580. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  581. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  582. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  583. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  584. goto out;
  585. }
  586. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  587. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  588. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  589. ret = 0;
  590. out:
  591. unlock_adl();
  592. return ret;
  593. }
  594. static void get_vddcrange(int gpu, float *imin, float *imax)
  595. {
  596. struct gpu_adl *ga;
  597. if (!gpus[gpu].has_adl || !adl_active) {
  598. wlogprint("Get vddcrange not supported\n");
  599. return;
  600. }
  601. ga = &gpus[gpu].adl;
  602. *imin = (float)ga->lpOdParameters.sVddc.iMin / 1000;
  603. *imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
  604. }
  605. static float curses_float(const char *query)
  606. {
  607. float ret;
  608. char *cvar;
  609. cvar = curses_input(query);
  610. ret = atof(cvar);
  611. free(cvar);
  612. return ret;
  613. }
  614. static int set_vddc(int gpu, float fVddc)
  615. {
  616. ADLODPerformanceLevels *lpOdPerformanceLevels;
  617. int i, iVddc, lev, ret = 1;
  618. struct gpu_adl *ga;
  619. if (!gpus[gpu].has_adl || !adl_active) {
  620. wlogprint("Set vddc not supported\n");
  621. return ret;
  622. }
  623. iVddc = 1000 * fVddc;
  624. ga = &gpus[gpu].adl;
  625. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  626. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  627. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  628. lock_adl();
  629. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  630. goto out;
  631. for (i = 0; i < lev; i++) {
  632. if (lpOdPerformanceLevels->aLevels[i].iVddc > iVddc)
  633. lpOdPerformanceLevels->aLevels[i].iVddc = iVddc;
  634. }
  635. lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
  636. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  637. goto out;
  638. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  639. /* Reset to old value if it fails! */
  640. if (lpOdPerformanceLevels->aLevels[lev].iVddc != iVddc) {
  641. /* Set all the parameters in case they're linked somehow */
  642. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  643. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  644. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  645. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  646. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  647. goto out;
  648. }
  649. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  650. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  651. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  652. ret = 0;
  653. out:
  654. unlock_adl();
  655. return ret;
  656. }
  657. static void get_fanrange(int gpu, int *imin, int *imax)
  658. {
  659. struct gpu_adl *ga;
  660. if (!gpus[gpu].has_adl || !adl_active) {
  661. wlogprint("Get fanrange not supported\n");
  662. return;
  663. }
  664. ga = &gpus[gpu].adl;
  665. *imin = ga->lpFanSpeedInfo.iMinPercent;
  666. *imax = ga->lpFanSpeedInfo.iMaxPercent;
  667. }
  668. static int set_fanspeed(int gpu, int iFanSpeed)
  669. {
  670. struct gpu_adl *ga;
  671. int ret = 1;
  672. if (!gpus[gpu].has_adl || !adl_active) {
  673. wlogprint("Set fanspeed not supported\n");
  674. return ret;
  675. }
  676. ga = &gpus[gpu].adl;
  677. if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE )))
  678. return ret;
  679. lock_adl();
  680. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  681. goto out;
  682. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  683. if ((ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE) &&
  684. !(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE)) {
  685. /* Must convert speed to an RPM */
  686. iFanSpeed = ga->lpFanSpeedInfo.iMaxRPM * iFanSpeed / 100;
  687. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  688. } else
  689. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  690. ga->lpFanSpeedValue.iFanSpeed = iFanSpeed;
  691. if (ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  692. goto out;
  693. ret = 0;
  694. out:
  695. unlock_adl();
  696. return ret;
  697. }
  698. static int set_powertune(int gpu, int iPercentage)
  699. {
  700. int oldPercentage, dummy;
  701. struct gpu_adl *ga;
  702. int ret = 1;
  703. if (!gpus[gpu].has_adl || !adl_active) {
  704. wlogprint("Set powertune not supported\n");
  705. return ret;
  706. }
  707. ga = &gpus[gpu].adl;
  708. oldPercentage = ga->iPercentage;
  709. lock_adl();
  710. if (ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, iPercentage) != ADL_OK) {
  711. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, ga->iPercentage);
  712. goto out;
  713. }
  714. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  715. ret = 0;
  716. out:
  717. unlock_adl();
  718. return ret;
  719. }
  720. void gpu_autotune(int gpu)
  721. {
  722. int temp, fanpercent, engine, newpercent, newengine;
  723. bool fan_optimal = true;
  724. struct gpu_adl *ga;
  725. if (!gpus[gpu].has_adl || !adl_active)
  726. return;
  727. ga = &gpus[gpu].adl;
  728. lock_adl();
  729. temp = __gpu_temp(ga);
  730. newpercent = fanpercent = __gpu_fanpercent(ga);
  731. unlock_adl();
  732. newengine = engine = gpu_engineclock(gpu) * 100;
  733. if (temp && fanpercent >= 0 && ga->autofan) {
  734. if (temp > ga->overtemp && fanpercent < 100) {
  735. applog(LOG_WARNING, "Overheat detected, increasing fan to 100%");
  736. newpercent = 100;
  737. } else if (temp > ga->targettemp && fanpercent < 85) {
  738. if (opt_debug)
  739. applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
  740. newpercent = fanpercent + 10;
  741. if (newpercent > 85)
  742. newpercent = 85;
  743. } else if (fanpercent && temp < ga->targettemp - opt_hysteresis) {
  744. if (opt_debug)
  745. applog(LOG_DEBUG, "Temperature %d degrees below target, decreasing fanspeed", opt_hysteresis);
  746. newpercent = fanpercent - 1;
  747. }
  748. if (newpercent > 100)
  749. newpercent = 100;
  750. else if (newpercent < 0)
  751. newpercent = 0;
  752. if (newpercent != fanpercent) {
  753. fan_optimal = false;
  754. applog(LOG_INFO, "Setting GPU %d fan percentage to %d", gpu, newpercent);
  755. set_fanspeed(gpu, newpercent);
  756. }
  757. }
  758. if (engine && ga->autoengine) {
  759. if (temp > ga->overtemp && engine > ga->minspeed) {
  760. applog(LOG_WARNING, "Overheat detected, decreasing GPU clock speed");
  761. newengine = ga->minspeed;
  762. } else if (temp > ga->targettemp + opt_hysteresis && engine > ga->minspeed && fan_optimal) {
  763. if (opt_debug)
  764. applog(LOG_DEBUG, "Temperature %d degrees over target, decreasing clock speed", opt_hysteresis);
  765. newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
  766. } else if (temp < ga->targettemp && engine < ga->maxspeed) {
  767. if (opt_debug)
  768. applog(LOG_DEBUG, "Temperature below target, increasing clock speed");
  769. newengine = engine + ga->lpOdParameters.sEngineClock.iStep;
  770. }
  771. if (newengine > ga->maxspeed)
  772. newengine = ga->maxspeed;
  773. else if (newengine < ga->minspeed)
  774. newengine = ga->minspeed;
  775. if (newengine != engine) {
  776. newengine /= 100;
  777. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, newengine);
  778. set_engineclock(gpu, newengine);
  779. }
  780. }
  781. }
  782. void set_defaultfan(int gpu)
  783. {
  784. struct gpu_adl *ga;
  785. if (!gpus[gpu].has_adl || !adl_active)
  786. return;
  787. ga = &gpus[gpu].adl;
  788. lock_adl();
  789. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  790. unlock_adl();
  791. }
  792. void set_defaultengine(int gpu)
  793. {
  794. struct gpu_adl *ga;
  795. if (!gpus[gpu].has_adl || !adl_active)
  796. return;
  797. ga = &gpus[gpu].adl;
  798. lock_adl();
  799. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  800. unlock_adl();
  801. }
  802. void change_autosettings(int gpu)
  803. {
  804. struct gpu_adl *ga = &gpus[gpu].adl;
  805. char input;
  806. int val;
  807. wlogprint("Target temperature: %d\n", ga->targettemp);
  808. wlogprint("Overheat temperature: %d\n", ga->overtemp);
  809. wlogprint("Hysteresis differece: %d\n", opt_hysteresis);
  810. wlogprint("Toggle [F]an auto [G]PU auto\nChange [T]arget [O]verheat [H]ysteresis\n");
  811. wlogprint("Or press any other key to continue\n");
  812. input = getch();
  813. if (!strncasecmp(&input, "f", 1)) {
  814. ga->autofan ^= true;
  815. wlogprint("Fan autotune is now %s\n", ga->autofan ? "enabled" : "disabled");
  816. if (!ga->autofan) {
  817. wlogprint("Resetting fan to startup settings\n");
  818. set_defaultfan(gpu);
  819. }
  820. } else if (!strncasecmp(&input, "g", 1)) {
  821. ga->autoengine ^= true;
  822. wlogprint("GPU engine clock autotune is now %s\n", ga->autoengine ? "enabled" : "disabled");
  823. if (!ga->autoengine) {
  824. wlogprint("Resetting GPU engine clock to startup settings\n");
  825. set_defaultengine(gpu);
  826. }
  827. } else if (!strncasecmp(&input, "t", 1)) {
  828. val = curses_int("Enter target temperature for this GPU in °C (0-100)");
  829. if (val < 0 || val > 100)
  830. wlogprint("Invalid temperature");
  831. else
  832. ga->targettemp = val;
  833. } else if (!strncasecmp(&input, "o", 1)) {
  834. wlogprint("Enter oveheat temperature for this GPU in °C (%d-100)", ga->targettemp);
  835. val = curses_int("");
  836. if (val <= ga->targettemp || val > 100)
  837. wlogprint("Invalid temperature");
  838. else
  839. ga->overtemp = val;
  840. } else if (!strncasecmp(&input, "h", 1)) {
  841. val = curses_int("Enter hysteresis temperature difference (0-10)");
  842. if (val < 1 || val > 10)
  843. wlogprint("Invalid value");
  844. else
  845. opt_hysteresis = val;
  846. }
  847. }
  848. void change_gpusettings(int gpu)
  849. {
  850. struct gpu_adl *ga = &gpus[gpu].adl;
  851. float fval, fmin = 0, fmax = 0;
  852. int val, imin = 0, imax = 0;
  853. char input;
  854. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  855. float temp = 0, vddc = 0;
  856. updated:
  857. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune))
  858. wlogprint("Temp: %.1f °C\nFan Speed: %d%% (%d RPM)\nEngine Clock: %d MHz\n"
  859. "Memory Clock: %d Mhz\nVddc: %.3f V\nActivity: %d%%\nPowertune: %d%%\n",
  860. temp, fanpercent, fanspeed, engineclock, memclock, vddc, activity, powertune);
  861. wlogprint("Fan autotune is %s\n", ga->autofan ? "enabled" : "disabled");
  862. wlogprint("GPU engine clock autotune is %s\n", ga->autoengine ? "enabled" : "disabled");
  863. wlogprint("Change [A]utomatic [E]ngine [F]an [M]emory [V]oltage [P]owertune\n");
  864. wlogprint("Or press any other key to continue\n");
  865. input = getch();
  866. if (!strncasecmp(&input, "a", 1)) {
  867. change_autosettings(gpu);
  868. } else if (!strncasecmp(&input, "e", 1)) {
  869. get_enginerange(gpu, &imin, &imax);
  870. wlogprint("Enter GPU engine clock speed (%d - %d Mhz)", imin, imax);
  871. val = curses_int("");
  872. if (val < imin || val > imax) {
  873. wlogprint("Value is outside safe range, are you sure?\n");
  874. input = getch();
  875. if (strncasecmp(&input, "y", 1))
  876. return;
  877. }
  878. if (!set_engineclock(gpu, val))
  879. wlogprint("Driver reports success but check values below\n");
  880. else
  881. wlogprint("Failed to modify engine clock speed\n");
  882. } else if (!strncasecmp(&input, "f", 1)) {
  883. get_fanrange(gpu, &imin, &imax);
  884. wlogprint("Enter fan percentage (%d - %d %)", imin, imax);
  885. val = curses_int("");
  886. if (val < imin || val > imax) {
  887. wlogprint("Value is outside safe range, are you sure?\n");
  888. input = getch();
  889. if (strncasecmp(&input, "y", 1))
  890. return;
  891. }
  892. if (!set_fanspeed(gpu, val))
  893. wlogprint("Driver reports success but check values below\n");
  894. else
  895. wlogprint("Failed to modify fan speed\n");
  896. } else if (!strncasecmp(&input, "m", 1)) {
  897. get_memoryrange(gpu, &imin, &imax);
  898. wlogprint("Enter GPU memory clock speed (%d - %d Mhz)", imin, imax);
  899. val = curses_int("");
  900. if (val < imin || val > imax) {
  901. wlogprint("Value is outside safe range, are you sure?\n");
  902. input = getch();
  903. if (strncasecmp(&input, "y", 1))
  904. return;
  905. }
  906. if (!set_memoryclock(gpu, val))
  907. wlogprint("Driver reports success but check values below\n");
  908. else
  909. wlogprint("Failed to modify memory clock speed\n");
  910. } else if (!strncasecmp(&input, "v", 1)) {
  911. get_vddcrange(gpu, &fmin, &fmax);
  912. wlogprint("Enter GPU voltage (%.3f - %.3f V)", fmin, fmax);
  913. fval = curses_float("");
  914. if (fval < fmin || fval > fmax) {
  915. wlogprint("Value is outside safe range, are you sure?\n");
  916. input = getch();
  917. if (strncasecmp(&input, "y", 1))
  918. return;
  919. }
  920. if (!set_vddc(gpu, fval))
  921. wlogprint("Driver reports success but check values below\n");
  922. else
  923. wlogprint("Failed to modify voltage\n");
  924. } else if (!strncasecmp(&input, "p", 1)) {
  925. val = curses_int("Enter powertune value (-20 - 20)");
  926. if (val < -20 || val > 20) {
  927. wlogprint("Value is outside safe range, are you sure?\n");
  928. input = getch();
  929. if (strncasecmp(&input, "y", 1))
  930. return;
  931. }
  932. if (!set_powertune(gpu, val))
  933. wlogprint("Driver reports success but check values below\n");
  934. else
  935. wlogprint("Failed to modify powertune value\n");
  936. } else
  937. return;
  938. sleep(1);
  939. goto updated;
  940. }
  941. void clear_adl(nDevs)
  942. {
  943. struct gpu_adl *ga;
  944. int i;
  945. if (!adl_active)
  946. return;
  947. lock_adl();
  948. /* Try to reset values to their defaults */
  949. for (i = 0; i < nDevs; i++) {
  950. ga = &gpus[i].adl;
  951. if (!gpus[i].has_adl)
  952. continue;
  953. ADL_Adapter_Speed_Set(ga->iAdapterIndex, ga->lpCurrent);
  954. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  955. free(ga->DefPerfLev);
  956. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  957. }
  958. ADL_Main_Memory_Free ( (void **)&lpInfo );
  959. ADL_Main_Control_Destroy ();
  960. unlock_adl();
  961. #if defined (LINUX)
  962. dlclose(hDLL);
  963. #else
  964. FreeLibrary(hDLL);
  965. #endif
  966. }
  967. #endif /* HAVE_ADL */