xref: /freebsd/sys/dev/acpica/acpi_timer.c (revision fc2ed9d9680461937c7ffa4cc77fa38e656deb8b)
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 static device_t			acpi_timer_dev;
60 static struct resource		*acpi_timer_reg;
61 static bus_space_handle_t	acpi_timer_bsh;
62 static bus_space_tag_t		acpi_timer_bst;
63 static eventhandler_tag		acpi_timer_eh;
64 
65 static u_int	acpi_timer_frequency = 14318182 / 4;
66 
67 /* Knob to disable acpi_timer device */
68 bool acpi_timer_disabled = false;
69 
70 static void	acpi_timer_identify(driver_t *driver, device_t parent);
71 static int	acpi_timer_probe(device_t dev);
72 static int	acpi_timer_attach(device_t dev);
73 static void	acpi_timer_resume_handler(struct timecounter *,
74 		    enum power_stype);
75 static void	acpi_timer_suspend_handler(struct timecounter *,
76 		    enum power_stype);
77 static u_int	acpi_timer_get_timecount(struct timecounter *tc);
78 static u_int	acpi_timer_get_timecount_safe(struct timecounter *tc);
79 static int	acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
80 
81 static device_method_t acpi_timer_methods[] = {
82     DEVMETHOD(device_identify,	acpi_timer_identify),
83     DEVMETHOD(device_probe,	acpi_timer_probe),
84     DEVMETHOD(device_attach,	acpi_timer_attach),
85 
86     DEVMETHOD_END
87 };
88 
89 static driver_t acpi_timer_driver = {
90     "acpi_timer",
91     acpi_timer_methods,
92     0,
93 };
94 
95 DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, 0, 0);
96 MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
97 
98 static struct timecounter acpi_timer_timecounter = {
99 	acpi_timer_get_timecount_safe,	/* get_timecount function */
100 	0,				/* no poll_pps */
101 	0,				/* no default counter_mask */
102 	0,				/* no default frequency */
103 	"ACPI",				/* name */
104 	-1				/* quality (chosen later) */
105 };
106 
107 static __inline uint32_t
acpi_timer_read(void)108 acpi_timer_read(void)
109 {
110 
111     return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0));
112 }
113 
114 /*
115  * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
116  * we will be using.
117  */
118 static void
acpi_timer_identify(driver_t * driver,device_t parent)119 acpi_timer_identify(driver_t *driver, device_t parent)
120 {
121     device_t dev;
122     rman_res_t rlen, rstart;
123     int rid, rtype;
124 
125     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
126 
127     if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) ||
128 	acpi_timer_dev || acpi_timer_disabled ||
129 	AcpiGbl_FADT.PmTimerLength == 0)
130 	return_VOID;
131 
132     if ((dev = BUS_ADD_CHILD(parent, 2, "acpi_timer", 0)) == NULL) {
133 	device_printf(parent, "could not add acpi_timer0\n");
134 	return_VOID;
135     }
136     acpi_timer_dev = dev;
137 
138     switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
139     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
140 	rtype = SYS_RES_MEMORY;
141 	break;
142     case ACPI_ADR_SPACE_SYSTEM_IO:
143 	rtype = SYS_RES_IOPORT;
144 	break;
145     default:
146 	return_VOID;
147     }
148     rid = 0;
149     rlen = AcpiGbl_FADT.PmTimerLength;
150     rstart = AcpiGbl_FADT.XPmTimerBlock.Address;
151     if (bus_set_resource(dev, rtype, rid, rstart, rlen))
152 	device_printf(dev, "couldn't set resource (%s 0x%jx+0x%jx)\n",
153 	    (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen);
154     return_VOID;
155 }
156 
157 static int
acpi_timer_probe(device_t dev)158 acpi_timer_probe(device_t dev)
159 {
160     int rid, rtype;
161 
162     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
163 
164     if (dev != acpi_timer_dev)
165 	return (ENXIO);
166 
167     switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
168     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
169 	rtype = SYS_RES_MEMORY;
170 	break;
171     case ACPI_ADR_SPACE_SYSTEM_IO:
172 	rtype = SYS_RES_IOPORT;
173 	break;
174     default:
175 	return (ENXIO);
176     }
177     rid = 0;
178     acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
179     if (acpi_timer_reg == NULL) {
180 	device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n",
181 	    (rtype == SYS_RES_IOPORT) ? "port" : "mem",
182 	    (u_long)AcpiGbl_FADT.XPmTimerBlock.Address);
183 	return (ENXIO);
184     }
185     acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
186     acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
187     if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER)
188 	acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
189     else
190 	acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
191     acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
192     acpi_timer_timecounter.tc_flags = TC_FLAGS_SUSPEND_SAFE;
193 
194     acpi_timer_timecounter.tc_name = "ACPI-fast";
195     acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
196     acpi_timer_timecounter.tc_quality = 900;
197     tc_init(&acpi_timer_timecounter);
198 
199     device_set_descf(dev, "%d-bit timer at %u.%06uMHz",
200 	(AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) != 0 ? 32 : 24,
201 	acpi_timer_frequency / 1000000, acpi_timer_frequency % 1000000);
202 
203     /* Release the resource, we'll allocate it again during attach. */
204     bus_release_resource(dev, rtype, rid, acpi_timer_reg);
205     return (0);
206 }
207 
208 static int
acpi_timer_attach(device_t dev)209 acpi_timer_attach(device_t dev)
210 {
211     int rid, rtype;
212 
213     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
214 
215     switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) {
216     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
217 	rtype = SYS_RES_MEMORY;
218 	break;
219     case ACPI_ADR_SPACE_SYSTEM_IO:
220 	rtype = SYS_RES_IOPORT;
221 	break;
222     default:
223 	return (ENXIO);
224     }
225     rid = 0;
226     acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
227     if (acpi_timer_reg == NULL)
228 	return (ENXIO);
229     acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
230     acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
231 
232     /* Register suspend event handler. */
233     if (EVENTHANDLER_REGISTER(power_suspend, acpi_timer_suspend_handler,
234 	&acpi_timer_timecounter, EVENTHANDLER_PRI_LAST) == NULL)
235 	device_printf(dev, "failed to register suspend event handler\n");
236 
237     return (0);
238 }
239 
240 static void
acpi_timer_resume_handler(struct timecounter * newtc,enum power_stype stype)241 acpi_timer_resume_handler(struct timecounter *newtc, enum power_stype stype)
242 {
243 	struct timecounter *tc;
244 
245 	tc = timecounter;
246 	if (tc != newtc) {
247 		if (bootverbose)
248 			device_printf(acpi_timer_dev,
249 			    "restoring timecounter, %s -> %s\n",
250 			    tc->tc_name, newtc->tc_name);
251 		(void)newtc->tc_get_timecount(newtc);
252 		timecounter = newtc;
253 	}
254 }
255 
256 static void
acpi_timer_suspend_handler(struct timecounter * newtc,enum power_stype stype)257 acpi_timer_suspend_handler(struct timecounter *newtc, enum power_stype stype)
258 {
259 	struct timecounter *tc;
260 
261 	/* Deregister existing resume event handler. */
262 	if (acpi_timer_eh != NULL) {
263 		EVENTHANDLER_DEREGISTER(power_resume, acpi_timer_eh);
264 		acpi_timer_eh = NULL;
265 	}
266 
267 	if ((timecounter->tc_flags & TC_FLAGS_SUSPEND_SAFE) != 0) {
268 		/*
269 		 * If we are using a suspend safe timecounter, don't
270 		 * save/restore it across suspend/resume.
271 		 */
272 		return;
273 	}
274 
275 	KASSERT(newtc == &acpi_timer_timecounter,
276 	    ("acpi_timer_suspend_handler: wrong timecounter"));
277 
278 	tc = timecounter;
279 	if (tc != newtc) {
280 		if (bootverbose)
281 			device_printf(acpi_timer_dev,
282 			    "switching timecounter, %s -> %s\n",
283 			    tc->tc_name, newtc->tc_name);
284 		(void)acpi_timer_read();
285 		(void)acpi_timer_read();
286 		timecounter = newtc;
287 		acpi_timer_eh = EVENTHANDLER_REGISTER(power_resume,
288 		    acpi_timer_resume_handler, tc, EVENTHANDLER_PRI_LAST);
289 	}
290 }
291 
292 /*
293  * Fetch current time value from reliable hardware.
294  */
295 static u_int
acpi_timer_get_timecount(struct timecounter * tc)296 acpi_timer_get_timecount(struct timecounter *tc)
297 {
298     return (acpi_timer_read());
299 }
300 
301 /*
302  * Fetch current time value from hardware that may not correctly
303  * latch the counter.  We need to read until we have three monotonic
304  * samples and then use the middle one, otherwise we are not protected
305  * against the fact that the bits can be wrong in two directions.  If
306  * we only cared about monosity, two reads would be enough.
307  */
308 static u_int
acpi_timer_get_timecount_safe(struct timecounter * tc)309 acpi_timer_get_timecount_safe(struct timecounter *tc)
310 {
311     u_int u1, u2, u3;
312 
313     u2 = acpi_timer_read();
314     u3 = acpi_timer_read();
315     do {
316 	u1 = u2;
317 	u2 = u3;
318 	u3 = acpi_timer_read();
319     } while (u1 > u2 || u2 > u3);
320 
321     return (u2);
322 }
323 
324 /*
325  * Timecounter freqency adjustment interface.
326  */
327 static int
acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)328 acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
329 {
330     int error;
331     u_int freq;
332 
333     if (acpi_timer_timecounter.tc_frequency == 0)
334 	return (EOPNOTSUPP);
335     freq = acpi_timer_frequency;
336     error = sysctl_handle_int(oidp, &freq, 0, req);
337     if (error == 0 && req->newptr != NULL) {
338 	acpi_timer_frequency = freq;
339 	acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
340     }
341 
342     return (error);
343 }
344 
345 SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq,
346     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
347     acpi_timer_sysctl_freq, "I",
348     "ACPI timer frequency");
349