adl.c 46 KB

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