adl.c 43 KB

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