adl.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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. 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. int lpStatus;
  161. ADLODPerformanceLevels *lpOdPerformanceLevels;
  162. int lev;
  163. iAdapterIndex = lpInfo[i].iAdapterIndex;
  164. /* Get unique identifier of the adapter, 0 means not AMD */
  165. if (ADL_Adapter_ID_Get(iAdapterIndex, &lpAdapterID) != ADL_OK) {
  166. applog(LOG_INFO, "Failed to ADL_Adapter_ID_Get");
  167. continue;
  168. }
  169. if (!lpAdapterID)
  170. continue;
  171. /* Each adapter may have multiple entries */
  172. if (lpAdapterID == last_adapter)
  173. continue;
  174. /* We found a truly new adapter instead of a logical
  175. * one. Now since there's no way of correlating the
  176. * opencl enumerated devices and the ADL enumerated
  177. * ones, we have to assume they're in the same order.*/
  178. if (++devices > nDevs) {
  179. applog(LOG_ERR, "ADL found more devices than opencl");
  180. return;
  181. }
  182. gpu = devices - 1;
  183. last_adapter = lpAdapterID;
  184. /* From here on we know this device is a discrete device and
  185. * should support ADL */
  186. ga = &gpus[gpu].adl;
  187. ga->iAdapterIndex = iAdapterIndex;
  188. ga->lpAdapterID = lpAdapterID;
  189. ga->DefPerfLev = NULL;
  190. /* Save whatever the current speed setting is to restore on exit */
  191. if (ADL_Adapter_Speed_Get(iAdapterIndex, &ga->lpCurrent, &dummy) != ADL_OK) {
  192. applog(LOG_INFO, "Failed to ADL_Adapter_Speed_Get");
  193. continue;
  194. }
  195. /* Force the speed to high, whether it's ignored or not */
  196. ADL_Adapter_Speed_Set(iAdapterIndex, ADL_CONTEXT_SPEED_FORCEHIGH);
  197. if (ADL_Overdrive5_ODParameters_Get(iAdapterIndex, &ga->lpOdParameters) != ADL_OK) {
  198. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODParameters_Get");
  199. continue;
  200. }
  201. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  202. /* We're only interested in the top performance level */
  203. lpOdPerformanceLevels = malloc(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  204. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  205. /* Get default performance levels first */
  206. if (ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 1, lpOdPerformanceLevels) != ADL_OK) {
  207. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODPerformanceLevels_Get");
  208. continue;
  209. }
  210. /* Set the limits we'd use based on default gpu speeds */
  211. ga->maxspeed = ga->minspeed = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  212. /* Now get the current performance levels for any existing overclock */
  213. ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
  214. /* Save these values as the defaults in case we wish to reset to defaults */
  215. ga->DefPerfLev = lpOdPerformanceLevels;
  216. if (gpus[gpu].gpu_engine) {
  217. int setengine = gpus[gpu].gpu_engine * 100;
  218. /* Lower profiles can't have a higher setting */
  219. for (j = 0; j < lev; j++) {
  220. if (lpOdPerformanceLevels->aLevels[j].iEngineClock > setengine)
  221. lpOdPerformanceLevels->aLevels[j].iEngineClock = setengine;
  222. }
  223. lpOdPerformanceLevels->aLevels[lev].iEngineClock = setengine;
  224. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, gpus[gpu].gpu_engine);
  225. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  226. }
  227. if (gpus[gpu].gpu_memclock) {
  228. int setmem = gpus[gpu].gpu_memclock * 100;
  229. for (j = 0; j < lev; j++) {
  230. if (lpOdPerformanceLevels->aLevels[j].iMemoryClock > setmem)
  231. lpOdPerformanceLevels->aLevels[j].iMemoryClock = setmem;
  232. }
  233. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = setmem;
  234. applog(LOG_INFO, "Setting GPU %d memory clock to %d", gpu, gpus[gpu].gpu_memclock);
  235. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  236. }
  237. if (gpus[gpu].gpu_vddc) {
  238. int setv = gpus[gpu].gpu_vddc * 1000;
  239. for (j = 0; j < lev; j++) {
  240. if (lpOdPerformanceLevels->aLevels[j].iVddc > setv)
  241. lpOdPerformanceLevels->aLevels[j].iVddc = setv;
  242. }
  243. lpOdPerformanceLevels->aLevels[lev].iVddc = setv;
  244. applog(LOG_INFO, "Setting GPU %d voltage to %.3f", gpu, gpus[gpu].gpu_vddc);
  245. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  246. }
  247. ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
  248. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  249. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  250. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  251. if (ga->iEngineClock < ga->minspeed)
  252. ga->minspeed = ga->iEngineClock;
  253. if (ga->iEngineClock > ga->maxspeed)
  254. ga->maxspeed = ga->iEngineClock;
  255. if (ADL_Overdrive5_FanSpeedInfo_Get(iAdapterIndex, 0, &ga->lpFanSpeedInfo) != ADL_OK) {
  256. applog(LOG_INFO, "Failed to ADL_Overdrive5_FanSpeedInfo_Get");
  257. continue;
  258. }
  259. /* Save the fanspeed values as defaults in case we reset later */
  260. ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  261. if (gpus[gpu].gpu_fan) {
  262. ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  263. ga->lpFanSpeedValue.iFanSpeed = gpus[gpu].gpu_fan;
  264. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  265. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  266. applog(LOG_INFO, "Setting GPU %d fan speed to %d%%", gpu, gpus[gpu].gpu_fan);
  267. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  268. }
  269. /* Not fatal if powercontrol get fails */
  270. if (ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy) != ADL_OK)
  271. applog(LOG_INFO, "Failed to ADL_Overdrive5_PowerControl_get");
  272. if (gpus[gpu].gpu_powertune) {
  273. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, gpus[gpu].gpu_powertune);
  274. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  275. }
  276. /* Flag if the values of this GPU were ever modified to tell us
  277. * we need to reset the values upon exiting */
  278. if (gpus[gpu].gpu_engine || gpus[gpu].gpu_memclock || gpus[gpu].gpu_vddc ||
  279. gpus[gpu].gpu_fan || gpus[gpu].gpu_powertune)
  280. ga->managed = true;
  281. /* Set some default temperatures for autotune when enabled */
  282. ga->targettemp = opt_targettemp;
  283. ga->overtemp = opt_overheattemp;
  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. if (temp > ga->overtemp && fanpercent < 100) {
  737. applog(LOG_WARNING, "Overheat detected, increasing fan to 100%");
  738. newpercent = 100;
  739. } else if (temp > ga->targettemp && fanpercent < 85) {
  740. if (opt_debug)
  741. applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
  742. if (temp > ga->targettemp + opt_hysteresis)
  743. newpercent = fanpercent + 10;
  744. else
  745. newpercent = fanpercent + 5;
  746. if (newpercent > 85)
  747. newpercent = 85;
  748. } else if (fanpercent && temp < ga->targettemp - opt_hysteresis) {
  749. if (opt_debug)
  750. applog(LOG_DEBUG, "Temperature %d degrees below target, decreasing fanspeed", opt_hysteresis);
  751. newpercent = fanpercent - 1;
  752. }
  753. if (newpercent > 100)
  754. newpercent = 100;
  755. else if (newpercent < 0)
  756. newpercent = 0;
  757. if (newpercent != fanpercent) {
  758. fan_optimal = false;
  759. applog(LOG_INFO, "Setting GPU %d fan percentage to %d", gpu, newpercent);
  760. set_fanspeed(gpu, newpercent);
  761. }
  762. }
  763. if (engine && ga->autoengine) {
  764. if (temp > ga->cutofftemp) {
  765. applog(LOG_WARNING, "Hit thermal cutoff limit, disabling GPU!");
  766. *enable = false;
  767. newengine = ga->minspeed;
  768. } else if (temp > ga->overtemp && engine > ga->minspeed) {
  769. applog(LOG_WARNING, "Overheat detected, decreasing GPU clock speed");
  770. newengine = ga->minspeed;
  771. } else if (temp > ga->targettemp + opt_hysteresis && engine > ga->minspeed && fan_optimal) {
  772. if (opt_debug)
  773. applog(LOG_DEBUG, "Temperature %d degrees over target, decreasing clock speed", opt_hysteresis);
  774. newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
  775. } else if (temp < ga->targettemp && engine < ga->maxspeed) {
  776. if (opt_debug)
  777. applog(LOG_DEBUG, "Temperature below target, increasing clock speed");
  778. newengine = engine + ga->lpOdParameters.sEngineClock.iStep;
  779. }
  780. if (newengine > ga->maxspeed)
  781. newengine = ga->maxspeed;
  782. else if (newengine < ga->minspeed)
  783. newengine = ga->minspeed;
  784. if (newengine != engine) {
  785. newengine /= 100;
  786. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, newengine);
  787. set_engineclock(gpu, newengine);
  788. }
  789. }
  790. }
  791. void set_defaultfan(int gpu)
  792. {
  793. struct gpu_adl *ga;
  794. if (!gpus[gpu].has_adl || !adl_active)
  795. return;
  796. ga = &gpus[gpu].adl;
  797. lock_adl();
  798. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  799. unlock_adl();
  800. }
  801. void set_defaultengine(int gpu)
  802. {
  803. struct gpu_adl *ga;
  804. if (!gpus[gpu].has_adl || !adl_active)
  805. return;
  806. ga = &gpus[gpu].adl;
  807. lock_adl();
  808. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  809. unlock_adl();
  810. }
  811. void change_autosettings(int gpu)
  812. {
  813. struct gpu_adl *ga = &gpus[gpu].adl;
  814. char input;
  815. int val;
  816. wlogprint("Target temperature: %d\n", ga->targettemp);
  817. wlogprint("Overheat temperature: %d\n", ga->overtemp);
  818. wlogprint("Cutoff temperature: %d\n", ga->cutofftemp);
  819. wlogprint("Hysteresis differece: %d\n", opt_hysteresis);
  820. wlogprint("Toggle [F]an auto [G]PU auto\nChange [T]arget [O]verheat [C]utoff [H]ysteresis\n");
  821. wlogprint("Or press any other key to continue\n");
  822. input = getch();
  823. if (!strncasecmp(&input, "f", 1)) {
  824. ga->autofan ^= true;
  825. wlogprint("Fan autotune is now %s\n", ga->autofan ? "enabled" : "disabled");
  826. if (!ga->autofan) {
  827. wlogprint("Resetting fan to startup settings\n");
  828. set_defaultfan(gpu);
  829. }
  830. } else if (!strncasecmp(&input, "g", 1)) {
  831. ga->autoengine ^= true;
  832. wlogprint("GPU engine clock autotune is now %s\n", ga->autoengine ? "enabled" : "disabled");
  833. if (!ga->autoengine) {
  834. wlogprint("Resetting GPU engine clock to startup settings\n");
  835. set_defaultengine(gpu);
  836. }
  837. } else if (!strncasecmp(&input, "t", 1)) {
  838. val = curses_int("Enter target temperature for this GPU in C (0-100)");
  839. if (val < 0 || val > 100)
  840. wlogprint("Invalid temperature");
  841. else
  842. ga->targettemp = val;
  843. } else if (!strncasecmp(&input, "o", 1)) {
  844. wlogprint("Enter overheat temperature for this GPU in C (%d+)", ga->targettemp);
  845. val = curses_int("");
  846. if (val <= ga->targettemp || val > 200)
  847. wlogprint("Invalid temperature");
  848. else
  849. ga->overtemp = val;
  850. } else if (!strncasecmp(&input, "c", 1)) {
  851. wlogprint("Enter cutoff temperature for this GPU in C (%d+)", ga->overtemp);
  852. val = curses_int("");
  853. if (val <= ga->overtemp || val > 200)
  854. wlogprint("Invalid temperature");
  855. else
  856. ga->cutofftemp = val;
  857. } else if (!strncasecmp(&input, "h", 1)) {
  858. val = curses_int("Enter hysteresis temperature difference (0-10)");
  859. if (val < 1 || val > 10)
  860. wlogprint("Invalid value");
  861. else
  862. opt_hysteresis = val;
  863. }
  864. }
  865. void change_gpusettings(int gpu)
  866. {
  867. struct gpu_adl *ga = &gpus[gpu].adl;
  868. float fval, fmin = 0, fmax = 0;
  869. int val, imin = 0, imax = 0;
  870. char input;
  871. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  872. float temp = 0, vddc = 0;
  873. updated:
  874. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune))
  875. wlogprint("Temp: %.1f C\nFan Speed: %d%% (%d RPM)\nEngine Clock: %d MHz\n"
  876. "Memory Clock: %d Mhz\nVddc: %.3f V\nActivity: %d%%\nPowertune: %d%%\n",
  877. temp, fanpercent, fanspeed, engineclock, memclock, vddc, activity, powertune);
  878. wlogprint("Fan autotune is %s\n", ga->autofan ? "enabled" : "disabled");
  879. wlogprint("GPU engine clock autotune is %s\n", ga->autoengine ? "enabled" : "disabled");
  880. wlogprint("Change [A]utomatic [E]ngine [F]an [M]emory [V]oltage [P]owertune\n");
  881. wlogprint("Or press any other key to continue\n");
  882. input = getch();
  883. if (!strncasecmp(&input, "a", 1)) {
  884. change_autosettings(gpu);
  885. } else if (!strncasecmp(&input, "e", 1)) {
  886. get_enginerange(gpu, &imin, &imax);
  887. wlogprint("Enter GPU engine clock speed (%d - %d Mhz)", imin, imax);
  888. val = curses_int("");
  889. if (val < imin || val > imax) {
  890. wlogprint("Value is outside safe range, are you sure?\n");
  891. input = getch();
  892. if (strncasecmp(&input, "y", 1))
  893. return;
  894. }
  895. if (!set_engineclock(gpu, val))
  896. wlogprint("Driver reports success but check values below\n");
  897. else
  898. wlogprint("Failed to modify engine clock speed\n");
  899. } else if (!strncasecmp(&input, "f", 1)) {
  900. get_fanrange(gpu, &imin, &imax);
  901. wlogprint("Enter fan percentage (%d - %d %)", imin, imax);
  902. val = curses_int("");
  903. if (val < imin || val > imax) {
  904. wlogprint("Value is outside safe range, are you sure?\n");
  905. input = getch();
  906. if (strncasecmp(&input, "y", 1))
  907. return;
  908. }
  909. if (!set_fanspeed(gpu, val))
  910. wlogprint("Driver reports success but check values below\n");
  911. else
  912. wlogprint("Failed to modify fan speed\n");
  913. } else if (!strncasecmp(&input, "m", 1)) {
  914. get_memoryrange(gpu, &imin, &imax);
  915. wlogprint("Enter GPU memory clock speed (%d - %d Mhz)", imin, imax);
  916. val = curses_int("");
  917. if (val < imin || val > imax) {
  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_memoryclock(gpu, val))
  924. wlogprint("Driver reports success but check values below\n");
  925. else
  926. wlogprint("Failed to modify memory clock speed\n");
  927. } else if (!strncasecmp(&input, "v", 1)) {
  928. get_vddcrange(gpu, &fmin, &fmax);
  929. wlogprint("Enter GPU voltage (%.3f - %.3f V)", fmin, fmax);
  930. fval = curses_float("");
  931. if (fval < fmin || fval > fmax) {
  932. wlogprint("Value is outside safe range, are you sure?\n");
  933. input = getch();
  934. if (strncasecmp(&input, "y", 1))
  935. return;
  936. }
  937. if (!set_vddc(gpu, fval))
  938. wlogprint("Driver reports success but check values below\n");
  939. else
  940. wlogprint("Failed to modify voltage\n");
  941. } else if (!strncasecmp(&input, "p", 1)) {
  942. val = curses_int("Enter powertune value (-20 - 20)");
  943. if (val < -20 || val > 20) {
  944. wlogprint("Value is outside safe range, are you sure?\n");
  945. input = getch();
  946. if (strncasecmp(&input, "y", 1))
  947. return;
  948. }
  949. if (!set_powertune(gpu, val))
  950. wlogprint("Driver reports success but check values below\n");
  951. else
  952. wlogprint("Failed to modify powertune value\n");
  953. } else
  954. return;
  955. sleep(1);
  956. goto updated;
  957. }
  958. void clear_adl(nDevs)
  959. {
  960. struct gpu_adl *ga;
  961. int i;
  962. if (!adl_active)
  963. return;
  964. lock_adl();
  965. /* Try to reset values to their defaults */
  966. for (i = 0; i < nDevs; i++) {
  967. ga = &gpus[i].adl;
  968. /* Only reset the values if we've changed them at any time */
  969. if (!gpus[i].has_adl || !ga->managed)
  970. continue;
  971. ADL_Adapter_Speed_Set(ga->iAdapterIndex, ga->lpCurrent);
  972. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  973. free(ga->DefPerfLev);
  974. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  975. }
  976. ADL_Main_Memory_Free ( (void **)&lpInfo );
  977. ADL_Main_Control_Destroy ();
  978. unlock_adl();
  979. #if defined (LINUX)
  980. dlclose(hDLL);
  981. #else
  982. FreeLibrary(hDLL);
  983. #endif
  984. }
  985. #endif /* HAVE_ADL */