psci.c 9.3 KB

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