From nobody Tue Feb 10 11:34:43 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 152078139228063.70759995763399; Sun, 11 Mar 2018 08:16:32 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 3000D22603AEA; Sun, 11 Mar 2018 08:10:10 -0700 (PDT) Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 791292258AF0A for ; Sun, 11 Mar 2018 08:10:06 -0700 (PDT) Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 11 Mar 2018 08:16:26 -0700 Received: from lzeng14-mobl2.ccr.corp.intel.com ([10.255.31.175]) by fmsmga006.fm.intel.com with ESMTP; 11 Mar 2018 08:16:24 -0700 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.151; helo=mga17.intel.com; envelope-from=star.zeng@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.47,455,1515484800"; d="scan'208";a="210652160" From: Star Zeng To: edk2-devel@lists.01.org Date: Sun, 11 Mar 2018 23:16:18 +0800 Message-Id: <20180311151619.1260-3-star.zeng@intel.com> X-Mailer: git-send-email 2.13.3.windows.1 In-Reply-To: <20180311151619.1260-1-star.zeng@intel.com> References: <20180311151619.1260-1-star.zeng@intel.com> Subject: [edk2] [PATCH 2/3] SourceLevelDebugPkg DebugCommUsb3: Realloc granted DXE DMA buffer X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ruiyu Ni , Hao Wu , Jiewen Yao , Star Zeng MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" For the case that the USB3 debug port instance and DMA buffers are from PEI HOB with IOMMU enabled, de8373fa07f8 is to reallocate the DMA buffers by AllocateAddress with the memory type accessible by SMM environment. But reallocating the DMA buffers by AllocateAddress may fail. Instead, this patch is to reinitialize USB3 debug port with granted DXE DMA buffer accessible by SMM environment. Cc: Jiewen Yao Cc: Ruiyu Ni Cc: Hao Wu Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Star Zeng --- .../DebugCommunicationLibUsb3Dxe.c | 246 +++++++++++++----= ---- .../DebugCommunicationLibUsb3Dxe.inf | 3 + .../DebugCommunicationLibUsb3Pei.c | 2 +- 3 files changed, 155 insertions(+), 96 deletions(-) diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCom= municationLibUsb3Dxe.c b/SourceLevelDebugPkg/Library/DebugCommunicationLibU= sb3/DebugCommunicationLibUsb3Dxe.c index 29cec56f39dc..588c9715bd00 100644 --- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicat= ionLibUsb3Dxe.c +++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicat= ionLibUsb3Dxe.c @@ -18,11 +18,13 @@ #include #include #include +#include #include "DebugCommunicationLibUsb3Internal.h" =20 GUID gUsb3DbgGuid =3D USB3_DBG_GUID; =20 USB3_DEBUG_PORT_HANDLE *mUsb3Instance =3D NULL; +EFI_PCI_IO_PROTOCOL *mUsb3PciIo =3D NULL; =20 /** Creates a named event that can be signaled. @@ -85,20 +87,16 @@ Usb3NamedEventListen ( /** USB3 map one DMA buffer. =20 - @param Instance Pointer to USB3 debug port instance. @param PciIo Pointer to PciIo for USB3 debug port. @param Address DMA buffer address to be mapped. @param NumberOfBytes Number of bytes to be mapped. - @param BackupBuffer Backup buffer address. =20 **/ VOID Usb3MapOneDmaBuffer ( - IN USB3_DEBUG_PORT_HANDLE *Instance, IN EFI_PCI_IO_PROTOCOL *PciIo, IN EFI_PHYSICAL_ADDRESS Address, - IN UINTN NumberOfBytes, - IN EFI_PHYSICAL_ADDRESS BackupBuffer + IN UINTN NumberOfBytes ) { EFI_STATUS Status; @@ -117,23 +115,6 @@ Usb3MapOneDmaBuffer ( ); ASSERT_EFI_ERROR (Status); ASSERT (DeviceAddress =3D=3D ((EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress= )); - if (Instance->FromHob) { - // - // Reallocate the DMA buffer by AllocateAddress with - // the memory type accessible by SMM. - // - CopyMem ((VOID *) (UINTN) BackupBuffer, (VOID *) (UINTN) Address, Numb= erOfBytes); - Status =3D gBS->FreePages (Address, EFI_SIZE_TO_PAGES (NumberOfBytes)); - ASSERT_EFI_ERROR (Status); - Status =3D gBS->AllocatePages ( - AllocateAddress, - EfiACPIMemoryNVS, - EFI_SIZE_TO_PAGES (NumberOfBytes), - &Address - ); - ASSERT_EFI_ERROR (Status); - CopyMem ((VOID *) (UINTN) Address, (VOID *) (UINTN) BackupBuffer, Numb= erOfBytes); - } } =20 /** @@ -149,99 +130,111 @@ Usb3MapDmaBuffers ( IN EFI_PCI_IO_PROTOCOL *PciIo ) { - EFI_STATUS Status; - EDKII_IOMMU_PROTOCOL *IoMmu; - EFI_PHYSICAL_ADDRESS BackupBuffer; - UINTN BackupBufferSize; - - IoMmu =3D NULL; - Status =3D gBS->LocateProtocol (&gEdkiiIoMmuProtocolGuid, NULL, (VOID **= ) &IoMmu); - if (EFI_ERROR (Status) || (IoMmu =3D=3D NULL)) { - // - // No need to map the DMA buffers. - // - return; - } - - // - // Allocate backup buffer for the case that the USB3 - // debug port instance and DMA buffers are from PEI HOB. - // For this case, the DMA buffers need to be reallocated - // by AllocateAddress with the memory type accessible by - // SMM. - // - BackupBufferSize =3D MAX (XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE * 2 + USB3_D= EBUG_PORT_WRITE_MAX_PACKET_SIZE, - MAX (sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER, - MAX (sizeof (TRB_TEMPLATE) * EVENT_RING_TRB= _NUMBER, - MAX (sizeof (EVENT_RING_SEG_TABLE_ENTR= Y) * ERST_NUMBER, - MAX (sizeof (XHC_DC_CONTEXT), - STRING0_DESC_LEN + MANU_DESC= _LEN + PRODUCT_DESC_LEN + SERIAL_DESC_LEN))))); - - Status =3D gBS->AllocatePages ( - AllocateAnyPages, - EfiBootServicesData, - EFI_SIZE_TO_PAGES (BackupBufferSize), - &BackupBuffer - ); - ASSERT_EFI_ERROR (Status); - Usb3MapOneDmaBuffer ( - Instance, PciIo, Instance->UrbIn.Data, - XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE * 2 + USB3_DEBUG_PORT_WRITE_MAX_PACK= ET_SIZE, - BackupBuffer + XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE * 2 + USB3_DEBUG_PORT_WRITE_MAX_PACK= ET_SIZE ); =20 Usb3MapOneDmaBuffer ( - Instance, PciIo, Instance->TransferRingIn.RingSeg0, - sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER, - BackupBuffer + sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER ); =20 Usb3MapOneDmaBuffer ( - Instance, PciIo, Instance->TransferRingOut.RingSeg0, - sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER, - BackupBuffer + sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER ); =20 Usb3MapOneDmaBuffer ( - Instance, PciIo, Instance->EventRing.EventRingSeg0, - sizeof (TRB_TEMPLATE) * EVENT_RING_TRB_NUMBER, - BackupBuffer + sizeof (TRB_TEMPLATE) * EVENT_RING_TRB_NUMBER ); =20 Usb3MapOneDmaBuffer ( - Instance, PciIo, Instance->EventRing.ERSTBase, - sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBER, - BackupBuffer + sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBER ); =20 Usb3MapOneDmaBuffer ( - Instance, PciIo, Instance->DebugCapabilityContext, - sizeof (XHC_DC_CONTEXT), - BackupBuffer + sizeof (XHC_DC_CONTEXT) ); =20 Usb3MapOneDmaBuffer ( - Instance, PciIo, ((XHC_DC_CONTEXT *) (UINTN) Instance->DebugCapabilityContext)->DbcInfo= Context.String0DescAddress, - STRING0_DESC_LEN + MANU_DESC_LEN + PRODUCT_DESC_LEN + SERIAL_DESC_LEN, - BackupBuffer + STRING0_DESC_LEN + MANU_DESC_LEN + PRODUCT_DESC_LEN + SERIAL_DESC_LEN ); +} + +/** + Invoke a notification event =20 - gBS->FreePages (BackupBuffer, EFI_SIZE_TO_PAGES (BackupBufferSize)); + @param[in] Event Event whose notification function is being= invoked. + @param[in] Context The pointer to the notification function's= context, + which is implementation-dependent. + +**/ +VOID +EFIAPI +Usb3DxeSmmReadyToLockNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + ASSERT (mUsb3Instance !=3D NULL); + + DEBUG ((DEBUG_INFO, "%a()\n", __FUNCTION__)); + mUsb3Instance->InNotify =3D TRUE; + + // + // For the case that the USB3 debug port instance and DMA buffers are + // from PEI HOB with IOMMU enabled. + // Reinitialize USB3 debug port with granted DXE DMA buffer accessible + // by SMM environment. + // + InitializeUsbDebugHardware (mUsb3Instance); + + // + // Wait some time for host to be ready after re-initialization. + // + MicroSecondDelay (1000000); + + mUsb3Instance->InNotify =3D FALSE; + gBS->CloseEvent (Event); +} + +/** + USB3 get IOMMU protocol. + + @return Pointer to IOMMU protocol. + +**/ +EDKII_IOMMU_PROTOCOL * +Usb3GetIoMmu ( + VOID + ) +{ + EFI_STATUS Status; + EDKII_IOMMU_PROTOCOL *IoMmu; + + IoMmu =3D NULL; + Status =3D gBS->LocateProtocol ( + &gEdkiiIoMmuProtocolGuid, + NULL, + (VOID **) &IoMmu + ); + if (!EFI_ERROR (Status) && (IoMmu !=3D NULL)) { + return IoMmu; + } + + return NULL; } =20 /** @@ -270,8 +263,6 @@ Usb3PciIoNotify ( UINTN PciFunctionNumber; UINT32 PciAddress; =20 - ASSERT (mUsb3Instance !=3D NULL); - Status =3D gBS->LocateHandleBuffer ( ByProtocol, &gEfiPciIoProtocolGuid, @@ -296,10 +287,23 @@ Usb3PciIoNotify ( // // Found the PciIo for USB3 debug port. // + ASSERT (mUsb3Instance !=3D NULL); DEBUG ((DEBUG_INFO, "%a()\n", __FUNCTION__)); - mUsb3Instance->InNotify =3D TRUE; - Usb3MapDmaBuffers (mUsb3Instance, PciIo); - mUsb3Instance->InNotify =3D FALSE; + if (Usb3GetIoMmu !=3D NULL) { + mUsb3Instance->InNotify =3D TRUE; + Usb3MapDmaBuffers (mUsb3Instance, PciIo); + mUsb3Instance->InNotify =3D FALSE; + + if (mUsb3Instance->FromHob) { + mUsb3PciIo =3D PciIo; + Usb3NamedEventListen ( + &gEfiDxeSmmReadyToLockProtocolGuid, + TPL_NOTIFY, + Usb3DxeSmmReadyToLockNotify, + &Event + ); + } + } gBS->CloseEvent ((EFI_EVENT) (UINTN) mUsb3Instance->PciIoEvent); break; } @@ -334,13 +338,57 @@ GetUsb3DebugPortInstance ( } =20 Done: - if (Instance !=3D NULL) { + if ((Instance !=3D NULL) && (!Instance->InNotify)) { DiscoverInitializeUsbDebugPort (Instance); } return Instance; } =20 /** + Allocates pages that are suitable for an OperationBusMasterCommonBuffer = or + OperationBusMasterCommonBuffer64 mapping. + + @param PciIo Pointer to PciIo for USB3 debug port. + @param Pages The number of pages to allocate. + @param Address A pointer to store the base system memory = address of the + allocated range. + + @retval EFI_SUCCESS The requested memory pages were allocated. + @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal = attribute bits are + MEMORY_WRITE_COMBINE and MEMORY_CACHED. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated. + +**/ +EFI_STATUS +Usb3AllocateDmaBuffer ( + IN EFI_PCI_IO_PROTOCOL *PciIo, + IN UINTN Pages, + OUT VOID **Address + ) +{ + EFI_STATUS Status; + + *Address =3D NULL; + Status =3D PciIo->AllocateBuffer ( + PciIo, + AllocateAnyPages, + EfiRuntimeServicesData, + Pages, + Address, + 0 + ); + if (!EFI_ERROR (Status)) { + Usb3MapOneDmaBuffer ( + PciIo, + (EFI_PHYSICAL_ADDRESS) (UINTN) *Address, + EFI_PAGES_TO_SIZE (Pages) + ); + } + return Status; +} + +/** Allocate aligned memory for XHC's usage. =20 @param BufferSize The size, in bytes, of the Buffer. @@ -360,15 +408,23 @@ AllocateAlignBuffer ( Buf =3D NULL; =20 if (gBS !=3D NULL) { - TmpAddr =3D 0xFFFFFFFF; - Status =3D gBS->AllocatePages ( - AllocateMaxAddress, - EfiACPIMemoryNVS, - EFI_SIZE_TO_PAGES (BufferSize), - &TmpAddr - ); - if (!EFI_ERROR (Status)) { - Buf =3D (VOID *) (UINTN) TmpAddr; + if (mUsb3PciIo !=3D NULL) { + Usb3AllocateDmaBuffer ( + mUsb3PciIo, + EFI_SIZE_TO_PAGES (BufferSize), + &Buf + ); + } else { + TmpAddr =3D 0xFFFFFFFF; + Status =3D gBS->AllocatePages ( + AllocateMaxAddress, + EfiACPIMemoryNVS, + EFI_SIZE_TO_PAGES (BufferSize), + &TmpAddr + ); + if (!EFI_ERROR (Status)) { + Buf =3D (VOID *) (UINTN) TmpAddr; + } } } =20 diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCom= municationLibUsb3Dxe.inf b/SourceLevelDebugPkg/Library/DebugCommunicationLi= bUsb3/DebugCommunicationLibUsb3Dxe.inf index 3af7e7180d37..9f737b9478a8 100644 --- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicat= ionLibUsb3Dxe.inf +++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicat= ionLibUsb3Dxe.inf @@ -63,6 +63,9 @@ [Protocols] ## SOMETIMES_CONSUMES gEfiPciIoProtocolGuid gEdkiiIoMmuProtocolGuid ## SOMETIMES_CONSUMES + ## NOTIFY + ## SOMETIMES_CONSUMES + gEfiDxeSmmReadyToLockProtocolGuid =20 [LibraryClasses] BaseLib diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCom= municationLibUsb3Pei.c b/SourceLevelDebugPkg/Library/DebugCommunicationLibU= sb3/DebugCommunicationLibUsb3Pei.c index 40e9b6406b2e..145113d28dc3 100644 --- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicat= ionLibUsb3Pei.c +++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicat= ionLibUsb3Pei.c @@ -106,7 +106,7 @@ IoMmuAllocateBuffer ( =20 Status =3D IoMmu->AllocateBuffer ( IoMmu, - EfiBootServicesData, + EfiRuntimeServicesData, Pages, HostAddress, 0 --=20 2.13.3.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel