acpi_timer.c revision 94936
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 94936 2002-04-17 13:06:36Z mux $
2867761Smsmith */
2967761Smsmith#include "opt_acpi.h"
3067761Smsmith#include <sys/param.h>
3180070Smsmith#include <sys/bus.h>
3267761Smsmith#include <sys/kernel.h>
3380070Smsmith#include <sys/sysctl.h>
3480070Smsmith#include <sys/timetc.h>
3567761Smsmith
3680070Smsmith#include <machine/bus_pio.h>
3780070Smsmith#include <machine/bus.h>
3880070Smsmith#include <machine/resource.h>
3980070Smsmith#include <sys/rman.h>
4080070Smsmith
4167761Smsmith#include "acpi.h"
4267761Smsmith
4380070Smsmith#include <acpica/acpivar.h>
4480070Smsmith#include <pci/pcivar.h>
4567761Smsmith
4669744Smsmith/*
4780070Smsmith * A timecounter based on the free-running ACPI timer.
4880070Smsmith *
4980070Smsmith * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
5080070Smsmith */
5180070Smsmith
5280070Smsmith/*
5369744Smsmith * Hooks for the ACPI CA debugging infrastructure
5469744Smsmith */
5577432Smsmith#define _COMPONENT	ACPI_SYSTEM
5691128SmsmithACPI_MODULE_NAME("TIMER")
5769744Smsmith
5880070Smsmithstatic device_t	acpi_timer_dev;
5980070Smsmithstruct resource	*acpi_timer_reg;
6080070Smsmith#define TIMER_READ	bus_space_read_4(rman_get_bustag(acpi_timer_reg),	\
6180070Smsmith					 rman_get_bushandle(acpi_timer_reg),	\
6280070Smsmith					 0)
6367761Smsmith
6480070Smsmithstatic u_int	acpi_timer_frequency = 14318182/4;
6567761Smsmith
6667761Smsmithstatic void	acpi_timer_identify(driver_t *driver, device_t parent);
6767761Smsmithstatic int	acpi_timer_probe(device_t dev);
6867761Smsmithstatic int	acpi_timer_attach(device_t dev);
6980070Smsmithstatic unsigned	acpi_timer_get_timecount(struct timecounter *tc);
7081096Smsmithstatic unsigned	acpi_timer_get_timecount_safe(struct timecounter *tc);
7180070Smsmithstatic int	acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
7280070Smsmithstatic void	acpi_timer_test(void);
7367761Smsmith
7480070Smsmith/*
7580070Smsmith * Driver hung off ACPI.
7680070Smsmith */
7767761Smsmithstatic device_method_t acpi_timer_methods[] = {
7867761Smsmith    DEVMETHOD(device_identify,	acpi_timer_identify),
7967761Smsmith    DEVMETHOD(device_probe,	acpi_timer_probe),
8067761Smsmith    DEVMETHOD(device_attach,	acpi_timer_attach),
8167761Smsmith
8267761Smsmith    {0, 0}
8367761Smsmith};
8467761Smsmith
8567761Smsmithstatic driver_t acpi_timer_driver = {
8667761Smsmith    "acpi_timer",
8767761Smsmith    acpi_timer_methods,
8880070Smsmith    0,
8967761Smsmith};
9067761Smsmith
9189054Smsmithstatic devclass_t acpi_timer_devclass;
9267761SmsmithDRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
9367761Smsmith
9480070Smsmith/*
9580070Smsmith * Timecounter.
9680070Smsmith */
9780070Smsmithstatic struct timecounter acpi_timer_timecounter = {
9881096Smsmith    acpi_timer_get_timecount_safe,
9980070Smsmith    0,
10080070Smsmith    0xffffff,
10180070Smsmith    0,
10280070Smsmith    "ACPI"
10380070Smsmith};
10480070Smsmith
10580070SmsmithSYSCTL_OPAQUE(_debug, OID_AUTO, acpi_timecounter, CTLFLAG_RD,
10680070Smsmith	      &acpi_timer_timecounter, sizeof(acpi_timer_timecounter), "S,timecounter", "");
10791237Sphkstatic int test_counter(void);
10880070Smsmith
10991237Sphk#define N 2000
11091237Sphkstatic int
11191237Sphktest_counter()
11291237Sphk{
11391237Sphk	int min, max, n, delta;
11491237Sphk	unsigned last, this;
11591237Sphk
11691237Sphk	min = 10000000;
11791237Sphk	max = 0;
11891237Sphk	last = TIMER_READ;
11991237Sphk	for (n = 0; n < N; n++) {
12091237Sphk		this = TIMER_READ;
12191237Sphk		delta = (this - last) & 0xffffff;
12291237Sphk		if (delta > max)
12391237Sphk			max = delta;
12491237Sphk		else if (delta < min)
12591237Sphk			min = delta;
12691237Sphk		last = this;
12791237Sphk	}
12891237Sphk	if (max - min > 2)
12991237Sphk		n = 0;
13091237Sphk	else if (min < 0)
13191237Sphk		n = 0;
13291237Sphk	else
13391237Sphk		n = 1;
13493093Sphk	if (bootverbose)
13593093Sphk		printf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
13693093Sphk			n ? "GOOD" : "BAD ",
13793093Sphk			min, max, max - min + 1);
13891237Sphk	return (n);
13991237Sphk}
14091237Sphk
14180070Smsmith/*
14280070Smsmith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
14380070Smsmith * we will be using.
14480070Smsmith */
14567761Smsmithstatic void
14667761Smsmithacpi_timer_identify(driver_t *driver, device_t parent)
14767761Smsmith{
14880070Smsmith    device_t	dev;
14980070Smsmith    char	desc[40];
15091237Sphk    int		rid, i, j;
15167761Smsmith
15291128Smsmith    ACPI_FUNCTION_TRACE(__func__);
15369744Smsmith
15469744Smsmith    if (acpi_disabled("timer"))
15569744Smsmith	return_VOID;
15669744Smsmith
15771872Smsmith    if (AcpiGbl_FADT == NULL)
15869744Smsmith	return_VOID;
15971872Smsmith
16067761Smsmith    if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) {
16167761Smsmith	device_printf(parent, "could not add acpi_timer0\n");
16269744Smsmith	return_VOID;
16367761Smsmith    }
16480070Smsmith    acpi_timer_dev = dev;
16580070Smsmith    rid = 0;
16680070Smsmith    bus_set_resource(dev, SYS_RES_IOPORT, rid, AcpiGbl_FADT->V1_PmTmrBlk, sizeof(u_int32_t));
16780070Smsmith    if ((acpi_timer_reg = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE)) == NULL) {
16880070Smsmith	device_printf(dev, "couldn't allocate I/O resource (port 0x%x)\n", AcpiGbl_FADT->V1_PmTmrBlk);
16969744Smsmith	return_VOID;
17067761Smsmith    }
17194936Smux    if (testenv("debug.acpi.timer_test"))
17280070Smsmith	acpi_timer_test();
17367761Smsmith
17480070Smsmith    acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
17591237Sphk    j = 0;
17691237Sphk    for(i = 0; i < 10; i++)
17791237Sphk	j += test_counter();
17891237Sphk    if (j == 10) {
17991237Sphk	acpi_timer_timecounter.tc_name = "ACPI-fast";
18091237Sphk	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
18191237Sphk    } else {
18291237Sphk	acpi_timer_timecounter.tc_name = "ACPI-safe";
18391237Sphk	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
18491237Sphk    }
18580070Smsmith    tc_init(&acpi_timer_timecounter);
18680070Smsmith
18771872Smsmith    sprintf(desc, "%d-bit timer at 3.579545MHz", AcpiGbl_FADT->TmrValExt ? 32 : 24);
18867761Smsmith    device_set_desc_copy(dev, desc);
18969744Smsmith
19069744Smsmith    return_VOID;
19167761Smsmith}
19267761Smsmith
19367761Smsmithstatic int
19467761Smsmithacpi_timer_probe(device_t dev)
19567761Smsmith{
19680070Smsmith    if (dev == acpi_timer_dev)
19767761Smsmith	return(0);
19867761Smsmith    return(ENXIO);
19967761Smsmith}
20067761Smsmith
20167761Smsmithstatic int
20267761Smsmithacpi_timer_attach(device_t dev)
20367761Smsmith{
20480070Smsmith    return(0);
20580070Smsmith}
20667761Smsmith
20780070Smsmith/*
20881096Smsmith * Fetch current time value from reliable hardware.
20980070Smsmith */
21080070Smsmithstatic unsigned
21180070Smsmithacpi_timer_get_timecount(struct timecounter *tc)
21280070Smsmith{
21380602Smsmith    return(TIMER_READ);
21467761Smsmith}
21580070Smsmith
21680070Smsmith/*
21781096Smsmith * Fetch current time value from hardware that may not correctly
21881096Smsmith * latch the counter.
21981096Smsmith */
22081096Smsmithstatic unsigned
22181096Smsmithacpi_timer_get_timecount_safe(struct timecounter *tc)
22281096Smsmith{
22381096Smsmith    unsigned u1, u2, u3;
22481096Smsmith
22581096Smsmith    u2 = TIMER_READ;
22681096Smsmith    u3 = TIMER_READ;
22781096Smsmith    do {
22881096Smsmith	u1 = u2;
22981096Smsmith	u2 = u3;
23081096Smsmith	u3 = TIMER_READ;
23191237Sphk    } while (u1 > u2 || u2 > u3 || (u3 - u1) > 15);
23281096Smsmith    return (u2);
23381096Smsmith}
23481096Smsmith
23581096Smsmith/*
23680070Smsmith * Timecounter freqency adjustment interface.
23780070Smsmith */
23880070Smsmithstatic int
23980070Smsmithacpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
24080070Smsmith{
24180070Smsmith    int error;
24280070Smsmith    u_int freq;
24380070Smsmith
24480070Smsmith    if (acpi_timer_timecounter.tc_frequency == 0)
24580070Smsmith	return (EOPNOTSUPP);
24680070Smsmith    freq = acpi_timer_frequency;
24780070Smsmith    error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
24880070Smsmith    if (error == 0 && req->newptr != NULL) {
24980070Smsmith	acpi_timer_frequency = freq;
25080070Smsmith	acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
25180070Smsmith	tc_update(&acpi_timer_timecounter);
25280070Smsmith    }
25380070Smsmith    return (error);
25480070Smsmith}
25580070Smsmith
25680070SmsmithSYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
25780070Smsmith	    0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "");
25880070Smsmith
25980070Smsmith/*
26080070Smsmith * Test harness for verifying ACPI timer behaviour.
26180070Smsmith * Boot with debug.acpi.timer_test set to invoke this.
26280070Smsmith */
26380070Smsmithstatic void
26480070Smsmithacpi_timer_test(void)
26580070Smsmith{
26680070Smsmith    u_int32_t	u1, u2, u3;
26780070Smsmith
26880070Smsmith    u1 = TIMER_READ;
26980070Smsmith    u2 = TIMER_READ;
27080070Smsmith    u3 = TIMER_READ;
27180070Smsmith
27280070Smsmith    device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
27380070Smsmith    for (;;) {
27480070Smsmith	/*
27580070Smsmith	 * The failure case is where u3 > u1, but u2 does not fall between the two,
27680070Smsmith	 * ie. it contains garbage.
27780070Smsmith	 */
27880070Smsmith	if (u3 > u1) {
27980070Smsmith	    if ((u2 < u1) || (u2 > u3))
28080070Smsmith		device_printf(acpi_timer_dev, "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
28180070Smsmith			      u1, u2, u3);
28280070Smsmith	}
28380070Smsmith	u1 = u2;
28480070Smsmith	u2 = u3;
28580070Smsmith	u3 = TIMER_READ;
28680070Smsmith    }
28780070Smsmith}
28880070Smsmith
28980602Smsmith/*
29080602Smsmith * Chipset workaround driver hung off PCI.
29180602Smsmith *
29281096Smsmith * Some ACPI timers are known or believed to suffer from implementation
29381096Smsmith * problems which can lead to erroneous values being read from the timer.
29481096Smsmith *
29581096Smsmith * Since we can't trust unknown chipsets, we default to a timer-read
29681096Smsmith * routine which compensates for the most common problem (as detailed
29781096Smsmith * in the excerpt from the Intel PIIX4 datasheet below).
29881096Smsmith *
29981096Smsmith * When we detect a known-functional chipset, we disable the workaround
30081096Smsmith * to improve speed.
30181096Smsmith *
30280602Smsmith * ] 20. ACPI Timer Errata
30380602Smsmith * ]
30480602Smsmith * ]   Problem: The power management timer may return improper result when
30580602Smsmith * ]   read. Although the timer value settles properly after incrementing,
30680602Smsmith * ]   while incrementing there is a 3nS window every 69.8nS where the
30780602Smsmith * ]   timer value is indeterminate (a 4.2% chance that the data will be
30880602Smsmith * ]   incorrect when read). As a result, the ACPI free running count up
30980602Smsmith * ]   timer specification is violated due to erroneous reads.  Implication:
31080602Smsmith * ]   System hangs due to the "inaccuracy" of the timer when used by
31180602Smsmith * ]   software for time critical events and delays.
31280602Smsmith * ]
31380602Smsmith * ] Workaround: Read the register twice and compare.
31480602Smsmith * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
31580602Smsmith * ] in the PIIX4M.
31680602Smsmith *
31780602Smsmith * The counter is in other words not latched to the PCI bus clock when
31880602Smsmith * read.  Notice the workaround isn't:  We need to read until we have
31980602Smsmith * three monotonic samples and then use the middle one, otherwise we are
32080602Smsmith * not protected against the fact that the bits can be wrong in two
32180602Smsmith * directions.  If we only cared about monosity two reads would be enough.
32280602Smsmith */
32380602Smsmith
32491237Sphk#if 0
32580602Smsmithstatic int	acpi_timer_pci_probe(device_t dev);
32680602Smsmith
32780602Smsmithstatic device_method_t acpi_timer_pci_methods[] = {
32880602Smsmith    DEVMETHOD(device_probe,	acpi_timer_pci_probe),
32980602Smsmith    {0, 0}
33080602Smsmith};
33180602Smsmith
33280602Smsmithstatic driver_t acpi_timer_pci_driver = {
33380602Smsmith    "acpi_timer_pci",
33480602Smsmith    acpi_timer_pci_methods,
33580602Smsmith    0,
33680602Smsmith};
33780602Smsmith
33880602Smsmithdevclass_t acpi_timer_pci_devclass;
33980602SmsmithDRIVER_MODULE(acpi_timer_pci, pci, acpi_timer_pci_driver, acpi_timer_pci_devclass, 0, 0);
34080602Smsmith
34180602Smsmith/*
34281096Smsmith * Look at PCI devices going past; if we detect one we know contains
34381096Smsmith * a functional ACPI timer device, enable the faster timecounter read
34481096Smsmith * routine.
34580602Smsmith */
34680602Smsmithstatic int
34780602Smsmithacpi_timer_pci_probe(device_t dev)
34880602Smsmith{
34981172Smsmith    int vendor, device, revid;
35081172Smsmith
35181172Smsmith    vendor = pci_get_vendor(dev);
35281172Smsmith    device = pci_get_device(dev);
35381172Smsmith    revid  = pci_get_revid(dev);
35481172Smsmith
35581172Smsmith    if (((vendor == 0x8086) && (device == 0x7113) && (revid >= 0x03))	|| /* PIIX4M */
35681172Smsmith	((vendor == 0x8086) && (device == 0x719b)) 			|| /* i440MX */
35781172Smsmith	0) {
35881172Smsmith
35981096Smsmith	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
36081172Smsmith	acpi_timer_timecounter.tc_name = "ACPI-fast";
36181096Smsmith	if (bootverbose)
36281172Smsmith	    device_printf(acpi_timer_dev, "functional ACPI timer detected, enabling fast timecount interface\n");
36380602Smsmith    }
36480602Smsmith
36580602Smsmith    return(ENXIO);		/* we never match anything */
36680602Smsmith}
36791237Sphk#endif
368