From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0b-002e3701.pphosted.com (mx0b-002e3701.pphosted.com [148.163.143.35]) by mx.groups.io with SMTP id smtpd.web10.5408.1688588154914444907 for ; Wed, 05 Jul 2023 13:16:15 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@hpe.com header.s=pps0720 header.b=aybf66t9; spf=temperror, err=temporary DNS error (domain: cat.eag.rdlabs.hpecorp.net, ip: 148.163.143.35, mailfrom: henz@cat.eag.rdlabs.hpecorp.net) Received: from pps.filterd (m0150245.ppops.net [127.0.0.1]) by mx0b-002e3701.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 365Gqxki025962 for ; Wed, 5 Jul 2023 20:15:44 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hpe.com; h=from : to : cc : subject : date : message-id : content-transfer-encoding : mime-version; s=pps0720; bh=UDCTo1RcKijuthSbepVw/v2rINsIeDz4nyjNzWpz2k4=; b=aybf66t9cDQCoOpDqfXiCnlgBFOQudJzFhw9O7xbstrNbZQD43Po7tcf7yfP7Lxd1upc IrlHByyRvZ+UqseL9TeBJB7EfGAmZvz7FMA2Ezkt7yTC4w/D3XfyzcC7Dq0xyhwS4Ysb xqdelPdgGfvnz1hAeX4OWE5jPTGz4hRW56ottFBoNvqF8Oaz8jY3BMLLN+sU4npkWrWu atJSTwji5Pm+T4bBJwC+fvAlmlnQv2GhnLcXMXPbulPO74lmsa2n1MVcRmU/eqc7Sb5s 0uY+WlPN/vrYQY1CWqWFmY1S7N9ODjxhzjEal+cHD0k/1goeCHYl7cbOAIXedGiySBRt iA== Received: from p1lg14878.it.hpe.com (p1lg14878.it.hpe.com [16.230.97.204]) by mx0b-002e3701.pphosted.com (PPS) with ESMTPS id 3rn2j4xjg6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 05 Jul 2023 20:15:43 +0000 Received: from p1lg14885.dc01.its.hpecorp.net (unknown [10.119.18.236]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by p1lg14878.it.hpe.com (Postfix) with ESMTPS id 3BE971479D for ; Wed, 5 Jul 2023 20:15:43 +0000 (UTC) Received: from cat.eag.rdlabs.hpecorp.net (unknown [16.231.227.36]) by p1lg14885.dc01.its.hpecorp.net (Postfix) with ESMTP id D9C5E8097EA; Wed, 5 Jul 2023 20:15:42 +0000 (UTC) Received: by cat.eag.rdlabs.hpecorp.net (Postfix, from userid 178665) id 13CBFB48A3; Wed, 5 Jul 2023 15:15:42 -0500 (CDT) From: "Henz, Patrick" To: devel@edk2.groups.io Cc: Patrick Henz Subject: [PATCH] MdeModulePkg/XhciDxe: Use Performance Timer for XHCI Timeouts Date: Wed, 5 Jul 2023 15:15:37 -0500 Message-Id: X-Mailer: git-send-email 2.35.3 X-Proofpoint-ORIG-GUID: A3v6q7bg-ALwe5P6je4flLfBrwETANtK X-Proofpoint-GUID: A3v6q7bg-ALwe5P6je4flLfBrwETANtK X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-07-05_11,2023-07-05_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=935 priorityscore=1501 bulkscore=0 phishscore=0 suspectscore=0 spamscore=0 malwarescore=0 impostorscore=0 clxscore=1011 mlxscore=0 adultscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2305260000 definitions=main-2307050184 Content-Transfer-Encoding: quoted-printable REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3D2948 XhciDxe uses the timer functionality provided by the boot services table to detect timeout conditions. This breaks the driver's ExitBootServices call back, as CoreExitBootServices halts the timer before signaling the ExitBootServices event. If the host controller fails to halt in the call back, the timeout condition will never occur and the boot gets stuck in an indefinite spin loop. Use 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/Xhc= iDxe/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=0D The XHCI controller driver.=0D =0D +(C) Copyright 2023 Hewlett Packard Enterprise Development LP
=0D Copyright (c) 2011 - 2022, Intel Corporation. All rights reserved.
=0D SPDX-License-Identifier: BSD-2-Clause-Patent=0D =0D @@ -2294,3 +2295,58 @@ XhcDriverBindingStop ( =0D return EFI_SUCCESS;=0D }=0D +=0D +/**=0D + Computes and returns the elapsed time in nanoseconds since=0D + PreviousTick. The value of PreviousTick is overwritten with the=0D + current performance counter value.=0D +=0D + @param PreviousTick Pointer to PreviousTick count.=0D + @return The elapsed time in nanoseconds since PreviousCount.=0D + PreviousCount is overwritten with the current performance=0D + counter value.=0D +**/=0D +UINT64=0D +XhcGetElapsedTime (=0D + IN OUT UINT64 *PreviousTick=0D + )=0D +{=0D + UINT64 StartValue;=0D + UINT64 EndValue;=0D + UINT64 CurrentTick;=0D + UINT64 Delta;=0D +=0D + GetPerformanceCounterProperties (&StartValue, &EndValue);=0D +=0D + CurrentTick =3D GetPerformanceCounter ();=0D +=0D + //=0D + // Determine if the counter is counting up or down=0D + //=0D + if (StartValue < EndValue) {=0D + //=0D + // Counter counts upwards, check for an overflow condition=0D + //=0D + if (*PreviousTick > CurrentTick) {=0D + Delta =3D (EndValue - *PreviousTick) + CurrentTick;=0D + } else {=0D + Delta =3D CurrentTick - *PreviousTick;=0D + }=0D + } else {=0D + //=0D + // Counter counts downwards, check for an underflow condition=0D + //=0D + if (*PreviousTick < CurrentTick) {=0D + Delta =3D (StartValue - CurrentTick) + *PreviousTick;=0D + } else {=0D + Delta =3D *PreviousTick - CurrentTick;=0D + }=0D + }=0D +=0D + //=0D + // Set PreviousTick to CurrentTick=0D + //=0D + *PreviousTick =3D CurrentTick;=0D +=0D + return GetTimeInNanoSecond (Delta);=0D +}=0D diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.h b/MdeModulePkg/Bus/Pci/Xhc= iDxe/Xhci.h index ca223bd20c..77feb66647 100644 --- a/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.h +++ b/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.h @@ -2,6 +2,7 @@ =0D Provides some data structure definitions used by the XHCI host controlle= r driver.=0D =0D +(C) Copyright 2023 Hewlett Packard Enterprise Development LP
=0D Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.
=0D Copyright (c) Microsoft Corporation.
=0D SPDX-License-Identifier: BSD-2-Clause-Patent=0D @@ -26,6 +27,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include =0D #include =0D #include =0D +#include =0D =0D #include =0D =0D @@ -37,6 +39,10 @@ typedef struct _USB_DEV_CONTEXT USB_DEV_CONTEXT; #include "ComponentName.h"=0D #include "UsbHcMem.h"=0D =0D +//=0D +// Converts a count from microseconds to nanoseconds=0D +//=0D +#define XHC_MICROSECOND_TO_NANOSECOND(Time) ((UINT64)(Time) * 1000)=0D //=0D // The unit is microsecond, setting it as 1us.=0D //=0D @@ -720,4 +726,20 @@ XhcAsyncIsochronousTransfer ( IN VOID *Context=0D );=0D =0D +/**=0D + Computes and returns the elapsed time in nanoseconds since=0D + PreviousTick. The value of PreviousTick is overwritten with the=0D + current performance counter value.=0D +=0D + @param PreviousTick Pointer to PreviousTick count.=0D + before calling this function.=0D + @return The elapsed time in nanoseconds since PreviousCount.=0D + PreviousCount is overwritten with the current performance=0D + counter value.=0D +**/=0D +UINT64=0D +XhcGetElapsedTime (=0D + IN OUT UINT64 *PreviousTick=0D + );=0D +=0D #endif=0D diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/XhciDxe.inf b/MdeModulePkg/Bus/Pc= i/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=0D # Control, Bulk, Interrupt and Isochronous requests to those attached usb= LS/FS/HS/SS devices.=0D #=0D +# (C) Copyright 2023 Hewlett Packard Enterprise Development LP
=0D # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.
= =0D #=0D # SPDX-License-Identifier: BSD-2-Clause-Patent=0D @@ -54,6 +55,7 @@ BaseMemoryLib=0D DebugLib=0D ReportStatusCodeLib=0D + TimerLib=0D =0D [Guids]=0D gEfiEventExitBootServicesGuid ## SOMETIMES_CONSUMES ## E= vent=0D diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/XhciReg.c b/MdeModulePkg/Bus/Pci/= XhciDxe/XhciReg.c index 5700fc5fb8..2499698715 100644 --- a/MdeModulePkg/Bus/Pci/XhciDxe/XhciReg.c +++ b/MdeModulePkg/Bus/Pci/XhciDxe/XhciReg.c @@ -2,6 +2,7 @@ =0D The XHCI register operation routines.=0D =0D +(C) Copyright 2023 Hewlett Packard Enterprise Development LP
=0D Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.
=0D SPDX-License-Identifier: BSD-2-Clause-Patent=0D =0D @@ -417,15 +418,14 @@ XhcClearOpRegBit ( Wait the operation register's bit as specified by Bit=0D to become set (or clear).=0D =0D - @param Xhc The XHCI Instance.=0D - @param Offset The offset of the operation register.=0D - @param Bit The bit of the register to wait for.=0D - @param WaitToSet Wait the bit to set or clear.=0D - @param Timeout The time to wait before abort (in millise= cond, ms).=0D + @param Xhc The XHCI Instance.=0D + @param Offset The offset of the operation register.=0D + @param Bit The bit of the register to wait for.=0D + @param WaitToSet Wait the bit to set or clear.=0D + @param Timeout The time to wait before abort (in millisecond, ms).= =0D =0D - @retval EFI_SUCCESS The bit successfully changed by host cont= roller.=0D - @retval EFI_TIMEOUT The time out occurred.=0D - @retval EFI_OUT_OF_RESOURCES Memory for the timer event could not be a= llocated.=0D + @retval EFI_SUCCESS The bit successfully changed by host controller.=0D + @retval EFI_TIMEOUT The time out occurred.=0D =0D **/=0D EFI_STATUS=0D @@ -437,54 +437,35 @@ XhcWaitOpRegBit ( IN UINT32 Timeout=0D )=0D {=0D - EFI_STATUS Status;=0D - EFI_EVENT TimeoutEvent;=0D -=0D - TimeoutEvent =3D NULL;=0D + UINT64 TimeoutTime;=0D + UINT64 ElapsedTime;=0D + UINT64 TimeDelta;=0D + UINT64 CurrentTick;=0D =0D if (Timeout =3D=3D 0) {=0D return EFI_TIMEOUT;=0D }=0D =0D - Status =3D gBS->CreateEvent (=0D - EVT_TIMER,=0D - TPL_CALLBACK,=0D - NULL,=0D - NULL,=0D - &TimeoutEvent=0D - );=0D -=0D - if (EFI_ERROR (Status)) {=0D - goto DONE;=0D - }=0D -=0D - Status =3D gBS->SetTimer (=0D - TimeoutEvent,=0D - TimerRelative,=0D - EFI_TIMER_PERIOD_MILLISECONDS (Timeout)=0D - );=0D -=0D - if (EFI_ERROR (Status)) {=0D - goto DONE;=0D - }=0D + TimeoutTime =3D XHC_MICROSECOND_TO_NANOSECOND (Timeout * XHC_1_MILLISECO= ND);=0D + ElapsedTime =3D 0;=0D + CurrentTick =3D GetPerformanceCounter ();=0D =0D do {=0D if (XHC_REG_BIT_IS_SET (Xhc, Offset, Bit) =3D=3D WaitToSet) {=0D - Status =3D EFI_SUCCESS;=0D - goto DONE;=0D + return EFI_SUCCESS;=0D }=0D =0D gBS->Stall (XHC_1_MICROSECOND);=0D - } while (EFI_ERROR (gBS->CheckEvent (TimeoutEvent)));=0D -=0D - Status =3D EFI_TIMEOUT;=0D + TimeDelta =3D XhcGetElapsedTime (&CurrentTick);=0D + // Ensure that ElapsedTime is always incremented to avoid indefinite h= angs=0D + if (TimeDelta =3D=3D 0) {=0D + TimeDelta =3D XHC_MICROSECOND_TO_NANOSECOND (XHC_1_MICROSECOND);=0D + }=0D =0D -DONE:=0D - if (TimeoutEvent !=3D NULL) {=0D - gBS->CloseEvent (TimeoutEvent);=0D - }=0D + ElapsedTime +=3D TimeDelta;=0D + } while (ElapsedTime < TimeoutTime);=0D =0D - return Status;=0D + return EFI_TIMEOUT;=0D }=0D =0D /**=0D diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c b/MdeModulePkg/Bus/Pc= i/XhciDxe/XhciSched.c index 298fb88b81..5177886e52 100644 --- a/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c +++ b/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c @@ -2,6 +2,7 @@ =0D XHCI transfer scheduling routines.=0D =0D +(C) Copyright 2023 Hewlett Packard Enterprise Development LP
=0D Copyright (c) 2011 - 2020, Intel Corporation. All rights reserved.
=0D Copyright (c) Microsoft Corporation.
=0D Copyright (C) 2022 Advanced Micro Devices, Inc. All rights reserved.
=0D @@ -1273,15 +1274,14 @@ EXIT: /**=0D Execute the transfer by polling the URB. This is a synchronous operation= .=0D =0D - @param Xhc The XHCI Instance.=0D - @param CmdTransfer The executed URB is for cmd transfer or n= ot.=0D - @param Urb The URB to execute.=0D - @param Timeout The time to wait before abort, in millise= cond.=0D + @param Xhc The XHCI Instance.=0D + @param CmdTransfer The executed URB is for cmd transfer or not.=0D + @param Urb The URB to execute.=0D + @param Timeout The time to wait before abort, in millisecond.= =0D =0D - @return EFI_DEVICE_ERROR The transfer failed due to transfer error= .=0D - @return EFI_TIMEOUT The transfer failed due to time out.=0D - @return EFI_SUCCESS The transfer finished OK.=0D - @retval EFI_OUT_OF_RESOURCES Memory for the timer event could not be a= llocated.=0D + @return EFI_DEVICE_ERROR The transfer failed due to transfer error.=0D + @return EFI_TIMEOUT The transfer failed due to time out.=0D + @return EFI_SUCCESS The transfer finished OK.=0D =0D **/=0D EFI_STATUS=0D @@ -1296,12 +1296,14 @@ XhcExecTransfer ( UINT8 SlotId;=0D UINT8 Dci;=0D BOOLEAN Finished;=0D - EFI_EVENT TimeoutEvent;=0D + UINT64 TimeoutTime;=0D + UINT64 ElapsedTime;=0D + UINT64 TimeDelta;=0D + UINT64 CurrentTick;=0D BOOLEAN IndefiniteTimeout;=0D =0D Status =3D EFI_SUCCESS;=0D Finished =3D FALSE;=0D - TimeoutEvent =3D NULL;=0D IndefiniteTimeout =3D FALSE;=0D =0D if (CmdTransfer) {=0D @@ -1319,34 +1321,14 @@ XhcExecTransfer ( =0D if (Timeout =3D=3D 0) {=0D IndefiniteTimeout =3D TRUE;=0D - goto RINGDOORBELL;=0D - }=0D -=0D - Status =3D gBS->CreateEvent (=0D - EVT_TIMER,=0D - TPL_CALLBACK,=0D - NULL,=0D - NULL,=0D - &TimeoutEvent=0D - );=0D -=0D - if (EFI_ERROR (Status)) {=0D - goto DONE;=0D }=0D =0D - Status =3D gBS->SetTimer (=0D - TimeoutEvent,=0D - TimerRelative,=0D - EFI_TIMER_PERIOD_MILLISECONDS (Timeout)=0D - );=0D -=0D - if (EFI_ERROR (Status)) {=0D - goto DONE;=0D - }=0D -=0D -RINGDOORBELL:=0D XhcRingDoorBell (Xhc, SlotId, Dci);=0D =0D + TimeoutTime =3D XHC_MICROSECOND_TO_NANOSECOND (Timeout * XHC_1_MILLISECO= ND);=0D + ElapsedTime =3D 0;=0D + CurrentTick =3D GetPerformanceCounter ();=0D +=0D do {=0D Finished =3D XhcCheckUrbResult (Xhc, Urb);=0D if (Finished) {=0D @@ -1354,22 +1336,22 @@ RINGDOORBELL: }=0D =0D gBS->Stall (XHC_1_MICROSECOND);=0D - } while (IndefiniteTimeout || EFI_ERROR (gBS->CheckEvent (TimeoutEvent))= );=0D + TimeDelta =3D XhcGetElapsedTime (&CurrentTick);=0D + // Ensure that ElapsedTime is always incremented to avoid indefinite h= angs=0D + if (TimeDelta =3D=3D 0) {=0D + TimeDelta =3D XHC_MICROSECOND_TO_NANOSECOND (XHC_1_MICROSECOND);=0D + }=0D =0D -DONE:=0D - if (EFI_ERROR (Status)) {=0D - Urb->Result =3D EFI_USB_ERR_NOTEXECUTE;=0D - } else if (!Finished) {=0D + ElapsedTime +=3D TimeDelta;=0D + } while (IndefiniteTimeout || ElapsedTime < TimeoutTime);=0D +=0D + if (!Finished) {=0D Urb->Result =3D EFI_USB_ERR_TIMEOUT;=0D Status =3D EFI_TIMEOUT;=0D } else if (Urb->Result !=3D EFI_USB_NOERROR) {=0D Status =3D EFI_DEVICE_ERROR;=0D }=0D =0D - if (TimeoutEvent !=3D NULL) {=0D - gBS->CloseEvent (TimeoutEvent);=0D - }=0D -=0D return Status;=0D }=0D =0D --=20 2.34.1