adl.c 26 KB

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