adl.c 44 KB

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