From nobody Sat May 18 22:54:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+95294+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+95294+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kunluntech.com.cn ARC-Seal: i=1; a=rsa-sha256; t=1666015724; cv=none; d=zohomail.com; s=zohoarc; b=cv6MHr78/3Uk7UG3awgYXYWzPF2qgdUaDRyoKd6vW2BV6mywaDgs0MOGCpNdx0hEpfwOS1Rf2ywK473TNf6Ir+VehCr4AdRZmNnsW0tvYGYuhxrAT9YyN1MYPmRB7eTFB+L8DQVFcRdjzp2sDo17XPb96CLr3F5YLJVBRexeve4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1666015724; h=Content-Type:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=sOPqjJ0ndLdJFKvYw7li9wIGR97pFrlD40+GLUN7TPQ=; b=BG7wFqhE+ujoZxEfH6eEJUbT3xaZgC+LkIj6E8G11pxMf81MqIFFgwZdFBJ/CzlhDNAA0JqaQdacjgYFXBrCTTKVdFna977HX6ddAi7ERcNIHwFsNk66TY986yCKsJ2aepPcXi0Sqw/mSdZgke+YenkJfmXobSvX8yDDOZFLQ/k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+95294+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1666015724918607.8707881293568; Mon, 17 Oct 2022 07:08:44 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id tcuZYY1788612xHtby8x4ffe; Mon, 17 Oct 2022 07:08:44 -0700 Subject: [edk2-devel] [PATCH] MdeModulePkg/XhciDxe: Add a parameter to indicate whether the memory allocation is for TRB Ring or not. To: devel@edk2.groups.io From: "Jiading Zhang" X-Originating-Location: Beijing, CN (120.244.160.188) X-Originating-Platform: Windows Chrome 106 User-Agent: GROUPS.IO Web Poster MIME-Version: 1.0 Date: Mon, 17 Oct 2022 07:08:43 -0700 Message-ID: Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,jdzhang@kunluntech.com.cn X-Gm-Message-State: 9MAQ8Tep4RzLmra8cpwshmeYx1787277AA= Content-Type: multipart/alternative; boundary="yTUoIrJf7fCOuOveMXqr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1666015724; bh=BRS9csHqKLhpmT/x9oarxcWdCBbcTH835vOfboWcY7k=; h=Content-Type:Date:From:Reply-To:Subject:To; b=AfHIcseD+6jpBqkffGIbiKDsPhyII7LkTABTXyd3VhY/6iFDP+S5wUPO2dzaMFpaXkK XEmP9NrdzqN/ntXM87+Nz+jx1QBkeO1nrmHThFz4Jv+3an9Pb4gYkK/YdO1PKl8O2xT0P 0RW6rnOllFeHaZ6RqKY2p8gUiMC79o76pJ4= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1666015725322100002 --yTUoIrJf7fCOuOveMXqr Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 According the Xhci Spec, TRB Rings may be larger than a Page, however they = shall not cross a 64K byte boundary, so add a parameter to indicate whether= the memory allocation is for TRB or not. It will ensure the allocation not= crossing 64K boundary in UsbHcAllocMemFromBlock if the memory is allocated= for TRB. Signed-off-by: jdzhang Reviewed-by: Hao A Wu > Reviewed-by: Hao A Wu --- MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c=C2=A0 | 37 +++++++++++++++++++----- MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.h=C2=A0 | 10 +++++-- MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c | 16 +++++----- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c b/MdeModulePkg/Bus/Pci= /XhciDxe/UsbHcMem.c index 99fb3521d5..24714502cd 100644 --- a/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c +++ b/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c @@ -132,8 +132,9 @@ UsbHcFreeMemBlock ( /** Alloc some memory from the block. -=C2=A0 @param=C2=A0 Block=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 The memory blo= ck to allocate memory from. -=C2=A0 @param=C2=A0 Units=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Number of memo= ry units to allocate. +=C2=A0 @param=C2=A0 Block=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 The memory block to allocate memory from. +=C2=A0 @param=C2=A0 Units=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 Number of memory units to allocate. +=C2=A0 @param=C2=A0 AllocationForRing=C2=A0 =C2=A0 The allocated memory is= for Ring or not. @return The pointer to the allocated memory. If couldn't allocate the neede= d memory, the return value is NULL. @@ -142,7 +143,8 @@ UsbHcFreeMemBlock ( VOID * UsbHcAllocMemFromBlock ( IN=C2=A0 USBHC_MEM_BLOCK=C2=A0 *Block, -=C2=A0 IN=C2=A0 UINTN=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Units +=C2=A0 IN=C2=A0 UINTN=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Units, +=C2=A0 IN=C2=A0 BOOLEAN=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 AllocationForTrb= Ring ) { UINTN=C2=A0 Byte; @@ -151,12 +153,15 @@ UsbHcAllocMemFromBlock ( UINT8=C2=A0 StartBit; UINTN=C2=A0 Available; UINTN=C2=A0 Count; +=C2=A0 UINTN=C2=A0 MemUnitAddr; +=C2=A0 UINTN=C2=A0 AlignmentMask; ASSERT ((Block !=3D 0) && (Units !=3D 0)); StartByte =3D 0; StartBit=C2=A0 =3D 0; Available =3D 0; +=C2=A0 AlignmentMask =3D ~((UINTN)USBHC_MEM_TRB_RINGS_BOUNDARY - 1); for (Byte =3D 0, Bit =3D 0; Byte < Block->BitsLen;) { // @@ -165,6 +170,20 @@ UsbHcAllocMemFromBlock ( // Available counts the consective number of zero bit. // if (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit)) { +=C2=A0 =C2=A0 =C2=A0 if (AllocationForTrbRing && (Available !=3D 0)) { +=C2=A0 =C2=A0 =C2=A0 =C2=A0 MemUnitAddr =3D (UINTN)Block->BufHost + (Byte = * 8 + Bit) * USBHC_MEM_UNIT; +=C2=A0 =C2=A0 =C2=A0 =C2=A0 if ((MemUnitAddr & AlignmentMask) !=3D ((MemUn= itAddr - USBHC_MEM_UNIT) & AlignmentMask)) { +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 // +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 // If the TRB Ring memory cross 64K-byt= e boundary, then restart the +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 // search starting at current memory un= it. +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 // Doing so is to meet the TRB Ring bou= ndary requirement in XHCI spec. +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 // +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Available =3D 0; +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 StartByte =3D Byte; +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 StartBit=C2=A0 =3D Bit; +=C2=A0 =C2=A0 =C2=A0 =C2=A0 } +=C2=A0 =C2=A0 =C2=A0 } + Available++; if (Available >=3D Units) { @@ -438,8 +457,9 @@ UsbHcFreeMemPool ( Allocate some memory from the host controller's memory pool which can be used to communicate with host controller. -=C2=A0 @param=C2=A0 Pool=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0The host = controller's memory pool. -=C2=A0 @param=C2=A0 Size=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Size of t= he memory to allocate. +=C2=A0 @param=C2=A0 Pool=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0The host controller's memory pool. +=C2=A0 @param=C2=A0 Size=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0Size of the memory to allocate. +=C2=A0 @param=C2=A0 AllocationForRing=C2=A0 =C2=A0 The allocated memory is= for Ring or not. @return The allocated memory or NULL. @@ -447,7 +467,8 @@ UsbHcFreeMemPool ( VOID * UsbHcAllocateMem ( IN=C2=A0 USBHC_MEM_POOL=C2=A0 *Pool, -=C2=A0 IN=C2=A0 UINTN=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Size +=C2=A0 IN=C2=A0 UINTN=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Size, +=C2=A0 IN=C2=A0 BOOLEAN=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0AllocationForRing ) { USBHC_MEM_BLOCK=C2=A0 *Head; @@ -466,7 +487,7 @@ UsbHcAllocateMem ( // First check whether current memory blocks can satisfy the allocation. // for (Block =3D Head; Block !=3D NULL; Block =3D Block->Next) { -=C2=A0 =C2=A0 Mem =3D UsbHcAllocMemFromBlock (Block, AllocSize / USBHC_MEM= _UNIT); +=C2=A0 =C2=A0 Mem =3D UsbHcAllocMemFromBlock (Block, AllocSize / USBHC_MEM= _UNIT, AllocationForRing); if (Mem !=3D NULL) { ZeroMem (Mem, Size); @@ -501,7 +522,7 @@ UsbHcAllocateMem ( // Add the new memory block to the pool, then allocate memory from it // UsbHcInsertMemBlockToPool (Head, NewBlock); -=C2=A0 Mem =3D UsbHcAllocMemFromBlock (NewBlock, AllocSize / USBHC_MEM_UNI= T); +=C2=A0 Mem =3D UsbHcAllocMemFromBlock (NewBlock, AllocSize / USBHC_MEM_UNI= T, AllocationForRing); if (Mem !=3D NULL) { ZeroMem (Mem, Size); diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.h b/MdeModulePkg/Bus/Pci= /XhciDxe/UsbHcMem.h index 48ae86141c..dfea0e52b3 100644 --- a/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.h +++ b/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.h @@ -48,6 +48,8 @@ typedef struct _USBHC_MEM_POOL { #define USBHC_MEM_ROUND(Len)=C2=A0 (((Len) + USBHC_MEM_UNIT_MASK) & (~USBHC= _MEM_UNIT_MASK)) +#define USBHC_MEM_TRB_RINGS_BOUNDARY=C2=A0 =C2=A0 SIZE_64KB + // // Advance the byte and bit to the next bit, adjust byte accordingly. // @@ -92,8 +94,9 @@ UsbHcFreeMemPool ( Allocate some memory from the host controller's memory pool which can be used to communicate with host controller. -=C2=A0 @param=C2=A0 Pool=C2=A0 The host controller's memory pool. -=C2=A0 @param=C2=A0 Size=C2=A0 Size of the memory to allocate. +=C2=A0 @param=C2=A0 Pool=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0The host controller's memory pool. +=C2=A0 @param=C2=A0 Size=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0Size of the memory to allocate. +=C2=A0 @param=C2=A0 AllocationForRing=C2=A0 =C2=A0 The allocated memory is= for Ring or not. @return The allocated memory or NULL. @@ -101,7 +104,8 @@ UsbHcFreeMemPool ( VOID * UsbHcAllocateMem ( IN=C2=A0 USBHC_MEM_POOL=C2=A0 *Pool, -=C2=A0 IN=C2=A0 UINTN=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Size +=C2=A0 IN=C2=A0 UINTN=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Size, +=C2=A0 IN=C2=A0 BOOLEAN=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0AllocationForRing ); /** diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c b/MdeModulePkg/Bus/Pc= i/XhciDxe/XhciSched.c index 4ae0297607..13b0400e83 100644 --- a/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c +++ b/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c @@ -506,7 +506,7 @@ XhcInitSched ( // Software shall set Device Context Base Address Array entries for unalloc= ated Device Slots to '0'. // Entries =3D (Xhc->MaxSlotsEn + 1) * sizeof (UINT64); -=C2=A0 Dcbaa=C2=A0 =C2=A0=3D UsbHcAllocateMem (Xhc->MemPool, Entries); +=C2=A0 Dcbaa=C2=A0 =C2=A0=3D UsbHcAllocateMem (Xhc->MemPool, Entries, FALS= E); ASSERT (Dcbaa !=3D NULL); ZeroMem (Dcbaa, Entries); @@ -795,7 +795,7 @@ CreateEventRing ( ASSERT (EventRing !=3D NULL); Size =3D sizeof (TRB_TEMPLATE) * EVENT_RING_TRB_NUMBER; -=C2=A0 Buf=C2=A0 =3D UsbHcAllocateMem (Xhc->MemPool, Size); +=C2=A0 Buf=C2=A0 =3D UsbHcAllocateMem (Xhc->MemPool, Size, TRUE); ASSERT (Buf !=3D NULL); ASSERT (((UINTN)Buf & 0x3F) =3D=3D 0); ZeroMem (Buf, Size); @@ -814,7 +814,7 @@ CreateEventRing ( EventRing->EventRingCCS =3D 1; Size =3D sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBER; -=C2=A0 Buf=C2=A0 =3D UsbHcAllocateMem (Xhc->MemPool, Size); +=C2=A0 Buf=C2=A0 =3D UsbHcAllocateMem (Xhc->MemPool, Size, FALSE); ASSERT (Buf !=3D NULL); ASSERT (((UINTN)Buf & 0x3F) =3D=3D 0); ZeroMem (Buf, Size); @@ -892,7 +892,7 @@ CreateTransferRing ( LINK_TRB=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 *EndTrb; EFI_PHYSICAL_ADDRESS=C2=A0 PhyAddr; -=C2=A0 Buf =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (TRB_TEMPLATE) * Trb= Num); +=C2=A0 Buf =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (TRB_TEMPLATE) * Trb= Num, TRUE); ASSERT (Buf !=3D NULL); ASSERT (((UINTN)Buf & 0x3F) =3D=3D 0); ZeroMem (Buf, sizeof (TRB_TEMPLATE) * TrbNum); @@ -2182,7 +2182,7 @@ XhcInitializeDeviceSlot ( // 4.3.3 Device Slot Initialization // 1) Allocate an Input Context data structure (6.2.5) and initialize all f= ields to '0'. // -=C2=A0 InputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (INPUT_CONT= EXT)); +=C2=A0 InputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (INPUT_CONT= EXT), FALSE); ASSERT (InputContext !=3D NULL); ASSERT (((UINTN)InputContext & 0x3F) =3D=3D 0); ZeroMem (InputContext, sizeof (INPUT_CONTEXT)); @@ -2284,7 +2284,7 @@ XhcInitializeDeviceSlot ( // // 6) Allocate the Output Device Context data structure (6.2.1) and initial= ize it to '0'. // -=C2=A0 OutputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (DEVICE_CO= NTEXT)); +=C2=A0 OutputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (DEVICE_CO= NTEXT), FALSE); ASSERT (OutputContext !=3D NULL); ASSERT (((UINTN)OutputContext & 0x3F) =3D=3D 0); ZeroMem (OutputContext, sizeof (DEVICE_CONTEXT)); @@ -2398,7 +2398,7 @@ XhcInitializeDeviceSlot64 ( // 4.3.3 Device Slot Initialization // 1) Allocate an Input Context data structure (6.2.5) and initialize all f= ields to '0'. // -=C2=A0 InputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (INPUT_CONT= EXT_64)); +=C2=A0 InputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (INPUT_CONT= EXT_64), FALSE); ASSERT (InputContext !=3D NULL); ASSERT (((UINTN)InputContext & 0x3F) =3D=3D 0); ZeroMem (InputContext, sizeof (INPUT_CONTEXT_64)); @@ -2500,7 +2500,7 @@ XhcInitializeDeviceSlot64 ( // // 6) Allocate the Output Device Context data structure (6.2.1) and initial= ize it to '0'. // -=C2=A0 OutputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (DEVICE_CO= NTEXT_64)); +=C2=A0 OutputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (DEVICE_CO= NTEXT_64), FALSE); ASSERT (OutputContext !=3D NULL); ASSERT (((UINTN)OutputContext & 0x3F) =3D=3D 0); ZeroMem (OutputContext, sizeof (DEVICE_CONTEXT_64)); -- 2.20.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#95294): https://edk2.groups.io/g/devel/message/95294 Mute This Topic: https://groups.io/mt/94385000/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- --yTUoIrJf7fCOuOveMXqr Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable
 
According the Xhci Spec, TRB Rings may be larger than a Page, however = they shall not cross a 64K byte boundary, so add a parameter to indicate wh= ether the memory allocation is for TRB or not. It will ensure the allocatio= n not crossing 64K boundary in UsbHcAllocMemFromBlock if the memory is allo= cated for TRB.
 
Signed-off-by: jdzhang <jdzhang@kunluntech.com.cn>
---
 MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c  | 37 +++++++++++++= ++++++-----
 MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.h  | 10 +++++--
 MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c | 16 +++++-----
 3 files changed, 44 insertions(+), 19 deletions(-)
 
diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c b/MdeModulePkg/Bu= s/Pci/XhciDxe/UsbHcMem.c
index 99fb3521d5..24714502cd 100644
--- a/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c
+++ b/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c
@@ -132,8 +132,9 @@ UsbHcFreeMemBlock (
 /**
   Alloc some memory from the block.
 
-  @param  Block          The memor= y block to allocate memory from.
-  @param  Units          Number of= memory units to allocate.
+  @param  Block            &n= bsp;   The memory block to allocate memory from.
+  @param  Units            &n= bsp;   Number of memory units to allocate.
+  @param  AllocationForRing    The allocated memo= ry is for Ring or not.
 
   @return The pointer to the allocated memory. If couldn't = allocate the needed memory,
           the return value is NULL.
@@ -142,7 +143,8 @@ UsbHcFreeMemBlock (
 VOID *
 UsbHcAllocMemFromBlock (
   IN  USBHC_MEM_BLOCK  *Block,
-  IN  UINTN            Units<= /div>
+  IN  UINTN            Units,=
+  IN  BOOLEAN          AllocationF= orTrbRing
   )
 {
   UINTN  Byte;
@@ -151,12 +153,15 @@ UsbHcAllocMemFromBlock (
   UINT8  StartBit;
   UINTN  Available;
   UINTN  Count;
+  UINTN  MemUnitAddr;
+  UINTN  AlignmentMask;
 
   ASSERT ((Block !=3D 0) && (Units !=3D 0));
 
   StartByte =3D 0;
   StartBit  =3D 0;
   Available =3D 0;
+  AlignmentMask =3D ~((UINTN)USBHC_MEM_TRB_RINGS_BOUNDARY - 1);<= /div>
 
   for (Byte =3D 0, Bit =3D 0; Byte < Block->BitsLen;)= {
     //
@@ -165,6 +170,20 @@ UsbHcAllocMemFromBlock (
     // Available counts the consective number of zero = bit.
     //
     if (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit)= ) {
+      if (AllocationForTrbRing && (Available != =3D 0)) {
+        MemUnitAddr =3D (UINTN)Block->BufHost = + (Byte * 8 + Bit) * USBHC_MEM_UNIT;
+        if ((MemUnitAddr & AlignmentMask) != =3D ((MemUnitAddr - USBHC_MEM_UNIT) & AlignmentMask)) {
+          //
+          // If the TRB Ring memory cross 64= K-byte boundary, then restart the
+          // search starting at current memo= ry unit.
+          // Doing so is to meet the TRB Rin= g boundary requirement in XHCI spec. 
+          //
+          Available =3D 0;
+          StartByte =3D Byte;
+          StartBit  =3D Bit;
+        }
+      }
+
       Available++;
 
       if (Available >=3D Units) {
@@ -438,8 +457,9 @@ UsbHcFreeMemPool (
   Allocate some memory from the host controller's memory po= ol
   which can be used to communicate with host controller.
 
-  @param  Pool           The = host controller's memory pool.
-  @param  Size           Size= of the memory to allocate.
+  @param  Pool            &nb= sp;    The host controller's memory pool.
+  @param  Size            &nb= sp;    Size of the memory to allocate.
+  @param  AllocationForRing    The allocated memo= ry is for Ring or not.
 
   @return The allocated memory or NULL.
 
@@ -447,7 +467,8 @@ UsbHcFreeMemPool (
 VOID *
 UsbHcAllocateMem (
   IN  USBHC_MEM_POOL  *Pool,
-  IN  UINTN           Size
+  IN  UINTN           Size,
+  IN  BOOLEAN         AllocationFo= rRing
   )
 {
   USBHC_MEM_BLOCK  *Head;
@@ -466,7 +487,7 @@ UsbHcAllocateMem (
   // First check whether current memory blocks can satisfy = the allocation.
   //
   for (Block =3D Head; Block !=3D NULL; Block =3D Block->= ;Next) {
-    Mem =3D UsbHcAllocMemFromBlock (Block, AllocSize / USBH= C_MEM_UNIT);
+    Mem =3D UsbHcAllocMemFromBlock (Block, AllocSize / USBH= C_MEM_UNIT, AllocationForRing);
 
     if (Mem !=3D NULL) {
       ZeroMem (Mem, Size);
@@ -501,7 +522,7 @@ UsbHcAllocateMem (
   // Add the new memory block to the pool, then allocate me= mory from it
   //
   UsbHcInsertMemBlockToPool (Head, NewBlock);
-  Mem =3D UsbHcAllocMemFromBlock (NewBlock, AllocSize / USBHC_ME= M_UNIT);
+  Mem =3D UsbHcAllocMemFromBlock (NewBlock, AllocSize / USBHC_ME= M_UNIT, AllocationForRing);
 
   if (Mem !=3D NULL) {
     ZeroMem (Mem, Size);
diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.h b/MdeModulePkg/Bu= s/Pci/XhciDxe/UsbHcMem.h
index 48ae86141c..dfea0e52b3 100644
--- a/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.h
+++ b/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.h
@@ -48,6 +48,8 @@ typedef struct _USBHC_MEM_POOL {
 
 #define USBHC_MEM_ROUND(Len)  (((Len) + USBHC_MEM_UNIT_MASK= ) & (~USBHC_MEM_UNIT_MASK))
 
+#define USBHC_MEM_TRB_RINGS_BOUNDARY    SIZE_64KB
+
 //
 // Advance the byte and bit to the next bit, adjust byte accordi= ngly.
 //
@@ -92,8 +94,9 @@ UsbHcFreeMemPool (
   Allocate some memory from the host controller's memory po= ol
   which can be used to communicate with host controller.
 
-  @param  Pool  The host controller's memory pool.
-  @param  Size  Size of the memory to allocate.
+  @param  Pool            &nb= sp;    The host controller's memory pool.
+  @param  Size            &nb= sp;    Size of the memory to allocate.
+  @param  AllocationForRing    The allocated memo= ry is for Ring or not.
 
   @return The allocated memory or NULL.
 
@@ -101,7 +104,8 @@ UsbHcFreeMemPool (
 VOID *
 UsbHcAllocateMem (
   IN  USBHC_MEM_POOL  *Pool,
-  IN  UINTN           Size
+  IN  UINTN           Size,
+  IN  BOOLEAN         AllocationFo= rRing
   );
 
 /**
diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c b/MdeModulePkg/B= us/Pci/XhciDxe/XhciSched.c
index 4ae0297607..13b0400e83 100644
--- a/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c
+++ b/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c
@@ -506,7 +506,7 @@ XhcInitSched (
   // Software shall set Device Context Base Address Array e= ntries for unallocated Device Slots to '0'.
   //
   Entries =3D (Xhc->MaxSlotsEn + 1) * sizeof (UINT64);
-  Dcbaa   =3D UsbHcAllocateMem (Xhc->MemPool, Entri= es);
+  Dcbaa   =3D UsbHcAllocateMem (Xhc->MemPool, Entri= es, FALSE);
   ASSERT (Dcbaa !=3D NULL);
   ZeroMem (Dcbaa, Entries);
 
@@ -795,7 +795,7 @@ CreateEventRing (
   ASSERT (EventRing !=3D NULL);
 
   Size =3D sizeof (TRB_TEMPLATE) * EVENT_RING_TRB_NUMBER;
-  Buf  =3D UsbHcAllocateMem (Xhc->MemPool, Size);
+  Buf  =3D UsbHcAllocateMem (Xhc->MemPool, Size, TRUE);<= /div>
   ASSERT (Buf !=3D NULL);
   ASSERT (((UINTN)Buf & 0x3F) =3D=3D 0);
   ZeroMem (Buf, Size);
@@ -814,7 +814,7 @@ CreateEventRing (
   EventRing->EventRingCCS =3D 1;
 
   Size =3D sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBE= R;
-  Buf  =3D UsbHcAllocateMem (Xhc->MemPool, Size);
+  Buf  =3D UsbHcAllocateMem (Xhc->MemPool, Size, FALSE);=
   ASSERT (Buf !=3D NULL);
   ASSERT (((UINTN)Buf & 0x3F) =3D=3D 0);
   ZeroMem (Buf, Size);
@@ -892,7 +892,7 @@ CreateTransferRing (
   LINK_TRB              = *EndTrb;
   EFI_PHYSICAL_ADDRESS  PhyAddr;
 
-  Buf =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (TRB_TEMPLAT= E) * TrbNum);
+  Buf =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (TRB_TEMPLAT= E) * TrbNum, TRUE);
   ASSERT (Buf !=3D NULL);
   ASSERT (((UINTN)Buf & 0x3F) =3D=3D 0);
   ZeroMem (Buf, sizeof (TRB_TEMPLATE) * TrbNum);
@@ -2182,7 +2182,7 @@ XhcInitializeDeviceSlot (
   // 4.3.3 Device Slot Initialization
   // 1) Allocate an Input Context data structure (6.2.5) an= d initialize all fields to '0'.
   //
-  InputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (IN= PUT_CONTEXT));
+  InputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (IN= PUT_CONTEXT), FALSE);
   ASSERT (InputContext !=3D NULL);
   ASSERT (((UINTN)InputContext & 0x3F) =3D=3D 0);
   ZeroMem (InputContext, sizeof (INPUT_CONTEXT));
@@ -2284,7 +2284,7 @@ XhcInitializeDeviceSlot (
   //
   // 6) Allocate the Output Device Context data structure (= 6.2.1) and initialize it to '0'.
   //
-  OutputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (D= EVICE_CONTEXT));
+  OutputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (D= EVICE_CONTEXT), FALSE);
   ASSERT (OutputContext !=3D NULL);
   ASSERT (((UINTN)OutputContext & 0x3F) =3D=3D 0);
   ZeroMem (OutputContext, sizeof (DEVICE_CONTEXT));
@@ -2398,7 +2398,7 @@ XhcInitializeDeviceSlot64 (
   // 4.3.3 Device Slot Initialization
   // 1) Allocate an Input Context data structure (6.2.5) an= d initialize all fields to '0'.
   //
-  InputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (IN= PUT_CONTEXT_64));
+  InputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (IN= PUT_CONTEXT_64), FALSE);
   ASSERT (InputContext !=3D NULL);
   ASSERT (((UINTN)InputContext & 0x3F) =3D=3D 0);
   ZeroMem (InputContext, sizeof (INPUT_CONTEXT_64));
@@ -2500,7 +2500,7 @@ XhcInitializeDeviceSlot64 (
   //
   // 6) Allocate the Output Device Context data structure (= 6.2.1) and initialize it to '0'.
   //
-  OutputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (D= EVICE_CONTEXT_64));
+  OutputContext =3D UsbHcAllocateMem (Xhc->MemPool, sizeof (D= EVICE_CONTEXT_64), FALSE);
   ASSERT (OutputContext !=3D NULL);
   ASSERT (((UINTN)OutputContext & 0x3F) =3D=3D 0);
   ZeroMem (OutputContext, sizeof (DEVICE_CONTEXT_64));
-- 
2.20.1.windows.1
 
_._,_._,_

G= roups.io Links:

You receive all messages sent to this group.

Vi= ew/Reply Online (#95294) | | Mute This Topic | New Topic
Your Subscription | Contact Group Owner | Unsubscribe [importer@patchew.org]
_._,_._,_

=20 --yTUoIrJf7fCOuOveMXqr--