psci.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright (c) 2013-2019, Arm Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2023, NVIDIA Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #ifndef PSCI_H
  8. #define PSCI_H
  9. #include <platform_def.h> /* for PLAT_NUM_PWR_DOMAINS */
  10. #include <common/bl_common.h>
  11. #include <lib/bakery_lock.h>
  12. #include <lib/psci/psci_lib.h> /* To maintain compatibility for SPDs */
  13. #include <lib/utils_def.h>
  14. /*******************************************************************************
  15. * Number of power domains whose state this PSCI implementation can track
  16. ******************************************************************************/
  17. #ifdef PLAT_NUM_PWR_DOMAINS
  18. #define PSCI_NUM_PWR_DOMAINS PLAT_NUM_PWR_DOMAINS
  19. #else
  20. #define PSCI_NUM_PWR_DOMAINS (U(2) * PLATFORM_CORE_COUNT)
  21. #endif
  22. #define PSCI_NUM_NON_CPU_PWR_DOMAINS (PSCI_NUM_PWR_DOMAINS - \
  23. PLATFORM_CORE_COUNT)
  24. /* This is the power level corresponding to a CPU */
  25. #define PSCI_CPU_PWR_LVL U(0)
  26. /*
  27. * The maximum power level supported by PSCI. Since PSCI CPU_SUSPEND
  28. * uses the old power_state parameter format which has 2 bits to specify the
  29. * power level, this constant is defined to be 3.
  30. */
  31. #define PSCI_MAX_PWR_LVL U(3)
  32. /*******************************************************************************
  33. * Defines for runtime services function ids
  34. ******************************************************************************/
  35. #define PSCI_VERSION U(0x84000000)
  36. #define PSCI_CPU_SUSPEND_AARCH32 U(0x84000001)
  37. #define PSCI_CPU_SUSPEND_AARCH64 U(0xc4000001)
  38. #define PSCI_CPU_OFF U(0x84000002)
  39. #define PSCI_CPU_ON_AARCH32 U(0x84000003)
  40. #define PSCI_CPU_ON_AARCH64 U(0xc4000003)
  41. #define PSCI_AFFINITY_INFO_AARCH32 U(0x84000004)
  42. #define PSCI_AFFINITY_INFO_AARCH64 U(0xc4000004)
  43. #define PSCI_MIG_AARCH32 U(0x84000005)
  44. #define PSCI_MIG_AARCH64 U(0xc4000005)
  45. #define PSCI_MIG_INFO_TYPE U(0x84000006)
  46. #define PSCI_MIG_INFO_UP_CPU_AARCH32 U(0x84000007)
  47. #define PSCI_MIG_INFO_UP_CPU_AARCH64 U(0xc4000007)
  48. #define PSCI_SYSTEM_OFF U(0x84000008)
  49. #define PSCI_SYSTEM_RESET U(0x84000009)
  50. #define PSCI_FEATURES U(0x8400000A)
  51. #define PSCI_NODE_HW_STATE_AARCH32 U(0x8400000d)
  52. #define PSCI_NODE_HW_STATE_AARCH64 U(0xc400000d)
  53. #define PSCI_SYSTEM_SUSPEND_AARCH32 U(0x8400000E)
  54. #define PSCI_SYSTEM_SUSPEND_AARCH64 U(0xc400000E)
  55. #define PSCI_SET_SUSPEND_MODE U(0x8400000F)
  56. #define PSCI_STAT_RESIDENCY_AARCH32 U(0x84000010)
  57. #define PSCI_STAT_RESIDENCY_AARCH64 U(0xc4000010)
  58. #define PSCI_STAT_COUNT_AARCH32 U(0x84000011)
  59. #define PSCI_STAT_COUNT_AARCH64 U(0xc4000011)
  60. #define PSCI_SYSTEM_RESET2_AARCH32 U(0x84000012)
  61. #define PSCI_SYSTEM_RESET2_AARCH64 U(0xc4000012)
  62. #define PSCI_MEM_PROTECT U(0x84000013)
  63. #define PSCI_MEM_CHK_RANGE_AARCH32 U(0x84000014)
  64. #define PSCI_MEM_CHK_RANGE_AARCH64 U(0xc4000014)
  65. /*
  66. * Number of PSCI calls (above) implemented
  67. */
  68. #if ENABLE_PSCI_STAT
  69. #if PSCI_OS_INIT_MODE
  70. #define PSCI_NUM_CALLS U(30)
  71. #else
  72. #define PSCI_NUM_CALLS U(29)
  73. #endif
  74. #else
  75. #if PSCI_OS_INIT_MODE
  76. #define PSCI_NUM_CALLS U(26)
  77. #else
  78. #define PSCI_NUM_CALLS U(25)
  79. #endif
  80. #endif
  81. /* The macros below are used to identify PSCI calls from the SMC function ID */
  82. #define PSCI_FID_MASK U(0xffe0)
  83. #define PSCI_FID_VALUE U(0)
  84. #define is_psci_fid(_fid) \
  85. (((_fid) & PSCI_FID_MASK) == PSCI_FID_VALUE)
  86. /*******************************************************************************
  87. * PSCI Migrate and friends
  88. ******************************************************************************/
  89. #define PSCI_TOS_UP_MIG_CAP 0
  90. #define PSCI_TOS_NOT_UP_MIG_CAP 1
  91. #define PSCI_TOS_NOT_PRESENT_MP 2
  92. /*******************************************************************************
  93. * PSCI CPU_SUSPEND 'power_state' parameter specific defines
  94. ******************************************************************************/
  95. #define PSTATE_ID_SHIFT U(0)
  96. #if PSCI_EXTENDED_STATE_ID
  97. #define PSTATE_VALID_MASK U(0xB0000000)
  98. #define PSTATE_TYPE_SHIFT U(30)
  99. #define PSTATE_ID_MASK U(0xfffffff)
  100. #else
  101. #define PSTATE_VALID_MASK U(0xFCFE0000)
  102. #define PSTATE_TYPE_SHIFT U(16)
  103. #define PSTATE_PWR_LVL_SHIFT U(24)
  104. #define PSTATE_ID_MASK U(0xffff)
  105. #define PSTATE_PWR_LVL_MASK U(0x3)
  106. #define psci_get_pstate_pwrlvl(pstate) (((pstate) >> PSTATE_PWR_LVL_SHIFT) & \
  107. PSTATE_PWR_LVL_MASK)
  108. #define psci_make_powerstate(state_id, type, pwrlvl) \
  109. (((state_id) & PSTATE_ID_MASK) << PSTATE_ID_SHIFT) |\
  110. (((type) & PSTATE_TYPE_MASK) << PSTATE_TYPE_SHIFT) |\
  111. (((pwrlvl) & PSTATE_PWR_LVL_MASK) << PSTATE_PWR_LVL_SHIFT)
  112. #endif /* __PSCI_EXTENDED_STATE_ID__ */
  113. #define PSTATE_TYPE_STANDBY U(0x0)
  114. #define PSTATE_TYPE_POWERDOWN U(0x1)
  115. #define PSTATE_TYPE_MASK U(0x1)
  116. /*******************************************************************************
  117. * PSCI CPU_FEATURES feature flag specific defines
  118. ******************************************************************************/
  119. /* Features flags for CPU SUSPEND power state parameter format. Bits [1:1] */
  120. #define FF_PSTATE_SHIFT U(1)
  121. #define FF_PSTATE_ORIG U(0)
  122. #define FF_PSTATE_EXTENDED U(1)
  123. #if PSCI_EXTENDED_STATE_ID
  124. #define FF_PSTATE FF_PSTATE_EXTENDED
  125. #else
  126. #define FF_PSTATE FF_PSTATE_ORIG
  127. #endif
  128. /* Features flags for CPU SUSPEND OS Initiated mode support. Bits [0:0] */
  129. #define FF_MODE_SUPPORT_SHIFT U(0)
  130. #if PSCI_OS_INIT_MODE
  131. #define FF_SUPPORTS_OS_INIT_MODE U(1)
  132. #else
  133. #define FF_SUPPORTS_OS_INIT_MODE U(0)
  134. #endif
  135. /*******************************************************************************
  136. * PSCI version
  137. ******************************************************************************/
  138. #define PSCI_MAJOR_VER (U(1) << 16)
  139. #define PSCI_MINOR_VER U(0x1)
  140. /*******************************************************************************
  141. * PSCI error codes
  142. ******************************************************************************/
  143. #define PSCI_E_SUCCESS 0
  144. #define PSCI_E_NOT_SUPPORTED -1
  145. #define PSCI_E_INVALID_PARAMS -2
  146. #define PSCI_E_DENIED -3
  147. #define PSCI_E_ALREADY_ON -4
  148. #define PSCI_E_ON_PENDING -5
  149. #define PSCI_E_INTERN_FAIL -6
  150. #define PSCI_E_NOT_PRESENT -7
  151. #define PSCI_E_DISABLED -8
  152. #define PSCI_E_INVALID_ADDRESS -9
  153. #define PSCI_INVALID_MPIDR ~((u_register_t)0)
  154. /*
  155. * SYSTEM_RESET2 macros
  156. */
  157. #define PSCI_RESET2_TYPE_VENDOR_SHIFT U(31)
  158. #define PSCI_RESET2_TYPE_VENDOR (U(1) << PSCI_RESET2_TYPE_VENDOR_SHIFT)
  159. #define PSCI_RESET2_TYPE_ARCH (U(0) << PSCI_RESET2_TYPE_VENDOR_SHIFT)
  160. #define PSCI_RESET2_SYSTEM_WARM_RESET (PSCI_RESET2_TYPE_ARCH | U(0))
  161. #ifndef __ASSEMBLER__
  162. #include <stdint.h>
  163. /* Function to help build the psci capabilities bitfield */
  164. static inline unsigned int define_psci_cap(unsigned int x)
  165. {
  166. return U(1) << (x & U(0x1f));
  167. }
  168. /* Power state helper functions */
  169. static inline unsigned int psci_get_pstate_id(unsigned int power_state)
  170. {
  171. return ((power_state) >> PSTATE_ID_SHIFT) & PSTATE_ID_MASK;
  172. }
  173. static inline unsigned int psci_get_pstate_type(unsigned int power_state)
  174. {
  175. return ((power_state) >> PSTATE_TYPE_SHIFT) & PSTATE_TYPE_MASK;
  176. }
  177. static inline unsigned int psci_check_power_state(unsigned int power_state)
  178. {
  179. return ((power_state) & PSTATE_VALID_MASK);
  180. }
  181. /*
  182. * These are the states reported by the PSCI_AFFINITY_INFO API for the specified
  183. * CPU. The definitions of these states can be found in Section 5.7.1 in the
  184. * PSCI specification (ARM DEN 0022C).
  185. */
  186. typedef enum {
  187. AFF_STATE_ON = U(0),
  188. AFF_STATE_OFF = U(1),
  189. AFF_STATE_ON_PENDING = U(2)
  190. } aff_info_state_t;
  191. /*
  192. * These are the power states reported by PSCI_NODE_HW_STATE API for the
  193. * specified CPU. The definitions of these states can be found in Section 5.15.3
  194. * of PSCI specification (ARM DEN 0022C).
  195. */
  196. #define HW_ON 0
  197. #define HW_OFF 1
  198. #define HW_STANDBY 2
  199. /*
  200. * Macro to represent invalid affinity level within PSCI.
  201. */
  202. #define PSCI_INVALID_PWR_LVL (PLAT_MAX_PWR_LVL + U(1))
  203. /*
  204. * Type for representing the local power state at a particular level.
  205. */
  206. typedef uint8_t plat_local_state_t;
  207. /* The local state macro used to represent RUN state. */
  208. #define PSCI_LOCAL_STATE_RUN U(0)
  209. /*
  210. * Function to test whether the plat_local_state is RUN state
  211. */
  212. static inline int is_local_state_run(unsigned int plat_local_state)
  213. {
  214. return (plat_local_state == PSCI_LOCAL_STATE_RUN) ? 1 : 0;
  215. }
  216. /*
  217. * Function to test whether the plat_local_state is RETENTION state
  218. */
  219. static inline int is_local_state_retn(unsigned int plat_local_state)
  220. {
  221. return ((plat_local_state > PSCI_LOCAL_STATE_RUN) &&
  222. (plat_local_state <= PLAT_MAX_RET_STATE)) ? 1 : 0;
  223. }
  224. /*
  225. * Function to test whether the plat_local_state is OFF state
  226. */
  227. static inline int is_local_state_off(unsigned int plat_local_state)
  228. {
  229. return ((plat_local_state > PLAT_MAX_RET_STATE) &&
  230. (plat_local_state <= PLAT_MAX_OFF_STATE)) ? 1 : 0;
  231. }
  232. /*****************************************************************************
  233. * This data structure defines the representation of the power state parameter
  234. * for its exchange between the generic PSCI code and the platform port. For
  235. * example, it is used by the platform port to specify the requested power
  236. * states during a power management operation. It is used by the generic code to
  237. * inform the platform about the target power states that each level should
  238. * enter.
  239. ****************************************************************************/
  240. typedef struct psci_power_state {
  241. /*
  242. * The pwr_domain_state[] stores the local power state at each level
  243. * for the CPU.
  244. */
  245. plat_local_state_t pwr_domain_state[PLAT_MAX_PWR_LVL + U(1)];
  246. #if PSCI_OS_INIT_MODE
  247. /*
  248. * The highest power level at which the current CPU is the last running
  249. * CPU.
  250. */
  251. unsigned int last_at_pwrlvl;
  252. #endif
  253. } psci_power_state_t;
  254. /*******************************************************************************
  255. * Structure used to store per-cpu information relevant to the PSCI service.
  256. * It is populated in the per-cpu data array. In return we get a guarantee that
  257. * this information will not reside on a cache line shared with another cpu.
  258. ******************************************************************************/
  259. typedef struct psci_cpu_data {
  260. /* State as seen by PSCI Affinity Info API */
  261. aff_info_state_t aff_info_state;
  262. /*
  263. * Highest power level which takes part in a power management
  264. * operation.
  265. */
  266. unsigned int target_pwrlvl;
  267. /* The local power state of this CPU */
  268. plat_local_state_t local_state;
  269. } psci_cpu_data_t;
  270. /*******************************************************************************
  271. * Structure populated by platform specific code to export routines which
  272. * perform common low level power management functions
  273. ******************************************************************************/
  274. typedef struct plat_psci_ops {
  275. void (*cpu_standby)(plat_local_state_t cpu_state);
  276. int (*pwr_domain_on)(u_register_t mpidr);
  277. void (*pwr_domain_off)(const psci_power_state_t *target_state);
  278. int (*pwr_domain_off_early)(const psci_power_state_t *target_state);
  279. #if PSCI_OS_INIT_MODE
  280. int (*pwr_domain_validate_suspend)(
  281. const psci_power_state_t *target_state);
  282. #endif
  283. void (*pwr_domain_suspend_pwrdown_early)(
  284. const psci_power_state_t *target_state);
  285. void (*pwr_domain_suspend)(const psci_power_state_t *target_state);
  286. void (*pwr_domain_on_finish)(const psci_power_state_t *target_state);
  287. void (*pwr_domain_on_finish_late)(
  288. const psci_power_state_t *target_state);
  289. void (*pwr_domain_suspend_finish)(
  290. const psci_power_state_t *target_state);
  291. void __dead2 (*pwr_domain_pwr_down_wfi)(
  292. const psci_power_state_t *target_state);
  293. void __dead2 (*system_off)(void);
  294. void __dead2 (*system_reset)(void);
  295. int (*validate_power_state)(unsigned int power_state,
  296. psci_power_state_t *req_state);
  297. int (*validate_ns_entrypoint)(uintptr_t ns_entrypoint);
  298. void (*get_sys_suspend_power_state)(
  299. psci_power_state_t *req_state);
  300. int (*get_pwr_lvl_state_idx)(plat_local_state_t pwr_domain_state,
  301. int pwrlvl);
  302. int (*translate_power_state_by_mpidr)(u_register_t mpidr,
  303. unsigned int power_state,
  304. psci_power_state_t *output_state);
  305. int (*get_node_hw_state)(u_register_t mpidr, unsigned int power_level);
  306. int (*mem_protect_chk)(uintptr_t base, u_register_t length);
  307. int (*read_mem_protect)(int *val);
  308. int (*write_mem_protect)(int val);
  309. int (*system_reset2)(int is_vendor,
  310. int reset_type, u_register_t cookie);
  311. } plat_psci_ops_t;
  312. /*******************************************************************************
  313. * Function & Data prototypes
  314. ******************************************************************************/
  315. unsigned int psci_version(void);
  316. int psci_cpu_on(u_register_t target_cpu,
  317. uintptr_t entrypoint,
  318. u_register_t context_id);
  319. int psci_cpu_suspend(unsigned int power_state,
  320. uintptr_t entrypoint,
  321. u_register_t context_id);
  322. int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id);
  323. int psci_cpu_off(void);
  324. int psci_affinity_info(u_register_t target_affinity,
  325. unsigned int lowest_affinity_level);
  326. int psci_migrate(u_register_t target_cpu);
  327. int psci_migrate_info_type(void);
  328. u_register_t psci_migrate_info_up_cpu(void);
  329. int psci_node_hw_state(u_register_t target_cpu,
  330. unsigned int power_level);
  331. int psci_features(unsigned int psci_fid);
  332. #if PSCI_OS_INIT_MODE
  333. int psci_set_suspend_mode(unsigned int mode);
  334. #endif
  335. void __dead2 psci_power_down_wfi(void);
  336. void psci_arch_setup(void);
  337. #endif /*__ASSEMBLER__*/
  338. #endif /* PSCI_H */