adl.c 44 KB

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