xref: /openbsd/sys/dev/acpi/acpitz.c (revision 3bef86f7)
1 /* $OpenBSD: acpitz.c,v 1.59 2022/08/10 16:58:16 patrick Exp $ */
2 /*
3  * Copyright (c) 2006 Can Erkin Acar <canacar@openbsd.org>
4  * Copyright (c) 2005 Marco Peereboom <marco@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/proc.h>
21 #include <sys/signalvar.h>
22 #include <sys/systm.h>
23 #include <sys/device.h>
24 #include <sys/malloc.h>
25 #include <sys/kernel.h>
26 #include <sys/kthread.h>
27 
28 #include <machine/bus.h>
29 
30 #include <dev/acpi/acpivar.h>
31 #include <dev/acpi/acpidev.h>
32 #include <dev/acpi/amltypes.h>
33 #include <dev/acpi/dsdt.h>
34 
35 #include <sys/sensors.h>
36 
37 #define KTOC(k)			((k - 2732) / 10)
38 #define ACPITZ_MAX_AC		(10)
39 #define ACPITZ_TMP_RETRY	(3)
40 #define ACPITZ_UNKNOWN		(-1)
41 
42 struct acpitz_softc {
43 	struct device		sc_dev;
44 
45 	struct acpi_softc	*sc_acpi;
46 	struct aml_node		*sc_devnode;
47 
48 	int			sc_tmp;
49 	int			sc_crt;
50 	int			sc_hot;
51 	int			sc_ac[ACPITZ_MAX_AC];
52 	int			sc_ac_stat[ACPITZ_MAX_AC];
53 	int			sc_pse;
54 	int			sc_psv;
55 	int			sc_tc1;
56 	int			sc_tc2;
57 	int			sc_lasttmp;
58 
59 	struct ksensor		sc_sens;
60 	struct ksensordev	sc_sensdev;
61 
62 	struct acpi_devlist_head	sc_psl;
63 	struct acpi_devlist_head	sc_alx[ACPITZ_MAX_AC];
64 };
65 
66 int	acpitz_match(struct device *, void *, void *);
67 void	acpitz_attach(struct device *, struct device *, void *);
68 int	acpitz_activate(struct device *, int);
69 
70 const struct cfattach acpitz_ca = {
71 	sizeof(struct acpitz_softc), acpitz_match, acpitz_attach,
72 	NULL, acpitz_activate
73 };
74 
75 struct cfdriver acpitz_cd = {
76 	NULL, "acpitz", DV_DULL
77 };
78 
79 void	acpitz_init_perf(void *);
80 void	acpitz_setperf(int);
81 void	acpitz_monitor(struct acpitz_softc *);
82 void	acpitz_refresh(void *);
83 int	acpitz_notify(struct aml_node *, int, void *);
84 int	acpitz_gettempreading(struct acpitz_softc *, char *);
85 int	acpitz_getreading(struct acpitz_softc *, char *);
86 int	acpitz_setfan(struct acpitz_softc *, int, char *);
87 void	acpitz_init(struct acpitz_softc *, int);
88 
89 void		(*acpitz_cpu_setperf)(int);
90 int		acpitz_perflevel = -1;
91 extern void	(*cpu_setperf)(int);
92 extern int	perflevel;
93 #define PERFSTEP 10
94 
95 #define ACPITZ_TRIPS	(1L << 0)
96 #define ACPITZ_DEVLIST	(1L << 1)
97 #define ACPITZ_INIT	(ACPITZ_TRIPS|ACPITZ_DEVLIST)
98 
99 void
100 acpitz_init_perf(void *arg)
101 {
102 	if (acpitz_perflevel == -1)
103 		acpitz_perflevel = perflevel;
104 
105 	if (cpu_setperf != acpitz_setperf) {
106 		acpitz_cpu_setperf = cpu_setperf;
107 		cpu_setperf = acpitz_setperf;
108 	}
109 }
110 
111 void
112 acpitz_setperf(int level)
113 {
114 	extern struct acpi_softc *acpi_softc;
115 
116 	if (level < 0 || level > 100)
117 		return;
118 
119 	if (acpi_softc == NULL)
120 		return;
121 	if (acpi_softc->sc_pse && level > acpitz_perflevel)
122 		return;
123 
124 	if (acpitz_cpu_setperf)
125 		acpitz_cpu_setperf(level);
126 }
127 
128 void
129 acpitz_init(struct acpitz_softc *sc, int flag)
130 {
131 	int i;
132 	char name[5];
133 	struct aml_value res;
134 
135 	/* Read trip points */
136 	if (flag & ACPITZ_TRIPS) {
137 		sc->sc_psv = acpitz_getreading(sc, "_PSV");
138 		for (i = 0; i < ACPITZ_MAX_AC; i++) {
139 			snprintf(name, sizeof(name), "_AC%d", i);
140 			sc->sc_ac[i] = acpitz_getreading(sc, name);
141 		}
142 	}
143 
144 	/* Read device lists */
145 	if (flag & ACPITZ_DEVLIST) {
146 		if (!aml_evalname(sc->sc_acpi, sc->sc_devnode, "_PSL",
147 		     0, NULL, &res)) {
148 			acpi_freedevlist(&sc->sc_psl);
149 			acpi_getdevlist(&sc->sc_psl, sc->sc_devnode, &res, 0);
150 			aml_freevalue(&res);
151 		}
152 		for (i = 0; i < ACPITZ_MAX_AC; i++) {
153 			snprintf(name, sizeof(name), "_AL%d", i);
154 			if (!aml_evalname(sc->sc_acpi, sc->sc_devnode, name,
155 			    0, NULL, &res)) {
156 				acpi_freedevlist(&sc->sc_alx[i]);
157 				acpi_getdevlist(&sc->sc_alx[i],
158 				    sc->sc_devnode, &res, 0);
159 				aml_freevalue(&res);
160 			}
161 			/* initialize current state to unknown */
162 			sc->sc_ac_stat[i] = ACPITZ_UNKNOWN;
163 		}
164 	}
165 }
166 
167 int
168 acpitz_match(struct device *parent, void *match, void *aux)
169 {
170 	struct acpi_attach_args	*aa = aux;
171 	struct cfdata		*cf = match;
172 
173 	/* sanity */
174 	if (aa->aaa_name == NULL ||
175 	    strcmp(aa->aaa_name, cf->cf_driver->cd_name) != 0 ||
176 	    aa->aaa_table != NULL)
177 		return (0);
178 
179 	if (aa->aaa_node->value->type != AML_OBJTYPE_THERMZONE)
180 		return (0);
181 
182 	return (1);
183 }
184 
185 void
186 acpitz_attach(struct device *parent, struct device *self, void *aux)
187 {
188 	struct acpitz_softc *sc = (struct acpitz_softc *)self;
189 	struct acpi_attach_args	*aa = aux;
190 	int i;
191 	char name[5];
192 
193 	sc->sc_acpi = (struct acpi_softc *)parent;
194 	sc->sc_devnode = aa->aaa_node;
195 
196 	TAILQ_INIT(&sc->sc_psl);
197 	for (i = 0; i < ACPITZ_MAX_AC; i++)
198 		TAILQ_INIT(&sc->sc_alx[i]);
199 
200 	/*
201 	 * Preread the trip points (discard/ignore values read here as we will
202 	 * re-read them later)
203 	 */
204 	acpitz_gettempreading(sc, "_CRT");
205 	acpitz_gettempreading(sc, "_HOT");
206 	acpitz_gettempreading(sc, "_PSV");
207 	for (i = 0; i < ACPITZ_MAX_AC; i++) {
208 		snprintf(name, sizeof(name), "_AC%d", i);
209 		acpitz_getreading(sc, name);
210 	}
211 	acpitz_gettempreading(sc, "_TMP");
212 
213 	sc->sc_lasttmp = -1;
214 	if ((sc->sc_tmp = acpitz_gettempreading(sc, "_TMP")) == -1) {
215 		dnprintf(10, ": failed to read _TMP");
216 		printf("\n");
217 		return;
218 	}
219 
220 	if ((sc->sc_crt = acpitz_gettempreading(sc, "_CRT")) == -1)
221 		printf(": no critical temperature defined\n");
222 	else
223 		printf(": critical temperature is %d degC\n", KTOC(sc->sc_crt));
224 
225 	sc->sc_hot = acpitz_gettempreading(sc, "_HOT");
226 	sc->sc_tc1 = acpitz_getreading(sc, "_TC1");
227 	sc->sc_tc2 = acpitz_getreading(sc, "_TC2");
228 
229 	/* get _PSL, _ALx */
230 	acpitz_init(sc, ACPITZ_INIT);
231 
232 	dnprintf(10, "%s: _HOT: %d _TC1: %d _TC2: %d _PSV: %d _TMP: %d "
233 	    "_CRT: %d\n", DEVNAME(sc), sc->sc_hot, sc->sc_tc1, sc->sc_tc2,
234 	    sc->sc_psv, sc->sc_tmp, sc->sc_crt);
235 
236 	strlcpy(sc->sc_sensdev.xname, DEVNAME(sc),
237 	    sizeof(sc->sc_sensdev.xname));
238 	strlcpy(sc->sc_sens.desc, "zone temperature",
239 	    sizeof(sc->sc_sens.desc));
240 	sc->sc_sens.type = SENSOR_TEMP;
241 	sensor_attach(&sc->sc_sensdev, &sc->sc_sens);
242 	sensordev_install(&sc->sc_sensdev);
243 
244 	aml_register_notify(sc->sc_devnode, NULL,
245 	    acpitz_notify, sc, ACPIDEV_POLL);
246 
247 	/*
248 	 * XXX use kthread_create_deferred to ensure we are the very last
249 	 * piece of code that touches this pointer after all CPUs have been
250 	 * fully attached
251 	 */
252 	kthread_create_deferred(acpitz_init_perf, sc);
253 }
254 
255 int
256 acpitz_activate(struct device *self, int act)
257 {
258 	struct acpitz_softc	*sc = (struct acpitz_softc *)self;
259 
260 	switch (act) {
261 	case DVACT_WAKEUP:
262 		acpitz_init(sc, ACPITZ_INIT);
263 		break;
264 	}
265 	return 0;
266 }
267 
268 int
269 acpitz_setfan(struct acpitz_softc *sc, int i, char *method)
270 {
271 	struct aml_node		*node;
272 	struct aml_value	res1, *ref;
273 	char			name[8];
274 	int			rv = 1, x, y;
275 	int64_t			sta;
276 	struct acpi_devlist    *dl;
277 
278 	dnprintf(20, "%s: acpitz_setfan(%d, %s)\n", DEVNAME(sc), i, method);
279 
280 	x = 0;
281 	snprintf(name, sizeof(name), "_AL%d", i);
282 	TAILQ_FOREACH(dl, &sc->sc_alx[i], dev_link) {
283 		if (aml_evalname(sc->sc_acpi, dl->dev_node, "_PR0",0 , NULL,
284 		    &res1)) {
285 			printf("%s: %s[%d] _PR0 failed\n", DEVNAME(sc),
286 			    name, x);
287 			aml_freevalue(&res1);
288 			x++;
289 
290 			/*
291 			 * This fan lacks the right method to operate:
292 			 * disabling active cooling trip points.
293 			 */
294 			sc->sc_ac[i] = -1;
295 			continue;
296 		}
297 		if (res1.type != AML_OBJTYPE_PACKAGE) {
298 			printf("%s: %s[%d] _PR0 not a package\n", DEVNAME(sc),
299 			    name, x);
300 			aml_freevalue(&res1);
301 			x++;
302 			continue;
303 		}
304 		for (y = 0; y < res1.length; y++) {
305 			ref = res1.v_package[y];
306 			if (ref->type == AML_OBJTYPE_NAMEREF) {
307 				node = aml_searchrel(sc->sc_devnode,
308 				    aml_getname(ref->v_nameref));
309 				if (node == NULL) {
310 					printf("%s: %s[%d.%d] _PR0"
311 					    " not a valid device\n",
312 					    DEVNAME(sc), name, x, y);
313 					continue;
314 				}
315 				ref = node->value;
316 			}
317 			if (ref->type == AML_OBJTYPE_OBJREF) {
318 				ref = ref->v_objref.ref;
319 			}
320 			if (ref->type != AML_OBJTYPE_DEVICE &&
321 			    ref->type != AML_OBJTYPE_POWERRSRC) {
322 				printf("%s: %s[%d.%d] _PR0 not a package\n",
323 				    DEVNAME(sc), name, x, y);
324 				continue;
325 			}
326 			if (aml_evalname(sc->sc_acpi, ref->node, method, 0,
327 			    NULL, NULL))
328 				printf("%s: %s[%d.%d] %s fails\n",
329 				    DEVNAME(sc), name, x, y, method);
330 
331 			/* save off status of fan */
332 			if (aml_evalinteger(sc->sc_acpi, ref->node, "_STA", 0,
333 			    NULL, &sta))
334 				printf("%s: %s[%d.%d] _STA fails\n",
335 				    DEVNAME(sc), name, x, y);
336 			else {
337 				sc->sc_ac_stat[i] = sta;
338 			}
339 		}
340 		aml_freevalue(&res1);
341 		x++;
342 	}
343 	rv = 0;
344 	return (rv);
345 }
346 
347 void
348 acpitz_refresh(void *arg)
349 {
350 	struct acpitz_softc	*sc = arg;
351 	int			i, trend, nperf;
352 
353 	dnprintf(30, "%s: %s: refresh\n", DEVNAME(sc),
354 	    sc->sc_devnode->name);
355 
356 	/* get _TMP and debounce the value */
357 	if ((sc->sc_tmp = acpitz_gettempreading(sc, "_TMP")) == -1) {
358 		printf("%s: %s: failed to read temp\n", DEVNAME(sc),
359 		    sc->sc_devnode->name);
360 		return;
361 	}
362 	/* critical trip points */
363 	if (sc->sc_crt != -1 && sc->sc_crt <= sc->sc_tmp) {
364 		/* do critical shutdown */
365 		printf("%s: critical temperature exceeded %dC, shutting "
366 		    "down\n", DEVNAME(sc), KTOC(sc->sc_tmp));
367 		prsignal(initprocess, SIGUSR2);
368 	}
369 	if (sc->sc_hot != -1 && sc->sc_hot <= sc->sc_tmp) {
370 		printf("%s: _HOT temperature\n", DEVNAME(sc));
371 		/* XXX go to S4, until then cool as hard as we can */
372 	}
373 
374 	/* passive cooling */
375 	if (sc->sc_lasttmp != -1 && sc->sc_tc1 != -1 && sc->sc_tc2 != -1 &&
376 	    sc->sc_psv != -1) {
377 		dnprintf(30, "%s: passive cooling: lasttmp: %d tc1: %d "
378 		    "tc2: %d psv: %d\n", DEVNAME(sc), sc->sc_lasttmp,
379 		    sc->sc_tc1, sc->sc_tc2, sc->sc_psv);
380 
381 		nperf = acpitz_perflevel;
382 		if (sc->sc_psv <= sc->sc_tmp) {
383 			/* Passive cooling enabled */
384 			dnprintf(1, "%s: enabling passive %d %d\n",
385 			    DEVNAME(sc), sc->sc_tmp, sc->sc_psv);
386 			if (!sc->sc_pse)
387 				sc->sc_acpi->sc_pse++;
388 			sc->sc_pse = 1;
389 
390 			trend = sc->sc_tc1 * (sc->sc_tmp - sc->sc_lasttmp) +
391 			    sc->sc_tc2 * (sc->sc_tmp - sc->sc_psv);
392 
393 			/* Depending on trend, slow down/speed up */
394 			if (trend > 0)
395 				nperf -= PERFSTEP;
396 			else
397 				nperf += PERFSTEP;
398 		}
399 		else {
400 			/* Passive cooling disabled, increase % */
401 			dnprintf(1, "%s: disabling passive %d %d\n",
402 			    DEVNAME(sc), sc->sc_tmp, sc->sc_psv);
403 			if (sc->sc_pse)
404 				sc->sc_acpi->sc_pse--;
405 			sc->sc_pse = 0;
406 			nperf += PERFSTEP;
407 		}
408 		if (nperf < 0)
409 			nperf = 0;
410 		else if (nperf > 100)
411 			nperf = 100;
412 
413 		/* clamp passive cooling request */
414 		if (nperf > perflevel)
415 			nperf = perflevel;
416 
417 		/* Perform CPU setperf */
418 		if (acpitz_cpu_setperf && nperf != acpitz_perflevel) {
419 			acpitz_perflevel = nperf;
420 			acpitz_cpu_setperf(nperf);
421 		}
422 	}
423 	sc->sc_lasttmp = sc->sc_tmp;
424 
425 	/* active cooling */
426 	for (i = 0; i < ACPITZ_MAX_AC; i++) {
427 		if (sc->sc_ac[i] != -1 && sc->sc_ac[i] <= sc->sc_tmp) {
428 			/* turn on fan i */
429 			if (sc->sc_ac_stat[i] <= 0)
430 				acpitz_setfan(sc, i, "_ON_");
431 		} else if (sc->sc_ac[i] != -1) {
432 			/* turn off fan i */
433 			if ((sc->sc_ac_stat[i] == ACPITZ_UNKNOWN) ||
434 			    (sc->sc_ac_stat[i] > 0))
435 				acpitz_setfan(sc, i, "_OFF");
436 		}
437 	}
438 	sc->sc_sens.value = sc->sc_tmp * 100000 - 50000;
439 }
440 
441 int
442 acpitz_getreading(struct acpitz_softc *sc, char *name)
443 {
444 	uint64_t		val;
445 
446 	if (!aml_evalinteger(sc->sc_acpi, sc->sc_devnode, name, 0, NULL, &val))
447 		return (val);
448 
449 	return (-1);
450 }
451 
452 int
453 acpitz_gettempreading(struct acpitz_softc *sc, char *name)
454 {
455 	int			rv = -1, tmp = -1, i;
456 
457 	for (i = 0; i < ACPITZ_TMP_RETRY; i++) {
458 		tmp = acpitz_getreading(sc, name);
459 		if (tmp == -1)
460 			goto out;
461 		if (KTOC(tmp) >= 0) {
462 			rv = tmp;
463 			break;
464 		} else {
465 			dnprintf(20, "%s: %d invalid reading on %s, "
466 			    "debouncing\n", DEVNAME(sc), tmp, name);
467 		}
468 
469 		acpi_sleep(1000, "acpitz");	/* debounce: 1000 msec */
470 	}
471 	if (i >= ACPITZ_TMP_RETRY) {
472 		printf("%s: %s: failed to read %s\n", DEVNAME(sc),
473 		    sc->sc_devnode->name, name);
474 		goto out;
475 	}
476  out:
477 	dnprintf(30, "%s: name: %s tmp: %d => %dC, rv: %d\n", DEVNAME(sc),
478 	    name, tmp, KTOC(tmp), rv);
479 	return (rv);
480 }
481 
482 int
483 acpitz_notify(struct aml_node *node, int notify_type, void *arg)
484 {
485 	struct acpitz_softc	*sc = arg;
486 
487 	dnprintf(10, "%s notify: %.2x %s\n", DEVNAME(sc), notify_type,
488 	    sc->sc_devnode->name);
489 
490 	switch (notify_type) {
491 	case 0x80:	/* hardware notifications */
492 		break;
493 	case 0x81:	/* operating Points changed */
494 		acpitz_init(sc, ACPITZ_TRIPS);
495 		break;
496 	case 0x82:	/* re-evaluate thermal device list */
497 		acpitz_init(sc, ACPITZ_DEVLIST);
498 		break;
499 	default:
500 		break;
501 	}
502 
503 	acpitz_refresh(sc);
504 	return (0);
505 }
506