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