acpi_timer.c revision 114277
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 114277 2003-04-30 05:27:01Z marcel $
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>
34105281Sjhb#if __FreeBSD_version >= 500000
3580070Smsmith#include <sys/timetc.h>
36105281Sjhb#else
37105281Sjhb#include <sys/time.h>
38105281Sjhb#endif
3967761Smsmith
4080070Smsmith#include <machine/bus.h>
4180070Smsmith#include <machine/resource.h>
4280070Smsmith#include <sys/rman.h>
4380070Smsmith
4467761Smsmith#include "acpi.h"
4567761Smsmith
46104726Sjhb#include <dev/acpica/acpivar.h>
4780070Smsmith#include <pci/pcivar.h>
4867761Smsmith
4969744Smsmith/*
5080070Smsmith * A timecounter based on the free-running ACPI timer.
5180070Smsmith *
5280070Smsmith * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
5380070Smsmith */
5480070Smsmith
5580070Smsmith/*
5669744Smsmith * Hooks for the ACPI CA debugging infrastructure
5769744Smsmith */
5877432Smsmith#define _COMPONENT	ACPI_SYSTEM
5991128SmsmithACPI_MODULE_NAME("TIMER")
6069744Smsmith
6180070Smsmithstatic device_t	acpi_timer_dev;
6280070Smsmithstruct resource	*acpi_timer_reg;
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
74114277Smarcelstatic u_int32_t read_counter(void);
75114277Smarcelstatic int test_counter(void);
76114277Smarcel
7780070Smsmith/*
7880070Smsmith * Driver hung off ACPI.
7980070Smsmith */
8067761Smsmithstatic device_method_t acpi_timer_methods[] = {
8167761Smsmith    DEVMETHOD(device_identify,	acpi_timer_identify),
8267761Smsmith    DEVMETHOD(device_probe,	acpi_timer_probe),
8367761Smsmith    DEVMETHOD(device_attach,	acpi_timer_attach),
8467761Smsmith
8567761Smsmith    {0, 0}
8667761Smsmith};
8767761Smsmith
8867761Smsmithstatic driver_t acpi_timer_driver = {
8967761Smsmith    "acpi_timer",
9067761Smsmith    acpi_timer_methods,
9180070Smsmith    0,
9267761Smsmith};
9367761Smsmith
9489054Smsmithstatic devclass_t acpi_timer_devclass;
9567761SmsmithDRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
9667761Smsmith
9780070Smsmith/*
9880070Smsmith * Timecounter.
9980070Smsmith */
10080070Smsmithstatic struct timecounter acpi_timer_timecounter = {
10181096Smsmith    acpi_timer_get_timecount_safe,
10280070Smsmith    0,
10380070Smsmith    0xffffff,
10480070Smsmith    0,
10580070Smsmith    "ACPI"
10680070Smsmith};
10780070Smsmith
10880070Smsmith
109114277Smarcelstatic u_int32_t
110114277Smarcelread_counter()
111114277Smarcel{
112114277Smarcel	bus_space_handle_t bsh;
113114277Smarcel	bus_space_tag_t bst;
114114277Smarcel	u_int32_t tv;
115114277Smarcel
116114277Smarcel	bsh = rman_get_bushandle(acpi_timer_reg);
117114277Smarcel	bst = rman_get_bustag(acpi_timer_reg);
118114277Smarcel	tv = bus_space_read_4(bst, bsh, 0);
119114277Smarcel	bus_space_barrier(bst, bsh, 0, 4, BUS_SPACE_BARRIER_READ);
120114277Smarcel	return (tv);
121114277Smarcel}
122114277Smarcel
12391237Sphk#define N 2000
12491237Sphkstatic int
12591237Sphktest_counter()
12691237Sphk{
12791237Sphk	int min, max, n, delta;
12891237Sphk	unsigned last, this;
12991237Sphk
13091237Sphk	min = 10000000;
13191237Sphk	max = 0;
132114277Smarcel	last = read_counter();
13391237Sphk	for (n = 0; n < N; n++) {
134114277Smarcel		this = read_counter();
13591237Sphk		delta = (this - last) & 0xffffff;
13691237Sphk		if (delta > max)
13791237Sphk			max = delta;
13891237Sphk		else if (delta < min)
13991237Sphk			min = delta;
14091237Sphk		last = this;
14191237Sphk	}
14291237Sphk	if (max - min > 2)
14391237Sphk		n = 0;
144114277Smarcel	else if (min < 0 || max == 0)
14591237Sphk		n = 0;
14691237Sphk	else
14791237Sphk		n = 1;
14893093Sphk	if (bootverbose)
14993093Sphk		printf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
15093093Sphk			n ? "GOOD" : "BAD ",
151114277Smarcel			min, max, max - min);
15291237Sphk	return (n);
15391237Sphk}
15491237Sphk
15580070Smsmith/*
15680070Smsmith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
15780070Smsmith * we will be using.
15880070Smsmith */
15967761Smsmithstatic void
16067761Smsmithacpi_timer_identify(driver_t *driver, device_t parent)
16167761Smsmith{
16280070Smsmith    device_t	dev;
16380070Smsmith    char	desc[40];
164114277Smarcel    u_long	rlen, rstart;
165114277Smarcel    int		i, j, rid, rtype;
16667761Smsmith
16796926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
16869744Smsmith
16969744Smsmith    if (acpi_disabled("timer"))
17069744Smsmith	return_VOID;
17169744Smsmith
17271872Smsmith    if (AcpiGbl_FADT == NULL)
17369744Smsmith	return_VOID;
17471872Smsmith
17567761Smsmith    if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) {
17667761Smsmith	device_printf(parent, "could not add acpi_timer0\n");
17769744Smsmith	return_VOID;
17867761Smsmith    }
17980070Smsmith    acpi_timer_dev = dev;
180114277Smarcel
18180070Smsmith    rid = 0;
182114277Smarcel    rlen = AcpiGbl_FADT->PmTmLen;
183114277Smarcel    rtype = (AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId)
184114277Smarcel      ? SYS_RES_IOPORT : SYS_RES_MEMORY;
185114277Smarcel    rstart = AcpiGbl_FADT->XPmTmrBlk.Address;
186114277Smarcel    bus_set_resource(dev, rtype, rid, rstart, rlen);
187114277Smarcel    acpi_timer_reg = bus_alloc_resource(dev, rtype, &rid, 0, ~0, 1, RF_ACTIVE);
188114277Smarcel    if (acpi_timer_reg == NULL) {
189114277Smarcel	device_printf(dev, "couldn't allocate I/O resource (%s 0x%lx)\n",
190114277Smarcel	  (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart);
19169744Smsmith	return_VOID;
19267761Smsmith    }
19394936Smux    if (testenv("debug.acpi.timer_test"))
19480070Smsmith	acpi_timer_test();
19567761Smsmith
19680070Smsmith    acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
19791237Sphk    j = 0;
19891237Sphk    for(i = 0; i < 10; i++)
19991237Sphk	j += test_counter();
20091237Sphk    if (j == 10) {
20191237Sphk	acpi_timer_timecounter.tc_name = "ACPI-fast";
20291237Sphk	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
20391237Sphk    } else {
20491237Sphk	acpi_timer_timecounter.tc_name = "ACPI-safe";
20591237Sphk	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
20691237Sphk    }
20780070Smsmith    tc_init(&acpi_timer_timecounter);
20880070Smsmith
209114277Smarcel    sprintf(desc, "%d-bit timer at 3.579545MHz", (AcpiGbl_FADT->TmrValExt)
210114277Smarcel      ? 32 : 24);
21167761Smsmith    device_set_desc_copy(dev, desc);
21269744Smsmith
21369744Smsmith    return_VOID;
21467761Smsmith}
21567761Smsmith
21667761Smsmithstatic int
21767761Smsmithacpi_timer_probe(device_t dev)
21867761Smsmith{
21980070Smsmith    if (dev == acpi_timer_dev)
22067761Smsmith	return(0);
22167761Smsmith    return(ENXIO);
22267761Smsmith}
22367761Smsmith
22467761Smsmithstatic int
22567761Smsmithacpi_timer_attach(device_t dev)
22667761Smsmith{
22780070Smsmith    return(0);
22880070Smsmith}
22967761Smsmith
23080070Smsmith/*
23181096Smsmith * Fetch current time value from reliable hardware.
23280070Smsmith */
23380070Smsmithstatic unsigned
23480070Smsmithacpi_timer_get_timecount(struct timecounter *tc)
23580070Smsmith{
236114277Smarcel    return (read_counter());
23767761Smsmith}
23880070Smsmith
23980070Smsmith/*
24081096Smsmith * Fetch current time value from hardware that may not correctly
24181096Smsmith * latch the counter.
24281096Smsmith */
24381096Smsmithstatic unsigned
24481096Smsmithacpi_timer_get_timecount_safe(struct timecounter *tc)
24581096Smsmith{
24681096Smsmith    unsigned u1, u2, u3;
24781096Smsmith
248114277Smarcel    u2 = read_counter();
249114277Smarcel    u3 = read_counter();
25081096Smsmith    do {
25181096Smsmith	u1 = u2;
25281096Smsmith	u2 = u3;
253114277Smarcel	u3 = read_counter();
25491237Sphk    } while (u1 > u2 || u2 > u3 || (u3 - u1) > 15);
25581096Smsmith    return (u2);
25681096Smsmith}
25781096Smsmith
25881096Smsmith/*
25980070Smsmith * Timecounter freqency adjustment interface.
26080070Smsmith */
26180070Smsmithstatic int
26280070Smsmithacpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
26380070Smsmith{
26480070Smsmith    int error;
26580070Smsmith    u_int freq;
26680070Smsmith
26780070Smsmith    if (acpi_timer_timecounter.tc_frequency == 0)
26880070Smsmith	return (EOPNOTSUPP);
26980070Smsmith    freq = acpi_timer_frequency;
27080070Smsmith    error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
27180070Smsmith    if (error == 0 && req->newptr != NULL) {
27280070Smsmith	acpi_timer_frequency = freq;
27380070Smsmith	acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
27480070Smsmith    }
27580070Smsmith    return (error);
27680070Smsmith}
27780070Smsmith
27880070SmsmithSYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
27980070Smsmith	    0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "");
28080070Smsmith
28180070Smsmith/*
28280070Smsmith * Test harness for verifying ACPI timer behaviour.
28380070Smsmith * Boot with debug.acpi.timer_test set to invoke this.
28480070Smsmith */
28580070Smsmithstatic void
28680070Smsmithacpi_timer_test(void)
28780070Smsmith{
28880070Smsmith    u_int32_t	u1, u2, u3;
28980070Smsmith
290114277Smarcel    u1 = read_counter();
291114277Smarcel    u2 = read_counter();
292114277Smarcel    u3 = read_counter();
29380070Smsmith
29480070Smsmith    device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
29580070Smsmith    for (;;) {
29680070Smsmith	/*
29780070Smsmith	 * The failure case is where u3 > u1, but u2 does not fall between the two,
29880070Smsmith	 * ie. it contains garbage.
29980070Smsmith	 */
30080070Smsmith	if (u3 > u1) {
30180070Smsmith	    if ((u2 < u1) || (u2 > u3))
30280070Smsmith		device_printf(acpi_timer_dev, "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
30380070Smsmith			      u1, u2, u3);
30480070Smsmith	}
30580070Smsmith	u1 = u2;
30680070Smsmith	u2 = u3;
307114277Smarcel	u3 = read_counter();
30880070Smsmith    }
30980070Smsmith}
31080070Smsmith
31180602Smsmith/*
31280602Smsmith * Chipset workaround driver hung off PCI.
31380602Smsmith *
31481096Smsmith * Some ACPI timers are known or believed to suffer from implementation
31581096Smsmith * problems which can lead to erroneous values being read from the timer.
31681096Smsmith *
31781096Smsmith * Since we can't trust unknown chipsets, we default to a timer-read
31881096Smsmith * routine which compensates for the most common problem (as detailed
31981096Smsmith * in the excerpt from the Intel PIIX4 datasheet below).
32081096Smsmith *
32181096Smsmith * When we detect a known-functional chipset, we disable the workaround
32281096Smsmith * to improve speed.
32381096Smsmith *
32480602Smsmith * ] 20. ACPI Timer Errata
32580602Smsmith * ]
32680602Smsmith * ]   Problem: The power management timer may return improper result when
32780602Smsmith * ]   read. Although the timer value settles properly after incrementing,
32880602Smsmith * ]   while incrementing there is a 3nS window every 69.8nS where the
32980602Smsmith * ]   timer value is indeterminate (a 4.2% chance that the data will be
33080602Smsmith * ]   incorrect when read). As a result, the ACPI free running count up
33180602Smsmith * ]   timer specification is violated due to erroneous reads.  Implication:
33280602Smsmith * ]   System hangs due to the "inaccuracy" of the timer when used by
33380602Smsmith * ]   software for time critical events and delays.
33480602Smsmith * ]
33580602Smsmith * ] Workaround: Read the register twice and compare.
33680602Smsmith * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
33780602Smsmith * ] in the PIIX4M.
33880602Smsmith *
33980602Smsmith * The counter is in other words not latched to the PCI bus clock when
34080602Smsmith * read.  Notice the workaround isn't:  We need to read until we have
34180602Smsmith * three monotonic samples and then use the middle one, otherwise we are
34280602Smsmith * not protected against the fact that the bits can be wrong in two
34380602Smsmith * directions.  If we only cared about monosity two reads would be enough.
34480602Smsmith */
34580602Smsmith
34691237Sphk#if 0
34780602Smsmithstatic int	acpi_timer_pci_probe(device_t dev);
34880602Smsmith
34980602Smsmithstatic device_method_t acpi_timer_pci_methods[] = {
35080602Smsmith    DEVMETHOD(device_probe,	acpi_timer_pci_probe),
35180602Smsmith    {0, 0}
35280602Smsmith};
35380602Smsmith
35480602Smsmithstatic driver_t acpi_timer_pci_driver = {
35580602Smsmith    "acpi_timer_pci",
35680602Smsmith    acpi_timer_pci_methods,
35780602Smsmith    0,
35880602Smsmith};
35980602Smsmith
36080602Smsmithdevclass_t acpi_timer_pci_devclass;
36180602SmsmithDRIVER_MODULE(acpi_timer_pci, pci, acpi_timer_pci_driver, acpi_timer_pci_devclass, 0, 0);
36280602Smsmith
36380602Smsmith/*
36481096Smsmith * Look at PCI devices going past; if we detect one we know contains
36581096Smsmith * a functional ACPI timer device, enable the faster timecounter read
36681096Smsmith * routine.
36780602Smsmith */
36880602Smsmithstatic int
36980602Smsmithacpi_timer_pci_probe(device_t dev)
37080602Smsmith{
37181172Smsmith    int vendor, device, revid;
37281172Smsmith
37381172Smsmith    vendor = pci_get_vendor(dev);
37481172Smsmith    device = pci_get_device(dev);
37581172Smsmith    revid  = pci_get_revid(dev);
37681172Smsmith
37781172Smsmith    if (((vendor == 0x8086) && (device == 0x7113) && (revid >= 0x03))	|| /* PIIX4M */
37881172Smsmith	((vendor == 0x8086) && (device == 0x719b)) 			|| /* i440MX */
37981172Smsmith	0) {
38081172Smsmith
38181096Smsmith	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
38281172Smsmith	acpi_timer_timecounter.tc_name = "ACPI-fast";
38381096Smsmith	if (bootverbose)
38481172Smsmith	    device_printf(acpi_timer_dev, "functional ACPI timer detected, enabling fast timecount interface\n");
38580602Smsmith    }
38680602Smsmith
38780602Smsmith    return(ENXIO);		/* we never match anything */
38880602Smsmith}
38991237Sphk#endif
390