acpi_timer.c revision 132527
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 *
2767761Smsmith *	$FreeBSD: head/sys/dev/acpica/acpi_timer.c 132527 2004-07-22 05:32:56Z njl $
2867761Smsmith */
2967761Smsmith#include "opt_acpi.h"
3067761Smsmith#include <sys/param.h>
3180070Smsmith#include <sys/bus.h>
3267761Smsmith#include <sys/kernel.h>
3380070Smsmith#include <sys/module.h>
34105281Sjhb#include <sys/sysctl.h>
3580070Smsmith#include <sys/timetc.h>
36105281Sjhb
37105281Sjhb#include <machine/bus.h>
38105281Sjhb#include <machine/resource.h>
3967761Smsmith#include <sys/rman.h>
4080070Smsmith
4180070Smsmith#include "acpi.h"
4280070Smsmith#include <dev/acpica/acpivar.h>
4380070Smsmith#include <dev/pci/pcivar.h>
4467761Smsmith
4567761Smsmith/*
46104726Sjhb * A timecounter based on the free-running ACPI timer.
4780070Smsmith *
4867761Smsmith * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
4969744Smsmith */
5080070Smsmith
5180070Smsmith/* Hooks for the ACPI CA debugging infrastructure */
5280070Smsmith#define _COMPONENT	ACPI_TIMER
5380070SmsmithACPI_MODULE_NAME("TIMER")
5480070Smsmith
5580070Smsmithstatic device_t			acpi_timer_dev;
5669744Smsmithstatic struct resource		*acpi_timer_reg;
5769744Smsmithstatic bus_space_handle_t	acpi_timer_bsh;
5877432Smsmithstatic bus_space_tag_t		acpi_timer_bst;
5991128Smsmith
6069744Smsmithstatic u_int	acpi_timer_frequency = 14318182 / 4;
6180070Smsmith
6280070Smsmithstatic void	acpi_timer_identify(driver_t *driver, device_t parent);
6367761Smsmithstatic int	acpi_timer_probe(device_t dev);
6480070Smsmithstatic int	acpi_timer_attach(device_t dev);
6567761Smsmithstatic u_int	acpi_timer_get_timecount(struct timecounter *tc);
6667761Smsmithstatic u_int	acpi_timer_get_timecount_safe(struct timecounter *tc);
6767761Smsmithstatic int	acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
6867761Smsmithstatic void	acpi_timer_boot_test(void);
6980070Smsmith
7081096Smsmithstatic u_int	acpi_timer_read(void);
7180070Smsmithstatic int	acpi_timer_test(void);
7280070Smsmith
7367761Smsmithstatic device_method_t acpi_timer_methods[] = {
74114277Smarcel    DEVMETHOD(device_identify,	acpi_timer_identify),
75114277Smarcel    DEVMETHOD(device_probe,	acpi_timer_probe),
76114277Smarcel    DEVMETHOD(device_attach,	acpi_timer_attach),
7780070Smsmith
7880070Smsmith    {0, 0}
7980070Smsmith};
8067761Smsmith
8167761Smsmithstatic driver_t acpi_timer_driver = {
8267761Smsmith    "acpi_timer",
8367761Smsmith    acpi_timer_methods,
8467761Smsmith    0,
8567761Smsmith};
8667761Smsmith
8767761Smsmithstatic devclass_t acpi_timer_devclass;
8867761SmsmithDRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
8967761SmsmithMODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
9067761Smsmith
9180070Smsmithstatic struct timecounter acpi_timer_timecounter = {
9267761Smsmith	acpi_timer_get_timecount_safe,	/* get_timecount function */
9367761Smsmith	0,				/* no poll_pps */
9489054Smsmith	0,				/* no default counter_mask */
9567761Smsmith	0,				/* no default frequency */
9667761Smsmith	"ACPI",				/* name */
9780070Smsmith	1000				/* quality */
9880070Smsmith};
9980070Smsmith
10080070Smsmithstatic u_int
10181096Smsmithacpi_timer_read()
10280070Smsmith{
10380070Smsmith    return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0));
10480070Smsmith}
10580070Smsmith
10680070Smsmith/*
10780070Smsmith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
10880070Smsmith * we will be using.
109114277Smarcel */
110114277Smarcelstatic void
111114277Smarcelacpi_timer_identify(driver_t *driver, device_t parent)
112114277Smarcel{
113114277Smarcel    device_t dev;
114114277Smarcel    u_long rlen, rstart;
115114277Smarcel    int rid, rtype;
116114277Smarcel
117114277Smarcel    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
118114277Smarcel
119114277Smarcel    if (acpi_disabled("timer") || AcpiGbl_FADT == NULL || acpi_timer_dev)
120114277Smarcel	return_VOID;
121114277Smarcel
122114277Smarcel    if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) {
12391237Sphk	device_printf(parent, "could not add acpi_timer0\n");
12491237Sphk	return_VOID;
12591237Sphk    }
12691237Sphk    acpi_timer_dev = dev;
12791237Sphk
12891237Sphk    rid = 0;
12991237Sphk    rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ?
13091237Sphk	SYS_RES_IOPORT : SYS_RES_MEMORY;
13191237Sphk    rlen = AcpiGbl_FADT->PmTmLen;
132114277Smarcel    rstart = AcpiGbl_FADT->XPmTmrBlk.Address;
13391237Sphk    if (bus_set_resource(dev, rtype, rid, rstart, rlen))
134114277Smarcel	device_printf(dev, "couldn't set resource (%s 0x%lx+0x%lx)\n",
13591237Sphk	    (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen);
13691237Sphk    return_VOID;
13791237Sphk}
13891237Sphk
13991237Sphkstatic int
14091237Sphkacpi_timer_probe(device_t dev)
14191237Sphk{
14291237Sphk    char desc[40];
14391237Sphk    int i, j, rid, rtype;
144114277Smarcel
14591237Sphk    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
14691237Sphk
14791237Sphk    if (dev != acpi_timer_dev)
14893093Sphk	return (ENXIO);
14993093Sphk
15093093Sphk    rid = 0;
151114277Smarcel    rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ?
15291237Sphk	SYS_RES_IOPORT : SYS_RES_MEMORY;
15391237Sphk    acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
15491237Sphk    if (acpi_timer_reg == NULL) {
15580070Smsmith	device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n",
15680070Smsmith	    (rtype == SYS_RES_IOPORT) ? "port" : "mem",
15780070Smsmith	    (u_long)AcpiGbl_FADT->XPmTmrBlk.Address);
15880070Smsmith	return (ENXIO);
15967761Smsmith    }
16067761Smsmith    if (AcpiGbl_FADT->TmrValExt != 0)
16167761Smsmith	acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
16280070Smsmith    else
16380070Smsmith	acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
164114277Smarcel    acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
165114277Smarcel    if (testenv("debug.acpi.timer_test"))
16667761Smsmith	acpi_timer_boot_test();
16796926Speter
16869744Smsmith    /*
16969744Smsmith     * If all tests of the counter succeed, use the ACPI-fast method.  If
17069744Smsmith     * at least one failed, default to using the safe routine, which reads
17169744Smsmith     * the timer multiple times to get a consistent value before returning.
17271872Smsmith     */
17369744Smsmith    j = 0;
17471872Smsmith    for (i = 0; i < 10; i++)
17567761Smsmith	j += acpi_timer_test();
17667761Smsmith    if (j == 10) {
17769744Smsmith	acpi_timer_timecounter.tc_name = "ACPI-fast";
17867761Smsmith	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
17980070Smsmith    } else {
180114277Smarcel	acpi_timer_timecounter.tc_name = "ACPI-safe";
18180070Smsmith	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
182114277Smarcel    }
183114277Smarcel    tc_init(&acpi_timer_timecounter);
184114277Smarcel
185114277Smarcel    sprintf(desc, "%d-bit timer at 3.579545MHz",
186114277Smarcel	AcpiGbl_FADT->TmrValExt ? 32 : 24);
187114277Smarcel    device_set_desc_copy(dev, desc);
188114277Smarcel
189114277Smarcel    /* Release the resource, we'll allocate it again during attach. */
190114277Smarcel    bus_release_resource(dev, rtype, rid, acpi_timer_reg);
19169744Smsmith    return (0);
19267761Smsmith}
19394936Smux
19480070Smsmithstatic int
19567761Smsmithacpi_timer_attach(device_t dev)
19680070Smsmith{
19791237Sphk    int rid, rtype;
19891237Sphk
19991237Sphk    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
20091237Sphk
20191237Sphk    rid = 0;
20291237Sphk    rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ?
20391237Sphk	SYS_RES_IOPORT : SYS_RES_MEMORY;
20491237Sphk    acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
20591237Sphk    if (acpi_timer_reg == NULL)
20691237Sphk	return (ENXIO);
20780070Smsmith    acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
20880070Smsmith    acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
209114277Smarcel    return (0);
210114277Smarcel}
21167761Smsmith
21269744Smsmith/*
21369744Smsmith * Fetch current time value from reliable hardware.
21467761Smsmith */
21567761Smsmithstatic u_int
21667761Smsmithacpi_timer_get_timecount(struct timecounter *tc)
21767761Smsmith{
21867761Smsmith    return (acpi_timer_read());
21980070Smsmith}
22067761Smsmith
22167761Smsmith/*
22267761Smsmith * Fetch current time value from hardware that may not correctly
22367761Smsmith * latch the counter.  We need to read until we have three monotonic
22467761Smsmith * samples and then use the middle one, otherwise we are not protected
22567761Smsmith * against the fact that the bits can be wrong in two directions.  If
22667761Smsmith * we only cared about monosity, two reads would be enough.
22780070Smsmith */
22880070Smsmithstatic u_int
22967761Smsmithacpi_timer_get_timecount_safe(struct timecounter *tc)
23080070Smsmith{
23181096Smsmith    u_int u1, u2, u3;
23280070Smsmith
23380070Smsmith    u2 = acpi_timer_read();
23480070Smsmith    u3 = acpi_timer_read();
23580070Smsmith    do {
236114277Smarcel	u1 = u2;
23767761Smsmith	u2 = u3;
23880070Smsmith	u3 = acpi_timer_read();
23980070Smsmith    } while (u1 > u2 || u2 > u3);
24081096Smsmith
24181096Smsmith    return (u2);
24281096Smsmith}
24381096Smsmith
24481096Smsmith/*
24581096Smsmith * Timecounter freqency adjustment interface.
24681096Smsmith */
24781096Smsmithstatic int
248114277Smarcelacpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
249114277Smarcel{
25081096Smsmith    int error;
25181096Smsmith    u_int freq;
25281096Smsmith
253114277Smarcel    if (acpi_timer_timecounter.tc_frequency == 0)
25491237Sphk	return (EOPNOTSUPP);
25581096Smsmith    freq = acpi_timer_frequency;
25681096Smsmith    error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
25781096Smsmith    if (error == 0 && req->newptr != NULL) {
25881096Smsmith	acpi_timer_frequency = freq;
25980070Smsmith	acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
26080070Smsmith    }
26180070Smsmith
26280070Smsmith    return (error);
26380070Smsmith}
26480070Smsmith
26580070SmsmithSYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
26680070Smsmith	    0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "");
26780070Smsmith
26880070Smsmith/*
26980070Smsmith * Some ACPI timers are known or believed to suffer from implementation
27080070Smsmith * problems which can lead to erroneous values being read.  This function
27180070Smsmith * tests for consistent results from the timer and returns 1 if it believes
27280070Smsmith * the timer is consistent, otherwise it returns 0.
27380070Smsmith *
27480070Smsmith * It appears the cause is that the counter is not latched to the PCI bus
27580070Smsmith * clock when read:
27680070Smsmith *
27780070Smsmith * ] 20. ACPI Timer Errata
27880070Smsmith * ]
27980070Smsmith * ]   Problem: The power management timer may return improper result when
28080070Smsmith * ]   read. Although the timer value settles properly after incrementing,
28180070Smsmith * ]   while incrementing there is a 3nS window every 69.8nS where the
28280070Smsmith * ]   timer value is indeterminate (a 4.2% chance that the data will be
28380070Smsmith * ]   incorrect when read). As a result, the ACPI free running count up
28480070Smsmith * ]   timer specification is violated due to erroneous reads.  Implication:
28580070Smsmith * ]   System hangs due to the "inaccuracy" of the timer when used by
28680070Smsmith * ]   software for time critical events and delays.
28780070Smsmith * ]
28880070Smsmith * ] Workaround: Read the register twice and compare.
28980070Smsmith * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
290114277Smarcel * ] in the PIIX4M.
291114277Smarcel */
292114277Smarcel#define N 2000
29380070Smsmithstatic int
29480070Smsmithacpi_timer_test()
29580070Smsmith{
29680070Smsmith    uint32_t	last, this;
29780070Smsmith    int		min, max, n, delta;
29880070Smsmith    register_t	s;
29980070Smsmith
30080070Smsmith    min = 10000000;
30180070Smsmith    max = 0;
30280070Smsmith
30380070Smsmith    /* Test the timer with interrupts disabled to get accurate results. */
30480070Smsmith    s = intr_disable();
30580070Smsmith    last = acpi_timer_read();
30680070Smsmith    for (n = 0; n < N; n++) {
307114277Smarcel	this = acpi_timer_read();
30880070Smsmith	delta = acpi_TimerDelta(this, last);
30980070Smsmith	if (delta > max)
31080070Smsmith	    max = delta;
31180602Smsmith	else if (delta < min)
31280602Smsmith	    min = delta;
31380602Smsmith	last = this;
31481096Smsmith    }
31581096Smsmith    intr_restore(s);
31681096Smsmith
31781096Smsmith    if (max - min > 2)
31881096Smsmith	n = 0;
31981096Smsmith    else if (min < 0 || max == 0)
32081096Smsmith	n = 0;
32181096Smsmith    else
32281096Smsmith	n = 1;
32381096Smsmith    if (bootverbose) {
32480602Smsmith	printf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
32580602Smsmith		n ? "GOOD" : "BAD ",
32680602Smsmith		min, max, max - min);
32780602Smsmith    }
32880602Smsmith
32980602Smsmith    return (n);
33080602Smsmith}
33180602Smsmith#undef N
33280602Smsmith
33380602Smsmith/*
33480602Smsmith * Test harness for verifying ACPI timer behaviour.
33580602Smsmith * Boot with debug.acpi.timer_test set to invoke this.
33680602Smsmith */
33780602Smsmithstatic void
33880602Smsmithacpi_timer_boot_test(void)
33980602Smsmith{
34080602Smsmith    uint32_t u1, u2, u3;
34180602Smsmith
34280602Smsmith    u1 = acpi_timer_read();
34380602Smsmith    u2 = acpi_timer_read();
34480602Smsmith    u3 = acpi_timer_read();
34580602Smsmith
34691237Sphk    device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
34780602Smsmith    for (;;) {
34880602Smsmith	/*
34980602Smsmith	 * The failure case is where u3 > u1, but u2 does not fall between
35080602Smsmith	 * the two, ie. it contains garbage.
35180602Smsmith	 */
35280602Smsmith	if (u3 > u1) {
35380602Smsmith	    if (u2 < u1 || u2 > u3)
35480602Smsmith		device_printf(acpi_timer_dev,
35580602Smsmith			      "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
35680602Smsmith			      u1, u2, u3);
35780602Smsmith	}
35880602Smsmith	u1 = u2;
35980602Smsmith	u2 = u3;
36080602Smsmith	u3 = acpi_timer_read();
36180602Smsmith    }
36280602Smsmith}
36380602Smsmith