]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/MemoryInitPei/MemoryInitPeiLib.c
ArmPkg: Move ARM Platform drivers from ArmPkg/Drivers/ to ArmPlatformPkg/Drivers/
[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_STATUS Status;
71 EFI_RESOURCE_ATTRIBUTE_TYPE Attributes;
72 ARM_SYSTEM_MEMORY_REGION_DESCRIPTOR* EfiMemoryMap;
73 UINTN Index;
74 EFI_PHYSICAL_ADDRESS SystemMemoryTop;
75
76 // Ensure PcdSystemMemorySize has been set
77 ASSERT (PcdGet32 (PcdSystemMemorySize) != 0);
78
79 SystemMemoryTop = (EFI_PHYSICAL_ADDRESS)((UINT32)PcdGet32 (PcdSystemMemoryBase) + (UINT32)PcdGet32 (PcdSystemMemorySize));
80
81 //
82 // Now, the permanent memory has been installed, we can call AllocatePages()
83 //
84 Attributes = (
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 // If it is not a standalone build we must reserved the space above the base address of the firmware volume
95 if (!PcdGet32(PcdStandalone)) {
96 // Check if firmware volume has not be copied at the top of DRAM then we must reserve the extra space
97 // between the firmware and the top
98 if (SystemMemoryTop != PcdGet32 (PcdNormalFdBaseAddress) + PcdGet32 (PcdNormalFdSize)) {
99 BuildResourceDescriptorHob (
100 EFI_RESOURCE_SYSTEM_MEMORY,
101 Attributes & (~EFI_RESOURCE_ATTRIBUTE_TESTED),
102 PcdGet32 (PcdNormalFdBaseAddress) + PcdGet32 (PcdNormalFdSize),
103 SystemMemoryTop - (PcdGet32 (PcdNormalFdBaseAddress) + PcdGet32 (PcdNormalFdSize))
104 );
105 }
106
107 // Reserved the memory space occupied by the firmware volume
108 BuildResourceDescriptorHob (
109 EFI_RESOURCE_SYSTEM_MEMORY,
110 Attributes & (~EFI_RESOURCE_ATTRIBUTE_PRESENT),
111 (UINT32)PcdGet32 (PcdNormalFdBaseAddress),
112 (UINT32)PcdGet32 (PcdNormalFdSize)
113 );
114 }
115
116 // Check there is no overlap between UEFI and Fix Address Regions
117 ASSERT (PcdGet32 (PcdSystemMemoryBase) + PcdGet32 (PcdSystemMemoryFixRegionSize) <= UefiMemoryBase);
118
119 // Reserved the UEFI Memory Region
120 BuildResourceDescriptorHob (
121 EFI_RESOURCE_SYSTEM_MEMORY,
122 Attributes,
123 UefiMemoryBase,
124 UefiMemorySize
125 );
126
127 // Reserved the Fix Address Region
128 BuildResourceDescriptorHob (
129 EFI_RESOURCE_SYSTEM_MEMORY,
130 Attributes,
131 PcdGet32 (PcdSystemMemoryBase),
132 PcdGet32 (PcdSystemMemoryFixRegionSize)
133 );
134
135 // Reserved the memory between UEFI and Fix Address regions
136 if (PcdGet32 (PcdSystemMemoryBase) + PcdGet32 (PcdSystemMemoryFixRegionSize) != UefiMemoryBase) {
137 BuildResourceDescriptorHob (
138 EFI_RESOURCE_SYSTEM_MEMORY,
139 Attributes & (~EFI_RESOURCE_ATTRIBUTE_TESTED),
140 PcdGet32 (PcdSystemMemoryBase) + PcdGet32 (PcdSystemMemoryFixRegionSize),
141 UefiMemoryBase - (PcdGet32 (PcdSystemMemoryBase) + PcdGet32 (PcdSystemMemoryFixRegionSize))
142 );
143 }
144
145 // If a platform has system memory extensions, it can declare those in this function
146 Status = ArmPlatformGetAdditionalSystemMemory (&EfiMemoryMap);
147 if (!EFI_ERROR(Status)) {
148 // Install the EFI Memory Map
149 for (Index = 0; EfiMemoryMap[Index].ResourceAttribute != 0; Index++) {
150 BuildResourceDescriptorHob (
151 EFI_RESOURCE_SYSTEM_MEMORY,
152 EfiMemoryMap[Index].ResourceAttribute,
153 EfiMemoryMap[Index].PhysicalStart,
154 EfiMemoryMap[Index].NumberOfBytes
155 );
156 }
157 FreePool (EfiMemoryMap);
158 }
159
160 // Build Memory Allocation Hob
161 InitMmu ();
162
163 if (FeaturePcdGet (PcdPrePiProduceMemoryTypeInformationHob)) {
164 // Optional feature that helps prevent EFI memory map fragmentation.
165 BuildMemoryTypeInformationHob ();
166 }
167
168 return EFI_SUCCESS;
169 }