acpi_timer.c revision 91128
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 91128 2002-02-23 05:31:38Z msmith $ 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> 3480070Smsmith#include <sys/timetc.h> 3567761Smsmith 3680070Smsmith#include <machine/bus_pio.h> 3780070Smsmith#include <machine/bus.h> 3880070Smsmith#include <machine/resource.h> 3980070Smsmith#include <sys/rman.h> 4080070Smsmith 4167761Smsmith#include "acpi.h" 4267761Smsmith 4380070Smsmith#include <acpica/acpivar.h> 4480070Smsmith#include <pci/pcivar.h> 4567761Smsmith 4669744Smsmith/* 4780070Smsmith * A timecounter based on the free-running ACPI timer. 4880070Smsmith * 4980070Smsmith * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>. 5080070Smsmith */ 5180070Smsmith 5280070Smsmith/* 5369744Smsmith * Hooks for the ACPI CA debugging infrastructure 5469744Smsmith */ 5577432Smsmith#define _COMPONENT ACPI_SYSTEM 5691128SmsmithACPI_MODULE_NAME("TIMER") 5769744Smsmith 5880070Smsmithstatic device_t acpi_timer_dev; 5980070Smsmithstruct resource *acpi_timer_reg; 6080070Smsmith#define TIMER_READ bus_space_read_4(rman_get_bustag(acpi_timer_reg), \ 6180070Smsmith rman_get_bushandle(acpi_timer_reg), \ 6280070Smsmith 0) 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 7480070Smsmith/* 7580070Smsmith * Driver hung off ACPI. 7680070Smsmith */ 7767761Smsmithstatic device_method_t acpi_timer_methods[] = { 7867761Smsmith DEVMETHOD(device_identify, acpi_timer_identify), 7967761Smsmith DEVMETHOD(device_probe, acpi_timer_probe), 8067761Smsmith DEVMETHOD(device_attach, acpi_timer_attach), 8167761Smsmith 8267761Smsmith {0, 0} 8367761Smsmith}; 8467761Smsmith 8567761Smsmithstatic driver_t acpi_timer_driver = { 8667761Smsmith "acpi_timer", 8767761Smsmith acpi_timer_methods, 8880070Smsmith 0, 8967761Smsmith}; 9067761Smsmith 9189054Smsmithstatic devclass_t acpi_timer_devclass; 9267761SmsmithDRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0); 9367761Smsmith 9480070Smsmith/* 9580070Smsmith * Timecounter. 9680070Smsmith */ 9780070Smsmithstatic struct timecounter acpi_timer_timecounter = { 9881096Smsmith acpi_timer_get_timecount_safe, 9980070Smsmith 0, 10080070Smsmith 0xffffff, 10180070Smsmith 0, 10280070Smsmith "ACPI" 10380070Smsmith}; 10480070Smsmith 10580070SmsmithSYSCTL_OPAQUE(_debug, OID_AUTO, acpi_timecounter, CTLFLAG_RD, 10680070Smsmith &acpi_timer_timecounter, sizeof(acpi_timer_timecounter), "S,timecounter", ""); 10780070Smsmith 10880070Smsmith/* 10980070Smsmith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources 11080070Smsmith * we will be using. 11180070Smsmith */ 11267761Smsmithstatic void 11367761Smsmithacpi_timer_identify(driver_t *driver, device_t parent) 11467761Smsmith{ 11580070Smsmith device_t dev; 11680070Smsmith char desc[40]; 11780070Smsmith int rid; 11867761Smsmith 11991128Smsmith ACPI_FUNCTION_TRACE(__func__); 12069744Smsmith 12169744Smsmith if (acpi_disabled("timer")) 12269744Smsmith return_VOID; 12369744Smsmith 12471872Smsmith if (AcpiGbl_FADT == NULL) 12569744Smsmith return_VOID; 12671872Smsmith 12767761Smsmith if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) { 12867761Smsmith device_printf(parent, "could not add acpi_timer0\n"); 12969744Smsmith return_VOID; 13067761Smsmith } 13180070Smsmith acpi_timer_dev = dev; 13280070Smsmith rid = 0; 13380070Smsmith bus_set_resource(dev, SYS_RES_IOPORT, rid, AcpiGbl_FADT->V1_PmTmrBlk, sizeof(u_int32_t)); 13480070Smsmith if ((acpi_timer_reg = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE)) == NULL) { 13580070Smsmith device_printf(dev, "couldn't allocate I/O resource (port 0x%x)\n", AcpiGbl_FADT->V1_PmTmrBlk); 13669744Smsmith return_VOID; 13767761Smsmith } 13880070Smsmith if (getenv("debug.acpi.timer_test") != NULL) 13980070Smsmith acpi_timer_test(); 14067761Smsmith 14180602Smsmith acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount; 14280070Smsmith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 14380070Smsmith tc_init(&acpi_timer_timecounter); 14480070Smsmith 14571872Smsmith sprintf(desc, "%d-bit timer at 3.579545MHz", AcpiGbl_FADT->TmrValExt ? 32 : 24); 14667761Smsmith device_set_desc_copy(dev, desc); 14769744Smsmith 14881096Smsmith#if 0 14981096Smsmith { 15081096Smsmith u_int64_t first; 15181096Smsmith 15281096Smsmith first = rdtsc(); 15381096Smsmith acpi_timer_get_timecount(NULL); 15481096Smsmith printf("acpi_timer_get_timecount %lld cycles\n", rdtsc() - first); 15581096Smsmith 15681096Smsmith first = rdtsc(); 15781096Smsmith acpi_timer_get_timecount_safe(NULL); 15881096Smsmith printf("acpi_timer_get_timecount_safe %lld cycles\n", rdtsc() - first); 15981096Smsmith } 16081096Smsmith#endif 16181096Smsmith 16269744Smsmith return_VOID; 16367761Smsmith} 16467761Smsmith 16567761Smsmithstatic int 16667761Smsmithacpi_timer_probe(device_t dev) 16767761Smsmith{ 16880070Smsmith if (dev == acpi_timer_dev) 16967761Smsmith return(0); 17067761Smsmith return(ENXIO); 17167761Smsmith} 17267761Smsmith 17367761Smsmithstatic int 17467761Smsmithacpi_timer_attach(device_t dev) 17567761Smsmith{ 17680070Smsmith return(0); 17780070Smsmith} 17867761Smsmith 17980070Smsmith/* 18081096Smsmith * Fetch current time value from reliable hardware. 18180070Smsmith */ 18280070Smsmithstatic unsigned 18380070Smsmithacpi_timer_get_timecount(struct timecounter *tc) 18480070Smsmith{ 18580602Smsmith return(TIMER_READ); 18667761Smsmith} 18780070Smsmith 18880070Smsmith/* 18981096Smsmith * Fetch current time value from hardware that may not correctly 19081096Smsmith * latch the counter. 19181096Smsmith */ 19281096Smsmithstatic unsigned 19381096Smsmithacpi_timer_get_timecount_safe(struct timecounter *tc) 19481096Smsmith{ 19581096Smsmith unsigned u1, u2, u3; 19681096Smsmith 19781096Smsmith u2 = TIMER_READ; 19881096Smsmith u3 = TIMER_READ; 19981096Smsmith do { 20081096Smsmith u1 = u2; 20181096Smsmith u2 = u3; 20281096Smsmith u3 = TIMER_READ; 20381096Smsmith } while (u1 > u2 || u2 > u3); 20481096Smsmith return (u2); 20581096Smsmith} 20681096Smsmith 20781096Smsmith/* 20880070Smsmith * Timecounter freqency adjustment interface. 20980070Smsmith */ 21080070Smsmithstatic int 21180070Smsmithacpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS) 21280070Smsmith{ 21380070Smsmith int error; 21480070Smsmith u_int freq; 21580070Smsmith 21680070Smsmith if (acpi_timer_timecounter.tc_frequency == 0) 21780070Smsmith return (EOPNOTSUPP); 21880070Smsmith freq = acpi_timer_frequency; 21980070Smsmith error = sysctl_handle_int(oidp, &freq, sizeof(freq), req); 22080070Smsmith if (error == 0 && req->newptr != NULL) { 22180070Smsmith acpi_timer_frequency = freq; 22280070Smsmith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 22380070Smsmith tc_update(&acpi_timer_timecounter); 22480070Smsmith } 22580070Smsmith return (error); 22680070Smsmith} 22780070Smsmith 22880070SmsmithSYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW, 22980070Smsmith 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", ""); 23080070Smsmith 23180070Smsmith/* 23280070Smsmith * Test harness for verifying ACPI timer behaviour. 23380070Smsmith * Boot with debug.acpi.timer_test set to invoke this. 23480070Smsmith */ 23580070Smsmithstatic void 23680070Smsmithacpi_timer_test(void) 23780070Smsmith{ 23880070Smsmith u_int32_t u1, u2, u3; 23980070Smsmith 24080070Smsmith u1 = TIMER_READ; 24180070Smsmith u2 = TIMER_READ; 24280070Smsmith u3 = TIMER_READ; 24380070Smsmith 24480070Smsmith device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n"); 24580070Smsmith for (;;) { 24680070Smsmith /* 24780070Smsmith * The failure case is where u3 > u1, but u2 does not fall between the two, 24880070Smsmith * ie. it contains garbage. 24980070Smsmith */ 25080070Smsmith if (u3 > u1) { 25180070Smsmith if ((u2 < u1) || (u2 > u3)) 25280070Smsmith device_printf(acpi_timer_dev, "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n", 25380070Smsmith u1, u2, u3); 25480070Smsmith } 25580070Smsmith u1 = u2; 25680070Smsmith u2 = u3; 25780070Smsmith u3 = TIMER_READ; 25880070Smsmith } 25980070Smsmith} 26080070Smsmith 26180602Smsmith/* 26280602Smsmith * Chipset workaround driver hung off PCI. 26380602Smsmith * 26481096Smsmith * Some ACPI timers are known or believed to suffer from implementation 26581096Smsmith * problems which can lead to erroneous values being read from the timer. 26681096Smsmith * 26781096Smsmith * Since we can't trust unknown chipsets, we default to a timer-read 26881096Smsmith * routine which compensates for the most common problem (as detailed 26981096Smsmith * in the excerpt from the Intel PIIX4 datasheet below). 27081096Smsmith * 27181096Smsmith * When we detect a known-functional chipset, we disable the workaround 27281096Smsmith * to improve speed. 27381096Smsmith * 27480602Smsmith * ] 20. ACPI Timer Errata 27580602Smsmith * ] 27680602Smsmith * ] Problem: The power management timer may return improper result when 27780602Smsmith * ] read. Although the timer value settles properly after incrementing, 27880602Smsmith * ] while incrementing there is a 3nS window every 69.8nS where the 27980602Smsmith * ] timer value is indeterminate (a 4.2% chance that the data will be 28080602Smsmith * ] incorrect when read). As a result, the ACPI free running count up 28180602Smsmith * ] timer specification is violated due to erroneous reads. Implication: 28280602Smsmith * ] System hangs due to the "inaccuracy" of the timer when used by 28380602Smsmith * ] software for time critical events and delays. 28480602Smsmith * ] 28580602Smsmith * ] Workaround: Read the register twice and compare. 28680602Smsmith * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed 28780602Smsmith * ] in the PIIX4M. 28880602Smsmith * 28980602Smsmith * The counter is in other words not latched to the PCI bus clock when 29080602Smsmith * read. Notice the workaround isn't: We need to read until we have 29180602Smsmith * three monotonic samples and then use the middle one, otherwise we are 29280602Smsmith * not protected against the fact that the bits can be wrong in two 29380602Smsmith * directions. If we only cared about monosity two reads would be enough. 29480602Smsmith */ 29580602Smsmith 29680602Smsmithstatic int acpi_timer_pci_probe(device_t dev); 29780602Smsmith 29880602Smsmithstatic device_method_t acpi_timer_pci_methods[] = { 29980602Smsmith DEVMETHOD(device_probe, acpi_timer_pci_probe), 30080602Smsmith {0, 0} 30180602Smsmith}; 30280602Smsmith 30380602Smsmithstatic driver_t acpi_timer_pci_driver = { 30480602Smsmith "acpi_timer_pci", 30580602Smsmith acpi_timer_pci_methods, 30680602Smsmith 0, 30780602Smsmith}; 30880602Smsmith 30980602Smsmithdevclass_t acpi_timer_pci_devclass; 31080602SmsmithDRIVER_MODULE(acpi_timer_pci, pci, acpi_timer_pci_driver, acpi_timer_pci_devclass, 0, 0); 31180602Smsmith 31280602Smsmith/* 31381096Smsmith * Look at PCI devices going past; if we detect one we know contains 31481096Smsmith * a functional ACPI timer device, enable the faster timecounter read 31581096Smsmith * routine. 31680602Smsmith */ 31780602Smsmithstatic int 31880602Smsmithacpi_timer_pci_probe(device_t dev) 31980602Smsmith{ 32081172Smsmith int vendor, device, revid; 32181172Smsmith 32281172Smsmith vendor = pci_get_vendor(dev); 32381172Smsmith device = pci_get_device(dev); 32481172Smsmith revid = pci_get_revid(dev); 32581172Smsmith 32681172Smsmith if (((vendor == 0x8086) && (device == 0x7113) && (revid >= 0x03)) || /* PIIX4M */ 32781172Smsmith ((vendor == 0x8086) && (device == 0x719b)) || /* i440MX */ 32881172Smsmith 0) { 32981172Smsmith 33081096Smsmith acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount; 33181172Smsmith acpi_timer_timecounter.tc_name = "ACPI-fast"; 33281096Smsmith if (bootverbose) 33381172Smsmith device_printf(acpi_timer_dev, "functional ACPI timer detected, enabling fast timecount interface\n"); 33480602Smsmith } 33580602Smsmith 33680602Smsmith return(ENXIO); /* we never match anything */ 33780602Smsmith} 338