adl.c 44 KB

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