psci.c 3.8 KB

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