adl.c 39 KB

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