:My only worry here is laptops.
:
:If my laptop is connected to the wall-power when it boots it comes
:up at full CPU speed during the probe and mask determination.
:
:Then I unplug it and the CPU speed drops by a large factor and
:suddenly the mask is too tight for the CPU to get two samples.
:
:I think we need an approach where we loosen the mask at runtime if
:some significant number of iterations fail to get a count may be
:needed:
:
:#define N      25
:
:       static unsigned mask = 0xfffffffe;
:
:       for (;;) {
:               for (i = 0; i < N; i++) {
:                       c = READ_TIMER & mask;
:                       d = READ_TIMER & mask;
:                       if (c == d)
:                               return(d);
:               }
:               printf("Adjusting mask to %08x\n", mask);
:               mask <<= 1;
:       }
:
:
:-- 
:Poul-Henning Kamp       | UNIX since Zilog Zeus 3.20
:[EMAIL PROTECTED]         | TCP/IP since RFC 956

    Ok, try this patch.  It will do fallback but does not do fallforward,
    and performance should be improved for the best case.  I also put
    the mask in a _debug sysctl so you can play with it.  Be careful,
    you can blow up the time counter if you set it to a bad value like 0

    reasonable values: -1, -2, -4, -8, -16, -32, -64.  If you set it too
    strict (i.e. -1) it will auto fallback.

                                        -Matt
                                        Matthew Dillon 
                                        <[EMAIL PROTECTED]>

Index: acpi_timer.c
===================================================================
RCS file: /home/ncvs/src/sys/dev/acpica/acpi_timer.c,v
retrieving revision 1.11
diff -u -r1.11 acpi_timer.c
--- acpi_timer.c        8 Jan 2002 06:45:56 -0000       1.11
+++ acpi_timer.c        18 Feb 2002 22:02:55 -0000
@@ -56,11 +56,17 @@
 MODULE_NAME("TIMER")
 
 static device_t        acpi_timer_dev;
+static u_int32_t acpi_timer_mask;
 struct resource        *acpi_timer_reg;
 #define TIMER_READ     bus_space_read_4(rman_get_bustag(acpi_timer_reg),       \
                                         rman_get_bushandle(acpi_timer_reg),    \
                                         0)
 
+/*
+ * Loops before we give up and recalculate the mask
+ */
+#define NLOOP  32      /* power of 2 */
+
 static u_int   acpi_timer_frequency = 14318182/4;
 
 static void    acpi_timer_identify(driver_t *driver, device_t parent);
@@ -70,6 +76,7 @@
 static unsigned        acpi_timer_get_timecount_safe(struct timecounter *tc);
 static int     acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
 static void    acpi_timer_test(void);
+static void    calc_acpi_timer_mask(void);
 
 /*
  * Driver hung off ACPI.
@@ -102,6 +109,7 @@
     "ACPI"
 };
 
+SYSCTL_INT(_debug, OID_AUTO, acpi_timer_mask, CTLFLAG_RW, &acpi_timer_mask, 0, "");
 SYSCTL_OPAQUE(_debug, OID_AUTO, acpi_timecounter, CTLFLAG_RD,
              &acpi_timer_timecounter, sizeof(acpi_timer_timecounter), 
"S,timecounter", "");
 
@@ -113,7 +121,7 @@
 acpi_timer_identify(driver_t *driver, device_t parent)
 {
     device_t   dev;
-    char       desc[40];
+    char       desc[48];
     int                rid;
 
     FUNCTION_TRACE(__func__);
@@ -138,14 +146,17 @@
     if (getenv("debug.acpi.timer_test") != NULL)
        acpi_timer_test();
 
-    acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
+    acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
     acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
     tc_init(&acpi_timer_timecounter);
 
-    sprintf(desc, "%d-bit timer at 3.579545MHz", AcpiGbl_FADT->TmrValExt ? 32 : 24);
+    calc_acpi_timer_mask();
+
+    sprintf(desc, "%d-bit timer at 3.579545MHz mask %08x", 
+       (AcpiGbl_FADT->TmrValExt ? 32 : 24), acpi_timer_mask);
     device_set_desc_copy(dev, desc);
 
-#if 0    
+#if 0
     {
        u_int64_t       first;
     
@@ -162,6 +173,32 @@
     return_VOID;
 }
 
+static
+void
+calc_acpi_timer_mask(void)
+{
+    u_int32_t mask;
+
+    critical_enter();
+    for (mask = 0xFFFFFFFF; mask; mask <<= 1) {
+       u_int32_t u1;
+       u_int32_t u2;
+       int count = NLOOP;
+
+       u1 = TIMER_READ;
+       u2 = TIMER_READ;
+       while (count && ((u1 ^ u2) & mask)) {
+           u1 = u2;
+           u2 = TIMER_READ;
+           --count;
+       }
+       if (count)
+           break;
+    }
+    critical_exit();
+    acpi_timer_mask = mask << 1;
+}
+
 static int
 acpi_timer_probe(device_t dev)
 {
@@ -192,16 +229,32 @@
 static unsigned
 acpi_timer_get_timecount_safe(struct timecounter *tc)
 {
-    unsigned u1, u2, u3;
+    u_int32_t u1;
+    u_int32_t u2;
+    int n;
+
+    u1 = TIMER_READ & acpi_timer_mask;
+    u2 = TIMER_READ & acpi_timer_mask;
+    if (u1 == u2)
+       return(u2);
+    u1 = u2;
+    u2 = TIMER_READ & acpi_timer_mask;
+    if (u1 == u2)
+       return(u2);
 
-    u2 = TIMER_READ;
-    u3 = TIMER_READ;
-    do {
+    n = 0;
+    for (;;) {
        u1 = u2;
-       u2 = u3;
-       u3 = TIMER_READ;
-    } while (u1 > u2 || u2 > u3);
-    return (u2);
+       u2 = TIMER_READ & acpi_timer_mask;
+       if (u1 == u2)
+           break;
+       ++n;
+       if ((n & (NLOOP - 1)) == 0) {
+           calc_acpi_timer_mask();
+           u2 = TIMER_READ & acpi_timer_mask;
+       }
+    }
+    return(u2);
 }
 
 /*

To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message

Reply via email to