fpsci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright : (C) 2023 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fpsci.c
  15. * Created Date: 2023-06-21 10:36:53
  16. * Last Modified: 2023-06-30 13:32:06
  17. * Description: This file is for
  18. *
  19. * Modify History:
  20. * Ver Who Date Changes
  21. * ----- ---------- -------- ---------------------------------
  22. * 1.0 huanghe 2023-06-21 first release
  23. */
  24. #include <stdio.h>
  25. #include "fsmcc.h" /* 根据你的平台和编译环境来确定这个路径 */
  26. #include "fpsci.h"
  27. #include "fassert.h"
  28. #include "fcompiler.h"
  29. #include "fcpu_info.h"
  30. #include "rtdbg.h"
  31. /* 定义PSCI 函数值 */
  32. #define FPSCI_0_2_FN32_BASE 0x84000000
  33. #define FPSCI_0_2_FN64_BASE 0xC4000000
  34. #define FPSCI_VERSION (FPSCI_0_2_FN32_BASE + 0x000)
  35. #define FPSCI_FEATURES (FPSCI_0_2_FN32_BASE + 0x00a)
  36. #define FPSCI_CPU_SUSPEND_AARCH32 (FPSCI_0_2_FN32_BASE + 0x001)
  37. #define FPSCI_CPU_SUSPEND_AARCH64 (FPSCI_0_2_FN64_BASE + 0x001)
  38. #define FPSCI_CPU_OFF (FPSCI_0_2_FN32_BASE + 0x002)
  39. #define FPSCI_CPU_ON_AARCH32 (FPSCI_0_2_FN32_BASE + 0x003)
  40. #define FPSCI_CPU_ON_AARCH64 (FPSCI_0_2_FN64_BASE + 0x003)
  41. #define FPSCI_FAFFINITY_INFO_AARCH32 (FPSCI_0_2_FN32_BASE + 0x004)
  42. #define FPSCI_FAFFINITY_INFO_AARCH64 (FPSCI_0_2_FN64_BASE + 0x004)
  43. #define FPSCI_SYSTEM_OFF (FPSCI_0_2_FN32_BASE + 0x008)
  44. #define FPSCI_SYSTEM_RESET (FPSCI_0_2_FN32_BASE + 0x009)
  45. #define FPSCI_SYSTEM_SUSPEND (FPSCI_0_2_FN32_BASE + 0x00E)
  46. /* 定义每个PSCI函数ID的位标记 */
  47. #define FPSCI_PSCI_VERSION_BIT (1 << 0)
  48. #define FPSCI_PSCI_FEATURES_BIT (1 << 1)
  49. #define FPSCI_CPU_SUSPEND_AARCH32_BIT (1 << 2)
  50. #define FPSCI_CPU_SUSPEND_AARCH64_BIT (1 << 3)
  51. #define FPSCI_CPU_OFF_BIT (1 << 4)
  52. #define FPSCI_CPU_ON_AARCH32_BIT (1 << 5)
  53. #define FPSCI_CPU_ON_AARCH64_BIT (1 << 6)
  54. #define FPSCI_AFFINITY_INFO_AARCH32_BIT (1 << 7)
  55. #define FPSCI_AFFINITY_INFO_AARCH64_BIT (1 << 8)
  56. #define FPSCI_SYSTEM_OFF_BIT (1 << 9)
  57. #define FPSCI_SYSTEM_RESET_BIT (1 << 10)
  58. static int fpsci_ringt_bit_flg = 0;
  59. /* 定义函数指针 */
  60. typedef void (*FPsciInvokeFun)(unsigned long arg0, unsigned long arg1,
  61. unsigned long arg2, unsigned long arg3,
  62. unsigned long arg4, unsigned long arg5,
  63. unsigned long arg6, unsigned long arg7,
  64. struct FSmcccRes *res);
  65. /* 为函数指针初始化为默认的函数 */
  66. FPsciInvokeFun f_psci_invoke = FSmcccSmcCall;
  67. /**
  68. * @name: FPsciVersion
  69. * @msg: Get the version of the PSCI implementation.
  70. * @return {int}: The version information of the PSCI implementation.
  71. * @note: This function returns the version information obtained from the PSCI VERSION function.
  72. */
  73. int FPsciVersion(void)
  74. {
  75. struct FSmcccRes res;
  76. FASSERT((*f_psci_invoke));
  77. (*f_psci_invoke)(FPSCI_VERSION, 0, 0, 0, 0, 0, 0, 0, &res);
  78. return res.a0;
  79. }
  80. /**
  81. * @name: FPsciFeatures
  82. * @msg: Check whether a PSCI function is supported.
  83. * @param {u32} psci_fid: The function ID of the PSCI function to be checked.
  84. * @return {int}: 1 if the function is supported; 0 otherwise.
  85. * @note: This function returns whether the PSCI function represented by psci_fid is supported or not.
  86. */
  87. int FPsciFeatures(u32 psci_fid)
  88. {
  89. struct FSmcccRes res;
  90. FASSERT((*f_psci_invoke));
  91. (*f_psci_invoke)(FPSCI_FEATURES, psci_fid, 0, 0, 0, 0, 0, 0, &res);
  92. return res.a0 == FPSCI_SUCCESS ? 1 : 0;
  93. }
  94. /**
  95. * @name: FPsciCpuSuspend
  96. * @msg: Suspend execution on a particular CPU.
  97. * @param {u32} power_state: The power state to be entered.
  98. * @param {unsigned long} entry_point_address: The address to be executed upon waking up.
  99. * @param {unsigned long} context_id: The context-specific identifier.
  100. * @return {int}: The status code of the operation, as defined by the PSCI specification.
  101. * @note: This function suspends the execution on a particular CPU and returns a status code indicating whether the operation was successful or not.
  102. */
  103. int FPsciCpuSuspend(u32 power_state, unsigned long entry_point_address, unsigned long context_id)
  104. {
  105. struct FSmcccRes res;
  106. FASSERT((fpsci_ringt_bit_flg & FPSCI_CPU_SUSPEND_AARCH32_BIT) != 0);
  107. FASSERT((*f_psci_invoke));
  108. (*f_psci_invoke)(FPSCI_CPU_SUSPEND_AARCH32, power_state, entry_point_address, context_id, 0, 0, 0, 0, &res);
  109. return res.a0;
  110. }
  111. /**
  112. * @name: FPsciCpuOn
  113. * @msg: Power on a particular CPU.
  114. * @param {unsigned long} target_cpu: The target CPU to be powered on.
  115. * @param {unsigned long} entry_point_address: The address to be executed upon waking up.
  116. * @param {unsigned long} context_id: The context-specific identifier.
  117. * @return {int}: The status code of the operation, as defined by the PSCI specification.
  118. * @note: This function powers on a particular CPU and returns a status code indicating whether the operation was successful or not.
  119. */
  120. int FPsciCpuOn(unsigned long target_cpu, unsigned long entry_point_address, unsigned long context_id)
  121. {
  122. struct FSmcccRes res;
  123. unsigned long cpu_on_id ;
  124. #if defined(FAARCH64_USE)
  125. cpu_on_id = FPSCI_CPU_ON_AARCH64 ;
  126. #else
  127. cpu_on_id = FPSCI_CPU_ON_AARCH32;
  128. #endif
  129. FASSERT((fpsci_ringt_bit_flg & (FPSCI_CPU_ON_AARCH32_BIT | FPSCI_CPU_ON_AARCH64_BIT)) != 0);
  130. FASSERT((*f_psci_invoke));
  131. (*f_psci_invoke)(cpu_on_id, target_cpu, entry_point_address, context_id, 0, 0, 0, 0, &res);
  132. return res.a0;
  133. }
  134. /**
  135. * @name: FPsciCpuOff
  136. * @msg: This is a wrapper for the PSCI CPU Off interface, intended to turn off the current CPU.
  137. * @return: Returns the 'a0' field of the 'FSmcccRes' structure, indicating the result of the call. A return value of 0 (PSCI_SUCCESS) indicates success, any other value indicates an error occurred.
  138. * @note: A core that is powered down by CPU_OFF can only be powered up again in response to a CPU_ON.
  139. */
  140. int FPsciCpuOff(void)
  141. {
  142. struct FSmcccRes res;
  143. FASSERT((fpsci_ringt_bit_flg & FPSCI_CPU_OFF_BIT) != 0);
  144. FASSERT((*f_psci_invoke));
  145. (*f_psci_invoke)(FPSCI_CPU_OFF, 0, 0, 0, 0, 0, 0, 0, &res);
  146. return res.a0;
  147. }
  148. /**
  149. * @name: FPsciAffinityInfo
  150. * @msg: Get the power state of a particular affinity level.
  151. * @param {unsigned long} target_affinity: The target affinity level.
  152. * @param {u32} lowest_affinity_level: The lowest affinity level.
  153. * @return {int}: The power state of the specified affinity level, as defined by the PSCI specification.
  154. * @note: This function returns the power state of a particular affinity level.
  155. */
  156. int FPsciAffinityInfo(unsigned long target_affinity, u32 lowest_affinity_level)
  157. {
  158. struct FSmcccRes res;
  159. FASSERT((fpsci_ringt_bit_flg & (FPSCI_AFFINITY_INFO_AARCH32_BIT | FPSCI_AFFINITY_INFO_AARCH64_BIT)) != 0);
  160. FASSERT((*f_psci_invoke));
  161. unsigned long cpu_on_id ;
  162. #if defined(FAARCH64_USE)
  163. cpu_on_id = FPSCI_CPU_ON_AARCH64 ;
  164. #else
  165. cpu_on_id = FPSCI_CPU_ON_AARCH32;
  166. #endif
  167. (*f_psci_invoke)(FPSCI_FAFFINITY_INFO_AARCH32, target_affinity, lowest_affinity_level, 0, 0, 0, 0, 0, &res);
  168. return res.a0;
  169. }
  170. /**
  171. * @name: FPsciSystemReset
  172. * @msg: Reset the system.
  173. * @param {u32} reset_type: The type of the system reset (cold/warm).
  174. * @note: This function resets the system. The reset type is specified by the parameter reset_type.
  175. */
  176. void FPsciSystemReset(u32 reset_type)
  177. {
  178. struct FSmcccRes res;
  179. FASSERT((fpsci_ringt_bit_flg & FPSCI_SYSTEM_RESET_BIT) != 0);
  180. FASSERT((*f_psci_invoke));
  181. (*f_psci_invoke)(FPSCI_SYSTEM_RESET, reset_type, 0, 0, 0, 0, 0, 0, &res);
  182. }
  183. /**
  184. * @name: FPsciCheckFeatures
  185. * @msg: This function checks for the availability of various PSCI features and sets the corresponding bits in the 'fpsci_ringt_bit_flg' global flag accordingly.
  186. * @return: This function does not return a value.
  187. */
  188. static void FPsciCheckFeatures(void)
  189. {
  190. LOG_I("Checking PSCI features...\r\n");
  191. fpsci_ringt_bit_flg = 0 ;
  192. if (FPsciFeatures(FPSCI_CPU_SUSPEND_AARCH32))
  193. {
  194. fpsci_ringt_bit_flg |= FPSCI_CPU_SUSPEND_AARCH32_BIT;
  195. LOG_I("CPU_SUSPEND_AARCH32 supported.\r\n");
  196. }
  197. else
  198. {
  199. LOG_E("CPU_SUSPEND_AARCH32 not supported.\r\n");
  200. }
  201. if (FPsciFeatures(FPSCI_CPU_OFF))
  202. {
  203. fpsci_ringt_bit_flg |= FPSCI_CPU_OFF_BIT;
  204. LOG_I("CPU_OFF supported.\r\n");
  205. }
  206. else
  207. {
  208. LOG_E("CPU_OFF not supported.\r\n");
  209. }
  210. #if defined(FAARCH64_USE)
  211. if (FPsciFeatures(FPSCI_CPU_ON_AARCH64))
  212. {
  213. fpsci_ringt_bit_flg |= FPSCI_CPU_ON_AARCH64_BIT;
  214. LOG_I("CPU_ON_AARCH64 supported.\r\n");
  215. }
  216. else
  217. {
  218. LOG_E("CPU_ON_AARCH64 not supported.\r\n");
  219. }
  220. #else
  221. if (FPsciFeatures(FPSCI_CPU_ON_AARCH32))
  222. {
  223. fpsci_ringt_bit_flg |= FPSCI_CPU_ON_AARCH32_BIT;
  224. LOG_I("CPU_ON_AARCH32 supported.\r\n");
  225. }
  226. else
  227. {
  228. LOG_E("CPU_ON_AARCH32 not supported.\r\n");
  229. }
  230. #endif
  231. #if defined(FAARCH64_USE)
  232. if (FPsciFeatures(FPSCI_FAFFINITY_INFO_AARCH64))
  233. {
  234. fpsci_ringt_bit_flg |= FPSCI_AFFINITY_INFO_AARCH64_BIT;
  235. LOG_I("AFFINITY_INFO_AARCH64 supported.\r\n");
  236. }
  237. else
  238. {
  239. LOG_E("AFFINITY_INFO_AARCH64 not supported.\r\n");
  240. }
  241. #else
  242. if (FPsciFeatures(FPSCI_FAFFINITY_INFO_AARCH32))
  243. {
  244. fpsci_ringt_bit_flg |= FPSCI_AFFINITY_INFO_AARCH32_BIT;
  245. LOG_I("FPSCI_AFFINITY_INFO_AARCH32 supported.\r\n");
  246. }
  247. else
  248. {
  249. LOG_E("AFFINITY_INFO_AARCH32 not supported.\r\n");
  250. }
  251. #endif
  252. if (FPsciFeatures(FPSCI_SYSTEM_OFF))
  253. {
  254. fpsci_ringt_bit_flg |= FPSCI_SYSTEM_OFF_BIT;
  255. LOG_I("SYSTEM_OFF supported.\r\n");
  256. }
  257. else
  258. {
  259. LOG_E("SYSTEM_OFF not supported.\r\n");
  260. }
  261. if (FPsciFeatures(FPSCI_SYSTEM_RESET))
  262. {
  263. fpsci_ringt_bit_flg |= FPSCI_SYSTEM_RESET_BIT;
  264. LOG_I("SYSTEM_RESET supported.\r\n");
  265. }
  266. else
  267. {
  268. LOG_E("SYSTEM_RESET not supported.\r\n");
  269. }
  270. }
  271. /**
  272. * @name: FPsci_CpuOn
  273. * @msg: Power up a core
  274. * @in param cpu_id_mask: cpu id mask
  275. * @in param bootaddr: a 32-bit entry point physical address (or IPA).
  276. * @return int
  277. */
  278. int FPsciCpuMaskOn(s32 cpu_id_mask, uintptr bootaddr)
  279. {
  280. FError ret ;
  281. u64 cluster = 0;
  282. ret = GetCpuAffinityByMask(cpu_id_mask, &cluster);
  283. if (ret != ERR_SUCCESS)
  284. {
  285. return FPSCI_INVALID_PARAMS;
  286. }
  287. return FPsciCpuOn(cluster, (unsigned long)bootaddr, 0) ;
  288. }
  289. static void FSmccInit(int method)
  290. {
  291. if (method == 1)
  292. {
  293. f_psci_invoke = FSmcccHvcCall;
  294. }
  295. else
  296. {
  297. f_psci_invoke = FSmcccSmcCall;
  298. }
  299. }
  300. int FPsciInit(void)
  301. {
  302. int psci_version = 0;
  303. FSmccInit(0);
  304. psci_version = FPsciVersion() ;
  305. LOG_I("major is 0x%x,minor is 0x%x \r\n", FPSCI_MAJOR_VERSION(psci_version), FPSCI_MINOR_VERSION(psci_version)) ;
  306. FPsciCheckFeatures();
  307. return 0;
  308. }