test the PM timer for glitches (using the algorithm from FreeBSD)

and use a single read if the timer looks good,
patch from Juan RP, with minor simplification by me
This commit is contained in:
drochner 2006-06-26 12:29:36 +00:00
parent 14db0438ab
commit 3ce3b9b485
1 changed files with 77 additions and 9 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: acpi_timer.c,v 1.1 2006/06/21 17:47:23 drochner Exp $ */
/* $NetBSD: acpi_timer.c,v 1.2 2006/06/26 12:29:36 drochner Exp $ */
#include <sys/types.h>
@ -10,14 +10,17 @@
#include <dev/acpi/acpica.h>
#include <dev/acpi/acpi_timer.h>
static u_int acpitimer_read(struct timecounter *);
static int acpitimer_test(void);
static uint32_t acpitimer_delta(uint32_t, uint32_t);
static u_int acpitimer_read_safe(struct timecounter *);
static u_int acpitimer_read_fast(struct timecounter *);
static struct timecounter acpi_timecounter = {
acpitimer_read,
acpitimer_read_safe,
0,
0x00ffffff,
PM_TIMER_FREQUENCY,
"ACPI_PM_TMR",
"ACPI-Safe",
900
};
@ -25,6 +28,7 @@ int
acpitimer_init()
{
uint32_t bits;
int i, j;
ACPI_STATUS res;
res = AcpiGetTimerResolution(&bits);
@ -33,21 +37,38 @@ acpitimer_init()
if (bits == 32)
acpi_timecounter.tc_counter_mask = 0xffffffff;
tc_init(&acpi_timecounter);
printf("acpitimer: %d bits\n", bits);
j = 0;
for (i = 0; i < 10; i++)
j += acpitimer_test();
aprint_verbose("acpitimer_test(): %d\n", j);
if (j >= 10) {
acpi_timecounter.tc_name = "ACPI-Fast";
acpi_timecounter.tc_get_timecount = acpitimer_read_fast;
acpi_timecounter.tc_quality = 1000;
}
tc_init(&acpi_timecounter);
aprint_normal("%s %d-bit timer\n", acpi_timecounter.tc_name, bits);
return (0);
}
static u_int
acpitimer_read_fast(struct timecounter *tc)
{
uint32_t t;
AcpiGetTimer(&t);
return (t);
}
/*
* Some chipsets (PIIX4 variants) do not latch correctly; there
* is a chance that a transition is hit.
* For now, just be conservative. We might detect the situation later
* (by testing, or chipset quirks).
*/
static u_int
acpitimer_read(struct timecounter *tc)
acpitimer_read_safe(struct timecounter *tc)
{
uint32_t t1, t2, t3;
@ -61,6 +82,53 @@ acpitimer_read(struct timecounter *tc)
return (t2);
}
static uint32_t
acpitimer_delta(uint32_t end, uint32_t start)
{
uint32_t delta;
u_int mask = acpi_timecounter.tc_counter_mask;
if (end >= start)
delta = end - start;
else
delta = ((mask - start) + end + 1) & mask;
return (delta);
}
#define N 2000
static int
acpitimer_test()
{
uint32_t last, this, delta;
int minl, maxl, n;
minl = 10000000;
maxl = 0;
disable_intr();
AcpiGetTimer(&last);
for (n = 0; n < N; n++) {
AcpiGetTimer(&this);
delta = acpitimer_delta(this, last);
if (delta > maxl)
maxl = delta;
else if (delta < minl)
minl = delta;
last = this;
}
enable_intr();
if (maxl - minl > 2 )
n = 0;
else if (minl < 0 || maxl == 0)
n = 0;
else
n = 1;
return (n);
}
#else
int