]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/MemoryInitPei/MemoryInitPeim.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / ArmPlatformPkg / MemoryInitPei / MemoryInitPeim.c
1 /** @file
2
3 Copyright (c) 2011, ARM Limited. All rights reserved.
4
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiPei.h>
10
11 //
12 // The protocols, PPI and GUID definitions for this module
13 //
14 #include <Ppi/MasterBootMode.h>
15 #include <Ppi/BootInRecoveryMode.h>
16 #include <Guid/MemoryTypeInformation.h>
17 //
18 // The Library classes this module consumes
19 //
20 #include <Library/ArmPlatformLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/HobLib.h>
23 #include <Library/PeimEntryPoint.h>
24 #include <Library/PeiServicesLib.h>
25 #include <Library/PcdLib.h>
26
27 EFI_STATUS
28 EFIAPI
29 MemoryPeim (
30 IN EFI_PHYSICAL_ADDRESS UefiMemoryBase,
31 IN UINT64 UefiMemorySize
32 );
33
34 // May want to put this into a library so you only need the PCD settings if you are using the feature?
35 VOID
36 BuildMemoryTypeInformationHob (
37 VOID
38 )
39 {
40 EFI_MEMORY_TYPE_INFORMATION Info[10];
41
42 Info[0].Type = EfiACPIReclaimMemory;
43 Info[0].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiACPIReclaimMemory);
44 Info[1].Type = EfiACPIMemoryNVS;
45 Info[1].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiACPIMemoryNVS);
46 Info[2].Type = EfiReservedMemoryType;
47 Info[2].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiReservedMemoryType);
48 Info[3].Type = EfiRuntimeServicesData;
49 Info[3].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiRuntimeServicesData);
50 Info[4].Type = EfiRuntimeServicesCode;
51 Info[4].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiRuntimeServicesCode);
52 Info[5].Type = EfiBootServicesCode;
53 Info[5].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiBootServicesCode);
54 Info[6].Type = EfiBootServicesData;
55 Info[6].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiBootServicesData);
56 Info[7].Type = EfiLoaderCode;
57 Info[7].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiLoaderCode);
58 Info[8].Type = EfiLoaderData;
59 Info[8].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiLoaderData);
60
61 // Terminator for the list
62 Info[9].Type = EfiMaxMemoryType;
63 Info[9].NumberOfPages = 0;
64
65 BuildGuidDataHob (&gEfiMemoryTypeInformationGuid, &Info, sizeof (Info));
66 }
67
68 /*++
69
70 Routine Description:
71
72
73
74 Arguments:
75
76 FileHandle - Handle of the file being invoked.
77 PeiServices - Describes the list of possible PEI Services.
78
79 Returns:
80
81 Status - EFI_SUCCESS if the boot mode could be set
82
83 --*/
84 EFI_STATUS
85 EFIAPI
86 InitializeMemory (
87 IN EFI_PEI_FILE_HANDLE FileHandle,
88 IN CONST EFI_PEI_SERVICES **PeiServices
89 )
90 {
91 EFI_STATUS Status;
92 UINTN SystemMemoryBase;
93 UINT64 SystemMemoryTop;
94 UINTN FdBase;
95 UINTN FdTop;
96 UINTN UefiMemoryBase;
97
98 DEBUG ((DEBUG_LOAD | DEBUG_INFO, "Memory Init PEIM Loaded\n"));
99
100 // Ensure PcdSystemMemorySize has been set
101 ASSERT (PcdGet64 (PcdSystemMemorySize) != 0);
102 ASSERT (PcdGet64 (PcdSystemMemoryBase) < (UINT64)MAX_ALLOC_ADDRESS);
103
104 SystemMemoryBase = (UINTN)PcdGet64 (PcdSystemMemoryBase);
105 SystemMemoryTop = SystemMemoryBase + PcdGet64 (PcdSystemMemorySize);
106 if (SystemMemoryTop - 1 > MAX_ALLOC_ADDRESS) {
107 SystemMemoryTop = (UINT64)MAX_ALLOC_ADDRESS + 1;
108 }
109
110 FdBase = (UINTN)PcdGet64 (PcdFdBaseAddress);
111 FdTop = FdBase + (UINTN)PcdGet32 (PcdFdSize);
112
113 //
114 // Declare the UEFI memory to PEI
115 //
116
117 // In case the firmware has been shadowed in the System Memory
118 if ((FdBase >= SystemMemoryBase) && (FdTop <= SystemMemoryTop)) {
119 // Check if there is enough space between the top of the system memory and the top of the
120 // firmware to place the UEFI memory (for PEI & DXE phases)
121 if (SystemMemoryTop - FdTop >= FixedPcdGet32 (PcdSystemMemoryUefiRegionSize)) {
122 UefiMemoryBase = SystemMemoryTop - FixedPcdGet32 (PcdSystemMemoryUefiRegionSize);
123 } else {
124 // Check there is enough space for the UEFI memory
125 ASSERT (SystemMemoryBase + FixedPcdGet32 (PcdSystemMemoryUefiRegionSize) <= FdBase);
126
127 UefiMemoryBase = FdBase - FixedPcdGet32 (PcdSystemMemoryUefiRegionSize);
128 }
129 } else {
130 // Check the Firmware does not overlapped with the system memory
131 ASSERT ((FdBase < SystemMemoryBase) || (FdBase >= SystemMemoryTop));
132 ASSERT ((FdTop <= SystemMemoryBase) || (FdTop > SystemMemoryTop));
133
134 UefiMemoryBase = SystemMemoryTop - FixedPcdGet32 (PcdSystemMemoryUefiRegionSize);
135 }
136
137 Status = PeiServicesInstallPeiMemory (UefiMemoryBase, FixedPcdGet32 (PcdSystemMemoryUefiRegionSize));
138 ASSERT_EFI_ERROR (Status);
139
140 // Initialize MMU and Memory HOBs (Resource Descriptor HOBs)
141 Status = MemoryPeim (UefiMemoryBase, FixedPcdGet32 (PcdSystemMemoryUefiRegionSize));
142 ASSERT_EFI_ERROR (Status);
143
144 return Status;
145 }