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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/sysctl.h>
38 #include <sys/timetc.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
47 #include <dev/acpica/acpivar.h>
48 #include <dev/pci/pcivar.h>
51 * A timecounter based on the free-running ACPI timer.
53 * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
56 /* Hooks for the ACPI CA debugging infrastructure */
57 #define _COMPONENT ACPI_TIMER
58 ACPI_MODULE_NAME("TIMER")
60 static device_t acpi_timer_dev;
61 static struct resource *acpi_timer_reg;
62 static bus_space_handle_t acpi_timer_bsh;
63 static bus_space_tag_t acpi_timer_bst;
64 static eventhandler_tag acpi_timer_eh;
66 static u_int acpi_timer_frequency = 14318182 / 4;
68 static void acpi_timer_identify(driver_t *driver, device_t parent);
69 static int acpi_timer_probe(device_t dev);
70 static int acpi_timer_attach(device_t dev);
71 static void acpi_timer_resume_handler(struct timecounter *);
72 static void acpi_timer_suspend_handler(struct timecounter *);
73 static u_int acpi_timer_get_timecount(struct timecounter *tc);
74 static u_int acpi_timer_get_timecount_safe(struct timecounter *tc);
75 static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
76 static void acpi_timer_boot_test(void);
78 static int acpi_timer_test(void);
80 static device_method_t acpi_timer_methods[] = {
81 DEVMETHOD(device_identify, acpi_timer_identify),
82 DEVMETHOD(device_probe, acpi_timer_probe),
83 DEVMETHOD(device_attach, acpi_timer_attach),
88 static driver_t acpi_timer_driver = {
94 static devclass_t acpi_timer_devclass;
95 DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
96 MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
98 static struct timecounter acpi_timer_timecounter = {
99 acpi_timer_get_timecount_safe, /* get_timecount function */
101 0, /* no default counter_mask */
102 0, /* no default frequency */
104 -1 /* quality (chosen later) */
107 static __inline uint32_t
108 acpi_timer_read(void)
111 return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0));
115 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
119 acpi_timer_identify(driver_t *driver, device_t parent)
125 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
127 if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) ||
131 if ((dev = BUS_ADD_CHILD(parent, 2, "acpi_timer", 0)) == NULL) {
132 device_printf(parent, "could not add acpi_timer0\n");
135 acpi_timer_dev = dev;
137 switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
138 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
139 rtype = SYS_RES_MEMORY;
141 case ACPI_ADR_SPACE_SYSTEM_IO:
142 rtype = SYS_RES_IOPORT;
148 rlen = AcpiGbl_FADT.PmTimerLength;
149 rstart = AcpiGbl_FADT.XPmTimerBlock.Address;
150 if (bus_set_resource(dev, rtype, rid, rstart, rlen))
151 device_printf(dev, "couldn't set resource (%s 0x%lx+0x%lx)\n",
152 (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen);
157 acpi_timer_probe(device_t dev)
160 int i, j, rid, rtype;
162 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
164 if (dev != acpi_timer_dev)
167 switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
168 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
169 rtype = SYS_RES_MEMORY;
171 case ACPI_ADR_SPACE_SYSTEM_IO:
172 rtype = SYS_RES_IOPORT;
178 acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
179 if (acpi_timer_reg == NULL) {
180 device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n",
181 (rtype == SYS_RES_IOPORT) ? "port" : "mem",
182 (u_long)AcpiGbl_FADT.XPmTimerBlock.Address);
185 acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
186 acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
187 if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER)
188 acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
190 acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
191 acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
192 acpi_timer_timecounter.tc_flags = TC_FLAGS_SUSPEND_SAFE;
193 if (testenv("debug.acpi.timer_test"))
194 acpi_timer_boot_test();
197 * If all tests of the counter succeed, use the ACPI-fast method. If
198 * at least one failed, default to using the safe routine, which reads
199 * the timer multiple times to get a consistent value before returning.
203 printf("ACPI timer:");
204 for (i = 0; i < 10; i++)
205 j += acpi_timer_test();
207 printf(" -> %d\n", j);
209 acpi_timer_timecounter.tc_name = "ACPI-fast";
210 acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
211 acpi_timer_timecounter.tc_quality = 900;
213 acpi_timer_timecounter.tc_name = "ACPI-safe";
214 acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
215 acpi_timer_timecounter.tc_quality = 850;
217 tc_init(&acpi_timer_timecounter);
219 sprintf(desc, "%d-bit timer at %u.%06uMHz",
220 (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) != 0 ? 32 : 24,
221 acpi_timer_frequency / 1000000, acpi_timer_frequency % 1000000);
222 device_set_desc_copy(dev, desc);
224 /* Release the resource, we'll allocate it again during attach. */
225 bus_release_resource(dev, rtype, rid, acpi_timer_reg);
230 acpi_timer_attach(device_t dev)
234 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
236 switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
237 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
238 rtype = SYS_RES_MEMORY;
240 case ACPI_ADR_SPACE_SYSTEM_IO:
241 rtype = SYS_RES_IOPORT;
247 acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
248 if (acpi_timer_reg == NULL)
250 acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
251 acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
253 /* Register suspend event handler. */
254 if (EVENTHANDLER_REGISTER(power_suspend, acpi_timer_suspend_handler,
255 &acpi_timer_timecounter, EVENTHANDLER_PRI_LAST) == NULL)
256 device_printf(dev, "failed to register suspend event handler\n");
262 acpi_timer_resume_handler(struct timecounter *newtc)
264 struct timecounter *tc;
269 device_printf(acpi_timer_dev,
270 "restoring timecounter, %s -> %s\n",
271 tc->tc_name, newtc->tc_name);
272 (void)newtc->tc_get_timecount(newtc);
273 (void)newtc->tc_get_timecount(newtc);
279 acpi_timer_suspend_handler(struct timecounter *newtc)
281 struct timecounter *tc;
283 /* Deregister existing resume event handler. */
284 if (acpi_timer_eh != NULL) {
285 EVENTHANDLER_DEREGISTER(power_resume, acpi_timer_eh);
286 acpi_timer_eh = NULL;
289 if ((timecounter->tc_flags & TC_FLAGS_SUSPEND_SAFE) != 0) {
291 * If we are using a suspend safe timecounter, don't
292 * save/restore it across suspend/resume.
297 KASSERT(newtc == &acpi_timer_timecounter,
298 ("acpi_timer_suspend_handler: wrong timecounter"));
303 device_printf(acpi_timer_dev,
304 "switching timecounter, %s -> %s\n",
305 tc->tc_name, newtc->tc_name);
306 (void)acpi_timer_read();
307 (void)acpi_timer_read();
309 acpi_timer_eh = EVENTHANDLER_REGISTER(power_resume,
310 acpi_timer_resume_handler, tc, EVENTHANDLER_PRI_LAST);
315 * Fetch current time value from reliable hardware.
318 acpi_timer_get_timecount(struct timecounter *tc)
320 return (acpi_timer_read());
324 * Fetch current time value from hardware that may not correctly
325 * latch the counter. We need to read until we have three monotonic
326 * samples and then use the middle one, otherwise we are not protected
327 * against the fact that the bits can be wrong in two directions. If
328 * we only cared about monosity, two reads would be enough.
331 acpi_timer_get_timecount_safe(struct timecounter *tc)
335 u2 = acpi_timer_read();
336 u3 = acpi_timer_read();
340 u3 = acpi_timer_read();
341 } while (u1 > u2 || u2 > u3);
347 * Timecounter freqency adjustment interface.
350 acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
355 if (acpi_timer_timecounter.tc_frequency == 0)
357 freq = acpi_timer_frequency;
358 error = sysctl_handle_int(oidp, &freq, 0, req);
359 if (error == 0 && req->newptr != NULL) {
360 acpi_timer_frequency = freq;
361 acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
367 SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
368 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "ACPI timer frequency");
371 * Some ACPI timers are known or believed to suffer from implementation
372 * problems which can lead to erroneous values being read. This function
373 * tests for consistent results from the timer and returns 1 if it believes
374 * the timer is consistent, otherwise it returns 0.
376 * It appears the cause is that the counter is not latched to the PCI bus
379 * ] 20. ACPI Timer Errata
381 * ] Problem: The power management timer may return improper result when
382 * ] read. Although the timer value settles properly after incrementing,
383 * ] while incrementing there is a 3nS window every 69.8nS where the
384 * ] timer value is indeterminate (a 4.2% chance that the data will be
385 * ] incorrect when read). As a result, the ACPI free running count up
386 * ] timer specification is violated due to erroneous reads. Implication:
387 * ] System hangs due to the "inaccuracy" of the timer when used by
388 * ] software for time critical events and delays.
390 * ] Workaround: Read the register twice and compare.
391 * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
399 int delta, max, max2, min, n;
405 /* Test the timer with interrupts disabled to get accurate results. */
407 last = acpi_timer_read();
408 for (n = 0; n < N; n++) {
409 this = acpi_timer_read();
410 delta = acpi_TimerDelta(this, last);
414 } else if (delta > max2)
423 if ((max - min > 8 || delta > 3) && vm_guest == VM_GUEST_NO)
425 else if (min < 0 || max == 0 || max2 == 0)
430 printf(" %d/%d", n, delta);
437 * Test harness for verifying ACPI timer behaviour.
438 * Boot with debug.acpi.timer_test set to invoke this.
441 acpi_timer_boot_test(void)
445 u1 = acpi_timer_read();
446 u2 = acpi_timer_read();
447 u3 = acpi_timer_read();
449 device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
452 * The failure case is where u3 > u1, but u2 does not fall between
453 * the two, ie. it contains garbage.
456 if (u2 < u1 || u2 > u3)
457 device_printf(acpi_timer_dev,
458 "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
463 u3 = acpi_timer_read();