3 Copyright (c) 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
26 UINTN mUsbHcDebugLevel
= DEBUG_INFO
;
30 Allocate a block of memory to be used by the buffer pool
32 @param Pool The buffer pool to allocate memory for
33 @param Pages How many pages to allocate
35 @return The allocated memory block or NULL if failed
41 IN USBHC_MEM_POOL
*Pool
,
45 USBHC_MEM_BLOCK
*Block
;
46 EFI_PCI_IO_PROTOCOL
*PciIo
;
49 EFI_PHYSICAL_ADDRESS MappedAddr
;
55 Block
= AllocateZeroPool (sizeof (USBHC_MEM_BLOCK
));
61 // each bit in the bit array represents USBHC_MEM_UNIT
62 // bytes of memory in the memory block.
64 ASSERT (USBHC_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
66 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
67 Block
->BitsLen
= Block
->BufLen
/ (USBHC_MEM_UNIT
* 8);
68 Block
->Bits
= AllocateZeroPool (Block
->BitsLen
);
70 if (Block
->Bits
== NULL
) {
71 gBS
->FreePool (Block
);
76 // Allocate the number of Pages of memory, then map it for
77 // bus master read and write.
79 Status
= PciIo
->AllocateBuffer (
88 if (EFI_ERROR (Status
)) {
92 Bytes
= EFI_PAGES_TO_SIZE (Pages
);
95 EfiPciIoOperationBusMasterCommonBuffer
,
102 if (EFI_ERROR (Status
) || (Bytes
!= EFI_PAGES_TO_SIZE (Pages
))) {
107 // Check whether the data structure used by the host controller
108 // should be restricted into the same 4G
110 if (Pool
->Check4G
&& (Pool
->Which4G
!= USB_HC_HIGH_32BIT (MappedAddr
))) {
111 PciIo
->Unmap (PciIo
, Mapping
);
115 Block
->BufHost
= BufHost
;
116 Block
->Buf
= (UINT8
*) ((UINTN
) MappedAddr
);
117 Block
->Mapping
= Mapping
;
119 DEBUG ((mUsbHcDebugLevel
, "UsbHcAllocMemBlock: block %x created with buffer %x\n",
125 PciIo
->FreeBuffer (PciIo
, Pages
, BufHost
);
128 gBS
->FreePool (Block
->Bits
);
129 gBS
->FreePool (Block
);
135 Free the memory block from the memory pool
137 @param Pool The memory pool to free the block from
138 @param Block The memory block to free
146 IN USBHC_MEM_POOL
*Pool
,
147 IN USBHC_MEM_BLOCK
*Block
150 EFI_PCI_IO_PROTOCOL
*PciIo
;
152 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
157 // Unmap the common buffer then free the structures
159 PciIo
->Unmap (PciIo
, Block
->Mapping
);
160 PciIo
->FreeBuffer (PciIo
, EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
);
162 gBS
->FreePool (Block
->Bits
);
163 gBS
->FreePool (Block
);
168 Alloc some memory from the block
170 @param Block The memory block to allocate memory from
171 @param Mem The variable to store the memory allocated
172 @param Units Number of memory units to allocate
174 @return EFI_SUCCESS : The needed memory is allocated
175 @return EFI_NOT_FOUND : Can't find the free memory
180 UsbHcAllocMemFromBlock (
181 IN USBHC_MEM_BLOCK
*Block
,
192 ASSERT ((Block
!= 0) && (Units
!= 0));
198 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
200 // If current bit is zero, the corresponding memory unit is
201 // available, otherwise we need to restart our searching.
202 // Available counts the consective number of zero bit.
204 if (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
207 if (Available
>= Units
) {
211 NEXT_BIT (Byte
, Bit
);
214 NEXT_BIT (Byte
, Bit
);
222 if (Available
< Units
) {
227 // Mark the memory as allocated
232 for (Count
= 0; Count
< Units
; Count
++) {
233 ASSERT (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
235 Block
->Bits
[Byte
] |= USB_HC_BIT (Bit
);
236 NEXT_BIT (Byte
, Bit
);
239 return Block
->Buf
+ (StartByte
* 8 + StartBit
) * USBHC_MEM_UNIT
;
244 Insert the memory block to the pool's list of the blocks
246 @param Head The head of the memory pool's block list
247 @param Block The memory block to insert
254 UsbHcInsertMemBlockToPool (
255 IN USBHC_MEM_BLOCK
*Head
,
256 IN USBHC_MEM_BLOCK
*Block
259 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
260 Block
->Next
= Head
->Next
;
266 Is the memory block empty?
268 @param Block The memory block to check
270 @return TRUE : The memory block is empty
271 @return FALSE : The memory block isn't empty
276 UsbHcIsMemBlockEmpty (
277 IN USBHC_MEM_BLOCK
*Block
282 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
283 if (Block
->Bits
[Index
] != 0) {
293 Unlink the memory block from the pool's list
295 @param Head The block list head of the memory's pool
296 @param BlockToUnlink The memory block to unlink.
303 UsbHcUnlinkMemBlock (
304 IN USBHC_MEM_BLOCK
*Head
,
305 IN USBHC_MEM_BLOCK
*BlockToUnlink
308 USBHC_MEM_BLOCK
*Block
;
310 ASSERT ((Head
!= NULL
) && (BlockToUnlink
!= NULL
));
312 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
313 if (Block
->Next
== BlockToUnlink
) {
314 Block
->Next
= BlockToUnlink
->Next
;
315 BlockToUnlink
->Next
= NULL
;
323 Initialize the memory management pool for the host controller
325 @param Pool The USB memory pool to initialize
326 @param PciIo The PciIo that can be used to access the host controller
327 @param Check4G Whether the host controller requires allocated memory
328 from one 4G address space.
329 @param Which4G The 4G memory area each memory allocated should be from
331 @return EFI_SUCCESS : The memory pool is initialized
332 @return EFI_OUT_OF_RESOURCE : Fail to init the memory pool
337 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
342 USBHC_MEM_POOL
*Pool
;
344 Pool
= AllocatePool (sizeof (USBHC_MEM_POOL
));
351 Pool
->Check4G
= Check4G
;
352 Pool
->Which4G
= Which4G
;
353 Pool
->Head
= UsbHcAllocMemBlock (Pool
, USBHC_MEM_DEFAULT_PAGES
);
355 if (Pool
->Head
== NULL
) {
356 gBS
->FreePool (Pool
);
365 Release the memory management pool
367 @param Pool The USB memory pool to free
369 @return EFI_SUCCESS : The memory pool is freed
370 @return EFI_DEVICE_ERROR : Failed to free the memory pool
375 IN USBHC_MEM_POOL
*Pool
378 USBHC_MEM_BLOCK
*Block
;
380 ASSERT (Pool
->Head
!= NULL
);
383 // Unlink all the memory blocks from the pool, then free them.
384 // UsbHcUnlinkMemBlock can't be used to unlink and free the
387 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
388 UsbHcUnlinkMemBlock (Pool
->Head
, Block
);
389 UsbHcFreeMemBlock (Pool
, Block
);
392 UsbHcFreeMemBlock (Pool
, Pool
->Head
);
393 gBS
->FreePool (Pool
);
399 Allocate some memory from the host controller's memory pool
400 which can be used to communicate with host controller.
402 @param Pool The host controller's memory pool
403 @param Size Size of the memory to allocate
405 @return The allocated memory or NULL
410 IN USBHC_MEM_POOL
*Pool
,
414 USBHC_MEM_BLOCK
*Head
;
415 USBHC_MEM_BLOCK
*Block
;
416 USBHC_MEM_BLOCK
*NewBlock
;
422 AllocSize
= USBHC_MEM_ROUND (Size
);
424 ASSERT (Head
!= NULL
);
427 // First check whether current memory blocks can satisfy the allocation.
429 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
430 Mem
= UsbHcAllocMemFromBlock (Block
, AllocSize
/ USBHC_MEM_UNIT
);
443 // Create a new memory block if there is not enough memory
444 // in the pool. If the allocation size is larger than the
445 // default page number, just allocate a large enough memory
446 // block. Otherwise allocate default pages.
448 if (AllocSize
> EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES
)) {
449 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
451 Pages
= USBHC_MEM_DEFAULT_PAGES
;
454 NewBlock
= UsbHcAllocMemBlock (Pool
, Pages
);
456 if (NewBlock
== NULL
) {
457 DEBUG ((mUsbHcDebugLevel
, "UsbHcAllocateMem: failed to allocate block\n"));
462 // Add the new memory block to the pool, then allocate memory from it
464 UsbHcInsertMemBlockToPool (Head
, NewBlock
);
465 Mem
= UsbHcAllocMemFromBlock (NewBlock
, AllocSize
/ USBHC_MEM_UNIT
);
476 Free the allocated memory back to the memory pool
478 @param Pool The memory pool of the host controller
479 @param Mem The memory to free
480 @param Size The size of the memory to free
487 IN USBHC_MEM_POOL
*Pool
,
492 USBHC_MEM_BLOCK
*Head
;
493 USBHC_MEM_BLOCK
*Block
;
501 AllocSize
= USBHC_MEM_ROUND (Size
);
502 ToFree
= (UINT8
*) Mem
;
504 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
506 // scan the memory block list for the memory block that
507 // completely contains the memory to free.
509 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
511 // compute the start byte and bit in the bit array
513 Byte
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) / 8;
514 Bit
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) % 8;
517 // reset associated bits in bit arry
519 for (Count
= 0; Count
< (AllocSize
/ USBHC_MEM_UNIT
); Count
++) {
520 ASSERT (USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
522 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ USB_HC_BIT (Bit
));
523 NEXT_BIT (Byte
, Bit
);
531 // If Block == NULL, it means that the current memory isn't
532 // in the host controller's pool. This is critical because
533 // the caller has passed in a wrong memory point
535 ASSERT (Block
!= NULL
);
538 // Release the current memory block if it is empty and not the head
540 if ((Block
!= Head
) && UsbHcIsMemBlockEmpty (Block
)) {
541 DEBUG ((mUsbHcDebugLevel
, "UsbHcFreeMem: block %x is empty, recycle\n", Block
));
543 UsbHcUnlinkMemBlock (Head
, Block
);
544 UsbHcFreeMemBlock (Pool
, Block
);