xref: /netbsd-src/sys/dev/acpi/acpi_timer.c (revision c211f59c2010d61077eca065bef1375080f5064a)
1*c211f59cSjmcneill /* $NetBSD: acpi_timer.c,v 1.27 2021/07/25 01:43:08 jmcneill Exp $ */
2eb8716a0Sjruoho 
3eb8716a0Sjruoho /*-
4eb8716a0Sjruoho  * Copyright (c) 2006 Matthias Drochner <drochner@NetBSD.org>
5eb8716a0Sjruoho  * All rights reserved.
6eb8716a0Sjruoho  *
7eb8716a0Sjruoho  * Redistribution and use in source and binary forms, with or without
8eb8716a0Sjruoho  * modification, are permitted provided that the following conditions
9eb8716a0Sjruoho  * are met:
10eb8716a0Sjruoho  * 1. Redistributions of source code must retain the above copyright
11eb8716a0Sjruoho  *    notice, this list of conditions and the following disclaimer.
12eb8716a0Sjruoho  * 2. Redistributions in binary form must reproduce the above copyright
13eb8716a0Sjruoho  *    notice, this list of conditions and the following disclaimer in the
14eb8716a0Sjruoho  *    documentation and/or other materials provided with the distribution.
15eb8716a0Sjruoho  *
16eb8716a0Sjruoho  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17eb8716a0Sjruoho  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18eb8716a0Sjruoho  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19eb8716a0Sjruoho  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20eb8716a0Sjruoho  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21eb8716a0Sjruoho  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22eb8716a0Sjruoho  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23eb8716a0Sjruoho  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24eb8716a0Sjruoho  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25eb8716a0Sjruoho  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26eb8716a0Sjruoho  * POSSIBILITY OF SUCH DAMAGE.
27eb8716a0Sjruoho  */
288ad8d840Sxtraeme 
298ad8d840Sxtraeme #include <sys/cdefs.h>
30*c211f59cSjmcneill __KERNEL_RCSID(0, "$NetBSD: acpi_timer.c,v 1.27 2021/07/25 01:43:08 jmcneill Exp $");
31c14d23acSdrochner 
32c14d23acSdrochner #include <sys/types.h>
33c14d23acSdrochner #include <sys/systm.h>
34c14d23acSdrochner #include <sys/time.h>
35c14d23acSdrochner #include <sys/timetc.h>
365a425210Sjruoho 
375a425210Sjruoho #include <dev/acpi/acpivar.h>
383615cf77Sjoerg #include <dev/acpi/acpi_timer.h>
395a425210Sjruoho 
408cf7d77cSjmcneill #include <machine/acpi_machdep.h>
41c14d23acSdrochner 
42cbdb37a1Sjmcneill #if (!ACPI_REDUCED_HARDWARE)
433ce3b9b4Sdrochner static int	acpitimer_test(void);
44c14d23acSdrochner 
45c14d23acSdrochner static struct timecounter acpi_timecounter = {
46482eef70Srin 	.tc_get_timecount = acpitimer_read_safe,
47482eef70Srin 	.tc_counter_mask = 0x00ffffff,
48482eef70Srin 	.tc_frequency = ACPI_PM_TIMER_FREQUENCY,
49482eef70Srin 	.tc_name = "ACPI-Safe",
50482eef70Srin 	.tc_quality = 900,
51c14d23acSdrochner };
52c14d23acSdrochner 
5334875336Sjmcneill static bool
acpitimer_supported(void)5434875336Sjmcneill acpitimer_supported(void)
5534875336Sjmcneill {
5634875336Sjmcneill 	return AcpiGbl_FADT.PmTimerLength != 0;
5734875336Sjmcneill }
58bc5cf562Sjmcneill #endif
5934875336Sjmcneill 
60c14d23acSdrochner int
acpitimer_init(struct acpi_softc * sc)61bd772854Sjruoho acpitimer_init(struct acpi_softc *sc)
62c14d23acSdrochner {
63cbdb37a1Sjmcneill #if (!ACPI_REDUCED_HARDWARE)
64*c211f59cSjmcneill 	ACPI_TABLE_WAET *waet;
659a11653aSjruoho 	ACPI_STATUS rv;
66c14d23acSdrochner 	uint32_t bits;
673ce3b9b4Sdrochner 	int i, j;
68c14d23acSdrochner 
6934875336Sjmcneill 	if (!acpitimer_supported())
7034875336Sjmcneill 		return -1;
7134875336Sjmcneill 
729a11653aSjruoho 	rv = AcpiGetTimerResolution(&bits);
73bd772854Sjruoho 
749a11653aSjruoho 	if (ACPI_FAILURE(rv))
754d8f91f9Sjruoho 		return -1;
76c14d23acSdrochner 
77c14d23acSdrochner 	if (bits == 32)
78c14d23acSdrochner 		acpi_timecounter.tc_counter_mask = 0xffffffff;
79c14d23acSdrochner 
80bd772854Sjruoho 	for (i = j = 0; i < 10; i++)
813ce3b9b4Sdrochner 		j += acpitimer_test();
821f14228eSxtraeme 
83*c211f59cSjmcneill 	rv = AcpiGetTable(ACPI_SIG_WAET, 0, (ACPI_TABLE_HEADER **)&waet);
84*c211f59cSjmcneill 	if (ACPI_SUCCESS(rv)) {
85*c211f59cSjmcneill 		/*
86*c211f59cSjmcneill 		 * Windows ACPI Emulated Devices Table (WAET) has a hint
87*c211f59cSjmcneill 		 * to let the OS know that a single read of the PM timer
88*c211f59cSjmcneill 		 * provides a reliable value.
89*c211f59cSjmcneill 		 */
90*c211f59cSjmcneill 		if ((waet->Flags & ACPI_WAET_TIMER_ONE_READ) != 0) {
91*c211f59cSjmcneill 			j += 10;
92*c211f59cSjmcneill 		}
93*c211f59cSjmcneill 	}
94*c211f59cSjmcneill 
953ce3b9b4Sdrochner 	if (j >= 10) {
963ce3b9b4Sdrochner 		acpi_timecounter.tc_name = "ACPI-Fast";
973ce3b9b4Sdrochner 		acpi_timecounter.tc_get_timecount = acpitimer_read_fast;
983ce3b9b4Sdrochner 		acpi_timecounter.tc_quality = 1000;
993ce3b9b4Sdrochner 	}
1003ce3b9b4Sdrochner 
1013ce3b9b4Sdrochner 	tc_init(&acpi_timecounter);
102c14d23acSdrochner 
1039a11653aSjruoho 	aprint_debug_dev(sc->sc_dev, "%s %d-bit timer\n",
1049a11653aSjruoho 	    acpi_timecounter.tc_name, bits);
1059a11653aSjruoho 
106bd772854Sjruoho 	return 0;
107bc5cf562Sjmcneill #else
108bc5cf562Sjmcneill 	return -1;
109bc5cf562Sjmcneill #endif
110c14d23acSdrochner }
111c14d23acSdrochner 
1125b0576e7Sdyoung int
acpitimer_detach(void)1135b0576e7Sdyoung acpitimer_detach(void)
1145b0576e7Sdyoung {
115cbdb37a1Sjmcneill #if (!ACPI_REDUCED_HARDWARE)
11634875336Sjmcneill 	if (!acpitimer_supported())
11734875336Sjmcneill 		return -1;
11834875336Sjmcneill 
1195b0576e7Sdyoung 	return tc_detach(&acpi_timecounter);
120bc5cf562Sjmcneill #else
121bc5cf562Sjmcneill 	return -1;
122bc5cf562Sjmcneill #endif
1235b0576e7Sdyoung }
1245b0576e7Sdyoung 
125cbdb37a1Sjmcneill #if (!ACPI_REDUCED_HARDWARE)
126bd772854Sjruoho u_int
acpitimer_read_fast(struct timecounter * tc)127168cd830Schristos acpitimer_read_fast(struct timecounter *tc)
1283ce3b9b4Sdrochner {
1293ce3b9b4Sdrochner 	uint32_t t;
1303ce3b9b4Sdrochner 
131bd772854Sjruoho 	(void)AcpiGetTimer(&t);
132bd772854Sjruoho 
133bd772854Sjruoho 	return t;
1343ce3b9b4Sdrochner }
1353ce3b9b4Sdrochner 
136c14d23acSdrochner /*
1374d8f91f9Sjruoho  * Some chipsets (PIIX4 variants) do not latch correctly;
1384d8f91f9Sjruoho  * there is a chance that a transition is hit.
139c14d23acSdrochner  */
140bd772854Sjruoho u_int
acpitimer_read_safe(struct timecounter * tc)141168cd830Schristos acpitimer_read_safe(struct timecounter *tc)
142c14d23acSdrochner {
143c14d23acSdrochner 	uint32_t t1, t2, t3;
144c14d23acSdrochner 
145bd772854Sjruoho 	(void)AcpiGetTimer(&t2);
146bd772854Sjruoho 	(void)AcpiGetTimer(&t3);
147bd772854Sjruoho 
148c14d23acSdrochner 	do {
149c14d23acSdrochner 		t1 = t2;
150c14d23acSdrochner 		t2 = t3;
151bd772854Sjruoho 
152bd772854Sjruoho 		(void)AcpiGetTimer(&t3);
153bd772854Sjruoho 
154c14d23acSdrochner 	} while ((t1 > t2) || (t2 > t3));
155bd772854Sjruoho 
156bd772854Sjruoho 	return t2;
157c14d23acSdrochner }
158c14d23acSdrochner 
1594d8f91f9Sjruoho uint32_t
acpitimer_delta(uint32_t end,uint32_t start)1603ce3b9b4Sdrochner acpitimer_delta(uint32_t end, uint32_t start)
1613ce3b9b4Sdrochner {
162bd772854Sjruoho 	const u_int mask = acpi_timecounter.tc_counter_mask;
1633ce3b9b4Sdrochner 	uint32_t delta;
1643ce3b9b4Sdrochner 
1653ce3b9b4Sdrochner 	if (end >= start)
1663ce3b9b4Sdrochner 		delta = end - start;
1673ce3b9b4Sdrochner 	else
1683ce3b9b4Sdrochner 		delta = ((mask - start) + end + 1) & mask;
1693ce3b9b4Sdrochner 
170bd772854Sjruoho 	return delta;
1713ce3b9b4Sdrochner }
1723ce3b9b4Sdrochner 
1733ce3b9b4Sdrochner #define N 2000
174bd772854Sjruoho 
1753ce3b9b4Sdrochner static int
acpitimer_test(void)176f7d20361Scegger acpitimer_test(void)
1773ce3b9b4Sdrochner {
1783ce3b9b4Sdrochner 	uint32_t last, this, delta;
1793ce3b9b4Sdrochner 	int minl, maxl, n;
1803ce3b9b4Sdrochner 
1813ce3b9b4Sdrochner 	minl = 10000000;
1823ce3b9b4Sdrochner 	maxl = 0;
1833ce3b9b4Sdrochner 
1848cf7d77cSjmcneill 	acpi_md_OsDisableInterrupt();
185bd772854Sjruoho 
186bd772854Sjruoho 	(void)AcpiGetTimer(&last);
187bd772854Sjruoho 
1883ce3b9b4Sdrochner 	for (n = 0; n < N; n++) {
189bd772854Sjruoho 
190bd772854Sjruoho 		(void)AcpiGetTimer(&this);
191bd772854Sjruoho 
1923ce3b9b4Sdrochner 		delta = acpitimer_delta(this, last);
193bd772854Sjruoho 
1943ce3b9b4Sdrochner 		if (delta > maxl)
1953ce3b9b4Sdrochner 			maxl = delta;
1963ce3b9b4Sdrochner 		else if (delta < minl)
1973ce3b9b4Sdrochner 			minl = delta;
198bd772854Sjruoho 
1993ce3b9b4Sdrochner 		last = this;
2003ce3b9b4Sdrochner 	}
201bd772854Sjruoho 
2028cf7d77cSjmcneill 	acpi_md_OsEnableInterrupt();
2033ce3b9b4Sdrochner 
2043ce3b9b4Sdrochner 	if (maxl - minl > 2 )
2053ce3b9b4Sdrochner 		n = 0;
2063ce3b9b4Sdrochner 	else if (minl < 0 || maxl == 0)
2073ce3b9b4Sdrochner 		n = 0;
2083ce3b9b4Sdrochner 	else
2093ce3b9b4Sdrochner 		n = 1;
2103ce3b9b4Sdrochner 
211bd772854Sjruoho 	return n;
2123ce3b9b4Sdrochner }
213bc5cf562Sjmcneill #endif
214