Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp702845pxm; Fri, 25 Feb 2022 17:44:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJzjl1c79isjx1yIbNq+CUaX8072UR8HsV1nT957fTKJIT6RH9mUCil/YYL7TPlpvP4sF7Z2 X-Received: by 2002:a63:455a:0:b0:373:e921:90c9 with SMTP id u26-20020a63455a000000b00373e92190c9mr8257320pgk.310.1645839850220; Fri, 25 Feb 2022 17:44:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645839850; cv=none; d=google.com; s=arc-20160816; b=rv0wiTMHVhFQnebHvRHTIaQD3psARMArZxmgDMv6Q9vqCesAh7sZesLWR9HlZwqcwM YE3h70IQshWBH0VMTu9CuJ5gr8uZRlwMRMNts9+g54oxJFd8DjOqC0WzYgcOnWHvnpi/ og9v/eYmyGlKQmgBkFNNBqJ3CA7KDXPyLxHleyNUBY0aw036wPCxNjHSbyZ4kBvz+vxZ kbNWlZWU8mfUCR2/ZB3L/LO7dtfJFTJ1DWnhPKtQFwZLqGFmQjzQ48q8ZOyzd1etZQLX I2kQcVjZ3YMw6phEzxGtpXKWQTbU6RJuNvyku/TF6XlpcKqU4tSGSS+61gqwPt7/NTnT ofdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=kIRg5QJjXdMvg4mGdci435YIQdJAxFFxB1p8dHt9xeI=; b=vVFttdXKb/VZb5pitaaxcN8Sas31pMZeau7RN/gAuAv4Yctg2n2N5/Q3R5uGpF6/q0 QGCv1SUX/MtTM2M47bCI7EJ5YMOXhIixguXVSHCb+aUnExpSeWb9tBlbg1yMsUfvuB9n dCIzJtAfWHfUxJ65W2aRM1yxEL9zvhFiPsec9puEI6mcufOJdFjRwI7d5ys9CAJza2ZG lpJReVaJzrW6ATajKUO6W0AR0JBdGkfQf3kR/LdzHSFFeA2Kphebj6jh8csypsEUu203 6askFxYENOdMddRfmwM/i2YIMxHlwSPw9pzoaRDqj0Y2cW9NXUyf7InuaY+tqr7TPmbY ciew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@o2.pl header.s=1024a header.b=RJQeOC8w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=o2.pl Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id w8-20020a1709027b8800b0014fc259bd77si3029700pll.203.2022.02.25.17.44.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Feb 2022 17:44:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@o2.pl header.s=1024a header.b=RJQeOC8w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=o2.pl Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 958491C2DA6; Fri, 25 Feb 2022 17:33:31 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233486AbiBYVvZ (ORCPT + 99 others); Fri, 25 Feb 2022 16:51:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233102AbiBYVvX (ORCPT ); Fri, 25 Feb 2022 16:51:23 -0500 Received: from mx-out.tlen.pl (mx-out.tlen.pl [193.222.135.145]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C67171FED81 for ; Fri, 25 Feb 2022 13:50:48 -0800 (PST) Received: (wp-smtpd smtp.tlen.pl 20423 invoked from network); 25 Feb 2022 22:50:45 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=o2.pl; s=1024a; t=1645825846; bh=kIRg5QJjXdMvg4mGdci435YIQdJAxFFxB1p8dHt9xeI=; h=From:To:Cc:Subject; b=RJQeOC8wlOJ8+AMPKXMckG9E8FShXSp3MvGwFUq/VRYDA7z4n2Ua8mMvdeBAtdjdb 6GvspIa8AlakjpkGEwt/XixMJzAI8hM8tFHXfZ0neuE9f/1Fse10o2F5gydYudd+r1 DyPw5QIY8/G7qS+9zBjPfcgcknvd069CJE5vq1+M= Received: from aaew227.neoplus.adsl.tpnet.pl (HELO localhost.localdomain) (mat.jonczyk@o2.pl@[83.4.126.227]) (envelope-sender ) by smtp.tlen.pl (WP-SMTPD) with SMTP for ; 25 Feb 2022 22:50:45 +0100 From: =?UTF-8?q?Mateusz=20Jo=C5=84czyk?= To: linux-kernel@vger.kernel.org, linux-rtc@vger.kernel.org Cc: =?UTF-8?q?Mateusz=20Jo=C5=84czyk?= , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Alessandro Zummo , Alexandre Belloni Subject: [PATCH 2/3] x86/rtc: rewrite mach_get_cmos_time Date: Fri, 25 Feb 2022 22:50:10 +0100 Message-Id: <20220225215011.861477-3-mat.jonczyk@o2.pl> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220225215011.861477-1-mat.jonczyk@o2.pl> References: <20220225215011.861477-1-mat.jonczyk@o2.pl> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-WP-MailID: 5bece34e986b7794637f50910b552e53 X-WP-AV: skaner antywirusowy Poczty o2 X-WP-SPAM: NO 0000000 [IYPE] X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are functions in drivers/rtc/rtc-mc146818-lib.c that handle reading from / writing to the CMOS RTC clock. mach_get_cmos_time() in arch/x86/kernel/rtc.c did not use them and was mostly a duplicate of mc146818_get_time(). Modify mach_get_cmos_time() to use mc146818_get_time() and remove the duplicated code. mach_get_cmos_time() used a different algorithm than mc146818_get_time(), but these functions were equivalent. The major differences were: - mc146818_get_time() is better refined: it was updated in commit 05a0302c3548 ("rtc: mc146818: Prevent reading garbage") to take account of various edge conditions, - when the UIP ("Update in progress") bit of the RTC is set, mach_get_cmos_time() was busy waiting with cpu_relax() while mc146818_get_time() is now using udelay(100) in every loop iteration, - mach_get_cmos_time() assumed that the RTC year must be >= 2000, which may not be true on some old boxes with a dead battery, - mach_get_cmos_time() was holding the rtc_lock for a long time. The RTC writing counterpart, mach_set_rtc_mmss() is already using mc146818_get_time() from drivers/rtc. This was done in commit 3195ef59cb42 ("x86: Do full rtc synchronization with ntp") It appears that mach_get_cmos_time() was simply forgotten. Signed-off-by: Mateusz Jończyk Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Cc: "H. Peter Anvin" Cc: Alessandro Zummo Cc: Alexandre Belloni --- arch/x86/kernel/rtc.c | 59 +++++-------------------------------------- 1 file changed, 7 insertions(+), 52 deletions(-) diff --git a/arch/x86/kernel/rtc.c b/arch/x86/kernel/rtc.c index 586f718b8e95..9f251b65219f 100644 --- a/arch/x86/kernel/rtc.c +++ b/arch/x86/kernel/rtc.c @@ -4,11 +4,8 @@ */ #include #include -#include -#include #include #include -#include #include #include @@ -20,15 +17,12 @@ /* * This is a special lock that is owned by the CPU and holds the index * register we are working with. It is required for NMI access to the - * CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details. + * CMOS/RTC registers. See arch/x86/include/asm/mc146818rtc.h for details. */ volatile unsigned long cmos_lock; EXPORT_SYMBOL(cmos_lock); #endif /* CONFIG_X86_32 */ -/* For two digit years assume time is always after that */ -#define CMOS_YEARS_OFFS 2000 - DEFINE_SPINLOCK(rtc_lock); EXPORT_SYMBOL(rtc_lock); @@ -62,8 +56,7 @@ int mach_set_rtc_mmss(const struct timespec64 *now) void mach_get_cmos_time(struct timespec64 *now) { - unsigned int status, year, mon, day, hour, min, sec, century = 0; - unsigned long flags; + struct rtc_time tm; /* * If pm_trace abused the RTC as storage, set the timespec to 0, @@ -74,51 +67,13 @@ void mach_get_cmos_time(struct timespec64 *now) return; } - spin_lock_irqsave(&rtc_lock, flags); - - /* - * If UIP is clear, then we have >= 244 microseconds before - * RTC registers will be updated. Spec sheet says that this - * is the reliable way to read RTC - registers. If UIP is set - * then the register access might be invalid. - */ - while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)) - cpu_relax(); - - sec = CMOS_READ(RTC_SECONDS); - min = CMOS_READ(RTC_MINUTES); - hour = CMOS_READ(RTC_HOURS); - day = CMOS_READ(RTC_DAY_OF_MONTH); - mon = CMOS_READ(RTC_MONTH); - year = CMOS_READ(RTC_YEAR); - -#ifdef CONFIG_ACPI - if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID && - acpi_gbl_FADT.century) - century = CMOS_READ(acpi_gbl_FADT.century); -#endif - - status = CMOS_READ(RTC_CONTROL); - WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY)); - - spin_unlock_irqrestore(&rtc_lock, flags); - - if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) { - sec = bcd2bin(sec); - min = bcd2bin(min); - hour = bcd2bin(hour); - day = bcd2bin(day); - mon = bcd2bin(mon); - year = bcd2bin(year); + if (mc146818_get_time(&tm)) { + pr_err_ratelimited("Unable to read current time from RTC\n"); + now->tv_sec = now->tv_nsec = 0; + return; } - if (century) { - century = bcd2bin(century); - year += century * 100; - } else - year += CMOS_YEARS_OFFS; - - now->tv_sec = mktime64(year, mon, day, hour, min, sec); + now->tv_sec = rtc_tm_to_time64(&tm); now->tv_nsec = 0; } -- 2.25.1