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