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
);
142 UFS_PEIM_NEXT_BIT (Byte
, Bit
);
150 if (Available
< Units
) {
155 // Mark the memory as allocated
160 for (Count
= 0; Count
< Units
; Count
++) {
161 ASSERT (!UFS_PEIM_MEM_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
163 Block
->Bits
[Byte
] = (UINT8
)(Block
->Bits
[Byte
] | (UINT8
)UFS_PEIM_MEM_BIT (Bit
));
164 UFS_PEIM_NEXT_BIT (Byte
, Bit
);
167 return Block
->Buf
+ (StartByte
* 8 + StartBit
) * UFS_PEIM_MEM_UNIT
;
171 Insert the memory block to the pool's list of the blocks.
173 @param Head The head of the memory pool's block list.
174 @param Block The memory block to insert.
178 UfsPeimInsertMemBlockToPool (
179 IN UFS_PEIM_MEM_BLOCK
*Head
,
180 IN UFS_PEIM_MEM_BLOCK
*Block
183 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
184 Block
->Next
= Head
->Next
;
189 Is the memory block empty?
191 @param Block The memory block to check.
193 @retval TRUE The memory block is empty.
194 @retval FALSE The memory block isn't empty.
198 UfsPeimIsMemBlockEmpty (
199 IN UFS_PEIM_MEM_BLOCK
*Block
204 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
205 if (Block
->Bits
[Index
] != 0) {
214 Initialize the memory management pool for the host controller.
216 @param Private The Ufs Peim driver private data.
218 @retval EFI_SUCCESS The memory pool is initialized.
219 @retval Others Fail to init the memory pool.
224 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
227 UFS_PEIM_MEM_POOL
*Pool
;
234 Status
= PeiServicesAllocatePool (sizeof (UFS_PEIM_MEM_POOL
), &TempPtr
);
235 if (EFI_ERROR (Status
)) {
236 return EFI_OUT_OF_RESOURCES
;
239 ZeroMem ((VOID
*)(UINTN
)TempPtr
, sizeof (UFS_PEIM_MEM_POOL
));
241 Pool
= (UFS_PEIM_MEM_POOL
*)((UINTN
)TempPtr
);
243 Pool
->Head
= UfsPeimAllocMemBlock (UFS_PEIM_MEM_DEFAULT_PAGES
);
245 if (Pool
->Head
== NULL
) {
246 return EFI_OUT_OF_RESOURCES
;
249 Private
->Pool
= Pool
;
254 Release the memory management pool.
256 @param Pool The memory pool to free.
258 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
259 @retval EFI_SUCCESS The memory pool is freed.
264 IN UFS_PEIM_MEM_POOL
*Pool
267 UFS_PEIM_MEM_BLOCK
*Block
;
269 ASSERT (Pool
->Head
!= NULL
);
272 // Unlink all the memory blocks from the pool, then free them.
274 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
275 UfsPeimFreeMemBlock (Pool
, Block
);
278 UfsPeimFreeMemBlock (Pool
, Pool
->Head
);
284 Allocate some memory from the host controller's memory pool
285 which can be used to communicate with host controller.
287 @param Pool The host controller's memory pool.
288 @param Size Size of the memory to allocate.
290 @return The allocated memory or NULL.
295 IN UFS_PEIM_MEM_POOL
*Pool
,
299 UFS_PEIM_MEM_BLOCK
*Head
;
300 UFS_PEIM_MEM_BLOCK
*Block
;
301 UFS_PEIM_MEM_BLOCK
*NewBlock
;
307 AllocSize
= UFS_PEIM_MEM_ROUND (Size
);
309 ASSERT (Head
!= NULL
);
312 // First check whether current memory blocks can satisfy the allocation.
314 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
315 Mem
= UfsPeimAllocMemFromBlock (Block
, AllocSize
/ UFS_PEIM_MEM_UNIT
);
328 // Create a new memory block if there is not enough memory
329 // in the pool. If the allocation size is larger than the
330 // default page number, just allocate a large enough memory
331 // block. Otherwise allocate default pages.
333 if (AllocSize
> EFI_PAGES_TO_SIZE (UFS_PEIM_MEM_DEFAULT_PAGES
)) {
334 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
336 Pages
= UFS_PEIM_MEM_DEFAULT_PAGES
;
339 NewBlock
= UfsPeimAllocMemBlock (Pages
);
340 if (NewBlock
== NULL
) {
345 // Add the new memory block to the pool, then allocate memory from it
347 UfsPeimInsertMemBlockToPool (Head
, NewBlock
);
348 Mem
= UfsPeimAllocMemFromBlock (NewBlock
, AllocSize
/ UFS_PEIM_MEM_UNIT
);
358 Free the allocated memory back to the memory pool.
360 @param Pool The memory pool of the host controller.
361 @param Mem The memory to free.
362 @param Size The size of the memory to free.
367 IN UFS_PEIM_MEM_POOL
*Pool
,
372 UFS_PEIM_MEM_BLOCK
*Head
;
373 UFS_PEIM_MEM_BLOCK
*Block
;
381 AllocSize
= UFS_PEIM_MEM_ROUND (Size
);
382 ToFree
= (UINT8
*)Mem
;
384 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
386 // scan the memory block list for the memory block that
387 // completely contains the memory to free.
389 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
391 // compute the start byte and bit in the bit array
393 Byte
= ((ToFree
- Block
->Buf
) / UFS_PEIM_MEM_UNIT
) / 8;
394 Bit
= ((ToFree
- Block
->Buf
) / UFS_PEIM_MEM_UNIT
) % 8;
397 // reset associated bits in bit array
399 for (Count
= 0; Count
< (AllocSize
/ UFS_PEIM_MEM_UNIT
); Count
++) {
400 ASSERT (UFS_PEIM_MEM_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
402 Block
->Bits
[Byte
] = (UINT8
)(Block
->Bits
[Byte
] ^ UFS_PEIM_MEM_BIT (Bit
));
403 UFS_PEIM_NEXT_BIT (Byte
, Bit
);
411 // If Block == NULL, it means that the current memory isn't
412 // in the host controller's pool. This is critical because
413 // the caller has passed in a wrong memory point
415 ASSERT (Block
!= NULL
);
418 // Release the current memory block if it is empty and not the head
420 if ((Block
!= Head
) && UfsPeimIsMemBlockEmpty (Block
)) {
421 UfsPeimFreeMemBlock (Pool
, Block
);