psci.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <rthw.h>
  10. #include <rtthread.h>
  11. #include <stdint.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "cpu.h"
  15. #include "psci.h"
  16. #include "psci_api.h"
  17. #include "smccc.h"
  18. #define DBG_TAG "libcpu.aarch64.psci"
  19. #define DBG_LVL DBG_INFO
  20. #include <rtdbg.h>
  21. /** template for creating 4 PSCI ops: SUSPEND, OFF, ON, MIGRATE */
  22. #define COMMON_PSCI_OPS_TEMPLATE(VER, SUSPEND, OFF, ON, MIGRATE) \
  23. static int psci_##VER##_cpu_suspend(uint32_t state, unsigned long entry_point) \
  24. { \
  25. return psci_call((SUSPEND), state, entry_point, 0); \
  26. } \
  27. static int psci_##VER##_cpu_off(uint32_t state) \
  28. { \
  29. return psci_call((OFF), state, 0, 0); \
  30. } \
  31. static int psci_##VER##_cpu_on(unsigned long cpuid, unsigned long entry_point) \
  32. { \
  33. return psci_call((ON), cpuid, entry_point, 0); \
  34. } \
  35. static int psci_##VER##_migrate(unsigned long cpuid) \
  36. { \
  37. return psci_call((MIGRATE), cpuid, 0, 0); \
  38. }
  39. #ifdef RT_USING_FDT
  40. #include "dtb_node.h"
  41. struct psci_ops_t psci_ops;
  42. #if __SIZE_WIDTH__ == 64
  43. #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN64_##name
  44. #else
  45. #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN_##name
  46. #endif
  47. /**
  48. * SMCCC can use either smc or hvc method
  49. * smccc_call will be init to proper interface when psci_init() was executed
  50. */
  51. static void (*smccc_call)(unsigned long a0, unsigned long a1, unsigned long a2,
  52. unsigned long a3, unsigned long a4, unsigned long a5,
  53. unsigned long a6, unsigned long a7, struct arm_smccc_res_t *res,
  54. struct arm_smccc_quirk_t *quirk);
  55. static rt_uint32_t psci_call(unsigned long a0, unsigned long a1, unsigned long a2, unsigned long a3)
  56. {
  57. struct arm_smccc_res_t res;
  58. smccc_call(a0, a1, a2, a3, 0, 0, 0, 0, &res, (void *)0);
  59. return res.a0;
  60. }
  61. static int _psci_probe_version(char *version, int *major, int *minor);
  62. static int _psci_init_with_version(int major, int minor);
  63. static struct dtb_node *psci_node;
  64. static int psci_ver_major;
  65. static int psci_ver_minor;
  66. /**
  67. * @brief init psci operations.
  68. * using device tree to probe version and psci-method,
  69. * setup psci ops for future use
  70. *
  71. * @return int 0 on success
  72. */
  73. int psci_init()
  74. {
  75. void *root = get_dtb_node_head();
  76. psci_node = dtb_node_get_dtb_node_by_path(root, "/psci");
  77. if (!psci_node)
  78. {
  79. LOG_E("No PSCI node found");
  80. return -1;
  81. }
  82. char *compatible = dtb_node_get_dtb_node_property_value(psci_node, "compatible", NULL);
  83. char *method = dtb_node_get_dtb_node_property_value(psci_node, "method", NULL);
  84. int retval = 0;
  85. // setup psci-method
  86. if (!strcmp("hvc", method))
  87. {
  88. smccc_call = arm_smccc_hvc;
  89. }
  90. else if (!strcmp("smc", method))
  91. {
  92. smccc_call = arm_smccc_smc;
  93. }
  94. else
  95. {
  96. LOG_E("Unknown PSCI method: %s", method);
  97. return -1;
  98. }
  99. LOG_D("Using psci method %s", method);
  100. retval = _psci_probe_version(compatible, &psci_ver_major, &psci_ver_minor);
  101. if (retval != 0)
  102. return retval;
  103. // init psci_ops with specified psci version
  104. retval = _psci_init_with_version(psci_ver_major, psci_ver_minor);
  105. return retval;
  106. }
  107. /* function id of PSCI v0.1 should be probed in FDT, they are implementation defined value */
  108. static rt_uint32_t cpu_suspend_0_1;
  109. static rt_uint32_t cpu_off_0_1;
  110. static rt_uint32_t cpu_on_0_1;
  111. static rt_uint32_t migrate_0_1;
  112. /* basic operations TEMPLATE for API since 0.1 version */
  113. COMMON_PSCI_OPS_TEMPLATE(0_1, cpu_suspend_0_1, cpu_off_0_1, cpu_on_0_1, migrate_0_1);
  114. /* used for v0.1 only, rely on FDT to probe function id */
  115. #define PROBE_AND_SET(FUNC_NAME) \
  116. do \
  117. { \
  118. int num_of_elem; \
  119. funcid = \
  120. dtb_node_get_dtb_node_property_value(psci_node, #FUNC_NAME, &num_of_elem); \
  121. if (num_of_elem != 4 || funcid == 0 || *funcid == 0) \
  122. { \
  123. LOG_E("Failed to probe " #FUNC_NAME " in FDT"); \
  124. } \
  125. else \
  126. { \
  127. FUNC_NAME##_0_1 = (rt_uint32_t)fdt32_to_cpu(*funcid); \
  128. psci_ops.FUNC_NAME = psci_0_1_##FUNC_NAME; \
  129. } \
  130. } while (0)
  131. static int psci_0_1_init()
  132. {
  133. // reading function id from fdt
  134. rt_uint32_t *funcid;
  135. PROBE_AND_SET(cpu_suspend);
  136. PROBE_AND_SET(cpu_off);
  137. PROBE_AND_SET(cpu_on);
  138. PROBE_AND_SET(migrate);
  139. return 0;
  140. }
  141. COMMON_PSCI_OPS_TEMPLATE(0_2, PSCI_FN_NATIVE(0_2, CPU_SUSPEND), PSCI_0_2_FN_CPU_OFF, PSCI_FN_NATIVE(0_2, CPU_ON), PSCI_FN_NATIVE(0_2, MIGRATE));
  142. static rt_uint32_t psci_0_2_get_version(void)
  143. {
  144. return psci_call(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
  145. }
  146. static void psci_0_2_set_basic_ops()
  147. {
  148. psci_ops = (struct psci_ops_t){
  149. .get_version = psci_0_2_get_version,
  150. // followings API are v0.1 compatible
  151. .cpu_suspend = psci_0_2_cpu_suspend,
  152. .cpu_off = psci_0_2_cpu_off,
  153. .cpu_on = psci_0_2_cpu_on,
  154. .migrate = psci_0_2_migrate,
  155. };
  156. }
  157. static void psci_0_2_system_off(void)
  158. {
  159. psci_call(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  160. }
  161. static void psci_0_2_system_reset(void)
  162. {
  163. psci_call(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  164. }
  165. static int psci_0_2_init()
  166. {
  167. psci_0_2_set_basic_ops();
  168. // TODO init other version 0.2 features...
  169. // psci system off and reset which controlling machine
  170. psci_ops.system_off = psci_0_2_system_off;
  171. psci_ops.system_reset = psci_0_2_system_reset;
  172. system_off = psci_0_2_system_off;
  173. return 0;
  174. }
  175. /* PSCI v1.0 & after */
  176. static int psci_1_0_features(uint32_t psci_func_id)
  177. {
  178. return psci_call(PSCI_1_0_FN_PSCI_FEATURES,
  179. psci_func_id, 0, 0);
  180. }
  181. static int psci_1_0_init()
  182. {
  183. psci_0_2_init();
  184. // TODO init other version 1.0 features...
  185. // remove unsupported features
  186. if (psci_1_0_features(PSCI_0_2_FN_SYSTEM_OFF) == PSCI_RET_NOT_SUPPORTED)
  187. {
  188. psci_ops.system_off = RT_NULL;
  189. system_off = RT_NULL;
  190. }
  191. else
  192. LOG_D("Using SYSTEM OFF feature");
  193. if (psci_1_0_features(PSCI_0_2_FN_SYSTEM_RESET) == PSCI_RET_NOT_SUPPORTED)
  194. psci_ops.system_reset = RT_NULL;
  195. else
  196. LOG_D("Using SYSTEM RESET feature");
  197. return 0;
  198. }
  199. /* probe psci version from fdt or SMC call */
  200. static int _psci_probe_version(char *version, int *major, int *minor)
  201. {
  202. int retval = 0;
  203. // if strcmp compatible 'arm,psci-0.1'
  204. if (!strcmp(version, "arm,psci"))
  205. {
  206. *major = 0;
  207. *minor = 1;
  208. }
  209. else if (!strncmp(version, "arm,psci-", 8))
  210. {
  211. // since psci-0.2, using psci call to probe version
  212. rt_uint32_t ret = psci_0_2_get_version();
  213. *major = PSCI_VERSION_MAJOR(ret);
  214. *minor = PSCI_VERSION_MINOR(ret);
  215. }
  216. else
  217. {
  218. LOG_E("[%s] was not a proper PSCI version", version);
  219. retval = -1;
  220. }
  221. LOG_D("Using PSCI v%d.%d", *major, *minor);
  222. return retval;
  223. }
  224. /* init psci ops with version info */
  225. static int _psci_init_with_version(int major, int minor)
  226. {
  227. int retval = -0xbeef; // mark unsupported
  228. if (major == 0)
  229. {
  230. // for v0.1, psci function id was provided fdt
  231. if (minor == 1)
  232. {
  233. retval = psci_0_1_init();
  234. }
  235. else if (minor == 2)
  236. {
  237. retval = psci_0_2_init();
  238. }
  239. }
  240. else if (major == 1)
  241. {
  242. // psci_1_0_init is a base setup for version after v1.0
  243. retval = psci_1_0_init();
  244. }
  245. if (retval == -0xbeef)
  246. {
  247. LOG_E("PSCI init with incompatible version %d.%d", major, minor);
  248. }
  249. return retval;
  250. }
  251. #endif /* RT_USING_FDT */