xref: /freebsd/sys/dev/acpica/acpi_timer.c (revision 439eb9c9771b8a62b5bcc8d2f7337f26c4d717e2)
1 /*-
2  * Copyright (c) 2000, 2001 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include "opt_acpi.h"
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/eventhandler.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/sysctl.h>
36 #include <sys/timetc.h>
37 #include <sys/power.h>
38 
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42 
43 #include <contrib/dev/acpica/include/acpi.h>
44 #include <contrib/dev/acpica/include/accommon.h>
45 
46 #include <dev/acpica/acpivar.h>
47 #include <dev/pci/pcivar.h>
48 
49 /*
50  * A timecounter based on the free-running ACPI timer.
51  *
52  * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
53  */
54 
55 /* Hooks for the ACPI CA debugging infrastructure */
56 #define _COMPONENT	ACPI_TIMER
57 ACPI_MODULE_NAME("TIMER")
58 
59 struct acpi_timer_softc {
60 	device_t	dev;
61 	struct resource *reg;
62 	struct timecounter *tc;
63 	struct timecounter *old_tc;
64 };
65 
66 #define	ACPI_TIMER_FREQUENCY	(14318182 / 4)
67 
68 static void	acpi_timer_identify(driver_t *driver, device_t parent);
69 static int	acpi_timer_probe(device_t dev);
70 static int	acpi_timer_attach(device_t dev);
71 static void	acpi_timer_resume_handler(struct acpi_timer_softc *,
72 		    enum power_stype);
73 static void	acpi_timer_suspend_handler(struct acpi_timer_softc *,
74 		    enum power_stype);
75 static u_int	acpi_timer_get_timecount(struct timecounter *tc);
76 static int	acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
77 
78 static device_method_t acpi_timer_methods[] = {
79     DEVMETHOD(device_identify,	acpi_timer_identify),
80     DEVMETHOD(device_probe,	acpi_timer_probe),
81     DEVMETHOD(device_attach,	acpi_timer_attach),
82 
83     DEVMETHOD_END
84 };
85 
86 static driver_t acpi_timer_driver = {
87     "acpi_timer",
88     acpi_timer_methods,
89     sizeof(struct acpi_timer_softc),
90 };
91 
92 DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, 0, 0);
93 MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
94 
95 static struct timecounter acpi_timer_timecounter = {
96 	acpi_timer_get_timecount,	/* get_timecount function */
97 	0,				/* no poll_pps */
98 	0,				/* no default counter_mask */
99 	0,				/* no default frequency */
100 	"ACPI",				/* name */
101 	900,				/* quality */
102 	TC_FLAGS_SUSPEND_SAFE		/* flags */
103 };
104 
105 static __inline uint32_t
106 acpi_timer_read(struct acpi_timer_softc *sc)
107 {
108 
109 	return (bus_read_4(sc->reg, 0));
110 }
111 
112 /*
113  * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
114  * we will be using.
115  */
116 static void
117 acpi_timer_identify(driver_t *driver, device_t parent)
118 {
119     device_t dev;
120     rman_res_t rlen, rstart;
121     int rtype;
122 
123     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
124 
125     if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) ||
126 	device_find_child(parent, "acpi_timer", DEVICE_UNIT_ANY) != NULL ||
127 	AcpiGbl_FADT.PmTimerLength == 0)
128 	return_VOID;
129 
130     if ((dev = BUS_ADD_CHILD(parent, 2, "acpi_timer", 0)) == NULL) {
131 	device_printf(parent, "could not add acpi_timer0\n");
132 	return_VOID;
133     }
134 
135     switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
136     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
137 	rtype = SYS_RES_MEMORY;
138 	break;
139     case ACPI_ADR_SPACE_SYSTEM_IO:
140 	rtype = SYS_RES_IOPORT;
141 	break;
142     default:
143 	return_VOID;
144     }
145     rlen = AcpiGbl_FADT.PmTimerLength;
146     rstart = AcpiGbl_FADT.XPmTimerBlock.Address;
147     if (bus_set_resource(dev, rtype, 0, rstart, rlen))
148 	device_printf(dev, "couldn't set resource (%s 0x%jx+0x%jx)\n",
149 	    (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen);
150     return_VOID;
151 }
152 
153 static int
154 acpi_timer_probe(device_t dev)
155 {
156     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
157 
158     device_set_descf(dev, "%d-bit timer at %u.%06uMHz",
159 	(AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) != 0 ? 32 : 24,
160 	ACPI_TIMER_FREQUENCY / 1000000, ACPI_TIMER_FREQUENCY % 1000000);
161     return (BUS_PROBE_NOWILDCARD);
162 }
163 
164 static int
165 acpi_timer_attach(device_t dev)
166 {
167     struct acpi_timer_softc *sc = device_get_softc(dev);
168     eventhandler_tag eh;
169     int rtype;
170 
171     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
172 
173     sc->dev = dev;
174     switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
175     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
176 	rtype = SYS_RES_MEMORY;
177 	break;
178     case ACPI_ADR_SPACE_SYSTEM_IO:
179 	rtype = SYS_RES_IOPORT;
180 	break;
181     default:
182 	return (ENXIO);
183     }
184 
185     sc->reg = bus_alloc_resource_any(dev, rtype, 0, RF_ACTIVE);
186     if (sc->reg == NULL)
187 	return (ENXIO);
188 
189     /* Register resume and suspend event handlers. */
190     eh = EVENTHANDLER_REGISTER(power_suspend, acpi_timer_suspend_handler,
191 	sc, EVENTHANDLER_PRI_LAST);
192     if (eh == NULL) {
193 	device_printf(dev, "failed to register suspend event handler\n");
194 	bus_release_resource(dev, sc->reg);
195 	return (ENXIO);
196     }
197     if (EVENTHANDLER_REGISTER(power_resume, acpi_timer_resume_handler,
198 	sc, EVENTHANDLER_PRI_LAST) == NULL) {
199 	device_printf(dev, "failed to register resume event handler\n");
200 	EVENTHANDLER_DEREGISTER(power_suspend, eh);
201 	bus_release_resource(dev, sc->reg);
202 	return (ENXIO);
203     }
204 
205     if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER)
206 	acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
207     else
208 	acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
209     acpi_timer_timecounter.tc_frequency = ACPI_TIMER_FREQUENCY;
210     acpi_timer_timecounter.tc_priv = sc;
211     sc->tc = &acpi_timer_timecounter;
212 
213     tc_init(sc->tc);
214 
215     SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_machdep), OID_AUTO,
216 	"acpi_timer_freq", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
217 	sc->tc, 0, acpi_timer_sysctl_freq, "I", "ACPI timer frequency");
218 
219     return (0);
220 }
221 
222 static void
223 acpi_timer_resume_handler(struct acpi_timer_softc *sc, enum power_stype stype)
224 {
225 	struct timecounter *newtc, *tc;
226 
227 	tc = timecounter;
228 	newtc = sc->old_tc;
229 	sc->old_tc = NULL;
230 	if (newtc != NULL && tc != newtc) {
231 		if (bootverbose)
232 			device_printf(sc->dev,
233 			    "restoring timecounter, %s -> %s\n",
234 			    tc->tc_name, newtc->tc_name);
235 		(void)newtc->tc_get_timecount(newtc);
236 		timecounter = newtc;
237 	}
238 }
239 
240 static void
241 acpi_timer_suspend_handler(struct acpi_timer_softc *sc, enum power_stype stype)
242 {
243 	struct timecounter *tc;
244 
245 	tc = timecounter;
246 	sc->old_tc = NULL;
247 	if ((tc->tc_flags & TC_FLAGS_SUSPEND_SAFE) != 0) {
248 		/*
249 		 * If we are using a suspend safe timecounter, don't
250 		 * save/restore it across suspend/resume.
251 		 */
252 		return;
253 	}
254 
255 	/*
256 	  * Our timecounter is suspend safe, so must not be currently
257 	  * active.
258 	  */
259 	MPASS(tc != sc->tc);
260 
261 	if (bootverbose)
262 		device_printf(sc->dev, "switching timecounter, %s -> %s\n",
263 		    tc->tc_name, sc->tc->tc_name);
264 	(void)acpi_timer_read(sc);
265 	(void)acpi_timer_read(sc);
266 	timecounter = sc->tc;
267 	sc->old_tc = tc;
268 }
269 
270 static u_int
271 acpi_timer_get_timecount(struct timecounter *tc)
272 {
273 	struct acpi_timer_softc *sc = tc->tc_priv;
274 	return (acpi_timer_read(sc));
275 }
276 
277 /*
278  * Timecounter frequency adjustment interface.
279  */
280 static int
281 acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
282 {
283     struct timecounter *tc = arg1;
284     int error;
285     u_int freq;
286 
287     freq = tc->tc_frequency;
288     error = sysctl_handle_int(oidp, &freq, 0, req);
289     if (error == 0 && req->newptr != NULL) {
290 	tc->tc_frequency = freq;
291     }
292 
293     return (error);
294 }
295