adl.c 44 KB

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