acpi_timer.c revision 128071
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 128071 2004-04-09 18:14:32Z 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/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"
45104726Sjhb#include <dev/acpica/acpivar.h>
46119281Simp#include <dev/pci/pcivar.h>
4767761Smsmith
4869744Smsmith/*
4980070Smsmith * A timecounter based on the free-running ACPI timer.
5080070Smsmith *
5180070Smsmith * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
5280070Smsmith */
5380070Smsmith
54119529Snjl/* Hooks for the ACPI CA debugging infrastructure */
55126517Snjl#define _COMPONENT	ACPI_TIMER
5691128SmsmithACPI_MODULE_NAME("TIMER")
5769744Smsmith
5880070Smsmithstatic device_t	acpi_timer_dev;
5980070Smsmithstruct resource	*acpi_timer_reg;
6067761Smsmith
61119529Snjlstatic u_int	acpi_timer_frequency = 14318182 / 4;
6267761Smsmith
6367761Smsmithstatic void	acpi_timer_identify(driver_t *driver, device_t parent);
6467761Smsmithstatic int	acpi_timer_probe(device_t dev);
6567761Smsmithstatic int	acpi_timer_attach(device_t dev);
6680070Smsmithstatic unsigned	acpi_timer_get_timecount(struct timecounter *tc);
6781096Smsmithstatic unsigned	acpi_timer_get_timecount_safe(struct timecounter *tc);
6880070Smsmithstatic int	acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
6980070Smsmithstatic void	acpi_timer_test(void);
7067761Smsmith
71119529Snjlstatic uint32_t read_counter(void);
72119529Snjlstatic int	test_counter(void);
73114277Smarcel
7467761Smsmithstatic device_method_t acpi_timer_methods[] = {
7567761Smsmith    DEVMETHOD(device_identify,	acpi_timer_identify),
7667761Smsmith    DEVMETHOD(device_probe,	acpi_timer_probe),
7767761Smsmith    DEVMETHOD(device_attach,	acpi_timer_attach),
7867761Smsmith
7967761Smsmith    {0, 0}
8067761Smsmith};
8167761Smsmith
8267761Smsmithstatic driver_t acpi_timer_driver = {
8367761Smsmith    "acpi_timer",
8467761Smsmith    acpi_timer_methods,
8580070Smsmith    0,
8667761Smsmith};
8767761Smsmith
8889054Smsmithstatic devclass_t acpi_timer_devclass;
8967761SmsmithDRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
90128071SnjlMODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
9167761Smsmith
9280070Smsmithstatic struct timecounter acpi_timer_timecounter = {
93118987Sphk	acpi_timer_get_timecount_safe,
94118987Sphk	0,
95118987Sphk	0xffffff,
96118987Sphk	0,
97118987Sphk	"ACPI",
98118987Sphk	1000
9980070Smsmith};
10080070Smsmith
101119529Snjlstatic uint32_t
102114277Smarcelread_counter()
103114277Smarcel{
104119529Snjl    bus_space_handle_t bsh;
105119529Snjl    bus_space_tag_t bst;
106119529Snjl    u_int32_t tv;
107114277Smarcel
108119529Snjl    bsh = rman_get_bushandle(acpi_timer_reg);
109119529Snjl    bst = rman_get_bustag(acpi_timer_reg);
110119529Snjl    tv = bus_space_read_4(bst, bsh, 0);
111119529Snjl    bus_space_barrier(bst, bsh, 0, 4, BUS_SPACE_BARRIER_READ);
112119529Snjl
113119529Snjl    return (tv);
114114277Smarcel}
115114277Smarcel
11691237Sphk#define N 2000
11791237Sphkstatic int
11891237Sphktest_counter()
11991237Sphk{
120119529Snjl    u_int	last, this;
121119529Snjl    int		min, max, n, delta;
12291237Sphk
123119529Snjl    min = 10000000;
124119529Snjl    max = 0;
125119529Snjl    last = read_counter();
126119529Snjl    for (n = 0; n < N; n++) {
127119529Snjl	this = read_counter();
128119529Snjl	delta = (this - last) & 0xffffff;
129119529Snjl	if (delta > max)
130119529Snjl	    max = delta;
131119529Snjl	else if (delta < min)
132119529Snjl	    min = delta;
133119529Snjl	last = this;
134119529Snjl    }
135119529Snjl    if (max - min > 2)
136119529Snjl	n = 0;
137119529Snjl    else if (min < 0 || max == 0)
138119529Snjl	n = 0;
139119529Snjl    else
140119529Snjl	n = 1;
141119529Snjl    if (bootverbose) {
142119529Snjl	printf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
143119529Snjl		n ? "GOOD" : "BAD ",
144119529Snjl		min, max, max - min);
145119529Snjl    }
146119529Snjl
147119529Snjl    return (n);
14891237Sphk}
149119529Snjl#undef N
15091237Sphk
15180070Smsmith/*
15280070Smsmith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
15380070Smsmith * we will be using.
15480070Smsmith */
15567761Smsmithstatic void
15667761Smsmithacpi_timer_identify(driver_t *driver, device_t parent)
15767761Smsmith{
15880070Smsmith    device_t	dev;
15980070Smsmith    char	desc[40];
160114277Smarcel    u_long	rlen, rstart;
161114277Smarcel    int		i, j, rid, rtype;
16267761Smsmith
16396926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
16469744Smsmith
165119529Snjl    if (acpi_disabled("timer") || AcpiGbl_FADT == NULL)
16669744Smsmith	return_VOID;
16771872Smsmith
16867761Smsmith    if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) {
16967761Smsmith	device_printf(parent, "could not add acpi_timer0\n");
17069744Smsmith	return_VOID;
17167761Smsmith    }
17280070Smsmith    acpi_timer_dev = dev;
173114277Smarcel
17480070Smsmith    rid = 0;
175114277Smarcel    rlen = AcpiGbl_FADT->PmTmLen;
176114277Smarcel    rtype = (AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId)
177114277Smarcel      ? SYS_RES_IOPORT : SYS_RES_MEMORY;
178114277Smarcel    rstart = AcpiGbl_FADT->XPmTmrBlk.Address;
179114277Smarcel    bus_set_resource(dev, rtype, rid, rstart, rlen);
180127135Snjl    acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
181114277Smarcel    if (acpi_timer_reg == NULL) {
182114277Smarcel	device_printf(dev, "couldn't allocate I/O resource (%s 0x%lx)\n",
183119529Snjl		      rtype == SYS_RES_IOPORT ? "port" : "mem", rstart);
18469744Smsmith	return_VOID;
18567761Smsmith    }
18694936Smux    if (testenv("debug.acpi.timer_test"))
18780070Smsmith	acpi_timer_test();
18867761Smsmith
18980070Smsmith    acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
19091237Sphk    j = 0;
19191237Sphk    for(i = 0; i < 10; i++)
19291237Sphk	j += test_counter();
19391237Sphk    if (j == 10) {
19491237Sphk	acpi_timer_timecounter.tc_name = "ACPI-fast";
19591237Sphk	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
19691237Sphk    } else {
19791237Sphk	acpi_timer_timecounter.tc_name = "ACPI-safe";
19891237Sphk	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
19991237Sphk    }
20080070Smsmith    tc_init(&acpi_timer_timecounter);
20180070Smsmith
202119529Snjl    sprintf(desc, "%d-bit timer at 3.579545MHz",
203119529Snjl	    AcpiGbl_FADT->TmrValExt ? 32 : 24);
20467761Smsmith    device_set_desc_copy(dev, desc);
20569744Smsmith
20669744Smsmith    return_VOID;
20767761Smsmith}
20867761Smsmith
20967761Smsmithstatic int
21067761Smsmithacpi_timer_probe(device_t dev)
21167761Smsmith{
21280070Smsmith    if (dev == acpi_timer_dev)
213119529Snjl	return (0);
214119529Snjl
215119529Snjl    return (ENXIO);
21667761Smsmith}
21767761Smsmith
21867761Smsmithstatic int
21967761Smsmithacpi_timer_attach(device_t dev)
22067761Smsmith{
221119529Snjl    return (0);
22280070Smsmith}
22367761Smsmith
22480070Smsmith/*
22581096Smsmith * Fetch current time value from reliable hardware.
22680070Smsmith */
22780070Smsmithstatic unsigned
22880070Smsmithacpi_timer_get_timecount(struct timecounter *tc)
22980070Smsmith{
230114277Smarcel    return (read_counter());
23167761Smsmith}
23280070Smsmith
23380070Smsmith/*
23481096Smsmith * Fetch current time value from hardware that may not correctly
23581096Smsmith * latch the counter.
23681096Smsmith */
23781096Smsmithstatic unsigned
23881096Smsmithacpi_timer_get_timecount_safe(struct timecounter *tc)
23981096Smsmith{
24081096Smsmith    unsigned u1, u2, u3;
24181096Smsmith
242114277Smarcel    u2 = read_counter();
243114277Smarcel    u3 = read_counter();
24481096Smsmith    do {
24581096Smsmith	u1 = u2;
24681096Smsmith	u2 = u3;
247114277Smarcel	u3 = read_counter();
248119529Snjl    } while (u1 > u2 || u2 > u3 || u3 - u1 > 15);
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/*
27880070Smsmith * Test harness for verifying ACPI timer behaviour.
27980070Smsmith * Boot with debug.acpi.timer_test set to invoke this.
28080070Smsmith */
28180070Smsmithstatic void
28280070Smsmithacpi_timer_test(void)
28380070Smsmith{
28480070Smsmith    u_int32_t	u1, u2, u3;
28580070Smsmith
286114277Smarcel    u1 = read_counter();
287114277Smarcel    u2 = read_counter();
288114277Smarcel    u3 = read_counter();
28980070Smsmith
29080070Smsmith    device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
29180070Smsmith    for (;;) {
29280070Smsmith	/*
293119529Snjl	 * The failure case is where u3 > u1, but u2 does not fall between
294119529Snjl	 * the two, ie. it contains garbage.
29580070Smsmith	 */
29680070Smsmith	if (u3 > u1) {
297119529Snjl	    if (u2 < u1 || u2 > u3)
298119529Snjl		device_printf(acpi_timer_dev,
299119529Snjl			      "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
30080070Smsmith			      u1, u2, u3);
30180070Smsmith	}
30280070Smsmith	u1 = u2;
30380070Smsmith	u2 = u3;
304114277Smarcel	u3 = read_counter();
30580070Smsmith    }
30680070Smsmith}
30780070Smsmith
30880602Smsmith/*
30980602Smsmith * Chipset workaround driver hung off PCI.
31080602Smsmith *
31181096Smsmith * Some ACPI timers are known or believed to suffer from implementation
31281096Smsmith * problems which can lead to erroneous values being read from the timer.
31381096Smsmith *
31481096Smsmith * Since we can't trust unknown chipsets, we default to a timer-read
31581096Smsmith * routine which compensates for the most common problem (as detailed
31681096Smsmith * in the excerpt from the Intel PIIX4 datasheet below).
31781096Smsmith *
31881096Smsmith * When we detect a known-functional chipset, we disable the workaround
31981096Smsmith * to improve speed.
32081096Smsmith *
32180602Smsmith * ] 20. ACPI Timer Errata
32280602Smsmith * ]
32380602Smsmith * ]   Problem: The power management timer may return improper result when
32480602Smsmith * ]   read. Although the timer value settles properly after incrementing,
32580602Smsmith * ]   while incrementing there is a 3nS window every 69.8nS where the
32680602Smsmith * ]   timer value is indeterminate (a 4.2% chance that the data will be
32780602Smsmith * ]   incorrect when read). As a result, the ACPI free running count up
32880602Smsmith * ]   timer specification is violated due to erroneous reads.  Implication:
32980602Smsmith * ]   System hangs due to the "inaccuracy" of the timer when used by
33080602Smsmith * ]   software for time critical events and delays.
33180602Smsmith * ]
33280602Smsmith * ] Workaround: Read the register twice and compare.
33380602Smsmith * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
33480602Smsmith * ] in the PIIX4M.
33580602Smsmith *
33680602Smsmith * The counter is in other words not latched to the PCI bus clock when
33780602Smsmith * read.  Notice the workaround isn't:  We need to read until we have
33880602Smsmith * three monotonic samples and then use the middle one, otherwise we are
33980602Smsmith * not protected against the fact that the bits can be wrong in two
34080602Smsmith * directions.  If we only cared about monosity two reads would be enough.
34180602Smsmith */
34280602Smsmith
34391237Sphk#if 0
34480602Smsmithstatic int	acpi_timer_pci_probe(device_t dev);
34580602Smsmith
34680602Smsmithstatic device_method_t acpi_timer_pci_methods[] = {
34780602Smsmith    DEVMETHOD(device_probe,	acpi_timer_pci_probe),
34880602Smsmith    {0, 0}
34980602Smsmith};
35080602Smsmith
35180602Smsmithstatic driver_t acpi_timer_pci_driver = {
35280602Smsmith    "acpi_timer_pci",
35380602Smsmith    acpi_timer_pci_methods,
35480602Smsmith    0,
35580602Smsmith};
35680602Smsmith
35780602Smsmithdevclass_t acpi_timer_pci_devclass;
358119529SnjlDRIVER_MODULE(acpi_timer_pci, pci, acpi_timer_pci_driver,
359119529Snjl	      acpi_timer_pci_devclass, 0, 0);
36080602Smsmith
36180602Smsmith/*
36281096Smsmith * Look at PCI devices going past; if we detect one we know contains
36381096Smsmith * a functional ACPI timer device, enable the faster timecounter read
36481096Smsmith * routine.
36580602Smsmith */
36680602Smsmithstatic int
36780602Smsmithacpi_timer_pci_probe(device_t dev)
36880602Smsmith{
36981172Smsmith    int vendor, device, revid;
37081172Smsmith
37181172Smsmith    vendor = pci_get_vendor(dev);
37281172Smsmith    device = pci_get_device(dev);
37381172Smsmith    revid  = pci_get_revid(dev);
37481172Smsmith
375119529Snjl    /* Detect the PIIX4M and i440MX, respectively */
376119529Snjl    if ((vendor == 0x8086 && device == 0x7113 && revid >= 0x03)	||
377119529Snjl	(vendor == 0x8086 && device == 0x719b)) {
37881172Smsmith
37981096Smsmith	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
38081172Smsmith	acpi_timer_timecounter.tc_name = "ACPI-fast";
381119529Snjl	if (bootverbose) {
382119529Snjl	    device_printf(acpi_timer_dev,"functional ACPI timer detected, "
383119529Snjl			  "enabling fast timecount interface\n");
384119529Snjl	}
38580602Smsmith    }
38680602Smsmith
387119529Snjl    /* We never match anything */
388119529Snjl    return (ENXIO);
38980602Smsmith}
39091237Sphk#endif
391