1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
/* $OpenBSD: acpitimer.c,v 1.16 2022/08/25 17:43:34 cheloha Exp $ */
/*
* Copyright (c) 2005 Thorsten Lockert <tholo@sigmasoft.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/stdint.h>
#include <sys/timetc.h>
#include <machine/bus.h>
#include <machine/cpu.h>
#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
struct acpitimer_softc {
struct device sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
};
int acpitimermatch(struct device *, void *, void *);
void acpitimerattach(struct device *, struct device *, void *);
void acpitimer_delay(int);
u_int acpi_get_timecount(struct timecounter *tc);
uint32_t acpitimer_read(struct acpitimer_softc *);
static struct timecounter acpi_timecounter = {
.tc_get_timecount = acpi_get_timecount,
.tc_poll_pps = 0,
.tc_counter_mask = 0x00ffffff, /* 24 bits */
.tc_frequency = ACPI_FREQUENCY,
.tc_name = 0,
.tc_quality = 1000,
.tc_priv = NULL,
.tc_user = 0,
};
const struct cfattach acpitimer_ca = {
sizeof(struct acpitimer_softc), acpitimermatch, acpitimerattach
};
struct cfdriver acpitimer_cd = {
NULL, "acpitimer", DV_DULL
};
int
acpitimermatch(struct device *parent, void *match, void *aux)
{
struct acpi_attach_args *aa = aux;
struct cfdata *cf = match;
/* sanity */
if (aa->aaa_name == NULL ||
strcmp(aa->aaa_name, cf->cf_driver->cd_name) != 0 ||
aa->aaa_table != NULL)
return (0);
return (1);
}
void
acpitimerattach(struct device *parent, struct device *self, void *aux)
{
struct acpitimer_softc *sc = (struct acpitimer_softc *) self;
struct acpi_softc *psc = (struct acpi_softc *) parent;
int rc;
if (psc->sc_fadt->hdr_revision >= 3 &&
psc->sc_fadt->x_pm_tmr_blk.address != 0)
rc = acpi_map_address(psc, &psc->sc_fadt->x_pm_tmr_blk, 0,
psc->sc_fadt->pm_tmr_len, &sc->sc_ioh, &sc->sc_iot);
else
rc = acpi_map_address(psc, NULL, psc->sc_fadt->pm_tmr_blk,
psc->sc_fadt->pm_tmr_len, &sc->sc_ioh, &sc->sc_iot);
if (rc) {
printf(": can't map i/o space\n");
return;
}
printf(": %d Hz, %d bits\n", ACPI_FREQUENCY,
psc->sc_fadt->flags & FADT_TMR_VAL_EXT ? 32 : 24);
if (psc->sc_fadt->flags & FADT_TMR_VAL_EXT)
acpi_timecounter.tc_counter_mask = 0xffffffffU;
acpi_timecounter.tc_priv = sc;
acpi_timecounter.tc_name = sc->sc_dev.dv_xname;
tc_init(&acpi_timecounter);
delay_init(acpitimer_delay, 1000);
#if defined(__amd64__)
extern void cpu_recalibrate_tsc(struct timecounter *);
cpu_recalibrate_tsc(&acpi_timecounter);
#endif
}
void
acpitimer_delay(int usecs)
{
uint64_t count = 0, cycles;
struct acpitimer_softc *sc = acpi_timecounter.tc_priv;
uint32_t mask = acpi_timecounter.tc_counter_mask;
uint32_t val1, val2;
val2 = acpitimer_read(sc);
cycles = usecs * acpi_timecounter.tc_frequency / 1000000;
while (count < cycles) {
CPU_BUSY_CYCLE();
val1 = val2;
val2 = acpitimer_read(sc);
count += (val2 - val1) & mask;
}
}
u_int
acpi_get_timecount(struct timecounter *tc)
{
return acpitimer_read(tc->tc_priv);
}
uint32_t
acpitimer_read(struct acpitimer_softc *sc)
{
uint32_t u1, u2, u3;
u2 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0);
u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0);
do {
u1 = u2;
u2 = u3;
u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0);
} while (u1 > u2 || u2 > u3);
return (u2);
}
|