gamemode-gpu.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // TODO
  31. // Gather GPU type and information
  32. // Allow configuration file specifying of gpu info
  33. // Apply Nvidia GPU settings (CoolBits will be needed)
  34. // Apply AMD GPU settings (Will need user changing pwm1_enable)
  35. // Intel?
  36. // Provide documentation on optimisations
  37. /* Enums for GPU vendors */
  38. enum GPUVendor {
  39. Vendor_Invalid = 0,
  40. Vendor_NVIDIA = 0x10de,
  41. Vendor_AMD = 0x1002,
  42. Vendor_Intel = 0x8086
  43. };
  44. /* Storage for static GPU info gathered at start */
  45. struct GameModeGPUInfo {
  46. enum GPUVendor vendor;
  47. int device; /* path to device, ie. /sys/class/drm/card#/ */
  48. };
  49. /**
  50. * Applies or removes Nvidia optimisations
  51. */
  52. static int apply_gpu_nvidia(bool apply)
  53. {
  54. // Running these commands:
  55. // nvidia-settings -a '[gpu:0]/GPUMemoryTransferRateOffset[3]=1400'
  56. // nvidia-settings -a '[gpu:0]/GPUGraphicsClockOffset[3]=50'
  57. if (apply) {
  58. } else {
  59. }
  60. return 0;
  61. }
  62. /**
  63. * Applies or removes AMD optimisations
  64. */
  65. static int apply_gpu_amd(bool apply)
  66. {
  67. // We'll want to set both the following:
  68. // core: device/pp_sclk_od (0%+ additional)
  69. // mem: device/pp_mclk_od (0%+ additional)
  70. // Guide from https://www.maketecheasier.com/overclock-amd-gpu-linux/
  71. if (apply) {
  72. } else {
  73. }
  74. return 0;
  75. }
  76. /**
  77. * Attempts to identify the current in use GPU information
  78. */
  79. int game_mode_identify_gpu(GameModeGPUInfo **info)
  80. {
  81. int status = 0;
  82. /* Verify input, this is programmer error */
  83. if (!info || *info)
  84. FATAL_ERROR("Invalid GameModeGPUInfo passed to %s", __func__);
  85. /* Create the context */
  86. GameModeGPUInfo *new_info = malloc(sizeof(GameModeGPUInfo));
  87. memset(new_info, 0, sizeof(GameModeGPUInfo));
  88. // TODO: Fill in the GPU info
  89. /* Give back the new gpu info */
  90. *info = new_info;
  91. return status;
  92. }
  93. /* Simply used to free the GPU info object */
  94. void game_mode_free_gpu(GameModeGPUInfo **info)
  95. {
  96. /* Simply free the object */
  97. free(*info);
  98. *info = NULL;
  99. }
  100. /**
  101. * Applies GPU optimisations when gamemode is active and removes them after
  102. */
  103. int game_mode_apply_gpu(const GameModeGPUInfo *info, bool apply)
  104. {
  105. switch (info->vendor) {
  106. case Vendor_NVIDIA:
  107. return apply_gpu_nvidia(apply);
  108. case Vendor_AMD:
  109. return apply_gpu_amd(apply);
  110. default:
  111. break;
  112. }
  113. /* Unsupported GPU vendor, do nothing */
  114. return 0;
  115. }