adl.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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. for (gpu = 0; gpu < devices; gpu++) {
  319. struct gpu_adl *ga = &gpus[gpu].adl;
  320. struct cgpu_info *cgpu = &gpus[gpu];
  321. int j;
  322. cgpu->virtual_gpu = 0;
  323. for (j = 0; j < devices; j++) {
  324. struct gpu_adl *other_ga;
  325. if (j == gpu)
  326. continue;
  327. other_ga = &gpus[j].adl;
  328. /* Find the real GPU order based on their order
  329. * according to their iBusNumber value */
  330. if (other_ga->iBusNumber < ga->iBusNumber)
  331. cgpu->virtual_gpu++;
  332. /* Search for twin GPUs on a single card. They will be
  333. * separated by one bus id and one will have fanspeed
  334. * while the other won't. */
  335. if (!ga->has_fanspeed) {
  336. if (fanspeed_twin(ga, other_ga)) {
  337. applog(LOG_INFO, "Dual GPUs detected: %d and %d",
  338. ga->gpu, other_ga->gpu);
  339. ga->twin = other_ga;
  340. other_ga->twin = ga;
  341. }
  342. }
  343. }
  344. applog(LOG_INFO, "GPU %d mapped to virtual GPU %d", gpu, cgpu->virtual_gpu);
  345. }
  346. }
  347. static float __gpu_temp(struct gpu_adl *ga)
  348. {
  349. if (ADL_Overdrive5_Temperature_Get(ga->iAdapterIndex, 0, &ga->lpTemperature) != ADL_OK)
  350. return -1;
  351. return (float)ga->lpTemperature.iTemperature / 1000;
  352. }
  353. float gpu_temp(int gpu)
  354. {
  355. struct gpu_adl *ga;
  356. float ret = -1;
  357. if (!gpus[gpu].has_adl || !adl_active)
  358. return ret;
  359. ga = &gpus[gpu].adl;
  360. lock_adl();
  361. ret = __gpu_temp(ga);
  362. unlock_adl();
  363. return ret;
  364. }
  365. static inline int __gpu_engineclock(struct gpu_adl *ga)
  366. {
  367. return ga->lpActivity.iEngineClock / 100;
  368. }
  369. int gpu_engineclock(int gpu)
  370. {
  371. struct gpu_adl *ga;
  372. int ret = -1;
  373. if (!gpus[gpu].has_adl || !adl_active)
  374. return ret;
  375. ga = &gpus[gpu].adl;
  376. lock_adl();
  377. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  378. goto out;
  379. ret = __gpu_engineclock(ga);
  380. out:
  381. unlock_adl();
  382. return ret;
  383. }
  384. static inline int __gpu_memclock(struct gpu_adl *ga)
  385. {
  386. return ga->lpActivity.iMemoryClock / 100;
  387. }
  388. int gpu_memclock(int gpu)
  389. {
  390. struct gpu_adl *ga;
  391. int ret = -1;
  392. if (!gpus[gpu].has_adl || !adl_active)
  393. return ret;
  394. ga = &gpus[gpu].adl;
  395. lock_adl();
  396. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  397. goto out;
  398. ret = __gpu_memclock(ga);
  399. out:
  400. unlock_adl();
  401. return ret;
  402. }
  403. static inline float __gpu_vddc(struct gpu_adl *ga)
  404. {
  405. return (float)ga->lpActivity.iVddc / 1000;
  406. }
  407. float gpu_vddc(int gpu)
  408. {
  409. struct gpu_adl *ga;
  410. float ret = -1;
  411. if (!gpus[gpu].has_adl || !adl_active)
  412. return ret;
  413. ga = &gpus[gpu].adl;
  414. lock_adl();
  415. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  416. goto out;
  417. ret = __gpu_vddc(ga);
  418. out:
  419. unlock_adl();
  420. return ret;
  421. }
  422. static inline int __gpu_activity(struct gpu_adl *ga)
  423. {
  424. if (!ga->lpOdParameters.iActivityReportingSupported)
  425. return -1;
  426. return ga->lpActivity.iActivityPercent;
  427. }
  428. int gpu_activity(int gpu)
  429. {
  430. struct gpu_adl *ga;
  431. int ret = -1;
  432. if (!gpus[gpu].has_adl || !adl_active)
  433. return ret;
  434. ga = &gpus[gpu].adl;
  435. lock_adl();
  436. ret = ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity);
  437. unlock_adl();
  438. if (ret != ADL_OK)
  439. return ret;
  440. if (!ga->lpOdParameters.iActivityReportingSupported)
  441. return ret;
  442. return ga->lpActivity.iActivityPercent;
  443. }
  444. static inline int __gpu_fanspeed(struct gpu_adl *ga)
  445. {
  446. if (!ga->has_fanspeed && ga->twin)
  447. return __gpu_fanspeed(ga->twin);
  448. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_READ))
  449. return -1;
  450. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  451. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  452. return -1;
  453. return ga->lpFanSpeedValue.iFanSpeed;
  454. }
  455. int gpu_fanspeed(int gpu)
  456. {
  457. struct gpu_adl *ga;
  458. int ret = -1;
  459. if (!gpus[gpu].has_adl || !adl_active)
  460. return ret;
  461. ga = &gpus[gpu].adl;
  462. lock_adl();
  463. ret = __gpu_fanspeed(ga);
  464. unlock_adl();
  465. return ret;
  466. }
  467. static int __gpu_fanpercent(struct gpu_adl *ga)
  468. {
  469. if (!ga->has_fanspeed && ga->twin)
  470. return __gpu_fanpercent(ga->twin);
  471. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ ))
  472. return -1;
  473. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  474. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  475. return -1;
  476. return ga->lpFanSpeedValue.iFanSpeed;
  477. }
  478. int gpu_fanpercent(int gpu)
  479. {
  480. struct gpu_adl *ga;
  481. int ret = -1;
  482. if (!gpus[gpu].has_adl || !adl_active)
  483. return ret;
  484. ga = &gpus[gpu].adl;
  485. lock_adl();
  486. ret = __gpu_fanpercent(ga);
  487. unlock_adl();
  488. return ret;
  489. }
  490. static inline int __gpu_powertune(struct gpu_adl *ga)
  491. {
  492. int dummy = 0;
  493. if (ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy) != ADL_OK)
  494. return -1;
  495. return ga->iPercentage;
  496. }
  497. int gpu_powertune(int gpu)
  498. {
  499. struct gpu_adl *ga;
  500. int ret = -1;
  501. if (!gpus[gpu].has_adl || !adl_active)
  502. return ret;
  503. ga = &gpus[gpu].adl;
  504. lock_adl();
  505. ret = __gpu_powertune(ga);
  506. unlock_adl();
  507. return ret;
  508. }
  509. bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc,
  510. int *activity, int *fanspeed, int *fanpercent, int *powertune)
  511. {
  512. struct gpu_adl *ga;
  513. if (!gpus[gpu].has_adl || !adl_active)
  514. return false;
  515. ga = &gpus[gpu].adl;
  516. lock_adl();
  517. *temp = __gpu_temp(ga);
  518. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK) {
  519. *engineclock = 0;
  520. *memclock = 0;
  521. *vddc = 0;
  522. *activity = 0;
  523. } else {
  524. *engineclock = __gpu_engineclock(ga);
  525. *memclock = __gpu_memclock(ga);
  526. *vddc = __gpu_vddc(ga);
  527. *activity = __gpu_activity(ga);
  528. }
  529. *fanspeed = __gpu_fanspeed(ga);
  530. *fanpercent = __gpu_fanpercent(ga);
  531. *powertune = __gpu_powertune(ga);
  532. unlock_adl();
  533. return true;
  534. }
  535. static void get_enginerange(int gpu, int *imin, int *imax)
  536. {
  537. struct gpu_adl *ga;
  538. if (!gpus[gpu].has_adl || !adl_active) {
  539. wlogprint("Get enginerange not supported\n");
  540. return;
  541. }
  542. ga = &gpus[gpu].adl;
  543. *imin = ga->lpOdParameters.sEngineClock.iMin / 100;
  544. *imax = ga->lpOdParameters.sEngineClock.iMax / 100;
  545. }
  546. int set_engineclock(int gpu, int iEngineClock)
  547. {
  548. ADLODPerformanceLevels *lpOdPerformanceLevels;
  549. int i, lev, ret = 1;
  550. struct gpu_adl *ga;
  551. if (!gpus[gpu].has_adl || !adl_active) {
  552. wlogprint("Set engineclock not supported\n");
  553. return ret;
  554. }
  555. iEngineClock *= 100;
  556. ga = &gpus[gpu].adl;
  557. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  558. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  559. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  560. lock_adl();
  561. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  562. goto out;
  563. for (i = 0; i < lev; i++) {
  564. if (lpOdPerformanceLevels->aLevels[i].iEngineClock > iEngineClock)
  565. lpOdPerformanceLevels->aLevels[i].iEngineClock = iEngineClock;
  566. }
  567. lpOdPerformanceLevels->aLevels[lev].iEngineClock = iEngineClock;
  568. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  569. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  570. if (lpOdPerformanceLevels->aLevels[lev].iEngineClock == iEngineClock)
  571. ret = 0;
  572. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  573. if (ga->iEngineClock > ga->maxspeed)
  574. ga->maxspeed = ga->iEngineClock;
  575. if (ga->iEngineClock < ga->minspeed)
  576. ga->minspeed = ga->iEngineClock;
  577. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  578. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  579. ga->managed = true;
  580. out:
  581. unlock_adl();
  582. return ret;
  583. }
  584. static void get_memoryrange(int gpu, int *imin, int *imax)
  585. {
  586. struct gpu_adl *ga;
  587. if (!gpus[gpu].has_adl || !adl_active) {
  588. wlogprint("Get memoryrange not supported\n");
  589. return;
  590. }
  591. ga = &gpus[gpu].adl;
  592. *imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
  593. *imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
  594. }
  595. int set_memoryclock(int gpu, int iMemoryClock)
  596. {
  597. ADLODPerformanceLevels *lpOdPerformanceLevels;
  598. int i, lev, ret = 1;
  599. struct gpu_adl *ga;
  600. if (!gpus[gpu].has_adl || !adl_active) {
  601. wlogprint("Set memoryclock not supported\n");
  602. return ret;
  603. }
  604. iMemoryClock *= 100;
  605. ga = &gpus[gpu].adl;
  606. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  607. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  608. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  609. lock_adl();
  610. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  611. goto out;
  612. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
  613. for (i = 0; i < lev; i++) {
  614. if (lpOdPerformanceLevels->aLevels[i].iMemoryClock > iMemoryClock)
  615. lpOdPerformanceLevels->aLevels[i].iMemoryClock = iMemoryClock;
  616. }
  617. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  618. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  619. if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock == iMemoryClock)
  620. ret = 0;
  621. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  622. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  623. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  624. ga->managed = true;
  625. out:
  626. unlock_adl();
  627. return ret;
  628. }
  629. static void get_vddcrange(int gpu, float *imin, float *imax)
  630. {
  631. struct gpu_adl *ga;
  632. if (!gpus[gpu].has_adl || !adl_active) {
  633. wlogprint("Get vddcrange not supported\n");
  634. return;
  635. }
  636. ga = &gpus[gpu].adl;
  637. *imin = (float)ga->lpOdParameters.sVddc.iMin / 1000;
  638. *imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
  639. }
  640. static float curses_float(const char *query)
  641. {
  642. float ret;
  643. char *cvar;
  644. cvar = curses_input(query);
  645. ret = atof(cvar);
  646. free(cvar);
  647. return ret;
  648. }
  649. int set_vddc(int gpu, float fVddc)
  650. {
  651. ADLODPerformanceLevels *lpOdPerformanceLevels;
  652. int i, iVddc, lev, ret = 1;
  653. struct gpu_adl *ga;
  654. if (!gpus[gpu].has_adl || !adl_active) {
  655. wlogprint("Set vddc not supported\n");
  656. return ret;
  657. }
  658. iVddc = 1000 * fVddc;
  659. ga = &gpus[gpu].adl;
  660. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  661. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  662. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  663. lock_adl();
  664. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  665. goto out;
  666. for (i = 0; i < lev; i++) {
  667. if (lpOdPerformanceLevels->aLevels[i].iVddc > iVddc)
  668. lpOdPerformanceLevels->aLevels[i].iVddc = iVddc;
  669. }
  670. lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
  671. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  672. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  673. if (lpOdPerformanceLevels->aLevels[lev].iVddc == iVddc)
  674. ret = 0;
  675. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  676. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  677. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  678. ga->managed = true;
  679. out:
  680. unlock_adl();
  681. return ret;
  682. }
  683. static void get_fanrange(int gpu, int *imin, int *imax)
  684. {
  685. struct gpu_adl *ga;
  686. if (!gpus[gpu].has_adl || !adl_active) {
  687. wlogprint("Get fanrange not supported\n");
  688. return;
  689. }
  690. ga = &gpus[gpu].adl;
  691. *imin = ga->lpFanSpeedInfo.iMinPercent;
  692. *imax = ga->lpFanSpeedInfo.iMaxPercent;
  693. }
  694. int set_fanspeed(int gpu, int iFanSpeed)
  695. {
  696. struct gpu_adl *ga;
  697. int ret = 1;
  698. if (!gpus[gpu].has_adl || !adl_active) {
  699. wlogprint("Set fanspeed not supported\n");
  700. return ret;
  701. }
  702. ga = &gpus[gpu].adl;
  703. if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE ))) {
  704. if (opt_debug)
  705. applog(LOG_DEBUG, "GPU %d doesn't support rpm or percent write", gpu);
  706. return ret;
  707. }
  708. /* Store what fanspeed we're actually aiming for for re-entrant changes
  709. * in case this device does not support fine setting changes */
  710. ga->targetfan = iFanSpeed;
  711. lock_adl();
  712. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK) {
  713. if (opt_debug)
  714. applog(LOG_DEBUG, "GPU %d call to fanspeed get failed", gpu);
  715. }
  716. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE)) {
  717. /* Must convert speed to an RPM */
  718. iFanSpeed = ga->lpFanSpeedInfo.iMaxRPM * iFanSpeed / 100;
  719. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  720. } else
  721. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  722. if (!(ga->lpFanSpeedValue.iFlags & ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED)) {
  723. /* If user defined is not already specified, set it first */
  724. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  725. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  726. }
  727. ga->lpFanSpeedValue.iFanSpeed = iFanSpeed;
  728. ret = ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  729. ga->managed = true;
  730. unlock_adl();
  731. return ret;
  732. }
  733. static int set_powertune(int gpu, int iPercentage)
  734. {
  735. struct gpu_adl *ga;
  736. int dummy, ret = 1;
  737. if (!gpus[gpu].has_adl || !adl_active) {
  738. wlogprint("Set powertune not supported\n");
  739. return ret;
  740. }
  741. ga = &gpus[gpu].adl;
  742. lock_adl();
  743. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, iPercentage);
  744. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  745. if (ga->iPercentage == iPercentage)
  746. ret = 0;
  747. ga->managed = true;
  748. unlock_adl();
  749. return ret;
  750. }
  751. static void fan_autotune(int gpu, int temp, int fanpercent, bool *fan_optimal)
  752. {
  753. struct cgpu_info *cgpu = &gpus[gpu];
  754. struct gpu_adl *ga = &cgpu->adl;
  755. int top = gpus[gpu].gpu_fan;
  756. int bot = gpus[gpu].min_fan;
  757. int newpercent = fanpercent;
  758. int iMin = 0, iMax = 100;
  759. get_fanrange(gpu, &iMin, &iMax);
  760. if (temp > ga->overtemp && fanpercent < iMax) {
  761. applog(LOG_WARNING, "Overheat detected on GPU %d, increasing fan to 100%", gpu);
  762. newpercent = iMax;
  763. } else if (temp > ga->targettemp && fanpercent < top && temp >= ga->lasttemp) {
  764. if (opt_debug)
  765. applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
  766. if (temp > ga->targettemp + opt_hysteresis)
  767. newpercent = ga->targetfan + 10;
  768. else
  769. newpercent = ga->targetfan + 5;
  770. if (newpercent > top)
  771. newpercent = top;
  772. } else if (fanpercent > bot && temp < ga->targettemp - opt_hysteresis && temp <= ga->lasttemp) {
  773. if (opt_debug)
  774. applog(LOG_DEBUG, "Temperature %d degrees below target, decreasing fanspeed", opt_hysteresis);
  775. newpercent = ga->targetfan - 1;
  776. } else {
  777. /* We're in the optimal range, make minor adjustments if the
  778. * temp is still drifting */
  779. if (fanpercent > bot && temp < ga->lasttemp && ga->lasttemp < ga->targettemp) {
  780. if (opt_debug)
  781. applog(LOG_DEBUG, "Temperature dropping while in target range, decreasing fanspeed");
  782. newpercent = ga->targetfan - 1;
  783. } else if (fanpercent < top && temp > ga->lasttemp && temp > ga->targettemp - opt_hysteresis) {
  784. if (opt_debug)
  785. applog(LOG_DEBUG, "Temperature rising while in target range, increasing fanspeed");
  786. newpercent = ga->targetfan + 1;
  787. }
  788. }
  789. if (newpercent > iMax)
  790. newpercent = iMax;
  791. else if (newpercent < iMin)
  792. newpercent = iMin;
  793. if (newpercent != fanpercent) {
  794. fan_optimal = false;
  795. applog(LOG_INFO, "Setting GPU %d fan percentage to %d", gpu, newpercent);
  796. set_fanspeed(gpu, newpercent);
  797. }
  798. }
  799. void gpu_autotune(int gpu, bool *enable)
  800. {
  801. int temp, fanpercent, engine, newengine, twintemp = 0;
  802. bool fan_optimal = true;
  803. struct cgpu_info *cgpu;
  804. struct gpu_adl *ga;
  805. cgpu = &gpus[gpu];
  806. ga = &cgpu->adl;
  807. lock_adl();
  808. ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity);
  809. temp = __gpu_temp(ga);
  810. if (ga->twin)
  811. twintemp = __gpu_temp(ga->twin);
  812. fanpercent = __gpu_fanpercent(ga);
  813. unlock_adl();
  814. newengine = engine = gpu_engineclock(gpu) * 100;
  815. if (temp && fanpercent >= 0 && ga->autofan) {
  816. if (!ga->twin)
  817. fan_autotune(gpu, temp, fanpercent, &fan_optimal);
  818. else if (ga->autofan && (ga->has_fanspeed || !ga->twin->autofan)) {
  819. /* On linked GPUs, we autotune the fan only once, based
  820. * on the highest temperature from either GPUs */
  821. int hightemp, fan_gpu;
  822. if (twintemp > temp)
  823. hightemp = twintemp;
  824. else
  825. hightemp = temp;
  826. if (ga->has_fanspeed)
  827. fan_gpu = gpu;
  828. else
  829. fan_gpu = ga->twin->gpu;
  830. fan_autotune(fan_gpu, hightemp, fanpercent, &fan_optimal);
  831. }
  832. }
  833. if (engine && ga->autoengine) {
  834. if (temp > ga->cutofftemp) {
  835. applog(LOG_WARNING, "Hit thermal cutoff limit on GPU %d, disabling!", gpu);
  836. *enable = false;
  837. newengine = ga->minspeed;
  838. } else if (temp > ga->overtemp && engine > ga->minspeed) {
  839. applog(LOG_WARNING, "Overheat detected, decreasing GPU %d clock speed", gpu);
  840. newengine = ga->minspeed;
  841. /* Only try to tune engine speed if the current performance level is at max */
  842. } else if ((ga->lpActivity.iCurrentPerformanceLevel == ga->lpOdParameters.iNumberOfPerformanceLevels - 1) &&
  843. (temp > ga->targettemp + opt_hysteresis && engine > ga->minspeed && fan_optimal)) {
  844. if (opt_debug)
  845. applog(LOG_DEBUG, "Temperature %d degrees over target, decreasing clock speed", opt_hysteresis);
  846. newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
  847. } else if ((ga->lpActivity.iCurrentPerformanceLevel == ga->lpOdParameters.iNumberOfPerformanceLevels - 1) &&
  848. (temp < ga->targettemp && engine < ga->maxspeed)) {
  849. if (opt_debug)
  850. applog(LOG_DEBUG, "Temperature below target, increasing clock speed");
  851. newengine = engine + ga->lpOdParameters.sEngineClock.iStep;
  852. }
  853. if (newengine > ga->maxspeed)
  854. newengine = ga->maxspeed;
  855. else if (newengine < ga->minspeed)
  856. newengine = ga->minspeed;
  857. if (newengine != engine) {
  858. newengine /= 100;
  859. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, newengine);
  860. set_engineclock(gpu, newengine);
  861. if (cgpu->gpu_memdiff)
  862. set_memoryclock(gpu, newengine + cgpu->gpu_memdiff);
  863. }
  864. }
  865. ga->lasttemp = temp;
  866. }
  867. void set_defaultfan(int gpu)
  868. {
  869. struct gpu_adl *ga;
  870. if (!gpus[gpu].has_adl || !adl_active)
  871. return;
  872. ga = &gpus[gpu].adl;
  873. lock_adl();
  874. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  875. unlock_adl();
  876. }
  877. void set_defaultengine(int gpu)
  878. {
  879. struct gpu_adl *ga;
  880. if (!gpus[gpu].has_adl || !adl_active)
  881. return;
  882. ga = &gpus[gpu].adl;
  883. lock_adl();
  884. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  885. unlock_adl();
  886. }
  887. void change_autosettings(int gpu)
  888. {
  889. struct gpu_adl *ga = &gpus[gpu].adl;
  890. char input;
  891. int val;
  892. wlogprint("Target temperature: %d\n", ga->targettemp);
  893. wlogprint("Overheat temperature: %d\n", ga->overtemp);
  894. wlogprint("Cutoff temperature: %d\n", ga->cutofftemp);
  895. wlogprint("Toggle [F]an auto [G]PU auto\nChange [T]arget [O]verheat [C]utoff\n");
  896. wlogprint("Or press any other key to continue\n");
  897. input = getch();
  898. if (!strncasecmp(&input, "f", 1)) {
  899. ga->autofan ^= true;
  900. wlogprint("Fan autotune is now %s\n", ga->autofan ? "enabled" : "disabled");
  901. if (!ga->autofan) {
  902. wlogprint("Resetting fan to startup settings\n");
  903. set_defaultfan(gpu);
  904. }
  905. } else if (!strncasecmp(&input, "g", 1)) {
  906. ga->autoengine ^= true;
  907. wlogprint("GPU engine clock autotune is now %s\n", ga->autoengine ? "enabled" : "disabled");
  908. if (!ga->autoengine) {
  909. wlogprint("Resetting GPU engine clock to startup settings\n");
  910. set_defaultengine(gpu);
  911. }
  912. } else if (!strncasecmp(&input, "t", 1)) {
  913. val = curses_int("Enter target temperature for this GPU in C (0-200)");
  914. if (val < 0 || val > 200)
  915. wlogprint("Invalid temperature");
  916. else
  917. ga->targettemp = val;
  918. } else if (!strncasecmp(&input, "o", 1)) {
  919. wlogprint("Enter overheat temperature for this GPU in C (%d+)", ga->targettemp);
  920. val = curses_int("");
  921. if (val <= ga->targettemp || val > 200)
  922. wlogprint("Invalid temperature");
  923. else
  924. ga->overtemp = val;
  925. } else if (!strncasecmp(&input, "c", 1)) {
  926. wlogprint("Enter cutoff temperature for this GPU in C (%d+)", ga->overtemp);
  927. val = curses_int("");
  928. if (val <= ga->overtemp || val > 200)
  929. wlogprint("Invalid temperature");
  930. else
  931. ga->cutofftemp = val;
  932. }
  933. }
  934. void change_gpusettings(int gpu)
  935. {
  936. struct gpu_adl *ga = &gpus[gpu].adl;
  937. float fval, fmin = 0, fmax = 0;
  938. int val, imin = 0, imax = 0;
  939. char input;
  940. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  941. float temp = 0, vddc = 0;
  942. updated:
  943. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune))
  944. wlogprint("Temp: %.1f C\n", temp);
  945. if (fanpercent >= 0 || fanspeed >= 0) {
  946. wlogprint("Fan Speed: ");
  947. if (fanpercent >= 0)
  948. wlogprint("%d%% ", fanpercent);
  949. if (fanspeed >= 0)
  950. wlogprint("(%d RPM)", fanspeed);
  951. wlogprint("\n");
  952. }
  953. wlogprint("Engine Clock: %d MHz\nMemory Clock: %d Mhz\nVddc: %.3f V\nActivity: %d%%\nPowertune: %d%%\n",
  954. engineclock, memclock, vddc, activity, powertune);
  955. wlogprint("Fan autotune is %s (%d-%d)\n", ga->autofan ? "enabled" : "disabled",
  956. gpus[gpu].min_fan, gpus[gpu].gpu_fan);
  957. wlogprint("GPU engine clock autotune is %s (%d-%d)\n", ga->autoengine ? "enabled" : "disabled",
  958. ga->minspeed / 100, ga->maxspeed / 100);
  959. wlogprint("Change [A]utomatic [E]ngine [F]an [M]emory [V]oltage [P]owertune\n");
  960. wlogprint("Or press any other key to continue\n");
  961. input = getch();
  962. if (!strncasecmp(&input, "a", 1)) {
  963. change_autosettings(gpu);
  964. } else if (!strncasecmp(&input, "e", 1)) {
  965. get_enginerange(gpu, &imin, &imax);
  966. wlogprint("Enter GPU engine clock speed (%d - %d Mhz)", imin, imax);
  967. val = curses_int("");
  968. if (val < imin || val > imax) {
  969. wlogprint("Value is outside safe range, are you sure?\n");
  970. input = getch();
  971. if (strncasecmp(&input, "y", 1))
  972. return;
  973. }
  974. if (!set_engineclock(gpu, val))
  975. wlogprint("Driver reports success but check values below\n");
  976. else
  977. wlogprint("Failed to modify engine clock speed\n");
  978. } else if (!strncasecmp(&input, "f", 1)) {
  979. get_fanrange(gpu, &imin, &imax);
  980. wlogprint("Enter fan percentage (%d - %d %)", imin, imax);
  981. val = curses_int("");
  982. if (val < imin || val > imax) {
  983. wlogprint("Value is outside safe range, are you sure?\n");
  984. input = getch();
  985. if (strncasecmp(&input, "y", 1))
  986. return;
  987. }
  988. if (!set_fanspeed(gpu, val))
  989. wlogprint("Driver reports success but check values below\n");
  990. else
  991. wlogprint("Failed to modify fan speed\n");
  992. } else if (!strncasecmp(&input, "m", 1)) {
  993. get_memoryrange(gpu, &imin, &imax);
  994. wlogprint("Enter GPU memory clock speed (%d - %d Mhz)", imin, imax);
  995. val = curses_int("");
  996. if (val < imin || val > imax) {
  997. wlogprint("Value is outside safe range, are you sure?\n");
  998. input = getch();
  999. if (strncasecmp(&input, "y", 1))
  1000. return;
  1001. }
  1002. if (!set_memoryclock(gpu, val))
  1003. wlogprint("Driver reports success but check values below\n");
  1004. else
  1005. wlogprint("Failed to modify memory clock speed\n");
  1006. } else if (!strncasecmp(&input, "v", 1)) {
  1007. get_vddcrange(gpu, &fmin, &fmax);
  1008. wlogprint("Enter GPU voltage (%.3f - %.3f V)", fmin, fmax);
  1009. fval = curses_float("");
  1010. if (fval < fmin || fval > fmax) {
  1011. wlogprint("Value is outside safe range, are you sure?\n");
  1012. input = getch();
  1013. if (strncasecmp(&input, "y", 1))
  1014. return;
  1015. }
  1016. if (!set_vddc(gpu, fval))
  1017. wlogprint("Driver reports success but check values below\n");
  1018. else
  1019. wlogprint("Failed to modify voltage\n");
  1020. } else if (!strncasecmp(&input, "p", 1)) {
  1021. val = curses_int("Enter powertune value (-20 - 20)");
  1022. if (val < -20 || val > 20) {
  1023. wlogprint("Value is outside safe range, are you sure?\n");
  1024. input = getch();
  1025. if (strncasecmp(&input, "y", 1))
  1026. return;
  1027. }
  1028. if (!set_powertune(gpu, val))
  1029. wlogprint("Driver reports success but check values below\n");
  1030. else
  1031. wlogprint("Failed to modify powertune value\n");
  1032. } else {
  1033. clear_logwin();
  1034. return;
  1035. }
  1036. sleep(1);
  1037. goto updated;
  1038. }
  1039. void clear_adl(nDevs)
  1040. {
  1041. struct gpu_adl *ga;
  1042. int i;
  1043. if (!adl_active)
  1044. return;
  1045. lock_adl();
  1046. /* Try to reset values to their defaults */
  1047. for (i = 0; i < nDevs; i++) {
  1048. ga = &gpus[i].adl;
  1049. /* Only reset the values if we've changed them at any time */
  1050. if (!gpus[i].has_adl || !ga->managed)
  1051. continue;
  1052. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  1053. free(ga->DefPerfLev);
  1054. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  1055. ADL_Overdrive5_FanSpeedToDefault_Set(ga->iAdapterIndex, 0);
  1056. }
  1057. ADL_Main_Memory_Free ( (void **)&lpInfo );
  1058. ADL_Main_Control_Destroy ();
  1059. unlock_adl();
  1060. #if defined (LINUX)
  1061. dlclose(hDLL);
  1062. #else
  1063. FreeLibrary(hDLL);
  1064. #endif
  1065. }
  1066. #endif /* HAVE_ADL */