acpi_timer.c revision 167814
167761Smsmith/*-
280070Smsmith * Copyright (c) 2000, 2001 Michael Smith
367761Smsmith * Copyright (c) 2000 BSDi
467761Smsmith * All rights reserved.
567761Smsmith *
667761Smsmith * Redistribution and use in source and binary forms, with or without
767761Smsmith * modification, are permitted provided that the following conditions
867761Smsmith * are met:
967761Smsmith * 1. Redistributions of source code must retain the above copyright
1067761Smsmith *    notice, this list of conditions and the following disclaimer.
1167761Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1267761Smsmith *    notice, this list of conditions and the following disclaimer in the
1367761Smsmith *    documentation and/or other materials provided with the distribution.
1467761Smsmith *
1567761Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1667761Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1767761Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1867761Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1967761Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2067761Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2167761Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2267761Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2367761Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2467761Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2567761Smsmith * SUCH DAMAGE.
2667761Smsmith */
27143002Sobrien
28143002Sobrien#include <sys/cdefs.h>
29143002Sobrien__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi_timer.c 167814 2007-03-22 18:16:43Z jkim $");
30143002Sobrien
3167761Smsmith#include "opt_acpi.h"
3267761Smsmith#include <sys/param.h>
3380070Smsmith#include <sys/bus.h>
3467761Smsmith#include <sys/kernel.h>
35129879Sphk#include <sys/module.h>
3680070Smsmith#include <sys/sysctl.h>
3780070Smsmith#include <sys/timetc.h>
3867761Smsmith
3980070Smsmith#include <machine/bus.h>
4080070Smsmith#include <machine/resource.h>
4180070Smsmith#include <sys/rman.h>
4280070Smsmith
43150003Sobrien#include <contrib/dev/acpica/acpi.h>
44104726Sjhb#include <dev/acpica/acpivar.h>
45119281Simp#include <dev/pci/pcivar.h>
4667761Smsmith
4769744Smsmith/*
4880070Smsmith * A timecounter based on the free-running ACPI timer.
4980070Smsmith *
5080070Smsmith * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
5180070Smsmith */
5280070Smsmith
53119529Snjl/* Hooks for the ACPI CA debugging infrastructure */
54126517Snjl#define _COMPONENT	ACPI_TIMER
5591128SmsmithACPI_MODULE_NAME("TIMER")
5669744Smsmith
57128528Snjlstatic device_t			acpi_timer_dev;
58128528Snjlstatic struct resource		*acpi_timer_reg;
59128543Snjlstatic bus_space_handle_t	acpi_timer_bsh;
60128543Snjlstatic bus_space_tag_t		acpi_timer_bst;
6167761Smsmith
62119529Snjlstatic u_int	acpi_timer_frequency = 14318182 / 4;
6367761Smsmith
6467761Smsmithstatic void	acpi_timer_identify(driver_t *driver, device_t parent);
6567761Smsmithstatic int	acpi_timer_probe(device_t dev);
6667761Smsmithstatic int	acpi_timer_attach(device_t dev);
67128506Snjlstatic u_int	acpi_timer_get_timecount(struct timecounter *tc);
68128506Snjlstatic u_int	acpi_timer_get_timecount_safe(struct timecounter *tc);
6980070Smsmithstatic int	acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
70128543Snjlstatic void	acpi_timer_boot_test(void);
7167761Smsmith
72128543Snjlstatic u_int	acpi_timer_read(void);
73128543Snjlstatic int	acpi_timer_test(void);
74114277Smarcel
7567761Smsmithstatic device_method_t acpi_timer_methods[] = {
7667761Smsmith    DEVMETHOD(device_identify,	acpi_timer_identify),
7767761Smsmith    DEVMETHOD(device_probe,	acpi_timer_probe),
7867761Smsmith    DEVMETHOD(device_attach,	acpi_timer_attach),
7967761Smsmith
8067761Smsmith    {0, 0}
8167761Smsmith};
8267761Smsmith
8367761Smsmithstatic driver_t acpi_timer_driver = {
8467761Smsmith    "acpi_timer",
8567761Smsmith    acpi_timer_methods,
8680070Smsmith    0,
8767761Smsmith};
8867761Smsmith
8989054Smsmithstatic devclass_t acpi_timer_devclass;
9067761SmsmithDRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
91128071SnjlMODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
9267761Smsmith
9380070Smsmithstatic struct timecounter acpi_timer_timecounter = {
94128543Snjl	acpi_timer_get_timecount_safe,	/* get_timecount function */
95128543Snjl	0,				/* no poll_pps */
96128543Snjl	0,				/* no default counter_mask */
97128543Snjl	0,				/* no default frequency */
98128543Snjl	"ACPI",				/* name */
99128543Snjl	1000				/* quality */
10080070Smsmith};
10180070Smsmith
102128506Snjlstatic u_int
103128543Snjlacpi_timer_read()
104114277Smarcel{
105128607Snjl    return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0));
106114277Smarcel}
107114277Smarcel
10880070Smsmith/*
10980070Smsmith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
11080070Smsmith * we will be using.
11180070Smsmith */
11267761Smsmithstatic void
11367761Smsmithacpi_timer_identify(driver_t *driver, device_t parent)
11467761Smsmith{
115132527Snjl    device_t dev;
116132527Snjl    u_long rlen, rstart;
117132527Snjl    int rid, rtype;
11867761Smsmith
11996926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
12069744Smsmith
121136270Snjl    if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) ||
122167814Sjkim	acpi_timer_dev)
12369744Smsmith	return_VOID;
124128506Snjl
12567761Smsmith    if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) {
12667761Smsmith	device_printf(parent, "could not add acpi_timer0\n");
12769744Smsmith	return_VOID;
12867761Smsmith    }
12980070Smsmith    acpi_timer_dev = dev;
130114277Smarcel
13180070Smsmith    rid = 0;
132167814Sjkim    rtype = AcpiGbl_FADT.XPmTimerBlock.SpaceId ?
133132527Snjl	SYS_RES_IOPORT : SYS_RES_MEMORY;
134167814Sjkim    rlen = AcpiGbl_FADT.PmTimerLength;
135167814Sjkim    rstart = AcpiGbl_FADT.XPmTimerBlock.Address;
136132527Snjl    if (bus_set_resource(dev, rtype, rid, rstart, rlen))
137132527Snjl	device_printf(dev, "couldn't set resource (%s 0x%lx+0x%lx)\n",
138132527Snjl	    (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen);
139132527Snjl    return_VOID;
140132527Snjl}
141132527Snjl
142132527Snjlstatic int
143132527Snjlacpi_timer_probe(device_t dev)
144132527Snjl{
145132527Snjl    char desc[40];
146132527Snjl    int i, j, rid, rtype;
147132527Snjl
148132527Snjl    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
149132527Snjl
150132527Snjl    if (dev != acpi_timer_dev)
151132527Snjl	return (ENXIO);
152132527Snjl
153132527Snjl    rid = 0;
154167814Sjkim    rtype = AcpiGbl_FADT.XPmTimerBlock.SpaceId ?
155132527Snjl	SYS_RES_IOPORT : SYS_RES_MEMORY;
156127135Snjl    acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
157114277Smarcel    if (acpi_timer_reg == NULL) {
158132527Snjl	device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n",
159132527Snjl	    (rtype == SYS_RES_IOPORT) ? "port" : "mem",
160167814Sjkim	    (u_long)AcpiGbl_FADT.XPmTimerBlock.Address);
161132527Snjl	return (ENXIO);
16267761Smsmith    }
163132528Snjl    acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
164132528Snjl    acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
165167814Sjkim    if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER)
166128506Snjl	acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
167128506Snjl    else
168128506Snjl	acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
169128506Snjl    acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
17094936Smux    if (testenv("debug.acpi.timer_test"))
171128543Snjl	acpi_timer_boot_test();
17267761Smsmith
173128528Snjl    /*
174128528Snjl     * If all tests of the counter succeed, use the ACPI-fast method.  If
175128528Snjl     * at least one failed, default to using the safe routine, which reads
176128528Snjl     * the timer multiple times to get a consistent value before returning.
177128528Snjl     */
17891237Sphk    j = 0;
179137151Sphk    if (bootverbose)
180137151Sphk	printf("ACPI timer:");
181128506Snjl    for (i = 0; i < 10; i++)
182128543Snjl	j += acpi_timer_test();
183137151Sphk    if (bootverbose)
184137151Sphk	printf(" -> %d\n", j);
18591237Sphk    if (j == 10) {
18691237Sphk	acpi_timer_timecounter.tc_name = "ACPI-fast";
18791237Sphk	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
18891237Sphk    } else {
18991237Sphk	acpi_timer_timecounter.tc_name = "ACPI-safe";
19091237Sphk	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
19191237Sphk    }
19280070Smsmith    tc_init(&acpi_timer_timecounter);
19380070Smsmith
194119529Snjl    sprintf(desc, "%d-bit timer at 3.579545MHz",
195167814Sjkim	(AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) ? 32 : 24);
19667761Smsmith    device_set_desc_copy(dev, desc);
19769744Smsmith
198132527Snjl    /* Release the resource, we'll allocate it again during attach. */
199132527Snjl    bus_release_resource(dev, rtype, rid, acpi_timer_reg);
200132527Snjl    return (0);
20167761Smsmith}
20267761Smsmith
20367761Smsmithstatic int
204132527Snjlacpi_timer_attach(device_t dev)
20567761Smsmith{
206132527Snjl    int rid, rtype;
207119529Snjl
208132527Snjl    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
20967761Smsmith
210132527Snjl    rid = 0;
211167814Sjkim    rtype = AcpiGbl_FADT.XPmTimerBlock.SpaceId ?
212132527Snjl	SYS_RES_IOPORT : SYS_RES_MEMORY;
213132527Snjl    acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
214132527Snjl    if (acpi_timer_reg == NULL)
215132527Snjl	return (ENXIO);
216132527Snjl    acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
217132527Snjl    acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
218119529Snjl    return (0);
21980070Smsmith}
22067761Smsmith
22180070Smsmith/*
22281096Smsmith * Fetch current time value from reliable hardware.
22380070Smsmith */
224128506Snjlstatic u_int
22580070Smsmithacpi_timer_get_timecount(struct timecounter *tc)
22680070Smsmith{
227128543Snjl    return (acpi_timer_read());
22867761Smsmith}
22980070Smsmith
23080070Smsmith/*
23181096Smsmith * Fetch current time value from hardware that may not correctly
232128528Snjl * latch the counter.  We need to read until we have three monotonic
233128528Snjl * samples and then use the middle one, otherwise we are not protected
234128528Snjl * against the fact that the bits can be wrong in two directions.  If
235128528Snjl * we only cared about monosity, two reads would be enough.
23681096Smsmith */
237128506Snjlstatic u_int
23881096Smsmithacpi_timer_get_timecount_safe(struct timecounter *tc)
23981096Smsmith{
240128506Snjl    u_int u1, u2, u3;
24181096Smsmith
242128543Snjl    u2 = acpi_timer_read();
243128543Snjl    u3 = acpi_timer_read();
24481096Smsmith    do {
24581096Smsmith	u1 = u2;
24681096Smsmith	u2 = u3;
247128543Snjl	u3 = acpi_timer_read();
248128543Snjl    } while (u1 > u2 || u2 > u3);
249119529Snjl
25081096Smsmith    return (u2);
25181096Smsmith}
25281096Smsmith
25381096Smsmith/*
25480070Smsmith * Timecounter freqency adjustment interface.
25580070Smsmith */
25680070Smsmithstatic int
25780070Smsmithacpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
25880070Smsmith{
25980070Smsmith    int error;
26080070Smsmith    u_int freq;
26180070Smsmith
26280070Smsmith    if (acpi_timer_timecounter.tc_frequency == 0)
26380070Smsmith	return (EOPNOTSUPP);
26480070Smsmith    freq = acpi_timer_frequency;
26580070Smsmith    error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
26680070Smsmith    if (error == 0 && req->newptr != NULL) {
26780070Smsmith	acpi_timer_frequency = freq;
26880070Smsmith	acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
26980070Smsmith    }
270119529Snjl
27180070Smsmith    return (error);
27280070Smsmith}
27380070Smsmith
27480070SmsmithSYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
27580070Smsmith	    0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "");
27680070Smsmith
27780070Smsmith/*
278128528Snjl * Some ACPI timers are known or believed to suffer from implementation
279128528Snjl * problems which can lead to erroneous values being read.  This function
280128528Snjl * tests for consistent results from the timer and returns 1 if it believes
281128528Snjl * the timer is consistent, otherwise it returns 0.
282128528Snjl *
283128528Snjl * It appears the cause is that the counter is not latched to the PCI bus
284128528Snjl * clock when read:
285128528Snjl *
286128528Snjl * ] 20. ACPI Timer Errata
287128528Snjl * ]
288128528Snjl * ]   Problem: The power management timer may return improper result when
289128528Snjl * ]   read. Although the timer value settles properly after incrementing,
290128528Snjl * ]   while incrementing there is a 3nS window every 69.8nS where the
291128528Snjl * ]   timer value is indeterminate (a 4.2% chance that the data will be
292128528Snjl * ]   incorrect when read). As a result, the ACPI free running count up
293128528Snjl * ]   timer specification is violated due to erroneous reads.  Implication:
294128528Snjl * ]   System hangs due to the "inaccuracy" of the timer when used by
295128528Snjl * ]   software for time critical events and delays.
296128528Snjl * ]
297128528Snjl * ] Workaround: Read the register twice and compare.
298128528Snjl * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
299128528Snjl * ] in the PIIX4M.
300128528Snjl */
301128528Snjl#define N 2000
302128528Snjlstatic int
303128543Snjlacpi_timer_test()
304128528Snjl{
305128528Snjl    uint32_t	last, this;
306128528Snjl    int		min, max, n, delta;
307128607Snjl    register_t	s;
308128528Snjl
309128528Snjl    min = 10000000;
310128528Snjl    max = 0;
311128607Snjl
312128607Snjl    /* Test the timer with interrupts disabled to get accurate results. */
313128607Snjl    s = intr_disable();
314128543Snjl    last = acpi_timer_read();
315128528Snjl    for (n = 0; n < N; n++) {
316128543Snjl	this = acpi_timer_read();
317128528Snjl	delta = acpi_TimerDelta(this, last);
318128528Snjl	if (delta > max)
319128528Snjl	    max = delta;
320128528Snjl	else if (delta < min)
321128528Snjl	    min = delta;
322128528Snjl	last = this;
323128528Snjl    }
324128607Snjl    intr_restore(s);
325128607Snjl
326128528Snjl    if (max - min > 2)
327128528Snjl	n = 0;
328128528Snjl    else if (min < 0 || max == 0)
329128528Snjl	n = 0;
330128528Snjl    else
331128528Snjl	n = 1;
332137151Sphk    if (bootverbose)
333137151Sphk	printf(" %d/%d", n, max-min);
334128528Snjl
335128528Snjl    return (n);
336128528Snjl}
337128528Snjl#undef N
338128528Snjl
339128528Snjl/*
34080070Smsmith * Test harness for verifying ACPI timer behaviour.
34180070Smsmith * Boot with debug.acpi.timer_test set to invoke this.
34280070Smsmith */
34380070Smsmithstatic void
344128543Snjlacpi_timer_boot_test(void)
34580070Smsmith{
346128506Snjl    uint32_t u1, u2, u3;
347128506Snjl
348128543Snjl    u1 = acpi_timer_read();
349128543Snjl    u2 = acpi_timer_read();
350128543Snjl    u3 = acpi_timer_read();
351128506Snjl
35280070Smsmith    device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
35380070Smsmith    for (;;) {
35480070Smsmith	/*
355119529Snjl	 * The failure case is where u3 > u1, but u2 does not fall between
356119529Snjl	 * the two, ie. it contains garbage.
35780070Smsmith	 */
35880070Smsmith	if (u3 > u1) {
359119529Snjl	    if (u2 < u1 || u2 > u3)
360119529Snjl		device_printf(acpi_timer_dev,
361119529Snjl			      "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
36280070Smsmith			      u1, u2, u3);
36380070Smsmith	}
36480070Smsmith	u1 = u2;
36580070Smsmith	u2 = u3;
366128543Snjl	u3 = acpi_timer_read();
36780070Smsmith    }
36880070Smsmith}
369