gamemode-gpu.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. Copyright (c) 2017-2025, Feral Interactive and the GameMode contributors
  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 "common-external.h"
  28. #include "common-gpu.h"
  29. #include "common-helpers.h"
  30. #include "common-logging.h"
  31. #include "gamemode.h"
  32. #include "gamemode-config.h"
  33. #include "build-config.h"
  34. _Static_assert(CONFIG_VALUE_MAX == GPU_VALUE_MAX, "Config max value and GPU value out of sync!");
  35. /**
  36. * Attempts to identify the current in use GPU information
  37. */
  38. int game_mode_initialise_gpu(GameModeConfig *config, GameModeGPUInfo **info)
  39. {
  40. /* Verify input, this is programmer error */
  41. if (!info || *info)
  42. FATAL_ERROR("Invalid GameModeGPUInfo passed to %s", __func__);
  43. /* Early out if we have this feature turned off */
  44. char apply[CONFIG_VALUE_MAX];
  45. config_get_apply_gpu_optimisations(config, apply);
  46. if (strlen(apply) == 0) {
  47. return 0;
  48. } else if (strncmp(apply, "accept-responsibility", CONFIG_VALUE_MAX) != 0) {
  49. LOG_ERROR(
  50. "apply_gpu_optimisations set to value other than \"accept-responsibility\" (%s), will "
  51. "not apply GPU optimisations!\n",
  52. apply);
  53. return -1;
  54. }
  55. /* Create the context */
  56. GameModeGPUInfo *new_info = malloc(sizeof(GameModeGPUInfo));
  57. memset(new_info, 0, sizeof(GameModeGPUInfo));
  58. /* Get the config parameters */
  59. new_info->device = config_get_gpu_device(config);
  60. /* verify device ID */
  61. if (new_info->device == -1) {
  62. LOG_ERROR(
  63. "Invalid gpu_device value set in configuration, will not apply "
  64. "optimisations!\n");
  65. free(new_info);
  66. return -1;
  67. }
  68. /* Fill in GPU vendor */
  69. new_info->vendor = gamemode_get_gpu_vendor(new_info->device);
  70. if (!GPUVendorValid(new_info->vendor)) {
  71. LOG_ERROR("Found invalid vendor, will not apply optimisations!\n");
  72. free(new_info);
  73. return -1;
  74. }
  75. /* Load the config based on GPU and also verify the values are sane */
  76. switch (new_info->vendor) {
  77. case Vendor_NVIDIA:
  78. new_info->nv_core = config_get_nv_core_clock_mhz_offset(config);
  79. new_info->nv_mem = config_get_nv_mem_clock_mhz_offset(config);
  80. new_info->nv_powermizer_mode = config_get_nv_powermizer_mode(config);
  81. /* Reject values over some guessed values
  82. * If a user wants to go into very unsafe levels they can recompile
  83. */
  84. const int nv_core_hard_limit = 200;
  85. const int nv_mem_hard_limit = 2000;
  86. if (new_info->nv_core > nv_core_hard_limit || new_info->nv_mem > nv_mem_hard_limit) {
  87. LOG_ERROR(
  88. "NVIDIA Overclock value above safety levels of +%d (core) +%d (mem), will "
  89. "not overclock!\n",
  90. nv_core_hard_limit,
  91. nv_mem_hard_limit);
  92. LOG_ERROR("nv_core_clock_mhz_offset:%ld nv_mem_clock_mhz_offset:%ld\n",
  93. new_info->nv_core,
  94. new_info->nv_mem);
  95. free(new_info);
  96. return -1;
  97. }
  98. break;
  99. case Vendor_AMD:
  100. config_get_amd_performance_level(config, new_info->amd_performance_level);
  101. /* Error about unsupported "manual" option, for now */
  102. if (strcmp(new_info->amd_performance_level, "manual") == 0) {
  103. LOG_ERROR("AMD Performance level set to \"manual\", this is currently unsupported");
  104. free(new_info);
  105. return -1;
  106. }
  107. break;
  108. default:
  109. break;
  110. }
  111. /* Give back the new gpu info */
  112. *info = new_info;
  113. return 0;
  114. }
  115. /* Simply used to free the GPU info object */
  116. void game_mode_free_gpu(GameModeGPUInfo **info)
  117. {
  118. /* Simply free the object */
  119. free(*info);
  120. *info = NULL;
  121. }
  122. /**
  123. * Applies GPU optimisations when gamemode is active and removes them after
  124. */
  125. int game_mode_apply_gpu(const GameModeGPUInfo *info)
  126. {
  127. // Null info means don't apply anything
  128. if (!info)
  129. return 0;
  130. LOG_MSG("Requesting GPU optimisations on device:%ld\n", info->device);
  131. /* Generate the input strings */
  132. char device[4];
  133. snprintf(device, 4, "%ld", info->device);
  134. char nv_core[8];
  135. snprintf(nv_core, 8, "%ld", info->nv_core);
  136. char nv_mem[8];
  137. snprintf(nv_mem, 8, "%ld", info->nv_mem);
  138. char nv_powermizer_mode[4];
  139. snprintf(nv_powermizer_mode, 4, "%ld", info->nv_powermizer_mode);
  140. // Set up our command line to pass to gpuclockctl
  141. const char *const exec_args[] = {
  142. "pkexec",
  143. LIBEXECDIR "/gpuclockctl",
  144. device,
  145. "set",
  146. info->vendor == Vendor_NVIDIA ? nv_core : info->amd_performance_level,
  147. info->vendor == Vendor_NVIDIA ? nv_mem : NULL, /* Only use this if Nvidia */
  148. info->vendor == Vendor_NVIDIA ? nv_powermizer_mode : NULL, /* Only use this if Nvidia */
  149. NULL,
  150. };
  151. if (run_external_process(exec_args, NULL, -1) != 0) {
  152. LOG_ERROR("Failed to call gpuclockctl, could not apply optimisations!\n");
  153. return -1;
  154. }
  155. return 0;
  156. }
  157. int game_mode_get_gpu(GameModeGPUInfo *info)
  158. {
  159. if (!info)
  160. return 0;
  161. /* Generate the input strings */
  162. char device[4];
  163. snprintf(device, 4, "%ld", info->device);
  164. // Set up our command line to pass to gpuclockctl
  165. // This doesn't need pkexec as get does not need elevated perms
  166. const char *const exec_args[] = {
  167. LIBEXECDIR "/gpuclockctl",
  168. device,
  169. "get",
  170. NULL,
  171. };
  172. char buffer[EXTERNAL_BUFFER_MAX] = { 0 };
  173. if (run_external_process(exec_args, buffer, -1) != 0) {
  174. LOG_ERROR("Failed to call gpuclockctl, could not get values!\n");
  175. return -1;
  176. }
  177. strtok(buffer, "\n");
  178. switch (info->vendor) {
  179. case Vendor_NVIDIA:
  180. if (sscanf(buffer,
  181. "%ld %ld %ld",
  182. &info->nv_core,
  183. &info->nv_mem,
  184. &info->nv_powermizer_mode) != 3) {
  185. LOG_ERROR("Failed to parse gpuclockctl output: %s\n", buffer);
  186. return -1;
  187. }
  188. break;
  189. case Vendor_AMD:
  190. strncpy(info->amd_performance_level, buffer, sizeof(info->amd_performance_level) - 1);
  191. info->amd_performance_level[sizeof(info->amd_performance_level) - 1] = '\0';
  192. break;
  193. }
  194. return 0;
  195. }