]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
DynamicTablesPkg: SSDT CPU _CPC generator
[mirror_edk2.git] / EmbeddedPkg / Library / PrePiMemoryAllocationLib / MemoryAllocationLib.c
CommitLineData
a6caee65 1/** @file\r
2 Implementation of the 6 PEI Ffs (FV) APIs in library form.\r
3\r
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
3402aac7 5\r
878b807a 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
a6caee65 7\r
8**/\r
9\r
10#include <PiPei.h>\r
11\r
12#include <Library/BaseLib.h>\r
864cba95 13#include <Library/BaseMemoryLib.h>\r
a6caee65 14#include <Library/PrePiLib.h>\r
15#include <Library/DebugLib.h>\r
16\r
fb008dbe 17STATIC\r
a6caee65 18VOID *\r
19EFIAPI\r
fb008dbe
MX
20InternalAllocatePages (\r
21 IN UINTN Pages,\r
22 IN EFI_MEMORY_TYPE MemoryType\r
a6caee65 23 )\r
24{\r
e7108d0e
MK
25 EFI_PEI_HOB_POINTERS Hob;\r
26 EFI_PHYSICAL_ADDRESS Offset;\r
a6caee65 27\r
28 Hob.Raw = GetHobList ();\r
29\r
30 // Check to see if on 4k boundary\r
31 Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;\r
32 if (Offset != 0) {\r
33 // If not aligned, make the allocation aligned.\r
34 Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;\r
35 }\r
36\r
37 //\r
38 // Verify that there is sufficient memory to satisfy the allocation\r
39 //\r
40 if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {\r
41 return 0;\r
42 } else {\r
43 //\r
44 // Update the PHIT to reflect the memory usage\r
45 //\r
46 Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;\r
47\r
48 // This routine used to create a memory allocation HOB a la PEI, but that's not\r
49 // necessary for us.\r
50\r
51 //\r
52 // Create a memory allocation HOB.\r
53 //\r
54 BuildMemoryAllocationHob (\r
e7108d0e
MK
55 Hob.HandoffInformationTable->EfiFreeMemoryTop,\r
56 Pages * EFI_PAGE_SIZE,\r
fb008dbe 57 MemoryType\r
e7108d0e 58 );\r
a6caee65 59 return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;\r
60 }\r
61}\r
62\r
fb008dbe
MX
63/**\r
64 Allocates one or more 4KB pages of type EfiBootServicesData.\r
65\r
66 Allocates the number of 4KB pages of MemoryType and returns a pointer to the\r
67 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
68 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
69 returned.\r
70\r
71 @param Pages The number of 4 KB pages to allocate.\r
72\r
73 @return A pointer to the allocated buffer or NULL if allocation fails.\r
74\r
75**/\r
76VOID *\r
77EFIAPI\r
78AllocatePages (\r
79 IN UINTN Pages\r
80 )\r
81{\r
82 return InternalAllocatePages (Pages, EfiBootServicesData);\r
83}\r
84\r
85/**\r
86 Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r
87\r
88 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
89 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
90 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
91 returned.\r
92\r
93 @param Pages The number of 4 KB pages to allocate.\r
94\r
95 @return A pointer to the allocated buffer or NULL if allocation fails.\r
96\r
97**/\r
98VOID *\r
99EFIAPI\r
100AllocateRuntimePages (\r
101 IN UINTN Pages\r
102 )\r
103{\r
104 return InternalAllocatePages (Pages, EfiRuntimeServicesData);\r
105}\r
106\r
a6caee65 107/**\r
108 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
109\r
110 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
111 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
112 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
113 request, then NULL is returned.\r
114 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
115\r
116 @param Pages The number of 4 KB pages to allocate.\r
117 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
118 If Alignment is zero, then byte alignment is used.\r
119\r
120 @return A pointer to the allocated buffer or NULL if allocation fails.\r
121\r
122**/\r
123VOID *\r
124EFIAPI\r
125AllocateAlignedPages (\r
126 IN UINTN Pages,\r
127 IN UINTN Alignment\r
128 )\r
129{\r
e7108d0e
MK
130 VOID *Memory;\r
131 UINTN AlignmentMask;\r
a6caee65 132\r
133 //\r
134 // Alignment must be a power of two or zero.\r
135 //\r
136 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
137\r
138 if (Pages == 0) {\r
139 return NULL;\r
140 }\r
e7108d0e 141\r
a6caee65 142 //\r
143 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
144 //\r
145 ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));\r
146 //\r
147 // We would rather waste some memory to save PEI code size.\r
148 //\r
149 Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));\r
150 if (Alignment == 0) {\r
151 AlignmentMask = Alignment;\r
152 } else {\r
153 AlignmentMask = Alignment - 1;\r
154 }\r
a6caee65 155\r
e7108d0e
MK
156 return (VOID *)(UINTN)(((UINTN)Memory + AlignmentMask) & ~AlignmentMask);\r
157}\r
a6caee65 158\r
0b342ffb
OM
159/**\r
160 Frees one or more 4KB pages that were previously allocated with one of the page allocation\r
161 functions in the Memory Allocation Library.\r
162\r
163 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
164 must have been allocated on a previous call to the page allocation services of the Memory\r
165 Allocation Library. If it is not possible to free allocated pages, then this function will\r
166 perform no actions.\r
167\r
168 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r
169 then ASSERT().\r
170 If Pages is zero, then ASSERT().\r
a6caee65 171\r
0b342ffb
OM
172 @param Buffer Pointer to the buffer of pages to free.\r
173 @param Pages The number of 4 KB pages to free.\r
174\r
175**/\r
176VOID\r
177EFIAPI\r
178FreePages (\r
179 IN VOID *Buffer,\r
180 IN UINTN Pages\r
181 )\r
182{\r
183 // For now, we do not support the ability to free pages in the PrePei Memory Allocator.\r
184 // The allocated memory is lost.\r
185}\r
a6caee65 186\r
187/**\r
188 Allocates a buffer of type EfiBootServicesData.\r
189\r
190 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
191 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
192 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
193\r
194 @param AllocationSize The number of bytes to allocate.\r
195\r
196 @return A pointer to the allocated buffer or NULL if allocation fails.\r
197\r
198**/\r
199VOID *\r
200EFIAPI\r
201AllocatePool (\r
202 IN UINTN AllocationSize\r
203 )\r
204{\r
e7108d0e 205 EFI_HOB_MEMORY_POOL *Hob;\r
a6caee65 206\r
207 Hob = GetHobList ();\r
208\r
a6caee65 209 //\r
210 // Verify that there is sufficient memory to satisfy the allocation\r
211 //\r
212 if (AllocationSize > 0x10000) {\r
c6a72cd7 213 // Please call AllocatePages for big allocations\r
a6caee65 214 return 0;\r
215 } else {\r
e7108d0e
MK
216 Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (\r
217 EFI_HOB_TYPE_MEMORY_POOL,\r
18198171 218 (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) +\r
e7108d0e
MK
219 AllocationSize)\r
220 );\r
a6caee65 221 return (VOID *)(Hob + 1);\r
222 }\r
223}\r
224\r
864cba95
JB
225/**\r
226 Allocates and zeros a buffer of type EfiBootServicesData.\r
227\r
228 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
229 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
230 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
231 request, then NULL is returned.\r
232\r
233 @param AllocationSize The number of bytes to allocate and zero.\r
234\r
235 @return A pointer to the allocated buffer or NULL if allocation fails.\r
236\r
237**/\r
238VOID *\r
239EFIAPI\r
240AllocateZeroPool (\r
241 IN UINTN AllocationSize\r
242 )\r
243{\r
e7108d0e 244 VOID *Buffer;\r
864cba95
JB
245\r
246 Buffer = AllocatePool (AllocationSize);\r
247 if (Buffer == NULL) {\r
248 return NULL;\r
249 }\r
250\r
251 ZeroMem (Buffer, AllocationSize);\r
252\r
253 return Buffer;\r
254}\r
255\r
a6caee65 256/**\r
257 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
258 Memory Allocation Library.\r
259\r
260 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
261 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
262 resources, then this function will perform no actions.\r
263\r
264 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
265 then ASSERT().\r
266\r
267 @param Buffer Pointer to the buffer to free.\r
268\r
269**/\r
270VOID\r
271EFIAPI\r
272FreePool (\r
e7108d0e 273 IN VOID *Buffer\r
a6caee65 274 )\r
275{\r
276 // Not implemented yet\r
277}\r