acpi_timer.c revision 114277
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 114277 2003-04-30 05:27:01Z marcel $
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#if __FreeBSD_version >= 500000
35#include <sys/timetc.h>
36#else
37#include <sys/time.h>
38#endif
39
40#include <machine/bus.h>
41#include <machine/resource.h>
42#include <sys/rman.h>
43
44#include "acpi.h"
45
46#include <dev/acpica/acpivar.h>
47#include <pci/pcivar.h>
48
49/*
50 * A timecounter based on the free-running ACPI timer.
51 *
52 * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
53 */
54
55/*
56 * Hooks for the ACPI CA debugging infrastructure
57 */
58#define _COMPONENT	ACPI_SYSTEM
59ACPI_MODULE_NAME("TIMER")
60
61static device_t	acpi_timer_dev;
62struct resource	*acpi_timer_reg;
63
64static u_int	acpi_timer_frequency = 14318182/4;
65
66static void	acpi_timer_identify(driver_t *driver, device_t parent);
67static int	acpi_timer_probe(device_t dev);
68static int	acpi_timer_attach(device_t dev);
69static unsigned	acpi_timer_get_timecount(struct timecounter *tc);
70static unsigned	acpi_timer_get_timecount_safe(struct timecounter *tc);
71static int	acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
72static void	acpi_timer_test(void);
73
74static u_int32_t read_counter(void);
75static int test_counter(void);
76
77/*
78 * Driver hung off ACPI.
79 */
80static device_method_t acpi_timer_methods[] = {
81    DEVMETHOD(device_identify,	acpi_timer_identify),
82    DEVMETHOD(device_probe,	acpi_timer_probe),
83    DEVMETHOD(device_attach,	acpi_timer_attach),
84
85    {0, 0}
86};
87
88static driver_t acpi_timer_driver = {
89    "acpi_timer",
90    acpi_timer_methods,
91    0,
92};
93
94static devclass_t acpi_timer_devclass;
95DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
96
97/*
98 * Timecounter.
99 */
100static struct timecounter acpi_timer_timecounter = {
101    acpi_timer_get_timecount_safe,
102    0,
103    0xffffff,
104    0,
105    "ACPI"
106};
107
108
109static u_int32_t
110read_counter()
111{
112	bus_space_handle_t bsh;
113	bus_space_tag_t bst;
114	u_int32_t tv;
115
116	bsh = rman_get_bushandle(acpi_timer_reg);
117	bst = rman_get_bustag(acpi_timer_reg);
118	tv = bus_space_read_4(bst, bsh, 0);
119	bus_space_barrier(bst, bsh, 0, 4, BUS_SPACE_BARRIER_READ);
120	return (tv);
121}
122
123#define N 2000
124static int
125test_counter()
126{
127	int min, max, n, delta;
128	unsigned last, this;
129
130	min = 10000000;
131	max = 0;
132	last = read_counter();
133	for (n = 0; n < N; n++) {
134		this = read_counter();
135		delta = (this - last) & 0xffffff;
136		if (delta > max)
137			max = delta;
138		else if (delta < min)
139			min = delta;
140		last = this;
141	}
142	if (max - min > 2)
143		n = 0;
144	else if (min < 0 || max == 0)
145		n = 0;
146	else
147		n = 1;
148	if (bootverbose)
149		printf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
150			n ? "GOOD" : "BAD ",
151			min, max, max - min);
152	return (n);
153}
154
155/*
156 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
157 * we will be using.
158 */
159static void
160acpi_timer_identify(driver_t *driver, device_t parent)
161{
162    device_t	dev;
163    char	desc[40];
164    u_long	rlen, rstart;
165    int		i, j, rid, rtype;
166
167    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
168
169    if (acpi_disabled("timer"))
170	return_VOID;
171
172    if (AcpiGbl_FADT == NULL)
173	return_VOID;
174
175    if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) {
176	device_printf(parent, "could not add acpi_timer0\n");
177	return_VOID;
178    }
179    acpi_timer_dev = dev;
180
181    rid = 0;
182    rlen = AcpiGbl_FADT->PmTmLen;
183    rtype = (AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId)
184      ? SYS_RES_IOPORT : SYS_RES_MEMORY;
185    rstart = AcpiGbl_FADT->XPmTmrBlk.Address;
186    bus_set_resource(dev, rtype, rid, rstart, rlen);
187    acpi_timer_reg = bus_alloc_resource(dev, rtype, &rid, 0, ~0, 1, RF_ACTIVE);
188    if (acpi_timer_reg == NULL) {
189	device_printf(dev, "couldn't allocate I/O resource (%s 0x%lx)\n",
190	  (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart);
191	return_VOID;
192    }
193    if (testenv("debug.acpi.timer_test"))
194	acpi_timer_test();
195
196    acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
197    j = 0;
198    for(i = 0; i < 10; i++)
199	j += test_counter();
200    if (j == 10) {
201	acpi_timer_timecounter.tc_name = "ACPI-fast";
202	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
203    } else {
204	acpi_timer_timecounter.tc_name = "ACPI-safe";
205	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
206    }
207    tc_init(&acpi_timer_timecounter);
208
209    sprintf(desc, "%d-bit timer at 3.579545MHz", (AcpiGbl_FADT->TmrValExt)
210      ? 32 : 24);
211    device_set_desc_copy(dev, desc);
212
213    return_VOID;
214}
215
216static int
217acpi_timer_probe(device_t dev)
218{
219    if (dev == acpi_timer_dev)
220	return(0);
221    return(ENXIO);
222}
223
224static int
225acpi_timer_attach(device_t dev)
226{
227    return(0);
228}
229
230/*
231 * Fetch current time value from reliable hardware.
232 */
233static unsigned
234acpi_timer_get_timecount(struct timecounter *tc)
235{
236    return (read_counter());
237}
238
239/*
240 * Fetch current time value from hardware that may not correctly
241 * latch the counter.
242 */
243static unsigned
244acpi_timer_get_timecount_safe(struct timecounter *tc)
245{
246    unsigned u1, u2, u3;
247
248    u2 = read_counter();
249    u3 = read_counter();
250    do {
251	u1 = u2;
252	u2 = u3;
253	u3 = read_counter();
254    } while (u1 > u2 || u2 > u3 || (u3 - u1) > 15);
255    return (u2);
256}
257
258/*
259 * Timecounter freqency adjustment interface.
260 */
261static int
262acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
263{
264    int error;
265    u_int freq;
266
267    if (acpi_timer_timecounter.tc_frequency == 0)
268	return (EOPNOTSUPP);
269    freq = acpi_timer_frequency;
270    error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
271    if (error == 0 && req->newptr != NULL) {
272	acpi_timer_frequency = freq;
273	acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
274    }
275    return (error);
276}
277
278SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
279	    0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "");
280
281/*
282 * Test harness for verifying ACPI timer behaviour.
283 * Boot with debug.acpi.timer_test set to invoke this.
284 */
285static void
286acpi_timer_test(void)
287{
288    u_int32_t	u1, u2, u3;
289
290    u1 = read_counter();
291    u2 = read_counter();
292    u3 = read_counter();
293
294    device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
295    for (;;) {
296	/*
297	 * The failure case is where u3 > u1, but u2 does not fall between the two,
298	 * ie. it contains garbage.
299	 */
300	if (u3 > u1) {
301	    if ((u2 < u1) || (u2 > u3))
302		device_printf(acpi_timer_dev, "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
303			      u1, u2, u3);
304	}
305	u1 = u2;
306	u2 = u3;
307	u3 = read_counter();
308    }
309}
310
311/*
312 * Chipset workaround driver hung off PCI.
313 *
314 * Some ACPI timers are known or believed to suffer from implementation
315 * problems which can lead to erroneous values being read from the timer.
316 *
317 * Since we can't trust unknown chipsets, we default to a timer-read
318 * routine which compensates for the most common problem (as detailed
319 * in the excerpt from the Intel PIIX4 datasheet below).
320 *
321 * When we detect a known-functional chipset, we disable the workaround
322 * to improve speed.
323 *
324 * ] 20. ACPI Timer Errata
325 * ]
326 * ]   Problem: The power management timer may return improper result when
327 * ]   read. Although the timer value settles properly after incrementing,
328 * ]   while incrementing there is a 3nS window every 69.8nS where the
329 * ]   timer value is indeterminate (a 4.2% chance that the data will be
330 * ]   incorrect when read). As a result, the ACPI free running count up
331 * ]   timer specification is violated due to erroneous reads.  Implication:
332 * ]   System hangs due to the "inaccuracy" of the timer when used by
333 * ]   software for time critical events and delays.
334 * ]
335 * ] Workaround: Read the register twice and compare.
336 * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
337 * ] in the PIIX4M.
338 *
339 * The counter is in other words not latched to the PCI bus clock when
340 * read.  Notice the workaround isn't:  We need to read until we have
341 * three monotonic samples and then use the middle one, otherwise we are
342 * not protected against the fact that the bits can be wrong in two
343 * directions.  If we only cared about monosity two reads would be enough.
344 */
345
346#if 0
347static int	acpi_timer_pci_probe(device_t dev);
348
349static device_method_t acpi_timer_pci_methods[] = {
350    DEVMETHOD(device_probe,	acpi_timer_pci_probe),
351    {0, 0}
352};
353
354static driver_t acpi_timer_pci_driver = {
355    "acpi_timer_pci",
356    acpi_timer_pci_methods,
357    0,
358};
359
360devclass_t acpi_timer_pci_devclass;
361DRIVER_MODULE(acpi_timer_pci, pci, acpi_timer_pci_driver, acpi_timer_pci_devclass, 0, 0);
362
363/*
364 * Look at PCI devices going past; if we detect one we know contains
365 * a functional ACPI timer device, enable the faster timecounter read
366 * routine.
367 */
368static int
369acpi_timer_pci_probe(device_t dev)
370{
371    int vendor, device, revid;
372
373    vendor = pci_get_vendor(dev);
374    device = pci_get_device(dev);
375    revid  = pci_get_revid(dev);
376
377    if (((vendor == 0x8086) && (device == 0x7113) && (revid >= 0x03))	|| /* PIIX4M */
378	((vendor == 0x8086) && (device == 0x719b)) 			|| /* i440MX */
379	0) {
380
381	acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
382	acpi_timer_timecounter.tc_name = "ACPI-fast";
383	if (bootverbose)
384	    device_printf(acpi_timer_dev, "functional ACPI timer detected, enabling fast timecount interface\n");
385    }
386
387    return(ENXIO);		/* we never match anything */
388}
389#endif
390