adl.c 43 KB

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