adl.c 43 KB

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