From nobody Tue May 14 20:10: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+108549+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+108549+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=hpe.com ARC-Seal: i=1; a=rsa-sha256; t=1694542023; cv=none; d=zohomail.com; s=zohoarc; b=DfxpIvnkoTW7hh2hXicTrqR0kw3liBYzbUGs2lLsWnQtrcOiwzgPDmVFedPnYr05IVNhA62iOMUgadUqVpdss/XOGncGAMfePCwZBsyD53DiGFZC5qzwu2ZqpSv+1fc7/e+e0HA6OkRauIQ7UjEb4eP+TtEN/xlB425+tlWso1w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1694542023; 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=Fr2qqzNUG+vr2RyRAusoArVzjw18bFt5afP/wQwzxoQ=; b=RbC4kpggSfPS7vZkizCoV3SxQJmQCKijIgHRncqjodAZF/vgTYOMC3IIhpoPpEVqksIIKezCo5hhibXLRYdW8hrWupevi3SyeG6C64lXkhWPwp60tnmUg7Z7v+y133Vb8/U2ms8OVwtfXn9NiJIDBe6dVK7tMElxcgsmuOuORmM= 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+108549+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 1694542023827180.17476883956977; Tue, 12 Sep 2023 11:07:03 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=aqqCqY4bhJgGRd5p5b0XP+kQ5gFxB6ZG/nbJyY0o0dc=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1694542023; v=1; b=qu6fwk/DMljqX+7JVaVXD0OD7ptTvayW3RTS8t72smTlgZ1GfMQFTjExKhcsUsJ74/25ys15 0LDecmBZg2poXZYKSLyiTP5f6Svmjx6JeeW6FkUwD8D4mpFdoP9R1avkb3zEwRq0IVqIad/Q0s+ rR9Xm1YWu0GdGpgANH8+gpT4= X-Received: by 127.0.0.2 with SMTP id Qx7pYY1788612xXGZjWwuCj6; Tue, 12 Sep 2023 11:07:03 -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.1740.1694542001882798165 for ; Tue, 12 Sep 2023 11:07:02 -0700 X-Received: from pps.filterd (m0134423.ppops.net [127.0.0.1]) by mx0b-002e3701.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 38CHh2aO018440; Tue, 12 Sep 2023 18:06:31 GMT X-Received: from p1lg14881.it.hpe.com ([16.230.97.202]) by mx0b-002e3701.pphosted.com (PPS) with ESMTPS id 3t2vmgr6y1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 12 Sep 2023 18:06:30 +0000 X-Received: from p1lg14886.dc01.its.hpecorp.net (unknown [10.119.18.237]) (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 p1lg14881.it.hpe.com (Postfix) with ESMTPS id 1DDB5805E5F; Tue, 12 Sep 2023 18:06:30 +0000 (UTC) X-Received: from cat.eag.rdlabs.hpecorp.net (unknown [16.231.227.39]) by p1lg14886.dc01.its.hpecorp.net (Postfix) with ESMTP id 9F74281260E; Tue, 12 Sep 2023 18:06:29 +0000 (UTC) X-Received: by cat.eag.rdlabs.hpecorp.net (Postfix, from userid 178665) id DEBDCAC763; Tue, 12 Sep 2023 13:06:28 -0500 (CDT) From: "Henz, Patrick" To: devel@edk2.groups.io Cc: hao.a.wu@intel.com, ray.ni@intel.com, Patrick Henz Subject: [edk2-devel] [PATCH v3] MdeModulePkg/XhciDxe: Use Performance Timer for XHCI Timeouts Date: Tue, 12 Sep 2023 13:05:57 -0500 Message-Id: <382ea3abf9d4e410cfcde1bcff453eac409e350b.1694541645.git.patrick.henz@hpe.com> X-Proofpoint-ORIG-GUID: -I6kDn5_cWKIGWMhTWYIj7mwFIyglLan X-Proofpoint-GUID: -I6kDn5_cWKIGWMhTWYIj7mwFIyglLan X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 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,patrick.henz@hpe.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: AXyqsGkc4IxBlXmIrw3LaVUVx1787277AA= Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1694542025030100001 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. Cc: Hao A Wu Cc: Ray Ni Signed-off-by: Patrick Henz Reviewed-by: Reviewed-by: Hao A Wu --- MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c | 117 +++++++++++++++++++++++ MdeModulePkg/Bus/Pci/XhciDxe/Xhci.h | 32 +++++++ MdeModulePkg/Bus/Pci/XhciDxe/XhciDxe.inf | 2 + MdeModulePkg/Bus/Pci/XhciDxe/XhciReg.c | 68 +++++-------- MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c | 72 ++++++-------- 5 files changed, 204 insertions(+), 87 deletions(-) diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c b/MdeModulePkg/Bus/Pci/Xhc= iDxe/Xhci.c index 62682dd27c..7a2e32a9dd 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 @@ -85,6 +86,11 @@ EFI_USB2_HC_PROTOCOL gXhciUsb2HcTemplate =3D { 0x0 }; =20 +UINT64 mPerformanceCounterStartValue; +UINT64 mPerformanceCounterEndValue; +UINT64 mPerformanceCounterFrequency; +BOOLEAN mPerformanceCounterValuesCached =3D FALSE; + /** Retrieves the capability of root hub ports. =20 @@ -2294,3 +2300,114 @@ XhcDriverBindingStop ( =20 return EFI_SUCCESS; } + +/** + Converts a time in nanoseconds to a performance counter tick count. + + @param Time The time in nanoseconds to be converted to performance cou= nter ticks. + @return Time in nanoseconds converted to ticks. +**/ +UINT64 +XhcConvertTimeToTicks ( + IN UINT64 Time + ) +{ + UINT64 Ticks; + UINT64 Remainder; + UINT64 Divisor; + UINTN Shift; + + // Cache the return values to avoid repeated calls to GetPerformanceCoun= terProperties () + if (!mPerformanceCounterValuesCached) { + mPerformanceCounterFrequency =3D GetPerformanceCounterProperties ( + &mPerformanceCounterStartValue, + &mPerformanceCounterEndValue + ); + + mPerformanceCounterValuesCached =3D TRUE; + } + + // Prevent returning a tick value of 0, unless Time is already 0 + if (0 =3D=3D mPerformanceCounterFrequency) { + return Time; + } + + // Nanoseconds per second + Divisor =3D 1000000000; + + // + // Frequency + // Ticks =3D ------------- x Time + // 1,000,000,000 + // + Ticks =3D MultU64x64 ( + DivU64x64Remainder ( + mPerformanceCounterFrequency, + Divisor, + &Remainder + ), + Time + ); + + // + // Ensure (Remainder * Time) will not overflow 64-bit. + // + // HighBitSet64 (Remainder) + 1 + HighBitSet64 (Time) + 1 <=3D 64 + // + Shift =3D MAX (0, HighBitSet64 (Remainder) + HighBitSet64 (Time) - 6= 2); + Remainder =3D RShiftU64 (Remainder, (UINTN)Shift); + Divisor =3D RShiftU64 (Divisor, (UINTN)Shift); + Ticks +=3D DivU64x64Remainder (MultU64x64 (Remainder, Time), Divisor,= NULL); + + return Ticks; +} + +/** + Computes and returns the elapsed ticks since PreviousTick. The + value of PreviousTick is overwritten with the current performance + counter value. + + @param PreviousTick Pointer to PreviousTick count. + @return The elapsed ticks since PreviousCount. PreviousCount is + overwritten with the current performance counter value. +**/ +UINT64 +XhcGetElapsedTicks ( + IN OUT UINT64 *PreviousTick + ) +{ + UINT64 CurrentTick; + UINT64 Delta; + + CurrentTick =3D GetPerformanceCounter (); + + // + // Determine if the counter is counting up or down + // + if (mPerformanceCounterStartValue < mPerformanceCounterEndValue) { + // + // Counter counts upwards, check for an overflow condition + // + if (*PreviousTick > CurrentTick) { + Delta =3D (mPerformanceCounterEndValue - *PreviousTick) + CurrentTic= k; + } else { + Delta =3D CurrentTick - *PreviousTick; + } + } else { + // + // Counter counts downwards, check for an underflow condition + // + if (*PreviousTick < CurrentTick) { + Delta =3D (mPerformanceCounterStartValue - CurrentTick) + *PreviousT= ick; + } else { + Delta =3D *PreviousTick - CurrentTick; + } + } + + // + // Set PreviousTick to CurrentTick + // + *PreviousTick =3D CurrentTick; + + return Delta; +} diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.h b/MdeModulePkg/Bus/Pci/Xhc= iDxe/Xhci.h index ca223bd20c..4401675872 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,11 @@ 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) (MultU64x32((Time), 1000)) + // // The unit is microsecond, setting it as 1us. // @@ -720,4 +727,29 @@ XhcAsyncIsochronousTransfer ( IN VOID *Context ); =20 +/** + Converts a time in nanoseconds to a performance counter tick count. + + @param Time The time in nanoseconds to be converted to performance cou= nter ticks. + @return Time in nanoseconds converted to ticks. +**/ +UINT64 +XhcConvertTimeToTicks ( + UINT64 Time + ); + +/** + Computes and returns the elapsed ticks since PreviousTick. The + value of PreviousTick is overwritten with the current performance + counter value. + + @param PreviousTick Pointer to PreviousTick count. + @return The elapsed ticks since PreviousCount. PreviousCount is + overwritten with the current performance counter value. +**/ +UINT64 +XhcGetElapsedTicks ( + 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..40f2f1f227 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,34 @@ XhcWaitOpRegBit ( IN UINT32 Timeout ) { - EFI_STATUS Status; - EFI_EVENT TimeoutEvent; - - TimeoutEvent =3D NULL; + UINT64 TimeoutTicks; + UINT64 ElapsedTicks; + UINT64 TicksDelta; + 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; - } - + TimeoutTicks =3D XhcConvertTimeToTicks (XHC_MICROSECOND_TO_NANOSECOND (T= imeout * XHC_1_MILLISECOND)); + ElapsedTicks =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; } =20 gBS->Stall (XHC_1_MICROSECOND); - } while (EFI_ERROR (gBS->CheckEvent (TimeoutEvent))); - - Status =3D EFI_TIMEOUT; + TicksDelta =3D XhcGetElapsedTicks (&CurrentTick); + // Ensure that ElapsedTicks is always incremented to avoid indefinite = hangs + if (TicksDelta =3D=3D 0) { + TicksDelta =3D XhcConvertTimeToTicks (XHC_MICROSECOND_TO_NANOSECOND = (XHC_1_MICROSECOND)); + } =20 -DONE: - if (TimeoutEvent !=3D NULL) { - gBS->CloseEvent (TimeoutEvent); - } + ElapsedTicks +=3D TicksDelta; + } while (ElapsedTicks < TimeoutTicks); =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 53421e64a8..613b1485f1 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.
@@ -1276,15 +1277,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 @@ -1299,12 +1299,14 @@ XhcExecTransfer ( UINT8 SlotId; UINT8 Dci; BOOLEAN Finished; - EFI_EVENT TimeoutEvent; + UINT64 TimeoutTicks; + UINT64 ElapsedTicks; + UINT64 TicksDelta; + UINT64 CurrentTick; BOOLEAN IndefiniteTimeout; =20 Status =3D EFI_SUCCESS; Finished =3D FALSE; - TimeoutEvent =3D NULL; IndefiniteTimeout =3D FALSE; =20 if (CmdTransfer) { @@ -1322,34 +1324,18 @@ 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; - } - - Status =3D gBS->SetTimer ( - TimeoutEvent, - TimerRelative, - EFI_TIMER_PERIOD_MILLISECONDS (Timeout) - ); - - if (EFI_ERROR (Status)) { - goto DONE; } =20 -RINGDOORBELL: XhcRingDoorBell (Xhc, SlotId, Dci); =20 + TimeoutTicks =3D XhcConvertTimeToTicks ( + XHC_MICROSECOND_TO_NANOSECOND ( + Timeout * XHC_1_MILLISECOND + ) + ); + ElapsedTicks =3D 0; + CurrentTick =3D GetPerformanceCounter (); + do { Finished =3D XhcCheckUrbResult (Xhc, Urb); if (Finished) { @@ -1357,22 +1343,22 @@ RINGDOORBELL: } =20 gBS->Stall (XHC_1_MICROSECOND); - } while (IndefiniteTimeout || EFI_ERROR (gBS->CheckEvent (TimeoutEvent))= ); + TicksDelta =3D XhcGetElapsedTicks (&CurrentTick); + // Ensure that ElapsedTicks is always incremented to avoid indefinite = hangs + if (TicksDelta =3D=3D 0) { + TicksDelta =3D XhcConvertTimeToTicks (XHC_MICROSECOND_TO_NANOSECOND = (XHC_1_MICROSECOND)); + } =20 -DONE: - if (EFI_ERROR (Status)) { - Urb->Result =3D EFI_USB_ERR_NOTEXECUTE; - } else if (!Finished) { + ElapsedTicks +=3D TicksDelta; + } while (IndefiniteTimeout || ElapsedTicks < TimeoutTicks); + + 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 (#108549): https://edk2.groups.io/g/devel/message/108549 Mute This Topic: https://groups.io/mt/101320913/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-