adl.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. if (ADL_Overdrive5_ODParameters_Get(iAdapterIndex, &ga->lpOdParameters) != ADL_OK) {
  170. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODParameters_Get");
  171. continue;
  172. }
  173. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  174. /* We're only interested in the top performance level */
  175. lpOdPerformanceLevels = malloc(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  176. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  177. /* Get default performance levels first */
  178. if (ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 1, lpOdPerformanceLevels) != ADL_OK) {
  179. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODPerformanceLevels_Get");
  180. continue;
  181. }
  182. /* Set the limits we'd use based on default gpu speeds */
  183. ga->maxspeed = ga->minspeed = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  184. /* Now get the current performance levels for any existing overclock */
  185. ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
  186. /* Save these values as the defaults in case we wish to reset to defaults */
  187. ga->DefPerfLev = lpOdPerformanceLevels;
  188. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  189. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  190. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  191. if (ga->iEngineClock < ga->minspeed)
  192. ga->minspeed = ga->iEngineClock;
  193. if (ga->iEngineClock > ga->maxspeed)
  194. ga->maxspeed = ga->iEngineClock;
  195. if (ADL_Overdrive5_FanSpeedInfo_Get(iAdapterIndex, 0, &ga->lpFanSpeedInfo) != ADL_OK) {
  196. applog(LOG_INFO, "Failed to ADL_Overdrive5_FanSpeedInfo_Get");
  197. continue;
  198. }
  199. /* Save the fanspeed values as defaults in case we reset later */
  200. ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  201. /* Set some default temperatures for autotune when enabled */
  202. ga->targettemp = 75;
  203. ga->overtemp = 85;
  204. if (opt_autofan)
  205. ga->autofan = true;
  206. if (opt_autoengine)
  207. ga->autoengine = true;
  208. gpus[devices - 1].has_adl = true;
  209. }
  210. adl_active = true;
  211. }
  212. float gpu_temp(int gpu)
  213. {
  214. struct gpu_adl *ga;
  215. if (!gpus[gpu].has_adl || !adl_active)
  216. return 0;
  217. ga = &gpus[gpu].adl;
  218. if (ADL_Overdrive5_Temperature_Get(ga->iAdapterIndex, 0, &ga->lpTemperature) != ADL_OK)
  219. return 0;
  220. return (float)ga->lpTemperature.iTemperature / 1000;
  221. }
  222. int gpu_engineclock(int gpu)
  223. {
  224. struct gpu_adl *ga;
  225. if (!gpus[gpu].has_adl || !adl_active)
  226. return 0;
  227. ga = &gpus[gpu].adl;
  228. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  229. return 0;
  230. return ga->lpActivity.iEngineClock / 100;
  231. }
  232. int gpu_memclock(int gpu)
  233. {
  234. struct gpu_adl *ga;
  235. if (!gpus[gpu].has_adl || !adl_active)
  236. return 0;
  237. ga = &gpus[gpu].adl;
  238. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  239. return 0;
  240. return ga->lpActivity.iMemoryClock / 100;
  241. }
  242. float gpu_vddc(int gpu)
  243. {
  244. struct gpu_adl *ga;
  245. if (!gpus[gpu].has_adl || !adl_active)
  246. return 0;
  247. ga = &gpus[gpu].adl;
  248. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  249. return 0;
  250. return (float)ga->lpActivity.iVddc / 1000;
  251. }
  252. int gpu_activity(int gpu)
  253. {
  254. struct gpu_adl *ga;
  255. if (!gpus[gpu].has_adl || !adl_active)
  256. return 0;
  257. ga = &gpus[gpu].adl;
  258. if (!ga->lpOdParameters.iActivityReportingSupported)
  259. return 0;
  260. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  261. return 0;
  262. return ga->lpActivity.iActivityPercent;
  263. }
  264. int gpu_fanspeed(int gpu)
  265. {
  266. struct gpu_adl *ga;
  267. if (!gpus[gpu].has_adl || !adl_active)
  268. return 0;
  269. ga = &gpus[gpu].adl;
  270. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_READ))
  271. return 0;
  272. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  273. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  274. return 0;
  275. return ga->lpFanSpeedValue.iFanSpeed;
  276. }
  277. static int gpu_fanpercent(int gpu)
  278. {
  279. struct gpu_adl *ga;
  280. if (!gpus[gpu].has_adl || !adl_active)
  281. return -1;
  282. ga = &gpus[gpu].adl;
  283. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ ))
  284. return -1;
  285. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  286. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  287. return -1;
  288. return ga->lpFanSpeedValue.iFanSpeed;
  289. }
  290. static void get_enginerange(int gpu, int *imin, int *imax)
  291. {
  292. struct gpu_adl *ga;
  293. if (!gpus[gpu].has_adl || !adl_active) {
  294. wlogprint("Get enginerange not supported\n");
  295. return;
  296. }
  297. ga = &gpus[gpu].adl;
  298. *imin = ga->lpOdParameters.sEngineClock.iMin / 100;
  299. *imax = ga->lpOdParameters.sEngineClock.iMax / 100;
  300. }
  301. static int set_engineclock(int gpu, int iEngineClock)
  302. {
  303. ADLODPerformanceLevels *lpOdPerformanceLevels;
  304. struct gpu_adl *ga;
  305. int lev;
  306. if (!gpus[gpu].has_adl || !adl_active) {
  307. wlogprint("Set engineclock not supported\n");
  308. return 1;
  309. }
  310. iEngineClock *= 100;
  311. ga = &gpus[gpu].adl;
  312. if (iEngineClock > ga->lpOdParameters.sEngineClock.iMax ||
  313. iEngineClock < ga->lpOdParameters.sEngineClock.iMin)
  314. return 1;
  315. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  316. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  317. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  318. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  319. return 1;
  320. lpOdPerformanceLevels->aLevels[lev].iEngineClock = iEngineClock;
  321. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  322. return 1;
  323. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  324. /* Reset to old value if it fails! */
  325. if (lpOdPerformanceLevels->aLevels[lev].iEngineClock != iEngineClock) {
  326. /* Set all the parameters in case they're linked somehow */
  327. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  328. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  329. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  330. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  331. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  332. return 1;
  333. }
  334. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  335. if (ga->iEngineClock > ga->maxspeed)
  336. ga->maxspeed = ga->iEngineClock;
  337. if (ga->iEngineClock < ga->minspeed)
  338. ga->minspeed = ga->iEngineClock;
  339. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  340. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  341. return 0;
  342. }
  343. static void get_memoryrange(int gpu, int *imin, int *imax)
  344. {
  345. struct gpu_adl *ga;
  346. if (!gpus[gpu].has_adl || !adl_active) {
  347. wlogprint("Get memoryrange not supported\n");
  348. return;
  349. }
  350. ga = &gpus[gpu].adl;
  351. *imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
  352. *imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
  353. }
  354. static int set_memoryclock(int gpu, int iMemoryClock)
  355. {
  356. ADLODPerformanceLevels *lpOdPerformanceLevels;
  357. struct gpu_adl *ga;
  358. int lev;
  359. if (!gpus[gpu].has_adl || !adl_active) {
  360. wlogprint("Set memoryclock not supported\n");
  361. return 1;
  362. }
  363. iMemoryClock *= 100;
  364. ga = &gpus[gpu].adl;
  365. if (iMemoryClock > ga->lpOdParameters.sMemoryClock.iMax ||
  366. iMemoryClock < ga->lpOdParameters.sMemoryClock.iMin)
  367. return 1;
  368. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  369. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  370. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  371. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  372. return 1;
  373. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
  374. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  375. return 1;
  376. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  377. /* Reset to old value if it fails! */
  378. if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock != iMemoryClock) {
  379. /* Set all the parameters in case they're linked somehow */
  380. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iEngineClock;
  381. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  382. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  383. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  384. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  385. return 1;
  386. }
  387. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  388. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  389. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  390. return 0;
  391. }
  392. static void get_vddcrange(int gpu, float *imin, float *imax)
  393. {
  394. struct gpu_adl *ga;
  395. if (!gpus[gpu].has_adl || !adl_active) {
  396. wlogprint("Get vddcrange not supported\n");
  397. return;
  398. }
  399. ga = &gpus[gpu].adl;
  400. *imin = (float)ga->lpOdParameters.sVddc.iMin / 1000;
  401. *imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
  402. }
  403. static float curses_float(const char *query)
  404. {
  405. float ret;
  406. char *cvar;
  407. cvar = curses_input(query);
  408. ret = atof(cvar);
  409. free(cvar);
  410. return ret;
  411. }
  412. static int set_vddc(int gpu, float fVddc)
  413. {
  414. ADLODPerformanceLevels *lpOdPerformanceLevels;
  415. struct gpu_adl *ga;
  416. int iVddc, lev;
  417. if (!gpus[gpu].has_adl || !adl_active) {
  418. wlogprint("Set vddc not supported\n");
  419. return 1;
  420. }
  421. iVddc = 1000 * fVddc;
  422. ga = &gpus[gpu].adl;
  423. if (iVddc > ga->lpOdParameters.sVddc.iMax ||
  424. iVddc < ga->lpOdParameters.sVddc.iMin)
  425. return 1;
  426. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  427. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  428. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  429. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  430. return 1;
  431. lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
  432. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  433. return 1;
  434. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  435. /* Reset to old value if it fails! */
  436. if (lpOdPerformanceLevels->aLevels[lev].iVddc != iVddc) {
  437. /* Set all the parameters in case they're linked somehow */
  438. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  439. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  440. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  441. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  442. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  443. return 1;
  444. }
  445. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  446. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  447. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  448. return 0;
  449. }
  450. static void get_fanrange(int gpu, int *imin, int *imax)
  451. {
  452. struct gpu_adl *ga;
  453. if (!gpus[gpu].has_adl || !adl_active) {
  454. wlogprint("Get fanrange not supported\n");
  455. return;
  456. }
  457. ga = &gpus[gpu].adl;
  458. *imin = ga->lpFanSpeedInfo.iMinPercent;
  459. *imax = ga->lpFanSpeedInfo.iMaxPercent;
  460. }
  461. static int set_fanspeed(int gpu, int iFanSpeed)
  462. {
  463. struct gpu_adl *ga;
  464. if (!gpus[gpu].has_adl || !adl_active) {
  465. wlogprint("Set fanspeed not supported\n");
  466. return 1;
  467. }
  468. ga = &gpus[gpu].adl;
  469. if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE )))
  470. return 1;
  471. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  472. return 1;
  473. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  474. if ((ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE) &&
  475. !(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE)) {
  476. /* Must convert speed to an RPM */
  477. iFanSpeed = ga->lpFanSpeedInfo.iMaxRPM * iFanSpeed / 100;
  478. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  479. } else
  480. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  481. ga->lpFanSpeedValue.iFanSpeed = iFanSpeed;
  482. if (ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  483. return 1;
  484. return 0;
  485. }
  486. void gpu_autotune(int gpu)
  487. {
  488. int temp, fanpercent, engine, newpercent, newengine;
  489. bool fan_optimal = true;
  490. struct gpu_adl *ga;
  491. if (!gpus[gpu].has_adl || !adl_active)
  492. return;
  493. temp = gpu_temp(gpu);
  494. newpercent = fanpercent = gpu_fanpercent(gpu);
  495. newengine = engine = gpu_engineclock(gpu) * 100;
  496. ga = &gpus[gpu].adl;
  497. if (temp && fanpercent >= 0 && ga->autofan) {
  498. if (temp > ga->overtemp && fanpercent < 100) {
  499. applog(LOG_WARNING, "Overhead detected, increasing fan to 100%");
  500. newpercent = 100;
  501. } else if (temp > ga->targettemp && fanpercent < 85) {
  502. if (opt_debug)
  503. applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
  504. newpercent = fanpercent + 5;
  505. } else if (fanpercent && temp < ga->targettemp - 5) {
  506. if (opt_debug)
  507. applog(LOG_DEBUG, "Temperature 5 degrees below target, decreasing fanspeed");
  508. newpercent = fanpercent - 1;
  509. }
  510. if (newpercent > 100)
  511. newpercent = 100;
  512. else if (newpercent < 0)
  513. newpercent = 0;
  514. if (newpercent != fanpercent) {
  515. fan_optimal = false;
  516. applog(LOG_INFO, "Setting GPU %d fan percentage to %d", gpu, newpercent);
  517. set_fanspeed(gpu, newpercent);
  518. }
  519. }
  520. if (engine && ga->autoengine) {
  521. if (temp > ga->overtemp && engine > ga->minspeed) {
  522. applog(LOG_WARNING, "Overheat detected, decreasing GPU clock speed");
  523. newengine = ga->minspeed;
  524. } else if (temp > ga->targettemp && engine > ga->minspeed && fan_optimal) {
  525. if (opt_debug)
  526. applog(LOG_DEBUG, "Temperature over target, decreasing clock speed");
  527. newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
  528. } else if (temp < ga->targettemp && engine < ga->maxspeed) {
  529. if (opt_debug)
  530. applog(LOG_DEBUG, "Temperature below target, increasing clock speed");
  531. newengine = engine + ga->lpOdParameters.sEngineClock.iStep;
  532. }
  533. if (newengine > ga->maxspeed)
  534. newengine = ga->maxspeed;
  535. else if (newengine < ga->minspeed)
  536. newengine = ga->minspeed;
  537. if (newengine != engine) {
  538. newengine /= 100;
  539. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, newengine);
  540. set_engineclock(gpu, newengine);
  541. }
  542. }
  543. }
  544. void set_defaultfan(int gpu)
  545. {
  546. struct gpu_adl *ga;
  547. if (!gpus[gpu].has_adl || !adl_active)
  548. return;
  549. ga = &gpus[gpu].adl;
  550. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  551. }
  552. void set_defaultengine(int gpu)
  553. {
  554. struct gpu_adl *ga;
  555. if (!gpus[gpu].has_adl || !adl_active)
  556. return;
  557. ga = &gpus[gpu].adl;
  558. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  559. }
  560. void change_autosettings(int gpu)
  561. {
  562. struct gpu_adl *ga = &gpus[gpu].adl;
  563. char input;
  564. wlogprint("Fan autotune is %s\n", ga->autofan ? "enabled" : "disabled");
  565. wlogprint("GPU engine clock autotune is %s\n", ga->autoengine ? "enabled" : "disabled");
  566. wlogprint("Target temperature: %d\n", ga->targettemp);
  567. wlogprint("Overheat temperature: %d\n", ga->overtemp);
  568. wlogprint("Toggle [F]an auto [G]PU auto, change [T]arget [O]verheat\n");
  569. wlogprint("Or press any other key to continue\n");
  570. input = getch();
  571. if (!strncasecmp(&input, "f", 1)) {
  572. ga->autofan ^= true;
  573. wlogprint("Fan autotune is now %s\n", ga->autofan ? "enabled" : "disabled");
  574. if (!ga->autofan) {
  575. wlogprint("Resetting fan to startup settings\n");
  576. set_defaultfan(gpu);
  577. }
  578. } else if (!strncasecmp(&input, "g", 1)) {
  579. ga->autoengine ^= true;
  580. wlogprint("GPU engine clock autotune is now %s\n", ga->autoengine ? "enabled" : "disabled");
  581. if (!ga->autoengine) {
  582. wlogprint("Resetting GPU engine clock to startup settings\n");
  583. set_defaultengine(gpu);
  584. }
  585. }
  586. }
  587. void change_gpusettings(int gpu)
  588. {
  589. int val, imin = 0, imax = 0;
  590. float fval, fmin = 0, fmax = 0;
  591. char input;
  592. wlogprint("Change [A]utomatic [E]ngine [F]an [M]emory [V]oltage\n");
  593. wlogprint("Or press any other key to continue\n");
  594. input = getch();
  595. if (!strncasecmp(&input, "a", 1)) {
  596. change_autosettings(gpu);
  597. } else if (!strncasecmp(&input, "e", 1)) {
  598. get_enginerange(gpu, &imin, &imax);
  599. wlogprint("Enter GPU engine clock speed (%d - %d Mhz)", imin, imax);
  600. val = curses_int("");
  601. if (val < imin || val > imax) {
  602. wlogprint("Value is outside safe range, are you sure?\n");
  603. input = getch();
  604. if (strncasecmp(&input, "y", 1))
  605. return;
  606. }
  607. if (!set_engineclock(gpu, val))
  608. wlogprint("Successfully modified engine clock speed\n");
  609. else
  610. wlogprint("Failed to modify engine clock speed\n");
  611. } else if (!strncasecmp(&input, "f", 1)) {
  612. get_fanrange(gpu, &imin, &imax);
  613. wlogprint("Enter fan percentage (%d - %d %)", imin, imax);
  614. val = curses_int("");
  615. if (val < imin || val > imax) {
  616. wlogprint("Value is outside safe range, are you sure?\n");
  617. input = getch();
  618. if (strncasecmp(&input, "y", 1))
  619. return;
  620. }
  621. if (!set_fanspeed(gpu, val))
  622. wlogprint("Successfully modified fan speed\n");
  623. else
  624. wlogprint("Failed to modify fan speed\n");
  625. } else if (!strncasecmp(&input, "m", 1)) {
  626. get_memoryrange(gpu, &imin, &imax);
  627. wlogprint("Enter GPU memory clock speed (%d - %d Mhz)", imin, imax);
  628. val = curses_int("");
  629. if (val < imin || val > imax) {
  630. wlogprint("Value is outside safe range, are you sure?\n");
  631. input = getch();
  632. if (strncasecmp(&input, "y", 1))
  633. return;
  634. }
  635. if (!set_memoryclock(gpu, val))
  636. wlogprint("Successfully modified memory clock speed\n");
  637. else
  638. wlogprint("Failed to modify memory clock speed\n");
  639. } else if (!strncasecmp(&input, "v", 1)) {
  640. get_vddcrange(gpu, &fmin, &fmax);
  641. wlogprint("Enter GPU voltage (%.3f - %.3f V)", fmin, fmax);
  642. fval = curses_float("");
  643. if (fval < fmin || fval > fmax) {
  644. wlogprint("Value is outside safe range, are you sure?\n");
  645. input = getch();
  646. if (strncasecmp(&input, "y", 1))
  647. return;
  648. }
  649. if (!set_vddc(gpu, fval))
  650. wlogprint("Successfully modified voltage\n");
  651. else
  652. wlogprint("Failed to modify voltage\n");
  653. }
  654. wlogprint("Press any key to continue\n");
  655. input = getch();
  656. }
  657. void clear_adl(void)
  658. {
  659. if (!adl_active)
  660. return;
  661. ADL_Main_Memory_Free ( (void **)&lpInfo );
  662. ADL_Main_Control_Destroy ();
  663. #if defined (LINUX)
  664. dlclose(hDLL);
  665. #else
  666. FreeLibrary(hDLL);
  667. #endif
  668. }
  669. #endif /* HAVE_ADL */