1
0

gpuclockctl.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. char arg[128] = { 0 };
  54. char buf[EXTERNAL_BUFFER_MAX] = { 0 };
  55. char *end;
  56. /* Set the GPUGraphicsClockOffset parameter */
  57. snprintf(arg,
  58. 128,
  59. NV_ATTRIBUTE_FORMAT,
  60. info->device,
  61. NV_CORE_OFFSET_ATTRIBUTE,
  62. info->nv_perf_level);
  63. const char *exec_args_core[] = { "/usr/bin/nvidia-settings", "-q", arg, "-t", NULL };
  64. if (run_external_process_get_output(exec_args_core, buf) != 0) {
  65. LOG_ERROR("Failed to set %s!\n", arg);
  66. return -1;
  67. }
  68. info->core = strtol(buf, &end, 10);
  69. if (end == buf) {
  70. LOG_ERROR("Failed to parse output for \"%s\" output was \"%s\"!\n", arg, buf);
  71. return -1;
  72. }
  73. /* Set the GPUMemoryTransferRateOffset parameter */
  74. snprintf(arg,
  75. 128,
  76. NV_ATTRIBUTE_FORMAT,
  77. info->device,
  78. NV_MEM_OFFSET_ATTRIBUTE,
  79. info->nv_perf_level);
  80. const char *exec_args_mem[] = { "/usr/bin/nvidia-settings", "-q", arg, "-t", NULL };
  81. if (run_external_process_get_output(exec_args_mem, buf) != 0) {
  82. LOG_ERROR("Failed to set %s!\n", arg);
  83. return -1;
  84. }
  85. info->mem = strtol(buf, &end, 10);
  86. if (end == buf) {
  87. LOG_ERROR("Failed to parse output for \"%s\" output was \"%s\"!\n", arg, buf);
  88. return -1;
  89. }
  90. return 0;
  91. }
  92. /**
  93. * Get the gpu state
  94. * Populates the struct with the GPU info on the system
  95. */
  96. static int get_gpu_state_amd(struct GameModeGPUInfo *info)
  97. {
  98. fprintf(stderr, "Fetching GPU state on AMD is currently unimplemented!\n");
  99. return info != NULL;
  100. }
  101. /**
  102. * Set the gpu state based on input parameters on Nvidia
  103. */
  104. static int set_gpu_state_nv(struct GameModeGPUInfo *info)
  105. {
  106. if (info->vendor != Vendor_NVIDIA)
  107. return -1;
  108. // These commands don't technically even need root
  109. /* Set the GPUGraphicsClockOffset parameter */
  110. char core_arg[128];
  111. snprintf(core_arg,
  112. 128,
  113. NV_ATTRIBUTE_FORMAT "=%ld",
  114. info->device,
  115. NV_CORE_OFFSET_ATTRIBUTE,
  116. info->nv_perf_level,
  117. info->core);
  118. const char *exec_args_core[] = { "/usr/bin/nvidia-settings", "-a", core_arg, NULL };
  119. if (run_external_process(exec_args_core) != 0) {
  120. LOG_ERROR("Failed to set %s!\n", core_arg);
  121. return -1;
  122. }
  123. /* Set the GPUMemoryTransferRateOffset parameter */
  124. char mem_arg[128];
  125. snprintf(mem_arg,
  126. 128,
  127. NV_ATTRIBUTE_FORMAT "=%ld",
  128. info->device,
  129. NV_MEM_OFFSET_ATTRIBUTE,
  130. info->nv_perf_level,
  131. info->mem);
  132. const char *exec_args_mem[] = { "/usr/bin/nvidia-settings", "-a", mem_arg, NULL };
  133. if (run_external_process(exec_args_mem) != 0) {
  134. LOG_ERROR("Failed to set %s!\n", mem_arg);
  135. return -1;
  136. }
  137. return 0;
  138. }
  139. /*
  140. * Sets the value in a file in the AMDGPU driver config
  141. * Files are:
  142. * /sys/class/drm/card0/device/pp_sclk_od
  143. * /sys/class/drm/card0/device/pp_mclk_od
  144. */
  145. static int set_gpu_state_amd_file(const char *filename, long device, long value)
  146. {
  147. char path[64];
  148. snprintf(path, 64, AMD_DRM_PATH, device, filename);
  149. FILE *file = fopen(path, "w");
  150. if (!file) {
  151. LOG_ERROR("Could not open %s for write (%s)!\n", path, strerror(errno));
  152. return -1;
  153. }
  154. if (fprintf(file, "%ld", value) < 0) {
  155. LOG_ERROR("Could not write to %s (%s)!\n", path, strerror(errno));
  156. return -1;
  157. }
  158. if (fclose(file) != 0) {
  159. LOG_ERROR("Could not close %s after writing (%s)!\n", path, strerror(errno));
  160. return -1;
  161. }
  162. return 0;
  163. }
  164. /**
  165. * Set the gpu state based on input parameters on amd
  166. */
  167. static int set_gpu_state_amd(struct GameModeGPUInfo *info)
  168. {
  169. if (info->vendor != Vendor_AMD)
  170. return -1;
  171. // Set the the core and mem clock speeds using the OverDrive files
  172. if (set_gpu_state_amd_file("pp_sclk_od", info->device, info->core) != 0)
  173. return -1;
  174. if (set_gpu_state_amd_file("pp_mclk_od", info->device, info->mem) != 0)
  175. return -1;
  176. return 0;
  177. }
  178. /* Helper to get and verify vendor value */
  179. static long get_vendor(const char *val)
  180. {
  181. char *end;
  182. long ret = strtol(val, &end, 0);
  183. if (!GPUVendorValid(ret) || end == val) {
  184. LOG_ERROR("Invalid GPU Vendor passed (0x%04x)!\n", (unsigned short)ret);
  185. print_usage_and_exit();
  186. }
  187. return ret;
  188. }
  189. /* Helper to get and verify device value */
  190. static long get_device(const char *val)
  191. {
  192. char *end;
  193. long ret = strtol(val, &end, 10);
  194. if (ret < 0 || end == val) {
  195. LOG_ERROR("Invalid GPU device passed (%ld)!\n", ret);
  196. print_usage_and_exit();
  197. }
  198. return ret;
  199. }
  200. /* Helper to get and verify core and mem value */
  201. static long get_generic_value(const char *val)
  202. {
  203. char *end;
  204. long ret = strtol(val, &end, 10);
  205. if (ret < 0 || end == val) {
  206. LOG_ERROR("Invalid value passed (%ld)!\n", ret);
  207. print_usage_and_exit();
  208. }
  209. return ret;
  210. }
  211. /**
  212. * Main entry point, dispatch to the appropriate helper
  213. */
  214. int main(int argc, char *argv[])
  215. {
  216. if (argc >= 4 && strncmp(argv[3], "get", 3) == 0) {
  217. /* Get and verify the vendor and device */
  218. struct GameModeGPUInfo info;
  219. memset(&info, 0, sizeof(info));
  220. info.vendor = get_vendor(argv[1]);
  221. info.device = get_device(argv[2]);
  222. if (info.vendor == Vendor_NVIDIA && argc > 4)
  223. info.nv_perf_level = get_generic_value(argv[4]);
  224. /* Fetch the state and print it out */
  225. switch (info.vendor) {
  226. case Vendor_NVIDIA:
  227. /* Get nvidia power level */
  228. if (get_gpu_state_nv(&info) != 0)
  229. exit(EXIT_FAILURE);
  230. break;
  231. case Vendor_AMD:
  232. if (get_gpu_state_amd(&info) != 0)
  233. exit(EXIT_FAILURE);
  234. break;
  235. default:
  236. printf("Currently unsupported GPU vendor 0x%04x, doing nothing!\n", (short)info.vendor);
  237. break;
  238. }
  239. printf("%ld %ld\n", info.core, info.mem);
  240. } else if (argc >= 6 && argc <= 7 && strncmp(argv[3], "set", 3) == 0) {
  241. /* Must be root to set the state */
  242. if (geteuid() != 0) {
  243. fprintf(stderr, "gpuclockctl must be run as root to set values\n");
  244. print_usage_and_exit();
  245. }
  246. /* Get and verify the vendor and device */
  247. struct GameModeGPUInfo info;
  248. memset(&info, 0, sizeof(info));
  249. info.vendor = get_vendor(argv[1]);
  250. info.device = get_device(argv[2]);
  251. info.core = get_generic_value(argv[4]);
  252. info.mem = get_generic_value(argv[5]);
  253. if (info.vendor == Vendor_NVIDIA && argc > 6)
  254. info.nv_perf_level = get_generic_value(argv[6]);
  255. printf("gpuclockctl setting core:%ld mem:%ld on device:%ld with vendor 0x%04x\n",
  256. info.core,
  257. info.mem,
  258. info.device,
  259. (unsigned short)info.vendor);
  260. if (info.vendor == Vendor_NVIDIA)
  261. printf("on Performance Level %ld\n", info.nv_perf_level);
  262. switch (info.vendor) {
  263. case Vendor_NVIDIA:
  264. return set_gpu_state_nv(&info);
  265. case Vendor_AMD:
  266. return set_gpu_state_amd(&info);
  267. default:
  268. printf("Currently unsupported GPU vendor 0x%04x, doing nothing!\n", (short)info.vendor);
  269. break;
  270. }
  271. } else {
  272. print_usage_and_exit();
  273. }
  274. return EXIT_SUCCESS;
  275. }