adl.c 39 KB

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