From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 5324594153B for ; Thu, 7 Sep 2023 07:36:39 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=0aAJvh+pLCF5NYACTVkp2ZEv5CGN3yz5/fsFDtCiiBw=; c=relaxed/simple; d=groups.io; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject:To:Cc:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Type:Content-Transfer-Encoding; s=20140610; t=1694072197; v=1; b=k7kcdq7HXNX8nUOjyCr8oGbOQSGTcobbTdeeKiL2busIIQU82uK71Xo6eDxjLMhKnvwByPhi zGqsHaWRN0BFTZU/3rmG12Orva+vrHXQPI8zhHIFsVkRG46sOOIFXhIjFC/aElHhA3cN1Y1vUoi sdKIBPu3xH+Ff7O1V4aKXiVM= X-Received: by 127.0.0.2 with SMTP id eNexYY7687511xUvOPWpKJHr; Thu, 07 Sep 2023 00:36:37 -0700 X-Received: from mail-yb1-f176.google.com (mail-yb1-f176.google.com [209.85.219.176]) by mx.groups.io with SMTP id smtpd.web11.8048.1694072197182855061 for ; Thu, 07 Sep 2023 00:36:37 -0700 X-Received: by mail-yb1-f176.google.com with SMTP id 3f1490d57ef6-d7eccc1b8c6so636851276.0 for ; Thu, 07 Sep 2023 00:36:37 -0700 (PDT) X-Gm-Message-State: Eu8QkP6vXG8oC02VBTEYb4Igx7686176AA= X-Google-Smtp-Source: AGHT+IEw462FC/mSHn+jvGVTYUH8CGCixahg4xUPbvYKJP9vQqFmTwTBTaQy3QtzEg+QKP6cWIoo4RBNLkPfYec8jJ0= X-Received: by 2002:a25:da0a:0:b0:d78:f1d:2e23 with SMTP id n10-20020a25da0a000000b00d780f1d2e23mr21743844ybf.53.1694072196155; Thu, 07 Sep 2023 00:36:36 -0700 (PDT) MIME-Version: 1.0 References: <177B4AD1F8C3A6A2.8497@groups.io> In-Reply-To: From: "Mike Maslenkin" Date: Thu, 7 Sep 2023 10:36:00 +0300 Message-ID: Subject: Re: [edk2-devel] [PATCH v2] MdeModulePkg/XhciDxe: Use Performance Timer for XHCI Timeouts To: devel@edk2.groups.io, hao.a.wu@intel.com Cc: "Henz, Patrick" Precedence: Bulk 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,mike.maslenkin@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=k7kcdq7H; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io Hello Hao Wu, Isn't GetPerformanceCounterProperties (&StartValue, &EndValue) call too "heavy" for XHCI paths? May be it's better to cache timer direction once? I'm asking in a context of PcAtChipsetPkg/Library/AcpiTimerLib instance used by a number of Intel's platform in edk-platforms. For this library GetPerformanceCounterProperties finally calls InternalCalculateTscFrequency, that isn't simple. Regards, Mike On Thu, Sep 7, 2023 at 4:27=E2=80=AFAM Wu, Hao A wrote= : > > Sorry for the late response. > Reviewed-by: Hao A Wu > > Best Regards, > Hao Wu > > > -----Original Message----- > > From: devel@edk2.groups.io On Behalf Of Henz, > > Patrick > > Sent: Thursday, September 7, 2023 4:37 AM > > To: devel@edk2.groups.io > > Subject: Re: [edk2-devel] [PATCH v2] MdeModulePkg/XhciDxe: Use > > Performance Timer for XHCI Timeouts > > > > I sent this patch out a few weeks ago now but haven't seen a reply, jus= t > > checking in to make sure it didn't get missed. > > > > Thanks, > > Patrick Henz > > > > -----Original Message----- > > From: devel@edk2.groups.io On Behalf Of Henz, > > Patrick > > Sent: Monday, August 14, 2023 10:52 AM > > To: devel@edk2.groups.io > > Cc: Henz, Patrick > > Subject: [edk2-devel] [PATCH v2] MdeModulePkg/XhciDxe: Use Performance > > Timer for XHCI Timeouts > > > > REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3D2948 > > > > XhciDxe uses the timer functionality provided by the boot services tabl= e to > > detect timeout conditions. This breaks the driver's ExitBootServices ca= ll back, > > as CoreExitBootServices halts the timer before signaling the ExitBootSe= rvices > > event. If the host controller fails to halt in the call back, the timeo= ut condition > > will never occur and the boot gets stuck in an indefinite spin loop. Us= e the > > free running timer provided by TimerLib to calculate timeouts, avoiding= the > > potential hang. > > > > Signed-off-by: Patrick Henz > > Reviewed-by: > > --- > > MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c | 56 +++++++++++++++++++ > > MdeModulePkg/Bus/Pci/XhciDxe/Xhci.h | 22 ++++++++ > > MdeModulePkg/Bus/Pci/XhciDxe/XhciDxe.inf | 2 + > > MdeModulePkg/Bus/Pci/XhciDxe/XhciReg.c | 67 +++++++++-------------- > > MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c | 68 +++++++++--------------- > > 5 files changed, 129 insertions(+), 86 deletions(-) > > > > diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c > > b/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c > > index 62682dd27c..1dcbe20512 100644 > > --- a/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c > > +++ b/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c > > @@ -1,6 +1,7 @@ > > /** @file > > The XHCI controller driver. > > > > +(C) Copyright 2023 Hewlett Packard Enterprise Development LP
> > Copyright (c) 2011 - 2022, Intel Corporation. All rights reserved.
> > SPDX-License-Identifier: BSD-2-Clause-Patent > > > > @@ -2294,3 +2295,58 @@ XhcDriverBindingStop ( > > > > return EFI_SUCCESS; > > } > > + > > +/** > > + Computes and returns the elapsed time in nanoseconds since > > + PreviousTick. The value of PreviousTick is overwritten with the > > + current performance counter value. > > + > > + @param PreviousTick Pointer to PreviousTick count. > > + @return The elapsed time in nanoseconds since PreviousCount. > > + PreviousCount is overwritten with the current performance > > + counter value. > > +**/ > > +UINT64 > > +XhcGetElapsedTime ( > > + IN OUT UINT64 *PreviousTick > > + ) > > +{ > > + UINT64 StartValue; > > + UINT64 EndValue; > > + UINT64 CurrentTick; > > + UINT64 Delta; > > + > > + GetPerformanceCounterProperties (&StartValue, &EndValue); > > + > > + CurrentTick =3D GetPerformanceCounter (); > > + > > + // > > + // Determine if the counter is counting up or down // if > > + (StartValue < EndValue) { > > + // > > + // Counter counts upwards, check for an overflow condition > > + // > > + if (*PreviousTick > CurrentTick) { > > + Delta =3D (EndValue - *PreviousTick) + CurrentTick; > > + } else { > > + Delta =3D CurrentTick - *PreviousTick; > > + } > > + } else { > > + // > > + // Counter counts downwards, check for an underflow condition > > + // > > + if (*PreviousTick < CurrentTick) { > > + Delta =3D (StartValue - CurrentTick) + *PreviousTick; > > + } else { > > + Delta =3D *PreviousTick - CurrentTick; > > + } > > + } > > + > > + // > > + // Set PreviousTick to CurrentTick > > + // > > + *PreviousTick =3D CurrentTick; > > + > > + return GetTimeInNanoSecond (Delta); > > +} > > diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.h > > b/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.h > > index ca223bd20c..77feb66647 100644 > > --- a/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.h > > +++ b/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.h > > @@ -2,6 +2,7 @@ > > > > Provides some data structure definitions used by the XHCI host contr= oller > > driver. > > > > +(C) Copyright 2023 Hewlett Packard Enterprise Development LP
> > Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.
> > Copyright (c) Microsoft Corporation.
> > SPDX-License-Identifier: BSD-2-Clause-Patent @@ -26,6 +27,7 @@ SPDX- > > License-Identifier: BSD-2-Clause-Patent #include = #include > > #include > > +#include > > > > #include > > > > @@ -37,6 +39,10 @@ typedef struct _USB_DEV_CONTEXT > > USB_DEV_CONTEXT; > > #include "ComponentName.h" > > #include "UsbHcMem.h" > > > > +// > > +// Converts a count from microseconds to nanoseconds // #define > > +XHC_MICROSECOND_TO_NANOSECOND(Time) ((UINT64)(Time) * 1000) > > // > > // The unit is microsecond, setting it as 1us. > > // > > @@ -720,4 +726,20 @@ XhcAsyncIsochronousTransfer ( > > IN VOID *Context > > ); > > > > +/** > > + Computes and returns the elapsed time in nanoseconds since > > + PreviousTick. The value of PreviousTick is overwritten with the > > + current performance counter value. > > + > > + @param PreviousTick Pointer to PreviousTick count. > > + before calling this function. > > + @return The elapsed time in nanoseconds since PreviousCount. > > + PreviousCount is overwritten with the current performance > > + counter value. > > +**/ > > +UINT64 > > +XhcGetElapsedTime ( > > + IN OUT UINT64 *PreviousTick > > + ); > > + > > #endif > > diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/XhciDxe.inf > > b/MdeModulePkg/Bus/Pci/XhciDxe/XhciDxe.inf > > index 5865d86822..18ef87916a 100644 > > --- a/MdeModulePkg/Bus/Pci/XhciDxe/XhciDxe.inf > > +++ b/MdeModulePkg/Bus/Pci/XhciDxe/XhciDxe.inf > > @@ -3,6 +3,7 @@ > > # It implements the interfaces of monitoring the status of all ports = and > > transferring # Control, Bulk, Interrupt and Isochronous requests to t= hose > > attached usb LS/FS/HS/SS devices. > > # > > +# (C) Copyright 2023 Hewlett Packard Enterprise Development LP
> > # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<= BR> # # > > SPDX-License-Identifier: BSD-2-Clause-Patent @@ -54,6 +55,7 @@ > > BaseMemoryLib > > DebugLib > > ReportStatusCodeLib > > + TimerLib > > > > [Guids] > > gEfiEventExitBootServicesGuid ## SOMETIMES_CONSUMES = ## > > Event > > diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/XhciReg.c > > b/MdeModulePkg/Bus/Pci/XhciDxe/XhciReg.c > > index 5700fc5fb8..07e57772b6 100644 > > --- a/MdeModulePkg/Bus/Pci/XhciDxe/XhciReg.c > > +++ b/MdeModulePkg/Bus/Pci/XhciDxe/XhciReg.c > > @@ -2,6 +2,7 @@ > > > > The XHCI register operation routines. > > > > +(C) Copyright 2023 Hewlett Packard Enterprise Development LP
> > Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.
> > SPDX-License-Identifier: BSD-2-Clause-Patent > > > > @@ -417,15 +418,14 @@ XhcClearOpRegBit ( > > Wait the operation register's bit as specified by Bit > > to become set (or clear). > > > > - @param Xhc The XHCI Instance. > > - @param Offset The offset of the operation register. > > - @param Bit The bit of the register to wait for. > > - @param WaitToSet Wait the bit to set or clear. > > - @param Timeout The time to wait before abort (in mil= lisecond, > > ms). > > + @param Xhc The XHCI Instance. > > + @param Offset The offset of the operation register. > > + @param Bit The bit of the register to wait for. > > + @param WaitToSet Wait the bit to set or clear. > > + @param Timeout The time to wait before abort (in millisecond, = ms). > > > > - @retval EFI_SUCCESS The bit successfully changed by host = controller. > > - @retval EFI_TIMEOUT The time out occurred. > > - @retval EFI_OUT_OF_RESOURCES Memory for the timer event could not > > be allocated. > > + @retval EFI_SUCCESS The bit successfully changed by host controller= . > > + @retval EFI_TIMEOUT The time out occurred. > > > > **/ > > EFI_STATUS > > @@ -437,54 +437,35 @@ XhcWaitOpRegBit ( > > IN UINT32 Timeout > > ) > > { > > - EFI_STATUS Status; > > - EFI_EVENT TimeoutEvent; > > - > > - TimeoutEvent =3D NULL; > > + UINT64 TimeoutTime; > > + UINT64 ElapsedTime; > > + UINT64 TimeDelta; > > + UINT64 CurrentTick; > > > > if (Timeout =3D=3D 0) { > > return EFI_TIMEOUT; > > } > > > > - Status =3D gBS->CreateEvent ( > > - EVT_TIMER, > > - TPL_CALLBACK, > > - NULL, > > - NULL, > > - &TimeoutEvent > > - ); > > - > > - if (EFI_ERROR (Status)) { > > - goto DONE; > > - } > > - > > - Status =3D gBS->SetTimer ( > > - TimeoutEvent, > > - TimerRelative, > > - EFI_TIMER_PERIOD_MILLISECONDS (Timeout) > > - ); > > - > > - if (EFI_ERROR (Status)) { > > - goto DONE; > > - } > > + TimeoutTime =3D XHC_MICROSECOND_TO_NANOSECOND ((UINT64)Timeout > > * > > + XHC_1_MILLISECOND); ElapsedTime =3D 0; CurrentTick =3D > > + GetPerformanceCounter (); > > > > do { > > if (XHC_REG_BIT_IS_SET (Xhc, Offset, Bit) =3D=3D WaitToSet) { > > - Status =3D EFI_SUCCESS; > > - goto DONE; > > + return EFI_SUCCESS; > > } > > > > gBS->Stall (XHC_1_MICROSECOND); > > - } while (EFI_ERROR (gBS->CheckEvent (TimeoutEvent))); > > - > > - Status =3D EFI_TIMEOUT; > > + TimeDelta =3D XhcGetElapsedTime (&CurrentTick); > > + // Ensure that ElapsedTime is always incremented to avoid indefini= te > > hangs > > + if (TimeDelta =3D=3D 0) { > > + TimeDelta =3D XHC_MICROSECOND_TO_NANOSECOND > > (XHC_1_MICROSECOND); > > + } > > > > -DONE: > > - if (TimeoutEvent !=3D NULL) { > > - gBS->CloseEvent (TimeoutEvent); > > - } > > + ElapsedTime +=3D TimeDelta; > > + } while (ElapsedTime < TimeoutTime); > > > > - return Status; > > + return EFI_TIMEOUT; > > } > > > > /** > > diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c > > b/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c > > index 53421e64a8..7d4b7a769d 100644 > > --- a/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c > > +++ b/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c > > @@ -2,6 +2,7 @@ > > > > XHCI transfer scheduling routines. > > > > +(C) Copyright 2023 Hewlett Packard Enterprise Development LP
> > Copyright (c) 2011 - 2020, Intel Corporation. All rights reserved.
> > Copyright (c) Microsoft Corporation.
Copyright (C) 2022 Advanced M= icro > > Devices, Inc. All rights reserved.
@@ -1276,15 +1277,14 @@ EXIT: > > /** > > Execute the transfer by polling the URB. This is a synchronous opera= tion. > > > > - @param Xhc The XHCI Instance. > > - @param CmdTransfer The executed URB is for cmd transfer = or not. > > - @param Urb The URB to execute. > > - @param Timeout The time to wait before abort, in mil= lisecond. > > + @param Xhc The XHCI Instance. > > + @param CmdTransfer The executed URB is for cmd transfer or no= t. > > + @param Urb The URB to execute. > > + @param Timeout The time to wait before abort, in millisec= ond. > > > > - @return EFI_DEVICE_ERROR The transfer failed due to transfer e= rror. > > - @return EFI_TIMEOUT The transfer failed due to time out. > > - @return EFI_SUCCESS The transfer finished OK. > > - @retval EFI_OUT_OF_RESOURCES Memory for the timer event could not > > be allocated. > > + @return EFI_DEVICE_ERROR The transfer failed due to transfer error. > > + @return EFI_TIMEOUT The transfer failed due to time out. > > + @return EFI_SUCCESS The transfer finished OK. > > > > **/ > > EFI_STATUS > > @@ -1299,12 +1299,14 @@ XhcExecTransfer ( > > UINT8 SlotId; > > UINT8 Dci; > > BOOLEAN Finished; > > - EFI_EVENT TimeoutEvent; > > + UINT64 TimeoutTime; > > + UINT64 ElapsedTime; > > + UINT64 TimeDelta; > > + UINT64 CurrentTick; > > BOOLEAN IndefiniteTimeout; > > > > Status =3D EFI_SUCCESS; > > Finished =3D FALSE; > > - TimeoutEvent =3D NULL; > > IndefiniteTimeout =3D FALSE; > > > > if (CmdTransfer) { > > @@ -1322,34 +1324,14 @@ XhcExecTransfer ( > > > > if (Timeout =3D=3D 0) { > > IndefiniteTimeout =3D TRUE; > > - goto RINGDOORBELL; > > - } > > - > > - Status =3D gBS->CreateEvent ( > > - EVT_TIMER, > > - TPL_CALLBACK, > > - NULL, > > - NULL, > > - &TimeoutEvent > > - ); > > - > > - if (EFI_ERROR (Status)) { > > - goto DONE; > > } > > > > - Status =3D gBS->SetTimer ( > > - TimeoutEvent, > > - TimerRelative, > > - EFI_TIMER_PERIOD_MILLISECONDS (Timeout) > > - ); > > - > > - if (EFI_ERROR (Status)) { > > - goto DONE; > > - } > > - > > -RINGDOORBELL: > > XhcRingDoorBell (Xhc, SlotId, Dci); > > > > + TimeoutTime =3D XHC_MICROSECOND_TO_NANOSECOND ((UINT64)Timeout > > * > > + XHC_1_MILLISECOND); ElapsedTime =3D 0; CurrentTick =3D > > + GetPerformanceCounter (); > > + > > do { > > Finished =3D XhcCheckUrbResult (Xhc, Urb); > > if (Finished) { > > @@ -1357,22 +1339,22 @@ RINGDOORBELL: > > } > > > > gBS->Stall (XHC_1_MICROSECOND); > > - } while (IndefiniteTimeout || EFI_ERROR (gBS->CheckEvent > > (TimeoutEvent))); > > + TimeDelta =3D XhcGetElapsedTime (&CurrentTick); > > + // Ensure that ElapsedTime is always incremented to avoid indefini= te > > hangs > > + if (TimeDelta =3D=3D 0) { > > + TimeDelta =3D XHC_MICROSECOND_TO_NANOSECOND > > (XHC_1_MICROSECOND); > > + } > > > > -DONE: > > - if (EFI_ERROR (Status)) { > > - Urb->Result =3D EFI_USB_ERR_NOTEXECUTE; > > - } else if (!Finished) { > > + ElapsedTime +=3D TimeDelta; > > + } while (IndefiniteTimeout || ElapsedTime < TimeoutTime); > > + > > + if (!Finished) { > > Urb->Result =3D EFI_USB_ERR_TIMEOUT; > > Status =3D EFI_TIMEOUT; > > } else if (Urb->Result !=3D EFI_USB_NOERROR) { > > Status =3D EFI_DEVICE_ERROR; > > } > > > > - if (TimeoutEvent !=3D NULL) { > > - gBS->CloseEvent (TimeoutEvent); > > - } > > - > > return Status; > > } > > > > -- > > 2.34.1 > > > > > > > > > > > > > > > > > > > > > > > >=20 > > -=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 (#108362): https://edk2.groups.io/g/devel/message/108362 Mute This Topic: https://groups.io/mt/100739508/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-