1 /*-
2 * Copyright (c) 2005 Poul-Henning Kamp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: releng/7.4/sys/dev/acpica/acpi_hpet.c 204481 2010-02-28 21:23:50Z avg $");
29
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/rman.h>
36 #include <sys/time.h>
37 #include <sys/timetc.h>
38
39 #include <contrib/dev/acpica/acpi.h>
40 #include <dev/acpica/acpivar.h>
41 #include <dev/acpica/acpi_hpet.h>
42
43 #define HPET_VENDID_AMD 0x4353
44 #define HPET_VENDID_INTEL 0x8086
45
46 ACPI_SERIAL_DECL(hpet, "ACPI HPET support");
47
48 static devclass_t acpi_hpet_devclass;
49
50 /* ACPI CA debugging */
51 #define _COMPONENT ACPI_TIMER
52 ACPI_MODULE_NAME("HPET")
53
54 struct acpi_hpet_softc {
55 device_t dev;
56 struct resource *mem_res;
57 ACPI_HANDLE handle;
58 };
59
60 static u_int hpet_get_timecount(struct timecounter *tc);
61 static void acpi_hpet_test(struct acpi_hpet_softc *sc);
62
63 static char *hpet_ids[] = { "PNP0103", NULL };
64
65 struct timecounter hpet_timecounter = {
66 .tc_get_timecount = hpet_get_timecount,
67 .tc_counter_mask = ~0u,
68 .tc_name = "HPET",
69 .tc_quality = 900,
70 };
71
72 static u_int
73 hpet_get_timecount(struct timecounter *tc)
74 {
75 struct acpi_hpet_softc *sc;
76
77 sc = tc->tc_priv;
78 return (bus_read_4(sc->mem_res, HPET_MAIN_COUNTER));
79 }
80
81 static void
82 hpet_enable(struct acpi_hpet_softc *sc)
83 {
84 uint32_t val;
85
86 val = bus_read_4(sc->mem_res, HPET_CONFIG);
87 val &= ~HPET_CNF_LEG_RT;
88 val |= HPET_CNF_ENABLE;
89 bus_write_4(sc->mem_res, HPET_CONFIG, val);
90 }
91
92 static void
93 hpet_disable(struct acpi_hpet_softc *sc)
94 {
95 uint32_t val;
96
97 val = bus_read_4(sc->mem_res, HPET_CONFIG);
98 val &= ~HPET_CNF_ENABLE;
99 bus_write_4(sc->mem_res, HPET_CONFIG, val);
100 }
101
102 /* Discover the HPET via the ACPI table of the same name. */
103 static void
104 acpi_hpet_identify(driver_t *driver, device_t parent)
105 {
106 ACPI_TABLE_HPET *hpet;
107 ACPI_TABLE_HEADER *hdr;
108 ACPI_STATUS status;
109 device_t child;
110
111 /* Only one HPET device can be added. */
112 if (devclass_get_device(acpi_hpet_devclass, 0))
113 return;
114
115 /* Currently, ID and minimum clock tick info is unused. */
116
117 status = AcpiGetTable(ACPI_SIG_HPET, 1, (ACPI_TABLE_HEADER **)&hdr);
118 if (ACPI_FAILURE(status))
119 return;
120
121 /*
122 * The unit number could be derived from hdr->Sequence but we only
123 * support one HPET device.
124 */
125 hpet = (ACPI_TABLE_HPET *)hdr;
126 if (hpet->Sequence != 0)
127 printf("ACPI HPET table warning: Sequence is non-zero (%d)\n",
128 hpet->Sequence);
129 child = BUS_ADD_CHILD(parent, ACPI_DEV_BASE_ORDER, "acpi_hpet", 0);
130 if (child == NULL) {
131 printf("%s: can't add child\n", __func__);
132 return;
133 }
134
135 bus_set_resource(child, SYS_RES_MEMORY, 0, hpet->Address.Address,
136 HPET_MEM_WIDTH);
137 }
138
139 static int
140 acpi_hpet_probe(device_t dev)
141 {
142 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
143
144 if (acpi_disabled("hpet"))
145 return (ENXIO);
146 if (acpi_get_handle(dev) != NULL &&
147 (ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL ||
148 device_get_unit(dev) != 0))
149 return (ENXIO);
150
151 device_set_desc(dev, "High Precision Event Timer");
152 return (0);
153 }
154
155 static int
156 acpi_hpet_attach(device_t dev)
157 {
158 struct acpi_hpet_softc *sc;
159 int rid, num_timers;
160 uint32_t val, val2;
161 uintmax_t freq;
162 uint16_t vendor;
163
164 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
165
166 sc = device_get_softc(dev);
167 sc->dev = dev;
168 sc->handle = acpi_get_handle(dev);
169
170 rid = 0;
171 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
172 RF_ACTIVE);
173 if (sc->mem_res == NULL)
174 return (ENOMEM);
175
176 /* Validate that we can access the whole region. */
177 if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH) {
178 device_printf(dev, "memory region width %ld too small\n",
179 rman_get_size(sc->mem_res));
180 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
181 return (ENXIO);
182 }
183
184 /* Be sure timer is enabled. */
185 hpet_enable(sc);
186
187 /* Read basic statistics about the timer. */
188 val = bus_read_4(sc->mem_res, HPET_PERIOD);
189 if (val == 0) {
190 device_printf(dev, "invalid period\n");
191 hpet_disable(sc);
192 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
193 return (ENXIO);
194 }
195
196 freq = (1000000000000000LL + val / 2) / val;
197 if (bootverbose) {
198 val = bus_read_4(sc->mem_res, HPET_CAPABILITIES);
199
200 /*
201 * ATI/AMD violates IA-PC HPET (High Precision Event Timers)
202 * Specification and provides an off by one number
203 * of timers/comparators.
204 * Additionally, they use unregistered value in VENDOR_ID field.
205 */
206 num_timers = 1 + ((val & HPET_CAP_NUM_TIM) >> 8);
207 vendor = val >> 16;
208 if (vendor == HPET_VENDID_AMD && num_timers > 0)
209 num_timers--;
210 device_printf(dev,
211 "vend: 0x%x rev: 0x%x num: %d hz: %jd opts:%s%s\n",
212 vendor, val & HPET_CAP_REV_ID,
213 num_timers, freq,
214 (val & HPET_CAP_LEG_RT) ? " legacy_route" : "",
215 (val & HPET_CAP_COUNT_SIZE) ? " 64-bit" : "");
216 }
217
218 if (testenv("debug.acpi.hpet_test"))
219 acpi_hpet_test(sc);
220
221 /*
222 * Don't attach if the timer never increments. Since the spec
223 * requires it to be at least 10 MHz, it has to change in 1 us.
224 */
225 val = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
226 DELAY(1);
227 val2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
228 if (val == val2) {
229 device_printf(dev, "HPET never increments, disabling\n");
230 hpet_disable(sc);
231 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
232 return (ENXIO);
233 }
234
235 hpet_timecounter.tc_frequency = freq;
236 hpet_timecounter.tc_priv = sc;
237 tc_init(&hpet_timecounter);
238
239 return (0);
240 }
241
242 static int
243 acpi_hpet_detach(device_t dev)
244 {
245 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
246
247 /* XXX Without a tc_remove() function, we can't detach. */
248 return (EBUSY);
249 }
250
251 static int
252 acpi_hpet_suspend(device_t dev)
253 {
254 struct acpi_hpet_softc *sc;
255
256 /*
257 * Disable the timer during suspend. The timer will not lose
258 * its state in S1 or S2, but we are required to disable
259 * it.
260 */
261 sc = device_get_softc(dev);
262 hpet_disable(sc);
263
264 return (0);
265 }
266
267 static int
268 acpi_hpet_resume(device_t dev)
269 {
270 struct acpi_hpet_softc *sc;
271
272 /* Re-enable the timer after a resume to keep the clock advancing. */
273 sc = device_get_softc(dev);
274 hpet_enable(sc);
275
276 return (0);
277 }
278
279 /* Print some basic latency/rate information to assist in debugging. */
280 static void
281 acpi_hpet_test(struct acpi_hpet_softc *sc)
282 {
283 int i;
284 uint32_t u1, u2;
285 struct bintime b0, b1, b2;
286 struct timespec ts;
287
288 binuptime(&b0);
289 binuptime(&b0);
290 binuptime(&b1);
291 u1 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
292 for (i = 1; i < 1000; i++)
293 u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
294 binuptime(&b2);
295 u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
296
297 bintime_sub(&b2, &b1);
298 bintime_sub(&b1, &b0);
299 bintime_sub(&b2, &b1);
300 bintime2timespec(&b2, &ts);
301
302 device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
303 (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1);
304
305 device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);
306 }
307
308 static device_method_t acpi_hpet_methods[] = {
309 /* Device interface */
310 DEVMETHOD(device_identify, acpi_hpet_identify),
311 DEVMETHOD(device_probe, acpi_hpet_probe),
312 DEVMETHOD(device_attach, acpi_hpet_attach),
313 DEVMETHOD(device_detach, acpi_hpet_detach),
314 DEVMETHOD(device_suspend, acpi_hpet_suspend),
315 DEVMETHOD(device_resume, acpi_hpet_resume),
316
317 {0, 0}
318 };
319
320 static driver_t acpi_hpet_driver = {
321 "acpi_hpet",
322 acpi_hpet_methods,
323 sizeof(struct acpi_hpet_softc),
324 };
325
326
327 DRIVER_MODULE(acpi_hpet, acpi, acpi_hpet_driver, acpi_hpet_devclass, 0, 0);
328 MODULE_DEPEND(acpi_hpet, acpi, 1, 1, 1);
Cache object: 9806dbd7678079173105ec6e8010be49
|