]> git.proxmox.com Git - mirror_edk2.git/blame - UefiPayloadPkg/UefiPayloadEntry/MemoryAllocation.c
MdePkg/Acpi62: Add type 7 NFIT Platform Capabilities Structure support
[mirror_edk2.git] / UefiPayloadPkg / UefiPayloadEntry / MemoryAllocation.c
CommitLineData
7c4ab1c2
GD
1/** @file\r
2\r
3\r
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
5 Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>\r
6\r
7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
8\r
9**/\r
10\r
11#include "UefiPayloadEntry.h"\r
12\r
13/**\r
14 Allocates one or more pages of type EfiBootServicesData.\r
15\r
16 Allocates the number of pages of MemoryType and returns a pointer to the\r
17 allocated buffer. The buffer returned is aligned on a 4KB boundary.\r
18 If Pages is 0, then NULL is returned.\r
19 If there is not enough memory availble to satisfy the request, then NULL\r
20 is returned.\r
21\r
22 @param Pages The number of 4 KB pages to allocate.\r
23 @return A pointer to the allocated buffer or NULL if allocation fails.\r
24**/\r
25VOID *\r
26EFIAPI\r
27AllocatePages (\r
e5efcf8b 28 IN UINTN Pages\r
7c4ab1c2
GD
29 )\r
30{\r
e5efcf8b
MK
31 EFI_PEI_HOB_POINTERS Hob;\r
32 EFI_PHYSICAL_ADDRESS Offset;\r
33 EFI_HOB_HANDOFF_INFO_TABLE *HobTable;\r
7c4ab1c2
GD
34\r
35 Hob.Raw = GetHobList ();\r
36 HobTable = Hob.HandoffInformationTable;\r
37\r
38 if (Pages == 0) {\r
39 return NULL;\r
40 }\r
41\r
42 // Make sure allocation address is page alligned.\r
43 Offset = HobTable->EfiFreeMemoryTop & EFI_PAGE_MASK;\r
44 if (Offset != 0) {\r
45 HobTable->EfiFreeMemoryTop -= Offset;\r
46 }\r
47\r
48 //\r
49 // Check available memory for the allocation\r
50 //\r
51 if (HobTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < HobTable->EfiFreeMemoryBottom) {\r
52 return NULL;\r
53 }\r
54\r
55 HobTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;\r
56 BuildMemoryAllocationHob (HobTable->EfiFreeMemoryTop, Pages * EFI_PAGE_SIZE, EfiBootServicesData);\r
57\r
58 return (VOID *)(UINTN)HobTable->EfiFreeMemoryTop;\r
59}\r
60\r
61/**\r
62 Frees one or more 4KB pages that were previously allocated with one of the page allocation\r
63 functions in the Memory Allocation Library.\r
64\r
65 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
66 must have been allocated on a previous call to the page allocation services of the Memory\r
67 Allocation Library. If it is not possible to free allocated pages, then this function will\r
68 perform no actions.\r
69\r
70 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r
71 then ASSERT().\r
72 If Pages is zero, then ASSERT().\r
73\r
74 @param Buffer Pointer to the buffer of pages to free.\r
75 @param Pages The number of 4 KB pages to free.\r
76\r
77**/\r
78VOID\r
79EFIAPI\r
80FreePages (\r
81 IN VOID *Buffer,\r
82 IN UINTN Pages\r
83 )\r
84{\r
85}\r
86\r
87/**\r
88 Allocates one or more pages of type EfiBootServicesData at a specified alignment.\r
89\r
90 Allocates the number of pages specified by Pages of type EfiBootServicesData with an\r
91 alignment specified by Alignment.\r
92 If Pages is 0, then NULL is returned.\r
93 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
94 If there is no enough memory at the specified alignment available to satisfy the\r
95 request, then NULL is returned.\r
96\r
97 @param Pages The number of 4 KB pages to allocate.\r
98 @param Alignment The requested alignment of the allocation.\r
99\r
100 @return A pointer to the allocated buffer or NULL if allocation fails.\r
101**/\r
102VOID *\r
103EFIAPI\r
104AllocateAlignedPages (\r
e5efcf8b
MK
105 IN UINTN Pages,\r
106 IN UINTN Alignment\r
7c4ab1c2
GD
107 )\r
108{\r
e5efcf8b
MK
109 VOID *Memory;\r
110 UINTN AlignmentMask;\r
7c4ab1c2
GD
111\r
112 //\r
113 // Alignment must be a power of two or zero.\r
114 //\r
115 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
116\r
117 if (Pages == 0) {\r
118 return NULL;\r
119 }\r
120\r
121 //\r
122 // Check overflow.\r
123 //\r
124 ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));\r
125\r
126 Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));\r
127 if (Memory == NULL) {\r
128 return NULL;\r
129 }\r
130\r
131 if (Alignment == 0) {\r
132 AlignmentMask = Alignment;\r
133 } else {\r
134 AlignmentMask = Alignment - 1;\r
135 }\r
136\r
e5efcf8b 137 return (VOID *)(UINTN)(((UINTN)Memory + AlignmentMask) & ~AlignmentMask);\r
7c4ab1c2
GD
138}\r
139\r
7c4ab1c2
GD
140/**\r
141 Allocates a buffer of type EfiBootServicesData.\r
142\r
143 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
144 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
145 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
146\r
147 @param AllocationSize The number of bytes to allocate.\r
148\r
149 @return A pointer to the allocated buffer or NULL if allocation fails.\r
150\r
151**/\r
152VOID *\r
153EFIAPI\r
154AllocatePool (\r
155 IN UINTN AllocationSize\r
156 )\r
157{\r
e5efcf8b 158 EFI_HOB_MEMORY_POOL *Hob;\r
7c4ab1c2
GD
159\r
160 if (AllocationSize > 0x4000) {\r
161 // Please use AllocatePages for big allocations\r
162 return NULL;\r
163 }\r
164\r
cdda3f74 165 Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (EFI_HOB_TYPE_MEMORY_POOL, (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + AllocationSize));\r
7c4ab1c2
GD
166 return (VOID *)(Hob + 1);\r
167}\r
168\r
169/**\r
170 Allocates and zeros a buffer of type EfiBootServicesData.\r
171\r
172 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
173 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
174 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
175 request, then NULL is returned.\r
176\r
177 @param AllocationSize The number of bytes to allocate and zero.\r
178\r
179 @return A pointer to the allocated buffer or NULL if allocation fails.\r
180\r
181**/\r
182VOID *\r
183EFIAPI\r
184AllocateZeroPool (\r
185 IN UINTN AllocationSize\r
186 )\r
187{\r
e5efcf8b 188 VOID *Buffer;\r
7c4ab1c2
GD
189\r
190 Buffer = AllocatePool (AllocationSize);\r
191 if (Buffer == NULL) {\r
192 return NULL;\r
193 }\r
194\r
195 ZeroMem (Buffer, AllocationSize);\r
196\r
197 return Buffer;\r
198}\r