Merge branch 'vendor/GCC50'
[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.35 2004/07/22 05:42:14 njl Exp $
28  */
29 #include "opt_acpi.h"
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/sysctl.h>
35 #include <sys/systimer.h>
36 #include <sys/rman.h>
37
38 #include <machine/lock.h>
39 #include <bus/pci/pcivar.h>
40
41 #include "acpi.h"
42 #include "accommon.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 ACPICA 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 int      acpi_timer_test(void);
89
90 static device_method_t acpi_timer_methods[] = {
91     DEVMETHOD(device_identify,  acpi_timer_identify),
92     DEVMETHOD(device_probe,     acpi_timer_probe),
93     DEVMETHOD(device_attach,    acpi_timer_attach),
94
95     DEVMETHOD_END
96 };
97
98 static driver_t acpi_timer_driver = {
99     "acpi_timer",
100     acpi_timer_methods,
101     0,
102 };
103
104 static devclass_t acpi_timer_devclass;
105 DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, NULL, NULL);
106 MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
107
108 static inline uint32_t
109 acpi_timer_read(void)
110 {
111     return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0));
112 }
113
114 /*
115  * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
116  * we will be using.
117  */
118 static int
119 acpi_timer_identify(driver_t *driver, device_t parent)
120 {
121     device_t dev;
122     u_long rlen, rstart;
123     int rid, rtype;
124
125     /*
126      * Just try once, do nothing if the 'acpi' bus is rescanned.
127      */
128     if (device_get_state(parent) == DS_ATTACHED)
129         return (0);
130
131     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
132
133     if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) ||
134         acpi_timer_dev)
135         return (ENXIO);
136
137     if ((dev = BUS_ADD_CHILD(parent, parent, 0, "acpi_timer", 0)) == NULL) {
138         device_printf(parent, "could not add acpi_timer0\n");
139         return (ENXIO);
140     }
141     acpi_timer_dev = dev;
142
143     switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
144     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
145         rtype = SYS_RES_MEMORY;
146         break;
147     case ACPI_ADR_SPACE_SYSTEM_IO:
148         rtype = SYS_RES_IOPORT;
149         break;
150     default:
151         return (ENXIO);
152     }
153     rid = 0;
154     rlen = AcpiGbl_FADT.PmTimerLength;
155     rstart = AcpiGbl_FADT.XPmTimerBlock.Address;
156     if (bus_set_resource(dev, rtype, rid, rstart, rlen, -1)) {
157         device_printf(dev, "couldn't set resource (%s 0x%lx+0x%lx)\n",
158             (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen);
159         return (ENXIO);
160     }
161     return (0);
162 }
163
164 static int
165 acpi_timer_probe(device_t dev)
166 {
167     char desc[40];
168     int i, j, rid, rtype;
169
170     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
171
172     if (dev != acpi_timer_dev)
173         return (ENXIO);
174
175     switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
176     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
177         rtype = SYS_RES_MEMORY;
178         break;
179     case ACPI_ADR_SPACE_SYSTEM_IO:
180         rtype = SYS_RES_IOPORT;
181         break;
182     default:
183         return (ENXIO);
184     }
185     rid = 0;
186     acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
187     if (acpi_timer_reg == NULL) {
188         device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n",
189             (rtype == SYS_RES_IOPORT) ? "port" : "mem",
190             (u_long)AcpiGbl_FADT.XPmTimerBlock.Address);
191         return (ENXIO);
192     }
193     acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
194     acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
195     if ((AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) != 0)
196         acpi_counter_mask = 0xffffffff;
197     else
198         acpi_counter_mask = 0x00ffffff;
199
200     /*
201      * If all tests of the counter succeed, use the ACPI-fast method.  If
202      * at least one failed, default to using the safe routine, which reads
203      * the timer multiple times to get a consistent value before returning.
204      */
205     j = 0;
206     for (i = 0; i < 10; i++)
207         j += acpi_timer_test();
208     if (j == 10) {
209         if (acpi_counter_mask == 0xffffffff) {
210             acpi_cputimer.name = "ACPI-fast";
211             acpi_cputimer.count = acpi_timer_get_timecount;
212         } else {
213             acpi_cputimer.name = "ACPI-fast24";
214             acpi_cputimer.count = acpi_timer_get_timecount24;
215         }
216     } else {
217         if (acpi_counter_mask == 0xffffffff)
218                 acpi_cputimer.name = "ACPI-safe";
219         else
220                 acpi_cputimer.name = "ACPI-safe24";
221         acpi_cputimer.count = acpi_timer_get_timecount_safe;
222     }
223
224     ksprintf(desc, "%d-bit timer at 3.579545MHz",
225             (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) ? 32 : 24);
226     device_set_desc_copy(dev, desc);
227
228     cputimer_register(&acpi_cputimer);
229     cputimer_select(&acpi_cputimer, 0);
230     /* Release the resource, we'll allocate it again during attach. */
231     bus_release_resource(dev, rtype, rid, acpi_timer_reg);
232     return (0);
233 }
234
235 static int
236 acpi_timer_attach(device_t dev)
237 {
238     int rid, rtype;
239
240     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
241
242     switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
243     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
244         rtype = SYS_RES_MEMORY;
245         break;
246     case ACPI_ADR_SPACE_SYSTEM_IO:
247         rtype = SYS_RES_IOPORT;
248         break;
249     default:
250         return (ENXIO);
251     }
252     rid = 0;
253     acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
254     if (acpi_timer_reg == NULL)
255         return (ENXIO);
256     acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
257     acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
258     return (0);
259 }
260
261 /*
262  * Construct the timer.  Adjust the base so the system clock does not
263  * jump weirdly.
264  */
265 static void
266 acpi_timer_construct(struct cputimer *timer, sysclock_t oldclock)
267 {
268     timer->base = 0;
269     timer->base = oldclock - acpi_timer_get_timecount_safe();
270 }
271
272 /*
273  * Fetch current time value from reliable hardware.
274  *
275  * The cputimer interface requires a 32 bit return value.  If the ACPI timer
276  * is only 24 bits then we have to keep track of the upper 8 bits on our
277  * own.
278  *
279  * XXX we could probably get away with using a per-cpu field for this and
280  * just use interrupt disablement instead of clock_lock.
281  */
282 static sysclock_t
283 acpi_timer_get_timecount24(void)
284 {
285     sysclock_t counter;
286
287     clock_lock();
288     counter = acpi_timer_read();
289     if (counter < acpi_last_counter)
290         acpi_cputimer.base += 0x01000000;
291     acpi_last_counter = counter;
292     counter += acpi_cputimer.base;
293     clock_unlock();
294     return (counter);
295 }
296
297 static sysclock_t
298 acpi_timer_get_timecount(void)
299 {
300     return (acpi_timer_read() + acpi_cputimer.base);
301 }
302
303 /*
304  * Fetch current time value from hardware that may not correctly
305  * latch the counter.  We need to read until we have three monotonic
306  * samples and then use the middle one, otherwise we are not protected
307  * against the fact that the bits can be wrong in two directions.  If
308  * we only cared about monosity, two reads would be enough.
309  */
310 static sysclock_t
311 acpi_timer_get_timecount_safe(void)
312 {
313     u_int u1, u2, u3;
314
315     if (acpi_counter_mask != 0xffffffff)
316         clock_lock();
317
318     u2 = acpi_timer_read();
319     u3 = acpi_timer_read();
320     do {
321         u1 = u2;
322         u2 = u3;
323         u3 = acpi_timer_read();
324     } while (u1 > u2 || u2 > u3);
325
326     if (acpi_counter_mask != 0xffffffff) {
327         if (u2 < acpi_last_counter)
328             acpi_cputimer.base += 0x01000000;
329         acpi_last_counter = u2;
330         clock_unlock();
331     }
332     return (u2 + acpi_cputimer.base);
333 }
334
335 /*
336  * Timecounter freqency adjustment interface.
337  */ 
338 static int
339 acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
340 {
341     int error;
342     u_int freq;
343  
344     if (acpi_cputimer.freq == 0)
345         return (EOPNOTSUPP);
346     freq = acpi_cputimer.freq;
347     error = sysctl_handle_int(oidp, &freq, 0, req);
348     if (error == 0 && req->newptr != NULL)
349         cputimer_set_frequency(&acpi_cputimer, freq);
350
351     return (error);
352 }
353  
354 SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
355     0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "ACPI timer frequency");
356
357 /*
358  * Some ACPI timers are known or believed to suffer from implementation
359  * problems which can lead to erroneous values being read.  This function
360  * tests for consistent results from the timer and returns 1 if it believes
361  * the timer is consistent, otherwise it returns 0.
362  *
363  * It appears the cause is that the counter is not latched to the PCI bus
364  * clock when read:
365  *
366  * ] 20. ACPI Timer Errata
367  * ]
368  * ]   Problem: The power management timer may return improper result when
369  * ]   read. Although the timer value settles properly after incrementing,
370  * ]   while incrementing there is a 3nS window every 69.8nS where the
371  * ]   timer value is indeterminate (a 4.2% chance that the data will be
372  * ]   incorrect when read). As a result, the ACPI free running count up
373  * ]   timer specification is violated due to erroneous reads.  Implication:
374  * ]   System hangs due to the "inaccuracy" of the timer when used by
375  * ]   software for time critical events and delays.
376  * ]
377  * ] Workaround: Read the register twice and compare.
378  * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
379  * ] in the PIIX4M.
380  */
381
382 static int
383 acpi_timer_test(void)
384 {
385     uint32_t    last, this;
386     int         min, max, max2, n, delta;
387     register_t  s;
388
389     min = INT32_MAX;
390     max = max2 = 0;
391
392     /* Test the timer with interrupts disabled to get accurate results. */
393 #if defined(__i386__)
394     s = read_eflags();
395 #elif defined(__x86_64__)
396     s = read_rflags();
397 #else
398 #error "no read_eflags"
399 #endif
400     cpu_disable_intr();
401     last = acpi_timer_read();
402     for (n = 0; n < 2000; n++) {
403         this = acpi_timer_read();
404         delta = acpi_TimerDelta(this, last);
405         if (delta > max) {
406             max2 = max;
407             max = delta;
408         } else if (delta > max2) {
409             max2 = delta;
410         }
411         if (delta < min)
412             min = delta;
413         last = this;
414     }
415 #if defined(__i386__)
416     write_eflags(s);
417 #elif defined(__x86_64__)
418     write_rflags(s);
419 #else
420 #error "no read_eflags"
421 #endif
422
423     delta = max2 - min;
424     if ((max - min > 8 || delta > 3) && vmm_guest == VMM_GUEST_NONE)
425         n = 0;
426     else if (min < 0 || max == 0 || max2 == 0)
427         n = 0;
428     else
429         n = 1;
430     if (bootverbose) {
431         kprintf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
432                 n ? "GOOD" : "BAD ",
433                 min, max, max - min);
434     }
435
436     return (n);
437 }