adl.c 44 KB

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