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