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