adl.c 40 KB

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