psci.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <linux/delay.h>
  16. #include <linux/libfdt.h>
  17. #include <linux/arm-smccc.h>
  18. #include <linux/errno.h>
  19. #include <linux/printk.h>
  20. #include <linux/psci.h>
  21. #define DRIVER_NAME "psci"
  22. #define PSCI_METHOD_HVC 1
  23. #define PSCI_METHOD_SMC 2
  24. int __efi_runtime_data psci_method;
  25. unsigned long __efi_runtime invoke_psci_fn
  26. (unsigned long function_id, unsigned long arg0,
  27. unsigned long arg1, unsigned long arg2)
  28. {
  29. struct arm_smccc_res res;
  30. /*
  31. * In the __efi_runtime we need to avoid the switch statement. In some
  32. * cases the compiler creates lookup tables to implement switch. These
  33. * tables are not correctly relocated when SetVirtualAddressMap is
  34. * called.
  35. */
  36. if (psci_method == PSCI_METHOD_SMC)
  37. arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
  38. else if (psci_method == PSCI_METHOD_HVC)
  39. arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
  40. else
  41. res.a0 = PSCI_RET_DISABLED;
  42. return res.a0;
  43. }
  44. static int psci_bind(struct udevice *dev)
  45. {
  46. /* No SYSTEM_RESET support for PSCI 0.1 */
  47. if (device_is_compatible(dev, "arm,psci-0.2") ||
  48. device_is_compatible(dev, "arm,psci-1.0")) {
  49. int ret;
  50. /* bind psci-sysreset optionally */
  51. ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
  52. NULL);
  53. if (ret)
  54. pr_debug("PSCI System Reset was not bound.\n");
  55. }
  56. return 0;
  57. }
  58. static int psci_probe(struct udevice *dev)
  59. {
  60. const char *method;
  61. method = ofnode_read_string(dev_ofnode(dev), "method");
  62. if (!method) {
  63. pr_warn("missing \"method\" property\n");
  64. return -ENXIO;
  65. }
  66. if (!strcmp("hvc", method)) {
  67. psci_method = PSCI_METHOD_HVC;
  68. } else if (!strcmp("smc", method)) {
  69. psci_method = PSCI_METHOD_SMC;
  70. } else {
  71. pr_warn("invalid \"method\" property: %s\n", method);
  72. return -EINVAL;
  73. }
  74. return 0;
  75. }
  76. /**
  77. * void do_psci_probe() - probe PSCI firmware driver
  78. *
  79. * Ensure that psci_method is initialized.
  80. */
  81. static void __maybe_unused do_psci_probe(void)
  82. {
  83. struct udevice *dev;
  84. uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
  85. }
  86. #if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
  87. efi_status_t efi_reset_system_init(void)
  88. {
  89. do_psci_probe();
  90. return EFI_SUCCESS;
  91. }
  92. void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type,
  93. efi_status_t reset_status,
  94. unsigned long data_size,
  95. void *reset_data)
  96. {
  97. if (reset_type == EFI_RESET_COLD ||
  98. reset_type == EFI_RESET_WARM ||
  99. reset_type == EFI_RESET_PLATFORM_SPECIFIC) {
  100. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  101. } else if (reset_type == EFI_RESET_SHUTDOWN) {
  102. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  103. }
  104. while (1)
  105. ;
  106. }
  107. #endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
  108. #ifdef CONFIG_PSCI_RESET
  109. void reset_misc(void)
  110. {
  111. do_psci_probe();
  112. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  113. }
  114. #endif /* CONFIG_PSCI_RESET */
  115. #ifdef CONFIG_CMD_POWEROFF
  116. int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  117. {
  118. do_psci_probe();
  119. puts("poweroff ...\n");
  120. udelay(50000); /* wait 50 ms */
  121. disable_interrupts();
  122. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  123. enable_interrupts();
  124. log_err("Power off not supported on this platform\n");
  125. return CMD_RET_FAILURE;
  126. }
  127. #endif
  128. static const struct udevice_id psci_of_match[] = {
  129. { .compatible = "arm,psci" },
  130. { .compatible = "arm,psci-0.2" },
  131. { .compatible = "arm,psci-1.0" },
  132. {},
  133. };
  134. U_BOOT_DRIVER(psci) = {
  135. .name = DRIVER_NAME,
  136. .id = UCLASS_FIRMWARE,
  137. .of_match = psci_of_match,
  138. .bind = psci_bind,
  139. .probe = psci_probe,
  140. };