acpi_hpet.c revision 232797
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 232797 2012-03-10 21:08:07Z 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
62232797Smav#define HPET_VENDID_SW		0x1166
63203062Savg
64151912SphkACPI_SERIAL_DECL(hpet, "ACPI HPET support");
65151912Sphk
66209371Smavstatic devclass_t hpet_devclass;
67169574Stakawata
68151931Sscottl/* ACPI CA debugging */
69151935Sscottl#define _COMPONENT	ACPI_TIMER
70151931SscottlACPI_MODULE_NAME("HPET")
71151931Sscottl
72209371Smavstruct hpet_softc {
73151912Sphk	device_t		dev;
74209371Smav	int			mem_rid;
75209371Smav	int			intr_rid;
76209371Smav	int			irq;
77209371Smav	int			useirq;
78209440Smav	int			legacy_route;
79212533Smav	int			per_cpu;
80212238Smav	uint32_t		allowed_irqs;
81159217Snjl	struct resource		*mem_res;
82209371Smav	struct resource		*intr_res;
83209371Smav	void			*intr_handle;
84151912Sphk	ACPI_HANDLE		handle;
85209371Smav	uint64_t		freq;
86209440Smav	uint32_t		caps;
87209371Smav	struct timecounter	tc;
88209371Smav	struct hpet_timer {
89209371Smav		struct eventtimer	et;
90209371Smav		struct hpet_softc	*sc;
91209371Smav		int			num;
92209371Smav		int			mode;
93209371Smav		int			intr_rid;
94209371Smav		int			irq;
95212323Smav		int			pcpu_cpu;
96212323Smav		int			pcpu_misrouted;
97209371Smav		int			pcpu_master;
98209371Smav		int			pcpu_slaves[MAXCPU];
99209371Smav		struct resource		*intr_res;
100209371Smav		void			*intr_handle;
101209371Smav		uint32_t		caps;
102209371Smav		uint32_t		vectors;
103209371Smav		uint32_t		div;
104212491Smav		uint32_t		next;
105209371Smav		char			name[8];
106209371Smav	} 			t[32];
107209371Smav	int			num_timers;
108151912Sphk};
109151912Sphk
110159217Snjlstatic u_int hpet_get_timecount(struct timecounter *tc);
111209371Smavstatic void hpet_test(struct hpet_softc *sc);
112151912Sphk
113159217Snjlstatic char *hpet_ids[] = { "PNP0103", NULL };
114159217Snjl
115159217Snjlstatic u_int
116151912Sphkhpet_get_timecount(struct timecounter *tc)
117151912Sphk{
118209371Smav	struct hpet_softc *sc;
119151912Sphk
120151912Sphk	sc = tc->tc_priv;
121175385Sjhb	return (bus_read_4(sc->mem_res, HPET_MAIN_COUNTER));
122151912Sphk}
123151912Sphk
124175361Sjhbstatic void
125209371Smavhpet_enable(struct hpet_softc *sc)
126175361Sjhb{
127175361Sjhb	uint32_t val;
128175385Sjhb
129175385Sjhb	val = bus_read_4(sc->mem_res, HPET_CONFIG);
130209440Smav	if (sc->legacy_route)
131209440Smav		val |= HPET_CNF_LEG_RT;
132209440Smav	else
133209440Smav		val &= ~HPET_CNF_LEG_RT;
134185103Sjkim	val |= HPET_CNF_ENABLE;
135185103Sjkim	bus_write_4(sc->mem_res, HPET_CONFIG, val);
136175361Sjhb}
137175361Sjhb
138175361Sjhbstatic void
139209371Smavhpet_disable(struct hpet_softc *sc)
140175361Sjhb{
141175361Sjhb	uint32_t val;
142175385Sjhb
143175385Sjhb	val = bus_read_4(sc->mem_res, HPET_CONFIG);
144185103Sjkim	val &= ~HPET_CNF_ENABLE;
145185103Sjkim	bus_write_4(sc->mem_res, HPET_CONFIG, val);
146175361Sjhb}
147175361Sjhb
148209371Smavstatic int
149209371Smavhpet_start(struct eventtimer *et,
150209371Smav    struct bintime *first, struct bintime *period)
151209371Smav{
152209371Smav	struct hpet_timer *mt = (struct hpet_timer *)et->et_priv;
153209371Smav	struct hpet_timer *t;
154209371Smav	struct hpet_softc *sc = mt->sc;
155212491Smav	uint32_t fdiv, now;
156209371Smav
157209371Smav	t = (mt->pcpu_master < 0) ? mt : &sc->t[mt->pcpu_slaves[curcpu]];
158209371Smav	if (period != NULL) {
159209371Smav		t->mode = 1;
160209371Smav		t->div = (sc->freq * (period->frac >> 32)) >> 32;
161209371Smav		if (period->sec != 0)
162209371Smav			t->div += sc->freq * period->sec;
163209371Smav	} else {
164209371Smav		t->mode = 2;
165209371Smav		t->div = 0;
166209371Smav	}
167210290Smav	if (first != NULL) {
168210290Smav		fdiv = (sc->freq * (first->frac >> 32)) >> 32;
169210290Smav		if (first->sec != 0)
170210290Smav			fdiv += sc->freq * first->sec;
171210290Smav	} else
172210290Smav		fdiv = t->div;
173212238Smav	if (t->irq < 0)
174212238Smav		bus_write_4(sc->mem_res, HPET_ISR, 1 << t->num);
175212238Smav	t->caps |= HPET_TCNF_INT_ENB;
176212491Smav	now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
177212238Smavrestart:
178212491Smav	t->next = now + fdiv;
179209371Smav	if (t->mode == 1 && (t->caps & HPET_TCAP_PER_INT)) {
180209371Smav		t->caps |= HPET_TCNF_TYPE;
181209371Smav		bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
182209371Smav		    t->caps | HPET_TCNF_VAL_SET);
183212491Smav		bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
184212491Smav		    t->next);
185212491Smav		bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
186212491Smav		    t->div);
187209371Smav	} else {
188212238Smav		t->caps &= ~HPET_TCNF_TYPE;
189212491Smav		bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
190212491Smav		    t->caps);
191212491Smav		bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
192212491Smav		    t->next);
193209371Smav	}
194224919Smav	now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
195224919Smav	if ((int32_t)(now - t->next + HPET_MIN_CYCLES) >= 0) {
196224919Smav		fdiv *= 2;
197224919Smav		goto restart;
198212238Smav	}
199209371Smav	return (0);
200209371Smav}
201209371Smav
202209371Smavstatic int
203209371Smavhpet_stop(struct eventtimer *et)
204209371Smav{
205209371Smav	struct hpet_timer *mt = (struct hpet_timer *)et->et_priv;
206209371Smav	struct hpet_timer *t;
207209371Smav	struct hpet_softc *sc = mt->sc;
208209371Smav
209209371Smav	t = (mt->pcpu_master < 0) ? mt : &sc->t[mt->pcpu_slaves[curcpu]];
210209371Smav	t->mode = 0;
211209371Smav	t->caps &= ~(HPET_TCNF_INT_ENB | HPET_TCNF_TYPE);
212209371Smav	bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), t->caps);
213209371Smav	return (0);
214209371Smav}
215209371Smav
216209371Smavstatic int
217209371Smavhpet_intr_single(void *arg)
218209371Smav{
219209371Smav	struct hpet_timer *t = (struct hpet_timer *)arg;
220209371Smav	struct hpet_timer *mt;
221209371Smav	struct hpet_softc *sc = t->sc;
222209371Smav	uint32_t now;
223209371Smav
224212491Smav	if (t->mode == 0)
225212491Smav		return (FILTER_STRAY);
226212323Smav	/* Check that per-CPU timer interrupt reached right CPU. */
227212323Smav	if (t->pcpu_cpu >= 0 && t->pcpu_cpu != curcpu) {
228212323Smav		if ((++t->pcpu_misrouted) % 32 == 0) {
229212323Smav			printf("HPET interrupt routed to the wrong CPU"
230212323Smav			    " (timer %d CPU %d -> %d)!\n",
231212323Smav			    t->num, t->pcpu_cpu, curcpu);
232212323Smav		}
233212323Smav
234212323Smav		/*
235212323Smav		 * Reload timer, hoping that next time may be more lucky
236212323Smav		 * (system will manage proper interrupt binding).
237212323Smav		 */
238212323Smav		if ((t->mode == 1 && (t->caps & HPET_TCAP_PER_INT) == 0) ||
239212323Smav		    t->mode == 2) {
240212491Smav			t->next = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER) +
241212491Smav			    sc->freq / 8;
242212323Smav			bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
243212491Smav			    t->next);
244212323Smav		}
245212323Smav		return (FILTER_HANDLED);
246212323Smav	}
247209371Smav	if (t->mode == 1 &&
248209371Smav	    (t->caps & HPET_TCAP_PER_INT) == 0) {
249212491Smav		t->next += t->div;
250209371Smav		now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
251212491Smav		if ((int32_t)((now + t->div / 2) - t->next) > 0)
252212491Smav			t->next = now + t->div / 2;
253209371Smav		bus_write_4(sc->mem_res,
254212491Smav		    HPET_TIMER_COMPARATOR(t->num), t->next);
255209371Smav	} else if (t->mode == 2)
256209371Smav		t->mode = 0;
257209371Smav	mt = (t->pcpu_master < 0) ? t : &sc->t[t->pcpu_master];
258209990Smav	if (mt->et.et_active)
259209990Smav		mt->et.et_event_cb(&mt->et, mt->et.et_arg);
260209371Smav	return (FILTER_HANDLED);
261209371Smav}
262209371Smav
263209371Smavstatic int
264209371Smavhpet_intr(void *arg)
265209371Smav{
266209371Smav	struct hpet_softc *sc = (struct hpet_softc *)arg;
267209371Smav	int i;
268209371Smav	uint32_t val;
269209371Smav
270209371Smav	val = bus_read_4(sc->mem_res, HPET_ISR);
271209371Smav	if (val) {
272209371Smav		bus_write_4(sc->mem_res, HPET_ISR, val);
273209371Smav		val &= sc->useirq;
274209371Smav		for (i = 0; i < sc->num_timers; i++) {
275209371Smav			if ((val & (1 << i)) == 0)
276209371Smav				continue;
277209371Smav			hpet_intr_single(&sc->t[i]);
278209371Smav		}
279209371Smav		return (FILTER_HANDLED);
280209371Smav	}
281209371Smav	return (FILTER_STRAY);
282209371Smav}
283209371Smav
284208436Smavstatic ACPI_STATUS
285209371Smavhpet_find(ACPI_HANDLE handle, UINT32 level, void *context,
286208436Smav    void **status)
287208436Smav{
288208436Smav	char 		**ids;
289208436Smav	uint32_t	id = (uint32_t)(uintptr_t)context;
290208438Smav	uint32_t	uid = 0;
291208436Smav
292208436Smav	for (ids = hpet_ids; *ids != NULL; ids++) {
293208436Smav		if (acpi_MatchHid(handle, *ids))
294208436Smav		        break;
295208436Smav	}
296208436Smav	if (*ids == NULL)
297208436Smav		return (AE_OK);
298209371Smav	if (ACPI_FAILURE(acpi_GetInteger(handle, "_UID", &uid)) ||
299209371Smav	    id == uid)
300208436Smav		*((int *)status) = 1;
301208436Smav	return (AE_OK);
302208436Smav}
303208436Smav
304216263Sjhb/*
305216263Sjhb * Find an existing IRQ resource that matches the requested IRQ range
306216263Sjhb * and return its RID.  If one is not found, use a new RID.
307216263Sjhb */
308216263Sjhbstatic int
309216263Sjhbhpet_find_irq_rid(device_t dev, u_long start, u_long end)
310216263Sjhb{
311216263Sjhb	u_long irq;
312216263Sjhb	int error, rid;
313216263Sjhb
314216263Sjhb	for (rid = 0;; rid++) {
315216263Sjhb		error = bus_get_resource(dev, SYS_RES_IRQ, rid, &irq, NULL);
316216263Sjhb		if (error != 0 || (start <= irq && irq <= end))
317216263Sjhb			return (rid);
318216263Sjhb	}
319216263Sjhb}
320216263Sjhb
321169592Snjl/* Discover the HPET via the ACPI table of the same name. */
322172489Snjlstatic void
323209371Smavhpet_identify(driver_t *driver, device_t parent)
324169574Stakawata{
325169574Stakawata	ACPI_TABLE_HPET *hpet;
326169574Stakawata	ACPI_STATUS	status;
327169574Stakawata	device_t	child;
328208436Smav	int 		i, found;
329169574Stakawata
330172489Snjl	/* Only one HPET device can be added. */
331209371Smav	if (devclass_get_device(hpet_devclass, 0))
332172489Snjl		return;
333208436Smav	for (i = 1; ; i++) {
334208436Smav		/* Search for HPET table. */
335208436Smav		status = AcpiGetTable(ACPI_SIG_HPET, i, (ACPI_TABLE_HEADER **)&hpet);
336208436Smav		if (ACPI_FAILURE(status))
337208436Smav			return;
338208436Smav		/* Search for HPET device with same ID. */
339208436Smav		found = 0;
340208436Smav		AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
341209371Smav		    100, hpet_find, NULL, (void *)(uintptr_t)hpet->Sequence, (void *)&found);
342208436Smav		/* If found - let it be probed in normal way. */
343208436Smav		if (found)
344208436Smav			continue;
345208436Smav		/* If not - create it from table info. */
346231161Sjkim		child = BUS_ADD_CHILD(parent, 2, "hpet", 0);
347208436Smav		if (child == NULL) {
348208436Smav			printf("%s: can't add child\n", __func__);
349208436Smav			continue;
350208436Smav		}
351208436Smav		bus_set_resource(child, SYS_RES_MEMORY, 0, hpet->Address.Address,
352208436Smav		    HPET_MEM_WIDTH);
353169574Stakawata	}
354169574Stakawata}
355169574Stakawata
356151912Sphkstatic int
357209371Smavhpet_probe(device_t dev)
358151912Sphk{
359159217Snjl	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
360159217Snjl
361169592Snjl	if (acpi_disabled("hpet"))
362151912Sphk		return (ENXIO);
363199016Savg	if (acpi_get_handle(dev) != NULL &&
364208436Smav	    ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL)
365169592Snjl		return (ENXIO);
366151912Sphk
367159217Snjl	device_set_desc(dev, "High Precision Event Timer");
368151912Sphk	return (0);
369151912Sphk}
370151912Sphk
371151912Sphkstatic int
372209371Smavhpet_attach(device_t dev)
373151912Sphk{
374209371Smav	struct hpet_softc *sc;
375209371Smav	struct hpet_timer *t;
376209371Smav	int i, j, num_msi, num_timers, num_percpu_et, num_percpu_t, cur_cpu;
377209371Smav	int pcpu_master;
378209371Smav	static int maxhpetet = 0;
379212238Smav	uint32_t val, val2, cvectors, dvectors;
380209371Smav	uint16_t vendor, rev;
381151912Sphk
382151912Sphk	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
383151912Sphk
384151912Sphk	sc = device_get_softc(dev);
385151912Sphk	sc->dev = dev;
386151912Sphk	sc->handle = acpi_get_handle(dev);
387151912Sphk
388209371Smav	sc->mem_rid = 0;
389209371Smav	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
390159217Snjl	    RF_ACTIVE);
391159217Snjl	if (sc->mem_res == NULL)
392159217Snjl		return (ENOMEM);
393151912Sphk
394159217Snjl	/* Validate that we can access the whole region. */
395159217Snjl	if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH) {
396159217Snjl		device_printf(dev, "memory region width %ld too small\n",
397159217Snjl		    rman_get_size(sc->mem_res));
398159217Snjl		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
399159217Snjl		return (ENXIO);
400159217Snjl	}
401151912Sphk
402171547Snjl	/* Be sure timer is enabled. */
403175361Sjhb	hpet_enable(sc);
404171547Snjl
405159217Snjl	/* Read basic statistics about the timer. */
406175385Sjhb	val = bus_read_4(sc->mem_res, HPET_PERIOD);
407175361Sjhb	if (val == 0) {
408175361Sjhb		device_printf(dev, "invalid period\n");
409175361Sjhb		hpet_disable(sc);
410175361Sjhb		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
411175361Sjhb		return (ENXIO);
412175361Sjhb	}
413175361Sjhb
414209371Smav	sc->freq = (1000000000000000LL + val / 2) / val;
415209440Smav	sc->caps = bus_read_4(sc->mem_res, HPET_CAPABILITIES);
416209440Smav	vendor = (sc->caps & HPET_CAP_VENDOR_ID) >> 16;
417209440Smav	rev = sc->caps & HPET_CAP_REV_ID;
418209440Smav	num_timers = 1 + ((sc->caps & HPET_CAP_NUM_TIM) >> 8);
419209371Smav	/*
420209371Smav	 * ATI/AMD violates IA-PC HPET (High Precision Event Timers)
421209371Smav	 * Specification and provides an off by one number
422209371Smav	 * of timers/comparators.
423209371Smav	 * Additionally, they use unregistered value in VENDOR_ID field.
424209371Smav	 */
425209371Smav	if (vendor == HPET_VENDID_AMD && rev < 0x10 && num_timers > 0)
426209371Smav		num_timers--;
427209371Smav	sc->num_timers = num_timers;
428159217Snjl	if (bootverbose) {
429159217Snjl		device_printf(dev,
430209371Smav		    "vendor 0x%x, rev 0x%x, %jdHz%s, %d timers,%s\n",
431209440Smav		    vendor, rev, sc->freq,
432209440Smav		    (sc->caps & HPET_CAP_COUNT_SIZE) ? " 64bit" : "",
433209440Smav		    num_timers,
434209440Smav		    (sc->caps & HPET_CAP_LEG_RT) ? " legacy route" : "");
435159217Snjl	}
436209371Smav	for (i = 0; i < num_timers; i++) {
437209371Smav		t = &sc->t[i];
438209371Smav		t->sc = sc;
439209371Smav		t->num = i;
440209371Smav		t->mode = 0;
441209371Smav		t->intr_rid = -1;
442209371Smav		t->irq = -1;
443212323Smav		t->pcpu_cpu = -1;
444212323Smav		t->pcpu_misrouted = 0;
445209371Smav		t->pcpu_master = -1;
446209371Smav		t->caps = bus_read_4(sc->mem_res, HPET_TIMER_CAP_CNF(i));
447209371Smav		t->vectors = bus_read_4(sc->mem_res, HPET_TIMER_CAP_CNF(i) + 4);
448209371Smav		if (bootverbose) {
449209371Smav			device_printf(dev,
450209371Smav			    " t%d: irqs 0x%08x (%d)%s%s%s\n", i,
451209371Smav			    t->vectors, (t->caps & HPET_TCNF_INT_ROUTE) >> 9,
452209371Smav			    (t->caps & HPET_TCAP_FSB_INT_DEL) ? ", MSI" : "",
453209371Smav			    (t->caps & HPET_TCAP_SIZE) ? ", 64bit" : "",
454209371Smav			    (t->caps & HPET_TCAP_PER_INT) ? ", periodic" : "");
455209371Smav		}
456209371Smav	}
457159217Snjl	if (testenv("debug.acpi.hpet_test"))
458209371Smav		hpet_test(sc);
459171547Snjl	/*
460171547Snjl	 * Don't attach if the timer never increments.  Since the spec
461171547Snjl	 * requires it to be at least 10 MHz, it has to change in 1 us.
462171547Snjl	 */
463175385Sjhb	val = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
464171547Snjl	DELAY(1);
465175385Sjhb	val2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
466171547Snjl	if (val == val2) {
467171547Snjl		device_printf(dev, "HPET never increments, disabling\n");
468175361Sjhb		hpet_disable(sc);
469171547Snjl		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
470171547Snjl		return (ENXIO);
471171547Snjl	}
472208436Smav	/* Announce first HPET as timecounter. */
473208436Smav	if (device_get_unit(dev) == 0) {
474209371Smav		sc->tc.tc_get_timecount = hpet_get_timecount,
475209371Smav		sc->tc.tc_counter_mask = ~0u,
476209371Smav		sc->tc.tc_name = "HPET",
477222222Sjkim		sc->tc.tc_quality = 950,
478209371Smav		sc->tc.tc_frequency = sc->freq;
479209371Smav		sc->tc.tc_priv = sc;
480209371Smav		tc_init(&sc->tc);
481208436Smav	}
482209371Smav	/* If not disabled - setup and announce event timers. */
483209371Smav	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
484209371Smav	     "clock", &i) == 0 && i == 0)
485209371Smav	        return (0);
486209440Smav
487209440Smav	/* Check whether we can and want legacy routing. */
488209440Smav	sc->legacy_route = 0;
489209440Smav	resource_int_value(device_get_name(dev), device_get_unit(dev),
490209440Smav	     "legacy_route", &sc->legacy_route);
491209440Smav	if ((sc->caps & HPET_CAP_LEG_RT) == 0)
492209440Smav		sc->legacy_route = 0;
493209440Smav	if (sc->legacy_route) {
494209440Smav		sc->t[0].vectors = 0;
495209440Smav		sc->t[1].vectors = 0;
496209440Smav	}
497209440Smav
498212238Smav	/* Check what IRQs we want use. */
499212238Smav	/* By default allow any PCI IRQs. */
500212238Smav	sc->allowed_irqs = 0xffff0000;
501209371Smav	/*
502209371Smav	 * HPETs in AMD chipsets before SB800 have problems with IRQs >= 16
503209371Smav	 * Lower are also not always working for different reasons.
504209371Smav	 * SB800 fixed it, but seems do not implements level triggering
505209371Smav	 * properly, that makes it very unreliable - it freezes after any
506209371Smav	 * interrupt loss. Avoid legacy IRQs for AMD.
507209371Smav	 */
508209371Smav	if (vendor == HPET_VENDID_AMD)
509212238Smav		sc->allowed_irqs = 0x00000000;
510212238Smav	/*
511213302Smav	 * NVidia MCP5x chipsets have number of unexplained interrupt
512213302Smav	 * problems. For some reason, using HPET interrupts breaks HDA sound.
513213302Smav	 */
514213302Smav	if (vendor == HPET_VENDID_NVIDIA && rev <= 0x01)
515213302Smav		sc->allowed_irqs = 0x00000000;
516213302Smav	/*
517232797Smav	 * ServerWorks HT1000 reported to have problems with IRQs >= 16.
518232797Smav	 * Lower IRQs are working, but allowed mask is not set correctly.
519232797Smav	 * Legacy_route mode works fine.
520232797Smav	 */
521232797Smav	if (vendor == HPET_VENDID_SW && rev <= 0x01)
522232797Smav		sc->allowed_irqs = 0x00000000;
523232797Smav	/*
524212238Smav	 * Neither QEMU nor VirtualBox report supported IRQs correctly.
525212238Smav	 * The only way to use HPET there is to specify IRQs manually
526215473Sjhb	 * and/or use legacy_route. Legacy_route mode works on both.
527212238Smav	 */
528212238Smav	if (vm_guest)
529212238Smav		sc->allowed_irqs = 0x00000000;
530212238Smav	/* Let user override. */
531212238Smav	resource_int_value(device_get_name(dev), device_get_unit(dev),
532212238Smav	     "allowed_irqs", &sc->allowed_irqs);
533212238Smav
534212533Smav	/* Get how much per-CPU timers we should try to provide. */
535212533Smav	sc->per_cpu = 1;
536212533Smav	resource_int_value(device_get_name(dev), device_get_unit(dev),
537212533Smav	     "per_cpu", &sc->per_cpu);
538212533Smav
539212238Smav	num_msi = 0;
540212238Smav	sc->useirq = 0;
541212238Smav	/* Find IRQ vectors for all timers. */
542212238Smav	cvectors = sc->allowed_irqs & 0xffff0000;
543212238Smav	dvectors = sc->allowed_irqs & 0x0000ffff;
544212238Smav	if (sc->legacy_route)
545212238Smav		dvectors &= 0x0000fefe;
546209371Smav	for (i = 0; i < num_timers; i++) {
547209371Smav		t = &sc->t[i];
548209440Smav		if (sc->legacy_route && i < 2)
549209440Smav			t->irq = (i == 0) ? 0 : 8;
550209371Smav#ifdef DEV_APIC
551209440Smav		else if (t->caps & HPET_TCAP_FSB_INT_DEL) {
552209371Smav			if ((j = PCIB_ALLOC_MSIX(
553209371Smav			    device_get_parent(device_get_parent(dev)), dev,
554209371Smav			    &t->irq))) {
555209371Smav				device_printf(dev,
556209440Smav				    "Can't allocate interrupt for t%d.\n", j);
557209440Smav			}
558209440Smav		}
559209440Smav#endif
560212238Smav		else if (dvectors & t->vectors) {
561212238Smav			t->irq = ffs(dvectors & t->vectors) - 1;
562212238Smav			dvectors &= ~(1 << t->irq);
563212238Smav		}
564209440Smav		if (t->irq >= 0) {
565216263Sjhb			t->intr_rid = hpet_find_irq_rid(dev, t->irq, t->irq);
566216490Sjhb			t->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
567216490Sjhb			    &t->intr_rid, t->irq, t->irq, 1, RF_ACTIVE);
568216490Sjhb			if (t->intr_res == NULL) {
569209440Smav				t->irq = -1;
570209440Smav				device_printf(dev,
571209440Smav				    "Can't map interrupt for t%d.\n", i);
572216490Sjhb			} else if (bus_setup_intr(dev, t->intr_res,
573216490Sjhb			    INTR_TYPE_CLK, hpet_intr_single, NULL, t,
574216490Sjhb			    &t->intr_handle) != 0) {
575209440Smav				t->irq = -1;
576209440Smav				device_printf(dev,
577209440Smav				    "Can't setup interrupt for t%d.\n", i);
578209371Smav			} else {
579209371Smav				bus_describe_intr(dev, t->intr_res,
580209371Smav				    t->intr_handle, "t%d", i);
581209371Smav				num_msi++;
582209371Smav			}
583209440Smav		}
584209440Smav		if (t->irq < 0 && (cvectors & t->vectors) != 0) {
585209371Smav			cvectors &= t->vectors;
586209371Smav			sc->useirq |= (1 << i);
587209371Smav		}
588209371Smav	}
589209440Smav	if (sc->legacy_route && sc->t[0].irq < 0 && sc->t[1].irq < 0)
590209440Smav		sc->legacy_route = 0;
591209440Smav	if (sc->legacy_route)
592209440Smav		hpet_enable(sc);
593209440Smav	/* Group timers for per-CPU operation. */
594212533Smav	num_percpu_et = min(num_msi / mp_ncpus, sc->per_cpu);
595209440Smav	num_percpu_t = num_percpu_et * mp_ncpus;
596209440Smav	pcpu_master = 0;
597209440Smav	cur_cpu = CPU_FIRST();
598209440Smav	for (i = 0; i < num_timers; i++) {
599209440Smav		t = &sc->t[i];
600209440Smav		if (t->irq >= 0 && num_percpu_t > 0) {
601209440Smav			if (cur_cpu == CPU_FIRST())
602209440Smav				pcpu_master = i;
603212323Smav			t->pcpu_cpu = cur_cpu;
604209440Smav			t->pcpu_master = pcpu_master;
605209440Smav			sc->t[pcpu_master].
606209440Smav			    pcpu_slaves[cur_cpu] = i;
607209440Smav			bus_bind_intr(dev, t->intr_res, cur_cpu);
608209440Smav			cur_cpu = CPU_NEXT(cur_cpu);
609209440Smav			num_percpu_t--;
610212238Smav		} else if (t->irq >= 0)
611212238Smav			bus_bind_intr(dev, t->intr_res, CPU_FIRST());
612209440Smav	}
613209371Smav	bus_write_4(sc->mem_res, HPET_ISR, 0xffffffff);
614209371Smav	sc->irq = -1;
615215473Sjhb	/* If at least one timer needs legacy IRQ - set it up. */
616209371Smav	if (sc->useirq) {
617209371Smav		j = i = fls(cvectors) - 1;
618209371Smav		while (j > 0 && (cvectors & (1 << (j - 1))) != 0)
619209371Smav			j--;
620216263Sjhb		sc->intr_rid = hpet_find_irq_rid(dev, j, i);
621216490Sjhb		sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
622216490Sjhb		    &sc->intr_rid, j, i, 1, RF_SHAREABLE | RF_ACTIVE);
623216490Sjhb		if (sc->intr_res == NULL)
624216490Sjhb			device_printf(dev, "Can't map interrupt.\n");
625216490Sjhb		else if (bus_setup_intr(dev, sc->intr_res, INTR_TYPE_CLK,
626216490Sjhb		    hpet_intr, NULL, sc, &sc->intr_handle) != 0) {
627209371Smav			device_printf(dev, "Can't setup interrupt.\n");
628209371Smav		} else {
629209371Smav			sc->irq = rman_get_start(sc->intr_res);
630209371Smav			/* Bind IRQ to BSP to avoid live migration. */
631209371Smav			bus_bind_intr(dev, sc->intr_res, CPU_FIRST());
632209371Smav		}
633209371Smav	}
634209371Smav	/* Program and announce event timers. */
635209371Smav	for (i = 0; i < num_timers; i++) {
636209371Smav		t = &sc->t[i];
637209371Smav		t->caps &= ~(HPET_TCNF_FSB_EN | HPET_TCNF_INT_ROUTE);
638209371Smav		t->caps &= ~(HPET_TCNF_VAL_SET | HPET_TCNF_INT_ENB);
639209440Smav		t->caps &= ~(HPET_TCNF_INT_TYPE);
640209371Smav		t->caps |= HPET_TCNF_32MODE;
641209440Smav		if (t->irq >= 0 && sc->legacy_route && i < 2) {
642209440Smav			/* Legacy route doesn't need more configuration. */
643209440Smav		} else
644209371Smav#ifdef DEV_APIC
645212238Smav		if ((t->caps & HPET_TCAP_FSB_INT_DEL) && t->irq >= 0) {
646209371Smav			uint64_t addr;
647209371Smav			uint32_t data;
648209371Smav
649209371Smav			if (PCIB_MAP_MSI(
650209371Smav			    device_get_parent(device_get_parent(dev)), dev,
651209371Smav			    t->irq, &addr, &data) == 0) {
652209371Smav				bus_write_4(sc->mem_res,
653209371Smav				    HPET_TIMER_FSB_ADDR(i), addr);
654209371Smav				bus_write_4(sc->mem_res,
655209371Smav				    HPET_TIMER_FSB_VAL(i), data);
656209371Smav				t->caps |= HPET_TCNF_FSB_EN;
657209371Smav			} else
658209371Smav				t->irq = -2;
659209371Smav		} else
660209371Smav#endif
661212238Smav		if (t->irq >= 0)
662212238Smav			t->caps |= (t->irq << 9);
663212238Smav		else if (sc->irq >= 0 && (t->vectors & (1 << sc->irq)))
664209371Smav			t->caps |= (sc->irq << 9) | HPET_TCNF_INT_TYPE;
665209371Smav		bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(i), t->caps);
666209371Smav		/* Skip event timers without set up IRQ. */
667209371Smav		if (t->irq < 0 &&
668209371Smav		    (sc->irq < 0 || (t->vectors & (1 << sc->irq)) == 0))
669209371Smav			continue;
670209371Smav		/* Announce the reset. */
671209371Smav		if (maxhpetet == 0)
672209371Smav			t->et.et_name = "HPET";
673209371Smav		else {
674209371Smav			sprintf(t->name, "HPET%d", maxhpetet);
675209371Smav			t->et.et_name = t->name;
676209371Smav		}
677209371Smav		t->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT;
678209371Smav		t->et.et_quality = 450;
679209371Smav		if (t->pcpu_master >= 0) {
680209371Smav			t->et.et_flags |= ET_FLAGS_PERCPU;
681209371Smav			t->et.et_quality += 100;
682209371Smav		}
683209371Smav		if ((t->caps & HPET_TCAP_PER_INT) == 0)
684209371Smav			t->et.et_quality -= 10;
685209371Smav		t->et.et_frequency = sc->freq;
686210290Smav		t->et.et_min_period.sec = 0;
687224919Smav		t->et.et_min_period.frac =
688224919Smav		    (((uint64_t)(HPET_MIN_CYCLES * 2) << 32) / sc->freq) << 32;
689210298Smav		t->et.et_max_period.sec = 0xfffffffeLLU / sc->freq;
690210290Smav		t->et.et_max_period.frac =
691210298Smav		    ((0xfffffffeLLU << 32) / sc->freq) << 32;
692209371Smav		t->et.et_start = hpet_start;
693209371Smav		t->et.et_stop = hpet_stop;
694209371Smav		t->et.et_priv = &sc->t[i];
695209371Smav		if (t->pcpu_master < 0 || t->pcpu_master == i) {
696209371Smav			et_register(&t->et);
697209371Smav			maxhpetet++;
698209371Smav		}
699209371Smav	}
700159217Snjl	return (0);
701159217Snjl}
702159217Snjl
703159217Snjlstatic int
704209371Smavhpet_detach(device_t dev)
705159217Snjl{
706159217Snjl	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
707159217Snjl
708159217Snjl	/* XXX Without a tc_remove() function, we can't detach. */
709159217Snjl	return (EBUSY);
710159217Snjl}
711159217Snjl
712168010Snjlstatic int
713209371Smavhpet_suspend(device_t dev)
714175361Sjhb{
715212541Smav//	struct hpet_softc *sc;
716175361Sjhb
717175361Sjhb	/*
718175361Sjhb	 * Disable the timer during suspend.  The timer will not lose
719175361Sjhb	 * its state in S1 or S2, but we are required to disable
720175361Sjhb	 * it.
721175361Sjhb	 */
722212541Smav//	sc = device_get_softc(dev);
723212541Smav//	hpet_disable(sc);
724175361Sjhb
725175361Sjhb	return (0);
726175361Sjhb}
727175361Sjhb
728175361Sjhbstatic int
729209371Smavhpet_resume(device_t dev)
730168010Snjl{
731209371Smav	struct hpet_softc *sc;
732209371Smav	struct hpet_timer *t;
733209371Smav	int i;
734168010Snjl
735168010Snjl	/* Re-enable the timer after a resume to keep the clock advancing. */
736168010Snjl	sc = device_get_softc(dev);
737175361Sjhb	hpet_enable(sc);
738209371Smav	/* Restart event timers that were running on suspend. */
739209371Smav	for (i = 0; i < sc->num_timers; i++) {
740209371Smav		t = &sc->t[i];
741209371Smav#ifdef DEV_APIC
742209440Smav		if (t->irq >= 0 && (sc->legacy_route == 0 || i >= 2)) {
743209371Smav			uint64_t addr;
744209371Smav			uint32_t data;
745209371Smav
746209371Smav			if (PCIB_MAP_MSI(
747209371Smav			    device_get_parent(device_get_parent(dev)), dev,
748209371Smav			    t->irq, &addr, &data) == 0) {
749209371Smav				bus_write_4(sc->mem_res,
750209371Smav				    HPET_TIMER_FSB_ADDR(i), addr);
751209371Smav				bus_write_4(sc->mem_res,
752209371Smav				    HPET_TIMER_FSB_VAL(i), data);
753209371Smav			}
754209371Smav		}
755209371Smav#endif
756209371Smav		if (t->mode == 0)
757209371Smav			continue;
758212491Smav		t->next = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
759209371Smav		if (t->mode == 1 && (t->caps & HPET_TCAP_PER_INT)) {
760209371Smav			t->caps |= HPET_TCNF_TYPE;
761212491Smav			t->next += t->div;
762209371Smav			bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
763209371Smav			    t->caps | HPET_TCNF_VAL_SET);
764209371Smav			bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
765212491Smav			    t->next);
766209371Smav			bus_read_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num));
767209371Smav			bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
768209371Smav			    t->div);
769209371Smav		} else {
770212491Smav			t->next += sc->freq / 1024;
771209371Smav			bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
772212491Smav			    t->next);
773209371Smav		}
774209371Smav		bus_write_4(sc->mem_res, HPET_ISR, 1 << t->num);
775209371Smav		bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), t->caps);
776209371Smav	}
777168010Snjl	return (0);
778168010Snjl}
779168010Snjl
780159217Snjl/* Print some basic latency/rate information to assist in debugging. */
781159217Snjlstatic void
782209371Smavhpet_test(struct hpet_softc *sc)
783159217Snjl{
784151912Sphk	int i;
785151912Sphk	uint32_t u1, u2;
786151912Sphk	struct bintime b0, b1, b2;
787151912Sphk	struct timespec ts;
788151912Sphk
789151912Sphk	binuptime(&b0);
790151912Sphk	binuptime(&b0);
791151912Sphk	binuptime(&b1);
792175385Sjhb	u1 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
793151912Sphk	for (i = 1; i < 1000; i++)
794175385Sjhb		u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
795151912Sphk	binuptime(&b2);
796175385Sjhb	u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
797151912Sphk
798151912Sphk	bintime_sub(&b2, &b1);
799151912Sphk	bintime_sub(&b1, &b0);
800151912Sphk	bintime_sub(&b2, &b1);
801151912Sphk	bintime2timespec(&b2, &ts);
802151912Sphk
803159217Snjl	device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
804151912Sphk	    (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1);
805151912Sphk
806159217Snjl	device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);
807151912Sphk}
808151912Sphk
809209371Smav#ifdef DEV_APIC
810209371Smavstatic int
811209371Smavhpet_remap_intr(device_t dev, device_t child, u_int irq)
812209371Smav{
813209371Smav	struct hpet_softc *sc = device_get_softc(dev);
814209371Smav	struct hpet_timer *t;
815209371Smav	uint64_t addr;
816209371Smav	uint32_t data;
817209371Smav	int error, i;
818209371Smav
819209371Smav	for (i = 0; i < sc->num_timers; i++) {
820209371Smav		t = &sc->t[i];
821209371Smav		if (t->irq != irq)
822209371Smav			continue;
823209371Smav		error = PCIB_MAP_MSI(
824209371Smav		    device_get_parent(device_get_parent(dev)), dev,
825209371Smav		    irq, &addr, &data);
826209371Smav		if (error)
827209371Smav			return (error);
828209371Smav		hpet_disable(sc); /* Stop timer to avoid interrupt loss. */
829209371Smav		bus_write_4(sc->mem_res, HPET_TIMER_FSB_ADDR(i), addr);
830209371Smav		bus_write_4(sc->mem_res, HPET_TIMER_FSB_VAL(i), data);
831209371Smav		hpet_enable(sc);
832209371Smav		return (0);
833209371Smav	}
834209371Smav	return (ENOENT);
835209371Smav}
836209371Smav#endif
837209371Smav
838209371Smavstatic device_method_t hpet_methods[] = {
839151912Sphk	/* Device interface */
840209371Smav	DEVMETHOD(device_identify, hpet_identify),
841209371Smav	DEVMETHOD(device_probe, hpet_probe),
842209371Smav	DEVMETHOD(device_attach, hpet_attach),
843209371Smav	DEVMETHOD(device_detach, hpet_detach),
844209371Smav	DEVMETHOD(device_suspend, hpet_suspend),
845209371Smav	DEVMETHOD(device_resume, hpet_resume),
846151912Sphk
847209371Smav#ifdef DEV_APIC
848209371Smav	DEVMETHOD(bus_remap_intr, hpet_remap_intr),
849209371Smav#endif
850209371Smav
851151912Sphk	{0, 0}
852151912Sphk};
853151912Sphk
854209371Smavstatic driver_t	hpet_driver = {
855209371Smav	"hpet",
856209371Smav	hpet_methods,
857209371Smav	sizeof(struct hpet_softc),
858151912Sphk};
859151912Sphk
860209371SmavDRIVER_MODULE(hpet, acpi, hpet_driver, hpet_devclass, 0, 0);
861209371SmavMODULE_DEPEND(hpet, acpi, 1, 1, 1);
862