adl.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. #include "config.h"
  2. #if defined(HAVE_ADL) && (defined(__linux) || defined (WIN32))
  3. #include <stdio.h>
  4. #include <curses.h>
  5. #include "miner.h"
  6. #include "ADL_SDK/adl_sdk.h"
  7. #if defined (__linux)
  8. #include <dlfcn.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #else /* WIN32 */
  12. #include <windows.h>
  13. #include <tchar.h>
  14. #define sleep(x) Sleep(x)
  15. #endif
  16. #include "adl_functions.h"
  17. bool adl_active;
  18. int opt_hysteresis = 3;
  19. int opt_targettemp = 75;
  20. int opt_overheattemp = 85;
  21. static pthread_mutex_t adl_lock;
  22. // Memory allocation function
  23. static void * __stdcall ADL_Main_Memory_Alloc(int iSize)
  24. {
  25. void *lpBuffer = malloc(iSize);
  26. return lpBuffer;
  27. }
  28. // Optional Memory de-allocation function
  29. static void __stdcall ADL_Main_Memory_Free (void **lpBuffer)
  30. {
  31. if (*lpBuffer) {
  32. free (*lpBuffer);
  33. *lpBuffer = NULL;
  34. }
  35. }
  36. #if defined (LINUX)
  37. // equivalent functions in linux
  38. static void *GetProcAddress(void *pLibrary, const char *name)
  39. {
  40. return dlsym( pLibrary, name);
  41. }
  42. #endif
  43. static ADL_MAIN_CONTROL_CREATE ADL_Main_Control_Create;
  44. static ADL_MAIN_CONTROL_DESTROY ADL_Main_Control_Destroy;
  45. static ADL_ADAPTER_NUMBEROFADAPTERS_GET ADL_Adapter_NumberOfAdapters_Get;
  46. static ADL_ADAPTER_ADAPTERINFO_GET ADL_Adapter_AdapterInfo_Get;
  47. static ADL_ADAPTER_ID_GET ADL_Adapter_ID_Get;
  48. static ADL_OVERDRIVE5_TEMPERATURE_GET ADL_Overdrive5_Temperature_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. static ADL_OVERDRIVE5_POWERCONTROL_GET ADL_Overdrive5_PowerControl_Get;
  58. static ADL_OVERDRIVE5_POWERCONTROL_SET ADL_Overdrive5_PowerControl_Set;
  59. static ADL_ADAPTER_SPEED_GET ADL_Adapter_Speed_Get;
  60. static ADL_ADAPTER_SPEED_SET ADL_Adapter_Speed_Set;
  61. #if defined (LINUX)
  62. static void *hDLL; // Handle to .so library
  63. #else
  64. HINSTANCE hDLL; // Handle to DLL
  65. #endif
  66. static int iNumberAdapters;
  67. static LPAdapterInfo lpInfo = NULL;
  68. static int set_fanspeed(int gpu, int iFanSpeed);
  69. static inline void lock_adl(void)
  70. {
  71. mutex_lock(&adl_lock);
  72. }
  73. static inline void unlock_adl(void)
  74. {
  75. mutex_unlock(&adl_lock);
  76. }
  77. void init_adl(int nDevs)
  78. {
  79. int i, j, devices = 0, last_adapter = -1, gpu = 0, dummy = 0;
  80. #if defined (LINUX)
  81. hDLL = dlopen( "libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
  82. #else
  83. hDLL = LoadLibrary("atiadlxx.dll");
  84. if (hDLL == NULL)
  85. // A 32 bit calling application on 64 bit OS will fail to LoadLIbrary.
  86. // Try to load the 32 bit library (atiadlxy.dll) instead
  87. hDLL = LoadLibrary("atiadlxy.dll");
  88. #endif
  89. if (hDLL == NULL) {
  90. applog(LOG_INFO, "Unable to load ati adl library");
  91. return;
  92. }
  93. if (unlikely(pthread_mutex_init(&adl_lock, NULL))) {
  94. applog(LOG_ERR, "Failed to init adl_lock in init_adl");
  95. return;
  96. }
  97. ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE) GetProcAddress(hDLL,"ADL_Main_Control_Create");
  98. ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY) GetProcAddress(hDLL,"ADL_Main_Control_Destroy");
  99. ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET) GetProcAddress(hDLL,"ADL_Adapter_NumberOfAdapters_Get");
  100. ADL_Adapter_AdapterInfo_Get = (ADL_ADAPTER_ADAPTERINFO_GET) GetProcAddress(hDLL,"ADL_Adapter_AdapterInfo_Get");
  101. ADL_Adapter_ID_Get = (ADL_ADAPTER_ID_GET) GetProcAddress(hDLL,"ADL_Adapter_ID_Get");
  102. ADL_Overdrive5_Temperature_Get = (ADL_OVERDRIVE5_TEMPERATURE_GET) GetProcAddress(hDLL,"ADL_Overdrive5_Temperature_Get");
  103. ADL_Overdrive5_CurrentActivity_Get = (ADL_OVERDRIVE5_CURRENTACTIVITY_GET) GetProcAddress(hDLL, "ADL_Overdrive5_CurrentActivity_Get");
  104. ADL_Overdrive5_ODParameters_Get = (ADL_OVERDRIVE5_ODPARAMETERS_GET) GetProcAddress(hDLL, "ADL_Overdrive5_ODParameters_Get");
  105. ADL_Overdrive5_FanSpeedInfo_Get = (ADL_OVERDRIVE5_FANSPEEDINFO_GET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeedInfo_Get");
  106. ADL_Overdrive5_FanSpeed_Get = (ADL_OVERDRIVE5_FANSPEED_GET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeed_Get");
  107. ADL_Overdrive5_FanSpeed_Set = (ADL_OVERDRIVE5_FANSPEED_SET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeed_Set");
  108. ADL_Overdrive5_ODPerformanceLevels_Get = (ADL_OVERDRIVE5_ODPERFORMANCELEVELS_GET) GetProcAddress(hDLL, "ADL_Overdrive5_ODPerformanceLevels_Get");
  109. ADL_Overdrive5_ODPerformanceLevels_Set = (ADL_OVERDRIVE5_ODPERFORMANCELEVELS_SET) GetProcAddress(hDLL, "ADL_Overdrive5_ODPerformanceLevels_Set");
  110. ADL_Main_Control_Refresh = (ADL_MAIN_CONTROL_REFRESH) GetProcAddress(hDLL, "ADL_Main_Control_Refresh");
  111. ADL_Overdrive5_PowerControl_Get = (ADL_OVERDRIVE5_POWERCONTROL_GET) GetProcAddress(hDLL, "ADL_Overdrive5_PowerControl_Get");
  112. ADL_Overdrive5_PowerControl_Set = (ADL_OVERDRIVE5_POWERCONTROL_SET) GetProcAddress(hDLL, "ADL_Overdrive5_PowerControl_Set");
  113. ADL_Adapter_Speed_Get = (ADL_ADAPTER_SPEED_GET) GetProcAddress(hDLL, "ADL_Adapter_Speed_Get");
  114. ADL_Adapter_Speed_Set = (ADL_ADAPTER_SPEED_SET) GetProcAddress(hDLL, "ADL_Adapter_Speed_Set");
  115. if (!ADL_Main_Control_Create || !ADL_Main_Control_Destroy ||
  116. !ADL_Adapter_NumberOfAdapters_Get || !ADL_Adapter_AdapterInfo_Get ||
  117. !ADL_Adapter_ID_Get || !ADL_Overdrive5_Temperature_Get ||
  118. !ADL_Overdrive5_CurrentActivity_Get ||
  119. !ADL_Overdrive5_ODParameters_Get || !ADL_Overdrive5_FanSpeedInfo_Get ||
  120. !ADL_Overdrive5_FanSpeed_Get || !ADL_Overdrive5_FanSpeed_Set ||
  121. !ADL_Overdrive5_ODPerformanceLevels_Get || !ADL_Overdrive5_ODPerformanceLevels_Set ||
  122. !ADL_Main_Control_Refresh || !ADL_Overdrive5_PowerControl_Get ||
  123. !ADL_Overdrive5_PowerControl_Set || !ADL_Adapter_Speed_Get ||
  124. !ADL_Adapter_Speed_Set) {
  125. applog(LOG_WARNING, "ATI ADL's API is missing");
  126. return;
  127. }
  128. // Initialise ADL. The second parameter is 1, which means:
  129. // retrieve adapter information only for adapters that are physically present and enabled in the system
  130. if (ADL_Main_Control_Create (ADL_Main_Memory_Alloc, 1) != ADL_OK) {
  131. applog(LOG_INFO, "ADL Initialisation Error!");
  132. return ;
  133. }
  134. if (ADL_Main_Control_Refresh() != ADL_OK) {
  135. applog(LOG_INFO, "ADL Refresh Error!");
  136. return ;
  137. }
  138. // Obtain the number of adapters for the system
  139. if (ADL_Adapter_NumberOfAdapters_Get ( &iNumberAdapters ) != ADL_OK) {
  140. applog(LOG_INFO, "Cannot get the number of adapters!\n");
  141. return ;
  142. }
  143. if (iNumberAdapters > 0) {
  144. lpInfo = malloc ( sizeof (AdapterInfo) * iNumberAdapters );
  145. memset ( lpInfo,'\0', sizeof (AdapterInfo) * iNumberAdapters );
  146. // Get the AdapterInfo structure for all adapters in the system
  147. if (ADL_Adapter_AdapterInfo_Get (lpInfo, sizeof (AdapterInfo) * iNumberAdapters) != ADL_OK) {
  148. applog(LOG_INFO, "ADL_Adapter_AdapterInfo_Get Error!");
  149. return ;
  150. }
  151. } else {
  152. applog(LOG_INFO, "No adapters found");
  153. return;
  154. }
  155. for (i = 0; i < iNumberAdapters; i++) {
  156. struct gpu_adl *ga;
  157. int iAdapterIndex;
  158. int lpAdapterID;
  159. int lpStatus;
  160. ADLODPerformanceLevels *lpOdPerformanceLevels;
  161. int lev;
  162. iAdapterIndex = lpInfo[i].iAdapterIndex;
  163. /* Get unique identifier of the adapter, 0 means not AMD */
  164. if (ADL_Adapter_ID_Get(iAdapterIndex, &lpAdapterID) != ADL_OK) {
  165. applog(LOG_INFO, "Failed to ADL_Adapter_ID_Get");
  166. continue;
  167. }
  168. if (!lpAdapterID)
  169. continue;
  170. /* Each adapter may have multiple entries */
  171. if (lpAdapterID == last_adapter)
  172. continue;
  173. /* We found a truly new adapter instead of a logical
  174. * one. Now since there's no way of correlating the
  175. * opencl enumerated devices and the ADL enumerated
  176. * ones, we have to assume they're in the same order.*/
  177. if (++devices > nDevs) {
  178. applog(LOG_ERR, "ADL found more devices than opencl");
  179. return;
  180. }
  181. gpu = devices - 1;
  182. last_adapter = lpAdapterID;
  183. /* From here on we know this device is a discrete device and
  184. * should support ADL */
  185. ga = &gpus[gpu].adl;
  186. ga->iAdapterIndex = iAdapterIndex;
  187. ga->lpAdapterID = lpAdapterID;
  188. ga->DefPerfLev = NULL;
  189. /* Save whatever the current speed setting is to restore on exit */
  190. if (ADL_Adapter_Speed_Get(iAdapterIndex, &ga->lpCurrent, &dummy) != ADL_OK) {
  191. applog(LOG_INFO, "Failed to ADL_Adapter_Speed_Get");
  192. continue;
  193. }
  194. /* Force the speed to high, whether it's ignored or not */
  195. ADL_Adapter_Speed_Set(iAdapterIndex, ADL_CONTEXT_SPEED_FORCEHIGH);
  196. if (ADL_Overdrive5_ODParameters_Get(iAdapterIndex, &ga->lpOdParameters) != ADL_OK) {
  197. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODParameters_Get");
  198. continue;
  199. }
  200. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  201. /* We're only interested in the top performance level */
  202. lpOdPerformanceLevels = malloc(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  203. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  204. /* Get default performance levels first */
  205. if (ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 1, lpOdPerformanceLevels) != ADL_OK) {
  206. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODPerformanceLevels_Get");
  207. continue;
  208. }
  209. /* Set the limits we'd use based on default gpu speeds */
  210. ga->maxspeed = ga->minspeed = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  211. /* Now get the current performance levels for any existing overclock */
  212. ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
  213. /* Save these values as the defaults in case we wish to reset to defaults */
  214. ga->DefPerfLev = lpOdPerformanceLevels;
  215. if (gpus[gpu].gpu_engine) {
  216. int setengine = gpus[gpu].gpu_engine * 100;
  217. /* Lower profiles can't have a higher setting */
  218. for (j = 0; j < lev; j++) {
  219. if (lpOdPerformanceLevels->aLevels[j].iEngineClock > setengine)
  220. lpOdPerformanceLevels->aLevels[j].iEngineClock = setengine;
  221. }
  222. lpOdPerformanceLevels->aLevels[lev].iEngineClock = setengine;
  223. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, gpus[gpu].gpu_engine);
  224. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  225. }
  226. if (gpus[gpu].gpu_memclock) {
  227. int setmem = gpus[gpu].gpu_memclock * 100;
  228. for (j = 0; j < lev; j++) {
  229. if (lpOdPerformanceLevels->aLevels[j].iMemoryClock > setmem)
  230. lpOdPerformanceLevels->aLevels[j].iMemoryClock = setmem;
  231. }
  232. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = setmem;
  233. applog(LOG_INFO, "Setting GPU %d memory clock to %d", gpu, gpus[gpu].gpu_memclock);
  234. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  235. }
  236. if (gpus[gpu].gpu_vddc) {
  237. int setv = gpus[gpu].gpu_vddc * 1000;
  238. for (j = 0; j < lev; j++) {
  239. if (lpOdPerformanceLevels->aLevels[j].iVddc > setv)
  240. lpOdPerformanceLevels->aLevels[j].iVddc = setv;
  241. }
  242. lpOdPerformanceLevels->aLevels[lev].iVddc = setv;
  243. applog(LOG_INFO, "Setting GPU %d voltage to %.3f", gpu, gpus[gpu].gpu_vddc);
  244. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  245. }
  246. ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
  247. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  248. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  249. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  250. if (ga->iEngineClock < ga->minspeed)
  251. ga->minspeed = ga->iEngineClock;
  252. if (ga->iEngineClock > ga->maxspeed)
  253. ga->maxspeed = ga->iEngineClock;
  254. if (ADL_Overdrive5_FanSpeedInfo_Get(iAdapterIndex, 0, &ga->lpFanSpeedInfo) != ADL_OK) {
  255. applog(LOG_INFO, "Failed to ADL_Overdrive5_FanSpeedInfo_Get");
  256. continue;
  257. }
  258. /* Save the fanspeed values as defaults in case we reset later */
  259. ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  260. if (gpus[gpu].gpu_fan) {
  261. ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  262. ga->lpFanSpeedValue.iFanSpeed = gpus[gpu].gpu_fan;
  263. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  264. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  265. applog(LOG_INFO, "Setting GPU %d fan speed to %d%%", gpu, gpus[gpu].gpu_fan);
  266. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  267. }
  268. /* Not fatal if powercontrol get fails */
  269. if (ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy) != ADL_OK)
  270. applog(LOG_INFO, "Failed to ADL_Overdrive5_PowerControl_get");
  271. if (gpus[gpu].gpu_powertune) {
  272. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, gpus[gpu].gpu_powertune);
  273. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  274. }
  275. /* Set some default temperatures for autotune when enabled */
  276. ga->targettemp = opt_targettemp;
  277. ga->overtemp = opt_overheattemp;
  278. if (opt_autofan) {
  279. ga->autofan = true;
  280. /* Set a safe starting default if we're automanaging fan speeds */
  281. set_fanspeed(gpu, 85);
  282. }
  283. if (opt_autoengine)
  284. ga->autoengine = true;
  285. gpus[gpu].has_adl = true;
  286. }
  287. adl_active = true;
  288. }
  289. static inline float __gpu_temp(struct gpu_adl *ga)
  290. {
  291. if (ADL_Overdrive5_Temperature_Get(ga->iAdapterIndex, 0, &ga->lpTemperature) != ADL_OK)
  292. return 0;
  293. return (float)ga->lpTemperature.iTemperature / 1000;
  294. }
  295. float gpu_temp(int gpu)
  296. {
  297. struct gpu_adl *ga;
  298. float ret = 0;
  299. if (!gpus[gpu].has_adl || !adl_active)
  300. return ret;
  301. ga = &gpus[gpu].adl;
  302. lock_adl();
  303. ret = __gpu_temp(ga);
  304. unlock_adl();
  305. return ret;
  306. }
  307. static inline int __gpu_engineclock(struct gpu_adl *ga)
  308. {
  309. return ga->lpActivity.iEngineClock / 100;
  310. }
  311. int gpu_engineclock(int gpu)
  312. {
  313. struct gpu_adl *ga;
  314. int ret = 0;
  315. if (!gpus[gpu].has_adl || !adl_active)
  316. return ret;
  317. ga = &gpus[gpu].adl;
  318. lock_adl();
  319. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  320. goto out;
  321. ret = __gpu_engineclock(ga);
  322. out:
  323. unlock_adl();
  324. return ret;
  325. }
  326. static inline int __gpu_memclock(struct gpu_adl *ga)
  327. {
  328. return ga->lpActivity.iMemoryClock / 100;
  329. }
  330. int gpu_memclock(int gpu)
  331. {
  332. struct gpu_adl *ga;
  333. int ret = 0;
  334. if (!gpus[gpu].has_adl || !adl_active)
  335. return ret;
  336. ga = &gpus[gpu].adl;
  337. lock_adl();
  338. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  339. goto out;
  340. ret = __gpu_memclock(ga);
  341. out:
  342. unlock_adl();
  343. return ret;
  344. }
  345. static inline float __gpu_vddc(struct gpu_adl *ga)
  346. {
  347. return (float)ga->lpActivity.iVddc / 1000;
  348. }
  349. float gpu_vddc(int gpu)
  350. {
  351. struct gpu_adl *ga;
  352. float ret = 0;
  353. if (!gpus[gpu].has_adl || !adl_active)
  354. return ret;
  355. ga = &gpus[gpu].adl;
  356. lock_adl();
  357. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  358. goto out;
  359. ret = __gpu_vddc(ga);
  360. out:
  361. unlock_adl();
  362. return ret;
  363. }
  364. static inline int __gpu_activity(struct gpu_adl *ga)
  365. {
  366. if (!ga->lpOdParameters.iActivityReportingSupported)
  367. return 0;
  368. return ga->lpActivity.iActivityPercent;
  369. }
  370. int gpu_activity(int gpu)
  371. {
  372. struct gpu_adl *ga;
  373. int ret;
  374. if (!gpus[gpu].has_adl || !adl_active)
  375. return 0;
  376. ga = &gpus[gpu].adl;
  377. lock_adl();
  378. ret = ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity);
  379. unlock_adl();
  380. if (ret != ADL_OK)
  381. return 0;
  382. if (!ga->lpOdParameters.iActivityReportingSupported)
  383. return 0;
  384. return ga->lpActivity.iActivityPercent;
  385. }
  386. static inline int __gpu_fanspeed(struct gpu_adl *ga)
  387. {
  388. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_READ))
  389. return 0;
  390. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  391. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  392. return 0;
  393. return ga->lpFanSpeedValue.iFanSpeed;
  394. }
  395. int gpu_fanspeed(int gpu)
  396. {
  397. struct gpu_adl *ga;
  398. int ret = 0;
  399. if (!gpus[gpu].has_adl || !adl_active)
  400. return ret;
  401. ga = &gpus[gpu].adl;
  402. lock_adl();
  403. ret = __gpu_fanspeed(ga);
  404. unlock_adl();
  405. return ret;
  406. }
  407. static inline int __gpu_fanpercent(struct gpu_adl *ga)
  408. {
  409. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ ))
  410. return -1;
  411. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  412. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  413. return -1;
  414. return ga->lpFanSpeedValue.iFanSpeed;
  415. }
  416. int gpu_fanpercent(int gpu)
  417. {
  418. struct gpu_adl *ga;
  419. int ret = 0;
  420. if (!gpus[gpu].has_adl || !adl_active)
  421. return -1;
  422. ga = &gpus[gpu].adl;
  423. lock_adl();
  424. ret = __gpu_fanpercent(ga);
  425. unlock_adl();
  426. return ret;
  427. }
  428. static inline int __gpu_powertune(struct gpu_adl *ga)
  429. {
  430. int dummy = 0;
  431. if (ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy) != ADL_OK)
  432. return 0;
  433. return ga->iPercentage;
  434. }
  435. int gpu_powertune(int gpu)
  436. {
  437. struct gpu_adl *ga;
  438. int ret = -1;
  439. if (!gpus[gpu].has_adl || !adl_active)
  440. return ret;
  441. ga = &gpus[gpu].adl;
  442. lock_adl();
  443. ret = __gpu_powertune(ga);
  444. unlock_adl();
  445. return ret;
  446. }
  447. bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc,
  448. int *activity, int *fanspeed, int *fanpercent, int *powertune)
  449. {
  450. struct gpu_adl *ga;
  451. if (!gpus[gpu].has_adl || !adl_active)
  452. return false;
  453. ga = &gpus[gpu].adl;
  454. lock_adl();
  455. *temp = __gpu_temp(ga);
  456. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK) {
  457. *engineclock = 0;
  458. *memclock = 0;
  459. *vddc = 0;
  460. *activity = 0;
  461. } else {
  462. *engineclock = __gpu_engineclock(ga);
  463. *memclock = __gpu_memclock(ga);
  464. *vddc = __gpu_vddc(ga);
  465. *activity = __gpu_activity(ga);
  466. }
  467. *fanspeed = __gpu_fanspeed(ga);
  468. *fanpercent = __gpu_fanpercent(ga);
  469. *powertune = __gpu_powertune(ga);
  470. unlock_adl();
  471. return true;
  472. }
  473. static void get_enginerange(int gpu, int *imin, int *imax)
  474. {
  475. struct gpu_adl *ga;
  476. if (!gpus[gpu].has_adl || !adl_active) {
  477. wlogprint("Get enginerange not supported\n");
  478. return;
  479. }
  480. ga = &gpus[gpu].adl;
  481. *imin = ga->lpOdParameters.sEngineClock.iMin / 100;
  482. *imax = ga->lpOdParameters.sEngineClock.iMax / 100;
  483. }
  484. static int set_engineclock(int gpu, int iEngineClock)
  485. {
  486. ADLODPerformanceLevels *lpOdPerformanceLevels;
  487. int i, lev, ret = 1;
  488. struct gpu_adl *ga;
  489. if (!gpus[gpu].has_adl || !adl_active) {
  490. wlogprint("Set engineclock not supported\n");
  491. return ret;
  492. }
  493. iEngineClock *= 100;
  494. ga = &gpus[gpu].adl;
  495. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  496. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  497. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  498. lock_adl();
  499. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  500. goto out;
  501. for (i = 0; i < lev; i++) {
  502. if (lpOdPerformanceLevels->aLevels[i].iEngineClock > iEngineClock)
  503. lpOdPerformanceLevels->aLevels[i].iEngineClock = iEngineClock;
  504. }
  505. lpOdPerformanceLevels->aLevels[lev].iEngineClock = iEngineClock;
  506. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  507. goto out;
  508. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  509. /* Reset to old value if it fails! */
  510. if (lpOdPerformanceLevels->aLevels[lev].iEngineClock != iEngineClock) {
  511. /* Set all the parameters in case they're linked somehow */
  512. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  513. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  514. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  515. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  516. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  517. goto out;
  518. }
  519. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  520. if (ga->iEngineClock > ga->maxspeed)
  521. ga->maxspeed = ga->iEngineClock;
  522. if (ga->iEngineClock < ga->minspeed)
  523. ga->minspeed = ga->iEngineClock;
  524. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  525. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  526. ret = 0;
  527. out:
  528. unlock_adl();
  529. return ret;
  530. }
  531. static void get_memoryrange(int gpu, int *imin, int *imax)
  532. {
  533. struct gpu_adl *ga;
  534. if (!gpus[gpu].has_adl || !adl_active) {
  535. wlogprint("Get memoryrange not supported\n");
  536. return;
  537. }
  538. ga = &gpus[gpu].adl;
  539. *imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
  540. *imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
  541. }
  542. static int set_memoryclock(int gpu, int iMemoryClock)
  543. {
  544. ADLODPerformanceLevels *lpOdPerformanceLevels;
  545. int i, lev, ret = 1;
  546. struct gpu_adl *ga;
  547. if (!gpus[gpu].has_adl || !adl_active) {
  548. wlogprint("Set memoryclock not supported\n");
  549. return ret;
  550. }
  551. iMemoryClock *= 100;
  552. ga = &gpus[gpu].adl;
  553. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  554. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  555. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  556. lock_adl();
  557. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  558. goto out;
  559. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
  560. for (i = 0; i < lev; i++) {
  561. if (lpOdPerformanceLevels->aLevels[i].iMemoryClock > iMemoryClock)
  562. lpOdPerformanceLevels->aLevels[i].iMemoryClock = iMemoryClock;
  563. }
  564. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  565. goto out;
  566. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  567. /* Reset to old value if it fails! */
  568. if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock != iMemoryClock) {
  569. /* Set all the parameters in case they're linked somehow */
  570. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iEngineClock;
  571. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  572. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  573. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  574. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  575. goto out;
  576. }
  577. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  578. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  579. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  580. ret = 0;
  581. out:
  582. unlock_adl();
  583. return ret;
  584. }
  585. static void get_vddcrange(int gpu, float *imin, float *imax)
  586. {
  587. struct gpu_adl *ga;
  588. if (!gpus[gpu].has_adl || !adl_active) {
  589. wlogprint("Get vddcrange not supported\n");
  590. return;
  591. }
  592. ga = &gpus[gpu].adl;
  593. *imin = (float)ga->lpOdParameters.sVddc.iMin / 1000;
  594. *imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
  595. }
  596. static float curses_float(const char *query)
  597. {
  598. float ret;
  599. char *cvar;
  600. cvar = curses_input(query);
  601. ret = atof(cvar);
  602. free(cvar);
  603. return ret;
  604. }
  605. static int set_vddc(int gpu, float fVddc)
  606. {
  607. ADLODPerformanceLevels *lpOdPerformanceLevels;
  608. int i, iVddc, lev, ret = 1;
  609. struct gpu_adl *ga;
  610. if (!gpus[gpu].has_adl || !adl_active) {
  611. wlogprint("Set vddc not supported\n");
  612. return ret;
  613. }
  614. iVddc = 1000 * fVddc;
  615. ga = &gpus[gpu].adl;
  616. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  617. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  618. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  619. lock_adl();
  620. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  621. goto out;
  622. for (i = 0; i < lev; i++) {
  623. if (lpOdPerformanceLevels->aLevels[i].iVddc > iVddc)
  624. lpOdPerformanceLevels->aLevels[i].iVddc = iVddc;
  625. }
  626. lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
  627. if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
  628. goto out;
  629. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  630. /* Reset to old value if it fails! */
  631. if (lpOdPerformanceLevels->aLevels[lev].iVddc != iVddc) {
  632. /* Set all the parameters in case they're linked somehow */
  633. lpOdPerformanceLevels->aLevels[lev].iEngineClock = ga->iEngineClock;
  634. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = ga->iMemoryClock;
  635. lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
  636. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  637. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  638. goto out;
  639. }
  640. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  641. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  642. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  643. ret = 0;
  644. out:
  645. unlock_adl();
  646. return ret;
  647. }
  648. static void get_fanrange(int gpu, int *imin, int *imax)
  649. {
  650. struct gpu_adl *ga;
  651. if (!gpus[gpu].has_adl || !adl_active) {
  652. wlogprint("Get fanrange not supported\n");
  653. return;
  654. }
  655. ga = &gpus[gpu].adl;
  656. *imin = ga->lpFanSpeedInfo.iMinPercent;
  657. *imax = ga->lpFanSpeedInfo.iMaxPercent;
  658. }
  659. static int set_fanspeed(int gpu, int iFanSpeed)
  660. {
  661. struct gpu_adl *ga;
  662. int ret = 1;
  663. if (!gpus[gpu].has_adl || !adl_active) {
  664. wlogprint("Set fanspeed not supported\n");
  665. return ret;
  666. }
  667. ga = &gpus[gpu].adl;
  668. if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE )))
  669. return ret;
  670. lock_adl();
  671. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  672. goto out;
  673. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  674. if ((ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE) &&
  675. !(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE)) {
  676. /* Must convert speed to an RPM */
  677. iFanSpeed = ga->lpFanSpeedInfo.iMaxRPM * iFanSpeed / 100;
  678. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  679. } else
  680. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  681. ga->lpFanSpeedValue.iFanSpeed = iFanSpeed;
  682. if (ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  683. goto out;
  684. ret = 0;
  685. out:
  686. unlock_adl();
  687. return ret;
  688. }
  689. static int set_powertune(int gpu, int iPercentage)
  690. {
  691. int oldPercentage, dummy;
  692. struct gpu_adl *ga;
  693. int ret = 1;
  694. if (!gpus[gpu].has_adl || !adl_active) {
  695. wlogprint("Set powertune not supported\n");
  696. return ret;
  697. }
  698. ga = &gpus[gpu].adl;
  699. oldPercentage = ga->iPercentage;
  700. lock_adl();
  701. if (ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, iPercentage) != ADL_OK) {
  702. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, ga->iPercentage);
  703. goto out;
  704. }
  705. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  706. ret = 0;
  707. out:
  708. unlock_adl();
  709. return ret;
  710. }
  711. void gpu_autotune(int gpu)
  712. {
  713. int temp, fanpercent, engine, newpercent, newengine;
  714. bool fan_optimal = true;
  715. struct gpu_adl *ga;
  716. if (!gpus[gpu].has_adl || !adl_active)
  717. return;
  718. ga = &gpus[gpu].adl;
  719. lock_adl();
  720. temp = __gpu_temp(ga);
  721. newpercent = fanpercent = __gpu_fanpercent(ga);
  722. unlock_adl();
  723. newengine = engine = gpu_engineclock(gpu) * 100;
  724. if (temp && fanpercent >= 0 && ga->autofan) {
  725. if (temp > ga->overtemp && fanpercent < 100) {
  726. applog(LOG_WARNING, "Overheat detected, increasing fan to 100%");
  727. newpercent = 100;
  728. } else if (temp > ga->targettemp && fanpercent < 85) {
  729. if (opt_debug)
  730. applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
  731. newpercent = fanpercent + 10;
  732. if (newpercent > 85)
  733. newpercent = 85;
  734. } else if (fanpercent && temp < ga->targettemp - opt_hysteresis) {
  735. if (opt_debug)
  736. applog(LOG_DEBUG, "Temperature %d degrees below target, decreasing fanspeed", opt_hysteresis);
  737. newpercent = fanpercent - 1;
  738. }
  739. if (newpercent > 100)
  740. newpercent = 100;
  741. else if (newpercent < 0)
  742. newpercent = 0;
  743. if (newpercent != fanpercent) {
  744. fan_optimal = false;
  745. applog(LOG_INFO, "Setting GPU %d fan percentage to %d", gpu, newpercent);
  746. set_fanspeed(gpu, newpercent);
  747. }
  748. }
  749. if (engine && ga->autoengine) {
  750. if (temp > ga->overtemp && engine > ga->minspeed) {
  751. applog(LOG_WARNING, "Overheat detected, decreasing GPU clock speed");
  752. newengine = ga->minspeed;
  753. } else if (temp > ga->targettemp + opt_hysteresis && engine > ga->minspeed && fan_optimal) {
  754. if (opt_debug)
  755. applog(LOG_DEBUG, "Temperature %d degrees over target, decreasing clock speed", opt_hysteresis);
  756. newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
  757. } else if (temp < ga->targettemp && engine < ga->maxspeed) {
  758. if (opt_debug)
  759. applog(LOG_DEBUG, "Temperature below target, increasing clock speed");
  760. newengine = engine + ga->lpOdParameters.sEngineClock.iStep;
  761. }
  762. if (newengine > ga->maxspeed)
  763. newengine = ga->maxspeed;
  764. else if (newengine < ga->minspeed)
  765. newengine = ga->minspeed;
  766. if (newengine != engine) {
  767. newengine /= 100;
  768. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, newengine);
  769. set_engineclock(gpu, newengine);
  770. }
  771. }
  772. }
  773. void set_defaultfan(int gpu)
  774. {
  775. struct gpu_adl *ga;
  776. if (!gpus[gpu].has_adl || !adl_active)
  777. return;
  778. ga = &gpus[gpu].adl;
  779. lock_adl();
  780. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  781. unlock_adl();
  782. }
  783. void set_defaultengine(int gpu)
  784. {
  785. struct gpu_adl *ga;
  786. if (!gpus[gpu].has_adl || !adl_active)
  787. return;
  788. ga = &gpus[gpu].adl;
  789. lock_adl();
  790. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  791. unlock_adl();
  792. }
  793. void change_autosettings(int gpu)
  794. {
  795. struct gpu_adl *ga = &gpus[gpu].adl;
  796. char input;
  797. int val;
  798. wlogprint("Target temperature: %d\n", ga->targettemp);
  799. wlogprint("Overheat temperature: %d\n", ga->overtemp);
  800. wlogprint("Hysteresis differece: %d\n", opt_hysteresis);
  801. wlogprint("Toggle [F]an auto [G]PU auto\nChange [T]arget [O]verheat [H]ysteresis\n");
  802. wlogprint("Or press any other key to continue\n");
  803. input = getch();
  804. if (!strncasecmp(&input, "f", 1)) {
  805. ga->autofan ^= true;
  806. wlogprint("Fan autotune is now %s\n", ga->autofan ? "enabled" : "disabled");
  807. if (!ga->autofan) {
  808. wlogprint("Resetting fan to startup settings\n");
  809. set_defaultfan(gpu);
  810. }
  811. } else if (!strncasecmp(&input, "g", 1)) {
  812. ga->autoengine ^= true;
  813. wlogprint("GPU engine clock autotune is now %s\n", ga->autoengine ? "enabled" : "disabled");
  814. if (!ga->autoengine) {
  815. wlogprint("Resetting GPU engine clock to startup settings\n");
  816. set_defaultengine(gpu);
  817. }
  818. } else if (!strncasecmp(&input, "t", 1)) {
  819. val = curses_int("Enter target temperature for this GPU in C (0-100)");
  820. if (val < 0 || val > 100)
  821. wlogprint("Invalid temperature");
  822. else
  823. ga->targettemp = val;
  824. } else if (!strncasecmp(&input, "o", 1)) {
  825. wlogprint("Enter oveheat temperature for this GPU in C (%d-100)", ga->targettemp);
  826. val = curses_int("");
  827. if (val <= ga->targettemp || val > 100)
  828. wlogprint("Invalid temperature");
  829. else
  830. ga->overtemp = val;
  831. } else if (!strncasecmp(&input, "h", 1)) {
  832. val = curses_int("Enter hysteresis temperature difference (0-10)");
  833. if (val < 1 || val > 10)
  834. wlogprint("Invalid value");
  835. else
  836. opt_hysteresis = val;
  837. }
  838. }
  839. void change_gpusettings(int gpu)
  840. {
  841. struct gpu_adl *ga = &gpus[gpu].adl;
  842. float fval, fmin = 0, fmax = 0;
  843. int val, imin = 0, imax = 0;
  844. char input;
  845. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  846. float temp = 0, vddc = 0;
  847. updated:
  848. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune))
  849. wlogprint("Temp: %.1f C\nFan Speed: %d%% (%d RPM)\nEngine Clock: %d MHz\n"
  850. "Memory Clock: %d Mhz\nVddc: %.3f V\nActivity: %d%%\nPowertune: %d%%\n",
  851. temp, fanpercent, fanspeed, engineclock, memclock, vddc, activity, powertune);
  852. wlogprint("Fan autotune is %s\n", ga->autofan ? "enabled" : "disabled");
  853. wlogprint("GPU engine clock autotune is %s\n", ga->autoengine ? "enabled" : "disabled");
  854. wlogprint("Change [A]utomatic [E]ngine [F]an [M]emory [V]oltage [P]owertune\n");
  855. wlogprint("Or press any other key to continue\n");
  856. input = getch();
  857. if (!strncasecmp(&input, "a", 1)) {
  858. change_autosettings(gpu);
  859. } else if (!strncasecmp(&input, "e", 1)) {
  860. get_enginerange(gpu, &imin, &imax);
  861. wlogprint("Enter GPU engine clock speed (%d - %d Mhz)", imin, imax);
  862. val = curses_int("");
  863. if (val < imin || val > imax) {
  864. wlogprint("Value is outside safe range, are you sure?\n");
  865. input = getch();
  866. if (strncasecmp(&input, "y", 1))
  867. return;
  868. }
  869. if (!set_engineclock(gpu, val))
  870. wlogprint("Driver reports success but check values below\n");
  871. else
  872. wlogprint("Failed to modify engine clock speed\n");
  873. } else if (!strncasecmp(&input, "f", 1)) {
  874. get_fanrange(gpu, &imin, &imax);
  875. wlogprint("Enter fan percentage (%d - %d %)", imin, imax);
  876. val = curses_int("");
  877. if (val < imin || val > imax) {
  878. wlogprint("Value is outside safe range, are you sure?\n");
  879. input = getch();
  880. if (strncasecmp(&input, "y", 1))
  881. return;
  882. }
  883. if (!set_fanspeed(gpu, val))
  884. wlogprint("Driver reports success but check values below\n");
  885. else
  886. wlogprint("Failed to modify fan speed\n");
  887. } else if (!strncasecmp(&input, "m", 1)) {
  888. get_memoryrange(gpu, &imin, &imax);
  889. wlogprint("Enter GPU memory clock speed (%d - %d Mhz)", imin, imax);
  890. val = curses_int("");
  891. if (val < imin || val > imax) {
  892. wlogprint("Value is outside safe range, are you sure?\n");
  893. input = getch();
  894. if (strncasecmp(&input, "y", 1))
  895. return;
  896. }
  897. if (!set_memoryclock(gpu, val))
  898. wlogprint("Driver reports success but check values below\n");
  899. else
  900. wlogprint("Failed to modify memory clock speed\n");
  901. } else if (!strncasecmp(&input, "v", 1)) {
  902. get_vddcrange(gpu, &fmin, &fmax);
  903. wlogprint("Enter GPU voltage (%.3f - %.3f V)", fmin, fmax);
  904. fval = curses_float("");
  905. if (fval < fmin || fval > fmax) {
  906. wlogprint("Value is outside safe range, are you sure?\n");
  907. input = getch();
  908. if (strncasecmp(&input, "y", 1))
  909. return;
  910. }
  911. if (!set_vddc(gpu, fval))
  912. wlogprint("Driver reports success but check values below\n");
  913. else
  914. wlogprint("Failed to modify voltage\n");
  915. } else if (!strncasecmp(&input, "p", 1)) {
  916. val = curses_int("Enter powertune value (-20 - 20)");
  917. if (val < -20 || val > 20) {
  918. wlogprint("Value is outside safe range, are you sure?\n");
  919. input = getch();
  920. if (strncasecmp(&input, "y", 1))
  921. return;
  922. }
  923. if (!set_powertune(gpu, val))
  924. wlogprint("Driver reports success but check values below\n");
  925. else
  926. wlogprint("Failed to modify powertune value\n");
  927. } else
  928. return;
  929. sleep(1);
  930. goto updated;
  931. }
  932. void clear_adl(nDevs)
  933. {
  934. struct gpu_adl *ga;
  935. int i;
  936. if (!adl_active)
  937. return;
  938. lock_adl();
  939. /* Try to reset values to their defaults */
  940. for (i = 0; i < nDevs; i++) {
  941. ga = &gpus[i].adl;
  942. if (!gpus[i].has_adl)
  943. continue;
  944. ADL_Adapter_Speed_Set(ga->iAdapterIndex, ga->lpCurrent);
  945. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  946. free(ga->DefPerfLev);
  947. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  948. }
  949. ADL_Main_Memory_Free ( (void **)&lpInfo );
  950. ADL_Main_Control_Destroy ();
  951. unlock_adl();
  952. #if defined (LINUX)
  953. dlclose(hDLL);
  954. #else
  955. FreeLibrary(hDLL);
  956. #endif
  957. }
  958. #endif /* HAVE_ADL */