adl.c 35 KB

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