xref: /freebsd/sys/dev/acpica/acpi_hpet.c (revision 19261079)
1 /*-
2  * Copyright (c) 2005 Poul-Henning Kamp
3  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_acpi.h"
32 
33 #if defined(__amd64__)
34 #define	DEV_APIC
35 #else
36 #include "opt_apic.h"
37 #endif
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/proc.h>
44 #include <sys/rman.h>
45 #include <sys/mman.h>
46 #include <sys/time.h>
47 #include <sys/smp.h>
48 #include <sys/sysctl.h>
49 #include <sys/timeet.h>
50 #include <sys/timetc.h>
51 #include <sys/vdso.h>
52 
53 #include <contrib/dev/acpica/include/acpi.h>
54 #include <contrib/dev/acpica/include/accommon.h>
55 
56 #include <dev/acpica/acpivar.h>
57 #include <dev/acpica/acpi_hpet.h>
58 
59 #ifdef DEV_APIC
60 #include "pcib_if.h"
61 #endif
62 
63 #define HPET_VENDID_AMD		0x4353
64 #define HPET_VENDID_AMD2	0x1022
65 #define HPET_VENDID_HYGON	0x1d94
66 #define HPET_VENDID_INTEL	0x8086
67 #define HPET_VENDID_NVIDIA	0x10de
68 #define HPET_VENDID_SW		0x1166
69 
70 ACPI_SERIAL_DECL(hpet, "ACPI HPET support");
71 
72 static devclass_t hpet_devclass;
73 
74 /* ACPI CA debugging */
75 #define _COMPONENT	ACPI_TIMER
76 ACPI_MODULE_NAME("HPET")
77 
78 struct hpet_softc {
79 	device_t		dev;
80 	int			mem_rid;
81 	int			intr_rid;
82 	int			irq;
83 	int			useirq;
84 	int			legacy_route;
85 	int			per_cpu;
86 	uint32_t		allowed_irqs;
87 	struct resource		*mem_res;
88 	struct resource		*intr_res;
89 	void			*intr_handle;
90 	ACPI_HANDLE		handle;
91 	uint32_t		acpi_uid;
92 	uint64_t		freq;
93 	uint32_t		caps;
94 	struct timecounter	tc;
95 	struct hpet_timer {
96 		struct eventtimer	et;
97 		struct hpet_softc	*sc;
98 		int			num;
99 		int			mode;
100 #define	TIMER_STOPPED	0
101 #define	TIMER_PERIODIC	1
102 #define	TIMER_ONESHOT	2
103 		int			intr_rid;
104 		int			irq;
105 		int			pcpu_cpu;
106 		int			pcpu_misrouted;
107 		int			pcpu_master;
108 		int			pcpu_slaves[MAXCPU];
109 		struct resource		*intr_res;
110 		void			*intr_handle;
111 		uint32_t		caps;
112 		uint32_t		vectors;
113 		uint32_t		div;
114 		uint32_t		next;
115 		char			name[8];
116 	} 			t[32];
117 	int			num_timers;
118 	struct cdev		*pdev;
119 	int			mmap_allow;
120 	int			mmap_allow_write;
121 };
122 
123 static d_open_t hpet_open;
124 static d_mmap_t hpet_mmap;
125 
126 static struct cdevsw hpet_cdevsw = {
127 	.d_version =	D_VERSION,
128 	.d_name =	"hpet",
129 	.d_open =	hpet_open,
130 	.d_mmap =	hpet_mmap,
131 };
132 
133 static u_int hpet_get_timecount(struct timecounter *tc);
134 static void hpet_test(struct hpet_softc *sc);
135 
136 static char *hpet_ids[] = { "PNP0103", NULL };
137 
138 /* Knob to disable acpi_hpet device */
139 bool acpi_hpet_disabled = false;
140 
141 static u_int
142 hpet_get_timecount(struct timecounter *tc)
143 {
144 	struct hpet_softc *sc;
145 
146 	sc = tc->tc_priv;
147 	return (bus_read_4(sc->mem_res, HPET_MAIN_COUNTER));
148 }
149 
150 uint32_t
151 hpet_vdso_timehands(struct vdso_timehands *vdso_th, struct timecounter *tc)
152 {
153 	struct hpet_softc *sc;
154 
155 	sc = tc->tc_priv;
156 	vdso_th->th_algo = VDSO_TH_ALGO_X86_HPET;
157 	vdso_th->th_x86_shift = 0;
158 	vdso_th->th_x86_hpet_idx = device_get_unit(sc->dev);
159 	vdso_th->th_x86_pvc_last_systime = 0;
160 	vdso_th->th_x86_pvc_stable_mask = 0;
161 	bzero(vdso_th->th_res, sizeof(vdso_th->th_res));
162 	return (sc->mmap_allow != 0);
163 }
164 
165 #ifdef COMPAT_FREEBSD32
166 uint32_t
167 hpet_vdso_timehands32(struct vdso_timehands32 *vdso_th32,
168     struct timecounter *tc)
169 {
170 	struct hpet_softc *sc;
171 
172 	sc = tc->tc_priv;
173 	vdso_th32->th_algo = VDSO_TH_ALGO_X86_HPET;
174 	vdso_th32->th_x86_shift = 0;
175 	vdso_th32->th_x86_hpet_idx = device_get_unit(sc->dev);
176 	vdso_th32->th_x86_pvc_last_systime = 0;
177 	vdso_th32->th_x86_pvc_stable_mask = 0;
178 	bzero(vdso_th32->th_res, sizeof(vdso_th32->th_res));
179 	return (sc->mmap_allow != 0);
180 }
181 #endif
182 
183 static void
184 hpet_enable(struct hpet_softc *sc)
185 {
186 	uint32_t val;
187 
188 	val = bus_read_4(sc->mem_res, HPET_CONFIG);
189 	if (sc->legacy_route)
190 		val |= HPET_CNF_LEG_RT;
191 	else
192 		val &= ~HPET_CNF_LEG_RT;
193 	val |= HPET_CNF_ENABLE;
194 	bus_write_4(sc->mem_res, HPET_CONFIG, val);
195 }
196 
197 static void
198 hpet_disable(struct hpet_softc *sc)
199 {
200 	uint32_t val;
201 
202 	val = bus_read_4(sc->mem_res, HPET_CONFIG);
203 	val &= ~HPET_CNF_ENABLE;
204 	bus_write_4(sc->mem_res, HPET_CONFIG, val);
205 }
206 
207 static int
208 hpet_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
209 {
210 	struct hpet_timer *mt = (struct hpet_timer *)et->et_priv;
211 	struct hpet_timer *t;
212 	struct hpet_softc *sc = mt->sc;
213 	uint32_t fdiv, now;
214 
215 	t = (mt->pcpu_master < 0) ? mt : &sc->t[mt->pcpu_slaves[curcpu]];
216 	if (period != 0) {
217 		t->mode = TIMER_PERIODIC;
218 		t->div = (sc->freq * period) >> 32;
219 	} else {
220 		t->mode = TIMER_ONESHOT;
221 		t->div = 0;
222 	}
223 	if (first != 0)
224 		fdiv = (sc->freq * first) >> 32;
225 	else
226 		fdiv = t->div;
227 	if (t->irq < 0)
228 		bus_write_4(sc->mem_res, HPET_ISR, 1 << t->num);
229 	t->caps |= HPET_TCNF_INT_ENB;
230 	now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
231 restart:
232 	t->next = now + fdiv;
233 	if (t->mode == TIMER_PERIODIC && (t->caps & HPET_TCAP_PER_INT)) {
234 		t->caps |= HPET_TCNF_TYPE;
235 		bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
236 		    t->caps | HPET_TCNF_VAL_SET);
237 		bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
238 		    t->next);
239 		bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
240 		    t->div);
241 	} else {
242 		t->caps &= ~HPET_TCNF_TYPE;
243 		bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
244 		    t->caps);
245 		bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
246 		    t->next);
247 	}
248 	now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
249 	if ((int32_t)(now - t->next + HPET_MIN_CYCLES) >= 0) {
250 		fdiv *= 2;
251 		goto restart;
252 	}
253 	return (0);
254 }
255 
256 static int
257 hpet_stop(struct eventtimer *et)
258 {
259 	struct hpet_timer *mt = (struct hpet_timer *)et->et_priv;
260 	struct hpet_timer *t;
261 	struct hpet_softc *sc = mt->sc;
262 
263 	t = (mt->pcpu_master < 0) ? mt : &sc->t[mt->pcpu_slaves[curcpu]];
264 	t->mode = TIMER_STOPPED;
265 	t->caps &= ~(HPET_TCNF_INT_ENB | HPET_TCNF_TYPE);
266 	bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), t->caps);
267 	return (0);
268 }
269 
270 static int
271 hpet_intr_single(void *arg)
272 {
273 	struct hpet_timer *t = (struct hpet_timer *)arg;
274 	struct hpet_timer *mt;
275 	struct hpet_softc *sc = t->sc;
276 	uint32_t now;
277 
278 	if (t->mode == TIMER_STOPPED)
279 		return (FILTER_STRAY);
280 	/* Check that per-CPU timer interrupt reached right CPU. */
281 	if (t->pcpu_cpu >= 0 && t->pcpu_cpu != curcpu) {
282 		if ((++t->pcpu_misrouted) % 32 == 0) {
283 			printf("HPET interrupt routed to the wrong CPU"
284 			    " (timer %d CPU %d -> %d)!\n",
285 			    t->num, t->pcpu_cpu, curcpu);
286 		}
287 
288 		/*
289 		 * Reload timer, hoping that next time may be more lucky
290 		 * (system will manage proper interrupt binding).
291 		 */
292 		if ((t->mode == TIMER_PERIODIC &&
293 		    (t->caps & HPET_TCAP_PER_INT) == 0) ||
294 		    t->mode == TIMER_ONESHOT) {
295 			t->next = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER) +
296 			    sc->freq / 8;
297 			bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
298 			    t->next);
299 		}
300 		return (FILTER_HANDLED);
301 	}
302 	if (t->mode == TIMER_PERIODIC &&
303 	    (t->caps & HPET_TCAP_PER_INT) == 0) {
304 		t->next += t->div;
305 		now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
306 		if ((int32_t)((now + t->div / 2) - t->next) > 0)
307 			t->next = now + t->div / 2;
308 		bus_write_4(sc->mem_res,
309 		    HPET_TIMER_COMPARATOR(t->num), t->next);
310 	} else if (t->mode == TIMER_ONESHOT)
311 		t->mode = TIMER_STOPPED;
312 	mt = (t->pcpu_master < 0) ? t : &sc->t[t->pcpu_master];
313 	if (mt->et.et_active)
314 		mt->et.et_event_cb(&mt->et, mt->et.et_arg);
315 	return (FILTER_HANDLED);
316 }
317 
318 static int
319 hpet_intr(void *arg)
320 {
321 	struct hpet_softc *sc = (struct hpet_softc *)arg;
322 	int i;
323 	uint32_t val;
324 
325 	val = bus_read_4(sc->mem_res, HPET_ISR);
326 	if (val) {
327 		bus_write_4(sc->mem_res, HPET_ISR, val);
328 		val &= sc->useirq;
329 		for (i = 0; i < sc->num_timers; i++) {
330 			if ((val & (1 << i)) == 0)
331 				continue;
332 			hpet_intr_single(&sc->t[i]);
333 		}
334 		return (FILTER_HANDLED);
335 	}
336 	return (FILTER_STRAY);
337 }
338 
339 uint32_t
340 hpet_get_uid(device_t dev)
341 {
342 	struct hpet_softc *sc;
343 
344 	sc = device_get_softc(dev);
345 	return (sc->acpi_uid);
346 }
347 
348 static ACPI_STATUS
349 hpet_find(ACPI_HANDLE handle, UINT32 level, void *context,
350     void **status)
351 {
352 	char 		**ids;
353 	uint32_t	id = (uint32_t)(uintptr_t)context;
354 	uint32_t	uid = 0;
355 
356 	for (ids = hpet_ids; *ids != NULL; ids++) {
357 		if (acpi_MatchHid(handle, *ids))
358 		        break;
359 	}
360 	if (*ids == NULL)
361 		return (AE_OK);
362 	if (ACPI_FAILURE(acpi_GetInteger(handle, "_UID", &uid)) ||
363 	    id == uid)
364 		*status = acpi_get_device(handle);
365 	return (AE_OK);
366 }
367 
368 /*
369  * Find an existing IRQ resource that matches the requested IRQ range
370  * and return its RID.  If one is not found, use a new RID.
371  */
372 static int
373 hpet_find_irq_rid(device_t dev, u_long start, u_long end)
374 {
375 	rman_res_t irq;
376 	int error, rid;
377 
378 	for (rid = 0;; rid++) {
379 		error = bus_get_resource(dev, SYS_RES_IRQ, rid, &irq, NULL);
380 		if (error != 0 || (start <= irq && irq <= end))
381 			return (rid);
382 	}
383 }
384 
385 static int
386 hpet_open(struct cdev *cdev, int oflags, int devtype, struct thread *td)
387 {
388 	struct hpet_softc *sc;
389 
390 	sc = cdev->si_drv1;
391 	if (!sc->mmap_allow)
392 		return (EPERM);
393 	else
394 		return (0);
395 }
396 
397 static int
398 hpet_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
399     int nprot, vm_memattr_t *memattr)
400 {
401 	struct hpet_softc *sc;
402 
403 	sc = cdev->si_drv1;
404 	if (offset >= rman_get_size(sc->mem_res))
405 		return (EINVAL);
406 	if (!sc->mmap_allow_write && (nprot & PROT_WRITE))
407 		return (EPERM);
408 	*paddr = rman_get_start(sc->mem_res) + offset;
409 	*memattr = VM_MEMATTR_UNCACHEABLE;
410 
411 	return (0);
412 }
413 
414 /* Discover the HPET via the ACPI table of the same name. */
415 static void
416 hpet_identify(driver_t *driver, device_t parent)
417 {
418 	ACPI_TABLE_HPET *hpet;
419 	ACPI_STATUS	status;
420 	device_t	child;
421 	int		i;
422 
423 	/* Only one HPET device can be added. */
424 	if (devclass_get_device(hpet_devclass, 0))
425 		return;
426 	for (i = 1; ; i++) {
427 		/* Search for HPET table. */
428 		status = AcpiGetTable(ACPI_SIG_HPET, i, (ACPI_TABLE_HEADER **)&hpet);
429 		if (ACPI_FAILURE(status))
430 			return;
431 		/* Search for HPET device with same ID. */
432 		child = NULL;
433 		AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
434 		    100, hpet_find, NULL, (void *)(uintptr_t)hpet->Sequence,
435 		    (void *)&child);
436 		/* If found - let it be probed in normal way. */
437 		if (child) {
438 			if (bus_get_resource(child, SYS_RES_MEMORY, 0,
439 			    NULL, NULL) != 0)
440 				bus_set_resource(child, SYS_RES_MEMORY, 0,
441 				    hpet->Address.Address, HPET_MEM_WIDTH);
442 			continue;
443 		}
444 		/* If not - create it from table info. */
445 		child = BUS_ADD_CHILD(parent, 2, "hpet", 0);
446 		if (child == NULL) {
447 			printf("%s: can't add child\n", __func__);
448 			continue;
449 		}
450 		bus_set_resource(child, SYS_RES_MEMORY, 0, hpet->Address.Address,
451 		    HPET_MEM_WIDTH);
452 	}
453 }
454 
455 static int
456 hpet_probe(device_t dev)
457 {
458 	int rv;
459 
460 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
461 	if (acpi_disabled("hpet") || acpi_hpet_disabled)
462 		return (ENXIO);
463 	if (acpi_get_handle(dev) != NULL)
464 		rv = ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids, NULL);
465 	else
466 		rv = 0;
467 	if (rv <= 0)
468 		device_set_desc(dev, "High Precision Event Timer");
469 	return (rv);
470 }
471 
472 static int
473 hpet_attach(device_t dev)
474 {
475 	struct hpet_softc *sc;
476 	struct hpet_timer *t;
477 	struct make_dev_args mda;
478 	int i, j, num_msi, num_timers, num_percpu_et, num_percpu_t, cur_cpu;
479 	int pcpu_master, error;
480 	static int maxhpetet = 0;
481 	uint32_t val, val2, cvectors, dvectors;
482 	uint16_t vendor, rev;
483 
484 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
485 
486 	sc = device_get_softc(dev);
487 	sc->dev = dev;
488 	sc->handle = acpi_get_handle(dev);
489 
490 	sc->mem_rid = 0;
491 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
492 	    RF_ACTIVE);
493 	if (sc->mem_res == NULL)
494 		return (ENOMEM);
495 
496 	/* Validate that we can access the whole region. */
497 	if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH) {
498 		device_printf(dev, "memory region width %jd too small\n",
499 		    rman_get_size(sc->mem_res));
500 		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
501 		return (ENXIO);
502 	}
503 
504 	/* Be sure timer is enabled. */
505 	hpet_enable(sc);
506 
507 	/* Read basic statistics about the timer. */
508 	val = bus_read_4(sc->mem_res, HPET_PERIOD);
509 	if (val == 0) {
510 		device_printf(dev, "invalid period\n");
511 		hpet_disable(sc);
512 		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
513 		return (ENXIO);
514 	}
515 
516 	sc->freq = (1000000000000000LL + val / 2) / val;
517 	sc->caps = bus_read_4(sc->mem_res, HPET_CAPABILITIES);
518 	vendor = (sc->caps & HPET_CAP_VENDOR_ID) >> 16;
519 	rev = sc->caps & HPET_CAP_REV_ID;
520 	num_timers = 1 + ((sc->caps & HPET_CAP_NUM_TIM) >> 8);
521 	/*
522 	 * ATI/AMD violates IA-PC HPET (High Precision Event Timers)
523 	 * Specification and provides an off by one number
524 	 * of timers/comparators.
525 	 * Additionally, they use unregistered value in VENDOR_ID field.
526 	 */
527 	if (vendor == HPET_VENDID_AMD && rev < 0x10 && num_timers > 0)
528 		num_timers--;
529 	sc->num_timers = num_timers;
530 	if (bootverbose) {
531 		device_printf(dev,
532 		    "vendor 0x%x, rev 0x%x, %jdHz%s, %d timers,%s\n",
533 		    vendor, rev, sc->freq,
534 		    (sc->caps & HPET_CAP_COUNT_SIZE) ? " 64bit" : "",
535 		    num_timers,
536 		    (sc->caps & HPET_CAP_LEG_RT) ? " legacy route" : "");
537 	}
538 	for (i = 0; i < num_timers; i++) {
539 		t = &sc->t[i];
540 		t->sc = sc;
541 		t->num = i;
542 		t->mode = TIMER_STOPPED;
543 		t->intr_rid = -1;
544 		t->irq = -1;
545 		t->pcpu_cpu = -1;
546 		t->pcpu_misrouted = 0;
547 		t->pcpu_master = -1;
548 		t->caps = bus_read_4(sc->mem_res, HPET_TIMER_CAP_CNF(i));
549 		t->vectors = bus_read_4(sc->mem_res, HPET_TIMER_CAP_CNF(i) + 4);
550 		if (bootverbose) {
551 			device_printf(dev,
552 			    " t%d: irqs 0x%08x (%d)%s%s%s\n", i,
553 			    t->vectors, (t->caps & HPET_TCNF_INT_ROUTE) >> 9,
554 			    (t->caps & HPET_TCAP_FSB_INT_DEL) ? ", MSI" : "",
555 			    (t->caps & HPET_TCAP_SIZE) ? ", 64bit" : "",
556 			    (t->caps & HPET_TCAP_PER_INT) ? ", periodic" : "");
557 		}
558 	}
559 	if (testenv("debug.acpi.hpet_test"))
560 		hpet_test(sc);
561 	/*
562 	 * Don't attach if the timer never increments.  Since the spec
563 	 * requires it to be at least 10 MHz, it has to change in 1 us.
564 	 */
565 	val = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
566 	DELAY(1);
567 	val2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
568 	if (val == val2) {
569 		device_printf(dev, "HPET never increments, disabling\n");
570 		hpet_disable(sc);
571 		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
572 		return (ENXIO);
573 	}
574 	/* Announce first HPET as timecounter. */
575 	if (device_get_unit(dev) == 0) {
576 		sc->tc.tc_get_timecount = hpet_get_timecount,
577 		sc->tc.tc_counter_mask = ~0u,
578 		sc->tc.tc_name = "HPET",
579 		sc->tc.tc_quality = 950,
580 		sc->tc.tc_frequency = sc->freq;
581 		sc->tc.tc_priv = sc;
582 		sc->tc.tc_fill_vdso_timehands = hpet_vdso_timehands;
583 #ifdef COMPAT_FREEBSD32
584 		sc->tc.tc_fill_vdso_timehands32 = hpet_vdso_timehands32;
585 #endif
586 		tc_init(&sc->tc);
587 	}
588 	/* If not disabled - setup and announce event timers. */
589 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
590 	     "clock", &i) == 0 && i == 0)
591 	        return (0);
592 
593 	/* Check whether we can and want legacy routing. */
594 	sc->legacy_route = 0;
595 	resource_int_value(device_get_name(dev), device_get_unit(dev),
596 	     "legacy_route", &sc->legacy_route);
597 	if ((sc->caps & HPET_CAP_LEG_RT) == 0)
598 		sc->legacy_route = 0;
599 	if (sc->legacy_route) {
600 		sc->t[0].vectors = 0;
601 		sc->t[1].vectors = 0;
602 	}
603 
604 	/* Check what IRQs we want use. */
605 	/* By default allow any PCI IRQs. */
606 	sc->allowed_irqs = 0xffff0000;
607 	/*
608 	 * HPETs in AMD chipsets before SB800 have problems with IRQs >= 16
609 	 * Lower are also not always working for different reasons.
610 	 * SB800 fixed it, but seems do not implements level triggering
611 	 * properly, that makes it very unreliable - it freezes after any
612 	 * interrupt loss. Avoid legacy IRQs for AMD.
613 	 */
614 	if (vendor == HPET_VENDID_AMD || vendor == HPET_VENDID_AMD2 ||
615 	    vendor == HPET_VENDID_HYGON)
616 		sc->allowed_irqs = 0x00000000;
617 	/*
618 	 * NVidia MCP5x chipsets have number of unexplained interrupt
619 	 * problems. For some reason, using HPET interrupts breaks HDA sound.
620 	 */
621 	if (vendor == HPET_VENDID_NVIDIA && rev <= 0x01)
622 		sc->allowed_irqs = 0x00000000;
623 	/*
624 	 * ServerWorks HT1000 reported to have problems with IRQs >= 16.
625 	 * Lower IRQs are working, but allowed mask is not set correctly.
626 	 * Legacy_route mode works fine.
627 	 */
628 	if (vendor == HPET_VENDID_SW && rev <= 0x01)
629 		sc->allowed_irqs = 0x00000000;
630 	/*
631 	 * Neither QEMU nor VirtualBox report supported IRQs correctly.
632 	 * The only way to use HPET there is to specify IRQs manually
633 	 * and/or use legacy_route. Legacy_route mode works on both.
634 	 */
635 	if (vm_guest)
636 		sc->allowed_irqs = 0x00000000;
637 	/* Let user override. */
638 	resource_int_value(device_get_name(dev), device_get_unit(dev),
639 	     "allowed_irqs", &sc->allowed_irqs);
640 
641 	/* Get how much per-CPU timers we should try to provide. */
642 	sc->per_cpu = 1;
643 	resource_int_value(device_get_name(dev), device_get_unit(dev),
644 	     "per_cpu", &sc->per_cpu);
645 
646 	num_msi = 0;
647 	sc->useirq = 0;
648 	/* Find IRQ vectors for all timers. */
649 	cvectors = sc->allowed_irqs & 0xffff0000;
650 	dvectors = sc->allowed_irqs & 0x0000ffff;
651 	if (sc->legacy_route)
652 		dvectors &= 0x0000fefe;
653 	for (i = 0; i < num_timers; i++) {
654 		t = &sc->t[i];
655 		if (sc->legacy_route && i < 2)
656 			t->irq = (i == 0) ? 0 : 8;
657 #ifdef DEV_APIC
658 		else if (t->caps & HPET_TCAP_FSB_INT_DEL) {
659 			if ((j = PCIB_ALLOC_MSIX(
660 			    device_get_parent(device_get_parent(dev)), dev,
661 			    &t->irq))) {
662 				device_printf(dev,
663 				    "Can't allocate interrupt for t%d: %d\n",
664 				    i, j);
665 			}
666 		}
667 #endif
668 		else if (dvectors & t->vectors) {
669 			t->irq = ffs(dvectors & t->vectors) - 1;
670 			dvectors &= ~(1 << t->irq);
671 		}
672 		if (t->irq >= 0) {
673 			t->intr_rid = hpet_find_irq_rid(dev, t->irq, t->irq);
674 			t->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
675 			    &t->intr_rid, t->irq, t->irq, 1, RF_ACTIVE);
676 			if (t->intr_res == NULL) {
677 				t->irq = -1;
678 				device_printf(dev,
679 				    "Can't map interrupt for t%d.\n", i);
680 			} else if (bus_setup_intr(dev, t->intr_res,
681 			    INTR_TYPE_CLK, hpet_intr_single, NULL, t,
682 			    &t->intr_handle) != 0) {
683 				t->irq = -1;
684 				device_printf(dev,
685 				    "Can't setup interrupt for t%d.\n", i);
686 			} else {
687 				bus_describe_intr(dev, t->intr_res,
688 				    t->intr_handle, "t%d", i);
689 				num_msi++;
690 			}
691 		}
692 		if (t->irq < 0 && (cvectors & t->vectors) != 0) {
693 			cvectors &= t->vectors;
694 			sc->useirq |= (1 << i);
695 		}
696 	}
697 	if (sc->legacy_route && sc->t[0].irq < 0 && sc->t[1].irq < 0)
698 		sc->legacy_route = 0;
699 	if (sc->legacy_route)
700 		hpet_enable(sc);
701 	/* Group timers for per-CPU operation. */
702 	num_percpu_et = min(num_msi / mp_ncpus, sc->per_cpu);
703 	num_percpu_t = num_percpu_et * mp_ncpus;
704 	pcpu_master = 0;
705 	cur_cpu = CPU_FIRST();
706 	for (i = 0; i < num_timers; i++) {
707 		t = &sc->t[i];
708 		if (t->irq >= 0 && num_percpu_t > 0) {
709 			if (cur_cpu == CPU_FIRST())
710 				pcpu_master = i;
711 			t->pcpu_cpu = cur_cpu;
712 			t->pcpu_master = pcpu_master;
713 			sc->t[pcpu_master].
714 			    pcpu_slaves[cur_cpu] = i;
715 			bus_bind_intr(dev, t->intr_res, cur_cpu);
716 			cur_cpu = CPU_NEXT(cur_cpu);
717 			num_percpu_t--;
718 		} else if (t->irq >= 0)
719 			bus_bind_intr(dev, t->intr_res, CPU_FIRST());
720 	}
721 	bus_write_4(sc->mem_res, HPET_ISR, 0xffffffff);
722 	sc->irq = -1;
723 	/* If at least one timer needs legacy IRQ - set it up. */
724 	if (sc->useirq) {
725 		j = i = fls(cvectors) - 1;
726 		while (j > 0 && (cvectors & (1 << (j - 1))) != 0)
727 			j--;
728 		sc->intr_rid = hpet_find_irq_rid(dev, j, i);
729 		sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
730 		    &sc->intr_rid, j, i, 1, RF_SHAREABLE | RF_ACTIVE);
731 		if (sc->intr_res == NULL)
732 			device_printf(dev, "Can't map interrupt.\n");
733 		else if (bus_setup_intr(dev, sc->intr_res, INTR_TYPE_CLK,
734 		    hpet_intr, NULL, sc, &sc->intr_handle) != 0) {
735 			device_printf(dev, "Can't setup interrupt.\n");
736 		} else {
737 			sc->irq = rman_get_start(sc->intr_res);
738 			/* Bind IRQ to BSP to avoid live migration. */
739 			bus_bind_intr(dev, sc->intr_res, CPU_FIRST());
740 		}
741 	}
742 	/* Program and announce event timers. */
743 	for (i = 0; i < num_timers; i++) {
744 		t = &sc->t[i];
745 		t->caps &= ~(HPET_TCNF_FSB_EN | HPET_TCNF_INT_ROUTE);
746 		t->caps &= ~(HPET_TCNF_VAL_SET | HPET_TCNF_INT_ENB);
747 		t->caps &= ~(HPET_TCNF_INT_TYPE);
748 		t->caps |= HPET_TCNF_32MODE;
749 		if (t->irq >= 0 && sc->legacy_route && i < 2) {
750 			/* Legacy route doesn't need more configuration. */
751 		} else
752 #ifdef DEV_APIC
753 		if ((t->caps & HPET_TCAP_FSB_INT_DEL) && t->irq >= 0) {
754 			uint64_t addr;
755 			uint32_t data;
756 
757 			if (PCIB_MAP_MSI(
758 			    device_get_parent(device_get_parent(dev)), dev,
759 			    t->irq, &addr, &data) == 0) {
760 				bus_write_4(sc->mem_res,
761 				    HPET_TIMER_FSB_ADDR(i), addr);
762 				bus_write_4(sc->mem_res,
763 				    HPET_TIMER_FSB_VAL(i), data);
764 				t->caps |= HPET_TCNF_FSB_EN;
765 			} else
766 				t->irq = -2;
767 		} else
768 #endif
769 		if (t->irq >= 0)
770 			t->caps |= (t->irq << 9);
771 		else if (sc->irq >= 0 && (t->vectors & (1 << sc->irq)))
772 			t->caps |= (sc->irq << 9) | HPET_TCNF_INT_TYPE;
773 		bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(i), t->caps);
774 		/* Skip event timers without set up IRQ. */
775 		if (t->irq < 0 &&
776 		    (sc->irq < 0 || (t->vectors & (1 << sc->irq)) == 0))
777 			continue;
778 		/* Announce the reset. */
779 		if (maxhpetet == 0)
780 			t->et.et_name = "HPET";
781 		else {
782 			sprintf(t->name, "HPET%d", maxhpetet);
783 			t->et.et_name = t->name;
784 		}
785 		t->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT;
786 		t->et.et_quality = 450;
787 		if (t->pcpu_master >= 0) {
788 			t->et.et_flags |= ET_FLAGS_PERCPU;
789 			t->et.et_quality += 100;
790 		} else if (mp_ncpus >= 8)
791 			t->et.et_quality -= 100;
792 		if ((t->caps & HPET_TCAP_PER_INT) == 0)
793 			t->et.et_quality -= 10;
794 		t->et.et_frequency = sc->freq;
795 		t->et.et_min_period =
796 		    ((uint64_t)(HPET_MIN_CYCLES * 2) << 32) / sc->freq;
797 		t->et.et_max_period = (0xfffffffeLLU << 32) / sc->freq;
798 		t->et.et_start = hpet_start;
799 		t->et.et_stop = hpet_stop;
800 		t->et.et_priv = &sc->t[i];
801 		if (t->pcpu_master < 0 || t->pcpu_master == i) {
802 			et_register(&t->et);
803 			maxhpetet++;
804 		}
805 	}
806 	acpi_GetInteger(sc->handle, "_UID", &sc->acpi_uid);
807 
808 	make_dev_args_init(&mda);
809 	mda.mda_devsw = &hpet_cdevsw;
810 	mda.mda_uid = UID_ROOT;
811 	mda.mda_gid = GID_WHEEL;
812 	mda.mda_mode = 0644;
813 	mda.mda_si_drv1 = sc;
814 	error = make_dev_s(&mda, &sc->pdev, "hpet%d", device_get_unit(dev));
815 	if (error == 0) {
816 		sc->mmap_allow = 1;
817 		TUNABLE_INT_FETCH("hw.acpi.hpet.mmap_allow",
818 		    &sc->mmap_allow);
819 		sc->mmap_allow_write = 0;
820 		TUNABLE_INT_FETCH("hw.acpi.hpet.mmap_allow_write",
821 		    &sc->mmap_allow_write);
822 		SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
823 		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
824 		    OID_AUTO, "mmap_allow",
825 		    CTLFLAG_RW, &sc->mmap_allow, 0,
826 		    "Allow userland to memory map HPET");
827 		SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
828 		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
829 		    OID_AUTO, "mmap_allow_write",
830 		    CTLFLAG_RW, &sc->mmap_allow_write, 0,
831 		    "Allow userland write to the HPET register space");
832 	} else {
833 		device_printf(dev, "could not create /dev/hpet%d, error %d\n",
834 		    device_get_unit(dev), error);
835 	}
836 
837 	return (0);
838 }
839 
840 static int
841 hpet_detach(device_t dev)
842 {
843 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
844 
845 	/* XXX Without a tc_remove() function, we can't detach. */
846 	return (EBUSY);
847 }
848 
849 static int
850 hpet_suspend(device_t dev)
851 {
852 //	struct hpet_softc *sc;
853 
854 	/*
855 	 * Disable the timer during suspend.  The timer will not lose
856 	 * its state in S1 or S2, but we are required to disable
857 	 * it.
858 	 */
859 //	sc = device_get_softc(dev);
860 //	hpet_disable(sc);
861 
862 	return (0);
863 }
864 
865 static int
866 hpet_resume(device_t dev)
867 {
868 	struct hpet_softc *sc;
869 	struct hpet_timer *t;
870 	int i;
871 
872 	/* Re-enable the timer after a resume to keep the clock advancing. */
873 	sc = device_get_softc(dev);
874 	hpet_enable(sc);
875 	/* Restart event timers that were running on suspend. */
876 	for (i = 0; i < sc->num_timers; i++) {
877 		t = &sc->t[i];
878 #ifdef DEV_APIC
879 		if (t->irq >= 0 && (sc->legacy_route == 0 || i >= 2)) {
880 			uint64_t addr;
881 			uint32_t data;
882 
883 			if (PCIB_MAP_MSI(
884 			    device_get_parent(device_get_parent(dev)), dev,
885 			    t->irq, &addr, &data) == 0) {
886 				bus_write_4(sc->mem_res,
887 				    HPET_TIMER_FSB_ADDR(i), addr);
888 				bus_write_4(sc->mem_res,
889 				    HPET_TIMER_FSB_VAL(i), data);
890 			}
891 		}
892 #endif
893 		if (t->mode == TIMER_STOPPED)
894 			continue;
895 		t->next = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
896 		if (t->mode == TIMER_PERIODIC &&
897 		    (t->caps & HPET_TCAP_PER_INT) != 0) {
898 			t->caps |= HPET_TCNF_TYPE;
899 			t->next += t->div;
900 			bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
901 			    t->caps | HPET_TCNF_VAL_SET);
902 			bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
903 			    t->next);
904 			bus_read_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num));
905 			bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
906 			    t->div);
907 		} else {
908 			t->next += sc->freq / 1024;
909 			bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
910 			    t->next);
911 		}
912 		bus_write_4(sc->mem_res, HPET_ISR, 1 << t->num);
913 		bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), t->caps);
914 	}
915 	return (0);
916 }
917 
918 /* Print some basic latency/rate information to assist in debugging. */
919 static void
920 hpet_test(struct hpet_softc *sc)
921 {
922 	int i;
923 	uint32_t u1, u2;
924 	struct bintime b0, b1, b2;
925 	struct timespec ts;
926 
927 	binuptime(&b0);
928 	binuptime(&b0);
929 	binuptime(&b1);
930 	u1 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
931 	for (i = 1; i < 1000; i++)
932 		u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
933 	binuptime(&b2);
934 	u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
935 
936 	bintime_sub(&b2, &b1);
937 	bintime_sub(&b1, &b0);
938 	bintime_sub(&b2, &b1);
939 	bintime2timespec(&b2, &ts);
940 
941 	device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
942 	    (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1);
943 
944 	device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);
945 }
946 
947 #ifdef DEV_APIC
948 static int
949 hpet_remap_intr(device_t dev, device_t child, u_int irq)
950 {
951 	struct hpet_softc *sc = device_get_softc(dev);
952 	struct hpet_timer *t;
953 	uint64_t addr;
954 	uint32_t data;
955 	int error, i;
956 
957 	for (i = 0; i < sc->num_timers; i++) {
958 		t = &sc->t[i];
959 		if (t->irq != irq)
960 			continue;
961 		error = PCIB_MAP_MSI(
962 		    device_get_parent(device_get_parent(dev)), dev,
963 		    irq, &addr, &data);
964 		if (error)
965 			return (error);
966 		hpet_disable(sc); /* Stop timer to avoid interrupt loss. */
967 		bus_write_4(sc->mem_res, HPET_TIMER_FSB_ADDR(i), addr);
968 		bus_write_4(sc->mem_res, HPET_TIMER_FSB_VAL(i), data);
969 		hpet_enable(sc);
970 		return (0);
971 	}
972 	return (ENOENT);
973 }
974 #endif
975 
976 static device_method_t hpet_methods[] = {
977 	/* Device interface */
978 	DEVMETHOD(device_identify, hpet_identify),
979 	DEVMETHOD(device_probe, hpet_probe),
980 	DEVMETHOD(device_attach, hpet_attach),
981 	DEVMETHOD(device_detach, hpet_detach),
982 	DEVMETHOD(device_suspend, hpet_suspend),
983 	DEVMETHOD(device_resume, hpet_resume),
984 
985 #ifdef DEV_APIC
986 	DEVMETHOD(bus_remap_intr, hpet_remap_intr),
987 #endif
988 
989 	DEVMETHOD_END
990 };
991 
992 static driver_t	hpet_driver = {
993 	"hpet",
994 	hpet_methods,
995 	sizeof(struct hpet_softc),
996 };
997 
998 DRIVER_MODULE(hpet, acpi, hpet_driver, hpet_devclass, 0, 0);
999 MODULE_DEPEND(hpet, acpi, 1, 1, 1);
1000