From nobody Tue May 14 22:53:23 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+106668+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+106668+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=hpe.com ARC-Seal: i=1; a=rsa-sha256; t=1688588176; cv=none; d=zohomail.com; s=zohoarc; b=MzuPBzfwWboTjogUGLMtJtSvNylR1LkfKZ6ET628wNbX2G3G6/YgQ91avHf0wsI3Ju1dgbT6DydoMVch/DnzuxMzN3L9xpKWWLCJCmkLjTLJSm9AqPJQHLfI6y7jqVb4lZF7on535C3J+VviqEwtUn+bGq+Fg5tMeA0xgp7n6ok= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1688588176; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=RaZCJewHkLHq5PiTJdHI+kY5KYCRUH/Hkn720IPInBY=; b=GdMaFXkJfnvmCx+kJle34QHiu1eCuUEUfTWOxHEZUh6OG+cpfC16VAtET6zo/vN5m0dyRi8pGZe+cgVhYS5LMFl/6wudhy/rNyjaQ4QrmWXVBT0rpRY0y+JHJyW4+541k3k0Lt7N8toeqaVJvGcJt44FW/5sijV9lZNl2tF5+OE= 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+106668+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 168858817647396.03671845891267; Wed, 5 Jul 2023 13:16:16 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id 5Ot8YY1788612xgoWeJStrj8; Wed, 05 Jul 2023 13:16:16 -0700 X-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 X-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 X-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 X-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) X-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) X-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: [edk2-devel] [PATCH] MdeModulePkg/XhciDxe: Use Performance Timer for XHCI Timeouts Date: Wed, 5 Jul 2023 15:15:37 -0500 Message-Id: X-Proofpoint-ORIG-GUID: A3v6q7bg-ALwe5P6je4flLfBrwETANtK X-Proofpoint-GUID: A3v6q7bg-ALwe5P6je4flLfBrwETANtK X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 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,patrick.henz@hpe.com X-Gm-Message-State: 1E1l3p4q5v1UYPskusxx0yKsx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1688588176; bh=vPuKSHKBNMnFuT1EExRiiMTR1XfOZjdP00ZQyKgQOk8=; h=Cc:Date:From:Reply-To:Subject:To; b=MLKFU7JN78JndZ52i3WzsiLTg9hTXZzJfE1my27k88YL9HGKg1ELI2I8TnwzCKJ3UMO KfbI/pLxRqvXK3570cLUZMBnjRc8wzpP3akgrGkkCaShqX8BCwdJ4z16qFy/1priENhvw b8Ku+uHo7fYZmX0yr38q3KcDasLZRGfXUm4= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1688588178556100003 Content-Type: text/plain; charset="utf-8" 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 The XHCI controller driver. =20 +(C) Copyright 2023 Hewlett Packard Enterprise Development LP
Copyright (c) 2011 - 2022, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -2294,3 +2295,58 @@ XhcDriverBindingStop ( =20 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/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 @@ =20 Provides some data structure definitions used by the XHCI host controlle= r driver. =20 +(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 =20 #include =20 @@ -37,6 +39,10 @@ typedef struct _USB_DEV_CONTEXT USB_DEV_CONTEXT; #include "ComponentName.h" #include "UsbHcMem.h" =20 +// +// 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 ); =20 +/** + 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/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 # Control, Bulk, Interrupt and Isochronous requests to those attached usb= LS/FS/HS/SS devices. # +# (C) Copyright 2023 Hewlett Packard Enterprise Development LP
# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.
# # SPDX-License-Identifier: BSD-2-Clause-Patent @@ -54,6 +55,7 @@ BaseMemoryLib DebugLib ReportStatusCodeLib + TimerLib =20 [Guids] gEfiEventExitBootServicesGuid ## SOMETIMES_CONSUMES ## E= vent 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 @@ =20 The XHCI register operation routines. =20 +(C) Copyright 2023 Hewlett Packard Enterprise Development LP
Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -417,15 +418,14 @@ XhcClearOpRegBit ( Wait the operation register's bit as specified by Bit to become set (or clear). =20 - @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 millise= cond, 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). =20 - @retval EFI_SUCCESS The bit successfully changed by host cont= roller. - @retval EFI_TIMEOUT The time out occurred. - @retval EFI_OUT_OF_RESOURCES Memory for the timer event could not be a= llocated. + @retval EFI_SUCCESS The bit successfully changed by host controller. + @retval EFI_TIMEOUT The time out occurred. =20 **/ 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; =20 if (Timeout =3D=3D 0) { return EFI_TIMEOUT; } =20 - 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 (Timeout * XHC_1_MILLISECO= ND); + ElapsedTime =3D 0; + CurrentTick =3D GetPerformanceCounter (); =20 do { if (XHC_REG_BIT_IS_SET (Xhc, Offset, Bit) =3D=3D WaitToSet) { - Status =3D EFI_SUCCESS; - goto DONE; + return EFI_SUCCESS; } =20 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 indefinite h= angs + if (TimeDelta =3D=3D 0) { + TimeDelta =3D XHC_MICROSECOND_TO_NANOSECOND (XHC_1_MICROSECOND); + } =20 -DONE: - if (TimeoutEvent !=3D NULL) { - gBS->CloseEvent (TimeoutEvent); - } + ElapsedTime +=3D TimeDelta; + } while (ElapsedTime < TimeoutTime); =20 - return Status; + return EFI_TIMEOUT; } =20 /** 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 @@ =20 XHCI transfer scheduling routines. =20 +(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 Micro Devices, Inc. All rights reserved.
@@ -1273,15 +1274,14 @@ EXIT: /** Execute the transfer by polling the URB. This is a synchronous operation. =20 - @param Xhc The XHCI Instance. - @param CmdTransfer The executed URB is for cmd transfer or n= ot. - @param Urb The URB to execute. - @param Timeout The time to wait before abort, in millise= cond. + @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 millisecond. =20 - @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. - @retval EFI_OUT_OF_RESOURCES Memory for the timer event could not be a= llocated. + @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. =20 **/ EFI_STATUS @@ -1296,12 +1296,14 @@ XhcExecTransfer ( UINT8 SlotId; UINT8 Dci; BOOLEAN Finished; - EFI_EVENT TimeoutEvent; + UINT64 TimeoutTime; + UINT64 ElapsedTime; + UINT64 TimeDelta; + UINT64 CurrentTick; BOOLEAN IndefiniteTimeout; =20 Status =3D EFI_SUCCESS; Finished =3D FALSE; - TimeoutEvent =3D NULL; IndefiniteTimeout =3D FALSE; =20 if (CmdTransfer) { @@ -1319,34 +1321,14 @@ XhcExecTransfer ( =20 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; } =20 - Status =3D gBS->SetTimer ( - TimeoutEvent, - TimerRelative, - EFI_TIMER_PERIOD_MILLISECONDS (Timeout) - ); - - if (EFI_ERROR (Status)) { - goto DONE; - } - -RINGDOORBELL: XhcRingDoorBell (Xhc, SlotId, Dci); =20 + TimeoutTime =3D XHC_MICROSECOND_TO_NANOSECOND (Timeout * XHC_1_MILLISECO= ND); + ElapsedTime =3D 0; + CurrentTick =3D GetPerformanceCounter (); + do { Finished =3D XhcCheckUrbResult (Xhc, Urb); if (Finished) { @@ -1354,22 +1336,22 @@ RINGDOORBELL: } =20 gBS->Stall (XHC_1_MICROSECOND); - } while (IndefiniteTimeout || EFI_ERROR (gBS->CheckEvent (TimeoutEvent))= ); + TimeDelta =3D XhcGetElapsedTime (&CurrentTick); + // Ensure that ElapsedTime is always incremented to avoid indefinite h= angs + if (TimeDelta =3D=3D 0) { + TimeDelta =3D XHC_MICROSECOND_TO_NANOSECOND (XHC_1_MICROSECOND); + } =20 -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; } =20 - if (TimeoutEvent !=3D NULL) { - gBS->CloseEvent (TimeoutEvent); - } - return Status; } =20 --=20 2.34.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 (#106668): https://edk2.groups.io/g/devel/message/106668 Mute This Topic: https://groups.io/mt/99972791/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-