adl.c 37 KB

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