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