From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (NAM04-MW2-obe.outbound.protection.outlook.com [40.107.101.131]) by mx.groups.io with SMTP id smtpd.web12.10032.1631721567789808902 for ; Wed, 15 Sep 2021 08:59:27 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@os.amperecomputing.com header.s=selector2 header.b=Ptgb01fE; spf=pass (domain: os.amperecomputing.com, ip: 40.107.101.131, mailfrom: nhi@os.amperecomputing.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=A23VHLZqjMPky6QiDq1h05Ewg4gZ5Zo9vD+y5LmEnFGiiVFAagU4ebNBL+cSBWX+7CAo4olQgngNTWXrCd7FoR+vRyau/KrXpH8LtN7NdkzWWVzDCkxIzRfJXwPUZEnRVduuYe6TFi3oNCzr3ZCuWyu7rXHfQOrq1wL/B0ELwxBhrasXT8RDIpZGtv+oXfvWhuJgL6WU8GQPde+EqEuaYfBdAlqwh6jGS7faSUQiWgb4MjpcJsf4z6Scv3xhTDucai3Bif/TwxN9i+f7upSkDalITUvUzJYyFgFHk+u9UNDj6UPl5ad90aBLbqP1YVTDdSa4LWv46M8URTsa2VCyqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=68j/3IKT8UYDTRdL5F2sak9r95PLew4cVrkXXY2qH14=; b=iy8C5aVaRQLu8IPFxKTAcsmZiRVxkoYPiNXcz4tJY2kS02SmxUJlnKy8UxQolBGpeMQFM2OWxc9QeSOgU5urxYLrCx1U12bMuuOwss43ntMYeTmJRNWf3a0ZfqXHirQOv6RXLjtwOU/rdRgjIpsVu105cHMpQf9z/8Jzb0wYr3R1XWVRBhZoBEfUHPWjTP5vXvGcvojpTpsEB5CeFCVG3bi/7ADzERsOtHL22+bT7zthuTE1BA3y+8r/bZHP7J7GgzxSq8nNfxd+Pndw8KHta/+pkh+CfLKEDCH2MNM6xhr0j/ZUmb1/CqEw9CSNTNKK6SE+1VRF1ypz4T7czo9cTg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=68j/3IKT8UYDTRdL5F2sak9r95PLew4cVrkXXY2qH14=; b=Ptgb01fEkVLu/LZwc6rVwW9P9akqRUutEs3tcRffCLyXbRakZNnVUABReMtCLwT9jEowqxOxKoN7jZz36RkNM80U/w7clpH1mZzLjmLQR3+mbakn1/0MtMfwDXDJ60mherlA86M9ybJTVxL2tbvWIBGi+dMwaauUPaGtygvxaiE= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=os.amperecomputing.com; Received: from DM6PR01MB5849.prod.exchangelabs.com (2603:10b6:5:205::20) by DM6PR01MB5513.prod.exchangelabs.com (2603:10b6:5:151::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4500.17; Wed, 15 Sep 2021 15:59:26 +0000 Received: from DM6PR01MB5849.prod.exchangelabs.com ([fe80::8eb:704f:2ba7:9bc3]) by DM6PR01MB5849.prod.exchangelabs.com ([fe80::8eb:704f:2ba7:9bc3%4]) with mapi id 15.20.4523.014; Wed, 15 Sep 2021 15:59:26 +0000 From: "Nhi Pham" To: devel@edk2.groups.io CC: patches@amperecomputing.com, nhi@os.amperecomputing.com, vunguyen@os.amperecomputing.com, Thang Nguyen , Chuong Tran , Phong Vo , Leif Lindholm , Michael D Kinney , Ard Biesheuvel , Nate DeSimone Subject: [PATCH v3 04/28] AmperePlatformPkg: Add FailSafe and WDT support Date: Wed, 15 Sep 2021 22:55:03 +0700 Message-ID: <20210915155527.8176-5-nhi@os.amperecomputing.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210915155527.8176-1-nhi@os.amperecomputing.com> References: <20210915155527.8176-1-nhi@os.amperecomputing.com> X-ClientProxiedBy: HK2PR0302CA0012.apcprd03.prod.outlook.com (2603:1096:202::22) To DM6PR01MB5849.prod.exchangelabs.com (2603:10b6:5:205::20) Return-Path: nhi@os.amperecomputing.com MIME-Version: 1.0 Received: from sw004.amperecomputing.com (118.69.219.201) by HK2PR0302CA0012.apcprd03.prod.outlook.com (2603:1096:202::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4544.5 via Frontend Transport; Wed, 15 Sep 2021 15:59:22 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f3a41fa2-8be2-42c9-1562-08d97861ca80 X-MS-TrafficTypeDiagnostic: DM6PR01MB5513: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6790; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6dnkhXmwkuaDrh/222xgKOrWVj8d1I14CVDgxTGsBNZXLHa5t3HPVW36/EJ7mnD13FAMQfTiOYi+uJtZr7TaB5oOeZC48yq330knimscrUMiyR/rwggw6gScF3yr4po3R6rgVkGv8u4r+pMM7SJTWeSQTQvCNDAqdpt1Fb+yI9Uyfsn6AvYi//0rQ67myZw/jl+2Z3LBdXuD5h0ivwXBD7ERBvGs50dUhzYw1Yw1kliNN/HRVQPoGMn1rJaWCqQLx4ifksIrjnSbyDi3LO6w8v2QGSebG0/mdc4hRo1OGhTYzdDsVNiOqtjcWjku3uvJo3tzyNroie8ZA1RMbYCmnnb+E9O3mo+u/87ofqla54QOSAOUWshDC9ASJPBJIuyatZefx23Uv9GnRt1bhurWR+eQ2PoaVqi/pEiyI8/lai4NMRbdnNybXWMkTLlWjXcrAjOL/8eYgEWCZSPux9479C25AzcHqG1dFUD/ijpLMjqI5ctk+mwTWMKMr9txuVOURNahTLb6dXkQck4uITj1qsdBWcwBK5PY9ePFsAQ0c1aJX+u2gmCOJY+79tgaYuz8tm6ZyHQyAMsb4/4rXsHKRH+Az2yrYfxIgFuCK+MZAV2CFA0UeeF5vUWu3nfnGGzlAVMkTGQxQSCeLBoHAnSGfCFx4PldSOti29BEUHq3uUuly8yLu7FRQ53Y5AA/WTIqwhy3k2bJ2lLjaXKHFrb7XkTwFgRQW03Lpaxos7XV4n4= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR01MB5849.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(4636009)(376002)(136003)(366004)(396003)(39850400004)(346002)(316002)(30864003)(1076003)(5660300002)(83380400001)(54906003)(6506007)(6666004)(4326008)(8676002)(186003)(956004)(6512007)(86362001)(38100700002)(19627235002)(6486002)(38350700002)(478600001)(66946007)(66476007)(66556008)(2616005)(26005)(6916009)(8936002)(52116002)(2906002)(44824005);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?wsQw17bW8+4nZ1P708s8dj9AlhlqwG36KsAX3Y3myk8uh+jzsduX4uODhAkx?= =?us-ascii?Q?bU1+nQvcBdtvb0wRXV6hFrDYh83DYLHKQmBXb6kmf53Hz9Q1RrIyzEl+HWh8?= =?us-ascii?Q?UJOEwGlq/bdVTDmnAwWNbEbNknXHmirr3ZgjP4zSaMEz4/z67msxJFuF+f2T?= =?us-ascii?Q?qj46qyqTWu/Pm6LWjbzcqNXeuEAkcmGw5vllJ3xjpv22VevA9oxZ1i4oo6Lr?= =?us-ascii?Q?W54DsB4xKbxQQXROoXy9b02ZaBvFfPu+Wt6LzgANsHqo4L1SWvxTZQIQatd0?= =?us-ascii?Q?NS7M2IkcuJrR35bw/jSchMVMk8XsZAGhbFjhBJH9t+Thrty7J2YvH4z/4RQ3?= =?us-ascii?Q?2j+yMvNe+ggstC035ejskLA9d+b5WDBSG5TyQCptL1AlQTZUQ48n6PF6v9Fc?= =?us-ascii?Q?ij/sBeIb5NnvMEdaVhyZgGxhIOTeI+qfhT3gzmqne+0tLsOlQtTBhIVP6hbi?= =?us-ascii?Q?iZXWZi2NnlN16JoI7Pn3dE00dcnOE5vZzmZN/iThCIkQzzUJsAlN0uZ1EXTf?= =?us-ascii?Q?7ZsysjwEUP06s7vjHciG/KiYnMXt8RAaxkvi+d9MmTNGbPhi/aeazS17uzOW?= =?us-ascii?Q?LhP9ghNZy8J+S/0YNsW+J0VHJXG4qKHO4TxeetklMP47pb/b+4ZQeaoQFLqF?= =?us-ascii?Q?sucJtWRK+psCnpIJScLZpo8OOHZCmYF6qQVSnBZViAv4Jemz8vGa8wzMeoDx?= =?us-ascii?Q?YDN6UGhSImt2TtePTVxe7eOnBCUW5D3o0vEwgbsmxCywb68mkf9rzhDRVso/?= =?us-ascii?Q?aZlQL2ElwmaKbaDE3pmwUIm1cmeVc9ILOLmBv2lVZIe9bjdS2ylUOAYVSLq6?= =?us-ascii?Q?cIg+PDgS/PdpYt/p0YQqOKlRXWb//STUIajReOOpEfJpI4v1MfI+N2j8UCt1?= =?us-ascii?Q?jGIKqNqYCtmEN0jDC75uF8XOXINddo/ZKneAHxGIcZfVcP9LAjVtPc93WSJP?= =?us-ascii?Q?PGcsQvIHtvM3a4lD+h0aIP2iKhzi5VQ35qprpYfabban7pKnDcSoqtSng+pn?= =?us-ascii?Q?NKdryw9jN/eeWnehQXNqui76TLLPJH7KGHgF9rysmcimcN3qv/jLPHUptsZZ?= =?us-ascii?Q?vLb8og9pmKpYvKBkzbj3MtARhw6W9/Kv1OCuWVe1u0qdymyGYTFcn4qyqK6Q?= =?us-ascii?Q?gaWsS/mCPEYphSfcwVcwJxyHxl+zQFmdz1R11MLUmPN18gLq1VaewMKf4f+a?= =?us-ascii?Q?lVQf+kHJFNpjglRiw6ejIj7X/0L5VidjWBvgy7IK8pVCiXhgBT/dSaBSOPHG?= =?us-ascii?Q?0vtYgFuNvKHSdrGEj63Uy2ZO21koirSBxqv9+i/Gcr+Yf2mu70xTji4Td+UE?= =?us-ascii?Q?h7i/9QgAIUOaX0CSadqjVSuD?= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: f3a41fa2-8be2-42c9-1562-08d97861ca80 X-MS-Exchange-CrossTenant-AuthSource: DM6PR01MB5849.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Sep 2021 15:59:25.9255 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: liVBCYGGakg9S2Eqlg/mGnG3qAZ5ikcWqNPuVEDXC2t5iaK1gqDYzyXOfg3DpTl2nGT/yWhFFpdGnRmX20uVQ2aaYmevmS7nUrG85ahBC3E= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR01MB5513 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain The FailSafeDxe is a driver for the FailSafe feature which reverts the system's configuration to known good values if the system fails to boot up multiple times. Also, this driver implements the Watchdog Timer Architectural Protocol to reset the system if it hangs, which is implemented by the MdeModulePkg/Universal/WatchdogTimerDxe module. So, the WDT is now used exclusively by the FailSafeDxe. By default, when system starts, it configures the secure watchdog timer with a default value of 5 minutes. If the system boots up cleanly to the considered good stage, the counter is cleared as it indicates FailSafe monitor (ATF) that has booted up successfully. If the timer expires, it is considered a failed boot and the system is rebooted. Cc: Thang Nguyen Cc: Chuong Tran Cc: Phong Vo Cc: Leif Lindholm Cc: Michael D Kinney Cc: Ard Biesheuvel Cc: Nate DeSimone Signed-off-by: Nhi Pham --- Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc | = 1 - Platform/Ampere/JadePkg/Jade.dsc | = 9 + Platform/Ampere/JadePkg/Jade.fdf | = 6 +- Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/FailSafeDxe.inf | 5= 4 +++ Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/FailSafe.h | 2= 0 ++ Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/Watchdog.h | 2= 9 ++ Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/FailSafeDxe.c | 18= 4 ++++++++++ Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/Watchdog.c | 35= 7 ++++++++++++++++++++ 8 files changed, 658 insertions(+), 2 deletions(-) diff --git a/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc b/Silicon= /Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc index 7dadf15b7825..89c143cd28e6 100644 --- a/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc +++ b/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc @@ -593,7 +593,6 @@ [Components.common] # Timer # ArmPkg/Drivers/TimerDxe/TimerDxe.inf - MdeModulePkg/Universal/WatchdogTimerDxe/WatchdogTimer.inf =20 # # ARM GIC Dxe diff --git a/Platform/Ampere/JadePkg/Jade.dsc b/Platform/Ampere/JadePkg/Jad= e.dsc index 1e580b5c4904..0681835dd175 100644 --- a/Platform/Ampere/JadePkg/Jade.dsc +++ b/Platform/Ampere/JadePkg/Jade.dsc @@ -77,6 +77,11 @@ [LibraryClasses] # RealTimeClockLib|EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualReal= TimeClockLib.inf =20 + # + # Library for FailSafe support + # + FailSafeLib|Platform/Ampere/AmperePlatformPkg/Library/FailSafeLib/FailSa= feLib.inf + ##########################################################################= ###### # # Specific Platform Pcds @@ -100,3 +105,7 @@ [PcdsFixedAtBuild.common] # ##########################################################################= ###### [Components.common] + # + # FailSafe and Watchdog Timer + # + Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/FailSafeDxe.inf diff --git a/Platform/Ampere/JadePkg/Jade.fdf b/Platform/Ampere/JadePkg/Jad= e.fdf index 905289844378..80a86d7c1156 100644 --- a/Platform/Ampere/JadePkg/Jade.fdf +++ b/Platform/Ampere/JadePkg/Jade.fdf @@ -185,7 +185,11 @@ [FV.FvMain] # Timer # INF ArmPkg/Drivers/TimerDxe/TimerDxe.inf - INF MdeModulePkg/Universal/WatchdogTimerDxe/WatchdogTimer.inf + + # + # FailSafe and Watchdog Timer + # + INF Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/FailSafeDxe.in= f =20 # # ARM GIC Dxe diff --git a/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/FailSafe= Dxe.inf b/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/FailSafeDxe= .inf new file mode 100644 index 000000000000..60de10c95c85 --- /dev/null +++ b/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/FailSafeDxe.inf @@ -0,0 +1,54 @@ +## @file +# +# Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x0001001B + BASE_NAME =3D FailSafeDxe + FILE_GUID =3D 7BC4F970-B1CF-11E6-80F5-76304DEC7EB7 + MODULE_TYPE =3D DXE_DRIVER + VERSION_STRING =3D 1.0 + ENTRY_POINT =3D FailSafeDxeEntryPoint + +[Sources] + FailSafe.h + FailSafeDxe.c + Watchdog.c + Watchdog.h + +[Packages] + ArmPkg/ArmPkg.dec + ArmPlatformPkg/ArmPlatformPkg.dec + EmbeddedPkg/EmbeddedPkg.dec + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + Platform/Ampere/AmperePlatformPkg/AmperePlatformPkg.dec + Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dec + Silicon/Ampere/AmpereSiliconPkg/AmpereSiliconPkg.dec + +[LibraryClasses] + ArmSmcLib + DebugLib + FailSafeLib + NVParamLib + PcdLib + TimerLib + UefiBootServicesTableLib + UefiDriverEntryPoint + UefiLib + UefiRuntimeServicesTableLib + +[Pcd] + gArmTokenSpaceGuid.PcdGenericWatchdogControlBase + gArmTokenSpaceGuid.PcdGenericWatchdogEl2IntrNum + +[Protocols] + gEfiWatchdogTimerArchProtocolGuid ## PRODUCES + gHardwareInterrupt2ProtocolGuid ## CONSUMES + +[Depex] + gHardwareInterrupt2ProtocolGuid diff --git a/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/FailSafe= .h b/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/FailSafe.h new file mode 100644 index 000000000000..8bf3a98f1d8e --- /dev/null +++ b/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/FailSafe.h @@ -0,0 +1,20 @@ +/** @file + + Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef FAILSAFE_H_ +#define FAILSAFE_H_ + +#include + +BOOLEAN +EFIAPI +IsFailSafeOff ( + VOID + ); + +#endif /* FAILSAFE_H_ */ diff --git a/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/Watchdog= .h b/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/Watchdog.h new file mode 100644 index 000000000000..6c9106fdbea5 --- /dev/null +++ b/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/Watchdog.h @@ -0,0 +1,29 @@ +/** @file + + Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef GENERIC_WATCHDOG_H_ +#define GENERIC_WATCHDOG_H_ + +#include + +/* The number of 100ns periods (the unit of time passed to these functions= ) + in a second */ +#define TIME_UNITS_PER_SECOND 10000000 + +/** + The function to install Watchdog timer protocol to the system + + @retval Return EFI_SUCCESS if install Watchdog timer protocol s= uccessfully. + **/ +EFI_STATUS +EFIAPI +WatchdogTimerInstallProtocol ( + EFI_WATCHDOG_TIMER_ARCH_PROTOCOL **WatchdogTimerProtocol + ); + +#endif /* GENERIC_WATCHDOG_H_ */ diff --git a/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/FailSafe= Dxe.c b/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/FailSafeDxe.c new file mode 100644 index 000000000000..4e4aab9e5353 --- /dev/null +++ b/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/FailSafeDxe.c @@ -0,0 +1,184 @@ +/** @file + + Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "FailSafe.h" +#include "Watchdog.h" + +STATIC UINTN gWatchdogOSTimeout; +STATIC BOOLEAN gFailSafeOff; +STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *gWatchdogTimer; + +EFI_STATUS +EFIAPI +FailSafeTestBootFailure ( + VOID + ); + +STATIC VOID +FailSafeTurnOff ( + VOID + ) +{ + EFI_STATUS Status; + + if (IsFailSafeOff ()) { + return; + } + + Status =3D FailSafeBootSuccessfully (); + ASSERT_EFI_ERROR (Status); + + gFailSafeOff =3D TRUE; + + /* Disable Watchdog timer */ + gWatchdogTimer->SetTimerPeriod (gWatchdogTimer, 0); +} + +BOOLEAN +EFIAPI +IsFailSafeOff ( + VOID + ) +{ + return gFailSafeOff; +} + +/** + The function to disable Watchdog timer when enter Setup screen + **/ +VOID +WdtTimerEnterSetupScreenCallback ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + /* Make sure FailSafe is turned off */ + FailSafeTurnOff (); +} + +/** + The function to refresh Watchdog timer in the event before booting + **/ +VOID +WdtTimerBeforeBootCallback ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + /* + * At this point, the system is considered boot successfully to BIOS + */ + FailSafeTurnOff (); + + /* + * It is BIOS's responsibility to setup Watchdog when load an EFI applic= ation + * after this step + */ +} + +/** + The function to refresh Watchdog timer in the event before exiting boot = services + **/ +VOID +WdtTimerExitBootServiceCallback ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + + /* Enable Watchdog timer for OS booting */ + if (gWatchdogOSTimeout !=3D 0) { + gWatchdogTimer->SetTimerPeriod ( + gWatchdogTimer, + gWatchdogOSTimeout * TIME_UNITS_PER_SECOND + ); + } else { + /* Disable Watchdog timer */ + gWatchdogTimer->SetTimerPeriod (gWatchdogTimer, 0); + } +} + +/** + This function is a hook called when user loads the manufacturing + or optimal defaults. + + @param Defaults : (NVRAM_VARIABLE *)optimal or manufacturing + @Data : Messagebox + + @retval VOID +**/ +VOID +LoadNVRAMDefaultConfig ( + IN VOID *Defaults, + IN UINTN Data + ) +{ + NVParamClrAll (); +} + +/** + Main entry for this driver. + + @param ImageHandle Image handle this driver. + @param SystemTable Pointer to SystemTable. + + @retval EFI_SUCESS This function always complete successfully. + +**/ +EFI_STATUS +EFIAPI +FailSafeDxeEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_EVENT ExitBootServicesEvent; + EFI_STATUS Status; + + gFailSafeOff =3D FALSE; + + FailSafeTestBootFailure (); + + /* We need to setup non secure Watchdog to ensure that the system will + * boot to OS successfully. + * + * The BIOS doesn't handle Watchdog interrupt so we expect WS1 asserted = EL3 + * when Watchdog timeout triggered + */ + + Status =3D WatchdogTimerInstallProtocol (&gWatchdogTimer); + ASSERT_EFI_ERROR (Status); + + // We should register a callback function before entering to Setup scree= n + // rather than always call it at DXE phase. + FailSafeTurnOff (); + + /* Register event before exit boot services */ + Status =3D gBS->CreateEvent ( + EVT_SIGNAL_EXIT_BOOT_SERVICES, + TPL_NOTIFY, + WdtTimerExitBootServiceCallback, + NULL, + &ExitBootServicesEvent + ); + ASSERT_EFI_ERROR (Status); + + return Status; +} diff --git a/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/Watchdog= .c b/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/Watchdog.c new file mode 100644 index 000000000000..34329d04206a --- /dev/null +++ b/Platform/Ampere/AmperePlatformPkg/Drivers/FailSafeDxe/Watchdog.c @@ -0,0 +1,357 @@ +/** @file + + Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "FailSafe.h" +#include "Watchdog.h" + +/* Watchdog timer controller registers */ +#define WDT_CTRL_BASE_REG FixedPcdGet64 (PcdGenericWat= chdogControlBase) +#define WDT_CTRL_WCS_OFF 0x0 +#define WDT_CTRL_WCS_ENABLE_MASK 0x1 +#define WDT_CTRL_WOR_OFF 0x8 +#define WDT_CTRL_WCV_OFF 0x10 +#define WS0_INTERRUPT_SOURCE FixedPcdGet32 (PcdGenericWat= chdogEl2IntrNum) + +STATIC UINT64 mNumTimerTicks; +STATIC EFI_HARDWARE_INTERRUPT2_PROTOCOL *mInterruptProtocol; +BOOLEAN mInterruptWS0Enabled; + +STATIC +VOID +WatchdogTimerWriteOffsetRegister ( + UINT32 Value + ) +{ + MmioWrite32 (WDT_CTRL_BASE_REG + WDT_CTRL_WOR_OFF, Value); +} + +STATIC +VOID +WatchdogTimerWriteCompareRegister ( + UINT64 Value + ) +{ + MmioWrite64 (WDT_CTRL_BASE_REG + WDT_CTRL_WCV_OFF, Value); +} + +STATIC +EFI_STATUS +WatchdogTimerEnable ( + IN BOOLEAN Enable + ) +{ + UINT32 Val =3D MmioRead32 ((UINTN)(WDT_CTRL_BASE_REG + WDT_CTRL_WCS_OFF= )); + + if (Enable) { + Val |=3D WDT_CTRL_WCS_ENABLE_MASK; + } else { + Val &=3D ~WDT_CTRL_WCS_ENABLE_MASK; + } + MmioWrite32 ((UINTN)(WDT_CTRL_BASE_REG + WDT_CTRL_WCS_OFF), Val); + + return EFI_SUCCESS; +} + +STATIC +EFI_STATUS +WatchdogTimerSetup ( + VOID + ) +{ + EFI_STATUS Status; + + /* Disable Watchdog timer */ + WatchdogTimerEnable (FALSE); + + if (!mInterruptWS0Enabled) { + Status =3D mInterruptProtocol->EnableInterruptSource ( + mInterruptProtocol, + WS0_INTERRUPT_SOURCE + ); + ASSERT_EFI_ERROR (Status); + + mInterruptWS0Enabled =3D TRUE; + } + + if (mNumTimerTicks =3D=3D 0) { + return EFI_SUCCESS; + } + + /* If the number of required ticks is greater than the max the Watchdog'= s + offset register (WOR) can hold, we need to manually compute and set + the compare register (WCV) */ + if (mNumTimerTicks > MAX_UINT32) { + /* We need to enable the Watchdog *before* writing to the compare regi= ster, + because enabling the Watchdog causes an "explicit refresh", which + clobbers the compare register (WCV). In order to make sure this doe= sn't + trigger an interrupt, set the offset to max. */ + WatchdogTimerWriteOffsetRegister (MAX_UINT32); + WatchdogTimerEnable (TRUE); + WatchdogTimerWriteCompareRegister (ArmGenericTimerGetSystemCount () + = mNumTimerTicks); + } else { + WatchdogTimerWriteOffsetRegister ((UINT32)mNumTimerTicks); + WatchdogTimerEnable (TRUE); + } + + return EFI_SUCCESS; +} + + +/* This function is called when the Watchdog's first signal (WS0) goes hig= h. + It uses the ResetSystem Runtime Service to reset the board. +*/ +VOID +EFIAPI +WatchdogTimerInterruptHandler ( + IN HARDWARE_INTERRUPT_SOURCE Source, + IN EFI_SYSTEM_CONTEXT SystemContext + ) +{ + STATIC CONST CHAR16 ResetString[]=3D L"The generic Watchdog timer ran ou= t."; + + mInterruptProtocol->EndOfInterrupt (mInterruptProtocol, Source); + + if (!IsFailSafeOff ()) { + /* Not handling interrupt as ATF is monitoring it */ + return; + } + + WatchdogTimerEnable (FALSE); + + gRT->ResetSystem ( + EfiResetCold, + EFI_TIMEOUT, + StrSize (ResetString), + (VOID *)&ResetString + ); + + /* If we got here then the reset didn't work */ + ASSERT (FALSE); +} + +/** + This function registers the handler NotifyFunction so it is called every= time + the Watchdog timer expires. It also passes the amount of time since the= last + handler call to the NotifyFunction. + If NotifyFunction is not NULL and a handler is not already registered, + then the new handler is registered and EFI_SUCCESS is returned. + If NotifyFunction is NULL, and a handler is already registered, + then that handler is unregistered. + If an attempt is made to register a handler when a handler is already + registered, then EFI_ALREADY_STARTED is returned. + If an attempt is made to unregister a handler when a handler is not + registered, then EFI_INVALID_PARAMETER is returned. + + @param This The EFI_TIMER_ARCH_PROTOCOL instance. + @param NotifyFunction The function to call when a timer interrupt fir= es. + This function executes at TPL_HIGH_LEVEL. The D= XE + Core will register a handler for the timer inte= rrupt, + so it can know how much time has passed. This + information is used to signal timer based event= s. + NULL will unregister the handler. + + @retval EFI_UNSUPPORTED The code does not support NotifyFunction. + +**/ +EFI_STATUS +EFIAPI +WatchdogTimerRegisterHandler ( + IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This, + IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction + ) +{ + /* Not support. Watchdog will reset the board */ + return EFI_UNSUPPORTED; +} + +/** + This function sets the amount of time to wait before firing the Watchdog + timer to TimerPeriod 100ns units. If TimerPeriod is 0, then the Watchdo= g + timer is disabled. + + @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance. + @param TimerPeriod The amount of time in 100ns units to wait befor= e + the Watchdog timer is fired. If TimerPeriod is = zero, + then the Watchdog timer is disabled. + + @retval EFI_SUCCESS The Watchdog timer has been programmed to = fire + in Time 100ns units. + @retval EFI_DEVICE_ERROR A Watchdog timer could not be programmed d= ue + to a device error. + +**/ +EFI_STATUS +EFIAPI +WatchdogTimerSetPeriod ( + IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This, + IN UINT64 TimerPeriod // In 100ns unit= s + ) +{ + mNumTimerTicks =3D (ArmGenericTimerGetTimerFreq () * TimerPeriod) / TIM= E_UNITS_PER_SECOND; + + if (!IsFailSafeOff ()) { + /* Not support Watchdog timer service until FailSafe is off as ATF is = monitoring it */ + return EFI_SUCCESS; + } + + return WatchdogTimerSetup (); +} + +/** + This function retrieves the period of timer interrupts in 100ns units, + returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPer= iod + is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 = is + returned, then the timer is currently disabled. + + @param This The EFI_TIMER_ARCH_PROTOCOL instance. + @param TimerPeriod A pointer to the timer period to retrieve in + 100ns units. If 0 is returned, then the timer i= s + currently disabled. + + + @retval EFI_SUCCESS The timer period was returned in TimerPeri= od. + @retval EFI_INVALID_PARAMETER TimerPeriod is NULL. + +**/ +EFI_STATUS +EFIAPI +WatchdogTimerGetPeriod ( + IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This, + OUT UINT64 *TimerPeriod + ) +{ + if (TimerPeriod =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + *TimerPeriod =3D ((TIME_UNITS_PER_SECOND / ArmGenericTimerGetTimerFreq (= )) * mNumTimerTicks); + + return EFI_SUCCESS; +} + +/** + Interface structure for the Watchdog Architectural Protocol. + + @par Protocol Description: + This protocol provides a service to set the amount of time to wait + before firing the Watchdog timer, and it also provides a service to + register a handler that is invoked when the Watchdog timer fires. + + @par When the Watchdog timer fires, control will be passed to a handler + if one has been registered. If no handler has been registered, + or the registered handler returns, then the system will be + reset by calling the Runtime Service ResetSystem(). + + @param RegisterHandler + Registers a handler that will be called each time the + Watchdogtimer interrupt fires. TimerPeriod defines the minimum + time between timer interrupts, so TimerPeriod will also + be the minimum time between calls to the registered + handler. + NOTE: If the Watchdog resets the system in hardware, then + this function will not have any chance of executing. + + @param SetTimerPeriod + Sets the period of the timer interrupt in 100ns units. + This function is optional, and may return EFI_UNSUPPORTED. + If this function is supported, then the timer period will + be rounded up to the nearest supported timer period. + + @param GetTimerPeriod + Retrieves the period of the timer interrupt in 100ns units. + +**/ +STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL gWatchdogTimer =3D { + (EFI_WATCHDOG_TIMER_REGISTER_HANDLER)WatchdogTimerRegisterHandler, + (EFI_WATCHDOG_TIMER_SET_TIMER_PERIOD)WatchdogTimerSetPeriod, + (EFI_WATCHDOG_TIMER_GET_TIMER_PERIOD)WatchdogTimerGetPeriod +}; + +EFI_STATUS +EFIAPI +WatchdogTimerInstallProtocol ( + EFI_WATCHDOG_TIMER_ARCH_PROTOCOL **WatchdogTimerProtocol + ) +{ + EFI_STATUS Status; + EFI_HANDLE Handle; + EFI_TPL CurrentTpl; + + /* Make sure the Watchdog Timer Architectural Protocol has not been inst= alled + in the system yet. + This will avoid conflicts with the universal Watchdog */ + ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolG= uid); + + ASSERT (ArmGenericTimerGetTimerFreq () !=3D 0); + + /* Install interrupt handler */ + Status =3D gBS->LocateProtocol ( + &gHardwareInterrupt2ProtocolGuid, + NULL, + (VOID **)&mInterruptProtocol + ); + ASSERT_EFI_ERROR (Status); + + /* + * We don't want to be interrupted while registering Watchdog interrupt = source as the interrupt + * may be trigger in the middle because the interrupt line already enabl= ed in the EL3. + */ + CurrentTpl =3D gBS->RaiseTPL (TPL_HIGH_LEVEL); + + Status =3D mInterruptProtocol->RegisterInterruptSource ( + mInterruptProtocol, + WS0_INTERRUPT_SOURCE, + WatchdogTimerInterruptHandler + ); + ASSERT_EFI_ERROR (Status); + + /* Don't enable interrupt until FailSafe off */ + mInterruptWS0Enabled =3D FALSE; + Status =3D mInterruptProtocol->DisableInterruptSource ( + mInterruptProtocol, + WS0_INTERRUPT_SOURCE + ); + ASSERT_EFI_ERROR (Status); + + gBS->RestoreTPL (CurrentTpl); + + Status =3D mInterruptProtocol->SetTriggerType ( + mInterruptProtocol, + WS0_INTERRUPT_SOURCE, + EFI_HARDWARE_INTERRUPT2_TRIGGER_LEVEL_HIG= H + ); + ASSERT_EFI_ERROR (Status); + + /* Install the Timer Architectural Protocol onto a new handle */ + Handle =3D NULL; + Status =3D gBS->InstallMultipleProtocolInterfaces ( + &Handle, + &gEfiWatchdogTimerArchProtocolGuid, + &gWatchdogTimer, + NULL + ); + ASSERT_EFI_ERROR (Status); + + mNumTimerTicks =3D 0; + + if (WatchdogTimerProtocol !=3D NULL) { + *WatchdogTimerProtocol =3D &gWatchdogTimer; + } + + return Status; +} --=20 2.17.1