acpi_timer.c revision 80070
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 80070 2001-07-21 04:08:42Z msmith $ 2867761Smsmith */ 2980070Smsmith/* 3080070Smsmith * ---------------------------------------------------------------------------- 3180070Smsmith * "THE BEER-WARE LICENSE" (Revision 42): 3280070Smsmith * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you 3380070Smsmith * can do whatever you want with this stuff. If we meet some day, and you think 3480070Smsmith * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 3580070Smsmith * ---------------------------------------------------------------------------- 3680070Smsmith */ 3767761Smsmith#include "opt_acpi.h" 3867761Smsmith#include <sys/param.h> 3980070Smsmith#include <sys/bus.h> 4067761Smsmith#include <sys/kernel.h> 4180070Smsmith#include <sys/sysctl.h> 4280070Smsmith#include <sys/timetc.h> 4367761Smsmith 4480070Smsmith#include <machine/bus_pio.h> 4580070Smsmith#include <machine/bus.h> 4680070Smsmith#include <machine/resource.h> 4780070Smsmith#include <sys/rman.h> 4880070Smsmith 4967761Smsmith#include "acpi.h" 5067761Smsmith 5180070Smsmith#include <acpica/acpivar.h> 5280070Smsmith#include <pci/pcivar.h> 5367761Smsmith 5469744Smsmith/* 5580070Smsmith * A timecounter based on the free-running ACPI timer. 5680070Smsmith * 5780070Smsmith * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>. 5880070Smsmith */ 5980070Smsmith 6080070Smsmith/* 6169744Smsmith * Hooks for the ACPI CA debugging infrastructure 6269744Smsmith */ 6377432Smsmith#define _COMPONENT ACPI_SYSTEM 6469744SmsmithMODULE_NAME("TIMER") 6569744Smsmith 6680070Smsmithstatic device_t acpi_timer_dev; 6780070Smsmithstruct resource *acpi_timer_reg; 6880070Smsmith#define TIMER_READ bus_space_read_4(rman_get_bustag(acpi_timer_reg), \ 6980070Smsmith rman_get_bushandle(acpi_timer_reg), \ 7080070Smsmith 0) 7180070Smsmithstatic int acpi_timer_flags; 7280070Smsmith/* 7380070Smsmith * ] 20. ACPI Timer Errata 7480070Smsmith * ] 7580070Smsmith * ] Problem: The power management timer may return improper result when 7680070Smsmith * ] read. Although the timer value settles properly after incrementing, 7780070Smsmith * ] while incrementing there is a 3nS window every 69.8nS where the 7880070Smsmith * ] timer value is indeterminate (a 4.2% chance that the data will be 7980070Smsmith * ] incorrect when read). As a result, the ACPI free running count up 8080070Smsmith * ] timer specification is violated due to erroneous reads. Implication: 8180070Smsmith * ] System hangs due to the "inaccuracy" of the timer when used by 8280070Smsmith * ] software for time critical events and delays. 8380070Smsmith * ] 8480070Smsmith * ] Workaround: Read the register twice and compare. 8580070Smsmith * ] Status: This will not be fixed in the PIIX4 or PIIX4E. 8680070Smsmith * 8780070Smsmith * The counter is in other words not latched to the PCI bus clock when 8880070Smsmith * read. Notice the workaround isn't: We need to read until we have 8980070Smsmith * three monotonic samples and then use the middle one, otherwise we are 9080070Smsmith * not protected against the fact that the bits can be wrong in two 9180070Smsmith * directions. If we only cared about monosity two reads would be enough. 9280070Smsmith */ 9380070Smsmith#define TFLAG_NEED_PIIX_WAR (1 << 0) 9467761Smsmith 9580070Smsmithstatic u_int acpi_timer_frequency = 14318182/4; 9667761Smsmith 9767761Smsmithstatic void acpi_timer_identify(driver_t *driver, device_t parent); 9867761Smsmithstatic int acpi_timer_probe(device_t dev); 9967761Smsmithstatic int acpi_timer_attach(device_t dev); 10080070Smsmithstatic int acpi_timer_pci_probe(device_t dev); 10180070Smsmithstatic unsigned acpi_timer_get_timecount(struct timecounter *tc); 10280070Smsmithstatic int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS); 10380070Smsmithstatic void acpi_timer_test(void); 10467761Smsmith 10580070Smsmith/* 10680070Smsmith * Driver hung off ACPI. 10780070Smsmith */ 10867761Smsmithstatic device_method_t acpi_timer_methods[] = { 10967761Smsmith DEVMETHOD(device_identify, acpi_timer_identify), 11067761Smsmith DEVMETHOD(device_probe, acpi_timer_probe), 11167761Smsmith DEVMETHOD(device_attach, acpi_timer_attach), 11267761Smsmith 11367761Smsmith {0, 0} 11467761Smsmith}; 11567761Smsmith 11667761Smsmithstatic driver_t acpi_timer_driver = { 11767761Smsmith "acpi_timer", 11867761Smsmith acpi_timer_methods, 11980070Smsmith 0, 12067761Smsmith}; 12167761Smsmith 12267761Smsmithdevclass_t acpi_timer_devclass; 12367761SmsmithDRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0); 12467761Smsmith 12580070Smsmith/* 12680070Smsmith * Chipset workaround driver hung off PCI. 12780070Smsmith */ 12880070Smsmithstatic device_method_t acpi_timer_pci_methods[] = { 12980070Smsmith DEVMETHOD(device_probe, acpi_timer_pci_probe), 13080070Smsmith {0, 0} 13180070Smsmith}; 13280070Smsmith 13380070Smsmithstatic driver_t acpi_timer_pci_driver = { 13480070Smsmith "acpi_timer_pci", 13580070Smsmith acpi_timer_pci_methods, 13680070Smsmith 0, 13780070Smsmith}; 13880070Smsmith 13980070Smsmithdevclass_t acpi_timer_pci_devclass; 14080070SmsmithDRIVER_MODULE(acpi_timer_pci, pci, acpi_timer_pci_driver, acpi_timer_pci_devclass, 0, 0); 14180070Smsmith 14280070Smsmith/* 14380070Smsmith * Timecounter. 14480070Smsmith */ 14580070Smsmithstatic struct timecounter acpi_timer_timecounter = { 14680070Smsmith acpi_timer_get_timecount, 14780070Smsmith 0, 14880070Smsmith 0xffffff, 14980070Smsmith 0, 15080070Smsmith "ACPI" 15180070Smsmith}; 15280070Smsmith 15380070SmsmithSYSCTL_OPAQUE(_debug, OID_AUTO, acpi_timecounter, CTLFLAG_RD, 15480070Smsmith &acpi_timer_timecounter, sizeof(acpi_timer_timecounter), "S,timecounter", ""); 15580070Smsmith 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]; 16580070Smsmith int rid; 16667761Smsmith 16777432Smsmith FUNCTION_TRACE(__func__); 16869744Smsmith 16969744Smsmith if (acpi_disabled("timer")) 17069744Smsmith return_VOID; 17169744Smsmith 17271872Smsmith if (AcpiGbl_FADT == NULL) 17369744Smsmith return_VOID; 17471872Smsmith 17567761Smsmith if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) { 17667761Smsmith device_printf(parent, "could not add acpi_timer0\n"); 17769744Smsmith return_VOID; 17867761Smsmith } 17980070Smsmith acpi_timer_dev = dev; 18080070Smsmith rid = 0; 18180070Smsmith bus_set_resource(dev, SYS_RES_IOPORT, rid, AcpiGbl_FADT->V1_PmTmrBlk, sizeof(u_int32_t)); 18280070Smsmith if ((acpi_timer_reg = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE)) == NULL) { 18380070Smsmith device_printf(dev, "couldn't allocate I/O resource (port 0x%x)\n", AcpiGbl_FADT->V1_PmTmrBlk); 18469744Smsmith return_VOID; 18567761Smsmith } 18680070Smsmith if (getenv("debug.acpi.timer_test") != NULL) 18780070Smsmith acpi_timer_test(); 18867761Smsmith 18980070Smsmith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 19080070Smsmith tc_init(&acpi_timer_timecounter); 19180070Smsmith 19271872Smsmith sprintf(desc, "%d-bit timer at 3.579545MHz", AcpiGbl_FADT->TmrValExt ? 32 : 24); 19367761Smsmith device_set_desc_copy(dev, desc); 19469744Smsmith 19569744Smsmith return_VOID; 19667761Smsmith} 19767761Smsmith 19867761Smsmithstatic int 19967761Smsmithacpi_timer_probe(device_t dev) 20067761Smsmith{ 20180070Smsmith if (dev == acpi_timer_dev) 20267761Smsmith return(0); 20367761Smsmith return(ENXIO); 20467761Smsmith} 20567761Smsmith 20667761Smsmithstatic int 20767761Smsmithacpi_timer_attach(device_t dev) 20867761Smsmith{ 20980070Smsmith return(0); 21080070Smsmith} 21167761Smsmith 21280070Smsmith/* 21380070Smsmith * Look at PCI devices as they go past, and if we detect a PIIX4, set 21480070Smsmith * the PIIX_WAR flag. 21580070Smsmith * 21680070Smsmith * XXX do we know that other timecounters work? Interesting question. 21780070Smsmith */ 21880070Smsmithstatic int 21980070Smsmithacpi_timer_pci_probe(device_t dev) 22080070Smsmith{ 22180070Smsmith if ((pci_get_vendor(dev) == 0x8086) && 22280070Smsmith (pci_get_device(dev) == 0x7113)) { 22380070Smsmith acpi_timer_flags |= TFLAG_NEED_PIIX_WAR; 22480070Smsmith device_printf(acpi_timer_dev, "enabling PIIX4 timer workaround\n"); 22580070Smsmith } 22669744Smsmith 22780070Smsmith return(ENXIO); /* we never match anything */ 22880070Smsmith} 22967761Smsmith 23080070Smsmith/* 23180070Smsmith * Fetch current time value from hardware. 23280070Smsmith * 23380070Smsmith * XXX This is currently written to be "correct", not 23480070Smsmith * "fast". Optimisation is strongly indicated. 23580070Smsmith */ 23680070Smsmithstatic unsigned 23780070Smsmithacpi_timer_get_timecount(struct timecounter *tc) 23880070Smsmith{ 23980070Smsmith unsigned u1, u2, u3; 24080070Smsmith 24180070Smsmith if (acpi_timer_flags & TFLAG_NEED_PIIX_WAR) { 24280070Smsmith u2 = TIMER_READ; 24380070Smsmith u3 = TIMER_READ; 24480070Smsmith do { 24580070Smsmith u1 = u2; 24680070Smsmith u2 = u3; 24780070Smsmith u3 = TIMER_READ; 24880070Smsmith } while (u1 > u2 || u2 > u3); 24980070Smsmith return (u2); 25080070Smsmith } else { 25180070Smsmith return(TIMER_READ); 25280070Smsmith } 25367761Smsmith} 25480070Smsmith 25580070Smsmith/* 25680070Smsmith * Timecounter freqency adjustment interface. 25780070Smsmith */ 25880070Smsmithstatic int 25980070Smsmithacpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS) 26080070Smsmith{ 26180070Smsmith int error; 26280070Smsmith u_int freq; 26380070Smsmith 26480070Smsmith if (acpi_timer_timecounter.tc_frequency == 0) 26580070Smsmith return (EOPNOTSUPP); 26680070Smsmith freq = acpi_timer_frequency; 26780070Smsmith error = sysctl_handle_int(oidp, &freq, sizeof(freq), req); 26880070Smsmith if (error == 0 && req->newptr != NULL) { 26980070Smsmith acpi_timer_frequency = freq; 27080070Smsmith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 27180070Smsmith tc_update(&acpi_timer_timecounter); 27280070Smsmith } 27380070Smsmith return (error); 27480070Smsmith} 27580070Smsmith 27680070SmsmithSYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW, 27780070Smsmith 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", ""); 27880070Smsmith 27980070Smsmith/* 28080070Smsmith * Test harness for verifying ACPI timer behaviour. 28180070Smsmith * Boot with debug.acpi.timer_test set to invoke this. 28280070Smsmith */ 28380070Smsmithstatic void 28480070Smsmithacpi_timer_test(void) 28580070Smsmith{ 28680070Smsmith u_int32_t u1, u2, u3; 28780070Smsmith 28880070Smsmith u1 = TIMER_READ; 28980070Smsmith u2 = TIMER_READ; 29080070Smsmith u3 = TIMER_READ; 29180070Smsmith 29280070Smsmith device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n"); 29380070Smsmith for (;;) { 29480070Smsmith /* 29580070Smsmith * The failure case is where u3 > u1, but u2 does not fall between the two, 29680070Smsmith * ie. it contains garbage. 29780070Smsmith */ 29880070Smsmith if (u3 > u1) { 29980070Smsmith if ((u2 < u1) || (u2 > u3)) 30080070Smsmith device_printf(acpi_timer_dev, "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n", 30180070Smsmith u1, u2, u3); 30280070Smsmith } 30380070Smsmith u1 = u2; 30480070Smsmith u2 = u3; 30580070Smsmith u3 = TIMER_READ; 30680070Smsmith } 30780070Smsmith} 30880070Smsmith 309