adl.c 29 KB

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