Deleted Added
full compact
acpi_timer.c (128528) acpi_timer.c (128543)
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:

--- 10 unchanged lines hidden (view full) ---

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 *
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:

--- 10 unchanged lines hidden (view full) ---

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 * $FreeBSD: head/sys/dev/acpica/acpi_timer.c 128528 2004-04-21 18:07:21Z njl $
27 * $FreeBSD: head/sys/dev/acpica/acpi_timer.c 128543 2004-04-22 01:50:08Z njl $
28 */
29#include "opt_acpi.h"
30#include <sys/param.h>
31#include <sys/bus.h>
32#include <sys/kernel.h>
33#include <sys/sysctl.h>
34#include <sys/timetc.h>
35

--- 12 unchanged lines hidden (view full) ---

48 */
49
50/* Hooks for the ACPI CA debugging infrastructure */
51#define _COMPONENT ACPI_TIMER
52ACPI_MODULE_NAME("TIMER")
53
54static device_t acpi_timer_dev;
55static struct resource *acpi_timer_reg;
28 */
29#include "opt_acpi.h"
30#include <sys/param.h>
31#include <sys/bus.h>
32#include <sys/kernel.h>
33#include <sys/sysctl.h>
34#include <sys/timetc.h>
35

--- 12 unchanged lines hidden (view full) ---

48 */
49
50/* Hooks for the ACPI CA debugging infrastructure */
51#define _COMPONENT ACPI_TIMER
52ACPI_MODULE_NAME("TIMER")
53
54static device_t acpi_timer_dev;
55static struct resource *acpi_timer_reg;
56static bus_space_handle_t timer_bsh;
57static bus_space_tag_t timer_bst;
56static bus_space_handle_t acpi_timer_bsh;
57static bus_space_tag_t acpi_timer_bst;
58
59static u_int acpi_timer_frequency = 14318182 / 4;
60
61static void acpi_timer_identify(driver_t *driver, device_t parent);
62static int acpi_timer_probe(device_t dev);
63static int acpi_timer_attach(device_t dev);
64static u_int acpi_timer_get_timecount(struct timecounter *tc);
65static u_int acpi_timer_get_timecount_safe(struct timecounter *tc);
66static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
58
59static u_int acpi_timer_frequency = 14318182 / 4;
60
61static void acpi_timer_identify(driver_t *driver, device_t parent);
62static int acpi_timer_probe(device_t dev);
63static int acpi_timer_attach(device_t dev);
64static u_int acpi_timer_get_timecount(struct timecounter *tc);
65static u_int acpi_timer_get_timecount_safe(struct timecounter *tc);
66static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
67static void acpi_timer_test(void);
67static void acpi_timer_boot_test(void);
68
68
69static u_int read_counter(void);
70static int test_counter(void);
69static u_int acpi_timer_read(void);
70static int acpi_timer_test(void);
71
72static device_method_t acpi_timer_methods[] = {
73 DEVMETHOD(device_identify, acpi_timer_identify),
74 DEVMETHOD(device_probe, acpi_timer_probe),
75 DEVMETHOD(device_attach, acpi_timer_attach),
76
77 {0, 0}
78};

--- 4 unchanged lines hidden (view full) ---

83 0,
84};
85
86static devclass_t acpi_timer_devclass;
87DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
88MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
89
90static struct timecounter acpi_timer_timecounter = {
71
72static device_method_t acpi_timer_methods[] = {
73 DEVMETHOD(device_identify, acpi_timer_identify),
74 DEVMETHOD(device_probe, acpi_timer_probe),
75 DEVMETHOD(device_attach, acpi_timer_attach),
76
77 {0, 0}
78};

--- 4 unchanged lines hidden (view full) ---

83 0,
84};
85
86static devclass_t acpi_timer_devclass;
87DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
88MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
89
90static struct timecounter acpi_timer_timecounter = {
91 .tc_get_timecount = acpi_timer_get_timecount_safe,
92 .tc_poll_pps = 0,
93 .tc_counter_mask = 0,
94 .tc_frequency = 0,
95 .tc_name = "ACPI",
96 .tc_quality = 1000
91 acpi_timer_get_timecount_safe, /* get_timecount function */
92 0, /* no poll_pps */
93 0, /* no default counter_mask */
94 0, /* no default frequency */
95 "ACPI", /* name */
96 1000 /* quality */
97};
98
99static u_int
97};
98
99static u_int
100read_counter()
100acpi_timer_read()
101{
102 uint32_t tv;
103
101{
102 uint32_t tv;
103
104 tv = bus_space_read_4(timer_bst, timer_bsh, 0);
105 bus_space_barrier(timer_bst, timer_bsh, 0, 4, BUS_SPACE_BARRIER_READ);
104 tv = bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0);
105 bus_space_barrier(acpi_timer_bst, acpi_timer_bsh, 0, 4,
106 BUS_SPACE_BARRIER_READ);
106 return (tv);
107}
108
109/*
110 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
111 * we will be using.
112 */
113static void

--- 22 unchanged lines hidden (view full) ---

136 rstart = AcpiGbl_FADT->XPmTmrBlk.Address;
137 bus_set_resource(dev, rtype, rid, rstart, rlen);
138 acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
139 if (acpi_timer_reg == NULL) {
140 device_printf(dev, "couldn't allocate I/O resource (%s 0x%lx)\n",
141 rtype == SYS_RES_IOPORT ? "port" : "mem", rstart);
142 return_VOID;
143 }
107 return (tv);
108}
109
110/*
111 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
112 * we will be using.
113 */
114static void

--- 22 unchanged lines hidden (view full) ---

137 rstart = AcpiGbl_FADT->XPmTmrBlk.Address;
138 bus_set_resource(dev, rtype, rid, rstart, rlen);
139 acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
140 if (acpi_timer_reg == NULL) {
141 device_printf(dev, "couldn't allocate I/O resource (%s 0x%lx)\n",
142 rtype == SYS_RES_IOPORT ? "port" : "mem", rstart);
143 return_VOID;
144 }
144 timer_bsh = rman_get_bushandle(acpi_timer_reg);
145 timer_bst = rman_get_bustag(acpi_timer_reg);
145 acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
146 acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
146 if (AcpiGbl_FADT->TmrValExt != 0)
147 acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
148 else
149 acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
150 acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
151 if (testenv("debug.acpi.timer_test"))
147 if (AcpiGbl_FADT->TmrValExt != 0)
148 acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
149 else
150 acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
151 acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
152 if (testenv("debug.acpi.timer_test"))
152 acpi_timer_test();
153 acpi_timer_boot_test();
153
154 /*
155 * If all tests of the counter succeed, use the ACPI-fast method. If
156 * at least one failed, default to using the safe routine, which reads
157 * the timer multiple times to get a consistent value before returning.
158 */
159 j = 0;
160 for (i = 0; i < 10; i++)
154
155 /*
156 * If all tests of the counter succeed, use the ACPI-fast method. If
157 * at least one failed, default to using the safe routine, which reads
158 * the timer multiple times to get a consistent value before returning.
159 */
160 j = 0;
161 for (i = 0; i < 10; i++)
161 j += test_counter();
162 j += acpi_timer_test();
162 if (j == 10) {
163 acpi_timer_timecounter.tc_name = "ACPI-fast";
164 acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
165 } else {
166 acpi_timer_timecounter.tc_name = "ACPI-safe";
167 acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
168 }
169 tc_init(&acpi_timer_timecounter);

--- 21 unchanged lines hidden (view full) ---

191}
192
193/*
194 * Fetch current time value from reliable hardware.
195 */
196static u_int
197acpi_timer_get_timecount(struct timecounter *tc)
198{
163 if (j == 10) {
164 acpi_timer_timecounter.tc_name = "ACPI-fast";
165 acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
166 } else {
167 acpi_timer_timecounter.tc_name = "ACPI-safe";
168 acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
169 }
170 tc_init(&acpi_timer_timecounter);

--- 21 unchanged lines hidden (view full) ---

192}
193
194/*
195 * Fetch current time value from reliable hardware.
196 */
197static u_int
198acpi_timer_get_timecount(struct timecounter *tc)
199{
199 return (read_counter());
200 return (acpi_timer_read());
200}
201
202/*
203 * Fetch current time value from hardware that may not correctly
204 * latch the counter. We need to read until we have three monotonic
205 * samples and then use the middle one, otherwise we are not protected
206 * against the fact that the bits can be wrong in two directions. If
207 * we only cared about monosity, two reads would be enough.
208 */
209static u_int
210acpi_timer_get_timecount_safe(struct timecounter *tc)
211{
212 u_int u1, u2, u3;
213
201}
202
203/*
204 * Fetch current time value from hardware that may not correctly
205 * latch the counter. We need to read until we have three monotonic
206 * samples and then use the middle one, otherwise we are not protected
207 * against the fact that the bits can be wrong in two directions. If
208 * we only cared about monosity, two reads would be enough.
209 */
210static u_int
211acpi_timer_get_timecount_safe(struct timecounter *tc)
212{
213 u_int u1, u2, u3;
214
214 u2 = read_counter();
215 u3 = read_counter();
215 u2 = acpi_timer_read();
216 u3 = acpi_timer_read();
216 do {
217 u1 = u2;
218 u2 = u3;
217 do {
218 u1 = u2;
219 u2 = u3;
219 u3 = read_counter();
220 } while (u1 > u2 || u2 > u3 || u3 - u1 > 15);
220 u3 = acpi_timer_read();
221 } while (u1 > u2 || u2 > u3);
221
222 return (u2);
223}
224
225/*
226 * Timecounter freqency adjustment interface.
227 */
228static int

--- 38 unchanged lines hidden (view full) ---

267 * ] software for time critical events and delays.
268 * ]
269 * ] Workaround: Read the register twice and compare.
270 * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
271 * ] in the PIIX4M.
272 */
273#define N 2000
274static int
222
223 return (u2);
224}
225
226/*
227 * Timecounter freqency adjustment interface.
228 */
229static int

--- 38 unchanged lines hidden (view full) ---

268 * ] software for time critical events and delays.
269 * ]
270 * ] Workaround: Read the register twice and compare.
271 * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
272 * ] in the PIIX4M.
273 */
274#define N 2000
275static int
275test_counter()
276acpi_timer_test()
276{
277 uint32_t last, this;
278 int min, max, n, delta;
279
280 min = 10000000;
281 max = 0;
277{
278 uint32_t last, this;
279 int min, max, n, delta;
280
281 min = 10000000;
282 max = 0;
282 last = read_counter();
283 last = acpi_timer_read();
283 for (n = 0; n < N; n++) {
284 for (n = 0; n < N; n++) {
284 this = read_counter();
285 this = acpi_timer_read();
285 delta = acpi_TimerDelta(this, last);
286 if (delta > max)
287 max = delta;
288 else if (delta < min)
289 min = delta;
290 last = this;
291 }
292 if (max - min > 2)

--- 12 unchanged lines hidden (view full) ---

305}
306#undef N
307
308/*
309 * Test harness for verifying ACPI timer behaviour.
310 * Boot with debug.acpi.timer_test set to invoke this.
311 */
312static void
286 delta = acpi_TimerDelta(this, last);
287 if (delta > max)
288 max = delta;
289 else if (delta < min)
290 min = delta;
291 last = this;
292 }
293 if (max - min > 2)

--- 12 unchanged lines hidden (view full) ---

306}
307#undef N
308
309/*
310 * Test harness for verifying ACPI timer behaviour.
311 * Boot with debug.acpi.timer_test set to invoke this.
312 */
313static void
313acpi_timer_test(void)
314acpi_timer_boot_test(void)
314{
315 uint32_t u1, u2, u3;
316
315{
316 uint32_t u1, u2, u3;
317
317 u1 = read_counter();
318 u2 = read_counter();
319 u3 = read_counter();
318 u1 = acpi_timer_read();
319 u2 = acpi_timer_read();
320 u3 = acpi_timer_read();
320
321 device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
322 for (;;) {
323 /*
324 * The failure case is where u3 > u1, but u2 does not fall between
325 * the two, ie. it contains garbage.
326 */
327 if (u3 > u1) {
328 if (u2 < u1 || u2 > u3)
329 device_printf(acpi_timer_dev,
330 "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
331 u1, u2, u3);
332 }
333 u1 = u2;
334 u2 = u3;
321
322 device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
323 for (;;) {
324 /*
325 * The failure case is where u3 > u1, but u2 does not fall between
326 * the two, ie. it contains garbage.
327 */
328 if (u3 > u1) {
329 if (u2 < u1 || u2 > u3)
330 device_printf(acpi_timer_dev,
331 "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
332 u1, u2, u3);
333 }
334 u1 = u2;
335 u2 = u3;
335 u3 = read_counter();
336 u3 = acpi_timer_read();
336 }
337}
337 }
338}