adl.c 42 KB

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