ArmSmcPsciResetSystemLib.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /** @file
  2. ResetSystemLib implementation using PSCI calls
  3. Copyright (c) 2017 - 2018, Linaro Ltd. All rights reserved.<BR>
  4. Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <PiDxe.h>
  8. #include <Library/ArmMmuLib.h>
  9. #include <Library/ArmSmcLib.h>
  10. #include <Library/BaseLib.h>
  11. #include <Library/DebugLib.h>
  12. #include <Library/ResetSystemLib.h>
  13. #include <Library/UefiBootServicesTableLib.h>
  14. #include <Library/UefiRuntimeLib.h>
  15. #include <IndustryStandard/ArmStdSmc.h>
  16. /**
  17. This function causes a system-wide reset (cold reset), in which
  18. all circuitry within the system returns to its initial state. This type of reset
  19. is asynchronous to system operation and operates without regard to
  20. cycle boundaries.
  21. If this function returns, it means that the system does not support cold reset.
  22. **/
  23. VOID
  24. EFIAPI
  25. ResetCold (
  26. VOID
  27. )
  28. {
  29. ARM_SMC_ARGS ArmSmcArgs;
  30. // Send a PSCI 0.2 SYSTEM_RESET command
  31. ArmSmcArgs.Arg0 = ARM_SMC_ID_PSCI_SYSTEM_RESET;
  32. ArmCallSmc (&ArmSmcArgs);
  33. }
  34. /**
  35. This function causes a system-wide initialization (warm reset), in which all processors
  36. are set to their initial state. Pending cycles are not corrupted.
  37. If this function returns, it means that the system does not support warm reset.
  38. **/
  39. VOID
  40. EFIAPI
  41. ResetWarm (
  42. VOID
  43. )
  44. {
  45. // Map a warm reset into a cold reset
  46. ResetCold ();
  47. }
  48. /**
  49. This function causes the system to enter a power state equivalent
  50. to the ACPI G2/S5 or G3 states.
  51. If this function returns, it means that the system does not support shutdown reset.
  52. **/
  53. VOID
  54. EFIAPI
  55. ResetShutdown (
  56. VOID
  57. )
  58. {
  59. ARM_SMC_ARGS ArmSmcArgs;
  60. // Send a PSCI 0.2 SYSTEM_OFF command
  61. ArmSmcArgs.Arg0 = ARM_SMC_ID_PSCI_SYSTEM_OFF;
  62. ArmCallSmc (&ArmSmcArgs);
  63. }
  64. VOID DisableMmuAndReenterPei (VOID);
  65. /**
  66. This function causes the system to enter S3 and then wake up immediately.
  67. If this function returns, it means that the system does not support S3 feature.
  68. **/
  69. VOID
  70. EFIAPI
  71. EnterS3WithImmediateWake (
  72. VOID
  73. )
  74. {
  75. EFI_PHYSICAL_ADDRESS Alloc;
  76. EFI_MEMORY_DESCRIPTOR *MemMap;
  77. UINTN MemMapSize;
  78. UINTN MapKey, DescriptorSize;
  79. UINT32 DescriptorVersion;
  80. EFI_STATUS Status;
  81. if (FeaturePcdGet (PcdArmReenterPeiForCapsuleWarmReboot) &&
  82. !EfiAtRuntime ()) {
  83. //
  84. // At boot time, we are the only core running, so we can implement the
  85. // immediate wake (which is used by capsule update) by disabling the MMU
  86. // and interrupts, and jumping to the PEI entry point.
  87. //
  88. //
  89. // Obtain the size of the memory map
  90. //
  91. MemMapSize = 0;
  92. MemMap = NULL;
  93. Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize,
  94. &DescriptorVersion);
  95. ASSERT (Status == EFI_BUFFER_TOO_SMALL);
  96. //
  97. // Add some slack to the allocation to cater for changes in the memory
  98. // map if ExitBootServices () fails the first time around.
  99. //
  100. MemMapSize += SIZE_4KB;
  101. Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData,
  102. EFI_SIZE_TO_PAGES (MemMapSize), &Alloc);
  103. ASSERT_EFI_ERROR (Status);
  104. MemMap = (EFI_MEMORY_DESCRIPTOR *)(UINTN)Alloc;
  105. Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize,
  106. &DescriptorVersion);
  107. ASSERT_EFI_ERROR (Status);
  108. Status = gBS->ExitBootServices (gImageHandle, MapKey);
  109. if (EFI_ERROR (Status)) {
  110. //
  111. // ExitBootServices () may fail the first time around if an event fired
  112. // right after the call to GetMemoryMap() which allocated or freed memory.
  113. // Since that first call to ExitBootServices () will disarm the timer,
  114. // this is guaranteed not to happen again, so one additional attempt
  115. // should suffice.
  116. //
  117. Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize,
  118. &DescriptorVersion);
  119. ASSERT_EFI_ERROR (Status);
  120. Status = gBS->ExitBootServices (gImageHandle, MapKey);
  121. ASSERT_EFI_ERROR (Status);
  122. }
  123. DisableMmuAndReenterPei ();
  124. }
  125. }
  126. /**
  127. This function causes a systemwide reset. The exact type of the reset is
  128. defined by the EFI_GUID that follows the Null-terminated Unicode string passed
  129. into ResetData. If the platform does not recognize the EFI_GUID in ResetData
  130. the platform must pick a supported reset type to perform.The platform may
  131. optionally log the parameters from any non-normal reset that occurs.
  132. @param[in] DataSize The size, in bytes, of ResetData.
  133. @param[in] ResetData The data buffer starts with a Null-terminated string,
  134. followed by the EFI_GUID.
  135. **/
  136. VOID
  137. EFIAPI
  138. ResetPlatformSpecific (
  139. IN UINTN DataSize,
  140. IN VOID *ResetData
  141. )
  142. {
  143. // Map the platform specific reset as reboot
  144. ResetCold ();
  145. }
  146. /**
  147. The ResetSystem function resets the entire platform.
  148. @param[in] ResetType The type of reset to perform.
  149. @param[in] ResetStatus The status code for the reset.
  150. @param[in] DataSize The size, in bytes, of ResetData.
  151. @param[in] ResetData For a ResetType of EfiResetCold, EfiResetWarm, or EfiResetShutdown
  152. the data buffer starts with a Null-terminated string, optionally
  153. followed by additional binary data. The string is a description
  154. that the caller may use to further indicate the reason for the
  155. system reset.
  156. **/
  157. VOID
  158. EFIAPI
  159. ResetSystem (
  160. IN EFI_RESET_TYPE ResetType,
  161. IN EFI_STATUS ResetStatus,
  162. IN UINTN DataSize,
  163. IN VOID *ResetData OPTIONAL
  164. )
  165. {
  166. switch (ResetType) {
  167. case EfiResetWarm:
  168. ResetWarm ();
  169. break;
  170. case EfiResetCold:
  171. ResetCold ();
  172. break;
  173. case EfiResetShutdown:
  174. ResetShutdown ();
  175. return;
  176. case EfiResetPlatformSpecific:
  177. ResetPlatformSpecific (DataSize, ResetData);
  178. return;
  179. default:
  180. return;
  181. }
  182. }