3 Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "UfsBlockIoPei.h"
12 Allocate a block of memory to be used by the buffer pool.
14 @param Pages How many pages to allocate.
16 @return The allocated memory block or NULL if failed.
20 UfsPeimAllocMemBlock (
24 UFS_PEIM_MEM_BLOCK
*Block
;
27 EFI_PHYSICAL_ADDRESS MappedAddr
;
34 Status
= PeiServicesAllocatePool (sizeof(UFS_PEIM_MEM_BLOCK
), &TempPtr
);
35 if (EFI_ERROR (Status
)) {
39 ZeroMem ((VOID
*)(UINTN
)TempPtr
, sizeof(UFS_PEIM_MEM_BLOCK
));
42 // each bit in the bit array represents UFS_PEIM_MEM_UNIT
43 // bytes of memory in the memory block.
45 ASSERT (UFS_PEIM_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
47 Block
= (UFS_PEIM_MEM_BLOCK
*)(UINTN
)TempPtr
;
48 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
49 Block
->BitsLen
= Block
->BufLen
/ (UFS_PEIM_MEM_UNIT
* 8);
51 Status
= PeiServicesAllocatePool (Block
->BitsLen
, &TempPtr
);
52 if (EFI_ERROR (Status
)) {
56 ZeroMem ((VOID
*)(UINTN
)TempPtr
, Block
->BitsLen
);
58 Block
->Bits
= (UINT8
*)(UINTN
)TempPtr
;
60 Status
= IoMmuAllocateBuffer (
66 if (EFI_ERROR (Status
)) {
70 ZeroMem ((VOID
*)(UINTN
)BufHost
, EFI_PAGES_TO_SIZE (Pages
));
72 Block
->BufHost
= (UINT8
*) (UINTN
) BufHost
;
73 Block
->Buf
= (UINT8
*) (UINTN
) MappedAddr
;
74 Block
->Mapping
= Mapping
;
81 Free the memory block from the memory pool.
83 @param Pool The memory pool to free the block from.
84 @param Block The memory block to free.
89 IN UFS_PEIM_MEM_POOL
*Pool
,
90 IN UFS_PEIM_MEM_BLOCK
*Block
93 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
95 IoMmuFreeBuffer (EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
, Block
->Mapping
);
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 UfsPeimAllocMemFromBlock (
110 IN UFS_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 (!UFS_PEIM_MEM_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
136 if (Available
>= Units
) {
140 UFS_PEIM_NEXT_BIT (Byte
, Bit
);
143 UFS_PEIM_NEXT_BIT (Byte
, Bit
);
151 if (Available
< Units
) {
156 // Mark the memory as allocated
161 for (Count
= 0; Count
< Units
; Count
++) {
162 ASSERT (!UFS_PEIM_MEM_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
164 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] | (UINT8
) UFS_PEIM_MEM_BIT (Bit
));
165 UFS_PEIM_NEXT_BIT (Byte
, Bit
);
168 return Block
->Buf
+ (StartByte
* 8 + StartBit
) * UFS_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 UfsPeimInsertMemBlockToPool (
180 IN UFS_PEIM_MEM_BLOCK
*Head
,
181 IN UFS_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 UfsPeimIsMemBlockEmpty (
200 IN UFS_PEIM_MEM_BLOCK
*Block
206 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
207 if (Block
->Bits
[Index
] != 0) {
218 Initialize the memory management pool for the host controller.
220 @param Private The Ufs Peim driver private data.
222 @retval EFI_SUCCESS The memory pool is initialized.
223 @retval Others Fail to init the memory pool.
228 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
231 UFS_PEIM_MEM_POOL
*Pool
;
238 Status
= PeiServicesAllocatePool (sizeof (UFS_PEIM_MEM_POOL
), &TempPtr
);
239 if (EFI_ERROR (Status
)) {
240 return EFI_OUT_OF_RESOURCES
;
243 ZeroMem ((VOID
*)(UINTN
)TempPtr
, sizeof (UFS_PEIM_MEM_POOL
));
245 Pool
= (UFS_PEIM_MEM_POOL
*)((UINTN
)TempPtr
);
247 Pool
->Head
= UfsPeimAllocMemBlock (UFS_PEIM_MEM_DEFAULT_PAGES
);
249 if (Pool
->Head
== NULL
) {
250 return EFI_OUT_OF_RESOURCES
;
253 Private
->Pool
= Pool
;
258 Release the memory management pool.
260 @param Pool The memory pool to free.
262 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
263 @retval EFI_SUCCESS The memory pool is freed.
268 IN UFS_PEIM_MEM_POOL
*Pool
271 UFS_PEIM_MEM_BLOCK
*Block
;
273 ASSERT (Pool
->Head
!= NULL
);
276 // Unlink all the memory blocks from the pool, then free them.
278 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
279 UfsPeimFreeMemBlock (Pool
, Block
);
282 UfsPeimFreeMemBlock (Pool
, Pool
->Head
);
288 Allocate some memory from the host controller's memory pool
289 which can be used to communicate with host controller.
291 @param Pool The host controller's memory pool.
292 @param Size Size of the memory to allocate.
294 @return The allocated memory or NULL.
299 IN UFS_PEIM_MEM_POOL
*Pool
,
303 UFS_PEIM_MEM_BLOCK
*Head
;
304 UFS_PEIM_MEM_BLOCK
*Block
;
305 UFS_PEIM_MEM_BLOCK
*NewBlock
;
311 AllocSize
= UFS_PEIM_MEM_ROUND (Size
);
313 ASSERT (Head
!= NULL
);
316 // First check whether current memory blocks can satisfy the allocation.
318 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
319 Mem
= UfsPeimAllocMemFromBlock (Block
, AllocSize
/ UFS_PEIM_MEM_UNIT
);
332 // Create a new memory block if there is not enough memory
333 // in the pool. If the allocation size is larger than the
334 // default page number, just allocate a large enough memory
335 // block. Otherwise allocate default pages.
337 if (AllocSize
> EFI_PAGES_TO_SIZE (UFS_PEIM_MEM_DEFAULT_PAGES
)) {
338 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
340 Pages
= UFS_PEIM_MEM_DEFAULT_PAGES
;
343 NewBlock
= UfsPeimAllocMemBlock (Pages
);
344 if (NewBlock
== NULL
) {
349 // Add the new memory block to the pool, then allocate memory from it
351 UfsPeimInsertMemBlockToPool (Head
, NewBlock
);
352 Mem
= UfsPeimAllocMemFromBlock (NewBlock
, AllocSize
/ UFS_PEIM_MEM_UNIT
);
362 Free the allocated memory back to the memory pool.
364 @param Pool The memory pool of the host controller.
365 @param Mem The memory to free.
366 @param Size The size of the memory to free.
371 IN UFS_PEIM_MEM_POOL
*Pool
,
376 UFS_PEIM_MEM_BLOCK
*Head
;
377 UFS_PEIM_MEM_BLOCK
*Block
;
385 AllocSize
= UFS_PEIM_MEM_ROUND (Size
);
386 ToFree
= (UINT8
*) Mem
;
388 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
390 // scan the memory block list for the memory block that
391 // completely contains the memory to free.
393 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
395 // compute the start byte and bit in the bit array
397 Byte
= ((ToFree
- Block
->Buf
) / UFS_PEIM_MEM_UNIT
) / 8;
398 Bit
= ((ToFree
- Block
->Buf
) / UFS_PEIM_MEM_UNIT
) % 8;
401 // reset associated bits in bit array
403 for (Count
= 0; Count
< (AllocSize
/ UFS_PEIM_MEM_UNIT
); Count
++) {
404 ASSERT (UFS_PEIM_MEM_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
406 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ UFS_PEIM_MEM_BIT (Bit
));
407 UFS_PEIM_NEXT_BIT (Byte
, Bit
);
415 // If Block == NULL, it means that the current memory isn't
416 // in the host controller's pool. This is critical because
417 // the caller has passed in a wrong memory point
419 ASSERT (Block
!= NULL
);
422 // Release the current memory block if it is empty and not the head
424 if ((Block
!= Head
) && UfsPeimIsMemBlockEmpty (Block
)) {
425 UfsPeimFreeMemBlock (Pool
, Block
);