psci.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-09-09 GuEe-GUI The first version
  9. */
  10. #include <psci.h>
  11. #include <smccc.h>
  12. #include <armv8.h>
  13. typedef uint64_t (*psci_call_handle)(uint32_t fn, uint64_t arg0, uint64_t arg1, uint64_t arg2);
  14. static uint64_t psci_smc_call(uint32_t fn, uint64_t arg0, uint64_t arg1, uint64_t arg2)
  15. {
  16. return arm_smc_call(fn, arg0, arg1, arg2, 0, 0, 0, 0).x0;
  17. }
  18. static uint64_t psci_hvc_call(uint32_t fn, uint64_t arg0, uint64_t arg1, uint64_t arg2)
  19. {
  20. return arm_hvc_call(fn, arg0, arg1, arg2, 0, 0, 0, 0).x0;
  21. }
  22. static psci_call_handle psci_call = psci_smc_call;
  23. static uint64_t shutdown_args[3] = {0, 0, 0};
  24. static uint64_t reboot_args[3] = {0, 0, 0};
  25. void arm_psci_init(uint64_t *platform_shutdown_args, uint64_t *platform_reboot_args)
  26. {
  27. if (rt_hw_get_current_el() < 2)
  28. {
  29. psci_call = psci_hvc_call;
  30. }
  31. if (platform_shutdown_args != RT_NULL)
  32. {
  33. shutdown_args[0] = platform_shutdown_args[0];
  34. shutdown_args[1] = platform_shutdown_args[1];
  35. shutdown_args[2] = platform_shutdown_args[2];
  36. }
  37. if (platform_reboot_args != RT_NULL)
  38. {
  39. reboot_args[0] = platform_reboot_args[0];
  40. reboot_args[1] = platform_reboot_args[1];
  41. reboot_args[2] = platform_reboot_args[2];
  42. }
  43. }
  44. uint32_t arm_psci_get_version()
  45. {
  46. return (uint32_t)psci_call(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
  47. }
  48. uint32_t arm_psci_get_affinity_info(uint64_t target_affinity, uint64_t lowest_affinity_level)
  49. {
  50. return (uint32_t)psci_call(PSCI_0_2_FN_AFFINITY_INFO, target_affinity, lowest_affinity_level, 0);
  51. }
  52. uint32_t arm_psci_get_feature(uint32_t psci_func_id)
  53. {
  54. return (uint32_t)psci_call(PSCI_1_0_FN_PSCI_FEATURES, psci_func_id, 0, 0);
  55. }
  56. uint32_t arm_psci_cpu_off(uint64_t state)
  57. {
  58. return (uint32_t)psci_call(PSCI_0_2_FN_CPU_OFF, state, 0, 0);
  59. }
  60. uint32_t arm_psci_cpu_on(uint64_t mpid, uint64_t entry)
  61. {
  62. /* [40:63] and [24:31] must be zero, other is aff3, aff2, aff1, aff0 */
  63. mpid = mpid & 0xff00ffffff;
  64. return (uint32_t)psci_call(PSCI_0_2_FN_CPU_ON, mpid, entry, 0);
  65. }
  66. uint32_t arm_psci_cpu_suspend(uint32_t power_state, uint64_t entry)
  67. {
  68. return (uint32_t)psci_call(PSCI_0_2_FN_CPU_SUSPEND, power_state, entry, 0);
  69. }
  70. void arm_psci_system_off()
  71. {
  72. psci_call(PSCI_0_2_FN_SYSTEM_OFF, shutdown_args[0], shutdown_args[1], shutdown_args[2]);
  73. }
  74. void arm_psci_system_reboot()
  75. {
  76. psci_call(PSCI_0_2_FN_SYSTEM_RESET, reboot_args[0], reboot_args[1], reboot_args[2]);
  77. }