adl.c 42 KB

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