1 /* $NetBSD: acpi_timer.c,v 1.27 2021/07/25 01:43:08 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Matthias Drochner <drochner@NetBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: acpi_timer.c,v 1.27 2021/07/25 01:43:08 jmcneill Exp $");
31
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/time.h>
35 #include <sys/timetc.h>
36
37 #include <dev/acpi/acpivar.h>
38 #include <dev/acpi/acpi_timer.h>
39
40 #include <machine/acpi_machdep.h>
41
42 #if (!ACPI_REDUCED_HARDWARE)
43 static int acpitimer_test(void);
44
45 static struct timecounter acpi_timecounter = {
46 .tc_get_timecount = acpitimer_read_safe,
47 .tc_counter_mask = 0x00ffffff,
48 .tc_frequency = ACPI_PM_TIMER_FREQUENCY,
49 .tc_name = "ACPI-Safe",
50 .tc_quality = 900,
51 };
52
53 static bool
acpitimer_supported(void)54 acpitimer_supported(void)
55 {
56 return AcpiGbl_FADT.PmTimerLength != 0;
57 }
58 #endif
59
60 int
acpitimer_init(struct acpi_softc * sc)61 acpitimer_init(struct acpi_softc *sc)
62 {
63 #if (!ACPI_REDUCED_HARDWARE)
64 ACPI_TABLE_WAET *waet;
65 ACPI_STATUS rv;
66 uint32_t bits;
67 int i, j;
68
69 if (!acpitimer_supported())
70 return -1;
71
72 rv = AcpiGetTimerResolution(&bits);
73
74 if (ACPI_FAILURE(rv))
75 return -1;
76
77 if (bits == 32)
78 acpi_timecounter.tc_counter_mask = 0xffffffff;
79
80 for (i = j = 0; i < 10; i++)
81 j += acpitimer_test();
82
83 rv = AcpiGetTable(ACPI_SIG_WAET, 0, (ACPI_TABLE_HEADER **)&waet);
84 if (ACPI_SUCCESS(rv)) {
85 /*
86 * Windows ACPI Emulated Devices Table (WAET) has a hint
87 * to let the OS know that a single read of the PM timer
88 * provides a reliable value.
89 */
90 if ((waet->Flags & ACPI_WAET_TIMER_ONE_READ) != 0) {
91 j += 10;
92 }
93 }
94
95 if (j >= 10) {
96 acpi_timecounter.tc_name = "ACPI-Fast";
97 acpi_timecounter.tc_get_timecount = acpitimer_read_fast;
98 acpi_timecounter.tc_quality = 1000;
99 }
100
101 tc_init(&acpi_timecounter);
102
103 aprint_debug_dev(sc->sc_dev, "%s %d-bit timer\n",
104 acpi_timecounter.tc_name, bits);
105
106 return 0;
107 #else
108 return -1;
109 #endif
110 }
111
112 int
acpitimer_detach(void)113 acpitimer_detach(void)
114 {
115 #if (!ACPI_REDUCED_HARDWARE)
116 if (!acpitimer_supported())
117 return -1;
118
119 return tc_detach(&acpi_timecounter);
120 #else
121 return -1;
122 #endif
123 }
124
125 #if (!ACPI_REDUCED_HARDWARE)
126 u_int
acpitimer_read_fast(struct timecounter * tc)127 acpitimer_read_fast(struct timecounter *tc)
128 {
129 uint32_t t;
130
131 (void)AcpiGetTimer(&t);
132
133 return t;
134 }
135
136 /*
137 * Some chipsets (PIIX4 variants) do not latch correctly;
138 * there is a chance that a transition is hit.
139 */
140 u_int
acpitimer_read_safe(struct timecounter * tc)141 acpitimer_read_safe(struct timecounter *tc)
142 {
143 uint32_t t1, t2, t3;
144
145 (void)AcpiGetTimer(&t2);
146 (void)AcpiGetTimer(&t3);
147
148 do {
149 t1 = t2;
150 t2 = t3;
151
152 (void)AcpiGetTimer(&t3);
153
154 } while ((t1 > t2) || (t2 > t3));
155
156 return t2;
157 }
158
159 uint32_t
acpitimer_delta(uint32_t end,uint32_t start)160 acpitimer_delta(uint32_t end, uint32_t start)
161 {
162 const u_int mask = acpi_timecounter.tc_counter_mask;
163 uint32_t delta;
164
165 if (end >= start)
166 delta = end - start;
167 else
168 delta = ((mask - start) + end + 1) & mask;
169
170 return delta;
171 }
172
173 #define N 2000
174
175 static int
acpitimer_test(void)176 acpitimer_test(void)
177 {
178 uint32_t last, this, delta;
179 int minl, maxl, n;
180
181 minl = 10000000;
182 maxl = 0;
183
184 acpi_md_OsDisableInterrupt();
185
186 (void)AcpiGetTimer(&last);
187
188 for (n = 0; n < N; n++) {
189
190 (void)AcpiGetTimer(&this);
191
192 delta = acpitimer_delta(this, last);
193
194 if (delta > maxl)
195 maxl = delta;
196 else if (delta < minl)
197 minl = delta;
198
199 last = this;
200 }
201
202 acpi_md_OsEnableInterrupt();
203
204 if (maxl - minl > 2 )
205 n = 0;
206 else if (minl < 0 || maxl == 0)
207 n = 0;
208 else
209 n = 1;
210
211 return n;
212 }
213 #endif
214