adl.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. #include "config.h"
  2. #if defined(HAVE_ADL) && (defined(__linux) || defined (WIN32))
  3. #include <stdio.h>
  4. #include <curses.h>
  5. #include "miner.h"
  6. #include "ADL_SDK/adl_sdk.h"
  7. #if defined (__linux)
  8. #include <dlfcn.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #else /* WIN32 */
  12. #include <windows.h>
  13. #include <tchar.h>
  14. #define sleep(x) Sleep(x)
  15. #endif
  16. #include "adl_functions.h"
  17. bool adl_active;
  18. int opt_hysteresis = 3;
  19. const int opt_targettemp = 75;
  20. const int opt_overheattemp = 85;
  21. const int opt_cutofftemp = 95;
  22. static pthread_mutex_t adl_lock;
  23. // Memory allocation function
  24. static void * __stdcall ADL_Main_Memory_Alloc(int iSize)
  25. {
  26. void *lpBuffer = malloc(iSize);
  27. return lpBuffer;
  28. }
  29. // Optional Memory de-allocation function
  30. static void __stdcall ADL_Main_Memory_Free (void **lpBuffer)
  31. {
  32. if (*lpBuffer) {
  33. free (*lpBuffer);
  34. *lpBuffer = NULL;
  35. }
  36. }
  37. #if defined (LINUX)
  38. // equivalent functions in linux
  39. static void *GetProcAddress(void *pLibrary, const char *name)
  40. {
  41. return dlsym( pLibrary, name);
  42. }
  43. #endif
  44. static ADL_MAIN_CONTROL_CREATE ADL_Main_Control_Create;
  45. static ADL_MAIN_CONTROL_DESTROY ADL_Main_Control_Destroy;
  46. static ADL_ADAPTER_NUMBEROFADAPTERS_GET ADL_Adapter_NumberOfAdapters_Get;
  47. static ADL_ADAPTER_ADAPTERINFO_GET ADL_Adapter_AdapterInfo_Get;
  48. static ADL_ADAPTER_ID_GET ADL_Adapter_ID_Get;
  49. static ADL_OVERDRIVE5_TEMPERATURE_GET ADL_Overdrive5_Temperature_Get;
  50. static ADL_OVERDRIVE5_CURRENTACTIVITY_GET ADL_Overdrive5_CurrentActivity_Get;
  51. static ADL_OVERDRIVE5_ODPARAMETERS_GET ADL_Overdrive5_ODParameters_Get;
  52. static ADL_OVERDRIVE5_FANSPEEDINFO_GET ADL_Overdrive5_FanSpeedInfo_Get;
  53. static ADL_OVERDRIVE5_FANSPEED_GET ADL_Overdrive5_FanSpeed_Get;
  54. static ADL_OVERDRIVE5_FANSPEED_SET ADL_Overdrive5_FanSpeed_Set;
  55. static ADL_OVERDRIVE5_ODPERFORMANCELEVELS_GET ADL_Overdrive5_ODPerformanceLevels_Get;
  56. static ADL_OVERDRIVE5_ODPERFORMANCELEVELS_SET ADL_Overdrive5_ODPerformanceLevels_Set;
  57. static ADL_MAIN_CONTROL_REFRESH ADL_Main_Control_Refresh;
  58. static ADL_OVERDRIVE5_POWERCONTROL_GET ADL_Overdrive5_PowerControl_Get;
  59. static ADL_OVERDRIVE5_POWERCONTROL_SET ADL_Overdrive5_PowerControl_Set;
  60. static ADL_ADAPTER_SPEED_GET ADL_Adapter_Speed_Get;
  61. static ADL_ADAPTER_SPEED_SET ADL_Adapter_Speed_Set;
  62. #if defined (LINUX)
  63. static void *hDLL; // Handle to .so library
  64. #else
  65. HINSTANCE hDLL; // Handle to DLL
  66. #endif
  67. static int iNumberAdapters;
  68. static LPAdapterInfo lpInfo = NULL;
  69. static int set_fanspeed(int gpu, int iFanSpeed);
  70. static inline void lock_adl(void)
  71. {
  72. mutex_lock(&adl_lock);
  73. }
  74. static inline void unlock_adl(void)
  75. {
  76. mutex_unlock(&adl_lock);
  77. }
  78. void init_adl(int nDevs)
  79. {
  80. int i, j, devices = 0, last_adapter = -1, gpu = 0, dummy = 0;
  81. #if defined (LINUX)
  82. hDLL = dlopen( "libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
  83. #else
  84. hDLL = LoadLibrary("atiadlxx.dll");
  85. if (hDLL == NULL)
  86. // A 32 bit calling application on 64 bit OS will fail to LoadLIbrary.
  87. // Try to load the 32 bit library (atiadlxy.dll) instead
  88. hDLL = LoadLibrary("atiadlxy.dll");
  89. #endif
  90. if (hDLL == NULL) {
  91. applog(LOG_INFO, "Unable to load ati adl library");
  92. return;
  93. }
  94. if (unlikely(pthread_mutex_init(&adl_lock, NULL))) {
  95. applog(LOG_ERR, "Failed to init adl_lock in init_adl");
  96. return;
  97. }
  98. ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE) GetProcAddress(hDLL,"ADL_Main_Control_Create");
  99. ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY) GetProcAddress(hDLL,"ADL_Main_Control_Destroy");
  100. ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET) GetProcAddress(hDLL,"ADL_Adapter_NumberOfAdapters_Get");
  101. ADL_Adapter_AdapterInfo_Get = (ADL_ADAPTER_ADAPTERINFO_GET) GetProcAddress(hDLL,"ADL_Adapter_AdapterInfo_Get");
  102. ADL_Adapter_ID_Get = (ADL_ADAPTER_ID_GET) GetProcAddress(hDLL,"ADL_Adapter_ID_Get");
  103. ADL_Overdrive5_Temperature_Get = (ADL_OVERDRIVE5_TEMPERATURE_GET) GetProcAddress(hDLL,"ADL_Overdrive5_Temperature_Get");
  104. ADL_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_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. ADLODPerformanceLevels *lpOdPerformanceLevels;
  161. int lev;
  162. iAdapterIndex = lpInfo[i].iAdapterIndex;
  163. /* Get unique identifier of the adapter, 0 means not AMD */
  164. if (ADL_Adapter_ID_Get(iAdapterIndex, &lpAdapterID) != ADL_OK) {
  165. applog(LOG_INFO, "Failed to ADL_Adapter_ID_Get");
  166. continue;
  167. }
  168. if (!lpAdapterID)
  169. continue;
  170. /* Each adapter may have multiple entries */
  171. if (lpAdapterID == last_adapter)
  172. continue;
  173. /* We found a truly new adapter instead of a logical
  174. * one. Now since there's no way of correlating the
  175. * opencl enumerated devices and the ADL enumerated
  176. * ones, we have to assume they're in the same order.*/
  177. if (++devices > nDevs) {
  178. applog(LOG_ERR, "ADL found more devices than opencl");
  179. return;
  180. }
  181. gpu = devices - 1;
  182. last_adapter = lpAdapterID;
  183. /* From here on we know this device is a discrete device and
  184. * should support ADL */
  185. ga = &gpus[gpu].adl;
  186. ga->iAdapterIndex = iAdapterIndex;
  187. ga->lpAdapterID = lpAdapterID;
  188. ga->DefPerfLev = NULL;
  189. /* Save whatever the current speed setting is to restore on exit */
  190. if (ADL_Adapter_Speed_Get(iAdapterIndex, &ga->lpCurrent, &dummy) != ADL_OK) {
  191. applog(LOG_INFO, "Failed to ADL_Adapter_Speed_Get");
  192. continue;
  193. }
  194. /* Force the speed to high, whether it's ignored or not */
  195. ADL_Adapter_Speed_Set(iAdapterIndex, ADL_CONTEXT_SPEED_FORCEHIGH);
  196. if (ADL_Overdrive5_ODParameters_Get(iAdapterIndex, &ga->lpOdParameters) != ADL_OK) {
  197. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODParameters_Get");
  198. continue;
  199. }
  200. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  201. /* We're only interested in the top performance level */
  202. lpOdPerformanceLevels = malloc(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  203. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  204. /* Get default performance levels first */
  205. if (ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 1, lpOdPerformanceLevels) != ADL_OK) {
  206. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODPerformanceLevels_Get");
  207. continue;
  208. }
  209. /* Set the limits we'd use based on default gpu speeds */
  210. ga->maxspeed = ga->minspeed = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  211. /* Now get the current performance levels for any existing overclock */
  212. ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
  213. /* Save these values as the defaults in case we wish to reset to defaults */
  214. ga->DefPerfLev = lpOdPerformanceLevels;
  215. if (gpus[gpu].gpu_engine) {
  216. int setengine = gpus[gpu].gpu_engine * 100;
  217. /* Lower profiles can't have a higher setting */
  218. for (j = 0; j < lev; j++) {
  219. if (lpOdPerformanceLevels->aLevels[j].iEngineClock > setengine)
  220. lpOdPerformanceLevels->aLevels[j].iEngineClock = setengine;
  221. }
  222. lpOdPerformanceLevels->aLevels[lev].iEngineClock = setengine;
  223. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, gpus[gpu].gpu_engine);
  224. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  225. ga->maxspeed = setengine;
  226. if (gpus[gpu].min_engine)
  227. ga->minspeed = gpus[gpu].min_engine * 100;
  228. ga->managed = true;
  229. }
  230. if (gpus[gpu].gpu_memclock) {
  231. int setmem = gpus[gpu].gpu_memclock * 100;
  232. for (j = 0; j < lev; j++) {
  233. if (lpOdPerformanceLevels->aLevels[j].iMemoryClock > setmem)
  234. lpOdPerformanceLevels->aLevels[j].iMemoryClock = setmem;
  235. }
  236. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = setmem;
  237. applog(LOG_INFO, "Setting GPU %d memory clock to %d", gpu, gpus[gpu].gpu_memclock);
  238. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  239. ga->managed = true;
  240. }
  241. if (gpus[gpu].gpu_vddc) {
  242. int setv = gpus[gpu].gpu_vddc * 1000;
  243. for (j = 0; j < lev; j++) {
  244. if (lpOdPerformanceLevels->aLevels[j].iVddc > setv)
  245. lpOdPerformanceLevels->aLevels[j].iVddc = setv;
  246. }
  247. lpOdPerformanceLevels->aLevels[lev].iVddc = setv;
  248. applog(LOG_INFO, "Setting GPU %d voltage to %.3f", gpu, gpus[gpu].gpu_vddc);
  249. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  250. ga->managed = true;
  251. }
  252. ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
  253. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  254. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  255. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  256. if (ADL_Overdrive5_FanSpeedInfo_Get(iAdapterIndex, 0, &ga->lpFanSpeedInfo) != ADL_OK)
  257. applog(LOG_INFO, "Failed to ADL_Overdrive5_FanSpeedInfo_Get");
  258. /* Save the fanspeed values as defaults in case we reset later */
  259. ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  260. if (gpus[gpu].gpu_fan) {
  261. ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  262. ga->lpFanSpeedValue.iFanSpeed = gpus[gpu].gpu_fan;
  263. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  264. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  265. applog(LOG_INFO, "Setting GPU %d fan speed to %d%%", gpu, gpus[gpu].gpu_fan);
  266. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  267. ga->managed = true;
  268. } else
  269. gpus[gpu].gpu_fan = 85; /* Set a nominal upper limit of 85% */
  270. /* Not fatal if powercontrol get fails */
  271. if (ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy) != ADL_OK)
  272. applog(LOG_INFO, "Failed to ADL_Overdrive5_PowerControl_get");
  273. if (gpus[gpu].gpu_powertune) {
  274. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, gpus[gpu].gpu_powertune);
  275. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  276. ga->managed = true;
  277. }
  278. /* Set some default temperatures for autotune when enabled */
  279. if (!ga->targettemp)
  280. ga->targettemp = opt_targettemp;
  281. if (!ga->overtemp)
  282. ga->overtemp = opt_overheattemp;
  283. if (!ga->cutofftemp)
  284. ga->cutofftemp = opt_cutofftemp;
  285. if (opt_autofan) {
  286. ga->autofan = true;
  287. /* Set a safe starting default if we're automanaging fan speeds */
  288. set_fanspeed(gpu, 85);
  289. }
  290. if (opt_autoengine)
  291. ga->autoengine = true;
  292. gpus[gpu].has_adl = true;
  293. /* Flag adl as active if any card is successfully activated */
  294. adl_active = true;
  295. }
  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. ga->managed = true;
  535. ret = 0;
  536. out:
  537. unlock_adl();
  538. return ret;
  539. }
  540. static void get_memoryrange(int gpu, int *imin, int *imax)
  541. {
  542. struct gpu_adl *ga;
  543. if (!gpus[gpu].has_adl || !adl_active) {
  544. wlogprint("Get memoryrange not supported\n");
  545. return;
  546. }
  547. ga = &gpus[gpu].adl;
  548. *imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
  549. *imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
  550. }
  551. static int set_memoryclock(int gpu, int iMemoryClock)
  552. {
  553. ADLODPerformanceLevels *lpOdPerformanceLevels;
  554. int i, lev, ret = 1;
  555. struct gpu_adl *ga;
  556. if (!gpus[gpu].has_adl || !adl_active) {
  557. wlogprint("Set memoryclock not supported\n");
  558. return ret;
  559. }
  560. iMemoryClock *= 100;
  561. ga = &gpus[gpu].adl;
  562. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  563. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  564. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  565. lock_adl();
  566. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  567. goto out;
  568. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
  569. for (i = 0; i < lev; i++) {
  570. if (lpOdPerformanceLevels->aLevels[i].iMemoryClock > iMemoryClock)
  571. lpOdPerformanceLevels->aLevels[i].iMemoryClock = iMemoryClock;
  572. }
  573. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  574. goto out;
  575. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  576. /* Reset to old value if it fails! */
  577. if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock != iMemoryClock) {
  578. /* Set all the parameters in case they're linked somehow */
  579. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iEngineClock;
  580. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  581. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  582. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  583. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  584. goto out;
  585. }
  586. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  587. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  588. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  589. ga->managed = true;
  590. ret = 0;
  591. out:
  592. unlock_adl();
  593. return ret;
  594. }
  595. static void get_vddcrange(int gpu, float *imin, float *imax)
  596. {
  597. struct gpu_adl *ga;
  598. if (!gpus[gpu].has_adl || !adl_active) {
  599. wlogprint("Get vddcrange not supported\n");
  600. return;
  601. }
  602. ga = &gpus[gpu].adl;
  603. *imin = (float)ga->lpOdParameters.sVddc.iMin / 1000;
  604. *imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
  605. }
  606. static float curses_float(const char *query)
  607. {
  608. float ret;
  609. char *cvar;
  610. cvar = curses_input(query);
  611. ret = atof(cvar);
  612. free(cvar);
  613. return ret;
  614. }
  615. static int set_vddc(int gpu, float fVddc)
  616. {
  617. ADLODPerformanceLevels *lpOdPerformanceLevels;
  618. int i, iVddc, lev, ret = 1;
  619. struct gpu_adl *ga;
  620. if (!gpus[gpu].has_adl || !adl_active) {
  621. wlogprint("Set vddc not supported\n");
  622. return ret;
  623. }
  624. iVddc = 1000 * fVddc;
  625. ga = &gpus[gpu].adl;
  626. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  627. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  628. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  629. lock_adl();
  630. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  631. goto out;
  632. for (i = 0; i < lev; i++) {
  633. if (lpOdPerformanceLevels->aLevels[i].iVddc > iVddc)
  634. lpOdPerformanceLevels->aLevels[i].iVddc = iVddc;
  635. }
  636. lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
  637. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  638. goto out;
  639. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  640. /* Reset to old value if it fails! */
  641. if (lpOdPerformanceLevels->aLevels[lev].iVddc != iVddc) {
  642. /* Set all the parameters in case they're linked somehow */
  643. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  644. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  645. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  646. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  647. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  648. goto out;
  649. }
  650. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  651. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  652. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  653. ga->managed = true;
  654. ret = 0;
  655. out:
  656. unlock_adl();
  657. return ret;
  658. }
  659. static void get_fanrange(int gpu, int *imin, int *imax)
  660. {
  661. struct gpu_adl *ga;
  662. if (!gpus[gpu].has_adl || !adl_active) {
  663. wlogprint("Get fanrange not supported\n");
  664. return;
  665. }
  666. ga = &gpus[gpu].adl;
  667. *imin = ga->lpFanSpeedInfo.iMinPercent;
  668. *imax = ga->lpFanSpeedInfo.iMaxPercent;
  669. }
  670. static int set_fanspeed(int gpu, int iFanSpeed)
  671. {
  672. struct gpu_adl *ga;
  673. int ret = 1;
  674. if (!gpus[gpu].has_adl || !adl_active) {
  675. wlogprint("Set fanspeed not supported\n");
  676. return ret;
  677. }
  678. ga = &gpus[gpu].adl;
  679. if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE )))
  680. return ret;
  681. lock_adl();
  682. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  683. goto out;
  684. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  685. if ((ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE) &&
  686. !(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE)) {
  687. /* Must convert speed to an RPM */
  688. iFanSpeed = ga->lpFanSpeedInfo.iMaxRPM * iFanSpeed / 100;
  689. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  690. } else
  691. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  692. ga->lpFanSpeedValue.iFanSpeed = iFanSpeed;
  693. if (ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  694. goto out;
  695. ga->managed = true;
  696. ret = 0;
  697. out:
  698. unlock_adl();
  699. return ret;
  700. }
  701. static int set_powertune(int gpu, int iPercentage)
  702. {
  703. int oldPercentage, dummy;
  704. struct gpu_adl *ga;
  705. int ret = 1;
  706. if (!gpus[gpu].has_adl || !adl_active) {
  707. wlogprint("Set powertune not supported\n");
  708. return ret;
  709. }
  710. ga = &gpus[gpu].adl;
  711. oldPercentage = ga->iPercentage;
  712. lock_adl();
  713. if (ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, iPercentage) != ADL_OK) {
  714. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, ga->iPercentage);
  715. goto out;
  716. }
  717. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  718. ga->managed = true;
  719. ret = 0;
  720. out:
  721. unlock_adl();
  722. return ret;
  723. }
  724. void gpu_autotune(int gpu, bool *enable)
  725. {
  726. int temp, fanpercent, engine, newpercent, newengine;
  727. bool fan_optimal = true;
  728. struct gpu_adl *ga;
  729. ga = &gpus[gpu].adl;
  730. lock_adl();
  731. temp = __gpu_temp(ga);
  732. newpercent = fanpercent = __gpu_fanpercent(ga);
  733. unlock_adl();
  734. newengine = engine = gpu_engineclock(gpu) * 100;
  735. if (temp && fanpercent >= 0 && ga->autofan) {
  736. int top = gpus[gpu].gpu_fan;
  737. int bot = gpus[gpu].min_fan;
  738. if (temp > ga->overtemp && fanpercent < 100) {
  739. applog(LOG_WARNING, "Overheat detected on GPU %d, increasing fan to 100%", gpu);
  740. newpercent = 100;
  741. } else if (temp > ga->targettemp && fanpercent < top) {
  742. if (opt_debug)
  743. applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
  744. if (temp > ga->targettemp + opt_hysteresis)
  745. newpercent = fanpercent + 10;
  746. else
  747. newpercent = fanpercent + 5;
  748. if (newpercent > top)
  749. newpercent = top;
  750. } else if (fanpercent > bot && temp < ga->targettemp - opt_hysteresis) {
  751. if (opt_debug)
  752. applog(LOG_DEBUG, "Temperature %d degrees below target, decreasing fanspeed", opt_hysteresis);
  753. newpercent = fanpercent - 1;
  754. }
  755. if (newpercent > 100)
  756. newpercent = 100;
  757. else if (newpercent < 0)
  758. newpercent = 0;
  759. if (newpercent != fanpercent) {
  760. fan_optimal = false;
  761. applog(LOG_INFO, "Setting GPU %d fan percentage to %d", gpu, newpercent);
  762. set_fanspeed(gpu, newpercent);
  763. }
  764. }
  765. if (engine && ga->autoengine) {
  766. if (temp > ga->cutofftemp) {
  767. applog(LOG_WARNING, "Hit thermal cutoff limit on GPU %d, disabling!", gpu);
  768. *enable = false;
  769. newengine = ga->minspeed;
  770. } else if (temp > ga->overtemp && engine > ga->minspeed) {
  771. applog(LOG_WARNING, "Overheat detected, decreasing GPU %d clock speed", gpu);
  772. newengine = ga->minspeed;
  773. } else if (temp > ga->targettemp + opt_hysteresis && engine > ga->minspeed && fan_optimal) {
  774. if (opt_debug)
  775. applog(LOG_DEBUG, "Temperature %d degrees over target, decreasing clock speed", opt_hysteresis);
  776. newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
  777. } else if (temp < ga->targettemp && engine < ga->maxspeed) {
  778. if (opt_debug)
  779. applog(LOG_DEBUG, "Temperature below target, increasing clock speed");
  780. newengine = engine + ga->lpOdParameters.sEngineClock.iStep;
  781. }
  782. if (newengine > ga->maxspeed)
  783. newengine = ga->maxspeed;
  784. else if (newengine < ga->minspeed)
  785. newengine = ga->minspeed;
  786. if (newengine != engine) {
  787. newengine /= 100;
  788. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, newengine);
  789. set_engineclock(gpu, newengine);
  790. }
  791. }
  792. }
  793. void set_defaultfan(int gpu)
  794. {
  795. struct gpu_adl *ga;
  796. if (!gpus[gpu].has_adl || !adl_active)
  797. return;
  798. ga = &gpus[gpu].adl;
  799. lock_adl();
  800. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  801. unlock_adl();
  802. }
  803. void set_defaultengine(int gpu)
  804. {
  805. struct gpu_adl *ga;
  806. if (!gpus[gpu].has_adl || !adl_active)
  807. return;
  808. ga = &gpus[gpu].adl;
  809. lock_adl();
  810. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  811. unlock_adl();
  812. }
  813. void change_autosettings(int gpu)
  814. {
  815. struct gpu_adl *ga = &gpus[gpu].adl;
  816. char input;
  817. int val;
  818. wlogprint("Target temperature: %d\n", ga->targettemp);
  819. wlogprint("Overheat temperature: %d\n", ga->overtemp);
  820. wlogprint("Cutoff temperature: %d\n", ga->cutofftemp);
  821. wlogprint("Hysteresis differece: %d\n", opt_hysteresis);
  822. wlogprint("Toggle [F]an auto [G]PU auto\nChange [T]arget [O]verheat [C]utoff [H]ysteresis\n");
  823. wlogprint("Or press any other key to continue\n");
  824. input = getch();
  825. if (!strncasecmp(&input, "f", 1)) {
  826. ga->autofan ^= true;
  827. wlogprint("Fan autotune is now %s\n", ga->autofan ? "enabled" : "disabled");
  828. if (!ga->autofan) {
  829. wlogprint("Resetting fan to startup settings\n");
  830. set_defaultfan(gpu);
  831. }
  832. } else if (!strncasecmp(&input, "g", 1)) {
  833. ga->autoengine ^= true;
  834. wlogprint("GPU engine clock autotune is now %s\n", ga->autoengine ? "enabled" : "disabled");
  835. if (!ga->autoengine) {
  836. wlogprint("Resetting GPU engine clock to startup settings\n");
  837. set_defaultengine(gpu);
  838. }
  839. } else if (!strncasecmp(&input, "t", 1)) {
  840. val = curses_int("Enter target temperature for this GPU in C (0-200)");
  841. if (val < 0 || val > 200)
  842. wlogprint("Invalid temperature");
  843. else
  844. ga->targettemp = val;
  845. } else if (!strncasecmp(&input, "o", 1)) {
  846. wlogprint("Enter overheat temperature for this GPU in C (%d+)", ga->targettemp);
  847. val = curses_int("");
  848. if (val <= ga->targettemp || val > 200)
  849. wlogprint("Invalid temperature");
  850. else
  851. ga->overtemp = val;
  852. } else if (!strncasecmp(&input, "c", 1)) {
  853. wlogprint("Enter cutoff temperature for this GPU in C (%d+)", ga->overtemp);
  854. val = curses_int("");
  855. if (val <= ga->overtemp || val > 200)
  856. wlogprint("Invalid temperature");
  857. else
  858. ga->cutofftemp = val;
  859. } else if (!strncasecmp(&input, "h", 1)) {
  860. val = curses_int("Enter hysteresis temperature difference (1-10)");
  861. if (val < 1 || val > 10)
  862. wlogprint("Invalid value");
  863. else
  864. opt_hysteresis = val;
  865. }
  866. }
  867. void change_gpusettings(int gpu)
  868. {
  869. struct gpu_adl *ga = &gpus[gpu].adl;
  870. float fval, fmin = 0, fmax = 0;
  871. int val, imin = 0, imax = 0;
  872. char input;
  873. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  874. float temp = 0, vddc = 0;
  875. updated:
  876. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune))
  877. wlogprint("Temp: %.1f C\nFan Speed: %d%% (%d RPM)\nEngine Clock: %d MHz\n"
  878. "Memory Clock: %d Mhz\nVddc: %.3f V\nActivity: %d%%\nPowertune: %d%%\n",
  879. temp, fanpercent, fanspeed, engineclock, memclock, vddc, activity, powertune);
  880. wlogprint("Fan autotune is %s\n", ga->autofan ? "enabled" : "disabled");
  881. wlogprint("GPU engine clock autotune is %s (%d-%d)\n", ga->autoengine ? "enabled" : "disabled",
  882. ga->minspeed / 100, ga->maxspeed / 100);
  883. wlogprint("Change [A]utomatic [E]ngine [F]an [M]emory [V]oltage [P]owertune\n");
  884. wlogprint("Or press any other key to continue\n");
  885. input = getch();
  886. if (!strncasecmp(&input, "a", 1)) {
  887. change_autosettings(gpu);
  888. } else if (!strncasecmp(&input, "e", 1)) {
  889. get_enginerange(gpu, &imin, &imax);
  890. wlogprint("Enter GPU engine clock speed (%d - %d Mhz)", imin, imax);
  891. val = curses_int("");
  892. if (val < imin || val > imax) {
  893. wlogprint("Value is outside safe range, are you sure?\n");
  894. input = getch();
  895. if (strncasecmp(&input, "y", 1))
  896. return;
  897. }
  898. if (!set_engineclock(gpu, val))
  899. wlogprint("Driver reports success but check values below\n");
  900. else
  901. wlogprint("Failed to modify engine clock speed\n");
  902. } else if (!strncasecmp(&input, "f", 1)) {
  903. get_fanrange(gpu, &imin, &imax);
  904. wlogprint("Enter fan percentage (%d - %d %)", imin, imax);
  905. val = curses_int("");
  906. if (val < imin || val > imax) {
  907. wlogprint("Value is outside safe range, are you sure?\n");
  908. input = getch();
  909. if (strncasecmp(&input, "y", 1))
  910. return;
  911. }
  912. if (!set_fanspeed(gpu, val))
  913. wlogprint("Driver reports success but check values below\n");
  914. else
  915. wlogprint("Failed to modify fan speed\n");
  916. } else if (!strncasecmp(&input, "m", 1)) {
  917. get_memoryrange(gpu, &imin, &imax);
  918. wlogprint("Enter GPU memory clock speed (%d - %d Mhz)", imin, imax);
  919. val = curses_int("");
  920. if (val < imin || val > imax) {
  921. wlogprint("Value is outside safe range, are you sure?\n");
  922. input = getch();
  923. if (strncasecmp(&input, "y", 1))
  924. return;
  925. }
  926. if (!set_memoryclock(gpu, val))
  927. wlogprint("Driver reports success but check values below\n");
  928. else
  929. wlogprint("Failed to modify memory clock speed\n");
  930. } else if (!strncasecmp(&input, "v", 1)) {
  931. get_vddcrange(gpu, &fmin, &fmax);
  932. wlogprint("Enter GPU voltage (%.3f - %.3f V)", fmin, fmax);
  933. fval = curses_float("");
  934. if (fval < fmin || fval > fmax) {
  935. wlogprint("Value is outside safe range, are you sure?\n");
  936. input = getch();
  937. if (strncasecmp(&input, "y", 1))
  938. return;
  939. }
  940. if (!set_vddc(gpu, fval))
  941. wlogprint("Driver reports success but check values below\n");
  942. else
  943. wlogprint("Failed to modify voltage\n");
  944. } else if (!strncasecmp(&input, "p", 1)) {
  945. val = curses_int("Enter powertune value (-20 - 20)");
  946. if (val < -20 || val > 20) {
  947. wlogprint("Value is outside safe range, are you sure?\n");
  948. input = getch();
  949. if (strncasecmp(&input, "y", 1))
  950. return;
  951. }
  952. if (!set_powertune(gpu, val))
  953. wlogprint("Driver reports success but check values below\n");
  954. else
  955. wlogprint("Failed to modify powertune value\n");
  956. } else
  957. return;
  958. sleep(1);
  959. goto updated;
  960. }
  961. void clear_adl(nDevs)
  962. {
  963. struct gpu_adl *ga;
  964. int i;
  965. if (!adl_active)
  966. return;
  967. lock_adl();
  968. /* Try to reset values to their defaults */
  969. for (i = 0; i < nDevs; i++) {
  970. ga = &gpus[i].adl;
  971. /* Only reset the values if we've changed them at any time */
  972. if (!gpus[i].has_adl || !ga->managed)
  973. continue;
  974. ADL_Adapter_Speed_Set(ga->iAdapterIndex, ga->lpCurrent);
  975. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  976. free(ga->DefPerfLev);
  977. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  978. }
  979. ADL_Main_Memory_Free ( (void **)&lpInfo );
  980. ADL_Main_Control_Destroy ();
  981. unlock_adl();
  982. #if defined (LINUX)
  983. dlclose(hDLL);
  984. #else
  985. FreeLibrary(hDLL);
  986. #endif
  987. }
  988. #endif /* HAVE_ADL */