adl.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. static inline float __gpu_temp(struct gpu_adl *ga)
  217. {
  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. float gpu_temp(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. return __gpu_temp(ga);
  229. }
  230. static inline int __gpu_engineclock(struct gpu_adl *ga)
  231. {
  232. return ga->lpActivity.iEngineClock / 100;
  233. }
  234. int gpu_engineclock(int gpu)
  235. {
  236. struct gpu_adl *ga;
  237. if (!gpus[gpu].has_adl || !adl_active)
  238. return 0;
  239. ga = &gpus[gpu].adl;
  240. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  241. return 0;
  242. return __gpu_engineclock(ga);
  243. }
  244. static inline int __gpu_memclock(struct gpu_adl *ga)
  245. {
  246. return ga->lpActivity.iMemoryClock / 100;
  247. }
  248. int gpu_memclock(int gpu)
  249. {
  250. struct gpu_adl *ga;
  251. if (!gpus[gpu].has_adl || !adl_active)
  252. return 0;
  253. ga = &gpus[gpu].adl;
  254. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  255. return 0;
  256. return __gpu_memclock(ga);
  257. }
  258. static inline float __gpu_vddc(struct gpu_adl *ga)
  259. {
  260. return (float)ga->lpActivity.iVddc / 1000;
  261. }
  262. float gpu_vddc(int gpu)
  263. {
  264. struct gpu_adl *ga;
  265. if (!gpus[gpu].has_adl || !adl_active)
  266. return 0;
  267. ga = &gpus[gpu].adl;
  268. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  269. return 0;
  270. return __gpu_vddc(ga);
  271. }
  272. static inline int __gpu_activity(struct gpu_adl *ga)
  273. {
  274. if (!ga->lpOdParameters.iActivityReportingSupported)
  275. return 0;
  276. return ga->lpActivity.iActivityPercent;
  277. }
  278. int gpu_activity(int gpu)
  279. {
  280. struct gpu_adl *ga;
  281. if (!gpus[gpu].has_adl || !adl_active)
  282. return 0;
  283. ga = &gpus[gpu].adl;
  284. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  285. return 0;
  286. return __gpu_activity(ga);
  287. }
  288. static inline int __gpu_fanspeed(struct gpu_adl *ga)
  289. {
  290. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_READ))
  291. return 0;
  292. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  293. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  294. return 0;
  295. return ga->lpFanSpeedValue.iFanSpeed;
  296. }
  297. int gpu_fanspeed(int gpu)
  298. {
  299. struct gpu_adl *ga;
  300. if (!gpus[gpu].has_adl || !adl_active)
  301. return 0;
  302. ga = &gpus[gpu].adl;
  303. return __gpu_fanspeed(ga);
  304. }
  305. static inline int __gpu_fanpercent(struct gpu_adl *ga)
  306. {
  307. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ ))
  308. return -1;
  309. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  310. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  311. return -1;
  312. return ga->lpFanSpeedValue.iFanSpeed;
  313. }
  314. int gpu_fanpercent(int gpu)
  315. {
  316. struct gpu_adl *ga;
  317. if (!gpus[gpu].has_adl || !adl_active)
  318. return -1;
  319. ga = &gpus[gpu].adl;
  320. return __gpu_fanpercent(ga);
  321. }
  322. bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc,
  323. int *activity, int *fanspeed, int *fanpercent)
  324. {
  325. struct gpu_adl *ga;
  326. if (!gpus[gpu].has_adl || !adl_active)
  327. return false;
  328. ga = &gpus[gpu].adl;
  329. *temp = __gpu_temp(ga);
  330. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK) {
  331. *engineclock = 0;
  332. *memclock = 0;
  333. *vddc = 0;
  334. *activity = 0;
  335. } else {
  336. *engineclock = __gpu_engineclock(ga);
  337. *memclock = __gpu_memclock(ga);
  338. *vddc = __gpu_vddc(ga);
  339. *activity = __gpu_activity(ga);
  340. }
  341. *fanspeed = __gpu_fanspeed(ga);
  342. *fanpercent = __gpu_fanpercent(ga);
  343. return true;
  344. }
  345. static void get_enginerange(int gpu, int *imin, int *imax)
  346. {
  347. struct gpu_adl *ga;
  348. if (!gpus[gpu].has_adl || !adl_active) {
  349. wlogprint("Get enginerange not supported\n");
  350. return;
  351. }
  352. ga = &gpus[gpu].adl;
  353. *imin = ga->lpOdParameters.sEngineClock.iMin / 100;
  354. *imax = ga->lpOdParameters.sEngineClock.iMax / 100;
  355. }
  356. static int set_engineclock(int gpu, int iEngineClock)
  357. {
  358. ADLODPerformanceLevels *lpOdPerformanceLevels;
  359. struct gpu_adl *ga;
  360. int lev;
  361. if (!gpus[gpu].has_adl || !adl_active) {
  362. wlogprint("Set engineclock not supported\n");
  363. return 1;
  364. }
  365. iEngineClock *= 100;
  366. ga = &gpus[gpu].adl;
  367. if (iEngineClock > ga->lpOdParameters.sEngineClock.iMax ||
  368. iEngineClock < ga->lpOdParameters.sEngineClock.iMin)
  369. return 1;
  370. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  371. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  372. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  373. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  374. return 1;
  375. lpOdPerformanceLevels->aLevels[lev].iEngineClock = iEngineClock;
  376. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  377. return 1;
  378. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  379. /* Reset to old value if it fails! */
  380. if (lpOdPerformanceLevels->aLevels[lev].iEngineClock != iEngineClock) {
  381. /* Set all the parameters in case they're linked somehow */
  382. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  383. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  384. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  385. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  386. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  387. return 1;
  388. }
  389. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  390. if (ga->iEngineClock > ga->maxspeed)
  391. ga->maxspeed = ga->iEngineClock;
  392. if (ga->iEngineClock < ga->minspeed)
  393. ga->minspeed = ga->iEngineClock;
  394. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  395. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  396. return 0;
  397. }
  398. static void get_memoryrange(int gpu, int *imin, int *imax)
  399. {
  400. struct gpu_adl *ga;
  401. if (!gpus[gpu].has_adl || !adl_active) {
  402. wlogprint("Get memoryrange not supported\n");
  403. return;
  404. }
  405. ga = &gpus[gpu].adl;
  406. *imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
  407. *imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
  408. }
  409. static int set_memoryclock(int gpu, int iMemoryClock)
  410. {
  411. ADLODPerformanceLevels *lpOdPerformanceLevels;
  412. struct gpu_adl *ga;
  413. int lev;
  414. if (!gpus[gpu].has_adl || !adl_active) {
  415. wlogprint("Set memoryclock not supported\n");
  416. return 1;
  417. }
  418. iMemoryClock *= 100;
  419. ga = &gpus[gpu].adl;
  420. if (iMemoryClock > ga->lpOdParameters.sMemoryClock.iMax ||
  421. iMemoryClock < ga->lpOdParameters.sMemoryClock.iMin)
  422. return 1;
  423. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  424. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  425. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  426. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  427. return 1;
  428. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
  429. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  430. return 1;
  431. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  432. /* Reset to old value if it fails! */
  433. if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock != iMemoryClock) {
  434. /* Set all the parameters in case they're linked somehow */
  435. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iEngineClock;
  436. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  437. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  438. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  439. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  440. return 1;
  441. }
  442. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  443. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  444. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  445. return 0;
  446. }
  447. static void get_vddcrange(int gpu, float *imin, float *imax)
  448. {
  449. struct gpu_adl *ga;
  450. if (!gpus[gpu].has_adl || !adl_active) {
  451. wlogprint("Get vddcrange not supported\n");
  452. return;
  453. }
  454. ga = &gpus[gpu].adl;
  455. *imin = (float)ga->lpOdParameters.sVddc.iMin / 1000;
  456. *imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
  457. }
  458. static float curses_float(const char *query)
  459. {
  460. float ret;
  461. char *cvar;
  462. cvar = curses_input(query);
  463. ret = atof(cvar);
  464. free(cvar);
  465. return ret;
  466. }
  467. static int set_vddc(int gpu, float fVddc)
  468. {
  469. ADLODPerformanceLevels *lpOdPerformanceLevels;
  470. struct gpu_adl *ga;
  471. int iVddc, lev;
  472. if (!gpus[gpu].has_adl || !adl_active) {
  473. wlogprint("Set vddc not supported\n");
  474. return 1;
  475. }
  476. iVddc = 1000 * fVddc;
  477. ga = &gpus[gpu].adl;
  478. if (iVddc > ga->lpOdParameters.sVddc.iMax ||
  479. iVddc < ga->lpOdParameters.sVddc.iMin)
  480. return 1;
  481. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  482. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  483. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  484. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  485. return 1;
  486. lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
  487. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  488. return 1;
  489. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  490. /* Reset to old value if it fails! */
  491. if (lpOdPerformanceLevels->aLevels[lev].iVddc != iVddc) {
  492. /* Set all the parameters in case they're linked somehow */
  493. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  494. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  495. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  496. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  497. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  498. return 1;
  499. }
  500. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  501. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  502. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  503. return 0;
  504. }
  505. static void get_fanrange(int gpu, int *imin, int *imax)
  506. {
  507. struct gpu_adl *ga;
  508. if (!gpus[gpu].has_adl || !adl_active) {
  509. wlogprint("Get fanrange not supported\n");
  510. return;
  511. }
  512. ga = &gpus[gpu].adl;
  513. *imin = ga->lpFanSpeedInfo.iMinPercent;
  514. *imax = ga->lpFanSpeedInfo.iMaxPercent;
  515. }
  516. static int set_fanspeed(int gpu, int iFanSpeed)
  517. {
  518. struct gpu_adl *ga;
  519. if (!gpus[gpu].has_adl || !adl_active) {
  520. wlogprint("Set fanspeed not supported\n");
  521. return 1;
  522. }
  523. ga = &gpus[gpu].adl;
  524. if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE )))
  525. return 1;
  526. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  527. return 1;
  528. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  529. if ((ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE) &&
  530. !(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE)) {
  531. /* Must convert speed to an RPM */
  532. iFanSpeed = ga->lpFanSpeedInfo.iMaxRPM * iFanSpeed / 100;
  533. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  534. } else
  535. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  536. ga->lpFanSpeedValue.iFanSpeed = iFanSpeed;
  537. if (ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  538. return 1;
  539. return 0;
  540. }
  541. void gpu_autotune(int gpu)
  542. {
  543. int temp, fanpercent, engine, newpercent, newengine;
  544. bool fan_optimal = true;
  545. struct gpu_adl *ga;
  546. if (!gpus[gpu].has_adl || !adl_active)
  547. return;
  548. ga = &gpus[gpu].adl;
  549. temp = __gpu_temp(ga);
  550. newpercent = fanpercent = __gpu_fanpercent(ga);
  551. newengine = engine = gpu_engineclock(gpu) * 100;
  552. if (temp && fanpercent >= 0 && ga->autofan) {
  553. if (temp > ga->overtemp && fanpercent < 100) {
  554. applog(LOG_WARNING, "Overheat detected, increasing fan to 100%");
  555. newpercent = 100;
  556. } else if (temp > ga->targettemp && fanpercent < 85) {
  557. if (opt_debug)
  558. applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
  559. newpercent = fanpercent + 5;
  560. } else if (fanpercent && temp < ga->targettemp - opt_hysteresis) {
  561. if (opt_debug)
  562. applog(LOG_DEBUG, "Temperature %d degrees below target, decreasing fanspeed", opt_hysteresis);
  563. newpercent = fanpercent - 1;
  564. }
  565. if (newpercent > 100)
  566. newpercent = 100;
  567. else if (newpercent < 0)
  568. newpercent = 0;
  569. if (newpercent != fanpercent) {
  570. fan_optimal = false;
  571. applog(LOG_INFO, "Setting GPU %d fan percentage to %d", gpu, newpercent);
  572. set_fanspeed(gpu, newpercent);
  573. }
  574. }
  575. if (engine && ga->autoengine) {
  576. if (temp > ga->overtemp && engine > ga->minspeed) {
  577. applog(LOG_WARNING, "Overheat detected, decreasing GPU clock speed");
  578. newengine = ga->minspeed;
  579. } else if (temp > ga->targettemp + opt_hysteresis && engine > ga->minspeed && fan_optimal) {
  580. if (opt_debug)
  581. applog(LOG_DEBUG, "Temperature %d degrees over target, decreasing clock speed", opt_hysteresis);
  582. newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
  583. } else if (temp < ga->targettemp && engine < ga->maxspeed) {
  584. if (opt_debug)
  585. applog(LOG_DEBUG, "Temperature below target, increasing clock speed");
  586. newengine = engine + ga->lpOdParameters.sEngineClock.iStep;
  587. }
  588. if (newengine > ga->maxspeed)
  589. newengine = ga->maxspeed;
  590. else if (newengine < ga->minspeed)
  591. newengine = ga->minspeed;
  592. if (newengine != engine) {
  593. newengine /= 100;
  594. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, newengine);
  595. set_engineclock(gpu, newengine);
  596. }
  597. }
  598. }
  599. void set_defaultfan(int gpu)
  600. {
  601. struct gpu_adl *ga;
  602. if (!gpus[gpu].has_adl || !adl_active)
  603. return;
  604. ga = &gpus[gpu].adl;
  605. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  606. }
  607. void set_defaultengine(int gpu)
  608. {
  609. struct gpu_adl *ga;
  610. if (!gpus[gpu].has_adl || !adl_active)
  611. return;
  612. ga = &gpus[gpu].adl;
  613. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  614. }
  615. void change_autosettings(int gpu)
  616. {
  617. struct gpu_adl *ga = &gpus[gpu].adl;
  618. char input;
  619. wlogprint("Target temperature: %d\n", ga->targettemp);
  620. wlogprint("Overheat temperature: %d\n", ga->overtemp);
  621. wlogprint("Toggle [F]an auto [G]PU auto, change [T]arget [O]verheat\n");
  622. wlogprint("Or press any other key to continue\n");
  623. input = getch();
  624. if (!strncasecmp(&input, "f", 1)) {
  625. ga->autofan ^= true;
  626. wlogprint("Fan autotune is now %s\n", ga->autofan ? "enabled" : "disabled");
  627. if (!ga->autofan) {
  628. wlogprint("Resetting fan to startup settings\n");
  629. set_defaultfan(gpu);
  630. }
  631. } else if (!strncasecmp(&input, "g", 1)) {
  632. ga->autoengine ^= true;
  633. wlogprint("GPU engine clock autotune is now %s\n", ga->autoengine ? "enabled" : "disabled");
  634. if (!ga->autoengine) {
  635. wlogprint("Resetting GPU engine clock to startup settings\n");
  636. set_defaultengine(gpu);
  637. }
  638. }
  639. }
  640. void change_gpusettings(int gpu)
  641. {
  642. struct gpu_adl *ga = &gpus[gpu].adl;
  643. float fval, fmin = 0, fmax = 0;
  644. int val, imin = 0, imax = 0;
  645. char input;
  646. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0;
  647. float temp = 0, vddc = 0;
  648. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent))
  649. wlogprint("Temp: %.1f °C\nFan Speed: %d%% (%d RPM)\nEngine Clock: %d MHz\nMemory Clock: %d Mhz\nVddc: %.3f V\nActivity: %d%%\n",
  650. temp, fanpercent, fanspeed, engineclock, memclock, vddc, activity);
  651. wlogprint("Fan autotune is %s\n", ga->autofan ? "enabled" : "disabled");
  652. wlogprint("GPU engine clock autotune is %s\n", ga->autoengine ? "enabled" : "disabled");
  653. wlogprint("Change [A]utomatic [E]ngine [F]an [M]emory [V]oltage\n");
  654. wlogprint("Or press any other key to continue\n");
  655. input = getch();
  656. if (!strncasecmp(&input, "a", 1)) {
  657. change_autosettings(gpu);
  658. } else if (!strncasecmp(&input, "e", 1)) {
  659. get_enginerange(gpu, &imin, &imax);
  660. wlogprint("Enter GPU engine clock speed (%d - %d Mhz)", imin, imax);
  661. val = curses_int("");
  662. if (val < imin || val > imax) {
  663. wlogprint("Value is outside safe range, are you sure?\n");
  664. input = getch();
  665. if (strncasecmp(&input, "y", 1))
  666. return;
  667. }
  668. if (!set_engineclock(gpu, val))
  669. wlogprint("Successfully modified engine clock speed\n");
  670. else
  671. wlogprint("Failed to modify engine clock speed\n");
  672. } else if (!strncasecmp(&input, "f", 1)) {
  673. get_fanrange(gpu, &imin, &imax);
  674. wlogprint("Enter fan percentage (%d - %d %)", imin, imax);
  675. val = curses_int("");
  676. if (val < imin || val > imax) {
  677. wlogprint("Value is outside safe range, are you sure?\n");
  678. input = getch();
  679. if (strncasecmp(&input, "y", 1))
  680. return;
  681. }
  682. if (!set_fanspeed(gpu, val))
  683. wlogprint("Successfully modified fan speed\n");
  684. else
  685. wlogprint("Failed to modify fan speed\n");
  686. } else if (!strncasecmp(&input, "m", 1)) {
  687. get_memoryrange(gpu, &imin, &imax);
  688. wlogprint("Enter GPU memory clock speed (%d - %d Mhz)", imin, imax);
  689. val = curses_int("");
  690. if (val < imin || val > imax) {
  691. wlogprint("Value is outside safe range, are you sure?\n");
  692. input = getch();
  693. if (strncasecmp(&input, "y", 1))
  694. return;
  695. }
  696. if (!set_memoryclock(gpu, val))
  697. wlogprint("Successfully modified memory clock speed\n");
  698. else
  699. wlogprint("Failed to modify memory clock speed\n");
  700. } else if (!strncasecmp(&input, "v", 1)) {
  701. get_vddcrange(gpu, &fmin, &fmax);
  702. wlogprint("Enter GPU voltage (%.3f - %.3f V)", fmin, fmax);
  703. fval = curses_float("");
  704. if (fval < fmin || fval > fmax) {
  705. wlogprint("Value is outside safe range, are you sure?\n");
  706. input = getch();
  707. if (strncasecmp(&input, "y", 1))
  708. return;
  709. }
  710. if (!set_vddc(gpu, fval))
  711. wlogprint("Successfully modified voltage\n");
  712. else
  713. wlogprint("Failed to modify voltage\n");
  714. }
  715. wlogprint("Press any key to continue\n");
  716. input = getch();
  717. }
  718. void clear_adl(nDevs)
  719. {
  720. struct gpu_adl *ga;
  721. int i;
  722. if (!adl_active)
  723. return;
  724. /* Try to reset values to their defaults */
  725. for (i = 0; i < nDevs; i++) {
  726. ga = &gpus[i].adl;
  727. if (!gpus[i].has_adl)
  728. continue;
  729. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  730. free(ga->DefPerfLev);
  731. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  732. }
  733. ADL_Main_Memory_Free ( (void **)&lpInfo );
  734. ADL_Main_Control_Destroy ();
  735. #if defined (LINUX)
  736. dlclose(hDLL);
  737. #else
  738. FreeLibrary(hDLL);
  739. #endif
  740. }
  741. #endif /* HAVE_ADL */