615c6dd0 |
1 | /** @file\r |
192f6d4c |
2 | \r |
3 | Copyright (c) 2006, Intel Corporation \r |
4 | All rights reserved. This program and the accompanying materials \r |
5 | are licensed and made available under the terms and conditions of the BSD License \r |
6 | which accompanies this distribution. The full text of the license may be found at \r |
7 | http://opensource.org/licenses/bsd-license.php \r |
8 | \r |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r |
11 | \r |
12 | Module Name:\r |
13 | \r |
14 | MemoryServices.c\r |
15 | \r |
16 | Abstract:\r |
17 | \r |
18 | EFI PEI Core memory services\r |
19 | \r |
615c6dd0 |
20 | **/\r |
192f6d4c |
21 | \r |
192f6d4c |
22 | #include <PeiMain.h>\r |
23 | \r |
24 | VOID\r |
25 | InitializeMemoryServices (\r |
b0d803fe |
26 | IN PEI_CORE_INSTANCE *PrivateData,\r |
5aae0aa7 |
27 | IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r |
192f6d4c |
28 | IN PEI_CORE_INSTANCE *OldCoreData\r |
29 | )\r |
30 | /*++\r |
31 | \r |
32 | Routine Description:\r |
33 | \r |
34 | Initialize the memory services.\r |
35 | \r |
36 | Arguments:\r |
37 | \r |
38 | PeiServices - The PEI core services table.\r |
5aae0aa7 |
39 | SecCoreData - Points to a data structure containing information about the PEI core's operating\r |
40 | environment, such as the size and location of temporary RAM, the stack location and\r |
41 | the BFV location.\r |
42 | \r |
192f6d4c |
43 | OldCoreData - Pointer to the PEI Core data.\r |
44 | NULL if being run in non-permament memory mode.\r |
45 | \r |
46 | Returns:\r |
47 | \r |
48 | None\r |
49 | \r |
50 | --*/\r |
51 | {\r |
58dcdada |
52 | \r |
53 | PrivateData->SwitchStackSignal = FALSE;\r |
192f6d4c |
54 | \r |
55 | if (OldCoreData == NULL) {\r |
56 | \r |
57 | PrivateData->PeiMemoryInstalled = FALSE;\r |
58 | \r |
58dcdada |
59 | PrivateData->BottomOfCarHeap = SecCoreData->PeiTemporaryRamBase; \r |
60 | PrivateData->TopOfCarHeap = (VOID *)((UINTN)(PrivateData->BottomOfCarHeap) + SecCoreData->PeiTemporaryRamSize);\r |
61 | PrivateData->SizeOfTemporaryMemory = SecCoreData->TemporaryRamSize;\r |
62 | PrivateData->StackSize = (UINT64) SecCoreData->StackSize;\r |
63 | \r |
192f6d4c |
64 | DEBUG_CODE_BEGIN ();\r |
5aae0aa7 |
65 | PrivateData->SizeOfCacheAsRam = SecCoreData->PeiTemporaryRamSize + SecCoreData->StackSize;\r |
66 | PrivateData->MaxTopOfCarHeap = (VOID *) ((UINTN) PrivateData->BottomOfCarHeap + (UINTN) PrivateData->SizeOfCacheAsRam);\r |
b0d803fe |
67 | PrivateData->StackBase = (EFI_PHYSICAL_ADDRESS) (UINTN) SecCoreData->StackBase;\r |
68 | PrivateData->StackSize = (UINT64) SecCoreData->StackSize;\r |
192f6d4c |
69 | DEBUG_CODE_END ();\r |
70 | \r |
71 | PrivateData->HobList.Raw = PrivateData->BottomOfCarHeap;\r |
72 | \r |
73 | PeiCoreBuildHobHandoffInfoTable (\r |
74 | BOOT_WITH_FULL_CONFIGURATION,\r |
75 | (EFI_PHYSICAL_ADDRESS) (UINTN) PrivateData->BottomOfCarHeap,\r |
5aae0aa7 |
76 | (UINTN) SecCoreData->PeiTemporaryRamSize\r |
192f6d4c |
77 | );\r |
192f6d4c |
78 | \r |
79 | //\r |
80 | // Set PS to point to ServiceTableShadow in Cache\r |
81 | //\r |
82 | PrivateData->PS = &(PrivateData->ServiceTableShadow);\r |
b0d803fe |
83 | }\r |
84 | \r |
192f6d4c |
85 | return;\r |
86 | }\r |
87 | \r |
88 | EFI_STATUS\r |
89 | EFIAPI\r |
90 | PeiInstallPeiMemory (\r |
58dcdada |
91 | IN CONST EFI_PEI_SERVICES **PeiServices,\r |
92 | IN EFI_PHYSICAL_ADDRESS MemoryBegin,\r |
93 | IN UINT64 MemoryLength\r |
192f6d4c |
94 | )\r |
95 | /*++\r |
96 | \r |
97 | Routine Description:\r |
98 | \r |
99 | Install the permanent memory is now available.\r |
100 | Creates HOB (PHIT and Stack).\r |
101 | \r |
102 | Arguments:\r |
103 | \r |
104 | PeiServices - The PEI core services table.\r |
105 | MemoryBegin - Start of memory address.\r |
106 | MemoryLength - Length of memory.\r |
107 | \r |
108 | Returns:\r |
109 | \r |
110 | Status - EFI_SUCCESS\r |
111 | \r |
112 | --*/\r |
113 | {\r |
114 | PEI_CORE_INSTANCE *PrivateData;\r |
58dcdada |
115 | \r |
a3a15d21 |
116 | DEBUG ((EFI_D_INFO, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin, MemoryLength));\r |
192f6d4c |
117 | PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r |
118 | \r |
58dcdada |
119 | PrivateData->PhysicalMemoryBegin = MemoryBegin;\r |
120 | PrivateData->PhysicalMemoryLength = MemoryLength;\r |
121 | PrivateData->FreePhysicalMemoryTop = MemoryBegin + MemoryLength;\r |
122 | \r |
123 | PrivateData->SwitchStackSignal = TRUE;\r |
192f6d4c |
124 | \r |
125 | return EFI_SUCCESS; \r |
126 | }\r |
127 | \r |
128 | EFI_STATUS\r |
129 | EFIAPI\r |
130 | PeiAllocatePages (\r |
0c2b5da8 |
131 | IN CONST EFI_PEI_SERVICES **PeiServices,\r |
192f6d4c |
132 | IN EFI_MEMORY_TYPE MemoryType,\r |
133 | IN UINTN Pages,\r |
134 | OUT EFI_PHYSICAL_ADDRESS *Memory\r |
135 | )\r |
136 | /*++\r |
137 | \r |
138 | Routine Description:\r |
139 | \r |
140 | Memory allocation service on permanent memory, \r |
141 | not usable prior to the memory installation.\r |
142 | \r |
143 | Arguments:\r |
144 | \r |
145 | PeiServices - The PEI core services table.\r |
146 | MemoryType - Type of memory to allocate.\r |
147 | Pages - Number of pages to allocate.\r |
148 | Memory - Pointer of memory allocated.\r |
149 | \r |
150 | Returns:\r |
151 | \r |
152 | Status - EFI_SUCCESS The allocation was successful\r |
153 | EFI_INVALID_PARAMETER Only AllocateAnyAddress is supported.\r |
154 | EFI_NOT_AVAILABLE_YET Called with permanent memory not available\r |
155 | EFI_OUT_OF_RESOURCES There is not enough HOB heap to satisfy the requirement\r |
156 | to allocate the number of pages.\r |
157 | \r |
158 | --*/\r |
159 | {\r |
160 | PEI_CORE_INSTANCE *PrivateData;\r |
161 | EFI_PEI_HOB_POINTERS Hob;\r |
162 | EFI_PHYSICAL_ADDRESS Offset;\r |
58dcdada |
163 | EFI_PHYSICAL_ADDRESS *FreeMemoryTop;\r |
164 | EFI_PHYSICAL_ADDRESS *FreeMemoryBottom;\r |
192f6d4c |
165 | \r |
166 | PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r |
58dcdada |
167 | Hob.Raw = PrivateData->HobList.Raw;\r |
168 | \r |
192f6d4c |
169 | //\r |
170 | // Check if Hob already available\r |
171 | //\r |
172 | if (!PrivateData->PeiMemoryInstalled) {\r |
58dcdada |
173 | //\r |
174 | // When PeiInstallMemory is called but CAR has *not* been moved to temporary memory,\r |
175 | // the AllocatePage will dependent the field of PEI_CORE_INSTANCE structure.\r |
176 | //\r |
177 | if (!PrivateData->SwitchStackSignal) {\r |
178 | return EFI_NOT_AVAILABLE_YET;\r |
179 | } else {\r |
180 | FreeMemoryTop = &(PrivateData->FreePhysicalMemoryTop);\r |
181 | FreeMemoryBottom = &(PrivateData->PhysicalMemoryBegin);\r |
182 | }\r |
183 | } else {\r |
184 | FreeMemoryTop = &(Hob.HandoffInformationTable->EfiFreeMemoryTop);\r |
185 | FreeMemoryBottom = &(Hob.HandoffInformationTable->EfiFreeMemoryBottom);\r |
192f6d4c |
186 | }\r |
187 | \r |
58dcdada |
188 | \r |
192f6d4c |
189 | \r |
190 | //\r |
191 | // Check to see if on 4k boundary\r |
192 | //\r |
58dcdada |
193 | Offset = *(FreeMemoryTop) & 0xFFF;\r |
194 | \r |
192f6d4c |
195 | //\r |
196 | // If not aligned, make the allocation aligned.\r |
197 | //\r |
198 | if (Offset != 0) {\r |
58dcdada |
199 | *(FreeMemoryTop) -= Offset;\r |
192f6d4c |
200 | }\r |
58dcdada |
201 | \r |
192f6d4c |
202 | //\r |
203 | // Verify that there is sufficient memory to satisfy the allocation\r |
204 | //\r |
58dcdada |
205 | if (*(FreeMemoryTop) - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < \r |
206 | *(FreeMemoryBottom)) {\r |
192f6d4c |
207 | DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%x Pages is available.\n", Pages));\r |
208 | DEBUG ((EFI_D_ERROR, "There is only left 0x%x pages memory resource to be allocated.\n", \\r |
58dcdada |
209 | EFI_SIZE_TO_PAGES ((UINTN) (*(FreeMemoryTop) - *(FreeMemoryBottom)))));\r |
192f6d4c |
210 | return EFI_OUT_OF_RESOURCES;\r |
211 | } else {\r |
212 | //\r |
213 | // Update the PHIT to reflect the memory usage\r |
214 | //\r |
58dcdada |
215 | *(FreeMemoryTop) -= Pages * EFI_PAGE_SIZE;\r |
192f6d4c |
216 | \r |
217 | //\r |
218 | // Update the value for the caller\r |
219 | //\r |
58dcdada |
220 | *Memory = *(FreeMemoryTop);\r |
192f6d4c |
221 | \r |
222 | //\r |
223 | // Create a memory allocation HOB.\r |
224 | //\r |
225 | BuildMemoryAllocationHob (\r |
58dcdada |
226 | *(FreeMemoryTop),\r |
0a7d0741 |
227 | Pages * EFI_PAGE_SIZE,\r |
192f6d4c |
228 | MemoryType\r |
229 | );\r |
230 | \r |
231 | return EFI_SUCCESS;\r |
232 | }\r |
233 | }\r |
234 | \r |
235 | \r |
236 | EFI_STATUS\r |
237 | EFIAPI\r |
238 | PeiAllocatePool (\r |
0c2b5da8 |
239 | IN CONST EFI_PEI_SERVICES **PeiServices,\r |
192f6d4c |
240 | IN UINTN Size,\r |
241 | OUT VOID **Buffer\r |
242 | )\r |
243 | /*++\r |
244 | \r |
245 | Routine Description:\r |
246 | \r |
247 | Memory allocation service on the CAR. \r |
248 | \r |
249 | Arguments:\r |
250 | \r |
251 | PeiServices - The PEI core services table.\r |
252 | \r |
253 | Size - Amount of memory required\r |
254 | \r |
255 | Buffer - Address of pointer to the buffer\r |
256 | \r |
257 | Returns:\r |
258 | \r |
259 | Status - EFI_SUCCESS The allocation was successful\r |
260 | EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement\r |
261 | to allocate the requested size.\r |
262 | \r |
263 | --*/\r |
264 | {\r |
265 | EFI_STATUS Status;\r |
266 | EFI_HOB_MEMORY_POOL *Hob;\r |
267 | \r |
268 | //\r |
269 | // If some "post-memory" PEIM wishes to allocate larger pool,\r |
270 | // it should use AllocatePages service instead.\r |
271 | //\r |
272 | ASSERT (Size < 0x10000 - sizeof (EFI_HOB_MEMORY_POOL));\r |
273 | Status = PeiServicesCreateHob (\r |
274 | EFI_HOB_TYPE_MEMORY_POOL,\r |
275 | (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size),\r |
276 | (VOID **)&Hob\r |
277 | );\r |
278 | *Buffer = Hob+1; \r |
279 | \r |
280 | \r |
281 | return Status;\r |
282 | }\r |