]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/MemoryInitPei/MemoryInit.c
Add ArmPlatformPkg from ARM Ltd. patch.
[mirror_edk2.git] / ArmPlatformPkg / MemoryInitPei / MemoryInit.c
1 /** @file
2 *
3 * Copyright (c) 2011, ARM Limited. All rights reserved.
4 *
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
9 *
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 *
13 **/
14
15 //
16 // The package level header files this module uses
17 //
18 #include <PiPei.h>
19 //
20 // The protocols, PPI and GUID defintions for this module
21 //
22 #include <Ppi/MasterBootMode.h>
23 #include <Ppi/BootInRecoveryMode.h>
24 #include <Guid/MemoryTypeInformation.h>
25 //
26 // The Library classes this module consumes
27 //
28 #include <Library/DebugLib.h>
29 #include <Library/PeimEntryPoint.h>
30 #include <Library/PcdLib.h>
31 #include <Library/HobLib.h>
32 #include <Library/PeiServicesLib.h>
33 #include <Library/ArmLib.h>
34 #include <Library/IoLib.h>
35 #include <Library/ArmPlatformLib.h>
36
37 //
38 // Module globals
39 //
40
41 VOID
42 InitMmu (
43 VOID
44 )
45 {
46 ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable;
47 VOID *TranslationTableBase;
48 UINTN TranslationTableSize;
49
50 // Get Virtual Memory Map from the Platform Library
51 ArmPlatformGetVirtualMemoryMap(&MemoryTable);
52
53 //Note: Because we called PeiServicesInstallPeiMemory() before to call InitMmu() the MMU Page Table resides in
54 // DRAM (even at the top of DRAM as it is the first permanent memory allocation)
55 ArmConfigureMmu (MemoryTable, &TranslationTableBase, &TranslationTableSize);
56 }
57
58 // May want to put this into a library so you only need the PCD setings if you are using the feature?
59 VOID
60 BuildMemoryTypeInformationHob (
61 VOID
62 )
63 {
64 EFI_MEMORY_TYPE_INFORMATION Info[10];
65
66 Info[0].Type = EfiACPIReclaimMemory;
67 Info[0].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiACPIReclaimMemory);
68 Info[1].Type = EfiACPIMemoryNVS;
69 Info[1].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiACPIMemoryNVS);
70 Info[2].Type = EfiReservedMemoryType;
71 Info[2].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiReservedMemoryType);
72 Info[3].Type = EfiRuntimeServicesData;
73 Info[3].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiRuntimeServicesData);
74 Info[4].Type = EfiRuntimeServicesCode;
75 Info[4].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiRuntimeServicesCode);
76 Info[5].Type = EfiBootServicesCode;
77 Info[5].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiBootServicesCode);
78 Info[6].Type = EfiBootServicesData;
79 Info[6].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiBootServicesData);
80 Info[7].Type = EfiLoaderCode;
81 Info[7].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiLoaderCode);
82 Info[8].Type = EfiLoaderData;
83 Info[8].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiLoaderData);
84
85 // Terminator for the list
86 Info[9].Type = EfiMaxMemoryType;
87 Info[9].NumberOfPages = 0;
88
89
90 BuildGuidDataHob (&gEfiMemoryTypeInformationGuid, &Info, sizeof (Info));
91 }
92
93 EFI_STATUS
94 EFIAPI
95 InitializeMemory (
96 IN EFI_PEI_FILE_HANDLE FileHandle,
97 IN CONST EFI_PEI_SERVICES **PeiServices
98 )
99 /*++
100
101 Routine Description:
102
103
104
105 Arguments:
106
107 FileHandle - Handle of the file being invoked.
108 PeiServices - Describes the list of possible PEI Services.
109
110 Returns:
111
112 Status - EFI_SUCCESS if the boot mode could be set
113
114 --*/
115 {
116 EFI_STATUS Status;
117 ARM_SYSTEM_MEMORY_REGION_DESCRIPTOR* EfiMemoryMap;
118 UINTN PeiMemoryBase;
119 UINTN PeiMemorySize;
120 UINTN Index;
121
122 DEBUG ((EFI_D_ERROR, "Memory Init PEIM Loaded\n"));
123
124 // If it is not a standalone version, then we need to initialize the System Memory
125 // In case of a standalone version, the DRAM is already initialized
126 if (FeaturePcdGet(PcdStandalone)) {
127 // Initialize the System Memory controller (DRAM)
128 ArmPlatformInitializeSystemMemory();
129 }
130
131 // Install the Memory to PEI
132 ArmPlatformGetPeiMemory (&PeiMemoryBase,&PeiMemorySize);
133 Status = PeiServicesInstallPeiMemory (PeiMemoryBase,PeiMemorySize);
134 ASSERT_EFI_ERROR (Status);
135
136 //
137 // Now, the permanent memory has been installed, we can call AllocatePages()
138 //
139
140 ArmPlatformGetEfiMemoryMap (&EfiMemoryMap);
141
142 // Install the EFI Memory Map
143 for (Index = 0; EfiMemoryMap[Index].ResourceAttribute != 0; Index++) {
144 BuildResourceDescriptorHob (
145 EFI_RESOURCE_SYSTEM_MEMORY,
146 EfiMemoryMap[Index].ResourceAttribute,
147 EfiMemoryMap[Index].PhysicalStart,
148 EfiMemoryMap[Index].NumberOfBytes
149 );
150 }
151
152 // Build Memory Allocation Hob
153 InitMmu ();
154
155 if (FeaturePcdGet (PcdPrePiProduceMemoryTypeInformationHob)) {
156 // Optional feature that helps prevent EFI memory map fragmentation.
157 BuildMemoryTypeInformationHob ();
158 }
159
160 return Status;
161 }