ArmVirtPkg: ArmVirtPlatformLib: find the lowest memory node
[mirror_edk2.git] / ArmVirtPkg / Library / ArmVirtPlatformLib / Virt.c
CommitLineData
a36d531f
MC
1/** @file\r
2*\r
3* Copyright (c) 2011-2013, ARM Limited. All rights reserved.\r
4* Copyright (c) 2014, Linaro Limited. All rights reserved.\r
5* Copyright (c) 2014, Red Hat, Inc.\r
6*\r
7*\r
8* This program and the accompanying materials\r
9* are licensed and made available under the terms and conditions of the BSD License\r
10* which accompanies this distribution. The full text of the license may be found at\r
11* http://opensource.org/licenses/bsd-license.php\r
12*\r
13* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15*\r
16**/\r
17\r
18#include <Library/IoLib.h>\r
19#include <Library/ArmPlatformLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/PcdLib.h>\r
22#include <ArmPlatform.h>\r
23#include <libfdt.h>\r
24#include <Pi/PiBootMode.h>\r
25#include <Uefi/UefiBaseType.h>\r
26#include <Uefi/UefiMultiPhase.h>\r
a36d531f
MC
27\r
28/**\r
29 Return the current Boot Mode\r
30\r
31 This function returns the boot reason on the platform\r
32\r
33 @return Return the current Boot Mode of the platform\r
34\r
35**/\r
36EFI_BOOT_MODE\r
37ArmPlatformGetBootMode (\r
38 VOID\r
39 )\r
40{\r
41 return BOOT_WITH_FULL_CONFIGURATION;\r
42}\r
43\r
44/**\r
45 This function is called by PrePeiCore, in the SEC phase.\r
46**/\r
47RETURN_STATUS\r
48ArmPlatformInitialize (\r
49 IN UINTN MpId\r
50 )\r
51{\r
52 //\r
53 // We are relying on ArmPlatformInitializeSystemMemory () being called from\r
54 // InitializeMemory (), which only occurs if the following feature is disabled\r
55 //\r
56 ASSERT (!FeaturePcdGet (PcdSystemMemoryInitializeInSec));\r
57 return RETURN_SUCCESS;\r
58}\r
59\r
60/**\r
61 Initialize the system (or sometimes called permanent) memory\r
62\r
63 This memory is generally represented by the DRAM.\r
64\r
65 This function is called from InitializeMemory() in MemoryInitPeim, in the PEI\r
66 phase.\r
67**/\r
68VOID\r
69ArmPlatformInitializeSystemMemory (\r
70 VOID\r
71 )\r
72{\r
73 VOID *DeviceTreeBase;\r
74 INT32 Node, Prev;\r
45f9bb91
SZ
75 UINT64 NewBase, CurBase;\r
76 UINT64 NewSize, CurSize;\r
a36d531f 77 CONST CHAR8 *Type;\r
a36d531f
MC
78 INT32 Len;\r
79 CONST UINT64 *RegProp;\r
a36d531f
MC
80\r
81 NewBase = 0;\r
82 NewSize = 0;\r
83\r
ada518b7 84 DeviceTreeBase = (VOID *)(UINTN)PcdGet64 (PcdDeviceTreeInitialBaseAddress);\r
a36d531f
MC
85 ASSERT (DeviceTreeBase != NULL);\r
86\r
87 //\r
88 // Make sure we have a valid device tree blob\r
89 //\r
90 ASSERT (fdt_check_header (DeviceTreeBase) == 0);\r
91\r
92 //\r
45f9bb91 93 // Look for the lowest memory node\r
a36d531f 94 //\r
337d450e 95 for (Prev = 0;; Prev = Node) {\r
a36d531f
MC
96 Node = fdt_next_node (DeviceTreeBase, Prev, NULL);\r
97 if (Node < 0) {\r
98 break;\r
99 }\r
100\r
101 //\r
102 // Check for memory node\r
103 //\r
104 Type = fdt_getprop (DeviceTreeBase, Node, "device_type", &Len);\r
105 if (Type && AsciiStrnCmp (Type, "memory", Len) == 0) {\r
106 //\r
107 // Get the 'reg' property of this node. For now, we will assume\r
108 // two 8 byte quantities for base and size, respectively.\r
109 //\r
110 RegProp = fdt_getprop (DeviceTreeBase, Node, "reg", &Len);\r
111 if (RegProp != 0 && Len == (2 * sizeof (UINT64))) {\r
112\r
45f9bb91
SZ
113 CurBase = fdt64_to_cpu (ReadUnaligned64 (RegProp));\r
114 CurSize = fdt64_to_cpu (ReadUnaligned64 (RegProp + 1));\r
a36d531f
MC
115\r
116 DEBUG ((EFI_D_INFO, "%a: System RAM @ 0x%lx - 0x%lx\n",\r
45f9bb91
SZ
117 __FUNCTION__, CurBase, CurBase + CurSize - 1));\r
118\r
119 if (NewBase > CurBase || NewBase == 0) {\r
120 NewBase = CurBase;\r
121 NewSize = CurSize;\r
122 }\r
a36d531f
MC
123 } else {\r
124 DEBUG ((EFI_D_ERROR, "%a: Failed to parse FDT memory node\n",\r
125 __FUNCTION__));\r
126 }\r
a36d531f
MC
127 }\r
128 }\r
129\r
45f9bb91
SZ
130 //\r
131 // Make sure the start of DRAM matches our expectation\r
132 //\r
133 ASSERT (FixedPcdGet64 (PcdSystemMemoryBase) == NewBase);\r
134 PcdSet64 (PcdSystemMemorySize, NewSize);\r
135\r
a36d531f
MC
136 //\r
137 // We need to make sure that the machine we are running on has at least\r
138 // 128 MB of memory configured, and is currently executing this binary from\r
139 // NOR flash. This prevents a device tree image in DRAM from getting\r
140 // clobbered when our caller installs permanent PEI RAM, before we have a\r
141 // chance of marking its location as reserved or copy it to a freshly\r
142 // allocated block in the permanent PEI RAM in the platform PEIM.\r
143 //\r
144 ASSERT (NewSize >= SIZE_128MB);\r
145 ASSERT (\r
bb5420bb 146 (((UINT64)PcdGet64 (PcdFdBaseAddress) +\r
a36d531f 147 (UINT64)PcdGet32 (PcdFdSize)) <= NewBase) ||\r
bb5420bb 148 ((UINT64)PcdGet64 (PcdFdBaseAddress) >= (NewBase + NewSize)));\r
a36d531f
MC
149}\r
150\r
151VOID\r
152ArmPlatformGetPlatformPpiList (\r
153 OUT UINTN *PpiListSize,\r
154 OUT EFI_PEI_PPI_DESCRIPTOR **PpiList\r
155 )\r
156{\r
157 *PpiListSize = 0;\r
158 *PpiList = NULL;\r
159}\r