TianoCore EDK2 master
Loading...
Searching...
No Matches
CloudHvVirtMemInfoLib.c
Go to the documentation of this file.
1
9#include <PiPei.h>
10
11#include <Base.h>
12#include <libfdt.h>
13#include <Library/ArmLib.h>
15#include <Library/DebugLib.h>
17#include <Library/PcdLib.h>
18
19#include <Library/PrePiLib.h>
20
22
23CLOUDHV_MEM_NODE_INFO CloudHvMemNode[CLOUDHV_MAX_MEM_NODE_NUM];
24
33RETURN_STATUS
34EFIAPI
36 VOID
37 )
38{
39 VOID *DeviceTreeBase;
40 EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttributes;
41 INT32 Node, Prev;
42 UINT64 FirMemNodeBase, FirMemNodeSize;
43 UINT64 CurBase, MemBase;
44 UINT64 CurSize;
45 CONST CHAR8 *Type;
46 INT32 Len;
47 CONST UINT64 *RegProp;
48 RETURN_STATUS PcdStatus;
49 UINT8 Index;
50
51 ZeroMem (CloudHvMemNode, sizeof (CloudHvMemNode));
52
53 FirMemNodeBase = 0;
54 FirMemNodeSize = 0;
55 Index = 0;
56 MemBase = FixedPcdGet64 (PcdSystemMemoryBase);
57 ResourceAttributes = (
58 EFI_RESOURCE_ATTRIBUTE_PRESENT |
59 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
60 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
61 EFI_RESOURCE_ATTRIBUTE_TESTED
62 );
63 DeviceTreeBase = (VOID *)(UINTN)PcdGet64 (PcdDeviceTreeInitialBaseAddress);
64 if (DeviceTreeBase == NULL) {
65 return EFI_NOT_FOUND;
66 }
67
68 //
69 // Make sure we have a valid device tree blob
70 //
71 if (fdt_check_header (DeviceTreeBase) != 0) {
72 return EFI_NOT_FOUND;
73 }
74
75 //
76 // Look for the lowest memory node
77 //
78 for (Prev = 0; ; Prev = Node) {
79 Node = fdt_next_node (DeviceTreeBase, Prev, NULL);
80 if (Node < 0) {
81 break;
82 }
83
84 //
85 // Check for memory node
86 //
87 Type = fdt_getprop (DeviceTreeBase, Node, "device_type", &Len);
88 if ((Type != 0) && (AsciiStrnCmp (Type, "memory", Len) == 0)) {
89 //
90 // Get the 'reg' property of this node. For now, we will assume
91 // two 8 byte quantities for base and size, respectively.
92 //
93 RegProp = fdt_getprop (DeviceTreeBase, Node, "reg", &Len);
94 if ((RegProp != 0) && (Len == (2 * sizeof (UINT64)))) {
95 CurBase = fdt64_to_cpu (ReadUnaligned64 (RegProp));
96 CurSize = fdt64_to_cpu (ReadUnaligned64 (RegProp + 1));
97
98 DEBUG ((
99 DEBUG_INFO,
100 "%a: System RAM @ 0x%lx - 0x%lx\n",
101 __func__,
102 CurBase,
103 CurBase + CurSize - 1
104 ));
105
106 // We should build Hob seperately for the memory node except the first one
107 if (CurBase != MemBase) {
109 EFI_RESOURCE_SYSTEM_MEMORY,
110 ResourceAttributes,
111 CurBase,
112 CurSize
113 );
114 } else {
115 FirMemNodeBase = CurBase;
116 FirMemNodeSize = CurSize;
117 }
118
119 CloudHvMemNode[Index].Base = CurBase;
120 CloudHvMemNode[Index].Size = CurSize;
121 Index++;
122
123 if (Index >= CLOUDHV_MAX_MEM_NODE_NUM) {
124 DEBUG ((
125 DEBUG_WARN,
126 "%a: memory node larger than %d will not be included into Memory System\n",
127 __func__,
128 CLOUDHV_MAX_MEM_NODE_NUM
129 ));
130 break;
131 }
132 } else {
133 DEBUG ((
134 DEBUG_ERROR,
135 "%a: Failed to parse FDT memory node\n",
136 __func__
137 ));
138 }
139 }
140 }
141
142 //
143 // Make sure the start of DRAM matches our expectation
144 //
145 if (FixedPcdGet64 (PcdSystemMemoryBase) != FirMemNodeBase) {
146 return EFI_NOT_FOUND;
147 }
148
149 PcdStatus = PcdSet64S (PcdSystemMemorySize, FirMemNodeSize);
150 ASSERT_RETURN_ERROR (PcdStatus);
151 ASSERT (
152 (((UINT64)PcdGet64 (PcdFdBaseAddress) +
153 (UINT64)PcdGet32 (PcdFdSize)) <= FirMemNodeBase) ||
154 ((UINT64)PcdGet64 (PcdFdBaseAddress) >= (FirMemNodeBase + FirMemNodeSize))
155 );
156
157 return RETURN_SUCCESS;
158}
159
173VOID
175 OUT ARM_MEMORY_REGION_DESCRIPTOR **VirtualMemoryMap
176 )
177{
178 ARM_MEMORY_REGION_DESCRIPTOR *VirtualMemoryTable;
179 UINT8 Index, MemNodeIndex;
180
181 ASSERT (VirtualMemoryMap != NULL);
182
183 VirtualMemoryTable = AllocatePool (
185 MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS
186 );
187
188 if (VirtualMemoryTable == NULL) {
189 DEBUG ((DEBUG_ERROR, "%a: Error: Failed AllocatePool()\n", __func__));
190 return;
191 }
192
193 Index = 0;
194 MemNodeIndex = 0;
195 // System DRAM
196 while ((MemNodeIndex < CLOUDHV_MAX_MEM_NODE_NUM) && (CloudHvMemNode[MemNodeIndex].Size != 0)) {
197 VirtualMemoryTable[Index].PhysicalBase = CloudHvMemNode[MemNodeIndex].Base;
198 VirtualMemoryTable[Index].VirtualBase = CloudHvMemNode[MemNodeIndex].Base;
199 VirtualMemoryTable[Index].Length = CloudHvMemNode[MemNodeIndex].Size;
200 VirtualMemoryTable[Index].Attributes = ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK;
201
202 DEBUG ((
203 DEBUG_INFO,
204 "%a: Dumping System DRAM Memory Node%d Map:\n"
205 "\tPhysicalBase: 0x%lX\n"
206 "\tVirtualBase: 0x%lX\n"
207 "\tLength: 0x%lX\n",
208 __func__,
209 MemNodeIndex,
210 VirtualMemoryTable[Index].PhysicalBase,
211 VirtualMemoryTable[Index].VirtualBase,
212 VirtualMemoryTable[Index].Length
213 ));
214 Index++;
215 MemNodeIndex++;
216 }
217
218 // Memory mapped peripherals (UART, RTC, GIC, virtio-mmio, etc)
219 VirtualMemoryTable[Index].PhysicalBase = MACH_VIRT_PERIPH_BASE;
220 VirtualMemoryTable[Index].VirtualBase = MACH_VIRT_PERIPH_BASE;
221 VirtualMemoryTable[Index].Length = MACH_VIRT_PERIPH_SIZE;
222 VirtualMemoryTable[Index].Attributes = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
223 Index++;
224
225 // Map the FV region as normal executable memory
226 VirtualMemoryTable[Index].PhysicalBase = PcdGet64 (PcdFvBaseAddress);
227 VirtualMemoryTable[Index].VirtualBase = VirtualMemoryTable[Index].PhysicalBase;
228 VirtualMemoryTable[Index].Length = FixedPcdGet32 (PcdFvSize);
229 VirtualMemoryTable[Index].Attributes = ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK;
230 Index++;
231
232 // Memory mapped for 32bit device (like TPM)
233 VirtualMemoryTable[Index].PhysicalBase = TOP_32BIT_DEVICE_BASE;
234 VirtualMemoryTable[Index].VirtualBase = TOP_32BIT_DEVICE_BASE;
235 VirtualMemoryTable[Index].Length = TOP_32BIT_DEVICE_SIZE;
236 VirtualMemoryTable[Index].Attributes = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
237 Index++;
238
239 // End of Table
240 ZeroMem (&VirtualMemoryTable[Index], sizeof (ARM_MEMORY_REGION_DESCRIPTOR));
241
242 *VirtualMemoryMap = VirtualMemoryTable;
243}
UINT64 UINTN
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
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 ZeroMem(OUT VOID *Buffer, IN UINTN Length)
RETURN_STATUS EFIAPI CloudHvVirtMemInfoPeiLibConstructor(VOID)
VOID ArmVirtGetMemoryMap(OUT ARM_MEMORY_REGION_DESCRIPTOR **VirtualMemoryMap)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define RETURN_SUCCESS
Definition: Base.h:1066
#define OUT
Definition: Base.h:284
#define ASSERT_RETURN_ERROR(StatusParameter)
Definition: DebugLib.h:493
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define PcdGet64(TokenName)
Definition: PcdLib.h:375
#define FixedPcdGet32(TokenName)
Definition: PcdLib.h:92
#define FixedPcdGet64(TokenName)
Definition: PcdLib.h:106
#define PcdGet32(TokenName)
Definition: PcdLib.h:362
#define PcdSet64S(TokenName, Value)
Definition: PcdLib.h:511
UINT32 EFI_RESOURCE_ATTRIBUTE_TYPE
Definition: PiHob.h:241
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)