acpi_timer.c revision 80602
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 80602 2001-07-30 08:57:55Z 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 5669744SmsmithMODULE_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); 7080070Smsmithstatic int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS); 7180070Smsmithstatic void acpi_timer_test(void); 7267761Smsmith 7380070Smsmith/* 7480070Smsmith * Driver hung off ACPI. 7580070Smsmith */ 7667761Smsmithstatic device_method_t acpi_timer_methods[] = { 7767761Smsmith DEVMETHOD(device_identify, acpi_timer_identify), 7867761Smsmith DEVMETHOD(device_probe, acpi_timer_probe), 7967761Smsmith DEVMETHOD(device_attach, acpi_timer_attach), 8067761Smsmith 8167761Smsmith {0, 0} 8267761Smsmith}; 8367761Smsmith 8467761Smsmithstatic driver_t acpi_timer_driver = { 8567761Smsmith "acpi_timer", 8667761Smsmith acpi_timer_methods, 8780070Smsmith 0, 8867761Smsmith}; 8967761Smsmith 9067761Smsmithdevclass_t acpi_timer_devclass; 9167761SmsmithDRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0); 9267761Smsmith 9380070Smsmith/* 9480070Smsmith * Timecounter. 9580070Smsmith */ 9680070Smsmithstatic struct timecounter acpi_timer_timecounter = { 9780070Smsmith acpi_timer_get_timecount, 9880070Smsmith 0, 9980070Smsmith 0xffffff, 10080070Smsmith 0, 10180070Smsmith "ACPI" 10280070Smsmith}; 10380070Smsmith 10480070SmsmithSYSCTL_OPAQUE(_debug, OID_AUTO, acpi_timecounter, CTLFLAG_RD, 10580070Smsmith &acpi_timer_timecounter, sizeof(acpi_timer_timecounter), "S,timecounter", ""); 10680070Smsmith 10780070Smsmith/* 10880070Smsmith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources 10980070Smsmith * we will be using. 11080070Smsmith */ 11167761Smsmithstatic void 11267761Smsmithacpi_timer_identify(driver_t *driver, device_t parent) 11367761Smsmith{ 11480070Smsmith device_t dev; 11580070Smsmith char desc[40]; 11680070Smsmith int rid; 11767761Smsmith 11877432Smsmith FUNCTION_TRACE(__func__); 11969744Smsmith 12069744Smsmith if (acpi_disabled("timer")) 12169744Smsmith return_VOID; 12269744Smsmith 12371872Smsmith if (AcpiGbl_FADT == NULL) 12469744Smsmith return_VOID; 12571872Smsmith 12667761Smsmith if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) { 12767761Smsmith device_printf(parent, "could not add acpi_timer0\n"); 12869744Smsmith return_VOID; 12967761Smsmith } 13080070Smsmith acpi_timer_dev = dev; 13180070Smsmith rid = 0; 13280070Smsmith bus_set_resource(dev, SYS_RES_IOPORT, rid, AcpiGbl_FADT->V1_PmTmrBlk, sizeof(u_int32_t)); 13380070Smsmith if ((acpi_timer_reg = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE)) == NULL) { 13480070Smsmith device_printf(dev, "couldn't allocate I/O resource (port 0x%x)\n", AcpiGbl_FADT->V1_PmTmrBlk); 13569744Smsmith return_VOID; 13667761Smsmith } 13780070Smsmith if (getenv("debug.acpi.timer_test") != NULL) 13880070Smsmith acpi_timer_test(); 13967761Smsmith 14080602Smsmith acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount; 14180070Smsmith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 14280070Smsmith tc_init(&acpi_timer_timecounter); 14380070Smsmith 14471872Smsmith sprintf(desc, "%d-bit timer at 3.579545MHz", AcpiGbl_FADT->TmrValExt ? 32 : 24); 14567761Smsmith device_set_desc_copy(dev, desc); 14669744Smsmith 14769744Smsmith return_VOID; 14867761Smsmith} 14967761Smsmith 15067761Smsmithstatic int 15167761Smsmithacpi_timer_probe(device_t dev) 15267761Smsmith{ 15380070Smsmith if (dev == acpi_timer_dev) 15467761Smsmith return(0); 15567761Smsmith return(ENXIO); 15667761Smsmith} 15767761Smsmith 15867761Smsmithstatic int 15967761Smsmithacpi_timer_attach(device_t dev) 16067761Smsmith{ 16180070Smsmith return(0); 16280070Smsmith} 16367761Smsmith 16480070Smsmith/* 16580070Smsmith * Fetch current time value from hardware. 16680070Smsmith */ 16780070Smsmithstatic unsigned 16880070Smsmithacpi_timer_get_timecount(struct timecounter *tc) 16980070Smsmith{ 17080602Smsmith return(TIMER_READ); 17167761Smsmith} 17280070Smsmith 17380070Smsmith/* 17480070Smsmith * Timecounter freqency adjustment interface. 17580070Smsmith */ 17680070Smsmithstatic int 17780070Smsmithacpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS) 17880070Smsmith{ 17980070Smsmith int error; 18080070Smsmith u_int freq; 18180070Smsmith 18280070Smsmith if (acpi_timer_timecounter.tc_frequency == 0) 18380070Smsmith return (EOPNOTSUPP); 18480070Smsmith freq = acpi_timer_frequency; 18580070Smsmith error = sysctl_handle_int(oidp, &freq, sizeof(freq), req); 18680070Smsmith if (error == 0 && req->newptr != NULL) { 18780070Smsmith acpi_timer_frequency = freq; 18880070Smsmith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 18980070Smsmith tc_update(&acpi_timer_timecounter); 19080070Smsmith } 19180070Smsmith return (error); 19280070Smsmith} 19380070Smsmith 19480070SmsmithSYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW, 19580070Smsmith 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", ""); 19680070Smsmith 19780070Smsmith/* 19880070Smsmith * Test harness for verifying ACPI timer behaviour. 19980070Smsmith * Boot with debug.acpi.timer_test set to invoke this. 20080070Smsmith */ 20180070Smsmithstatic void 20280070Smsmithacpi_timer_test(void) 20380070Smsmith{ 20480070Smsmith u_int32_t u1, u2, u3; 20580070Smsmith 20680070Smsmith u1 = TIMER_READ; 20780070Smsmith u2 = TIMER_READ; 20880070Smsmith u3 = TIMER_READ; 20980070Smsmith 21080070Smsmith device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n"); 21180070Smsmith for (;;) { 21280070Smsmith /* 21380070Smsmith * The failure case is where u3 > u1, but u2 does not fall between the two, 21480070Smsmith * ie. it contains garbage. 21580070Smsmith */ 21680070Smsmith if (u3 > u1) { 21780070Smsmith if ((u2 < u1) || (u2 > u3)) 21880070Smsmith device_printf(acpi_timer_dev, "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n", 21980070Smsmith u1, u2, u3); 22080070Smsmith } 22180070Smsmith u1 = u2; 22280070Smsmith u2 = u3; 22380070Smsmith u3 = TIMER_READ; 22480070Smsmith } 22580070Smsmith} 22680070Smsmith 22780602Smsmith/* 22880602Smsmith * Chipset workaround driver hung off PCI. 22980602Smsmith * 23080602Smsmith * ] 20. ACPI Timer Errata 23180602Smsmith * ] 23280602Smsmith * ] Problem: The power management timer may return improper result when 23380602Smsmith * ] read. Although the timer value settles properly after incrementing, 23480602Smsmith * ] while incrementing there is a 3nS window every 69.8nS where the 23580602Smsmith * ] timer value is indeterminate (a 4.2% chance that the data will be 23680602Smsmith * ] incorrect when read). As a result, the ACPI free running count up 23780602Smsmith * ] timer specification is violated due to erroneous reads. Implication: 23880602Smsmith * ] System hangs due to the "inaccuracy" of the timer when used by 23980602Smsmith * ] software for time critical events and delays. 24080602Smsmith * ] 24180602Smsmith * ] Workaround: Read the register twice and compare. 24280602Smsmith * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed 24380602Smsmith * ] in the PIIX4M. 24480602Smsmith * 24580602Smsmith * The counter is in other words not latched to the PCI bus clock when 24680602Smsmith * read. Notice the workaround isn't: We need to read until we have 24780602Smsmith * three monotonic samples and then use the middle one, otherwise we are 24880602Smsmith * not protected against the fact that the bits can be wrong in two 24980602Smsmith * directions. If we only cared about monosity two reads would be enough. 25080602Smsmith */ 25180602Smsmith 25280602Smsmithstatic int acpi_timer_pci_probe(device_t dev); 25380602Smsmithstatic unsigned acpi_timer_get_timecount_piix(struct timecounter *tc); 25480602Smsmith 25580602Smsmithstatic device_method_t acpi_timer_pci_methods[] = { 25680602Smsmith DEVMETHOD(device_probe, acpi_timer_pci_probe), 25780602Smsmith {0, 0} 25880602Smsmith}; 25980602Smsmith 26080602Smsmithstatic driver_t acpi_timer_pci_driver = { 26180602Smsmith "acpi_timer_pci", 26280602Smsmith acpi_timer_pci_methods, 26380602Smsmith 0, 26480602Smsmith}; 26580602Smsmith 26680602Smsmithdevclass_t acpi_timer_pci_devclass; 26780602SmsmithDRIVER_MODULE(acpi_timer_pci, pci, acpi_timer_pci_driver, acpi_timer_pci_devclass, 0, 0); 26880602Smsmith 26980602Smsmith/* 27080602Smsmith * Look at PCI devices as they go past, and if we detect a PIIX4 older than 27180602Smsmith * the PIIX4M, use an alternate get_timecount routine. 27280602Smsmith * 27380602Smsmith * XXX do we know that other timecounters work? Perhaps we should test them? 27480602Smsmith */ 27580602Smsmithstatic int 27680602Smsmithacpi_timer_pci_probe(device_t dev) 27780602Smsmith{ 27880602Smsmith if ((pci_get_vendor(dev) == 0x8086) && 27980602Smsmith (pci_get_device(dev) == 0x7113) && 28080602Smsmith (pci_get_revid(dev) < 0x03)) { 28180602Smsmith acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_piix; 28280602Smsmith acpi_timer_timecounter.tc_name = "ACPI-PIIX"; 28380602Smsmith device_printf(acpi_timer_dev, "enabling PIIX4 timer workaround\n"); 28480602Smsmith } 28580602Smsmith 28680602Smsmith return(ENXIO); /* we never match anything */ 28780602Smsmith} 28880602Smsmith 28980602Smsmith/* 29080602Smsmith * Read the buggy PIIX4 ACPI timer and compensate for its behaviour. 29180602Smsmith */ 29280602Smsmithstatic unsigned 29380602Smsmithacpi_timer_get_timecount_piix(struct timecounter *tc) 29480602Smsmith{ 29580602Smsmith unsigned u1, u2, u3; 29680602Smsmith 29780602Smsmith u2 = TIMER_READ; 29880602Smsmith u3 = TIMER_READ; 29980602Smsmith do { 30080602Smsmith u1 = u2; 30180602Smsmith u2 = u3; 30280602Smsmith u3 = TIMER_READ; 30380602Smsmith } while (u1 > u2 || u2 > u3); 30480602Smsmith return (u2); 30580602Smsmith} 30680602Smsmith 307