]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkModulePkg/Bus/Pci/Ehci/Dxe/EhciMem.c
1. Add NULL QH to set as QH header;
[mirror_edk2.git] / EdkModulePkg / Bus / Pci / Ehci / Dxe / EhciMem.c
index aba27cb82c1f8624bda734a64fb262cd794b3557..9eef0537d0e36fb5fd17df7e9e6056fd198a50b0 100644 (file)
@@ -1,20 +1,20 @@
 /*++\r
 \r
-Copyright (c) 2006, Intel Corporation                                                     \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+Copyright (c) 2006, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 Module Name:\r
 \r
     EhciMem.c\r
-    \r
-Abstract: \r
-    \r
+\r
+Abstract:\r
+\r
 \r
 Revision History\r
 --*/\r
@@ -40,13 +40,13 @@ Arguments:
   HcDev                  - USB2_HC_DEV\r
   MemoryHeader           - MEMORY_MANAGE_HEADER to output\r
   MemoryBlockSizeInPages - MemoryBlockSizeInPages\r
-  \r
+\r
 Returns:\r
 \r
   EFI_SUCCESS           Success\r
   EFI_OUT_OF_RESOURCES  Fail for no resources\r
   EFI_UNSUPPORTED       Unsupported currently\r
-  \r
+\r
 --*/\r
 {\r
   EFI_STATUS            Status;\r
@@ -73,7 +73,7 @@ Returns:
   //\r
   // each bit in Bit Array will manage 32 bytes memory in memory block\r
   //\r
-  (*MemoryHeader)->BitArraySizeInBytes = ((*MemoryHeader)->MemoryBlockSizeInBytes / 32) / 8;\r
+  (*MemoryHeader)->BitArraySizeInBytes = ((*MemoryHeader)->MemoryBlockSizeInBytes / MEM_UNIT_SIZE) / 8;\r
 \r
   //\r
   // Allocate memory for BitArray\r
@@ -83,7 +83,7 @@ Returns:
     gBS->FreePool (*MemoryHeader);\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  \r
+\r
   //\r
   // Memory Block uses MemoryBlockSizeInPages pages,\r
   // and it is allocated as common buffer use.\r
@@ -112,7 +112,7 @@ Returns:
                            &Mapping\r
                            );\r
   //\r
-  // If returned Mapped size is less than the size \r
+  // If returned Mapped size is less than the size\r
   // we request,do not support.\r
   //\r
   if (EFI_ERROR (Status) || (MemoryBlockSizeInBytes != EFI_PAGES_TO_SIZE (MemoryBlockSizeInPages))) {\r
@@ -121,9 +121,9 @@ Returns:
     gBS->FreePool (*MemoryHeader);\r
     return EFI_UNSUPPORTED;\r
   }\r
-  \r
+\r
   //\r
-  // Data structure involved by host controller \r
+  // Data structure involved by host controller\r
   // should be restricted into the same 4G\r
   //\r
   if (HcDev->Is64BitCapable != 0) {\r
@@ -135,7 +135,7 @@ Returns:
       return EFI_UNSUPPORTED;\r
        }\r
   }\r
-  \r
+\r
   //\r
   // Set Memory block initial address\r
   //\r
@@ -240,16 +240,16 @@ Returns:
   ASSERT (MemoryHeader != NULL);\r
 \r
   OldTpl = gBS->RaiseTPL (EFI_TPL_NOTIFY + 1);\r
-  \r
+\r
   //\r
   // allocate unit is 32 bytes (align on 32 byte)\r
   //\r
-  if (AllocSize & 0x1F) {\r
-    RealAllocSize = (AllocSize / 32 + 1) * 32;\r
+  if (AllocSize & (MEM_UNIT_SIZE - 1)) {\r
+    RealAllocSize = (AllocSize / MEM_UNIT_SIZE + 1) * MEM_UNIT_SIZE;\r
   } else {\r
     RealAllocSize = AllocSize;\r
   }\r
-  \r
+\r
   //\r
   // There may be linked MemoryHeaders.\r
   // To allocate a free pool in Memory blocks,\r
@@ -262,22 +262,26 @@ Returns:
     Status = AllocMemInMemoryBlock (\r
               TempHeaderPtr,\r
               (VOID **) Pool,\r
-              RealAllocSize / 32\r
+              RealAllocSize / MEM_UNIT_SIZE\r
               );\r
     if (!EFI_ERROR (Status)) {\r
-      ZeroMem (*Pool, AllocSize);\r
-      gBS->RestoreTPL (OldTpl);\r
-      return EFI_SUCCESS;\r
+       break;\r
     }\r
   }\r
 \r
   gBS->RestoreTPL (OldTpl);\r
-  \r
+\r
+  if (!EFI_ERROR (Status)) {\r
+     ZeroMem (*Pool, AllocSize);\r
+     return EFI_SUCCESS;\r
+  }\r
+\r
+\r
   //\r
   // There is no enough memory,\r
   // Create a new Memory Block\r
   //\r
-  \r
+\r
   //\r
   // if pool size is larger than NORMAL_MEMORY_BLOCK_UNIT_IN_PAGES,\r
   // just allocate a large enough memory block.\r
@@ -303,13 +307,15 @@ Returns:
   Status = AllocMemInMemoryBlock (\r
              NewMemoryHeader,\r
              (VOID **) Pool,\r
-             RealAllocSize / 32\r
+             RealAllocSize / MEM_UNIT_SIZE\r
              );\r
+\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
   if (!EFI_ERROR (Status)) {\r
     ZeroMem (*Pool, AllocSize);\r
   }\r
 \r
-  gBS->RestoreTPL (OldTpl);\r
   return Status;\r
 }\r
 \r
@@ -354,12 +360,12 @@ Returns:
   //\r
   // allocate unit is 32 byte (align on 32 byte)\r
   //\r
-  if (AllocSize & 0x1F) {\r
-    RealAllocSize = (AllocSize / 32 + 1) * 32;\r
+  if (AllocSize & (MEM_UNIT_SIZE - 1)) {\r
+    RealAllocSize = (AllocSize / MEM_UNIT_SIZE + 1) * MEM_UNIT_SIZE;\r
   } else {\r
     RealAllocSize = AllocSize;\r
   }\r
-  \r
+\r
   //\r
   // scan the memory header linked list for\r
   // the asigned memory to free.\r
@@ -373,14 +379,16 @@ Returns:
       // Pool is in the Memory Block area,\r
       // find the start byte and bit in the bit array\r
       //\r
-      StartBytePos  = ((Pool - TempHeaderPtr->MemoryBlockPtr) / 32) / 8;\r
-      StartBitPos   = (UINT8) (((Pool - TempHeaderPtr->MemoryBlockPtr) / 32) & 0x7);\r
+      StartBytePos  = ((Pool - TempHeaderPtr->MemoryBlockPtr) / MEM_UNIT_SIZE) / 8;\r
+      StartBitPos   = (UINT8) (((Pool - TempHeaderPtr->MemoryBlockPtr) / MEM_UNIT_SIZE) & 0x7);\r
 \r
       //\r
       // reset associated bits in bit arry\r
       //\r
-      for (Index = StartBytePos, Index2 = StartBitPos, Count = 0; Count < (RealAllocSize / 32); Count++) {\r
-        TempHeaderPtr->BitArrayPtr[Index] = (UINT8) (TempHeaderPtr->BitArrayPtr[Index] ^ (bit (Index2)));\r
+      for (Index = StartBytePos, Index2 = StartBitPos, Count = 0; Count < (RealAllocSize / MEM_UNIT_SIZE); Count++) {\r
+        ASSERT ((TempHeaderPtr->BitArrayPtr[Index] & bit (Index2) )== bit (Index2));\r
+\r
+        TempHeaderPtr->BitArrayPtr[Index] ^= (UINT8) (bit (Index2));\r
         Index2++;\r
         if (Index2 == 8) {\r
           Index += 1;\r
@@ -393,7 +401,7 @@ Returns:
       break;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // Release emptied memory blocks (only if the memory block is not\r
   // the first one in the memory header list\r
@@ -479,7 +487,7 @@ Arguments:
 Returns:\r
 \r
   EFI_SUCCESS    Success\r
-  EFI_NOT_FOUND  Can't find the free memory \r
+  EFI_NOT_FOUND  Can't find the free memory\r
 \r
 --*/\r
 {\r
@@ -508,7 +516,7 @@ Returns:
     //\r
     // right shift the byte\r
     //\r
-    ByteValue /= 2;\r
+    ByteValue = ByteValue >> 1;\r
 \r
     if (BitValue == 0) {\r
       //\r
@@ -564,11 +572,11 @@ Returns:
   if (NumberOfZeros < NumberOfMemoryUnit) {\r
     return EFI_NOT_FOUND;\r
   }\r
-  \r
+\r
   //\r
   // Found enough free space.\r
   //\r
-  \r
+\r
   //\r
   // The values recorded in (FoundBytePos,FoundBitPos) have two conditions:\r
   //  1)(FoundBytePos,FoundBitPos) record the position\r
@@ -581,7 +589,7 @@ Returns:
   if ((MemoryHeader->BitArrayPtr[FoundBytePos] & bit (FoundBitPos)) != 0) {\r
     FoundBitPos += 1;\r
   }\r
-  \r
+\r
   //\r
   // Have the (FoundBytePos,FoundBitPos) make sense.\r
   //\r
@@ -589,13 +597,14 @@ Returns:
     FoundBytePos += 1;\r
     FoundBitPos -= 8;\r
   }\r
-  \r
+\r
   //\r
   // Set the memory as allocated\r
   //\r
   for (TempBytePos = FoundBytePos, Index = FoundBitPos, Count = 0; Count < NumberOfMemoryUnit; Count++) {\r
 \r
-    MemoryHeader->BitArrayPtr[TempBytePos] = (UINT8) (MemoryHeader->BitArrayPtr[TempBytePos] | (bit (Index)));\r
+    ASSERT ((MemoryHeader->BitArrayPtr[TempBytePos] & bit (Index) )== 0);\r
+    MemoryHeader->BitArrayPtr[TempBytePos] |= bit (Index);\r
     Index++;\r
     if (Index == 8) {\r
       TempBytePos += 1;\r
@@ -603,7 +612,7 @@ Returns:
     }\r
   }\r
 \r
-  *Pool = MemoryHeader->MemoryBlockPtr + (FoundBytePos * 8 + FoundBitPos) * 32;\r
+  *Pool = MemoryHeader->MemoryBlockPtr + (FoundBytePos * 8 + FoundBitPos) * MEM_UNIT_SIZE;\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -625,7 +634,7 @@ Arguments:
 Returns:\r
 \r
   TRUE    Empty\r
-  FALSE   Not Empty \r
+  FALSE   Not Empty\r
 \r
 --*/\r
 {\r
@@ -675,6 +684,7 @@ Returns:
       // Link the before and after\r
       //\r
       TempHeaderPtr->Next = NeedFreeMemoryHeader->Next;\r
+      NeedFreeMemoryHeader->Next = NULL;\r
       break;\r
     }\r
   }\r
@@ -698,7 +708,7 @@ Returns:
 \r
   EFI_SUCCESS        Success\r
   EFI_DEVICE_ERROR   Fail\r
-  \r
+\r
 --*/\r
 {\r
   EFI_STATUS            Status;\r
@@ -736,7 +746,7 @@ Returns:
 \r
   EFI_SUCCESS        Success\r
   EFI_DEVICE_ERROR   Fail\r
-  \r
+\r
 --*/\r
 {\r
   MEMORY_MANAGE_HEADER  *TempHeaderPtr;\r