adl.c 44 KB

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