adl.c 43 KB

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