3 The routine procedure for uhci memory allocate/free.
5 Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
14 Allocate a block of memory to be used by the buffer pool.
16 @param Pool The buffer pool to allocate memory for.
17 @param Pages How many pages to allocate.
19 @return The allocated memory block or NULL if failed.
24 IN USBHC_MEM_POOL
*Pool
,
28 USBHC_MEM_BLOCK
*Block
;
29 EFI_PCI_IO_PROTOCOL
*PciIo
;
32 EFI_PHYSICAL_ADDRESS MappedAddr
;
38 Block
= AllocateZeroPool (sizeof (USBHC_MEM_BLOCK
));
44 // each bit in the bit array represents USBHC_MEM_UNIT
45 // bytes of memory in the memory block.
47 ASSERT (USBHC_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
49 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
50 Block
->BitsLen
= Block
->BufLen
/ (USBHC_MEM_UNIT
* 8);
51 Block
->Bits
= AllocateZeroPool (Block
->BitsLen
);
53 if (Block
->Bits
== NULL
) {
54 gBS
->FreePool (Block
);
59 // Allocate the number of Pages of memory, then map it for
60 // bus master read and write.
62 Status
= PciIo
->AllocateBuffer (
71 if (EFI_ERROR (Status
)) {
75 Bytes
= EFI_PAGES_TO_SIZE (Pages
);
78 EfiPciIoOperationBusMasterCommonBuffer
,
85 if (EFI_ERROR (Status
) || (Bytes
!= EFI_PAGES_TO_SIZE (Pages
))) {
90 // Check whether the data structure used by the host controller
91 // should be restricted into the same 4G
93 if (Pool
->Check4G
&& (Pool
->Which4G
!= USB_HC_HIGH_32BIT (MappedAddr
))) {
94 PciIo
->Unmap (PciIo
, Mapping
);
98 Block
->BufHost
= BufHost
;
99 Block
->Buf
= (UINT8
*) ((UINTN
) MappedAddr
);
100 Block
->Mapping
= Mapping
;
105 PciIo
->FreeBuffer (PciIo
, Pages
, BufHost
);
108 gBS
->FreePool (Block
->Bits
);
109 gBS
->FreePool (Block
);
115 Free the memory block from the memory pool.
117 @param Pool The memory pool to free the block from.
118 @param Block The memory block to free.
123 IN USBHC_MEM_POOL
*Pool
,
124 IN USBHC_MEM_BLOCK
*Block
127 EFI_PCI_IO_PROTOCOL
*PciIo
;
129 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
134 // Unmap the common buffer then free the structures
136 PciIo
->Unmap (PciIo
, Block
->Mapping
);
137 PciIo
->FreeBuffer (PciIo
, EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
);
139 gBS
->FreePool (Block
->Bits
);
140 gBS
->FreePool (Block
);
145 Alloc some memory from the block.
147 @param Block The memory block to allocate memory from.
148 @param Units Number of memory units to allocate.
150 @return EFI_SUCCESS The needed memory is allocated.
151 @return EFI_NOT_FOUND Can't find the free memory.
155 UsbHcAllocMemFromBlock (
156 IN USBHC_MEM_BLOCK
*Block
,
167 ASSERT ((Block
!= 0) && (Units
!= 0));
173 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
175 // If current bit is zero, the corresponding memory unit is
176 // available, otherwise we need to restart our searching.
177 // Available counts the consective number of zero bit.
179 if (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
182 if (Available
>= Units
) {
186 NEXT_BIT (Byte
, Bit
);
189 NEXT_BIT (Byte
, Bit
);
197 if (Available
< Units
) {
202 // Mark the memory as allocated
207 for (Count
= 0; Count
< Units
; Count
++) {
208 ASSERT (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
210 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] | (UINT8
) USB_HC_BIT (Bit
));
211 NEXT_BIT (Byte
, Bit
);
214 return Block
->Buf
+ (StartByte
* 8 + StartBit
) * USBHC_MEM_UNIT
;
218 Calculate the corresponding pci bus address according to the Mem parameter.
220 @param Pool The memory pool of the host controller.
221 @param Mem The pointer to host memory.
222 @param Size The size of the memory region.
224 @return the pci memory address
227 UsbHcGetPciAddressForHostMem (
228 IN USBHC_MEM_POOL
*Pool
,
233 USBHC_MEM_BLOCK
*Head
;
234 USBHC_MEM_BLOCK
*Block
;
236 EFI_PHYSICAL_ADDRESS PhyAddr
;
240 AllocSize
= USBHC_MEM_ROUND (Size
);
246 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
248 // scan the memory block list for the memory block that
249 // completely contains the allocated memory.
251 if ((Block
->BufHost
<= (UINT8
*) Mem
) && (((UINT8
*) Mem
+ AllocSize
) <= (Block
->BufHost
+ Block
->BufLen
))) {
256 ASSERT ((Block
!= NULL
));
258 // calculate the pci memory address for host memory address.
260 Offset
= (UINT8
*)Mem
- Block
->BufHost
;
261 PhyAddr
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) (Block
->Buf
+ Offset
);
266 Insert the memory block to the pool's list of the blocks.
268 @param Head The head of the memory pool's block list.
269 @param Block The memory block to insert.
273 UsbHcInsertMemBlockToPool (
274 IN USBHC_MEM_BLOCK
*Head
,
275 IN USBHC_MEM_BLOCK
*Block
278 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
279 Block
->Next
= Head
->Next
;
285 Is the memory block empty?
287 @param Block The memory block to check.
289 @return TRUE The memory block is empty.
290 @return FALSE The memory block isn't empty.
294 UsbHcIsMemBlockEmpty (
295 IN USBHC_MEM_BLOCK
*Block
300 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
301 if (Block
->Bits
[Index
] != 0) {
311 Unlink the memory block from the pool's list.
313 @param Head The block list head of the memory's pool.
314 @param BlockToUnlink The memory block to unlink.
318 UsbHcUnlinkMemBlock (
319 IN USBHC_MEM_BLOCK
*Head
,
320 IN USBHC_MEM_BLOCK
*BlockToUnlink
323 USBHC_MEM_BLOCK
*Block
;
325 ASSERT ((Head
!= NULL
) && (BlockToUnlink
!= NULL
));
327 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
328 if (Block
->Next
== BlockToUnlink
) {
329 Block
->Next
= BlockToUnlink
->Next
;
330 BlockToUnlink
->Next
= NULL
;
338 Initialize the memory management pool for the host controller.
340 @param PciIo The PciIo that can be used to access the host controller.
341 @param Check4G Whether the host controller requires allocated memory
342 from one 4G address space.
343 @param Which4G The 4G memory area each memory allocated should be from.
345 @return EFI_SUCCESS The memory pool is initialized.
346 @return EFI_OUT_OF_RESOURCE Fail to init the memory pool.
351 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
356 USBHC_MEM_POOL
*Pool
;
358 Pool
= AllocatePool (sizeof (USBHC_MEM_POOL
));
365 Pool
->Check4G
= Check4G
;
366 Pool
->Which4G
= Which4G
;
367 Pool
->Head
= UsbHcAllocMemBlock (Pool
, USBHC_MEM_DEFAULT_PAGES
);
369 if (Pool
->Head
== NULL
) {
370 gBS
->FreePool (Pool
);
379 Release the memory management pool.
381 @param Pool The USB memory pool to free.
383 @return EFI_SUCCESS The memory pool is freed.
384 @return EFI_DEVICE_ERROR Failed to free the memory pool.
389 IN USBHC_MEM_POOL
*Pool
392 USBHC_MEM_BLOCK
*Block
;
394 ASSERT (Pool
->Head
!= NULL
);
397 // Unlink all the memory blocks from the pool, then free them.
398 // UsbHcUnlinkMemBlock can't be used to unlink and free the
401 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
402 UsbHcUnlinkMemBlock (Pool
->Head
, Block
);
403 UsbHcFreeMemBlock (Pool
, Block
);
406 UsbHcFreeMemBlock (Pool
, Pool
->Head
);
407 gBS
->FreePool (Pool
);
413 Allocate some memory from the host controller's memory pool
414 which can be used to communicate with host controller.
416 @param Pool The host controller's memory pool.
417 @param Size Size of the memory to allocate.
419 @return The allocated memory or NULL.
424 IN USBHC_MEM_POOL
*Pool
,
428 USBHC_MEM_BLOCK
*Head
;
429 USBHC_MEM_BLOCK
*Block
;
430 USBHC_MEM_BLOCK
*NewBlock
;
436 AllocSize
= USBHC_MEM_ROUND (Size
);
438 ASSERT (Head
!= NULL
);
441 // First check whether current memory blocks can satisfy the allocation.
443 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
444 Mem
= UsbHcAllocMemFromBlock (Block
, AllocSize
/ USBHC_MEM_UNIT
);
457 // Create a new memory block if there is not enough memory
458 // in the pool. If the allocation size is larger than the
459 // default page number, just allocate a large enough memory
460 // block. Otherwise allocate default pages.
462 if (AllocSize
> EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES
)) {
463 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
465 Pages
= USBHC_MEM_DEFAULT_PAGES
;
468 NewBlock
= UsbHcAllocMemBlock (Pool
, Pages
);
470 if (NewBlock
== NULL
) {
471 DEBUG ((DEBUG_INFO
, "UsbHcAllocateMem: failed to allocate block\n"));
476 // Add the new memory block to the pool, then allocate memory from it
478 UsbHcInsertMemBlockToPool (Head
, NewBlock
);
479 Mem
= UsbHcAllocMemFromBlock (NewBlock
, AllocSize
/ USBHC_MEM_UNIT
);
490 Free the allocated memory back to the memory pool.
492 @param Pool The memory pool of the host controller.
493 @param Mem The memory to free.
494 @param Size The size of the memory to free.
499 IN USBHC_MEM_POOL
*Pool
,
504 USBHC_MEM_BLOCK
*Head
;
505 USBHC_MEM_BLOCK
*Block
;
513 AllocSize
= USBHC_MEM_ROUND (Size
);
514 ToFree
= (UINT8
*) Mem
;
516 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
518 // scan the memory block list for the memory block that
519 // completely contains the memory to free.
521 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
523 // compute the start byte and bit in the bit array
525 Byte
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) / 8;
526 Bit
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) % 8;
529 // reset associated bits in bit array
531 for (Count
= 0; Count
< (AllocSize
/ USBHC_MEM_UNIT
); Count
++) {
532 ASSERT (USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
534 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ USB_HC_BIT (Bit
));
535 NEXT_BIT (Byte
, Bit
);
543 // If Block == NULL, it means that the current memory isn't
544 // in the host controller's pool. This is critical because
545 // the caller has passed in a wrong memory point
547 ASSERT (Block
!= NULL
);
550 // Release the current memory block if it is empty and not the head
552 if ((Block
!= Head
) && UsbHcIsMemBlockEmpty (Block
)) {
553 UsbHcUnlinkMemBlock (Head
, Block
);
554 UsbHcFreeMemBlock (Pool
, Block
);