acpi_hpet.c revision 210298
1151912Sphk/*- 2151912Sphk * Copyright (c) 2005 Poul-Henning Kamp 3209440Smav * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org> 4151912Sphk * All rights reserved. 5151912Sphk * 6151912Sphk * Redistribution and use in source and binary forms, with or without 7151912Sphk * modification, are permitted provided that the following conditions 8151912Sphk * are met: 9151912Sphk * 1. Redistributions of source code must retain the above copyright 10151912Sphk * notice, this list of conditions and the following disclaimer. 11151912Sphk * 2. Redistributions in binary form must reproduce the above copyright 12151912Sphk * notice, this list of conditions and the following disclaimer in the 13151912Sphk * documentation and/or other materials provided with the distribution. 14151912Sphk * 15151912Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16151912Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17151912Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18151912Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19151912Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20151912Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21151912Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22151912Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23151912Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24151912Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25151912Sphk * SUCH DAMAGE. 26151912Sphk */ 27151912Sphk 28151912Sphk#include <sys/cdefs.h> 29151912Sphk__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi_hpet.c 210298 2010-07-20 15:48:29Z mav $"); 30151912Sphk 31151912Sphk#include "opt_acpi.h" 32209402Smav#if defined(__amd64__) || defined(__ia64__) 33209371Smav#define DEV_APIC 34209371Smav#else 35209371Smav#include "opt_apic.h" 36209371Smav#endif 37151912Sphk#include <sys/param.h> 38159217Snjl#include <sys/bus.h> 39151912Sphk#include <sys/kernel.h> 40151912Sphk#include <sys/module.h> 41209371Smav#include <sys/proc.h> 42151912Sphk#include <sys/rman.h> 43151912Sphk#include <sys/time.h> 44209371Smav#include <sys/smp.h> 45209371Smav#include <sys/sysctl.h> 46209371Smav#include <sys/timeet.h> 47151912Sphk#include <sys/timetc.h> 48159217Snjl 49193530Sjkim#include <contrib/dev/acpica/include/acpi.h> 50193530Sjkim#include <contrib/dev/acpica/include/accommon.h> 51193530Sjkim 52151912Sphk#include <dev/acpica/acpivar.h> 53175385Sjhb#include <dev/acpica/acpi_hpet.h> 54151912Sphk 55209371Smav#ifdef DEV_APIC 56209371Smav#include "pcib_if.h" 57209371Smav#endif 58209371Smav 59203062Savg#define HPET_VENDID_AMD 0x4353 60203062Savg#define HPET_VENDID_INTEL 0x8086 61203062Savg 62151912SphkACPI_SERIAL_DECL(hpet, "ACPI HPET support"); 63151912Sphk 64209371Smavstatic devclass_t hpet_devclass; 65169574Stakawata 66151931Sscottl/* ACPI CA debugging */ 67151935Sscottl#define _COMPONENT ACPI_TIMER 68151931SscottlACPI_MODULE_NAME("HPET") 69151931Sscottl 70209371Smavstruct hpet_softc { 71151912Sphk device_t dev; 72209371Smav int mem_rid; 73209371Smav int intr_rid; 74209371Smav int irq; 75209371Smav int useirq; 76209440Smav int legacy_route; 77159217Snjl struct resource *mem_res; 78209371Smav struct resource *intr_res; 79209371Smav void *intr_handle; 80151912Sphk ACPI_HANDLE handle; 81209371Smav uint64_t freq; 82209440Smav uint32_t caps; 83209371Smav struct timecounter tc; 84209371Smav struct hpet_timer { 85209371Smav struct eventtimer et; 86209371Smav struct hpet_softc *sc; 87209371Smav int num; 88209371Smav int mode; 89209371Smav int intr_rid; 90209371Smav int irq; 91209371Smav int pcpu_master; 92209371Smav int pcpu_slaves[MAXCPU]; 93209371Smav struct resource *intr_res; 94209371Smav void *intr_handle; 95209371Smav uint32_t caps; 96209371Smav uint32_t vectors; 97209371Smav uint32_t div; 98209371Smav uint32_t last; 99209371Smav char name[8]; 100209371Smav } t[32]; 101209371Smav int num_timers; 102151912Sphk}; 103151912Sphk 104159217Snjlstatic u_int hpet_get_timecount(struct timecounter *tc); 105209371Smavstatic void hpet_test(struct hpet_softc *sc); 106151912Sphk 107159217Snjlstatic char *hpet_ids[] = { "PNP0103", NULL }; 108159217Snjl 109159217Snjlstatic u_int 110151912Sphkhpet_get_timecount(struct timecounter *tc) 111151912Sphk{ 112209371Smav struct hpet_softc *sc; 113151912Sphk 114151912Sphk sc = tc->tc_priv; 115175385Sjhb return (bus_read_4(sc->mem_res, HPET_MAIN_COUNTER)); 116151912Sphk} 117151912Sphk 118175361Sjhbstatic void 119209371Smavhpet_enable(struct hpet_softc *sc) 120175361Sjhb{ 121175361Sjhb uint32_t val; 122175385Sjhb 123175385Sjhb val = bus_read_4(sc->mem_res, HPET_CONFIG); 124209440Smav if (sc->legacy_route) 125209440Smav val |= HPET_CNF_LEG_RT; 126209440Smav else 127209440Smav val &= ~HPET_CNF_LEG_RT; 128185103Sjkim val |= HPET_CNF_ENABLE; 129185103Sjkim bus_write_4(sc->mem_res, HPET_CONFIG, val); 130175361Sjhb} 131175361Sjhb 132175361Sjhbstatic void 133209371Smavhpet_disable(struct hpet_softc *sc) 134175361Sjhb{ 135175361Sjhb uint32_t val; 136175385Sjhb 137175385Sjhb val = bus_read_4(sc->mem_res, HPET_CONFIG); 138185103Sjkim val &= ~HPET_CNF_ENABLE; 139185103Sjkim bus_write_4(sc->mem_res, HPET_CONFIG, val); 140175361Sjhb} 141175361Sjhb 142209371Smavstatic int 143209371Smavhpet_start(struct eventtimer *et, 144209371Smav struct bintime *first, struct bintime *period) 145209371Smav{ 146209371Smav struct hpet_timer *mt = (struct hpet_timer *)et->et_priv; 147209371Smav struct hpet_timer *t; 148209371Smav struct hpet_softc *sc = mt->sc; 149209371Smav uint32_t fdiv; 150209371Smav 151209371Smav t = (mt->pcpu_master < 0) ? mt : &sc->t[mt->pcpu_slaves[curcpu]]; 152209371Smav if (period != NULL) { 153209371Smav t->mode = 1; 154209371Smav t->div = (sc->freq * (period->frac >> 32)) >> 32; 155209371Smav if (period->sec != 0) 156209371Smav t->div += sc->freq * period->sec; 157209371Smav } else { 158209371Smav t->mode = 2; 159209371Smav t->div = 0; 160209371Smav } 161210290Smav if (first != NULL) { 162210290Smav fdiv = (sc->freq * (first->frac >> 32)) >> 32; 163210290Smav if (first->sec != 0) 164210290Smav fdiv += sc->freq * first->sec; 165210290Smav } else 166210290Smav fdiv = t->div; 167209371Smav t->last = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER); 168209371Smav if (t->mode == 1 && (t->caps & HPET_TCAP_PER_INT)) { 169209371Smav t->caps |= HPET_TCNF_TYPE; 170209371Smav bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), 171209371Smav t->caps | HPET_TCNF_VAL_SET); 172209371Smav bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num), 173209371Smav t->last + fdiv); 174209371Smav bus_read_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num)); 175209371Smav bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num), 176209371Smav t->div); 177209371Smav } else { 178209371Smav bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num), 179209371Smav t->last + fdiv); 180209371Smav } 181209371Smav t->caps |= HPET_TCNF_INT_ENB; 182209371Smav bus_write_4(sc->mem_res, HPET_ISR, 1 << t->num); 183209371Smav bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), t->caps); 184209371Smav return (0); 185209371Smav} 186209371Smav 187209371Smavstatic int 188209371Smavhpet_stop(struct eventtimer *et) 189209371Smav{ 190209371Smav struct hpet_timer *mt = (struct hpet_timer *)et->et_priv; 191209371Smav struct hpet_timer *t; 192209371Smav struct hpet_softc *sc = mt->sc; 193209371Smav 194209371Smav t = (mt->pcpu_master < 0) ? mt : &sc->t[mt->pcpu_slaves[curcpu]]; 195209371Smav t->mode = 0; 196209371Smav t->caps &= ~(HPET_TCNF_INT_ENB | HPET_TCNF_TYPE); 197209371Smav bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), t->caps); 198209371Smav return (0); 199209371Smav} 200209371Smav 201209371Smavstatic int 202209371Smavhpet_intr_single(void *arg) 203209371Smav{ 204209371Smav struct hpet_timer *t = (struct hpet_timer *)arg; 205209371Smav struct hpet_timer *mt; 206209371Smav struct hpet_softc *sc = t->sc; 207209371Smav uint32_t now; 208209371Smav 209209371Smav if (t->mode == 1 && 210209371Smav (t->caps & HPET_TCAP_PER_INT) == 0) { 211209371Smav t->last += t->div; 212209371Smav now = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER); 213209371Smav if ((int32_t)(now - (t->last + t->div / 2)) > 0) 214209371Smav t->last = now - t->div / 2; 215209371Smav bus_write_4(sc->mem_res, 216209371Smav HPET_TIMER_COMPARATOR(t->num), t->last + t->div); 217209371Smav } else if (t->mode == 2) 218209371Smav t->mode = 0; 219209371Smav mt = (t->pcpu_master < 0) ? t : &sc->t[t->pcpu_master]; 220209990Smav if (mt->et.et_active) 221209990Smav mt->et.et_event_cb(&mt->et, mt->et.et_arg); 222209371Smav return (FILTER_HANDLED); 223209371Smav} 224209371Smav 225209371Smavstatic int 226209371Smavhpet_intr(void *arg) 227209371Smav{ 228209371Smav struct hpet_softc *sc = (struct hpet_softc *)arg; 229209371Smav int i; 230209371Smav uint32_t val; 231209371Smav 232209371Smav val = bus_read_4(sc->mem_res, HPET_ISR); 233209371Smav if (val) { 234209371Smav bus_write_4(sc->mem_res, HPET_ISR, val); 235209371Smav val &= sc->useirq; 236209371Smav for (i = 0; i < sc->num_timers; i++) { 237209371Smav if ((val & (1 << i)) == 0) 238209371Smav continue; 239209371Smav hpet_intr_single(&sc->t[i]); 240209371Smav } 241209371Smav return (FILTER_HANDLED); 242209371Smav } 243209371Smav return (FILTER_STRAY); 244209371Smav} 245209371Smav 246208436Smavstatic ACPI_STATUS 247209371Smavhpet_find(ACPI_HANDLE handle, UINT32 level, void *context, 248208436Smav void **status) 249208436Smav{ 250208436Smav char **ids; 251208436Smav uint32_t id = (uint32_t)(uintptr_t)context; 252208438Smav uint32_t uid = 0; 253208436Smav 254208436Smav for (ids = hpet_ids; *ids != NULL; ids++) { 255208436Smav if (acpi_MatchHid(handle, *ids)) 256208436Smav break; 257208436Smav } 258208436Smav if (*ids == NULL) 259208436Smav return (AE_OK); 260209371Smav if (ACPI_FAILURE(acpi_GetInteger(handle, "_UID", &uid)) || 261209371Smav id == uid) 262208436Smav *((int *)status) = 1; 263208436Smav return (AE_OK); 264208436Smav} 265208436Smav 266169592Snjl/* Discover the HPET via the ACPI table of the same name. */ 267172489Snjlstatic void 268209371Smavhpet_identify(driver_t *driver, device_t parent) 269169574Stakawata{ 270169574Stakawata ACPI_TABLE_HPET *hpet; 271169574Stakawata ACPI_STATUS status; 272169574Stakawata device_t child; 273208436Smav int i, found; 274169574Stakawata 275172489Snjl /* Only one HPET device can be added. */ 276209371Smav if (devclass_get_device(hpet_devclass, 0)) 277172489Snjl return; 278208436Smav for (i = 1; ; i++) { 279208436Smav /* Search for HPET table. */ 280208436Smav status = AcpiGetTable(ACPI_SIG_HPET, i, (ACPI_TABLE_HEADER **)&hpet); 281208436Smav if (ACPI_FAILURE(status)) 282208436Smav return; 283208436Smav /* Search for HPET device with same ID. */ 284208436Smav found = 0; 285208436Smav AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 286209371Smav 100, hpet_find, NULL, (void *)(uintptr_t)hpet->Sequence, (void *)&found); 287208436Smav /* If found - let it be probed in normal way. */ 288208436Smav if (found) 289208436Smav continue; 290208436Smav /* If not - create it from table info. */ 291209371Smav child = BUS_ADD_CHILD(parent, ACPI_DEV_BASE_ORDER, "hpet", 0); 292208436Smav if (child == NULL) { 293208436Smav printf("%s: can't add child\n", __func__); 294208436Smav continue; 295208436Smav } 296208436Smav bus_set_resource(child, SYS_RES_MEMORY, 0, hpet->Address.Address, 297208436Smav HPET_MEM_WIDTH); 298169574Stakawata } 299169574Stakawata} 300169574Stakawata 301151912Sphkstatic int 302209371Smavhpet_probe(device_t dev) 303151912Sphk{ 304159217Snjl ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__); 305159217Snjl 306169592Snjl if (acpi_disabled("hpet")) 307151912Sphk return (ENXIO); 308199016Savg if (acpi_get_handle(dev) != NULL && 309208436Smav ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL) 310169592Snjl return (ENXIO); 311151912Sphk 312159217Snjl device_set_desc(dev, "High Precision Event Timer"); 313151912Sphk return (0); 314151912Sphk} 315151912Sphk 316151912Sphkstatic int 317209371Smavhpet_attach(device_t dev) 318151912Sphk{ 319209371Smav struct hpet_softc *sc; 320209371Smav struct hpet_timer *t; 321209371Smav int i, j, num_msi, num_timers, num_percpu_et, num_percpu_t, cur_cpu; 322209371Smav int pcpu_master; 323209371Smav static int maxhpetet = 0; 324209371Smav uint32_t val, val2, cvectors; 325209371Smav uint16_t vendor, rev; 326151912Sphk 327151912Sphk ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__); 328151912Sphk 329151912Sphk sc = device_get_softc(dev); 330151912Sphk sc->dev = dev; 331151912Sphk sc->handle = acpi_get_handle(dev); 332151912Sphk 333209371Smav sc->mem_rid = 0; 334209371Smav sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid, 335159217Snjl RF_ACTIVE); 336159217Snjl if (sc->mem_res == NULL) 337159217Snjl return (ENOMEM); 338151912Sphk 339159217Snjl /* Validate that we can access the whole region. */ 340159217Snjl if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH) { 341159217Snjl device_printf(dev, "memory region width %ld too small\n", 342159217Snjl rman_get_size(sc->mem_res)); 343159217Snjl bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res); 344159217Snjl return (ENXIO); 345159217Snjl } 346151912Sphk 347171547Snjl /* Be sure timer is enabled. */ 348175361Sjhb hpet_enable(sc); 349171547Snjl 350159217Snjl /* Read basic statistics about the timer. */ 351175385Sjhb val = bus_read_4(sc->mem_res, HPET_PERIOD); 352175361Sjhb if (val == 0) { 353175361Sjhb device_printf(dev, "invalid period\n"); 354175361Sjhb hpet_disable(sc); 355175361Sjhb bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res); 356175361Sjhb return (ENXIO); 357175361Sjhb } 358175361Sjhb 359209371Smav sc->freq = (1000000000000000LL + val / 2) / val; 360209440Smav sc->caps = bus_read_4(sc->mem_res, HPET_CAPABILITIES); 361209440Smav vendor = (sc->caps & HPET_CAP_VENDOR_ID) >> 16; 362209440Smav rev = sc->caps & HPET_CAP_REV_ID; 363209440Smav num_timers = 1 + ((sc->caps & HPET_CAP_NUM_TIM) >> 8); 364209371Smav /* 365209371Smav * ATI/AMD violates IA-PC HPET (High Precision Event Timers) 366209371Smav * Specification and provides an off by one number 367209371Smav * of timers/comparators. 368209371Smav * Additionally, they use unregistered value in VENDOR_ID field. 369209371Smav */ 370209371Smav if (vendor == HPET_VENDID_AMD && rev < 0x10 && num_timers > 0) 371209371Smav num_timers--; 372209371Smav sc->num_timers = num_timers; 373159217Snjl if (bootverbose) { 374159217Snjl device_printf(dev, 375209371Smav "vendor 0x%x, rev 0x%x, %jdHz%s, %d timers,%s\n", 376209440Smav vendor, rev, sc->freq, 377209440Smav (sc->caps & HPET_CAP_COUNT_SIZE) ? " 64bit" : "", 378209440Smav num_timers, 379209440Smav (sc->caps & HPET_CAP_LEG_RT) ? " legacy route" : ""); 380159217Snjl } 381209371Smav for (i = 0; i < num_timers; i++) { 382209371Smav t = &sc->t[i]; 383209371Smav t->sc = sc; 384209371Smav t->num = i; 385209371Smav t->mode = 0; 386209371Smav t->intr_rid = -1; 387209371Smav t->irq = -1; 388209371Smav t->pcpu_master = -1; 389209371Smav t->caps = bus_read_4(sc->mem_res, HPET_TIMER_CAP_CNF(i)); 390209371Smav t->vectors = bus_read_4(sc->mem_res, HPET_TIMER_CAP_CNF(i) + 4); 391209371Smav if (bootverbose) { 392209371Smav device_printf(dev, 393209371Smav " t%d: irqs 0x%08x (%d)%s%s%s\n", i, 394209371Smav t->vectors, (t->caps & HPET_TCNF_INT_ROUTE) >> 9, 395209371Smav (t->caps & HPET_TCAP_FSB_INT_DEL) ? ", MSI" : "", 396209371Smav (t->caps & HPET_TCAP_SIZE) ? ", 64bit" : "", 397209371Smav (t->caps & HPET_TCAP_PER_INT) ? ", periodic" : ""); 398209371Smav } 399209371Smav } 400159217Snjl if (testenv("debug.acpi.hpet_test")) 401209371Smav hpet_test(sc); 402171547Snjl /* 403171547Snjl * Don't attach if the timer never increments. Since the spec 404171547Snjl * requires it to be at least 10 MHz, it has to change in 1 us. 405171547Snjl */ 406175385Sjhb val = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER); 407171547Snjl DELAY(1); 408175385Sjhb val2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER); 409171547Snjl if (val == val2) { 410171547Snjl device_printf(dev, "HPET never increments, disabling\n"); 411175361Sjhb hpet_disable(sc); 412171547Snjl bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res); 413171547Snjl return (ENXIO); 414171547Snjl } 415208436Smav /* Announce first HPET as timecounter. */ 416208436Smav if (device_get_unit(dev) == 0) { 417209371Smav sc->tc.tc_get_timecount = hpet_get_timecount, 418209371Smav sc->tc.tc_counter_mask = ~0u, 419209371Smav sc->tc.tc_name = "HPET", 420209371Smav sc->tc.tc_quality = 900, 421209371Smav sc->tc.tc_frequency = sc->freq; 422209371Smav sc->tc.tc_priv = sc; 423209371Smav tc_init(&sc->tc); 424208436Smav } 425209371Smav /* If not disabled - setup and announce event timers. */ 426209371Smav if (resource_int_value(device_get_name(dev), device_get_unit(dev), 427209371Smav "clock", &i) == 0 && i == 0) 428209371Smav return (0); 429209440Smav 430209440Smav /* Check whether we can and want legacy routing. */ 431209440Smav sc->legacy_route = 0; 432209440Smav resource_int_value(device_get_name(dev), device_get_unit(dev), 433209440Smav "legacy_route", &sc->legacy_route); 434209440Smav if ((sc->caps & HPET_CAP_LEG_RT) == 0) 435209440Smav sc->legacy_route = 0; 436209440Smav if (sc->legacy_route) { 437209440Smav sc->t[0].vectors = 0; 438209440Smav sc->t[1].vectors = 0; 439209440Smav } 440209440Smav 441209440Smav num_msi = 0; 442209440Smav sc->useirq = 0; 443209371Smav /* Find common legacy IRQ vectors for all timers. */ 444209371Smav cvectors = 0xffff0000; 445209371Smav /* 446209371Smav * HPETs in AMD chipsets before SB800 have problems with IRQs >= 16 447209371Smav * Lower are also not always working for different reasons. 448209371Smav * SB800 fixed it, but seems do not implements level triggering 449209371Smav * properly, that makes it very unreliable - it freezes after any 450209371Smav * interrupt loss. Avoid legacy IRQs for AMD. 451209371Smav */ 452209371Smav if (vendor == HPET_VENDID_AMD) 453209371Smav cvectors = 0x00000000; 454209371Smav for (i = 0; i < num_timers; i++) { 455209371Smav t = &sc->t[i]; 456209440Smav if (sc->legacy_route && i < 2) 457209440Smav t->irq = (i == 0) ? 0 : 8; 458209371Smav#ifdef DEV_APIC 459209440Smav else if (t->caps & HPET_TCAP_FSB_INT_DEL) { 460209371Smav if ((j = PCIB_ALLOC_MSIX( 461209371Smav device_get_parent(device_get_parent(dev)), dev, 462209371Smav &t->irq))) { 463209371Smav device_printf(dev, 464209440Smav "Can't allocate interrupt for t%d.\n", j); 465209440Smav } 466209440Smav } 467209440Smav#endif 468209440Smav if (t->irq >= 0) { 469209440Smav if (!(t->intr_res = 470209371Smav bus_alloc_resource(dev, SYS_RES_IRQ, &t->intr_rid, 471209440Smav t->irq, t->irq, 1, RF_ACTIVE))) { 472209440Smav t->irq = -1; 473209440Smav device_printf(dev, 474209440Smav "Can't map interrupt for t%d.\n", i); 475209371Smav } else if ((bus_setup_intr(dev, t->intr_res, 476209371Smav INTR_MPSAFE | INTR_TYPE_CLK, 477209371Smav (driver_filter_t *)hpet_intr_single, NULL, 478209371Smav t, &t->intr_handle))) { 479209440Smav t->irq = -1; 480209440Smav device_printf(dev, 481209440Smav "Can't setup interrupt for t%d.\n", i); 482209371Smav } else { 483209371Smav bus_describe_intr(dev, t->intr_res, 484209371Smav t->intr_handle, "t%d", i); 485209371Smav num_msi++; 486209371Smav } 487209440Smav } 488209440Smav if (t->irq < 0 && (cvectors & t->vectors) != 0) { 489209371Smav cvectors &= t->vectors; 490209371Smav sc->useirq |= (1 << i); 491209371Smav } 492209371Smav } 493209440Smav if (sc->legacy_route && sc->t[0].irq < 0 && sc->t[1].irq < 0) 494209440Smav sc->legacy_route = 0; 495209440Smav if (sc->legacy_route) 496209440Smav hpet_enable(sc); 497209440Smav /* Group timers for per-CPU operation. */ 498209440Smav num_percpu_et = min(num_msi / mp_ncpus, 2); 499209440Smav num_percpu_t = num_percpu_et * mp_ncpus; 500209440Smav pcpu_master = 0; 501209440Smav cur_cpu = CPU_FIRST(); 502209440Smav for (i = 0; i < num_timers; i++) { 503209440Smav t = &sc->t[i]; 504209440Smav if (t->irq >= 0 && num_percpu_t > 0) { 505209440Smav if (cur_cpu == CPU_FIRST()) 506209440Smav pcpu_master = i; 507209440Smav t->pcpu_master = pcpu_master; 508209440Smav sc->t[pcpu_master]. 509209440Smav pcpu_slaves[cur_cpu] = i; 510209440Smav bus_bind_intr(dev, t->intr_res, cur_cpu); 511209440Smav cur_cpu = CPU_NEXT(cur_cpu); 512209440Smav num_percpu_t--; 513209440Smav } 514209440Smav } 515209371Smav bus_write_4(sc->mem_res, HPET_ISR, 0xffffffff); 516209371Smav sc->irq = -1; 517209371Smav sc->intr_rid = -1; 518209371Smav /* If at least one timer needs legacy IRQ - setup it. */ 519209371Smav if (sc->useirq) { 520209371Smav j = i = fls(cvectors) - 1; 521209371Smav while (j > 0 && (cvectors & (1 << (j - 1))) != 0) 522209371Smav j--; 523209371Smav if (!(sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ, 524209371Smav &sc->intr_rid, j, i, 1, RF_SHAREABLE | RF_ACTIVE))) 525209371Smav device_printf(dev,"Can't map interrupt.\n"); 526209371Smav else if ((bus_setup_intr(dev, sc->intr_res, 527209371Smav INTR_MPSAFE | INTR_TYPE_CLK, 528209371Smav (driver_filter_t *)hpet_intr, NULL, 529209371Smav sc, &sc->intr_handle))) { 530209371Smav device_printf(dev, "Can't setup interrupt.\n"); 531209371Smav } else { 532209371Smav sc->irq = rman_get_start(sc->intr_res); 533209371Smav /* Bind IRQ to BSP to avoid live migration. */ 534209371Smav bus_bind_intr(dev, sc->intr_res, CPU_FIRST()); 535209371Smav } 536209371Smav } 537209371Smav /* Program and announce event timers. */ 538209371Smav for (i = 0; i < num_timers; i++) { 539209371Smav t = &sc->t[i]; 540209371Smav t->caps &= ~(HPET_TCNF_FSB_EN | HPET_TCNF_INT_ROUTE); 541209371Smav t->caps &= ~(HPET_TCNF_VAL_SET | HPET_TCNF_INT_ENB); 542209440Smav t->caps &= ~(HPET_TCNF_INT_TYPE); 543209371Smav t->caps |= HPET_TCNF_32MODE; 544209440Smav if (t->irq >= 0 && sc->legacy_route && i < 2) { 545209440Smav /* Legacy route doesn't need more configuration. */ 546209440Smav } else 547209371Smav#ifdef DEV_APIC 548209371Smav if (t->irq >= 0) { 549209371Smav uint64_t addr; 550209371Smav uint32_t data; 551209371Smav 552209371Smav if (PCIB_MAP_MSI( 553209371Smav device_get_parent(device_get_parent(dev)), dev, 554209371Smav t->irq, &addr, &data) == 0) { 555209371Smav bus_write_4(sc->mem_res, 556209371Smav HPET_TIMER_FSB_ADDR(i), addr); 557209371Smav bus_write_4(sc->mem_res, 558209371Smav HPET_TIMER_FSB_VAL(i), data); 559209371Smav t->caps |= HPET_TCNF_FSB_EN; 560209371Smav } else 561209371Smav t->irq = -2; 562209371Smav } else 563209371Smav#endif 564209431Smav if (sc->irq >= 0 && (t->vectors & (1 << sc->irq))) 565209371Smav t->caps |= (sc->irq << 9) | HPET_TCNF_INT_TYPE; 566209371Smav bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(i), t->caps); 567209371Smav /* Skip event timers without set up IRQ. */ 568209371Smav if (t->irq < 0 && 569209371Smav (sc->irq < 0 || (t->vectors & (1 << sc->irq)) == 0)) 570209371Smav continue; 571209371Smav /* Announce the reset. */ 572209371Smav if (maxhpetet == 0) 573209371Smav t->et.et_name = "HPET"; 574209371Smav else { 575209371Smav sprintf(t->name, "HPET%d", maxhpetet); 576209371Smav t->et.et_name = t->name; 577209371Smav } 578209371Smav t->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT; 579209371Smav t->et.et_quality = 450; 580209371Smav if (t->pcpu_master >= 0) { 581209371Smav t->et.et_flags |= ET_FLAGS_PERCPU; 582209371Smav t->et.et_quality += 100; 583209371Smav } 584209371Smav if ((t->caps & HPET_TCAP_PER_INT) == 0) 585209371Smav t->et.et_quality -= 10; 586209371Smav t->et.et_frequency = sc->freq; 587210290Smav t->et.et_min_period.sec = 0; 588210298Smav t->et.et_min_period.frac = 0x00004000LLU << 32; 589210298Smav t->et.et_max_period.sec = 0xfffffffeLLU / sc->freq; 590210290Smav t->et.et_max_period.frac = 591210298Smav ((0xfffffffeLLU << 32) / sc->freq) << 32; 592209371Smav t->et.et_start = hpet_start; 593209371Smav t->et.et_stop = hpet_stop; 594209371Smav t->et.et_priv = &sc->t[i]; 595209371Smav if (t->pcpu_master < 0 || t->pcpu_master == i) { 596209371Smav et_register(&t->et); 597209371Smav maxhpetet++; 598209371Smav } 599209371Smav } 600159217Snjl return (0); 601159217Snjl} 602159217Snjl 603159217Snjlstatic int 604209371Smavhpet_detach(device_t dev) 605159217Snjl{ 606159217Snjl ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__); 607159217Snjl 608159217Snjl /* XXX Without a tc_remove() function, we can't detach. */ 609159217Snjl return (EBUSY); 610159217Snjl} 611159217Snjl 612168010Snjlstatic int 613209371Smavhpet_suspend(device_t dev) 614175361Sjhb{ 615209371Smav struct hpet_softc *sc; 616175361Sjhb 617175361Sjhb /* 618175361Sjhb * Disable the timer during suspend. The timer will not lose 619175361Sjhb * its state in S1 or S2, but we are required to disable 620175361Sjhb * it. 621175361Sjhb */ 622175361Sjhb sc = device_get_softc(dev); 623175361Sjhb hpet_disable(sc); 624175361Sjhb 625175361Sjhb return (0); 626175361Sjhb} 627175361Sjhb 628175361Sjhbstatic int 629209371Smavhpet_resume(device_t dev) 630168010Snjl{ 631209371Smav struct hpet_softc *sc; 632209371Smav struct hpet_timer *t; 633209371Smav int i; 634168010Snjl 635168010Snjl /* Re-enable the timer after a resume to keep the clock advancing. */ 636168010Snjl sc = device_get_softc(dev); 637175361Sjhb hpet_enable(sc); 638209371Smav /* Restart event timers that were running on suspend. */ 639209371Smav for (i = 0; i < sc->num_timers; i++) { 640209371Smav t = &sc->t[i]; 641209371Smav#ifdef DEV_APIC 642209440Smav if (t->irq >= 0 && (sc->legacy_route == 0 || i >= 2)) { 643209371Smav uint64_t addr; 644209371Smav uint32_t data; 645209371Smav 646209371Smav if (PCIB_MAP_MSI( 647209371Smav device_get_parent(device_get_parent(dev)), dev, 648209371Smav t->irq, &addr, &data) == 0) { 649209371Smav bus_write_4(sc->mem_res, 650209371Smav HPET_TIMER_FSB_ADDR(i), addr); 651209371Smav bus_write_4(sc->mem_res, 652209371Smav HPET_TIMER_FSB_VAL(i), data); 653209371Smav } 654209371Smav } 655209371Smav#endif 656209371Smav if (t->mode == 0) 657209371Smav continue; 658209371Smav t->last = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER); 659209371Smav if (t->mode == 1 && (t->caps & HPET_TCAP_PER_INT)) { 660209371Smav t->caps |= HPET_TCNF_TYPE; 661209371Smav bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), 662209371Smav t->caps | HPET_TCNF_VAL_SET); 663209371Smav bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num), 664209371Smav t->last + t->div); 665209371Smav bus_read_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num)); 666209371Smav bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num), 667209371Smav t->div); 668209371Smav } else { 669209371Smav bus_write_4(sc->mem_res, HPET_TIMER_COMPARATOR(t->num), 670209371Smav t->last + sc->freq / 1024); 671209371Smav } 672209371Smav bus_write_4(sc->mem_res, HPET_ISR, 1 << t->num); 673209371Smav bus_write_4(sc->mem_res, HPET_TIMER_CAP_CNF(t->num), t->caps); 674209371Smav } 675168010Snjl return (0); 676168010Snjl} 677168010Snjl 678159217Snjl/* Print some basic latency/rate information to assist in debugging. */ 679159217Snjlstatic void 680209371Smavhpet_test(struct hpet_softc *sc) 681159217Snjl{ 682151912Sphk int i; 683151912Sphk uint32_t u1, u2; 684151912Sphk struct bintime b0, b1, b2; 685151912Sphk struct timespec ts; 686151912Sphk 687151912Sphk binuptime(&b0); 688151912Sphk binuptime(&b0); 689151912Sphk binuptime(&b1); 690175385Sjhb u1 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER); 691151912Sphk for (i = 1; i < 1000; i++) 692175385Sjhb u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER); 693151912Sphk binuptime(&b2); 694175385Sjhb u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER); 695151912Sphk 696151912Sphk bintime_sub(&b2, &b1); 697151912Sphk bintime_sub(&b1, &b0); 698151912Sphk bintime_sub(&b2, &b1); 699151912Sphk bintime2timespec(&b2, &ts); 700151912Sphk 701159217Snjl device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n", 702151912Sphk (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1); 703151912Sphk 704159217Snjl device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000); 705151912Sphk} 706151912Sphk 707209371Smav#ifdef DEV_APIC 708209371Smavstatic int 709209371Smavhpet_remap_intr(device_t dev, device_t child, u_int irq) 710209371Smav{ 711209371Smav struct hpet_softc *sc = device_get_softc(dev); 712209371Smav struct hpet_timer *t; 713209371Smav uint64_t addr; 714209371Smav uint32_t data; 715209371Smav int error, i; 716209371Smav 717209371Smav for (i = 0; i < sc->num_timers; i++) { 718209371Smav t = &sc->t[i]; 719209371Smav if (t->irq != irq) 720209371Smav continue; 721209371Smav error = PCIB_MAP_MSI( 722209371Smav device_get_parent(device_get_parent(dev)), dev, 723209371Smav irq, &addr, &data); 724209371Smav if (error) 725209371Smav return (error); 726209371Smav hpet_disable(sc); /* Stop timer to avoid interrupt loss. */ 727209371Smav bus_write_4(sc->mem_res, HPET_TIMER_FSB_ADDR(i), addr); 728209371Smav bus_write_4(sc->mem_res, HPET_TIMER_FSB_VAL(i), data); 729209371Smav hpet_enable(sc); 730209371Smav return (0); 731209371Smav } 732209371Smav return (ENOENT); 733209371Smav} 734209371Smav#endif 735209371Smav 736209371Smavstatic device_method_t hpet_methods[] = { 737151912Sphk /* Device interface */ 738209371Smav DEVMETHOD(device_identify, hpet_identify), 739209371Smav DEVMETHOD(device_probe, hpet_probe), 740209371Smav DEVMETHOD(device_attach, hpet_attach), 741209371Smav DEVMETHOD(device_detach, hpet_detach), 742209371Smav DEVMETHOD(device_suspend, hpet_suspend), 743209371Smav DEVMETHOD(device_resume, hpet_resume), 744151912Sphk 745209371Smav#ifdef DEV_APIC 746209371Smav DEVMETHOD(bus_remap_intr, hpet_remap_intr), 747209371Smav#endif 748209371Smav 749151912Sphk {0, 0} 750151912Sphk}; 751151912Sphk 752209371Smavstatic driver_t hpet_driver = { 753209371Smav "hpet", 754209371Smav hpet_methods, 755209371Smav sizeof(struct hpet_softc), 756151912Sphk}; 757151912Sphk 758209371SmavDRIVER_MODULE(hpet, acpi, hpet_driver, hpet_devclass, 0, 0); 759209371SmavMODULE_DEPEND(hpet, acpi, 1, 1, 1); 760