acpi_timer.c revision 128607
1/*-
2 * Copyright (c) 2000, 2001 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *	$FreeBSD: head/sys/dev/acpica/acpi_timer.c 128607 2004-04-24 16:25:00Z njl $
28 */
29#include "opt_acpi.h"
30#include <sys/param.h>
31#include <sys/bus.h>
32#include <sys/kernel.h>
33#include <sys/sysctl.h>
34#include <sys/timetc.h>
35
36#include <machine/bus.h>
37#include <machine/resource.h>
38#include <sys/rman.h>
39
40#include "acpi.h"
41#include <dev/acpica/acpivar.h>
42#include <dev/pci/pcivar.h>
43
44/*
45 * A timecounter based on the free-running ACPI timer.
46 *
47 * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
48 */
49
50/* Hooks for the ACPI CA debugging infrastructure */
51#define _COMPONENT	ACPI_TIMER
52ACPI_MODULE_NAME("TIMER")
53
54static device_t			acpi_timer_dev;
55static struct resource		*acpi_timer_reg;
56static bus_space_handle_t	acpi_timer_bsh;
57static bus_space_tag_t		acpi_timer_bst;
58
59static u_int	acpi_timer_frequency = 14318182 / 4;
60
61static void	acpi_timer_identify(driver_t *driver, device_t parent);
62static int	acpi_timer_probe(device_t dev);
63static int	acpi_timer_attach(device_t dev);
64static u_int	acpi_timer_get_timecount(struct timecounter *tc);
65static u_int	acpi_timer_get_timecount_safe(struct timecounter *tc);
66static int	acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
67static void	acpi_timer_boot_test(void);
68
69static u_int	acpi_timer_read(void);
70static int	acpi_timer_test(void);
71
72static device_method_t acpi_timer_methods[] = {
73    DEVMETHOD(device_identify,	acpi_timer_identify),
74    DEVMETHOD(device_probe,	acpi_timer_probe),
75    DEVMETHOD(device_attach,	acpi_timer_attach),
76
77    {0, 0}
78};
79
80static driver_t acpi_timer_driver = {
81    "acpi_timer",
82    acpi_timer_methods,
83    0,
84};
85
86static devclass_t acpi_timer_devclass;
87DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
88MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
89
90static struct timecounter acpi_timer_timecounter = {
91	acpi_timer_get_timecount_safe,	/* get_timecount function */
92	0,				/* no poll_pps */
93	0,				/* no default counter_mask */
94	0,				/* no default frequency */
95	"ACPI",				/* name */
96	1000				/* quality */
97};
98
99static u_int
100acpi_timer_read()
101{
102    return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0));
103}
104
105/*
106 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
107 * we will be using.
108 */
109static void
110acpi_timer_identify(driver_t *driver, device_t parent)
111{
112    device_t	dev;
113    char	desc[40];
114    u_long	rlen, rstart;
115    int		i, j, rid, rtype;
116
117    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
118
119    if (acpi_disabled("timer") || AcpiGbl_FADT == NULL)
120	return_VOID;
121
122    if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) {
123	device_printf(parent, "could not add acpi_timer0\n");
124	return_VOID;
125    }
126    acpi_timer_dev = dev;
127
128    rid = 0;
129    rlen = AcpiGbl_FADT->PmTmLen;
130    rtype = (AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId)
131      ? SYS_RES_IOPORT : SYS_RES_MEMORY;
132    rstart = AcpiGbl_FADT->XPmTmrBlk.Address;
133    bus_set_resource(dev, rtype, rid, rstart, rlen);
134    acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
135    if (acpi_timer_reg == NULL) {
136	device_printf(dev, "couldn't allocate I/O resource (%s 0x%lx)\n",
137		      rtype == SYS_RES_IOPORT ? "port" : "mem", rstart);
138	return_VOID;
139    }
140    acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
141    acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
142    if (AcpiGbl_FADT->TmrValExt != 0)
143	acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
144    else
145	acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
146    acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
147    if (testenv("debug.acpi.timer_test"))
148	acpi_timer_boot_test();
149
150    /*
151     * If all tests of the counter succeed, use the ACPI-fast method.  If
152     * at least one failed, default to using the safe routine, which reads
153     * the timer multiple times to get a consistent value before returning.
154     */
155    j = 0;
156    for (i = 0; i < 10; i++)
157	j += acpi_timer_test();
158    if (j == 10) {
159	acpi_timer_timecounter.tc_name = "ACPI-fast";
160	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
161    } else {
162	acpi_timer_timecounter.tc_name = "ACPI-safe";
163	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
164    }
165    tc_init(&acpi_timer_timecounter);
166
167    sprintf(desc, "%d-bit timer at 3.579545MHz",
168	    AcpiGbl_FADT->TmrValExt ? 32 : 24);
169    device_set_desc_copy(dev, desc);
170
171    return_VOID;
172}
173
174static int
175acpi_timer_probe(device_t dev)
176{
177    if (dev == acpi_timer_dev)
178	return (0);
179
180    return (ENXIO);
181}
182
183static int
184acpi_timer_attach(device_t dev)
185{
186    return (0);
187}
188
189/*
190 * Fetch current time value from reliable hardware.
191 */
192static u_int
193acpi_timer_get_timecount(struct timecounter *tc)
194{
195    return (acpi_timer_read());
196}
197
198/*
199 * Fetch current time value from hardware that may not correctly
200 * latch the counter.  We need to read until we have three monotonic
201 * samples and then use the middle one, otherwise we are not protected
202 * against the fact that the bits can be wrong in two directions.  If
203 * we only cared about monosity, two reads would be enough.
204 */
205static u_int
206acpi_timer_get_timecount_safe(struct timecounter *tc)
207{
208    u_int u1, u2, u3;
209
210    u2 = acpi_timer_read();
211    u3 = acpi_timer_read();
212    do {
213	u1 = u2;
214	u2 = u3;
215	u3 = acpi_timer_read();
216    } while (u1 > u2 || u2 > u3);
217
218    return (u2);
219}
220
221/*
222 * Timecounter freqency adjustment interface.
223 */
224static int
225acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
226{
227    int error;
228    u_int freq;
229
230    if (acpi_timer_timecounter.tc_frequency == 0)
231	return (EOPNOTSUPP);
232    freq = acpi_timer_frequency;
233    error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
234    if (error == 0 && req->newptr != NULL) {
235	acpi_timer_frequency = freq;
236	acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
237    }
238
239    return (error);
240}
241
242SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
243	    0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "");
244
245/*
246 * Some ACPI timers are known or believed to suffer from implementation
247 * problems which can lead to erroneous values being read.  This function
248 * tests for consistent results from the timer and returns 1 if it believes
249 * the timer is consistent, otherwise it returns 0.
250 *
251 * It appears the cause is that the counter is not latched to the PCI bus
252 * clock when read:
253 *
254 * ] 20. ACPI Timer Errata
255 * ]
256 * ]   Problem: The power management timer may return improper result when
257 * ]   read. Although the timer value settles properly after incrementing,
258 * ]   while incrementing there is a 3nS window every 69.8nS where the
259 * ]   timer value is indeterminate (a 4.2% chance that the data will be
260 * ]   incorrect when read). As a result, the ACPI free running count up
261 * ]   timer specification is violated due to erroneous reads.  Implication:
262 * ]   System hangs due to the "inaccuracy" of the timer when used by
263 * ]   software for time critical events and delays.
264 * ]
265 * ] Workaround: Read the register twice and compare.
266 * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
267 * ] in the PIIX4M.
268 */
269#define N 2000
270static int
271acpi_timer_test()
272{
273    uint32_t	last, this;
274    int		min, max, n, delta;
275    register_t	s;
276
277    min = 10000000;
278    max = 0;
279
280    /* Test the timer with interrupts disabled to get accurate results. */
281    s = intr_disable();
282    last = acpi_timer_read();
283    for (n = 0; n < N; n++) {
284	this = acpi_timer_read();
285	delta = acpi_TimerDelta(this, last);
286	if (delta > max)
287	    max = delta;
288	else if (delta < min)
289	    min = delta;
290	last = this;
291    }
292    intr_restore(s);
293
294    if (max - min > 2)
295	n = 0;
296    else if (min < 0 || max == 0)
297	n = 0;
298    else
299	n = 1;
300    if (bootverbose) {
301	printf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
302		n ? "GOOD" : "BAD ",
303		min, max, max - min);
304    }
305
306    return (n);
307}
308#undef N
309
310/*
311 * Test harness for verifying ACPI timer behaviour.
312 * Boot with debug.acpi.timer_test set to invoke this.
313 */
314static void
315acpi_timer_boot_test(void)
316{
317    uint32_t u1, u2, u3;
318
319    u1 = acpi_timer_read();
320    u2 = acpi_timer_read();
321    u3 = acpi_timer_read();
322
323    device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
324    for (;;) {
325	/*
326	 * The failure case is where u3 > u1, but u2 does not fall between
327	 * the two, ie. it contains garbage.
328	 */
329	if (u3 > u1) {
330	    if (u2 < u1 || u2 > u3)
331		device_printf(acpi_timer_dev,
332			      "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
333			      u1, u2, u3);
334	}
335	u1 = u2;
336	u2 = u3;
337	u3 = acpi_timer_read();
338    }
339}
340