adl.c 39 KB

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