adl.c 35 KB

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