xref: /openbsd/sys/dev/acpi/acpihpet.c (revision 24ee467d)
1*24ee467dScheloha /* $OpenBSD: acpihpet.c,v 1.31 2023/02/04 19:19:37 cheloha Exp $ */
2c04efc0bSmarco /*
3c04efc0bSmarco  * Copyright (c) 2005 Thorsten Lockert <tholo@sigmasoft.com>
4c04efc0bSmarco  *
5c04efc0bSmarco  * Permission to use, copy, modify, and distribute this software for any
6c04efc0bSmarco  * purpose with or without fee is hereby granted, provided that the above
7c04efc0bSmarco  * copyright notice and this permission notice appear in all copies.
8c04efc0bSmarco  *
9c04efc0bSmarco  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10c04efc0bSmarco  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11c04efc0bSmarco  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12c04efc0bSmarco  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13c04efc0bSmarco  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14c04efc0bSmarco  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15c04efc0bSmarco  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16c04efc0bSmarco  */
17c04efc0bSmarco 
18c04efc0bSmarco #include <sys/param.h>
19c04efc0bSmarco #include <sys/systm.h>
20c04efc0bSmarco #include <sys/device.h>
217de1614aScheloha #include <sys/stdint.h>
22c04efc0bSmarco #include <sys/timetc.h>
23c04efc0bSmarco 
24c04efc0bSmarco #include <machine/bus.h>
257de1614aScheloha #include <machine/cpu.h>
26c04efc0bSmarco 
27c04efc0bSmarco #include <dev/acpi/acpireg.h>
28c04efc0bSmarco #include <dev/acpi/acpivar.h>
29c04efc0bSmarco #include <dev/acpi/acpidev.h>
30c04efc0bSmarco 
317a772371Smikeb int acpihpet_attached;
327a772371Smikeb 
33c04efc0bSmarco int acpihpet_match(struct device *, void *, void *);
34c04efc0bSmarco void acpihpet_attach(struct device *, struct device *, void *);
35e78728c7Spirofti int acpihpet_activate(struct device *, int);
367de1614aScheloha void acpihpet_delay(int);
37c04efc0bSmarco u_int acpihpet_gettime(struct timecounter *tc);
38c04efc0bSmarco 
39d2eaebe9Skettenis uint64_t	acpihpet_r(bus_space_tag_t _iot, bus_space_handle_t _ioh,
4083270512Sderaadt 		    bus_size_t _ioa);
4183270512Sderaadt void		acpihpet_w(bus_space_tag_t _iot, bus_space_handle_t _ioh,
42d2eaebe9Skettenis 		    bus_size_t _ioa, uint64_t _val);
4383270512Sderaadt 
44c04efc0bSmarco static struct timecounter hpet_timecounter = {
458611d3cdScheloha 	.tc_get_timecount = acpihpet_gettime,
468611d3cdScheloha 	.tc_counter_mask = 0xffffffff,
478611d3cdScheloha 	.tc_frequency = 0,
488611d3cdScheloha 	.tc_name = 0,
498611d3cdScheloha 	.tc_quality = 1000,
508611d3cdScheloha 	.tc_priv = NULL,
518611d3cdScheloha 	.tc_user = 0,
52c04efc0bSmarco };
53c04efc0bSmarco 
5483270512Sderaadt #define HPET_TIMERS	3
5583270512Sderaadt struct hpet_regs {
56d2eaebe9Skettenis 	uint64_t	configuration;
57d2eaebe9Skettenis 	uint64_t	interrupt_status;
58d2eaebe9Skettenis 	uint64_t	main_counter;
5983270512Sderaadt 	struct {	/* timers */
60d2eaebe9Skettenis 		uint64_t config;
61d2eaebe9Skettenis 		uint64_t compare;
62d2eaebe9Skettenis 		uint64_t interrupt;
6383270512Sderaadt 	} timers[HPET_TIMERS];
6483270512Sderaadt };
6583270512Sderaadt 
66c04efc0bSmarco struct acpihpet_softc {
67c04efc0bSmarco 	struct device		sc_dev;
68c04efc0bSmarco 
69c04efc0bSmarco 	bus_space_tag_t		sc_iot;
70c04efc0bSmarco 	bus_space_handle_t	sc_ioh;
7183270512Sderaadt 
72d2eaebe9Skettenis 	uint32_t		sc_conf;
7383270512Sderaadt 	struct hpet_regs	sc_save;
74c04efc0bSmarco };
75c04efc0bSmarco 
76471aeecfSnaddy const struct cfattach acpihpet_ca = {
773add14d5Sderaadt 	sizeof(struct acpihpet_softc), acpihpet_match, acpihpet_attach,
783add14d5Sderaadt 	NULL, acpihpet_activate
79c04efc0bSmarco };
80c04efc0bSmarco 
81c04efc0bSmarco struct cfdriver acpihpet_cd = {
82c04efc0bSmarco 	NULL, "acpihpet", DV_DULL
83c04efc0bSmarco };
84c04efc0bSmarco 
85d2eaebe9Skettenis uint64_t
acpihpet_r(bus_space_tag_t iot,bus_space_handle_t ioh,bus_size_t ioa)8683270512Sderaadt acpihpet_r(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t ioa)
8783270512Sderaadt {
88d2eaebe9Skettenis 	uint64_t val;
8983270512Sderaadt 
9083270512Sderaadt 	val = bus_space_read_4(iot, ioh, ioa + 4);
9183270512Sderaadt 	val = val << 32;
9283270512Sderaadt 	val |= bus_space_read_4(iot, ioh, ioa);
9383270512Sderaadt 	return (val);
9483270512Sderaadt }
9583270512Sderaadt 
9683270512Sderaadt void
acpihpet_w(bus_space_tag_t iot,bus_space_handle_t ioh,bus_size_t ioa,uint64_t val)9783270512Sderaadt acpihpet_w(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t ioa,
98d2eaebe9Skettenis     uint64_t val)
9983270512Sderaadt {
10083270512Sderaadt 	bus_space_write_4(iot, ioh, ioa + 4, val >> 32);
10183270512Sderaadt 	bus_space_write_4(iot, ioh, ioa, val & 0xffffffff);
10283270512Sderaadt }
10383270512Sderaadt 
104c04efc0bSmarco int
acpihpet_activate(struct device * self,int act)105e78728c7Spirofti acpihpet_activate(struct device *self, int act)
1063ccb1ee2Spirofti {
1073ccb1ee2Spirofti 	struct acpihpet_softc *sc = (struct acpihpet_softc *) self;
1083ccb1ee2Spirofti 
1093ccb1ee2Spirofti 	switch (act) {
11047f416edSderaadt 	case DVACT_SUSPEND:
1116a28311cScheloha 		delay_fini(acpihpet_delay);
1126a28311cScheloha 
11383270512Sderaadt 		/* stop, then save */
11447f416edSderaadt 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
11547f416edSderaadt 		    HPET_CONFIGURATION, sc->sc_conf);
11683270512Sderaadt 
11783270512Sderaadt 		sc->sc_save.configuration = acpihpet_r(sc->sc_iot,
11883270512Sderaadt 		    sc->sc_ioh, HPET_CONFIGURATION);
11983270512Sderaadt 		sc->sc_save.interrupt_status = acpihpet_r(sc->sc_iot,
12083270512Sderaadt 		    sc->sc_ioh, HPET_INTERRUPT_STATUS);
12183270512Sderaadt 		sc->sc_save.main_counter = acpihpet_r(sc->sc_iot,
12283270512Sderaadt 		    sc->sc_ioh, HPET_MAIN_COUNTER);
12383270512Sderaadt 		sc->sc_save.timers[0].config = acpihpet_r(sc->sc_iot,
12483270512Sderaadt 		    sc->sc_ioh, HPET_TIMER0_CONFIG);
12583270512Sderaadt 		sc->sc_save.timers[0].interrupt = acpihpet_r(sc->sc_iot,
12683270512Sderaadt 		    sc->sc_ioh, HPET_TIMER0_INTERRUPT);
12783270512Sderaadt 		sc->sc_save.timers[0].compare = acpihpet_r(sc->sc_iot,
12883270512Sderaadt 		    sc->sc_ioh, HPET_TIMER0_COMPARE);
12983270512Sderaadt 		sc->sc_save.timers[1].config = acpihpet_r(sc->sc_iot,
13083270512Sderaadt 		    sc->sc_ioh, HPET_TIMER1_CONFIG);
13183270512Sderaadt 		sc->sc_save.timers[1].interrupt = acpihpet_r(sc->sc_iot,
13283270512Sderaadt 		    sc->sc_ioh, HPET_TIMER1_INTERRUPT);
13383270512Sderaadt 		sc->sc_save.timers[1].compare = acpihpet_r(sc->sc_iot,
13483270512Sderaadt 		    sc->sc_ioh, HPET_TIMER1_COMPARE);
13583270512Sderaadt 		sc->sc_save.timers[2].config = acpihpet_r(sc->sc_iot,
13683270512Sderaadt 		    sc->sc_ioh, HPET_TIMER2_CONFIG);
13783270512Sderaadt 		sc->sc_save.timers[2].interrupt = acpihpet_r(sc->sc_iot,
13883270512Sderaadt 		    sc->sc_ioh, HPET_TIMER2_INTERRUPT);
13983270512Sderaadt 		sc->sc_save.timers[2].compare = acpihpet_r(sc->sc_iot,
14083270512Sderaadt 		    sc->sc_ioh, HPET_TIMER2_COMPARE);
14147f416edSderaadt 		break;
142ef0fa280Spirofti 	case DVACT_RESUME:
14383270512Sderaadt 		/* stop, restore, then restart */
14483270512Sderaadt 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
14583270512Sderaadt 		    HPET_CONFIGURATION, sc->sc_conf);
14683270512Sderaadt 
14783270512Sderaadt 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
14883270512Sderaadt 		    HPET_CONFIGURATION, sc->sc_save.configuration);
14983270512Sderaadt 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
15083270512Sderaadt 		    HPET_INTERRUPT_STATUS, sc->sc_save.interrupt_status);
15183270512Sderaadt 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
15283270512Sderaadt 		    HPET_MAIN_COUNTER, sc->sc_save.main_counter);
15383270512Sderaadt 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
15483270512Sderaadt 		    HPET_TIMER0_CONFIG, sc->sc_save.timers[0].config);
15583270512Sderaadt 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
15683270512Sderaadt 		    HPET_TIMER0_INTERRUPT, sc->sc_save.timers[0].interrupt);
15783270512Sderaadt 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
15883270512Sderaadt 		    HPET_TIMER0_COMPARE, sc->sc_save.timers[0].compare);
15983270512Sderaadt 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
16083270512Sderaadt 		    HPET_TIMER1_CONFIG, sc->sc_save.timers[1].config);
16183270512Sderaadt 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
16283270512Sderaadt 		    HPET_TIMER1_INTERRUPT, sc->sc_save.timers[1].interrupt);
16383270512Sderaadt 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
16483270512Sderaadt 		    HPET_TIMER1_COMPARE, sc->sc_save.timers[1].compare);
16583270512Sderaadt 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
16683270512Sderaadt 		    HPET_TIMER2_CONFIG, sc->sc_save.timers[2].config);
16783270512Sderaadt 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
16883270512Sderaadt 		    HPET_TIMER2_INTERRUPT, sc->sc_save.timers[2].interrupt);
16983270512Sderaadt 		acpihpet_w(sc->sc_iot, sc->sc_ioh,
17083270512Sderaadt 		    HPET_TIMER2_COMPARE, sc->sc_save.timers[2].compare);
1713ccb1ee2Spirofti 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
17247f416edSderaadt 		    HPET_CONFIGURATION, sc->sc_conf | 1);
1736a28311cScheloha 
1746a28311cScheloha 		delay_init(acpihpet_delay, 2000);
1753ccb1ee2Spirofti 		break;
1763ccb1ee2Spirofti 	}
1773ccb1ee2Spirofti 
1783ccb1ee2Spirofti 	return 0;
1793ccb1ee2Spirofti }
1803ccb1ee2Spirofti 
1813ccb1ee2Spirofti int
acpihpet_match(struct device * parent,void * match,void * aux)182c04efc0bSmarco acpihpet_match(struct device *parent, void *match, void *aux)
183c04efc0bSmarco {
184c04efc0bSmarco 	struct acpi_attach_args *aaa = aux;
185c04efc0bSmarco 	struct acpi_table_header *hdr;
186c04efc0bSmarco 
187c04efc0bSmarco 	/*
1887a772371Smikeb 	 * If we do not have a table, it is not us; attach only once
189c04efc0bSmarco 	 */
1907a772371Smikeb 	if (acpihpet_attached || aaa->aaa_table == NULL)
191c04efc0bSmarco 		return (0);
192c04efc0bSmarco 
193c04efc0bSmarco 	/*
194c04efc0bSmarco 	 * If it is an HPET table, we can attach
195c04efc0bSmarco 	 */
196c04efc0bSmarco 	hdr = (struct acpi_table_header *)aaa->aaa_table;
197c04efc0bSmarco 	if (memcmp(hdr->signature, HPET_SIG, sizeof(HPET_SIG) - 1) != 0)
198c04efc0bSmarco 		return (0);
199c04efc0bSmarco 
200c04efc0bSmarco 	return (1);
201c04efc0bSmarco }
202c04efc0bSmarco 
203c04efc0bSmarco void
acpihpet_attach(struct device * parent,struct device * self,void * aux)204c04efc0bSmarco acpihpet_attach(struct device *parent, struct device *self, void *aux)
205c04efc0bSmarco {
206c04efc0bSmarco 	struct acpihpet_softc *sc = (struct acpihpet_softc *) self;
207b61ce84dSjordan 	struct acpi_softc *psc = (struct acpi_softc *)parent;
208b61ce84dSjordan 	struct acpi_attach_args *aaa = aux;
209b61ce84dSjordan 	struct acpi_hpet *hpet = (struct acpi_hpet *)aaa->aaa_table;
210d2eaebe9Skettenis 	uint64_t period, freq;	/* timer period in femtoseconds (10^-15) */
211d2eaebe9Skettenis 	uint32_t v1, v2;
2125648a1ffSkettenis 	int timeout;
213c04efc0bSmarco 
214b61ce84dSjordan 	if (acpi_map_address(psc, &hpet->base_address, 0, HPET_REG_SIZE,
215b61ce84dSjordan 	    &sc->sc_ioh, &sc->sc_iot))	{
216c04efc0bSmarco 		printf(": can't map i/o space\n");
217c04efc0bSmarco 		return;
218c04efc0bSmarco 	}
219c04efc0bSmarco 
2205648a1ffSkettenis 	/*
2215648a1ffSkettenis 	 * Revisions 0x30 through 0x3a of the AMD SB700, with spread
2225648a1ffSkettenis 	 * spectrum enabled, have an SMM based HPET emulation that's
2235648a1ffSkettenis 	 * subtly broken.  The hardware is initialized upon first
2245648a1ffSkettenis 	 * access of the configuration register.  Initialization takes
2255648a1ffSkettenis 	 * some time during which the configuration register returns
2265648a1ffSkettenis 	 * 0xffffffff.
2275648a1ffSkettenis 	 */
2285648a1ffSkettenis 	timeout = 1000;
2295648a1ffSkettenis 	do {
2305648a1ffSkettenis 		if (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
2315648a1ffSkettenis 		    HPET_CONFIGURATION) != 0xffffffff)
2325648a1ffSkettenis 			break;
2335648a1ffSkettenis 	} while(--timeout > 0);
2345648a1ffSkettenis 
2355648a1ffSkettenis 	if (timeout == 0) {
2365648a1ffSkettenis 		printf(": disabled\n");
2375648a1ffSkettenis 		return;
2385648a1ffSkettenis 	}
2395648a1ffSkettenis 
2404aec2e72Smarco 	/* enable hpet */
24147f416edSderaadt 	sc->sc_conf = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
24247f416edSderaadt 	    HPET_CONFIGURATION) & ~1;
24347f416edSderaadt 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, HPET_CONFIGURATION,
24447f416edSderaadt 	    sc->sc_conf | 1);
2454aec2e72Smarco 
2464aec2e72Smarco 	/* make sure hpet is working */
2474aec2e72Smarco 	v1 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, HPET_MAIN_COUNTER);
2484aec2e72Smarco 	delay(1);
2494aec2e72Smarco 	v2 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, HPET_MAIN_COUNTER);
2504aec2e72Smarco 	if (v1 == v2) {
2514aec2e72Smarco 		printf(": counter not incrementing\n");
2524aec2e72Smarco 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
25347f416edSderaadt 		    HPET_CONFIGURATION, sc->sc_conf);
2544aec2e72Smarco 		return;
2554aec2e72Smarco 	}
2564aec2e72Smarco 
257c04efc0bSmarco 	period = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
258d2eaebe9Skettenis 	    HPET_CAPABILITIES + sizeof(uint32_t));
259a02f4b6cSmlarkin 
260a02f4b6cSmlarkin 	/* Period must be > 0 and less than 100ns (10^8 fs) */
261a02f4b6cSmlarkin 	if (period == 0 || period > HPET_MAX_PERIOD) {
2624aec2e72Smarco 		printf(": invalid period\n");
2634aec2e72Smarco 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
26447f416edSderaadt 		    HPET_CONFIGURATION, sc->sc_conf);
2654aec2e72Smarco 		return;
2664aec2e72Smarco 	}
267c04efc0bSmarco 	freq = 1000000000000000ull / period;
268c04efc0bSmarco 	printf(": %lld Hz\n", freq);
269c04efc0bSmarco 
27055cc0373Scheloha 	hpet_timecounter.tc_frequency = freq;
271c04efc0bSmarco 	hpet_timecounter.tc_priv = sc;
272c04efc0bSmarco 	hpet_timecounter.tc_name = sc->sc_dev.dv_xname;
273c04efc0bSmarco 	tc_init(&hpet_timecounter);
2747de1614aScheloha 
2757de1614aScheloha 	delay_init(acpihpet_delay, 2000);
2767de1614aScheloha 
277eb35b7b4Smikeb #if defined(__amd64__)
278eb35b7b4Smikeb 	extern void cpu_recalibrate_tsc(struct timecounter *);
279eb35b7b4Smikeb 	cpu_recalibrate_tsc(&hpet_timecounter);
280eb35b7b4Smikeb #endif
2817a772371Smikeb 	acpihpet_attached++;
282c04efc0bSmarco }
283c04efc0bSmarco 
2847de1614aScheloha void
acpihpet_delay(int usecs)2857de1614aScheloha acpihpet_delay(int usecs)
2867de1614aScheloha {
28781ef0162Scheloha 	uint64_t count = 0, cycles;
2887de1614aScheloha 	struct acpihpet_softc *sc = hpet_timecounter.tc_priv;
28981ef0162Scheloha 	uint32_t val1, val2;
2907de1614aScheloha 
29181ef0162Scheloha 	val2 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, HPET_MAIN_COUNTER);
29281ef0162Scheloha 	cycles = usecs * hpet_timecounter.tc_frequency / 1000000;
29381ef0162Scheloha 	while (count < cycles) {
2947de1614aScheloha 		CPU_BUSY_CYCLE();
29581ef0162Scheloha 		val1 = val2;
29681ef0162Scheloha 		val2 = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
29781ef0162Scheloha 		    HPET_MAIN_COUNTER);
29881ef0162Scheloha 		count += val2 - val1;
29981ef0162Scheloha 	}
3007de1614aScheloha }
3017de1614aScheloha 
302c04efc0bSmarco u_int
acpihpet_gettime(struct timecounter * tc)303c04efc0bSmarco acpihpet_gettime(struct timecounter *tc)
304c04efc0bSmarco {
305c04efc0bSmarco 	struct acpihpet_softc *sc = tc->tc_priv;
306c04efc0bSmarco 
307c04efc0bSmarco 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, HPET_MAIN_COUNTER));
308c04efc0bSmarco }
309