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