adl.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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. int opt_targettemp = 75;
  20. int opt_overheattemp = 85;
  21. 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. ga->targettemp = opt_targettemp;
  280. ga->overtemp = opt_overheattemp;
  281. ga->cutofftemp = opt_cutofftemp;
  282. if (opt_autofan) {
  283. ga->autofan = true;
  284. /* Set a safe starting default if we're automanaging fan speeds */
  285. set_fanspeed(gpu, 85);
  286. }
  287. if (opt_autoengine)
  288. ga->autoengine = true;
  289. gpus[gpu].has_adl = true;
  290. /* Flag adl as active if any card is successfully activated */
  291. adl_active = true;
  292. }
  293. }
  294. static inline float __gpu_temp(struct gpu_adl *ga)
  295. {
  296. if (ADL_Overdrive5_Temperature_Get(ga->iAdapterIndex, 0, &ga->lpTemperature) != ADL_OK)
  297. return 0;
  298. return (float)ga->lpTemperature.iTemperature / 1000;
  299. }
  300. float gpu_temp(int gpu)
  301. {
  302. struct gpu_adl *ga;
  303. float ret = 0;
  304. if (!gpus[gpu].has_adl || !adl_active)
  305. return ret;
  306. ga = &gpus[gpu].adl;
  307. lock_adl();
  308. ret = __gpu_temp(ga);
  309. unlock_adl();
  310. return ret;
  311. }
  312. static inline int __gpu_engineclock(struct gpu_adl *ga)
  313. {
  314. return ga->lpActivity.iEngineClock / 100;
  315. }
  316. int gpu_engineclock(int gpu)
  317. {
  318. struct gpu_adl *ga;
  319. int ret = 0;
  320. if (!gpus[gpu].has_adl || !adl_active)
  321. return ret;
  322. ga = &gpus[gpu].adl;
  323. lock_adl();
  324. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  325. goto out;
  326. ret = __gpu_engineclock(ga);
  327. out:
  328. unlock_adl();
  329. return ret;
  330. }
  331. static inline int __gpu_memclock(struct gpu_adl *ga)
  332. {
  333. return ga->lpActivity.iMemoryClock / 100;
  334. }
  335. int gpu_memclock(int gpu)
  336. {
  337. struct gpu_adl *ga;
  338. int ret = 0;
  339. if (!gpus[gpu].has_adl || !adl_active)
  340. return ret;
  341. ga = &gpus[gpu].adl;
  342. lock_adl();
  343. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  344. goto out;
  345. ret = __gpu_memclock(ga);
  346. out:
  347. unlock_adl();
  348. return ret;
  349. }
  350. static inline float __gpu_vddc(struct gpu_adl *ga)
  351. {
  352. return (float)ga->lpActivity.iVddc / 1000;
  353. }
  354. float gpu_vddc(int gpu)
  355. {
  356. struct gpu_adl *ga;
  357. float ret = 0;
  358. if (!gpus[gpu].has_adl || !adl_active)
  359. return ret;
  360. ga = &gpus[gpu].adl;
  361. lock_adl();
  362. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  363. goto out;
  364. ret = __gpu_vddc(ga);
  365. out:
  366. unlock_adl();
  367. return ret;
  368. }
  369. static inline int __gpu_activity(struct gpu_adl *ga)
  370. {
  371. if (!ga->lpOdParameters.iActivityReportingSupported)
  372. return 0;
  373. return ga->lpActivity.iActivityPercent;
  374. }
  375. int gpu_activity(int gpu)
  376. {
  377. struct gpu_adl *ga;
  378. int ret;
  379. if (!gpus[gpu].has_adl || !adl_active)
  380. return 0;
  381. ga = &gpus[gpu].adl;
  382. lock_adl();
  383. ret = ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity);
  384. unlock_adl();
  385. if (ret != ADL_OK)
  386. return 0;
  387. if (!ga->lpOdParameters.iActivityReportingSupported)
  388. return 0;
  389. return ga->lpActivity.iActivityPercent;
  390. }
  391. static inline int __gpu_fanspeed(struct gpu_adl *ga)
  392. {
  393. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_READ))
  394. return 0;
  395. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  396. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  397. return 0;
  398. return ga->lpFanSpeedValue.iFanSpeed;
  399. }
  400. int gpu_fanspeed(int gpu)
  401. {
  402. struct gpu_adl *ga;
  403. int ret = 0;
  404. if (!gpus[gpu].has_adl || !adl_active)
  405. return ret;
  406. ga = &gpus[gpu].adl;
  407. lock_adl();
  408. ret = __gpu_fanspeed(ga);
  409. unlock_adl();
  410. return ret;
  411. }
  412. static inline int __gpu_fanpercent(struct gpu_adl *ga)
  413. {
  414. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ ))
  415. return -1;
  416. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  417. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  418. return -1;
  419. return ga->lpFanSpeedValue.iFanSpeed;
  420. }
  421. int gpu_fanpercent(int gpu)
  422. {
  423. struct gpu_adl *ga;
  424. int ret = 0;
  425. if (!gpus[gpu].has_adl || !adl_active)
  426. return -1;
  427. ga = &gpus[gpu].adl;
  428. lock_adl();
  429. ret = __gpu_fanpercent(ga);
  430. unlock_adl();
  431. return ret;
  432. }
  433. static inline int __gpu_powertune(struct gpu_adl *ga)
  434. {
  435. int dummy = 0;
  436. if (ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy) != ADL_OK)
  437. return 0;
  438. return ga->iPercentage;
  439. }
  440. int gpu_powertune(int gpu)
  441. {
  442. struct gpu_adl *ga;
  443. int ret = -1;
  444. if (!gpus[gpu].has_adl || !adl_active)
  445. return ret;
  446. ga = &gpus[gpu].adl;
  447. lock_adl();
  448. ret = __gpu_powertune(ga);
  449. unlock_adl();
  450. return ret;
  451. }
  452. bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc,
  453. int *activity, int *fanspeed, int *fanpercent, int *powertune)
  454. {
  455. struct gpu_adl *ga;
  456. if (!gpus[gpu].has_adl || !adl_active)
  457. return false;
  458. ga = &gpus[gpu].adl;
  459. lock_adl();
  460. *temp = __gpu_temp(ga);
  461. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK) {
  462. *engineclock = 0;
  463. *memclock = 0;
  464. *vddc = 0;
  465. *activity = 0;
  466. } else {
  467. *engineclock = __gpu_engineclock(ga);
  468. *memclock = __gpu_memclock(ga);
  469. *vddc = __gpu_vddc(ga);
  470. *activity = __gpu_activity(ga);
  471. }
  472. *fanspeed = __gpu_fanspeed(ga);
  473. *fanpercent = __gpu_fanpercent(ga);
  474. *powertune = __gpu_powertune(ga);
  475. unlock_adl();
  476. return true;
  477. }
  478. static void get_enginerange(int gpu, int *imin, int *imax)
  479. {
  480. struct gpu_adl *ga;
  481. if (!gpus[gpu].has_adl || !adl_active) {
  482. wlogprint("Get enginerange not supported\n");
  483. return;
  484. }
  485. ga = &gpus[gpu].adl;
  486. *imin = ga->lpOdParameters.sEngineClock.iMin / 100;
  487. *imax = ga->lpOdParameters.sEngineClock.iMax / 100;
  488. }
  489. static int set_engineclock(int gpu, int iEngineClock)
  490. {
  491. ADLODPerformanceLevels *lpOdPerformanceLevels;
  492. int i, lev, ret = 1;
  493. struct gpu_adl *ga;
  494. if (!gpus[gpu].has_adl || !adl_active) {
  495. wlogprint("Set engineclock not supported\n");
  496. return ret;
  497. }
  498. iEngineClock *= 100;
  499. ga = &gpus[gpu].adl;
  500. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  501. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  502. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  503. lock_adl();
  504. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  505. goto out;
  506. for (i = 0; i < lev; i++) {
  507. if (lpOdPerformanceLevels->aLevels[i].iEngineClock > iEngineClock)
  508. lpOdPerformanceLevels->aLevels[i].iEngineClock = iEngineClock;
  509. }
  510. lpOdPerformanceLevels->aLevels[lev].iEngineClock = iEngineClock;
  511. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  512. goto out;
  513. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  514. /* Reset to old value if it fails! */
  515. if (lpOdPerformanceLevels->aLevels[lev].iEngineClock != iEngineClock) {
  516. /* Set all the parameters in case they're linked somehow */
  517. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  518. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  519. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  520. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  521. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  522. goto out;
  523. }
  524. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  525. if (ga->iEngineClock > ga->maxspeed)
  526. ga->maxspeed = ga->iEngineClock;
  527. if (ga->iEngineClock < ga->minspeed)
  528. ga->minspeed = ga->iEngineClock;
  529. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  530. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  531. ga->managed = true;
  532. ret = 0;
  533. out:
  534. unlock_adl();
  535. return ret;
  536. }
  537. static void get_memoryrange(int gpu, int *imin, int *imax)
  538. {
  539. struct gpu_adl *ga;
  540. if (!gpus[gpu].has_adl || !adl_active) {
  541. wlogprint("Get memoryrange not supported\n");
  542. return;
  543. }
  544. ga = &gpus[gpu].adl;
  545. *imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
  546. *imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
  547. }
  548. static int set_memoryclock(int gpu, int iMemoryClock)
  549. {
  550. ADLODPerformanceLevels *lpOdPerformanceLevels;
  551. int i, lev, ret = 1;
  552. struct gpu_adl *ga;
  553. if (!gpus[gpu].has_adl || !adl_active) {
  554. wlogprint("Set memoryclock not supported\n");
  555. return ret;
  556. }
  557. iMemoryClock *= 100;
  558. ga = &gpus[gpu].adl;
  559. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  560. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  561. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  562. lock_adl();
  563. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  564. goto out;
  565. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
  566. for (i = 0; i < lev; i++) {
  567. if (lpOdPerformanceLevels->aLevels[i].iMemoryClock > iMemoryClock)
  568. lpOdPerformanceLevels->aLevels[i].iMemoryClock = iMemoryClock;
  569. }
  570. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  571. goto out;
  572. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  573. /* Reset to old value if it fails! */
  574. if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock != iMemoryClock) {
  575. /* Set all the parameters in case they're linked somehow */
  576. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iEngineClock;
  577. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  578. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  579. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  580. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  581. goto out;
  582. }
  583. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  584. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  585. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  586. ga->managed = true;
  587. ret = 0;
  588. out:
  589. unlock_adl();
  590. return ret;
  591. }
  592. static void get_vddcrange(int gpu, float *imin, float *imax)
  593. {
  594. struct gpu_adl *ga;
  595. if (!gpus[gpu].has_adl || !adl_active) {
  596. wlogprint("Get vddcrange not supported\n");
  597. return;
  598. }
  599. ga = &gpus[gpu].adl;
  600. *imin = (float)ga->lpOdParameters.sVddc.iMin / 1000;
  601. *imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
  602. }
  603. static float curses_float(const char *query)
  604. {
  605. float ret;
  606. char *cvar;
  607. cvar = curses_input(query);
  608. ret = atof(cvar);
  609. free(cvar);
  610. return ret;
  611. }
  612. static int set_vddc(int gpu, float fVddc)
  613. {
  614. ADLODPerformanceLevels *lpOdPerformanceLevels;
  615. int i, iVddc, lev, ret = 1;
  616. struct gpu_adl *ga;
  617. if (!gpus[gpu].has_adl || !adl_active) {
  618. wlogprint("Set vddc not supported\n");
  619. return ret;
  620. }
  621. iVddc = 1000 * fVddc;
  622. ga = &gpus[gpu].adl;
  623. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  624. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  625. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  626. lock_adl();
  627. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  628. goto out;
  629. for (i = 0; i < lev; i++) {
  630. if (lpOdPerformanceLevels->aLevels[i].iVddc > iVddc)
  631. lpOdPerformanceLevels->aLevels[i].iVddc = iVddc;
  632. }
  633. lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
  634. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  635. goto out;
  636. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  637. /* Reset to old value if it fails! */
  638. if (lpOdPerformanceLevels->aLevels[lev].iVddc != iVddc) {
  639. /* Set all the parameters in case they're linked somehow */
  640. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  641. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  642. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  643. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  644. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  645. goto out;
  646. }
  647. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  648. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  649. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  650. ga->managed = true;
  651. ret = 0;
  652. out:
  653. unlock_adl();
  654. return ret;
  655. }
  656. static void get_fanrange(int gpu, int *imin, int *imax)
  657. {
  658. struct gpu_adl *ga;
  659. if (!gpus[gpu].has_adl || !adl_active) {
  660. wlogprint("Get fanrange not supported\n");
  661. return;
  662. }
  663. ga = &gpus[gpu].adl;
  664. *imin = ga->lpFanSpeedInfo.iMinPercent;
  665. *imax = ga->lpFanSpeedInfo.iMaxPercent;
  666. }
  667. static int set_fanspeed(int gpu, int iFanSpeed)
  668. {
  669. struct gpu_adl *ga;
  670. int ret = 1;
  671. if (!gpus[gpu].has_adl || !adl_active) {
  672. wlogprint("Set fanspeed not supported\n");
  673. return ret;
  674. }
  675. ga = &gpus[gpu].adl;
  676. if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE )))
  677. return ret;
  678. lock_adl();
  679. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  680. goto out;
  681. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  682. if ((ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE) &&
  683. !(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE)) {
  684. /* Must convert speed to an RPM */
  685. iFanSpeed = ga->lpFanSpeedInfo.iMaxRPM * iFanSpeed / 100;
  686. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  687. } else
  688. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  689. ga->lpFanSpeedValue.iFanSpeed = iFanSpeed;
  690. if (ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  691. goto out;
  692. ga->managed = true;
  693. ret = 0;
  694. out:
  695. unlock_adl();
  696. return ret;
  697. }
  698. static int set_powertune(int gpu, int iPercentage)
  699. {
  700. int oldPercentage, dummy;
  701. struct gpu_adl *ga;
  702. int ret = 1;
  703. if (!gpus[gpu].has_adl || !adl_active) {
  704. wlogprint("Set powertune not supported\n");
  705. return ret;
  706. }
  707. ga = &gpus[gpu].adl;
  708. oldPercentage = ga->iPercentage;
  709. lock_adl();
  710. if (ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, iPercentage) != ADL_OK) {
  711. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, ga->iPercentage);
  712. goto out;
  713. }
  714. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  715. ga->managed = true;
  716. ret = 0;
  717. out:
  718. unlock_adl();
  719. return ret;
  720. }
  721. void gpu_autotune(int gpu, bool *enable)
  722. {
  723. int temp, fanpercent, engine, newpercent, newengine;
  724. bool fan_optimal = true;
  725. struct gpu_adl *ga;
  726. ga = &gpus[gpu].adl;
  727. lock_adl();
  728. temp = __gpu_temp(ga);
  729. newpercent = fanpercent = __gpu_fanpercent(ga);
  730. unlock_adl();
  731. newengine = engine = gpu_engineclock(gpu) * 100;
  732. if (temp && fanpercent >= 0 && ga->autofan) {
  733. int top = gpus[gpu].gpu_fan;
  734. int bot = gpus[gpu].min_fan;
  735. if (temp > ga->overtemp && fanpercent < 100) {
  736. applog(LOG_WARNING, "Overheat detected on GPU %d, increasing fan to 100%", gpu);
  737. newpercent = 100;
  738. } else if (temp > ga->targettemp && fanpercent < top) {
  739. if (opt_debug)
  740. applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
  741. if (temp > ga->targettemp + opt_hysteresis)
  742. newpercent = fanpercent + 10;
  743. else
  744. newpercent = fanpercent + 5;
  745. if (newpercent > top)
  746. newpercent = top;
  747. } else if (fanpercent > bot && temp < ga->targettemp - opt_hysteresis) {
  748. if (opt_debug)
  749. applog(LOG_DEBUG, "Temperature %d degrees below target, decreasing fanspeed", opt_hysteresis);
  750. newpercent = fanpercent - 1;
  751. }
  752. if (newpercent > 100)
  753. newpercent = 100;
  754. else if (newpercent < 0)
  755. newpercent = 0;
  756. if (newpercent != fanpercent) {
  757. fan_optimal = false;
  758. applog(LOG_INFO, "Setting GPU %d fan percentage to %d", gpu, newpercent);
  759. set_fanspeed(gpu, newpercent);
  760. }
  761. }
  762. if (engine && ga->autoengine) {
  763. if (temp > ga->cutofftemp) {
  764. applog(LOG_WARNING, "Hit thermal cutoff limit on GPU %d, disabling!", gpu);
  765. *enable = false;
  766. newengine = ga->minspeed;
  767. } else if (temp > ga->overtemp && engine > ga->minspeed) {
  768. applog(LOG_WARNING, "Overheat detected, decreasing GPU %d clock speed", gpu);
  769. newengine = ga->minspeed;
  770. } else if (temp > ga->targettemp + opt_hysteresis && engine > ga->minspeed && fan_optimal) {
  771. if (opt_debug)
  772. applog(LOG_DEBUG, "Temperature %d degrees over target, decreasing clock speed", opt_hysteresis);
  773. newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
  774. } else if (temp < ga->targettemp && engine < ga->maxspeed) {
  775. if (opt_debug)
  776. applog(LOG_DEBUG, "Temperature below target, increasing clock speed");
  777. newengine = engine + ga->lpOdParameters.sEngineClock.iStep;
  778. }
  779. if (newengine > ga->maxspeed)
  780. newengine = ga->maxspeed;
  781. else if (newengine < ga->minspeed)
  782. newengine = ga->minspeed;
  783. if (newengine != engine) {
  784. newengine /= 100;
  785. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, newengine);
  786. set_engineclock(gpu, newengine);
  787. }
  788. }
  789. }
  790. void set_defaultfan(int gpu)
  791. {
  792. struct gpu_adl *ga;
  793. if (!gpus[gpu].has_adl || !adl_active)
  794. return;
  795. ga = &gpus[gpu].adl;
  796. lock_adl();
  797. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  798. unlock_adl();
  799. }
  800. void set_defaultengine(int gpu)
  801. {
  802. struct gpu_adl *ga;
  803. if (!gpus[gpu].has_adl || !adl_active)
  804. return;
  805. ga = &gpus[gpu].adl;
  806. lock_adl();
  807. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  808. unlock_adl();
  809. }
  810. void change_autosettings(int gpu)
  811. {
  812. struct gpu_adl *ga = &gpus[gpu].adl;
  813. char input;
  814. int val;
  815. wlogprint("Target temperature: %d\n", ga->targettemp);
  816. wlogprint("Overheat temperature: %d\n", ga->overtemp);
  817. wlogprint("Cutoff temperature: %d\n", ga->cutofftemp);
  818. wlogprint("Hysteresis differece: %d\n", opt_hysteresis);
  819. wlogprint("Toggle [F]an auto [G]PU auto\nChange [T]arget [O]verheat [C]utoff [H]ysteresis\n");
  820. wlogprint("Or press any other key to continue\n");
  821. input = getch();
  822. if (!strncasecmp(&input, "f", 1)) {
  823. ga->autofan ^= true;
  824. wlogprint("Fan autotune is now %s\n", ga->autofan ? "enabled" : "disabled");
  825. if (!ga->autofan) {
  826. wlogprint("Resetting fan to startup settings\n");
  827. set_defaultfan(gpu);
  828. }
  829. } else if (!strncasecmp(&input, "g", 1)) {
  830. ga->autoengine ^= true;
  831. wlogprint("GPU engine clock autotune is now %s\n", ga->autoengine ? "enabled" : "disabled");
  832. if (!ga->autoengine) {
  833. wlogprint("Resetting GPU engine clock to startup settings\n");
  834. set_defaultengine(gpu);
  835. }
  836. } else if (!strncasecmp(&input, "t", 1)) {
  837. val = curses_int("Enter target temperature for this GPU in C (0-200)");
  838. if (val < 0 || val > 200)
  839. wlogprint("Invalid temperature");
  840. else
  841. ga->targettemp = val;
  842. } else if (!strncasecmp(&input, "o", 1)) {
  843. wlogprint("Enter overheat temperature for this GPU in C (%d+)", ga->targettemp);
  844. val = curses_int("");
  845. if (val <= ga->targettemp || val > 200)
  846. wlogprint("Invalid temperature");
  847. else
  848. ga->overtemp = val;
  849. } else if (!strncasecmp(&input, "c", 1)) {
  850. wlogprint("Enter cutoff temperature for this GPU in C (%d+)", ga->overtemp);
  851. val = curses_int("");
  852. if (val <= ga->overtemp || val > 200)
  853. wlogprint("Invalid temperature");
  854. else
  855. ga->cutofftemp = val;
  856. } else if (!strncasecmp(&input, "h", 1)) {
  857. val = curses_int("Enter hysteresis temperature difference (1-10)");
  858. if (val < 1 || val > 10)
  859. wlogprint("Invalid value");
  860. else
  861. opt_hysteresis = val;
  862. }
  863. }
  864. void change_gpusettings(int gpu)
  865. {
  866. struct gpu_adl *ga = &gpus[gpu].adl;
  867. float fval, fmin = 0, fmax = 0;
  868. int val, imin = 0, imax = 0;
  869. char input;
  870. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  871. float temp = 0, vddc = 0;
  872. updated:
  873. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune))
  874. wlogprint("Temp: %.1f C\nFan Speed: %d%% (%d RPM)\nEngine Clock: %d MHz\n"
  875. "Memory Clock: %d Mhz\nVddc: %.3f V\nActivity: %d%%\nPowertune: %d%%\n",
  876. temp, fanpercent, fanspeed, engineclock, memclock, vddc, activity, powertune);
  877. wlogprint("Fan autotune is %s\n", ga->autofan ? "enabled" : "disabled");
  878. wlogprint("GPU engine clock autotune is %s (%d-%d)\n", ga->autoengine ? "enabled" : "disabled",
  879. ga->minspeed / 100, ga->maxspeed / 100);
  880. wlogprint("Change [A]utomatic [E]ngine [F]an [M]emory [V]oltage [P]owertune\n");
  881. wlogprint("Or press any other key to continue\n");
  882. input = getch();
  883. if (!strncasecmp(&input, "a", 1)) {
  884. change_autosettings(gpu);
  885. } else if (!strncasecmp(&input, "e", 1)) {
  886. get_enginerange(gpu, &imin, &imax);
  887. wlogprint("Enter GPU engine clock speed (%d - %d Mhz)", imin, imax);
  888. val = curses_int("");
  889. if (val < imin || val > imax) {
  890. wlogprint("Value is outside safe range, are you sure?\n");
  891. input = getch();
  892. if (strncasecmp(&input, "y", 1))
  893. return;
  894. }
  895. if (!set_engineclock(gpu, val))
  896. wlogprint("Driver reports success but check values below\n");
  897. else
  898. wlogprint("Failed to modify engine clock speed\n");
  899. } else if (!strncasecmp(&input, "f", 1)) {
  900. get_fanrange(gpu, &imin, &imax);
  901. wlogprint("Enter fan percentage (%d - %d %)", imin, imax);
  902. val = curses_int("");
  903. if (val < imin || val > imax) {
  904. wlogprint("Value is outside safe range, are you sure?\n");
  905. input = getch();
  906. if (strncasecmp(&input, "y", 1))
  907. return;
  908. }
  909. if (!set_fanspeed(gpu, val))
  910. wlogprint("Driver reports success but check values below\n");
  911. else
  912. wlogprint("Failed to modify fan speed\n");
  913. } else if (!strncasecmp(&input, "m", 1)) {
  914. get_memoryrange(gpu, &imin, &imax);
  915. wlogprint("Enter GPU memory clock speed (%d - %d Mhz)", imin, imax);
  916. val = curses_int("");
  917. if (val < imin || val > imax) {
  918. wlogprint("Value is outside safe range, are you sure?\n");
  919. input = getch();
  920. if (strncasecmp(&input, "y", 1))
  921. return;
  922. }
  923. if (!set_memoryclock(gpu, val))
  924. wlogprint("Driver reports success but check values below\n");
  925. else
  926. wlogprint("Failed to modify memory clock speed\n");
  927. } else if (!strncasecmp(&input, "v", 1)) {
  928. get_vddcrange(gpu, &fmin, &fmax);
  929. wlogprint("Enter GPU voltage (%.3f - %.3f V)", fmin, fmax);
  930. fval = curses_float("");
  931. if (fval < fmin || fval > fmax) {
  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_vddc(gpu, fval))
  938. wlogprint("Driver reports success but check values below\n");
  939. else
  940. wlogprint("Failed to modify voltage\n");
  941. } else if (!strncasecmp(&input, "p", 1)) {
  942. val = curses_int("Enter powertune value (-20 - 20)");
  943. if (val < -20 || val > 20) {
  944. wlogprint("Value is outside safe range, are you sure?\n");
  945. input = getch();
  946. if (strncasecmp(&input, "y", 1))
  947. return;
  948. }
  949. if (!set_powertune(gpu, val))
  950. wlogprint("Driver reports success but check values below\n");
  951. else
  952. wlogprint("Failed to modify powertune value\n");
  953. } else
  954. return;
  955. sleep(1);
  956. goto updated;
  957. }
  958. void clear_adl(nDevs)
  959. {
  960. struct gpu_adl *ga;
  961. int i;
  962. if (!adl_active)
  963. return;
  964. lock_adl();
  965. /* Try to reset values to their defaults */
  966. for (i = 0; i < nDevs; i++) {
  967. ga = &gpus[i].adl;
  968. /* Only reset the values if we've changed them at any time */
  969. if (!gpus[i].has_adl || !ga->managed)
  970. continue;
  971. ADL_Adapter_Speed_Set(ga->iAdapterIndex, ga->lpCurrent);
  972. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  973. free(ga->DefPerfLev);
  974. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  975. }
  976. ADL_Main_Memory_Free ( (void **)&lpInfo );
  977. ADL_Main_Control_Destroy ();
  978. unlock_adl();
  979. #if defined (LINUX)
  980. dlclose(hDLL);
  981. #else
  982. FreeLibrary(hDLL);
  983. #endif
  984. }
  985. #endif /* HAVE_ADL */