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(devclass_find("hpet"), 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 rman_res_t hpet_region_size;
481 static int maxhpetet = 0;
482 uint32_t val, val2, cvectors, dvectors;
483 uint16_t vendor, rev;
485 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
487 sc = device_get_softc(dev);
489 sc->handle = acpi_get_handle(dev);
492 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
494 if (sc->mem_res == NULL)
497 hpet_region_size = rman_get_size(sc->mem_res);
498 /* Validate that the region is big enough for the control registers. */
499 if (hpet_region_size < HPET_MEM_MIN_WIDTH) {
500 device_printf(dev, "memory region width %jd too small\n",
502 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
506 /* Be sure timer is enabled. */
509 /* Read basic statistics about the timer. */
510 val = bus_read_4(sc->mem_res, HPET_PERIOD);
512 device_printf(dev, "invalid period\n");
514 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
518 sc->freq = (1000000000000000LL + val / 2) / val;
519 sc->caps = bus_read_4(sc->mem_res, HPET_CAPABILITIES);
520 vendor = (sc->caps & HPET_CAP_VENDOR_ID) >> 16;
521 rev = sc->caps & HPET_CAP_REV_ID;
522 num_timers = 1 + ((sc->caps & HPET_CAP_NUM_TIM) >> 8);
524 * ATI/AMD violates IA-PC HPET (High Precision Event Timers)
525 * Specification and provides an off by one number
526 * of timers/comparators.
527 * Additionally, they use unregistered value in VENDOR_ID field.
529 if (vendor == HPET_VENDID_AMD && rev < 0x10 && num_timers > 0)
532 * Now validate that the region is big enough to address all counters.
534 if (hpet_region_size < HPET_TIMER_CAP_CNF(num_timers)) {
536 "memory region width %jd too small for %d timers\n",
537 hpet_region_size, num_timers);
539 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
543 sc->num_timers = num_timers;
546 "vendor 0x%x, rev 0x%x, %jdHz%s, %d timers,%s\n",
547 vendor, rev, sc->freq,
548 (sc->caps & HPET_CAP_COUNT_SIZE) ? " 64bit" : "",
550 (sc->caps & HPET_CAP_LEG_RT) ? " legacy route" : "");
552 for (i = 0; i < num_timers; i++) {
556 t->mode = TIMER_STOPPED;
560 t->pcpu_misrouted = 0;
562 t->caps = bus_read_4(sc->mem_res, HPET_TIMER_CAP_CNF(i));
563 t->vectors = bus_read_4(sc->mem_res, HPET_TIMER_CAP_CNF(i) + 4);
566 " t%d: irqs 0x%08x (%d)%s%s%s\n", i,
567 t->vectors, (t->caps & HPET_TCNF_INT_ROUTE) >> 9,
568 (t->caps & HPET_TCAP_FSB_INT_DEL) ? ", MSI" : "",
569 (t->caps & HPET_TCAP_SIZE) ? ", 64bit" : "",
570 (t->caps & HPET_TCAP_PER_INT) ? ", periodic" : "");
573 if (testenv("debug.acpi.hpet_test"))
576 * Don't attach if the timer never increments. Since the spec
577 * requires it to be at least 10 MHz, it has to change in 1 us.
579 val = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
581 val2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
583 device_printf(dev, "HPET never increments, disabling\n");
585 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
588 /* Announce first HPET as timecounter. */
589 if (device_get_unit(dev) == 0) {
590 sc->tc.tc_get_timecount = hpet_get_timecount,
591 sc->tc.tc_counter_mask = ~0u,
592 sc->tc.tc_name = "HPET",
593 sc->tc.tc_quality = 950,
594 sc->tc.tc_frequency = sc->freq;
596 sc->tc.tc_fill_vdso_timehands = hpet_vdso_timehands;
597 #ifdef COMPAT_FREEBSD32
598 sc->tc.tc_fill_vdso_timehands32 = hpet_vdso_timehands32;
602 /* If not disabled - setup and announce event timers. */
603 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
604 "clock", &i) == 0 && i == 0)
607 /* Check whether we can and want legacy routing. */
608 sc->legacy_route = 0;
609 resource_int_value(device_get_name(dev), device_get_unit(dev),
610 "legacy_route", &sc->legacy_route);
611 if ((sc->caps & HPET_CAP_LEG_RT) == 0)
612 sc->legacy_route = 0;
613 if (sc->legacy_route) {
614 sc->t[0].vectors = 0;
615 sc->t[1].vectors = 0;
618 /* Check what IRQs we want use. */
619 /* By default allow any PCI IRQs. */
620 sc->allowed_irqs = 0xffff0000;
622 * HPETs in AMD chipsets before SB800 have problems with IRQs >= 16
623 * Lower are also not always working for different reasons.
624 * SB800 fixed it, but seems do not implements level triggering
625 * properly, that makes it very unreliable - it freezes after any
626 * interrupt loss. Avoid legacy IRQs for AMD.
628 if (vendor == HPET_VENDID_AMD || vendor == HPET_VENDID_AMD2 ||
629 vendor == HPET_VENDID_HYGON)
630 sc->allowed_irqs = 0x00000000;
632 * NVidia MCP5x chipsets have number of unexplained interrupt
633 * problems. For some reason, using HPET interrupts breaks HDA sound.
635 if (vendor == HPET_VENDID_NVIDIA && rev <= 0x01)
636 sc->allowed_irqs = 0x00000000;
638 * ServerWorks HT1000 reported to have problems with IRQs >= 16.
639 * Lower IRQs are working, but allowed mask is not set correctly.
640 * Legacy_route mode works fine.
642 if (vendor == HPET_VENDID_SW && rev <= 0x01)
643 sc->allowed_irqs = 0x00000000;
645 * Neither QEMU nor VirtualBox report supported IRQs correctly.
646 * The only way to use HPET there is to specify IRQs manually
647 * and/or use legacy_route. Legacy_route mode works on both.
650 sc->allowed_irqs = 0x00000000;
651 /* Let user override. */
652 resource_int_value(device_get_name(dev), device_get_unit(dev),
653 "allowed_irqs", &sc->allowed_irqs);
655 /* Get how much per-CPU timers we should try to provide. */
657 resource_int_value(device_get_name(dev), device_get_unit(dev),
658 "per_cpu", &sc->per_cpu);
662 /* Find IRQ vectors for all timers. */
663 cvectors = sc->allowed_irqs & 0xffff0000;
664 dvectors = sc->allowed_irqs & 0x0000ffff;
665 if (sc->legacy_route)
666 dvectors &= 0x0000fefe;
667 for (i = 0; i < num_timers; i++) {
669 if (sc->legacy_route && i < 2)
670 t->irq = (i == 0) ? 0 : 8;
672 else if (t->caps & HPET_TCAP_FSB_INT_DEL) {
673 if ((j = PCIB_ALLOC_MSIX(
674 device_get_parent(device_get_parent(dev)), dev,
677 "Can't allocate interrupt for t%d: %d\n",
682 else if (dvectors & t->vectors) {
683 t->irq = ffs(dvectors & t->vectors) - 1;
684 dvectors &= ~(1 << t->irq);
687 t->intr_rid = hpet_find_irq_rid(dev, t->irq, t->irq);
688 t->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
689 &t->intr_rid, t->irq, t->irq, 1, RF_ACTIVE);
690 if (t->intr_res == NULL) {
693 "Can't map interrupt for t%d.\n", i);
694 } else if (bus_setup_intr(dev, t->intr_res,
695 INTR_TYPE_CLK, hpet_intr_single, NULL, t,
696 &t->intr_handle) != 0) {
699 "Can't setup interrupt for t%d.\n", i);
701 bus_describe_intr(dev, t->intr_res,
702 t->intr_handle, "t%d", i);
706 if (t->irq < 0 && (cvectors & t->vectors) != 0) {
707 cvectors &= t->vectors;
708 sc->useirq |= (1 << i);
711 if (sc->legacy_route && sc->t[0].irq < 0 && sc->t[1].irq < 0)
712 sc->legacy_route = 0;
713 if (sc->legacy_route)
715 /* Group timers for per-CPU operation. */
716 num_percpu_et = min(num_msi / mp_ncpus, sc->per_cpu);
717 num_percpu_t = num_percpu_et * mp_ncpus;
719 cur_cpu = CPU_FIRST();
720 for (i = 0; i < num_timers; i++) {
722 if (t->irq >= 0 && num_percpu_t > 0) {
723 if (cur_cpu == CPU_FIRST())
725 t->pcpu_cpu = cur_cpu;
726 t->pcpu_master = pcpu_master;
728 pcpu_slaves[cur_cpu] = i;
729 bus_bind_intr(dev, t->intr_res, cur_cpu);
730 cur_cpu = CPU_NEXT(cur_cpu);
732 } else if (t->irq >= 0)
733 bus_bind_intr(dev, t->intr_res, CPU_FIRST());
735 bus_write_4(sc->mem_res, HPET_ISR, 0xffffffff);
737 /* If at least one timer needs legacy IRQ - set it up. */
739 j = i = fls(cvectors) - 1;
740 while (j > 0 && (cvectors & (1 << (j - 1))) != 0)
742 sc->intr_rid = hpet_find_irq_rid(dev, j, i);
743 sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
744 &sc->intr_rid, j, i, 1, RF_SHAREABLE | RF_ACTIVE);
745 if (sc->intr_res == NULL)
746 device_printf(dev, "Can't map interrupt.\n");
747 else if (bus_setup_intr(dev, sc->intr_res, INTR_TYPE_CLK,
748 hpet_intr, NULL, sc, &sc->intr_handle) != 0) {
749 device_printf(dev, "Can't setup interrupt.\n");
751 sc->irq = rman_get_start(sc->intr_res);
752 /* Bind IRQ to BSP to avoid live migration. */
753 bus_bind_intr(dev, sc->intr_res, CPU_FIRST());
756 /* Program and announce event timers. */
757 for (i = 0; i < num_timers; i++) {
759 t->caps &= ~(HPET_TCNF_FSB_EN | HPET_TCNF_INT_ROUTE);
760 t->caps &= ~(HPET_TCNF_VAL_SET | HPET_TCNF_INT_ENB);
761 t->caps &= ~(HPET_TCNF_INT_TYPE);
762 t->caps |= HPET_TCNF_32MODE;
763 if (t->irq >= 0 && sc->legacy_route && i < 2) {
764 /* Legacy route doesn't need more configuration. */
767 if ((t->caps & HPET_TCAP_FSB_INT_DEL) && t->irq >= 0) {
772 device_get_parent(device_get_parent(dev)), dev,
773 t->irq, &addr, &data) == 0) {
774 bus_write_4(sc->mem_res,
775 HPET_TIMER_FSB_ADDR(i), addr);
776 bus_write_4(sc->mem_res,
777 HPET_TIMER_FSB_VAL(i), data);
778 t->caps |= HPET_TCNF_FSB_EN;
784 t->caps |= (t->irq << 9);
785 else if (sc->irq >= 0 && (t->vectors & (1 << sc->irq)))
786 t->caps |= (sc->irq << 9) | HPET_TCNF_INT_TYPE;
787 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(i), t->caps);
788 /* Skip event timers without set up IRQ. */
790 (sc->irq < 0 || (t->vectors & (1 << sc->irq)) == 0))
792 /* Announce the reset. */
794 t->et.et_name = "HPET";
796 sprintf(t->name, "HPET%d", maxhpetet);
797 t->et.et_name = t->name;
799 t->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT;
800 t->et.et_quality = 450;
801 if (t->pcpu_master >= 0) {
802 t->et.et_flags |= ET_FLAGS_PERCPU;
803 t->et.et_quality += 100;
804 } else if (mp_ncpus >= 8)
805 t->et.et_quality -= 100;
806 if ((t->caps & HPET_TCAP_PER_INT) == 0)
807 t->et.et_quality -= 10;
808 t->et.et_frequency = sc->freq;
809 t->et.et_min_period =
810 ((uint64_t)(HPET_MIN_CYCLES * 2) << 32) / sc->freq;
811 t->et.et_max_period = (0xfffffffeLLU << 32) / sc->freq;
812 t->et.et_start = hpet_start;
813 t->et.et_stop = hpet_stop;
814 t->et.et_priv = &sc->t[i];
815 if (t->pcpu_master < 0 || t->pcpu_master == i) {
820 acpi_GetInteger(sc->handle, "_UID", &sc->acpi_uid);
822 make_dev_args_init(&mda);
823 mda.mda_devsw = &hpet_cdevsw;
824 mda.mda_uid = UID_ROOT;
825 mda.mda_gid = GID_WHEEL;
827 mda.mda_si_drv1 = sc;
828 error = make_dev_s(&mda, &sc->pdev, "hpet%d", device_get_unit(dev));
831 TUNABLE_INT_FETCH("hw.acpi.hpet.mmap_allow",
833 sc->mmap_allow_write = 0;
834 TUNABLE_INT_FETCH("hw.acpi.hpet.mmap_allow_write",
835 &sc->mmap_allow_write);
836 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
837 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
838 OID_AUTO, "mmap_allow",
839 CTLFLAG_RW, &sc->mmap_allow, 0,
840 "Allow userland to memory map HPET");
841 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
842 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
843 OID_AUTO, "mmap_allow_write",
844 CTLFLAG_RW, &sc->mmap_allow_write, 0,
845 "Allow userland write to the HPET register space");
847 device_printf(dev, "could not create /dev/hpet%d, error %d\n",
848 device_get_unit(dev), error);
855 hpet_detach(device_t dev)
857 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
859 /* XXX Without a tc_remove() function, we can't detach. */
864 hpet_suspend(device_t dev)
866 // struct hpet_softc *sc;
869 * Disable the timer during suspend. The timer will not lose
870 * its state in S1 or S2, but we are required to disable
873 // sc = device_get_softc(dev);
880 hpet_resume(device_t dev)
882 struct hpet_softc *sc;
883 struct hpet_timer *t;
886 /* Re-enable the timer after a resume to keep the clock advancing. */
887 sc = device_get_softc(dev);
889 /* Restart event timers that were running on suspend. */
890 for (i = 0; i < sc->num_timers; i++) {
893 if (t->irq >= 0 && (sc->legacy_route == 0 || i >= 2)) {
898 device_get_parent(device_get_parent(dev)), dev,
899 t->irq, &addr, &data) == 0) {
900 bus_write_4(sc->mem_res,
901 HPET_TIMER_FSB_ADDR(i), addr);
902 bus_write_4(sc->mem_res,
903 HPET_TIMER_FSB_VAL(i), data);
907 if (t->mode == TIMER_STOPPED)
909 t->next = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
910 if (t->mode == TIMER_PERIODIC &&
911 (t->caps & HPET_TCAP_PER_INT) != 0) {
912 t->caps |= HPET_TCNF_TYPE;
914 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num),
915 t->caps | HPET_TCNF_VAL_SET);
916 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
918 bus_read_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num));
919 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
922 t->next += sc->freq / 1024;
923 bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num),
926 bus_write_4(sc->mem_res, HPET_ISR, 1 << t->num);
927 bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), t->caps);
932 /* Print some basic latency/rate information to assist in debugging. */
934 hpet_test(struct hpet_softc *sc)
938 struct bintime b0, b1, b2;
944 u1 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
945 for (i = 1; i < 1000; i++)
946 u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
948 u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
950 bintime_sub(&b2, &b1);
951 bintime_sub(&b1, &b0);
952 bintime_sub(&b2, &b1);
953 bintime2timespec(&b2, &ts);
955 device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
956 (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1);
958 device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);
963 hpet_remap_intr(device_t dev, device_t child, u_int irq)
965 struct hpet_softc *sc = device_get_softc(dev);
966 struct hpet_timer *t;
971 for (i = 0; i < sc->num_timers; i++) {
975 error = PCIB_MAP_MSI(
976 device_get_parent(device_get_parent(dev)), dev,
980 hpet_disable(sc); /* Stop timer to avoid interrupt loss. */
981 bus_write_4(sc->mem_res, HPET_TIMER_FSB_ADDR(i), addr);
982 bus_write_4(sc->mem_res, HPET_TIMER_FSB_VAL(i), data);
990 static device_method_t hpet_methods[] = {
991 /* Device interface */
992 DEVMETHOD(device_identify, hpet_identify),
993 DEVMETHOD(device_probe, hpet_probe),
994 DEVMETHOD(device_attach, hpet_attach),
995 DEVMETHOD(device_detach, hpet_detach),
996 DEVMETHOD(device_suspend, hpet_suspend),
997 DEVMETHOD(device_resume, hpet_resume),
1000 DEVMETHOD(bus_remap_intr, hpet_remap_intr),
1006 static driver_t hpet_driver = {
1009 sizeof(struct hpet_softc),
1012 DRIVER_MODULE(hpet, acpi, hpet_driver, hpet_devclass, 0, 0);
1013 MODULE_DEPEND(hpet, acpi, 1, 1, 1);