adl.c 41 KB

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