]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | EFI PEI Core memory services\r | |
3 | \r | |
4 | Copyright (c) 2006 - 2010, Intel Corporation \r | |
5 | All rights reserved. This program and the accompanying materials \r | |
6 | are licensed and made available under the terms and conditions of the BSD License \r | |
7 | which accompanies this distribution. The full text of the license may be found at \r | |
8 | http://opensource.org/licenses/bsd-license.php \r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "PeiMain.h"\r | |
16 | \r | |
17 | /**\r | |
18 | \r | |
19 | Initialize the memory services.\r | |
20 | \r | |
21 | @param PrivateData Points to PeiCore's private instance data.\r | |
22 | @param SecCoreData Points to a data structure containing information about the PEI core's operating\r | |
23 | environment, such as the size and location of temporary RAM, the stack location and\r | |
24 | the BFV location.\r | |
25 | @param OldCoreData Pointer to the PEI Core data.\r | |
26 | NULL if being run in non-permament memory mode.\r | |
27 | \r | |
28 | **/\r | |
29 | VOID\r | |
30 | InitializeMemoryServices (\r | |
31 | IN PEI_CORE_INSTANCE *PrivateData,\r | |
32 | IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r | |
33 | IN PEI_CORE_INSTANCE *OldCoreData\r | |
34 | )\r | |
35 | {\r | |
36 | \r | |
37 | PrivateData->SwitchStackSignal = FALSE;\r | |
38 | \r | |
39 | //\r | |
40 | // First entering PeiCore, following code will initialized some field\r | |
41 | // in PeiCore's private data according to hand off data from sec core.\r | |
42 | //\r | |
43 | if (OldCoreData == NULL) {\r | |
44 | \r | |
45 | PrivateData->PeiMemoryInstalled = FALSE;\r | |
46 | PrivateData->HobList.Raw = SecCoreData->PeiTemporaryRamBase;\r | |
47 | \r | |
48 | PeiCoreBuildHobHandoffInfoTable (\r | |
49 | BOOT_WITH_FULL_CONFIGURATION,\r | |
50 | (EFI_PHYSICAL_ADDRESS) (UINTN) SecCoreData->PeiTemporaryRamBase,\r | |
51 | (UINTN) SecCoreData->PeiTemporaryRamSize\r | |
52 | );\r | |
53 | \r | |
54 | //\r | |
55 | // Set PS to point to ServiceTableShadow in Cache\r | |
56 | //\r | |
57 | PrivateData->PS = &(PrivateData->ServiceTableShadow);\r | |
58 | }\r | |
59 | \r | |
60 | return;\r | |
61 | }\r | |
62 | \r | |
63 | /**\r | |
64 | \r | |
65 | This function registers the found memory configuration with the PEI Foundation.\r | |
66 | \r | |
67 | The usage model is that the PEIM that discovers the permanent memory shall invoke this service.\r | |
68 | This routine will hold discoveried memory information into PeiCore's private data,\r | |
69 | and set SwitchStackSignal flag. After PEIM who discovery memory is dispatched,\r | |
70 | PeiDispatcher will migrate temporary memory to permenement memory.\r | |
71 | \r | |
72 | @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r | |
73 | @param MemoryBegin Start of memory address.\r | |
74 | @param MemoryLength Length of memory.\r | |
75 | \r | |
76 | @return EFI_SUCCESS Always success.\r | |
77 | \r | |
78 | **/\r | |
79 | EFI_STATUS\r | |
80 | EFIAPI\r | |
81 | PeiInstallPeiMemory (\r | |
82 | IN CONST EFI_PEI_SERVICES **PeiServices,\r | |
83 | IN EFI_PHYSICAL_ADDRESS MemoryBegin,\r | |
84 | IN UINT64 MemoryLength\r | |
85 | )\r | |
86 | {\r | |
87 | PEI_CORE_INSTANCE *PrivateData;\r | |
88 | \r | |
89 | DEBUG ((EFI_D_INFO, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin, MemoryLength));\r | |
90 | PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r | |
91 | \r | |
92 | //\r | |
93 | // PEI_SERVICE.InstallPeiMemory should only be called one time during whole PEI phase.\r | |
94 | // If it is invoked more than one time, ASSERT information is given for developer debugging in debug tip and\r | |
95 | // simply return EFI_SUCESS in release tip to ignore it.\r | |
96 | // \r | |
97 | if (PrivateData->PeiMemoryInstalled) {\r | |
98 | DEBUG ((EFI_D_ERROR, "ERROR: PeiInstallPeiMemory is called more than once!\n"));\r | |
99 | ASSERT (PrivateData->PeiMemoryInstalled);\r | |
100 | return EFI_SUCCESS;\r | |
101 | }\r | |
102 | \r | |
103 | PrivateData->PhysicalMemoryBegin = MemoryBegin;\r | |
104 | PrivateData->PhysicalMemoryLength = MemoryLength;\r | |
105 | PrivateData->FreePhysicalMemoryTop = MemoryBegin + MemoryLength;\r | |
106 | \r | |
107 | PrivateData->SwitchStackSignal = TRUE;\r | |
108 | \r | |
109 | return EFI_SUCCESS; \r | |
110 | }\r | |
111 | \r | |
112 | /**\r | |
113 | \r | |
114 | Memory allocation service on permanent memory,\r | |
115 | not usable prior to the memory installation.\r | |
116 | \r | |
117 | \r | |
118 | @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r | |
119 | @param MemoryType Type of memory to allocate.\r | |
120 | @param Pages Number of pages to allocate.\r | |
121 | @param Memory Pointer of memory allocated.\r | |
122 | \r | |
123 | @retval EFI_SUCCESS The allocation was successful\r | |
124 | @retval EFI_NOT_AVAILABLE_YET Called with permanent memory not available\r | |
125 | @retval EFI_OUT_OF_RESOURCES There is not enough HOB heap to satisfy the requirement\r | |
126 | to allocate the number of pages.\r | |
127 | \r | |
128 | **/\r | |
129 | EFI_STATUS\r | |
130 | EFIAPI\r | |
131 | PeiAllocatePages (\r | |
132 | IN CONST EFI_PEI_SERVICES **PeiServices,\r | |
133 | IN EFI_MEMORY_TYPE MemoryType,\r | |
134 | IN UINTN Pages,\r | |
135 | OUT EFI_PHYSICAL_ADDRESS *Memory\r | |
136 | )\r | |
137 | {\r | |
138 | PEI_CORE_INSTANCE *PrivateData;\r | |
139 | EFI_PEI_HOB_POINTERS Hob;\r | |
140 | EFI_PHYSICAL_ADDRESS *FreeMemoryTop;\r | |
141 | EFI_PHYSICAL_ADDRESS *FreeMemoryBottom;\r | |
142 | UINTN RemainingPages;\r | |
143 | \r | |
144 | PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r | |
145 | Hob.Raw = PrivateData->HobList.Raw;\r | |
146 | \r | |
147 | //\r | |
148 | // Check if Hob already available\r | |
149 | //\r | |
150 | if (!PrivateData->PeiMemoryInstalled) {\r | |
151 | //\r | |
152 | // When PeiInstallMemory is called but temporary memory has *not* been moved to temporary memory,\r | |
153 | // the AllocatePage will depend on the field of PEI_CORE_INSTANCE structure.\r | |
154 | //\r | |
155 | if (!PrivateData->SwitchStackSignal) {\r | |
156 | return EFI_NOT_AVAILABLE_YET;\r | |
157 | } else {\r | |
158 | FreeMemoryTop = &(PrivateData->FreePhysicalMemoryTop);\r | |
159 | FreeMemoryBottom = &(PrivateData->PhysicalMemoryBegin);\r | |
160 | }\r | |
161 | } else {\r | |
162 | FreeMemoryTop = &(Hob.HandoffInformationTable->EfiFreeMemoryTop);\r | |
163 | FreeMemoryBottom = &(Hob.HandoffInformationTable->EfiFreeMemoryBottom);\r | |
164 | }\r | |
165 | \r | |
166 | //\r | |
167 | // Check to see if on 4k boundary, If not aligned, make the allocation aligned.\r | |
168 | //\r | |
169 | *(FreeMemoryTop) -= *(FreeMemoryTop) & 0xFFF;\r | |
170 | \r | |
171 | //\r | |
172 | // Verify that there is sufficient memory to satisfy the allocation\r | |
173 | //\r | |
174 | RemainingPages = EFI_SIZE_TO_PAGES ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom));\r | |
175 | //\r | |
176 | // For page allocation, the overhead sizeof (EFI_HOB_MEMORY_ALLOCATION) needs one extra page.\r | |
177 | // So the number of remaining pages needs to be greater than that of the request pages.\r | |
178 | //\r | |
179 | if (RemainingPages <= Pages) {\r | |
180 | DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%lx Pages is available.\n", (UINT64) Pages));\r | |
181 | DEBUG ((EFI_D_ERROR, "There is only left 0x%lx pages memory resource to be allocated.\n", (UINT64) RemainingPages));\r | |
182 | return EFI_OUT_OF_RESOURCES;\r | |
183 | } else {\r | |
184 | //\r | |
185 | // Update the PHIT to reflect the memory usage\r | |
186 | //\r | |
187 | *(FreeMemoryTop) -= Pages * EFI_PAGE_SIZE;\r | |
188 | \r | |
189 | //\r | |
190 | // Update the value for the caller\r | |
191 | //\r | |
192 | *Memory = *(FreeMemoryTop);\r | |
193 | \r | |
194 | //\r | |
195 | // Create a memory allocation HOB.\r | |
196 | //\r | |
197 | BuildMemoryAllocationHob (\r | |
198 | *(FreeMemoryTop),\r | |
199 | Pages * EFI_PAGE_SIZE,\r | |
200 | MemoryType\r | |
201 | );\r | |
202 | \r | |
203 | return EFI_SUCCESS;\r | |
204 | }\r | |
205 | }\r | |
206 | \r | |
207 | /**\r | |
208 | \r | |
209 | Pool allocation service. Before permenent memory is discoveried, the pool will \r | |
210 | be allocated the heap in the temporary memory. Genenrally, the size of heap in temporary \r | |
211 | memory does not exceed to 64K, so the biggest pool size could be allocated is \r | |
212 | 64K.\r | |
213 | \r | |
214 | @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r | |
215 | @param Size Amount of memory required\r | |
216 | @param Buffer Address of pointer to the buffer\r | |
217 | \r | |
218 | @retval EFI_SUCCESS The allocation was successful\r | |
219 | @retval EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement\r | |
220 | to allocate the requested size.\r | |
221 | \r | |
222 | **/\r | |
223 | EFI_STATUS\r | |
224 | EFIAPI\r | |
225 | PeiAllocatePool (\r | |
226 | IN CONST EFI_PEI_SERVICES **PeiServices,\r | |
227 | IN UINTN Size,\r | |
228 | OUT VOID **Buffer\r | |
229 | )\r | |
230 | {\r | |
231 | EFI_STATUS Status;\r | |
232 | EFI_HOB_MEMORY_POOL *Hob;\r | |
233 | \r | |
234 | //\r | |
235 | // If some "post-memory" PEIM wishes to allocate larger pool,\r | |
236 | // it should use AllocatePages service instead.\r | |
237 | //\r | |
238 | \r | |
239 | //\r | |
240 | // Generally, the size of heap in temporary memory does not exceed to 64K,\r | |
241 | // so the maxmium size of pool is 0x10000 - sizeof (EFI_HOB_MEMORY_POOL)\r | |
242 | //\r | |
243 | if (Size >= (0x10000 - sizeof (EFI_HOB_MEMORY_POOL))) {\r | |
244 | return EFI_OUT_OF_RESOURCES;\r | |
245 | }\r | |
246 | \r | |
247 | Status = PeiServicesCreateHob (\r | |
248 | EFI_HOB_TYPE_MEMORY_POOL,\r | |
249 | (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size),\r | |
250 | (VOID **)&Hob\r | |
251 | );\r | |
252 | *Buffer = Hob+1; \r | |
253 | \r | |
254 | return Status;\r | |
255 | }\r |