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) {
225 Initialize the memory management pool for the host controller.
227 @param Private The Emmc Peim driver private data.
229 @retval EFI_SUCCESS The memory pool is initialized.
230 @retval Others Fail to init the memory pool.
234 EmmcPeimInitMemPool (
235 IN EMMC_PEIM_HC_PRIVATE_DATA
*Private
238 EMMC_PEIM_MEM_POOL
*Pool
;
245 Status
= PeiServicesAllocatePool (sizeof (EMMC_PEIM_MEM_POOL
), &TempPtr
);
246 if (EFI_ERROR (Status
)) {
247 return EFI_OUT_OF_RESOURCES
;
250 ZeroMem ((VOID
*)(UINTN
)TempPtr
, sizeof (EMMC_PEIM_MEM_POOL
));
252 Pool
= (EMMC_PEIM_MEM_POOL
*)((UINTN
)TempPtr
);
254 Pool
->Head
= EmmcPeimAllocMemBlock (EMMC_PEIM_MEM_DEFAULT_PAGES
);
256 if (Pool
->Head
== NULL
) {
257 return EFI_OUT_OF_RESOURCES
;
260 Private
->Pool
= Pool
;
265 Release the memory management pool.
267 @param Pool The memory pool to free.
269 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
270 @retval EFI_SUCCESS The memory pool is freed.
274 EmmcPeimFreeMemPool (
275 IN EMMC_PEIM_MEM_POOL
*Pool
278 EMMC_PEIM_MEM_BLOCK
*Block
;
280 ASSERT (Pool
->Head
!= NULL
);
283 // Unlink all the memory blocks from the pool, then free them.
285 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
286 EmmcPeimFreeMemBlock (Pool
, Block
);
289 EmmcPeimFreeMemBlock (Pool
, Pool
->Head
);
295 Allocate some memory from the host controller's memory pool
296 which can be used to communicate with host controller.
298 @param Pool The host controller's memory pool.
299 @param Size Size of the memory to allocate.
301 @return The allocated memory or NULL.
305 EmmcPeimAllocateMem (
306 IN EMMC_PEIM_MEM_POOL
*Pool
,
310 EMMC_PEIM_MEM_BLOCK
*Head
;
311 EMMC_PEIM_MEM_BLOCK
*Block
;
312 EMMC_PEIM_MEM_BLOCK
*NewBlock
;
318 AllocSize
= EMMC_PEIM_MEM_ROUND (Size
);
320 ASSERT (Head
!= NULL
);
323 // First check whether current memory blocks can satisfy the allocation.
325 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
326 Mem
= EmmcPeimAllocMemFromBlock (Block
, AllocSize
/ EMMC_PEIM_MEM_UNIT
);
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.
344 if (AllocSize
> EFI_PAGES_TO_SIZE (EMMC_PEIM_MEM_DEFAULT_PAGES
)) {
345 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
347 Pages
= EMMC_PEIM_MEM_DEFAULT_PAGES
;
350 NewBlock
= EmmcPeimAllocMemBlock (Pages
);
351 if (NewBlock
== NULL
) {
356 // Add the new memory block to the pool, then allocate memory from it
358 EmmcPeimInsertMemBlockToPool (Head
, NewBlock
);
359 Mem
= EmmcPeimAllocMemFromBlock (NewBlock
, AllocSize
/ EMMC_PEIM_MEM_UNIT
);
369 Free the allocated memory back to the memory pool.
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.
378 IN EMMC_PEIM_MEM_POOL
*Pool
,
383 EMMC_PEIM_MEM_BLOCK
*Head
;
384 EMMC_PEIM_MEM_BLOCK
*Block
;
392 AllocSize
= EMMC_PEIM_MEM_ROUND (Size
);
393 ToFree
= (UINT8
*) Mem
;
395 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
397 // scan the memory block list for the memory block that
398 // completely contains the memory to free.
400 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
402 // compute the start byte and bit in the bit array
404 Byte
= ((ToFree
- Block
->Buf
) / EMMC_PEIM_MEM_UNIT
) / 8;
405 Bit
= ((ToFree
- Block
->Buf
) / EMMC_PEIM_MEM_UNIT
) % 8;
408 // reset associated bits in bit array
410 for (Count
= 0; Count
< (AllocSize
/ EMMC_PEIM_MEM_UNIT
); Count
++) {
411 ASSERT (EMMC_PEIM_MEM_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
413 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ EMMC_PEIM_MEM_BIT (Bit
));
414 EMMC_PEIM_NEXT_BIT (Byte
, Bit
);
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
426 ASSERT (Block
!= NULL
);
429 // Release the current memory block if it is empty and not the head
431 if ((Block
!= Head
) && EmmcPeimIsMemBlockEmpty (Block
)) {
432 EmmcPeimFreeMemBlock (Pool
, Block
);