115e32d5dSMike Smith /*-
2787fa5b8SMike Smith * Copyright (c) 2000, 2001 Michael Smith
315e32d5dSMike Smith * Copyright (c) 2000 BSDi
415e32d5dSMike Smith * All rights reserved.
515e32d5dSMike Smith *
615e32d5dSMike Smith * Redistribution and use in source and binary forms, with or without
715e32d5dSMike Smith * modification, are permitted provided that the following conditions
815e32d5dSMike Smith * are met:
915e32d5dSMike Smith * 1. Redistributions of source code must retain the above copyright
1015e32d5dSMike Smith * notice, this list of conditions and the following disclaimer.
1115e32d5dSMike Smith * 2. Redistributions in binary form must reproduce the above copyright
1215e32d5dSMike Smith * notice, this list of conditions and the following disclaimer in the
1315e32d5dSMike Smith * documentation and/or other materials provided with the distribution.
1415e32d5dSMike Smith *
1515e32d5dSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1615e32d5dSMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1715e32d5dSMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1815e32d5dSMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1915e32d5dSMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2015e32d5dSMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2115e32d5dSMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2215e32d5dSMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2315e32d5dSMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2415e32d5dSMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2515e32d5dSMike Smith * SUCH DAMAGE.
2615e32d5dSMike Smith */
27dad97feeSDavid E. O'Brien
28dad97feeSDavid E. O'Brien #include <sys/cdefs.h>
2915e32d5dSMike Smith #include "opt_acpi.h"
3015e32d5dSMike Smith #include <sys/param.h>
3115e32d5dSMike Smith #include <sys/bus.h>
32cc43a851SJung-uk Kim #include <sys/eventhandler.h>
33787fa5b8SMike Smith #include <sys/kernel.h>
34fe12f24bSPoul-Henning Kamp #include <sys/module.h>
35787fa5b8SMike Smith #include <sys/sysctl.h>
36787fa5b8SMike Smith #include <sys/timetc.h>
37787fa5b8SMike Smith
38787fa5b8SMike Smith #include <machine/bus.h>
39787fa5b8SMike Smith #include <machine/resource.h>
40787fa5b8SMike Smith #include <sys/rman.h>
4115e32d5dSMike Smith
42129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
43129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
44129d3046SJung-uk Kim
4587e5d361SJohn Baldwin #include <dev/acpica/acpivar.h>
46cace7a2aSWarner Losh #include <dev/pci/pcivar.h>
47787fa5b8SMike Smith
48787fa5b8SMike Smith /*
49787fa5b8SMike Smith * A timecounter based on the free-running ACPI timer.
50787fa5b8SMike Smith *
51787fa5b8SMike Smith * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
52787fa5b8SMike Smith */
5315e32d5dSMike Smith
54be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
553184cf5aSNate Lawson #define _COMPONENT ACPI_TIMER
5672e5754cSMike Smith ACPI_MODULE_NAME("TIMER")
570ae55423SMike Smith
58787fa5b8SMike Smith static device_t acpi_timer_dev;
59af807c0fSNate Lawson static struct resource *acpi_timer_reg;
60d9b6df60SNate Lawson static bus_space_handle_t acpi_timer_bsh;
61d9b6df60SNate Lawson static bus_space_tag_t acpi_timer_bst;
62cc43a851SJung-uk Kim static eventhandler_tag acpi_timer_eh;
6315e32d5dSMike Smith
64787fa5b8SMike Smith static u_int acpi_timer_frequency = 14318182 / 4;
6515e32d5dSMike Smith
66c2641d23SRoger Pau Monné /* Knob to disable acpi_timer device */
67c2641d23SRoger Pau Monné bool acpi_timer_disabled = false;
68c2641d23SRoger Pau Monné
6915e32d5dSMike Smith static void acpi_timer_identify(driver_t *driver, device_t parent);
7015e32d5dSMike Smith static int acpi_timer_probe(device_t dev);
7115e32d5dSMike Smith static int acpi_timer_attach(device_t dev);
72cc43a851SJung-uk Kim static void acpi_timer_resume_handler(struct timecounter *);
736303ae4dSJung-uk Kim static void acpi_timer_suspend_handler(struct timecounter *);
7475988358SNate Lawson static u_int acpi_timer_get_timecount(struct timecounter *tc);
7575988358SNate Lawson static u_int acpi_timer_get_timecount_safe(struct timecounter *tc);
76787fa5b8SMike Smith static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
77214475d8SMarcel Moolenaar
7815e32d5dSMike Smith static device_method_t acpi_timer_methods[] = {
7915e32d5dSMike Smith DEVMETHOD(device_identify, acpi_timer_identify),
8015e32d5dSMike Smith DEVMETHOD(device_probe, acpi_timer_probe),
8115e32d5dSMike Smith DEVMETHOD(device_attach, acpi_timer_attach),
8215e32d5dSMike Smith
8361bfd867SSofian Brabez DEVMETHOD_END
8415e32d5dSMike Smith };
8515e32d5dSMike Smith
8615e32d5dSMike Smith static driver_t acpi_timer_driver = {
8715e32d5dSMike Smith "acpi_timer",
8815e32d5dSMike Smith acpi_timer_methods,
89787fa5b8SMike Smith 0,
9015e32d5dSMike Smith };
9115e32d5dSMike Smith
92916a5d8aSJohn Baldwin DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, 0, 0);
9364278df5SNate Lawson MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
9415e32d5dSMike Smith
95787fa5b8SMike Smith static struct timecounter acpi_timer_timecounter = {
96d9b6df60SNate Lawson acpi_timer_get_timecount_safe, /* get_timecount function */
97d9b6df60SNate Lawson 0, /* no poll_pps */
98d9b6df60SNate Lawson 0, /* no default counter_mask */
99d9b6df60SNate Lawson 0, /* no default frequency */
100d9b6df60SNate Lawson "ACPI", /* name */
101430eaa74SNate Lawson -1 /* quality (chosen later) */
102787fa5b8SMike Smith };
103787fa5b8SMike Smith
10493bd1f7eSJung-uk Kim static __inline uint32_t
acpi_timer_read(void)10593bd1f7eSJung-uk Kim acpi_timer_read(void)
106214475d8SMarcel Moolenaar {
10793bd1f7eSJung-uk Kim
108c0b9a6deSNate Lawson return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0));
109214475d8SMarcel Moolenaar }
110cb877d00SPoul-Henning Kamp
111787fa5b8SMike Smith /*
112787fa5b8SMike Smith * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
113787fa5b8SMike Smith * we will be using.
114787fa5b8SMike Smith */
11515e32d5dSMike Smith static void
acpi_timer_identify(driver_t * driver,device_t parent)11615e32d5dSMike Smith acpi_timer_identify(driver_t *driver, device_t parent)
11715e32d5dSMike Smith {
11815e32d5dSMike Smith device_t dev;
1192dd1bdf1SJustin Hibbits rman_res_t rlen, rstart;
120be1841b4SNate Lawson int rid, rtype;
12115e32d5dSMike Smith
122b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1230ae55423SMike Smith
1244f8c4e4dSNate Lawson if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) ||
125ef745449SAndrew Turner acpi_timer_dev || acpi_timer_disabled ||
126ef745449SAndrew Turner AcpiGbl_FADT.PmTimerLength == 0)
1270ae55423SMike Smith return_VOID;
12815e32d5dSMike Smith
129404b0d10SJung-uk Kim if ((dev = BUS_ADD_CHILD(parent, 2, "acpi_timer", 0)) == NULL) {
13015e32d5dSMike Smith device_printf(parent, "could not add acpi_timer0\n");
1310ae55423SMike Smith return_VOID;
13215e32d5dSMike Smith }
133787fa5b8SMike Smith acpi_timer_dev = dev;
134214475d8SMarcel Moolenaar
1356af444b1SJung-uk Kim switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
1366af444b1SJung-uk Kim case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1376af444b1SJung-uk Kim rtype = SYS_RES_MEMORY;
1386af444b1SJung-uk Kim break;
1396af444b1SJung-uk Kim case ACPI_ADR_SPACE_SYSTEM_IO:
1406af444b1SJung-uk Kim rtype = SYS_RES_IOPORT;
1416af444b1SJung-uk Kim break;
1426af444b1SJung-uk Kim default:
1436af444b1SJung-uk Kim return_VOID;
1446af444b1SJung-uk Kim }
145787fa5b8SMike Smith rid = 0;
1462be4e471SJung-uk Kim rlen = AcpiGbl_FADT.PmTimerLength;
1472be4e471SJung-uk Kim rstart = AcpiGbl_FADT.XPmTimerBlock.Address;
148be1841b4SNate Lawson if (bus_set_resource(dev, rtype, rid, rstart, rlen))
149da1b038aSJustin Hibbits device_printf(dev, "couldn't set resource (%s 0x%jx+0x%jx)\n",
150be1841b4SNate Lawson (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen);
1510ae55423SMike Smith return_VOID;
15215e32d5dSMike Smith }
153be1841b4SNate Lawson
154be1841b4SNate Lawson static int
acpi_timer_probe(device_t dev)155be1841b4SNate Lawson acpi_timer_probe(device_t dev)
156be1841b4SNate Lawson {
157*3fdf11feSBaptiste Daroussin int rid, rtype;
158be1841b4SNate Lawson
159be1841b4SNate Lawson ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
160be1841b4SNate Lawson
161be1841b4SNate Lawson if (dev != acpi_timer_dev)
162be1841b4SNate Lawson return (ENXIO);
163be1841b4SNate Lawson
1646af444b1SJung-uk Kim switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
1656af444b1SJung-uk Kim case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1666af444b1SJung-uk Kim rtype = SYS_RES_MEMORY;
1676af444b1SJung-uk Kim break;
1686af444b1SJung-uk Kim case ACPI_ADR_SPACE_SYSTEM_IO:
1696af444b1SJung-uk Kim rtype = SYS_RES_IOPORT;
1706af444b1SJung-uk Kim break;
1716af444b1SJung-uk Kim default:
1726af444b1SJung-uk Kim return (ENXIO);
1736af444b1SJung-uk Kim }
174be1841b4SNate Lawson rid = 0;
175be1841b4SNate Lawson acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
176be1841b4SNate Lawson if (acpi_timer_reg == NULL) {
177be1841b4SNate Lawson device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n",
178be1841b4SNate Lawson (rtype == SYS_RES_IOPORT) ? "port" : "mem",
1792be4e471SJung-uk Kim (u_long)AcpiGbl_FADT.XPmTimerBlock.Address);
180be1841b4SNate Lawson return (ENXIO);
181be1841b4SNate Lawson }
18214827d7eSNate Lawson acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
18314827d7eSNate Lawson acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
1842be4e471SJung-uk Kim if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER)
18575988358SNate Lawson acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
18675988358SNate Lawson else
18775988358SNate Lawson acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
18875988358SNate Lawson acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
189428b7ca2SJustin T. Gibbs acpi_timer_timecounter.tc_flags = TC_FLAGS_SUSPEND_SAFE;
190787fa5b8SMike Smith
191cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_name = "ACPI-fast";
192cb877d00SPoul-Henning Kamp acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
193ca5f1efdSJung-uk Kim acpi_timer_timecounter.tc_quality = 900;
194787fa5b8SMike Smith tc_init(&acpi_timer_timecounter);
19515e32d5dSMike Smith
196bad36a49SMark Johnston device_set_descf(dev, "%d-bit timer at %u.%06uMHz",
1976af444b1SJung-uk Kim (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) != 0 ? 32 : 24,
1986af444b1SJung-uk Kim acpi_timer_frequency / 1000000, acpi_timer_frequency % 1000000);
1990ae55423SMike Smith
200be1841b4SNate Lawson /* Release the resource, we'll allocate it again during attach. */
201be1841b4SNate Lawson bus_release_resource(dev, rtype, rid, acpi_timer_reg);
20215e32d5dSMike Smith return (0);
20315e32d5dSMike Smith }
20415e32d5dSMike Smith
20515e32d5dSMike Smith static int
acpi_timer_attach(device_t dev)20615e32d5dSMike Smith acpi_timer_attach(device_t dev)
20715e32d5dSMike Smith {
208be1841b4SNate Lawson int rid, rtype;
209be1841b4SNate Lawson
210be1841b4SNate Lawson ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
211be1841b4SNate Lawson
2126af444b1SJung-uk Kim switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
2136af444b1SJung-uk Kim case ACPI_ADR_SPACE_SYSTEM_MEMORY:
2146af444b1SJung-uk Kim rtype = SYS_RES_MEMORY;
2156af444b1SJung-uk Kim break;
2166af444b1SJung-uk Kim case ACPI_ADR_SPACE_SYSTEM_IO:
2176af444b1SJung-uk Kim rtype = SYS_RES_IOPORT;
2186af444b1SJung-uk Kim break;
2196af444b1SJung-uk Kim default:
2206af444b1SJung-uk Kim return (ENXIO);
2216af444b1SJung-uk Kim }
222be1841b4SNate Lawson rid = 0;
223be1841b4SNate Lawson acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
224be1841b4SNate Lawson if (acpi_timer_reg == NULL)
225be1841b4SNate Lawson return (ENXIO);
226be1841b4SNate Lawson acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
227be1841b4SNate Lawson acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
2286303ae4dSJung-uk Kim
2296303ae4dSJung-uk Kim /* Register suspend event handler. */
2306303ae4dSJung-uk Kim if (EVENTHANDLER_REGISTER(power_suspend, acpi_timer_suspend_handler,
2316303ae4dSJung-uk Kim &acpi_timer_timecounter, EVENTHANDLER_PRI_LAST) == NULL)
2326303ae4dSJung-uk Kim device_printf(dev, "failed to register suspend event handler\n");
2336303ae4dSJung-uk Kim
234787fa5b8SMike Smith return (0);
23515e32d5dSMike Smith }
236787fa5b8SMike Smith
237cc43a851SJung-uk Kim static void
acpi_timer_resume_handler(struct timecounter * newtc)238cc43a851SJung-uk Kim acpi_timer_resume_handler(struct timecounter *newtc)
239cc43a851SJung-uk Kim {
240cc43a851SJung-uk Kim struct timecounter *tc;
241cc43a851SJung-uk Kim
242cc43a851SJung-uk Kim tc = timecounter;
243cc43a851SJung-uk Kim if (tc != newtc) {
244cc43a851SJung-uk Kim if (bootverbose)
245cc43a851SJung-uk Kim device_printf(acpi_timer_dev,
246cc43a851SJung-uk Kim "restoring timecounter, %s -> %s\n",
247cc43a851SJung-uk Kim tc->tc_name, newtc->tc_name);
248cc43a851SJung-uk Kim (void)newtc->tc_get_timecount(newtc);
249cc43a851SJung-uk Kim timecounter = newtc;
250cc43a851SJung-uk Kim }
251cc43a851SJung-uk Kim }
252cc43a851SJung-uk Kim
2536303ae4dSJung-uk Kim static void
acpi_timer_suspend_handler(struct timecounter * newtc)2546303ae4dSJung-uk Kim acpi_timer_suspend_handler(struct timecounter *newtc)
255cc43a851SJung-uk Kim {
2566303ae4dSJung-uk Kim struct timecounter *tc;
257cc43a851SJung-uk Kim
2586303ae4dSJung-uk Kim /* Deregister existing resume event handler. */
259cc43a851SJung-uk Kim if (acpi_timer_eh != NULL) {
260cc43a851SJung-uk Kim EVENTHANDLER_DEREGISTER(power_resume, acpi_timer_eh);
261cc43a851SJung-uk Kim acpi_timer_eh = NULL;
262cc43a851SJung-uk Kim }
2636303ae4dSJung-uk Kim
264428b7ca2SJustin T. Gibbs if ((timecounter->tc_flags & TC_FLAGS_SUSPEND_SAFE) != 0) {
265428b7ca2SJustin T. Gibbs /*
266428b7ca2SJustin T. Gibbs * If we are using a suspend safe timecounter, don't
267428b7ca2SJustin T. Gibbs * save/restore it across suspend/resume.
268428b7ca2SJustin T. Gibbs */
269428b7ca2SJustin T. Gibbs return;
270428b7ca2SJustin T. Gibbs }
271428b7ca2SJustin T. Gibbs
2726303ae4dSJung-uk Kim KASSERT(newtc == &acpi_timer_timecounter,
2736303ae4dSJung-uk Kim ("acpi_timer_suspend_handler: wrong timecounter"));
2746303ae4dSJung-uk Kim
275cc43a851SJung-uk Kim tc = timecounter;
276cc43a851SJung-uk Kim if (tc != newtc) {
277cc43a851SJung-uk Kim if (bootverbose)
2786303ae4dSJung-uk Kim device_printf(acpi_timer_dev,
2796303ae4dSJung-uk Kim "switching timecounter, %s -> %s\n",
280cc43a851SJung-uk Kim tc->tc_name, newtc->tc_name);
281cc43a851SJung-uk Kim (void)acpi_timer_read();
282cc43a851SJung-uk Kim (void)acpi_timer_read();
283cc43a851SJung-uk Kim timecounter = newtc;
284cc43a851SJung-uk Kim acpi_timer_eh = EVENTHANDLER_REGISTER(power_resume,
285cc43a851SJung-uk Kim acpi_timer_resume_handler, tc, EVENTHANDLER_PRI_LAST);
286cc43a851SJung-uk Kim }
287cc43a851SJung-uk Kim }
288cc43a851SJung-uk Kim
289787fa5b8SMike Smith /*
290feade919SMike Smith * Fetch current time value from reliable hardware.
291787fa5b8SMike Smith */
29275988358SNate Lawson static u_int
acpi_timer_get_timecount(struct timecounter * tc)293787fa5b8SMike Smith acpi_timer_get_timecount(struct timecounter *tc)
294787fa5b8SMike Smith {
295d9b6df60SNate Lawson return (acpi_timer_read());
296787fa5b8SMike Smith }
297787fa5b8SMike Smith
298787fa5b8SMike Smith /*
299feade919SMike Smith * Fetch current time value from hardware that may not correctly
300af807c0fSNate Lawson * latch the counter. We need to read until we have three monotonic
301af807c0fSNate Lawson * samples and then use the middle one, otherwise we are not protected
302af807c0fSNate Lawson * against the fact that the bits can be wrong in two directions. If
303af807c0fSNate Lawson * we only cared about monosity, two reads would be enough.
304feade919SMike Smith */
30575988358SNate Lawson static u_int
acpi_timer_get_timecount_safe(struct timecounter * tc)306feade919SMike Smith acpi_timer_get_timecount_safe(struct timecounter *tc)
307feade919SMike Smith {
30875988358SNate Lawson u_int u1, u2, u3;
309feade919SMike Smith
310d9b6df60SNate Lawson u2 = acpi_timer_read();
311d9b6df60SNate Lawson u3 = acpi_timer_read();
312feade919SMike Smith do {
313feade919SMike Smith u1 = u2;
314feade919SMike Smith u2 = u3;
315d9b6df60SNate Lawson u3 = acpi_timer_read();
316d9b6df60SNate Lawson } while (u1 > u2 || u2 > u3);
317be2b1797SNate Lawson
318feade919SMike Smith return (u2);
319feade919SMike Smith }
320feade919SMike Smith
321feade919SMike Smith /*
322787fa5b8SMike Smith * Timecounter freqency adjustment interface.
323787fa5b8SMike Smith */
324787fa5b8SMike Smith static int
acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)325787fa5b8SMike Smith acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
326787fa5b8SMike Smith {
327787fa5b8SMike Smith int error;
328787fa5b8SMike Smith u_int freq;
329787fa5b8SMike Smith
330787fa5b8SMike Smith if (acpi_timer_timecounter.tc_frequency == 0)
331787fa5b8SMike Smith return (EOPNOTSUPP);
332787fa5b8SMike Smith freq = acpi_timer_frequency;
333041b706bSDavid Malone error = sysctl_handle_int(oidp, &freq, 0, req);
334787fa5b8SMike Smith if (error == 0 && req->newptr != NULL) {
335787fa5b8SMike Smith acpi_timer_frequency = freq;
336787fa5b8SMike Smith acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
337787fa5b8SMike Smith }
338be2b1797SNate Lawson
339787fa5b8SMike Smith return (error);
340787fa5b8SMike Smith }
341787fa5b8SMike Smith
3427029da5cSPawel Biernacki SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq,
3433e68d2c5SAlexander Motin CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
3447029da5cSPawel Biernacki acpi_timer_sysctl_freq, "I",
3457029da5cSPawel Biernacki "ACPI timer frequency");
346