adl.c 35 KB

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