MFC numerous features from HEAD.
[dragonfly.git] / sys / dev / acpica5 / acpi_timer.c
blob380de8f90e51814d181ed28ec6123ec3c531c3b4
1 /*-
2 * Copyright (c) 2000, 2001 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * All rights reserved.
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.
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.
27 * $FreeBSD: src/sys/dev/acpica/acpi_timer.c,v 1.35 2004/07/22 05:42:14 njl Exp $
28 * $DragonFly: src/sys/dev/acpica5/acpi_timer.c,v 1.13 2007/10/09 09:46:56 y0netan1 Exp $
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/sysctl.h>
36 #include <sys/systimer.h>
37 #include <sys/rman.h>
39 #include <machine/lock.h>
40 #include <bus/pci/pcivar.h>
42 #include "acpi.h"
43 #include "acpivar.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 ACPI CA debugging infrastructure */
52 #define _COMPONENT ACPI_TIMER
53 ACPI_MODULE_NAME("TIMER")
55 static device_t acpi_timer_dev;
56 static struct resource *acpi_timer_reg;
57 static bus_space_handle_t acpi_timer_bsh;
58 static bus_space_tag_t acpi_timer_bst;
59 static sysclock_t acpi_counter_mask;
60 static sysclock_t acpi_last_counter;
62 #define ACPI_TIMER_FREQ (14318182 / 4)
64 static sysclock_t acpi_timer_get_timecount(void);
65 static sysclock_t acpi_timer_get_timecount24(void);
66 static sysclock_t acpi_timer_get_timecount_safe(void);
67 static void acpi_timer_construct(struct cputimer *timer, sysclock_t oldclock);
69 static struct cputimer acpi_cputimer = {
70 SLIST_ENTRY_INITIALIZER,
71 "ACPI",
72 CPUTIMER_PRI_ACPI,
73 CPUTIMER_ACPI,
74 acpi_timer_get_timecount_safe,
75 cputimer_default_fromhz,
76 cputimer_default_fromus,
77 acpi_timer_construct,
78 cputimer_default_destruct,
79 ACPI_TIMER_FREQ,
80 0, 0, 0
83 static int acpi_timer_identify(driver_t *driver, device_t parent);
84 static int acpi_timer_probe(device_t dev);
85 static int acpi_timer_attach(device_t dev);
86 static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
88 static u_int acpi_timer_read(void);
89 static int acpi_timer_test(void);
91 static device_method_t acpi_timer_methods[] = {
92 DEVMETHOD(device_identify, acpi_timer_identify),
93 DEVMETHOD(device_probe, acpi_timer_probe),
94 DEVMETHOD(device_attach, acpi_timer_attach),
96 {0, 0}
99 static driver_t acpi_timer_driver = {
100 "acpi_timer",
101 acpi_timer_methods,
105 static devclass_t acpi_timer_devclass;
106 DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
107 MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
109 static u_int
110 acpi_timer_read(void)
112 return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0));
116 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
117 * we will be using.
119 static int
120 acpi_timer_identify(driver_t *driver, device_t parent)
122 device_t dev;
123 u_long rlen, rstart;
124 int rid, rtype;
127 * Just try once, do nothing if the 'acpi' bus is rescanned.
129 if (device_get_state(parent) == DS_ATTACHED)
130 return (0);
132 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
134 if (acpi_disabled("timer") || acpi_timer_dev)
135 return (ENXIO);
137 if ((dev = BUS_ADD_CHILD(parent, parent, 0, "acpi_timer", 0)) == NULL) {
138 device_printf(parent, "could not add acpi_timer0\n");
139 return (ENXIO);
141 acpi_timer_dev = dev;
143 rid = 0;
144 rtype = AcpiGbl_FADT.XPmTimerBlock.SpaceId ?
145 SYS_RES_IOPORT : SYS_RES_MEMORY;
146 rlen = AcpiGbl_FADT.PmTimerLength;
147 rstart = AcpiGbl_FADT.XPmTimerBlock.Address;
148 if (bus_set_resource(dev, rtype, rid, rstart, rlen)) {
149 device_printf(dev, "couldn't set resource (%s 0x%lx+0x%lx)\n",
150 (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen);
151 return (ENXIO);
153 return (0);
156 static int
157 acpi_timer_probe(device_t dev)
159 char desc[40];
160 int i, j, rid, rtype;
162 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
164 if (dev != acpi_timer_dev)
165 return (ENXIO);
167 rid = 0;
168 rtype = AcpiGbl_FADT.XPmTimerBlock.SpaceId ?
169 SYS_RES_IOPORT : SYS_RES_MEMORY;
170 acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
171 if (acpi_timer_reg == NULL) {
172 device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n",
173 (rtype == SYS_RES_IOPORT) ? "port" : "mem",
174 (u_long)AcpiGbl_FADT.XPmTimerBlock.Address);
175 return (ENXIO);
177 acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
178 acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
179 if ((AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) != 0)
180 acpi_counter_mask = 0xffffffff;
181 else
182 acpi_counter_mask = 0x00ffffff;
185 * If all tests of the counter succeed, use the ACPI-fast method. If
186 * at least one failed, default to using the safe routine, which reads
187 * the timer multiple times to get a consistent value before returning.
189 j = 0;
190 for (i = 0; i < 10; i++)
191 j += acpi_timer_test();
192 if (j == 10) {
193 if (acpi_counter_mask == 0xffffffff) {
194 acpi_cputimer.name = "ACPI-fast";
195 acpi_cputimer.count = acpi_timer_get_timecount;
196 } else {
197 acpi_cputimer.name = "ACPI-fast24";
198 acpi_cputimer.count = acpi_timer_get_timecount24;
200 } else {
201 acpi_cputimer.name = "ACPI-safe";
202 acpi_cputimer.count = acpi_timer_get_timecount_safe;
205 ksprintf(desc, "%d-bit timer at 3.579545MHz",
206 (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) ? 32 : 24);
207 device_set_desc_copy(dev, desc);
209 cputimer_register(&acpi_cputimer);
210 cputimer_select(&acpi_cputimer, 0);
211 /* Release the resource, we'll allocate it again during attach. */
212 bus_release_resource(dev, rtype, rid, acpi_timer_reg);
213 return (0);
216 static int
217 acpi_timer_attach(device_t dev)
219 int rid, rtype;
221 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
223 rid = 0;
224 rtype = AcpiGbl_FADT.XPmTimerBlock.SpaceId ?
225 SYS_RES_IOPORT : SYS_RES_MEMORY;
226 acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
227 if (acpi_timer_reg == NULL)
228 return (ENXIO);
229 acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
230 acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
231 return (0);
235 * Construct the timer. Adjust the base so the system clock does not
236 * jump weirdly.
238 static void
239 acpi_timer_construct(struct cputimer *timer, sysclock_t oldclock)
241 timer->base = 0;
242 timer->base = oldclock - acpi_timer_get_timecount_safe();
246 * Fetch current time value from reliable hardware.
248 * The cputimer interface requires a 32 bit return value. If the ACPI timer
249 * is only 24 bits then we have to keep track of the upper 8 bits on our
250 * own.
252 * XXX we could probably get away with using a per-cpu field for this and
253 * just use interrupt disablement instead of clock_lock.
255 static sysclock_t
256 acpi_timer_get_timecount24(void)
258 sysclock_t counter;
260 clock_lock();
261 counter = acpi_timer_read();
262 if (counter < acpi_last_counter)
263 acpi_cputimer.base += 0x01000000;
264 acpi_last_counter = counter;
265 counter += acpi_cputimer.base;
266 clock_unlock();
267 return (counter);
270 static sysclock_t
271 acpi_timer_get_timecount(void)
273 return (acpi_timer_read() + acpi_cputimer.base);
277 * Fetch current time value from hardware that may not correctly
278 * latch the counter. We need to read until we have three monotonic
279 * samples and then use the middle one, otherwise we are not protected
280 * against the fact that the bits can be wrong in two directions. If
281 * we only cared about monosity, two reads would be enough.
283 static sysclock_t
284 acpi_timer_get_timecount_safe(void)
286 u_int u1, u2, u3;
288 if (acpi_counter_mask != 0xffffffff)
289 clock_lock();
291 u2 = acpi_timer_read();
292 u3 = acpi_timer_read();
293 do {
294 u1 = u2;
295 u2 = u3;
296 u3 = acpi_timer_read();
297 } while (u1 > u2 || u2 > u3);
299 if (acpi_counter_mask != 0xffffffff) {
300 if (u2 < acpi_last_counter)
301 acpi_cputimer.base += 0x01000000;
302 acpi_last_counter = u2;
303 clock_unlock();
305 return (u2 + acpi_cputimer.base);
309 * Timecounter freqency adjustment interface.
311 static int
312 acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
314 int error;
315 u_int freq;
317 if (acpi_cputimer.freq == 0)
318 return (EOPNOTSUPP);
319 freq = acpi_cputimer.freq;
320 error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
321 if (error == 0 && req->newptr != NULL)
322 cputimer_set_frequency(&acpi_cputimer, freq);
324 return (error);
327 SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
328 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "");
331 * Some ACPI timers are known or believed to suffer from implementation
332 * problems which can lead to erroneous values being read. This function
333 * tests for consistent results from the timer and returns 1 if it believes
334 * the timer is consistent, otherwise it returns 0.
336 * It appears the cause is that the counter is not latched to the PCI bus
337 * clock when read:
339 * ] 20. ACPI Timer Errata
341 * ] Problem: The power management timer may return improper result when
342 * ] read. Although the timer value settles properly after incrementing,
343 * ] while incrementing there is a 3nS window every 69.8nS where the
344 * ] timer value is indeterminate (a 4.2% chance that the data will be
345 * ] incorrect when read). As a result, the ACPI free running count up
346 * ] timer specification is violated due to erroneous reads. Implication:
347 * ] System hangs due to the "inaccuracy" of the timer when used by
348 * ] software for time critical events and delays.
350 * ] Workaround: Read the register twice and compare.
351 * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
352 * ] in the PIIX4M.
355 static int
356 acpi_timer_test(void)
358 uint32_t last, this;
359 int min, max, n, delta;
360 register_t s;
362 min = 10000000;
363 max = 0;
365 /* Test the timer with interrupts disabled to get accurate results. */
366 s = read_eflags();
367 cpu_disable_intr();
368 last = acpi_timer_read();
369 for (n = 0; n < 2000; n++) {
370 this = acpi_timer_read();
371 delta = acpi_TimerDelta(this, last);
372 if (delta > max)
373 max = delta;
374 else if (delta < min)
375 min = delta;
376 last = this;
378 write_eflags(s);
380 if (max - min > 2)
381 n = 0;
382 else if (min < 0 || max == 0)
383 n = 0;
384 else
385 n = 1;
386 if (bootverbose) {
387 kprintf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
388 n ? "GOOD" : "BAD ",
389 min, max, max - min);
392 return (n);