2 * Copyright (c) 2009 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
37 #include <sys/ctype.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
40 #include <sys/reboot.h>
42 #include <sys/sysctl.h>
43 #include <sys/unistd.h>
45 #include <machine/bus.h>
46 #include <machine/intr_machdep.h>
47 #include <machine/md_var.h>
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>
59 STAILQ_ENTRY(smu_cmd) cmd_q;
62 STAILQ_HEAD(smu_cmdq, smu_cmd);
90 struct resource *sc_memr;
93 bus_dma_tag_t sc_dmatag;
94 bus_space_tag_t sc_bt;
95 bus_space_handle_t sc_mailbox;
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;
102 struct smu_fan *sc_fans;
104 struct smu_sensor *sc_sensors;
107 int sc_doorbellirqid;
108 struct resource *sc_doorbellirq;
109 void *sc_doorbellirqcookie;
111 struct proc *sc_fanmgt_proc;
112 time_t sc_lastuserchange;
114 /* Calibration data */
115 uint16_t sc_cpu_diode_scale;
116 int16_t sc_cpu_diode_offset;
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;
123 uint16_t sc_slots_pow_scale;
124 int16_t sc_slots_pow_offset;
126 /* Thermal management parameters */
127 int sc_target_temp; /* Default 55 C */
128 int sc_critical_temp; /* Default 90 C */
130 struct cdev *sc_leddev;
133 /* regular bus attachment functions */
135 static int smu_probe(device_t);
136 static int smu_attach(device_t);
138 /* cpufreq notification hooks */
140 static void smu_cpufreq_pre_change(device_t, const struct cf_level *level);
141 static void smu_cpufreq_post_change(device_t, const struct cf_level *level);
143 /* utility functions */
144 static int smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait);
145 static int smu_get_datablock(device_t dev, int8_t id, uint8_t *buf,
147 static void smu_attach_fans(device_t dev, phandle_t fanroot);
148 static void smu_attach_sensors(device_t dev, phandle_t sensroot);
149 static void smu_fan_management_proc(void *xdev);
150 static void smu_manage_fans(device_t smu);
151 static void smu_set_sleepled(void *xdev, int onoff);
152 static int smu_server_mode(SYSCTL_HANDLER_ARGS);
153 static void smu_doorbell_intr(void *xdev);
155 /* where to find the doorbell GPIO */
157 static device_t smu_doorbell = NULL;
159 static device_method_t smu_methods[] = {
160 /* Device interface */
161 DEVMETHOD(device_probe, smu_probe),
162 DEVMETHOD(device_attach, smu_attach),
166 static driver_t smu_driver = {
169 sizeof(struct smu_softc)
172 static devclass_t smu_devclass;
174 DRIVER_MODULE(smu, nexus, smu_driver, smu_devclass, 0, 0);
175 MALLOC_DEFINE(M_SMU, "smu", "SMU Sensor Information");
177 #define SMU_MAILBOX 0x8000860c
178 #define SMU_FANMGT_INTERVAL 1000 /* ms */
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
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
203 #define SMU_CPUTEMP_CAL 0x18
204 #define SMU_CPUVOLT_CAL 0x21
205 #define SMU_SLOTPW_CAL 0x78
208 #define SMU_PARTITION 0x3e
209 #define SMU_PARTITION_LATEST 0x01
210 #define SMU_PARTITION_BASE 0x02
211 #define SMU_PARTITION_UPDATE 0x03
214 smu_probe(device_t dev)
216 const char *name = ofw_bus_get_name(dev);
218 if (strcmp(name, "smu") != 0)
221 device_set_desc(dev, "Apple System Management Unit");
226 smu_phys_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
228 struct smu_softc *sc = xsc;
230 sc->sc_cmd_phys = segs[0].ds_addr;
234 smu_attach(device_t dev)
236 struct smu_softc *sc;
237 phandle_t node, child;
240 sc = device_get_softc(dev);
242 mtx_init(&sc->sc_mtx, "smu", NULL, MTX_DEF);
243 sc->sc_cur_cmd = NULL;
244 sc->sc_doorbellirqid = -1;
247 * Map the mailbox area. This should be determined from firmware,
248 * but I have not found a simple way to do that.
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);
257 * Allocate the command buffer. This can be anywhere in the low 4 GB
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);
267 * Set up handlers to change CPU voltage when CPU frequency is changed.
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);
275 * Detect and attach child devices.
277 node = ofw_bus_get_node(dev);
278 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
280 memset(name, 0, sizeof(name));
281 OF_getprop(child, "name", name, sizeof(name));
283 if (strncmp(name, "rpm-fans", 9) == 0 ||
284 strncmp(name, "fans", 5) == 0)
285 smu_attach_fans(dev, child);
287 if (strncmp(name, "sensors", 8) == 0)
288 smu_attach_sensors(dev, child);
292 * Collect calibration constants.
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];
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];
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];
309 * Set up simple-minded thermal management.
311 sc->sc_target_temp = 55;
312 sc->sc_critical_temp = 90;
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)");
323 kproc_create(smu_fan_management_proc, dev, &sc->sc_fanmgt_proc,
324 RFHIGHPID, 0, "smu_thermal");
327 * Set up LED interface
329 sc->sc_leddev = led_create(smu_set_sleepled, dev, "sleepled");
332 * Reset on power loss behavior
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");
341 * Set up doorbell interrupt.
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);
356 smu_send_cmd(device_t dev, struct smu_cmd *cmd)
358 struct smu_softc *sc;
360 sc = device_get_softc(dev);
362 mtx_assert(&sc->sc_mtx, MA_OWNED);
364 powerpc_pow_enabled = 0; /* SMU cannot work if we go to NAP */
365 sc->sc_cur_cmd = cmd;
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);
374 /* Flush the cacheline it is in -- SMU bypasses the cache */
375 __asm __volatile("sync; dcbf 0,%0; sync" :: "r"(sc->sc_cmd): "memory");
377 /* Ring SMU doorbell */
378 macgpio_write(smu_doorbell, GPIO_DDR_OUTPUT);
382 smu_doorbell_intr(void *xdev)
385 struct smu_softc *sc;
389 doorbell_ack = macgpio_read(smu_doorbell);
390 sc = device_get_softc(smu);
392 if (doorbell_ack != (GPIO_DDR_OUTPUT | GPIO_LEVEL_RO | GPIO_DATA))
395 mtx_lock(&sc->sc_mtx);
397 if (sc->sc_cur_cmd == NULL) /* spurious */
400 /* Check result. First invalidate the cache again... */
401 __asm __volatile("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory");
403 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_POSTREAD);
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;
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);
421 mtx_unlock(&sc->sc_mtx);
425 smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait)
427 struct smu_softc *sc;
431 sc = device_get_softc(dev);
434 mtx_lock(&sc->sc_mtx);
435 if (sc->sc_cur_cmd != NULL) {
436 STAILQ_INSERT_TAIL(&sc->sc_cmdq, cmd, cmd_q);
438 smu_send_cmd(dev, cmd);
439 mtx_unlock(&sc->sc_mtx);
444 if (sc->sc_doorbellirqid < 0) {
445 /* Poll if the IRQ has not been set up yet */
448 smu_doorbell_intr(dev);
449 } while (sc->sc_cur_cmd != NULL);
451 /* smu_doorbell_intr will wake us when the command is ACK'ed */
452 error = tsleep(cmd, 0, "smu", 800 * hz / 1000);
454 smu_doorbell_intr(dev); /* One last chance */
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;
463 STAILQ_REMOVE(&sc->sc_cmdq, cmd, smu_cmd,
465 mtx_unlock(&sc->sc_mtx);
469 mtx_unlock(&sc->sc_mtx);
473 /* SMU acks the command by inverting the command bits */
474 if (cmd->cmd == ((~cmd_code) & 0xff))
483 smu_get_datablock(device_t dev, int8_t id, uint8_t *buf, size_t len)
488 cmd.cmd = SMU_PARTITION;
490 cmd.data[0] = SMU_PARTITION_LATEST;
493 smu_run_cmd(dev, &cmd, 1);
495 addr[0] = addr[1] = 0;
496 addr[2] = cmd.data[0];
497 addr[3] = cmd.data[1];
501 cmd.data[0] = SMU_MISC_GET_DATA;
502 cmd.data[1] = sizeof(addr);
503 memcpy(&cmd.data[2], addr, sizeof(addr));
506 smu_run_cmd(dev, &cmd, 1);
507 memcpy(buf, cmd.data, len);
512 smu_slew_cpu_voltage(device_t dev, int to)
527 smu_run_cmd(dev, &cmd, 1);
531 smu_cpufreq_pre_change(device_t dev, const struct cf_level *level)
534 * Make sure the CPU voltage is raised before we raise
538 if (level->rel_set[0].freq == 10000 /* max */)
539 smu_slew_cpu_voltage(dev, 0);
543 smu_cpufreq_post_change(device_t dev, const struct cf_level *level)
545 /* We are safe to reduce CPU voltage after a downward transition */
547 if (level->rel_set[0].freq < 10000 /* max */)
548 smu_slew_cpu_voltage(dev, 1); /* XXX: 1/4 voltage for 970MP? */
551 /* Routines for probing the SMU doorbell GPIO */
552 static int doorbell_probe(device_t dev);
553 static int doorbell_attach(device_t dev);
555 static device_method_t doorbell_methods[] = {
556 /* Device interface */
557 DEVMETHOD(device_probe, doorbell_probe),
558 DEVMETHOD(device_attach, doorbell_attach),
562 static driver_t doorbell_driver = {
568 static devclass_t doorbell_devclass;
570 DRIVER_MODULE(smudoorbell, macgpio, doorbell_driver, doorbell_devclass, 0, 0);
573 doorbell_probe(device_t dev)
575 const char *name = ofw_bus_get_name(dev);
577 if (strcmp(name, "smu-doorbell") != 0)
580 device_set_desc(dev, "SMU Doorbell GPIO");
586 doorbell_attach(device_t dev)
593 * Sensor and fan management
597 smu_fan_set_rpm(device_t smu, struct smu_fan *fan, int rpm)
605 /* Clamp to allowed range */
606 rpm = max(fan->min_rpm, rpm);
607 rpm = min(fan->max_rpm, rpm);
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.
615 if (!fan->old_style) {
618 cmd.data[1] = fan->reg;
619 cmd.data[2] = (rpm >> 8) & 0xff;
620 cmd.data[3] = rpm & 0xff;
622 error = smu_run_cmd(smu, &cmd, 1);
627 if (fan->old_style) {
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);
643 smu_fan_read_rpm(device_t smu, struct smu_fan *fan)
648 if (!fan->old_style) {
652 cmd.data[1] = fan->reg;
654 error = smu_run_cmd(smu, &cmd, 1);
658 rpm = (cmd.data[0] << 8) | cmd.data[1];
661 if (fan->old_style) {
666 error = smu_run_cmd(smu, &cmd, 1);
670 rpm = (cmd.data[fan->reg*2+1] << 8) | cmd.data[fan->reg*2+2];
677 smu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
680 struct smu_softc *sc;
685 sc = device_get_softc(smu);
686 fan = &sc->sc_fans[arg2];
688 rpm = smu_fan_read_rpm(smu, fan);
692 error = sysctl_handle_int(oidp, &rpm, 0, req);
694 if (error || !req->newptr)
697 sc->sc_lastuserchange = time_uptime;
699 return (smu_fan_set_rpm(smu, fan, rpm));
703 smu_attach_fans(device_t dev, phandle_t fanroot)
706 struct smu_softc *sc;
707 struct sysctl_oid *oid, *fanroot_oid;
708 struct sysctl_ctx_list *ctx;
710 char type[32], sysctl_name[32];
713 sc = device_get_softc(dev);
716 for (child = OF_child(fanroot); child != 0; child = OF_peer(child))
719 if (sc->sc_nfans == 0) {
720 device_printf(dev, "WARNING: No fans detected!\n");
724 sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct smu_fan), M_SMU,
730 ctx = device_get_sysctl_ctx(dev);
731 fanroot_oid = SYSCTL_ADD_NODE(ctx,
732 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "fans",
733 CTLFLAG_RD, 0, "SMU Fan Information");
735 for (child = OF_child(fanroot); child != 0; child = OF_peer(child)) {
736 OF_getprop(child, "device_type", type, sizeof(type));
737 if (strcmp(type, "fan-rpm-control") != 0)
741 OF_getprop(child, "reg", &fan->reg, sizeof(cell_t));
742 OF_getprop(child, "min-value", &fan->min_rpm, sizeof(cell_t));
743 OF_getprop(child, "max-value", &fan->max_rpm, sizeof(cell_t));
745 if (OF_getprop(child, "unmanaged-value", &fan->unmanaged_rpm,
746 sizeof(cell_t)) != sizeof(cell_t))
747 fan->unmanaged_rpm = fan->max_rpm;
749 fan->setpoint = smu_fan_read_rpm(dev, fan);
751 OF_getprop(child, "location", fan->location,
752 sizeof(fan->location));
755 for (i = 0; i < strlen(fan->location); i++) {
756 sysctl_name[i] = tolower(fan->location[i]);
757 if (isspace(sysctl_name[i]))
758 sysctl_name[i] = '_';
762 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(fanroot_oid),
763 OID_AUTO, sysctl_name, CTLFLAG_RD, 0, "Fan Information");
764 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "minrpm",
765 CTLTYPE_INT | CTLFLAG_RD, &fan->min_rpm, sizeof(cell_t),
766 "Minimum allowed RPM");
767 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "maxrpm",
768 CTLTYPE_INT | CTLFLAG_RD, &fan->max_rpm, sizeof(cell_t),
769 "Maximum allowed RPM");
770 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "rpm",
771 CTLTYPE_INT | CTLFLAG_RW, dev, sc->sc_nfans,
772 smu_fanrpm_sysctl, "I", "Fan RPM");
780 smu_sensor_read(device_t smu, struct smu_sensor *sens, int *val)
783 struct smu_softc *sc;
789 cmd.data[0] = sens->reg;
792 error = smu_run_cmd(smu, &cmd, 1);
796 sc = device_get_softc(smu);
797 value = (cmd.data[0] << 8) | cmd.data[1];
799 switch (sens->type) {
800 case SMU_TEMP_SENSOR:
801 value *= sc->sc_cpu_diode_scale;
803 value += ((int64_t)sc->sc_cpu_diode_offset) << 9;
806 /* Convert from 16.16 fixed point degC into integer C. */
809 case SMU_VOLTAGE_SENSOR:
810 value *= sc->sc_cpu_volt_scale;
811 value += sc->sc_cpu_volt_offset;
814 /* Convert from 16.16 fixed point V into mV. */
819 case SMU_CURRENT_SENSOR:
820 value *= sc->sc_cpu_curr_scale;
821 value += sc->sc_cpu_curr_offset;
824 /* Convert from 16.16 fixed point A into mA. */
829 case SMU_POWER_SENSOR:
830 value *= sc->sc_slots_pow_scale;
831 value += sc->sc_slots_pow_offset;
834 /* Convert from 16.16 fixed point W into mW. */
846 smu_sensor_sysctl(SYSCTL_HANDLER_ARGS)
849 struct smu_softc *sc;
850 struct smu_sensor *sens;
854 sc = device_get_softc(smu);
855 sens = &sc->sc_sensors[arg2];
857 error = smu_sensor_read(smu, sens, &value);
861 error = sysctl_handle_int(oidp, &value, 0, req);
867 smu_attach_sensors(device_t dev, phandle_t sensroot)
869 struct smu_sensor *sens;
870 struct smu_softc *sc;
871 struct sysctl_oid *sensroot_oid;
872 struct sysctl_ctx_list *ctx;
877 sc = device_get_softc(dev);
880 for (child = OF_child(sensroot); child != 0; child = OF_peer(child))
883 if (sc->sc_nsensors == 0) {
884 device_printf(dev, "WARNING: No sensors detected!\n");
888 sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor),
889 M_SMU, M_WAITOK | M_ZERO);
891 sens = sc->sc_sensors;
894 ctx = device_get_sysctl_ctx(dev);
895 sensroot_oid = SYSCTL_ADD_NODE(ctx,
896 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensors",
897 CTLFLAG_RD, 0, "SMU Sensor Information");
899 for (child = OF_child(sensroot); child != 0; child = OF_peer(child)) {
900 char sysctl_name[40], sysctl_desc[40];
903 OF_getprop(child, "device_type", type, sizeof(type));
905 if (strcmp(type, "current-sensor") == 0) {
906 sens->type = SMU_CURRENT_SENSOR;
908 } else if (strcmp(type, "temp-sensor") == 0) {
909 sens->type = SMU_TEMP_SENSOR;
911 } else if (strcmp(type, "voltage-sensor") == 0) {
912 sens->type = SMU_VOLTAGE_SENSOR;
914 } else if (strcmp(type, "power-sensor") == 0) {
915 sens->type = SMU_POWER_SENSOR;
921 OF_getprop(child, "reg", &sens->reg, sizeof(cell_t));
922 OF_getprop(child, "location", sens->location,
923 sizeof(sens->location));
925 for (i = 0; i < strlen(sens->location); i++) {
926 sysctl_name[i] = tolower(sens->location[i]);
927 if (isspace(sysctl_name[i]))
928 sysctl_name[i] = '_';
932 sprintf(sysctl_desc,"%s (%s)", sens->location, units);
934 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO,
935 sysctl_name, CTLTYPE_INT | CTLFLAG_RD, dev, sc->sc_nsensors,
936 smu_sensor_sysctl, "I", sysctl_desc);
944 smu_fan_management_proc(void *xdev)
949 smu_manage_fans(smu);
950 pause("smu", SMU_FANMGT_INTERVAL * hz / 1000);
955 smu_manage_fans(device_t smu)
957 struct smu_softc *sc;
958 int i, maxtemp, temp, factor, error;
960 sc = device_get_softc(smu);
963 for (i = 0; i < sc->sc_nsensors; i++) {
964 if (sc->sc_sensors[i].type != SMU_TEMP_SENSOR)
967 error = smu_sensor_read(smu, &sc->sc_sensors[i], &temp);
968 if (error == 0 && temp > maxtemp)
972 if (maxtemp < 10) { /* Bail if no good sensors */
973 for (i = 0; i < sc->sc_nfans; i++)
974 smu_fan_set_rpm(smu, &sc->sc_fans[i],
975 sc->sc_fans[i].unmanaged_rpm);
979 if (maxtemp > sc->sc_critical_temp) {
980 device_printf(smu, "WARNING: Current system temperature (%d C) "
981 "exceeds critical temperature (%d C)! Shutting down!\n",
982 maxtemp, sc->sc_critical_temp);
983 shutdown_nice(RB_POWEROFF);
986 if (maxtemp - sc->sc_target_temp > 20)
987 device_printf(smu, "WARNING: Current system temperature (%d C) "
988 "more than 20 degrees over target temperature (%d C)!\n",
989 maxtemp, sc->sc_target_temp);
991 if (time_uptime - sc->sc_lastuserchange < 3) {
993 * If we have heard from a user process in the last 3 seconds,
1000 if (maxtemp - sc->sc_target_temp > 4)
1002 else if (maxtemp - sc->sc_target_temp > 1)
1004 else if (sc->sc_target_temp - maxtemp > 4)
1006 else if (sc->sc_target_temp - maxtemp > 1)
1011 for (i = 0; i < sc->sc_nfans; i++)
1012 smu_fan_set_rpm(smu, &sc->sc_fans[i],
1013 (sc->sc_fans[i].setpoint * factor) / 100);
1017 smu_set_sleepled(void *xdev, int onoff)
1019 static struct smu_cmd cmd;
1020 device_t smu = xdev;
1024 cmd.data[0] = SMU_MISC_LED_CTRL;
1026 cmd.data[2] = onoff;
1028 smu_run_cmd(smu, &cmd, 0);
1032 smu_server_mode(SYSCTL_HANDLER_ARGS)
1036 device_t smu = arg1;
1039 cmd.cmd = SMU_POWER_EVENTS;
1041 cmd.data[0] = SMU_PWR_GET_POWERUP;
1043 error = smu_run_cmd(smu, &cmd, 1);
1048 server_mode = (cmd.data[1] & SMU_WAKEUP_AC_INSERT) ? 1 : 0;
1050 error = sysctl_handle_int(oidp, &server_mode, 0, req);
1052 if (error || !req->newptr)
1055 if (server_mode == 1)
1056 cmd.data[0] = SMU_PWR_SET_POWERUP;
1057 else if (server_mode == 0)
1058 cmd.data[0] = SMU_PWR_CLR_POWERUP;
1064 cmd.data[2] = SMU_WAKEUP_AC_INSERT;
1066 return (smu_run_cmd(smu, &cmd, 1));