acpi_timer.c revision 222222
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 */
27143002Sobrien
28143002Sobrien#include <sys/cdefs.h>
29143002Sobrien__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi_timer.c 222222 2011-05-23 20:12:36Z jkim $");
30143002Sobrien
3167761Smsmith#include "opt_acpi.h"
3267761Smsmith#include <sys/param.h>
3380070Smsmith#include <sys/bus.h>
3467761Smsmith#include <sys/kernel.h>
35129879Sphk#include <sys/module.h>
3680070Smsmith#include <sys/sysctl.h>
3780070Smsmith#include <sys/timetc.h>
3867761Smsmith
3980070Smsmith#include <machine/bus.h>
4080070Smsmith#include <machine/resource.h>
4180070Smsmith#include <sys/rman.h>
4280070Smsmith
43193530Sjkim#include <contrib/dev/acpica/include/acpi.h>
44193530Sjkim#include <contrib/dev/acpica/include/accommon.h>
45193530Sjkim
46104726Sjhb#include <dev/acpica/acpivar.h>
47119281Simp#include <dev/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
55119529Snjl/* Hooks for the ACPI CA debugging infrastructure */
56126517Snjl#define _COMPONENT	ACPI_TIMER
5791128SmsmithACPI_MODULE_NAME("TIMER")
5869744Smsmith
59128528Snjlstatic device_t			acpi_timer_dev;
60128528Snjlstatic struct resource		*acpi_timer_reg;
61128543Snjlstatic bus_space_handle_t	acpi_timer_bsh;
62128543Snjlstatic bus_space_tag_t		acpi_timer_bst;
6367761Smsmith
64119529Snjlstatic 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);
69128506Snjlstatic u_int	acpi_timer_get_timecount(struct timecounter *tc);
70128506Snjlstatic u_int	acpi_timer_get_timecount_safe(struct timecounter *tc);
7180070Smsmithstatic int	acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
72128543Snjlstatic void	acpi_timer_boot_test(void);
7367761Smsmith
74128543Snjlstatic int	acpi_timer_test(void);
75114277Smarcel
7667761Smsmithstatic device_method_t acpi_timer_methods[] = {
7767761Smsmith    DEVMETHOD(device_identify,	acpi_timer_identify),
7867761Smsmith    DEVMETHOD(device_probe,	acpi_timer_probe),
7967761Smsmith    DEVMETHOD(device_attach,	acpi_timer_attach),
8067761Smsmith
8167761Smsmith    {0, 0}
8267761Smsmith};
8367761Smsmith
8467761Smsmithstatic driver_t acpi_timer_driver = {
8567761Smsmith    "acpi_timer",
8667761Smsmith    acpi_timer_methods,
8780070Smsmith    0,
8867761Smsmith};
8967761Smsmith
9089054Smsmithstatic devclass_t acpi_timer_devclass;
9167761SmsmithDRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
92128071SnjlMODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
9367761Smsmith
9480070Smsmithstatic struct timecounter acpi_timer_timecounter = {
95128543Snjl	acpi_timer_get_timecount_safe,	/* get_timecount function */
96128543Snjl	0,				/* no poll_pps */
97128543Snjl	0,				/* no default counter_mask */
98128543Snjl	0,				/* no default frequency */
99128543Snjl	"ACPI",				/* name */
100171657Snjl	-1				/* quality (chosen later) */
10180070Smsmith};
10280070Smsmith
103220331Sjkimstatic __inline uint32_t
104220331Sjkimacpi_timer_read(void)
105114277Smarcel{
106220331Sjkim
107128607Snjl    return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0));
108114277Smarcel}
109114277Smarcel
11080070Smsmith/*
11180070Smsmith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
11280070Smsmith * we will be using.
11380070Smsmith */
11467761Smsmithstatic void
11567761Smsmithacpi_timer_identify(driver_t *driver, device_t parent)
11667761Smsmith{
117132527Snjl    device_t dev;
118132527Snjl    u_long rlen, rstart;
119132527Snjl    int rid, rtype;
12067761Smsmith
12196926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
12269744Smsmith
123136270Snjl    if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) ||
124167814Sjkim	acpi_timer_dev)
12569744Smsmith	return_VOID;
126128506Snjl
12767761Smsmith    if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) {
12867761Smsmith	device_printf(parent, "could not add acpi_timer0\n");
12969744Smsmith	return_VOID;
13067761Smsmith    }
13180070Smsmith    acpi_timer_dev = dev;
132114277Smarcel
133220805Sjkim    switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
134220805Sjkim    case ACPI_ADR_SPACE_SYSTEM_MEMORY:
135220805Sjkim	rtype = SYS_RES_MEMORY;
136220805Sjkim	break;
137220805Sjkim    case ACPI_ADR_SPACE_SYSTEM_IO:
138220805Sjkim	rtype = SYS_RES_IOPORT;
139220805Sjkim	break;
140220805Sjkim    default:
141220805Sjkim	return_VOID;
142220805Sjkim    }
14380070Smsmith    rid = 0;
144167814Sjkim    rlen = AcpiGbl_FADT.PmTimerLength;
145167814Sjkim    rstart = AcpiGbl_FADT.XPmTimerBlock.Address;
146132527Snjl    if (bus_set_resource(dev, rtype, rid, rstart, rlen))
147132527Snjl	device_printf(dev, "couldn't set resource (%s 0x%lx+0x%lx)\n",
148132527Snjl	    (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen);
149132527Snjl    return_VOID;
150132527Snjl}
151132527Snjl
152132527Snjlstatic int
153132527Snjlacpi_timer_probe(device_t dev)
154132527Snjl{
155132527Snjl    char desc[40];
156132527Snjl    int i, j, rid, rtype;
157132527Snjl
158132527Snjl    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
159132527Snjl
160132527Snjl    if (dev != acpi_timer_dev)
161132527Snjl	return (ENXIO);
162132527Snjl
163220805Sjkim    switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
164220805Sjkim    case ACPI_ADR_SPACE_SYSTEM_MEMORY:
165220805Sjkim	rtype = SYS_RES_MEMORY;
166220805Sjkim	break;
167220805Sjkim    case ACPI_ADR_SPACE_SYSTEM_IO:
168220805Sjkim	rtype = SYS_RES_IOPORT;
169220805Sjkim	break;
170220805Sjkim    default:
171220805Sjkim	return (ENXIO);
172220805Sjkim    }
173132527Snjl    rid = 0;
174127135Snjl    acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
175114277Smarcel    if (acpi_timer_reg == NULL) {
176132527Snjl	device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n",
177132527Snjl	    (rtype == SYS_RES_IOPORT) ? "port" : "mem",
178167814Sjkim	    (u_long)AcpiGbl_FADT.XPmTimerBlock.Address);
179132527Snjl	return (ENXIO);
18067761Smsmith    }
181132528Snjl    acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
182132528Snjl    acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
183167814Sjkim    if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER)
184128506Snjl	acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
185128506Snjl    else
186128506Snjl	acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
187128506Snjl    acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
18894936Smux    if (testenv("debug.acpi.timer_test"))
189128543Snjl	acpi_timer_boot_test();
19067761Smsmith
191128528Snjl    /*
192128528Snjl     * If all tests of the counter succeed, use the ACPI-fast method.  If
193128528Snjl     * at least one failed, default to using the safe routine, which reads
194128528Snjl     * the timer multiple times to get a consistent value before returning.
195128528Snjl     */
19691237Sphk    j = 0;
197137151Sphk    if (bootverbose)
198137151Sphk	printf("ACPI timer:");
199128506Snjl    for (i = 0; i < 10; i++)
200128543Snjl	j += acpi_timer_test();
201137151Sphk    if (bootverbose)
202137151Sphk	printf(" -> %d\n", j);
20391237Sphk    if (j == 10) {
20491237Sphk	acpi_timer_timecounter.tc_name = "ACPI-fast";
20591237Sphk	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
206222222Sjkim	acpi_timer_timecounter.tc_quality = 900;
20791237Sphk    } else {
20891237Sphk	acpi_timer_timecounter.tc_name = "ACPI-safe";
20991237Sphk	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
210171657Snjl	acpi_timer_timecounter.tc_quality = 850;
21191237Sphk    }
21280070Smsmith    tc_init(&acpi_timer_timecounter);
21380070Smsmith
214220805Sjkim    sprintf(desc, "%d-bit timer at %u.%06uMHz",
215220805Sjkim	(AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) != 0 ? 32 : 24,
216220805Sjkim	acpi_timer_frequency / 1000000, acpi_timer_frequency % 1000000);
21767761Smsmith    device_set_desc_copy(dev, desc);
21869744Smsmith
219132527Snjl    /* Release the resource, we'll allocate it again during attach. */
220132527Snjl    bus_release_resource(dev, rtype, rid, acpi_timer_reg);
221132527Snjl    return (0);
22267761Smsmith}
22367761Smsmith
22467761Smsmithstatic int
225132527Snjlacpi_timer_attach(device_t dev)
22667761Smsmith{
227132527Snjl    int rid, rtype;
228119529Snjl
229132527Snjl    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
23067761Smsmith
231220805Sjkim    switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
232220805Sjkim    case ACPI_ADR_SPACE_SYSTEM_MEMORY:
233220805Sjkim	rtype = SYS_RES_MEMORY;
234220805Sjkim	break;
235220805Sjkim    case ACPI_ADR_SPACE_SYSTEM_IO:
236220805Sjkim	rtype = SYS_RES_IOPORT;
237220805Sjkim	break;
238220805Sjkim    default:
239220805Sjkim	return (ENXIO);
240220805Sjkim    }
241132527Snjl    rid = 0;
242132527Snjl    acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
243132527Snjl    if (acpi_timer_reg == NULL)
244132527Snjl	return (ENXIO);
245132527Snjl    acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
246132527Snjl    acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
247119529Snjl    return (0);
24880070Smsmith}
24967761Smsmith
25080070Smsmith/*
25181096Smsmith * Fetch current time value from reliable hardware.
25280070Smsmith */
253128506Snjlstatic u_int
25480070Smsmithacpi_timer_get_timecount(struct timecounter *tc)
25580070Smsmith{
256128543Snjl    return (acpi_timer_read());
25767761Smsmith}
25880070Smsmith
25980070Smsmith/*
26081096Smsmith * Fetch current time value from hardware that may not correctly
261128528Snjl * latch the counter.  We need to read until we have three monotonic
262128528Snjl * samples and then use the middle one, otherwise we are not protected
263128528Snjl * against the fact that the bits can be wrong in two directions.  If
264128528Snjl * we only cared about monosity, two reads would be enough.
26581096Smsmith */
266128506Snjlstatic u_int
26781096Smsmithacpi_timer_get_timecount_safe(struct timecounter *tc)
26881096Smsmith{
269128506Snjl    u_int u1, u2, u3;
27081096Smsmith
271128543Snjl    u2 = acpi_timer_read();
272128543Snjl    u3 = acpi_timer_read();
27381096Smsmith    do {
27481096Smsmith	u1 = u2;
27581096Smsmith	u2 = u3;
276128543Snjl	u3 = acpi_timer_read();
277128543Snjl    } while (u1 > u2 || u2 > u3);
278119529Snjl
27981096Smsmith    return (u2);
28081096Smsmith}
28181096Smsmith
28281096Smsmith/*
28380070Smsmith * Timecounter freqency adjustment interface.
28480070Smsmith */
28580070Smsmithstatic int
28680070Smsmithacpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
28780070Smsmith{
28880070Smsmith    int error;
28980070Smsmith    u_int freq;
29080070Smsmith
29180070Smsmith    if (acpi_timer_timecounter.tc_frequency == 0)
29280070Smsmith	return (EOPNOTSUPP);
29380070Smsmith    freq = acpi_timer_frequency;
294170289Sdwmalone    error = sysctl_handle_int(oidp, &freq, 0, req);
29580070Smsmith    if (error == 0 && req->newptr != NULL) {
29680070Smsmith	acpi_timer_frequency = freq;
29780070Smsmith	acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
29880070Smsmith    }
299119529Snjl
30080070Smsmith    return (error);
30180070Smsmith}
30280070Smsmith
30380070SmsmithSYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
304220613Sjkim    0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "ACPI timer frequency");
30580070Smsmith
30680070Smsmith/*
307128528Snjl * Some ACPI timers are known or believed to suffer from implementation
308128528Snjl * problems which can lead to erroneous values being read.  This function
309128528Snjl * tests for consistent results from the timer and returns 1 if it believes
310128528Snjl * the timer is consistent, otherwise it returns 0.
311128528Snjl *
312128528Snjl * It appears the cause is that the counter is not latched to the PCI bus
313128528Snjl * clock when read:
314128528Snjl *
315128528Snjl * ] 20. ACPI Timer Errata
316128528Snjl * ]
317128528Snjl * ]   Problem: The power management timer may return improper result when
318128528Snjl * ]   read. Although the timer value settles properly after incrementing,
319128528Snjl * ]   while incrementing there is a 3nS window every 69.8nS where the
320128528Snjl * ]   timer value is indeterminate (a 4.2% chance that the data will be
321128528Snjl * ]   incorrect when read). As a result, the ACPI free running count up
322128528Snjl * ]   timer specification is violated due to erroneous reads.  Implication:
323128528Snjl * ]   System hangs due to the "inaccuracy" of the timer when used by
324128528Snjl * ]   software for time critical events and delays.
325128528Snjl * ]
326128528Snjl * ] Workaround: Read the register twice and compare.
327128528Snjl * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
328128528Snjl * ] in the PIIX4M.
329128528Snjl */
330128528Snjl#define N 2000
331128528Snjlstatic int
332128543Snjlacpi_timer_test()
333128528Snjl{
334220369Sjkim    uint32_t last, this;
335220369Sjkim    int delta, max, max2, min, n;
336220369Sjkim    register_t s;
337128528Snjl
338220336Sjkim    min = INT32_MAX;
339220369Sjkim    max = max2 = 0;
340128607Snjl
341128607Snjl    /* Test the timer with interrupts disabled to get accurate results. */
342128607Snjl    s = intr_disable();
343128543Snjl    last = acpi_timer_read();
344128528Snjl    for (n = 0; n < N; n++) {
345128543Snjl	this = acpi_timer_read();
346128528Snjl	delta = acpi_TimerDelta(this, last);
347220369Sjkim	if (delta > max) {
348220369Sjkim	    max2 = max;
349128528Snjl	    max = delta;
350220369Sjkim	} else if (delta > max2)
351220369Sjkim	    max2 = delta;
352220336Sjkim	if (delta < min)
353128528Snjl	    min = delta;
354128528Snjl	last = this;
355128528Snjl    }
356128607Snjl    intr_restore(s);
357128607Snjl
358220369Sjkim    delta = max2 - min;
359220369Sjkim    if ((max - min > 8 || delta > 3) && vm_guest == VM_GUEST_NO)
360128528Snjl	n = 0;
361220369Sjkim    else if (min < 0 || max == 0 || max2 == 0)
362128528Snjl	n = 0;
363128528Snjl    else
364128528Snjl	n = 1;
365137151Sphk    if (bootverbose)
366220333Sjkim	printf(" %d/%d", n, delta);
367128528Snjl
368128528Snjl    return (n);
369128528Snjl}
370128528Snjl#undef N
371128528Snjl
372128528Snjl/*
37380070Smsmith * Test harness for verifying ACPI timer behaviour.
37480070Smsmith * Boot with debug.acpi.timer_test set to invoke this.
37580070Smsmith */
37680070Smsmithstatic void
377128543Snjlacpi_timer_boot_test(void)
37880070Smsmith{
379128506Snjl    uint32_t u1, u2, u3;
380128506Snjl
381128543Snjl    u1 = acpi_timer_read();
382128543Snjl    u2 = acpi_timer_read();
383128543Snjl    u3 = acpi_timer_read();
384128506Snjl
38580070Smsmith    device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
38680070Smsmith    for (;;) {
38780070Smsmith	/*
388119529Snjl	 * The failure case is where u3 > u1, but u2 does not fall between
389119529Snjl	 * the two, ie. it contains garbage.
39080070Smsmith	 */
39180070Smsmith	if (u3 > u1) {
392119529Snjl	    if (u2 < u1 || u2 > u3)
393119529Snjl		device_printf(acpi_timer_dev,
394119529Snjl			      "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
39580070Smsmith			      u1, u2, u3);
39680070Smsmith	}
39780070Smsmith	u1 = u2;
39880070Smsmith	u2 = u3;
399128543Snjl	u3 = acpi_timer_read();
40080070Smsmith    }
40180070Smsmith}
402