3 Routine procedures for memory allocate/free.
5 Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
15 Allocate a block of memory to be used by the buffer pool.
17 @param Pool The buffer pool to allocate memory for.
18 @param Pages How many pages to allocate.
20 @return The allocated memory block or NULL if failed.
25 IN USBHC_MEM_POOL
*Pool
,
29 USBHC_MEM_BLOCK
*Block
;
30 EFI_PCI_IO_PROTOCOL
*PciIo
;
33 EFI_PHYSICAL_ADDRESS MappedAddr
;
39 Block
= AllocateZeroPool (sizeof (USBHC_MEM_BLOCK
));
45 // each bit in the bit array represents USBHC_MEM_UNIT
46 // bytes of memory in the memory block.
48 ASSERT (USBHC_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
50 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
51 Block
->BitsLen
= Block
->BufLen
/ (USBHC_MEM_UNIT
* 8);
52 Block
->Bits
= AllocateZeroPool (Block
->BitsLen
);
54 if (Block
->Bits
== NULL
) {
55 gBS
->FreePool (Block
);
60 // Allocate the number of Pages of memory, then map it for
61 // bus master read and write.
63 Status
= PciIo
->AllocateBuffer (
72 if (EFI_ERROR (Status
)) {
76 Bytes
= EFI_PAGES_TO_SIZE (Pages
);
79 EfiPciIoOperationBusMasterCommonBuffer
,
86 if (EFI_ERROR (Status
) || (Bytes
!= EFI_PAGES_TO_SIZE (Pages
))) {
91 // Check whether the data structure used by the host controller
92 // should be restricted into the same 4G
94 if (Pool
->Check4G
&& (Pool
->Which4G
!= USB_HC_HIGH_32BIT (MappedAddr
))) {
95 PciIo
->Unmap (PciIo
, Mapping
);
99 Block
->BufHost
= BufHost
;
100 Block
->Buf
= (UINT8
*) ((UINTN
) MappedAddr
);
101 Block
->Mapping
= Mapping
;
106 PciIo
->FreeBuffer (PciIo
, Pages
, BufHost
);
109 gBS
->FreePool (Block
->Bits
);
110 gBS
->FreePool (Block
);
116 Free the memory block from the memory pool.
118 @param Pool The memory pool to free the block from.
119 @param Block The memory block to free.
124 IN USBHC_MEM_POOL
*Pool
,
125 IN USBHC_MEM_BLOCK
*Block
128 EFI_PCI_IO_PROTOCOL
*PciIo
;
130 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
135 // Unmap the common buffer then free the structures
137 PciIo
->Unmap (PciIo
, Block
->Mapping
);
138 PciIo
->FreeBuffer (PciIo
, EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
);
140 gBS
->FreePool (Block
->Bits
);
141 gBS
->FreePool (Block
);
146 Alloc some memory from the block.
148 @param Block The memory block to allocate memory from.
149 @param Units Number of memory units to allocate.
151 @return The pointer to the allocated memory. If couldn't allocate the needed memory,
152 the return value is NULL.
156 UsbHcAllocMemFromBlock (
157 IN USBHC_MEM_BLOCK
*Block
,
168 ASSERT ((Block
!= 0) && (Units
!= 0));
174 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
176 // If current bit is zero, the corresponding memory unit is
177 // available, otherwise we need to restart our searching.
178 // Available counts the consective number of zero bit.
180 if (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
183 if (Available
>= Units
) {
187 NEXT_BIT (Byte
, Bit
);
190 NEXT_BIT (Byte
, Bit
);
198 if (Available
< Units
) {
203 // Mark the memory as allocated
208 for (Count
= 0; Count
< Units
; Count
++) {
209 ASSERT (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
211 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] | USB_HC_BIT (Bit
));
212 NEXT_BIT (Byte
, Bit
);
215 return Block
->BufHost
+ (StartByte
* 8 + StartBit
) * USBHC_MEM_UNIT
;
219 Calculate the corresponding pci bus address according to the Mem parameter.
221 @param Pool The memory pool of the host controller.
222 @param Mem The pointer to host memory.
223 @param Size The size of the memory region.
225 @return the pci memory address
228 UsbHcGetPciAddressForHostMem (
229 IN USBHC_MEM_POOL
*Pool
,
234 USBHC_MEM_BLOCK
*Head
;
235 USBHC_MEM_BLOCK
*Block
;
237 EFI_PHYSICAL_ADDRESS PhyAddr
;
241 AllocSize
= USBHC_MEM_ROUND (Size
);
247 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
249 // scan the memory block list for the memory block that
250 // completely contains the allocated memory.
252 if ((Block
->BufHost
<= (UINT8
*) Mem
) && (((UINT8
*) Mem
+ AllocSize
) <= (Block
->BufHost
+ Block
->BufLen
))) {
257 ASSERT ((Block
!= NULL
));
259 // calculate the pci memory address for host memory address.
261 Offset
= (UINT8
*)Mem
- Block
->BufHost
;
262 PhyAddr
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) (Block
->Buf
+ Offset
);
268 Insert the memory block to the pool's list of the blocks.
270 @param Head The head of the memory pool's block list.
271 @param Block The memory block to insert.
275 UsbHcInsertMemBlockToPool (
276 IN USBHC_MEM_BLOCK
*Head
,
277 IN USBHC_MEM_BLOCK
*Block
280 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
281 Block
->Next
= Head
->Next
;
287 Is the memory block empty?
289 @param Block The memory block to check.
291 @retval TRUE The memory block is empty.
292 @retval FALSE The memory block isn't empty.
296 UsbHcIsMemBlockEmpty (
297 IN USBHC_MEM_BLOCK
*Block
302 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
303 if (Block
->Bits
[Index
] != 0) {
313 Unlink the memory block from the pool's list.
315 @param Head The block list head of the memory's pool.
316 @param BlockToUnlink The memory block to unlink.
320 UsbHcUnlinkMemBlock (
321 IN USBHC_MEM_BLOCK
*Head
,
322 IN USBHC_MEM_BLOCK
*BlockToUnlink
325 USBHC_MEM_BLOCK
*Block
;
327 ASSERT ((Head
!= NULL
) && (BlockToUnlink
!= NULL
));
329 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
330 if (Block
->Next
== BlockToUnlink
) {
331 Block
->Next
= BlockToUnlink
->Next
;
332 BlockToUnlink
->Next
= NULL
;
340 Initialize the memory management pool for the host controller.
342 @param PciIo The PciIo that can be used to access the host controller.
343 @param Check4G Whether the host controller requires allocated memory
344 from one 4G address space.
345 @param Which4G The 4G memory area each memory allocated should be from.
347 @retval EFI_SUCCESS The memory pool is initialized.
348 @retval EFI_OUT_OF_RESOURCE Fail to init the memory pool.
353 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
358 USBHC_MEM_POOL
*Pool
;
360 Pool
= AllocatePool (sizeof (USBHC_MEM_POOL
));
367 Pool
->Check4G
= Check4G
;
368 Pool
->Which4G
= Which4G
;
369 Pool
->Head
= UsbHcAllocMemBlock (Pool
, USBHC_MEM_DEFAULT_PAGES
);
371 if (Pool
->Head
== NULL
) {
372 gBS
->FreePool (Pool
);
381 Release the memory management pool.
383 @param Pool The USB memory pool to free.
385 @retval EFI_SUCCESS The memory pool is freed.
386 @retval EFI_DEVICE_ERROR Failed to free the memory pool.
391 IN USBHC_MEM_POOL
*Pool
394 USBHC_MEM_BLOCK
*Block
;
396 ASSERT (Pool
->Head
!= NULL
);
399 // Unlink all the memory blocks from the pool, then free them.
400 // UsbHcUnlinkMemBlock can't be used to unlink and free the
403 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
404 UsbHcUnlinkMemBlock (Pool
->Head
, Block
);
405 UsbHcFreeMemBlock (Pool
, Block
);
408 UsbHcFreeMemBlock (Pool
, Pool
->Head
);
409 gBS
->FreePool (Pool
);
415 Allocate some memory from the host controller's memory pool
416 which can be used to communicate with host controller.
418 @param Pool The host controller's memory pool.
419 @param Size Size of the memory to allocate.
421 @return The allocated memory or NULL.
426 IN USBHC_MEM_POOL
*Pool
,
430 USBHC_MEM_BLOCK
*Head
;
431 USBHC_MEM_BLOCK
*Block
;
432 USBHC_MEM_BLOCK
*NewBlock
;
438 AllocSize
= USBHC_MEM_ROUND (Size
);
440 ASSERT (Head
!= NULL
);
443 // First check whether current memory blocks can satisfy the allocation.
445 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
446 Mem
= UsbHcAllocMemFromBlock (Block
, AllocSize
/ USBHC_MEM_UNIT
);
459 // Create a new memory block if there is not enough memory
460 // in the pool. If the allocation size is larger than the
461 // default page number, just allocate a large enough memory
462 // block. Otherwise allocate default pages.
464 if (AllocSize
> EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES
)) {
465 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
467 Pages
= USBHC_MEM_DEFAULT_PAGES
;
470 NewBlock
= UsbHcAllocMemBlock (Pool
, Pages
);
472 if (NewBlock
== NULL
) {
473 DEBUG ((EFI_D_ERROR
, "UsbHcAllocateMem: failed to allocate block\n"));
478 // Add the new memory block to the pool, then allocate memory from it
480 UsbHcInsertMemBlockToPool (Head
, NewBlock
);
481 Mem
= UsbHcAllocMemFromBlock (NewBlock
, AllocSize
/ USBHC_MEM_UNIT
);
492 Free the allocated memory back to the memory pool.
494 @param Pool The memory pool of the host controller.
495 @param Mem The memory to free.
496 @param Size The size of the memory to free.
501 IN USBHC_MEM_POOL
*Pool
,
506 USBHC_MEM_BLOCK
*Head
;
507 USBHC_MEM_BLOCK
*Block
;
515 AllocSize
= USBHC_MEM_ROUND (Size
);
516 ToFree
= (UINT8
*) Mem
;
518 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
520 // scan the memory block list for the memory block that
521 // completely contains the memory to free.
523 if ((Block
->BufHost
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->BufHost
+ Block
->BufLen
))) {
525 // compute the start byte and bit in the bit array
527 Byte
= ((ToFree
- Block
->BufHost
) / USBHC_MEM_UNIT
) / 8;
528 Bit
= ((ToFree
- Block
->BufHost
) / USBHC_MEM_UNIT
) % 8;
531 // reset associated bits in bit array
533 for (Count
= 0; Count
< (AllocSize
/ USBHC_MEM_UNIT
); Count
++) {
534 ASSERT (USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
536 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ USB_HC_BIT (Bit
));
537 NEXT_BIT (Byte
, Bit
);
545 // If Block == NULL, it means that the current memory isn't
546 // in the host controller's pool. This is critical because
547 // the caller has passed in a wrong memory point
549 ASSERT (Block
!= NULL
);
552 // Release the current memory block if it is empty and not the head
554 if ((Block
!= Head
) && UsbHcIsMemBlockEmpty (Block
)) {
555 UsbHcUnlinkMemBlock (Head
, Block
);
556 UsbHcFreeMemBlock (Pool
, Block
);