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