adl.c 34 KB

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