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