adl.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. #include "miner.h"
  2. #include "adl.h"
  3. bool adl_active;
  4. #ifdef HAVE_ADL
  5. #if defined (__linux)
  6. #include "ADL_SDK/adl_sdk.h"
  7. #include <dlfcn.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #else
  11. #include <windows.h>
  12. #include <tchar.h>
  13. #include "ADL_SDK/adl_sdk.h"
  14. #endif
  15. #include <stdio.h>
  16. #include <curses.h>
  17. #include "adl_functions.h"
  18. // Memory allocation function
  19. static void * __stdcall ADL_Main_Memory_Alloc(int iSize)
  20. {
  21. void *lpBuffer = malloc(iSize);
  22. return lpBuffer;
  23. }
  24. // Optional Memory de-allocation function
  25. static void __stdcall ADL_Main_Memory_Free (void **lpBuffer)
  26. {
  27. if (*lpBuffer) {
  28. free (*lpBuffer);
  29. *lpBuffer = NULL;
  30. }
  31. }
  32. #if defined (LINUX)
  33. // equivalent functions in linux
  34. static void *GetProcAddress(void *pLibrary, const char *name)
  35. {
  36. return dlsym( pLibrary, name);
  37. }
  38. #endif
  39. static ADL_MAIN_CONTROL_CREATE ADL_Main_Control_Create;
  40. static ADL_MAIN_CONTROL_DESTROY ADL_Main_Control_Destroy;
  41. static ADL_ADAPTER_NUMBEROFADAPTERS_GET ADL_Adapter_NumberOfAdapters_Get;
  42. static ADL_ADAPTER_ADAPTERINFO_GET ADL_Adapter_AdapterInfo_Get;
  43. static ADL_ADAPTER_ID_GET ADL_Adapter_ID_Get;
  44. static ADL_OVERDRIVE5_TEMPERATURE_GET ADL_Overdrive5_Temperature_Get;
  45. static ADL_ADAPTER_ACTIVE_GET ADL_Adapter_Active_Get;
  46. static ADL_OVERDRIVE5_CURRENTACTIVITY_GET ADL_Overdrive5_CurrentActivity_Get;
  47. static ADL_OVERDRIVE5_ODPARAMETERS_GET ADL_Overdrive5_ODParameters_Get;
  48. static ADL_OVERDRIVE5_FANSPEEDINFO_GET ADL_Overdrive5_FanSpeedInfo_Get;
  49. static ADL_OVERDRIVE5_FANSPEED_GET ADL_Overdrive5_FanSpeed_Get;
  50. static ADL_OVERDRIVE5_FANSPEED_SET ADL_Overdrive5_FanSpeed_Set;
  51. static ADL_OVERDRIVE5_ODPERFORMANCELEVELS_GET ADL_Overdrive5_ODPerformanceLevels_Get;
  52. static ADL_OVERDRIVE5_ODPERFORMANCELEVELS_SET ADL_Overdrive5_ODPerformanceLevels_Set;
  53. static ADL_MAIN_CONTROL_REFRESH ADL_Main_Control_Refresh;
  54. #if defined (LINUX)
  55. static void *hDLL; // Handle to .so library
  56. #else
  57. HINSTANCE hDLL; // Handle to DLL
  58. #endif
  59. static int iNumberAdapters;
  60. static LPAdapterInfo lpInfo = NULL;
  61. void init_adl(int nDevs)
  62. {
  63. int i, devices = 0, last_adapter = -1;
  64. #if defined (LINUX)
  65. hDLL = dlopen( "libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
  66. #else
  67. hDLL = LoadLibrary("atiadlxx.dll");
  68. if (hDLL == NULL)
  69. // A 32 bit calling application on 64 bit OS will fail to LoadLIbrary.
  70. // Try to load the 32 bit library (atiadlxy.dll) instead
  71. hDLL = LoadLibrary("atiadlxy.dll");
  72. #endif
  73. if (hDLL == NULL) {
  74. applog(LOG_INFO, "Unable to load ati adl library");
  75. return;
  76. }
  77. ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE) GetProcAddress(hDLL,"ADL_Main_Control_Create");
  78. ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY) GetProcAddress(hDLL,"ADL_Main_Control_Destroy");
  79. ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET) GetProcAddress(hDLL,"ADL_Adapter_NumberOfAdapters_Get");
  80. ADL_Adapter_AdapterInfo_Get = (ADL_ADAPTER_ADAPTERINFO_GET) GetProcAddress(hDLL,"ADL_Adapter_AdapterInfo_Get");
  81. ADL_Adapter_ID_Get = (ADL_ADAPTER_ID_GET) GetProcAddress(hDLL,"ADL_Adapter_ID_Get");
  82. ADL_Overdrive5_Temperature_Get = (ADL_OVERDRIVE5_TEMPERATURE_GET) GetProcAddress(hDLL,"ADL_Overdrive5_Temperature_Get");
  83. ADL_Adapter_Active_Get = (ADL_ADAPTER_ACTIVE_GET) GetProcAddress(hDLL, "ADL_Adapter_Active_Get");
  84. ADL_Overdrive5_CurrentActivity_Get = (ADL_OVERDRIVE5_CURRENTACTIVITY_GET) GetProcAddress(hDLL, "ADL_Overdrive5_CurrentActivity_Get");
  85. ADL_Overdrive5_ODParameters_Get = (ADL_OVERDRIVE5_ODPARAMETERS_GET) GetProcAddress(hDLL, "ADL_Overdrive5_ODParameters_Get");
  86. ADL_Overdrive5_FanSpeedInfo_Get = (ADL_OVERDRIVE5_FANSPEEDINFO_GET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeedInfo_Get");
  87. ADL_Overdrive5_FanSpeed_Get = (ADL_OVERDRIVE5_FANSPEED_GET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeed_Get");
  88. ADL_Overdrive5_FanSpeed_Set = (ADL_OVERDRIVE5_FANSPEED_SET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeed_Set");
  89. ADL_Overdrive5_ODPerformanceLevels_Get = (ADL_OVERDRIVE5_ODPERFORMANCELEVELS_GET) GetProcAddress(hDLL, "ADL_Overdrive5_ODPerformanceLevels_Get");
  90. ADL_Overdrive5_ODPerformanceLevels_Set = (ADL_OVERDRIVE5_ODPERFORMANCELEVELS_SET) GetProcAddress(hDLL, "ADL_Overdrive5_ODPerformanceLevels_Set");
  91. ADL_Main_Control_Refresh = (ADL_MAIN_CONTROL_REFRESH) GetProcAddress(hDLL, "ADL_Main_Control_Refresh");
  92. if (!ADL_Main_Control_Create || !ADL_Main_Control_Destroy ||
  93. !ADL_Adapter_NumberOfAdapters_Get || !ADL_Adapter_AdapterInfo_Get ||
  94. !ADL_Adapter_ID_Get || !ADL_Overdrive5_Temperature_Get ||
  95. !ADL_Adapter_Active_Get || !ADL_Overdrive5_CurrentActivity_Get ||
  96. !ADL_Overdrive5_ODParameters_Get || !ADL_Overdrive5_FanSpeedInfo_Get ||
  97. !ADL_Overdrive5_FanSpeed_Get || !ADL_Overdrive5_FanSpeed_Set ||
  98. !ADL_Overdrive5_ODPerformanceLevels_Get || !ADL_Overdrive5_ODPerformanceLevels_Set ||
  99. !ADL_Main_Control_Refresh) {
  100. applog(LOG_INFO, "ATI ADL's API is missing");
  101. return;
  102. }
  103. // Initialise ADL. The second parameter is 1, which means:
  104. // retrieve adapter information only for adapters that are physically present and enabled in the system
  105. if (ADL_Main_Control_Create (ADL_Main_Memory_Alloc, 1) != ADL_OK) {
  106. applog(LOG_INFO, "ADL Initialisation Error!");
  107. return ;
  108. }
  109. if (ADL_Main_Control_Refresh() != ADL_OK) {
  110. applog(LOG_INFO, "ADL Refresh Error!");
  111. return ;
  112. }
  113. // Obtain the number of adapters for the system
  114. if (ADL_Adapter_NumberOfAdapters_Get ( &iNumberAdapters ) != ADL_OK) {
  115. applog(LOG_INFO, "Cannot get the number of adapters!\n");
  116. return ;
  117. }
  118. if (iNumberAdapters > 0) {
  119. lpInfo = malloc ( sizeof (AdapterInfo) * iNumberAdapters );
  120. memset ( lpInfo,'\0', sizeof (AdapterInfo) * iNumberAdapters );
  121. // Get the AdapterInfo structure for all adapters in the system
  122. if (ADL_Adapter_AdapterInfo_Get (lpInfo, sizeof (AdapterInfo) * iNumberAdapters) != ADL_OK) {
  123. applog(LOG_INFO, "ADL_Adapter_AdapterInfo_Get Error!");
  124. return ;
  125. }
  126. } else {
  127. applog(LOG_INFO, "No adapters found");
  128. return;
  129. }
  130. for (i = 0; i < iNumberAdapters; i++) {
  131. struct gpu_adl *ga;
  132. int iAdapterIndex;
  133. int lpAdapterID;
  134. int lpStatus;
  135. ADLODPerformanceLevels *lpOdPerformanceLevels;
  136. int lev;
  137. iAdapterIndex = lpInfo[i].iAdapterIndex;
  138. /* Get unique identifier of the adapter */
  139. if (ADL_Adapter_ID_Get(iAdapterIndex, &lpAdapterID) != ADL_OK) {
  140. applog(LOG_INFO, "Failed to ADL_Adapter_ID_Get");
  141. continue;
  142. }
  143. if (!lpAdapterID)
  144. continue;
  145. if (lpAdapterID != last_adapter) {
  146. /* We found a truly new adapter instead of a logical
  147. * one. Now since there's no way of correlating the
  148. * opencl enumerated devices and the ADL enumerated
  149. * ones, we have to assume they're in the same order.*/
  150. if (++devices > nDevs) {
  151. applog(LOG_ERR, "ADL found more devices than opencl");
  152. return;
  153. }
  154. last_adapter = lpAdapterID;
  155. }
  156. /* See if the adapter is an AMD device with ADL active */
  157. if (ADL_Adapter_Active_Get(iAdapterIndex, &lpStatus) != ADL_OK) {
  158. applog(LOG_INFO, "Failed to ADL_Adapter_Active_Get");
  159. continue;
  160. }
  161. if (!lpStatus)
  162. continue;
  163. /* From here on we know this device is a discrete device and
  164. * should support ADL */
  165. ga = &gpus[devices - 1].adl;
  166. ga->iAdapterIndex = iAdapterIndex;
  167. ga->lpAdapterID = lpAdapterID;
  168. ga->lpStatus = lpStatus;
  169. ga->DefPerfLev = NULL;
  170. if (ADL_Overdrive5_ODParameters_Get(iAdapterIndex, &ga->lpOdParameters) != ADL_OK) {
  171. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODParameters_Get");
  172. continue;
  173. }
  174. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  175. /* We're only interested in the top performance level */
  176. lpOdPerformanceLevels = malloc(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  177. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  178. /* Get default performance levels first */
  179. if (ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 1, lpOdPerformanceLevels) != ADL_OK) {
  180. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODPerformanceLevels_Get");
  181. continue;
  182. }
  183. /* Set the limits we'd use based on default gpu speeds */
  184. ga->maxspeed = ga->minspeed = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  185. /* Now get the current performance levels for any existing overclock */
  186. ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
  187. /* Save these values as the defaults in case we wish to reset to defaults */
  188. ga->DefPerfLev = lpOdPerformanceLevels;
  189. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  190. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  191. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  192. if (ga->iEngineClock < ga->minspeed)
  193. ga->minspeed = ga->iEngineClock;
  194. if (ga->iEngineClock > ga->maxspeed)
  195. ga->maxspeed = ga->iEngineClock;
  196. if (ADL_Overdrive5_FanSpeedInfo_Get(iAdapterIndex, 0, &ga->lpFanSpeedInfo) != ADL_OK) {
  197. applog(LOG_INFO, "Failed to ADL_Overdrive5_FanSpeedInfo_Get");
  198. continue;
  199. }
  200. /* Save the fanspeed values as defaults in case we reset later */
  201. ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  202. /* Set some default temperatures for autotune when enabled */
  203. ga->targettemp = 75;
  204. ga->overtemp = 85;
  205. if (opt_autofan)
  206. ga->autofan = true;
  207. if (opt_autoengine)
  208. ga->autoengine = true;
  209. gpus[devices - 1].has_adl = true;
  210. }
  211. adl_active = true;
  212. }
  213. float gpu_temp(int gpu)
  214. {
  215. struct gpu_adl *ga;
  216. if (!gpus[gpu].has_adl || !adl_active)
  217. return 0;
  218. ga = &gpus[gpu].adl;
  219. if (ADL_Overdrive5_Temperature_Get(ga->iAdapterIndex, 0, &ga->lpTemperature) != ADL_OK)
  220. return 0;
  221. return (float)ga->lpTemperature.iTemperature / 1000;
  222. }
  223. int gpu_engineclock(int gpu)
  224. {
  225. struct gpu_adl *ga;
  226. if (!gpus[gpu].has_adl || !adl_active)
  227. return 0;
  228. ga = &gpus[gpu].adl;
  229. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  230. return 0;
  231. return ga->lpActivity.iEngineClock / 100;
  232. }
  233. int gpu_memclock(int gpu)
  234. {
  235. struct gpu_adl *ga;
  236. if (!gpus[gpu].has_adl || !adl_active)
  237. return 0;
  238. ga = &gpus[gpu].adl;
  239. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  240. return 0;
  241. return ga->lpActivity.iMemoryClock / 100;
  242. }
  243. float gpu_vddc(int gpu)
  244. {
  245. struct gpu_adl *ga;
  246. if (!gpus[gpu].has_adl || !adl_active)
  247. return 0;
  248. ga = &gpus[gpu].adl;
  249. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  250. return 0;
  251. return (float)ga->lpActivity.iVddc / 1000;
  252. }
  253. int gpu_activity(int gpu)
  254. {
  255. struct gpu_adl *ga;
  256. if (!gpus[gpu].has_adl || !adl_active)
  257. return 0;
  258. ga = &gpus[gpu].adl;
  259. if (!ga->lpOdParameters.iActivityReportingSupported)
  260. return 0;
  261. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  262. return 0;
  263. return ga->lpActivity.iActivityPercent;
  264. }
  265. int gpu_fanspeed(int gpu)
  266. {
  267. struct gpu_adl *ga;
  268. if (!gpus[gpu].has_adl || !adl_active)
  269. return 0;
  270. ga = &gpus[gpu].adl;
  271. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_READ))
  272. return 0;
  273. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  274. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  275. return 0;
  276. return ga->lpFanSpeedValue.iFanSpeed;
  277. }
  278. static int gpu_fanpercent(int gpu)
  279. {
  280. struct gpu_adl *ga;
  281. if (!gpus[gpu].has_adl || !adl_active)
  282. return -1;
  283. ga = &gpus[gpu].adl;
  284. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ ))
  285. return -1;
  286. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  287. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  288. return -1;
  289. return ga->lpFanSpeedValue.iFanSpeed;
  290. }
  291. static void get_enginerange(int gpu, int *imin, int *imax)
  292. {
  293. struct gpu_adl *ga;
  294. if (!gpus[gpu].has_adl || !adl_active) {
  295. wlogprint("Get enginerange not supported\n");
  296. return;
  297. }
  298. ga = &gpus[gpu].adl;
  299. *imin = ga->lpOdParameters.sEngineClock.iMin / 100;
  300. *imax = ga->lpOdParameters.sEngineClock.iMax / 100;
  301. }
  302. static int set_engineclock(int gpu, int iEngineClock)
  303. {
  304. ADLODPerformanceLevels *lpOdPerformanceLevels;
  305. struct gpu_adl *ga;
  306. int lev;
  307. if (!gpus[gpu].has_adl || !adl_active) {
  308. wlogprint("Set engineclock not supported\n");
  309. return 1;
  310. }
  311. iEngineClock *= 100;
  312. ga = &gpus[gpu].adl;
  313. if (iEngineClock > ga->lpOdParameters.sEngineClock.iMax ||
  314. iEngineClock < ga->lpOdParameters.sEngineClock.iMin)
  315. return 1;
  316. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  317. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  318. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  319. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  320. return 1;
  321. lpOdPerformanceLevels->aLevels[lev].iEngineClock = iEngineClock;
  322. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  323. return 1;
  324. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  325. /* Reset to old value if it fails! */
  326. if (lpOdPerformanceLevels->aLevels[lev].iEngineClock != iEngineClock) {
  327. /* Set all the parameters in case they're linked somehow */
  328. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  329. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  330. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  331. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  332. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  333. return 1;
  334. }
  335. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  336. if (ga->iEngineClock > ga->maxspeed)
  337. ga->maxspeed = ga->iEngineClock;
  338. if (ga->iEngineClock < ga->minspeed)
  339. ga->minspeed = ga->iEngineClock;
  340. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  341. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  342. return 0;
  343. }
  344. static void get_memoryrange(int gpu, int *imin, int *imax)
  345. {
  346. struct gpu_adl *ga;
  347. if (!gpus[gpu].has_adl || !adl_active) {
  348. wlogprint("Get memoryrange not supported\n");
  349. return;
  350. }
  351. ga = &gpus[gpu].adl;
  352. *imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
  353. *imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
  354. }
  355. static int set_memoryclock(int gpu, int iMemoryClock)
  356. {
  357. ADLODPerformanceLevels *lpOdPerformanceLevels;
  358. struct gpu_adl *ga;
  359. int lev;
  360. if (!gpus[gpu].has_adl || !adl_active) {
  361. wlogprint("Set memoryclock not supported\n");
  362. return 1;
  363. }
  364. iMemoryClock *= 100;
  365. ga = &gpus[gpu].adl;
  366. if (iMemoryClock > ga->lpOdParameters.sMemoryClock.iMax ||
  367. iMemoryClock < ga->lpOdParameters.sMemoryClock.iMin)
  368. return 1;
  369. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  370. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  371. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  372. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  373. return 1;
  374. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
  375. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  376. return 1;
  377. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  378. /* Reset to old value if it fails! */
  379. if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock != iMemoryClock) {
  380. /* Set all the parameters in case they're linked somehow */
  381. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iEngineClock;
  382. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  383. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  384. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  385. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  386. return 1;
  387. }
  388. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  389. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  390. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  391. return 0;
  392. }
  393. static void get_vddcrange(int gpu, float *imin, float *imax)
  394. {
  395. struct gpu_adl *ga;
  396. if (!gpus[gpu].has_adl || !adl_active) {
  397. wlogprint("Get vddcrange not supported\n");
  398. return;
  399. }
  400. ga = &gpus[gpu].adl;
  401. *imin = (float)ga->lpOdParameters.sVddc.iMin / 1000;
  402. *imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
  403. }
  404. static float curses_float(const char *query)
  405. {
  406. float ret;
  407. char *cvar;
  408. cvar = curses_input(query);
  409. ret = atof(cvar);
  410. free(cvar);
  411. return ret;
  412. }
  413. static int set_vddc(int gpu, float fVddc)
  414. {
  415. ADLODPerformanceLevels *lpOdPerformanceLevels;
  416. struct gpu_adl *ga;
  417. int iVddc, lev;
  418. if (!gpus[gpu].has_adl || !adl_active) {
  419. wlogprint("Set vddc not supported\n");
  420. return 1;
  421. }
  422. iVddc = 1000 * fVddc;
  423. ga = &gpus[gpu].adl;
  424. if (iVddc > ga->lpOdParameters.sVddc.iMax ||
  425. iVddc < ga->lpOdParameters.sVddc.iMin)
  426. return 1;
  427. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  428. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  429. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  430. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  431. return 1;
  432. lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
  433. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  434. return 1;
  435. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  436. /* Reset to old value if it fails! */
  437. if (lpOdPerformanceLevels->aLevels[lev].iVddc != iVddc) {
  438. /* Set all the parameters in case they're linked somehow */
  439. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  440. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  441. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  442. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  443. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  444. return 1;
  445. }
  446. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  447. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  448. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  449. return 0;
  450. }
  451. static void get_fanrange(int gpu, int *imin, int *imax)
  452. {
  453. struct gpu_adl *ga;
  454. if (!gpus[gpu].has_adl || !adl_active) {
  455. wlogprint("Get fanrange not supported\n");
  456. return;
  457. }
  458. ga = &gpus[gpu].adl;
  459. *imin = ga->lpFanSpeedInfo.iMinPercent;
  460. *imax = ga->lpFanSpeedInfo.iMaxPercent;
  461. }
  462. static int set_fanspeed(int gpu, int iFanSpeed)
  463. {
  464. struct gpu_adl *ga;
  465. if (!gpus[gpu].has_adl || !adl_active) {
  466. wlogprint("Set fanspeed not supported\n");
  467. return 1;
  468. }
  469. ga = &gpus[gpu].adl;
  470. if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE )))
  471. return 1;
  472. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  473. return 1;
  474. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  475. if ((ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE) &&
  476. !(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE)) {
  477. /* Must convert speed to an RPM */
  478. iFanSpeed = ga->lpFanSpeedInfo.iMaxRPM * iFanSpeed / 100;
  479. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  480. } else
  481. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  482. ga->lpFanSpeedValue.iFanSpeed = iFanSpeed;
  483. if (ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  484. return 1;
  485. return 0;
  486. }
  487. void gpu_autotune(int gpu)
  488. {
  489. int temp, fanpercent, engine, newpercent, newengine;
  490. bool fan_optimal = true;
  491. struct gpu_adl *ga;
  492. if (!gpus[gpu].has_adl || !adl_active)
  493. return;
  494. temp = gpu_temp(gpu);
  495. newpercent = fanpercent = gpu_fanpercent(gpu);
  496. newengine = engine = gpu_engineclock(gpu) * 100;
  497. ga = &gpus[gpu].adl;
  498. if (temp && fanpercent >= 0 && ga->autofan) {
  499. if (temp > ga->overtemp && fanpercent < 100) {
  500. applog(LOG_WARNING, "Overhead detected, increasing fan to 100%");
  501. newpercent = 100;
  502. } else if (temp > ga->targettemp && fanpercent < 85) {
  503. if (opt_debug)
  504. applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
  505. newpercent = fanpercent + 5;
  506. } else if (fanpercent && temp < ga->targettemp - 5) {
  507. if (opt_debug)
  508. applog(LOG_DEBUG, "Temperature 5 degrees below target, decreasing fanspeed");
  509. newpercent = fanpercent - 1;
  510. }
  511. if (newpercent > 100)
  512. newpercent = 100;
  513. else if (newpercent < 0)
  514. newpercent = 0;
  515. if (newpercent != fanpercent) {
  516. fan_optimal = false;
  517. applog(LOG_INFO, "Setting GPU %d fan percentage to %d", gpu, newpercent);
  518. set_fanspeed(gpu, newpercent);
  519. }
  520. }
  521. if (engine && ga->autoengine) {
  522. if (temp > ga->overtemp && engine > ga->minspeed) {
  523. applog(LOG_WARNING, "Overheat detected, decreasing GPU clock speed");
  524. newengine = ga->minspeed;
  525. } else if (temp > ga->targettemp && engine > ga->minspeed && fan_optimal) {
  526. if (opt_debug)
  527. applog(LOG_DEBUG, "Temperature over target, decreasing clock speed");
  528. newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
  529. } else if (temp < ga->targettemp && engine < ga->maxspeed) {
  530. if (opt_debug)
  531. applog(LOG_DEBUG, "Temperature below target, increasing clock speed");
  532. newengine = engine + ga->lpOdParameters.sEngineClock.iStep;
  533. }
  534. if (newengine > ga->maxspeed)
  535. newengine = ga->maxspeed;
  536. else if (newengine < ga->minspeed)
  537. newengine = ga->minspeed;
  538. if (newengine != engine) {
  539. newengine /= 100;
  540. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, newengine);
  541. set_engineclock(gpu, newengine);
  542. }
  543. }
  544. }
  545. void set_defaultfan(int gpu)
  546. {
  547. struct gpu_adl *ga;
  548. if (!gpus[gpu].has_adl || !adl_active)
  549. return;
  550. ga = &gpus[gpu].adl;
  551. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  552. }
  553. void set_defaultengine(int gpu)
  554. {
  555. struct gpu_adl *ga;
  556. if (!gpus[gpu].has_adl || !adl_active)
  557. return;
  558. ga = &gpus[gpu].adl;
  559. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  560. }
  561. void change_autosettings(int gpu)
  562. {
  563. struct gpu_adl *ga = &gpus[gpu].adl;
  564. char input;
  565. wlogprint("Target temperature: %d\n", ga->targettemp);
  566. wlogprint("Overheat temperature: %d\n", ga->overtemp);
  567. wlogprint("Toggle [F]an auto [G]PU auto, change [T]arget [O]verheat\n");
  568. wlogprint("Or press any other key to continue\n");
  569. input = getch();
  570. if (!strncasecmp(&input, "f", 1)) {
  571. ga->autofan ^= true;
  572. wlogprint("Fan autotune is now %s\n", ga->autofan ? "enabled" : "disabled");
  573. if (!ga->autofan) {
  574. wlogprint("Resetting fan to startup settings\n");
  575. set_defaultfan(gpu);
  576. }
  577. } else if (!strncasecmp(&input, "g", 1)) {
  578. ga->autoengine ^= true;
  579. wlogprint("GPU engine clock autotune is now %s\n", ga->autoengine ? "enabled" : "disabled");
  580. if (!ga->autoengine) {
  581. wlogprint("Resetting GPU engine clock to startup settings\n");
  582. set_defaultengine(gpu);
  583. }
  584. }
  585. }
  586. void change_gpusettings(int gpu)
  587. {
  588. struct gpu_adl *ga = &gpus[gpu].adl;
  589. float fval, fmin = 0, fmax = 0;
  590. int val, imin = 0, imax = 0;
  591. char input;
  592. wlogprint("Temp: %.1f °C\nFan Speed: %d RPM\nEngine Clock: %d MHz\nMemory Clock: %d Mhz\nVddc: %.3f V\nActivity: %d%%\n",
  593. gpu_temp(gpu), gpu_fanspeed(gpu), gpu_engineclock(gpu), gpu_memclock(gpu), gpu_vddc(gpu), gpu_activity(gpu));
  594. wlogprint("Fan autotune is %s\n", ga->autofan ? "enabled" : "disabled");
  595. wlogprint("GPU engine clock autotune is %s\n", ga->autoengine ? "enabled" : "disabled");
  596. wlogprint("Change [A]utomatic [E]ngine [F]an [M]emory [V]oltage\n");
  597. wlogprint("Or press any other key to continue\n");
  598. input = getch();
  599. if (!strncasecmp(&input, "a", 1)) {
  600. change_autosettings(gpu);
  601. } else if (!strncasecmp(&input, "e", 1)) {
  602. get_enginerange(gpu, &imin, &imax);
  603. wlogprint("Enter GPU engine clock speed (%d - %d Mhz)", imin, imax);
  604. val = curses_int("");
  605. if (val < imin || val > imax) {
  606. wlogprint("Value is outside safe range, are you sure?\n");
  607. input = getch();
  608. if (strncasecmp(&input, "y", 1))
  609. return;
  610. }
  611. if (!set_engineclock(gpu, val))
  612. wlogprint("Successfully modified engine clock speed\n");
  613. else
  614. wlogprint("Failed to modify engine clock speed\n");
  615. } else if (!strncasecmp(&input, "f", 1)) {
  616. get_fanrange(gpu, &imin, &imax);
  617. wlogprint("Enter fan percentage (%d - %d %)", imin, imax);
  618. val = curses_int("");
  619. if (val < imin || val > imax) {
  620. wlogprint("Value is outside safe range, are you sure?\n");
  621. input = getch();
  622. if (strncasecmp(&input, "y", 1))
  623. return;
  624. }
  625. if (!set_fanspeed(gpu, val))
  626. wlogprint("Successfully modified fan speed\n");
  627. else
  628. wlogprint("Failed to modify fan speed\n");
  629. } else if (!strncasecmp(&input, "m", 1)) {
  630. get_memoryrange(gpu, &imin, &imax);
  631. wlogprint("Enter GPU memory clock speed (%d - %d Mhz)", imin, imax);
  632. val = curses_int("");
  633. if (val < imin || val > imax) {
  634. wlogprint("Value is outside safe range, are you sure?\n");
  635. input = getch();
  636. if (strncasecmp(&input, "y", 1))
  637. return;
  638. }
  639. if (!set_memoryclock(gpu, val))
  640. wlogprint("Successfully modified memory clock speed\n");
  641. else
  642. wlogprint("Failed to modify memory clock speed\n");
  643. } else if (!strncasecmp(&input, "v", 1)) {
  644. get_vddcrange(gpu, &fmin, &fmax);
  645. wlogprint("Enter GPU voltage (%.3f - %.3f V)", fmin, fmax);
  646. fval = curses_float("");
  647. if (fval < fmin || fval > fmax) {
  648. wlogprint("Value is outside safe range, are you sure?\n");
  649. input = getch();
  650. if (strncasecmp(&input, "y", 1))
  651. return;
  652. }
  653. if (!set_vddc(gpu, fval))
  654. wlogprint("Successfully modified voltage\n");
  655. else
  656. wlogprint("Failed to modify voltage\n");
  657. }
  658. wlogprint("Press any key to continue\n");
  659. input = getch();
  660. }
  661. void clear_adl(nDevs)
  662. {
  663. struct gpu_adl *ga;
  664. int i;
  665. if (!adl_active)
  666. return;
  667. /* Try to reset values to their defaults */
  668. for (i = 0; i < nDevs; i++) {
  669. ga = &gpus[i].adl;
  670. if (!gpus[i].has_adl)
  671. continue;
  672. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  673. free(ga->DefPerfLev);
  674. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  675. }
  676. ADL_Main_Memory_Free ( (void **)&lpInfo );
  677. ADL_Main_Control_Destroy ();
  678. #if defined (LINUX)
  679. dlclose(hDLL);
  680. #else
  681. FreeLibrary(hDLL);
  682. #endif
  683. }
  684. #endif /* HAVE_ADL */