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