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