adl.c 46 KB

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