Merge from vendor branch OPENSSL:
[dragonfly.git] / sys / dev / acpica5 / 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.33 2004/05/30 20:08:23 phk Exp $
28  * $DragonFly: src/sys/dev/acpica5/acpi_timer.c,v 1.3 2004/07/05 00:07:35 dillon 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/module.h>
35 #include <sys/sysctl.h>
36 #if __FreeBSD_version >= 500000
37 #include <sys/timetc.h>
38 #else
39 #include <sys/time.h>
40 #endif
41
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <sys/rman.h>
45 #include <bus/pci/pcivar.h>
46
47 #include "acpi.h"
48 #include "acpivar.h"
49
50 #if 0
51
52 /*
53  * A timecounter based on the free-running ACPI timer.
54  *
55  * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
56  */
57
58 /* Hooks for the ACPI CA debugging infrastructure */
59 #define _COMPONENT      ACPI_TIMER
60 ACPI_MODULE_NAME("TIMER")
61
62 static device_t                 acpi_timer_dev;
63 static struct resource          *acpi_timer_reg;
64 static bus_space_handle_t       acpi_timer_bsh;
65 static bus_space_tag_t          acpi_timer_bst;
66
67 static u_int    acpi_timer_frequency = 14318182 / 4;
68
69 static void     acpi_timer_identify(driver_t *driver, device_t parent);
70 static int      acpi_timer_probe(device_t dev);
71 static int      acpi_timer_attach(device_t dev);
72 static u_int    acpi_timer_get_timecount(struct timecounter *tc);
73 static u_int    acpi_timer_get_timecount_safe(struct timecounter *tc);
74 static int      acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
75 static void     acpi_timer_boot_test(void);
76
77 static u_int    acpi_timer_read(void);
78 static int      acpi_timer_test(void);
79
80 static device_method_t acpi_timer_methods[] = {
81     DEVMETHOD(device_identify,  acpi_timer_identify),
82     DEVMETHOD(device_probe,     acpi_timer_probe),
83     DEVMETHOD(device_attach,    acpi_timer_attach),
84
85     {0, 0}
86 };
87
88 static driver_t acpi_timer_driver = {
89     "acpi_timer",
90     acpi_timer_methods,
91     0,
92 };
93
94 static devclass_t acpi_timer_devclass;
95 DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
96 MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
97
98 static struct timecounter acpi_timer_timecounter = {
99         acpi_timer_get_timecount_safe,  /* get_timecount function */
100         0,                              /* no poll_pps */
101         0,                              /* no default counter_mask */
102         0,                              /* no default frequency */
103         "ACPI",                         /* name */
104         1000                            /* quality */
105 };
106
107 static u_int
108 acpi_timer_read()
109 {
110     return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0));
111 }
112
113 /*
114  * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
115  * we will be using.
116  */
117 static void
118 acpi_timer_identify(driver_t *driver, device_t parent)
119 {
120     device_t    dev;
121     char        desc[40];
122     u_long      rlen, rstart;
123     int         i, j, rid, rtype;
124
125     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
126
127     if (acpi_disabled("timer") || AcpiGbl_FADT == NULL)
128         return_VOID;
129
130     if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) {
131         device_printf(parent, "could not add acpi_timer0\n");
132         return_VOID;
133     }
134     acpi_timer_dev = dev;
135
136     rid = 0;
137     rlen = AcpiGbl_FADT->PmTmLen;
138     rtype = (AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId)
139       ? SYS_RES_IOPORT : SYS_RES_MEMORY;
140     rstart = AcpiGbl_FADT->XPmTmrBlk.Address;
141     bus_set_resource(dev, rtype, rid, rstart, rlen);
142     acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
143     if (acpi_timer_reg == NULL) {
144         device_printf(dev, "couldn't allocate I/O resource (%s 0x%lx)\n",
145                       rtype == SYS_RES_IOPORT ? "port" : "mem", rstart);
146         return_VOID;
147     }
148     acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
149     acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
150     if (AcpiGbl_FADT->TmrValExt != 0)
151         acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
152     else
153         acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
154     acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
155     if (testenv("debug.acpi.timer_test"))
156         acpi_timer_boot_test();
157
158     /*
159      * If all tests of the counter succeed, use the ACPI-fast method.  If
160      * at least one failed, default to using the safe routine, which reads
161      * the timer multiple times to get a consistent value before returning.
162      */
163     j = 0;
164     for (i = 0; i < 10; i++)
165         j += acpi_timer_test();
166     if (j == 10) {
167         acpi_timer_timecounter.tc_name = "ACPI-fast";
168         acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
169     } else {
170         acpi_timer_timecounter.tc_name = "ACPI-safe";
171         acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
172     }
173     tc_init(&acpi_timer_timecounter);
174
175     sprintf(desc, "%d-bit timer at 3.579545MHz",
176             AcpiGbl_FADT->TmrValExt ? 32 : 24);
177     device_set_desc_copy(dev, desc);
178
179     return_VOID;
180 }
181
182 static int
183 acpi_timer_probe(device_t dev)
184 {
185     if (dev == acpi_timer_dev)
186         return (0);
187
188     return (ENXIO);
189 }
190
191 static int
192 acpi_timer_attach(device_t dev)
193 {
194     return (0);
195 }
196
197 /*
198  * Fetch current time value from reliable hardware.
199  */
200 static u_int
201 acpi_timer_get_timecount(struct timecounter *tc)
202 {
203     return (acpi_timer_read());
204 }
205
206 /*
207  * Fetch current time value from hardware that may not correctly
208  * latch the counter.  We need to read until we have three monotonic
209  * samples and then use the middle one, otherwise we are not protected
210  * against the fact that the bits can be wrong in two directions.  If
211  * we only cared about monosity, two reads would be enough.
212  */
213 static u_int
214 acpi_timer_get_timecount_safe(struct timecounter *tc)
215 {
216     u_int u1, u2, u3;
217
218     u2 = acpi_timer_read();
219     u3 = acpi_timer_read();
220     do {
221         u1 = u2;
222         u2 = u3;
223         u3 = acpi_timer_read();
224     } while (u1 > u2 || u2 > u3);
225
226     return (u2);
227 }
228
229 /*
230  * Timecounter freqency adjustment interface.
231  */ 
232 static int
233 acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
234 {
235     int error;
236     u_int freq;
237  
238     if (acpi_timer_timecounter.tc_frequency == 0)
239         return (EOPNOTSUPP);
240     freq = acpi_timer_frequency;
241     error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
242     if (error == 0 && req->newptr != NULL) {
243         acpi_timer_frequency = freq;
244         acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
245     }
246
247     return (error);
248 }
249  
250 SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
251             0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "");
252
253 /*
254  * Some ACPI timers are known or believed to suffer from implementation
255  * problems which can lead to erroneous values being read.  This function
256  * tests for consistent results from the timer and returns 1 if it believes
257  * the timer is consistent, otherwise it returns 0.
258  *
259  * It appears the cause is that the counter is not latched to the PCI bus
260  * clock when read:
261  *
262  * ] 20. ACPI Timer Errata
263  * ]
264  * ]   Problem: The power management timer may return improper result when
265  * ]   read. Although the timer value settles properly after incrementing,
266  * ]   while incrementing there is a 3nS window every 69.8nS where the
267  * ]   timer value is indeterminate (a 4.2% chance that the data will be
268  * ]   incorrect when read). As a result, the ACPI free running count up
269  * ]   timer specification is violated due to erroneous reads.  Implication:
270  * ]   System hangs due to the "inaccuracy" of the timer when used by
271  * ]   software for time critical events and delays.
272  * ]
273  * ] Workaround: Read the register twice and compare.
274  * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
275  * ] in the PIIX4M.
276  */
277 #define N 2000
278 static int
279 acpi_timer_test()
280 {
281     uint32_t    last, this;
282     int         min, max, n, delta;
283     register_t  s;
284
285     min = 10000000;
286     max = 0;
287
288     /* Test the timer with interrupts disabled to get accurate results. */
289     s = intr_disable();
290     last = acpi_timer_read();
291     for (n = 0; n < N; n++) {
292         this = acpi_timer_read();
293         delta = acpi_TimerDelta(this, last);
294         if (delta > max)
295             max = delta;
296         else if (delta < min)
297             min = delta;
298         last = this;
299     }
300     intr_restore(s);
301
302     if (max - min > 2)
303         n = 0;
304     else if (min < 0 || max == 0)
305         n = 0;
306     else
307         n = 1;
308     if (bootverbose) {
309         printf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
310                 n ? "GOOD" : "BAD ",
311                 min, max, max - min);
312     }
313
314     return (n);
315 }
316 #undef N
317
318 /*
319  * Test harness for verifying ACPI timer behaviour.
320  * Boot with debug.acpi.timer_test set to invoke this.
321  */
322 static void
323 acpi_timer_boot_test(void)
324 {
325     uint32_t u1, u2, u3;
326
327     u1 = acpi_timer_read();
328     u2 = acpi_timer_read();
329     u3 = acpi_timer_read();
330
331     device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
332     for (;;) {
333         /*
334          * The failure case is where u3 > u1, but u2 does not fall between
335          * the two, ie. it contains garbage.
336          */
337         if (u3 > u1) {
338             if (u2 < u1 || u2 > u3)
339                 device_printf(acpi_timer_dev,
340                               "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
341                               u1, u2, u3);
342         }
343         u1 = u2;
344         u2 = u3;
345         u3 = acpi_timer_read();
346     }
347 }
348
349 #endif /* 0 */