kv_smc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 "radeon.h"
  26. #include "cikd.h"
  27. #include "kv_dpm.h"
  28. int kv_notify_message_to_smu(struct radeon_device *rdev, u32 id)
  29. {
  30. u32 i;
  31. u32 tmp = 0;
  32. WREG32(SMC_MESSAGE_0, id & SMC_MSG_MASK);
  33. for (i = 0; i < rdev->usec_timeout; i++) {
  34. if ((RREG32(SMC_RESP_0) & SMC_RESP_MASK) != 0)
  35. break;
  36. udelay(1);
  37. }
  38. tmp = RREG32(SMC_RESP_0) & SMC_RESP_MASK;
  39. if (tmp != 1) {
  40. if (tmp == 0xFF)
  41. return -EINVAL;
  42. else if (tmp == 0xFE)
  43. return -EINVAL;
  44. }
  45. return 0;
  46. }
  47. int kv_dpm_get_enable_mask(struct radeon_device *rdev, u32 *enable_mask)
  48. {
  49. int ret;
  50. ret = kv_notify_message_to_smu(rdev, PPSMC_MSG_SCLKDPM_GetEnabledMask);
  51. if (ret == 0)
  52. *enable_mask = RREG32_SMC(SMC_SYSCON_MSG_ARG_0);
  53. return ret;
  54. }
  55. int kv_send_msg_to_smc_with_parameter(struct radeon_device *rdev,
  56. PPSMC_Msg msg, u32 parameter)
  57. {
  58. WREG32(SMC_MSG_ARG_0, parameter);
  59. return kv_notify_message_to_smu(rdev, msg);
  60. }
  61. static int kv_set_smc_sram_address(struct radeon_device *rdev,
  62. u32 smc_address, u32 limit)
  63. {
  64. if (smc_address & 3)
  65. return -EINVAL;
  66. if ((smc_address + 3) > limit)
  67. return -EINVAL;
  68. WREG32(SMC_IND_INDEX_0, smc_address);
  69. WREG32_P(SMC_IND_ACCESS_CNTL, 0, ~AUTO_INCREMENT_IND_0);
  70. return 0;
  71. }
  72. int kv_read_smc_sram_dword(struct radeon_device *rdev, u32 smc_address,
  73. u32 *value, u32 limit)
  74. {
  75. int ret;
  76. ret = kv_set_smc_sram_address(rdev, smc_address, limit);
  77. if (ret)
  78. return ret;
  79. *value = RREG32(SMC_IND_DATA_0);
  80. return 0;
  81. }
  82. int kv_smc_dpm_enable(struct radeon_device *rdev, bool enable)
  83. {
  84. if (enable)
  85. return kv_notify_message_to_smu(rdev, PPSMC_MSG_DPM_Enable);
  86. else
  87. return kv_notify_message_to_smu(rdev, PPSMC_MSG_DPM_Disable);
  88. }
  89. int kv_smc_bapm_enable(struct radeon_device *rdev, bool enable)
  90. {
  91. if (enable)
  92. return kv_notify_message_to_smu(rdev, PPSMC_MSG_EnableBAPM);
  93. else
  94. return kv_notify_message_to_smu(rdev, PPSMC_MSG_DisableBAPM);
  95. }
  96. int kv_copy_bytes_to_smc(struct radeon_device *rdev,
  97. u32 smc_start_address,
  98. const u8 *src, u32 byte_count, u32 limit)
  99. {
  100. int ret;
  101. u32 data, original_data, addr, extra_shift, t_byte, count, mask;
  102. if ((smc_start_address + byte_count) > limit)
  103. return -EINVAL;
  104. addr = smc_start_address;
  105. t_byte = addr & 3;
  106. /* RMW for the initial bytes */
  107. if (t_byte != 0) {
  108. addr -= t_byte;
  109. ret = kv_set_smc_sram_address(rdev, addr, limit);
  110. if (ret)
  111. return ret;
  112. original_data = RREG32(SMC_IND_DATA_0);
  113. data = 0;
  114. mask = 0;
  115. count = 4;
  116. while (count > 0) {
  117. if (t_byte > 0) {
  118. mask = (mask << 8) | 0xff;
  119. t_byte--;
  120. } else if (byte_count > 0) {
  121. data = (data << 8) + *src++;
  122. byte_count--;
  123. mask <<= 8;
  124. } else {
  125. data <<= 8;
  126. mask = (mask << 8) | 0xff;
  127. }
  128. count--;
  129. }
  130. data |= original_data & mask;
  131. ret = kv_set_smc_sram_address(rdev, addr, limit);
  132. if (ret)
  133. return ret;
  134. WREG32(SMC_IND_DATA_0, data);
  135. addr += 4;
  136. }
  137. while (byte_count >= 4) {
  138. /* SMC address space is BE */
  139. data = (src[0] << 24) + (src[1] << 16) + (src[2] << 8) + src[3];
  140. ret = kv_set_smc_sram_address(rdev, addr, limit);
  141. if (ret)
  142. return ret;
  143. WREG32(SMC_IND_DATA_0, data);
  144. src += 4;
  145. byte_count -= 4;
  146. addr += 4;
  147. }
  148. /* RMW for the final bytes */
  149. if (byte_count > 0) {
  150. data = 0;
  151. ret = kv_set_smc_sram_address(rdev, addr, limit);
  152. if (ret)
  153. return ret;
  154. original_data= RREG32(SMC_IND_DATA_0);
  155. extra_shift = 8 * (4 - byte_count);
  156. while (byte_count > 0) {
  157. /* SMC address space is BE */
  158. data = (data << 8) + *src++;
  159. byte_count--;
  160. }
  161. data <<= extra_shift;
  162. data |= (original_data & ~((~0UL) << extra_shift));
  163. ret = kv_set_smc_sram_address(rdev, addr, limit);
  164. if (ret)
  165. return ret;
  166. WREG32(SMC_IND_DATA_0, data);
  167. }
  168. return 0;
  169. }