xref: /openbsd/sys/dev/acpi/acpihpet.c (revision 24ee467d)
1 /* $OpenBSD: acpihpet.c,v 1.31 2023/02/04 19:19:37 cheloha 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 #include <dev/acpi/acpidev.h>
30 
31 int acpihpet_attached;
32 
33 int acpihpet_match(struct device *, void *, void *);
34 void acpihpet_attach(struct device *, struct device *, void *);
35 int acpihpet_activate(struct device *, int);
36 void acpihpet_delay(int);
37 u_int acpihpet_gettime(struct timecounter *tc);
38 
39 uint64_t	acpihpet_r(bus_space_tag_t _iot, bus_space_handle_t _ioh,
40 		    bus_size_t _ioa);
41 void		acpihpet_w(bus_space_tag_t _iot, bus_space_handle_t _ioh,
42 		    bus_size_t _ioa, uint64_t _val);
43 
44 static struct timecounter hpet_timecounter = {
45 	.tc_get_timecount = acpihpet_gettime,
46 	.tc_counter_mask = 0xffffffff,
47 	.tc_frequency = 0,
48 	.tc_name = 0,
49 	.tc_quality = 1000,
50 	.tc_priv = NULL,
51 	.tc_user = 0,
52 };
53 
54 #define HPET_TIMERS	3
55 struct hpet_regs {
56 	uint64_t	configuration;
57 	uint64_t	interrupt_status;
58 	uint64_t	main_counter;
59 	struct {	/* timers */
60 		uint64_t config;
61 		uint64_t compare;
62 		uint64_t interrupt;
63 	} timers[HPET_TIMERS];
64 };
65 
66 struct acpihpet_softc {
67 	struct device		sc_dev;
68 
69 	bus_space_tag_t		sc_iot;
70 	bus_space_handle_t	sc_ioh;
71 
72 	uint32_t		sc_conf;
73 	struct hpet_regs	sc_save;
74 };
75 
76 const struct cfattach acpihpet_ca = {
77 	sizeof(struct acpihpet_softc), acpihpet_match, acpihpet_attach,
78 	NULL, acpihpet_activate
79 };
80 
81 struct cfdriver acpihpet_cd = {
82 	NULL, "acpihpet", DV_DULL
83 };
84 
85 uint64_t
acpihpet_r(bus_space_tag_t iot,bus_space_handle_t ioh,bus_size_t ioa)86 acpihpet_r(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t ioa)
87 {
88 	uint64_t val;
89 
90 	val = bus_space_read_4(iot, ioh, ioa + 4);
91 	val = val << 32;
92 	val |= bus_space_read_4(iot, ioh, ioa);
93 	return (val);
94 }
95 
96 void
acpihpet_w(bus_space_tag_t iot,bus_space_handle_t ioh,bus_size_t ioa,uint64_t val)97 acpihpet_w(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t ioa,
98     uint64_t val)
99 {
100 	bus_space_write_4(iot, ioh, ioa + 4, val >> 32);
101 	bus_space_write_4(iot, ioh, ioa, val & 0xffffffff);
102 }
103 
104 int
acpihpet_activate(struct device * self,int act)105 acpihpet_activate(struct device *self, int act)
106 {
107 	struct acpihpet_softc *sc = (struct acpihpet_softc *) self;
108 
109 	switch (act) {
110 	case DVACT_SUSPEND:
111 		delay_fini(acpihpet_delay);
112 
113 		/* stop, then save */
114 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
115 		    HPET_CONFIGURATION, sc->sc_conf);
116 
117 		sc->sc_save.configuration = acpihpet_r(sc->sc_iot,
118 		    sc->sc_ioh, HPET_CONFIGURATION);
119 		sc->sc_save.interrupt_status = acpihpet_r(sc->sc_iot,
120 		    sc->sc_ioh, HPET_INTERRUPT_STATUS);
121 		sc->sc_save.main_counter = acpihpet_r(sc->sc_iot,
122 		    sc->sc_ioh, HPET_MAIN_COUNTER);
123 		sc->sc_save.timers[0].config = acpihpet_r(sc->sc_iot,
124 		    sc->sc_ioh, HPET_TIMER0_CONFIG);
125 		sc->sc_save.timers[0].interrupt = acpihpet_r(sc->sc_iot,
126 		    sc->sc_ioh, HPET_TIMER0_INTERRUPT);
127 		sc->sc_save.timers[0].compare = acpihpet_r(sc->sc_iot,
128 		    sc->sc_ioh, HPET_TIMER0_COMPARE);
129 		sc->sc_save.timers[1].config = acpihpet_r(sc->sc_iot,
130 		    sc->sc_ioh, HPET_TIMER1_CONFIG);
131 		sc->sc_save.timers[1].interrupt = acpihpet_r(sc->sc_iot,
132 		    sc->sc_ioh, HPET_TIMER1_INTERRUPT);
133 		sc->sc_save.timers[1].compare = acpihpet_r(sc->sc_iot,
134 		    sc->sc_ioh, HPET_TIMER1_COMPARE);
135 		sc->sc_save.timers[2].config = acpihpet_r(sc->sc_iot,
136 		    sc->sc_ioh, HPET_TIMER2_CONFIG);
137 		sc->sc_save.timers[2].interrupt = acpihpet_r(sc->sc_iot,
138 		    sc->sc_ioh, HPET_TIMER2_INTERRUPT);
139 		sc->sc_save.timers[2].compare = acpihpet_r(sc->sc_iot,
140 		    sc->sc_ioh, HPET_TIMER2_COMPARE);
141 		break;
142 	case DVACT_RESUME:
143 		/* stop, restore, then restart */
144 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
145 		    HPET_CONFIGURATION, sc->sc_conf);
146 
147 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
148 		    HPET_CONFIGURATION, sc->sc_save.configuration);
149 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
150 		    HPET_INTERRUPT_STATUS, sc->sc_save.interrupt_status);
151 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
152 		    HPET_MAIN_COUNTER, sc->sc_save.main_counter);
153 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
154 		    HPET_TIMER0_CONFIG, sc->sc_save.timers[0].config);
155 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
156 		    HPET_TIMER0_INTERRUPT, sc->sc_save.timers[0].interrupt);
157 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
158 		    HPET_TIMER0_COMPARE, sc->sc_save.timers[0].compare);
159 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
160 		    HPET_TIMER1_CONFIG, sc->sc_save.timers[1].config);
161 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
162 		    HPET_TIMER1_INTERRUPT, sc->sc_save.timers[1].interrupt);
163 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
164 		    HPET_TIMER1_COMPARE, sc->sc_save.timers[1].compare);
165 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
166 		    HPET_TIMER2_CONFIG, sc->sc_save.timers[2].config);
167 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
168 		    HPET_TIMER2_INTERRUPT, sc->sc_save.timers[2].interrupt);
169 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
170 		    HPET_TIMER2_COMPARE, sc->sc_save.timers[2].compare);
171 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
172 		    HPET_CONFIGURATION, sc->sc_conf | 1);
173 
174 		delay_init(acpihpet_delay, 2000);
175 		break;
176 	}
177 
178 	return 0;
179 }
180 
181 int
acpihpet_match(struct device * parent,void * match,void * aux)182 acpihpet_match(struct device *parent, void *match, void *aux)
183 {
184 	struct acpi_attach_args *aaa = aux;
185 	struct acpi_table_header *hdr;
186 
187 	/*
188 	 * If we do not have a table, it is not us; attach only once
189 	 */
190 	if (acpihpet_attached || aaa->aaa_table == NULL)
191 		return (0);
192 
193 	/*
194 	 * If it is an HPET table, we can attach
195 	 */
196 	hdr = (struct acpi_table_header *)aaa->aaa_table;
197 	if (memcmp(hdr->signature, HPET_SIG, sizeof(HPET_SIG) - 1) != 0)
198 		return (0);
199 
200 	return (1);
201 }
202 
203 void
acpihpet_attach(struct device * parent,struct device * self,void * aux)204 acpihpet_attach(struct device *parent, struct device *self, void *aux)
205 {
206 	struct acpihpet_softc *sc = (struct acpihpet_softc *) self;
207 	struct acpi_softc *psc = (struct acpi_softc *)parent;
208 	struct acpi_attach_args *aaa = aux;
209 	struct acpi_hpet *hpet = (struct acpi_hpet *)aaa->aaa_table;
210 	uint64_t period, freq;	/* timer period in femtoseconds (10^-15) */
211 	uint32_t v1, v2;
212 	int timeout;
213 
214 	if (acpi_map_address(psc, &hpet->base_address, 0, HPET_REG_SIZE,
215 	    &sc->sc_ioh, &sc->sc_iot))	{
216 		printf(": can't map i/o space\n");
217 		return;
218 	}
219 
220 	/*
221 	 * Revisions 0x30 through 0x3a of the AMD SB700, with spread
222 	 * spectrum enabled, have an SMM based HPET emulation that's
223 	 * subtly broken.  The hardware is initialized upon first
224 	 * access of the configuration register.  Initialization takes
225 	 * some time during which the configuration register returns
226 	 * 0xffffffff.
227 	 */
228 	timeout = 1000;
229 	do {
230 		if (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
231 		    HPET_CONFIGURATION) != 0xffffffff)
232 			break;
233 	} while(--timeout > 0);
234 
235 	if (timeout == 0) {
236 		printf(": disabled\n");
237 		return;
238 	}
239 
240 	/* enable hpet */
241 	sc->sc_conf = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
242 	    HPET_CONFIGURATION) & ~1;
243 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, HPET_CONFIGURATION,
244 	    sc->sc_conf | 1);
245 
246 	/* make sure hpet is working */
247 	v1 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, HPET_MAIN_COUNTER);
248 	delay(1);
249 	v2 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, HPET_MAIN_COUNTER);
250 	if (v1 == v2) {
251 		printf(": counter not incrementing\n");
252 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
253 		    HPET_CONFIGURATION, sc->sc_conf);
254 		return;
255 	}
256 
257 	period = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
258 	    HPET_CAPABILITIES + sizeof(uint32_t));
259 
260 	/* Period must be > 0 and less than 100ns (10^8 fs) */
261 	if (period == 0 || period > HPET_MAX_PERIOD) {
262 		printf(": invalid period\n");
263 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
264 		    HPET_CONFIGURATION, sc->sc_conf);
265 		return;
266 	}
267 	freq = 1000000000000000ull / period;
268 	printf(": %lld Hz\n", freq);
269 
270 	hpet_timecounter.tc_frequency = freq;
271 	hpet_timecounter.tc_priv = sc;
272 	hpet_timecounter.tc_name = sc->sc_dev.dv_xname;
273 	tc_init(&hpet_timecounter);
274 
275 	delay_init(acpihpet_delay, 2000);
276 
277 #if defined(__amd64__)
278 	extern void cpu_recalibrate_tsc(struct timecounter *);
279 	cpu_recalibrate_tsc(&hpet_timecounter);
280 #endif
281 	acpihpet_attached++;
282 }
283 
284 void
acpihpet_delay(int usecs)285 acpihpet_delay(int usecs)
286 {
287 	uint64_t count = 0, cycles;
288 	struct acpihpet_softc *sc = hpet_timecounter.tc_priv;
289 	uint32_t val1, val2;
290 
291 	val2 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, HPET_MAIN_COUNTER);
292 	cycles = usecs * hpet_timecounter.tc_frequency / 1000000;
293 	while (count < cycles) {
294 		CPU_BUSY_CYCLE();
295 		val1 = val2;
296 		val2 = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
297 		    HPET_MAIN_COUNTER);
298 		count += val2 - val1;
299 	}
300 }
301 
302 u_int
acpihpet_gettime(struct timecounter * tc)303 acpihpet_gettime(struct timecounter *tc)
304 {
305 	struct acpihpet_softc *sc = tc->tc_priv;
306 
307 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, HPET_MAIN_COUNTER));
308 }
309