From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=104.47.42.120; helo=nam03-by2-obe.outbound.protection.outlook.com; envelope-from=christopher.co@microsoft.com; receiver=edk2-devel@lists.01.org Received: from NAM03-BY2-obe.outbound.protection.outlook.com (mail-by2nam03on0120.outbound.protection.outlook.com [104.47.42.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id BECDC2114B14C for ; Fri, 21 Sep 2018 01:26:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=k+CWdEEu+mxFfSH3I8eROBec7IgrqRWURe2Fc7A+0zM=; b=IrY6Fw/xJyzwAT/CBkxG6cHOpKWlx67cni4RDgI8yckyZTUiyKu1bTxdLXqnK7ITZr0a5pp7fv1KQrrNgDMw0bmNkOm2DZyRKO0nsG1PWa23iHHO7BcPXs/YleKt5cGvM4lTrWwxjPKqd2t9YxG48lwKqaZegMjTdiKPN3qDoKo= Received: from DM5PR2101MB1128.namprd21.prod.outlook.com (52.132.133.20) by DM5PR2101MB1112.namprd21.prod.outlook.com (52.132.133.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1185.5; Fri, 21 Sep 2018 08:25:59 +0000 Received: from DM5PR2101MB1128.namprd21.prod.outlook.com ([fe80::81f8:300e:d90:d49]) by DM5PR2101MB1128.namprd21.prod.outlook.com ([fe80::81f8:300e:d90:d49%3]) with mapi id 15.20.1164.008; Fri, 21 Sep 2018 08:25:59 +0000 From: Chris Co To: "edk2-devel@lists.01.org" CC: Ard Biesheuvel , Leif Lindholm , Michael D Kinney Thread-Topic: [PATCH edk2-platforms 08/27] Silicon/NXP: Add Virtual RTC support for i.MX platform Thread-Index: AQHUUYS5SnZc/SNz7kuaXYqPJ3NXlw== Date: Fri, 21 Sep 2018 08:25:59 +0000 Message-ID: <20180921082542.35768-9-christopher.co@microsoft.com> References: <20180921082542.35768-1-christopher.co@microsoft.com> In-Reply-To: <20180921082542.35768-1-christopher.co@microsoft.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MWHPR17CA0054.namprd17.prod.outlook.com (2603:10b6:300:93::16) To DM5PR2101MB1128.namprd21.prod.outlook.com (2603:10b6:4:a8::20) x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [2001:4898:80e8:8:388a:edc9:7085:c18] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; DM5PR2101MB1112; 6:b0SoVnyAJ8iC3jZve/eZ60YVaYidytTfbyfZUitFddslF+PX0biHijnuTolfWoyxfkNZe6Y18r6tRQitqYIQvncrCg+N3G0qVRx1DQwAwhr0I94KgTUht8J9IQk1Q+A8nl+RRIyJHUNp8WnKNdxbRG79MIZ77WUyCYv76XOrgs1Qr5z9WH3nB9GB5Er2i2Fj3gnPJijVt54as5EKpIbram9Yj5SspaeTu8opocI/X9nGThtwokg3WVrWHTLNxdkIHODuo3ENMQKcv7O3aSCMQi5iFbH3CSSE5xNR1ZsxML/VNL/iQmz1bbrvfvUNQ1TnJ+buxUEo7wJQnS8z3kqIKblX4Njlp3zQ8N3MIZc3lE/GkmsSVGuzeoSFHGbuzAbsmLhUu5Gv6ysPPHjqtd5jo5WyXxVK2Ralx1sMsaMiJLw2VdD2PRdrnQKj4JMLit25ythvamjtMqCSeG1vwqFOmQ==; 5:4B1zMz+2pG1/ST6vua2+mqQp7awT+9tWqg1083s4PgkvJ6U8rOIKlqjMUr26qnUNqS7hTJZGBxB7lfAIaEWiP3eZUmnEmFfUTYa6yfPPXPs+AQJUD/YGcxaDkYDsxqfnPeOFA2l65pHzmDUgkFwZP/wA1AtMsdJLhyeIFrDZ4es=; 7:61pZ2abeDaKOwls897dXYDijKvo7lJWUoTLHeoVHY/+lyXkhZ11/oAPvMqo8z+WGYL2pmaUUU6nsu7PcnFVv6YWX0S1/QKOsy6ZpQr47xzDOc0bHBGIK5fe8MzU67uTd5Fp8GkwCV9fYzngPiJlo2mI1KUgNvjG6L3v6XWqOYZKFlELRc7pfnY6QHahVlHBwO0W6mkTZuRAbS7D/nozqxxHHPlTwS4+YffhEssJM56+DB/J5AePBJStBkJN9goD2 x-ms-office365-filtering-correlation-id: 6a354589-b23a-47a3-d949-08d61f9bdbf6 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0; PCL:0; RULEID:(7020095)(4652040)(8989299)(4534165)(4627221)(201703031133081)(201702281549075)(8990200)(5600074)(711020)(4618075)(2017052603328)(7193020); SRVR:DM5PR2101MB1112; x-ms-traffictypediagnostic: DM5PR2101MB1112: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(788757137089)(28532068793085)(89211679590171)(12401385986421)(228905959029699); x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(5005006)(8121501046)(3002001)(10201501046)(3231355)(2280164)(944501410)(52105095)(2018427008)(93006095)(93001095)(6055026)(149027)(150027)(6041310)(20161123560045)(20161123564045)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(201708071742011)(7699051)(76991041); SRVR:DM5PR2101MB1112; BCL:0; PCL:0; RULEID:; SRVR:DM5PR2101MB1112; x-forefront-prvs: 0802ADD973 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(136003)(346002)(39860400002)(376002)(366004)(396003)(199004)(189003)(106356001)(6512007)(81166006)(6306002)(7736002)(966005)(10090500001)(14444005)(15188155005)(186003)(6436002)(6506007)(54906003)(102836004)(386003)(6346003)(6916009)(316002)(256004)(81156014)(5640700003)(86362001)(105586002)(14454004)(86612001)(68736007)(305945005)(8676002)(5250100002)(52116002)(5660300001)(25786009)(2501003)(76176011)(6486002)(6116002)(8936002)(486006)(71190400001)(71200400001)(4326008)(11346002)(2616005)(53376002)(476003)(446003)(97736004)(53936002)(478600001)(10290500003)(72206003)(22452003)(2351001)(2900100001)(36756003)(16799955002)(1076002)(99286004)(46003)(2906002)(60540400001); DIR:OUT; SFP:1102; SCL:1; SRVR:DM5PR2101MB1112; H:DM5PR2101MB1128.namprd21.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Christopher.Co@microsoft.com; x-microsoft-antispam-message-info: BaB0mR2Bv6vewGhLI02se5PDOEu+Da+o3Bnd+SNOz6DmJ9iSJxKtQ6qpX+TfEC1XggCSyMX/ETfUD877QTxHvT+cOHjIlbcyqG19IYTcQK9AUOwLOTLcMvQhyBokEnjjJIUmHc8Vno011y8gpM+7Qu9Xi/GRk1GgkX03J/zYd5vwz7JZJuyZgmFr7aIEYPCGXq0yXNy7XCfPdf0C3J09vySwxqW65Bmq5/YyoE5pYhoxnLPP/o6YLuTHbuUa/40ExzC0Vm0tBN2lAyLOh+VAokx1okDxaRb47+ZcisJC9/wEBFpLj7Qwv88jH5KT7I5uDj0+VsJkp7oXQCkXO5764V7yur1PgdtyHxDhM4H7rGmJ4+Q/WUHEjzvWu5mYBC9Zpt6le3mvz20JUZmwCNS9zUEx+x53hDr5pYN002joX7/u+M6/+QBTek7Xjji8Sf8xu/B4u6NdcozlIu+dzB8yjQS90httDcoecKaE8W5jaP47F3GorTeiuuenfzYfygahEBAHwSvoEAqCZdF6uDPfx524SXtkPlgxC2sEX/Vb6W5dl8c9epVA4yLnv+EafQ97 spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6a354589-b23a-47a3-d949-08d61f9bdbf6 X-MS-Exchange-CrossTenant-originalarrivaltime: 21 Sep 2018 08:25:59.4189 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB1112 Subject: [PATCH edk2-platforms 08/27] Silicon/NXP: Add Virtual RTC support for i.MX platform X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Sep 2018 08:26:01 -0000 Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable This adds a virtual RTC library that implements EFI RTC runtime services based on the ARM performance counter. It should only be used for relative time measurement, such as for Windows Boot Manager. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Christopher Co Cc: Ard Biesheuvel Cc: Leif Lindholm Cc: Michael D Kinney --- Silicon/NXP/iMXPlatformPkg/Library/VirtualRealTimeClockLib/VirtualRealTime= ClockLib.c | 227 ++++++++++++++++++++ Silicon/NXP/iMXPlatformPkg/Library/VirtualRealTimeClockLib/VirtualRealTime= ClockLib.inf | 37 ++++ 2 files changed, 264 insertions(+) diff --git a/Silicon/NXP/iMXPlatformPkg/Library/VirtualRealTimeClockLib/Vir= tualRealTimeClockLib.c b/Silicon/NXP/iMXPlatformPkg/Library/VirtualRealTime= ClockLib/VirtualRealTimeClockLib.c new file mode 100644 index 000000000000..771c1d7a8f9c --- /dev/null +++ b/Silicon/NXP/iMXPlatformPkg/Library/VirtualRealTimeClockLib/VirtualRea= lTimeClockLib.c @@ -0,0 +1,227 @@ +/** @file +* +* Implement EFI RealTimeClock runtime services based on ARM Performance C= ounter. +* +* Currently this driver does not support time setting, alarms, or runtime= calls. +* This special library is NOT meant to replace a HW RTC implementation to +* measure date/time. Use this library ONLY to measure relative time betwe= en +* two EFI_GET_TIME readings. +* The performance counter will wrap-around eventually after a long time, = make +* sure to consider this limitation if you are depending on this library f= or +* relative time measurement. e.g. For the ARM 64-bit counter with 19.2MHz +* frequency, the counter will wrap-around after approximately 30465 year. +* +* Copyright (c) 2018 Microsoft Corporation. All rights reserved. +* +* This program and the accompanying materials +* are licensed and made available under the terms and conditions of the B= SD License +* which accompanies this distribution. The full text of the license may = be found at +* http://opensource.org/licenses/bsd-license.php +* +* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IM= PLIED. +* +**/ + +#include + +#include +#include +#include +#include +#include + +#define LOG_FMT_HELPER(FMT, ...) \ + "VirtualRealTimeClock:" FMT "%a\n", __VA_ARGS__ + +#define LOG_TRACE(...) \ + DEBUG((DEBUG_BLKIO, LOG_FMT_HELPER(__VA_ARGS__, ""))) + +/** + Returns the current time and date information, and the time-keeping capa= bilities + of the virtual RTC. + + For simplicity, this LibGetTime does not report Years/Months, instead it= will + only report current Day, Hours, Minutes and Seconds starting from the be= ginning + of CPU up-time. Otherwise, a more complex logic will be required to acco= unt + for leap years and days/month differences. + + @param Time A pointer to storage to receive a snapshot= of + the current time. + @param Capabilities An optional pointer to a buffer to receive= the + real time clock device's capabilities. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER Time is NULL. + @retval EFI_DEVICE_ERROR The time could not be retrieved due to har= dware error. + +**/ +EFI_STATUS +EFIAPI +LibGetTime ( + OUT EFI_TIME *Time, + OUT EFI_TIME_CAPABILITIES *Capabilities + ) +{ + UINT64 ElapsedSeconds; + UINT64 TimerFreq; + + if (Time =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // Depend on ARM ARCH Timer (i.e. performance counter) to report date/ti= me + // relative to the start of CPU timer counting where date and time will = always + // be relative to the date/time 1/1/1900 00H:00M:00S + if (PcdGet32 (PcdArmArchTimerFreqInHz) > 0) { + TimerFreq =3D PcdGet32 (PcdArmArchTimerFreqInHz); + } else { + TimerFreq =3D GetPerformanceCounterProperties (NULL, NULL); + } + + ASSERT (TimerFreq > 0); + if (TimerFreq =3D=3D 0) { + return EFI_DEVICE_ERROR; + } + + if (Capabilities) { + Capabilities->Accuracy =3D 0; + Capabilities->Resolution =3D TimerFreq; + Capabilities->SetsToZero =3D FALSE; + } + + ElapsedSeconds =3D GetPerformanceCounter () / TimerFreq; + + // Don't report Year/Month since Leap Year logic is not implemented. Thi= s should + // be fine since the sole purpose of this special implementation is to b= e + // used for relative time measurement. e.g. Windows Boot Manager. + Time->Year =3D 0; + Time->Month =3D 0; + + CONST UINT64 SECONDS_PER_DAY =3D 24 * 60 * 60; + Time->Day =3D (ElapsedSeconds / SECONDS_PER_DAY); + ElapsedSeconds %=3D SECONDS_PER_DAY; + + CONST UINT64 SECONDS_PER_HOUR =3D 60 * 60; + Time->Hour =3D (ElapsedSeconds / SECONDS_PER_HOUR); + ElapsedSeconds %=3D SECONDS_PER_HOUR; + + CONST UINT64 SECONDS_PER_MINUTE =3D 60; + Time->Minute =3D (ElapsedSeconds / SECONDS_PER_MINUTE); + ElapsedSeconds %=3D SECONDS_PER_MINUTE; + + Time->Second =3D ElapsedSeconds; + Time->Nanosecond =3D 0; + Time->TimeZone =3D 0; + Time->Daylight =3D 0; + + LOG_TRACE ( + "Time Elapsed Since Power-On: Day%d %dh:%dm:%ds", + (UINT32)Time->Day, + (UINT32)Time->Hour, + (UINT32)Time->Minute, + (UINT32)Time->Second); + + return EFI_SUCCESS; +} + +/** + Sets the current local time and date information. + + @param Time A pointer to the current time. + + @retval EFI_UNSUPPORTED This operation is not supported. + +**/ +EFI_STATUS +EFIAPI +LibSetTime ( + IN EFI_TIME *Time + ) +{ + // The virtual clock is read-only. + return EFI_UNSUPPORTED; +} + +/** + Returns the current wakeup alarm clock setting. + + @param Enabled Indicates if the alarm is currently enable= d or + disabled. + @param Pending Indicates if the alarm signal is pending a= nd + requires acknowledgement. + @param Time The current alarm setting. + + @retval EFI_UNSUPPORTED A wakeup timer is not supported on this pl= atform. + +**/ +EFI_STATUS +EFIAPI +LibGetWakeupTime ( + OUT BOOLEAN *Enabled, + OUT BOOLEAN *Pending, + OUT EFI_TIME *Time + ) +{ + return EFI_UNSUPPORTED; +} + +/** + Sets the system wakeup alarm clock time. + + @param Enabled Enable or disable the wakeup alarm. + @param Time If Enable is TRUE, the time to set the wak= eup alarm for. + + @retval EFI_UNSUPPORTED A wakeup timer is not supported on this pl= atform. + +**/ +EFI_STATUS +EFIAPI +LibSetWakeupTime ( + IN BOOLEAN Enabled, + OUT EFI_TIME *Time + ) +{ + return EFI_UNSUPPORTED; +} + +/** + This is the declaration of an EFI image entry point. This can be the ent= ry point + to an application written to this specification, an EFI boot service dri= ver, + or an EFI runtime driver. + + @param ImageHandle Handle that identifies the loaded image. + @param SystemTable System Table for this image. + + @retval EFI_SUCCESS The operation completed successfully. + +**/ +EFI_STATUS +EFIAPI +LibRtcInitialize ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + // ARM ARCH Timer is already initialized in the SEC/PEI phase. + return EFI_SUCCESS; +} + +/** + Fixup internal data so that EFI can be call in virtual mode. + Call the passed in Child Notify event and convert any pointers in + lib to virtual mode. + + @param[in] Event The Event that is being processed + @param[in] Context Event Context +**/ +VOID +EFIAPI +LibRtcVirtualNotifyEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // Not supporting OS calling RTC functions in virtual mode. + return; +} diff --git a/Silicon/NXP/iMXPlatformPkg/Library/VirtualRealTimeClockLib/Vir= tualRealTimeClockLib.inf b/Silicon/NXP/iMXPlatformPkg/Library/VirtualRealTi= meClockLib/VirtualRealTimeClockLib.inf new file mode 100644 index 000000000000..54aacde8c741 --- /dev/null +++ b/Silicon/NXP/iMXPlatformPkg/Library/VirtualRealTimeClockLib/VirtualRea= lTimeClockLib.inf @@ -0,0 +1,37 @@ +## @file +# +# Copyright (c) 2018 Microsoft Corporation. All rights reserved. +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the B= SD License +# which accompanies this distribution. The full text of the license may = be found at +# http://opensource.org/licenses/bsd-license.php +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IM= PLIED. +# +## + +[Defines] + INF_VERSION =3D 0x0001001A + BASE_NAME =3D VirtualRealTimeClockLib + FILE_GUID =3D 1E27D461-78F3-4F7D-B1C2-F72384F13A6E + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D RealTimeClockLib + +[Sources.common] + VirtualRealTimeClockLib.c + +[Packages] + ArmPkg/ArmPkg.dec + EmbeddedPkg/EmbeddedPkg.dec + MdePkg/MdePkg.dec + +[LibraryClasses] + DebugLib + IoLib + TimerLib + +[FixedPcd] + gArmTokenSpaceGuid.PcdArmArchTimerFreqInHz --=20 2.16.2.gvfs.1.33.gf5370f1