TianoCore EDK2 master
Loading...
Searching...
No Matches
Memory.c
Go to the documentation of this file.
1
15//
16// The package level header files this module uses
17//
18#include <PiPei.h>
19
20//
21// The Library classes this module consumes
22//
24#include <Library/DebugLib.h>
25#include <Library/HobLib.h>
26#include <Library/IoLib.h>
27#include <Library/PcdLib.h>
31#include <Library/PrePiLib.h>
32#include <libfdt.h>
33#include <Guid/FdtHob.h>
34
35VOID
37 VOID
38 );
39
49VOID
51 IN EFI_PHYSICAL_ADDRESS MemoryBase,
52 IN UINT64 MemorySize
53 )
54{
56 EFI_RESOURCE_SYSTEM_MEMORY,
57 EFI_RESOURCE_ATTRIBUTE_PRESENT |
58 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
59 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
60 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
61 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
62 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
63 EFI_RESOURCE_ATTRIBUTE_TESTED,
64 MemoryBase,
65 MemorySize
66 );
67}
68
78VOID
80 IN EFI_PHYSICAL_ADDRESS MemoryBase,
81 IN EFI_PHYSICAL_ADDRESS MemoryLimit
82 )
83{
84 AddMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase));
85}
86
92VOID
94 IN EFI_PHYSICAL_ADDRESS SystemMemoryBase,
95 IN UINT64 SystemMemorySize
96 )
97{
99 SystemMemoryBase,
100 SystemMemoryBase + SystemMemorySize
101 );
102}
103
112STATIC
113INT32
115 IN VOID *Fdt,
116 IN INT32 Node,
117 IN CONST CHAR8 *Name
118 )
119{
120 CONST INT32 *Prop;
121 INT32 Len;
122 UINT32 Val;
123
124 Prop = fdt_getprop (Fdt, Node, Name, &Len);
125 if (Prop == NULL) {
126 return Len;
127 }
128
129 if (Len != sizeof (*Prop)) {
130 return -FDT_ERR_BADNCELLS;
131 }
132
133 Val = fdt32_to_cpu (*Prop);
134 if (Val > FDT_MAX_NCELLS) {
135 return -FDT_ERR_BADNCELLS;
136 }
137
138 return (INT32)Val;
139}
140
151STATIC
152VOID
154 IN VOID *FdtPointer
155 )
156{
157 CONST INT32 *RegProp;
158 INT32 Node;
159 INT32 SubNode;
160 INT32 Len;
162 UINT64 Size;
163 INTN NumRsv, i;
164 INT32 NumAddrCells, NumSizeCells;
165
166 NumRsv = fdt_num_mem_rsv (FdtPointer);
167
168 /* Look for an existing entry and add it to the efi mem map. */
169 for (i = 0; i < NumRsv; i++) {
170 if (fdt_get_mem_rsv (FdtPointer, i, &Addr, &Size) != 0) {
171 continue;
172 }
173
175 Addr,
176 Size,
178 );
179 }
180
181 /* process reserved-memory */
182 Node = fdt_subnode_offset (FdtPointer, 0, "reserved-memory");
183 if (Node >= 0) {
184 NumAddrCells = GetNumCells (FdtPointer, Node, "#address-cells");
185 if (NumAddrCells <= 0) {
186 return;
187 }
188
189 NumSizeCells = GetNumCells (FdtPointer, Node, "#size-cells");
190 if (NumSizeCells <= 0) {
191 return;
192 }
193
194 fdt_for_each_subnode (SubNode, FdtPointer, Node) {
195 RegProp = fdt_getprop (FdtPointer, SubNode, "reg", &Len);
196
197 if ((RegProp != 0) && (Len == ((NumAddrCells + NumSizeCells) * sizeof (INT32)))) {
198 Addr = fdt32_to_cpu (RegProp[0]);
199
200 if (NumAddrCells > 1) {
201 Addr = (Addr << 32) | fdt32_to_cpu (RegProp[1]);
202 }
203
204 RegProp += NumAddrCells;
205 Size = fdt32_to_cpu (RegProp[0]);
206
207 if (NumSizeCells > 1) {
208 Size = (Size << 32) | fdt32_to_cpu (RegProp[1]);
209 }
210
211 DEBUG ((
212 DEBUG_INFO,
213 "%a: Adding Reserved Memory Addr = 0x%llx, Size = 0x%llx\n",
214 __func__,
215 Addr,
216 Size
217 ));
218
219 if (fdt_getprop (FdtPointer, SubNode, "no-map", &Len)) {
221 Addr,
222 Size,
224 );
225 } else {
227 Addr,
228 Size,
230 );
231 }
232 }
233 }
234 }
235}
236
245 VOID
246 )
247{
248 EFI_RISCV_FIRMWARE_CONTEXT *FirmwareContext;
249 CONST UINT64 *RegProp;
250 CONST CHAR8 *Type;
251 UINT64 CurBase, CurSize;
252 INT32 Node, Prev;
253 INT32 Len;
254 VOID *FdtPointer;
255
256 FirmwareContext = NULL;
257 GetFirmwareContextPointer (&FirmwareContext);
258
259 if (FirmwareContext == NULL) {
260 DEBUG ((DEBUG_ERROR, "%a: Firmware Context is NULL\n", __func__));
261 return EFI_UNSUPPORTED;
262 }
263
264 FdtPointer = (VOID *)FirmwareContext->FlattenedDeviceTree;
265 if (FdtPointer == NULL) {
266 DEBUG ((DEBUG_ERROR, "%a: Invalid FDT pointer\n", __func__));
267 return EFI_UNSUPPORTED;
268 }
269
270 // Look for the lowest memory node
271 for (Prev = 0; ; Prev = Node) {
272 Node = fdt_next_node (FdtPointer, Prev, NULL);
273 if (Node < 0) {
274 break;
275 }
276
277 // Check for memory node
278 Type = fdt_getprop (FdtPointer, Node, "device_type", &Len);
279 if (Type && (AsciiStrnCmp (Type, "memory", Len) == 0)) {
280 // Get the 'reg' property of this node. For now, we will assume
281 // two 8 byte quantities for base and size, respectively.
282 RegProp = fdt_getprop (FdtPointer, Node, "reg", &Len);
283 if ((RegProp != 0) && (Len == (2 * sizeof (UINT64)))) {
284 CurBase = fdt64_to_cpu (ReadUnaligned64 (RegProp));
285 CurSize = fdt64_to_cpu (ReadUnaligned64 (RegProp + 1));
286
287 DEBUG ((
288 DEBUG_INFO,
289 "%a: System RAM @ 0x%lx - 0x%lx\n",
290 __func__,
291 CurBase,
292 CurBase + CurSize - 1
293 ));
294
296 CurBase,
297 CurSize
298 );
299 } else {
300 DEBUG ((
301 DEBUG_ERROR,
302 "%a: Failed to parse FDT memory node\n",
303 __func__
304 ));
305 }
306 }
307 }
308
309 AddReservedMemoryMap (FdtPointer);
310
311 /* Make sure SEC is booting with bare mode */
312 ASSERT ((RiscVGetSupervisorAddressTranslationRegister () & SATP64_MODE) == (SATP_MODE_OFF << SATP64_MODE_SHIFT));
313
315
316 return EFI_SUCCESS;
317}
INT64 INTN
VOID EFIAPI BuildResourceDescriptorHob(IN EFI_RESOURCE_TYPE ResourceType, IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute, IN EFI_PHYSICAL_ADDRESS PhysicalStart, IN UINT64 NumberOfBytes)
Definition: HobLib.c:299
VOID EFIAPI BuildMemoryAllocationHob(IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN EFI_MEMORY_TYPE MemoryType)
Definition: HobLib.c:601
UINT64 EFIAPI ReadUnaligned64(IN CONST UINT64 *Buffer)
Definition: Unaligned.c:204
INTN EFIAPI AsciiStrnCmp(IN CONST CHAR8 *FirstString, IN CONST CHAR8 *SecondString, IN UINTN Length)
Definition: String.c:872
VOID EFIAPI GetFirmwareContextPointer(IN OUT EFI_RISCV_FIRMWARE_CONTEXT **FirmwareContextPtr)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define IN
Definition: Base.h:279
#define DEBUG(Expression)
Definition: DebugLib.h:434
STATIC VOID AddReservedMemoryMap(IN VOID *FdtPointer)
Definition: Memory.c:153
STATIC INT32 GetNumCells(IN VOID *Fdt, IN INT32 Node, IN CONST CHAR8 *Name)
Definition: Memory.c:114
STATIC VOID AddMemoryRangeHob(IN EFI_PHYSICAL_ADDRESS MemoryBase, IN EFI_PHYSICAL_ADDRESS MemoryLimit)
Definition: Memory.c:79
VOID BuildMemoryTypeInformationHob(VOID)
EFI_STATUS MemoryPeimInitialization(VOID)
Definition: Memory.c:244
STATIC VOID InitializeRamRegions(IN EFI_PHYSICAL_ADDRESS SystemMemoryBase, IN UINT64 SystemMemorySize)
Definition: Memory.c:93
STATIC VOID AddMemoryBaseSizeHob(IN EFI_PHYSICAL_ADDRESS MemoryBase, IN UINT64 MemorySize)
Definition: Memory.c:50
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
@ EfiBootServicesData
@ EfiReservedMemoryType