Skip to contentSkip to conversation

x86/ACPI: allow CMOS RTC use even when ACPI says there is none

PATCH In review Jan Beulich 2 participants 6 messages Jul 25, 2014 – Jul 28, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/ACPI: allow CMOS RTC use even when ACPI says there is none

HP is setting the ACPI_FADT_NO_CMOS_RTC flag on newer systems, regardless of whether they're being booted from UEFI. Add a command line option to allow probing for a working RTC in that case.

docs/misc/xen-command-line.markdown +8 -0
@@ -218,6 +218,14 @@ If set, override Xen's calculation of th
 
 If set, override Xen's default choice for the platform timer.
 
+### cmos-rtc-probe
+> `= <boolean>`
+
+> Default: `false`
+
+Flag to indicate whether to probe for a CMOS Real Time Clock irrespective of
+ACPI indicating none to be there.
+
 ### com1,com2
 > `= <baud>[/<clock_hz>][,[DPS][,[<io-base>|pci|amt][,[<irq>][,[<port-bdf>][,[<bridge-bdf>]]]]]]`
 
xen/arch/x86/time.c +70 -32

130 lines not on this page · raw message

x86/ACPI: allow CMOS RTC use even when ACPI says there is none

HP is setting the ACPI_FADT_NO_CMOS_RTC flag on newer systems, regardless of whether they're being booted from UEFI. Add a command line option to allow probing for a working RTC in that case.

docs/misc/xen-command-line.markdown +8 -0
@@ -218,6 +218,14 @@ If set, override Xen's calculation of th
 
 If set, override Xen's default choice for the platform timer.
 
+### cmos-rtc-probe
+> `= <boolean>`
+
+> Default: `false`
+
+Flag to indicate whether to probe for a CMOS Real Time Clock irrespective of
+ACPI indicating none to be there.
+
 ### com1,com2
 > `= <baud>[/<clock_hz>][,[DPS][,[<io-base>|pci|amt][,[<irq>][,[<port-bdf>][,[<bridge-bdf>]]]]]]`
 
xen/arch/x86/time.c +70 -32

130 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#1 lore raw
Quoted 105 lines · Jan Beulich

HP is setting the ACPI_FADT_NO_CMOS_RTC flag on newer systems, regardless of whether they're being booted from UEFI. Add a command line option to allow probing for a working RTC in that case.

docs/misc/xen-command-line.markdown +8 -0
@@ -218,6 +218,14 @@ If set, override Xen's calculation of th
 
 If set, override Xen's default choice for the platform timer.
 
+### cmos-rtc-probe
+> `= <boolean>`
+
+> Default: `false`
+
+Flag to indicate whether to probe for a CMOS Real Time Clock irrespective of
+ACPI indicating none to be there.
+
 ### com1,com2
 > `= <baud>[/<clock_hz>][,[DPS][,[<io-base>|pci|amt][,[<irq>][,[<port-bdf>][,[<bridge-bdf>]]]]]]`
 
xen/arch/x86/time.c +30 -27
@@ -654,37 +654,40 @@ mktime (unsigned int year, unsigned int 
         )*60 + sec; /* finally seconds */
 }
 
-static unsigned long __get_cmos_time(void)
-{
+struct rtc_time {
     unsigned int year, mon, day, hour, min, sec;
+};
 
-    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);
+static void __get_cmos_time(struct rtc_time *rtc)
+{
+    rtc->sec  = CMOS_READ(RTC_SECONDS);
+    rtc->min  = CMOS_READ(RTC_MINUTES);
+    rtc->hour = CMOS_READ(RTC_HOURS);
+    rtc->day  = CMOS_READ(RTC_DAY_OF_MONTH);
+    rtc->mon  = CMOS_READ(RTC_MONTH);
+    rtc->year = CMOS_READ(RTC_YEAR);
 
     if ( RTC_ALWAYS_BCD || !(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) )
     {
-        BCD_TO_BIN(sec);
-        BCD_TO_BIN(min);
-        BCD_TO_BIN(hour);
-        BCD_TO_BIN(day);
-        BCD_TO_BIN(mon);
-        BCD_TO_BIN(year);
+        BCD_TO_BIN(rtc->sec);
+        BCD_TO_BIN(rtc->min);
+        BCD_TO_BIN(rtc->hour);
+        BCD_TO_BIN(rtc->day);
+        BCD_TO_BIN(rtc->mon);
+        BCD_TO_BIN(rtc->year);
     }
 
-    if ( (year += 1900) < 1970 )
-        year += 100;
-
-    return mktime(year, mon, day, hour, min, sec);
+    if ( (rtc->year += 1900) < 1970 )
+        rtc->year += 100;
 }
 
 static unsigned long get_cmos_time(void)
 {
     unsigned long res, flags;
-    int i;
+    struct rtc_time rtc;
+    unsigned int seconds = 60;
+    static bool_t __read_mostly cmos_rtc_probe;
+    boolean_param("cmos-rtc-probe", cmos_rtc_probe);
 
     if ( efi_enabled )
     {
@@ -693,23 +696,58 @@ static unsigned long get_cmos_time(void)
             return res;
     }
 
-    if ( unlikely(acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_CMOS_RTC) )
-        panic("System without CMOS RTC must be booted from EFI");
-
-    spin_lock_irqsave(&rtc_lock, flags);
-
-    /* read RTC exactly on falling edge of update flag */
-    for ( i = 0 ; i < 1000000 ; i++ ) /* may take up to 1 second... */
-        if ( (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) )
+    if ( likely(!(acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_CMOS_RTC)) )
+        cmos_rtc_probe = 0;
+    else if ( system_state < SYS_STATE_smp_boot && !cmos_rtc_probe )
+        panic("System with no CMOS RTC advertised must be booted from EFI"
+              " (or with command line option \"cmos-rtc-probe\")");
+
+    for ( ; ; )
+    {

xen/arch/x86/time.c static unsigned long get_cmos_time(void) · line 716

Quoted 10 lines
xen/arch/x86/time.c +10 -0
+        s_time_t start, t1, t2;
+
+        spin_lock_irqsave(&rtc_lock, flags);
+
+        /* read RTC exactly on falling edge of update flag */
+        start = NOW();
+        do { /* may take up to 1 second... */
+            t1 = NOW() - start;
+        } while ( !(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) &&
+                  t1 <= SECONDS(1) );

Can we not break early if we exceed 1 second an have not seen an UIP ?

Quoted 11 lines
xen/arch/x86/time.c +11 -0
+
+        start = NOW();
+        do { /* must try at least 2.228 ms */
+            t2 = NOW() - start;
+        } while ( (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) &&
+                  t2 < MILLISECS(3) );
+
+        __get_cmos_time(&rtc);
+
+        spin_unlock_irqrestore(&rtc_lock, flags);
+

xen/arch/x86/time.c static unsigned long get_cmos_time(void) · line 735

Quoted 10 lines
xen/arch/x86/time.c +7 -2
+        if ( likely(!cmos_rtc_probe) ||
+             t1 > SECONDS(1) || t2 >= MILLISECS(3) ||
+             rtc.sec >= 60 || rtc.min >= 60 || rtc.hour >= 24 ||
+             !rtc.day || rtc.day > 31 ||
+             !rtc.mon || rtc.mon > 12 )
             break;
-    for ( i = 0 ; i < 1000000 ; i++ ) /* must try at least 2.228 ms */
-        if ( !(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) )
+
+        if ( seconds < 60 )

Seconds doesn't appear to be updated before this point, meaning that we will reprobe even if we find a plausible RTC.

Quoted 4 lines
xen/arch/x86/time.c +3 -0
+        {
+            if ( rtc.sec != seconds )
+                cmos_rtc_probe = 0;
             break;

xen/arch/x86/time.c static unsigned long get_cmos_time(void) · line 748

Quoted 10 lines
xen/arch/x86/time.c +8 -1
+        }
+
+        process_pending_softirqs();
+
+        seconds = rtc.sec;
+    }
 
-    res = __get_cmos_time();
+    if ( unlikely(cmos_rtc_probe) )
+        panic("No CMOS RTC found - system must be booted from EFI");

What happens in the case that we broke because of the validity checks for t1,t2 or rtc ? Do we want to differentiate between "no RTC" and "RTC giving bogus values" ?

~Andrew

Quoted 9 lines
xen/arch/x86/time.c +1 -2
-    spin_unlock_irqrestore(&rtc_lock, flags);
-    return res;
+    return mktime(rtc.year, rtc.mon, rtc.day, rtc.hour, rtc.min, rtc.sec);
 }
 
 /***************************************************************************
 
#2 lore raw
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 28.07.14 at 14:40, <[email protected]> wrote:

xen/arch/x86/time.c static unsigned long get_cmos_time(void) · line 716

Quoted 12 lines · Jan Beulich
xen/arch/x86/time.c +12 -0
+    for ( ; ; )
+    {
+        s_time_t start, t1, t2;
+
+        spin_lock_irqsave(&rtc_lock, flags);
+
+        /* read RTC exactly on falling edge of update flag */
+        start = NOW();
+        do { /* may take up to 1 second... */
+            t1 = NOW() - start;
+        } while ( !(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) &&
+                  t1 <= SECONDS(1) );

Can we not break early if we exceed 1 second an have not seen an UIP ?

Maybe, but I didn't want to make changes to the logic where not necessary.

Quoted 16 lines
Quoted 16 lines
xen/arch/x86/time.c +16 -0
+
+        start = NOW();
+        do { /* must try at least 2.228 ms */
+            t2 = NOW() - start;
+        } while ( (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) &&
+                  t2 < MILLISECS(3) );
+
+        __get_cmos_time(&rtc);
+
+        spin_unlock_irqrestore(&rtc_lock, flags);
+
+        if ( likely(!cmos_rtc_probe) ||
+             t1 > SECONDS(1) || t2 >= MILLISECS(3) ||
+             rtc.sec >= 60 || rtc.min >= 60 || rtc.hour >= 24 ||
+             !rtc.day || rtc.day > 31 ||
+             !rtc.mon || rtc.mon > 12 )
Quoted 8 lines

xen/arch/x86/time.c static unsigned long get_cmos_time(void) · line 735

Quoted 5 lines
             break;
xen/arch/x86/time.c +2 -2
-    for ( i = 0 ; i < 1000000 ; i++ ) /* must try at least 2.228 ms */
-        if ( !(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) )
+
+        if ( seconds < 60 )

Seconds doesn't appear to be updated before this point, meaning that we will reprobe even if we find a plausible RTC.

But that's exactly the point: We want to go through the loop twice. Only if the second round results in updated seconds do we consider the RTC okay for use.

Quoted 11 lines
Quoted 11 lines
xen/arch/x86/time.c +9 -0
+        {
+            if ( rtc.sec != seconds )
+                cmos_rtc_probe = 0;
             break;
+        }
+
+        process_pending_softirqs();
+
+        seconds = rtc.sec;
+    }
Quoted 7 lines

xen/arch/x86/time.c static unsigned long get_cmos_time(void) · line 748

Quoted 3 lines
xen/arch/x86/time.c +2 -1
-    res = __get_cmos_time();
+    if ( unlikely(cmos_rtc_probe) )
+        panic("No CMOS RTC found - system must be booted from EFI");

What happens in the case that we broke because of the validity checks for t1,t2 or rtc ? Do we want to differentiate between "no RTC" and "RTC giving bogus values" ?

How would you suggest to tell one from the other?

Jan

#3 lore raw
Quoted 24 lines · Jan Beulich
Quoted 16 lines
Quoted 16 lines
xen/arch/x86/time.c +15 -0
+
+        start = NOW();
+        do { /* must try at least 2.228 ms */
+            t2 = NOW() - start;
+        } while ( (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) &&
+                  t2 < MILLISECS(3) );
+
+        __get_cmos_time(&rtc);
+
+        spin_unlock_irqrestore(&rtc_lock, flags);
+
+        if ( likely(!cmos_rtc_probe) ||
+             t1 > SECONDS(1) || t2 >= MILLISECS(3) ||
+             rtc.sec >= 60 || rtc.min >= 60 || rtc.hour >= 24 ||
+             !rtc.day || rtc.day > 31 ||
Quoted 8 lines

xen/arch/x86/time.c static unsigned long get_cmos_time(void) · line 735

Quoted 6 lines
xen/arch/x86/time.c +3 -2
+             !rtc.mon || rtc.mon > 12 )
             break;
-    for ( i = 0 ; i < 1000000 ; i++ ) /* must try at least 2.228 ms */
-        if ( !(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) )
+
+        if ( seconds < 60 )

Seconds doesn't appear to be updated before this point, meaning that we will reprobe even if we find a plausible RTC.

Quoted 3 lines

But that's exactly the point: We want to go through the loop twice. Only if the second round results in updated seconds do we consider the RTC okay for use.

Right, but in the case that the RTC is handing back static values (which is slightly more likely if we are probing something which might not be a CMOS RTC), we will sit in the loop forever.

If on the second iteration seconds haven’t increased we should declare the probe to have failed.

Quoted 18 lines
Quoted 12 lines
Quoted 12 lines
xen/arch/x86/time.c +9 -0
+        {
+            if ( rtc.sec != seconds )
+                cmos_rtc_probe = 0;
             break;
+        }
+
+        process_pending_softirqs();
+
+        seconds = rtc.sec;
+    }
Quoted 6 lines

xen/arch/x86/time.c static unsigned long get_cmos_time(void) · line 748

Quoted 3 lines
xen/arch/x86/time.c +2 -1
-    res = __get_cmos_time();
+    if ( unlikely(cmos_rtc_probe) )
+        panic("No CMOS RTC found - system must be booted from EFI");

What happens in the case that we broke because of the validity checks for t1,t2 or rtc ? Do we want to differentiate between "no RTC" and "RTC giving bogus values" ?

Quoted 4 lines

How would you suggest to tell one from the other?

Jan

Now you put it like that, those two cases are rather hard to disentangle.

~Andrew

#4 lore raw
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 28.07.14 at 15:04, <[email protected]> wrote:
Quoted 24 lines · Jan Beulich
Quoted 24 lines
Quoted 12 lines
xen/arch/x86/time.c +11 -0
+
+        start = NOW();
+        do { /* must try at least 2.228 ms */
+            t2 = NOW() - start;
+        } while ( (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) &&
+                  t2 < MILLISECS(3) );
+
+        __get_cmos_time(&rtc);
+
+        spin_unlock_irqrestore(&rtc_lock, flags);
+

xen/arch/x86/time.c static unsigned long get_cmos_time(void) · line 735

Quoted 10 lines
xen/arch/x86/time.c +7 -2
+        if ( likely(!cmos_rtc_probe) ||
+             t1 > SECONDS(1) || t2 >= MILLISECS(3) ||
+             rtc.sec >= 60 || rtc.min >= 60 || rtc.hour >= 24 ||
+             !rtc.day || rtc.day > 31 ||
+             !rtc.mon || rtc.mon > 12 )
             break;
-    for ( i = 0 ; i < 1000000 ; i++ ) /* must try at least 2.228 ms */
-        if ( !(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) )
+
+        if ( seconds < 60 )

Seconds doesn't appear to be updated before this point, meaning that we will reprobe even if we find a plausible RTC.

Quoted 7 lines
Quoted 3 lines

But that's exactly the point: We want to go through the loop twice. Only if the second round results in updated seconds do we consider the RTC okay for use.

Right, but in the case that the RTC is handing back static values (which is slightly more likely if we are probing something which might not be a CMOS RTC), we will sit in the loop forever.

We won't: If seconds is an invalid value, we bail in the first iteration. If seconds is a valid value but unchanged on the second iteration, we bail there (see the still available context below.

Jan

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
xen/arch/x86/time.c +11 -1
+        {
+            if ( rtc.sec != seconds )
+                cmos_rtc_probe = 0;
             break;
+        }
+
+        process_pending_softirqs();
+
+        seconds = rtc.sec;
+    }
 
-    res = __get_cmos_time();
+    if ( unlikely(cmos_rtc_probe) )
+        panic("No CMOS RTC found - system must be booted from EFI");
#5 lore raw
Quoted 28 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 28.07.14 at 15:04, <[email protected]> wrote:
Quoted 26 lines · Jan Beulich
Quoted 15 lines
Quoted 15 lines
xen/arch/x86/time.c +15 -0
+
+        start = NOW();
+        do { /* must try at least 2.228 ms */
+            t2 = NOW() - start;
+        } while ( (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) &&
+                  t2 < MILLISECS(3) );
+
+        __get_cmos_time(&rtc);
+
+        spin_unlock_irqrestore(&rtc_lock, flags);
+
+        if ( likely(!cmos_rtc_probe) ||
+             t1 > SECONDS(1) || t2 >= MILLISECS(3) ||
+             rtc.sec >= 60 || rtc.min >= 60 || rtc.hour >= 24 ||
+             !rtc.day || rtc.day > 31 ||
Quoted 8 lines

xen/arch/x86/time.c static unsigned long get_cmos_time(void) · line 735

Quoted 6 lines
xen/arch/x86/time.c +3 -2
+             !rtc.mon || rtc.mon > 12 )
             break;
-    for ( i = 0 ; i < 1000000 ; i++ ) /* must try at least 2.228 ms */
-        if ( !(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) )
+
+        if ( seconds < 60 )

Seconds doesn't appear to be updated before this point, meaning that we will reprobe even if we find a plausible RTC.

But that's exactly the point: We want to go through the loop twice. Only if the second round results in updated seconds do we consider the RTC okay for use.

Quoted 8 lines
Quoted 3 lines

Right, but in the case that the RTC is handing back static values (which is slightly more likely if we are probing something which might not be a CMOS RTC), we will sit in the loop forever.

We won't: If seconds is an invalid value, we bail in the first iteration. If seconds is a valid value but unchanged on the second iteration, we bail there (see the still available context below.

Jan

Silly me. I had mentally included the break in the if ( rtc.sec != seconds ) scope.

In which case, Reviewed-by: Andrew Cooper <[email protected]>

Unfortunately we don't have affected hardware to test with.

~Andrew

Quoted 16 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
xen/arch/x86/time.c +11 -1
+        {
+            if ( rtc.sec != seconds )
+                cmos_rtc_probe = 0;
             break;
+        }
+
+        process_pending_softirqs();
+
+        seconds = rtc.sec;
+    }
 
-    res = __get_cmos_time();
+    if ( unlikely(cmos_rtc_probe) )
+        panic("No CMOS RTC found - system must be booted from EFI");