kv_smc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright 2013 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Alex Deucher
  23. */
  24. #include "drmP.h"
  25. #include "amdgpu.h"
  26. #include "cikd.h"
  27. #include "kv_dpm.h"
  28. #include "smu/smu_7_0_0_d.h"
  29. #include "smu/smu_7_0_0_sh_mask.h"
  30. int amdgpu_kv_notify_message_to_smu(struct amdgpu_device *adev, u32 id)
  31. {
  32. u32 i;
  33. u32 tmp = 0;
  34. WREG32(mmSMC_MESSAGE_0, id & SMC_MESSAGE_0__SMC_MSG_MASK);
  35. for (i = 0; i < adev->usec_timeout; i++) {
  36. if ((RREG32(mmSMC_RESP_0) & SMC_RESP_0__SMC_RESP_MASK) != 0)
  37. break;
  38. udelay(1);
  39. }
  40. tmp = RREG32(mmSMC_RESP_0) & SMC_RESP_0__SMC_RESP_MASK;
  41. if (tmp != 1) {
  42. if (tmp == 0xFF)
  43. return -EINVAL;
  44. else if (tmp == 0xFE)
  45. return -EINVAL;
  46. }
  47. return 0;
  48. }
  49. int amdgpu_kv_dpm_get_enable_mask(struct amdgpu_device *adev, u32 *enable_mask)
  50. {
  51. int ret;
  52. ret = amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_SCLKDPM_GetEnabledMask);
  53. if (ret == 0)
  54. *enable_mask = RREG32_SMC(ixSMC_SYSCON_MSG_ARG_0);
  55. return ret;
  56. }
  57. int amdgpu_kv_send_msg_to_smc_with_parameter(struct amdgpu_device *adev,
  58. PPSMC_Msg msg, u32 parameter)
  59. {
  60. WREG32(mmSMC_MSG_ARG_0, parameter);
  61. return amdgpu_kv_notify_message_to_smu(adev, msg);
  62. }
  63. static int kv_set_smc_sram_address(struct amdgpu_device *adev,
  64. u32 smc_address, u32 limit)
  65. {
  66. if (smc_address & 3)
  67. return -EINVAL;
  68. if ((smc_address + 3) > limit)
  69. return -EINVAL;
  70. WREG32(mmSMC_IND_INDEX_0, smc_address);
  71. WREG32_P(mmSMC_IND_ACCESS_CNTL, 0,
  72. ~SMC_IND_ACCESS_CNTL__AUTO_INCREMENT_IND_0_MASK);
  73. return 0;
  74. }
  75. int amdgpu_kv_read_smc_sram_dword(struct amdgpu_device *adev, u32 smc_address,
  76. u32 *value, u32 limit)
  77. {
  78. int ret;
  79. ret = kv_set_smc_sram_address(adev, smc_address, limit);
  80. if (ret)
  81. return ret;
  82. *value = RREG32(mmSMC_IND_DATA_0);
  83. return 0;
  84. }
  85. int amdgpu_kv_smc_dpm_enable(struct amdgpu_device *adev, bool enable)
  86. {
  87. if (enable)
  88. return amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_DPM_Enable);
  89. else
  90. return amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_DPM_Disable);
  91. }
  92. int amdgpu_kv_smc_bapm_enable(struct amdgpu_device *adev, bool enable)
  93. {
  94. if (enable)
  95. return amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_EnableBAPM);
  96. else
  97. return amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_DisableBAPM);
  98. }
  99. int amdgpu_kv_copy_bytes_to_smc(struct amdgpu_device *adev,
  100. u32 smc_start_address,
  101. const u8 *src, u32 byte_count, u32 limit)
  102. {
  103. int ret;
  104. u32 data, original_data, addr, extra_shift, t_byte, count, mask;
  105. if ((smc_start_address + byte_count) > limit)
  106. return -EINVAL;
  107. addr = smc_start_address;
  108. t_byte = addr & 3;
  109. /* RMW for the initial bytes */
  110. if (t_byte != 0) {
  111. addr -= t_byte;
  112. ret = kv_set_smc_sram_address(adev, addr, limit);
  113. if (ret)
  114. return ret;
  115. original_data = RREG32(mmSMC_IND_DATA_0);
  116. data = 0;
  117. mask = 0;
  118. count = 4;
  119. while (count > 0) {
  120. if (t_byte > 0) {
  121. mask = (mask << 8) | 0xff;
  122. t_byte--;
  123. } else if (byte_count > 0) {
  124. data = (data << 8) + *src++;
  125. byte_count--;
  126. mask <<= 8;
  127. } else {
  128. data <<= 8;
  129. mask = (mask << 8) | 0xff;
  130. }
  131. count--;
  132. }
  133. data |= original_data & mask;
  134. ret = kv_set_smc_sram_address(adev, addr, limit);
  135. if (ret)
  136. return ret;
  137. WREG32(mmSMC_IND_DATA_0, data);
  138. addr += 4;
  139. }
  140. while (byte_count >= 4) {
  141. /* SMC address space is BE */
  142. data = (src[0] << 24) + (src[1] << 16) + (src[2] << 8) + src[3];
  143. ret = kv_set_smc_sram_address(adev, addr, limit);
  144. if (ret)
  145. return ret;
  146. WREG32(mmSMC_IND_DATA_0, data);
  147. src += 4;
  148. byte_count -= 4;
  149. addr += 4;
  150. }
  151. /* RMW for the final bytes */
  152. if (byte_count > 0) {
  153. data = 0;
  154. ret = kv_set_smc_sram_address(adev, addr, limit);
  155. if (ret)
  156. return ret;
  157. original_data = RREG32(mmSMC_IND_DATA_0);
  158. extra_shift = 8 * (4 - byte_count);
  159. while (byte_count > 0) {
  160. /* SMC address space is BE */
  161. data = (data << 8) + *src++;
  162. byte_count--;
  163. }
  164. data <<= extra_shift;
  165. data |= (original_data & ~((~0UL) << extra_shift));
  166. ret = kv_set_smc_sram_address(adev, addr, limit);
  167. if (ret)
  168. return ret;
  169. WREG32(mmSMC_IND_DATA_0, data);
  170. }
  171. return 0;
  172. }