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/sysctl.h> 34 #include <sys/timetc.h> 35 36 #include <machine/bus_pio.h> 37 #include <machine/bus.h> 38 #include <machine/resource.h> 39 #include <sys/rman.h> 40 41 #include "acpi.h" 42 43 #include <acpica/acpivar.h> 44 #include <pci/pcivar.h> 45 46 /* 47 * A timecounter based on the free-running ACPI timer. 48 * 49 * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>. 50 */ 51 52 /* 53 * Hooks for the ACPI CA debugging infrastructure 54 */ 55 #define _COMPONENT ACPI_SYSTEM 56 ACPI_MODULE_NAME("TIMER") 57 58 static device_t acpi_timer_dev; 59 struct resource *acpi_timer_reg; 60 #define TIMER_READ bus_space_read_4(rman_get_bustag(acpi_timer_reg), \ 61 rman_get_bushandle(acpi_timer_reg), \ 62 0) 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 unsigned acpi_timer_get_timecount(struct timecounter *tc); 70 static unsigned acpi_timer_get_timecount_safe(struct timecounter *tc); 71 static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS); 72 static void acpi_timer_test(void); 73 74 /* 75 * Driver hung off ACPI. 76 */ 77 static device_method_t acpi_timer_methods[] = { 78 DEVMETHOD(device_identify, acpi_timer_identify), 79 DEVMETHOD(device_probe, acpi_timer_probe), 80 DEVMETHOD(device_attach, acpi_timer_attach), 81 82 {0, 0} 83 }; 84 85 static driver_t acpi_timer_driver = { 86 "acpi_timer", 87 acpi_timer_methods, 88 0, 89 }; 90 91 static devclass_t acpi_timer_devclass; 92 DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0); 93 94 /* 95 * Timecounter. 96 */ 97 static struct timecounter acpi_timer_timecounter = { 98 acpi_timer_get_timecount_safe, 99 0, 100 0xffffff, 101 0, 102 "ACPI" 103 }; 104 105 static int test_counter(void); 106 107 #define N 2000 108 static int 109 test_counter() 110 { 111 int min, max, n, delta; 112 unsigned last, this; 113 114 min = 10000000; 115 max = 0; 116 last = TIMER_READ; 117 for (n = 0; n < N; n++) { 118 this = TIMER_READ; 119 delta = (this - last) & 0xffffff; 120 if (delta > max) 121 max = delta; 122 else if (delta < min) 123 min = delta; 124 last = this; 125 } 126 if (max - min > 2) 127 n = 0; 128 else if (min < 0) 129 n = 0; 130 else 131 n = 1; 132 if (bootverbose) 133 printf("ACPI timer looks %s min = %d, max = %d, width = %d\n", 134 n ? "GOOD" : "BAD ", 135 min, max, max - min + 1); 136 return (n); 137 } 138 139 /* 140 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources 141 * we will be using. 142 */ 143 static void 144 acpi_timer_identify(driver_t *driver, device_t parent) 145 { 146 device_t dev; 147 char desc[40]; 148 int rid, i, j; 149 150 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 151 152 if (acpi_disabled("timer")) 153 return_VOID; 154 155 if (AcpiGbl_FADT == NULL) 156 return_VOID; 157 158 if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) { 159 device_printf(parent, "could not add acpi_timer0\n"); 160 return_VOID; 161 } 162 acpi_timer_dev = dev; 163 rid = 0; 164 bus_set_resource(dev, SYS_RES_IOPORT, rid, AcpiGbl_FADT->V1_PmTmrBlk, sizeof(u_int32_t)); 165 if ((acpi_timer_reg = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE)) == NULL) { 166 device_printf(dev, "couldn't allocate I/O resource (port 0x%x)\n", AcpiGbl_FADT->V1_PmTmrBlk); 167 return_VOID; 168 } 169 if (testenv("debug.acpi.timer_test")) 170 acpi_timer_test(); 171 172 acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 173 j = 0; 174 for(i = 0; i < 10; i++) 175 j += test_counter(); 176 if (j == 10) { 177 acpi_timer_timecounter.tc_name = "ACPI-fast"; 178 acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount; 179 } else { 180 acpi_timer_timecounter.tc_name = "ACPI-safe"; 181 acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe; 182 } 183 tc_init(&acpi_timer_timecounter); 184 185 sprintf(desc, "%d-bit timer at 3.579545MHz", AcpiGbl_FADT->TmrValExt ? 32 : 24); 186 device_set_desc_copy(dev, desc); 187 188 return_VOID; 189 } 190 191 static int 192 acpi_timer_probe(device_t dev) 193 { 194 if (dev == acpi_timer_dev) 195 return(0); 196 return(ENXIO); 197 } 198 199 static int 200 acpi_timer_attach(device_t dev) 201 { 202 return(0); 203 } 204 205 /* 206 * Fetch current time value from reliable hardware. 207 */ 208 static unsigned 209 acpi_timer_get_timecount(struct timecounter *tc) 210 { 211 return(TIMER_READ); 212 } 213 214 /* 215 * Fetch current time value from hardware that may not correctly 216 * latch the counter. 217 */ 218 static unsigned 219 acpi_timer_get_timecount_safe(struct timecounter *tc) 220 { 221 unsigned u1, u2, u3; 222 223 u2 = TIMER_READ; 224 u3 = TIMER_READ; 225 do { 226 u1 = u2; 227 u2 = u3; 228 u3 = TIMER_READ; 229 } while (u1 > u2 || u2 > u3 || (u3 - u1) > 15); 230 return (u2); 231 } 232 233 /* 234 * Timecounter freqency adjustment interface. 235 */ 236 static int 237 acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS) 238 { 239 int error; 240 u_int freq; 241 242 if (acpi_timer_timecounter.tc_frequency == 0) 243 return (EOPNOTSUPP); 244 freq = acpi_timer_frequency; 245 error = sysctl_handle_int(oidp, &freq, sizeof(freq), req); 246 if (error == 0 && req->newptr != NULL) { 247 acpi_timer_frequency = freq; 248 acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 249 } 250 return (error); 251 } 252 253 SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW, 254 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", ""); 255 256 /* 257 * Test harness for verifying ACPI timer behaviour. 258 * Boot with debug.acpi.timer_test set to invoke this. 259 */ 260 static void 261 acpi_timer_test(void) 262 { 263 u_int32_t u1, u2, u3; 264 265 u1 = TIMER_READ; 266 u2 = TIMER_READ; 267 u3 = TIMER_READ; 268 269 device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n"); 270 for (;;) { 271 /* 272 * The failure case is where u3 > u1, but u2 does not fall between the two, 273 * ie. it contains garbage. 274 */ 275 if (u3 > u1) { 276 if ((u2 < u1) || (u2 > u3)) 277 device_printf(acpi_timer_dev, "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n", 278 u1, u2, u3); 279 } 280 u1 = u2; 281 u2 = u3; 282 u3 = TIMER_READ; 283 } 284 } 285 286 /* 287 * Chipset workaround driver hung off PCI. 288 * 289 * Some ACPI timers are known or believed to suffer from implementation 290 * problems which can lead to erroneous values being read from the timer. 291 * 292 * Since we can't trust unknown chipsets, we default to a timer-read 293 * routine which compensates for the most common problem (as detailed 294 * in the excerpt from the Intel PIIX4 datasheet below). 295 * 296 * When we detect a known-functional chipset, we disable the workaround 297 * to improve speed. 298 * 299 * ] 20. ACPI Timer Errata 300 * ] 301 * ] Problem: The power management timer may return improper result when 302 * ] read. Although the timer value settles properly after incrementing, 303 * ] while incrementing there is a 3nS window every 69.8nS where the 304 * ] timer value is indeterminate (a 4.2% chance that the data will be 305 * ] incorrect when read). As a result, the ACPI free running count up 306 * ] timer specification is violated due to erroneous reads. Implication: 307 * ] System hangs due to the "inaccuracy" of the timer when used by 308 * ] software for time critical events and delays. 309 * ] 310 * ] Workaround: Read the register twice and compare. 311 * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed 312 * ] in the PIIX4M. 313 * 314 * The counter is in other words not latched to the PCI bus clock when 315 * read. Notice the workaround isn't: We need to read until we have 316 * three monotonic samples and then use the middle one, otherwise we are 317 * not protected against the fact that the bits can be wrong in two 318 * directions. If we only cared about monosity two reads would be enough. 319 */ 320 321 #if 0 322 static int acpi_timer_pci_probe(device_t dev); 323 324 static device_method_t acpi_timer_pci_methods[] = { 325 DEVMETHOD(device_probe, acpi_timer_pci_probe), 326 {0, 0} 327 }; 328 329 static driver_t acpi_timer_pci_driver = { 330 "acpi_timer_pci", 331 acpi_timer_pci_methods, 332 0, 333 }; 334 335 devclass_t acpi_timer_pci_devclass; 336 DRIVER_MODULE(acpi_timer_pci, pci, acpi_timer_pci_driver, acpi_timer_pci_devclass, 0, 0); 337 338 /* 339 * Look at PCI devices going past; if we detect one we know contains 340 * a functional ACPI timer device, enable the faster timecounter read 341 * routine. 342 */ 343 static int 344 acpi_timer_pci_probe(device_t dev) 345 { 346 int vendor, device, revid; 347 348 vendor = pci_get_vendor(dev); 349 device = pci_get_device(dev); 350 revid = pci_get_revid(dev); 351 352 if (((vendor == 0x8086) && (device == 0x7113) && (revid >= 0x03)) || /* PIIX4M */ 353 ((vendor == 0x8086) && (device == 0x719b)) || /* i440MX */ 354 0) { 355 356 acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount; 357 acpi_timer_timecounter.tc_name = "ACPI-fast"; 358 if (bootverbose) 359 device_printf(acpi_timer_dev, "functional ACPI timer detected, enabling fast timecount interface\n"); 360 } 361 362 return(ENXIO); /* we never match anything */ 363 } 364 #endif 365