adl.c 43 KB

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