Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2624086pxb; Sun, 17 Oct 2021 20:49:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxLhXHHY+7nNuyso+GD+sxvxGsjBTt2K3xR1VObtvWGS3NQWsCyRLvq6sXG+ywlDVDaLFZl X-Received: by 2002:a63:be01:: with SMTP id l1mr2311281pgf.445.1634528851142; Sun, 17 Oct 2021 20:47:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634528851; cv=none; d=google.com; s=arc-20160816; b=NtG4yuQtYxr6QRfl0PTgk4lwezFeMjiFkzi38zFqdY+0uCVXV4ZS38HTUjvNwoUnDh 3rVptgbsCQFJ+FWuo930CJu3x/81k0+dqxMR/Zd4IKJ5RA42yV3b+SDga5k/DqnfGaqN KQ+Su4tzy0XJJREwowLSOAFeGFkvE/tG5B23ydgKikWeRMh7npZCNso3SzXAuMS0FJft 6Yk6vS5qAcICGVw3RhzklPJ8vtrW+PFAX/mrAR3r74brtavlakZjy1tmodP3x5VMoGrb xFGP7GRE8Y4PZrFrXakRPSMw5YYOBX1D88GJaexSwBgHy/gUK18MUSnfXlJAkkiJNHeo sEnA== 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=XRf4WaQo+RlXU1/FyjY3NoF5PMpZedJLswBjgSBPu7s=; b=UWHzpZf2gb9whbGgO9elM0h1xHsvD/iT95ILMhVBCGJ7Z+p1IExkUSrePWz0FN2S5H rNR2oJhUNypHIe7q8iJBg2R97tOPe9+SRTWGrVifpbpN20bu7jxZfkI/5oChAmUbOKi9 dMQXSLqukE2YZByRyMV5ipolkjXIvhy8CpPIZRhawINnNYQVgfxptGTLwIZRyrPDcJnG lQiveVsfFoFM/j0UCwb8T20nYEptNwzPMvfsRyFApxzLLWjaeUcUh0oAwNE54PPOAmLc YSnR7q+csmA8VRS+HX2aKvplv2iSXCAOD1qrRMUYcCyadd7CbE6CyvMLw1NPHKxqxUoT nJhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@o2.pl header.s=1024a header.b=NrxPwUQb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u12si26949905plg.438.2021.10.17.20.47.19; Sun, 17 Oct 2021 20:47:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@o2.pl header.s=1024a header.b=NrxPwUQb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.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: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344556AbhJQTmK (ORCPT + 98 others); Sun, 17 Oct 2021 15:42:10 -0400 Received: from mx-out.tlen.pl ([193.222.135.142]:47133 "EHLO mx-out.tlen.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344565AbhJQTmG (ORCPT ); Sun, 17 Oct 2021 15:42:06 -0400 Received: (wp-smtpd smtp.tlen.pl 4997 invoked from network); 17 Oct 2021 21:39:49 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=o2.pl; s=1024a; t=1634499589; bh=XRf4WaQo+RlXU1/FyjY3NoF5PMpZedJLswBjgSBPu7s=; h=From:To:Cc:Subject; b=NrxPwUQb9aOAAK7BHgjFZRVsphCebi36ue4J65oagyxHPsEkePbRDj+1cxsbheCbe 5iJnWs7qalMa/8dHAmAi9QmiyXMqtJmZfvP7F3NO1a2AxfJL0h+zEttgkZL7/TLOGA fL7XF00GgqsUjhIpSqqaVGok5hPvV+7neqZuSEJY= Received: from aaet142.neoplus.adsl.tpnet.pl (HELO localhost.localdomain) (mat.jonczyk@o2.pl@[83.4.123.142]) (envelope-sender ) by smtp.tlen.pl (WP-SMTPD) with SMTP for ; 17 Oct 2021 21:39:49 +0200 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 , Alessandro Zummo , Alexandre Belloni Subject: [PATCH RESEND v2 3/6] rtc-mc146818-lib: fix RTC presence check Date: Sun, 17 Oct 2021 21:39:24 +0200 Message-Id: <20211017193927.277409-4-mat.jonczyk@o2.pl> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211017193927.277409-1-mat.jonczyk@o2.pl> References: <20211017193927.277409-1-mat.jonczyk@o2.pl> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-WP-MailID: 77a28373b63ec872af226726696d0fc9 X-WP-AV: skaner antywirusowy Poczty o2 X-WP-SPAM: NO 0000000 [cTO0] Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org To prevent an infinite loop in mc146818_get_time(), commit 211e5db19d15 ("rtc: mc146818: Detect and handle broken RTCs") added a check for RTC availability. Together with a later fix, it checked if bit 6 in register 0x0d is cleared. This, however, caused a false negative on a motherboard with an AMD SB710 southbridge; according to the specification [1], bit 6 of register 0x0d of this chipset is a scratchbit. This caused a regression in Linux 5.11 - the RTC was determined broken by the kernel and not used by rtc-cmos.c [3]. As a better alternative, check whether the UIP ("Update-in-progress") bit is set for longer then 10ms. If that is the case, then apparently the RTC is either absent (and all register reads return 0xff) or broken. Also limit the number of loop iterations in mc146818_get_time() to 10. In a previous approach to this problem, I implemented a check whether the RTC_HOURS register contains a value <= 24. This, however, sometimes did not work correctly on my Intel Kaby Lake laptop. According to Intel's documentation [2], "the time and date RAM locations (0-9) are disconnected from the external bus" during the update cycle so reading this register without checking the UIP bit is incorrect. [1] AMD SB700/710/750 Register Reference Guide, page 308, https://developer.amd.com/wordpress/media/2012/10/43009_sb7xx_rrg_pub_1.00.pdf [2] 7th Generation Intel ® Processor Family I/O for U/Y Platforms [...] Datasheet Volume 1 of 2, page 209 Intel's Document Number: 334658-006, https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/7th-and-8th-gen-core-family-mobile-u-y-processor-lines-i-o-datasheet-vol-1.pdf [3] Functions in arch/x86/kernel/rtc.c apparently were using it. Fixes: 211e5db19d15 ("rtc: mc146818: Detect and handle broken RTCs") Fixes: ebb22a059436 ("rtc: mc146818: Dont test for bit 0-5 in Register D") Signed-off-by: Mateusz Jończyk Cc: Thomas Gleixner Cc: Alessandro Zummo Cc: Alexandre Belloni --- drivers/rtc/rtc-cmos.c | 10 ++++------ drivers/rtc/rtc-mc146818-lib.c | 34 ++++++++++++++++++++++++++++++---- include/linux/mc146818rtc.h | 1 + 3 files changed, 35 insertions(+), 10 deletions(-) v2: Tweak commit description, remove "Cc: stable" (I'll send it manually after more regression testing). diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c index 643433d984ab..f353a41dfe8c 100644 --- a/drivers/rtc/rtc-cmos.c +++ b/drivers/rtc/rtc-cmos.c @@ -833,16 +833,14 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq) rename_region(ports, dev_name(&cmos_rtc.rtc->dev)); - spin_lock_irq(&rtc_lock); - - /* Ensure that the RTC is accessible. Bit 6 must be 0! */ - if ((CMOS_READ(RTC_VALID) & 0x40) != 0) { - spin_unlock_irq(&rtc_lock); - dev_warn(dev, "not accessible\n"); + if (!mc146818_does_rtc_work()) { + dev_warn(dev, "broken or not accessible\n"); retval = -ENXIO; goto cleanup1; } + spin_lock_irq(&rtc_lock); + if (!(flags & CMOS_RTC_FLAGS_NOFREQ)) { /* force periodic irq to CMOS reset default of 1024Hz; * diff --git a/drivers/rtc/rtc-mc146818-lib.c b/drivers/rtc/rtc-mc146818-lib.c index dcfaf09946ee..9175e11baf26 100644 --- a/drivers/rtc/rtc-mc146818-lib.c +++ b/drivers/rtc/rtc-mc146818-lib.c @@ -8,10 +8,35 @@ #include #endif +/* + * If the UIP (Update-in-progress) bit of the RTC is set for more then + * 10ms, the RTC apparently is broken or not present. + */ +unsigned int mc146818_does_rtc_work(void) +{ + int i; + unsigned char val; + unsigned long flags; + + for (i = 0; i < 20; i++) { + spin_lock_irqsave(&rtc_lock, flags); + val = CMOS_READ(RTC_FREQ_SELECT); + spin_unlock_irqrestore(&rtc_lock, flags); + + if ((val & RTC_UIP) == 0) + return 1; + + udelay(500); + } + + return 0; +} + unsigned int mc146818_get_time(struct rtc_time *time) { unsigned char ctrl; unsigned long flags; + unsigned int iter_count = 0; unsigned char century = 0; bool retry; @@ -20,13 +45,14 @@ unsigned int mc146818_get_time(struct rtc_time *time) #endif again: - spin_lock_irqsave(&rtc_lock, flags); - /* Ensure that the RTC is accessible. Bit 6 must be 0! */ - if (WARN_ON_ONCE((CMOS_READ(RTC_VALID) & 0x40) != 0)) { - spin_unlock_irqrestore(&rtc_lock, flags); + if (iter_count > 10) { + pr_err_ratelimited("Unable to read current time from RTC\n"); memset(time, 0xff, sizeof(*time)); return 0; } + iter_count++; + + spin_lock_irqsave(&rtc_lock, flags); /* * Check whether there is an update in progress during which the diff --git a/include/linux/mc146818rtc.h b/include/linux/mc146818rtc.h index 0661af17a758..046a03df1e56 100644 --- a/include/linux/mc146818rtc.h +++ b/include/linux/mc146818rtc.h @@ -123,6 +123,7 @@ struct cmos_rtc_board_info { #define RTC_IO_EXTENT_USED RTC_IO_EXTENT #endif /* ARCH_RTC_LOCATION */ +unsigned int mc146818_does_rtc_work(void); unsigned int mc146818_get_time(struct rtc_time *time); int mc146818_set_time(struct rtc_time *time); -- 2.25.1