adl.c 43 KB

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