acpi_tz.c revision 1.1 1 /* $NetBSD: acpi_tz.c,v 1.1 2003/01/04 05:36:03 jmcneill Exp $ */
2
3 /*
4 * Copyright (c) 2003 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * 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 /*
29 * ACPI Thermal Zone driver
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: acpi_tz.c,v 1.1 2003/01/04 05:36:03 jmcneill Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/errno.h>
39 #include <sys/ioctl.h>
40 #include <sys/syslog.h>
41 #include <sys/device.h>
42 #include <sys/callout.h>
43 #include <sys/proc.h>
44 #include <sys/lock.h>
45 #include <dev/sysmon/sysmonvar.h>
46
47 #include <dev/acpi/acpica.h>
48 #include <dev/acpi/acpireg.h>
49 #include <dev/acpi/acpivar.h>
50
51 /* flags */
52 #define ATZ_F_VERBOSE 0x01 /* show events to console */
53
54 /* constants */
55 #define ATZ_TZP_RATE 300 /* default if no _TZP CM present (30 secs) */
56 #define ATZ_NLEVELS 10 /* number of cooling levels, from ACPI spec */
57
58 /* sensor indexes */
59 #define ATZ_SENSOR_TEMP 0 /* thermal zone temperature */
60 #define ATZ_NUMSENSORS 1 /* number of sensors */
61
62 const struct envsys_range acpitz_ranges[] = {
63 { 0, 1, ATZ_SENSOR_TEMP },
64 };
65
66 int acpitz_match(struct device *, struct cfdata *, void *);
67 void acpitz_attach(struct device *, struct device *, void *);
68
69 /*
70 * ACPI Temperature Zone information. Note all temperatures are reported
71 * in tenths of degrees Kelvin
72 */
73 struct acpitz_zone {
74 /* Active cooling temperature threshold */
75 UINT32 ac[ATZ_NLEVELS];
76 /* Package of references to all active cooling devices for a level */
77 ACPI_BUFFER al[ATZ_NLEVELS];
78 /* Critical temperature threshold for system shutdown */
79 UINT32 crt;
80 /* Critical temperature threshold for S4 sleep */
81 UINT32 hot;
82 /* Package of references to processor objects for passive cooling */
83 ACPI_BUFFER psl;
84 /* Passive cooling temperature threshold */
85 UINT32 psv;
86 /* Thermal constants for use in passive cooling formulas */
87 UINT32 tc1, tc2;
88 /* Current temperature of the thermal zone */
89 UINT32 tmp;
90 /* Thermal sampling period for passive cooling, in tenths of seconds */
91 UINT32 tsp;
92 /* Package of references to devices in this TZ (optional) */
93 ACPI_BUFFER tzd;
94 /* Recommended TZ polling frequency, in tenths of seconds */
95 UINT32 tzp;
96 };
97
98 struct acpitz_softc {
99 struct device sc_dev;
100 struct acpi_devnode *sc_devnode;
101 struct acpitz_zone sc_zone;
102 struct callout sc_callout;
103 struct envsys_tre_data sc_data[ATZ_NUMSENSORS];
104 struct envsys_basic_info sc_info[ATZ_NUMSENSORS];
105 struct sysmon_envsys sc_sysmon;
106 struct simplelock sc_slock;
107 int sc_flags;
108 int sc_rate; /* tz poll rate */
109 };
110
111 void acpitz_get_status(void *);
112 static void acpitz_print_status(struct acpitz_softc *);
113 void acpitz_notify_handler(ACPI_HANDLE, UINT32, void *);
114 int acpitz_get_integer(struct acpitz_softc *, char *, UINT32 *);
115 static void acpitz_tick(void *);
116 static void acpitz_init_envsys(struct acpitz_softc *);
117 static int acpitz_gtredata(struct sysmon_envsys *,
118 struct envsys_tre_data *);
119 static int acpitz_streinfo(struct sysmon_envsys *,
120 struct envsys_basic_info *);
121
122 CFATTACH_DECL(acpitz, sizeof(struct acpitz_softc), acpitz_match,
123 acpitz_attach, NULL, NULL);
124
125 /*
126 * acpitz_match: autoconf(9) match routine
127 */
128 int
129 acpitz_match(struct device *parent, struct cfdata *match, void *aux)
130 {
131 struct acpi_attach_args *aa = aux;
132
133 if (aa->aa_node->ad_type != ACPI_TYPE_THERMAL)
134 return 0;
135
136 return 1;
137 }
138
139 /*
140 * acpitz_attach: autoconf(9) attach routine
141 */
142 void
143 acpitz_attach(struct device *parent, struct device *self, void *aux)
144 {
145 struct acpitz_softc *sc = (struct acpitz_softc *)self;
146 struct acpi_attach_args *aa = aux;
147 ACPI_STATUS rv;
148
149 #if 0
150 sc->sc_flags = ATZ_F_VERBOSE;
151 #endif
152 sc->sc_devnode = aa->aa_node;
153
154 printf(": ACPI Thermal Zone\n");
155
156 if (acpitz_get_integer(sc, "_TZP", &sc->sc_zone.tzp)) {
157 printf("%s: unable to get poll rate, using default\n",
158 sc->sc_dev.dv_xname);
159 sc->sc_zone.tzp = ATZ_TZP_RATE;
160 }
161 /* XXX a value of 0 means "polling is not necessary" */
162 if (sc->sc_zone.tzp == 0)
163 sc->sc_zone.tzp = ATZ_TZP_RATE;
164
165 acpitz_get_status(sc);
166
167 rv = AcpiInstallNotifyHandler(sc->sc_devnode->ad_handle,
168 ACPI_DEVICE_NOTIFY, acpitz_notify_handler, sc);
169 if (rv != AE_OK) {
170 printf("%s: unable to install device notify handler\n",
171 sc->sc_dev.dv_xname);
172 return;
173 }
174
175 rv = AcpiInstallNotifyHandler(sc->sc_devnode->ad_handle,
176 ACPI_SYSTEM_NOTIFY, acpitz_notify_handler, sc);
177 if (rv != AE_OK) {
178 printf("%s: unable to install system notify handler\n",
179 sc->sc_dev.dv_xname);
180 return;
181 }
182
183 callout_init(&sc->sc_callout);
184 callout_reset(&sc->sc_callout, (sc->sc_zone.tzp / 10) * hz,
185 acpitz_tick, sc);
186
187 acpitz_init_envsys(sc);
188 }
189
190 void
191 acpitz_get_status(void *opaque)
192 {
193 struct acpitz_softc *sc = opaque;
194
195 acpitz_get_integer(sc, "_TMP", &sc->sc_zone.tmp);
196 sc->sc_data[ATZ_SENSOR_TEMP].cur.data_us = sc->sc_zone.tmp * 100000;
197
198 if (sc->sc_flags & ATZ_F_VERBOSE)
199 acpitz_print_status(sc);
200
201 return;
202 }
203
204 static void
205 acpitz_print_status(struct acpitz_softc *sc)
206 {
207
208 printf("%s: zone temperature is now %d K\n", sc->sc_dev.dv_xname,
209 sc->sc_zone.tmp / 10);
210
211 return;
212 }
213
214 void
215 acpitz_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
216 {
217 struct acpitz_softc *sc = opaque;
218 int rv;
219
220 switch (notify) {
221 case ACPI_NOTIFY_ThermalZoneStatusChanged:
222 case ACPI_NOTIFY_ThermalZoneTripPointsChanged:
223 rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
224 acpitz_get_status, sc);
225 if (rv != AE_OK) {
226 printf("%s: unable to queue status check\n",
227 sc->sc_dev.dv_xname);
228 }
229 break;
230 default:
231 printf("%s: received unhandled notify message %d\n",
232 sc->sc_dev.dv_xname, notify);
233 break;
234 }
235
236 return;
237 }
238
239 int
240 acpitz_get_integer(struct acpitz_softc *sc, char *cm, UINT32 *rv)
241 {
242 ACPI_STATUS status;
243
244 status = acpi_eval_integer(sc->sc_devnode->ad_handle, cm, rv);
245 if (status != AE_OK) {
246 #ifdef ACPI_DEBUG
247 printf("%s: failed to evalulate %s: %x\n", sc->sc_dev.dv_xname,
248 cm, status);
249 #endif
250 return 1;
251 }
252
253 return 0;
254 }
255
256 static void
257 acpitz_tick(void *opaque)
258 {
259 struct acpitz_softc *sc = opaque;
260
261 callout_reset(&sc->sc_callout, (sc->sc_zone.tzp / 10) * hz,
262 acpitz_tick, opaque);
263 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpitz_get_status, sc);
264
265 return;
266 }
267
268 static void
269 acpitz_init_envsys(struct acpitz_softc *sc)
270 {
271 int i;
272
273 simple_lock_init(&sc->sc_slock);
274
275 for (i = 0; i < ATZ_NUMSENSORS; i++) {
276 sc->sc_data[i].sensor = sc->sc_info[i].sensor = i;
277 sc->sc_data[i].validflags = (ENVSYS_FVALID | ENVSYS_FCURVALID);
278 sc->sc_info[i].validflags = ENVSYS_FVALID;
279 sc->sc_data[i].warnflags = ENVSYS_WARN_OK;
280 }
281 #define INITDATA(index, unit, string) \
282 sc->sc_data[index].units = unit; \
283 sc->sc_info[index].units = unit; \
284 snprintf(sc->sc_info[index].desc, sizeof(sc->sc_info[index].desc), \
285 "%s %s", sc->sc_dev.dv_xname, string);
286
287 INITDATA(ATZ_SENSOR_TEMP, ENVSYS_STEMP, "temperature");
288
289 /* hook into sysmon */
290 sc->sc_sysmon.sme_ranges = acpitz_ranges;
291 sc->sc_sysmon.sme_sensor_info = sc->sc_info;
292 sc->sc_sysmon.sme_sensor_data = sc->sc_data;
293 sc->sc_sysmon.sme_cookie = sc;
294 sc->sc_sysmon.sme_gtredata = acpitz_gtredata;
295 sc->sc_sysmon.sme_streinfo = acpitz_streinfo;
296 sc->sc_sysmon.sme_nsensors = ATZ_NUMSENSORS;
297 sc->sc_sysmon.sme_envsys_version = 1000;
298
299 if (sysmon_envsys_register(&sc->sc_sysmon))
300 printf("%s: unable to register with sysmon\n",
301 sc->sc_dev.dv_xname);
302 }
303
304 int
305 acpitz_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
306 {
307 struct acpitz_softc *sc = sme->sme_cookie;
308
309 simple_lock(&sc->sc_slock);
310
311 *tred = sc->sc_data[tred->sensor];
312
313 simple_unlock(&sc->sc_slock);
314
315 return 0;
316 }
317
318 int
319 acpitz_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
320 {
321
322 /* XXX not implemented */
323 binfo->validflags = 0;
324
325 return 0;
326 }
327