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