2008-05-12 02:16:45 +04:00
|
|
|
/* $NetBSD: acpi_timer.c,v 1.11 2008/05/11 22:16:45 ad Exp $ */
|
2006-07-01 03:21:19 +04:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2008-05-12 02:16:45 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: acpi_timer.c,v 1.11 2008/05/11 22:16:45 ad Exp $");
|
2006-06-21 21:47:23 +04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/timetc.h>
|
|
|
|
#include <dev/acpi/acpica.h>
|
2008-01-20 21:09:03 +03:00
|
|
|
#include <dev/acpi/acpi_timer.h>
|
2006-06-21 21:47:23 +04:00
|
|
|
|
2006-06-26 16:29:36 +04:00
|
|
|
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 *);
|
2006-06-21 21:47:23 +04:00
|
|
|
|
|
|
|
static struct timecounter acpi_timecounter = {
|
2006-06-26 16:29:36 +04:00
|
|
|
acpitimer_read_safe,
|
2006-06-21 21:47:23 +04:00
|
|
|
0,
|
|
|
|
0x00ffffff,
|
|
|
|
PM_TIMER_FREQUENCY,
|
2006-06-26 16:29:36 +04:00
|
|
|
"ACPI-Safe",
|
2006-08-28 03:13:47 +04:00
|
|
|
900,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2006-06-21 21:47:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
acpitimer_init()
|
|
|
|
{
|
|
|
|
uint32_t bits;
|
2006-06-26 16:29:36 +04:00
|
|
|
int i, j;
|
2006-06-21 21:47:23 +04:00
|
|
|
ACPI_STATUS res;
|
|
|
|
|
|
|
|
res = AcpiGetTimerResolution(&bits);
|
|
|
|
if (res != AE_OK)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (bits == 32)
|
|
|
|
acpi_timecounter.tc_counter_mask = 0xffffffff;
|
|
|
|
|
2006-06-26 16:29:36 +04:00
|
|
|
j = 0;
|
|
|
|
for (i = 0; i < 10; i++)
|
|
|
|
j += acpitimer_test();
|
2006-07-01 03:19:40 +04:00
|
|
|
|
2006-06-26 16:29:36 +04:00
|
|
|
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);
|
2008-05-12 02:16:45 +04:00
|
|
|
aprint_verbose("%s %d-bit timer\n", acpi_timecounter.tc_name, bits);
|
2006-06-21 21:47:23 +04:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2006-06-26 16:29:36 +04:00
|
|
|
static u_int
|
2006-11-16 04:32:37 +03:00
|
|
|
acpitimer_read_fast(struct timecounter *tc)
|
2006-06-26 16:29:36 +04:00
|
|
|
{
|
|
|
|
uint32_t t;
|
|
|
|
|
|
|
|
AcpiGetTimer(&t);
|
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
|
2006-06-21 21:47:23 +04:00
|
|
|
/*
|
|
|
|
* Some chipsets (PIIX4 variants) do not latch correctly; there
|
|
|
|
* is a chance that a transition is hit.
|
|
|
|
*/
|
|
|
|
static u_int
|
2006-11-16 04:32:37 +03:00
|
|
|
acpitimer_read_safe(struct timecounter *tc)
|
2006-06-21 21:47:23 +04:00
|
|
|
{
|
|
|
|
uint32_t t1, t2, t3;
|
|
|
|
|
|
|
|
AcpiGetTimer(&t2);
|
|
|
|
AcpiGetTimer(&t3);
|
|
|
|
do {
|
|
|
|
t1 = t2;
|
|
|
|
t2 = t3;
|
|
|
|
AcpiGetTimer(&t3);
|
|
|
|
} while ((t1 > t2) || (t2 > t3));
|
|
|
|
return (t2);
|
|
|
|
}
|
|
|
|
|
2006-06-26 16:29:36 +04:00
|
|
|
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;
|
|
|
|
|
2007-09-26 23:48:34 +04:00
|
|
|
x86_disable_intr();
|
2006-06-26 16:29:36 +04:00
|
|
|
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;
|
|
|
|
}
|
2007-09-26 23:48:34 +04:00
|
|
|
x86_enable_intr();
|
2006-06-26 16:29:36 +04:00
|
|
|
|
|
|
|
if (maxl - minl > 2 )
|
|
|
|
n = 0;
|
|
|
|
else if (minl < 0 || maxl == 0)
|
|
|
|
n = 0;
|
|
|
|
else
|
|
|
|
n = 1;
|
|
|
|
|
|
|
|
return (n);
|
|
|
|
}
|