MemDetect.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /** @file
  2. Memory Detection for Virtual Machines.
  3. Copyright (c) 2022 Loongson Technology Corporation Limited. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. //
  7. // The package level header files this module uses
  8. //
  9. #include <PiPei.h>
  10. //
  11. // The Library classes this module consumes
  12. //
  13. #include <Library/BaseMemoryLib.h>
  14. #include <Library/MemoryAllocationLib.h>
  15. #include <Library/DebugLib.h>
  16. #include <Library/HobLib.h>
  17. #include <Library/IoLib.h>
  18. #include <Library/PcdLib.h>
  19. #include <Library/PeimEntryPoint.h>
  20. #include <Library/ResourcePublicationLib.h>
  21. #include <Library/QemuFwCfgLib.h>
  22. #include "Platform.h"
  23. /**
  24. Publish PEI core memory
  25. @return EFI_SUCCESS The PEIM initialized successfully.
  26. **/
  27. EFI_STATUS
  28. PublishPeiMemory (
  29. VOID
  30. )
  31. {
  32. EFI_STATUS Status;
  33. UINT64 Base;
  34. UINT64 Size;
  35. UINT64 RamTop;
  36. //
  37. // Determine the range of memory to use during PEI
  38. //
  39. Base = PcdGet64 (PcdSecPeiTempRamBase) + PcdGet32 (PcdSecPeiTempRamSize);
  40. RamTop = PcdGet64 (PcdUefiRamTop);
  41. Size = RamTop - Base;
  42. //
  43. // Publish this memory to the PEI Core
  44. //
  45. Status = PublishSystemMemory (Base, Size);
  46. ASSERT_EFI_ERROR (Status);
  47. DEBUG ((DEBUG_INFO, "Publish Memory Initialize done.\n"));
  48. return Status;
  49. }
  50. /**
  51. Peform Memory Detection
  52. Publish system RAM and reserve memory regions
  53. **/
  54. VOID
  55. InitializeRamRegions (
  56. VOID
  57. )
  58. {
  59. EFI_STATUS Status;
  60. FIRMWARE_CONFIG_ITEM FwCfgItem;
  61. UINTN FwCfgSize;
  62. LOONGARCH_MEMMAP_ENTRY MemoryMapEntry;
  63. LOONGARCH_MEMMAP_ENTRY *StartEntry;
  64. LOONGARCH_MEMMAP_ENTRY *pEntry;
  65. UINTN Processed;
  66. Status = QemuFwCfgFindFile ("etc/memmap", &FwCfgItem, &FwCfgSize);
  67. if (EFI_ERROR (Status)) {
  68. DEBUG ((DEBUG_ERROR, "%a %d read etc/memmap error Status %d \n", __func__, __LINE__, Status));
  69. return ;
  70. }
  71. if (FwCfgSize % sizeof MemoryMapEntry != 0) {
  72. DEBUG ((DEBUG_ERROR, "no MemoryMapEntry FwCfgSize:%d\n", FwCfgSize));
  73. return ;
  74. }
  75. QemuFwCfgSelectItem (FwCfgItem);
  76. StartEntry = AllocatePages (EFI_SIZE_TO_PAGES (FwCfgSize));
  77. QemuFwCfgReadBytes (FwCfgSize, StartEntry);
  78. for (Processed = 0; Processed < (FwCfgSize / sizeof MemoryMapEntry); Processed++) {
  79. pEntry = StartEntry + Processed;
  80. if (pEntry->Length == 0) {
  81. continue;
  82. }
  83. DEBUG ((DEBUG_INFO, "MemmapEntry Base %p length %p type %d\n", pEntry->BaseAddr, pEntry->Length, pEntry->Type));
  84. if (pEntry->Type != EfiAcpiAddressRangeMemory) {
  85. continue;
  86. }
  87. AddMemoryRangeHob ( pEntry->BaseAddr, pEntry->BaseAddr + pEntry->Length);
  88. }
  89. }