3 Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
4 Copyright (c) 1985 - 2022, American Megatrends International LLC. <BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "UfsBlockIoPei.h"
13 Allocate a block of memory to be used by the buffer pool.
15 @param Pages How many pages to allocate.
17 @return The allocated memory block or NULL if failed.
21 UfsPeimAllocMemBlock (
25 UFS_PEIM_MEM_BLOCK
*Block
;
28 EFI_PHYSICAL_ADDRESS MappedAddr
;
35 Status
= PeiServicesAllocatePool (sizeof (UFS_PEIM_MEM_BLOCK
), &TempPtr
);
36 if (EFI_ERROR (Status
)) {
40 ZeroMem ((VOID
*)(UINTN
)TempPtr
, sizeof (UFS_PEIM_MEM_BLOCK
));
43 // each bit in the bit array represents UFS_PEIM_MEM_UNIT
44 // bytes of memory in the memory block.
46 ASSERT (UFS_PEIM_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
48 Block
= (UFS_PEIM_MEM_BLOCK
*)(UINTN
)TempPtr
;
49 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
50 Block
->BitsLen
= Block
->BufLen
/ (UFS_PEIM_MEM_UNIT
* 8);
52 Status
= PeiServicesAllocatePool (Block
->BitsLen
, &TempPtr
);
53 if (EFI_ERROR (Status
)) {
57 ZeroMem ((VOID
*)(UINTN
)TempPtr
, Block
->BitsLen
);
59 Block
->Bits
= (UINT8
*)(UINTN
)TempPtr
;
61 Status
= IoMmuAllocateBuffer (
67 if (EFI_ERROR (Status
)) {
71 ZeroMem ((VOID
*)(UINTN
)BufHost
, EFI_PAGES_TO_SIZE (Pages
));
73 Block
->BufHost
= (UINT8
*)(UINTN
)BufHost
;
74 Block
->Buf
= (UINT8
*)(UINTN
)MappedAddr
;
75 Block
->Mapping
= Mapping
;
82 Free the memory block from the memory pool.
84 @param Pool The memory pool to free the block from.
85 @param Block The memory block to free.
90 IN UFS_PEIM_MEM_POOL
*Pool
,
91 IN UFS_PEIM_MEM_BLOCK
*Block
94 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
96 IoMmuFreeBuffer (EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
, Block
->Mapping
);
100 Alloc some memory from the block.
102 @param Block The memory block to allocate memory from.
103 @param Units Number of memory units to allocate.
105 @return The pointer to the allocated memory. If couldn't allocate the needed memory,
106 the return value is NULL.
110 UfsPeimAllocMemFromBlock (
111 IN UFS_PEIM_MEM_BLOCK
*Block
,
122 ASSERT ((Block
!= 0) && (Units
!= 0));
128 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
130 // If current bit is zero, the corresponding memory unit is
131 // available, otherwise we need to restart our searching.
132 // Available counts the consective number of zero bit.
134 if (!UFS_PEIM_MEM_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
137 if (Available
>= Units
) {
141 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
205 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
206 if (Block
->Bits
[Index
] != 0) {
215 Initialize the memory management pool for the host controller.
217 @param Private The Ufs Peim driver private data.
219 @retval EFI_SUCCESS The memory pool is initialized.
220 @retval Others Fail to init the memory pool.
225 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
228 UFS_PEIM_MEM_POOL
*Pool
;
235 Status
= PeiServicesAllocatePool (sizeof (UFS_PEIM_MEM_POOL
), &TempPtr
);
236 if (EFI_ERROR (Status
)) {
237 return EFI_OUT_OF_RESOURCES
;
240 ZeroMem ((VOID
*)(UINTN
)TempPtr
, sizeof (UFS_PEIM_MEM_POOL
));
242 Pool
= (UFS_PEIM_MEM_POOL
*)((UINTN
)TempPtr
);
244 Pool
->Head
= UfsPeimAllocMemBlock (UFS_PEIM_MEM_DEFAULT_PAGES
);
246 if (Pool
->Head
== NULL
) {
247 return EFI_OUT_OF_RESOURCES
;
250 Private
->Pool
= Pool
;
255 Release the memory management pool.
257 @param Pool The memory pool to free.
259 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
260 @retval EFI_SUCCESS The memory pool is freed.
265 IN UFS_PEIM_MEM_POOL
*Pool
268 UFS_PEIM_MEM_BLOCK
*Block
;
270 ASSERT (Pool
->Head
!= NULL
);
273 // Unlink all the memory blocks from the pool, then free them.
275 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
276 UfsPeimFreeMemBlock (Pool
, Block
);
279 UfsPeimFreeMemBlock (Pool
, Pool
->Head
);
285 Allocate some memory from the host controller's memory pool
286 which can be used to communicate with host controller.
288 @param Pool The host controller's memory pool.
289 @param Size Size of the memory to allocate.
291 @return The allocated memory or NULL.
296 IN UFS_PEIM_MEM_POOL
*Pool
,
300 UFS_PEIM_MEM_BLOCK
*Head
;
301 UFS_PEIM_MEM_BLOCK
*Block
;
302 UFS_PEIM_MEM_BLOCK
*NewBlock
;
308 AllocSize
= UFS_PEIM_MEM_ROUND (Size
);
310 ASSERT (Head
!= NULL
);
313 // First check whether current memory blocks can satisfy the allocation.
315 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
316 Mem
= UfsPeimAllocMemFromBlock (Block
, AllocSize
/ UFS_PEIM_MEM_UNIT
);
329 // Create a new memory block if there is not enough memory
330 // in the pool. If the allocation size is larger than the
331 // default page number, just allocate a large enough memory
332 // block. Otherwise allocate default pages.
334 if (AllocSize
> EFI_PAGES_TO_SIZE (UFS_PEIM_MEM_DEFAULT_PAGES
)) {
335 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
337 Pages
= UFS_PEIM_MEM_DEFAULT_PAGES
;
340 NewBlock
= UfsPeimAllocMemBlock (Pages
);
341 if (NewBlock
== NULL
) {
346 // Add the new memory block to the pool, then allocate memory from it
348 UfsPeimInsertMemBlockToPool (Head
, NewBlock
);
349 Mem
= UfsPeimAllocMemFromBlock (NewBlock
, AllocSize
/ UFS_PEIM_MEM_UNIT
);
359 Free the allocated memory back to the memory pool.
361 @param Pool The memory pool of the host controller.
362 @param Mem The memory to free.
363 @param Size The size of the memory to free.
368 IN UFS_PEIM_MEM_POOL
*Pool
,
373 UFS_PEIM_MEM_BLOCK
*Head
;
374 UFS_PEIM_MEM_BLOCK
*Block
;
382 AllocSize
= UFS_PEIM_MEM_ROUND (Size
);
383 ToFree
= (UINT8
*)Mem
;
385 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
387 // scan the memory block list for the memory block that
388 // completely contains the memory to free.
390 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
392 // compute the start byte and bit in the bit array
394 Byte
= ((ToFree
- Block
->Buf
) / UFS_PEIM_MEM_UNIT
) / 8;
395 Bit
= ((ToFree
- Block
->Buf
) / UFS_PEIM_MEM_UNIT
) % 8;
398 // reset associated bits in bit array
400 for (Count
= 0; Count
< (AllocSize
/ UFS_PEIM_MEM_UNIT
); Count
++) {
401 ASSERT (UFS_PEIM_MEM_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
403 Block
->Bits
[Byte
] = (UINT8
)(Block
->Bits
[Byte
] ^ UFS_PEIM_MEM_BIT (Bit
));
404 UFS_PEIM_NEXT_BIT (Byte
, Bit
);
412 // If Block == NULL, it means that the current memory isn't
413 // in the host controller's pool. This is critical because
414 // the caller has passed in a wrong memory point
416 ASSERT (Block
!= NULL
);
423 // Release the current memory block if it is empty and not the head
425 if ((Block
!= Head
) && UfsPeimIsMemBlockEmpty (Block
)) {
426 UfsPeimFreeMemBlock (Pool
, Block
);