]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/MemoryInitPei/MemoryInitPeiLib.c
ArmPlatformPkg: Change the memory model for the ARM Platform components
[mirror_edk2.git] / ArmPlatformPkg / MemoryInitPei / MemoryInitPeiLib.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 #include <PiPei.h>
16
17 #include <Library/ArmPlatformLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/HobLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/PcdLib.h>
22
23 #include <Chipset/ArmV7.h>
24
25 VOID
26 BuildMemoryTypeInformationHob (
27 VOID
28 );
29
30 VOID
31 InitMmu (
32 VOID
33 )
34 {
35 ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable;
36 VOID *TranslationTableBase;
37 UINTN TranslationTableSize;
38
39 // Get Virtual Memory Map from the Platform Library
40 ArmPlatformGetVirtualMemoryMap(&MemoryTable);
41
42 //Note: Because we called PeiServicesInstallPeiMemory() before to call InitMmu() the MMU Page Table resides in
43 // DRAM (even at the top of DRAM as it is the first permanent memory allocation)
44 ArmConfigureMmu (MemoryTable, &TranslationTableBase, &TranslationTableSize);
45 }
46
47 /*++
48
49 Routine Description:
50
51
52
53 Arguments:
54
55 FileHandle - Handle of the file being invoked.
56 PeiServices - Describes the list of possible PEI Services.
57
58 Returns:
59
60 Status - EFI_SUCCESS if the boot mode could be set
61
62 --*/
63 EFI_STATUS
64 EFIAPI
65 MemoryPeim (
66 IN EFI_PHYSICAL_ADDRESS UefiMemoryBase,
67 IN UINT64 UefiMemorySize
68 )
69 {
70 EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttributes;
71 UINT64 ResourceLength;
72 EFI_PEI_HOB_POINTERS NextHob;
73 EFI_PHYSICAL_ADDRESS FdTop;
74 EFI_PHYSICAL_ADDRESS SystemMemoryTop;
75 EFI_PHYSICAL_ADDRESS ResourceTop;
76 BOOLEAN Found;
77
78 // Ensure PcdSystemMemorySize has been set
79 ASSERT (PcdGet32 (PcdSystemMemorySize) != 0);
80
81 //
82 // Now, the permanent memory has been installed, we can call AllocatePages()
83 //
84 ResourceAttributes = (
85 EFI_RESOURCE_ATTRIBUTE_PRESENT |
86 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
87 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
88 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
89 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
90 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
91 EFI_RESOURCE_ATTRIBUTE_TESTED
92 );
93
94 // Reserved the memory space occupied by the firmware volume
95 BuildResourceDescriptorHob (
96 EFI_RESOURCE_SYSTEM_MEMORY,
97 ResourceAttributes,
98 PcdGet32 (PcdSystemMemoryBase),
99 PcdGet32 (PcdSystemMemorySize)
100 );
101
102 SystemMemoryTop = PcdGet32 (PcdSystemMemoryBase) + PcdGet32 (PcdSystemMemorySize);
103 FdTop = PcdGet32(PcdNormalFdBaseAddress) + PcdGet32(PcdNormalFdSize);
104
105 // EDK2 does not have the concept of boot firmware copied into DRAM. To avoid the DXE
106 // core to overwrite this area we must mark the region with the attribute non-present
107 if ((PcdGet32 (PcdNormalFdBaseAddress) >= PcdGet32 (PcdSystemMemoryBase)) && (FdTop <= SystemMemoryTop)) {
108 Found = FALSE;
109
110 // Search for System Memory Hob that contains the firmware
111 NextHob.Raw = GetHobList ();
112 while ((NextHob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, NextHob.Raw)) != NULL) {
113 if ((NextHob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) &&
114 (PcdGet32(PcdNormalFdBaseAddress) >= NextHob.ResourceDescriptor->PhysicalStart) &&
115 (FdTop <= NextHob.ResourceDescriptor->PhysicalStart + NextHob.ResourceDescriptor->ResourceLength))
116 {
117 ResourceAttributes = NextHob.ResourceDescriptor->ResourceAttribute;
118 ResourceLength = NextHob.ResourceDescriptor->ResourceLength;
119 ResourceTop = NextHob.ResourceDescriptor->PhysicalStart + ResourceLength;
120
121 if (PcdGet32(PcdNormalFdBaseAddress) == NextHob.ResourceDescriptor->PhysicalStart) {
122 if (SystemMemoryTop == FdTop) {
123 NextHob.ResourceDescriptor->ResourceAttribute = ResourceAttributes & ~EFI_RESOURCE_ATTRIBUTE_PRESENT;
124 } else {
125 // Create the System Memory HOB for the firmware with the non-present attribute
126 BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,
127 ResourceAttributes & ~EFI_RESOURCE_ATTRIBUTE_PRESENT,
128 PcdGet32(PcdNormalFdBaseAddress),
129 PcdGet32(PcdNormalFdSize));
130
131 // Top of the FD is system memory available for UEFI
132 NextHob.ResourceDescriptor->PhysicalStart += PcdGet32(PcdNormalFdSize);
133 NextHob.ResourceDescriptor->ResourceLength -= PcdGet32(PcdNormalFdSize);
134 }
135 } else {
136 // Create the System Memory HOB for the firmware with the non-present attribute
137 BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,
138 ResourceAttributes & ~EFI_RESOURCE_ATTRIBUTE_PRESENT,
139 PcdGet32(PcdNormalFdBaseAddress),
140 PcdGet32(PcdNormalFdSize));
141
142 // Update the HOB
143 NextHob.ResourceDescriptor->ResourceLength = PcdGet32(PcdNormalFdBaseAddress) - NextHob.ResourceDescriptor->PhysicalStart;
144
145 // If there is some memory available on the top of the FD then create a HOB
146 if (FdTop < NextHob.ResourceDescriptor->PhysicalStart + ResourceLength) {
147 // Create the System Memory HOB for the remaining region (top of the FD)
148 BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,
149 ResourceAttributes,
150 FdTop,
151 ResourceTop - FdTop);
152 }
153 }
154 Found = TRUE;
155 break;
156 }
157 NextHob.Raw = GET_NEXT_HOB (NextHob);
158 }
159
160 ASSERT(Found);
161 }
162
163 // Build Memory Allocation Hob
164 InitMmu ();
165
166 if (FeaturePcdGet (PcdPrePiProduceMemoryTypeInformationHob)) {
167 // Optional feature that helps prevent EFI memory map fragmentation.
168 BuildMemoryTypeInformationHob ();
169 }
170
171 return EFI_SUCCESS;
172 }