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 119be1841b4SNate Lawson if (acpi_disabled("timer") || AcpiGbl_FADT == NULL || acpi_timer_dev) 1200ae55423SMike Smith return_VOID; 12115e32d5dSMike Smith 12215e32d5dSMike Smith if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) { 12315e32d5dSMike Smith device_printf(parent, "could not add acpi_timer0\n"); 1240ae55423SMike Smith return_VOID; 12515e32d5dSMike Smith } 126787fa5b8SMike Smith acpi_timer_dev = dev; 127214475d8SMarcel Moolenaar 128787fa5b8SMike Smith rid = 0; 129be1841b4SNate Lawson rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ? 130be1841b4SNate Lawson SYS_RES_IOPORT : SYS_RES_MEMORY; 131214475d8SMarcel Moolenaar rlen = AcpiGbl_FADT->PmTmLen; 132214475d8SMarcel Moolenaar rstart = AcpiGbl_FADT->XPmTmrBlk.Address; 133be1841b4SNate Lawson if (bus_set_resource(dev, rtype, rid, rstart, rlen)) 134be1841b4SNate Lawson device_printf(dev, "couldn't set resource (%s 0x%lx+0x%lx)\n", 135be1841b4SNate Lawson (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen); 1360ae55423SMike Smith return_VOID; 13715e32d5dSMike Smith } 138be1841b4SNate Lawson 139be1841b4SNate Lawson static int 140be1841b4SNate Lawson acpi_timer_probe(device_t dev) 141be1841b4SNate Lawson { 142be1841b4SNate Lawson char desc[40]; 143be1841b4SNate Lawson int i, j, rid, rtype; 144be1841b4SNate Lawson 145be1841b4SNate Lawson ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 146be1841b4SNate Lawson 147be1841b4SNate Lawson if (dev != acpi_timer_dev) 148be1841b4SNate Lawson return (ENXIO); 149be1841b4SNate Lawson 150be1841b4SNate Lawson rid = 0; 151be1841b4SNate Lawson rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ? 152be1841b4SNate Lawson SYS_RES_IOPORT : SYS_RES_MEMORY; 153be1841b4SNate Lawson acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 154be1841b4SNate Lawson if (acpi_timer_reg == NULL) { 155be1841b4SNate Lawson device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n", 156be1841b4SNate Lawson (rtype == SYS_RES_IOPORT) ? "port" : "mem", 157be1841b4SNate Lawson (u_long)AcpiGbl_FADT->XPmTmrBlk.Address); 158be1841b4SNate Lawson return (ENXIO); 159be1841b4SNate Lawson } 16014827d7eSNate Lawson acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg); 16114827d7eSNate Lawson acpi_timer_bst = rman_get_bustag(acpi_timer_reg); 16275988358SNate Lawson if (AcpiGbl_FADT->TmrValExt != 0) 16375988358SNate Lawson acpi_timer_timecounter.tc_counter_mask = 0xffffffff; 16475988358SNate Lawson else 16575988358SNate Lawson acpi_timer_timecounter.tc_counter_mask = 0x00ffffff; 16675988358SNate Lawson acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 167d786139cSMaxime Henrion if (testenv("debug.acpi.timer_test")) 168d9b6df60SNate Lawson acpi_timer_boot_test(); 169787fa5b8SMike Smith 170af807c0fSNate Lawson /* 171af807c0fSNate Lawson * If all tests of the counter succeed, use the ACPI-fast method. If 172af807c0fSNate Lawson * at least one failed, default to using the safe routine, which reads 173af807c0fSNate Lawson * the timer multiple times to get a consistent value before returning. 174af807c0fSNate Lawson */ 175cb877d00SPoul-Henning Kamp j = 0; 176cb877d00SPoul-Henning Kamp for (i = 0; i < 10; i++) 177d9b6df60SNate Lawson j += acpi_timer_test(); 178cb877d00SPoul-Henning Kamp if (j == 10) { 179cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_name = "ACPI-fast"; 180cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount; 181cb877d00SPoul-Henning Kamp } else { 182cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_name = "ACPI-safe"; 183cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe; 184cb877d00SPoul-Henning Kamp } 185787fa5b8SMike Smith tc_init(&acpi_timer_timecounter); 18615e32d5dSMike Smith 187be2b1797SNate Lawson sprintf(desc, "%d-bit timer at 3.579545MHz", 188be2b1797SNate Lawson AcpiGbl_FADT->TmrValExt ? 32 : 24); 18915e32d5dSMike Smith device_set_desc_copy(dev, desc); 1900ae55423SMike Smith 191be1841b4SNate Lawson /* Release the resource, we'll allocate it again during attach. */ 192be1841b4SNate Lawson bus_release_resource(dev, rtype, rid, acpi_timer_reg); 19315e32d5dSMike Smith return (0); 19415e32d5dSMike Smith } 19515e32d5dSMike Smith 19615e32d5dSMike Smith static int 19715e32d5dSMike Smith acpi_timer_attach(device_t dev) 19815e32d5dSMike Smith { 199be1841b4SNate Lawson int rid, rtype; 200be1841b4SNate Lawson 201be1841b4SNate Lawson ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 202be1841b4SNate Lawson 203be1841b4SNate Lawson rid = 0; 204be1841b4SNate Lawson rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ? 205be1841b4SNate Lawson SYS_RES_IOPORT : SYS_RES_MEMORY; 206be1841b4SNate Lawson acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 207be1841b4SNate Lawson if (acpi_timer_reg == NULL) 208be1841b4SNate Lawson return (ENXIO); 209be1841b4SNate Lawson acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg); 210be1841b4SNate Lawson acpi_timer_bst = rman_get_bustag(acpi_timer_reg); 211787fa5b8SMike Smith return (0); 21215e32d5dSMike Smith } 213787fa5b8SMike Smith 214787fa5b8SMike Smith /* 215feade919SMike Smith * Fetch current time value from reliable hardware. 216787fa5b8SMike Smith */ 21775988358SNate Lawson static u_int 218787fa5b8SMike Smith acpi_timer_get_timecount(struct timecounter *tc) 219787fa5b8SMike Smith { 220d9b6df60SNate Lawson return (acpi_timer_read()); 221787fa5b8SMike Smith } 222787fa5b8SMike Smith 223787fa5b8SMike Smith /* 224feade919SMike Smith * Fetch current time value from hardware that may not correctly 225af807c0fSNate Lawson * latch the counter. We need to read until we have three monotonic 226af807c0fSNate Lawson * samples and then use the middle one, otherwise we are not protected 227af807c0fSNate Lawson * against the fact that the bits can be wrong in two directions. If 228af807c0fSNate Lawson * we only cared about monosity, two reads would be enough. 229feade919SMike Smith */ 23075988358SNate Lawson static u_int 231feade919SMike Smith acpi_timer_get_timecount_safe(struct timecounter *tc) 232feade919SMike Smith { 23375988358SNate Lawson u_int u1, u2, u3; 234feade919SMike Smith 235d9b6df60SNate Lawson u2 = acpi_timer_read(); 236d9b6df60SNate Lawson u3 = acpi_timer_read(); 237feade919SMike Smith do { 238feade919SMike Smith u1 = u2; 239feade919SMike Smith u2 = u3; 240d9b6df60SNate Lawson u3 = acpi_timer_read(); 241d9b6df60SNate Lawson } while (u1 > u2 || u2 > u3); 242be2b1797SNate Lawson 243feade919SMike Smith return (u2); 244feade919SMike Smith } 245feade919SMike Smith 246feade919SMike Smith /* 247787fa5b8SMike Smith * Timecounter freqency adjustment interface. 248787fa5b8SMike Smith */ 249787fa5b8SMike Smith static int 250787fa5b8SMike Smith acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS) 251787fa5b8SMike Smith { 252787fa5b8SMike Smith int error; 253787fa5b8SMike Smith u_int freq; 254787fa5b8SMike Smith 255787fa5b8SMike Smith if (acpi_timer_timecounter.tc_frequency == 0) 256787fa5b8SMike Smith return (EOPNOTSUPP); 257787fa5b8SMike Smith freq = acpi_timer_frequency; 258787fa5b8SMike Smith error = sysctl_handle_int(oidp, &freq, sizeof(freq), req); 259787fa5b8SMike Smith if (error == 0 && req->newptr != NULL) { 260787fa5b8SMike Smith acpi_timer_frequency = freq; 261787fa5b8SMike Smith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 262787fa5b8SMike Smith } 263be2b1797SNate Lawson 264787fa5b8SMike Smith return (error); 265787fa5b8SMike Smith } 266787fa5b8SMike Smith 267787fa5b8SMike Smith SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW, 268787fa5b8SMike Smith 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", ""); 269787fa5b8SMike Smith 270787fa5b8SMike Smith /* 271af807c0fSNate Lawson * Some ACPI timers are known or believed to suffer from implementation 272af807c0fSNate Lawson * problems which can lead to erroneous values being read. This function 273af807c0fSNate Lawson * tests for consistent results from the timer and returns 1 if it believes 274af807c0fSNate Lawson * the timer is consistent, otherwise it returns 0. 275af807c0fSNate Lawson * 276af807c0fSNate Lawson * It appears the cause is that the counter is not latched to the PCI bus 277af807c0fSNate Lawson * clock when read: 278af807c0fSNate Lawson * 279af807c0fSNate Lawson * ] 20. ACPI Timer Errata 280af807c0fSNate Lawson * ] 281af807c0fSNate Lawson * ] Problem: The power management timer may return improper result when 282af807c0fSNate Lawson * ] read. Although the timer value settles properly after incrementing, 283af807c0fSNate Lawson * ] while incrementing there is a 3nS window every 69.8nS where the 284af807c0fSNate Lawson * ] timer value is indeterminate (a 4.2% chance that the data will be 285af807c0fSNate Lawson * ] incorrect when read). As a result, the ACPI free running count up 286af807c0fSNate Lawson * ] timer specification is violated due to erroneous reads. Implication: 287af807c0fSNate Lawson * ] System hangs due to the "inaccuracy" of the timer when used by 288af807c0fSNate Lawson * ] software for time critical events and delays. 289af807c0fSNate Lawson * ] 290af807c0fSNate Lawson * ] Workaround: Read the register twice and compare. 291af807c0fSNate Lawson * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed 292af807c0fSNate Lawson * ] in the PIIX4M. 293af807c0fSNate Lawson */ 294af807c0fSNate Lawson #define N 2000 295af807c0fSNate Lawson static int 296d9b6df60SNate Lawson acpi_timer_test() 297af807c0fSNate Lawson { 298af807c0fSNate Lawson uint32_t last, this; 299af807c0fSNate Lawson int min, max, n, delta; 300c0b9a6deSNate Lawson register_t s; 301af807c0fSNate Lawson 302af807c0fSNate Lawson min = 10000000; 303af807c0fSNate Lawson max = 0; 304c0b9a6deSNate Lawson 305c0b9a6deSNate Lawson /* Test the timer with interrupts disabled to get accurate results. */ 306c0b9a6deSNate Lawson s = intr_disable(); 307d9b6df60SNate Lawson last = acpi_timer_read(); 308af807c0fSNate Lawson for (n = 0; n < N; n++) { 309d9b6df60SNate Lawson this = acpi_timer_read(); 310af807c0fSNate Lawson delta = acpi_TimerDelta(this, last); 311af807c0fSNate Lawson if (delta > max) 312af807c0fSNate Lawson max = delta; 313af807c0fSNate Lawson else if (delta < min) 314af807c0fSNate Lawson min = delta; 315af807c0fSNate Lawson last = this; 316af807c0fSNate Lawson } 317c0b9a6deSNate Lawson intr_restore(s); 318c0b9a6deSNate Lawson 319af807c0fSNate Lawson if (max - min > 2) 320af807c0fSNate Lawson n = 0; 321af807c0fSNate Lawson else if (min < 0 || max == 0) 322af807c0fSNate Lawson n = 0; 323af807c0fSNate Lawson else 324af807c0fSNate Lawson n = 1; 325af807c0fSNate Lawson if (bootverbose) { 326af807c0fSNate Lawson printf("ACPI timer looks %s min = %d, max = %d, width = %d\n", 327af807c0fSNate Lawson n ? "GOOD" : "BAD ", 328af807c0fSNate Lawson min, max, max - min); 329af807c0fSNate Lawson } 330af807c0fSNate Lawson 331af807c0fSNate Lawson return (n); 332af807c0fSNate Lawson } 333af807c0fSNate Lawson #undef N 334af807c0fSNate Lawson 335af807c0fSNate Lawson /* 336787fa5b8SMike Smith * Test harness for verifying ACPI timer behaviour. 337787fa5b8SMike Smith * Boot with debug.acpi.timer_test set to invoke this. 338787fa5b8SMike Smith */ 339787fa5b8SMike Smith static void 340d9b6df60SNate Lawson acpi_timer_boot_test(void) 341787fa5b8SMike Smith { 34275988358SNate Lawson uint32_t u1, u2, u3; 343787fa5b8SMike Smith 344d9b6df60SNate Lawson u1 = acpi_timer_read(); 345d9b6df60SNate Lawson u2 = acpi_timer_read(); 346d9b6df60SNate Lawson u3 = acpi_timer_read(); 347787fa5b8SMike Smith 348787fa5b8SMike Smith device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n"); 349787fa5b8SMike Smith for (;;) { 350787fa5b8SMike Smith /* 351be2b1797SNate Lawson * The failure case is where u3 > u1, but u2 does not fall between 352be2b1797SNate Lawson * the two, ie. it contains garbage. 353787fa5b8SMike Smith */ 354787fa5b8SMike Smith if (u3 > u1) { 355be2b1797SNate Lawson if (u2 < u1 || u2 > u3) 356be2b1797SNate Lawson device_printf(acpi_timer_dev, 357be2b1797SNate Lawson "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n", 358787fa5b8SMike Smith u1, u2, u3); 359787fa5b8SMike Smith } 360787fa5b8SMike Smith u1 = u2; 361787fa5b8SMike Smith u2 = u3; 362d9b6df60SNate Lawson u3 = acpi_timer_read(); 363787fa5b8SMike Smith } 364787fa5b8SMike Smith } 365