adl.c 37 KB

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