46 ASSERT (USBHC_MEM_UNIT * 8 <= EFI_PAGE_SIZE);
49 Block->BitsLen = Block->BufLen / (USBHC_MEM_UNIT * 8);
52 if (Block->Bits ==
NULL) {
53 gBS->FreePool (Block);
61 Status = PciIo->AllocateBuffer (
70 if (EFI_ERROR (Status)) {
88 Block->BufHost = BufHost;
89 Block->Buf = (UINT8 *)((
UINTN)MappedAddr);
90 Block->Mapping = Mapping;
95 PciIo->FreeBuffer (PciIo, Pages, BufHost);
98 gBS->FreePool (Block->Bits);
99 gBS->FreePool (Block);
118 ASSERT ((Pool !=
NULL) && (Block !=
NULL));
125 PciIo->Unmap (PciIo, Block->Mapping);
128 gBS->FreePool (Block->Bits);
129 gBS->FreePool (Block);
147 IN BOOLEAN AllocationForRing
159 ASSERT ((Block != 0) && (Units != 0));
164 AlignmentMask = ~((
UINTN)USBHC_MEM_TRB_RINGS_BOUNDARY - 1);
166 for (Byte = 0, Bit = 0; Byte < Block->BitsLen;) {
172 if (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit)) {
173 if (AllocationForRing && (Available != 0)) {
174 MemUnitAddr = (
UINTN)Block->BufHost + (Byte * 8 + Bit) * USBHC_MEM_UNIT;
175 if ((MemUnitAddr & AlignmentMask) != ((MemUnitAddr - USBHC_MEM_UNIT) & AlignmentMask)) {
189 if (Available >= Units) {
193 NEXT_BIT (Byte, Bit);
195 NEXT_BIT (Byte, Bit);
203 if (Available < Units) {
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] | USB_HC_BIT (Bit));
217 NEXT_BIT (Byte, Bit);
220 return Block->BufHost + (StartByte * 8 + StartBit) * USBHC_MEM_UNIT;
250 AllocSize = USBHC_MEM_ROUND (Size);
259 for (Block = Head; Block !=
NULL; Block = Block->Next) {
264 if ((Block->BufHost <= (UINT8 *)Mem) && (((UINT8 *)Mem + AllocSize) <= (Block->BufHost + Block->BufLen))) {
269 ASSERT ((Block !=
NULL));
273 Offset = (UINT8 *)Mem - Block->BufHost;
305 AllocSize = USBHC_MEM_ROUND (Size);
314 for (Block = Head; Block !=
NULL; Block = Block->Next) {
319 if ((Block->Buf <= (UINT8 *)Mem) && (((UINT8 *)Mem + AllocSize) <= (Block->Buf + Block->BufLen))) {
324 ASSERT ((Block !=
NULL));
328 Offset = (UINT8 *)Mem - Block->Buf;
346 ASSERT ((Head !=
NULL) && (Block !=
NULL));
347 Block->Next = Head->Next;
367 for (Index = 0; Index < Block->BitsLen; Index++) {
368 if (Block->Bits[Index] != 0) {
391 ASSERT ((Head !=
NULL) && (BlockToUnlink !=
NULL));
393 for (Block = Head; Block !=
NULL; Block = Block->Next) {
394 if (Block->Next == BlockToUnlink) {
395 Block->Next = BlockToUnlink->Next;
396 BlockToUnlink->Next =
NULL;
427 if (Pool->Head ==
NULL) {
428 gBS->FreePool (Pool);
451 ASSERT (Pool->Head !=
NULL);
458 for (Block = Pool->Head->Next; Block !=
NULL; Block = Pool->Head->Next) {
464 gBS->FreePool (Pool);
483 IN BOOLEAN AllocationForRing
494 AllocSize = USBHC_MEM_ROUND (Size);
496 ASSERT (Head !=
NULL);
501 for (Block = Head; Block !=
NULL; Block = Block->Next) {
523 Pages = USBHC_MEM_DEFAULT_PAGES;
528 if (NewBlock ==
NULL) {
529 DEBUG ((DEBUG_ERROR,
"UsbHcAllocateMem: failed to allocate block\n"));
570 AllocSize = USBHC_MEM_ROUND (Size);
571 ToFree = (UINT8 *)Mem;
573 for (Block = Head; Block !=
NULL; Block = Block->Next) {
578 if ((Block->BufHost <= ToFree) && ((ToFree + AllocSize) <= (Block->BufHost + Block->BufLen))) {
582 Byte = ((ToFree - Block->BufHost) / USBHC_MEM_UNIT) / 8;
583 Bit = ((ToFree - Block->BufHost) / USBHC_MEM_UNIT) % 8;
588 for (Count = 0; Count < (AllocSize / USBHC_MEM_UNIT); Count++) {
589 ASSERT (USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));
591 Block->Bits[Byte] = (UINT8)(Block->Bits[Byte] ^ USB_HC_BIT (Bit));
592 NEXT_BIT (Byte, Bit);
604 ASSERT (Block !=
NULL);
641 OUT VOID **HostAddress,
650 UINTN UnalignedPages;
657 ASSERT ((Alignment & (Alignment - 1)) == 0);
659 if ((Alignment & (Alignment - 1)) != 0) {
660 return EFI_INVALID_PARAMETER;
664 return EFI_INVALID_PARAMETER;
667 if (Alignment > EFI_PAGE_SIZE) {
671 AlignmentMask = Alignment - 1;
676 ASSERT (RealPages > Pages);
678 Status = PciIo->AllocateBuffer (
686 if (EFI_ERROR (Status)) {
687 return EFI_OUT_OF_RESOURCES;
690 AlignedMemory = ((
UINTN)Memory + AlignmentMask) & ~AlignmentMask;
692 if (UnalignedPages > 0) {
696 Status = PciIo->FreeBuffer (PciIo, UnalignedPages, Memory);
701 UnalignedPages = RealPages - Pages - UnalignedPages;
702 if (UnalignedPages > 0) {
706 Status = PciIo->FreeBuffer (PciIo, UnalignedPages, Memory);
713 Status = PciIo->AllocateBuffer (
721 if (EFI_ERROR (Status)) {
722 return EFI_OUT_OF_RESOURCES;
725 AlignedMemory = (
UINTN)Memory;
729 Status = PciIo->Map (
732 (VOID *)AlignedMemory,
739 Status = PciIo->FreeBuffer (PciIo, Pages, (VOID *)AlignedMemory);
740 return EFI_OUT_OF_RESOURCES;
743 *HostAddress = (VOID *)AlignedMemory;
760 IN VOID *HostAddress,
769 Status = PciIo->Unmap (PciIo, Mapping);
772 Status = PciIo->FreeBuffer (
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID * UsbHcAllocMemFromBlock(IN USBHC_MEM_BLOCK *Block, IN UINTN Units)
EFI_STATUS UsbHcFreeMemPool(IN USBHC_MEM_POOL *Pool)
VOID UsbHcFreeMemBlock(IN USBHC_MEM_POOL *Pool, IN USBHC_MEM_BLOCK *Block)
BOOLEAN UsbHcIsMemBlockEmpty(IN USBHC_MEM_BLOCK *Block)
VOID UsbHcFreeMem(IN USBHC_MEM_POOL *Pool, IN VOID *Mem, IN UINTN Size)
VOID * UsbHcAllocateMem(IN USBHC_MEM_POOL *Pool, IN UINTN Size)
USBHC_MEM_BLOCK * UsbHcAllocMemBlock(IN USBHC_MEM_POOL *Pool, IN UINTN Pages)
VOID UsbHcUnlinkMemBlock(IN USBHC_MEM_BLOCK *Head, IN USBHC_MEM_BLOCK *BlockToUnlink)
VOID UsbHcInsertMemBlockToPool(IN USBHC_MEM_BLOCK *Head, IN USBHC_MEM_BLOCK *Block)
USBHC_MEM_POOL * UsbHcInitMemPool(IN EFI_PCI_IO_PROTOCOL *PciIo, IN BOOLEAN Check4G, IN UINT32 Which4G)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
#define ASSERT_EFI_ERROR(StatusParameter)
#define DEBUG(Expression)
@ EfiPciIoOperationBusMasterCommonBuffer
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
UINT64 EFI_PHYSICAL_ADDRESS
#define EFI_PAGES_TO_SIZE(Pages)
#define EFI_SIZE_TO_PAGES(Size)
EFI_PHYSICAL_ADDRESS UsbHcGetPciAddrForHostAddr(IN USBHC_MEM_POOL *Pool, IN VOID *Mem, IN UINTN Size, IN BOOLEAN Alignment)
EFI_STATUS UsbHcAllocateAlignedPages(IN EFI_PCI_IO_PROTOCOL *PciIo, IN UINTN Pages, IN UINTN Alignment, OUT VOID **HostAddress, OUT EFI_PHYSICAL_ADDRESS *DeviceAddress, OUT VOID **Mapping)
EFI_PHYSICAL_ADDRESS UsbHcGetHostAddrForPciAddr(IN USBHC_MEM_POOL *Pool, IN VOID *Mem, IN UINTN Size, IN BOOLEAN Alignment)
VOID UsbHcFreeAlignedPages(IN EFI_PCI_IO_PROTOCOL *PciIo, IN VOID *HostAddress, IN UINTN Pages, VOID *Mapping)