From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (NAM11-CO1-obe.outbound.protection.outlook.com [40.107.220.91]) by mx.groups.io with SMTP id smtpd.web12.5456.1622023885191837331 for ; Wed, 26 May 2021 03:11:25 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@os.amperecomputing.com header.s=selector2 header.b=avFwT65F; spf=pass (domain: os.amperecomputing.com, ip: 40.107.220.91, mailfrom: nhi@os.amperecomputing.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PC4YuMgpxb+IzjIgkERIgPF7WxlaXAmaQXgDk9gzQ+VxUBHyHK4/ybqkiMoO6rjmBo2Ph3Y09P+R3OqHr3eCwjO39CZioMn5FutnZaySQrqijsJ3aCiPlr9sYsXeMU8R5BwKi44IJSS8yygFGNXPvdKdqlKuwVe+PakjTT+xBNYtILKVc58R4lWD17/sh8qe0sAHa12q2i91apAHuCGvAZ2LUMtc2hTdQI0+zEp52faLhJk/2iZvSxBWGLCue0OqqK8O/dMdt5l1o0o2Isa2pTXO5dhYCnuknRcK6tdR4TF4ScOVnMC+7P8391Dj3iNfHd7daEJ/v/dTfL0q45JzJg== 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:X-MS-Exchange-SenderADCheck; bh=DB1QJwAN7Rd9lJ0/fJrJiUHNJ0GJfBya1Fci2Y3zAPc=; b=Kogc2JN53Ot8yrZ4SqiY9i5XXuDmPeykIpoFM6mNLRZl3DcR4UkmSr+H4cWUUTPrIrKIA2WhROc13f+QHc+uSy9DydlzoM/MYagmGGIPdZ2DOhSXr+JScxOTSt1rGLhFw/VbmD5GrAkib1qOTUs+G2hos4x2PXQOGK5tNyWe9FQHOWg91FXEpKsBNUrTjcQV0IsPzyQlbRLGujhy1TqCi8FhPaj0teX/uTyquaYMBK8XBNvZ2kIlCmA2x5XXnybzSLDuLdgoCm4D9RDEJY8sFb0eaEJqb6DSLVAt+28KdvaiF2potogaubA3AseZPHlA2yY9jSeDlpIN2++3D5C5RQ== 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=DB1QJwAN7Rd9lJ0/fJrJiUHNJ0GJfBya1Fci2Y3zAPc=; b=avFwT65FxEPLigB0hqlew7I82BjbGPCQXImVX6fPkHd9hWfJlGg+ZnaSDhIqNBd4UzIu8K4cRrO4pNQ6lwTW2OctJpfUdFuCoLlXKMOAqVqGgvAAdyvJyTymJyCxZQku1zmJAyt5gdio11J7KQxzkjC2UvArg7rzax/Zy4B+15o= 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 DM5PR0101MB3067.prod.exchangelabs.com (2603:10b6:4:31::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4129.28; Wed, 26 May 2021 10:11:23 +0000 Received: from DM6PR01MB5849.prod.exchangelabs.com ([fe80::60d2:86dd:1f1c:51dd]) by DM6PR01MB5849.prod.exchangelabs.com ([fe80::60d2:86dd:1f1c:51dd%7]) with mapi id 15.20.4173.020; Wed, 26 May 2021 10:11:23 +0000 From: "Nhi Pham" To: devel@edk2.groups.io CC: Nhi Pham , Thang Nguyen , Chuong Tran , Phong Vo , Leif Lindholm , Michael D Kinney , Ard Biesheuvel , Nate DeSimone Subject: [edk2-platforms][PATCH v2 04/32] AmperePlatformPkg: Add FailSafe and WDT support Date: Wed, 26 May 2021 17:06:56 +0700 Message-ID: <20210526100724.5359-6-nhi@os.amperecomputing.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210526100724.5359-1-nhi@os.amperecomputing.com> References: <20210526100724.5359-1-nhi@os.amperecomputing.com> X-Originating-IP: [118.69.219.201] X-ClientProxiedBy: HKAPR04CA0008.apcprd04.prod.outlook.com (2603:1096:203:d0::18) To DM6PR01MB5849.prod.exchangelabs.com (2603:10b6:5:205::20) Return-Path: nhi@os.amperecomputing.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (118.69.219.201) by HKAPR04CA0008.apcprd04.prod.outlook.com (2603:1096:203:d0::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4173.20 via Frontend Transport; Wed, 26 May 2021 10:11:20 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 82460329-d8f3-427a-f8dc-08d9202e9d6f X-MS-TrafficTypeDiagnostic: DM5PR0101MB3067: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6790; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: m2Fi/0fOs6qH814e8peWnv4C2Viz4zHUa2CJf/JEhe+7GScPFKx/cYG9ppkfjvySrXBDMCt+73rmCaaQwmT+XvgT4DTfnk/PblnDw8FG9p+/B4NIuhHyKJU6cjI4uTVnxNiNxsgtB5Ogqkbznra7Hj3shdqwUOCMHZfSfnVB/0nilFkmoOMmrxW5vviAKDfydCqvygrbTiq09kYQE1tw1WCEMUwL6aLRN2PZGQ7XzpeNyFW4aGOYGsBXI3RlVGOzcTgUEBtNRCYcMgC2OkPBjG9IZlH4dyiYALVM7C8mHW0rPU+huaLArmtkid4nvcXXTJpJIhzr12QoNZPYnmbTZqOUUqTBCjHRhFHe5NtKyORKmM/GvmEmi43avi4SVQgk1pfo3ZV+C8yMcbkwFjH+sRngEiKM+6kLDcHNTCwI6G5Q0u8GGJHb9YaxFGbTkKy4fLWrsJdGu3W+siaZ4lX1E6wrCIwndBvMHWRTqlFJIdc8RUeEuPRL8pl377izbGvY0zq4WFlvRcmUGhe1uqeb4BLPu9GT0qvMGwXqs1LCx5k7q4xBXncz2RuBEU3frMDuybruhqLqh8WfTLyo5UtaNIONzHHy74X+VtVLmUX4/I+ORagjp5k0pRRs+RhmPawc14tZN1cR1sfc2x1c7F3wHxHk+NHSYfsBEAN57UMmocDK060RXP/Y7cPvlHvUc3PidFtUgLKJYo3NNRfo7ucHnA== 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)(396003)(39840400004)(346002)(136003)(376002)(366004)(4326008)(26005)(19627235002)(6666004)(1076003)(2616005)(38350700002)(6506007)(83380400001)(86362001)(2906002)(6486002)(8676002)(30864003)(956004)(52116002)(8936002)(186003)(54906003)(66556008)(6916009)(66476007)(6512007)(66946007)(38100700002)(5660300002)(16526019)(478600001)(316002)(69590400013)(44824005);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?IUBUph+wcnwesDyPfZZnyrVPjElKokVat8UjpsHCIaykNTlHmP8seUhIDVDN?= =?us-ascii?Q?vsczkqH9U+IdVN0/X+x2co1hAc3ofrZVB06NMqTBGHEqQE2tQ1ordFO8OJMc?= =?us-ascii?Q?DYZDTOP6GdI5NemieKqcuZVRsgWvg6kh20Y8w1wRDrDDpdFezio8yeAHKrVe?= =?us-ascii?Q?Yt+CVHRCvF4f2NMiZZEPzY8H0oIiLLbVXBJbjz0lYRTYZ6ARvZKWMGB3kzQ3?= =?us-ascii?Q?X/rTsymib/rYwu01F/wH1873J/YeylDdlbDMjRL0epUX+PElLIDJIxAlw1Ca?= =?us-ascii?Q?X0s+FrVOKN2LOB82ZT742pCI7lzy6a1M0jWkT6wmbfCLEpkZJA9oIlctKC1d?= =?us-ascii?Q?TOjKyH6h9gc9KbU6SWoDwB5GVOkMp514zOpDUeJMEJVuAo+j8xTagd1BPP6D?= =?us-ascii?Q?E0zb+VobB/SPTJn9Wd8hDykMephMemH5uizFnQq5tZT6z1Jd5cZ+rqBUG0BJ?= =?us-ascii?Q?wuIsq2+iE0rDYX6eGc9pGOAVxUz1I85GXOKlInn6ffr4A3s68Ue2l5v07uY+?= =?us-ascii?Q?IEzWR6BpGHpCkgXke52jE2mTIw8OyNIA++wr9OK0mmxcUs1mLT7Pl82t6oSP?= =?us-ascii?Q?+A9sCTSxefXaJbAPJBYJfHINRMGUDm7Y4xQakzakxD0T8sGkZgNv+L67Q0Sk?= =?us-ascii?Q?dhdgafUogDW5S8jJzY+MAKNmfsbpXut6ksdxqXY5snKzKId2iKT7LW0EbzWF?= =?us-ascii?Q?z+b0xkSKOg1I0X/UKQnBPbvs5CPnEhxDj4pQgIsYYg+pWBP/vbXdBLudqx1/?= =?us-ascii?Q?c0h2IEoYaFvDKcW9TnJdyElo1EGV21gU0xjl+aW9h3RWdj79mqkSHYNrMX7Y?= =?us-ascii?Q?E1ljXr/UVnC+Hko0wf5/8FcSamtUmnkvwKWJ+2pYGrzLUp/zZisOElPAtAa4?= =?us-ascii?Q?4E5rgwADUoRxnq5KjgZanzCWzeUEGufNlXhg9J2G75xlRHyCKiFj+3NZCsp3?= =?us-ascii?Q?8YJxuYAqwo7oQ33+QrW3hqqaKmOfHGFVtVYMPfF2texlzxgzt3SXMRFzAoyr?= =?us-ascii?Q?mwLQy3wAhBPC55oJjZYQOgUGmcG60FwlsKINAV9Tv1ZohLmkH4FbNUw6ER8J?= =?us-ascii?Q?VbxMIDj2DvpyfizxOadl4lePpYYHijohx+PqgXn9UPxOuLX/nBlmj2FByXlM?= =?us-ascii?Q?Y2tVX5tCD0cMn45Jzi7YMkpF074cb9G/Z8F4bbml4NE6BETowvMoykawFjnT?= =?us-ascii?Q?wV2ZL6+owLXAzdTpWIXwRYhLdx2cori+qStxdzr7EkPDjGvOOFDNBtNMPdk1?= =?us-ascii?Q?KfxhNqvKGR9omu1rV79qhiDftrOmphlv/CsD7+IFxFtoc4XXcEmBsT+Na7jZ?= =?us-ascii?Q?4BMHdYYMa6XEdcy5yWQx0GTb?= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 82460329-d8f3-427a-f8dc-08d9202e9d6f X-MS-Exchange-CrossTenant-AuthSource: DM6PR01MB5849.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 May 2021 10:11:23.4839 (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: AVNxftXjlvGRSLPwhbFLQ+POkBK0Z6q5oH7BPUUwgoz/AqpLfvSu9XLaftCGEqpz8CFBLcXTXsSfi+LUwEVVdECxWJuuNf+/kpCDkO+j63I= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR0101MB3067 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain The FailSafeDxe driver reverts the system's configuration to known good values if the system fails to boot up multiple times. It also implements the Watchdog Timer Architectural Protocol to reset the system if it hangs. 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 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 0332473b59b0..6a6f72e995af 100755 --- a/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc +++ b/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc @@ -585,7 +585,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 f68af24a0d78..f92855af99ab 100755 --- a/Platform/Ampere/JadePkg/Jade.dsc +++ b/Platform/Ampere/JadePkg/Jade.dsc @@ -75,6 +75,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 @@ -98,3 +103,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 100755 --- 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 100755 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 100755 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..1b8978b12ea7 --- /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); + + // FIXME: We should register a callback function before entering to Setu= p screen + // 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