adl.c 44 KB

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