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