From nobody Sun Feb 8 02:56:00 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8998EB64D7 for ; Fri, 30 Jun 2023 17:33:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232773AbjF3RdU (ORCPT ); Fri, 30 Jun 2023 13:33:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232757AbjF3RdN (ORCPT ); Fri, 30 Jun 2023 13:33:13 -0400 Received: from refb01.tmes.trendmicro.eu (refb01.tmes.trendmicro.eu [18.185.115.54]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 877211FE7 for ; Fri, 30 Jun 2023 10:33:11 -0700 (PDT) Received: from 104.47.11.170_.trendmicro.com (unknown [172.21.19.48]) by refb01.tmes.trendmicro.eu (Postfix) with ESMTPS id EAF6C10A25576 for ; Fri, 30 Jun 2023 17:12:30 +0000 (UTC) Received: from 104.47.11.170_.trendmicro.com (unknown [172.21.178.36]) by repost01.tmes.trendmicro.eu (Postfix) with SMTP id 07BB810000D10; Fri, 30 Jun 2023 17:12:29 +0000 (UTC) X-TM-MAIL-RECEIVED-TIME: 1688145148.576000 X-TM-MAIL-UUID: 86f23f87-e04d-4cef-a032-bf5c1ece3099 Received: from DEU01-FR2-obe.outbound.protection.outlook.com (unknown [104.47.11.170]) by repre01.tmes.trendmicro.eu (Trend Micro Email Security) with ESMTPS id 8CD0D10000E20; Fri, 30 Jun 2023 17:12:28 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=T5PDywLNPEoByCJScO2ZE12/BW2HdxlMPe3UdVNys8IHumrRMmCofYh9wWvdcr6G5dqjvEEgCxIRZe7vTanbn7jieSxYRmah0fC1EjglRHeitOYiTDPlFpQ1In/qk0wTMc/UibbLJ+wtB5cOmf6DdomAkvk4XcWHuOy0UEg1QDd1lTxd31EZ6pqqmP/ycUuB6zqO6DrQNZeoup5vgc0/HbeB+Ddki5/DpbDAADFcO+6WaDK8TokxO2elf/H8XJsryT+3Zbq9QwFJjgKBFJDENFEQ0ykpsLMUMGFIca7fjG8HUkvEAxwD4caHulC1k2aHkzGVVah0MIhiRnrGzAYJdw== 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=vXN+mcxHCBD/Bc+55O++gwMaslKK7ehhRDriFCoeY88=; b=fZr06qRyjL0y8Vohph4xOAthjoz1OiUVTnQT8DSzvkkwZ7o7pUrdepRkfv3mtcTTxx+/clKGE7FRzWJoq7gkeQ5BoRLdzTKil/s9D8oCRA7yR1VyjccnkEomqKvXSyjvs5GnFH/DhoD27DAxFBD7oHZl/8UfpBO2+ilWiJKVIThvM4LiBXvLMr1kyZMpQVbmkY8VPUix4Qv/QWVM55mPGq/rVKg4eMaqF7CBSLmHYceZtHo5oG4OGzxvh7GgIlSMwaRo6i8YegT1n8NbOfdWR37mGEGiEuuO/dPR6uBlj9pjpWGs3pSnhw/T6vuAdCp7l2kaGnZ7R0W7KeCeVAXz+w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 217.66.60.4) smtp.rcpttodomain=google.com smtp.mailfrom=opensynergy.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=opensynergy.com; dkim=none (message not signed); arc=none X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 217.66.60.4) smtp.mailfrom=opensynergy.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=opensynergy.com; Received-SPF: Pass (protection.outlook.com: domain of opensynergy.com designates 217.66.60.4 as permitted sender) receiver=protection.outlook.com; client-ip=217.66.60.4; helo=SR-MAIL-03.open-synergy.com; pr=C From: Peter Hilber To: linux-kernel@vger.kernel.org Cc: Peter Hilber , John Stultz , Thomas Gleixner , Stephen Boyd , "Christopher S. Hall" Subject: [RFC PATCH 1/7] timekeeping: Fix cross-timestamp interpolation on counter wrap Date: Fri, 30 Jun 2023 19:10:44 +0200 Message-Id: <20230630171052.985577-2-peter.hilber@opensynergy.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230630171052.985577-1-peter.hilber@opensynergy.com> References: <20230630171052.985577-1-peter.hilber@opensynergy.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB8EUR05FT036:EE_|FRYP281MB2106:EE_ X-MS-Office365-Filtering-Correlation-Id: a56cb0b6-6348-47c2-3cf9-08db798d2e35 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ub4xbx8gL3Xf4qnFyZgrn9+pByovhq+rsSihZQ/5deL1bqyvrg8E1LVopGtunXCPujPA320KrUEPYpxZYQBNd0s4oDoEymutiPAHo1UdTbRZgBcAjXR/O0T02VgPV9+hYT72vZriKhjVHW1WRevUxP9FPui7gRXDEVpvrBUVh9PssPgcWeBrp/RvxtbtgLzLg5abDRdBaieumEVnVGqkJ3uvrsIG814puc10S/om0HlKBpwaU7MLopEnes5tE1zrkjW5jcDTusUPkSnHBSBVZBuMEdkRXzHUSR1/Q/KQqtGsWeq3H6YQv9KxZsJWTF7iLUWELR4tRks7XEVNU4yXFs0LWyXgTdMq754G12H5/8W+l0Kz37g7he5I5ayuDvDgN07Z2OdxXw0ph1u9Z8EYS043b3cDcXENQCDnHfC+4cA2j1dAZeF//CcgSONkjHkl4qyjedw/sur2sUBJxDINqTYbYS6pbjuVAlaEf5nTRGibqREol3G1kLd3gKR9amm1tnoqLApIJLHt/5KsWWs27rH3JYLUh+DgnbaAiB2GDPIDEu6tddpXTAZfQWODR8mvDNC/3tK4gIR0Rm8rLx9+UJZjvXiSpIhboq51vrwoh3s76fPwaMziCFUKO4vh6YcNBzqkSScAZh2/7MMuXjoXFR1RNGNerm+fJyGSTnmX2KWbc6nbS+5A1dsDinNTrzwjATRh/5QUUWB4Zur2e4qe2Ibsu803bo+WIA/7pIpVBdpchvhcR8zuwG7/HeRTtHrAZW4z2vCvu56tUmdaXcgKkw== X-Forefront-Antispam-Report: CIP:217.66.60.4;CTRY:DE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SR-MAIL-03.open-synergy.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(376002)(396003)(39840400004)(136003)(346002)(451199021)(46966006)(36840700001)(86362001)(54906003)(8936002)(8676002)(5660300002)(82310400005)(26005)(44832011)(1076003)(478600001)(6916009)(4326008)(70206006)(70586007)(41300700001)(316002)(42186006)(186003)(2616005)(2906002)(4744005)(47076005)(83380400001)(336012)(36860700001)(40480700001)(36756003)(81166007)(36900700001);DIR:OUT;SFP:1102; X-OriginatorOrg: opensynergy.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jun 2023 17:12:27.4554 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a56cb0b6-6348-47c2-3cf9-08db798d2e35 X-MS-Exchange-CrossTenant-Id: 800fae25-9b1b-4edc-993d-c939c4e84a64 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=800fae25-9b1b-4edc-993d-c939c4e84a64;Ip=[217.66.60.4];Helo=[SR-MAIL-03.open-synergy.com] X-MS-Exchange-CrossTenant-AuthSource: DB8EUR05FT036.eop-eur05.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: FRYP281MB2106 X-TM-AS-ERS: 104.47.11.170-0.0.0.0 X-TMASE-Version: StarCloud-1.3-9.1.1011-27724.001 X-TMASE-Result: 10--2.538700-4.000000 X-TMASE-MatchedRID: SVm/7o6CSMO6TyUO/xKZhTakEkBtr136rutCeVh17xk7dRmDCUrvToyD IPOAt5xjKy3tE1HfkQHDxsdOfchH340cVCEiK1Pf0pUa1RtpyoMqq8hD1DGRIOU0ANNKseCVi8+ IGr+Jb3Uyk/Hw4XN9jTDfSGFUeim/x10k8QqyoAwRLjqoJJt/9Ye8/oJiio9KA3+iPIxcfrY69F U9T760azOhN3RGo1v1NMyO/nomWfCAp+J3UCS7tJfmvpdxWEjowcHhVE1atjtxZskk+1rr0wD2c Tn+TrXw X-TMASE-XGENCLOUD: 04980225-2de2-4c51-a9ba-2ac3d94fe891-0-0-200-0 X-TM-Deliver-Signature: 8A49E00382A2AA2D967A2786A772B5E2 X-TM-Addin-Auth: R5IOi/pDqqAVg65gh0FlFR2xaV1lHVp8faf0lFZ1rN5Zer5q7PeLxG8r1pu 0sMKwNZ/xEGe5kuSsll6h4+tUMi1c1H1RnKvLMjGZ0w8v3y/tkx9svkJASeEmkQKBnd4zo8dIEs DzJPGq3QgEjNlOjFaqtWMtui2/LOB0EpMAVPsQ+KtRVYgISrVLnx/EMVJuq6FkbEg7NuVgUbvi8 ALBRk3L5TtuW1T6S8OXaIdoLWTbDo4fSfKwIycDJnxrlLylycFbnqlGbsE9kRZXUm5TE+FIIt7R CHRNq4KhHmj0CRQ=.gAHxG/GY+BbIZvt/xoHYcRtWP2oBWhH/QBlvxnoo13JFOmMR1/bnB8madT +/NN82fHG/rAGfOGUi0BFqSUxCC6iY6uR8QpRAVWORhwt0JWVKvBpGW6O4P+gMrxIM2Y1lulCDx maGZlp3TY7sZ8zylMzWrpSx/p/k4rnanudhPW5pOwL6xfqbnfHaKc6C4Zk7H938zUgCi/zeFdYm RfAUIBfBu/C1kwHx8NDqed4bo/ScVfvm+g9jYEiN4TyZVntSnJGGGCjViV0MHgJk6K5+qEoB6yF sJEVCbXgexFGLPGGWbG91oNvRviFQF9beGBl1iSgj1NgEMRBubC0WpI6Cog== X-TM-Addin-ProductCode: EMS DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=opensynergy.com; s=TM-DKIM-20210503141657; t=1688145148; bh=KJ7jdUAF6nQwrGeh3TA5XMiM4xrWxyEC0N2xmTiFGAg=; l=991; h=From:To:Date; b=t3mhNt+7SLp07gRdKR5fSDttFdItnUSqNJxsBl1dY3kmyVSMnllDRvtu5mCWRktUe 8Fbh7XgsvH0JyXKB0sKSs7O6AEiYJBA0pzKyN+8Bvs6pRglvjerRRPL9j6aJvmAnae F2B64CK2d+65P/GxAJOjjLLLF6nf7Vk1w2g6x8P3wErQwJEcPKK2d+rBQprSNNWFxi CPOauH/E810SF0szXxv6YZIdCKdQvUxnSSWJX5bp5tRgw2aSWIiq5D5oQDFBXWeT6V Abk2fUBaxRkSKjXMfY7QOTqcbDdsHaZzdZf+Ig24bP/3skjUziZhd4qR8pa3ECTMJn hDpGnkkniAhvw== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" cycle_between() decides whether get_device_system_crosststamp() will interpolate for older counter readings. cycle_between() yields wrong results for a counter wrap-around where after < before < test, and for the case after < test < before. Fix the comparison logic. Fixes: 2c756feb18d9 ("time: Add history to cross timestamp interface suppor= ting slower devices") Signed-off-by: Peter Hilber Acked-by: John Stultz --- kernel/time/timekeeping.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 266d02809dbb..8f35455b6250 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1186,7 +1186,7 @@ static bool cycle_between(u64 before, u64 test, u64 a= fter) { if (test > before && test < after) return true; - if (test < before && before > after) + if (before > after && (test > before || test < after)) return true; return false; } --=20 2.39.2 From nobody Sun Feb 8 02:56:00 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 49677EB64D7 for ; Fri, 30 Jun 2023 17:33:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232815AbjF3RdX (ORCPT ); Fri, 30 Jun 2023 13:33:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232744AbjF3RdP (ORCPT ); Fri, 30 Jun 2023 13:33:15 -0400 Received: from refb01.tmes.trendmicro.eu (refb01.tmes.trendmicro.eu [18.185.115.53]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23AC7E6C for ; Fri, 30 Jun 2023 10:33:13 -0700 (PDT) Received: from 104.47.7.168_.trendmicro.com (unknown [172.21.19.58]) by refb01.tmes.trendmicro.eu (Postfix) with ESMTPS id B505810A258A1 for ; Fri, 30 Jun 2023 17:12:31 +0000 (UTC) Received: from 104.47.7.168_.trendmicro.com (unknown [172.21.199.100]) by repost01.tmes.trendmicro.eu (Postfix) with SMTP id 40E57100017BF; Fri, 30 Jun 2023 17:12:30 +0000 (UTC) X-TM-MAIL-RECEIVED-TIME: 1688145149.849000 X-TM-MAIL-UUID: 71dbd5db-74cf-45ce-a709-5fcf4c19fa38 Received: from DEU01-BE0-obe.outbound.protection.outlook.com (unknown [104.47.7.168]) by repre01.tmes.trendmicro.eu (Trend Micro Email Security) with ESMTPS id CF7A810000E23; Fri, 30 Jun 2023 17:12:29 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=htesn4jFwT0TKgU+LaT+mzqfGxvxEaHATF19aJxr/0zRNCPF9jiFPbi/Oxh2Xz6TxzrjhgNiKncXWXJ4b3Hnc3v1iOoQ0dzv8/TewC7dXloFhqREfULfsm0dbU0QwlvRoDtPyawZz8UcHUbK+AdTvjQvv9C8kbeg0V2fmsrmm58Rk8ffvnv2yvIY739Gxrl79DAkMqkMMOMQz1U8E+UCAslw0hRY1q2ha5Cn+FvMmp3rQNvmp5SKCZtdQPZvAfmo7rpuQE++1pHhtD62PvM5OAJQTbax48/M0lgk4yZJ/S9rz4gt/uiY5HR7e0oYab2OWpgbFzycaF+9r0jFX6Ukgg== 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=Q60tNdBmhsnchPnWNLACIYzfR/FeJ2vn169CHo43Ki8=; b=NOY7mFPE23mPpAZ4DcrshFAuQoCymV4Qpg29Xlt+kDE7VvxWJSHVz+AsVQ8Y6vMyCDv8XzIgeUgzG56393hATueIuoKX0OOlKbFJqEg4SUSS6fxcdwRuBd3XRtBfHfR/C8GGFv4UCkT6GbI8GhDErLwwPYfp9d9YCZahZjoXDkij2tKtrQc8GK3GJj8yIqfDIXP5QavY+QLYj6hkf0R7iculLsSet4gELdRrqSKQbCB6/dfatl6I0/bCDRV2o8dLxsDoiXZ64I5IgvgJqlQ+8WIyT1O/J5vMeEHHpgFKFmiOZ9ktn05unQ/3DisS21Md40OqN5H3WoMnqDWZnrK+Mw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 217.66.60.4) smtp.rcpttodomain=google.com smtp.mailfrom=opensynergy.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=opensynergy.com; dkim=none (message not signed); arc=none X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 217.66.60.4) smtp.mailfrom=opensynergy.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=opensynergy.com; Received-SPF: Pass (protection.outlook.com: domain of opensynergy.com designates 217.66.60.4 as permitted sender) receiver=protection.outlook.com; client-ip=217.66.60.4; helo=SR-MAIL-03.open-synergy.com; pr=C From: Peter Hilber To: linux-kernel@vger.kernel.org Cc: Peter Hilber , John Stultz , Thomas Gleixner , Stephen Boyd , "Christopher S. Hall" Subject: [RFC PATCH 2/7] timekeeping: Fix cross-timestamp interpolation corner case decision Date: Fri, 30 Jun 2023 19:10:45 +0200 Message-Id: <20230630171052.985577-3-peter.hilber@opensynergy.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230630171052.985577-1-peter.hilber@opensynergy.com> References: <20230630171052.985577-1-peter.hilber@opensynergy.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB8EUR05FT043:EE_|BEZP281MB1879:EE_ X-MS-Office365-Filtering-Correlation-Id: ebcbff29-e16f-4686-b065-08db798d2e7f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /JRVLcZhO1vAL7O81m022CXTz7yvw8LI6wMvEOB+m9RgARhuABqMDn4OnaO/cYiJDrh7JAUyjbUDR+QqFiGwIpJFUhEufHVuOKCVtU/C+woVgDCLeElLmHdYdKwHqKUp7agQGxA8Azl91TfWDb1rR29WDq7Wx90hXcIPIbHY0B8SklE+Z7tzw8NJ7tIY2UXeDnX68wVpd1/lCKgSjALhVqdv23T0lXkSNLGZmouT7c/IdpErsFIQu4AhVZ83HN3kHkMrQ1izdPOZeGk+rYDQ1LiIzd12IXvU4KSCfQj52sTkqgMG0TR8eM2j0yFzSSZRpGLS3+2k1ah4r7AzDzXMWWp0JQEdhfOpU+DfFcDSxprTvDaIVfBjGHawg5qOLHx0LzE/6GxHv3I2x2yP3OR2ULybegd+U4H7XTGOW3/2gmw6ubabXrLywGVH2hyA5ggM5Yo1lvjkETBXjODmPUrc1sgtS6p3YVLjwlWF5WrA311nZP9XhpFoEkITzRTyzweIbl0sUzM5TdECsE0KuCmtvKsO95+8a0Ko0EiFnX3S1UjvzXQuem2IaDTod4EGqUIVQXzSYMVbe7PDqCNiBl4LJCmFhYw8j5gY0HEytNRKA2StVf+TbHPOalifx9Zm595JpWRE7YzPhNPejfIb0Qfs538ZtjxjfI1bzxfA8AF9Sc22M7yg/5SsVhvJJqL01T+6Mibv7rYaNn2yYboGUt24QOAFd2bTQzcYp3tgCU1cHabx1BVA9mMuD1m5XeOMNLUtZaF18CVy2ac+3G6POs7q7A== X-Forefront-Antispam-Report: CIP:217.66.60.4;CTRY:DE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SR-MAIL-03.open-synergy.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(346002)(136003)(39840400004)(376002)(396003)(451199021)(36840700001)(46966006)(336012)(47076005)(83380400001)(2616005)(2906002)(81166007)(36860700001)(40480700001)(36756003)(8936002)(8676002)(82310400005)(5660300002)(86362001)(54906003)(41300700001)(6916009)(4326008)(70206006)(70586007)(316002)(478600001)(42186006)(186003)(44832011)(1076003)(26005)(36900700001);DIR:OUT;SFP:1102; X-OriginatorOrg: opensynergy.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jun 2023 17:12:27.9433 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ebcbff29-e16f-4686-b065-08db798d2e7f X-MS-Exchange-CrossTenant-Id: 800fae25-9b1b-4edc-993d-c939c4e84a64 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=800fae25-9b1b-4edc-993d-c939c4e84a64;Ip=[217.66.60.4];Helo=[SR-MAIL-03.open-synergy.com] X-MS-Exchange-CrossTenant-AuthSource: DB8EUR05FT043.eop-eur05.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BEZP281MB1879 X-TM-AS-ERS: 104.47.7.168-0.0.0.0 X-TMASE-Version: StarCloud-1.3-9.1.1011-27724.001 X-TMASE-Result: 10--7.750800-4.000000 X-TMASE-MatchedRID: 1XqbU0So2KXeBLQRDW0C4eI/SZYjZQvVuxdWEFKDsyyXYoGYte3R6q4H XgANsgPq0mUWktwu7TXnChKkI+hacAbTZXdljfOOm2eUW/oDuiHUWSTjY4IXEm149/2JSpDnQfG nDcNT/+U/ll3y6AJK8EURbKbsUADuwijDQnirsG3SlRrVG2nKgyqryEPUMZEg5TQA00qx4JWLz4 gav4lvdTKT8fDhc32NMySZ9aHRPt/PupojnxPiBHOjEKYddvv6cnxszfxH2Ckmx8kl1Wnd+q+eC SMeOiOkfZ9uentPYeaJcb4M4L3rfi1QF7giu85XOwBXM346/+wU7BU91OLaCei3GsXe4mHOzB/L iqfZJh8aw7I5evsuCHFywM3Ch1Yp X-TMASE-XGENCLOUD: 3b0ad64e-e23b-423b-89d1-10fbd1df4d76-0-0-200-0 X-TM-Deliver-Signature: ADFDF136A588C9A88EB8FEB556EB8A2B X-TM-Addin-Auth: DtwCBclG1ECHvjY8OmFIA3YFulJggL4gVfPFWuOd7F5yj/x0J2Ow7CBfJ47 kui0ERey5u5nX98KEnoKW9BjGBk0q/MnfJ2LGuw2Mr8wlWzXX1qVXkWcu3+HvC5jpyzrLcujLV4 B+cpHvZ2HVOu0Zh1Q2pj0hmDHsPYTD1aCSh+8bMLOTbD9mahISclDjutArwvNF1Fq6FiTj64G4K iS78T363jxDgumFQT+VOKBGlA4J7zRU3imu7xccWpH04dl6jHjUyBhpRiG54DT02Q2To9pEx0iD 6kG3hs8opEyLF5A=.H5AV2DlODN1T79Z4+7H29YjOQTxYkBZOA8PUa1PiknwAst0ejxA0SFycgV vVFKkB9/Ta/Uzh7eYdshe3LrgoXyJ2TMhVQbYV0lAJ79wxRXxh2M3uB358YALnxF6L1zPT+9T0m UYFeJ2TktKTXIBfolaoZzHkPiynISlSrZjhN2B4r2pblWjGFMpAIn3BWq4TQgyYXEWwVtHwcKr1 Tqpq5a+EOBHRkqCin1ToYcUnsZ4+y98bNh0Nm9f5aaHXnWMtEWm3/V6NgvIXzOFp95CG/EwJqKn Lh7ubFfG5cCL1lfNanwdr0TOfRjIosTEOpMtLWP/qUyMYtBgcUgA7R7wRSg== X-TM-Addin-ProductCode: EMS DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=opensynergy.com; s=TM-DKIM-20210503141657; t=1688145150; bh=vw7IXbVbuGsoAPP7rGBOB/cR+N7efyuPwJIzEnE6rAk=; l=2613; h=From:To:Date; b=ldpexf99aNZV7pwnG8ud0mWpRmclSY+Y/DJDcBOTJrPy+uLK/E2IxduD3XmZPnK91 H6JS5zqF2IAeoP8dFPMyqtzTkhyHHvhZriu4LVb9OmB2we2csfse6Sy5pfXG5z38hJ /fRaS899IiIJA4iqzkRQYb8H1xDadVKNRy/F/U1qE0bCJcsitWcv3NF5dnz3l5a3xG b6liLfA1bxAfg19y3mIWDKPRgq6787/7HzkF1WhDGS0OQbh/jXfF91wg/WD5ITddab UOTm7FYgEW2f5hM25RRkCNLkCcnalt4c60yIeyN90tRMnnZFXt0gab4+pVRT7zjmDQ UfeVGmpR8H8SA== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" cycle_between() decides whether get_device_system_crosststamp() will interpolate for older counter readings. So far, cycle_between() checks if parameter test is in the open interval (before, after), when disregarding the special case before > after. The only cycle_between() user, get_device_system_crosststamp(), has the following problem with this: If interval_start =3D=3D cycles, cycle_between(interval_start, cycles, now) returns false. If a history_begin was supplied to get_device_system_crosststamp(), it will later call cycle_between() again, with effective argument values cycle_between(history_begin->cycles, cycles, cycles). Due to the test against the open interval, cycle_between() returns false again, and get_device_system_crosststamp() returns -EINVAL, when it could have succeeded. Fix this by testing against the closed interval in cycle_between(). This disables interpolation if interval_start =3D=3D cycles. For the special case before > after, similar arguments hold. Fix this in a similar way. At the second cycle_between() call site, add an extra condition in order to effectively check a half-open interval, which keeps the condition documented above the call site satisfied. Fixes: 2c756feb18d9 ("time: Add history to cross timestamp interface suppor= ting slower devices") Signed-off-by: Peter Hilber --- kernel/time/timekeeping.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 8f35455b6250..7e86d5cd784d 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1180,13 +1180,13 @@ static int adjust_historical_crosststamp(struct sys= tem_time_snapshot *history, } =20 /* - * cycle_between - true if test occurs chronologically between before and = after + * cycle_between - true if test occurs chronologically in [before, after] */ static bool cycle_between(u64 before, u64 test, u64 after) { - if (test > before && test < after) + if (test >=3D before && test <=3D after) return true; - if (before > after && (test > before || test < after)) + if (before > after && (test >=3D before || test <=3D after)) return true; return false; } @@ -1282,6 +1282,7 @@ int get_device_system_crosststamp(int (*get_time_fn) * clocksource change */ if (!history_begin || + history_begin->cycles =3D=3D system_counterval.cycles || !cycle_between(history_begin->cycles, system_counterval.cycles, cycles) || history_begin->cs_was_changed_seq !=3D cs_was_changed_seq) --=20 2.39.2 From nobody Sun Feb 8 02:56:00 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40167EB64DA for ; Fri, 30 Jun 2023 17:27:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233068AbjF3R10 (ORCPT ); Fri, 30 Jun 2023 13:27:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233071AbjF3R1W (ORCPT ); Fri, 30 Jun 2023 13:27:22 -0400 X-Greylist: delayed 889 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Fri, 30 Jun 2023 10:27:19 PDT Received: from refb02.tmes.trendmicro.eu (unknown [18.185.115.58]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E98791FE8 for ; Fri, 30 Jun 2023 10:27:19 -0700 (PDT) Received: from 104.47.11.168_.trendmicro.com (unknown [172.21.19.113]) by refb02.tmes.trendmicro.eu (Postfix) with ESMTPS id F11AF108FE5B1 for ; Fri, 30 Jun 2023 17:12:31 +0000 (UTC) Received: from 104.47.11.168_.trendmicro.com (unknown [172.21.166.90]) by repost01.tmes.trendmicro.eu (Postfix) with SMTP id 769AF100017A6; Fri, 30 Jun 2023 17:12:30 +0000 (UTC) X-TM-MAIL-RECEIVED-TIME: 1688145150.028000 X-TM-MAIL-UUID: 1a4b49c5-5fbb-4004-86fb-cca94cc54f20 Received: from DEU01-FR2-obe.outbound.protection.outlook.com (unknown [104.47.11.168]) by repre01.tmes.trendmicro.eu (Trend Micro Email Security) with ESMTPS id 0720510045509; Fri, 30 Jun 2023 17:12:30 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fqMlCXf/SSLS2cqfEvSIx+ucPsNwE8XQeRxwq8WeQ4OXLBHowuz6GN97YfWrOLGvw7fSSX0TFIc8pCilHqejonoQjf9YwizwE9IDNDehGE6PKruptbYYiT5EyJJdgE/79JygxqGZp1aG+l1T9LVDd1Opxc+KNM7UcoARRz4QAyB3ojTvx/A0DX7lpQkd/710PGqfFKhG6R4xovN6kNtvAQc4NmUl4ayDPB1XgDyYuht7GMqXJ+asN9Q4Vxf+S4LhNrCiXfrYq1OSVPKN3/nBP4uSfqvTQ3fkNRgbo+kKjMvdhKp4rVah8BGM4/Bqi0jpF8uszhn3C4V4DhQuS2iR4w== 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=R/AApkBYgQrRMxPDdlg61GqM6e0QpZo2H/Km3r71LWo=; b=B+QBQxiMcmgbj9G2f4PAUbiQK/lFD0Aaly4Fz2FWexmpvB1bIdocvGFzqLHT6ggeG9pOZac1T3CqmBsv4U/SUp/KegbU+1K2AMaut+3U5gyFUhTdqRg1s5qAmAXaFufv0GWDKiwJZkLOjTmqlLJoUToi94ALoWRG8KfOkqzWaNx+m+mfe9kbWYQP49Ym8OPmVuhmNn7us6BRBMxOOJoaE54s+xTe0UgMb+werSZ8oD1I5MS7QFuKFUNVX+eXCwYveVyygCFK0EJP71LBRP2mAJLIrt8X8PsRRZXl15qOtfz9JZXs4u6clv2SW/So4WOVFwTOEiDzXv/3VgXxwqDXyw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 217.66.60.4) smtp.rcpttodomain=google.com smtp.mailfrom=opensynergy.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=opensynergy.com; dkim=none (message not signed); arc=none X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 217.66.60.4) smtp.mailfrom=opensynergy.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=opensynergy.com; Received-SPF: Pass (protection.outlook.com: domain of opensynergy.com designates 217.66.60.4 as permitted sender) receiver=protection.outlook.com; client-ip=217.66.60.4; helo=SR-MAIL-03.open-synergy.com; pr=C From: Peter Hilber To: linux-kernel@vger.kernel.org Cc: Peter Hilber , John Stultz , Thomas Gleixner , Stephen Boyd , "Christopher S. Hall" Subject: [RFC PATCH 3/7] timekeeping: Fix cross-timestamp interpolation for non-x86 Date: Fri, 30 Jun 2023 19:10:46 +0200 Message-Id: <20230630171052.985577-4-peter.hilber@opensynergy.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230630171052.985577-1-peter.hilber@opensynergy.com> References: <20230630171052.985577-1-peter.hilber@opensynergy.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6EUR05FT062:EE_|BEZP281MB3382:EE_ X-MS-Office365-Filtering-Correlation-Id: 20f1c5f6-e9fc-4955-00ba-08db798d2eb6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RCTJzpusdmd5HQLxa0xJBAcRauvsUeO7bAftUC89GRnNXXqpzBW2a5FqFoBTY7zv4qXPBWG21UbBgVD78V2mXQSP2f8yIKnNmoGcaH19WrLdlCx+INUlClvQGGOIb55V0aehG/D92QjfBizunK+i2S3xw2hdXMtZh6/8pvTIoktC6pbcjxUkjHry5DLhxrW2gq1OCJXzrDSu3eFOjlwZK0M3Uq2mmAYjlRwmWaEN7ZBKaurf3JylcMYVvaCyyNBa3AIUURrVPM/VoDimYyCSs0ft672uxSaTng1HBU6d/aO0cX03vgML8AGcJGIjT6seFc2Vo/3W3a64ee0+W0Vvwd6Bh8KDMPI6Kc6CWB4PZmacC8CrF6XyPEvVrnvbGqMQRKjfHlYGmCk6l3GkTcBi2PXTu4p040oo9xa4fMGciFG1MS2kWade74thZfCAIh6heuRC15sUpkk5k9yZMG/y1q2h+lPRkxIhVBV7MI7zzjqD/BmTvki7BDDxHoAz2MPvoS4yBQfS2qoEMu0Q+WY5cAbvdNofLZbwWY/IlXGnu/Qew3MvhZjDC/pJqLTOZy5lK/WwVnbnBCCS1g49zO4cAsZLEX4MsJbTc5CMyS9HOScDQXBg15NawWDnf/A4nk9lwKgQkVU7NXw2PKpZc33CFs+1TE+rwHIDfXBWEQmAUVGsXXlslQQrKddr7itwAABk2M4V1mzdFNc4K+UfFBhTBlB/fS3SakijPw5354sWZk49fOrdMhNV1BtOFq0mLgk2RMHlgXw2KeFx2L/sd8XDdPKZQzhbErEa5imj2bMAiCs= X-Forefront-Antispam-Report: CIP:217.66.60.4;CTRY:DE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SR-MAIL-03.open-synergy.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(346002)(39840400004)(396003)(136003)(376002)(451199021)(46966006)(36840700001)(336012)(47076005)(83380400001)(2616005)(2906002)(81166007)(36860700001)(40480700001)(36756003)(8936002)(8676002)(82310400005)(5660300002)(86362001)(54906003)(41300700001)(70586007)(6916009)(4326008)(70206006)(42186006)(316002)(478600001)(186003)(44832011)(1076003)(26005)(36900700001);DIR:OUT;SFP:1102; X-OriginatorOrg: opensynergy.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jun 2023 17:12:28.3305 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 20f1c5f6-e9fc-4955-00ba-08db798d2eb6 X-MS-Exchange-CrossTenant-Id: 800fae25-9b1b-4edc-993d-c939c4e84a64 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=800fae25-9b1b-4edc-993d-c939c4e84a64;Ip=[217.66.60.4];Helo=[SR-MAIL-03.open-synergy.com] X-MS-Exchange-CrossTenant-AuthSource: AM6EUR05FT062.eop-eur05.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BEZP281MB3382 X-TM-AS-ERS: 104.47.11.168-0.0.0.0 X-TMASE-Version: StarCloud-1.3-9.1.1011-27724.001 X-TMASE-Result: 10--4.514000-4.000000 X-TMASE-MatchedRID: vFGChw3WFt9eKR/wKUhyN50UyaWO1QCnDNB/jaV2d3ufOd3yw6VkrA9M 5f/N3N4Qthzaop7IM7sxRT5xs1+q/JuBYEgl4gCvJw66cQh/R5eB37bQADgKr2kRdpOqv8vywmu Rf7t8+crfNpMtD+fWwOUTedInjF4ziRz4gknBIqDk+QJ4zu017ddP7KM+8gexMHiMmW0ZUNss3n AY9fdM9dh5c4TN8zbZ7PVkFWMnCn8N5Rk4kJccyL/a0nd+hIFCI/NGWt0UYPCPILr6ZIjnGDaWr KX2wgM5ft7/Qwb81tOa8WPi91Yg/EO0p1hE1Fv7 X-TMASE-XGENCLOUD: aef0d7f4-5fc6-4e25-92b3-7d1ca97f1c13-0-0-200-0 X-TM-Deliver-Signature: 5F4C735E15E25C3882DF00FA4548FB32 X-TM-Addin-Auth: h8TSFSCO5W/H03ZEsIu2bxA8yuwiRmvm5BZ6Rbk84YfiEgmlUfHYX1Fzqtr 8Ud1u47b/kkLfJIlS7qqmBHAosFSj852SyGsWfhdfERjxnFTKEt0PdDk1yuBl888/OXrUJAhNL4 3JgzfywTMDpIPBL+gGaR7cSZzCDhRLLQ8QcXOcCFBzjrIkwvylGNz+46G/v9dAvSeA1DwReavkL Rho/mnEDNds0J1/IYjU2EGtDMHAObuc5LD9tOXSH5VVEYBUFrcrbSuxFh8HZe8lrUwDCQrUKQvD JyaW0U3Xl1G4DHw=.o1VdnndHBG0e1esY3dUZhkRihEzRRitHAwHFWvm1wWcK5jzDPV+7P4PmsB n5rFg34jtye6yZB2lT4mHBGtsiwCdlDRQg3gLcrppVeM11MrSXkCHzZid0A7lpqb2tUYj5+aic+ Y48T7uj1F0HScNXdzZfTxwqJvB0Q1BIfwaCLnoRKr1RYB2SccGM2z+QZGIIqqAZgD6uynT4p4yw ZbBPKLnu/sFAMXwZDsQf89oROl54qgwaAjLHc9plurj34HgxwWUHKkwVTE2jg+7p7nsps5xrwuM JlbSBMgRr111Usm5jlVupkxwnVh8YCxtFEbH3ec7f/QhEeKe/y8WCYe8W5g== X-TM-Addin-ProductCode: EMS DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=opensynergy.com; s=TM-DKIM-20210503141657; t=1688145150; bh=nYjxLeGfkRg/ocG7JLN7YAACduDGu5P9dwsmki6C2Es=; l=2048; h=From:To:Date; b=Cc3w3HSrCEO657zi+DaF5wwCgJ6t0ucP5aQC01DluiTLnfIHDMDJzEHdeNxxTY/6e qtSjghjtHvwp4D+5SLdTGAvDPnn1oZAk8MZvsyT+Nk/uyGoEoO0Jkp6o0NWVQUbLeI +uUTqWrs+n5CWCvdJnuCaIeLLbVfQ8zQAq2fD/kxEOlfq9/crJnBUzQzSEuUYP2ZGG HV3W5V5Xb9L83YGGk5ehT9TdXxY7Vw9a3kkCIgB4Jm9ApxvGQ4Q5FxmHUNNZqWBVwZ NLRk1Zni+yMGbfAAblEwpG/tBBug9GXVzmJdl7GzLy0Y2BL4Y0A5UffQ168zLBJyhj 4EbZtnTDhF3bA== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" So far, get_device_system_crosststamp() unconditionally passes system_counterval.cycles to timekeeping_cycles_to_ns(). But when interpolating system time (do_interp =3D=3D true), system_counterval.cycles= is before tkr_mono.cycle_last, contrary to the timekeeping_cycles_to_ns() expectations. On x86, CONFIG_CLOCKSOURCE_VALIDATE_LAST_CYCLE will mitigate on interpolating, setting delta to 0. With delta =3D=3D 0, xtstamp->sys_monoraw and xtstamp->sys_realtime are then set to the last update time, as implicitly expected by adjust_historical_crosststamp(). On other architectures, the resulting nonsense xtstamp->sys_monoraw and xtstamp->sys_realtime corrupt the xtstamp (ts) adjustment in adjust_historical_crosststamp(). Fix this by always setting the delta to 0 when interpolating. Fixes: 2c756feb18d9 ("time: Add history to cross timestamp interface suppor= ting slower devices") Signed-off-by: Peter Hilber --- kernel/time/timekeeping.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 7e86d5cd784d..7ccc2377c319 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1259,10 +1259,15 @@ int get_device_system_crosststamp(int (*get_time_fn) tk_core.timekeeper.offs_real); base_raw =3D tk->tkr_raw.base; =20 - nsec_real =3D timekeeping_cycles_to_ns(&tk->tkr_mono, - system_counterval.cycles); - nsec_raw =3D timekeeping_cycles_to_ns(&tk->tkr_raw, - system_counterval.cycles); + if (do_interp) { + nsec_real =3D timekeeping_delta_to_ns(&tk->tkr_mono, 0); + nsec_raw =3D timekeeping_delta_to_ns(&tk->tkr_raw, 0); + } else { + nsec_real =3D timekeeping_cycles_to_ns( + &tk->tkr_mono, system_counterval.cycles); + nsec_raw =3D timekeeping_cycles_to_ns( + &tk->tkr_raw, system_counterval.cycles); + } } while (read_seqcount_retry(&tk_core.seq, seq)); =20 xtstamp->sys_realtime =3D ktime_add_ns(base_real, nsec_real); --=20 2.39.2 From nobody Sun Feb 8 02:56:00 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6AA93EB64DC for ; Fri, 30 Jun 2023 17:33:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229546AbjF3Rdn (ORCPT ); Fri, 30 Jun 2023 13:33:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232675AbjF3Rdd (ORCPT ); Fri, 30 Jun 2023 13:33:33 -0400 Received: from refb01.tmes.trendmicro.eu (refb01.tmes.trendmicro.eu [18.185.115.54]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B037BE6C for ; Fri, 30 Jun 2023 10:33:15 -0700 (PDT) Received: from 104.47.11.176_.trendmicro.com (unknown [172.21.19.56]) by refb01.tmes.trendmicro.eu (Postfix) with ESMTPS id AE58210A28461 for ; Fri, 30 Jun 2023 17:12:36 +0000 (UTC) Received: from 104.47.11.176_.trendmicro.com (unknown [172.21.178.36]) by repost01.tmes.trendmicro.eu (Postfix) with SMTP id 2B48F10000C22; Fri, 30 Jun 2023 17:12:35 +0000 (UTC) X-TM-MAIL-RECEIVED-TIME: 1688145154.290000 X-TM-MAIL-UUID: bc47f6dc-f1e2-4258-8266-33b2b274a254 Received: from DEU01-FR2-obe.outbound.protection.outlook.com (unknown [104.47.11.176]) by repre01.tmes.trendmicro.eu (Trend Micro Email Security) with ESMTPS id 46DBC10000E3B; Fri, 30 Jun 2023 17:12:34 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QKKSIxnG/BwQ2sxs4Uq3vr/ZsR8CCySj2JHMBqImtwPz/WV97jgg0uv7CTiBP38xTutFeesd7AU0sh/M3Nqmjtxh1yoRGDEBRxw1z7W/EB0L3wCEuqD/9Zbu2AWoBjhjhoeGvoAoi+aUorVLRIYUlRjf5Db50Z2SK8Pw/R7VpbQ9zXcWLqQqIgelEXci2x76QJEdyBOYBbbiXAgZ4Us2FwCo9U3krelZcBotuPX99ZmRGuDt/iLChc5chRDVbWKW0mHc6SWKbzBWJbdzxsfugNfRRnRLud4BVaJQ5fkw5qryjzs6YQYoT04yvoXxweB+OJPkUoJjGl29seBNKjJqxQ== 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=S9NbgHcVcRIpJ1o8jHBxREqk9X7WWyDwFMlxSLETOxE=; b=a1OJRxjsLBEnmpD7u+NMgYybn50YynSOCvS6RySAJEKWwx1ePJ8RNQRlf/3r9GTIn3Z4XyAt8ZPja4kCfgVHMtjjrvQwjzcgVngDt0WvVpea/MmrZNYslPjdhPZrAY0LH875yf9Mg7bWKcPQ/Ue6DDuPUbC8hipBMAQaSYwnTmP+oKqwsCWal8e6HSKS6ZiCIvftIH47+s6j2kheoCIwvX57wckQPqA2VFTBy9p8A0K8DpQ+N/iSs7AzzjCB+bWgeRe3z8TMwBX+z08UY90jFcQKrVjGGyKMm1ucZ2ucScsfpa8RrjqMNG40w5P1oOGosccDAZJw8e60N9MmXmpdNw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 217.66.60.4) smtp.rcpttodomain=arm.com smtp.mailfrom=opensynergy.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=opensynergy.com; dkim=none (message not signed); arc=none X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 217.66.60.4) smtp.mailfrom=opensynergy.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=opensynergy.com; Received-SPF: Pass (protection.outlook.com: domain of opensynergy.com designates 217.66.60.4 as permitted sender) receiver=protection.outlook.com; client-ip=217.66.60.4; helo=SR-MAIL-03.open-synergy.com; pr=C From: Peter Hilber To: linux-arm-kernel@lists.infradead.org Cc: Peter Hilber , linux-kernel@vger.kernel.org, Mark Rutland , Marc Zyngier , Daniel Lezcano , Thomas Gleixner Subject: [RFC PATCH 4/7] clocksource: arm_arch_timer: Export counter type, clocksource Date: Fri, 30 Jun 2023 19:10:47 +0200 Message-Id: <20230630171052.985577-5-peter.hilber@opensynergy.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230630171052.985577-1-peter.hilber@opensynergy.com> References: <20230630171052.985577-1-peter.hilber@opensynergy.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6EUR05FT061:EE_|BEZP281MB2005:EE_ X-MS-Office365-Filtering-Correlation-Id: 43f0c4dc-6b26-40af-3761-08db798d2f11 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 217UTAg+BHziMoQ8pM5wo3B1PFMHCsvQVv55bxD3MGYVnrB8sZhtyMydZ0UqcssrEOQK5JyXY4bpRALj61FMH1LOLQiNP9UpMnplB00lJGufsPXUNGfIvpc/orlkPHOGgQSLXl/N32LcODHGgAvAjykq8LMqhIuRJNZKMoztTo2Mp0IlDMk+h1dnpZn9+cnD+wkNgcUXO+jgVCgCieb2bWF9FGsMyeH73iUU1dLnLmdY9zA5eu/ejhbXehNKEJlYN+gElxGEeZUaOhiPf0txSrt8doSBzmvYjfmrH9vPtqaPuIWWvGTEhQnQSOVKApAmiWW61IEgQZd+4jedZpIcAHv7S81XSVXx1GG9z0myubWPM0qCYDDUKQo94iYqxkjqcJppS0hp63499IkSwIOd14/uUjZMmiqPr9H4mbi/a6WDpxJAG6+G98QoRNj01Z907u5IoazJDzN49BI8FAR7Bc/fpAshlVGIsR1DR17bmdusobdGJ8he1NY1gfDO1v99Ggttg+yHgeErGGiHn727blsATBwjvwGAhnLMj8IStHlxoTeR4LRGOc8PPpRATFna6k9bcx1duiPRTw6E03wGaYR2IuEqjlKhSq9Mq/rRapArNenB0ycgeykiWLWV8JK00s2K4iGFOSvMchwjSdjm5FBo2h26KOLUIx6VpLPFChablUFcXLd9UD0993Nxx5hZIp66tR4hjPftsABDIUJn0jGz32HueeAhPXGj2aGCphcLzvXqXDy7L6BLzg0vuqSDoXl3FtZ8QE4TfQd1ZU724g== X-Forefront-Antispam-Report: CIP:217.66.60.4;CTRY:DE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SR-MAIL-03.open-synergy.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(346002)(396003)(39840400004)(376002)(136003)(451199021)(46966006)(36840700001)(966005)(478600001)(36860700001)(47076005)(186003)(26005)(40480700001)(1076003)(336012)(83380400001)(36756003)(2616005)(82310400005)(86362001)(81166007)(6916009)(4326008)(70586007)(70206006)(316002)(2906002)(5660300002)(44832011)(8936002)(8676002)(41300700001)(42186006)(54906003)(36900700001);DIR:OUT;SFP:1102; X-OriginatorOrg: opensynergy.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jun 2023 17:12:28.9126 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 43f0c4dc-6b26-40af-3761-08db798d2f11 X-MS-Exchange-CrossTenant-Id: 800fae25-9b1b-4edc-993d-c939c4e84a64 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=800fae25-9b1b-4edc-993d-c939c4e84a64;Ip=[217.66.60.4];Helo=[SR-MAIL-03.open-synergy.com] X-MS-Exchange-CrossTenant-AuthSource: AM6EUR05FT061.eop-eur05.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BEZP281MB2005 X-TM-AS-ERS: 104.47.11.176-0.0.0.0 X-TMASE-Version: StarCloud-1.3-9.1.1011-27724.001 X-TMASE-Result: 10--2.385400-4.000000 X-TMASE-MatchedRID: W1Gv3YQwcksfq1C9yqtpY3iXIXWXK9Sta3FVQe5d4zs3j/Mq9utADezy hpwh5Kn1QaSYRDMTd7qDdsVaS6GBx8eYpzz6yNBUUlO0zoIcpU6B1fO2o4QGcOEKbxqb5I/nhf7 60Jrkn7K+1+8zb5yWP0dRX+q/OXWrFFzaf2J+d+zW0zrdUW8Q+Ytm4/7hkNAG0kDOV2EVXlmbSR 3JK+cvXno17dTpompJBjNUyX1MCsUTfhpDeOWkVYYtgu3ZTSlqcnxszfxH2Ckmx8kl1Wnd+q+eC SMeOiOkfZ9uentPYeaJcb4M4L3rfi1QF7giu85XOwBXM346/+xtaDgQylf8ASH4cW+2DMrOI91i xqpCYnJ+yCBP6lg3OpZ1wa3cQxeJ X-TMASE-XGENCLOUD: 09e92e8c-4928-4749-896e-f0ec90671b87-0-0-200-0 X-TM-Deliver-Signature: 454AA7F489DE1321776A11B1FC23D40E X-TM-Addin-Auth: J2WEeMMnNi36NAC4plcsdivwdRHsoSn/K1YCkMtas4HCMIRhbuNiXqqRqBw V/jJPRW/heu3DW9M8mjz66E1+y76MsWgeiZYfCE3AwQxb6VsRlYiO5n9wMN17IEHhrt64Ai426u 56iALRoI+qNnXz3a1x4s1i8JJMf99tFCI3OSq/cqtaMFGtV4qbOMX0dHvVMpD6z15cohAOh4n0z UGRMDiIOg0pZmlrlB+zGExkI5MNSKvVY++vEE/+r9NWf2Sjt+KndlB7MrrgffOcJWUSCerY0lYI sLtYJhezoMRkoCw=.PrUJTGDOxGIJ7ANxvKcp0OFgX4bc8nZXWE/5xINoL59tNQq8GAt6FJNwm6 JbR6c8kL6fIdSA4tuKKNn6O7UQ9tMWJCYtvpdGyvgjTSDPkai7bnRshyiJqVb7lOWmh7sAjXUON wIl+z5Qr8TAkYwfppEIejzwbF3kyd6Yztb0TF9QQP6vYmC3150K9xyVglvGJHwEZG9w3miLGI/T eDdWiUEJGYl6LheACjmPv1kty5rmYaFoFBwXi4qWFC1ncDe1jK7KdpDK1h/cU4j5QN8PjRfSvaO GsjtovCBRVDFIEBekW9ClYk+1Ut2/RhrS9oeUeDRwhUHiqDKRJiFDQ9u+Cg== X-TM-Addin-ProductCode: EMS DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=opensynergy.com; s=TM-DKIM-20210503141657; t=1688145154; bh=7GJ/6QyfKmdk9vfZ7fTYL619asKPR22BGPfJCTCrlBA=; l=4094; h=From:To:Date; b=OgmWdB2fJ0aEfknPUyIUMIh7XG95cOVap9nxE/ldScaW3KfVEewlqeRQJt47gwZzq NjpGouwZYxwbxYkKc+p9YABfzu828ElP1MWH8tCdpgsOP7W3OmBqJCjR1KiYhqlMMP nkbVd7b+L56kQalvO9M5cx4DQcUSCiX2pe0AilN7Ci+O6Iadqn6hiRP7H2A+3fPtRc lnoUbjgnJV9k3iNxUz9AScVcUhPJJ2F/5YAS3slXZx+7SyM2+OVt5A6bkh1QCFw+wq 9gQVzFpCIOuLr/uqFV5KT2JiAENEY6TY7NcwXDLcojY2nUCA+41WoErLF8gszJ48OT 22/3Qy/o3MLgA== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Export helper functions to allow other code to - determine the counter type in use (virtual or physical, CP15 or memory), - get a pointer to the arm_arch_timer clocksource, which can be compared with the current clocksource. The virtio_rtc driver will require the clocksource pointer when using get_device_system_crosststamp(), and should communicate the actual Arm counter type to the Virtio RTC device (cf. spec draft [1]). [1] https://lists.oasis-open.org/archives/virtio-comment/202306/msg00592.ht= ml Signed-off-by: Peter Hilber --- drivers/clocksource/arm_arch_timer.c | 16 ++++++++++++++++ include/clocksource/arm_arch_timer.h | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/drivers/clocksource/arm_arch_timer.c b/drivers/clocksource/arm= _arch_timer.c index e733a2a1927a..cebdc1b2db4c 100644 --- a/drivers/clocksource/arm_arch_timer.c +++ b/drivers/clocksource/arm_arch_timer.c @@ -92,6 +92,7 @@ static enum vdso_clock_mode vdso_default =3D VDSO_CLOCKMO= DE_ARCHTIMER; #else static enum vdso_clock_mode vdso_default =3D VDSO_CLOCKMODE_NONE; #endif /* CONFIG_GENERIC_GETTIMEOFDAY */ +static enum arch_timer_counter_type arch_counter_type __ro_after_init =3D = ARCH_COUNTER_CP15_VIRT; =20 static cpumask_t evtstrm_available =3D CPU_MASK_NONE; static bool evtstrm_enable __ro_after_init =3D IS_ENABLED(CONFIG_ARM_ARCH_= TIMER_EVTSTREAM); @@ -1109,6 +1110,7 @@ static void __init arch_counter_register(unsigned typ= e) rd =3D arch_counter_get_cntvct; scr =3D arch_counter_get_cntvct; } + arch_counter_type =3D ARCH_COUNTER_CP15_VIRT; } else { if (arch_timer_counter_has_wa()) { rd =3D arch_counter_get_cntpct_stable; @@ -1117,6 +1119,7 @@ static void __init arch_counter_register(unsigned typ= e) rd =3D arch_counter_get_cntpct; scr =3D arch_counter_get_cntpct; } + arch_counter_type =3D ARCH_COUNTER_CP15_PHYS; } =20 arch_timer_read_counter =3D rd; @@ -1124,6 +1127,7 @@ static void __init arch_counter_register(unsigned typ= e) } else { arch_timer_read_counter =3D arch_counter_get_cntvct_mem; scr =3D arch_counter_get_cntvct_mem; + arch_counter_type =3D ARCH_COUNTER_MEM_VIRT; } =20 width =3D arch_counter_get_width(); @@ -1777,6 +1781,18 @@ static int __init arch_timer_acpi_init(struct acpi_t= able_header *table) TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init); #endif =20 +enum arch_timer_counter_type arch_timer_counter_get_type(void) +{ + return arch_counter_type; +} +EXPORT_SYMBOL_GPL(arch_timer_counter_get_type); + +struct clocksource *arch_timer_get_cs(void) +{ + return &clocksource_counter; +} +EXPORT_SYMBOL_GPL(arch_timer_get_cs); + int kvm_arch_ptp_get_crosststamp(u64 *cycle, struct timespec64 *ts, struct clocksource **cs) { diff --git a/include/clocksource/arm_arch_timer.h b/include/clocksource/arm= _arch_timer.h index cbbc9a6dc571..b442db0b5ca0 100644 --- a/include/clocksource/arm_arch_timer.h +++ b/include/clocksource/arm_arch_timer.h @@ -43,6 +43,13 @@ enum arch_timer_spi_nr { ARCH_TIMER_MAX_TIMER_SPI }; =20 +enum arch_timer_counter_type { + ARCH_COUNTER_CP15_VIRT, + ARCH_COUNTER_CP15_PHYS, + ARCH_COUNTER_MEM_VIRT, + ARCH_COUNTER_MEM_PHYS, +}; + #define ARCH_TIMER_PHYS_ACCESS 0 #define ARCH_TIMER_VIRT_ACCESS 1 #define ARCH_TIMER_MEM_PHYS_ACCESS 2 @@ -89,6 +96,8 @@ extern u32 arch_timer_get_rate(void); extern u64 (*arch_timer_read_counter)(void); extern struct arch_timer_kvm_info *arch_timer_get_kvm_info(void); extern bool arch_timer_evtstrm_available(void); +extern enum arch_timer_counter_type arch_timer_counter_get_type(void); +extern struct clocksource *arch_timer_get_cs(void); =20 #else =20 @@ -107,6 +116,16 @@ static inline bool arch_timer_evtstrm_available(void) return false; } =20 +static inline enum arch_timer_counter_type arch_timer_counter_get_type(voi= d) +{ + return ARCH_COUNTER_CP15_VIRT; +} + +static inline struct clocksource *arch_timer_get_cs(void) +{ + return NULL; +} + #endif =20 #endif --=20 2.39.2 From nobody Sun Feb 8 02:56:00 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DCE52EB64D7 for ; Fri, 30 Jun 2023 17:35:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232608AbjF3RfP (ORCPT ); Fri, 30 Jun 2023 13:35:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232119AbjF3Rez (ORCPT ); Fri, 30 Jun 2023 13:34:55 -0400 Received: from refb01.tmes.trendmicro.eu (refb01.tmes.trendmicro.eu [18.185.115.53]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43F1444AF for ; Fri, 30 Jun 2023 10:34:28 -0700 (PDT) Received: from 104.47.7.171_.trendmicro.com (unknown [172.21.10.52]) by refb01.tmes.trendmicro.eu (Postfix) with ESMTPS id 457E610A258D4 for ; Fri, 30 Jun 2023 17:12:34 +0000 (UTC) Received: from 104.47.7.171_.trendmicro.com (unknown [172.21.199.100]) by repost01.tmes.trendmicro.eu (Postfix) with SMTP id C4FDD1000174E; Fri, 30 Jun 2023 17:12:32 +0000 (UTC) X-TM-MAIL-RECEIVED-TIME: 1688145152.010000 X-TM-MAIL-UUID: 3afd12a0-e562-41cb-987f-d7629145e4b7 Received: from DEU01-BE0-obe.outbound.protection.outlook.com (unknown [104.47.7.171]) by repre01.tmes.trendmicro.eu (Trend Micro Email Security) with ESMTPS id 02AB1100003D3; Fri, 30 Jun 2023 17:12:31 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DAo2lQAFsmcFNQmY03rkvIqvcDkogyOOknHCXx2+lCiB8gqLnJ8mku6ce9UELqupHHhLfBfnoDJP7D/Q2vcdGOGEZ2Olt+cs1HxDOkAUuoNsHw9BIYQAi0O+bL5GYJ5F4DfP4bH87/O0pVwvZUDDktm2Y7OZy9qcKCwNi/S5/WR+qeRG6EQzRefMKAjNNfvlKmco5/K5kKs61Ns/+o+IC2T9eKu9gHnA+3wcyAZr+qoUi2FnNBHULFACgxNv6OaQOEpGZ+TED9eofsw+X8o1zZEKOINgTdju23u6ANio7CyUbmSu9siK801D6URYAlryN0pOTIO90fGs/NfdfnF84w== 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=DXcHDUvZRjXgevViWtysxdHSyDacwp7ceSIRIVR+vSs=; b=Lfva/RiUmrLIYyn6CrrAuolIIYKf1EMPG4DfUb+icpSTQ6oIFP+CpGHwb+2HxClRKSbIdLHFbsPAVCvoJQ0wV5ALg66n9wT+R/oKH5BdUaSMh6PSzi0VSp6zAtZWF6am7aeidGXBHMTcS/ONpwsaAZqnssyG9mC8essteI/cZlRbK4vWBBT0h8EvzjB1sO3a66S1rSiSjzKjskzu0XOCGvCuvDIs44V6ntn1hvJNlZIdF0KEkpA0Tt6PokbvXkUdaF5Ah6tiCh4HrZpXGK2aG0oX/pVj44L2El2SMg0HUHZJ5cWr+wjDkMl8lVwZrXzpkBAUU+clDCNKflpqX0bUkw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 217.66.60.4) smtp.rcpttodomain=gmail.com smtp.mailfrom=opensynergy.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=opensynergy.com; dkim=none (message not signed); arc=none X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 217.66.60.4) smtp.mailfrom=opensynergy.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=opensynergy.com; Received-SPF: Pass (protection.outlook.com: domain of opensynergy.com designates 217.66.60.4 as permitted sender) receiver=protection.outlook.com; client-ip=217.66.60.4; helo=SR-MAIL-03.open-synergy.com; pr=C From: Peter Hilber To: virtualization@lists.linux-foundation.org, virtio-dev@lists.oasis-open.org Cc: Peter Hilber , linux-kernel@vger.kernel.org, "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , Richard Cochran , netdev@vger.kernel.org Subject: [RFC PATCH 5/7] virtio_rtc: Add module and driver core Date: Fri, 30 Jun 2023 19:10:48 +0200 Message-Id: <20230630171052.985577-6-peter.hilber@opensynergy.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230630171052.985577-1-peter.hilber@opensynergy.com> References: <20230630171052.985577-1-peter.hilber@opensynergy.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB8EUR05FT067:EE_|FRYP281MB2620:EE_ X-MS-Office365-Filtering-Correlation-Id: 06de956d-a923-4a82-edb3-08db798d2fc4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: O4mcAGxb/98B0pR5T2J4d8dBzHezLu+uqQnOnQu6MppYJdyn+k3iigdSgcMO+/0yy2Z6ULO3ikOdGGCinefcbScONDHIRVPC0TH+/roGs/J3tfdbwIS/1CV+8EIxoahmqBP6hoUsFyKi5xePIk6rKcM/mAzyPU4wgZI2OIdmoxYkCdgdI9mS6qC6RyeFdhMITT/GqPAkVb1YwFxVyWVpJT1EoJy0EkwmmJXX7T/67miYN6R3OSydy32H9wlRD2GQ+QdtB0sTrHALdpM9wB0mEqgkmeZxKrPOwI/BkpjYrd5swhoZgw/+PF0EGR5xtZ77KCimFvke3I53W7fZYkBFVRAGEoKH4dAYzJdXsPI2ltb6TIzko5PYtcZ0UlJa+rgtcEPRmrkpkz/Akk4dxcnoJzibhsY8bN2qgRvo2pBRPn7ZEVHs9Xp9TUZ07Zkho3SMGckJDPFojliFS2ItFPcQG2TCfOW6dgiSB7Q1HGCmF+aRnrW6l8yOVLTOKFe/vS/15C3MA3RDDUTdXPBli8eCr2GHqRY+k10xod394GsL/XM17xglqGJi05PZpFX066zitJhSDKqqsSrgTPhy9kANym0U2s84DiDuNL2io85KHXRSFRJ/odc126gv0O52Vuu1TyPzFTZrpS3r8u3hjWCD2iV1VAlIMEuzr6btfD8NRw4Iu5rnZwFCC0Om8dPccT2FDj6O6+8dnjAIdiBKl/PgaPcUoQoweEAmgqbj18VPLfaC2fBlHvlOot5m4gMhcvfOzR9UF25dKyXIBqDf37S/QA== X-Forefront-Antispam-Report: CIP:217.66.60.4;CTRY:DE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SR-MAIL-03.open-synergy.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(376002)(396003)(39840400004)(136003)(346002)(451199021)(46966006)(36840700001)(86362001)(54906003)(8936002)(8676002)(5660300002)(82310400005)(26005)(44832011)(1076003)(478600001)(4326008)(70206006)(70586007)(966005)(41300700001)(316002)(42186006)(186003)(66899021)(2616005)(2906002)(47076005)(83380400001)(336012)(30864003)(36860700001)(40480700001)(36756003)(81166007)(36900700001);DIR:OUT;SFP:1102; X-OriginatorOrg: opensynergy.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jun 2023 17:12:30.0904 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 06de956d-a923-4a82-edb3-08db798d2fc4 X-MS-Exchange-CrossTenant-Id: 800fae25-9b1b-4edc-993d-c939c4e84a64 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=800fae25-9b1b-4edc-993d-c939c4e84a64;Ip=[217.66.60.4];Helo=[SR-MAIL-03.open-synergy.com] X-MS-Exchange-CrossTenant-AuthSource: DB8EUR05FT067.eop-eur05.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: FRYP281MB2620 X-TM-AS-ERS: 104.47.7.171-0.0.0.0 X-TMASE-Version: StarCloud-1.3-9.1.1011-27724.001 X-TMASE-Result: 10--8.385500-4.000000 X-TMASE-MatchedRID: CtIu++P39qfJ+P2VFrJmrGy53dw2JHQm5f2pKgY/c023UJJ8+n/4RSJS j0953fkRWpS5sR1Imtmbfc6n6VIFW7O1F/ODk5naCqJ0RbJ3bb3t08AkgFFynZASM5lkTKECyom U41gxOyOvmMduBbYSfBOxOZUm/FwcqMfOugFNRj0v/mdfeJBuVwf8yw0JGfKV0SCNQ10PIeZ9OD X3a/d/aTkJ8R13PVYv5RN50ieMXjOJHPiCScEioNwK3aKXLlLAFT2urH0z7F2ocjoCWfKow0zXs GsDTkZg2agkMppXv0m2/m2VjREHcXV2KXA5dledk5TUws0flAgxF6GI4Gs7uFQxWbscxchmxHtx 9qt/1r9FiIQ0fcK1gMaDKYR20wpbGzL7OqSPhBBch/PYekr/BIld6gdpSPED0g+dAcmSn6KEmEj VFBjobGwB+QHjMMuMBPsCWqKv2JA5D7FiRTCtpYruruLIWfnxAJ4bT6ig74eO46CtGFNzgCOOHp I1HwbloJemVG4ZAoyJwQLSKO0PQrxrCWBi8DCI5HjnXG+QH9kWRcTK0fEGOYhlQKYkSwK5B4yfk Zdpr6xd/lwGDhHRwr720qx8L+B47XR4UA785+mUnSCU9aFRnc9pbisPwZaMJn8Qrtc/icFKEKDa /2sGfx9J7BdYDf1apNQn4E7eyOOisvk326rjUQjWmqF6z+8JOuQL6sws+DMugFBQy+58BY1VnSH /ifaxQYD94YqgH+G2LbUCsnf1HoSNXZ9mdTRviL+RrlWsewoIyIQE9neGFTqan03RiCOu4YlChl K+lWXfXDU2ciEUMsKjGRQnQ81hzxPIJ+YGuapbyCHfDNuoE9umu0ICVejLoli4ZoiOHT9+ICquN i0WJGLJDgYVcjrsEoLA+9HxWvlV+L+rJdAzBxkbbKPWP6ItftwZ3X11IV0= X-TMASE-XGENCLOUD: fa5f6ec7-cb81-4a75-9c9c-59296e672b19-0-0-200-0 X-TM-Deliver-Signature: CF21A3D2C99C291F4DFADA070B2F6B82 X-TM-Addin-Auth: qi1g3EituuwXGoOYBJDpwn/jmm2Yo6I5nVvjDom2tGxrjeQixn1mvMmD3tO AHsBgOS1/JwmVLBa0J5OhumQNBASsZkEe3zHccWoU72I+oYojEc8fOReZlw5Wd+d3G4lrEYfeI0 sr4Q6tQugW/7Ap2NUcxC/rgVpae8TssNdLrY3WMinY1KDqUsiKo6Hkwy0eRmGjPcmLnM+Vl3Tz9 l2LbHxGzTqRXDSBPMAFe8VcXO1ucIlicEh8aa7DqLol1Iw3EFhtslsA7pij0GHPBAql+CzbbWMj QNlCba+dx1FDtLE=.n1A/FNvK8YKCremxuuaWmQxOu9/gVtighqYhj5Xe/grc9t7bxYtVz76k6/ YBVGNS/6YrOcbG6+7quaUhV4qRGrncidlVi9onsbeKEpVhaen3KZDpWILzc7dndeH9m033wHx5+ w6sbduBZJH11hVBKhn2kxkwoh/DfBOLvPOc8rswRdrQla7XU96Fy06uM/jKXOot8+C8VrY3aPtC cSkDOO5yt963zCQwmWczmNU56EGeMqAofnV5ZMqhXIhaNjv9eBj1d2JMyJW4SWmP5/zJbo8ZKGS zfU5tYNGAllgCMz50XvpfaUYdC8B7FhgOr5h70QVsMxeTkMT1PxANDeYM8Q== X-TM-Addin-ProductCode: EMS DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=opensynergy.com; s=TM-DKIM-20210503141657; t=1688145152; bh=f8Sxbiy2zSjrDU8177WA7Pj1Yok2tbCtGyRHWNerols=; l=28231; h=From:To:Date; b=XTszhdzZhhoD2CgKI8SRYyZ9jaah5p8qGeWaoiQKqXJtxj2JtdLdDnPoNyPWDfcuE j7rhYz70T41EdSPYhK5qzI+7iAQKeAk5zy2gO3U2GIF9tcDdttmzOWnz++pJqBy/V3 i5GGTkOKFjkp7WT0TtI+cni1kS5nlNBECl1cxkdL1RZfBomkrfO8vjpaXQ7oix6MjL PHZSruBKJ1fjjlLcm/TpInAkSYP0ds7mO+faxY9agi4uiC0WGM4sudiHWZHQ8Rp9DT agz4tGT4i0pgz0HQk2N70ZzjTcWNU0Yqs/uMXIITIHM7mwNW1e58nkSG26w7IvlbS/ OBgUY8umxCk3w== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add the virtio_rtc module and driver core. The virtio_rtc module implements a driver compatible with the proposed Virtio RTC device specification [1]. The Virtio RTC (Real Time Clock) device provides information about current time. The device can provide different clocks, e.g. for the UTC or TAI time standards, or for physical time elapsed since some past epoch. The driver can read the clocks with simple or more accurate methods. Implement the core, which interacts with the Virtio RTC device. Apart from this, the core does not expose functionality outside of the virtio_rtc module. A follow-up patch will expose PTP clocks. Provide synchronous messaging, which is enough for the expected time synchronization use cases through PTP clocks (similar to ptp_kvm) or RTC Class driver. [1] https://lists.oasis-open.org/archives/virtio-comment/202306/msg00592.ht= ml Signed-off-by: Peter Hilber --- MAINTAINERS | 7 + drivers/virtio/Kconfig | 14 + drivers/virtio/Makefile | 2 + drivers/virtio/virtio_rtc_driver.c | 736 +++++++++++++++++++++++++++ drivers/virtio/virtio_rtc_internal.h | 23 + include/uapi/linux/virtio_rtc.h | 159 ++++++ 6 files changed, 941 insertions(+) create mode 100644 drivers/virtio/virtio_rtc_driver.c create mode 100644 drivers/virtio/virtio_rtc_internal.h create mode 100644 include/uapi/linux/virtio_rtc.h diff --git a/MAINTAINERS b/MAINTAINERS index cd5388a33410..4dcdb98146be 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22661,6 +22661,13 @@ S: Maintained F: drivers/nvdimm/nd_virtio.c F: drivers/nvdimm/virtio_pmem.c =20 +VIRTIO RTC DRIVER +M: Peter Hilber +L: virtualization@lists.linux-foundation.org +S: Maintained +F: drivers/virtio/virtio_rtc_* +F: include/uapi/linux/virtio_rtc.h + VIRTIO SOUND DRIVER M: Anton Yakovlev M: "Michael S. Tsirkin" diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index 0a53a61231c2..e3dbf16fa977 100644 --- a/drivers/virtio/Kconfig +++ b/drivers/virtio/Kconfig @@ -173,4 +173,18 @@ config VIRTIO_DMA_SHARED_BUFFER This option adds a flavor of dma buffers that are backed by virtio resources. =20 +config VIRTIO_RTC + tristate "Virtio RTC driver" + depends on VIRTIO + depends on PTP_1588_CLOCK_OPTIONAL + help + This driver provides current time from a Virtio RTC device. The driver + provides the time through one or more clocks. The driver sub-option + VIRTIO_RTC_PTP must be enabled to expose the clocks to userspace. + + To compile this code as a module, choose M here: the module will be + called virtio_rtc. + + If unsure, say M. + endif # VIRTIO_MENU diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile index 8e98d24917cc..f760414ed6ab 100644 --- a/drivers/virtio/Makefile +++ b/drivers/virtio/Makefile @@ -12,3 +12,5 @@ obj-$(CONFIG_VIRTIO_INPUT) +=3D virtio_input.o obj-$(CONFIG_VIRTIO_VDPA) +=3D virtio_vdpa.o obj-$(CONFIG_VIRTIO_MEM) +=3D virtio_mem.o obj-$(CONFIG_VIRTIO_DMA_SHARED_BUFFER) +=3D virtio_dma_buf.o +obj-$(CONFIG_VIRTIO_RTC) +=3D virtio_rtc.o +virtio_rtc-y :=3D virtio_rtc_driver.o diff --git a/drivers/virtio/virtio_rtc_driver.c b/drivers/virtio/virtio_rtc= _driver.c new file mode 100644 index 000000000000..424500d2c4f7 --- /dev/null +++ b/drivers/virtio/virtio_rtc_driver.c @@ -0,0 +1,736 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * virtio_rtc driver core + * + * Copyright (C) 2022-2023 OpenSynergy GmbH + */ + +#include +#include +#include +#include +#include + +#include + +#include "virtio_rtc_internal.h" + +/* virtqueue order */ +enum { + VIORTC_READQ, + VIORTC_CONTROLQ, + VIORTC_MAX_NR_QUEUES, +}; + +/** + * struct viortc_vq - virtqueue abstraction + * @vq: virtqueue + * @lock: protects access to vq + */ +struct viortc_vq { + struct virtqueue *vq; + spinlock_t lock; +}; + +/** + * struct viortc_dev - virtio_rtc device data + * @vdev: virtio device + * @vqs: virtqueues + * @num_clocks: # of virtio_rtc clocks + */ +struct viortc_dev { + struct virtio_device *vdev; + struct viortc_vq vqs[VIORTC_MAX_NR_QUEUES]; + u16 num_clocks; +}; + +/** + * struct viortc_msg - Message requested by driver, responded by device. + * @viortc: device data + * @req: request buffer + * @resp: response buffer + * @responded: vqueue callback signals response reception + * @refcnt: Message reference count, message and buffers will be deallocat= ed + * once 0. refcnt is decremented in the vqueue callback and in the + * thread waiting on the responded completion. + * If a message response wait function times out, the message wil= l be + * freed upon late reception (refcnt will reach 0 in the callback= ), or + * device removal. + * @req_size: size of request in bytes + * @resp_cap: maximum size of response in bytes + * @resp_actual_size: actual size of response + */ +struct viortc_msg { + struct viortc_dev *viortc; + void *req; + void *resp; + struct completion responded; + refcount_t refcnt; + unsigned int req_size; + unsigned int resp_cap; + unsigned int resp_actual_size; +}; + +/** + * viortc_msg_init() - Allocate and initialize message. + * @viortc: device data + * @msg_type: virtio_rtc message type + * @req_size: size of request buffer to be allocated + * @resp_cap: size of response buffer to be allocated + * + * Initializes the message refcnt to 2. The refcnt will be decremented onc= e in + * the virtqueue callback, and once in the thread waiting on the message (= on + * completion or timeout). + * + * Context: Process context. + * Return: non-NULL on success. + */ +static struct viortc_msg *viortc_msg_init(struct viortc_dev *viortc, + u16 msg_type, unsigned int req_size, + unsigned int resp_cap) +{ + struct viortc_msg *msg; + struct device *dev =3D &viortc->vdev->dev; + struct virtio_rtc_req_head *req_head; + + msg =3D devm_kzalloc(dev, sizeof(*msg), GFP_KERNEL); + if (!msg) + return NULL; + + init_completion(&msg->responded); + + msg->req =3D devm_kzalloc(dev, req_size, GFP_KERNEL); + if (!msg->req) + goto err_free_msg; + + req_head =3D msg->req; + + msg->resp =3D devm_kzalloc(dev, resp_cap, GFP_KERNEL); + if (!msg->resp) + goto err_free_msg_req; + + msg->viortc =3D viortc; + msg->req_size =3D req_size; + msg->resp_cap =3D resp_cap; + + refcount_set(&msg->refcnt, 2); + + req_head->msg_type =3D virtio_cpu_to_le(msg_type, req_head->msg_type); + + return msg; + +err_free_msg_req: + devm_kfree(dev, msg->req); + +err_free_msg: + devm_kfree(dev, msg); + + return NULL; +} + +/** + * viortc_msg_release() - Decrement message refcnt, potentially free messa= ge. + * @msg: message requested by driver + * + * Context: Any context. + */ +static void viortc_msg_release(struct viortc_msg *msg) +{ + if (refcount_dec_and_test(&msg->refcnt)) { + struct device *dev =3D &msg->viortc->vdev->dev; + + devm_kfree(dev, msg->req); + devm_kfree(dev, msg->resp); + devm_kfree(dev, msg); + } +} + +/** + * viortc_cb() - callback for readq and controlq + * @vq: virtqueue with device response + * + * Signals completion for each received message. + * + * Context: virtqueue callback, typically interrupt. Takes and releases vq= lock. + */ +static void viortc_cb(struct virtqueue *vq) +{ + struct viortc_dev *viortc =3D vq->vdev->priv; + spinlock_t *lock =3D &viortc->vqs[vq->index].lock; + unsigned long flags; + struct viortc_msg *msg; + unsigned int len; + bool cb_enabled =3D true; + + for (;;) { + spin_lock_irqsave(lock, flags); + + if (cb_enabled) { + virtqueue_disable_cb(vq); + cb_enabled =3D false; + } + + msg =3D virtqueue_get_buf(vq, &len); + if (!msg) { + if (virtqueue_enable_cb(vq)) { + spin_unlock_irqrestore(lock, flags); + return; + } + cb_enabled =3D true; + } + + spin_unlock_irqrestore(lock, flags); + + if (msg) { + msg->resp_actual_size =3D len; + + /* + * completion waiter must see our msg metadata, but + * complete() does not guarantee a memory barrier + */ + smp_wmb(); + + complete(&msg->responded); + viortc_msg_release(msg); + } + } +} + +/** + * viortc_get_resp_errno() - converts virtio_rtc errnos to system errnos + * @resp_head: message response header + * + * Return: negative system errno, or 0 + */ +static int viortc_get_resp_errno(struct virtio_rtc_resp_head *resp_head) +{ + switch (virtio_le_to_cpu(resp_head->status)) { + case VIRTIO_RTC_S_OK: + return 0; + case VIRTIO_RTC_S_UNSUPP: + return -EOPNOTSUPP; + case VIRTIO_RTC_S_INVAL: + return -EINVAL; + case VIRTIO_RTC_S_NODEV: + return -ENODEV; + case VIRTIO_RTC_S_DEVERR: + default: + return -EIO; + } +} + +/** + * viortc_msg_xfer() - send message request, wait until message response + * @vq: virtqueue + * @msg: message with driver request + * @timeout_jiffies: message response timeout, 0 for no timeout + * + * Context: Process context. Takes and releases vq.lock. May sleep. + */ +static int viortc_msg_xfer(struct viortc_vq *vq, struct viortc_msg *msg, + unsigned long timeout_jiffies) +{ + int ret; + unsigned long flags; + struct scatterlist out_sg[1]; + struct scatterlist in_sg[1]; + struct scatterlist *sgs[2] =3D { out_sg, in_sg }; + bool notify; + + sg_init_one(out_sg, msg->req, msg->req_size); + sg_init_one(in_sg, msg->resp, msg->resp_cap); + + spin_lock_irqsave(&vq->lock, flags); + + ret =3D virtqueue_add_sgs(vq->vq, sgs, 1, 1, msg, GFP_ATOMIC); + if (ret) { + spin_unlock_irqrestore(&vq->lock, flags); + /* + * Release in place of the response callback, which will never + * come. + */ + viortc_msg_release(msg); + return ret; + } + + notify =3D virtqueue_kick_prepare(vq->vq); + + spin_unlock_irqrestore(&vq->lock, flags); + + if (notify) + virtqueue_notify(vq->vq); + + if (timeout_jiffies) { + long timeout_ret; + + timeout_ret =3D wait_for_completion_interruptible_timeout( + &msg->responded, timeout_jiffies); + + if (!timeout_ret) + return -ETIMEDOUT; + else if (timeout_ret < 0) + return (int)timeout_ret; + } else { + ret =3D wait_for_completion_interruptible(&msg->responded); + if (ret) + return ret; + } + + /* + * Ensure we can read message metadata written in the virtqueue + * callback. + */ + smp_rmb(); + + /* + * There is not yet a case where returning a short message would make + * sense, so consider any deviation an error. + */ + if (msg->resp_actual_size !=3D msg->resp_cap) + return -EINVAL; + + return viortc_get_resp_errno(msg->resp); +} + +/* + * common message handle macros for messages of different types + */ + +/** + * VIORTC_DECLARE_MSG_HDL_ONSTACK() - declare message handle on stack + * @hdl: message handle name + * @msg_suf_lowerc: message type suffix in lowercase + * @msg_suf_upperc: message type suffix in uppercase + */ +#define VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, msg_suf_lowerc, msg_suf_upperc= ) \ + struct { \ + struct viortc_msg *msg; \ + struct virtio_rtc_req_##msg_suf_lowerc *req; \ + struct virtio_rtc_resp_##msg_suf_lowerc *resp; \ + unsigned int req_size; \ + unsigned int resp_cap; \ + u16 msg_type; \ + } hdl =3D { \ + NULL, \ + NULL, \ + NULL, \ + sizeof(struct virtio_rtc_req_##msg_suf_lowerc), \ + sizeof(struct virtio_rtc_resp_##msg_suf_lowerc), \ + VIRTIO_RTC_M_##msg_suf_upperc, \ + } + +/** + * VIORTC_MSG() - extract message from message handle + * + * Return: struct viortc_msg + */ +#define VIORTC_MSG(hdl) ((hdl).msg) + +/** + * VIORTC_MSG_INIT() - initialize message handle + * @hdl: message handle + * @viortc: device data (struct viortc_dev *) + * + * Context: Process context. + * Return: 0 on success, -ENOMEM otherwise. + */ +#define VIORTC_MSG_INIT(hdl, viortc) = \ + ({ \ + typeof(hdl) *_hdl =3D &(hdl); \ + \ + _hdl->msg =3D viortc_msg_init((viortc), _hdl->msg_type, \ + _hdl->req_size, _hdl->resp_cap); \ + if (_hdl->msg) { \ + _hdl->req =3D _hdl->msg->req; \ + _hdl->resp =3D _hdl->msg->resp; \ + } \ + _hdl->msg ? 0 : -ENOMEM; \ + }) + +/** + * VIORTC_MSG_WRITE() - write a request message field + * @hdl: message handle + * @dest_member: request message field name + * @src_ptr: pointer to data of compatible type + * + * Writes the field in little-endian format. + */ +#define VIORTC_MSG_WRITE(hdl, dest_member, src_ptr) = \ + do { \ + typeof(hdl) _hdl =3D (hdl); \ + typeof(src_ptr) _src_ptr =3D (src_ptr); \ + \ + /* Sanity check: must match the member's type */ \ + typecheck(typeof(_hdl.req->dest_member), *_src_ptr); \ + \ + _hdl.req->dest_member =3D \ + virtio_cpu_to_le(*_src_ptr, _hdl.req->dest_member); \ + } while (0) + +/** + * VIORTC_MSG_READ() - read from a response message field + * @hdl: message handle + * @src_member: response message field name + * @dest_ptr: pointer to data of compatible type + * + * Converts from little-endian format and writes to dest_ptr. + */ +#define VIORTC_MSG_READ(hdl, src_member, dest_ptr) \ + do { \ + typeof(dest_ptr) _dest_ptr =3D (dest_ptr); \ + \ + /* Sanity check: must match the member's type */ \ + typecheck(typeof((hdl).resp->src_member), *_dest_ptr); \ + \ + *_dest_ptr =3D virtio_le_to_cpu((hdl).resp->src_member); \ + } while (0) + +/* + * readq messages + */ + +/** timeout for clock readings, where timeouts are considered non-fatal */ +#define VIORTC_MSG_READ_TIMEOUT (msecs_to_jiffies(60 * 1000)) + +/** + * viortc_read() - VIRTIO_RTC_M_READ message wrapper + * @viortc: device data + * @vio_clk_id: virtio_rtc clock id + * @reading: clock reading [ns] + * + * Context: Process context. + * Return: Zero on success, negative error code otherwise. + */ +int viortc_read(struct viortc_dev *viortc, u64 vio_clk_id, u64 *reading) +{ + int ret; + VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, read, READ); + + ret =3D VIORTC_MSG_INIT(hdl, viortc); + if (ret) + return ret; + + VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id); + + ret =3D viortc_msg_xfer(&viortc->vqs[VIORTC_READQ], VIORTC_MSG(hdl), + VIORTC_MSG_READ_TIMEOUT); + if (ret) { + dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__, + ret); + goto out_release; + } + + VIORTC_MSG_READ(hdl, clock_reading, reading); + +out_release: + viortc_msg_release(VIORTC_MSG(hdl)); + + return ret; +} + +/** + * viortc_read_cross() - VIRTIO_RTC_M_READ_CROSS message wrapper + * @viortc: device data + * @vio_clk_id: virtio_rtc clock id + * @hw_counter: virtio_rtc HW counter type + * @reading: clock reading [ns] + * @cycles: HW counter cycles during clock reading + * + * Context: Process context. + * Return: Zero on success, negative error code otherwise. + */ +int viortc_read_cross(struct viortc_dev *viortc, u64 vio_clk_id, u16 hw_co= unter, + u64 *reading, u64 *cycles) +{ + int ret; + VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, read_cross, READ_CROSS); + + ret =3D VIORTC_MSG_INIT(hdl, viortc); + if (ret) + return ret; + + VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id); + VIORTC_MSG_WRITE(hdl, hw_counter, &hw_counter); + + ret =3D viortc_msg_xfer(&viortc->vqs[VIORTC_READQ], VIORTC_MSG(hdl), + VIORTC_MSG_READ_TIMEOUT); + if (ret) { + dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__, + ret); + goto out_release; + } + + VIORTC_MSG_READ(hdl, clock_reading, reading); + VIORTC_MSG_READ(hdl, counter_cycles, cycles); + +out_release: + viortc_msg_release(VIORTC_MSG(hdl)); + + return ret; +} + +/* + * controlq messages + */ + +/** + * viortc_cfg() - VIRTIO_RTC_M_CFG message wrapper + * @viortc: device data + * @num_clocks: # of virtio_rtc clocks + * + * Context: Process context. + * Return: Zero on success, negative error code otherwise. + */ +static int viortc_cfg(struct viortc_dev *viortc, u16 *num_clocks) +{ + int ret; + VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, cfg, CFG); + + ret =3D VIORTC_MSG_INIT(hdl, viortc); + if (ret) + return ret; + + ret =3D viortc_msg_xfer(&viortc->vqs[VIORTC_CONTROLQ], VIORTC_MSG(hdl), + 0); + if (ret) { + dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__, + ret); + goto out_release; + } + + VIORTC_MSG_READ(hdl, num_clocks, num_clocks); + +out_release: + viortc_msg_release(VIORTC_MSG(hdl)); + + return ret; +} + +/** + * viortc_clock_cap() - VIRTIO_RTC_M_CLOCK_CAP message wrapper + * @viortc: device data + * @vio_clk_id: virtio_rtc clock id + * @type: virtio_rtc clock type + * + * Context: Process context. + * Return: Zero on success, negative error code otherwise. + */ +static int viortc_clock_cap(struct viortc_dev *viortc, u64 vio_clk_id, + u16 *type) +{ + int ret; + VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, clock_cap, CLOCK_CAP); + + ret =3D VIORTC_MSG_INIT(hdl, viortc); + if (ret) + return ret; + + VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id); + + ret =3D viortc_msg_xfer(&viortc->vqs[VIORTC_CONTROLQ], VIORTC_MSG(hdl), + 0); + if (ret) { + dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__, + ret); + goto out_release; + } + + VIORTC_MSG_READ(hdl, type, type); + +out_release: + viortc_msg_release(VIORTC_MSG(hdl)); + + return ret; +} + +/** + * viortc_cross_cap() - VIRTIO_RTC_M_CROSS_CAP message wrapper + * @viortc: device data + * @vio_clk_id: virtio_rtc clock id + * @hw_counter: virtio_rtc HW counter type + * @supported: xtstamping is supported for the vio_clk_id/hw_counter pair + * + * Context: Process context. + * Return: Zero on success, negative error code otherwise. + */ +int viortc_cross_cap(struct viortc_dev *viortc, u64 vio_clk_id, u16 hw_cou= nter, + bool *supported) +{ + int ret; + VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, cross_cap, CROSS_CAP); + u8 flags; + + ret =3D VIORTC_MSG_INIT(hdl, viortc); + if (ret) + return ret; + + VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id); + VIORTC_MSG_WRITE(hdl, hw_counter, &hw_counter); + + ret =3D viortc_msg_xfer(&viortc->vqs[VIORTC_CONTROLQ], VIORTC_MSG(hdl), + 0); + if (ret) { + dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__, + ret); + goto out_release; + } + + VIORTC_MSG_READ(hdl, flags, &flags); + *supported =3D !!(flags & BIT(VIRTIO_RTC_FLAG_CROSS_CAP)); + +out_release: + viortc_msg_release(VIORTC_MSG(hdl)); + + return ret; +} + +/* + * init, deinit + */ + +/** + * viortc_clocks_init() - init local representations of virtio_rtc clocks + * @viortc: device data + * + * Context: Process context. + * Return: Zero on success, negative error code otherwise. + */ +static int viortc_clocks_init(struct viortc_dev *viortc) +{ + int ret; + u16 num_clocks; + + ret =3D viortc_cfg(viortc, &num_clocks); + if (ret) + return ret; + + if (num_clocks < 1) { + dev_err(&viortc->vdev->dev, "device reported 0 clocks\n"); + return -ENODEV; + } + + viortc->num_clocks =3D num_clocks; + + /* In the future, PTP clocks will be initialized here. */ + (void)viortc_clock_cap; + + return 0; +} + +/** + * viortc_init_vqs() - init virtqueues + * @viortc: device data + * + * Context: Process context. + * Return: Zero on success, negative error code otherwise. + * + * Init virtqueues, and their abstractions. + */ +static int viortc_init_vqs(struct viortc_dev *viortc) +{ + int ret; + struct virtio_device *vdev =3D viortc->vdev; + const char *names[VIORTC_MAX_NR_QUEUES]; + vq_callback_t *callbacks[VIORTC_MAX_NR_QUEUES]; + struct virtqueue *vqs[VIORTC_MAX_NR_QUEUES]; + int nr_queues; + + names[VIORTC_READQ] =3D "readq"; + callbacks[VIORTC_READQ] =3D viortc_cb; + + names[VIORTC_CONTROLQ] =3D "controlq"; + callbacks[VIORTC_CONTROLQ] =3D viortc_cb; + + nr_queues =3D 2; + + ret =3D virtio_find_vqs(vdev, nr_queues, vqs, callbacks, names, NULL); + if (ret) + return ret; + + viortc->vqs[VIORTC_READQ].vq =3D vqs[VIORTC_READQ]; + spin_lock_init(&viortc->vqs[VIORTC_READQ].lock); + + viortc->vqs[VIORTC_CONTROLQ].vq =3D vqs[VIORTC_CONTROLQ]; + spin_lock_init(&viortc->vqs[VIORTC_CONTROLQ].lock); + + return 0; +} + +/** + * viortc_probe() - probe a virtio_rtc virtio device + * @vdev: virtio device + * + * Context: Process context. + * Return: Zero on success, negative error code otherwise. + */ +static int viortc_probe(struct virtio_device *vdev) +{ + struct viortc_dev *viortc; + int ret; + + viortc =3D devm_kzalloc(&vdev->dev, sizeof(*viortc), GFP_KERNEL); + if (!viortc) + return -ENOMEM; + + vdev->priv =3D viortc; + viortc->vdev =3D vdev; + + ret =3D viortc_init_vqs(viortc); + if (ret) + return ret; + + virtio_device_ready(vdev); + + /* Ready vdev for use by frontend devices initialized next. */ + smp_wmb(); + + ret =3D viortc_clocks_init(viortc); + if (ret) + goto err_reset_vdev; + + return 0; + +err_reset_vdev: + virtio_reset_device(vdev); + vdev->config->del_vqs(vdev); + + return ret; +} + +/** + * viortc_remove() - remove a virtio_rtc virtio device + * @vdev: virtio device + */ +static void viortc_remove(struct virtio_device *vdev) +{ + /* In the future, PTP clocks will be deinitialized here. */ + + virtio_reset_device(vdev); + vdev->config->del_vqs(vdev); +} + +static unsigned int features[] =3D { + VIRTIO_RTC_F_READ_CROSS, +}; + +static struct virtio_device_id id_table[] =3D { + { VIRTIO_ID_CLOCK, VIRTIO_DEV_ANY_ID }, + { 0 }, +}; +MODULE_DEVICE_TABLE(virtio, id_table); + +static struct virtio_driver virtio_rtc_drv =3D { + .driver.name =3D KBUILD_MODNAME, + .driver.owner =3D THIS_MODULE, + .feature_table =3D features, + .feature_table_size =3D ARRAY_SIZE(features), + .id_table =3D id_table, + .probe =3D viortc_probe, + .remove =3D viortc_remove, +}; + +module_virtio_driver(virtio_rtc_drv); + +MODULE_DESCRIPTION("Virtio RTC driver"); +MODULE_AUTHOR("OpenSynergy GmbH"); +MODULE_LICENSE("GPL"); diff --git a/drivers/virtio/virtio_rtc_internal.h b/drivers/virtio/virtio_r= tc_internal.h new file mode 100644 index 000000000000..c2b5387f506f --- /dev/null +++ b/drivers/virtio/virtio_rtc_internal.h @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * virtio_rtc internal interfaces + * + * Copyright (C) 2022-2023 OpenSynergy GmbH + */ + +#ifndef _VIRTIO_RTC_INTERNAL_H_ +#define _VIRTIO_RTC_INTERNAL_H_ + +#include + +/* driver core IFs */ + +struct viortc_dev; + +int viortc_read(struct viortc_dev *viortc, u64 vio_clk_id, u64 *reading); +int viortc_read_cross(struct viortc_dev *viortc, u64 vio_clk_id, u16 hw_co= unter, + u64 *reading, u64 *cycles); +int viortc_cross_cap(struct viortc_dev *viortc, u64 vio_clk_id, u16 hw_cou= nter, + bool *supported); + +#endif /* _VIRTIO_RTC_INTERNAL_H_ */ diff --git a/include/uapi/linux/virtio_rtc.h b/include/uapi/linux/virtio_rt= c.h new file mode 100644 index 000000000000..0926b3d58254 --- /dev/null +++ b/include/uapi/linux/virtio_rtc.h @@ -0,0 +1,159 @@ +/* SPDX-License-Identifier: ((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-C= lause) */ +/* + * Copyright (C) 2022-2023 OpenSynergy GmbH + */ + +#ifndef _LINUX_VIRTIO_RTC_H +#define _LINUX_VIRTIO_RTC_H + +#include + +/* Device-specific features */ + +#define VIRTIO_RTC_F_READ_CROSS 0 + +/* readq message types */ + +#define VIRTIO_RTC_M_READ 0x0001 +#define VIRTIO_RTC_M_READ_CROSS 0x0002 + +/* controlq message types */ + +#define VIRTIO_RTC_M_CFG 0x1000 +#define VIRTIO_RTC_M_CLOCK_CAP 0x1001 +#define VIRTIO_RTC_M_CROSS_CAP 0x1002 + +/* Message headers */ + +/** common request header */ +struct virtio_rtc_req_head { + __le16 msg_type; + __u8 reserved[2]; +}; + +/** common response header */ +struct virtio_rtc_resp_head { +#define VIRTIO_RTC_S_OK 0 +#define VIRTIO_RTC_S_UNSUPP 1 +#define VIRTIO_RTC_S_NODEV 2 +#define VIRTIO_RTC_S_INVAL 3 +#define VIRTIO_RTC_S_DEVERR 4 + __u8 status; + __u8 reserved[3]; +}; + +/* readq messages */ + +/* VIRTIO_RTC_M_READ message */ + +struct virtio_rtc_req_read { + struct virtio_rtc_req_head head; + __u8 reserved[4]; + __le64 clock_id; +}; + +struct virtio_rtc_resp_read { + struct virtio_rtc_resp_head head; + __u8 reserved[4]; + __le64 clock_reading; +}; + +/* VIRTIO_RTC_M_READ_CROSS message */ + +struct virtio_rtc_req_read_cross { + struct virtio_rtc_req_head head; +/** Arm Generic Timer Virtual Count */ +#define VIRTIO_RTC_COUNTER_ARM_VIRT 0 +/** Arm Generic Timer Physical Count */ +#define VIRTIO_RTC_COUNTER_ARM_PHYS 1 +/** x86 Time Stamp Counter */ +#define VIRTIO_RTC_COUNTER_X86_TSC 2 + __le16 hw_counter; + __u8 reserved[2]; + __le64 clock_id; +}; + +struct virtio_rtc_resp_read_cross { + struct virtio_rtc_resp_head head; + __u8 reserved[4]; + __le64 clock_reading; + __le64 counter_cycles; +}; + +/** Union of request types for readq */ +union virtio_rtc_req_readq { + struct virtio_rtc_req_read read; + struct virtio_rtc_req_read_cross read_cross; +}; + +/** Union of response types for readq */ +union virtio_rtc_resp_readq { + struct virtio_rtc_resp_read read; + struct virtio_rtc_resp_read_cross read_cross; +}; + +/* controlq messages */ + +/* VIRTIO_RTC_M_CFG message */ + +struct virtio_rtc_req_cfg { + struct virtio_rtc_req_head head; + /* no request params */ + __u8 reserved[4]; +}; + +struct virtio_rtc_resp_cfg { + struct virtio_rtc_resp_head head; + /** # of clocks -> clock ids < num_clocks are valid */ + __le16 num_clocks; + __u8 reserved[10]; +}; + +/* VIRTIO_RTC_M_CLOCK_CAP message */ + +struct virtio_rtc_req_clock_cap { + struct virtio_rtc_req_head head; + __u8 reserved[4]; + __le64 clock_id; +}; + +struct virtio_rtc_resp_clock_cap { + struct virtio_rtc_resp_head head; +#define VIRTIO_RTC_CLOCK_UTC 0 +#define VIRTIO_RTC_CLOCK_TAI 1 +#define VIRTIO_RTC_CLOCK_MONO 2 + __le16 type; + __u8 reserved[10]; +}; + +/* VIRTIO_RTC_M_CROSS_CAP message */ + +struct virtio_rtc_req_cross_cap { + struct virtio_rtc_req_head head; + __le16 hw_counter; + __u8 reserved[2]; + __le64 clock_id; +}; + +struct virtio_rtc_resp_cross_cap { + struct virtio_rtc_resp_head head; +#define VIRTIO_RTC_FLAG_CROSS_CAP 0 + __u8 flags; + __u8 reserved[11]; +}; + +/** Union of request types for controlq */ +union virtio_rtc_req_controlq { + struct virtio_rtc_req_cfg cfg; + struct virtio_rtc_req_clock_cap clock_cap; + struct virtio_rtc_req_cross_cap cross_cap; +}; + +/** Union of response types for controlq */ +union virtio_rtc_resp_controlq { + struct virtio_rtc_resp_cfg cfg; + struct virtio_rtc_resp_clock_cap clock_cap; + struct virtio_rtc_resp_cross_cap cross_cap; +}; + +#endif /* _LINUX_VIRTIO_RTC_H */ --=20 2.39.2 From nobody Sun Feb 8 02:56:00 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07720EB64DA for ; Fri, 30 Jun 2023 17:27:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233084AbjF3R13 (ORCPT ); Fri, 30 Jun 2023 13:27:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233061AbjF3R1Y (ORCPT ); Fri, 30 Jun 2023 13:27:24 -0400 Received: from refb02.tmes.trendmicro.eu (refb02.tmes.trendmicro.eu [18.185.115.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F9851FF2 for ; Fri, 30 Jun 2023 10:27:20 -0700 (PDT) Received: from 104.47.11.177_.trendmicro.com (unknown [172.21.19.51]) by refb02.tmes.trendmicro.eu (Postfix) with ESMTPS id BC2B7108FF1DB; Fri, 30 Jun 2023 17:12:34 +0000 (UTC) Received: from 104.47.11.177_.trendmicro.com (unknown [172.21.178.36]) by repost01.tmes.trendmicro.eu (Postfix) with SMTP id 32A4C100004FF; Fri, 30 Jun 2023 17:12:33 +0000 (UTC) X-TM-MAIL-RECEIVED-TIME: 1688145152.682000 X-TM-MAIL-UUID: 3871fe6c-b6a5-4d22-b8ba-6073f2918062 Received: from DEU01-FR2-obe.outbound.protection.outlook.com (unknown [104.47.11.177]) by repre01.tmes.trendmicro.eu (Trend Micro Email Security) with ESMTPS id A6CF2100003DA; Fri, 30 Jun 2023 17:12:32 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hhxmdl2MY8W4kWuMPZaBD6E7Hje4A29VJ8ZGykf2vG1cGU2ifxPm5JRcNoS/cniyAHoKaqRVkONTEk7d1JTPqb0tYPfNambhtv0iqV3SnXGcoCq9nTjO5bJvwdrYWc31UulvCVuYUlYUv1vvF0VtNpFIwUdtPb3OS8vr+1cz/DT0pVGTyG7G2k8pkNezM7wKHafpU7xUhl7Qmk82dBuFLUZbpbrAjxgcpOQjf88w2WPpsKWDGYo3wPoeorBBWB9S5IebSP5l/XoZxuIHOAFl3EZ2qt+DsNQPsmv3iIE2OQNfgLDZuWapbAiKgTpA1EIqlucEQvleUNvn9NztZvWQUw== 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=HtTkodt8TjTUIJXjHXZ5ggCtrxGe55E6vmqBOnZiztw=; b=FvdIkuDm5ivoDV5i/q6QzOMAuDFSK3LVlL90c1FTJ1JEUJ/vdL1p0P66szN2qg/Qv+32LPQ/3mivYwUqWG0+WGVRM3PJFonOeFaEQCIvMCDormGTJCww4ZJnXPDw1YTzcFX3dZlFEV4QYURR5hFWvHX38DnNfBQPhIM92h2dDZ39sMMPuGEIhW1zLfJLJmyl0J7oSMa/rqJgzhZ37/mpr906dn5KlV2AdNsGPbbXmxqubGcO4q6+N9/ep9jj/LN8bAIosoYKe0KSzMcFYveDU6rRoyRrTqFPP+p1aPnLvhwqd2HB2vh4EO778BlBgM6g01WjhwoPqAvljVUh14Tmug== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 217.66.60.4) smtp.rcpttodomain=gmail.com smtp.mailfrom=opensynergy.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=opensynergy.com; dkim=none (message not signed); arc=none X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 217.66.60.4) smtp.mailfrom=opensynergy.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=opensynergy.com; Received-SPF: Pass (protection.outlook.com: domain of opensynergy.com designates 217.66.60.4 as permitted sender) receiver=protection.outlook.com; client-ip=217.66.60.4; helo=SR-MAIL-03.open-synergy.com; pr=C From: Peter Hilber To: virtualization@lists.linux-foundation.org, virtio-dev@lists.oasis-open.org Cc: Peter Hilber , linux-kernel@vger.kernel.org, "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , Richard Cochran , netdev@vger.kernel.org Subject: [RFC PATCH 6/7] virtio_rtc: Add PTP clocks Date: Fri, 30 Jun 2023 19:10:49 +0200 Message-Id: <20230630171052.985577-7-peter.hilber@opensynergy.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230630171052.985577-1-peter.hilber@opensynergy.com> References: <20230630171052.985577-1-peter.hilber@opensynergy.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB8EUR05FT005:EE_|FR2P281MB0282:EE_ X-MS-Office365-Filtering-Correlation-Id: d4cfacfd-980d-44b7-0e19-08db798d305d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AE8ZUmmKrnnqU7aS4l2Jbei9XqbQWkyl4lu1CRfd8mwftT/JI8qodJAA5XhiSnN0SDKL6/1opsxx6GhwpPcQt6r4ZW6nw3AP2PuzSynX4s1TXX6NqAFUmiNSdgw1tV9l7QRUKagvDWzF5awB65fSfoy5h2CmWEamxyBEFM/Yz87WQfGpvO3mr8/Xlnyqiu6sB24ZCEY2O5301U9GfjOyLmX71YagZxCv/EJ472o69Mpqqp/XasRfse+NnuuTDIIJ2lHaLip9xG32J3cmRkQ4F9NAkC/Gz3orVlleban40hiJ/8nOFHbzz3vRqCWAsCta9kvD4iOnM9QU4O1OK5+5eOjn/0XC1kd3sz8TmMZQkVE3Mlq5SsP+1UboNAd8T+bgJ7vKxhYFjblCFOyqJ9Mwmc+66Gr3B+b26eKd2Vdm6FZwAy1aLgqA+XuwlV0j2Twg+Tm1poaQ8BQdwKdLlb+zvAYpzKZ50UxYGO5OUsmd6DmhA5b/x1zkRUvi2HAu+ND218h7DO/CM6OzxWvUOQ8UrsvGdo/WG24P0VOsddSWYSoYHNOvJldlpgfaEaAR4aKccyA29OBN9FnFfrKtSmp1VC1B6KIIc8QkC9eRA6Pd/uwxrGUJ9vCqTueDWzbklYUdkwNtIRrcE8tuELXExPc4aFuZQP6dWzekS9KBPBIVKvFDRQw2wy4OM6I2kZWresVxsDO5QLLmliBo3fHpayjmAglfXMpVUEaNP76hNBi+KC2WHnjZ5t+Tz0LPEhmOE6Xwo/ZYE+ChRArIFh8/vRafsw== X-Forefront-Antispam-Report: CIP:217.66.60.4;CTRY:DE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SR-MAIL-03.open-synergy.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(396003)(136003)(346002)(376002)(39830400003)(451199021)(46966006)(36840700001)(2616005)(81166007)(186003)(1076003)(83380400001)(36860700001)(47076005)(40480700001)(26005)(336012)(30864003)(44832011)(41300700001)(5660300002)(8936002)(8676002)(2906002)(36756003)(42186006)(316002)(86362001)(70206006)(70586007)(4326008)(478600001)(54906003)(82310400005)(36900700001);DIR:OUT;SFP:1102; X-OriginatorOrg: opensynergy.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jun 2023 17:12:31.0717 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d4cfacfd-980d-44b7-0e19-08db798d305d X-MS-Exchange-CrossTenant-Id: 800fae25-9b1b-4edc-993d-c939c4e84a64 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=800fae25-9b1b-4edc-993d-c939c4e84a64;Ip=[217.66.60.4];Helo=[SR-MAIL-03.open-synergy.com] X-MS-Exchange-CrossTenant-AuthSource: DB8EUR05FT005.eop-eur05.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: FR2P281MB0282 X-TM-AS-ERS: 104.47.11.177-0.0.0.0 X-TMASE-Version: StarCloud-1.3-9.1.1011-27724.001 X-TMASE-Result: 10--16.843500-4.000000 X-TMASE-MatchedRID: vv7MY3lDOKoY9bhGrdH9F9wy9mwTudtiRyG5SSR7mMGmopAzNrNOJn+F psIKKIVlk8IAUKtJYbMmnuaVQc5qV4oSaqhQo1toHW+C8sNk3WDFZzmk4ajzuCe5CWghtIzPb1O 21F3h9PlzEFhMBQYFa/6kCjQkL+3Saroq2fxRivXHfwwbjh/dN1pMWq1GqY9jyEYRIXZC1hKyCR +docAU/Ob1Nz8aB8UnAW1a55mUwfQHfNbJzbBb9f0hW+Pi7Cos3V8nMuq8B6LlbwjdfEVENOqqk o1PXeESs2CEDKA6J1Ob2SBPGBzRQQIfpHdEtL3FiL+RrlWsewoHgEx+rhLN4ngwJo5QMsV9KMNS xwZpXDY9yytr4YofCE5yycj+UwCjMUcXkeXNyfeK7q7iyFn58WnTqal/GlimkVj8lcfFncZldff r8GYLT6jZdiFdYobCo9RNpYDQkvQTevXTasglgVY9XYa1m8xAIQdkvxKngOsC28FWVjkhF9ho/I sfJ5coNljDlR2ZyXisiaeLjSuHt97BQ4zNEspZGwz+ElW8lobsrnN2nOWPXqiqsiuuQaXipVLP8 mlEC5lKjlC2ODrqQEGt9OJES2KrzNq26dMbJoCjmpJtigrtbnJ8bM38R9gpJsfJJdVp3fqvngkj HjojpH2fbnp7T2HmiXG+DOC9637Q6QzQbo9igznxJqun3snzJQhrLH5KSJ0= X-TMASE-XGENCLOUD: b584f51f-8ad7-4750-a884-cb78ad6a3201-0-0-200-0 X-TM-Deliver-Signature: 25589D73EFA9610A5AC4D72DD90123B8 X-TM-Addin-Auth: 0RD+6gItsc0HPlz1WG6qHUNsXbWh7UcZmeBxvMejIeHxSXtbXZIR4iVD90X cbVhNpkOJPA24HMxUCx9U6wWZqZ3QwGrhgz6lCE40i4TxEkM6l1iqylsX4n4KyZGTELijOS6KUp sbQAOHfXjq5aJBi370BaRunWPAYgSY9GDhjcylHO7isbNuOUA5JMg+AWwQBKndX7s1ieCKeRFG5 wWdSUPfI22qEmLThkkzOxfwIn/EPhDzl9ImZIIMFNC1p/BOIZWBf1ZeYGIO/SOqglEQnNt3UzwI g6aKOsVmJRrdXsM=.UKRAVygnQxQ4NR8AjlO+iFdbxMafzR7kKUEl5Pxz3+O4SeRTkp76ppUfx/ hzNwwk44Ex+p2Pyg+BGabUxE67Nvoa+IPpQ/FjDI4O26YE//C+dHsHAGX0D0xSgOI/CsX+2GpAk uC36YebagjxBS8C1Vk+Esw9sjppV44uaw5zybPfpvVnTMKQnnTLVaNMvbnsWD588AW9lsDyCa0l EFV5fh1bju/s876iZko0sIlYP0M1ISvIJqJwIveesiFaE5bMrImiR7z7KAfOV33wI4zEebYZyI6 8XU9/d6sbWYWIWV4/NTzaW10kkdXTEcxOUkGjRwJuZGFsvgsG9ahd7EzyzA== X-TM-Addin-ProductCode: EMS DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=opensynergy.com; s=TM-DKIM-20210503141657; t=1688145153; bh=s2A5FGg6U2xNr2VnGVaTfvO0HLF7T3Yy42ZePqFzDEY=; l=20488; h=From:To:Date; b=aKHkpqPxrepG+Ju8Oc41Vplt2Rbas1JR/ije0oGPvOBXFzsXw+k17bpDjvFF0crO5 TCzMi/WQPc67utO6e6+bVO12kirBIHKeAB5WkbpuBOqpvnh/kTRb8JTmN5WV8ffbqA 4IVL/wiDCxAmkJ9ARIURYVnXBLkLjXVQwzwsNH3oa1hqn/OFcElm9FqBb4VbdESMpJ zLuvvAQG0jLWRnVWQ9P4HP9/ULquAmu3iygiuJ34VtpIOW7z48U3aa2OjZB0C6Wfc2 /BDByYKo7wbwxqoYKtDw7lUObP+07svSdTpyA3RY0PnKsvhVuIgiffPjnpeZQaPNbA wNHwORRiiO6wA== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Expose the virtio_rtc clocks as PTP clocks to userspace, similar to ptp_kvm. virtio_rtc can expose multiple clocks, e.g. a UTC clock and a monotonic clock. Userspace should distinguish different clocks through the name assigned by the driver. A udev rule such as the following can be used to get a symlink /dev/ptp_virtio to the UTC clock: SUBSYSTEM=3D=3D"ptp", ATTR{clock_name}=3D=3D"Virtio PTP UTC", SYMLINK +=3D= "ptp_virtio" The preferred PTP clock reading method is ioctl PTP_SYS_OFFSET_PRECISE2, through the ptp_clock_info.getcrosststamp() op. For now, PTP_SYS_OFFSET_PRECISE2 will return -EOPNOTSUPP through a weak function. PTP_SYS_OFFSET_PRECISE2 requires cross-timestamping support for specific clocksources, which will be added in the following. If the clocksource specific code is enabled, check that the Virtio RTC device supports the respective HW counter before obtaining an actual cross-timestamp from the Virtio device. The Virtio RTC device response time may be higher than the timekeeper seqcount increment interval. Therefore, obtain the cross-timestamp before calling get_device_system_crosststamp(). As a fallback, support the ioctl PTP_SYS_OFFSET_EXTENDED2 for all platforms. Assume that concurrency issues during PTP clock removal are avoided by the posix_clock framework. Kconfig recursive dependencies prevent virtio_rtc from implicitly enabling PTP_1588_CLOCK, therefore just warn the user if PTP_1588_CLOCK is not available. Since virtio_rtc should in the future also expose clocks as RTC class devices, it should make sense to not have VIRTIO_RTC depend on PTP_1588_CLOCK. Signed-off-by: Peter Hilber --- drivers/virtio/Kconfig | 16 ++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio_rtc_driver.c | 111 +++++++- drivers/virtio/virtio_rtc_internal.h | 62 +++++ drivers/virtio/virtio_rtc_ptp.c | 384 +++++++++++++++++++++++++++ 5 files changed, 571 insertions(+), 3 deletions(-) create mode 100644 drivers/virtio/virtio_rtc_ptp.c diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index e3dbf16fa977..7369ecd7dd01 100644 --- a/drivers/virtio/Kconfig +++ b/drivers/virtio/Kconfig @@ -187,4 +187,20 @@ config VIRTIO_RTC =20 If unsure, say M. =20 +comment "WARNING: The Virtio RTC driver is useless without VIRTIO_RTC_PTP." + depends on VIRTIO_RTC && !VIRTIO_RTC_PTP + +comment "Enable PTP_1588_CLOCK in order to enable VIRTIO_RTC_PTP." + depends on VIRTIO_RTC && PTP_1588_CLOCK=3Dn + +config VIRTIO_RTC_PTP + bool "Virtio RTC PTP clocks" + default y + depends on VIRTIO_RTC && PTP_1588_CLOCK + help + This exposes any Virtio RTC clocks as PTP Hardware Clocks (PHCs) to + userspace. + + If unsure, say Y. + endif # VIRTIO_MENU diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile index f760414ed6ab..4d48cbcae6bb 100644 --- a/drivers/virtio/Makefile +++ b/drivers/virtio/Makefile @@ -14,3 +14,4 @@ obj-$(CONFIG_VIRTIO_MEM) +=3D virtio_mem.o obj-$(CONFIG_VIRTIO_DMA_SHARED_BUFFER) +=3D virtio_dma_buf.o obj-$(CONFIG_VIRTIO_RTC) +=3D virtio_rtc.o virtio_rtc-y :=3D virtio_rtc_driver.o +virtio_rtc-$(CONFIG_VIRTIO_RTC_PTP) +=3D virtio_rtc_ptp.o diff --git a/drivers/virtio/virtio_rtc_driver.c b/drivers/virtio/virtio_rtc= _driver.c index 424500d2c4f7..3c11fa95b9a7 100644 --- a/drivers/virtio/virtio_rtc_driver.c +++ b/drivers/virtio/virtio_rtc_driver.c @@ -36,11 +36,16 @@ struct viortc_vq { * struct viortc_dev - virtio_rtc device data * @vdev: virtio device * @vqs: virtqueues + * @clocks_to_unregister: Clock references, which are only used during dev= ice + * removal. + * For other uses, there would be a race between device + * creation and setting the pointers here. * @num_clocks: # of virtio_rtc clocks */ struct viortc_dev { struct virtio_device *vdev; struct viortc_vq vqs[VIORTC_MAX_NR_QUEUES]; + struct viortc_ptp_clock **clocks_to_unregister; u16 num_clocks; }; =20 @@ -588,6 +593,89 @@ int viortc_cross_cap(struct viortc_dev *viortc, u64 vi= o_clk_id, u16 hw_counter, * init, deinit */ =20 +/** + * viortc_init_clock() - init local representation of virtio_rtc clock (PH= C) + * @viortc: device data + * @vio_clk_id: virtio_rtc clock id + * + * Context: Process context. + * Return: Zero on success, negative error code otherwise. + */ +static int viortc_init_clock(struct viortc_dev *viortc, u64 vio_clk_id) +{ + int ret; + u16 clock_type; + char ptp_clock_name[PTP_CLOCK_NAME_LEN]; + const char *type_name; + /* fit prefix + u16 in decimal */ + char type_name_buf[5 + 5 + 1]; + bool has_xtstamp_feature; + struct viortc_ptp_clock *vio_ptp; + struct virtio_device *vdev =3D viortc->vdev; + + ret =3D viortc_clock_cap(viortc, vio_clk_id, &clock_type); + if (ret) + return ret; + + switch (clock_type) { + case VIRTIO_RTC_CLOCK_UTC: + type_name =3D "UTC"; + break; + case VIRTIO_RTC_CLOCK_TAI: + type_name =3D "TAI"; + break; + case VIRTIO_RTC_CLOCK_MONO: + type_name =3D "monotonic"; + break; + default: + snprintf(type_name_buf, sizeof(type_name_buf), "type %hu", + clock_type); + type_name =3D type_name_buf; + } + + snprintf(ptp_clock_name, PTP_CLOCK_NAME_LEN, "Virtio PTP %s", + type_name); + + has_xtstamp_feature =3D virtio_has_feature(vdev, VIRTIO_RTC_F_READ_CROSS); + + vio_ptp =3D viortc_ptp_register(viortc, &vdev->dev, vio_clk_id, + ptp_clock_name, has_xtstamp_feature); + if (IS_ERR(vio_ptp)) { + dev_err(&vdev->dev, "failed to register PTP clock '%s'\n", + ptp_clock_name); + return PTR_ERR(vio_ptp); + } + + viortc->clocks_to_unregister[vio_clk_id] =3D vio_ptp; + + if (!vio_ptp) + dev_warn(&vdev->dev, "clock %llu is not exposed to userspace\n", + vio_clk_id); + + return 0; +} + +/** + * viortc_clocks_exit() - unregister PHCs + * @viortc: device data + */ +static void viortc_clocks_exit(struct viortc_dev *viortc) +{ + unsigned int i; + struct viortc_ptp_clock *vio_ptp; + + for (i =3D 0; i < viortc->num_clocks; i++) { + vio_ptp =3D viortc->clocks_to_unregister[i]; + + if (!vio_ptp) + continue; + + viortc->clocks_to_unregister[i] =3D NULL; + + WARN_ON(viortc_ptp_unregister(vio_ptp, &viortc->vdev->dev)); + } +} + /** * viortc_clocks_init() - init local representations of virtio_rtc clocks * @viortc: device data @@ -599,6 +687,7 @@ static int viortc_clocks_init(struct viortc_dev *viortc) { int ret; u16 num_clocks; + unsigned int i; =20 ret =3D viortc_cfg(viortc, &num_clocks); if (ret) @@ -611,10 +700,24 @@ static int viortc_clocks_init(struct viortc_dev *vior= tc) =20 viortc->num_clocks =3D num_clocks; =20 - /* In the future, PTP clocks will be initialized here. */ - (void)viortc_clock_cap; + viortc->clocks_to_unregister =3D + devm_kcalloc(&viortc->vdev->dev, num_clocks, + sizeof(*viortc->clocks_to_unregister), GFP_KERNEL); + if (!viortc->clocks_to_unregister) + return -ENOMEM; + + for (i =3D 0; i < num_clocks; i++) { + ret =3D viortc_init_clock(viortc, i); + if (ret) + goto err_free_clocks; + } =20 return 0; + +err_free_clocks: + viortc_clocks_exit(viortc); + + return ret; } =20 /** @@ -703,7 +806,9 @@ static int viortc_probe(struct virtio_device *vdev) */ static void viortc_remove(struct virtio_device *vdev) { - /* In the future, PTP clocks will be deinitialized here. */ + struct viortc_dev *viortc =3D vdev->priv; + + viortc_clocks_exit(viortc); =20 virtio_reset_device(vdev); vdev->config->del_vqs(vdev); diff --git a/drivers/virtio/virtio_rtc_internal.h b/drivers/virtio/virtio_r= tc_internal.h index c2b5387f506f..d8bd008cb8f6 100644 --- a/drivers/virtio/virtio_rtc_internal.h +++ b/drivers/virtio/virtio_rtc_internal.h @@ -9,6 +9,7 @@ #define _VIRTIO_RTC_INTERNAL_H_ =20 #include +#include =20 /* driver core IFs */ =20 @@ -20,4 +21,65 @@ int viortc_read_cross(struct viortc_dev *viortc, u64 vio= _clk_id, u16 hw_counter, int viortc_cross_cap(struct viortc_dev *viortc, u64 vio_clk_id, u16 hw_cou= nter, bool *supported); =20 +/* PTP IFs */ + +struct viortc_ptp_clock; + +#if IS_ENABLED(CONFIG_VIRTIO_RTC_PTP) + +struct viortc_ptp_clock *viortc_ptp_register(struct viortc_dev *viortc, + struct device *parent_dev, + u64 vio_clk_id, + const char *ptp_clock_name, + bool try_enable_xtstamp); +int viortc_ptp_unregister(struct viortc_ptp_clock *vio_ptp, + struct device *parent_dev); + +#else + +static inline struct viortc_ptp_clock * +viortc_ptp_register(struct viortc_dev *viortc, struct device *parent_dev, + u64 vio_clk_id, const char *ptp_clock_name, + bool try_enable_xtstamp) +{ + return NULL; +} + +int viortc_ptp_unregister(struct viortc_ptp_clock *vio_ptp, + struct device *parent_dev) +{ + return -ENODEV; +} + +#endif + +/* HW counter IFs */ + +/** + * Maximum # of HW counters which the driver can support - can be increase= d. + */ +#define VIORTC_CAP_HW_COUNTERS 4 + +/** + * viortc_hw_get_counters() - get HW counters present + * @hw_counters: virtio_rtc HW counters + * @num_hw_counters: number of HW counters + * + * num_hw_counters must not exceed VIORTC_CAP_HW_COUNTERS. + * + * Context: Process context. + * Return: Zero on success, negative error code otherwise. + */ +int viortc_hw_get_counters(const u16 **hw_counters, int *num_hw_counters); + +/** + * viortc_hw_xtstamp_params() - get HW-specific xtstamp params + * @hw_counter: virtio_rtc HW counter type + * @cs: clocksource corresponding to hw_counter + * + * Context: Process context. + * Return: Zero on success, negative error code otherwise. + */ +int viortc_hw_xtstamp_params(u16 *hw_counter, struct clocksource **cs); + #endif /* _VIRTIO_RTC_INTERNAL_H_ */ diff --git a/drivers/virtio/virtio_rtc_ptp.c b/drivers/virtio/virtio_rtc_pt= p.c new file mode 100644 index 000000000000..e52205a1caa9 --- /dev/null +++ b/drivers/virtio/virtio_rtc_ptp.c @@ -0,0 +1,384 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Expose virtio_rtc clocks as PTP clocks. + * + * Copyright (C) 2022-2023 OpenSynergy GmbH + * + * Derived from ptp_kvm_common.c, virtual PTP 1588 clock for use with KVM + * guests. + * + * Copyright (C) 2017 Red Hat Inc. + */ + +#include +#include +#include + +#include + +#include "virtio_rtc_internal.h" + +/** + * struct viortc_ptp_clock - PTP clock abstraction + * @vio_clk_id: virtio_rtc clock id + * @ptp_clock: PTP clock handle + * @viortc: virtio_rtc device data + * @ptp_info: PTP clock description + * @num_hw_counters: actual # of hw_counters + * @hw_counters: HW clocks which are supported for xtstamping + */ +struct viortc_ptp_clock { + u64 vio_clk_id; + struct ptp_clock *ptp_clock; + struct viortc_dev *viortc; + struct ptp_clock_info ptp_info; + u32 num_hw_counters; + u16 hw_counters[VIORTC_CAP_HW_COUNTERS]; +}; + +/** + * struct viortc_ptp_cross_ctx - context for get_device_system_crosststamp= () + * @device_time: device clock reading + * @system_counterval: HW counter value at device_time + * + * Provides the already obtained crosststamp to get_device_system_crosstst= amp(). + */ +struct viortc_ptp_cross_ctx { + ktime_t device_time; + struct system_counterval_t system_counterval; +}; + +/* Weak functions in case get_device_system_crosststamp() is not supported= */ + +int __weak viortc_hw_get_counters(const u16 **hw_counters, int *num_hw_cou= nters) +{ + *hw_counters =3D NULL; + *num_hw_counters =3D 0; + return 0; +} + +int __weak viortc_hw_xtstamp_params(u16 *hw_counter, struct clocksource **= cs) +{ + return -EOPNOTSUPP; +} + +/** + * viortc_ptp_get_time_fn() - callback for get_device_system_crosststamp() + * @device_time: device clock reading + * @system_counterval: HW counter value at device_time + * @ctx: context with already obtained crosststamp + * + * Return: zero (success). + */ +static int viortc_ptp_get_time_fn(ktime_t *device_time, + struct system_counterval_t *system_counterval, + void *ctx) +{ + struct viortc_ptp_cross_ctx *vio_ctx =3D ctx; + + *device_time =3D vio_ctx->device_time; + *system_counterval =3D vio_ctx->system_counterval; + + return 0; +} + +/** + * viortc_ptp_check_hw_counter_supported() - look up if xtstamp supported + * @vio_ptp: virtio_rtc PTP clock + * @hw_counter: virtio_rtc HW counter type + * + * Return: Zero if xtstamp is supported for hw_counter, negative error code + * otherwise. + */ +static int +viortc_ptp_check_hw_counter_supported(struct viortc_ptp_clock *vio_ptp, + u16 hw_counter) +{ + u32 i; + + for (i =3D 0; i < vio_ptp->num_hw_counters; i++) { + if (vio_ptp->hw_counters[i] =3D=3D hw_counter) + return 0; + } + + return -EOPNOTSUPP; +} + +/** + * viortc_ptp_do_xtstamp() - get HW-specific crosststamp from device + * @vio_ptp: virtio_rtc PTP clock + * @ctx: context for get_device_system_crosststamp() + * + * Gets HW-specific crosststamp params and reads crosststamp from device. + * + * Context: Process context. + * Return: Zero on success, negative error code otherwise. + */ +static int viortc_ptp_do_xtstamp(struct viortc_ptp_clock *vio_ptp, + struct viortc_ptp_cross_ctx *ctx) +{ + u16 hw_counter; + u64 ns; + u64 max_ns; + int ret; + + ret =3D viortc_hw_xtstamp_params(&hw_counter, &ctx->system_counterval.cs); + if (ret) + return ret; + + ret =3D viortc_ptp_check_hw_counter_supported(vio_ptp, hw_counter); + if (ret) + return ret; + + ret =3D viortc_read_cross(vio_ptp->viortc, vio_ptp->vio_clk_id, + hw_counter, &ns, + &ctx->system_counterval.cycles); + if (ret) + return ret; + + max_ns =3D (u64)ktime_to_ns(KTIME_MAX); + if (ns > max_ns) + return -EINVAL; + + ctx->device_time =3D ns_to_ktime(ns); + + return 0; +} + +/* + * PTP clock operations + */ + +/** + * viortc_ptp_getcrosststamp() - PTP clock getcrosststamp op + * @vio_ptp: virtio_rtc PTP clock + * @xtstamp: crosststamp + * + * Context: Process context. + * Return: Zero on success, negative error code otherwise. + */ +static int viortc_ptp_getcrosststamp(struct ptp_clock_info *ptp, + struct system_device_crosststamp *xtstamp) +{ + struct viortc_ptp_clock *vio_ptp =3D + container_of(ptp, struct viortc_ptp_clock, ptp_info); + int ret; + struct system_time_snapshot history_begin; + struct viortc_ptp_cross_ctx ctx; + + ktime_get_snapshot(&history_begin); + + /* + * Getting the timestamp can take many milliseconds with a slow Virtio + * device. This is too long for viortc_ptp_get_time_fn() passed to + * get_device_system_crosststamp(), which has to usually return before + * the timekeeper seqcount increases (every tick or so). + * + * So, get the actual cross-timestamp first. + */ + ret =3D viortc_ptp_do_xtstamp(vio_ptp, &ctx); + if (ret) + return ret; + + ret =3D get_device_system_crosststamp(viortc_ptp_get_time_fn, &ctx, + &history_begin, xtstamp); + if (ret) { + pr_debug("%s: get_device_system_crosststamp() returned %d\n", + __func__, ret); + } + + return ret; +} + +/** viortc_ptp_adjfine() - unsupported PTP clock adjfine op */ +static int viortc_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) +{ + return -EOPNOTSUPP; +} + +/** viortc_ptp_adjtime() - unsupported PTP clock adjtime op */ +static int viortc_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) +{ + return -EOPNOTSUPP; +} + +/** viortc_ptp_settime64() - unsupported PTP clock settime64 op */ +static int viortc_ptp_settime64(struct ptp_clock_info *ptp, + const struct timespec64 *ts) +{ + return -EOPNOTSUPP; +} + +/** + * viortc_ptp_gettimex64() - PTP clock gettimex64 op + * + * Context: Process context. + */ +static int viortc_ptp_gettimex64(struct ptp_clock_info *ptp, + struct timespec64 *ts, + struct ptp_system_timestamp *sts) +{ + struct viortc_ptp_clock *vio_ptp =3D + container_of(ptp, struct viortc_ptp_clock, ptp_info); + u64 ns; + int ret; + + ptp_read_system_prets(sts); + ret =3D viortc_read(vio_ptp->viortc, vio_ptp->vio_clk_id, &ns); + ptp_read_system_postts(sts); + + if (ret) + return ret; + + if (ns > (u64)S64_MAX) + return -EINVAL; + + *ts =3D ns_to_timespec64((s64)ns); + + return 0; +} + +/** viortc_ptp_enable() - unsupported PTP clock enable op */ +static int viortc_ptp_enable(struct ptp_clock_info *ptp, + struct ptp_clock_request *rq, int on) +{ + return -EOPNOTSUPP; +} + +/** + * viortc_ptp_info_template - ptp_clock_info template + * + * The .name member will be set for individual virtio_rtc PTP clocks. + */ +static const struct ptp_clock_info viortc_ptp_info_template =3D { + .owner =3D THIS_MODULE, + /* .name is set according to clock type */ + .adjfine =3D viortc_ptp_adjfine, + .adjtime =3D viortc_ptp_adjtime, + .gettimex64 =3D viortc_ptp_gettimex64, + .settime64 =3D viortc_ptp_settime64, + .enable =3D viortc_ptp_enable, + .getcrosststamp =3D viortc_ptp_getcrosststamp, +}; + +/** + * viortc_ptp_unregister() - PTP clock unregistering wrapper + * @vio_ptp: virtio_rtc PTP clock + * @parent_dev: parent device of PTP clock + * + * Return: Zero on success, negative error code otherwise. + */ +int viortc_ptp_unregister(struct viortc_ptp_clock *vio_ptp, + struct device *parent_dev) +{ + int ret =3D ptp_clock_unregister(vio_ptp->ptp_clock); + + if (!ret) + devm_kfree(parent_dev, vio_ptp); + + return ret; +} + +/** + * viortc_ptp_get_cross_cap() - get xtstamp support info from device + * @viortc: virtio_rtc device data + * @vio_ptp: virtio_rtc PTP clock abstraction + * + * Context: Process context. + * Return: Zero on success, negative error code otherwise. + */ +static int viortc_ptp_get_cross_cap(struct viortc_dev *viortc, + struct viortc_ptp_clock *vio_ptp) +{ + int ret; + const u16 *hw_counters_driver; + u32 num_hw_counters_driver; + u32 i; + u32 num_hw_counters =3D 0; + + ret =3D viortc_hw_get_counters(&hw_counters_driver, + &num_hw_counters_driver); + if (ret) + return ret; + + if (num_hw_counters_driver > VIORTC_CAP_HW_COUNTERS) { + pr_err("%s: HW counter capacity exceeded\n", __func__); + return -ENOMEM; + } + + for (i =3D 0; i < num_hw_counters_driver; i++) { + u16 hw_counter =3D hw_counters_driver[i]; + bool xtstamp_supported; + + ret =3D viortc_cross_cap(viortc, vio_ptp->vio_clk_id, hw_counter, + &xtstamp_supported); + if (ret) + return ret; + + if (xtstamp_supported) + vio_ptp->hw_counters[num_hw_counters++] =3D hw_counter; + } + + vio_ptp->num_hw_counters =3D num_hw_counters; + + return 0; +} + +/** + * viortc_ptp_register() - prepare and register PTP clock + * @viortc: virtio_rtc device data + * @parent_dev: parent device for PTP clock + * @vio_clk_id: id of virtio_rtc clock which backs PTP clock + * @ptp_clock_name: PTP clock name + * @try_enable_xtstamp: enable xtstamp op, if available + * + * Context: Process context. + * Return: Pointer on success, ERR_PTR() otherwise; NULL if PTP clock supp= ort + * not available. + */ +struct viortc_ptp_clock *viortc_ptp_register(struct viortc_dev *viortc, + struct device *parent_dev, + u64 vio_clk_id, + const char *ptp_clock_name, + bool try_enable_xtstamp) +{ + struct viortc_ptp_clock *vio_ptp; + struct ptp_clock *ptp_clock; + ssize_t len; + int ret; + + vio_ptp =3D devm_kzalloc(parent_dev, sizeof(*vio_ptp), GFP_KERNEL); + if (!vio_ptp) + return ERR_PTR(-ENOMEM); + + vio_ptp->viortc =3D viortc; + vio_ptp->vio_clk_id =3D vio_clk_id; + vio_ptp->ptp_info =3D viortc_ptp_info_template; + len =3D strscpy(vio_ptp->ptp_info.name, ptp_clock_name, + sizeof(vio_ptp->ptp_info.name)); + if (len < 0) { + ret =3D len; + goto err_free_dev; + } + + if (try_enable_xtstamp) { + ret =3D viortc_ptp_get_cross_cap(viortc, vio_ptp); + if (ret) + goto err_free_dev; + } + + ptp_clock =3D ptp_clock_register(&vio_ptp->ptp_info, parent_dev); + if (IS_ERR(ptp_clock)) + goto err_on_register; + + vio_ptp->ptp_clock =3D ptp_clock; + + return vio_ptp; + +err_on_register: + ret =3D PTR_ERR(ptp_clock); + +err_free_dev: + devm_kfree(parent_dev, vio_ptp); + return ERR_PTR(ret); +} --=20 2.39.2 From nobody Sun Feb 8 02:56:00 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D181EEB64DA for ; Fri, 30 Jun 2023 17:33:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232971AbjF3Rdv (ORCPT ); Fri, 30 Jun 2023 13:33:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231946AbjF3Rdf (ORCPT ); Fri, 30 Jun 2023 13:33:35 -0400 Received: from refb01.tmes.trendmicro.eu (refb01.tmes.trendmicro.eu [18.185.115.53]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FE183C07 for ; Fri, 30 Jun 2023 10:33:18 -0700 (PDT) Received: from 104.47.7.177_.trendmicro.com (unknown [172.21.9.104]) by refb01.tmes.trendmicro.eu (Postfix) with ESMTPS id 5B03510A280F6 for ; Fri, 30 Jun 2023 17:12:35 +0000 (UTC) Received: from 104.47.7.177_.trendmicro.com (unknown [172.21.178.217]) by repost01.tmes.trendmicro.eu (Postfix) with SMTP id C48B1100004FE; Fri, 30 Jun 2023 17:12:33 +0000 (UTC) X-TM-MAIL-RECEIVED-TIME: 1688145153.430000 X-TM-MAIL-UUID: ee4b92a0-79ce-403f-ae6a-ff1396bc5399 Received: from DEU01-BE0-obe.outbound.protection.outlook.com (unknown [104.47.7.177]) by repre01.tmes.trendmicro.eu (Trend Micro Email Security) with ESMTPS id 6913B10020CF2; Fri, 30 Jun 2023 17:12:33 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=X3EMykZidH56HeJnGTjqhqtT3uSrRs720E2RWQI91+GnGVablCd6xS1Fvp+5eMkej0wzu3JyYEUKg1Bt4ZsrXR+rNpnGpP6w0ULl7QO0rHS+1FvP27y+tDJnKQgujos6RoGJctBNe9TW66g+fGOmDgEuyuvKTpNowcilTryQnn5XpL9A1OT102Vu/PpYFGao1QyqVYb1tOLXwjm9FaH9qpUO6jjfSweVA4SgFcBsuJnXEoK5qnzGpKRvN4siQ1/r6eRs8iVo/FzitMzPi4y5Y3uZai+u9U7Oun5L1krOq7NnuVR9DCic6rbX2weuYXuAeZlFh9C5D59rcDChY4AuyA== 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=FmFoyCaOlkqdJbrsaGL8tIEEXRJei17uNfH7jf8XZN8=; b=nRnyfidYKnrhjTuzbIcxjXkR6s9GkF1xLVuyWtqfaeUJe+DwP1QZ4CFJFBHuQXEqZeu29hnzeP5gbZgHRLuPA0pRmaG4r9zeTAG5cHsVn5bRvtW1IaqUmxA1sKmh54PGab9Wk9lBjAiQY2uCiHpnyMlHyIdlW3U2HNn0Xj8B0VWMqsdob348sNzFmnKGmKVmrEf+LD8hVAmutyaRRL2Vab6R+k6UTmAaammp0uTfvdr4bZxdHa1kcta+fC2j0YVuO509HmM1MDxu84ZXgntpwCm7wsrtC6IHXfTKFESI9P2RWuDK02wKelfyKpJOdqTN4ulwjAs+WIOWvbCQg1nHGQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 217.66.60.4) smtp.rcpttodomain=linux.alibaba.com smtp.mailfrom=opensynergy.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=opensynergy.com; dkim=none (message not signed); arc=none X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 217.66.60.4) smtp.mailfrom=opensynergy.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=opensynergy.com; Received-SPF: Pass (protection.outlook.com: domain of opensynergy.com designates 217.66.60.4 as permitted sender) receiver=protection.outlook.com; client-ip=217.66.60.4; helo=SR-MAIL-03.open-synergy.com; pr=C From: Peter Hilber To: virtualization@lists.linux-foundation.org, virtio-dev@lists.oasis-open.org Cc: Peter Hilber , linux-kernel@vger.kernel.org, "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo Subject: [RFC PATCH 7/7] virtio_rtc: Add Arm Generic Timer cross-timestamping Date: Fri, 30 Jun 2023 19:10:50 +0200 Message-Id: <20230630171052.985577-8-peter.hilber@opensynergy.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230630171052.985577-1-peter.hilber@opensynergy.com> References: <20230630171052.985577-1-peter.hilber@opensynergy.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB8EUR05FT036:EE_|FR0P281MB1657:EE_ X-MS-Office365-Filtering-Correlation-Id: c8cd4262-85b8-4365-506f-08db798d30bb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OiQP6oSlTNBhJTbFjYANv7FzY6Rt9o1VCywT2YLqVd2SLVyi+ilb4+LC189uP54+fJM9d+FonlBALxPnlcmirsZiNa8hvKO0bVrWHpuTQSXIdvgTzsHTP1jEZW9nRj1yuyJaoaARKKGcjs7Vh3IiEtaAJu9qOWPZz6viTJw9g5AZwrukWEzYBdvEwYV4zv2aJyRKgqodamKV+eI33Gmq8z5ebwldOjY4n/U+E7qmuoWsEd902H/nEbV0J7mec5ZNq9Kr7LgkfJOak0+o4U7zIiPIE16anCOXFQgnmcDpjmZuxorODYhVSnXGsHuvId7G+/YR0JSZdkjhxphbrV7VrrlVhDAAeCMRuzcz2KJIZlIEc5uuZytazrI81K8HteFpuipSx7f1EPD3TOYJ1FrwUodHLbjbzFnq1Y9gkodgm2ZKn3WE4fLkXxaZuKl09bRykUHMu+KdnnjsTWF31rh6ek0Z0VVyMXdVC5BgzOFCGdippjMkuP/H9rfQLXevSsiC7ZNdzeB5pWVVHpcohN4LjuO0Ey4DwU/xURjpzyPoMOytqQSfbHBpjgGzhwWsbWBI2UvwAE5gFvdPs0fMW3GqCtGar+RQUka9KIvHUYYUwu0eXWrJkRAGDN9/R4W5BZ306FSeO2IBXDcZsk2nICyuRVQEjGyKqNrWkRQ4Iv2aaF90MVNC3g8CBLANXG6jGsOxo1FvOoGFhFMFL4p7Az0og4qJvdTQnov0mcAVHP0cmhGg1C0Q+/5Lk7m3b6ApbiLiskBInbLtth2FEpwXExzca43S1TFyMyZmx8V1fiZW6K0= X-Forefront-Antispam-Report: CIP:217.66.60.4;CTRY:DE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SR-MAIL-03.open-synergy.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(376002)(396003)(39840400004)(136003)(346002)(451199021)(46966006)(36840700001)(86362001)(54906003)(8936002)(8676002)(5660300002)(82310400005)(26005)(44832011)(1076003)(478600001)(4326008)(70206006)(70586007)(966005)(41300700001)(316002)(42186006)(186003)(2616005)(2906002)(47076005)(83380400001)(336012)(36860700001)(40480700001)(36756003)(81166007)(41533002)(36900700001);DIR:OUT;SFP:1102; X-OriginatorOrg: opensynergy.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jun 2023 17:12:31.6895 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c8cd4262-85b8-4365-506f-08db798d30bb X-MS-Exchange-CrossTenant-Id: 800fae25-9b1b-4edc-993d-c939c4e84a64 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=800fae25-9b1b-4edc-993d-c939c4e84a64;Ip=[217.66.60.4];Helo=[SR-MAIL-03.open-synergy.com] X-MS-Exchange-CrossTenant-AuthSource: DB8EUR05FT036.eop-eur05.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: FR0P281MB1657 X-TM-AS-ERS: 104.47.7.177-0.0.0.0 X-TMASE-Version: StarCloud-1.3-9.1.1011-27724.001 X-TMASE-Result: 10--11.667500-4.000000 X-TMASE-MatchedRID: huMMtcahIlDc1YtpVABxhScOunEIf0eXRyG5SSR7mMFHwPpoS7xiTQ3u hqemVNabwqNosrPrbHMTsTmVJvxcHKjHzroBTUY9L/5nX3iQblcH/MsNCRnylchGESF2QtYSsgk fnaHAFPzm9Tc/GgfFJwFtWueZlMH0Hw0ziwm/5yd81fikUYgyLefShpTpYz7797gvW7V0yoZtyV XNSSC+Bni2yHSIHoZstv5tlY0RB3F1dilwOXZXnZOU1MLNH5QIp9ot5GYsw7Gc2JxbD3wPF24q1 hfzDZy5xyWmjWIJIoJwT/DvNCCX4MddJPEKsqAMES46qCSbf/WHvP6CYoqPSgN/ojyMXH62DEom GUxecKpxg7svMtapLpefsKoW209bgHkGDkIaUnULYpBk5/uhSeQZtxwUYGQZRpMDBdUmwGo= X-TMASE-XGENCLOUD: fbc7fa83-7c2b-44b2-acde-9dcb2ca7bd88-0-0-200-0 X-TM-Deliver-Signature: B3A2AABD22BC6B721119BBE8E963F1C3 X-TM-Addin-Auth: FqTsgZGS/LY7uD/3u5PjxWmSaftmsshxVUNRF/ZAPGFXer8fdMKSkX3/9jh 1KfeIN/AiRbIL2xkjrDrr4wIBEMCuOidwS5xZsXBiosoUZgOzjb4SUgfAEVOCBCatS4pB6LPyHp y0Q1DpGIhs52iMPg2V5CVxhmdo6AHu1v7U4jVLmy6mQnJ158CrOdfhx0JpTCyIY/3WK241rIJ29 t2FN7qUYitRkAeX2zjtckBDhlmkHLjaN7vDVFs76aV6qm1yaJliJVkB0VHbxNyQiCkx8rNv8Rho yFqmNrFf+FKzdDg=.0DHIXYL8xaY4xFvpFTmQhLEmLY1fHWqAjCLWjFCQYEkATto+GZQNH+ZiZo l4Kw242CpfzgwoPk6WkjdjelSLs8Ot4gDSTt+cvMpCwFTFSRv6HsZnWLiWbYt6OC0Fge7Pyxl4Z M1Z96C3/vYnrR4j4TXNxK/894g93mJL1NNbDy/KA7U+IdT2gQiHnK4mwvyVnCtpRcELAkm37nO/ yizyHEnfHzS75JuT88wnbX+q5iOyGfoSlrWaQJeBDcm9plA7ipJ8BGfvNTSx3k37pM/5OUVBeem h05TKU84j8Eq42ZP2ICSvsSVN4fiXtmAI+h7qKXLjFrY2bSRGDlHsJNG5EA== X-TM-Addin-ProductCode: EMS DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=opensynergy.com; s=TM-DKIM-20210503141657; t=1688145153; bh=+btlWovU5V7tHS5gyvszgPfHBPOLNvhNFh8IVZ28uwY=; l=3149; h=From:To:Date; b=eoyDQiYgNxkGDkgrT1VFWHYo59u2BLLOEZ3EgFUkzWrCoBUofGCmJG+wZobQP7jVA ZaGIn3q7sJ04faHqQQEWm5OI94uLo3zfLJVDLk4R9qDdv79cq4QexoXXV6n/LZYltL BCfD+aoUTcxzF1e90lxIP5oPgKqNnGQPj83mHbV0Da14n3J3tm9vdYlukh1EkokAnS isw2HN26Bq4zgDh/UrAr2AnKRoTOAmjCIuC8Pv9Da9RE+VmCenoXXcYSl419aNdPBB E6fj0FFSOLFz8IjMedXDvEaVFTrVinvZlTO++qN9S4aec7TPfivlScOfkF+KQE++R1 ftE1DUQ+zqo8g== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add PTP_SYS_OFFSET_PRECISE2 support on platforms using the Arm Generic Timer, by forwarding the clocksource information from arm_arch_timer. Support only the CP15 counter interfaces, since the memory-mapped interfaces are not supported by the Virtio RTC draft spec [1]. [1] https://lists.oasis-open.org/archives/virtio-comment/202306/msg00592.ht= ml Signed-off-by: Peter Hilber --- drivers/virtio/Kconfig | 13 ++++++++++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio_rtc_arm.c | 44 +++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 drivers/virtio/virtio_rtc_arm.c diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index 7369ecd7dd01..ed3f541032a0 100644 --- a/drivers/virtio/Kconfig +++ b/drivers/virtio/Kconfig @@ -203,4 +203,17 @@ config VIRTIO_RTC_PTP =20 If unsure, say Y. =20 +config VIRTIO_RTC_ARM + bool "Virtio RTC cross-timestamping using Arm Generic Timer" + default y + depends on VIRTIO_RTC_PTP && ARM_ARCH_TIMER + help + This enables Virtio RTC cross-timestamping using the Arm Generic Timer. + It only has an effect if the Virtio RTC device also supports this. The + cross-timestamp is available through the PTP clock driver precise + cross-timestamp ioctl (PTP_SYS_OFFSET_PRECISE2 or + PTP_SYS_OFFSET_PRECISE). + + If unsure, say Y. + endif # VIRTIO_MENU diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile index 4d48cbcae6bb..781dff9f8822 100644 --- a/drivers/virtio/Makefile +++ b/drivers/virtio/Makefile @@ -15,3 +15,4 @@ obj-$(CONFIG_VIRTIO_DMA_SHARED_BUFFER) +=3D virtio_dma_bu= f.o obj-$(CONFIG_VIRTIO_RTC) +=3D virtio_rtc.o virtio_rtc-y :=3D virtio_rtc_driver.o virtio_rtc-$(CONFIG_VIRTIO_RTC_PTP) +=3D virtio_rtc_ptp.o +virtio_rtc-$(CONFIG_VIRTIO_RTC_ARM) +=3D virtio_rtc_arm.o diff --git a/drivers/virtio/virtio_rtc_arm.c b/drivers/virtio/virtio_rtc_ar= m.c new file mode 100644 index 000000000000..2367f054081c --- /dev/null +++ b/drivers/virtio/virtio_rtc_arm.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Provides cross-timestamp params for Arm. + * + * Copyright (C) 2022-2023 OpenSynergy GmbH + */ + +#include +#include + +#include + +#include "virtio_rtc_internal.h" + +static const u16 viortc_hw_counters[] =3D { VIRTIO_RTC_COUNTER_ARM_VIRT, + VIRTIO_RTC_COUNTER_ARM_PHYS }; + +/* see header for doc */ +int viortc_hw_get_counters(const u16 **hw_counters, int *num_hw_counters) +{ + *hw_counters =3D viortc_hw_counters; + *num_hw_counters =3D ARRAY_SIZE(viortc_hw_counters); + + return 0; +} + +/* see header for doc */ +int viortc_hw_xtstamp_params(u16 *hw_counter, struct clocksource **cs) +{ + *cs =3D arch_timer_get_cs(); + + switch (arch_timer_counter_get_type()) { + case ARCH_COUNTER_CP15_VIRT: + *hw_counter =3D VIRTIO_RTC_COUNTER_ARM_VIRT; + break; + case ARCH_COUNTER_CP15_PHYS: + *hw_counter =3D VIRTIO_RTC_COUNTER_ARM_PHYS; + break; + default: + return -EINVAL; + } + + return 0; +} --=20 2.39.2