psci.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Masahiro Yamada <yamada.masahiro@socionext.com>
  4. *
  5. * Based on drivers/firmware/psci.c from Linux:
  6. * Copyright (C) 2015 ARM Limited
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <dm.h>
  11. #include <irq_func.h>
  12. #include <log.h>
  13. #include <dm/lists.h>
  14. #include <efi_loader.h>
  15. #include <sysreset.h>
  16. #include <linux/delay.h>
  17. #include <linux/libfdt.h>
  18. #include <linux/arm-smccc.h>
  19. #include <linux/errno.h>
  20. #include <linux/printk.h>
  21. #include <linux/psci.h>
  22. #include <asm/system.h>
  23. #define DRIVER_NAME "psci"
  24. #define PSCI_METHOD_HVC 1
  25. #define PSCI_METHOD_SMC 2
  26. /*
  27. * While a 64-bit OS can make calls with SMC32 calling conventions, for some
  28. * calls it is necessary to use SMC64 to pass or return 64-bit values.
  29. * For such calls PSCI_FN_NATIVE(version, name) will choose the appropriate
  30. * (native-width) function ID.
  31. */
  32. #if defined(CONFIG_ARM64)
  33. #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN64_##name
  34. #else
  35. #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN_##name
  36. #endif
  37. #if CONFIG_IS_ENABLED(EFI_LOADER)
  38. int __efi_runtime_data psci_method;
  39. #else
  40. int psci_method __section(".data");
  41. #endif
  42. unsigned long __efi_runtime invoke_psci_fn
  43. (unsigned long function_id, unsigned long arg0,
  44. unsigned long arg1, unsigned long arg2)
  45. {
  46. struct arm_smccc_res res;
  47. /*
  48. * In the __efi_runtime we need to avoid the switch statement. In some
  49. * cases the compiler creates lookup tables to implement switch. These
  50. * tables are not correctly relocated when SetVirtualAddressMap is
  51. * called.
  52. */
  53. if (psci_method == PSCI_METHOD_SMC)
  54. arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
  55. else if (psci_method == PSCI_METHOD_HVC)
  56. arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
  57. else
  58. res.a0 = PSCI_RET_DISABLED;
  59. return res.a0;
  60. }
  61. static int request_psci_features(u32 psci_func_id)
  62. {
  63. return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
  64. psci_func_id, 0, 0);
  65. }
  66. static u32 psci_0_2_get_version(void)
  67. {
  68. return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
  69. }
  70. static bool psci_is_system_reset2_supported(void)
  71. {
  72. int ret;
  73. u32 ver;
  74. ver = psci_0_2_get_version();
  75. if (PSCI_VERSION_MAJOR(ver) >= 1) {
  76. ret = request_psci_features(PSCI_FN_NATIVE(1_1,
  77. SYSTEM_RESET2));
  78. if (ret != PSCI_RET_NOT_SUPPORTED)
  79. return true;
  80. }
  81. return false;
  82. }
  83. static int psci_bind(struct udevice *dev)
  84. {
  85. /* No SYSTEM_RESET support for PSCI 0.1 */
  86. if (device_is_compatible(dev, "arm,psci-0.2") ||
  87. device_is_compatible(dev, "arm,psci-1.0")) {
  88. int ret;
  89. /* bind psci-sysreset optionally */
  90. ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
  91. NULL);
  92. if (ret)
  93. pr_debug("PSCI System Reset was not bound.\n");
  94. }
  95. return 0;
  96. }
  97. static int psci_probe(struct udevice *dev)
  98. {
  99. const char *method;
  100. #if defined(CONFIG_ARM64)
  101. if (current_el() == 3)
  102. return -EINVAL;
  103. #endif
  104. method = ofnode_read_string(dev_ofnode(dev), "method");
  105. if (!method) {
  106. pr_warn("missing \"method\" property\n");
  107. return -ENXIO;
  108. }
  109. if (!strcmp("hvc", method)) {
  110. psci_method = PSCI_METHOD_HVC;
  111. } else if (!strcmp("smc", method)) {
  112. psci_method = PSCI_METHOD_SMC;
  113. } else {
  114. pr_warn("invalid \"method\" property: %s\n", method);
  115. return -EINVAL;
  116. }
  117. return 0;
  118. }
  119. /**
  120. * void do_psci_probe() - probe PSCI firmware driver
  121. *
  122. * Ensure that psci_method is initialized.
  123. */
  124. static void __maybe_unused do_psci_probe(void)
  125. {
  126. struct udevice *dev;
  127. uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
  128. }
  129. #if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
  130. efi_status_t efi_reset_system_init(void)
  131. {
  132. do_psci_probe();
  133. return EFI_SUCCESS;
  134. }
  135. void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type,
  136. efi_status_t reset_status,
  137. unsigned long data_size,
  138. void *reset_data)
  139. {
  140. if (reset_type == EFI_RESET_COLD ||
  141. reset_type == EFI_RESET_WARM ||
  142. reset_type == EFI_RESET_PLATFORM_SPECIFIC) {
  143. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  144. } else if (reset_type == EFI_RESET_SHUTDOWN) {
  145. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  146. }
  147. while (1)
  148. ;
  149. }
  150. #endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
  151. #ifdef CONFIG_PSCI_RESET
  152. void reset_misc(void)
  153. {
  154. do_psci_probe();
  155. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  156. }
  157. #endif /* CONFIG_PSCI_RESET */
  158. void psci_sys_reset(u32 type)
  159. {
  160. bool reset2_supported;
  161. do_psci_probe();
  162. reset2_supported = psci_is_system_reset2_supported();
  163. if (type == SYSRESET_WARM && reset2_supported) {
  164. /*
  165. * reset_type[31] = 0 (architectural)
  166. * reset_type[30:0] = 0 (SYSTEM_WARM_RESET)
  167. * cookie = 0 (ignored by the implementation)
  168. */
  169. invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0);
  170. } else {
  171. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  172. }
  173. }
  174. void psci_sys_poweroff(void)
  175. {
  176. do_psci_probe();
  177. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  178. }
  179. #ifdef CONFIG_CMD_POWEROFF
  180. int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  181. {
  182. do_psci_probe();
  183. puts("poweroff ...\n");
  184. udelay(50000); /* wait 50 ms */
  185. disable_interrupts();
  186. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  187. enable_interrupts();
  188. log_err("Power off not supported on this platform\n");
  189. return CMD_RET_FAILURE;
  190. }
  191. #endif
  192. static const struct udevice_id psci_of_match[] = {
  193. { .compatible = "arm,psci" },
  194. { .compatible = "arm,psci-0.2" },
  195. { .compatible = "arm,psci-1.0" },
  196. {},
  197. };
  198. U_BOOT_DRIVER(psci) = {
  199. .name = DRIVER_NAME,
  200. .id = UCLASS_FIRMWARE,
  201. .of_match = psci_of_match,
  202. .bind = psci_bind,
  203. .probe = psci_probe,
  204. };