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