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