adl.c 43 KB

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