3 Routine procedures for memory allocate/free.
5 Copyright (c) 2007 - 2009, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 Allocate a block of memory to be used by the buffer pool.
23 @param Pool The buffer pool to allocate memory for.
24 @param Pages How many pages to allocate.
26 @return The allocated memory block or NULL if failed.
31 IN USBHC_MEM_POOL
*Pool
,
35 USBHC_MEM_BLOCK
*Block
;
36 EFI_PCI_IO_PROTOCOL
*PciIo
;
39 EFI_PHYSICAL_ADDRESS MappedAddr
;
45 Block
= AllocateZeroPool (sizeof (USBHC_MEM_BLOCK
));
51 // each bit in the bit array represents USBHC_MEM_UNIT
52 // bytes of memory in the memory block.
54 ASSERT (USBHC_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
56 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
57 Block
->BitsLen
= Block
->BufLen
/ (USBHC_MEM_UNIT
* 8);
58 Block
->Bits
= AllocateZeroPool (Block
->BitsLen
);
60 if (Block
->Bits
== NULL
) {
61 gBS
->FreePool (Block
);
66 // Allocate the number of Pages of memory, then map it for
67 // bus master read and write.
69 Status
= PciIo
->AllocateBuffer (
78 if (EFI_ERROR (Status
)) {
82 Bytes
= EFI_PAGES_TO_SIZE (Pages
);
85 EfiPciIoOperationBusMasterCommonBuffer
,
92 if (EFI_ERROR (Status
) || (Bytes
!= EFI_PAGES_TO_SIZE (Pages
))) {
97 // Check whether the data structure used by the host controller
98 // should be restricted into the same 4G
100 if (Pool
->Check4G
&& (Pool
->Which4G
!= USB_HC_HIGH_32BIT (MappedAddr
))) {
101 PciIo
->Unmap (PciIo
, Mapping
);
105 Block
->BufHost
= BufHost
;
106 Block
->Buf
= (UINT8
*) ((UINTN
) MappedAddr
);
107 Block
->Mapping
= Mapping
;
112 PciIo
->FreeBuffer (PciIo
, Pages
, BufHost
);
115 gBS
->FreePool (Block
->Bits
);
116 gBS
->FreePool (Block
);
122 Free the memory block from the memory pool.
124 @param Pool The memory pool to free the block from.
125 @param Block The memory block to free.
130 IN USBHC_MEM_POOL
*Pool
,
131 IN USBHC_MEM_BLOCK
*Block
134 EFI_PCI_IO_PROTOCOL
*PciIo
;
136 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
141 // Unmap the common buffer then free the structures
143 PciIo
->Unmap (PciIo
, Block
->Mapping
);
144 PciIo
->FreeBuffer (PciIo
, EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
);
146 gBS
->FreePool (Block
->Bits
);
147 gBS
->FreePool (Block
);
152 Alloc some memory from the block.
154 @param Block The memory block to allocate memory from.
155 @param Units Number of memory units to allocate.
157 @return The pointer to the allocated memory. If couldn't allocate the needed memory,
158 the return value is NULL.
162 UsbHcAllocMemFromBlock (
163 IN USBHC_MEM_BLOCK
*Block
,
174 ASSERT ((Block
!= 0) && (Units
!= 0));
180 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
182 // If current bit is zero, the corresponding memory unit is
183 // available, otherwise we need to restart our searching.
184 // Available counts the consective number of zero bit.
186 if (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
189 if (Available
>= Units
) {
193 NEXT_BIT (Byte
, Bit
);
196 NEXT_BIT (Byte
, Bit
);
204 if (Available
< Units
) {
209 // Mark the memory as allocated
214 for (Count
= 0; Count
< Units
; Count
++) {
215 ASSERT (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
217 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] | USB_HC_BIT (Bit
));
218 NEXT_BIT (Byte
, Bit
);
221 return Block
->Buf
+ (StartByte
* 8 + StartBit
) * USBHC_MEM_UNIT
;
226 Insert the memory block to the pool's list of the blocks.
228 @param Head The head of the memory pool's block list.
229 @param Block The memory block to insert.
233 UsbHcInsertMemBlockToPool (
234 IN USBHC_MEM_BLOCK
*Head
,
235 IN USBHC_MEM_BLOCK
*Block
238 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
239 Block
->Next
= Head
->Next
;
245 Is the memory block empty?
247 @param Block The memory block to check.
249 @retval TRUE The memory block is empty.
250 @retval FALSE The memory block isn't empty.
254 UsbHcIsMemBlockEmpty (
255 IN USBHC_MEM_BLOCK
*Block
260 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
261 if (Block
->Bits
[Index
] != 0) {
271 Unlink the memory block from the pool's list.
273 @param Head The block list head of the memory's pool.
274 @param BlockToUnlink The memory block to unlink.
278 UsbHcUnlinkMemBlock (
279 IN USBHC_MEM_BLOCK
*Head
,
280 IN USBHC_MEM_BLOCK
*BlockToUnlink
283 USBHC_MEM_BLOCK
*Block
;
285 ASSERT ((Head
!= NULL
) && (BlockToUnlink
!= NULL
));
287 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
288 if (Block
->Next
== BlockToUnlink
) {
289 Block
->Next
= BlockToUnlink
->Next
;
290 BlockToUnlink
->Next
= NULL
;
298 Initialize the memory management pool for the host controller.
300 @param PciIo The PciIo that can be used to access the host controller.
301 @param Check4G Whether the host controller requires allocated memory
302 from one 4G address space.
303 @param Which4G The 4G memory area each memory allocated should be from.
305 @retval EFI_SUCCESS The memory pool is initialized.
306 @retval EFI_OUT_OF_RESOURCE Fail to init the memory pool.
311 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
316 USBHC_MEM_POOL
*Pool
;
318 Pool
= AllocatePool (sizeof (USBHC_MEM_POOL
));
325 Pool
->Check4G
= Check4G
;
326 Pool
->Which4G
= Which4G
;
327 Pool
->Head
= UsbHcAllocMemBlock (Pool
, USBHC_MEM_DEFAULT_PAGES
);
329 if (Pool
->Head
== NULL
) {
330 gBS
->FreePool (Pool
);
339 Release the memory management pool.
341 @param Pool The USB memory pool to free.
343 @retval EFI_SUCCESS The memory pool is freed.
344 @retval EFI_DEVICE_ERROR Failed to free the memory pool.
349 IN USBHC_MEM_POOL
*Pool
352 USBHC_MEM_BLOCK
*Block
;
354 ASSERT (Pool
->Head
!= NULL
);
357 // Unlink all the memory blocks from the pool, then free them.
358 // UsbHcUnlinkMemBlock can't be used to unlink and free the
361 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
362 UsbHcUnlinkMemBlock (Pool
->Head
, Block
);
363 UsbHcFreeMemBlock (Pool
, Block
);
366 UsbHcFreeMemBlock (Pool
, Pool
->Head
);
367 gBS
->FreePool (Pool
);
373 Allocate some memory from the host controller's memory pool
374 which can be used to communicate with host controller.
376 @param Pool The host controller's memory pool.
377 @param Size Size of the memory to allocate.
379 @return The allocated memory or NULL.
384 IN USBHC_MEM_POOL
*Pool
,
388 USBHC_MEM_BLOCK
*Head
;
389 USBHC_MEM_BLOCK
*Block
;
390 USBHC_MEM_BLOCK
*NewBlock
;
396 AllocSize
= USBHC_MEM_ROUND (Size
);
398 ASSERT (Head
!= NULL
);
401 // First check whether current memory blocks can satisfy the allocation.
403 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
404 Mem
= UsbHcAllocMemFromBlock (Block
, AllocSize
/ USBHC_MEM_UNIT
);
417 // Create a new memory block if there is not enough memory
418 // in the pool. If the allocation size is larger than the
419 // default page number, just allocate a large enough memory
420 // block. Otherwise allocate default pages.
422 if (AllocSize
> EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES
)) {
423 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
425 Pages
= USBHC_MEM_DEFAULT_PAGES
;
428 NewBlock
= UsbHcAllocMemBlock (Pool
, Pages
);
430 if (NewBlock
== NULL
) {
431 DEBUG ((EFI_D_INFO
, "UsbHcAllocateMem: failed to allocate block\n"));
436 // Add the new memory block to the pool, then allocate memory from it
438 UsbHcInsertMemBlockToPool (Head
, NewBlock
);
439 Mem
= UsbHcAllocMemFromBlock (NewBlock
, AllocSize
/ USBHC_MEM_UNIT
);
450 Free the allocated memory back to the memory pool.
452 @param Pool The memory pool of the host controller.
453 @param Mem The memory to free.
454 @param Size The size of the memory to free.
459 IN USBHC_MEM_POOL
*Pool
,
464 USBHC_MEM_BLOCK
*Head
;
465 USBHC_MEM_BLOCK
*Block
;
473 AllocSize
= USBHC_MEM_ROUND (Size
);
474 ToFree
= (UINT8
*) Mem
;
476 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
478 // scan the memory block list for the memory block that
479 // completely contains the memory to free.
481 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
483 // compute the start byte and bit in the bit array
485 Byte
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) / 8;
486 Bit
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) % 8;
489 // reset associated bits in bit arry
491 for (Count
= 0; Count
< (AllocSize
/ USBHC_MEM_UNIT
); Count
++) {
492 ASSERT (USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
494 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ USB_HC_BIT (Bit
));
495 NEXT_BIT (Byte
, Bit
);
503 // If Block == NULL, it means that the current memory isn't
504 // in the host controller's pool. This is critical because
505 // the caller has passed in a wrong memory point
507 ASSERT (Block
!= NULL
);
510 // Release the current memory block if it is empty and not the head
512 if ((Block
!= Head
) && UsbHcIsMemBlockEmpty (Block
)) {
513 UsbHcUnlinkMemBlock (Head
, Block
);
514 UsbHcFreeMemBlock (Pool
, Block
);