1 /* $OpenBSD: acpitz.c,v 1.54 2018/06/29 17:39:18 kettenis 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 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 extern struct aml_node aml_root; 100 101 void 102 acpitz_init_perf(void *arg) 103 { 104 if (acpitz_perflevel == -1) 105 acpitz_perflevel = perflevel; 106 107 if (cpu_setperf != acpitz_setperf) { 108 acpitz_cpu_setperf = cpu_setperf; 109 cpu_setperf = acpitz_setperf; 110 } 111 } 112 113 void 114 acpitz_setperf(int level) 115 { 116 extern struct acpi_softc *acpi_softc; 117 118 if (level < 0 || level > 100) 119 return; 120 121 if (acpi_softc == NULL) 122 return; 123 if (acpi_softc->sc_pse && level > acpitz_perflevel) 124 return; 125 126 if (acpitz_cpu_setperf) 127 acpitz_cpu_setperf(level); 128 } 129 130 void 131 acpitz_init(struct acpitz_softc *sc, int flag) 132 { 133 int i; 134 char name[5]; 135 struct aml_value res; 136 137 /* Read trip points */ 138 if (flag & ACPITZ_TRIPS) { 139 sc->sc_psv = acpitz_getreading(sc, "_PSV"); 140 for (i = 0; i < ACPITZ_MAX_AC; i++) { 141 snprintf(name, sizeof(name), "_AC%d", i); 142 sc->sc_ac[i] = acpitz_getreading(sc, name); 143 } 144 } 145 146 /* Read device lists */ 147 if (flag & ACPITZ_DEVLIST) { 148 if (!aml_evalname(sc->sc_acpi, sc->sc_devnode, "_PSL", 149 0, NULL, &res)) { 150 acpi_freedevlist(&sc->sc_psl); 151 acpi_getdevlist(&sc->sc_psl, sc->sc_devnode, &res, 0); 152 aml_freevalue(&res); 153 } 154 for (i = 0; i < ACPITZ_MAX_AC; i++) { 155 snprintf(name, sizeof(name), "_AL%d", i); 156 if (!aml_evalname(sc->sc_acpi, sc->sc_devnode, name, 157 0, NULL, &res)) { 158 acpi_freedevlist(&sc->sc_alx[i]); 159 acpi_getdevlist(&sc->sc_alx[i], 160 sc->sc_devnode, &res, 0); 161 aml_freevalue(&res); 162 } 163 /* initialize current state to unknown */ 164 sc->sc_ac_stat[i] = ACPITZ_UNKNOWN; 165 } 166 } 167 } 168 169 int 170 acpitz_match(struct device *parent, void *match, void *aux) 171 { 172 struct acpi_attach_args *aa = aux; 173 struct cfdata *cf = match; 174 175 /* sanity */ 176 if (aa->aaa_name == NULL || 177 strcmp(aa->aaa_name, cf->cf_driver->cd_name) != 0 || 178 aa->aaa_table != NULL) 179 return (0); 180 181 if (aa->aaa_node->value->type != AML_OBJTYPE_THERMZONE) 182 return (0); 183 184 return (1); 185 } 186 187 void 188 acpitz_attach(struct device *parent, struct device *self, void *aux) 189 { 190 struct acpitz_softc *sc = (struct acpitz_softc *)self; 191 struct acpi_attach_args *aa = aux; 192 int i; 193 char name[5]; 194 195 sc->sc_acpi = (struct acpi_softc *)parent; 196 sc->sc_devnode = aa->aaa_node; 197 198 TAILQ_INIT(&sc->sc_psl); 199 for (i = 0; i < ACPITZ_MAX_AC; i++) 200 TAILQ_INIT(&sc->sc_alx[i]); 201 202 /* 203 * Preread the trip points (discard/ignore values read here as we will 204 * re-read them later) 205 */ 206 acpitz_gettempreading(sc, "_CRT"); 207 acpitz_gettempreading(sc, "_HOT"); 208 acpitz_gettempreading(sc, "_PSV"); 209 for (i = 0; i < ACPITZ_MAX_AC; i++) { 210 snprintf(name, sizeof(name), "_AC%d", i); 211 acpitz_getreading(sc, name); 212 } 213 acpitz_gettempreading(sc, "_TMP"); 214 215 sc->sc_lasttmp = -1; 216 if ((sc->sc_tmp = acpitz_gettempreading(sc, "_TMP")) == -1) { 217 dnprintf(10, ": failed to read _TMP\n"); 218 return; 219 } 220 221 if ((sc->sc_crt = acpitz_gettempreading(sc, "_CRT")) == -1) 222 printf(": no critical temperature defined\n"); 223 else 224 printf(": critical temperature is %d degC\n", KTOC(sc->sc_crt)); 225 226 sc->sc_hot = acpitz_gettempreading(sc, "_HOT"); 227 sc->sc_tc1 = acpitz_getreading(sc, "_TC1"); 228 sc->sc_tc2 = acpitz_getreading(sc, "_TC2"); 229 230 /* get _PSL, _ALx */ 231 acpitz_init(sc, ACPITZ_INIT); 232 233 dnprintf(10, "%s: _HOT: %d _TC1: %d _TC2: %d _PSV: %d _TMP: %d " 234 "_CRT: %d\n", DEVNAME(sc), sc->sc_hot, sc->sc_tc1, sc->sc_tc2, 235 sc->sc_psv, sc->sc_tmp, sc->sc_crt); 236 237 strlcpy(sc->sc_sensdev.xname, DEVNAME(sc), 238 sizeof(sc->sc_sensdev.xname)); 239 strlcpy(sc->sc_sens.desc, "zone temperature", 240 sizeof(sc->sc_sens.desc)); 241 sc->sc_sens.type = SENSOR_TEMP; 242 sensor_attach(&sc->sc_sensdev, &sc->sc_sens); 243 sensordev_install(&sc->sc_sensdev); 244 245 aml_register_notify(sc->sc_devnode, NULL, 246 acpitz_notify, sc, ACPIDEV_POLL); 247 248 /* 249 * XXX use kthread_create_deferred to ensure we are the very last 250 * piece of code that touches this pointer after all CPUs have been 251 * fully attached 252 */ 253 kthread_create_deferred(acpitz_init_perf, sc); 254 } 255 256 int 257 acpitz_activate(struct device *self, int act) 258 { 259 struct acpitz_softc *sc = (struct acpitz_softc *)self; 260 261 switch (act) { 262 case DVACT_WAKEUP: 263 acpitz_init(sc, ACPITZ_INIT); 264 break; 265 } 266 return 0; 267 } 268 269 int 270 acpitz_setfan(struct acpitz_softc *sc, int i, char *method) 271 { 272 struct aml_node *node; 273 struct aml_value res1, *ref; 274 char name[8]; 275 int rv = 1, x, y; 276 int64_t sta; 277 struct acpi_devlist *dl; 278 279 dnprintf(20, "%s: acpitz_setfan(%d, %s)\n", DEVNAME(sc), i, method); 280 281 x = 0; 282 snprintf(name, sizeof(name), "_AL%d", i); 283 TAILQ_FOREACH(dl, &sc->sc_alx[i], dev_link) { 284 if (aml_evalname(sc->sc_acpi, dl->dev_node, "_PR0",0 , NULL, 285 &res1)) { 286 printf("%s: %s[%d] _PR0 failed\n", DEVNAME(sc), 287 name, x); 288 aml_freevalue(&res1); 289 x++; 290 291 /* 292 * This fan lacks the right method to operate: 293 * disabling active cooling trip points. 294 */ 295 sc->sc_ac[i] = -1; 296 continue; 297 } 298 if (res1.type != AML_OBJTYPE_PACKAGE) { 299 printf("%s: %s[%d] _PR0 not a package\n", DEVNAME(sc), 300 name, x); 301 aml_freevalue(&res1); 302 x++; 303 continue; 304 } 305 for (y = 0; y < res1.length; y++) { 306 ref = res1.v_package[y]; 307 if (ref->type == AML_OBJTYPE_STRING) { 308 node = aml_searchrel(sc->sc_devnode, 309 ref->v_string); 310 if (node == NULL) { 311 printf("%s: %s[%d.%d] _PR0" 312 " not a valid device\n", 313 DEVNAME(sc), name, x, y); 314 continue; 315 } 316 ref = node->value; 317 } 318 if (ref->type == AML_OBJTYPE_OBJREF) { 319 ref = ref->v_objref.ref; 320 } 321 if (ref->type != AML_OBJTYPE_DEVICE && 322 ref->type != AML_OBJTYPE_POWERRSRC) { 323 printf("%s: %s[%d.%d] _PR0 not a package\n", 324 DEVNAME(sc), name, x, y); 325 continue; 326 } 327 if (aml_evalname(sc->sc_acpi, ref->node, method, 0, 328 NULL, NULL)) 329 printf("%s: %s[%d.%d] %s fails\n", 330 DEVNAME(sc), name, x, y, method); 331 332 /* save off status of fan */ 333 if (aml_evalinteger(sc->sc_acpi, ref->node, "_STA", 0, 334 NULL, &sta)) 335 printf("%s: %s[%d.%d] _STA fails\n", 336 DEVNAME(sc), name, x, y); 337 else { 338 sc->sc_ac_stat[i] = sta; 339 } 340 } 341 aml_freevalue(&res1); 342 x++; 343 } 344 rv = 0; 345 return (rv); 346 } 347 348 void 349 acpitz_refresh(void *arg) 350 { 351 struct acpitz_softc *sc = arg; 352 int i, trend, nperf; 353 354 dnprintf(30, "%s: %s: refresh\n", DEVNAME(sc), 355 sc->sc_devnode->name); 356 357 /* get _TMP and debounce the value */ 358 if ((sc->sc_tmp = acpitz_gettempreading(sc, "_TMP")) == -1) { 359 printf("%s: %s: failed to read temp\n", DEVNAME(sc), 360 sc->sc_devnode->name); 361 return; 362 } 363 /* critical trip points */ 364 if (sc->sc_crt != -1 && sc->sc_crt <= sc->sc_tmp) { 365 /* do critical shutdown */ 366 printf("%s: critical temperature exceeded %dC, shutting " 367 "down\n", DEVNAME(sc), KTOC(sc->sc_tmp)); 368 prsignal(initprocess, SIGUSR2); 369 } 370 if (sc->sc_hot != -1 && sc->sc_hot <= sc->sc_tmp) { 371 printf("%s: _HOT temperature\n", DEVNAME(sc)); 372 /* XXX go to S4, until then cool as hard as we can */ 373 } 374 375 /* passive cooling */ 376 if (sc->sc_lasttmp != -1 && sc->sc_tc1 != -1 && sc->sc_tc2 != -1 && 377 sc->sc_psv != -1) { 378 dnprintf(30, "%s: passive cooling: lasttmp: %d tc1: %d " 379 "tc2: %d psv: %d\n", DEVNAME(sc), sc->sc_lasttmp, 380 sc->sc_tc1, sc->sc_tc2, sc->sc_psv); 381 382 nperf = acpitz_perflevel; 383 if (sc->sc_psv <= sc->sc_tmp) { 384 /* Passive cooling enabled */ 385 dnprintf(1, "%s: enabling passive %d %d\n", 386 DEVNAME(sc), sc->sc_tmp, sc->sc_psv); 387 if (!sc->sc_pse) 388 sc->sc_acpi->sc_pse++; 389 sc->sc_pse = 1; 390 391 trend = sc->sc_tc1 * (sc->sc_tmp - sc->sc_lasttmp) + 392 sc->sc_tc2 * (sc->sc_tmp - sc->sc_psv); 393 394 /* Depending on trend, slow down/speed up */ 395 if (trend > 0) 396 nperf -= PERFSTEP; 397 else 398 nperf += PERFSTEP; 399 } 400 else { 401 /* Passive cooling disabled, increase % */ 402 dnprintf(1, "%s: disabling passive %d %d\n", 403 DEVNAME(sc), sc->sc_tmp, sc->sc_psv); 404 if (sc->sc_pse) 405 sc->sc_acpi->sc_pse--; 406 sc->sc_pse = 0; 407 nperf += PERFSTEP; 408 } 409 if (nperf < 0) 410 nperf = 0; 411 else if (nperf > 100) 412 nperf = 100; 413 414 /* clamp passive cooling request */ 415 if (nperf > perflevel) 416 nperf = perflevel; 417 418 /* Perform CPU setperf */ 419 if (acpitz_cpu_setperf && nperf != acpitz_perflevel) { 420 acpitz_perflevel = nperf; 421 acpitz_cpu_setperf(nperf); 422 } 423 } 424 sc->sc_lasttmp = sc->sc_tmp; 425 426 /* active cooling */ 427 for (i = 0; i < ACPITZ_MAX_AC; i++) { 428 if (sc->sc_ac[i] != -1 && sc->sc_ac[i] <= sc->sc_tmp) { 429 /* turn on fan i */ 430 if (sc->sc_ac_stat[i] <= 0) 431 acpitz_setfan(sc, i, "_ON_"); 432 } else if (sc->sc_ac[i] != -1) { 433 /* turn off fan i */ 434 if ((sc->sc_ac_stat[i] == ACPITZ_UNKNOWN) || 435 (sc->sc_ac_stat[i] > 0)) 436 acpitz_setfan(sc, i, "_OFF"); 437 } 438 } 439 sc->sc_sens.value = sc->sc_tmp * 100000 - 50000; 440 } 441 442 int 443 acpitz_getreading(struct acpitz_softc *sc, char *name) 444 { 445 uint64_t val; 446 447 if (!aml_evalinteger(sc->sc_acpi, sc->sc_devnode, name, 0, NULL, &val)) 448 return (val); 449 450 return (-1); 451 } 452 453 int 454 acpitz_gettempreading(struct acpitz_softc *sc, char *name) 455 { 456 int rv = -1, tmp = -1, i; 457 458 for (i = 0; i < ACPITZ_TMP_RETRY; i++) { 459 tmp = acpitz_getreading(sc, name); 460 if (tmp == -1) 461 goto out; 462 if (KTOC(tmp) >= 0) { 463 rv = tmp; 464 break; 465 } else { 466 dnprintf(20, "%s: %d invalid reading on %s, " 467 "debouncing\n", DEVNAME(sc), tmp, name); 468 } 469 470 acpi_sleep(1000, "acpitz"); /* debounce: 1000 msec */ 471 } 472 if (i >= ACPITZ_TMP_RETRY) { 473 printf("%s: %s: failed to read %s\n", DEVNAME(sc), 474 sc->sc_devnode->name, name); 475 goto out; 476 } 477 out: 478 dnprintf(30, "%s: name: %s tmp: %d => %dC, rv: %d\n", DEVNAME(sc), 479 name, tmp, KTOC(tmp), rv); 480 return (rv); 481 } 482 483 int 484 acpitz_notify(struct aml_node *node, int notify_type, void *arg) 485 { 486 struct acpitz_softc *sc = arg; 487 488 dnprintf(10, "%s notify: %.2x %s\n", DEVNAME(sc), notify_type, 489 sc->sc_devnode->name); 490 491 switch (notify_type) { 492 case 0x80: /* hardware notifications */ 493 break; 494 case 0x81: /* operating Points changed */ 495 acpitz_init(sc, ACPITZ_TRIPS); 496 break; 497 case 0x82: /* re-evaluate thermal device list */ 498 acpitz_init(sc, ACPITZ_DEVLIST); 499 break; 500 default: 501 break; 502 } 503 504 acpitz_refresh(sc); 505 return (0); 506 } 507