]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Sd/EmmcBlockIoPei/EmmcHcMem.c
MdeModulePkg EmmcBlockIoPei: Remove a redundant function
[mirror_edk2.git] / MdeModulePkg / Bus / Sd / EmmcBlockIoPei / EmmcHcMem.c
1 /** @file
2
3 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
4
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions
7 of the BSD License which accompanies this distribution. The
8 full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "EmmcBlockIoPei.h"
17
18 /**
19 Allocate a block of memory to be used by the buffer pool.
20
21 @param Pages How many pages to allocate.
22
23 @return The allocated memory block or NULL if failed.
24
25 **/
26 EMMC_PEIM_MEM_BLOCK *
27 EmmcPeimAllocMemBlock (
28 IN UINTN Pages
29 )
30 {
31 EMMC_PEIM_MEM_BLOCK *Block;
32 VOID *BufHost;
33 VOID *Mapping;
34 EFI_PHYSICAL_ADDRESS MappedAddr;
35 EFI_STATUS Status;
36 VOID *TempPtr;
37
38 TempPtr = NULL;
39 Block = NULL;
40
41 Status = PeiServicesAllocatePool (sizeof(EMMC_PEIM_MEM_BLOCK), &TempPtr);
42 if (EFI_ERROR (Status)) {
43 return NULL;
44 }
45
46 ZeroMem ((VOID*)(UINTN)TempPtr, sizeof(EMMC_PEIM_MEM_BLOCK));
47
48 //
49 // each bit in the bit array represents EMMC_PEIM_MEM_UNIT
50 // bytes of memory in the memory block.
51 //
52 ASSERT (EMMC_PEIM_MEM_UNIT * 8 <= EFI_PAGE_SIZE);
53
54 Block = (EMMC_PEIM_MEM_BLOCK*)(UINTN)TempPtr;
55 Block->BufLen = EFI_PAGES_TO_SIZE (Pages);
56 Block->BitsLen = Block->BufLen / (EMMC_PEIM_MEM_UNIT * 8);
57
58 Status = PeiServicesAllocatePool (Block->BitsLen, &TempPtr);
59 if (EFI_ERROR (Status)) {
60 return NULL;
61 }
62
63 ZeroMem ((VOID*)(UINTN)TempPtr, Block->BitsLen);
64
65 Block->Bits = (UINT8*)(UINTN)TempPtr;
66
67 Status = IoMmuAllocateBuffer (
68 Pages,
69 &BufHost,
70 &MappedAddr,
71 &Mapping
72 );
73 if (EFI_ERROR (Status)) {
74 return NULL;
75 }
76
77 ZeroMem ((VOID*)(UINTN)BufHost, EFI_PAGES_TO_SIZE (Pages));
78
79 Block->BufHost = (UINT8 *) (UINTN) BufHost;
80 Block->Buf = (UINT8 *) (UINTN) MappedAddr;
81 Block->Mapping = Mapping;
82 Block->Next = NULL;
83
84 return Block;
85 }
86
87 /**
88 Free the memory block from the memory pool.
89
90 @param Pool The memory pool to free the block from.
91 @param Block The memory block to free.
92
93 **/
94 VOID
95 EmmcPeimFreeMemBlock (
96 IN EMMC_PEIM_MEM_POOL *Pool,
97 IN EMMC_PEIM_MEM_BLOCK *Block
98 )
99 {
100 ASSERT ((Pool != NULL) && (Block != NULL));
101
102 IoMmuFreeBuffer (EFI_SIZE_TO_PAGES (Block->BufLen), Block->BufHost, Block->Mapping);
103 }
104
105 /**
106 Alloc some memory from the block.
107
108 @param Block The memory block to allocate memory from.
109 @param Units Number of memory units to allocate.
110
111 @return The pointer to the allocated memory. If couldn't allocate the needed memory,
112 the return value is NULL.
113
114 **/
115 VOID *
116 EmmcPeimAllocMemFromBlock (
117 IN EMMC_PEIM_MEM_BLOCK *Block,
118 IN UINTN Units
119 )
120 {
121 UINTN Byte;
122 UINT8 Bit;
123 UINTN StartByte;
124 UINT8 StartBit;
125 UINTN Available;
126 UINTN Count;
127
128 ASSERT ((Block != 0) && (Units != 0));
129
130 StartByte = 0;
131 StartBit = 0;
132 Available = 0;
133
134 for (Byte = 0, Bit = 0; Byte < Block->BitsLen;) {
135 //
136 // If current bit is zero, the corresponding memory unit is
137 // available, otherwise we need to restart our searching.
138 // Available counts the consective number of zero bit.
139 //
140 if (!EMMC_PEIM_MEM_BIT_IS_SET (Block->Bits[Byte], Bit)) {
141 Available++;
142
143 if (Available >= Units) {
144 break;
145 }
146
147 EMMC_PEIM_NEXT_BIT (Byte, Bit);
148
149 } else {
150 EMMC_PEIM_NEXT_BIT (Byte, Bit);
151
152 Available = 0;
153 StartByte = Byte;
154 StartBit = Bit;
155 }
156 }
157
158 if (Available < Units) {
159 return NULL;
160 }
161
162 //
163 // Mark the memory as allocated
164 //
165 Byte = StartByte;
166 Bit = StartBit;
167
168 for (Count = 0; Count < Units; Count++) {
169 ASSERT (!EMMC_PEIM_MEM_BIT_IS_SET (Block->Bits[Byte], Bit));
170
171 Block->Bits[Byte] = (UINT8) (Block->Bits[Byte] | (UINT8) EMMC_PEIM_MEM_BIT (Bit));
172 EMMC_PEIM_NEXT_BIT (Byte, Bit);
173 }
174
175 return Block->Buf + (StartByte * 8 + StartBit) * EMMC_PEIM_MEM_UNIT;
176 }
177
178 /**
179 Insert the memory block to the pool's list of the blocks.
180
181 @param Head The head of the memory pool's block list.
182 @param Block The memory block to insert.
183
184 **/
185 VOID
186 EmmcPeimInsertMemBlockToPool (
187 IN EMMC_PEIM_MEM_BLOCK *Head,
188 IN EMMC_PEIM_MEM_BLOCK *Block
189 )
190 {
191 ASSERT ((Head != NULL) && (Block != NULL));
192 Block->Next = Head->Next;
193 Head->Next = Block;
194 }
195
196 /**
197 Is the memory block empty?
198
199 @param Block The memory block to check.
200
201 @retval TRUE The memory block is empty.
202 @retval FALSE The memory block isn't empty.
203
204 **/
205 BOOLEAN
206 EmmcPeimIsMemBlockEmpty (
207 IN EMMC_PEIM_MEM_BLOCK *Block
208 )
209 {
210 UINTN Index;
211
212
213 for (Index = 0; Index < Block->BitsLen; Index++) {
214 if (Block->Bits[Index] != 0) {
215 return FALSE;
216 }
217 }
218
219 return TRUE;
220 }
221
222
223
224 /**
225 Initialize the memory management pool for the host controller.
226
227 @param Private The Emmc Peim driver private data.
228
229 @retval EFI_SUCCESS The memory pool is initialized.
230 @retval Others Fail to init the memory pool.
231
232 **/
233 EFI_STATUS
234 EmmcPeimInitMemPool (
235 IN EMMC_PEIM_HC_PRIVATE_DATA *Private
236 )
237 {
238 EMMC_PEIM_MEM_POOL *Pool;
239 EFI_STATUS Status;
240 VOID *TempPtr;
241
242 TempPtr = NULL;
243 Pool = NULL;
244
245 Status = PeiServicesAllocatePool (sizeof (EMMC_PEIM_MEM_POOL), &TempPtr);
246 if (EFI_ERROR (Status)) {
247 return EFI_OUT_OF_RESOURCES;
248 }
249
250 ZeroMem ((VOID*)(UINTN)TempPtr, sizeof (EMMC_PEIM_MEM_POOL));
251
252 Pool = (EMMC_PEIM_MEM_POOL *)((UINTN)TempPtr);
253
254 Pool->Head = EmmcPeimAllocMemBlock (EMMC_PEIM_MEM_DEFAULT_PAGES);
255
256 if (Pool->Head == NULL) {
257 return EFI_OUT_OF_RESOURCES;
258 }
259
260 Private->Pool = Pool;
261 return EFI_SUCCESS;
262 }
263
264 /**
265 Release the memory management pool.
266
267 @param Pool The memory pool to free.
268
269 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
270 @retval EFI_SUCCESS The memory pool is freed.
271
272 **/
273 EFI_STATUS
274 EmmcPeimFreeMemPool (
275 IN EMMC_PEIM_MEM_POOL *Pool
276 )
277 {
278 EMMC_PEIM_MEM_BLOCK *Block;
279
280 ASSERT (Pool->Head != NULL);
281
282 //
283 // Unlink all the memory blocks from the pool, then free them.
284 //
285 for (Block = Pool->Head->Next; Block != NULL; Block = Pool->Head->Next) {
286 EmmcPeimFreeMemBlock (Pool, Block);
287 }
288
289 EmmcPeimFreeMemBlock (Pool, Pool->Head);
290
291 return EFI_SUCCESS;
292 }
293
294 /**
295 Allocate some memory from the host controller's memory pool
296 which can be used to communicate with host controller.
297
298 @param Pool The host controller's memory pool.
299 @param Size Size of the memory to allocate.
300
301 @return The allocated memory or NULL.
302
303 **/
304 VOID *
305 EmmcPeimAllocateMem (
306 IN EMMC_PEIM_MEM_POOL *Pool,
307 IN UINTN Size
308 )
309 {
310 EMMC_PEIM_MEM_BLOCK *Head;
311 EMMC_PEIM_MEM_BLOCK *Block;
312 EMMC_PEIM_MEM_BLOCK *NewBlock;
313 VOID *Mem;
314 UINTN AllocSize;
315 UINTN Pages;
316
317 Mem = NULL;
318 AllocSize = EMMC_PEIM_MEM_ROUND (Size);
319 Head = Pool->Head;
320 ASSERT (Head != NULL);
321
322 //
323 // First check whether current memory blocks can satisfy the allocation.
324 //
325 for (Block = Head; Block != NULL; Block = Block->Next) {
326 Mem = EmmcPeimAllocMemFromBlock (Block, AllocSize / EMMC_PEIM_MEM_UNIT);
327
328 if (Mem != NULL) {
329 ZeroMem (Mem, Size);
330 break;
331 }
332 }
333
334 if (Mem != NULL) {
335 return Mem;
336 }
337
338 //
339 // Create a new memory block if there is not enough memory
340 // in the pool. If the allocation size is larger than the
341 // default page number, just allocate a large enough memory
342 // block. Otherwise allocate default pages.
343 //
344 if (AllocSize > EFI_PAGES_TO_SIZE (EMMC_PEIM_MEM_DEFAULT_PAGES)) {
345 Pages = EFI_SIZE_TO_PAGES (AllocSize) + 1;
346 } else {
347 Pages = EMMC_PEIM_MEM_DEFAULT_PAGES;
348 }
349
350 NewBlock = EmmcPeimAllocMemBlock (Pages);
351 if (NewBlock == NULL) {
352 return NULL;
353 }
354
355 //
356 // Add the new memory block to the pool, then allocate memory from it
357 //
358 EmmcPeimInsertMemBlockToPool (Head, NewBlock);
359 Mem = EmmcPeimAllocMemFromBlock (NewBlock, AllocSize / EMMC_PEIM_MEM_UNIT);
360
361 if (Mem != NULL) {
362 ZeroMem (Mem, Size);
363 }
364
365 return Mem;
366 }
367
368 /**
369 Free the allocated memory back to the memory pool.
370
371 @param Pool The memory pool of the host controller.
372 @param Mem The memory to free.
373 @param Size The size of the memory to free.
374
375 **/
376 VOID
377 EmmcPeimFreeMem (
378 IN EMMC_PEIM_MEM_POOL *Pool,
379 IN VOID *Mem,
380 IN UINTN Size
381 )
382 {
383 EMMC_PEIM_MEM_BLOCK *Head;
384 EMMC_PEIM_MEM_BLOCK *Block;
385 UINT8 *ToFree;
386 UINTN AllocSize;
387 UINTN Byte;
388 UINTN Bit;
389 UINTN Count;
390
391 Head = Pool->Head;
392 AllocSize = EMMC_PEIM_MEM_ROUND (Size);
393 ToFree = (UINT8 *) Mem;
394
395 for (Block = Head; Block != NULL; Block = Block->Next) {
396 //
397 // scan the memory block list for the memory block that
398 // completely contains the memory to free.
399 //
400 if ((Block->Buf <= ToFree) && ((ToFree + AllocSize) <= (Block->Buf + Block->BufLen))) {
401 //
402 // compute the start byte and bit in the bit array
403 //
404 Byte = ((ToFree - Block->Buf) / EMMC_PEIM_MEM_UNIT) / 8;
405 Bit = ((ToFree - Block->Buf) / EMMC_PEIM_MEM_UNIT) % 8;
406
407 //
408 // reset associated bits in bit array
409 //
410 for (Count = 0; Count < (AllocSize / EMMC_PEIM_MEM_UNIT); Count++) {
411 ASSERT (EMMC_PEIM_MEM_BIT_IS_SET (Block->Bits[Byte], Bit));
412
413 Block->Bits[Byte] = (UINT8) (Block->Bits[Byte] ^ EMMC_PEIM_MEM_BIT (Bit));
414 EMMC_PEIM_NEXT_BIT (Byte, Bit);
415 }
416
417 break;
418 }
419 }
420
421 //
422 // If Block == NULL, it means that the current memory isn't
423 // in the host controller's pool. This is critical because
424 // the caller has passed in a wrong memory point
425 //
426 ASSERT (Block != NULL);
427
428 //
429 // Release the current memory block if it is empty and not the head
430 //
431 if ((Block != Head) && EmmcPeimIsMemBlockEmpty (Block)) {
432 EmmcPeimFreeMemBlock (Pool, Block);
433 }
434
435 return ;
436 }