3 The routine procedure for uhci memory allocate/free.
5 Copyright (c) 2007 - 2010, 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.
20 Allocate a block of memory to be used by the buffer pool.
22 @param Pool The buffer pool to allocate memory for.
23 @param Pages How many pages to allocate.
25 @return The allocated memory block or NULL if failed.
30 IN USBHC_MEM_POOL
*Pool
,
34 USBHC_MEM_BLOCK
*Block
;
35 EFI_PCI_IO_PROTOCOL
*PciIo
;
38 EFI_PHYSICAL_ADDRESS MappedAddr
;
44 Block
= AllocateZeroPool (sizeof (USBHC_MEM_BLOCK
));
50 // each bit in the bit array represents USBHC_MEM_UNIT
51 // bytes of memory in the memory block.
53 ASSERT (USBHC_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
55 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
56 Block
->BitsLen
= Block
->BufLen
/ (USBHC_MEM_UNIT
* 8);
57 Block
->Bits
= AllocateZeroPool (Block
->BitsLen
);
59 if (Block
->Bits
== NULL
) {
60 gBS
->FreePool (Block
);
65 // Allocate the number of Pages of memory, then map it for
66 // bus master read and write.
68 Status
= PciIo
->AllocateBuffer (
77 if (EFI_ERROR (Status
)) {
81 Bytes
= EFI_PAGES_TO_SIZE (Pages
);
84 EfiPciIoOperationBusMasterCommonBuffer
,
91 if (EFI_ERROR (Status
) || (Bytes
!= EFI_PAGES_TO_SIZE (Pages
))) {
96 // Check whether the data structure used by the host controller
97 // should be restricted into the same 4G
99 if (Pool
->Check4G
&& (Pool
->Which4G
!= USB_HC_HIGH_32BIT (MappedAddr
))) {
100 PciIo
->Unmap (PciIo
, Mapping
);
104 Block
->BufHost
= BufHost
;
105 Block
->Buf
= (UINT8
*) ((UINTN
) MappedAddr
);
106 Block
->Mapping
= Mapping
;
111 PciIo
->FreeBuffer (PciIo
, Pages
, BufHost
);
114 gBS
->FreePool (Block
->Bits
);
115 gBS
->FreePool (Block
);
121 Free the memory block from the memory pool.
123 @param Pool The memory pool to free the block from.
124 @param Block The memory block to free.
129 IN USBHC_MEM_POOL
*Pool
,
130 IN USBHC_MEM_BLOCK
*Block
133 EFI_PCI_IO_PROTOCOL
*PciIo
;
135 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
140 // Unmap the common buffer then free the structures
142 PciIo
->Unmap (PciIo
, Block
->Mapping
);
143 PciIo
->FreeBuffer (PciIo
, EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
);
145 gBS
->FreePool (Block
->Bits
);
146 gBS
->FreePool (Block
);
151 Alloc some memory from the block.
153 @param Block The memory block to allocate memory from.
154 @param Units Number of memory units to allocate.
156 @return EFI_SUCCESS The needed memory is allocated.
157 @return EFI_NOT_FOUND Can't find the free memory.
161 UsbHcAllocMemFromBlock (
162 IN USBHC_MEM_BLOCK
*Block
,
173 ASSERT ((Block
!= 0) && (Units
!= 0));
179 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
181 // If current bit is zero, the corresponding memory unit is
182 // available, otherwise we need to restart our searching.
183 // Available counts the consective number of zero bit.
185 if (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
188 if (Available
>= Units
) {
192 NEXT_BIT (Byte
, Bit
);
195 NEXT_BIT (Byte
, Bit
);
203 if (Available
< Units
) {
208 // Mark the memory as allocated
213 for (Count
= 0; Count
< Units
; Count
++) {
214 ASSERT (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
216 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] | (UINT8
) USB_HC_BIT (Bit
));
217 NEXT_BIT (Byte
, Bit
);
220 return Block
->Buf
+ (StartByte
* 8 + StartBit
) * USBHC_MEM_UNIT
;
224 Calculate the corresponding pci bus address according to the Mem parameter.
226 @param Pool The memory pool of the host controller.
227 @param Mem The pointer to host memory.
228 @param Size The size of the memory region.
230 @return the pci memory address
233 UsbHcGetPciAddressForHostMem (
234 IN USBHC_MEM_POOL
*Pool
,
239 USBHC_MEM_BLOCK
*Head
;
240 USBHC_MEM_BLOCK
*Block
;
242 EFI_PHYSICAL_ADDRESS PhyAddr
;
246 AllocSize
= USBHC_MEM_ROUND (Size
);
252 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
254 // scan the memory block list for the memory block that
255 // completely contains the allocated memory.
257 if ((Block
->BufHost
<= (UINT8
*) Mem
) && (((UINT8
*) Mem
+ AllocSize
) <= (Block
->BufHost
+ Block
->BufLen
))) {
262 ASSERT ((Block
!= NULL
));
264 // calculate the pci memory address for host memory address.
266 Offset
= (UINT8
*)Mem
- Block
->BufHost
;
267 PhyAddr
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) (Block
->Buf
+ Offset
);
272 Insert the memory block to the pool's list of the blocks.
274 @param Head The head of the memory pool's block list.
275 @param Block The memory block to insert.
279 UsbHcInsertMemBlockToPool (
280 IN USBHC_MEM_BLOCK
*Head
,
281 IN USBHC_MEM_BLOCK
*Block
284 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
285 Block
->Next
= Head
->Next
;
291 Is the memory block empty?
293 @param Block The memory block to check.
295 @return TRUE The memory block is empty.
296 @return FALSE The memory block isn't empty.
300 UsbHcIsMemBlockEmpty (
301 IN USBHC_MEM_BLOCK
*Block
306 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
307 if (Block
->Bits
[Index
] != 0) {
317 Unlink the memory block from the pool's list.
319 @param Head The block list head of the memory's pool.
320 @param BlockToUnlink The memory block to unlink.
324 UsbHcUnlinkMemBlock (
325 IN USBHC_MEM_BLOCK
*Head
,
326 IN USBHC_MEM_BLOCK
*BlockToUnlink
329 USBHC_MEM_BLOCK
*Block
;
331 ASSERT ((Head
!= NULL
) && (BlockToUnlink
!= NULL
));
333 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
334 if (Block
->Next
== BlockToUnlink
) {
335 Block
->Next
= BlockToUnlink
->Next
;
336 BlockToUnlink
->Next
= NULL
;
344 Initialize the memory management pool for the host controller.
346 @param PciIo The PciIo that can be used to access the host controller.
347 @param Check4G Whether the host controller requires allocated memory
348 from one 4G address space.
349 @param Which4G The 4G memory area each memory allocated should be from.
351 @return EFI_SUCCESS The memory pool is initialized.
352 @return EFI_OUT_OF_RESOURCE Fail to init the memory pool.
357 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
362 USBHC_MEM_POOL
*Pool
;
364 Pool
= AllocatePool (sizeof (USBHC_MEM_POOL
));
371 Pool
->Check4G
= Check4G
;
372 Pool
->Which4G
= Which4G
;
373 Pool
->Head
= UsbHcAllocMemBlock (Pool
, USBHC_MEM_DEFAULT_PAGES
);
375 if (Pool
->Head
== NULL
) {
376 gBS
->FreePool (Pool
);
385 Release the memory management pool.
387 @param Pool The USB memory pool to free.
389 @return EFI_SUCCESS The memory pool is freed.
390 @return EFI_DEVICE_ERROR Failed to free the memory pool.
395 IN USBHC_MEM_POOL
*Pool
398 USBHC_MEM_BLOCK
*Block
;
400 ASSERT (Pool
->Head
!= NULL
);
403 // Unlink all the memory blocks from the pool, then free them.
404 // UsbHcUnlinkMemBlock can't be used to unlink and free the
407 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
408 UsbHcUnlinkMemBlock (Pool
->Head
, Block
);
409 UsbHcFreeMemBlock (Pool
, Block
);
412 UsbHcFreeMemBlock (Pool
, Pool
->Head
);
413 gBS
->FreePool (Pool
);
419 Allocate some memory from the host controller's memory pool
420 which can be used to communicate with host controller.
422 @param Pool The host controller's memory pool.
423 @param Size Size of the memory to allocate.
425 @return The allocated memory or NULL.
430 IN USBHC_MEM_POOL
*Pool
,
434 USBHC_MEM_BLOCK
*Head
;
435 USBHC_MEM_BLOCK
*Block
;
436 USBHC_MEM_BLOCK
*NewBlock
;
442 AllocSize
= USBHC_MEM_ROUND (Size
);
444 ASSERT (Head
!= NULL
);
447 // First check whether current memory blocks can satisfy the allocation.
449 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
450 Mem
= UsbHcAllocMemFromBlock (Block
, AllocSize
/ USBHC_MEM_UNIT
);
463 // Create a new memory block if there is not enough memory
464 // in the pool. If the allocation size is larger than the
465 // default page number, just allocate a large enough memory
466 // block. Otherwise allocate default pages.
468 if (AllocSize
> EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES
)) {
469 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
471 Pages
= USBHC_MEM_DEFAULT_PAGES
;
474 NewBlock
= UsbHcAllocMemBlock (Pool
, Pages
);
476 if (NewBlock
== NULL
) {
477 DEBUG ((EFI_D_INFO
, "UsbHcAllocateMem: failed to allocate block\n"));
482 // Add the new memory block to the pool, then allocate memory from it
484 UsbHcInsertMemBlockToPool (Head
, NewBlock
);
485 Mem
= UsbHcAllocMemFromBlock (NewBlock
, AllocSize
/ USBHC_MEM_UNIT
);
496 Free the allocated memory back to the memory pool.
498 @param Pool The memory pool of the host controller.
499 @param Mem The memory to free.
500 @param Size The size of the memory to free.
505 IN USBHC_MEM_POOL
*Pool
,
510 USBHC_MEM_BLOCK
*Head
;
511 USBHC_MEM_BLOCK
*Block
;
519 AllocSize
= USBHC_MEM_ROUND (Size
);
520 ToFree
= (UINT8
*) Mem
;
522 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
524 // scan the memory block list for the memory block that
525 // completely contains the memory to free.
527 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
529 // compute the start byte and bit in the bit array
531 Byte
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) / 8;
532 Bit
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) % 8;
535 // reset associated bits in bit arry
537 for (Count
= 0; Count
< (AllocSize
/ USBHC_MEM_UNIT
); Count
++) {
538 ASSERT (USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
540 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ USB_HC_BIT (Bit
));
541 NEXT_BIT (Byte
, Bit
);
549 // If Block == NULL, it means that the current memory isn't
550 // in the host controller's pool. This is critical because
551 // the caller has passed in a wrong memory point
553 ASSERT (Block
!= NULL
);
556 // Release the current memory block if it is empty and not the head
558 if ((Block
!= Head
) && UsbHcIsMemBlockEmpty (Block
)) {
559 UsbHcUnlinkMemBlock (Head
, Block
);
560 UsbHcFreeMemBlock (Pool
, Block
);