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
13 Allocate a block of memory to be used by the buffer pool.
15 @param Pool The buffer pool to allocate memory for.
16 @param Pages How many pages to allocate.
18 @return The allocated memory block or NULL if failed.
23 IN USBHC_MEM_POOL
*Pool
,
27 USBHC_MEM_BLOCK
*Block
;
28 EFI_PCI_IO_PROTOCOL
*PciIo
;
31 EFI_PHYSICAL_ADDRESS MappedAddr
;
37 Block
= AllocateZeroPool (sizeof (USBHC_MEM_BLOCK
));
43 // each bit in the bit array represents USBHC_MEM_UNIT
44 // bytes of memory in the memory block.
46 ASSERT (USBHC_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
48 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
49 Block
->BitsLen
= Block
->BufLen
/ (USBHC_MEM_UNIT
* 8);
50 Block
->Bits
= AllocateZeroPool (Block
->BitsLen
);
52 if (Block
->Bits
== NULL
) {
53 gBS
->FreePool (Block
);
58 // Allocate the number of Pages of memory, then map it for
59 // bus master read and write.
61 Status
= PciIo
->AllocateBuffer (
70 if (EFI_ERROR (Status
)) {
74 Bytes
= EFI_PAGES_TO_SIZE (Pages
);
77 EfiPciIoOperationBusMasterCommonBuffer
,
84 if (EFI_ERROR (Status
) || (Bytes
!= EFI_PAGES_TO_SIZE (Pages
))) {
89 // Check whether the data structure used by the host controller
90 // should be restricted into the same 4G
92 if (Pool
->Check4G
&& (Pool
->Which4G
!= USB_HC_HIGH_32BIT (MappedAddr
))) {
93 PciIo
->Unmap (PciIo
, Mapping
);
97 Block
->BufHost
= BufHost
;
98 Block
->Buf
= (UINT8
*)((UINTN
)MappedAddr
);
99 Block
->Mapping
= Mapping
;
104 PciIo
->FreeBuffer (PciIo
, Pages
, BufHost
);
107 gBS
->FreePool (Block
->Bits
);
108 gBS
->FreePool (Block
);
113 Free the memory block from the memory pool.
115 @param Pool The memory pool to free the block from.
116 @param Block The memory block to free.
121 IN USBHC_MEM_POOL
*Pool
,
122 IN USBHC_MEM_BLOCK
*Block
125 EFI_PCI_IO_PROTOCOL
*PciIo
;
127 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
132 // Unmap the common buffer then free the structures
134 PciIo
->Unmap (PciIo
, Block
->Mapping
);
135 PciIo
->FreeBuffer (PciIo
, EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
);
137 gBS
->FreePool (Block
->Bits
);
138 gBS
->FreePool (Block
);
142 Alloc some memory from the block.
144 @param Block The memory block to allocate memory from.
145 @param Units Number of memory units to allocate.
147 @return EFI_SUCCESS The needed memory is allocated.
148 @return EFI_NOT_FOUND Can't find the free memory.
152 UsbHcAllocMemFromBlock (
153 IN USBHC_MEM_BLOCK
*Block
,
164 ASSERT ((Block
!= 0) && (Units
!= 0));
170 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
172 // If current bit is zero, the corresponding memory unit is
173 // available, otherwise we need to restart our searching.
174 // Available counts the consective number of zero bit.
176 if (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
179 if (Available
>= Units
) {
183 NEXT_BIT (Byte
, Bit
);
185 NEXT_BIT (Byte
, Bit
);
193 if (Available
< Units
) {
198 // Mark the memory as allocated
203 for (Count
= 0; Count
< Units
; Count
++) {
204 ASSERT (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
206 Block
->Bits
[Byte
] = (UINT8
)(Block
->Bits
[Byte
] | (UINT8
)USB_HC_BIT (Bit
));
207 NEXT_BIT (Byte
, Bit
);
210 return Block
->BufHost
+ (StartByte
* 8 + StartBit
) * USBHC_MEM_UNIT
;
214 Calculate the corresponding pci bus address according to the Mem parameter.
216 @param Pool The memory pool of the host controller.
217 @param Mem The pointer to host memory.
218 @param Size The size of the memory region.
220 @return the pci memory address
223 UsbHcGetPciAddressForHostMem (
224 IN USBHC_MEM_POOL
*Pool
,
229 USBHC_MEM_BLOCK
*Head
;
230 USBHC_MEM_BLOCK
*Block
;
232 EFI_PHYSICAL_ADDRESS PhyAddr
;
236 AllocSize
= USBHC_MEM_ROUND (Size
);
242 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
244 // scan the memory block list for the memory block that
245 // completely contains the allocated memory.
247 if ((Block
->BufHost
<= (UINT8
*)Mem
) && (((UINT8
*)Mem
+ AllocSize
) <= (Block
->BufHost
+ Block
->BufLen
))) {
252 ASSERT ((Block
!= NULL
));
254 // calculate the pci memory address for host memory address.
256 Offset
= (UINT8
*)Mem
- Block
->BufHost
;
257 PhyAddr
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(Block
->Buf
+ Offset
);
262 Insert the memory block to the pool's list of the blocks.
264 @param Head The head of the memory pool's block list.
265 @param Block The memory block to insert.
269 UsbHcInsertMemBlockToPool (
270 IN USBHC_MEM_BLOCK
*Head
,
271 IN USBHC_MEM_BLOCK
*Block
274 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
275 Block
->Next
= Head
->Next
;
280 Is the memory block empty?
282 @param Block The memory block to check.
284 @return TRUE The memory block is empty.
285 @return FALSE The memory block isn't empty.
289 UsbHcIsMemBlockEmpty (
290 IN USBHC_MEM_BLOCK
*Block
295 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
296 if (Block
->Bits
[Index
] != 0) {
305 Unlink the memory block from the pool's list.
307 @param Head The block list head of the memory's pool.
308 @param BlockToUnlink The memory block to unlink.
312 UsbHcUnlinkMemBlock (
313 IN USBHC_MEM_BLOCK
*Head
,
314 IN USBHC_MEM_BLOCK
*BlockToUnlink
317 USBHC_MEM_BLOCK
*Block
;
319 ASSERT ((Head
!= NULL
) && (BlockToUnlink
!= NULL
));
321 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
322 if (Block
->Next
== BlockToUnlink
) {
323 Block
->Next
= BlockToUnlink
->Next
;
324 BlockToUnlink
->Next
= NULL
;
331 Initialize the memory management pool for the host controller.
333 @param PciIo The PciIo that can be used to access the host controller.
334 @param Check4G Whether the host controller requires allocated memory
335 from one 4G address space.
336 @param Which4G The 4G memory area each memory allocated should be from.
338 @return EFI_SUCCESS The memory pool is initialized.
339 @return EFI_OUT_OF_RESOURCE Fail to init the memory pool.
344 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
349 USBHC_MEM_POOL
*Pool
;
351 Pool
= AllocatePool (sizeof (USBHC_MEM_POOL
));
358 Pool
->Check4G
= Check4G
;
359 Pool
->Which4G
= Which4G
;
360 Pool
->Head
= UsbHcAllocMemBlock (Pool
, USBHC_MEM_DEFAULT_PAGES
);
362 if (Pool
->Head
== NULL
) {
363 gBS
->FreePool (Pool
);
371 Release the memory management pool.
373 @param Pool The USB memory pool to free.
375 @return EFI_SUCCESS The memory pool is freed.
376 @return EFI_DEVICE_ERROR Failed to free the memory pool.
381 IN USBHC_MEM_POOL
*Pool
384 USBHC_MEM_BLOCK
*Block
;
386 ASSERT (Pool
->Head
!= NULL
);
389 // Unlink all the memory blocks from the pool, then free them.
390 // UsbHcUnlinkMemBlock can't be used to unlink and free the
393 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
394 UsbHcUnlinkMemBlock (Pool
->Head
, Block
);
395 UsbHcFreeMemBlock (Pool
, Block
);
398 UsbHcFreeMemBlock (Pool
, Pool
->Head
);
399 gBS
->FreePool (Pool
);
404 Allocate some memory from the host controller's memory pool
405 which can be used to communicate with host controller.
407 @param Pool The host controller's memory pool.
408 @param Size Size of the memory to allocate.
410 @return The allocated memory or NULL.
415 IN USBHC_MEM_POOL
*Pool
,
419 USBHC_MEM_BLOCK
*Head
;
420 USBHC_MEM_BLOCK
*Block
;
421 USBHC_MEM_BLOCK
*NewBlock
;
427 AllocSize
= USBHC_MEM_ROUND (Size
);
429 ASSERT (Head
!= NULL
);
432 // First check whether current memory blocks can satisfy the allocation.
434 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
435 Mem
= UsbHcAllocMemFromBlock (Block
, AllocSize
/ USBHC_MEM_UNIT
);
448 // Create a new memory block if there is not enough memory
449 // in the pool. If the allocation size is larger than the
450 // default page number, just allocate a large enough memory
451 // block. Otherwise allocate default pages.
453 if (AllocSize
> EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES
)) {
454 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
456 Pages
= USBHC_MEM_DEFAULT_PAGES
;
459 NewBlock
= UsbHcAllocMemBlock (Pool
, Pages
);
461 if (NewBlock
== NULL
) {
462 DEBUG ((DEBUG_INFO
, "UsbHcAllocateMem: failed to allocate block\n"));
467 // Add the new memory block to the pool, then allocate memory from it
469 UsbHcInsertMemBlockToPool (Head
, NewBlock
);
470 Mem
= UsbHcAllocMemFromBlock (NewBlock
, AllocSize
/ USBHC_MEM_UNIT
);
480 Free the allocated memory back to the memory pool.
482 @param Pool The memory pool of the host controller.
483 @param Mem The memory to free.
484 @param Size The size of the memory to free.
489 IN USBHC_MEM_POOL
*Pool
,
494 USBHC_MEM_BLOCK
*Head
;
495 USBHC_MEM_BLOCK
*Block
;
503 AllocSize
= USBHC_MEM_ROUND (Size
);
504 ToFree
= (UINT8
*)Mem
;
506 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
508 // scan the memory block list for the memory block that
509 // completely contains the memory to free.
511 if ((Block
->BufHost
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->BufHost
+ Block
->BufLen
))) {
513 // compute the start byte and bit in the bit array
515 Byte
= ((ToFree
- Block
->BufHost
) / USBHC_MEM_UNIT
) / 8;
516 Bit
= ((ToFree
- Block
->BufHost
) / USBHC_MEM_UNIT
) % 8;
519 // reset associated bits in bit array
521 for (Count
= 0; Count
< (AllocSize
/ USBHC_MEM_UNIT
); Count
++) {
522 ASSERT (USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
524 Block
->Bits
[Byte
] = (UINT8
)(Block
->Bits
[Byte
] ^ USB_HC_BIT (Bit
));
525 NEXT_BIT (Byte
, Bit
);
533 // If Block == NULL, it means that the current memory isn't
534 // in the host controller's pool. This is critical because
535 // the caller has passed in a wrong memory point
537 ASSERT (Block
!= NULL
);
540 // Release the current memory block if it is empty and not the head
542 if ((Block
!= Head
) && UsbHcIsMemBlockEmpty (Block
)) {
543 UsbHcUnlinkMemBlock (Head
, Block
);
544 UsbHcFreeMemBlock (Pool
, Block
);