sq_scmi.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <arch_helpers.h>
  9. #include <common/debug.h>
  10. #include <drivers/arm/css/css_mhu_doorbell.h>
  11. #include <drivers/arm/css/css_scp.h>
  12. #include <drivers/arm/css/scmi.h>
  13. #include <plat/arm/css/common/css_pm.h>
  14. #include <plat/common/platform.h>
  15. #include <platform_def.h>
  16. #include <scmi_sq.h>
  17. #include <sq_common.h>
  18. /*
  19. * This file implements the SCP helper functions using SCMI protocol.
  20. */
  21. DEFINE_BAKERY_LOCK(sq_scmi_lock);
  22. #define SQ_SCMI_LOCK_GET_INSTANCE (&sq_scmi_lock)
  23. #define SQ_SCMI_PAYLOAD_BASE PLAT_SQ_SCP_COM_SHARED_MEM_BASE
  24. #define MHU_CPU_INTR_S_SET_OFFSET 0x308
  25. const uint32_t sq_core_pos_to_scmi_dmn_id_map[PLATFORM_CORE_COUNT] = {
  26. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  27. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23
  28. };
  29. static scmi_channel_plat_info_t sq_scmi_plat_info = {
  30. .scmi_mbx_mem = SQ_SCMI_PAYLOAD_BASE,
  31. .db_reg_addr = PLAT_SQ_MHU_BASE + MHU_CPU_INTR_S_SET_OFFSET,
  32. .db_preserve_mask = 0xfffffffe,
  33. .db_modify_mask = 0x1,
  34. .ring_doorbell = &mhu_ring_doorbell,
  35. };
  36. /*
  37. * SCMI power state parameter bit field encoding for SynQuacer platform.
  38. *
  39. * 31 20 19 16 15 12 11 8 7 4 3 0
  40. * +-------------------------------------------------------------+
  41. * | SBZ | Max level | Level 3 | Level 2 | Level 1 | Level 0 |
  42. * | | | state | state | state | state |
  43. * +-------------------------------------------------------------+
  44. *
  45. * `Max level` encodes the highest level that has a valid power state
  46. * encoded in the power state.
  47. */
  48. #define SCMI_PWR_STATE_MAX_PWR_LVL_SHIFT 16
  49. #define SCMI_PWR_STATE_MAX_PWR_LVL_WIDTH 4
  50. #define SCMI_PWR_STATE_MAX_PWR_LVL_MASK \
  51. ((1 << SCMI_PWR_STATE_MAX_PWR_LVL_WIDTH) - 1)
  52. #define SCMI_SET_PWR_STATE_MAX_PWR_LVL(_power_state, _max_level) \
  53. (_power_state) |= ((_max_level) & SCMI_PWR_STATE_MAX_PWR_LVL_MASK)\
  54. << SCMI_PWR_STATE_MAX_PWR_LVL_SHIFT
  55. #define SCMI_GET_PWR_STATE_MAX_PWR_LVL(_power_state) \
  56. (((_power_state) >> SCMI_PWR_STATE_MAX_PWR_LVL_SHIFT) \
  57. & SCMI_PWR_STATE_MAX_PWR_LVL_MASK)
  58. #define SCMI_PWR_STATE_LVL_WIDTH 4
  59. #define SCMI_PWR_STATE_LVL_MASK \
  60. ((1 << SCMI_PWR_STATE_LVL_WIDTH) - 1)
  61. #define SCMI_SET_PWR_STATE_LVL(_power_state, _level, _level_state) \
  62. (_power_state) |= ((_level_state) & SCMI_PWR_STATE_LVL_MASK) \
  63. << (SCMI_PWR_STATE_LVL_WIDTH * (_level))
  64. #define SCMI_GET_PWR_STATE_LVL(_power_state, _level) \
  65. (((_power_state) >> (SCMI_PWR_STATE_LVL_WIDTH * (_level))) & \
  66. SCMI_PWR_STATE_LVL_MASK)
  67. /*
  68. * The SCMI power state enumeration for a power domain level
  69. */
  70. typedef enum {
  71. scmi_power_state_off = 0,
  72. scmi_power_state_on = 1,
  73. scmi_power_state_sleep = 2,
  74. } scmi_power_state_t;
  75. /*
  76. * The global handle for invoking the SCMI driver APIs after the driver
  77. * has been initialized.
  78. */
  79. static void *sq_scmi_handle;
  80. /* The SCMI channel global object */
  81. static scmi_channel_t channel;
  82. /*
  83. * Helper function to turn off a CPU power domain and
  84. * its parent power domains if applicable.
  85. */
  86. void sq_scmi_off(const struct psci_power_state *target_state)
  87. {
  88. int lvl = 0, ret;
  89. uint32_t scmi_pwr_state = 0;
  90. /* At-least the CPU level should be specified to be OFF */
  91. assert(target_state->pwr_domain_state[SQ_PWR_LVL0] ==
  92. SQ_LOCAL_STATE_OFF);
  93. for (; lvl <= PLAT_MAX_PWR_LVL; lvl++) {
  94. if (target_state->pwr_domain_state[lvl] == SQ_LOCAL_STATE_RUN)
  95. break;
  96. assert(target_state->pwr_domain_state[lvl] ==
  97. SQ_LOCAL_STATE_OFF);
  98. SCMI_SET_PWR_STATE_LVL(scmi_pwr_state, lvl,
  99. scmi_power_state_off);
  100. }
  101. SCMI_SET_PWR_STATE_MAX_PWR_LVL(scmi_pwr_state, lvl - 1);
  102. ret = scmi_pwr_state_set(sq_scmi_handle,
  103. sq_core_pos_to_scmi_dmn_id_map[plat_my_core_pos()],
  104. scmi_pwr_state);
  105. if (ret != SCMI_E_QUEUED && ret != SCMI_E_SUCCESS) {
  106. ERROR("SCMI set power state command return 0x%x unexpected\n",
  107. ret);
  108. panic();
  109. }
  110. }
  111. /*
  112. * Helper function to turn ON a CPU power domain and
  113. *its parent power domains if applicable.
  114. */
  115. void sq_scmi_on(u_register_t mpidr)
  116. {
  117. int lvl = 0, ret, core_pos;
  118. uint32_t scmi_pwr_state = 0;
  119. for (; lvl <= PLAT_MAX_PWR_LVL; lvl++)
  120. SCMI_SET_PWR_STATE_LVL(scmi_pwr_state, lvl,
  121. scmi_power_state_on);
  122. SCMI_SET_PWR_STATE_MAX_PWR_LVL(scmi_pwr_state, lvl - 1);
  123. core_pos = plat_core_pos_by_mpidr(mpidr);
  124. assert(core_pos >= 0 && core_pos < PLATFORM_CORE_COUNT);
  125. ret = scmi_pwr_state_set(sq_scmi_handle,
  126. sq_core_pos_to_scmi_dmn_id_map[core_pos],
  127. scmi_pwr_state);
  128. if (ret != SCMI_E_QUEUED && ret != SCMI_E_SUCCESS) {
  129. ERROR("SCMI set power state command return 0x%x unexpected\n",
  130. ret);
  131. panic();
  132. }
  133. }
  134. void __dead2 sq_scmi_system_off(int state)
  135. {
  136. int ret;
  137. /*
  138. * Disable GIC CPU interface to prevent pending interrupt from waking
  139. * up the AP from WFI.
  140. */
  141. sq_gic_cpuif_disable();
  142. /*
  143. * Issue SCMI command. First issue a graceful
  144. * request and if that fails force the request.
  145. */
  146. ret = scmi_sys_pwr_state_set(sq_scmi_handle,
  147. SCMI_SYS_PWR_FORCEFUL_REQ,
  148. state);
  149. if (ret != SCMI_E_SUCCESS) {
  150. ERROR("SCMI system power state set 0x%x returns unexpected 0x%x\n",
  151. state, ret);
  152. panic();
  153. }
  154. wfi();
  155. ERROR("SCMI set power state: operation not handled.\n");
  156. panic();
  157. }
  158. /*
  159. * Helper function to reset the system via SCMI.
  160. */
  161. void __dead2 sq_scmi_sys_shutdown(void)
  162. {
  163. sq_scmi_system_off(SCMI_SYS_PWR_SHUTDOWN);
  164. }
  165. void __dead2 sq_scmi_sys_reboot(void)
  166. {
  167. sq_scmi_system_off(SCMI_SYS_PWR_COLD_RESET);
  168. }
  169. static int scmi_ap_core_init(scmi_channel_t *ch)
  170. {
  171. #if PROGRAMMABLE_RESET_ADDRESS
  172. uint32_t version;
  173. int ret;
  174. ret = scmi_proto_version(ch, SCMI_AP_CORE_PROTO_ID, &version);
  175. if (ret != SCMI_E_SUCCESS) {
  176. WARN("SCMI AP core protocol version message failed\n");
  177. return -1;
  178. }
  179. if (!is_scmi_version_compatible(SCMI_AP_CORE_PROTO_VER, version)) {
  180. WARN("SCMI AP core protocol version 0x%x incompatible with driver version 0x%x\n",
  181. version, SCMI_AP_CORE_PROTO_VER);
  182. return -1;
  183. }
  184. INFO("SCMI AP core protocol version 0x%x detected\n", version);
  185. #endif
  186. return 0;
  187. }
  188. void __init plat_sq_pwrc_setup(void)
  189. {
  190. channel.info = &sq_scmi_plat_info;
  191. channel.lock = SQ_SCMI_LOCK_GET_INSTANCE;
  192. sq_scmi_handle = scmi_init(&channel);
  193. if (sq_scmi_handle == NULL) {
  194. ERROR("SCMI Initialization failed\n");
  195. panic();
  196. }
  197. if (scmi_ap_core_init(&channel) < 0) {
  198. ERROR("SCMI AP core protocol initialization failed\n");
  199. panic();
  200. }
  201. }
  202. uint32_t sq_scmi_get_draminfo(struct draminfo *info)
  203. {
  204. scmi_get_draminfo(sq_scmi_handle, info);
  205. return 0;
  206. }