2 * Copyright (c) 2005 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/dev/acpica/acpi_hpet.c,v 1.12.2.1.2.1 2008/11/25 02:59:29 kensmith Exp $
31 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/systimer.h>
38 #if !defined(KLD_MODULE)
39 #include <machine/pmap.h>
45 #include "acpi_hpet.h"
47 #if !defined(KLD_MODULE)
48 #include <platform/pc64/acpica/acpi_sdt_var.h>
51 /* Hooks for the ACPICA debugging infrastructure */
52 #define _COMPONENT ACPI_TIMER
53 ACPI_MODULE_NAME("HPET")
55 static bus_space_handle_t acpi_hpet_bsh;
56 static bus_space_tag_t acpi_hpet_bst;
57 static u_long acpi_hpet_res_start;
59 struct acpi_hpet_softc {
61 struct resource *mem_res;
65 #define DEV_HPET(x) (acpi_get_magic(x) == (uintptr_t)&acpi_hpet_devclass)
67 static sysclock_t acpi_hpet_get_timecount(void);
68 static void acpi_hpet_construct(struct cputimer *, sysclock_t);
70 static int acpi_hpet_identify(driver_t *, device_t);
71 static int acpi_hpet_probe(device_t);
72 static int acpi_hpet_attach(device_t);
73 static int acpi_hpet_resume(device_t);
74 static int acpi_hpet_suspend(device_t);
76 static void acpi_hpet_test(struct acpi_hpet_softc *sc);
77 static u_int acpi_hpet_read(void);
78 static void acpi_hpet_enable(struct acpi_hpet_softc *);
79 static void acpi_hpet_disable(struct acpi_hpet_softc *);
81 static char *hpet_ids[] = { "PNP0103", NULL };
83 static struct cputimer acpi_hpet_timer = {
84 .next = SLIST_ENTRY_INITIALIZER,
86 .pri = CPUTIMER_PRI_HPET,
87 .type = CPUTIMER_HPET,
88 .count = acpi_hpet_get_timecount,
89 .fromhz = cputimer_default_fromhz,
90 .fromus = cputimer_default_fromus,
91 .construct = acpi_hpet_construct,
92 .destruct = cputimer_default_destruct,
93 .freq = 0 /* determined later */
96 static device_method_t acpi_hpet_methods[] = {
97 DEVMETHOD(device_identify, acpi_hpet_identify),
98 DEVMETHOD(device_probe, acpi_hpet_probe),
99 DEVMETHOD(device_attach, acpi_hpet_attach),
100 DEVMETHOD(device_suspend, acpi_hpet_suspend),
101 DEVMETHOD(device_resume, acpi_hpet_resume),
105 static driver_t acpi_hpet_driver = {
108 sizeof(struct acpi_hpet_softc),
109 .gpri = KOBJ_GPRI_ACPI+2
112 static devclass_t acpi_hpet_devclass;
113 DRIVER_MODULE(acpi_hpet, acpi, acpi_hpet_driver, acpi_hpet_devclass, NULL, NULL);
114 MODULE_DEPEND(acpi_hpet, acpi, 1, 1, 1);
119 return bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
123 #if !defined(KLD_MODULE)
124 extern int i8254_cputimer_disable;
126 static vm_offset_t ptr = 0;
128 static int acpi_hpet_for_calibration = 1;
129 TUNABLE_INT("hw.calibrate_timers_with_hpet", &acpi_hpet_for_calibration);
132 acpi_hpet_early_get_timecount(void)
134 sysclock_t last_counter;
135 sysclock_t next_counter;
138 last_counter = acpi_hpet_timer.base;
141 counter = readl(ptr + HPET_MAIN_COUNTER);
142 if (counter < (last_counter & 0xFFFFFFFFU))
143 next_counter = ((last_counter + 0x0100000000U) &
144 0xFFFFFFFF00000000LU) | counter;
146 next_counter = (last_counter &
147 0xFFFFFFFF00000000LU) | counter;
148 if (atomic_fcmpset_long(&acpi_hpet_timer.base, &last_counter,
157 acpi_hpet_early_construct(struct cputimer *timer, sysclock_t oldclock)
161 val = readl(ptr + HPET_CONFIG);
162 writel(ptr + HPET_CONFIG, val | HPET_CNF_ENABLE);
165 timer->base = oldclock - acpi_hpet_early_get_timecount();
169 acpi_hpet_early_destruct(struct cputimer *timer)
173 val = readl(ptr + HPET_CONFIG);
174 writel(ptr + HPET_CONFIG, val & ~HPET_CNF_ENABLE);
178 acpi_hpet_early_init(void)
181 uint64_t old_tsc, new_tsc;
184 val = readl(ptr + HPET_CONFIG);
185 writel(ptr + HPET_CONFIG, val | HPET_CNF_ENABLE);
187 /* Read basic statistics about the timer. */
188 val = readl(ptr + HPET_PERIOD);
190 kprintf("acpi_hpet: invalid period\n");
191 val = readl(ptr + HPET_CONFIG);
192 writel(ptr + HPET_CONFIG, val & ~HPET_CNF_ENABLE);
196 freq = (1000000000000000LL + val / 2) / val;
198 val = readl(ptr + HPET_CAPABILITIES);
199 kprintf("acpi_hpet: "
200 "vend: 0x%x, rev: 0x%x, num: %d, opts:%s%s\n",
201 val >> 16, val & HPET_CAP_REV_ID,
202 (val & HPET_CAP_NUM_TIM) >> 8,
203 (val & HPET_CAP_LEG_RT) ? " legacy_route" : "",
204 (val & HPET_CAP_COUNT_SIZE) ? " 64-bit" : "");
208 if (ktestenv("debug.acpi.hpet_test"))
213 * Don't attach if the timer never increments. Since the spec
214 * requires it to be at least 10 MHz, it has to change in 1 us.
216 val = readl(ptr + HPET_MAIN_COUNTER);
217 /* This delay correspond to 1us, even at 6 GHz TSC. */
222 } while (new_tsc - old_tsc < 6000);
223 val2 = readl(ptr + HPET_MAIN_COUNTER);
225 kprintf("acpi_hpet: HPET never increments, disabling\n");
226 val = readl(ptr + HPET_CONFIG);
227 writel(ptr + HPET_CONFIG, val & ~HPET_CNF_ENABLE);
231 val = readl(ptr + HPET_CONFIG);
232 writel(ptr + HPET_CONFIG, val & ~HPET_CNF_ENABLE);
233 acpi_hpet_timer.freq = freq;
234 kprintf("acpi_hpet: frequency %lu\n", acpi_hpet_timer.freq);
236 acpi_hpet_timer.count = acpi_hpet_early_get_timecount;
237 acpi_hpet_timer.construct = acpi_hpet_early_construct;
238 acpi_hpet_timer.destruct = acpi_hpet_early_destruct;
240 cputimer_register(&acpi_hpet_timer);
241 cputimer_select(&acpi_hpet_timer, 0);
246 acpi_hpet_cputimer_register(void)
248 ACPI_TABLE_HPET *hpet;
249 vm_paddr_t hpet_paddr;
251 if (acpi_hpet_for_calibration == 0)
254 if (acpi_disabled("hpet"))
257 hpet_paddr = sdt_search(ACPI_SIG_HPET);
258 if (hpet_paddr == 0) {
260 kprintf("acpi_hpet: can't locate HPET\n");
264 hpet = sdt_sdth_map(hpet_paddr);
268 if (hpet->Header.Length < 56) {
269 kprintf("acpi_hpet: HPET table too short. Length: 0x%x\n",
270 hpet->Header.Length);
274 if (hpet->Sequence != 0) {
275 kprintf("acpi_hpet: "
276 "HPET table Sequence not 0. Sequence: 0x%x\n", hpet->Id);
280 acpi_hpet_res_start = hpet->Address.Address;
281 if (acpi_hpet_res_start == 0)
284 ptr = (vm_offset_t)pmap_mapdev(acpi_hpet_res_start, HPET_MEM_WIDTH);
285 if (acpi_hpet_early_init() == 0) {
286 i8254_cputimer_disable = 1;
288 pmap_unmapdev(ptr, HPET_MEM_WIDTH);
293 sdt_sdth_unmap(&hpet->Header);
297 TIMECOUNTER_INIT(acpi_hpet_init, acpi_hpet_cputimer_register);
301 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
305 acpi_hpet_identify(driver_t *driver, device_t parent)
307 ACPI_TABLE_HPET *hpet;
308 ACPI_TABLE_HEADER *hdr;
313 * Just try once, do nothing if the 'acpi' bus is rescanned.
315 if (device_get_state(parent) == DS_ATTACHED)
318 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
320 /* Only one HPET device can be added. */
321 if (devclass_get_device(acpi_hpet_devclass, 0))
324 #if !defined(KLD_MODULE)
326 /* Use data from early boot for attachment. */
327 child = BUS_ADD_CHILD(parent, parent, 0, "acpi_hpet", 0);
329 device_printf(parent, "%s: can't add acpi_hpet0\n",
334 /* Record a magic value so we can detect this device later. */
335 acpi_set_magic(child, (uintptr_t)&acpi_hpet_devclass);
337 if (bus_set_resource(child, SYS_RES_MEMORY, 0,
338 acpi_hpet_res_start, HPET_MEM_WIDTH, -1)) {
340 "could not set iomem resources: 0x%jx, %d\n",
341 (uintmax_t)acpi_hpet_res_start, HPET_MEM_WIDTH);
349 /* Currently, ID and minimum clock tick info is unused. */
351 status = AcpiGetTable(ACPI_SIG_HPET, 1, &hdr);
352 if (ACPI_FAILURE(status))
356 * The unit number could be derived from hdr->Sequence but we only
357 * support one HPET device.
359 hpet = (ACPI_TABLE_HPET *)hdr;
360 if (hpet->Sequence != 0) {
361 kprintf("ACPI HPET table warning: Sequence is non-zero (%d)\n",
365 child = BUS_ADD_CHILD(parent, parent, 0, "acpi_hpet", 0);
367 device_printf(parent, "%s: can't add acpi_hpet0\n", __func__);
371 /* Record a magic value so we can detect this device later. */
372 acpi_set_magic(child, (uintptr_t)&acpi_hpet_devclass);
374 acpi_hpet_res_start = hpet->Address.Address;
375 if (bus_set_resource(child, SYS_RES_MEMORY, 0,
376 hpet->Address.Address, HPET_MEM_WIDTH, -1)) {
377 device_printf(child, "could not set iomem resources: "
378 "0x%jx, %d\n", (uintmax_t)hpet->Address.Address,
386 acpi_hpet_probe(device_t dev)
388 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
390 if (acpi_disabled("hpet"))
393 if (!DEV_HPET(dev) &&
394 (ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL ||
395 device_get_unit(dev) != 0))
398 device_set_desc(dev, "High Precision Event Timer");
403 acpi_hpet_attach(device_t dev)
405 struct acpi_hpet_softc *sc;
410 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
412 sc = device_get_softc(dev);
414 sc->handle = acpi_get_handle(dev);
417 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
419 if (sc->mem_res == NULL) {
421 * We only need to make sure that main counter
424 device_printf(dev, "can't map %dB register space, try %dB\n",
425 HPET_MEM_WIDTH, HPET_MEM_WIDTH_MIN);
427 sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
429 acpi_hpet_res_start + HPET_MEM_WIDTH_MIN - 1,
430 HPET_MEM_WIDTH_MIN, RF_ACTIVE);
431 if (sc->mem_res == NULL)
435 /* Validate that we can access the whole region. */
436 if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH_MIN) {
437 device_printf(dev, "memory region width %ld too small\n",
438 rman_get_size(sc->mem_res));
439 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->mem_res);
443 acpi_hpet_bsh = rman_get_bushandle(sc->mem_res);
444 acpi_hpet_bst = rman_get_bustag(sc->mem_res);
446 #if !defined(KLD_MODULE)
448 /* Use data from early boot for attachment. */
449 if (ktestenv("debug.acpi.hpet_test"))
455 /* Be sure timer is enabled. */
456 acpi_hpet_enable(sc);
458 /* Read basic statistics about the timer. */
459 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_PERIOD);
461 device_printf(dev, "invalid period\n");
462 acpi_hpet_disable(sc);
463 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->mem_res);
467 freq = (1000000000000000LL + val / 2) / val;
469 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
472 "vend: 0x%x, rev: 0x%x, num: %d, opts:%s%s\n",
473 val >> 16, val & HPET_CAP_REV_ID,
474 (val & HPET_CAP_NUM_TIM) >> 8,
475 (val & HPET_CAP_LEG_RT) ? " legacy_route" : "",
476 (val & HPET_CAP_COUNT_SIZE) ? " 64-bit" : "");
479 if (ktestenv("debug.acpi.hpet_test"))
483 * Don't attach if the timer never increments. Since the spec
484 * requires it to be at least 10 MHz, it has to change in 1 us.
486 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
489 val2 = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
492 device_printf(dev, "HPET never increments, disabling\n");
493 acpi_hpet_disable(sc);
494 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->mem_res);
498 acpi_hpet_timer.freq = freq;
499 device_printf(dev, "frequency %lu\n", acpi_hpet_timer.freq);
501 cputimer_register(&acpi_hpet_timer);
502 cputimer_select(&acpi_hpet_timer, 0);
508 * Construct the timer. Adjust the base so the system clock does not
512 acpi_hpet_construct(struct cputimer *timer, sysclock_t oldclock)
515 timer->base = oldclock - acpi_hpet_get_timecount();
519 acpi_hpet_get_timecount(void)
521 sysclock_t last_counter;
522 sysclock_t next_counter;
525 last_counter = acpi_hpet_timer.base;
528 counter = acpi_hpet_read();
529 if (counter < (last_counter & 0xFFFFFFFFU))
530 next_counter = ((last_counter + 0x0100000000U) &
531 0xFFFFFFFF00000000LU) | counter;
533 next_counter = (last_counter &
534 0xFFFFFFFF00000000LU) | counter;
535 if (atomic_fcmpset_long(&acpi_hpet_timer.base, &last_counter,
544 acpi_hpet_enable(struct acpi_hpet_softc *sc)
548 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_CONFIG);
549 bus_space_write_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_CONFIG,
550 val | HPET_CNF_ENABLE);
554 acpi_hpet_disable(struct acpi_hpet_softc *sc)
558 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_CONFIG);
559 bus_space_write_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_CONFIG,
560 val & ~HPET_CNF_ENABLE);
564 acpi_hpet_suspend(device_t dev)
567 * According to IA-PC HPET specification rev 1.0a
570 * "1. The Event Timer registers (including the main counter)
571 * are not expected to be preserved through an S3, S4, or S5
575 * "3. The main counter is permitted, but not required, to run
576 * during S1 or S2 states. ..."
578 * These mean we are not allowed to enter any of Sx states,
579 * if HPET is used as the sys_cputimer.
581 if (sys_cputimer != &acpi_hpet_timer) {
582 struct acpi_hpet_softc *sc;
584 sc = device_get_softc(dev);
585 acpi_hpet_disable(sc);
594 acpi_hpet_resume(device_t dev)
596 if (sys_cputimer != &acpi_hpet_timer) {
597 struct acpi_hpet_softc *sc;
599 sc = device_get_softc(dev);
600 acpi_hpet_enable(sc);
605 /* Print some basic latency/rate information to assist in debugging. */
607 acpi_hpet_test(struct acpi_hpet_softc *sc)
611 struct timeval b0, b1, b2;
617 u1 = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_MAIN_COUNTER);
618 for (i = 1; i < 1000; i++) {
619 u2 = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
623 u2 = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_MAIN_COUNTER);
625 timevalsub(&b2, &b1);
626 timevalsub(&b1, &b0);
627 timevalsub(&b2, &b1);
629 TIMEVAL_TO_TIMESPEC(&b2, &ts);
631 device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
632 (long)b2.tv_sec, b2.tv_usec, u1, u2, u2 - u1);
634 device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);