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 #include "opt_compat.h"
34 #if defined(__amd64__)
39 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
49 #include <sys/sysctl.h>
50 #include <sys/timeet.h>
51 #include <sys/timetc.h>
54 #include <contrib/dev/acpica/include/acpi.h>
55 #include <contrib/dev/acpica/include/accommon.h>
57 #include <dev/acpica/acpivar.h>
58 #include <dev/acpica/acpi_hpet.h>
64 #define HPET_VENDID_AMD 0x4353
65 #define HPET_VENDID_AMD2 0x1022
66 #define HPET_VENDID_INTEL 0x8086
67 #define HPET_VENDID_NVIDIA 0x10de
68 #define HPET_VENDID_SW 0x1166
70 ACPI_SERIAL_DECL(hpet, "ACPI HPET support");
72 static devclass_t hpet_devclass;
74 /* ACPI CA debugging */
75 #define _COMPONENT ACPI_TIMER
76 ACPI_MODULE_NAME("HPET")
86 uint32_t allowed_irqs;
87 struct resource *mem_res;
88 struct resource *intr_res;
94 struct timecounter tc;
97 struct hpet_softc *sc;
105 int pcpu_slaves[MAXCPU];
106 struct resource *intr_res;
117 int mmap_allow_write;
120 static d_open_t hpet_open;
121 static d_mmap_t hpet_mmap;
123 static struct cdevsw hpet_cdevsw = {
124 .d_version = D_VERSION,
130 static u_int hpet_get_timecount(struct timecounter *tc);
131 static void hpet_test(struct hpet_softc *sc);
133 static char *hpet_ids[] = { "PNP0103", NULL };
135 /* Knob to disable acpi_hpet device */
136 bool acpi_hpet_disabled = false;
139 hpet_get_timecount(struct timecounter *tc)
141 struct hpet_softc *sc;
144 return (bus_read_4(sc->mem_res, HPET_MAIN_COUNTER));
148 hpet_vdso_timehands(struct vdso_timehands *vdso_th, struct timecounter *tc)
150 struct hpet_softc *sc;
153 vdso_th->th_algo = VDSO_TH_ALGO_X86_HPET;
154 vdso_th->th_x86_shift = 0;
155 vdso_th->th_x86_hpet_idx = device_get_unit(sc->dev);
156 bzero(vdso_th->th_res, sizeof(vdso_th->th_res));
157 return (sc->mmap_allow != 0);
160 #ifdef COMPAT_FREEBSD32
162 hpet_vdso_timehands32(struct vdso_timehands32 *vdso_th32,
163 struct timecounter *tc)
165 struct hpet_softc *sc;
168 vdso_th32->th_algo = VDSO_TH_ALGO_X86_HPET;
169 vdso_th32->th_x86_shift = 0;
170 vdso_th32->th_x86_hpet_idx = device_get_unit(sc->dev);
171 bzero(vdso_th32->th_res, sizeof(vdso_th32->th_res));
172 return (sc->mmap_allow != 0);
177 hpet_enable(struct hpet_softc *sc)
181 val = bus_read_4(sc->mem_res, HPET_CONFIG);
182 if (sc->legacy_route)
183 val |= HPET_CNF_LEG_RT;
185 val &= ~HPET_CNF_LEG_RT;
186 val |= HPET_CNF_ENABLE;
187 bus_write_4(sc->mem_res, HPET_CONFIG, val);
191 hpet_disable(struct hpet_softc *sc)
195 val = bus_read_4(sc->mem_res, HPET_CONFIG);
196 val &= ~HPET_CNF_ENABLE;
197 bus_write_4(sc->mem_res, HPET_CONFIG, val);
201 hpet_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
203 struct hpet_timer *mt = (struct hpet_timer *)et->et_priv;
204 struct hpet_timer *t;
205 struct hpet_softc *sc = mt->sc;
208 t = (mt->pcpu_master < 0) ? mt : &sc->t[mt->pcpu_slaves[curcpu]];
211 t->div = (sc->freq * period) >> 32;
217 fdiv = (sc->freq * first) >> 32;
221 bus_write_4(sc->mem_res, HPET_ISR, 1 << t->num);
222 t->caps |= HPET_TCNF_INT_ENB;
223 now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
225 t->next = now + fdiv;
226 if (t->mode == 1 && (t->caps & HPET_TCAP_PER_INT)) {
227 t->caps |= HPET_TCNF_TYPE;
228 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
229 t->caps | HPET_TCNF_VAL_SET);
230 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
232 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
235 t->caps &= ~HPET_TCNF_TYPE;
236 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
238 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
241 now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
242 if ((int32_t)(now - t->next + HPET_MIN_CYCLES) >= 0) {
250 hpet_stop(struct eventtimer *et)
252 struct hpet_timer *mt = (struct hpet_timer *)et->et_priv;
253 struct hpet_timer *t;
254 struct hpet_softc *sc = mt->sc;
256 t = (mt->pcpu_master < 0) ? mt : &sc->t[mt->pcpu_slaves[curcpu]];
258 t->caps &= ~(HPET_TCNF_INT_ENB | HPET_TCNF_TYPE);
259 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), t->caps);
264 hpet_intr_single(void *arg)
266 struct hpet_timer *t = (struct hpet_timer *)arg;
267 struct hpet_timer *mt;
268 struct hpet_softc *sc = t->sc;
272 return (FILTER_STRAY);
273 /* Check that per-CPU timer interrupt reached right CPU. */
274 if (t->pcpu_cpu >= 0 && t->pcpu_cpu != curcpu) {
275 if ((++t->pcpu_misrouted) % 32 == 0) {
276 printf("HPET interrupt routed to the wrong CPU"
277 " (timer %d CPU %d -> %d)!\n",
278 t->num, t->pcpu_cpu, curcpu);
282 * Reload timer, hoping that next time may be more lucky
283 * (system will manage proper interrupt binding).
285 if ((t->mode == 1 && (t->caps & HPET_TCAP_PER_INT) == 0) ||
287 t->next = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER) +
289 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
292 return (FILTER_HANDLED);
295 (t->caps & HPET_TCAP_PER_INT) == 0) {
297 now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
298 if ((int32_t)((now + t->div / 2) - t->next) > 0)
299 t->next = now + t->div / 2;
300 bus_write_4(sc->mem_res,
301 HPET_TIMER_COMPARATOR(t->num), t->next);
302 } else if (t->mode == 2)
304 mt = (t->pcpu_master < 0) ? t : &sc->t[t->pcpu_master];
305 if (mt->et.et_active)
306 mt->et.et_event_cb(&mt->et, mt->et.et_arg);
307 return (FILTER_HANDLED);
313 struct hpet_softc *sc = (struct hpet_softc *)arg;
317 val = bus_read_4(sc->mem_res, HPET_ISR);
319 bus_write_4(sc->mem_res, HPET_ISR, val);
321 for (i = 0; i < sc->num_timers; i++) {
322 if ((val & (1 << i)) == 0)
324 hpet_intr_single(&sc->t[i]);
326 return (FILTER_HANDLED);
328 return (FILTER_STRAY);
332 hpet_get_uid(device_t dev)
334 struct hpet_softc *sc;
336 sc = device_get_softc(dev);
337 return (sc->acpi_uid);
341 hpet_find(ACPI_HANDLE handle, UINT32 level, void *context,
345 uint32_t id = (uint32_t)(uintptr_t)context;
348 for (ids = hpet_ids; *ids != NULL; ids++) {
349 if (acpi_MatchHid(handle, *ids))
354 if (ACPI_FAILURE(acpi_GetInteger(handle, "_UID", &uid)) ||
356 *status = acpi_get_device(handle);
361 * Find an existing IRQ resource that matches the requested IRQ range
362 * and return its RID. If one is not found, use a new RID.
365 hpet_find_irq_rid(device_t dev, u_long start, u_long end)
370 for (rid = 0;; rid++) {
371 error = bus_get_resource(dev, SYS_RES_IRQ, rid, &irq, NULL);
372 if (error != 0 || (start <= irq && irq <= end))
378 hpet_open(struct cdev *cdev, int oflags, int devtype, struct thread *td)
380 struct hpet_softc *sc;
390 hpet_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
391 int nprot, vm_memattr_t *memattr)
393 struct hpet_softc *sc;
396 if (offset > rman_get_size(sc->mem_res))
398 if (!sc->mmap_allow_write && (nprot & PROT_WRITE))
400 *paddr = rman_get_start(sc->mem_res) + offset;
401 *memattr = VM_MEMATTR_UNCACHEABLE;
406 /* Discover the HPET via the ACPI table of the same name. */
408 hpet_identify(driver_t *driver, device_t parent)
410 ACPI_TABLE_HPET *hpet;
415 /* Only one HPET device can be added. */
416 if (devclass_get_device(hpet_devclass, 0))
419 /* Search for HPET table. */
420 status = AcpiGetTable(ACPI_SIG_HPET, i, (ACPI_TABLE_HEADER **)&hpet);
421 if (ACPI_FAILURE(status))
423 /* Search for HPET device with same ID. */
425 AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
426 100, hpet_find, NULL, (void *)(uintptr_t)hpet->Sequence,
428 /* If found - let it be probed in normal way. */
430 if (bus_get_resource(child, SYS_RES_MEMORY, 0,
432 bus_set_resource(child, SYS_RES_MEMORY, 0,
433 hpet->Address.Address, HPET_MEM_WIDTH);
436 /* If not - create it from table info. */
437 child = BUS_ADD_CHILD(parent, 2, "hpet", 0);
439 printf("%s: can't add child\n", __func__);
442 bus_set_resource(child, SYS_RES_MEMORY, 0, hpet->Address.Address,
448 hpet_probe(device_t dev)
450 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
452 if (acpi_disabled("hpet") || acpi_hpet_disabled)
454 if (acpi_get_handle(dev) != NULL &&
455 ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL)
458 device_set_desc(dev, "High Precision Event Timer");
463 hpet_attach(device_t dev)
465 struct hpet_softc *sc;
466 struct hpet_timer *t;
467 struct make_dev_args mda;
468 int i, j, num_msi, num_timers, num_percpu_et, num_percpu_t, cur_cpu;
469 int pcpu_master, error;
470 static int maxhpetet = 0;
471 uint32_t val, val2, cvectors, dvectors;
472 uint16_t vendor, rev;
474 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
476 sc = device_get_softc(dev);
478 sc->handle = acpi_get_handle(dev);
481 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
483 if (sc->mem_res == NULL)
486 /* Validate that we can access the whole region. */
487 if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH) {
488 device_printf(dev, "memory region width %jd too small\n",
489 rman_get_size(sc->mem_res));
490 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
494 /* Be sure timer is enabled. */
497 /* Read basic statistics about the timer. */
498 val = bus_read_4(sc->mem_res, HPET_PERIOD);
500 device_printf(dev, "invalid period\n");
502 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
506 sc->freq = (1000000000000000LL + val / 2) / val;
507 sc->caps = bus_read_4(sc->mem_res, HPET_CAPABILITIES);
508 vendor = (sc->caps & HPET_CAP_VENDOR_ID) >> 16;
509 rev = sc->caps & HPET_CAP_REV_ID;
510 num_timers = 1 + ((sc->caps & HPET_CAP_NUM_TIM) >> 8);
512 * ATI/AMD violates IA-PC HPET (High Precision Event Timers)
513 * Specification and provides an off by one number
514 * of timers/comparators.
515 * Additionally, they use unregistered value in VENDOR_ID field.
517 if (vendor == HPET_VENDID_AMD && rev < 0x10 && num_timers > 0)
519 sc->num_timers = num_timers;
522 "vendor 0x%x, rev 0x%x, %jdHz%s, %d timers,%s\n",
523 vendor, rev, sc->freq,
524 (sc->caps & HPET_CAP_COUNT_SIZE) ? " 64bit" : "",
526 (sc->caps & HPET_CAP_LEG_RT) ? " legacy route" : "");
528 for (i = 0; i < num_timers; i++) {
536 t->pcpu_misrouted = 0;
538 t->caps = bus_read_4(sc->mem_res, HPET_TIMER_CAP_CNF(i));
539 t->vectors = bus_read_4(sc->mem_res, HPET_TIMER_CAP_CNF(i) + 4);
542 " t%d: irqs 0x%08x (%d)%s%s%s\n", i,
543 t->vectors, (t->caps & HPET_TCNF_INT_ROUTE) >> 9,
544 (t->caps & HPET_TCAP_FSB_INT_DEL) ? ", MSI" : "",
545 (t->caps & HPET_TCAP_SIZE) ? ", 64bit" : "",
546 (t->caps & HPET_TCAP_PER_INT) ? ", periodic" : "");
549 if (testenv("debug.acpi.hpet_test"))
552 * Don't attach if the timer never increments. Since the spec
553 * requires it to be at least 10 MHz, it has to change in 1 us.
555 val = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
557 val2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
559 device_printf(dev, "HPET never increments, disabling\n");
561 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
564 /* Announce first HPET as timecounter. */
565 if (device_get_unit(dev) == 0) {
566 sc->tc.tc_get_timecount = hpet_get_timecount,
567 sc->tc.tc_counter_mask = ~0u,
568 sc->tc.tc_name = "HPET",
569 sc->tc.tc_quality = 950,
570 sc->tc.tc_frequency = sc->freq;
572 sc->tc.tc_fill_vdso_timehands = hpet_vdso_timehands;
573 #ifdef COMPAT_FREEBSD32
574 sc->tc.tc_fill_vdso_timehands32 = hpet_vdso_timehands32;
578 /* If not disabled - setup and announce event timers. */
579 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
580 "clock", &i) == 0 && i == 0)
583 /* Check whether we can and want legacy routing. */
584 sc->legacy_route = 0;
585 resource_int_value(device_get_name(dev), device_get_unit(dev),
586 "legacy_route", &sc->legacy_route);
587 if ((sc->caps & HPET_CAP_LEG_RT) == 0)
588 sc->legacy_route = 0;
589 if (sc->legacy_route) {
590 sc->t[0].vectors = 0;
591 sc->t[1].vectors = 0;
594 /* Check what IRQs we want use. */
595 /* By default allow any PCI IRQs. */
596 sc->allowed_irqs = 0xffff0000;
598 * HPETs in AMD chipsets before SB800 have problems with IRQs >= 16
599 * Lower are also not always working for different reasons.
600 * SB800 fixed it, but seems do not implements level triggering
601 * properly, that makes it very unreliable - it freezes after any
602 * interrupt loss. Avoid legacy IRQs for AMD.
604 if (vendor == HPET_VENDID_AMD || vendor == HPET_VENDID_AMD2)
605 sc->allowed_irqs = 0x00000000;
607 * NVidia MCP5x chipsets have number of unexplained interrupt
608 * problems. For some reason, using HPET interrupts breaks HDA sound.
610 if (vendor == HPET_VENDID_NVIDIA && rev <= 0x01)
611 sc->allowed_irqs = 0x00000000;
613 * ServerWorks HT1000 reported to have problems with IRQs >= 16.
614 * Lower IRQs are working, but allowed mask is not set correctly.
615 * Legacy_route mode works fine.
617 if (vendor == HPET_VENDID_SW && rev <= 0x01)
618 sc->allowed_irqs = 0x00000000;
620 * Neither QEMU nor VirtualBox report supported IRQs correctly.
621 * The only way to use HPET there is to specify IRQs manually
622 * and/or use legacy_route. Legacy_route mode works on both.
625 sc->allowed_irqs = 0x00000000;
626 /* Let user override. */
627 resource_int_value(device_get_name(dev), device_get_unit(dev),
628 "allowed_irqs", &sc->allowed_irqs);
630 /* Get how much per-CPU timers we should try to provide. */
632 resource_int_value(device_get_name(dev), device_get_unit(dev),
633 "per_cpu", &sc->per_cpu);
637 /* Find IRQ vectors for all timers. */
638 cvectors = sc->allowed_irqs & 0xffff0000;
639 dvectors = sc->allowed_irqs & 0x0000ffff;
640 if (sc->legacy_route)
641 dvectors &= 0x0000fefe;
642 for (i = 0; i < num_timers; i++) {
644 if (sc->legacy_route && i < 2)
645 t->irq = (i == 0) ? 0 : 8;
647 else if (t->caps & HPET_TCAP_FSB_INT_DEL) {
648 if ((j = PCIB_ALLOC_MSIX(
649 device_get_parent(device_get_parent(dev)), dev,
652 "Can't allocate interrupt for t%d: %d\n",
657 else if (dvectors & t->vectors) {
658 t->irq = ffs(dvectors & t->vectors) - 1;
659 dvectors &= ~(1 << t->irq);
662 t->intr_rid = hpet_find_irq_rid(dev, t->irq, t->irq);
663 t->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
664 &t->intr_rid, t->irq, t->irq, 1, RF_ACTIVE);
665 if (t->intr_res == NULL) {
668 "Can't map interrupt for t%d.\n", i);
669 } else if (bus_setup_intr(dev, t->intr_res,
670 INTR_TYPE_CLK, hpet_intr_single, NULL, t,
671 &t->intr_handle) != 0) {
674 "Can't setup interrupt for t%d.\n", i);
676 bus_describe_intr(dev, t->intr_res,
677 t->intr_handle, "t%d", i);
681 if (t->irq < 0 && (cvectors & t->vectors) != 0) {
682 cvectors &= t->vectors;
683 sc->useirq |= (1 << i);
686 if (sc->legacy_route && sc->t[0].irq < 0 && sc->t[1].irq < 0)
687 sc->legacy_route = 0;
688 if (sc->legacy_route)
690 /* Group timers for per-CPU operation. */
691 num_percpu_et = min(num_msi / mp_ncpus, sc->per_cpu);
692 num_percpu_t = num_percpu_et * mp_ncpus;
694 cur_cpu = CPU_FIRST();
695 for (i = 0; i < num_timers; i++) {
697 if (t->irq >= 0 && num_percpu_t > 0) {
698 if (cur_cpu == CPU_FIRST())
700 t->pcpu_cpu = cur_cpu;
701 t->pcpu_master = pcpu_master;
703 pcpu_slaves[cur_cpu] = i;
704 bus_bind_intr(dev, t->intr_res, cur_cpu);
705 cur_cpu = CPU_NEXT(cur_cpu);
707 } else if (t->irq >= 0)
708 bus_bind_intr(dev, t->intr_res, CPU_FIRST());
710 bus_write_4(sc->mem_res, HPET_ISR, 0xffffffff);
712 /* If at least one timer needs legacy IRQ - set it up. */
714 j = i = fls(cvectors) - 1;
715 while (j > 0 && (cvectors & (1 << (j - 1))) != 0)
717 sc->intr_rid = hpet_find_irq_rid(dev, j, i);
718 sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
719 &sc->intr_rid, j, i, 1, RF_SHAREABLE | RF_ACTIVE);
720 if (sc->intr_res == NULL)
721 device_printf(dev, "Can't map interrupt.\n");
722 else if (bus_setup_intr(dev, sc->intr_res, INTR_TYPE_CLK,
723 hpet_intr, NULL, sc, &sc->intr_handle) != 0) {
724 device_printf(dev, "Can't setup interrupt.\n");
726 sc->irq = rman_get_start(sc->intr_res);
727 /* Bind IRQ to BSP to avoid live migration. */
728 bus_bind_intr(dev, sc->intr_res, CPU_FIRST());
731 /* Program and announce event timers. */
732 for (i = 0; i < num_timers; i++) {
734 t->caps &= ~(HPET_TCNF_FSB_EN | HPET_TCNF_INT_ROUTE);
735 t->caps &= ~(HPET_TCNF_VAL_SET | HPET_TCNF_INT_ENB);
736 t->caps &= ~(HPET_TCNF_INT_TYPE);
737 t->caps |= HPET_TCNF_32MODE;
738 if (t->irq >= 0 && sc->legacy_route && i < 2) {
739 /* Legacy route doesn't need more configuration. */
742 if ((t->caps & HPET_TCAP_FSB_INT_DEL) && t->irq >= 0) {
747 device_get_parent(device_get_parent(dev)), dev,
748 t->irq, &addr, &data) == 0) {
749 bus_write_4(sc->mem_res,
750 HPET_TIMER_FSB_ADDR(i), addr);
751 bus_write_4(sc->mem_res,
752 HPET_TIMER_FSB_VAL(i), data);
753 t->caps |= HPET_TCNF_FSB_EN;
759 t->caps |= (t->irq << 9);
760 else if (sc->irq >= 0 && (t->vectors & (1 << sc->irq)))
761 t->caps |= (sc->irq << 9) | HPET_TCNF_INT_TYPE;
762 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(i), t->caps);
763 /* Skip event timers without set up IRQ. */
765 (sc->irq < 0 || (t->vectors & (1 << sc->irq)) == 0))
767 /* Announce the reset. */
769 t->et.et_name = "HPET";
771 sprintf(t->name, "HPET%d", maxhpetet);
772 t->et.et_name = t->name;
774 t->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT;
775 t->et.et_quality = 450;
776 if (t->pcpu_master >= 0) {
777 t->et.et_flags |= ET_FLAGS_PERCPU;
778 t->et.et_quality += 100;
779 } else if (mp_ncpus >= 8)
780 t->et.et_quality -= 100;
781 if ((t->caps & HPET_TCAP_PER_INT) == 0)
782 t->et.et_quality -= 10;
783 t->et.et_frequency = sc->freq;
784 t->et.et_min_period =
785 ((uint64_t)(HPET_MIN_CYCLES * 2) << 32) / sc->freq;
786 t->et.et_max_period = (0xfffffffeLLU << 32) / sc->freq;
787 t->et.et_start = hpet_start;
788 t->et.et_stop = hpet_stop;
789 t->et.et_priv = &sc->t[i];
790 if (t->pcpu_master < 0 || t->pcpu_master == i) {
795 acpi_GetInteger(sc->handle, "_UID", &sc->acpi_uid);
797 make_dev_args_init(&mda);
798 mda.mda_devsw = &hpet_cdevsw;
799 mda.mda_uid = UID_ROOT;
800 mda.mda_gid = GID_WHEEL;
802 mda.mda_si_drv1 = sc;
803 error = make_dev_s(&mda, &sc->pdev, "hpet%d", device_get_unit(dev));
806 TUNABLE_INT_FETCH("hw.acpi.hpet.mmap_allow",
808 sc->mmap_allow_write = 0;
809 TUNABLE_INT_FETCH("hw.acpi.hpet.mmap_allow_write",
810 &sc->mmap_allow_write);
811 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
812 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
813 OID_AUTO, "mmap_allow",
814 CTLFLAG_RW, &sc->mmap_allow, 0,
815 "Allow userland to memory map HPET");
816 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
817 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
818 OID_AUTO, "mmap_allow_write",
819 CTLFLAG_RW, &sc->mmap_allow_write, 0,
820 "Allow userland write to the HPET register space");
822 device_printf(dev, "could not create /dev/hpet%d, error %d\n",
823 device_get_unit(dev), error);
830 hpet_detach(device_t dev)
832 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
834 /* XXX Without a tc_remove() function, we can't detach. */
839 hpet_suspend(device_t dev)
841 // struct hpet_softc *sc;
844 * Disable the timer during suspend. The timer will not lose
845 * its state in S1 or S2, but we are required to disable
848 // sc = device_get_softc(dev);
855 hpet_resume(device_t dev)
857 struct hpet_softc *sc;
858 struct hpet_timer *t;
861 /* Re-enable the timer after a resume to keep the clock advancing. */
862 sc = device_get_softc(dev);
864 /* Restart event timers that were running on suspend. */
865 for (i = 0; i < sc->num_timers; i++) {
868 if (t->irq >= 0 && (sc->legacy_route == 0 || i >= 2)) {
873 device_get_parent(device_get_parent(dev)), dev,
874 t->irq, &addr, &data) == 0) {
875 bus_write_4(sc->mem_res,
876 HPET_TIMER_FSB_ADDR(i), addr);
877 bus_write_4(sc->mem_res,
878 HPET_TIMER_FSB_VAL(i), data);
884 t->next = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
885 if (t->mode == 1 && (t->caps & HPET_TCAP_PER_INT)) {
886 t->caps |= HPET_TCNF_TYPE;
888 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
889 t->caps | HPET_TCNF_VAL_SET);
890 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
892 bus_read_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num));
893 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
896 t->next += sc->freq / 1024;
897 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
900 bus_write_4(sc->mem_res, HPET_ISR, 1 << t->num);
901 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), t->caps);
906 /* Print some basic latency/rate information to assist in debugging. */
908 hpet_test(struct hpet_softc *sc)
912 struct bintime b0, b1, b2;
918 u1 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
919 for (i = 1; i < 1000; i++)
920 u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
922 u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
924 bintime_sub(&b2, &b1);
925 bintime_sub(&b1, &b0);
926 bintime_sub(&b2, &b1);
927 bintime2timespec(&b2, &ts);
929 device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
930 (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1);
932 device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);
937 hpet_remap_intr(device_t dev, device_t child, u_int irq)
939 struct hpet_softc *sc = device_get_softc(dev);
940 struct hpet_timer *t;
945 for (i = 0; i < sc->num_timers; i++) {
949 error = PCIB_MAP_MSI(
950 device_get_parent(device_get_parent(dev)), dev,
954 hpet_disable(sc); /* Stop timer to avoid interrupt loss. */
955 bus_write_4(sc->mem_res, HPET_TIMER_FSB_ADDR(i), addr);
956 bus_write_4(sc->mem_res, HPET_TIMER_FSB_VAL(i), data);
964 static device_method_t hpet_methods[] = {
965 /* Device interface */
966 DEVMETHOD(device_identify, hpet_identify),
967 DEVMETHOD(device_probe, hpet_probe),
968 DEVMETHOD(device_attach, hpet_attach),
969 DEVMETHOD(device_detach, hpet_detach),
970 DEVMETHOD(device_suspend, hpet_suspend),
971 DEVMETHOD(device_resume, hpet_resume),
974 DEVMETHOD(bus_remap_intr, hpet_remap_intr),
980 static driver_t hpet_driver = {
983 sizeof(struct hpet_softc),
986 DRIVER_MODULE(hpet, acpi, hpet_driver, hpet_devclass, 0, 0);
987 MODULE_DEPEND(hpet, acpi, 1, 1, 1);