acpi_timer.c revision 119281
167761Smsmith/*- 280070Smsmith * Copyright (c) 2000, 2001 Michael Smith 367761Smsmith * Copyright (c) 2000 BSDi 467761Smsmith * All rights reserved. 567761Smsmith * 667761Smsmith * Redistribution and use in source and binary forms, with or without 767761Smsmith * modification, are permitted provided that the following conditions 867761Smsmith * are met: 967761Smsmith * 1. Redistributions of source code must retain the above copyright 1067761Smsmith * notice, this list of conditions and the following disclaimer. 1167761Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1267761Smsmith * notice, this list of conditions and the following disclaimer in the 1367761Smsmith * documentation and/or other materials provided with the distribution. 1467761Smsmith * 1567761Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1667761Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1767761Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1867761Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1967761Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2067761Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2167761Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2267761Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2367761Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2467761Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2567761Smsmith * SUCH DAMAGE. 2667761Smsmith * 2767761Smsmith * $FreeBSD: head/sys/dev/acpica/acpi_timer.c 119281 2003-08-22 06:06:16Z imp $ 2867761Smsmith */ 2967761Smsmith#include "opt_acpi.h" 3067761Smsmith#include <sys/param.h> 3180070Smsmith#include <sys/bus.h> 3267761Smsmith#include <sys/kernel.h> 3380070Smsmith#include <sys/sysctl.h> 34105281Sjhb#if __FreeBSD_version >= 500000 3580070Smsmith#include <sys/timetc.h> 36105281Sjhb#else 37105281Sjhb#include <sys/time.h> 38105281Sjhb#endif 3967761Smsmith 4080070Smsmith#include <machine/bus.h> 4180070Smsmith#include <machine/resource.h> 4280070Smsmith#include <sys/rman.h> 4380070Smsmith 4467761Smsmith#include "acpi.h" 4567761Smsmith 46104726Sjhb#include <dev/acpica/acpivar.h> 47119281Simp#include <dev/pci/pcivar.h> 4867761Smsmith 4969744Smsmith/* 5080070Smsmith * A timecounter based on the free-running ACPI timer. 5180070Smsmith * 5280070Smsmith * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>. 5380070Smsmith */ 5480070Smsmith 5580070Smsmith/* 5669744Smsmith * Hooks for the ACPI CA debugging infrastructure 5769744Smsmith */ 5877432Smsmith#define _COMPONENT ACPI_SYSTEM 5991128SmsmithACPI_MODULE_NAME("TIMER") 6069744Smsmith 6180070Smsmithstatic device_t acpi_timer_dev; 6280070Smsmithstruct resource *acpi_timer_reg; 6367761Smsmith 6480070Smsmithstatic u_int acpi_timer_frequency = 14318182/4; 6567761Smsmith 6667761Smsmithstatic void acpi_timer_identify(driver_t *driver, device_t parent); 6767761Smsmithstatic int acpi_timer_probe(device_t dev); 6867761Smsmithstatic int acpi_timer_attach(device_t dev); 6980070Smsmithstatic unsigned acpi_timer_get_timecount(struct timecounter *tc); 7081096Smsmithstatic unsigned acpi_timer_get_timecount_safe(struct timecounter *tc); 7180070Smsmithstatic int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS); 7280070Smsmithstatic void acpi_timer_test(void); 7367761Smsmith 74114277Smarcelstatic u_int32_t read_counter(void); 75114277Smarcelstatic int test_counter(void); 76114277Smarcel 7780070Smsmith/* 7880070Smsmith * Driver hung off ACPI. 7980070Smsmith */ 8067761Smsmithstatic device_method_t acpi_timer_methods[] = { 8167761Smsmith DEVMETHOD(device_identify, acpi_timer_identify), 8267761Smsmith DEVMETHOD(device_probe, acpi_timer_probe), 8367761Smsmith DEVMETHOD(device_attach, acpi_timer_attach), 8467761Smsmith 8567761Smsmith {0, 0} 8667761Smsmith}; 8767761Smsmith 8867761Smsmithstatic driver_t acpi_timer_driver = { 8967761Smsmith "acpi_timer", 9067761Smsmith acpi_timer_methods, 9180070Smsmith 0, 9267761Smsmith}; 9367761Smsmith 9489054Smsmithstatic devclass_t acpi_timer_devclass; 9567761SmsmithDRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0); 9667761Smsmith 9780070Smsmith/* 9880070Smsmith * Timecounter. 9980070Smsmith */ 10080070Smsmithstatic struct timecounter acpi_timer_timecounter = { 101118987Sphk acpi_timer_get_timecount_safe, 102118987Sphk 0, 103118987Sphk 0xffffff, 104118987Sphk 0, 105118987Sphk "ACPI", 106118987Sphk 1000 10780070Smsmith}; 10880070Smsmith 10980070Smsmith 110114277Smarcelstatic u_int32_t 111114277Smarcelread_counter() 112114277Smarcel{ 113114277Smarcel bus_space_handle_t bsh; 114114277Smarcel bus_space_tag_t bst; 115114277Smarcel u_int32_t tv; 116114277Smarcel 117114277Smarcel bsh = rman_get_bushandle(acpi_timer_reg); 118114277Smarcel bst = rman_get_bustag(acpi_timer_reg); 119114277Smarcel tv = bus_space_read_4(bst, bsh, 0); 120114277Smarcel bus_space_barrier(bst, bsh, 0, 4, BUS_SPACE_BARRIER_READ); 121114277Smarcel return (tv); 122114277Smarcel} 123114277Smarcel 12491237Sphk#define N 2000 12591237Sphkstatic int 12691237Sphktest_counter() 12791237Sphk{ 12891237Sphk int min, max, n, delta; 12991237Sphk unsigned last, this; 13091237Sphk 13191237Sphk min = 10000000; 13291237Sphk max = 0; 133114277Smarcel last = read_counter(); 13491237Sphk for (n = 0; n < N; n++) { 135114277Smarcel this = read_counter(); 13691237Sphk delta = (this - last) & 0xffffff; 13791237Sphk if (delta > max) 13891237Sphk max = delta; 13991237Sphk else if (delta < min) 14091237Sphk min = delta; 14191237Sphk last = this; 14291237Sphk } 14391237Sphk if (max - min > 2) 14491237Sphk n = 0; 145114277Smarcel else if (min < 0 || max == 0) 14691237Sphk n = 0; 14791237Sphk else 14891237Sphk n = 1; 14993093Sphk if (bootverbose) 15093093Sphk printf("ACPI timer looks %s min = %d, max = %d, width = %d\n", 15193093Sphk n ? "GOOD" : "BAD ", 152114277Smarcel min, max, max - min); 15391237Sphk return (n); 15491237Sphk} 15591237Sphk 15680070Smsmith/* 15780070Smsmith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources 15880070Smsmith * we will be using. 15980070Smsmith */ 16067761Smsmithstatic void 16167761Smsmithacpi_timer_identify(driver_t *driver, device_t parent) 16267761Smsmith{ 16380070Smsmith device_t dev; 16480070Smsmith char desc[40]; 165114277Smarcel u_long rlen, rstart; 166114277Smarcel int i, j, rid, rtype; 16767761Smsmith 16896926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16969744Smsmith 17069744Smsmith if (acpi_disabled("timer")) 17169744Smsmith return_VOID; 17269744Smsmith 17371872Smsmith if (AcpiGbl_FADT == NULL) 17469744Smsmith return_VOID; 17571872Smsmith 17667761Smsmith if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) { 17767761Smsmith device_printf(parent, "could not add acpi_timer0\n"); 17869744Smsmith return_VOID; 17967761Smsmith } 18080070Smsmith acpi_timer_dev = dev; 181114277Smarcel 18280070Smsmith rid = 0; 183114277Smarcel rlen = AcpiGbl_FADT->PmTmLen; 184114277Smarcel rtype = (AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId) 185114277Smarcel ? SYS_RES_IOPORT : SYS_RES_MEMORY; 186114277Smarcel rstart = AcpiGbl_FADT->XPmTmrBlk.Address; 187114277Smarcel bus_set_resource(dev, rtype, rid, rstart, rlen); 188114277Smarcel acpi_timer_reg = bus_alloc_resource(dev, rtype, &rid, 0, ~0, 1, RF_ACTIVE); 189114277Smarcel if (acpi_timer_reg == NULL) { 190114277Smarcel device_printf(dev, "couldn't allocate I/O resource (%s 0x%lx)\n", 191114277Smarcel (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart); 19269744Smsmith return_VOID; 19367761Smsmith } 19494936Smux if (testenv("debug.acpi.timer_test")) 19580070Smsmith acpi_timer_test(); 19667761Smsmith 19780070Smsmith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 19891237Sphk j = 0; 19991237Sphk for(i = 0; i < 10; i++) 20091237Sphk j += test_counter(); 20191237Sphk if (j == 10) { 20291237Sphk acpi_timer_timecounter.tc_name = "ACPI-fast"; 20391237Sphk acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount; 20491237Sphk } else { 20591237Sphk acpi_timer_timecounter.tc_name = "ACPI-safe"; 20691237Sphk acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe; 20791237Sphk } 20880070Smsmith tc_init(&acpi_timer_timecounter); 20980070Smsmith 210114277Smarcel sprintf(desc, "%d-bit timer at 3.579545MHz", (AcpiGbl_FADT->TmrValExt) 211114277Smarcel ? 32 : 24); 21267761Smsmith device_set_desc_copy(dev, desc); 21369744Smsmith 21469744Smsmith return_VOID; 21567761Smsmith} 21667761Smsmith 21767761Smsmithstatic int 21867761Smsmithacpi_timer_probe(device_t dev) 21967761Smsmith{ 22080070Smsmith if (dev == acpi_timer_dev) 22167761Smsmith return(0); 22267761Smsmith return(ENXIO); 22367761Smsmith} 22467761Smsmith 22567761Smsmithstatic int 22667761Smsmithacpi_timer_attach(device_t dev) 22767761Smsmith{ 22880070Smsmith return(0); 22980070Smsmith} 23067761Smsmith 23180070Smsmith/* 23281096Smsmith * Fetch current time value from reliable hardware. 23380070Smsmith */ 23480070Smsmithstatic unsigned 23580070Smsmithacpi_timer_get_timecount(struct timecounter *tc) 23680070Smsmith{ 237114277Smarcel return (read_counter()); 23867761Smsmith} 23980070Smsmith 24080070Smsmith/* 24181096Smsmith * Fetch current time value from hardware that may not correctly 24281096Smsmith * latch the counter. 24381096Smsmith */ 24481096Smsmithstatic unsigned 24581096Smsmithacpi_timer_get_timecount_safe(struct timecounter *tc) 24681096Smsmith{ 24781096Smsmith unsigned u1, u2, u3; 24881096Smsmith 249114277Smarcel u2 = read_counter(); 250114277Smarcel u3 = read_counter(); 25181096Smsmith do { 25281096Smsmith u1 = u2; 25381096Smsmith u2 = u3; 254114277Smarcel u3 = read_counter(); 25591237Sphk } while (u1 > u2 || u2 > u3 || (u3 - u1) > 15); 25681096Smsmith return (u2); 25781096Smsmith} 25881096Smsmith 25981096Smsmith/* 26080070Smsmith * Timecounter freqency adjustment interface. 26180070Smsmith */ 26280070Smsmithstatic int 26380070Smsmithacpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS) 26480070Smsmith{ 26580070Smsmith int error; 26680070Smsmith u_int freq; 26780070Smsmith 26880070Smsmith if (acpi_timer_timecounter.tc_frequency == 0) 26980070Smsmith return (EOPNOTSUPP); 27080070Smsmith freq = acpi_timer_frequency; 27180070Smsmith error = sysctl_handle_int(oidp, &freq, sizeof(freq), req); 27280070Smsmith if (error == 0 && req->newptr != NULL) { 27380070Smsmith acpi_timer_frequency = freq; 27480070Smsmith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 27580070Smsmith } 27680070Smsmith return (error); 27780070Smsmith} 27880070Smsmith 27980070SmsmithSYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW, 28080070Smsmith 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", ""); 28180070Smsmith 28280070Smsmith/* 28380070Smsmith * Test harness for verifying ACPI timer behaviour. 28480070Smsmith * Boot with debug.acpi.timer_test set to invoke this. 28580070Smsmith */ 28680070Smsmithstatic void 28780070Smsmithacpi_timer_test(void) 28880070Smsmith{ 28980070Smsmith u_int32_t u1, u2, u3; 29080070Smsmith 291114277Smarcel u1 = read_counter(); 292114277Smarcel u2 = read_counter(); 293114277Smarcel u3 = read_counter(); 29480070Smsmith 29580070Smsmith device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n"); 29680070Smsmith for (;;) { 29780070Smsmith /* 29880070Smsmith * The failure case is where u3 > u1, but u2 does not fall between the two, 29980070Smsmith * ie. it contains garbage. 30080070Smsmith */ 30180070Smsmith if (u3 > u1) { 30280070Smsmith if ((u2 < u1) || (u2 > u3)) 30380070Smsmith device_printf(acpi_timer_dev, "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n", 30480070Smsmith u1, u2, u3); 30580070Smsmith } 30680070Smsmith u1 = u2; 30780070Smsmith u2 = u3; 308114277Smarcel u3 = read_counter(); 30980070Smsmith } 31080070Smsmith} 31180070Smsmith 31280602Smsmith/* 31380602Smsmith * Chipset workaround driver hung off PCI. 31480602Smsmith * 31581096Smsmith * Some ACPI timers are known or believed to suffer from implementation 31681096Smsmith * problems which can lead to erroneous values being read from the timer. 31781096Smsmith * 31881096Smsmith * Since we can't trust unknown chipsets, we default to a timer-read 31981096Smsmith * routine which compensates for the most common problem (as detailed 32081096Smsmith * in the excerpt from the Intel PIIX4 datasheet below). 32181096Smsmith * 32281096Smsmith * When we detect a known-functional chipset, we disable the workaround 32381096Smsmith * to improve speed. 32481096Smsmith * 32580602Smsmith * ] 20. ACPI Timer Errata 32680602Smsmith * ] 32780602Smsmith * ] Problem: The power management timer may return improper result when 32880602Smsmith * ] read. Although the timer value settles properly after incrementing, 32980602Smsmith * ] while incrementing there is a 3nS window every 69.8nS where the 33080602Smsmith * ] timer value is indeterminate (a 4.2% chance that the data will be 33180602Smsmith * ] incorrect when read). As a result, the ACPI free running count up 33280602Smsmith * ] timer specification is violated due to erroneous reads. Implication: 33380602Smsmith * ] System hangs due to the "inaccuracy" of the timer when used by 33480602Smsmith * ] software for time critical events and delays. 33580602Smsmith * ] 33680602Smsmith * ] Workaround: Read the register twice and compare. 33780602Smsmith * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed 33880602Smsmith * ] in the PIIX4M. 33980602Smsmith * 34080602Smsmith * The counter is in other words not latched to the PCI bus clock when 34180602Smsmith * read. Notice the workaround isn't: We need to read until we have 34280602Smsmith * three monotonic samples and then use the middle one, otherwise we are 34380602Smsmith * not protected against the fact that the bits can be wrong in two 34480602Smsmith * directions. If we only cared about monosity two reads would be enough. 34580602Smsmith */ 34680602Smsmith 34791237Sphk#if 0 34880602Smsmithstatic int acpi_timer_pci_probe(device_t dev); 34980602Smsmith 35080602Smsmithstatic device_method_t acpi_timer_pci_methods[] = { 35180602Smsmith DEVMETHOD(device_probe, acpi_timer_pci_probe), 35280602Smsmith {0, 0} 35380602Smsmith}; 35480602Smsmith 35580602Smsmithstatic driver_t acpi_timer_pci_driver = { 35680602Smsmith "acpi_timer_pci", 35780602Smsmith acpi_timer_pci_methods, 35880602Smsmith 0, 35980602Smsmith}; 36080602Smsmith 36180602Smsmithdevclass_t acpi_timer_pci_devclass; 36280602SmsmithDRIVER_MODULE(acpi_timer_pci, pci, acpi_timer_pci_driver, acpi_timer_pci_devclass, 0, 0); 36380602Smsmith 36480602Smsmith/* 36581096Smsmith * Look at PCI devices going past; if we detect one we know contains 36681096Smsmith * a functional ACPI timer device, enable the faster timecounter read 36781096Smsmith * routine. 36880602Smsmith */ 36980602Smsmithstatic int 37080602Smsmithacpi_timer_pci_probe(device_t dev) 37180602Smsmith{ 37281172Smsmith int vendor, device, revid; 37381172Smsmith 37481172Smsmith vendor = pci_get_vendor(dev); 37581172Smsmith device = pci_get_device(dev); 37681172Smsmith revid = pci_get_revid(dev); 37781172Smsmith 37881172Smsmith if (((vendor == 0x8086) && (device == 0x7113) && (revid >= 0x03)) || /* PIIX4M */ 37981172Smsmith ((vendor == 0x8086) && (device == 0x719b)) || /* i440MX */ 38081172Smsmith 0) { 38181172Smsmith 38281096Smsmith acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount; 38381172Smsmith acpi_timer_timecounter.tc_name = "ACPI-fast"; 38481096Smsmith if (bootverbose) 38581172Smsmith device_printf(acpi_timer_dev, "functional ACPI timer detected, enabling fast timecount interface\n"); 38680602Smsmith } 38780602Smsmith 38880602Smsmith return(ENXIO); /* we never match anything */ 38980602Smsmith} 39091237Sphk#endif 391