From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=104.47.34.131; helo=nam01-by2-obe.outbound.protection.outlook.com; envelope-from=christopher.co@microsoft.com; receiver=edk2-devel@lists.01.org Received: from NAM01-BY2-obe.outbound.protection.outlook.com (mail-by2nam01on0131.outbound.protection.outlook.com [104.47.34.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id E0E312098C8C3 for ; Wed, 18 Jul 2018 21:11:26 -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=y/UqXUTXcOpBEjSv+oSrxnJoLQjQpQV4MS9DZs+J0Q4=; b=aRL/632cmSuuD4ci9375lVyxMJZEC1wIaEJxuX8JnDLMKw36+PCdL3Qv8P1kNHG3vRygT0cBtd1frGcgF2KBKsCOCyod3AR9keO4PhBTxAkjWfA6l+smoLffTRCzoMO/wCiXUrDsL1s74N8uymtokonCnWMqf10m7FXHO333zWs= Received: from DM5PR2101MB1128.namprd21.prod.outlook.com (52.132.133.20) by DM5PR2101MB0888.namprd21.prod.outlook.com (52.132.132.157) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.995.0; Thu, 19 Jul 2018 04:11:25 +0000 Received: from DM5PR2101MB1128.namprd21.prod.outlook.com ([fe80::b4d3:dabb:9372:9740]) by DM5PR2101MB1128.namprd21.prod.outlook.com ([fe80::b4d3:dabb:9372:9740%2]) with mapi id 15.20.0995.008; Thu, 19 Jul 2018 04:11:25 +0000 From: Chris Co To: "edk2-devel@lists.01.org" CC: Ard Biesheuvel , Leif Lindholm , Michael D Kinney Thread-Topic: [PATCH edk2-platforms 5/7] Silicon/NXP: Add Virtual RTC support for IMX platform Thread-Index: AQHUHxaOEG3lQJwjcEG6HdpnmZ6mfA== Date: Thu, 19 Jul 2018 04:11:24 +0000 Message-ID: <20180719041103.9072-6-christopher.co@microsoft.com> References: <20180719041103.9072-1-christopher.co@microsoft.com> In-Reply-To: <20180719041103.9072-1-christopher.co@microsoft.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: BN6PR11CA0037.namprd11.prod.outlook.com (2603:10b6:404:4b::23) To DM5PR2101MB1128.namprd21.prod.outlook.com (2603:10b6:4:a8::20) x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [2001:4898:80e8:1:d144:e4c:c05:68bd] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; DM5PR2101MB0888; 6:vf4OsCRhC6UsjGjvk4SNVArn7y4+0lJJbrLeuqEji0/m3KyJTdxC7qhpd2OvOy4SORixdc2//ZpRY2J/JMHyFpB+aZRqWFsw/9yNp6pEYeBqbx15yvfdHa1GKEZGo7Y2rVd406+49AcqPRYEL6r7iNTjN2cFqPPt86EIzJ0n3sAQn2mu1kyHG+cHnsGyuXBWui+7YxoQD3jO72/DNVK+s/PEV/A905rCLhX4+Ni+/ndR8gYNAeINBUcaTRQHnSvj7R4okdln6MNpuA78OH2KNb9VP728tpfkU9Kmwgnxs/eGZt5AKHY7IuM7LbnMwDBvWG7KN0nMr71y71zCphFMaAn5ufu+rx8tIJd/ZuOSP5750xdAStQCncnNKt4K2FxK7Bkiyb3FqKrrHv5MnlMqrQ5Ge9YOBYDn6fIJCrNG06YbbEfkvTbCI8PgEoMZRa3pFgN281qQEvUqJI1/gQkCag==; 5:VxuvyXOY/sGxd2JFSxdhU9QQm7ZllAVV8Pb4IpJJph4JlXkcKu6sti8TA23V6pSpGl/XwWMqTHoyrglFRP4jbahq8yMZl0WN5ph8S8lIHdNGA9bGwkFh9ll2qasMGG+sfH0BuxUJe/zbcjOMtVQVYd2o/J5aL5brJ4Jj3m61f6I=; 7:mIm9fpSE8mu4Z++ykmTEfhaVg8pqHjCnDzAJYvqd/twbIdZB0AAFU4dTYTOrLO5xwdhedqHyfV7HQtQnTE6E1hSm8Y/hMokjXGWWnxUKYKoQZBXl16cHmWLvvTv432KbNEVynhcb8YIHGqNWRjjSgkYiKQ6QbROBJ8QugnR1QCxKyWX0e6VlvtGKbZI3FJIXsWLwYu1ZVTUmvKsCH3SjCWJs7yEmjh+dVfXhR3CzlO6XDSlQ3XenSkR2V3mM1WhR x-ms-office365-filtering-correlation-id: 02cb9368-e08d-4000-7414-08d5ed2db136 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0; PCL:0; RULEID:(7020095)(4652040)(8989117)(5600067)(711020)(4618075)(4534165)(4627221)(201703031133081)(201702281549075)(8990107)(2017052603328)(7193020); SRVR:DM5PR2101MB0888; x-ms-traffictypediagnostic: DM5PR2101MB0888: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Christopher.Co@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(788757137089)(228905959029699); x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(5005006)(8121501046)(3231311)(944501410)(52105095)(2018427008)(93006095)(93001095)(3002001)(10201501046)(6055026)(149027)(150027)(6041310)(20161123562045)(20161123564045)(20161123560045)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011)(7699016); SRVR:DM5PR2101MB0888; BCL:0; PCL:0; RULEID:; SRVR:DM5PR2101MB0888; x-forefront-prvs: 0738AF4208 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(366004)(376002)(346002)(396003)(136003)(39860400002)(189003)(199004)(10090500001)(2906002)(305945005)(6486002)(14444005)(7736002)(46003)(22452003)(256004)(72206003)(2501003)(6436002)(2900100001)(2351001)(4326008)(53376002)(25786009)(53936002)(68736007)(5250100002)(316002)(478600001)(5660300001)(36756003)(966005)(6512007)(10290500003)(14454004)(99286004)(6306002)(54906003)(6916009)(186003)(97736004)(102836004)(81166006)(76176011)(86362001)(81156014)(86612001)(8676002)(386003)(6506007)(1076002)(5640700003)(476003)(2616005)(486006)(8936002)(15188155005)(6116002)(52116002)(105586002)(16799955002)(106356001)(11346002)(446003); DIR:OUT; SFP:1102; SCL:1; SRVR:DM5PR2101MB0888; H:DM5PR2101MB1128.namprd21.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: pUdVoCWe8n+/TIY4bkbH5c+i3+y43upDn+Ey8QZd7CE9mLULsJ+5NK8wSp2aRwwJPAI0WzrNdKId5qjvFbIHEVT3qxyKJCXqayhFhE+7pq7t+Fa0gnvJXSJN51aqsPYT7gIiXaIg5YhbhHZ4X1OI4hVLdpLYk5RBa/BRdQzxhXUm1/dpPT20mClw1vYdOCSU8lrNhUInhJC4ZIKBbbyM5D4Mrp2w1qspLaggsgYrgWfTVYmEmj9dvcXkQ5LDv5iICymeECuIp9SzIBLb8EO+kW4W0+n/pMYlb1kH/64yttKhh30TFFcCa039IRUO/fYBgksYdZQ9DjuQv9d1Ye4jUs5zQp8FH8Fr7fgDppmbdlA= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 02cb9368-e08d-4000-7414-08d5ed2db136 X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Jul 2018 04:11:25.0084 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB0888 Subject: [PATCH edk2-platforms 5/7] Silicon/NXP: Add Virtual RTC support for IMX platform X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 Jul 2018 04:11:27 -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 | 251 ++++++++++++++++++++ Silicon/NXP/iMXPlatformPkg/Library/VirtualRealTimeClockLib/VirtualRealTime= ClockLib.inf | 37 +++ 2 files changed, 288 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..c4c9efb48f61 --- /dev/null +++ b/Silicon/NXP/iMXPlatformPkg/Library/VirtualRealTimeClockLib/VirtualRea= lTimeClockLib.c @@ -0,0 +1,251 @@ +/** @file +* +* Copyright (c) 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. +* +**/ +/** +* Implement EFI RealTimeClock runtime services based on ARM Performance C= ounter. +* +* Currently this driver does not support time setting, alarms, runtime vi= rtual calling +* +* IMPORTANT NOTES: +* - This special library is NOT meant to replace a HW RTC implementation = to measure date/time. +* - Use this library ONLY to measure relative time between two EFI_GET_TI= ME 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 for relative +* time measurement. e.g. For the ARM 64-bit counter with 19.2MHz frequ= ency, the counter +* will wrap-around after approximately 30465 year. +**/ + +#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 begining of CPU up-tim= e. Otherwise, a more + complex logic will be required to account 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 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; + } + + UINT64 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 be u= sed 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; + + // + // Not required to report in our special case + // + + 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_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER A time field is out of range. + @retval EFI_DEVICE_ERROR The time could not be set due due to hardw= are error. + +**/ +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_SUCCESS The alarm settings were returned. + @retval EFI_INVALID_PARAMETER Any parameter is NULL. + @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due= to a hardware error. + +**/ +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_SUCCESS If Enable is TRUE, then the wakeup alarm w= as enabled. If + Enable is FALSE, then the wakeup alarm was= disabled. + @retval EFI_INVALID_PARAMETER A time field is out of range. + @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a = hardware error. + @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 driver, or an EFI run= time 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..e3eb9e2f3148 --- /dev/null +++ b/Silicon/NXP/iMXPlatformPkg/Library/VirtualRealTimeClockLib/VirtualRea= lTimeClockLib.inf @@ -0,0 +1,37 @@ +## @file +# +# Copyright (c) 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 0x00010005 + 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 + MdePkg/MdePkg.dec + EmbeddedPkg/EmbeddedPkg.dec + +[LibraryClasses] + IoLib + DebugLib + TimerLib + +[FixedPcd] + gArmTokenSpaceGuid.PcdArmArchTimerFreqInHz --=20 2.16.2.gvfs.1.33.gf5370f1