acpi_hpet.c revision 199016
1/*-
2 * Copyright (c) 2005 Poul-Henning Kamp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi_hpet.c 199016 2009-11-07 11:46:38Z avg $");
29
30#include "opt_acpi.h"
31#include <sys/param.h>
32#include <sys/bus.h>
33#include <sys/kernel.h>
34#include <sys/module.h>
35#include <sys/rman.h>
36#include <sys/time.h>
37#include <sys/timetc.h>
38
39#include <contrib/dev/acpica/include/acpi.h>
40#include <contrib/dev/acpica/include/accommon.h>
41
42#include <dev/acpica/acpivar.h>
43#include <dev/acpica/acpi_hpet.h>
44
45ACPI_SERIAL_DECL(hpet, "ACPI HPET support");
46
47static devclass_t acpi_hpet_devclass;
48
49/* ACPI CA debugging */
50#define _COMPONENT	ACPI_TIMER
51ACPI_MODULE_NAME("HPET")
52
53struct acpi_hpet_softc {
54	device_t		dev;
55	struct resource		*mem_res;
56	ACPI_HANDLE		handle;
57};
58
59static u_int hpet_get_timecount(struct timecounter *tc);
60static void acpi_hpet_test(struct acpi_hpet_softc *sc);
61
62static char *hpet_ids[] = { "PNP0103", NULL };
63
64struct timecounter hpet_timecounter = {
65	.tc_get_timecount =	hpet_get_timecount,
66	.tc_counter_mask =	~0u,
67	.tc_name =		"HPET",
68	.tc_quality =		900,
69};
70
71static u_int
72hpet_get_timecount(struct timecounter *tc)
73{
74	struct acpi_hpet_softc *sc;
75
76	sc = tc->tc_priv;
77	return (bus_read_4(sc->mem_res, HPET_MAIN_COUNTER));
78}
79
80static void
81hpet_enable(struct acpi_hpet_softc *sc)
82{
83	uint32_t val;
84
85	val = bus_read_4(sc->mem_res, HPET_CONFIG);
86	val &= ~HPET_CNF_LEG_RT;
87	val |= HPET_CNF_ENABLE;
88	bus_write_4(sc->mem_res, HPET_CONFIG, val);
89}
90
91static void
92hpet_disable(struct acpi_hpet_softc *sc)
93{
94	uint32_t val;
95
96	val = bus_read_4(sc->mem_res, HPET_CONFIG);
97	val &= ~HPET_CNF_ENABLE;
98	bus_write_4(sc->mem_res, HPET_CONFIG, val);
99}
100
101/* Discover the HPET via the ACPI table of the same name. */
102static void
103acpi_hpet_identify(driver_t *driver, device_t parent)
104{
105	ACPI_TABLE_HPET *hpet;
106	ACPI_TABLE_HEADER *hdr;
107	ACPI_STATUS	status;
108	device_t	child;
109
110	/* Only one HPET device can be added. */
111	if (devclass_get_device(acpi_hpet_devclass, 0))
112		return;
113
114	/* Currently, ID and minimum clock tick info is unused. */
115
116	status = AcpiGetTable(ACPI_SIG_HPET, 1, (ACPI_TABLE_HEADER **)&hdr);
117	if (ACPI_FAILURE(status))
118		return;
119
120	/*
121	 * The unit number could be derived from hdr->Sequence but we only
122	 * support one HPET device.
123	 */
124	hpet = (ACPI_TABLE_HPET *)hdr;
125	if (hpet->Sequence != 0)
126		printf("ACPI HPET table warning: Sequence is non-zero (%d)\n",
127		    hpet->Sequence);
128	child = BUS_ADD_CHILD(parent, ACPI_DEV_BASE_ORDER, "acpi_hpet", 0);
129	if (child == NULL) {
130		printf("%s: can't add child\n", __func__);
131		return;
132	}
133
134	bus_set_resource(child, SYS_RES_MEMORY, 0, hpet->Address.Address,
135	    HPET_MEM_WIDTH);
136}
137
138static int
139acpi_hpet_probe(device_t dev)
140{
141	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
142
143	if (acpi_disabled("hpet"))
144		return (ENXIO);
145	if (acpi_get_handle(dev) != NULL &&
146	    (ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL ||
147	    device_get_unit(dev) != 0))
148		return (ENXIO);
149
150	device_set_desc(dev, "High Precision Event Timer");
151	return (0);
152}
153
154static int
155acpi_hpet_attach(device_t dev)
156{
157	struct acpi_hpet_softc *sc;
158	int rid;
159	uint32_t val, val2;
160	uintmax_t freq;
161
162	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
163
164	sc = device_get_softc(dev);
165	sc->dev = dev;
166	sc->handle = acpi_get_handle(dev);
167
168	rid = 0;
169	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
170	    RF_ACTIVE);
171	if (sc->mem_res == NULL)
172		return (ENOMEM);
173
174	/* Validate that we can access the whole region. */
175	if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH) {
176		device_printf(dev, "memory region width %ld too small\n",
177		    rman_get_size(sc->mem_res));
178		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
179		return (ENXIO);
180	}
181
182	/* Be sure timer is enabled. */
183	hpet_enable(sc);
184
185	/* Read basic statistics about the timer. */
186	val = bus_read_4(sc->mem_res, HPET_PERIOD);
187	if (val == 0) {
188		device_printf(dev, "invalid period\n");
189		hpet_disable(sc);
190		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
191		return (ENXIO);
192	}
193
194	freq = (1000000000000000LL + val / 2) / val;
195	if (bootverbose) {
196		val = bus_read_4(sc->mem_res, HPET_CAPABILITIES);
197		device_printf(dev,
198		    "vend: 0x%x rev: 0x%x num: %d hz: %jd opts:%s%s\n",
199		    val >> 16, val & HPET_CAP_REV_ID,
200		    (val & HPET_CAP_NUM_TIM) >> 8, freq,
201		    (val & HPET_CAP_LEG_RT) ? " legacy_route" : "",
202		    (val & HPET_CAP_COUNT_SIZE) ? " 64-bit" : "");
203	}
204
205	if (testenv("debug.acpi.hpet_test"))
206		acpi_hpet_test(sc);
207
208	/*
209	 * Don't attach if the timer never increments.  Since the spec
210	 * requires it to be at least 10 MHz, it has to change in 1 us.
211	 */
212	val = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
213	DELAY(1);
214	val2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
215	if (val == val2) {
216		device_printf(dev, "HPET never increments, disabling\n");
217		hpet_disable(sc);
218		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
219		return (ENXIO);
220	}
221
222	hpet_timecounter.tc_frequency = freq;
223	hpet_timecounter.tc_priv = sc;
224	tc_init(&hpet_timecounter);
225
226	return (0);
227}
228
229static int
230acpi_hpet_detach(device_t dev)
231{
232	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
233
234	/* XXX Without a tc_remove() function, we can't detach. */
235	return (EBUSY);
236}
237
238static int
239acpi_hpet_suspend(device_t dev)
240{
241	struct acpi_hpet_softc *sc;
242
243	/*
244	 * Disable the timer during suspend.  The timer will not lose
245	 * its state in S1 or S2, but we are required to disable
246	 * it.
247	 */
248	sc = device_get_softc(dev);
249	hpet_disable(sc);
250
251	return (0);
252}
253
254static int
255acpi_hpet_resume(device_t dev)
256{
257	struct acpi_hpet_softc *sc;
258
259	/* Re-enable the timer after a resume to keep the clock advancing. */
260	sc = device_get_softc(dev);
261	hpet_enable(sc);
262
263	return (0);
264}
265
266/* Print some basic latency/rate information to assist in debugging. */
267static void
268acpi_hpet_test(struct acpi_hpet_softc *sc)
269{
270	int i;
271	uint32_t u1, u2;
272	struct bintime b0, b1, b2;
273	struct timespec ts;
274
275	binuptime(&b0);
276	binuptime(&b0);
277	binuptime(&b1);
278	u1 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
279	for (i = 1; i < 1000; i++)
280		u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
281	binuptime(&b2);
282	u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
283
284	bintime_sub(&b2, &b1);
285	bintime_sub(&b1, &b0);
286	bintime_sub(&b2, &b1);
287	bintime2timespec(&b2, &ts);
288
289	device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
290	    (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1);
291
292	device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);
293}
294
295static device_method_t acpi_hpet_methods[] = {
296	/* Device interface */
297	DEVMETHOD(device_identify, acpi_hpet_identify),
298	DEVMETHOD(device_probe, acpi_hpet_probe),
299	DEVMETHOD(device_attach, acpi_hpet_attach),
300	DEVMETHOD(device_detach, acpi_hpet_detach),
301	DEVMETHOD(device_suspend, acpi_hpet_suspend),
302	DEVMETHOD(device_resume, acpi_hpet_resume),
303
304	{0, 0}
305};
306
307static driver_t	acpi_hpet_driver = {
308	"acpi_hpet",
309	acpi_hpet_methods,
310	sizeof(struct acpi_hpet_softc),
311};
312
313
314DRIVER_MODULE(acpi_hpet, acpi, acpi_hpet_driver, acpi_hpet_devclass, 0, 0);
315MODULE_DEPEND(acpi_hpet, acpi, 1, 1, 1);
316