adl.c 41 KB

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