adl.c 35 KB

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