1
0

gamemode-gpu.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "config.h"
  28. #include "external-helper.h"
  29. #include "gamemode.h"
  30. #include "helpers.h"
  31. #include "logging.h"
  32. #include "daemon_config.h"
  33. #include "gpu-query.h"
  34. // TODO
  35. // Gather GPU type and information automatically
  36. // Apply Nvidia GPU settings (CoolBits will be needed)
  37. // Apply AMD GPU settings (Will need user changing pwm1_enable)
  38. // Intel?
  39. // Provide documentation on optimisations
  40. /**
  41. * Attempts to identify the current in use GPU information
  42. */
  43. int game_mode_initialise_gpu(GameModeConfig *config, GameModeGPUInfo **info)
  44. {
  45. int status = 0;
  46. /* Verify input, this is programmer error */
  47. if (!info || *info)
  48. FATAL_ERROR("Invalid GameModeGPUInfo passed to %s", __func__);
  49. /* Early out if we have this feature turned off */
  50. char apply[CONFIG_VALUE_MAX];
  51. config_get_apply_gpu_optimisations(config, apply);
  52. if (strlen(apply) == 0) {
  53. return 0;
  54. } else if (strncmp(apply, "accept-responsibility", CONFIG_VALUE_MAX) != 0) {
  55. LOG_ERROR(
  56. "apply_gpu_optimisations set to value other than \"accept-responsibility\" (%s), will "
  57. "not apply GPU optimisations!\n",
  58. apply);
  59. return -1;
  60. }
  61. /* Create the context */
  62. GameModeGPUInfo *new_info = malloc(sizeof(GameModeGPUInfo));
  63. memset(new_info, 0, sizeof(GameModeGPUInfo));
  64. /* Get the config parameters */
  65. config_get_gpu_vendor(config, &new_info->vendor);
  66. config_get_gpu_device(config, &new_info->device);
  67. /* TODO: Detect the GPU vendor and device automatically when these aren't set */
  68. /* verify device ID */
  69. if (new_info->device == -1) {
  70. LOG_ERROR(
  71. "ERROR: Invalid gpu_device value set in configuration, will not apply "
  72. "optimisations!\n");
  73. free(new_info);
  74. return -1;
  75. }
  76. /* verify GPU vendor */
  77. if (!GPUVendorValid(new_info->vendor)) {
  78. LOG_ERROR(
  79. "ERROR: Invalid gpu_vendor value (0x%04x) set in configuration, will not apply "
  80. "optimisations!\n",
  81. (unsigned int)new_info->vendor);
  82. LOG_ERROR("Possible values are: 0x%04x (NVIDIA) 0x%04x (AMD) 0x%04x (Intel)\n",
  83. Vendor_NVIDIA,
  84. Vendor_AMD,
  85. Vendor_Intel);
  86. free(new_info);
  87. return -1;
  88. }
  89. /* Load the config based on GPU and also verify the values are sane */
  90. switch (new_info->vendor) {
  91. case Vendor_NVIDIA:
  92. config_get_nv_core_clock_mhz_offset(config, &new_info->core);
  93. config_get_nv_mem_clock_mhz_offset(config, &new_info->mem);
  94. /* Reject values over some guessed values
  95. * If a user wants to go into very unsafe levels they can recompile
  96. */
  97. const int nv_core_hard_limit = 200;
  98. const int nv_mem_hard_limit = 2000;
  99. if (new_info->core > nv_core_hard_limit || new_info->mem > nv_mem_hard_limit) {
  100. LOG_ERROR(
  101. "ERROR NVIDIA Overclock value above safety levels of +%d (core) +%d (mem), will "
  102. "not overclock!\n",
  103. nv_core_hard_limit,
  104. nv_mem_hard_limit);
  105. LOG_ERROR("nv_core_clock_mhz_offset:%ld nv_mem_clock_mhz_offset:%ld\n",
  106. new_info->core,
  107. new_info->mem);
  108. free(new_info);
  109. return -1;
  110. }
  111. break;
  112. case Vendor_AMD:
  113. config_get_amd_core_clock_percentage(config, &new_info->core);
  114. config_get_amd_mem_clock_percentage(config, &new_info->mem);
  115. /* Reject values over 25%
  116. * If a user wants to go into very unsafe levels they can recompile
  117. */
  118. const int amd_hard_limit = 25;
  119. if (new_info->core > amd_hard_limit || new_info->mem > amd_hard_limit) {
  120. LOG_ERROR("ERROR AMD Overclock value above safety level of %d%%, will not overclock!\n",
  121. amd_hard_limit);
  122. LOG_ERROR("amd_core_clock_percentage:%ld amd_mem_clock_percentage:%ld\n",
  123. new_info->core,
  124. new_info->mem);
  125. free(new_info);
  126. return -1;
  127. }
  128. break;
  129. default:
  130. break;
  131. }
  132. /* Give back the new gpu info */
  133. *info = new_info;
  134. return status;
  135. }
  136. /* Simply used to free the GPU info object */
  137. void game_mode_free_gpu(GameModeGPUInfo **info)
  138. {
  139. /* Simply free the object */
  140. free(*info);
  141. *info = NULL;
  142. }
  143. //#include <linux/limits.h>
  144. //#include <stdio.h>
  145. //#include <sys/wait.h>
  146. //#include <unistd.h>
  147. /**
  148. * Applies GPU optimisations when gamemode is active and removes them after
  149. */
  150. int game_mode_apply_gpu(const GameModeGPUInfo *info, bool apply)
  151. {
  152. // Null info means don't apply anything
  153. if (!info)
  154. return 0;
  155. LOG_MSG("Requesting GPU optimisations on device:%ld with settings core:%ld clock:%ld\n",
  156. info->device,
  157. info->core,
  158. info->mem);
  159. // TODO: Actually pass right arguments
  160. const char *const exec_args[] = {
  161. "/usr/bin/pkexec",
  162. LIBEXECDIR "/gpuclockctl",
  163. NULL,
  164. };
  165. if (run_external_process(exec_args) != 0) {
  166. LOG_ERROR("ERROR: Failed to call gpuclockctl, could not apply optimisations!\n");
  167. return -1;
  168. }
  169. return 0;
  170. }