gamemode-gpu.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "gamemode.h"
  28. #include "helpers.h"
  29. #include "logging.h"
  30. #include "daemon_config.h"
  31. // TODO
  32. // Gather GPU type and information
  33. // Allow configuration file specifying of gpu info
  34. // Apply Nvidia GPU settings (CoolBits will be needed)
  35. // Apply AMD GPU settings (Will need user changing pwm1_enable)
  36. // Intel?
  37. // Provide documentation on optimisations
  38. /* Enums for GPU vendors */
  39. enum GPUVendor {
  40. Vendor_Invalid = 0,
  41. Vendor_NVIDIA = 0x10de,
  42. Vendor_AMD = 0x1002,
  43. Vendor_Intel = 0x8086
  44. };
  45. /* Storage for static GPU info gathered at start */
  46. struct GameModeGPUInfo {
  47. enum GPUVendor vendor;
  48. int device; /* path to device, ie. /sys/class/drm/card#/ */
  49. long core; /* Core clock to apply */
  50. long mem; /* Mem clock to apply */
  51. };
  52. /**
  53. * Applies or removes Nvidia optimisations
  54. */
  55. static int apply_gpu_nvidia(const GameModeGPUInfo *info, bool apply)
  56. {
  57. if (!info)
  58. return 0;
  59. // Running these commands:
  60. // nvidia-settings -a '[gpu:0]/GPUMemoryTransferRateOffset[3]=1400'
  61. // nvidia-settings -a '[gpu:0]/GPUGraphicsClockOffset[3]=50'
  62. if (apply) {
  63. } else {
  64. }
  65. return 0;
  66. }
  67. /**
  68. * Applies or removes AMD optimisations
  69. */
  70. static int apply_gpu_amd(const GameModeGPUInfo *info, bool apply)
  71. {
  72. if (!info)
  73. return 0;
  74. // We'll want to set both the following:
  75. // core: device/pp_sclk_od (0%+ additional)
  76. // mem: device/pp_mclk_od (0%+ additional)
  77. // Guide from https://www.maketecheasier.com/overclock-amd-gpu-linux/
  78. if (apply) {
  79. } else {
  80. }
  81. return 0;
  82. }
  83. /**
  84. * Attempts to identify the current in use GPU information
  85. */
  86. int game_mode_initialise_gpu(GameModeConfig *config, GameModeGPUInfo **info)
  87. {
  88. int status = 0;
  89. /* Verify input, this is programmer error */
  90. if (!info || *info)
  91. FATAL_ERROR("Invalid GameModeGPUInfo passed to %s", __func__);
  92. /* Early out if we have this feature turned off */
  93. long apply = 0;
  94. config_get_apply_gpu_optimisations(config, &apply);
  95. if (apply == 0)
  96. return 0;
  97. /* Create the context */
  98. GameModeGPUInfo *new_info = malloc(sizeof(GameModeGPUInfo));
  99. memset(new_info, 0, sizeof(GameModeGPUInfo));
  100. // TODO: Fill in the GPU info
  101. /* Load the config based on GPU */
  102. switch (new_info->vendor) {
  103. case Vendor_NVIDIA:
  104. config_get_nv_core_clock_mhz_offset(config, &new_info->core);
  105. config_get_nv_mem_clock_mhz_offset(config, &new_info->mem);
  106. break;
  107. case Vendor_AMD:
  108. config_get_amd_core_clock_percentage(config, &new_info->core);
  109. config_get_amd_mem_clock_percentage(config, &new_info->mem);
  110. break;
  111. default:
  112. break;
  113. }
  114. /* Give back the new gpu info */
  115. *info = new_info;
  116. return status;
  117. }
  118. /* Simply used to free the GPU info object */
  119. void game_mode_free_gpu(GameModeGPUInfo **info)
  120. {
  121. /* Simply free the object */
  122. free(*info);
  123. *info = NULL;
  124. }
  125. /**
  126. * Applies GPU optimisations when gamemode is active and removes them after
  127. */
  128. int game_mode_apply_gpu(const GameModeGPUInfo *info, bool apply)
  129. {
  130. // Null info means don't apply anything
  131. if (!info)
  132. return 0;
  133. switch (info->vendor) {
  134. case Vendor_NVIDIA:
  135. return apply_gpu_nvidia(info, apply);
  136. case Vendor_AMD:
  137. return apply_gpu_amd(info, apply);
  138. default:
  139. break;
  140. }
  141. /* Unsupported GPU vendor, do nothing */
  142. return 0;
  143. }