smu.c revision 204692
1/*- 2 * Copyright (c) 2009 Nathan Whitehorn 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 ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 * 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 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: head/sys/powerpc/powermac/smu.c 204692 2010-03-04 06:36:00Z nwhitehorn $"); 30 31#include <sys/param.h> 32#include <sys/bus.h> 33#include <sys/systm.h> 34#include <sys/module.h> 35#include <sys/conf.h> 36#include <sys/cpu.h> 37#include <sys/ctype.h> 38#include <sys/kernel.h> 39#include <sys/kthread.h> 40#include <sys/reboot.h> 41#include <sys/rman.h> 42#include <sys/sysctl.h> 43#include <sys/unistd.h> 44 45#include <machine/bus.h> 46#include <machine/intr_machdep.h> 47#include <machine/md_var.h> 48 49#include <dev/led/led.h> 50#include <dev/ofw/openfirm.h> 51#include <dev/ofw/ofw_bus.h> 52#include <powerpc/powermac/macgpiovar.h> 53 54struct smu_cmd { 55 volatile uint8_t cmd; 56 uint8_t len; 57 uint8_t data[254]; 58 59 STAILQ_ENTRY(smu_cmd) cmd_q; 60}; 61 62STAILQ_HEAD(smu_cmdq, smu_cmd); 63 64struct smu_fan { 65 cell_t reg; 66 cell_t min_rpm; 67 cell_t max_rpm; 68 cell_t unmanaged_rpm; 69 char location[32]; 70 71 int old_style; 72 int setpoint; 73}; 74 75struct smu_sensor { 76 cell_t reg; 77 char location[32]; 78 enum { 79 SMU_CURRENT_SENSOR, 80 SMU_VOLTAGE_SENSOR, 81 SMU_POWER_SENSOR, 82 SMU_TEMP_SENSOR 83 } type; 84}; 85 86struct smu_softc { 87 device_t sc_dev; 88 struct mtx sc_mtx; 89 90 struct resource *sc_memr; 91 int sc_memrid; 92 93 bus_dma_tag_t sc_dmatag; 94 bus_space_tag_t sc_bt; 95 bus_space_handle_t sc_mailbox; 96 97 struct smu_cmd *sc_cmd, *sc_cur_cmd; 98 bus_addr_t sc_cmd_phys; 99 bus_dmamap_t sc_cmd_dmamap; 100 struct smu_cmdq sc_cmdq; 101 102 struct smu_fan *sc_fans; 103 int sc_nfans; 104 struct smu_sensor *sc_sensors; 105 int sc_nsensors; 106 107 int sc_doorbellirqid; 108 struct resource *sc_doorbellirq; 109 void *sc_doorbellirqcookie; 110 111 struct proc *sc_fanmgt_proc; 112 time_t sc_lastuserchange; 113 114 /* Calibration data */ 115 uint16_t sc_cpu_diode_scale; 116 int16_t sc_cpu_diode_offset; 117 118 uint16_t sc_cpu_volt_scale; 119 int16_t sc_cpu_volt_offset; 120 uint16_t sc_cpu_curr_scale; 121 int16_t sc_cpu_curr_offset; 122 123 uint16_t sc_slots_pow_scale; 124 int16_t sc_slots_pow_offset; 125 126 /* Thermal management parameters */ 127 int sc_target_temp; /* Default 55 C */ 128 int sc_critical_temp; /* Default 90 C */ 129 130 struct cdev *sc_leddev; 131}; 132 133/* regular bus attachment functions */ 134 135static int smu_probe(device_t); 136static int smu_attach(device_t); 137 138/* cpufreq notification hooks */ 139 140static void smu_cpufreq_pre_change(device_t, const struct cf_level *level); 141static void smu_cpufreq_post_change(device_t, const struct cf_level *level); 142 143/* utility functions */ 144static int smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait); 145static int smu_get_datablock(device_t dev, int8_t id, uint8_t *buf, 146 size_t len); 147static void smu_attach_fans(device_t dev, phandle_t fanroot); 148static void smu_attach_sensors(device_t dev, phandle_t sensroot); 149static void smu_fan_management_proc(void *xdev); 150static void smu_manage_fans(device_t smu); 151static void smu_set_sleepled(void *xdev, int onoff); 152static int smu_server_mode(SYSCTL_HANDLER_ARGS); 153static void smu_doorbell_intr(void *xdev); 154 155/* where to find the doorbell GPIO */ 156 157static device_t smu_doorbell = NULL; 158 159static device_method_t smu_methods[] = { 160 /* Device interface */ 161 DEVMETHOD(device_probe, smu_probe), 162 DEVMETHOD(device_attach, smu_attach), 163 { 0, 0 }, 164}; 165 166static driver_t smu_driver = { 167 "smu", 168 smu_methods, 169 sizeof(struct smu_softc) 170}; 171 172static devclass_t smu_devclass; 173 174DRIVER_MODULE(smu, nexus, smu_driver, smu_devclass, 0, 0); 175MALLOC_DEFINE(M_SMU, "smu", "SMU Sensor Information"); 176 177#define SMU_MAILBOX 0x8000860c 178#define SMU_FANMGT_INTERVAL 1000 /* ms */ 179 180/* Command types */ 181#define SMU_ADC 0xd8 182#define SMU_FAN 0x4a 183#define SMU_I2C 0x9a 184#define SMU_I2C_SIMPLE 0x00 185#define SMU_I2C_NORMAL 0x01 186#define SMU_I2C_COMBINED 0x02 187#define SMU_MISC 0xee 188#define SMU_MISC_GET_DATA 0x02 189#define SMU_MISC_LED_CTRL 0x04 190#define SMU_POWER 0xaa 191#define SMU_POWER_EVENTS 0x8f 192#define SMU_PWR_GET_POWERUP 0x00 193#define SMU_PWR_SET_POWERUP 0x01 194#define SMU_PWR_CLR_POWERUP 0x02 195 196/* Power event types */ 197#define SMU_WAKEUP_KEYPRESS 0x01 198#define SMU_WAKEUP_AC_INSERT 0x02 199#define SMU_WAKEUP_AC_CHANGE 0x04 200#define SMU_WAKEUP_RING 0x10 201 202/* Data blocks */ 203#define SMU_CPUTEMP_CAL 0x18 204#define SMU_CPUVOLT_CAL 0x21 205#define SMU_SLOTPW_CAL 0x78 206 207/* Partitions */ 208#define SMU_PARTITION 0x3e 209#define SMU_PARTITION_LATEST 0x01 210#define SMU_PARTITION_BASE 0x02 211#define SMU_PARTITION_UPDATE 0x03 212 213static int 214smu_probe(device_t dev) 215{ 216 const char *name = ofw_bus_get_name(dev); 217 218 if (strcmp(name, "smu") != 0) 219 return (ENXIO); 220 221 device_set_desc(dev, "Apple System Management Unit"); 222 return (0); 223} 224 225static void 226smu_phys_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 227{ 228 struct smu_softc *sc = xsc; 229 230 sc->sc_cmd_phys = segs[0].ds_addr; 231} 232 233static int 234smu_attach(device_t dev) 235{ 236 struct smu_softc *sc; 237 phandle_t node, child; 238 uint8_t data[12]; 239 240 sc = device_get_softc(dev); 241 242 mtx_init(&sc->sc_mtx, "smu", NULL, MTX_DEF); 243 sc->sc_cur_cmd = NULL; 244 sc->sc_doorbellirqid = -1; 245 246 /* 247 * Map the mailbox area. This should be determined from firmware, 248 * but I have not found a simple way to do that. 249 */ 250 bus_dma_tag_create(NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT, 251 BUS_SPACE_MAXADDR, NULL, NULL, PAGE_SIZE, 1, PAGE_SIZE, 0, NULL, 252 NULL, &(sc->sc_dmatag)); 253 sc->sc_bt = &bs_le_tag; 254 bus_space_map(sc->sc_bt, SMU_MAILBOX, 4, 0, &sc->sc_mailbox); 255 256 /* 257 * Allocate the command buffer. This can be anywhere in the low 4 GB 258 * of memory. 259 */ 260 bus_dmamem_alloc(sc->sc_dmatag, (void **)&sc->sc_cmd, BUS_DMA_WAITOK | 261 BUS_DMA_ZERO, &sc->sc_cmd_dmamap); 262 bus_dmamap_load(sc->sc_dmatag, sc->sc_cmd_dmamap, 263 sc->sc_cmd, PAGE_SIZE, smu_phys_callback, sc, 0); 264 STAILQ_INIT(&sc->sc_cmdq); 265 266 /* 267 * Set up handlers to change CPU voltage when CPU frequency is changed. 268 */ 269 EVENTHANDLER_REGISTER(cpufreq_pre_change, smu_cpufreq_pre_change, dev, 270 EVENTHANDLER_PRI_ANY); 271 EVENTHANDLER_REGISTER(cpufreq_post_change, smu_cpufreq_post_change, dev, 272 EVENTHANDLER_PRI_ANY); 273 274 /* 275 * Detect and attach child devices. 276 */ 277 node = ofw_bus_get_node(dev); 278 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 279 char name[32]; 280 memset(name, 0, sizeof(name)); 281 OF_getprop(child, "name", name, sizeof(name)); 282 283 if (strncmp(name, "rpm-fans", 9) == 0 || 284 strncmp(name, "fans", 5) == 0) 285 smu_attach_fans(dev, child); 286 287 if (strncmp(name, "sensors", 8) == 0) 288 smu_attach_sensors(dev, child); 289 } 290 291 /* 292 * Collect calibration constants. 293 */ 294 smu_get_datablock(dev, SMU_CPUTEMP_CAL, data, sizeof(data)); 295 sc->sc_cpu_diode_scale = (data[4] << 8) + data[5]; 296 sc->sc_cpu_diode_offset = (data[6] << 8) + data[7]; 297 298 smu_get_datablock(dev, SMU_CPUVOLT_CAL, data, sizeof(data)); 299 sc->sc_cpu_volt_scale = (data[4] << 8) + data[5]; 300 sc->sc_cpu_volt_offset = (data[6] << 8) + data[7]; 301 sc->sc_cpu_curr_scale = (data[8] << 8) + data[9]; 302 sc->sc_cpu_curr_offset = (data[10] << 8) + data[11]; 303 304 smu_get_datablock(dev, SMU_SLOTPW_CAL, data, sizeof(data)); 305 sc->sc_slots_pow_scale = (data[4] << 8) + data[5]; 306 sc->sc_slots_pow_offset = (data[6] << 8) + data[7]; 307 308 /* 309 * Set up simple-minded thermal management. 310 */ 311 sc->sc_target_temp = 55; 312 sc->sc_critical_temp = 90; 313 314 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 315 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 316 "target_temp", CTLTYPE_INT | CTLFLAG_RW, &sc->sc_target_temp, 317 sizeof(int), "Target temperature (C)"); 318 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 319 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 320 "critical_temp", CTLTYPE_INT | CTLFLAG_RW, 321 &sc->sc_critical_temp, sizeof(int), "Critical temperature (C)"); 322 323 kproc_create(smu_fan_management_proc, dev, &sc->sc_fanmgt_proc, 324 RFHIGHPID, 0, "smu_thermal"); 325 326 /* 327 * Set up LED interface 328 */ 329 sc->sc_leddev = led_create(smu_set_sleepled, dev, "sleepled"); 330 331 /* 332 * Reset on power loss behavior 333 */ 334 335 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 336 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 337 "server_mode", CTLTYPE_INT | CTLFLAG_RW, dev, 0, 338 smu_server_mode, "I", "Enable reboot after power failure"); 339 340 /* 341 * Set up doorbell interrupt. 342 */ 343 sc->sc_doorbellirqid = 0; 344 sc->sc_doorbellirq = bus_alloc_resource_any(smu_doorbell, SYS_RES_IRQ, 345 &sc->sc_doorbellirqid, RF_ACTIVE); 346 bus_setup_intr(smu_doorbell, sc->sc_doorbellirq, 347 INTR_TYPE_MISC | INTR_MPSAFE, NULL, smu_doorbell_intr, dev, 348 &sc->sc_doorbellirqcookie); 349 powerpc_config_intr(rman_get_start(sc->sc_doorbellirq), 350 INTR_TRIGGER_EDGE, INTR_POLARITY_LOW); 351 352 return (0); 353} 354 355static void 356smu_send_cmd(device_t dev, struct smu_cmd *cmd) 357{ 358 struct smu_softc *sc; 359 360 sc = device_get_softc(dev); 361 362 mtx_assert(&sc->sc_mtx, MA_OWNED); 363 364 powerpc_pow_enabled = 0; /* SMU cannot work if we go to NAP */ 365 sc->sc_cur_cmd = cmd; 366 367 /* Copy the command to the mailbox */ 368 sc->sc_cmd->cmd = cmd->cmd; 369 sc->sc_cmd->len = cmd->len; 370 memcpy(sc->sc_cmd->data, cmd->data, sizeof(cmd->data)); 371 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_PREWRITE); 372 bus_space_write_4(sc->sc_bt, sc->sc_mailbox, 0, sc->sc_cmd_phys); 373 374 /* Flush the cacheline it is in -- SMU bypasses the cache */ 375 __asm __volatile("sync; dcbf 0,%0; sync" :: "r"(sc->sc_cmd): "memory"); 376 377 /* Ring SMU doorbell */ 378 macgpio_write(smu_doorbell, GPIO_DDR_OUTPUT); 379} 380 381static void 382smu_doorbell_intr(void *xdev) 383{ 384 device_t smu; 385 struct smu_softc *sc; 386 int doorbell_ack; 387 388 smu = xdev; 389 doorbell_ack = macgpio_read(smu_doorbell); 390 sc = device_get_softc(smu); 391 392 if (doorbell_ack != (GPIO_DDR_OUTPUT | GPIO_LEVEL_RO | GPIO_DATA)) 393 return; 394 395 mtx_lock(&sc->sc_mtx); 396 397 if (sc->sc_cur_cmd == NULL) /* spurious */ 398 goto done; 399 400 /* Check result. First invalidate the cache again... */ 401 __asm __volatile("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory"); 402 403 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_POSTREAD); 404 405 sc->sc_cur_cmd->cmd = sc->sc_cmd->cmd; 406 sc->sc_cur_cmd->len = sc->sc_cmd->len; 407 memcpy(sc->sc_cur_cmd->data, sc->sc_cmd->data, 408 sizeof(sc->sc_cmd->data)); 409 wakeup(sc->sc_cur_cmd); 410 sc->sc_cur_cmd = NULL; 411 powerpc_pow_enabled = 1; 412 413 done: 414 /* Queue next command if one is pending */ 415 if (STAILQ_FIRST(&sc->sc_cmdq) != NULL) { 416 sc->sc_cur_cmd = STAILQ_FIRST(&sc->sc_cmdq); 417 STAILQ_REMOVE_HEAD(&sc->sc_cmdq, cmd_q); 418 smu_send_cmd(smu, sc->sc_cur_cmd); 419 } 420 421 mtx_unlock(&sc->sc_mtx); 422} 423 424static int 425smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait) 426{ 427 struct smu_softc *sc; 428 uint8_t cmd_code; 429 int error; 430 431 sc = device_get_softc(dev); 432 cmd_code = cmd->cmd; 433 434 mtx_lock(&sc->sc_mtx); 435 if (sc->sc_cur_cmd != NULL) { 436 STAILQ_INSERT_TAIL(&sc->sc_cmdq, cmd, cmd_q); 437 } else 438 smu_send_cmd(dev, cmd); 439 mtx_unlock(&sc->sc_mtx); 440 441 if (!wait) 442 return (0); 443 444 if (sc->sc_doorbellirqid < 0) { 445 /* Poll if the IRQ has not been set up yet */ 446 do { 447 DELAY(50); 448 smu_doorbell_intr(dev); 449 } while (sc->sc_cur_cmd != NULL); 450 } else { 451 /* smu_doorbell_intr will wake us when the command is ACK'ed */ 452 error = tsleep(cmd, 0, "smu", 800 * hz / 1000); 453 if (error != 0) 454 smu_doorbell_intr(dev); /* One last chance */ 455 456 if (error != 0) { 457 mtx_lock(&sc->sc_mtx); 458 if (cmd->cmd == cmd_code) { /* Never processed */ 459 /* Abort this command if we timed out */ 460 if (sc->sc_cur_cmd == cmd) 461 sc->sc_cur_cmd = NULL; 462 else 463 STAILQ_REMOVE(&sc->sc_cmdq, cmd, smu_cmd, 464 cmd_q); 465 mtx_unlock(&sc->sc_mtx); 466 return (error); 467 } 468 error = 0; 469 mtx_unlock(&sc->sc_mtx); 470 } 471 } 472 473 /* SMU acks the command by inverting the command bits */ 474 if (cmd->cmd == ((~cmd_code) & 0xff)) 475 error = 0; 476 else 477 error = EIO; 478 479 return (error); 480} 481 482static int 483smu_get_datablock(device_t dev, int8_t id, uint8_t *buf, size_t len) 484{ 485 struct smu_cmd cmd; 486 uint8_t addr[4]; 487 488 cmd.cmd = SMU_PARTITION; 489 cmd.len = 2; 490 cmd.data[0] = SMU_PARTITION_LATEST; 491 cmd.data[1] = id; 492 493 smu_run_cmd(dev, &cmd, 1); 494 495 addr[0] = addr[1] = 0; 496 addr[2] = cmd.data[0]; 497 addr[3] = cmd.data[1]; 498 499 cmd.cmd = SMU_MISC; 500 cmd.len = 7; 501 cmd.data[0] = SMU_MISC_GET_DATA; 502 cmd.data[1] = sizeof(addr); 503 memcpy(&cmd.data[2], addr, sizeof(addr)); 504 cmd.data[6] = len; 505 506 smu_run_cmd(dev, &cmd, 1); 507 memcpy(buf, cmd.data, len); 508 return (0); 509} 510 511static void 512smu_slew_cpu_voltage(device_t dev, int to) 513{ 514 struct smu_cmd cmd; 515 516 cmd.cmd = SMU_POWER; 517 cmd.len = 8; 518 cmd.data[0] = 'V'; 519 cmd.data[1] = 'S'; 520 cmd.data[2] = 'L'; 521 cmd.data[3] = 'E'; 522 cmd.data[4] = 'W'; 523 cmd.data[5] = 0xff; 524 cmd.data[6] = 1; 525 cmd.data[7] = to; 526 527 smu_run_cmd(dev, &cmd, 1); 528} 529 530static void 531smu_cpufreq_pre_change(device_t dev, const struct cf_level *level) 532{ 533 /* 534 * Make sure the CPU voltage is raised before we raise 535 * the clock. 536 */ 537 538 if (level->rel_set[0].freq == 10000 /* max */) 539 smu_slew_cpu_voltage(dev, 0); 540} 541 542static void 543smu_cpufreq_post_change(device_t dev, const struct cf_level *level) 544{ 545 /* We are safe to reduce CPU voltage after a downward transition */ 546 547 if (level->rel_set[0].freq < 10000 /* max */) 548 smu_slew_cpu_voltage(dev, 1); /* XXX: 1/4 voltage for 970MP? */ 549} 550 551/* Routines for probing the SMU doorbell GPIO */ 552static int doorbell_probe(device_t dev); 553static int doorbell_attach(device_t dev); 554 555static device_method_t doorbell_methods[] = { 556 /* Device interface */ 557 DEVMETHOD(device_probe, doorbell_probe), 558 DEVMETHOD(device_attach, doorbell_attach), 559 { 0, 0 }, 560}; 561 562static driver_t doorbell_driver = { 563 "smudoorbell", 564 doorbell_methods, 565 0 566}; 567 568static devclass_t doorbell_devclass; 569 570DRIVER_MODULE(smudoorbell, macgpio, doorbell_driver, doorbell_devclass, 0, 0); 571 572static int 573doorbell_probe(device_t dev) 574{ 575 const char *name = ofw_bus_get_name(dev); 576 577 if (strcmp(name, "smu-doorbell") != 0) 578 return (ENXIO); 579 580 device_set_desc(dev, "SMU Doorbell GPIO"); 581 device_quiet(dev); 582 return (0); 583} 584 585static int 586doorbell_attach(device_t dev) 587{ 588 smu_doorbell = dev; 589 return (0); 590} 591 592/* 593 * Sensor and fan management 594 */ 595 596static int 597smu_fan_set_rpm(device_t smu, struct smu_fan *fan, int rpm) 598{ 599 struct smu_cmd cmd; 600 int error; 601 602 cmd.cmd = SMU_FAN; 603 error = EIO; 604 605 /* Clamp to allowed range */ 606 rpm = max(fan->min_rpm, rpm); 607 rpm = min(fan->max_rpm, rpm); 608 609 /* 610 * Apple has two fan control mechanisms. We can't distinguish 611 * them except by seeing if the new one fails. If the new one 612 * fails, use the old one. 613 */ 614 615 if (!fan->old_style) { 616 cmd.len = 4; 617 cmd.data[0] = 0x30; 618 cmd.data[1] = fan->reg; 619 cmd.data[2] = (rpm >> 8) & 0xff; 620 cmd.data[3] = rpm & 0xff; 621 622 error = smu_run_cmd(smu, &cmd, 1); 623 if (error) 624 fan->old_style = 1; 625 } 626 627 if (fan->old_style) { 628 cmd.len = 14; 629 cmd.data[0] = 0; 630 cmd.data[1] = 1 << fan->reg; 631 cmd.data[2 + 2*fan->reg] = (rpm >> 8) & 0xff; 632 cmd.data[3 + 2*fan->reg] = rpm & 0xff; 633 error = smu_run_cmd(smu, &cmd, 1); 634 } 635 636 if (error == 0) 637 fan->setpoint = rpm; 638 639 return (error); 640} 641 642static int 643smu_fan_read_rpm(device_t smu, struct smu_fan *fan) 644{ 645 struct smu_cmd cmd; 646 647 cmd.cmd = SMU_FAN; 648 cmd.len = 1; 649 cmd.data[0] = 1; 650 651 smu_run_cmd(smu, &cmd, 1); 652 653 return ((cmd.data[fan->reg*2+1] << 8) | cmd.data[fan->reg*2+2]); 654} 655 656static int 657smu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS) 658{ 659 device_t smu; 660 struct smu_softc *sc; 661 struct smu_fan *fan; 662 int rpm, error; 663 664 smu = arg1; 665 sc = device_get_softc(smu); 666 fan = &sc->sc_fans[arg2]; 667 668 rpm = smu_fan_read_rpm(smu, fan); 669 error = sysctl_handle_int(oidp, &rpm, 0, req); 670 671 if (error || !req->newptr) 672 return (error); 673 674 sc->sc_lastuserchange = time_uptime; 675 676 return (smu_fan_set_rpm(smu, fan, rpm)); 677} 678 679static void 680smu_attach_fans(device_t dev, phandle_t fanroot) 681{ 682 struct smu_fan *fan; 683 struct smu_softc *sc; 684 struct sysctl_oid *oid, *fanroot_oid; 685 struct sysctl_ctx_list *ctx; 686 phandle_t child; 687 char type[32], sysctl_name[32]; 688 int i; 689 690 sc = device_get_softc(dev); 691 sc->sc_nfans = 0; 692 693 for (child = OF_child(fanroot); child != 0; child = OF_peer(child)) 694 sc->sc_nfans++; 695 696 if (sc->sc_nfans == 0) { 697 device_printf(dev, "WARNING: No fans detected!\n"); 698 return; 699 } 700 701 sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct smu_fan), M_SMU, 702 M_WAITOK | M_ZERO); 703 704 fan = sc->sc_fans; 705 sc->sc_nfans = 0; 706 707 ctx = device_get_sysctl_ctx(dev); 708 fanroot_oid = SYSCTL_ADD_NODE(ctx, 709 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "fans", 710 CTLFLAG_RD, 0, "SMU Fan Information"); 711 712 for (child = OF_child(fanroot); child != 0; child = OF_peer(child)) { 713 OF_getprop(child, "device_type", type, sizeof(type)); 714 if (strcmp(type, "fan-rpm-control") != 0) 715 continue; 716 717 fan->old_style = 0; 718 OF_getprop(child, "reg", &fan->reg, sizeof(cell_t)); 719 OF_getprop(child, "min-value", &fan->min_rpm, sizeof(cell_t)); 720 OF_getprop(child, "max-value", &fan->max_rpm, sizeof(cell_t)); 721 722 if (OF_getprop(child, "unmanaged-value", &fan->unmanaged_rpm, 723 sizeof(cell_t)) != sizeof(cell_t)) 724 fan->unmanaged_rpm = fan->max_rpm; 725 726 fan->setpoint = smu_fan_read_rpm(dev, fan); 727 728 OF_getprop(child, "location", fan->location, 729 sizeof(fan->location)); 730 731 /* Add sysctls */ 732 for (i = 0; i < strlen(fan->location); i++) { 733 sysctl_name[i] = tolower(fan->location[i]); 734 if (isspace(sysctl_name[i])) 735 sysctl_name[i] = '_'; 736 } 737 sysctl_name[i] = 0; 738 739 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(fanroot_oid), 740 OID_AUTO, sysctl_name, CTLFLAG_RD, 0, "Fan Information"); 741 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "minrpm", 742 CTLTYPE_INT | CTLFLAG_RD, &fan->min_rpm, sizeof(cell_t), 743 "Minimum allowed RPM"); 744 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "maxrpm", 745 CTLTYPE_INT | CTLFLAG_RD, &fan->max_rpm, sizeof(cell_t), 746 "Maximum allowed RPM"); 747 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "rpm", 748 CTLTYPE_INT | CTLFLAG_RW, dev, sc->sc_nfans, 749 smu_fanrpm_sysctl, "I", "Fan RPM"); 750 751 fan++; 752 sc->sc_nfans++; 753 } 754} 755 756static int 757smu_sensor_read(device_t smu, struct smu_sensor *sens, int *val) 758{ 759 struct smu_cmd cmd; 760 struct smu_softc *sc; 761 int64_t value; 762 int error; 763 764 cmd.cmd = SMU_ADC; 765 cmd.len = 1; 766 cmd.data[0] = sens->reg; 767 error = 0; 768 769 error = smu_run_cmd(smu, &cmd, 1); 770 if (error != 0) 771 return (error); 772 773 sc = device_get_softc(smu); 774 value = (cmd.data[0] << 8) | cmd.data[1]; 775 776 switch (sens->type) { 777 case SMU_TEMP_SENSOR: 778 value *= sc->sc_cpu_diode_scale; 779 value >>= 3; 780 value += ((int64_t)sc->sc_cpu_diode_offset) << 9; 781 value <<= 1; 782 783 /* Convert from 16.16 fixed point degC into integer C. */ 784 value >>= 16; 785 break; 786 case SMU_VOLTAGE_SENSOR: 787 value *= sc->sc_cpu_volt_scale; 788 value += sc->sc_cpu_volt_offset; 789 value <<= 4; 790 791 /* Convert from 16.16 fixed point V into mV. */ 792 value *= 15625; 793 value /= 1024; 794 value /= 1000; 795 break; 796 case SMU_CURRENT_SENSOR: 797 value *= sc->sc_cpu_curr_scale; 798 value += sc->sc_cpu_curr_offset; 799 value <<= 4; 800 801 /* Convert from 16.16 fixed point A into mA. */ 802 value *= 15625; 803 value /= 1024; 804 value /= 1000; 805 break; 806 case SMU_POWER_SENSOR: 807 value *= sc->sc_slots_pow_scale; 808 value += sc->sc_slots_pow_offset; 809 value <<= 4; 810 811 /* Convert from 16.16 fixed point W into mW. */ 812 value *= 15625; 813 value /= 1024; 814 value /= 1000; 815 break; 816 } 817 818 *val = value; 819 return (0); 820} 821 822static int 823smu_sensor_sysctl(SYSCTL_HANDLER_ARGS) 824{ 825 device_t smu; 826 struct smu_softc *sc; 827 struct smu_sensor *sens; 828 int value, error; 829 830 smu = arg1; 831 sc = device_get_softc(smu); 832 sens = &sc->sc_sensors[arg2]; 833 834 error = smu_sensor_read(smu, sens, &value); 835 if (error != 0) 836 return (error); 837 838 error = sysctl_handle_int(oidp, &value, 0, req); 839 840 return (error); 841} 842 843static void 844smu_attach_sensors(device_t dev, phandle_t sensroot) 845{ 846 struct smu_sensor *sens; 847 struct smu_softc *sc; 848 struct sysctl_oid *sensroot_oid; 849 struct sysctl_ctx_list *ctx; 850 phandle_t child; 851 char type[32]; 852 int i; 853 854 sc = device_get_softc(dev); 855 sc->sc_nsensors = 0; 856 857 for (child = OF_child(sensroot); child != 0; child = OF_peer(child)) 858 sc->sc_nsensors++; 859 860 if (sc->sc_nsensors == 0) { 861 device_printf(dev, "WARNING: No sensors detected!\n"); 862 return; 863 } 864 865 sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor), 866 M_SMU, M_WAITOK | M_ZERO); 867 868 sens = sc->sc_sensors; 869 sc->sc_nsensors = 0; 870 871 ctx = device_get_sysctl_ctx(dev); 872 sensroot_oid = SYSCTL_ADD_NODE(ctx, 873 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensors", 874 CTLFLAG_RD, 0, "SMU Sensor Information"); 875 876 for (child = OF_child(sensroot); child != 0; child = OF_peer(child)) { 877 char sysctl_name[40], sysctl_desc[40]; 878 const char *units; 879 880 OF_getprop(child, "device_type", type, sizeof(type)); 881 882 if (strcmp(type, "current-sensor") == 0) { 883 sens->type = SMU_CURRENT_SENSOR; 884 units = "mA"; 885 } else if (strcmp(type, "temp-sensor") == 0) { 886 sens->type = SMU_TEMP_SENSOR; 887 units = "C"; 888 } else if (strcmp(type, "voltage-sensor") == 0) { 889 sens->type = SMU_VOLTAGE_SENSOR; 890 units = "mV"; 891 } else if (strcmp(type, "power-sensor") == 0) { 892 sens->type = SMU_POWER_SENSOR; 893 units = "mW"; 894 } else { 895 continue; 896 } 897 898 OF_getprop(child, "reg", &sens->reg, sizeof(cell_t)); 899 OF_getprop(child, "location", sens->location, 900 sizeof(sens->location)); 901 902 for (i = 0; i < strlen(sens->location); i++) { 903 sysctl_name[i] = tolower(sens->location[i]); 904 if (isspace(sysctl_name[i])) 905 sysctl_name[i] = '_'; 906 } 907 sysctl_name[i] = 0; 908 909 sprintf(sysctl_desc,"%s (%s)", sens->location, units); 910 911 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO, 912 sysctl_name, CTLTYPE_INT | CTLFLAG_RD, dev, sc->sc_nsensors, 913 smu_sensor_sysctl, "I", sysctl_desc); 914 915 sens++; 916 sc->sc_nsensors++; 917 } 918} 919 920static void 921smu_fan_management_proc(void *xdev) 922{ 923 device_t smu = xdev; 924 925 while(1) { 926 smu_manage_fans(smu); 927 pause("smu", SMU_FANMGT_INTERVAL * hz / 1000); 928 } 929} 930 931static void 932smu_manage_fans(device_t smu) 933{ 934 struct smu_softc *sc; 935 int i, maxtemp, temp, factor, error; 936 937 sc = device_get_softc(smu); 938 939 maxtemp = 0; 940 for (i = 0; i < sc->sc_nsensors; i++) { 941 if (sc->sc_sensors[i].type != SMU_TEMP_SENSOR) 942 continue; 943 944 error = smu_sensor_read(smu, &sc->sc_sensors[i], &temp); 945 if (error == 0 && temp > maxtemp) 946 maxtemp = temp; 947 } 948 949 if (maxtemp < 10) { /* Bail if no good sensors */ 950 for (i = 0; i < sc->sc_nfans; i++) 951 smu_fan_set_rpm(smu, &sc->sc_fans[i], 952 sc->sc_fans[i].unmanaged_rpm); 953 return; 954 } 955 956 if (maxtemp > sc->sc_critical_temp) { 957 device_printf(smu, "WARNING: Current system temperature (%d C) " 958 "exceeds critical temperature (%d C)! Shutting down!\n", 959 maxtemp, sc->sc_critical_temp); 960 shutdown_nice(RB_POWEROFF); 961 } 962 963 if (maxtemp - sc->sc_target_temp > 20) 964 device_printf(smu, "WARNING: Current system temperature (%d C) " 965 "more than 20 degrees over target temperature (%d C)!\n", 966 maxtemp, sc->sc_target_temp); 967 968 if (time_uptime - sc->sc_lastuserchange < 3) { 969 /* 970 * If we have heard from a user process in the last 3 seconds, 971 * go away. 972 */ 973 974 return; 975 } 976 977 if (maxtemp - sc->sc_target_temp > 4) 978 factor = 110; 979 else if (maxtemp - sc->sc_target_temp > 1) 980 factor = 105; 981 else if (sc->sc_target_temp - maxtemp > 4) 982 factor = 90; 983 else if (sc->sc_target_temp - maxtemp > 1) 984 factor = 95; 985 else 986 factor = 100; 987 988 for (i = 0; i < sc->sc_nfans; i++) 989 smu_fan_set_rpm(smu, &sc->sc_fans[i], 990 (sc->sc_fans[i].setpoint * factor) / 100); 991} 992 993static void 994smu_set_sleepled(void *xdev, int onoff) 995{ 996 static struct smu_cmd cmd; 997 device_t smu = xdev; 998 999 cmd.cmd = SMU_MISC; 1000 cmd.len = 3; 1001 cmd.data[0] = SMU_MISC_LED_CTRL; 1002 cmd.data[1] = 0; 1003 cmd.data[2] = onoff; 1004 1005 smu_run_cmd(smu, &cmd, 0); 1006} 1007 1008static int 1009smu_server_mode(SYSCTL_HANDLER_ARGS) 1010{ 1011 struct smu_cmd cmd; 1012 u_int server_mode; 1013 device_t smu = arg1; 1014 int error; 1015 1016 cmd.cmd = SMU_POWER_EVENTS; 1017 cmd.len = 1; 1018 cmd.data[0] = SMU_PWR_GET_POWERUP; 1019 1020 error = smu_run_cmd(smu, &cmd, 1); 1021 1022 if (error) 1023 return (error); 1024 1025 server_mode = (cmd.data[1] & SMU_WAKEUP_AC_INSERT) ? 1 : 0; 1026 1027 error = sysctl_handle_int(oidp, &server_mode, 0, req); 1028 1029 if (error || !req->newptr) 1030 return (error); 1031 1032 if (server_mode == 1) 1033 cmd.data[0] = SMU_PWR_SET_POWERUP; 1034 else if (server_mode == 0) 1035 cmd.data[0] = SMU_PWR_CLR_POWERUP; 1036 else 1037 return (EINVAL); 1038 1039 cmd.len = 3; 1040 cmd.data[1] = 0; 1041 cmd.data[2] = SMU_WAKEUP_AC_INSERT; 1042 1043 return (smu_run_cmd(smu, &cmd, 1)); 1044} 1045 1046