acpi_timer.c revision 127135
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 127135 2004-03-17 17:50:55Z 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);
9067761Smsmith
9180070Smsmithstatic struct timecounter acpi_timer_timecounter = {
92118987Sphk	acpi_timer_get_timecount_safe,
93118987Sphk	0,
94118987Sphk	0xffffff,
95118987Sphk	0,
96118987Sphk	"ACPI",
97118987Sphk	1000
9880070Smsmith};
9980070Smsmith
100119529Snjlstatic uint32_t
101114277Smarcelread_counter()
102114277Smarcel{
103119529Snjl    bus_space_handle_t bsh;
104119529Snjl    bus_space_tag_t bst;
105119529Snjl    u_int32_t tv;
106114277Smarcel
107119529Snjl    bsh = rman_get_bushandle(acpi_timer_reg);
108119529Snjl    bst = rman_get_bustag(acpi_timer_reg);
109119529Snjl    tv = bus_space_read_4(bst, bsh, 0);
110119529Snjl    bus_space_barrier(bst, bsh, 0, 4, BUS_SPACE_BARRIER_READ);
111119529Snjl
112119529Snjl    return (tv);
113114277Smarcel}
114114277Smarcel
11591237Sphk#define N 2000
11691237Sphkstatic int
11791237Sphktest_counter()
11891237Sphk{
119119529Snjl    u_int	last, this;
120119529Snjl    int		min, max, n, delta;
12191237Sphk
122119529Snjl    min = 10000000;
123119529Snjl    max = 0;
124119529Snjl    last = read_counter();
125119529Snjl    for (n = 0; n < N; n++) {
126119529Snjl	this = read_counter();
127119529Snjl	delta = (this - last) & 0xffffff;
128119529Snjl	if (delta > max)
129119529Snjl	    max = delta;
130119529Snjl	else if (delta < min)
131119529Snjl	    min = delta;
132119529Snjl	last = this;
133119529Snjl    }
134119529Snjl    if (max - min > 2)
135119529Snjl	n = 0;
136119529Snjl    else if (min < 0 || max == 0)
137119529Snjl	n = 0;
138119529Snjl    else
139119529Snjl	n = 1;
140119529Snjl    if (bootverbose) {
141119529Snjl	printf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
142119529Snjl		n ? "GOOD" : "BAD ",
143119529Snjl		min, max, max - min);
144119529Snjl    }
145119529Snjl
146119529Snjl    return (n);
14791237Sphk}
148119529Snjl#undef N
14991237Sphk
15080070Smsmith/*
15180070Smsmith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
15280070Smsmith * we will be using.
15380070Smsmith */
15467761Smsmithstatic void
15567761Smsmithacpi_timer_identify(driver_t *driver, device_t parent)
15667761Smsmith{
15780070Smsmith    device_t	dev;
15880070Smsmith    char	desc[40];
159114277Smarcel    u_long	rlen, rstart;
160114277Smarcel    int		i, j, rid, rtype;
16167761Smsmith
16296926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
16369744Smsmith
164119529Snjl    if (acpi_disabled("timer") || AcpiGbl_FADT == NULL)
16569744Smsmith	return_VOID;
16671872Smsmith
16767761Smsmith    if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) {
16867761Smsmith	device_printf(parent, "could not add acpi_timer0\n");
16969744Smsmith	return_VOID;
17067761Smsmith    }
17180070Smsmith    acpi_timer_dev = dev;
172114277Smarcel
17380070Smsmith    rid = 0;
174114277Smarcel    rlen = AcpiGbl_FADT->PmTmLen;
175114277Smarcel    rtype = (AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId)
176114277Smarcel      ? SYS_RES_IOPORT : SYS_RES_MEMORY;
177114277Smarcel    rstart = AcpiGbl_FADT->XPmTmrBlk.Address;
178114277Smarcel    bus_set_resource(dev, rtype, rid, rstart, rlen);
179127135Snjl    acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
180114277Smarcel    if (acpi_timer_reg == NULL) {
181114277Smarcel	device_printf(dev, "couldn't allocate I/O resource (%s 0x%lx)\n",
182119529Snjl		      rtype == SYS_RES_IOPORT ? "port" : "mem", rstart);
18369744Smsmith	return_VOID;
18467761Smsmith    }
18594936Smux    if (testenv("debug.acpi.timer_test"))
18680070Smsmith	acpi_timer_test();
18767761Smsmith
18880070Smsmith    acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
18991237Sphk    j = 0;
19091237Sphk    for(i = 0; i < 10; i++)
19191237Sphk	j += test_counter();
19291237Sphk    if (j == 10) {
19391237Sphk	acpi_timer_timecounter.tc_name = "ACPI-fast";
19491237Sphk	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
19591237Sphk    } else {
19691237Sphk	acpi_timer_timecounter.tc_name = "ACPI-safe";
19791237Sphk	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
19891237Sphk    }
19980070Smsmith    tc_init(&acpi_timer_timecounter);
20080070Smsmith
201119529Snjl    sprintf(desc, "%d-bit timer at 3.579545MHz",
202119529Snjl	    AcpiGbl_FADT->TmrValExt ? 32 : 24);
20367761Smsmith    device_set_desc_copy(dev, desc);
20469744Smsmith
20569744Smsmith    return_VOID;
20667761Smsmith}
20767761Smsmith
20867761Smsmithstatic int
20967761Smsmithacpi_timer_probe(device_t dev)
21067761Smsmith{
21180070Smsmith    if (dev == acpi_timer_dev)
212119529Snjl	return (0);
213119529Snjl
214119529Snjl    return (ENXIO);
21567761Smsmith}
21667761Smsmith
21767761Smsmithstatic int
21867761Smsmithacpi_timer_attach(device_t dev)
21967761Smsmith{
220119529Snjl    return (0);
22180070Smsmith}
22267761Smsmith
22380070Smsmith/*
22481096Smsmith * Fetch current time value from reliable hardware.
22580070Smsmith */
22680070Smsmithstatic unsigned
22780070Smsmithacpi_timer_get_timecount(struct timecounter *tc)
22880070Smsmith{
229114277Smarcel    return (read_counter());
23067761Smsmith}
23180070Smsmith
23280070Smsmith/*
23381096Smsmith * Fetch current time value from hardware that may not correctly
23481096Smsmith * latch the counter.
23581096Smsmith */
23681096Smsmithstatic unsigned
23781096Smsmithacpi_timer_get_timecount_safe(struct timecounter *tc)
23881096Smsmith{
23981096Smsmith    unsigned u1, u2, u3;
24081096Smsmith
241114277Smarcel    u2 = read_counter();
242114277Smarcel    u3 = read_counter();
24381096Smsmith    do {
24481096Smsmith	u1 = u2;
24581096Smsmith	u2 = u3;
246114277Smarcel	u3 = read_counter();
247119529Snjl    } while (u1 > u2 || u2 > u3 || u3 - u1 > 15);
248119529Snjl
24981096Smsmith    return (u2);
25081096Smsmith}
25181096Smsmith
25281096Smsmith/*
25380070Smsmith * Timecounter freqency adjustment interface.
25480070Smsmith */
25580070Smsmithstatic int
25680070Smsmithacpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
25780070Smsmith{
25880070Smsmith    int error;
25980070Smsmith    u_int freq;
26080070Smsmith
26180070Smsmith    if (acpi_timer_timecounter.tc_frequency == 0)
26280070Smsmith	return (EOPNOTSUPP);
26380070Smsmith    freq = acpi_timer_frequency;
26480070Smsmith    error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
26580070Smsmith    if (error == 0 && req->newptr != NULL) {
26680070Smsmith	acpi_timer_frequency = freq;
26780070Smsmith	acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
26880070Smsmith    }
269119529Snjl
27080070Smsmith    return (error);
27180070Smsmith}
27280070Smsmith
27380070SmsmithSYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
27480070Smsmith	    0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "");
27580070Smsmith
27680070Smsmith/*
27780070Smsmith * Test harness for verifying ACPI timer behaviour.
27880070Smsmith * Boot with debug.acpi.timer_test set to invoke this.
27980070Smsmith */
28080070Smsmithstatic void
28180070Smsmithacpi_timer_test(void)
28280070Smsmith{
28380070Smsmith    u_int32_t	u1, u2, u3;
28480070Smsmith
285114277Smarcel    u1 = read_counter();
286114277Smarcel    u2 = read_counter();
287114277Smarcel    u3 = read_counter();
28880070Smsmith
28980070Smsmith    device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
29080070Smsmith    for (;;) {
29180070Smsmith	/*
292119529Snjl	 * The failure case is where u3 > u1, but u2 does not fall between
293119529Snjl	 * the two, ie. it contains garbage.
29480070Smsmith	 */
29580070Smsmith	if (u3 > u1) {
296119529Snjl	    if (u2 < u1 || u2 > u3)
297119529Snjl		device_printf(acpi_timer_dev,
298119529Snjl			      "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
29980070Smsmith			      u1, u2, u3);
30080070Smsmith	}
30180070Smsmith	u1 = u2;
30280070Smsmith	u2 = u3;
303114277Smarcel	u3 = read_counter();
30480070Smsmith    }
30580070Smsmith}
30680070Smsmith
30780602Smsmith/*
30880602Smsmith * Chipset workaround driver hung off PCI.
30980602Smsmith *
31081096Smsmith * Some ACPI timers are known or believed to suffer from implementation
31181096Smsmith * problems which can lead to erroneous values being read from the timer.
31281096Smsmith *
31381096Smsmith * Since we can't trust unknown chipsets, we default to a timer-read
31481096Smsmith * routine which compensates for the most common problem (as detailed
31581096Smsmith * in the excerpt from the Intel PIIX4 datasheet below).
31681096Smsmith *
31781096Smsmith * When we detect a known-functional chipset, we disable the workaround
31881096Smsmith * to improve speed.
31981096Smsmith *
32080602Smsmith * ] 20. ACPI Timer Errata
32180602Smsmith * ]
32280602Smsmith * ]   Problem: The power management timer may return improper result when
32380602Smsmith * ]   read. Although the timer value settles properly after incrementing,
32480602Smsmith * ]   while incrementing there is a 3nS window every 69.8nS where the
32580602Smsmith * ]   timer value is indeterminate (a 4.2% chance that the data will be
32680602Smsmith * ]   incorrect when read). As a result, the ACPI free running count up
32780602Smsmith * ]   timer specification is violated due to erroneous reads.  Implication:
32880602Smsmith * ]   System hangs due to the "inaccuracy" of the timer when used by
32980602Smsmith * ]   software for time critical events and delays.
33080602Smsmith * ]
33180602Smsmith * ] Workaround: Read the register twice and compare.
33280602Smsmith * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
33380602Smsmith * ] in the PIIX4M.
33480602Smsmith *
33580602Smsmith * The counter is in other words not latched to the PCI bus clock when
33680602Smsmith * read.  Notice the workaround isn't:  We need to read until we have
33780602Smsmith * three monotonic samples and then use the middle one, otherwise we are
33880602Smsmith * not protected against the fact that the bits can be wrong in two
33980602Smsmith * directions.  If we only cared about monosity two reads would be enough.
34080602Smsmith */
34180602Smsmith
34291237Sphk#if 0
34380602Smsmithstatic int	acpi_timer_pci_probe(device_t dev);
34480602Smsmith
34580602Smsmithstatic device_method_t acpi_timer_pci_methods[] = {
34680602Smsmith    DEVMETHOD(device_probe,	acpi_timer_pci_probe),
34780602Smsmith    {0, 0}
34880602Smsmith};
34980602Smsmith
35080602Smsmithstatic driver_t acpi_timer_pci_driver = {
35180602Smsmith    "acpi_timer_pci",
35280602Smsmith    acpi_timer_pci_methods,
35380602Smsmith    0,
35480602Smsmith};
35580602Smsmith
35680602Smsmithdevclass_t acpi_timer_pci_devclass;
357119529SnjlDRIVER_MODULE(acpi_timer_pci, pci, acpi_timer_pci_driver,
358119529Snjl	      acpi_timer_pci_devclass, 0, 0);
35980602Smsmith
36080602Smsmith/*
36181096Smsmith * Look at PCI devices going past; if we detect one we know contains
36281096Smsmith * a functional ACPI timer device, enable the faster timecounter read
36381096Smsmith * routine.
36480602Smsmith */
36580602Smsmithstatic int
36680602Smsmithacpi_timer_pci_probe(device_t dev)
36780602Smsmith{
36881172Smsmith    int vendor, device, revid;
36981172Smsmith
37081172Smsmith    vendor = pci_get_vendor(dev);
37181172Smsmith    device = pci_get_device(dev);
37281172Smsmith    revid  = pci_get_revid(dev);
37381172Smsmith
374119529Snjl    /* Detect the PIIX4M and i440MX, respectively */
375119529Snjl    if ((vendor == 0x8086 && device == 0x7113 && revid >= 0x03)	||
376119529Snjl	(vendor == 0x8086 && device == 0x719b)) {
37781172Smsmith
37881096Smsmith	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
37981172Smsmith	acpi_timer_timecounter.tc_name = "ACPI-fast";
380119529Snjl	if (bootverbose) {
381119529Snjl	    device_printf(acpi_timer_dev,"functional ACPI timer detected, "
382119529Snjl			  "enabling fast timecount interface\n");
383119529Snjl	}
38480602Smsmith    }
38580602Smsmith
386119529Snjl    /* We never match anything */
387119529Snjl    return (ENXIO);
38880602Smsmith}
38991237Sphk#endif
390