Merge branch 'vendor/OPENBSD_LIBM'
[dragonfly.git] / sys / dev / acpica / acpi_timer.c
1 /*-
2  * Copyright (c) 2000, 2001 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/acpica/acpi_timer.c,v 1.35 2004/07/22 05:42:14 njl Exp $
28  */
29 #include "opt_acpi.h"
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/sysctl.h>
35 #include <sys/systimer.h>
36 #include <sys/rman.h>
37
38 #include <machine/lock.h>
39 #include <bus/pci/pcivar.h>
40
41 #include "acpi.h"
42 #include "accommon.h"
43 #include "acpivar.h"
44
45 /*
46  * A timecounter based on the free-running ACPI timer.
47  *
48  * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
49  */
50
51 /* Hooks for the ACPICA debugging infrastructure */
52 #define _COMPONENT      ACPI_TIMER
53 ACPI_MODULE_NAME("TIMER")
54
55 static device_t                 acpi_timer_dev;
56 static UINT32                   acpi_timer_resolution;
57 static sysclock_t               acpi_last_counter;
58
59 static sysclock_t acpi_timer_get_timecount(void);
60 static sysclock_t acpi_timer_get_timecount24(void);
61 static sysclock_t acpi_timer_get_timecount_safe(void);
62 static void acpi_timer_construct(struct cputimer *timer, sysclock_t oldclock);
63
64 static struct cputimer acpi_cputimer = {
65         SLIST_ENTRY_INITIALIZER,
66         "ACPI",
67         CPUTIMER_PRI_ACPI,
68         CPUTIMER_ACPI,
69         acpi_timer_get_timecount_safe,
70         cputimer_default_fromhz,
71         cputimer_default_fromus,
72         acpi_timer_construct,
73         cputimer_default_destruct,
74         ACPI_PM_TIMER_FREQUENCY,
75         0, 0, 0
76 };
77
78 static int      acpi_timer_identify(driver_t *driver, device_t parent);
79 static int      acpi_timer_probe(device_t dev);
80 static int      acpi_timer_attach(device_t dev);
81 static int      acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
82
83 static int      acpi_timer_test(void);
84
85 static device_method_t acpi_timer_methods[] = {
86     DEVMETHOD(device_identify,  acpi_timer_identify),
87     DEVMETHOD(device_probe,     acpi_timer_probe),
88     DEVMETHOD(device_attach,    acpi_timer_attach),
89
90     DEVMETHOD_END
91 };
92
93 static driver_t acpi_timer_driver = {
94     "acpi_timer",
95     acpi_timer_methods,
96     0,
97 };
98
99 static devclass_t acpi_timer_devclass;
100 DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, NULL, NULL);
101 MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
102
103 /*
104  * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
105  * we will be using.
106  */
107 static int
108 acpi_timer_identify(driver_t *driver, device_t parent)
109 {
110     device_t dev;
111
112     /*
113      * Just try once, do nothing if the 'acpi' bus is rescanned.
114      */
115     if (device_get_state(parent) == DS_ATTACHED)
116         return (0);
117
118     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
119
120     if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) ||
121         acpi_timer_dev)
122         return (ENXIO);
123
124     if ((dev = BUS_ADD_CHILD(parent, parent, 0, "acpi_timer", 0)) == NULL) {
125         device_printf(parent, "could not add acpi_timer0\n");
126         return (ENXIO);
127     }
128     acpi_timer_dev = dev;
129
130     return (0);
131 }
132
133 static int
134 acpi_timer_probe(device_t dev)
135 {
136     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
137
138     if (dev != acpi_timer_dev)
139         return (ENXIO);
140
141     if (ACPI_FAILURE(AcpiGetTimerResolution(&acpi_timer_resolution)))
142         return (ENXIO);
143
144     return (0);
145 }
146
147 static int
148 acpi_timer_attach(device_t dev)
149 {
150     char desc[40];
151     int i, j;
152
153     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
154
155     /*
156      * If all tests of the counter succeed, use the ACPI-fast method.  If
157      * at least one failed, default to using the safe routine, which reads
158      * the timer multiple times to get a consistent value before returning.
159      */
160     j = 0;
161     for (i = 0; i < 10; i++)
162         j += acpi_timer_test();
163     if (j == 10) {
164         if (acpi_timer_resolution == 32) {
165             acpi_cputimer.name = "ACPI-fast";
166             acpi_cputimer.count = acpi_timer_get_timecount;
167         } else {
168             acpi_cputimer.name = "ACPI-fast24";
169             acpi_cputimer.count = acpi_timer_get_timecount24;
170         }
171     } else {
172         if (acpi_timer_resolution == 32)
173             acpi_cputimer.name = "ACPI-safe";
174         else
175             acpi_cputimer.name = "ACPI-safe24";
176         acpi_cputimer.count = acpi_timer_get_timecount_safe;
177     }
178
179     ksprintf(desc, "%u-bit timer at 3.579545MHz", acpi_timer_resolution);
180     device_set_desc_copy(dev, desc);
181
182     cputimer_register(&acpi_cputimer);
183     cputimer_select(&acpi_cputimer, 0);
184
185     return (0);
186 }
187
188 /*
189  * Construct the timer.  Adjust the base so the system clock does not
190  * jump weirdly.
191  */
192 static void
193 acpi_timer_construct(struct cputimer *timer, sysclock_t oldclock)
194 {
195     timer->base = 0;
196     timer->base = oldclock - acpi_timer_get_timecount_safe();
197 }
198
199 /*
200  * Fetch current time value from reliable hardware.
201  *
202  * The cputimer interface requires a 32 bit return value.  If the ACPI timer
203  * is only 24 bits then we have to keep track of the upper 8 bits on our
204  * own.
205  *
206  * XXX we could probably get away with using a per-cpu field for this and
207  * just use interrupt disablement instead of clock_lock.
208  */
209 static sysclock_t
210 acpi_timer_get_timecount24(void)
211 {
212     sysclock_t counter;
213
214     clock_lock();
215     AcpiGetTimer(&counter);
216     if (counter < acpi_last_counter)
217         acpi_cputimer.base += 0x01000000;
218     acpi_last_counter = counter;
219     counter += acpi_cputimer.base;
220     clock_unlock();
221     return (counter);
222 }
223
224 static sysclock_t
225 acpi_timer_get_timecount(void)
226 {
227     sysclock_t counter;
228
229     AcpiGetTimer(&counter);
230     return (counter + acpi_cputimer.base);
231 }
232
233 /*
234  * Fetch current time value from hardware that may not correctly
235  * latch the counter.  We need to read until we have three monotonic
236  * samples and then use the middle one, otherwise we are not protected
237  * against the fact that the bits can be wrong in two directions.  If
238  * we only cared about monosity, two reads would be enough.
239  */
240 static sysclock_t
241 acpi_timer_get_timecount_safe(void)
242 {
243     u_int u1, u2, u3;
244
245     if (acpi_timer_resolution != 32)
246         clock_lock();
247
248     AcpiGetTimer(&u2);
249     AcpiGetTimer(&u3);
250     do {
251         u1 = u2;
252         u2 = u3;
253         AcpiGetTimer(&u3);
254     } while (u1 > u2 || u2 > u3);
255
256     if (acpi_timer_resolution != 32) {
257         if (u2 < acpi_last_counter)
258             acpi_cputimer.base += 0x01000000;
259         acpi_last_counter = u2;
260         clock_unlock();
261     }
262     return (u2 + acpi_cputimer.base);
263 }
264
265 /*
266  * Timecounter freqency adjustment interface.
267  */ 
268 static int
269 acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
270 {
271     int error;
272     u_int freq;
273  
274     if (acpi_cputimer.freq == 0)
275         return (EOPNOTSUPP);
276     freq = acpi_cputimer.freq;
277     error = sysctl_handle_int(oidp, &freq, 0, req);
278     if (error == 0 && req->newptr != NULL)
279         cputimer_set_frequency(&acpi_cputimer, freq);
280
281     return (error);
282 }
283  
284 SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
285     0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "ACPI timer frequency");
286
287 /*
288  * Some ACPI timers are known or believed to suffer from implementation
289  * problems which can lead to erroneous values being read.  This function
290  * tests for consistent results from the timer and returns 1 if it believes
291  * the timer is consistent, otherwise it returns 0.
292  *
293  * It appears the cause is that the counter is not latched to the PCI bus
294  * clock when read:
295  *
296  * ] 20. ACPI Timer Errata
297  * ]
298  * ]   Problem: The power management timer may return improper result when
299  * ]   read. Although the timer value settles properly after incrementing,
300  * ]   while incrementing there is a 3nS window every 69.8nS where the
301  * ]   timer value is indeterminate (a 4.2% chance that the data will be
302  * ]   incorrect when read). As a result, the ACPI free running count up
303  * ]   timer specification is violated due to erroneous reads.  Implication:
304  * ]   System hangs due to the "inaccuracy" of the timer when used by
305  * ]   software for time critical events and delays.
306  * ]
307  * ] Workaround: Read the register twice and compare.
308  * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
309  * ] in the PIIX4M.
310  */
311
312 static int
313 acpi_timer_test(void)
314 {
315     uint32_t    last, this;
316     int         min, max, max2, n, delta;
317     register_t  s;
318
319     min = INT32_MAX;
320     max = max2 = 0;
321
322     /* Test the timer with interrupts disabled to get accurate results. */
323 #if defined(__i386__)
324     s = read_eflags();
325 #elif defined(__x86_64__)
326     s = read_rflags();
327 #else
328 #error "no read_eflags"
329 #endif
330     cpu_disable_intr();
331     AcpiGetTimer(&last);
332     for (n = 0; n < 2000; n++) {
333         AcpiGetTimer(&this);
334         delta = acpi_TimerDelta(this, last);
335         if (delta > max) {
336             max2 = max;
337             max = delta;
338         } else if (delta > max2) {
339             max2 = delta;
340         }
341         if (delta < min)
342             min = delta;
343         last = this;
344     }
345 #if defined(__i386__)
346     write_eflags(s);
347 #elif defined(__x86_64__)
348     write_rflags(s);
349 #else
350 #error "no read_eflags"
351 #endif
352
353     delta = max2 - min;
354     if ((max - min > 8 || delta > 3) && vmm_guest == VMM_GUEST_NONE)
355         n = 0;
356     else if (min < 0 || max == 0 || max2 == 0)
357         n = 0;
358     else
359         n = 1;
360     if (bootverbose) {
361         kprintf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
362                 n ? "GOOD" : "BAD ",
363                 min, max, max - min);
364     }
365
366     return (n);
367 }