adl.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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. #include "compat.h"
  9. #if defined (__linux)
  10. #include <dlfcn.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #else /* WIN32 */
  14. #include <windows.h>
  15. #include <tchar.h>
  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. 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 result, i, j, devices = 0, last_adapter = -1, gpu = 0, dummy = 0;
  100. struct gpu_adapters adapters[MAX_GPUDEVICES], vadapters[MAX_GPUDEVICES];
  101. bool devs_match = true;
  102. #if defined (LINUX)
  103. hDLL = dlopen( "libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
  104. #else
  105. hDLL = LoadLibrary("atiadlxx.dll");
  106. if (hDLL == NULL)
  107. // A 32 bit calling application on 64 bit OS will fail to LoadLIbrary.
  108. // Try to load the 32 bit library (atiadlxy.dll) instead
  109. hDLL = LoadLibrary("atiadlxy.dll");
  110. #endif
  111. if (hDLL == NULL) {
  112. applog(LOG_INFO, "Unable to load ati adl library");
  113. return;
  114. }
  115. if (unlikely(pthread_mutex_init(&adl_lock, NULL))) {
  116. applog(LOG_ERR, "Failed to init adl_lock in init_adl");
  117. return;
  118. }
  119. ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE) GetProcAddress(hDLL,"ADL_Main_Control_Create");
  120. ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY) GetProcAddress(hDLL,"ADL_Main_Control_Destroy");
  121. ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET) GetProcAddress(hDLL,"ADL_Adapter_NumberOfAdapters_Get");
  122. ADL_Adapter_AdapterInfo_Get = (ADL_ADAPTER_ADAPTERINFO_GET) GetProcAddress(hDLL,"ADL_Adapter_AdapterInfo_Get");
  123. ADL_Adapter_ID_Get = (ADL_ADAPTER_ID_GET) GetProcAddress(hDLL,"ADL_Adapter_ID_Get");
  124. ADL_Overdrive5_Temperature_Get = (ADL_OVERDRIVE5_TEMPERATURE_GET) GetProcAddress(hDLL,"ADL_Overdrive5_Temperature_Get");
  125. ADL_Overdrive5_CurrentActivity_Get = (ADL_OVERDRIVE5_CURRENTACTIVITY_GET) GetProcAddress(hDLL, "ADL_Overdrive5_CurrentActivity_Get");
  126. ADL_Overdrive5_ODParameters_Get = (ADL_OVERDRIVE5_ODPARAMETERS_GET) GetProcAddress(hDLL, "ADL_Overdrive5_ODParameters_Get");
  127. ADL_Overdrive5_FanSpeedInfo_Get = (ADL_OVERDRIVE5_FANSPEEDINFO_GET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeedInfo_Get");
  128. ADL_Overdrive5_FanSpeed_Get = (ADL_OVERDRIVE5_FANSPEED_GET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeed_Get");
  129. ADL_Overdrive5_FanSpeed_Set = (ADL_OVERDRIVE5_FANSPEED_SET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeed_Set");
  130. ADL_Overdrive5_ODPerformanceLevels_Get = (ADL_OVERDRIVE5_ODPERFORMANCELEVELS_GET) GetProcAddress(hDLL, "ADL_Overdrive5_ODPerformanceLevels_Get");
  131. ADL_Overdrive5_ODPerformanceLevels_Set = (ADL_OVERDRIVE5_ODPERFORMANCELEVELS_SET) GetProcAddress(hDLL, "ADL_Overdrive5_ODPerformanceLevels_Set");
  132. ADL_Main_Control_Refresh = (ADL_MAIN_CONTROL_REFRESH) GetProcAddress(hDLL, "ADL_Main_Control_Refresh");
  133. ADL_Overdrive5_PowerControl_Get = (ADL_OVERDRIVE5_POWERCONTROL_GET) GetProcAddress(hDLL, "ADL_Overdrive5_PowerControl_Get");
  134. ADL_Overdrive5_PowerControl_Set = (ADL_OVERDRIVE5_POWERCONTROL_SET) GetProcAddress(hDLL, "ADL_Overdrive5_PowerControl_Set");
  135. ADL_Overdrive5_FanSpeedToDefault_Set = (ADL_OVERDRIVE5_FANSPEEDTODEFAULT_SET) GetProcAddress(hDLL, "ADL_Overdrive5_FanSpeedToDefault_Set");
  136. if (!ADL_Main_Control_Create || !ADL_Main_Control_Destroy ||
  137. !ADL_Adapter_NumberOfAdapters_Get || !ADL_Adapter_AdapterInfo_Get ||
  138. !ADL_Adapter_ID_Get || !ADL_Overdrive5_Temperature_Get ||
  139. !ADL_Overdrive5_CurrentActivity_Get ||
  140. !ADL_Overdrive5_ODParameters_Get || !ADL_Overdrive5_FanSpeedInfo_Get ||
  141. !ADL_Overdrive5_FanSpeed_Get || !ADL_Overdrive5_FanSpeed_Set ||
  142. !ADL_Overdrive5_ODPerformanceLevels_Get || !ADL_Overdrive5_ODPerformanceLevels_Set ||
  143. !ADL_Main_Control_Refresh || !ADL_Overdrive5_PowerControl_Get ||
  144. !ADL_Overdrive5_PowerControl_Set || !ADL_Overdrive5_FanSpeedToDefault_Set) {
  145. applog(LOG_WARNING, "ATI ADL's API is missing");
  146. return;
  147. }
  148. // Initialise ADL. The second parameter is 1, which means:
  149. // retrieve adapter information only for adapters that are physically present and enabled in the system
  150. result = ADL_Main_Control_Create (ADL_Main_Memory_Alloc, 1);
  151. if (result != ADL_OK) {
  152. applog(LOG_INFO, "ADL Initialisation Error! Error %d!", result);
  153. return ;
  154. }
  155. result = ADL_Main_Control_Refresh();
  156. if (result != ADL_OK) {
  157. applog(LOG_INFO, "ADL Refresh Error! Error %d!", result);
  158. return ;
  159. }
  160. // Obtain the number of adapters for the system
  161. result = ADL_Adapter_NumberOfAdapters_Get (&iNumberAdapters);
  162. if (result != ADL_OK) {
  163. applog(LOG_INFO, "Cannot get the number of adapters! Error %d!", result);
  164. return ;
  165. }
  166. if (iNumberAdapters > 0) {
  167. lpInfo = malloc ( sizeof (AdapterInfo) * iNumberAdapters );
  168. memset ( lpInfo,'\0', sizeof (AdapterInfo) * iNumberAdapters );
  169. lpInfo->iSize = sizeof(lpInfo);
  170. // Get the AdapterInfo structure for all adapters in the system
  171. result = ADL_Adapter_AdapterInfo_Get (lpInfo, sizeof (AdapterInfo) * iNumberAdapters);
  172. if (result != ADL_OK) {
  173. applog(LOG_INFO, "ADL_Adapter_AdapterInfo_Get Error! Error %d", result);
  174. return ;
  175. }
  176. } else {
  177. applog(LOG_INFO, "No adapters found");
  178. return;
  179. }
  180. /* Iterate over iNumberAdapters and find the lpAdapterID of real devices */
  181. for (i = 0; i < iNumberAdapters; i++) {
  182. int iAdapterIndex;
  183. int lpAdapterID;
  184. iAdapterIndex = lpInfo[i].iAdapterIndex;
  185. /* Get unique identifier of the adapter, 0 means not AMD */
  186. result = ADL_Adapter_ID_Get(iAdapterIndex, &lpAdapterID);
  187. if (result != ADL_OK) {
  188. applog(LOG_INFO, "Failed to ADL_Adapter_ID_Get. Error %d", result);
  189. continue;
  190. }
  191. /* Each adapter may have multiple entries */
  192. if (lpAdapterID == last_adapter)
  193. continue;
  194. applog(LOG_DEBUG, "GPU %d "
  195. "iAdapterIndex %d "
  196. "strUDID %s "
  197. "iBusNumber %d "
  198. "iDeviceNumber %d "
  199. "iFunctionNumber %d "
  200. "iVendorID %d "
  201. "strAdapterName %s ",
  202. devices,
  203. iAdapterIndex,
  204. lpInfo[i].strUDID,
  205. lpInfo[i].iBusNumber,
  206. lpInfo[i].iDeviceNumber,
  207. lpInfo[i].iFunctionNumber,
  208. lpInfo[i].iVendorID,
  209. lpInfo[i].strAdapterName);
  210. adapters[devices].iAdapterIndex = iAdapterIndex;
  211. adapters[devices].iBusNumber = lpInfo[i].iBusNumber;
  212. adapters[devices].id = i;
  213. /* We found a truly new adapter instead of a logical
  214. * one. Now since there's no way of correlating the
  215. * opencl enumerated devices and the ADL enumerated
  216. * ones, we have to assume they're in the same order.*/
  217. if (++devices > nDevs) {
  218. applog(LOG_ERR, "ADL found more devices than opencl!");
  219. applog(LOG_ERR, "There is possibly at least one GPU that doesn't support OpenCL");
  220. devs_match = false;
  221. devices = nDevs;
  222. break;
  223. }
  224. last_adapter = lpAdapterID;
  225. if (!lpAdapterID) {
  226. applog(LOG_INFO, "Adapter returns ID 0 meaning not AMD. Card order might be confused");
  227. continue;
  228. }
  229. }
  230. if (devices < nDevs) {
  231. applog(LOG_ERR, "ADL found less devices than opencl!");
  232. applog(LOG_ERR, "There is possibly more than one display attached to a GPU");
  233. devs_match = false;
  234. }
  235. for (i = 0; i < nDevs; i++) {
  236. vadapters[i].virtual_gpu = i;
  237. vadapters[i].id = adapters[i].id;
  238. }
  239. if (!devs_match) {
  240. applog(LOG_ERR, "WARNING: Number of OpenCL and ADL devices does not match!");
  241. applog(LOG_ERR, "Hardware monitoring may NOT match up with devices!");
  242. } else if (opt_reorder) {
  243. /* Windows has some kind of random ordering for bus number IDs and
  244. * ordering the GPUs according to ascending order fixes it. Linux
  245. * has usually sequential but decreasing order instead! */
  246. for (i = 0; i < devices; i++) {
  247. int j, virtual_gpu;
  248. virtual_gpu = 0;
  249. for (j = 0; j < devices; j++) {
  250. if (i == j)
  251. continue;
  252. #ifdef WIN32
  253. if (adapters[j].iBusNumber < adapters[i].iBusNumber)
  254. #else
  255. if (adapters[j].iBusNumber > adapters[i].iBusNumber)
  256. #endif
  257. virtual_gpu++;
  258. }
  259. if (virtual_gpu != i) {
  260. applog(LOG_INFO, "Mapping device %d to GPU %d according to Bus Number order",
  261. i, virtual_gpu);
  262. vadapters[virtual_gpu].virtual_gpu = i;
  263. vadapters[virtual_gpu].id = adapters[i].id;
  264. }
  265. }
  266. }
  267. for (gpu = 0; gpu < devices; gpu++) {
  268. struct gpu_adl *ga;
  269. int iAdapterIndex;
  270. int lpAdapterID;
  271. ADLODPerformanceLevels *lpOdPerformanceLevels;
  272. int lev;
  273. i = vadapters[gpu].id;
  274. iAdapterIndex = lpInfo[i].iAdapterIndex;
  275. gpus[gpu].virtual_gpu = vadapters[gpu].virtual_gpu;
  276. /* Get unique identifier of the adapter, 0 means not AMD */
  277. result = ADL_Adapter_ID_Get(iAdapterIndex, &lpAdapterID);
  278. if (result != ADL_OK) {
  279. applog(LOG_INFO, "Failed to ADL_Adapter_ID_Get. Error %d", result);
  280. continue;
  281. }
  282. if (!gpus[gpu].enabled) {
  283. gpus[i].gpu_engine =
  284. gpus[i].gpu_memclock =
  285. gpus[i].gpu_vddc =
  286. gpus[i].gpu_fan =
  287. gpus[i].gpu_powertune = 0;
  288. continue;
  289. }
  290. applog(LOG_INFO, "GPU %d %s hardware monitoring enabled", gpu, lpInfo[i].strAdapterName);
  291. gpus[gpu].has_adl = true;
  292. /* Flag adl as active if any card is successfully activated */
  293. adl_active = true;
  294. /* From here on we know this device is a discrete device and
  295. * should support ADL */
  296. ga = &gpus[gpu].adl;
  297. ga->gpu = gpu;
  298. ga->iAdapterIndex = iAdapterIndex;
  299. ga->lpAdapterID = lpAdapterID;
  300. strcpy(ga->strAdapterName, lpInfo[i].strAdapterName);
  301. ga->DefPerfLev = NULL;
  302. ga->twin = NULL;
  303. ga->lpOdParameters.iSize = sizeof(ADLODParameters);
  304. if (ADL_Overdrive5_ODParameters_Get(iAdapterIndex, &ga->lpOdParameters) != ADL_OK)
  305. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODParameters_Get");
  306. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  307. /* We're only interested in the top performance level */
  308. lpOdPerformanceLevels = malloc(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  309. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  310. /* Get default performance levels first */
  311. if (ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 1, lpOdPerformanceLevels) != ADL_OK)
  312. applog(LOG_INFO, "Failed to ADL_Overdrive5_ODPerformanceLevels_Get");
  313. /* Set the limits we'd use based on default gpu speeds */
  314. ga->maxspeed = ga->minspeed = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  315. ga->lpTemperature.iSize = sizeof(ADLTemperature);
  316. ga->lpFanSpeedInfo.iSize = sizeof(ADLFanSpeedInfo);
  317. ga->lpFanSpeedValue.iSize = ga->DefFanSpeedValue.iSize = sizeof(ADLFanSpeedValue);
  318. /* Now get the current performance levels for any existing overclock */
  319. ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
  320. /* Save these values as the defaults in case we wish to reset to defaults */
  321. ga->DefPerfLev = lpOdPerformanceLevels;
  322. if (gpus[gpu].gpu_engine) {
  323. int setengine = gpus[gpu].gpu_engine * 100;
  324. /* Lower profiles can't have a higher setting */
  325. for (j = 0; j < lev; j++) {
  326. if (lpOdPerformanceLevels->aLevels[j].iEngineClock > setengine)
  327. lpOdPerformanceLevels->aLevels[j].iEngineClock = setengine;
  328. }
  329. lpOdPerformanceLevels->aLevels[lev].iEngineClock = setengine;
  330. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, gpus[gpu].gpu_engine);
  331. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  332. ga->maxspeed = setengine;
  333. if (gpus[gpu].min_engine)
  334. ga->minspeed = gpus[gpu].min_engine * 100;
  335. ga->managed = true;
  336. if (gpus[gpu].gpu_memdiff)
  337. set_memoryclock(gpu, gpus[gpu].gpu_engine + gpus[gpu].gpu_memdiff);
  338. }
  339. if (gpus[gpu].gpu_memclock) {
  340. int setmem = gpus[gpu].gpu_memclock * 100;
  341. for (j = 0; j < lev; j++) {
  342. if (lpOdPerformanceLevels->aLevels[j].iMemoryClock > setmem)
  343. lpOdPerformanceLevels->aLevels[j].iMemoryClock = setmem;
  344. }
  345. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = setmem;
  346. applog(LOG_INFO, "Setting GPU %d memory clock to %d", gpu, gpus[gpu].gpu_memclock);
  347. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  348. ga->managed = true;
  349. }
  350. if (gpus[gpu].gpu_vddc) {
  351. int setv = gpus[gpu].gpu_vddc * 1000;
  352. for (j = 0; j < lev; j++) {
  353. if (lpOdPerformanceLevels->aLevels[j].iVddc > setv)
  354. lpOdPerformanceLevels->aLevels[j].iVddc = setv;
  355. }
  356. lpOdPerformanceLevels->aLevels[lev].iVddc = setv;
  357. applog(LOG_INFO, "Setting GPU %d voltage to %.3f", gpu, gpus[gpu].gpu_vddc);
  358. ADL_Overdrive5_ODPerformanceLevels_Set(iAdapterIndex, lpOdPerformanceLevels);
  359. ga->managed = true;
  360. }
  361. ADL_Overdrive5_ODPerformanceLevels_Get(iAdapterIndex, 0, lpOdPerformanceLevels);
  362. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  363. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  364. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  365. ga->iBusNumber = lpInfo[i].iBusNumber;
  366. if (ADL_Overdrive5_FanSpeedInfo_Get(iAdapterIndex, 0, &ga->lpFanSpeedInfo) != ADL_OK)
  367. applog(LOG_INFO, "Failed to ADL_Overdrive5_FanSpeedInfo_Get");
  368. else
  369. ga->has_fanspeed = true;
  370. /* Save the fanspeed values as defaults in case we reset later */
  371. ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  372. if (gpus[gpu].gpu_fan)
  373. set_fanspeed(gpu, gpus[gpu].gpu_fan);
  374. else
  375. gpus[gpu].gpu_fan = 85; /* Set a nominal upper limit of 85% */
  376. /* Not fatal if powercontrol get fails */
  377. if (ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy) != ADL_OK)
  378. applog(LOG_INFO, "Failed to ADL_Overdrive5_PowerControl_get");
  379. if (gpus[gpu].gpu_powertune) {
  380. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, gpus[gpu].gpu_powertune);
  381. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  382. ga->managed = true;
  383. }
  384. /* Set some default temperatures for autotune when enabled */
  385. if (!ga->targettemp)
  386. ga->targettemp = opt_targettemp;
  387. if (!ga->overtemp)
  388. ga->overtemp = opt_overheattemp;
  389. if (!gpus[gpu].cutofftemp)
  390. gpus[gpu].cutofftemp = opt_cutofftemp;
  391. if (opt_autofan) {
  392. ga->autofan = true;
  393. /* Set a safe starting default if we're automanaging fan speeds */
  394. set_fanspeed(gpu, gpus[gpu].gpu_fan);
  395. }
  396. if (opt_autoengine) {
  397. ga->autoengine = true;
  398. ga->managed = true;
  399. }
  400. ga->lasttemp = __gpu_temp(ga);
  401. }
  402. for (gpu = 0; gpu < devices; gpu++) {
  403. struct gpu_adl *ga = &gpus[gpu].adl;
  404. int j;
  405. for (j = 0; j < devices; j++) {
  406. struct gpu_adl *other_ga;
  407. if (j == gpu)
  408. continue;
  409. other_ga = &gpus[j].adl;
  410. /* Search for twin GPUs on a single card. They will be
  411. * separated by one bus id and one will have fanspeed
  412. * while the other won't. */
  413. if (!ga->has_fanspeed) {
  414. if (fanspeed_twin(ga, other_ga)) {
  415. applog(LOG_INFO, "Dual GPUs detected: %d and %d",
  416. ga->gpu, other_ga->gpu);
  417. ga->twin = other_ga;
  418. other_ga->twin = ga;
  419. }
  420. }
  421. }
  422. }
  423. }
  424. static float __gpu_temp(struct gpu_adl *ga)
  425. {
  426. if (ADL_Overdrive5_Temperature_Get(ga->iAdapterIndex, 0, &ga->lpTemperature) != ADL_OK)
  427. return -1;
  428. return (float)ga->lpTemperature.iTemperature / 1000;
  429. }
  430. float gpu_temp(int gpu)
  431. {
  432. struct gpu_adl *ga;
  433. float ret = -1;
  434. if (!gpus[gpu].has_adl || !adl_active)
  435. return ret;
  436. ga = &gpus[gpu].adl;
  437. lock_adl();
  438. ret = __gpu_temp(ga);
  439. unlock_adl();
  440. gpus[gpu].temp = ret;
  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. /* Keep track of intended engine clock in case the device changes
  636. * profile and drops while idle, not taking the new engine clock */
  637. ga->lastengine = iEngineClock;
  638. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  639. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  640. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  641. lock_adl();
  642. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  643. goto out;
  644. for (i = 0; i < lev; i++) {
  645. if (lpOdPerformanceLevels->aLevels[i].iEngineClock > iEngineClock)
  646. lpOdPerformanceLevels->aLevels[i].iEngineClock = iEngineClock;
  647. }
  648. lpOdPerformanceLevels->aLevels[lev].iEngineClock = iEngineClock;
  649. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  650. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  651. if (lpOdPerformanceLevels->aLevels[lev].iEngineClock == iEngineClock)
  652. ret = 0;
  653. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  654. if (ga->iEngineClock > ga->maxspeed)
  655. ga->maxspeed = ga->iEngineClock;
  656. if (ga->iEngineClock < ga->minspeed)
  657. ga->minspeed = ga->iEngineClock;
  658. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  659. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  660. ga->managed = true;
  661. out:
  662. unlock_adl();
  663. return ret;
  664. }
  665. static void get_memoryrange(int gpu, int *imin, int *imax)
  666. {
  667. struct gpu_adl *ga;
  668. if (!gpus[gpu].has_adl || !adl_active) {
  669. wlogprint("Get memoryrange not supported\n");
  670. return;
  671. }
  672. ga = &gpus[gpu].adl;
  673. *imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
  674. *imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
  675. }
  676. int set_memoryclock(int gpu, int iMemoryClock)
  677. {
  678. ADLODPerformanceLevels *lpOdPerformanceLevels;
  679. int i, lev, ret = 1;
  680. struct gpu_adl *ga;
  681. if (!gpus[gpu].has_adl || !adl_active) {
  682. wlogprint("Set memoryclock not supported\n");
  683. return ret;
  684. }
  685. iMemoryClock *= 100;
  686. ga = &gpus[gpu].adl;
  687. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  688. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  689. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  690. lock_adl();
  691. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  692. goto out;
  693. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
  694. for (i = 0; i < lev; i++) {
  695. if (lpOdPerformanceLevels->aLevels[i].iMemoryClock > iMemoryClock)
  696. lpOdPerformanceLevels->aLevels[i].iMemoryClock = iMemoryClock;
  697. }
  698. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  699. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  700. if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock == iMemoryClock)
  701. ret = 0;
  702. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  703. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  704. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  705. ga->managed = true;
  706. out:
  707. unlock_adl();
  708. return ret;
  709. }
  710. static void get_vddcrange(int gpu, float *imin, float *imax)
  711. {
  712. struct gpu_adl *ga;
  713. if (!gpus[gpu].has_adl || !adl_active) {
  714. wlogprint("Get vddcrange not supported\n");
  715. return;
  716. }
  717. ga = &gpus[gpu].adl;
  718. *imin = (float)ga->lpOdParameters.sVddc.iMin / 1000;
  719. *imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
  720. }
  721. static float curses_float(const char *query)
  722. {
  723. float ret;
  724. char *cvar;
  725. cvar = curses_input(query);
  726. ret = atof(cvar);
  727. free(cvar);
  728. return ret;
  729. }
  730. int set_vddc(int gpu, float fVddc)
  731. {
  732. ADLODPerformanceLevels *lpOdPerformanceLevels;
  733. int i, iVddc, lev, ret = 1;
  734. struct gpu_adl *ga;
  735. if (!gpus[gpu].has_adl || !adl_active) {
  736. wlogprint("Set vddc not supported\n");
  737. return ret;
  738. }
  739. iVddc = 1000 * fVddc;
  740. ga = &gpus[gpu].adl;
  741. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  742. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  743. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  744. lock_adl();
  745. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  746. goto out;
  747. for (i = 0; i < lev; i++) {
  748. if (lpOdPerformanceLevels->aLevels[i].iVddc > iVddc)
  749. lpOdPerformanceLevels->aLevels[i].iVddc = iVddc;
  750. }
  751. lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
  752. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  753. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  754. if (lpOdPerformanceLevels->aLevels[lev].iVddc == iVddc)
  755. ret = 0;
  756. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  757. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  758. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  759. ga->managed = true;
  760. out:
  761. unlock_adl();
  762. return ret;
  763. }
  764. static void get_fanrange(int gpu, int *imin, int *imax)
  765. {
  766. struct gpu_adl *ga;
  767. if (!gpus[gpu].has_adl || !adl_active) {
  768. wlogprint("Get fanrange not supported\n");
  769. return;
  770. }
  771. ga = &gpus[gpu].adl;
  772. *imin = ga->lpFanSpeedInfo.iMinPercent;
  773. *imax = ga->lpFanSpeedInfo.iMaxPercent;
  774. }
  775. int set_fanspeed(int gpu, int iFanSpeed)
  776. {
  777. struct gpu_adl *ga;
  778. int ret = 1;
  779. if (!gpus[gpu].has_adl || !adl_active) {
  780. wlogprint("Set fanspeed not supported\n");
  781. return ret;
  782. }
  783. ga = &gpus[gpu].adl;
  784. if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE ))) {
  785. applog(LOG_DEBUG, "GPU %d doesn't support rpm or percent write", gpu);
  786. return ret;
  787. }
  788. /* Store what fanspeed we're actually aiming for for re-entrant changes
  789. * in case this device does not support fine setting changes */
  790. ga->targetfan = iFanSpeed;
  791. lock_adl();
  792. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK) {
  793. applog(LOG_DEBUG, "GPU %d call to fanspeed get failed", gpu);
  794. }
  795. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE)) {
  796. /* Must convert speed to an RPM */
  797. iFanSpeed = ga->lpFanSpeedInfo.iMaxRPM * iFanSpeed / 100;
  798. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  799. } else
  800. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  801. if (!(ga->lpFanSpeedValue.iFlags & ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED)) {
  802. /* If user defined is not already specified, set it first */
  803. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  804. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  805. }
  806. ga->lpFanSpeedValue.iFanSpeed = iFanSpeed;
  807. ret = ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  808. ga->managed = true;
  809. unlock_adl();
  810. return ret;
  811. }
  812. static int set_powertune(int gpu, int iPercentage)
  813. {
  814. struct gpu_adl *ga;
  815. int dummy, ret = 1;
  816. if (!gpus[gpu].has_adl || !adl_active) {
  817. wlogprint("Set powertune not supported\n");
  818. return ret;
  819. }
  820. ga = &gpus[gpu].adl;
  821. lock_adl();
  822. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, iPercentage);
  823. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  824. if (ga->iPercentage == iPercentage)
  825. ret = 0;
  826. ga->managed = true;
  827. unlock_adl();
  828. return ret;
  829. }
  830. static void fan_autotune(int gpu, int temp, int fanpercent, bool __maybe_unused *fan_optimal)
  831. {
  832. struct cgpu_info *cgpu = &gpus[gpu];
  833. struct gpu_adl *ga = &cgpu->adl;
  834. int top = gpus[gpu].gpu_fan;
  835. int bot = gpus[gpu].min_fan;
  836. int newpercent = fanpercent;
  837. int iMin = 0, iMax = 100;
  838. get_fanrange(gpu, &iMin, &iMax);
  839. if (temp > ga->overtemp && fanpercent < iMax) {
  840. applog(LOG_WARNING, "Overheat detected on GPU %d, increasing fan to 100%", gpu);
  841. newpercent = iMax;
  842. } else if (temp > ga->targettemp && fanpercent < top && temp >= ga->lasttemp) {
  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. applog(LOG_DEBUG, "Temperature %d degrees below target, decreasing fanspeed", opt_hysteresis);
  852. newpercent = ga->targetfan - 1;
  853. } else {
  854. /* We're in the optimal range, make minor adjustments if the
  855. * temp is still drifting */
  856. if (fanpercent > bot && temp < ga->lasttemp && ga->lasttemp < ga->targettemp) {
  857. applog(LOG_DEBUG, "Temperature dropping while in target range, decreasing fanspeed");
  858. newpercent = ga->targetfan - 1;
  859. } else if (fanpercent < top && temp > ga->lasttemp && temp > ga->targettemp - opt_hysteresis) {
  860. applog(LOG_DEBUG, "Temperature rising while in target range, increasing fanspeed");
  861. newpercent = ga->targetfan + 1;
  862. }
  863. }
  864. if (newpercent > iMax)
  865. newpercent = iMax;
  866. else if (newpercent < iMin)
  867. newpercent = iMin;
  868. if (newpercent != fanpercent) {
  869. fan_optimal = false;
  870. applog(LOG_INFO, "Setting GPU %d fan percentage to %d", gpu, newpercent);
  871. set_fanspeed(gpu, newpercent);
  872. }
  873. }
  874. void gpu_autotune(int gpu, bool *enable)
  875. {
  876. int temp, fanpercent, engine, newengine, twintemp = 0;
  877. bool fan_optimal = true;
  878. struct cgpu_info *cgpu;
  879. struct gpu_adl *ga;
  880. cgpu = &gpus[gpu];
  881. ga = &cgpu->adl;
  882. lock_adl();
  883. ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity);
  884. temp = __gpu_temp(ga);
  885. if (ga->twin)
  886. twintemp = __gpu_temp(ga->twin);
  887. fanpercent = __gpu_fanpercent(ga);
  888. unlock_adl();
  889. newengine = engine = gpu_engineclock(gpu) * 100;
  890. if (temp && fanpercent >= 0 && ga->autofan) {
  891. if (!ga->twin)
  892. fan_autotune(gpu, temp, fanpercent, &fan_optimal);
  893. else if (ga->autofan && (ga->has_fanspeed || !ga->twin->autofan)) {
  894. /* On linked GPUs, we autotune the fan only once, based
  895. * on the highest temperature from either GPUs */
  896. int hightemp, fan_gpu;
  897. if (twintemp > temp)
  898. hightemp = twintemp;
  899. else
  900. hightemp = temp;
  901. if (ga->has_fanspeed)
  902. fan_gpu = gpu;
  903. else
  904. fan_gpu = ga->twin->gpu;
  905. fan_autotune(fan_gpu, hightemp, fanpercent, &fan_optimal);
  906. }
  907. }
  908. if (engine && ga->autoengine) {
  909. if (temp > cgpu->cutofftemp) {
  910. applog(LOG_WARNING, "Hit thermal cutoff limit on GPU %d, disabling!", gpu);
  911. *enable = false;
  912. newengine = ga->minspeed;
  913. } else if (temp > ga->overtemp && engine > ga->minspeed) {
  914. applog(LOG_WARNING, "Overheat detected, decreasing GPU %d clock speed", gpu);
  915. newengine = ga->minspeed;
  916. } else if (temp > ga->targettemp + opt_hysteresis && engine > ga->minspeed && fan_optimal) {
  917. applog(LOG_DEBUG, "Temperature %d degrees over target, decreasing clock speed", opt_hysteresis);
  918. newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
  919. /* Only try to tune engine speed up if this GPU is not disabled */
  920. } else if (temp < ga->targettemp && engine < ga->maxspeed && *enable) {
  921. applog(LOG_DEBUG, "Temperature below target, increasing clock speed");
  922. newengine = engine + ga->lpOdParameters.sEngineClock.iStep;
  923. }
  924. if (newengine > ga->maxspeed)
  925. newengine = ga->maxspeed;
  926. else if (newengine < ga->minspeed)
  927. newengine = ga->minspeed;
  928. /* Adjust engine clock speed if it's lower, or if it's higher
  929. * but higher than the last intended value as well as the
  930. * current speed, to avoid setting the engine clock speed to
  931. * a speed relateive to a lower profile during idle periods. */
  932. if (newengine < engine || (newengine > engine && newengine > ga->lastengine)) {
  933. newengine /= 100;
  934. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, newengine);
  935. set_engineclock(gpu, newengine);
  936. if (cgpu->gpu_memdiff)
  937. set_memoryclock(gpu, newengine + cgpu->gpu_memdiff);
  938. }
  939. }
  940. ga->lasttemp = temp;
  941. }
  942. void set_defaultfan(int gpu)
  943. {
  944. struct gpu_adl *ga;
  945. if (!gpus[gpu].has_adl || !adl_active)
  946. return;
  947. ga = &gpus[gpu].adl;
  948. lock_adl();
  949. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  950. unlock_adl();
  951. }
  952. void set_defaultengine(int gpu)
  953. {
  954. struct gpu_adl *ga;
  955. if (!gpus[gpu].has_adl || !adl_active)
  956. return;
  957. ga = &gpus[gpu].adl;
  958. lock_adl();
  959. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  960. unlock_adl();
  961. }
  962. void change_autosettings(int gpu)
  963. {
  964. struct gpu_adl *ga = &gpus[gpu].adl;
  965. char input;
  966. int val;
  967. wlogprint("Target temperature: %d\n", ga->targettemp);
  968. wlogprint("Overheat temperature: %d\n", ga->overtemp);
  969. wlogprint("Cutoff temperature: %d\n", gpus[gpu].cutofftemp);
  970. wlogprint("Toggle [F]an auto [G]PU auto\nChange [T]arget [O]verheat [C]utoff\n");
  971. wlogprint("Or press any other key to continue\n");
  972. input = getch();
  973. if (!strncasecmp(&input, "f", 1)) {
  974. ga->autofan ^= true;
  975. wlogprint("Fan autotune is now %s\n", ga->autofan ? "enabled" : "disabled");
  976. if (!ga->autofan) {
  977. wlogprint("Resetting fan to startup settings\n");
  978. set_defaultfan(gpu);
  979. }
  980. } else if (!strncasecmp(&input, "g", 1)) {
  981. ga->autoengine ^= true;
  982. wlogprint("GPU engine clock autotune is now %s\n", ga->autoengine ? "enabled" : "disabled");
  983. if (!ga->autoengine) {
  984. wlogprint("Resetting GPU engine clock to startup settings\n");
  985. set_defaultengine(gpu);
  986. }
  987. } else if (!strncasecmp(&input, "t", 1)) {
  988. val = curses_int("Enter target temperature for this GPU in C (0-200)");
  989. if (val < 0 || val > 200)
  990. wlogprint("Invalid temperature");
  991. else
  992. ga->targettemp = val;
  993. } else if (!strncasecmp(&input, "o", 1)) {
  994. wlogprint("Enter overheat temperature for this GPU in C (%d+)", ga->targettemp);
  995. val = curses_int("");
  996. if (val <= ga->targettemp || val > 200)
  997. wlogprint("Invalid temperature");
  998. else
  999. ga->overtemp = val;
  1000. } else if (!strncasecmp(&input, "c", 1)) {
  1001. wlogprint("Enter cutoff temperature for this GPU in C (%d+)", ga->overtemp);
  1002. val = curses_int("");
  1003. if (val <= ga->overtemp || val > 200)
  1004. wlogprint("Invalid temperature");
  1005. else
  1006. gpus[gpu].cutofftemp = val;
  1007. }
  1008. }
  1009. void change_gpusettings(int gpu)
  1010. {
  1011. struct gpu_adl *ga = &gpus[gpu].adl;
  1012. float fval, fmin = 0, fmax = 0;
  1013. int val, imin = 0, imax = 0;
  1014. char input;
  1015. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  1016. float temp = 0, vddc = 0;
  1017. updated:
  1018. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune))
  1019. wlogprint("Temp: %.1f C\n", temp);
  1020. if (fanpercent >= 0 || fanspeed >= 0) {
  1021. wlogprint("Fan Speed: ");
  1022. if (fanpercent >= 0)
  1023. wlogprint("%d%% ", fanpercent);
  1024. if (fanspeed >= 0)
  1025. wlogprint("(%d RPM)", fanspeed);
  1026. wlogprint("\n");
  1027. }
  1028. wlogprint("Engine Clock: %d MHz\nMemory Clock: %d Mhz\nVddc: %.3f V\nActivity: %d%%\nPowertune: %d%%\n",
  1029. engineclock, memclock, vddc, activity, powertune);
  1030. wlogprint("Fan autotune is %s (%d-%d)\n", ga->autofan ? "enabled" : "disabled",
  1031. gpus[gpu].min_fan, gpus[gpu].gpu_fan);
  1032. wlogprint("GPU engine clock autotune is %s (%d-%d)\n", ga->autoengine ? "enabled" : "disabled",
  1033. ga->minspeed / 100, ga->maxspeed / 100);
  1034. wlogprint("Change [A]utomatic [E]ngine [F]an [M]emory [V]oltage [P]owertune\n");
  1035. wlogprint("Or press any other key to continue\n");
  1036. input = getch();
  1037. if (!strncasecmp(&input, "a", 1)) {
  1038. change_autosettings(gpu);
  1039. } else if (!strncasecmp(&input, "e", 1)) {
  1040. get_enginerange(gpu, &imin, &imax);
  1041. wlogprint("Enter GPU engine clock speed (%d - %d Mhz)", imin, imax);
  1042. val = curses_int("");
  1043. if (val < imin || val > imax) {
  1044. wlogprint("Value is outside safe range, are you sure?\n");
  1045. input = getch();
  1046. if (strncasecmp(&input, "y", 1))
  1047. return;
  1048. }
  1049. if (!set_engineclock(gpu, val))
  1050. wlogprint("Driver reports success but check values below\n");
  1051. else
  1052. wlogprint("Failed to modify engine clock speed\n");
  1053. } else if (!strncasecmp(&input, "f", 1)) {
  1054. get_fanrange(gpu, &imin, &imax);
  1055. wlogprint("Enter fan percentage (%d - %d %)", imin, imax);
  1056. val = curses_int("");
  1057. if (val < imin || val > imax) {
  1058. wlogprint("Value is outside safe range, are you sure?\n");
  1059. input = getch();
  1060. if (strncasecmp(&input, "y", 1))
  1061. return;
  1062. }
  1063. if (!set_fanspeed(gpu, val))
  1064. wlogprint("Driver reports success but check values below\n");
  1065. else
  1066. wlogprint("Failed to modify fan speed\n");
  1067. } else if (!strncasecmp(&input, "m", 1)) {
  1068. get_memoryrange(gpu, &imin, &imax);
  1069. wlogprint("Enter GPU memory clock speed (%d - %d Mhz)", imin, imax);
  1070. val = curses_int("");
  1071. if (val < imin || val > imax) {
  1072. wlogprint("Value is outside safe range, are you sure?\n");
  1073. input = getch();
  1074. if (strncasecmp(&input, "y", 1))
  1075. return;
  1076. }
  1077. if (!set_memoryclock(gpu, val))
  1078. wlogprint("Driver reports success but check values below\n");
  1079. else
  1080. wlogprint("Failed to modify memory clock speed\n");
  1081. } else if (!strncasecmp(&input, "v", 1)) {
  1082. get_vddcrange(gpu, &fmin, &fmax);
  1083. wlogprint("Enter GPU voltage (%.3f - %.3f V)", fmin, fmax);
  1084. fval = curses_float("");
  1085. if (fval < fmin || fval > fmax) {
  1086. wlogprint("Value is outside safe range, are you sure?\n");
  1087. input = getch();
  1088. if (strncasecmp(&input, "y", 1))
  1089. return;
  1090. }
  1091. if (!set_vddc(gpu, fval))
  1092. wlogprint("Driver reports success but check values below\n");
  1093. else
  1094. wlogprint("Failed to modify voltage\n");
  1095. } else if (!strncasecmp(&input, "p", 1)) {
  1096. val = curses_int("Enter powertune value (-20 - 20)");
  1097. if (val < -20 || val > 20) {
  1098. wlogprint("Value is outside safe range, are you sure?\n");
  1099. input = getch();
  1100. if (strncasecmp(&input, "y", 1))
  1101. return;
  1102. }
  1103. if (!set_powertune(gpu, val))
  1104. wlogprint("Driver reports success but check values below\n");
  1105. else
  1106. wlogprint("Failed to modify powertune value\n");
  1107. } else {
  1108. clear_logwin();
  1109. return;
  1110. }
  1111. sleep(1);
  1112. goto updated;
  1113. }
  1114. void clear_adl(int nDevs)
  1115. {
  1116. struct gpu_adl *ga;
  1117. int i;
  1118. if (!adl_active)
  1119. return;
  1120. lock_adl();
  1121. /* Try to reset values to their defaults */
  1122. for (i = 0; i < nDevs; i++) {
  1123. ga = &gpus[i].adl;
  1124. /* Only reset the values if we've changed them at any time */
  1125. if (!gpus[i].has_adl || !ga->managed)
  1126. continue;
  1127. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  1128. free(ga->DefPerfLev);
  1129. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  1130. ADL_Overdrive5_FanSpeedToDefault_Set(ga->iAdapterIndex, 0);
  1131. }
  1132. ADL_Main_Memory_Free ( (void **)&lpInfo );
  1133. ADL_Main_Control_Destroy ();
  1134. unlock_adl();
  1135. #if defined (LINUX)
  1136. dlclose(hDLL);
  1137. #else
  1138. FreeLibrary(hDLL);
  1139. #endif
  1140. }
  1141. #endif /* HAVE_ADL */