1
0

gpuclockctl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. Copyright (c) 2017-2018, Feral Interactive
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Feral Interactive nor the names of its contributors
  12. may be used to endorse or promote products derived from this software
  13. without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #define _GNU_SOURCE
  27. #include "logging.h"
  28. #include "external-helper.h"
  29. #include "gpu-control.h"
  30. /* NV constants */
  31. #define NV_CORE_OFFSET_ATTRIBUTE "GPUGraphicsClockOffset"
  32. #define NV_MEM_OFFSET_ATTRIBUTE "GPUMemoryTransferRateOffset"
  33. #define NV_ATTRIBUTE_FORMAT "[gpu:%ld]/%s[%ld]"
  34. /* AMD constants */
  35. #define AMD_DRM_PATH "/sys/class/drm/card%ld/device/%s"
  36. /* Plausible extras to add:
  37. * Intel support - https://blog.ffwll.ch/2013/03/overclocking-your-intel-gpu-on-linux.html
  38. * AMD - Allow setting fan speed as well
  39. * Store baseline values with get_gpu_state to apply when leaving gamemode
  40. */
  41. /* Helper to quit with usage */
  42. static const char *usage_text =
  43. "usage: gpuclockctl PCI_ID DEVICE [get] [set CORE MEM [PERF_LEVEL]]]";
  44. static void print_usage_and_exit(void)
  45. {
  46. fprintf(stderr, "%s\n", usage_text);
  47. exit(EXIT_FAILURE);
  48. }
  49. static int get_gpu_state_nv(struct GameModeGPUInfo *info)
  50. {
  51. if (info->vendor != Vendor_NVIDIA)
  52. return -1;
  53. if (!getenv("DISPLAY"))
  54. LOG_ERROR("Getting Nvidia parameters requires DISPLAY to be set - will likely fail!\n");
  55. char arg[128] = { 0 };
  56. char buf[EXTERNAL_BUFFER_MAX] = { 0 };
  57. char *end;
  58. /* Set the GPUGraphicsClockOffset parameter */
  59. snprintf(arg,
  60. 128,
  61. NV_ATTRIBUTE_FORMAT,
  62. info->device,
  63. NV_CORE_OFFSET_ATTRIBUTE,
  64. info->nv_perf_level);
  65. const char *exec_args_core[] = { "/usr/bin/nvidia-settings", "-q", arg, "-t", NULL };
  66. if (run_external_process(exec_args_core, buf, -1) != 0) {
  67. LOG_ERROR("Failed to set %s!\n", arg);
  68. return -1;
  69. }
  70. info->nv_core = strtol(buf, &end, 10);
  71. if (end == buf) {
  72. LOG_ERROR("Failed to parse output for \"%s\" output was \"%s\"!\n", arg, buf);
  73. return -1;
  74. }
  75. /* Set the GPUMemoryTransferRateOffset parameter */
  76. snprintf(arg,
  77. 128,
  78. NV_ATTRIBUTE_FORMAT,
  79. info->device,
  80. NV_MEM_OFFSET_ATTRIBUTE,
  81. info->nv_perf_level);
  82. const char *exec_args_mem[] = { "/usr/bin/nvidia-settings", "-q", arg, "-t", NULL };
  83. if (run_external_process(exec_args_mem, buf, -1) != 0) {
  84. LOG_ERROR("Failed to set %s!\n", arg);
  85. return -1;
  86. }
  87. info->nv_mem = strtol(buf, &end, 10);
  88. if (end == buf) {
  89. LOG_ERROR("Failed to parse output for \"%s\" output was \"%s\"!\n", arg, buf);
  90. return -1;
  91. }
  92. return 0;
  93. }
  94. /**
  95. * Set the gpu state based on input parameters on Nvidia
  96. */
  97. static int set_gpu_state_nv(struct GameModeGPUInfo *info)
  98. {
  99. if (info->vendor != Vendor_NVIDIA)
  100. return -1;
  101. if (!getenv("DISPLAY") || !getenv("XAUTHORITY"))
  102. LOG_ERROR(
  103. "Setting Nvidia parameters requires DISPLAY and XAUTHORITY to be set - will likely "
  104. "fail!\n");
  105. /* Set the GPUGraphicsClockOffset parameter */
  106. char core_arg[128];
  107. snprintf(core_arg,
  108. 128,
  109. NV_ATTRIBUTE_FORMAT "=%ld",
  110. info->device,
  111. NV_CORE_OFFSET_ATTRIBUTE,
  112. info->nv_perf_level,
  113. info->nv_core);
  114. const char *exec_args_core[] = { "/usr/bin/nvidia-settings", "-a", core_arg, NULL };
  115. if (run_external_process(exec_args_core, NULL, -1) != 0) {
  116. LOG_ERROR("Failed to set %s!\n", core_arg);
  117. return -1;
  118. }
  119. /* Set the GPUMemoryTransferRateOffset parameter */
  120. char mem_arg[128];
  121. snprintf(mem_arg,
  122. 128,
  123. NV_ATTRIBUTE_FORMAT "=%ld",
  124. info->device,
  125. NV_MEM_OFFSET_ATTRIBUTE,
  126. info->nv_perf_level,
  127. info->nv_mem);
  128. const char *exec_args_mem[] = { "/usr/bin/nvidia-settings", "-a", mem_arg, NULL };
  129. if (run_external_process(exec_args_mem, NULL, -1) != 0) {
  130. LOG_ERROR("Failed to set %s!\n", mem_arg);
  131. return -1;
  132. }
  133. return 0;
  134. }
  135. /**
  136. * Get the gpu state
  137. * Populates the struct with the GPU info on the system
  138. */
  139. static int get_gpu_state_amd(struct GameModeGPUInfo *info)
  140. {
  141. if (info->vendor != Vendor_AMD)
  142. return -1;
  143. /* Get the contents of power_dpm_force_performance_level */
  144. char path[64];
  145. snprintf(path, 64, AMD_DRM_PATH, info->device, "power_dpm_force_performance_level");
  146. FILE *file = fopen(path, "r");
  147. if (!file) {
  148. LOG_ERROR("Could not open %s for read (%s)!\n", path, strerror(errno));
  149. return -1;
  150. }
  151. char buff[CONFIG_VALUE_MAX];
  152. if (!fgets(buff, CONFIG_VALUE_MAX, file)) {
  153. LOG_ERROR("Could not read file %s (%s)!\n", path, strerror(errno));
  154. return -1;
  155. }
  156. if (fclose(file) != 0) {
  157. LOG_ERROR("Could not close %s after reading (%s)!\n", path, strerror(errno));
  158. return -1;
  159. }
  160. /* Copy in the value from the file */
  161. strncpy(info->amd_performance_level, buff, CONFIG_VALUE_MAX);
  162. return info == NULL;
  163. }
  164. /*
  165. * Simply set an amd drm file to a value
  166. */
  167. static int set_gpu_state_amd_file(const char *filename, long device, const char *value)
  168. {
  169. char path[64];
  170. snprintf(path, 64, AMD_DRM_PATH, device, filename);
  171. FILE *file = fopen(path, "w");
  172. if (!file) {
  173. LOG_ERROR("Could not open %s for write (%s)!\n", path, strerror(errno));
  174. return -1;
  175. }
  176. if (fprintf(file, "%s", value) < 0) {
  177. LOG_ERROR("Could not write to %s (%s)!\n", path, strerror(errno));
  178. return -1;
  179. }
  180. if (fclose(file) != 0) {
  181. LOG_ERROR("Could not close %s after writing (%s)!\n", path, strerror(errno));
  182. return -1;
  183. }
  184. return 0;
  185. }
  186. /**
  187. * Set the gpu state based on input parameters on amd
  188. */
  189. static int set_gpu_state_amd(struct GameModeGPUInfo *info)
  190. {
  191. if (info->vendor != Vendor_AMD)
  192. return -1;
  193. /* Must be root to set the state */
  194. if (geteuid() != 0) {
  195. fprintf(stderr, "gpuclockctl must be run as root to set AMD values\n");
  196. print_usage_and_exit();
  197. }
  198. /* First set the amd_performance_level to the chosen setting */
  199. if (set_gpu_state_amd_file("power_dpm_force_performance_level",
  200. info->device,
  201. info->amd_performance_level) != 0)
  202. return -1;
  203. /* TODO: If amd_performance_level is set to "manual" we need to adjust pp_table and/or
  204. pp_od_clk_voltage see
  205. https://dri.freedesktop.org/docs/drm/gpu/amdgpu.html#gpu-power-thermal-controls-and-monitoring
  206. */
  207. return 0;
  208. }
  209. /* Helper to get and verify vendor value */
  210. static long get_vendor(const char *val)
  211. {
  212. char *end;
  213. long ret = strtol(val, &end, 0);
  214. if (!GPUVendorValid(ret) || end == val) {
  215. LOG_ERROR("Invalid GPU Vendor passed (0x%04x)!\n", (unsigned short)ret);
  216. print_usage_and_exit();
  217. }
  218. return ret;
  219. }
  220. /* Helper to get and verify device value */
  221. static long get_device(const char *val)
  222. {
  223. char *end;
  224. long ret = strtol(val, &end, 10);
  225. if (ret < 0 || end == val) {
  226. LOG_ERROR("Invalid GPU device passed (%ld)!\n", ret);
  227. print_usage_and_exit();
  228. }
  229. return ret;
  230. }
  231. /* Helper to get and verify nv_core and nv_mem value */
  232. static long get_generic_value(const char *val)
  233. {
  234. char *end;
  235. long ret = strtol(val, &end, 10);
  236. if (ret < 0 || end == val) {
  237. LOG_ERROR("Invalid value passed (%ld)!\n", ret);
  238. print_usage_and_exit();
  239. }
  240. return ret;
  241. }
  242. /**
  243. * Main entry point, dispatch to the appropriate helper
  244. */
  245. int main(int argc, char *argv[])
  246. {
  247. if (argc >= 4 && strncmp(argv[3], "get", 3) == 0) {
  248. /* Get and verify the vendor and device */
  249. struct GameModeGPUInfo info;
  250. memset(&info, 0, sizeof(info));
  251. info.vendor = get_vendor(argv[1]);
  252. info.device = get_device(argv[2]);
  253. if (info.vendor == Vendor_NVIDIA && argc > 4)
  254. info.nv_perf_level = get_generic_value(argv[4]);
  255. /* Fetch the state and print it out */
  256. switch (info.vendor) {
  257. case Vendor_NVIDIA:
  258. /* Get nvidia power level */
  259. if (get_gpu_state_nv(&info) != 0)
  260. exit(EXIT_FAILURE);
  261. printf("%ld %ld\n", info.nv_core, info.nv_mem);
  262. break;
  263. case Vendor_AMD:
  264. if (get_gpu_state_amd(&info) != 0)
  265. exit(EXIT_FAILURE);
  266. printf("%s\n", info.amd_performance_level);
  267. break;
  268. default:
  269. printf("Currently unsupported GPU vendor 0x%04x, doing nothing!\n", (short)info.vendor);
  270. break;
  271. }
  272. } else if (argc >= 5 && argc <= 7 && strncmp(argv[3], "set", 3) == 0) {
  273. /* Get and verify the vendor and device */
  274. struct GameModeGPUInfo info;
  275. memset(&info, 0, sizeof(info));
  276. info.vendor = get_vendor(argv[1]);
  277. info.device = get_device(argv[2]);
  278. switch (info.vendor) {
  279. case Vendor_NVIDIA:
  280. info.nv_core = get_generic_value(argv[4]);
  281. info.nv_mem = get_generic_value(argv[5]);
  282. if (argc > 6)
  283. info.nv_perf_level = get_generic_value(argv[6]);
  284. break;
  285. case Vendor_AMD:
  286. strncpy(info.amd_performance_level, argv[4], CONFIG_VALUE_MAX);
  287. break;
  288. default:
  289. printf("Currently unsupported GPU vendor 0x%04x, doing nothing!\n", (short)info.vendor);
  290. break;
  291. }
  292. printf("gpuclockctl setting values on device:%ld with vendor 0x%04x",
  293. info.device,
  294. (unsigned short)info.vendor);
  295. switch (info.vendor) {
  296. case Vendor_NVIDIA:
  297. return set_gpu_state_nv(&info);
  298. case Vendor_AMD:
  299. return set_gpu_state_amd(&info);
  300. default:
  301. printf("Currently unsupported GPU vendor 0x%04x, doing nothing!\n", (short)info.vendor);
  302. break;
  303. }
  304. } else {
  305. print_usage_and_exit();
  306. }
  307. return EXIT_SUCCESS;
  308. }