adl.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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. ga->autofan = true;
  437. /* Set a safe starting default if we're automanaging fan speeds */
  438. set_fanspeed(gpu, 50);
  439. }
  440. if (opt_autoengine) {
  441. ga->autoengine = true;
  442. ga->managed = true;
  443. }
  444. gpus[gpu].temp =
  445. ga->lasttemp = __gpu_temp(ga);
  446. }
  447. for (gpu = 0; gpu < devices; gpu++) {
  448. struct gpu_adl *ga = &gpus[gpu].adl;
  449. int j;
  450. for (j = 0; j < devices; j++) {
  451. struct gpu_adl *other_ga;
  452. if (j == gpu)
  453. continue;
  454. other_ga = &gpus[j].adl;
  455. /* Search for twin GPUs on a single card. They will be
  456. * separated by one bus id and one will have fanspeed
  457. * while the other won't. */
  458. if (!ga->has_fanspeed) {
  459. if (fanspeed_twin(ga, other_ga)) {
  460. applog(LOG_INFO, "Dual GPUs detected: %d and %d",
  461. ga->gpu, other_ga->gpu);
  462. ga->twin = other_ga;
  463. other_ga->twin = ga;
  464. }
  465. }
  466. }
  467. }
  468. }
  469. static float __gpu_temp(struct gpu_adl *ga)
  470. {
  471. if (ADL_Overdrive5_Temperature_Get(ga->iAdapterIndex, 0, &ga->lpTemperature) != ADL_OK)
  472. return -1;
  473. return (float)ga->lpTemperature.iTemperature / 1000;
  474. }
  475. float gpu_temp(int gpu)
  476. {
  477. struct gpu_adl *ga;
  478. float ret = -1;
  479. if (!gpus[gpu].has_adl || !adl_active)
  480. return ret;
  481. ga = &gpus[gpu].adl;
  482. lock_adl();
  483. ret = __gpu_temp(ga);
  484. unlock_adl();
  485. gpus[gpu].temp = ret;
  486. return ret;
  487. }
  488. static inline int __gpu_engineclock(struct gpu_adl *ga)
  489. {
  490. return ga->lpActivity.iEngineClock / 100;
  491. }
  492. int gpu_engineclock(int gpu)
  493. {
  494. struct gpu_adl *ga;
  495. int ret = -1;
  496. if (!gpus[gpu].has_adl || !adl_active)
  497. return ret;
  498. ga = &gpus[gpu].adl;
  499. lock_adl();
  500. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  501. goto out;
  502. ret = __gpu_engineclock(ga);
  503. out:
  504. unlock_adl();
  505. return ret;
  506. }
  507. static inline int __gpu_memclock(struct gpu_adl *ga)
  508. {
  509. return ga->lpActivity.iMemoryClock / 100;
  510. }
  511. int gpu_memclock(int gpu)
  512. {
  513. struct gpu_adl *ga;
  514. int ret = -1;
  515. if (!gpus[gpu].has_adl || !adl_active)
  516. return ret;
  517. ga = &gpus[gpu].adl;
  518. lock_adl();
  519. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  520. goto out;
  521. ret = __gpu_memclock(ga);
  522. out:
  523. unlock_adl();
  524. return ret;
  525. }
  526. static inline float __gpu_vddc(struct gpu_adl *ga)
  527. {
  528. return (float)ga->lpActivity.iVddc / 1000;
  529. }
  530. float gpu_vddc(int gpu)
  531. {
  532. struct gpu_adl *ga;
  533. float ret = -1;
  534. if (!gpus[gpu].has_adl || !adl_active)
  535. return ret;
  536. ga = &gpus[gpu].adl;
  537. lock_adl();
  538. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
  539. goto out;
  540. ret = __gpu_vddc(ga);
  541. out:
  542. unlock_adl();
  543. return ret;
  544. }
  545. static inline int __gpu_activity(struct gpu_adl *ga)
  546. {
  547. if (!ga->lpOdParameters.iActivityReportingSupported)
  548. return -1;
  549. return ga->lpActivity.iActivityPercent;
  550. }
  551. int gpu_activity(int gpu)
  552. {
  553. struct gpu_adl *ga;
  554. int ret = -1;
  555. if (!gpus[gpu].has_adl || !adl_active)
  556. return ret;
  557. ga = &gpus[gpu].adl;
  558. lock_adl();
  559. ret = ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity);
  560. unlock_adl();
  561. if (ret != ADL_OK)
  562. return ret;
  563. if (!ga->lpOdParameters.iActivityReportingSupported)
  564. return ret;
  565. return ga->lpActivity.iActivityPercent;
  566. }
  567. static inline int __gpu_fanspeed(struct gpu_adl *ga)
  568. {
  569. if (!ga->has_fanspeed && ga->twin)
  570. return __gpu_fanspeed(ga->twin);
  571. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_READ))
  572. return -1;
  573. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  574. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  575. return -1;
  576. return ga->lpFanSpeedValue.iFanSpeed;
  577. }
  578. int gpu_fanspeed(int gpu)
  579. {
  580. struct gpu_adl *ga;
  581. int ret = -1;
  582. if (!gpus[gpu].has_adl || !adl_active)
  583. return ret;
  584. ga = &gpus[gpu].adl;
  585. lock_adl();
  586. ret = __gpu_fanspeed(ga);
  587. unlock_adl();
  588. return ret;
  589. }
  590. static int __gpu_fanpercent(struct gpu_adl *ga)
  591. {
  592. if (!ga->has_fanspeed && ga->twin)
  593. return __gpu_fanpercent(ga->twin);
  594. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ ))
  595. return -1;
  596. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  597. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
  598. return -1;
  599. return ga->lpFanSpeedValue.iFanSpeed;
  600. }
  601. int gpu_fanpercent(int gpu)
  602. {
  603. struct gpu_adl *ga;
  604. int ret = -1;
  605. if (!gpus[gpu].has_adl || !adl_active)
  606. return ret;
  607. ga = &gpus[gpu].adl;
  608. lock_adl();
  609. ret = __gpu_fanpercent(ga);
  610. unlock_adl();
  611. return ret;
  612. }
  613. static inline int __gpu_powertune(struct gpu_adl *ga)
  614. {
  615. int dummy = 0;
  616. if (ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy) != ADL_OK)
  617. return -1;
  618. return ga->iPercentage;
  619. }
  620. int gpu_powertune(int gpu)
  621. {
  622. struct gpu_adl *ga;
  623. int ret = -1;
  624. if (!gpus[gpu].has_adl || !adl_active)
  625. return ret;
  626. ga = &gpus[gpu].adl;
  627. lock_adl();
  628. ret = __gpu_powertune(ga);
  629. unlock_adl();
  630. return ret;
  631. }
  632. bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc,
  633. int *activity, int *fanspeed, int *fanpercent, int *powertune)
  634. {
  635. struct gpu_adl *ga;
  636. if (!gpus[gpu].has_adl || !adl_active)
  637. return false;
  638. ga = &gpus[gpu].adl;
  639. lock_adl();
  640. gpus[gpu].temp =
  641. *temp = __gpu_temp(ga);
  642. if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK) {
  643. *engineclock = 0;
  644. *memclock = 0;
  645. *vddc = 0;
  646. *activity = 0;
  647. } else {
  648. *engineclock = __gpu_engineclock(ga);
  649. *memclock = __gpu_memclock(ga);
  650. *vddc = __gpu_vddc(ga);
  651. *activity = __gpu_activity(ga);
  652. }
  653. *fanspeed = __gpu_fanspeed(ga);
  654. *fanpercent = __gpu_fanpercent(ga);
  655. *powertune = __gpu_powertune(ga);
  656. unlock_adl();
  657. return true;
  658. }
  659. #ifdef HAVE_CURSES
  660. static void get_enginerange(int gpu, int *imin, int *imax)
  661. {
  662. struct gpu_adl *ga;
  663. if (!gpus[gpu].has_adl || !adl_active) {
  664. wlogprint("Get enginerange not supported\n");
  665. return;
  666. }
  667. ga = &gpus[gpu].adl;
  668. *imin = ga->lpOdParameters.sEngineClock.iMin / 100;
  669. *imax = ga->lpOdParameters.sEngineClock.iMax / 100;
  670. }
  671. #endif
  672. int set_engineclock(int gpu, int iEngineClock)
  673. {
  674. ADLODPerformanceLevels *lpOdPerformanceLevels;
  675. struct cgpu_info *cgpu;
  676. int i, lev, ret = 1;
  677. struct gpu_adl *ga;
  678. if (!gpus[gpu].has_adl || !adl_active) {
  679. wlogprint("Set engineclock not supported\n");
  680. return ret;
  681. }
  682. iEngineClock *= 100;
  683. ga = &gpus[gpu].adl;
  684. /* Keep track of intended engine clock in case the device changes
  685. * profile and drops while idle, not taking the new engine clock */
  686. ga->lastengine = iEngineClock;
  687. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  688. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  689. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  690. lock_adl();
  691. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  692. goto out;
  693. for (i = 0; i < lev; i++) {
  694. if (lpOdPerformanceLevels->aLevels[i].iEngineClock > iEngineClock)
  695. lpOdPerformanceLevels->aLevels[i].iEngineClock = iEngineClock;
  696. }
  697. lpOdPerformanceLevels->aLevels[lev].iEngineClock = iEngineClock;
  698. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  699. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  700. if (lpOdPerformanceLevels->aLevels[lev].iEngineClock == iEngineClock)
  701. ret = 0;
  702. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  703. if (ga->iEngineClock > ga->maxspeed)
  704. ga->maxspeed = ga->iEngineClock;
  705. if (ga->iEngineClock < ga->minspeed)
  706. ga->minspeed = ga->iEngineClock;
  707. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  708. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  709. ga->managed = true;
  710. out:
  711. unlock_adl();
  712. cgpu = &gpus[gpu];
  713. if (cgpu->gpu_memdiff)
  714. set_memoryclock(gpu, iEngineClock / 100 + cgpu->gpu_memdiff);
  715. return ret;
  716. }
  717. #ifdef HAVE_CURSES
  718. static void get_memoryrange(int gpu, int *imin, int *imax)
  719. {
  720. struct gpu_adl *ga;
  721. if (!gpus[gpu].has_adl || !adl_active) {
  722. wlogprint("Get memoryrange not supported\n");
  723. return;
  724. }
  725. ga = &gpus[gpu].adl;
  726. *imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
  727. *imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
  728. }
  729. #endif
  730. int set_memoryclock(int gpu, int iMemoryClock)
  731. {
  732. ADLODPerformanceLevels *lpOdPerformanceLevels;
  733. int i, lev, ret = 1;
  734. struct gpu_adl *ga;
  735. if (!gpus[gpu].has_adl || !adl_active) {
  736. wlogprint("Set memoryclock not supported\n");
  737. return ret;
  738. }
  739. gpus[gpu].gpu_memclock = iMemoryClock;
  740. iMemoryClock *= 100;
  741. ga = &gpus[gpu].adl;
  742. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  743. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  744. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  745. lock_adl();
  746. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  747. goto out;
  748. lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
  749. for (i = 0; i < lev; i++) {
  750. if (lpOdPerformanceLevels->aLevels[i].iMemoryClock > iMemoryClock)
  751. lpOdPerformanceLevels->aLevels[i].iMemoryClock = iMemoryClock;
  752. }
  753. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  754. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  755. if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock == iMemoryClock)
  756. ret = 0;
  757. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  758. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  759. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  760. ga->managed = true;
  761. out:
  762. unlock_adl();
  763. return ret;
  764. }
  765. #ifdef HAVE_CURSES
  766. static void get_vddcrange(int gpu, float *imin, float *imax)
  767. {
  768. struct gpu_adl *ga;
  769. if (!gpus[gpu].has_adl || !adl_active) {
  770. wlogprint("Get vddcrange not supported\n");
  771. return;
  772. }
  773. ga = &gpus[gpu].adl;
  774. *imin = (float)ga->lpOdParameters.sVddc.iMin / 1000;
  775. *imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
  776. }
  777. static float curses_float(const char *query)
  778. {
  779. float ret;
  780. char *cvar;
  781. cvar = curses_input(query);
  782. ret = atof(cvar);
  783. free(cvar);
  784. return ret;
  785. }
  786. #endif
  787. int set_vddc(int gpu, float fVddc)
  788. {
  789. ADLODPerformanceLevels *lpOdPerformanceLevels;
  790. int i, iVddc, lev, ret = 1;
  791. struct gpu_adl *ga;
  792. if (!gpus[gpu].has_adl || !adl_active) {
  793. wlogprint("Set vddc not supported\n");
  794. return ret;
  795. }
  796. iVddc = 1000 * fVddc;
  797. ga = &gpus[gpu].adl;
  798. lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
  799. lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
  800. lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
  801. lock_adl();
  802. if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
  803. goto out;
  804. for (i = 0; i < lev; i++) {
  805. if (lpOdPerformanceLevels->aLevels[i].iVddc > iVddc)
  806. lpOdPerformanceLevels->aLevels[i].iVddc = iVddc;
  807. }
  808. lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
  809. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
  810. ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
  811. if (lpOdPerformanceLevels->aLevels[lev].iVddc == iVddc)
  812. ret = 0;
  813. ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
  814. ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
  815. ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
  816. ga->managed = true;
  817. out:
  818. unlock_adl();
  819. return ret;
  820. }
  821. static void get_fanrange(int gpu, int *imin, int *imax)
  822. {
  823. struct gpu_adl *ga;
  824. if (!gpus[gpu].has_adl || !adl_active) {
  825. wlogprint("Get fanrange not supported\n");
  826. return;
  827. }
  828. ga = &gpus[gpu].adl;
  829. *imin = ga->lpFanSpeedInfo.iMinPercent;
  830. *imax = ga->lpFanSpeedInfo.iMaxPercent;
  831. }
  832. int set_fanspeed(int gpu, int iFanSpeed)
  833. {
  834. struct gpu_adl *ga;
  835. int ret = 1;
  836. if (!gpus[gpu].has_adl || !adl_active) {
  837. wlogprint("Set fanspeed not supported\n");
  838. return ret;
  839. }
  840. ga = &gpus[gpu].adl;
  841. if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE ))) {
  842. applog(LOG_DEBUG, "GPU %d doesn't support rpm or percent write", gpu);
  843. return ret;
  844. }
  845. /* Store what fanspeed we're actually aiming for for re-entrant changes
  846. * in case this device does not support fine setting changes */
  847. ga->targetfan = iFanSpeed;
  848. lock_adl();
  849. if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK) {
  850. applog(LOG_DEBUG, "GPU %d call to fanspeed get failed", gpu);
  851. }
  852. if (!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE)) {
  853. /* Must convert speed to an RPM */
  854. iFanSpeed = ga->lpFanSpeedInfo.iMaxRPM * iFanSpeed / 100;
  855. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
  856. } else
  857. ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
  858. if (!(ga->lpFanSpeedValue.iFlags & ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED)) {
  859. /* If user defined is not already specified, set it first */
  860. ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
  861. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  862. }
  863. ga->lpFanSpeedValue.iFanSpeed = iFanSpeed;
  864. ret = ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue);
  865. ga->managed = true;
  866. unlock_adl();
  867. return ret;
  868. }
  869. #ifdef HAVE_CURSES
  870. static int set_powertune(int gpu, int iPercentage)
  871. {
  872. struct gpu_adl *ga;
  873. int dummy, ret = 1;
  874. if (!gpus[gpu].has_adl || !adl_active) {
  875. wlogprint("Set powertune not supported\n");
  876. return ret;
  877. }
  878. ga = &gpus[gpu].adl;
  879. lock_adl();
  880. ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, iPercentage);
  881. ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
  882. if (ga->iPercentage == iPercentage)
  883. ret = 0;
  884. ga->managed = true;
  885. unlock_adl();
  886. return ret;
  887. }
  888. #endif
  889. /* Returns whether the fanspeed is optimal already or not. The fan_window bool
  890. * tells us whether the current fanspeed is in the target range for fanspeeds.
  891. */
  892. static bool fan_autotune(int gpu, int temp, int fanpercent, int lasttemp, bool *fan_window)
  893. {
  894. struct cgpu_info *cgpu = &gpus[gpu];
  895. int tdiff = round(temp - lasttemp);
  896. struct gpu_adl *ga = &cgpu->adl;
  897. int top = gpus[gpu].gpu_fan;
  898. int bot = gpus[gpu].min_fan;
  899. int newpercent = fanpercent;
  900. int iMin = 0, iMax = 100;
  901. get_fanrange(gpu, &iMin, &iMax);
  902. if (temp > ga->overtemp && fanpercent < iMax) {
  903. applog(LOG_WARNING, "Overheat detected on GPU %d, increasing fan to 100%%", gpu);
  904. newpercent = iMax;
  905. dev_error(cgpu, REASON_DEV_OVER_HEAT);
  906. } else if (temp > gpus[gpu].targettemp && fanpercent < top && tdiff >= 0) {
  907. applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
  908. if (temp > gpus[gpu].targettemp + opt_hysteresis)
  909. newpercent = ga->targetfan + 10;
  910. else
  911. newpercent = ga->targetfan + 5;
  912. if (newpercent > top)
  913. newpercent = top;
  914. } else if (fanpercent > bot && temp < gpus[gpu].targettemp - opt_hysteresis) {
  915. /* Detect large swings of 5 degrees or more and change fan by
  916. * a proportion more */
  917. if (tdiff <= 0) {
  918. applog(LOG_DEBUG, "Temperature %d degrees below target, decreasing fanspeed", opt_hysteresis);
  919. newpercent = ga->targetfan - 1 + tdiff / 5;
  920. } else if (tdiff >= 5) {
  921. applog(LOG_DEBUG, "Temperature climbed %d while below target, increasing fanspeed", tdiff);
  922. newpercent = ga->targetfan + tdiff / 5;
  923. }
  924. } else {
  925. /* We're in the optimal range, make minor adjustments if the
  926. * temp is still drifting */
  927. if (fanpercent > bot && tdiff < 0 && lasttemp < gpus[gpu].targettemp) {
  928. applog(LOG_DEBUG, "Temperature dropping while in target range, decreasing fanspeed");
  929. newpercent = ga->targetfan + tdiff;
  930. } else if (fanpercent < top && tdiff > 0 && temp > gpus[gpu].targettemp - opt_hysteresis) {
  931. applog(LOG_DEBUG, "Temperature rising while in target range, increasing fanspeed");
  932. newpercent = ga->targetfan + tdiff;
  933. }
  934. }
  935. if (newpercent > iMax)
  936. newpercent = iMax;
  937. else if (newpercent < iMin)
  938. newpercent = iMin;
  939. if (newpercent <= top)
  940. *fan_window = true;
  941. else
  942. *fan_window = false;
  943. if (newpercent != fanpercent) {
  944. applog(LOG_INFO, "Setting GPU %d fan percentage to %d", gpu, newpercent);
  945. set_fanspeed(gpu, newpercent);
  946. /* If the fanspeed is going down and we're below the top speed,
  947. * consider the fan optimal to prevent minute changes in
  948. * fanspeed delaying GPU engine speed changes */
  949. if (newpercent < fanpercent && *fan_window)
  950. return true;
  951. return false;
  952. }
  953. return true;
  954. }
  955. void gpu_autotune(int gpu, enum dev_enable *denable)
  956. {
  957. int temp, fanpercent, engine, newengine, twintemp = 0;
  958. bool fan_optimal = true, fan_window = true;
  959. struct cgpu_info *cgpu;
  960. struct gpu_adl *ga;
  961. cgpu = &gpus[gpu];
  962. ga = &cgpu->adl;
  963. lock_adl();
  964. ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity);
  965. gpus[gpu].temp =
  966. temp = __gpu_temp(ga);
  967. if (ga->twin)
  968. twintemp = __gpu_temp(ga->twin);
  969. fanpercent = __gpu_fanpercent(ga);
  970. unlock_adl();
  971. newengine = engine = gpu_engineclock(gpu) * 100;
  972. if (temp && fanpercent >= 0 && ga->autofan) {
  973. if (!ga->twin)
  974. fan_optimal = fan_autotune(gpu, temp, fanpercent, ga->lasttemp, &fan_window);
  975. else if (ga->autofan && (ga->has_fanspeed || !ga->twin->autofan)) {
  976. /* On linked GPUs, we autotune the fan only once, based
  977. * on the highest temperature from either GPUs */
  978. int hightemp, fan_gpu;
  979. int lasttemp;
  980. if (twintemp > temp) {
  981. lasttemp = ga->twin->lasttemp;
  982. hightemp = twintemp;
  983. } else {
  984. lasttemp = ga->lasttemp;
  985. hightemp = temp;
  986. }
  987. if (ga->has_fanspeed)
  988. fan_gpu = gpu;
  989. else
  990. fan_gpu = ga->twin->gpu;
  991. fan_optimal = fan_autotune(fan_gpu, hightemp, fanpercent, lasttemp, &fan_window);
  992. }
  993. }
  994. if (engine && ga->autoengine) {
  995. if (temp > cgpu->cutofftemp) {
  996. // Shutoff and recovery happens back in watchdog_thread
  997. newengine = ga->minspeed;
  998. } else if (temp > ga->overtemp && engine > ga->minspeed) {
  999. applog(LOG_WARNING, "Overheat detected, decreasing GPU %d clock speed", gpu);
  1000. newengine = ga->minspeed;
  1001. dev_error(cgpu, REASON_DEV_OVER_HEAT);
  1002. } else if (temp > gpus[gpu].targettemp + opt_hysteresis && engine > ga->minspeed && fan_optimal) {
  1003. applog(LOG_DEBUG, "Temperature %d degrees over target, decreasing clock speed", opt_hysteresis);
  1004. newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
  1005. /* Only try to tune engine speed up if this GPU is not disabled */
  1006. } else if (temp < gpus[gpu].targettemp && engine < ga->maxspeed && fan_window && *denable == DEV_ENABLED) {
  1007. int iStep = ga->lpOdParameters.sEngineClock.iStep;
  1008. applog(LOG_DEBUG, "Temperature below target, increasing clock speed");
  1009. if (temp < gpus[gpu].targettemp - opt_hysteresis)
  1010. iStep *= 2;
  1011. newengine = engine + iStep;
  1012. }
  1013. if (newengine > ga->maxspeed)
  1014. newengine = ga->maxspeed;
  1015. else if (newengine < ga->minspeed)
  1016. newengine = ga->minspeed;
  1017. /* Adjust engine clock speed if it's lower, or if it's higher
  1018. * but higher than the last intended value as well as the
  1019. * current speed, to avoid setting the engine clock speed to
  1020. * a speed relateive to a lower profile during idle periods. */
  1021. if (newengine < engine || (newengine > engine && newengine > ga->lastengine)) {
  1022. newengine /= 100;
  1023. applog(LOG_INFO, "Setting GPU %d engine clock to %d", gpu, newengine);
  1024. set_engineclock(gpu, newengine);
  1025. }
  1026. }
  1027. ga->lasttemp = temp;
  1028. }
  1029. void set_defaultfan(int gpu)
  1030. {
  1031. struct gpu_adl *ga;
  1032. if (!gpus[gpu].has_adl || !adl_active)
  1033. return;
  1034. ga = &gpus[gpu].adl;
  1035. lock_adl();
  1036. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  1037. unlock_adl();
  1038. }
  1039. void set_defaultengine(int gpu)
  1040. {
  1041. struct gpu_adl *ga;
  1042. if (!gpus[gpu].has_adl || !adl_active)
  1043. return;
  1044. ga = &gpus[gpu].adl;
  1045. lock_adl();
  1046. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  1047. unlock_adl();
  1048. }
  1049. #ifdef HAVE_CURSES
  1050. void change_autosettings(int gpu)
  1051. {
  1052. struct gpu_adl *ga = &gpus[gpu].adl;
  1053. char input;
  1054. int val;
  1055. wlogprint("Target temperature: %d\n", gpus[gpu].targettemp);
  1056. wlogprint("Overheat temperature: %d\n", ga->overtemp);
  1057. wlogprint("Cutoff temperature: %d\n", gpus[gpu].cutofftemp);
  1058. wlogprint("Toggle [F]an auto [G]PU auto\nChange [T]arget [O]verheat [C]utoff\n");
  1059. wlogprint("Or press any other key to continue\n");
  1060. input = getch();
  1061. if (!strncasecmp(&input, "f", 1)) {
  1062. ga->autofan ^= true;
  1063. wlogprint("Fan autotune is now %s\n", ga->autofan ? "enabled" : "disabled");
  1064. if (!ga->autofan) {
  1065. wlogprint("Resetting fan to startup settings\n");
  1066. set_defaultfan(gpu);
  1067. }
  1068. } else if (!strncasecmp(&input, "g", 1)) {
  1069. ga->autoengine ^= true;
  1070. wlogprint("GPU engine clock autotune is now %s\n", ga->autoengine ? "enabled" : "disabled");
  1071. if (!ga->autoengine) {
  1072. wlogprint("Resetting GPU engine clock to startup settings\n");
  1073. set_defaultengine(gpu);
  1074. }
  1075. } else if (!strncasecmp(&input, "t", 1)) {
  1076. val = curses_int("Enter target temperature for this GPU in C (0-200)");
  1077. if (val < 0 || val > 200)
  1078. wlogprint("Invalid temperature");
  1079. else
  1080. gpus[gpu].targettemp = val;
  1081. } else if (!strncasecmp(&input, "o", 1)) {
  1082. wlogprint("Enter overheat temperature for this GPU in C (%d+)", gpus[gpu].targettemp);
  1083. val = curses_int("");
  1084. if (val <= gpus[gpu].targettemp || val > 200)
  1085. wlogprint("Invalid temperature");
  1086. else
  1087. ga->overtemp = val;
  1088. } else if (!strncasecmp(&input, "c", 1)) {
  1089. wlogprint("Enter cutoff temperature for this GPU in C (%d+)", ga->overtemp);
  1090. val = curses_int("");
  1091. if (val <= ga->overtemp || val > 200)
  1092. wlogprint("Invalid temperature");
  1093. else
  1094. gpus[gpu].cutofftemp = val;
  1095. }
  1096. }
  1097. void change_gpusettings(int gpu)
  1098. {
  1099. struct gpu_adl *ga = &gpus[gpu].adl;
  1100. float fval, fmin = 0, fmax = 0;
  1101. int val, imin = 0, imax = 0;
  1102. char input;
  1103. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  1104. float temp = 0, vddc = 0;
  1105. updated:
  1106. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune))
  1107. wlogprint("Temp: %.1f C\n", temp);
  1108. if (fanpercent >= 0 || fanspeed >= 0) {
  1109. wlogprint("Fan Speed: ");
  1110. if (fanpercent >= 0)
  1111. wlogprint("%d%% ", fanpercent);
  1112. if (fanspeed >= 0)
  1113. wlogprint("(%d RPM)", fanspeed);
  1114. wlogprint("\n");
  1115. }
  1116. wlogprint("Engine Clock: %d MHz\nMemory Clock: %d MHz\nVddc: %.3f V\nActivity: %d%%\nPowertune: %d%%\n",
  1117. engineclock, memclock, vddc, activity, powertune);
  1118. wlogprint("Fan autotune is %s (%d-%d)\n", ga->autofan ? "enabled" : "disabled",
  1119. gpus[gpu].min_fan, gpus[gpu].gpu_fan);
  1120. wlogprint("GPU engine clock autotune is %s (%d-%d)\n", ga->autoengine ? "enabled" : "disabled",
  1121. ga->minspeed / 100, ga->maxspeed / 100);
  1122. wlogprint("Change [A]utomatic [E]ngine [F]an [M]emory [V]oltage [P]owertune\n");
  1123. wlogprint("Or press any other key to continue\n");
  1124. input = getch();
  1125. if (!strncasecmp(&input, "a", 1)) {
  1126. change_autosettings(gpu);
  1127. } else if (!strncasecmp(&input, "e", 1)) {
  1128. get_enginerange(gpu, &imin, &imax);
  1129. wlogprint("Enter GPU engine clock speed (%d - %d MHz)", imin, imax);
  1130. val = curses_int("");
  1131. if (val < imin || val > imax) {
  1132. wlogprint("Value is outside safe range, are you sure?\n");
  1133. input = getch();
  1134. if (strncasecmp(&input, "y", 1))
  1135. return;
  1136. }
  1137. if (!set_engineclock(gpu, val))
  1138. wlogprint("Driver reports success but check values below\n");
  1139. else
  1140. wlogprint("Failed to modify engine clock speed\n");
  1141. } else if (!strncasecmp(&input, "f", 1)) {
  1142. get_fanrange(gpu, &imin, &imax);
  1143. wlogprint("Enter fan percentage (%d - %d %)", imin, imax);
  1144. val = curses_int("");
  1145. if (val < imin || val > imax) {
  1146. wlogprint("Value is outside safe range, are you sure?\n");
  1147. input = getch();
  1148. if (strncasecmp(&input, "y", 1))
  1149. return;
  1150. }
  1151. if (!set_fanspeed(gpu, val))
  1152. wlogprint("Driver reports success but check values below\n");
  1153. else
  1154. wlogprint("Failed to modify fan speed\n");
  1155. } else if (!strncasecmp(&input, "m", 1)) {
  1156. get_memoryrange(gpu, &imin, &imax);
  1157. wlogprint("Enter GPU memory clock speed (%d - %d MHz)", imin, imax);
  1158. val = curses_int("");
  1159. if (val < imin || val > imax) {
  1160. wlogprint("Value is outside safe range, are you sure?\n");
  1161. input = getch();
  1162. if (strncasecmp(&input, "y", 1))
  1163. return;
  1164. }
  1165. if (!set_memoryclock(gpu, val))
  1166. wlogprint("Driver reports success but check values below\n");
  1167. else
  1168. wlogprint("Failed to modify memory clock speed\n");
  1169. } else if (!strncasecmp(&input, "v", 1)) {
  1170. get_vddcrange(gpu, &fmin, &fmax);
  1171. wlogprint("Enter GPU voltage (%.3f - %.3f V)", fmin, fmax);
  1172. fval = curses_float("");
  1173. if (fval < fmin || fval > fmax) {
  1174. wlogprint("Value is outside safe range, are you sure?\n");
  1175. input = getch();
  1176. if (strncasecmp(&input, "y", 1))
  1177. return;
  1178. }
  1179. if (!set_vddc(gpu, fval))
  1180. wlogprint("Driver reports success but check values below\n");
  1181. else
  1182. wlogprint("Failed to modify voltage\n");
  1183. } else if (!strncasecmp(&input, "p", 1)) {
  1184. val = curses_int("Enter powertune value (-20 - 20)");
  1185. if (val < -20 || val > 20) {
  1186. wlogprint("Value is outside safe range, are you sure?\n");
  1187. input = getch();
  1188. if (strncasecmp(&input, "y", 1))
  1189. return;
  1190. }
  1191. if (!set_powertune(gpu, val))
  1192. wlogprint("Driver reports success but check values below\n");
  1193. else
  1194. wlogprint("Failed to modify powertune value\n");
  1195. } else {
  1196. clear_logwin();
  1197. return;
  1198. }
  1199. nmsleep(1000);
  1200. goto updated;
  1201. }
  1202. #endif
  1203. static void free_adl(void)
  1204. {
  1205. ADL_Main_Memory_Free ((void **)&lpInfo);
  1206. ADL_Main_Control_Destroy ();
  1207. #ifndef WIN32
  1208. dlclose(hDLL);
  1209. #else
  1210. FreeLibrary(hDLL);
  1211. #endif
  1212. }
  1213. void clear_adl(int nDevs)
  1214. {
  1215. struct gpu_adl *ga;
  1216. int i;
  1217. if (!adl_active)
  1218. return;
  1219. lock_adl();
  1220. /* Try to reset values to their defaults */
  1221. for (i = 0; i < nDevs; i++) {
  1222. ga = &gpus[i].adl;
  1223. /* Only reset the values if we've changed them at any time */
  1224. if (!gpus[i].has_adl || !ga->managed)
  1225. continue;
  1226. ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
  1227. free(ga->DefPerfLev);
  1228. ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
  1229. ADL_Overdrive5_FanSpeedToDefault_Set(ga->iAdapterIndex, 0);
  1230. }
  1231. adl_active = false;
  1232. unlock_adl();
  1233. free_adl();
  1234. }
  1235. #endif /* HAVE_ADL */