From: Supreeth Venkatesh <supreeth.venkat...@arm.com>

This patch implements management mode memory allocation services.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Supreeth Venkatesh <supreeth.venkat...@arm.com>
Reviewed-by: Achin Gupta <achin.gu...@arm.com>
Reviewed-by: Jiewen Yao <jiewen....@intel.com>
Signed-off-by: Sughosh Ganu <sughosh.g...@arm.com>
---
 StandaloneMmPkg/Include/Guid/MmCoreData.h          | 133 ++++++++++++++++++
 StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h  |  62 +++++++++
 .../StandaloneMmCoreMemoryAllocationLib.c          | 155 ++++++++++++++++-----
 .../StandaloneMmCoreMemoryAllocationLib.inf        |  49 +++++++
 .../StandaloneMmCoreMemoryAllocationServices.h     |  38 +++++
 5 files changed, 403 insertions(+), 34 deletions(-)
 create mode 100644 StandaloneMmPkg/Include/Guid/MmCoreData.h
 create mode 100644 StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
 copy MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c => 
StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c
 (82%)
 create mode 100644 
StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.inf
 create mode 100644 
StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationServices.h

diff --git a/StandaloneMmPkg/Include/Guid/MmCoreData.h 
b/StandaloneMmPkg/Include/Guid/MmCoreData.h
new file mode 100644
index 000000000000..a1168f95f7d5
--- /dev/null
+++ b/StandaloneMmPkg/Include/Guid/MmCoreData.h
@@ -0,0 +1,133 @@
+/** @file
+  MM Core data.
+
+Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2018, ARM Limited. All rights reserved.<BR>
+This program and the accompanying materials are licensed and made available 
under
+the terms and conditions of the BSD License that accompanies this distribution.
+The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php.
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef __MM_CORE_DATA_H__
+#define __MM_CORE_DATA_H__
+
+#define MM_CORE_DATA_HOB_GUID \
+  { 0xa160bf99, 0x2aa4, 0x4d7d, { 0x99, 0x93, 0x89, 0x9c, 0xb1, 0x2d, 0xf3, 
0x76 }}
+
+extern EFI_GUID gMmCoreDataHobGuid;
+
+typedef struct {
+  //
+  // Address pointer to MM_CORE_PRIVATE_DATA
+  //
+  EFI_PHYSICAL_ADDRESS   Address;
+} MM_CORE_DATA_HOB_DATA;
+
+
+///
+/// Define values for the communications buffer used when 
gEfiEventDxeDispatchGuid is
+/// event signaled.  This event is signaled by the DXE Core each time the DXE 
Core
+/// dispatcher has completed its work.  When this event is signaled, the MM 
Core
+/// if notified, so the MM Core can dispatch MM drivers.  If 
COMM_BUFFER_MM_DISPATCH_ERROR
+/// is returned in the communication buffer, then an error occurred 
dispatching MM
+/// Drivers.  If COMM_BUFFER_MM_DISPATCH_SUCCESS is returned, then the MM Core
+/// dispatched all the drivers it could.  If COMM_BUFFER_MM_DISPATCH_RESTART is
+/// returned, then the MM Core just dispatched the MM Driver that registered
+/// the MM Entry Point enabling the use of MM Mode.  In this case, the MM Core
+/// should be notified again to dispatch more MM Drivers using MM Mode.
+///
+#define COMM_BUFFER_MM_DISPATCH_ERROR    0x00
+#define COMM_BUFFER_MM_DISPATCH_SUCCESS  0x01
+#define COMM_BUFFER_MM_DISPATCH_RESTART  0x02
+
+///
+/// Signature for the private structure shared between the MM IPL and the MM 
Core
+///
+#define MM_CORE_PRIVATE_DATA_SIGNATURE  SIGNATURE_32 ('m', 'm', 'i', 'c')
+
+///
+/// Private structure that is used to share information between the MM IPL and
+/// the MM Core.  This structure is allocated from memory of type 
EfiRuntimeServicesData.
+/// Since runtime memory types are converted to available memory when a legacy 
boot
+/// is performed, the MM Core must not access any fields of this structure if 
a legacy
+/// boot is performed.  As a result, the MM IPL must create an event 
notification
+/// for the Legacy Boot event and notify the MM Core that a legacy boot is 
being
+/// performed.  The MM Core can then use this information to filter accesses to
+/// thos structure.
+///
+typedef struct {
+  UINT64                          Signature;
+
+  ///
+  /// The number of MMRAM ranges passed from the MM IPL to the MM Core.  The MM
+  /// Core uses these ranges of MMRAM to initialize the MM Core memory manager.
+  ///
+  UINT64                          MmramRangeCount;
+
+  ///
+  /// A table of MMRAM ranges passed from the MM IPL to the MM Core.  The MM
+  /// Core uses these ranges of MMRAM to initialize the MM Core memory manager.
+  ///
+  EFI_PHYSICAL_ADDRESS            MmramRanges;
+
+  ///
+  /// The MM Foundation Entry Point.  The MM Core fills in this field when the
+  /// MM Core is initialized.  The MM IPL is responsbile for registering this 
entry
+  /// point with the MM Configuration Protocol.  The MM Configuration Protocol 
may
+  /// not be available at the time the MM IPL and MM Core are started, so the 
MM IPL
+  /// sets up a protocol notification on the MM Configuration Protocol and 
registers
+  /// the MM Foundation Entry Point as soon as the MM Configuration Protocol is
+  /// available.
+  ///
+  EFI_PHYSICAL_ADDRESS            MmEntryPoint;
+
+  ///
+  /// Boolean flag set to TRUE while an MMI is being processed by the MM Core.
+  ///
+  BOOLEAN                         MmEntryPointRegistered;
+
+  ///
+  /// Boolean flag set to TRUE while an MMI is being processed by the MM Core.
+  ///
+  BOOLEAN                         InMm;
+
+  ///
+  /// This field is set by the MM Core then the MM Core is initialized.  This 
field is
+  /// used by the MM Base 2 Protocol and MM Communication Protocol 
implementations in
+  /// the MM IPL.
+  ///
+  EFI_PHYSICAL_ADDRESS            Mmst;
+
+  ///
+  /// This field is used by the MM Communicatioon Protocol to pass a buffer 
into
+  /// a software MMI handler and for the software MMI handler to pass a buffer 
back to
+  /// the caller of the MM Communication Protocol.
+  ///
+  EFI_PHYSICAL_ADDRESS            CommunicationBuffer;
+
+  ///
+  /// This field is used by the MM Communicatioon Protocol to pass the size of 
a buffer,
+  /// in bytes, into a software MMI handler and for the software MMI handler 
to pass the
+  /// size, in bytes, of a buffer back to the caller of the MM Communication 
Protocol.
+  ///
+  UINT64                          BufferSize;
+
+  ///
+  /// This field is used by the MM Communication Protocol to pass the return 
status from
+  /// a software MMI handler back to the caller of the MM Communication 
Protocol.
+  ///
+  UINT64                          ReturnStatus;
+
+  EFI_PHYSICAL_ADDRESS            MmCoreImageBase;
+  UINT64                          MmCoreImageSize;
+  EFI_PHYSICAL_ADDRESS            MmCoreEntryPoint;
+
+  EFI_PHYSICAL_ADDRESS            StandaloneBfvAddress;
+} MM_CORE_PRIVATE_DATA;
+
+#endif
diff --git a/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h 
b/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
new file mode 100644
index 000000000000..15818b5a8aa1
--- /dev/null
+++ b/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
@@ -0,0 +1,62 @@
+/** @file
+  Definition of GUIDed HOB for reserving MMRAM regions.
+
+  This file defines:
+  * the GUID used to identify the GUID HOB for reserving MMRAM regions.
+  * the data structure of MMRAM descriptor to describe MMRAM candidate regions
+  * values of state of MMRAM candidate regions
+  * the GUID specific data structure of HOB for reserving MMRAM regions.
+  This GUIDed HOB can be used to convey the existence of the T-SEG reservation 
and H-SEG usage
+
+Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
+
+This program and the accompanying materials are licensed and made available 
under
+the terms and conditions of the BSD License that accompanies this distribution.
+The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php.
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+  @par Revision Reference:
+  GUIDs defined in MmCis spec version 0.9.
+
+**/
+
+#ifndef _EFI_MM_PEI_MMRAM_MEMORY_RESERVE_H_
+#define _EFI_MM_PEI_MMRAM_MEMORY_RESERVE_H_
+
+#define EFI_MM_PEI_MMRAM_MEMORY_RESERVE \
+  { \
+    0x0703f912, 0xbf8d, 0x4e2a, {0xbe, 0x07, 0xab, 0x27, 0x25, 0x25, 0xc5, 
0x92 } \
+  }
+
+/**
+* GUID specific data structure of HOB for reserving MMRAM regions.
+*
+* Inconsistent with specification here:
+* EFI_HOB_MMRAM_DESCRIPTOR_BLOCK has been changed to 
EFI_MMRAM_HOB_DESCRIPTOR_BLOCK.
+* This inconsistency is kept in code in order for backward compatibility.
+**/
+typedef struct {
+  ///
+  /// Designates the number of possible regions in the system
+  /// that can be usable for MMRAM.
+  ///
+  /// Inconsistent with specification here:
+  /// In Framework MM CIS 0.91 specification, it defines the field type as 
UINTN.
+  /// However, HOBs are supposed to be CPU neutral, so UINT32 should be used 
instead.
+  ///
+  UINT32                NumberOfMmReservedRegions;
+  ///
+  /// Used throughout this protocol to describe the candidate
+  /// regions for MMRAM that are supported by this platform.
+  ///
+  EFI_MMRAM_DESCRIPTOR  Descriptor[1];
+} EFI_MMRAM_HOB_DESCRIPTOR_BLOCK;
+
+extern EFI_GUID gEfiMmPeiSmramMemoryReserveGuid;
+
+#endif
+
diff --git a/MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c 
b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c
similarity index 82%
copy from MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c
copy to 
StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c
index dce13bbdf5e6..6ab9859b6861 100644
--- a/MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c
+++ 
b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c
@@ -1,26 +1,29 @@
 /** @file
-  Support routines for memory allocation routines based
-  on boot services for Dxe phase drivers.
+  Support routines for memory allocation routines based on Standalone MM Core 
internal functions.
+
+  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
 
-  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD 
License
   which accompanies this distribution.  The full text of the license may be 
found at
-  http://opensource.org/licenses/bsd-license.php.
+  http://opensource.org/licenses/bsd-license.php
 
   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 
 **/
 
+#include <PiMm.h>
 
-#include <Uefi.h>
-
-
+#include <Guid/MmramMemoryReserve.h>
 #include <Library/MemoryAllocationLib.h>
-#include <Library/UefiBootServicesTableLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/DebugLib.h>
+#include <Library/HobLib.h>
+#include "StandaloneMmCoreMemoryAllocationServices.h"
+
+EFI_MM_SYSTEM_TABLE   *gMmst = NULL;
 
 /**
   Allocates one or more 4KB pages of a certain memory type.
@@ -48,7 +51,7 @@ InternalAllocatePages (
     return NULL;
   }
 
-  Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);
+  Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, Pages, 
&Memory);
   if (EFI_ERROR (Status)) {
     return NULL;
   }
@@ -74,7 +77,7 @@ AllocatePages (
   IN UINTN  Pages
   )
 {
-  return InternalAllocatePages (EfiBootServicesData, Pages);
+  return InternalAllocatePages (EfiRuntimeServicesData, Pages);
 }
 
 /**
@@ -118,7 +121,7 @@ AllocateReservedPages (
   IN UINTN  Pages
   )
 {
-  return InternalAllocatePages (EfiReservedMemoryType, Pages);
+  return NULL;
 }
 
 /**
@@ -134,7 +137,7 @@ AllocateReservedPages (
   then ASSERT().
   If Pages is zero, then ASSERT().
 
-  @param  Buffer                The pointer to the buffer of pages to free.
+  @param  Buffer                Pointer to the buffer of pages to free.
   @param  Pages                 The number of 4 KB pages to free.
 
 **/
@@ -148,7 +151,7 @@ FreePages (
   EFI_STATUS  Status;
 
   ASSERT (Pages != 0);
-  Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);
+  Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);
   ASSERT_EFI_ERROR (Status);
 }
 
@@ -203,7 +206,7 @@ InternalAllocateAlignedPages (
     //
     ASSERT (RealPages > Pages);
 
-    Status         = gBS->AllocatePages (AllocateAnyPages, MemoryType, 
RealPages, &Memory);
+    Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, RealPages, 
&Memory);
     if (EFI_ERROR (Status)) {
       return NULL;
     }
@@ -213,23 +216,23 @@ InternalAllocateAlignedPages (
       //
       // Free first unaligned page(s).
       //
-      Status = gBS->FreePages (Memory, UnalignedPages);
+      Status = gMmst->MmFreePages (Memory, UnalignedPages);
       ASSERT_EFI_ERROR (Status);
     }
-    Memory         = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);
+    Memory = (EFI_PHYSICAL_ADDRESS) (AlignedMemory + EFI_PAGES_TO_SIZE 
(Pages));
     UnalignedPages = RealPages - Pages - UnalignedPages;
     if (UnalignedPages > 0) {
       //
       // Free last unaligned page(s).
       //
-      Status = gBS->FreePages (Memory, UnalignedPages);
+      Status = gMmst->MmFreePages (Memory, UnalignedPages);
       ASSERT_EFI_ERROR (Status);
     }
   } else {
     //
     // Do not over-allocate pages in this case.
     //
-    Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);
+    Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, Pages, 
&Memory);
     if (EFI_ERROR (Status)) {
       return NULL;
     }
@@ -263,7 +266,7 @@ AllocateAlignedPages (
   IN UINTN  Alignment
   )
 {
-  return InternalAllocateAlignedPages (EfiBootServicesData, Pages, Alignment);
+  return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, 
Alignment);
 }
 
 /**
@@ -319,7 +322,7 @@ AllocateAlignedReservedPages (
   IN UINTN  Alignment
   )
 {
-  return InternalAllocateAlignedPages (EfiReservedMemoryType, Pages, 
Alignment);
+  return NULL;
 }
 
 /**
@@ -335,7 +338,7 @@ AllocateAlignedReservedPages (
   Library, then ASSERT().
   If Pages is zero, then ASSERT().
 
-  @param  Buffer                The pointer to the buffer of pages to free.
+  @param  Buffer                Pointer to the buffer of pages to free.
   @param  Pages                 The number of 4 KB pages to free.
 
 **/
@@ -349,7 +352,7 @@ FreeAlignedPages (
   EFI_STATUS  Status;
 
   ASSERT (Pages != 0);
-  Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);
+  Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);
   ASSERT_EFI_ERROR (Status);
 }
 
@@ -375,7 +378,9 @@ InternalAllocatePool (
   EFI_STATUS  Status;
   VOID        *Memory;
 
-  Status = gBS->AllocatePool (MemoryType, AllocationSize, &Memory);
+  Memory = NULL;
+
+  Status = gMmst->MmAllocatePool (MemoryType, AllocationSize, &Memory);
   if (EFI_ERROR (Status)) {
     Memory = NULL;
   }
@@ -400,7 +405,7 @@ AllocatePool (
   IN UINTN  AllocationSize
   )
 {
-  return InternalAllocatePool (EfiBootServicesData, AllocationSize);
+  return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
 }
 
 /**
@@ -442,7 +447,7 @@ AllocateReservedPool (
   IN UINTN  AllocationSize
   )
 {
-  return InternalAllocatePool (EfiReservedMemoryType, AllocationSize);
+  return NULL;
 }
 
 /**
@@ -493,7 +498,7 @@ AllocateZeroPool (
   IN UINTN  AllocationSize
   )
 {
-  return InternalAllocateZeroPool (EfiBootServicesData, AllocationSize);
+  return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
 }
 
 /**
@@ -537,7 +542,7 @@ AllocateReservedZeroPool (
   IN UINTN  AllocationSize
   )
 {
-  return InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize);
+  return NULL;
 }
 
 /**
@@ -571,7 +576,7 @@ InternalAllocateCopyPool (
 
   Memory = InternalAllocatePool (PoolType, AllocationSize);
   if (Memory != NULL) {
-     Memory = CopyMem (Memory, Buffer, AllocationSize);
+    Memory = CopyMem (Memory, Buffer, AllocationSize);
   }
   return Memory;
 }
@@ -600,7 +605,7 @@ AllocateCopyPool (
   IN CONST VOID  *Buffer
   )
 {
-  return InternalAllocateCopyPool (EfiBootServicesData, AllocationSize, 
Buffer);
+  return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, 
Buffer);
 }
 
 /**
@@ -654,7 +659,7 @@ AllocateReservedCopyPool (
   IN CONST VOID  *Buffer
   )
 {
-  return InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, 
Buffer);
+  return NULL;
 }
 
 /**
@@ -726,7 +731,7 @@ ReallocatePool (
   IN VOID   *OldBuffer  OPTIONAL
   )
 {
-  return InternalReallocatePool (EfiBootServicesData, OldSize, NewSize, 
OldBuffer);
+  return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, 
OldBuffer);
 }
 
 /**
@@ -790,7 +795,7 @@ ReallocateReservedPool (
   IN VOID   *OldBuffer  OPTIONAL
   )
 {
-  return InternalReallocatePool (EfiReservedMemoryType, OldSize, NewSize, 
OldBuffer);
+  return NULL;
 }
 
 /**
@@ -804,7 +809,7 @@ ReallocateReservedPool (
   If Buffer was not allocated with a pool allocation function in the Memory 
Allocation Library,
   then ASSERT().
 
-  @param  Buffer                The pointer to the buffer to free.
+  @param  Buffer                Pointer to the buffer to free.
 
 **/
 VOID
@@ -815,7 +820,89 @@ FreePool (
 {
   EFI_STATUS    Status;
 
-  Status = gBS->FreePool (Buffer);
+  Status = gMmst->MmFreePool (Buffer);
   ASSERT_EFI_ERROR (Status);
 }
 
+/**
+  The constructor function calls MmInitializeMemoryServices to initialize
+  memory in MMRAM and caches EFI_MM_SYSTEM_TABLE pointer.
+
+  @param  ImageHandle   The firmware allocated handle for the EFI image.
+  @param  SystemTable   A pointer to the Management mode System Table.
+
+  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
+
+**/
+EFI_STATUS
+EFIAPI
+MemoryAllocationLibConstructor (
+  IN EFI_HANDLE             ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
+  )
+{
+  MM_CORE_PRIVATE_DATA           *MmCorePrivate;
+  EFI_HOB_GUID_TYPE               *GuidHob;
+  MM_CORE_DATA_HOB_DATA          *DataInHob;
+  VOID                            *HobStart;
+  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
+  EFI_MMRAM_DESCRIPTOR            *MmramRanges;
+  UINT32                           MmramRangeCount;
+  EFI_HOB_GUID_TYPE               *MmramRangesHob;
+
+  HobStart = GetHobList ();
+  DEBUG ((DEBUG_INFO, "StandaloneMmCoreMemoryAllocationLibConstructor - 
0x%x\n", HobStart));
+
+  //
+  // Extract MM Core Private context from the Hob. If absent search for
+  // a Hob containing the MMRAM ranges
+  //
+  GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
+  if (GuidHob == NULL) {
+    MmramRangesHob = GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, 
HobStart);
+    if (MmramRangesHob == NULL) {
+      return EFI_UNSUPPORTED;
+    }
+
+    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
+    if (MmramRangesHobData == NULL) {
+      return EFI_UNSUPPORTED;
+    }
+
+    MmramRanges = MmramRangesHobData->Descriptor;
+    if (MmramRanges == NULL) {
+      return EFI_UNSUPPORTED;
+    }
+
+    MmramRangeCount = MmramRangesHobData->NumberOfMmReservedRegions;
+    if (MmramRanges == NULL) {
+      return EFI_UNSUPPORTED;
+    }
+
+  } else {
+    DataInHob      = GET_GUID_HOB_DATA (GuidHob);
+    MmCorePrivate = (MM_CORE_PRIVATE_DATA *)(UINTN)DataInHob->Address;
+    MmramRanges     = (EFI_MMRAM_DESCRIPTOR 
*)(UINTN)MmCorePrivate->MmramRanges;
+    MmramRangeCount = MmCorePrivate->MmramRangeCount;
+  }
+
+  {
+    UINTN                Index;
+
+    DEBUG ((DEBUG_INFO, "MmramRangeCount - 0x%x\n", MmramRangeCount));
+    for (Index = 0; Index < MmramRangeCount; Index++) {
+      DEBUG ((DEBUG_INFO, "MmramRanges[%d]: 0x%016lx - 0x%016lx\n",
+              Index, MmramRanges[Index].CpuStart, 
MmramRanges[Index].PhysicalSize));
+    }
+  }
+
+  //
+  // Initialize memory service using free MMRAM
+  //
+  DEBUG ((DEBUG_INFO, "MmInitializeMemoryServices\n"));
+  MmInitializeMemoryServices ((UINTN)MmramRangeCount, (VOID 
*)(UINTN)MmramRanges);
+
+  // Initialize MM Services Table
+  gMmst = MmSystemTable;
+  return EFI_SUCCESS;
+}
diff --git 
a/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.inf
 
b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.inf
new file mode 100644
index 000000000000..3958655cb4cf
--- /dev/null
+++ 
b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.inf
@@ -0,0 +1,49 @@
+## @file
+# Memory Allocation Library instance dedicated to MM Core.
+# The implementation borrows the MM Core Memory Allocation services as the 
primitive
+# for memory allocation instead of using MM System Table servces in an 
indirect way.
+# It is assumed that this library instance must be linked with MM Core in this 
package.
+#
+# Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
+#
+#  This program and the accompanying materials
+#  are licensed and made available under the terms and conditions of the BSD 
License
+#  which accompanies this distribution. The full text of the license may be 
found at
+#  http://opensource.org/licenses/bsd-license.php
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR 
IMPLIED.
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001A
+  BASE_NAME                      = MemoryAllocationLib
+  FILE_GUID                      = DCDCBE1D-E760-4E1D-85B4-96E3F0439C41
+  MODULE_TYPE                    = MM_CORE_STANDALONE
+  VERSION_STRING                 = 1.0
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  LIBRARY_CLASS                  = MemoryAllocationLib|MM_CORE_STANDALONE
+  CONSTRUCTOR                    = MemoryAllocationLibConstructor
+
+#
+# The following information is for reference only and not required by the 
build tools.
+#
+#  VALID_ARCHITECTURES           = IA32 X64
+#
+
+[Sources]
+  StandaloneMmCoreMemoryAllocationLib.c
+  StandaloneMmCoreMemoryAllocationServices.h
+
+[Packages]
+  MdePkg/MdePkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
+
+[LibraryClasses]
+  BaseMemoryLib
+  DebugLib
+  HobLib
+
+[Guids]
+  gEfiMmPeiMmramMemoryReserveGuid
diff --git 
a/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationServices.h
 
b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationServices.h
new file mode 100644
index 000000000000..07abaac2a866
--- /dev/null
+++ 
b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationServices.h
@@ -0,0 +1,38 @@
+/** @file
+  Contains function prototypes for Memory Services in the MM Core.
+
+  This header file borrows the StandaloneMmCore Memory Allocation services as 
the primitive
+  for memory allocation.
+
+  Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
+
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD 
License
+  which accompanies this distribution.  The full text of the license may be 
found at
+  http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef _PI_MM_CORE_MEMORY_ALLOCATION_SERVICES_H_
+#define _PI_MM_CORE_MEMORY_ALLOCATION_SERVICES_H_
+
+#include <Guid/MmCoreData.h>
+
+/**
+  Called to initialize the memory service.
+
+  @param   MmramRangeCount       Number of MMRAM Regions
+  @param   MmramRanges           Pointer to MMRAM Descriptors
+
+**/
+VOID
+MmInitializeMemoryServices (
+  IN UINTN                 MmramRangeCount,
+  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
+  );
+
+#endif
-- 
2.7.4

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to