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