3 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
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
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.
16 #include "EmmcBlockIoPei.h"
19 Allocate a block of memory to be used by the buffer pool.
21 @param Pages How many pages to allocate.
23 @return The allocated memory block or NULL if failed.
27 EmmcPeimAllocMemBlock (
31 EMMC_PEIM_MEM_BLOCK
*Block
;
34 EFI_PHYSICAL_ADDRESS MappedAddr
;
41 Status
= PeiServicesAllocatePool (sizeof(EMMC_PEIM_MEM_BLOCK
), &TempPtr
);
42 if (EFI_ERROR (Status
)) {
46 ZeroMem ((VOID
*)(UINTN
)TempPtr
, sizeof(EMMC_PEIM_MEM_BLOCK
));
49 // each bit in the bit array represents EMMC_PEIM_MEM_UNIT
50 // bytes of memory in the memory block.
52 ASSERT (EMMC_PEIM_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
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);
58 Status
= PeiServicesAllocatePool (Block
->BitsLen
, &TempPtr
);
59 if (EFI_ERROR (Status
)) {
63 ZeroMem ((VOID
*)(UINTN
)TempPtr
, Block
->BitsLen
);
65 Block
->Bits
= (UINT8
*)(UINTN
)TempPtr
;
67 Status
= IoMmuAllocateBuffer (
73 if (EFI_ERROR (Status
)) {
77 ZeroMem ((VOID
*)(UINTN
)BufHost
, EFI_PAGES_TO_SIZE (Pages
));
79 Block
->BufHost
= (UINT8
*) (UINTN
) BufHost
;
80 Block
->Buf
= (UINT8
*) (UINTN
) MappedAddr
;
81 Block
->Mapping
= Mapping
;
88 Free the memory block from the memory pool.
90 @param Pool The memory pool to free the block from.
91 @param Block The memory block to free.
95 EmmcPeimFreeMemBlock (
96 IN EMMC_PEIM_MEM_POOL
*Pool
,
97 IN EMMC_PEIM_MEM_BLOCK
*Block
100 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
102 IoMmuFreeBuffer (EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
, Block
->Mapping
);
106 Alloc some memory from the block.
108 @param Block The memory block to allocate memory from.
109 @param Units Number of memory units to allocate.
111 @return The pointer to the allocated memory. If couldn't allocate the needed memory,
112 the return value is NULL.
116 EmmcPeimAllocMemFromBlock (
117 IN EMMC_PEIM_MEM_BLOCK
*Block
,
128 ASSERT ((Block
!= 0) && (Units
!= 0));
134 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
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.
140 if (!EMMC_PEIM_MEM_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
143 if (Available
>= Units
) {
147 EMMC_PEIM_NEXT_BIT (Byte
, Bit
);
150 EMMC_PEIM_NEXT_BIT (Byte
, Bit
);
158 if (Available
< Units
) {
163 // Mark the memory as allocated
168 for (Count
= 0; Count
< Units
; Count
++) {
169 ASSERT (!EMMC_PEIM_MEM_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
171 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] | (UINT8
) EMMC_PEIM_MEM_BIT (Bit
));
172 EMMC_PEIM_NEXT_BIT (Byte
, Bit
);
175 return Block
->Buf
+ (StartByte
* 8 + StartBit
) * EMMC_PEIM_MEM_UNIT
;
179 Insert the memory block to the pool's list of the blocks.
181 @param Head The head of the memory pool's block list.
182 @param Block The memory block to insert.
186 EmmcPeimInsertMemBlockToPool (
187 IN EMMC_PEIM_MEM_BLOCK
*Head
,
188 IN EMMC_PEIM_MEM_BLOCK
*Block
191 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
192 Block
->Next
= Head
->Next
;
197 Is the memory block empty?
199 @param Block The memory block to check.
201 @retval TRUE The memory block is empty.
202 @retval FALSE The memory block isn't empty.
206 EmmcPeimIsMemBlockEmpty (
207 IN EMMC_PEIM_MEM_BLOCK
*Block
213 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
214 if (Block
->Bits
[Index
] != 0) {
223 Unlink the memory block from the pool's list.
225 @param Head The block list head of the memory's pool.
226 @param BlockToUnlink The memory block to unlink.
230 EmmcPeimUnlinkMemBlock (
231 IN EMMC_PEIM_MEM_BLOCK
*Head
,
232 IN EMMC_PEIM_MEM_BLOCK
*BlockToUnlink
235 EMMC_PEIM_MEM_BLOCK
*Block
;
237 ASSERT ((Head
!= NULL
) && (BlockToUnlink
!= NULL
));
239 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
240 if (Block
->Next
== BlockToUnlink
) {
241 Block
->Next
= BlockToUnlink
->Next
;
242 BlockToUnlink
->Next
= NULL
;
249 Initialize the memory management pool for the host controller.
251 @param Private The Emmc Peim driver private data.
253 @retval EFI_SUCCESS The memory pool is initialized.
254 @retval Others Fail to init the memory pool.
258 EmmcPeimInitMemPool (
259 IN EMMC_PEIM_HC_PRIVATE_DATA
*Private
262 EMMC_PEIM_MEM_POOL
*Pool
;
269 Status
= PeiServicesAllocatePool (sizeof (EMMC_PEIM_MEM_POOL
), &TempPtr
);
270 if (EFI_ERROR (Status
)) {
271 return EFI_OUT_OF_RESOURCES
;
274 ZeroMem ((VOID
*)(UINTN
)TempPtr
, sizeof (EMMC_PEIM_MEM_POOL
));
276 Pool
= (EMMC_PEIM_MEM_POOL
*)((UINTN
)TempPtr
);
278 Pool
->Head
= EmmcPeimAllocMemBlock (EMMC_PEIM_MEM_DEFAULT_PAGES
);
280 if (Pool
->Head
== NULL
) {
281 return EFI_OUT_OF_RESOURCES
;
284 Private
->Pool
= Pool
;
289 Release the memory management pool.
291 @param Pool The memory pool to free.
293 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
294 @retval EFI_SUCCESS The memory pool is freed.
298 EmmcPeimFreeMemPool (
299 IN EMMC_PEIM_MEM_POOL
*Pool
302 EMMC_PEIM_MEM_BLOCK
*Block
;
304 ASSERT (Pool
->Head
!= NULL
);
307 // Unlink all the memory blocks from the pool, then free them.
308 // EmmcPeimUnlinkMemBlock can't be used to unlink and free the
311 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
312 EmmcPeimFreeMemBlock (Pool
, Block
);
315 EmmcPeimFreeMemBlock (Pool
, Pool
->Head
);
321 Allocate some memory from the host controller's memory pool
322 which can be used to communicate with host controller.
324 @param Pool The host controller's memory pool.
325 @param Size Size of the memory to allocate.
327 @return The allocated memory or NULL.
331 EmmcPeimAllocateMem (
332 IN EMMC_PEIM_MEM_POOL
*Pool
,
336 EMMC_PEIM_MEM_BLOCK
*Head
;
337 EMMC_PEIM_MEM_BLOCK
*Block
;
338 EMMC_PEIM_MEM_BLOCK
*NewBlock
;
344 AllocSize
= EMMC_PEIM_MEM_ROUND (Size
);
346 ASSERT (Head
!= NULL
);
349 // First check whether current memory blocks can satisfy the allocation.
351 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
352 Mem
= EmmcPeimAllocMemFromBlock (Block
, AllocSize
/ EMMC_PEIM_MEM_UNIT
);
365 // Create a new memory block if there is not enough memory
366 // in the pool. If the allocation size is larger than the
367 // default page number, just allocate a large enough memory
368 // block. Otherwise allocate default pages.
370 if (AllocSize
> EFI_PAGES_TO_SIZE (EMMC_PEIM_MEM_DEFAULT_PAGES
)) {
371 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
373 Pages
= EMMC_PEIM_MEM_DEFAULT_PAGES
;
376 NewBlock
= EmmcPeimAllocMemBlock (Pages
);
377 if (NewBlock
== NULL
) {
382 // Add the new memory block to the pool, then allocate memory from it
384 EmmcPeimInsertMemBlockToPool (Head
, NewBlock
);
385 Mem
= EmmcPeimAllocMemFromBlock (NewBlock
, AllocSize
/ EMMC_PEIM_MEM_UNIT
);
395 Free the allocated memory back to the memory pool.
397 @param Pool The memory pool of the host controller.
398 @param Mem The memory to free.
399 @param Size The size of the memory to free.
404 IN EMMC_PEIM_MEM_POOL
*Pool
,
409 EMMC_PEIM_MEM_BLOCK
*Head
;
410 EMMC_PEIM_MEM_BLOCK
*Block
;
418 AllocSize
= EMMC_PEIM_MEM_ROUND (Size
);
419 ToFree
= (UINT8
*) Mem
;
421 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
423 // scan the memory block list for the memory block that
424 // completely contains the memory to free.
426 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
428 // compute the start byte and bit in the bit array
430 Byte
= ((ToFree
- Block
->Buf
) / EMMC_PEIM_MEM_UNIT
) / 8;
431 Bit
= ((ToFree
- Block
->Buf
) / EMMC_PEIM_MEM_UNIT
) % 8;
434 // reset associated bits in bit array
436 for (Count
= 0; Count
< (AllocSize
/ EMMC_PEIM_MEM_UNIT
); Count
++) {
437 ASSERT (EMMC_PEIM_MEM_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
439 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ EMMC_PEIM_MEM_BIT (Bit
));
440 EMMC_PEIM_NEXT_BIT (Byte
, Bit
);
448 // If Block == NULL, it means that the current memory isn't
449 // in the host controller's pool. This is critical because
450 // the caller has passed in a wrong memory point
452 ASSERT (Block
!= NULL
);
455 // Release the current memory block if it is empty and not the head
457 if ((Block
!= Head
) && EmmcPeimIsMemBlockEmpty (Block
)) {
458 EmmcPeimFreeMemBlock (Pool
, Block
);