adl.c 43 KB

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