Skip to content

Commit fc04b2c

Browse files
matjonIngo Molnar
authored andcommitted
x86/rtc: Rewrite & simplify mach_get_cmos_time() by deleting duplicated functionality
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 functionality. mach_get_cmos_time() used a different algorithm than mc146818_get_time(), but these functions are equivalent. The major differences are: - mc146818_get_time() is better refined and handles 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 using mdelay(1) in every loop iteration. (However, there is my commit merged for Linux 5.20 / 6.0 to decrease this period to 100us: commit d2a632a ("rtc: mc146818-lib: reduce RTC_UIP polling period") ), - mach_get_cmos_time() assumed that the RTC year is >= 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 and could hang if the RTC is broken or not present. The RTC writing counterpart, mach_set_rtc_mmss() is already using mc146818_get_time() from drivers/rtc. This was done in commit 3195ef5 ("x86: Do full rtc synchronization with ntp") It appears that mach_get_cmos_time() was simply forgotten. mach_get_cmos_time() is really used only in read_persistent_clock64(), which is called only in a few places in kernel/time/timekeeping.c . [ mingo: These changes are not supposed to change behavior, but they are not identity transformations either, as mc146818_get_time() is a better but different implementation of the same logic - so regressions are possible in principle. ] Signed-off-by: Mateusz Jończyk <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Acked-by: Alexandre Belloni <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent ffcf9c5 commit fc04b2c

File tree

1 file changed

+7
-52
lines changed

1 file changed

+7
-52
lines changed

arch/x86/kernel/rtc.c

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
*/
55
#include <linux/platform_device.h>
66
#include <linux/mc146818rtc.h>
7-
#include <linux/acpi.h>
8-
#include <linux/bcd.h>
97
#include <linux/export.h>
108
#include <linux/pnp.h>
11-
#include <linux/of.h>
129

1310
#include <asm/vsyscall.h>
1411
#include <asm/x86_init.h>
@@ -20,15 +17,12 @@
2017
/*
2118
* This is a special lock that is owned by the CPU and holds the index
2219
* register we are working with. It is required for NMI access to the
23-
* CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details.
20+
* CMOS/RTC registers. See arch/x86/include/asm/mc146818rtc.h for details.
2421
*/
2522
volatile unsigned long cmos_lock;
2623
EXPORT_SYMBOL(cmos_lock);
2724
#endif /* CONFIG_X86_32 */
2825

29-
/* For two digit years assume time is always after that */
30-
#define CMOS_YEARS_OFFS 2000
31-
3226
DEFINE_SPINLOCK(rtc_lock);
3327
EXPORT_SYMBOL(rtc_lock);
3428

@@ -62,8 +56,7 @@ int mach_set_rtc_mmss(const struct timespec64 *now)
6256

6357
void mach_get_cmos_time(struct timespec64 *now)
6458
{
65-
unsigned int status, year, mon, day, hour, min, sec, century = 0;
66-
unsigned long flags;
59+
struct rtc_time tm;
6760

6861
/*
6962
* 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)
7467
return;
7568
}
7669

77-
spin_lock_irqsave(&rtc_lock, flags);
78-
79-
/*
80-
* If UIP is clear, then we have >= 244 microseconds before
81-
* RTC registers will be updated. Spec sheet says that this
82-
* is the reliable way to read RTC - registers. If UIP is set
83-
* then the register access might be invalid.
84-
*/
85-
while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
86-
cpu_relax();
87-
88-
sec = CMOS_READ(RTC_SECONDS);
89-
min = CMOS_READ(RTC_MINUTES);
90-
hour = CMOS_READ(RTC_HOURS);
91-
day = CMOS_READ(RTC_DAY_OF_MONTH);
92-
mon = CMOS_READ(RTC_MONTH);
93-
year = CMOS_READ(RTC_YEAR);
94-
95-
#ifdef CONFIG_ACPI
96-
if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
97-
acpi_gbl_FADT.century)
98-
century = CMOS_READ(acpi_gbl_FADT.century);
99-
#endif
100-
101-
status = CMOS_READ(RTC_CONTROL);
102-
WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY));
103-
104-
spin_unlock_irqrestore(&rtc_lock, flags);
105-
106-
if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) {
107-
sec = bcd2bin(sec);
108-
min = bcd2bin(min);
109-
hour = bcd2bin(hour);
110-
day = bcd2bin(day);
111-
mon = bcd2bin(mon);
112-
year = bcd2bin(year);
70+
if (mc146818_get_time(&tm)) {
71+
pr_err("Unable to read current time from RTC\n");
72+
now->tv_sec = now->tv_nsec = 0;
73+
return;
11374
}
11475

115-
if (century) {
116-
century = bcd2bin(century);
117-
year += century * 100;
118-
} else
119-
year += CMOS_YEARS_OFFS;
120-
121-
now->tv_sec = mktime64(year, mon, day, hour, min, sec);
76+
now->tv_sec = rtc_tm_to_time64(&tm);
12277
now->tv_nsec = 0;
12378
}
12479

0 commit comments

Comments
 (0)