From patchwork Wed Nov 24 11:01:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wen-chien Jesse Sung X-Patchwork-Id: 1559055 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=FbW0u4OA; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HzdNz2sLTz9t2p for ; Wed, 24 Nov 2021 22:01:38 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1mpq1m-0001OR-8m; Wed, 24 Nov 2021 11:01:26 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1mpq1j-0001Mh-9n for kernel-team@lists.ubuntu.com; Wed, 24 Nov 2021 11:01:23 +0000 Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id C46293F19E for ; Wed, 24 Nov 2021 11:01:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1637751682; bh=oJaE76DNUhUixq6gbaUB8J+miRbpgQYwaFHEgO3gJo4=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FbW0u4OAn54HbJocPK7W/54nwAlIdVPDCr/RmDpd1PQAzEsHqfilNNrLCm/Mh7MVz vOgmaGICghrZ4/stAVZvDKEkTwJjFxoLPHD1aQB00ectHqaMFdrRrbDOQNalLnVHv7 B3hoxpxN+R3Un1qVdh278DIk0Bstc/x/NYg44GqgzvK+Z9RDmhAB3ylpfivO1x7WzP 9KVMvFX07bbdzijCTfPLmBT4rLAlFuKH806LFAGx+L5u4dBkmKSg9yBx5v5ZPRZsp/ EvzOeKnf5nCGVWx44oIzoazEcq78I1KTbh5cqSwrn9CBFRMGSOFG3H0FqAjnzoC+t4 L8YFsASDI6Kkg== Received: by mail-pj1-f69.google.com with SMTP id x18-20020a17090a789200b001a7317f995cso3167292pjk.4 for ; Wed, 24 Nov 2021 03:01:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oJaE76DNUhUixq6gbaUB8J+miRbpgQYwaFHEgO3gJo4=; b=NkhPioFZFesGEob0fuZDWl4kcXtJU122XdVnRKV+lxyOdmn3R21ZMGd+iC3cC4Wac2 0Fr1+Z5hERCHveg2IzykygYN1BJ9PcA3XCLnKYqDag2f6Cs21M/LB7zk9WG25HAsah5x P/REzn0VFDFbWTNxUYlcn924B87fxH4p67681bb4coM5JLExMfYHrMWgZ53WKJ5wQho/ KgLNlTphTAY7fiFZFk58S5SwzqNzYaTbLuPMmPOmrfyG2dbJGDZEmK4j6mb8/Yr2xffZ KPl30+NiuWmaAhSprcNcMqHJmarFM6lJP/h6U716uzRa/mL32jmGiNpfPZ/YvP8cgFdx jPjw== X-Gm-Message-State: AOAM530KdVY7JW+WaRNXkXQvkjW7RPb48yOJVxdtG8uORnVzLfI42swl Ptsm00GPUV/pEa0zoMlZRlnFFctY71ezGiqVafsNEW/chQQlgszLS2C4FcOwoWp+gGfqYTp8aEx A96tjcUUwfb9SaPHuQHQTIQ1AemAZhumd5NMH/PVacg== X-Received: by 2002:a17:902:b60b:b0:144:e601:de5 with SMTP id b11-20020a170902b60b00b00144e6010de5mr16608018pls.36.1637751680311; Wed, 24 Nov 2021 03:01:20 -0800 (PST) X-Google-Smtp-Source: ABdhPJxHgjrah4nDkZzD2o31llR8fzlA5gRBtVbjTRVPWwO3FUZCItgdDHyaRNw+eCw2+j9qlJSSNw== X-Received: by 2002:a17:902:b60b:b0:144:e601:de5 with SMTP id b11-20020a170902b60b00b00144e6010de5mr16607942pls.36.1637751679846; Wed, 24 Nov 2021 03:01:19 -0800 (PST) Received: from localhost.localdomain (2001-b011-6c05-1b55-0000-0000-0000-1dba.dynamic-ip6.hinet.net. [2001:b011:6c05:1b55::1dba]) by smtp.gmail.com with ESMTPSA id p21sm17210509pfh.43.2021.11.24.03.01.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 03:01:19 -0800 (PST) From: Wen-chien Jesse Sung To: kernel-team@lists.ubuntu.com Subject: [F][PATCH 1/1] rtc: class: support hctosys from modular RTC drivers Date: Wed, 24 Nov 2021 19:01:17 +0800 Message-Id: <20211124110117.676002-2-jesse.sung@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211124110117.676002-1-jesse.sung@canonical.com> References: <20211124110117.676002-1-jesse.sung@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Steve Muckle BugLink: https://launchpad.net/bugs/1952077 Due to distribution constraints it may not be possible to statically compile the required RTC driver into the kernel. Expand RTC_HCTOSYS support to cover all RTC devices (statically compiled or not) by checking at the end of RTC device registration whether the time should be synced. Signed-off-by: Steve Muckle Link: https://lore.kernel.org/r/20191106194625.116692-1-smuckle@google.com Signed-off-by: Alexandre Belloni (cherry picked from commit f9b2a4d6a5f18e0aaf715206a056565c56889d9f) Signed-off-by: Wen-chien Jesse Sung --- drivers/rtc/Kconfig | 3 -- drivers/rtc/Makefile | 1 - drivers/rtc/class.c | 61 ++++++++++++++++++++++++++++++++++++++ drivers/rtc/hctosys.c | 69 ------------------------------------------- 4 files changed, 61 insertions(+), 73 deletions(-) delete mode 100644 drivers/rtc/hctosys.c diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index b4d39e3b2a4f1..b4e5ea9a082f5 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -41,9 +41,6 @@ config RTC_HCTOSYS_DEVICE device should record time in UTC, since the kernel won't do timezone correction. - The driver for this RTC device must be loaded before late_initcall - functions run, so it must usually be statically linked. - This clock should be battery-backed, so that it reads the correct time when the system boots from a power-off state. Otherwise, your system will need an external clock source (like an NTP server). diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index a20943eafaab2..9918b04293838 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -6,7 +6,6 @@ ccflags-$(CONFIG_RTC_DEBUG) := -DDEBUG obj-$(CONFIG_RTC_LIB) += lib.o -obj-$(CONFIG_RTC_HCTOSYS) += hctosys.o obj-$(CONFIG_RTC_SYSTOHC) += systohc.o obj-$(CONFIG_RTC_CLASS) += rtc-core.o obj-$(CONFIG_RTC_MC146818_LIB) += rtc-mc146818-lib.o diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c index 9458e6d6686ac..8793b2b8cf9d9 100644 --- a/drivers/rtc/class.c +++ b/drivers/rtc/class.c @@ -34,6 +34,62 @@ static void rtc_device_release(struct device *dev) #ifdef CONFIG_RTC_HCTOSYS_DEVICE /* Result of the last RTC to system clock attempt. */ int rtc_hctosys_ret = -ENODEV; + +/* IMPORTANT: the RTC only stores whole seconds. It is arbitrary + * whether it stores the most close value or the value with partial + * seconds truncated. However, it is important that we use it to store + * the truncated value. This is because otherwise it is necessary, + * in an rtc sync function, to read both xtime.tv_sec and + * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read + * of >32bits is not possible. So storing the most close value would + * slow down the sync API. So here we have the truncated value and + * the best guess is to add 0.5s. + */ + +static int rtc_hctosys(void) +{ + int err = -ENODEV; + struct rtc_time tm; + struct timespec64 tv64 = { + .tv_nsec = NSEC_PER_SEC >> 1, + }; + struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE); + + if (!rtc) { + pr_info("unable to open rtc device (%s)\n", + CONFIG_RTC_HCTOSYS_DEVICE); + goto err_open; + } + + err = rtc_read_time(rtc, &tm); + if (err) { + dev_err(rtc->dev.parent, + "hctosys: unable to read the hardware clock\n"); + goto err_read; + } + + tv64.tv_sec = rtc_tm_to_time64(&tm); + +#if BITS_PER_LONG == 32 + if (tv64.tv_sec > INT_MAX) { + err = -ERANGE; + goto err_read; + } +#endif + + err = do_settimeofday64(&tv64); + + dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n", + &tm, (long long)tv64.tv_sec); + +err_read: + rtc_class_close(rtc); + +err_open: + rtc_hctosys_ret = err; + + return err; +} #endif #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE) @@ -375,6 +431,11 @@ int __rtc_register_device(struct module *owner, struct rtc_device *rtc) dev_info(rtc->dev.parent, "registered as %s\n", dev_name(&rtc->dev)); +#ifdef CONFIG_RTC_HCTOSYS_DEVICE + if (!strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE)) + rtc_hctosys(); +#endif + return 0; } EXPORT_SYMBOL_GPL(__rtc_register_device); diff --git a/drivers/rtc/hctosys.c b/drivers/rtc/hctosys.c deleted file mode 100644 index a74d0d890600a..0000000000000 --- a/drivers/rtc/hctosys.c +++ /dev/null @@ -1,69 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* - * RTC subsystem, initialize system time on startup - * - * Copyright (C) 2005 Tower Technologies - * Author: Alessandro Zummo - */ - -#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt - -#include - -/* IMPORTANT: the RTC only stores whole seconds. It is arbitrary - * whether it stores the most close value or the value with partial - * seconds truncated. However, it is important that we use it to store - * the truncated value. This is because otherwise it is necessary, - * in an rtc sync function, to read both xtime.tv_sec and - * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read - * of >32bits is not possible. So storing the most close value would - * slow down the sync API. So here we have the truncated value and - * the best guess is to add 0.5s. - */ - -static int __init rtc_hctosys(void) -{ - int err = -ENODEV; - struct rtc_time tm; - struct timespec64 tv64 = { - .tv_nsec = NSEC_PER_SEC >> 1, - }; - struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE); - - if (!rtc) { - pr_info("unable to open rtc device (%s)\n", - CONFIG_RTC_HCTOSYS_DEVICE); - goto err_open; - } - - err = rtc_read_time(rtc, &tm); - if (err) { - dev_err(rtc->dev.parent, - "hctosys: unable to read the hardware clock\n"); - goto err_read; - } - - tv64.tv_sec = rtc_tm_to_time64(&tm); - -#if BITS_PER_LONG == 32 - if (tv64.tv_sec > INT_MAX) { - err = -ERANGE; - goto err_read; - } -#endif - - err = do_settimeofday64(&tv64); - - dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n", - &tm, (long long)tv64.tv_sec); - -err_read: - rtc_class_close(rtc); - -err_open: - rtc_hctosys_ret = err; - - return err; -} - -late_initcall(rtc_hctosys);