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.h> 37787fa5b8SMike Smith #include <machine/resource.h> 38787fa5b8SMike Smith #include <sys/rman.h> 3915e32d5dSMike Smith 4015e32d5dSMike Smith #include "acpi.h" 4187e5d361SJohn Baldwin #include <dev/acpica/acpivar.h> 42cace7a2aSWarner Losh #include <dev/pci/pcivar.h> 43787fa5b8SMike Smith 44787fa5b8SMike Smith /* 45787fa5b8SMike Smith * A timecounter based on the free-running ACPI timer. 46787fa5b8SMike Smith * 47787fa5b8SMike Smith * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>. 48787fa5b8SMike Smith */ 4915e32d5dSMike Smith 50be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 513184cf5aSNate Lawson #define _COMPONENT ACPI_TIMER 5272e5754cSMike Smith ACPI_MODULE_NAME("TIMER") 530ae55423SMike Smith 54787fa5b8SMike Smith static device_t acpi_timer_dev; 55787fa5b8SMike Smith struct resource *acpi_timer_reg; 5615e32d5dSMike Smith 57787fa5b8SMike Smith static u_int acpi_timer_frequency = 14318182 / 4; 5815e32d5dSMike Smith 5915e32d5dSMike Smith static void acpi_timer_identify(driver_t *driver, device_t parent); 6015e32d5dSMike Smith static int acpi_timer_probe(device_t dev); 6115e32d5dSMike Smith static int acpi_timer_attach(device_t dev); 6275988358SNate Lawson static u_int acpi_timer_get_timecount(struct timecounter *tc); 6375988358SNate Lawson static u_int acpi_timer_get_timecount_safe(struct timecounter *tc); 64787fa5b8SMike Smith static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS); 65787fa5b8SMike Smith static void acpi_timer_test(void); 6615e32d5dSMike Smith 6775988358SNate Lawson static u_int read_counter(void); 68214475d8SMarcel Moolenaar static int test_counter(void); 69214475d8SMarcel Moolenaar 7015e32d5dSMike Smith static device_method_t acpi_timer_methods[] = { 7115e32d5dSMike Smith DEVMETHOD(device_identify, acpi_timer_identify), 7215e32d5dSMike Smith DEVMETHOD(device_probe, acpi_timer_probe), 7315e32d5dSMike Smith DEVMETHOD(device_attach, acpi_timer_attach), 7415e32d5dSMike Smith 7515e32d5dSMike Smith {0, 0} 7615e32d5dSMike Smith }; 7715e32d5dSMike Smith 7815e32d5dSMike Smith static driver_t acpi_timer_driver = { 7915e32d5dSMike Smith "acpi_timer", 8015e32d5dSMike Smith acpi_timer_methods, 81787fa5b8SMike Smith 0, 8215e32d5dSMike Smith }; 8315e32d5dSMike Smith 843273b005SMike Smith static devclass_t acpi_timer_devclass; 8515e32d5dSMike Smith DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0); 8664278df5SNate Lawson MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1); 8715e32d5dSMike Smith 88787fa5b8SMike Smith static struct timecounter acpi_timer_timecounter = { 8975988358SNate Lawson .tc_get_timecount = acpi_timer_get_timecount_safe, 9075988358SNate Lawson .tc_poll_pps = 0, 9175988358SNate Lawson .tc_counter_mask = 0, 9275988358SNate Lawson .tc_frequency = 0, 9375988358SNate Lawson .tc_name = "ACPI", 9475988358SNate Lawson .tc_quality = 1000 95787fa5b8SMike Smith }; 96787fa5b8SMike Smith 9775988358SNate Lawson static u_int 98214475d8SMarcel Moolenaar read_counter() 99214475d8SMarcel Moolenaar { 100214475d8SMarcel Moolenaar bus_space_handle_t bsh; 101214475d8SMarcel Moolenaar bus_space_tag_t bst; 10275988358SNate Lawson uint32_t tv; 103214475d8SMarcel Moolenaar 104214475d8SMarcel Moolenaar bsh = rman_get_bushandle(acpi_timer_reg); 105214475d8SMarcel Moolenaar bst = rman_get_bustag(acpi_timer_reg); 106214475d8SMarcel Moolenaar tv = bus_space_read_4(bst, bsh, 0); 107214475d8SMarcel Moolenaar bus_space_barrier(bst, bsh, 0, 4, BUS_SPACE_BARRIER_READ); 108be2b1797SNate Lawson 109214475d8SMarcel Moolenaar return (tv); 110214475d8SMarcel Moolenaar } 111cb877d00SPoul-Henning Kamp 112cb877d00SPoul-Henning Kamp #define N 2000 113cb877d00SPoul-Henning Kamp static int 114cb877d00SPoul-Henning Kamp test_counter() 115cb877d00SPoul-Henning Kamp { 11675988358SNate Lawson uint32_t last, this; 117cb877d00SPoul-Henning Kamp int min, max, n, delta; 118cb877d00SPoul-Henning Kamp 119cb877d00SPoul-Henning Kamp min = 10000000; 120cb877d00SPoul-Henning Kamp max = 0; 121214475d8SMarcel Moolenaar last = read_counter(); 122cb877d00SPoul-Henning Kamp for (n = 0; n < N; n++) { 123214475d8SMarcel Moolenaar this = read_counter(); 12475988358SNate Lawson delta = acpi_TimerDelta(this, last); 125cb877d00SPoul-Henning Kamp if (delta > max) 126cb877d00SPoul-Henning Kamp max = delta; 127cb877d00SPoul-Henning Kamp else if (delta < min) 128cb877d00SPoul-Henning Kamp min = delta; 129cb877d00SPoul-Henning Kamp last = this; 130cb877d00SPoul-Henning Kamp } 131cb877d00SPoul-Henning Kamp if (max - min > 2) 132cb877d00SPoul-Henning Kamp n = 0; 133214475d8SMarcel Moolenaar else if (min < 0 || max == 0) 134cb877d00SPoul-Henning Kamp n = 0; 135cb877d00SPoul-Henning Kamp else 136cb877d00SPoul-Henning Kamp n = 1; 137be2b1797SNate Lawson if (bootverbose) { 138cb877d00SPoul-Henning Kamp printf("ACPI timer looks %s min = %d, max = %d, width = %d\n", 139cb877d00SPoul-Henning Kamp n ? "GOOD" : "BAD ", 140214475d8SMarcel Moolenaar min, max, max - min); 141be2b1797SNate Lawson } 142be2b1797SNate Lawson 143cb877d00SPoul-Henning Kamp return (n); 144cb877d00SPoul-Henning Kamp } 145be2b1797SNate Lawson #undef N 146787fa5b8SMike Smith 147787fa5b8SMike Smith /* 148787fa5b8SMike Smith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources 149787fa5b8SMike Smith * we will be using. 150787fa5b8SMike Smith */ 15115e32d5dSMike Smith static void 15215e32d5dSMike Smith acpi_timer_identify(driver_t *driver, device_t parent) 15315e32d5dSMike Smith { 15415e32d5dSMike Smith device_t dev; 15515e32d5dSMike Smith char desc[40]; 156214475d8SMarcel Moolenaar u_long rlen, rstart; 157214475d8SMarcel Moolenaar int i, j, rid, rtype; 15815e32d5dSMike Smith 159b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1600ae55423SMike Smith 161be2b1797SNate Lawson if (acpi_disabled("timer") || AcpiGbl_FADT == NULL) 1620ae55423SMike Smith return_VOID; 16315e32d5dSMike Smith 16415e32d5dSMike Smith if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) { 16515e32d5dSMike Smith device_printf(parent, "could not add acpi_timer0\n"); 1660ae55423SMike Smith return_VOID; 16715e32d5dSMike Smith } 168787fa5b8SMike Smith acpi_timer_dev = dev; 169214475d8SMarcel Moolenaar 170787fa5b8SMike Smith rid = 0; 171214475d8SMarcel Moolenaar rlen = AcpiGbl_FADT->PmTmLen; 172214475d8SMarcel Moolenaar rtype = (AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId) 173214475d8SMarcel Moolenaar ? SYS_RES_IOPORT : SYS_RES_MEMORY; 174214475d8SMarcel Moolenaar rstart = AcpiGbl_FADT->XPmTmrBlk.Address; 175214475d8SMarcel Moolenaar bus_set_resource(dev, rtype, rid, rstart, rlen); 1765f96beb9SNate Lawson acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 177214475d8SMarcel Moolenaar if (acpi_timer_reg == NULL) { 178214475d8SMarcel Moolenaar device_printf(dev, "couldn't allocate I/O resource (%s 0x%lx)\n", 179be2b1797SNate Lawson rtype == SYS_RES_IOPORT ? "port" : "mem", rstart); 1800ae55423SMike Smith return_VOID; 18115e32d5dSMike Smith } 18275988358SNate Lawson if (AcpiGbl_FADT->TmrValExt != 0) 18375988358SNate Lawson acpi_timer_timecounter.tc_counter_mask = 0xffffffff; 18475988358SNate Lawson else 18575988358SNate Lawson acpi_timer_timecounter.tc_counter_mask = 0x00ffffff; 18675988358SNate Lawson acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 187d786139cSMaxime Henrion if (testenv("debug.acpi.timer_test")) 188787fa5b8SMike Smith acpi_timer_test(); 189787fa5b8SMike Smith 190cb877d00SPoul-Henning Kamp j = 0; 191cb877d00SPoul-Henning Kamp for (i = 0; i < 10; i++) 192cb877d00SPoul-Henning Kamp j += test_counter(); 193cb877d00SPoul-Henning Kamp if (j == 10) { 194cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_name = "ACPI-fast"; 195cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount; 196cb877d00SPoul-Henning Kamp } else { 197cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_name = "ACPI-safe"; 198cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe; 199cb877d00SPoul-Henning Kamp } 200787fa5b8SMike Smith tc_init(&acpi_timer_timecounter); 20115e32d5dSMike Smith 202be2b1797SNate Lawson sprintf(desc, "%d-bit timer at 3.579545MHz", 203be2b1797SNate Lawson AcpiGbl_FADT->TmrValExt ? 32 : 24); 20415e32d5dSMike Smith device_set_desc_copy(dev, desc); 2050ae55423SMike Smith 2060ae55423SMike Smith return_VOID; 20715e32d5dSMike Smith } 20815e32d5dSMike Smith 20915e32d5dSMike Smith static int 21015e32d5dSMike Smith acpi_timer_probe(device_t dev) 21115e32d5dSMike Smith { 212787fa5b8SMike Smith if (dev == acpi_timer_dev) 21315e32d5dSMike Smith return (0); 214be2b1797SNate Lawson 21515e32d5dSMike Smith return (ENXIO); 21615e32d5dSMike Smith } 21715e32d5dSMike Smith 21815e32d5dSMike Smith static int 21915e32d5dSMike Smith acpi_timer_attach(device_t dev) 22015e32d5dSMike Smith { 221787fa5b8SMike Smith return (0); 22215e32d5dSMike Smith } 223787fa5b8SMike Smith 224787fa5b8SMike Smith /* 225feade919SMike Smith * Fetch current time value from reliable hardware. 226787fa5b8SMike Smith */ 22775988358SNate Lawson static u_int 228787fa5b8SMike Smith acpi_timer_get_timecount(struct timecounter *tc) 229787fa5b8SMike Smith { 230214475d8SMarcel Moolenaar return (read_counter()); 231787fa5b8SMike Smith } 232787fa5b8SMike Smith 233787fa5b8SMike Smith /* 234feade919SMike Smith * Fetch current time value from hardware that may not correctly 235feade919SMike Smith * latch the counter. 236feade919SMike Smith */ 23775988358SNate Lawson static u_int 238feade919SMike Smith acpi_timer_get_timecount_safe(struct timecounter *tc) 239feade919SMike Smith { 24075988358SNate Lawson u_int u1, u2, u3; 241feade919SMike Smith 242214475d8SMarcel Moolenaar u2 = read_counter(); 243214475d8SMarcel Moolenaar u3 = read_counter(); 244feade919SMike Smith do { 245feade919SMike Smith u1 = u2; 246feade919SMike Smith u2 = u3; 247214475d8SMarcel Moolenaar u3 = read_counter(); 248be2b1797SNate Lawson } while (u1 > u2 || u2 > u3 || u3 - u1 > 15); 249be2b1797SNate Lawson 250feade919SMike Smith return (u2); 251feade919SMike Smith } 252feade919SMike Smith 253feade919SMike Smith /* 254787fa5b8SMike Smith * Timecounter freqency adjustment interface. 255787fa5b8SMike Smith */ 256787fa5b8SMike Smith static int 257787fa5b8SMike Smith acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS) 258787fa5b8SMike Smith { 259787fa5b8SMike Smith int error; 260787fa5b8SMike Smith u_int freq; 261787fa5b8SMike Smith 262787fa5b8SMike Smith if (acpi_timer_timecounter.tc_frequency == 0) 263787fa5b8SMike Smith return (EOPNOTSUPP); 264787fa5b8SMike Smith freq = acpi_timer_frequency; 265787fa5b8SMike Smith error = sysctl_handle_int(oidp, &freq, sizeof(freq), req); 266787fa5b8SMike Smith if (error == 0 && req->newptr != NULL) { 267787fa5b8SMike Smith acpi_timer_frequency = freq; 268787fa5b8SMike Smith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 269787fa5b8SMike Smith } 270be2b1797SNate Lawson 271787fa5b8SMike Smith return (error); 272787fa5b8SMike Smith } 273787fa5b8SMike Smith 274787fa5b8SMike Smith SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW, 275787fa5b8SMike Smith 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", ""); 276787fa5b8SMike Smith 277787fa5b8SMike Smith /* 278787fa5b8SMike Smith * Test harness for verifying ACPI timer behaviour. 279787fa5b8SMike Smith * Boot with debug.acpi.timer_test set to invoke this. 280787fa5b8SMike Smith */ 281787fa5b8SMike Smith static void 282787fa5b8SMike Smith acpi_timer_test(void) 283787fa5b8SMike Smith { 28475988358SNate Lawson uint32_t u1, u2, u3; 285787fa5b8SMike Smith 286214475d8SMarcel Moolenaar u1 = read_counter(); 287214475d8SMarcel Moolenaar u2 = read_counter(); 288214475d8SMarcel Moolenaar u3 = read_counter(); 289787fa5b8SMike Smith 290787fa5b8SMike Smith device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n"); 291787fa5b8SMike Smith for (;;) { 292787fa5b8SMike Smith /* 293be2b1797SNate Lawson * The failure case is where u3 > u1, but u2 does not fall between 294be2b1797SNate Lawson * the two, ie. it contains garbage. 295787fa5b8SMike Smith */ 296787fa5b8SMike Smith if (u3 > u1) { 297be2b1797SNate Lawson if (u2 < u1 || u2 > u3) 298be2b1797SNate Lawson device_printf(acpi_timer_dev, 299be2b1797SNate Lawson "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n", 300787fa5b8SMike Smith u1, u2, u3); 301787fa5b8SMike Smith } 302787fa5b8SMike Smith u1 = u2; 303787fa5b8SMike Smith u2 = u3; 304214475d8SMarcel Moolenaar u3 = read_counter(); 305787fa5b8SMike Smith } 306787fa5b8SMike Smith } 307