AcpiS3Save.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /** @file
  2. This is an implementation of the ACPI S3 Save protocol. This is defined in
  3. S3 boot path specification 0.9.
  4. Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <PiDxe.h>
  8. #include <Library/BaseMemoryLib.h>
  9. #include <Library/UefiBootServicesTableLib.h>
  10. #include <Library/PcdLib.h>
  11. #include <Library/DebugLib.h>
  12. #include <Protocol/AcpiS3Save.h>
  13. #include "AcpiS3Save.h"
  14. /**
  15. Hook point for AcpiVariableThunkPlatform for InstallAcpiS3Save.
  16. **/
  17. VOID
  18. InstallAcpiS3SaveThunk (
  19. VOID
  20. );
  21. /**
  22. Hook point for AcpiVariableThunkPlatform for S3Ready.
  23. **/
  24. VOID
  25. S3ReadyThunkPlatform (
  26. VOID
  27. );
  28. UINTN mLegacyRegionSize;
  29. EFI_ACPI_S3_SAVE_PROTOCOL mS3Save = {
  30. LegacyGetS3MemorySize,
  31. S3Ready,
  32. };
  33. /**
  34. Allocate memory below 4G memory address.
  35. This function allocates memory below 4G memory address.
  36. @param MemoryType Memory type of memory to allocate.
  37. @param Size Size of memory to allocate.
  38. @return Allocated address for output.
  39. **/
  40. VOID*
  41. AllocateMemoryBelow4G (
  42. IN EFI_MEMORY_TYPE MemoryType,
  43. IN UINTN Size
  44. )
  45. {
  46. UINTN Pages;
  47. EFI_PHYSICAL_ADDRESS Address;
  48. EFI_STATUS Status;
  49. VOID* Buffer;
  50. Pages = EFI_SIZE_TO_PAGES (Size);
  51. Address = 0xffffffff;
  52. Status = gBS->AllocatePages (
  53. AllocateMaxAddress,
  54. MemoryType,
  55. Pages,
  56. &Address
  57. );
  58. ASSERT_EFI_ERROR (Status);
  59. Buffer = (VOID *) (UINTN) Address;
  60. ZeroMem (Buffer, Size);
  61. return Buffer;
  62. }
  63. /**
  64. Gets the buffer of legacy memory below 1 MB
  65. This function is to get the buffer in legacy memory below 1MB that is required during S3 resume.
  66. @param This A pointer to the EFI_ACPI_S3_SAVE_PROTOCOL instance.
  67. @param Size The returned size of legacy memory below 1 MB.
  68. @retval EFI_SUCCESS Size is successfully returned.
  69. @retval EFI_INVALID_PARAMETER The pointer Size is NULL.
  70. **/
  71. EFI_STATUS
  72. EFIAPI
  73. LegacyGetS3MemorySize (
  74. IN EFI_ACPI_S3_SAVE_PROTOCOL *This,
  75. OUT UINTN *Size
  76. )
  77. {
  78. if (Size == NULL) {
  79. return EFI_INVALID_PARAMETER;
  80. }
  81. *Size = mLegacyRegionSize;
  82. return EFI_SUCCESS;
  83. }
  84. /**
  85. Prepares all information that is needed in the S3 resume boot path.
  86. Allocate the resources or prepare informations and save in ACPI variable set for S3 resume boot path
  87. @param This A pointer to the EFI_ACPI_S3_SAVE_PROTOCOL instance.
  88. @param LegacyMemoryAddress The base address of legacy memory.
  89. @retval EFI_NOT_FOUND Some necessary information cannot be found.
  90. @retval EFI_SUCCESS All information was saved successfully.
  91. @retval EFI_OUT_OF_RESOURCES Resources were insufficient to save all the information.
  92. @retval EFI_INVALID_PARAMETER The memory range is not located below 1 MB.
  93. **/
  94. EFI_STATUS
  95. EFIAPI
  96. S3Ready (
  97. IN EFI_ACPI_S3_SAVE_PROTOCOL *This,
  98. IN VOID *LegacyMemoryAddress
  99. )
  100. {
  101. STATIC BOOLEAN AlreadyEntered;
  102. DEBUG ((EFI_D_INFO, "S3Ready!\n"));
  103. //
  104. // Platform may invoke AcpiS3Save->S3Save() before ExitPmAuth, because we need save S3 information there, while BDS ReadyToBoot may invoke it again.
  105. // So if 2nd S3Save() is triggered later, we need ignore it.
  106. //
  107. if (AlreadyEntered) {
  108. return EFI_SUCCESS;
  109. }
  110. AlreadyEntered = TRUE;
  111. return EFI_SUCCESS;
  112. }
  113. /**
  114. The Driver Entry Point.
  115. The function is the driver Entry point which will produce AcpiS3SaveProtocol.
  116. @param ImageHandle A handle for the image that is initializing this driver
  117. @param SystemTable A pointer to the EFI system table
  118. @retval EFI_SUCCESS Driver initialized successfully
  119. @retval EFI_UNSUPPORTED Do not support ACPI S3
  120. @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
  121. **/
  122. EFI_STATUS
  123. EFIAPI
  124. InstallAcpiS3Save (
  125. IN EFI_HANDLE ImageHandle,
  126. IN EFI_SYSTEM_TABLE *SystemTable
  127. )
  128. {
  129. EFI_STATUS Status;
  130. if (!PcdGetBool (PcdAcpiS3Enable)) {
  131. return EFI_UNSUPPORTED;
  132. }
  133. if (!FeaturePcdGet(PcdPlatformCsmSupport)) {
  134. //
  135. // More memory for no CSM tip, because GDT need relocation
  136. //
  137. mLegacyRegionSize = 0x250;
  138. } else {
  139. mLegacyRegionSize = 0x100;
  140. }
  141. Status = gBS->InstallProtocolInterface (
  142. &ImageHandle,
  143. &gEfiAcpiS3SaveProtocolGuid,
  144. EFI_NATIVE_INTERFACE,
  145. &mS3Save
  146. );
  147. ASSERT_EFI_ERROR (Status);
  148. return Status;
  149. }