adl.c 44 KB

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