adl.c 40 KB

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