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