adl.c 35 KB

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