1 /* $NetBSD: acpi_timer.c,v 1.16 2010/03/05 17:04:26 jruoho 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.16 2010/03/05 17:04:26 jruoho 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 static int acpitimer_test(void); 43 static uint32_t acpitimer_delta(uint32_t, uint32_t); 44 static u_int acpitimer_read_safe(struct timecounter *); 45 static u_int acpitimer_read_fast(struct timecounter *); 46 47 static struct timecounter acpi_timecounter = { 48 acpitimer_read_safe, 49 0, 50 0x00ffffff, 51 PM_TIMER_FREQUENCY, 52 "ACPI-Safe", 53 900, 54 NULL, 55 NULL, 56 }; 57 58 int 59 acpitimer_init(void) 60 { 61 uint32_t bits; 62 int i, j; 63 ACPI_STATUS res; 64 65 res = AcpiGetTimerResolution(&bits); 66 if (res != AE_OK) 67 return (-1); 68 69 if (bits == 32) 70 acpi_timecounter.tc_counter_mask = 0xffffffff; 71 72 j = 0; 73 for (i = 0; i < 10; i++) 74 j += acpitimer_test(); 75 76 if (j >= 10) { 77 acpi_timecounter.tc_name = "ACPI-Fast"; 78 acpi_timecounter.tc_get_timecount = acpitimer_read_fast; 79 acpi_timecounter.tc_quality = 1000; 80 } 81 82 tc_init(&acpi_timecounter); 83 aprint_verbose("%s %d-bit timer\n", acpi_timecounter.tc_name, bits); 84 85 return (0); 86 } 87 88 int 89 acpitimer_detach(void) 90 { 91 return tc_detach(&acpi_timecounter); 92 } 93 94 static u_int 95 acpitimer_read_fast(struct timecounter *tc) 96 { 97 uint32_t t; 98 99 AcpiGetTimer(&t); 100 return (t); 101 } 102 103 /* 104 * Some chipsets (PIIX4 variants) do not latch correctly; there 105 * is a chance that a transition is hit. 106 */ 107 static u_int 108 acpitimer_read_safe(struct timecounter *tc) 109 { 110 uint32_t t1, t2, t3; 111 112 AcpiGetTimer(&t2); 113 AcpiGetTimer(&t3); 114 do { 115 t1 = t2; 116 t2 = t3; 117 AcpiGetTimer(&t3); 118 } while ((t1 > t2) || (t2 > t3)); 119 return (t2); 120 } 121 122 static uint32_t 123 acpitimer_delta(uint32_t end, uint32_t start) 124 { 125 uint32_t delta; 126 u_int mask = acpi_timecounter.tc_counter_mask; 127 128 if (end >= start) 129 delta = end - start; 130 else 131 delta = ((mask - start) + end + 1) & mask; 132 133 return (delta); 134 } 135 136 #define N 2000 137 static int 138 acpitimer_test(void) 139 { 140 uint32_t last, this, delta; 141 int minl, maxl, n; 142 143 minl = 10000000; 144 maxl = 0; 145 146 acpi_md_OsDisableInterrupt(); 147 AcpiGetTimer(&last); 148 for (n = 0; n < N; n++) { 149 AcpiGetTimer(&this); 150 delta = acpitimer_delta(this, last); 151 if (delta > maxl) 152 maxl = delta; 153 else if (delta < minl) 154 minl = delta; 155 last = this; 156 } 157 acpi_md_OsEnableInterrupt(); 158 159 if (maxl - minl > 2 ) 160 n = 0; 161 else if (minl < 0 || maxl == 0) 162 n = 0; 163 else 164 n = 1; 165 166 return (n); 167 } 168