From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (NAM12-MW2-obe.outbound.protection.outlook.com [40.107.244.91]) by mx.groups.io with SMTP id smtpd.web12.4640.1634883578632999725 for ; Thu, 21 Oct 2021 23:19:38 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@os.amperecomputing.com header.s=selector2 header.b=jLwHB+Lf; spf=pass (domain: os.amperecomputing.com, ip: 40.107.244.91, mailfrom: nhi@os.amperecomputing.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=McBDszhCY+qFW4RihFsdLXoHOAzaqwAaC7m0RvCRIFHU9TuMDQI3x3W4GRK7HlMyF0SVdAvPWuoO5KdxiS2PR9LaLe/NkqCNjKoofUdFU0GtLJFHfcFu5pzC88FazMmhKUBlKvJTL4velfxmYhiVORFnUvuvYJNNTM7R9Tg0jey262u3o/olzyKIge3V3ALZ6wl0Wrpgf+F3JiBUMwvv9Vs6kTVjpO9zbuDXH6Tg00SbT/XHZnRsRcVuTEp8GC5JSN3vt6xIgqmmd0GcNkbnQy/5qv1p0nBDyJXyDn3hCpExOkQFi2Uq3+/qfKHEETdJFBqWtFiupUSLl+ua2rFiog== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=iIpyQHfUJiO3UYyQ1vabYFKb0wQ3GNdxfaN7bQoFg8U=; b=S/kpW6lMr3WHDxLn9JNIiz6OMXogAywPQD7DUUNpnPO4QNZD1srBHMt2BJTmyZ8GeCXQ+Iq9iCkBe00JsN9Yhe/B8DbmjxrpJqEafb7fboWYbMm1RjqefxZsshJEIvoKjw1BTx3ZHDwBPhIEWBWQFYi0KIJueSU1zrwSNjVsOPshFcv5+UVuMtIlnp9U+9cRgoeGNdOQV0gsiF7h1vmPi8ZLOBk774toyu8BJ7LEi3ZIT9kJZueDD/zhx+negwzK4la6emXdprHJx+RLa25yQ7wR5tzJ853IylBnBWcd/cB/cnSKumcGc+xk1/RSRin9KB0DRBhzrim6AIsw6zn8/g== 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=iIpyQHfUJiO3UYyQ1vabYFKb0wQ3GNdxfaN7bQoFg8U=; b=jLwHB+Lf+0KHJK4QO66s6LjuXyySIsYF5wh19b9sa96GlUv2u/qMTPNcRKTAaMMELz/O4LkI3dx129g2GM8LCOTp5V0uhYJRiqmGThnatmAZNoDdZDEFOcKQhBu0oYw+4N0l9p0oUSuqUdaxhK9QYS++Re8Fh/bFLUhcyLCzXQo= 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 PH0PR01MB7287.prod.exchangelabs.com (2603:10b6:510:10a::21) by PH0PR01MB6103.prod.exchangelabs.com (2603:10b6:510:13::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Fri, 22 Oct 2021 06:19:36 +0000 Received: from PH0PR01MB7287.prod.exchangelabs.com ([fe80::254c:9533:7f35:aee]) by PH0PR01MB7287.prod.exchangelabs.com ([fe80::254c:9533:7f35:aee%4]) with mapi id 15.20.4628.016; Fri, 22 Oct 2021 06:19:36 +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: [edk2-platforms][PATCH v4 03/31] AmpereAltraPkg: Add FailSafe and WDT support Date: Fri, 22 Oct 2021 13:17:41 +0700 Message-ID: <20211022061809.31087-4-nhi@os.amperecomputing.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211022061809.31087-1-nhi@os.amperecomputing.com> References: <20211022061809.31087-1-nhi@os.amperecomputing.com> X-ClientProxiedBy: HK2PR02CA0168.apcprd02.prod.outlook.com (2603:1096:201:1f::28) To PH0PR01MB7287.prod.exchangelabs.com (2603:10b6:510:10a::21) Return-Path: nhi@os.amperecomputing.com MIME-Version: 1.0 Received: from sw004.amperecomputing.com (118.69.219.201) by HK2PR02CA0168.apcprd02.prod.outlook.com (2603:1096:201:1f::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4628.16 via Frontend Transport; Fri, 22 Oct 2021 06:19:32 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 174d8e66-cd52-49d6-c27a-08d99523eb60 X-MS-TrafficTypeDiagnostic: PH0PR01MB6103: 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: kzRDtl83z4vCcU8q8WbGJEPb6KUIMigOEqjIgIVHVVbExAar0Op2/8JW8X1DMMpyQjQwONEIdo8bSDKbhoK/ptPx0QHvzl3SS9pKJU+Hs9IRVkFtE17z6fiwq2svpBSbQ3KIXIDXnJINNAC8H7+oc1LiOHE2eYRwGxJLgWDeEyJtZZiAn2WRL1ntvNEF7wr4/UG7USJF5gHzSNxxASfL1/kO0vxd7QbOluPg5dRuTXjWLQRZQ6NWUVck6XDboHFP7Z6pwyTUnOsDzQXdGX8VKuF/VJThzGIcbu4KUfiWW2IS26A/ZbW3jDmkeGLdvVlZoOqzcxMEvocw8e8h4symbQw+nGsxuXU/XNThMn528YaelpxHJ5TFjBoqcr1nDe8SvtuNulbPLG3nrlv6lFlf6amrd1wcCf3G6gWDc90LsBTWLQzbtMED7g9oj38acyjUxikfMM6/59qXlJ2uxjx1NOH0u3aM9Ebo2WWXX2i3N1GDJddfCwY5J0r2A2Q69EdYuYMYpL5onoEArui7ScbS6h8yZjSH9/o4Vf4SOozINals1iQ4FLUTNKoD8A5o0lCZJJMMazuDsi+W8bWytObqIJxF5yS6rZO2kWHMcEJ65HCdNno+YtKZfr0XB5AjTDuvezbqKViLD7R8FJbIMdw3SI+gghS+rDCaPJUEWZ5e7hndEszYUdasF3dWUJxKdV9qxj3p8nuU9dxt04PmgQFszm9cLkw33vYFglmTEXhHDBc= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PH0PR01MB7287.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(5660300002)(2906002)(86362001)(38100700002)(19627235002)(508600001)(66946007)(6916009)(316002)(186003)(66476007)(4326008)(1076003)(8936002)(956004)(2616005)(8676002)(83380400001)(6512007)(6486002)(26005)(52116002)(66556008)(30864003)(38350700002)(54906003)(6666004)(6506007)(44824005);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?hIgYUCZbg7iQORtjYD6BXj5gw6SwyiTmD9VSqNBlK0n73TE+f4U8gLFvlfA6?= =?us-ascii?Q?Wn2T3OJjx0Jf5hW4B0Y63N/oXqHXUendDEn24A7zjiie6KNklTb9C8eEfzbx?= =?us-ascii?Q?n+O01iiNnmvmqBf4uN+htquUfM2seTCjVrqvTX1U9MBwjgTRnyagafiOt8H9?= =?us-ascii?Q?UY5hd1+E6DbDzbNPZ57IlZA8F3xrigmeIGH830IZj6jOEpoIP87QZlyXRdVl?= =?us-ascii?Q?jyyKNBUh46s3FcuApJGwz7M2JPkNmoKrdXUbaAQl8XDxSVyiPd6Qm+ncR1Ey?= =?us-ascii?Q?Crt/hZ0j7Sk3ZzHAckh4nJETc1yW1Ixpvln3xxMvgihlxE2XDVInsNQqRMpa?= =?us-ascii?Q?YT4fyQcQdrcRvrHPIIBxd5DMcGcBn3YR6RrFI8hB6xDv+KHafSMvidqb2rwS?= =?us-ascii?Q?4kS88xkWw/CM9PDW50ingJy4wNgtncsA4GZdplzJXtCUpo4dSclg8kPjTu3Y?= =?us-ascii?Q?VjPI9xf5sijVm3iAgwm849DnFwlbVK/BpFgTkQpv+eEnLkIYaCq0vhlYgfFj?= =?us-ascii?Q?Y6atoWEjtIlN+uRvyd/Q3EdwMJVH85KJdJKH/gDfd1GuimRRP2NA4wgsCcZu?= =?us-ascii?Q?foebI25gSZDWKXFie6Zv6MA280eWMJBrVSv3QCrURjz7sz/tgYAaqsp5j0uj?= =?us-ascii?Q?SKbCh7WE/EX/R5ALmdmZiNO+IcptJXLuLuUyzYbEceO5kpVO95pPvRV3OD2z?= =?us-ascii?Q?A5/TmhM0cwtt/nkRUGvnqfIq85KQMvPEqapqjkU7D/V5GXm2HXXT7WWRxllR?= =?us-ascii?Q?V5oGVU+fjz5ToS3eCyzcaIPyvx/GTE6ITkvGIswsdZtv0/mxZrLH4umkc/jW?= =?us-ascii?Q?2bK7AEuPuIN54XOnGEv+u8cYWgRgsaMd9k4zmwivtu/0fW9/oXnotEbd07uo?= =?us-ascii?Q?XRz4kh72v1PfBK3TAGxPCG0EYxktJqvo9Zea01LPp7TvgCeCCvnbT5KwhxPB?= =?us-ascii?Q?W5FNsVbMNBtfB3kZJFd/wrAYRurcZldt2xq8hCMJmAaskKC0XF/SktjYmE5c?= =?us-ascii?Q?hAQ5MrwBy9EnP4fWis8V0DNZgV7HfWK2zygDG/esy86ATTsIIYvr0UgjFS+x?= =?us-ascii?Q?lbMvxPk8v9x2C7AUiWv3yUjzeL3lLpS58EcBZesqWBnUWQi+ShilVnZqn9zD?= =?us-ascii?Q?BPymmLGq1xTO8Q3kQrTB9vUXPlQz4tHlcrsgXQnIzGfo/8lKt8mE9vSOy8id?= =?us-ascii?Q?SMhmsoGcRdewIBOgmJ4ma3zc8wJ39LukblfhBJ6qNtMYAaYEr4x24z6a++vc?= =?us-ascii?Q?2YYgKbprAezJBuMKB4gOwXa58KBJ7moCJxXA+adxeIbqlA1zs9lJM77HVo5c?= =?us-ascii?Q?zq0dC3hBH9nM6XACAXJ1Nv6F?= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 174d8e66-cd52-49d6-c27a-08d99523eb60 X-MS-Exchange-CrossTenant-AuthSource: PH0PR01MB7287.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2021 06:19:35.8849 (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: nhi@amperemail.onmicrosoft.com X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR01MB6103 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 Reviewed-by: Leif Lindholm --- Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc | 6 +- Platform/Ampere/JadePkg/Jade.fdf | 6 +- Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/FailSafeDxe.inf | 51 ++= + Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/FailSafe.h | 44 ++= + Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/Watchdog.h | 29 ++ Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/FailSafeDxe.c | 243 ++= +++++++++++ Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/Watchdog.c | 357 ++= ++++++++++++++++++ 7 files changed, 734 insertions(+), 2 deletions(-) diff --git a/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc b/Silicon= /Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc index 69a6caa56752..bfe66f332c56 100644 --- a/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc +++ b/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc @@ -588,7 +588,11 @@ [Components.common] # Timer # ArmPkg/Drivers/TimerDxe/TimerDxe.inf - MdeModulePkg/Universal/WatchdogTimerDxe/WatchdogTimer.inf + + # + # FailSafe and Watchdog Timer + # + Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/FailSafeDxe.inf =20 # # ARM GIC Dxe diff --git a/Platform/Ampere/JadePkg/Jade.fdf b/Platform/Ampere/JadePkg/Jad= e.fdf index 6e228d4ecb89..49e38db1bce4 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 Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/FailSafeDxe.inf =20 # # ARM GIC Dxe diff --git a/Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/FailSafeDxe.= inf b/Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/FailSafeDxe.inf new file mode 100644 index 000000000000..cea69516d0bb --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/FailSafeDxe.inf @@ -0,0 +1,51 @@ +## @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 + Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dec + Silicon/Ampere/AmpereSiliconPkg/AmpereSiliconPkg.dec + +[LibraryClasses] + DebugLib + FlashLib + IoLib + NVParamLib + TimerLib + UefiBootServicesTableLib + UefiDriverEntryPoint + UefiLib + +[Pcd] + gArmTokenSpaceGuid.PcdGenericWatchdogControlBase + gArmTokenSpaceGuid.PcdGenericWatchdogEl2IntrNum + +[Protocols] + gEfiWatchdogTimerArchProtocolGuid ## PRODUCES + gHardwareInterrupt2ProtocolGuid ## CONSUMES + +[Depex] + gHardwareInterrupt2ProtocolGuid diff --git a/Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/FailSafe.h b= /Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/FailSafe.h new file mode 100644 index 000000000000..911b093dce28 --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/FailSafe.h @@ -0,0 +1,44 @@ +/** @file + + Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef FAILSAFE_H_ +#define FAILSAFE_H_ + +#define FAILSAFE_BOOT_NORMAL 0x00 +#define FAILSAFE_BOOT_LAST_KNOWN_SETTINGS 0x01 +#define FAILSAFE_BOOT_DEFAULT_SETTINGS 0x02 +#define FAILSAFE_BOOT_DDR_DOWNGRADE 0x03 +#define FAILSAFE_BOOT_SUCCESSFUL 0x04 + +#pragma pack(1) +typedef struct { + UINT8 ImgMajorVer; + UINT8 ImgMinorVer; + UINT32 NumRetry1; + UINT32 NumRetry2; + UINT32 MaxRetry; + UINT8 Status; + // + // Byte[3]: Reserved + // Byte[2]: Slave MCU Failure Mask + // Byte[1]: Reserved + // Byte[0]: Master MCU Failure Mask + // + UINT32 MCUFailsMask; + UINT16 CRC16; + UINT8 Reserved[3]; +} FAIL_SAFE_CONTEXT; +#pragma pack() + +BOOLEAN +EFIAPI +IsFailSafeOff ( + VOID + ); + +#endif /* FAILSAFE_H_ */ diff --git a/Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/Watchdog.h b= /Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/Watchdog.h new file mode 100644 index 000000000000..6c9106fdbea5 --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/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/Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/FailSafeDxe.= c b/Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/FailSafeDxe.c new file mode 100644 index 000000000000..487e0d3870ab --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/FailSafeDxe.c @@ -0,0 +1,243 @@ +/** @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" + +STATIC UINTN gWatchdogOSTimeout; +STATIC BOOLEAN gFailSafeOff; +STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *gWatchdogTimer; + +STATIC +INTN +CheckCrc16 ( + UINT8 *Pointer, + INTN Count + ) +{ + INTN Crc =3D 0; + INTN Index; + + while (--Count >=3D 0) { + Crc =3D Crc ^ (INTN)*Pointer++ << 8; + for (Index =3D 0; Index < 8; ++Index) { + if ((Crc & 0x8000) !=3D 0) { + Crc =3D Crc << 1 ^ 0x1021; + } else { + Crc =3D Crc << 1; + } + } + } + + return Crc & 0xFFFF; +} + +BOOLEAN +FailSafeValidCRC ( + FAIL_SAFE_CONTEXT *FailSafeBuf + ) +{ + UINT8 Valid; + UINT16 Crc; + UINT32 Len; + + Len =3D sizeof (FAIL_SAFE_CONTEXT); + Crc =3D FailSafeBuf->CRC16; + FailSafeBuf->CRC16 =3D 0; + + Valid =3D (Crc =3D=3D CheckCrc16 ((UINT8 *)FailSafeBuf, Len)); + FailSafeBuf->CRC16 =3D Crc; + + return Valid; +} + +BOOLEAN +FailSafeFailureStatus ( + UINT8 Status + ) +{ + if ((Status =3D=3D FAILSAFE_BOOT_LAST_KNOWN_SETTINGS) || + (Status =3D=3D FAILSAFE_BOOT_DEFAULT_SETTINGS) || + (Status =3D=3D FAILSAFE_BOOT_DDR_DOWNGRADE)) { + return TRUE; + } + + return FALSE; +} + +EFI_STATUS +EFIAPI +FailSafeBootSuccessfully ( + VOID + ) +{ + EFI_STATUS Status; + FAIL_SAFE_CONTEXT FailSafeBuf; + UINT32 FailSafeSize; + UINT64 FailSafeStartOffset; + + Status =3D FlashGetFailSafeInfo (&FailSafeStartOffset, &FailSafeSize); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: Failed to get context region information\n",= __FUNCTION__)); + return EFI_DEVICE_ERROR; + } + + Status =3D FlashReadCommand (FailSafeStartOffset, (UINT8 *)&FailSafeBuf,= sizeof (FAIL_SAFE_CONTEXT)); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // If failsafe context is valid, and: + // - The status indicate non-failure, then don't clear it + // - The status indicate a failure, then go and clear it + // + if (FailSafeValidCRC (&FailSafeBuf) + && !FailSafeFailureStatus (FailSafeBuf.Status)) { + return EFI_SUCCESS; + } + + Status =3D FlashEraseCommand (FailSafeStartOffset, FailSafeSize); + if (EFI_ERROR (Status)) { + return Status; + } + + return EFI_SUCCESS; +} + +EFI_STATUS +FailSafeTestBootFailure ( + VOID + ) +{ + EFI_STATUS Status; + UINT32 Value =3D 0; + + // + // Simulate UEFI boot failure due to config wrong NVPARAM for + // testing failsafe feature + // + Status =3D NVParamGet (NV_SI_UEFI_FAILURE_FAILSAFE, NV_PERM_ALL, &Value)= ; + if (!EFI_ERROR (Status) && (Value =3D=3D 1)) { + CpuDeadLoop (); + } + + return EFI_SUCCESS; +} + +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 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); + } +} + +/** + Main entry for this driver. + + @param ImageHandle Image handle this driver. + @param SystemTable Pointer to SystemTable. + + @retval EFI_SUCCESS 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/Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/Watchdog.c b= /Silicon/Ampere/AmpereAltraPkg/Drivers/FailSafeDxe/Watchdog.c new file mode 100644 index 000000000000..34329d04206a --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/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