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