adl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. #include "miner.h"
  2. #include "adl.h"
  3. bool adl_active;
  4. #ifdef HAVE_ADL
  5. #if defined (__linux)
  6. #include "ADL_SDK/adl_sdk.h"
  7. #include <dlfcn.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #else
  11. #include <windows.h>
  12. #include <tchar.h>
  13. #include "ADL_SDK/adl_sdk.h"
  14. #endif
  15. #include <stdio.h>
  16. #include <curses.h>
  17. #include "adl_functions.h"
  18. // Memory allocation function
  19. static void * __stdcall ADL_Main_Memory_Alloc(int iSize)
  20. {
  21. void *lpBuffer = malloc(iSize);
  22. return lpBuffer;
  23. }
  24. // Optional Memory de-allocation function
  25. static void __stdcall ADL_Main_Memory_Free (void **lpBuffer)
  26. {
  27. if (*lpBuffer) {
  28. free (*lpBuffer);
  29. *lpBuffer = NULL;
  30. }
  31. }
  32. #if defined (LINUX)
  33. // equivalent functions in linux
  34. static void *GetProcAddress(void *pLibrary, const char *name)
  35. {
  36. return dlsym( pLibrary, name);
  37. }
  38. #endif
  39. static ADL_MAIN_CONTROL_CREATE ADL_Main_Control_Create;
  40. static ADL_MAIN_CONTROL_DESTROY ADL_Main_Control_Destroy;
  41. static ADL_ADAPTER_NUMBEROFADAPTERS_GET ADL_Adapter_NumberOfAdapters_Get;
  42. static ADL_ADAPTER_ADAPTERINFO_GET ADL_Adapter_AdapterInfo_Get;
  43. static ADL_ADAPTER_ID_GET ADL_Adapter_ID_Get;
  44. static ADL_OVERDRIVE5_TEMPERATURE_GET ADL_Overdrive5_Temperature_Get;
  45. static ADL_ADAPTER_ACTIVE_GET ADL_Adapter_Active_Get;
  46. static ADL_OVERDRIVE5_CURRENTACTIVITY_GET ADL_Overdrive5_CurrentActivity_Get;
  47. static ADL_OVERDRIVE5_ODPARAMETERS_GET ADL_Overdrive5_ODParameters_Get;
  48. static ADL_OVERDRIVE5_FANSPEEDINFO_GET ADL_Overdrive5_FanSpeedInfo_Get;
  49. static ADL_OVERDRIVE5_FANSPEED_GET ADL_Overdrive5_FanSpeed_Get;
  50. static ADL_OVERDRIVE5_FANSPEED_SET ADL_Overdrive5_FanSpeed_Set;
  51. static ADL_OVERDRIVE5_ODPERFORMANCELEVELS_GET ADL_Overdrive5_ODPerformanceLevels_Get;
  52. static ADL_OVERDRIVE5_ODPERFORMANCELEVELS_SET ADL_Overdrive5_ODPerformanceLevels_Set;
  53. static ADL_MAIN_CONTROL_REFRESH ADL_Main_Control_Refresh;
  54. #if defined (LINUX)
  55. static void *hDLL; // Handle to .so library
  56. #else
  57. HINSTANCE hDLL; // Handle to DLL
  58. #endif
  59. static int iNumberAdapters;
  60. static LPAdapterInfo lpInfo = NULL;
  61. void init_adl(int nDevs)
  62. {
  63. int i, devices = 0, last_adapter = -1;
  64. #if defined (LINUX)
  65. hDLL = dlopen( "libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
  66. #else
  67. hDLL = LoadLibrary("atiadlxx.dll");
  68. if (hDLL == NULL)
  69. // A 32 bit calling application on 64 bit OS will fail to LoadLIbrary.
  70. // Try to load the 32 bit library (atiadlxy.dll) instead
  71. hDLL = LoadLibrary("atiadlxy.dll");
  72. #endif
  73. if (hDLL == NULL) {
  74. applog(LOG_INFO, "Unable to load ati adl library");
  75. return;
  76. }
  77. ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE) GetProcAddress(hDLL,"ADL_Main_Control_Create");
  78. ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY) GetProcAddress(hDLL,"ADL_Main_Control_Destroy");
  79. ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET) GetProcAddress(hDLL,"ADL_Adapter_NumberOfAdapters_Get");
  80. ADL_Adapter_AdapterInfo_Get = (ADL_ADAPTER_ADAPTERINFO_GET) GetProcAddress(hDLL,"ADL_Adapter_AdapterInfo_Get");
  81. ADL_Adapter_ID_Get = (ADL_ADAPTER_ID_GET) GetProcAddress(hDLL,"ADL_Adapter_ID_Get");
  82. ADL_Overdrive5_Temperature_Get = (ADL_OVERDRIVE5_TEMPERATURE_GET) GetProcAddress(hDLL,"ADL_Overdrive5_Temperature_Get");
  83. ADL_Adapter_Active_Get = (ADL_ADAPTER_ACTIVE_GET) GetProcAddress(hDLL, "ADL_Adapter_Active_Get");
  84. ADL_Overdrive5_CurrentActivity_Get = (ADL_OVERDRIVE5_CURRENTACTIVITY_GET) GetProcAddress(hDLL, "ADL_Overdrive5_CurrentActivity_Get");
  85. ADL_Overdrive5_ODParameters_Get = (ADL_OVERDRIVE5_ODPARAMETERS_GET) GetProcAddress(hDLL, "ADL_Overdrive5_ODParameters_Get");
  86. ADL_Overdrive5_FanSpeedInfo_Get = (ADL_OVERDRIVE5_FANSPEEDINFO_GET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeedInfo_Get");
  87. ADL_Overdrive5_FanSpeed_Get = (ADL_OVERDRIVE5_FANSPEED_GET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeed_Get");
  88. ADL_Overdrive5_FanSpeed_Set = (ADL_OVERDRIVE5_FANSPEED_SET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeed_Set");
  89. ADL_Overdrive5_ODPerformanceLevels_Get = (ADL_OVERDRIVE5_ODPERFORMANCELEVELS_GET) GetProcAddress(hDLL, "ADL_Overdrive5_ODPerformanceLevels_Get");
  90. ADL_Overdrive5_ODPerformanceLevels_Set = (ADL_OVERDRIVE5_ODPERFORMANCELEVELS_SET) GetProcAddress(hDLL, "ADL_Overdrive5_ODPerformanceLevels_Set");
  91. ADL_Main_Control_Refresh = (ADL_MAIN_CONTROL_REFRESH) GetProcAddress(hDLL, "ADL_Main_Control_Refresh");
  92. if (!ADL_Main_Control_Create || !ADL_Main_Control_Destroy ||
  93. !ADL_Adapter_NumberOfAdapters_Get || !ADL_Adapter_AdapterInfo_Get ||
  94. !ADL_Adapter_ID_Get || !ADL_Overdrive5_Temperature_Get ||
  95. !ADL_Adapter_Active_Get || !ADL_Overdrive5_CurrentActivity_Get ||
  96. !ADL_Overdrive5_ODParameters_Get || !ADL_Overdrive5_FanSpeedInfo_Get ||
  97. !ADL_Overdrive5_FanSpeed_Get || !ADL_Overdrive5_FanSpeed_Set ||
  98. !ADL_Overdrive5_ODPerformanceLevels_Get || !ADL_Overdrive5_ODPerformanceLevels_Set ||
  99. !ADL_Main_Control_Refresh) {
  100. applog(LOG_INFO, "ATI ADL's API is missing");
  101. return;
  102. }
  103. // Initialise ADL. The second parameter is 1, which means:
  104. // retrieve adapter information only for adapters that are physically present and enabled in the system
  105. if (ADL_Main_Control_Create (ADL_Main_Memory_Alloc, 1) != ADL_OK) {
  106. applog(LOG_INFO, "ADL Initialisation Error!");
  107. return ;
  108. }
  109. if (ADL_Main_Control_Refresh() != ADL_OK) {
  110. applog(LOG_INFO, "ADL Refresh Error!");
  111. return ;
  112. }
  113. // Obtain the number of adapters for the system
  114. if (ADL_Adapter_NumberOfAdapters_Get ( &iNumberAdapters ) != ADL_OK) {
  115. applog(LOG_INFO, "Cannot get the number of adapters!\n");
  116. return ;
  117. }
  118. if (iNumberAdapters > 0) {
  119. lpInfo = malloc ( sizeof (AdapterInfo) * iNumberAdapters );
  120. memset ( lpInfo,'\0', sizeof (AdapterInfo) * iNumberAdapters );
  121. // Get the AdapterInfo structure for all adapters in the system
  122. if (ADL_Adapter_AdapterInfo_Get (lpInfo, sizeof (AdapterInfo) * iNumberAdapters) != ADL_OK) {
  123. applog(LOG_INFO, "ADL_Adapter_AdapterInfo_Get Error!");
  124. return ;
  125. }
  126. } else {
  127. applog(LOG_INFO, "No adapters found");
  128. return;
  129. }
  130. for ( i = 0; i < iNumberAdapters; i++ ) {
  131. struct gpu_adl *ga;
  132. int iAdapterIndex;
  133. int lpAdapterID;
  134. int lpStatus;
  135. ADLODPerformanceLevels *lpOdPerformanceLevels;
  136. int lev;
  137. iAdapterIndex = lpInfo[i].iAdapterIndex;
  138. /* Get unique identifier of the adapter */
  139. if (ADL_Adapter_ID_Get(iAdapterIndex, &lpAdapterID) != ADL_OK) {
  140. applog(LOG_INFO, "Failed to ADL_Adapter_ID_Get");
  141. continue;
  142. }
  143. if (!lpAdapterID)
  144. continue;
  145. if (lpAdapterID != last_adapter) {
  146. /* We found a truly new adapter instead of a logical
  147. * one. Now since there's no way of correlating the
  148. * opencl enumerated devices and the ADL enumerated
  149. * ones, we have to assume they're in the same order.*/
  150. if (++devices > nDevs) {
  151. applog(LOG_ERR, "ADL found more devices than opencl");
  152. return;
  153. }
  154. last_adapter = lpAdapterID;
  155. }
  156. /* See if the adapter is an AMD device with ADL active */
  157. if (ADL_Adapter_Active_Get(iAdapterIndex, &lpStatus) != ADL_OK) {
  158. applog(LOG_INFO, "Failed to ADL_Adapter_Active_Get");
  159. continue;
  160. }
  161. if (!lpStatus)
  162. continue;
  163. /* From here on we know this device is a discrete device and
  164. * should support ADL */
  165. ga = &gpus[devices - 1].adl;
  166. ga->iAdapterIndex = iAdapterIndex;
  167. ga->lpAdapterID = lpAdapterID;
  168. ga->lpStatus = lpStatus;
  169. if (ADL_Overdrive5_ODParameters_Get(iAdapterIndex, &ga->lpOdParameters) != ADL_OK) {
  170. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODParameters_Get");
  171. continue;
  172. }
  173. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  174. /* We're only interested in the top performance level */
  175. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  176. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  177. if (ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK) {
  178. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODPerformanceLevels_Get");
  179. continue;
  180. }
  181. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  182. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  183. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  184. if (ADL_Overdrive5_FanSpeedInfo_Get(iAdapterIndex, 0, &ga->lpFanSpeedInfo) != ADL_OK) {
  185. applog(LOG_INFO, "Failed to ADL_Overdrive5_FanSpeedInfo_Get");
  186. continue;
  187. }
  188. gpus[devices - 1].has_adl = true;
  189. }
  190. adl_active = true;
  191. }
  192. float gpu_temp(int gpu)
  193. {
  194. struct gpu_adl *ga;
  195. if (!gpus[gpu].has_adl || !adl_active)
  196. return 0;
  197. ga = &gpus[gpu].adl;
  198. if (ADL_Overdrive5_Temperature_Get(ga->iAdapterIndex, 0, &ga->lpTemperature) != ADL_OK)
  199. return 0;
  200. return (float)ga->lpTemperature.iTemperature / 1000;
  201. }
  202. int gpu_engineclock(int gpu)
  203. {
  204. struct gpu_adl *ga;
  205. if (!gpus[gpu].has_adl || !adl_active)
  206. return 0;
  207. ga = &gpus[gpu].adl;
  208. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  209. return 0;
  210. return ga->lpActivity.iEngineClock / 100;
  211. }
  212. int gpu_memclock(int gpu)
  213. {
  214. struct gpu_adl *ga;
  215. if (!gpus[gpu].has_adl || !adl_active)
  216. return 0;
  217. ga = &gpus[gpu].adl;
  218. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  219. return 0;
  220. return ga->lpActivity.iMemoryClock / 100;
  221. }
  222. float gpu_vddc(int gpu)
  223. {
  224. struct gpu_adl *ga;
  225. if (!gpus[gpu].has_adl || !adl_active)
  226. return 0;
  227. ga = &gpus[gpu].adl;
  228. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  229. return 0;
  230. return (float)ga->lpActivity.iVddc / 1000;
  231. }
  232. int gpu_activity(int gpu)
  233. {
  234. struct gpu_adl *ga;
  235. if (!gpus[gpu].has_adl || !adl_active)
  236. return 0;
  237. ga = &gpus[gpu].adl;
  238. if (!ga->lpOdParameters.iActivityReportingSupported)
  239. return 0;
  240. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  241. return 0;
  242. return ga->lpActivity.iActivityPercent;
  243. }
  244. int gpu_fanspeed(int gpu)
  245. {
  246. struct gpu_adl *ga;
  247. if (!gpus[gpu].has_adl || !adl_active)
  248. return 0;
  249. ga = &gpus[gpu].adl;
  250. if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_READ | ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ )))
  251. return 0;
  252. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  253. return 0;
  254. return ga->lpFanSpeedValue.iFanSpeed;
  255. }
  256. static void get_enginerange(int gpu, int *imin, int *imax)
  257. {
  258. struct gpu_adl *ga;
  259. if (!gpus[gpu].has_adl || !adl_active) {
  260. wlogprint("Get enginerange not supported\n");
  261. return;
  262. }
  263. ga = &gpus[gpu].adl;
  264. *imin = ga->lpOdParameters.sEngineClock.iMin / 100;
  265. *imax = ga->lpOdParameters.sEngineClock.iMax / 100;
  266. }
  267. static int set_engineclock(int gpu, int iEngineClock)
  268. {
  269. ADLODPerformanceLevels *lpOdPerformanceLevels;
  270. struct gpu_adl *ga;
  271. int lev;
  272. if (!gpus[gpu].has_adl || !adl_active) {
  273. wlogprint("Set engineclock not supported\n");
  274. return 1;
  275. }
  276. iEngineClock *= 100;
  277. ga = &gpus[gpu].adl;
  278. if (iEngineClock > ga->lpOdParameters.sEngineClock.iMax ||
  279. iEngineClock < ga->lpOdParameters.sEngineClock.iMin)
  280. return 1;
  281. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  282. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  283. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  284. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  285. return 1;
  286. lpOdPerformanceLevels->aLevels[lev].iEngineClock = iEngineClock;
  287. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  288. return 1;
  289. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  290. /* Reset to old value if it fails! */
  291. if (lpOdPerformanceLevels->aLevels[lev].iEngineClock != iEngineClock) {
  292. /* Set all the parameters in case they're linked somehow */
  293. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  294. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  295. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  296. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  297. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  298. return 1;
  299. }
  300. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  301. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  302. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  303. return 0;
  304. }
  305. static void get_memoryrange(int gpu, int *imin, int *imax)
  306. {
  307. struct gpu_adl *ga;
  308. if (!gpus[gpu].has_adl || !adl_active) {
  309. wlogprint("Get memoryrange not supported\n");
  310. return;
  311. }
  312. ga = &gpus[gpu].adl;
  313. *imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
  314. *imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
  315. }
  316. static int set_memoryclock(int gpu, int iMemoryClock)
  317. {
  318. ADLODPerformanceLevels *lpOdPerformanceLevels;
  319. struct gpu_adl *ga;
  320. int lev;
  321. if (!gpus[gpu].has_adl || !adl_active) {
  322. wlogprint("Set memoryclock not supported\n");
  323. return 1;
  324. }
  325. iMemoryClock *= 100;
  326. ga = &gpus[gpu].adl;
  327. if (iMemoryClock > ga->lpOdParameters.sMemoryClock.iMax ||
  328. iMemoryClock < ga->lpOdParameters.sMemoryClock.iMin)
  329. return 1;
  330. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  331. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  332. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  333. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  334. return 1;
  335. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
  336. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  337. return 1;
  338. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  339. /* Reset to old value if it fails! */
  340. if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock != iMemoryClock) {
  341. /* Set all the parameters in case they're linked somehow */
  342. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iEngineClock;
  343. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  344. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  345. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  346. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  347. return 1;
  348. }
  349. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  350. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  351. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  352. return 0;
  353. }
  354. static void get_vddcrange(int gpu, float *imin, float *imax)
  355. {
  356. struct gpu_adl *ga;
  357. if (!gpus[gpu].has_adl || !adl_active) {
  358. wlogprint("Get vddcrange not supported\n");
  359. return;
  360. }
  361. ga = &gpus[gpu].adl;
  362. *imin = (float)ga->lpOdParameters.sVddc.iMin / 1000;
  363. *imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
  364. }
  365. static float curses_float(const char *query)
  366. {
  367. float ret;
  368. char *cvar;
  369. cvar = curses_input(query);
  370. ret = atof(cvar);
  371. free(cvar);
  372. return ret;
  373. }
  374. static int set_vddc(int gpu, float fVddc)
  375. {
  376. ADLODPerformanceLevels *lpOdPerformanceLevels;
  377. struct gpu_adl *ga;
  378. int iVddc, lev;
  379. if (!gpus[gpu].has_adl || !adl_active) {
  380. wlogprint("Set vddc not supported\n");
  381. return 1;
  382. }
  383. iVddc = 1000 * fVddc;
  384. ga = &gpus[gpu].adl;
  385. if (iVddc > ga->lpOdParameters.sVddc.iMax ||
  386. iVddc < ga->lpOdParameters.sVddc.iMin)
  387. return 1;
  388. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  389. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  390. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  391. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  392. return 1;
  393. lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
  394. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  395. return 1;
  396. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  397. /* Reset to old value if it fails! */
  398. if (lpOdPerformanceLevels->aLevels[lev].iVddc != iVddc) {
  399. /* Set all the parameters in case they're linked somehow */
  400. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  401. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  402. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  403. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  404. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  405. return 1;
  406. }
  407. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  408. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  409. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  410. return 0;
  411. }
  412. static void get_fanrange(int gpu, int *imin, int *imax)
  413. {
  414. struct gpu_adl *ga;
  415. if (!gpus[gpu].has_adl || !adl_active) {
  416. wlogprint("Get fanrange not supported\n");
  417. return;
  418. }
  419. ga = &gpus[gpu].adl;
  420. *imin = ga->lpFanSpeedInfo.iMinPercent;
  421. *imax = ga->lpFanSpeedInfo.iMaxPercent;
  422. }
  423. static int set_fanspeed(int gpu, int iFanSpeed)
  424. {
  425. struct gpu_adl *ga;
  426. if (!gpus[gpu].has_adl || !adl_active) {
  427. wlogprint("Set fanspeed not supported\n");
  428. return 1;
  429. }
  430. ga = &gpus[gpu].adl;
  431. if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE )))
  432. return 1;
  433. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  434. return 1;
  435. if ((ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE) &&
  436. !(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE)) {
  437. /* Must convert speed to an RPM */
  438. iFanSpeed = ga->lpFanSpeedInfo.iMaxRPM * iFanSpeed / 100;
  439. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  440. }
  441. ga->lpFanSpeedValue.iFanSpeed = iFanSpeed;
  442. if (ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  443. return 1;
  444. return 0;
  445. }
  446. void change_gpusettings(int gpu)
  447. {
  448. int val, imin = 0, imax = 0;
  449. float fval, fmin = 0, fmax = 0;
  450. char input;
  451. wlogprint("Change [E]ngine [F]an [M]emory [V]oltage\n");
  452. wlogprint("Or press any other key to continue\n");
  453. input = getch();
  454. if (!strncasecmp(&input, "e", 1)) {
  455. get_enginerange(gpu, &imin, &imax);
  456. wlogprint("Enter GPU engine clock speed (%d - %d Mhz)", imin, imax);
  457. val = curses_int("");
  458. if (val < imin || val > imax) {
  459. wlogprint("Value is outside safe range, are you sure?\n");
  460. input = getch();
  461. if (strncasecmp(&input, "y", 1))
  462. return;
  463. }
  464. if (!set_engineclock(gpu, val))
  465. wlogprint("Successfully modified engine clock speed\n");
  466. else
  467. wlogprint("Failed to modify engine clock speed\n");
  468. } else if (!strncasecmp(&input, "f", 1)) {
  469. get_fanrange(gpu, &imin, &imax);
  470. wlogprint("Enter fan percentage (%d - %d %%)", imin, imax);
  471. val = curses_int("");
  472. if (val < imin || val > imax) {
  473. wlogprint("Value is outside safe range, are you sure?\n");
  474. input = getch();
  475. if (strncasecmp(&input, "y", 1))
  476. return;
  477. }
  478. if (!set_fanspeed(gpu, val))
  479. wlogprint("Successfully modified engine clock speed\n");
  480. else
  481. wlogprint("Failed to modify engine clock speed\n");
  482. } else if (!strncasecmp(&input, "m", 1)) {
  483. get_memoryrange(gpu, &imin, &imax);
  484. wlogprint("Enter GPU memory clock speed (%d - %d Mhz)", imin, imax);
  485. val = curses_int("");
  486. if (val < imin || val > imax) {
  487. wlogprint("Value is outside safe range, are you sure?\n");
  488. input = getch();
  489. if (strncasecmp(&input, "y", 1))
  490. return;
  491. }
  492. if (!set_memoryclock(gpu, val))
  493. wlogprint("Successfully modified memory clock speed\n");
  494. else
  495. wlogprint("Failed to modify memory clock speed\n");
  496. } else if (!strncasecmp(&input, "v", 1)) {
  497. get_vddcrange(gpu, &fmin, &fmax);
  498. wlogprint("Enter GPU voltage (%.3f - %.3f V)", fmin, fmax);
  499. fval = curses_float("");
  500. if (fval < fmin || fval > fmax) {
  501. wlogprint("Value is outside safe range, are you sure?\n");
  502. input = getch();
  503. if (strncasecmp(&input, "y", 1))
  504. return;
  505. }
  506. if (!set_vddc(gpu, fval))
  507. wlogprint("Successfully modified voltage\n");
  508. else
  509. wlogprint("Failed to modify voltage\n");
  510. }
  511. wlogprint("Press any key to continue\n");
  512. input = getch();
  513. }
  514. void clear_adl(void)
  515. {
  516. if (!adl_active)
  517. return;
  518. ADL_Main_Memory_Free ( (void **)&lpInfo );
  519. ADL_Main_Control_Destroy ();
  520. #if defined (LINUX)
  521. dlclose(hDLL);
  522. #else
  523. FreeLibrary(hDLL);
  524. #endif
  525. }
  526. #endif /* HAVE_ADL */