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> 33787fa5b8SMike Smith #include <sys/sysctl.h> 34787fa5b8SMike Smith #include <sys/timetc.h> 35787fa5b8SMike Smith 36787fa5b8SMike Smith #include <machine/bus_pio.h> 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" 4215e32d5dSMike Smith 43787fa5b8SMike Smith #include <acpica/acpivar.h> 44787fa5b8SMike Smith #include <pci/pcivar.h> 45787fa5b8SMike Smith 46787fa5b8SMike Smith /* 47787fa5b8SMike Smith * A timecounter based on the free-running ACPI timer. 48787fa5b8SMike Smith * 49787fa5b8SMike Smith * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>. 50787fa5b8SMike Smith */ 5115e32d5dSMike Smith 520ae55423SMike Smith /* 530ae55423SMike Smith * Hooks for the ACPI CA debugging infrastructure 540ae55423SMike Smith */ 552a4ac806SMike Smith #define _COMPONENT ACPI_SYSTEM 5672e5754cSMike Smith ACPI_MODULE_NAME("TIMER") 570ae55423SMike Smith 58787fa5b8SMike Smith static device_t acpi_timer_dev; 59787fa5b8SMike Smith struct resource *acpi_timer_reg; 60787fa5b8SMike Smith #define TIMER_READ bus_space_read_4(rman_get_bustag(acpi_timer_reg), \ 61787fa5b8SMike Smith rman_get_bushandle(acpi_timer_reg), \ 62787fa5b8SMike Smith 0) 6315e32d5dSMike Smith 64787fa5b8SMike Smith static u_int acpi_timer_frequency = 14318182/4; 6515e32d5dSMike Smith 6615e32d5dSMike Smith static void acpi_timer_identify(driver_t *driver, device_t parent); 6715e32d5dSMike Smith static int acpi_timer_probe(device_t dev); 6815e32d5dSMike Smith static int acpi_timer_attach(device_t dev); 69787fa5b8SMike Smith static unsigned acpi_timer_get_timecount(struct timecounter *tc); 70feade919SMike Smith static unsigned acpi_timer_get_timecount_safe(struct timecounter *tc); 71787fa5b8SMike Smith static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS); 72787fa5b8SMike Smith static void acpi_timer_test(void); 7315e32d5dSMike Smith 74787fa5b8SMike Smith /* 75787fa5b8SMike Smith * Driver hung off ACPI. 76787fa5b8SMike Smith */ 7715e32d5dSMike Smith static device_method_t acpi_timer_methods[] = { 7815e32d5dSMike Smith DEVMETHOD(device_identify, acpi_timer_identify), 7915e32d5dSMike Smith DEVMETHOD(device_probe, acpi_timer_probe), 8015e32d5dSMike Smith DEVMETHOD(device_attach, acpi_timer_attach), 8115e32d5dSMike Smith 8215e32d5dSMike Smith {0, 0} 8315e32d5dSMike Smith }; 8415e32d5dSMike Smith 8515e32d5dSMike Smith static driver_t acpi_timer_driver = { 8615e32d5dSMike Smith "acpi_timer", 8715e32d5dSMike Smith acpi_timer_methods, 88787fa5b8SMike Smith 0, 8915e32d5dSMike Smith }; 9015e32d5dSMike Smith 913273b005SMike Smith static devclass_t acpi_timer_devclass; 9215e32d5dSMike Smith DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0); 9315e32d5dSMike Smith 94787fa5b8SMike Smith /* 95787fa5b8SMike Smith * Timecounter. 96787fa5b8SMike Smith */ 97787fa5b8SMike Smith static struct timecounter acpi_timer_timecounter = { 98feade919SMike Smith acpi_timer_get_timecount_safe, 99787fa5b8SMike Smith 0, 100787fa5b8SMike Smith 0xffffff, 101787fa5b8SMike Smith 0, 102787fa5b8SMike Smith "ACPI" 103787fa5b8SMike Smith }; 104787fa5b8SMike Smith 105787fa5b8SMike Smith SYSCTL_OPAQUE(_debug, OID_AUTO, acpi_timecounter, CTLFLAG_RD, 106787fa5b8SMike Smith &acpi_timer_timecounter, sizeof(acpi_timer_timecounter), "S,timecounter", ""); 107cb877d00SPoul-Henning Kamp static int test_counter(void); 108cb877d00SPoul-Henning Kamp 109cb877d00SPoul-Henning Kamp #define N 2000 110cb877d00SPoul-Henning Kamp static int 111cb877d00SPoul-Henning Kamp test_counter() 112cb877d00SPoul-Henning Kamp { 113cb877d00SPoul-Henning Kamp int min, max, n, delta; 114cb877d00SPoul-Henning Kamp unsigned last, this; 115cb877d00SPoul-Henning Kamp 116cb877d00SPoul-Henning Kamp min = 10000000; 117cb877d00SPoul-Henning Kamp max = 0; 118cb877d00SPoul-Henning Kamp last = TIMER_READ; 119cb877d00SPoul-Henning Kamp for (n = 0; n < N; n++) { 120cb877d00SPoul-Henning Kamp this = TIMER_READ; 121cb877d00SPoul-Henning Kamp delta = (this - last) & 0xffffff; 122cb877d00SPoul-Henning Kamp if (delta > max) 123cb877d00SPoul-Henning Kamp max = delta; 124cb877d00SPoul-Henning Kamp else if (delta < min) 125cb877d00SPoul-Henning Kamp min = delta; 126cb877d00SPoul-Henning Kamp last = this; 127cb877d00SPoul-Henning Kamp } 128cb877d00SPoul-Henning Kamp if (max - min > 2) 129cb877d00SPoul-Henning Kamp n = 0; 130cb877d00SPoul-Henning Kamp else if (min < 0) 131cb877d00SPoul-Henning Kamp n = 0; 132cb877d00SPoul-Henning Kamp else 133cb877d00SPoul-Henning Kamp n = 1; 134116caf7cSPoul-Henning Kamp if (bootverbose) 135cb877d00SPoul-Henning Kamp printf("ACPI timer looks %s min = %d, max = %d, width = %d\n", 136cb877d00SPoul-Henning Kamp n ? "GOOD" : "BAD ", 137cb877d00SPoul-Henning Kamp min, max, max - min + 1); 138cb877d00SPoul-Henning Kamp return (n); 139cb877d00SPoul-Henning Kamp } 140787fa5b8SMike Smith 141787fa5b8SMike Smith /* 142787fa5b8SMike Smith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources 143787fa5b8SMike Smith * we will be using. 144787fa5b8SMike Smith */ 14515e32d5dSMike Smith static void 14615e32d5dSMike Smith acpi_timer_identify(driver_t *driver, device_t parent) 14715e32d5dSMike Smith { 14815e32d5dSMike Smith device_t dev; 14915e32d5dSMike Smith char desc[40]; 150cb877d00SPoul-Henning Kamp int rid, i, j; 15115e32d5dSMike Smith 15272e5754cSMike Smith ACPI_FUNCTION_TRACE(__func__); 1530ae55423SMike Smith 1540ae55423SMike Smith if (acpi_disabled("timer")) 1550ae55423SMike Smith return_VOID; 1560ae55423SMike Smith 15791467fc6SMike Smith if (AcpiGbl_FADT == NULL) 1580ae55423SMike Smith return_VOID; 15915e32d5dSMike Smith 16015e32d5dSMike Smith if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) { 16115e32d5dSMike Smith device_printf(parent, "could not add acpi_timer0\n"); 1620ae55423SMike Smith return_VOID; 16315e32d5dSMike Smith } 164787fa5b8SMike Smith acpi_timer_dev = dev; 165787fa5b8SMike Smith rid = 0; 166787fa5b8SMike Smith bus_set_resource(dev, SYS_RES_IOPORT, rid, AcpiGbl_FADT->V1_PmTmrBlk, sizeof(u_int32_t)); 167787fa5b8SMike Smith if ((acpi_timer_reg = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE)) == NULL) { 168787fa5b8SMike Smith device_printf(dev, "couldn't allocate I/O resource (port 0x%x)\n", AcpiGbl_FADT->V1_PmTmrBlk); 1690ae55423SMike Smith return_VOID; 17015e32d5dSMike Smith } 171d786139cSMaxime Henrion if (testenv("debug.acpi.timer_test")) 172787fa5b8SMike Smith acpi_timer_test(); 173787fa5b8SMike Smith 174787fa5b8SMike Smith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 175cb877d00SPoul-Henning Kamp j = 0; 176cb877d00SPoul-Henning Kamp for(i = 0; i < 10; i++) 177cb877d00SPoul-Henning Kamp j += test_counter(); 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 18791467fc6SMike Smith sprintf(desc, "%d-bit timer at 3.579545MHz", AcpiGbl_FADT->TmrValExt ? 32 : 24); 18815e32d5dSMike Smith device_set_desc_copy(dev, desc); 1890ae55423SMike Smith 1900ae55423SMike Smith return_VOID; 19115e32d5dSMike Smith } 19215e32d5dSMike Smith 19315e32d5dSMike Smith static int 19415e32d5dSMike Smith acpi_timer_probe(device_t dev) 19515e32d5dSMike Smith { 196787fa5b8SMike Smith if (dev == acpi_timer_dev) 19715e32d5dSMike Smith return(0); 19815e32d5dSMike Smith return(ENXIO); 19915e32d5dSMike Smith } 20015e32d5dSMike Smith 20115e32d5dSMike Smith static int 20215e32d5dSMike Smith acpi_timer_attach(device_t dev) 20315e32d5dSMike Smith { 204787fa5b8SMike Smith return(0); 20515e32d5dSMike Smith } 206787fa5b8SMike Smith 207787fa5b8SMike Smith /* 208feade919SMike Smith * Fetch current time value from reliable hardware. 209787fa5b8SMike Smith */ 210787fa5b8SMike Smith static unsigned 211787fa5b8SMike Smith acpi_timer_get_timecount(struct timecounter *tc) 212787fa5b8SMike Smith { 213787fa5b8SMike Smith return(TIMER_READ); 214787fa5b8SMike Smith } 215787fa5b8SMike Smith 216787fa5b8SMike Smith /* 217feade919SMike Smith * Fetch current time value from hardware that may not correctly 218feade919SMike Smith * latch the counter. 219feade919SMike Smith */ 220feade919SMike Smith static unsigned 221feade919SMike Smith acpi_timer_get_timecount_safe(struct timecounter *tc) 222feade919SMike Smith { 223feade919SMike Smith unsigned u1, u2, u3; 224feade919SMike Smith 225feade919SMike Smith u2 = TIMER_READ; 226feade919SMike Smith u3 = TIMER_READ; 227feade919SMike Smith do { 228feade919SMike Smith u1 = u2; 229feade919SMike Smith u2 = u3; 230feade919SMike Smith u3 = TIMER_READ; 231cb877d00SPoul-Henning Kamp } while (u1 > u2 || u2 > u3 || (u3 - u1) > 15); 232feade919SMike Smith return (u2); 233feade919SMike Smith } 234feade919SMike Smith 235feade919SMike Smith /* 236787fa5b8SMike Smith * Timecounter freqency adjustment interface. 237787fa5b8SMike Smith */ 238787fa5b8SMike Smith static int 239787fa5b8SMike Smith acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS) 240787fa5b8SMike Smith { 241787fa5b8SMike Smith int error; 242787fa5b8SMike Smith u_int freq; 243787fa5b8SMike Smith 244787fa5b8SMike Smith if (acpi_timer_timecounter.tc_frequency == 0) 245787fa5b8SMike Smith return (EOPNOTSUPP); 246787fa5b8SMike Smith freq = acpi_timer_frequency; 247787fa5b8SMike Smith error = sysctl_handle_int(oidp, &freq, sizeof(freq), req); 248787fa5b8SMike Smith if (error == 0 && req->newptr != NULL) { 249787fa5b8SMike Smith acpi_timer_frequency = freq; 250787fa5b8SMike Smith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 251787fa5b8SMike Smith tc_update(&acpi_timer_timecounter); 252787fa5b8SMike Smith } 253787fa5b8SMike Smith return (error); 254787fa5b8SMike Smith } 255787fa5b8SMike Smith 256787fa5b8SMike Smith SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW, 257787fa5b8SMike Smith 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", ""); 258787fa5b8SMike Smith 259787fa5b8SMike Smith /* 260787fa5b8SMike Smith * Test harness for verifying ACPI timer behaviour. 261787fa5b8SMike Smith * Boot with debug.acpi.timer_test set to invoke this. 262787fa5b8SMike Smith */ 263787fa5b8SMike Smith static void 264787fa5b8SMike Smith acpi_timer_test(void) 265787fa5b8SMike Smith { 266787fa5b8SMike Smith u_int32_t u1, u2, u3; 267787fa5b8SMike Smith 268787fa5b8SMike Smith u1 = TIMER_READ; 269787fa5b8SMike Smith u2 = TIMER_READ; 270787fa5b8SMike Smith u3 = TIMER_READ; 271787fa5b8SMike Smith 272787fa5b8SMike Smith device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n"); 273787fa5b8SMike Smith for (;;) { 274787fa5b8SMike Smith /* 275787fa5b8SMike Smith * The failure case is where u3 > u1, but u2 does not fall between the two, 276787fa5b8SMike Smith * ie. it contains garbage. 277787fa5b8SMike Smith */ 278787fa5b8SMike Smith if (u3 > u1) { 279787fa5b8SMike Smith if ((u2 < u1) || (u2 > u3)) 280787fa5b8SMike Smith device_printf(acpi_timer_dev, "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n", 281787fa5b8SMike Smith u1, u2, u3); 282787fa5b8SMike Smith } 283787fa5b8SMike Smith u1 = u2; 284787fa5b8SMike Smith u2 = u3; 285787fa5b8SMike Smith u3 = TIMER_READ; 286787fa5b8SMike Smith } 287787fa5b8SMike Smith } 288787fa5b8SMike Smith 289d8a9fe36SMike Smith /* 290d8a9fe36SMike Smith * Chipset workaround driver hung off PCI. 291d8a9fe36SMike Smith * 292feade919SMike Smith * Some ACPI timers are known or believed to suffer from implementation 293feade919SMike Smith * problems which can lead to erroneous values being read from the timer. 294feade919SMike Smith * 295feade919SMike Smith * Since we can't trust unknown chipsets, we default to a timer-read 296feade919SMike Smith * routine which compensates for the most common problem (as detailed 297feade919SMike Smith * in the excerpt from the Intel PIIX4 datasheet below). 298feade919SMike Smith * 299feade919SMike Smith * When we detect a known-functional chipset, we disable the workaround 300feade919SMike Smith * to improve speed. 301feade919SMike Smith * 302d8a9fe36SMike Smith * ] 20. ACPI Timer Errata 303d8a9fe36SMike Smith * ] 304d8a9fe36SMike Smith * ] Problem: The power management timer may return improper result when 305d8a9fe36SMike Smith * ] read. Although the timer value settles properly after incrementing, 306d8a9fe36SMike Smith * ] while incrementing there is a 3nS window every 69.8nS where the 307d8a9fe36SMike Smith * ] timer value is indeterminate (a 4.2% chance that the data will be 308d8a9fe36SMike Smith * ] incorrect when read). As a result, the ACPI free running count up 309d8a9fe36SMike Smith * ] timer specification is violated due to erroneous reads. Implication: 310d8a9fe36SMike Smith * ] System hangs due to the "inaccuracy" of the timer when used by 311d8a9fe36SMike Smith * ] software for time critical events and delays. 312d8a9fe36SMike Smith * ] 313d8a9fe36SMike Smith * ] Workaround: Read the register twice and compare. 314d8a9fe36SMike Smith * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed 315d8a9fe36SMike Smith * ] in the PIIX4M. 316d8a9fe36SMike Smith * 317d8a9fe36SMike Smith * The counter is in other words not latched to the PCI bus clock when 318d8a9fe36SMike Smith * read. Notice the workaround isn't: We need to read until we have 319d8a9fe36SMike Smith * three monotonic samples and then use the middle one, otherwise we are 320d8a9fe36SMike Smith * not protected against the fact that the bits can be wrong in two 321d8a9fe36SMike Smith * directions. If we only cared about monosity two reads would be enough. 322d8a9fe36SMike Smith */ 323d8a9fe36SMike Smith 324cb877d00SPoul-Henning Kamp #if 0 325d8a9fe36SMike Smith static int acpi_timer_pci_probe(device_t dev); 326d8a9fe36SMike Smith 327d8a9fe36SMike Smith static device_method_t acpi_timer_pci_methods[] = { 328d8a9fe36SMike Smith DEVMETHOD(device_probe, acpi_timer_pci_probe), 329d8a9fe36SMike Smith {0, 0} 330d8a9fe36SMike Smith }; 331d8a9fe36SMike Smith 332d8a9fe36SMike Smith static driver_t acpi_timer_pci_driver = { 333d8a9fe36SMike Smith "acpi_timer_pci", 334d8a9fe36SMike Smith acpi_timer_pci_methods, 335d8a9fe36SMike Smith 0, 336d8a9fe36SMike Smith }; 337d8a9fe36SMike Smith 338d8a9fe36SMike Smith devclass_t acpi_timer_pci_devclass; 339d8a9fe36SMike Smith DRIVER_MODULE(acpi_timer_pci, pci, acpi_timer_pci_driver, acpi_timer_pci_devclass, 0, 0); 340d8a9fe36SMike Smith 341d8a9fe36SMike Smith /* 342feade919SMike Smith * Look at PCI devices going past; if we detect one we know contains 343feade919SMike Smith * a functional ACPI timer device, enable the faster timecounter read 344feade919SMike Smith * routine. 345d8a9fe36SMike Smith */ 346d8a9fe36SMike Smith static int 347d8a9fe36SMike Smith acpi_timer_pci_probe(device_t dev) 348d8a9fe36SMike Smith { 349b2c98accSMike Smith int vendor, device, revid; 350b2c98accSMike Smith 351b2c98accSMike Smith vendor = pci_get_vendor(dev); 352b2c98accSMike Smith device = pci_get_device(dev); 353b2c98accSMike Smith revid = pci_get_revid(dev); 354b2c98accSMike Smith 355b2c98accSMike Smith if (((vendor == 0x8086) && (device == 0x7113) && (revid >= 0x03)) || /* PIIX4M */ 356b2c98accSMike Smith ((vendor == 0x8086) && (device == 0x719b)) || /* i440MX */ 357b2c98accSMike Smith 0) { 358b2c98accSMike Smith 359feade919SMike Smith acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount; 360b2c98accSMike Smith acpi_timer_timecounter.tc_name = "ACPI-fast"; 361feade919SMike Smith if (bootverbose) 362b2c98accSMike Smith device_printf(acpi_timer_dev, "functional ACPI timer detected, enabling fast timecount interface\n"); 363d8a9fe36SMike Smith } 364d8a9fe36SMike Smith 365d8a9fe36SMike Smith return(ENXIO); /* we never match anything */ 366d8a9fe36SMike Smith } 367cb877d00SPoul-Henning Kamp #endif 368