1 /* $OpenBSD: acpitimer.c,v 1.16 2022/08/25 17:43:34 cheloha Exp $ */ 2 /* 3 * Copyright (c) 2005 Thorsten Lockert <tholo@sigmasoft.com> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/device.h> 21 #include <sys/stdint.h> 22 #include <sys/timetc.h> 23 24 #include <machine/bus.h> 25 #include <machine/cpu.h> 26 27 #include <dev/acpi/acpireg.h> 28 #include <dev/acpi/acpivar.h> 29 30 struct acpitimer_softc { 31 struct device sc_dev; 32 33 bus_space_tag_t sc_iot; 34 bus_space_handle_t sc_ioh; 35 }; 36 37 int acpitimermatch(struct device *, void *, void *); 38 void acpitimerattach(struct device *, struct device *, void *); 39 void acpitimer_delay(int); 40 u_int acpi_get_timecount(struct timecounter *tc); 41 uint32_t acpitimer_read(struct acpitimer_softc *); 42 43 static struct timecounter acpi_timecounter = { 44 .tc_get_timecount = acpi_get_timecount, 45 .tc_poll_pps = 0, 46 .tc_counter_mask = 0x00ffffff, /* 24 bits */ 47 .tc_frequency = ACPI_FREQUENCY, 48 .tc_name = 0, 49 .tc_quality = 1000, 50 .tc_priv = NULL, 51 .tc_user = 0, 52 }; 53 54 const struct cfattach acpitimer_ca = { 55 sizeof(struct acpitimer_softc), acpitimermatch, acpitimerattach 56 }; 57 58 struct cfdriver acpitimer_cd = { 59 NULL, "acpitimer", DV_DULL 60 }; 61 62 int 63 acpitimermatch(struct device *parent, void *match, void *aux) 64 { 65 struct acpi_attach_args *aa = aux; 66 struct cfdata *cf = match; 67 68 /* sanity */ 69 if (aa->aaa_name == NULL || 70 strcmp(aa->aaa_name, cf->cf_driver->cd_name) != 0 || 71 aa->aaa_table != NULL) 72 return (0); 73 74 return (1); 75 } 76 77 void 78 acpitimerattach(struct device *parent, struct device *self, void *aux) 79 { 80 struct acpitimer_softc *sc = (struct acpitimer_softc *) self; 81 struct acpi_softc *psc = (struct acpi_softc *) parent; 82 int rc; 83 84 if (psc->sc_fadt->hdr_revision >= 3 && 85 psc->sc_fadt->x_pm_tmr_blk.address != 0) 86 rc = acpi_map_address(psc, &psc->sc_fadt->x_pm_tmr_blk, 0, 87 psc->sc_fadt->pm_tmr_len, &sc->sc_ioh, &sc->sc_iot); 88 else 89 rc = acpi_map_address(psc, NULL, psc->sc_fadt->pm_tmr_blk, 90 psc->sc_fadt->pm_tmr_len, &sc->sc_ioh, &sc->sc_iot); 91 if (rc) { 92 printf(": can't map i/o space\n"); 93 return; 94 } 95 96 printf(": %d Hz, %d bits\n", ACPI_FREQUENCY, 97 psc->sc_fadt->flags & FADT_TMR_VAL_EXT ? 32 : 24); 98 99 if (psc->sc_fadt->flags & FADT_TMR_VAL_EXT) 100 acpi_timecounter.tc_counter_mask = 0xffffffffU; 101 acpi_timecounter.tc_priv = sc; 102 acpi_timecounter.tc_name = sc->sc_dev.dv_xname; 103 tc_init(&acpi_timecounter); 104 105 delay_init(acpitimer_delay, 1000); 106 107 #if defined(__amd64__) 108 extern void cpu_recalibrate_tsc(struct timecounter *); 109 cpu_recalibrate_tsc(&acpi_timecounter); 110 #endif 111 } 112 113 void 114 acpitimer_delay(int usecs) 115 { 116 uint64_t count = 0, cycles; 117 struct acpitimer_softc *sc = acpi_timecounter.tc_priv; 118 uint32_t mask = acpi_timecounter.tc_counter_mask; 119 uint32_t val1, val2; 120 121 val2 = acpitimer_read(sc); 122 cycles = usecs * acpi_timecounter.tc_frequency / 1000000; 123 while (count < cycles) { 124 CPU_BUSY_CYCLE(); 125 val1 = val2; 126 val2 = acpitimer_read(sc); 127 count += (val2 - val1) & mask; 128 } 129 } 130 131 u_int 132 acpi_get_timecount(struct timecounter *tc) 133 { 134 return acpitimer_read(tc->tc_priv); 135 } 136 137 uint32_t 138 acpitimer_read(struct acpitimer_softc *sc) 139 { 140 uint32_t u1, u2, u3; 141 142 u2 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0); 143 u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0); 144 do { 145 u1 = u2; 146 u2 = u3; 147 u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0); 148 } while (u1 > u2 || u2 > u3); 149 150 return (u2); 151 } 152