adl.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  516. goto out;
  517. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  518. /* Reset to old value if it fails! */
  519. if (lpOdPerformanceLevels->aLevels[lev].iEngineClock != iEngineClock) {
  520. /* Set all the parameters in case they're linked somehow */
  521. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  522. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  523. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  524. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  525. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  526. goto out;
  527. }
  528. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  529. if (ga->iEngineClock > ga->maxspeed)
  530. ga->maxspeed = ga->iEngineClock;
  531. if (ga->iEngineClock < ga->minspeed)
  532. ga->minspeed = ga->iEngineClock;
  533. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  534. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  535. ga->managed = true;
  536. ret = 0;
  537. out:
  538. unlock_adl();
  539. return ret;
  540. }
  541. static void get_memoryrange(int gpu, int *imin, int *imax)
  542. {
  543. struct gpu_adl *ga;
  544. if (!gpus[gpu].has_adl || !adl_active) {
  545. wlogprint("Get memoryrange not supported\n");
  546. return;
  547. }
  548. ga = &gpus[gpu].adl;
  549. *imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
  550. *imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
  551. }
  552. static int set_memoryclock(int gpu, int iMemoryClock)
  553. {
  554. ADLODPerformanceLevels *lpOdPerformanceLevels;
  555. int i, lev, ret = 1;
  556. struct gpu_adl *ga;
  557. if (!gpus[gpu].has_adl || !adl_active) {
  558. wlogprint("Set memoryclock not supported\n");
  559. return ret;
  560. }
  561. iMemoryClock *= 100;
  562. ga = &gpus[gpu].adl;
  563. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  564. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  565. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  566. lock_adl();
  567. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  568. goto out;
  569. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
  570. for (i = 0; i < lev; i++) {
  571. if (lpOdPerformanceLevels->aLevels[i].iMemoryClock > iMemoryClock)
  572. lpOdPerformanceLevels->aLevels[i].iMemoryClock = iMemoryClock;
  573. }
  574. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  575. goto out;
  576. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  577. /* Reset to old value if it fails! */
  578. if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock != iMemoryClock) {
  579. /* Set all the parameters in case they're linked somehow */
  580. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iEngineClock;
  581. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  582. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  583. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  584. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  585. goto out;
  586. }
  587. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  588. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  589. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  590. ga->managed = true;
  591. ret = 0;
  592. out:
  593. unlock_adl();
  594. return ret;
  595. }
  596. static void get_vddcrange(int gpu, float *imin, float *imax)
  597. {
  598. struct gpu_adl *ga;
  599. if (!gpus[gpu].has_adl || !adl_active) {
  600. wlogprint("Get vddcrange not supported\n");
  601. return;
  602. }
  603. ga = &gpus[gpu].adl;
  604. *imin = (float)ga->lpOdParameters.sVddc.iMin / 1000;
  605. *imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
  606. }
  607. static float curses_float(const char *query)
  608. {
  609. float ret;
  610. char *cvar;
  611. cvar = curses_input(query);
  612. ret = atof(cvar);
  613. free(cvar);
  614. return ret;
  615. }
  616. static int set_vddc(int gpu, float fVddc)
  617. {
  618. ADLODPerformanceLevels *lpOdPerformanceLevels;
  619. int i, iVddc, lev, ret = 1;
  620. struct gpu_adl *ga;
  621. if (!gpus[gpu].has_adl || !adl_active) {
  622. wlogprint("Set vddc not supported\n");
  623. return ret;
  624. }
  625. iVddc = 1000 * fVddc;
  626. ga = &gpus[gpu].adl;
  627. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  628. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  629. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  630. lock_adl();
  631. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  632. goto out;
  633. for (i = 0; i < lev; i++) {
  634. if (lpOdPerformanceLevels->aLevels[i].iVddc > iVddc)
  635. lpOdPerformanceLevels->aLevels[i].iVddc = iVddc;
  636. }
  637. lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
  638. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  639. goto out;
  640. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  641. /* Reset to old value if it fails! */
  642. if (lpOdPerformanceLevels->aLevels[lev].iVddc != iVddc) {
  643. /* Set all the parameters in case they're linked somehow */
  644. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  645. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  646. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  647. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  648. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  649. goto out;
  650. }
  651. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  652. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  653. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  654. ga->managed = true;
  655. ret = 0;
  656. out:
  657. unlock_adl();
  658. return ret;
  659. }
  660. static void get_fanrange(int gpu, int *imin, int *imax)
  661. {
  662. struct gpu_adl *ga;
  663. if (!gpus[gpu].has_adl || !adl_active) {
  664. wlogprint("Get fanrange not supported\n");
  665. return;
  666. }
  667. ga = &gpus[gpu].adl;
  668. *imin = ga->lpFanSpeedInfo.iMinPercent;
  669. *imax = ga->lpFanSpeedInfo.iMaxPercent;
  670. }
  671. static int set_fanspeed(int gpu, int iFanSpeed)
  672. {
  673. struct gpu_adl *ga;
  674. int ret = 1;
  675. if (!gpus[gpu].has_adl || !adl_active) {
  676. wlogprint("Set fanspeed not supported\n");
  677. return ret;
  678. }
  679. ga = &gpus[gpu].adl;
  680. if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE )))
  681. return ret;
  682. lock_adl();
  683. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  684. goto out;
  685. /* Store what fanspeed we're actually aiming for for re-entrant changes
  686. * in case this device does not support fine setting changes */
  687. ga->targetfan = iFanSpeed;
  688. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  689. if ((ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE) &&
  690. !(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. ga->lpFanSpeedValue.iFanSpeed = iFanSpeed;
  697. if (ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  698. goto out;
  699. ga->managed = true;
  700. ret = 0;
  701. out:
  702. unlock_adl();
  703. return ret;
  704. }
  705. static int set_powertune(int gpu, int iPercentage)
  706. {
  707. int oldPercentage, dummy;
  708. struct gpu_adl *ga;
  709. int ret = 1;
  710. if (!gpus[gpu].has_adl || !adl_active) {
  711. wlogprint("Set powertune not supported\n");
  712. return ret;
  713. }
  714. ga = &gpus[gpu].adl;
  715. oldPercentage = ga->iPercentage;
  716. lock_adl();
  717. if (ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, iPercentage) != ADL_OK) {
  718. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, ga->iPercentage);
  719. goto out;
  720. }
  721. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  722. ga->managed = true;
  723. ret = 0;
  724. out:
  725. unlock_adl();
  726. return ret;
  727. }
  728. void gpu_autotune(int gpu, bool *enable)
  729. {
  730. int temp, fanpercent, engine, newpercent, newengine;
  731. bool fan_optimal = true;
  732. struct gpu_adl *ga;
  733. ga = &gpus[gpu].adl;
  734. lock_adl();
  735. temp = __gpu_temp(ga);
  736. newpercent = fanpercent = __gpu_fanpercent(ga);
  737. unlock_adl();
  738. newengine = engine = gpu_engineclock(gpu) * 100;
  739. if (temp && fanpercent >= 0 && ga->autofan) {
  740. int top = gpus[gpu].gpu_fan;
  741. int bot = gpus[gpu].min_fan;
  742. if (temp > ga->overtemp && fanpercent < 100) {
  743. applog(LOG_WARNING, "Overheat detected on GPU %d, increasing fan to 100%", gpu);
  744. newpercent = 100;
  745. } else if (temp > ga->targettemp && fanpercent < top) {
  746. if (opt_debug)
  747. applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
  748. if (temp > ga->targettemp + opt_hysteresis)
  749. newpercent = ga->targetfan + 10;
  750. else
  751. newpercent = ga->targetfan + 5;
  752. if (newpercent > top)
  753. newpercent = top;
  754. } else if (fanpercent > bot && temp < ga->targettemp - opt_hysteresis) {
  755. if (opt_debug)
  756. applog(LOG_DEBUG, "Temperature %d degrees below target, decreasing fanspeed", opt_hysteresis);
  757. newpercent = ga->targetfan - 1;
  758. }
  759. if (newpercent > 100)
  760. newpercent = 100;
  761. else if (newpercent < 0)
  762. newpercent = 0;
  763. if (newpercent != fanpercent) {
  764. fan_optimal = false;
  765. applog(LOG_INFO, "Setting GPU %d fan percentage to %d", gpu, newpercent);
  766. set_fanspeed(gpu, newpercent);
  767. if (newpercent > fanpercent) {
  768. int changefan = gpu_fanspeed(gpu);
  769. if (changefan < newpercent) {
  770. newpercent += 10;
  771. newpercent -= newpercent % 10;
  772. set_fanspeed(gpu, newpercent);
  773. }
  774. }
  775. }
  776. }
  777. if (engine && ga->autoengine) {
  778. if (temp > ga->cutofftemp) {
  779. applog(LOG_WARNING, "Hit thermal cutoff limit on GPU %d, disabling!", gpu);
  780. *enable = false;
  781. newengine = ga->minspeed;
  782. } else if (temp > ga->overtemp && engine > ga->minspeed) {
  783. applog(LOG_WARNING, "Overheat detected, decreasing GPU %d clock speed", gpu);
  784. newengine = ga->minspeed;
  785. } else if (temp > ga->targettemp + opt_hysteresis && engine > ga->minspeed && fan_optimal) {
  786. if (opt_debug)
  787. applog(LOG_DEBUG, "Temperature %d degrees over target, decreasing clock speed", opt_hysteresis);
  788. newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
  789. } else if (temp < ga->targettemp && engine < ga->maxspeed) {
  790. if (opt_debug)
  791. applog(LOG_DEBUG, "Temperature below target, increasing clock speed");
  792. newengine = engine + ga->lpOdParameters.sEngineClock.iStep;
  793. }
  794. if (newengine > ga->maxspeed)
  795. newengine = ga->maxspeed;
  796. else if (newengine < ga->minspeed)
  797. newengine = ga->minspeed;
  798. if (newengine != engine) {
  799. newengine /= 100;
  800. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, newengine);
  801. set_engineclock(gpu, newengine);
  802. }
  803. }
  804. }
  805. void set_defaultfan(int gpu)
  806. {
  807. struct gpu_adl *ga;
  808. if (!gpus[gpu].has_adl || !adl_active)
  809. return;
  810. ga = &gpus[gpu].adl;
  811. lock_adl();
  812. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  813. unlock_adl();
  814. }
  815. void set_defaultengine(int gpu)
  816. {
  817. struct gpu_adl *ga;
  818. if (!gpus[gpu].has_adl || !adl_active)
  819. return;
  820. ga = &gpus[gpu].adl;
  821. lock_adl();
  822. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  823. unlock_adl();
  824. }
  825. void change_autosettings(int gpu)
  826. {
  827. struct gpu_adl *ga = &gpus[gpu].adl;
  828. char input;
  829. int val;
  830. wlogprint("Target temperature: %d\n", ga->targettemp);
  831. wlogprint("Overheat temperature: %d\n", ga->overtemp);
  832. wlogprint("Cutoff temperature: %d\n", ga->cutofftemp);
  833. wlogprint("Toggle [F]an auto [G]PU auto\nChange [T]arget [O]verheat [C]utoff\n");
  834. wlogprint("Or press any other key to continue\n");
  835. input = getch();
  836. if (!strncasecmp(&input, "f", 1)) {
  837. ga->autofan ^= true;
  838. wlogprint("Fan autotune is now %s\n", ga->autofan ? "enabled" : "disabled");
  839. if (!ga->autofan) {
  840. wlogprint("Resetting fan to startup settings\n");
  841. set_defaultfan(gpu);
  842. }
  843. } else if (!strncasecmp(&input, "g", 1)) {
  844. ga->autoengine ^= true;
  845. wlogprint("GPU engine clock autotune is now %s\n", ga->autoengine ? "enabled" : "disabled");
  846. if (!ga->autoengine) {
  847. wlogprint("Resetting GPU engine clock to startup settings\n");
  848. set_defaultengine(gpu);
  849. }
  850. } else if (!strncasecmp(&input, "t", 1)) {
  851. val = curses_int("Enter target temperature for this GPU in C (0-200)");
  852. if (val < 0 || val > 200)
  853. wlogprint("Invalid temperature");
  854. else
  855. ga->targettemp = val;
  856. } else if (!strncasecmp(&input, "o", 1)) {
  857. wlogprint("Enter overheat temperature for this GPU in C (%d+)", ga->targettemp);
  858. val = curses_int("");
  859. if (val <= ga->targettemp || val > 200)
  860. wlogprint("Invalid temperature");
  861. else
  862. ga->overtemp = val;
  863. } else if (!strncasecmp(&input, "c", 1)) {
  864. wlogprint("Enter cutoff temperature for this GPU in C (%d+)", ga->overtemp);
  865. val = curses_int("");
  866. if (val <= ga->overtemp || val > 200)
  867. wlogprint("Invalid temperature");
  868. else
  869. ga->cutofftemp = val;
  870. }
  871. }
  872. void change_gpusettings(int gpu)
  873. {
  874. struct gpu_adl *ga = &gpus[gpu].adl;
  875. float fval, fmin = 0, fmax = 0;
  876. int val, imin = 0, imax = 0;
  877. char input;
  878. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  879. float temp = 0, vddc = 0;
  880. updated:
  881. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune))
  882. wlogprint("Temp: %.1f C\nFan Speed: %d%% (%d RPM)\nEngine Clock: %d MHz\n"
  883. "Memory Clock: %d Mhz\nVddc: %.3f V\nActivity: %d%%\nPowertune: %d%%\n",
  884. temp, fanpercent, fanspeed, engineclock, memclock, vddc, activity, powertune);
  885. wlogprint("Fan autotune is %s (%d-%d)\n", ga->autofan ? "enabled" : "disabled",
  886. gpus[gpu].min_fan, gpus[gpu].gpu_fan);
  887. wlogprint("GPU engine clock autotune is %s (%d-%d)\n", ga->autoengine ? "enabled" : "disabled",
  888. ga->minspeed / 100, ga->maxspeed / 100);
  889. wlogprint("Change [A]utomatic [E]ngine [F]an [M]emory [V]oltage [P]owertune\n");
  890. wlogprint("Or press any other key to continue\n");
  891. input = getch();
  892. if (!strncasecmp(&input, "a", 1)) {
  893. change_autosettings(gpu);
  894. } else if (!strncasecmp(&input, "e", 1)) {
  895. get_enginerange(gpu, &imin, &imax);
  896. wlogprint("Enter GPU engine clock speed (%d - %d Mhz)", 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_engineclock(gpu, val))
  905. wlogprint("Driver reports success but check values below\n");
  906. else
  907. wlogprint("Failed to modify engine clock speed\n");
  908. } else if (!strncasecmp(&input, "f", 1)) {
  909. get_fanrange(gpu, &imin, &imax);
  910. wlogprint("Enter fan percentage (%d - %d %)", 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_fanspeed(gpu, val))
  919. wlogprint("Driver reports success but check values below\n");
  920. else
  921. wlogprint("Failed to modify fan speed\n");
  922. } else if (!strncasecmp(&input, "m", 1)) {
  923. get_memoryrange(gpu, &imin, &imax);
  924. wlogprint("Enter GPU memory clock speed (%d - %d Mhz)", imin, imax);
  925. val = curses_int("");
  926. if (val < imin || val > imax) {
  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_memoryclock(gpu, val))
  933. wlogprint("Driver reports success but check values below\n");
  934. else
  935. wlogprint("Failed to modify memory clock speed\n");
  936. } else if (!strncasecmp(&input, "v", 1)) {
  937. get_vddcrange(gpu, &fmin, &fmax);
  938. wlogprint("Enter GPU voltage (%.3f - %.3f V)", fmin, fmax);
  939. fval = curses_float("");
  940. if (fval < fmin || fval > fmax) {
  941. wlogprint("Value is outside safe range, are you sure?\n");
  942. input = getch();
  943. if (strncasecmp(&input, "y", 1))
  944. return;
  945. }
  946. if (!set_vddc(gpu, fval))
  947. wlogprint("Driver reports success but check values below\n");
  948. else
  949. wlogprint("Failed to modify voltage\n");
  950. } else if (!strncasecmp(&input, "p", 1)) {
  951. val = curses_int("Enter powertune value (-20 - 20)");
  952. if (val < -20 || val > 20) {
  953. wlogprint("Value is outside safe range, are you sure?\n");
  954. input = getch();
  955. if (strncasecmp(&input, "y", 1))
  956. return;
  957. }
  958. if (!set_powertune(gpu, val))
  959. wlogprint("Driver reports success but check values below\n");
  960. else
  961. wlogprint("Failed to modify powertune value\n");
  962. } else
  963. return;
  964. sleep(1);
  965. goto updated;
  966. }
  967. void clear_adl(nDevs)
  968. {
  969. struct gpu_adl *ga;
  970. int i;
  971. if (!adl_active)
  972. return;
  973. lock_adl();
  974. /* Try to reset values to their defaults */
  975. for (i = 0; i < nDevs; i++) {
  976. ga = &gpus[i].adl;
  977. /* Only reset the values if we've changed them at any time */
  978. if (!gpus[i].has_adl || !ga->managed)
  979. continue;
  980. ADL_Adapter_Speed_Set(ga->iAdapterIndex, ga->lpCurrent);
  981. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  982. free(ga->DefPerfLev);
  983. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  984. }
  985. ADL_Main_Memory_Free ( (void **)&lpInfo );
  986. ADL_Main_Control_Destroy ();
  987. unlock_adl();
  988. #if defined (LINUX)
  989. dlclose(hDLL);
  990. #else
  991. FreeLibrary(hDLL);
  992. #endif
  993. }
  994. #endif /* HAVE_ADL */