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.
27 UINTN mUsbHcDebugLevel
= DEBUG_INFO
;
31 Allocate a block of memory to be used by the buffer pool
33 @param Pool The buffer pool to allocate memory for
34 @param Pages How many pages to allocate
36 @return The allocated memory block or NULL if failed
42 IN USBHC_MEM_POOL
*Pool
,
46 USBHC_MEM_BLOCK
*Block
;
47 EFI_PCI_IO_PROTOCOL
*PciIo
;
50 EFI_PHYSICAL_ADDRESS MappedAddr
;
56 Block
= AllocateZeroPool (sizeof (USBHC_MEM_BLOCK
));
62 // each bit in the bit array represents USBHC_MEM_UNIT
63 // bytes of memory in the memory block.
65 ASSERT (USBHC_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
67 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
68 Block
->BitsLen
= Block
->BufLen
/ (USBHC_MEM_UNIT
* 8);
69 Block
->Bits
= AllocateZeroPool (Block
->BitsLen
);
71 if (Block
->Bits
== NULL
) {
72 gBS
->FreePool (Block
);
77 // Allocate the number of Pages of memory, then map it for
78 // bus master read and write.
80 Status
= PciIo
->AllocateBuffer (
89 if (EFI_ERROR (Status
)) {
93 Bytes
= EFI_PAGES_TO_SIZE (Pages
);
96 EfiPciIoOperationBusMasterCommonBuffer
,
103 if (EFI_ERROR (Status
) || (Bytes
!= EFI_PAGES_TO_SIZE (Pages
))) {
108 // Check whether the data structure used by the host controller
109 // should be restricted into the same 4G
111 if (Pool
->Check4G
&& (Pool
->Which4G
!= USB_HC_HIGH_32BIT (MappedAddr
))) {
112 PciIo
->Unmap (PciIo
, Mapping
);
116 Block
->BufHost
= BufHost
;
117 Block
->Buf
= (UINT8
*) ((UINTN
) MappedAddr
);
118 Block
->Mapping
= Mapping
;
120 DEBUG ((mUsbHcDebugLevel
, "UsbHcAllocMemBlock: block %x created with buffer %x\n",
126 PciIo
->FreeBuffer (PciIo
, Pages
, BufHost
);
129 gBS
->FreePool (Block
->Bits
);
130 gBS
->FreePool (Block
);
136 Free the memory block from the memory pool
138 @param Pool The memory pool to free the block from
139 @param Block The memory block to free
147 IN USBHC_MEM_POOL
*Pool
,
148 IN USBHC_MEM_BLOCK
*Block
151 EFI_PCI_IO_PROTOCOL
*PciIo
;
153 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
158 // Unmap the common buffer then free the structures
160 PciIo
->Unmap (PciIo
, Block
->Mapping
);
161 PciIo
->FreeBuffer (PciIo
, EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
);
163 gBS
->FreePool (Block
->Bits
);
164 gBS
->FreePool (Block
);
169 Alloc some memory from the block
171 @param Block The memory block to allocate memory from
172 @param Mem The variable to store the memory allocated
173 @param Units Number of memory units to allocate
175 @return EFI_SUCCESS : The needed memory is allocated
176 @return EFI_NOT_FOUND : Can't find the free memory
181 UsbHcAllocMemFromBlock (
182 IN USBHC_MEM_BLOCK
*Block
,
193 ASSERT ((Block
!= 0) && (Units
!= 0));
199 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
201 // If current bit is zero, the corresponding memory unit is
202 // available, otherwise we need to restart our searching.
203 // Available counts the consective number of zero bit.
205 if (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
208 if (Available
>= Units
) {
212 NEXT_BIT (Byte
, Bit
);
215 NEXT_BIT (Byte
, Bit
);
223 if (Available
< Units
) {
228 // Mark the memory as allocated
233 for (Count
= 0; Count
< Units
; Count
++) {
234 ASSERT (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
236 Block
->Bits
[Byte
] |= USB_HC_BIT (Bit
);
237 NEXT_BIT (Byte
, Bit
);
240 return Block
->Buf
+ (StartByte
* 8 + StartBit
) * USBHC_MEM_UNIT
;
245 Insert the memory block to the pool's list of the blocks
247 @param Head The head of the memory pool's block list
248 @param Block The memory block to insert
255 UsbHcInsertMemBlockToPool (
256 IN USBHC_MEM_BLOCK
*Head
,
257 IN USBHC_MEM_BLOCK
*Block
260 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
261 Block
->Next
= Head
->Next
;
267 Is the memory block empty?
269 @param Block The memory block to check
271 @return TRUE : The memory block is empty
272 @return FALSE : The memory block isn't empty
277 UsbHcIsMemBlockEmpty (
278 IN USBHC_MEM_BLOCK
*Block
283 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
284 if (Block
->Bits
[Index
] != 0) {
294 Unlink the memory block from the pool's list
296 @param Head The block list head of the memory's pool
297 @param BlockToUnlink The memory block to unlink.
304 UsbHcUnlinkMemBlock (
305 IN USBHC_MEM_BLOCK
*Head
,
306 IN USBHC_MEM_BLOCK
*BlockToUnlink
309 USBHC_MEM_BLOCK
*Block
;
311 ASSERT ((Head
!= NULL
) && (BlockToUnlink
!= NULL
));
313 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
314 if (Block
->Next
== BlockToUnlink
) {
315 Block
->Next
= BlockToUnlink
->Next
;
316 BlockToUnlink
->Next
= NULL
;
324 Initialize the memory management pool for the host controller
326 @param Pool The USB memory pool to initialize
327 @param PciIo The PciIo that can be used to access the host controller
328 @param Check4G Whether the host controller requires allocated memory
329 from one 4G address space.
330 @param Which4G The 4G memory area each memory allocated should be from
332 @return EFI_SUCCESS : The memory pool is initialized
333 @return EFI_OUT_OF_RESOURCE : Fail to init the memory pool
338 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
343 USBHC_MEM_POOL
*Pool
;
345 Pool
= AllocatePool (sizeof (USBHC_MEM_POOL
));
352 Pool
->Check4G
= Check4G
;
353 Pool
->Which4G
= Which4G
;
354 Pool
->Head
= UsbHcAllocMemBlock (Pool
, USBHC_MEM_DEFAULT_PAGES
);
356 if (Pool
->Head
== NULL
) {
357 gBS
->FreePool (Pool
);
366 Release the memory management pool
368 @param Pool The USB memory pool to free
370 @return EFI_SUCCESS : The memory pool is freed
371 @return EFI_DEVICE_ERROR : Failed to free the memory pool
376 IN USBHC_MEM_POOL
*Pool
379 USBHC_MEM_BLOCK
*Block
;
381 ASSERT (Pool
->Head
!= NULL
);
384 // Unlink all the memory blocks from the pool, then free them.
385 // UsbHcUnlinkMemBlock can't be used to unlink and free the
388 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
389 UsbHcUnlinkMemBlock (Pool
->Head
, Block
);
390 UsbHcFreeMemBlock (Pool
, Block
);
393 UsbHcFreeMemBlock (Pool
, Pool
->Head
);
394 gBS
->FreePool (Pool
);
400 Allocate some memory from the host controller's memory pool
401 which can be used to communicate with host controller.
403 @param Pool The host controller's memory pool
404 @param Size Size of the memory to allocate
406 @return The allocated memory or NULL
411 IN USBHC_MEM_POOL
*Pool
,
415 USBHC_MEM_BLOCK
*Head
;
416 USBHC_MEM_BLOCK
*Block
;
417 USBHC_MEM_BLOCK
*NewBlock
;
423 AllocSize
= USBHC_MEM_ROUND (Size
);
425 ASSERT (Head
!= NULL
);
428 // First check whether current memory blocks can satisfy the allocation.
430 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
431 Mem
= UsbHcAllocMemFromBlock (Block
, AllocSize
/ USBHC_MEM_UNIT
);
444 // Create a new memory block if there is not enough memory
445 // in the pool. If the allocation size is larger than the
446 // default page number, just allocate a large enough memory
447 // block. Otherwise allocate default pages.
449 if (AllocSize
> EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES
)) {
450 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
452 Pages
= USBHC_MEM_DEFAULT_PAGES
;
455 NewBlock
= UsbHcAllocMemBlock (Pool
, Pages
);
457 if (NewBlock
== NULL
) {
458 DEBUG ((mUsbHcDebugLevel
, "UsbHcAllocateMem: failed to allocate block\n"));
463 // Add the new memory block to the pool, then allocate memory from it
465 UsbHcInsertMemBlockToPool (Head
, NewBlock
);
466 Mem
= UsbHcAllocMemFromBlock (NewBlock
, AllocSize
/ USBHC_MEM_UNIT
);
477 Free the allocated memory back to the memory pool
479 @param Pool The memory pool of the host controller
480 @param Mem The memory to free
481 @param Size The size of the memory to free
488 IN USBHC_MEM_POOL
*Pool
,
493 USBHC_MEM_BLOCK
*Head
;
494 USBHC_MEM_BLOCK
*Block
;
502 AllocSize
= USBHC_MEM_ROUND (Size
);
503 ToFree
= (UINT8
*) Mem
;
505 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
507 // scan the memory block list for the memory block that
508 // completely contains the memory to free.
510 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
512 // compute the start byte and bit in the bit array
514 Byte
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) / 8;
515 Bit
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) % 8;
518 // reset associated bits in bit arry
520 for (Count
= 0; Count
< (AllocSize
/ USBHC_MEM_UNIT
); Count
++) {
521 ASSERT (USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
523 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ USB_HC_BIT (Bit
));
524 NEXT_BIT (Byte
, Bit
);
532 // If Block == NULL, it means that the current memory isn't
533 // in the host controller's pool. This is critical because
534 // the caller has passed in a wrong memory point
536 ASSERT (Block
!= NULL
);
539 // Release the current memory block if it is empty and not the head
541 if ((Block
!= Head
) && UsbHcIsMemBlockEmpty (Block
)) {
542 DEBUG ((mUsbHcDebugLevel
, "UsbHcFreeMem: block %x is empty, recycle\n", Block
));
544 UsbHcUnlinkMemBlock (Head
, Block
);
545 UsbHcFreeMemBlock (Pool
, Block
);