2 * Copyright (c) 2000, 2001 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: src/sys/dev/acpica/acpi_timer.c,v 1.35 2004/07/22 05:42:14 njl Exp $
30 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/sysctl.h>
35 #include <sys/systimer.h>
38 #include <machine/lock.h>
39 #include <bus/pci/pcivar.h>
46 * A timecounter based on the free-running ACPI timer.
48 * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
51 /* Hooks for the ACPICA debugging infrastructure */
52 #define _COMPONENT ACPI_TIMER
53 ACPI_MODULE_NAME("TIMER")
55 static device_t acpi_timer_dev;
56 static UINT32 acpi_timer_resolution;
58 static sysclock_t acpi_timer_get_timecount(void);
59 static sysclock_t acpi_timer_get_timecount24(void);
60 static sysclock_t acpi_timer_get_timecount_safe(void);
61 static void acpi_timer_construct(struct cputimer *timer, sysclock_t oldclock);
63 static struct cputimer acpi_cputimer = {
64 .next = SLIST_ENTRY_INITIALIZER,
66 .pri = CPUTIMER_PRI_ACPI,
67 .type = CPUTIMER_ACPI,
68 .count = acpi_timer_get_timecount_safe,
69 .fromhz = cputimer_default_fromhz,
70 .fromus = cputimer_default_fromus,
71 .construct = acpi_timer_construct,
72 .destruct = cputimer_default_destruct,
73 .freq = ACPI_PM_TIMER_FREQUENCY
76 static int acpi_timer_identify(driver_t *driver, device_t parent);
77 static int acpi_timer_probe(device_t dev);
78 static int acpi_timer_attach(device_t dev);
79 static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
81 static int acpi_timer_test(void);
83 static device_method_t acpi_timer_methods[] = {
84 DEVMETHOD(device_identify, acpi_timer_identify),
85 DEVMETHOD(device_probe, acpi_timer_probe),
86 DEVMETHOD(device_attach, acpi_timer_attach),
91 static driver_t acpi_timer_driver = {
95 .gpri = KOBJ_GPRI_ACPI+2
98 static devclass_t acpi_timer_devclass;
99 DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, NULL, NULL);
100 MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
103 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
107 acpi_timer_identify(driver_t *driver, device_t parent)
112 * Just try once, do nothing if the 'acpi' bus is rescanned.
114 if (device_get_state(parent) == DS_ATTACHED)
117 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
119 if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) ||
123 if ((dev = BUS_ADD_CHILD(parent, parent, 0, "acpi_timer", 0)) == NULL) {
124 device_printf(parent, "could not add acpi_timer0\n");
127 acpi_timer_dev = dev;
133 acpi_timer_probe(device_t dev)
135 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
137 if (dev != acpi_timer_dev)
140 if (ACPI_FAILURE(AcpiGetTimerResolution(&acpi_timer_resolution)))
147 acpi_timer_attach(device_t dev)
152 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
155 * If all tests of the counter succeed, use the ACPI-fast method. If
156 * at least one failed, default to using the safe routine, which reads
157 * the timer multiple times to get a consistent value before returning.
160 for (i = 0; i < 10; i++)
161 j += acpi_timer_test();
163 if (acpi_timer_resolution == 32) {
164 acpi_cputimer.name = "ACPI-fast";
165 acpi_cputimer.count = acpi_timer_get_timecount;
167 acpi_cputimer.name = "ACPI-fast24";
168 acpi_cputimer.count = acpi_timer_get_timecount24;
171 if (acpi_timer_resolution == 32)
172 acpi_cputimer.name = "ACPI-safe";
174 acpi_cputimer.name = "ACPI-safe24";
175 acpi_cputimer.count = acpi_timer_get_timecount_safe;
178 ksprintf(desc, "%u-bit timer at 3.579545MHz", acpi_timer_resolution);
179 device_set_desc_copy(dev, desc);
181 cputimer_register(&acpi_cputimer);
182 cputimer_select(&acpi_cputimer, 0);
188 * Construct the timer. Adjust the base so the system clock does not
192 acpi_timer_construct(struct cputimer *timer, sysclock_t oldclock)
195 timer->base = oldclock - acpi_timer_get_timecount_safe();
199 * Fetch current time value from reliable hardware.
201 * The cputimer interface requires a 32 bit return value. If the ACPI timer
202 * is only 24 bits then we have to keep track of the upper 8 bits on our
205 * per-cpu tracking fields can cause problems on VMs if one or more cpus
206 * stalls long-enough for the timer to turn-over twice, so instead optimize
207 * the locking case by not updating acpi_cputimer.base until the timer
208 * has gone more than 1/16 its full range.
210 * These are horrible hacks, but at least the SMP interference is minimal
211 * with them. Note that just reading the ACPI timer itself represents a
212 * bottleneck due to the slow I/O.
215 acpi_timer_get_timecount24(void)
218 sysclock_t last_counter;
223 * Range beyond which we force clock_lock. Make it occur more
224 * often on one cpu than on the others to avoid nearly all races.
226 if (mycpu->gd_cpuid == 1)
227 admit = (ssysclock_t)(0x01000000 / 32);
229 admit = (ssysclock_t)(0x01000000 / 16);
232 * 24-bit timer, shortcut
234 last_counter = acpi_cputimer.base;
236 AcpiGetTimer(&counter);
237 delta = (ssysclock_t)(counter - (last_counter & 0x00FFFFFF));
238 if (delta > -admit && delta < admit)
239 return ((last_counter & 0xFF000000) + counter);
242 * 24-bit timer, the hard way
245 last_counter = acpi_cputimer.base;
246 AcpiGetTimer(&counter);
247 if (counter < (last_counter & 0x00FFFFFF))
248 last_counter += 0x01000000;
249 last_counter = (last_counter & 0xFF000000) + counter;
251 acpi_cputimer.base = last_counter;
254 return (last_counter);
258 acpi_timer_get_timecount(void)
262 AcpiGetTimer(&counter);
263 return (counter + acpi_cputimer.base);
267 * Fetch current time value from hardware that may not correctly
268 * latch the counter. We need to read until we have three monotonic
269 * samples and then use the middle one, otherwise we are not protected
270 * against the fact that the bits can be wrong in two directions. If
271 * we only cared about monosity, two reads would be enough.
273 static __inline sysclock_t
274 _acpi_timer_get_timecount_safe(void)
284 } while (u1 > u2 || u2 > u3);
290 acpi_timer_get_timecount_safe(void)
293 sysclock_t last_counter;
298 * Range beyond which we force clock_lock. Make it occur more
299 * often on one cpu than on the others to avoid nearly all races.
301 if (mycpu->gd_cpuid == 1)
302 admit = (ssysclock_t)(0x01000000 / 32);
304 admit = (ssysclock_t)(0x01000000 / 16);
307 * 32-bit timer is easy
309 if (acpi_timer_resolution == 32)
310 return _acpi_timer_get_timecount_safe();
313 * 24-bit timer, shortcut
315 last_counter = acpi_cputimer.base;
317 counter = _acpi_timer_get_timecount_safe();
318 delta = (ssysclock_t)(counter - (last_counter & 0x00FFFFFF));
319 if (delta > -admit && delta < admit)
320 return ((last_counter & 0xFF000000) + counter);
323 * 24-bit timer, the hard way
326 last_counter = acpi_cputimer.base;
327 counter = _acpi_timer_get_timecount_safe();
328 if (counter < (last_counter & 0x00FFFFFF))
329 last_counter += 0x01000000;
330 last_counter = (last_counter & 0xFF000000) + counter;
332 acpi_cputimer.base = last_counter;
335 return (last_counter);
339 * Timecounter freqency adjustment interface.
342 acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
347 if (acpi_cputimer.freq == 0)
349 freq = acpi_cputimer.freq;
350 error = sysctl_handle_int(oidp, &freq, 0, req);
351 if (error == 0 && req->newptr != NULL)
352 cputimer_set_frequency(&acpi_cputimer, freq);
357 SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
358 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "ACPI timer frequency");
361 * Some ACPI timers are known or believed to suffer from implementation
362 * problems which can lead to erroneous values being read. This function
363 * tests for consistent results from the timer and returns 1 if it believes
364 * the timer is consistent, otherwise it returns 0.
366 * It appears the cause is that the counter is not latched to the PCI bus
369 * ] 20. ACPI Timer Errata
371 * ] Problem: The power management timer may return improper result when
372 * ] read. Although the timer value settles properly after incrementing,
373 * ] while incrementing there is a 3nS window every 69.8nS where the
374 * ] timer value is indeterminate (a 4.2% chance that the data will be
375 * ] incorrect when read). As a result, the ACPI free running count up
376 * ] timer specification is violated due to erroneous reads. Implication:
377 * ] System hangs due to the "inaccuracy" of the timer when used by
378 * ] software for time critical events and delays.
380 * ] Workaround: Read the register twice and compare.
381 * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
386 acpi_timer_test(void)
389 int min, max, max2, n, delta;
395 /* Test the timer with interrupts disabled to get accurate results. */
396 #if defined(__x86_64__)
399 #error "no read_*flags"
403 for (n = 0; n < 2000; n++) {
405 delta = acpi_TimerDelta(this, last);
409 } else if (delta > max2) {
416 /* cpu_enable_intr(); restored to original by write_rflags() */
417 #if defined(__x86_64__)
420 #error "no read_*flags"
424 if ((max - min > 8 || delta > 3) && vmm_guest == VMM_GUEST_NONE)
426 else if (min < 0 || max == 0 || max2 == 0)
431 kprintf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
433 min, max, max - min);