acpi_hpet.c revision 213302
1151912Sphk/*-
2151912Sphk * Copyright (c) 2005 Poul-Henning Kamp
3209440Smav * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
4151912Sphk * All rights reserved.
5151912Sphk *
6151912Sphk * Redistribution and use in source and binary forms, with or without
7151912Sphk * modification, are permitted provided that the following conditions
8151912Sphk * are met:
9151912Sphk * 1. Redistributions of source code must retain the above copyright
10151912Sphk *    notice, this list of conditions and the following disclaimer.
11151912Sphk * 2. Redistributions in binary form must reproduce the above copyright
12151912Sphk *    notice, this list of conditions and the following disclaimer in the
13151912Sphk *    documentation and/or other materials provided with the distribution.
14151912Sphk *
15151912Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16151912Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17151912Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18151912Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19151912Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20151912Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21151912Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22151912Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23151912Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24151912Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25151912Sphk * SUCH DAMAGE.
26151912Sphk */
27151912Sphk
28151912Sphk#include <sys/cdefs.h>
29151912Sphk__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi_hpet.c 213302 2010-09-30 16:23:01Z mav $");
30151912Sphk
31151912Sphk#include "opt_acpi.h"
32209402Smav#if defined(__amd64__) || defined(__ia64__)
33209371Smav#define	DEV_APIC
34209371Smav#else
35209371Smav#include "opt_apic.h"
36209371Smav#endif
37151912Sphk#include <sys/param.h>
38159217Snjl#include <sys/bus.h>
39151912Sphk#include <sys/kernel.h>
40151912Sphk#include <sys/module.h>
41209371Smav#include <sys/proc.h>
42151912Sphk#include <sys/rman.h>
43151912Sphk#include <sys/time.h>
44209371Smav#include <sys/smp.h>
45209371Smav#include <sys/sysctl.h>
46209371Smav#include <sys/timeet.h>
47151912Sphk#include <sys/timetc.h>
48159217Snjl
49193530Sjkim#include <contrib/dev/acpica/include/acpi.h>
50193530Sjkim#include <contrib/dev/acpica/include/accommon.h>
51193530Sjkim
52151912Sphk#include <dev/acpica/acpivar.h>
53175385Sjhb#include <dev/acpica/acpi_hpet.h>
54151912Sphk
55209371Smav#ifdef DEV_APIC
56209371Smav#include "pcib_if.h"
57209371Smav#endif
58209371Smav
59203062Savg#define HPET_VENDID_AMD		0x4353
60203062Savg#define HPET_VENDID_INTEL	0x8086
61213302Smav#define HPET_VENDID_NVIDIA	0x10de
62203062Savg
63151912SphkACPI_SERIAL_DECL(hpet, "ACPI HPET support");
64151912Sphk
65209371Smavstatic devclass_t hpet_devclass;
66169574Stakawata
67151931Sscottl/* ACPI CA debugging */
68151935Sscottl#define _COMPONENT	ACPI_TIMER
69151931SscottlACPI_MODULE_NAME("HPET")
70151931Sscottl
71209371Smavstruct hpet_softc {
72151912Sphk	device_t		dev;
73209371Smav	int			mem_rid;
74209371Smav	int			intr_rid;
75209371Smav	int			irq;
76209371Smav	int			useirq;
77209440Smav	int			legacy_route;
78212533Smav	int			per_cpu;
79212238Smav	uint32_t		allowed_irqs;
80159217Snjl	struct resource		*mem_res;
81209371Smav	struct resource		*intr_res;
82209371Smav	void			*intr_handle;
83151912Sphk	ACPI_HANDLE		handle;
84209371Smav	uint64_t		freq;
85209440Smav	uint32_t		caps;
86209371Smav	struct timecounter	tc;
87209371Smav	struct hpet_timer {
88209371Smav		struct eventtimer	et;
89209371Smav		struct hpet_softc	*sc;
90209371Smav		int			num;
91209371Smav		int			mode;
92209371Smav		int			intr_rid;
93209371Smav		int			irq;
94212323Smav		int			pcpu_cpu;
95212323Smav		int			pcpu_misrouted;
96209371Smav		int			pcpu_master;
97209371Smav		int			pcpu_slaves[MAXCPU];
98209371Smav		struct resource		*intr_res;
99209371Smav		void			*intr_handle;
100209371Smav		uint32_t		caps;
101209371Smav		uint32_t		vectors;
102209371Smav		uint32_t		div;
103212491Smav		uint32_t		next;
104209371Smav		char			name[8];
105209371Smav	} 			t[32];
106209371Smav	int			num_timers;
107151912Sphk};
108151912Sphk
109159217Snjlstatic u_int hpet_get_timecount(struct timecounter *tc);
110209371Smavstatic void hpet_test(struct hpet_softc *sc);
111151912Sphk
112159217Snjlstatic char *hpet_ids[] = { "PNP0103", NULL };
113159217Snjl
114159217Snjlstatic u_int
115151912Sphkhpet_get_timecount(struct timecounter *tc)
116151912Sphk{
117209371Smav	struct hpet_softc *sc;
118151912Sphk
119151912Sphk	sc = tc->tc_priv;
120175385Sjhb	return (bus_read_4(sc->mem_res, HPET_MAIN_COUNTER));
121151912Sphk}
122151912Sphk
123175361Sjhbstatic void
124209371Smavhpet_enable(struct hpet_softc *sc)
125175361Sjhb{
126175361Sjhb	uint32_t val;
127175385Sjhb
128175385Sjhb	val = bus_read_4(sc->mem_res, HPET_CONFIG);
129209440Smav	if (sc->legacy_route)
130209440Smav		val |= HPET_CNF_LEG_RT;
131209440Smav	else
132209440Smav		val &= ~HPET_CNF_LEG_RT;
133185103Sjkim	val |= HPET_CNF_ENABLE;
134185103Sjkim	bus_write_4(sc->mem_res, HPET_CONFIG, val);
135175361Sjhb}
136175361Sjhb
137175361Sjhbstatic void
138209371Smavhpet_disable(struct hpet_softc *sc)
139175361Sjhb{
140175361Sjhb	uint32_t val;
141175385Sjhb
142175385Sjhb	val = bus_read_4(sc->mem_res, HPET_CONFIG);
143185103Sjkim	val &= ~HPET_CNF_ENABLE;
144185103Sjkim	bus_write_4(sc->mem_res, HPET_CONFIG, val);
145175361Sjhb}
146175361Sjhb
147209371Smavstatic int
148209371Smavhpet_start(struct eventtimer *et,
149209371Smav    struct bintime *first, struct bintime *period)
150209371Smav{
151209371Smav	struct hpet_timer *mt = (struct hpet_timer *)et->et_priv;
152209371Smav	struct hpet_timer *t;
153209371Smav	struct hpet_softc *sc = mt->sc;
154212491Smav	uint32_t fdiv, now;
155209371Smav
156209371Smav	t = (mt->pcpu_master < 0) ? mt : &sc->t[mt->pcpu_slaves[curcpu]];
157209371Smav	if (period != NULL) {
158209371Smav		t->mode = 1;
159209371Smav		t->div = (sc->freq * (period->frac >> 32)) >> 32;
160209371Smav		if (period->sec != 0)
161209371Smav			t->div += sc->freq * period->sec;
162209371Smav	} else {
163209371Smav		t->mode = 2;
164209371Smav		t->div = 0;
165209371Smav	}
166210290Smav	if (first != NULL) {
167210290Smav		fdiv = (sc->freq * (first->frac >> 32)) >> 32;
168210290Smav		if (first->sec != 0)
169210290Smav			fdiv += sc->freq * first->sec;
170210290Smav	} else
171210290Smav		fdiv = t->div;
172212238Smav	if (t->irq < 0)
173212238Smav		bus_write_4(sc->mem_res, HPET_ISR, 1 << t->num);
174212238Smav	t->caps |= HPET_TCNF_INT_ENB;
175212491Smav	now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
176212238Smavrestart:
177212491Smav	t->next = now + fdiv;
178209371Smav	if (t->mode == 1 && (t->caps & HPET_TCAP_PER_INT)) {
179209371Smav		t->caps |= HPET_TCNF_TYPE;
180209371Smav		bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
181209371Smav		    t->caps | HPET_TCNF_VAL_SET);
182212491Smav		bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
183212491Smav		    t->next);
184212491Smav		bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
185212491Smav		    t->div);
186209371Smav	} else {
187212238Smav		t->caps &= ~HPET_TCNF_TYPE;
188212491Smav		bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
189212491Smav		    t->caps);
190212491Smav		bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
191212491Smav		    t->next);
192209371Smav	}
193212238Smav	if (fdiv < 5000) {
194212238Smav		bus_read_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num));
195212491Smav		now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
196212491Smav		if ((int32_t)(now - t->next) >= 0) {
197212238Smav			fdiv *= 2;
198212238Smav			goto restart;
199212238Smav		}
200212238Smav	}
201209371Smav	return (0);
202209371Smav}
203209371Smav
204209371Smavstatic int
205209371Smavhpet_stop(struct eventtimer *et)
206209371Smav{
207209371Smav	struct hpet_timer *mt = (struct hpet_timer *)et->et_priv;
208209371Smav	struct hpet_timer *t;
209209371Smav	struct hpet_softc *sc = mt->sc;
210209371Smav
211209371Smav	t = (mt->pcpu_master < 0) ? mt : &sc->t[mt->pcpu_slaves[curcpu]];
212209371Smav	t->mode = 0;
213209371Smav	t->caps &= ~(HPET_TCNF_INT_ENB | HPET_TCNF_TYPE);
214209371Smav	bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), t->caps);
215209371Smav	return (0);
216209371Smav}
217209371Smav
218209371Smavstatic int
219209371Smavhpet_intr_single(void *arg)
220209371Smav{
221209371Smav	struct hpet_timer *t = (struct hpet_timer *)arg;
222209371Smav	struct hpet_timer *mt;
223209371Smav	struct hpet_softc *sc = t->sc;
224209371Smav	uint32_t now;
225209371Smav
226212491Smav	if (t->mode == 0)
227212491Smav		return (FILTER_STRAY);
228212323Smav	/* Check that per-CPU timer interrupt reached right CPU. */
229212323Smav	if (t->pcpu_cpu >= 0 && t->pcpu_cpu != curcpu) {
230212323Smav		if ((++t->pcpu_misrouted) % 32 == 0) {
231212323Smav			printf("HPET interrupt routed to the wrong CPU"
232212323Smav			    " (timer %d CPU %d -> %d)!\n",
233212323Smav			    t->num, t->pcpu_cpu, curcpu);
234212323Smav		}
235212323Smav
236212323Smav		/*
237212323Smav		 * Reload timer, hoping that next time may be more lucky
238212323Smav		 * (system will manage proper interrupt binding).
239212323Smav		 */
240212323Smav		if ((t->mode == 1 && (t->caps & HPET_TCAP_PER_INT) == 0) ||
241212323Smav		    t->mode == 2) {
242212491Smav			t->next = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER) +
243212491Smav			    sc->freq / 8;
244212323Smav			bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
245212491Smav			    t->next);
246212323Smav		}
247212323Smav		return (FILTER_HANDLED);
248212323Smav	}
249209371Smav	if (t->mode == 1 &&
250209371Smav	    (t->caps & HPET_TCAP_PER_INT) == 0) {
251212491Smav		t->next += t->div;
252209371Smav		now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
253212491Smav		if ((int32_t)((now + t->div / 2) - t->next) > 0)
254212491Smav			t->next = now + t->div / 2;
255209371Smav		bus_write_4(sc->mem_res,
256212491Smav		    HPET_TIMER_COMPARATOR(t->num), t->next);
257209371Smav	} else if (t->mode == 2)
258209371Smav		t->mode = 0;
259209371Smav	mt = (t->pcpu_master < 0) ? t : &sc->t[t->pcpu_master];
260209990Smav	if (mt->et.et_active)
261209990Smav		mt->et.et_event_cb(&mt->et, mt->et.et_arg);
262209371Smav	return (FILTER_HANDLED);
263209371Smav}
264209371Smav
265209371Smavstatic int
266209371Smavhpet_intr(void *arg)
267209371Smav{
268209371Smav	struct hpet_softc *sc = (struct hpet_softc *)arg;
269209371Smav	int i;
270209371Smav	uint32_t val;
271209371Smav
272209371Smav	val = bus_read_4(sc->mem_res, HPET_ISR);
273209371Smav	if (val) {
274209371Smav		bus_write_4(sc->mem_res, HPET_ISR, val);
275209371Smav		val &= sc->useirq;
276209371Smav		for (i = 0; i < sc->num_timers; i++) {
277209371Smav			if ((val & (1 << i)) == 0)
278209371Smav				continue;
279209371Smav			hpet_intr_single(&sc->t[i]);
280209371Smav		}
281209371Smav		return (FILTER_HANDLED);
282209371Smav	}
283209371Smav	return (FILTER_STRAY);
284209371Smav}
285209371Smav
286208436Smavstatic ACPI_STATUS
287209371Smavhpet_find(ACPI_HANDLE handle, UINT32 level, void *context,
288208436Smav    void **status)
289208436Smav{
290208436Smav	char 		**ids;
291208436Smav	uint32_t	id = (uint32_t)(uintptr_t)context;
292208438Smav	uint32_t	uid = 0;
293208436Smav
294208436Smav	for (ids = hpet_ids; *ids != NULL; ids++) {
295208436Smav		if (acpi_MatchHid(handle, *ids))
296208436Smav		        break;
297208436Smav	}
298208436Smav	if (*ids == NULL)
299208436Smav		return (AE_OK);
300209371Smav	if (ACPI_FAILURE(acpi_GetInteger(handle, "_UID", &uid)) ||
301209371Smav	    id == uid)
302208436Smav		*((int *)status) = 1;
303208436Smav	return (AE_OK);
304208436Smav}
305208436Smav
306169592Snjl/* Discover the HPET via the ACPI table of the same name. */
307172489Snjlstatic void
308209371Smavhpet_identify(driver_t *driver, device_t parent)
309169574Stakawata{
310169574Stakawata	ACPI_TABLE_HPET *hpet;
311169574Stakawata	ACPI_STATUS	status;
312169574Stakawata	device_t	child;
313208436Smav	int 		i, found;
314169574Stakawata
315172489Snjl	/* Only one HPET device can be added. */
316209371Smav	if (devclass_get_device(hpet_devclass, 0))
317172489Snjl		return;
318208436Smav	for (i = 1; ; i++) {
319208436Smav		/* Search for HPET table. */
320208436Smav		status = AcpiGetTable(ACPI_SIG_HPET, i, (ACPI_TABLE_HEADER **)&hpet);
321208436Smav		if (ACPI_FAILURE(status))
322208436Smav			return;
323208436Smav		/* Search for HPET device with same ID. */
324208436Smav		found = 0;
325208436Smav		AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
326209371Smav		    100, hpet_find, NULL, (void *)(uintptr_t)hpet->Sequence, (void *)&found);
327208436Smav		/* If found - let it be probed in normal way. */
328208436Smav		if (found)
329208436Smav			continue;
330208436Smav		/* If not - create it from table info. */
331209371Smav		child = BUS_ADD_CHILD(parent, ACPI_DEV_BASE_ORDER, "hpet", 0);
332208436Smav		if (child == NULL) {
333208436Smav			printf("%s: can't add child\n", __func__);
334208436Smav			continue;
335208436Smav		}
336208436Smav		bus_set_resource(child, SYS_RES_MEMORY, 0, hpet->Address.Address,
337208436Smav		    HPET_MEM_WIDTH);
338169574Stakawata	}
339169574Stakawata}
340169574Stakawata
341151912Sphkstatic int
342209371Smavhpet_probe(device_t dev)
343151912Sphk{
344159217Snjl	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
345159217Snjl
346169592Snjl	if (acpi_disabled("hpet"))
347151912Sphk		return (ENXIO);
348199016Savg	if (acpi_get_handle(dev) != NULL &&
349208436Smav	    ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL)
350169592Snjl		return (ENXIO);
351151912Sphk
352159217Snjl	device_set_desc(dev, "High Precision Event Timer");
353151912Sphk	return (0);
354151912Sphk}
355151912Sphk
356151912Sphkstatic int
357209371Smavhpet_attach(device_t dev)
358151912Sphk{
359209371Smav	struct hpet_softc *sc;
360209371Smav	struct hpet_timer *t;
361209371Smav	int i, j, num_msi, num_timers, num_percpu_et, num_percpu_t, cur_cpu;
362209371Smav	int pcpu_master;
363209371Smav	static int maxhpetet = 0;
364212238Smav	uint32_t val, val2, cvectors, dvectors;
365209371Smav	uint16_t vendor, rev;
366151912Sphk
367151912Sphk	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
368151912Sphk
369151912Sphk	sc = device_get_softc(dev);
370151912Sphk	sc->dev = dev;
371151912Sphk	sc->handle = acpi_get_handle(dev);
372151912Sphk
373209371Smav	sc->mem_rid = 0;
374209371Smav	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
375159217Snjl	    RF_ACTIVE);
376159217Snjl	if (sc->mem_res == NULL)
377159217Snjl		return (ENOMEM);
378151912Sphk
379159217Snjl	/* Validate that we can access the whole region. */
380159217Snjl	if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH) {
381159217Snjl		device_printf(dev, "memory region width %ld too small\n",
382159217Snjl		    rman_get_size(sc->mem_res));
383159217Snjl		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
384159217Snjl		return (ENXIO);
385159217Snjl	}
386151912Sphk
387171547Snjl	/* Be sure timer is enabled. */
388175361Sjhb	hpet_enable(sc);
389171547Snjl
390159217Snjl	/* Read basic statistics about the timer. */
391175385Sjhb	val = bus_read_4(sc->mem_res, HPET_PERIOD);
392175361Sjhb	if (val == 0) {
393175361Sjhb		device_printf(dev, "invalid period\n");
394175361Sjhb		hpet_disable(sc);
395175361Sjhb		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
396175361Sjhb		return (ENXIO);
397175361Sjhb	}
398175361Sjhb
399209371Smav	sc->freq = (1000000000000000LL + val / 2) / val;
400209440Smav	sc->caps = bus_read_4(sc->mem_res, HPET_CAPABILITIES);
401209440Smav	vendor = (sc->caps & HPET_CAP_VENDOR_ID) >> 16;
402209440Smav	rev = sc->caps & HPET_CAP_REV_ID;
403209440Smav	num_timers = 1 + ((sc->caps & HPET_CAP_NUM_TIM) >> 8);
404209371Smav	/*
405209371Smav	 * ATI/AMD violates IA-PC HPET (High Precision Event Timers)
406209371Smav	 * Specification and provides an off by one number
407209371Smav	 * of timers/comparators.
408209371Smav	 * Additionally, they use unregistered value in VENDOR_ID field.
409209371Smav	 */
410209371Smav	if (vendor == HPET_VENDID_AMD && rev < 0x10 && num_timers > 0)
411209371Smav		num_timers--;
412209371Smav	sc->num_timers = num_timers;
413159217Snjl	if (bootverbose) {
414159217Snjl		device_printf(dev,
415209371Smav		    "vendor 0x%x, rev 0x%x, %jdHz%s, %d timers,%s\n",
416209440Smav		    vendor, rev, sc->freq,
417209440Smav		    (sc->caps & HPET_CAP_COUNT_SIZE) ? " 64bit" : "",
418209440Smav		    num_timers,
419209440Smav		    (sc->caps & HPET_CAP_LEG_RT) ? " legacy route" : "");
420159217Snjl	}
421209371Smav	for (i = 0; i < num_timers; i++) {
422209371Smav		t = &sc->t[i];
423209371Smav		t->sc = sc;
424209371Smav		t->num = i;
425209371Smav		t->mode = 0;
426209371Smav		t->intr_rid = -1;
427209371Smav		t->irq = -1;
428212323Smav		t->pcpu_cpu = -1;
429212323Smav		t->pcpu_misrouted = 0;
430209371Smav		t->pcpu_master = -1;
431209371Smav		t->caps = bus_read_4(sc->mem_res, HPET_TIMER_CAP_CNF(i));
432209371Smav		t->vectors = bus_read_4(sc->mem_res, HPET_TIMER_CAP_CNF(i) + 4);
433209371Smav		if (bootverbose) {
434209371Smav			device_printf(dev,
435209371Smav			    " t%d: irqs 0x%08x (%d)%s%s%s\n", i,
436209371Smav			    t->vectors, (t->caps & HPET_TCNF_INT_ROUTE) >> 9,
437209371Smav			    (t->caps & HPET_TCAP_FSB_INT_DEL) ? ", MSI" : "",
438209371Smav			    (t->caps & HPET_TCAP_SIZE) ? ", 64bit" : "",
439209371Smav			    (t->caps & HPET_TCAP_PER_INT) ? ", periodic" : "");
440209371Smav		}
441209371Smav	}
442159217Snjl	if (testenv("debug.acpi.hpet_test"))
443209371Smav		hpet_test(sc);
444171547Snjl	/*
445171547Snjl	 * Don't attach if the timer never increments.  Since the spec
446171547Snjl	 * requires it to be at least 10 MHz, it has to change in 1 us.
447171547Snjl	 */
448175385Sjhb	val = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
449171547Snjl	DELAY(1);
450175385Sjhb	val2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
451171547Snjl	if (val == val2) {
452171547Snjl		device_printf(dev, "HPET never increments, disabling\n");
453175361Sjhb		hpet_disable(sc);
454171547Snjl		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
455171547Snjl		return (ENXIO);
456171547Snjl	}
457208436Smav	/* Announce first HPET as timecounter. */
458208436Smav	if (device_get_unit(dev) == 0) {
459209371Smav		sc->tc.tc_get_timecount = hpet_get_timecount,
460209371Smav		sc->tc.tc_counter_mask = ~0u,
461209371Smav		sc->tc.tc_name = "HPET",
462209371Smav		sc->tc.tc_quality = 900,
463209371Smav		sc->tc.tc_frequency = sc->freq;
464209371Smav		sc->tc.tc_priv = sc;
465209371Smav		tc_init(&sc->tc);
466208436Smav	}
467209371Smav	/* If not disabled - setup and announce event timers. */
468209371Smav	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
469209371Smav	     "clock", &i) == 0 && i == 0)
470209371Smav	        return (0);
471209440Smav
472209440Smav	/* Check whether we can and want legacy routing. */
473209440Smav	sc->legacy_route = 0;
474209440Smav	resource_int_value(device_get_name(dev), device_get_unit(dev),
475209440Smav	     "legacy_route", &sc->legacy_route);
476209440Smav	if ((sc->caps & HPET_CAP_LEG_RT) == 0)
477209440Smav		sc->legacy_route = 0;
478209440Smav	if (sc->legacy_route) {
479209440Smav		sc->t[0].vectors = 0;
480209440Smav		sc->t[1].vectors = 0;
481209440Smav	}
482209440Smav
483212238Smav	/* Check what IRQs we want use. */
484212238Smav	/* By default allow any PCI IRQs. */
485212238Smav	sc->allowed_irqs = 0xffff0000;
486209371Smav	/*
487209371Smav	 * HPETs in AMD chipsets before SB800 have problems with IRQs >= 16
488209371Smav	 * Lower are also not always working for different reasons.
489209371Smav	 * SB800 fixed it, but seems do not implements level triggering
490209371Smav	 * properly, that makes it very unreliable - it freezes after any
491209371Smav	 * interrupt loss. Avoid legacy IRQs for AMD.
492209371Smav	 */
493209371Smav	if (vendor == HPET_VENDID_AMD)
494212238Smav		sc->allowed_irqs = 0x00000000;
495212238Smav	/*
496213302Smav	 * NVidia MCP5x chipsets have number of unexplained interrupt
497213302Smav	 * problems. For some reason, using HPET interrupts breaks HDA sound.
498213302Smav	 */
499213302Smav	if (vendor == HPET_VENDID_NVIDIA && rev <= 0x01)
500213302Smav		sc->allowed_irqs = 0x00000000;
501213302Smav	/*
502212238Smav	 * Neither QEMU nor VirtualBox report supported IRQs correctly.
503212238Smav	 * The only way to use HPET there is to specify IRQs manually
504212238Smav	 * and/or use legacy_route. Legacy_route mode work on both.
505212238Smav	 */
506212238Smav	if (vm_guest)
507212238Smav		sc->allowed_irqs = 0x00000000;
508212238Smav	/* Let user override. */
509212238Smav	resource_int_value(device_get_name(dev), device_get_unit(dev),
510212238Smav	     "allowed_irqs", &sc->allowed_irqs);
511212238Smav
512212533Smav	/* Get how much per-CPU timers we should try to provide. */
513212533Smav	sc->per_cpu = 1;
514212533Smav	resource_int_value(device_get_name(dev), device_get_unit(dev),
515212533Smav	     "per_cpu", &sc->per_cpu);
516212533Smav
517212238Smav	num_msi = 0;
518212238Smav	sc->useirq = 0;
519212238Smav	/* Find IRQ vectors for all timers. */
520212238Smav	cvectors = sc->allowed_irqs & 0xffff0000;
521212238Smav	dvectors = sc->allowed_irqs & 0x0000ffff;
522212238Smav	if (sc->legacy_route)
523212238Smav		dvectors &= 0x0000fefe;
524209371Smav	for (i = 0; i < num_timers; i++) {
525209371Smav		t = &sc->t[i];
526209440Smav		if (sc->legacy_route && i < 2)
527209440Smav			t->irq = (i == 0) ? 0 : 8;
528209371Smav#ifdef DEV_APIC
529209440Smav		else if (t->caps & HPET_TCAP_FSB_INT_DEL) {
530209371Smav			if ((j = PCIB_ALLOC_MSIX(
531209371Smav			    device_get_parent(device_get_parent(dev)), dev,
532209371Smav			    &t->irq))) {
533209371Smav				device_printf(dev,
534209440Smav				    "Can't allocate interrupt for t%d.\n", j);
535209440Smav			}
536209440Smav		}
537209440Smav#endif
538212238Smav		else if (dvectors & t->vectors) {
539212238Smav			t->irq = ffs(dvectors & t->vectors) - 1;
540212238Smav			dvectors &= ~(1 << t->irq);
541212238Smav		}
542209440Smav		if (t->irq >= 0) {
543209440Smav			if (!(t->intr_res =
544209371Smav			    bus_alloc_resource(dev, SYS_RES_IRQ, &t->intr_rid,
545209440Smav			    t->irq, t->irq, 1, RF_ACTIVE))) {
546209440Smav				t->irq = -1;
547209440Smav				device_printf(dev,
548209440Smav				    "Can't map interrupt for t%d.\n", i);
549209371Smav			} else if ((bus_setup_intr(dev, t->intr_res,
550209371Smav			    INTR_MPSAFE | INTR_TYPE_CLK,
551209371Smav			    (driver_filter_t *)hpet_intr_single, NULL,
552209371Smav			    t, &t->intr_handle))) {
553209440Smav				t->irq = -1;
554209440Smav				device_printf(dev,
555209440Smav				    "Can't setup interrupt for t%d.\n", i);
556209371Smav			} else {
557209371Smav				bus_describe_intr(dev, t->intr_res,
558209371Smav				    t->intr_handle, "t%d", i);
559209371Smav				num_msi++;
560209371Smav			}
561209440Smav		}
562209440Smav		if (t->irq < 0 && (cvectors & t->vectors) != 0) {
563209371Smav			cvectors &= t->vectors;
564209371Smav			sc->useirq |= (1 << i);
565209371Smav		}
566209371Smav	}
567209440Smav	if (sc->legacy_route && sc->t[0].irq < 0 && sc->t[1].irq < 0)
568209440Smav		sc->legacy_route = 0;
569209440Smav	if (sc->legacy_route)
570209440Smav		hpet_enable(sc);
571209440Smav	/* Group timers for per-CPU operation. */
572212533Smav	num_percpu_et = min(num_msi / mp_ncpus, sc->per_cpu);
573209440Smav	num_percpu_t = num_percpu_et * mp_ncpus;
574209440Smav	pcpu_master = 0;
575209440Smav	cur_cpu = CPU_FIRST();
576209440Smav	for (i = 0; i < num_timers; i++) {
577209440Smav		t = &sc->t[i];
578209440Smav		if (t->irq >= 0 && num_percpu_t > 0) {
579209440Smav			if (cur_cpu == CPU_FIRST())
580209440Smav				pcpu_master = i;
581212323Smav			t->pcpu_cpu = cur_cpu;
582209440Smav			t->pcpu_master = pcpu_master;
583209440Smav			sc->t[pcpu_master].
584209440Smav			    pcpu_slaves[cur_cpu] = i;
585209440Smav			bus_bind_intr(dev, t->intr_res, cur_cpu);
586209440Smav			cur_cpu = CPU_NEXT(cur_cpu);
587209440Smav			num_percpu_t--;
588212238Smav		} else if (t->irq >= 0)
589212238Smav			bus_bind_intr(dev, t->intr_res, CPU_FIRST());
590209440Smav	}
591209371Smav	bus_write_4(sc->mem_res, HPET_ISR, 0xffffffff);
592209371Smav	sc->irq = -1;
593209371Smav	sc->intr_rid = -1;
594209371Smav	/* If at least one timer needs legacy IRQ - setup it. */
595209371Smav	if (sc->useirq) {
596209371Smav		j = i = fls(cvectors) - 1;
597209371Smav		while (j > 0 && (cvectors & (1 << (j - 1))) != 0)
598209371Smav			j--;
599209371Smav		if (!(sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
600209371Smav		    &sc->intr_rid, j, i, 1, RF_SHAREABLE | RF_ACTIVE)))
601209371Smav			device_printf(dev,"Can't map interrupt.\n");
602209371Smav		else if ((bus_setup_intr(dev, sc->intr_res,
603209371Smav		    INTR_MPSAFE | INTR_TYPE_CLK,
604209371Smav		    (driver_filter_t *)hpet_intr, NULL,
605209371Smav		    sc, &sc->intr_handle))) {
606209371Smav			device_printf(dev, "Can't setup interrupt.\n");
607209371Smav		} else {
608209371Smav			sc->irq = rman_get_start(sc->intr_res);
609209371Smav			/* Bind IRQ to BSP to avoid live migration. */
610209371Smav			bus_bind_intr(dev, sc->intr_res, CPU_FIRST());
611209371Smav		}
612209371Smav	}
613209371Smav	/* Program and announce event timers. */
614209371Smav	for (i = 0; i < num_timers; i++) {
615209371Smav		t = &sc->t[i];
616209371Smav		t->caps &= ~(HPET_TCNF_FSB_EN | HPET_TCNF_INT_ROUTE);
617209371Smav		t->caps &= ~(HPET_TCNF_VAL_SET | HPET_TCNF_INT_ENB);
618209440Smav		t->caps &= ~(HPET_TCNF_INT_TYPE);
619209371Smav		t->caps |= HPET_TCNF_32MODE;
620209440Smav		if (t->irq >= 0 && sc->legacy_route && i < 2) {
621209440Smav			/* Legacy route doesn't need more configuration. */
622209440Smav		} else
623209371Smav#ifdef DEV_APIC
624212238Smav		if ((t->caps & HPET_TCAP_FSB_INT_DEL) && t->irq >= 0) {
625209371Smav			uint64_t addr;
626209371Smav			uint32_t data;
627209371Smav
628209371Smav			if (PCIB_MAP_MSI(
629209371Smav			    device_get_parent(device_get_parent(dev)), dev,
630209371Smav			    t->irq, &addr, &data) == 0) {
631209371Smav				bus_write_4(sc->mem_res,
632209371Smav				    HPET_TIMER_FSB_ADDR(i), addr);
633209371Smav				bus_write_4(sc->mem_res,
634209371Smav				    HPET_TIMER_FSB_VAL(i), data);
635209371Smav				t->caps |= HPET_TCNF_FSB_EN;
636209371Smav			} else
637209371Smav				t->irq = -2;
638209371Smav		} else
639209371Smav#endif
640212238Smav		if (t->irq >= 0)
641212238Smav			t->caps |= (t->irq << 9);
642212238Smav		else if (sc->irq >= 0 && (t->vectors & (1 << sc->irq)))
643209371Smav			t->caps |= (sc->irq << 9) | HPET_TCNF_INT_TYPE;
644209371Smav		bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(i), t->caps);
645209371Smav		/* Skip event timers without set up IRQ. */
646209371Smav		if (t->irq < 0 &&
647209371Smav		    (sc->irq < 0 || (t->vectors & (1 << sc->irq)) == 0))
648209371Smav			continue;
649209371Smav		/* Announce the reset. */
650209371Smav		if (maxhpetet == 0)
651209371Smav			t->et.et_name = "HPET";
652209371Smav		else {
653209371Smav			sprintf(t->name, "HPET%d", maxhpetet);
654209371Smav			t->et.et_name = t->name;
655209371Smav		}
656209371Smav		t->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT;
657209371Smav		t->et.et_quality = 450;
658209371Smav		if (t->pcpu_master >= 0) {
659209371Smav			t->et.et_flags |= ET_FLAGS_PERCPU;
660209371Smav			t->et.et_quality += 100;
661209371Smav		}
662209371Smav		if ((t->caps & HPET_TCAP_PER_INT) == 0)
663209371Smav			t->et.et_quality -= 10;
664209371Smav		t->et.et_frequency = sc->freq;
665210290Smav		t->et.et_min_period.sec = 0;
666212238Smav		t->et.et_min_period.frac = 0x00008000LLU << 32;
667210298Smav		t->et.et_max_period.sec = 0xfffffffeLLU / sc->freq;
668210290Smav		t->et.et_max_period.frac =
669210298Smav		    ((0xfffffffeLLU << 32) / sc->freq) << 32;
670209371Smav		t->et.et_start = hpet_start;
671209371Smav		t->et.et_stop = hpet_stop;
672209371Smav		t->et.et_priv = &sc->t[i];
673209371Smav		if (t->pcpu_master < 0 || t->pcpu_master == i) {
674209371Smav			et_register(&t->et);
675209371Smav			maxhpetet++;
676209371Smav		}
677209371Smav	}
678159217Snjl	return (0);
679159217Snjl}
680159217Snjl
681159217Snjlstatic int
682209371Smavhpet_detach(device_t dev)
683159217Snjl{
684159217Snjl	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
685159217Snjl
686159217Snjl	/* XXX Without a tc_remove() function, we can't detach. */
687159217Snjl	return (EBUSY);
688159217Snjl}
689159217Snjl
690168010Snjlstatic int
691209371Smavhpet_suspend(device_t dev)
692175361Sjhb{
693212541Smav//	struct hpet_softc *sc;
694175361Sjhb
695175361Sjhb	/*
696175361Sjhb	 * Disable the timer during suspend.  The timer will not lose
697175361Sjhb	 * its state in S1 or S2, but we are required to disable
698175361Sjhb	 * it.
699175361Sjhb	 */
700212541Smav//	sc = device_get_softc(dev);
701212541Smav//	hpet_disable(sc);
702175361Sjhb
703175361Sjhb	return (0);
704175361Sjhb}
705175361Sjhb
706175361Sjhbstatic int
707209371Smavhpet_resume(device_t dev)
708168010Snjl{
709209371Smav	struct hpet_softc *sc;
710209371Smav	struct hpet_timer *t;
711209371Smav	int i;
712168010Snjl
713168010Snjl	/* Re-enable the timer after a resume to keep the clock advancing. */
714168010Snjl	sc = device_get_softc(dev);
715175361Sjhb	hpet_enable(sc);
716209371Smav	/* Restart event timers that were running on suspend. */
717209371Smav	for (i = 0; i < sc->num_timers; i++) {
718209371Smav		t = &sc->t[i];
719209371Smav#ifdef DEV_APIC
720209440Smav		if (t->irq >= 0 && (sc->legacy_route == 0 || i >= 2)) {
721209371Smav			uint64_t addr;
722209371Smav			uint32_t data;
723209371Smav
724209371Smav			if (PCIB_MAP_MSI(
725209371Smav			    device_get_parent(device_get_parent(dev)), dev,
726209371Smav			    t->irq, &addr, &data) == 0) {
727209371Smav				bus_write_4(sc->mem_res,
728209371Smav				    HPET_TIMER_FSB_ADDR(i), addr);
729209371Smav				bus_write_4(sc->mem_res,
730209371Smav				    HPET_TIMER_FSB_VAL(i), data);
731209371Smav			}
732209371Smav		}
733209371Smav#endif
734209371Smav		if (t->mode == 0)
735209371Smav			continue;
736212491Smav		t->next = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
737209371Smav		if (t->mode == 1 && (t->caps & HPET_TCAP_PER_INT)) {
738209371Smav			t->caps |= HPET_TCNF_TYPE;
739212491Smav			t->next += t->div;
740209371Smav			bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
741209371Smav			    t->caps | HPET_TCNF_VAL_SET);
742209371Smav			bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
743212491Smav			    t->next);
744209371Smav			bus_read_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num));
745209371Smav			bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
746209371Smav			    t->div);
747209371Smav		} else {
748212491Smav			t->next += sc->freq / 1024;
749209371Smav			bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
750212491Smav			    t->next);
751209371Smav		}
752209371Smav		bus_write_4(sc->mem_res, HPET_ISR, 1 << t->num);
753209371Smav		bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), t->caps);
754209371Smav	}
755168010Snjl	return (0);
756168010Snjl}
757168010Snjl
758159217Snjl/* Print some basic latency/rate information to assist in debugging. */
759159217Snjlstatic void
760209371Smavhpet_test(struct hpet_softc *sc)
761159217Snjl{
762151912Sphk	int i;
763151912Sphk	uint32_t u1, u2;
764151912Sphk	struct bintime b0, b1, b2;
765151912Sphk	struct timespec ts;
766151912Sphk
767151912Sphk	binuptime(&b0);
768151912Sphk	binuptime(&b0);
769151912Sphk	binuptime(&b1);
770175385Sjhb	u1 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
771151912Sphk	for (i = 1; i < 1000; i++)
772175385Sjhb		u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
773151912Sphk	binuptime(&b2);
774175385Sjhb	u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
775151912Sphk
776151912Sphk	bintime_sub(&b2, &b1);
777151912Sphk	bintime_sub(&b1, &b0);
778151912Sphk	bintime_sub(&b2, &b1);
779151912Sphk	bintime2timespec(&b2, &ts);
780151912Sphk
781159217Snjl	device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
782151912Sphk	    (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1);
783151912Sphk
784159217Snjl	device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);
785151912Sphk}
786151912Sphk
787209371Smav#ifdef DEV_APIC
788209371Smavstatic int
789209371Smavhpet_remap_intr(device_t dev, device_t child, u_int irq)
790209371Smav{
791209371Smav	struct hpet_softc *sc = device_get_softc(dev);
792209371Smav	struct hpet_timer *t;
793209371Smav	uint64_t addr;
794209371Smav	uint32_t data;
795209371Smav	int error, i;
796209371Smav
797209371Smav	for (i = 0; i < sc->num_timers; i++) {
798209371Smav		t = &sc->t[i];
799209371Smav		if (t->irq != irq)
800209371Smav			continue;
801209371Smav		error = PCIB_MAP_MSI(
802209371Smav		    device_get_parent(device_get_parent(dev)), dev,
803209371Smav		    irq, &addr, &data);
804209371Smav		if (error)
805209371Smav			return (error);
806209371Smav		hpet_disable(sc); /* Stop timer to avoid interrupt loss. */
807209371Smav		bus_write_4(sc->mem_res, HPET_TIMER_FSB_ADDR(i), addr);
808209371Smav		bus_write_4(sc->mem_res, HPET_TIMER_FSB_VAL(i), data);
809209371Smav		hpet_enable(sc);
810209371Smav		return (0);
811209371Smav	}
812209371Smav	return (ENOENT);
813209371Smav}
814209371Smav#endif
815209371Smav
816209371Smavstatic device_method_t hpet_methods[] = {
817151912Sphk	/* Device interface */
818209371Smav	DEVMETHOD(device_identify, hpet_identify),
819209371Smav	DEVMETHOD(device_probe, hpet_probe),
820209371Smav	DEVMETHOD(device_attach, hpet_attach),
821209371Smav	DEVMETHOD(device_detach, hpet_detach),
822209371Smav	DEVMETHOD(device_suspend, hpet_suspend),
823209371Smav	DEVMETHOD(device_resume, hpet_resume),
824151912Sphk
825209371Smav#ifdef DEV_APIC
826209371Smav	DEVMETHOD(bus_remap_intr, hpet_remap_intr),
827209371Smav#endif
828209371Smav
829151912Sphk	{0, 0}
830151912Sphk};
831151912Sphk
832209371Smavstatic driver_t	hpet_driver = {
833209371Smav	"hpet",
834209371Smav	hpet_methods,
835209371Smav	sizeof(struct hpet_softc),
836151912Sphk};
837151912Sphk
838209371SmavDRIVER_MODULE(hpet, acpi, hpet_driver, hpet_devclass, 0, 0);
839209371SmavMODULE_DEPEND(hpet, acpi, 1, 1, 1);
840