adl.c 34 KB

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