3 Copyright (c) 2016, 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 Address
;
39 Status
= PeiServicesAllocatePool (sizeof(EMMC_PEIM_MEM_BLOCK
), &TempPtr
);
40 if (EFI_ERROR (Status
)) {
44 ZeroMem ((VOID
*)(UINTN
)TempPtr
, sizeof(EMMC_PEIM_MEM_BLOCK
));
47 // each bit in the bit array represents EMMC_PEIM_MEM_UNIT
48 // bytes of memory in the memory block.
50 ASSERT (EMMC_PEIM_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
52 Block
= (EMMC_PEIM_MEM_BLOCK
*)(UINTN
)TempPtr
;
53 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
54 Block
->BitsLen
= Block
->BufLen
/ (EMMC_PEIM_MEM_UNIT
* 8);
56 Status
= PeiServicesAllocatePool (Block
->BitsLen
, &TempPtr
);
57 if (EFI_ERROR (Status
)) {
61 ZeroMem ((VOID
*)(UINTN
)TempPtr
, Block
->BitsLen
);
63 Block
->Bits
= (UINT8
*)(UINTN
)TempPtr
;
65 Status
= PeiServicesAllocatePages (
70 if (EFI_ERROR (Status
)) {
74 ZeroMem ((VOID
*)(UINTN
)Address
, EFI_PAGES_TO_SIZE (Pages
));
76 Block
->Buf
= (UINT8
*)((UINTN
)Address
);
83 Free the memory block from the memory pool.
85 @param Pool The memory pool to free the block from.
86 @param Block The memory block to free.
90 EmmcPeimFreeMemBlock (
91 IN EMMC_PEIM_MEM_POOL
*Pool
,
92 IN EMMC_PEIM_MEM_BLOCK
*Block
95 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
99 Alloc some memory from the block.
101 @param Block The memory block to allocate memory from.
102 @param Units Number of memory units to allocate.
104 @return The pointer to the allocated memory. If couldn't allocate the needed memory,
105 the return value is NULL.
109 EmmcPeimAllocMemFromBlock (
110 IN EMMC_PEIM_MEM_BLOCK
*Block
,
121 ASSERT ((Block
!= 0) && (Units
!= 0));
127 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
129 // If current bit is zero, the corresponding memory unit is
130 // available, otherwise we need to restart our searching.
131 // Available counts the consective number of zero bit.
133 if (!EMMC_PEIM_MEM_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
136 if (Available
>= Units
) {
140 EMMC_PEIM_NEXT_BIT (Byte
, Bit
);
143 EMMC_PEIM_NEXT_BIT (Byte
, Bit
);
151 if (Available
< Units
) {
156 // Mark the memory as allocated
161 for (Count
= 0; Count
< Units
; Count
++) {
162 ASSERT (!EMMC_PEIM_MEM_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
164 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] | (UINT8
) EMMC_PEIM_MEM_BIT (Bit
));
165 EMMC_PEIM_NEXT_BIT (Byte
, Bit
);
168 return Block
->Buf
+ (StartByte
* 8 + StartBit
) * EMMC_PEIM_MEM_UNIT
;
172 Insert the memory block to the pool's list of the blocks.
174 @param Head The head of the memory pool's block list.
175 @param Block The memory block to insert.
179 EmmcPeimInsertMemBlockToPool (
180 IN EMMC_PEIM_MEM_BLOCK
*Head
,
181 IN EMMC_PEIM_MEM_BLOCK
*Block
184 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
185 Block
->Next
= Head
->Next
;
190 Is the memory block empty?
192 @param Block The memory block to check.
194 @retval TRUE The memory block is empty.
195 @retval FALSE The memory block isn't empty.
199 EmmcPeimIsMemBlockEmpty (
200 IN EMMC_PEIM_MEM_BLOCK
*Block
206 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
207 if (Block
->Bits
[Index
] != 0) {
216 Unlink the memory block from the pool's list.
218 @param Head The block list head of the memory's pool.
219 @param BlockToUnlink The memory block to unlink.
223 EmmcPeimUnlinkMemBlock (
224 IN EMMC_PEIM_MEM_BLOCK
*Head
,
225 IN EMMC_PEIM_MEM_BLOCK
*BlockToUnlink
228 EMMC_PEIM_MEM_BLOCK
*Block
;
230 ASSERT ((Head
!= NULL
) && (BlockToUnlink
!= NULL
));
232 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
233 if (Block
->Next
== BlockToUnlink
) {
234 Block
->Next
= BlockToUnlink
->Next
;
235 BlockToUnlink
->Next
= NULL
;
242 Initialize the memory management pool for the host controller.
244 @param Private The Emmc Peim driver private data.
246 @retval EFI_SUCCESS The memory pool is initialized.
247 @retval Others Fail to init the memory pool.
251 EmmcPeimInitMemPool (
252 IN EMMC_PEIM_HC_PRIVATE_DATA
*Private
255 EMMC_PEIM_MEM_POOL
*Pool
;
262 Status
= PeiServicesAllocatePool (sizeof (EMMC_PEIM_MEM_POOL
), &TempPtr
);
263 if (EFI_ERROR (Status
)) {
264 return EFI_OUT_OF_RESOURCES
;
267 ZeroMem ((VOID
*)(UINTN
)TempPtr
, sizeof (EMMC_PEIM_MEM_POOL
));
269 Pool
= (EMMC_PEIM_MEM_POOL
*)((UINTN
)TempPtr
);
271 Pool
->Head
= EmmcPeimAllocMemBlock (EMMC_PEIM_MEM_DEFAULT_PAGES
);
273 if (Pool
->Head
== NULL
) {
274 return EFI_OUT_OF_RESOURCES
;
277 Private
->Pool
= Pool
;
282 Release the memory management pool.
284 @param Pool The memory pool to free.
286 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
287 @retval EFI_SUCCESS The memory pool is freed.
291 EmmcPeimFreeMemPool (
292 IN EMMC_PEIM_MEM_POOL
*Pool
295 EMMC_PEIM_MEM_BLOCK
*Block
;
297 ASSERT (Pool
->Head
!= NULL
);
300 // Unlink all the memory blocks from the pool, then free them.
301 // EmmcPeimUnlinkMemBlock can't be used to unlink and free the
304 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
305 EmmcPeimFreeMemBlock (Pool
, Block
);
308 EmmcPeimFreeMemBlock (Pool
, Pool
->Head
);
314 Allocate some memory from the host controller's memory pool
315 which can be used to communicate with host controller.
317 @param Pool The host controller's memory pool.
318 @param Size Size of the memory to allocate.
320 @return The allocated memory or NULL.
324 EmmcPeimAllocateMem (
325 IN EMMC_PEIM_MEM_POOL
*Pool
,
329 EMMC_PEIM_MEM_BLOCK
*Head
;
330 EMMC_PEIM_MEM_BLOCK
*Block
;
331 EMMC_PEIM_MEM_BLOCK
*NewBlock
;
337 AllocSize
= EMMC_PEIM_MEM_ROUND (Size
);
339 ASSERT (Head
!= NULL
);
342 // First check whether current memory blocks can satisfy the allocation.
344 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
345 Mem
= EmmcPeimAllocMemFromBlock (Block
, AllocSize
/ EMMC_PEIM_MEM_UNIT
);
358 // Create a new memory block if there is not enough memory
359 // in the pool. If the allocation size is larger than the
360 // default page number, just allocate a large enough memory
361 // block. Otherwise allocate default pages.
363 if (AllocSize
> EFI_PAGES_TO_SIZE (EMMC_PEIM_MEM_DEFAULT_PAGES
)) {
364 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
366 Pages
= EMMC_PEIM_MEM_DEFAULT_PAGES
;
369 NewBlock
= EmmcPeimAllocMemBlock (Pages
);
370 if (NewBlock
== NULL
) {
375 // Add the new memory block to the pool, then allocate memory from it
377 EmmcPeimInsertMemBlockToPool (Head
, NewBlock
);
378 Mem
= EmmcPeimAllocMemFromBlock (NewBlock
, AllocSize
/ EMMC_PEIM_MEM_UNIT
);
388 Free the allocated memory back to the memory pool.
390 @param Pool The memory pool of the host controller.
391 @param Mem The memory to free.
392 @param Size The size of the memory to free.
397 IN EMMC_PEIM_MEM_POOL
*Pool
,
402 EMMC_PEIM_MEM_BLOCK
*Head
;
403 EMMC_PEIM_MEM_BLOCK
*Block
;
411 AllocSize
= EMMC_PEIM_MEM_ROUND (Size
);
412 ToFree
= (UINT8
*) Mem
;
414 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
416 // scan the memory block list for the memory block that
417 // completely contains the memory to free.
419 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
421 // compute the start byte and bit in the bit array
423 Byte
= ((ToFree
- Block
->Buf
) / EMMC_PEIM_MEM_UNIT
) / 8;
424 Bit
= ((ToFree
- Block
->Buf
) / EMMC_PEIM_MEM_UNIT
) % 8;
427 // reset associated bits in bit array
429 for (Count
= 0; Count
< (AllocSize
/ EMMC_PEIM_MEM_UNIT
); Count
++) {
430 ASSERT (EMMC_PEIM_MEM_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
432 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ EMMC_PEIM_MEM_BIT (Bit
));
433 EMMC_PEIM_NEXT_BIT (Byte
, Bit
);
441 // If Block == NULL, it means that the current memory isn't
442 // in the host controller's pool. This is critical because
443 // the caller has passed in a wrong memory point
445 ASSERT (Block
!= NULL
);
448 // Release the current memory block if it is empty and not the head
450 if ((Block
!= Head
) && EmmcPeimIsMemBlockEmpty (Block
)) {
451 EmmcPeimFreeMemBlock (Pool
, Block
);