xref: /freebsd/sys/dev/acpica/acpi_timer.c (revision af807c0f339beac8648be9eb3db1641d114c42bf)
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  *
2715e32d5dSMike Smith  *	$FreeBSD$
2815e32d5dSMike Smith  */
2915e32d5dSMike Smith #include "opt_acpi.h"
3015e32d5dSMike Smith #include <sys/param.h>
3115e32d5dSMike Smith #include <sys/bus.h>
32787fa5b8SMike Smith #include <sys/kernel.h>
33787fa5b8SMike Smith #include <sys/sysctl.h>
34787fa5b8SMike Smith #include <sys/timetc.h>
35787fa5b8SMike Smith 
36787fa5b8SMike Smith #include <machine/bus.h>
37787fa5b8SMike Smith #include <machine/resource.h>
38787fa5b8SMike Smith #include <sys/rman.h>
3915e32d5dSMike Smith 
4015e32d5dSMike Smith #include "acpi.h"
4187e5d361SJohn Baldwin #include <dev/acpica/acpivar.h>
42cace7a2aSWarner Losh #include <dev/pci/pcivar.h>
43787fa5b8SMike Smith 
44787fa5b8SMike Smith /*
45787fa5b8SMike Smith  * A timecounter based on the free-running ACPI timer.
46787fa5b8SMike Smith  *
47787fa5b8SMike Smith  * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
48787fa5b8SMike Smith  */
4915e32d5dSMike Smith 
50be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
513184cf5aSNate Lawson #define _COMPONENT	ACPI_TIMER
5272e5754cSMike Smith ACPI_MODULE_NAME("TIMER")
530ae55423SMike Smith 
54787fa5b8SMike Smith static device_t			acpi_timer_dev;
55af807c0fSNate Lawson static struct resource		*acpi_timer_reg;
56af807c0fSNate Lawson static bus_space_handle_t	timer_bsh;
57af807c0fSNate Lawson static bus_space_tag_t		timer_bst;
5815e32d5dSMike Smith 
59787fa5b8SMike Smith static u_int	acpi_timer_frequency = 14318182 / 4;
6015e32d5dSMike Smith 
6115e32d5dSMike Smith static void	acpi_timer_identify(driver_t *driver, device_t parent);
6215e32d5dSMike Smith static int	acpi_timer_probe(device_t dev);
6315e32d5dSMike Smith static int	acpi_timer_attach(device_t dev);
6475988358SNate Lawson static u_int	acpi_timer_get_timecount(struct timecounter *tc);
6575988358SNate Lawson static u_int	acpi_timer_get_timecount_safe(struct timecounter *tc);
66787fa5b8SMike Smith static int	acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
67787fa5b8SMike Smith static void	acpi_timer_test(void);
6815e32d5dSMike Smith 
6975988358SNate Lawson static u_int	read_counter(void);
70214475d8SMarcel Moolenaar static int	test_counter(void);
71214475d8SMarcel Moolenaar 
7215e32d5dSMike Smith static device_method_t acpi_timer_methods[] = {
7315e32d5dSMike Smith     DEVMETHOD(device_identify,	acpi_timer_identify),
7415e32d5dSMike Smith     DEVMETHOD(device_probe,	acpi_timer_probe),
7515e32d5dSMike Smith     DEVMETHOD(device_attach,	acpi_timer_attach),
7615e32d5dSMike Smith 
7715e32d5dSMike Smith     {0, 0}
7815e32d5dSMike Smith };
7915e32d5dSMike Smith 
8015e32d5dSMike Smith static driver_t acpi_timer_driver = {
8115e32d5dSMike Smith     "acpi_timer",
8215e32d5dSMike Smith     acpi_timer_methods,
83787fa5b8SMike Smith     0,
8415e32d5dSMike Smith };
8515e32d5dSMike Smith 
863273b005SMike Smith static devclass_t acpi_timer_devclass;
8715e32d5dSMike Smith DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
8864278df5SNate Lawson MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
8915e32d5dSMike Smith 
90787fa5b8SMike Smith static struct timecounter acpi_timer_timecounter = {
9175988358SNate Lawson 	.tc_get_timecount =	acpi_timer_get_timecount_safe,
9275988358SNate Lawson 	.tc_poll_pps =		0,
9375988358SNate Lawson 	.tc_counter_mask =	0,
9475988358SNate Lawson 	.tc_frequency =		0,
9575988358SNate Lawson 	.tc_name =		"ACPI",
9675988358SNate Lawson 	.tc_quality =		1000
97787fa5b8SMike Smith };
98787fa5b8SMike Smith 
9975988358SNate Lawson static u_int
100214475d8SMarcel Moolenaar read_counter()
101214475d8SMarcel Moolenaar {
10275988358SNate Lawson     uint32_t tv;
103214475d8SMarcel Moolenaar 
104af807c0fSNate Lawson     tv = bus_space_read_4(timer_bst, timer_bsh, 0);
105af807c0fSNate Lawson     bus_space_barrier(timer_bst, timer_bsh, 0, 4, BUS_SPACE_BARRIER_READ);
106214475d8SMarcel Moolenaar     return (tv);
107214475d8SMarcel Moolenaar }
108cb877d00SPoul-Henning Kamp 
109787fa5b8SMike Smith /*
110787fa5b8SMike Smith  * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
111787fa5b8SMike Smith  * we will be using.
112787fa5b8SMike Smith  */
11315e32d5dSMike Smith static void
11415e32d5dSMike Smith acpi_timer_identify(driver_t *driver, device_t parent)
11515e32d5dSMike Smith {
11615e32d5dSMike Smith     device_t	dev;
11715e32d5dSMike Smith     char	desc[40];
118214475d8SMarcel Moolenaar     u_long	rlen, rstart;
119214475d8SMarcel Moolenaar     int		i, j, rid, rtype;
12015e32d5dSMike Smith 
121b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1220ae55423SMike Smith 
123be2b1797SNate Lawson     if (acpi_disabled("timer") || AcpiGbl_FADT == NULL)
1240ae55423SMike Smith 	return_VOID;
12515e32d5dSMike Smith 
12615e32d5dSMike Smith     if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) {
12715e32d5dSMike Smith 	device_printf(parent, "could not add acpi_timer0\n");
1280ae55423SMike Smith 	return_VOID;
12915e32d5dSMike Smith     }
130787fa5b8SMike Smith     acpi_timer_dev = dev;
131214475d8SMarcel Moolenaar 
132787fa5b8SMike Smith     rid = 0;
133214475d8SMarcel Moolenaar     rlen = AcpiGbl_FADT->PmTmLen;
134214475d8SMarcel Moolenaar     rtype = (AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId)
135214475d8SMarcel Moolenaar       ? SYS_RES_IOPORT : SYS_RES_MEMORY;
136214475d8SMarcel Moolenaar     rstart = AcpiGbl_FADT->XPmTmrBlk.Address;
137214475d8SMarcel Moolenaar     bus_set_resource(dev, rtype, rid, rstart, rlen);
1385f96beb9SNate Lawson     acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
139214475d8SMarcel Moolenaar     if (acpi_timer_reg == NULL) {
140214475d8SMarcel Moolenaar 	device_printf(dev, "couldn't allocate I/O resource (%s 0x%lx)\n",
141be2b1797SNate Lawson 		      rtype == SYS_RES_IOPORT ? "port" : "mem", rstart);
1420ae55423SMike Smith 	return_VOID;
14315e32d5dSMike Smith     }
144af807c0fSNate Lawson     timer_bsh = rman_get_bushandle(acpi_timer_reg);
145af807c0fSNate Lawson     timer_bst = rman_get_bustag(acpi_timer_reg);
14675988358SNate Lawson     if (AcpiGbl_FADT->TmrValExt != 0)
14775988358SNate Lawson 	acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
14875988358SNate Lawson     else
14975988358SNate Lawson 	acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
15075988358SNate Lawson     acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
151d786139cSMaxime Henrion     if (testenv("debug.acpi.timer_test"))
152787fa5b8SMike Smith 	acpi_timer_test();
153787fa5b8SMike Smith 
154af807c0fSNate Lawson     /*
155af807c0fSNate Lawson      * If all tests of the counter succeed, use the ACPI-fast method.  If
156af807c0fSNate Lawson      * at least one failed, default to using the safe routine, which reads
157af807c0fSNate Lawson      * the timer multiple times to get a consistent value before returning.
158af807c0fSNate Lawson      */
159cb877d00SPoul-Henning Kamp     j = 0;
160cb877d00SPoul-Henning Kamp     for (i = 0; i < 10; i++)
161cb877d00SPoul-Henning Kamp 	j += test_counter();
162cb877d00SPoul-Henning Kamp     if (j == 10) {
163cb877d00SPoul-Henning Kamp 	acpi_timer_timecounter.tc_name = "ACPI-fast";
164cb877d00SPoul-Henning Kamp 	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
165cb877d00SPoul-Henning Kamp     } else {
166cb877d00SPoul-Henning Kamp 	acpi_timer_timecounter.tc_name = "ACPI-safe";
167cb877d00SPoul-Henning Kamp 	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
168cb877d00SPoul-Henning Kamp     }
169787fa5b8SMike Smith     tc_init(&acpi_timer_timecounter);
17015e32d5dSMike Smith 
171be2b1797SNate Lawson     sprintf(desc, "%d-bit timer at 3.579545MHz",
172be2b1797SNate Lawson 	    AcpiGbl_FADT->TmrValExt ? 32 : 24);
17315e32d5dSMike Smith     device_set_desc_copy(dev, desc);
1740ae55423SMike Smith 
1750ae55423SMike Smith     return_VOID;
17615e32d5dSMike Smith }
17715e32d5dSMike Smith 
17815e32d5dSMike Smith static int
17915e32d5dSMike Smith acpi_timer_probe(device_t dev)
18015e32d5dSMike Smith {
181787fa5b8SMike Smith     if (dev == acpi_timer_dev)
18215e32d5dSMike Smith 	return (0);
183be2b1797SNate Lawson 
18415e32d5dSMike Smith     return (ENXIO);
18515e32d5dSMike Smith }
18615e32d5dSMike Smith 
18715e32d5dSMike Smith static int
18815e32d5dSMike Smith acpi_timer_attach(device_t dev)
18915e32d5dSMike Smith {
190787fa5b8SMike Smith     return (0);
19115e32d5dSMike Smith }
192787fa5b8SMike Smith 
193787fa5b8SMike Smith /*
194feade919SMike Smith  * Fetch current time value from reliable hardware.
195787fa5b8SMike Smith  */
19675988358SNate Lawson static u_int
197787fa5b8SMike Smith acpi_timer_get_timecount(struct timecounter *tc)
198787fa5b8SMike Smith {
199214475d8SMarcel Moolenaar     return (read_counter());
200787fa5b8SMike Smith }
201787fa5b8SMike Smith 
202787fa5b8SMike Smith /*
203feade919SMike Smith  * Fetch current time value from hardware that may not correctly
204af807c0fSNate Lawson  * latch the counter.  We need to read until we have three monotonic
205af807c0fSNate Lawson  * samples and then use the middle one, otherwise we are not protected
206af807c0fSNate Lawson  * against the fact that the bits can be wrong in two directions.  If
207af807c0fSNate Lawson  * we only cared about monosity, two reads would be enough.
208feade919SMike Smith  */
20975988358SNate Lawson static u_int
210feade919SMike Smith acpi_timer_get_timecount_safe(struct timecounter *tc)
211feade919SMike Smith {
21275988358SNate Lawson     u_int u1, u2, u3;
213feade919SMike Smith 
214214475d8SMarcel Moolenaar     u2 = read_counter();
215214475d8SMarcel Moolenaar     u3 = read_counter();
216feade919SMike Smith     do {
217feade919SMike Smith 	u1 = u2;
218feade919SMike Smith 	u2 = u3;
219214475d8SMarcel Moolenaar 	u3 = read_counter();
220be2b1797SNate Lawson     } while (u1 > u2 || u2 > u3 || u3 - u1 > 15);
221be2b1797SNate Lawson 
222feade919SMike Smith     return (u2);
223feade919SMike Smith }
224feade919SMike Smith 
225feade919SMike Smith /*
226787fa5b8SMike Smith  * Timecounter freqency adjustment interface.
227787fa5b8SMike Smith  */
228787fa5b8SMike Smith static int
229787fa5b8SMike Smith acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
230787fa5b8SMike Smith {
231787fa5b8SMike Smith     int error;
232787fa5b8SMike Smith     u_int freq;
233787fa5b8SMike Smith 
234787fa5b8SMike Smith     if (acpi_timer_timecounter.tc_frequency == 0)
235787fa5b8SMike Smith 	return (EOPNOTSUPP);
236787fa5b8SMike Smith     freq = acpi_timer_frequency;
237787fa5b8SMike Smith     error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
238787fa5b8SMike Smith     if (error == 0 && req->newptr != NULL) {
239787fa5b8SMike Smith 	acpi_timer_frequency = freq;
240787fa5b8SMike Smith 	acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
241787fa5b8SMike Smith     }
242be2b1797SNate Lawson 
243787fa5b8SMike Smith     return (error);
244787fa5b8SMike Smith }
245787fa5b8SMike Smith 
246787fa5b8SMike Smith SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
247787fa5b8SMike Smith 	    0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "");
248787fa5b8SMike Smith 
249787fa5b8SMike Smith /*
250af807c0fSNate Lawson  * Some ACPI timers are known or believed to suffer from implementation
251af807c0fSNate Lawson  * problems which can lead to erroneous values being read.  This function
252af807c0fSNate Lawson  * tests for consistent results from the timer and returns 1 if it believes
253af807c0fSNate Lawson  * the timer is consistent, otherwise it returns 0.
254af807c0fSNate Lawson  *
255af807c0fSNate Lawson  * It appears the cause is that the counter is not latched to the PCI bus
256af807c0fSNate Lawson  * clock when read:
257af807c0fSNate Lawson  *
258af807c0fSNate Lawson  * ] 20. ACPI Timer Errata
259af807c0fSNate Lawson  * ]
260af807c0fSNate Lawson  * ]   Problem: The power management timer may return improper result when
261af807c0fSNate Lawson  * ]   read. Although the timer value settles properly after incrementing,
262af807c0fSNate Lawson  * ]   while incrementing there is a 3nS window every 69.8nS where the
263af807c0fSNate Lawson  * ]   timer value is indeterminate (a 4.2% chance that the data will be
264af807c0fSNate Lawson  * ]   incorrect when read). As a result, the ACPI free running count up
265af807c0fSNate Lawson  * ]   timer specification is violated due to erroneous reads.  Implication:
266af807c0fSNate Lawson  * ]   System hangs due to the "inaccuracy" of the timer when used by
267af807c0fSNate Lawson  * ]   software for time critical events and delays.
268af807c0fSNate Lawson  * ]
269af807c0fSNate Lawson  * ] Workaround: Read the register twice and compare.
270af807c0fSNate Lawson  * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
271af807c0fSNate Lawson  * ] in the PIIX4M.
272af807c0fSNate Lawson  */
273af807c0fSNate Lawson #define N 2000
274af807c0fSNate Lawson static int
275af807c0fSNate Lawson test_counter()
276af807c0fSNate Lawson {
277af807c0fSNate Lawson     uint32_t	last, this;
278af807c0fSNate Lawson     int		min, max, n, delta;
279af807c0fSNate Lawson 
280af807c0fSNate Lawson     min = 10000000;
281af807c0fSNate Lawson     max = 0;
282af807c0fSNate Lawson     last = read_counter();
283af807c0fSNate Lawson     for (n = 0; n < N; n++) {
284af807c0fSNate Lawson 	this = read_counter();
285af807c0fSNate Lawson 	delta = acpi_TimerDelta(this, last);
286af807c0fSNate Lawson 	if (delta > max)
287af807c0fSNate Lawson 	    max = delta;
288af807c0fSNate Lawson 	else if (delta < min)
289af807c0fSNate Lawson 	    min = delta;
290af807c0fSNate Lawson 	last = this;
291af807c0fSNate Lawson     }
292af807c0fSNate Lawson     if (max - min > 2)
293af807c0fSNate Lawson 	n = 0;
294af807c0fSNate Lawson     else if (min < 0 || max == 0)
295af807c0fSNate Lawson 	n = 0;
296af807c0fSNate Lawson     else
297af807c0fSNate Lawson 	n = 1;
298af807c0fSNate Lawson     if (bootverbose) {
299af807c0fSNate Lawson 	printf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
300af807c0fSNate Lawson 		n ? "GOOD" : "BAD ",
301af807c0fSNate Lawson 		min, max, max - min);
302af807c0fSNate Lawson     }
303af807c0fSNate Lawson 
304af807c0fSNate Lawson     return (n);
305af807c0fSNate Lawson }
306af807c0fSNate Lawson #undef N
307af807c0fSNate Lawson 
308af807c0fSNate Lawson /*
309787fa5b8SMike Smith  * Test harness for verifying ACPI timer behaviour.
310787fa5b8SMike Smith  * Boot with debug.acpi.timer_test set to invoke this.
311787fa5b8SMike Smith  */
312787fa5b8SMike Smith static void
313787fa5b8SMike Smith acpi_timer_test(void)
314787fa5b8SMike Smith {
31575988358SNate Lawson     uint32_t u1, u2, u3;
316787fa5b8SMike Smith 
317214475d8SMarcel Moolenaar     u1 = read_counter();
318214475d8SMarcel Moolenaar     u2 = read_counter();
319214475d8SMarcel Moolenaar     u3 = read_counter();
320787fa5b8SMike Smith 
321787fa5b8SMike Smith     device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
322787fa5b8SMike Smith     for (;;) {
323787fa5b8SMike Smith 	/*
324be2b1797SNate Lawson 	 * The failure case is where u3 > u1, but u2 does not fall between
325be2b1797SNate Lawson 	 * the two, ie. it contains garbage.
326787fa5b8SMike Smith 	 */
327787fa5b8SMike Smith 	if (u3 > u1) {
328be2b1797SNate Lawson 	    if (u2 < u1 || u2 > u3)
329be2b1797SNate Lawson 		device_printf(acpi_timer_dev,
330be2b1797SNate Lawson 			      "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
331787fa5b8SMike Smith 			      u1, u2, u3);
332787fa5b8SMike Smith 	}
333787fa5b8SMike Smith 	u1 = u2;
334787fa5b8SMike Smith 	u2 = u3;
335214475d8SMarcel Moolenaar 	u3 = read_counter();
336787fa5b8SMike Smith     }
337787fa5b8SMike Smith }
338