1 /* $NetBSD: acpi_timer.c,v 1.25 2018/10/16 10:25:33 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2006 Matthias Drochner <drochner@NetBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: acpi_timer.c,v 1.25 2018/10/16 10:25:33 jmcneill Exp $"); 31 32 #include <sys/types.h> 33 #include <sys/systm.h> 34 #include <sys/time.h> 35 #include <sys/timetc.h> 36 37 #include <dev/acpi/acpivar.h> 38 #include <dev/acpi/acpi_timer.h> 39 40 #include <machine/acpi_machdep.h> 41 42 #if (!ACPI_REDUCED_HARDWARE) 43 static int acpitimer_test(void); 44 45 static struct timecounter acpi_timecounter = { 46 acpitimer_read_safe, 47 0, 48 0x00ffffff, 49 ACPI_PM_TIMER_FREQUENCY, 50 "ACPI-Safe", 51 900, 52 NULL, 53 NULL, 54 }; 55 56 static bool 57 acpitimer_supported(void) 58 { 59 return AcpiGbl_FADT.PmTimerLength != 0; 60 } 61 #endif 62 63 int 64 acpitimer_init(struct acpi_softc *sc) 65 { 66 #if (!ACPI_REDUCED_HARDWARE) 67 ACPI_STATUS rv; 68 uint32_t bits; 69 int i, j; 70 71 if (!acpitimer_supported()) 72 return -1; 73 74 rv = AcpiGetTimerResolution(&bits); 75 76 if (ACPI_FAILURE(rv)) 77 return -1; 78 79 if (bits == 32) 80 acpi_timecounter.tc_counter_mask = 0xffffffff; 81 82 for (i = j = 0; i < 10; i++) 83 j += acpitimer_test(); 84 85 if (j >= 10) { 86 acpi_timecounter.tc_name = "ACPI-Fast"; 87 acpi_timecounter.tc_get_timecount = acpitimer_read_fast; 88 acpi_timecounter.tc_quality = 1000; 89 } 90 91 tc_init(&acpi_timecounter); 92 93 aprint_debug_dev(sc->sc_dev, "%s %d-bit timer\n", 94 acpi_timecounter.tc_name, bits); 95 96 return 0; 97 #else 98 return -1; 99 #endif 100 } 101 102 int 103 acpitimer_detach(void) 104 { 105 #if (!ACPI_REDUCED_HARDWARE) 106 if (!acpitimer_supported()) 107 return -1; 108 109 return tc_detach(&acpi_timecounter); 110 #else 111 return -1; 112 #endif 113 } 114 115 #if (!ACPI_REDUCED_HARDWARE) 116 u_int 117 acpitimer_read_fast(struct timecounter *tc) 118 { 119 uint32_t t; 120 121 (void)AcpiGetTimer(&t); 122 123 return t; 124 } 125 126 /* 127 * Some chipsets (PIIX4 variants) do not latch correctly; 128 * there is a chance that a transition is hit. 129 */ 130 u_int 131 acpitimer_read_safe(struct timecounter *tc) 132 { 133 uint32_t t1, t2, t3; 134 135 (void)AcpiGetTimer(&t2); 136 (void)AcpiGetTimer(&t3); 137 138 do { 139 t1 = t2; 140 t2 = t3; 141 142 (void)AcpiGetTimer(&t3); 143 144 } while ((t1 > t2) || (t2 > t3)); 145 146 return t2; 147 } 148 149 uint32_t 150 acpitimer_delta(uint32_t end, uint32_t start) 151 { 152 const u_int mask = acpi_timecounter.tc_counter_mask; 153 uint32_t delta; 154 155 if (end >= start) 156 delta = end - start; 157 else 158 delta = ((mask - start) + end + 1) & mask; 159 160 return delta; 161 } 162 163 #define N 2000 164 165 static int 166 acpitimer_test(void) 167 { 168 uint32_t last, this, delta; 169 int minl, maxl, n; 170 171 minl = 10000000; 172 maxl = 0; 173 174 acpi_md_OsDisableInterrupt(); 175 176 (void)AcpiGetTimer(&last); 177 178 for (n = 0; n < N; n++) { 179 180 (void)AcpiGetTimer(&this); 181 182 delta = acpitimer_delta(this, last); 183 184 if (delta > maxl) 185 maxl = delta; 186 else if (delta < minl) 187 minl = delta; 188 189 last = this; 190 } 191 192 acpi_md_OsEnableInterrupt(); 193 194 if (maxl - minl > 2 ) 195 n = 0; 196 else if (minl < 0 || maxl == 0) 197 n = 0; 198 else 199 n = 1; 200 201 return n; 202 } 203 #endif 204