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