adl.c 34 KB

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