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