nvg.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <errno.h>
  7. #include <arch.h>
  8. #include <arch_helpers.h>
  9. #include <common/debug.h>
  10. #include <denver.h>
  11. #include <lib/mmio.h>
  12. #include <mce_private.h>
  13. #include <t18x_ari.h>
  14. #include <tegra_private.h>
  15. int32_t nvg_enter_cstate(uint32_t ari_base, uint32_t state, uint32_t wake_time)
  16. {
  17. int32_t ret = 0;
  18. uint64_t val = 0ULL;
  19. (void)ari_base;
  20. /* check for allowed power state */
  21. if ((state != TEGRA_ARI_CORE_C0) && (state != TEGRA_ARI_CORE_C1) &&
  22. (state != TEGRA_ARI_CORE_C6) && (state != TEGRA_ARI_CORE_C7)) {
  23. ERROR("%s: unknown cstate (%d)\n", __func__, state);
  24. ret = EINVAL;
  25. } else {
  26. /* time (TSC ticks) until the core is expected to get a wake event */
  27. nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_WAKE_TIME, wake_time);
  28. /* set the core cstate */
  29. val = read_actlr_el1() & ~ACTLR_EL1_PMSTATE_MASK;
  30. write_actlr_el1(val | (uint64_t)state);
  31. }
  32. return ret;
  33. }
  34. /*
  35. * This request allows updating of CLUSTER_CSTATE, CCPLEX_CSTATE and
  36. * SYSTEM_CSTATE values.
  37. */
  38. int32_t nvg_update_cstate_info(uint32_t ari_base, uint32_t cluster, uint32_t ccplex,
  39. uint32_t system, uint8_t sys_state_force, uint32_t wake_mask,
  40. uint8_t update_wake_mask)
  41. {
  42. uint64_t val = 0ULL;
  43. (void)ari_base;
  44. /* update CLUSTER_CSTATE? */
  45. if (cluster != 0U) {
  46. val |= ((uint64_t)cluster & CLUSTER_CSTATE_MASK) |
  47. CLUSTER_CSTATE_UPDATE_BIT;
  48. }
  49. /* update CCPLEX_CSTATE? */
  50. if (ccplex != 0U) {
  51. val |= (((uint64_t)ccplex & CCPLEX_CSTATE_MASK) << CCPLEX_CSTATE_SHIFT) |
  52. CCPLEX_CSTATE_UPDATE_BIT;
  53. }
  54. /* update SYSTEM_CSTATE? */
  55. if (system != 0U) {
  56. val |= (((uint64_t)system & SYSTEM_CSTATE_MASK) << SYSTEM_CSTATE_SHIFT) |
  57. (((uint64_t)sys_state_force << SYSTEM_CSTATE_FORCE_UPDATE_SHIFT) |
  58. SYSTEM_CSTATE_UPDATE_BIT);
  59. }
  60. /* update wake mask value? */
  61. if (update_wake_mask != 0U) {
  62. val |= CSTATE_WAKE_MASK_UPDATE_BIT;
  63. }
  64. /* set the wake mask */
  65. val &= CSTATE_WAKE_MASK_CLEAR;
  66. val |= ((uint64_t)wake_mask << CSTATE_WAKE_MASK_SHIFT);
  67. /* set the updated cstate info */
  68. nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_CSTATE_INFO, val);
  69. return 0;
  70. }
  71. int32_t nvg_update_crossover_time(uint32_t ari_base, uint32_t type, uint32_t time)
  72. {
  73. int32_t ret = 0;
  74. (void)ari_base;
  75. /* sanity check crossover type */
  76. if (type > TEGRA_ARI_CROSSOVER_CCP3_SC1) {
  77. ret = EINVAL;
  78. } else {
  79. /*
  80. * The crossover threshold limit types start from
  81. * TEGRA_CROSSOVER_TYPE_C1_C6 to TEGRA_CROSSOVER_TYPE_CCP3_SC7.
  82. * The command indices for updating the threshold be generated
  83. * by adding the type to the NVG_SET_THRESHOLD_CROSSOVER_C1_C6
  84. * command index.
  85. */
  86. nvg_set_request_data((TEGRA_NVG_CHANNEL_CROSSOVER_C1_C6 +
  87. (uint64_t)type), (uint64_t)time);
  88. }
  89. return ret;
  90. }
  91. uint64_t nvg_read_cstate_stats(uint32_t ari_base, uint32_t state)
  92. {
  93. uint64_t ret;
  94. (void)ari_base;
  95. /* sanity check state */
  96. if (state == 0U) {
  97. ret = EINVAL;
  98. } else {
  99. /*
  100. * The cstate types start from NVG_READ_CSTATE_STATS_SC7_ENTRIES
  101. * to NVG_GET_LAST_CSTATE_ENTRY_A57_3. The command indices for
  102. * reading the threshold can be generated by adding the type to
  103. * the NVG_CLEAR_CSTATE_STATS command index.
  104. */
  105. nvg_set_request((TEGRA_NVG_CHANNEL_CSTATE_STATS_CLEAR +
  106. (uint64_t)state));
  107. ret = nvg_get_result();
  108. }
  109. return ret;
  110. }
  111. int32_t nvg_write_cstate_stats(uint32_t ari_base, uint32_t state, uint32_t stats)
  112. {
  113. uint64_t val;
  114. (void)ari_base;
  115. /*
  116. * The only difference between a CSTATE_STATS_WRITE and
  117. * CSTATE_STATS_READ is the usage of the 63:32 in the request.
  118. * 63:32 are set to '0' for a read, while a write contains the
  119. * actual stats value to be written.
  120. */
  121. val = ((uint64_t)stats << MCE_CSTATE_STATS_TYPE_SHIFT) | state;
  122. /*
  123. * The cstate types start from NVG_READ_CSTATE_STATS_SC7_ENTRIES
  124. * to NVG_GET_LAST_CSTATE_ENTRY_A57_3. The command indices for
  125. * reading the threshold can be generated by adding the type to
  126. * the NVG_CLEAR_CSTATE_STATS command index.
  127. */
  128. nvg_set_request_data((TEGRA_NVG_CHANNEL_CSTATE_STATS_CLEAR +
  129. (uint64_t)state), val);
  130. return 0;
  131. }
  132. int32_t nvg_is_ccx_allowed(uint32_t ari_base, uint32_t state, uint32_t wake_time)
  133. {
  134. (void)ari_base;
  135. (void)state;
  136. (void)wake_time;
  137. /* This does not apply to the Denver cluster */
  138. return 0;
  139. }
  140. int32_t nvg_is_sc7_allowed(uint32_t ari_base, uint32_t state, uint32_t wake_time)
  141. {
  142. uint64_t val;
  143. int32_t ret;
  144. (void)ari_base;
  145. /* check for allowed power state */
  146. if ((state != TEGRA_ARI_CORE_C0) && (state != TEGRA_ARI_CORE_C1) &&
  147. (state != TEGRA_ARI_CORE_C6) && (state != TEGRA_ARI_CORE_C7)) {
  148. ERROR("%s: unknown cstate (%d)\n", __func__, state);
  149. ret = EINVAL;
  150. } else {
  151. /*
  152. * Request format -
  153. * 63:32 = wake time
  154. * 31:0 = C-state for this core
  155. */
  156. val = ((uint64_t)wake_time << MCE_SC7_WAKE_TIME_SHIFT) |
  157. ((uint64_t)state & MCE_SC7_ALLOWED_MASK);
  158. /* issue command to check if SC7 is allowed */
  159. nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_IS_SC7_ALLOWED, val);
  160. /* 1 = SC7 allowed, 0 = SC7 not allowed */
  161. ret = (nvg_get_result() != 0ULL) ? 1 : 0;
  162. }
  163. return ret;
  164. }
  165. int32_t nvg_online_core(uint32_t ari_base, uint32_t core)
  166. {
  167. uint64_t cpu = read_mpidr() & MPIDR_CPU_MASK;
  168. uint64_t impl = (read_midr() >> MIDR_IMPL_SHIFT) & MIDR_IMPL_MASK;
  169. int32_t ret = 0;
  170. (void)ari_base;
  171. /* sanity check code id */
  172. if ((core >= MCE_CORE_ID_MAX) || (cpu == core)) {
  173. ERROR("%s: unsupported core id (%d)\n", __func__, core);
  174. ret = EINVAL;
  175. } else {
  176. /*
  177. * The Denver cluster has 2 CPUs only - 0, 1.
  178. */
  179. if ((impl == DENVER_IMPL) && ((core == 2U) || (core == 3U))) {
  180. ERROR("%s: unknown core id (%d)\n", __func__, core);
  181. ret = EINVAL;
  182. } else {
  183. /* get a core online */
  184. nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_ONLINE_CORE,
  185. ((uint64_t)core & MCE_CORE_ID_MASK));
  186. }
  187. }
  188. return ret;
  189. }
  190. int32_t nvg_cc3_ctrl(uint32_t ari_base, uint32_t freq, uint32_t volt, uint8_t enable)
  191. {
  192. uint32_t val;
  193. (void)ari_base;
  194. /*
  195. * If the enable bit is cleared, Auto-CC3 will be disabled by setting
  196. * the SW visible voltage/frequency request registers for all non
  197. * floorswept cores valid independent of StandbyWFI and disabling
  198. * the IDLE voltage/frequency request register. If set, Auto-CC3
  199. * will be enabled by setting the ARM SW visible voltage/frequency
  200. * request registers for all non floorswept cores to be enabled by
  201. * StandbyWFI or the equivalent signal, and always keeping the IDLE
  202. * voltage/frequency request register enabled.
  203. */
  204. val = (((freq & MCE_AUTO_CC3_FREQ_MASK) << MCE_AUTO_CC3_FREQ_SHIFT) |
  205. ((volt & MCE_AUTO_CC3_VTG_MASK) << MCE_AUTO_CC3_VTG_SHIFT) |
  206. ((enable != 0U) ? MCE_AUTO_CC3_ENABLE_BIT : 0U));
  207. nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_CC3_CTRL, (uint64_t)val);
  208. return 0;
  209. }