xref: /freebsd/sys/dev/acpica/acpi_timer.c (revision ca5f1efdd9403258db867e40cc32ae6c434318ea)
115e32d5dSMike Smith /*-
2787fa5b8SMike Smith  * Copyright (c) 2000, 2001 Michael Smith
315e32d5dSMike Smith  * Copyright (c) 2000 BSDi
415e32d5dSMike Smith  * All rights reserved.
515e32d5dSMike Smith  *
615e32d5dSMike Smith  * Redistribution and use in source and binary forms, with or without
715e32d5dSMike Smith  * modification, are permitted provided that the following conditions
815e32d5dSMike Smith  * are met:
915e32d5dSMike Smith  * 1. Redistributions of source code must retain the above copyright
1015e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer.
1115e32d5dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1215e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
1315e32d5dSMike Smith  *    documentation and/or other materials provided with the distribution.
1415e32d5dSMike Smith  *
1515e32d5dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1615e32d5dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1715e32d5dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1815e32d5dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1915e32d5dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2015e32d5dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2115e32d5dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2215e32d5dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2315e32d5dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2415e32d5dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2515e32d5dSMike Smith  * SUCH DAMAGE.
2615e32d5dSMike Smith  */
27dad97feeSDavid E. O'Brien 
28dad97feeSDavid E. O'Brien #include <sys/cdefs.h>
29dad97feeSDavid E. O'Brien __FBSDID("$FreeBSD$");
30dad97feeSDavid E. O'Brien 
3115e32d5dSMike Smith #include "opt_acpi.h"
3215e32d5dSMike Smith #include <sys/param.h>
3315e32d5dSMike Smith #include <sys/bus.h>
34787fa5b8SMike Smith #include <sys/kernel.h>
35fe12f24bSPoul-Henning Kamp #include <sys/module.h>
36787fa5b8SMike Smith #include <sys/sysctl.h>
37787fa5b8SMike Smith #include <sys/timetc.h>
38787fa5b8SMike Smith 
39787fa5b8SMike Smith #include <machine/bus.h>
40787fa5b8SMike Smith #include <machine/resource.h>
41787fa5b8SMike Smith #include <sys/rman.h>
4215e32d5dSMike Smith 
43129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
44129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
45129d3046SJung-uk Kim 
4687e5d361SJohn Baldwin #include <dev/acpica/acpivar.h>
47cace7a2aSWarner Losh #include <dev/pci/pcivar.h>
48787fa5b8SMike Smith 
49787fa5b8SMike Smith /*
50787fa5b8SMike Smith  * A timecounter based on the free-running ACPI timer.
51787fa5b8SMike Smith  *
52787fa5b8SMike Smith  * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
53787fa5b8SMike Smith  */
5415e32d5dSMike Smith 
55be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
563184cf5aSNate Lawson #define _COMPONENT	ACPI_TIMER
5772e5754cSMike Smith ACPI_MODULE_NAME("TIMER")
580ae55423SMike Smith 
59787fa5b8SMike Smith static device_t			acpi_timer_dev;
60af807c0fSNate Lawson static struct resource		*acpi_timer_reg;
61d9b6df60SNate Lawson static bus_space_handle_t	acpi_timer_bsh;
62d9b6df60SNate Lawson static bus_space_tag_t		acpi_timer_bst;
6315e32d5dSMike Smith 
64787fa5b8SMike Smith static u_int	acpi_timer_frequency = 14318182 / 4;
6515e32d5dSMike Smith 
6615e32d5dSMike Smith static void	acpi_timer_identify(driver_t *driver, device_t parent);
6715e32d5dSMike Smith static int	acpi_timer_probe(device_t dev);
6815e32d5dSMike Smith static int	acpi_timer_attach(device_t dev);
6975988358SNate Lawson static u_int	acpi_timer_get_timecount(struct timecounter *tc);
7075988358SNate Lawson static u_int	acpi_timer_get_timecount_safe(struct timecounter *tc);
71787fa5b8SMike Smith static int	acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
72d9b6df60SNate Lawson static void	acpi_timer_boot_test(void);
7315e32d5dSMike Smith 
74d9b6df60SNate Lawson static int	acpi_timer_test(void);
75214475d8SMarcel Moolenaar 
7615e32d5dSMike Smith static device_method_t acpi_timer_methods[] = {
7715e32d5dSMike Smith     DEVMETHOD(device_identify,	acpi_timer_identify),
7815e32d5dSMike Smith     DEVMETHOD(device_probe,	acpi_timer_probe),
7915e32d5dSMike Smith     DEVMETHOD(device_attach,	acpi_timer_attach),
8015e32d5dSMike Smith 
8115e32d5dSMike Smith     {0, 0}
8215e32d5dSMike Smith };
8315e32d5dSMike Smith 
8415e32d5dSMike Smith static driver_t acpi_timer_driver = {
8515e32d5dSMike Smith     "acpi_timer",
8615e32d5dSMike Smith     acpi_timer_methods,
87787fa5b8SMike Smith     0,
8815e32d5dSMike Smith };
8915e32d5dSMike Smith 
903273b005SMike Smith static devclass_t acpi_timer_devclass;
9115e32d5dSMike Smith DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
9264278df5SNate Lawson MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
9315e32d5dSMike Smith 
94787fa5b8SMike Smith static struct timecounter acpi_timer_timecounter = {
95d9b6df60SNate Lawson 	acpi_timer_get_timecount_safe,	/* get_timecount function */
96d9b6df60SNate Lawson 	0,				/* no poll_pps */
97d9b6df60SNate Lawson 	0,				/* no default counter_mask */
98d9b6df60SNate Lawson 	0,				/* no default frequency */
99d9b6df60SNate Lawson 	"ACPI",				/* name */
100430eaa74SNate Lawson 	-1				/* quality (chosen later) */
101787fa5b8SMike Smith };
102787fa5b8SMike Smith 
10393bd1f7eSJung-uk Kim static __inline uint32_t
10493bd1f7eSJung-uk Kim acpi_timer_read(void)
105214475d8SMarcel Moolenaar {
10693bd1f7eSJung-uk Kim 
107c0b9a6deSNate Lawson     return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0));
108214475d8SMarcel Moolenaar }
109cb877d00SPoul-Henning Kamp 
110787fa5b8SMike Smith /*
111787fa5b8SMike Smith  * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
112787fa5b8SMike Smith  * we will be using.
113787fa5b8SMike Smith  */
11415e32d5dSMike Smith static void
11515e32d5dSMike Smith acpi_timer_identify(driver_t *driver, device_t parent)
11615e32d5dSMike Smith {
11715e32d5dSMike Smith     device_t dev;
118214475d8SMarcel Moolenaar     u_long rlen, rstart;
119be1841b4SNate Lawson     int rid, rtype;
12015e32d5dSMike Smith 
121b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1220ae55423SMike Smith 
1234f8c4e4dSNate Lawson     if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) ||
1242be4e471SJung-uk Kim 	acpi_timer_dev)
1250ae55423SMike Smith 	return_VOID;
12615e32d5dSMike Smith 
12715e32d5dSMike Smith     if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) {
12815e32d5dSMike Smith 	device_printf(parent, "could not add acpi_timer0\n");
1290ae55423SMike Smith 	return_VOID;
13015e32d5dSMike Smith     }
131787fa5b8SMike Smith     acpi_timer_dev = dev;
132214475d8SMarcel Moolenaar 
1336af444b1SJung-uk Kim     switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
1346af444b1SJung-uk Kim     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1356af444b1SJung-uk Kim 	rtype = SYS_RES_MEMORY;
1366af444b1SJung-uk Kim 	break;
1376af444b1SJung-uk Kim     case ACPI_ADR_SPACE_SYSTEM_IO:
1386af444b1SJung-uk Kim 	rtype = SYS_RES_IOPORT;
1396af444b1SJung-uk Kim 	break;
1406af444b1SJung-uk Kim     default:
1416af444b1SJung-uk Kim 	return_VOID;
1426af444b1SJung-uk Kim     }
143787fa5b8SMike Smith     rid = 0;
1442be4e471SJung-uk Kim     rlen = AcpiGbl_FADT.PmTimerLength;
1452be4e471SJung-uk Kim     rstart = AcpiGbl_FADT.XPmTimerBlock.Address;
146be1841b4SNate Lawson     if (bus_set_resource(dev, rtype, rid, rstart, rlen))
147be1841b4SNate Lawson 	device_printf(dev, "couldn't set resource (%s 0x%lx+0x%lx)\n",
148be1841b4SNate Lawson 	    (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen);
1490ae55423SMike Smith     return_VOID;
15015e32d5dSMike Smith }
151be1841b4SNate Lawson 
152be1841b4SNate Lawson static int
153be1841b4SNate Lawson acpi_timer_probe(device_t dev)
154be1841b4SNate Lawson {
155be1841b4SNate Lawson     char desc[40];
156be1841b4SNate Lawson     int i, j, rid, rtype;
157be1841b4SNate Lawson 
158be1841b4SNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
159be1841b4SNate Lawson 
160be1841b4SNate Lawson     if (dev != acpi_timer_dev)
161be1841b4SNate Lawson 	return (ENXIO);
162be1841b4SNate Lawson 
1636af444b1SJung-uk Kim     switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
1646af444b1SJung-uk Kim     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1656af444b1SJung-uk Kim 	rtype = SYS_RES_MEMORY;
1666af444b1SJung-uk Kim 	break;
1676af444b1SJung-uk Kim     case ACPI_ADR_SPACE_SYSTEM_IO:
1686af444b1SJung-uk Kim 	rtype = SYS_RES_IOPORT;
1696af444b1SJung-uk Kim 	break;
1706af444b1SJung-uk Kim     default:
1716af444b1SJung-uk Kim 	return (ENXIO);
1726af444b1SJung-uk Kim     }
173be1841b4SNate Lawson     rid = 0;
174be1841b4SNate Lawson     acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
175be1841b4SNate Lawson     if (acpi_timer_reg == NULL) {
176be1841b4SNate Lawson 	device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n",
177be1841b4SNate Lawson 	    (rtype == SYS_RES_IOPORT) ? "port" : "mem",
1782be4e471SJung-uk Kim 	    (u_long)AcpiGbl_FADT.XPmTimerBlock.Address);
179be1841b4SNate Lawson 	return (ENXIO);
180be1841b4SNate Lawson     }
18114827d7eSNate Lawson     acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
18214827d7eSNate Lawson     acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
1832be4e471SJung-uk Kim     if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER)
18475988358SNate Lawson 	acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
18575988358SNate Lawson     else
18675988358SNate Lawson 	acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
18775988358SNate Lawson     acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
188d786139cSMaxime Henrion     if (testenv("debug.acpi.timer_test"))
189d9b6df60SNate Lawson 	acpi_timer_boot_test();
190787fa5b8SMike Smith 
191af807c0fSNate Lawson     /*
192af807c0fSNate Lawson      * If all tests of the counter succeed, use the ACPI-fast method.  If
193af807c0fSNate Lawson      * at least one failed, default to using the safe routine, which reads
194af807c0fSNate Lawson      * the timer multiple times to get a consistent value before returning.
195af807c0fSNate Lawson      */
196cb877d00SPoul-Henning Kamp     j = 0;
1972a921b05SPoul-Henning Kamp     if (bootverbose)
1982a921b05SPoul-Henning Kamp 	printf("ACPI timer:");
199cb877d00SPoul-Henning Kamp     for (i = 0; i < 10; i++)
200d9b6df60SNate Lawson 	j += acpi_timer_test();
2012a921b05SPoul-Henning Kamp     if (bootverbose)
2022a921b05SPoul-Henning Kamp 	printf(" -> %d\n", j);
203cb877d00SPoul-Henning Kamp     if (j == 10) {
204cb877d00SPoul-Henning Kamp 	acpi_timer_timecounter.tc_name = "ACPI-fast";
205cb877d00SPoul-Henning Kamp 	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
206*ca5f1efdSJung-uk Kim 	acpi_timer_timecounter.tc_quality = 900;
207cb877d00SPoul-Henning Kamp     } else {
208cb877d00SPoul-Henning Kamp 	acpi_timer_timecounter.tc_name = "ACPI-safe";
209cb877d00SPoul-Henning Kamp 	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
210430eaa74SNate Lawson 	acpi_timer_timecounter.tc_quality = 850;
211cb877d00SPoul-Henning Kamp     }
212787fa5b8SMike Smith     tc_init(&acpi_timer_timecounter);
21315e32d5dSMike Smith 
2146af444b1SJung-uk Kim     sprintf(desc, "%d-bit timer at %u.%06uMHz",
2156af444b1SJung-uk Kim 	(AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) != 0 ? 32 : 24,
2166af444b1SJung-uk Kim 	acpi_timer_frequency / 1000000, acpi_timer_frequency % 1000000);
21715e32d5dSMike Smith     device_set_desc_copy(dev, desc);
2180ae55423SMike Smith 
219be1841b4SNate Lawson     /* Release the resource, we'll allocate it again during attach. */
220be1841b4SNate Lawson     bus_release_resource(dev, rtype, rid, acpi_timer_reg);
22115e32d5dSMike Smith     return (0);
22215e32d5dSMike Smith }
22315e32d5dSMike Smith 
22415e32d5dSMike Smith static int
22515e32d5dSMike Smith acpi_timer_attach(device_t dev)
22615e32d5dSMike Smith {
227be1841b4SNate Lawson     int rid, rtype;
228be1841b4SNate Lawson 
229be1841b4SNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
230be1841b4SNate Lawson 
2316af444b1SJung-uk Kim     switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
2326af444b1SJung-uk Kim     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
2336af444b1SJung-uk Kim 	rtype = SYS_RES_MEMORY;
2346af444b1SJung-uk Kim 	break;
2356af444b1SJung-uk Kim     case ACPI_ADR_SPACE_SYSTEM_IO:
2366af444b1SJung-uk Kim 	rtype = SYS_RES_IOPORT;
2376af444b1SJung-uk Kim 	break;
2386af444b1SJung-uk Kim     default:
2396af444b1SJung-uk Kim 	return (ENXIO);
2406af444b1SJung-uk Kim     }
241be1841b4SNate Lawson     rid = 0;
242be1841b4SNate Lawson     acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
243be1841b4SNate Lawson     if (acpi_timer_reg == NULL)
244be1841b4SNate Lawson 	return (ENXIO);
245be1841b4SNate Lawson     acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
246be1841b4SNate Lawson     acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
247787fa5b8SMike Smith     return (0);
24815e32d5dSMike Smith }
249787fa5b8SMike Smith 
250787fa5b8SMike Smith /*
251feade919SMike Smith  * Fetch current time value from reliable hardware.
252787fa5b8SMike Smith  */
25375988358SNate Lawson static u_int
254787fa5b8SMike Smith acpi_timer_get_timecount(struct timecounter *tc)
255787fa5b8SMike Smith {
256d9b6df60SNate Lawson     return (acpi_timer_read());
257787fa5b8SMike Smith }
258787fa5b8SMike Smith 
259787fa5b8SMike Smith /*
260feade919SMike Smith  * Fetch current time value from hardware that may not correctly
261af807c0fSNate Lawson  * latch the counter.  We need to read until we have three monotonic
262af807c0fSNate Lawson  * samples and then use the middle one, otherwise we are not protected
263af807c0fSNate Lawson  * against the fact that the bits can be wrong in two directions.  If
264af807c0fSNate Lawson  * we only cared about monosity, two reads would be enough.
265feade919SMike Smith  */
26675988358SNate Lawson static u_int
267feade919SMike Smith acpi_timer_get_timecount_safe(struct timecounter *tc)
268feade919SMike Smith {
26975988358SNate Lawson     u_int u1, u2, u3;
270feade919SMike Smith 
271d9b6df60SNate Lawson     u2 = acpi_timer_read();
272d9b6df60SNate Lawson     u3 = acpi_timer_read();
273feade919SMike Smith     do {
274feade919SMike Smith 	u1 = u2;
275feade919SMike Smith 	u2 = u3;
276d9b6df60SNate Lawson 	u3 = acpi_timer_read();
277d9b6df60SNate Lawson     } while (u1 > u2 || u2 > u3);
278be2b1797SNate Lawson 
279feade919SMike Smith     return (u2);
280feade919SMike Smith }
281feade919SMike Smith 
282feade919SMike Smith /*
283787fa5b8SMike Smith  * Timecounter freqency adjustment interface.
284787fa5b8SMike Smith  */
285787fa5b8SMike Smith static int
286787fa5b8SMike Smith acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
287787fa5b8SMike Smith {
288787fa5b8SMike Smith     int error;
289787fa5b8SMike Smith     u_int freq;
290787fa5b8SMike Smith 
291787fa5b8SMike Smith     if (acpi_timer_timecounter.tc_frequency == 0)
292787fa5b8SMike Smith 	return (EOPNOTSUPP);
293787fa5b8SMike Smith     freq = acpi_timer_frequency;
294041b706bSDavid Malone     error = sysctl_handle_int(oidp, &freq, 0, req);
295787fa5b8SMike Smith     if (error == 0 && req->newptr != NULL) {
296787fa5b8SMike Smith 	acpi_timer_frequency = freq;
297787fa5b8SMike Smith 	acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
298787fa5b8SMike Smith     }
299be2b1797SNate Lawson 
300787fa5b8SMike Smith     return (error);
301787fa5b8SMike Smith }
302787fa5b8SMike Smith 
303787fa5b8SMike Smith SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
3045331d61dSJung-uk Kim     0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "ACPI timer frequency");
305787fa5b8SMike Smith 
306787fa5b8SMike Smith /*
307af807c0fSNate Lawson  * Some ACPI timers are known or believed to suffer from implementation
308af807c0fSNate Lawson  * problems which can lead to erroneous values being read.  This function
309af807c0fSNate Lawson  * tests for consistent results from the timer and returns 1 if it believes
310af807c0fSNate Lawson  * the timer is consistent, otherwise it returns 0.
311af807c0fSNate Lawson  *
312af807c0fSNate Lawson  * It appears the cause is that the counter is not latched to the PCI bus
313af807c0fSNate Lawson  * clock when read:
314af807c0fSNate Lawson  *
315af807c0fSNate Lawson  * ] 20. ACPI Timer Errata
316af807c0fSNate Lawson  * ]
317af807c0fSNate Lawson  * ]   Problem: The power management timer may return improper result when
318af807c0fSNate Lawson  * ]   read. Although the timer value settles properly after incrementing,
319af807c0fSNate Lawson  * ]   while incrementing there is a 3nS window every 69.8nS where the
320af807c0fSNate Lawson  * ]   timer value is indeterminate (a 4.2% chance that the data will be
321af807c0fSNate Lawson  * ]   incorrect when read). As a result, the ACPI free running count up
322af807c0fSNate Lawson  * ]   timer specification is violated due to erroneous reads.  Implication:
323af807c0fSNate Lawson  * ]   System hangs due to the "inaccuracy" of the timer when used by
324af807c0fSNate Lawson  * ]   software for time critical events and delays.
325af807c0fSNate Lawson  * ]
326af807c0fSNate Lawson  * ] Workaround: Read the register twice and compare.
327af807c0fSNate Lawson  * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
328af807c0fSNate Lawson  * ] in the PIIX4M.
329af807c0fSNate Lawson  */
330af807c0fSNate Lawson #define N 2000
331af807c0fSNate Lawson static int
332d9b6df60SNate Lawson acpi_timer_test()
333af807c0fSNate Lawson {
334af807c0fSNate Lawson     uint32_t last, this;
3359abd8cd0SJung-uk Kim     int delta, max, max2, min, n;
336c0b9a6deSNate Lawson     register_t s;
337af807c0fSNate Lawson 
338aa977fc7SJung-uk Kim     min = INT32_MAX;
3399abd8cd0SJung-uk Kim     max = max2 = 0;
340c0b9a6deSNate Lawson 
341c0b9a6deSNate Lawson     /* Test the timer with interrupts disabled to get accurate results. */
342c0b9a6deSNate Lawson     s = intr_disable();
343d9b6df60SNate Lawson     last = acpi_timer_read();
344af807c0fSNate Lawson     for (n = 0; n < N; n++) {
345d9b6df60SNate Lawson 	this = acpi_timer_read();
346af807c0fSNate Lawson 	delta = acpi_TimerDelta(this, last);
3479abd8cd0SJung-uk Kim 	if (delta > max) {
3489abd8cd0SJung-uk Kim 	    max2 = max;
349af807c0fSNate Lawson 	    max = delta;
3509abd8cd0SJung-uk Kim 	} else if (delta > max2)
3519abd8cd0SJung-uk Kim 	    max2 = delta;
352aa977fc7SJung-uk Kim 	if (delta < min)
353af807c0fSNate Lawson 	    min = delta;
354af807c0fSNate Lawson 	last = this;
355af807c0fSNate Lawson     }
356c0b9a6deSNate Lawson     intr_restore(s);
357c0b9a6deSNate Lawson 
3589abd8cd0SJung-uk Kim     delta = max2 - min;
3599abd8cd0SJung-uk Kim     if ((max - min > 8 || delta > 3) && vm_guest == VM_GUEST_NO)
360af807c0fSNate Lawson 	n = 0;
3619abd8cd0SJung-uk Kim     else if (min < 0 || max == 0 || max2 == 0)
362af807c0fSNate Lawson 	n = 0;
363af807c0fSNate Lawson     else
364af807c0fSNate Lawson 	n = 1;
3652a921b05SPoul-Henning Kamp     if (bootverbose)
366d141bf6eSJung-uk Kim 	printf(" %d/%d", n, delta);
367af807c0fSNate Lawson 
368af807c0fSNate Lawson     return (n);
369af807c0fSNate Lawson }
370af807c0fSNate Lawson #undef N
371af807c0fSNate Lawson 
372af807c0fSNate Lawson /*
373787fa5b8SMike Smith  * Test harness for verifying ACPI timer behaviour.
374787fa5b8SMike Smith  * Boot with debug.acpi.timer_test set to invoke this.
375787fa5b8SMike Smith  */
376787fa5b8SMike Smith static void
377d9b6df60SNate Lawson acpi_timer_boot_test(void)
378787fa5b8SMike Smith {
37975988358SNate Lawson     uint32_t u1, u2, u3;
380787fa5b8SMike Smith 
381d9b6df60SNate Lawson     u1 = acpi_timer_read();
382d9b6df60SNate Lawson     u2 = acpi_timer_read();
383d9b6df60SNate Lawson     u3 = acpi_timer_read();
384787fa5b8SMike Smith 
385787fa5b8SMike Smith     device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
386787fa5b8SMike Smith     for (;;) {
387787fa5b8SMike Smith 	/*
388be2b1797SNate Lawson 	 * The failure case is where u3 > u1, but u2 does not fall between
389be2b1797SNate Lawson 	 * the two, ie. it contains garbage.
390787fa5b8SMike Smith 	 */
391787fa5b8SMike Smith 	if (u3 > u1) {
392be2b1797SNate Lawson 	    if (u2 < u1 || u2 > u3)
393be2b1797SNate Lawson 		device_printf(acpi_timer_dev,
394be2b1797SNate Lawson 			      "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
395787fa5b8SMike Smith 			      u1, u2, u3);
396787fa5b8SMike Smith 	}
397787fa5b8SMike Smith 	u1 = u2;
398787fa5b8SMike Smith 	u2 = u3;
399d9b6df60SNate Lawson 	u3 = acpi_timer_read();
400787fa5b8SMike Smith     }
401787fa5b8SMike Smith }
402