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