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