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 */ 27dad97feeSDavid E. O'Brien 28dad97feeSDavid E. O'Brien #include <sys/cdefs.h> 29dad97feeSDavid E. O'Brien __FBSDID("$FreeBSD$"); 30dad97feeSDavid E. O'Brien 3115e32d5dSMike Smith #include "opt_acpi.h" 3215e32d5dSMike Smith #include <sys/param.h> 3315e32d5dSMike Smith #include <sys/bus.h> 34787fa5b8SMike Smith #include <sys/kernel.h> 35fe12f24bSPoul-Henning Kamp #include <sys/module.h> 36787fa5b8SMike Smith #include <sys/sysctl.h> 37787fa5b8SMike Smith #include <sys/timetc.h> 38787fa5b8SMike Smith 39787fa5b8SMike Smith #include <machine/bus.h> 40787fa5b8SMike Smith #include <machine/resource.h> 41787fa5b8SMike Smith #include <sys/rman.h> 4215e32d5dSMike Smith 432a191126SDavid E. O'Brien #include <contrib/dev/acpica/acpi.h> 4487e5d361SJohn Baldwin #include <dev/acpica/acpivar.h> 45cace7a2aSWarner Losh #include <dev/pci/pcivar.h> 46787fa5b8SMike Smith 47787fa5b8SMike Smith /* 48787fa5b8SMike Smith * A timecounter based on the free-running ACPI timer. 49787fa5b8SMike Smith * 50787fa5b8SMike Smith * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>. 51787fa5b8SMike Smith */ 5215e32d5dSMike Smith 53be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 543184cf5aSNate Lawson #define _COMPONENT ACPI_TIMER 5572e5754cSMike Smith ACPI_MODULE_NAME("TIMER") 560ae55423SMike Smith 57787fa5b8SMike Smith static device_t acpi_timer_dev; 58af807c0fSNate Lawson static struct resource *acpi_timer_reg; 59d9b6df60SNate Lawson static bus_space_handle_t acpi_timer_bsh; 60d9b6df60SNate Lawson static bus_space_tag_t acpi_timer_bst; 6115e32d5dSMike Smith 62787fa5b8SMike Smith static u_int acpi_timer_frequency = 14318182 / 4; 6315e32d5dSMike Smith 6415e32d5dSMike Smith static void acpi_timer_identify(driver_t *driver, device_t parent); 6515e32d5dSMike Smith static int acpi_timer_probe(device_t dev); 6615e32d5dSMike Smith static int acpi_timer_attach(device_t dev); 6775988358SNate Lawson static u_int acpi_timer_get_timecount(struct timecounter *tc); 6875988358SNate Lawson static u_int acpi_timer_get_timecount_safe(struct timecounter *tc); 69787fa5b8SMike Smith static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS); 70d9b6df60SNate Lawson static void acpi_timer_boot_test(void); 7115e32d5dSMike Smith 72d9b6df60SNate Lawson static u_int acpi_timer_read(void); 73d9b6df60SNate Lawson static int acpi_timer_test(void); 74214475d8SMarcel Moolenaar 7515e32d5dSMike Smith static device_method_t acpi_timer_methods[] = { 7615e32d5dSMike Smith DEVMETHOD(device_identify, acpi_timer_identify), 7715e32d5dSMike Smith DEVMETHOD(device_probe, acpi_timer_probe), 7815e32d5dSMike Smith DEVMETHOD(device_attach, acpi_timer_attach), 7915e32d5dSMike Smith 8015e32d5dSMike Smith {0, 0} 8115e32d5dSMike Smith }; 8215e32d5dSMike Smith 8315e32d5dSMike Smith static driver_t acpi_timer_driver = { 8415e32d5dSMike Smith "acpi_timer", 8515e32d5dSMike Smith acpi_timer_methods, 86787fa5b8SMike Smith 0, 8715e32d5dSMike Smith }; 8815e32d5dSMike Smith 893273b005SMike Smith static devclass_t acpi_timer_devclass; 9015e32d5dSMike Smith DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0); 9164278df5SNate Lawson MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1); 9215e32d5dSMike Smith 93787fa5b8SMike Smith static struct timecounter acpi_timer_timecounter = { 94d9b6df60SNate Lawson acpi_timer_get_timecount_safe, /* get_timecount function */ 95d9b6df60SNate Lawson 0, /* no poll_pps */ 96d9b6df60SNate Lawson 0, /* no default counter_mask */ 97d9b6df60SNate Lawson 0, /* no default frequency */ 98d9b6df60SNate Lawson "ACPI", /* name */ 99d9b6df60SNate Lawson 1000 /* quality */ 100787fa5b8SMike Smith }; 101787fa5b8SMike Smith 10275988358SNate Lawson static u_int 103d9b6df60SNate Lawson acpi_timer_read() 104214475d8SMarcel Moolenaar { 105c0b9a6deSNate Lawson return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0)); 106214475d8SMarcel Moolenaar } 107cb877d00SPoul-Henning Kamp 108787fa5b8SMike Smith /* 109787fa5b8SMike Smith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources 110787fa5b8SMike Smith * we will be using. 111787fa5b8SMike Smith */ 11215e32d5dSMike Smith static void 11315e32d5dSMike Smith acpi_timer_identify(driver_t *driver, device_t parent) 11415e32d5dSMike Smith { 11515e32d5dSMike Smith device_t dev; 116214475d8SMarcel Moolenaar u_long rlen, rstart; 117be1841b4SNate Lawson int rid, rtype; 11815e32d5dSMike Smith 119b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1200ae55423SMike Smith 1214f8c4e4dSNate Lawson if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) || 1222be4e471SJung-uk Kim acpi_timer_dev) 1230ae55423SMike Smith return_VOID; 12415e32d5dSMike Smith 12515e32d5dSMike Smith if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) { 12615e32d5dSMike Smith device_printf(parent, "could not add acpi_timer0\n"); 1270ae55423SMike Smith return_VOID; 12815e32d5dSMike Smith } 129787fa5b8SMike Smith acpi_timer_dev = dev; 130214475d8SMarcel Moolenaar 131787fa5b8SMike Smith rid = 0; 1322be4e471SJung-uk Kim rtype = AcpiGbl_FADT.XPmTimerBlock.SpaceId ? 133be1841b4SNate Lawson SYS_RES_IOPORT : SYS_RES_MEMORY; 1342be4e471SJung-uk Kim rlen = AcpiGbl_FADT.PmTimerLength; 1352be4e471SJung-uk Kim rstart = AcpiGbl_FADT.XPmTimerBlock.Address; 136be1841b4SNate Lawson if (bus_set_resource(dev, rtype, rid, rstart, rlen)) 137be1841b4SNate Lawson device_printf(dev, "couldn't set resource (%s 0x%lx+0x%lx)\n", 138be1841b4SNate Lawson (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen); 1390ae55423SMike Smith return_VOID; 14015e32d5dSMike Smith } 141be1841b4SNate Lawson 142be1841b4SNate Lawson static int 143be1841b4SNate Lawson acpi_timer_probe(device_t dev) 144be1841b4SNate Lawson { 145be1841b4SNate Lawson char desc[40]; 146be1841b4SNate Lawson int i, j, rid, rtype; 147be1841b4SNate Lawson 148be1841b4SNate Lawson ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 149be1841b4SNate Lawson 150be1841b4SNate Lawson if (dev != acpi_timer_dev) 151be1841b4SNate Lawson return (ENXIO); 152be1841b4SNate Lawson 153be1841b4SNate Lawson rid = 0; 1542be4e471SJung-uk Kim rtype = AcpiGbl_FADT.XPmTimerBlock.SpaceId ? 155be1841b4SNate Lawson SYS_RES_IOPORT : SYS_RES_MEMORY; 156be1841b4SNate Lawson acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 157be1841b4SNate Lawson if (acpi_timer_reg == NULL) { 158be1841b4SNate Lawson device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n", 159be1841b4SNate Lawson (rtype == SYS_RES_IOPORT) ? "port" : "mem", 1602be4e471SJung-uk Kim (u_long)AcpiGbl_FADT.XPmTimerBlock.Address); 161be1841b4SNate Lawson return (ENXIO); 162be1841b4SNate Lawson } 16314827d7eSNate Lawson acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg); 16414827d7eSNate Lawson acpi_timer_bst = rman_get_bustag(acpi_timer_reg); 1652be4e471SJung-uk Kim if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) 16675988358SNate Lawson acpi_timer_timecounter.tc_counter_mask = 0xffffffff; 16775988358SNate Lawson else 16875988358SNate Lawson acpi_timer_timecounter.tc_counter_mask = 0x00ffffff; 16975988358SNate Lawson acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 170d786139cSMaxime Henrion if (testenv("debug.acpi.timer_test")) 171d9b6df60SNate Lawson acpi_timer_boot_test(); 172787fa5b8SMike Smith 173af807c0fSNate Lawson /* 174af807c0fSNate Lawson * If all tests of the counter succeed, use the ACPI-fast method. If 175af807c0fSNate Lawson * at least one failed, default to using the safe routine, which reads 176af807c0fSNate Lawson * the timer multiple times to get a consistent value before returning. 177af807c0fSNate Lawson */ 178cb877d00SPoul-Henning Kamp j = 0; 1792a921b05SPoul-Henning Kamp if (bootverbose) 1802a921b05SPoul-Henning Kamp printf("ACPI timer:"); 181cb877d00SPoul-Henning Kamp for (i = 0; i < 10; i++) 182d9b6df60SNate Lawson j += acpi_timer_test(); 1832a921b05SPoul-Henning Kamp if (bootverbose) 1842a921b05SPoul-Henning Kamp printf(" -> %d\n", j); 185cb877d00SPoul-Henning Kamp if (j == 10) { 186cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_name = "ACPI-fast"; 187cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount; 188cb877d00SPoul-Henning Kamp } else { 189cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_name = "ACPI-safe"; 190cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe; 191cb877d00SPoul-Henning Kamp } 192787fa5b8SMike Smith tc_init(&acpi_timer_timecounter); 19315e32d5dSMike Smith 194be2b1797SNate Lawson sprintf(desc, "%d-bit timer at 3.579545MHz", 1952be4e471SJung-uk Kim (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) ? 32 : 24); 19615e32d5dSMike Smith device_set_desc_copy(dev, desc); 1970ae55423SMike Smith 198be1841b4SNate Lawson /* Release the resource, we'll allocate it again during attach. */ 199be1841b4SNate Lawson bus_release_resource(dev, rtype, rid, acpi_timer_reg); 20015e32d5dSMike Smith return (0); 20115e32d5dSMike Smith } 20215e32d5dSMike Smith 20315e32d5dSMike Smith static int 20415e32d5dSMike Smith acpi_timer_attach(device_t dev) 20515e32d5dSMike Smith { 206be1841b4SNate Lawson int rid, rtype; 207be1841b4SNate Lawson 208be1841b4SNate Lawson ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 209be1841b4SNate Lawson 210be1841b4SNate Lawson rid = 0; 2112be4e471SJung-uk Kim rtype = AcpiGbl_FADT.XPmTimerBlock.SpaceId ? 212be1841b4SNate Lawson SYS_RES_IOPORT : SYS_RES_MEMORY; 213be1841b4SNate Lawson acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 214be1841b4SNate Lawson if (acpi_timer_reg == NULL) 215be1841b4SNate Lawson return (ENXIO); 216be1841b4SNate Lawson acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg); 217be1841b4SNate Lawson acpi_timer_bst = rman_get_bustag(acpi_timer_reg); 218787fa5b8SMike Smith return (0); 21915e32d5dSMike Smith } 220787fa5b8SMike Smith 221787fa5b8SMike Smith /* 222feade919SMike Smith * Fetch current time value from reliable hardware. 223787fa5b8SMike Smith */ 22475988358SNate Lawson static u_int 225787fa5b8SMike Smith acpi_timer_get_timecount(struct timecounter *tc) 226787fa5b8SMike Smith { 227d9b6df60SNate Lawson return (acpi_timer_read()); 228787fa5b8SMike Smith } 229787fa5b8SMike Smith 230787fa5b8SMike Smith /* 231feade919SMike Smith * Fetch current time value from hardware that may not correctly 232af807c0fSNate Lawson * latch the counter. We need to read until we have three monotonic 233af807c0fSNate Lawson * samples and then use the middle one, otherwise we are not protected 234af807c0fSNate Lawson * against the fact that the bits can be wrong in two directions. If 235af807c0fSNate Lawson * we only cared about monosity, two reads would be enough. 236feade919SMike Smith */ 23775988358SNate Lawson static u_int 238feade919SMike Smith acpi_timer_get_timecount_safe(struct timecounter *tc) 239feade919SMike Smith { 24075988358SNate Lawson u_int u1, u2, u3; 241feade919SMike Smith 242d9b6df60SNate Lawson u2 = acpi_timer_read(); 243d9b6df60SNate Lawson u3 = acpi_timer_read(); 244feade919SMike Smith do { 245feade919SMike Smith u1 = u2; 246feade919SMike Smith u2 = u3; 247d9b6df60SNate Lawson u3 = acpi_timer_read(); 248d9b6df60SNate Lawson } while (u1 > u2 || u2 > u3); 249be2b1797SNate Lawson 250feade919SMike Smith return (u2); 251feade919SMike Smith } 252feade919SMike Smith 253feade919SMike Smith /* 254787fa5b8SMike Smith * Timecounter freqency adjustment interface. 255787fa5b8SMike Smith */ 256787fa5b8SMike Smith static int 257787fa5b8SMike Smith acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS) 258787fa5b8SMike Smith { 259787fa5b8SMike Smith int error; 260787fa5b8SMike Smith u_int freq; 261787fa5b8SMike Smith 262787fa5b8SMike Smith if (acpi_timer_timecounter.tc_frequency == 0) 263787fa5b8SMike Smith return (EOPNOTSUPP); 264787fa5b8SMike Smith freq = acpi_timer_frequency; 265787fa5b8SMike Smith error = sysctl_handle_int(oidp, &freq, sizeof(freq), req); 266787fa5b8SMike Smith if (error == 0 && req->newptr != NULL) { 267787fa5b8SMike Smith acpi_timer_frequency = freq; 268787fa5b8SMike Smith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 269787fa5b8SMike Smith } 270be2b1797SNate Lawson 271787fa5b8SMike Smith return (error); 272787fa5b8SMike Smith } 273787fa5b8SMike Smith 274787fa5b8SMike Smith SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW, 275787fa5b8SMike Smith 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", ""); 276787fa5b8SMike Smith 277787fa5b8SMike Smith /* 278af807c0fSNate Lawson * Some ACPI timers are known or believed to suffer from implementation 279af807c0fSNate Lawson * problems which can lead to erroneous values being read. This function 280af807c0fSNate Lawson * tests for consistent results from the timer and returns 1 if it believes 281af807c0fSNate Lawson * the timer is consistent, otherwise it returns 0. 282af807c0fSNate Lawson * 283af807c0fSNate Lawson * It appears the cause is that the counter is not latched to the PCI bus 284af807c0fSNate Lawson * clock when read: 285af807c0fSNate Lawson * 286af807c0fSNate Lawson * ] 20. ACPI Timer Errata 287af807c0fSNate Lawson * ] 288af807c0fSNate Lawson * ] Problem: The power management timer may return improper result when 289af807c0fSNate Lawson * ] read. Although the timer value settles properly after incrementing, 290af807c0fSNate Lawson * ] while incrementing there is a 3nS window every 69.8nS where the 291af807c0fSNate Lawson * ] timer value is indeterminate (a 4.2% chance that the data will be 292af807c0fSNate Lawson * ] incorrect when read). As a result, the ACPI free running count up 293af807c0fSNate Lawson * ] timer specification is violated due to erroneous reads. Implication: 294af807c0fSNate Lawson * ] System hangs due to the "inaccuracy" of the timer when used by 295af807c0fSNate Lawson * ] software for time critical events and delays. 296af807c0fSNate Lawson * ] 297af807c0fSNate Lawson * ] Workaround: Read the register twice and compare. 298af807c0fSNate Lawson * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed 299af807c0fSNate Lawson * ] in the PIIX4M. 300af807c0fSNate Lawson */ 301af807c0fSNate Lawson #define N 2000 302af807c0fSNate Lawson static int 303d9b6df60SNate Lawson acpi_timer_test() 304af807c0fSNate Lawson { 305af807c0fSNate Lawson uint32_t last, this; 306af807c0fSNate Lawson int min, max, n, delta; 307c0b9a6deSNate Lawson register_t s; 308af807c0fSNate Lawson 309af807c0fSNate Lawson min = 10000000; 310af807c0fSNate Lawson max = 0; 311c0b9a6deSNate Lawson 312c0b9a6deSNate Lawson /* Test the timer with interrupts disabled to get accurate results. */ 313c0b9a6deSNate Lawson s = intr_disable(); 314d9b6df60SNate Lawson last = acpi_timer_read(); 315af807c0fSNate Lawson for (n = 0; n < N; n++) { 316d9b6df60SNate Lawson this = acpi_timer_read(); 317af807c0fSNate Lawson delta = acpi_TimerDelta(this, last); 318af807c0fSNate Lawson if (delta > max) 319af807c0fSNate Lawson max = delta; 320af807c0fSNate Lawson else if (delta < min) 321af807c0fSNate Lawson min = delta; 322af807c0fSNate Lawson last = this; 323af807c0fSNate Lawson } 324c0b9a6deSNate Lawson intr_restore(s); 325c0b9a6deSNate Lawson 326af807c0fSNate Lawson if (max - min > 2) 327af807c0fSNate Lawson n = 0; 328af807c0fSNate Lawson else if (min < 0 || max == 0) 329af807c0fSNate Lawson n = 0; 330af807c0fSNate Lawson else 331af807c0fSNate Lawson n = 1; 3322a921b05SPoul-Henning Kamp if (bootverbose) 3332a921b05SPoul-Henning Kamp printf(" %d/%d", n, max-min); 334af807c0fSNate Lawson 335af807c0fSNate Lawson return (n); 336af807c0fSNate Lawson } 337af807c0fSNate Lawson #undef N 338af807c0fSNate Lawson 339af807c0fSNate Lawson /* 340787fa5b8SMike Smith * Test harness for verifying ACPI timer behaviour. 341787fa5b8SMike Smith * Boot with debug.acpi.timer_test set to invoke this. 342787fa5b8SMike Smith */ 343787fa5b8SMike Smith static void 344d9b6df60SNate Lawson acpi_timer_boot_test(void) 345787fa5b8SMike Smith { 34675988358SNate Lawson uint32_t u1, u2, u3; 347787fa5b8SMike Smith 348d9b6df60SNate Lawson u1 = acpi_timer_read(); 349d9b6df60SNate Lawson u2 = acpi_timer_read(); 350d9b6df60SNate Lawson u3 = acpi_timer_read(); 351787fa5b8SMike Smith 352787fa5b8SMike Smith device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n"); 353787fa5b8SMike Smith for (;;) { 354787fa5b8SMike Smith /* 355be2b1797SNate Lawson * The failure case is where u3 > u1, but u2 does not fall between 356be2b1797SNate Lawson * the two, ie. it contains garbage. 357787fa5b8SMike Smith */ 358787fa5b8SMike Smith if (u3 > u1) { 359be2b1797SNate Lawson if (u2 < u1 || u2 > u3) 360be2b1797SNate Lawson device_printf(acpi_timer_dev, 361be2b1797SNate Lawson "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n", 362787fa5b8SMike Smith u1, u2, u3); 363787fa5b8SMike Smith } 364787fa5b8SMike Smith u1 = u2; 365787fa5b8SMike Smith u2 = u3; 366d9b6df60SNate Lawson u3 = acpi_timer_read(); 367787fa5b8SMike Smith } 368787fa5b8SMike Smith } 369