acpi_timer.c revision 220333
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 220333 2011-04-04 17:00:50Z 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
13380070Smsmith    rid = 0;
134167814Sjkim    rtype = AcpiGbl_FADT.XPmTimerBlock.SpaceId ?
135132527Snjl	SYS_RES_IOPORT : SYS_RES_MEMORY;
136167814Sjkim    rlen = AcpiGbl_FADT.PmTimerLength;
137167814Sjkim    rstart = AcpiGbl_FADT.XPmTimerBlock.Address;
138132527Snjl    if (bus_set_resource(dev, rtype, rid, rstart, rlen))
139132527Snjl	device_printf(dev, "couldn't set resource (%s 0x%lx+0x%lx)\n",
140132527Snjl	    (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen);
141132527Snjl    return_VOID;
142132527Snjl}
143132527Snjl
144132527Snjlstatic int
145132527Snjlacpi_timer_probe(device_t dev)
146132527Snjl{
147132527Snjl    char desc[40];
148132527Snjl    int i, j, rid, rtype;
149132527Snjl
150132527Snjl    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
151132527Snjl
152132527Snjl    if (dev != acpi_timer_dev)
153132527Snjl	return (ENXIO);
154132527Snjl
155132527Snjl    rid = 0;
156167814Sjkim    rtype = AcpiGbl_FADT.XPmTimerBlock.SpaceId ?
157132527Snjl	SYS_RES_IOPORT : SYS_RES_MEMORY;
158127135Snjl    acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
159114277Smarcel    if (acpi_timer_reg == NULL) {
160132527Snjl	device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n",
161132527Snjl	    (rtype == SYS_RES_IOPORT) ? "port" : "mem",
162167814Sjkim	    (u_long)AcpiGbl_FADT.XPmTimerBlock.Address);
163132527Snjl	return (ENXIO);
16467761Smsmith    }
165132528Snjl    acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
166132528Snjl    acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
167167814Sjkim    if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER)
168128506Snjl	acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
169128506Snjl    else
170128506Snjl	acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
171128506Snjl    acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
17294936Smux    if (testenv("debug.acpi.timer_test"))
173128543Snjl	acpi_timer_boot_test();
17467761Smsmith
175128528Snjl    /*
176128528Snjl     * If all tests of the counter succeed, use the ACPI-fast method.  If
177128528Snjl     * at least one failed, default to using the safe routine, which reads
178128528Snjl     * the timer multiple times to get a consistent value before returning.
179128528Snjl     */
18091237Sphk    j = 0;
181137151Sphk    if (bootverbose)
182137151Sphk	printf("ACPI timer:");
183128506Snjl    for (i = 0; i < 10; i++)
184128543Snjl	j += acpi_timer_test();
185137151Sphk    if (bootverbose)
186137151Sphk	printf(" -> %d\n", j);
18791237Sphk    if (j == 10) {
18891237Sphk	acpi_timer_timecounter.tc_name = "ACPI-fast";
18991237Sphk	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
190171657Snjl	acpi_timer_timecounter.tc_quality = 1000;
19191237Sphk    } else {
19291237Sphk	acpi_timer_timecounter.tc_name = "ACPI-safe";
19391237Sphk	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
194171657Snjl	acpi_timer_timecounter.tc_quality = 850;
19591237Sphk    }
19680070Smsmith    tc_init(&acpi_timer_timecounter);
19780070Smsmith
198119529Snjl    sprintf(desc, "%d-bit timer at 3.579545MHz",
199167814Sjkim	(AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) ? 32 : 24);
20067761Smsmith    device_set_desc_copy(dev, desc);
20169744Smsmith
202132527Snjl    /* Release the resource, we'll allocate it again during attach. */
203132527Snjl    bus_release_resource(dev, rtype, rid, acpi_timer_reg);
204132527Snjl    return (0);
20567761Smsmith}
20667761Smsmith
20767761Smsmithstatic int
208132527Snjlacpi_timer_attach(device_t dev)
20967761Smsmith{
210132527Snjl    int rid, rtype;
211119529Snjl
212132527Snjl    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
21367761Smsmith
214132527Snjl    rid = 0;
215167814Sjkim    rtype = AcpiGbl_FADT.XPmTimerBlock.SpaceId ?
216132527Snjl	SYS_RES_IOPORT : SYS_RES_MEMORY;
217132527Snjl    acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
218132527Snjl    if (acpi_timer_reg == NULL)
219132527Snjl	return (ENXIO);
220132527Snjl    acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
221132527Snjl    acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
222119529Snjl    return (0);
22380070Smsmith}
22467761Smsmith
22580070Smsmith/*
22681096Smsmith * Fetch current time value from reliable hardware.
22780070Smsmith */
228128506Snjlstatic u_int
22980070Smsmithacpi_timer_get_timecount(struct timecounter *tc)
23080070Smsmith{
231128543Snjl    return (acpi_timer_read());
23267761Smsmith}
23380070Smsmith
23480070Smsmith/*
23581096Smsmith * Fetch current time value from hardware that may not correctly
236128528Snjl * latch the counter.  We need to read until we have three monotonic
237128528Snjl * samples and then use the middle one, otherwise we are not protected
238128528Snjl * against the fact that the bits can be wrong in two directions.  If
239128528Snjl * we only cared about monosity, two reads would be enough.
24081096Smsmith */
241128506Snjlstatic u_int
24281096Smsmithacpi_timer_get_timecount_safe(struct timecounter *tc)
24381096Smsmith{
244128506Snjl    u_int u1, u2, u3;
24581096Smsmith
246128543Snjl    u2 = acpi_timer_read();
247128543Snjl    u3 = acpi_timer_read();
24881096Smsmith    do {
24981096Smsmith	u1 = u2;
25081096Smsmith	u2 = u3;
251128543Snjl	u3 = acpi_timer_read();
252128543Snjl    } while (u1 > u2 || u2 > u3);
253119529Snjl
25481096Smsmith    return (u2);
25581096Smsmith}
25681096Smsmith
25781096Smsmith/*
25880070Smsmith * Timecounter freqency adjustment interface.
25980070Smsmith */
26080070Smsmithstatic int
26180070Smsmithacpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
26280070Smsmith{
26380070Smsmith    int error;
26480070Smsmith    u_int freq;
26580070Smsmith
26680070Smsmith    if (acpi_timer_timecounter.tc_frequency == 0)
26780070Smsmith	return (EOPNOTSUPP);
26880070Smsmith    freq = acpi_timer_frequency;
269170289Sdwmalone    error = sysctl_handle_int(oidp, &freq, 0, req);
27080070Smsmith    if (error == 0 && req->newptr != NULL) {
27180070Smsmith	acpi_timer_frequency = freq;
27280070Smsmith	acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
27380070Smsmith    }
274119529Snjl
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/*
282128528Snjl * Some ACPI timers are known or believed to suffer from implementation
283128528Snjl * problems which can lead to erroneous values being read.  This function
284128528Snjl * tests for consistent results from the timer and returns 1 if it believes
285128528Snjl * the timer is consistent, otherwise it returns 0.
286128528Snjl *
287128528Snjl * It appears the cause is that the counter is not latched to the PCI bus
288128528Snjl * clock when read:
289128528Snjl *
290128528Snjl * ] 20. ACPI Timer Errata
291128528Snjl * ]
292128528Snjl * ]   Problem: The power management timer may return improper result when
293128528Snjl * ]   read. Although the timer value settles properly after incrementing,
294128528Snjl * ]   while incrementing there is a 3nS window every 69.8nS where the
295128528Snjl * ]   timer value is indeterminate (a 4.2% chance that the data will be
296128528Snjl * ]   incorrect when read). As a result, the ACPI free running count up
297128528Snjl * ]   timer specification is violated due to erroneous reads.  Implication:
298128528Snjl * ]   System hangs due to the "inaccuracy" of the timer when used by
299128528Snjl * ]   software for time critical events and delays.
300128528Snjl * ]
301128528Snjl * ] Workaround: Read the register twice and compare.
302128528Snjl * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
303128528Snjl * ] in the PIIX4M.
304128528Snjl */
305128528Snjl#define N 2000
306128528Snjlstatic int
307128543Snjlacpi_timer_test()
308128528Snjl{
309128528Snjl    uint32_t	last, this;
310128528Snjl    int		min, max, n, delta;
311128607Snjl    register_t	s;
312128528Snjl
313128528Snjl    min = 10000000;
314128528Snjl    max = 0;
315128607Snjl
316128607Snjl    /* Test the timer with interrupts disabled to get accurate results. */
317128607Snjl    s = intr_disable();
318128543Snjl    last = acpi_timer_read();
319128528Snjl    for (n = 0; n < N; n++) {
320128543Snjl	this = acpi_timer_read();
321128528Snjl	delta = acpi_TimerDelta(this, last);
322128528Snjl	if (delta > max)
323128528Snjl	    max = delta;
324128528Snjl	else if (delta < min)
325128528Snjl	    min = delta;
326128528Snjl	last = this;
327128528Snjl    }
328128607Snjl    intr_restore(s);
329128607Snjl
330220333Sjkim    delta = max - min;
331220333Sjkim    if (delta > 2 && vm_guest == VM_GUEST_NO)
332128528Snjl	n = 0;
333128528Snjl    else if (min < 0 || max == 0)
334128528Snjl	n = 0;
335128528Snjl    else
336128528Snjl	n = 1;
337137151Sphk    if (bootverbose)
338220333Sjkim	printf(" %d/%d", n, delta);
339128528Snjl
340128528Snjl    return (n);
341128528Snjl}
342128528Snjl#undef N
343128528Snjl
344128528Snjl/*
34580070Smsmith * Test harness for verifying ACPI timer behaviour.
34680070Smsmith * Boot with debug.acpi.timer_test set to invoke this.
34780070Smsmith */
34880070Smsmithstatic void
349128543Snjlacpi_timer_boot_test(void)
35080070Smsmith{
351128506Snjl    uint32_t u1, u2, u3;
352128506Snjl
353128543Snjl    u1 = acpi_timer_read();
354128543Snjl    u2 = acpi_timer_read();
355128543Snjl    u3 = acpi_timer_read();
356128506Snjl
35780070Smsmith    device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
35880070Smsmith    for (;;) {
35980070Smsmith	/*
360119529Snjl	 * The failure case is where u3 > u1, but u2 does not fall between
361119529Snjl	 * the two, ie. it contains garbage.
36280070Smsmith	 */
36380070Smsmith	if (u3 > u1) {
364119529Snjl	    if (u2 < u1 || u2 > u3)
365119529Snjl		device_printf(acpi_timer_dev,
366119529Snjl			      "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
36780070Smsmith			      u1, u2, u3);
36880070Smsmith	}
36980070Smsmith	u1 = u2;
37080070Smsmith	u2 = u3;
371128543Snjl	u3 = acpi_timer_read();
37280070Smsmith    }
37380070Smsmith}
374