adl.c 40 KB

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