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/eventhandler.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/sysctl.h> 38 #include <sys/timetc.h> 39 40 #include <machine/bus.h> 41 #include <machine/resource.h> 42 #include <sys/rman.h> 43 44 #include <contrib/dev/acpica/include/acpi.h> 45 #include <contrib/dev/acpica/include/accommon.h> 46 47 #include <dev/acpica/acpivar.h> 48 #include <dev/pci/pcivar.h> 49 50 /* 51 * A timecounter based on the free-running ACPI timer. 52 * 53 * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>. 54 */ 55 56 /* Hooks for the ACPI CA debugging infrastructure */ 57 #define _COMPONENT ACPI_TIMER 58 ACPI_MODULE_NAME("TIMER") 59 60 static device_t acpi_timer_dev; 61 static struct resource *acpi_timer_reg; 62 static bus_space_handle_t acpi_timer_bsh; 63 static bus_space_tag_t acpi_timer_bst; 64 static eventhandler_tag acpi_timer_eh; 65 66 static u_int acpi_timer_frequency = 14318182 / 4; 67 68 /* Knob to disable acpi_timer device */ 69 bool acpi_timer_disabled = false; 70 71 static void acpi_timer_identify(driver_t *driver, device_t parent); 72 static int acpi_timer_probe(device_t dev); 73 static int acpi_timer_attach(device_t dev); 74 static void acpi_timer_resume_handler(struct timecounter *); 75 static void acpi_timer_suspend_handler(struct timecounter *); 76 static u_int acpi_timer_get_timecount(struct timecounter *tc); 77 static u_int acpi_timer_get_timecount_safe(struct timecounter *tc); 78 static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS); 79 static void acpi_timer_boot_test(void); 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 }; 96 97 static devclass_t acpi_timer_devclass; 98 DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0); 99 MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1); 100 101 static struct timecounter acpi_timer_timecounter = { 102 acpi_timer_get_timecount_safe, /* get_timecount function */ 103 0, /* no poll_pps */ 104 0, /* no default counter_mask */ 105 0, /* no default frequency */ 106 "ACPI", /* name */ 107 -1 /* quality (chosen later) */ 108 }; 109 110 static __inline uint32_t 111 acpi_timer_read(void) 112 { 113 114 return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0)); 115 } 116 117 /* 118 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources 119 * we will be using. 120 */ 121 static void 122 acpi_timer_identify(driver_t *driver, device_t parent) 123 { 124 device_t dev; 125 rman_res_t rlen, rstart; 126 int rid, rtype; 127 128 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 129 130 if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) || 131 acpi_timer_dev || acpi_timer_disabled || 132 AcpiGbl_FADT.PmTimerLength == 0) 133 return_VOID; 134 135 if ((dev = BUS_ADD_CHILD(parent, 2, "acpi_timer", 0)) == NULL) { 136 device_printf(parent, "could not add acpi_timer0\n"); 137 return_VOID; 138 } 139 acpi_timer_dev = dev; 140 141 switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) { 142 case ACPI_ADR_SPACE_SYSTEM_MEMORY: 143 rtype = SYS_RES_MEMORY; 144 break; 145 case ACPI_ADR_SPACE_SYSTEM_IO: 146 rtype = SYS_RES_IOPORT; 147 break; 148 default: 149 return_VOID; 150 } 151 rid = 0; 152 rlen = AcpiGbl_FADT.PmTimerLength; 153 rstart = AcpiGbl_FADT.XPmTimerBlock.Address; 154 if (bus_set_resource(dev, rtype, rid, rstart, rlen)) 155 device_printf(dev, "couldn't set resource (%s 0x%jx+0x%jx)\n", 156 (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen); 157 return_VOID; 158 } 159 160 static int 161 acpi_timer_probe(device_t dev) 162 { 163 char desc[40]; 164 int i, j, rid, rtype; 165 166 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 167 168 if (dev != acpi_timer_dev) 169 return (ENXIO); 170 171 switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) { 172 case ACPI_ADR_SPACE_SYSTEM_MEMORY: 173 rtype = SYS_RES_MEMORY; 174 break; 175 case ACPI_ADR_SPACE_SYSTEM_IO: 176 rtype = SYS_RES_IOPORT; 177 break; 178 default: 179 return (ENXIO); 180 } 181 rid = 0; 182 acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 183 if (acpi_timer_reg == NULL) { 184 device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n", 185 (rtype == SYS_RES_IOPORT) ? "port" : "mem", 186 (u_long)AcpiGbl_FADT.XPmTimerBlock.Address); 187 return (ENXIO); 188 } 189 acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg); 190 acpi_timer_bst = rman_get_bustag(acpi_timer_reg); 191 if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) 192 acpi_timer_timecounter.tc_counter_mask = 0xffffffff; 193 else 194 acpi_timer_timecounter.tc_counter_mask = 0x00ffffff; 195 acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 196 acpi_timer_timecounter.tc_flags = TC_FLAGS_SUSPEND_SAFE; 197 if (testenv("debug.acpi.timer_test")) 198 acpi_timer_boot_test(); 199 200 /* 201 * If all tests of the counter succeed, use the ACPI-fast method. If 202 * at least one failed, default to using the safe routine, which reads 203 * the timer multiple times to get a consistent value before returning. 204 */ 205 j = 0; 206 if (bootverbose) 207 printf("ACPI timer:"); 208 for (i = 0; i < 10; i++) 209 j += acpi_timer_test(); 210 if (bootverbose) 211 printf(" -> %d\n", j); 212 if (j == 10) { 213 acpi_timer_timecounter.tc_name = "ACPI-fast"; 214 acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount; 215 acpi_timer_timecounter.tc_quality = 900; 216 } else { 217 acpi_timer_timecounter.tc_name = "ACPI-safe"; 218 acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe; 219 acpi_timer_timecounter.tc_quality = 850; 220 } 221 tc_init(&acpi_timer_timecounter); 222 223 sprintf(desc, "%d-bit timer at %u.%06uMHz", 224 (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) != 0 ? 32 : 24, 225 acpi_timer_frequency / 1000000, acpi_timer_frequency % 1000000); 226 device_set_desc_copy(dev, desc); 227 228 /* Release the resource, we'll allocate it again during attach. */ 229 bus_release_resource(dev, rtype, rid, acpi_timer_reg); 230 return (0); 231 } 232 233 static int 234 acpi_timer_attach(device_t dev) 235 { 236 int rid, rtype; 237 238 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 239 240 switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) { 241 case ACPI_ADR_SPACE_SYSTEM_MEMORY: 242 rtype = SYS_RES_MEMORY; 243 break; 244 case ACPI_ADR_SPACE_SYSTEM_IO: 245 rtype = SYS_RES_IOPORT; 246 break; 247 default: 248 return (ENXIO); 249 } 250 rid = 0; 251 acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 252 if (acpi_timer_reg == NULL) 253 return (ENXIO); 254 acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg); 255 acpi_timer_bst = rman_get_bustag(acpi_timer_reg); 256 257 /* Register suspend event handler. */ 258 if (EVENTHANDLER_REGISTER(power_suspend, acpi_timer_suspend_handler, 259 &acpi_timer_timecounter, EVENTHANDLER_PRI_LAST) == NULL) 260 device_printf(dev, "failed to register suspend event handler\n"); 261 262 return (0); 263 } 264 265 static void 266 acpi_timer_resume_handler(struct timecounter *newtc) 267 { 268 struct timecounter *tc; 269 270 tc = timecounter; 271 if (tc != newtc) { 272 if (bootverbose) 273 device_printf(acpi_timer_dev, 274 "restoring timecounter, %s -> %s\n", 275 tc->tc_name, newtc->tc_name); 276 (void)newtc->tc_get_timecount(newtc); 277 timecounter = newtc; 278 } 279 } 280 281 static void 282 acpi_timer_suspend_handler(struct timecounter *newtc) 283 { 284 struct timecounter *tc; 285 286 /* Deregister existing resume event handler. */ 287 if (acpi_timer_eh != NULL) { 288 EVENTHANDLER_DEREGISTER(power_resume, acpi_timer_eh); 289 acpi_timer_eh = NULL; 290 } 291 292 if ((timecounter->tc_flags & TC_FLAGS_SUSPEND_SAFE) != 0) { 293 /* 294 * If we are using a suspend safe timecounter, don't 295 * save/restore it across suspend/resume. 296 */ 297 return; 298 } 299 300 KASSERT(newtc == &acpi_timer_timecounter, 301 ("acpi_timer_suspend_handler: wrong timecounter")); 302 303 tc = timecounter; 304 if (tc != newtc) { 305 if (bootverbose) 306 device_printf(acpi_timer_dev, 307 "switching timecounter, %s -> %s\n", 308 tc->tc_name, newtc->tc_name); 309 (void)acpi_timer_read(); 310 (void)acpi_timer_read(); 311 timecounter = newtc; 312 acpi_timer_eh = EVENTHANDLER_REGISTER(power_resume, 313 acpi_timer_resume_handler, tc, EVENTHANDLER_PRI_LAST); 314 } 315 } 316 317 /* 318 * Fetch current time value from reliable hardware. 319 */ 320 static u_int 321 acpi_timer_get_timecount(struct timecounter *tc) 322 { 323 return (acpi_timer_read()); 324 } 325 326 /* 327 * Fetch current time value from hardware that may not correctly 328 * latch the counter. We need to read until we have three monotonic 329 * samples and then use the middle one, otherwise we are not protected 330 * against the fact that the bits can be wrong in two directions. If 331 * we only cared about monosity, two reads would be enough. 332 */ 333 static u_int 334 acpi_timer_get_timecount_safe(struct timecounter *tc) 335 { 336 u_int u1, u2, u3; 337 338 u2 = acpi_timer_read(); 339 u3 = acpi_timer_read(); 340 do { 341 u1 = u2; 342 u2 = u3; 343 u3 = acpi_timer_read(); 344 } while (u1 > u2 || u2 > u3); 345 346 return (u2); 347 } 348 349 /* 350 * Timecounter freqency adjustment interface. 351 */ 352 static int 353 acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS) 354 { 355 int error; 356 u_int freq; 357 358 if (acpi_timer_timecounter.tc_frequency == 0) 359 return (EOPNOTSUPP); 360 freq = acpi_timer_frequency; 361 error = sysctl_handle_int(oidp, &freq, 0, req); 362 if (error == 0 && req->newptr != NULL) { 363 acpi_timer_frequency = freq; 364 acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 365 } 366 367 return (error); 368 } 369 370 SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, 371 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, sizeof(u_int), 372 acpi_timer_sysctl_freq, "I", 373 "ACPI timer frequency"); 374 375 /* 376 * Some ACPI timers are known or believed to suffer from implementation 377 * problems which can lead to erroneous values being read. This function 378 * tests for consistent results from the timer and returns 1 if it believes 379 * the timer is consistent, otherwise it returns 0. 380 * 381 * It appears the cause is that the counter is not latched to the PCI bus 382 * clock when read: 383 * 384 * ] 20. ACPI Timer Errata 385 * ] 386 * ] Problem: The power management timer may return improper result when 387 * ] read. Although the timer value settles properly after incrementing, 388 * ] while incrementing there is a 3nS window every 69.8nS where the 389 * ] timer value is indeterminate (a 4.2% chance that the data will be 390 * ] incorrect when read). As a result, the ACPI free running count up 391 * ] timer specification is violated due to erroneous reads. Implication: 392 * ] System hangs due to the "inaccuracy" of the timer when used by 393 * ] software for time critical events and delays. 394 * ] 395 * ] Workaround: Read the register twice and compare. 396 * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed 397 * ] in the PIIX4M. 398 */ 399 #define N 2000 400 static int 401 acpi_timer_test() 402 { 403 uint32_t last, this; 404 int delta, max, max2, min, n; 405 register_t s; 406 407 min = INT32_MAX; 408 max = max2 = 0; 409 410 /* Test the timer with interrupts disabled to get accurate results. */ 411 s = intr_disable(); 412 last = acpi_timer_read(); 413 for (n = 0; n < N; n++) { 414 this = acpi_timer_read(); 415 delta = acpi_TimerDelta(this, last); 416 if (delta > max) { 417 max2 = max; 418 max = delta; 419 } else if (delta > max2) 420 max2 = delta; 421 if (delta < min) 422 min = delta; 423 last = this; 424 } 425 intr_restore(s); 426 427 delta = max2 - min; 428 if ((max - min > 8 || delta > 3) && vm_guest == VM_GUEST_NO) 429 n = 0; 430 else if (min < 0 || max == 0 || max2 == 0) 431 n = 0; 432 else 433 n = 1; 434 if (bootverbose) 435 printf(" %d/%d", n, delta); 436 437 return (n); 438 } 439 #undef N 440 441 /* 442 * Test harness for verifying ACPI timer behaviour. 443 * Boot with debug.acpi.timer_test set to invoke this. 444 */ 445 static void 446 acpi_timer_boot_test(void) 447 { 448 uint32_t u1, u2, u3; 449 450 u1 = acpi_timer_read(); 451 u2 = acpi_timer_read(); 452 u3 = acpi_timer_read(); 453 454 device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n"); 455 for (;;) { 456 /* 457 * The failure case is where u3 > u1, but u2 does not fall between 458 * the two, ie. it contains garbage. 459 */ 460 if (u3 > u1) { 461 if (u2 < u1 || u2 > u3) 462 device_printf(acpi_timer_dev, 463 "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n", 464 u1, u2, u3); 465 } 466 u1 = u2; 467 u2 = u3; 468 u3 = acpi_timer_read(); 469 } 470 } 471