adl.c 42 KB

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