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