adl.c 40 KB

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