adl.c 34 KB

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