acpi_timer.c revision 132527
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 132527 2004-07-22 05:32:56Z 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/module.h>
34#include <sys/sysctl.h>
35#include <sys/timetc.h>
36
37#include <machine/bus.h>
38#include <machine/resource.h>
39#include <sys/rman.h>
40
41#include "acpi.h"
42#include <dev/acpica/acpivar.h>
43#include <dev/pci/pcivar.h>
44
45/*
46 * A timecounter based on the free-running ACPI timer.
47 *
48 * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
49 */
50
51/* Hooks for the ACPI CA debugging infrastructure */
52#define _COMPONENT	ACPI_TIMER
53ACPI_MODULE_NAME("TIMER")
54
55static device_t			acpi_timer_dev;
56static struct resource		*acpi_timer_reg;
57static bus_space_handle_t	acpi_timer_bsh;
58static bus_space_tag_t		acpi_timer_bst;
59
60static u_int	acpi_timer_frequency = 14318182 / 4;
61
62static void	acpi_timer_identify(driver_t *driver, device_t parent);
63static int	acpi_timer_probe(device_t dev);
64static int	acpi_timer_attach(device_t dev);
65static u_int	acpi_timer_get_timecount(struct timecounter *tc);
66static u_int	acpi_timer_get_timecount_safe(struct timecounter *tc);
67static int	acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
68static void	acpi_timer_boot_test(void);
69
70static u_int	acpi_timer_read(void);
71static int	acpi_timer_test(void);
72
73static device_method_t acpi_timer_methods[] = {
74    DEVMETHOD(device_identify,	acpi_timer_identify),
75    DEVMETHOD(device_probe,	acpi_timer_probe),
76    DEVMETHOD(device_attach,	acpi_timer_attach),
77
78    {0, 0}
79};
80
81static driver_t acpi_timer_driver = {
82    "acpi_timer",
83    acpi_timer_methods,
84    0,
85};
86
87static devclass_t acpi_timer_devclass;
88DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
89MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
90
91static struct timecounter acpi_timer_timecounter = {
92	acpi_timer_get_timecount_safe,	/* get_timecount function */
93	0,				/* no poll_pps */
94	0,				/* no default counter_mask */
95	0,				/* no default frequency */
96	"ACPI",				/* name */
97	1000				/* quality */
98};
99
100static u_int
101acpi_timer_read()
102{
103    return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0));
104}
105
106/*
107 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
108 * we will be using.
109 */
110static void
111acpi_timer_identify(driver_t *driver, device_t parent)
112{
113    device_t dev;
114    u_long rlen, rstart;
115    int rid, rtype;
116
117    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
118
119    if (acpi_disabled("timer") || AcpiGbl_FADT == NULL || acpi_timer_dev)
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    rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ?
130	SYS_RES_IOPORT : SYS_RES_MEMORY;
131    rlen = AcpiGbl_FADT->PmTmLen;
132    rstart = AcpiGbl_FADT->XPmTmrBlk.Address;
133    if (bus_set_resource(dev, rtype, rid, rstart, rlen))
134	device_printf(dev, "couldn't set resource (%s 0x%lx+0x%lx)\n",
135	    (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen);
136    return_VOID;
137}
138
139static int
140acpi_timer_probe(device_t dev)
141{
142    char desc[40];
143    int i, j, rid, rtype;
144
145    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
146
147    if (dev != acpi_timer_dev)
148	return (ENXIO);
149
150    rid = 0;
151    rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ?
152	SYS_RES_IOPORT : SYS_RES_MEMORY;
153    acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
154    if (acpi_timer_reg == NULL) {
155	device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n",
156	    (rtype == SYS_RES_IOPORT) ? "port" : "mem",
157	    (u_long)AcpiGbl_FADT->XPmTmrBlk.Address);
158	return (ENXIO);
159    }
160    if (AcpiGbl_FADT->TmrValExt != 0)
161	acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
162    else
163	acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
164    acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
165    if (testenv("debug.acpi.timer_test"))
166	acpi_timer_boot_test();
167
168    /*
169     * If all tests of the counter succeed, use the ACPI-fast method.  If
170     * at least one failed, default to using the safe routine, which reads
171     * the timer multiple times to get a consistent value before returning.
172     */
173    j = 0;
174    for (i = 0; i < 10; i++)
175	j += acpi_timer_test();
176    if (j == 10) {
177	acpi_timer_timecounter.tc_name = "ACPI-fast";
178	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
179    } else {
180	acpi_timer_timecounter.tc_name = "ACPI-safe";
181	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
182    }
183    tc_init(&acpi_timer_timecounter);
184
185    sprintf(desc, "%d-bit timer at 3.579545MHz",
186	AcpiGbl_FADT->TmrValExt ? 32 : 24);
187    device_set_desc_copy(dev, desc);
188
189    /* Release the resource, we'll allocate it again during attach. */
190    bus_release_resource(dev, rtype, rid, acpi_timer_reg);
191    return (0);
192}
193
194static int
195acpi_timer_attach(device_t dev)
196{
197    int rid, rtype;
198
199    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
200
201    rid = 0;
202    rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ?
203	SYS_RES_IOPORT : SYS_RES_MEMORY;
204    acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
205    if (acpi_timer_reg == NULL)
206	return (ENXIO);
207    acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
208    acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
209    return (0);
210}
211
212/*
213 * Fetch current time value from reliable hardware.
214 */
215static u_int
216acpi_timer_get_timecount(struct timecounter *tc)
217{
218    return (acpi_timer_read());
219}
220
221/*
222 * Fetch current time value from hardware that may not correctly
223 * latch the counter.  We need to read until we have three monotonic
224 * samples and then use the middle one, otherwise we are not protected
225 * against the fact that the bits can be wrong in two directions.  If
226 * we only cared about monosity, two reads would be enough.
227 */
228static u_int
229acpi_timer_get_timecount_safe(struct timecounter *tc)
230{
231    u_int u1, u2, u3;
232
233    u2 = acpi_timer_read();
234    u3 = acpi_timer_read();
235    do {
236	u1 = u2;
237	u2 = u3;
238	u3 = acpi_timer_read();
239    } while (u1 > u2 || u2 > u3);
240
241    return (u2);
242}
243
244/*
245 * Timecounter freqency adjustment interface.
246 */
247static int
248acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
249{
250    int error;
251    u_int freq;
252
253    if (acpi_timer_timecounter.tc_frequency == 0)
254	return (EOPNOTSUPP);
255    freq = acpi_timer_frequency;
256    error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
257    if (error == 0 && req->newptr != NULL) {
258	acpi_timer_frequency = freq;
259	acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
260    }
261
262    return (error);
263}
264
265SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
266	    0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "");
267
268/*
269 * Some ACPI timers are known or believed to suffer from implementation
270 * problems which can lead to erroneous values being read.  This function
271 * tests for consistent results from the timer and returns 1 if it believes
272 * the timer is consistent, otherwise it returns 0.
273 *
274 * It appears the cause is that the counter is not latched to the PCI bus
275 * clock when read:
276 *
277 * ] 20. ACPI Timer Errata
278 * ]
279 * ]   Problem: The power management timer may return improper result when
280 * ]   read. Although the timer value settles properly after incrementing,
281 * ]   while incrementing there is a 3nS window every 69.8nS where the
282 * ]   timer value is indeterminate (a 4.2% chance that the data will be
283 * ]   incorrect when read). As a result, the ACPI free running count up
284 * ]   timer specification is violated due to erroneous reads.  Implication:
285 * ]   System hangs due to the "inaccuracy" of the timer when used by
286 * ]   software for time critical events and delays.
287 * ]
288 * ] Workaround: Read the register twice and compare.
289 * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
290 * ] in the PIIX4M.
291 */
292#define N 2000
293static int
294acpi_timer_test()
295{
296    uint32_t	last, this;
297    int		min, max, n, delta;
298    register_t	s;
299
300    min = 10000000;
301    max = 0;
302
303    /* Test the timer with interrupts disabled to get accurate results. */
304    s = intr_disable();
305    last = acpi_timer_read();
306    for (n = 0; n < N; n++) {
307	this = acpi_timer_read();
308	delta = acpi_TimerDelta(this, last);
309	if (delta > max)
310	    max = delta;
311	else if (delta < min)
312	    min = delta;
313	last = this;
314    }
315    intr_restore(s);
316
317    if (max - min > 2)
318	n = 0;
319    else if (min < 0 || max == 0)
320	n = 0;
321    else
322	n = 1;
323    if (bootverbose) {
324	printf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
325		n ? "GOOD" : "BAD ",
326		min, max, max - min);
327    }
328
329    return (n);
330}
331#undef N
332
333/*
334 * Test harness for verifying ACPI timer behaviour.
335 * Boot with debug.acpi.timer_test set to invoke this.
336 */
337static void
338acpi_timer_boot_test(void)
339{
340    uint32_t u1, u2, u3;
341
342    u1 = acpi_timer_read();
343    u2 = acpi_timer_read();
344    u3 = acpi_timer_read();
345
346    device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
347    for (;;) {
348	/*
349	 * The failure case is where u3 > u1, but u2 does not fall between
350	 * the two, ie. it contains garbage.
351	 */
352	if (u3 > u1) {
353	    if (u2 < u1 || u2 > u3)
354		device_printf(acpi_timer_dev,
355			      "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
356			      u1, u2, u3);
357	}
358	u1 = u2;
359	u2 = u3;
360	u3 = acpi_timer_read();
361    }
362}
363