psci.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 <efi_loader.h>
  12. #include <irq_func.h>
  13. #include <linker_lists.h>
  14. #include <log.h>
  15. #include <sysreset.h>
  16. #include <asm/system.h>
  17. #include <dm/device-internal.h>
  18. #include <dm/lists.h>
  19. #include <linux/arm-smccc.h>
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/libfdt.h>
  23. #include <linux/printk.h>
  24. #include <linux/psci.h>
  25. #define DRIVER_NAME "psci"
  26. #define PSCI_METHOD_HVC 1
  27. #define PSCI_METHOD_SMC 2
  28. /*
  29. * While a 64-bit OS can make calls with SMC32 calling conventions, for some
  30. * calls it is necessary to use SMC64 to pass or return 64-bit values.
  31. * For such calls PSCI_FN_NATIVE(version, name) will choose the appropriate
  32. * (native-width) function ID.
  33. */
  34. #if defined(CONFIG_ARM64)
  35. #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN64_##name
  36. #else
  37. #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN_##name
  38. #endif
  39. #if CONFIG_IS_ENABLED(EFI_LOADER)
  40. int __efi_runtime_data psci_method;
  41. #else
  42. int psci_method __section(".data");
  43. #endif
  44. unsigned long __efi_runtime invoke_psci_fn
  45. (unsigned long function_id, unsigned long arg0,
  46. unsigned long arg1, unsigned long arg2)
  47. {
  48. struct arm_smccc_res res;
  49. /*
  50. * In the __efi_runtime we need to avoid the switch statement. In some
  51. * cases the compiler creates lookup tables to implement switch. These
  52. * tables are not correctly relocated when SetVirtualAddressMap is
  53. * called.
  54. */
  55. if (psci_method == PSCI_METHOD_SMC)
  56. arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
  57. else if (psci_method == PSCI_METHOD_HVC)
  58. arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
  59. else
  60. res.a0 = PSCI_RET_DISABLED;
  61. return res.a0;
  62. }
  63. static int request_psci_features(u32 psci_func_id)
  64. {
  65. return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
  66. psci_func_id, 0, 0);
  67. }
  68. static u32 psci_0_2_get_version(void)
  69. {
  70. return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
  71. }
  72. static bool psci_is_system_reset2_supported(void)
  73. {
  74. int ret;
  75. u32 ver;
  76. ver = psci_0_2_get_version();
  77. if (PSCI_VERSION_MAJOR(ver) >= 1) {
  78. ret = request_psci_features(PSCI_FN_NATIVE(1_1,
  79. SYSTEM_RESET2));
  80. if (ret != PSCI_RET_NOT_SUPPORTED)
  81. return true;
  82. }
  83. return false;
  84. }
  85. static void smccc_invoke_hvc(unsigned long a0, unsigned long a1,
  86. unsigned long a2, unsigned long a3,
  87. unsigned long a4, unsigned long a5,
  88. unsigned long a6, unsigned long a7,
  89. struct arm_smccc_res *res)
  90. {
  91. arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
  92. }
  93. static void smccc_invoke_smc(unsigned long a0, unsigned long a1,
  94. unsigned long a2, unsigned long a3,
  95. unsigned long a4, unsigned long a5,
  96. unsigned long a6, unsigned long a7,
  97. struct arm_smccc_res *res)
  98. {
  99. arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
  100. }
  101. static int bind_smccc_features(struct udevice *dev, int psci_method)
  102. {
  103. struct psci_plat_data *pdata = dev_get_plat(dev);
  104. struct arm_smccc_feature *feature;
  105. size_t feature_cnt, n;
  106. if (!IS_ENABLED(CONFIG_ARM_SMCCC_FEATURES))
  107. return 0;
  108. /*
  109. * SMCCC features discovery invoke SMCCC standard function ID
  110. * ARM_SMCCC_ARCH_FEATURES but this sequence requires that this
  111. * standard ARM_SMCCC_ARCH_FEATURES function ID itself is supported.
  112. * It is queried here with invoking PSCI_FEATURES known available
  113. * from PSCI 1.0.
  114. */
  115. if (!device_is_compatible(dev, "arm,psci-1.0") ||
  116. PSCI_VERSION_MAJOR(psci_0_2_get_version()) == 0)
  117. return 0;
  118. if (request_psci_features(ARM_SMCCC_ARCH_FEATURES) ==
  119. PSCI_RET_NOT_SUPPORTED)
  120. return 0;
  121. if (psci_method == PSCI_METHOD_HVC)
  122. pdata->invoke_fn = smccc_invoke_hvc;
  123. else
  124. pdata->invoke_fn = smccc_invoke_smc;
  125. feature_cnt = ll_entry_count(struct arm_smccc_feature, arm_smccc_feature);
  126. feature = ll_entry_start(struct arm_smccc_feature, arm_smccc_feature);
  127. for (n = 0; n < feature_cnt; n++, feature++) {
  128. const char *drv_name = feature->driver_name;
  129. struct udevice *dev2;
  130. int ret;
  131. if (!feature->is_supported || !feature->is_supported(pdata->invoke_fn))
  132. continue;
  133. ret = device_bind_driver(dev, drv_name, drv_name, &dev2);
  134. if (ret) {
  135. pr_warn("%s was not bound: %d, ignore\n", drv_name, ret);
  136. continue;
  137. }
  138. dev_set_parent_plat(dev2, dev_get_plat(dev));
  139. }
  140. return 0;
  141. }
  142. static int psci_bind(struct udevice *dev)
  143. {
  144. /* No SYSTEM_RESET support for PSCI 0.1 */
  145. if (device_is_compatible(dev, "arm,psci-0.2") ||
  146. device_is_compatible(dev, "arm,psci-1.0")) {
  147. int ret;
  148. /* bind psci-sysreset optionally */
  149. ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
  150. NULL);
  151. if (ret)
  152. pr_debug("PSCI System Reset was not bound.\n");
  153. }
  154. /* From PSCI v1.0 onward we can discover services through ARM_SMCCC_FEATURE */
  155. if (IS_ENABLED(CONFIG_ARM_SMCCC_FEATURES) && device_is_compatible(dev, "arm,psci-1.0"))
  156. dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
  157. return 0;
  158. }
  159. static int psci_probe(struct udevice *dev)
  160. {
  161. const char *method;
  162. #if defined(CONFIG_ARM64)
  163. if (current_el() == 3)
  164. return -EINVAL;
  165. #endif
  166. method = ofnode_read_string(dev_ofnode(dev), "method");
  167. if (!method) {
  168. pr_warn("missing \"method\" property\n");
  169. return -ENXIO;
  170. }
  171. if (!strcmp("hvc", method)) {
  172. psci_method = PSCI_METHOD_HVC;
  173. } else if (!strcmp("smc", method)) {
  174. psci_method = PSCI_METHOD_SMC;
  175. } else {
  176. pr_warn("invalid \"method\" property: %s\n", method);
  177. return -EINVAL;
  178. }
  179. return bind_smccc_features(dev, psci_method);
  180. }
  181. /**
  182. * void do_psci_probe() - probe PSCI firmware driver
  183. *
  184. * Ensure that psci_method is initialized.
  185. */
  186. static void __maybe_unused do_psci_probe(void)
  187. {
  188. struct udevice *dev;
  189. uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
  190. }
  191. #if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
  192. efi_status_t efi_reset_system_init(void)
  193. {
  194. do_psci_probe();
  195. return EFI_SUCCESS;
  196. }
  197. void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type,
  198. efi_status_t reset_status,
  199. unsigned long data_size,
  200. void *reset_data)
  201. {
  202. if (reset_type == EFI_RESET_COLD ||
  203. reset_type == EFI_RESET_WARM ||
  204. reset_type == EFI_RESET_PLATFORM_SPECIFIC) {
  205. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  206. } else if (reset_type == EFI_RESET_SHUTDOWN) {
  207. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  208. }
  209. while (1)
  210. ;
  211. }
  212. #endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
  213. #ifdef CONFIG_PSCI_RESET
  214. void reset_misc(void)
  215. {
  216. do_psci_probe();
  217. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  218. }
  219. #endif /* CONFIG_PSCI_RESET */
  220. void psci_sys_reset(u32 type)
  221. {
  222. bool reset2_supported;
  223. do_psci_probe();
  224. reset2_supported = psci_is_system_reset2_supported();
  225. if (type == SYSRESET_WARM && reset2_supported) {
  226. /*
  227. * reset_type[31] = 0 (architectural)
  228. * reset_type[30:0] = 0 (SYSTEM_WARM_RESET)
  229. * cookie = 0 (ignored by the implementation)
  230. */
  231. invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0);
  232. } else {
  233. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  234. }
  235. }
  236. void psci_sys_poweroff(void)
  237. {
  238. do_psci_probe();
  239. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  240. }
  241. #if IS_ENABLED(CONFIG_CMD_POWEROFF) && !IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF)
  242. int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  243. {
  244. do_psci_probe();
  245. puts("poweroff ...\n");
  246. udelay(50000); /* wait 50 ms */
  247. disable_interrupts();
  248. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  249. enable_interrupts();
  250. log_err("Power off not supported on this platform\n");
  251. return CMD_RET_FAILURE;
  252. }
  253. #endif
  254. static const struct udevice_id psci_of_match[] = {
  255. { .compatible = "arm,psci" },
  256. { .compatible = "arm,psci-0.2" },
  257. { .compatible = "arm,psci-1.0" },
  258. {},
  259. };
  260. U_BOOT_DRIVER(psci) = {
  261. .name = DRIVER_NAME,
  262. .id = UCLASS_FIRMWARE,
  263. .of_match = psci_of_match,
  264. .bind = psci_bind,
  265. .probe = psci_probe,
  266. #ifdef CONFIG_ARM_SMCCC_FEATURES
  267. .plat_auto = sizeof(struct psci_plat_data),
  268. #endif
  269. .flags = DM_FLAG_PRE_RELOC,
  270. };