2 * Copyright (c) 2005 Poul-Henning Kamp
3 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #if defined(__amd64__) || defined(__ia64__)
37 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
47 #include <sys/sysctl.h>
48 #include <sys/timeet.h>
49 #include <sys/timetc.h>
51 #include <contrib/dev/acpica/include/acpi.h>
52 #include <contrib/dev/acpica/include/accommon.h>
54 #include <dev/acpica/acpivar.h>
55 #include <dev/acpica/acpi_hpet.h>
61 #define HPET_VENDID_AMD 0x4353
62 #define HPET_VENDID_AMD2 0x1022
63 #define HPET_VENDID_INTEL 0x8086
64 #define HPET_VENDID_NVIDIA 0x10de
65 #define HPET_VENDID_SW 0x1166
67 ACPI_SERIAL_DECL(hpet, "ACPI HPET support");
69 static devclass_t hpet_devclass;
71 /* ACPI CA debugging */
72 #define _COMPONENT ACPI_TIMER
73 ACPI_MODULE_NAME("HPET")
83 uint32_t allowed_irqs;
84 struct resource *mem_res;
85 struct resource *intr_res;
90 struct timecounter tc;
93 struct hpet_softc *sc;
101 int pcpu_slaves[MAXCPU];
102 struct resource *intr_res;
113 int mmap_allow_write;
116 static d_open_t hpet_open;
117 static d_mmap_t hpet_mmap;
119 static struct cdevsw hpet_cdevsw = {
120 .d_version = D_VERSION,
126 static u_int hpet_get_timecount(struct timecounter *tc);
127 static void hpet_test(struct hpet_softc *sc);
129 static char *hpet_ids[] = { "PNP0103", NULL };
132 hpet_get_timecount(struct timecounter *tc)
134 struct hpet_softc *sc;
137 return (bus_read_4(sc->mem_res, HPET_MAIN_COUNTER));
141 hpet_enable(struct hpet_softc *sc)
145 val = bus_read_4(sc->mem_res, HPET_CONFIG);
146 if (sc->legacy_route)
147 val |= HPET_CNF_LEG_RT;
149 val &= ~HPET_CNF_LEG_RT;
150 val |= HPET_CNF_ENABLE;
151 bus_write_4(sc->mem_res, HPET_CONFIG, val);
155 hpet_disable(struct hpet_softc *sc)
159 val = bus_read_4(sc->mem_res, HPET_CONFIG);
160 val &= ~HPET_CNF_ENABLE;
161 bus_write_4(sc->mem_res, HPET_CONFIG, val);
165 hpet_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
167 struct hpet_timer *mt = (struct hpet_timer *)et->et_priv;
168 struct hpet_timer *t;
169 struct hpet_softc *sc = mt->sc;
172 t = (mt->pcpu_master < 0) ? mt : &sc->t[mt->pcpu_slaves[curcpu]];
175 t->div = (sc->freq * period) >> 32;
181 fdiv = (sc->freq * first) >> 32;
185 bus_write_4(sc->mem_res, HPET_ISR, 1 << t->num);
186 t->caps |= HPET_TCNF_INT_ENB;
187 now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
189 t->next = now + fdiv;
190 if (t->mode == 1 && (t->caps & HPET_TCAP_PER_INT)) {
191 t->caps |= HPET_TCNF_TYPE;
192 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
193 t->caps | HPET_TCNF_VAL_SET);
194 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
196 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
199 t->caps &= ~HPET_TCNF_TYPE;
200 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
202 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
205 now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
206 if ((int32_t)(now - t->next + HPET_MIN_CYCLES) >= 0) {
214 hpet_stop(struct eventtimer *et)
216 struct hpet_timer *mt = (struct hpet_timer *)et->et_priv;
217 struct hpet_timer *t;
218 struct hpet_softc *sc = mt->sc;
220 t = (mt->pcpu_master < 0) ? mt : &sc->t[mt->pcpu_slaves[curcpu]];
222 t->caps &= ~(HPET_TCNF_INT_ENB | HPET_TCNF_TYPE);
223 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), t->caps);
228 hpet_intr_single(void *arg)
230 struct hpet_timer *t = (struct hpet_timer *)arg;
231 struct hpet_timer *mt;
232 struct hpet_softc *sc = t->sc;
236 return (FILTER_STRAY);
237 /* Check that per-CPU timer interrupt reached right CPU. */
238 if (t->pcpu_cpu >= 0 && t->pcpu_cpu != curcpu) {
239 if ((++t->pcpu_misrouted) % 32 == 0) {
240 printf("HPET interrupt routed to the wrong CPU"
241 " (timer %d CPU %d -> %d)!\n",
242 t->num, t->pcpu_cpu, curcpu);
246 * Reload timer, hoping that next time may be more lucky
247 * (system will manage proper interrupt binding).
249 if ((t->mode == 1 && (t->caps & HPET_TCAP_PER_INT) == 0) ||
251 t->next = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER) +
253 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
256 return (FILTER_HANDLED);
259 (t->caps & HPET_TCAP_PER_INT) == 0) {
261 now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
262 if ((int32_t)((now + t->div / 2) - t->next) > 0)
263 t->next = now + t->div / 2;
264 bus_write_4(sc->mem_res,
265 HPET_TIMER_COMPARATOR(t->num), t->next);
266 } else if (t->mode == 2)
268 mt = (t->pcpu_master < 0) ? t : &sc->t[t->pcpu_master];
269 if (mt->et.et_active)
270 mt->et.et_event_cb(&mt->et, mt->et.et_arg);
271 return (FILTER_HANDLED);
277 struct hpet_softc *sc = (struct hpet_softc *)arg;
281 val = bus_read_4(sc->mem_res, HPET_ISR);
283 bus_write_4(sc->mem_res, HPET_ISR, val);
285 for (i = 0; i < sc->num_timers; i++) {
286 if ((val & (1 << i)) == 0)
288 hpet_intr_single(&sc->t[i]);
290 return (FILTER_HANDLED);
292 return (FILTER_STRAY);
296 hpet_find(ACPI_HANDLE handle, UINT32 level, void *context,
300 uint32_t id = (uint32_t)(uintptr_t)context;
303 for (ids = hpet_ids; *ids != NULL; ids++) {
304 if (acpi_MatchHid(handle, *ids))
309 if (ACPI_FAILURE(acpi_GetInteger(handle, "_UID", &uid)) ||
311 *status = acpi_get_device(handle);
316 * Find an existing IRQ resource that matches the requested IRQ range
317 * and return its RID. If one is not found, use a new RID.
320 hpet_find_irq_rid(device_t dev, u_long start, u_long end)
325 for (rid = 0;; rid++) {
326 error = bus_get_resource(dev, SYS_RES_IRQ, rid, &irq, NULL);
327 if (error != 0 || (start <= irq && irq <= end))
333 hpet_open(struct cdev *cdev, int oflags, int devtype, struct thread *td)
335 struct hpet_softc *sc;
345 hpet_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
346 int nprot, vm_memattr_t *memattr)
348 struct hpet_softc *sc;
351 if (offset > rman_get_size(sc->mem_res))
353 if (!sc->mmap_allow_write && (nprot & PROT_WRITE))
355 *paddr = rman_get_start(sc->mem_res) + offset;
356 *memattr = VM_MEMATTR_UNCACHEABLE;
361 /* Discover the HPET via the ACPI table of the same name. */
363 hpet_identify(driver_t *driver, device_t parent)
365 ACPI_TABLE_HPET *hpet;
370 /* Only one HPET device can be added. */
371 if (devclass_get_device(hpet_devclass, 0))
374 /* Search for HPET table. */
375 status = AcpiGetTable(ACPI_SIG_HPET, i, (ACPI_TABLE_HEADER **)&hpet);
376 if (ACPI_FAILURE(status))
378 /* Search for HPET device with same ID. */
380 AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
381 100, hpet_find, NULL, (void *)(uintptr_t)hpet->Sequence,
383 /* If found - let it be probed in normal way. */
385 if (bus_get_resource(child, SYS_RES_MEMORY, 0,
387 bus_set_resource(child, SYS_RES_MEMORY, 0,
388 hpet->Address.Address, HPET_MEM_WIDTH);
391 /* If not - create it from table info. */
392 child = BUS_ADD_CHILD(parent, 2, "hpet", 0);
394 printf("%s: can't add child\n", __func__);
397 bus_set_resource(child, SYS_RES_MEMORY, 0, hpet->Address.Address,
403 hpet_probe(device_t dev)
405 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
407 if (acpi_disabled("hpet"))
409 if (acpi_get_handle(dev) != NULL &&
410 ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL)
413 device_set_desc(dev, "High Precision Event Timer");
418 hpet_attach(device_t dev)
420 struct hpet_softc *sc;
421 struct hpet_timer *t;
422 int i, j, num_msi, num_timers, num_percpu_et, num_percpu_t, cur_cpu;
424 static int maxhpetet = 0;
425 uint32_t val, val2, cvectors, dvectors;
426 uint16_t vendor, rev;
428 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
430 sc = device_get_softc(dev);
432 sc->handle = acpi_get_handle(dev);
435 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
437 if (sc->mem_res == NULL)
440 /* Validate that we can access the whole region. */
441 if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH) {
442 device_printf(dev, "memory region width %ld too small\n",
443 rman_get_size(sc->mem_res));
444 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
448 /* Be sure timer is enabled. */
451 /* Read basic statistics about the timer. */
452 val = bus_read_4(sc->mem_res, HPET_PERIOD);
454 device_printf(dev, "invalid period\n");
456 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
460 sc->freq = (1000000000000000LL + val / 2) / val;
461 sc->caps = bus_read_4(sc->mem_res, HPET_CAPABILITIES);
462 vendor = (sc->caps & HPET_CAP_VENDOR_ID) >> 16;
463 rev = sc->caps & HPET_CAP_REV_ID;
464 num_timers = 1 + ((sc->caps & HPET_CAP_NUM_TIM) >> 8);
466 * ATI/AMD violates IA-PC HPET (High Precision Event Timers)
467 * Specification and provides an off by one number
468 * of timers/comparators.
469 * Additionally, they use unregistered value in VENDOR_ID field.
471 if (vendor == HPET_VENDID_AMD && rev < 0x10 && num_timers > 0)
473 sc->num_timers = num_timers;
476 "vendor 0x%x, rev 0x%x, %jdHz%s, %d timers,%s\n",
477 vendor, rev, sc->freq,
478 (sc->caps & HPET_CAP_COUNT_SIZE) ? " 64bit" : "",
480 (sc->caps & HPET_CAP_LEG_RT) ? " legacy route" : "");
482 for (i = 0; i < num_timers; i++) {
490 t->pcpu_misrouted = 0;
492 t->caps = bus_read_4(sc->mem_res, HPET_TIMER_CAP_CNF(i));
493 t->vectors = bus_read_4(sc->mem_res, HPET_TIMER_CAP_CNF(i) + 4);
496 " t%d: irqs 0x%08x (%d)%s%s%s\n", i,
497 t->vectors, (t->caps & HPET_TCNF_INT_ROUTE) >> 9,
498 (t->caps & HPET_TCAP_FSB_INT_DEL) ? ", MSI" : "",
499 (t->caps & HPET_TCAP_SIZE) ? ", 64bit" : "",
500 (t->caps & HPET_TCAP_PER_INT) ? ", periodic" : "");
503 if (testenv("debug.acpi.hpet_test"))
506 * Don't attach if the timer never increments. Since the spec
507 * requires it to be at least 10 MHz, it has to change in 1 us.
509 val = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
511 val2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
513 device_printf(dev, "HPET never increments, disabling\n");
515 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
518 /* Announce first HPET as timecounter. */
519 if (device_get_unit(dev) == 0) {
520 sc->tc.tc_get_timecount = hpet_get_timecount,
521 sc->tc.tc_counter_mask = ~0u,
522 sc->tc.tc_name = "HPET",
523 sc->tc.tc_quality = 950,
524 sc->tc.tc_frequency = sc->freq;
528 /* If not disabled - setup and announce event timers. */
529 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
530 "clock", &i) == 0 && i == 0)
533 /* Check whether we can and want legacy routing. */
534 sc->legacy_route = 0;
535 resource_int_value(device_get_name(dev), device_get_unit(dev),
536 "legacy_route", &sc->legacy_route);
537 if ((sc->caps & HPET_CAP_LEG_RT) == 0)
538 sc->legacy_route = 0;
539 if (sc->legacy_route) {
540 sc->t[0].vectors = 0;
541 sc->t[1].vectors = 0;
544 /* Check what IRQs we want use. */
545 /* By default allow any PCI IRQs. */
546 sc->allowed_irqs = 0xffff0000;
548 * HPETs in AMD chipsets before SB800 have problems with IRQs >= 16
549 * Lower are also not always working for different reasons.
550 * SB800 fixed it, but seems do not implements level triggering
551 * properly, that makes it very unreliable - it freezes after any
552 * interrupt loss. Avoid legacy IRQs for AMD.
554 if (vendor == HPET_VENDID_AMD || vendor == HPET_VENDID_AMD2)
555 sc->allowed_irqs = 0x00000000;
557 * NVidia MCP5x chipsets have number of unexplained interrupt
558 * problems. For some reason, using HPET interrupts breaks HDA sound.
560 if (vendor == HPET_VENDID_NVIDIA && rev <= 0x01)
561 sc->allowed_irqs = 0x00000000;
563 * ServerWorks HT1000 reported to have problems with IRQs >= 16.
564 * Lower IRQs are working, but allowed mask is not set correctly.
565 * Legacy_route mode works fine.
567 if (vendor == HPET_VENDID_SW && rev <= 0x01)
568 sc->allowed_irqs = 0x00000000;
570 * Neither QEMU nor VirtualBox report supported IRQs correctly.
571 * The only way to use HPET there is to specify IRQs manually
572 * and/or use legacy_route. Legacy_route mode works on both.
575 sc->allowed_irqs = 0x00000000;
576 /* Let user override. */
577 resource_int_value(device_get_name(dev), device_get_unit(dev),
578 "allowed_irqs", &sc->allowed_irqs);
580 /* Get how much per-CPU timers we should try to provide. */
582 resource_int_value(device_get_name(dev), device_get_unit(dev),
583 "per_cpu", &sc->per_cpu);
587 /* Find IRQ vectors for all timers. */
588 cvectors = sc->allowed_irqs & 0xffff0000;
589 dvectors = sc->allowed_irqs & 0x0000ffff;
590 if (sc->legacy_route)
591 dvectors &= 0x0000fefe;
592 for (i = 0; i < num_timers; i++) {
594 if (sc->legacy_route && i < 2)
595 t->irq = (i == 0) ? 0 : 8;
597 else if (t->caps & HPET_TCAP_FSB_INT_DEL) {
598 if ((j = PCIB_ALLOC_MSIX(
599 device_get_parent(device_get_parent(dev)), dev,
602 "Can't allocate interrupt for t%d.\n", j);
606 else if (dvectors & t->vectors) {
607 t->irq = ffs(dvectors & t->vectors) - 1;
608 dvectors &= ~(1 << t->irq);
611 t->intr_rid = hpet_find_irq_rid(dev, t->irq, t->irq);
612 t->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
613 &t->intr_rid, t->irq, t->irq, 1, RF_ACTIVE);
614 if (t->intr_res == NULL) {
617 "Can't map interrupt for t%d.\n", i);
618 } else if (bus_setup_intr(dev, t->intr_res,
619 INTR_TYPE_CLK, hpet_intr_single, NULL, t,
620 &t->intr_handle) != 0) {
623 "Can't setup interrupt for t%d.\n", i);
625 bus_describe_intr(dev, t->intr_res,
626 t->intr_handle, "t%d", i);
630 if (t->irq < 0 && (cvectors & t->vectors) != 0) {
631 cvectors &= t->vectors;
632 sc->useirq |= (1 << i);
635 if (sc->legacy_route && sc->t[0].irq < 0 && sc->t[1].irq < 0)
636 sc->legacy_route = 0;
637 if (sc->legacy_route)
639 /* Group timers for per-CPU operation. */
640 num_percpu_et = min(num_msi / mp_ncpus, sc->per_cpu);
641 num_percpu_t = num_percpu_et * mp_ncpus;
643 cur_cpu = CPU_FIRST();
644 for (i = 0; i < num_timers; i++) {
646 if (t->irq >= 0 && num_percpu_t > 0) {
647 if (cur_cpu == CPU_FIRST())
649 t->pcpu_cpu = cur_cpu;
650 t->pcpu_master = pcpu_master;
652 pcpu_slaves[cur_cpu] = i;
653 bus_bind_intr(dev, t->intr_res, cur_cpu);
654 cur_cpu = CPU_NEXT(cur_cpu);
656 } else if (t->irq >= 0)
657 bus_bind_intr(dev, t->intr_res, CPU_FIRST());
659 bus_write_4(sc->mem_res, HPET_ISR, 0xffffffff);
661 /* If at least one timer needs legacy IRQ - set it up. */
663 j = i = fls(cvectors) - 1;
664 while (j > 0 && (cvectors & (1 << (j - 1))) != 0)
666 sc->intr_rid = hpet_find_irq_rid(dev, j, i);
667 sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
668 &sc->intr_rid, j, i, 1, RF_SHAREABLE | RF_ACTIVE);
669 if (sc->intr_res == NULL)
670 device_printf(dev, "Can't map interrupt.\n");
671 else if (bus_setup_intr(dev, sc->intr_res, INTR_TYPE_CLK,
672 hpet_intr, NULL, sc, &sc->intr_handle) != 0) {
673 device_printf(dev, "Can't setup interrupt.\n");
675 sc->irq = rman_get_start(sc->intr_res);
676 /* Bind IRQ to BSP to avoid live migration. */
677 bus_bind_intr(dev, sc->intr_res, CPU_FIRST());
680 /* Program and announce event timers. */
681 for (i = 0; i < num_timers; i++) {
683 t->caps &= ~(HPET_TCNF_FSB_EN | HPET_TCNF_INT_ROUTE);
684 t->caps &= ~(HPET_TCNF_VAL_SET | HPET_TCNF_INT_ENB);
685 t->caps &= ~(HPET_TCNF_INT_TYPE);
686 t->caps |= HPET_TCNF_32MODE;
687 if (t->irq >= 0 && sc->legacy_route && i < 2) {
688 /* Legacy route doesn't need more configuration. */
691 if ((t->caps & HPET_TCAP_FSB_INT_DEL) && t->irq >= 0) {
696 device_get_parent(device_get_parent(dev)), dev,
697 t->irq, &addr, &data) == 0) {
698 bus_write_4(sc->mem_res,
699 HPET_TIMER_FSB_ADDR(i), addr);
700 bus_write_4(sc->mem_res,
701 HPET_TIMER_FSB_VAL(i), data);
702 t->caps |= HPET_TCNF_FSB_EN;
708 t->caps |= (t->irq << 9);
709 else if (sc->irq >= 0 && (t->vectors & (1 << sc->irq)))
710 t->caps |= (sc->irq << 9) | HPET_TCNF_INT_TYPE;
711 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(i), t->caps);
712 /* Skip event timers without set up IRQ. */
714 (sc->irq < 0 || (t->vectors & (1 << sc->irq)) == 0))
716 /* Announce the reset. */
718 t->et.et_name = "HPET";
720 sprintf(t->name, "HPET%d", maxhpetet);
721 t->et.et_name = t->name;
723 t->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT;
724 t->et.et_quality = 450;
725 if (t->pcpu_master >= 0) {
726 t->et.et_flags |= ET_FLAGS_PERCPU;
727 t->et.et_quality += 100;
728 } else if (mp_ncpus >= 8)
729 t->et.et_quality -= 100;
730 if ((t->caps & HPET_TCAP_PER_INT) == 0)
731 t->et.et_quality -= 10;
732 t->et.et_frequency = sc->freq;
733 t->et.et_min_period =
734 ((uint64_t)(HPET_MIN_CYCLES * 2) << 32) / sc->freq;
735 t->et.et_max_period = (0xfffffffeLLU << 32) / sc->freq;
736 t->et.et_start = hpet_start;
737 t->et.et_stop = hpet_stop;
738 t->et.et_priv = &sc->t[i];
739 if (t->pcpu_master < 0 || t->pcpu_master == i) {
745 sc->pdev = make_dev(&hpet_cdevsw, 0, UID_ROOT, GID_WHEEL,
746 0600, "hpet%d", device_get_unit(dev));
748 sc->pdev->si_drv1 = sc;
750 TUNABLE_INT_FETCH("hw.acpi.hpet.mmap_allow",
752 sc->mmap_allow_write = 1;
753 TUNABLE_INT_FETCH("hw.acpi.hpet.mmap_allow_write",
754 &sc->mmap_allow_write);
755 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
756 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
757 OID_AUTO, "mmap_allow",
758 CTLFLAG_RW, &sc->mmap_allow, 0,
759 "Allow userland to memory map HPET");
760 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
761 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
762 OID_AUTO, "mmap_allow_write",
763 CTLFLAG_RW, &sc->mmap_allow_write, 0,
764 "Allow userland write to the HPET register space");
766 device_printf(dev, "could not create /dev/hpet%d\n",
767 device_get_unit(dev));
773 hpet_detach(device_t dev)
775 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
777 /* XXX Without a tc_remove() function, we can't detach. */
782 hpet_suspend(device_t dev)
784 // struct hpet_softc *sc;
787 * Disable the timer during suspend. The timer will not lose
788 * its state in S1 or S2, but we are required to disable
791 // sc = device_get_softc(dev);
798 hpet_resume(device_t dev)
800 struct hpet_softc *sc;
801 struct hpet_timer *t;
804 /* Re-enable the timer after a resume to keep the clock advancing. */
805 sc = device_get_softc(dev);
807 /* Restart event timers that were running on suspend. */
808 for (i = 0; i < sc->num_timers; i++) {
811 if (t->irq >= 0 && (sc->legacy_route == 0 || i >= 2)) {
816 device_get_parent(device_get_parent(dev)), dev,
817 t->irq, &addr, &data) == 0) {
818 bus_write_4(sc->mem_res,
819 HPET_TIMER_FSB_ADDR(i), addr);
820 bus_write_4(sc->mem_res,
821 HPET_TIMER_FSB_VAL(i), data);
827 t->next = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
828 if (t->mode == 1 && (t->caps & HPET_TCAP_PER_INT)) {
829 t->caps |= HPET_TCNF_TYPE;
831 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
832 t->caps | HPET_TCNF_VAL_SET);
833 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
835 bus_read_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num));
836 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
839 t->next += sc->freq / 1024;
840 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
843 bus_write_4(sc->mem_res, HPET_ISR, 1 << t->num);
844 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), t->caps);
849 /* Print some basic latency/rate information to assist in debugging. */
851 hpet_test(struct hpet_softc *sc)
855 struct bintime b0, b1, b2;
861 u1 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
862 for (i = 1; i < 1000; i++)
863 u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
865 u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
867 bintime_sub(&b2, &b1);
868 bintime_sub(&b1, &b0);
869 bintime_sub(&b2, &b1);
870 bintime2timespec(&b2, &ts);
872 device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
873 (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1);
875 device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);
880 hpet_remap_intr(device_t dev, device_t child, u_int irq)
882 struct hpet_softc *sc = device_get_softc(dev);
883 struct hpet_timer *t;
888 for (i = 0; i < sc->num_timers; i++) {
892 error = PCIB_MAP_MSI(
893 device_get_parent(device_get_parent(dev)), dev,
897 hpet_disable(sc); /* Stop timer to avoid interrupt loss. */
898 bus_write_4(sc->mem_res, HPET_TIMER_FSB_ADDR(i), addr);
899 bus_write_4(sc->mem_res, HPET_TIMER_FSB_VAL(i), data);
907 static device_method_t hpet_methods[] = {
908 /* Device interface */
909 DEVMETHOD(device_identify, hpet_identify),
910 DEVMETHOD(device_probe, hpet_probe),
911 DEVMETHOD(device_attach, hpet_attach),
912 DEVMETHOD(device_detach, hpet_detach),
913 DEVMETHOD(device_suspend, hpet_suspend),
914 DEVMETHOD(device_resume, hpet_resume),
917 DEVMETHOD(bus_remap_intr, hpet_remap_intr),
923 static driver_t hpet_driver = {
926 sizeof(struct hpet_softc),
929 DRIVER_MODULE(hpet, acpi, hpet_driver, hpet_devclass, 0, 0);
930 MODULE_DEPEND(hpet, acpi, 1, 1, 1);