115e32d5dSMike Smith /*- 2787fa5b8SMike Smith * Copyright (c) 2000, 2001 Michael Smith 315e32d5dSMike Smith * Copyright (c) 2000 BSDi 415e32d5dSMike Smith * All rights reserved. 515e32d5dSMike Smith * 615e32d5dSMike Smith * Redistribution and use in source and binary forms, with or without 715e32d5dSMike Smith * modification, are permitted provided that the following conditions 815e32d5dSMike Smith * are met: 915e32d5dSMike Smith * 1. Redistributions of source code must retain the above copyright 1015e32d5dSMike Smith * notice, this list of conditions and the following disclaimer. 1115e32d5dSMike Smith * 2. Redistributions in binary form must reproduce the above copyright 1215e32d5dSMike Smith * notice, this list of conditions and the following disclaimer in the 1315e32d5dSMike Smith * documentation and/or other materials provided with the distribution. 1415e32d5dSMike Smith * 1515e32d5dSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1615e32d5dSMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1715e32d5dSMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1815e32d5dSMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1915e32d5dSMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2015e32d5dSMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2115e32d5dSMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2215e32d5dSMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2315e32d5dSMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2415e32d5dSMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2515e32d5dSMike Smith * SUCH DAMAGE. 2615e32d5dSMike Smith * 2715e32d5dSMike Smith * $FreeBSD$ 2815e32d5dSMike Smith */ 2915e32d5dSMike Smith #include "opt_acpi.h" 3015e32d5dSMike Smith #include <sys/param.h> 3115e32d5dSMike Smith #include <sys/bus.h> 32787fa5b8SMike Smith #include <sys/kernel.h> 33fe12f24bSPoul-Henning Kamp #include <sys/module.h> 34787fa5b8SMike Smith #include <sys/sysctl.h> 35787fa5b8SMike Smith #include <sys/timetc.h> 36787fa5b8SMike Smith 37787fa5b8SMike Smith #include <machine/bus.h> 38787fa5b8SMike Smith #include <machine/resource.h> 39787fa5b8SMike Smith #include <sys/rman.h> 4015e32d5dSMike Smith 4115e32d5dSMike Smith #include "acpi.h" 4287e5d361SJohn Baldwin #include <dev/acpica/acpivar.h> 43cace7a2aSWarner Losh #include <dev/pci/pcivar.h> 44787fa5b8SMike Smith 45787fa5b8SMike Smith /* 46787fa5b8SMike Smith * A timecounter based on the free-running ACPI timer. 47787fa5b8SMike Smith * 48787fa5b8SMike Smith * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>. 49787fa5b8SMike Smith */ 5015e32d5dSMike Smith 51be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 523184cf5aSNate Lawson #define _COMPONENT ACPI_TIMER 5372e5754cSMike Smith ACPI_MODULE_NAME("TIMER") 540ae55423SMike Smith 55787fa5b8SMike Smith static device_t acpi_timer_dev; 56af807c0fSNate Lawson static struct resource *acpi_timer_reg; 57d9b6df60SNate Lawson static bus_space_handle_t acpi_timer_bsh; 58d9b6df60SNate Lawson static bus_space_tag_t acpi_timer_bst; 5915e32d5dSMike Smith 60787fa5b8SMike Smith static u_int acpi_timer_frequency = 14318182 / 4; 6115e32d5dSMike Smith 6215e32d5dSMike Smith static void acpi_timer_identify(driver_t *driver, device_t parent); 6315e32d5dSMike Smith static int acpi_timer_probe(device_t dev); 6415e32d5dSMike Smith static int acpi_timer_attach(device_t dev); 6575988358SNate Lawson static u_int acpi_timer_get_timecount(struct timecounter *tc); 6675988358SNate Lawson static u_int acpi_timer_get_timecount_safe(struct timecounter *tc); 67787fa5b8SMike Smith static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS); 68d9b6df60SNate Lawson static void acpi_timer_boot_test(void); 6915e32d5dSMike Smith 70d9b6df60SNate Lawson static u_int acpi_timer_read(void); 71d9b6df60SNate Lawson static int acpi_timer_test(void); 72214475d8SMarcel Moolenaar 7315e32d5dSMike Smith static device_method_t acpi_timer_methods[] = { 7415e32d5dSMike Smith DEVMETHOD(device_identify, acpi_timer_identify), 7515e32d5dSMike Smith DEVMETHOD(device_probe, acpi_timer_probe), 7615e32d5dSMike Smith DEVMETHOD(device_attach, acpi_timer_attach), 7715e32d5dSMike Smith 7815e32d5dSMike Smith {0, 0} 7915e32d5dSMike Smith }; 8015e32d5dSMike Smith 8115e32d5dSMike Smith static driver_t acpi_timer_driver = { 8215e32d5dSMike Smith "acpi_timer", 8315e32d5dSMike Smith acpi_timer_methods, 84787fa5b8SMike Smith 0, 8515e32d5dSMike Smith }; 8615e32d5dSMike Smith 873273b005SMike Smith static devclass_t acpi_timer_devclass; 8815e32d5dSMike Smith DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0); 8964278df5SNate Lawson MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1); 9015e32d5dSMike Smith 91787fa5b8SMike Smith static struct timecounter acpi_timer_timecounter = { 92d9b6df60SNate Lawson acpi_timer_get_timecount_safe, /* get_timecount function */ 93d9b6df60SNate Lawson 0, /* no poll_pps */ 94d9b6df60SNate Lawson 0, /* no default counter_mask */ 95d9b6df60SNate Lawson 0, /* no default frequency */ 96d9b6df60SNate Lawson "ACPI", /* name */ 97d9b6df60SNate Lawson 1000 /* quality */ 98787fa5b8SMike Smith }; 99787fa5b8SMike Smith 10075988358SNate Lawson static u_int 101d9b6df60SNate Lawson acpi_timer_read() 102214475d8SMarcel Moolenaar { 103c0b9a6deSNate Lawson return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0)); 104214475d8SMarcel Moolenaar } 105cb877d00SPoul-Henning Kamp 106787fa5b8SMike Smith /* 107787fa5b8SMike Smith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources 108787fa5b8SMike Smith * we will be using. 109787fa5b8SMike Smith */ 11015e32d5dSMike Smith static void 11115e32d5dSMike Smith acpi_timer_identify(driver_t *driver, device_t parent) 11215e32d5dSMike Smith { 11315e32d5dSMike Smith device_t dev; 114214475d8SMarcel Moolenaar u_long rlen, rstart; 115be1841b4SNate Lawson int rid, rtype; 11615e32d5dSMike Smith 117b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1180ae55423SMike Smith 1194f8c4e4dSNate Lawson if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) || 1204f8c4e4dSNate Lawson AcpiGbl_FADT == NULL || acpi_timer_dev) 1210ae55423SMike Smith return_VOID; 12215e32d5dSMike Smith 12315e32d5dSMike Smith if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) { 12415e32d5dSMike Smith device_printf(parent, "could not add acpi_timer0\n"); 1250ae55423SMike Smith return_VOID; 12615e32d5dSMike Smith } 127787fa5b8SMike Smith acpi_timer_dev = dev; 128214475d8SMarcel Moolenaar 129787fa5b8SMike Smith rid = 0; 130be1841b4SNate Lawson rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ? 131be1841b4SNate Lawson SYS_RES_IOPORT : SYS_RES_MEMORY; 132214475d8SMarcel Moolenaar rlen = AcpiGbl_FADT->PmTmLen; 133214475d8SMarcel Moolenaar rstart = AcpiGbl_FADT->XPmTmrBlk.Address; 134be1841b4SNate Lawson if (bus_set_resource(dev, rtype, rid, rstart, rlen)) 135be1841b4SNate Lawson device_printf(dev, "couldn't set resource (%s 0x%lx+0x%lx)\n", 136be1841b4SNate Lawson (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen); 1370ae55423SMike Smith return_VOID; 13815e32d5dSMike Smith } 139be1841b4SNate Lawson 140be1841b4SNate Lawson static int 141be1841b4SNate Lawson acpi_timer_probe(device_t dev) 142be1841b4SNate Lawson { 143be1841b4SNate Lawson char desc[40]; 144be1841b4SNate Lawson int i, j, rid, rtype; 145be1841b4SNate Lawson 146be1841b4SNate Lawson ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 147be1841b4SNate Lawson 148be1841b4SNate Lawson if (dev != acpi_timer_dev) 149be1841b4SNate Lawson return (ENXIO); 150be1841b4SNate Lawson 151be1841b4SNate Lawson rid = 0; 152be1841b4SNate Lawson rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ? 153be1841b4SNate Lawson SYS_RES_IOPORT : SYS_RES_MEMORY; 154be1841b4SNate Lawson acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 155be1841b4SNate Lawson if (acpi_timer_reg == NULL) { 156be1841b4SNate Lawson device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n", 157be1841b4SNate Lawson (rtype == SYS_RES_IOPORT) ? "port" : "mem", 158be1841b4SNate Lawson (u_long)AcpiGbl_FADT->XPmTmrBlk.Address); 159be1841b4SNate Lawson return (ENXIO); 160be1841b4SNate Lawson } 16114827d7eSNate Lawson acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg); 16214827d7eSNate Lawson acpi_timer_bst = rman_get_bustag(acpi_timer_reg); 16375988358SNate Lawson if (AcpiGbl_FADT->TmrValExt != 0) 16475988358SNate Lawson acpi_timer_timecounter.tc_counter_mask = 0xffffffff; 16575988358SNate Lawson else 16675988358SNate Lawson acpi_timer_timecounter.tc_counter_mask = 0x00ffffff; 16775988358SNate Lawson acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 168d786139cSMaxime Henrion if (testenv("debug.acpi.timer_test")) 169d9b6df60SNate Lawson acpi_timer_boot_test(); 170787fa5b8SMike Smith 171af807c0fSNate Lawson /* 172af807c0fSNate Lawson * If all tests of the counter succeed, use the ACPI-fast method. If 173af807c0fSNate Lawson * at least one failed, default to using the safe routine, which reads 174af807c0fSNate Lawson * the timer multiple times to get a consistent value before returning. 175af807c0fSNate Lawson */ 176cb877d00SPoul-Henning Kamp j = 0; 177cb877d00SPoul-Henning Kamp for (i = 0; i < 10; i++) 178d9b6df60SNate Lawson j += acpi_timer_test(); 179cb877d00SPoul-Henning Kamp if (j == 10) { 180cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_name = "ACPI-fast"; 181cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount; 182cb877d00SPoul-Henning Kamp } else { 183cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_name = "ACPI-safe"; 184cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe; 185cb877d00SPoul-Henning Kamp } 186787fa5b8SMike Smith tc_init(&acpi_timer_timecounter); 18715e32d5dSMike Smith 188be2b1797SNate Lawson sprintf(desc, "%d-bit timer at 3.579545MHz", 189be2b1797SNate Lawson AcpiGbl_FADT->TmrValExt ? 32 : 24); 19015e32d5dSMike Smith device_set_desc_copy(dev, desc); 1910ae55423SMike Smith 192be1841b4SNate Lawson /* Release the resource, we'll allocate it again during attach. */ 193be1841b4SNate Lawson bus_release_resource(dev, rtype, rid, acpi_timer_reg); 19415e32d5dSMike Smith return (0); 19515e32d5dSMike Smith } 19615e32d5dSMike Smith 19715e32d5dSMike Smith static int 19815e32d5dSMike Smith acpi_timer_attach(device_t dev) 19915e32d5dSMike Smith { 200be1841b4SNate Lawson int rid, rtype; 201be1841b4SNate Lawson 202be1841b4SNate Lawson ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 203be1841b4SNate Lawson 204be1841b4SNate Lawson rid = 0; 205be1841b4SNate Lawson rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ? 206be1841b4SNate Lawson SYS_RES_IOPORT : SYS_RES_MEMORY; 207be1841b4SNate Lawson acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 208be1841b4SNate Lawson if (acpi_timer_reg == NULL) 209be1841b4SNate Lawson return (ENXIO); 210be1841b4SNate Lawson acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg); 211be1841b4SNate Lawson acpi_timer_bst = rman_get_bustag(acpi_timer_reg); 212787fa5b8SMike Smith return (0); 21315e32d5dSMike Smith } 214787fa5b8SMike Smith 215787fa5b8SMike Smith /* 216feade919SMike Smith * Fetch current time value from reliable hardware. 217787fa5b8SMike Smith */ 21875988358SNate Lawson static u_int 219787fa5b8SMike Smith acpi_timer_get_timecount(struct timecounter *tc) 220787fa5b8SMike Smith { 221d9b6df60SNate Lawson return (acpi_timer_read()); 222787fa5b8SMike Smith } 223787fa5b8SMike Smith 224787fa5b8SMike Smith /* 225feade919SMike Smith * Fetch current time value from hardware that may not correctly 226af807c0fSNate Lawson * latch the counter. We need to read until we have three monotonic 227af807c0fSNate Lawson * samples and then use the middle one, otherwise we are not protected 228af807c0fSNate Lawson * against the fact that the bits can be wrong in two directions. If 229af807c0fSNate Lawson * we only cared about monosity, two reads would be enough. 230feade919SMike Smith */ 23175988358SNate Lawson static u_int 232feade919SMike Smith acpi_timer_get_timecount_safe(struct timecounter *tc) 233feade919SMike Smith { 23475988358SNate Lawson u_int u1, u2, u3; 235feade919SMike Smith 236d9b6df60SNate Lawson u2 = acpi_timer_read(); 237d9b6df60SNate Lawson u3 = acpi_timer_read(); 238feade919SMike Smith do { 239feade919SMike Smith u1 = u2; 240feade919SMike Smith u2 = u3; 241d9b6df60SNate Lawson u3 = acpi_timer_read(); 242d9b6df60SNate Lawson } while (u1 > u2 || u2 > u3); 243be2b1797SNate Lawson 244feade919SMike Smith return (u2); 245feade919SMike Smith } 246feade919SMike Smith 247feade919SMike Smith /* 248787fa5b8SMike Smith * Timecounter freqency adjustment interface. 249787fa5b8SMike Smith */ 250787fa5b8SMike Smith static int 251787fa5b8SMike Smith acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS) 252787fa5b8SMike Smith { 253787fa5b8SMike Smith int error; 254787fa5b8SMike Smith u_int freq; 255787fa5b8SMike Smith 256787fa5b8SMike Smith if (acpi_timer_timecounter.tc_frequency == 0) 257787fa5b8SMike Smith return (EOPNOTSUPP); 258787fa5b8SMike Smith freq = acpi_timer_frequency; 259787fa5b8SMike Smith error = sysctl_handle_int(oidp, &freq, sizeof(freq), req); 260787fa5b8SMike Smith if (error == 0 && req->newptr != NULL) { 261787fa5b8SMike Smith acpi_timer_frequency = freq; 262787fa5b8SMike Smith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 263787fa5b8SMike Smith } 264be2b1797SNate Lawson 265787fa5b8SMike Smith return (error); 266787fa5b8SMike Smith } 267787fa5b8SMike Smith 268787fa5b8SMike Smith SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW, 269787fa5b8SMike Smith 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", ""); 270787fa5b8SMike Smith 271787fa5b8SMike Smith /* 272af807c0fSNate Lawson * Some ACPI timers are known or believed to suffer from implementation 273af807c0fSNate Lawson * problems which can lead to erroneous values being read. This function 274af807c0fSNate Lawson * tests for consistent results from the timer and returns 1 if it believes 275af807c0fSNate Lawson * the timer is consistent, otherwise it returns 0. 276af807c0fSNate Lawson * 277af807c0fSNate Lawson * It appears the cause is that the counter is not latched to the PCI bus 278af807c0fSNate Lawson * clock when read: 279af807c0fSNate Lawson * 280af807c0fSNate Lawson * ] 20. ACPI Timer Errata 281af807c0fSNate Lawson * ] 282af807c0fSNate Lawson * ] Problem: The power management timer may return improper result when 283af807c0fSNate Lawson * ] read. Although the timer value settles properly after incrementing, 284af807c0fSNate Lawson * ] while incrementing there is a 3nS window every 69.8nS where the 285af807c0fSNate Lawson * ] timer value is indeterminate (a 4.2% chance that the data will be 286af807c0fSNate Lawson * ] incorrect when read). As a result, the ACPI free running count up 287af807c0fSNate Lawson * ] timer specification is violated due to erroneous reads. Implication: 288af807c0fSNate Lawson * ] System hangs due to the "inaccuracy" of the timer when used by 289af807c0fSNate Lawson * ] software for time critical events and delays. 290af807c0fSNate Lawson * ] 291af807c0fSNate Lawson * ] Workaround: Read the register twice and compare. 292af807c0fSNate Lawson * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed 293af807c0fSNate Lawson * ] in the PIIX4M. 294af807c0fSNate Lawson */ 295af807c0fSNate Lawson #define N 2000 296af807c0fSNate Lawson static int 297d9b6df60SNate Lawson acpi_timer_test() 298af807c0fSNate Lawson { 299af807c0fSNate Lawson uint32_t last, this; 300af807c0fSNate Lawson int min, max, n, delta; 301c0b9a6deSNate Lawson register_t s; 302af807c0fSNate Lawson 303af807c0fSNate Lawson min = 10000000; 304af807c0fSNate Lawson max = 0; 305c0b9a6deSNate Lawson 306c0b9a6deSNate Lawson /* Test the timer with interrupts disabled to get accurate results. */ 307c0b9a6deSNate Lawson s = intr_disable(); 308d9b6df60SNate Lawson last = acpi_timer_read(); 309af807c0fSNate Lawson for (n = 0; n < N; n++) { 310d9b6df60SNate Lawson this = acpi_timer_read(); 311af807c0fSNate Lawson delta = acpi_TimerDelta(this, last); 312af807c0fSNate Lawson if (delta > max) 313af807c0fSNate Lawson max = delta; 314af807c0fSNate Lawson else if (delta < min) 315af807c0fSNate Lawson min = delta; 316af807c0fSNate Lawson last = this; 317af807c0fSNate Lawson } 318c0b9a6deSNate Lawson intr_restore(s); 319c0b9a6deSNate Lawson 320af807c0fSNate Lawson if (max - min > 2) 321af807c0fSNate Lawson n = 0; 322af807c0fSNate Lawson else if (min < 0 || max == 0) 323af807c0fSNate Lawson n = 0; 324af807c0fSNate Lawson else 325af807c0fSNate Lawson n = 1; 326af807c0fSNate Lawson if (bootverbose) { 327af807c0fSNate Lawson printf("ACPI timer looks %s min = %d, max = %d, width = %d\n", 328af807c0fSNate Lawson n ? "GOOD" : "BAD ", 329af807c0fSNate Lawson min, max, max - min); 330af807c0fSNate Lawson } 331af807c0fSNate Lawson 332af807c0fSNate Lawson return (n); 333af807c0fSNate Lawson } 334af807c0fSNate Lawson #undef N 335af807c0fSNate Lawson 336af807c0fSNate Lawson /* 337787fa5b8SMike Smith * Test harness for verifying ACPI timer behaviour. 338787fa5b8SMike Smith * Boot with debug.acpi.timer_test set to invoke this. 339787fa5b8SMike Smith */ 340787fa5b8SMike Smith static void 341d9b6df60SNate Lawson acpi_timer_boot_test(void) 342787fa5b8SMike Smith { 34375988358SNate Lawson uint32_t u1, u2, u3; 344787fa5b8SMike Smith 345d9b6df60SNate Lawson u1 = acpi_timer_read(); 346d9b6df60SNate Lawson u2 = acpi_timer_read(); 347d9b6df60SNate Lawson u3 = acpi_timer_read(); 348787fa5b8SMike Smith 349787fa5b8SMike Smith device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n"); 350787fa5b8SMike Smith for (;;) { 351787fa5b8SMike Smith /* 352be2b1797SNate Lawson * The failure case is where u3 > u1, but u2 does not fall between 353be2b1797SNate Lawson * the two, ie. it contains garbage. 354787fa5b8SMike Smith */ 355787fa5b8SMike Smith if (u3 > u1) { 356be2b1797SNate Lawson if (u2 < u1 || u2 > u3) 357be2b1797SNate Lawson device_printf(acpi_timer_dev, 358be2b1797SNate Lawson "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n", 359787fa5b8SMike Smith u1, u2, u3); 360787fa5b8SMike Smith } 361787fa5b8SMike Smith u1 = u2; 362787fa5b8SMike Smith u2 = u3; 363d9b6df60SNate Lawson u3 = acpi_timer_read(); 364787fa5b8SMike Smith } 365787fa5b8SMike Smith } 366