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$");
33 #if defined(__amd64__)
38 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
48 #include <sys/sysctl.h>
49 #include <sys/timeet.h>
50 #include <sys/timetc.h>
53 #include <contrib/dev/acpica/include/acpi.h>
54 #include <contrib/dev/acpica/include/accommon.h>
56 #include <dev/acpica/acpivar.h>
57 #include <dev/acpica/acpi_hpet.h>
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
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;
100 #define TIMER_STOPPED 0
101 #define TIMER_PERIODIC 1
102 #define TIMER_ONESHOT 2
108 int pcpu_slaves[MAXCPU];
109 struct resource *intr_res;
120 int mmap_allow_write;
123 static d_open_t hpet_open;
124 static d_mmap_t hpet_mmap;
126 static struct cdevsw hpet_cdevsw = {
127 .d_version = D_VERSION,
133 static u_int hpet_get_timecount(struct timecounter *tc);
134 static void hpet_test(struct hpet_softc *sc);
136 static char *hpet_ids[] = { "PNP0103", NULL };
138 /* Knob to disable acpi_hpet device */
139 bool acpi_hpet_disabled = false;
142 hpet_get_timecount(struct timecounter *tc)
144 struct hpet_softc *sc;
147 return (bus_read_4(sc->mem_res, HPET_MAIN_COUNTER));
151 hpet_vdso_timehands(struct vdso_timehands *vdso_th, struct timecounter *tc)
153 struct hpet_softc *sc;
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);
165 #ifdef COMPAT_FREEBSD32
167 hpet_vdso_timehands32(struct vdso_timehands32 *vdso_th32,
168 struct timecounter *tc)
170 struct hpet_softc *sc;
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);
184 hpet_enable(struct hpet_softc *sc)
188 val = bus_read_4(sc->mem_res, HPET_CONFIG);
189 if (sc->legacy_route)
190 val |= HPET_CNF_LEG_RT;
192 val &= ~HPET_CNF_LEG_RT;
193 val |= HPET_CNF_ENABLE;
194 bus_write_4(sc->mem_res, HPET_CONFIG, val);
198 hpet_disable(struct hpet_softc *sc)
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);
208 hpet_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
210 struct hpet_timer *mt = (struct hpet_timer *)et->et_priv;
211 struct hpet_timer *t;
212 struct hpet_softc *sc = mt->sc;
215 t = (mt->pcpu_master < 0) ? mt : &sc->t[mt->pcpu_slaves[curcpu]];
217 t->mode = TIMER_PERIODIC;
218 t->div = (sc->freq * period) >> 32;
220 t->mode = TIMER_ONESHOT;
224 fdiv = (sc->freq * first) >> 32;
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);
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),
239 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
242 t->caps &= ~HPET_TCNF_TYPE;
243 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
245 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
248 now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
249 if ((int32_t)(now - t->next + HPET_MIN_CYCLES) >= 0) {
257 hpet_stop(struct eventtimer *et)
259 struct hpet_timer *mt = (struct hpet_timer *)et->et_priv;
260 struct hpet_timer *t;
261 struct hpet_softc *sc = mt->sc;
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);
271 hpet_intr_single(void *arg)
273 struct hpet_timer *t = (struct hpet_timer *)arg;
274 struct hpet_timer *mt;
275 struct hpet_softc *sc = t->sc;
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);
289 * Reload timer, hoping that next time may be more lucky
290 * (system will manage proper interrupt binding).
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) +
297 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
300 return (FILTER_HANDLED);
302 if (t->mode == TIMER_PERIODIC &&
303 (t->caps & HPET_TCAP_PER_INT) == 0) {
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);
321 struct hpet_softc *sc = (struct hpet_softc *)arg;
325 val = bus_read_4(sc->mem_res, HPET_ISR);
327 bus_write_4(sc->mem_res, HPET_ISR, val);
329 for (i = 0; i < sc->num_timers; i++) {
330 if ((val & (1 << i)) == 0)
332 hpet_intr_single(&sc->t[i]);
334 return (FILTER_HANDLED);
336 return (FILTER_STRAY);
340 hpet_get_uid(device_t dev)
342 struct hpet_softc *sc;
344 sc = device_get_softc(dev);
345 return (sc->acpi_uid);
349 hpet_find(ACPI_HANDLE handle, UINT32 level, void *context,
353 uint32_t id = (uint32_t)(uintptr_t)context;
356 for (ids = hpet_ids; *ids != NULL; ids++) {
357 if (acpi_MatchHid(handle, *ids))
362 if (ACPI_FAILURE(acpi_GetInteger(handle, "_UID", &uid)) ||
364 *status = acpi_get_device(handle);
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.
373 hpet_find_irq_rid(device_t dev, u_long start, u_long end)
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))
386 hpet_open(struct cdev *cdev, int oflags, int devtype, struct thread *td)
388 struct hpet_softc *sc;
398 hpet_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
399 int nprot, vm_memattr_t *memattr)
401 struct hpet_softc *sc;
404 if (offset >= rman_get_size(sc->mem_res))
406 if (!sc->mmap_allow_write && (nprot & PROT_WRITE))
408 *paddr = rman_get_start(sc->mem_res) + offset;
409 *memattr = VM_MEMATTR_UNCACHEABLE;
414 /* Discover the HPET via the ACPI table of the same name. */
416 hpet_identify(driver_t *driver, device_t parent)
418 ACPI_TABLE_HPET *hpet;
423 /* Only one HPET device can be added. */
424 if (devclass_get_device(hpet_devclass, 0))
427 /* Search for HPET table. */
428 status = AcpiGetTable(ACPI_SIG_HPET, i, (ACPI_TABLE_HEADER **)&hpet);
429 if (ACPI_FAILURE(status))
431 /* Search for HPET device with same ID. */
433 AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
434 100, hpet_find, NULL, (void *)(uintptr_t)hpet->Sequence,
436 /* If found - let it be probed in normal way. */
438 if (bus_get_resource(child, SYS_RES_MEMORY, 0,
440 bus_set_resource(child, SYS_RES_MEMORY, 0,
441 hpet->Address.Address, HPET_MEM_WIDTH);
444 /* If not - create it from table info. */
445 child = BUS_ADD_CHILD(parent, 2, "hpet", 0);
447 printf("%s: can't add child\n", __func__);
450 bus_set_resource(child, SYS_RES_MEMORY, 0, hpet->Address.Address,
456 hpet_probe(device_t dev)
460 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
461 if (acpi_disabled("hpet") || acpi_hpet_disabled)
463 if (acpi_get_handle(dev) != NULL)
464 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids, NULL);
468 device_set_desc(dev, "High Precision Event Timer");
473 hpet_attach(device_t dev)
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;
484 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
486 sc = device_get_softc(dev);
488 sc->handle = acpi_get_handle(dev);
491 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
493 if (sc->mem_res == NULL)
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);
504 /* Be sure timer is enabled. */
507 /* Read basic statistics about the timer. */
508 val = bus_read_4(sc->mem_res, HPET_PERIOD);
510 device_printf(dev, "invalid period\n");
512 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
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);
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.
527 if (vendor == HPET_VENDID_AMD && rev < 0x10 && num_timers > 0)
529 sc->num_timers = num_timers;
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" : "",
536 (sc->caps & HPET_CAP_LEG_RT) ? " legacy route" : "");
538 for (i = 0; i < num_timers; i++) {
542 t->mode = TIMER_STOPPED;
546 t->pcpu_misrouted = 0;
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);
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" : "");
559 if (testenv("debug.acpi.hpet_test"))
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.
565 val = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
567 val2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
569 device_printf(dev, "HPET never increments, disabling\n");
571 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
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;
582 sc->tc.tc_fill_vdso_timehands = hpet_vdso_timehands;
583 #ifdef COMPAT_FREEBSD32
584 sc->tc.tc_fill_vdso_timehands32 = hpet_vdso_timehands32;
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)
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;
604 /* Check what IRQs we want use. */
605 /* By default allow any PCI IRQs. */
606 sc->allowed_irqs = 0xffff0000;
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.
614 if (vendor == HPET_VENDID_AMD || vendor == HPET_VENDID_AMD2 ||
615 vendor == HPET_VENDID_HYGON)
616 sc->allowed_irqs = 0x00000000;
618 * NVidia MCP5x chipsets have number of unexplained interrupt
619 * problems. For some reason, using HPET interrupts breaks HDA sound.
621 if (vendor == HPET_VENDID_NVIDIA && rev <= 0x01)
622 sc->allowed_irqs = 0x00000000;
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.
628 if (vendor == HPET_VENDID_SW && rev <= 0x01)
629 sc->allowed_irqs = 0x00000000;
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.
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);
641 /* Get how much per-CPU timers we should try to provide. */
643 resource_int_value(device_get_name(dev), device_get_unit(dev),
644 "per_cpu", &sc->per_cpu);
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++) {
655 if (sc->legacy_route && i < 2)
656 t->irq = (i == 0) ? 0 : 8;
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,
663 "Can't allocate interrupt for t%d: %d\n",
668 else if (dvectors & t->vectors) {
669 t->irq = ffs(dvectors & t->vectors) - 1;
670 dvectors &= ~(1 << t->irq);
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) {
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) {
685 "Can't setup interrupt for t%d.\n", i);
687 bus_describe_intr(dev, t->intr_res,
688 t->intr_handle, "t%d", i);
692 if (t->irq < 0 && (cvectors & t->vectors) != 0) {
693 cvectors &= t->vectors;
694 sc->useirq |= (1 << i);
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)
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;
705 cur_cpu = CPU_FIRST();
706 for (i = 0; i < num_timers; i++) {
708 if (t->irq >= 0 && num_percpu_t > 0) {
709 if (cur_cpu == CPU_FIRST())
711 t->pcpu_cpu = cur_cpu;
712 t->pcpu_master = 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);
718 } else if (t->irq >= 0)
719 bus_bind_intr(dev, t->intr_res, CPU_FIRST());
721 bus_write_4(sc->mem_res, HPET_ISR, 0xffffffff);
723 /* If at least one timer needs legacy IRQ - set it up. */
725 j = i = fls(cvectors) - 1;
726 while (j > 0 && (cvectors & (1 << (j - 1))) != 0)
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");
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());
742 /* Program and announce event timers. */
743 for (i = 0; i < num_timers; 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. */
753 if ((t->caps & HPET_TCAP_FSB_INT_DEL) && t->irq >= 0) {
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;
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. */
776 (sc->irq < 0 || (t->vectors & (1 << sc->irq)) == 0))
778 /* Announce the reset. */
780 t->et.et_name = "HPET";
782 sprintf(t->name, "HPET%d", maxhpetet);
783 t->et.et_name = t->name;
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) {
806 acpi_GetInteger(sc->handle, "_UID", &sc->acpi_uid);
808 make_dev_args_init(&mda);
809 mda.mda_devsw = &hpet_cdevsw;
810 mda.mda_uid = UID_ROOT;
811 mda.mda_gid = GID_WHEEL;
813 mda.mda_si_drv1 = sc;
814 error = make_dev_s(&mda, &sc->pdev, "hpet%d", device_get_unit(dev));
817 TUNABLE_INT_FETCH("hw.acpi.hpet.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");
833 device_printf(dev, "could not create /dev/hpet%d, error %d\n",
834 device_get_unit(dev), error);
841 hpet_detach(device_t dev)
843 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
845 /* XXX Without a tc_remove() function, we can't detach. */
850 hpet_suspend(device_t dev)
852 // struct hpet_softc *sc;
855 * Disable the timer during suspend. The timer will not lose
856 * its state in S1 or S2, but we are required to disable
859 // sc = device_get_softc(dev);
866 hpet_resume(device_t dev)
868 struct hpet_softc *sc;
869 struct hpet_timer *t;
872 /* Re-enable the timer after a resume to keep the clock advancing. */
873 sc = device_get_softc(dev);
875 /* Restart event timers that were running on suspend. */
876 for (i = 0; i < sc->num_timers; i++) {
879 if (t->irq >= 0 && (sc->legacy_route == 0 || i >= 2)) {
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);
893 if (t->mode == TIMER_STOPPED)
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;
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),
904 bus_read_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num));
905 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
908 t->next += sc->freq / 1024;
909 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
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);
918 /* Print some basic latency/rate information to assist in debugging. */
920 hpet_test(struct hpet_softc *sc)
924 struct bintime b0, b1, b2;
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);
934 u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
936 bintime_sub(&b2, &b1);
937 bintime_sub(&b1, &b0);
938 bintime_sub(&b2, &b1);
939 bintime2timespec(&b2, &ts);
941 device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
942 (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1);
944 device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);
949 hpet_remap_intr(device_t dev, device_t child, u_int irq)
951 struct hpet_softc *sc = device_get_softc(dev);
952 struct hpet_timer *t;
957 for (i = 0; i < sc->num_timers; i++) {
961 error = PCIB_MAP_MSI(
962 device_get_parent(device_get_parent(dev)), dev,
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);
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),
986 DEVMETHOD(bus_remap_intr, hpet_remap_intr),
992 static driver_t hpet_driver = {
995 sizeof(struct hpet_softc),
998 DRIVER_MODULE(hpet, acpi, hpet_driver, hpet_devclass, 0, 0);
999 MODULE_DEPEND(hpet, acpi, 1, 1, 1);