Home | History | Annotate | Line # | Download | only in acpi
acpi_timer.c revision 1.26
      1 /* $NetBSD: acpi_timer.c,v 1.26 2020/05/29 12:30:41 rin Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2006 Matthias Drochner <drochner (at) 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.26 2020/05/29 12:30:41 rin 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 	.tc_get_timecount = acpitimer_read_safe,
     47 	.tc_counter_mask = 0x00ffffff,
     48 	.tc_frequency = ACPI_PM_TIMER_FREQUENCY,
     49 	.tc_name = "ACPI-Safe",
     50 	.tc_quality = 900,
     51 };
     52 
     53 static bool
     54 acpitimer_supported(void)
     55 {
     56 	return AcpiGbl_FADT.PmTimerLength != 0;
     57 }
     58 #endif
     59 
     60 int
     61 acpitimer_init(struct acpi_softc *sc)
     62 {
     63 #if (!ACPI_REDUCED_HARDWARE)
     64 	ACPI_STATUS rv;
     65 	uint32_t bits;
     66 	int i, j;
     67 
     68 	if (!acpitimer_supported())
     69 		return -1;
     70 
     71 	rv = AcpiGetTimerResolution(&bits);
     72 
     73 	if (ACPI_FAILURE(rv))
     74 		return -1;
     75 
     76 	if (bits == 32)
     77 		acpi_timecounter.tc_counter_mask = 0xffffffff;
     78 
     79 	for (i = j = 0; i < 10; i++)
     80 		j += acpitimer_test();
     81 
     82 	if (j >= 10) {
     83 		acpi_timecounter.tc_name = "ACPI-Fast";
     84 		acpi_timecounter.tc_get_timecount = acpitimer_read_fast;
     85 		acpi_timecounter.tc_quality = 1000;
     86 	}
     87 
     88 	tc_init(&acpi_timecounter);
     89 
     90 	aprint_debug_dev(sc->sc_dev, "%s %d-bit timer\n",
     91 	    acpi_timecounter.tc_name, bits);
     92 
     93 	return 0;
     94 #else
     95 	return -1;
     96 #endif
     97 }
     98 
     99 int
    100 acpitimer_detach(void)
    101 {
    102 #if (!ACPI_REDUCED_HARDWARE)
    103 	if (!acpitimer_supported())
    104 		return -1;
    105 
    106 	return tc_detach(&acpi_timecounter);
    107 #else
    108 	return -1;
    109 #endif
    110 }
    111 
    112 #if (!ACPI_REDUCED_HARDWARE)
    113 u_int
    114 acpitimer_read_fast(struct timecounter *tc)
    115 {
    116 	uint32_t t;
    117 
    118 	(void)AcpiGetTimer(&t);
    119 
    120 	return t;
    121 }
    122 
    123 /*
    124  * Some chipsets (PIIX4 variants) do not latch correctly;
    125  * there is a chance that a transition is hit.
    126  */
    127 u_int
    128 acpitimer_read_safe(struct timecounter *tc)
    129 {
    130 	uint32_t t1, t2, t3;
    131 
    132 	(void)AcpiGetTimer(&t2);
    133 	(void)AcpiGetTimer(&t3);
    134 
    135 	do {
    136 		t1 = t2;
    137 		t2 = t3;
    138 
    139 		(void)AcpiGetTimer(&t3);
    140 
    141 	} while ((t1 > t2) || (t2 > t3));
    142 
    143 	return t2;
    144 }
    145 
    146 uint32_t
    147 acpitimer_delta(uint32_t end, uint32_t start)
    148 {
    149 	const u_int mask = acpi_timecounter.tc_counter_mask;
    150 	uint32_t delta;
    151 
    152 	if (end >= start)
    153 		delta = end - start;
    154 	else
    155 		delta = ((mask - start) + end + 1) & mask;
    156 
    157 	return delta;
    158 }
    159 
    160 #define N 2000
    161 
    162 static int
    163 acpitimer_test(void)
    164 {
    165 	uint32_t last, this, delta;
    166 	int minl, maxl, n;
    167 
    168 	minl = 10000000;
    169 	maxl = 0;
    170 
    171 	acpi_md_OsDisableInterrupt();
    172 
    173 	(void)AcpiGetTimer(&last);
    174 
    175 	for (n = 0; n < N; n++) {
    176 
    177 		(void)AcpiGetTimer(&this);
    178 
    179 		delta = acpitimer_delta(this, last);
    180 
    181 		if (delta > maxl)
    182 			maxl = delta;
    183 		else if (delta < minl)
    184 			minl = delta;
    185 
    186 		last = this;
    187 	}
    188 
    189 	acpi_md_OsEnableInterrupt();
    190 
    191 	if (maxl - minl > 2 )
    192 		n = 0;
    193 	else if (minl < 0 || maxl == 0)
    194 		n = 0;
    195 	else
    196 		n = 1;
    197 
    198 	return n;
    199 }
    200 #endif
    201