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