adl.c 39 KB

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