acpi_tz.c revision 1.68 1 1.68 jruoho /* $NetBSD: acpi_tz.c,v 1.68 2010/04/24 06:31:44 jruoho Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*
4 1.1 jmcneill * Copyright (c) 2003 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. The name of the author may not be used to endorse or promote products
13 1.1 jmcneill * derived from this software without specific prior written permission.
14 1.1 jmcneill *
15 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 jmcneill * SUCH DAMAGE.
26 1.1 jmcneill */
27 1.1 jmcneill
28 1.1 jmcneill /*
29 1.1 jmcneill * ACPI Thermal Zone driver
30 1.1 jmcneill */
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/cdefs.h>
33 1.68 jruoho __KERNEL_RCSID(0, "$NetBSD: acpi_tz.c,v 1.68 2010/04/24 06:31:44 jruoho Exp $");
34 1.1 jmcneill
35 1.1 jmcneill #include <sys/param.h>
36 1.1 jmcneill #include <sys/device.h>
37 1.1 jmcneill #include <sys/callout.h>
38 1.61 jruoho #include <sys/kernel.h>
39 1.61 jruoho #include <sys/systm.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <dev/acpi/acpireg.h>
42 1.1 jmcneill #include <dev/acpi/acpivar.h>
43 1.67 jruoho #include <dev/acpi/acpi_power.h>
44 1.1 jmcneill
45 1.47 mlelstv #define _COMPONENT ACPI_TZ_COMPONENT
46 1.47 mlelstv ACPI_MODULE_NAME ("acpi_tz")
47 1.47 mlelstv
48 1.1 jmcneill /* flags */
49 1.1 jmcneill #define ATZ_F_VERBOSE 0x01 /* show events to console */
50 1.13 kochi #define ATZ_F_CRITICAL 0x02 /* zone critical */
51 1.13 kochi #define ATZ_F_HOT 0x04 /* zone hot */
52 1.13 kochi #define ATZ_F_PASSIVE 0x08 /* zone passive cooling */
53 1.13 kochi #define ATZ_F_PASSIVEONLY 0x10 /* zone is passive cooling only */
54 1.13 kochi
55 1.13 kochi /* no active cooling level */
56 1.13 kochi #define ATZ_ACTIVE_NONE -1
57 1.1 jmcneill
58 1.1 jmcneill /* constants */
59 1.1 jmcneill #define ATZ_TZP_RATE 300 /* default if no _TZP CM present (30 secs) */
60 1.1 jmcneill #define ATZ_NLEVELS 10 /* number of cooling levels, from ACPI spec */
61 1.60 pgoyette #define ATZ_ZEROC 2732 /* 0C, measured in 0.1 Kelvin */
62 1.13 kochi #define ATZ_TMP_INVALID 0xffffffff /* invalid temperature */
63 1.13 kochi #define ATZ_ZONE_EXPIRE 9000 /* zone info refetch interval (15min) */
64 1.1 jmcneill
65 1.1 jmcneill /* sensor indexes */
66 1.1 jmcneill #define ATZ_SENSOR_TEMP 0 /* thermal zone temperature */
67 1.1 jmcneill
68 1.1 jmcneill /*
69 1.1 jmcneill * ACPI Temperature Zone information. Note all temperatures are reported
70 1.60 pgoyette * in 0.1 Kelvin, and that the ACPI specification assumes that
71 1.60 pgoyette * K = C + 273.2 rather than the nominal 273.15 used by envsys(4).
72 1.45 pgoyette * So define an appropriate conversion.
73 1.1 jmcneill */
74 1.45 pgoyette
75 1.45 pgoyette #define ATZ2UKELVIN(t) ((t) * 100000 - 50000)
76 1.45 pgoyette
77 1.1 jmcneill struct acpitz_zone {
78 1.1 jmcneill /* Active cooling temperature threshold */
79 1.64 jruoho uint32_t ac[ATZ_NLEVELS];
80 1.1 jmcneill /* Package of references to all active cooling devices for a level */
81 1.1 jmcneill ACPI_BUFFER al[ATZ_NLEVELS];
82 1.1 jmcneill /* Critical temperature threshold for system shutdown */
83 1.64 jruoho uint32_t crt;
84 1.1 jmcneill /* Critical temperature threshold for S4 sleep */
85 1.64 jruoho uint32_t hot;
86 1.1 jmcneill /* Package of references to processor objects for passive cooling */
87 1.1 jmcneill ACPI_BUFFER psl;
88 1.48 cegger /* Conveys if temperatures are absolute or relative values. */
89 1.64 jruoho uint32_t rtv;
90 1.1 jmcneill /* Passive cooling temperature threshold */
91 1.64 jruoho uint32_t psv;
92 1.1 jmcneill /* Thermal constants for use in passive cooling formulas */
93 1.64 jruoho uint32_t tc1, tc2;
94 1.1 jmcneill /* Current temperature of the thermal zone */
95 1.64 jruoho uint32_t prevtmp, tmp;
96 1.1 jmcneill /* Thermal sampling period for passive cooling, in tenths of seconds */
97 1.64 jruoho uint32_t tsp;
98 1.1 jmcneill /* Package of references to devices in this TZ (optional) */
99 1.1 jmcneill ACPI_BUFFER tzd;
100 1.1 jmcneill /* Recommended TZ polling frequency, in tenths of seconds */
101 1.64 jruoho uint32_t tzp;
102 1.49 cegger /* Thermal zone name */
103 1.49 cegger char *name;
104 1.49 cegger /* FAN min, max, current rpms */
105 1.64 jruoho uint32_t fanmin, fanmax, fancurrent;
106 1.1 jmcneill };
107 1.1 jmcneill
108 1.1 jmcneill struct acpitz_softc {
109 1.1 jmcneill struct acpi_devnode *sc_devnode;
110 1.1 jmcneill struct acpitz_zone sc_zone;
111 1.1 jmcneill struct callout sc_callout;
112 1.27 xtraeme struct sysmon_envsys *sc_sme;
113 1.49 cegger envsys_data_t sc_temp_sensor;
114 1.49 cegger envsys_data_t sc_fan_sensor;
115 1.13 kochi int sc_active; /* active cooling level */
116 1.1 jmcneill int sc_flags;
117 1.1 jmcneill int sc_rate; /* tz poll rate */
118 1.13 kochi int sc_zone_expire;
119 1.29 jmcneill
120 1.29 jmcneill int sc_first;
121 1.49 cegger int sc_have_fan; /* FAN sensor is optional */
122 1.1 jmcneill };
123 1.1 jmcneill
124 1.68 jruoho static int acpitz_match(device_t, cfdata_t, void *);
125 1.68 jruoho static void acpitz_attach(device_t, device_t, void *);
126 1.68 jruoho static int acpitz_detach(device_t, int);
127 1.68 jruoho
128 1.11 kochi static void acpitz_get_status(void *);
129 1.14 martin static void acpitz_get_zone(void *, int);
130 1.14 martin static void acpitz_get_zone_quiet(void *);
131 1.33 xtraeme static char *acpitz_celcius_string(int);
132 1.33 xtraeme static void acpitz_print_status(device_t);
133 1.13 kochi static void acpitz_power_off(struct acpitz_softc *);
134 1.13 kochi static void acpitz_power_zone(struct acpitz_softc *, int, int);
135 1.64 jruoho static void acpitz_sane_temp(uint32_t *tmp);
136 1.13 kochi static ACPI_STATUS
137 1.13 kochi acpitz_switch_cooler(ACPI_OBJECT *, void *);
138 1.64 jruoho static void acpitz_notify_handler(ACPI_HANDLE, uint32_t, void *);
139 1.64 jruoho static int acpitz_get_integer(device_t, const char *, uint32_t *);
140 1.1 jmcneill static void acpitz_tick(void *);
141 1.33 xtraeme static void acpitz_init_envsys(device_t);
142 1.42 pgoyette static void acpitz_get_limits(struct sysmon_envsys *, envsys_data_t *,
143 1.58 pgoyette sysmon_envsys_lim_t *, uint32_t *);
144 1.64 jruoho static int acpitz_get_fanspeed(device_t, uint32_t *,
145 1.64 jruoho uint32_t *, uint32_t *);
146 1.49 cegger #ifdef notyet
147 1.49 cegger static ACPI_STATUS
148 1.64 jruoho acpitz_set_fanspeed(device_t, uint32_t);
149 1.49 cegger #endif
150 1.1 jmcneill
151 1.68 jruoho CFATTACH_DECL_NEW(acpitz, sizeof(struct acpitz_softc),
152 1.68 jruoho acpitz_match, acpitz_attach, acpitz_detach, NULL);
153 1.1 jmcneill
154 1.1 jmcneill /*
155 1.1 jmcneill * acpitz_match: autoconf(9) match routine
156 1.1 jmcneill */
157 1.11 kochi static int
158 1.33 xtraeme acpitz_match(device_t parent, cfdata_t match, void *aux)
159 1.1 jmcneill {
160 1.1 jmcneill struct acpi_attach_args *aa = aux;
161 1.1 jmcneill
162 1.1 jmcneill if (aa->aa_node->ad_type != ACPI_TYPE_THERMAL)
163 1.1 jmcneill return 0;
164 1.1 jmcneill
165 1.1 jmcneill return 1;
166 1.1 jmcneill }
167 1.1 jmcneill
168 1.1 jmcneill /*
169 1.1 jmcneill * acpitz_attach: autoconf(9) attach routine
170 1.1 jmcneill */
171 1.11 kochi static void
172 1.33 xtraeme acpitz_attach(device_t parent, device_t self, void *aux)
173 1.1 jmcneill {
174 1.33 xtraeme struct acpitz_softc *sc = device_private(self);
175 1.1 jmcneill struct acpi_attach_args *aa = aux;
176 1.1 jmcneill ACPI_STATUS rv;
177 1.9 martin ACPI_INTEGER v;
178 1.1 jmcneill
179 1.1 jmcneill #if 0
180 1.1 jmcneill sc->sc_flags = ATZ_F_VERBOSE;
181 1.1 jmcneill #endif
182 1.1 jmcneill sc->sc_devnode = aa->aa_node;
183 1.1 jmcneill
184 1.29 jmcneill aprint_naive("\n");
185 1.63 jruoho aprint_normal(": ACPI Thermal Zone\n");
186 1.1 jmcneill
187 1.9 martin rv = acpi_eval_integer(sc->sc_devnode->ad_handle, "_TZP", &v);
188 1.31 jmcneill if (ACPI_FAILURE(rv))
189 1.1 jmcneill sc->sc_zone.tzp = ATZ_TZP_RATE;
190 1.31 jmcneill else
191 1.9 martin sc->sc_zone.tzp = v;
192 1.31 jmcneill
193 1.63 jruoho aprint_debug_dev(self, "sample rate %d.%ds\n",
194 1.31 jmcneill sc->sc_zone.tzp / 10, sc->sc_zone.tzp % 10);
195 1.3 mycroft
196 1.1 jmcneill /* XXX a value of 0 means "polling is not necessary" */
197 1.1 jmcneill if (sc->sc_zone.tzp == 0)
198 1.1 jmcneill sc->sc_zone.tzp = ATZ_TZP_RATE;
199 1.10 kochi
200 1.13 kochi sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp;
201 1.29 jmcneill sc->sc_first = 1;
202 1.49 cegger sc->sc_have_fan = 0;
203 1.49 cegger if (acpitz_get_fanspeed(self,
204 1.49 cegger &sc->sc_zone.fanmin, &sc->sc_zone.fanmax, &sc->sc_zone.fancurrent)
205 1.49 cegger == 0)
206 1.49 cegger sc->sc_have_fan = 1;
207 1.13 kochi
208 1.49 cegger rv = acpi_eval_string(sc->sc_devnode->ad_handle,
209 1.49 cegger "REGN", &sc->sc_zone.name);
210 1.68 jruoho
211 1.49 cegger if (ACPI_FAILURE(rv))
212 1.49 cegger sc->sc_zone.name = __UNCONST("temperature");
213 1.63 jruoho
214 1.33 xtraeme acpitz_get_zone(self, 1);
215 1.33 xtraeme acpitz_get_status(self);
216 1.1 jmcneill
217 1.68 jruoho (void)pmf_device_register(self, NULL, NULL);
218 1.65 jruoho (void)acpi_register_notify(sc->sc_devnode, acpitz_notify_handler);
219 1.1 jmcneill
220 1.33 xtraeme callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
221 1.33 xtraeme callout_setfunc(&sc->sc_callout, acpitz_tick, self);
222 1.1 jmcneill
223 1.33 xtraeme acpitz_init_envsys(self);
224 1.25 joerg
225 1.68 jruoho callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
226 1.68 jruoho }
227 1.68 jruoho
228 1.68 jruoho static int
229 1.68 jruoho acpitz_detach(device_t self, int flags)
230 1.68 jruoho {
231 1.68 jruoho struct acpitz_softc *sc = device_private(self);
232 1.68 jruoho ACPI_HANDLE hdl;
233 1.68 jruoho ACPI_BUFFER al;
234 1.68 jruoho ACPI_STATUS rv;
235 1.68 jruoho int i;
236 1.68 jruoho
237 1.68 jruoho callout_halt(&sc->sc_callout, NULL);
238 1.68 jruoho callout_destroy(&sc->sc_callout);
239 1.68 jruoho
240 1.68 jruoho pmf_device_deregister(self);
241 1.68 jruoho acpi_deregister_notify(sc->sc_devnode);
242 1.68 jruoho
243 1.68 jruoho /*
244 1.68 jruoho * Although the device itself should not contain any power
245 1.68 jruoho * resources, we have possibly used the resources of active
246 1.68 jruoho * cooling devices. To unregister these, first fetch a fresh
247 1.68 jruoho * active cooling zone, and then detach the resources from
248 1.68 jruoho * the reference handles contained in the cooling zone.
249 1.68 jruoho */
250 1.68 jruoho acpitz_get_zone(self, 0);
251 1.68 jruoho
252 1.68 jruoho for (i = 0; i < ATZ_NLEVELS; i++) {
253 1.68 jruoho
254 1.68 jruoho if (sc->sc_zone.al[i].Pointer == NULL)
255 1.68 jruoho continue;
256 1.68 jruoho
257 1.68 jruoho al = sc->sc_zone.al[i];
258 1.68 jruoho rv = acpi_eval_reference_handle(al.Pointer, &hdl);
259 1.68 jruoho
260 1.68 jruoho if (ACPI_SUCCESS(rv))
261 1.68 jruoho acpi_power_deregister_from_handle(hdl);
262 1.68 jruoho
263 1.68 jruoho ACPI_FREE(sc->sc_zone.al[i].Pointer);
264 1.68 jruoho }
265 1.32 jmcneill
266 1.68 jruoho if (sc->sc_sme != NULL)
267 1.68 jruoho sysmon_envsys_unregister(sc->sc_sme);
268 1.68 jruoho
269 1.68 jruoho return 0;
270 1.1 jmcneill }
271 1.1 jmcneill
272 1.11 kochi static void
273 1.14 martin acpitz_get_zone_quiet(void *opaque)
274 1.14 martin {
275 1.14 martin acpitz_get_zone(opaque, 0);
276 1.14 martin }
277 1.14 martin
278 1.14 martin static void
279 1.1 jmcneill acpitz_get_status(void *opaque)
280 1.1 jmcneill {
281 1.33 xtraeme device_t dv = opaque;
282 1.33 xtraeme struct acpitz_softc *sc = device_private(dv);
283 1.64 jruoho uint32_t tmp, active, fmin, fmax, fcurrent;
284 1.13 kochi int i, flags;
285 1.13 kochi
286 1.13 kochi sc->sc_zone_expire--;
287 1.13 kochi if (sc->sc_zone_expire <= 0) {
288 1.13 kochi sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp;
289 1.13 kochi if (sc->sc_flags & ATZ_F_VERBOSE)
290 1.33 xtraeme printf("%s: force refetch zone\n", device_xname(dv));
291 1.34 xtraeme acpitz_get_zone(dv, 0);
292 1.13 kochi }
293 1.3 mycroft
294 1.63 jruoho if (acpitz_get_integer(dv, "_TMP", &tmp) != 0)
295 1.3 mycroft return;
296 1.49 cegger
297 1.49 cegger sc->sc_zone.prevtmp = sc->sc_zone.tmp;
298 1.13 kochi sc->sc_zone.tmp = tmp;
299 1.49 cegger if (sc->sc_first)
300 1.49 cegger sc->sc_zone.prevtmp = tmp;
301 1.13 kochi /* XXX sanity check for tmp here? */
302 1.1 jmcneill
303 1.49 cegger if (acpitz_get_fanspeed(dv, &fmin, &fmax, &fcurrent) == 0) {
304 1.49 cegger if (fcurrent != ATZ_TMP_INVALID)
305 1.49 cegger sc->sc_zone.fancurrent = fcurrent;
306 1.49 cegger }
307 1.49 cegger
308 1.7 soren /*
309 1.13 kochi * The temperature unit for envsys(4) is microKelvin, so convert to
310 1.7 soren * that from ACPI's microKelvin. Also, the ACPI specification assumes
311 1.13 kochi * that K = C + 273.2 rather than the nominal 273.15 used by envsys(4),
312 1.7 soren * so we correct for that too.
313 1.7 soren */
314 1.49 cegger sc->sc_temp_sensor.value_cur = ATZ2UKELVIN(sc->sc_zone.tmp);
315 1.49 cegger sc->sc_temp_sensor.state = ENVSYS_SVALID;
316 1.49 cegger
317 1.49 cegger sc->sc_fan_sensor.value_cur = sc->sc_zone.fancurrent;
318 1.49 cegger sc->sc_fan_sensor.state = ENVSYS_SVALID;
319 1.1 jmcneill
320 1.1 jmcneill if (sc->sc_flags & ATZ_F_VERBOSE)
321 1.33 xtraeme acpitz_print_status(dv);
322 1.1 jmcneill
323 1.13 kochi if (sc->sc_flags & ATZ_F_PASSIVEONLY) {
324 1.13 kochi /* Passive Cooling: XXX not yet */
325 1.13 kochi
326 1.13 kochi } else {
327 1.13 kochi /* Active Cooling */
328 1.13 kochi
329 1.13 kochi /* temperature threshold: _AC0 > ... > _AC9 */
330 1.13 kochi active = ATZ_ACTIVE_NONE;
331 1.13 kochi for (i = ATZ_NLEVELS - 1; i >= 0; i--) {
332 1.13 kochi if (sc->sc_zone.ac[i] == ATZ_TMP_INVALID)
333 1.13 kochi continue;
334 1.13 kochi
335 1.13 kochi /* we want to keep highest cooling mode in 'active' */
336 1.13 kochi if (sc->sc_zone.ac[i] <= tmp)
337 1.13 kochi active = i;
338 1.13 kochi }
339 1.13 kochi
340 1.33 xtraeme flags = sc->sc_flags &
341 1.33 xtraeme ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
342 1.13 kochi if (sc->sc_zone.psv != ATZ_TMP_INVALID &&
343 1.13 kochi tmp >= sc->sc_zone.psv)
344 1.13 kochi flags |= ATZ_F_PASSIVE;
345 1.13 kochi if (sc->sc_zone.hot != ATZ_TMP_INVALID &&
346 1.13 kochi tmp >= sc->sc_zone.hot)
347 1.13 kochi flags |= ATZ_F_HOT;
348 1.13 kochi if (sc->sc_zone.crt != ATZ_TMP_INVALID &&
349 1.13 kochi tmp >= sc->sc_zone.crt)
350 1.13 kochi flags |= ATZ_F_CRITICAL;
351 1.13 kochi
352 1.13 kochi if (flags != sc->sc_flags) {
353 1.13 kochi int changed = (sc->sc_flags ^ flags) & flags;
354 1.13 kochi sc->sc_flags = flags;
355 1.22 xtraeme if (changed & ATZ_F_CRITICAL) {
356 1.49 cegger sc->sc_temp_sensor.state = ENVSYS_SCRITOVER;
357 1.39 pgoyette aprint_debug_dev(dv,
358 1.33 xtraeme "zone went critical at temp %sC\n",
359 1.13 kochi acpitz_celcius_string(tmp));
360 1.22 xtraeme } else if (changed & ATZ_F_HOT) {
361 1.49 cegger sc->sc_temp_sensor.state = ENVSYS_SCRITOVER;
362 1.39 pgoyette aprint_debug_dev(dv,
363 1.33 xtraeme "zone went hot at temp %sC\n",
364 1.13 kochi acpitz_celcius_string(tmp));
365 1.22 xtraeme }
366 1.13 kochi }
367 1.13 kochi
368 1.13 kochi /* power on fans */
369 1.13 kochi if (sc->sc_active != active) {
370 1.13 kochi if (sc->sc_active != ATZ_ACTIVE_NONE)
371 1.13 kochi acpitz_power_zone(sc, sc->sc_active, 0);
372 1.13 kochi
373 1.13 kochi if (active != ATZ_ACTIVE_NONE) {
374 1.13 kochi if (sc->sc_flags & ATZ_F_VERBOSE)
375 1.13 kochi printf("%s: active cooling level %u\n",
376 1.33 xtraeme device_xname(dv), active);
377 1.13 kochi acpitz_power_zone(sc, active, 1);
378 1.52 pgoyette } else if (sc->sc_flags & ATZ_F_VERBOSE)
379 1.52 pgoyette printf("%s: no active cooling level\n",
380 1.52 pgoyette device_xname(dv));
381 1.13 kochi
382 1.13 kochi sc->sc_active = active;
383 1.13 kochi }
384 1.13 kochi }
385 1.13 kochi
386 1.1 jmcneill return;
387 1.1 jmcneill }
388 1.1 jmcneill
389 1.13 kochi static char *
390 1.13 kochi acpitz_celcius_string(int dk)
391 1.13 kochi {
392 1.13 kochi static char buf[10];
393 1.59 pgoyette int dc;
394 1.13 kochi
395 1.59 pgoyette dc = abs(dk - ATZ_ZEROC);
396 1.59 pgoyette snprintf(buf, sizeof(buf), "%s%d.%d", (dk >= ATZ_ZEROC)?"":"-",
397 1.59 pgoyette dc / 10, dc % 10);
398 1.13 kochi
399 1.13 kochi return buf;
400 1.13 kochi }
401 1.13 kochi
402 1.1 jmcneill static void
403 1.33 xtraeme acpitz_print_status(device_t dv)
404 1.1 jmcneill {
405 1.33 xtraeme struct acpitz_softc *sc = device_private(dv);
406 1.1 jmcneill
407 1.33 xtraeme printf("%s: zone temperature is now %sC\n", device_xname(dv),
408 1.13 kochi acpitz_celcius_string(sc->sc_zone.tmp));
409 1.49 cegger if (sc->sc_have_fan) {
410 1.49 cegger printf("%s: fan rpm %u\n", device_xname(dv),
411 1.49 cegger sc->sc_zone.fancurrent);
412 1.49 cegger }
413 1.1 jmcneill
414 1.1 jmcneill return;
415 1.1 jmcneill }
416 1.1 jmcneill
417 1.13 kochi static ACPI_STATUS
418 1.13 kochi acpitz_switch_cooler(ACPI_OBJECT *obj, void *arg)
419 1.13 kochi {
420 1.68 jruoho int flag, pwr_state;
421 1.13 kochi ACPI_HANDLE cooler;
422 1.13 kochi ACPI_STATUS rv;
423 1.13 kochi
424 1.68 jruoho /*
425 1.68 jruoho * The _ALx object is a package in which the elements
426 1.68 jruoho * are reference handles to an active cooling device
427 1.68 jruoho * (typically PNP0C0B, ACPI fan device). Try to turn
428 1.68 jruoho * on (or off) the power resources behind these handles
429 1.68 jruoho * to start (or terminate) the active cooling.
430 1.68 jruoho */
431 1.13 kochi flag = *(int *)arg;
432 1.68 jruoho pwr_state = (flag != 0) ? ACPI_STATE_D0 : ACPI_STATE_D3;
433 1.13 kochi
434 1.56 jruoho rv = acpi_eval_reference_handle(obj, &cooler);
435 1.67 jruoho
436 1.56 jruoho if (ACPI_FAILURE(rv)) {
437 1.68 jruoho aprint_error("%s: failed to get reference handle\n", __func__);
438 1.56 jruoho return rv;
439 1.13 kochi }
440 1.13 kochi
441 1.67 jruoho (void)acpi_power_set_from_handle(cooler, pwr_state);
442 1.13 kochi
443 1.13 kochi return AE_OK;
444 1.13 kochi }
445 1.13 kochi
446 1.13 kochi /*
447 1.13 kochi * acpitz_power_zone:
448 1.13 kochi * power on or off the i:th part of the zone zone
449 1.13 kochi */
450 1.13 kochi static void
451 1.13 kochi acpitz_power_zone(struct acpitz_softc *sc, int i, int on)
452 1.13 kochi {
453 1.13 kochi KASSERT(i >= 0 && i < ATZ_NLEVELS);
454 1.13 kochi
455 1.13 kochi acpi_foreach_package_object(sc->sc_zone.al[i].Pointer,
456 1.13 kochi acpitz_switch_cooler, &on);
457 1.13 kochi }
458 1.13 kochi
459 1.13 kochi
460 1.13 kochi /*
461 1.13 kochi * acpitz_power_off:
462 1.13 kochi * power off parts of the zone
463 1.13 kochi */
464 1.13 kochi static void
465 1.13 kochi acpitz_power_off(struct acpitz_softc *sc)
466 1.13 kochi {
467 1.13 kochi int i;
468 1.13 kochi
469 1.13 kochi for (i = 0 ; i < ATZ_NLEVELS; i++) {
470 1.13 kochi if (sc->sc_zone.al[i].Pointer == NULL)
471 1.13 kochi continue;
472 1.13 kochi acpitz_power_zone(sc, i, 0);
473 1.13 kochi }
474 1.13 kochi sc->sc_active = ATZ_ACTIVE_NONE;
475 1.13 kochi sc->sc_flags &= ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
476 1.13 kochi }
477 1.13 kochi
478 1.13 kochi static void
479 1.14 martin acpitz_get_zone(void *opaque, int verbose)
480 1.13 kochi {
481 1.33 xtraeme device_t dv = opaque;
482 1.33 xtraeme struct acpitz_softc *sc = device_private(dv);
483 1.13 kochi ACPI_STATUS rv;
484 1.13 kochi char buf[8];
485 1.13 kochi int i, valid_levels;
486 1.13 kochi
487 1.29 jmcneill if (!sc->sc_first) {
488 1.13 kochi acpitz_power_off(sc);
489 1.13 kochi
490 1.13 kochi for (i = 0; i < ATZ_NLEVELS; i++) {
491 1.13 kochi if (sc->sc_zone.al[i].Pointer != NULL)
492 1.47 mlelstv ACPI_FREE(sc->sc_zone.al[i].Pointer);
493 1.13 kochi sc->sc_zone.al[i].Pointer = NULL;
494 1.13 kochi }
495 1.63 jruoho }
496 1.13 kochi
497 1.13 kochi valid_levels = 0;
498 1.13 kochi
499 1.13 kochi for (i = 0; i < ATZ_NLEVELS; i++) {
500 1.13 kochi ACPI_OBJECT *obj;
501 1.13 kochi
502 1.13 kochi snprintf(buf, sizeof(buf), "_AC%d", i);
503 1.33 xtraeme if (acpitz_get_integer(dv, buf, &sc->sc_zone.ac[i]))
504 1.13 kochi continue;
505 1.13 kochi
506 1.13 kochi snprintf(buf, sizeof(buf), "_AL%d", i);
507 1.13 kochi rv = acpi_eval_struct(sc->sc_devnode->ad_handle, buf,
508 1.13 kochi &sc->sc_zone.al[i]);
509 1.13 kochi if (ACPI_FAILURE(rv)) {
510 1.13 kochi sc->sc_zone.al[i].Pointer = NULL;
511 1.13 kochi continue;
512 1.13 kochi }
513 1.13 kochi
514 1.13 kochi obj = sc->sc_zone.al[i].Pointer;
515 1.13 kochi if (obj != NULL) {
516 1.13 kochi if (obj->Type != ACPI_TYPE_PACKAGE) {
517 1.29 jmcneill aprint_error("%d not package\n", i);
518 1.47 mlelstv ACPI_FREE(obj);
519 1.13 kochi sc->sc_zone.al[i].Pointer = NULL;
520 1.13 kochi continue;
521 1.13 kochi }
522 1.13 kochi }
523 1.13 kochi
524 1.29 jmcneill if (sc->sc_first)
525 1.29 jmcneill aprint_normal(" active cooling level %d: %sC", i,
526 1.13 kochi acpitz_celcius_string(sc->sc_zone.ac[i]));
527 1.13 kochi
528 1.13 kochi valid_levels++;
529 1.13 kochi }
530 1.13 kochi
531 1.33 xtraeme acpitz_get_integer(dv, "_TMP", &sc->sc_zone.tmp);
532 1.33 xtraeme acpitz_get_integer(dv, "_CRT", &sc->sc_zone.crt);
533 1.33 xtraeme acpitz_get_integer(dv, "_HOT", &sc->sc_zone.hot);
534 1.54 jruoho acpitz_get_integer(dv, "_PSV", &sc->sc_zone.psv);
535 1.54 jruoho acpitz_get_integer(dv, "_TC1", &sc->sc_zone.tc1);
536 1.54 jruoho acpitz_get_integer(dv, "_TC2", &sc->sc_zone.tc2);
537 1.54 jruoho
538 1.54 jruoho #if 0
539 1.47 mlelstv sc->sc_zone.psl.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
540 1.13 kochi sc->sc_zone.psl.Pointer = NULL;
541 1.49 cegger AcpiEvaluateObject(sc->sc_devnode->ad_handle,
542 1.49 cegger "_PSL", NULL, &sc->sc_zone.psl);
543 1.54 jruoho #endif
544 1.13 kochi
545 1.48 cegger /* ACPI spec: If _RTV is not present or present and zero,
546 1.48 cegger * values are absolute. */
547 1.48 cegger acpitz_get_integer(dv, "_RTV", &sc->sc_zone.rtv);
548 1.48 cegger if (sc->sc_zone.rtv == ATZ_TMP_INVALID)
549 1.48 cegger sc->sc_zone.rtv = 0;
550 1.48 cegger
551 1.48 cegger
552 1.13 kochi acpitz_sane_temp(&sc->sc_zone.tmp);
553 1.13 kochi acpitz_sane_temp(&sc->sc_zone.crt);
554 1.13 kochi acpitz_sane_temp(&sc->sc_zone.hot);
555 1.13 kochi acpitz_sane_temp(&sc->sc_zone.psv);
556 1.13 kochi
557 1.63 jruoho if (verbose != 0) {
558 1.66 christos int comma = 0;
559 1.66 christos
560 1.63 jruoho aprint_verbose_dev(dv, "");
561 1.63 jruoho
562 1.66 christos if (sc->sc_zone.crt != ATZ_TMP_INVALID) {
563 1.63 jruoho aprint_verbose("critical %s C",
564 1.14 martin acpitz_celcius_string(sc->sc_zone.crt));
565 1.66 christos comma = 1;
566 1.66 christos }
567 1.63 jruoho
568 1.66 christos if (sc->sc_zone.hot != ATZ_TMP_INVALID) {
569 1.66 christos aprint_verbose("%shot %s C", comma ? ", " : "",
570 1.14 martin acpitz_celcius_string(sc->sc_zone.hot));
571 1.66 christos comma = 1;
572 1.66 christos }
573 1.63 jruoho
574 1.66 christos if (sc->sc_zone.psv != ATZ_TMP_INVALID) {
575 1.66 christos aprint_normal("%spassive %s C", comma ? ", " : "",
576 1.59 pgoyette acpitz_celcius_string(sc->sc_zone.psv));
577 1.66 christos comma = 1;
578 1.66 christos }
579 1.29 jmcneill
580 1.66 christos if (valid_levels == 0) {
581 1.66 christos sc->sc_flags |= ATZ_F_PASSIVEONLY;
582 1.63 jruoho
583 1.66 christos if (sc->sc_first)
584 1.66 christos aprint_verbose("%spassive cooling", comma ?
585 1.66 christos ", " : "");
586 1.66 christos }
587 1.63 jruoho
588 1.63 jruoho aprint_verbose("\n");
589 1.66 christos }
590 1.13 kochi
591 1.13 kochi for (i = 0; i < ATZ_NLEVELS; i++)
592 1.13 kochi acpitz_sane_temp(&sc->sc_zone.ac[i]);
593 1.13 kochi
594 1.13 kochi acpitz_power_off(sc);
595 1.29 jmcneill sc->sc_first = 0;
596 1.13 kochi }
597 1.13 kochi
598 1.11 kochi static void
599 1.64 jruoho acpitz_notify_handler(ACPI_HANDLE hdl, uint32_t notify, void *opaque)
600 1.1 jmcneill {
601 1.33 xtraeme device_t dv = opaque;
602 1.15 kochi ACPI_OSD_EXEC_CALLBACK func = NULL;
603 1.13 kochi const char *name;
604 1.46 jmcneill ACPI_STATUS rv;
605 1.1 jmcneill
606 1.1 jmcneill switch (notify) {
607 1.1 jmcneill case ACPI_NOTIFY_ThermalZoneStatusChanged:
608 1.13 kochi func = acpitz_get_status;
609 1.13 kochi name = "status check";
610 1.13 kochi break;
611 1.1 jmcneill case ACPI_NOTIFY_ThermalZoneTripPointsChanged:
612 1.13 kochi case ACPI_NOTIFY_DeviceListsChanged:
613 1.14 martin func = acpitz_get_zone_quiet;
614 1.13 kochi name = "get zone";
615 1.1 jmcneill break;
616 1.1 jmcneill default:
617 1.33 xtraeme aprint_debug_dev(dv,
618 1.33 xtraeme "received unhandled notify message 0x%x\n", notify);
619 1.13 kochi return;
620 1.1 jmcneill }
621 1.1 jmcneill
622 1.13 kochi KASSERT(func != NULL);
623 1.13 kochi
624 1.33 xtraeme rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, func, dv);
625 1.46 jmcneill if (ACPI_FAILURE(rv))
626 1.33 xtraeme aprint_debug_dev(dv, "unable to queue %s\n", name);
627 1.1 jmcneill }
628 1.1 jmcneill
629 1.1 jmcneill static void
630 1.64 jruoho acpitz_sane_temp(uint32_t *tmp)
631 1.13 kochi {
632 1.13 kochi /* Sane temperatures are beteen 0 and 150 C */
633 1.13 kochi if (*tmp < ATZ_ZEROC || *tmp > ATZ_ZEROC + 1500)
634 1.13 kochi *tmp = ATZ_TMP_INVALID;
635 1.13 kochi }
636 1.13 kochi
637 1.13 kochi static int
638 1.64 jruoho acpitz_get_integer(device_t dv, const char *cm, uint32_t *val)
639 1.13 kochi {
640 1.33 xtraeme struct acpitz_softc *sc = device_private(dv);
641 1.13 kochi ACPI_STATUS rv;
642 1.13 kochi ACPI_INTEGER tmp;
643 1.13 kochi
644 1.13 kochi rv = acpi_eval_integer(sc->sc_devnode->ad_handle, cm, &tmp);
645 1.63 jruoho
646 1.13 kochi if (ACPI_FAILURE(rv)) {
647 1.13 kochi *val = ATZ_TMP_INVALID;
648 1.63 jruoho
649 1.63 jruoho if (rv != AE_NOT_FOUND)
650 1.63 jruoho aprint_debug_dev(dv, "failed to evaluate %s: %s\n",
651 1.63 jruoho cm, AcpiFormatException(rv));
652 1.63 jruoho
653 1.13 kochi return 1;
654 1.13 kochi }
655 1.13 kochi
656 1.13 kochi *val = tmp;
657 1.13 kochi
658 1.13 kochi return 0;
659 1.13 kochi }
660 1.13 kochi
661 1.49 cegger static int
662 1.49 cegger acpitz_get_fanspeed(device_t dv,
663 1.64 jruoho uint32_t *fanmin, uint32_t *fanmax, uint32_t *fancurrent)
664 1.49 cegger {
665 1.49 cegger struct acpitz_softc *sc = device_private(dv);
666 1.49 cegger ACPI_STATUS rv;
667 1.49 cegger ACPI_HANDLE handle;
668 1.49 cegger ACPI_INTEGER fmin, fmax, fcurr;
669 1.49 cegger int rc = 0;
670 1.49 cegger
671 1.49 cegger handle = sc->sc_devnode->ad_handle;
672 1.49 cegger rv = acpi_eval_integer(handle, "FMIN", &fmin);
673 1.49 cegger if (ACPI_FAILURE(rv)) {
674 1.49 cegger fmin = ATZ_TMP_INVALID;
675 1.49 cegger rc = 1;
676 1.49 cegger }
677 1.49 cegger rv = acpi_eval_integer(handle, "FMAX", &fmax);
678 1.49 cegger if (ACPI_FAILURE(rv)) {
679 1.49 cegger fmax = ATZ_TMP_INVALID;
680 1.49 cegger rc = 1;
681 1.49 cegger }
682 1.49 cegger rv = acpi_eval_integer(handle, "FRSP", &fcurr);
683 1.49 cegger if (ACPI_FAILURE(rv)) {
684 1.49 cegger fcurr = ATZ_TMP_INVALID;
685 1.49 cegger rc = 1;
686 1.49 cegger }
687 1.49 cegger
688 1.49 cegger if (fanmin)
689 1.49 cegger *fanmin = fmin;
690 1.49 cegger if (fanmax)
691 1.49 cegger *fanmax = fmax;
692 1.49 cegger if (fancurrent)
693 1.49 cegger *fancurrent = fcurr;
694 1.49 cegger return rc;
695 1.49 cegger }
696 1.49 cegger
697 1.49 cegger #ifdef notyet
698 1.49 cegger static ACPI_STATUS
699 1.64 jruoho acpitz_set_fanspeed(device_t dv, uint32_t fanspeed)
700 1.49 cegger {
701 1.49 cegger struct acpitz_softc *sc = device_private(dv);
702 1.49 cegger ACPI_STATUS rv;
703 1.49 cegger ACPI_HANDLE handle;
704 1.49 cegger handle = sc->sc_devnode->ad_handle;
705 1.49 cegger
706 1.49 cegger rv = acpi_eval_set_integer(handle, "FSSP", fanspeed);
707 1.49 cegger if (ACPI_FAILURE(rv))
708 1.49 cegger aprint_debug_dev(dv, "failed to set fanspeed to %u rpm: %s\n",
709 1.49 cegger fanspeed, AcpiFormatException(rv));
710 1.49 cegger return rv;
711 1.49 cegger }
712 1.49 cegger #endif
713 1.49 cegger
714 1.13 kochi static void
715 1.1 jmcneill acpitz_tick(void *opaque)
716 1.1 jmcneill {
717 1.33 xtraeme device_t dv = opaque;
718 1.33 xtraeme struct acpitz_softc *sc = device_private(dv);
719 1.1 jmcneill
720 1.33 xtraeme AcpiOsExecute(OSL_NOTIFY_HANDLER, acpitz_get_status, dv);
721 1.1 jmcneill
722 1.25 joerg callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
723 1.1 jmcneill }
724 1.1 jmcneill
725 1.1 jmcneill static void
726 1.33 xtraeme acpitz_init_envsys(device_t dv)
727 1.1 jmcneill {
728 1.68 jruoho const int flags = ENVSYS_FMONLIMITS | ENVSYS_FMONNOTSUPP;
729 1.33 xtraeme struct acpitz_softc *sc = device_private(dv);
730 1.33 xtraeme
731 1.27 xtraeme sc->sc_sme = sysmon_envsys_create();
732 1.68 jruoho
733 1.42 pgoyette sc->sc_sme->sme_cookie = sc;
734 1.42 pgoyette sc->sc_sme->sme_name = device_xname(dv);
735 1.42 pgoyette sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
736 1.68 jruoho sc->sc_sme->sme_get_limits = acpitz_get_limits;
737 1.49 cegger
738 1.68 jruoho sc->sc_temp_sensor.flags = flags;
739 1.49 cegger sc->sc_temp_sensor.units = ENVSYS_STEMP;
740 1.68 jruoho
741 1.68 jruoho (void)strlcpy(sc->sc_temp_sensor.desc,
742 1.49 cegger sc->sc_zone.name, sizeof(sc->sc_temp_sensor.desc));
743 1.68 jruoho
744 1.49 cegger if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_temp_sensor))
745 1.42 pgoyette goto out;
746 1.42 pgoyette
747 1.68 jruoho if (sc->sc_have_fan != 0) {
748 1.68 jruoho
749 1.68 jruoho sc->sc_fan_sensor.flags = flags;
750 1.49 cegger sc->sc_fan_sensor.units = ENVSYS_SFANRPM;
751 1.68 jruoho
752 1.68 jruoho (void)strlcpy(sc->sc_fan_sensor.desc,
753 1.49 cegger "FAN", sizeof(sc->sc_fan_sensor.desc));
754 1.68 jruoho
755 1.68 jruoho /* Ignore error because fan sensor is optional. */
756 1.68 jruoho (void)sysmon_envsys_sensor_attach(sc->sc_sme,
757 1.68 jruoho &sc->sc_fan_sensor);
758 1.49 cegger }
759 1.49 cegger
760 1.42 pgoyette if (sysmon_envsys_register(sc->sc_sme) == 0)
761 1.27 xtraeme return;
762 1.49 cegger
763 1.42 pgoyette out:
764 1.42 pgoyette aprint_error_dev(dv, "unable to register with sysmon\n");
765 1.68 jruoho
766 1.42 pgoyette sysmon_envsys_destroy(sc->sc_sme);
767 1.68 jruoho sc->sc_sme = NULL;
768 1.42 pgoyette }
769 1.42 pgoyette
770 1.42 pgoyette static void
771 1.42 pgoyette acpitz_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
772 1.58 pgoyette sysmon_envsys_lim_t *limits, uint32_t *props)
773 1.42 pgoyette {
774 1.42 pgoyette struct acpitz_softc *sc = sme->sme_cookie;
775 1.42 pgoyette int i;
776 1.1 jmcneill
777 1.49 cegger switch (edata->units) {
778 1.49 cegger case ENVSYS_STEMP:
779 1.58 pgoyette *props = 0;
780 1.49 cegger if (sc->sc_zone.hot != ATZ_TMP_INVALID) {
781 1.58 pgoyette *props |= PROP_CRITMAX;
782 1.49 cegger limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.hot);
783 1.49 cegger } else if (sc->sc_zone.crt != ATZ_TMP_INVALID) {
784 1.58 pgoyette *props |= PROP_CRITMAX;
785 1.49 cegger limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.crt);
786 1.49 cegger }
787 1.49 cegger for (i = 0; i < ATZ_NLEVELS; i++) {
788 1.49 cegger if (sc->sc_zone.ac[i] != ATZ_TMP_INVALID) {
789 1.51 pgoyette limits->sel_warnmax =
790 1.49 cegger ATZ2UKELVIN(sc->sc_zone.ac[i]);
791 1.58 pgoyette *props |= PROP_WARNMAX;
792 1.49 cegger break;
793 1.49 cegger }
794 1.49 cegger }
795 1.49 cegger break;
796 1.49 cegger
797 1.49 cegger case ENVSYS_SFANRPM:
798 1.58 pgoyette *props = 0;
799 1.49 cegger if (sc->sc_zone.fanmin != ATZ_TMP_INVALID) {
800 1.58 pgoyette *props |= PROP_WARNMIN;
801 1.49 cegger limits->sel_warnmin = sc->sc_zone.fanmin;
802 1.49 cegger sc->sc_fan_sensor.flags |= ENVSYS_FVALID_MIN;
803 1.49 cegger }
804 1.49 cegger if (sc->sc_zone.fanmax != ATZ_TMP_INVALID) {
805 1.58 pgoyette *props |= PROP_WARNMAX;
806 1.49 cegger limits->sel_warnmax = sc->sc_zone.fanmax;
807 1.49 cegger sc->sc_fan_sensor.flags |= ENVSYS_FVALID_MAX;
808 1.42 pgoyette }
809 1.49 cegger break;
810 1.49 cegger }
811 1.1 jmcneill }
812