xref: /netbsd-src/sys/dev/acpi/acpi_timer.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /* $NetBSD: acpi_timer.c,v 1.9 2007/09/26 19:48:45 ad Exp $ */
2 
3 #include <sys/cdefs.h>
4 __KERNEL_RCSID(0, "$NetBSD: acpi_timer.c,v 1.9 2007/09/26 19:48:45 ad Exp $");
5 
6 #include <sys/types.h>
7 #include <dev/acpi/acpi_timer.h>
8 
9 #ifdef __HAVE_TIMECOUNTER
10 
11 #include <sys/systm.h>
12 #include <sys/time.h>
13 #include <sys/timetc.h>
14 #include <dev/acpi/acpica.h>
15 
16 static int acpitimer_test(void);
17 static uint32_t acpitimer_delta(uint32_t, uint32_t);
18 static u_int acpitimer_read_safe(struct timecounter *);
19 static u_int acpitimer_read_fast(struct timecounter *);
20 
21 static struct timecounter acpi_timecounter = {
22 	acpitimer_read_safe,
23 	0,
24 	0x00ffffff,
25 	PM_TIMER_FREQUENCY,
26 	"ACPI-Safe",
27 	900,
28 	NULL,
29 	NULL,
30 };
31 
32 int
33 acpitimer_init()
34 {
35 	uint32_t bits;
36 	int i, j;
37 	ACPI_STATUS res;
38 
39 	res = AcpiGetTimerResolution(&bits);
40 	if (res != AE_OK)
41 		return (-1);
42 
43 	if (bits == 32)
44 		acpi_timecounter.tc_counter_mask = 0xffffffff;
45 
46 	j = 0;
47 	for (i = 0; i < 10; i++)
48 		j += acpitimer_test();
49 
50 	if (j >= 10) {
51 		acpi_timecounter.tc_name = "ACPI-Fast";
52 		acpi_timecounter.tc_get_timecount = acpitimer_read_fast;
53 		acpi_timecounter.tc_quality = 1000;
54 	}
55 
56 	tc_init(&acpi_timecounter);
57 	aprint_normal("%s %d-bit timer\n", acpi_timecounter.tc_name, bits);
58 
59 	return (0);
60 }
61 
62 static u_int
63 acpitimer_read_fast(struct timecounter *tc)
64 {
65 	uint32_t t;
66 
67 	AcpiGetTimer(&t);
68 	return (t);
69 }
70 
71 /*
72  * Some chipsets (PIIX4 variants) do not latch correctly; there
73  * is a chance that a transition is hit.
74  */
75 static u_int
76 acpitimer_read_safe(struct timecounter *tc)
77 {
78 	uint32_t t1, t2, t3;
79 
80 	AcpiGetTimer(&t2);
81 	AcpiGetTimer(&t3);
82 	do {
83 		t1 = t2;
84 		t2 = t3;
85 		AcpiGetTimer(&t3);
86 	} while ((t1 > t2) || (t2 > t3));
87 	return (t2);
88 }
89 
90 static uint32_t
91 acpitimer_delta(uint32_t end, uint32_t start)
92 {
93 	uint32_t delta;
94 	u_int mask = acpi_timecounter.tc_counter_mask;
95 
96 	if (end >= start)
97 		delta = end - start;
98 	else
99 		delta = ((mask - start) + end + 1) & mask;
100 
101 	return (delta);
102 }
103 
104 #define N 2000
105 static int
106 acpitimer_test()
107 {
108 	uint32_t last, this, delta;
109 	int minl, maxl, n;
110 
111 	minl = 10000000;
112 	maxl = 0;
113 
114 	x86_disable_intr();
115 	AcpiGetTimer(&last);
116 	for (n = 0; n < N; n++) {
117 		AcpiGetTimer(&this);
118 		delta = acpitimer_delta(this, last);
119 		if (delta > maxl)
120 			maxl = delta;
121 		else if (delta < minl)
122 			minl = delta;
123 		last = this;
124 	}
125 	x86_enable_intr();
126 
127 	if (maxl - minl > 2 )
128 		n = 0;
129 	else if (minl < 0 || maxl == 0)
130 		n = 0;
131 	else
132 		n = 1;
133 
134 	return (n);
135 }
136 
137 #else
138 
139 int
140 acpitimer_init()
141 {
142 
143 	return (0);
144 }
145 
146 #endif
147