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 UINT32 acpi_timer_resolution; 57 58 static sysclock_t acpi_timer_get_timecount(void); 59 static sysclock_t acpi_timer_get_timecount24(void); 60 static sysclock_t acpi_timer_get_timecount_safe(void); 61 static void acpi_timer_construct(struct cputimer *timer, sysclock_t oldclock); 62 63 static struct cputimer acpi_cputimer = { 64 .next = SLIST_ENTRY_INITIALIZER, 65 .name = "ACPI", 66 .pri = CPUTIMER_PRI_ACPI, 67 .type = CPUTIMER_ACPI, 68 .count = acpi_timer_get_timecount_safe, 69 .fromhz = cputimer_default_fromhz, 70 .fromus = cputimer_default_fromus, 71 .construct = acpi_timer_construct, 72 .destruct = cputimer_default_destruct, 73 .freq = ACPI_PM_TIMER_FREQUENCY 74 }; 75 76 static int acpi_timer_identify(driver_t *driver, device_t parent); 77 static int acpi_timer_probe(device_t dev); 78 static int acpi_timer_attach(device_t dev); 79 static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS); 80 81 static int acpi_timer_test(void); 82 83 static device_method_t acpi_timer_methods[] = { 84 DEVMETHOD(device_identify, acpi_timer_identify), 85 DEVMETHOD(device_probe, acpi_timer_probe), 86 DEVMETHOD(device_attach, acpi_timer_attach), 87 88 DEVMETHOD_END 89 }; 90 91 static driver_t acpi_timer_driver = { 92 "acpi_timer", 93 acpi_timer_methods, 94 0, 95 .gpri = KOBJ_GPRI_ACPI+2 96 }; 97 98 static devclass_t acpi_timer_devclass; 99 DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, NULL, NULL); 100 MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1); 101 102 /* 103 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources 104 * we will be using. 105 */ 106 static int 107 acpi_timer_identify(driver_t *driver, device_t parent) 108 { 109 device_t dev; 110 111 /* 112 * Just try once, do nothing if the 'acpi' bus is rescanned. 113 */ 114 if (device_get_state(parent) == DS_ATTACHED) 115 return (0); 116 117 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 118 119 if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) || 120 acpi_timer_dev) 121 return (ENXIO); 122 123 if ((dev = BUS_ADD_CHILD(parent, parent, 0, "acpi_timer", 0)) == NULL) { 124 device_printf(parent, "could not add acpi_timer0\n"); 125 return (ENXIO); 126 } 127 acpi_timer_dev = dev; 128 129 return (0); 130 } 131 132 static int 133 acpi_timer_probe(device_t dev) 134 { 135 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 136 137 if (dev != acpi_timer_dev) 138 return (ENXIO); 139 140 if (ACPI_FAILURE(AcpiGetTimerResolution(&acpi_timer_resolution))) 141 return (ENXIO); 142 143 return (0); 144 } 145 146 static int 147 acpi_timer_attach(device_t dev) 148 { 149 char desc[40]; 150 int i, j; 151 152 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 153 154 /* 155 * If all tests of the counter succeed, use the ACPI-fast method. If 156 * at least one failed, default to using the safe routine, which reads 157 * the timer multiple times to get a consistent value before returning. 158 */ 159 j = 0; 160 for (i = 0; i < 10; i++) 161 j += acpi_timer_test(); 162 if (j == 10) { 163 if (acpi_timer_resolution == 32) { 164 acpi_cputimer.name = "ACPI-fast"; 165 acpi_cputimer.count = acpi_timer_get_timecount; 166 } else { 167 acpi_cputimer.name = "ACPI-fast24"; 168 acpi_cputimer.count = acpi_timer_get_timecount24; 169 } 170 } else { 171 if (acpi_timer_resolution == 32) 172 acpi_cputimer.name = "ACPI-safe"; 173 else 174 acpi_cputimer.name = "ACPI-safe24"; 175 acpi_cputimer.count = acpi_timer_get_timecount_safe; 176 } 177 178 ksprintf(desc, "%u-bit timer at 3.579545MHz", acpi_timer_resolution); 179 device_set_desc_copy(dev, desc); 180 181 cputimer_register(&acpi_cputimer); 182 cputimer_select(&acpi_cputimer, 0); 183 184 return (0); 185 } 186 187 /* 188 * Construct the timer. Adjust the base so the system clock does not 189 * jump weirdly. 190 */ 191 static void 192 acpi_timer_construct(struct cputimer *timer, sysclock_t oldclock) 193 { 194 timer->base = 0; 195 timer->base = oldclock - acpi_timer_get_timecount_safe(); 196 } 197 198 /* 199 * Fetch current time value from reliable hardware. 200 * 201 * The cputimer interface requires a 32 bit return value. If the ACPI timer 202 * is only 24 bits then we have to keep track of the upper 8 bits on our 203 * own. 204 * 205 * per-cpu tracking fields can cause problems on VMs if one or more cpus 206 * stalls long-enough for the timer to turn-over twice, so instead optimize 207 * the locking case by not updating acpi_cputimer.base until the timer 208 * has gone more than 1/16 its full range. 209 * 210 * These are horrible hacks, but at least the SMP interference is minimal 211 * with them. Note that just reading the ACPI timer itself represents a 212 * bottleneck due to the slow I/O. 213 */ 214 static sysclock_t 215 acpi_timer_get_timecount24(void) 216 { 217 sysclock_t last_counter; 218 sysclock_t next_counter; 219 uint32_t counter; 220 221 last_counter = acpi_cputimer.base; 222 for (;;) { 223 cpu_ccfence(); 224 AcpiGetTimer(&counter); 225 if (counter < (last_counter & 0x00FFFFFFU)) 226 next_counter = ((last_counter + 0x01000000U) & 227 0xFFFFFFFFFF000000LU) | counter; 228 else 229 next_counter = (last_counter & 230 0xFFFFFFFFFF000000LU) | counter; 231 if (atomic_fcmpset_long(&acpi_cputimer.base, &last_counter, 232 next_counter)) { 233 break; 234 } 235 } 236 return next_counter; 237 } 238 239 static sysclock_t 240 acpi_timer_get_timecount(void) 241 { 242 sysclock_t last_counter; 243 sysclock_t next_counter; 244 uint32_t counter; 245 246 last_counter = acpi_cputimer.base; 247 for (;;) { 248 cpu_ccfence(); 249 AcpiGetTimer(&counter); 250 if (counter < (last_counter & 0xFFFFFFFFU)) 251 next_counter = ((last_counter + 0x0100000000U) & 252 0xFFFFFFFF00000000LU) | counter; 253 else 254 next_counter = (last_counter & 255 0xFFFFFFFF00000000LU) | counter; 256 if (atomic_fcmpset_long(&acpi_cputimer.base, &last_counter, 257 next_counter)) { 258 break; 259 } 260 } 261 return next_counter; 262 } 263 264 /* 265 * Fetch current time value from hardware that may not correctly 266 * latch the counter. We need to read until we have three monotonic 267 * samples and then use the middle one, otherwise we are not protected 268 * against the fact that the bits can be wrong in two directions. If 269 * we only cared about monosity, two reads would be enough. 270 */ 271 static __inline sysclock_t 272 _acpi_timer_get_timecount_safe(void) 273 { 274 u_int u1, u2, u3; 275 276 AcpiGetTimer(&u2); 277 AcpiGetTimer(&u3); 278 do { 279 u1 = u2; 280 u2 = u3; 281 AcpiGetTimer(&u3); 282 } while (u1 > u2 || u2 > u3); 283 284 return (u2); 285 } 286 287 static sysclock_t 288 acpi_timer_get_timecount_safe(void) 289 { 290 sysclock_t last_counter; 291 sysclock_t next_counter; 292 uint32_t counter; 293 294 last_counter = acpi_cputimer.base; 295 for (;;) { 296 cpu_ccfence(); 297 counter = _acpi_timer_get_timecount_safe(); 298 299 if (acpi_timer_resolution == 32) { 300 if (counter < (last_counter & 0xFFFFFFFFU)) 301 next_counter = ((last_counter + 0x0100000000U) & 302 0xFFFFFFFF00000000LU) | counter; 303 else 304 next_counter = (last_counter & 305 0xFFFFFFFF00000000LU) | counter; 306 } else { 307 if (counter < (last_counter & 0x00FFFFFFU)) 308 next_counter = ((last_counter + 0x01000000U) & 309 0xFFFFFFFFFF000000LU) | counter; 310 else 311 next_counter = (last_counter & 312 0xFFFFFFFFFF000000LU) | counter; 313 } 314 if (atomic_fcmpset_long(&acpi_cputimer.base, &last_counter, 315 next_counter)) { 316 break; 317 } 318 } 319 return next_counter; 320 } 321 322 /* 323 * Timecounter freqency adjustment interface. 324 */ 325 static int 326 acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS) 327 { 328 int error; 329 u_int freq; 330 331 if (acpi_cputimer.freq == 0) 332 return (EOPNOTSUPP); 333 freq = acpi_cputimer.freq; 334 error = sysctl_handle_int(oidp, &freq, 0, req); 335 if (error == 0 && req->newptr != NULL) 336 cputimer_set_frequency(&acpi_cputimer, freq); 337 338 return (error); 339 } 340 341 SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW, 342 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "ACPI timer frequency"); 343 344 /* 345 * Some ACPI timers are known or believed to suffer from implementation 346 * problems which can lead to erroneous values being read. This function 347 * tests for consistent results from the timer and returns 1 if it believes 348 * the timer is consistent, otherwise it returns 0. 349 * 350 * It appears the cause is that the counter is not latched to the PCI bus 351 * clock when read: 352 * 353 * ] 20. ACPI Timer Errata 354 * ] 355 * ] Problem: The power management timer may return improper result when 356 * ] read. Although the timer value settles properly after incrementing, 357 * ] while incrementing there is a 3nS window every 69.8nS where the 358 * ] timer value is indeterminate (a 4.2% chance that the data will be 359 * ] incorrect when read). As a result, the ACPI free running count up 360 * ] timer specification is violated due to erroneous reads. Implication: 361 * ] System hangs due to the "inaccuracy" of the timer when used by 362 * ] software for time critical events and delays. 363 * ] 364 * ] Workaround: Read the register twice and compare. 365 * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed 366 * ] in the PIIX4M. 367 */ 368 369 static int 370 acpi_timer_test(void) 371 { 372 uint32_t last, this; 373 int min, max, max2, n, delta; 374 register_t s; 375 376 min = INT32_MAX; 377 max = max2 = 0; 378 379 /* Test the timer with interrupts disabled to get accurate results. */ 380 #if defined(__x86_64__) 381 s = read_rflags(); 382 #else 383 #error "no read_*flags" 384 #endif 385 cpu_disable_intr(); 386 AcpiGetTimer(&last); 387 for (n = 0; n < 2000; n++) { 388 AcpiGetTimer(&this); 389 delta = acpi_TimerDelta(this, last); 390 if (delta > max) { 391 max2 = max; 392 max = delta; 393 } else if (delta > max2) { 394 max2 = delta; 395 } 396 if (delta < min) 397 min = delta; 398 last = this; 399 } 400 /* cpu_enable_intr(); restored to original by write_rflags() */ 401 #if defined(__x86_64__) 402 write_rflags(s); 403 #else 404 #error "no read_*flags" 405 #endif 406 407 delta = max2 - min; 408 if ((max - min > 8 || delta > 3) && vmm_guest == VMM_GUEST_NONE) 409 n = 0; 410 else if (min < 0 || max == 0 || max2 == 0) 411 n = 0; 412 else 413 n = 1; 414 if (bootverbose) { 415 kprintf("ACPI timer looks %s min = %d, max = %d, width = %d\n", 416 n ? "GOOD" : "BAD ", 417 min, max, max - min); 418 } 419 420 return (n); 421 } 422